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