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