blob: bf1c4fc5e7e81717cc7d23dcb8dc16ecf375eeb3 [file] [log] [blame]
Adam Tkacfded0782008-03-22 11:20:54 +00001/* $XConsortium: cppsetup.c,v 1.13 94/04/17 20:10:32 gildea Exp $ */
2/*
3
4Copyright (c) 1993, 1994 X Consortium
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in
14all copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23Except as contained in this notice, the name of the X Consortium shall not be
24used in advertising or otherwise to promote the sale, use or other dealings
25in 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 */
51extern char *outp, *inp, *newp, *pend;
52extern char *ptrtab;
53extern char fastab[];
54extern char slotab[];
55
56/*
57 * cppsetup
58 */
59struct filepointer *currentfile;
60struct inclist *currentinc;
61
62cppsetup(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
98struct symtab *lookup(symbol)
99 char *symbol;
100{
101 static struct symtab undefined;
102 struct symtab *sp;
103
104 sp = isdefined(symbol, currentinc, NULL);
105 if (sp == NULL) {
106 sp = &undefined;
107 sp->s_value = NULL;
108 }
109 return (sp);
110}
111
112pperror(tag, x0,x1,x2,x3,x4)
113 int tag,x0,x1,x2,x3,x4;
114{
115 warning("\"%s\", line %d: ", currentinc->i_file, currentfile->f_line);
116 warning(x0,x1,x2,x3,x4);
117}
118
119
120yyerror(s)
121 register char *s;
122{
123 fatalerr("Fatal error: %s\n", s);
124}
125#else /* not CPP */
126
127#include "ifparser.h"
128struct _parse_data {
129 struct filepointer *filep;
130 struct inclist *inc;
131 const char *line;
132};
133
134static const char *
135_my_if_errors (ip, cp, expecting)
136 IfParser *ip;
137 const char *cp;
138 const char *expecting;
139{
140 struct _parse_data *pd = (struct _parse_data *) ip->data;
141 int lineno = pd->filep->f_line;
142 char *filename = pd->inc->i_file;
143 char prefix[300];
144 int prefixlen;
145 int i;
146
147 sprintf (prefix, "\"%s\":%d", filename, lineno);
148 prefixlen = strlen(prefix);
149 fprintf (stderr, "%s: %s", prefix, pd->line);
150 i = cp - pd->line;
151 if (i > 0 && pd->line[i-1] != '\n') {
152 putc ('\n', stderr);
153 }
154 for (i += prefixlen + 3; i > 0; i--) {
155 putc (' ', stderr);
156 }
157 fprintf (stderr, "^--- expecting %s\n", expecting);
158 return NULL;
159}
160
161
162#define MAXNAMELEN 256
163
164static struct symtab *
165_lookup_variable (ip, var, len)
166 IfParser *ip;
167 const char *var;
168 int len;
169{
170 char tmpbuf[MAXNAMELEN + 1];
171 struct _parse_data *pd = (struct _parse_data *) ip->data;
172
173 if (len > MAXNAMELEN)
174 return 0;
175
176 strncpy (tmpbuf, var, len);
177 tmpbuf[len] = '\0';
178 return isdefined (tmpbuf, pd->inc, NULL);
179}
180
181
182static int
183_my_eval_defined (ip, var, len)
184 IfParser *ip;
185 const char *var;
186 int len;
187{
188 if (_lookup_variable (ip, var, len))
189 return 1;
190 else
191 return 0;
192}
193
194#define isvarfirstletter(ccc) (isalpha(ccc) || (ccc) == '_')
195
196static int
197_my_eval_variable (ip, var, len)
198 IfParser *ip;
199 const char *var;
200 int len;
201{
202 struct symtab *s;
203
204 s = _lookup_variable (ip, var, len);
205 if (!s)
206 return 0;
207 do {
208 var = s->s_value;
209 if (!isvarfirstletter(*var))
210 break;
211 s = _lookup_variable (ip, var, strlen(var));
212 } while (s);
213
214 return atoi(var);
215}
216
217
218cppsetup(line, filep, inc)
219 register char *line;
220 register struct filepointer *filep;
221 register struct inclist *inc;
222{
223 IfParser ip;
224 struct _parse_data pd;
225 int val = 0;
226
227 pd.filep = filep;
228 pd.inc = inc;
229 pd.line = line;
230 ip.funcs.handle_error = _my_if_errors;
231 ip.funcs.eval_defined = _my_eval_defined;
232 ip.funcs.eval_variable = _my_eval_variable;
233 ip.data = (char *) &pd;
234
235 (void) ParseIfExpression (&ip, line, &val);
236 if (val)
237 return IF;
238 else
239 return IFFALSE;
240}
241#endif /* CPP */
242