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 -------------------------------------------------------------------------*/
26 #include "dbuf_string.h"
29 eval_macros (hTab
* pvals
, const char *pfrom
)
31 bool fdidsomething
= FALSE
;
38 dbuf_init (&dbuf
, 256);
47 /* write previous quote */
48 dbuf_append_char (&dbuf
, quote
);
55 const char *pend
= ++pfrom
;
59 /* Find the end of macro */
60 while (*pend
&& '}' != *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
);
77 /* Empty macro value */
83 /* Start quote equals end quote: skip both */
88 /* Start quote not equals end quote: add both */
89 dbuf_append_char (&dbuf
, quote
);
97 dbuf_append_char (&dbuf
, quote
);
99 dbuf_append_str (&dbuf
, pval
);
100 fdidsomething
= TRUE
;
111 dbuf_append_char (&dbuf
, quote
);
115 dbuf_append_char (&dbuf
, *pfrom
++);
121 dbuf_append_char (&dbuf
, quote
);
125 /* If we did something then recursively expand any expanded macros */
128 char *ret
= eval_macros (pvals
, dbuf_c_str (&dbuf
));
129 dbuf_destroy (&dbuf
);
133 return dbuf_detach_c_str (&dbuf
);
137 mvsprintf (hTab
* pvals
, const char *pformat
, va_list ap
)
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
);
151 /* Recursively evaluate any macros that were used as arguments */
152 p
= eval_macros (pvals
, dbuf_c_str (&dbuf
));
153 dbuf_destroy (&dbuf
);
158 msprintf (hTab
* pvals
, const char *pformat
, ...)
163 va_start (ap
, pformat
);
165 pret
= mvsprintf (pvals
, pformat
, ap
);
173 mfprintf (FILE * fp
, hTab
* pvals
, const char *pformat
, ...)
178 va_start (ap
, pformat
);
180 p
= mvsprintf (pvals
, pformat
, ap
);