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