2 Main file for Parse shell level 2 function.
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
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.
16 #include "UefiShellLevel2CommandsLib.h"
19 Check if data is coming from StdIn output.
23 @retval TRUE StdIn stream data available to parse
24 @retval FALSE StdIn stream data is not available to parse.
27 IsStdInDataAvailable (
31 SHELL_FILE_HANDLE FileHandle
;
35 UINT64 OriginalFilePosition
;
39 OriginalFilePosition
= 0;
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
))) {
48 gEfiShellProtocol
->SetFilePosition(FileHandle
, OriginalFilePosition
);
51 if (FileHandle
== NULL
) {
59 Function to read a single line (up to but not including the \n) using StdIn data from a SHELL_FILE_HANDLE.
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.
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.
73 @retval EFI_SUCCESS The operation was successful. The line is stored in
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.
82 ShellFileHandleReadStdInLine(
83 IN SHELL_FILE_HANDLE Handle
,
84 IN OUT CHAR16
*Buffer
,
93 UINT64 OriginalFilePosition
;
99 return (EFI_INVALID_PARAMETER
);
101 if (Buffer
== NULL
) {
106 gEfiShellProtocol
->GetFilePosition (Handle
, &OriginalFilePosition
);
108 for (CountSoFar
= 0;;CountSoFar
++){
110 CharSize
= sizeof(CHAR16
);
111 Status
= gEfiShellProtocol
->ReadFile (Handle
, &CharSize
, &CharBuffer
);
112 if ( EFI_ERROR(Status
)
114 || (CharBuffer
== L
'\n')
119 // if we have space save it...
121 if ((CountSoFar
+1)*sizeof(CHAR16
) < *Size
){
122 ASSERT(Buffer
!= NULL
);
123 ((CHAR16
*)Buffer
)[CountSoFar
] = CharBuffer
;
124 ((CHAR16
*)Buffer
)[CountSoFar
+1] = CHAR_NULL
;
129 // if we ran out of space tell when...
131 if ((CountSoFar
+1)*sizeof(CHAR16
) > *Size
){
132 *Size
= (CountSoFar
+1)*sizeof(CHAR16
);
134 gEfiShellProtocol
->SetFilePosition(Handle
, OriginalFilePosition
);
136 DEBUG((DEBUG_WARN
, "The line was truncated in ShellFileHandleReadLine"));
138 return (EFI_BUFFER_TOO_SMALL
);
140 while(Buffer
[StrLen(Buffer
)-1] == L
'\r') {
141 Buffer
[StrLen(Buffer
)-1] = CHAR_NULL
;
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.
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.
155 @param[in] Handle SHELL_FILE_HANDLE to read from.
157 @return The line of text from the file.
158 @retval NULL There was not enough memory available.
160 @sa ShellFileHandleReadLine
164 ParseReturnStdInLine (
165 IN SHELL_FILE_HANDLE Handle
175 Status
= ShellFileHandleReadStdInLine (Handle
, RetVal
, &Size
, FALSE
);
176 if (Status
== EFI_BUFFER_TOO_SMALL
) {
177 RetVal
= AllocateZeroPool(Size
);
178 if (RetVal
== NULL
) {
181 Status
= ShellFileHandleReadStdInLine (Handle
, RetVal
, &Size
, FALSE
);
184 if (EFI_ERROR(Status
) && (RetVal
!= NULL
)) {
192 Do the actual parsing of the file. the file should be SFO output from a
193 shell command or a similar format.
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
202 @retval SHELL_NOT_FOUND The requested instance was not found.
203 @retval SHELL_SUCCESS The operation was successful.
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
216 SHELL_FILE_HANDLE FileHandle
;
222 CHAR16
*ColumnPointer
;
223 SHELL_STATUS ShellStatus
;
226 ASSERT(FileName
!= NULL
);
227 ASSERT(TableName
!= NULL
);
229 ShellStatus
= SHELL_SUCCESS
;
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
;
239 for (LoopVariable
= 0 ; LoopVariable
< ShellCommandInstance
&& !ShellFileHandleEof(FileHandle
);) {
240 if (StreamingUnicode
) {
241 TempLine
= ParseReturnStdInLine (FileHandle
);
243 TempLine
= ShellFileHandleReturnLine (FileHandle
, &Ascii
);
246 if ((TempLine
== NULL
) || (*TempLine
== CHAR_NULL
&& StreamingUnicode
== TRUE
)) {
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.
255 if (StrStr (TempLine
, L
"ShellCommand,") == TempLine
) {
258 SHELL_FREE_NON_NULL(TempLine
);
260 if (LoopVariable
== ShellCommandInstance
) {
263 if (StreamingUnicode
) {
264 TempLine
= ParseReturnStdInLine (FileHandle
);
266 TempLine
= ShellFileHandleReturnLine (FileHandle
, &Ascii
);
269 || *TempLine
== CHAR_NULL
270 || StrStr (TempLine
, L
"ShellCommand,") == TempLine
) {
271 SHELL_FREE_NON_NULL(TempLine
);
274 if (StrStr (TempLine
, TableName
) == TempLine
) {
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
){
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
;
289 TempSpot
= StrStr (ColumnPointer
, L
",\"");
290 if (TempSpot
!= NULL
) {
291 *TempSpot
= CHAR_NULL
;
293 while (ColumnPointer
!= NULL
&& *ColumnPointer
!= CHAR_NULL
&& ColumnPointer
[0] == L
' '){
296 if (ColumnPointer
!= NULL
&& *ColumnPointer
!= CHAR_NULL
&& ColumnPointer
[0] == L
'\"'){
299 if (ColumnPointer
!= NULL
&& *ColumnPointer
!= CHAR_NULL
&& ColumnPointer
[StrLen (ColumnPointer
) - 1] == L
'\"'){
300 ColumnPointer
[StrLen (ColumnPointer
) - 1] = CHAR_NULL
;
303 ShellPrintEx (-1, -1, L
"%s\r\n", ColumnPointer
);
308 SHELL_FREE_NON_NULL(TempLine
);
312 return (ShellStatus
);
315 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
322 Function for 'parse' command.
324 @param[in] ImageHandle Handle to the Image (NULL if Internal).
325 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
329 ShellCommandRunParse (
330 IN EFI_HANDLE ImageHandle
,
331 IN EFI_SYSTEM_TABLE
*SystemTable
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
;
345 ShellStatus
= SHELL_SUCCESS
;
347 StreamingUnicode
= FALSE
;
350 // initialize the shell lib (we must be in non-auto-init...)
352 Status
= ShellInitialize();
353 ASSERT_EFI_ERROR(Status
);
356 // parse the command line
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
;
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
;
378 if (StreamingUnicode
) {
380 TableName
= ShellCommandLineGetRawValue(Package
, 1);
381 ColumnString
= ShellCommandLineGetRawValue(Package
, 2);
383 FileName
= ShellCommandLineGetRawValue(Package
, 1);
384 TableName
= ShellCommandLineGetRawValue(Package
, 2);
385 ColumnString
= ShellCommandLineGetRawValue(Package
, 3);
387 if (ShellCommandLineGetValue(Package
, L
"-i") == NULL
) {
388 TableNameInstance
= (UINTN
)-1;
390 TableNameInstance
= ShellStrToUintn(ShellCommandLineGetValue(Package
, L
"-i"));
392 if (ShellCommandLineGetValue(Package
, L
"-s") == NULL
) {
393 ShellCommandInstance
= 1;
395 ShellCommandInstance
= ShellStrToUintn(ShellCommandLineGetValue(Package
, L
"-s"));
398 ShellStatus
= PerformParsing(FileName
, TableName
, ShellStrToUintn(ColumnString
), TableNameInstance
, ShellCommandInstance
, StreamingUnicode
);
403 // free the command line package
405 ShellCommandLineFreeVarList (Package
);
407 return (ShellStatus
);