* support/Util/NewAlloc.c (freeTrace): Changed free for the gc case to not free...
[fw/sdcc] / src / SDCCmacro.c
1 /*-------------------------------------------------------------------------
2   SDCCmain.c - Macro support code.
3
4              Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1999)
5
6    This program is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by the
8    Free Software Foundation; either version 2, or (at your option) any
9    later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20    In other words, you are welcome to use, share and improve this program.
21    You are forbidden to forbid anyone else to use, share and improve
22    what you give them.   Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
24
25 #include "common.h"
26
27 enum 
28   {
29     MAX_STRING_LENGTH   = FILENAME_MAX,
30     MAX_MACRO_NAME_LENGTH = 128
31   };
32
33 void
34 _evalMacros(char *apinto, hTab *pvals, const char *pfrom)
35 {
36   bool fdidsomething = FALSE;
37   char *pinto = apinto;
38
39   assert(pinto && pvals && pfrom);
40
41   while (*pfrom)
42     {
43       if (*pfrom == '{')
44         {
45           const char *pend = ++pfrom;
46           char name[MAX_MACRO_NAME_LENGTH];
47           const char *pval;
48
49           while (*pend && *pend != '}')
50             {
51               pend++;
52             }
53           if (*pend != '}')
54             {
55               wassertl(0, "Unterminated macro expansion");
56             }
57           /* Pull out the macro name */
58           strncpy(name, pfrom, pend-pfrom);
59           name[pend-pfrom] = '\0';
60
61           /* Look up the value in the hash table */
62           pval = shash_find (pvals, name);
63           
64           if (pval == NULL)
65             {
66               fprintf (stderr, "Cant find macro \"%s\"\n", name);
67               wassertl (0, "Invalid macro name");
68             }
69
70           /* Replace */
71           strcpy(pinto, pval);
72           pinto += strlen(pval);
73           fdidsomething = TRUE;
74
75           pfrom = pend+1;
76         }
77       else
78         {
79           /* Pass through */
80           *pinto++ = *pfrom++;
81         }
82     }
83
84   *pinto = '\0';
85
86   /* If we did something then recursivly expand any expanded macros */
87   if (fdidsomething)
88     {
89       char ainto[MAX_STRING_LENGTH];
90       _evalMacros(ainto, pvals, apinto);
91       strcpy(apinto, ainto);
92     }
93 }
94
95 char *
96 mvsprintf(hTab *pvals, const char *pformat, va_list ap)
97 {
98   char ainto[MAX_STRING_LENGTH];
99   char atmp[MAX_STRING_LENGTH];
100
101   /* Recursivly evaluate all the macros in the string */
102   _evalMacros(ainto, pvals, pformat);
103   /* Evaluate all the arguments */
104   vsprintf(atmp, ainto, ap);
105   /* Recursivly evaluate any macros that were used as arguments */
106   _evalMacros(ainto, pvals, atmp);
107
108   /* Return a copy of the evaluated string. */
109   return Safe_strdup(ainto);
110 }
111
112 char *msprintf(hTab *pvals, const char *pformat, ...)
113 {
114   va_list ap;
115   char *pret;
116
117   va_start(ap, pformat);
118
119   pret = mvsprintf(pvals, pformat, ap);
120
121   va_end(ap);
122
123   return pret;
124 }