]> git.sesse.net Git - rdpsrv/blob - Xserver/config/makedepend/cppsetup.c
Import X server from vnc-3.3.7.
[rdpsrv] / Xserver / config / makedepend / cppsetup.c
1 /* $XConsortium: cppsetup.c /main/17 1996/09/28 16:15:03 rws $ */
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 /* $XFree86: xc/config/makedepend/cppsetup.c,v 3.2 1996/12/30 13:57:53 dawes Exp $ */
29
30 #include "def.h"
31
32 #ifdef  CPP
33 /*
34  * This file is strictly for the sake of cpy.y and yylex.c (if
35  * you indeed have the source for cpp).
36  */
37 #define IB 1
38 #define SB 2
39 #define NB 4
40 #define CB 8
41 #define QB 16
42 #define WB 32
43 #define SALT '#'
44 #if defined(pdp11) || defined(vax) || defined(ns16000) || defined(mc68000) || defined(ibm032)
45 #define COFF 128
46 #else
47 #define COFF 0
48 #endif
49 /*
50  * These variables used by cpy.y and yylex.c
51  */
52 extern char     *outp, *inp, *newp, *pend;
53 extern char     *ptrtab;
54 extern char     fastab[];
55 extern char     slotab[];
56
57 /*
58  * cppsetup
59  */
60 struct filepointer      *currentfile;
61 struct inclist          *currentinc;
62
63 cppsetup(line, filep, inc)
64         register char   *line;
65         register struct filepointer     *filep;
66         register struct inclist         *inc;
67 {
68         register char *p, savec;
69         static boolean setupdone = FALSE;
70         boolean value;
71
72         if (!setupdone) {
73                 cpp_varsetup();
74                 setupdone = TRUE;
75         }
76
77         currentfile = filep;
78         currentinc = inc;
79         inp = newp = line;
80         for (p=newp; *p; p++)
81                 ;
82
83         /*
84          * put a newline back on the end, and set up pend, etc.
85          */
86         *p++ = '\n';
87         savec = *p;
88         *p = '\0';
89         pend = p;
90
91         ptrtab = slotab+COFF;
92         *--inp = SALT; 
93         outp=inp; 
94         value = yyparse();
95         *p = savec;
96         return(value);
97 }
98
99 struct symtab **lookup(symbol)
100         char    *symbol;
101 {
102         static struct symtab    *undefined;
103         struct symtab   **sp;
104
105         sp = isdefined(symbol, currentinc, NULL);
106         if (sp == NULL) {
107                 sp = &undefined;
108                 (*sp)->s_value = NULL;
109         }
110         return (sp);
111 }
112
113 pperror(tag, x0,x1,x2,x3,x4)
114         int     tag,x0,x1,x2,x3,x4;
115 {
116         warning("\"%s\", line %d: ", currentinc->i_file, currentfile->f_line);
117         warning(x0,x1,x2,x3,x4);
118 }
119
120
121 yyerror(s)
122         register char   *s;
123 {
124         fatalerr("Fatal error: %s\n", s);
125 }
126 #else /* not CPP */
127
128 #include "ifparser.h"
129 struct _parse_data {
130     struct filepointer *filep;
131     struct inclist *inc;
132     const char *line;
133 };
134
135 static const char *
136 my_if_errors (ip, cp, expecting)
137     IfParser *ip;
138     const char *cp;
139     const char *expecting;
140 {
141     struct _parse_data *pd = (struct _parse_data *) ip->data;
142     int lineno = pd->filep->f_line;
143     char *filename = pd->inc->i_file;
144     char prefix[300];
145     int prefixlen;
146     int i;
147
148     sprintf (prefix, "\"%s\":%d", filename, lineno);
149     prefixlen = strlen(prefix);
150     fprintf (stderr, "%s:  %s", prefix, pd->line);
151     i = cp - pd->line;
152     if (i > 0 && pd->line[i-1] != '\n') {
153         putc ('\n', stderr);
154     }
155     for (i += prefixlen + 3; i > 0; i--) {
156         putc (' ', stderr);
157     }
158     fprintf (stderr, "^--- expecting %s\n", expecting);
159     return NULL;
160 }
161
162
163 #define MAXNAMELEN 256
164
165 static struct symtab **
166 lookup_variable (ip, var, len)
167     IfParser *ip;
168     const char *var;
169     int len;
170 {
171     char tmpbuf[MAXNAMELEN + 1];
172     struct _parse_data *pd = (struct _parse_data *) ip->data;
173
174     if (len > MAXNAMELEN)
175         return 0;
176
177     strncpy (tmpbuf, var, len);
178     tmpbuf[len] = '\0';
179     return isdefined (tmpbuf, pd->inc, NULL);
180 }
181
182
183 static int
184 my_eval_defined (ip, var, len)
185     IfParser *ip;
186     const char *var;
187     int len;
188 {
189     if (lookup_variable (ip, var, len))
190         return 1;
191     else
192         return 0;
193 }
194
195 #define isvarfirstletter(ccc) (isalpha(ccc) || (ccc) == '_')
196
197 static long
198 my_eval_variable (ip, var, len)
199     IfParser *ip;
200     const char *var;
201     int len;
202 {
203     struct symtab **s;
204
205     s = lookup_variable (ip, var, len);
206     if (!s)
207         return 0;
208     do {
209         var = (*s)->s_value;
210         if (!isvarfirstletter(*var))
211             break;
212         s = lookup_variable (ip, var, strlen(var));
213     } while (s);
214
215     return strtol(var, NULL, 0);
216 }
217
218
219 cppsetup(line, filep, inc)
220         register char   *line;
221         register struct filepointer     *filep;
222         register struct inclist         *inc;
223 {
224     IfParser ip;
225     struct _parse_data pd;
226     long val = 0;
227
228     pd.filep = filep;
229     pd.inc = inc;
230     pd.line = line;
231     ip.funcs.handle_error = my_if_errors;
232     ip.funcs.eval_defined = my_eval_defined;
233     ip.funcs.eval_variable = my_eval_variable;
234     ip.data = (char *) &pd;
235
236     (void) ParseIfExpression (&ip, line, &val);
237     if (val)
238         return IF;
239     else
240         return IFFALSE;
241 }
242 #endif /* CPP */
243