2 Main file for Parse shell level 2 function.
4 (C) Copyright 2013-2014, Hewlett-Packard Development Company, L.P.
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 Do the actual parsing of the file. the file should be SFO output from a
20 shell command or a similar format.
22 @param[in] FileName The filename to open.
23 @param[in] TableName The name of the table to find.
24 @param[in] ColumnIndex The column number to get.
25 @param[in] TableNameInstance Which instance of the table to get (row).
26 @param[in] ShellCommandInstance Which instance of the command to get.
28 @retval SHELL_NOT_FOUND The requested instance was not found.
29 @retval SHELL_SUCCESS The operation was successful.
34 IN CONST CHAR16
*FileName
,
35 IN CONST CHAR16
*TableName
,
36 IN CONST UINTN ColumnIndex
,
37 IN CONST UINTN TableNameInstance
,
38 IN CONST UINTN ShellCommandInstance
41 SHELL_FILE_HANDLE FileHandle
;
47 CHAR16
*ColumnPointer
;
48 SHELL_STATUS ShellStatus
;
51 ASSERT(FileName
!= NULL
);
52 ASSERT(TableName
!= NULL
);
54 ShellStatus
= SHELL_SUCCESS
;
56 Status
= ShellOpenFileByName(FileName
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
57 if (EFI_ERROR(Status
)) {
58 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL
), gShellLevel2HiiHandle
, FileName
);
59 ShellStatus
= SHELL_NOT_FOUND
;
60 } else if (!EFI_ERROR (FileHandleIsDirectory (FileHandle
))) {
61 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_NOT_FILE
), gShellLevel2HiiHandle
, FileName
);
62 ShellStatus
= SHELL_NOT_FOUND
;
64 for (LoopVariable
= 0 ; LoopVariable
< ShellCommandInstance
&& !ShellFileHandleEof(FileHandle
);) {
65 TempLine
= ShellFileHandleReturnLine(FileHandle
, &Ascii
);
66 if (TempLine
== NULL
) {
71 // Search for "ShellCommand," in the file to start the SFO table
72 // for a given ShellCommand. The UEFI Shell spec does not specify
73 // a space after the comma.
75 if (StrStr (TempLine
, L
"ShellCommand,") == TempLine
) {
78 SHELL_FREE_NON_NULL(TempLine
);
80 if (LoopVariable
== ShellCommandInstance
) {
83 TempLine
= ShellFileHandleReturnLine(FileHandle
, &Ascii
);
85 || *TempLine
== CHAR_NULL
86 || StrStr (TempLine
, L
"ShellCommand,") == TempLine
) {
87 SHELL_FREE_NON_NULL(TempLine
);
90 if (StrStr (TempLine
, TableName
) == TempLine
) {
92 if (LoopVariable
== TableNameInstance
93 || (TableNameInstance
== (UINTN
)-1)) {
94 for (ColumnLoop
= 1, ColumnPointer
= TempLine
; ColumnLoop
< ColumnIndex
&& ColumnPointer
!= NULL
&& *ColumnPointer
!= CHAR_NULL
; ColumnLoop
++) {
95 ColumnPointer
= StrStr (ColumnPointer
, L
",\"");
96 if (ColumnPointer
!= NULL
&& *ColumnPointer
!= CHAR_NULL
){
100 if (ColumnLoop
== ColumnIndex
) {
101 if (ColumnPointer
== NULL
) {
102 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM_VAL
), gShellLevel2HiiHandle
, L
"Column Index");
103 ShellStatus
= SHELL_INVALID_PARAMETER
;
105 TempSpot
= StrStr (ColumnPointer
, L
",\"");
106 if (TempSpot
!= NULL
) {
107 *TempSpot
= CHAR_NULL
;
109 while (ColumnPointer
!= NULL
&& *ColumnPointer
!= CHAR_NULL
&& ColumnPointer
[0] == L
' '){
112 if (ColumnPointer
!= NULL
&& *ColumnPointer
!= CHAR_NULL
&& ColumnPointer
[0] == L
'\"'){
115 if (ColumnPointer
!= NULL
&& *ColumnPointer
!= CHAR_NULL
&& ColumnPointer
[StrLen (ColumnPointer
) - 1] == L
'\"'){
116 ColumnPointer
[StrLen (ColumnPointer
) - 1] = CHAR_NULL
;
119 ShellPrintEx (-1, -1, L
"%s\r\n", ColumnPointer
);
124 SHELL_FREE_NON_NULL(TempLine
);
128 return (ShellStatus
);
131 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
138 Function for 'parse' command.
140 @param[in] ImageHandle Handle to the Image (NULL if Internal).
141 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
145 ShellCommandRunParse (
146 IN EFI_HANDLE ImageHandle
,
147 IN EFI_SYSTEM_TABLE
*SystemTable
152 CHAR16
*ProblemParam
;
153 CONST CHAR16
*FileName
;
154 CONST CHAR16
*TableName
;
155 CONST CHAR16
*ColumnString
;
156 SHELL_STATUS ShellStatus
;
157 UINTN ShellCommandInstance
;
158 UINTN TableNameInstance
;
160 ShellStatus
= SHELL_SUCCESS
;
164 // initialize the shell lib (we must be in non-auto-init...)
166 Status
= ShellInitialize();
167 ASSERT_EFI_ERROR(Status
);
170 // parse the command line
172 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
173 if (EFI_ERROR(Status
)) {
174 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
175 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellLevel2HiiHandle
, ProblemParam
);
176 FreePool(ProblemParam
);
177 ShellStatus
= SHELL_INVALID_PARAMETER
;
182 if (ShellCommandLineGetCount(Package
) < 4) {
183 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellLevel2HiiHandle
);
184 ShellStatus
= SHELL_INVALID_PARAMETER
;
185 } else if (ShellCommandLineGetCount(Package
) > 4) {
186 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellLevel2HiiHandle
);
187 ShellStatus
= SHELL_INVALID_PARAMETER
;
189 FileName
= ShellCommandLineGetRawValue(Package
, 1);
190 TableName
= ShellCommandLineGetRawValue(Package
, 2);
191 ColumnString
= ShellCommandLineGetRawValue(Package
, 3);
193 if (ShellCommandLineGetValue(Package
, L
"-i") == NULL
) {
194 TableNameInstance
= (UINTN
)-1;
196 TableNameInstance
= ShellStrToUintn(ShellCommandLineGetValue(Package
, L
"-i"));
198 if (ShellCommandLineGetValue(Package
, L
"-s") == NULL
) {
199 ShellCommandInstance
= 1;
201 ShellCommandInstance
= ShellStrToUintn(ShellCommandLineGetValue(Package
, L
"-s"));
204 ShellStatus
= PerformParsing(FileName
, TableName
, ShellStrToUintn(ColumnString
), TableNameInstance
, ShellCommandInstance
);
209 // free the command line package
211 ShellCommandLineFreeVarList (Package
);
213 return (ShellStatus
);