]> git.proxmox.com Git - mirror_edk2.git/blobdiff - Tools/CCode/Source/ModifyInf/ModifyInf.c
More moves for Tool Packages
[mirror_edk2.git] / Tools / CCode / Source / ModifyInf / ModifyInf.c
diff --git a/Tools/CCode/Source/ModifyInf/ModifyInf.c b/Tools/CCode/Source/ModifyInf/ModifyInf.c
new file mode 100755 (executable)
index 0000000..6008feb
--- /dev/null
@@ -0,0 +1,321 @@
+/*++\r
+\r
+Copyright (c)  1999-2006 Intel Corporation. All rights reserved\r
+This program and the accompanying materials are licensed and made available \r
+under the terms and conditions of the BSD License which accompanies this \r
+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
+\r
+\r
+Module Name:\r
+\r
+  ModifyInf.c\r
+\r
+Abstract:\r
+\r
+  It is a simple tool to modify some fields in a FV inf file \r
+  and output a new FV inf file.  \r
+\r
+--*/\r
+\r
+#include "stdio.h"\r
+#include "string.h"\r
+\r
+//\r
+// Read a line into buffer including '\r\n'\r
+//\r
+int\r
+ReadLine (\r
+  char *LineBuffer,\r
+  FILE *fp\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  GC_TODO: Add function description\r
+\r
+Arguments:\r
+\r
+  LineBuffer  - GC_TODO: add argument description\r
+  fp          - GC_TODO: add argument description\r
+\r
+Returns:\r
+\r
+  GC_TODO: add return values\r
+\r
+--*/\r
+{\r
+  int   CharC;\r
+  char  *Line;\r
+\r
+  Line = LineBuffer;\r
+\r
+  while ((CharC = fgetc (fp)) != EOF) {\r
+    *Line++ = (char) CharC;\r
+    if (CharC == 0x0a) {\r
+      break;\r
+    }\r
+  }\r
+\r
+  *Line = 0;\r
+\r
+  if (CharC == EOF) {\r
+    return 0;\r
+  } else {\r
+    return 1;\r
+  }\r
+\r
+}\r
+//\r
+// Write a line into output file\r
+//\r
+int\r
+WriteLine (\r
+  char *Line,\r
+  FILE *fp\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  GC_TODO: Add function description\r
+\r
+Arguments:\r
+\r
+  Line  - GC_TODO: add argument description\r
+  fp    - GC_TODO: add argument description\r
+\r
+Returns:\r
+\r
+  GC_TODO: add return values\r
+\r
+--*/\r
+{\r
+  fwrite (Line, strlen (Line), 1, fp);\r
+  return 0;\r
+}\r
+//\r
+// Apply patterns to a line\r
+// Currently there are 2 patterns to support\r
+// '==' replace a field value with a new value\r
+// '+=' append a string at the end of original line\r
+// '-'  prevent the line from applying any patterns\r
+//      it has the highest priority\r
+//\r
+int\r
+ApplyPattern (\r
+  char *Line,\r
+  char *argv[],\r
+  int  argc\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  GC_TODO: Add function description\r
+\r
+Arguments:\r
+\r
+  Line  - GC_TODO: add argument description\r
+  ]     - GC_TODO: add argument description\r
+  argc  - GC_TODO: add argument description\r
+\r
+Returns:\r
+\r
+  GC_TODO: add return values\r
+\r
+--*/\r
+{\r
+  static char Section[256];\r
+  char        PatternBuffer[256];\r
+  char        *Pattern;\r
+  char        *Pattern1;\r
+  char        *Pattern2;\r
+  int         PatternNum;\r
+  char        *Ptr;\r
+\r
+  Pattern     = PatternBuffer;\r
+\r
+  PatternNum  = argc;\r
+\r
+  //\r
+  // For section field\r
+  // record current scope section into static buffer\r
+  //\r
+  Ptr = Line;\r
+  if (*Ptr == '[') {\r
+    while (*Ptr != ']') {\r
+      if (!(*Ptr++)) {\r
+        return -1;\r
+      }\r
+    }\r
+\r
+    strcpy (Section, Line);\r
+    Section[Ptr - Line + 1] = 0;\r
+  }\r
+  //\r
+  // Apply each pattern on the line\r
+  //\r
+  while (PatternNum-- > 3) {\r
+\r
+    strcpy (Pattern, argv[PatternNum]);\r
+\r
+    //\r
+    // For pattern '-'\r
+    // keep it unmodified by other patterns\r
+    //\r
+    if (*Pattern == '-') {\r
+      if (strstr (Line, Pattern + 1)) {\r
+        return 0;\r
+      } else {\r
+        continue;\r
+      }\r
+    }\r
+    //\r
+    // For other patterns\r
+    // get its section at first if it has\r
+    //\r
+    if (*Pattern == '[') {\r
+      if (strncmp (Section, Pattern, strlen (Section))) {\r
+        //\r
+        // This pattern can't be appied for current section\r
+        //\r
+        continue;\r
+      }\r
+      //\r
+      // Strip the section field\r
+      //\r
+      while (*Pattern != ']') {\r
+        if (!(*Pattern++)) {\r
+          return -1;\r
+        }\r
+      }\r
+\r
+      Pattern++;\r
+    }\r
+    //\r
+    // Apply patterns\r
+    //\r
+    Pattern1  = strstr (Pattern, "==");\r
+    Pattern2  = strstr (Pattern, "+=");\r
+    if (Pattern1) {\r
+      //\r
+      // For pattern '=='\r
+      // replace the field value with a new string\r
+      //\r
+      if (!strncmp (Line, Pattern, Pattern1 - Pattern)) {\r
+        Pattern1 += 2;\r
+        Ptr = strstr (Line, "=");\r
+        if (!Ptr) {\r
+          return -1;\r
+        }\r
+\r
+        while (*(++Ptr) == ' ')\r
+          ;\r
+        *Ptr = 0;\r
+        strcat (Line, Pattern1);\r
+        strcat (Line, "\r\n");\r
+      }\r
+    } else if (Pattern2) {\r
+      //\r
+      // For pattern '+='\r
+      // append a string at end of the original string\r
+      //\r
+      if (!strncmp (Line, Pattern, Pattern2 - Pattern)) {\r
+        Pattern2 += 2;\r
+        Ptr = Line;\r
+        while (*Ptr != 0x0D && *Ptr != 0x0A) {\r
+          Ptr++;\r
+        }\r
+\r
+        *Ptr = 0;\r
+        strcat (Line, Pattern2);\r
+        strcat (Line, "\r\n");\r
+      }\r
+    }\r
+  }\r
+\r
+  return 0;\r
+}\r
+\r
+void\r
+Usage (\r
+  void\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  GC_TODO: Add function description\r
+\r
+Arguments:\r
+\r
+  None\r
+\r
+Returns:\r
+\r
+  GC_TODO: add return values\r
+\r
+--*/\r
+{\r
+  printf ("ModifyInf InputFVInfFileName OutputFVInfFileName [Pattern strings]\r\n");\r
+}\r
+\r
+int\r
+main (\r
+  int argc,\r
+  char*argv[]\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  GC_TODO: Add function description\r
+\r
+Arguments:\r
+\r
+  argc  - GC_TODO: add argument description\r
+  ]     - GC_TODO: add argument description\r
+\r
+Returns:\r
+\r
+  GC_TODO: add return values\r
+\r
+--*/\r
+{\r
+  char  LineBuffer[256];\r
+  FILE  *fpin;\r
+  FILE  *fpout;\r
+\r
+  if (argc < 3) {\r
+    Usage ();\r
+    return -1;\r
+  }\r
+\r
+  fpin = fopen (argv[1], "rb");\r
+  if (!fpin) {\r
+    printf ("Can't open input file!\r\n");\r
+    return -1;\r
+  }\r
+\r
+  fpout = fopen (argv[2], "wb");\r
+  if (!fpout) {\r
+    fclose (fpin);\r
+    printf ("Can't create output file!\r\n");\r
+    return -1;\r
+  }\r
+\r
+  while (ReadLine (LineBuffer, fpin)) {\r
+    ApplyPattern (LineBuffer, argv, argc);\r
+    WriteLine (LineBuffer, fpout);\r
+  }\r
+\r
+  fclose (fpin);\r
+  fclose (fpout);\r
+\r
+  return 0;\r
+}\r