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