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