1 /* $NetBSD: parse.c,v 1.154 2009/01/08 21:12:09 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.154 2009/01/08 21:12:09 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.154 2009/01/08 21:12:09 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 char ch; 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 /* Scan for one of the assignment operators outside a variable expansion */ 1397 while ((ch = *line++) != 0) { 1398 if (ch == '(' || ch == '{') { 1399 level++; 1400 continue; 1401 } 1402 if (ch == ')' || ch == '}') { 1403 level--; 1404 continue; 1405 } 1406 if (level != 0) 1407 continue; 1408 while (ch == ' ' || ch == '\t') { 1409 ch = *line++; 1410 wasSpace = TRUE; 1411 } 1412 if (ch == '=') 1413 return TRUE; 1414 if (*line == '=' && ISEQOPERATOR(ch)) 1415 return TRUE; 1416 if (wasSpace) 1417 return FALSE; 1418 } 1419 1420 return FALSE; 1421 } 1422 1423 /*- 1424 *--------------------------------------------------------------------- 1425 * Parse_DoVar -- 1426 * Take the variable assignment in the passed line and do it in the 1427 * global context. 1428 * 1429 * Note: There is a lexical ambiguity with assignment modifier characters 1430 * in variable names. This routine interprets the character before the = 1431 * as a modifier. Therefore, an assignment like 1432 * C++=/usr/bin/CC 1433 * is interpreted as "C+ +=" instead of "C++ =". 1434 * 1435 * Input: 1436 * line a line guaranteed to be a variable assignment. 1437 * This reduces error checks 1438 * ctxt Context in which to do the assignment 1439 * 1440 * Results: 1441 * none 1442 * 1443 * Side Effects: 1444 * the variable structure of the given variable name is altered in the 1445 * global context. 1446 *--------------------------------------------------------------------- 1447 */ 1448 void 1449 Parse_DoVar(char *line, GNode *ctxt) 1450 { 1451 char *cp; /* pointer into line */ 1452 enum { 1453 VAR_SUBST, VAR_APPEND, VAR_SHELL, VAR_NORMAL 1454 } type; /* Type of assignment */ 1455 char *opc; /* ptr to operator character to 1456 * null-terminate the variable name */ 1457 Boolean freeCp = FALSE; /* TRUE if cp needs to be freed, 1458 * i.e. if any variable expansion was 1459 * performed */ 1460 int depth; 1461 1462 /* 1463 * Skip to variable name 1464 */ 1465 while ((*line == ' ') || (*line == '\t')) { 1466 line++; 1467 } 1468 1469 /* 1470 * Skip to operator character, nulling out whitespace as we go 1471 * XXX Rather than counting () and {} we should look for $ and 1472 * then expand the variable. 1473 */ 1474 for (depth = 0, cp = line + 1; depth != 0 || *cp != '='; cp++) { 1475 if (*cp == '(' || *cp == '{') { 1476 depth++; 1477 continue; 1478 } 1479 if (*cp == ')' || *cp == '}') { 1480 depth--; 1481 continue; 1482 } 1483 if (depth == 0 && isspace ((unsigned char)*cp)) { 1484 *cp = '\0'; 1485 } 1486 } 1487 opc = cp-1; /* operator is the previous character */ 1488 *cp++ = '\0'; /* nuke the = */ 1489 1490 /* 1491 * Check operator type 1492 */ 1493 switch (*opc) { 1494 case '+': 1495 type = VAR_APPEND; 1496 *opc = '\0'; 1497 break; 1498 1499 case '?': 1500 /* 1501 * If the variable already has a value, we don't do anything. 1502 */ 1503 *opc = '\0'; 1504 if (Var_Exists(line, ctxt)) { 1505 return; 1506 } else { 1507 type = VAR_NORMAL; 1508 } 1509 break; 1510 1511 case ':': 1512 type = VAR_SUBST; 1513 *opc = '\0'; 1514 break; 1515 1516 case '!': 1517 type = VAR_SHELL; 1518 *opc = '\0'; 1519 break; 1520 1521 default: 1522 #ifdef SUNSHCMD 1523 while (opc > line && *opc != ':') 1524 opc--; 1525 1526 if (strncmp(opc, ":sh", 3) == 0) { 1527 type = VAR_SHELL; 1528 *opc = '\0'; 1529 break; 1530 } 1531 #endif 1532 type = VAR_NORMAL; 1533 break; 1534 } 1535 1536 while (isspace ((unsigned char)*cp)) { 1537 cp++; 1538 } 1539 1540 if (type == VAR_APPEND) { 1541 Var_Append(line, cp, ctxt); 1542 } else if (type == VAR_SUBST) { 1543 /* 1544 * Allow variables in the old value to be undefined, but leave their 1545 * invocation alone -- this is done by forcing oldVars to be false. 1546 * XXX: This can cause recursive variables, but that's not hard to do, 1547 * and this allows someone to do something like 1548 * 1549 * CFLAGS = $(.INCLUDES) 1550 * CFLAGS := -I.. $(CFLAGS) 1551 * 1552 * And not get an error. 1553 */ 1554 Boolean oldOldVars = oldVars; 1555 1556 oldVars = FALSE; 1557 1558 /* 1559 * make sure that we set the variable the first time to nothing 1560 * so that it gets substituted! 1561 */ 1562 if (!Var_Exists(line, ctxt)) 1563 Var_Set(line, "", ctxt, 0); 1564 1565 cp = Var_Subst(NULL, cp, ctxt, FALSE); 1566 oldVars = oldOldVars; 1567 freeCp = TRUE; 1568 1569 Var_Set(line, cp, ctxt, 0); 1570 } else if (type == VAR_SHELL) { 1571 char *res; 1572 const char *error; 1573 1574 if (strchr(cp, '$') != NULL) { 1575 /* 1576 * There's a dollar sign in the command, so perform variable 1577 * expansion on the whole thing. The resulting string will need 1578 * freeing when we're done, so set freeCmd to TRUE. 1579 */ 1580 cp = Var_Subst(NULL, cp, VAR_CMD, TRUE); 1581 freeCp = TRUE; 1582 } 1583 1584 res = Cmd_Exec(cp, &error); 1585 Var_Set(line, res, ctxt, 0); 1586 free(res); 1587 1588 if (error) 1589 Parse_Error(PARSE_WARNING, error, cp); 1590 } else { 1591 /* 1592 * Normal assignment -- just do it. 1593 */ 1594 Var_Set(line, cp, ctxt, 0); 1595 } 1596 if (strcmp(line, MAKEOVERRIDES) == 0) 1597 Main_ExportMAKEFLAGS(FALSE); /* re-export MAKEFLAGS */ 1598 else if (strcmp(line, ".CURDIR") == 0) { 1599 /* 1600 * Somone is being (too?) clever... 1601 * Let's pretend they know what they are doing and 1602 * re-initialize the 'cur' Path. 1603 */ 1604 Dir_InitCur(cp); 1605 Dir_SetPATH(); 1606 } else if (strcmp(line, MAKE_JOB_PREFIX) == 0) { 1607 Job_SetPrefix(); 1608 } else if (strcmp(line, MAKE_EXPORTED) == 0) { 1609 Var_Export(cp, 0); 1610 } 1611 if (freeCp) 1612 free(cp); 1613 } 1614 1615 1616 /*- 1617 * ParseAddCmd -- 1618 * Lst_ForEach function to add a command line to all targets 1619 * 1620 * Input: 1621 * gnp the node to which the command is to be added 1622 * cmd the command to add 1623 * 1624 * Results: 1625 * Always 0 1626 * 1627 * Side Effects: 1628 * A new element is added to the commands list of the node. 1629 */ 1630 static int 1631 ParseAddCmd(ClientData gnp, ClientData cmd) 1632 { 1633 GNode *gn = (GNode *)gnp; 1634 1635 /* Add to last (ie current) cohort for :: targets */ 1636 if ((gn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (gn->cohorts)) 1637 gn = (GNode *)Lst_Datum(Lst_Last(gn->cohorts)); 1638 1639 /* if target already supplied, ignore commands */ 1640 if (!(gn->type & OP_HAS_COMMANDS)) { 1641 (void)Lst_AtEnd(gn->commands, cmd); 1642 ParseMark(gn); 1643 } else { 1644 #ifdef notyet 1645 /* XXX: We cannot do this until we fix the tree */ 1646 (void)Lst_AtEnd(gn->commands, cmd); 1647 Parse_Error(PARSE_WARNING, 1648 "overriding commands for target \"%s\"; " 1649 "previous commands defined at %s: %d ignored", 1650 gn->name, gn->fname, gn->lineno); 1651 #else 1652 Parse_Error(PARSE_WARNING, 1653 "duplicate script for target \"%s\" ignored", 1654 gn->name); 1655 ParseErrorInternal(gn->fname, gn->lineno, PARSE_WARNING, 1656 "using previous script for \"%s\" defined here", 1657 gn->name); 1658 #endif 1659 } 1660 return(0); 1661 } 1662 1663 /*- 1664 *----------------------------------------------------------------------- 1665 * ParseHasCommands -- 1666 * Callback procedure for Parse_File when destroying the list of 1667 * targets on the last dependency line. Marks a target as already 1668 * having commands if it does, to keep from having shell commands 1669 * on multiple dependency lines. 1670 * 1671 * Input: 1672 * gnp Node to examine 1673 * 1674 * Results: 1675 * None 1676 * 1677 * Side Effects: 1678 * OP_HAS_COMMANDS may be set for the target. 1679 * 1680 *----------------------------------------------------------------------- 1681 */ 1682 static void 1683 ParseHasCommands(ClientData gnp) 1684 { 1685 GNode *gn = (GNode *)gnp; 1686 if (!Lst_IsEmpty(gn->commands)) { 1687 gn->type |= OP_HAS_COMMANDS; 1688 } 1689 } 1690 1691 /*- 1692 *----------------------------------------------------------------------- 1693 * Parse_AddIncludeDir -- 1694 * Add a directory to the path searched for included makefiles 1695 * bracketed by double-quotes. Used by functions in main.c 1696 * 1697 * Input: 1698 * dir The name of the directory to add 1699 * 1700 * Results: 1701 * None. 1702 * 1703 * Side Effects: 1704 * The directory is appended to the list. 1705 * 1706 *----------------------------------------------------------------------- 1707 */ 1708 void 1709 Parse_AddIncludeDir(char *dir) 1710 { 1711 (void)Dir_AddDir(parseIncPath, dir); 1712 } 1713 1714 /*- 1715 *--------------------------------------------------------------------- 1716 * ParseDoInclude -- 1717 * Push to another file. 1718 * 1719 * The input is the line minus the `.'. A file spec is a string 1720 * enclosed in <> or "". The former is looked for only in sysIncPath. 1721 * The latter in . and the directories specified by -I command line 1722 * options 1723 * 1724 * Results: 1725 * None 1726 * 1727 * Side Effects: 1728 * A structure is added to the includes Lst and readProc, lineno, 1729 * fname and curFILE are altered for the new file 1730 *--------------------------------------------------------------------- 1731 */ 1732 1733 static void 1734 Parse_include_file(char *file, Boolean isSystem, int silent) 1735 { 1736 char *fullname; /* full pathname of file */ 1737 char *newName; 1738 char *prefEnd, *incdir; 1739 int fd; 1740 int i; 1741 1742 /* 1743 * Now we know the file's name and its search path, we attempt to 1744 * find the durn thing. A return of NULL indicates the file don't 1745 * exist. 1746 */ 1747 fullname = file[0] == '/' ? bmake_strdup(file) : NULL; 1748 1749 if (fullname == NULL && !isSystem) { 1750 /* 1751 * Include files contained in double-quotes are first searched for 1752 * relative to the including file's location. We don't want to 1753 * cd there, of course, so we just tack on the old file's 1754 * leading path components and call Dir_FindFile to see if 1755 * we can locate the beast. 1756 */ 1757 1758 incdir = bmake_strdup(curFile->fname); 1759 prefEnd = strrchr(incdir, '/'); 1760 if (prefEnd != NULL) { 1761 *prefEnd = '\0'; 1762 /* Now do lexical processing of leading "../" on the filename */ 1763 for (i = 0; strncmp(file + i, "../", 3) == 0; i += 3) { 1764 prefEnd = strrchr(incdir + 1, '/'); 1765 if (prefEnd == NULL || strcmp(prefEnd, "/..") == 0) 1766 break; 1767 *prefEnd = '\0'; 1768 } 1769 newName = str_concat(incdir, file + i, STR_ADDSLASH); 1770 fullname = Dir_FindFile(newName, parseIncPath); 1771 if (fullname == NULL) 1772 fullname = Dir_FindFile(newName, dirSearchPath); 1773 free(newName); 1774 } 1775 free(incdir); 1776 1777 if (fullname == NULL) { 1778 /* 1779 * Makefile wasn't found in same directory as included makefile. 1780 * Search for it first on the -I search path, 1781 * then on the .PATH search path, if not found in a -I directory. 1782 * If we have a suffix specific path we should use that. 1783 */ 1784 char *suff; 1785 Lst suffPath = NULL; 1786 1787 if ((suff = strrchr(file, '.'))) { 1788 suffPath = Suff_GetPath(suff); 1789 if (suffPath != NULL) { 1790 fullname = Dir_FindFile(file, suffPath); 1791 } 1792 } 1793 if (fullname == NULL) { 1794 fullname = Dir_FindFile(file, parseIncPath); 1795 if (fullname == NULL) { 1796 fullname = Dir_FindFile(file, dirSearchPath); 1797 } 1798 } 1799 } 1800 } 1801 1802 /* Looking for a system file or file still not found */ 1803 if (fullname == NULL) { 1804 /* 1805 * Look for it on the system path 1806 */ 1807 fullname = Dir_FindFile(file, 1808 Lst_IsEmpty(sysIncPath) ? defIncPath : sysIncPath); 1809 } 1810 1811 if (fullname == NULL) { 1812 if (!silent) 1813 Parse_Error(PARSE_FATAL, "Could not find %s", file); 1814 return; 1815 } 1816 1817 /* Actually open the file... */ 1818 fd = open(fullname, O_RDONLY); 1819 if (fd == -1) { 1820 if (!silent) 1821 Parse_Error(PARSE_FATAL, "Cannot open %s", fullname); 1822 free(fullname); 1823 return; 1824 } 1825 1826 /* Start reading from this file next */ 1827 Parse_SetInput(fullname, 0, fd, NULL); 1828 } 1829 1830 static void 1831 ParseDoInclude(char *line) 1832 { 1833 char endc; /* the character which ends the file spec */ 1834 char *cp; /* current position in file spec */ 1835 int silent = (*line != 'i') ? 1 : 0; 1836 char *file = &line[7 + silent]; 1837 1838 /* Skip to delimiter character so we know where to look */ 1839 while (*file == ' ' || *file == '\t') 1840 file++; 1841 1842 if (*file != '"' && *file != '<') { 1843 Parse_Error(PARSE_FATAL, 1844 ".include filename must be delimited by '\"' or '<'"); 1845 return; 1846 } 1847 1848 /* 1849 * Set the search path on which to find the include file based on the 1850 * characters which bracket its name. Angle-brackets imply it's 1851 * a system Makefile while double-quotes imply it's a user makefile 1852 */ 1853 if (*file == '<') { 1854 endc = '>'; 1855 } else { 1856 endc = '"'; 1857 } 1858 1859 /* Skip to matching delimiter */ 1860 for (cp = ++file; *cp && *cp != endc; cp++) 1861 continue; 1862 1863 if (*cp != endc) { 1864 Parse_Error(PARSE_FATAL, 1865 "Unclosed %cinclude filename. '%c' expected", 1866 '.', endc); 1867 return; 1868 } 1869 *cp = '\0'; 1870 1871 /* 1872 * Substitute for any variables in the file name before trying to 1873 * find the thing. 1874 */ 1875 file = Var_Subst(NULL, file, VAR_CMD, FALSE); 1876 1877 Parse_include_file(file, endc == '>', silent); 1878 free(file); 1879 } 1880 1881 1882 /*- 1883 *--------------------------------------------------------------------- 1884 * ParseSetParseFile -- 1885 * Set the .PARSEDIR and .PARSEFILE variables to the dirname and 1886 * basename of the given filename 1887 * 1888 * Results: 1889 * None 1890 * 1891 * Side Effects: 1892 * The .PARSEDIR and .PARSEFILE variables are overwritten by the 1893 * dirname and basename of the given filename. 1894 *--------------------------------------------------------------------- 1895 */ 1896 static void 1897 ParseSetParseFile(const char *filename) 1898 { 1899 char *slash; 1900 char *dirname; 1901 int len; 1902 1903 slash = strrchr(filename, '/'); 1904 if (slash == NULL) { 1905 Var_Set(".PARSEDIR", ".", VAR_GLOBAL, 0); 1906 Var_Set(".PARSEFILE", filename, VAR_GLOBAL, 0); 1907 } else { 1908 len = slash - filename; 1909 dirname = bmake_malloc(len + 1); 1910 memcpy(dirname, filename, len); 1911 dirname[len] = 0; 1912 Var_Set(".PARSEDIR", dirname, VAR_GLOBAL, 0); 1913 Var_Set(".PARSEFILE", slash+1, VAR_GLOBAL, 0); 1914 free(dirname); 1915 } 1916 } 1917 1918 /* 1919 * Track the makefiles we read - so makefiles can 1920 * set dependencies on them. 1921 * Avoid adding anything more than once. 1922 */ 1923 1924 static void 1925 ParseTrackInput(const char *name) 1926 { 1927 char *old; 1928 char *fp = NULL; 1929 size_t name_len = strlen(name); 1930 1931 old = Var_Value(MAKE_MAKEFILES, VAR_GLOBAL, &fp); 1932 if (old) { 1933 /* does it contain name? */ 1934 for (; old != NULL; old = strchr(old, ' ')) { 1935 if (*old == ' ') 1936 old++; 1937 if (memcmp(old, name, name_len) == 0 1938 && (old[name_len] == 0 || old[name_len] == ' ')) 1939 goto cleanup; 1940 } 1941 } 1942 Var_Append (MAKE_MAKEFILES, name, VAR_GLOBAL); 1943 cleanup: 1944 if (fp) { 1945 free(fp); 1946 } 1947 } 1948 1949 1950 /*- 1951 *--------------------------------------------------------------------- 1952 * Parse_setInput -- 1953 * Start Parsing from the given source 1954 * 1955 * Results: 1956 * None 1957 * 1958 * Side Effects: 1959 * A structure is added to the includes Lst and readProc, lineno, 1960 * fname and curFile are altered for the new file 1961 *--------------------------------------------------------------------- 1962 */ 1963 void 1964 Parse_SetInput(const char *name, int line, int fd, char *buf) 1965 { 1966 if (name == NULL) 1967 name = curFile->fname; 1968 else 1969 ParseTrackInput(name); 1970 1971 if (DEBUG(PARSE)) 1972 fprintf(debug_file, "Parse_SetInput: file %s, line %d, fd %d, buf %p\n", 1973 name, line, fd, buf); 1974 1975 if (fd == -1 && buf == NULL) 1976 /* sanity */ 1977 return; 1978 1979 if (curFile != NULL) 1980 /* Save exiting file info */ 1981 Lst_AtFront(includes, curFile); 1982 1983 /* Allocate and fill in new structure */ 1984 curFile = bmake_malloc(sizeof *curFile); 1985 1986 /* 1987 * Once the previous state has been saved, we can get down to reading 1988 * the new file. We set up the name of the file to be the absolute 1989 * name of the include file so error messages refer to the right 1990 * place. 1991 */ 1992 curFile->fname = name; 1993 curFile->lineno = line; 1994 curFile->fd = fd; 1995 curFile->cond_depth = Cond_save_depth(); 1996 1997 ParseSetParseFile(name); 1998 1999 if (buf == NULL) { 2000 /* 2001 * Allocate a 32k data buffer (as stdio seems to). 2002 * Set pointers so that first ParseReadc has to do a file read. 2003 */ 2004 buf = bmake_malloc(IFILE_BUFLEN); 2005 buf[0] = 0; 2006 curFile->P_str = buf; 2007 curFile->P_ptr = buf; 2008 curFile->P_end = buf; 2009 curFile->P_buflen = IFILE_BUFLEN; 2010 } else { 2011 /* Start reading from the start of the buffer */ 2012 curFile->P_str = buf; 2013 curFile->P_ptr = buf; 2014 curFile->P_end = NULL; 2015 } 2016 2017 } 2018 2019 #ifdef SYSVINCLUDE 2020 /*- 2021 *--------------------------------------------------------------------- 2022 * ParseTraditionalInclude -- 2023 * Push to another file. 2024 * 2025 * The input is the current line. The file name(s) are 2026 * following the "include". 2027 * 2028 * Results: 2029 * None 2030 * 2031 * Side Effects: 2032 * A structure is added to the includes Lst and readProc, lineno, 2033 * fname and curFILE are altered for the new file 2034 *--------------------------------------------------------------------- 2035 */ 2036 static void 2037 ParseTraditionalInclude(char *line) 2038 { 2039 char *cp; /* current position in file spec */ 2040 int done = 0; 2041 int silent = (line[0] != 'i') ? 1 : 0; 2042 char *file = &line[silent + 7]; 2043 char *all_files; 2044 2045 if (DEBUG(PARSE)) { 2046 fprintf(debug_file, "ParseTraditionalInclude: %s\n", file); 2047 } 2048 2049 /* 2050 * Skip over whitespace 2051 */ 2052 while (isspace((unsigned char)*file)) 2053 file++; 2054 2055 /* 2056 * Substitute for any variables in the file name before trying to 2057 * find the thing. 2058 */ 2059 all_files = Var_Subst(NULL, file, VAR_CMD, FALSE); 2060 2061 if (*file == '\0') { 2062 Parse_Error(PARSE_FATAL, 2063 "Filename missing from \"include\""); 2064 return; 2065 } 2066 2067 for (file = all_files; !done; file = cp + 1) { 2068 /* Skip to end of line or next whitespace */ 2069 for (cp = file; *cp && !isspace((unsigned char) *cp); cp++) 2070 continue; 2071 2072 if (*cp) 2073 *cp = '\0'; 2074 else 2075 done = 1; 2076 2077 Parse_include_file(file, FALSE, silent); 2078 } 2079 free(all_files); 2080 } 2081 #endif 2082 2083 /*- 2084 *--------------------------------------------------------------------- 2085 * ParseEOF -- 2086 * Called when EOF is reached in the current file. If we were reading 2087 * an include file, the includes stack is popped and things set up 2088 * to go back to reading the previous file at the previous location. 2089 * 2090 * Results: 2091 * CONTINUE if there's more to do. DONE if not. 2092 * 2093 * Side Effects: 2094 * The old curFILE, is closed. The includes list is shortened. 2095 * lineno, curFILE, and fname are changed if CONTINUE is returned. 2096 *--------------------------------------------------------------------- 2097 */ 2098 static int 2099 ParseEOF(void) 2100 { 2101 /* Ensure the makefile (or loop) didn't have mismatched conditionals */ 2102 Cond_restore_depth(curFile->cond_depth); 2103 2104 /* Dispose of curFile info */ 2105 /* Leak curFile->fname because all the gnodes have pointers to it */ 2106 if (curFile->fd != -1) 2107 close(curFile->fd); 2108 free(curFile->P_str); 2109 free(curFile); 2110 2111 curFile = Lst_DeQueue(includes); 2112 2113 if (curFile == NULL) { 2114 /* We've run out of input */ 2115 Var_Delete(".PARSEDIR", VAR_GLOBAL); 2116 Var_Delete(".PARSEFILE", VAR_GLOBAL); 2117 return DONE; 2118 } 2119 2120 if (DEBUG(PARSE)) 2121 fprintf(debug_file, "ParseEOF: returning to file %s, line %d, fd %d\n", 2122 curFile->fname, curFile->lineno, curFile->fd); 2123 2124 /* Restore the PARSEDIR/PARSEFILE variables */ 2125 ParseSetParseFile(curFile->fname); 2126 return (CONTINUE); 2127 } 2128 2129 #define PARSE_RAW 1 2130 #define PARSE_SKIP 2 2131 2132 static char * 2133 ParseGetLine(int flags, int *length) 2134 { 2135 IFile *cf = curFile; 2136 char *ptr; 2137 char ch; 2138 char *line; 2139 char *line_end; 2140 char *escaped; 2141 char *comment; 2142 char *tp; 2143 int len, dist; 2144 2145 /* Loop through blank lines and comment lines */ 2146 for (;;) { 2147 cf->lineno++; 2148 line = cf->P_ptr; 2149 ptr = line; 2150 line_end = line; 2151 escaped = NULL; 2152 comment = NULL; 2153 for (;;) { 2154 ch = *ptr; 2155 if (ch == 0 || (ch == '\\' && ptr[1] == 0)) { 2156 if (cf->P_end == NULL) 2157 /* End of string (aka for loop) data */ 2158 break; 2159 /* End of data read from file, read more data */ 2160 if (ptr != cf->P_end && (ch != '\\' || ptr + 1 != cf->P_end)) { 2161 Parse_Error(PARSE_FATAL, "Zero byte read from file"); 2162 return NULL; 2163 } 2164 /* Move existing data to (near) start of file buffer */ 2165 len = cf->P_end - cf->P_ptr; 2166 tp = cf->P_str + 32; 2167 memmove(tp, cf->P_ptr, len); 2168 dist = cf->P_ptr - tp; 2169 /* Update all pointers to reflect moved data */ 2170 ptr -= dist; 2171 line -= dist; 2172 line_end -= dist; 2173 if (escaped) 2174 escaped -= dist; 2175 if (comment) 2176 comment -= dist; 2177 cf->P_ptr = tp; 2178 tp += len; 2179 cf->P_end = tp; 2180 /* Try to read more data from file into buffer space */ 2181 len = cf->P_str + cf->P_buflen - tp - 32; 2182 if (len <= 0) { 2183 /* We need a bigger buffer to hold this line */ 2184 tp = bmake_realloc(cf->P_str, cf->P_buflen + IFILE_BUFLEN); 2185 cf->P_ptr = cf->P_ptr - cf->P_str + tp; 2186 cf->P_end = cf->P_end - cf->P_str + tp; 2187 ptr = ptr - cf->P_str + tp; 2188 line = line - cf->P_str + tp; 2189 line_end = line_end - cf->P_str + tp; 2190 if (escaped) 2191 escaped = escaped - cf->P_str + tp; 2192 if (comment) 2193 comment = comment - cf->P_str + tp; 2194 cf->P_str = tp; 2195 tp = cf->P_end; 2196 len += IFILE_BUFLEN; 2197 cf->P_buflen += IFILE_BUFLEN; 2198 } 2199 len = read(cf->fd, tp, len); 2200 if (len <= 0) { 2201 if (len < 0) { 2202 Parse_Error(PARSE_FATAL, "Makefile read error: %s", 2203 strerror(errno)); 2204 return NULL; 2205 } 2206 /* End of file */ 2207 break; 2208 } 2209 /* 0 terminate the data, and update end pointer */ 2210 tp += len; 2211 cf->P_end = tp; 2212 *tp = 0; 2213 /* Process newly read characters */ 2214 continue; 2215 } 2216 2217 if (ch == '\\') { 2218 /* Don't treat next character as special, remember first one */ 2219 if (escaped == NULL) 2220 escaped = ptr; 2221 if (ptr[1] == '\n') 2222 cf->lineno++; 2223 ptr += 2; 2224 line_end = ptr; 2225 continue; 2226 } 2227 if (ch == '#' && comment == NULL) { 2228 /* Remember first '#' for comment stripping */ 2229 comment = line_end; 2230 } 2231 ptr++; 2232 if (ch == '\n') 2233 break; 2234 if (!isspace((unsigned char)ch)) 2235 /* We are not interested in trailing whitespace */ 2236 line_end = ptr; 2237 } 2238 2239 /* Save next 'to be processed' location */ 2240 cf->P_ptr = ptr; 2241 2242 /* Check we have a non-comment, non-blank line */ 2243 if (line_end == line || comment == line) { 2244 if (ch == 0) 2245 /* At end of file */ 2246 return NULL; 2247 /* Parse another line */ 2248 continue; 2249 } 2250 2251 /* We now have a line of data */ 2252 *line_end = 0; 2253 2254 if (flags & PARSE_RAW) { 2255 /* Leave '\' (etc) in line buffer (eg 'for' lines) */ 2256 *length = line_end - line; 2257 return line; 2258 } 2259 2260 if (flags & PARSE_SKIP) { 2261 /* Completely ignore non-directives */ 2262 if (line[0] != '.') 2263 continue; 2264 /* We could do more of the .else/.elif/.endif checks here */ 2265 } 2266 break; 2267 } 2268 2269 /* Brutally ignore anything after a non-escaped '#' in non-commands */ 2270 if (comment != NULL && line[0] != '\t') { 2271 line_end = comment; 2272 *line_end = 0; 2273 } 2274 2275 /* If we didn't see a '\\' then the in-situ data is fine */ 2276 if (escaped == NULL) { 2277 *length = line_end - line; 2278 return line; 2279 } 2280 2281 /* Remove escapes from '\n' and '#' */ 2282 tp = ptr = escaped; 2283 escaped = line; 2284 for (; ; *tp++ = ch) { 2285 ch = *ptr++; 2286 if (ch != '\\') { 2287 if (ch == 0) 2288 break; 2289 continue; 2290 } 2291 2292 ch = *ptr++; 2293 if (ch == 0) { 2294 /* Delete '\\' at end of buffer */ 2295 tp--; 2296 break; 2297 } 2298 2299 if (ch == '#' && line[0] != '\t') 2300 /* Delete '\\' from before '#' on non-command lines */ 2301 continue; 2302 2303 if (ch != '\n') { 2304 /* Leave '\\' in buffer for later */ 2305 *tp++ = '\\'; 2306 /* Make sure we don't delete an escaped ' ' from the line end */ 2307 escaped = tp + 1; 2308 continue; 2309 } 2310 2311 /* Escaped '\n' replace following whitespace with a single ' ' */ 2312 while (ptr[0] == ' ' || ptr[0] == '\t') 2313 ptr++; 2314 ch = ' '; 2315 } 2316 2317 /* Delete any trailing spaces - eg from empty continuations */ 2318 while (tp > escaped && isspace((unsigned char)tp[-1])) 2319 tp--; 2320 2321 *tp = 0; 2322 *length = tp - line; 2323 return line; 2324 } 2325 2326 /*- 2327 *--------------------------------------------------------------------- 2328 * ParseReadLine -- 2329 * Read an entire line from the input file. Called only by Parse_File. 2330 * 2331 * Results: 2332 * A line w/o its newline 2333 * 2334 * Side Effects: 2335 * Only those associated with reading a character 2336 *--------------------------------------------------------------------- 2337 */ 2338 static char * 2339 ParseReadLine(void) 2340 { 2341 char *line; /* Result */ 2342 int lineLength; /* Length of result */ 2343 int lineno; /* Saved line # */ 2344 int rval; 2345 2346 for (;;) { 2347 line = ParseGetLine(0, &lineLength); 2348 if (line == NULL) 2349 return NULL; 2350 2351 if (line[0] != '.') 2352 return line; 2353 2354 /* 2355 * The line might be a conditional. Ask the conditional module 2356 * about it and act accordingly 2357 */ 2358 switch (Cond_Eval(line)) { 2359 case COND_SKIP: 2360 /* Skip to next conditional that evaluates to COND_PARSE. */ 2361 do { 2362 line = ParseGetLine(PARSE_SKIP, &lineLength); 2363 } while (line && Cond_Eval(line) != COND_PARSE); 2364 if (line == NULL) 2365 break; 2366 continue; 2367 case COND_PARSE: 2368 continue; 2369 case COND_INVALID: /* Not a conditional line */ 2370 /* Check for .for loops */ 2371 rval = For_Eval(line); 2372 if (rval == 0) 2373 /* Not a .for line */ 2374 break; 2375 if (rval < 0) 2376 /* Syntax error - error printed, ignore line */ 2377 continue; 2378 /* Start of a .for loop */ 2379 lineno = curFile->lineno; 2380 /* Accumulate loop lines until matching .endfor */ 2381 do { 2382 line = ParseGetLine(PARSE_RAW, &lineLength); 2383 if (line == NULL) { 2384 Parse_Error(PARSE_FATAL, 2385 "Unexpected end of file in for loop.\n"); 2386 break; 2387 } 2388 } while (For_Accum(line)); 2389 /* Stash each iteration as a new 'input file' */ 2390 For_Run(lineno); 2391 /* Read next line from for-loop buffer */ 2392 continue; 2393 } 2394 return (line); 2395 } 2396 } 2397 2398 /*- 2399 *----------------------------------------------------------------------- 2400 * ParseFinishLine -- 2401 * Handle the end of a dependency group. 2402 * 2403 * Results: 2404 * Nothing. 2405 * 2406 * Side Effects: 2407 * inLine set FALSE. 'targets' list destroyed. 2408 * 2409 *----------------------------------------------------------------------- 2410 */ 2411 static void 2412 ParseFinishLine(void) 2413 { 2414 if (inLine) { 2415 Lst_ForEach(targets, Suff_EndTransform, NULL); 2416 Lst_Destroy(targets, ParseHasCommands); 2417 targets = NULL; 2418 inLine = FALSE; 2419 } 2420 } 2421 2422 2423 /*- 2424 *--------------------------------------------------------------------- 2425 * Parse_File -- 2426 * Parse a file into its component parts, incorporating it into the 2427 * current dependency graph. This is the main function and controls 2428 * almost every other function in this module 2429 * 2430 * Input: 2431 * name the name of the file being read 2432 * fd Open file to makefile to parse 2433 * 2434 * Results: 2435 * None 2436 * 2437 * Side Effects: 2438 * closes fd. 2439 * Loads. Nodes are added to the list of all targets, nodes and links 2440 * are added to the dependency graph. etc. etc. etc. 2441 *--------------------------------------------------------------------- 2442 */ 2443 void 2444 Parse_File(const char *name, int fd) 2445 { 2446 char *cp; /* pointer into the line */ 2447 char *line; /* the line we're working on */ 2448 2449 inLine = FALSE; 2450 fatals = 0; 2451 2452 Parse_SetInput(name, 0, fd, NULL); 2453 2454 do { 2455 for (; (line = ParseReadLine()) != NULL; ) { 2456 if (DEBUG(PARSE)) 2457 fprintf(debug_file, "ParseReadLine (%d): '%s'\n", 2458 curFile->lineno, line); 2459 if (*line == '.') { 2460 /* 2461 * Lines that begin with the special character are either 2462 * include or undef directives. 2463 */ 2464 for (cp = line + 1; isspace((unsigned char)*cp); cp++) { 2465 continue; 2466 } 2467 if (strncmp(cp, "include", 7) == 0 || 2468 ((cp[0] == 's' || cp[0] == '-') && 2469 strncmp(&cp[1], "include", 7) == 0)) { 2470 ParseDoInclude(cp); 2471 continue; 2472 } 2473 if (strncmp(cp, "undef", 5) == 0) { 2474 char *cp2; 2475 for (cp += 5; isspace((unsigned char) *cp); cp++) 2476 continue; 2477 for (cp2 = cp; !isspace((unsigned char) *cp2) && 2478 (*cp2 != '\0'); cp2++) 2479 continue; 2480 *cp2 = '\0'; 2481 Var_Delete(cp, VAR_GLOBAL); 2482 continue; 2483 } else if (strncmp(cp, "export", 6) == 0) { 2484 for (cp += 6; isspace((unsigned char) *cp); cp++) 2485 continue; 2486 Var_Export(cp, 1); 2487 continue; 2488 } 2489 } 2490 2491 if (*line == '\t') { 2492 /* 2493 * If a line starts with a tab, it can only hope to be 2494 * a creation command. 2495 */ 2496 cp = line + 1; 2497 shellCommand: 2498 for (; isspace ((unsigned char)*cp); cp++) { 2499 continue; 2500 } 2501 if (*cp) { 2502 if (!inLine) 2503 Parse_Error(PARSE_FATAL, 2504 "Unassociated shell command \"%s\"", 2505 cp); 2506 /* 2507 * So long as it's not a blank line and we're actually 2508 * in a dependency spec, add the command to the list of 2509 * commands of all targets in the dependency spec 2510 */ 2511 if (targets) { 2512 cp = bmake_strdup(cp); 2513 Lst_ForEach(targets, ParseAddCmd, cp); 2514 #ifdef CLEANUP 2515 Lst_AtEnd(targCmds, cp); 2516 #endif 2517 } 2518 } 2519 continue; 2520 } 2521 2522 #ifdef SYSVINCLUDE 2523 if (((strncmp(line, "include", 7) == 0 && 2524 isspace((unsigned char) line[7])) || 2525 ((line[0] == 's' || line[0] == '-') && 2526 strncmp(&line[1], "include", 7) == 0 && 2527 isspace((unsigned char) line[8]))) && 2528 strchr(line, ':') == NULL) { 2529 /* 2530 * It's an S3/S5-style "include". 2531 */ 2532 ParseTraditionalInclude(line); 2533 continue; 2534 } 2535 #endif 2536 if (Parse_IsVar(line)) { 2537 ParseFinishLine(); 2538 Parse_DoVar(line, VAR_GLOBAL); 2539 continue; 2540 } 2541 2542 #ifndef POSIX 2543 /* 2544 * To make life easier on novices, if the line is indented we 2545 * first make sure the line has a dependency operator in it. 2546 * If it doesn't have an operator and we're in a dependency 2547 * line's script, we assume it's actually a shell command 2548 * and add it to the current list of targets. 2549 */ 2550 cp = line; 2551 if (isspace((unsigned char) line[0])) { 2552 while ((*cp != '\0') && isspace((unsigned char) *cp)) 2553 cp++; 2554 while (*cp && (ParseIsEscaped(line, cp) || 2555 (*cp != ':') && (*cp != '!'))) { 2556 cp++; 2557 } 2558 if (*cp == '\0') { 2559 if (inLine) { 2560 Parse_Error(PARSE_WARNING, 2561 "Shell command needs a leading tab"); 2562 goto shellCommand; 2563 } 2564 } 2565 } 2566 #endif 2567 ParseFinishLine(); 2568 2569 /* 2570 * For some reason - probably to make the parser impossible - 2571 * a ';' can be used to separate commands from dependencies. 2572 * No attempt is made to avoid ';' inside substitution patterns. 2573 */ 2574 for (cp = line; *cp != 0; cp++) { 2575 if (*cp == '\\' && cp[1] != 0) { 2576 cp++; 2577 continue; 2578 } 2579 if (*cp == ';') 2580 break; 2581 } 2582 if (*cp != 0) 2583 /* Terminate the dependency list at the ';' */ 2584 *cp++ = 0; 2585 else 2586 cp = NULL; 2587 2588 /* 2589 * We now know it's a dependency line so it needs to have all 2590 * variables expanded before being parsed. Tell the variable 2591 * module to complain if some variable is undefined... 2592 */ 2593 line = Var_Subst(NULL, line, VAR_CMD, TRUE); 2594 2595 /* 2596 * Need a non-circular list for the target nodes 2597 */ 2598 if (targets) 2599 Lst_Destroy(targets, NULL); 2600 2601 targets = Lst_Init(FALSE); 2602 inLine = TRUE; 2603 2604 ParseDoDependency(line); 2605 free(line); 2606 2607 /* If there were commands after a ';', add them now */ 2608 if (cp != NULL) { 2609 goto shellCommand; 2610 } 2611 } 2612 /* 2613 * Reached EOF, but it may be just EOF of an include file... 2614 */ 2615 } while (ParseEOF() == CONTINUE); 2616 2617 if (fatals) { 2618 (void)fprintf(stderr, 2619 "%s: Fatal errors encountered -- cannot continue\n", 2620 progname); 2621 PrintOnError(NULL); 2622 exit(1); 2623 } 2624 } 2625 2626 /*- 2627 *--------------------------------------------------------------------- 2628 * Parse_Init -- 2629 * initialize the parsing module 2630 * 2631 * Results: 2632 * none 2633 * 2634 * Side Effects: 2635 * the parseIncPath list is initialized... 2636 *--------------------------------------------------------------------- 2637 */ 2638 void 2639 Parse_Init(void) 2640 { 2641 mainNode = NULL; 2642 parseIncPath = Lst_Init(FALSE); 2643 sysIncPath = Lst_Init(FALSE); 2644 defIncPath = Lst_Init(FALSE); 2645 includes = Lst_Init(FALSE); 2646 #ifdef CLEANUP 2647 targCmds = Lst_Init(FALSE); 2648 #endif 2649 } 2650 2651 void 2652 Parse_End(void) 2653 { 2654 #ifdef CLEANUP 2655 Lst_Destroy(targCmds, (FreeProc *)free); 2656 if (targets) 2657 Lst_Destroy(targets, NULL); 2658 Lst_Destroy(defIncPath, Dir_Destroy); 2659 Lst_Destroy(sysIncPath, Dir_Destroy); 2660 Lst_Destroy(parseIncPath, Dir_Destroy); 2661 Lst_Destroy(includes, NULL); /* Should be empty now */ 2662 #endif 2663 } 2664 2665 2666 /*- 2667 *----------------------------------------------------------------------- 2668 * Parse_MainName -- 2669 * Return a Lst of the main target to create for main()'s sake. If 2670 * no such target exists, we Punt with an obnoxious error message. 2671 * 2672 * Results: 2673 * A Lst of the single node to create. 2674 * 2675 * Side Effects: 2676 * None. 2677 * 2678 *----------------------------------------------------------------------- 2679 */ 2680 Lst 2681 Parse_MainName(void) 2682 { 2683 Lst mainList; /* result list */ 2684 2685 mainList = Lst_Init(FALSE); 2686 2687 if (mainNode == NULL) { 2688 Punt("no target to make."); 2689 /*NOTREACHED*/ 2690 } else if (mainNode->type & OP_DOUBLEDEP) { 2691 (void)Lst_AtEnd(mainList, mainNode); 2692 Lst_Concat(mainList, mainNode->cohorts, LST_CONCNEW); 2693 } 2694 else 2695 (void)Lst_AtEnd(mainList, mainNode); 2696 Var_Append(".TARGETS", mainNode->name, VAR_GLOBAL); 2697 return (mainList); 2698 } 2699 2700 /*- 2701 *----------------------------------------------------------------------- 2702 * ParseMark -- 2703 * Add the filename and lineno to the GNode so that we remember 2704 * where it was first defined. 2705 * 2706 * Side Effects: 2707 * None. 2708 * 2709 *----------------------------------------------------------------------- 2710 */ 2711 static void 2712 ParseMark(GNode *gn) 2713 { 2714 gn->fname = curFile->fname; 2715 gn->lineno = curFile->lineno; 2716 } 2717