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