2 Main file for If and else shell level 1 function.
4 (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "UefiShellLevel1CommandsLib.h"
17 #include <Library/PrintLib.h>
31 OperatorGreatorOrEqual
,
33 OperatorUnisgnedGreaterThan
,
34 OperatorUnsignedLessThan
,
35 OperatorUnsignedGreaterOrEqual
,
36 OperatorUnsignedLessOrEqual
,
41 Extract the next fragment, if there is one.
43 @param[in, out] Statement The current remaining statement.
44 @param[in] Fragment The current fragment.
45 @param[out] Match TRUE when there is another Fragment in Statement,
48 @retval EFI_SUCCESS The match operation is performed successfully.
49 @retval EFI_OUT_OF_RESOURCES Out of resources.
53 IN OUT CONST CHAR16
**Statement
,
54 IN CONST CHAR16
*Fragment
,
62 Tester
= StrnCatGrow(&Tester
, NULL
, *Statement
, StrLen(Fragment
));
64 return EFI_OUT_OF_RESOURCES
;
66 Tester
[StrLen(Fragment
)] = CHAR_NULL
;
67 if (gUnicodeCollation
->StriColl(
72 // increment the string pointer to the end of what we found and then chop off spaces...
74 *Statement
+=StrLen(Fragment
);
75 while (*Statement
[0] == L
' ') {
87 Determine if String represents a valid profile.
89 @param[in] String The pointer to the string to test.
91 @retval TRUE String is a valid profile.
92 @retval FALSE String is not a valid profile.
97 IN CONST CHAR16
*String
100 CONST CHAR16
*ProfilesString
;
101 CONST CHAR16
*TempLocation
;
103 ProfilesString
= ShellGetEnvironmentVariable(L
"profiles");
104 ASSERT(ProfilesString
!= NULL
);
105 TempLocation
= StrStr(ProfilesString
, String
);
106 if ((TempLocation
!= NULL
) && (*(TempLocation
-1) == L
';') && (*(TempLocation
+StrLen(String
)) == L
';')) {
113 Do a comparison between 2 things.
115 @param[in] Compare1 The first item to compare.
116 @param[in] Compare2 The second item to compare.
117 @param[in] BinOp The type of comparison to perform.
118 @param[in] CaseInsensitive TRUE to do non-case comparison, FALSE otherwise.
119 @param[in] ForceStringCompare TRUE to force string comparison, FALSE otherwise.
121 @return The result of the comparison.
126 IN CONST CHAR16
*Compare1
,
127 IN CONST CHAR16
*Compare2
,
128 IN CONST BIN_OPERATOR_TYPE BinOp
,
129 IN CONST BOOLEAN CaseInsensitive
,
130 IN CONST BOOLEAN ForceStringCompare
137 // "Compare1 BinOp Compare2"
140 case OperatorUnisgnedGreaterThan
:
141 case OperatorGreaterThan
:
142 if (ForceStringCompare
|| !ShellIsHexOrDecimalNumber(Compare1
, FALSE
, FALSE
) || !ShellIsHexOrDecimalNumber(Compare2
, FALSE
, FALSE
)) {
146 if ((CaseInsensitive
&& StringNoCaseCompare(&Compare1
, &Compare2
) > 0) || (StringCompare(&Compare1
, &Compare2
) > 0)) {
153 if (Compare1
[0] == L
'-') {
154 Cmp1
= 0 - (INTN
)ShellStrToUintn(Compare1
+1);
156 Cmp1
= (INTN
)ShellStrToUintn(Compare1
);
158 if (Compare2
[0] == L
'-') {
159 Cmp2
= 0 - (INTN
)ShellStrToUintn(Compare2
+1);
161 Cmp2
= (INTN
)ShellStrToUintn(Compare2
);
163 if (BinOp
== OperatorGreaterThan
) {
168 if ((UINTN
)Cmp1
> (UINTN
)Cmp2
) {
174 case OperatorUnsignedLessThan
:
175 case OperatorLessThan
:
176 if (ForceStringCompare
|| !ShellIsHexOrDecimalNumber(Compare1
, FALSE
, FALSE
) || !ShellIsHexOrDecimalNumber(Compare2
, FALSE
, FALSE
)) {
180 if ((CaseInsensitive
&& StringNoCaseCompare(&Compare1
, &Compare2
) < 0) || (StringCompare(&Compare1
, &Compare2
) < 0)) {
187 if (Compare1
[0] == L
'-') {
188 Cmp1
= 0 - (INTN
)ShellStrToUintn(Compare1
+1);
190 Cmp1
= (INTN
)ShellStrToUintn(Compare1
);
192 if (Compare2
[0] == L
'-') {
193 Cmp2
= 0 - (INTN
)ShellStrToUintn(Compare2
+1);
195 Cmp2
= (INTN
)ShellStrToUintn(Compare2
);
197 if (BinOp
== OperatorLessThan
) {
202 if ((UINTN
)Cmp1
< (UINTN
)Cmp2
) {
210 if (ForceStringCompare
|| !ShellIsHexOrDecimalNumber(Compare1
, FALSE
, FALSE
) || !ShellIsHexOrDecimalNumber(Compare2
, FALSE
, FALSE
)) {
214 if ((CaseInsensitive
&& StringNoCaseCompare(&Compare1
, &Compare2
) == 0) || (StringCompare(&Compare1
, &Compare2
) == 0)) {
221 if (Compare1
[0] == L
'-') {
222 Cmp1
= 0 - (INTN
)ShellStrToUintn(Compare1
+1);
224 Cmp1
= (INTN
)ShellStrToUintn(Compare1
);
226 if (Compare2
[0] == L
'-') {
227 Cmp2
= 0 - (INTN
)ShellStrToUintn(Compare2
+1);
229 Cmp2
= (INTN
)ShellStrToUintn(Compare2
);
236 case OperatorNotEqual
:
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
)ShellStrToUintn(Compare1
+1);
251 Cmp1
= (INTN
)ShellStrToUintn(Compare1
);
253 if (Compare2
[0] == L
'-') {
254 Cmp2
= 0 - (INTN
)ShellStrToUintn(Compare2
+1);
256 Cmp2
= (INTN
)ShellStrToUintn(Compare2
);
263 case OperatorUnsignedGreaterOrEqual
:
264 case OperatorGreatorOrEqual
:
265 if (ForceStringCompare
|| !ShellIsHexOrDecimalNumber(Compare1
, FALSE
, FALSE
) || !ShellIsHexOrDecimalNumber(Compare2
, FALSE
, FALSE
)) {
269 if ((CaseInsensitive
&& StringNoCaseCompare(&Compare1
, &Compare2
) >= 0) || (StringCompare(&Compare1
, &Compare2
) >= 0)) {
276 if (Compare1
[0] == L
'-') {
277 Cmp1
= 0 - (INTN
)ShellStrToUintn(Compare1
+1);
279 Cmp1
= (INTN
)ShellStrToUintn(Compare1
);
281 if (Compare2
[0] == L
'-') {
282 Cmp2
= 0 - (INTN
)ShellStrToUintn(Compare2
+1);
284 Cmp2
= (INTN
)ShellStrToUintn(Compare2
);
286 if (BinOp
== OperatorGreatorOrEqual
) {
291 if ((UINTN
)Cmp1
>= (UINTN
)Cmp2
) {
297 case OperatorLessOrEqual
:
298 case OperatorUnsignedLessOrEqual
:
299 if (ForceStringCompare
|| !ShellIsHexOrDecimalNumber(Compare1
, FALSE
, FALSE
) || !ShellIsHexOrDecimalNumber(Compare2
, FALSE
, FALSE
)) {
303 if ((CaseInsensitive
&& StringNoCaseCompare(&Compare1
, &Compare2
) <= 0) || (StringCompare(&Compare1
, &Compare2
) <= 0)) {
310 if (Compare1
[0] == L
'-') {
311 Cmp1
= 0 - (INTN
)ShellStrToUintn(Compare1
+1);
313 Cmp1
= (INTN
)ShellStrToUintn(Compare1
);
315 if (Compare2
[0] == L
'-') {
316 Cmp2
= 0 - (INTN
)ShellStrToUintn(Compare2
+1);
318 Cmp2
= (INTN
)ShellStrToUintn(Compare2
);
320 if (BinOp
== OperatorLessOrEqual
) {
325 if ((UINTN
)Cmp1
<= (UINTN
)Cmp2
) {
338 Process an if statement and determine if its is valid or not.
340 @param[in, out] PassingState Opon entry, the current state. Upon exit,
342 @param[in] StartParameterNumber The number of the first parameter of
344 @param[in] EndParameterNumber The number of the final parameter of
346 @param[in] OperatorToUse The type of termination operator.
347 @param[in] CaseInsensitive TRUE for case insensitive, FALSE otherwise.
348 @param[in] ForceStringCompare TRUE for all string based, FALSE otherwise.
350 @retval EFI_INVALID_PARAMETER A parameter was invalid.
351 @retval EFI_SUCCESS The operation was successful.
356 IN OUT BOOLEAN
*PassingState
,
357 IN UINTN StartParameterNumber
,
358 IN UINTN EndParameterNumber
,
359 IN CONST END_TAG_TYPE OperatorToUse
,
360 IN CONST BOOLEAN CaseInsensitive
,
361 IN CONST BOOLEAN ForceStringCompare
365 BOOLEAN OperationResult
;
367 CHAR16
*StatementWalker
;
368 BIN_OPERATOR_TYPE BinOp
;
371 CHAR16 HexString
[20];
375 ASSERT((END_TAG_TYPE
)OperatorToUse
!= EndTagThen
);
377 Status
= EFI_SUCCESS
;
379 OperationResult
= FALSE
;
381 StatementWalker
= gEfiShellParametersProtocol
->Argv
[StartParameterNumber
];
382 if (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"not", &Match
)) && Match
) {
384 StatementWalker
= gEfiShellParametersProtocol
->Argv
[++StartParameterNumber
];
390 // now check for 'boolfunc' operators
392 if (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"isint", &Match
)) && Match
) {
393 if (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(", &Match
)) && Match
394 && StatementWalker
[StrLen(StatementWalker
)-1] == L
')') {
395 StatementWalker
[StrLen(StatementWalker
)-1] = CHAR_NULL
;
396 OperationResult
= ShellIsHexOrDecimalNumber(StatementWalker
, FALSE
, FALSE
);
398 Status
= EFI_INVALID_PARAMETER
;
399 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"isint");
401 } else if ((!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"exists", &Match
)) && Match
) ||
402 (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"exist", &Match
)) && Match
)) {
403 if (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(", &Match
)) && Match
&&
404 StatementWalker
[StrLen(StatementWalker
)-1] == L
')') {
405 StatementWalker
[StrLen(StatementWalker
)-1] = CHAR_NULL
;
407 // is what remains a file in CWD???
409 OperationResult
= (BOOLEAN
)(ShellFileExists(StatementWalker
)==EFI_SUCCESS
);
410 } else if (StatementWalker
[0] == CHAR_NULL
&& StartParameterNumber
+1 == EndParameterNumber
) {
411 OperationResult
= (BOOLEAN
)(ShellFileExists(gEfiShellParametersProtocol
->Argv
[++StartParameterNumber
])==EFI_SUCCESS
);
413 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"exist(s)");
414 Status
= EFI_INVALID_PARAMETER
;
416 } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"available", &Match
)) && Match
) {
417 if (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(", &Match
)) && Match
&&
418 StatementWalker
[StrLen(StatementWalker
)-1] == L
')') {
419 StatementWalker
[StrLen(StatementWalker
)-1] = CHAR_NULL
;
421 // is what remains a file in the CWD or path???
423 OperationResult
= (BOOLEAN
)(ShellIsFileInPath(StatementWalker
)==EFI_SUCCESS
);
425 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"available");
426 Status
= EFI_INVALID_PARAMETER
;
428 } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"profile", &Match
)) && Match
) {
429 if (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(", &Match
)) && Match
&&
430 StatementWalker
[StrLen(StatementWalker
)-1] == L
')') {
434 StatementWalker
[StrLen(StatementWalker
)-1] = CHAR_NULL
;
435 OperationResult
= IsValidProfile(StatementWalker
);
437 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"profile");
438 Status
= EFI_INVALID_PARAMETER
;
440 } else if (StartParameterNumber
+1 >= EndParameterNumber
) {
441 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, gEfiShellParametersProtocol
->Argv
[StartParameterNumber
]);
442 Status
= EFI_INVALID_PARAMETER
;
445 // must be 'item binop item' style
452 // get the first item
454 StatementWalker
= gEfiShellParametersProtocol
->Argv
[StartParameterNumber
];
455 if (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"efierror", &Match
)) && Match
) {
456 TempSpot
= StrStr(StatementWalker
, L
")");
457 if (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(", &Match
)) && Match
&& TempSpot
!= NULL
) {
458 *TempSpot
= CHAR_NULL
;
459 if (ShellIsHexOrDecimalNumber(StatementWalker
, FALSE
, FALSE
)) {
460 UnicodeSPrint(HexString
, sizeof(HexString
), L
"0x%x", ShellStrToUintn(StatementWalker
)|MAX_BIT
);
461 ASSERT(Compare1
== NULL
);
462 Compare1
= StrnCatGrow(&Compare1
, NULL
, HexString
, 0);
463 StatementWalker
+= StrLen(StatementWalker
) + 1;
465 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"efierror");
466 Status
= EFI_INVALID_PARAMETER
;
469 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"efierror");
470 Status
= EFI_INVALID_PARAMETER
;
472 } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"pierror", &Match
)) && Match
) {
473 TempSpot
= StrStr(StatementWalker
, L
")");
474 if (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(", &Match
)) && Match
&& TempSpot
!= NULL
) {
475 *TempSpot
= CHAR_NULL
;
476 if (ShellIsHexOrDecimalNumber(StatementWalker
, FALSE
, FALSE
)) {
477 UnicodeSPrint(HexString
, sizeof(HexString
), L
"0x%x", ShellStrToUintn(StatementWalker
)|MAX_BIT
|(MAX_BIT
>>2));
478 ASSERT(Compare1
== NULL
);
479 Compare1
= StrnCatGrow(&Compare1
, NULL
, HexString
, 0);
480 StatementWalker
+= StrLen(StatementWalker
) + 1;
482 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"pierror");
483 Status
= EFI_INVALID_PARAMETER
;
486 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"pierror");
487 Status
= EFI_INVALID_PARAMETER
;
489 } else if (!EFI_ERROR (IsNextFragment ((CONST CHAR16
**)(&StatementWalker
), L
"oemerror", &Match
)) && Match
) {
490 TempSpot
= StrStr(StatementWalker
, L
")");
491 if (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(", &Match
)) && Match
&& TempSpot
!= NULL
) {
492 TempSpot
= CHAR_NULL
;
493 if (ShellIsHexOrDecimalNumber(StatementWalker
, FALSE
, FALSE
)) {
494 UnicodeSPrint(HexString
, sizeof(HexString
), L
"0x%x", ShellStrToUintn(StatementWalker
)|MAX_BIT
|(MAX_BIT
>>1));
495 ASSERT(Compare1
== NULL
);
496 Compare1
= StrnCatGrow(&Compare1
, NULL
, HexString
, 0);
497 StatementWalker
+= StrLen(StatementWalker
) + 1;
499 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"oemerror");
500 Status
= EFI_INVALID_PARAMETER
;
503 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"oemerror");
504 Status
= EFI_INVALID_PARAMETER
;
507 ASSERT(Compare1
== NULL
);
508 if (EndParameterNumber
- StartParameterNumber
> 2) {
509 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_STARTING
), gShellLevel1HiiHandle
, gEfiShellParametersProtocol
->Argv
[StartParameterNumber
+2]);
510 Status
= EFI_INVALID_PARAMETER
;
513 // must be a raw string
515 Compare1
= StrnCatGrow(&Compare1
, NULL
, StatementWalker
, 0);
522 ASSERT(StartParameterNumber
+1<EndParameterNumber
);
523 StatementWalker
= gEfiShellParametersProtocol
->Argv
[StartParameterNumber
+1];
524 if (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"gt", &Match
)) && Match
) {
525 BinOp
= OperatorGreaterThan
;
526 } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"lt", &Match
)) && Match
) {
527 BinOp
= OperatorLessThan
;
528 } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"eq", &Match
)) && Match
) {
529 BinOp
= OperatorEqual
;
530 } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"ne", &Match
)) && Match
) {
531 BinOp
= OperatorNotEqual
;
532 } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"ge", &Match
)) && Match
) {
533 BinOp
= OperatorGreatorOrEqual
;
534 } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"le", &Match
)) && Match
) {
535 BinOp
= OperatorLessOrEqual
;
536 } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"==", &Match
)) && Match
) {
537 BinOp
= OperatorEqual
;
538 } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"ugt", &Match
)) && Match
) {
539 BinOp
= OperatorUnisgnedGreaterThan
;
540 } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"ult", &Match
)) && Match
) {
541 BinOp
= OperatorUnsignedLessThan
;
542 } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"uge", &Match
)) && Match
) {
543 BinOp
= OperatorUnsignedGreaterOrEqual
;
544 } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"ule", &Match
)) && Match
) {
545 BinOp
= OperatorUnsignedLessOrEqual
;
547 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_INVALID_BINOP
), gShellLevel1HiiHandle
, StatementWalker
);
548 Status
= EFI_INVALID_PARAMETER
;
552 // get the second item
554 ASSERT(StartParameterNumber
+2<=EndParameterNumber
);
555 StatementWalker
= gEfiShellParametersProtocol
->Argv
[StartParameterNumber
+2];
556 if (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"efierror", &Match
)) && Match
) {
557 TempSpot
= StrStr(StatementWalker
, L
")");
558 if (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(", &Match
)) && Match
&& TempSpot
!= NULL
) {
559 TempSpot
= CHAR_NULL
;
560 if (ShellIsHexOrDecimalNumber(StatementWalker
, FALSE
, FALSE
)) {
561 UnicodeSPrint(HexString
, sizeof(HexString
), L
"0x%x", ShellStrToUintn(StatementWalker
)|MAX_BIT
);
562 ASSERT(Compare2
== NULL
);
563 Compare2
= StrnCatGrow(&Compare2
, NULL
, HexString
, 0);
564 StatementWalker
+= StrLen(StatementWalker
) + 1;
566 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"efierror");
567 Status
= EFI_INVALID_PARAMETER
;
570 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"efierror");
571 Status
= EFI_INVALID_PARAMETER
;
574 // can this be collapsed into the above?
576 } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"pierror", &Match
)) && Match
) {
577 TempSpot
= StrStr(StatementWalker
, L
")");
578 if (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(", &Match
)) && Match
&& TempSpot
!= NULL
) {
579 TempSpot
= CHAR_NULL
;
580 if (ShellIsHexOrDecimalNumber(StatementWalker
, FALSE
, FALSE
)) {
581 UnicodeSPrint(HexString
, sizeof(HexString
), L
"0x%x", ShellStrToUintn(StatementWalker
)|MAX_BIT
|(MAX_BIT
>>2));
582 ASSERT(Compare2
== NULL
);
583 Compare2
= StrnCatGrow(&Compare2
, NULL
, HexString
, 0);
584 StatementWalker
+= StrLen(StatementWalker
) + 1;
586 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"pierror");
587 Status
= EFI_INVALID_PARAMETER
;
590 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"pierror");
591 Status
= EFI_INVALID_PARAMETER
;
593 } else if (!EFI_ERROR (IsNextFragment ((CONST CHAR16
**)(&StatementWalker
), L
"oemerror", &Match
)) && Match
) {
594 TempSpot
= StrStr(StatementWalker
, L
")");
595 if (!EFI_ERROR (IsNextFragment((CONST CHAR16
**)(&StatementWalker
), L
"(", &Match
)) && Match
&& TempSpot
!= NULL
) {
596 TempSpot
= CHAR_NULL
;
597 if (ShellIsHexOrDecimalNumber(StatementWalker
, FALSE
, FALSE
)) {
598 UnicodeSPrint(HexString
, sizeof(HexString
), L
"0x%x", ShellStrToUintn(StatementWalker
)|MAX_BIT
|(MAX_BIT
>>1));
599 ASSERT(Compare2
== NULL
);
600 Compare2
= StrnCatGrow(&Compare2
, NULL
, HexString
, 0);
601 StatementWalker
+= StrLen(StatementWalker
) + 1;
603 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"oemerror");
604 Status
= EFI_INVALID_PARAMETER
;
607 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_IN
), gShellLevel1HiiHandle
, L
"oemerror");
608 Status
= EFI_INVALID_PARAMETER
;
612 // must be a raw string
614 ASSERT(Compare2
== NULL
);
615 Compare2
= StrnCatGrow(&Compare2
, NULL
, StatementWalker
, 0);
618 if (Compare1
!= NULL
&& Compare2
!= NULL
&& BinOp
!= OperatorMax
) {
619 OperationResult
= TestOperation(Compare1
, Compare2
, BinOp
, CaseInsensitive
, ForceStringCompare
);
622 SHELL_FREE_NON_NULL(Compare1
);
623 SHELL_FREE_NON_NULL(Compare2
);
627 // done processing do result...
630 if (!EFI_ERROR(Status
)) {
632 OperationResult
= (BOOLEAN
)(!OperationResult
);
634 switch(OperatorToUse
) {
636 *PassingState
= (BOOLEAN
)(*PassingState
|| OperationResult
);
639 *PassingState
= (BOOLEAN
)(*PassingState
&& OperationResult
);
642 *PassingState
= (BOOLEAN
)(OperationResult
);
652 Break up the next part of the if statement (until the next 'and', 'or', or 'then').
654 @param[in] ParameterNumber The current parameter number.
655 @param[out] EndParameter Upon successful return, will point to the
656 parameter to start the next iteration with.
657 @param[out] EndTag Upon successful return, will point to the
658 type that was found at the end of this statement.
660 @retval TRUE A valid statement was found.
661 @retval FALSE A valid statement was not found.
666 IN UINTN ParameterNumber
,
667 OUT UINTN
*EndParameter
,
668 OUT END_TAG_TYPE
*EndTag
674 ; ParameterNumber
< gEfiShellParametersProtocol
->Argc
677 if (gUnicodeCollation
->StriColl(
679 gEfiShellParametersProtocol
->Argv
[ParameterNumber
],
681 *EndParameter
= ParameterNumber
- 1;
684 } else if (gUnicodeCollation
->StriColl(
686 gEfiShellParametersProtocol
->Argv
[ParameterNumber
],
688 *EndParameter
= ParameterNumber
- 1;
691 } else if (gUnicodeCollation
->StriColl(
693 gEfiShellParametersProtocol
->Argv
[ParameterNumber
],
695 *EndParameter
= ParameterNumber
- 1;
696 *EndTag
= EndTagThen
;
700 if (*EndTag
== EndTagMax
) {
707 Move the script file pointer to a different place in the script file.
708 This one is special since it handles the if/else/endif syntax.
710 @param[in] ScriptFile The script file from GetCurrnetScriptFile().
712 @retval TRUE The move target was found and the move was successful.
713 @retval FALSE Something went wrong.
718 IN SCRIPT_FILE
*ScriptFile
721 SCRIPT_COMMAND_LIST
*CommandNode
;
725 CHAR16
*CommandWalker
;
726 CHAR16
*TempLocation
;
731 if (ScriptFile
== NULL
) {
735 for (CommandNode
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&ScriptFile
->CommandList
, &ScriptFile
->CurrentCommand
->Link
), Found
= FALSE
736 ; !IsNull(&ScriptFile
->CommandList
, &CommandNode
->Link
) && !Found
737 ; CommandNode
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&ScriptFile
->CommandList
, &CommandNode
->Link
)
741 // get just the first part of the command line...
744 CommandName
= StrnCatGrow(&CommandName
, NULL
, CommandNode
->Cl
, 0);
745 if (CommandName
== NULL
) {
748 CommandWalker
= CommandName
;
751 // Skip leading spaces and tabs.
753 while ((CommandWalker
[0] == L
' ') || (CommandWalker
[0] == L
'\t')) {
756 TempLocation
= StrStr(CommandWalker
, L
" ");
758 if (TempLocation
!= NULL
) {
759 *TempLocation
= CHAR_NULL
;
763 // did we find a nested item ?
765 if (gUnicodeCollation
->StriColl(
767 (CHAR16
*)CommandWalker
,
770 } else if (TargetCount
== 1 && gUnicodeCollation
->StriColl(
772 (CHAR16
*)CommandWalker
,
773 (CHAR16
*)L
"else") == 0) {
775 // else can only decrement the last part... not an nested if
776 // hence the TargetCount compare added
779 } else if (gUnicodeCollation
->StriColl(
781 (CHAR16
*)CommandWalker
,
782 (CHAR16
*)L
"endif") == 0) {
785 if (TargetCount
== 0) {
786 ScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&ScriptFile
->CommandList
, &CommandNode
->Link
);
791 // Free the memory for this loop...
793 SHELL_FREE_NON_NULL(CommandName
);
799 Deal with the result of the if operation.
801 @param[in] Result The result of the if.
803 @retval EFI_SUCCESS The operation was successful.
804 @retval EFI_NOT_FOUND The ending tag could not be found.
808 PerformResultOperation (
809 IN CONST BOOLEAN Result
812 if (Result
|| MoveToTagSpecial(ShellCommandGetCurrentScriptFile())) {
813 return (EFI_SUCCESS
);
815 return (EFI_NOT_FOUND
);
819 Function for 'if' command.
821 @param[in] ImageHandle Handle to the Image (NULL if Internal).
822 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
827 IN EFI_HANDLE ImageHandle
,
828 IN EFI_SYSTEM_TABLE
*SystemTable
832 SHELL_STATUS ShellStatus
;
833 BOOLEAN CaseInsensitive
;
835 UINTN CurrentParameter
;
837 BOOLEAN CurrentValue
;
839 END_TAG_TYPE PreviousEnding
;
840 SCRIPT_FILE
*CurrentScriptFile
;
842 Status
= CommandInit();
843 ASSERT_EFI_ERROR(Status
);
845 if (!gEfiShellProtocol
->BatchIsActive()) {
846 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_NO_SCRIPT
), gShellLevel1HiiHandle
, L
"if");
847 return (SHELL_UNSUPPORTED
);
850 if (gEfiShellParametersProtocol
->Argc
< 3) {
851 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellLevel1HiiHandle
, L
"if");
852 return (SHELL_INVALID_PARAMETER
);
856 // Make sure that an End exists.
858 CurrentScriptFile
= ShellCommandGetCurrentScriptFile();
859 if (!MoveToTag(GetNextNode
, L
"endif", L
"if", NULL
, CurrentScriptFile
, TRUE
, TRUE
, FALSE
)) {
864 STRING_TOKEN (STR_SYNTAX_NO_MATCHING
),
865 gShellLevel1HiiHandle
,
868 CurrentScriptFile
!=NULL
869 && CurrentScriptFile
->CurrentCommand
!=NULL
870 ? CurrentScriptFile
->CurrentCommand
->Line
:0);
871 return (SHELL_DEVICE_ERROR
);
875 // initialize the shell lib (we must be in non-auto-init...)
877 Status
= ShellInitialize();
878 ASSERT_EFI_ERROR(Status
);
880 CurrentParameter
= 1;
883 if (gUnicodeCollation
->StriColl(
885 gEfiShellParametersProtocol
->Argv
[1],
887 gUnicodeCollation
->StriColl(
889 gEfiShellParametersProtocol
->Argv
[2],
891 (gEfiShellParametersProtocol
->Argc
> 3 && gUnicodeCollation
->StriColl(
893 gEfiShellParametersProtocol
->Argv
[3],
895 CaseInsensitive
= TRUE
;
898 CaseInsensitive
= FALSE
;
900 if (gUnicodeCollation
->StriColl(
902 gEfiShellParametersProtocol
->Argv
[1],
904 gUnicodeCollation
->StriColl(
906 gEfiShellParametersProtocol
->Argv
[2],
908 (gEfiShellParametersProtocol
->Argc
> 3 && gUnicodeCollation
->StriColl(
910 gEfiShellParametersProtocol
->Argv
[3],
918 for ( ShellStatus
= SHELL_SUCCESS
, CurrentValue
= FALSE
, Ending
= EndTagMax
919 ; CurrentParameter
< gEfiShellParametersProtocol
->Argc
&& ShellStatus
== SHELL_SUCCESS
920 ; CurrentParameter
++) {
921 if (gUnicodeCollation
->StriColl(
923 gEfiShellParametersProtocol
->Argv
[CurrentParameter
],
926 // we are at the then
928 if (CurrentParameter
+1 != gEfiShellParametersProtocol
->Argc
) {
929 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_TEXT_AFTER_THEN
), gShellLevel1HiiHandle
, L
"if");
930 ShellStatus
= SHELL_INVALID_PARAMETER
;
932 Status
= PerformResultOperation(CurrentValue
);
933 if (EFI_ERROR(Status
)) {
934 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_AFTER_BAD
), gShellLevel1HiiHandle
, L
"if", gEfiShellParametersProtocol
->Argv
[CurrentParameter
]);
935 ShellStatus
= SHELL_INVALID_PARAMETER
;
939 PreviousEnding
= Ending
;
941 // build up the next statement for analysis
943 if (!BuildNextStatement(CurrentParameter
, &EndParameter
, &Ending
)) {
944 CurrentScriptFile
= ShellCommandGetCurrentScriptFile();
949 STRING_TOKEN (STR_SYNTAX_NO_MATCHING
),
950 gShellLevel1HiiHandle
,
953 CurrentScriptFile
!=NULL
954 && CurrentScriptFile
->CurrentCommand
!=NULL
955 ? CurrentScriptFile
->CurrentCommand
->Line
:0);
956 ShellStatus
= SHELL_INVALID_PARAMETER
;
959 // Analyze the statement
961 Status
= ProcessStatement(&CurrentValue
, CurrentParameter
, EndParameter
, PreviousEnding
, CaseInsensitive
, ForceString
);
962 if (EFI_ERROR(Status
)) {
963 // ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_STARTING), gShellLevel1HiiHandle, gEfiShellParametersProtocol->Argv[CurrentParameter]);
964 ShellStatus
= SHELL_INVALID_PARAMETER
;
967 // Optomize to get out of the loop early...
969 if ((Ending
== EndTagOr
&& CurrentValue
) || (Ending
== EndTagAnd
&& !CurrentValue
)) {
970 Status
= PerformResultOperation(CurrentValue
);
971 if (EFI_ERROR(Status
)) {
972 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SYNTAX_AFTER_BAD
), gShellLevel1HiiHandle
, L
"if", gEfiShellParametersProtocol
->Argv
[CurrentParameter
]);
973 ShellStatus
= SHELL_INVALID_PARAMETER
;
979 if (ShellStatus
== SHELL_SUCCESS
){
980 CurrentParameter
= EndParameter
;
982 // Skip over the or or and parameter.
984 if (Ending
== EndTagOr
|| Ending
== EndTagAnd
) {
990 return (ShellStatus
);
994 Function for 'else' command.
996 @param[in] ImageHandle Handle to the Image (NULL if Internal).
997 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
1001 ShellCommandRunElse (
1002 IN EFI_HANDLE ImageHandle
,
1003 IN EFI_SYSTEM_TABLE
*SystemTable
1007 SCRIPT_FILE
*CurrentScriptFile
;
1009 Status
= CommandInit ();
1010 ASSERT_EFI_ERROR (Status
);
1012 if (gEfiShellParametersProtocol
->Argc
> 1) {
1013 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellLevel1HiiHandle
, L
"if");
1014 return (SHELL_INVALID_PARAMETER
);
1017 if (!gEfiShellProtocol
->BatchIsActive()) {
1018 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_NO_SCRIPT
), gShellLevel1HiiHandle
, L
"Else");
1019 return (SHELL_UNSUPPORTED
);
1022 CurrentScriptFile
= ShellCommandGetCurrentScriptFile();
1024 if (!MoveToTag(GetPreviousNode
, L
"if", L
"endif", NULL
, CurrentScriptFile
, FALSE
, TRUE
, FALSE
)) {
1029 STRING_TOKEN (STR_SYNTAX_NO_MATCHING
),
1030 gShellLevel1HiiHandle
,
1033 CurrentScriptFile
!=NULL
1034 && CurrentScriptFile
->CurrentCommand
!=NULL
1035 ? CurrentScriptFile
->CurrentCommand
->Line
:0);
1036 return (SHELL_DEVICE_ERROR
);
1038 if (!MoveToTag(GetPreviousNode
, L
"if", L
"else", NULL
, CurrentScriptFile
, FALSE
, TRUE
, FALSE
)) {
1043 STRING_TOKEN (STR_SYNTAX_NO_MATCHING
),
1044 gShellLevel1HiiHandle
,
1047 CurrentScriptFile
!=NULL
1048 && CurrentScriptFile
->CurrentCommand
!=NULL
1049 ? CurrentScriptFile
->CurrentCommand
->Line
:0);
1050 return (SHELL_DEVICE_ERROR
);
1053 if (!MoveToTag(GetNextNode
, L
"endif", L
"if", NULL
, CurrentScriptFile
, FALSE
, FALSE
, FALSE
)) {
1058 STRING_TOKEN (STR_SYNTAX_NO_MATCHING
),
1059 gShellLevel1HiiHandle
,
1062 CurrentScriptFile
!=NULL
1063 && CurrentScriptFile
->CurrentCommand
!=NULL
1064 ? CurrentScriptFile
->CurrentCommand
->Line
:0);
1065 return (SHELL_DEVICE_ERROR
);
1068 return (SHELL_SUCCESS
);
1072 Function for 'endif' command.
1074 @param[in] ImageHandle Handle to the Image (NULL if Internal).
1075 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
1079 ShellCommandRunEndIf (
1080 IN EFI_HANDLE ImageHandle
,
1081 IN EFI_SYSTEM_TABLE
*SystemTable
1085 SCRIPT_FILE
*CurrentScriptFile
;
1087 Status
= CommandInit ();
1088 ASSERT_EFI_ERROR (Status
);
1090 if (gEfiShellParametersProtocol
->Argc
> 1) {
1091 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellLevel1HiiHandle
, L
"if");
1092 return (SHELL_INVALID_PARAMETER
);
1095 if (!gEfiShellProtocol
->BatchIsActive()) {
1096 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_NO_SCRIPT
), gShellLevel1HiiHandle
, L
"Endif");
1097 return (SHELL_UNSUPPORTED
);
1100 CurrentScriptFile
= ShellCommandGetCurrentScriptFile();
1101 if (!MoveToTag(GetPreviousNode
, L
"if", L
"endif", NULL
, CurrentScriptFile
, FALSE
, TRUE
, FALSE
)) {
1106 STRING_TOKEN (STR_SYNTAX_NO_MATCHING
),
1107 gShellLevel1HiiHandle
,
1110 CurrentScriptFile
!=NULL
1111 && CurrentScriptFile
->CurrentCommand
!=NULL
1112 ? CurrentScriptFile
->CurrentCommand
->Line
:0);
1113 return (SHELL_DEVICE_ERROR
);
1116 return (SHELL_SUCCESS
);