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