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
) {
316 IN OUT BOOLEAN
*PassingState
,
317 IN UINTN StartParameterNumber
,
318 IN UINTN EndParameterNumber
,
319 IN CONST END_TAG_TYPE OperatorToUse
,
320 IN CONST BOOLEAN CaseInsensitive
,
321 IN CONST BOOLEAN ForceStringCompare
325 BOOLEAN OperationResult
;
327 CHAR16
*StatementWalker
;
328 BIN_OPERATOR_TYPE BinOp
;
331 CHAR16 HexString
[20];
334 ASSERT((END_TAG_TYPE
)OperatorToUse
!= END_TAG_THEN
);
336 Status
= EFI_SUCCESS
;
337 BinOp
= OPERATOR_MAX
;
338 OperationResult
= FALSE
;
339 StatementWalker
= gEfiShellParametersProtocol
->Argv
[StartParameterNumber
];
340 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"not")) {
342 StatementWalker
= gEfiShellParametersProtocol
->Argv
[++StartParameterNumber
];
348 // now check for 'boolfunc' operators
350 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"isint")) {
351 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && StatementWalker
[StrLen(StatementWalker
)-1] == L
')') {
352 StatementWalker
[StrLen(StatementWalker
)-1] = CHAR_NULL
;
353 OperationResult
= ShellIsHexOrDecimalNumber(StatementWalker
, FALSE
, FALSE
);
355 Status
= EFI_INVALID_PARAMETER
;
356 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"isint");
358 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"exists") || IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"exist")) {
359 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && StatementWalker
[StrLen(StatementWalker
)-1] == L
')') {
360 StatementWalker
[StrLen(StatementWalker
)-1] = CHAR_NULL
;
362 // is what remains a file in CWD???
364 OperationResult
= (BOOLEAN
)(ShellFileExists(StatementWalker
)==EFI_SUCCESS
);
365 } else if (StatementWalker
[0] == CHAR_NULL
&& StartParameterNumber
+1 == EndParameterNumber
) {
366 OperationResult
= (BOOLEAN
)(ShellFileExists(gEfiShellParametersProtocol
->Argv
[++StartParameterNumber
])==EFI_SUCCESS
);
368 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"exist(s)");
369 Status
= EFI_INVALID_PARAMETER
;
371 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"available")) {
372 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && StatementWalker
[StrLen(StatementWalker
)-1] == L
')') {
373 StatementWalker
[StrLen(StatementWalker
)-1] = CHAR_NULL
;
375 // is what remains a file in the CWD or path???
377 OperationResult
= (BOOLEAN
)(ShellIsFileInPath(StatementWalker
)==EFI_SUCCESS
);
379 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"available");
380 Status
= EFI_INVALID_PARAMETER
;
382 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"profile")) {
383 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && StatementWalker
[StrLen(StatementWalker
)-1] == L
')') {
387 StatementWalker
[StrLen(StatementWalker
)-1] = CHAR_NULL
;
388 OperationResult
= IsValidProfile(StatementWalker
);
390 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"profile");
391 Status
= EFI_INVALID_PARAMETER
;
393 } else if (StartParameterNumber
+1 >= EndParameterNumber
) {
394 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, gEfiShellParametersProtocol
->Argv
[StartParameterNumber
]);
395 Status
= EFI_INVALID_PARAMETER
;
398 // must be 'item binop item' style
402 BinOp
= OPERATOR_MAX
;
405 // get the first item
407 StatementWalker
= gEfiShellParametersProtocol
->Argv
[StartParameterNumber
];
408 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"efierror")) {
409 TempSpot
= StrStr(StatementWalker
, L
")");
410 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && TempSpot
!= NULL
) {
411 *TempSpot
= CHAR_NULL
;
412 if (ShellIsHexOrDecimalNumber(StatementWalker
, FALSE
, FALSE
)) {
413 UnicodeSPrint(HexString
, sizeof(HexString
), L
"0x%x", ShellStrToUintn(StatementWalker
)|MAX_BIT
);
414 ASSERT(Compare1
== NULL
);
415 Compare1
= StrnCatGrow(&Compare1
, NULL
, HexString
, 0);
416 StatementWalker
+= StrLen(StatementWalker
) + 1;
418 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"efierror");
419 Status
= EFI_INVALID_PARAMETER
;
422 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"efierror");
423 Status
= EFI_INVALID_PARAMETER
;
425 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"pierror")) {
426 TempSpot
= StrStr(StatementWalker
, L
")");
427 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && TempSpot
!= NULL
) {
428 *TempSpot
= CHAR_NULL
;
429 if (ShellIsHexOrDecimalNumber(StatementWalker
, FALSE
, FALSE
)) {
430 UnicodeSPrint(HexString
, sizeof(HexString
), L
"0x%x", ShellStrToUintn(StatementWalker
)|MAX_BIT
|(MAX_BIT
>>2));
431 ASSERT(Compare1
== NULL
);
432 Compare1
= StrnCatGrow(&Compare1
, NULL
, HexString
, 0);
433 StatementWalker
+= StrLen(StatementWalker
) + 1;
435 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"pierror");
436 Status
= EFI_INVALID_PARAMETER
;
439 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"pierror");
440 Status
= EFI_INVALID_PARAMETER
;
442 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"oemerror")) {
443 TempSpot
= StrStr(StatementWalker
, L
")");
444 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && TempSpot
!= NULL
) {
445 TempSpot
= CHAR_NULL
;
446 if (ShellIsHexOrDecimalNumber(StatementWalker
, FALSE
, FALSE
)) {
447 UnicodeSPrint(HexString
, sizeof(HexString
), L
"0x%x", ShellStrToUintn(StatementWalker
)|MAX_BIT
|(MAX_BIT
>>1));
448 ASSERT(Compare1
== NULL
);
449 Compare1
= StrnCatGrow(&Compare1
, NULL
, HexString
, 0);
450 StatementWalker
+= StrLen(StatementWalker
) + 1;
452 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"oemerror");
453 Status
= EFI_INVALID_PARAMETER
;
456 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"oemerror");
457 Status
= EFI_INVALID_PARAMETER
;
460 ASSERT(Compare1
== NULL
);
461 if (EndParameterNumber
- StartParameterNumber
> 2) {
462 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_STARTING
), gShellLevel1HiiHandle
, gEfiShellParametersProtocol
->Argv
[StartParameterNumber
+2]);
463 Status
= EFI_INVALID_PARAMETER
;
466 // must be a raw string
468 Compare1
= StrnCatGrow(&Compare1
, NULL
, StatementWalker
, 0);
475 ASSERT(StartParameterNumber
+1<EndParameterNumber
);
476 StatementWalker
= gEfiShellParametersProtocol
->Argv
[StartParameterNumber
+1];
477 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"gt")) {
479 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"lt")) {
481 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"eq")) {
483 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"ne")) {
485 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"ge")) {
487 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"le")) {
489 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"==")) {
491 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"ugt")) {
492 BinOp
= OPERATOR_UGT
;
493 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"ult")) {
494 BinOp
= OPERATOR_ULT
;
495 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"uge")) {
496 BinOp
= OPERATOR_UGE
;
497 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"ule")) {
498 BinOp
= OPERATOR_ULE
;
500 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_INVALID_BINOP
), gShellLevel1HiiHandle
, StatementWalker
);
501 Status
= EFI_INVALID_PARAMETER
;
505 // get the second item
507 ASSERT(StartParameterNumber
+2<=EndParameterNumber
);
508 StatementWalker
= gEfiShellParametersProtocol
->Argv
[StartParameterNumber
+2];
509 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"efierror")) {
510 TempSpot
= StrStr(StatementWalker
, L
")");
511 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && TempSpot
!= NULL
) {
512 TempSpot
= CHAR_NULL
;
513 if (ShellIsHexOrDecimalNumber(StatementWalker
, FALSE
, FALSE
)) {
514 UnicodeSPrint(HexString
, sizeof(HexString
), L
"0x%x", ShellStrToUintn(StatementWalker
)|MAX_BIT
);
515 ASSERT(Compare2
== NULL
);
516 Compare2
= StrnCatGrow(&Compare2
, NULL
, HexString
, 0);
517 StatementWalker
+= StrLen(StatementWalker
) + 1;
519 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"efierror");
520 Status
= EFI_INVALID_PARAMETER
;
523 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"efierror");
524 Status
= EFI_INVALID_PARAMETER
;
527 // can this be collapsed into the above?
529 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"pierror")) {
530 TempSpot
= StrStr(StatementWalker
, L
")");
531 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && TempSpot
!= NULL
) {
532 TempSpot
= CHAR_NULL
;
533 if (ShellIsHexOrDecimalNumber(StatementWalker
, FALSE
, FALSE
)) {
534 UnicodeSPrint(HexString
, sizeof(HexString
), L
"0x%x", ShellStrToUintn(StatementWalker
)|MAX_BIT
|(MAX_BIT
>>2));
535 ASSERT(Compare2
== NULL
);
536 Compare2
= StrnCatGrow(&Compare2
, NULL
, HexString
, 0);
537 StatementWalker
+= StrLen(StatementWalker
) + 1;
539 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"pierror");
540 Status
= EFI_INVALID_PARAMETER
;
543 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"pierror");
544 Status
= EFI_INVALID_PARAMETER
;
546 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"oemerror")) {
547 TempSpot
= StrStr(StatementWalker
, L
")");
548 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && TempSpot
!= NULL
) {
549 TempSpot
= CHAR_NULL
;
550 if (ShellIsHexOrDecimalNumber(StatementWalker
, FALSE
, FALSE
)) {
551 UnicodeSPrint(HexString
, sizeof(HexString
), L
"0x%x", ShellStrToUintn(StatementWalker
)|MAX_BIT
|(MAX_BIT
>>1));
552 ASSERT(Compare2
== NULL
);
553 Compare2
= StrnCatGrow(&Compare2
, NULL
, HexString
, 0);
554 StatementWalker
+= StrLen(StatementWalker
) + 1;
556 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"oemerror");
557 Status
= EFI_INVALID_PARAMETER
;
560 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"oemerror");
561 Status
= EFI_INVALID_PARAMETER
;
565 // must be a raw string
567 ASSERT(Compare2
== NULL
);
568 Compare2
= StrnCatGrow(&Compare2
, NULL
, StatementWalker
, 0);
571 if (Compare1
!= NULL
&& Compare2
!= NULL
&& BinOp
!= OPERATOR_MAX
) {
572 OperationResult
= TestOperation(Compare1
, Compare2
, BinOp
, CaseInsensitive
, ForceStringCompare
);
575 SHELL_FREE_NON_NULL(Compare1
);
576 SHELL_FREE_NON_NULL(Compare2
);
580 // done processing do result...
583 if (!EFI_ERROR(Status
)) {
585 OperationResult
= (BOOLEAN
)(!OperationResult
);
587 switch(OperatorToUse
) {
589 *PassingState
= (BOOLEAN
)(*PassingState
|| OperationResult
);
592 *PassingState
= (BOOLEAN
)(*PassingState
&& OperationResult
);
595 *PassingState
= (BOOLEAN
)(OperationResult
);
607 IN UINTN ParameterNumber
,
608 OUT UINTN
*EndParameter
,
609 OUT END_TAG_TYPE
*EndTag
615 *EndTag
= END_TAG_MAX
;
617 for(Buffer
= NULL
, BufferSize
= 0
618 ; ParameterNumber
< gEfiShellParametersProtocol
->Argc
621 if (gUnicodeCollation
->StriColl(
623 gEfiShellParametersProtocol
->Argv
[ParameterNumber
],
625 *EndParameter
= ParameterNumber
- 1;
626 *EndTag
= END_TAG_OR
;
628 } else if (gUnicodeCollation
->StriColl(
630 gEfiShellParametersProtocol
->Argv
[ParameterNumber
],
632 *EndParameter
= ParameterNumber
- 1;
633 *EndTag
= END_TAG_AND
;
635 } else if (gUnicodeCollation
->StriColl(
637 gEfiShellParametersProtocol
->Argv
[ParameterNumber
],
639 *EndParameter
= ParameterNumber
- 1;
640 *EndTag
= END_TAG_THEN
;
644 if (*EndTag
== END_TAG_MAX
) {
653 IN SCRIPT_FILE
*ScriptFile
656 SCRIPT_COMMAND_LIST
*CommandNode
;
660 CHAR16
*CommandWalker
;
661 CHAR16
*TempLocation
;
666 if (ScriptFile
== NULL
) {
670 for (CommandNode
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&ScriptFile
->CommandList
, &ScriptFile
->CurrentCommand
->Link
), Found
= FALSE
671 ; !IsNull(&ScriptFile
->CommandList
, &CommandNode
->Link
) && !Found
672 ; CommandNode
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&ScriptFile
->CommandList
, &CommandNode
->Link
)
676 // get just the first part of the command line...
679 CommandName
= StrnCatGrow(&CommandName
, NULL
, CommandNode
->Cl
, 0);
680 CommandWalker
= CommandName
;
681 while (CommandWalker
[0] == L
' ') {
684 TempLocation
= StrStr(CommandWalker
, L
" ");
686 if (TempLocation
!= NULL
) {
687 *TempLocation
= CHAR_NULL
;
691 // did we find a nested item ?
693 if (gUnicodeCollation
->StriColl(
695 (CHAR16
*)CommandWalker
,
698 } else if (TargetCount
== 1 && gUnicodeCollation
->StriColl(
700 (CHAR16
*)CommandWalker
,
701 (CHAR16
*)L
"else") == 0) {
703 // else can only decrement the last part... not an nested if
704 // hence the TargetCount compare added
707 } else if (gUnicodeCollation
->StriColl(
709 (CHAR16
*)CommandWalker
,
710 (CHAR16
*)L
"endif") == 0) {
713 if (TargetCount
== 0) {
714 ScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&ScriptFile
->CommandList
, &CommandNode
->Link
);
719 // Free the memory for this loop...
721 SHELL_FREE_NON_NULL(CommandName
);
728 PerformResultOperation (
729 IN CONST BOOLEAN Result
732 if (Result
|| MoveToTagSpecial(ShellCommandGetCurrentScriptFile())) {
733 return (EFI_SUCCESS
);
735 return (EFI_NOT_FOUND
);
739 Function for 'if' command.
741 @param[in] ImageHandle Handle to the Image (NULL if Internal).
742 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
747 IN EFI_HANDLE ImageHandle
,
748 IN EFI_SYSTEM_TABLE
*SystemTable
752 SHELL_STATUS ShellStatus
;
753 BOOLEAN CaseInsensitive
;
755 UINTN CurrentParameter
;
757 BOOLEAN CurrentValue
;
759 END_TAG_TYPE PreviousEnding
;
762 Status
= CommandInit();
763 ASSERT_EFI_ERROR(Status
);
765 if (!gEfiShellProtocol
->BatchIsActive()) {
766 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_NO_SCRIPT
), gShellLevel1HiiHandle
, L
"If");
767 return (SHELL_UNSUPPORTED
);
770 if (gEfiShellParametersProtocol
->Argc
< 3) {
771 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellLevel1HiiHandle
);
772 return (SHELL_INVALID_PARAMETER
);
776 // Make sure that an End exists.
778 if (!MoveToTag(GetNextNode
, L
"endif", L
"if", NULL
, ShellCommandGetCurrentScriptFile(), TRUE
, TRUE
, FALSE
)) {
779 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_NO_MATCHING
), gShellLevel1HiiHandle
, L
"EnfIf", L
"If", ShellCommandGetCurrentScriptFile()->CurrentCommand
->Line
);
780 return (SHELL_DEVICE_ERROR
);
784 // initialize the shell lib (we must be in non-auto-init...)
786 Status
= ShellInitialize();
787 ASSERT_EFI_ERROR(Status
);
789 CurrentParameter
= 1;
792 if (gUnicodeCollation
->StriColl(
794 gEfiShellParametersProtocol
->Argv
[1],
796 gUnicodeCollation
->StriColl(
798 gEfiShellParametersProtocol
->Argv
[2],
800 (gEfiShellParametersProtocol
->Argc
> 3 && gUnicodeCollation
->StriColl(
802 gEfiShellParametersProtocol
->Argv
[3],
804 CaseInsensitive
= TRUE
;
807 CaseInsensitive
= FALSE
;
809 if (gUnicodeCollation
->StriColl(
811 gEfiShellParametersProtocol
->Argv
[1],
813 gUnicodeCollation
->StriColl(
815 gEfiShellParametersProtocol
->Argv
[2],
817 (gEfiShellParametersProtocol
->Argc
> 3 && gUnicodeCollation
->StriColl(
819 gEfiShellParametersProtocol
->Argv
[3],
827 for ( ShellStatus
= SHELL_SUCCESS
, CurrentValue
= FALSE
, Ending
= END_TAG_MAX
828 ; CurrentParameter
< gEfiShellParametersProtocol
->Argc
&& ShellStatus
== SHELL_SUCCESS
829 ; CurrentParameter
++) {
830 if (gUnicodeCollation
->StriColl(
832 gEfiShellParametersProtocol
->Argv
[CurrentParameter
],
835 // we are at the then
837 if (CurrentParameter
+1 != gEfiShellParametersProtocol
->Argc
) {
838 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_TEXT_AFTER_THEN
), gShellLevel1HiiHandle
);
839 ShellStatus
= SHELL_INVALID_PARAMETER
;
841 Status
= PerformResultOperation(CurrentValue
);
842 if (EFI_ERROR(Status
)) {
843 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_AFTER_BAD
), gShellLevel1HiiHandle
, gEfiShellParametersProtocol
->Argv
[CurrentParameter
]);
844 ShellStatus
= SHELL_INVALID_PARAMETER
;
848 PreviousEnding
= Ending
;
850 // build up the next statement for analysis
852 if (!BuildNextStatement(CurrentParameter
, &EndParameter
, &Ending
)) {
853 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_NO_MATCHING
), gShellLevel1HiiHandle
, L
"Then", L
"If", ShellCommandGetCurrentScriptFile()->CurrentCommand
->Line
);
854 ShellStatus
= SHELL_INVALID_PARAMETER
;
857 // Analyze the statement
859 Status
= ProcessStatement(&CurrentValue
, CurrentParameter
, EndParameter
, PreviousEnding
, CaseInsensitive
, ForceString
);
860 if (EFI_ERROR(Status
)) {
861 // ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_STARTING), gShellLevel1HiiHandle, gEfiShellParametersProtocol->Argv[CurrentParameter]);
862 ShellStatus
= SHELL_INVALID_PARAMETER
;
865 // Optomize to get out of the loop early...
867 if ((Ending
== END_TAG_OR
&& CurrentValue
) || (Ending
== END_TAG_AND
&& !CurrentValue
)) {
868 Status
= PerformResultOperation(CurrentValue
);
869 if (EFI_ERROR(Status
)) {
870 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_AFTER_BAD
), gShellLevel1HiiHandle
, gEfiShellParametersProtocol
->Argv
[CurrentParameter
]);
871 ShellStatus
= SHELL_INVALID_PARAMETER
;
877 if (ShellStatus
== SHELL_SUCCESS
){
878 CurrentParameter
= EndParameter
;
880 // Skip over the or or and parameter.
882 if (Ending
== END_TAG_OR
|| Ending
== END_TAG_AND
) {
888 return (ShellStatus
);
892 Function for 'else' command.
894 @param[in] ImageHandle Handle to the Image (NULL if Internal).
895 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
899 ShellCommandRunElse (
900 IN EFI_HANDLE ImageHandle
,
901 IN EFI_SYSTEM_TABLE
*SystemTable
904 ASSERT_EFI_ERROR(CommandInit());
906 if (gEfiShellParametersProtocol
->Argc
> 1) {
907 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellLevel1HiiHandle
);
908 return (SHELL_INVALID_PARAMETER
);
911 if (!gEfiShellProtocol
->BatchIsActive()) {
912 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_NO_SCRIPT
), gShellLevel1HiiHandle
, L
"Else");
913 return (SHELL_UNSUPPORTED
);
917 if (!MoveToTag(GetPreviousNode
, L
"if", L
"endif", NULL
, ShellCommandGetCurrentScriptFile(), FALSE
, TRUE
, FALSE
)) {
918 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_NO_MATCHING
), gShellLevel1HiiHandle
, L
"If", L
"Else", ShellCommandGetCurrentScriptFile()->CurrentCommand
->Line
);
919 return (SHELL_DEVICE_ERROR
);
921 if (!MoveToTag(GetPreviousNode
, L
"if", L
"else", NULL
, ShellCommandGetCurrentScriptFile(), FALSE
, TRUE
, FALSE
)) {
922 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_NO_MATCHING
), gShellLevel1HiiHandle
, L
"If", L
"Else", ShellCommandGetCurrentScriptFile()->CurrentCommand
->Line
);
923 return (SHELL_DEVICE_ERROR
);
926 if (!MoveToTag(GetNextNode
, L
"endif", L
"if", NULL
, ShellCommandGetCurrentScriptFile(), FALSE
, FALSE
, FALSE
)) {
927 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_NO_MATCHING
), gShellLevel1HiiHandle
, L
"EndIf", "Else", ShellCommandGetCurrentScriptFile()->CurrentCommand
->Line
);
928 return (SHELL_DEVICE_ERROR
);
931 return (SHELL_SUCCESS
);
935 Function for 'endif' command.
937 @param[in] ImageHandle Handle to the Image (NULL if Internal).
938 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
942 ShellCommandRunEndIf (
943 IN EFI_HANDLE ImageHandle
,
944 IN EFI_SYSTEM_TABLE
*SystemTable
947 ASSERT_EFI_ERROR(CommandInit());
949 if (gEfiShellParametersProtocol
->Argc
> 1) {
950 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellLevel1HiiHandle
);
951 return (SHELL_INVALID_PARAMETER
);
954 if (!gEfiShellProtocol
->BatchIsActive()) {
955 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_NO_SCRIPT
), gShellLevel1HiiHandle
, L
"Endif");
956 return (SHELL_UNSUPPORTED
);
959 if (!MoveToTag(GetPreviousNode
, L
"if", L
"endif", NULL
, ShellCommandGetCurrentScriptFile(), FALSE
, TRUE
, FALSE
)) {
960 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_NO_MATCHING
), gShellLevel1HiiHandle
, L
"If", L
"EndIf", ShellCommandGetCurrentScriptFile()->CurrentCommand
->Line
);
961 return (SHELL_DEVICE_ERROR
);
964 return (SHELL_SUCCESS
);