Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* $Xorg: parse.c,v 1.6 2001/02/09 02:03:16 xorgcvs Exp $ */ |
michael@0 | 2 | /* |
michael@0 | 3 | |
michael@0 | 4 | Copyright (c) 1993, 1994, 1998 The Open Group |
michael@0 | 5 | |
michael@0 | 6 | Permission to use, copy, modify, distribute, and sell this software and its |
michael@0 | 7 | documentation for any purpose is hereby granted without fee, provided that |
michael@0 | 8 | the above copyright notice appear in all copies and that both that |
michael@0 | 9 | copyright notice and this permission notice appear in supporting |
michael@0 | 10 | documentation. |
michael@0 | 11 | |
michael@0 | 12 | The above copyright notice and this permission notice shall be included in |
michael@0 | 13 | all copies or substantial portions of the Software. |
michael@0 | 14 | |
michael@0 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
michael@0 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
michael@0 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
michael@0 | 18 | OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
michael@0 | 19 | AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
michael@0 | 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
michael@0 | 21 | |
michael@0 | 22 | Except as contained in this notice, the name of The Open Group shall not be |
michael@0 | 23 | used in advertising or otherwise to promote the sale, use or other dealings |
michael@0 | 24 | in this Software without prior written authorization from The Open Group. |
michael@0 | 25 | |
michael@0 | 26 | */ |
michael@0 | 27 | /* $XFree86: xc/config/makedepend/parse.c,v 1.12 2002/02/26 05:09:10 tsi Exp $ */ |
michael@0 | 28 | |
michael@0 | 29 | #include "def.h" |
michael@0 | 30 | |
michael@0 | 31 | extern char *directives[]; |
michael@0 | 32 | extern struct inclist inclist[ MAXFILES ], |
michael@0 | 33 | *inclistnext, |
michael@0 | 34 | maininclist; |
michael@0 | 35 | extern char *includedirs[ ], |
michael@0 | 36 | **includedirsnext; |
michael@0 | 37 | |
michael@0 | 38 | static int deftype (char *line, struct filepointer *filep, |
michael@0 | 39 | struct inclist *file_red, struct inclist *file, |
michael@0 | 40 | int parse_it); |
michael@0 | 41 | static int zero_value(char *filename, char *exp, struct filepointer *filep, |
michael@0 | 42 | struct inclist *file_red); |
michael@0 | 43 | static int merge2defines(struct inclist *file1, struct inclist *file2); |
michael@0 | 44 | |
michael@0 | 45 | static int |
michael@0 | 46 | gobble(struct filepointer *filep, struct inclist *file, |
michael@0 | 47 | struct inclist *file_red) |
michael@0 | 48 | { |
michael@0 | 49 | char *line; |
michael@0 | 50 | int type; |
michael@0 | 51 | |
michael@0 | 52 | while ((line = getnextline(filep))) { |
michael@0 | 53 | switch(type = deftype(line, filep, file_red, file, FALSE)) { |
michael@0 | 54 | case IF: |
michael@0 | 55 | case IFFALSE: |
michael@0 | 56 | case IFGUESSFALSE: |
michael@0 | 57 | case IFDEF: |
michael@0 | 58 | case IFNDEF: |
michael@0 | 59 | type = gobble(filep, file, file_red); |
michael@0 | 60 | while ((type == ELIF) || (type == ELIFFALSE) || |
michael@0 | 61 | (type == ELIFGUESSFALSE)) |
michael@0 | 62 | type = gobble(filep, file, file_red); |
michael@0 | 63 | if (type == ELSE) |
michael@0 | 64 | (void)gobble(filep, file, file_red); |
michael@0 | 65 | break; |
michael@0 | 66 | case ELSE: |
michael@0 | 67 | case ENDIF: |
michael@0 | 68 | debug(0,("%s, line %d: #%s\n", |
michael@0 | 69 | file->i_file, filep->f_line, |
michael@0 | 70 | directives[type])); |
michael@0 | 71 | return(type); |
michael@0 | 72 | case DEFINE: |
michael@0 | 73 | case UNDEF: |
michael@0 | 74 | case INCLUDE: |
michael@0 | 75 | case INCLUDEDOT: |
michael@0 | 76 | case PRAGMA: |
michael@0 | 77 | case ERROR: |
michael@0 | 78 | case IDENT: |
michael@0 | 79 | case SCCS: |
michael@0 | 80 | case EJECT: |
michael@0 | 81 | case WARNING: |
michael@0 | 82 | case INCLUDENEXT: |
michael@0 | 83 | case INCLUDENEXTDOT: |
michael@0 | 84 | break; |
michael@0 | 85 | case ELIF: |
michael@0 | 86 | case ELIFFALSE: |
michael@0 | 87 | case ELIFGUESSFALSE: |
michael@0 | 88 | return(type); |
michael@0 | 89 | case -1: |
michael@0 | 90 | warning("%s", file_red->i_file); |
michael@0 | 91 | if (file_red != file) |
michael@0 | 92 | warning1(" (reading %s)", file->i_file); |
michael@0 | 93 | warning1(", line %d: unknown directive == \"%s\"\n", |
michael@0 | 94 | filep->f_line, line); |
michael@0 | 95 | break; |
michael@0 | 96 | } |
michael@0 | 97 | } |
michael@0 | 98 | return(-1); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | /* |
michael@0 | 102 | * Decide what type of # directive this line is. |
michael@0 | 103 | */ |
michael@0 | 104 | static int |
michael@0 | 105 | deftype (char *line, struct filepointer *filep, |
michael@0 | 106 | struct inclist *file_red, struct inclist *file, int parse_it) |
michael@0 | 107 | { |
michael@0 | 108 | register char *p; |
michael@0 | 109 | char *directive, savechar, *q; |
michael@0 | 110 | register int ret; |
michael@0 | 111 | |
michael@0 | 112 | /* |
michael@0 | 113 | * Parse the directive... |
michael@0 | 114 | */ |
michael@0 | 115 | directive=line+1; |
michael@0 | 116 | while (*directive == ' ' || *directive == '\t') |
michael@0 | 117 | directive++; |
michael@0 | 118 | |
michael@0 | 119 | p = directive; |
michael@0 | 120 | while ((*p == '_') || (*p >= 'a' && *p <= 'z')) |
michael@0 | 121 | p++; |
michael@0 | 122 | savechar = *p; |
michael@0 | 123 | *p = '\0'; |
michael@0 | 124 | ret = match(directive, directives); |
michael@0 | 125 | *p = savechar; |
michael@0 | 126 | |
michael@0 | 127 | /* If we don't recognize this compiler directive or we happen to just |
michael@0 | 128 | * be gobbling up text while waiting for an #endif or #elif or #else |
michael@0 | 129 | * in the case of an #elif we must check the zero_value and return an |
michael@0 | 130 | * ELIF or an ELIFFALSE. |
michael@0 | 131 | */ |
michael@0 | 132 | |
michael@0 | 133 | if (ret == ELIF && !parse_it) |
michael@0 | 134 | { |
michael@0 | 135 | while (*p == ' ' || *p == '\t') |
michael@0 | 136 | p++; |
michael@0 | 137 | /* |
michael@0 | 138 | * parse an expression. |
michael@0 | 139 | */ |
michael@0 | 140 | debug(0,("%s, line %d: #elif %s ", |
michael@0 | 141 | file->i_file, filep->f_line, p)); |
michael@0 | 142 | ret = zero_value(file->i_file, p, filep, file_red); |
michael@0 | 143 | if (ret != IF) |
michael@0 | 144 | { |
michael@0 | 145 | debug(0,("false...\n")); |
michael@0 | 146 | if (ret == IFFALSE) |
michael@0 | 147 | return(ELIFFALSE); |
michael@0 | 148 | else |
michael@0 | 149 | return(ELIFGUESSFALSE); |
michael@0 | 150 | } |
michael@0 | 151 | else |
michael@0 | 152 | { |
michael@0 | 153 | debug(0,("true...\n")); |
michael@0 | 154 | return(ELIF); |
michael@0 | 155 | } |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | if (ret < 0 || ! parse_it) |
michael@0 | 159 | return(ret); |
michael@0 | 160 | |
michael@0 | 161 | /* |
michael@0 | 162 | * now decide how to parse the directive, and do it. |
michael@0 | 163 | */ |
michael@0 | 164 | while (*p == ' ' || *p == '\t') |
michael@0 | 165 | p++; |
michael@0 | 166 | q = p + strlen(p); |
michael@0 | 167 | do { |
michael@0 | 168 | q--; |
michael@0 | 169 | } while (*q == ' ' || *q == '\t'); |
michael@0 | 170 | q[1] = '\0'; |
michael@0 | 171 | switch (ret) { |
michael@0 | 172 | case IF: |
michael@0 | 173 | /* |
michael@0 | 174 | * parse an expression. |
michael@0 | 175 | */ |
michael@0 | 176 | ret = zero_value(file->i_file, p, filep, file_red); |
michael@0 | 177 | debug(0,("%s, line %d: %s #if %s\n", |
michael@0 | 178 | file->i_file, filep->f_line, ret?"false":"true", p)); |
michael@0 | 179 | break; |
michael@0 | 180 | case IFDEF: |
michael@0 | 181 | case IFNDEF: |
michael@0 | 182 | debug(0,("%s, line %d: #%s %s\n", |
michael@0 | 183 | file->i_file, filep->f_line, directives[ret], p)); |
michael@0 | 184 | case UNDEF: |
michael@0 | 185 | /* |
michael@0 | 186 | * separate the name of a single symbol. |
michael@0 | 187 | */ |
michael@0 | 188 | while (isalnum(*p) || *p == '_') |
michael@0 | 189 | *line++ = *p++; |
michael@0 | 190 | *line = '\0'; |
michael@0 | 191 | break; |
michael@0 | 192 | case INCLUDE: |
michael@0 | 193 | case INCLUDENEXT: |
michael@0 | 194 | debug(2,("%s, line %d: #include%s %s\n", |
michael@0 | 195 | file->i_file, filep->f_line, |
michael@0 | 196 | (ret == INCLUDE) ? "" : "_next", p)); |
michael@0 | 197 | |
michael@0 | 198 | /* Support ANSI macro substitution */ |
michael@0 | 199 | while (1) { |
michael@0 | 200 | struct symtab **sym; |
michael@0 | 201 | |
michael@0 | 202 | if (!*p || *p == '"' || *p == '<') |
michael@0 | 203 | break; |
michael@0 | 204 | |
michael@0 | 205 | sym = isdefined(p, file_red, NULL); |
michael@0 | 206 | if (!sym) |
michael@0 | 207 | break; |
michael@0 | 208 | |
michael@0 | 209 | p = (*sym)->s_value; |
michael@0 | 210 | debug(3,("%s : #includes SYMBOL %s = %s\n", |
michael@0 | 211 | file->i_incstring, |
michael@0 | 212 | (*sym) -> s_name, |
michael@0 | 213 | (*sym) -> s_value)); |
michael@0 | 214 | /* mark file as having included a 'soft include' */ |
michael@0 | 215 | file->i_flags |= INCLUDED_SYM; |
michael@0 | 216 | } |
michael@0 | 217 | |
michael@0 | 218 | /* |
michael@0 | 219 | * Separate the name of the include file. |
michael@0 | 220 | */ |
michael@0 | 221 | while (*p && *p != '"' && *p != '<') |
michael@0 | 222 | p++; |
michael@0 | 223 | if (! *p) |
michael@0 | 224 | return(-2); |
michael@0 | 225 | if (*p++ == '"') { |
michael@0 | 226 | if (ret == INCLUDE) |
michael@0 | 227 | ret = INCLUDEDOT; |
michael@0 | 228 | else |
michael@0 | 229 | ret = INCLUDENEXTDOT; |
michael@0 | 230 | while (*p && *p != '"') |
michael@0 | 231 | *line++ = *p++; |
michael@0 | 232 | } else |
michael@0 | 233 | while (*p && *p != '>') |
michael@0 | 234 | *line++ = *p++; |
michael@0 | 235 | *line = '\0'; |
michael@0 | 236 | break; |
michael@0 | 237 | case DEFINE: |
michael@0 | 238 | /* |
michael@0 | 239 | * copy the definition back to the beginning of the line. |
michael@0 | 240 | */ |
michael@0 | 241 | strcpy (line, p); |
michael@0 | 242 | break; |
michael@0 | 243 | case ELSE: |
michael@0 | 244 | case ENDIF: |
michael@0 | 245 | case ELIF: |
michael@0 | 246 | case PRAGMA: |
michael@0 | 247 | case ERROR: |
michael@0 | 248 | case IDENT: |
michael@0 | 249 | case SCCS: |
michael@0 | 250 | case EJECT: |
michael@0 | 251 | case WARNING: |
michael@0 | 252 | debug(0,("%s, line %d: #%s\n", |
michael@0 | 253 | file->i_file, filep->f_line, directives[ret])); |
michael@0 | 254 | /* |
michael@0 | 255 | * nothing to do. |
michael@0 | 256 | */ |
michael@0 | 257 | break; |
michael@0 | 258 | } |
michael@0 | 259 | return(ret); |
michael@0 | 260 | } |
michael@0 | 261 | |
michael@0 | 262 | struct symtab ** |
michael@0 | 263 | fdefined(char *symbol, struct inclist *file, struct inclist **srcfile) |
michael@0 | 264 | { |
michael@0 | 265 | struct inclist **ip; |
michael@0 | 266 | struct symtab **val; |
michael@0 | 267 | int i; |
michael@0 | 268 | static int recurse_lvl = 0; |
michael@0 | 269 | |
michael@0 | 270 | if (file->i_flags & DEFCHECKED) |
michael@0 | 271 | return(NULL); |
michael@0 | 272 | debug(2,("Looking for %s in %s\n", symbol, file->i_file)); |
michael@0 | 273 | file->i_flags |= DEFCHECKED; |
michael@0 | 274 | if ((val = slookup(symbol, file))) |
michael@0 | 275 | debug(1,("%s defined in %s as %s\n", |
michael@0 | 276 | symbol, file->i_file, (*val)->s_value)); |
michael@0 | 277 | if (val == NULL && file->i_list) |
michael@0 | 278 | { |
michael@0 | 279 | for (ip = file->i_list, i=0; i < file->i_listlen; i++, ip++) |
michael@0 | 280 | if (file->i_merged[i]==FALSE) { |
michael@0 | 281 | val = fdefined(symbol, *ip, srcfile); |
michael@0 | 282 | file->i_merged[i]=merge2defines(file,*ip); |
michael@0 | 283 | if (val!=NULL) break; |
michael@0 | 284 | } |
michael@0 | 285 | } |
michael@0 | 286 | else if (val != NULL && srcfile != NULL) *srcfile = file; |
michael@0 | 287 | recurse_lvl--; |
michael@0 | 288 | file->i_flags &= ~DEFCHECKED; |
michael@0 | 289 | |
michael@0 | 290 | return(val); |
michael@0 | 291 | } |
michael@0 | 292 | |
michael@0 | 293 | struct symtab ** |
michael@0 | 294 | isdefined(char *symbol, struct inclist *file, struct inclist **srcfile) |
michael@0 | 295 | { |
michael@0 | 296 | struct symtab **val; |
michael@0 | 297 | |
michael@0 | 298 | if ((val = slookup(symbol, &maininclist))) { |
michael@0 | 299 | debug(1,("%s defined on command line\n", symbol)); |
michael@0 | 300 | if (srcfile != NULL) *srcfile = &maininclist; |
michael@0 | 301 | return(val); |
michael@0 | 302 | } |
michael@0 | 303 | if ((val = fdefined(symbol, file, srcfile))) |
michael@0 | 304 | return(val); |
michael@0 | 305 | debug(1,("%s not defined in %s\n", symbol, file->i_file)); |
michael@0 | 306 | return(NULL); |
michael@0 | 307 | } |
michael@0 | 308 | |
michael@0 | 309 | /* |
michael@0 | 310 | * Return type based on if the #if expression evaluates to 0 |
michael@0 | 311 | */ |
michael@0 | 312 | static int |
michael@0 | 313 | zero_value(char *filename, |
michael@0 | 314 | char *exp, |
michael@0 | 315 | struct filepointer *filep, |
michael@0 | 316 | struct inclist *file_red) |
michael@0 | 317 | { |
michael@0 | 318 | if (cppsetup(filename, exp, filep, file_red)) |
michael@0 | 319 | return(IFFALSE); |
michael@0 | 320 | else |
michael@0 | 321 | return(IF); |
michael@0 | 322 | } |
michael@0 | 323 | |
michael@0 | 324 | void |
michael@0 | 325 | define2(char *name, char *val, struct inclist *file) |
michael@0 | 326 | { |
michael@0 | 327 | int first, last, below; |
michael@0 | 328 | register struct symtab **sp = NULL, **dest; |
michael@0 | 329 | struct symtab *stab; |
michael@0 | 330 | |
michael@0 | 331 | /* Make space if it's needed */ |
michael@0 | 332 | if (file->i_defs == NULL) |
michael@0 | 333 | { |
michael@0 | 334 | file->i_defs = (struct symtab **) |
michael@0 | 335 | malloc(sizeof (struct symtab*) * SYMTABINC); |
michael@0 | 336 | file->i_ndefs = 0; |
michael@0 | 337 | } |
michael@0 | 338 | else if (!(file->i_ndefs % SYMTABINC)) |
michael@0 | 339 | file->i_defs = (struct symtab **) |
michael@0 | 340 | realloc(file->i_defs, |
michael@0 | 341 | sizeof(struct symtab*)*(file->i_ndefs+SYMTABINC)); |
michael@0 | 342 | |
michael@0 | 343 | if (file->i_defs == NULL) |
michael@0 | 344 | fatalerr("malloc()/realloc() failure in insert_defn()\n"); |
michael@0 | 345 | |
michael@0 | 346 | below = first = 0; |
michael@0 | 347 | last = file->i_ndefs - 1; |
michael@0 | 348 | while (last >= first) |
michael@0 | 349 | { |
michael@0 | 350 | /* Fast inline binary search */ |
michael@0 | 351 | register char *s1; |
michael@0 | 352 | register char *s2; |
michael@0 | 353 | register int middle = (first + last) / 2; |
michael@0 | 354 | |
michael@0 | 355 | /* Fast inline strchr() */ |
michael@0 | 356 | s1 = name; |
michael@0 | 357 | s2 = file->i_defs[middle]->s_name; |
michael@0 | 358 | while (*s1++ == *s2++) |
michael@0 | 359 | if (s2[-1] == '\0') break; |
michael@0 | 360 | |
michael@0 | 361 | /* If exact match, set sp and break */ |
michael@0 | 362 | if (*--s1 == *--s2) |
michael@0 | 363 | { |
michael@0 | 364 | sp = file->i_defs + middle; |
michael@0 | 365 | break; |
michael@0 | 366 | } |
michael@0 | 367 | |
michael@0 | 368 | /* If name > i_defs[middle] ... */ |
michael@0 | 369 | if (*s1 > *s2) |
michael@0 | 370 | { |
michael@0 | 371 | below = first; |
michael@0 | 372 | first = middle + 1; |
michael@0 | 373 | } |
michael@0 | 374 | /* else ... */ |
michael@0 | 375 | else |
michael@0 | 376 | { |
michael@0 | 377 | below = last = middle - 1; |
michael@0 | 378 | } |
michael@0 | 379 | } |
michael@0 | 380 | |
michael@0 | 381 | /* Search is done. If we found an exact match to the symbol name, |
michael@0 | 382 | just replace its s_value */ |
michael@0 | 383 | if (sp != NULL) |
michael@0 | 384 | { |
michael@0 | 385 | debug(1,("redefining %s from %s to %s in file %s\n", |
michael@0 | 386 | name, (*sp)->s_value, val, file->i_file)); |
michael@0 | 387 | free((*sp)->s_value); |
michael@0 | 388 | (*sp)->s_value = copy(val); |
michael@0 | 389 | return; |
michael@0 | 390 | } |
michael@0 | 391 | |
michael@0 | 392 | sp = file->i_defs + file->i_ndefs++; |
michael@0 | 393 | dest = file->i_defs + below + 1; |
michael@0 | 394 | while (sp > dest) |
michael@0 | 395 | { |
michael@0 | 396 | *sp = sp[-1]; |
michael@0 | 397 | sp--; |
michael@0 | 398 | } |
michael@0 | 399 | stab = (struct symtab *) malloc(sizeof (struct symtab)); |
michael@0 | 400 | if (stab == NULL) |
michael@0 | 401 | fatalerr("malloc()/realloc() failure in insert_defn()\n"); |
michael@0 | 402 | |
michael@0 | 403 | debug(1,("defining %s to %s in file %s\n", name, val, file->i_file)); |
michael@0 | 404 | stab->s_name = copy(name); |
michael@0 | 405 | stab->s_value = copy(val); |
michael@0 | 406 | *sp = stab; |
michael@0 | 407 | } |
michael@0 | 408 | |
michael@0 | 409 | void |
michael@0 | 410 | define(char *def, struct inclist *file) |
michael@0 | 411 | { |
michael@0 | 412 | char *val; |
michael@0 | 413 | |
michael@0 | 414 | /* Separate symbol name and its value */ |
michael@0 | 415 | val = def; |
michael@0 | 416 | while (isalnum(*val) || *val == '_') |
michael@0 | 417 | val++; |
michael@0 | 418 | if (*val) |
michael@0 | 419 | *val++ = '\0'; |
michael@0 | 420 | while (*val == ' ' || *val == '\t') |
michael@0 | 421 | val++; |
michael@0 | 422 | |
michael@0 | 423 | if (!*val) |
michael@0 | 424 | val = "1"; |
michael@0 | 425 | define2(def, val, file); |
michael@0 | 426 | } |
michael@0 | 427 | |
michael@0 | 428 | struct symtab ** |
michael@0 | 429 | slookup(char *symbol, struct inclist *file) |
michael@0 | 430 | { |
michael@0 | 431 | register int first = 0; |
michael@0 | 432 | register int last = file->i_ndefs - 1; |
michael@0 | 433 | |
michael@0 | 434 | if (file) while (last >= first) |
michael@0 | 435 | { |
michael@0 | 436 | /* Fast inline binary search */ |
michael@0 | 437 | register char *s1; |
michael@0 | 438 | register char *s2; |
michael@0 | 439 | register int middle = (first + last) / 2; |
michael@0 | 440 | |
michael@0 | 441 | /* Fast inline strchr() */ |
michael@0 | 442 | s1 = symbol; |
michael@0 | 443 | s2 = file->i_defs[middle]->s_name; |
michael@0 | 444 | while (*s1++ == *s2++) |
michael@0 | 445 | if (s2[-1] == '\0') break; |
michael@0 | 446 | |
michael@0 | 447 | /* If exact match, we're done */ |
michael@0 | 448 | if (*--s1 == *--s2) |
michael@0 | 449 | { |
michael@0 | 450 | return file->i_defs + middle; |
michael@0 | 451 | } |
michael@0 | 452 | |
michael@0 | 453 | /* If symbol > i_defs[middle] ... */ |
michael@0 | 454 | if (*s1 > *s2) |
michael@0 | 455 | { |
michael@0 | 456 | first = middle + 1; |
michael@0 | 457 | } |
michael@0 | 458 | /* else ... */ |
michael@0 | 459 | else |
michael@0 | 460 | { |
michael@0 | 461 | last = middle - 1; |
michael@0 | 462 | } |
michael@0 | 463 | } |
michael@0 | 464 | return(NULL); |
michael@0 | 465 | } |
michael@0 | 466 | |
michael@0 | 467 | static int |
michael@0 | 468 | merge2defines(struct inclist *file1, struct inclist *file2) |
michael@0 | 469 | { |
michael@0 | 470 | int i; |
michael@0 | 471 | |
michael@0 | 472 | if ((file1==NULL) || (file2==NULL) || |
michael@0 | 473 | !(file2->i_flags & FINISHED)) |
michael@0 | 474 | return 0; |
michael@0 | 475 | |
michael@0 | 476 | for (i=0; i < file2->i_listlen; i++) |
michael@0 | 477 | if (file2->i_merged[i]==FALSE) |
michael@0 | 478 | return 0; |
michael@0 | 479 | |
michael@0 | 480 | { |
michael@0 | 481 | int first1 = 0; |
michael@0 | 482 | int last1 = file1->i_ndefs - 1; |
michael@0 | 483 | |
michael@0 | 484 | int first2 = 0; |
michael@0 | 485 | int last2 = file2->i_ndefs - 1; |
michael@0 | 486 | |
michael@0 | 487 | int first=0; |
michael@0 | 488 | struct symtab** i_defs = NULL; |
michael@0 | 489 | int deflen=file1->i_ndefs+file2->i_ndefs; |
michael@0 | 490 | |
michael@0 | 491 | debug(2,("merging %s into %s\n", |
michael@0 | 492 | file2->i_file, file1->i_file)); |
michael@0 | 493 | |
michael@0 | 494 | if (deflen>0) |
michael@0 | 495 | { |
michael@0 | 496 | /* make sure deflen % SYMTABINC == 0 is still true */ |
michael@0 | 497 | deflen += (SYMTABINC - deflen % SYMTABINC) % SYMTABINC; |
michael@0 | 498 | i_defs=(struct symtab**) |
michael@0 | 499 | malloc(deflen*sizeof(struct symtab*)); |
michael@0 | 500 | if (i_defs==NULL) return 0; |
michael@0 | 501 | } |
michael@0 | 502 | |
michael@0 | 503 | while ((last1 >= first1) && (last2 >= first2)) |
michael@0 | 504 | { |
michael@0 | 505 | char *s1=file1->i_defs[first1]->s_name; |
michael@0 | 506 | char *s2=file2->i_defs[first2]->s_name; |
michael@0 | 507 | |
michael@0 | 508 | if (strcmp(s1,s2) < 0) |
michael@0 | 509 | i_defs[first++]=file1->i_defs[first1++]; |
michael@0 | 510 | else if (strcmp(s1,s2) > 0) |
michael@0 | 511 | i_defs[first++]=file2->i_defs[first2++]; |
michael@0 | 512 | else /* equal */ |
michael@0 | 513 | { |
michael@0 | 514 | i_defs[first++]=file2->i_defs[first2++]; |
michael@0 | 515 | first1++; |
michael@0 | 516 | } |
michael@0 | 517 | } |
michael@0 | 518 | while (last1 >= first1) |
michael@0 | 519 | { |
michael@0 | 520 | i_defs[first++]=file1->i_defs[first1++]; |
michael@0 | 521 | } |
michael@0 | 522 | while (last2 >= first2) |
michael@0 | 523 | { |
michael@0 | 524 | i_defs[first++]=file2->i_defs[first2++]; |
michael@0 | 525 | } |
michael@0 | 526 | |
michael@0 | 527 | if (file1->i_defs) free(file1->i_defs); |
michael@0 | 528 | file1->i_defs=i_defs; |
michael@0 | 529 | file1->i_ndefs=first; |
michael@0 | 530 | |
michael@0 | 531 | return 1; |
michael@0 | 532 | } |
michael@0 | 533 | } |
michael@0 | 534 | |
michael@0 | 535 | void |
michael@0 | 536 | undefine(char *symbol, struct inclist *file) |
michael@0 | 537 | { |
michael@0 | 538 | register struct symtab **ptr; |
michael@0 | 539 | struct inclist *srcfile; |
michael@0 | 540 | while ((ptr = isdefined(symbol, file, &srcfile)) != NULL) |
michael@0 | 541 | { |
michael@0 | 542 | srcfile->i_ndefs--; |
michael@0 | 543 | for (; ptr < srcfile->i_defs + srcfile->i_ndefs; ptr++) |
michael@0 | 544 | *ptr = ptr[1]; |
michael@0 | 545 | } |
michael@0 | 546 | } |
michael@0 | 547 | |
michael@0 | 548 | int |
michael@0 | 549 | find_includes(struct filepointer *filep, struct inclist *file, |
michael@0 | 550 | struct inclist *file_red, int recursion, boolean failOK) |
michael@0 | 551 | { |
michael@0 | 552 | struct inclist *inclistp; |
michael@0 | 553 | char **includedirsp; |
michael@0 | 554 | register char *line; |
michael@0 | 555 | register int type; |
michael@0 | 556 | boolean recfailOK; |
michael@0 | 557 | |
michael@0 | 558 | while ((line = getnextline(filep))) { |
michael@0 | 559 | switch(type = deftype(line, filep, file_red, file, TRUE)) { |
michael@0 | 560 | case IF: |
michael@0 | 561 | doif: |
michael@0 | 562 | type = find_includes(filep, file, |
michael@0 | 563 | file_red, recursion+1, failOK); |
michael@0 | 564 | while ((type == ELIF) || (type == ELIFFALSE) || |
michael@0 | 565 | (type == ELIFGUESSFALSE)) |
michael@0 | 566 | type = gobble(filep, file, file_red); |
michael@0 | 567 | if (type == ELSE) |
michael@0 | 568 | gobble(filep, file, file_red); |
michael@0 | 569 | break; |
michael@0 | 570 | case IFFALSE: |
michael@0 | 571 | case IFGUESSFALSE: |
michael@0 | 572 | doiffalse: |
michael@0 | 573 | if (type == IFGUESSFALSE || type == ELIFGUESSFALSE) |
michael@0 | 574 | recfailOK = TRUE; |
michael@0 | 575 | else |
michael@0 | 576 | recfailOK = failOK; |
michael@0 | 577 | type = gobble(filep, file, file_red); |
michael@0 | 578 | if (type == ELSE) |
michael@0 | 579 | find_includes(filep, file, |
michael@0 | 580 | file_red, recursion+1, recfailOK); |
michael@0 | 581 | else |
michael@0 | 582 | if (type == ELIF) |
michael@0 | 583 | goto doif; |
michael@0 | 584 | else |
michael@0 | 585 | if ((type == ELIFFALSE) || (type == ELIFGUESSFALSE)) |
michael@0 | 586 | goto doiffalse; |
michael@0 | 587 | break; |
michael@0 | 588 | case IFDEF: |
michael@0 | 589 | case IFNDEF: |
michael@0 | 590 | if ((type == IFDEF && isdefined(line, file_red, NULL)) |
michael@0 | 591 | || (type == IFNDEF && !isdefined(line, file_red, NULL))) { |
michael@0 | 592 | debug(1,(type == IFNDEF ? |
michael@0 | 593 | "line %d: %s !def'd in %s via %s%s\n" : "", |
michael@0 | 594 | filep->f_line, line, |
michael@0 | 595 | file->i_file, file_red->i_file, ": doit")); |
michael@0 | 596 | type = find_includes(filep, file, |
michael@0 | 597 | file_red, recursion+1, failOK); |
michael@0 | 598 | while (type == ELIF || type == ELIFFALSE || type == ELIFGUESSFALSE) |
michael@0 | 599 | type = gobble(filep, file, file_red); |
michael@0 | 600 | if (type == ELSE) |
michael@0 | 601 | gobble(filep, file, file_red); |
michael@0 | 602 | } |
michael@0 | 603 | else { |
michael@0 | 604 | debug(1,(type == IFDEF ? |
michael@0 | 605 | "line %d: %s !def'd in %s via %s%s\n" : "", |
michael@0 | 606 | filep->f_line, line, |
michael@0 | 607 | file->i_file, file_red->i_file, ": gobble")); |
michael@0 | 608 | type = gobble(filep, file, file_red); |
michael@0 | 609 | if (type == ELSE) |
michael@0 | 610 | find_includes(filep, file, |
michael@0 | 611 | file_red, recursion+1, failOK); |
michael@0 | 612 | else if (type == ELIF) |
michael@0 | 613 | goto doif; |
michael@0 | 614 | else if (type == ELIFFALSE || type == ELIFGUESSFALSE) |
michael@0 | 615 | goto doiffalse; |
michael@0 | 616 | } |
michael@0 | 617 | break; |
michael@0 | 618 | case ELSE: |
michael@0 | 619 | case ELIFFALSE: |
michael@0 | 620 | case ELIFGUESSFALSE: |
michael@0 | 621 | case ELIF: |
michael@0 | 622 | if (!recursion) |
michael@0 | 623 | gobble(filep, file, file_red); |
michael@0 | 624 | case ENDIF: |
michael@0 | 625 | if (recursion) |
michael@0 | 626 | return(type); |
michael@0 | 627 | case DEFINE: |
michael@0 | 628 | define(line, file); |
michael@0 | 629 | break; |
michael@0 | 630 | case UNDEF: |
michael@0 | 631 | if (!*line) { |
michael@0 | 632 | warning("%s", file_red->i_file); |
michael@0 | 633 | if (file_red != file) |
michael@0 | 634 | warning1(" (reading %s)", file->i_file); |
michael@0 | 635 | warning1(", line %d: incomplete undef == \"%s\"\n", |
michael@0 | 636 | filep->f_line, line); |
michael@0 | 637 | break; |
michael@0 | 638 | } |
michael@0 | 639 | undefine(line, file_red); |
michael@0 | 640 | break; |
michael@0 | 641 | case INCLUDE: |
michael@0 | 642 | case INCLUDEDOT: |
michael@0 | 643 | case INCLUDENEXT: |
michael@0 | 644 | case INCLUDENEXTDOT: |
michael@0 | 645 | inclistp = inclistnext; |
michael@0 | 646 | includedirsp = includedirsnext; |
michael@0 | 647 | debug(2,("%s, reading %s, includes %s\n", |
michael@0 | 648 | file_red->i_file, file->i_file, line)); |
michael@0 | 649 | add_include(filep, file, file_red, line, type, failOK); |
michael@0 | 650 | inclistnext = inclistp; |
michael@0 | 651 | includedirsnext = includedirsp; |
michael@0 | 652 | break; |
michael@0 | 653 | case ERROR: |
michael@0 | 654 | case WARNING: |
michael@0 | 655 | warning("%s", file_red->i_file); |
michael@0 | 656 | if (file_red != file) |
michael@0 | 657 | warning1(" (reading %s)", file->i_file); |
michael@0 | 658 | warning1(", line %d: %s\n", |
michael@0 | 659 | filep->f_line, line); |
michael@0 | 660 | break; |
michael@0 | 661 | |
michael@0 | 662 | case PRAGMA: |
michael@0 | 663 | case IDENT: |
michael@0 | 664 | case SCCS: |
michael@0 | 665 | case EJECT: |
michael@0 | 666 | break; |
michael@0 | 667 | case -1: |
michael@0 | 668 | warning("%s", file_red->i_file); |
michael@0 | 669 | if (file_red != file) |
michael@0 | 670 | warning1(" (reading %s)", file->i_file); |
michael@0 | 671 | warning1(", line %d: unknown directive == \"%s\"\n", |
michael@0 | 672 | filep->f_line, line); |
michael@0 | 673 | break; |
michael@0 | 674 | case -2: |
michael@0 | 675 | warning("%s", file_red->i_file); |
michael@0 | 676 | if (file_red != file) |
michael@0 | 677 | warning1(" (reading %s)", file->i_file); |
michael@0 | 678 | warning1(", line %d: incomplete include == \"%s\"\n", |
michael@0 | 679 | filep->f_line, line); |
michael@0 | 680 | break; |
michael@0 | 681 | } |
michael@0 | 682 | } |
michael@0 | 683 | file->i_flags |= FINISHED; |
michael@0 | 684 | debug(2,("finished with %s\n", file->i_file)); |
michael@0 | 685 | return(-1); |
michael@0 | 686 | } |