* .version: Updated to 2.3.1
[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           printf("Mapping %s to \"%s\"\n", name, pval);
71
72           /* Replace */
73           strcpy(pinto, pval);
74           pinto += strlen(pval);
75           fdidsomething = TRUE;
76
77           pfrom = pend+1;
78         }
79       else
80         {
81           /* Pass through */
82           *pinto++ = *pfrom++;
83         }
84     }
85
86   *pinto = '\0';
87
88   /* If we did something then recursivly expand any expanded macros */
89   if (fdidsomething)
90     {
91       char ainto[MAX_STRING_LENGTH];
92       _evalMacros(ainto, pvals, apinto);
93       strcpy(apinto, ainto);
94     }
95 }
96
97 char *
98 mvsprintf(hTab *pvals, const char *pformat, va_list ap)
99 {
100   char ainto[MAX_STRING_LENGTH];
101   char atmp[MAX_STRING_LENGTH];
102
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);
109
110   /* Return a copy of the evaluated string. */
111   return gc_strdup(ainto);
112 }
113
114 char *msprintf(hTab *pvals, const char *pformat, ...)
115 {
116   va_list ap;
117   char *pret;
118
119   va_start(ap, pformat);
120
121   pret = mvsprintf(pvals, pformat, ap);
122
123   va_end(ap);
124
125   return pret;
126 }