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