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