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