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