]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellLib/UefiShellLib.c
updating comments mostly. also added some new lib functions.
[mirror_edk2.git] / ShellPkg / Library / UefiShellLib / UefiShellLib.c
index fb763bde6162bc81f0e9514d4ca76912513d5b19..e1aaa9db80c818b6ff4b10fb493b2ce91a4d9aae 100644 (file)
@@ -1,37 +1,17 @@
 /** @file\r
   Provides interface to shell functionality for shell commands and applications.\r
 \r
-Copyright (c) 2006 - 2009, Intel Corporation<BR>\r
-All rights reserved. This program and the accompanying materials\r
-are licensed and made available under the terms and conditions of the BSD License\r
-which accompanies this distribution.  The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
+  Copyright (c) 2006 - 2010, Intel Corporation<BR>\r
+  All rights reserved. This program and the accompanying materials\r
+  are licensed and made available under the terms and conditions of the BSD License\r
+  which accompanies this distribution.  The full text of the license may be found at\r
+  http://opensource.org/licenses/bsd-license.php\r
 \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
 **/\r
 \r
-#include <Uefi.h>\r
-#include <Library/ShellLib.h>\r
-#include <Library/UefiBootServicesTableLib.h>\r
-#include <Library/BaseLib.h>\r
-#include <Library/BaseMemoryLib.h>\r
-#include <Library/DebugLib.h>\r
-#include <Library/MemoryAllocationLib.h>\r
-#include <Library/DevicePathLib.h>\r
-#include <Library/PcdLib.h>\r
-#include <Library/FileHandleLib.h>\r
-#include <Library/PrintLib.h>\r
-#include <Library/UefiLib.h>\r
-#include <Library/HiiLib.h>\r
-\r
-#include <Protocol/EfiShellEnvironment2.h>\r
-#include <Protocol/EfiShellInterface.h>\r
-#include <Protocol/EfiShell.h>\r
-#include <Protocol/EfiShellParameters.h>\r
-#include <Protocol/SimpleFileSystem.h>\r
-\r
 #include "UefiShellLib.h"\r
 \r
 #define MAX_FILE_NAME_LEN 522 // (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for bytes)\r
@@ -56,6 +36,7 @@ STATIC FILE_HANDLE_FUNCTION_MAP      FileFunctionMap;
 STATIC UINTN                         mTotalParameterCount;\r
 STATIC CHAR16                        *mPostReplaceFormat;\r
 STATIC CHAR16                        *mPostReplaceFormat2;\r
+\r
 /**\r
   Check if a Unicode character is a hexadecimal character.\r
 \r
@@ -72,7 +53,7 @@ STATIC CHAR16                        *mPostReplaceFormat2;
 **/\r
 BOOLEAN\r
 EFIAPI\r
-ShellInternalIsHexaDecimalDigitCharacter (\r
+ShellLibIsHexaDecimalDigitCharacter (\r
   IN      CHAR16                    Char\r
   ) {\r
   return (BOOLEAN) ((Char >= L'0' && Char <= L'9') || (Char >= L'A' && Char <= L'F') || (Char >= L'a' && Char <= L'f'));\r
@@ -161,10 +142,10 @@ ShellLibConstructorWorker (
   ) {\r
   EFI_STATUS Status;\r
 \r
-  ASSERT(PcdGet16 (PcdShellLibMaxPrintBufferSize) < PcdGet32 (PcdMaximumUnicodeStringLength));\r
-  mPostReplaceFormat = AllocateZeroPool (PcdGet16 (PcdShellLibMaxPrintBufferSize));\r
+  ASSERT(PcdGet16 (PcdShellPrintBufferSize) < PcdGet32 (PcdMaximumUnicodeStringLength));\r
+  mPostReplaceFormat = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));\r
   ASSERT (mPostReplaceFormat != NULL);\r
-  mPostReplaceFormat2 = AllocateZeroPool (PcdGet16 (PcdShellLibMaxPrintBufferSize));\r
+  mPostReplaceFormat2 = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));\r
   ASSERT (mPostReplaceFormat2 != NULL);\r
 \r
   //\r
@@ -1487,9 +1468,9 @@ ShellCloseFileMetaArg (
 /**\r
   Find a file by searching the CWD and then the path.\r
 \r
-  if FileName is NULL then ASSERT.\r
+  If FileName is NULL then ASSERT.\r
 \r
-  if the return value is not NULL then the memory must be caller freed.\r
+  If the return value is not NULL then the memory must be caller freed.\r
 \r
   @param FileName               Filename string.\r
 \r
@@ -1557,6 +1538,62 @@ ShellFindFilePath (
   return (RetVal);\r
 }\r
 \r
+/**\r
+  Find a file by searching the CWD and then the path with a variable set of file \r
+  extensions.  If the file is not found it will append each extension in the list \r
+  in the order provided and return the first one that is successful.\r
+\r
+  If FileName is NULL, then ASSERT.\r
+  If FileExtension is NULL, then behavior is identical to ShellFindFilePath.\r
+\r
+  If the return value is not NULL then the memory must be caller freed.\r
+\r
+  @param[in] FileName           Filename string.\r
+  @param[in] FileExtension      Semi-colon delimeted list of possible extensions.\r
+\r
+  @retval NULL                  The file was not found.\r
+  @retval !NULL                 The path to the file.\r
+**/\r
+CHAR16 *\r
+EFIAPI\r
+ShellFindFilePathEx (\r
+  IN CONST CHAR16 *FileName,\r
+  IN CONST CHAR16 *FileExtension\r
+  )\r
+{\r
+  CHAR16            *TestPath;\r
+  CHAR16            *RetVal;\r
+  CONST CHAR16      *ExtensionWalker;\r
+  ASSERT(FileName != NULL);\r
+  if (FileExtension == NULL) {\r
+    return (ShellFindFilePath(FileName));\r
+  }\r
+  RetVal = ShellFindFilePath(FileName);\r
+  if (RetVal != NULL) {\r
+    return (RetVal);\r
+  }\r
+  TestPath = AllocateZeroPool(StrSize(FileName) + StrSize(FileExtension));\r
+  for (ExtensionWalker = FileExtension ;  ; ExtensionWalker = StrStr(ExtensionWalker, L";") + 1 ){\r
+    StrCpy(TestPath, FileName);\r
+    StrCat(TestPath, ExtensionWalker);\r
+    if (StrStr(TestPath, L";") != NULL) {\r
+      *(StrStr(TestPath, L";")) = CHAR_NULL;\r
+    }\r
+    RetVal = ShellFindFilePath(TestPath);\r
+    if (RetVal != NULL) {\r
+      break;\r
+    }\r
+    //\r
+    // Must be after first loop...\r
+    //\r
+    if (StrStr(ExtensionWalker, L";") == NULL) {\r
+      break;\r
+    }\r
+  }\r
+  FreePool(TestPath);\r
+  return (RetVal);\r
+}\r
+\r
 typedef struct {\r
   LIST_ENTRY     Link;\r
   CHAR16         *Name;\r
@@ -1647,7 +1684,7 @@ InternalIsFlag (
   //\r
   // If we accept numbers then dont return TRUE. (they will be values)\r
   //\r
-  if (((Name[0] == L'-' || Name[0] == L'+') && ShellInternalIsHexaDecimalDigitCharacter(Name[1])) && AlwaysAllowNumbers == TRUE) {\r
+  if (((Name[0] == L'-' || Name[0] == L'+') && ShellLibIsHexaDecimalDigitCharacter(Name[1])) && AlwaysAllowNumbers != FALSE) {\r
     return (FALSE);\r
   }\r
 \r
@@ -1738,7 +1775,7 @@ InternalCommandLineParse (
       //\r
       // do nothing for NULL argv\r
       //\r
-    } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType) == TRUE) {\r
+    } else if (InternalIsOnCheckList(Argv[LoopCounter], CheckList, &CurrentItemType) != FALSE) {\r
       //\r
       // We might have leftover if last parameter didnt have optional value\r
       //\r
@@ -2200,9 +2237,11 @@ ShellCommandLineCheckDuplicate (
 }\r
 \r
 /**\r
-  This is a find and replace function.  it will return the NewString as a copy of \r
+  This is a find and replace function.  Upon successful return the NewString is a copy of \r
   SourceString with each instance of FindTarget replaced with ReplaceWith.\r
 \r
+  If SourceString and NewString overlap the behavior is undefined.\r
+\r
   If the string would grow bigger than NewSize it will halt and return error.\r
 \r
   @param[in] SourceString             String with source buffer\r
@@ -2224,7 +2263,7 @@ ShellCommandLineCheckDuplicate (
 \r
 EFI_STATUS\r
 EFIAPI\r
-CopyReplace(\r
+ShellLibCopySearchAndReplace(\r
   IN CHAR16 CONST                     *SourceString,\r
   IN CHAR16                           *NewString,\r
   IN UINTN                            NewSize,\r
@@ -2342,21 +2381,21 @@ InternalShellPrintWorker(
   //\r
   // Back and forth each time fixing up 1 of our flags...\r
   //\r
-  Status = CopyReplace(Format,             mPostReplaceFormat,  PcdGet16 (PcdShellLibMaxPrintBufferSize), L"%N", L"%%N");\r
+  Status = ShellLibCopySearchAndReplace(Format,             mPostReplaceFormat,  PcdGet16 (PcdShellPrintBufferSize), L"%N", L"%%N");\r
   ASSERT_EFI_ERROR(Status);\r
-  Status = CopyReplace(mPostReplaceFormat,  mPostReplaceFormat2, PcdGet16 (PcdShellLibMaxPrintBufferSize), L"%E", L"%%E");\r
+  Status = ShellLibCopySearchAndReplace(mPostReplaceFormat,  mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%E", L"%%E");\r
   ASSERT_EFI_ERROR(Status);\r
-  Status = CopyReplace(mPostReplaceFormat2, mPostReplaceFormat,  PcdGet16 (PcdShellLibMaxPrintBufferSize), L"%H", L"%%H");\r
+  Status = ShellLibCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat,  PcdGet16 (PcdShellPrintBufferSize), L"%H", L"%%H");\r
   ASSERT_EFI_ERROR(Status);\r
-  Status = CopyReplace(mPostReplaceFormat,  mPostReplaceFormat2, PcdGet16 (PcdShellLibMaxPrintBufferSize), L"%B", L"%%B");\r
+  Status = ShellLibCopySearchAndReplace(mPostReplaceFormat,  mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), L"%B", L"%%B");\r
   ASSERT_EFI_ERROR(Status);\r
-  Status = CopyReplace(mPostReplaceFormat2, mPostReplaceFormat,  PcdGet16 (PcdShellLibMaxPrintBufferSize), L"%V", L"%%V");\r
+  Status = ShellLibCopySearchAndReplace(mPostReplaceFormat2, mPostReplaceFormat,  PcdGet16 (PcdShellPrintBufferSize), L"%V", L"%%V");\r
   ASSERT_EFI_ERROR(Status);\r
 \r
   //\r
   // Use the last buffer from replacing to print from...\r
   //\r
-  Return = UnicodeVSPrint (mPostReplaceFormat2, PcdGet16 (PcdShellLibMaxPrintBufferSize), mPostReplaceFormat, Marker);\r
+  Return = UnicodeVSPrint (mPostReplaceFormat2, PcdGet16 (PcdShellPrintBufferSize), mPostReplaceFormat, Marker);\r
 \r
   if (Col != -1 && Row != -1) {\r
     Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);\r
@@ -2593,6 +2632,39 @@ ShellIsFile(
   return (EFI_NOT_FOUND);\r
 }\r
 \r
+/**\r
+  Function to determine if a given filename represents a file.\r
+\r
+  This will search the CWD and then the Path.\r
+\r
+  If Name is NULL, then ASSERT.\r
+\r
+  @param[in] Name         Path to file to test.\r
+\r
+  @retval EFI_SUCCESS     The Path represents a file.\r
+  @retval EFI_NOT_FOUND   The Path does not represent a file.\r
+  @retval other           The path failed to open.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+ShellIsFileInPath(\r
+  IN CONST CHAR16 *Name\r
+  ) {\r
+  CHAR16      *NewName;\r
+  EFI_STATUS  Status;\r
+\r
+  if (!EFI_ERROR(ShellIsFile(Name))) {\r
+    return (TRUE);\r
+  }\r
+\r
+  NewName = ShellFindFilePath(Name);\r
+  if (NewName == NULL) {\r
+    return (EFI_NOT_FOUND);\r
+  }\r
+  Status = ShellIsFile(NewName);\r
+  FreePool(NewName);\r
+  return (Status);\r
+}\r
 /**\r
   Function to determine whether a string is decimal or hex representation of a number \r
   and return the number converted from the string.\r
@@ -2608,7 +2680,7 @@ ShellStrToUintn(
   )\r
 {\r
   CONST CHAR16  *Walker;\r
-  for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker = Walker + 1);\r
+  for (Walker = String; Walker != NULL && *Walker != CHAR_NULL && *Walker == L' '; Walker++);\r
   if (StrnCmp(Walker, L"0x", 2) == 0 || StrnCmp(Walker, L"0X", 2) == 0){\r
     return (StrHexToUintn(Walker));\r
   }\r