[BACK]Return to parse.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / make

Annotation of src/usr.bin/make/parse.c, Revision 1.13

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