Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / config / makedepend / cppsetup.c
1 /* $TOG: cppsetup.c /main/18 1998/02/06 11:09:35 kaleb $ */
2 /*
3
4 Copyright (c) 1993, 1994, 1998  The Open Group
5
6 All Rights Reserved.
7
8 The above copyright notice and this permission notice shall be included in
9 all copies or substantial portions of the Software.
10
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
14 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
15 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17
18 Except as contained in this notice, the name of The Open Group shall not be
19 used in advertising or otherwise to promote the sale, use or other dealings
20 in this Software without prior written authorization from The Open Group.
21
22 */
23
24 #include "def.h"
25
26 #ifdef  CPP
27 /*
28  * This file is strictly for the sake of cpy.y and yylex.c (if
29  * you indeed have the source for cpp).
30  */
31 #define IB 1
32 #define SB 2
33 #define NB 4
34 #define CB 8
35 #define QB 16
36 #define WB 32
37 #define SALT '#'
38 #if pdp11 | vax | ns16000 | mc68000 | ibm032
39 #define COFF 128
40 #else
41 #define COFF 0
42 #endif
43 /*
44  * These variables used by cpy.y and yylex.c
45  */
46 extern char     *outp, *inp, *newp, *pend;
47 extern char     *ptrtab;
48 extern char     fastab[];
49 extern char     slotab[];
50
51 /*
52  * cppsetup
53  */
54 struct filepointer      *currentfile;
55 struct inclist          *currentinc;
56
57 cppsetup(line, filep, inc)
58         register char   *line;
59         register struct filepointer     *filep;
60         register struct inclist         *inc;
61 {
62         register char *p, savec;
63         static boolean setupdone = FALSE;
64         boolean value;
65
66         if (!setupdone) {
67                 cpp_varsetup();
68                 setupdone = TRUE;
69         }
70
71         currentfile = filep;
72         currentinc = inc;
73         inp = newp = line;
74         for (p=newp; *p; p++)
75                 ;
76
77         /*
78          * put a newline back on the end, and set up pend, etc.
79          */
80         *p++ = '\n';
81         savec = *p;
82         *p = '\0';
83         pend = p;
84
85         ptrtab = slotab+COFF;
86         *--inp = SALT; 
87         outp=inp; 
88         value = yyparse();
89         *p = savec;
90         return(value);
91 }
92
93 struct symtab **lookup(symbol)
94         char    *symbol;
95 {
96         static struct symtab    *undefined;
97         struct symtab   **sp;
98
99         sp = isdefined(symbol, currentinc, NULL);
100         if (sp == NULL) {
101                 sp = &undefined;
102                 (*sp)->s_value = NULL;
103         }
104         return (sp);
105 }
106
107 pperror(tag, x0,x1,x2,x3,x4)
108         int     tag,x0,x1,x2,x3,x4;
109 {
110         warning("\"%s\", line %d: ", currentinc->i_file, currentfile->f_line);
111         warning(x0,x1,x2,x3,x4);
112 }
113
114
115 yyerror(s)
116         register char   *s;
117 {
118         fatalerr("Fatal error: %s\n", s);
119 }
120 #else /* not CPP */
121
122 #include "ifparser.h"
123 struct _parse_data {
124     struct filepointer *filep;
125     struct inclist *inc;
126     const char *line;
127 };
128
129 static const char *
130 my_if_errors (ip, cp, expecting)
131     IfParser *ip;
132     const char *cp;
133     const char *expecting;
134 {
135     struct _parse_data *pd = (struct _parse_data *) ip->data;
136     int lineno = pd->filep->f_line;
137     char *filename = pd->inc->i_file;
138     char prefix[300];
139     int prefixlen;
140     int i;
141
142     sprintf (prefix, "\"%s\":%d", filename, lineno);
143     prefixlen = strlen(prefix);
144     fprintf (stderr, "%s:  %s", prefix, pd->line);
145     i = cp - pd->line;
146     if (i > 0 && pd->line[i-1] != '\n') {
147         putc ('\n', stderr);
148     }
149     for (i += prefixlen + 3; i > 0; i--) {
150         putc (' ', stderr);
151     }
152     fprintf (stderr, "^--- expecting %s\n", expecting);
153     return NULL;
154 }
155
156
157 #define MAXNAMELEN 256
158
159 static struct symtab **
160 lookup_variable (ip, var, len)
161     IfParser *ip;
162     const char *var;
163     int len;
164 {
165     char tmpbuf[MAXNAMELEN + 1];
166     struct _parse_data *pd = (struct _parse_data *) ip->data;
167
168     if (len > MAXNAMELEN)
169         return 0;
170
171     strncpy (tmpbuf, var, len);
172     tmpbuf[len] = '\0';
173     return isdefined (tmpbuf, pd->inc, NULL);
174 }
175
176
177 static int
178 my_eval_defined (ip, var, len)
179     IfParser *ip;
180     const char *var;
181     int len;
182 {
183     if (lookup_variable (ip, var, len))
184         return 1;
185     else
186         return 0;
187 }
188
189 #define isvarfirstletter(ccc) (isalpha(ccc) || (ccc) == '_')
190
191 static long
192 my_eval_variable (ip, var, len)
193     IfParser *ip;
194     const char *var;
195     int len;
196 {
197     struct symtab **s;
198
199     s = lookup_variable (ip, var, len);
200     if (!s)
201         return 0;
202     do {
203         var = (*s)->s_value;
204         if (!isvarfirstletter(*var))
205             break;
206         s = lookup_variable (ip, var, strlen(var));
207     } while (s);
208
209     return strtol(var, NULL, 0);
210 }
211
212
213 cppsetup(line, filep, inc)
214         register char   *line;
215         register struct filepointer     *filep;
216         register struct inclist         *inc;
217 {
218     IfParser ip;
219     struct _parse_data pd;
220     long val = 0;
221
222     pd.filep = filep;
223     pd.inc = inc;
224     pd.line = line;
225     ip.funcs.handle_error = my_if_errors;
226     ip.funcs.eval_defined = my_eval_defined;
227     ip.funcs.eval_variable = my_eval_variable;
228     ip.data = (char *) &pd;
229
230     (void) ParseIfExpression (&ip, line, &val);
231     if (val)
232         return IF;
233     else
234         return IFFALSE;
235 }
236 #endif /* CPP */
237