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