]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellLevel1CommandsLib/For.c
ShellPkg: Update Level1 profile commands response output
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel1CommandsLib / For.c
1 /** @file
2 Main file for endfor and for shell level 1 functions.
3
4 Copyright (c) 2015, Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2009 - 2014, 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 #include <Library/PrintLib.h>
18
19 /**
20 Determine if a valid string is a valid number for the 'for' command.
21
22 @param[in] Number The pointer to the string representation of the number to test.
23
24 @retval TRUE The number is valid.
25 @retval FALSE The number is not valid.
26 **/
27 BOOLEAN
28 EFIAPI
29 ShellIsValidForNumber (
30 IN CONST CHAR16 *Number
31 )
32 {
33 if (Number == NULL || *Number == CHAR_NULL) {
34 return (FALSE);
35 }
36
37 if (*Number == L'-') {
38 Number++;
39 }
40
41 if (StrLen(Number) == 0) {
42 return (FALSE);
43 }
44
45 if (StrLen(Number) >= 7) {
46 if ((StrStr(Number, L" ") == NULL) || (((StrStr(Number, L" ") != NULL) && (StrStr(Number, L" ") - Number) >= 7))) {
47 return (FALSE);
48 }
49 }
50
51 if (!ShellIsDecimalDigitCharacter(*Number)) {
52 return (FALSE);
53 }
54
55 return (TRUE);
56 }
57
58 /**
59 Function for 'endfor' command.
60
61 @param[in] ImageHandle Handle to the Image (NULL if Internal).
62 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
63 **/
64 SHELL_STATUS
65 EFIAPI
66 ShellCommandRunEndFor (
67 IN EFI_HANDLE ImageHandle,
68 IN EFI_SYSTEM_TABLE *SystemTable
69 )
70 {
71 EFI_STATUS Status;
72 BOOLEAN Found;
73 SCRIPT_FILE *CurrentScriptFile;
74
75 Status = CommandInit();
76 ASSERT_EFI_ERROR(Status);
77
78 if (!gEfiShellProtocol->BatchIsActive()) {
79 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_NO_SCRIPT), gShellLevel1HiiHandle, L"endfor");
80 return (SHELL_UNSUPPORTED);
81 }
82
83 if (gEfiShellParametersProtocol->Argc > 1) {
84 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel1HiiHandle, L"endfor");
85 return (SHELL_INVALID_PARAMETER);
86 }
87
88 Found = MoveToTag(GetPreviousNode, L"for", L"endfor", NULL, ShellCommandGetCurrentScriptFile(), FALSE, FALSE, FALSE);
89
90 if (!Found) {
91 CurrentScriptFile = ShellCommandGetCurrentScriptFile();
92 ShellPrintHiiEx(
93 -1,
94 -1,
95 NULL,
96 STRING_TOKEN (STR_SYNTAX_NO_MATCHING),
97 gShellLevel1HiiHandle,
98 L"For",
99 L"EndFor",
100 CurrentScriptFile!=NULL
101 && CurrentScriptFile->CurrentCommand!=NULL
102 ? CurrentScriptFile->CurrentCommand->Line:0);
103 return (SHELL_NOT_FOUND);
104 }
105 return (SHELL_SUCCESS);
106 }
107
108 typedef struct {
109 UINT32 Signature;
110 INTN Current;
111 INTN End;
112 INTN Step;
113 CHAR16 *ReplacementName;
114 CHAR16 *CurrentValue;
115 BOOLEAN RemoveSubstAlias;
116 CHAR16 Set[1];
117 } SHELL_FOR_INFO;
118 #define SIZE_OF_SHELL_FOR_INFO OFFSET_OF (SHELL_FOR_INFO, Set)
119 #define SHELL_FOR_INFO_SIGNATURE SIGNATURE_32 ('S', 'F', 'I', 's')
120
121 /**
122 Update the value of a given alias on the list. If the alias is not there then add it.
123
124 @param[in] Alias The alias to test for.
125 @param[in] CommandString The updated command string.
126 @param[in, out] List The list to search.
127
128 @retval EFI_SUCCESS The operation was completed successfully.
129 @retval EFI_OUT_OF_RESOURCES There was not enough free memory.
130 **/
131 EFI_STATUS
132 EFIAPI
133 InternalUpdateAliasOnList(
134 IN CONST CHAR16 *Alias,
135 IN CONST CHAR16 *CommandString,
136 IN OUT LIST_ENTRY *List
137 )
138 {
139 ALIAS_LIST *Node;
140 BOOLEAN Found;
141
142 //
143 // assert for NULL parameter
144 //
145 ASSERT(Alias != NULL);
146
147 //
148 // check for the Alias
149 //
150 for ( Node = (ALIAS_LIST *)GetFirstNode(List), Found = FALSE
151 ; !IsNull(List, &Node->Link)
152 ; Node = (ALIAS_LIST *)GetNextNode(List, &Node->Link)
153 ){
154 ASSERT(Node->CommandString != NULL);
155 ASSERT(Node->Alias != NULL);
156 if (StrCmp(Node->Alias, Alias)==0) {
157 FreePool(Node->CommandString);
158 Node->CommandString = NULL;
159 Node->CommandString = StrnCatGrow(&Node->CommandString, NULL, CommandString, 0);
160 Found = TRUE;
161 break;
162 }
163 }
164 if (!Found) {
165 Node = AllocateZeroPool(sizeof(ALIAS_LIST));
166 if (Node == NULL) {
167 return (EFI_OUT_OF_RESOURCES);
168 }
169 ASSERT(Node->Alias == NULL);
170 Node->Alias = StrnCatGrow(&Node->Alias, NULL, Alias, 0);
171 ASSERT(Node->CommandString == NULL);
172 Node->CommandString = StrnCatGrow(&Node->CommandString, NULL, CommandString, 0);
173 InsertTailList(List, &Node->Link);
174 }
175 return (EFI_SUCCESS);
176 }
177
178 /**
179 Find out if an alias is on the given list.
180
181 @param[in] Alias The alias to test for.
182 @param[in] List The list to search.
183
184 @retval TRUE The alias is on the list.
185 @retval FALSE The alias is not on the list.
186 **/
187 BOOLEAN
188 EFIAPI
189 InternalIsAliasOnList(
190 IN CONST CHAR16 *Alias,
191 IN CONST LIST_ENTRY *List
192 )
193 {
194 ALIAS_LIST *Node;
195
196 //
197 // assert for NULL parameter
198 //
199 ASSERT(Alias != NULL);
200
201 //
202 // check for the Alias
203 //
204 for ( Node = (ALIAS_LIST *)GetFirstNode(List)
205 ; !IsNull(List, &Node->Link)
206 ; Node = (ALIAS_LIST *)GetNextNode(List, &Node->Link)
207 ){
208 ASSERT(Node->CommandString != NULL);
209 ASSERT(Node->Alias != NULL);
210 if (StrCmp(Node->Alias, Alias)==0) {
211 return (TRUE);
212 }
213 }
214 return (FALSE);
215 }
216
217 /**
218 Remove an alias from the given list.
219
220 @param[in] Alias The alias to remove.
221 @param[in, out] List The list to search.
222 **/
223 BOOLEAN
224 EFIAPI
225 InternalRemoveAliasFromList(
226 IN CONST CHAR16 *Alias,
227 IN OUT LIST_ENTRY *List
228 )
229 {
230 ALIAS_LIST *Node;
231
232 //
233 // assert for NULL parameter
234 //
235 ASSERT(Alias != NULL);
236
237 //
238 // check for the Alias
239 //
240 for ( Node = (ALIAS_LIST *)GetFirstNode(List)
241 ; !IsNull(List, &Node->Link)
242 ; Node = (ALIAS_LIST *)GetNextNode(List, &Node->Link)
243 ){
244 ASSERT(Node->CommandString != NULL);
245 ASSERT(Node->Alias != NULL);
246 if (StrCmp(Node->Alias, Alias)==0) {
247 RemoveEntryList(&Node->Link);
248 FreePool(Node->Alias);
249 FreePool(Node->CommandString);
250 FreePool(Node);
251 return (TRUE);
252 }
253 }
254 return (FALSE);
255 }
256
257 /**
258 Function to determine whether a string is decimal or hex representation of a number
259 and return the number converted from the string.
260
261 @param[in] String String representation of a number
262
263 @return the number
264 @retval (UINTN)(-1) An error ocurred.
265 **/
266 UINTN
267 EFIAPI
268 ReturnUintn(
269 IN CONST CHAR16 *String
270 )
271 {
272 UINT64 RetVal;
273
274 if (!EFI_ERROR(ShellConvertStringToUint64(String, &RetVal, FALSE, TRUE))) {
275 return ((UINTN)RetVal);
276 }
277 return ((UINTN)(-1));
278 }
279
280 /**
281 Function for 'for' command.
282
283 @param[in] ImageHandle Handle to the Image (NULL if Internal).
284 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
285 **/
286 SHELL_STATUS
287 EFIAPI
288 ShellCommandRunFor (
289 IN EFI_HANDLE ImageHandle,
290 IN EFI_SYSTEM_TABLE *SystemTable
291 )
292 {
293 EFI_STATUS Status;
294 SHELL_STATUS ShellStatus;
295 SCRIPT_FILE *CurrentScriptFile;
296 CHAR16 *ArgSet;
297 CHAR16 *ArgSetWalker;
298 CHAR16 *Parameter;
299 UINTN ArgSize;
300 UINTN LoopVar;
301 SHELL_FOR_INFO *Info;
302 CHAR16 *TempString;
303 CHAR16 *TempSpot;
304 BOOLEAN FirstPass;
305 EFI_SHELL_FILE_INFO *Node;
306 EFI_SHELL_FILE_INFO *FileList;
307 UINTN NewSize;
308
309 ArgSet = NULL;
310 ArgSize = 0;
311 ShellStatus = SHELL_SUCCESS;
312 ArgSetWalker = NULL;
313 TempString = NULL;
314 Parameter = NULL;
315 FirstPass = FALSE;
316
317 //
318 // initialize the shell lib (we must be in non-auto-init...)
319 //
320 Status = ShellInitialize();
321 ASSERT_EFI_ERROR(Status);
322
323 Status = CommandInit();
324 ASSERT_EFI_ERROR(Status);
325
326 if (!gEfiShellProtocol->BatchIsActive()) {
327 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_NO_SCRIPT), gShellLevel1HiiHandle, L"for");
328 return (SHELL_UNSUPPORTED);
329 }
330
331 if (gEfiShellParametersProtocol->Argc < 4) {
332 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel1HiiHandle, L"for");
333 return (SHELL_INVALID_PARAMETER);
334 }
335
336 CurrentScriptFile = ShellCommandGetCurrentScriptFile();
337 ASSERT(CurrentScriptFile != NULL);
338
339 if ((CurrentScriptFile->CurrentCommand != NULL) && (CurrentScriptFile->CurrentCommand->Data == NULL)) {
340 FirstPass = TRUE;
341
342 //
343 // Make sure that an End exists.
344 //
345 if (!MoveToTag(GetNextNode, L"endfor", L"for", NULL, CurrentScriptFile, TRUE, TRUE, FALSE)) {
346 ShellPrintHiiEx(
347 -1,
348 -1,
349 NULL,
350 STRING_TOKEN (STR_SYNTAX_NO_MATCHING),
351 gShellLevel1HiiHandle,
352 L"EndFor",
353 L"For",
354 CurrentScriptFile->CurrentCommand->Line);
355 return (SHELL_DEVICE_ERROR);
356 }
357
358 //
359 // Process the line.
360 //
361 if (gEfiShellParametersProtocol->Argv[1][0] != L'%' || gEfiShellParametersProtocol->Argv[1][2] != CHAR_NULL
362 ||!((gEfiShellParametersProtocol->Argv[1][1] >= L'a' && gEfiShellParametersProtocol->Argv[1][1] <= L'z')
363 ||(gEfiShellParametersProtocol->Argv[1][1] >= L'A' && gEfiShellParametersProtocol->Argv[1][1] <= L'Z'))
364 ) {
365 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_INV_VAR), gShellLevel1HiiHandle, gEfiShellParametersProtocol->Argv[1]);
366 return (SHELL_INVALID_PARAMETER);
367 }
368
369 if (gUnicodeCollation->StriColl(
370 gUnicodeCollation,
371 L"in",
372 gEfiShellParametersProtocol->Argv[2]) == 0) {
373 for (LoopVar = 0x3 ; LoopVar < gEfiShellParametersProtocol->Argc ; LoopVar++) {
374 ASSERT((ArgSet == NULL && ArgSize == 0) || (ArgSet != NULL));
375 if (StrStr(gEfiShellParametersProtocol->Argv[LoopVar], L"*") != NULL
376 ||StrStr(gEfiShellParametersProtocol->Argv[LoopVar], L"?") != NULL
377 ||StrStr(gEfiShellParametersProtocol->Argv[LoopVar], L"[") != NULL
378 ||StrStr(gEfiShellParametersProtocol->Argv[LoopVar], L"]") != NULL) {
379 FileList = NULL;
380 Status = ShellOpenFileMetaArg ((CHAR16*)gEfiShellParametersProtocol->Argv[LoopVar], EFI_FILE_MODE_READ, &FileList);
381 if (EFI_ERROR(Status) || FileList == NULL || IsListEmpty(&FileList->Link)) {
382 ArgSet = StrnCatGrow(&ArgSet, &ArgSize, L" \"", 0);
383 ArgSet = StrnCatGrow(&ArgSet, &ArgSize, gEfiShellParametersProtocol->Argv[LoopVar], 0);
384 ArgSet = StrnCatGrow(&ArgSet, &ArgSize, L"\"", 0);
385 } else {
386 for (Node = (EFI_SHELL_FILE_INFO *)GetFirstNode(&FileList->Link)
387 ; !IsNull(&FileList->Link, &Node->Link)
388 ; Node = (EFI_SHELL_FILE_INFO *)GetNextNode(&FileList->Link, &Node->Link)
389 ){
390 ArgSet = StrnCatGrow(&ArgSet, &ArgSize, L" \"", 0);
391 ArgSet = StrnCatGrow(&ArgSet, &ArgSize, Node->FullName, 0);
392 ArgSet = StrnCatGrow(&ArgSet, &ArgSize, L"\"", 0);
393 }
394 ShellCloseFileMetaArg(&FileList);
395 }
396 } else {
397 Parameter = gEfiShellParametersProtocol->Argv[LoopVar];
398 if (Parameter[0] == L'\"' && Parameter[StrLen(Parameter)-1] == L'\"') {
399 ArgSet = StrnCatGrow(&ArgSet, &ArgSize, L" ", 0);
400 ArgSet = StrnCatGrow(&ArgSet, &ArgSize, Parameter, 0);
401 } else {
402 ArgSet = StrnCatGrow(&ArgSet, &ArgSize, L" \"", 0);
403 ArgSet = StrnCatGrow(&ArgSet, &ArgSize, Parameter, 0);
404 ArgSet = StrnCatGrow(&ArgSet, &ArgSize, L"\"", 0);
405 }
406 }
407 }
408 if (ArgSet == NULL) {
409 ShellStatus = SHELL_OUT_OF_RESOURCES;
410 } else {
411 //
412 // set up for an 'in' for loop
413 //
414 NewSize = StrSize(ArgSet);
415 NewSize += sizeof(SHELL_FOR_INFO)+StrSize(gEfiShellParametersProtocol->Argv[1]);
416 Info = AllocateZeroPool(NewSize);
417 ASSERT(Info != NULL);
418 Info->Signature = SHELL_FOR_INFO_SIGNATURE;
419 CopyMem(Info->Set, ArgSet, StrSize(ArgSet));
420 NewSize = StrSize(gEfiShellParametersProtocol->Argv[1]);
421 CopyMem(Info->Set+(StrSize(ArgSet)/sizeof(Info->Set[0])), gEfiShellParametersProtocol->Argv[1], NewSize);
422 Info->ReplacementName = Info->Set+StrSize(ArgSet)/sizeof(Info->Set[0]);
423 Info->CurrentValue = (CHAR16*)Info->Set;
424 Info->Step = 0;
425 Info->Current = 0;
426 Info->End = 0;
427
428 if (InternalIsAliasOnList(Info->ReplacementName, &CurrentScriptFile->SubstList)) {
429 Info->RemoveSubstAlias = FALSE;
430 } else {
431 Info->RemoveSubstAlias = TRUE;
432 }
433 CurrentScriptFile->CurrentCommand->Data = Info;
434 }
435 } else if (gUnicodeCollation->StriColl(
436 gUnicodeCollation,
437 L"run",
438 gEfiShellParametersProtocol->Argv[2]) == 0) {
439 for (LoopVar = 0x3 ; LoopVar < gEfiShellParametersProtocol->Argc ; LoopVar++) {
440 ASSERT((ArgSet == NULL && ArgSize == 0) || (ArgSet != NULL));
441 if (ArgSet == NULL) {
442 // ArgSet = StrnCatGrow(&ArgSet, &ArgSize, L"\"", 0);
443 } else {
444 ArgSet = StrnCatGrow(&ArgSet, &ArgSize, L" ", 0);
445 }
446 ArgSet = StrnCatGrow(&ArgSet, &ArgSize, gEfiShellParametersProtocol->Argv[LoopVar], 0);
447 // ArgSet = StrnCatGrow(&ArgSet, &ArgSize, L" ", 0);
448 }
449 if (ArgSet == NULL) {
450 ShellStatus = SHELL_OUT_OF_RESOURCES;
451 } else {
452 //
453 // set up for a 'run' for loop
454 //
455 Info = AllocateZeroPool(sizeof(SHELL_FOR_INFO)+StrSize(gEfiShellParametersProtocol->Argv[1]));
456 ASSERT(Info != NULL);
457 Info->Signature = SHELL_FOR_INFO_SIGNATURE;
458 CopyMem(Info->Set, gEfiShellParametersProtocol->Argv[1], StrSize(gEfiShellParametersProtocol->Argv[1]));
459 Info->ReplacementName = Info->Set;
460 Info->CurrentValue = NULL;
461 ArgSetWalker = ArgSet;
462 if (ArgSetWalker[0] != L'(') {
463 ShellPrintHiiEx(
464 -1,
465 -1,
466 NULL,
467 STRING_TOKEN (STR_GEN_PROBLEM_SCRIPT),
468 gShellLevel1HiiHandle,
469 ArgSet,
470 CurrentScriptFile->CurrentCommand->Line);
471 ShellStatus = SHELL_INVALID_PARAMETER;
472 } else {
473 TempSpot = StrStr(ArgSetWalker, L")");
474 if (TempSpot != NULL) {
475 TempString = TempSpot+1;
476 if (*(TempString) != CHAR_NULL) {
477 while(TempString != NULL && *TempString == L' ') {
478 TempString++;
479 }
480 if (StrLen(TempString) > 0) {
481 TempSpot = NULL;
482 }
483 }
484 }
485 if (TempSpot == NULL) {
486 ShellPrintHiiEx(
487 -1,
488 -1,
489 NULL,
490 STRING_TOKEN (STR_GEN_PROBLEM_SCRIPT),
491 gShellLevel1HiiHandle,
492 CurrentScriptFile->CurrentCommand->Line);
493 ShellStatus = SHELL_INVALID_PARAMETER;
494 } else {
495 *TempSpot = CHAR_NULL;
496 ArgSetWalker++;
497 while (ArgSetWalker != NULL && ArgSetWalker[0] == L' ') {
498 ArgSetWalker++;
499 }
500 if (!ShellIsValidForNumber(ArgSetWalker)) {
501 ShellPrintHiiEx(
502 -1,
503 -1,
504 NULL,
505 STRING_TOKEN (STR_GEN_PROBLEM_SCRIPT),
506 gShellLevel1HiiHandle,
507 ArgSet,
508 CurrentScriptFile->CurrentCommand->Line);
509 ShellStatus = SHELL_INVALID_PARAMETER;
510 } else {
511 if (ArgSetWalker[0] == L'-') {
512 Info->Current = 0 - (INTN)ReturnUintn(ArgSetWalker+1);
513 } else {
514 Info->Current = (INTN)ReturnUintn(ArgSetWalker);
515 }
516 ArgSetWalker = StrStr(ArgSetWalker, L" ");
517 while (ArgSetWalker != NULL && ArgSetWalker[0] == L' ') {
518 ArgSetWalker++;
519 }
520 if (ArgSetWalker == NULL || *ArgSetWalker == CHAR_NULL || !ShellIsValidForNumber(ArgSetWalker)){
521 ShellPrintHiiEx(
522 -1,
523 -1,
524 NULL,
525 STRING_TOKEN (STR_GEN_PROBLEM_SCRIPT),
526 gShellLevel1HiiHandle,
527 ArgSet,
528 CurrentScriptFile->CurrentCommand->Line);
529 ShellStatus = SHELL_INVALID_PARAMETER;
530 } else {
531 if (ArgSetWalker[0] == L'-') {
532 Info->End = 0 - (INTN)ReturnUintn(ArgSetWalker+1);
533 } else {
534 Info->End = (INTN)ReturnUintn(ArgSetWalker);
535 }
536 if (Info->Current < Info->End) {
537 Info->Step = 1;
538 } else {
539 Info->Step = -1;
540 }
541
542 ArgSetWalker = StrStr(ArgSetWalker, L" ");
543 while (ArgSetWalker != NULL && ArgSetWalker[0] == L' ') {
544 ArgSetWalker++;
545 }
546 if (ArgSetWalker != NULL && *ArgSetWalker != CHAR_NULL) {
547 if (ArgSetWalker == NULL || *ArgSetWalker == CHAR_NULL || !ShellIsValidForNumber(ArgSetWalker)){
548 ShellPrintHiiEx(
549 -1,
550 -1,
551 NULL,
552 STRING_TOKEN (STR_GEN_PROBLEM_SCRIPT),
553 gShellLevel1HiiHandle,
554 ArgSet,
555 CurrentScriptFile->CurrentCommand->Line);
556 ShellStatus = SHELL_INVALID_PARAMETER;
557 } else {
558 if (*ArgSetWalker == L')') {
559 ASSERT(Info->Step == 1 || Info->Step == -1);
560 } else {
561 if (ArgSetWalker[0] == L'-') {
562 Info->Step = 0 - (INTN)ReturnUintn(ArgSetWalker+1);
563 } else {
564 Info->Step = (INTN)ReturnUintn(ArgSetWalker);
565 }
566
567 if (StrStr(ArgSetWalker, L" ") != NULL) {
568 ShellPrintHiiEx(
569 -1,
570 -1,
571 NULL,
572 STRING_TOKEN (STR_GEN_PROBLEM_SCRIPT),
573 gShellLevel1HiiHandle,
574 ArgSet,
575 CurrentScriptFile->CurrentCommand->Line);
576 ShellStatus = SHELL_INVALID_PARAMETER;
577 }
578 }
579 }
580
581 }
582 }
583 }
584 }
585 }
586 if (ShellStatus == SHELL_SUCCESS) {
587 if (InternalIsAliasOnList(Info->ReplacementName, &CurrentScriptFile->SubstList)) {
588 Info->RemoveSubstAlias = FALSE;
589 } else {
590 Info->RemoveSubstAlias = TRUE;
591 }
592 }
593 if (CurrentScriptFile->CurrentCommand != NULL) {
594 CurrentScriptFile->CurrentCommand->Data = Info;
595 }
596 }
597 } else {
598 ShellPrintHiiEx(
599 -1,
600 -1,
601 NULL,
602 STRING_TOKEN (STR_GEN_PROBLEM_SCRIPT),
603 gShellLevel1HiiHandle,
604 ArgSet,
605 CurrentScriptFile!=NULL
606 && CurrentScriptFile->CurrentCommand!=NULL
607 ? CurrentScriptFile->CurrentCommand->Line:0);
608 ShellStatus = SHELL_INVALID_PARAMETER;
609 }
610 } else {
611 //
612 // These need to be NULL since they are used to determine if this is the first pass later on...
613 //
614 ASSERT(ArgSetWalker == NULL);
615 ASSERT(ArgSet == NULL);
616 }
617
618 if (CurrentScriptFile != NULL && CurrentScriptFile->CurrentCommand != NULL) {
619 Info = (SHELL_FOR_INFO*)CurrentScriptFile->CurrentCommand->Data;
620 if (CurrentScriptFile->CurrentCommand->Reset) {
621 Info->CurrentValue = (CHAR16*)Info->Set;
622 FirstPass = TRUE;
623 CurrentScriptFile->CurrentCommand->Reset = FALSE;
624 }
625 } else {
626 ShellStatus = SHELL_UNSUPPORTED;
627 Info = NULL;
628 }
629 if (ShellStatus == SHELL_SUCCESS) {
630 ASSERT(Info != NULL);
631 if (Info->Step != 0) {
632 //
633 // only advance if not the first pass
634 //
635 if (!FirstPass) {
636 //
637 // sequence version of for loop...
638 //
639 Info->Current += Info->Step;
640 }
641
642 TempString = AllocateZeroPool(50*sizeof(CHAR16));
643 UnicodeSPrint(TempString, 50*sizeof(CHAR16), L"%d", Info->Current);
644 InternalUpdateAliasOnList(Info->ReplacementName, TempString, &CurrentScriptFile->SubstList);
645 FreePool(TempString);
646
647 if ((Info->Step > 0 && Info->Current > Info->End) || (Info->Step < 0 && Info->Current < Info->End)) {
648 CurrentScriptFile->CurrentCommand->Data = NULL;
649 //
650 // find the matching endfor (we're done with the loop)
651 //
652 if (!MoveToTag(GetNextNode, L"endfor", L"for", NULL, CurrentScriptFile, TRUE, FALSE, FALSE)) {
653 ShellPrintHiiEx(
654 -1,
655 -1,
656 NULL,
657 STRING_TOKEN (STR_SYNTAX_NO_MATCHING),
658 gShellLevel1HiiHandle,
659 L"EndFor",
660 L"For",
661 CurrentScriptFile!=NULL
662 && CurrentScriptFile->CurrentCommand!=NULL
663 ? CurrentScriptFile->CurrentCommand->Line:0);
664 ShellStatus = SHELL_DEVICE_ERROR;
665 }
666 if (Info->RemoveSubstAlias) {
667 //
668 // remove item from list
669 //
670 InternalRemoveAliasFromList(Info->ReplacementName, &CurrentScriptFile->SubstList);
671 }
672 FreePool(Info);
673 }
674 } else {
675 //
676 // Must be in 'in' version of for loop...
677 //
678 ASSERT(Info->Set != NULL);
679 if (Info->CurrentValue != NULL && *Info->CurrentValue != CHAR_NULL) {
680 if (Info->CurrentValue[0] == L' ') {
681 Info->CurrentValue++;
682 }
683 if (Info->CurrentValue[0] == L'\"') {
684 Info->CurrentValue++;
685 }
686 //
687 // do the next one of the set
688 //
689 ASSERT(TempString == NULL);
690 TempString = StrnCatGrow(&TempString, NULL, Info->CurrentValue, 0);
691 if (TempString == NULL) {
692 ShellStatus = SHELL_OUT_OF_RESOURCES;
693 } else {
694 TempSpot = StrStr(TempString, L"\" \"");
695 if (TempSpot != NULL) {
696 *TempSpot = CHAR_NULL;
697 }
698 while (TempString[StrLen(TempString)-1] == L'\"') {
699 TempString[StrLen(TempString)-1] = CHAR_NULL;
700 }
701 InternalUpdateAliasOnList(Info->ReplacementName, TempString, &CurrentScriptFile->SubstList);
702 Info->CurrentValue += StrLen(TempString);
703
704 if (Info->CurrentValue[0] == L'\"') {
705 Info->CurrentValue++;
706 }
707 FreePool(TempString);
708 }
709 } else {
710 CurrentScriptFile->CurrentCommand->Data = NULL;
711 //
712 // find the matching endfor (we're done with the loop)
713 //
714 if (!MoveToTag(GetNextNode, L"endfor", L"for", NULL, CurrentScriptFile, TRUE, FALSE, FALSE)) {
715 ShellPrintHiiEx(
716 -1,
717 -1,
718 NULL,
719 STRING_TOKEN (STR_SYNTAX_NO_MATCHING),
720 gShellLevel1HiiHandle,
721 L"EndFor",
722 L"For",
723 CurrentScriptFile!=NULL
724 && CurrentScriptFile->CurrentCommand!=NULL
725 ? CurrentScriptFile->CurrentCommand->Line:0);
726 ShellStatus = SHELL_DEVICE_ERROR;
727 }
728 if (Info->RemoveSubstAlias) {
729 //
730 // remove item from list
731 //
732 InternalRemoveAliasFromList(Info->ReplacementName, &CurrentScriptFile->SubstList);
733 }
734 FreePool(Info);
735 }
736 }
737 }
738 if (ArgSet != NULL) {
739 FreePool(ArgSet);
740 }
741 return (ShellStatus);
742 }
743