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