]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellLevel1CommandsLib/UefiShellLevel1CommandsLib.c
47b6f92864931931b95ba423cb3cd6c5cb4e801e
[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 - 2010, 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 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_UNSUPPORTED);
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"for", ShellCommandRunFor , ShellCommandGetManFileNameLevel1, 1, L"", FALSE, gShellLevel1HiiHandle, (EFI_STRING_ID)(PcdGet8(PcdShellSupportLevel) < 3 ? 0 : STRING_TOKEN(STR_GET_HELP_FOR) ));
68 ShellCommandRegisterCommandName(L"goto", ShellCommandRunGoto , ShellCommandGetManFileNameLevel1, 1, L"", FALSE, gShellLevel1HiiHandle, (EFI_STRING_ID)(PcdGet8(PcdShellSupportLevel) < 3 ? 0 : STRING_TOKEN(STR_GET_HELP_GOTO) ));
69 ShellCommandRegisterCommandName(L"if", ShellCommandRunIf , ShellCommandGetManFileNameLevel1, 1, L"", FALSE, gShellLevel1HiiHandle, (EFI_STRING_ID)(PcdGet8(PcdShellSupportLevel) < 3 ? 0 : STRING_TOKEN(STR_GET_HELP_IF) ));
70 ShellCommandRegisterCommandName(L"shift", ShellCommandRunShift , ShellCommandGetManFileNameLevel1, 1, L"", FALSE, gShellLevel1HiiHandle, (EFI_STRING_ID)(PcdGet8(PcdShellSupportLevel) < 3 ? 0 : STRING_TOKEN(STR_GET_HELP_SHIFT) ));
71 ShellCommandRegisterCommandName(L"exit", ShellCommandRunExit , ShellCommandGetManFileNameLevel1, 1, L"", TRUE , gShellLevel1HiiHandle, (EFI_STRING_ID)(PcdGet8(PcdShellSupportLevel) < 3 ? 0 : STRING_TOKEN(STR_GET_HELP_EXIT) ));
72 ShellCommandRegisterCommandName(L"else", ShellCommandRunElse , ShellCommandGetManFileNameLevel1, 1, L"", FALSE, gShellLevel1HiiHandle, (EFI_STRING_ID)(PcdGet8(PcdShellSupportLevel) < 3 ? 0 : STRING_TOKEN(STR_GET_HELP_ELSE) ));
73 ShellCommandRegisterCommandName(L"endif", ShellCommandRunEndIf , ShellCommandGetManFileNameLevel1, 1, L"", FALSE, gShellLevel1HiiHandle, (EFI_STRING_ID)(PcdGet8(PcdShellSupportLevel) < 3 ? 0 : STRING_TOKEN(STR_GET_HELP_ENDIF) ));
74 ShellCommandRegisterCommandName(L"endfor", ShellCommandRunEndFor , ShellCommandGetManFileNameLevel1, 1, L"", FALSE, gShellLevel1HiiHandle, (EFI_STRING_ID)(PcdGet8(PcdShellSupportLevel) < 3 ? 0 : STRING_TOKEN(STR_GET_HELP_ENDFOR)));
75
76 return (EFI_SUCCESS);
77 }
78
79 /**
80 Destructor for the library. free any resources.
81 **/
82 EFI_STATUS
83 EFIAPI
84 ShellLevel1CommandsLibDestructor (
85 IN EFI_HANDLE ImageHandle,
86 IN EFI_SYSTEM_TABLE *SystemTable
87 )
88 {
89 if (gShellLevel1HiiHandle != NULL) {
90 HiiRemovePackages(gShellLevel1HiiHandle);
91 }
92 return (EFI_SUCCESS);
93 }
94
95 BOOLEAN
96 EFIAPI
97 TestNodeForMove (
98 IN CONST LIST_MANIP_FUNC Function,
99 IN CONST CHAR16 *DecrementerTag,
100 IN CONST CHAR16 *IncrementerTag,
101 IN CONST CHAR16 *Label OPTIONAL,
102 IN SCRIPT_FILE *ScriptFile,
103 IN CONST BOOLEAN MovePast,
104 IN CONST BOOLEAN FindOnly,
105 IN CONST SCRIPT_COMMAND_LIST *CommandNode,
106 IN UINTN *TargetCount
107 )
108 {
109 BOOLEAN Found;
110 CHAR16 *CommandName;
111 CHAR16 *CommandNameWalker;
112 CHAR16 *TempLocation;
113
114 Found = FALSE;
115
116 //
117 // get just the first part of the command line...
118 //
119 CommandName = NULL;
120 CommandName = StrnCatGrow(&CommandName, NULL, CommandNode->Cl, 0);
121 CommandNameWalker = CommandName;
122 while(CommandNameWalker[0] == L' ') {
123 CommandNameWalker++;
124 }
125 TempLocation = StrStr(CommandNameWalker, L" ");
126
127 if (TempLocation != NULL) {
128 *TempLocation = CHAR_NULL;
129 }
130
131 //
132 // did we find a nested item ?
133 //
134 if (gUnicodeCollation->StriColl(
135 gUnicodeCollation,
136 (CHAR16*)CommandNameWalker,
137 (CHAR16*)IncrementerTag) == 0) {
138 (*TargetCount)++;
139 } else if (gUnicodeCollation->StriColl(
140 gUnicodeCollation,
141 (CHAR16*)CommandNameWalker,
142 (CHAR16*)DecrementerTag) == 0) {
143 if (*TargetCount > 0) {
144 (*TargetCount)--;
145 }
146 }
147
148 //
149 // did we find the matching one...
150 //
151 if (Label == NULL) {
152 if (*TargetCount == 0) {
153 Found = TRUE;
154 if (!FindOnly) {
155 if (MovePast) {
156 ScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)(*Function)(&ScriptFile->CommandList, &CommandNode->Link);
157 } else {
158 ScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)CommandNode;
159 }
160 }
161 }
162 } else {
163 if (gUnicodeCollation->StriColl(
164 gUnicodeCollation,
165 (CHAR16*)CommandNameWalker,
166 (CHAR16*)Label) == 0
167 && (*TargetCount) == 0) {
168 Found = TRUE;
169 if (!FindOnly) {
170 //
171 // we found the target label without loops
172 //
173 if (MovePast) {
174 ScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)(*Function)(&ScriptFile->CommandList, &CommandNode->Link);
175 } else {
176 ScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)CommandNode;
177 }
178 }
179 }
180 }
181
182 //
183 // Free the memory for this loop...
184 //
185 FreePool(CommandName);
186 return (Found);
187 }
188
189 BOOLEAN
190 EFIAPI
191 MoveToTag (
192 IN CONST LIST_MANIP_FUNC Function,
193 IN CONST CHAR16 *DecrementerTag,
194 IN CONST CHAR16 *IncrementerTag,
195 IN CONST CHAR16 *Label OPTIONAL,
196 IN SCRIPT_FILE *ScriptFile,
197 IN CONST BOOLEAN MovePast,
198 IN CONST BOOLEAN FindOnly,
199 IN CONST BOOLEAN WrapAroundScript
200 )
201 {
202 SCRIPT_COMMAND_LIST *CommandNode;
203 BOOLEAN Found;
204 UINTN TargetCount;
205
206 if (Label == NULL) {
207 TargetCount = 1;
208 } else {
209 TargetCount = 0;
210 }
211
212 if (ScriptFile == NULL) {
213 return FALSE;
214 }
215
216 for (CommandNode = (SCRIPT_COMMAND_LIST *)(*Function)(&ScriptFile->CommandList, &ScriptFile->CurrentCommand->Link), Found = FALSE
217 ; !IsNull(&ScriptFile->CommandList, &CommandNode->Link)&& !Found
218 ; CommandNode = (SCRIPT_COMMAND_LIST *)(*Function)(&ScriptFile->CommandList, &CommandNode->Link)
219 ){
220 Found = TestNodeForMove(
221 Function,
222 DecrementerTag,
223 IncrementerTag,
224 Label,
225 ScriptFile,
226 MovePast,
227 FindOnly,
228 CommandNode,
229 &TargetCount);
230 }
231
232 if (WrapAroundScript && !Found) {
233 for (CommandNode = (SCRIPT_COMMAND_LIST *)GetFirstNode(&ScriptFile->CommandList), Found = FALSE
234 ; CommandNode != ScriptFile->CurrentCommand && !Found
235 ; CommandNode = (SCRIPT_COMMAND_LIST *)(*Function)(&ScriptFile->CommandList, &CommandNode->Link)
236 ){
237 Found = TestNodeForMove(
238 Function,
239 DecrementerTag,
240 IncrementerTag,
241 Label,
242 ScriptFile,
243 MovePast,
244 FindOnly,
245 CommandNode,
246 &TargetCount);
247 }
248 }
249 return (Found);
250 }
251