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