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