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
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 -------------------------------------------------------------------------*/
29 MAX_STRING_LENGTH = 2048,
30 MAX_MACRO_NAME_LENGTH = 128
34 _evalMacros(char *apinto, hTab *pvals, const char *pfrom, size_t alen)
36 bool fdidsomething = FALSE;
45 while (plen > 0 && *pfrom) {
50 /* write previous quote */
59 const char *pend = ++pfrom;
60 char name[MAX_MACRO_NAME_LENGTH];
63 /* Find the end of macro */
64 while (*pend && '}' != *pend) {
68 wassertl(0, "Unterminated macro expansion");
71 /* Pull out the macro name */
72 if (pend - pfrom >= MAX_MACRO_NAME_LENGTH) {
73 wassertl(0, "macro name too long");
76 strncpy(name, pfrom, pend - pfrom);
77 name[pend - pfrom] = '\0';
79 /* Look up the value in the hash table */
80 pval = shash_find (pvals, name);
83 /* Empty macro value */
86 if (pend[1] == quote) {
87 /* Start quote equals end quote: skip both */
91 /* Start quote not equals end quote: add both */
99 /* It was a quote, add it */
105 strncpy(pinto, pval, plen);
106 pinto += strlen(pval);
107 plen -= plen > strlen(pval) ? strlen(pval) : plen;
108 fdidsomething = TRUE;
132 if (plen > 0 && '\0' != quote) {
138 wassertl(0, "macro expansion too long");
143 /* If we did something then recursivly expand any expanded macros */
145 char ainto[MAX_STRING_LENGTH];
146 _evalMacros(ainto, pvals, apinto, MAX_STRING_LENGTH);
147 strncpyz(apinto, ainto, alen);
152 mvsprintf(hTab *pvals, const char *pformat, va_list ap)
154 char ainto[MAX_STRING_LENGTH];
155 char atmp[MAX_STRING_LENGTH];
157 /* Recursivly evaluate all the macros in the string */
158 _evalMacros(ainto, pvals, pformat, MAX_STRING_LENGTH);
159 /* Evaluate all the arguments */
160 #if defined(HAVE_VSNPRINTF)
161 if (vsnprintf(atmp, MAX_STRING_LENGTH, ainto, ap) >= MAX_STRING_LENGTH)
163 fprintf(stderr, "Internal error: mvsprintf output truncated.\n");
169 wlen = vsprintf(atmp, ainto, ap);
171 if (wlen < 0 || wlen >= MAX_STRING_LENGTH)
173 wassertl(0, "mvsprintf overflowed.");
178 /* Recursivly evaluate any macros that were used as arguments */
179 _evalMacros(ainto, pvals, atmp, MAX_STRING_LENGTH);
181 /* Return a copy of the evaluated string. */
182 return Safe_strdup(ainto);
185 char *msprintf(hTab *pvals, const char *pformat, ...)
190 va_start(ap, pformat);
192 pret = mvsprintf(pvals, pformat, ap);
200 mfprintf(FILE *fp, hTab *pvals, const char *pformat, ...)
205 va_start(ap, pformat);
207 p = mvsprintf(pvals, pformat, ap);