]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellLevel2CommandsLib/Parse.c
1cdc9e32e302dc6e467bdebfdd61da8cbe9455e2
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Parse.c
1 /** @file
2 Main file for Parse shell level 2 function.
3
4 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "UefiShellLevel2CommandsLib.h"
16
17 /**
18 Do the actual parsing of the file. the file should be SFO output from a
19 shell command or a similar format.
20
21 @param[in] FileName The filename to open.
22 @param[in] TableName The name of the table to find.
23 @param[in] ColumnIndex The column number to get.
24 @param[in] TableNameInstance Which instance of the table to get (row).
25 @param[in] ShellCommandInstance Which instance of the command to get.
26
27 @retval SHELL_NOT_FOUND The requested instance was not found.
28 @retval SHELL_SUCCESS The operation was successful.
29 **/
30 SHELL_STATUS
31 EFIAPI
32 PerformParsing(
33 IN CONST CHAR16 *FileName,
34 IN CONST CHAR16 *TableName,
35 IN CONST UINTN ColumnIndex,
36 IN CONST UINTN TableNameInstance,
37 IN CONST UINTN ShellCommandInstance
38 )
39 {
40 SHELL_FILE_HANDLE FileHandle;
41 EFI_STATUS Status;
42 BOOLEAN Ascii;
43 UINTN LoopVariable;
44 UINTN ColumnLoop;
45 CHAR16 *TempLine;
46 CHAR16 *ColumnPointer;
47 SHELL_STATUS ShellStatus;
48 CHAR16 *TempSpot;
49
50 ASSERT(FileName != NULL);
51 ASSERT(TableName != NULL);
52
53 ShellStatus = SHELL_SUCCESS;
54
55 Status = ShellOpenFileByName(FileName, &FileHandle, EFI_FILE_MODE_READ, 0);
56 if (EFI_ERROR(Status)) {
57 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellLevel2HiiHandle, FileName);
58 ShellStatus = SHELL_NOT_FOUND;
59 } else {
60 for (LoopVariable = 0 ; LoopVariable < ShellCommandInstance && !ShellFileHandleEof(FileHandle);) {
61 TempLine = ShellFileHandleReturnLine(FileHandle, &Ascii);
62 if (TempLine == NULL) {
63 break;
64 }
65 if (StrStr(TempLine, L"ShellCommand, \"") == TempLine) {
66 LoopVariable++;
67 }
68 SHELL_FREE_NON_NULL(TempLine);
69 }
70 if (LoopVariable == ShellCommandInstance) {
71 LoopVariable = 0;
72 while(1) {
73 TempLine = ShellFileHandleReturnLine(FileHandle, &Ascii);
74 if ( TempLine == NULL
75 || *TempLine == CHAR_NULL
76 || StrStr(TempLine, L"ShellCommand, \"") == TempLine
77 ){
78 SHELL_FREE_NON_NULL(TempLine);
79 break;
80 }
81 if (StrStr(TempLine, TableName) == TempLine) {
82 LoopVariable++;
83 }
84 if ( LoopVariable == TableNameInstance
85 || (TableNameInstance == (UINTN)-1 && StrStr(TempLine, TableName) == TempLine)
86 ){
87 for (ColumnLoop = 1, ColumnPointer = TempLine; ColumnLoop < ColumnIndex && ColumnPointer != NULL && *ColumnPointer != CHAR_NULL; ColumnLoop++) {
88 ColumnPointer = StrStr(ColumnPointer, L",");
89 if (ColumnPointer != NULL && *ColumnPointer != CHAR_NULL){
90 ColumnPointer++;
91 }
92 }
93 if (ColumnLoop == ColumnIndex) {
94 ASSERT(ColumnPointer != NULL);
95 TempSpot = StrStr(ColumnPointer, L",");
96 if (TempSpot != NULL) {
97 *TempSpot = CHAR_NULL;
98 }
99 while (ColumnPointer != NULL && *ColumnPointer != CHAR_NULL && ColumnPointer[0] == L' '){
100 ColumnPointer++;
101 }
102 if (ColumnPointer != NULL && *ColumnPointer != CHAR_NULL && ColumnPointer[0] == L'\"'){
103 ColumnPointer++;
104 }
105 if (ColumnPointer != NULL && *ColumnPointer != CHAR_NULL && ColumnPointer[StrLen(ColumnPointer)-1] == L'\"'){
106 ColumnPointer[StrLen(ColumnPointer)-1] = CHAR_NULL;
107 }
108
109 ShellPrintEx(-1, -1, L"%s\r\n", ColumnPointer);
110 }
111 }
112 SHELL_FREE_NON_NULL(TempLine);
113 }
114 }
115 }
116 return (ShellStatus);
117 }
118
119 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
120 {L"-i", TypeValue},
121 {L"-s", TypeValue},
122 {NULL, TypeMax}
123 };
124
125 /**
126 Function for 'parse' command.
127
128 @param[in] ImageHandle Handle to the Image (NULL if Internal).
129 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
130 **/
131 SHELL_STATUS
132 EFIAPI
133 ShellCommandRunParse (
134 IN EFI_HANDLE ImageHandle,
135 IN EFI_SYSTEM_TABLE *SystemTable
136 )
137 {
138 EFI_STATUS Status;
139 LIST_ENTRY *Package;
140 CHAR16 *ProblemParam;
141 CONST CHAR16 *FileName;
142 CONST CHAR16 *TableName;
143 CONST CHAR16 *ColumnString;
144 SHELL_STATUS ShellStatus;
145 UINTN ShellCommandInstance;
146 UINTN TableNameInstance;
147
148 ShellStatus = SHELL_SUCCESS;
149 ProblemParam = NULL;
150
151 //
152 // initialize the shell lib (we must be in non-auto-init...)
153 //
154 Status = ShellInitialize();
155 ASSERT_EFI_ERROR(Status);
156
157 //
158 // parse the command line
159 //
160 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
161 if (EFI_ERROR(Status)) {
162 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
163 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, ProblemParam);
164 FreePool(ProblemParam);
165 ShellStatus = SHELL_INVALID_PARAMETER;
166 } else {
167 ASSERT(FALSE);
168 }
169 } else {
170 if (ShellCommandLineGetCount(Package) < 4) {
171 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle);
172 ShellStatus = SHELL_INVALID_PARAMETER;
173 } else if (ShellCommandLineGetCount(Package) > 4) {
174 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle);
175 ShellStatus = SHELL_INVALID_PARAMETER;
176 } else {
177 FileName = ShellCommandLineGetRawValue(Package, 1);
178 TableName = ShellCommandLineGetRawValue(Package, 2);
179 ColumnString = ShellCommandLineGetRawValue(Package, 3);
180
181 if (ShellCommandLineGetValue(Package, L"-i") == NULL) {
182 TableNameInstance = (UINTN)-1;
183 } else {
184 TableNameInstance = ShellStrToUintn(ShellCommandLineGetValue(Package, L"-i"));
185 }
186 if (ShellCommandLineGetValue(Package, L"-s") == NULL) {
187 ShellCommandInstance = 1;
188 } else {
189 ShellCommandInstance = ShellStrToUintn(ShellCommandLineGetValue(Package, L"-s"));
190 }
191
192 ShellStatus = PerformParsing(FileName, TableName, ShellStrToUintn(ColumnString), TableNameInstance, ShellCommandInstance);
193 }
194 }
195
196 //
197 // free the command line package
198 //
199 ShellCommandLineFreeVarList (Package);
200
201 return (ShellStatus);
202 }
203