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