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