]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/TianoTools/FlashMap/Symbols.c
471128a6b6abbc13576a95947d869e2b4b433106
3 Copyright (c) 2004 Intel Corporation. All rights reserved
4 This software and associated documentation (if any) is furnished
5 under a license and may only be used or copied in accordance
6 with the terms of the license. Except as permitted by such
7 license, no part of this software or documentation may be
8 reproduced, stored in a retrieval system, or transmitted in any
9 form or by any means without the express written consent of
19 Class-like implementation for a symbol table.
23 // GC_TODO: fix comment to set correct module name: Symbols.c
33 #include "EfiUtilityMsgs.h"
36 #define MAX_LINE_LEN 512
39 // Linked list to keep track of all symbols
41 typedef struct _SYMBOL
{
62 static SYMBOL
*mSymbolTable
= NULL
;
72 GC_TODO: Add function description
80 GC_TODO: add return values
95 GC_TODO: Add function description
103 GC_TODO: add return values
107 mSymbolTable
= FreeSymbols (mSymbolTable
);
118 Look up a symbol in our symbol table.
126 Pointer to the value of the symbol if found
127 NULL if the symbol is not found
130 // GC_TODO: SymbolName - add argument and description to function comment
134 // Walk the symbol table
136 Symbol
= mSymbolTable
;
138 if (stricmp (SymbolName
, Symbol
->Name
) == 0) {
139 return Symbol
->Value
;
142 Symbol
= Symbol
->Next
;
158 Add a symbol name/value to the symbol table
162 Name - name of symbol to add
163 Value - value of symbol to add
164 Mode - currrently unused
168 Length of symbol added.
171 If Value == NULL, then this routine will assume that the Name field
172 looks something like "MySymName = MySymValue", and will try to parse
173 it that way and add the symbol name/pair from the string.
190 // If value pointer is null, then they passed us a line something like:
191 // varname = value, or simply var =
195 while (*Name
&& isspace (*Name
)) {
203 // Find the end of the name. Either space or a '='.
205 for (Value
= Name
; *Value
&& !isspace (*Value
) && (*Value
!= '='); Value
++)
214 while (*Value
&& (*Value
!= '=')) {
222 // Now truncate the name
226 // Skip over the = and then any spaces
229 while (*Value
&& isspace (*Value
)) {
234 // Find end of string, checking for quoted string
236 if (*Value
== '\"') {
238 for (Cptr
= Value
; *Cptr
&& *Cptr
!= '\"'; Cptr
++)
241 for (Cptr
= Value
; *Cptr
&& !isspace (*Cptr
); Cptr
++)
245 // Null terminate the value string
250 Len
= (int) (Cptr
- Start
);
253 // We now have a symbol name and a value. Look for an existing variable
256 Symbol
= mSymbolTable
;
259 // Check for symbol name match
261 if (stricmp (Name
, Symbol
->Name
) == 0) {
262 _free (Symbol
->Value
);
263 Symbol
->Value
= (char *) _malloc (strlen (Value
) + 1);
264 if (Symbol
->Value
== NULL
) {
265 Error (NULL
, 0, 0, NULL
, "failed to allocate memory");
269 strcpy (Symbol
->Value
, Value
);
271 // If value == "NULL", then make it a 0-length string
273 if (stricmp (Symbol
->Value
, "NULL") == 0) {
274 Symbol
->Value
[0] = 0;
280 Symbol
= Symbol
->Next
;
283 // Does not exist, create a new one
285 NewSymbol
= (SYMBOL
*) _malloc (sizeof (SYMBOL
));
286 if (NewSymbol
== NULL
) {
287 Error (NULL
, 0, 0, NULL
, "memory allocation failure");
291 memset ((char *) NewSymbol
, 0, sizeof (SYMBOL
));
292 NewSymbol
->Name
= (char *) _malloc (strlen (Name
) + 1);
293 if (NewSymbol
->Name
== NULL
) {
294 Error (NULL
, 0, 0, NULL
, "memory allocation failure");
299 NewSymbol
->Value
= (char *) _malloc (strlen (Value
) + 1);
300 if (NewSymbol
->Value
== NULL
) {
301 Error (NULL
, 0, 0, NULL
, "memory allocation failure");
302 _free (NewSymbol
->Name
);
307 strcpy (NewSymbol
->Name
, Name
);
308 strcpy (NewSymbol
->Value
, Value
);
310 // Remove trailing spaces
312 Cptr
= NewSymbol
->Value
+ strlen (NewSymbol
->Value
) - 1;
313 while (Cptr
> NewSymbol
->Value
) {
314 if (isspace (*Cptr
)) {
322 // Add it to the head of the list.
324 NewSymbol
->Next
= mSymbolTable
;
325 mSymbolTable
= NewSymbol
;
327 // If value == "NULL", then make it a 0-length string
329 if (stricmp (NewSymbol
->Value
, "NULL") == 0) {
330 NewSymbol
->Value
[0] = 0;
333 // Restore the terminator we inserted if they passed in var=value
335 if (SaveCptr
!= NULL
) {
338 _free (NewSymbol
->Value
);
339 _free (NewSymbol
->Name
);
354 Remove a symbol name/value from the symbol table
358 Name - name of symbol to remove
359 SymbolType - type of symbol to remove
363 STATUS_SUCCESS - matching symbol found and removed
364 STATUS_ERROR - matching symbol not found in symbol table
373 Symbol
= mSymbolTable
;
375 // Walk the linked list of symbols in the symbol table looking
376 // for a match of both symbol name and type.
379 if ((stricmp (Name
, Symbol
->Name
) == 0) && (Symbol
->Type
& SymbolType
)) {
381 // If the symbol has a value associated with it, free the memory
382 // allocated for the value.
383 // Then free the memory allocated for the symbols string name.
386 _free (Symbol
->Value
);
389 _free (Symbol
->Name
);
391 // Link the previous symbol to the next symbol to effectively
392 // remove this symbol from the linked list.
395 PrevSymbol
->Next
= Symbol
->Next
;
397 mSymbolTable
= Symbol
->Next
;
401 return STATUS_SUCCESS
;
405 Symbol
= Symbol
->Next
;
408 return STATUS_WARNING
;
420 GC_TODO: Add function description
424 Syms - GC_TODO: add argument description
428 GC_TODO: add return values
434 if (Syms
->Name
!= NULL
) {
438 if (Syms
->Value
!= NULL
) {
461 Given a line of text, replace all variables of format $(NAME) with values
462 from our symbol table.
466 SourceLine - input line of text to do symbol replacements on
467 DestLine - on output, SourceLine with symbols replaced
468 LineLen - length of DestLine, so we don't exceed its allocated length
472 STATUS_SUCCESS - no problems encountered
473 STATUS_WARNING - missing closing parenthesis on a symbol reference in SourceLine
474 STATUS_ERROR - memory allocation failure
478 static int NestDepth
= 0;
491 Status
= STATUS_SUCCESS
;
492 LocalDestLine
= (char *) _malloc (LineLen
);
493 if (LocalDestLine
== NULL
) {
494 Error (__FILE__
, __LINE__
, 0, "memory allocation failed", NULL
);
498 FromPtr
= SourceLine
;
499 ToPtr
= LocalDestLine
;
501 // Walk the entire line, replacing $(MACRO_NAME).
503 LocalLineLen
= LineLen
;
505 while (*FromPtr
&& (LocalLineLen
> 0)) {
506 if ((*FromPtr
== '$') && (*(FromPtr
+ 1) == '(')) {
508 // Save the start in case it's undefined, in which case we copy it as-is.
513 // Macro expansion time. Find the end (no spaces allowed)
516 for (Cptr
= FromPtr
; *Cptr
&& (*Cptr
!= ')'); Cptr
++)
520 // Truncate the string at the closing parenthesis for ease-of-use.
521 // Then copy the string directly to the destination line in case we don't find
522 // a definition for it.
525 strcpy (ToPtr
, SaveStart
);
526 if ((value
= GetSymbolValue (FromPtr
)) != NULL
) {
527 strcpy (ToPtr
, value
);
528 LocalLineLen
-= strlen (value
);
529 ToPtr
+= strlen (value
);
536 // Restore closing parenthesis, and advance to next character
539 FromPtr
= SaveStart
+ 1;
545 Error (NULL
, 0, 0, SourceLine
, "missing closing parenthesis on macro");
546 strcpy (ToPtr
, FromPtr
);
547 Status
= STATUS_WARNING
;
563 // If we expanded at least one string successfully, then make a recursive call to try again.
565 if ((ExpandedCount
!= 0) && (Status
== STATUS_SUCCESS
) && (NestDepth
< 10)) {
566 Status
= ExpandMacros (LocalDestLine
, DestLine
, LineLen
);
567 _free (LocalDestLine
);
573 if (Status
!= STATUS_ERROR
) {
574 strcpy (DestLine
, LocalDestLine
);
578 _free (LocalDestLine
);
583 SymbolsFileStringsReplace (
591 Given input and output file names, read in the input file, replace variable
592 references of format $(NAME) with appropriate values from our symbol table,
593 and write the result out to the output file.
597 InFileName - name of input text file to replace variable references
598 OutFileName - name of output text file to write results to
602 STATUS_SUCCESS - no problems encountered
603 STATUS_ERROR - failed to open input or output file
610 char Line
[MAX_LINE_LEN
];
611 char OutLine
[MAX_LINE_LEN
];
613 Status
= STATUS_ERROR
;
615 // Open input and output files
619 if ((InFptr
= fopen (InFileName
, "r")) == NULL
) {
620 Error (NULL
, 0, 0, InFileName
, "failed to open input file for reading");
624 if ((OutFptr
= fopen (OutFileName
, "w")) == NULL
) {
625 Error (NULL
, 0, 0, OutFileName
, "failed to open output file for writing");
629 // Read lines from input file until done
631 while (fgets (Line
, sizeof (Line
), InFptr
) != NULL
) {
632 ExpandMacros (Line
, OutLine
, sizeof (OutLine
));
633 fprintf (OutFptr
, OutLine
);
636 Status
= STATUS_SUCCESS
;
638 if (InFptr
!= NULL
) {
642 if (OutFptr
!= NULL
) {