2 Main file for If and else shell level 1 function.
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
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.
15 #include "UefiShellLevel1CommandsLib.h"
16 #include <Library/PrintLib.h>
45 IN CONST CHAR16
**Statement
,
46 IN CONST CHAR16
*Fragment
53 Tester
= StrnCatGrow(&Tester
, NULL
, *Statement
, StrLen(Fragment
));
54 ASSERT(Tester
!= NULL
);
55 Tester
[StrLen(Fragment
)] = CHAR_NULL
;
56 if (gUnicodeCollation
->StriColl(
61 // increment the string pointer to the end of what we found and then chop off spaces...
63 *Statement
+=StrLen(Fragment
);
64 while (*Statement
[0] == L
' ') {
77 IN CONST CHAR16
*String
80 CONST CHAR16
*ProfilesString
;
81 CONST CHAR16
*TempLocation
;
83 ProfilesString
= ShellGetEnvironmentVariable(L
"profiles");
84 TempLocation
= StrStr(ProfilesString
, String
);
85 if ((TempLocation
!= NULL
) && (*(TempLocation
-1) == L
';') && (*(TempLocation
+StrLen(String
)) == L
';')) {
94 IN CONST CHAR16
*Compare1
,
95 IN CONST CHAR16
*Compare2
,
96 IN CONST BIN_OPERATOR_TYPE BinOp
,
97 IN CONST BOOLEAN CaseInsensitive
,
98 IN CONST BOOLEAN ForceStringCompare
105 // "Compare1 BinOp Compare2"
110 if (ForceStringCompare
|| !ShellIsHexOrDecimalNumber(Compare1
, FALSE
, FALSE
) || !ShellIsHexOrDecimalNumber(Compare2
, FALSE
, FALSE
)) {
114 if ((CaseInsensitive
&& StringNoCaseCompare(&Compare1
, &Compare2
) > 0) || (StringCompare(&Compare1
, &Compare2
) > 0)) {
121 if (Compare1
[0] == L
'-') {
122 Cmp1
= 0 - (INTN
)StrDecimalToUintn(Compare1
+1);
124 Cmp1
= (INTN
)StrDecimalToUintn(Compare1
);
126 if (Compare2
[0] == L
'-') {
127 Cmp2
= 0 - (INTN
)StrDecimalToUintn(Compare2
+1);
129 Cmp2
= (INTN
)StrDecimalToUintn(Compare2
);
131 if (BinOp
== OPERATOR_GT
) {
136 if ((UINTN
)Cmp1
> (UINTN
)Cmp2
) {
145 if (ForceStringCompare
|| !ShellIsHexOrDecimalNumber(Compare1
, FALSE
, FALSE
) || !ShellIsHexOrDecimalNumber(Compare2
, FALSE
, FALSE
)) {
149 if ((CaseInsensitive
&& StringNoCaseCompare(&Compare1
, &Compare2
) < 0) || (StringCompare(&Compare1
, &Compare2
) < 0)) {
156 if (Compare1
[0] == L
'-') {
157 Cmp1
= 0 - (INTN
)StrDecimalToUintn(Compare1
+1);
159 Cmp1
= (INTN
)StrDecimalToUintn(Compare1
);
161 if (Compare2
[0] == L
'-') {
162 Cmp2
= 0 - (INTN
)StrDecimalToUintn(Compare2
+1);
164 Cmp2
= (INTN
)StrDecimalToUintn(Compare2
);
166 if (BinOp
== OPERATOR_LT
) {
171 if ((UINTN
)Cmp1
< (UINTN
)Cmp2
) {
180 if (ForceStringCompare
|| !ShellIsHexOrDecimalNumber(Compare1
, FALSE
, FALSE
) || !ShellIsHexOrDecimalNumber(Compare2
, FALSE
, FALSE
)) {
184 if ((CaseInsensitive
&& StringNoCaseCompare(&Compare1
, &Compare2
) == 0) || (StringCompare(&Compare1
, &Compare2
) == 0)) {
191 if (Compare1
[0] == L
'-') {
192 Cmp1
= 0 - (INTN
)ShellStrToUintn(Compare1
+1);
194 Cmp1
= (INTN
)ShellStrToUintn(Compare1
);
196 if (Compare2
[0] == L
'-') {
197 Cmp2
= 0 - (INTN
)ShellStrToUintn(Compare2
+1);
199 Cmp2
= (INTN
)ShellStrToUintn(Compare2
);
208 if (ForceStringCompare
|| !ShellIsHexOrDecimalNumber(Compare1
, FALSE
, FALSE
) || !ShellIsHexOrDecimalNumber(Compare2
, FALSE
, FALSE
)) {
212 if ((CaseInsensitive
&& StringNoCaseCompare(&Compare1
, &Compare2
) != 0) || (StringCompare(&Compare1
, &Compare2
) != 0)) {
219 if (Compare1
[0] == L
'-') {
220 Cmp1
= 0 - (INTN
)StrDecimalToUintn(Compare1
+1);
222 Cmp1
= (INTN
)StrDecimalToUintn(Compare1
);
224 if (Compare2
[0] == L
'-') {
225 Cmp2
= 0 - (INTN
)StrDecimalToUintn(Compare2
+1);
227 Cmp2
= (INTN
)StrDecimalToUintn(Compare2
);
237 if (ForceStringCompare
|| !ShellIsHexOrDecimalNumber(Compare1
, FALSE
, FALSE
) || !ShellIsHexOrDecimalNumber(Compare2
, FALSE
, FALSE
)) {
241 if ((CaseInsensitive
&& StringNoCaseCompare(&Compare1
, &Compare2
) >= 0) || (StringCompare(&Compare1
, &Compare2
) >= 0)) {
248 if (Compare1
[0] == L
'-') {
249 Cmp1
= 0 - (INTN
)StrDecimalToUintn(Compare1
+1);
251 Cmp1
= (INTN
)StrDecimalToUintn(Compare1
);
253 if (Compare2
[0] == L
'-') {
254 Cmp2
= 0 - (INTN
)StrDecimalToUintn(Compare2
+1);
256 Cmp2
= (INTN
)StrDecimalToUintn(Compare2
);
258 if (BinOp
== OPERATOR_GE
) {
263 if ((UINTN
)Cmp1
>= (UINTN
)Cmp2
) {
272 if (ForceStringCompare
|| !ShellIsHexOrDecimalNumber(Compare1
, FALSE
, FALSE
) || !ShellIsHexOrDecimalNumber(Compare2
, FALSE
, FALSE
)) {
276 if ((CaseInsensitive
&& StringNoCaseCompare(&Compare1
, &Compare2
) <= 0) || (StringCompare(&Compare1
, &Compare2
) <= 0)) {
283 if (Compare1
[0] == L
'-') {
284 Cmp1
= 0 - (INTN
)StrDecimalToUintn(Compare1
+1);
286 Cmp1
= (INTN
)StrDecimalToUintn(Compare1
);
288 if (Compare2
[0] == L
'-') {
289 Cmp2
= 0 - (INTN
)StrDecimalToUintn(Compare2
+1);
291 Cmp2
= (INTN
)StrDecimalToUintn(Compare2
);
293 if (BinOp
== OPERATOR_LE
) {
298 if ((UINTN
)Cmp1
<= (UINTN
)Cmp2
) {
314 IN OUT BOOLEAN
*PassingState
,
315 IN UINTN StartParameterNumber
,
316 IN UINTN EndParameterNumber
,
317 IN CONST END_TAG_TYPE OperatorToUse
,
318 IN CONST BOOLEAN CaseInsensitive
,
319 IN CONST BOOLEAN ForceStringCompare
323 BOOLEAN OperationResult
;
325 CHAR16
*StatementWalker
;
326 BIN_OPERATOR_TYPE BinOp
;
329 CHAR16 HexString
[20];
332 ASSERT((END_TAG_TYPE
)OperatorToUse
!= END_TAG_THEN
);
334 Status
= EFI_SUCCESS
;
335 BinOp
= OPERATOR_MAX
;
336 OperationResult
= FALSE
;
337 StatementWalker
= gEfiShellParametersProtocol
->Argv
[StartParameterNumber
];
338 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"not")) {
340 StatementWalker
= gEfiShellParametersProtocol
->Argv
[++StartParameterNumber
];
346 // now check for 'boolfunc' operators
348 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"isint")) {
349 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && StatementWalker
[StrLen(StatementWalker
)-1] == L
')') {
350 StatementWalker
[StrLen(StatementWalker
)-1] = CHAR_NULL
;
351 OperationResult
= ShellIsHexOrDecimalNumber(StatementWalker
, FALSE
, FALSE
);
353 Status
= EFI_INVALID_PARAMETER
;
354 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"isint");
356 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"exists") || IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"exist")) {
357 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && StatementWalker
[StrLen(StatementWalker
)-1] == L
')') {
358 StatementWalker
[StrLen(StatementWalker
)-1] = CHAR_NULL
;
360 // is what remains a file in CWD???
362 OperationResult
= (BOOLEAN
)(ShellFileExists(StatementWalker
)==EFI_SUCCESS
);
363 } else if (StatementWalker
[0] == CHAR_NULL
&& StartParameterNumber
+1 == EndParameterNumber
) {
364 OperationResult
= (BOOLEAN
)(ShellFileExists(gEfiShellParametersProtocol
->Argv
[++StartParameterNumber
])==EFI_SUCCESS
);
366 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"exist(s)");
367 Status
= EFI_INVALID_PARAMETER
;
369 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"available")) {
370 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && StatementWalker
[StrLen(StatementWalker
)-1] == L
')') {
371 StatementWalker
[StrLen(StatementWalker
)-1] = CHAR_NULL
;
373 // is what remains a file in the CWD or path???
375 OperationResult
= (BOOLEAN
)(ShellIsFileInPath(StatementWalker
)==EFI_SUCCESS
);
377 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"available");
378 Status
= EFI_INVALID_PARAMETER
;
380 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"profile")) {
381 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && StatementWalker
[StrLen(StatementWalker
)-1] == L
')') {
385 StatementWalker
[StrLen(StatementWalker
)-1] = CHAR_NULL
;
386 OperationResult
= IsValidProfile(StatementWalker
);
388 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"profile");
389 Status
= EFI_INVALID_PARAMETER
;
391 } else if (StartParameterNumber
+1 >= EndParameterNumber
) {
392 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, gEfiShellParametersProtocol
->Argv
[StartParameterNumber
]);
393 Status
= EFI_INVALID_PARAMETER
;
396 // must be 'item binop item' style
400 BinOp
= OPERATOR_MAX
;
403 // get the first item
405 StatementWalker
= gEfiShellParametersProtocol
->Argv
[StartParameterNumber
];
406 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"efierror")) {
407 TempSpot
= StrStr(StatementWalker
, L
")");
408 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && TempSpot
!= NULL
) {
409 *TempSpot
= CHAR_NULL
;
410 if (ShellIsHexOrDecimalNumber(StatementWalker
, FALSE
, FALSE
)) {
411 UnicodeSPrint(HexString
, sizeof(HexString
), L
"0x%x", ShellStrToUintn(StatementWalker
)|MAX_BIT
);
412 ASSERT(Compare1
== NULL
);
413 Compare1
= StrnCatGrow(&Compare1
, NULL
, HexString
, 0);
414 StatementWalker
+= StrLen(StatementWalker
) + 1;
416 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"efierror");
417 Status
= EFI_INVALID_PARAMETER
;
420 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"efierror");
421 Status
= EFI_INVALID_PARAMETER
;
423 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"pierror")) {
424 TempSpot
= StrStr(StatementWalker
, L
")");
425 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && TempSpot
!= NULL
) {
426 *TempSpot
= CHAR_NULL
;
427 if (ShellIsHexOrDecimalNumber(StatementWalker
, FALSE
, FALSE
)) {
428 UnicodeSPrint(HexString
, sizeof(HexString
), L
"0x%x", ShellStrToUintn(StatementWalker
)|MAX_BIT
|(MAX_BIT
>>2));
429 ASSERT(Compare1
== NULL
);
430 Compare1
= StrnCatGrow(&Compare1
, NULL
, HexString
, 0);
431 StatementWalker
+= StrLen(StatementWalker
) + 1;
433 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"pierror");
434 Status
= EFI_INVALID_PARAMETER
;
437 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"pierror");
438 Status
= EFI_INVALID_PARAMETER
;
440 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"oemerror")) {
441 TempSpot
= StrStr(StatementWalker
, L
")");
442 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && TempSpot
!= NULL
) {
443 TempSpot
= CHAR_NULL
;
444 if (ShellIsHexOrDecimalNumber(StatementWalker
, FALSE
, FALSE
)) {
445 UnicodeSPrint(HexString
, sizeof(HexString
), L
"0x%x", ShellStrToUintn(StatementWalker
)|MAX_BIT
|(MAX_BIT
>>1));
446 ASSERT(Compare1
== NULL
);
447 Compare1
= StrnCatGrow(&Compare1
, NULL
, HexString
, 0);
448 StatementWalker
+= StrLen(StatementWalker
) + 1;
450 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"oemerror");
451 Status
= EFI_INVALID_PARAMETER
;
454 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"oemerror");
455 Status
= EFI_INVALID_PARAMETER
;
458 ASSERT(Compare1
== NULL
);
459 if (EndParameterNumber
- StartParameterNumber
> 2) {
460 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_STARTING
), gShellLevel1HiiHandle
, gEfiShellParametersProtocol
->Argv
[StartParameterNumber
+2]);
461 Status
= EFI_INVALID_PARAMETER
;
464 // must be a raw string
466 Compare1
= StrnCatGrow(&Compare1
, NULL
, StatementWalker
, 0);
473 ASSERT(StartParameterNumber
+1<EndParameterNumber
);
474 StatementWalker
= gEfiShellParametersProtocol
->Argv
[StartParameterNumber
+1];
475 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"gt")) {
477 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"lt")) {
479 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"eq")) {
481 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"ne")) {
483 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"ge")) {
485 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"le")) {
487 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"==")) {
489 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"ugt")) {
490 BinOp
= OPERATOR_UGT
;
491 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"ult")) {
492 BinOp
= OPERATOR_ULT
;
493 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"uge")) {
494 BinOp
= OPERATOR_UGE
;
495 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"ule")) {
496 BinOp
= OPERATOR_ULE
;
498 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_INVALID_BINOP
), gShellLevel1HiiHandle
, StatementWalker
);
499 Status
= EFI_INVALID_PARAMETER
;
503 // get the second item
505 ASSERT(StartParameterNumber
+2<=EndParameterNumber
);
506 StatementWalker
= gEfiShellParametersProtocol
->Argv
[StartParameterNumber
+2];
507 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"efierror")) {
508 TempSpot
= StrStr(StatementWalker
, L
")");
509 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && TempSpot
!= NULL
) {
510 TempSpot
= CHAR_NULL
;
511 if (ShellIsHexOrDecimalNumber(StatementWalker
, FALSE
, FALSE
)) {
512 UnicodeSPrint(HexString
, sizeof(HexString
), L
"0x%x", ShellStrToUintn(StatementWalker
)|MAX_BIT
);
513 ASSERT(Compare2
== NULL
);
514 Compare2
= StrnCatGrow(&Compare2
, NULL
, HexString
, 0);
515 StatementWalker
+= StrLen(StatementWalker
) + 1;
517 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"efierror");
518 Status
= EFI_INVALID_PARAMETER
;
521 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"efierror");
522 Status
= EFI_INVALID_PARAMETER
;
525 // can this be collapsed into the above?
527 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"pierror")) {
528 TempSpot
= StrStr(StatementWalker
, L
")");
529 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && TempSpot
!= NULL
) {
530 TempSpot
= CHAR_NULL
;
531 if (ShellIsHexOrDecimalNumber(StatementWalker
, FALSE
, FALSE
)) {
532 UnicodeSPrint(HexString
, sizeof(HexString
), L
"0x%x", ShellStrToUintn(StatementWalker
)|MAX_BIT
|(MAX_BIT
>>2));
533 ASSERT(Compare2
== NULL
);
534 Compare2
= StrnCatGrow(&Compare2
, NULL
, HexString
, 0);
535 StatementWalker
+= StrLen(StatementWalker
) + 1;
537 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"pierror");
538 Status
= EFI_INVALID_PARAMETER
;
541 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"pierror");
542 Status
= EFI_INVALID_PARAMETER
;
544 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"oemerror")) {
545 TempSpot
= StrStr(StatementWalker
, L
")");
546 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && TempSpot
!= NULL
) {
547 TempSpot
= CHAR_NULL
;
548 if (ShellIsHexOrDecimalNumber(StatementWalker
, FALSE
, FALSE
)) {
549 UnicodeSPrint(HexString
, sizeof(HexString
), L
"0x%x", ShellStrToUintn(StatementWalker
)|MAX_BIT
|(MAX_BIT
>>1));
550 ASSERT(Compare2
== NULL
);
551 Compare2
= StrnCatGrow(&Compare2
, NULL
, HexString
, 0);
552 StatementWalker
+= StrLen(StatementWalker
) + 1;
554 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"oemerror");
555 Status
= EFI_INVALID_PARAMETER
;
558 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"oemerror");
559 Status
= EFI_INVALID_PARAMETER
;
563 // must be a raw string
565 ASSERT(Compare2
== NULL
);
566 Compare2
= StrnCatGrow(&Compare2
, NULL
, StatementWalker
, 0);
569 if (Compare1
!= NULL
&& Compare2
!= NULL
&& BinOp
!= OPERATOR_MAX
) {
570 OperationResult
= TestOperation(Compare1
, Compare2
, BinOp
, CaseInsensitive
, ForceStringCompare
);
573 SHELL_FREE_NON_NULL(Compare1
);
574 SHELL_FREE_NON_NULL(Compare2
);
578 // done processing do result...
581 if (!EFI_ERROR(Status
)) {
583 OperationResult
= (BOOLEAN
)(!OperationResult
);
585 switch(OperatorToUse
) {
587 *PassingState
= (BOOLEAN
)(*PassingState
|| OperationResult
);
590 *PassingState
= (BOOLEAN
)(*PassingState
&& OperationResult
);
593 *PassingState
= (BOOLEAN
)(OperationResult
);
605 IN UINTN ParameterNumber
,
606 OUT UINTN
*EndParameter
,
607 OUT END_TAG_TYPE
*EndTag
613 *EndTag
= END_TAG_MAX
;
615 for(Buffer
= NULL
, BufferSize
= 0
616 ; ParameterNumber
< gEfiShellParametersProtocol
->Argc
619 if (gUnicodeCollation
->StriColl(
621 gEfiShellParametersProtocol
->Argv
[ParameterNumber
],
623 *EndParameter
= ParameterNumber
- 1;
624 *EndTag
= END_TAG_OR
;
626 } else if (gUnicodeCollation
->StriColl(
628 gEfiShellParametersProtocol
->Argv
[ParameterNumber
],
630 *EndParameter
= ParameterNumber
- 1;
631 *EndTag
= END_TAG_AND
;
633 } else if (gUnicodeCollation
->StriColl(
635 gEfiShellParametersProtocol
->Argv
[ParameterNumber
],
637 *EndParameter
= ParameterNumber
- 1;
638 *EndTag
= END_TAG_THEN
;
642 if (*EndTag
== END_TAG_MAX
) {
651 IN SCRIPT_FILE
*ScriptFile
654 SCRIPT_COMMAND_LIST
*CommandNode
;
658 CHAR16
*CommandWalker
;
659 CHAR16
*TempLocation
;
664 if (ScriptFile
== NULL
) {
668 for (CommandNode
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&ScriptFile
->CommandList
, &ScriptFile
->CurrentCommand
->Link
), Found
= FALSE
669 ; !IsNull(&ScriptFile
->CommandList
, &CommandNode
->Link
) && !Found
670 ; CommandNode
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&ScriptFile
->CommandList
, &CommandNode
->Link
)
674 // get just the first part of the command line...
677 CommandName
= StrnCatGrow(&CommandName
, NULL
, CommandNode
->Cl
, 0);
678 CommandWalker
= CommandName
;
679 while (CommandWalker
[0] == L
' ') {
682 TempLocation
= StrStr(CommandWalker
, L
" ");
684 if (TempLocation
!= NULL
) {
685 *TempLocation
= CHAR_NULL
;
689 // did we find a nested item ?
691 if (gUnicodeCollation
->StriColl(
693 (CHAR16
*)CommandWalker
,
696 } else if (TargetCount
== 1 && gUnicodeCollation
->StriColl(
698 (CHAR16
*)CommandWalker
,
699 (CHAR16
*)L
"else") == 0) {
701 // else can only decrement the last part... not an nested if
702 // hence the TargetCount compare added
705 } else if (gUnicodeCollation
->StriColl(
707 (CHAR16
*)CommandWalker
,
708 (CHAR16
*)L
"endif") == 0) {
711 if (TargetCount
== 0) {
712 ScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&ScriptFile
->CommandList
, &CommandNode
->Link
);
717 // Free the memory for this loop...
719 SHELL_FREE_NON_NULL(CommandName
);
726 PerformResultOperation (
727 IN CONST BOOLEAN Result
730 if (Result
|| MoveToTagSpecial(ShellCommandGetCurrentScriptFile())) {
731 return (EFI_SUCCESS
);
733 return (EFI_NOT_FOUND
);
737 Function for 'if' command.
739 @param[in] ImageHandle Handle to the Image (NULL if Internal).
740 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
745 IN EFI_HANDLE ImageHandle
,
746 IN EFI_SYSTEM_TABLE
*SystemTable
750 SHELL_STATUS ShellStatus
;
751 BOOLEAN CaseInsensitive
;
753 UINTN CurrentParameter
;
755 BOOLEAN CurrentValue
;
757 END_TAG_TYPE PreviousEnding
;
760 Status
= CommandInit();
761 ASSERT_EFI_ERROR(Status
);
763 if (!gEfiShellProtocol
->BatchIsActive()) {
764 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_NO_SCRIPT
), gShellLevel1HiiHandle
, L
"If");
765 return (SHELL_UNSUPPORTED
);
768 if (gEfiShellParametersProtocol
->Argc
< 3) {
769 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellLevel1HiiHandle
);
770 return (SHELL_INVALID_PARAMETER
);
774 // Make sure that an End exists.
776 if (!MoveToTag(GetNextNode
, L
"endif", L
"if", NULL
, ShellCommandGetCurrentScriptFile(), TRUE
, TRUE
, FALSE
)) {
777 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_NO_MATCHING
), gShellLevel1HiiHandle
, L
"EnfIf", L
"If", ShellCommandGetCurrentScriptFile()->CurrentCommand
->Line
);
778 return (SHELL_DEVICE_ERROR
);
782 // initialize the shell lib (we must be in non-auto-init...)
784 Status
= ShellInitialize();
785 ASSERT_EFI_ERROR(Status
);
787 CurrentParameter
= 1;
790 if (gUnicodeCollation
->StriColl(
792 gEfiShellParametersProtocol
->Argv
[1],
794 gUnicodeCollation
->StriColl(
796 gEfiShellParametersProtocol
->Argv
[2],
798 (gEfiShellParametersProtocol
->Argc
> 3 && gUnicodeCollation
->StriColl(
800 gEfiShellParametersProtocol
->Argv
[3],
802 CaseInsensitive
= TRUE
;
805 CaseInsensitive
= FALSE
;
807 if (gUnicodeCollation
->StriColl(
809 gEfiShellParametersProtocol
->Argv
[1],
811 gUnicodeCollation
->StriColl(
813 gEfiShellParametersProtocol
->Argv
[2],
815 (gEfiShellParametersProtocol
->Argc
> 3 && gUnicodeCollation
->StriColl(
817 gEfiShellParametersProtocol
->Argv
[3],
825 for ( ShellStatus
= SHELL_SUCCESS
, CurrentValue
= FALSE
, Ending
= END_TAG_MAX
826 ; CurrentParameter
< gEfiShellParametersProtocol
->Argc
&& ShellStatus
== SHELL_SUCCESS
827 ; CurrentParameter
++) {
828 if (gUnicodeCollation
->StriColl(
830 gEfiShellParametersProtocol
->Argv
[CurrentParameter
],
833 // we are at the then
835 if (CurrentParameter
+1 != gEfiShellParametersProtocol
->Argc
) {
836 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_TEXT_AFTER_THEN
), gShellLevel1HiiHandle
);
837 ShellStatus
= SHELL_INVALID_PARAMETER
;
839 Status
= PerformResultOperation(CurrentValue
);
840 if (EFI_ERROR(Status
)) {
841 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_AFTER_BAD
), gShellLevel1HiiHandle
, gEfiShellParametersProtocol
->Argv
[CurrentParameter
]);
842 ShellStatus
= SHELL_INVALID_PARAMETER
;
846 PreviousEnding
= Ending
;
848 // build up the next statement for analysis
850 if (!BuildNextStatement(CurrentParameter
, &EndParameter
, &Ending
)) {
851 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_NO_MATCHING
), gShellLevel1HiiHandle
, L
"Then", L
"If", ShellCommandGetCurrentScriptFile()->CurrentCommand
->Line
);
852 ShellStatus
= SHELL_INVALID_PARAMETER
;
855 // Analyze the statement
857 Status
= ProcessStatement(&CurrentValue
, CurrentParameter
, EndParameter
, PreviousEnding
, CaseInsensitive
, ForceString
);
858 if (EFI_ERROR(Status
)) {
859 // ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_STARTING), gShellLevel1HiiHandle, gEfiShellParametersProtocol->Argv[CurrentParameter]);
860 ShellStatus
= SHELL_INVALID_PARAMETER
;
863 // Optomize to get out of the loop early...
865 if ((Ending
== END_TAG_OR
&& CurrentValue
) || (Ending
== END_TAG_AND
&& !CurrentValue
)) {
866 Status
= PerformResultOperation(CurrentValue
);
867 if (EFI_ERROR(Status
)) {
868 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_AFTER_BAD
), gShellLevel1HiiHandle
, gEfiShellParametersProtocol
->Argv
[CurrentParameter
]);
869 ShellStatus
= SHELL_INVALID_PARAMETER
;
875 if (ShellStatus
== SHELL_SUCCESS
){
876 CurrentParameter
= EndParameter
;
878 // Skip over the or or and parameter.
880 if (Ending
== END_TAG_OR
|| Ending
== END_TAG_AND
) {
886 return (ShellStatus
);
890 Function for 'else' command.
892 @param[in] ImageHandle Handle to the Image (NULL if Internal).
893 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
897 ShellCommandRunElse (
898 IN EFI_HANDLE ImageHandle
,
899 IN EFI_SYSTEM_TABLE
*SystemTable
902 ASSERT_EFI_ERROR(CommandInit());
904 if (gEfiShellParametersProtocol
->Argc
> 1) {
905 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellLevel1HiiHandle
);
906 return (SHELL_INVALID_PARAMETER
);
909 if (!gEfiShellProtocol
->BatchIsActive()) {
910 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_NO_SCRIPT
), gShellLevel1HiiHandle
, L
"Else");
911 return (SHELL_UNSUPPORTED
);
915 if (!MoveToTag(GetPreviousNode
, L
"if", L
"endif", NULL
, ShellCommandGetCurrentScriptFile(), FALSE
, TRUE
, FALSE
)) {
916 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_NO_MATCHING
), gShellLevel1HiiHandle
, L
"If", L
"Else", ShellCommandGetCurrentScriptFile()->CurrentCommand
->Line
);
917 return (SHELL_DEVICE_ERROR
);
919 if (!MoveToTag(GetPreviousNode
, L
"if", L
"else", NULL
, ShellCommandGetCurrentScriptFile(), FALSE
, TRUE
, FALSE
)) {
920 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_NO_MATCHING
), gShellLevel1HiiHandle
, L
"If", L
"Else", ShellCommandGetCurrentScriptFile()->CurrentCommand
->Line
);
921 return (SHELL_DEVICE_ERROR
);
924 if (!MoveToTag(GetNextNode
, L
"endif", L
"if", NULL
, ShellCommandGetCurrentScriptFile(), FALSE
, FALSE
, FALSE
)) {
925 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_NO_MATCHING
), gShellLevel1HiiHandle
, L
"EndIf", "Else", ShellCommandGetCurrentScriptFile()->CurrentCommand
->Line
);
926 return (SHELL_DEVICE_ERROR
);
929 return (SHELL_SUCCESS
);
933 Function for 'endif' command.
935 @param[in] ImageHandle Handle to the Image (NULL if Internal).
936 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
940 ShellCommandRunEndIf (
941 IN EFI_HANDLE ImageHandle
,
942 IN EFI_SYSTEM_TABLE
*SystemTable
945 ASSERT_EFI_ERROR(CommandInit());
947 if (gEfiShellParametersProtocol
->Argc
> 1) {
948 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellLevel1HiiHandle
);
949 return (SHELL_INVALID_PARAMETER
);
952 if (!gEfiShellProtocol
->BatchIsActive()) {
953 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_NO_SCRIPT
), gShellLevel1HiiHandle
, L
"Endif");
954 return (SHELL_UNSUPPORTED
);
957 if (!MoveToTag(GetPreviousNode
, L
"if", L
"endif", NULL
, ShellCommandGetCurrentScriptFile(), FALSE
, TRUE
, FALSE
)) {
958 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_NO_MATCHING
), gShellLevel1HiiHandle
, L
"If", L
"EndIf", ShellCommandGetCurrentScriptFile()->CurrentCommand
->Line
);
959 return (SHELL_DEVICE_ERROR
);
962 return (SHELL_SUCCESS
);