]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellLevel2CommandsLib/Parse.c
aae2834a5a124992675d0a5bbeafaeef4825e1f5
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Parse.c
1 /** @file
2 Main file for Parse shell level 2 function.
3
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
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 Do the actual parsing of the file. the file should be SFO output from a
20 shell command or a similar format.
21
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.
27
28 @retval SHELL_NOT_FOUND The requested instance was not found.
29 @retval SHELL_SUCCESS The operation was successful.
30 **/
31 SHELL_STATUS
32 EFIAPI
33 PerformParsing(
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
39 )
40 {
41 SHELL_FILE_HANDLE FileHandle;
42 EFI_STATUS Status;
43 BOOLEAN Ascii;
44 UINTN LoopVariable;
45 UINTN ColumnLoop;
46 CHAR16 *TempLine;
47 CHAR16 *ColumnPointer;
48 SHELL_STATUS ShellStatus;
49 CHAR16 *TempSpot;
50
51 ASSERT(FileName != NULL);
52 ASSERT(TableName != NULL);
53
54 ShellStatus = SHELL_SUCCESS;
55
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;
63 } else {
64 for (LoopVariable = 0 ; LoopVariable < ShellCommandInstance && !ShellFileHandleEof(FileHandle);) {
65 TempLine = ShellFileHandleReturnLine(FileHandle, &Ascii);
66 if (TempLine == NULL) {
67 break;
68 }
69
70 //
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.
74 //
75 if (StrStr (TempLine, L"ShellCommand,") == TempLine) {
76 LoopVariable++;
77 }
78 SHELL_FREE_NON_NULL(TempLine);
79 }
80 if (LoopVariable == ShellCommandInstance) {
81 LoopVariable = 0;
82 while(1) {
83 TempLine = ShellFileHandleReturnLine(FileHandle, &Ascii);
84 if (TempLine == NULL
85 || *TempLine == CHAR_NULL
86 || StrStr (TempLine, L"ShellCommand,") == TempLine) {
87 SHELL_FREE_NON_NULL(TempLine);
88 break;
89 }
90 if (StrStr (TempLine, TableName) == TempLine) {
91 LoopVariable++;
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){
97 ColumnPointer++;
98 }
99 }
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;
104 } else {
105 TempSpot = StrStr (ColumnPointer, L",\"");
106 if (TempSpot != NULL) {
107 *TempSpot = CHAR_NULL;
108 }
109 while (ColumnPointer != NULL && *ColumnPointer != CHAR_NULL && ColumnPointer[0] == L' '){
110 ColumnPointer++;
111 }
112 if (ColumnPointer != NULL && *ColumnPointer != CHAR_NULL && ColumnPointer[0] == L'\"'){
113 ColumnPointer++;
114 }
115 if (ColumnPointer != NULL && *ColumnPointer != CHAR_NULL && ColumnPointer[StrLen (ColumnPointer) - 1] == L'\"'){
116 ColumnPointer[StrLen (ColumnPointer) - 1] = CHAR_NULL;
117 }
118
119 ShellPrintEx (-1, -1, L"%s\r\n", ColumnPointer);
120 }
121 }
122 }
123 }
124 SHELL_FREE_NON_NULL(TempLine);
125 }
126 }
127 }
128 return (ShellStatus);
129 }
130
131 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
132 {L"-i", TypeValue},
133 {L"-s", TypeValue},
134 {NULL, TypeMax}
135 };
136
137 /**
138 Function for 'parse' command.
139
140 @param[in] ImageHandle Handle to the Image (NULL if Internal).
141 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
142 **/
143 SHELL_STATUS
144 EFIAPI
145 ShellCommandRunParse (
146 IN EFI_HANDLE ImageHandle,
147 IN EFI_SYSTEM_TABLE *SystemTable
148 )
149 {
150 EFI_STATUS Status;
151 LIST_ENTRY *Package;
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;
159
160 ShellStatus = SHELL_SUCCESS;
161 ProblemParam = NULL;
162
163 //
164 // initialize the shell lib (we must be in non-auto-init...)
165 //
166 Status = ShellInitialize();
167 ASSERT_EFI_ERROR(Status);
168
169 //
170 // parse the command line
171 //
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;
178 } else {
179 ASSERT(FALSE);
180 }
181 } else {
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;
188 } else {
189 FileName = ShellCommandLineGetRawValue(Package, 1);
190 TableName = ShellCommandLineGetRawValue(Package, 2);
191 ColumnString = ShellCommandLineGetRawValue(Package, 3);
192
193 if (ShellCommandLineGetValue(Package, L"-i") == NULL) {
194 TableNameInstance = (UINTN)-1;
195 } else {
196 TableNameInstance = ShellStrToUintn(ShellCommandLineGetValue(Package, L"-i"));
197 }
198 if (ShellCommandLineGetValue(Package, L"-s") == NULL) {
199 ShellCommandInstance = 1;
200 } else {
201 ShellCommandInstance = ShellStrToUintn(ShellCommandLineGetValue(Package, L"-s"));
202 }
203
204 ShellStatus = PerformParsing(FileName, TableName, ShellStrToUintn(ColumnString), TableNameInstance, ShellCommandInstance);
205 }
206 }
207
208 //
209 // free the command line package
210 //
211 ShellCommandLineFreeVarList (Package);
212
213 return (ShellStatus);
214 }
215