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 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "UefiShellLevel2CommandsLib.h"
13 Check if data is coming from StdIn output.
17 @retval TRUE StdIn stream data available to parse
18 @retval FALSE StdIn stream data is not available to parse.
21 IsStdInDataAvailable (
25 SHELL_FILE_HANDLE FileHandle
;
29 UINT64 OriginalFilePosition
;
33 OriginalFilePosition
= 0;
35 if (ShellOpenFileByName (L
">i", &FileHandle
, EFI_FILE_MODE_READ
, 0) == EFI_SUCCESS
) {
36 CharSize
= sizeof(CHAR16
);
37 gEfiShellProtocol
->GetFilePosition (FileHandle
, &OriginalFilePosition
);
38 Status
= gEfiShellProtocol
->ReadFile (FileHandle
, &CharSize
, &CharBuffer
);
39 if (EFI_ERROR (Status
) || (CharSize
!= sizeof(CHAR16
))) {
42 gEfiShellProtocol
->SetFilePosition(FileHandle
, OriginalFilePosition
);
45 if (FileHandle
== NULL
) {
53 Handle stings for SFO Output with escape character ^ in a string
54 1. Quotation marks in the string must be escaped by using a ^ character (i.e. ^").
55 2. The ^ character may be inserted using ^^.
57 @param[in] String The Unicode NULL-terminated string.
59 @retval NewString The new string handled for SFO.
62 HandleStringWithEscapeCharForParse (
75 // start to parse the input string.
77 NewStr
= AllocateZeroPool (StrSize (String
));
83 while (*StrWalker
!= CHAR_NULL
) {
84 if (*StrWalker
== L
'^' && (*(StrWalker
+ 1) == L
'^' || *(StrWalker
+ 1) == L
'"')) {
85 *NewStr
= *(StrWalker
+ 1);
99 Do the actual parsing of the file. the file should be SFO output from a
100 shell command or a similar format.
102 @param[in] FileName The filename to open.
103 @param[in] TableName The name of the table to find.
104 @param[in] ColumnIndex The column number to get.
105 @param[in] TableNameInstance Which instance of the table to get (row).
106 @param[in] ShellCommandInstance Which instance of the command to get.
107 @param[in] StreamingUnicode Indicates Input file is StdIn Unicode streaming data or not
109 @retval SHELL_NOT_FOUND The requested instance was not found.
110 @retval SHELL_SUCCESS The operation was successful.
114 IN CONST CHAR16
*FileName
,
115 IN CONST CHAR16
*TableName
,
116 IN CONST UINTN ColumnIndex
,
117 IN CONST UINTN TableNameInstance
,
118 IN CONST UINTN ShellCommandInstance
,
119 IN BOOLEAN StreamingUnicode
122 SHELL_FILE_HANDLE FileHandle
;
128 CHAR16
*ColumnPointer
;
129 SHELL_STATUS ShellStatus
;
133 ASSERT(FileName
!= NULL
);
134 ASSERT(TableName
!= NULL
);
136 ShellStatus
= SHELL_SUCCESS
;
138 Status
= ShellOpenFileByName(FileName
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
139 if (EFI_ERROR(Status
)) {
140 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL
), gShellLevel2HiiHandle
, L
"parse", FileName
);
141 ShellStatus
= SHELL_NOT_FOUND
;
142 } else if (!EFI_ERROR (FileHandleIsDirectory (FileHandle
))) {
143 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_NOT_FILE
), gShellLevel2HiiHandle
, L
"parse", FileName
);
144 ShellStatus
= SHELL_NOT_FOUND
;
146 for (LoopVariable
= 0 ; LoopVariable
< ShellCommandInstance
&& !ShellFileHandleEof(FileHandle
);) {
147 TempLine
= ShellFileHandleReturnLine (FileHandle
, &Ascii
);
149 if ((TempLine
== NULL
) || (*TempLine
== CHAR_NULL
&& StreamingUnicode
)) {
154 // Search for "ShellCommand," in the file to start the SFO table
155 // for a given ShellCommand. The UEFI Shell spec does not specify
156 // a space after the comma.
158 if (StrStr (TempLine
, L
"ShellCommand,") == TempLine
) {
161 SHELL_FREE_NON_NULL(TempLine
);
163 if (LoopVariable
== ShellCommandInstance
) {
166 TempLine
= ShellFileHandleReturnLine (FileHandle
, &Ascii
);
168 || *TempLine
== CHAR_NULL
169 || StrStr (TempLine
, L
"ShellCommand,") == TempLine
) {
170 SHELL_FREE_NON_NULL(TempLine
);
173 if (StrStr (TempLine
, TableName
) == TempLine
) {
175 if (LoopVariable
== TableNameInstance
176 || (TableNameInstance
== (UINTN
)-1)) {
177 for (ColumnLoop
= 1, ColumnPointer
= TempLine
; ColumnLoop
< ColumnIndex
&& ColumnPointer
!= NULL
&& *ColumnPointer
!= CHAR_NULL
; ColumnLoop
++) {
178 ColumnPointer
= StrStr (ColumnPointer
, L
",\"");
179 if (ColumnPointer
!= NULL
&& *ColumnPointer
!= CHAR_NULL
){
183 if (ColumnLoop
== ColumnIndex
) {
184 if (ColumnPointer
== NULL
) {
185 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_VALUE
), gShellLevel2HiiHandle
, L
"parse", L
"Column Index");
186 ShellStatus
= SHELL_INVALID_PARAMETER
;
188 TempSpot
= StrStr (ColumnPointer
, L
",\"");
189 if (TempSpot
!= NULL
) {
190 *TempSpot
= CHAR_NULL
;
192 while (ColumnPointer
!= NULL
&& *ColumnPointer
!= CHAR_NULL
&& ColumnPointer
[0] == L
' '){
195 if (ColumnPointer
!= NULL
&& *ColumnPointer
!= CHAR_NULL
&& ColumnPointer
[0] == L
'\"'){
198 if (ColumnPointer
!= NULL
&& *ColumnPointer
!= CHAR_NULL
&& ColumnPointer
[StrLen (ColumnPointer
) - 1] == L
'\"'){
199 ColumnPointer
[StrLen (ColumnPointer
) - 1] = CHAR_NULL
;
201 SfoString
= HandleStringWithEscapeCharForParse (ColumnPointer
);
202 if (SfoString
!= NULL
) {
203 ShellPrintEx (-1, -1, L
"%s\r\n", SfoString
);
204 SHELL_FREE_NON_NULL (SfoString
);
210 SHELL_FREE_NON_NULL(TempLine
);
214 return (ShellStatus
);
217 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
224 Function for 'parse' command.
226 @param[in] ImageHandle Handle to the Image (NULL if Internal).
227 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
231 ShellCommandRunParse (
232 IN EFI_HANDLE ImageHandle
,
233 IN EFI_SYSTEM_TABLE
*SystemTable
238 CHAR16
*ProblemParam
;
239 CONST CHAR16
*FileName
;
240 CONST CHAR16
*TableName
;
241 CONST CHAR16
*ColumnString
;
242 SHELL_STATUS ShellStatus
;
243 UINTN ShellCommandInstance
;
244 UINTN TableNameInstance
;
245 BOOLEAN StreamingUnicode
;
247 ShellStatus
= SHELL_SUCCESS
;
249 StreamingUnicode
= FALSE
;
252 // initialize the shell lib (we must be in non-auto-init...)
254 Status
= ShellInitialize();
255 ASSERT_EFI_ERROR(Status
);
258 // parse the command line
260 Status
= ShellCommandLineParseEx (ParamList
, &Package
, &ProblemParam
, TRUE
, FALSE
);
261 if (EFI_ERROR(Status
)) {
262 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
263 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellLevel2HiiHandle
, L
"parse", ProblemParam
);
264 FreePool(ProblemParam
);
265 ShellStatus
= SHELL_INVALID_PARAMETER
;
270 StreamingUnicode
= IsStdInDataAvailable ();
271 if ((!StreamingUnicode
&& (ShellCommandLineGetCount(Package
) < 4)) ||
272 (ShellCommandLineGetCount(Package
) < 3)) {
273 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellLevel2HiiHandle
, L
"parse");
274 ShellStatus
= SHELL_INVALID_PARAMETER
;
275 } else if ((StreamingUnicode
&& (ShellCommandLineGetCount(Package
) > 3)) ||
276 (ShellCommandLineGetCount(Package
) > 4)) {
277 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellLevel2HiiHandle
, L
"parse");
278 ShellStatus
= SHELL_INVALID_PARAMETER
;
280 if (StreamingUnicode
) {
282 TableName
= ShellCommandLineGetRawValue(Package
, 1);
283 ColumnString
= ShellCommandLineGetRawValue(Package
, 2);
285 FileName
= ShellCommandLineGetRawValue(Package
, 1);
286 TableName
= ShellCommandLineGetRawValue(Package
, 2);
287 ColumnString
= ShellCommandLineGetRawValue(Package
, 3);
289 if (ShellCommandLineGetValue(Package
, L
"-i") == NULL
) {
290 TableNameInstance
= (UINTN
)-1;
292 TableNameInstance
= ShellStrToUintn(ShellCommandLineGetValue(Package
, L
"-i"));
294 if (ShellCommandLineGetValue(Package
, L
"-s") == NULL
) {
295 ShellCommandInstance
= 1;
297 ShellCommandInstance
= ShellStrToUintn(ShellCommandLineGetValue(Package
, L
"-s"));
300 ShellStatus
= PerformParsing(FileName
, TableName
, ShellStrToUintn(ColumnString
), TableNameInstance
, ShellCommandInstance
, StreamingUnicode
);
305 // free the command line package
307 ShellCommandLineFreeVarList (Package
);
309 return (ShellStatus
);