]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellLevel2CommandsLib/Parse.c
545b7af3427f0a77edb336b18bf80712eb7d2692
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Parse.c
1 /** @file
2 Main file for Parse shell level 2 function.
3
4 (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "UefiShellLevel2CommandsLib.h"
17
18 /**
19 Check if data is coming from StdIn output.
20
21 @param[in] None
22
23 @retval TRUE StdIn stream data available to parse
24 @retval FALSE StdIn stream data is not available to parse.
25 **/
26 BOOLEAN
27 IsStdInDataAvailable (
28 VOID
29 )
30 {
31 SHELL_FILE_HANDLE FileHandle;
32 EFI_STATUS Status;
33 CHAR16 CharBuffer;
34 UINTN CharSize;
35 UINT64 OriginalFilePosition;
36
37 Status = EFI_SUCCESS;
38 FileHandle = NULL;
39 OriginalFilePosition = 0;
40
41 if (ShellOpenFileByName (L">i", &FileHandle, EFI_FILE_MODE_READ, 0) == EFI_SUCCESS) {
42 CharSize = sizeof(CHAR16);
43 gEfiShellProtocol->GetFilePosition (FileHandle, &OriginalFilePosition);
44 Status = gEfiShellProtocol->ReadFile (FileHandle, &CharSize, &CharBuffer);
45 if (EFI_ERROR (Status) || (CharSize != sizeof(CHAR16))) {
46 return FALSE;
47 }
48 gEfiShellProtocol->SetFilePosition(FileHandle, OriginalFilePosition);
49 }
50
51 if (FileHandle == NULL) {
52 return FALSE;
53 } else {
54 return TRUE;
55 }
56 }
57
58 /**
59 Function to read a single line (up to but not including the \n) using StdIn data from a SHELL_FILE_HANDLE.
60
61 If the position upon start is 0, then the Ascii Boolean will be set. This should be
62 maintained and not changed for all operations with the same file.
63
64 @param[in] Handle SHELL_FILE_HANDLE to read from.
65 @param[in, out] Buffer The pointer to buffer to read into.
66 @param[in, out] Size The pointer to number of bytes in Buffer.
67 @param[in] Truncate If the buffer is large enough, this has no effect.
68 If the buffer is is too small and Truncate is TRUE,
69 the line will be truncated.
70 If the buffer is is too small and Truncate is FALSE,
71 then no read will occur.
72
73 @retval EFI_SUCCESS The operation was successful. The line is stored in
74 Buffer.
75 @retval EFI_INVALID_PARAMETER Handle was NULL.
76 @retval EFI_INVALID_PARAMETER Size was NULL.
77 @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
78 Size was updated to the minimum space required.
79 **/
80 EFI_STATUS
81 EFIAPI
82 ShellFileHandleReadStdInLine(
83 IN SHELL_FILE_HANDLE Handle,
84 IN OUT CHAR16 *Buffer,
85 IN OUT UINTN *Size,
86 IN BOOLEAN Truncate
87 )
88 {
89 EFI_STATUS Status;
90 CHAR16 CharBuffer;
91 UINTN CharSize;
92 UINTN CountSoFar;
93 UINT64 OriginalFilePosition;
94
95
96 if (Handle == NULL
97 ||Size == NULL
98 ){
99 return (EFI_INVALID_PARAMETER);
100 }
101 if (Buffer == NULL) {
102 ASSERT(*Size == 0);
103 } else {
104 *Buffer = CHAR_NULL;
105 }
106 gEfiShellProtocol->GetFilePosition (Handle, &OriginalFilePosition);
107
108 for (CountSoFar = 0;;CountSoFar++){
109 CharBuffer = 0;
110 CharSize = sizeof(CHAR16);
111 Status = gEfiShellProtocol->ReadFile (Handle, &CharSize, &CharBuffer);
112 if ( EFI_ERROR(Status)
113 || CharSize == 0
114 || (CharBuffer == L'\n')
115 ){
116 break;
117 }
118 //
119 // if we have space save it...
120 //
121 if ((CountSoFar+1)*sizeof(CHAR16) < *Size){
122 ASSERT(Buffer != NULL);
123 ((CHAR16*)Buffer)[CountSoFar] = CharBuffer;
124 ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL;
125 }
126 }
127
128 //
129 // if we ran out of space tell when...
130 //
131 if ((CountSoFar+1)*sizeof(CHAR16) > *Size){
132 *Size = (CountSoFar+1)*sizeof(CHAR16);
133 if (!Truncate) {
134 gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition);
135 } else {
136 DEBUG((DEBUG_WARN, "The line was truncated in ShellFileHandleReadLine"));
137 }
138 return (EFI_BUFFER_TOO_SMALL);
139 }
140 while(Buffer[StrLen(Buffer)-1] == L'\r') {
141 Buffer[StrLen(Buffer)-1] = CHAR_NULL;
142 }
143
144 return (Status);
145 }
146
147
148 /**
149 Function to read a single line using StdIn from a SHELL_FILE_HANDLE. The \n is not included in the returned
150 buffer. The returned buffer must be callee freed.
151
152 If the position upon start is 0, then the Ascii Boolean will be set. This should be
153 maintained and not changed for all operations with the same file.
154
155 @param[in] Handle SHELL_FILE_HANDLE to read from.
156
157 @return The line of text from the file.
158 @retval NULL There was not enough memory available.
159
160 @sa ShellFileHandleReadLine
161 **/
162 CHAR16*
163 EFIAPI
164 ParseReturnStdInLine (
165 IN SHELL_FILE_HANDLE Handle
166 )
167 {
168 CHAR16 *RetVal;
169 UINTN Size;
170 EFI_STATUS Status;
171
172 Size = 0;
173 RetVal = NULL;
174
175 Status = ShellFileHandleReadStdInLine (Handle, RetVal, &Size, FALSE);
176 if (Status == EFI_BUFFER_TOO_SMALL) {
177 RetVal = AllocateZeroPool(Size);
178 if (RetVal == NULL) {
179 return (NULL);
180 }
181 Status = ShellFileHandleReadStdInLine (Handle, RetVal, &Size, FALSE);
182
183 }
184 if (EFI_ERROR(Status) && (RetVal != NULL)) {
185 FreePool(RetVal);
186 RetVal = NULL;
187 }
188 return (RetVal);
189 }
190
191 /**
192 Do the actual parsing of the file. the file should be SFO output from a
193 shell command or a similar format.
194
195 @param[in] FileName The filename to open.
196 @param[in] TableName The name of the table to find.
197 @param[in] ColumnIndex The column number to get.
198 @param[in] TableNameInstance Which instance of the table to get (row).
199 @param[in] ShellCommandInstance Which instance of the command to get.
200 @param[in] StreamingUnicode Indicates Input file is StdIn Unicode streaming data or not
201
202 @retval SHELL_NOT_FOUND The requested instance was not found.
203 @retval SHELL_SUCCESS The operation was successful.
204 **/
205 SHELL_STATUS
206 EFIAPI
207 PerformParsing(
208 IN CONST CHAR16 *FileName,
209 IN CONST CHAR16 *TableName,
210 IN CONST UINTN ColumnIndex,
211 IN CONST UINTN TableNameInstance,
212 IN CONST UINTN ShellCommandInstance,
213 IN BOOLEAN StreamingUnicode
214 )
215 {
216 SHELL_FILE_HANDLE FileHandle;
217 EFI_STATUS Status;
218 BOOLEAN Ascii;
219 UINTN LoopVariable;
220 UINTN ColumnLoop;
221 CHAR16 *TempLine;
222 CHAR16 *ColumnPointer;
223 SHELL_STATUS ShellStatus;
224 CHAR16 *TempSpot;
225
226 ASSERT(FileName != NULL);
227 ASSERT(TableName != NULL);
228
229 ShellStatus = SHELL_SUCCESS;
230
231 Status = ShellOpenFileByName(FileName, &FileHandle, EFI_FILE_MODE_READ, 0);
232 if (EFI_ERROR(Status)) {
233 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellLevel2HiiHandle, L"parse", FileName);
234 ShellStatus = SHELL_NOT_FOUND;
235 } else if (!EFI_ERROR (FileHandleIsDirectory (FileHandle))) {
236 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_FILE), gShellLevel2HiiHandle, L"parse", FileName);
237 ShellStatus = SHELL_NOT_FOUND;
238 } else {
239 for (LoopVariable = 0 ; LoopVariable < ShellCommandInstance && !ShellFileHandleEof(FileHandle);) {
240 if (StreamingUnicode) {
241 TempLine = ParseReturnStdInLine (FileHandle);
242 } else {
243 TempLine = ShellFileHandleReturnLine (FileHandle, &Ascii);
244 }
245
246 if ((TempLine == NULL) || (*TempLine == CHAR_NULL && StreamingUnicode == TRUE)) {
247 break;
248 }
249
250 //
251 // Search for "ShellCommand," in the file to start the SFO table
252 // for a given ShellCommand. The UEFI Shell spec does not specify
253 // a space after the comma.
254 //
255 if (StrStr (TempLine, L"ShellCommand,") == TempLine) {
256 LoopVariable++;
257 }
258 SHELL_FREE_NON_NULL(TempLine);
259 }
260 if (LoopVariable == ShellCommandInstance) {
261 LoopVariable = 0;
262 while(1) {
263 if (StreamingUnicode) {
264 TempLine = ParseReturnStdInLine (FileHandle);
265 } else {
266 TempLine = ShellFileHandleReturnLine (FileHandle, &Ascii);
267 }
268 if (TempLine == NULL
269 || *TempLine == CHAR_NULL
270 || StrStr (TempLine, L"ShellCommand,") == TempLine) {
271 SHELL_FREE_NON_NULL(TempLine);
272 break;
273 }
274 if (StrStr (TempLine, TableName) == TempLine) {
275 LoopVariable++;
276 if (LoopVariable == TableNameInstance
277 || (TableNameInstance == (UINTN)-1)) {
278 for (ColumnLoop = 1, ColumnPointer = TempLine; ColumnLoop < ColumnIndex && ColumnPointer != NULL && *ColumnPointer != CHAR_NULL; ColumnLoop++) {
279 ColumnPointer = StrStr (ColumnPointer, L",\"");
280 if (ColumnPointer != NULL && *ColumnPointer != CHAR_NULL){
281 ColumnPointer++;
282 }
283 }
284 if (ColumnLoop == ColumnIndex) {
285 if (ColumnPointer == NULL) {
286 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_VALUE), gShellLevel2HiiHandle, L"parse", L"Column Index");
287 ShellStatus = SHELL_INVALID_PARAMETER;
288 } else {
289 TempSpot = StrStr (ColumnPointer, L",\"");
290 if (TempSpot != NULL) {
291 *TempSpot = CHAR_NULL;
292 }
293 while (ColumnPointer != NULL && *ColumnPointer != CHAR_NULL && ColumnPointer[0] == L' '){
294 ColumnPointer++;
295 }
296 if (ColumnPointer != NULL && *ColumnPointer != CHAR_NULL && ColumnPointer[0] == L'\"'){
297 ColumnPointer++;
298 }
299 if (ColumnPointer != NULL && *ColumnPointer != CHAR_NULL && ColumnPointer[StrLen (ColumnPointer) - 1] == L'\"'){
300 ColumnPointer[StrLen (ColumnPointer) - 1] = CHAR_NULL;
301 }
302
303 ShellPrintEx (-1, -1, L"%s\r\n", ColumnPointer);
304 }
305 }
306 }
307 }
308 SHELL_FREE_NON_NULL(TempLine);
309 }
310 }
311 }
312 return (ShellStatus);
313 }
314
315 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
316 {L"-i", TypeValue},
317 {L"-s", TypeValue},
318 {NULL, TypeMax}
319 };
320
321 /**
322 Function for 'parse' command.
323
324 @param[in] ImageHandle Handle to the Image (NULL if Internal).
325 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
326 **/
327 SHELL_STATUS
328 EFIAPI
329 ShellCommandRunParse (
330 IN EFI_HANDLE ImageHandle,
331 IN EFI_SYSTEM_TABLE *SystemTable
332 )
333 {
334 EFI_STATUS Status;
335 LIST_ENTRY *Package;
336 CHAR16 *ProblemParam;
337 CONST CHAR16 *FileName;
338 CONST CHAR16 *TableName;
339 CONST CHAR16 *ColumnString;
340 SHELL_STATUS ShellStatus;
341 UINTN ShellCommandInstance;
342 UINTN TableNameInstance;
343 BOOLEAN StreamingUnicode;
344
345 ShellStatus = SHELL_SUCCESS;
346 ProblemParam = NULL;
347 StreamingUnicode = FALSE;
348
349 //
350 // initialize the shell lib (we must be in non-auto-init...)
351 //
352 Status = ShellInitialize();
353 ASSERT_EFI_ERROR(Status);
354
355 //
356 // parse the command line
357 //
358 Status = ShellCommandLineParseEx (ParamList, &Package, &ProblemParam, TRUE, FALSE);
359 if (EFI_ERROR(Status)) {
360 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
361 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"parse", ProblemParam);
362 FreePool(ProblemParam);
363 ShellStatus = SHELL_INVALID_PARAMETER;
364 } else {
365 ASSERT(FALSE);
366 }
367 } else {
368 StreamingUnicode = IsStdInDataAvailable ();
369 if ((!StreamingUnicode && (ShellCommandLineGetCount(Package) < 4)) ||
370 (ShellCommandLineGetCount(Package) < 3)) {
371 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle, L"parse");
372 ShellStatus = SHELL_INVALID_PARAMETER;
373 } else if ((StreamingUnicode && (ShellCommandLineGetCount(Package) > 3)) ||
374 (ShellCommandLineGetCount(Package) > 4)) {
375 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle, L"parse");
376 ShellStatus = SHELL_INVALID_PARAMETER;
377 } else {
378 if (StreamingUnicode) {
379 FileName = L">i";
380 TableName = ShellCommandLineGetRawValue(Package, 1);
381 ColumnString = ShellCommandLineGetRawValue(Package, 2);
382 } else {
383 FileName = ShellCommandLineGetRawValue(Package, 1);
384 TableName = ShellCommandLineGetRawValue(Package, 2);
385 ColumnString = ShellCommandLineGetRawValue(Package, 3);
386 }
387 if (ShellCommandLineGetValue(Package, L"-i") == NULL) {
388 TableNameInstance = (UINTN)-1;
389 } else {
390 TableNameInstance = ShellStrToUintn(ShellCommandLineGetValue(Package, L"-i"));
391 }
392 if (ShellCommandLineGetValue(Package, L"-s") == NULL) {
393 ShellCommandInstance = 1;
394 } else {
395 ShellCommandInstance = ShellStrToUintn(ShellCommandLineGetValue(Package, L"-s"));
396 }
397
398 ShellStatus = PerformParsing(FileName, TableName, ShellStrToUintn(ColumnString), TableNameInstance, ShellCommandInstance, StreamingUnicode);
399 }
400 }
401
402 //
403 // free the command line package
404 //
405 ShellCommandLineFreeVarList (Package);
406
407 return (ShellStatus);
408 }
409