]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Application/Shell/ShellManParser.c
NetworkPkg:Fix a bug the 2nd httpboot fail issue.
[mirror_edk2.git] / ShellPkg / Application / Shell / ShellManParser.c
index fe9facb483ced97a78f6d95e74cbbc98df0fd816..f12775f8658531f6376a26363452158fb9126ec7 100644 (file)
@@ -1,7 +1,8 @@
 /** @file\r
   Provides interface to shell MAN file parser.\r
 \r
-  Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>\r
+  Copyright 2015 Dell Inc.\r
   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
@@ -14,6 +15,8 @@
 \r
 #include "Shell.h"\r
 \r
+CHAR16 EFIAPI InternalShellCharToUpper (IN CHAR16  Char);\r
+\r
 /**\r
   Verifies that the filename has .MAN on the end.\r
 \r
@@ -32,17 +35,28 @@ GetManFileName(
   )\r
 {\r
   CHAR16 *Buffer;\r
-  ASSERT(ManFileName != NULL);\r
+  if (ManFileName == NULL) {\r
+    return (NULL);\r
+  }\r
   //\r
   // Fix the file name\r
   //\r
   if (StrnCmp(ManFileName+StrLen(ManFileName)-4, L".man", 4)==0) {\r
-    Buffer = AllocateZeroPool(StrSize(ManFileName));\r
-    StrCpy(Buffer, ManFileName);\r
+    Buffer = AllocateCopyPool(StrSize(ManFileName), ManFileName);\r
   } else {\r
     Buffer = AllocateZeroPool(StrSize(ManFileName) + 4*sizeof(CHAR16));\r
-    StrCpy(Buffer, ManFileName);\r
-    StrCat(Buffer, L".man");\r
+    if (Buffer != NULL) {\r
+      StrnCpyS( Buffer, \r
+                (StrSize(ManFileName) + 4*sizeof(CHAR16))/sizeof(CHAR16), \r
+                ManFileName, \r
+                StrLen(ManFileName)\r
+                );\r
+      StrnCatS( Buffer, \r
+                (StrSize(ManFileName) + 4*sizeof(CHAR16))/sizeof(CHAR16),\r
+                L".man", \r
+                4\r
+                );\r
+    }\r
   }\r
   return (Buffer);\r
 }\r
@@ -174,6 +188,10 @@ ManBufferFindSections(
         TempString2 = MIN(TempString2, StrStr(CurrentLocation, L"\n"));\r
         ASSERT(TempString == NULL);\r
         TempString = StrnCatGrow(&TempString, NULL, CurrentLocation, TempString2==NULL?0:TempString2 - CurrentLocation);\r
+        if (TempString == NULL) {\r
+          Status = EFI_OUT_OF_RESOURCES;\r
+          break;\r
+        }\r
         SectionName = TempString;\r
         SectionLen = StrLen(SectionName);\r
         SectionName = StrStr(Sections, SectionName);\r
@@ -191,12 +209,24 @@ ManBufferFindSections(
         TempString2 = MIN(TempString2, StrStr(CurrentLocation, L"\n"));\r
         ASSERT(TempString == NULL);\r
         TempString = StrnCatGrow(&TempString, NULL, CurrentLocation, TempString2==NULL?0:TempString2 - CurrentLocation);\r
+        if (TempString == NULL) {\r
+          Status = EFI_OUT_OF_RESOURCES;\r
+          break;\r
+        }\r
         //\r
         // copy and save the current line.\r
         //\r
         ASSERT((*HelpText == NULL && *HelpSize == 0) || (*HelpText != NULL));\r
         StrnCatGrow (HelpText, HelpSize, TempString, 0);\r
+        if (HelpText == NULL) {\r
+          Status = EFI_OUT_OF_RESOURCES;\r
+          break;\r
+        }\r
         StrnCatGrow (HelpText, HelpSize, L"\r\n", 0);\r
+        if (HelpText == NULL) {\r
+          Status = EFI_OUT_OF_RESOURCES;\r
+          break;\r
+        }\r
       }\r
     }\r
     SHELL_FREE_NON_NULL(TempString);\r
@@ -352,6 +382,9 @@ ManBufferFindTitleSection(
   CHAR16        *TitleString;\r
   CHAR16        *TitleEnd;\r
   CHAR16        *CurrentLocation;\r
+  UINTN         TitleLength;\r
+  CONST CHAR16  StartString[] = L".TH ";\r
+  CONST CHAR16  EndString[]   = L" 0 ";\r
 \r
   if ( Buffer     == NULL\r
     || Command    == NULL\r
@@ -362,13 +395,17 @@ ManBufferFindTitleSection(
 \r
   Status    = EFI_SUCCESS;\r
 \r
-  TitleString = AllocatePool((7*sizeof(CHAR16)) + StrSize(Command));\r
+  //\r
+  // more characters for StartString and EndString\r
+  //\r
+  TitleLength = StrSize(Command) + (StrLen(StartString) + StrLen(EndString)) * sizeof(CHAR16);\r
+  TitleString = AllocateZeroPool(TitleLength);\r
   if (TitleString == NULL) {\r
     return (EFI_OUT_OF_RESOURCES);\r
   }\r
-  StrCpy(TitleString, L".TH ");\r
-  StrCat(TitleString, Command);\r
-  StrCat(TitleString, L" 0 ");\r
+  StrCpyS(TitleString, TitleLength/sizeof(CHAR16), StartString);\r
+  StrCatS(TitleString, TitleLength/sizeof(CHAR16), Command);\r
+  StrCatS(TitleString, TitleLength/sizeof(CHAR16), EndString);\r
 \r
   CurrentLocation = StrStr(*Buffer, TitleString);\r
   if (CurrentLocation == NULL){\r
@@ -383,47 +420,179 @@ ManBufferFindTitleSection(
       ;  CurrentLocation++);\r
 \r
     TitleEnd = StrStr(CurrentLocation, L"\"");\r
-    ASSERT(TitleEnd != NULL);\r
-    if (BriefDesc != NULL) {\r
-      *BriefSize = StrSize(TitleEnd);\r
-      *BriefDesc = AllocateZeroPool(*BriefSize);\r
-      if (*BriefDesc == NULL) {\r
-        Status = EFI_OUT_OF_RESOURCES;\r
-      } else {\r
-        StrnCpy(*BriefDesc, CurrentLocation, TitleEnd-CurrentLocation);\r
+    if (TitleEnd == NULL) {\r
+      Status = EFI_DEVICE_ERROR;\r
+    } else {\r
+      if (BriefDesc != NULL) {\r
+        *BriefSize = StrSize(TitleEnd);\r
+        *BriefDesc = AllocateZeroPool(*BriefSize);\r
+        if (*BriefDesc == NULL) {\r
+          Status = EFI_OUT_OF_RESOURCES;\r
+        } else {\r
+          StrnCpyS(*BriefDesc, (*BriefSize)/sizeof(CHAR16), CurrentLocation, TitleEnd-CurrentLocation);\r
+        }\r
       }\r
-    }\r
 \r
-    for (CurrentLocation = TitleEnd\r
-      ;  *CurrentLocation != L'\n'\r
-      ;  CurrentLocation++);\r
-    for (\r
-      ;  *CurrentLocation == L' ' || *CurrentLocation == L'\n' || *CurrentLocation == L'\r'\r
-      ;  CurrentLocation++);\r
-    *Buffer = CurrentLocation;\r
+      for (CurrentLocation = TitleEnd\r
+        ;  *CurrentLocation != L'\n'\r
+        ;  CurrentLocation++);\r
+      for (\r
+        ;  *CurrentLocation == L' ' || *CurrentLocation == L'\n' || *CurrentLocation == L'\r'\r
+        ;  CurrentLocation++);\r
+      *Buffer = CurrentLocation;\r
+    }\r
   }\r
 \r
   FreePool(TitleString);\r
   return (Status);\r
 }\r
 \r
+/**\r
+  Parses a line from a MAN file to see if it is the Title Header. If it is, then\r
+  if the "Brief Description" is desired, allocate a buffer for it and return a\r
+  copy. Upon a sucessful return the caller is responsible to free the memory in\r
+  *BriefDesc\r
+\r
+  Uses a simple state machine that allows "unlimited" whitespace before and after the\r
+  ".TH", compares Command and the MAN file commnd name without respect to case, and\r
+  allows "unlimited" whitespace and '0' and '1' characters before the Short Description.\r
+  The PCRE regex describing this functionality is: ^\s*\.TH\s+(\S)\s[\s01]*(.*)$\r
+  where group 1 is the Command Name and group 2 is the Short Description.\r
+\r
+  @param[in] Command             name of command whose MAN file we think Line came from\r
+  @param[in] Line                Pointer to a line from the MAN file\r
+  @param[out] BriefDesc          pointer to pointer to string where description goes.\r
+  @param[out] BriefSize          pointer to size of allocated BriefDesc\r
+  @param[out] Found              TRUE if the Title Header was found and it belongs to Command\r
+\r
+  @retval TRUE   Line contained the Title Header\r
+  @retval FALSE  Line did not contain the Title Header\r
+**/\r
+BOOLEAN\r
+IsTitleHeader(\r
+  IN CONST CHAR16       *Command,\r
+  IN CHAR16             *Line,\r
+  OUT CHAR16            **BriefDesc OPTIONAL,\r
+  OUT UINTN             *BriefSize OPTIONAL,\r
+  OUT BOOLEAN           *Found\r
+  )\r
+{\r
+  // The states of a simple state machine used to recognize a title header line\r
+  // and to extract the Short Description, if desired.\r
+  typedef enum {\r
+    LookForThMacro, LookForCommandName, CompareCommands, GetBriefDescription, Final\r
+  } STATEVALUES;\r
+\r
+  STATEVALUES  State;\r
+  UINTN        CommandIndex; // Indexes Command as we compare its chars to the MAN file.\r
+  BOOLEAN      ReturnValue;  // TRUE if this the Title Header line of *some* MAN file.\r
+  BOOLEAN      ReturnFound;  // TRUE if this the Title Header line of *the desired* MAN file.\r
+\r
+  ReturnValue = FALSE;\r
+  ReturnFound = FALSE;\r
+  CommandIndex = 0;\r
+  State = LookForThMacro;\r
+\r
+  do {\r
+\r
+    if (*Line == L'\0') {\r
+      break;\r
+    }\r
+\r
+    switch (State) {\r
+\r
+      // Handle "^\s*.TH\s"\r
+      // Go to state LookForCommandName if the title header macro is present; otherwise,\r
+      // eat white space. If we see something other than white space, this is not a\r
+      // title header line.\r
+      case LookForThMacro:\r
+        if (StrnCmp (L".TH ", Line, 4) == 0 || StrnCmp (L".TH\t", Line, 4) == 0) {\r
+          Line += 4;\r
+          State = LookForCommandName;\r
+        }\r
+        else if (*Line == L' ' || *Line == L'\t') {\r
+          Line++;\r
+        }\r
+        else {\r
+          State = Final;\r
+        }\r
+      break;\r
+\r
+      // Handle "\s*"\r
+      // Eat any "extra" whitespace after the title header macro (we have already seen\r
+      // at least one white space character). Go to state CompareCommands when a\r
+      // non-white space is seen.\r
+      case LookForCommandName:\r
+        if (*Line == L' ' || *Line == L'\t') {\r
+          Line++;\r
+        }\r
+        else {\r
+          ReturnValue = TRUE;  // This is *some* command's title header line.\r
+          State = CompareCommands;\r
+          // Do not increment Line; it points to the first character of the command\r
+          // name on the title header line.\r
+        }\r
+      break;\r
+\r
+      // Handle "(\S)\s"\r
+      // Compare Command to the title header command name, ignoring case. When we\r
+      // reach the end of the command (i.e. we see white space), the next state\r
+      // depends on whether the caller wants a copy of the Brief Description.\r
+      case CompareCommands:\r
+        if (*Line == L' ' || *Line == L'\t') {\r
+          ReturnFound = TRUE;  // This is the desired command's title header line.\r
+          State = (BriefDesc == NULL) ? Final : GetBriefDescription;\r
+        }\r
+        else if (InternalShellCharToUpper (*Line) != InternalShellCharToUpper (*(Command + CommandIndex++))) {\r
+          State = Final;\r
+        }\r
+        Line++;\r
+      break;\r
+\r
+      // Handle "[\s01]*(.*)$"\r
+      // Skip whitespace, '0', and '1' characters, if any, prior to the brief description.\r
+      // Return the description to the caller.\r
+      case GetBriefDescription:\r
+        if (*Line != L' ' && *Line != L'\t' && *Line != L'0' && *Line != L'1') {\r
+          *BriefSize = StrSize(Line);\r
+          *BriefDesc = AllocateZeroPool(*BriefSize);\r
+          if (*BriefDesc != NULL) {\r
+            StrCpyS(*BriefDesc, (*BriefSize)/sizeof(CHAR16), Line);\r
+          }\r
+          State = Final;\r
+        }\r
+        Line++;\r
+      break;\r
+\r
+      default:\r
+       break;\r
+    }\r
+\r
+  } while (State < Final);\r
+\r
+  *Found = ReturnFound;\r
+  return ReturnValue;\r
+}\r
+\r
 /**\r
   parses through the MAN file specified by SHELL_FILE_HANDLE and returns the\r
-  "Brief Description" for the .TH section as specified by Command.  if the\r
+  "Brief Description" for the .TH section as specified by Command.  If the\r
   command section is not found return EFI_NOT_FOUND.\r
 \r
   Upon a sucessful return the caller is responsible to free the memory in *BriefDesc\r
 \r
-  @param[in] Handle             FileHandle to read from\r
-  @param[in] Command            name of command's section to find\r
-  @param[out] BriefDesc         pointer to pointer to string where description goes.\r
-  @param[out] BriefSize         pointer to size of allocated BriefDesc\r
-  @param[in,out] Ascii          TRUE if the file is ASCII, FALSE otherwise, will be\r
-                                set if the file handle is at the 0 position.\r
+  @param[in] Handle              FileHandle to read from\r
+  @param[in] Command             name of command's section to find as entered on the\r
+                                 command line (may be a relative or absolute path or\r
+                                 be in any case: upper, lower, or mixed in numerous ways!).\r
+  @param[out] BriefDesc          pointer to pointer to string where description goes.\r
+  @param[out] BriefSize          pointer to size of allocated BriefDesc\r
+  @param[in, out] Ascii          TRUE if the file is ASCII, FALSE otherwise, will be\r
+                                 set if the file handle is at the 0 position.\r
 \r
   @retval EFI_OUT_OF_RESOURCES  a memory allocation failed.\r
-  @retval EFI_SUCCESS           the section was found and its description sotred in\r
-                                an alloceted buffer.\r
+  @retval EFI_SUCCESS           the section was found and its description stored in\r
+                                an allocated buffer if requested.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -436,12 +605,10 @@ ManFileFindTitleSection(
   )\r
 {\r
   EFI_STATUS  Status;\r
-  CHAR16      *TitleString;\r
   CHAR16      *ReadLine;\r
   UINTN       Size;\r
-  CHAR16      *TitleEnd;\r
-  UINTN       TitleLen;\r
   BOOLEAN     Found;\r
+  UINTN       Start;\r
 \r
   if ( Handle     == NULL\r
     || Command    == NULL\r
@@ -459,57 +626,34 @@ ManFileFindTitleSection(
     return (EFI_OUT_OF_RESOURCES);\r
   }\r
 \r
-  TitleString = AllocatePool((4*sizeof(CHAR16)) + StrSize(Command));\r
-  if (TitleString == NULL) {\r
-    FreePool(ReadLine);\r
-    return (EFI_OUT_OF_RESOURCES);\r
+  //\r
+  // Do not pass any leading path information that may be present to IsTitleHeader().\r
+  //\r
+  Start = StrLen(Command);\r
+  while (Start\r
+         && (*(Command + Start - 1) != L'\\')\r
+         && (*(Command + Start - 1) != L'/')\r
+         && (*(Command + Start - 1) != L':')) {\r
+    --Start;\r
   }\r
-  StrCpy(TitleString, L".TH ");\r
-  StrCat(TitleString, Command);\r
-  TitleLen = StrLen(TitleString);\r
+\r
   for (;!ShellFileHandleEof(Handle);Size = 1024) {\r
    Status = ShellFileHandleReadLine(Handle, ReadLine, &Size, TRUE, Ascii);\r
-    if (ReadLine[0] == L'#') {\r
-      //\r
-      // Skip comment lines\r
-      //\r
-      continue;\r
-    }\r
     //\r
     // ignore too small of buffer...\r
     //\r
-    if (Status == EFI_BUFFER_TOO_SMALL) {\r
-      Status = EFI_SUCCESS;\r
-    }\r
-    if (EFI_ERROR(Status)) {\r
+    if (EFI_ERROR(Status) && Status != EFI_BUFFER_TOO_SMALL) {\r
       break;\r
     }\r
-    if (StrnCmp(ReadLine, TitleString, TitleLen) == 0) {\r
-      Found = TRUE;\r
-      //\r
-      // we found it so copy out the rest of the line into BriefDesc\r
-      // After skipping any spaces or zeroes\r
-      //\r
-      for ( TitleEnd = ReadLine+TitleLen\r
-          ; *TitleEnd == L' ' || *TitleEnd == L'0' || *TitleEnd == L'1'\r
-          ; TitleEnd++);\r
-      if (BriefDesc != NULL) {\r
-        *BriefSize = StrSize(TitleEnd);\r
-        *BriefDesc = AllocateZeroPool(*BriefSize);\r
-        if (*BriefDesc == NULL) {\r
-          Status = EFI_OUT_OF_RESOURCES;\r
-          break;\r
-        }\r
-        StrCpy(*BriefDesc, TitleEnd);\r
-      }\r
+\r
+    Status = EFI_NOT_FOUND;\r
+    if (IsTitleHeader (Command+Start, ReadLine, BriefDesc, BriefSize, &Found)) {\r
+      Status = Found ? EFI_SUCCESS : EFI_NOT_FOUND;\r
       break;\r
     }\r
   }\r
+\r
   FreePool(ReadLine);\r
-  FreePool(TitleString);\r
-  if (!Found && !EFI_ERROR(Status)) {\r
-    return (EFI_NOT_FOUND);\r
-  }\r
   return (Status);\r
 }\r
 \r
@@ -539,7 +683,8 @@ ManFileFindTitleSection(
   @retval EFI_SUCCESS           The help text was returned.\r
   @retval EFI_OUT_OF_RESOURCES  The necessary buffer could not be allocated to hold the\r
                                 returned help text.\r
-  @retval EFI_INVALID_PARAMETER HelpText is NULL\r
+  @retval EFI_INVALID_PARAMETER HelpText is NULL.\r
+  @retval EFI_INVALID_PARAMETER ManFileName is invalid.\r
   @retval EFI_NOT_FOUND         There is no help text available for Command.\r
 **/\r
 EFI_STATUS\r
@@ -572,6 +717,7 @@ ProcessManFile(
   HelpSize    = 0;\r
   BriefSize   = 0;\r
   TempString  = NULL;\r
+  Ascii       = FALSE;\r
   //\r
   // See if it's in HII first\r
   //\r
@@ -585,6 +731,9 @@ ProcessManFile(
   } else {\r
     FileHandle    = NULL;\r
     TempString  = GetManFileName(ManFileName);\r
+    if (TempString == NULL) {\r
+      return (EFI_INVALID_PARAMETER);\r
+    }\r
 \r
     Status = SearchPathForFile(TempString, &FileHandle);\r
     if (EFI_ERROR(Status)) {\r