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