]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/TianoTools/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 InputBuffer
[CharsToCopy
- 1] = '\0';
122 // Increment the current file pointer (include the 0x0A)
124 InputFile
->CurrentFilePointer
+= CharsToCopy
+ 1;
127 // Strip any comments
129 CharPtr
= strstr (InputBuffer
, "//");
141 IN MEMORY_FILE
*InputFile
,
148 This function parses a file from the beginning to find a section.
149 The section string may be anywhere within a line.
153 InputFile Memory file image.
154 Section Section to search for
158 FALSE if error or EOF
159 TRUE if section found
163 CHAR8 InputBuffer
[_MAX_PATH
];
167 // Verify input is not NULL
169 assert (InputFile
->FileImage
);
170 assert (InputFile
->Eof
);
171 assert (InputFile
->CurrentFilePointer
);
175 // Rewind to beginning of file
177 InputFile
->CurrentFilePointer
= InputFile
->FileImage
;
180 // Read lines until the section is found
182 while (InputFile
->CurrentFilePointer
< InputFile
->Eof
) {
186 ReadLine (InputFile
, InputBuffer
, _MAX_PATH
);
189 // Check if the section is found
191 CurrentToken
= strstr (InputBuffer
, Section
);
192 if (CurrentToken
!= NULL
) {
202 IN MEMORY_FILE
*InputFile
,
212 Finds a token value given the section and token to search for.
216 InputFile Memory file image.
217 Section The section to search for, a string within [].
218 Token The token to search for, e.g. EFI_PEIM_RECOVERY, followed by an = in the INF file.
219 Instance The instance of the token to search for. Zero is the first instance.
220 Value The string that holds the value following the =. Must be _MAX_PATH in size.
224 EFI_SUCCESS Value found.
225 EFI_ABORTED Format error detected in INF file.
226 EFI_INVALID_PARAMETER Input argument was null.
227 EFI_LOAD_ERROR Error reading from the file.
228 EFI_NOT_FOUND Section/Token/Value not found.
232 CHAR8 InputBuffer
[_MAX_PATH
];
239 // Check input parameters
241 if (InputFile
->FileImage
== NULL
||
242 InputFile
->Eof
== NULL
||
243 InputFile
->CurrentFilePointer
== NULL
||
245 strlen (Section
) == 0 ||
247 strlen (Token
) == 0 ||
250 return EFI_INVALID_PARAMETER
;
253 // Initialize error codes
259 // Initialize our instance counter for the search token
263 if (FindSection (InputFile
, Section
)) {
265 // Found the desired section, find and read the desired token
269 // Read a line from the file
271 if (ReadLine (InputFile
, InputBuffer
, _MAX_PATH
) == NULL
) {
273 // Error reading from input file
279 // Get the first non-whitespace string
281 CurrentToken
= strtok (InputBuffer
, " \t\n");
282 if (CurrentToken
== NULL
) {
284 // Whitespace line found (or comment) so continue
286 CurrentToken
= InputBuffer
;
290 // Make sure we have not reached the end of the current section
292 if (CurrentToken
[0] == '[') {
296 // Compare the current token with the desired token
298 if (strcmp (CurrentToken
, Token
) == 0) {
303 // Check if it is the correct instance
305 if (Instance
== Occurrance
) {
307 // Copy the contents following the =
309 CurrentToken
= strtok (NULL
, "= \t\n");
310 if (CurrentToken
== NULL
) {
312 // Nothing found, parsing error
317 // Copy the current token to the output value
319 strcpy (Value
, CurrentToken
);
324 // Increment the occurrance found
332 InputFile
->CurrentFilePointer
< InputFile
->Eof
&&
333 CurrentToken
[0] != '[' &&
334 Occurrance
<= Instance
338 // Distinguish between read errors and INF file format errors.
341 return EFI_LOAD_ERROR
;
348 return EFI_NOT_FOUND
;
353 IN CHAR8
*AsciiGuidBuffer
,
354 OUT EFI_GUID
*GuidBuffer
360 Converts a string to an EFI_GUID. The string must be in the
361 xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx format.
365 AsciiGuidBuffer - pointer to ascii string
366 GuidBuffer - pointer to destination Guid
370 EFI_ABORTED Could not convert the string
371 EFI_SUCCESS The string was successfully converted
372 EFI_INVALID_PARAMETER Input parameter is invalid.
382 if (AsciiGuidBuffer
== NULL
|| GuidBuffer
== NULL
) {
383 return EFI_INVALID_PARAMETER
;
386 // Scan the guid string into the buffer
390 "%08x-%04x-%04x-%02x%02x-%02hx%02hx%02hx%02hx%02hx%02hx",
405 // Verify the correct number of items were scanned.
408 printf ("ERROR: Malformed GUID \"%s\".\n\n", AsciiGuidBuffer
);
412 // Copy the data into our GUID.
414 GuidBuffer
->Data1
= (UINT32
) Data1
;
415 GuidBuffer
->Data2
= (UINT16
) Data2
;
416 GuidBuffer
->Data3
= (UINT16
) Data3
;
417 GuidBuffer
->Data4
[0] = (UINT8
) Data4
[0];
418 GuidBuffer
->Data4
[1] = (UINT8
) Data4
[1];
419 GuidBuffer
->Data4
[2] = (UINT8
) Data4
[2];
420 GuidBuffer
->Data4
[3] = (UINT8
) Data4
[3];
421 GuidBuffer
->Data4
[4] = (UINT8
) Data4
[4];
422 GuidBuffer
->Data4
[5] = (UINT8
) Data4
[5];
423 GuidBuffer
->Data4
[6] = (UINT8
) Data4
[6];
424 GuidBuffer
->Data4
[7] = (UINT8
) Data4
[7];
430 AsciiStringToUint64 (
431 IN CONST CHAR8
*AsciiString
,
433 OUT UINT64
*ReturnValue
439 Converts a null terminated ascii string that represents a number into a
440 UINT64 value. A hex number may be preceeded by a 0x, but may not be
441 succeeded by an h. A number without 0x or 0X is considered to be base 10
442 unless the IsHex input is true.
446 AsciiString The string to convert.
447 IsHex Force the string to be treated as a hex number.
448 ReturnValue The return value.
452 EFI_SUCCESS Number successfully converted.
453 EFI_ABORTED Invalid character encountered.
462 // Initialize the result
467 // Add each character to the result
469 if (IsHex
|| (AsciiString
[0] == '0' && (AsciiString
[1] == 'x' || AsciiString
[1] == 'X'))) {
471 // Verify string is a hex number
473 for (Index
= 2; Index
< strlen (AsciiString
); Index
++) {
474 if (isxdigit (AsciiString
[Index
]) == 0) {
479 // Convert the hex string.
481 for (Index
= 2; AsciiString
[Index
] != '\0'; Index
++) {
482 CurrentChar
= AsciiString
[Index
];
484 if (CurrentChar
>= '0' && CurrentChar
<= '9') {
485 HexNumber
+= CurrentChar
- '0';
486 } else if (CurrentChar
>= 'a' && CurrentChar
<= 'f') {
487 HexNumber
+= CurrentChar
- 'a' + 10;
488 } else if (CurrentChar
>= 'A' && CurrentChar
<= 'F') {
489 HexNumber
+= CurrentChar
- 'A' + 10;
492 // Unrecognized character
498 *ReturnValue
= HexNumber
;
501 // Verify string is a number
503 for (Index
= 0; Index
< strlen (AsciiString
); Index
++) {
504 if (isdigit (AsciiString
[Index
]) == 0) {
509 *ReturnValue
= atol (AsciiString
);
518 IN OUT CHAR8
*InputBuffer
524 This function reads a line, stripping any comments.
525 // BUGBUG: This is obsolete once genmake goes away...
529 InputFile Stream pointer.
530 InputBuffer Buffer to read into, must be _MAX_PATH size.
535 InputBuffer otherwise
542 // Verify input parameters are not null
545 assert (InputBuffer
);
550 if (fgets (InputBuffer
, _MAX_PATH
, InputFile
) == NULL
) {
554 // Strip any comments
556 CharPtr
= strstr (InputBuffer
, "//");
561 CharPtr
= strstr (InputBuffer
, "#");
572 FindSectionInStream (
580 This function parses a stream file from the beginning to find a section.
581 The section string may be anywhere within a line.
582 // BUGBUG: This is obsolete once genmake goes away...
586 InputFile Stream pointer.
587 Section Section to search for
591 FALSE if error or EOF
592 TRUE if section found
596 CHAR8 InputBuffer
[_MAX_PATH
];
600 // Verify input is not NULL
606 // Rewind to beginning of file
608 if (fseek (InputFile
, 0, SEEK_SET
) != 0) {
612 // Read lines until the section is found
614 while (feof (InputFile
) == 0) {
618 ReadLineInStream (InputFile
, InputBuffer
);
621 // Check if the section is found
623 CurrentToken
= strstr (InputBuffer
, Section
);
624 if (CurrentToken
!= NULL
) {