|
1 /* $Xorg: cppsetup.c,v 1.5 2001/02/09 02:03:16 xorgcvs Exp $ */ |
|
2 /* |
|
3 |
|
4 Copyright (c) 1993, 1994, 1998 The Open Group |
|
5 |
|
6 Permission to use, copy, modify, distribute, and sell this software and its |
|
7 documentation for any purpose is hereby granted without fee, provided that |
|
8 the above copyright notice appear in all copies and that both that |
|
9 copyright notice and this permission notice appear in supporting |
|
10 documentation. |
|
11 |
|
12 The above copyright notice and this permission notice shall be included in |
|
13 all copies or substantial portions of the Software. |
|
14 |
|
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
18 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
|
19 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
|
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
21 |
|
22 Except as contained in this notice, the name of The Open Group shall not be |
|
23 used in advertising or otherwise to promote the sale, use or other dealings |
|
24 in this Software without prior written authorization from The Open Group. |
|
25 |
|
26 */ |
|
27 /* $XFree86: xc/config/makedepend/cppsetup.c,v 3.11 2001/12/17 20:52:22 dawes Exp $ */ |
|
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 defined(pdp11) || defined(vax) || defined(ns16000) || defined(mc68000) || defined(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 |
|
62 int |
|
63 cppsetup(char *line, struct filepointer *filep, struct inclist *inc) |
|
64 { |
|
65 char *p, savec; |
|
66 static boolean setupdone = FALSE; |
|
67 boolean value; |
|
68 |
|
69 if (!setupdone) { |
|
70 cpp_varsetup(); |
|
71 setupdone = TRUE; |
|
72 } |
|
73 |
|
74 currentfile = filep; |
|
75 currentinc = inc; |
|
76 inp = newp = line; |
|
77 for (p=newp; *p; p++) |
|
78 ; |
|
79 |
|
80 /* |
|
81 * put a newline back on the end, and set up pend, etc. |
|
82 */ |
|
83 *p++ = '\n'; |
|
84 savec = *p; |
|
85 *p = '\0'; |
|
86 pend = p; |
|
87 |
|
88 ptrtab = slotab+COFF; |
|
89 *--inp = SALT; |
|
90 outp=inp; |
|
91 value = yyparse(); |
|
92 *p = savec; |
|
93 return(value); |
|
94 } |
|
95 |
|
96 struct symtab **lookup(symbol) |
|
97 char *symbol; |
|
98 { |
|
99 static struct symtab *undefined; |
|
100 struct symtab **sp; |
|
101 |
|
102 sp = isdefined(symbol, currentinc, NULL); |
|
103 if (sp == NULL) { |
|
104 sp = &undefined; |
|
105 (*sp)->s_value = NULL; |
|
106 } |
|
107 return (sp); |
|
108 } |
|
109 |
|
110 pperror(tag, x0,x1,x2,x3,x4) |
|
111 int tag,x0,x1,x2,x3,x4; |
|
112 { |
|
113 warning("\"%s\", line %d: ", currentinc->i_file, currentfile->f_line); |
|
114 warning(x0,x1,x2,x3,x4); |
|
115 } |
|
116 |
|
117 |
|
118 yyerror(s) |
|
119 register char *s; |
|
120 { |
|
121 fatalerr("Fatal error: %s\n", s); |
|
122 } |
|
123 #else /* not CPP */ |
|
124 |
|
125 #include "ifparser.h" |
|
126 struct _parse_data { |
|
127 struct filepointer *filep; |
|
128 struct inclist *inc; |
|
129 char *filename; |
|
130 const char *line; |
|
131 }; |
|
132 |
|
133 static const char * |
|
134 my_if_errors (IfParser *ip, const char *cp, const char *expecting) |
|
135 { |
|
136 struct _parse_data *pd = (struct _parse_data *) ip->data; |
|
137 int lineno = pd->filep->f_line; |
|
138 char *filename = pd->filename; |
|
139 char prefix[300]; |
|
140 int prefixlen; |
|
141 int i; |
|
142 |
|
143 sprintf (prefix, "\"%s\":%d", filename, lineno); |
|
144 prefixlen = strlen(prefix); |
|
145 fprintf (stderr, "%s: %s", prefix, pd->line); |
|
146 i = cp - pd->line; |
|
147 if (i > 0 && pd->line[i-1] != '\n') { |
|
148 putc ('\n', stderr); |
|
149 } |
|
150 for (i += prefixlen + 3; i > 0; i--) { |
|
151 putc (' ', stderr); |
|
152 } |
|
153 fprintf (stderr, "^--- expecting %s\n", expecting); |
|
154 return NULL; |
|
155 } |
|
156 |
|
157 |
|
158 #define MAXNAMELEN 256 |
|
159 |
|
160 static struct symtab ** |
|
161 lookup_variable (IfParser *ip, const char *var, int len) |
|
162 { |
|
163 char tmpbuf[MAXNAMELEN + 1]; |
|
164 struct _parse_data *pd = (struct _parse_data *) ip->data; |
|
165 |
|
166 if (len > MAXNAMELEN) |
|
167 return 0; |
|
168 |
|
169 strncpy (tmpbuf, var, len); |
|
170 tmpbuf[len] = '\0'; |
|
171 return isdefined (tmpbuf, pd->inc, NULL); |
|
172 } |
|
173 |
|
174 |
|
175 static int |
|
176 my_eval_defined (IfParser *ip, const char *var, int len) |
|
177 { |
|
178 if (lookup_variable (ip, var, len)) |
|
179 return 1; |
|
180 else |
|
181 return 0; |
|
182 } |
|
183 |
|
184 #define isvarfirstletter(ccc) (isalpha(ccc) || (ccc) == '_') |
|
185 |
|
186 static long |
|
187 my_eval_variable (IfParser *ip, const char *var, int len) |
|
188 { |
|
189 long val; |
|
190 struct symtab **s; |
|
191 |
|
192 s = lookup_variable (ip, var, len); |
|
193 if (!s) |
|
194 return 0; |
|
195 do { |
|
196 var = (*s)->s_value; |
|
197 if (!isvarfirstletter(*var) || !strcmp((*s)->s_name, var)) |
|
198 break; |
|
199 s = lookup_variable (ip, var, strlen(var)); |
|
200 } while (s); |
|
201 |
|
202 var = ParseIfExpression(ip, var, &val); |
|
203 if (var && *var) debug(4, ("extraneous: '%s'\n", var)); |
|
204 return val; |
|
205 } |
|
206 |
|
207 int |
|
208 cppsetup(char *filename, |
|
209 char *line, |
|
210 struct filepointer *filep, |
|
211 struct inclist *inc) |
|
212 { |
|
213 IfParser ip; |
|
214 struct _parse_data pd; |
|
215 long val = 0; |
|
216 |
|
217 pd.filep = filep; |
|
218 pd.inc = inc; |
|
219 pd.line = line; |
|
220 pd.filename = filename; |
|
221 ip.funcs.handle_error = my_if_errors; |
|
222 ip.funcs.eval_defined = my_eval_defined; |
|
223 ip.funcs.eval_variable = my_eval_variable; |
|
224 ip.data = (char *) &pd; |
|
225 |
|
226 (void) ParseIfExpression (&ip, line, &val); |
|
227 if (val) |
|
228 return IF; |
|
229 else |
|
230 return IFFALSE; |
|
231 } |
|
232 #endif /* CPP */ |
|
233 |