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