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