Adam Tkac | fded078 | 2008-03-22 11:20:54 +0000 | [diff] [blame] | 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 | #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 pdp11 | vax | ns16000 | mc68000 | 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 | cppsetup(line, filep, inc) |
| 63 | register char *line; |
| 64 | register struct filepointer *filep; |
| 65 | register struct inclist *inc; |
| 66 | { |
| 67 | register char *p, savec; |
| 68 | static boolean setupdone = FALSE; |
| 69 | boolean value; |
| 70 | |
| 71 | if (!setupdone) { |
| 72 | cpp_varsetup(); |
| 73 | setupdone = TRUE; |
| 74 | } |
| 75 | |
| 76 | currentfile = filep; |
| 77 | currentinc = inc; |
| 78 | inp = newp = line; |
| 79 | for (p=newp; *p; p++) |
| 80 | ; |
| 81 | |
| 82 | /* |
| 83 | * put a newline back on the end, and set up pend, etc. |
| 84 | */ |
| 85 | *p++ = '\n'; |
| 86 | savec = *p; |
| 87 | *p = '\0'; |
| 88 | pend = p; |
| 89 | |
| 90 | ptrtab = slotab+COFF; |
| 91 | *--inp = SALT; |
| 92 | outp=inp; |
| 93 | value = yyparse(); |
| 94 | *p = savec; |
| 95 | return(value); |
| 96 | } |
| 97 | |
| 98 | struct symtab *lookup(symbol) |
| 99 | char *symbol; |
| 100 | { |
| 101 | static struct symtab undefined; |
| 102 | struct symtab *sp; |
| 103 | |
| 104 | sp = isdefined(symbol, currentinc, NULL); |
| 105 | if (sp == NULL) { |
| 106 | sp = &undefined; |
| 107 | sp->s_value = NULL; |
| 108 | } |
| 109 | return (sp); |
| 110 | } |
| 111 | |
| 112 | pperror(tag, x0,x1,x2,x3,x4) |
| 113 | int tag,x0,x1,x2,x3,x4; |
| 114 | { |
| 115 | warning("\"%s\", line %d: ", currentinc->i_file, currentfile->f_line); |
| 116 | warning(x0,x1,x2,x3,x4); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | yyerror(s) |
| 121 | register char *s; |
| 122 | { |
| 123 | fatalerr("Fatal error: %s\n", s); |
| 124 | } |
| 125 | #else /* not CPP */ |
| 126 | |
| 127 | #include "ifparser.h" |
| 128 | struct _parse_data { |
| 129 | struct filepointer *filep; |
| 130 | struct inclist *inc; |
| 131 | const char *line; |
| 132 | }; |
| 133 | |
| 134 | static const char * |
| 135 | _my_if_errors (ip, cp, expecting) |
| 136 | IfParser *ip; |
| 137 | const char *cp; |
| 138 | const char *expecting; |
| 139 | { |
| 140 | struct _parse_data *pd = (struct _parse_data *) ip->data; |
| 141 | int lineno = pd->filep->f_line; |
| 142 | char *filename = pd->inc->i_file; |
| 143 | char prefix[300]; |
| 144 | int prefixlen; |
| 145 | int i; |
| 146 | |
| 147 | sprintf (prefix, "\"%s\":%d", filename, lineno); |
| 148 | prefixlen = strlen(prefix); |
| 149 | fprintf (stderr, "%s: %s", prefix, pd->line); |
| 150 | i = cp - pd->line; |
| 151 | if (i > 0 && pd->line[i-1] != '\n') { |
| 152 | putc ('\n', stderr); |
| 153 | } |
| 154 | for (i += prefixlen + 3; i > 0; i--) { |
| 155 | putc (' ', stderr); |
| 156 | } |
| 157 | fprintf (stderr, "^--- expecting %s\n", expecting); |
| 158 | return NULL; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | #define MAXNAMELEN 256 |
| 163 | |
| 164 | static struct symtab * |
| 165 | _lookup_variable (ip, var, len) |
| 166 | IfParser *ip; |
| 167 | const char *var; |
| 168 | int len; |
| 169 | { |
| 170 | char tmpbuf[MAXNAMELEN + 1]; |
| 171 | struct _parse_data *pd = (struct _parse_data *) ip->data; |
| 172 | |
| 173 | if (len > MAXNAMELEN) |
| 174 | return 0; |
| 175 | |
| 176 | strncpy (tmpbuf, var, len); |
| 177 | tmpbuf[len] = '\0'; |
| 178 | return isdefined (tmpbuf, pd->inc, NULL); |
| 179 | } |
| 180 | |
| 181 | |
| 182 | static int |
| 183 | _my_eval_defined (ip, var, len) |
| 184 | IfParser *ip; |
| 185 | const char *var; |
| 186 | int len; |
| 187 | { |
| 188 | if (_lookup_variable (ip, var, len)) |
| 189 | return 1; |
| 190 | else |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | #define isvarfirstletter(ccc) (isalpha(ccc) || (ccc) == '_') |
| 195 | |
| 196 | static int |
| 197 | _my_eval_variable (ip, var, len) |
| 198 | IfParser *ip; |
| 199 | const char *var; |
| 200 | int len; |
| 201 | { |
| 202 | struct symtab *s; |
| 203 | |
| 204 | s = _lookup_variable (ip, var, len); |
| 205 | if (!s) |
| 206 | return 0; |
| 207 | do { |
| 208 | var = s->s_value; |
| 209 | if (!isvarfirstletter(*var)) |
| 210 | break; |
| 211 | s = _lookup_variable (ip, var, strlen(var)); |
| 212 | } while (s); |
| 213 | |
| 214 | return atoi(var); |
| 215 | } |
| 216 | |
| 217 | |
| 218 | cppsetup(line, filep, inc) |
| 219 | register char *line; |
| 220 | register struct filepointer *filep; |
| 221 | register struct inclist *inc; |
| 222 | { |
| 223 | IfParser ip; |
| 224 | struct _parse_data pd; |
| 225 | int val = 0; |
| 226 | |
| 227 | pd.filep = filep; |
| 228 | pd.inc = inc; |
| 229 | pd.line = line; |
| 230 | ip.funcs.handle_error = _my_if_errors; |
| 231 | ip.funcs.eval_defined = _my_eval_defined; |
| 232 | ip.funcs.eval_variable = _my_eval_variable; |
| 233 | ip.data = (char *) &pd; |
| 234 | |
| 235 | (void) ParseIfExpression (&ip, line, &val); |
| 236 | if (val) |
| 237 | return IF; |
| 238 | else |
| 239 | return IFFALSE; |
| 240 | } |
| 241 | #endif /* CPP */ |
| 242 | |