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
) {
313 IN OUT BOOLEAN
*PassingState
,
314 IN UINTN StartParameterNumber
,
315 IN UINTN EndParameterNumber
,
316 IN CONST END_TAG_TYPE OperatorToUse
,
317 IN CONST BOOLEAN CaseInsensitive
,
318 IN CONST BOOLEAN ForceStringCompare
322 BOOLEAN OperationResult
;
324 CHAR16
*StatementWalker
;
325 BIN_OPERATOR_TYPE BinOp
;
328 CHAR16 HexString
[20];
331 ASSERT((END_TAG_TYPE
)OperatorToUse
!= END_TAG_THEN
);
333 Status
= EFI_SUCCESS
;
334 BinOp
= OPERATOR_MAX
;
335 OperationResult
= FALSE
;
336 StatementWalker
= gEfiShellParametersProtocol
->Argv
[StartParameterNumber
];
337 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"not")) {
339 StatementWalker
= gEfiShellParametersProtocol
->Argv
[++StartParameterNumber
];
345 // now check for 'boolfunc' operators
347 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"isint")) {
348 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && StatementWalker
[StrLen(StatementWalker
)-1] == L
')') {
349 StatementWalker
[StrLen(StatementWalker
)-1] = CHAR_NULL
;
350 OperationResult
= ShellIsHexOrDecimalNumber(StatementWalker
, FALSE
, FALSE
);
352 Status
= EFI_INVALID_PARAMETER
;
353 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"isint");
355 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"exists") || IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"exist")) {
356 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && StatementWalker
[StrLen(StatementWalker
)-1] == L
')') {
357 StatementWalker
[StrLen(StatementWalker
)-1] = CHAR_NULL
;
359 // is what remains a file in CWD???
361 OperationResult
= (BOOLEAN
)(ShellFileExists(StatementWalker
)==EFI_SUCCESS
);
362 } else if (StatementWalker
[0] == CHAR_NULL
&& StartParameterNumber
+1 == EndParameterNumber
) {
363 OperationResult
= (BOOLEAN
)(ShellFileExists(gEfiShellParametersProtocol
->Argv
[++StartParameterNumber
])==EFI_SUCCESS
);
365 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"exist(s)");
366 Status
= EFI_INVALID_PARAMETER
;
368 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"available")) {
369 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && StatementWalker
[StrLen(StatementWalker
)-1] == L
')') {
370 StatementWalker
[StrLen(StatementWalker
)-1] = CHAR_NULL
;
372 // is what remains a file in the CWD or path???
374 OperationResult
= (BOOLEAN
)(ShellIsFileInPath(StatementWalker
)==EFI_SUCCESS
);
376 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"available");
377 Status
= EFI_INVALID_PARAMETER
;
379 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"profile")) {
380 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && StatementWalker
[StrLen(StatementWalker
)-1] == L
')') {
384 StatementWalker
[StrLen(StatementWalker
)-1] = CHAR_NULL
;
385 OperationResult
= IsValidProfile(StatementWalker
);
387 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"profile");
388 Status
= EFI_INVALID_PARAMETER
;
390 } else if (StartParameterNumber
+1 >= EndParameterNumber
) {
391 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, gEfiShellParametersProtocol
->Argv
[StartParameterNumber
]);
392 Status
= EFI_INVALID_PARAMETER
;
395 // must be 'item binop item' style
399 BinOp
= OPERATOR_MAX
;
402 // get the first item
404 StatementWalker
= gEfiShellParametersProtocol
->Argv
[StartParameterNumber
];
405 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"efierror")) {
406 TempSpot
= StrStr(StatementWalker
, L
")");
407 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && TempSpot
!= NULL
) {
408 *TempSpot
= CHAR_NULL
;
409 if (ShellIsHexOrDecimalNumber(StatementWalker
, FALSE
, FALSE
)) {
410 UnicodeSPrint(HexString
, sizeof(HexString
), L
"0x%x", ShellStrToUintn(StatementWalker
)|MAX_BIT
);
411 ASSERT(Compare1
== NULL
);
412 Compare1
= StrnCatGrow(&Compare1
, NULL
, HexString
, 0);
413 StatementWalker
+= StrLen(StatementWalker
) + 1;
415 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"efierror");
416 Status
= EFI_INVALID_PARAMETER
;
419 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"efierror");
420 Status
= EFI_INVALID_PARAMETER
;
422 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"pierror")) {
423 TempSpot
= StrStr(StatementWalker
, L
")");
424 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && TempSpot
!= NULL
) {
425 *TempSpot
= CHAR_NULL
;
426 if (ShellIsHexOrDecimalNumber(StatementWalker
, FALSE
, FALSE
)) {
427 UnicodeSPrint(HexString
, sizeof(HexString
), L
"0x%x", ShellStrToUintn(StatementWalker
)|MAX_BIT
|(MAX_BIT
>>2));
428 ASSERT(Compare1
== NULL
);
429 Compare1
= StrnCatGrow(&Compare1
, NULL
, HexString
, 0);
430 StatementWalker
+= StrLen(StatementWalker
) + 1;
432 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"pierror");
433 Status
= EFI_INVALID_PARAMETER
;
436 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"pierror");
437 Status
= EFI_INVALID_PARAMETER
;
439 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"oemerror")) {
440 TempSpot
= StrStr(StatementWalker
, L
")");
441 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && TempSpot
!= NULL
) {
442 TempSpot
= CHAR_NULL
;
443 if (ShellIsHexOrDecimalNumber(StatementWalker
, FALSE
, FALSE
)) {
444 UnicodeSPrint(HexString
, sizeof(HexString
), L
"0x%x", ShellStrToUintn(StatementWalker
)|MAX_BIT
|(MAX_BIT
>>1));
445 ASSERT(Compare1
== NULL
);
446 Compare1
= StrnCatGrow(&Compare1
, NULL
, HexString
, 0);
447 StatementWalker
+= StrLen(StatementWalker
) + 1;
449 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"oemerror");
450 Status
= EFI_INVALID_PARAMETER
;
453 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"oemerror");
454 Status
= EFI_INVALID_PARAMETER
;
457 ASSERT(Compare1
== NULL
);
458 if (EndParameterNumber
- StartParameterNumber
> 2) {
459 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_STARTING
), gShellLevel1HiiHandle
, gEfiShellParametersProtocol
->Argv
[StartParameterNumber
+2]);
460 Status
= EFI_INVALID_PARAMETER
;
463 // must be a raw string
465 Compare1
= StrnCatGrow(&Compare1
, NULL
, StatementWalker
, 0);
472 ASSERT(StartParameterNumber
+1<EndParameterNumber
);
473 StatementWalker
= gEfiShellParametersProtocol
->Argv
[StartParameterNumber
+1];
474 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"gt")) {
476 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"lt")) {
478 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"eq")) {
480 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"ne")) {
482 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"ge")) {
484 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"le")) {
486 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"==")) {
488 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"ugt")) {
489 BinOp
= OPERATOR_UGT
;
490 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"ult")) {
491 BinOp
= OPERATOR_ULT
;
492 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"uge")) {
493 BinOp
= OPERATOR_UGE
;
494 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"ule")) {
495 BinOp
= OPERATOR_ULE
;
497 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_INVALID_BINOP
), gShellLevel1HiiHandle
, StatementWalker
);
498 Status
= EFI_INVALID_PARAMETER
;
502 // get the second item
504 ASSERT(StartParameterNumber
+2<=EndParameterNumber
);
505 StatementWalker
= gEfiShellParametersProtocol
->Argv
[StartParameterNumber
+2];
506 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"efierror")) {
507 TempSpot
= StrStr(StatementWalker
, L
")");
508 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && TempSpot
!= NULL
) {
509 TempSpot
= CHAR_NULL
;
510 if (ShellIsHexOrDecimalNumber(StatementWalker
, FALSE
, FALSE
)) {
511 UnicodeSPrint(HexString
, sizeof(HexString
), L
"0x%x", ShellStrToUintn(StatementWalker
)|MAX_BIT
);
512 ASSERT(Compare2
== NULL
);
513 Compare2
= StrnCatGrow(&Compare2
, NULL
, HexString
, 0);
514 StatementWalker
+= StrLen(StatementWalker
) + 1;
516 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"efierror");
517 Status
= EFI_INVALID_PARAMETER
;
520 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"efierror");
521 Status
= EFI_INVALID_PARAMETER
;
524 // can this be collapsed into the above?
526 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"pierror")) {
527 TempSpot
= StrStr(StatementWalker
, L
")");
528 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && TempSpot
!= NULL
) {
529 TempSpot
= CHAR_NULL
;
530 if (ShellIsHexOrDecimalNumber(StatementWalker
, FALSE
, FALSE
)) {
531 UnicodeSPrint(HexString
, sizeof(HexString
), L
"0x%x", ShellStrToUintn(StatementWalker
)|MAX_BIT
|(MAX_BIT
>>2));
532 ASSERT(Compare2
== NULL
);
533 Compare2
= StrnCatGrow(&Compare2
, NULL
, HexString
, 0);
534 StatementWalker
+= StrLen(StatementWalker
) + 1;
536 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"pierror");
537 Status
= EFI_INVALID_PARAMETER
;
540 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"pierror");
541 Status
= EFI_INVALID_PARAMETER
;
543 } else if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"oemerror")) {
544 TempSpot
= StrStr(StatementWalker
, L
")");
545 if (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(") && TempSpot
!= NULL
) {
546 TempSpot
= CHAR_NULL
;
547 if (ShellIsHexOrDecimalNumber(StatementWalker
, FALSE
, FALSE
)) {
548 UnicodeSPrint(HexString
, sizeof(HexString
), L
"0x%x", ShellStrToUintn(StatementWalker
)|MAX_BIT
|(MAX_BIT
>>1));
549 ASSERT(Compare2
== NULL
);
550 Compare2
= StrnCatGrow(&Compare2
, NULL
, HexString
, 0);
551 StatementWalker
+= StrLen(StatementWalker
) + 1;
553 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"oemerror");
554 Status
= EFI_INVALID_PARAMETER
;
557 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"oemerror");
558 Status
= EFI_INVALID_PARAMETER
;
562 // must be a raw string
564 ASSERT(Compare2
== NULL
);
565 Compare2
= StrnCatGrow(&Compare2
, NULL
, StatementWalker
, 0);
568 if (Compare1
!= NULL
&& Compare2
!= NULL
&& BinOp
!= OPERATOR_MAX
) {
569 OperationResult
= TestOperation(Compare1
, Compare2
, BinOp
, CaseInsensitive
, ForceStringCompare
);
572 SHELL_FREE_NON_NULL(Compare1
);
573 SHELL_FREE_NON_NULL(Compare2
);
577 // done processing do result...
580 if (!EFI_ERROR(Status
)) {
582 OperationResult
= (BOOLEAN
)(!OperationResult
);
584 switch(OperatorToUse
) {
586 *PassingState
= (BOOLEAN
)(*PassingState
|| OperationResult
);
589 *PassingState
= (BOOLEAN
)(*PassingState
&& OperationResult
);
592 *PassingState
= (BOOLEAN
)(OperationResult
);
604 IN UINTN ParameterNumber
,
605 OUT UINTN
*EndParameter
,
606 OUT END_TAG_TYPE
*EndTag
612 *EndTag
= END_TAG_MAX
;
614 for(Buffer
= NULL
, BufferSize
= 0
615 ; ParameterNumber
< gEfiShellParametersProtocol
->Argc
618 if (gUnicodeCollation
->StriColl(
620 gEfiShellParametersProtocol
->Argv
[ParameterNumber
],
622 *EndParameter
= ParameterNumber
- 1;
623 *EndTag
= END_TAG_OR
;
625 } else if (gUnicodeCollation
->StriColl(
627 gEfiShellParametersProtocol
->Argv
[ParameterNumber
],
629 *EndParameter
= ParameterNumber
- 1;
630 *EndTag
= END_TAG_AND
;
632 } else if (gUnicodeCollation
->StriColl(
634 gEfiShellParametersProtocol
->Argv
[ParameterNumber
],
636 *EndParameter
= ParameterNumber
- 1;
637 *EndTag
= END_TAG_THEN
;
641 if (*EndTag
== END_TAG_MAX
) {
650 IN SCRIPT_FILE
*ScriptFile
653 SCRIPT_COMMAND_LIST
*CommandNode
;
657 CHAR16
*CommandWalker
;
658 CHAR16
*TempLocation
;
663 if (ScriptFile
== NULL
) {
667 for (CommandNode
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&ScriptFile
->CommandList
, &ScriptFile
->CurrentCommand
->Link
), Found
= FALSE
668 ; !IsNull(&ScriptFile
->CommandList
, &CommandNode
->Link
) && !Found
669 ; CommandNode
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&ScriptFile
->CommandList
, &CommandNode
->Link
)
673 // get just the first part of the command line...
676 CommandName
= StrnCatGrow(&CommandName
, NULL
, CommandNode
->Cl
, 0);
677 CommandWalker
= CommandName
;
678 while (CommandWalker
[0] == L
' ') {
681 TempLocation
= StrStr(CommandWalker
, L
" ");
683 if (TempLocation
!= NULL
) {
684 *TempLocation
= CHAR_NULL
;
688 // did we find a nested item ?
690 if (gUnicodeCollation
->StriColl(
692 (CHAR16
*)CommandWalker
,
695 } else if (TargetCount
== 1 && gUnicodeCollation
->StriColl(
697 (CHAR16
*)CommandWalker
,
698 (CHAR16
*)L
"else") == 0) {
700 // else can only decrement the last part... not an nested if
701 // hence the TargetCount compare added
704 } else if (gUnicodeCollation
->StriColl(
706 (CHAR16
*)CommandWalker
,
707 (CHAR16
*)L
"endif") == 0) {
710 if (TargetCount
== 0) {
711 ScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&ScriptFile
->CommandList
, &CommandNode
->Link
);
716 // Free the memory for this loop...
718 SHELL_FREE_NON_NULL(CommandName
);
725 PerformResultOperation (
726 IN CONST BOOLEAN Result
729 if (Result
|| MoveToTagSpecial(ShellCommandGetCurrentScriptFile())) {
730 return (EFI_SUCCESS
);
732 return (EFI_NOT_FOUND
);
736 Function for 'if' command.
738 @param[in] ImageHandle Handle to the Image (NULL if Internal).
739 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
744 IN EFI_HANDLE ImageHandle
,
745 IN EFI_SYSTEM_TABLE
*SystemTable
749 SHELL_STATUS ShellStatus
;
750 BOOLEAN CaseInsensitive
;
752 UINTN CurrentParameter
;
754 BOOLEAN CurrentValue
;
756 END_TAG_TYPE PreviousEnding
;
759 Status
= CommandInit();
760 ASSERT_EFI_ERROR(Status
);
762 if (!gEfiShellProtocol
->BatchIsActive()) {
763 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_NO_SCRIPT
), gShellLevel1HiiHandle
, L
"If");
764 return (SHELL_UNSUPPORTED
);
767 if (gEfiShellParametersProtocol
->Argc
< 3) {
768 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellLevel1HiiHandle
);
769 return (SHELL_INVALID_PARAMETER
);
773 // Make sure that an End exists.
775 if (!MoveToTag(GetNextNode
, L
"endif", L
"if", NULL
, ShellCommandGetCurrentScriptFile(), TRUE
, TRUE
, FALSE
)) {
776 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_NO_MATCHING
), gShellLevel1HiiHandle
, L
"EnfIf", L
"If", ShellCommandGetCurrentScriptFile()->CurrentCommand
->Line
);
777 return (SHELL_DEVICE_ERROR
);
781 // initialize the shell lib (we must be in non-auto-init...)
783 Status
= ShellInitialize();
784 ASSERT_EFI_ERROR(Status
);
786 CurrentParameter
= 1;
789 if (gUnicodeCollation
->StriColl(
791 gEfiShellParametersProtocol
->Argv
[1],
793 gUnicodeCollation
->StriColl(
795 gEfiShellParametersProtocol
->Argv
[2],
797 (gEfiShellParametersProtocol
->Argc
> 3 && gUnicodeCollation
->StriColl(
799 gEfiShellParametersProtocol
->Argv
[3],
801 CaseInsensitive
= TRUE
;
804 CaseInsensitive
= FALSE
;
806 if (gUnicodeCollation
->StriColl(
808 gEfiShellParametersProtocol
->Argv
[1],
810 gUnicodeCollation
->StriColl(
812 gEfiShellParametersProtocol
->Argv
[2],
814 (gEfiShellParametersProtocol
->Argc
> 3 && gUnicodeCollation
->StriColl(
816 gEfiShellParametersProtocol
->Argv
[3],
824 for ( ShellStatus
= SHELL_SUCCESS
, CurrentValue
= FALSE
, Ending
= END_TAG_MAX
825 ; CurrentParameter
< gEfiShellParametersProtocol
->Argc
&& ShellStatus
== SHELL_SUCCESS
826 ; CurrentParameter
++) {
827 if (gUnicodeCollation
->StriColl(
829 gEfiShellParametersProtocol
->Argv
[CurrentParameter
],
832 // we are at the then
834 if (CurrentParameter
+1 != gEfiShellParametersProtocol
->Argc
) {
835 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_TEXT_AFTER_THEN
), gShellLevel1HiiHandle
);
836 ShellStatus
= SHELL_INVALID_PARAMETER
;
838 Status
= PerformResultOperation(CurrentValue
);
839 if (EFI_ERROR(Status
)) {
840 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_AFTER_BAD
), gShellLevel1HiiHandle
, gEfiShellParametersProtocol
->Argv
[CurrentParameter
]);
841 ShellStatus
= SHELL_INVALID_PARAMETER
;
845 PreviousEnding
= Ending
;
847 // build up the next statement for analysis
849 if (!BuildNextStatement(CurrentParameter
, &EndParameter
, &Ending
)) {
850 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_NO_MATCHING
), gShellLevel1HiiHandle
, L
"Then", L
"If", ShellCommandGetCurrentScriptFile()->CurrentCommand
->Line
);
851 ShellStatus
= SHELL_INVALID_PARAMETER
;
854 // Analyze the statement
856 Status
= ProcessStatement(&CurrentValue
, CurrentParameter
, EndParameter
, PreviousEnding
, CaseInsensitive
, ForceString
);
857 if (EFI_ERROR(Status
)) {
858 // ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_STARTING), gShellLevel1HiiHandle, gEfiShellParametersProtocol->Argv[CurrentParameter]);
859 ShellStatus
= SHELL_INVALID_PARAMETER
;
862 // Optomize to get out of the loop early...
864 if ((Ending
== END_TAG_OR
&& CurrentValue
) || (Ending
== END_TAG_AND
&& !CurrentValue
)) {
865 Status
= PerformResultOperation(CurrentValue
);
866 if (EFI_ERROR(Status
)) {
867 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_AFTER_BAD
), gShellLevel1HiiHandle
, gEfiShellParametersProtocol
->Argv
[CurrentParameter
]);
868 ShellStatus
= SHELL_INVALID_PARAMETER
;
874 if (ShellStatus
== SHELL_SUCCESS
){
875 CurrentParameter
= EndParameter
;
877 // Skip over the or or and parameter.
879 if (Ending
== END_TAG_OR
|| Ending
== END_TAG_AND
) {
885 return (ShellStatus
);
889 Function for 'else' command.
891 @param[in] ImageHandle Handle to the Image (NULL if Internal).
892 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
896 ShellCommandRunElse (
897 IN EFI_HANDLE ImageHandle
,
898 IN EFI_SYSTEM_TABLE
*SystemTable
901 ASSERT_EFI_ERROR(CommandInit());
903 if (gEfiShellParametersProtocol
->Argc
> 1) {
904 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellLevel1HiiHandle
);
905 return (SHELL_INVALID_PARAMETER
);
908 if (!gEfiShellProtocol
->BatchIsActive()) {
909 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_NO_SCRIPT
), gShellLevel1HiiHandle
, L
"Else");
910 return (SHELL_UNSUPPORTED
);
914 if (!MoveToTag(GetPreviousNode
, L
"if", L
"endif", NULL
, ShellCommandGetCurrentScriptFile(), FALSE
, TRUE
, FALSE
)) {
915 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_NO_MATCHING
), gShellLevel1HiiHandle
, L
"If", L
"Else", ShellCommandGetCurrentScriptFile()->CurrentCommand
->Line
);
916 return (SHELL_DEVICE_ERROR
);
918 if (!MoveToTag(GetPreviousNode
, L
"if", L
"else", NULL
, ShellCommandGetCurrentScriptFile(), FALSE
, TRUE
, FALSE
)) {
919 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_NO_MATCHING
), gShellLevel1HiiHandle
, L
"If", L
"Else", ShellCommandGetCurrentScriptFile()->CurrentCommand
->Line
);
920 return (SHELL_DEVICE_ERROR
);
923 if (!MoveToTag(GetNextNode
, L
"endif", L
"if", NULL
, ShellCommandGetCurrentScriptFile(), FALSE
, FALSE
, FALSE
)) {
924 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_NO_MATCHING
), gShellLevel1HiiHandle
, L
"EndIf", "Else", ShellCommandGetCurrentScriptFile()->CurrentCommand
->Line
);
925 return (SHELL_DEVICE_ERROR
);
928 return (SHELL_SUCCESS
);
932 Function for 'endif' command.
934 @param[in] ImageHandle Handle to the Image (NULL if Internal).
935 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
939 ShellCommandRunEndIf (
940 IN EFI_HANDLE ImageHandle
,
941 IN EFI_SYSTEM_TABLE
*SystemTable
944 ASSERT_EFI_ERROR(CommandInit());
946 if (gEfiShellParametersProtocol
->Argc
> 1) {
947 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellLevel1HiiHandle
);
948 return (SHELL_INVALID_PARAMETER
);
951 if (!gEfiShellProtocol
->BatchIsActive()) {
952 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_NO_SCRIPT
), gShellLevel1HiiHandle
, L
"Endif");
953 return (SHELL_UNSUPPORTED
);
956 if (!MoveToTag(GetPreviousNode
, L
"if", L
"endif", NULL
, ShellCommandGetCurrentScriptFile(), FALSE
, TRUE
, FALSE
)) {
957 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_NO_MATCHING
), gShellLevel1HiiHandle
, L
"If", L
"EndIf", ShellCommandGetCurrentScriptFile()->CurrentCommand
->Line
);
958 return (SHELL_DEVICE_ERROR
);
961 return (SHELL_SUCCESS
);