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