1 /* $NetBSD: parse.c,v 1.209 2016/01/17 17:45:21 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1988, 1989, 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Adam de Boor. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 /* 36 * Copyright (c) 1989 by Berkeley Softworks 37 * All rights reserved. 38 * 39 * This code is derived from software contributed to Berkeley by 40 * Adam de Boor. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. All advertising materials mentioning features or use of this software 51 * must display the following acknowledgement: 52 * This product includes software developed by the University of 53 * California, Berkeley and its contributors. 54 * 4. Neither the name of the University nor the names of its contributors 55 * may be used to endorse or promote products derived from this software 56 * without specific prior written permission. 57 * 58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 68 * SUCH DAMAGE. 69 */ 70 71 #ifndef MAKE_NATIVE 72 static char rcsid[] = "$NetBSD: parse.c,v 1.209 2016/01/17 17:45:21 christos Exp $"; 73 #else 74 #include <sys/cdefs.h> 75 #ifndef lint 76 #if 0 77 static char sccsid[] = "@(#)parse.c 8.3 (Berkeley) 3/19/94"; 78 #else 79 __RCSID("$NetBSD: parse.c,v 1.209 2016/01/17 17:45:21 christos Exp $"); 80 #endif 81 #endif /* not lint */ 82 #endif 83 84 /*- 85 * parse.c -- 86 * Functions to parse a makefile. 87 * 88 * One function, Parse_Init, must be called before any functions 89 * in this module are used. After that, the function Parse_File is the 90 * main entry point and controls most of the other functions in this 91 * module. 92 * 93 * Most important structures are kept in Lsts. Directories for 94 * the .include "..." function are kept in the 'parseIncPath' Lst, while 95 * those for the .include <...> are kept in the 'sysIncPath' Lst. The 96 * targets currently being defined are kept in the 'targets' Lst. 97 * 98 * The variables 'fname' and 'lineno' are used to track the name 99 * of the current file and the line number in that file so that error 100 * messages can be more meaningful. 101 * 102 * Interface: 103 * Parse_Init Initialization function which must be 104 * called before anything else in this module 105 * is used. 106 * 107 * Parse_End Cleanup the module 108 * 109 * Parse_File Function used to parse a makefile. It must 110 * be given the name of the file, which should 111 * already have been opened, and a function 112 * to call to read a character from the file. 113 * 114 * Parse_IsVar Returns TRUE if the given line is a 115 * variable assignment. Used by MainParseArgs 116 * to determine if an argument is a target 117 * or a variable assignment. Used internally 118 * for pretty much the same thing... 119 * 120 * Parse_Error Function called when an error occurs in 121 * parsing. Used by the variable and 122 * conditional modules. 123 * Parse_MainName Returns a Lst of the main target to create. 124 */ 125 126 #include <sys/types.h> 127 #include <sys/mman.h> 128 #include <sys/stat.h> 129 #include <assert.h> 130 #include <ctype.h> 131 #include <errno.h> 132 #include <stdarg.h> 133 #include <stdio.h> 134 135 #ifndef MAP_FILE 136 #define MAP_FILE 0 137 #endif 138 #ifndef MAP_COPY 139 #define MAP_COPY MAP_PRIVATE 140 #endif 141 142 #include "make.h" 143 #include "hash.h" 144 #include "dir.h" 145 #include "job.h" 146 #include "buf.h" 147 #include "pathnames.h" 148 149 //////////////////////////////////////////////////////////// 150 // types and constants 151 152 /* 153 * Structure for a file being read ("included file") 154 */ 155 typedef struct IFile { 156 char *fname; /* name of file */ 157 int lineno; /* current line number in file */ 158 int first_lineno; /* line number of start of text */ 159 int cond_depth; /* 'if' nesting when file opened */ 160 char *P_str; /* point to base of string buffer */ 161 char *P_ptr; /* point to next char of string buffer */ 162 char *P_end; /* point to the end of string buffer */ 163 char *(*nextbuf)(void *, size_t *); /* Function to get more data */ 164 void *nextbuf_arg; /* Opaque arg for nextbuf() */ 165 struct loadedfile *lf; /* loadedfile object, if any */ 166 } IFile; 167 168 169 /* 170 * These values are returned by ParseEOF to tell Parse_File whether to 171 * CONTINUE parsing, i.e. it had only reached the end of an include file, 172 * or if it's DONE. 173 */ 174 #define CONTINUE 1 175 #define DONE 0 176 177 /* 178 * Tokens for target attributes 179 */ 180 typedef enum { 181 Begin, /* .BEGIN */ 182 Default, /* .DEFAULT */ 183 End, /* .END */ 184 dotError, /* .ERROR */ 185 Ignore, /* .IGNORE */ 186 Includes, /* .INCLUDES */ 187 Interrupt, /* .INTERRUPT */ 188 Libs, /* .LIBS */ 189 Meta, /* .META */ 190 MFlags, /* .MFLAGS or .MAKEFLAGS */ 191 Main, /* .MAIN and we don't have anything user-specified to 192 * make */ 193 NoExport, /* .NOEXPORT */ 194 NoMeta, /* .NOMETA */ 195 NoMetaCmp, /* .NOMETA_CMP */ 196 NoPath, /* .NOPATH */ 197 Not, /* Not special */ 198 NotParallel, /* .NOTPARALLEL */ 199 Null, /* .NULL */ 200 ExObjdir, /* .OBJDIR */ 201 Order, /* .ORDER */ 202 Parallel, /* .PARALLEL */ 203 ExPath, /* .PATH */ 204 Phony, /* .PHONY */ 205 #ifdef POSIX 206 Posix, /* .POSIX */ 207 #endif 208 Precious, /* .PRECIOUS */ 209 ExShell, /* .SHELL */ 210 Silent, /* .SILENT */ 211 SingleShell, /* .SINGLESHELL */ 212 Stale, /* .STALE */ 213 Suffixes, /* .SUFFIXES */ 214 Wait, /* .WAIT */ 215 Attribute /* Generic attribute */ 216 } ParseSpecial; 217 218 /* 219 * Other tokens 220 */ 221 #define LPAREN '(' 222 #define RPAREN ')' 223 224 225 //////////////////////////////////////////////////////////// 226 // result data 227 228 /* 229 * The main target to create. This is the first target on the first 230 * dependency line in the first makefile. 231 */ 232 static GNode *mainNode; 233 234 //////////////////////////////////////////////////////////// 235 // eval state 236 237 /* targets we're working on */ 238 static Lst targets; 239 240 #ifdef CLEANUP 241 /* command lines for targets */ 242 static Lst targCmds; 243 #endif 244 245 /* 246 * specType contains the SPECial TYPE of the current target. It is 247 * Not if the target is unspecial. If it *is* special, however, the children 248 * are linked as children of the parent but not vice versa. This variable is 249 * set in ParseDoDependency 250 */ 251 static ParseSpecial specType; 252 253 /* 254 * Predecessor node for handling .ORDER. Initialized to NULL when .ORDER 255 * seen, then set to each successive source on the line. 256 */ 257 static GNode *predecessor; 258 259 //////////////////////////////////////////////////////////// 260 // parser state 261 262 /* true if currently in a dependency line or its commands */ 263 static Boolean inLine; 264 265 /* number of fatal errors */ 266 static int fatals = 0; 267 268 /* 269 * Variables for doing includes 270 */ 271 272 /* current file being read */ 273 static IFile *curFile; 274 275 /* stack of IFiles generated by .includes */ 276 static Lst includes; 277 278 /* include paths (lists of directories) */ 279 Lst parseIncPath; /* dirs for "..." includes */ 280 Lst sysIncPath; /* dirs for <...> includes */ 281 Lst defIncPath; /* default for sysIncPath */ 282 283 //////////////////////////////////////////////////////////// 284 // parser tables 285 286 /* 287 * The parseKeywords table is searched using binary search when deciding 288 * if a target or source is special. The 'spec' field is the ParseSpecial 289 * type of the keyword ("Not" if the keyword isn't special as a target) while 290 * the 'op' field is the operator to apply to the list of targets if the 291 * keyword is used as a source ("0" if the keyword isn't special as a source) 292 */ 293 static const struct { 294 const char *name; /* Name of keyword */ 295 ParseSpecial spec; /* Type when used as a target */ 296 int op; /* Operator when used as a source */ 297 } parseKeywords[] = { 298 { ".BEGIN", Begin, 0 }, 299 { ".DEFAULT", Default, 0 }, 300 { ".END", End, 0 }, 301 { ".ERROR", dotError, 0 }, 302 { ".EXEC", Attribute, OP_EXEC }, 303 { ".IGNORE", Ignore, OP_IGNORE }, 304 { ".INCLUDES", Includes, 0 }, 305 { ".INTERRUPT", Interrupt, 0 }, 306 { ".INVISIBLE", Attribute, OP_INVISIBLE }, 307 { ".JOIN", Attribute, OP_JOIN }, 308 { ".LIBS", Libs, 0 }, 309 { ".MADE", Attribute, OP_MADE }, 310 { ".MAIN", Main, 0 }, 311 { ".MAKE", Attribute, OP_MAKE }, 312 { ".MAKEFLAGS", MFlags, 0 }, 313 { ".META", Meta, OP_META }, 314 { ".MFLAGS", MFlags, 0 }, 315 { ".NOMETA", NoMeta, OP_NOMETA }, 316 { ".NOMETA_CMP", NoMetaCmp, OP_NOMETA_CMP }, 317 { ".NOPATH", NoPath, OP_NOPATH }, 318 { ".NOTMAIN", Attribute, OP_NOTMAIN }, 319 { ".NOTPARALLEL", NotParallel, 0 }, 320 { ".NO_PARALLEL", NotParallel, 0 }, 321 { ".NULL", Null, 0 }, 322 { ".OBJDIR", ExObjdir, 0 }, 323 { ".OPTIONAL", Attribute, OP_OPTIONAL }, 324 { ".ORDER", Order, 0 }, 325 { ".PARALLEL", Parallel, 0 }, 326 { ".PATH", ExPath, 0 }, 327 { ".PHONY", Phony, OP_PHONY }, 328 #ifdef POSIX 329 { ".POSIX", Posix, 0 }, 330 #endif 331 { ".PRECIOUS", Precious, OP_PRECIOUS }, 332 { ".RECURSIVE", Attribute, OP_MAKE }, 333 { ".SHELL", ExShell, 0 }, 334 { ".SILENT", Silent, OP_SILENT }, 335 { ".SINGLESHELL", SingleShell, 0 }, 336 { ".STALE", Stale, 0 }, 337 { ".SUFFIXES", Suffixes, 0 }, 338 { ".USE", Attribute, OP_USE }, 339 { ".USEBEFORE", Attribute, OP_USEBEFORE }, 340 { ".WAIT", Wait, 0 }, 341 }; 342 343 //////////////////////////////////////////////////////////// 344 // local functions 345 346 static int ParseIsEscaped(const char *, const char *); 347 static void ParseErrorInternal(const char *, size_t, int, const char *, ...) 348 MAKE_ATTR_PRINTFLIKE(4,5); 349 static void ParseVErrorInternal(FILE *, const char *, size_t, int, const char *, va_list) 350 MAKE_ATTR_PRINTFLIKE(5, 0); 351 static int ParseFindKeyword(const char *); 352 static int ParseLinkSrc(void *, void *); 353 static int ParseDoOp(void *, void *); 354 static void ParseDoSrc(int, const char *); 355 static int ParseFindMain(void *, void *); 356 static int ParseAddDir(void *, void *); 357 static int ParseClearPath(void *, void *); 358 static void ParseDoDependency(char *); 359 static int ParseAddCmd(void *, void *); 360 static void ParseHasCommands(void *); 361 static void ParseDoInclude(char *); 362 static void ParseSetParseFile(const char *); 363 static void ParseSetIncludedFile(void); 364 #ifdef SYSVINCLUDE 365 static void ParseTraditionalInclude(char *); 366 #endif 367 #ifdef GMAKEEXPORT 368 static void ParseGmakeExport(char *); 369 #endif 370 static int ParseEOF(void); 371 static char *ParseReadLine(void); 372 static void ParseFinishLine(void); 373 static void ParseMark(GNode *); 374 375 //////////////////////////////////////////////////////////// 376 // file loader 377 378 struct loadedfile { 379 const char *path; /* name, for error reports */ 380 char *buf; /* contents buffer */ 381 size_t len; /* length of contents */ 382 size_t maplen; /* length of mmap area, or 0 */ 383 Boolean used; /* XXX: have we used the data yet */ 384 }; 385 386 /* 387 * Constructor/destructor for loadedfile 388 */ 389 static struct loadedfile * 390 loadedfile_create(const char *path) 391 { 392 struct loadedfile *lf; 393 394 lf = bmake_malloc(sizeof(*lf)); 395 lf->path = (path == NULL ? "(stdin)" : path); 396 lf->buf = NULL; 397 lf->len = 0; 398 lf->maplen = 0; 399 lf->used = FALSE; 400 return lf; 401 } 402 403 static void 404 loadedfile_destroy(struct loadedfile *lf) 405 { 406 if (lf->buf != NULL) { 407 if (lf->maplen > 0) { 408 munmap(lf->buf, lf->maplen); 409 } else { 410 free(lf->buf); 411 } 412 } 413 free(lf); 414 } 415 416 /* 417 * nextbuf() operation for loadedfile, as needed by the weird and twisted 418 * logic below. Once that's cleaned up, we can get rid of lf->used... 419 */ 420 static char * 421 loadedfile_nextbuf(void *x, size_t *len) 422 { 423 struct loadedfile *lf = x; 424 425 if (lf->used) { 426 return NULL; 427 } 428 lf->used = TRUE; 429 *len = lf->len; 430 return lf->buf; 431 } 432 433 /* 434 * Try to get the size of a file. 435 */ 436 static ReturnStatus 437 load_getsize(int fd, size_t *ret) 438 { 439 struct stat st; 440 441 if (fstat(fd, &st) < 0) { 442 return FAILURE; 443 } 444 445 if (!S_ISREG(st.st_mode)) { 446 return FAILURE; 447 } 448 449 /* 450 * st_size is an off_t, which is 64 bits signed; *ret is 451 * size_t, which might be 32 bits unsigned or 64 bits 452 * unsigned. Rather than being elaborate, just punt on 453 * files that are more than 2^31 bytes. We should never 454 * see a makefile that size in practice... 455 * 456 * While we're at it reject negative sizes too, just in case. 457 */ 458 if (st.st_size < 0 || st.st_size > 0x7fffffff) { 459 return FAILURE; 460 } 461 462 *ret = (size_t) st.st_size; 463 return SUCCESS; 464 } 465 466 /* 467 * Read in a file. 468 * 469 * Until the path search logic can be moved under here instead of 470 * being in the caller in another source file, we need to have the fd 471 * passed in already open. Bleh. 472 * 473 * If the path is NULL use stdin and (to insure against fd leaks) 474 * assert that the caller passed in -1. 475 */ 476 static struct loadedfile * 477 loadfile(const char *path, int fd) 478 { 479 struct loadedfile *lf; 480 long pagesize; 481 ssize_t result; 482 size_t bufpos; 483 484 lf = loadedfile_create(path); 485 486 if (path == NULL) { 487 assert(fd == -1); 488 fd = STDIN_FILENO; 489 } else { 490 #if 0 /* notyet */ 491 fd = open(path, O_RDONLY); 492 if (fd < 0) { 493 ... 494 Error("%s: %s", path, strerror(errno)); 495 exit(1); 496 } 497 #endif 498 } 499 500 if (load_getsize(fd, &lf->len) == SUCCESS) { 501 /* found a size, try mmap */ 502 pagesize = sysconf(_SC_PAGESIZE); 503 if (pagesize <= 0) { 504 pagesize = 0x1000; 505 } 506 /* round size up to a page */ 507 lf->maplen = pagesize * ((lf->len + pagesize - 1)/pagesize); 508 509 /* 510 * XXX hack for dealing with empty files; remove when 511 * we're no longer limited by interfacing to the old 512 * logic elsewhere in this file. 513 */ 514 if (lf->maplen == 0) { 515 lf->maplen = pagesize; 516 } 517 518 /* 519 * FUTURE: remove PROT_WRITE when the parser no longer 520 * needs to scribble on the input. 521 */ 522 lf->buf = mmap(NULL, lf->maplen, PROT_READ|PROT_WRITE, 523 MAP_FILE|MAP_COPY, fd, 0); 524 if (lf->buf != MAP_FAILED) { 525 /* succeeded */ 526 if (lf->len == lf->maplen && lf->buf[lf->len - 1] != '\n') { 527 char *b = malloc(lf->len + 1); 528 b[lf->len] = '\n'; 529 memcpy(b, lf->buf, lf->len++); 530 munmap(lf->buf, lf->maplen); 531 lf->maplen = 0; 532 lf->buf = b; 533 } 534 goto done; 535 } 536 } 537 538 /* cannot mmap; load the traditional way */ 539 540 lf->maplen = 0; 541 lf->len = 1024; 542 lf->buf = bmake_malloc(lf->len); 543 544 bufpos = 0; 545 while (1) { 546 assert(bufpos <= lf->len); 547 if (bufpos == lf->len) { 548 lf->len *= 2; 549 lf->buf = bmake_realloc(lf->buf, lf->len); 550 } 551 result = read(fd, lf->buf + bufpos, lf->len - bufpos); 552 if (result < 0) { 553 Error("%s: read error: %s", path, strerror(errno)); 554 exit(1); 555 } 556 if (result == 0) { 557 break; 558 } 559 bufpos += result; 560 } 561 assert(bufpos <= lf->len); 562 lf->len = bufpos; 563 564 /* truncate malloc region to actual length (maybe not useful) */ 565 if (lf->len > 0) { 566 lf->buf = bmake_realloc(lf->buf, lf->len); 567 } 568 569 done: 570 if (path != NULL) { 571 close(fd); 572 } 573 return lf; 574 } 575 576 //////////////////////////////////////////////////////////// 577 // old code 578 579 /*- 580 *---------------------------------------------------------------------- 581 * ParseIsEscaped -- 582 * Check if the current character is escaped on the current line 583 * 584 * Results: 585 * 0 if the character is not backslash escaped, 1 otherwise 586 * 587 * Side Effects: 588 * None 589 *---------------------------------------------------------------------- 590 */ 591 static int 592 ParseIsEscaped(const char *line, const char *c) 593 { 594 int active = 0; 595 for (;;) { 596 if (line == c) 597 return active; 598 if (*--c != '\\') 599 return active; 600 active = !active; 601 } 602 } 603 604 /*- 605 *---------------------------------------------------------------------- 606 * ParseFindKeyword -- 607 * Look in the table of keywords for one matching the given string. 608 * 609 * Input: 610 * str String to find 611 * 612 * Results: 613 * The index of the keyword, or -1 if it isn't there. 614 * 615 * Side Effects: 616 * None 617 *---------------------------------------------------------------------- 618 */ 619 static int 620 ParseFindKeyword(const char *str) 621 { 622 int start, end, cur; 623 int diff; 624 625 start = 0; 626 end = (sizeof(parseKeywords)/sizeof(parseKeywords[0])) - 1; 627 628 do { 629 cur = start + ((end - start) / 2); 630 diff = strcmp(str, parseKeywords[cur].name); 631 632 if (diff == 0) { 633 return (cur); 634 } else if (diff < 0) { 635 end = cur - 1; 636 } else { 637 start = cur + 1; 638 } 639 } while (start <= end); 640 return (-1); 641 } 642 643 /*- 644 * ParseVErrorInternal -- 645 * Error message abort function for parsing. Prints out the context 646 * of the error (line number and file) as well as the message with 647 * two optional arguments. 648 * 649 * Results: 650 * None 651 * 652 * Side Effects: 653 * "fatals" is incremented if the level is PARSE_FATAL. 654 */ 655 /* VARARGS */ 656 static void 657 ParseVErrorInternal(FILE *f, const char *cfname, size_t clineno, int type, 658 const char *fmt, va_list ap) 659 { 660 static Boolean fatal_warning_error_printed = FALSE; 661 662 (void)fprintf(f, "%s: ", progname); 663 664 if (cfname != NULL) { 665 (void)fprintf(f, "\""); 666 if (*cfname != '/' && strcmp(cfname, "(stdin)") != 0) { 667 char *cp; 668 const char *dir; 669 670 /* 671 * Nothing is more annoying than not knowing 672 * which Makefile is the culprit. 673 */ 674 dir = Var_Value(".PARSEDIR", VAR_GLOBAL, &cp); 675 if (dir == NULL || *dir == '\0' || 676 (*dir == '.' && dir[1] == '\0')) 677 dir = Var_Value(".CURDIR", VAR_GLOBAL, &cp); 678 if (dir == NULL) 679 dir = "."; 680 681 (void)fprintf(f, "%s/%s", dir, cfname); 682 } else 683 (void)fprintf(f, "%s", cfname); 684 685 (void)fprintf(f, "\" line %d: ", (int)clineno); 686 } 687 if (type == PARSE_WARNING) 688 (void)fprintf(f, "warning: "); 689 (void)vfprintf(f, fmt, ap); 690 (void)fprintf(f, "\n"); 691 (void)fflush(f); 692 if (type == PARSE_FATAL || parseWarnFatal) 693 fatals += 1; 694 if (parseWarnFatal && !fatal_warning_error_printed) { 695 Error("parsing warnings being treated as errors"); 696 fatal_warning_error_printed = TRUE; 697 } 698 } 699 700 /*- 701 * ParseErrorInternal -- 702 * Error function 703 * 704 * Results: 705 * None 706 * 707 * Side Effects: 708 * None 709 */ 710 /* VARARGS */ 711 static void 712 ParseErrorInternal(const char *cfname, size_t clineno, int type, 713 const char *fmt, ...) 714 { 715 va_list ap; 716 717 va_start(ap, fmt); 718 (void)fflush(stdout); 719 ParseVErrorInternal(stderr, cfname, clineno, type, fmt, ap); 720 va_end(ap); 721 722 if (debug_file != stderr && debug_file != stdout) { 723 va_start(ap, fmt); 724 ParseVErrorInternal(debug_file, cfname, clineno, type, fmt, ap); 725 va_end(ap); 726 } 727 } 728 729 /*- 730 * Parse_Error -- 731 * External interface to ParseErrorInternal; uses the default filename 732 * Line number. 733 * 734 * Results: 735 * None 736 * 737 * Side Effects: 738 * None 739 */ 740 /* VARARGS */ 741 void 742 Parse_Error(int type, const char *fmt, ...) 743 { 744 va_list ap; 745 const char *fname; 746 size_t lineno; 747 748 if (curFile == NULL) { 749 fname = NULL; 750 lineno = 0; 751 } else { 752 fname = curFile->fname; 753 lineno = curFile->lineno; 754 } 755 756 va_start(ap, fmt); 757 (void)fflush(stdout); 758 ParseVErrorInternal(stderr, fname, lineno, type, fmt, ap); 759 va_end(ap); 760 761 if (debug_file != stderr && debug_file != stdout) { 762 va_start(ap, fmt); 763 ParseVErrorInternal(debug_file, fname, lineno, type, fmt, ap); 764 va_end(ap); 765 } 766 } 767 768 769 /* 770 * ParseMessage 771 * Parse a .info .warning or .error directive 772 * 773 * The input is the line minus the ".". We substitute 774 * variables, print the message and exit(1) (for .error) or just print 775 * a warning if the directive is malformed. 776 */ 777 static Boolean 778 ParseMessage(char *line) 779 { 780 int mtype; 781 782 switch(*line) { 783 case 'i': 784 mtype = 0; 785 break; 786 case 'w': 787 mtype = PARSE_WARNING; 788 break; 789 case 'e': 790 mtype = PARSE_FATAL; 791 break; 792 default: 793 Parse_Error(PARSE_WARNING, "invalid syntax: \".%s\"", line); 794 return FALSE; 795 } 796 797 while (isalpha((u_char)*line)) 798 line++; 799 if (!isspace((u_char)*line)) 800 return FALSE; /* not for us */ 801 while (isspace((u_char)*line)) 802 line++; 803 804 line = Var_Subst(NULL, line, VAR_CMD, FALSE, TRUE, FALSE); 805 Parse_Error(mtype, "%s", line); 806 free(line); 807 808 if (mtype == PARSE_FATAL) { 809 /* Terminate immediately. */ 810 exit(1); 811 } 812 return TRUE; 813 } 814 815 /*- 816 *--------------------------------------------------------------------- 817 * ParseLinkSrc -- 818 * Link the parent node to its new child. Used in a Lst_ForEach by 819 * ParseDoDependency. If the specType isn't 'Not', the parent 820 * isn't linked as a parent of the child. 821 * 822 * Input: 823 * pgnp The parent node 824 * cgpn The child node 825 * 826 * Results: 827 * Always = 0 828 * 829 * Side Effects: 830 * New elements are added to the parents list of cgn and the 831 * children list of cgn. the unmade field of pgn is updated 832 * to reflect the additional child. 833 *--------------------------------------------------------------------- 834 */ 835 static int 836 ParseLinkSrc(void *pgnp, void *cgnp) 837 { 838 GNode *pgn = (GNode *)pgnp; 839 GNode *cgn = (GNode *)cgnp; 840 841 if ((pgn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (pgn->cohorts)) 842 pgn = (GNode *)Lst_Datum(Lst_Last(pgn->cohorts)); 843 (void)Lst_AtEnd(pgn->children, cgn); 844 if (specType == Not) 845 (void)Lst_AtEnd(cgn->parents, pgn); 846 pgn->unmade += 1; 847 if (DEBUG(PARSE)) { 848 fprintf(debug_file, "# %s: added child %s - %s\n", __func__, 849 pgn->name, cgn->name); 850 Targ_PrintNode(pgn, 0); 851 Targ_PrintNode(cgn, 0); 852 } 853 return (0); 854 } 855 856 /*- 857 *--------------------------------------------------------------------- 858 * ParseDoOp -- 859 * Apply the parsed operator to the given target node. Used in a 860 * Lst_ForEach call by ParseDoDependency once all targets have 861 * been found and their operator parsed. If the previous and new 862 * operators are incompatible, a major error is taken. 863 * 864 * Input: 865 * gnp The node to which the operator is to be applied 866 * opp The operator to apply 867 * 868 * Results: 869 * Always 0 870 * 871 * Side Effects: 872 * The type field of the node is altered to reflect any new bits in 873 * the op. 874 *--------------------------------------------------------------------- 875 */ 876 static int 877 ParseDoOp(void *gnp, void *opp) 878 { 879 GNode *gn = (GNode *)gnp; 880 int op = *(int *)opp; 881 /* 882 * If the dependency mask of the operator and the node don't match and 883 * the node has actually had an operator applied to it before, and 884 * the operator actually has some dependency information in it, complain. 885 */ 886 if (((op & OP_OPMASK) != (gn->type & OP_OPMASK)) && 887 !OP_NOP(gn->type) && !OP_NOP(op)) 888 { 889 Parse_Error(PARSE_FATAL, "Inconsistent operator for %s", gn->name); 890 return (1); 891 } 892 893 if ((op == OP_DOUBLEDEP) && ((gn->type & OP_OPMASK) == OP_DOUBLEDEP)) { 894 /* 895 * If the node was the object of a :: operator, we need to create a 896 * new instance of it for the children and commands on this dependency 897 * line. The new instance is placed on the 'cohorts' list of the 898 * initial one (note the initial one is not on its own cohorts list) 899 * and the new instance is linked to all parents of the initial 900 * instance. 901 */ 902 GNode *cohort; 903 904 /* 905 * Propagate copied bits to the initial node. They'll be propagated 906 * back to the rest of the cohorts later. 907 */ 908 gn->type |= op & ~OP_OPMASK; 909 910 cohort = Targ_FindNode(gn->name, TARG_NOHASH); 911 if (doing_depend) 912 ParseMark(cohort); 913 /* 914 * Make the cohort invisible as well to avoid duplicating it into 915 * other variables. True, parents of this target won't tend to do 916 * anything with their local variables, but better safe than 917 * sorry. (I think this is pointless now, since the relevant list 918 * traversals will no longer see this node anyway. -mycroft) 919 */ 920 cohort->type = op | OP_INVISIBLE; 921 (void)Lst_AtEnd(gn->cohorts, cohort); 922 cohort->centurion = gn; 923 gn->unmade_cohorts += 1; 924 snprintf(cohort->cohort_num, sizeof cohort->cohort_num, "#%d", 925 gn->unmade_cohorts); 926 } else { 927 /* 928 * We don't want to nuke any previous flags (whatever they were) so we 929 * just OR the new operator into the old 930 */ 931 gn->type |= op; 932 } 933 934 return (0); 935 } 936 937 /*- 938 *--------------------------------------------------------------------- 939 * ParseDoSrc -- 940 * Given the name of a source, figure out if it is an attribute 941 * and apply it to the targets if it is. Else decide if there is 942 * some attribute which should be applied *to* the source because 943 * of some special target and apply it if so. Otherwise, make the 944 * source be a child of the targets in the list 'targets' 945 * 946 * Input: 947 * tOp operator (if any) from special targets 948 * src name of the source to handle 949 * 950 * Results: 951 * None 952 * 953 * Side Effects: 954 * Operator bits may be added to the list of targets or to the source. 955 * The targets may have a new source added to their lists of children. 956 *--------------------------------------------------------------------- 957 */ 958 static void 959 ParseDoSrc(int tOp, const char *src) 960 { 961 GNode *gn = NULL; 962 static int wait_number = 0; 963 char wait_src[16]; 964 965 if (*src == '.' && isupper ((unsigned char)src[1])) { 966 int keywd = ParseFindKeyword(src); 967 if (keywd != -1) { 968 int op = parseKeywords[keywd].op; 969 if (op != 0) { 970 Lst_ForEach(targets, ParseDoOp, &op); 971 return; 972 } 973 if (parseKeywords[keywd].spec == Wait) { 974 /* 975 * We add a .WAIT node in the dependency list. 976 * After any dynamic dependencies (and filename globbing) 977 * have happened, it is given a dependency on the each 978 * previous child back to and previous .WAIT node. 979 * The next child won't be scheduled until the .WAIT node 980 * is built. 981 * We give each .WAIT node a unique name (mainly for diag). 982 */ 983 snprintf(wait_src, sizeof wait_src, ".WAIT_%u", ++wait_number); 984 gn = Targ_FindNode(wait_src, TARG_NOHASH); 985 if (doing_depend) 986 ParseMark(gn); 987 gn->type = OP_WAIT | OP_PHONY | OP_DEPENDS | OP_NOTMAIN; 988 Lst_ForEach(targets, ParseLinkSrc, gn); 989 return; 990 } 991 } 992 } 993 994 switch (specType) { 995 case Main: 996 /* 997 * If we have noted the existence of a .MAIN, it means we need 998 * to add the sources of said target to the list of things 999 * to create. The string 'src' is likely to be free, so we 1000 * must make a new copy of it. Note that this will only be 1001 * invoked if the user didn't specify a target on the command 1002 * line. This is to allow #ifmake's to succeed, or something... 1003 */ 1004 (void)Lst_AtEnd(create, bmake_strdup(src)); 1005 /* 1006 * Add the name to the .TARGETS variable as well, so the user can 1007 * employ that, if desired. 1008 */ 1009 Var_Append(".TARGETS", src, VAR_GLOBAL); 1010 return; 1011 1012 case Order: 1013 /* 1014 * Create proper predecessor/successor links between the previous 1015 * source and the current one. 1016 */ 1017 gn = Targ_FindNode(src, TARG_CREATE); 1018 if (doing_depend) 1019 ParseMark(gn); 1020 if (predecessor != NULL) { 1021 (void)Lst_AtEnd(predecessor->order_succ, gn); 1022 (void)Lst_AtEnd(gn->order_pred, predecessor); 1023 if (DEBUG(PARSE)) { 1024 fprintf(debug_file, "# %s: added Order dependency %s - %s\n", 1025 __func__, predecessor->name, gn->name); 1026 Targ_PrintNode(predecessor, 0); 1027 Targ_PrintNode(gn, 0); 1028 } 1029 } 1030 /* 1031 * The current source now becomes the predecessor for the next one. 1032 */ 1033 predecessor = gn; 1034 break; 1035 1036 default: 1037 /* 1038 * If the source is not an attribute, we need to find/create 1039 * a node for it. After that we can apply any operator to it 1040 * from a special target or link it to its parents, as 1041 * appropriate. 1042 * 1043 * In the case of a source that was the object of a :: operator, 1044 * the attribute is applied to all of its instances (as kept in 1045 * the 'cohorts' list of the node) or all the cohorts are linked 1046 * to all the targets. 1047 */ 1048 1049 /* Find/create the 'src' node and attach to all targets */ 1050 gn = Targ_FindNode(src, TARG_CREATE); 1051 if (doing_depend) 1052 ParseMark(gn); 1053 if (tOp) { 1054 gn->type |= tOp; 1055 } else { 1056 Lst_ForEach(targets, ParseLinkSrc, gn); 1057 } 1058 break; 1059 } 1060 } 1061 1062 /*- 1063 *----------------------------------------------------------------------- 1064 * ParseFindMain -- 1065 * Find a real target in the list and set it to be the main one. 1066 * Called by ParseDoDependency when a main target hasn't been found 1067 * yet. 1068 * 1069 * Input: 1070 * gnp Node to examine 1071 * 1072 * Results: 1073 * 0 if main not found yet, 1 if it is. 1074 * 1075 * Side Effects: 1076 * mainNode is changed and Targ_SetMain is called. 1077 * 1078 *----------------------------------------------------------------------- 1079 */ 1080 static int 1081 ParseFindMain(void *gnp, void *dummy) 1082 { 1083 GNode *gn = (GNode *)gnp; 1084 if ((gn->type & OP_NOTARGET) == 0) { 1085 mainNode = gn; 1086 Targ_SetMain(gn); 1087 return (dummy ? 1 : 1); 1088 } else { 1089 return (dummy ? 0 : 0); 1090 } 1091 } 1092 1093 /*- 1094 *----------------------------------------------------------------------- 1095 * ParseAddDir -- 1096 * Front-end for Dir_AddDir to make sure Lst_ForEach keeps going 1097 * 1098 * Results: 1099 * === 0 1100 * 1101 * Side Effects: 1102 * See Dir_AddDir. 1103 * 1104 *----------------------------------------------------------------------- 1105 */ 1106 static int 1107 ParseAddDir(void *path, void *name) 1108 { 1109 (void)Dir_AddDir((Lst) path, (char *)name); 1110 return(0); 1111 } 1112 1113 /*- 1114 *----------------------------------------------------------------------- 1115 * ParseClearPath -- 1116 * Front-end for Dir_ClearPath to make sure Lst_ForEach keeps going 1117 * 1118 * Results: 1119 * === 0 1120 * 1121 * Side Effects: 1122 * See Dir_ClearPath 1123 * 1124 *----------------------------------------------------------------------- 1125 */ 1126 static int 1127 ParseClearPath(void *path, void *dummy) 1128 { 1129 Dir_ClearPath((Lst) path); 1130 return(dummy ? 0 : 0); 1131 } 1132 1133 /*- 1134 *--------------------------------------------------------------------- 1135 * ParseDoDependency -- 1136 * Parse the dependency line in line. 1137 * 1138 * Input: 1139 * line the line to parse 1140 * 1141 * Results: 1142 * None 1143 * 1144 * Side Effects: 1145 * The nodes of the sources are linked as children to the nodes of the 1146 * targets. Some nodes may be created. 1147 * 1148 * We parse a dependency line by first extracting words from the line and 1149 * finding nodes in the list of all targets with that name. This is done 1150 * until a character is encountered which is an operator character. Currently 1151 * these are only ! and :. At this point the operator is parsed and the 1152 * pointer into the line advanced until the first source is encountered. 1153 * The parsed operator is applied to each node in the 'targets' list, 1154 * which is where the nodes found for the targets are kept, by means of 1155 * the ParseDoOp function. 1156 * The sources are read in much the same way as the targets were except 1157 * that now they are expanded using the wildcarding scheme of the C-Shell 1158 * and all instances of the resulting words in the list of all targets 1159 * are found. Each of the resulting nodes is then linked to each of the 1160 * targets as one of its children. 1161 * Certain targets are handled specially. These are the ones detailed 1162 * by the specType variable. 1163 * The storing of transformation rules is also taken care of here. 1164 * A target is recognized as a transformation rule by calling 1165 * Suff_IsTransform. If it is a transformation rule, its node is gotten 1166 * from the suffix module via Suff_AddTransform rather than the standard 1167 * Targ_FindNode in the target module. 1168 *--------------------------------------------------------------------- 1169 */ 1170 static void 1171 ParseDoDependency(char *line) 1172 { 1173 char *cp; /* our current position */ 1174 GNode *gn = NULL; /* a general purpose temporary node */ 1175 int op; /* the operator on the line */ 1176 char savec; /* a place to save a character */ 1177 Lst paths; /* List of search paths to alter when parsing 1178 * a list of .PATH targets */ 1179 int tOp; /* operator from special target */ 1180 Lst sources; /* list of archive source names after 1181 * expansion */ 1182 Lst curTargs; /* list of target names to be found and added 1183 * to the targets list */ 1184 char *lstart = line; 1185 1186 if (DEBUG(PARSE)) 1187 fprintf(debug_file, "ParseDoDependency(%s)\n", line); 1188 tOp = 0; 1189 1190 specType = Not; 1191 paths = NULL; 1192 1193 curTargs = Lst_Init(FALSE); 1194 1195 /* 1196 * First, grind through the targets. 1197 */ 1198 1199 do { 1200 /* 1201 * Here LINE points to the beginning of the next word, and 1202 * LSTART points to the actual beginning of the line. 1203 */ 1204 1205 /* Find the end of the next word. */ 1206 for (cp = line; *cp && (ParseIsEscaped(lstart, cp) || 1207 !(isspace((unsigned char)*cp) || 1208 *cp == '!' || *cp == ':' || *cp == LPAREN)); 1209 cp++) { 1210 if (*cp == '$') { 1211 /* 1212 * Must be a dynamic source (would have been expanded 1213 * otherwise), so call the Var module to parse the puppy 1214 * so we can safely advance beyond it...There should be 1215 * no errors in this, as they would have been discovered 1216 * in the initial Var_Subst and we wouldn't be here. 1217 */ 1218 int length; 1219 void *freeIt; 1220 1221 (void)Var_Parse(cp, VAR_CMD, TRUE, TRUE, FALSE, &length, &freeIt); 1222 free(freeIt); 1223 cp += length-1; 1224 } 1225 } 1226 1227 /* 1228 * If the word is followed by a left parenthesis, it's the 1229 * name of an object file inside an archive (ar file). 1230 */ 1231 if (!ParseIsEscaped(lstart, cp) && *cp == LPAREN) { 1232 /* 1233 * Archives must be handled specially to make sure the OP_ARCHV 1234 * flag is set in their 'type' field, for one thing, and because 1235 * things like "archive(file1.o file2.o file3.o)" are permissible. 1236 * Arch_ParseArchive will set 'line' to be the first non-blank 1237 * after the archive-spec. It creates/finds nodes for the members 1238 * and places them on the given list, returning SUCCESS if all 1239 * went well and FAILURE if there was an error in the 1240 * specification. On error, line should remain untouched. 1241 */ 1242 if (Arch_ParseArchive(&line, targets, VAR_CMD) != SUCCESS) { 1243 Parse_Error(PARSE_FATAL, 1244 "Error in archive specification: \"%s\"", line); 1245 goto out; 1246 } else { 1247 /* Done with this word; on to the next. */ 1248 continue; 1249 } 1250 } 1251 1252 if (!*cp) { 1253 /* 1254 * We got to the end of the line while we were still 1255 * looking at targets. 1256 * 1257 * Ending a dependency line without an operator is a Bozo 1258 * no-no. As a heuristic, this is also often triggered by 1259 * undetected conflicts from cvs/rcs merges. 1260 */ 1261 if ((strncmp(line, "<<<<<<", 6) == 0) || 1262 (strncmp(line, "======", 6) == 0) || 1263 (strncmp(line, ">>>>>>", 6) == 0)) 1264 Parse_Error(PARSE_FATAL, 1265 "Makefile appears to contain unresolved cvs/rcs/??? merge conflicts"); 1266 else 1267 Parse_Error(PARSE_FATAL, lstart[0] == '.' ? "Unknown directive" 1268 : "Need an operator"); 1269 goto out; 1270 } 1271 1272 /* Insert a null terminator. */ 1273 savec = *cp; 1274 *cp = '\0'; 1275 1276 /* 1277 * Got the word. See if it's a special target and if so set 1278 * specType to match it. 1279 */ 1280 if (*line == '.' && isupper ((unsigned char)line[1])) { 1281 /* 1282 * See if the target is a special target that must have it 1283 * or its sources handled specially. 1284 */ 1285 int keywd = ParseFindKeyword(line); 1286 if (keywd != -1) { 1287 if (specType == ExPath && parseKeywords[keywd].spec != ExPath) { 1288 Parse_Error(PARSE_FATAL, "Mismatched special targets"); 1289 goto out; 1290 } 1291 1292 specType = parseKeywords[keywd].spec; 1293 tOp = parseKeywords[keywd].op; 1294 1295 /* 1296 * Certain special targets have special semantics: 1297 * .PATH Have to set the dirSearchPath 1298 * variable too 1299 * .MAIN Its sources are only used if 1300 * nothing has been specified to 1301 * create. 1302 * .DEFAULT Need to create a node to hang 1303 * commands on, but we don't want 1304 * it in the graph, nor do we want 1305 * it to be the Main Target, so we 1306 * create it, set OP_NOTMAIN and 1307 * add it to the list, setting 1308 * DEFAULT to the new node for 1309 * later use. We claim the node is 1310 * A transformation rule to make 1311 * life easier later, when we'll 1312 * use Make_HandleUse to actually 1313 * apply the .DEFAULT commands. 1314 * .PHONY The list of targets 1315 * .NOPATH Don't search for file in the path 1316 * .STALE 1317 * .BEGIN 1318 * .END 1319 * .ERROR 1320 * .INTERRUPT Are not to be considered the 1321 * main target. 1322 * .NOTPARALLEL Make only one target at a time. 1323 * .SINGLESHELL Create a shell for each command. 1324 * .ORDER Must set initial predecessor to NULL 1325 */ 1326 switch (specType) { 1327 case ExPath: 1328 if (paths == NULL) { 1329 paths = Lst_Init(FALSE); 1330 } 1331 (void)Lst_AtEnd(paths, dirSearchPath); 1332 break; 1333 case Main: 1334 if (!Lst_IsEmpty(create)) { 1335 specType = Not; 1336 } 1337 break; 1338 case Begin: 1339 case End: 1340 case Stale: 1341 case dotError: 1342 case Interrupt: 1343 gn = Targ_FindNode(line, TARG_CREATE); 1344 if (doing_depend) 1345 ParseMark(gn); 1346 gn->type |= OP_NOTMAIN|OP_SPECIAL; 1347 (void)Lst_AtEnd(targets, gn); 1348 break; 1349 case Default: 1350 gn = Targ_NewGN(".DEFAULT"); 1351 gn->type |= (OP_NOTMAIN|OP_TRANSFORM); 1352 (void)Lst_AtEnd(targets, gn); 1353 DEFAULT = gn; 1354 break; 1355 case NotParallel: 1356 maxJobs = 1; 1357 break; 1358 case SingleShell: 1359 compatMake = TRUE; 1360 break; 1361 case Order: 1362 predecessor = NULL; 1363 break; 1364 default: 1365 break; 1366 } 1367 } else if (strncmp(line, ".PATH", 5) == 0) { 1368 /* 1369 * .PATH<suffix> has to be handled specially. 1370 * Call on the suffix module to give us a path to 1371 * modify. 1372 */ 1373 Lst path; 1374 1375 specType = ExPath; 1376 path = Suff_GetPath(&line[5]); 1377 if (path == NULL) { 1378 Parse_Error(PARSE_FATAL, 1379 "Suffix '%s' not defined (yet)", 1380 &line[5]); 1381 goto out; 1382 } else { 1383 if (paths == NULL) { 1384 paths = Lst_Init(FALSE); 1385 } 1386 (void)Lst_AtEnd(paths, path); 1387 } 1388 } 1389 } 1390 1391 /* 1392 * Have word in line. Get or create its node and stick it at 1393 * the end of the targets list 1394 */ 1395 if ((specType == Not) && (*line != '\0')) { 1396 if (Dir_HasWildcards(line)) { 1397 /* 1398 * Targets are to be sought only in the current directory, 1399 * so create an empty path for the thing. Note we need to 1400 * use Dir_Destroy in the destruction of the path as the 1401 * Dir module could have added a directory to the path... 1402 */ 1403 Lst emptyPath = Lst_Init(FALSE); 1404 1405 Dir_Expand(line, emptyPath, curTargs); 1406 1407 Lst_Destroy(emptyPath, Dir_Destroy); 1408 } else { 1409 /* 1410 * No wildcards, but we want to avoid code duplication, 1411 * so create a list with the word on it. 1412 */ 1413 (void)Lst_AtEnd(curTargs, line); 1414 } 1415 1416 /* Apply the targets. */ 1417 1418 while(!Lst_IsEmpty(curTargs)) { 1419 char *targName = (char *)Lst_DeQueue(curTargs); 1420 1421 if (!Suff_IsTransform (targName)) { 1422 gn = Targ_FindNode(targName, TARG_CREATE); 1423 } else { 1424 gn = Suff_AddTransform(targName); 1425 } 1426 if (doing_depend) 1427 ParseMark(gn); 1428 1429 (void)Lst_AtEnd(targets, gn); 1430 } 1431 } else if (specType == ExPath && *line != '.' && *line != '\0') { 1432 Parse_Error(PARSE_WARNING, "Extra target (%s) ignored", line); 1433 } 1434 1435 /* Don't need the inserted null terminator any more. */ 1436 *cp = savec; 1437 1438 /* 1439 * If it is a special type and not .PATH, it's the only target we 1440 * allow on this line... 1441 */ 1442 if (specType != Not && specType != ExPath) { 1443 Boolean warning = FALSE; 1444 1445 while (*cp && (ParseIsEscaped(lstart, cp) || 1446 ((*cp != '!') && (*cp != ':')))) { 1447 if (ParseIsEscaped(lstart, cp) || 1448 (*cp != ' ' && *cp != '\t')) { 1449 warning = TRUE; 1450 } 1451 cp++; 1452 } 1453 if (warning) { 1454 Parse_Error(PARSE_WARNING, "Extra target ignored"); 1455 } 1456 } else { 1457 while (*cp && isspace ((unsigned char)*cp)) { 1458 cp++; 1459 } 1460 } 1461 line = cp; 1462 } while (*line && (ParseIsEscaped(lstart, line) || 1463 ((*line != '!') && (*line != ':')))); 1464 1465 /* 1466 * Don't need the list of target names anymore... 1467 */ 1468 Lst_Destroy(curTargs, NULL); 1469 curTargs = NULL; 1470 1471 if (!Lst_IsEmpty(targets)) { 1472 switch(specType) { 1473 default: 1474 Parse_Error(PARSE_WARNING, "Special and mundane targets don't mix. Mundane ones ignored"); 1475 break; 1476 case Default: 1477 case Stale: 1478 case Begin: 1479 case End: 1480 case dotError: 1481 case Interrupt: 1482 /* 1483 * These four create nodes on which to hang commands, so 1484 * targets shouldn't be empty... 1485 */ 1486 case Not: 1487 /* 1488 * Nothing special here -- targets can be empty if it wants. 1489 */ 1490 break; 1491 } 1492 } 1493 1494 /* 1495 * Have now parsed all the target names. Must parse the operator next. The 1496 * result is left in op . 1497 */ 1498 if (*cp == '!') { 1499 op = OP_FORCE; 1500 } else if (*cp == ':') { 1501 if (cp[1] == ':') { 1502 op = OP_DOUBLEDEP; 1503 cp++; 1504 } else { 1505 op = OP_DEPENDS; 1506 } 1507 } else { 1508 Parse_Error(PARSE_FATAL, lstart[0] == '.' ? "Unknown directive" 1509 : "Missing dependency operator"); 1510 goto out; 1511 } 1512 1513 /* Advance beyond the operator */ 1514 cp++; 1515 1516 /* 1517 * Apply the operator to the target. This is how we remember which 1518 * operator a target was defined with. It fails if the operator 1519 * used isn't consistent across all references. 1520 */ 1521 Lst_ForEach(targets, ParseDoOp, &op); 1522 1523 /* 1524 * Onward to the sources. 1525 * 1526 * LINE will now point to the first source word, if any, or the 1527 * end of the string if not. 1528 */ 1529 while (*cp && isspace ((unsigned char)*cp)) { 1530 cp++; 1531 } 1532 line = cp; 1533 1534 /* 1535 * Several special targets take different actions if present with no 1536 * sources: 1537 * a .SUFFIXES line with no sources clears out all old suffixes 1538 * a .PRECIOUS line makes all targets precious 1539 * a .IGNORE line ignores errors for all targets 1540 * a .SILENT line creates silence when making all targets 1541 * a .PATH removes all directories from the search path(s). 1542 */ 1543 if (!*line) { 1544 switch (specType) { 1545 case Suffixes: 1546 Suff_ClearSuffixes(); 1547 break; 1548 case Precious: 1549 allPrecious = TRUE; 1550 break; 1551 case Ignore: 1552 ignoreErrors = TRUE; 1553 break; 1554 case Silent: 1555 beSilent = TRUE; 1556 break; 1557 case ExPath: 1558 Lst_ForEach(paths, ParseClearPath, NULL); 1559 Dir_SetPATH(); 1560 break; 1561 #ifdef POSIX 1562 case Posix: 1563 Var_Set("%POSIX", "1003.2", VAR_GLOBAL, 0); 1564 break; 1565 #endif 1566 default: 1567 break; 1568 } 1569 } else if (specType == MFlags) { 1570 /* 1571 * Call on functions in main.c to deal with these arguments and 1572 * set the initial character to a null-character so the loop to 1573 * get sources won't get anything 1574 */ 1575 Main_ParseArgLine(line); 1576 *line = '\0'; 1577 } else if (specType == ExShell) { 1578 if (Job_ParseShell(line) != SUCCESS) { 1579 Parse_Error(PARSE_FATAL, "improper shell specification"); 1580 goto out; 1581 } 1582 *line = '\0'; 1583 } else if ((specType == NotParallel) || (specType == SingleShell)) { 1584 *line = '\0'; 1585 } 1586 1587 /* 1588 * NOW GO FOR THE SOURCES 1589 */ 1590 if ((specType == Suffixes) || (specType == ExPath) || 1591 (specType == Includes) || (specType == Libs) || 1592 (specType == Null) || (specType == ExObjdir)) 1593 { 1594 while (*line) { 1595 /* 1596 * If the target was one that doesn't take files as its sources 1597 * but takes something like suffixes, we take each 1598 * space-separated word on the line as a something and deal 1599 * with it accordingly. 1600 * 1601 * If the target was .SUFFIXES, we take each source as a 1602 * suffix and add it to the list of suffixes maintained by the 1603 * Suff module. 1604 * 1605 * If the target was a .PATH, we add the source as a directory 1606 * to search on the search path. 1607 * 1608 * If it was .INCLUDES, the source is taken to be the suffix of 1609 * files which will be #included and whose search path should 1610 * be present in the .INCLUDES variable. 1611 * 1612 * If it was .LIBS, the source is taken to be the suffix of 1613 * files which are considered libraries and whose search path 1614 * should be present in the .LIBS variable. 1615 * 1616 * If it was .NULL, the source is the suffix to use when a file 1617 * has no valid suffix. 1618 * 1619 * If it was .OBJDIR, the source is a new definition for .OBJDIR, 1620 * and will cause make to do a new chdir to that path. 1621 */ 1622 while (*cp && !isspace ((unsigned char)*cp)) { 1623 cp++; 1624 } 1625 savec = *cp; 1626 *cp = '\0'; 1627 switch (specType) { 1628 case Suffixes: 1629 Suff_AddSuffix(line, &mainNode); 1630 break; 1631 case ExPath: 1632 Lst_ForEach(paths, ParseAddDir, line); 1633 break; 1634 case Includes: 1635 Suff_AddInclude(line); 1636 break; 1637 case Libs: 1638 Suff_AddLib(line); 1639 break; 1640 case Null: 1641 Suff_SetNull(line); 1642 break; 1643 case ExObjdir: 1644 Main_SetObjdir(line); 1645 break; 1646 default: 1647 break; 1648 } 1649 *cp = savec; 1650 if (savec != '\0') { 1651 cp++; 1652 } 1653 while (*cp && isspace ((unsigned char)*cp)) { 1654 cp++; 1655 } 1656 line = cp; 1657 } 1658 if (paths) { 1659 Lst_Destroy(paths, NULL); 1660 } 1661 if (specType == ExPath) 1662 Dir_SetPATH(); 1663 } else { 1664 while (*line) { 1665 /* 1666 * The targets take real sources, so we must beware of archive 1667 * specifications (i.e. things with left parentheses in them) 1668 * and handle them accordingly. 1669 */ 1670 for (; *cp && !isspace ((unsigned char)*cp); cp++) { 1671 if ((*cp == LPAREN) && (cp > line) && (cp[-1] != '$')) { 1672 /* 1673 * Only stop for a left parenthesis if it isn't at the 1674 * start of a word (that'll be for variable changes 1675 * later) and isn't preceded by a dollar sign (a dynamic 1676 * source). 1677 */ 1678 break; 1679 } 1680 } 1681 1682 if (*cp == LPAREN) { 1683 sources = Lst_Init(FALSE); 1684 if (Arch_ParseArchive(&line, sources, VAR_CMD) != SUCCESS) { 1685 Parse_Error(PARSE_FATAL, 1686 "Error in source archive spec \"%s\"", line); 1687 goto out; 1688 } 1689 1690 while (!Lst_IsEmpty (sources)) { 1691 gn = (GNode *)Lst_DeQueue(sources); 1692 ParseDoSrc(tOp, gn->name); 1693 } 1694 Lst_Destroy(sources, NULL); 1695 cp = line; 1696 } else { 1697 if (*cp) { 1698 *cp = '\0'; 1699 cp += 1; 1700 } 1701 1702 ParseDoSrc(tOp, line); 1703 } 1704 while (*cp && isspace ((unsigned char)*cp)) { 1705 cp++; 1706 } 1707 line = cp; 1708 } 1709 } 1710 1711 if (mainNode == NULL) { 1712 /* 1713 * If we have yet to decide on a main target to make, in the 1714 * absence of any user input, we want the first target on 1715 * the first dependency line that is actually a real target 1716 * (i.e. isn't a .USE or .EXEC rule) to be made. 1717 */ 1718 Lst_ForEach(targets, ParseFindMain, NULL); 1719 } 1720 1721 out: 1722 if (curTargs) 1723 Lst_Destroy(curTargs, NULL); 1724 } 1725 1726 /*- 1727 *--------------------------------------------------------------------- 1728 * Parse_IsVar -- 1729 * Return TRUE if the passed line is a variable assignment. A variable 1730 * assignment consists of a single word followed by optional whitespace 1731 * followed by either a += or an = operator. 1732 * This function is used both by the Parse_File function and main when 1733 * parsing the command-line arguments. 1734 * 1735 * Input: 1736 * line the line to check 1737 * 1738 * Results: 1739 * TRUE if it is. FALSE if it ain't 1740 * 1741 * Side Effects: 1742 * none 1743 *--------------------------------------------------------------------- 1744 */ 1745 Boolean 1746 Parse_IsVar(char *line) 1747 { 1748 Boolean wasSpace = FALSE; /* set TRUE if found a space */ 1749 char ch; 1750 int level = 0; 1751 #define ISEQOPERATOR(c) \ 1752 (((c) == '+') || ((c) == ':') || ((c) == '?') || ((c) == '!')) 1753 1754 /* 1755 * Skip to variable name 1756 */ 1757 for (;(*line == ' ') || (*line == '\t'); line++) 1758 continue; 1759 1760 /* Scan for one of the assignment operators outside a variable expansion */ 1761 while ((ch = *line++) != 0) { 1762 if (ch == '(' || ch == '{') { 1763 level++; 1764 continue; 1765 } 1766 if (ch == ')' || ch == '}') { 1767 level--; 1768 continue; 1769 } 1770 if (level != 0) 1771 continue; 1772 while (ch == ' ' || ch == '\t') { 1773 ch = *line++; 1774 wasSpace = TRUE; 1775 } 1776 #ifdef SUNSHCMD 1777 if (ch == ':' && strncmp(line, "sh", 2) == 0) { 1778 line += 2; 1779 continue; 1780 } 1781 #endif 1782 if (ch == '=') 1783 return TRUE; 1784 if (*line == '=' && ISEQOPERATOR(ch)) 1785 return TRUE; 1786 if (wasSpace) 1787 return FALSE; 1788 } 1789 1790 return FALSE; 1791 } 1792 1793 /*- 1794 *--------------------------------------------------------------------- 1795 * Parse_DoVar -- 1796 * Take the variable assignment in the passed line and do it in the 1797 * global context. 1798 * 1799 * Note: There is a lexical ambiguity with assignment modifier characters 1800 * in variable names. This routine interprets the character before the = 1801 * as a modifier. Therefore, an assignment like 1802 * C++=/usr/bin/CC 1803 * is interpreted as "C+ +=" instead of "C++ =". 1804 * 1805 * Input: 1806 * line a line guaranteed to be a variable assignment. 1807 * This reduces error checks 1808 * ctxt Context in which to do the assignment 1809 * 1810 * Results: 1811 * none 1812 * 1813 * Side Effects: 1814 * the variable structure of the given variable name is altered in the 1815 * global context. 1816 *--------------------------------------------------------------------- 1817 */ 1818 void 1819 Parse_DoVar(char *line, GNode *ctxt) 1820 { 1821 char *cp; /* pointer into line */ 1822 enum { 1823 VAR_SUBST, VAR_APPEND, VAR_SHELL, VAR_NORMAL 1824 } type; /* Type of assignment */ 1825 char *opc; /* ptr to operator character to 1826 * null-terminate the variable name */ 1827 Boolean freeCp = FALSE; /* TRUE if cp needs to be freed, 1828 * i.e. if any variable expansion was 1829 * performed */ 1830 int depth; 1831 1832 /* 1833 * Skip to variable name 1834 */ 1835 while ((*line == ' ') || (*line == '\t')) { 1836 line++; 1837 } 1838 1839 /* 1840 * Skip to operator character, nulling out whitespace as we go 1841 * XXX Rather than counting () and {} we should look for $ and 1842 * then expand the variable. 1843 */ 1844 for (depth = 0, cp = line + 1; depth != 0 || *cp != '='; cp++) { 1845 if (*cp == '(' || *cp == '{') { 1846 depth++; 1847 continue; 1848 } 1849 if (*cp == ')' || *cp == '}') { 1850 depth--; 1851 continue; 1852 } 1853 if (depth == 0 && isspace ((unsigned char)*cp)) { 1854 *cp = '\0'; 1855 } 1856 } 1857 opc = cp-1; /* operator is the previous character */ 1858 *cp++ = '\0'; /* nuke the = */ 1859 1860 /* 1861 * Check operator type 1862 */ 1863 switch (*opc) { 1864 case '+': 1865 type = VAR_APPEND; 1866 *opc = '\0'; 1867 break; 1868 1869 case '?': 1870 /* 1871 * If the variable already has a value, we don't do anything. 1872 */ 1873 *opc = '\0'; 1874 if (Var_Exists(line, ctxt)) { 1875 return; 1876 } else { 1877 type = VAR_NORMAL; 1878 } 1879 break; 1880 1881 case ':': 1882 type = VAR_SUBST; 1883 *opc = '\0'; 1884 break; 1885 1886 case '!': 1887 type = VAR_SHELL; 1888 *opc = '\0'; 1889 break; 1890 1891 default: 1892 #ifdef SUNSHCMD 1893 while (opc > line && *opc != ':') 1894 opc--; 1895 1896 if (strncmp(opc, ":sh", 3) == 0) { 1897 type = VAR_SHELL; 1898 *opc = '\0'; 1899 break; 1900 } 1901 #endif 1902 type = VAR_NORMAL; 1903 break; 1904 } 1905 1906 while (isspace ((unsigned char)*cp)) { 1907 cp++; 1908 } 1909 1910 if (type == VAR_APPEND) { 1911 Var_Append(line, cp, ctxt); 1912 } else if (type == VAR_SUBST) { 1913 /* 1914 * Allow variables in the old value to be undefined, but leave their 1915 * invocation alone -- this is done by forcing oldVars to be false. 1916 * XXX: This can cause recursive variables, but that's not hard to do, 1917 * and this allows someone to do something like 1918 * 1919 * CFLAGS = $(.INCLUDES) 1920 * CFLAGS := -I.. $(CFLAGS) 1921 * 1922 * And not get an error. 1923 */ 1924 Boolean oldOldVars = oldVars; 1925 1926 oldVars = FALSE; 1927 1928 /* 1929 * make sure that we set the variable the first time to nothing 1930 * so that it gets substituted! 1931 */ 1932 if (!Var_Exists(line, ctxt)) 1933 Var_Set(line, "", ctxt, 0); 1934 1935 cp = Var_Subst(NULL, cp, ctxt, FALSE, TRUE, TRUE); 1936 oldVars = oldOldVars; 1937 freeCp = TRUE; 1938 1939 Var_Set(line, cp, ctxt, 0); 1940 } else if (type == VAR_SHELL) { 1941 char *res; 1942 const char *error; 1943 1944 if (strchr(cp, '$') != NULL) { 1945 /* 1946 * There's a dollar sign in the command, so perform variable 1947 * expansion on the whole thing. The resulting string will need 1948 * freeing when we're done, so set freeCmd to TRUE. 1949 */ 1950 cp = Var_Subst(NULL, cp, VAR_CMD, TRUE, TRUE, FALSE); 1951 freeCp = TRUE; 1952 } 1953 1954 res = Cmd_Exec(cp, &error); 1955 Var_Set(line, res, ctxt, 0); 1956 free(res); 1957 1958 if (error) 1959 Parse_Error(PARSE_WARNING, error, cp); 1960 } else { 1961 /* 1962 * Normal assignment -- just do it. 1963 */ 1964 Var_Set(line, cp, ctxt, 0); 1965 } 1966 if (strcmp(line, MAKEOVERRIDES) == 0) 1967 Main_ExportMAKEFLAGS(FALSE); /* re-export MAKEFLAGS */ 1968 else if (strcmp(line, ".CURDIR") == 0) { 1969 /* 1970 * Somone is being (too?) clever... 1971 * Let's pretend they know what they are doing and 1972 * re-initialize the 'cur' Path. 1973 */ 1974 Dir_InitCur(cp); 1975 Dir_SetPATH(); 1976 } else if (strcmp(line, MAKE_JOB_PREFIX) == 0) { 1977 Job_SetPrefix(); 1978 } else if (strcmp(line, MAKE_EXPORTED) == 0) { 1979 Var_Export(cp, 0); 1980 } 1981 if (freeCp) 1982 free(cp); 1983 } 1984 1985 1986 /* 1987 * ParseMaybeSubMake -- 1988 * Scan the command string to see if it a possible submake node 1989 * Input: 1990 * cmd the command to scan 1991 * Results: 1992 * TRUE if the command is possibly a submake, FALSE if not. 1993 */ 1994 static Boolean 1995 ParseMaybeSubMake(const char *cmd) 1996 { 1997 size_t i; 1998 static struct { 1999 const char *name; 2000 size_t len; 2001 } vals[] = { 2002 #define MKV(A) { A, sizeof(A) - 1 } 2003 MKV("${MAKE}"), 2004 MKV("${.MAKE}"), 2005 MKV("$(MAKE)"), 2006 MKV("$(.MAKE)"), 2007 MKV("make"), 2008 }; 2009 for (i = 0; i < sizeof(vals)/sizeof(vals[0]); i++) { 2010 char *ptr; 2011 if ((ptr = strstr(cmd, vals[i].name)) == NULL) 2012 continue; 2013 if ((ptr == cmd || !isalnum((unsigned char)ptr[-1])) 2014 && !isalnum((unsigned char)ptr[vals[i].len])) 2015 return TRUE; 2016 } 2017 return FALSE; 2018 } 2019 2020 /*- 2021 * ParseAddCmd -- 2022 * Lst_ForEach function to add a command line to all targets 2023 * 2024 * Input: 2025 * gnp the node to which the command is to be added 2026 * cmd the command to add 2027 * 2028 * Results: 2029 * Always 0 2030 * 2031 * Side Effects: 2032 * A new element is added to the commands list of the node, 2033 * and the node can be marked as a submake node if the command is 2034 * determined to be that. 2035 */ 2036 static int 2037 ParseAddCmd(void *gnp, void *cmd) 2038 { 2039 GNode *gn = (GNode *)gnp; 2040 2041 /* Add to last (ie current) cohort for :: targets */ 2042 if ((gn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (gn->cohorts)) 2043 gn = (GNode *)Lst_Datum(Lst_Last(gn->cohorts)); 2044 2045 /* if target already supplied, ignore commands */ 2046 if (!(gn->type & OP_HAS_COMMANDS)) { 2047 (void)Lst_AtEnd(gn->commands, cmd); 2048 if (ParseMaybeSubMake(cmd)) 2049 gn->type |= OP_SUBMAKE; 2050 ParseMark(gn); 2051 } else { 2052 #ifdef notyet 2053 /* XXX: We cannot do this until we fix the tree */ 2054 (void)Lst_AtEnd(gn->commands, cmd); 2055 Parse_Error(PARSE_WARNING, 2056 "overriding commands for target \"%s\"; " 2057 "previous commands defined at %s: %d ignored", 2058 gn->name, gn->fname, gn->lineno); 2059 #else 2060 Parse_Error(PARSE_WARNING, 2061 "duplicate script for target \"%s\" ignored", 2062 gn->name); 2063 ParseErrorInternal(gn->fname, gn->lineno, PARSE_WARNING, 2064 "using previous script for \"%s\" defined here", 2065 gn->name); 2066 #endif 2067 } 2068 return(0); 2069 } 2070 2071 /*- 2072 *----------------------------------------------------------------------- 2073 * ParseHasCommands -- 2074 * Callback procedure for Parse_File when destroying the list of 2075 * targets on the last dependency line. Marks a target as already 2076 * having commands if it does, to keep from having shell commands 2077 * on multiple dependency lines. 2078 * 2079 * Input: 2080 * gnp Node to examine 2081 * 2082 * Results: 2083 * None 2084 * 2085 * Side Effects: 2086 * OP_HAS_COMMANDS may be set for the target. 2087 * 2088 *----------------------------------------------------------------------- 2089 */ 2090 static void 2091 ParseHasCommands(void *gnp) 2092 { 2093 GNode *gn = (GNode *)gnp; 2094 if (!Lst_IsEmpty(gn->commands)) { 2095 gn->type |= OP_HAS_COMMANDS; 2096 } 2097 } 2098 2099 /*- 2100 *----------------------------------------------------------------------- 2101 * Parse_AddIncludeDir -- 2102 * Add a directory to the path searched for included makefiles 2103 * bracketed by double-quotes. Used by functions in main.c 2104 * 2105 * Input: 2106 * dir The name of the directory to add 2107 * 2108 * Results: 2109 * None. 2110 * 2111 * Side Effects: 2112 * The directory is appended to the list. 2113 * 2114 *----------------------------------------------------------------------- 2115 */ 2116 void 2117 Parse_AddIncludeDir(char *dir) 2118 { 2119 (void)Dir_AddDir(parseIncPath, dir); 2120 } 2121 2122 /*- 2123 *--------------------------------------------------------------------- 2124 * ParseDoInclude -- 2125 * Push to another file. 2126 * 2127 * The input is the line minus the `.'. A file spec is a string 2128 * enclosed in <> or "". The former is looked for only in sysIncPath. 2129 * The latter in . and the directories specified by -I command line 2130 * options 2131 * 2132 * Results: 2133 * None 2134 * 2135 * Side Effects: 2136 * A structure is added to the includes Lst and readProc, lineno, 2137 * fname and curFILE are altered for the new file 2138 *--------------------------------------------------------------------- 2139 */ 2140 2141 static void 2142 Parse_include_file(char *file, Boolean isSystem, int silent) 2143 { 2144 struct loadedfile *lf; 2145 char *fullname; /* full pathname of file */ 2146 char *newName; 2147 char *prefEnd, *incdir; 2148 int fd; 2149 int i; 2150 2151 /* 2152 * Now we know the file's name and its search path, we attempt to 2153 * find the durn thing. A return of NULL indicates the file don't 2154 * exist. 2155 */ 2156 fullname = file[0] == '/' ? bmake_strdup(file) : NULL; 2157 2158 if (fullname == NULL && !isSystem) { 2159 /* 2160 * Include files contained in double-quotes are first searched for 2161 * relative to the including file's location. We don't want to 2162 * cd there, of course, so we just tack on the old file's 2163 * leading path components and call Dir_FindFile to see if 2164 * we can locate the beast. 2165 */ 2166 2167 incdir = bmake_strdup(curFile->fname); 2168 prefEnd = strrchr(incdir, '/'); 2169 if (prefEnd != NULL) { 2170 *prefEnd = '\0'; 2171 /* Now do lexical processing of leading "../" on the filename */ 2172 for (i = 0; strncmp(file + i, "../", 3) == 0; i += 3) { 2173 prefEnd = strrchr(incdir + 1, '/'); 2174 if (prefEnd == NULL || strcmp(prefEnd, "/..") == 0) 2175 break; 2176 *prefEnd = '\0'; 2177 } 2178 newName = str_concat(incdir, file + i, STR_ADDSLASH); 2179 fullname = Dir_FindFile(newName, parseIncPath); 2180 if (fullname == NULL) 2181 fullname = Dir_FindFile(newName, dirSearchPath); 2182 free(newName); 2183 } 2184 free(incdir); 2185 2186 if (fullname == NULL) { 2187 /* 2188 * Makefile wasn't found in same directory as included makefile. 2189 * Search for it first on the -I search path, 2190 * then on the .PATH search path, if not found in a -I directory. 2191 * If we have a suffix specific path we should use that. 2192 */ 2193 char *suff; 2194 Lst suffPath = NULL; 2195 2196 if ((suff = strrchr(file, '.'))) { 2197 suffPath = Suff_GetPath(suff); 2198 if (suffPath != NULL) { 2199 fullname = Dir_FindFile(file, suffPath); 2200 } 2201 } 2202 if (fullname == NULL) { 2203 fullname = Dir_FindFile(file, parseIncPath); 2204 if (fullname == NULL) { 2205 fullname = Dir_FindFile(file, dirSearchPath); 2206 } 2207 } 2208 } 2209 } 2210 2211 /* Looking for a system file or file still not found */ 2212 if (fullname == NULL) { 2213 /* 2214 * Look for it on the system path 2215 */ 2216 fullname = Dir_FindFile(file, 2217 Lst_IsEmpty(sysIncPath) ? defIncPath : sysIncPath); 2218 } 2219 2220 if (fullname == NULL) { 2221 if (!silent) 2222 Parse_Error(PARSE_FATAL, "Could not find %s", file); 2223 return; 2224 } 2225 2226 /* Actually open the file... */ 2227 fd = open(fullname, O_RDONLY); 2228 if (fd == -1) { 2229 if (!silent) 2230 Parse_Error(PARSE_FATAL, "Cannot open %s", fullname); 2231 free(fullname); 2232 return; 2233 } 2234 2235 /* load it */ 2236 lf = loadfile(fullname, fd); 2237 2238 ParseSetIncludedFile(); 2239 /* Start reading from this file next */ 2240 Parse_SetInput(fullname, 0, -1, loadedfile_nextbuf, lf); 2241 curFile->lf = lf; 2242 } 2243 2244 static void 2245 ParseDoInclude(char *line) 2246 { 2247 char endc; /* the character which ends the file spec */ 2248 char *cp; /* current position in file spec */ 2249 int silent = (*line != 'i') ? 1 : 0; 2250 char *file = &line[7 + silent]; 2251 2252 /* Skip to delimiter character so we know where to look */ 2253 while (*file == ' ' || *file == '\t') 2254 file++; 2255 2256 if (*file != '"' && *file != '<') { 2257 Parse_Error(PARSE_FATAL, 2258 ".include filename must be delimited by '\"' or '<'"); 2259 return; 2260 } 2261 2262 /* 2263 * Set the search path on which to find the include file based on the 2264 * characters which bracket its name. Angle-brackets imply it's 2265 * a system Makefile while double-quotes imply it's a user makefile 2266 */ 2267 if (*file == '<') { 2268 endc = '>'; 2269 } else { 2270 endc = '"'; 2271 } 2272 2273 /* Skip to matching delimiter */ 2274 for (cp = ++file; *cp && *cp != endc; cp++) 2275 continue; 2276 2277 if (*cp != endc) { 2278 Parse_Error(PARSE_FATAL, 2279 "Unclosed %cinclude filename. '%c' expected", 2280 '.', endc); 2281 return; 2282 } 2283 *cp = '\0'; 2284 2285 /* 2286 * Substitute for any variables in the file name before trying to 2287 * find the thing. 2288 */ 2289 file = Var_Subst(NULL, file, VAR_CMD, FALSE, TRUE, FALSE); 2290 2291 Parse_include_file(file, endc == '>', silent); 2292 free(file); 2293 } 2294 2295 2296 /*- 2297 *--------------------------------------------------------------------- 2298 * ParseSetIncludedFile -- 2299 * Set the .INCLUDEDFROMFILE variable to the contents of .PARSEFILE 2300 * and the .INCLUDEDFROMDIR variable to the contents of .PARSEDIR 2301 * 2302 * Results: 2303 * None 2304 * 2305 * Side Effects: 2306 * The .INCLUDEDFROMFILE variable is overwritten by the contents 2307 * of .PARSEFILE and the .INCLUDEDFROMDIR variable is overwriten 2308 * by the contents of .PARSEDIR 2309 *--------------------------------------------------------------------- 2310 */ 2311 static void 2312 ParseSetIncludedFile(void) 2313 { 2314 char *pf, *fp = NULL; 2315 char *pd, *dp = NULL; 2316 2317 pf = Var_Value(".PARSEFILE", VAR_GLOBAL, &fp); 2318 Var_Set(".INCLUDEDFROMFILE", pf, VAR_GLOBAL, 0); 2319 pd = Var_Value(".PARSEDIR", VAR_GLOBAL, &dp); 2320 Var_Set(".INCLUDEDFROMDIR", pd, VAR_GLOBAL, 0); 2321 2322 if (DEBUG(PARSE)) 2323 fprintf(debug_file, "%s: ${.INCLUDEDFROMDIR} = `%s' " 2324 "${.INCLUDEDFROMFILE} = `%s'\n", __func__, pd, pf); 2325 2326 free(fp); 2327 free(dp); 2328 } 2329 /*- 2330 *--------------------------------------------------------------------- 2331 * ParseSetParseFile -- 2332 * Set the .PARSEDIR and .PARSEFILE variables to the dirname and 2333 * basename of the given filename 2334 * 2335 * Results: 2336 * None 2337 * 2338 * Side Effects: 2339 * The .PARSEDIR and .PARSEFILE variables are overwritten by the 2340 * dirname and basename of the given filename. 2341 *--------------------------------------------------------------------- 2342 */ 2343 static void 2344 ParseSetParseFile(const char *filename) 2345 { 2346 char *slash, *dirname; 2347 const char *pd, *pf; 2348 int len; 2349 2350 slash = strrchr(filename, '/'); 2351 if (slash == NULL) { 2352 Var_Set(".PARSEDIR", pd = curdir, VAR_GLOBAL, 0); 2353 Var_Set(".PARSEFILE", pf = filename, VAR_GLOBAL, 0); 2354 dirname= NULL; 2355 } else { 2356 len = slash - filename; 2357 dirname = bmake_malloc(len + 1); 2358 memcpy(dirname, filename, len); 2359 dirname[len] = '\0'; 2360 Var_Set(".PARSEDIR", pd = dirname, VAR_GLOBAL, 0); 2361 Var_Set(".PARSEFILE", pf = slash + 1, VAR_GLOBAL, 0); 2362 } 2363 if (DEBUG(PARSE)) 2364 fprintf(debug_file, "%s: ${.PARSEDIR} = `%s' ${.PARSEFILE} = `%s'\n", 2365 __func__, pd, pf); 2366 free(dirname); 2367 } 2368 2369 /* 2370 * Track the makefiles we read - so makefiles can 2371 * set dependencies on them. 2372 * Avoid adding anything more than once. 2373 */ 2374 2375 static void 2376 ParseTrackInput(const char *name) 2377 { 2378 char *old; 2379 char *ep; 2380 char *fp = NULL; 2381 size_t name_len = strlen(name); 2382 2383 old = Var_Value(MAKE_MAKEFILES, VAR_GLOBAL, &fp); 2384 if (old) { 2385 ep = old + strlen(old) - name_len; 2386 /* does it contain name? */ 2387 for (; old != NULL; old = strchr(old, ' ')) { 2388 if (*old == ' ') 2389 old++; 2390 if (old >= ep) 2391 break; /* cannot contain name */ 2392 if (memcmp(old, name, name_len) == 0 2393 && (old[name_len] == 0 || old[name_len] == ' ')) 2394 goto cleanup; 2395 } 2396 } 2397 Var_Append (MAKE_MAKEFILES, name, VAR_GLOBAL); 2398 cleanup: 2399 if (fp) { 2400 free(fp); 2401 } 2402 } 2403 2404 2405 /*- 2406 *--------------------------------------------------------------------- 2407 * Parse_setInput -- 2408 * Start Parsing from the given source 2409 * 2410 * Results: 2411 * None 2412 * 2413 * Side Effects: 2414 * A structure is added to the includes Lst and readProc, lineno, 2415 * fname and curFile are altered for the new file 2416 *--------------------------------------------------------------------- 2417 */ 2418 void 2419 Parse_SetInput(const char *name, int line, int fd, 2420 char *(*nextbuf)(void *, size_t *), void *arg) 2421 { 2422 char *buf; 2423 size_t len; 2424 2425 if (name == NULL) 2426 name = curFile->fname; 2427 else 2428 ParseTrackInput(name); 2429 2430 if (DEBUG(PARSE)) 2431 fprintf(debug_file, "%s: file %s, line %d, fd %d, nextbuf %p, arg %p\n", 2432 __func__, name, line, fd, nextbuf, arg); 2433 2434 if (fd == -1 && nextbuf == NULL) 2435 /* sanity */ 2436 return; 2437 2438 if (curFile != NULL) 2439 /* Save exiting file info */ 2440 Lst_AtFront(includes, curFile); 2441 2442 /* Allocate and fill in new structure */ 2443 curFile = bmake_malloc(sizeof *curFile); 2444 2445 /* 2446 * Once the previous state has been saved, we can get down to reading 2447 * the new file. We set up the name of the file to be the absolute 2448 * name of the include file so error messages refer to the right 2449 * place. 2450 */ 2451 curFile->fname = bmake_strdup(name); 2452 curFile->lineno = line; 2453 curFile->first_lineno = line; 2454 curFile->nextbuf = nextbuf; 2455 curFile->nextbuf_arg = arg; 2456 curFile->lf = NULL; 2457 2458 assert(nextbuf != NULL); 2459 2460 /* Get first block of input data */ 2461 buf = curFile->nextbuf(curFile->nextbuf_arg, &len); 2462 if (buf == NULL) { 2463 /* Was all a waste of time ... */ 2464 if (curFile->fname) 2465 free(curFile->fname); 2466 free(curFile); 2467 return; 2468 } 2469 curFile->P_str = buf; 2470 curFile->P_ptr = buf; 2471 curFile->P_end = buf+len; 2472 2473 curFile->cond_depth = Cond_save_depth(); 2474 ParseSetParseFile(name); 2475 } 2476 2477 #ifdef SYSVINCLUDE 2478 /*- 2479 *--------------------------------------------------------------------- 2480 * ParseTraditionalInclude -- 2481 * Push to another file. 2482 * 2483 * The input is the current line. The file name(s) are 2484 * following the "include". 2485 * 2486 * Results: 2487 * None 2488 * 2489 * Side Effects: 2490 * A structure is added to the includes Lst and readProc, lineno, 2491 * fname and curFILE are altered for the new file 2492 *--------------------------------------------------------------------- 2493 */ 2494 static void 2495 ParseTraditionalInclude(char *line) 2496 { 2497 char *cp; /* current position in file spec */ 2498 int done = 0; 2499 int silent = (line[0] != 'i') ? 1 : 0; 2500 char *file = &line[silent + 7]; 2501 char *all_files; 2502 2503 if (DEBUG(PARSE)) { 2504 fprintf(debug_file, "%s: %s\n", __func__, file); 2505 } 2506 2507 /* 2508 * Skip over whitespace 2509 */ 2510 while (isspace((unsigned char)*file)) 2511 file++; 2512 2513 /* 2514 * Substitute for any variables in the file name before trying to 2515 * find the thing. 2516 */ 2517 all_files = Var_Subst(NULL, file, VAR_CMD, FALSE, TRUE, FALSE); 2518 2519 if (*file == '\0') { 2520 Parse_Error(PARSE_FATAL, 2521 "Filename missing from \"include\""); 2522 return; 2523 } 2524 2525 for (file = all_files; !done; file = cp + 1) { 2526 /* Skip to end of line or next whitespace */ 2527 for (cp = file; *cp && !isspace((unsigned char) *cp); cp++) 2528 continue; 2529 2530 if (*cp) 2531 *cp = '\0'; 2532 else 2533 done = 1; 2534 2535 Parse_include_file(file, FALSE, silent); 2536 } 2537 free(all_files); 2538 } 2539 #endif 2540 2541 #ifdef GMAKEEXPORT 2542 /*- 2543 *--------------------------------------------------------------------- 2544 * ParseGmakeExport -- 2545 * Parse export <variable>=<value> 2546 * 2547 * And set the environment with it. 2548 * 2549 * Results: 2550 * None 2551 * 2552 * Side Effects: 2553 * None 2554 *--------------------------------------------------------------------- 2555 */ 2556 static void 2557 ParseGmakeExport(char *line) 2558 { 2559 char *variable = &line[6]; 2560 char *value; 2561 2562 if (DEBUG(PARSE)) { 2563 fprintf(debug_file, "%s: %s\n", __func__, variable); 2564 } 2565 2566 /* 2567 * Skip over whitespace 2568 */ 2569 while (isspace((unsigned char)*variable)) 2570 variable++; 2571 2572 for (value = variable; *value && *value != '='; value++) 2573 continue; 2574 2575 if (*value != '=') { 2576 Parse_Error(PARSE_FATAL, 2577 "Variable/Value missing from \"export\""); 2578 return; 2579 } 2580 *value++ = '\0'; /* terminate variable */ 2581 2582 /* 2583 * Expand the value before putting it in the environment. 2584 */ 2585 value = Var_Subst(NULL, value, VAR_CMD, FALSE, TRUE, FALSE); 2586 setenv(variable, value, 1); 2587 } 2588 #endif 2589 2590 /*- 2591 *--------------------------------------------------------------------- 2592 * ParseEOF -- 2593 * Called when EOF is reached in the current file. If we were reading 2594 * an include file, the includes stack is popped and things set up 2595 * to go back to reading the previous file at the previous location. 2596 * 2597 * Results: 2598 * CONTINUE if there's more to do. DONE if not. 2599 * 2600 * Side Effects: 2601 * The old curFILE, is closed. The includes list is shortened. 2602 * lineno, curFILE, and fname are changed if CONTINUE is returned. 2603 *--------------------------------------------------------------------- 2604 */ 2605 static int 2606 ParseEOF(void) 2607 { 2608 char *ptr; 2609 size_t len; 2610 2611 assert(curFile->nextbuf != NULL); 2612 2613 /* get next input buffer, if any */ 2614 ptr = curFile->nextbuf(curFile->nextbuf_arg, &len); 2615 curFile->P_ptr = ptr; 2616 curFile->P_str = ptr; 2617 curFile->P_end = ptr + len; 2618 curFile->lineno = curFile->first_lineno; 2619 if (ptr != NULL) { 2620 /* Iterate again */ 2621 return CONTINUE; 2622 } 2623 2624 /* Ensure the makefile (or loop) didn't have mismatched conditionals */ 2625 Cond_restore_depth(curFile->cond_depth); 2626 2627 if (curFile->lf != NULL) { 2628 loadedfile_destroy(curFile->lf); 2629 curFile->lf = NULL; 2630 } 2631 2632 /* Dispose of curFile info */ 2633 /* Leak curFile->fname because all the gnodes have pointers to it */ 2634 free(curFile->P_str); 2635 free(curFile); 2636 2637 curFile = Lst_DeQueue(includes); 2638 2639 if (curFile == NULL) { 2640 /* We've run out of input */ 2641 Var_Delete(".PARSEDIR", VAR_GLOBAL); 2642 Var_Delete(".PARSEFILE", VAR_GLOBAL); 2643 Var_Delete(".INCLUDEDFROMDIR", VAR_GLOBAL); 2644 Var_Delete(".INCLUDEDFROMFILE", VAR_GLOBAL); 2645 return DONE; 2646 } 2647 2648 if (DEBUG(PARSE)) 2649 fprintf(debug_file, "ParseEOF: returning to file %s, line %d\n", 2650 curFile->fname, curFile->lineno); 2651 2652 /* Restore the PARSEDIR/PARSEFILE variables */ 2653 ParseSetParseFile(curFile->fname); 2654 return (CONTINUE); 2655 } 2656 2657 #define PARSE_RAW 1 2658 #define PARSE_SKIP 2 2659 2660 static char * 2661 ParseGetLine(int flags, int *length) 2662 { 2663 IFile *cf = curFile; 2664 char *ptr; 2665 char ch; 2666 char *line; 2667 char *line_end; 2668 char *escaped; 2669 char *comment; 2670 char *tp; 2671 2672 /* Loop through blank lines and comment lines */ 2673 for (;;) { 2674 cf->lineno++; 2675 line = cf->P_ptr; 2676 ptr = line; 2677 line_end = line; 2678 escaped = NULL; 2679 comment = NULL; 2680 for (;;) { 2681 if (cf->P_end != NULL && ptr == cf->P_end) { 2682 /* end of buffer */ 2683 ch = 0; 2684 break; 2685 } 2686 ch = *ptr; 2687 if (ch == 0 || (ch == '\\' && ptr[1] == 0)) { 2688 if (cf->P_end == NULL) 2689 /* End of string (aka for loop) data */ 2690 break; 2691 /* see if there is more we can parse */ 2692 while (ptr++ < cf->P_end) { 2693 if ((ch = *ptr) == '\n') { 2694 if (ptr > line && ptr[-1] == '\\') 2695 continue; 2696 Parse_Error(PARSE_WARNING, 2697 "Zero byte read from file, skipping rest of line."); 2698 break; 2699 } 2700 } 2701 if (cf->nextbuf != NULL) { 2702 /* 2703 * End of this buffer; return EOF and outer logic 2704 * will get the next one. (eww) 2705 */ 2706 break; 2707 } 2708 Parse_Error(PARSE_FATAL, "Zero byte read from file"); 2709 return NULL; 2710 } 2711 2712 if (ch == '\\') { 2713 /* Don't treat next character as special, remember first one */ 2714 if (escaped == NULL) 2715 escaped = ptr; 2716 if (ptr[1] == '\n') 2717 cf->lineno++; 2718 ptr += 2; 2719 line_end = ptr; 2720 continue; 2721 } 2722 if (ch == '#' && comment == NULL) { 2723 /* Remember first '#' for comment stripping */ 2724 /* Unless previous char was '[', as in modifier :[#] */ 2725 if (!(ptr > line && ptr[-1] == '[')) 2726 comment = line_end; 2727 } 2728 ptr++; 2729 if (ch == '\n') 2730 break; 2731 if (!isspace((unsigned char)ch)) 2732 /* We are not interested in trailing whitespace */ 2733 line_end = ptr; 2734 } 2735 2736 /* Save next 'to be processed' location */ 2737 cf->P_ptr = ptr; 2738 2739 /* Check we have a non-comment, non-blank line */ 2740 if (line_end == line || comment == line) { 2741 if (ch == 0) 2742 /* At end of file */ 2743 return NULL; 2744 /* Parse another line */ 2745 continue; 2746 } 2747 2748 /* We now have a line of data */ 2749 *line_end = 0; 2750 2751 if (flags & PARSE_RAW) { 2752 /* Leave '\' (etc) in line buffer (eg 'for' lines) */ 2753 *length = line_end - line; 2754 return line; 2755 } 2756 2757 if (flags & PARSE_SKIP) { 2758 /* Completely ignore non-directives */ 2759 if (line[0] != '.') 2760 continue; 2761 /* We could do more of the .else/.elif/.endif checks here */ 2762 } 2763 break; 2764 } 2765 2766 /* Brutally ignore anything after a non-escaped '#' in non-commands */ 2767 if (comment != NULL && line[0] != '\t') { 2768 line_end = comment; 2769 *line_end = 0; 2770 } 2771 2772 /* If we didn't see a '\\' then the in-situ data is fine */ 2773 if (escaped == NULL) { 2774 *length = line_end - line; 2775 return line; 2776 } 2777 2778 /* Remove escapes from '\n' and '#' */ 2779 tp = ptr = escaped; 2780 escaped = line; 2781 for (; ; *tp++ = ch) { 2782 ch = *ptr++; 2783 if (ch != '\\') { 2784 if (ch == 0) 2785 break; 2786 continue; 2787 } 2788 2789 ch = *ptr++; 2790 if (ch == 0) { 2791 /* Delete '\\' at end of buffer */ 2792 tp--; 2793 break; 2794 } 2795 2796 if (ch == '#' && line[0] != '\t') 2797 /* Delete '\\' from before '#' on non-command lines */ 2798 continue; 2799 2800 if (ch != '\n') { 2801 /* Leave '\\' in buffer for later */ 2802 *tp++ = '\\'; 2803 /* Make sure we don't delete an escaped ' ' from the line end */ 2804 escaped = tp + 1; 2805 continue; 2806 } 2807 2808 /* Escaped '\n' replace following whitespace with a single ' ' */ 2809 while (ptr[0] == ' ' || ptr[0] == '\t') 2810 ptr++; 2811 ch = ' '; 2812 } 2813 2814 /* Delete any trailing spaces - eg from empty continuations */ 2815 while (tp > escaped && isspace((unsigned char)tp[-1])) 2816 tp--; 2817 2818 *tp = 0; 2819 *length = tp - line; 2820 return line; 2821 } 2822 2823 /*- 2824 *--------------------------------------------------------------------- 2825 * ParseReadLine -- 2826 * Read an entire line from the input file. Called only by Parse_File. 2827 * 2828 * Results: 2829 * A line w/o its newline 2830 * 2831 * Side Effects: 2832 * Only those associated with reading a character 2833 *--------------------------------------------------------------------- 2834 */ 2835 static char * 2836 ParseReadLine(void) 2837 { 2838 char *line; /* Result */ 2839 int lineLength; /* Length of result */ 2840 int lineno; /* Saved line # */ 2841 int rval; 2842 2843 for (;;) { 2844 line = ParseGetLine(0, &lineLength); 2845 if (line == NULL) 2846 return NULL; 2847 2848 if (line[0] != '.') 2849 return line; 2850 2851 /* 2852 * The line might be a conditional. Ask the conditional module 2853 * about it and act accordingly 2854 */ 2855 switch (Cond_Eval(line)) { 2856 case COND_SKIP: 2857 /* Skip to next conditional that evaluates to COND_PARSE. */ 2858 do { 2859 line = ParseGetLine(PARSE_SKIP, &lineLength); 2860 } while (line && Cond_Eval(line) != COND_PARSE); 2861 if (line == NULL) 2862 break; 2863 continue; 2864 case COND_PARSE: 2865 continue; 2866 case COND_INVALID: /* Not a conditional line */ 2867 /* Check for .for loops */ 2868 rval = For_Eval(line); 2869 if (rval == 0) 2870 /* Not a .for line */ 2871 break; 2872 if (rval < 0) 2873 /* Syntax error - error printed, ignore line */ 2874 continue; 2875 /* Start of a .for loop */ 2876 lineno = curFile->lineno; 2877 /* Accumulate loop lines until matching .endfor */ 2878 do { 2879 line = ParseGetLine(PARSE_RAW, &lineLength); 2880 if (line == NULL) { 2881 Parse_Error(PARSE_FATAL, 2882 "Unexpected end of file in for loop."); 2883 break; 2884 } 2885 } while (For_Accum(line)); 2886 /* Stash each iteration as a new 'input file' */ 2887 For_Run(lineno); 2888 /* Read next line from for-loop buffer */ 2889 continue; 2890 } 2891 return (line); 2892 } 2893 } 2894 2895 /*- 2896 *----------------------------------------------------------------------- 2897 * ParseFinishLine -- 2898 * Handle the end of a dependency group. 2899 * 2900 * Results: 2901 * Nothing. 2902 * 2903 * Side Effects: 2904 * inLine set FALSE. 'targets' list destroyed. 2905 * 2906 *----------------------------------------------------------------------- 2907 */ 2908 static void 2909 ParseFinishLine(void) 2910 { 2911 if (inLine) { 2912 Lst_ForEach(targets, Suff_EndTransform, NULL); 2913 Lst_Destroy(targets, ParseHasCommands); 2914 targets = NULL; 2915 inLine = FALSE; 2916 } 2917 } 2918 2919 2920 /*- 2921 *--------------------------------------------------------------------- 2922 * Parse_File -- 2923 * Parse a file into its component parts, incorporating it into the 2924 * current dependency graph. This is the main function and controls 2925 * almost every other function in this module 2926 * 2927 * Input: 2928 * name the name of the file being read 2929 * fd Open file to makefile to parse 2930 * 2931 * Results: 2932 * None 2933 * 2934 * Side Effects: 2935 * closes fd. 2936 * Loads. Nodes are added to the list of all targets, nodes and links 2937 * are added to the dependency graph. etc. etc. etc. 2938 *--------------------------------------------------------------------- 2939 */ 2940 void 2941 Parse_File(const char *name, int fd) 2942 { 2943 char *cp; /* pointer into the line */ 2944 char *line; /* the line we're working on */ 2945 struct loadedfile *lf; 2946 2947 lf = loadfile(name, fd); 2948 2949 inLine = FALSE; 2950 fatals = 0; 2951 2952 if (name == NULL) { 2953 name = "(stdin)"; 2954 } 2955 2956 Parse_SetInput(name, 0, -1, loadedfile_nextbuf, lf); 2957 curFile->lf = lf; 2958 2959 do { 2960 for (; (line = ParseReadLine()) != NULL; ) { 2961 if (DEBUG(PARSE)) 2962 fprintf(debug_file, "ParseReadLine (%d): '%s'\n", 2963 curFile->lineno, line); 2964 if (*line == '.') { 2965 /* 2966 * Lines that begin with the special character may be 2967 * include or undef directives. 2968 * On the other hand they can be suffix rules (.c.o: ...) 2969 * or just dependencies for filenames that start '.'. 2970 */ 2971 for (cp = line + 1; isspace((unsigned char)*cp); cp++) { 2972 continue; 2973 } 2974 if (strncmp(cp, "include", 7) == 0 || 2975 ((cp[0] == 's' || cp[0] == '-') && 2976 strncmp(&cp[1], "include", 7) == 0)) { 2977 ParseDoInclude(cp); 2978 continue; 2979 } 2980 if (strncmp(cp, "undef", 5) == 0) { 2981 char *cp2; 2982 for (cp += 5; isspace((unsigned char) *cp); cp++) 2983 continue; 2984 for (cp2 = cp; !isspace((unsigned char) *cp2) && 2985 (*cp2 != '\0'); cp2++) 2986 continue; 2987 *cp2 = '\0'; 2988 Var_Delete(cp, VAR_GLOBAL); 2989 continue; 2990 } else if (strncmp(cp, "export", 6) == 0) { 2991 for (cp += 6; isspace((unsigned char) *cp); cp++) 2992 continue; 2993 Var_Export(cp, 1); 2994 continue; 2995 } else if (strncmp(cp, "unexport", 8) == 0) { 2996 Var_UnExport(cp); 2997 continue; 2998 } else if (strncmp(cp, "info", 4) == 0 || 2999 strncmp(cp, "error", 5) == 0 || 3000 strncmp(cp, "warning", 7) == 0) { 3001 if (ParseMessage(cp)) 3002 continue; 3003 } 3004 } 3005 3006 if (*line == '\t') { 3007 /* 3008 * If a line starts with a tab, it can only hope to be 3009 * a creation command. 3010 */ 3011 cp = line + 1; 3012 shellCommand: 3013 for (; isspace ((unsigned char)*cp); cp++) { 3014 continue; 3015 } 3016 if (*cp) { 3017 if (!inLine) 3018 Parse_Error(PARSE_FATAL, 3019 "Unassociated shell command \"%s\"", 3020 cp); 3021 /* 3022 * So long as it's not a blank line and we're actually 3023 * in a dependency spec, add the command to the list of 3024 * commands of all targets in the dependency spec 3025 */ 3026 if (targets) { 3027 cp = bmake_strdup(cp); 3028 Lst_ForEach(targets, ParseAddCmd, cp); 3029 #ifdef CLEANUP 3030 Lst_AtEnd(targCmds, cp); 3031 #endif 3032 } 3033 } 3034 continue; 3035 } 3036 3037 #ifdef SYSVINCLUDE 3038 if (((strncmp(line, "include", 7) == 0 && 3039 isspace((unsigned char) line[7])) || 3040 ((line[0] == 's' || line[0] == '-') && 3041 strncmp(&line[1], "include", 7) == 0 && 3042 isspace((unsigned char) line[8]))) && 3043 strchr(line, ':') == NULL) { 3044 /* 3045 * It's an S3/S5-style "include". 3046 */ 3047 ParseTraditionalInclude(line); 3048 continue; 3049 } 3050 #endif 3051 #ifdef GMAKEEXPORT 3052 if (strncmp(line, "export", 6) == 0 && 3053 isspace((unsigned char) line[6]) && 3054 strchr(line, ':') == NULL) { 3055 /* 3056 * It's a Gmake "export". 3057 */ 3058 ParseGmakeExport(line); 3059 continue; 3060 } 3061 #endif 3062 if (Parse_IsVar(line)) { 3063 ParseFinishLine(); 3064 Parse_DoVar(line, VAR_GLOBAL); 3065 continue; 3066 } 3067 3068 #ifndef POSIX 3069 /* 3070 * To make life easier on novices, if the line is indented we 3071 * first make sure the line has a dependency operator in it. 3072 * If it doesn't have an operator and we're in a dependency 3073 * line's script, we assume it's actually a shell command 3074 * and add it to the current list of targets. 3075 */ 3076 cp = line; 3077 if (isspace((unsigned char) line[0])) { 3078 while ((*cp != '\0') && isspace((unsigned char) *cp)) 3079 cp++; 3080 while (*cp && (ParseIsEscaped(line, cp) || 3081 (*cp != ':') && (*cp != '!'))) { 3082 cp++; 3083 } 3084 if (*cp == '\0') { 3085 if (inLine) { 3086 Parse_Error(PARSE_WARNING, 3087 "Shell command needs a leading tab"); 3088 goto shellCommand; 3089 } 3090 } 3091 } 3092 #endif 3093 ParseFinishLine(); 3094 3095 /* 3096 * For some reason - probably to make the parser impossible - 3097 * a ';' can be used to separate commands from dependencies. 3098 * Attempt to avoid ';' inside substitution patterns. 3099 */ 3100 { 3101 int level = 0; 3102 3103 for (cp = line; *cp != 0; cp++) { 3104 if (*cp == '\\' && cp[1] != 0) { 3105 cp++; 3106 continue; 3107 } 3108 if (*cp == '$' && 3109 (cp[1] == '(' || cp[1] == '{')) { 3110 level++; 3111 continue; 3112 } 3113 if (level > 0) { 3114 if (*cp == ')' || *cp == '}') { 3115 level--; 3116 continue; 3117 } 3118 } else if (*cp == ';') { 3119 break; 3120 } 3121 } 3122 } 3123 if (*cp != 0) 3124 /* Terminate the dependency list at the ';' */ 3125 *cp++ = 0; 3126 else 3127 cp = NULL; 3128 3129 /* 3130 * We now know it's a dependency line so it needs to have all 3131 * variables expanded before being parsed. Tell the variable 3132 * module to complain if some variable is undefined... 3133 */ 3134 line = Var_Subst(NULL, line, VAR_CMD, TRUE, TRUE, FALSE); 3135 3136 /* 3137 * Need a non-circular list for the target nodes 3138 */ 3139 if (targets) 3140 Lst_Destroy(targets, NULL); 3141 3142 targets = Lst_Init(FALSE); 3143 inLine = TRUE; 3144 3145 ParseDoDependency(line); 3146 free(line); 3147 3148 /* If there were commands after a ';', add them now */ 3149 if (cp != NULL) { 3150 goto shellCommand; 3151 } 3152 } 3153 /* 3154 * Reached EOF, but it may be just EOF of an include file... 3155 */ 3156 } while (ParseEOF() == CONTINUE); 3157 3158 if (fatals) { 3159 (void)fflush(stdout); 3160 (void)fprintf(stderr, 3161 "%s: Fatal errors encountered -- cannot continue", 3162 progname); 3163 PrintOnError(NULL, NULL); 3164 exit(1); 3165 } 3166 } 3167 3168 /*- 3169 *--------------------------------------------------------------------- 3170 * Parse_Init -- 3171 * initialize the parsing module 3172 * 3173 * Results: 3174 * none 3175 * 3176 * Side Effects: 3177 * the parseIncPath list is initialized... 3178 *--------------------------------------------------------------------- 3179 */ 3180 void 3181 Parse_Init(void) 3182 { 3183 mainNode = NULL; 3184 parseIncPath = Lst_Init(FALSE); 3185 sysIncPath = Lst_Init(FALSE); 3186 defIncPath = Lst_Init(FALSE); 3187 includes = Lst_Init(FALSE); 3188 #ifdef CLEANUP 3189 targCmds = Lst_Init(FALSE); 3190 #endif 3191 } 3192 3193 void 3194 Parse_End(void) 3195 { 3196 #ifdef CLEANUP 3197 Lst_Destroy(targCmds, (FreeProc *)free); 3198 if (targets) 3199 Lst_Destroy(targets, NULL); 3200 Lst_Destroy(defIncPath, Dir_Destroy); 3201 Lst_Destroy(sysIncPath, Dir_Destroy); 3202 Lst_Destroy(parseIncPath, Dir_Destroy); 3203 Lst_Destroy(includes, NULL); /* Should be empty now */ 3204 #endif 3205 } 3206 3207 3208 /*- 3209 *----------------------------------------------------------------------- 3210 * Parse_MainName -- 3211 * Return a Lst of the main target to create for main()'s sake. If 3212 * no such target exists, we Punt with an obnoxious error message. 3213 * 3214 * Results: 3215 * A Lst of the single node to create. 3216 * 3217 * Side Effects: 3218 * None. 3219 * 3220 *----------------------------------------------------------------------- 3221 */ 3222 Lst 3223 Parse_MainName(void) 3224 { 3225 Lst mainList; /* result list */ 3226 3227 mainList = Lst_Init(FALSE); 3228 3229 if (mainNode == NULL) { 3230 Punt("no target to make."); 3231 /*NOTREACHED*/ 3232 } else if (mainNode->type & OP_DOUBLEDEP) { 3233 (void)Lst_AtEnd(mainList, mainNode); 3234 Lst_Concat(mainList, mainNode->cohorts, LST_CONCNEW); 3235 } 3236 else 3237 (void)Lst_AtEnd(mainList, mainNode); 3238 Var_Append(".TARGETS", mainNode->name, VAR_GLOBAL); 3239 return (mainList); 3240 } 3241 3242 /*- 3243 *----------------------------------------------------------------------- 3244 * ParseMark -- 3245 * Add the filename and lineno to the GNode so that we remember 3246 * where it was first defined. 3247 * 3248 * Side Effects: 3249 * None. 3250 * 3251 *----------------------------------------------------------------------- 3252 */ 3253 static void 3254 ParseMark(GNode *gn) 3255 { 3256 gn->fname = curFile->fname; 3257 gn->lineno = curFile->lineno; 3258 } 3259