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