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