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