]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/CCode/Source/Common/ParseInf.c
3 Copyright (c) 2004, 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
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.
18 This contains some useful functions for parsing INF files.
34 IN MEMORY_FILE
*InputFile
,
35 IN OUT CHAR8
*InputBuffer
,
42 This function reads a line, stripping any comments.
43 The function reads a string from the input stream argument and stores it in
44 the input string. ReadLine reads characters from the current file position
45 to and including the first newline character, to the end of the stream, or
46 until the number of characters read is equal to MaxLength - 1, whichever
47 comes first. The newline character, if read, is replaced with a \0.
51 InputFile Memory file image.
52 InputBuffer Buffer to read into, must be _MAX_PATH size.
53 MaxLength The maximum size of the input buffer.
67 // Verify input parameters are not null
70 assert (InputFile
->FileImage
);
71 assert (InputFile
->Eof
);
72 assert (InputFile
->CurrentFilePointer
);
75 // Check for end of file condition
77 if (InputFile
->CurrentFilePointer
>= InputFile
->Eof
) {
81 // Find the next newline char
83 EndOfLine
= strchr (InputFile
->CurrentFilePointer
, '\n');
86 // Determine the number of characters to copy.
90 // If no newline found, copy to the end of the file.
92 CharsToCopy
= InputFile
->Eof
- InputFile
->CurrentFilePointer
;
93 } else if (EndOfLine
>= InputFile
->Eof
) {
95 // If the newline found was beyond the end of file, copy to the eof.
97 CharsToCopy
= InputFile
->Eof
- InputFile
->CurrentFilePointer
;
100 // Newline found in the file.
102 CharsToCopy
= EndOfLine
- InputFile
->CurrentFilePointer
;
105 // If the end of line is too big for the current buffer, set it to the max
106 // size of the buffer (leaving room for the \0.
108 if (CharsToCopy
> MaxLength
- 1) {
109 CharsToCopy
= MaxLength
- 1;
114 memcpy (InputBuffer
, InputFile
->CurrentFilePointer
, CharsToCopy
);
117 // Add the null termination over the 0x0D
119 if (InputBuffer
[CharsToCopy
- 1] == '\r') {
121 InputBuffer
[CharsToCopy
- 1] = '\0';
125 InputBuffer
[CharsToCopy
] = '\0';
130 // Increment the current file pointer (include the 0x0A)
132 InputFile
->CurrentFilePointer
+= CharsToCopy
+ 1;
135 // Strip any comments
137 CharPtr
= strstr (InputBuffer
, "//");
149 IN MEMORY_FILE
*InputFile
,
156 This function parses a file from the beginning to find a section.
157 The section string may be anywhere within a line.
161 InputFile Memory file image.
162 Section Section to search for
166 FALSE if error or EOF
167 TRUE if section found
171 CHAR8 InputBuffer
[_MAX_PATH
];
175 // Verify input is not NULL
177 assert (InputFile
->FileImage
);
178 assert (InputFile
->Eof
);
179 assert (InputFile
->CurrentFilePointer
);
183 // Rewind to beginning of file
185 InputFile
->CurrentFilePointer
= InputFile
->FileImage
;
188 // Read lines until the section is found
190 while (InputFile
->CurrentFilePointer
< InputFile
->Eof
) {
194 ReadLine (InputFile
, InputBuffer
, _MAX_PATH
);
197 // Check if the section is found
199 CurrentToken
= strstr (InputBuffer
, Section
);
200 if (CurrentToken
!= NULL
) {
210 IN MEMORY_FILE
*InputFile
,
220 Finds a token value given the section and token to search for.
224 InputFile Memory file image.
225 Section The section to search for, a string within [].
226 Token The token to search for, e.g. EFI_PEIM_RECOVERY, followed by an = in the INF file.
227 Instance The instance of the token to search for. Zero is the first instance.
228 Value The string that holds the value following the =. Must be _MAX_PATH in size.
232 EFI_SUCCESS Value found.
233 EFI_ABORTED Format error detected in INF file.
234 EFI_INVALID_PARAMETER Input argument was null.
235 EFI_LOAD_ERROR Error reading from the file.
236 EFI_NOT_FOUND Section/Token/Value not found.
240 CHAR8 InputBuffer
[_MAX_PATH
];
247 // Check input parameters
249 if (InputFile
->FileImage
== NULL
||
250 InputFile
->Eof
== NULL
||
251 InputFile
->CurrentFilePointer
== NULL
||
253 strlen (Section
) == 0 ||
255 strlen (Token
) == 0 ||
258 return EFI_INVALID_PARAMETER
;
261 // Initialize error codes
267 // Initialize our instance counter for the search token
271 if (FindSection (InputFile
, Section
)) {
273 // Found the desired section, find and read the desired token
277 // Read a line from the file
279 if (ReadLine (InputFile
, InputBuffer
, _MAX_PATH
) == NULL
) {
281 // Error reading from input file
287 // Get the first non-whitespace string
289 CurrentToken
= strtok (InputBuffer
, " \t\n");
290 if (CurrentToken
== NULL
) {
292 // Whitespace line found (or comment) so continue
294 CurrentToken
= InputBuffer
;
298 // Make sure we have not reached the end of the current section
300 if (CurrentToken
[0] == '[') {
304 // Compare the current token with the desired token
306 if (strcmp (CurrentToken
, Token
) == 0) {
311 // Check if it is the correct instance
313 if (Instance
== Occurrance
) {
315 // Copy the contents following the =
317 CurrentToken
= strtok (NULL
, "= \t\n");
318 if (CurrentToken
== NULL
) {
320 // Nothing found, parsing error
325 // Copy the current token to the output value
327 strcpy (Value
, CurrentToken
);
332 // Increment the occurrance found
340 InputFile
->CurrentFilePointer
< InputFile
->Eof
&&
341 CurrentToken
[0] != '[' &&
342 Occurrance
<= Instance
346 // Distinguish between read errors and INF file format errors.
349 return EFI_LOAD_ERROR
;
356 return EFI_NOT_FOUND
;
361 IN CHAR8
*AsciiGuidBuffer
,
362 OUT EFI_GUID
*GuidBuffer
368 Converts a string to an EFI_GUID. The string must be in the
369 xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx format.
373 AsciiGuidBuffer - pointer to ascii string
374 GuidBuffer - pointer to destination Guid
378 EFI_ABORTED Could not convert the string
379 EFI_SUCCESS The string was successfully converted
380 EFI_INVALID_PARAMETER Input parameter is invalid.
390 if (AsciiGuidBuffer
== NULL
|| GuidBuffer
== NULL
) {
391 return EFI_INVALID_PARAMETER
;
394 // Scan the guid string into the buffer
398 "%08x-%04x-%04x-%02x%02x-%02hx%02hx%02hx%02hx%02hx%02hx",
413 // Verify the correct number of items were scanned.
416 printf ("ERROR: Malformed GUID \"%s\".\n\n", AsciiGuidBuffer
);
420 // Copy the data into our GUID.
422 GuidBuffer
->Data1
= (UINT32
) Data1
;
423 GuidBuffer
->Data2
= (UINT16
) Data2
;
424 GuidBuffer
->Data3
= (UINT16
) Data3
;
425 GuidBuffer
->Data4
[0] = (UINT8
) Data4
[0];
426 GuidBuffer
->Data4
[1] = (UINT8
) Data4
[1];
427 GuidBuffer
->Data4
[2] = (UINT8
) Data4
[2];
428 GuidBuffer
->Data4
[3] = (UINT8
) Data4
[3];
429 GuidBuffer
->Data4
[4] = (UINT8
) Data4
[4];
430 GuidBuffer
->Data4
[5] = (UINT8
) Data4
[5];
431 GuidBuffer
->Data4
[6] = (UINT8
) Data4
[6];
432 GuidBuffer
->Data4
[7] = (UINT8
) Data4
[7];
438 AsciiStringToUint64 (
439 IN CONST CHAR8
*AsciiString
,
441 OUT UINT64
*ReturnValue
447 Converts a null terminated ascii string that represents a number into a
448 UINT64 value. A hex number may be preceeded by a 0x, but may not be
449 succeeded by an h. A number without 0x or 0X is considered to be base 10
450 unless the IsHex input is true.
454 AsciiString The string to convert.
455 IsHex Force the string to be treated as a hex number.
456 ReturnValue The return value.
460 EFI_SUCCESS Number successfully converted.
461 EFI_ABORTED Invalid character encountered.
470 // Initialize the result
475 // Add each character to the result
477 if (IsHex
|| (AsciiString
[0] == '0' && (AsciiString
[1] == 'x' || AsciiString
[1] == 'X'))) {
479 // Verify string is a hex number
481 for (Index
= 2; Index
< strlen (AsciiString
); Index
++) {
482 if (isxdigit (AsciiString
[Index
]) == 0) {
487 // Convert the hex string.
489 for (Index
= 2; AsciiString
[Index
] != '\0'; Index
++) {
490 CurrentChar
= AsciiString
[Index
];
492 if (CurrentChar
>= '0' && CurrentChar
<= '9') {
493 HexNumber
+= CurrentChar
- '0';
494 } else if (CurrentChar
>= 'a' && CurrentChar
<= 'f') {
495 HexNumber
+= CurrentChar
- 'a' + 10;
496 } else if (CurrentChar
>= 'A' && CurrentChar
<= 'F') {
497 HexNumber
+= CurrentChar
- 'A' + 10;
500 // Unrecognized character
506 *ReturnValue
= HexNumber
;
509 // Verify string is a number
511 for (Index
= 0; Index
< strlen (AsciiString
); Index
++) {
512 if (isdigit (AsciiString
[Index
]) == 0) {
517 *ReturnValue
= atol (AsciiString
);
526 IN OUT CHAR8
*InputBuffer
532 This function reads a line, stripping any comments.
533 // BUGBUG: This is obsolete once genmake goes away...
537 InputFile Stream pointer.
538 InputBuffer Buffer to read into, must be _MAX_PATH size.
543 InputBuffer otherwise
550 // Verify input parameters are not null
553 assert (InputBuffer
);
558 if (fgets (InputBuffer
, _MAX_PATH
, InputFile
) == NULL
) {
562 // Strip any comments
564 CharPtr
= strstr (InputBuffer
, "//");
569 CharPtr
= strstr (InputBuffer
, "#");
580 FindSectionInStream (
588 This function parses a stream file from the beginning to find a section.
589 The section string may be anywhere within a line.
590 // BUGBUG: This is obsolete once genmake goes away...
594 InputFile Stream pointer.
595 Section Section to search for
599 FALSE if error or EOF
600 TRUE if section found
604 CHAR8 InputBuffer
[_MAX_PATH
];
608 // Verify input is not NULL
614 // Rewind to beginning of file
616 if (fseek (InputFile
, 0, SEEK_SET
) != 0) {
620 // Read lines until the section is found
622 while (feof (InputFile
) == 0) {
626 ReadLineInStream (InputFile
, InputBuffer
);
629 // Check if the section is found
631 CurrentToken
= strstr (InputBuffer
, Section
);
632 if (CurrentToken
!= NULL
) {