struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / src / SDCCmacro.c
bloba01f3469a4e0856f3028ac739f336d9783b05610
1 /*-------------------------------------------------------------------------
2 SDCCmain.c - Macro support code.
4 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999)
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.
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.
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.
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 -------------------------------------------------------------------------*/
25 #include "common.h"
26 #include "dbuf_string.h"
28 char *
29 eval_macros (hTab * pvals, const char *pfrom)
31 bool fdidsomething = FALSE;
32 char quote = '\0';
33 struct dbuf_s dbuf;
35 assert (pvals);
36 assert (pfrom);
38 dbuf_init (&dbuf, 256);
39 while (*pfrom)
41 switch (*pfrom)
43 case '"':
44 case '\'':
45 if (quote != '\0')
47 /* write previous quote */
48 dbuf_append_char (&dbuf, quote);
50 quote = *pfrom++;
51 break;
53 case '{':
55 const char *pend = ++pfrom;
56 const char *pval;
57 char *name;
59 /* Find the end of macro */
60 while (*pend && '}' != *pend)
62 pend++;
64 if ('}' != *pend)
66 wassertl (0, "Unterminated macro expansion");
69 name = Safe_strndup (pfrom, pend - pfrom);
71 /* Look up the value in the hash table */
72 pval = shash_find (pvals, name);
73 Safe_free (name);
75 if (NULL == pval)
77 /* Empty macro value */
78 if ('\0' != quote)
80 /* It was a quote */
81 if (pend[1] == quote)
83 /* Start quote equals end quote: skip both */
84 ++pend;
86 else
88 /* Start quote not equals end quote: add both */
89 dbuf_append_char (&dbuf, quote);
93 else
95 if ('\0' != quote)
97 dbuf_append_char (&dbuf, quote);
99 dbuf_append_str (&dbuf, pval);
100 fdidsomething = TRUE;
103 quote = '\0';
104 pfrom = pend + 1;
106 break;
108 default:
109 if ('\0' != quote)
111 dbuf_append_char (&dbuf, quote);
112 quote = '\0';
115 dbuf_append_char (&dbuf, *pfrom++);
119 if ('\0' != quote)
121 dbuf_append_char (&dbuf, quote);
125 /* If we did something then recursively expand any expanded macros */
126 if (fdidsomething)
128 char *ret = eval_macros (pvals, dbuf_c_str (&dbuf));
129 dbuf_destroy (&dbuf);
130 return ret;
133 return dbuf_detach_c_str (&dbuf);
136 char *
137 mvsprintf (hTab * pvals, const char *pformat, va_list ap)
139 char *p;
140 struct dbuf_s dbuf;
142 dbuf_init (&dbuf, 256);
144 /* Recursively evaluate all the macros in the string */
145 p = eval_macros (pvals, pformat);
147 /* Evaluate all the arguments */
148 dbuf_vprintf (&dbuf, p, ap);
149 Safe_free (p);
151 /* Recursively evaluate any macros that were used as arguments */
152 p = eval_macros (pvals, dbuf_c_str (&dbuf));
153 dbuf_destroy (&dbuf);
154 return p;
157 char *
158 msprintf (hTab * pvals, const char *pformat, ...)
160 va_list ap;
161 char *pret;
163 va_start (ap, pformat);
165 pret = mvsprintf (pvals, pformat, ap);
167 va_end (ap);
169 return pret;
172 void
173 mfprintf (FILE * fp, hTab * pvals, const char *pformat, ...)
175 va_list ap;
176 char *p;
178 va_start (ap, pformat);
180 p = mvsprintf (pvals, pformat, ap);
182 va_end (ap);
184 fputs (p, fp);
185 Safe_free (p);