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