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 = PATH_MAX,
30 MAX_MACRO_NAME_LENGTH = 128
34 _evalMacros(char *apinto, hTab *pvals, const char *pfrom)
36 bool fdidsomething = FALSE;
47 const char *pend = ++pfrom;
48 char name[MAX_MACRO_NAME_LENGTH];
51 while (*pend && *pend != '}')
57 wassertl(0, "Unterminated macro expansion");
59 /* Pull out the macro name */
60 strncpy(name, pfrom, pend-pfrom);
61 name[pend-pfrom] = '\0';
63 /* Look up the value in the hash table */
64 pval = shash_find (pvals, name);
68 fprintf (stderr, "Cant find macro \"%s\"\n", name);
69 wassertl (0, "Invalid macro name");
74 pinto += strlen(pval);
88 /* If we did something then recursivly expand any expanded macros */
91 char ainto[MAX_STRING_LENGTH];
92 _evalMacros(ainto, pvals, apinto);
93 strcpy(apinto, ainto);
98 mvsprintf(hTab *pvals, const char *pformat, va_list ap)
100 char ainto[MAX_STRING_LENGTH];
101 char atmp[MAX_STRING_LENGTH];
103 /* Recursivly evaluate all the macros in the string */
104 _evalMacros(ainto, pvals, pformat);
105 /* Evaluate all the arguments */
106 vsprintf(atmp, ainto, ap);
107 /* Recursivly evaluate any macros that were used as arguments */
108 _evalMacros(ainto, pvals, atmp);
110 /* Return a copy of the evaluated string. */
111 return Safe_strdup(ainto);
114 char *msprintf(hTab *pvals, const char *pformat, ...)
119 va_start(ap, pformat);
121 pret = mvsprintf(pvals, pformat, ap);
129 mfprintf(FILE *fp, hTab *pvals, const char *pformat, ...)
134 va_start(ap, pformat);
136 p = mvsprintf(pvals, pformat, ap);