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