140395Sbostic /* 240395Sbostic * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. 340395Sbostic * Copyright (c) 1988, 1989 by Adam de Boor 440395Sbostic * Copyright (c) 1989 by Berkeley Softworks 540395Sbostic * All rights reserved. 640395Sbostic * 740395Sbostic * This code is derived from software contributed to Berkeley by 840395Sbostic * Adam de Boor. 940395Sbostic * 1040395Sbostic * Redistribution and use in source and binary forms are permitted 1140395Sbostic * provided that the above copyright notice and this paragraph are 1240395Sbostic * duplicated in all such forms and that any documentation, 1340395Sbostic * advertising materials, and other materials related to such 1440395Sbostic * distribution and use acknowledge that the software was developed 1540395Sbostic * by the University of California, Berkeley. The name of the 1640395Sbostic * University may not be used to endorse or promote products derived 1740395Sbostic * from this software without specific prior written permission. 1840395Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1940395Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 2040395Sbostic * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 2140395Sbostic */ 2240395Sbostic 2340395Sbostic #ifndef lint 24*40534Sbostic static char sccsid[] = "@(#)parse.c 5.9 (Berkeley) 03/19/90"; 2540395Sbostic #endif /* not lint */ 2640395Sbostic 2740393Sbostic /*- 2840393Sbostic * parse.c -- 2940393Sbostic * Functions to parse a makefile. 3040393Sbostic * 3140393Sbostic * One function, Parse_Init, must be called before any functions 3240393Sbostic * in this module are used. After that, the function Parse_File is the 3340393Sbostic * main entry point and controls most of the other functions in this 3440393Sbostic * module. 3540393Sbostic * 3640393Sbostic * Most important structures are kept in Lsts. Directories for 3740393Sbostic * the #include "..." function are kept in the 'parseIncPath' Lst, while 3840393Sbostic * those for the #include <...> are kept in the 'sysIncPath' Lst. The 3940393Sbostic * targets currently being defined are kept in the 'targets' Lst. 4040393Sbostic * 4140393Sbostic * The variables 'fname' and 'lineno' are used to track the name 4240393Sbostic * of the current file and the line number in that file so that error 4340393Sbostic * messages can be more meaningful. 4440393Sbostic * 4540393Sbostic * Interface: 4640393Sbostic * Parse_Init Initialization function which must be 4740393Sbostic * called before anything else in this module 4840393Sbostic * is used. 4940393Sbostic * 5040393Sbostic * Parse_File Function used to parse a makefile. It must 5140393Sbostic * be given the name of the file, which should 5240393Sbostic * already have been opened, and a function 5340393Sbostic * to call to read a character from the file. 5440393Sbostic * 5540393Sbostic * Parse_IsVar Returns TRUE if the given line is a 5640393Sbostic * variable assignment. Used by MainParseArgs 5740393Sbostic * to determine if an argument is a target 5840393Sbostic * or a variable assignment. Used internally 5940393Sbostic * for pretty much the same thing... 6040393Sbostic * 6140393Sbostic * Parse_Error Function called when an error occurs in 6240393Sbostic * parsing. Used by the variable and 6340393Sbostic * conditional modules. 6440393Sbostic * Parse_MainName Returns a Lst of the main target to create. 6540393Sbostic */ 6640393Sbostic 6740444Sbostic #include <varargs.h> 6840444Sbostic #include <stdio.h> 6940444Sbostic #include <ctype.h> 7040444Sbostic #include "make.h" 7140444Sbostic #include "buf.h" 7240515Sbostic #include "pathnames.h" 7340393Sbostic 7440393Sbostic /* 7540393Sbostic * These values are returned by ParseEOF to tell Parse_File whether to 7640393Sbostic * CONTINUE parsing, i.e. it had only reached the end of an include file, 7740393Sbostic * or if it's DONE. 7840393Sbostic */ 7940393Sbostic #define CONTINUE 1 8040393Sbostic #define DONE 0 8140393Sbostic static int ParseEOF(); 8240393Sbostic 8340393Sbostic static Lst targets; /* targets we're working on */ 8440393Sbostic static Boolean inLine; /* true if currently in a dependency 8540393Sbostic * line or its commands */ 8640393Sbostic 8740393Sbostic static char *fname; /* name of current file (for errors) */ 8840393Sbostic static int lineno; /* line number in current file */ 8940393Sbostic static FILE *curFILE; /* current makefile */ 9040393Sbostic 9140393Sbostic static int fatals = 0; 9240393Sbostic 9340393Sbostic static GNode *mainNode; /* The main target to create. This is the 9440393Sbostic * first target on the first dependency 9540393Sbostic * line in the first makefile */ 9640393Sbostic /* 9740393Sbostic * Definitions for handling #include specifications 9840393Sbostic */ 9940393Sbostic typedef struct IFile { 10040393Sbostic char *fname; /* name of previous file */ 10140393Sbostic int lineno; /* saved line number */ 10240393Sbostic FILE * F; /* the open stream */ 10340393Sbostic } IFile; 10440393Sbostic 10540393Sbostic static Lst includes; /* stack of IFiles generated by 10640393Sbostic * #includes */ 10740393Sbostic Lst parseIncPath; /* list of directories for "..." includes */ 10840393Sbostic Lst sysIncPath; /* list of directories for <...> includes */ 10940393Sbostic 11040393Sbostic /*- 11140393Sbostic * specType contains the SPECial TYPE of the current target. It is 11240393Sbostic * Not if the target is unspecial. If it *is* special, however, the children 11340393Sbostic * are linked as children of the parent but not vice versa. This variable is 11440393Sbostic * set in ParseDoDependency 11540393Sbostic */ 11640393Sbostic typedef enum { 11740393Sbostic Begin, /* .BEGIN */ 11840393Sbostic Default, /* .DEFAULT */ 11940393Sbostic End, /* .END */ 12040393Sbostic Ignore, /* .IGNORE */ 12140393Sbostic Includes, /* .INCLUDES */ 12240393Sbostic Interrupt, /* .INTERRUPT */ 12340393Sbostic Libs, /* .LIBS */ 12440393Sbostic MFlags, /* .MFLAGS or .MAKEFLAGS */ 12540393Sbostic Main, /* .MAIN and we don't have anything user-specified to 12640393Sbostic * make */ 12740393Sbostic Not, /* Not special */ 12840393Sbostic NotParallel, /* .NOTPARALELL */ 12940393Sbostic Null, /* .NULL */ 13040393Sbostic Order, /* .ORDER */ 13140393Sbostic Path, /* .PATH */ 13240393Sbostic Precious, /* .PRECIOUS */ 13340393Sbostic Shell, /* .SHELL */ 13440393Sbostic Silent, /* .SILENT */ 13540393Sbostic SingleShell, /* .SINGLESHELL */ 13640393Sbostic Suffixes, /* .SUFFIXES */ 13740393Sbostic Attribute, /* Generic attribute */ 13840393Sbostic } ParseSpecial; 13940393Sbostic 14040393Sbostic ParseSpecial specType; 14140393Sbostic 14240393Sbostic /* 14340393Sbostic * Predecessor node for handling .ORDER. Initialized to NILGNODE when .ORDER 14440393Sbostic * seen, then set to each successive source on the line. 14540393Sbostic */ 14640393Sbostic static GNode *predecessor; 14740393Sbostic 14840393Sbostic /* 14940393Sbostic * The parseKeywords table is searched using binary search when deciding 15040393Sbostic * if a target or source is special. The 'spec' field is the ParseSpecial 15140393Sbostic * type of the keyword ("Not" if the keyword isn't special as a target) while 15240393Sbostic * the 'op' field is the operator to apply to the list of targets if the 15340393Sbostic * keyword is used as a source ("0" if the keyword isn't special as a source) 15440393Sbostic */ 15540393Sbostic static struct { 15640393Sbostic char *name; /* Name of keyword */ 15740393Sbostic ParseSpecial spec; /* Type when used as a target */ 15840393Sbostic int op; /* Operator when used as a source */ 15940393Sbostic } parseKeywords[] = { 16040393Sbostic { ".BEGIN", Begin, 0 }, 16140393Sbostic { ".DEFAULT", Default, 0 }, 16240393Sbostic { ".DONTCARE", Attribute, OP_DONTCARE }, 16340393Sbostic { ".END", End, 0 }, 16440393Sbostic { ".EXEC", Attribute, OP_EXEC }, 16540393Sbostic { ".IGNORE", Ignore, OP_IGNORE }, 16640393Sbostic { ".INCLUDES", Includes, 0 }, 16740393Sbostic { ".INTERRUPT", Interrupt, 0 }, 16840393Sbostic { ".INVISIBLE", Attribute, OP_INVISIBLE }, 16940393Sbostic { ".JOIN", Attribute, OP_JOIN }, 17040393Sbostic { ".LIBS", Libs, 0 }, 17140393Sbostic { ".MAIN", Main, 0 }, 17240393Sbostic { ".MAKE", Attribute, OP_MAKE }, 17340393Sbostic { ".MAKEFLAGS", MFlags, 0 }, 17440393Sbostic { ".MFLAGS", MFlags, 0 }, 17540393Sbostic { ".NOTMAIN", Attribute, OP_NOTMAIN }, 17640393Sbostic { ".NOTPARALLEL", NotParallel, 0 }, 17740393Sbostic { ".NULL", Null, 0 }, 17840393Sbostic { ".ORDER", Order, 0 }, 17940393Sbostic { ".PATH", Path, 0 }, 18040393Sbostic { ".PRECIOUS", Precious, OP_PRECIOUS }, 18140393Sbostic { ".RECURSIVE", Attribute, OP_MAKE }, 18240393Sbostic { ".SHELL", Shell, 0 }, 18340393Sbostic { ".SILENT", Silent, OP_SILENT }, 18440393Sbostic { ".SINGLESHELL", SingleShell, 0 }, 18540393Sbostic { ".SUFFIXES", Suffixes, 0 }, 18640393Sbostic { ".USE", Attribute, OP_USE }, 18740393Sbostic }; 18840424Sbostic 18940393Sbostic /*- 19040393Sbostic *---------------------------------------------------------------------- 19140393Sbostic * ParseFindKeyword -- 19240393Sbostic * Look in the table of keywords for one matching the given string. 19340393Sbostic * 19440393Sbostic * Results: 19540393Sbostic * The index of the keyword, or -1 if it isn't there. 19640393Sbostic * 19740393Sbostic * Side Effects: 19840393Sbostic * None 19940393Sbostic *---------------------------------------------------------------------- 20040393Sbostic */ 20140393Sbostic static int 20240393Sbostic ParseFindKeyword (str) 20340393Sbostic char *str; /* String to find */ 20440393Sbostic { 20540393Sbostic register int start, 20640393Sbostic end, 20740393Sbostic cur; 20840393Sbostic register int diff; 20940393Sbostic 21040393Sbostic start = 0; 21140393Sbostic end = (sizeof(parseKeywords)/sizeof(parseKeywords[0])) - 1; 21240393Sbostic 21340393Sbostic do { 21440393Sbostic cur = start + ((end - start) / 2); 21540393Sbostic diff = strcmp (str, parseKeywords[cur].name); 21640393Sbostic 21740393Sbostic if (diff == 0) { 21840393Sbostic return (cur); 21940393Sbostic } else if (diff < 0) { 22040393Sbostic end = cur - 1; 22140393Sbostic } else { 22240393Sbostic start = cur + 1; 22340393Sbostic } 22440393Sbostic } while (start <= end); 22540393Sbostic return (-1); 22640393Sbostic } 22740424Sbostic 22840393Sbostic /*- 22940393Sbostic * Parse_Error -- 23040393Sbostic * Error message abort function for parsing. Prints out the context 23140393Sbostic * of the error (line number and file) as well as the message with 23240393Sbostic * two optional arguments. 23340393Sbostic * 23440393Sbostic * Results: 23540393Sbostic * None 23640393Sbostic * 23740393Sbostic * Side Effects: 23840393Sbostic * "fatals" is incremented if the level is PARSE_FATAL. 23940393Sbostic */ 24040444Sbostic /* VARARGS */ 24140393Sbostic void 24240444Sbostic Parse_Error(type, va_alist) 24340444Sbostic int type; /* Error type (PARSE_WARNING, PARSE_FATAL) */ 24440444Sbostic va_dcl 24540393Sbostic { 24640444Sbostic va_list ap; 24740444Sbostic char *fmt; 24840393Sbostic 24940444Sbostic if (type == PARSE_WARNING && noWarnings) 25040444Sbostic return; 25140444Sbostic (void)fprintf(stderr, "\"%s\", line %d: ", fname, lineno); 25240444Sbostic if (type == PARSE_WARNING) 25340444Sbostic (void)fprintf(stderr, "Warning: "); 25440476Sbostic va_start(ap); 25540444Sbostic fmt = va_arg(ap, char *); 25640444Sbostic (void)vfprintf(stderr, fmt, ap); 25740444Sbostic va_end(ap); 25840444Sbostic (void)fprintf(stderr, "\n"); 25940444Sbostic (void)fflush(stderr); 26040444Sbostic if (type == PARSE_FATAL) 26140444Sbostic fatals += 1; 26240393Sbostic } 26340424Sbostic 26440393Sbostic /*- 26540393Sbostic *--------------------------------------------------------------------- 26640393Sbostic * ParseLinkSrc -- 26740393Sbostic * Link the parent node to its new child. Used in a Lst_ForEach by 26840393Sbostic * ParseDoDependency. If the specType isn't 'Not', the parent 26940393Sbostic * isn't linked as a parent of the child. 27040393Sbostic * 27140393Sbostic * Results: 27240393Sbostic * Always = 0 27340393Sbostic * 27440393Sbostic * Side Effects: 27540393Sbostic * New elements are added to the parents list of cgn and the 27640393Sbostic * children list of cgn. the unmade field of pgn is updated 27740393Sbostic * to reflect the additional child. 27840393Sbostic *--------------------------------------------------------------------- 27940393Sbostic */ 28040393Sbostic static int 28140393Sbostic ParseLinkSrc (pgn, cgn) 28240393Sbostic GNode *pgn; /* The parent node */ 28340393Sbostic GNode *cgn; /* The child node */ 28440393Sbostic { 28540393Sbostic if (Lst_Member (pgn->children, (ClientData)cgn) == NILLNODE) { 28640393Sbostic (void)Lst_AtEnd (pgn->children, (ClientData)cgn); 28740393Sbostic if (specType == Not) { 28840393Sbostic (void)Lst_AtEnd (cgn->parents, (ClientData)pgn); 28940393Sbostic } 29040393Sbostic pgn->unmade += 1; 29140393Sbostic } 29240393Sbostic return (0); 29340393Sbostic } 29440424Sbostic 29540393Sbostic /*- 29640393Sbostic *--------------------------------------------------------------------- 29740393Sbostic * ParseDoOp -- 29840393Sbostic * Apply the parsed operator to the given target node. Used in a 29940393Sbostic * Lst_ForEach call by ParseDoDependency once all targets have 30040393Sbostic * been found and their operator parsed. If the previous and new 30140393Sbostic * operators are incompatible, a major error is taken. 30240393Sbostic * 30340393Sbostic * Results: 30440393Sbostic * Always 0 30540393Sbostic * 30640393Sbostic * Side Effects: 30740393Sbostic * The type field of the node is altered to reflect any new bits in 30840393Sbostic * the op. 30940393Sbostic *--------------------------------------------------------------------- 31040393Sbostic */ 31140393Sbostic static int 31240393Sbostic ParseDoOp (gn, op) 31340393Sbostic GNode *gn; /* The node to which the operator is to be 31440393Sbostic * applied */ 31540393Sbostic int op; /* The operator to apply */ 31640393Sbostic { 31740393Sbostic /* 31840393Sbostic * If the dependency mask of the operator and the node don't match and 31940393Sbostic * the node has actually had an operator applied to it before, and 32040393Sbostic * the operator actually has some dependency information in it, complain. 32140393Sbostic */ 32240393Sbostic if (((op & OP_OPMASK) != (gn->type & OP_OPMASK)) && 32340393Sbostic !OP_NOP(gn->type) && !OP_NOP(op)) 32440393Sbostic { 32540393Sbostic Parse_Error (PARSE_FATAL, "Inconsistent operator for %s", gn->name); 32640393Sbostic return (1); 32740393Sbostic } 32840393Sbostic 32940393Sbostic if ((op == OP_DOUBLEDEP) && ((gn->type & OP_OPMASK) == OP_DOUBLEDEP)) { 33040393Sbostic /* 33140393Sbostic * If the node was the object of a :: operator, we need to create a 33240393Sbostic * new instance of it for the children and commands on this dependency 33340393Sbostic * line. The new instance is placed on the 'cohorts' list of the 33440393Sbostic * initial one (note the initial one is not on its own cohorts list) 33540393Sbostic * and the new instance is linked to all parents of the initial 33640393Sbostic * instance. 33740393Sbostic */ 33840393Sbostic register GNode *cohort; 33940393Sbostic LstNode ln; 34040393Sbostic 34140393Sbostic cohort = Targ_NewGN(gn->name); 34240393Sbostic /* 34340393Sbostic * Duplicate links to parents so graph traversal is simple. Perhaps 34440393Sbostic * some type bits should be duplicated? 34540393Sbostic * 34640393Sbostic * Make the cohort invisible as well to avoid duplicating it into 34740393Sbostic * other variables. True, parents of this target won't tend to do 34840393Sbostic * anything with their local variables, but better safe than 34940393Sbostic * sorry. 35040393Sbostic */ 35140393Sbostic Lst_ForEach(gn->parents, ParseLinkSrc, (ClientData)cohort); 35240393Sbostic cohort->type = OP_DOUBLEDEP|OP_INVISIBLE; 35340393Sbostic (void)Lst_AtEnd(gn->cohorts, (ClientData)cohort); 35440393Sbostic 35540393Sbostic /* 35640393Sbostic * Replace the node in the targets list with the new copy 35740393Sbostic */ 35840393Sbostic ln = Lst_Member(targets, (ClientData)gn); 35940393Sbostic Lst_Replace(ln, (ClientData)cohort); 36040393Sbostic gn = cohort; 36140393Sbostic } 36240393Sbostic /* 36340393Sbostic * We don't want to nuke any previous flags (whatever they were) so we 36440393Sbostic * just OR the new operator into the old 36540393Sbostic */ 36640393Sbostic gn->type |= op; 36740393Sbostic 36840393Sbostic return (0); 36940393Sbostic } 37040424Sbostic 37140393Sbostic /*- 37240393Sbostic *--------------------------------------------------------------------- 37340393Sbostic * ParseDoSrc -- 37440393Sbostic * Given the name of a source, figure out if it is an attribute 37540393Sbostic * and apply it to the targets if it is. Else decide if there is 37640393Sbostic * some attribute which should be applied *to* the source because 37740393Sbostic * of some special target and apply it if so. Otherwise, make the 37840393Sbostic * source be a child of the targets in the list 'targets' 37940393Sbostic * 38040393Sbostic * Results: 38140393Sbostic * None 38240393Sbostic * 38340393Sbostic * Side Effects: 38440393Sbostic * Operator bits may be added to the list of targets or to the source. 38540393Sbostic * The targets may have a new source added to their lists of children. 38640393Sbostic *--------------------------------------------------------------------- 38740393Sbostic */ 38840393Sbostic static void 38940393Sbostic ParseDoSrc (tOp, src) 39040393Sbostic int tOp; /* operator (if any) from special targets */ 39140393Sbostic char *src; /* name of the source to handle */ 39240393Sbostic { 39340393Sbostic int op; /* operator (if any) from special source */ 39440393Sbostic GNode *gn; 39540393Sbostic 39640393Sbostic op = 0; 39740393Sbostic if (*src == '.' && isupper (src[1])) { 39840393Sbostic int keywd = ParseFindKeyword(src); 39940393Sbostic if (keywd != -1) { 40040393Sbostic op = parseKeywords[keywd].op; 40140393Sbostic } 40240393Sbostic } 40340393Sbostic if (op != 0) { 40440393Sbostic Lst_ForEach (targets, ParseDoOp, (ClientData)op); 40540393Sbostic } else if (specType == Main) { 40640393Sbostic /* 40740393Sbostic * If we have noted the existence of a .MAIN, it means we need 40840393Sbostic * to add the sources of said target to the list of things 40940393Sbostic * to create. The string 'src' is likely to be free, so we 41040393Sbostic * must make a new copy of it. Note that this will only be 41140393Sbostic * invoked if the user didn't specify a target on the command 41240393Sbostic * line. This is to allow #ifmake's to succeed, or something... 41340393Sbostic */ 41440424Sbostic (void) Lst_AtEnd (create, (ClientData)strdup(src)); 41540393Sbostic /* 41640393Sbostic * Add the name to the .TARGETS variable as well, so the user cna 41740393Sbostic * employ that, if desired. 41840393Sbostic */ 41940393Sbostic Var_Append(".TARGETS", src, VAR_GLOBAL); 42040393Sbostic } else if (specType == Order) { 42140393Sbostic /* 42240393Sbostic * Create proper predecessor/successor links between the previous 42340393Sbostic * source and the current one. 42440393Sbostic */ 42540393Sbostic gn = Targ_FindNode(src, TARG_CREATE); 42640393Sbostic if (predecessor != NILGNODE) { 42740393Sbostic (void)Lst_AtEnd(predecessor->successors, (ClientData)gn); 42840393Sbostic (void)Lst_AtEnd(gn->preds, (ClientData)predecessor); 42940393Sbostic } 43040393Sbostic /* 43140393Sbostic * The current source now becomes the predecessor for the next one. 43240393Sbostic */ 43340393Sbostic predecessor = gn; 43440393Sbostic } else { 43540393Sbostic /* 43640393Sbostic * If the source is not an attribute, we need to find/create 43740393Sbostic * a node for it. After that we can apply any operator to it 43840393Sbostic * from a special target or link it to its parents, as 43940393Sbostic * appropriate. 44040393Sbostic * 44140393Sbostic * In the case of a source that was the object of a :: operator, 44240393Sbostic * the attribute is applied to all of its instances (as kept in 44340393Sbostic * the 'cohorts' list of the node) or all the cohorts are linked 44440393Sbostic * to all the targets. 44540393Sbostic */ 44640393Sbostic gn = Targ_FindNode (src, TARG_CREATE); 44740393Sbostic if (tOp) { 44840393Sbostic gn->type |= tOp; 44940393Sbostic } else { 45040393Sbostic Lst_ForEach (targets, ParseLinkSrc, (ClientData)gn); 45140393Sbostic } 45240393Sbostic if ((gn->type & OP_OPMASK) == OP_DOUBLEDEP) { 45340393Sbostic register GNode *cohort; 45440393Sbostic register LstNode ln; 45540393Sbostic 45640393Sbostic for (ln=Lst_First(gn->cohorts); ln != NILLNODE; ln = Lst_Succ(ln)){ 45740393Sbostic cohort = (GNode *)Lst_Datum(ln); 45840393Sbostic if (tOp) { 45940393Sbostic cohort->type |= tOp; 46040393Sbostic } else { 46140393Sbostic Lst_ForEach(targets, ParseLinkSrc, (ClientData)cohort); 46240393Sbostic } 46340393Sbostic } 46440393Sbostic } 46540393Sbostic } 46640393Sbostic } 46740424Sbostic 46840393Sbostic /*- 46940393Sbostic *----------------------------------------------------------------------- 47040393Sbostic * ParseFindMain -- 47140393Sbostic * Find a real target in the list and set it to be the main one. 47240393Sbostic * Called by ParseDoDependency when a main target hasn't been found 47340393Sbostic * yet. 47440393Sbostic * 47540393Sbostic * Results: 47640393Sbostic * 0 if main not found yet, 1 if it is. 47740393Sbostic * 47840393Sbostic * Side Effects: 47940393Sbostic * mainNode is changed and Targ_SetMain is called. 48040393Sbostic * 48140393Sbostic *----------------------------------------------------------------------- 48240393Sbostic */ 48340393Sbostic static int 48440393Sbostic ParseFindMain(gn) 48540393Sbostic GNode *gn; /* Node to examine */ 48640393Sbostic { 48740393Sbostic if ((gn->type & (OP_NOTMAIN|OP_USE|OP_EXEC|OP_TRANSFORM)) == 0) { 48840393Sbostic mainNode = gn; 48940393Sbostic Targ_SetMain(gn); 49040393Sbostic return (1); 49140393Sbostic } else { 49240393Sbostic return (0); 49340393Sbostic } 49440393Sbostic } 49540424Sbostic 49640393Sbostic /*- 49740393Sbostic *----------------------------------------------------------------------- 49840393Sbostic * ParseAddDir -- 49940393Sbostic * Front-end for Dir_AddDir to make sure Lst_ForEach keeps going 50040393Sbostic * 50140393Sbostic * Results: 50240393Sbostic * === 0 50340393Sbostic * 50440393Sbostic * Side Effects: 50540393Sbostic * See Dir_AddDir. 50640393Sbostic * 50740393Sbostic *----------------------------------------------------------------------- 50840393Sbostic */ 50940393Sbostic static int 51040393Sbostic ParseAddDir(path, name) 51140393Sbostic Lst path; 51240393Sbostic char *name; 51340393Sbostic { 51440393Sbostic Dir_AddDir(path, name); 51540393Sbostic return(0); 51640393Sbostic } 51740424Sbostic 51840393Sbostic /*- 51940393Sbostic *----------------------------------------------------------------------- 52040393Sbostic * ParseClearPath -- 52140393Sbostic * Front-end for Dir_ClearPath to make sure Lst_ForEach keeps going 52240393Sbostic * 52340393Sbostic * Results: 52440393Sbostic * === 0 52540393Sbostic * 52640393Sbostic * Side Effects: 52740393Sbostic * See Dir_ClearPath 52840393Sbostic * 52940393Sbostic *----------------------------------------------------------------------- 53040393Sbostic */ 53140393Sbostic static int 53240393Sbostic ParseClearPath(path) 53340393Sbostic Lst path; 53440393Sbostic { 53540393Sbostic Dir_ClearPath(path); 53640393Sbostic return(0); 53740393Sbostic } 53840424Sbostic 53940393Sbostic /*- 54040393Sbostic *--------------------------------------------------------------------- 54140393Sbostic * ParseDoDependency -- 54240393Sbostic * Parse the dependency line in line. 54340393Sbostic * 54440393Sbostic * Results: 54540393Sbostic * None 54640393Sbostic * 54740393Sbostic * Side Effects: 54840393Sbostic * The nodes of the sources are linked as children to the nodes of the 54940393Sbostic * targets. Some nodes may be created. 55040393Sbostic * 55140393Sbostic * We parse a dependency line by first extracting words from the line and 55240393Sbostic * finding nodes in the list of all targets with that name. This is done 55340393Sbostic * until a character is encountered which is an operator character. Currently 55440393Sbostic * these are only ! and :. At this point the operator is parsed and the 55540393Sbostic * pointer into the line advanced until the first source is encountered. 55640393Sbostic * The parsed operator is applied to each node in the 'targets' list, 55740393Sbostic * which is where the nodes found for the targets are kept, by means of 55840393Sbostic * the ParseDoOp function. 55940393Sbostic * The sources are read in much the same way as the targets were except 56040393Sbostic * that now they are expanded using the wildcarding scheme of the C-Shell 56140393Sbostic * and all instances of the resulting words in the list of all targets 56240393Sbostic * are found. Each of the resulting nodes is then linked to each of the 56340393Sbostic * targets as one of its children. 56440393Sbostic * Certain targets are handled specially. These are the ones detailed 56540393Sbostic * by the specType variable. 56640393Sbostic * The storing of transformation rules is also taken care of here. 56740393Sbostic * A target is recognized as a transformation rule by calling 56840393Sbostic * Suff_IsTransform. If it is a transformation rule, its node is gotten 56940393Sbostic * from the suffix module via Suff_AddTransform rather than the standard 57040393Sbostic * Targ_FindNode in the target module. 57140393Sbostic *--------------------------------------------------------------------- 57240393Sbostic */ 57340393Sbostic static void 57440393Sbostic ParseDoDependency (line) 57540393Sbostic char *line; /* the line to parse */ 57640393Sbostic { 57740393Sbostic register char *cp; /* our current position */ 57840393Sbostic register GNode *gn; /* a general purpose temporary node */ 57940393Sbostic register int op; /* the operator on the line */ 58040393Sbostic char savec; /* a place to save a character */ 58140393Sbostic Lst paths; /* List of search paths to alter when parsing 58240393Sbostic * a list of .PATH targets */ 58340393Sbostic int tOp; /* operator from special target */ 58440393Sbostic Lst sources; /* list of source names after expansion */ 58540393Sbostic Lst curTargs; /* list of target names to be found and added 58640393Sbostic * to the targets list */ 58740393Sbostic 58840393Sbostic tOp = 0; 58940393Sbostic 59040393Sbostic specType = Not; 59140393Sbostic paths = (Lst)NULL; 59240393Sbostic 59340393Sbostic curTargs = Lst_Init(FALSE); 59440393Sbostic 59540393Sbostic do { 59640393Sbostic for (cp = line; 59740393Sbostic *cp && !isspace (*cp) && 59840393Sbostic (*cp != '!') && (*cp != ':') && (*cp != '('); 59940393Sbostic cp++) 60040393Sbostic { 60140393Sbostic if (*cp == '$') { 60240393Sbostic /* 60340393Sbostic * Must be a dynamic source (would have been expanded 60440393Sbostic * otherwise), so call the Var module to parse the puppy 60540393Sbostic * so we can safely advance beyond it...There should be 60640393Sbostic * no errors in this, as they would have been discovered 60740393Sbostic * in the initial Var_Subst and we wouldn't be here. 60840393Sbostic */ 60940393Sbostic int length; 61040393Sbostic Boolean freeIt; 61140393Sbostic char *result; 61240393Sbostic 61340393Sbostic result=Var_Parse(cp, VAR_CMD, TRUE, &length, &freeIt); 61440393Sbostic 61540393Sbostic if (freeIt) { 61640393Sbostic free(result); 61740393Sbostic } 61840393Sbostic cp += length-1; 61940393Sbostic } 62040393Sbostic continue; 62140393Sbostic } 62240393Sbostic if (*cp == '(') { 62340393Sbostic /* 62440393Sbostic * Archives must be handled specially to make sure the OP_ARCHV 62540393Sbostic * flag is set in their 'type' field, for one thing, and because 62640393Sbostic * things like "archive(file1.o file2.o file3.o)" are permissible. 62740393Sbostic * Arch_ParseArchive will set 'line' to be the first non-blank 62840393Sbostic * after the archive-spec. It creates/finds nodes for the members 62940393Sbostic * and places them on the given list, returning SUCCESS if all 63040393Sbostic * went well and FAILURE if there was an error in the 63140393Sbostic * specification. On error, line should remain untouched. 63240393Sbostic */ 63340393Sbostic if (Arch_ParseArchive (&line, targets, VAR_CMD) != SUCCESS) { 63440393Sbostic Parse_Error (PARSE_FATAL, 63540393Sbostic "Error in archive specification: \"%s\"", line); 63640393Sbostic return; 63740393Sbostic } else { 63840393Sbostic continue; 63940393Sbostic } 64040393Sbostic } 64140393Sbostic savec = *cp; 64240393Sbostic 64340393Sbostic if (!*cp) { 64440393Sbostic /* 64540393Sbostic * Ending a dependency line without an operator is a Bozo 64640393Sbostic * no-no 64740393Sbostic */ 64840393Sbostic Parse_Error (PARSE_FATAL, "Need an operator"); 64940393Sbostic return; 65040393Sbostic } 65140393Sbostic *cp = '\0'; 65240393Sbostic /* 65340393Sbostic * Have a word in line. See if it's a special target and set 65440393Sbostic * specType to match it. 65540393Sbostic */ 65640393Sbostic if (*line == '.' && isupper (line[1])) { 65740393Sbostic /* 65840393Sbostic * See if the target is a special target that must have it 65940393Sbostic * or its sources handled specially. 66040393Sbostic */ 66140393Sbostic int keywd = ParseFindKeyword(line); 66240393Sbostic if (keywd != -1) { 66340393Sbostic if (specType == Path && parseKeywords[keywd].spec != Path) { 66440393Sbostic Parse_Error(PARSE_FATAL, "Mismatched special targets"); 66540393Sbostic return; 66640393Sbostic } 66740393Sbostic 66840393Sbostic specType = parseKeywords[keywd].spec; 66940393Sbostic tOp = parseKeywords[keywd].op; 67040393Sbostic 67140393Sbostic /* 67240393Sbostic * Certain special targets have special semantics: 67340393Sbostic * .PATH Have to set the dirSearchPath 67440393Sbostic * variable too 67540393Sbostic * .MAIN Its sources are only used if 67640393Sbostic * nothing has been specified to 67740393Sbostic * create. 67840393Sbostic * .DEFAULT Need to create a node to hang 67940393Sbostic * commands on, but we don't want 68040393Sbostic * it in the graph, nor do we want 68140393Sbostic * it to be the Main Target, so we 68240393Sbostic * create it, set OP_NOTMAIN and 68340393Sbostic * add it to the list, setting 68440393Sbostic * DEFAULT to the new node for 68540393Sbostic * later use. We claim the node is 68640393Sbostic * A transformation rule to make 68740393Sbostic * life easier later, when we'll 68840393Sbostic * use Make_HandleUse to actually 68940393Sbostic * apply the .DEFAULT commands. 69040393Sbostic * .BEGIN 69140393Sbostic * .END 69240393Sbostic * .INTERRUPT Are not to be considered the 69340393Sbostic * main target. 69440393Sbostic * .NOTPARALLEL Make only one target at a time. 69540393Sbostic * .SINGLESHELL Create a shell for each command. 69640393Sbostic * .ORDER Must set initial predecessor to NIL 69740393Sbostic */ 69840393Sbostic switch (specType) { 69940393Sbostic case Path: 70040393Sbostic if (paths == NULL) { 70140393Sbostic paths = Lst_Init(FALSE); 70240393Sbostic } 70340393Sbostic (void)Lst_AtEnd(paths, (ClientData)dirSearchPath); 70440393Sbostic break; 70540393Sbostic case Main: 70640393Sbostic if (!Lst_IsEmpty(create)) { 70740393Sbostic specType = Not; 70840393Sbostic } 70940393Sbostic break; 71040393Sbostic case Begin: 71140393Sbostic case End: 71240393Sbostic case Interrupt: 71340393Sbostic gn = Targ_FindNode(line, TARG_CREATE); 71440393Sbostic gn->type |= OP_NOTMAIN; 71540393Sbostic (void)Lst_AtEnd(targets, (ClientData)gn); 71640393Sbostic break; 71740393Sbostic case Default: 71840393Sbostic gn = Targ_NewGN(".DEFAULT"); 71940393Sbostic gn->type |= (OP_NOTMAIN|OP_TRANSFORM); 72040393Sbostic (void)Lst_AtEnd(targets, (ClientData)gn); 72140393Sbostic DEFAULT = gn; 72240393Sbostic break; 72340393Sbostic case NotParallel: 72440393Sbostic { 72540393Sbostic extern int maxJobs; 72640393Sbostic 72740393Sbostic maxJobs = 1; 72840393Sbostic break; 72940393Sbostic } 73040393Sbostic case SingleShell: 73140444Sbostic /* backwards = 1; */ 73240393Sbostic break; 73340393Sbostic case Order: 73440393Sbostic predecessor = NILGNODE; 73540393Sbostic break; 73640393Sbostic } 73740393Sbostic } else if (strncmp (line, ".PATH", 5) == 0) { 73840393Sbostic /* 73940393Sbostic * .PATH<suffix> has to be handled specially. 74040393Sbostic * Call on the suffix module to give us a path to 74140393Sbostic * modify. 74240393Sbostic */ 74340393Sbostic Lst path; 74440393Sbostic 74540393Sbostic specType = Path; 74640393Sbostic path = Suff_GetPath (&line[5]); 74740393Sbostic if (path == NILLST) { 74840393Sbostic Parse_Error (PARSE_FATAL, 74940393Sbostic "Suffix '%s' not defined (yet)", 75040393Sbostic &line[5]); 75140393Sbostic return; 75240393Sbostic } else { 75340393Sbostic if (paths == (Lst)NULL) { 75440393Sbostic paths = Lst_Init(FALSE); 75540393Sbostic } 75640393Sbostic (void)Lst_AtEnd(paths, (ClientData)path); 75740393Sbostic } 75840393Sbostic } 75940393Sbostic } 76040393Sbostic 76140393Sbostic /* 76240393Sbostic * Have word in line. Get or create its node and stick it at 76340393Sbostic * the end of the targets list 76440393Sbostic */ 76540393Sbostic if ((specType == Not) && (*line != '\0')) { 76640393Sbostic if (Dir_HasWildcards(line)) { 76740393Sbostic /* 76840393Sbostic * Targets are to be sought only in the current directory, 76940393Sbostic * so create an empty path for the thing. Note we need to 77040393Sbostic * use Dir_Destroy in the destruction of the path as the 77140393Sbostic * Dir module could have added a directory to the path... 77240393Sbostic */ 77340393Sbostic Lst emptyPath = Lst_Init(FALSE); 77440393Sbostic 77540393Sbostic Dir_Expand(line, emptyPath, curTargs); 77640393Sbostic 77740393Sbostic Lst_Destroy(emptyPath, Dir_Destroy); 77840393Sbostic } else { 77940393Sbostic /* 78040393Sbostic * No wildcards, but we want to avoid code duplication, 78140393Sbostic * so create a list with the word on it. 78240393Sbostic */ 78340393Sbostic (void)Lst_AtEnd(curTargs, (ClientData)line); 78440393Sbostic } 78540393Sbostic 78640393Sbostic while(!Lst_IsEmpty(curTargs)) { 78740393Sbostic char *targName = (char *)Lst_DeQueue(curTargs); 78840393Sbostic 78940393Sbostic if (!Suff_IsTransform (targName)) { 79040393Sbostic gn = Targ_FindNode (targName, TARG_CREATE); 79140393Sbostic } else { 79240393Sbostic gn = Suff_AddTransform (targName); 79340393Sbostic } 79440393Sbostic 79540393Sbostic (void)Lst_AtEnd (targets, (ClientData)gn); 79640393Sbostic } 79740393Sbostic } else if (specType == Path && *line != '.' && *line != '\0') { 79840393Sbostic Parse_Error(PARSE_WARNING, "Extra target (%s) ignored", line); 79940393Sbostic } 80040393Sbostic 80140393Sbostic *cp = savec; 80240393Sbostic /* 80340393Sbostic * If it is a special type and not .PATH, it's the only target we 80440393Sbostic * allow on this line... 80540393Sbostic */ 80640393Sbostic if (specType != Not && specType != Path) { 80740393Sbostic Boolean warn = FALSE; 80840393Sbostic 80940393Sbostic while ((*cp != '!') && (*cp != ':') && *cp) { 81040393Sbostic if (*cp != ' ' && *cp != '\t') { 81140393Sbostic warn = TRUE; 81240393Sbostic } 81340393Sbostic cp++; 81440393Sbostic } 81540393Sbostic if (warn) { 81640393Sbostic Parse_Error(PARSE_WARNING, "Extra target ignored"); 81740393Sbostic } 81840393Sbostic } else { 81940393Sbostic while (*cp && isspace (*cp)) { 82040393Sbostic cp++; 82140393Sbostic } 82240393Sbostic } 82340393Sbostic line = cp; 82440393Sbostic } while ((*line != '!') && (*line != ':') && *line); 82540393Sbostic 82640393Sbostic /* 82740393Sbostic * Don't need the list of target names anymore... 82840393Sbostic */ 82940393Sbostic Lst_Destroy(curTargs, NOFREE); 83040393Sbostic 83140393Sbostic if (!Lst_IsEmpty(targets)) { 83240393Sbostic switch(specType) { 83340393Sbostic default: 83440393Sbostic Parse_Error(PARSE_WARNING, "Special and mundane targets don't mix. Mundane ones ignored"); 83540393Sbostic break; 83640393Sbostic case Default: 83740393Sbostic case Begin: 83840393Sbostic case End: 83940393Sbostic case Interrupt: 84040393Sbostic /* 84140393Sbostic * These four create nodes on which to hang commands, so 84240393Sbostic * targets shouldn't be empty... 84340393Sbostic */ 84440393Sbostic case Not: 84540393Sbostic /* 84640393Sbostic * Nothing special here -- targets can be empty if it wants. 84740393Sbostic */ 84840393Sbostic break; 84940393Sbostic } 85040393Sbostic } 85140393Sbostic 85240393Sbostic /* 85340393Sbostic * Have now parsed all the target names. Must parse the operator next. The 85440393Sbostic * result is left in op . 85540393Sbostic */ 85640393Sbostic if (*cp == '!') { 85740393Sbostic op = OP_FORCE; 85840393Sbostic } else if (*cp == ':') { 85940393Sbostic if (cp[1] == ':') { 86040393Sbostic op = OP_DOUBLEDEP; 86140393Sbostic cp++; 86240393Sbostic } else { 86340393Sbostic op = OP_DEPENDS; 86440393Sbostic } 86540393Sbostic } else { 86640393Sbostic Parse_Error (PARSE_FATAL, "Missing dependency operator"); 86740393Sbostic return; 86840393Sbostic } 86940393Sbostic 87040393Sbostic cp++; /* Advance beyond operator */ 87140393Sbostic 87240393Sbostic Lst_ForEach (targets, ParseDoOp, (ClientData)op); 87340393Sbostic 87440393Sbostic /* 87540393Sbostic * Get to the first source 87640393Sbostic */ 87740393Sbostic while (*cp && isspace (*cp)) { 87840393Sbostic cp++; 87940393Sbostic } 88040393Sbostic line = cp; 88140393Sbostic 88240393Sbostic /* 88340393Sbostic * Several special targets take different actions if present with no 88440393Sbostic * sources: 88540393Sbostic * a .SUFFIXES line with no sources clears out all old suffixes 88640393Sbostic * a .PRECIOUS line makes all targets precious 88740393Sbostic * a .IGNORE line ignores errors for all targets 88840393Sbostic * a .SILENT line creates silence when making all targets 88940393Sbostic * a .PATH removes all directories from the search path(s). 89040393Sbostic */ 89140393Sbostic if (!*line) { 89240393Sbostic switch (specType) { 89340393Sbostic case Suffixes: 89440393Sbostic Suff_ClearSuffixes (); 89540393Sbostic break; 89640393Sbostic case Precious: 89740393Sbostic allPrecious = TRUE; 89840393Sbostic break; 89940393Sbostic case Ignore: 90040393Sbostic ignoreErrors = TRUE; 90140393Sbostic break; 90240393Sbostic case Silent: 90340393Sbostic beSilent = TRUE; 90440393Sbostic break; 90540393Sbostic case Path: 90640393Sbostic Lst_ForEach(paths, ParseClearPath, (ClientData)NULL); 90740393Sbostic break; 90840393Sbostic } 90940393Sbostic } else if (specType == MFlags) { 91040393Sbostic /* 91140393Sbostic * Call on functions in main.c to deal with these arguments and 91240393Sbostic * set the initial character to a null-character so the loop to 91340393Sbostic * get sources won't get anything 91440393Sbostic */ 91540393Sbostic Main_ParseArgLine (line); 91640393Sbostic *line = '\0'; 91740393Sbostic } else if (specType == Shell) { 91840393Sbostic if (Job_ParseShell (line) != SUCCESS) { 91940393Sbostic Parse_Error (PARSE_FATAL, "improper shell specification"); 92040393Sbostic return; 92140393Sbostic } 92240393Sbostic *line = '\0'; 92340393Sbostic } else if ((specType == NotParallel) || (specType == SingleShell)) { 92440393Sbostic *line = '\0'; 92540393Sbostic } 92640393Sbostic 92740393Sbostic /* 92840393Sbostic * NOW GO FOR THE SOURCES 92940393Sbostic */ 93040393Sbostic if ((specType == Suffixes) || (specType == Path) || 93140393Sbostic (specType == Includes) || (specType == Libs) || 93240439Sbostic (specType == Null)) 93340393Sbostic { 93440393Sbostic while (*line) { 93540393Sbostic /* 93640393Sbostic * If the target was one that doesn't take files as its sources 93740393Sbostic * but takes something like suffixes, we take each 93840393Sbostic * space-separated word on the line as a something and deal 93940393Sbostic * with it accordingly. 94040393Sbostic * 94140393Sbostic * If the target was .SUFFIXES, we take each source as a 94240393Sbostic * suffix and add it to the list of suffixes maintained by the 94340393Sbostic * Suff module. 94440393Sbostic * 94540393Sbostic * If the target was a .PATH, we add the source as a directory 94640393Sbostic * to search on the search path. 94740393Sbostic * 94840393Sbostic * If it was .INCLUDES, the source is taken to be the suffix of 94940393Sbostic * files which will be #included and whose search path should 95040393Sbostic * be present in the .INCLUDES variable. 95140393Sbostic * 95240393Sbostic * If it was .LIBS, the source is taken to be the suffix of 95340393Sbostic * files which are considered libraries and whose search path 95440393Sbostic * should be present in the .LIBS variable. 95540393Sbostic * 95640393Sbostic * If it was .NULL, the source is the suffix to use when a file 95740393Sbostic * has no valid suffix. 95840393Sbostic */ 95940393Sbostic char savec; 96040393Sbostic while (*cp && !isspace (*cp)) { 96140393Sbostic cp++; 96240393Sbostic } 96340393Sbostic savec = *cp; 96440393Sbostic *cp = '\0'; 96540393Sbostic switch (specType) { 96640393Sbostic case Suffixes: 96740393Sbostic Suff_AddSuffix (line); 96840393Sbostic break; 96940393Sbostic case Path: 97040393Sbostic Lst_ForEach(paths, ParseAddDir, (ClientData)line); 97140393Sbostic break; 97240393Sbostic case Includes: 97340393Sbostic Suff_AddInclude (line); 97440393Sbostic break; 97540393Sbostic case Libs: 97640393Sbostic Suff_AddLib (line); 97740393Sbostic break; 97840393Sbostic case Null: 97940393Sbostic Suff_SetNull (line); 98040393Sbostic break; 98140393Sbostic } 98240393Sbostic *cp = savec; 98340393Sbostic if (savec != '\0') { 98440393Sbostic cp++; 98540393Sbostic } 98640393Sbostic while (*cp && isspace (*cp)) { 98740393Sbostic cp++; 98840393Sbostic } 98940393Sbostic line = cp; 99040393Sbostic } 99140393Sbostic if (paths) { 99240393Sbostic Lst_Destroy(paths, NOFREE); 99340393Sbostic } 99440393Sbostic } else { 99540393Sbostic while (*line) { 99640393Sbostic /* 99740393Sbostic * The targets take real sources, so we must beware of archive 99840393Sbostic * specifications (i.e. things with left parentheses in them) 99940393Sbostic * and handle them accordingly. 100040393Sbostic */ 100140393Sbostic while (*cp && !isspace (*cp)) { 100240393Sbostic if ((*cp == '(') && (cp > line) && (cp[-1] != '$')) { 100340393Sbostic /* 100440393Sbostic * Only stop for a left parenthesis if it isn't at the 100540393Sbostic * start of a word (that'll be for variable changes 100640393Sbostic * later) and isn't preceded by a dollar sign (a dynamic 100740393Sbostic * source). 100840393Sbostic */ 100940393Sbostic break; 101040393Sbostic } else { 101140393Sbostic cp++; 101240393Sbostic } 101340393Sbostic } 101440393Sbostic 101540393Sbostic if (*cp == '(') { 101640393Sbostic GNode *gn; 101740393Sbostic 101840393Sbostic sources = Lst_Init (FALSE); 101940393Sbostic if (Arch_ParseArchive (&line, sources, VAR_CMD) != SUCCESS) { 102040393Sbostic Parse_Error (PARSE_FATAL, 102140393Sbostic "Error in source archive spec \"%s\"", line); 102240393Sbostic return; 102340393Sbostic } 102440393Sbostic 102540393Sbostic while (!Lst_IsEmpty (sources)) { 102640393Sbostic gn = (GNode *) Lst_DeQueue (sources); 102740393Sbostic ParseDoSrc (tOp, gn->name); 102840393Sbostic } 102940393Sbostic Lst_Destroy (sources, NOFREE); 103040393Sbostic cp = line; 103140393Sbostic } else { 103240393Sbostic if (*cp) { 103340393Sbostic *cp = '\0'; 103440393Sbostic cp += 1; 103540393Sbostic } 103640393Sbostic 103740393Sbostic ParseDoSrc (tOp, line); 103840393Sbostic } 103940393Sbostic while (*cp && isspace (*cp)) { 104040393Sbostic cp++; 104140393Sbostic } 104240393Sbostic line = cp; 104340393Sbostic } 104440393Sbostic } 104540393Sbostic 104640393Sbostic if (mainNode == NILGNODE) { 104740393Sbostic /* 104840393Sbostic * If we have yet to decide on a main target to make, in the 104940393Sbostic * absence of any user input, we want the first target on 105040393Sbostic * the first dependency line that is actually a real target 105140393Sbostic * (i.e. isn't a .USE or .EXEC rule) to be made. 105240393Sbostic */ 105340393Sbostic Lst_ForEach (targets, ParseFindMain, (ClientData)0); 105440393Sbostic } 105540393Sbostic 105640393Sbostic } 105740424Sbostic 105840393Sbostic /*- 105940393Sbostic *--------------------------------------------------------------------- 106040393Sbostic * Parse_IsVar -- 106140393Sbostic * Return TRUE if the passed line is a variable assignment. A variable 106240393Sbostic * assignment consists of a single word followed by optional whitespace 106340393Sbostic * followed by either a += or an = operator. 106440393Sbostic * This function is used both by the Parse_File function and main when 106540393Sbostic * parsing the command-line arguments. 106640393Sbostic * 106740393Sbostic * Results: 106840393Sbostic * TRUE if it is. FALSE if it ain't 106940393Sbostic * 107040393Sbostic * Side Effects: 107140393Sbostic * none 107240393Sbostic *--------------------------------------------------------------------- 107340393Sbostic */ 107440393Sbostic Boolean 107540393Sbostic Parse_IsVar (line) 107640393Sbostic register char *line; /* the line to check */ 107740393Sbostic { 107840393Sbostic register Boolean wasSpace = FALSE; /* set TRUE if found a space */ 107940393Sbostic register Boolean haveName = FALSE; /* Set TRUE if have a variable name */ 108040393Sbostic 108140393Sbostic /* 108240393Sbostic * Skip to variable name 108340393Sbostic */ 108440393Sbostic while ((*line == ' ') || (*line == '\t')) { 108540393Sbostic line++; 108640393Sbostic } 108740393Sbostic 108840393Sbostic while (*line != '=') { 108940393Sbostic if (*line == '\0') { 109040393Sbostic /* 109140393Sbostic * end-of-line -- can't be a variable assignment. 109240393Sbostic */ 109340393Sbostic return (FALSE); 109440393Sbostic } else if ((*line == ' ') || (*line == '\t')) { 109540393Sbostic /* 109640393Sbostic * there can be as much white space as desired so long as there is 109740393Sbostic * only one word before the operator 109840393Sbostic */ 109940393Sbostic wasSpace = TRUE; 110040393Sbostic } else if (wasSpace && haveName) { 110140393Sbostic /* 110240393Sbostic * Stop when an = operator is found. 110340393Sbostic */ 110440393Sbostic if ((*line == '+') || (*line == ':') || (*line == '?') || 110540393Sbostic (*line == '!')) { 110640393Sbostic break; 110740393Sbostic } 110840393Sbostic 110940393Sbostic /* 111040393Sbostic * This is the start of another word, so not assignment. 111140393Sbostic */ 111240393Sbostic return (FALSE); 111340393Sbostic } else { 111440393Sbostic haveName = TRUE; 111540393Sbostic wasSpace = FALSE; 111640393Sbostic } 111740393Sbostic line++; 111840393Sbostic } 111940393Sbostic 112040393Sbostic /* 112140393Sbostic * A final check: if we stopped on a +, ?, ! or :, the next character must 112240393Sbostic * be an = or it ain't a valid assignment 112340393Sbostic */ 112440393Sbostic if (((*line == '+') || 112540393Sbostic (*line == '?') || 112640393Sbostic (*line == ':') || 112740393Sbostic (*line == '!')) && 112840393Sbostic (line[1] != '=')) 112940393Sbostic { 113040393Sbostic return (FALSE); 113140393Sbostic } else { 113240393Sbostic return (haveName); 113340393Sbostic } 113440393Sbostic } 113540424Sbostic 113640393Sbostic /*- 113740393Sbostic *--------------------------------------------------------------------- 113840393Sbostic * Parse_DoVar -- 113940393Sbostic * Take the variable assignment in the passed line and do it in the 114040393Sbostic * global context. 114140393Sbostic * 114240393Sbostic * Note: There is a lexical ambiguity with assignment modifier characters 114340393Sbostic * in variable names. This routine interprets the character before the = 114440393Sbostic * as a modifier. Therefore, an assignment like 114540393Sbostic * C++=/usr/bin/CC 114640393Sbostic * is interpreted as "C+ +=" instead of "C++ =". 114740393Sbostic * 114840393Sbostic * Results: 114940393Sbostic * none 115040393Sbostic * 115140393Sbostic * Side Effects: 115240393Sbostic * the variable structure of the given variable name is altered in the 115340393Sbostic * global context. 115440393Sbostic *--------------------------------------------------------------------- 115540393Sbostic */ 115640393Sbostic void 115740393Sbostic Parse_DoVar (line, ctxt) 115840393Sbostic char *line; /* a line guaranteed to be a variable 115940393Sbostic * assignment. This reduces error checks */ 116040393Sbostic GNode *ctxt; /* Context in which to do the assignment */ 116140393Sbostic { 116240393Sbostic register char *cp; /* pointer into line */ 116340393Sbostic enum { 116440393Sbostic VAR_SUBST, VAR_APPEND, VAR_SHELL, VAR_NORMAL 116540393Sbostic } type; /* Type of assignment */ 116640393Sbostic char *opc; /* ptr to operator character to 116740393Sbostic * null-terminate the variable name */ 116840393Sbostic 116940393Sbostic /* 117040393Sbostic * Skip to variable name 117140393Sbostic */ 117240393Sbostic while ((*line == ' ') || (*line == '\t')) { 117340393Sbostic line++; 117440393Sbostic } 117540393Sbostic 117640393Sbostic /* 117740393Sbostic * Skip to operator character, nulling out whitespace as we go 117840393Sbostic */ 117940393Sbostic for (cp = line + 1; *cp != '='; cp++) { 118040393Sbostic if (isspace (*cp)) { 118140393Sbostic *cp = '\0'; 118240393Sbostic } 118340393Sbostic } 118440393Sbostic opc = cp-1; /* operator is the previous character */ 118540393Sbostic *cp++ = '\0'; /* nuke the = */ 118640393Sbostic 118740393Sbostic /* 118840393Sbostic * Check operator type 118940393Sbostic */ 119040393Sbostic switch (*opc) { 119140393Sbostic case '+': 119240393Sbostic type = VAR_APPEND; 119340393Sbostic *opc = '\0'; 119440393Sbostic break; 119540393Sbostic 119640393Sbostic case '?': 119740393Sbostic /* 119840393Sbostic * If the variable already has a value, we don't do anything. 119940393Sbostic */ 120040393Sbostic *opc = '\0'; 120140393Sbostic if (Var_Exists(line, ctxt)) { 120240393Sbostic return; 120340393Sbostic } else { 120440393Sbostic type = VAR_NORMAL; 120540393Sbostic } 120640393Sbostic break; 120740393Sbostic 120840393Sbostic case ':': 120940393Sbostic type = VAR_SUBST; 121040393Sbostic *opc = '\0'; 121140393Sbostic break; 121240393Sbostic 121340393Sbostic case '!': 121440393Sbostic type = VAR_SHELL; 121540393Sbostic *opc = '\0'; 121640393Sbostic break; 121740393Sbostic 121840393Sbostic default: 121940393Sbostic type = VAR_NORMAL; 122040393Sbostic break; 122140393Sbostic } 122240393Sbostic 122340393Sbostic while (isspace (*cp)) { 122440393Sbostic cp++; 122540393Sbostic } 122640393Sbostic 122740393Sbostic if (type == VAR_APPEND) { 122840393Sbostic Var_Append (line, cp, ctxt); 122940393Sbostic } else if (type == VAR_SUBST) { 123040393Sbostic /* 123140393Sbostic * Allow variables in the old value to be undefined, but leave their 123240393Sbostic * invocation alone -- this is done by forcing oldVars to be false. 123340393Sbostic * XXX: This can cause recursive variables, but that's not hard to do, 123440393Sbostic * and this allows someone to do something like 123540393Sbostic * 123640393Sbostic * CFLAGS = $(.INCLUDES) 123740393Sbostic * CFLAGS := -I.. $(CFLAGS) 123840393Sbostic * 123940393Sbostic * And not get an error. 124040393Sbostic */ 124140393Sbostic Boolean oldOldVars = oldVars; 124240393Sbostic 124340393Sbostic oldVars = FALSE; 124440393Sbostic cp = Var_Subst(cp, ctxt, FALSE); 124540393Sbostic oldVars = oldOldVars; 124640393Sbostic 124740393Sbostic Var_Set(line, cp, ctxt); 124840393Sbostic free(cp); 124940393Sbostic } else if (type == VAR_SHELL) { 125040393Sbostic char result[BUFSIZ]; /* Result of command */ 125140393Sbostic char *args[4]; /* Args for invoking the shell */ 125240393Sbostic int fds[2]; /* Pipe streams */ 125340393Sbostic int cpid; /* Child PID */ 125440393Sbostic int pid; /* PID from wait() */ 125540393Sbostic Boolean freeCmd; /* TRUE if the command needs to be freed, i.e. 125640393Sbostic * if any variable expansion was performed */ 125740393Sbostic 125840393Sbostic /* 125940393Sbostic * Set up arguments for shell 126040393Sbostic */ 126140393Sbostic args[0] = "sh"; 126240393Sbostic args[1] = "-c"; 126340393Sbostic if (index(cp, '$') != (char *)NULL) { 126440393Sbostic /* 126540393Sbostic * There's a dollar sign in the command, so perform variable 126640393Sbostic * expansion on the whole thing. The resulting string will need 126740393Sbostic * freeing when we're done, so set freeCmd to TRUE. 126840393Sbostic */ 126940393Sbostic args[2] = Var_Subst(cp, VAR_CMD, TRUE); 127040393Sbostic freeCmd = TRUE; 127140393Sbostic } else { 127240393Sbostic args[2] = cp; 127340393Sbostic freeCmd = FALSE; 127440393Sbostic } 127540393Sbostic args[3] = (char *)NULL; 127640393Sbostic 127740393Sbostic /* 127840393Sbostic * Open a pipe for fetching its output 127940393Sbostic */ 128040393Sbostic pipe(fds); 128140393Sbostic 128240393Sbostic /* 128340393Sbostic * Fork 128440393Sbostic */ 128540393Sbostic cpid = vfork(); 128640393Sbostic if (cpid == 0) { 128740393Sbostic /* 128840393Sbostic * Close input side of pipe 128940393Sbostic */ 129040393Sbostic close(fds[0]); 129140393Sbostic 129240393Sbostic /* 129340393Sbostic * Duplicate the output stream to the shell's output, then 129440393Sbostic * shut the extra thing down. Note we don't fetch the error 129540393Sbostic * stream...why not? Why? 129640393Sbostic */ 129740393Sbostic dup2(fds[1], 1); 129840393Sbostic close(fds[1]); 129940393Sbostic 130040393Sbostic execv("/bin/sh", args); 130140393Sbostic _exit(1); 130240393Sbostic } else if (cpid < 0) { 130340393Sbostic /* 130440393Sbostic * Couldn't fork -- tell the user and make the variable null 130540393Sbostic */ 130640393Sbostic Parse_Error(PARSE_WARNING, "Couldn't exec \"%s\"", cp); 130740393Sbostic Var_Set(line, "", ctxt); 130840393Sbostic } else { 130940393Sbostic int status; 131040393Sbostic int cc; 131140393Sbostic 131240393Sbostic /* 131340393Sbostic * No need for the writing half 131440393Sbostic */ 131540393Sbostic close(fds[1]); 131640393Sbostic 131740393Sbostic /* 131840393Sbostic * Wait for the process to exit. 131940393Sbostic * 132040393Sbostic * XXX: If the child writes more than a pipe's worth, we will 132140393Sbostic * deadlock. 132240393Sbostic */ 132340393Sbostic while(((pid = wait(&status)) != cpid) && (pid >= 0)) { 132440393Sbostic ; 132540393Sbostic } 132640393Sbostic 132740393Sbostic /* 132840393Sbostic * Read all the characters the child wrote. 132940393Sbostic */ 133040393Sbostic cc = read(fds[0], result, sizeof(result)); 133140393Sbostic 133240393Sbostic if (cc < 0) { 133340393Sbostic /* 133440393Sbostic * Couldn't read the child's output -- tell the user and 133540393Sbostic * set the variable to null 133640393Sbostic */ 133740393Sbostic Parse_Error(PARSE_WARNING, "Couldn't read shell's output"); 133840393Sbostic cc = 0; 133940393Sbostic } 134040393Sbostic 134140393Sbostic if (status) { 134240393Sbostic /* 134340393Sbostic * Child returned an error -- tell the user but still use 134440393Sbostic * the result. 134540393Sbostic */ 134640393Sbostic Parse_Error(PARSE_WARNING, "\"%s\" returned non-zero", cp); 134740393Sbostic } 134840393Sbostic /* 134940393Sbostic * Null-terminate the result, convert newlines to spaces and 135040393Sbostic * install it in the variable. 135140393Sbostic */ 135240393Sbostic result[cc] = '\0'; 135340393Sbostic cp = &result[cc] - 1; 135440393Sbostic 135540393Sbostic if (*cp == '\n') { 135640393Sbostic /* 135740393Sbostic * A final newline is just stripped 135840393Sbostic */ 135940393Sbostic *cp-- = '\0'; 136040393Sbostic } 136140393Sbostic while (cp >= result) { 136240393Sbostic if (*cp == '\n') { 136340393Sbostic *cp = ' '; 136440393Sbostic } 136540393Sbostic cp--; 136640393Sbostic } 136740393Sbostic Var_Set(line, result, ctxt); 136840393Sbostic 136940393Sbostic /* 137040393Sbostic * Close the input side of the pipe. 137140393Sbostic */ 137240393Sbostic close(fds[0]); 137340393Sbostic } 137440393Sbostic if (freeCmd) { 137540393Sbostic free(args[2]); 137640393Sbostic } 137740393Sbostic } else { 137840393Sbostic /* 137940393Sbostic * Normal assignment -- just do it. 138040393Sbostic */ 138140393Sbostic Var_Set (line, cp, ctxt); 138240393Sbostic } 138340393Sbostic } 138440424Sbostic 138540393Sbostic /*- 138640393Sbostic * ParseAddCmd -- 138740393Sbostic * Lst_ForEach function to add a command line to all targets 138840393Sbostic * 138940393Sbostic * Results: 139040393Sbostic * Always 0 139140393Sbostic * 139240393Sbostic * Side Effects: 139340393Sbostic * A new element is added to the commands list of the node. 139440393Sbostic */ 139540476Sbostic static 139640476Sbostic ParseAddCmd(gn, cmd) 139740476Sbostic GNode *gn; /* the node to which the command is to be added */ 139840476Sbostic char *cmd; /* the command to add */ 139940393Sbostic { 140040476Sbostic /* if target already supplied, ignore commands */ 140140476Sbostic if (!(gn->type & OP_HAS_COMMANDS)) 140240476Sbostic (void)Lst_AtEnd(gn->commands, (ClientData)cmd); 140340476Sbostic return(0); 140440393Sbostic } 140540424Sbostic 140640393Sbostic /*- 140740393Sbostic *----------------------------------------------------------------------- 140840393Sbostic * ParseHasCommands -- 140940393Sbostic * Callback procedure for Parse_File when destroying the list of 141040393Sbostic * targets on the last dependency line. Marks a target as already 141140393Sbostic * having commands if it does, to keep from having shell commands 141240393Sbostic * on multiple dependency lines. 141340393Sbostic * 141440393Sbostic * Results: 141540393Sbostic * Always 0. 141640393Sbostic * 141740393Sbostic * Side Effects: 141840393Sbostic * OP_HAS_COMMANDS may be set for the target. 141940393Sbostic * 142040393Sbostic *----------------------------------------------------------------------- 142140393Sbostic */ 142240393Sbostic static int 142340393Sbostic ParseHasCommands(gn) 142440393Sbostic GNode *gn; /* Node to examine */ 142540393Sbostic { 142640393Sbostic if (!Lst_IsEmpty(gn->commands)) { 142740393Sbostic gn->type |= OP_HAS_COMMANDS; 142840393Sbostic } 142940393Sbostic return(0); 143040393Sbostic } 143140424Sbostic 143240393Sbostic /*- 143340393Sbostic *----------------------------------------------------------------------- 143440393Sbostic * Parse_AddIncludeDir -- 143540393Sbostic * Add a directory to the path searched for included makefiles 143640393Sbostic * bracketed by double-quotes. Used by functions in main.c 143740393Sbostic * 143840393Sbostic * Results: 143940393Sbostic * None. 144040393Sbostic * 144140393Sbostic * Side Effects: 144240393Sbostic * The directory is appended to the list. 144340393Sbostic * 144440393Sbostic *----------------------------------------------------------------------- 144540393Sbostic */ 144640393Sbostic void 144740393Sbostic Parse_AddIncludeDir (dir) 144840393Sbostic char *dir; /* The name of the directory to add */ 144940393Sbostic { 145040393Sbostic Dir_AddDir (parseIncPath, dir); 145140393Sbostic } 145240424Sbostic 145340393Sbostic /*- 145440393Sbostic *--------------------------------------------------------------------- 145540393Sbostic * ParseDoInclude -- 145640393Sbostic * Push to another file. 145740393Sbostic * 145840393Sbostic * The input is the line minus the #include. A file spec is a string 145940393Sbostic * enclosed in <> or "". The former is looked for only in sysIncPath. 146040393Sbostic * The latter in . and the directories specified by -I command line 146140393Sbostic * options 146240393Sbostic * 146340393Sbostic * Results: 146440393Sbostic * None 146540393Sbostic * 146640393Sbostic * Side Effects: 146740393Sbostic * A structure is added to the includes Lst and readProc, lineno, 146840393Sbostic * fname and curFILE are altered for the new file 146940393Sbostic *--------------------------------------------------------------------- 147040393Sbostic */ 147140393Sbostic static void 147240393Sbostic ParseDoInclude (file) 147340393Sbostic char *file; /* file specification */ 147440393Sbostic { 147540393Sbostic char *fullname; /* full pathname of file */ 147640393Sbostic IFile *oldFile; /* state associated with current file */ 147740393Sbostic Lst path; /* the path to use to find the file */ 147840393Sbostic char endc; /* the character which ends the file spec */ 147940393Sbostic char *cp; /* current position in file spec */ 148040393Sbostic Boolean isSystem; /* TRUE if makefile is a system makefile */ 148140393Sbostic 148240393Sbostic /* 148340393Sbostic * Skip to delimiter character so we know where to look 148440393Sbostic */ 148540393Sbostic while ((*file == ' ') || (*file == '\t')) { 148640393Sbostic file++; 148740393Sbostic } 148840393Sbostic 148940393Sbostic if ((*file != '"') && (*file != '<')) { 149040393Sbostic /* 149140393Sbostic * XXX: Should give some sort of error message, I suppose, but because 149240393Sbostic * # is used for both comments and directives, we can't be sure if 149340393Sbostic * the thing might not just be a comment, so we just return... 149440393Sbostic */ 149540393Sbostic return; 149640393Sbostic } 149740393Sbostic 149840393Sbostic /* 149940393Sbostic * Set the search path on which to find the include file based on the 150040393Sbostic * characters which bracket its name. Angle-brackets imply it's 150140393Sbostic * a system Makefile while double-quotes imply it's a user makefile 150240393Sbostic */ 150340393Sbostic if (*file == '<') { 150440393Sbostic isSystem = TRUE; 150540393Sbostic endc = '>'; 150640393Sbostic } else { 150740393Sbostic isSystem = FALSE; 150840393Sbostic endc = '"'; 150940393Sbostic } 151040393Sbostic 151140393Sbostic /* 151240393Sbostic * Skip to matching delimiter 151340393Sbostic */ 151440393Sbostic for (cp = ++file; *cp && *cp != endc; cp++) { 151540393Sbostic continue; 151640393Sbostic } 151740393Sbostic 151840393Sbostic if (*cp != endc) { 151940393Sbostic Parse_Error (PARSE_FATAL, 152040435Sbostic "Unclosed .include filename. '%c' expected", endc); 152140393Sbostic return; 152240393Sbostic } 152340393Sbostic *cp = '\0'; 152440393Sbostic 152540393Sbostic /* 152640393Sbostic * Substitute for any variables in the file name before trying to 152740393Sbostic * find the thing. 152840393Sbostic */ 152940393Sbostic file = Var_Subst (file, VAR_CMD, FALSE); 153040393Sbostic 153140393Sbostic /* 153240393Sbostic * Now we know the file's name and its search path, we attempt to 153340393Sbostic * find the durn thing. A return of NULL indicates the file don't 153440393Sbostic * exist. 153540393Sbostic */ 153640393Sbostic if (!isSystem) { 153740393Sbostic /* 153840393Sbostic * Include files contained in double-quotes are first searched for 153940393Sbostic * relative to the including file's location. We don't want to 154040393Sbostic * cd there, of course, so we just tack on the old file's 154140393Sbostic * leading path components and call Dir_FindFile to see if 154240393Sbostic * we can locate the beast. 154340393Sbostic */ 154440393Sbostic char *prefEnd; 154540393Sbostic 154640393Sbostic prefEnd = rindex (fname, '/'); 154740393Sbostic if (prefEnd != (char *)NULL) { 154840393Sbostic char *newName; 154940393Sbostic 155040393Sbostic *prefEnd = '\0'; 1551*40534Sbostic newName = str_concat (fname, file, STR_ADDSLASH); 155240393Sbostic fullname = Dir_FindFile (newName, parseIncPath); 155340393Sbostic if (fullname == (char *)NULL) { 155440393Sbostic fullname = Dir_FindFile(newName, dirSearchPath); 155540393Sbostic } 155640393Sbostic free (newName); 155740393Sbostic *prefEnd = '/'; 155840393Sbostic } else { 155940393Sbostic fullname = (char *)NULL; 156040393Sbostic } 156140393Sbostic } else { 156240393Sbostic fullname = (char *)NULL; 156340393Sbostic } 156440393Sbostic 156540393Sbostic if (fullname == (char *)NULL) { 156640393Sbostic /* 156740393Sbostic * System makefile or makefile wasn't found in same directory as 156840393Sbostic * included makefile. Search for it first on the -I search path, 156940393Sbostic * then on the .PATH search path, if not found in a -I directory. 157040393Sbostic * XXX: Suffix specific? 157140393Sbostic */ 157240393Sbostic fullname = Dir_FindFile (file, parseIncPath); 157340393Sbostic if (fullname == (char *)NULL) { 157440393Sbostic fullname = Dir_FindFile(file, dirSearchPath); 157540393Sbostic } 157640393Sbostic } 157740393Sbostic 157840393Sbostic if (fullname == (char *)NULL) { 157940393Sbostic /* 158040393Sbostic * Still haven't found the makefile. Look for it on the system 158140393Sbostic * path as a last resort. 158240393Sbostic */ 158340393Sbostic fullname = Dir_FindFile(file, sysIncPath); 158440393Sbostic } 158540393Sbostic 158640393Sbostic if (fullname == (char *) NULL) { 158740393Sbostic *cp = endc; 158840393Sbostic Parse_Error (PARSE_FATAL, "Could not find %s", file); 158940393Sbostic return; 159040393Sbostic } 159140393Sbostic 159240393Sbostic /* 159340393Sbostic * Once we find the absolute path to the file, we get to save all the 159440393Sbostic * state from the current file before we can start reading this 159540393Sbostic * include file. The state is stored in an IFile structure which 159640393Sbostic * is placed on a list with other IFile structures. The list makes 159740393Sbostic * a very nice stack to track how we got here... 159840393Sbostic */ 1599*40534Sbostic oldFile = (IFile *) emalloc (sizeof (IFile)); 160040393Sbostic oldFile->fname = fname; 160140393Sbostic 160240393Sbostic oldFile->F = curFILE; 160340393Sbostic oldFile->lineno = lineno; 160440393Sbostic 160540393Sbostic (void) Lst_AtFront (includes, (ClientData)oldFile); 160640393Sbostic 160740393Sbostic /* 160840393Sbostic * Once the previous state has been saved, we can get down to reading 160940393Sbostic * the new file. We set up the name of the file to be the absolute 161040393Sbostic * name of the include file so error messages refer to the right 161140393Sbostic * place. Naturally enough, we start reading at line number 0. 161240393Sbostic */ 161340393Sbostic fname = fullname; 161440393Sbostic lineno = 0; 161540393Sbostic 161640393Sbostic curFILE = fopen (fullname, "r"); 161740393Sbostic if (curFILE == (FILE * ) NULL) { 161840393Sbostic Parse_Error (PARSE_FATAL, "Cannot open %s", fullname); 161940393Sbostic /* 162040393Sbostic * Pop to previous file 162140393Sbostic */ 162240393Sbostic (void) ParseEOF(); 162340393Sbostic } 162440393Sbostic } 162540424Sbostic 162640393Sbostic /*- 162740393Sbostic *--------------------------------------------------------------------- 162840393Sbostic * ParseEOF -- 162940393Sbostic * Called when EOF is reached in the current file. If we were reading 163040393Sbostic * an include file, the includes stack is popped and things set up 163140393Sbostic * to go back to reading the previous file at the previous location. 163240393Sbostic * 163340393Sbostic * Results: 163440393Sbostic * CONTINUE if there's more to do. DONE if not. 163540393Sbostic * 163640393Sbostic * Side Effects: 163740393Sbostic * The old curFILE, is closed. The includes list is shortened. 163840393Sbostic * lineno, curFILE, and fname are changed if CONTINUE is returned. 163940393Sbostic *--------------------------------------------------------------------- 164040393Sbostic */ 164140393Sbostic static int 164240393Sbostic ParseEOF () 164340393Sbostic { 164440393Sbostic IFile *ifile; /* the state on the top of the includes stack */ 164540393Sbostic 164640393Sbostic if (Lst_IsEmpty (includes)) { 164740393Sbostic return (DONE); 164840393Sbostic } 164940393Sbostic 165040393Sbostic ifile = (IFile *) Lst_DeQueue (includes); 165140393Sbostic free (fname); 165240393Sbostic fname = ifile->fname; 165340393Sbostic lineno = ifile->lineno; 165440393Sbostic fclose (curFILE); 165540393Sbostic curFILE = ifile->F; 165640393Sbostic free ((Address)ifile); 165740393Sbostic return (CONTINUE); 165840393Sbostic } 165940424Sbostic 166040393Sbostic /*- 166140393Sbostic *--------------------------------------------------------------------- 166240393Sbostic * ParseReadc -- 166340393Sbostic * Read a character from the current file and update the line number 166440393Sbostic * counter as necessary 166540393Sbostic * 166640393Sbostic * Results: 166740393Sbostic * The character that was read 166840393Sbostic * 166940393Sbostic * Side Effects: 167040393Sbostic * The lineno counter is incremented if the character is a newline 167140393Sbostic *--------------------------------------------------------------------- 167240393Sbostic */ 167340393Sbostic #ifdef notdef 167440393Sbostic static int parseReadChar; 167540393Sbostic 167640393Sbostic #define ParseReadc() (((parseReadChar = getc(curFILE)) == '\n') ? \ 167740393Sbostic (lineno++, '\n') : parseReadChar) 167840393Sbostic #else 167940393Sbostic #define ParseReadc() (getc(curFILE)) 168040393Sbostic #endif /* notdef */ 168140393Sbostic 168240424Sbostic 168340393Sbostic /*- 168440393Sbostic *--------------------------------------------------------------------- 168540393Sbostic * ParseReadLine -- 168640393Sbostic * Read an entire line from the input file. Called only by Parse_File. 168740393Sbostic * To facilitate escaped newlines and what have you, a character is 168840393Sbostic * buffered in 'lastc', which is '\0' when no characters have been 168940393Sbostic * read. When we break out of the loop, c holds the terminating 169040393Sbostic * character and lastc holds a character that should be added to 169140393Sbostic * the line (unless we don't read anything but a terminator). 169240393Sbostic * 169340393Sbostic * Results: 169440393Sbostic * A line w/o its newline 169540393Sbostic * 169640393Sbostic * Side Effects: 169740393Sbostic * Only those associated with reading a character 169840393Sbostic *--------------------------------------------------------------------- 169940393Sbostic */ 170040393Sbostic static char * 170140393Sbostic ParseReadLine () 170240393Sbostic { 170340393Sbostic Buffer buf; /* Buffer for current line */ 170440393Sbostic register int c; /* the current character */ 170540393Sbostic register int lastc; /* The most-recent character */ 170640393Sbostic Boolean semiNL; /* treat semi-colons as newlines */ 170740393Sbostic Boolean ignDepOp; /* TRUE if should ignore dependency operators 170840393Sbostic * for the purposes of setting semiNL */ 170940393Sbostic Boolean ignComment; /* TRUE if should ignore comments (in a 171040393Sbostic * shell command */ 171140393Sbostic char *line; /* Result */ 171240393Sbostic int lineLength; /* Length of result */ 171340393Sbostic 171440393Sbostic semiNL = FALSE; 171540393Sbostic ignDepOp = FALSE; 171640393Sbostic ignComment = FALSE; 171740393Sbostic 171840393Sbostic /* 171940393Sbostic * Handle special-characters at the beginning of the line. Either a 172040393Sbostic * leading tab (shell command) or pound-sign (possible conditional) 172140393Sbostic * forces us to ignore comments and dependency operators and treat 172240393Sbostic * semi-colons as semi-colons (by leaving semiNL FALSE). This also 172340393Sbostic * discards completely blank lines. 172440393Sbostic */ 172540393Sbostic while(1) { 172640393Sbostic c = ParseReadc(); 172740393Sbostic 172840435Sbostic if ((c == '\t') || (c == '.')) { 172940393Sbostic ignComment = ignDepOp = TRUE; 173040393Sbostic break; 173140393Sbostic } else if (c == '\n') { 173240393Sbostic lineno++; 173340393Sbostic } else { 173440393Sbostic /* 173540393Sbostic * Anything else breaks out without doing anything 173640393Sbostic */ 173740393Sbostic break; 173840393Sbostic } 173940393Sbostic } 174040393Sbostic 174140393Sbostic if (c != EOF) { 174240393Sbostic lastc = c; 174340393Sbostic buf = Buf_Init(BSIZE); 174440393Sbostic 174540393Sbostic while (((c = ParseReadc ()) != '\n' || (lastc == '\\')) && 174640393Sbostic (c != EOF)) 174740393Sbostic { 174840393Sbostic test_char: 174940393Sbostic switch(c) { 175040393Sbostic case '\n': 175140393Sbostic /* 175240393Sbostic * Escaped newline: read characters until a non-space or an 175340393Sbostic * unescaped newline and replace them all by a single space. 175440393Sbostic * This is done by storing the space over the backslash and 175540393Sbostic * dropping through with the next nonspace. If it is a 175640393Sbostic * semi-colon and semiNL is TRUE, it will be recognized as a 175740393Sbostic * newline in the code below this... 175840393Sbostic */ 175940393Sbostic lineno++; 176040393Sbostic lastc = ' '; 176140393Sbostic while ((c = ParseReadc ()) == ' ' || c == '\t') { 176240393Sbostic continue; 176340393Sbostic } 176440393Sbostic if (c == EOF || c == '\n') { 176540393Sbostic goto line_read; 176640393Sbostic } else { 176740393Sbostic /* 176840393Sbostic * Check for comments, semiNL's, etc. -- easier than 176940393Sbostic * ungetc(c, curFILE); continue; 177040393Sbostic */ 177140393Sbostic goto test_char; 177240393Sbostic } 177340393Sbostic break; 177440393Sbostic case ';': 177540393Sbostic /* 177640393Sbostic * Semi-colon: Need to see if it should be interpreted as a 177740393Sbostic * newline 177840393Sbostic */ 177940393Sbostic if (semiNL) { 178040393Sbostic /* 178140393Sbostic * To make sure the command that may be following this 178240393Sbostic * semi-colon begins with a tab, we push one back into the 178340393Sbostic * input stream. This will overwrite the semi-colon in the 178440393Sbostic * buffer. If there is no command following, this does no 178540393Sbostic * harm, since the newline remains in the buffer and the 178640393Sbostic * whole line is ignored. 178740393Sbostic */ 178840393Sbostic ungetc('\t', curFILE); 178940393Sbostic goto line_read; 179040393Sbostic } 179140393Sbostic break; 179240393Sbostic case '=': 179340393Sbostic if (!semiNL) { 179440393Sbostic /* 179540393Sbostic * Haven't seen a dependency operator before this, so this 179640393Sbostic * must be a variable assignment -- don't pay attention to 179740393Sbostic * dependency operators after this. 179840393Sbostic */ 179940393Sbostic ignDepOp = TRUE; 180040393Sbostic } else if (lastc == ':' || lastc == '!') { 180140393Sbostic /* 180240393Sbostic * Well, we've seen a dependency operator already, but it 180340393Sbostic * was the previous character, so this is really just an 180440393Sbostic * expanded variable assignment. Revert semi-colons to 180540393Sbostic * being just semi-colons again and ignore any more 180640393Sbostic * dependency operators. 180740393Sbostic * 180840393Sbostic * XXX: Note that a line like "foo : a:=b" will blow up, 180940393Sbostic * but who'd write a line like that anyway? 181040393Sbostic */ 181140393Sbostic ignDepOp = TRUE; semiNL = FALSE; 181240393Sbostic } 181340393Sbostic break; 181440393Sbostic case '#': 181540393Sbostic if (!ignComment) { 181640393Sbostic /* 181740393Sbostic * If the character is a hash mark and it isn't escaped 181840393Sbostic * (or we're being compatible), the thing is a comment. 181940393Sbostic * Skip to the end of the line. 182040393Sbostic */ 182140393Sbostic do { 182240393Sbostic c = ParseReadc(); 182340393Sbostic } while ((c != '\n') && (c != EOF)); 182440393Sbostic goto line_read; 182540393Sbostic } 182640393Sbostic break; 182740393Sbostic case ':': 182840393Sbostic case '!': 182940393Sbostic if (!ignDepOp && (c == ':' || c == '!')) { 183040393Sbostic /* 183140393Sbostic * A semi-colon is recognized as a newline only on 183240393Sbostic * dependency lines. Dependency lines are lines with a 183340393Sbostic * colon or an exclamation point. Ergo... 183440393Sbostic */ 183540393Sbostic semiNL = TRUE; 183640393Sbostic } 183740393Sbostic break; 183840393Sbostic } 183940393Sbostic /* 184040393Sbostic * Copy in the previous character and save this one in lastc. 184140393Sbostic */ 184240393Sbostic Buf_AddByte (buf, (Byte)lastc); 184340393Sbostic lastc = c; 184440393Sbostic 184540393Sbostic } 184640393Sbostic line_read: 184740393Sbostic lineno++; 184840393Sbostic 184940393Sbostic if (lastc != '\0') { 185040393Sbostic Buf_AddByte (buf, (Byte)lastc); 185140393Sbostic } 185240393Sbostic Buf_AddByte (buf, (Byte)'\0'); 185340393Sbostic line = (char *)Buf_GetAll (buf, &lineLength); 185440393Sbostic Buf_Destroy (buf, FALSE); 185540393Sbostic 185640435Sbostic if (line[0] == '.') { 185740393Sbostic /* 185840393Sbostic * The line might be a conditional. Ask the conditional module 185940393Sbostic * about it and act accordingly 186040393Sbostic */ 186140393Sbostic switch (Cond_Eval (line)) { 186240393Sbostic case COND_SKIP: 186340393Sbostic do { 186440393Sbostic /* 186540393Sbostic * Skip to next conditional that evaluates to COND_PARSE. 186640393Sbostic */ 186740393Sbostic free (line); 186840393Sbostic c = ParseReadc(); 186940393Sbostic /* 187040393Sbostic * Skip lines until get to one that begins with a 187140393Sbostic * special char. 187240393Sbostic */ 187340435Sbostic while ((c != '.') && (c != EOF)) { 187440393Sbostic while (((c != '\n') || (lastc == '\\')) && 187540393Sbostic (c != EOF)) 187640393Sbostic { 187740393Sbostic /* 187840393Sbostic * Advance to next unescaped newline 187940393Sbostic */ 188040393Sbostic if ((lastc = c) == '\n') { 188140393Sbostic lineno++; 188240393Sbostic } 188340393Sbostic c = ParseReadc(); 188440393Sbostic } 188540393Sbostic lineno++; 188640393Sbostic 188740393Sbostic lastc = c; 188840393Sbostic c = ParseReadc (); 188940393Sbostic } 189040393Sbostic 189140393Sbostic if (c == EOF) { 189240393Sbostic Parse_Error (PARSE_FATAL, "Unclosed conditional"); 189340393Sbostic return ((char *)NULL); 189440393Sbostic } 189540393Sbostic 189640393Sbostic /* 189740393Sbostic * Read the entire line into buf 189840393Sbostic */ 189940393Sbostic buf = Buf_Init (BSIZE); 190040393Sbostic do { 190140393Sbostic Buf_AddByte (buf, (Byte)c); 190240393Sbostic c = ParseReadc(); 190340393Sbostic } while ((c != '\n') && (c != EOF)); 190440393Sbostic lineno++; 190540393Sbostic 190640393Sbostic Buf_AddByte (buf, (Byte)'\0'); 190740393Sbostic line = (char *)Buf_GetAll (buf, &lineLength); 190840393Sbostic Buf_Destroy (buf, FALSE); 190940393Sbostic } while (Cond_Eval(line) != COND_PARSE); 191040393Sbostic /*FALLTHRU*/ 191140393Sbostic case COND_PARSE: 191240393Sbostic free (line); 191340393Sbostic line = ParseReadLine(); 191440393Sbostic break; 191540393Sbostic } 191640393Sbostic } 191740393Sbostic 191840393Sbostic return (line); 191940393Sbostic } else { 192040393Sbostic /* 192140393Sbostic * Hit end-of-file, so return a NULL line to indicate this. 192240393Sbostic */ 192340393Sbostic return((char *)NULL); 192440393Sbostic } 192540393Sbostic } 192640424Sbostic 192740393Sbostic /*- 192840393Sbostic *----------------------------------------------------------------------- 192940393Sbostic * ParseFinishLine -- 193040393Sbostic * Handle the end of a dependency group. 193140393Sbostic * 193240393Sbostic * Results: 193340393Sbostic * Nothing. 193440393Sbostic * 193540393Sbostic * Side Effects: 193640393Sbostic * inLine set FALSE. 'targets' list destroyed. 193740393Sbostic * 193840393Sbostic *----------------------------------------------------------------------- 193940393Sbostic */ 194040393Sbostic static void 194140393Sbostic ParseFinishLine() 194240393Sbostic { 194340393Sbostic extern int Suff_EndTransform(); 194440393Sbostic 194540393Sbostic if (inLine) { 194640393Sbostic Lst_ForEach(targets, Suff_EndTransform, (ClientData)NULL); 194740393Sbostic Lst_Destroy (targets, ParseHasCommands); 194840393Sbostic inLine = FALSE; 194940393Sbostic } 195040393Sbostic } 195140393Sbostic 195240424Sbostic 195340393Sbostic /*- 195440393Sbostic *--------------------------------------------------------------------- 195540393Sbostic * Parse_File -- 195640393Sbostic * Parse a file into its component parts, incorporating it into the 195740393Sbostic * current dependency graph. This is the main function and controls 195840393Sbostic * almost every other function in this module 195940393Sbostic * 196040393Sbostic * Results: 196140393Sbostic * None 196240393Sbostic * 196340393Sbostic * Side Effects: 196440393Sbostic * Loads. Nodes are added to the list of all targets, nodes and links 196540393Sbostic * are added to the dependency graph. etc. etc. etc. 196640393Sbostic *--------------------------------------------------------------------- 196740393Sbostic */ 196840393Sbostic void 196940393Sbostic Parse_File(name, stream) 197040393Sbostic char *name; /* the name of the file being read */ 197140393Sbostic FILE * stream; /* Stream open to makefile to parse */ 197240393Sbostic { 197340393Sbostic register char *cp, /* pointer into the line */ 197440393Sbostic *line; /* the line we're working on */ 197540393Sbostic 197640393Sbostic inLine = FALSE; 197740393Sbostic fname = name; 197840393Sbostic curFILE = stream; 197940393Sbostic lineno = 0; 198040393Sbostic fatals = 0; 198140393Sbostic 198240393Sbostic do { 198340393Sbostic while (line = ParseReadLine ()) { 198440435Sbostic if (*line == '.') { 198540393Sbostic /* 198640393Sbostic * Lines that begin with the special character are either 198740393Sbostic * include or undef directives. 198840393Sbostic */ 198940393Sbostic for (cp = line + 1; isspace (*cp); cp++) { 199040393Sbostic continue; 199140393Sbostic } 199240393Sbostic if (strncmp (cp, "include", 7) == 0) { 199340393Sbostic ParseDoInclude (cp + 7); 199440393Sbostic goto nextLine; 199540393Sbostic } else if (strncmp(cp, "undef", 5) == 0) { 199640393Sbostic char *cp2; 199740393Sbostic for (cp += 5; isspace(*cp); cp++) { 199840393Sbostic continue; 199940393Sbostic } 200040393Sbostic 200140393Sbostic for (cp2 = cp; !isspace(*cp2) && (*cp2 != '\0'); cp2++) { 200240393Sbostic continue; 200340393Sbostic } 200440393Sbostic 200540393Sbostic *cp2 = '\0'; 200640393Sbostic 200740393Sbostic Var_Delete(cp, VAR_GLOBAL); 200840393Sbostic goto nextLine; 200940393Sbostic } 201040393Sbostic } 201140393Sbostic if (*line == '#') { 201240435Sbostic /* If we're this far, the line must be a comment. */ 201340393Sbostic goto nextLine; 201440393Sbostic } 201540393Sbostic 201640393Sbostic if (*line == '\t' 201740393Sbostic #ifdef POSIX 201840393Sbostic || *line == ' ' 201940393Sbostic #endif 202040393Sbostic ) 202140393Sbostic { 202240393Sbostic /* 202340393Sbostic * If a line starts with a tab (or space in POSIX-land), it 202440393Sbostic * can only hope to be a creation command. 202540393Sbostic */ 202640393Sbostic shellCommand: 202740393Sbostic for (cp = line + 1; isspace (*cp); cp++) { 202840393Sbostic continue; 202940393Sbostic } 203040393Sbostic if (*cp) { 203140393Sbostic if (inLine) { 203240393Sbostic /* 203340393Sbostic * So long as it's not a blank line and we're actually 203440393Sbostic * in a dependency spec, add the command to the list of 203540393Sbostic * commands of all targets in the dependency spec 203640393Sbostic */ 203740393Sbostic Lst_ForEach (targets, ParseAddCmd, (ClientData)cp); 203840393Sbostic continue; 203940393Sbostic } else { 204040393Sbostic Parse_Error (PARSE_FATAL, 204140393Sbostic "Unassociated shell command \"%.20s\"", 204240393Sbostic cp); 204340393Sbostic } 204440393Sbostic } 204540393Sbostic } else if (Parse_IsVar (line)) { 204640393Sbostic ParseFinishLine(); 204740393Sbostic Parse_DoVar (line, VAR_GLOBAL); 204840393Sbostic } else { 204940393Sbostic /* 205040393Sbostic * We now know it's a dependency line so it needs to have all 205140393Sbostic * variables expanded before being parsed. Tell the variable 205240393Sbostic * module to complain if some variable is undefined... 205340393Sbostic * To make life easier on novices, if the line is indented we 205440393Sbostic * first make sure the line has a dependency operator in it. 205540393Sbostic * If it doesn't have an operator and we're in a dependency 205640393Sbostic * line's script, we assume it's actually a shell command 205740393Sbostic * and add it to the current list of targets. 205840393Sbostic * 205940393Sbostic * Note that POSIX declares all lines that start with 206040393Sbostic * whitespace are shell commands, so there's no need to check 206140393Sbostic * here... 206240393Sbostic */ 206340393Sbostic Boolean nonSpace = FALSE; 206440393Sbostic 206540393Sbostic cp = line; 206640393Sbostic #ifndef POSIX 206740393Sbostic if (line[0] == ' ') { 206840393Sbostic while ((*cp != ':') && (*cp != '!') && (*cp != '\0')) { 206940393Sbostic if (!isspace(*cp)) { 207040393Sbostic nonSpace = TRUE; 207140393Sbostic } 207240393Sbostic cp++; 207340393Sbostic } 207440393Sbostic } 207540393Sbostic 207640393Sbostic if (*cp == '\0') { 207740393Sbostic if (inLine) { 207840393Sbostic Parse_Error (PARSE_WARNING, 207940393Sbostic "Shell command needs a leading tab"); 208040393Sbostic goto shellCommand; 208140393Sbostic } else if (nonSpace) { 208240393Sbostic Parse_Error (PARSE_FATAL, "Missing operator"); 208340393Sbostic } 208440393Sbostic } else { 208540393Sbostic #endif 208640393Sbostic ParseFinishLine(); 208740393Sbostic 208840393Sbostic cp = Var_Subst (line, VAR_CMD, TRUE); 208940393Sbostic free (line); 209040393Sbostic line = cp; 209140393Sbostic 209240393Sbostic /* 209340393Sbostic * Need a non-circular list for the target nodes 209440393Sbostic */ 209540393Sbostic targets = Lst_Init (FALSE); 209640393Sbostic inLine = TRUE; 209740393Sbostic 209840393Sbostic ParseDoDependency (line); 209940393Sbostic #ifndef POSIX 210040393Sbostic } 210140393Sbostic #endif 210240393Sbostic } 210340393Sbostic 210440393Sbostic nextLine: 210540393Sbostic 210640393Sbostic free (line); 210740393Sbostic } 210840393Sbostic /* 210940393Sbostic * Reached EOF, but it may be just EOF of an include file... 211040393Sbostic */ 211140393Sbostic } while (ParseEOF() == CONTINUE); 211240393Sbostic 211340393Sbostic /* 211440393Sbostic * Make sure conditionals are clean 211540393Sbostic */ 211640393Sbostic Cond_End(); 211740393Sbostic 211840393Sbostic if (fatals) { 211940393Sbostic fprintf (stderr, "Fatal errors encountered -- cannot continue\n"); 212040393Sbostic exit (1); 212140393Sbostic } 212240393Sbostic } 212340424Sbostic 212440393Sbostic /*- 212540393Sbostic *--------------------------------------------------------------------- 212640393Sbostic * Parse_Init -- 212740393Sbostic * initialize the parsing module 212840393Sbostic * 212940393Sbostic * Results: 213040393Sbostic * none 213140393Sbostic * 213240393Sbostic * Side Effects: 213340393Sbostic * the parseIncPath list is initialized... 213440393Sbostic *--------------------------------------------------------------------- 213540393Sbostic */ 213640393Sbostic Parse_Init () 213740393Sbostic { 213840515Sbostic char *cp, *start; 213940515Sbostic /* avoid faults on read-only strings */ 214040515Sbostic static char syspath[] = _PATH_DEFSYSPATH; 214140393Sbostic 214240393Sbostic mainNode = NILGNODE; 214340393Sbostic parseIncPath = Lst_Init (FALSE); 214440393Sbostic sysIncPath = Lst_Init (FALSE); 214540393Sbostic includes = Lst_Init (FALSE); 214640393Sbostic 214740393Sbostic /* 214840393Sbostic * Add the directories from the DEFSYSPATH (more than one may be given 214940393Sbostic * as dir1:...:dirn) to the system include path. 215040393Sbostic */ 215140393Sbostic for (start = syspath; *start != '\0'; start = cp) { 215240393Sbostic for (cp = start; *cp != '\0' && *cp != ':'; cp++) { 215340393Sbostic ; 215440393Sbostic } 215540393Sbostic if (*cp == '\0') { 215640393Sbostic Dir_AddDir(sysIncPath, start); 215740393Sbostic } else { 215840393Sbostic *cp++ = '\0'; 215940393Sbostic Dir_AddDir(sysIncPath, start); 216040393Sbostic } 216140393Sbostic } 216240393Sbostic } 216340424Sbostic 216440393Sbostic /*- 216540393Sbostic *----------------------------------------------------------------------- 216640393Sbostic * Parse_MainName -- 216740393Sbostic * Return a Lst of the main target to create for main()'s sake. If 216840393Sbostic * no such target exists, we Punt with an obnoxious error message. 216940393Sbostic * 217040393Sbostic * Results: 217140393Sbostic * A Lst of the single node to create. 217240393Sbostic * 217340393Sbostic * Side Effects: 217440393Sbostic * None. 217540393Sbostic * 217640393Sbostic *----------------------------------------------------------------------- 217740393Sbostic */ 217840393Sbostic Lst 217940393Sbostic Parse_MainName() 218040393Sbostic { 218140393Sbostic Lst main; /* result list */ 218240393Sbostic 218340393Sbostic main = Lst_Init (FALSE); 218440393Sbostic 218540393Sbostic if (mainNode == NILGNODE) { 218640393Sbostic Punt ("I don't know what to DO!\n"); 218740393Sbostic /*NOTREACHED*/ 218840393Sbostic } else if (mainNode->type & OP_DOUBLEDEP) { 218940393Sbostic Lst_Concat(main, mainNode->cohorts, LST_CONCNEW); 219040393Sbostic } 219140393Sbostic (void) Lst_AtEnd (main, (ClientData)mainNode); 219240393Sbostic return (main); 219340393Sbostic } 2194