PageRenderTime 24ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/edk2/EdkCompatibilityPkg/Sample/Tools/Source/ModifyInf/ModifyInf.c

https://gitlab.com/envieidoc/Clover
C | 339 lines | 167 code | 25 blank | 147 comment | 42 complexity | 888aeff4ec5f632023c870389b046592 MD5 | raw file
  1. /*++
  2. Copyright (c) 1999 - 2010, Intel Corporation. All rights reserved.<BR>
  3. This program and the accompanying materials
  4. are licensed and made available under the terms and conditions of the BSD License
  5. which accompanies this distribution. The full text of the license may be found at
  6. http://opensource.org/licenses/bsd-license.php
  7. THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
  9. Module Name:
  10. ModifyInf.c
  11. Abstract:
  12. It is a simple tool to modify some fields in a FV inf file
  13. and output a new FV inf file.
  14. --*/
  15. #include "stdio.h"
  16. #include "string.h"
  17. #define UTILITY_NAME "ModifyInf"
  18. #define UTILITY_VERSION "v1.0"
  19. //
  20. // Read a line into buffer including '\r\n'
  21. //
  22. int
  23. ReadLine (
  24. char *LineBuffer,
  25. FILE *fp
  26. )
  27. /*++
  28. Routine Description:
  29. GC_TODO: Add function description
  30. Arguments:
  31. LineBuffer - GC_TODO: add argument description
  32. fp - GC_TODO: add argument description
  33. Returns:
  34. GC_TODO: add return values
  35. --*/
  36. {
  37. int CharC;
  38. char *Line;
  39. Line = LineBuffer;
  40. while ((CharC = fgetc (fp)) != EOF) {
  41. *Line++ = (char) CharC;
  42. if (CharC == 0x0a) {
  43. break;
  44. }
  45. }
  46. *Line = 0;
  47. if (CharC == EOF) {
  48. return 0;
  49. } else {
  50. return 1;
  51. }
  52. }
  53. //
  54. // Write a line into output file
  55. //
  56. int
  57. WriteLine (
  58. char *Line,
  59. FILE *fp
  60. )
  61. /*++
  62. Routine Description:
  63. GC_TODO: Add function description
  64. Arguments:
  65. Line - GC_TODO: add argument description
  66. fp - GC_TODO: add argument description
  67. Returns:
  68. GC_TODO: add return values
  69. --*/
  70. {
  71. fwrite (Line, strlen (Line), 1, fp);
  72. return 0;
  73. }
  74. //
  75. // Apply patterns to a line
  76. // Currently there are 2 patterns to support
  77. // '==' replace a field value with a new value
  78. // '+=' append a string at the end of original line
  79. // '-' prevent the line from applying any patterns
  80. // it has the highest priority
  81. //
  82. int
  83. ApplyPattern (
  84. char *Line,
  85. char *argv[],
  86. int argc
  87. )
  88. /*++
  89. Routine Description:
  90. GC_TODO: Add function description
  91. Arguments:
  92. Line - GC_TODO: add argument description
  93. ] - GC_TODO: add argument description
  94. argc - GC_TODO: add argument description
  95. Returns:
  96. GC_TODO: add return values
  97. --*/
  98. {
  99. static char Section[256];
  100. int SectionLength;
  101. char PatternBuffer[256];
  102. char *Pattern;
  103. char *Pattern1;
  104. char *Pattern2;
  105. int PatternNum;
  106. char *Ptr;
  107. Pattern = PatternBuffer;
  108. PatternNum = argc;
  109. //
  110. // For section field
  111. // record current scope section into static buffer
  112. //
  113. Ptr = Line;
  114. if (*Ptr == '[') {
  115. while (*Ptr != ']') {
  116. if (!(*Ptr++)) {
  117. return -1;
  118. }
  119. }
  120. SectionLength = Ptr - Line + 1;
  121. SectionLength = SectionLength > 255 ? 255 : SectionLength;
  122. strncpy (Section, Line, SectionLength);
  123. Section[SectionLength] = 0;
  124. }
  125. //
  126. // Apply each pattern on the line
  127. //
  128. while (PatternNum-- > 3) {
  129. strcpy (Pattern, argv[PatternNum]);
  130. //
  131. // For pattern '-'
  132. // keep it unmodified by other patterns
  133. //
  134. if (*Pattern == '-') {
  135. if (strstr (Line, Pattern + 1)) {
  136. return 0;
  137. } else {
  138. continue;
  139. }
  140. }
  141. //
  142. // For other patterns
  143. // get its section at first if it has
  144. //
  145. if (*Pattern == '[') {
  146. if (strncmp (Section, Pattern, strlen (Section))) {
  147. //
  148. // This pattern can't be appied for current section
  149. //
  150. continue;
  151. }
  152. //
  153. // Strip the section field
  154. //
  155. while (*Pattern != ']') {
  156. if (!(*Pattern++)) {
  157. return -1;
  158. }
  159. }
  160. Pattern++;
  161. }
  162. //
  163. // Apply patterns
  164. //
  165. Pattern1 = strstr (Pattern, "==");
  166. Pattern2 = strstr (Pattern, "+=");
  167. if (Pattern1) {
  168. //
  169. // For pattern '=='
  170. // replace the field value with a new string
  171. //
  172. if (!strncmp (Line, Pattern, Pattern1 - Pattern)) {
  173. Pattern1 += 2;
  174. Ptr = strstr (Line, "=");
  175. if (!Ptr) {
  176. return -1;
  177. }
  178. while (*(++Ptr) == ' ')
  179. ;
  180. *Ptr = 0;
  181. strcat (Line, Pattern1);
  182. strcat (Line, "\r\n");
  183. }
  184. } else if (Pattern2) {
  185. //
  186. // For pattern '+='
  187. // append a string at end of the original string
  188. //
  189. if (!strncmp (Line, Pattern, Pattern2 - Pattern)) {
  190. Pattern2 += 2;
  191. Ptr = Line;
  192. while (*Ptr != 0x0D && *Ptr != 0x0A) {
  193. Ptr++;
  194. }
  195. *Ptr = 0;
  196. strcat (Line, Pattern2);
  197. strcat (Line, "\r\n");
  198. }
  199. }
  200. }
  201. return 0;
  202. }
  203. void
  204. Usage (
  205. void
  206. )
  207. /*++
  208. Routine Description:
  209. GC_TODO: Add function description
  210. Arguments:
  211. None
  212. Returns:
  213. GC_TODO: add return values
  214. --*/
  215. {
  216. int Index;
  217. const char *Str[] = {
  218. UTILITY_NAME" "UTILITY_VERSION" - Intel Modify INF File Utility",
  219. " Copyright (C), 1999 - 2008 Intel Corporation",
  220. #if ( defined(UTILITY_BUILD) && defined(UTILITY_VENDOR) )
  221. " Built from "UTILITY_BUILD", project of "UTILITY_VENDOR,
  222. #endif
  223. "",
  224. "Usage:",
  225. " "UTILITY_NAME" SOURCE DEST [PATTERN]",
  226. NULL
  227. };
  228. for (Index = 0; Str[Index] != NULL; Index++) {
  229. fprintf (stdout, "%s\n", Str[Index]);
  230. }
  231. }
  232. int
  233. main (
  234. int argc,
  235. char*argv[]
  236. )
  237. /*++
  238. Routine Description:
  239. GC_TODO: Add function description
  240. Arguments:
  241. argc - GC_TODO: add argument description
  242. ] - GC_TODO: add argument description
  243. Returns:
  244. GC_TODO: add return values
  245. --*/
  246. {
  247. char LineBuffer[256];
  248. FILE *fpin;
  249. FILE *fpout;
  250. if (argc < 3) {
  251. Usage ();
  252. return -1;
  253. }
  254. fpin = fopen (argv[1], "rb");
  255. if (!fpin) {
  256. printf ("Can't open input file!\r\n");
  257. return -1;
  258. }
  259. fpout = fopen (argv[2], "wb");
  260. if (!fpout) {
  261. fclose (fpin);
  262. printf ("Can't create output file!\r\n");
  263. return -1;
  264. }
  265. while (ReadLine (LineBuffer, fpin)) {
  266. ApplyPattern (LineBuffer, argv, argc);
  267. WriteLine (LineBuffer, fpout);
  268. }
  269. fclose (fpin);
  270. fclose (fpout);
  271. return 0;
  272. }