]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellLib/UefiShellLib.c
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@8633 6f19259b...
[mirror_edk2.git] / ShellPkg / Library / UefiShellLib / UefiShellLib.c
index 7ac7765b4b0dadd3ce386433fd46f67192023f58..3a56903789a1693b0c57a6215148e83337a6d8b7 100644 (file)
@@ -258,8 +258,7 @@ EFIAPI
 ShellLibDestructor (\r
   IN EFI_HANDLE        ImageHandle,\r
   IN EFI_SYSTEM_TABLE  *SystemTable\r
-  )\r
-{\r
+  ){\r
   if (mEfiShellEnvironment2 != NULL) {\r
     gBS->CloseProtocol(mEfiShellEnvironment2Handle==NULL?ImageHandle:mEfiShellEnvironment2Handle,\r
                        &gEfiShellEnvironment2Guid,\r
@@ -984,7 +983,7 @@ ShellGetExecutionBreakFlag(
 CONST CHAR16*\r
 EFIAPI\r
 ShellGetEnvironmentVariable (\r
-  IN CHAR16                     *EnvKey\r
+  IN CONST CHAR16                *EnvKey\r
   )\r
 {\r
   // \r
@@ -1002,7 +1001,7 @@ ShellGetEnvironmentVariable (
   //\r
   // using EFI Shell\r
   //\r
-  return (mEfiShellEnvironment2->GetEnv(EnvKey));\r
+  return (mEfiShellEnvironment2->GetEnv((CHAR16*)EnvKey));\r
 }\r
 /**\r
   set the value of an environment variable\r
@@ -1220,32 +1219,26 @@ typedef struct {
   EFI_SHELL_FILE_INFO based list.  it is up to the caller to free the memory via\r
   the ShellCloseFileMetaArg function.\r
 \r
-  @param FileList               the EFI shell list type\r
+  @param[in] FileList           the EFI shell list type\r
+  @param[in][out] ListHead      the list to add to\r
 \r
   @retval the resultant head of the double linked new format list;\r
 **/\r
 LIST_ENTRY*\r
 EFIAPI\r
 InternalShellConvertFileListType (\r
-  LIST_ENTRY                *FileList\r
-  )\r
-{\r
-  LIST_ENTRY                    *ListHead;\r
+  IN LIST_ENTRY                 *FileList,\r
+  IN OUT LIST_ENTRY             *ListHead\r
+  ){\r
   SHELL_FILE_ARG                *OldInfo;\r
-  LIST_ENTRY                *Link;\r
+  LIST_ENTRY                    *Link;\r
   EFI_SHELL_FILE_INFO_NO_CONST  *NewInfo;\r
 \r
   //\r
-  // ASSERT that FileList is not NULL\r
-  //\r
-  ASSERT(FileList != NULL);\r
-\r
-  //\r
-  // Allocate our list head and initialize the list\r
+  // ASSERTs\r
   //\r
-  ListHead = AllocateZeroPool(sizeof(LIST_ENTRY));\r
-  ASSERT (ListHead != NULL);\r
-  ListHead = InitializeListHead (ListHead);\r
+  ASSERT(FileList  != NULL);\r
+  ASSERT(ListHead  != NULL);\r
 \r
   //\r
   // enumerate through each member of the old list and copy\r
@@ -1299,7 +1292,7 @@ InternalShellConvertFileListType (
     //\r
     // add that to the list\r
     //\r
-    InsertTailList(ListHead, (LIST_ENTRY*)NewInfo);\r
+    InsertTailList(ListHead, &NewInfo->Link);\r
   }\r
   return (ListHead);\r
 }\r
@@ -1313,7 +1306,7 @@ InternalShellConvertFileListType (
   and will process '?' and '*' as such.  the list must be freed with a call to \r
   ShellCloseFileMetaArg().\r
 \r
-  This function will fail if called sequentially without freeing the list in the middle.\r
+  If you are NOT appending to an existing list *ListHead must be NULL.\r
 \r
   @param Arg                    pointer to path string\r
   @param OpenMode               mode to open files with\r
@@ -1336,8 +1329,7 @@ ShellOpenFileMetaArg (
   )\r
 {\r
   EFI_STATUS                    Status;\r
-  LIST_ENTRY                    *EmptyNode;\r
-  LIST_ENTRY                    *mOldStyleFileList;\r
+  LIST_ENTRY                    mOldStyleFileList;\r
   \r
   //\r
   // ASSERT that Arg and ListHead are not NULL\r
@@ -1359,43 +1351,36 @@ ShellOpenFileMetaArg (
   //\r
   ASSERT(mEfiShellEnvironment2 != NULL);\r
 \r
-  //\r
-  // allocate memory for old list head\r
-  //\r
-  mOldStyleFileList = (LIST_ENTRY*)AllocatePool(sizeof(LIST_ENTRY));\r
-  ASSERT(mOldStyleFileList != NULL);\r
-\r
   //\r
   // make sure the list head is initialized\r
   //\r
-  InitializeListHead((LIST_ENTRY*)mOldStyleFileList);\r
+  InitializeListHead(&mOldStyleFileList);\r
 \r
   //\r
   // Get the EFI Shell list of files\r
   //\r
-  Status = mEfiShellEnvironment2->FileMetaArg(Arg, mOldStyleFileList);\r
+  Status = mEfiShellEnvironment2->FileMetaArg(Arg, &mOldStyleFileList);\r
   if (EFI_ERROR(Status)) {\r
     *ListHead = NULL;\r
     return (Status);\r
   }\r
 \r
+  if (*ListHead == NULL) {\r
+    *ListHead = (EFI_SHELL_FILE_INFO    *)AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
+    if (*ListHead == NULL) {\r
+      return (EFI_OUT_OF_RESOURCES);\r
+    }\r
+  }\r
+\r
   //\r
   // Convert that to equivalent of UEFI Shell 2.0 structure\r
   //\r
-  EmptyNode = InternalShellConvertFileListType(mOldStyleFileList);\r
+  InternalShellConvertFileListType(&mOldStyleFileList, &(*ListHead)->Link);\r
 \r
   //\r
   // Free the EFI Shell version that was converted.\r
   //\r
-  ASSERT_EFI_ERROR(mEfiShellEnvironment2->FreeFileList(mOldStyleFileList));\r
-  FreePool(mOldStyleFileList);\r
-  mOldStyleFileList = NULL;\r
-\r
-  //\r
-  // remove the empty head of the list\r
-  //\r
-  *ListHead = (EFI_SHELL_FILE_INFO*)RemoveEntryList(EmptyNode);\r
-  FreePool(EmptyNode);  \r
+  mEfiShellEnvironment2->FreeFileList(&mOldStyleFileList);\r
 \r
   return (Status);\r
 }\r
@@ -1431,7 +1416,9 @@ ShellCloseFileMetaArg (
     // Since this is EFI Shell version we need to free our internally made copy \r
     // of the list\r
     //\r
-    for (Node = GetFirstNode((LIST_ENTRY*)*ListHead) ; IsListEmpty((LIST_ENTRY*)*ListHead) == FALSE ; Node = GetFirstNode((LIST_ENTRY*)*ListHead)) {\r
+    for ( Node = GetFirstNode(&(*ListHead)->Link) \r
+        ; IsListEmpty(&(*ListHead)->Link) == FALSE \r
+        ; Node = GetFirstNode(&(*ListHead)->Link)) {\r
       RemoveEntryList(Node);\r
       ((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle->Close(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->Handle);\r
       FreePool(((EFI_SHELL_FILE_INFO_NO_CONST*)Node)->FullName);\r
@@ -1444,7 +1431,7 @@ ShellCloseFileMetaArg (
 }\r
 \r
 typedef struct {\r
-  LIST_ENTRY List;\r
+  LIST_ENTRY     Link;\r
   CHAR16         *Name;\r
   ParamType      Type;\r
   CHAR16         *Value;\r
@@ -1636,7 +1623,7 @@ InternalCommandLineParse (
         //\r
         // this item has no value expected; we are done\r
         //\r
-        InsertHeadList(*CheckPackage, (LIST_ENTRY*)CurrentItemPackage);\r
+        InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
       }\r
     } else if (GetItemValue == TRUE && InternalIsFlag(Argv[LoopCounter]) == FALSE) {\r
       ASSERT(CurrentItemPackage != NULL);\r
@@ -1647,7 +1634,7 @@ InternalCommandLineParse (
       CurrentItemPackage->Value = AllocateZeroPool(StrSize(Argv[LoopCounter]));\r
       ASSERT(CurrentItemPackage->Value != NULL);\r
       StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);\r
-      InsertHeadList(*CheckPackage, (LIST_ENTRY*)CurrentItemPackage);\r
+      InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
     } else if (InternalIsFlag(Argv[LoopCounter]) == FALSE) {\r
       //\r
       // add this one as a non-flag\r
@@ -1660,7 +1647,7 @@ InternalCommandLineParse (
       ASSERT(CurrentItemPackage->Value != NULL);\r
       StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);\r
       CurrentItemPackage->OriginalPosition = Count++;\r
-      InsertHeadList(*CheckPackage, (LIST_ENTRY*)CurrentItemPackage);\r
+      InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);\r
     } else if (ProblemParam) {\r
       //\r
       // this was a non-recognised flag... error!\r
@@ -1946,15 +1933,73 @@ ShellCommandLineGetRawValue (
   }\r
   return (NULL);\r
 }\r
+/**\r
+  This is a find and replace function.  it will return the NewString as a copy of \r
+  SourceString with each instance of FindTarget replaced with ReplaceWith.\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
+  @param[in][out] NewString           String with resultant buffer\r
+  @param[in] NewSize                  Size in bytes of NewString\r
+  @param[in] FindTarget               String to look for\r
+  @param[in[ ReplaceWith              String to replace FindTarget with\r
+\r
+  @retval EFI_INVALID_PARAMETER       SourceString was NULL\r
+  @retval EFI_INVALID_PARAMETER       NewString was NULL\r
+  @retval EFI_INVALID_PARAMETER       FindTarget was NULL\r
+  @retval EFI_INVALID_PARAMETER       ReplaceWith was NULL\r
+  @retval EFI_INVALID_PARAMETER       FindTarget had length < 1\r
+  @retval EFI_INVALID_PARAMETER       SourceString had length < 1\r
+  @retval EFI_BUFFER_TOO_SMALL        NewSize was less than the minimum size to hold \r
+                                      the new string (truncation occurred)\r
+  @retval EFI_SUCCESS                 the string was sucessfully copied with replacement\r
+**/\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+CopyReplace(\r
+  IN CHAR16 CONST                     *SourceString,\r
+  IN CHAR16                           *NewString,\r
+  IN UINTN                            NewSize,\r
+  IN CONST CHAR16                     *FindTarget,\r
+  IN CONST CHAR16                     *ReplaceWith\r
+  ){\r
+  if ( (SourceString == NULL)\r
+    || (NewString    == NULL)\r
+    || (FindTarget   == NULL)\r
+    || (ReplaceWith  == NULL)\r
+    || (StrLen(FindTarget) < 1)\r
+    || (StrLen(SourceString) < 1)\r
+    ){\r
+    return (EFI_INVALID_PARAMETER);\r
+  }\r
+  NewString = SetMem16(NewString, NewSize, L'\0');\r
+  while (*SourceString != L'\0') {\r
+    if (StrnCmp(SourceString, FindTarget, StrLen(FindTarget)) == 0) {\r
+      SourceString += StrLen(FindTarget);\r
+      if (StrSize(NewString) + (StrLen(ReplaceWith)*sizeof(CHAR16)) > NewSize) {\r
+        return (EFI_BUFFER_TOO_SMALL);\r
+      }\r
+      StrCat(NewString, ReplaceWith);\r
+    } else {\r
+      if (StrSize(NewString) + sizeof(CHAR16) > NewSize) {\r
+        return (EFI_BUFFER_TOO_SMALL);\r
+      }\r
+      StrnCat(NewString, SourceString, 1);\r
+      SourceString++;\r
+    }\r
+  }\r
+  return (EFI_SUCCESS);\r
+}\r
 \r
 /**\r
   Print at a specific location on the screen.\r
 \r
-  This function will move the cursor to a given screen location, print the specified string, \r
-  and return the cursor to the original locaiton.  \r
+  This function will move the cursor to a given screen location and print the specified string\r
   \r
   If -1 is specified for either the Row or Col the current screen location for BOTH \r
-  will be used and the cursor's position will not be moved back to an original location.\r
+  will be used.\r
 \r
   if either Row or Col is out of range for the current console, then ASSERT\r
   if Format is NULL, then ASSERT\r
@@ -1986,33 +2031,103 @@ ShellPrintEx(
   ){\r
   VA_LIST           Marker;\r
   UINTN             BufferSize;\r
-  CHAR16            *Buffer;\r
+  CHAR16            *PostReplaceFormat;\r
+  CHAR16            *PostReplaceFormat2;\r
   UINTN             Return;\r
-  INT32             CurrentCol;\r
-  INT32             CurrentRow;\r
+\r
   EFI_STATUS        Status;\r
+  UINTN             NormalAttribute;\r
+  CHAR16            *ResumeLocation;\r
+  CHAR16            *FormatWalker;\r
+\r
+  VA_START (Marker, Format);\r
   \r
   BufferSize = (PcdGet32 (PcdUefiLibMaxPrintBufferSize) + 1) * sizeof (CHAR16);\r
-  Buffer = AllocateZeroPool (BufferSize);\r
-  ASSERT (Buffer != NULL);\r
+  PostReplaceFormat = AllocateZeroPool (BufferSize);\r
+  ASSERT (PostReplaceFormat != NULL);\r
+  PostReplaceFormat2 = AllocateZeroPool (BufferSize);\r
+  ASSERT (PostReplaceFormat2 != NULL);\r
 \r
-  VA_START (Marker, Format);\r
-  Return = UnicodeVSPrint (Buffer, BufferSize, Format, Marker);\r
+  //\r
+  // Back and forth each time fixing up 1 of our flags...\r
+  //\r
+  Status = CopyReplace(Format,             PostReplaceFormat,  BufferSize, L"%N", L"%%N");\r
+  ASSERT_EFI_ERROR(Status);\r
+  Status = CopyReplace(PostReplaceFormat,  PostReplaceFormat2, BufferSize, L"%E", L"%%E");\r
+  ASSERT_EFI_ERROR(Status);\r
+  Status = CopyReplace(PostReplaceFormat2, PostReplaceFormat,  BufferSize, L"%H", L"%%H");\r
+  ASSERT_EFI_ERROR(Status);\r
+  Status = CopyReplace(PostReplaceFormat,  PostReplaceFormat2, BufferSize, L"%B", L"%%B");\r
+  ASSERT_EFI_ERROR(Status);\r
+  Status = CopyReplace(PostReplaceFormat2, PostReplaceFormat,  BufferSize, 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 (PostReplaceFormat2, BufferSize, PostReplaceFormat, Marker);\r
+\r
+  FreePool(PostReplaceFormat);\r
 \r
   if (Col != -1 && Row != -1) {\r
-    CurrentCol = gST->ConOut->Mode->CursorColumn;\r
-    CurrentRow = gST->ConOut->Mode->CursorRow;\r
     Status = gST->ConOut->SetCursorPosition(gST->ConOut, Col, Row);\r
     ASSERT_EFI_ERROR(Status);\r
-    Status = gST->ConOut->OutputString(gST->ConOut, Buffer);\r
-    ASSERT_EFI_ERROR(Status);\r
-    Status = gST->ConOut->SetCursorPosition(gST->ConOut, CurrentCol, CurrentRow);\r
-    ASSERT_EFI_ERROR(Status);\r
-  } else {\r
-    Status = gST->ConOut->OutputString(gST->ConOut, Buffer);\r
+  }\r
+\r
+  NormalAttribute = gST->ConOut->Mode->Attribute;\r
+  FormatWalker = PostReplaceFormat2;\r
+  while (*FormatWalker != L'\0') {\r
+    //\r
+    // Find the next attribute change request\r
+    //\r
+    ResumeLocation = StrStr(FormatWalker, L"%");\r
+    if (ResumeLocation != NULL) {\r
+      *ResumeLocation = L'\0';\r
+    }\r
+    //\r
+    // print the current FormatWalker string\r
+    //\r
+    Status = gST->ConOut->OutputString(gST->ConOut, FormatWalker);\r
     ASSERT_EFI_ERROR(Status);\r
+    //\r
+    // update the attribute\r
+    //\r
+    if (ResumeLocation != NULL) {\r
+      switch (*(ResumeLocation+1)) {\r
+        case (L'N'):\r
+          gST->ConOut->SetAttribute(gST->ConOut, NormalAttribute);\r
+          break;\r
+        case (L'E'):\r
+          gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_YELLOW, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
+          break;\r
+        case (L'H'):\r
+          gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_WHITE, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
+          break;\r
+        case (L'B'):\r
+          gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_BLUE, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
+          break;\r
+        case (L'V'):\r
+          gST->ConOut->SetAttribute(gST->ConOut, EFI_TEXT_ATTR(EFI_GREEN, ((NormalAttribute&(BIT4|BIT5|BIT6))>>4)));\r
+          break;\r
+        default:\r
+          ASSERT(FALSE);\r
+          break;\r
+      }\r
+    } else {\r
+      //\r
+      // reset to normal now...\r
+      //\r
+      gST->ConOut->SetAttribute(gST->ConOut, NormalAttribute);\r
+      break;\r
+    }\r
+\r
+    //\r
+    // update FormatWalker to Resume + 2 (skip the % and the indicator)\r
+    //\r
+    FormatWalker = ResumeLocation + 2;\r
   }\r
 \r
-  FreePool(Buffer);\r
+  FreePool(PostReplaceFormat2);\r
+\r
   return (Return);\r
 }
\ No newline at end of file