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