]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellLevel1CommandsLib/UefiShellLevel1CommandsLib.c
ShellPkg/Dp: Add null pointer check
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel1CommandsLib / UefiShellLevel1CommandsLib.c
1 /** @file
2 Main file for NULL named library for level 1 shell command functions.
3
4 (C) Copyright 2013 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2009 - 2011, 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 "UefiShellLevel1CommandsLib.h"
17
18 STATIC CONST CHAR16 mFileName[] = L"ShellCommands";
19 EFI_HANDLE gShellLevel1HiiHandle = NULL;
20
21 /**
22 Return the help text filename. Only used if no HII information found.
23
24 @retval the filename.
25 **/
26 CONST CHAR16*
27 EFIAPI
28 ShellCommandGetManFileNameLevel1 (
29 VOID
30 )
31 {
32 return (mFileName);
33 }
34
35 /**
36 Constructor for the Shell Level 1 Commands library.
37
38 Install the handlers for level 1 UEFI Shell 2.0 commands.
39
40 @param ImageHandle the image handle of the process
41 @param SystemTable the EFI System Table pointer
42
43 @retval EFI_SUCCESS the shell command handlers were installed sucessfully
44 @retval EFI_UNSUPPORTED the shell level required was not found.
45 **/
46 EFI_STATUS
47 EFIAPI
48 ShellLevel1CommandsLibConstructor (
49 IN EFI_HANDLE ImageHandle,
50 IN EFI_SYSTEM_TABLE *SystemTable
51 )
52 {
53 //
54 // if shell level is less than 2 do nothing
55 //
56 if (PcdGet8(PcdShellSupportLevel) < 1) {
57 return (EFI_SUCCESS);
58 }
59
60 gShellLevel1HiiHandle = HiiAddPackages (&gShellLevel1HiiGuid, gImageHandle, UefiShellLevel1CommandsLibStrings, NULL);
61 if (gShellLevel1HiiHandle == NULL) {
62 return (EFI_DEVICE_ERROR);
63 }
64
65 //
66 // install our shell command handlers that are always installed
67 //
68 ShellCommandRegisterCommandName(L"stall", ShellCommandRunStall , ShellCommandGetManFileNameLevel1, 1, L"", FALSE, gShellLevel1HiiHandle, (EFI_STRING_ID)(PcdGet8(PcdShellSupportLevel) < 3 ? 0 : STRING_TOKEN(STR_GET_HELP_STALL) ));
69 ShellCommandRegisterCommandName(L"for", ShellCommandRunFor , ShellCommandGetManFileNameLevel1, 1, L"", FALSE, gShellLevel1HiiHandle, (EFI_STRING_ID)(PcdGet8(PcdShellSupportLevel) < 3 ? 0 : STRING_TOKEN(STR_GET_HELP_FOR) ));
70 ShellCommandRegisterCommandName(L"goto", ShellCommandRunGoto , ShellCommandGetManFileNameLevel1, 1, L"", FALSE, gShellLevel1HiiHandle, (EFI_STRING_ID)(PcdGet8(PcdShellSupportLevel) < 3 ? 0 : STRING_TOKEN(STR_GET_HELP_GOTO) ));
71 ShellCommandRegisterCommandName(L"if", ShellCommandRunIf , ShellCommandGetManFileNameLevel1, 1, L"", FALSE, gShellLevel1HiiHandle, (EFI_STRING_ID)(PcdGet8(PcdShellSupportLevel) < 3 ? 0 : STRING_TOKEN(STR_GET_HELP_IF) ));
72 ShellCommandRegisterCommandName(L"shift", ShellCommandRunShift , ShellCommandGetManFileNameLevel1, 1, L"", FALSE, gShellLevel1HiiHandle, (EFI_STRING_ID)(PcdGet8(PcdShellSupportLevel) < 3 ? 0 : STRING_TOKEN(STR_GET_HELP_SHIFT) ));
73 ShellCommandRegisterCommandName(L"exit", ShellCommandRunExit , ShellCommandGetManFileNameLevel1, 1, L"", TRUE , gShellLevel1HiiHandle, (EFI_STRING_ID)(PcdGet8(PcdShellSupportLevel) < 3 ? 0 : STRING_TOKEN(STR_GET_HELP_EXIT) ));
74 ShellCommandRegisterCommandName(L"else", ShellCommandRunElse , ShellCommandGetManFileNameLevel1, 1, L"", FALSE, gShellLevel1HiiHandle, (EFI_STRING_ID)(PcdGet8(PcdShellSupportLevel) < 3 ? 0 : STRING_TOKEN(STR_GET_HELP_ELSE) ));
75 ShellCommandRegisterCommandName(L"endif", ShellCommandRunEndIf , ShellCommandGetManFileNameLevel1, 1, L"", FALSE, gShellLevel1HiiHandle, (EFI_STRING_ID)(PcdGet8(PcdShellSupportLevel) < 3 ? 0 : STRING_TOKEN(STR_GET_HELP_ENDIF) ));
76 ShellCommandRegisterCommandName(L"endfor", ShellCommandRunEndFor , ShellCommandGetManFileNameLevel1, 1, L"", FALSE, gShellLevel1HiiHandle, (EFI_STRING_ID)(PcdGet8(PcdShellSupportLevel) < 3 ? 0 : STRING_TOKEN(STR_GET_HELP_ENDFOR)));
77
78 return (EFI_SUCCESS);
79 }
80
81 /**
82 Destructor for the library. free any resources.
83
84 @param ImageHandle The image handle of the process.
85 @param SystemTable The EFI System Table pointer.
86 **/
87 EFI_STATUS
88 EFIAPI
89 ShellLevel1CommandsLibDestructor (
90 IN EFI_HANDLE ImageHandle,
91 IN EFI_SYSTEM_TABLE *SystemTable
92 )
93 {
94 if (gShellLevel1HiiHandle != NULL) {
95 HiiRemovePackages(gShellLevel1HiiHandle);
96 }
97 return (EFI_SUCCESS);
98 }
99
100 /**
101 Test a node to see if meets the criterion.
102
103 It functions so that count starts at 1 and it increases or decreases when it
104 hits the specified tags. when it hits zero the location has been found.
105
106 DecrementerTag and IncrementerTag are used to get around for/endfor and
107 similar paired types where the entire middle should be ignored.
108
109 If label is used it will be used instead of the count.
110
111 @param[in] Function The function to use to enumerate through the
112 list. Normally GetNextNode or GetPreviousNode.
113 @param[in] DecrementerTag The tag to decrement the count at.
114 @param[in] IncrementerTag The tag to increment the count at.
115 @param[in] Label A label to look for.
116 @param[in, out] ScriptFile The pointer to the current script file structure.
117 @param[in] MovePast TRUE makes function return 1 past the found
118 location.
119 @param[in] FindOnly TRUE to not change the ScriptFile.
120 @param[in] CommandNode The pointer to the Node to test.
121 @param[in, out] TargetCount The pointer to the current count.
122 **/
123 BOOLEAN
124 TestNodeForMove (
125 IN CONST LIST_MANIP_FUNC Function,
126 IN CONST CHAR16 *DecrementerTag,
127 IN CONST CHAR16 *IncrementerTag,
128 IN CONST CHAR16 *Label OPTIONAL,
129 IN OUT SCRIPT_FILE *ScriptFile,
130 IN CONST BOOLEAN MovePast,
131 IN CONST BOOLEAN FindOnly,
132 IN CONST SCRIPT_COMMAND_LIST *CommandNode,
133 IN OUT UINTN *TargetCount
134 )
135 {
136 BOOLEAN Found;
137 CHAR16 *CommandName;
138 CHAR16 *CommandNameWalker;
139 CHAR16 *TempLocation;
140
141 Found = FALSE;
142
143 //
144 // get just the first part of the command line...
145 //
146 CommandName = NULL;
147 CommandName = StrnCatGrow(&CommandName, NULL, CommandNode->Cl, 0);
148 if (CommandName == NULL) {
149 return (FALSE);
150 }
151
152 CommandNameWalker = CommandName;
153
154 //
155 // Skip leading spaces and tabs.
156 //
157 while ((CommandNameWalker[0] == L' ') || (CommandNameWalker[0] == L'\t')) {
158 CommandNameWalker++;
159 }
160 TempLocation = StrStr(CommandNameWalker, L" ");
161
162 if (TempLocation != NULL) {
163 *TempLocation = CHAR_NULL;
164 }
165
166 //
167 // did we find a nested item ?
168 //
169 if (gUnicodeCollation->StriColl(
170 gUnicodeCollation,
171 (CHAR16*)CommandNameWalker,
172 (CHAR16*)IncrementerTag) == 0) {
173 (*TargetCount)++;
174 } else if (gUnicodeCollation->StriColl(
175 gUnicodeCollation,
176 (CHAR16*)CommandNameWalker,
177 (CHAR16*)DecrementerTag) == 0) {
178 if (*TargetCount > 0) {
179 (*TargetCount)--;
180 }
181 }
182
183 //
184 // did we find the matching one...
185 //
186 if (Label == NULL) {
187 if (*TargetCount == 0) {
188 Found = TRUE;
189 if (!FindOnly) {
190 if (MovePast) {
191 ScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)(*Function)(&ScriptFile->CommandList, &CommandNode->Link);
192 } else {
193 ScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)CommandNode;
194 }
195 }
196 }
197 } else {
198 if (gUnicodeCollation->StriColl(
199 gUnicodeCollation,
200 (CHAR16*)CommandNameWalker,
201 (CHAR16*)Label) == 0
202 && (*TargetCount) == 0) {
203 Found = TRUE;
204 if (!FindOnly) {
205 //
206 // we found the target label without loops
207 //
208 if (MovePast) {
209 ScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)(*Function)(&ScriptFile->CommandList, &CommandNode->Link);
210 } else {
211 ScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)CommandNode;
212 }
213 }
214 }
215 }
216
217 //
218 // Free the memory for this loop...
219 //
220 FreePool(CommandName);
221 return (Found);
222 }
223
224 /**
225 Move the script pointer from 1 tag (line) to another.
226
227 It functions so that count starts at 1 and it increases or decreases when it
228 hits the specified tags. when it hits zero the location has been found.
229
230 DecrementerTag and IncrementerTag are used to get around for/endfor and
231 similar paired types where the entire middle should be ignored.
232
233 If label is used it will be used instead of the count.
234
235 @param[in] Function The function to use to enumerate through the
236 list. Normally GetNextNode or GetPreviousNode.
237 @param[in] DecrementerTag The tag to decrement the count at.
238 @param[in] IncrementerTag The tag to increment the count at.
239 @param[in] Label A label to look for.
240 @param[in, out] ScriptFile The pointer to the current script file structure.
241 @param[in] MovePast TRUE makes function return 1 past the found
242 location.
243 @param[in] FindOnly TRUE to not change the ScriptFile.
244 @param[in] WrapAroundScript TRUE to wrap end-to-begining or vise versa in
245 searching.
246 **/
247 BOOLEAN
248 MoveToTag (
249 IN CONST LIST_MANIP_FUNC Function,
250 IN CONST CHAR16 *DecrementerTag,
251 IN CONST CHAR16 *IncrementerTag,
252 IN CONST CHAR16 *Label OPTIONAL,
253 IN OUT SCRIPT_FILE *ScriptFile,
254 IN CONST BOOLEAN MovePast,
255 IN CONST BOOLEAN FindOnly,
256 IN CONST BOOLEAN WrapAroundScript
257 )
258 {
259 SCRIPT_COMMAND_LIST *CommandNode;
260 BOOLEAN Found;
261 UINTN TargetCount;
262
263 if (Label == NULL) {
264 TargetCount = 1;
265 } else {
266 TargetCount = 0;
267 }
268
269 if (ScriptFile == NULL) {
270 return FALSE;
271 }
272
273 for (CommandNode = (SCRIPT_COMMAND_LIST *)(*Function)(&ScriptFile->CommandList, &ScriptFile->CurrentCommand->Link), Found = FALSE
274 ; !IsNull(&ScriptFile->CommandList, &CommandNode->Link)&& !Found
275 ; CommandNode = (SCRIPT_COMMAND_LIST *)(*Function)(&ScriptFile->CommandList, &CommandNode->Link)
276 ){
277 Found = TestNodeForMove(
278 Function,
279 DecrementerTag,
280 IncrementerTag,
281 Label,
282 ScriptFile,
283 MovePast,
284 FindOnly,
285 CommandNode,
286 &TargetCount);
287 }
288
289 if (WrapAroundScript && !Found) {
290 for (CommandNode = (SCRIPT_COMMAND_LIST *)GetFirstNode(&ScriptFile->CommandList), Found = FALSE
291 ; CommandNode != ScriptFile->CurrentCommand && !Found
292 ; CommandNode = (SCRIPT_COMMAND_LIST *)(*Function)(&ScriptFile->CommandList, &CommandNode->Link)
293 ){
294 Found = TestNodeForMove(
295 Function,
296 DecrementerTag,
297 IncrementerTag,
298 Label,
299 ScriptFile,
300 MovePast,
301 FindOnly,
302 CommandNode,
303 &TargetCount);
304 }
305 }
306 return (Found);
307 }
308