]>
Commit | Line | Data |
---|---|---|
a405b86d | 1 | /** @file\r |
2 | Main file for Parse shell level 2 function.\r | |
3 | \r | |
c011b6c9 | 4 | (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.<BR>\r |
8fcf74a8 | 5 | Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>\r |
a405b86d | 6 | This program and the accompanying materials\r |
7 | are licensed and made available under the terms and conditions of the BSD License\r | |
8 | which accompanies this distribution. The full text of the license may be found at\r | |
9 | http://opensource.org/licenses/bsd-license.php\r | |
10 | \r | |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | \r | |
14 | **/\r | |
15 | \r | |
16 | #include "UefiShellLevel2CommandsLib.h"\r | |
17 | \r | |
421fbf99 TS |
18 | /**\r |
19 | Check if data is coming from StdIn output.\r | |
20 | \r | |
21 | @param[in] None\r | |
22 | \r | |
23 | @retval TRUE StdIn stream data available to parse \r | |
24 | @retval FALSE StdIn stream data is not available to parse. \r | |
25 | **/\r | |
26 | BOOLEAN\r | |
27 | IsStdInDataAvailable (\r | |
28 | VOID\r | |
29 | )\r | |
30 | {\r | |
31 | SHELL_FILE_HANDLE FileHandle;\r | |
32 | EFI_STATUS Status;\r | |
33 | CHAR16 CharBuffer; \r | |
34 | UINTN CharSize;\r | |
35 | UINT64 OriginalFilePosition;\r | |
36 | \r | |
37 | Status = EFI_SUCCESS; \r | |
38 | FileHandle = NULL;\r | |
39 | OriginalFilePosition = 0;\r | |
40 | \r | |
41 | if (ShellOpenFileByName (L">i", &FileHandle, EFI_FILE_MODE_READ, 0) == EFI_SUCCESS) {\r | |
42 | CharSize = sizeof(CHAR16);\r | |
43 | gEfiShellProtocol->GetFilePosition (FileHandle, &OriginalFilePosition);\r | |
44 | Status = gEfiShellProtocol->ReadFile (FileHandle, &CharSize, &CharBuffer);\r | |
45 | if (EFI_ERROR (Status) || (CharSize != sizeof(CHAR16))) {\r | |
46 | return FALSE;\r | |
47 | }\r | |
48 | gEfiShellProtocol->SetFilePosition(FileHandle, OriginalFilePosition);\r | |
49 | }\r | |
50 | \r | |
51 | if (FileHandle == NULL) {\r | |
52 | return FALSE;\r | |
53 | } else {\r | |
54 | return TRUE;\r | |
55 | }\r | |
56 | }\r | |
57 | \r | |
58 | /**\r | |
59 | Function to read a single line (up to but not including the \n) using StdIn data from a SHELL_FILE_HANDLE.\r | |
60 | \r | |
61 | If the position upon start is 0, then the Ascii Boolean will be set. This should be\r | |
62 | maintained and not changed for all operations with the same file.\r | |
63 | \r | |
64 | @param[in] Handle SHELL_FILE_HANDLE to read from.\r | |
65 | @param[in, out] Buffer The pointer to buffer to read into.\r | |
66 | @param[in, out] Size The pointer to number of bytes in Buffer.\r | |
67 | @param[in] Truncate If the buffer is large enough, this has no effect.\r | |
68 | If the buffer is is too small and Truncate is TRUE,\r | |
69 | the line will be truncated.\r | |
70 | If the buffer is is too small and Truncate is FALSE,\r | |
71 | then no read will occur.\r | |
72 | \r | |
73 | @retval EFI_SUCCESS The operation was successful. The line is stored in\r | |
74 | Buffer.\r | |
75 | @retval EFI_INVALID_PARAMETER Handle was NULL.\r | |
76 | @retval EFI_INVALID_PARAMETER Size was NULL.\r | |
77 | @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.\r | |
78 | Size was updated to the minimum space required.\r | |
79 | **/\r | |
80 | EFI_STATUS\r | |
81 | EFIAPI\r | |
82 | ShellFileHandleReadStdInLine(\r | |
83 | IN SHELL_FILE_HANDLE Handle,\r | |
84 | IN OUT CHAR16 *Buffer,\r | |
85 | IN OUT UINTN *Size,\r | |
86 | IN BOOLEAN Truncate\r | |
87 | )\r | |
88 | {\r | |
89 | EFI_STATUS Status;\r | |
90 | CHAR16 CharBuffer;\r | |
91 | UINTN CharSize;\r | |
92 | UINTN CountSoFar;\r | |
93 | UINT64 OriginalFilePosition;\r | |
94 | \r | |
95 | \r | |
96 | if (Handle == NULL\r | |
97 | ||Size == NULL\r | |
98 | ){\r | |
99 | return (EFI_INVALID_PARAMETER);\r | |
100 | }\r | |
101 | if (Buffer == NULL) {\r | |
102 | ASSERT(*Size == 0);\r | |
103 | } else {\r | |
104 | *Buffer = CHAR_NULL;\r | |
105 | }\r | |
106 | gEfiShellProtocol->GetFilePosition (Handle, &OriginalFilePosition);\r | |
107 | \r | |
108 | for (CountSoFar = 0;;CountSoFar++){\r | |
109 | CharBuffer = 0;\r | |
110 | CharSize = sizeof(CHAR16);\r | |
111 | Status = gEfiShellProtocol->ReadFile (Handle, &CharSize, &CharBuffer);\r | |
112 | if ( EFI_ERROR(Status)\r | |
113 | || CharSize == 0\r | |
114 | || (CharBuffer == L'\n')\r | |
115 | ){\r | |
116 | break;\r | |
117 | }\r | |
118 | //\r | |
119 | // if we have space save it...\r | |
120 | //\r | |
121 | if ((CountSoFar+1)*sizeof(CHAR16) < *Size){\r | |
122 | ASSERT(Buffer != NULL);\r | |
123 | ((CHAR16*)Buffer)[CountSoFar] = CharBuffer;\r | |
124 | ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL;\r | |
125 | }\r | |
126 | }\r | |
127 | \r | |
128 | //\r | |
129 | // if we ran out of space tell when...\r | |
130 | //\r | |
131 | if ((CountSoFar+1)*sizeof(CHAR16) > *Size){\r | |
132 | *Size = (CountSoFar+1)*sizeof(CHAR16);\r | |
133 | if (!Truncate) {\r | |
134 | gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);\r | |
135 | } else {\r | |
136 | DEBUG((DEBUG_WARN, "The line was truncated in ShellFileHandleReadLine"));\r | |
137 | }\r | |
138 | return (EFI_BUFFER_TOO_SMALL);\r | |
139 | }\r | |
140 | while(Buffer[StrLen(Buffer)-1] == L'\r') {\r | |
141 | Buffer[StrLen(Buffer)-1] = CHAR_NULL;\r | |
142 | }\r | |
143 | \r | |
144 | return (Status);\r | |
145 | }\r | |
146 | \r | |
147 | \r | |
148 | /**\r | |
149 | Function to read a single line using StdIn from a SHELL_FILE_HANDLE. The \n is not included in the returned\r | |
150 | buffer. The returned buffer must be callee freed.\r | |
151 | \r | |
152 | If the position upon start is 0, then the Ascii Boolean will be set. This should be\r | |
153 | maintained and not changed for all operations with the same file.\r | |
154 | \r | |
155 | @param[in] Handle SHELL_FILE_HANDLE to read from.\r | |
156 | \r | |
157 | @return The line of text from the file.\r | |
158 | @retval NULL There was not enough memory available.\r | |
159 | \r | |
160 | @sa ShellFileHandleReadLine\r | |
161 | **/\r | |
162 | CHAR16*\r | |
163 | EFIAPI\r | |
164 | ParseReturnStdInLine (\r | |
165 | IN SHELL_FILE_HANDLE Handle\r | |
166 | )\r | |
167 | {\r | |
168 | CHAR16 *RetVal;\r | |
169 | UINTN Size;\r | |
170 | EFI_STATUS Status;\r | |
171 | \r | |
172 | Size = 0;\r | |
173 | RetVal = NULL;\r | |
174 | \r | |
175 | Status = ShellFileHandleReadStdInLine (Handle, RetVal, &Size, FALSE);\r | |
176 | if (Status == EFI_BUFFER_TOO_SMALL) {\r | |
177 | RetVal = AllocateZeroPool(Size);\r | |
178 | if (RetVal == NULL) {\r | |
179 | return (NULL);\r | |
180 | }\r | |
181 | Status = ShellFileHandleReadStdInLine (Handle, RetVal, &Size, FALSE);\r | |
182 | \r | |
183 | }\r | |
184 | if (EFI_ERROR(Status) && (RetVal != NULL)) {\r | |
185 | FreePool(RetVal);\r | |
186 | RetVal = NULL;\r | |
187 | }\r | |
188 | return (RetVal);\r | |
189 | }\r | |
190 | \r | |
307f2ce4 TS |
191 | /**\r |
192 | Handle stings for SFO Output with escape character ^ in a string\r | |
193 | Content-type: text/html ]>