summaryrefslogtreecommitdiff
path: root/Tools/CCode/Source/ModifyInf/ModifyInf.c
blob: 7115c00903b6d5577362635acb43a53cfed5d5c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*++

Copyright (c)  1999-2006 Intel Corporation. All rights reserved
This program and the accompanying materials are licensed and made available 
under the terms and conditions of the BSD License which accompanies this 
distribution.  The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.


Module Name:

  ModifyInf.c

Abstract:

  It is a simple tool to modify some fields in a FV inf file 
  and output a new FV inf file.  

--*/

#include "stdio.h"
#include "string.h"

#define UTILITY_NAME "ModifyInf"
#define UTILITY_MAJOR_VERSION 1
#define UTILITY_MINOR_VERSION 1

//
// Read a line into buffer including '\r\n'
//
int
ReadLine (
  char *LineBuffer,
  FILE *fp
  )
/*++

Routine Description:

  GC_TODO: Add function description

Arguments:

  LineBuffer  - GC_TODO: add argument description
  fp          - GC_TODO: add argument description

Returns:

  GC_TODO: add return values

--*/
{
  int   CharC;
  char  *Line;

  Line = LineBuffer;

  while ((CharC = fgetc (fp)) != EOF) {
    *Line++ = (char) CharC;
    if (CharC == 0x0a) {
      break;
    }
  }

  *Line = 0;

  if (CharC == EOF) {
    return 0;
  } else {
    return 1;
  }

}
//
// Write a line into output file
//
int
WriteLine (
  char *Line,
  FILE *fp
  )
/*++

Routine Description:

  GC_TODO: Add function description

Arguments:

  Line  - GC_TODO: add argument description
  fp    - GC_TODO: add argument description

Returns:

  GC_TODO: add return values

--*/
{
  fwrite (Line, strlen (Line), 1, fp);
  return 0;
}
//
// Apply patterns to a line
// Currently there are 2 patterns to support
// '==' replace a field value with a new value
// '+=' append a string at the end of original line
// '-'  prevent the line from applying any patterns
//      it has the highest priority
//
int
ApplyPattern (
  char *Line,
  char *argv[],
  int  argc
  )
/*++

Routine Description:

  GC_TODO: Add function description

Arguments:

  Line  - GC_TODO: add argument description
  ]     - GC_TODO: add argument description
  argc  - GC_TODO: add argument description

Returns:

  GC_TODO: add return values

--*/
{
  static char Section[256];
  char        PatternBuffer[256];
  char        *Pattern;
  char        *Pattern1;
  char        *Pattern2;
  int         PatternNum;
  char        *Ptr;

  Pattern     = PatternBuffer;

  PatternNum  = argc;

  //
  // For section field
  // record current scope section into static buffer
  //
  Ptr = Line;
  if (*Ptr == '[') {
    while (*Ptr != ']') {
      if (!(*Ptr++)) {
        return -1;
      }
    }

    strcpy (Section, Line);
    Section[Ptr - Line + 1] = 0;
  }
  //
  // Apply each pattern on the line
  //
  while (PatternNum-- > 3) {

    strcpy (Pattern, argv[PatternNum]);

    //
    // For pattern '-'
    // keep it unmodified by other patterns
    //
    if (*Pattern == '-') {
      if (strstr (Line, Pattern + 1)) {
        return 0;
      } else {
        continue;
      }
    }
    //
    // For other patterns
    // get its section at first if it has
    //
    if (*Pattern == '[') {
      if (strncmp (Section, Pattern, strlen (Section))) {
        //
        // This pattern can't be appied for current section
        //
        continue;
      }
      //
      // Strip the section field
      //
      while (*Pattern != ']') {
        if (!(*Pattern++)) {
          return -1;
        }
      }

      Pattern++;
    }
    //
    // Apply patterns
    //
    Pattern1  = strstr (Pattern, "==");
    Pattern2  = strstr (Pattern, "+=");
    if (Pattern1) {
      //
      // For pattern '=='
      // replace the field value with a new string
      //
      if (!strncmp (Line, Pattern, Pattern1 - Pattern)) {
        Pattern1 += 2;
        Ptr = strstr (Line, "=");
        if (!Ptr) {
          return -1;
        }

        while (*(++Ptr) == ' ')
          ;
        *Ptr = 0;
        strcat (Line, Pattern1);
        strcat (Line, "\r\n");
      }
    } else if (Pattern2) {
      //
      // For pattern '+='
      // append a string at end of the original string
      //
      if (!strncmp (Line, Pattern, Pattern2 - Pattern)) {
        Pattern2 += 2;
        Ptr = Line;
        while (*Ptr != 0x0D && *Ptr != 0x0A) {
          Ptr++;
        }

        *Ptr = 0;
        strcat (Line, Pattern2);
        strcat (Line, "\r\n");
      }
    }
  }

  return 0;
}

void 
Version(
  void
  )
/*++

Routine Description:

  Print out version information for Strip.

Arguments:

  None
  
Returns:

  None
  
--*/ 
{
  printf ("%s v%d.%d -EDK Modify fields in FV inf files.\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION);
  printf ("Copyright (c) 2005-2006 Intel Corporation. All rights reserved.\n");
}

void 
Usage(
  void
  )
/*++

Routine Description:

  Print out usage information for Strip.

Arguments:

  None
  
Returns:

  None
  
--*/ 
{
  Version();
  printf ("\nUsage: %s InputFile OutputFile Pattern_String [Pattern_String ¡­]\n\
   Where: \n\
     Pattern_String is of the format (note that the section name must be \n\
     enclosed within square brackets):\n\
         [section]FieldKey<op>Value [(FieldKey<op>Value) ¡­] \n\
     The operator, <op>, must be one of the following: \n\
         '==' replace a field value with a new value \n\
         '+=' append a string at the end of original line \n\
         '-'  prevent the line from applying any patterns \n\
     Example: \n\
         ModifyInf BuildRootFvFvMain.inf BuildRootFvFvMainEXP.inf \\ \n\
               [files]EFI_FILE_NAME+=.Org EFI_NUM_BLOCKS==0x20 \\ \n\
               [options]EFI_FILENAME==FcMainCompact.fv -DpsdSignature.dxe \n", UTILITY_NAME);
}


int
main (
  int argc,
  char*argv[]
  )
/*++

Routine Description:

  GC_TODO: Add function description

Arguments:

  argc  - GC_TODO: add argument description
  ]     - GC_TODO: add argument description

Returns:

  GC_TODO: add return values

--*/
{
  char  LineBuffer[256];
  FILE  *fpin;
  FILE  *fpout;

  if (argc == 1) {
    Usage();
    return -1;
  }
  
  if ((strcmp(argv[1], "-h") == 0) || (strcmp(argv[1], "--help") == 0) ||
      (strcmp(argv[1], "-?") == 0) || (strcmp(argv[1], "/?") == 0)) {
    Usage();
    return 0;
  }
  
  if ((strcmp(argv[1], "-V") == 0) || (strcmp(argv[1], "--version") == 0)) {
    Version();
    return 0;
  }
  
  if (argc < 3) {
    Usage();
    return -1;
  }

  fpin = fopen (argv[1], "rb");
  if (!fpin) {
    printf ("Can't open input file!\r\n");
    return -1;
  }

  fpout = fopen (argv[2], "wb");
  if (!fpout) {
    fclose (fpin);
    printf ("Can't create output file!\r\n");
    return -1;
  }

  while (ReadLine (LineBuffer, fpin)) {
    ApplyPattern (LineBuffer, argv, argc);
    WriteLine (LineBuffer, fpout);
  }

  fclose (fpin);
  fclose (fpout);

  return 0;
}