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