1 /* $NetBSD: parse.c,v 1.192 2013/10/18 20:47:06 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1988, 1989, 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Adam de Boor. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 /* 36 * Copyright (c) 1989 by Berkeley Softworks 37 * All rights reserved. 38 * 39 * This code is derived from software contributed to Berkeley by 40 * Adam de Boor. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. All advertising materials mentioning features or use of this software 51 * must display the following acknowledgement: 52 * This product includes software developed by the University of 53 * California, Berkeley and its contributors. 54 * 4. Neither the name of the University nor the names of its contributors 55 * may be used to endorse or promote products derived from this software 56 * without specific prior written permission. 57 * 58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 68 * SUCH DAMAGE. 69 */ 70 71 #ifndef MAKE_NATIVE 72 static char rcsid[] = "$NetBSD: parse.c,v 1.192 2013/10/18 20:47:06 christos Exp $"; 73 #else 74 #include <sys/cdefs.h> 75 #ifndef lint 76 #if 0 77 static char sccsid[] = "@(#)parse.c 8.3 (Berkeley) 3/19/94"; 78 #else 79 __RCSID("$NetBSD: parse.c,v 1.192 2013/10/18 20:47:06 christos Exp $"); 80 #endif 81 #endif /* not lint */ 82 #endif 83 84 /*- 85 * parse.c -- 86 * Functions to parse a makefile. 87 * 88 * One function, Parse_Init, must be called before any functions 89 * in this module are used. After that, the function Parse_File is the 90 * main entry point and controls most of the other functions in this 91 * module. 92 * 93 * Most important structures are kept in Lsts. Directories for 94 * the .include "..." function are kept in the 'parseIncPath' Lst, while 95 * those for the .include <...> are kept in the 'sysIncPath' Lst. The 96 * targets currently being defined are kept in the 'targets' Lst. 97 * 98 * The variables 'fname' and 'lineno' are used to track the name 99 * of the current file and the line number in that file so that error 100 * messages can be more meaningful. 101 * 102 * Interface: 103 * Parse_Init Initialization function which must be 104 * called before anything else in this module 105 * is used. 106 * 107 * Parse_End Cleanup the module 108 * 109 * Parse_File Function used to parse a makefile. It must 110 * be given the name of the file, which should 111 * already have been opened, and a function 112 * to call to read a character from the file. 113 * 114 * Parse_IsVar Returns TRUE if the given line is a 115 * variable assignment. Used by MainParseArgs 116 * to determine if an argument is a target 117 * or a variable assignment. Used internally 118 * for pretty much the same thing... 119 * 120 * Parse_Error Function called when an error occurs in 121 * parsing. Used by the variable and 122 * conditional modules. 123 * Parse_MainName Returns a Lst of the main target to create. 124 */ 125 126 #include <sys/types.h> 127 #include <sys/mman.h> 128 #include <sys/stat.h> 129 #include <assert.h> 130 #include <ctype.h> 131 #include <errno.h> 132 #include <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 1210 (void)Var_Parse(cp, VAR_CMD, TRUE, &length, &freeIt); 1211 if (freeIt) 1212 free(freeIt); 1213 cp += length-1; 1214 } 1215 } 1216 1217 if (!ParseIsEscaped(lstart, cp) && *cp == LPAREN) { 1218 /* 1219 * Archives must be handled specially to make sure the OP_ARCHV 1220 * flag is set in their 'type' field, for one thing, and because 1221 * things like "archive(file1.o file2.o file3.o)" are permissible. 1222 * Arch_ParseArchive will set 'line' to be the first non-blank 1223 * after the archive-spec. It creates/finds nodes for the members 1224 * and places them on the given list, returning SUCCESS if all 1225 * went well and FAILURE if there was an error in the 1226 * specification. On error, line should remain untouched. 1227 */ 1228 if (Arch_ParseArchive(&line, targets, VAR_CMD) != SUCCESS) { 1229 Parse_Error(PARSE_FATAL, 1230 "Error in archive specification: \"%s\"", line); 1231 goto out; 1232 } else { 1233 continue; 1234 } 1235 } 1236 savec = *cp; 1237 1238 if (!*cp) { 1239 /* 1240 * Ending a dependency line without an operator is a Bozo 1241 * no-no. As a heuristic, this is also often triggered by 1242 * undetected conflicts from cvs/rcs merges. 1243 */ 1244 if ((strncmp(line, "<<<<<<", 6) == 0) || 1245 (strncmp(line, "======", 6) == 0) || 1246 (strncmp(line, ">>>>>>", 6) == 0)) 1247 Parse_Error(PARSE_FATAL, 1248 "Makefile appears to contain unresolved cvs/rcs/??? merge conflicts"); 1249 else 1250 Parse_Error(PARSE_FATAL, lstart[0] == '.' ? "Unknown directive" 1251 : "Need an operator"); 1252 goto out; 1253 } 1254 *cp = '\0'; 1255 1256 /* 1257 * Have a word in line. See if it's a special target and set 1258 * specType to match it. 1259 */ 1260 if (*line == '.' && isupper ((unsigned char)line[1])) { 1261 /* 1262 * See if the target is a special target that must have it 1263 * or its sources handled specially. 1264 */ 1265 int keywd = ParseFindKeyword(line); 1266 if (keywd != -1) { 1267 if (specType == ExPath && parseKeywords[keywd].spec != ExPath) { 1268 Parse_Error(PARSE_FATAL, "Mismatched special targets"); 1269 goto out; 1270 } 1271 1272 specType = parseKeywords[keywd].spec; 1273 tOp = parseKeywords[keywd].op; 1274 1275 /* 1276 * Certain special targets have special semantics: 1277 * .PATH Have to set the dirSearchPath 1278 * variable too 1279 * .MAIN Its sources are only used if 1280 * nothing has been specified to 1281 * create. 1282 * .DEFAULT Need to create a node to hang 1283 * commands on, but we don't want 1284 * it in the graph, nor do we want 1285 * it to be the Main Target, so we 1286 * create it, set OP_NOTMAIN and 1287 * add it to the list, setting 1288 * DEFAULT to the new node for 1289 * later use. We claim the node is 1290 * A transformation rule to make 1291 * life easier later, when we'll 1292 * use Make_HandleUse to actually 1293 * apply the .DEFAULT commands. 1294 * .PHONY The list of targets 1295 * .NOPATH Don't search for file in the path 1296 * .STALE 1297 * .BEGIN 1298 * .END 1299 * .ERROR 1300 * .INTERRUPT Are not to be considered the 1301 * main target. 1302 * .NOTPARALLEL Make only one target at a time. 1303 * .SINGLESHELL Create a shell for each command. 1304 * .ORDER Must set initial predecessor to NULL 1305 */ 1306 switch (specType) { 1307 case ExPath: 1308 if (paths == NULL) { 1309 paths = Lst_Init(FALSE); 1310 } 1311 (void)Lst_AtEnd(paths, dirSearchPath); 1312 break; 1313 case Main: 1314 if (!Lst_IsEmpty(create)) { 1315 specType = Not; 1316 } 1317 break; 1318 case Begin: 1319 case End: 1320 case Stale: 1321 case dotError: 1322 case Interrupt: 1323 gn = Targ_FindNode(line, TARG_CREATE); 1324 if (doing_depend) 1325 ParseMark(gn); 1326 gn->type |= OP_NOTMAIN|OP_SPECIAL; 1327 (void)Lst_AtEnd(targets, gn); 1328 break; 1329 case Default: 1330 gn = Targ_NewGN(".DEFAULT"); 1331 gn->type |= (OP_NOTMAIN|OP_TRANSFORM); 1332 (void)Lst_AtEnd(targets, gn); 1333 DEFAULT = gn; 1334 break; 1335 case NotParallel: 1336 maxJobs = 1; 1337 break; 1338 case SingleShell: 1339 compatMake = TRUE; 1340 break; 1341 case Order: 1342 predecessor = NULL; 1343 break; 1344 default: 1345 break; 1346 } 1347 } else if (strncmp(line, ".PATH", 5) == 0) { 1348 /* 1349 * .PATH<suffix> has to be handled specially. 1350 * Call on the suffix module to give us a path to 1351 * modify. 1352 */ 1353 Lst path; 1354 1355 specType = ExPath; 1356 path = Suff_GetPath(&line[5]); 1357 if (path == NULL) { 1358 Parse_Error(PARSE_FATAL, 1359 "Suffix '%s' not defined (yet)", 1360 &line[5]); 1361 goto out; 1362 } else { 1363 if (paths == NULL) { 1364 paths = Lst_Init(FALSE); 1365 } 1366 (void)Lst_AtEnd(paths, path); 1367 } 1368 } 1369 } 1370 1371 /* 1372 * Have word in line. Get or create its node and stick it at 1373 * the end of the targets list 1374 */ 1375 if ((specType == Not) && (*line != '\0')) { 1376 if (Dir_HasWildcards(line)) { 1377 /* 1378 * Targets are to be sought only in the current directory, 1379 * so create an empty path for the thing. Note we need to 1380 * use Dir_Destroy in the destruction of the path as the 1381 * Dir module could have added a directory to the path... 1382 */ 1383 Lst emptyPath = Lst_Init(FALSE); 1384 1385 Dir_Expand(line, emptyPath, curTargs); 1386 1387 Lst_Destroy(emptyPath, Dir_Destroy); 1388 } else { 1389 /* 1390 * No wildcards, but we want to avoid code duplication, 1391 * so create a list with the word on it. 1392 */ 1393 (void)Lst_AtEnd(curTargs, line); 1394 } 1395 1396 while(!Lst_IsEmpty(curTargs)) { 1397 char *targName = (char *)Lst_DeQueue(curTargs); 1398 1399 if (!Suff_IsTransform (targName)) { 1400 gn = Targ_FindNode(targName, TARG_CREATE); 1401 } else { 1402 gn = Suff_AddTransform(targName); 1403 } 1404 if (doing_depend) 1405 ParseMark(gn); 1406 1407 (void)Lst_AtEnd(targets, gn); 1408 } 1409 } else if (specType == ExPath && *line != '.' && *line != '\0') { 1410 Parse_Error(PARSE_WARNING, "Extra target (%s) ignored", line); 1411 } 1412 1413 *cp = savec; 1414 /* 1415 * If it is a special type and not .PATH, it's the only target we 1416 * allow on this line... 1417 */ 1418 if (specType != Not && specType != ExPath) { 1419 Boolean warning = FALSE; 1420 1421 while (*cp && (ParseIsEscaped(lstart, cp) || 1422 ((*cp != '!') && (*cp != ':')))) { 1423 if (ParseIsEscaped(lstart, cp) || 1424 (*cp != ' ' && *cp != '\t')) { 1425 warning = TRUE; 1426 } 1427 cp++; 1428 } 1429 if (warning) { 1430 Parse_Error(PARSE_WARNING, "Extra target ignored"); 1431 } 1432 } else { 1433 while (*cp && isspace ((unsigned char)*cp)) { 1434 cp++; 1435 } 1436 } 1437 line = cp; 1438 } while (*line && (ParseIsEscaped(lstart, line) || 1439 ((*line != '!') && (*line != ':')))); 1440 1441 /* 1442 * Don't need the list of target names anymore... 1443 */ 1444 Lst_Destroy(curTargs, NULL); 1445 curTargs = NULL; 1446 1447 if (!Lst_IsEmpty(targets)) { 1448 switch(specType) { 1449 default: 1450 Parse_Error(PARSE_WARNING, "Special and mundane targets don't mix. Mundane ones ignored"); 1451 break; 1452 case Default: 1453 case Stale: 1454 case Begin: 1455 case End: 1456 case dotError: 1457 case Interrupt: 1458 /* 1459 * These four create nodes on which to hang commands, so 1460 * targets shouldn't be empty... 1461 */ 1462 case Not: 1463 /* 1464 * Nothing special here -- targets can be empty if it wants. 1465 */ 1466 break; 1467 } 1468 } 1469 1470 /* 1471 * Have now parsed all the target names. Must parse the operator next. The 1472 * result is left in op . 1473 */ 1474 if (*cp == '!') { 1475 op = OP_FORCE; 1476 } else if (*cp == ':') { 1477 if (cp[1] == ':') { 1478 op = OP_DOUBLEDEP; 1479 cp++; 1480 } else { 1481 op = OP_DEPENDS; 1482 } 1483 } else { 1484 Parse_Error(PARSE_FATAL, lstart[0] == '.' ? "Unknown directive" 1485 : "Missing dependency operator"); 1486 goto out; 1487 } 1488 1489 cp++; /* Advance beyond operator */ 1490 1491 Lst_ForEach(targets, ParseDoOp, &op); 1492 1493 /* 1494 * Get to the first source 1495 */ 1496 while (*cp && isspace ((unsigned char)*cp)) { 1497 cp++; 1498 } 1499 line = cp; 1500 1501 /* 1502 * Several special targets take different actions if present with no 1503 * sources: 1504 * a .SUFFIXES line with no sources clears out all old suffixes 1505 * a .PRECIOUS line makes all targets precious 1506 * a .IGNORE line ignores errors for all targets 1507 * a .SILENT line creates silence when making all targets 1508 * a .PATH removes all directories from the search path(s). 1509 */ 1510 if (!*line) { 1511 switch (specType) { 1512 case Suffixes: 1513 Suff_ClearSuffixes(); 1514 break; 1515 case Precious: 1516 allPrecious = TRUE; 1517 break; 1518 case Ignore: 1519 ignoreErrors = TRUE; 1520 break; 1521 case Silent: 1522 beSilent = TRUE; 1523 break; 1524 case ExPath: 1525 Lst_ForEach(paths, ParseClearPath, NULL); 1526 Dir_SetPATH(); 1527 break; 1528 #ifdef POSIX 1529 case Posix: 1530 Var_Set("%POSIX", "1003.2", VAR_GLOBAL, 0); 1531 break; 1532 #endif 1533 default: 1534 break; 1535 } 1536 } else if (specType == MFlags) { 1537 /* 1538 * Call on functions in main.c to deal with these arguments and 1539 * set the initial character to a null-character so the loop to 1540 * get sources won't get anything 1541 */ 1542 Main_ParseArgLine(line); 1543 *line = '\0'; 1544 } else if (specType == ExShell) { 1545 if (Job_ParseShell(line) != SUCCESS) { 1546 Parse_Error(PARSE_FATAL, "improper shell specification"); 1547 goto out; 1548 } 1549 *line = '\0'; 1550 } else if ((specType == NotParallel) || (specType == SingleShell)) { 1551 *line = '\0'; 1552 } 1553 1554 /* 1555 * NOW GO FOR THE SOURCES 1556 */ 1557 if ((specType == Suffixes) || (specType == ExPath) || 1558 (specType == Includes) || (specType == Libs) || 1559 (specType == Null) || (specType == ExObjdir)) 1560 { 1561 while (*line) { 1562 /* 1563 * If the target was one that doesn't take files as its sources 1564 * but takes something like suffixes, we take each 1565 * space-separated word on the line as a something and deal 1566 * with it accordingly. 1567 * 1568 * If the target was .SUFFIXES, we take each source as a 1569 * suffix and add it to the list of suffixes maintained by the 1570 * Suff module. 1571 * 1572 * If the target was a .PATH, we add the source as a directory 1573 * to search on the search path. 1574 * 1575 * If it was .INCLUDES, the source is taken to be the suffix of 1576 * files which will be #included and whose search path should 1577 * be present in the .INCLUDES variable. 1578 * 1579 * If it was .LIBS, the source is taken to be the suffix of 1580 * files which are considered libraries and whose search path 1581 * should be present in the .LIBS variable. 1582 * 1583 * If it was .NULL, the source is the suffix to use when a file 1584 * has no valid suffix. 1585 * 1586 * If it was .OBJDIR, the source is a new definition for .OBJDIR, 1587 * and will cause make to do a new chdir to that path. 1588 */ 1589 while (*cp && !isspace ((unsigned char)*cp)) { 1590 cp++; 1591 } 1592 savec = *cp; 1593 *cp = '\0'; 1594 switch (specType) { 1595 case Suffixes: 1596 Suff_AddSuffix(line, &mainNode); 1597 break; 1598 case ExPath: 1599 Lst_ForEach(paths, ParseAddDir, line); 1600 break; 1601 case Includes: 1602 Suff_AddInclude(line); 1603 break; 1604 case Libs: 1605 Suff_AddLib(line); 1606 break; 1607 case Null: 1608 Suff_SetNull(line); 1609 break; 1610 case ExObjdir: 1611 Main_SetObjdir(line); 1612 break; 1613 default: 1614 break; 1615 } 1616 *cp = savec; 1617 if (savec != '\0') { 1618 cp++; 1619 } 1620 while (*cp && isspace ((unsigned char)*cp)) { 1621 cp++; 1622 } 1623 line = cp; 1624 } 1625 if (paths) { 1626 Lst_Destroy(paths, NULL); 1627 } 1628 if (specType == ExPath) 1629 Dir_SetPATH(); 1630 } else { 1631 while (*line) { 1632 /* 1633 * The targets take real sources, so we must beware of archive 1634 * specifications (i.e. things with left parentheses in them) 1635 * and handle them accordingly. 1636 */ 1637 for (; *cp && !isspace ((unsigned char)*cp); cp++) { 1638 if ((*cp == LPAREN) && (cp > line) && (cp[-1] != '$')) { 1639 /* 1640 * Only stop for a left parenthesis if it isn't at the 1641 * start of a word (that'll be for variable changes 1642 * later) and isn't preceded by a dollar sign (a dynamic 1643 * source). 1644 */ 1645 break; 1646 } 1647 } 1648 1649 if (*cp == LPAREN) { 1650 sources = Lst_Init(FALSE); 1651 if (Arch_ParseArchive(&line, sources, VAR_CMD) != SUCCESS) { 1652 Parse_Error(PARSE_FATAL, 1653 "Error in source archive spec \"%s\"", line); 1654 goto out; 1655 } 1656 1657 while (!Lst_IsEmpty (sources)) { 1658 gn = (GNode *)Lst_DeQueue(sources); 1659 ParseDoSrc(tOp, gn->name); 1660 } 1661 Lst_Destroy(sources, NULL); 1662 cp = line; 1663 } else { 1664 if (*cp) { 1665 *cp = '\0'; 1666 cp += 1; 1667 } 1668 1669 ParseDoSrc(tOp, line); 1670 } 1671 while (*cp && isspace ((unsigned char)*cp)) { 1672 cp++; 1673 } 1674 line = cp; 1675 } 1676 } 1677 1678 if (mainNode == NULL) { 1679 /* 1680 * If we have yet to decide on a main target to make, in the 1681 * absence of any user input, we want the first target on 1682 * the first dependency line that is actually a real target 1683 * (i.e. isn't a .USE or .EXEC rule) to be made. 1684 */ 1685 Lst_ForEach(targets, ParseFindMain, NULL); 1686 } 1687 1688 out: 1689 if (curTargs) 1690 Lst_Destroy(curTargs, NULL); 1691 } 1692 1693 /*- 1694 *--------------------------------------------------------------------- 1695 * Parse_IsVar -- 1696 * Return TRUE if the passed line is a variable assignment. A variable 1697 * assignment consists of a single word followed by optional whitespace 1698 * followed by either a += or an = operator. 1699 * This function is used both by the Parse_File function and main when 1700 * parsing the command-line arguments. 1701 * 1702 * Input: 1703 * line the line to check 1704 * 1705 * Results: 1706 * TRUE if it is. FALSE if it ain't 1707 * 1708 * Side Effects: 1709 * none 1710 *--------------------------------------------------------------------- 1711 */ 1712 Boolean 1713 Parse_IsVar(char *line) 1714 { 1715 Boolean wasSpace = FALSE; /* set TRUE if found a space */ 1716 char ch; 1717 int level = 0; 1718 #define ISEQOPERATOR(c) \ 1719 (((c) == '+') || ((c) == ':') || ((c) == '?') || ((c) == '!')) 1720 1721 /* 1722 * Skip to variable name 1723 */ 1724 for (;(*line == ' ') || (*line == '\t'); line++) 1725 continue; 1726 1727 /* Scan for one of the assignment operators outside a variable expansion */ 1728 while ((ch = *line++) != 0) { 1729 if (ch == '(' || ch == '{') { 1730 level++; 1731 continue; 1732 } 1733 if (ch == ')' || ch == '}') { 1734 level--; 1735 continue; 1736 } 1737 if (level != 0) 1738 continue; 1739 while (ch == ' ' || ch == '\t') { 1740 ch = *line++; 1741 wasSpace = TRUE; 1742 } 1743 #ifdef SUNSHCMD 1744 if (ch == ':' && strncmp(line, "sh", 2) == 0) { 1745 line += 2; 1746 continue; 1747 } 1748 #endif 1749 if (ch == '=') 1750 return TRUE; 1751 if (*line == '=' && ISEQOPERATOR(ch)) 1752 return TRUE; 1753 if (wasSpace) 1754 return FALSE; 1755 } 1756 1757 return FALSE; 1758 } 1759 1760 /*- 1761 *--------------------------------------------------------------------- 1762 * Parse_DoVar -- 1763 * Take the variable assignment in the passed line and do it in the 1764 * global context. 1765 * 1766 * Note: There is a lexical ambiguity with assignment modifier characters 1767 * in variable names. This routine interprets the character before the = 1768 * as a modifier. Therefore, an assignment like 1769 * C++=/usr/bin/CC 1770 * is interpreted as "C+ +=" instead of "C++ =". 1771 * 1772 * Input: 1773 * line a line guaranteed to be a variable assignment. 1774 * This reduces error checks 1775 * ctxt Context in which to do the assignment 1776 * 1777 * Results: 1778 * none 1779 * 1780 * Side Effects: 1781 * the variable structure of the given variable name is altered in the 1782 * global context. 1783 *--------------------------------------------------------------------- 1784 */ 1785 void 1786 Parse_DoVar(char *line, GNode *ctxt) 1787 { 1788 char *cp; /* pointer into line */ 1789 enum { 1790 VAR_SUBST, VAR_APPEND, VAR_SHELL, VAR_NORMAL 1791 } type; /* Type of assignment */ 1792 char *opc; /* ptr to operator character to 1793 * null-terminate the variable name */ 1794 Boolean freeCp = FALSE; /* TRUE if cp needs to be freed, 1795 * i.e. if any variable expansion was 1796 * performed */ 1797 int depth; 1798 1799 /* 1800 * Skip to variable name 1801 */ 1802 while ((*line == ' ') || (*line == '\t')) { 1803 line++; 1804 } 1805 1806 /* 1807 * Skip to operator character, nulling out whitespace as we go 1808 * XXX Rather than counting () and {} we should look for $ and 1809 * then expand the variable. 1810 */ 1811 for (depth = 0, cp = line + 1; depth != 0 || *cp != '='; cp++) { 1812 if (*cp == '(' || *cp == '{') { 1813 depth++; 1814 continue; 1815 } 1816 if (*cp == ')' || *cp == '}') { 1817 depth--; 1818 continue; 1819 } 1820 if (depth == 0 && isspace ((unsigned char)*cp)) { 1821 *cp = '\0'; 1822 } 1823 } 1824 opc = cp-1; /* operator is the previous character */ 1825 *cp++ = '\0'; /* nuke the = */ 1826 1827 /* 1828 * Check operator type 1829 */ 1830 switch (*opc) { 1831 case '+': 1832 type = VAR_APPEND; 1833 *opc = '\0'; 1834 break; 1835 1836 case '?': 1837 /* 1838 * If the variable already has a value, we don't do anything. 1839 */ 1840 *opc = '\0'; 1841 if (Var_Exists(line, ctxt)) { 1842 return; 1843 } else { 1844 type = VAR_NORMAL; 1845 } 1846 break; 1847 1848 case ':': 1849 type = VAR_SUBST; 1850 *opc = '\0'; 1851 break; 1852 1853 case '!': 1854 type = VAR_SHELL; 1855 *opc = '\0'; 1856 break; 1857 1858 default: 1859 #ifdef SUNSHCMD 1860 while (opc > line && *opc != ':') 1861 opc--; 1862 1863 if (strncmp(opc, ":sh", 3) == 0) { 1864 type = VAR_SHELL; 1865 *opc = '\0'; 1866 break; 1867 } 1868 #endif 1869 type = VAR_NORMAL; 1870 break; 1871 } 1872 1873 while (isspace ((unsigned char)*cp)) { 1874 cp++; 1875 } 1876 1877 if (type == VAR_APPEND) { 1878 Var_Append(line, cp, ctxt); 1879 } else if (type == VAR_SUBST) { 1880 /* 1881 * Allow variables in the old value to be undefined, but leave their 1882 * invocation alone -- this is done by forcing oldVars to be false. 1883 * XXX: This can cause recursive variables, but that's not hard to do, 1884 * and this allows someone to do something like 1885 * 1886 * CFLAGS = $(.INCLUDES) 1887 * CFLAGS := -I.. $(CFLAGS) 1888 * 1889 * And not get an error. 1890 */ 1891 Boolean oldOldVars = oldVars; 1892 1893 oldVars = FALSE; 1894 1895 /* 1896 * make sure that we set the variable the first time to nothing 1897 * so that it gets substituted! 1898 */ 1899 if (!Var_Exists(line, ctxt)) 1900 Var_Set(line, "", ctxt, 0); 1901 1902 cp = Var_Subst(NULL, cp, ctxt, FALSE); 1903 oldVars = oldOldVars; 1904 freeCp = TRUE; 1905 1906 Var_Set(line, cp, ctxt, 0); 1907 } else if (type == VAR_SHELL) { 1908 char *res; 1909 const char *error; 1910 1911 if (strchr(cp, '$') != NULL) { 1912 /* 1913 * There's a dollar sign in the command, so perform variable 1914 * expansion on the whole thing. The resulting string will need 1915 * freeing when we're done, so set freeCmd to TRUE. 1916 */ 1917 cp = Var_Subst(NULL, cp, VAR_CMD, TRUE); 1918 freeCp = TRUE; 1919 } 1920 1921 res = Cmd_Exec(cp, &error); 1922 Var_Set(line, res, ctxt, 0); 1923 free(res); 1924 1925 if (error) 1926 Parse_Error(PARSE_WARNING, error, cp); 1927 } else { 1928 /* 1929 * Normal assignment -- just do it. 1930 */ 1931 Var_Set(line, cp, ctxt, 0); 1932 } 1933 if (strcmp(line, MAKEOVERRIDES) == 0) 1934 Main_ExportMAKEFLAGS(FALSE); /* re-export MAKEFLAGS */ 1935 else if (strcmp(line, ".CURDIR") == 0) { 1936 /* 1937 * Somone is being (too?) clever... 1938 * Let's pretend they know what they are doing and 1939 * re-initialize the 'cur' Path. 1940 */ 1941 Dir_InitCur(cp); 1942 Dir_SetPATH(); 1943 } else if (strcmp(line, MAKE_JOB_PREFIX) == 0) { 1944 Job_SetPrefix(); 1945 } else if (strcmp(line, MAKE_EXPORTED) == 0) { 1946 Var_Export(cp, 0); 1947 } 1948 if (freeCp) 1949 free(cp); 1950 } 1951 1952 1953 /*- 1954 * ParseAddCmd -- 1955 * Lst_ForEach function to add a command line to all targets 1956 * 1957 * Input: 1958 * gnp the node to which the command is to be added 1959 * cmd the command to add 1960 * 1961 * Results: 1962 * Always 0 1963 * 1964 * Side Effects: 1965 * A new element is added to the commands list of the node. 1966 */ 1967 static int 1968 ParseAddCmd(void *gnp, void *cmd) 1969 { 1970 GNode *gn = (GNode *)gnp; 1971 1972 /* Add to last (ie current) cohort for :: targets */ 1973 if ((gn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (gn->cohorts)) 1974 gn = (GNode *)Lst_Datum(Lst_Last(gn->cohorts)); 1975 1976 /* if target already supplied, ignore commands */ 1977 if (!(gn->type & OP_HAS_COMMANDS)) { 1978 (void)Lst_AtEnd(gn->commands, cmd); 1979 ParseMark(gn); 1980 } else { 1981 #ifdef notyet 1982 /* XXX: We cannot do this until we fix the tree */ 1983 (void)Lst_AtEnd(gn->commands, cmd); 1984 Parse_Error(PARSE_WARNING, 1985 "overriding commands for target \"%s\"; " 1986 "previous commands defined at %s: %d ignored", 1987 gn->name, gn->fname, gn->lineno); 1988 #else 1989 Parse_Error(PARSE_WARNING, 1990 "duplicate script for target \"%s\" ignored", 1991 gn->name); 1992 ParseErrorInternal(gn->fname, gn->lineno, PARSE_WARNING, 1993 "using previous script for \"%s\" defined here", 1994 gn->name); 1995 #endif 1996 } 1997 return(0); 1998 } 1999 2000 /*- 2001 *----------------------------------------------------------------------- 2002 * ParseHasCommands -- 2003 * Callback procedure for Parse_File when destroying the list of 2004 * targets on the last dependency line. Marks a target as already 2005 * having commands if it does, to keep from having shell commands 2006 * on multiple dependency lines. 2007 * 2008 * Input: 2009 * gnp Node to examine 2010 * 2011 * Results: 2012 * None 2013 * 2014 * Side Effects: 2015 * OP_HAS_COMMANDS may be set for the target. 2016 * 2017 *----------------------------------------------------------------------- 2018 */ 2019 static void 2020 ParseHasCommands(void *gnp) 2021 { 2022 GNode *gn = (GNode *)gnp; 2023 if (!Lst_IsEmpty(gn->commands)) { 2024 gn->type |= OP_HAS_COMMANDS; 2025 } 2026 } 2027 2028 /*- 2029 *----------------------------------------------------------------------- 2030 * Parse_AddIncludeDir -- 2031 * Add a directory to the path searched for included makefiles 2032 * bracketed by double-quotes. Used by functions in main.c 2033 * 2034 * Input: 2035 * dir The name of the directory to add 2036 * 2037 * Results: 2038 * None. 2039 * 2040 * Side Effects: 2041 * The directory is appended to the list. 2042 * 2043 *----------------------------------------------------------------------- 2044 */ 2045 void 2046 Parse_AddIncludeDir(char *dir) 2047 { 2048 (void)Dir_AddDir(parseIncPath, dir); 2049 } 2050 2051 /*- 2052 *--------------------------------------------------------------------- 2053 * ParseDoInclude -- 2054 * Push to another file. 2055 * 2056 * The input is the line minus the `.'. A file spec is a string 2057 * enclosed in <> or "". The former is looked for only in sysIncPath. 2058 * The latter in . and the directories specified by -I command line 2059 * options 2060 * 2061 * Results: 2062 * None 2063 * 2064 * Side Effects: 2065 * A structure is added to the includes Lst and readProc, lineno, 2066 * fname and curFILE are altered for the new file 2067 *--------------------------------------------------------------------- 2068 */ 2069 2070 static void 2071 Parse_include_file(char *file, Boolean isSystem, int silent) 2072 { 2073 struct loadedfile *lf; 2074 char *fullname; /* full pathname of file */ 2075 char *newName; 2076 char *prefEnd, *incdir; 2077 int fd; 2078 int i; 2079 2080 /* 2081 * Now we know the file's name and its search path, we attempt to 2082 * find the durn thing. A return of NULL indicates the file don't 2083 * exist. 2084 */ 2085 fullname = file[0] == '/' ? bmake_strdup(file) : NULL; 2086 2087 if (fullname == NULL && !isSystem) { 2088 /* 2089 * Include files contained in double-quotes are first searched for 2090 * relative to the including file's location. We don't want to 2091 * cd there, of course, so we just tack on the old file's 2092 * leading path components and call Dir_FindFile to see if 2093 * we can locate the beast. 2094 */ 2095 2096 incdir = bmake_strdup(curFile->fname); 2097 prefEnd = strrchr(incdir, '/'); 2098 if (prefEnd != NULL) { 2099 *prefEnd = '\0'; 2100 /* Now do lexical processing of leading "../" on the filename */ 2101 for (i = 0; strncmp(file + i, "../", 3) == 0; i += 3) { 2102 prefEnd = strrchr(incdir + 1, '/'); 2103 if (prefEnd == NULL || strcmp(prefEnd, "/..") == 0) 2104 break; 2105 *prefEnd = '\0'; 2106 } 2107 newName = str_concat(incdir, file + i, STR_ADDSLASH); 2108 fullname = Dir_FindFile(newName, parseIncPath); 2109 if (fullname == NULL) 2110 fullname = Dir_FindFile(newName, dirSearchPath); 2111 free(newName); 2112 } 2113 free(incdir); 2114 2115 if (fullname == NULL) { 2116 /* 2117 * Makefile wasn't found in same directory as included makefile. 2118 * Search for it first on the -I search path, 2119 * then on the .PATH search path, if not found in a -I directory. 2120 * If we have a suffix specific path we should use that. 2121 */ 2122 char *suff; 2123 Lst suffPath = NULL; 2124 2125 if ((suff = strrchr(file, '.'))) { 2126 suffPath = Suff_GetPath(suff); 2127 if (suffPath != NULL) { 2128 fullname = Dir_FindFile(file, suffPath); 2129 } 2130 } 2131 if (fullname == NULL) { 2132 fullname = Dir_FindFile(file, parseIncPath); 2133 if (fullname == NULL) { 2134 fullname = Dir_FindFile(file, dirSearchPath); 2135 } 2136 } 2137 } 2138 } 2139 2140 /* Looking for a system file or file still not found */ 2141 if (fullname == NULL) { 2142 /* 2143 * Look for it on the system path 2144 */ 2145 fullname = Dir_FindFile(file, 2146 Lst_IsEmpty(sysIncPath) ? defIncPath : sysIncPath); 2147 } 2148 2149 if (fullname == NULL) { 2150 if (!silent) 2151 Parse_Error(PARSE_FATAL, "Could not find %s", file); 2152 return; 2153 } 2154 2155 /* Actually open the file... */ 2156 fd = open(fullname, O_RDONLY); 2157 if (fd == -1) { 2158 if (!silent) 2159 Parse_Error(PARSE_FATAL, "Cannot open %s", fullname); 2160 free(fullname); 2161 return; 2162 } 2163 2164 /* load it */ 2165 lf = loadfile(fullname, fd); 2166 2167 /* Start reading from this file next */ 2168 Parse_SetInput(fullname, 0, -1, loadedfile_nextbuf, lf); 2169 curFile->lf = lf; 2170 } 2171 2172 static void 2173 ParseDoInclude(char *line) 2174 { 2175 char endc; /* the character which ends the file spec */ 2176 char *cp; /* current position in file spec */ 2177 int silent = (*line != 'i') ? 1 : 0; 2178 char *file = &line[7 + silent]; 2179 2180 /* Skip to delimiter character so we know where to look */ 2181 while (*file == ' ' || *file == '\t') 2182 file++; 2183 2184 if (*file != '"' && *file != '<') { 2185 Parse_Error(PARSE_FATAL, 2186 ".include filename must be delimited by '\"' or '<'"); 2187 return; 2188 } 2189 2190 /* 2191 * Set the search path on which to find the include file based on the 2192 * characters which bracket its name. Angle-brackets imply it's 2193 * a system Makefile while double-quotes imply it's a user makefile 2194 */ 2195 if (*file == '<') { 2196 endc = '>'; 2197 } else { 2198 endc = '"'; 2199 } 2200 2201 /* Skip to matching delimiter */ 2202 for (cp = ++file; *cp && *cp != endc; cp++) 2203 continue; 2204 2205 if (*cp != endc) { 2206 Parse_Error(PARSE_FATAL, 2207 "Unclosed %cinclude filename. '%c' expected", 2208 '.', endc); 2209 return; 2210 } 2211 *cp = '\0'; 2212 2213 /* 2214 * Substitute for any variables in the file name before trying to 2215 * find the thing. 2216 */ 2217 file = Var_Subst(NULL, file, VAR_CMD, FALSE); 2218 2219 Parse_include_file(file, endc == '>', silent); 2220 free(file); 2221 } 2222 2223 2224 /*- 2225 *--------------------------------------------------------------------- 2226 * ParseSetParseFile -- 2227 * Set the .PARSEDIR and .PARSEFILE variables to the dirname and 2228 * basename of the given filename 2229 * 2230 * Results: 2231 * None 2232 * 2233 * Side Effects: 2234 * The .PARSEDIR and .PARSEFILE variables are overwritten by the 2235 * dirname and basename of the given filename. 2236 *--------------------------------------------------------------------- 2237 */ 2238 static void 2239 ParseSetParseFile(const char *filename) 2240 { 2241 char *slash, *dirname; 2242 const char *pd, *pf; 2243 int len; 2244 2245 slash = strrchr(filename, '/'); 2246 if (slash == NULL) { 2247 Var_Set(".PARSEDIR", pd = curdir, VAR_GLOBAL, 0); 2248 Var_Set(".PARSEFILE", pf = filename, VAR_GLOBAL, 0); 2249 dirname= NULL; 2250 } else { 2251 len = slash - filename; 2252 dirname = bmake_malloc(len + 1); 2253 memcpy(dirname, filename, len); 2254 dirname[len] = '\0'; 2255 Var_Set(".PARSEDIR", pd = dirname, VAR_GLOBAL, 0); 2256 Var_Set(".PARSEFILE", pf = slash + 1, VAR_GLOBAL, 0); 2257 } 2258 if (DEBUG(PARSE)) 2259 fprintf(debug_file, "ParseSetParseFile: ${.PARSEDIR} = `%s' " 2260 "${.PARSEFILE} = `%s'\n", pd, pf); 2261 free(dirname); 2262 } 2263 2264 /* 2265 * Track the makefiles we read - so makefiles can 2266 * set dependencies on them. 2267 * Avoid adding anything more than once. 2268 */ 2269 2270 static void 2271 ParseTrackInput(const char *name) 2272 { 2273 char *old; 2274 char *fp = NULL; 2275 size_t name_len = strlen(name); 2276 2277 old = Var_Value(MAKE_MAKEFILES, VAR_GLOBAL, &fp); 2278 if (old) { 2279 /* does it contain name? */ 2280 for (; old != NULL; old = strchr(old, ' ')) { 2281 if (*old == ' ') 2282 old++; 2283 if (memcmp(old, name, name_len) == 0 2284 && (old[name_len] == 0 || old[name_len] == ' ')) 2285 goto cleanup; 2286 } 2287 } 2288 Var_Append (MAKE_MAKEFILES, name, VAR_GLOBAL); 2289 cleanup: 2290 if (fp) { 2291 free(fp); 2292 } 2293 } 2294 2295 2296 /*- 2297 *--------------------------------------------------------------------- 2298 * Parse_setInput -- 2299 * Start Parsing from the given source 2300 * 2301 * Results: 2302 * None 2303 * 2304 * Side Effects: 2305 * A structure is added to the includes Lst and readProc, lineno, 2306 * fname and curFile are altered for the new file 2307 *--------------------------------------------------------------------- 2308 */ 2309 void 2310 Parse_SetInput(const char *name, int line, int fd, 2311 char *(*nextbuf)(void *, size_t *), void *arg) 2312 { 2313 char *buf; 2314 size_t len; 2315 2316 if (name == NULL) 2317 name = curFile->fname; 2318 else 2319 ParseTrackInput(name); 2320 2321 if (DEBUG(PARSE)) 2322 fprintf(debug_file, "Parse_SetInput: file %s, line %d, fd %d, nextbuf %p, arg %p\n", 2323 name, line, fd, nextbuf, arg); 2324 2325 if (fd == -1 && nextbuf == NULL) 2326 /* sanity */ 2327 return; 2328 2329 if (curFile != NULL) 2330 /* Save exiting file info */ 2331 Lst_AtFront(includes, curFile); 2332 2333 /* Allocate and fill in new structure */ 2334 curFile = bmake_malloc(sizeof *curFile); 2335 2336 /* 2337 * Once the previous state has been saved, we can get down to reading 2338 * the new file. We set up the name of the file to be the absolute 2339 * name of the include file so error messages refer to the right 2340 * place. 2341 */ 2342 curFile->fname = bmake_strdup(name); 2343 curFile->lineno = line; 2344 curFile->first_lineno = line; 2345 curFile->nextbuf = nextbuf; 2346 curFile->nextbuf_arg = arg; 2347 curFile->lf = NULL; 2348 2349 assert(nextbuf != NULL); 2350 2351 /* Get first block of input data */ 2352 buf = curFile->nextbuf(curFile->nextbuf_arg, &len); 2353 if (buf == NULL) { 2354 /* Was all a waste of time ... */ 2355 if (curFile->fname) 2356 free(curFile->fname); 2357 free(curFile); 2358 return; 2359 } 2360 curFile->P_str = buf; 2361 curFile->P_ptr = buf; 2362 curFile->P_end = buf+len; 2363 2364 curFile->cond_depth = Cond_save_depth(); 2365 ParseSetParseFile(name); 2366 } 2367 2368 #ifdef SYSVINCLUDE 2369 /*- 2370 *--------------------------------------------------------------------- 2371 * ParseTraditionalInclude -- 2372 * Push to another file. 2373 * 2374 * The input is the current line. The file name(s) are 2375 * following the "include". 2376 * 2377 * Results: 2378 * None 2379 * 2380 * Side Effects: 2381 * A structure is added to the includes Lst and readProc, lineno, 2382 * fname and curFILE are altered for the new file 2383 *--------------------------------------------------------------------- 2384 */ 2385 static void 2386 ParseTraditionalInclude(char *line) 2387 { 2388 char *cp; /* current position in file spec */ 2389 int done = 0; 2390 int silent = (line[0] != 'i') ? 1 : 0; 2391 char *file = &line[silent + 7]; 2392 char *all_files; 2393 2394 if (DEBUG(PARSE)) { 2395 fprintf(debug_file, "ParseTraditionalInclude: %s\n", file); 2396 } 2397 2398 /* 2399 * Skip over whitespace 2400 */ 2401 while (isspace((unsigned char)*file)) 2402 file++; 2403 2404 /* 2405 * Substitute for any variables in the file name before trying to 2406 * find the thing. 2407 */ 2408 all_files = Var_Subst(NULL, file, VAR_CMD, FALSE); 2409 2410 if (*file == '\0') { 2411 Parse_Error(PARSE_FATAL, 2412 "Filename missing from \"include\""); 2413 return; 2414 } 2415 2416 for (file = all_files; !done; file = cp + 1) { 2417 /* Skip to end of line or next whitespace */ 2418 for (cp = file; *cp && !isspace((unsigned char) *cp); cp++) 2419 continue; 2420 2421 if (*cp) 2422 *cp = '\0'; 2423 else 2424 done = 1; 2425 2426 Parse_include_file(file, FALSE, silent); 2427 } 2428 free(all_files); 2429 } 2430 #endif 2431 2432 #ifdef GMAKEEXPORT 2433 /*- 2434 *--------------------------------------------------------------------- 2435 * ParseGmakeExport -- 2436 * Parse export <variable>=<value> 2437 * 2438 * And set the environment with it. 2439 * 2440 * Results: 2441 * None 2442 * 2443 * Side Effects: 2444 * None 2445 *--------------------------------------------------------------------- 2446 */ 2447 static void 2448 ParseGmakeExport(char *line) 2449 { 2450 char *variable = &line[6]; 2451 char *value; 2452 2453 if (DEBUG(PARSE)) { 2454 fprintf(debug_file, "ParseGmakeExport: %s\n", variable); 2455 } 2456 2457 /* 2458 * Skip over whitespace 2459 */ 2460 while (isspace((unsigned char)*variable)) 2461 variable++; 2462 2463 for (value = variable; *value && *value != '='; value++) 2464 continue; 2465 2466 if (*value != '=') { 2467 Parse_Error(PARSE_FATAL, 2468 "Variable/Value missing from \"export\""); 2469 return; 2470 } 2471 *value++ = '\0'; /* terminate variable */ 2472 2473 /* 2474 * Expand the value before putting it in the environment. 2475 */ 2476 value = Var_Subst(NULL, value, VAR_CMD, FALSE); 2477 setenv(variable, value, 1); 2478 } 2479 #endif 2480 2481 /*- 2482 *--------------------------------------------------------------------- 2483 * ParseEOF -- 2484 * Called when EOF is reached in the current file. If we were reading 2485 * an include file, the includes stack is popped and things set up 2486 * to go back to reading the previous file at the previous location. 2487 * 2488 * Results: 2489 * CONTINUE if there's more to do. DONE if not. 2490 * 2491 * Side Effects: 2492 * The old curFILE, is closed. The includes list is shortened. 2493 * lineno, curFILE, and fname are changed if CONTINUE is returned. 2494 *--------------------------------------------------------------------- 2495 */ 2496 static int 2497 ParseEOF(void) 2498 { 2499 char *ptr; 2500 size_t len; 2501 2502 assert(curFile->nextbuf != NULL); 2503 2504 /* get next input buffer, if any */ 2505 ptr = curFile->nextbuf(curFile->nextbuf_arg, &len); 2506 curFile->P_ptr = ptr; 2507 curFile->P_str = ptr; 2508 curFile->P_end = ptr + len; 2509 curFile->lineno = curFile->first_lineno; 2510 if (ptr != NULL) { 2511 /* Iterate again */ 2512 return CONTINUE; 2513 } 2514 2515 /* Ensure the makefile (or loop) didn't have mismatched conditionals */ 2516 Cond_restore_depth(curFile->cond_depth); 2517 2518 if (curFile->lf != NULL) { 2519 loadedfile_destroy(curFile->lf); 2520 curFile->lf = NULL; 2521 } 2522 2523 /* Dispose of curFile info */ 2524 /* Leak curFile->fname because all the gnodes have pointers to it */ 2525 free(curFile->P_str); 2526 free(curFile); 2527 2528 curFile = Lst_DeQueue(includes); 2529 2530 if (curFile == NULL) { 2531 /* We've run out of input */ 2532 Var_Delete(".PARSEDIR", VAR_GLOBAL); 2533 Var_Delete(".PARSEFILE", VAR_GLOBAL); 2534 return DONE; 2535 } 2536 2537 if (DEBUG(PARSE)) 2538 fprintf(debug_file, "ParseEOF: returning to file %s, line %d\n", 2539 curFile->fname, curFile->lineno); 2540 2541 /* Restore the PARSEDIR/PARSEFILE variables */ 2542 ParseSetParseFile(curFile->fname); 2543 return (CONTINUE); 2544 } 2545 2546 #define PARSE_RAW 1 2547 #define PARSE_SKIP 2 2548 2549 static char * 2550 ParseGetLine(int flags, int *length) 2551 { 2552 IFile *cf = curFile; 2553 char *ptr; 2554 char ch; 2555 char *line; 2556 char *line_end; 2557 char *escaped; 2558 char *comment; 2559 char *tp; 2560 2561 /* Loop through blank lines and comment lines */ 2562 for (;;) { 2563 cf->lineno++; 2564 line = cf->P_ptr; 2565 ptr = line; 2566 line_end = line; 2567 escaped = NULL; 2568 comment = NULL; 2569 for (;;) { 2570 if (cf->P_end != NULL && ptr == cf->P_end) { 2571 /* end of buffer */ 2572 ch = 0; 2573 break; 2574 } 2575 ch = *ptr; 2576 if (ch == 0 || (ch == '\\' && ptr[1] == 0)) { 2577 if (cf->P_end == NULL) 2578 /* End of string (aka for loop) data */ 2579 break; 2580 /* see if there is more we can parse */ 2581 while (ptr++ < cf->P_end) { 2582 if ((ch = *ptr) == '\n') { 2583 if (ptr > line && ptr[-1] == '\\') 2584 continue; 2585 Parse_Error(PARSE_WARNING, 2586 "Zero byte read from file, skipping rest of line."); 2587 break; 2588 } 2589 } 2590 if (cf->nextbuf != NULL) { 2591 /* 2592 * End of this buffer; return EOF and outer logic 2593 * will get the next one. (eww) 2594 */ 2595 break; 2596 } 2597 Parse_Error(PARSE_FATAL, "Zero byte read from file"); 2598 return NULL; 2599 } 2600 2601 if (ch == '\\') { 2602 /* Don't treat next character as special, remember first one */ 2603 if (escaped == NULL) 2604 escaped = ptr; 2605 if (ptr[1] == '\n') 2606 cf->lineno++; 2607 ptr += 2; 2608 line_end = ptr; 2609 continue; 2610 } 2611 if (ch == '#' && comment == NULL) { 2612 /* Remember first '#' for comment stripping */ 2613 /* Unless previous char was '[', as in modifier :[#] */ 2614 if (!(ptr > line && ptr[-1] == '[')) 2615 comment = line_end; 2616 } 2617 ptr++; 2618 if (ch == '\n') 2619 break; 2620 if (!isspace((unsigned char)ch)) 2621 /* We are not interested in trailing whitespace */ 2622 line_end = ptr; 2623 } 2624 2625 /* Save next 'to be processed' location */ 2626 cf->P_ptr = ptr; 2627 2628 /* Check we have a non-comment, non-blank line */ 2629 if (line_end == line || comment == line) { 2630 if (ch == 0) 2631 /* At end of file */ 2632 return NULL; 2633 /* Parse another line */ 2634 continue; 2635 } 2636 2637 /* We now have a line of data */ 2638 *line_end = 0; 2639 2640 if (flags & PARSE_RAW) { 2641 /* Leave '\' (etc) in line buffer (eg 'for' lines) */ 2642 *length = line_end - line; 2643 return line; 2644 } 2645 2646 if (flags & PARSE_SKIP) { 2647 /* Completely ignore non-directives */ 2648 if (line[0] != '.') 2649 continue; 2650 /* We could do more of the .else/.elif/.endif checks here */ 2651 } 2652 break; 2653 } 2654 2655 /* Brutally ignore anything after a non-escaped '#' in non-commands */ 2656 if (comment != NULL && line[0] != '\t') { 2657 line_end = comment; 2658 *line_end = 0; 2659 } 2660 2661 /* If we didn't see a '\\' then the in-situ data is fine */ 2662 if (escaped == NULL) { 2663 *length = line_end - line; 2664 return line; 2665 } 2666 2667 /* Remove escapes from '\n' and '#' */ 2668 tp = ptr = escaped; 2669 escaped = line; 2670 for (; ; *tp++ = ch) { 2671 ch = *ptr++; 2672 if (ch != '\\') { 2673 if (ch == 0) 2674 break; 2675 continue; 2676 } 2677 2678 ch = *ptr++; 2679 if (ch == 0) { 2680 /* Delete '\\' at end of buffer */ 2681 tp--; 2682 break; 2683 } 2684 2685 if (ch == '#' && line[0] != '\t') 2686 /* Delete '\\' from before '#' on non-command lines */ 2687 continue; 2688 2689 if (ch != '\n') { 2690 /* Leave '\\' in buffer for later */ 2691 *tp++ = '\\'; 2692 /* Make sure we don't delete an escaped ' ' from the line end */ 2693 escaped = tp + 1; 2694 continue; 2695 } 2696 2697 /* Escaped '\n' replace following whitespace with a single ' ' */ 2698 while (ptr[0] == ' ' || ptr[0] == '\t') 2699 ptr++; 2700 ch = ' '; 2701 } 2702 2703 /* Delete any trailing spaces - eg from empty continuations */ 2704 while (tp > escaped && isspace((unsigned char)tp[-1])) 2705 tp--; 2706 2707 *tp = 0; 2708 *length = tp - line; 2709 return line; 2710 } 2711 2712 /*- 2713 *--------------------------------------------------------------------- 2714 * ParseReadLine -- 2715 * Read an entire line from the input file. Called only by Parse_File. 2716 * 2717 * Results: 2718 * A line w/o its newline 2719 * 2720 * Side Effects: 2721 * Only those associated with reading a character 2722 *--------------------------------------------------------------------- 2723 */ 2724 static char * 2725 ParseReadLine(void) 2726 { 2727 char *line; /* Result */ 2728 int lineLength; /* Length of result */ 2729 int lineno; /* Saved line # */ 2730 int rval; 2731 2732 for (;;) { 2733 line = ParseGetLine(0, &lineLength); 2734 if (line == NULL) 2735 return NULL; 2736 2737 if (line[0] != '.') 2738 return line; 2739 2740 /* 2741 * The line might be a conditional. Ask the conditional module 2742 * about it and act accordingly 2743 */ 2744 switch (Cond_Eval(line)) { 2745 case COND_SKIP: 2746 /* Skip to next conditional that evaluates to COND_PARSE. */ 2747 do { 2748 line = ParseGetLine(PARSE_SKIP, &lineLength); 2749 } while (line && Cond_Eval(line) != COND_PARSE); 2750 if (line == NULL) 2751 break; 2752 continue; 2753 case COND_PARSE: 2754 continue; 2755 case COND_INVALID: /* Not a conditional line */ 2756 /* Check for .for loops */ 2757 rval = For_Eval(line); 2758 if (rval == 0) 2759 /* Not a .for line */ 2760 break; 2761 if (rval < 0) 2762 /* Syntax error - error printed, ignore line */ 2763 continue; 2764 /* Start of a .for loop */ 2765 lineno = curFile->lineno; 2766 /* Accumulate loop lines until matching .endfor */ 2767 do { 2768 line = ParseGetLine(PARSE_RAW, &lineLength); 2769 if (line == NULL) { 2770 Parse_Error(PARSE_FATAL, 2771 "Unexpected end of file in for loop."); 2772 break; 2773 } 2774 } while (For_Accum(line)); 2775 /* Stash each iteration as a new 'input file' */ 2776 For_Run(lineno); 2777 /* Read next line from for-loop buffer */ 2778 continue; 2779 } 2780 return (line); 2781 } 2782 } 2783 2784 /*- 2785 *----------------------------------------------------------------------- 2786 * ParseFinishLine -- 2787 * Handle the end of a dependency group. 2788 * 2789 * Results: 2790 * Nothing. 2791 * 2792 * Side Effects: 2793 * inLine set FALSE. 'targets' list destroyed. 2794 * 2795 *----------------------------------------------------------------------- 2796 */ 2797 static void 2798 ParseFinishLine(void) 2799 { 2800 if (inLine) { 2801 Lst_ForEach(targets, Suff_EndTransform, NULL); 2802 Lst_Destroy(targets, ParseHasCommands); 2803 targets = NULL; 2804 inLine = FALSE; 2805 } 2806 } 2807 2808 2809 /*- 2810 *--------------------------------------------------------------------- 2811 * Parse_File -- 2812 * Parse a file into its component parts, incorporating it into the 2813 * current dependency graph. This is the main function and controls 2814 * almost every other function in this module 2815 * 2816 * Input: 2817 * name the name of the file being read 2818 * fd Open file to makefile to parse 2819 * 2820 * Results: 2821 * None 2822 * 2823 * Side Effects: 2824 * closes fd. 2825 * Loads. Nodes are added to the list of all targets, nodes and links 2826 * are added to the dependency graph. etc. etc. etc. 2827 *--------------------------------------------------------------------- 2828 */ 2829 void 2830 Parse_File(const char *name, int fd) 2831 { 2832 char *cp; /* pointer into the line */ 2833 char *line; /* the line we're working on */ 2834 struct loadedfile *lf; 2835 2836 lf = loadfile(name, fd); 2837 2838 inLine = FALSE; 2839 fatals = 0; 2840 2841 if (name == NULL) { 2842 name = "(stdin)"; 2843 } 2844 2845 Parse_SetInput(name, 0, -1, loadedfile_nextbuf, lf); 2846 curFile->lf = lf; 2847 2848 do { 2849 for (; (line = ParseReadLine()) != NULL; ) { 2850 if (DEBUG(PARSE)) 2851 fprintf(debug_file, "ParseReadLine (%d): '%s'\n", 2852 curFile->lineno, line); 2853 if (*line == '.') { 2854 /* 2855 * Lines that begin with the special character may be 2856 * include or undef directives. 2857 * On the other hand they can be suffix rules (.c.o: ...) 2858 * or just dependencies for filenames that start '.'. 2859 */ 2860 for (cp = line + 1; isspace((unsigned char)*cp); cp++) { 2861 continue; 2862 } 2863 if (strncmp(cp, "include", 7) == 0 || 2864 ((cp[0] == 's' || cp[0] == '-') && 2865 strncmp(&cp[1], "include", 7) == 0)) { 2866 ParseDoInclude(cp); 2867 continue; 2868 } 2869 if (strncmp(cp, "undef", 5) == 0) { 2870 char *cp2; 2871 for (cp += 5; isspace((unsigned char) *cp); cp++) 2872 continue; 2873 for (cp2 = cp; !isspace((unsigned char) *cp2) && 2874 (*cp2 != '\0'); cp2++) 2875 continue; 2876 *cp2 = '\0'; 2877 Var_Delete(cp, VAR_GLOBAL); 2878 continue; 2879 } else if (strncmp(cp, "export", 6) == 0) { 2880 for (cp += 6; isspace((unsigned char) *cp); cp++) 2881 continue; 2882 Var_Export(cp, 1); 2883 continue; 2884 } else if (strncmp(cp, "unexport", 8) == 0) { 2885 Var_UnExport(cp); 2886 continue; 2887 } else if (strncmp(cp, "info", 4) == 0 || 2888 strncmp(cp, "error", 5) == 0 || 2889 strncmp(cp, "warning", 7) == 0) { 2890 if (ParseMessage(cp)) 2891 continue; 2892 } 2893 } 2894 2895 if (*line == '\t') { 2896 /* 2897 * If a line starts with a tab, it can only hope to be 2898 * a creation command. 2899 */ 2900 cp = line + 1; 2901 shellCommand: 2902 for (; isspace ((unsigned char)*cp); cp++) { 2903 continue; 2904 } 2905 if (*cp) { 2906 if (!inLine) 2907 Parse_Error(PARSE_FATAL, 2908 "Unassociated shell command \"%s\"", 2909 cp); 2910 /* 2911 * So long as it's not a blank line and we're actually 2912 * in a dependency spec, add the command to the list of 2913 * commands of all targets in the dependency spec 2914 */ 2915 if (targets) { 2916 cp = bmake_strdup(cp); 2917 Lst_ForEach(targets, ParseAddCmd, cp); 2918 #ifdef CLEANUP 2919 Lst_AtEnd(targCmds, cp); 2920 #endif 2921 } 2922 } 2923 continue; 2924 } 2925 2926 #ifdef SYSVINCLUDE 2927 if (((strncmp(line, "include", 7) == 0 && 2928 isspace((unsigned char) line[7])) || 2929 ((line[0] == 's' || line[0] == '-') && 2930 strncmp(&line[1], "include", 7) == 0 && 2931 isspace((unsigned char) line[8]))) && 2932 strchr(line, ':') == NULL) { 2933 /* 2934 * It's an S3/S5-style "include". 2935 */ 2936 ParseTraditionalInclude(line); 2937 continue; 2938 } 2939 #endif 2940 #ifdef GMAKEEXPORT 2941 if (strncmp(line, "export", 6) == 0 && 2942 isspace((unsigned char) line[6]) && 2943 strchr(line, ':') == NULL) { 2944 /* 2945 * It's a Gmake "export". 2946 */ 2947 ParseGmakeExport(line); 2948 continue; 2949 } 2950 #endif 2951 if (Parse_IsVar(line)) { 2952 ParseFinishLine(); 2953 Parse_DoVar(line, VAR_GLOBAL); 2954 continue; 2955 } 2956 2957 #ifndef POSIX 2958 /* 2959 * To make life easier on novices, if the line is indented we 2960 * first make sure the line has a dependency operator in it. 2961 * If it doesn't have an operator and we're in a dependency 2962 * line's script, we assume it's actually a shell command 2963 * and add it to the current list of targets. 2964 */ 2965 cp = line; 2966 if (isspace((unsigned char) line[0])) { 2967 while ((*cp != '\0') && isspace((unsigned char) *cp)) 2968 cp++; 2969 while (*cp && (ParseIsEscaped(line, cp) || 2970 (*cp != ':') && (*cp != '!'))) { 2971 cp++; 2972 } 2973 if (*cp == '\0') { 2974 if (inLine) { 2975 Parse_Error(PARSE_WARNING, 2976 "Shell command needs a leading tab"); 2977 goto shellCommand; 2978 } 2979 } 2980 } 2981 #endif 2982 ParseFinishLine(); 2983 2984 /* 2985 * For some reason - probably to make the parser impossible - 2986 * a ';' can be used to separate commands from dependencies. 2987 * Attempt to avoid ';' inside substitution patterns. 2988 */ 2989 { 2990 int level = 0; 2991 2992 for (cp = line; *cp != 0; cp++) { 2993 if (*cp == '\\' && cp[1] != 0) { 2994 cp++; 2995 continue; 2996 } 2997 if (*cp == '$' && 2998 (cp[1] == '(' || cp[1] == '{')) { 2999 level++; 3000 continue; 3001 } 3002 if (level > 0) { 3003 if (*cp == ')' || *cp == '}') { 3004 level--; 3005 continue; 3006 } 3007 } else if (*cp == ';') { 3008 break; 3009 } 3010 } 3011 } 3012 if (*cp != 0) 3013 /* Terminate the dependency list at the ';' */ 3014 *cp++ = 0; 3015 else 3016 cp = NULL; 3017 3018 /* 3019 * We now know it's a dependency line so it needs to have all 3020 * variables expanded before being parsed. Tell the variable 3021 * module to complain if some variable is undefined... 3022 */ 3023 line = Var_Subst(NULL, line, VAR_CMD, TRUE); 3024 3025 /* 3026 * Need a non-circular list for the target nodes 3027 */ 3028 if (targets) 3029 Lst_Destroy(targets, NULL); 3030 3031 targets = Lst_Init(FALSE); 3032 inLine = TRUE; 3033 3034 ParseDoDependency(line); 3035 free(line); 3036 3037 /* If there were commands after a ';', add them now */ 3038 if (cp != NULL) { 3039 goto shellCommand; 3040 } 3041 } 3042 /* 3043 * Reached EOF, but it may be just EOF of an include file... 3044 */ 3045 } while (ParseEOF() == CONTINUE); 3046 3047 if (fatals) { 3048 (void)fflush(stdout); 3049 (void)fprintf(stderr, 3050 "%s: Fatal errors encountered -- cannot continue", 3051 progname); 3052 PrintOnError(NULL, NULL); 3053 exit(1); 3054 } 3055 } 3056 3057 /*- 3058 *--------------------------------------------------------------------- 3059 * Parse_Init -- 3060 * initialize the parsing module 3061 * 3062 * Results: 3063 * none 3064 * 3065 * Side Effects: 3066 * the parseIncPath list is initialized... 3067 *--------------------------------------------------------------------- 3068 */ 3069 void 3070 Parse_Init(void) 3071 { 3072 mainNode = NULL; 3073 parseIncPath = Lst_Init(FALSE); 3074 sysIncPath = Lst_Init(FALSE); 3075 defIncPath = Lst_Init(FALSE); 3076 includes = Lst_Init(FALSE); 3077 #ifdef CLEANUP 3078 targCmds = Lst_Init(FALSE); 3079 #endif 3080 } 3081 3082 void 3083 Parse_End(void) 3084 { 3085 #ifdef CLEANUP 3086 Lst_Destroy(targCmds, (FreeProc *)free); 3087 if (targets) 3088 Lst_Destroy(targets, NULL); 3089 Lst_Destroy(defIncPath, Dir_Destroy); 3090 Lst_Destroy(sysIncPath, Dir_Destroy); 3091 Lst_Destroy(parseIncPath, Dir_Destroy); 3092 Lst_Destroy(includes, NULL); /* Should be empty now */ 3093 #endif 3094 } 3095 3096 3097 /*- 3098 *----------------------------------------------------------------------- 3099 * Parse_MainName -- 3100 * Return a Lst of the main target to create for main()'s sake. If 3101 * no such target exists, we Punt with an obnoxious error message. 3102 * 3103 * Results: 3104 * A Lst of the single node to create. 3105 * 3106 * Side Effects: 3107 * None. 3108 * 3109 *----------------------------------------------------------------------- 3110 */ 3111 Lst 3112 Parse_MainName(void) 3113 { 3114 Lst mainList; /* result list */ 3115 3116 mainList = Lst_Init(FALSE); 3117 3118 if (mainNode == NULL) { 3119 Punt("no target to make."); 3120 /*NOTREACHED*/ 3121 } else if (mainNode->type & OP_DOUBLEDEP) { 3122 (void)Lst_AtEnd(mainList, mainNode); 3123 Lst_Concat(mainList, mainNode->cohorts, LST_CONCNEW); 3124 } 3125 else 3126 (void)Lst_AtEnd(mainList, mainNode); 3127 Var_Append(".TARGETS", mainNode->name, VAR_GLOBAL); 3128 return (mainList); 3129 } 3130 3131 /*- 3132 *----------------------------------------------------------------------- 3133 * ParseMark -- 3134 * Add the filename and lineno to the GNode so that we remember 3135 * where it was first defined. 3136 * 3137 * Side Effects: 3138 * None. 3139 * 3140 *----------------------------------------------------------------------- 3141 */ 3142 static void 3143 ParseMark(GNode *gn) 3144 { 3145 gn->fname = curFile->fname; 3146 gn->lineno = curFile->lineno; 3147 } 3148