]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellLevel2CommandsLib/Parse.c
Add code to check whether the pointer 'CorrectedPath' and 'FullPath' are NULL before...
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Parse.c
1 /** @file
2 Main file for Parse shell level 2 function.
3
4 Copyright (c) 2009 - 2012, 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 if (ColumnPointer == NULL) {
95 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM_VAL), gShellLevel2HiiHandle, L"Column Index");
96 ShellStatus = SHELL_INVALID_PARAMETER;
97 } else {
98 TempSpot = StrStr(ColumnPointer, L",");
99 if (TempSpot != NULL) {
100 *TempSpot = CHAR_NULL;
101 }
102 while (ColumnPointer != NULL && *ColumnPointer != CHAR_NULL && ColumnPointer[0] == L' '){
103 ColumnPointer++;
104 }
105 if (ColumnPointer != NULL && *ColumnPointer != CHAR_NULL && ColumnPointer[0] == L'\"'){
106 ColumnPointer++;
107 }
108 if (ColumnPointer != NULL && *ColumnPointer != CHAR_NULL && ColumnPointer[StrLen(ColumnPointer)-1] == L'\"'){
109 ColumnPointer[StrLen(ColumnPointer)-1] = CHAR_NULL;
110 }
111
112 ShellPrintEx(-1, -1, L"%s\r\n", ColumnPointer);
113 }
114 }
115 }
116 SHELL_FREE_NON_NULL(TempLine);
117 }
118 }
119 }
120 return (ShellStatus);
121 }
122
123 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
124 {L"-i", TypeValue},
125 {L"-s", TypeValue},
126 {NULL, TypeMax}
127 };
128
129 /**
130 Function for 'parse' command.
131
132 @param[in] ImageHandle Handle to the Image (NULL if Internal).
133 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
134 **/
135 SHELL_STATUS
136 EFIAPI
137 ShellCommandRunParse (
138 IN EFI_HANDLE ImageHandle,
139 IN EFI_SYSTEM_TABLE *SystemTable
140 )
141 {
142 EFI_STATUS Status;
143 LIST_ENTRY *Package;
144 CHAR16 *ProblemParam;
145 CONST CHAR16 *FileName;
146 CONST CHAR16 *TableName;
147 CONST CHAR16 *ColumnString;
148 SHELL_STATUS ShellStatus;
149 UINTN ShellCommandInstance;
150 UINTN TableNameInstance;
151
152 ShellStatus = SHELL_SUCCESS;
153 ProblemParam = NULL;
154
155 //
156 // initialize the shell lib (we must be in non-auto-init...)
157 //
158 Status = ShellInitialize();
159 ASSERT_EFI_ERROR(Status);
160
161 //
162 // parse the command line
163 //
164 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
165 if (EFI_ERROR(Status)) {
166 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
167 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, ProblemParam);
168 FreePool(ProblemParam);
169 ShellStatus = SHELL_INVALID_PARAMETER;
170 } else {
171 ASSERT(FALSE);
172 }
173 } else {
174 if (ShellCommandLineGetCount(Package) < 4) {
175 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle);
176 ShellStatus = SHELL_INVALID_PARAMETER;
177 } else if (ShellCommandLineGetCount(Package) > 4) {
178 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle);
179 ShellStatus = SHELL_INVALID_PARAMETER;
180 } else {
181 FileName = ShellCommandLineGetRawValue(Package, 1);
182 TableName = ShellCommandLineGetRawValue(Package, 2);
183 ColumnString = ShellCommandLineGetRawValue(Package, 3);
184
185 if (ShellCommandLineGetValue(Package, L"-i") == NULL) {
186 TableNameInstance = (UINTN)-1;
187 } else {
188 TableNameInstance = ShellStrToUintn(ShellCommandLineGetValue(Package, L"-i"));
189 }
190 if (ShellCommandLineGetValue(Package, L"-s") == NULL) {
191 ShellCommandInstance = 1;
192 } else {
193 ShellCommandInstance = ShellStrToUintn(ShellCommandLineGetValue(Package, L"-s"));
194 }
195
196 ShellStatus = PerformParsing(FileName, TableName, ShellStrToUintn(ColumnString), TableNameInstance, ShellCommandInstance);
197 }
198 }
199
200 //
201 // free the command line package
202 //
203 ShellCommandLineFreeVarList (Package);
204
205 return (ShellStatus);
206 }
207