]> git.proxmox.com Git - mirror_edk2.git/blame - EdkCompatibilityPkg/Sample/Tools/Source/ModifyInf/ModifyInf.c
Sync all bug fixes between EDK1.04 and EDK1.06 into EdkCompatibilityPkg.
[mirror_edk2.git] / EdkCompatibilityPkg / Sample / Tools / Source / ModifyInf / ModifyInf.c
CommitLineData
3eb9473e 1/*++\r
2\r
3e99020d 3Copyright (c) 1999 - 2010, Intel Corporation. All rights reserved.<BR>\r
4b1e1121 4This program and the accompanying materials \r
3eb9473e 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
3e99020d
LG
26#define UTILITY_NAME "ModifyInf"\r
27#define UTILITY_VERSION "v1.0"\r
3eb9473e 28//\r
29// Read a line into buffer including '\r\n'\r
30//\r
31int\r
32ReadLine (\r
33 char *LineBuffer,\r
34 FILE *fp\r
35 )\r
36/*++\r
37\r
38Routine Description:\r
39\r
40 GC_TODO: Add function description\r
41\r
42Arguments:\r
43\r
44 LineBuffer - GC_TODO: add argument description\r
45 fp - GC_TODO: add argument description\r
46\r
47Returns:\r
48\r
49 GC_TODO: add return values\r
50\r
51--*/\r
52{\r
53 int CharC;\r
54 char *Line;\r
55\r
56 Line = LineBuffer;\r
57\r
58 while ((CharC = fgetc (fp)) != EOF) {\r
59 *Line++ = (char) CharC;\r
60 if (CharC == 0x0a) {\r
61 break;\r
62 }\r
63 }\r
64\r
65 *Line = 0;\r
66\r
67 if (CharC == EOF) {\r
68 return 0;\r
69 } else {\r
70 return 1;\r
71 }\r
72\r
73}\r
74//\r
75// Write a line into output file\r
76//\r
77int\r
78WriteLine (\r
79 char *Line,\r
80 FILE *fp\r
81 )\r
82/*++\r
83\r
84Routine Description:\r
85\r
86 GC_TODO: Add function description\r
87\r
88Arguments:\r
89\r
90 Line - GC_TODO: add argument description\r
91 fp - GC_TODO: add argument description\r
92\r
93Returns:\r
94\r
95 GC_TODO: add return values\r
96\r
97--*/\r
98{\r
99 fwrite (Line, strlen (Line), 1, fp);\r
100 return 0;\r
101}\r
102//\r
103// Apply patterns to a line\r
104// Currently there are 2 patterns to support\r
105// '==' replace a field value with a new value\r
106// '+=' append a string at the end of original line\r
107// '-' prevent the line from applying any patterns\r
108// it has the highest priority\r
109//\r
110int\r
111ApplyPattern (\r
112 char *Line,\r
113 char *argv[],\r
114 int argc\r
115 )\r
116/*++\r
117\r
118Routine Description:\r
119\r
120 GC_TODO: Add function description\r
121\r
122Arguments:\r
123\r
124 Line - GC_TODO: add argument description\r
125 ] - GC_TODO: add argument description\r
126 argc - GC_TODO: add argument description\r
127\r
128Returns:\r
129\r
130 GC_TODO: add return values\r
131\r
132--*/\r
133{\r
134 static char Section[256];\r
135 int SectionLength;\r
136 char PatternBuffer[256];\r
137 char *Pattern;\r
138 char *Pattern1;\r
139 char *Pattern2;\r
140 int PatternNum;\r
141 char *Ptr;\r
142\r
143 Pattern = PatternBuffer;\r
144\r
145 PatternNum = argc;\r
146\r
147 //\r
148 // For section field\r
149 // record current scope section into static buffer\r
150 //\r
151 Ptr = Line;\r
152 if (*Ptr == '[') {\r
153 while (*Ptr != ']') {\r
154 if (!(*Ptr++)) {\r
155 return -1;\r
156 }\r
157 }\r
158 SectionLength = Ptr - Line + 1;\r
159 SectionLength = SectionLength > 255 ? 255 : SectionLength;\r
160 strncpy (Section, Line, SectionLength);\r
161 Section[SectionLength] = 0;\r
162 }\r
163 //\r
164 // Apply each pattern on the line\r
165 //\r
166 while (PatternNum-- > 3) {\r
167\r
168 strcpy (Pattern, argv[PatternNum]);\r
169\r
170 //\r
171 // For pattern '-'\r
172 // keep it unmodified by other patterns\r
173 //\r
174 if (*Pattern == '-') {\r
175 if (strstr (Line, Pattern + 1)) {\r
176 return 0;\r
177 } else {\r
178 continue;\r
179 }\r
180 }\r
181 //\r
182 // For other patterns\r
183 // get its section at first if it has\r
184 //\r
185 if (*Pattern == '[') {\r
186 if (strncmp (Section, Pattern, strlen (Section))) {\r
187 //\r
188 // This pattern can't be appied for current section\r
189 //\r
190 continue;\r
191 }\r
192 //\r
193 // Strip the section field\r
194 //\r
195 while (*Pattern != ']') {\r
196 if (!(*Pattern++)) {\r
197 return -1;\r
198 }\r
199 }\r
200\r
201 Pattern++;\r
202 }\r
203 //\r
204 // Apply patterns\r
205 //\r
206 Pattern1 = strstr (Pattern, "==");\r
207 Pattern2 = strstr (Pattern, "+=");\r
208 if (Pattern1) {\r
209 //\r
210 // For pattern '=='\r
211 // replace the field value with a new string\r
212 //\r
213 if (!strncmp (Line, Pattern, Pattern1 - Pattern)) {\r
214 Pattern1 += 2;\r
215 Ptr = strstr (Line, "=");\r
216 if (!Ptr) {\r
217 return -1;\r
218 }\r
219\r
220 while (*(++Ptr) == ' ')\r
221 ;\r
222 *Ptr = 0;\r
223 strcat (Line, Pattern1);\r
224 strcat (Line, "\r\n");\r
225 }\r
226 } else if (Pattern2) {\r
227 //\r
228 // For pattern '+='\r
229 // append a string at end of the original string\r
230 //\r
231 if (!strncmp (Line, Pattern, Pattern2 - Pattern)) {\r
232 Pattern2 += 2;\r
233 Ptr = Line;\r
234 while (*Ptr != 0x0D && *Ptr != 0x0A) {\r
235 Ptr++;\r
236 }\r
237\r
238 *Ptr = 0;\r
239 strcat (Line, Pattern2);\r
240 strcat (Line, "\r\n");\r
241 }\r
242 }\r
243 }\r
244\r
245 return 0;\r
246}\r
247\r
248void\r
249Usage (\r
250 void\r
251 )\r
252/*++\r
253\r
254Routine Description:\r
255\r
256 GC_TODO: Add function description\r
257\r
258Arguments:\r
259\r
260 None\r
261\r
262Returns:\r
263\r
264 GC_TODO: add return values\r
265\r
266--*/\r
267{\r
3e99020d
LG
268 int Index;\r
269 const char *Str[] = {\r
270 UTILITY_NAME" "UTILITY_VERSION" - Intel Modify INF File Utility",\r
271 " Copyright (C), 1999 - 2008 Intel Corporation",\r
272 \r
273#if ( defined(UTILITY_BUILD) && defined(UTILITY_VENDOR) )\r
274 " Built from "UTILITY_BUILD", project of "UTILITY_VENDOR,\r
275#endif\r
276 "",\r
277 "Usage:",\r
278 " "UTILITY_NAME" SOURCE DEST [PATTERN]",\r
279 NULL\r
280 };\r
281 for (Index = 0; Str[Index] != NULL; Index++) {\r
282 fprintf (stdout, "%s\n", Str[Index]);\r
283 } \r
3eb9473e 284}\r
285\r
286int\r
287main (\r
288 int argc,\r
289 char*argv[]\r
290 )\r
291/*++\r
292\r
293Routine Description:\r
294\r
295 GC_TODO: Add function description\r
296\r
297Arguments:\r
298\r
299 argc - GC_TODO: add argument description\r
300 ] - GC_TODO: add argument description\r
301\r
302Returns:\r
303\r
304 GC_TODO: add return values\r
305\r
306--*/\r
307{\r
308 char LineBuffer[256];\r
309 FILE *fpin;\r
310 FILE *fpout;\r
311\r
312 if (argc < 3) {\r
313 Usage ();\r
314 return -1;\r
315 }\r
316\r
317 fpin = fopen (argv[1], "rb");\r
318 if (!fpin) {\r
319 printf ("Can't open input file!\r\n");\r
320 return -1;\r
321 }\r
322\r
323 fpout = fopen (argv[2], "wb");\r
324 if (!fpout) {\r
325 fclose (fpin);\r
326 printf ("Can't create output file!\r\n");\r
327 return -1;\r
328 }\r
329\r
330 while (ReadLine (LineBuffer, fpin)) {\r
331 ApplyPattern (LineBuffer, argv, argc);\r
332 WriteLine (LineBuffer, fpout);\r
333 }\r
334\r
335 fclose (fpin);\r
336 fclose (fpout);\r
337\r
338 return 0;\r
339}\r