Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
5.0 KiB

  1. /*++
  2. Copyright (c) 1999 Intel Corporation
  3. Module Name:
  4. if.c
  5. Abstract:
  6. Internal Shell cmd "if" & "endif"
  7. Revision History
  8. --*/
  9. #include "shelle.h"
  10. /*
  11. * Internal prototypes
  12. */
  13. EFI_STATUS
  14. CheckIfFileExists(
  15. IN CHAR16 *FileName,
  16. OUT BOOLEAN *FileExists
  17. );
  18. /*///////////////////////////////////////////////////////////////////////
  19. Function Name:
  20. SEnvCmdIf
  21. Description:
  22. Builtin shell command "if" for conditional execution in script files.
  23. */
  24. EFI_STATUS
  25. SEnvCmdIf (
  26. IN EFI_HANDLE ImageHandle,
  27. IN EFI_SYSTEM_TABLE *SystemTable
  28. )
  29. {
  30. CHAR16 **Argv;
  31. UINTN Argc = 0;
  32. UINTN Index = 0;
  33. UINTN NNots = 0;
  34. EFI_STATUS Status = EFI_SUCCESS;
  35. CHAR16 *FileName = NULL;
  36. BOOLEAN FileExists = FALSE;
  37. CHAR16 *String1 = NULL;
  38. CHAR16 *String2 = NULL;
  39. InitializeShellApplication (ImageHandle, SystemTable);
  40. Argv = SI->Argv;
  41. Argc = SI->Argc;
  42. if ( !SEnvBatchIsActive() ) {
  43. Print( L"Error: IF command only supported in script files\n" );
  44. Status = EFI_UNSUPPORTED;
  45. goto Done;
  46. }
  47. /*
  48. * Two forms of the if command:
  49. * if [not] exist file then
  50. * if [not] string1 == string2
  51. *
  52. * First, parse it
  53. */
  54. if ( Argc < 4 ) {
  55. Status = EFI_INVALID_PARAMETER;
  56. goto Done;
  57. }
  58. if ( StriCmp( Argv[1], L"not" ) == 0 ) {
  59. NNots = 1;
  60. } else {
  61. NNots = 0;
  62. }
  63. if ( StriCmp( Argv[NNots+1], L"exist" ) == 0 ) {
  64. /*
  65. * first form of the command, test for file existence
  66. */
  67. if ( (Argc != NNots + 4) || (StriCmp( Argv[NNots+3], L"then" ) != 0) ) {
  68. Status = EFI_INVALID_PARAMETER;
  69. goto Done;
  70. }
  71. FileName = Argv[NNots+2];
  72. /*
  73. * Test for existence
  74. */
  75. Status = CheckIfFileExists( FileName, &FileExists );
  76. if ( EFI_ERROR( Status ) ) {
  77. goto Done;
  78. }
  79. SEnvBatchSetCondition( (BOOLEAN) ((NNots == 0 && FileExists) ||
  80. (NNots == 1 && !FileExists)) );
  81. } else {
  82. /*
  83. * second form of the command, compare two strings
  84. */
  85. if ( (Argc != NNots + 5) || (StriCmp( Argv[NNots+2], L"==" ) != 0)
  86. || (StriCmp( Argv[NNots+4], L"then" ) != 0) ) {
  87. Status = EFI_INVALID_PARAMETER;
  88. goto Done;
  89. }
  90. String1 = Argv[NNots+1];
  91. String2 = Argv[NNots+3];
  92. SEnvBatchSetCondition(
  93. (BOOLEAN)((NNots == 0 && StriCmp( String1, String2 ) == 0) ||
  94. (NNots == 1 && StriCmp( String1, String2 ) != 0)) );
  95. }
  96. Done:
  97. return Status;
  98. }
  99. /*///////////////////////////////////////////////////////////////////////
  100. Function Name:
  101. CheckIfFileExists
  102. Description:
  103. Check file parameter to see if file exists. Wildcards are supported,
  104. but if the argument expands to more than one file name an invalid
  105. parameter error is returned and "not found" is assumed.
  106. */
  107. EFI_STATUS
  108. CheckIfFileExists(
  109. IN CHAR16 *FileName,
  110. OUT BOOLEAN *FileExists
  111. )
  112. {
  113. LIST_ENTRY FileList;
  114. LIST_ENTRY *Link;
  115. SHELL_FILE_ARG *Arg;
  116. EFI_STATUS Status = EFI_SUCCESS;
  117. UINTN NFiles = 0;
  118. *FileExists = FALSE;
  119. InitializeListHead (&FileList);
  120. /*
  121. * Attempt to open the file, expanding any wildcards.
  122. */
  123. Status = ShellFileMetaArg( FileName, &FileList);
  124. if ( EFI_ERROR( Status ) ) {
  125. if ( Status == EFI_NOT_FOUND ) {
  126. Status = EFI_SUCCESS;
  127. goto Done;
  128. }
  129. }
  130. /*
  131. * Make sure there is one and only one valid file in the file list
  132. */
  133. NFiles = 0;
  134. for (Link=FileList.Flink; Link!=&FileList; Link=Link->Flink) {
  135. Arg = CR(Link, SHELL_FILE_ARG, Link, SHELL_FILE_ARG_SIGNATURE);
  136. if ( Arg->Handle ) {
  137. /*
  138. * Non-NULL handle means file was there and open-able
  139. */
  140. NFiles += 1;
  141. }
  142. }
  143. if ( NFiles > 0 ) {
  144. /*
  145. * Found one or more files, so set the flag
  146. */
  147. *FileExists = TRUE;
  148. }
  149. Done:
  150. ShellFreeFileList (&FileList);
  151. return Status;
  152. }
  153. /*///////////////////////////////////////////////////////////////////////
  154. Function Name:
  155. SEnvCmdEndif
  156. Description:
  157. Builtin shell command "endif".
  158. */
  159. EFI_STATUS
  160. SEnvCmdEndif (
  161. IN EFI_HANDLE ImageHandle,
  162. IN EFI_SYSTEM_TABLE *SystemTable
  163. )
  164. {
  165. InitializeShellApplication (ImageHandle, SystemTable);
  166. /*
  167. * Just reset the condition flag to resume normal execution.
  168. */
  169. SEnvBatchSetCondition( TRUE );
  170. return EFI_SUCCESS;
  171. }