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