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