[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.16

1.16    ! millert     1: /*     $OpenBSD: parse.c,v 1.15 1997/07/25 21:05:35 mickey Exp $       */
1.13      millert     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.16    ! millert    46: static char rcsid[] = "$OpenBSD: parse.c,v 1.15 1997/07/25 21:05:35 mickey 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:
1.15      mickey     92: #ifdef __STDC__
1.1       deraadt    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
1.15      mickey    313: #ifdef __STDC__
1.1       deraadt   314: Parse_Error(int type, char *fmt, ...)
                    315: #else
                    316: Parse_Error(va_alist)
                    317:        va_dcl
                    318: #endif
                    319: {
                    320:        va_list ap;
1.15      mickey    321: #ifdef __STDC__
1.1       deraadt   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.16    ! millert   946:                if (gn != NULL)
        !           947:                    (void)Lst_AtEnd (targets, (ClientData)gn);
1.1       deraadt   948:            }
                    949:        } else if (specType == ExPath && *line != '.' && *line != '\0') {
                    950:            Parse_Error(PARSE_WARNING, "Extra target (%s) ignored", line);
                    951:        }
1.11      millert   952:
1.1       deraadt   953:        *cp = savec;
                    954:        /*
                    955:         * If it is a special type and not .PATH, it's the only target we
                    956:         * allow on this line...
                    957:         */
                    958:        if (specType != Not && specType != ExPath) {
                    959:            Boolean warn = FALSE;
1.11      millert   960:
1.1       deraadt   961:            while ((*cp != '!') && (*cp != ':') && *cp) {
                    962:                if (*cp != ' ' && *cp != '\t') {
                    963:                    warn = TRUE;
                    964:                }
                    965:                cp++;
                    966:            }
                    967:            if (warn) {
                    968:                Parse_Error(PARSE_WARNING, "Extra target ignored");
                    969:            }
                    970:        } else {
                    971:            while (*cp && isspace (*cp)) {
                    972:                cp++;
                    973:            }
                    974:        }
                    975:        line = cp;
                    976:     } while ((*line != '!') && (*line != ':') && *line);
                    977:
                    978:     /*
                    979:      * Don't need the list of target names anymore...
                    980:      */
                    981:     Lst_Destroy(curTargs, NOFREE);
                    982:
                    983:     if (!Lst_IsEmpty(targets)) {
                    984:        switch(specType) {
                    985:            default:
                    986:                Parse_Error(PARSE_WARNING, "Special and mundane targets don't mix. Mundane ones ignored");
                    987:                break;
                    988:            case Default:
                    989:            case Begin:
                    990:            case End:
                    991:            case Interrupt:
                    992:                /*
                    993:                 * These four create nodes on which to hang commands, so
                    994:                 * targets shouldn't be empty...
                    995:                 */
                    996:            case Not:
                    997:                /*
                    998:                 * Nothing special here -- targets can be empty if it wants.
                    999:                 */
                   1000:                break;
                   1001:        }
                   1002:     }
                   1003:
                   1004:     /*
                   1005:      * Have now parsed all the target names. Must parse the operator next. The
                   1006:      * result is left in  op .
                   1007:      */
                   1008:     if (*cp == '!') {
                   1009:        op = OP_FORCE;
                   1010:     } else if (*cp == ':') {
                   1011:        if (cp[1] == ':') {
                   1012:            op = OP_DOUBLEDEP;
                   1013:            cp++;
                   1014:        } else {
                   1015:            op = OP_DEPENDS;
                   1016:        }
                   1017:     } else {
                   1018:        Parse_Error (PARSE_FATAL, "Missing dependency operator");
                   1019:        return;
                   1020:     }
                   1021:
                   1022:     cp++;                      /* Advance beyond operator */
                   1023:
                   1024:     Lst_ForEach (targets, ParseDoOp, (ClientData)&op);
                   1025:
                   1026:     /*
1.11      millert  1027:      * Get to the first source
1.1       deraadt  1028:      */
                   1029:     while (*cp && isspace (*cp)) {
                   1030:        cp++;
                   1031:     }
                   1032:     line = cp;
                   1033:
                   1034:     /*
                   1035:      * Several special targets take different actions if present with no
                   1036:      * sources:
                   1037:      * a .SUFFIXES line with no sources clears out all old suffixes
                   1038:      * a .PRECIOUS line makes all targets precious
                   1039:      * a .IGNORE line ignores errors for all targets
                   1040:      * a .SILENT line creates silence when making all targets
                   1041:      * a .PATH removes all directories from the search path(s).
                   1042:      */
                   1043:     if (!*line) {
                   1044:        switch (specType) {
                   1045:            case Suffixes:
                   1046:                Suff_ClearSuffixes ();
                   1047:                break;
                   1048:            case Precious:
                   1049:                allPrecious = TRUE;
                   1050:                break;
                   1051:            case Ignore:
                   1052:                ignoreErrors = TRUE;
                   1053:                break;
                   1054:            case Silent:
                   1055:                beSilent = TRUE;
                   1056:                break;
                   1057:            case ExPath:
                   1058:                Lst_ForEach(paths, ParseClearPath, (ClientData)NULL);
                   1059:                break;
                   1060:            default:
                   1061:                break;
                   1062:        }
                   1063:     } else if (specType == MFlags) {
                   1064:        /*
                   1065:         * Call on functions in main.c to deal with these arguments and
                   1066:         * set the initial character to a null-character so the loop to
                   1067:         * get sources won't get anything
                   1068:         */
                   1069:        Main_ParseArgLine (line);
                   1070:        *line = '\0';
                   1071:     } else if (specType == ExShell) {
                   1072:        if (Job_ParseShell (line) != SUCCESS) {
                   1073:            Parse_Error (PARSE_FATAL, "improper shell specification");
                   1074:            return;
                   1075:        }
                   1076:        *line = '\0';
                   1077:     } else if ((specType == NotParallel) || (specType == SingleShell)) {
                   1078:        *line = '\0';
                   1079:     }
1.11      millert  1080:
1.1       deraadt  1081:     /*
1.11      millert  1082:      * NOW GO FOR THE SOURCES
1.1       deraadt  1083:      */
                   1084:     if ((specType == Suffixes) || (specType == ExPath) ||
                   1085:        (specType == Includes) || (specType == Libs) ||
                   1086:        (specType == Null))
                   1087:     {
                   1088:        while (*line) {
                   1089:            /*
                   1090:             * If the target was one that doesn't take files as its sources
                   1091:             * but takes something like suffixes, we take each
                   1092:             * space-separated word on the line as a something and deal
                   1093:             * with it accordingly.
                   1094:             *
                   1095:             * If the target was .SUFFIXES, we take each source as a
                   1096:             * suffix and add it to the list of suffixes maintained by the
                   1097:             * Suff module.
                   1098:             *
                   1099:             * If the target was a .PATH, we add the source as a directory
                   1100:             * to search on the search path.
                   1101:             *
                   1102:             * If it was .INCLUDES, the source is taken to be the suffix of
                   1103:             * files which will be #included and whose search path should
                   1104:             * be present in the .INCLUDES variable.
                   1105:             *
                   1106:             * If it was .LIBS, the source is taken to be the suffix of
                   1107:             * files which are considered libraries and whose search path
                   1108:             * should be present in the .LIBS variable.
                   1109:             *
                   1110:             * If it was .NULL, the source is the suffix to use when a file
                   1111:             * has no valid suffix.
                   1112:             */
                   1113:            char  savec;
                   1114:            while (*cp && !isspace (*cp)) {
                   1115:                cp++;
                   1116:            }
                   1117:            savec = *cp;
                   1118:            *cp = '\0';
                   1119:            switch (specType) {
                   1120:                case Suffixes:
                   1121:                    Suff_AddSuffix (line);
                   1122:                    break;
                   1123:                case ExPath:
                   1124:                    Lst_ForEach(paths, ParseAddDir, (ClientData)line);
                   1125:                    break;
                   1126:                case Includes:
                   1127:                    Suff_AddInclude (line);
                   1128:                    break;
                   1129:                case Libs:
                   1130:                    Suff_AddLib (line);
                   1131:                    break;
                   1132:                case Null:
                   1133:                    Suff_SetNull (line);
                   1134:                    break;
                   1135:                default:
                   1136:                    break;
                   1137:            }
                   1138:            *cp = savec;
                   1139:            if (savec != '\0') {
                   1140:                cp++;
                   1141:            }
                   1142:            while (*cp && isspace (*cp)) {
                   1143:                cp++;
                   1144:            }
                   1145:            line = cp;
                   1146:        }
                   1147:        if (paths) {
                   1148:            Lst_Destroy(paths, NOFREE);
                   1149:        }
                   1150:     } else {
                   1151:        while (*line) {
                   1152:            /*
                   1153:             * The targets take real sources, so we must beware of archive
                   1154:             * specifications (i.e. things with left parentheses in them)
                   1155:             * and handle them accordingly.
                   1156:             */
                   1157:            while (*cp && !isspace (*cp)) {
                   1158:                if ((*cp == '(') && (cp > line) && (cp[-1] != '$')) {
                   1159:                    /*
                   1160:                     * Only stop for a left parenthesis if it isn't at the
                   1161:                     * start of a word (that'll be for variable changes
                   1162:                     * later) and isn't preceded by a dollar sign (a dynamic
                   1163:                     * source).
                   1164:                     */
                   1165:                    break;
                   1166:                } else {
                   1167:                    cp++;
                   1168:                }
                   1169:            }
                   1170:
                   1171:            if (*cp == '(') {
                   1172:                GNode     *gn;
                   1173:
                   1174:                sources = Lst_Init (FALSE);
                   1175:                if (Arch_ParseArchive (&line, sources, VAR_CMD) != SUCCESS) {
                   1176:                    Parse_Error (PARSE_FATAL,
                   1177:                                 "Error in source archive spec \"%s\"", line);
                   1178:                    return;
                   1179:                }
                   1180:
                   1181:                while (!Lst_IsEmpty (sources)) {
                   1182:                    gn = (GNode *) Lst_DeQueue (sources);
1.3       deraadt  1183:                    ParseDoSrc (tOp, gn->name, curSrcs);
1.1       deraadt  1184:                }
                   1185:                Lst_Destroy (sources, NOFREE);
                   1186:                cp = line;
                   1187:            } else {
                   1188:                if (*cp) {
                   1189:                    *cp = '\0';
                   1190:                    cp += 1;
                   1191:                }
                   1192:
1.3       deraadt  1193:                ParseDoSrc (tOp, line, curSrcs);
1.1       deraadt  1194:            }
                   1195:            while (*cp && isspace (*cp)) {
                   1196:                cp++;
                   1197:            }
                   1198:            line = cp;
                   1199:        }
                   1200:     }
1.11      millert  1201:
1.1       deraadt  1202:     if (mainNode == NILGNODE) {
                   1203:        /*
                   1204:         * If we have yet to decide on a main target to make, in the
                   1205:         * absence of any user input, we want the first target on
                   1206:         * the first dependency line that is actually a real target
                   1207:         * (i.e. isn't a .USE or .EXEC rule) to be made.
                   1208:         */
                   1209:        Lst_ForEach (targets, ParseFindMain, (ClientData)0);
                   1210:     }
                   1211:
1.3       deraadt  1212:     /*
                   1213:      * Finally, destroy the list of sources
                   1214:      */
                   1215:     Lst_Destroy(curSrcs, NOFREE);
1.1       deraadt  1216: }
                   1217:
                   1218: /*-
                   1219:  *---------------------------------------------------------------------
                   1220:  * Parse_IsVar  --
                   1221:  *     Return TRUE if the passed line is a variable assignment. A variable
                   1222:  *     assignment consists of a single word followed by optional whitespace
                   1223:  *     followed by either a += or an = operator.
                   1224:  *     This function is used both by the Parse_File function and main when
                   1225:  *     parsing the command-line arguments.
                   1226:  *
                   1227:  * Results:
                   1228:  *     TRUE if it is. FALSE if it ain't
                   1229:  *
                   1230:  * Side Effects:
                   1231:  *     none
                   1232:  *---------------------------------------------------------------------
                   1233:  */
                   1234: Boolean
                   1235: Parse_IsVar (line)
                   1236:     register char  *line;      /* the line to check */
                   1237: {
                   1238:     register Boolean wasSpace = FALSE; /* set TRUE if found a space */
                   1239:     register Boolean haveName = FALSE; /* Set TRUE if have a variable name */
                   1240:     int level = 0;
                   1241: #define ISEQOPERATOR(c) \
                   1242:        (((c) == '+') || ((c) == ':') || ((c) == '?') || ((c) == '!'))
                   1243:
                   1244:     /*
                   1245:      * Skip to variable name
                   1246:      */
1.11      millert  1247:     for (;(*line == ' ') || (*line == '\t'); line++)
1.1       deraadt  1248:        continue;
                   1249:
                   1250:     for (; *line != '=' || level != 0; line++)
                   1251:        switch (*line) {
                   1252:        case '\0':
                   1253:            /*
                   1254:             * end-of-line -- can't be a variable assignment.
                   1255:             */
                   1256:            return FALSE;
                   1257:
                   1258:        case ' ':
                   1259:        case '\t':
                   1260:            /*
                   1261:             * there can be as much white space as desired so long as there is
1.11      millert  1262:             * only one word before the operator
1.1       deraadt  1263:             */
                   1264:            wasSpace = TRUE;
                   1265:            break;
                   1266:
                   1267:        case '(':
                   1268:        case '{':
                   1269:            level++;
                   1270:            break;
                   1271:
                   1272:        case '}':
                   1273:        case ')':
                   1274:            level--;
                   1275:            break;
1.11      millert  1276:
1.1       deraadt  1277:        default:
                   1278:            if (wasSpace && haveName) {
                   1279:                    if (ISEQOPERATOR(*line)) {
                   1280:                        /*
1.9       briggs   1281:                         * We must have a finished word
                   1282:                         */
                   1283:                        if (level != 0)
                   1284:                            return FALSE;
                   1285:
                   1286:                        /*
1.1       deraadt  1287:                         * When an = operator [+?!:] is found, the next
1.9       briggs   1288:                         * character must be an = or it ain't a valid
1.1       deraadt  1289:                         * assignment.
                   1290:                         */
1.9       briggs   1291:                        if (line[1] == '=')
1.1       deraadt  1292:                            return haveName;
1.9       briggs   1293: #ifdef SUNSHCMD
1.1       deraadt  1294:                        /*
1.9       briggs   1295:                         * This is a shell command
1.1       deraadt  1296:                         */
1.9       briggs   1297:                        if (strncmp(line, ":sh", 3) == 0)
                   1298:                            return haveName;
                   1299: #endif
1.1       deraadt  1300:                    }
1.9       briggs   1301:                    /*
                   1302:                     * This is the start of another word, so not assignment.
                   1303:                     */
                   1304:                    return FALSE;
1.1       deraadt  1305:            }
                   1306:            else {
1.11      millert  1307:                haveName = TRUE;
1.1       deraadt  1308:                wasSpace = FALSE;
                   1309:            }
                   1310:            break;
                   1311:        }
                   1312:
                   1313:     return haveName;
                   1314: }
                   1315:
                   1316: /*-
                   1317:  *---------------------------------------------------------------------
                   1318:  * Parse_DoVar  --
                   1319:  *     Take the variable assignment in the passed line and do it in the
                   1320:  *     global context.
                   1321:  *
                   1322:  *     Note: There is a lexical ambiguity with assignment modifier characters
                   1323:  *     in variable names. This routine interprets the character before the =
                   1324:  *     as a modifier. Therefore, an assignment like
                   1325:  *         C++=/usr/bin/CC
                   1326:  *     is interpreted as "C+ +=" instead of "C++ =".
                   1327:  *
                   1328:  * Results:
                   1329:  *     none
                   1330:  *
                   1331:  * Side Effects:
                   1332:  *     the variable structure of the given variable name is altered in the
                   1333:  *     global context.
                   1334:  *---------------------------------------------------------------------
                   1335:  */
                   1336: void
                   1337: Parse_DoVar (line, ctxt)
                   1338:     char            *line;     /* a line guaranteed to be a variable
                   1339:                                 * assignment. This reduces error checks */
                   1340:     GNode          *ctxt;      /* Context in which to do the assignment */
                   1341: {
                   1342:     char          *cp; /* pointer into line */
                   1343:     enum {
                   1344:        VAR_SUBST, VAR_APPEND, VAR_SHELL, VAR_NORMAL
                   1345:     }              type;       /* Type of assignment */
1.11      millert  1346:     char            *opc;      /* ptr to operator character to
1.1       deraadt  1347:                                 * null-terminate the variable name */
1.11      millert  1348:     /*
1.1       deraadt  1349:      * Avoid clobbered variable warnings by forcing the compiler
                   1350:      * to ``unregister'' variables
                   1351:      */
                   1352: #if __GNUC__
                   1353:     (void) &cp;
                   1354:     (void) &line;
                   1355: #endif
                   1356:
                   1357:     /*
                   1358:      * Skip to variable name
                   1359:      */
                   1360:     while ((*line == ' ') || (*line == '\t')) {
                   1361:        line++;
                   1362:     }
                   1363:
                   1364:     /*
                   1365:      * Skip to operator character, nulling out whitespace as we go
                   1366:      */
                   1367:     for (cp = line + 1; *cp != '='; cp++) {
                   1368:        if (isspace (*cp)) {
                   1369:            *cp = '\0';
                   1370:        }
                   1371:     }
                   1372:     opc = cp-1;                /* operator is the previous character */
                   1373:     *cp++ = '\0';      /* nuke the = */
                   1374:
                   1375:     /*
                   1376:      * Check operator type
                   1377:      */
                   1378:     switch (*opc) {
                   1379:        case '+':
                   1380:            type = VAR_APPEND;
                   1381:            *opc = '\0';
                   1382:            break;
                   1383:
                   1384:        case '?':
                   1385:            /*
                   1386:             * If the variable already has a value, we don't do anything.
                   1387:             */
                   1388:            *opc = '\0';
                   1389:            if (Var_Exists(line, ctxt)) {
                   1390:                return;
                   1391:            } else {
                   1392:                type = VAR_NORMAL;
                   1393:            }
                   1394:            break;
                   1395:
                   1396:        case ':':
                   1397:            type = VAR_SUBST;
                   1398:            *opc = '\0';
                   1399:            break;
                   1400:
                   1401:        case '!':
                   1402:            type = VAR_SHELL;
                   1403:            *opc = '\0';
                   1404:            break;
                   1405:
                   1406:        default:
1.9       briggs   1407: #ifdef SUNSHCMD
                   1408:            while (*opc != ':')
                   1409:                if (--opc < line)
                   1410:                    break;
                   1411:
                   1412:            if (strncmp(opc, ":sh", 3) == 0) {
                   1413:                type = VAR_SHELL;
                   1414:                *opc = '\0';
                   1415:                break;
                   1416:            }
                   1417: #endif
1.1       deraadt  1418:            type = VAR_NORMAL;
                   1419:            break;
                   1420:     }
                   1421:
                   1422:     while (isspace (*cp)) {
                   1423:        cp++;
                   1424:     }
                   1425:
                   1426:     if (type == VAR_APPEND) {
                   1427:        Var_Append (line, cp, ctxt);
                   1428:     } else if (type == VAR_SUBST) {
                   1429:        /*
                   1430:         * Allow variables in the old value to be undefined, but leave their
                   1431:         * invocation alone -- this is done by forcing oldVars to be false.
                   1432:         * XXX: This can cause recursive variables, but that's not hard to do,
                   1433:         * and this allows someone to do something like
                   1434:         *
                   1435:         *  CFLAGS = $(.INCLUDES)
                   1436:         *  CFLAGS := -I.. $(CFLAGS)
                   1437:         *
                   1438:         * And not get an error.
                   1439:         */
                   1440:        Boolean   oldOldVars = oldVars;
                   1441:
                   1442:        oldVars = FALSE;
                   1443:        cp = Var_Subst(NULL, cp, ctxt, FALSE);
                   1444:        oldVars = oldOldVars;
                   1445:
                   1446:        Var_Set(line, cp, ctxt);
                   1447:        free(cp);
                   1448:     } else if (type == VAR_SHELL) {
1.9       briggs   1449:        Boolean freeCmd = FALSE; /* TRUE if the command needs to be freed, i.e.
                   1450:                                  * if any variable expansion was performed */
                   1451:        char *res, *err;
1.1       deraadt  1452:
1.9       briggs   1453:        if (strchr(cp, '$') != NULL) {
1.1       deraadt  1454:            /*
                   1455:             * There's a dollar sign in the command, so perform variable
                   1456:             * expansion on the whole thing. The resulting string will need
                   1457:             * freeing when we're done, so set freeCmd to TRUE.
                   1458:             */
1.9       briggs   1459:            cp = Var_Subst(NULL, cp, VAR_CMD, TRUE);
1.1       deraadt  1460:            freeCmd = TRUE;
                   1461:        }
                   1462:
1.9       briggs   1463:        res = Cmd_Exec(cp, &err);
                   1464:        Var_Set(line, res, ctxt);
                   1465:        free(res);
1.1       deraadt  1466:
1.9       briggs   1467:        if (err)
                   1468:            Parse_Error(PARSE_WARNING, err, cp);
1.1       deraadt  1469:
1.9       briggs   1470:        if (freeCmd)
                   1471:            free(cp);
1.1       deraadt  1472:     } else {
                   1473:        /*
                   1474:         * Normal assignment -- just do it.
                   1475:         */
1.9       briggs   1476:        Var_Set(line, cp, ctxt);
1.1       deraadt  1477:     }
                   1478: }
                   1479:
1.9       briggs   1480:
1.1       deraadt  1481: /*-
                   1482:  * ParseAddCmd  --
                   1483:  *     Lst_ForEach function to add a command line to all targets
                   1484:  *
                   1485:  * Results:
                   1486:  *     Always 0
                   1487:  *
                   1488:  * Side Effects:
                   1489:  *     A new element is added to the commands list of the node.
                   1490:  */
                   1491: static int
                   1492: ParseAddCmd(gnp, cmd)
                   1493:     ClientData gnp;    /* the node to which the command is to be added */
                   1494:     ClientData cmd;    /* the command to add */
                   1495: {
                   1496:     GNode *gn = (GNode *) gnp;
                   1497:     /* if target already supplied, ignore commands */
                   1498:     if (!(gn->type & OP_HAS_COMMANDS))
                   1499:        (void)Lst_AtEnd(gn->commands, cmd);
                   1500:     return(0);
                   1501: }
                   1502:
                   1503: /*-
                   1504:  *-----------------------------------------------------------------------
                   1505:  * ParseHasCommands --
                   1506:  *     Callback procedure for Parse_File when destroying the list of
                   1507:  *     targets on the last dependency line. Marks a target as already
                   1508:  *     having commands if it does, to keep from having shell commands
                   1509:  *     on multiple dependency lines.
                   1510:  *
                   1511:  * Results:
                   1512:  *     None
                   1513:  *
                   1514:  * Side Effects:
                   1515:  *     OP_HAS_COMMANDS may be set for the target.
                   1516:  *
                   1517:  *-----------------------------------------------------------------------
                   1518:  */
                   1519: static void
                   1520: ParseHasCommands(gnp)
                   1521:     ClientData           gnp;      /* Node to examine */
                   1522: {
                   1523:     GNode *gn = (GNode *) gnp;
                   1524:     if (!Lst_IsEmpty(gn->commands)) {
                   1525:        gn->type |= OP_HAS_COMMANDS;
                   1526:     }
                   1527: }
                   1528:
                   1529: /*-
                   1530:  *-----------------------------------------------------------------------
                   1531:  * Parse_AddIncludeDir --
                   1532:  *     Add a directory to the path searched for included makefiles
                   1533:  *     bracketed by double-quotes. Used by functions in main.c
                   1534:  *
                   1535:  * Results:
                   1536:  *     None.
                   1537:  *
                   1538:  * Side Effects:
                   1539:  *     The directory is appended to the list.
                   1540:  *
                   1541:  *-----------------------------------------------------------------------
                   1542:  */
                   1543: void
                   1544: Parse_AddIncludeDir (dir)
                   1545:     char         *dir;     /* The name of the directory to add */
                   1546: {
                   1547:     Dir_AddDir (parseIncPath, dir);
                   1548: }
                   1549:
                   1550: /*-
                   1551:  *---------------------------------------------------------------------
                   1552:  * ParseDoInclude  --
                   1553:  *     Push to another file.
1.11      millert  1554:  *
1.1       deraadt  1555:  *     The input is the line minus the #include. A file spec is a string
                   1556:  *     enclosed in <> or "". The former is looked for only in sysIncPath.
                   1557:  *     The latter in . and the directories specified by -I command line
                   1558:  *     options
                   1559:  *
                   1560:  * Results:
                   1561:  *     None
                   1562:  *
                   1563:  * Side Effects:
                   1564:  *     A structure is added to the includes Lst and readProc, lineno,
                   1565:  *     fname and curFILE are altered for the new file
                   1566:  *---------------------------------------------------------------------
                   1567:  */
                   1568: static void
                   1569: ParseDoInclude (file)
                   1570:     char          *file;       /* file specification */
                   1571: {
                   1572:     char          *fullname;   /* full pathname of file */
                   1573:     IFile         *oldFile;    /* state associated with current file */
                   1574:     char          endc;                /* the character which ends the file spec */
                   1575:     char          *cp;         /* current position in file spec */
                   1576:     Boolean      isSystem;     /* TRUE if makefile is a system makefile */
                   1577:
                   1578:     /*
                   1579:      * Skip to delimiter character so we know where to look
                   1580:      */
                   1581:     while ((*file == ' ') || (*file == '\t')) {
                   1582:        file++;
                   1583:     }
                   1584:
                   1585:     if ((*file != '"') && (*file != '<')) {
                   1586:        Parse_Error (PARSE_FATAL,
                   1587:            ".include filename must be delimited by '\"' or '<'");
                   1588:        return;
                   1589:     }
                   1590:
                   1591:     /*
                   1592:      * Set the search path on which to find the include file based on the
                   1593:      * characters which bracket its name. Angle-brackets imply it's
                   1594:      * a system Makefile while double-quotes imply it's a user makefile
                   1595:      */
                   1596:     if (*file == '<') {
                   1597:        isSystem = TRUE;
                   1598:        endc = '>';
                   1599:     } else {
                   1600:        isSystem = FALSE;
                   1601:        endc = '"';
                   1602:     }
                   1603:
                   1604:     /*
                   1605:      * Skip to matching delimiter
                   1606:      */
                   1607:     for (cp = ++file; *cp && *cp != endc; cp++) {
                   1608:        continue;
                   1609:     }
                   1610:
                   1611:     if (*cp != endc) {
                   1612:        Parse_Error (PARSE_FATAL,
                   1613:                     "Unclosed %cinclude filename. '%c' expected",
                   1614:                     '.', endc);
                   1615:        return;
                   1616:     }
                   1617:     *cp = '\0';
                   1618:
                   1619:     /*
                   1620:      * Substitute for any variables in the file name before trying to
                   1621:      * find the thing.
                   1622:      */
                   1623:     file = Var_Subst (NULL, file, VAR_CMD, FALSE);
                   1624:
                   1625:     /*
                   1626:      * Now we know the file's name and its search path, we attempt to
                   1627:      * find the durn thing. A return of NULL indicates the file don't
                   1628:      * exist.
                   1629:      */
                   1630:     if (!isSystem) {
                   1631:        /*
                   1632:         * Include files contained in double-quotes are first searched for
                   1633:         * relative to the including file's location. We don't want to
                   1634:         * cd there, of course, so we just tack on the old file's
                   1635:         * leading path components and call Dir_FindFile to see if
                   1636:         * we can locate the beast.
                   1637:         */
1.4       niklas   1638:        char      *prefEnd, *Fname;
1.1       deraadt  1639:
1.4       niklas   1640:        /* Make a temporary copy of this, to be safe. */
1.9       briggs   1641:        Fname = estrdup(fname);
1.4       niklas   1642:
                   1643:        prefEnd = strrchr (Fname, '/');
1.1       deraadt  1644:        if (prefEnd != (char *)NULL) {
                   1645:            char        *newName;
1.11      millert  1646:
1.1       deraadt  1647:            *prefEnd = '\0';
                   1648:            if (file[0] == '/')
1.9       briggs   1649:                newName = estrdup(file);
1.1       deraadt  1650:            else
1.4       niklas   1651:                newName = str_concat (Fname, file, STR_ADDSLASH);
1.1       deraadt  1652:            fullname = Dir_FindFile (newName, parseIncPath);
                   1653:            if (fullname == (char *)NULL) {
                   1654:                fullname = Dir_FindFile(newName, dirSearchPath);
                   1655:            }
                   1656:            free (newName);
                   1657:            *prefEnd = '/';
                   1658:        } else {
                   1659:            fullname = (char *)NULL;
                   1660:        }
1.4       niklas   1661:        free (Fname);
1.1       deraadt  1662:     } else {
                   1663:        fullname = (char *)NULL;
                   1664:     }
                   1665:
                   1666:     if (fullname == (char *)NULL) {
                   1667:        /*
                   1668:         * System makefile or makefile wasn't found in same directory as
                   1669:         * included makefile. Search for it first on the -I search path,
                   1670:         * then on the .PATH search path, if not found in a -I directory.
                   1671:         * XXX: Suffix specific?
                   1672:         */
                   1673:        fullname = Dir_FindFile (file, parseIncPath);
                   1674:        if (fullname == (char *)NULL) {
                   1675:            fullname = Dir_FindFile(file, dirSearchPath);
                   1676:        }
                   1677:     }
                   1678:
                   1679:     if (fullname == (char *)NULL) {
                   1680:        /*
                   1681:         * Still haven't found the makefile. Look for it on the system
                   1682:         * path as a last resort.
                   1683:         */
                   1684:        fullname = Dir_FindFile(file, sysIncPath);
                   1685:     }
                   1686:
                   1687:     if (fullname == (char *) NULL) {
                   1688:        *cp = endc;
                   1689:        Parse_Error (PARSE_FATAL, "Could not find %s", file);
                   1690:        return;
                   1691:     }
                   1692:
                   1693:     free(file);
                   1694:
                   1695:     /*
                   1696:      * Once we find the absolute path to the file, we get to save all the
                   1697:      * state from the current file before we can start reading this
                   1698:      * include file. The state is stored in an IFile structure which
                   1699:      * is placed on a list with other IFile structures. The list makes
                   1700:      * a very nice stack to track how we got here...
                   1701:      */
                   1702:     oldFile = (IFile *) emalloc (sizeof (IFile));
                   1703:     oldFile->fname = fname;
                   1704:
                   1705:     oldFile->F = curFILE;
                   1706:     oldFile->p = curPTR;
                   1707:     oldFile->lineno = lineno;
                   1708:
                   1709:     (void) Lst_AtFront (includes, (ClientData)oldFile);
                   1710:
                   1711:     /*
                   1712:      * Once the previous state has been saved, we can get down to reading
                   1713:      * the new file. We set up the name of the file to be the absolute
                   1714:      * name of the include file so error messages refer to the right
                   1715:      * place. Naturally enough, we start reading at line number 0.
                   1716:      */
                   1717:     fname = fullname;
                   1718:     lineno = 0;
                   1719:
                   1720:     curFILE = fopen (fullname, "r");
                   1721:     curPTR = NULL;
                   1722:     if (curFILE == (FILE * ) NULL) {
                   1723:        Parse_Error (PARSE_FATAL, "Cannot open %s", fullname);
                   1724:        /*
                   1725:         * Pop to previous file
                   1726:         */
                   1727:        (void) ParseEOF(0);
                   1728:     }
                   1729: }
                   1730:
                   1731:
                   1732: /*-
                   1733:  *---------------------------------------------------------------------
                   1734:  * Parse_FromString  --
                   1735:  *     Start Parsing from the given string
1.11      millert  1736:  *
1.1       deraadt  1737:  * Results:
                   1738:  *     None
                   1739:  *
                   1740:  * Side Effects:
                   1741:  *     A structure is added to the includes Lst and readProc, lineno,
                   1742:  *     fname and curFILE are altered for the new file
                   1743:  *---------------------------------------------------------------------
                   1744:  */
                   1745: void
                   1746: Parse_FromString(str)
                   1747:     char *str;
                   1748: {
                   1749:     IFile         *oldFile;    /* state associated with this file */
                   1750:
                   1751:     if (DEBUG(FOR))
                   1752:        (void) fprintf(stderr, "%s\n----\n", str);
                   1753:
                   1754:     oldFile = (IFile *) emalloc (sizeof (IFile));
                   1755:     oldFile->lineno = lineno;
                   1756:     oldFile->fname = fname;
                   1757:     oldFile->F = curFILE;
                   1758:     oldFile->p = curPTR;
1.11      millert  1759:
1.1       deraadt  1760:     (void) Lst_AtFront (includes, (ClientData)oldFile);
                   1761:
                   1762:     curFILE = NULL;
                   1763:     curPTR = (PTR *) emalloc (sizeof (PTR));
                   1764:     curPTR->str = curPTR->ptr = str;
                   1765:     lineno = 0;
1.9       briggs   1766:     fname = estrdup(fname);
1.1       deraadt  1767: }
                   1768:
                   1769:
                   1770: #ifdef SYSVINCLUDE
                   1771: /*-
                   1772:  *---------------------------------------------------------------------
                   1773:  * ParseTraditionalInclude  --
                   1774:  *     Push to another file.
1.11      millert  1775:  *
1.1       deraadt  1776:  *     The input is the line minus the "include".  The file name is
                   1777:  *     the string following the "include".
                   1778:  *
                   1779:  * Results:
                   1780:  *     None
                   1781:  *
                   1782:  * Side Effects:
                   1783:  *     A structure is added to the includes Lst and readProc, lineno,
                   1784:  *     fname and curFILE are altered for the new file
                   1785:  *---------------------------------------------------------------------
                   1786:  */
                   1787: static void
                   1788: ParseTraditionalInclude (file)
                   1789:     char          *file;       /* file specification */
                   1790: {
                   1791:     char          *fullname;   /* full pathname of file */
                   1792:     IFile         *oldFile;    /* state associated with current file */
                   1793:     char          *cp;         /* current position in file spec */
                   1794:     char         *prefEnd;
                   1795:
                   1796:     /*
                   1797:      * Skip over whitespace
                   1798:      */
                   1799:     while ((*file == ' ') || (*file == '\t')) {
                   1800:        file++;
                   1801:     }
                   1802:
                   1803:     if (*file == '\0') {
                   1804:        Parse_Error (PARSE_FATAL,
                   1805:                     "Filename missing from \"include\"");
                   1806:        return;
                   1807:     }
                   1808:
                   1809:     /*
                   1810:      * Skip to end of line or next whitespace
                   1811:      */
                   1812:     for (cp = file; *cp && *cp != '\n' && *cp != '\t' && *cp != ' '; cp++) {
                   1813:        continue;
                   1814:     }
                   1815:
                   1816:     *cp = '\0';
                   1817:
                   1818:     /*
                   1819:      * Substitute for any variables in the file name before trying to
                   1820:      * find the thing.
                   1821:      */
                   1822:     file = Var_Subst (NULL, file, VAR_CMD, FALSE);
                   1823:
                   1824:     /*
                   1825:      * Now we know the file's name, we attempt to find the durn thing.
                   1826:      * A return of NULL indicates the file don't exist.
                   1827:      *
                   1828:      * Include files are first searched for relative to the including
                   1829:      * file's location. We don't want to cd there, of course, so we
                   1830:      * just tack on the old file's leading path components and call
                   1831:      * Dir_FindFile to see if we can locate the beast.
                   1832:      * XXX - this *does* search in the current directory, right?
                   1833:      */
                   1834:
                   1835:     prefEnd = strrchr (fname, '/');
                   1836:     if (prefEnd != (char *)NULL) {
                   1837:        char    *newName;
1.11      millert  1838:
1.1       deraadt  1839:        *prefEnd = '\0';
                   1840:        newName = str_concat (fname, file, STR_ADDSLASH);
                   1841:        fullname = Dir_FindFile (newName, parseIncPath);
                   1842:        if (fullname == (char *)NULL) {
                   1843:            fullname = Dir_FindFile(newName, dirSearchPath);
                   1844:        }
                   1845:        free (newName);
                   1846:        *prefEnd = '/';
                   1847:     } else {
                   1848:        fullname = (char *)NULL;
                   1849:     }
                   1850:
                   1851:     if (fullname == (char *)NULL) {
                   1852:        /*
                   1853:         * System makefile or makefile wasn't found in same directory as
                   1854:         * included makefile. Search for it first on the -I search path,
                   1855:         * then on the .PATH search path, if not found in a -I directory.
                   1856:         * XXX: Suffix specific?
                   1857:         */
                   1858:        fullname = Dir_FindFile (file, parseIncPath);
                   1859:        if (fullname == (char *)NULL) {
                   1860:            fullname = Dir_FindFile(file, dirSearchPath);
                   1861:        }
                   1862:     }
                   1863:
                   1864:     if (fullname == (char *)NULL) {
                   1865:        /*
                   1866:         * Still haven't found the makefile. Look for it on the system
                   1867:         * path as a last resort.
                   1868:         */
                   1869:        fullname = Dir_FindFile(file, sysIncPath);
                   1870:     }
                   1871:
                   1872:     if (fullname == (char *) NULL) {
                   1873:        Parse_Error (PARSE_FATAL, "Could not find %s", file);
                   1874:        return;
                   1875:     }
                   1876:
                   1877:     /*
                   1878:      * Once we find the absolute path to the file, we get to save all the
                   1879:      * state from the current file before we can start reading this
                   1880:      * include file. The state is stored in an IFile structure which
                   1881:      * is placed on a list with other IFile structures. The list makes
                   1882:      * a very nice stack to track how we got here...
                   1883:      */
                   1884:     oldFile = (IFile *) emalloc (sizeof (IFile));
                   1885:     oldFile->fname = fname;
                   1886:
                   1887:     oldFile->F = curFILE;
                   1888:     oldFile->p = curPTR;
                   1889:     oldFile->lineno = lineno;
                   1890:
                   1891:     (void) Lst_AtFront (includes, (ClientData)oldFile);
                   1892:
                   1893:     /*
                   1894:      * Once the previous state has been saved, we can get down to reading
                   1895:      * the new file. We set up the name of the file to be the absolute
                   1896:      * name of the include file so error messages refer to the right
                   1897:      * place. Naturally enough, we start reading at line number 0.
                   1898:      */
                   1899:     fname = fullname;
                   1900:     lineno = 0;
                   1901:
                   1902:     curFILE = fopen (fullname, "r");
                   1903:     curPTR = NULL;
                   1904:     if (curFILE == (FILE * ) NULL) {
                   1905:        Parse_Error (PARSE_FATAL, "Cannot open %s", fullname);
                   1906:        /*
                   1907:         * Pop to previous file
                   1908:         */
                   1909:        (void) ParseEOF(1);
                   1910:     }
                   1911: }
                   1912: #endif
                   1913:
                   1914: /*-
                   1915:  *---------------------------------------------------------------------
                   1916:  * ParseEOF  --
                   1917:  *     Called when EOF is reached in the current file. If we were reading
                   1918:  *     an include file, the includes stack is popped and things set up
                   1919:  *     to go back to reading the previous file at the previous location.
                   1920:  *
                   1921:  * Results:
                   1922:  *     CONTINUE if there's more to do. DONE if not.
                   1923:  *
                   1924:  * Side Effects:
                   1925:  *     The old curFILE, is closed. The includes list is shortened.
                   1926:  *     lineno, curFILE, and fname are changed if CONTINUE is returned.
                   1927:  *---------------------------------------------------------------------
                   1928:  */
                   1929: static int
                   1930: ParseEOF (opened)
                   1931:     int opened;
                   1932: {
                   1933:     IFile     *ifile;  /* the state on the top of the includes stack */
                   1934:
                   1935:     if (Lst_IsEmpty (includes)) {
                   1936:        return (DONE);
                   1937:     }
                   1938:
                   1939:     ifile = (IFile *) Lst_DeQueue (includes);
                   1940:     free ((Address) fname);
                   1941:     fname = ifile->fname;
                   1942:     lineno = ifile->lineno;
                   1943:     if (opened && curFILE)
                   1944:        (void) fclose (curFILE);
                   1945:     if (curPTR) {
                   1946:        free((Address) curPTR->str);
                   1947:        free((Address) curPTR);
                   1948:     }
                   1949:     curFILE = ifile->F;
                   1950:     curPTR = ifile->p;
                   1951:     free ((Address)ifile);
                   1952:     return (CONTINUE);
                   1953: }
                   1954:
                   1955: /*-
                   1956:  *---------------------------------------------------------------------
                   1957:  * ParseReadc  --
1.11      millert  1958:  *     Read a character from the current file
1.1       deraadt  1959:  *
                   1960:  * Results:
                   1961:  *     The character that was read
                   1962:  *
                   1963:  * Side Effects:
                   1964:  *---------------------------------------------------------------------
                   1965:  */
                   1966: static int
                   1967: ParseReadc()
                   1968: {
                   1969:     if (curFILE)
                   1970:        return fgetc(curFILE);
1.11      millert  1971:
1.1       deraadt  1972:     if (curPTR && *curPTR->ptr)
                   1973:        return *curPTR->ptr++;
                   1974:     return EOF;
                   1975: }
                   1976:
                   1977:
                   1978: /*-
                   1979:  *---------------------------------------------------------------------
                   1980:  * ParseUnreadc  --
1.11      millert  1981:  *     Put back a character to the current file
1.1       deraadt  1982:  *
                   1983:  * Results:
                   1984:  *     None.
                   1985:  *
                   1986:  * Side Effects:
                   1987:  *---------------------------------------------------------------------
                   1988:  */
                   1989: static void
                   1990: ParseUnreadc(c)
                   1991:     int c;
                   1992: {
                   1993:     if (curFILE) {
                   1994:        ungetc(c, curFILE);
                   1995:        return;
                   1996:     }
                   1997:     if (curPTR) {
                   1998:        *--(curPTR->ptr) = c;
                   1999:        return;
                   2000:     }
                   2001: }
                   2002:
                   2003:
                   2004: /* ParseSkipLine():
                   2005:  *     Grab the next line
                   2006:  */
                   2007: static char *
                   2008: ParseSkipLine(skip)
                   2009:     int skip;          /* Skip lines that don't start with . */
                   2010: {
                   2011:     char *line;
1.11      millert  2012:     int c, lastc, lineLength = 0;
1.1       deraadt  2013:     Buffer buf;
                   2014:
1.11      millert  2015:     buf = Buf_Init(MAKE_BSIZE);
                   2016:
                   2017:     do {
                   2018:         Buf_Discard(buf, lineLength);
                   2019:         lastc = '\0';
                   2020:
                   2021:         while (((c = ParseReadc()) != '\n' || lastc == '\\')
                   2022:                && c != EOF) {
                   2023:             if (c == '\n') {
                   2024:                 Buf_ReplaceLastByte(buf, (Byte)' ');
                   2025:                 lineno++;
                   2026:
                   2027:                 while ((c = ParseReadc()) == ' ' || c == '\t');
                   2028:
                   2029:                 if (c == EOF)
                   2030:                     break;
                   2031:             }
                   2032:
                   2033:             Buf_AddByte(buf, (Byte)c);
                   2034:             lastc = c;
                   2035:         }
                   2036:
                   2037:         if (c == EOF) {
                   2038:             Parse_Error(PARSE_FATAL, "Unclosed conditional/for loop");
                   2039:             Buf_Destroy(buf, TRUE);
                   2040:             return((char *)NULL);
                   2041:         }
                   2042:
                   2043:         lineno++;
                   2044:         Buf_AddByte(buf, (Byte)'\0');
                   2045:         line = (char *)Buf_GetAll(buf, &lineLength);
                   2046:     } while (skip == 1 && line[0] != '.');
1.1       deraadt  2047:
1.11      millert  2048:     Buf_Destroy(buf, FALSE);
1.1       deraadt  2049:     return line;
                   2050: }
                   2051:
                   2052:
                   2053: /*-
                   2054:  *---------------------------------------------------------------------
                   2055:  * ParseReadLine --
                   2056:  *     Read an entire line from the input file. Called only by Parse_File.
                   2057:  *     To facilitate escaped newlines and what have you, a character is
                   2058:  *     buffered in 'lastc', which is '\0' when no characters have been
                   2059:  *     read. When we break out of the loop, c holds the terminating
                   2060:  *     character and lastc holds a character that should be added to
                   2061:  *     the line (unless we don't read anything but a terminator).
                   2062:  *
                   2063:  * Results:
                   2064:  *     A line w/o its newline
                   2065:  *
                   2066:  * Side Effects:
                   2067:  *     Only those associated with reading a character
                   2068:  *---------------------------------------------------------------------
                   2069:  */
                   2070: static char *
                   2071: ParseReadLine ()
                   2072: {
                   2073:     Buffer       buf;          /* Buffer for current line */
                   2074:     register int  c;           /* the current character */
                   2075:     register int  lastc;       /* The most-recent character */
                   2076:     Boolean      semiNL;       /* treat semi-colons as newlines */
                   2077:     Boolean      ignDepOp;     /* TRUE if should ignore dependency operators
                   2078:                                 * for the purposes of setting semiNL */
                   2079:     Boolean      ignComment;   /* TRUE if should ignore comments (in a
                   2080:                                 * shell command */
                   2081:     char         *line;        /* Result */
                   2082:     char          *ep;         /* to strip trailing blanks */
                   2083:     int                  lineLength;   /* Length of result */
                   2084:
                   2085:     semiNL = FALSE;
                   2086:     ignDepOp = FALSE;
                   2087:     ignComment = FALSE;
                   2088:
                   2089:     /*
                   2090:      * Handle special-characters at the beginning of the line. Either a
                   2091:      * leading tab (shell command) or pound-sign (possible conditional)
                   2092:      * forces us to ignore comments and dependency operators and treat
                   2093:      * semi-colons as semi-colons (by leaving semiNL FALSE). This also
                   2094:      * discards completely blank lines.
                   2095:      */
                   2096:     for (;;) {
                   2097:        c = ParseReadc();
                   2098:
                   2099:        if (c == '\t') {
                   2100:            ignComment = ignDepOp = TRUE;
                   2101:            break;
                   2102:        } else if (c == '\n') {
                   2103:            lineno++;
                   2104:        } else if (c == '#') {
                   2105:            ParseUnreadc(c);
                   2106:            break;
                   2107:        } else {
                   2108:            /*
                   2109:             * Anything else breaks out without doing anything
                   2110:             */
                   2111:            break;
                   2112:        }
                   2113:     }
1.11      millert  2114:
1.1       deraadt  2115:     if (c != EOF) {
                   2116:        lastc = c;
                   2117:        buf = Buf_Init(MAKE_BSIZE);
1.11      millert  2118:
1.1       deraadt  2119:        while (((c = ParseReadc ()) != '\n' || (lastc == '\\')) &&
                   2120:               (c != EOF))
                   2121:        {
                   2122: test_char:
                   2123:            switch(c) {
                   2124:            case '\n':
                   2125:                /*
                   2126:                 * Escaped newline: read characters until a non-space or an
                   2127:                 * unescaped newline and replace them all by a single space.
                   2128:                 * This is done by storing the space over the backslash and
                   2129:                 * dropping through with the next nonspace. If it is a
                   2130:                 * semi-colon and semiNL is TRUE, it will be recognized as a
                   2131:                 * newline in the code below this...
                   2132:                 */
                   2133:                lineno++;
                   2134:                lastc = ' ';
                   2135:                while ((c = ParseReadc ()) == ' ' || c == '\t') {
                   2136:                    continue;
                   2137:                }
                   2138:                if (c == EOF || c == '\n') {
                   2139:                    goto line_read;
                   2140:                } else {
                   2141:                    /*
                   2142:                     * Check for comments, semiNL's, etc. -- easier than
                   2143:                     * ParseUnreadc(c); continue;
                   2144:                     */
                   2145:                    goto test_char;
                   2146:                }
                   2147:                /*NOTREACHED*/
                   2148:                break;
                   2149:
                   2150:            case ';':
                   2151:                /*
                   2152:                 * Semi-colon: Need to see if it should be interpreted as a
                   2153:                 * newline
                   2154:                 */
                   2155:                if (semiNL) {
                   2156:                    /*
                   2157:                     * To make sure the command that may be following this
                   2158:                     * semi-colon begins with a tab, we push one back into the
                   2159:                     * input stream. This will overwrite the semi-colon in the
                   2160:                     * buffer. If there is no command following, this does no
                   2161:                     * harm, since the newline remains in the buffer and the
                   2162:                     * whole line is ignored.
                   2163:                     */
                   2164:                    ParseUnreadc('\t');
                   2165:                    goto line_read;
1.11      millert  2166:                }
1.1       deraadt  2167:                break;
                   2168:            case '=':
                   2169:                if (!semiNL) {
                   2170:                    /*
                   2171:                     * Haven't seen a dependency operator before this, so this
                   2172:                     * must be a variable assignment -- don't pay attention to
                   2173:                     * dependency operators after this.
                   2174:                     */
                   2175:                    ignDepOp = TRUE;
                   2176:                } else if (lastc == ':' || lastc == '!') {
                   2177:                    /*
                   2178:                     * Well, we've seen a dependency operator already, but it
                   2179:                     * was the previous character, so this is really just an
                   2180:                     * expanded variable assignment. Revert semi-colons to
                   2181:                     * being just semi-colons again and ignore any more
                   2182:                     * dependency operators.
                   2183:                     *
                   2184:                     * XXX: Note that a line like "foo : a:=b" will blow up,
                   2185:                     * but who'd write a line like that anyway?
                   2186:                     */
                   2187:                    ignDepOp = TRUE; semiNL = FALSE;
                   2188:                }
                   2189:                break;
                   2190:            case '#':
                   2191:                if (!ignComment) {
1.2       deraadt  2192:                    if (
                   2193: #if 0
                   2194:                    compatMake &&
                   2195: #endif
                   2196:                    (lastc != '\\')) {
1.1       deraadt  2197:                        /*
                   2198:                         * If the character is a hash mark and it isn't escaped
                   2199:                         * (or we're being compatible), the thing is a comment.
                   2200:                         * Skip to the end of the line.
                   2201:                         */
                   2202:                        do {
                   2203:                            c = ParseReadc();
                   2204:                        } while ((c != '\n') && (c != EOF));
                   2205:                        goto line_read;
                   2206:                    } else {
                   2207:                        /*
                   2208:                         * Don't add the backslash. Just let the # get copied
                   2209:                         * over.
                   2210:                         */
                   2211:                        lastc = c;
                   2212:                        continue;
                   2213:                    }
                   2214:                }
                   2215:                break;
                   2216:            case ':':
                   2217:            case '!':
                   2218:                if (!ignDepOp && (c == ':' || c == '!')) {
                   2219:                    /*
                   2220:                     * A semi-colon is recognized as a newline only on
                   2221:                     * dependency lines. Dependency lines are lines with a
                   2222:                     * colon or an exclamation point. Ergo...
                   2223:                     */
                   2224:                    semiNL = TRUE;
                   2225:                }
                   2226:                break;
                   2227:            }
                   2228:            /*
                   2229:             * Copy in the previous character and save this one in lastc.
                   2230:             */
                   2231:            Buf_AddByte (buf, (Byte)lastc);
                   2232:            lastc = c;
1.11      millert  2233:
1.1       deraadt  2234:        }
                   2235:     line_read:
                   2236:        lineno++;
1.11      millert  2237:
1.1       deraadt  2238:        if (lastc != '\0') {
                   2239:            Buf_AddByte (buf, (Byte)lastc);
                   2240:        }
                   2241:        Buf_AddByte (buf, (Byte)'\0');
                   2242:        line = (char *)Buf_GetAll (buf, &lineLength);
                   2243:        Buf_Destroy (buf, FALSE);
                   2244:
                   2245:        /*
                   2246:         * Strip trailing blanks and tabs from the line.
                   2247:         * Do not strip a blank or tab that is preceeded by
                   2248:         * a '\'
                   2249:         */
                   2250:        ep = line;
                   2251:        while (*ep)
                   2252:            ++ep;
1.11      millert  2253:        while (ep > line + 1 && (ep[-1] == ' ' || ep[-1] == '\t')) {
1.1       deraadt  2254:            if (ep > line + 1 && ep[-2] == '\\')
                   2255:                break;
                   2256:            --ep;
                   2257:        }
                   2258:        *ep = 0;
1.11      millert  2259:
1.1       deraadt  2260:        if (line[0] == '.') {
                   2261:            /*
                   2262:             * The line might be a conditional. Ask the conditional module
                   2263:             * about it and act accordingly
                   2264:             */
                   2265:            switch (Cond_Eval (line)) {
                   2266:            case COND_SKIP:
                   2267:                /*
                   2268:                 * Skip to next conditional that evaluates to COND_PARSE.
                   2269:                 */
                   2270:                do {
                   2271:                    free (line);
                   2272:                    line = ParseSkipLine(1);
                   2273:                } while (line && Cond_Eval(line) != COND_PARSE);
                   2274:                if (line == NULL)
                   2275:                    break;
                   2276:                /*FALLTHRU*/
                   2277:            case COND_PARSE:
                   2278:                free ((Address) line);
                   2279:                line = ParseReadLine();
                   2280:                break;
                   2281:            case COND_INVALID:
                   2282:                if (For_Eval(line)) {
                   2283:                    int ok;
                   2284:                    free(line);
                   2285:                    do {
                   2286:                        /*
                   2287:                         * Skip after the matching end
                   2288:                         */
                   2289:                        line = ParseSkipLine(0);
                   2290:                        if (line == NULL) {
1.11      millert  2291:                            Parse_Error (PARSE_FATAL,
1.1       deraadt  2292:                                     "Unexpected end of file in for loop.\n");
                   2293:                            break;
                   2294:                        }
                   2295:                        ok = For_Eval(line);
                   2296:                        free(line);
                   2297:                    }
                   2298:                    while (ok);
                   2299:                    if (line != NULL)
                   2300:                        For_Run();
                   2301:                    line = ParseReadLine();
                   2302:                }
                   2303:                break;
                   2304:            }
                   2305:        }
                   2306:        return (line);
                   2307:
                   2308:     } else {
                   2309:        /*
                   2310:         * Hit end-of-file, so return a NULL line to indicate this.
                   2311:         */
                   2312:        return((char *)NULL);
                   2313:     }
                   2314: }
                   2315:
                   2316: /*-
                   2317:  *-----------------------------------------------------------------------
                   2318:  * ParseFinishLine --
                   2319:  *     Handle the end of a dependency group.
                   2320:  *
                   2321:  * Results:
                   2322:  *     Nothing.
                   2323:  *
                   2324:  * Side Effects:
                   2325:  *     inLine set FALSE. 'targets' list destroyed.
                   2326:  *
                   2327:  *-----------------------------------------------------------------------
                   2328:  */
                   2329: static void
                   2330: ParseFinishLine()
                   2331: {
                   2332:     if (inLine) {
                   2333:        Lst_ForEach(targets, Suff_EndTransform, (ClientData)NULL);
                   2334:        Lst_Destroy (targets, ParseHasCommands);
                   2335:        targets = NULL;
                   2336:        inLine = FALSE;
                   2337:     }
                   2338: }
1.11      millert  2339:
1.1       deraadt  2340:
                   2341: /*-
                   2342:  *---------------------------------------------------------------------
                   2343:  * Parse_File --
                   2344:  *     Parse a file into its component parts, incorporating it into the
                   2345:  *     current dependency graph. This is the main function and controls
                   2346:  *     almost every other function in this module
                   2347:  *
                   2348:  * Results:
                   2349:  *     None
                   2350:  *
                   2351:  * Side Effects:
                   2352:  *     Loads. Nodes are added to the list of all targets, nodes and links
                   2353:  *     are added to the dependency graph. etc. etc. etc.
                   2354:  *---------------------------------------------------------------------
                   2355:  */
                   2356: void
                   2357: Parse_File(name, stream)
                   2358:     char          *name;       /* the name of the file being read */
                   2359:     FILE *       stream;       /* Stream open to makefile to parse */
                   2360: {
                   2361:     register char *cp,         /* pointer into the line */
                   2362:                   *line;       /* the line we're working on */
                   2363:
                   2364:     inLine = FALSE;
                   2365:     fname = name;
                   2366:     curFILE = stream;
                   2367:     lineno = 0;
                   2368:     fatals = 0;
                   2369:
                   2370:     do {
                   2371:        while ((line = ParseReadLine ()) != NULL) {
                   2372:            if (*line == '.') {
                   2373:                /*
                   2374:                 * Lines that begin with the special character are either
                   2375:                 * include or undef directives.
                   2376:                 */
                   2377:                for (cp = line + 1; isspace (*cp); cp++) {
                   2378:                    continue;
                   2379:                }
                   2380:                if (strncmp (cp, "include", 7) == 0) {
                   2381:                    ParseDoInclude (cp + 7);
                   2382:                    goto nextLine;
                   2383:                } else if (strncmp(cp, "undef", 5) == 0) {
                   2384:                    char *cp2;
                   2385:                    for (cp += 5; isspace((unsigned char) *cp); cp++) {
                   2386:                        continue;
                   2387:                    }
                   2388:
                   2389:                    for (cp2 = cp; !isspace((unsigned char) *cp2) &&
                   2390:                                   (*cp2 != '\0'); cp2++) {
                   2391:                        continue;
                   2392:                    }
                   2393:
                   2394:                    *cp2 = '\0';
                   2395:
                   2396:                    Var_Delete(cp, VAR_GLOBAL);
                   2397:                    goto nextLine;
                   2398:                }
                   2399:            }
1.11      millert  2400:            if (*line == '#') {
                   2401:                /* If we're this far, the line must be a comment. */
1.1       deraadt  2402:                goto nextLine;
                   2403:            }
1.11      millert  2404:
1.1       deraadt  2405:            if (*line == '\t') {
                   2406:                /*
                   2407:                 * If a line starts with a tab, it can only hope to be
                   2408:                 * a creation command.
                   2409:                 */
                   2410: #ifndef POSIX
                   2411:            shellCommand:
                   2412: #endif
                   2413:                for (cp = line + 1; isspace (*cp); cp++) {
                   2414:                    continue;
                   2415:                }
                   2416:                if (*cp) {
                   2417:                    if (inLine) {
                   2418:                        /*
                   2419:                         * So long as it's not a blank line and we're actually
                   2420:                         * in a dependency spec, add the command to the list of
1.11      millert  2421:                         * commands of all targets in the dependency spec
1.1       deraadt  2422:                         */
                   2423:                        Lst_ForEach (targets, ParseAddCmd, cp);
                   2424:                        Lst_AtEnd(targCmds, (ClientData) line);
                   2425:                        continue;
                   2426:                    } else {
                   2427:                        Parse_Error (PARSE_FATAL,
1.10      briggs   2428:                                     "Unassociated shell command \"%s\"",
1.1       deraadt  2429:                                     cp);
                   2430:                    }
                   2431:                }
                   2432: #ifdef SYSVINCLUDE
1.11      millert  2433:            } else if (strncmp (line, "include", 7) == 0 &&
1.6       tholo    2434:                       isspace((unsigned char) line[7]) &&
1.1       deraadt  2435:                       strchr(line, ':') == NULL) {
                   2436:                /*
                   2437:                 * It's an S3/S5-style "include".
                   2438:                 */
                   2439:                ParseTraditionalInclude (line + 7);
                   2440:                goto nextLine;
                   2441: #endif
                   2442:            } else if (Parse_IsVar (line)) {
                   2443:                ParseFinishLine();
                   2444:                Parse_DoVar (line, VAR_GLOBAL);
                   2445:            } else {
                   2446:                /*
                   2447:                 * We now know it's a dependency line so it needs to have all
                   2448:                 * variables expanded before being parsed. Tell the variable
                   2449:                 * module to complain if some variable is undefined...
                   2450:                 * To make life easier on novices, if the line is indented we
                   2451:                 * first make sure the line has a dependency operator in it.
                   2452:                 * If it doesn't have an operator and we're in a dependency
                   2453:                 * line's script, we assume it's actually a shell command
                   2454:                 * and add it to the current list of targets.
                   2455:                 */
                   2456: #ifndef POSIX
                   2457:                Boolean nonSpace = FALSE;
                   2458: #endif
1.11      millert  2459:
1.1       deraadt  2460:                cp = line;
                   2461:                if (isspace((unsigned char) line[0])) {
                   2462:                    while ((*cp != '\0') && isspace((unsigned char) *cp)) {
                   2463:                        cp++;
                   2464:                    }
                   2465:                    if (*cp == '\0') {
                   2466:                        goto nextLine;
                   2467:                    }
                   2468: #ifndef POSIX
                   2469:                    while ((*cp != ':') && (*cp != '!') && (*cp != '\0')) {
                   2470:                        nonSpace = TRUE;
                   2471:                        cp++;
                   2472:                    }
                   2473: #endif
                   2474:                }
1.11      millert  2475:
1.1       deraadt  2476: #ifndef POSIX
                   2477:                if (*cp == '\0') {
                   2478:                    if (inLine) {
                   2479:                        Parse_Error (PARSE_WARNING,
                   2480:                                     "Shell command needs a leading tab");
                   2481:                        goto shellCommand;
                   2482:                    } else if (nonSpace) {
                   2483:                        Parse_Error (PARSE_FATAL, "Missing operator");
                   2484:                    }
                   2485:                } else {
                   2486: #endif
                   2487:                    ParseFinishLine();
                   2488:
                   2489:                    cp = Var_Subst (NULL, line, VAR_CMD, TRUE);
                   2490:                    free (line);
                   2491:                    line = cp;
1.11      millert  2492:
1.1       deraadt  2493:                    /*
1.11      millert  2494:                     * Need a non-circular list for the target nodes
1.1       deraadt  2495:                     */
                   2496:                    if (targets)
                   2497:                        Lst_Destroy(targets, NOFREE);
                   2498:
                   2499:                    targets = Lst_Init (FALSE);
                   2500:                    inLine = TRUE;
1.11      millert  2501:
1.1       deraadt  2502:                    ParseDoDependency (line);
                   2503: #ifndef POSIX
                   2504:                }
                   2505: #endif
                   2506:            }
                   2507:
                   2508:            nextLine:
                   2509:
                   2510:            free (line);
                   2511:        }
                   2512:        /*
1.11      millert  2513:         * Reached EOF, but it may be just EOF of an include file...
1.1       deraadt  2514:         */
                   2515:     } while (ParseEOF(1) == CONTINUE);
                   2516:
                   2517:     /*
                   2518:      * Make sure conditionals are clean
                   2519:      */
                   2520:     Cond_End();
                   2521:
                   2522:     if (fatals) {
                   2523:        fprintf (stderr, "Fatal errors encountered -- cannot continue\n");
                   2524:        exit (1);
                   2525:     }
                   2526: }
                   2527:
                   2528: /*-
                   2529:  *---------------------------------------------------------------------
                   2530:  * Parse_Init --
                   2531:  *     initialize the parsing module
                   2532:  *
                   2533:  * Results:
                   2534:  *     none
                   2535:  *
                   2536:  * Side Effects:
                   2537:  *     the parseIncPath list is initialized...
                   2538:  *---------------------------------------------------------------------
                   2539:  */
                   2540: void
                   2541: Parse_Init ()
                   2542: {
                   2543:     mainNode = NILGNODE;
                   2544:     parseIncPath = Lst_Init (FALSE);
                   2545:     sysIncPath = Lst_Init (FALSE);
                   2546:     includes = Lst_Init (FALSE);
                   2547:     targCmds = Lst_Init (FALSE);
                   2548: }
                   2549:
                   2550: void
                   2551: Parse_End()
                   2552: {
                   2553:     Lst_Destroy(targCmds, (void (*) __P((ClientData))) free);
                   2554:     if (targets)
                   2555:        Lst_Destroy(targets, NOFREE);
                   2556:     Lst_Destroy(sysIncPath, Dir_Destroy);
                   2557:     Lst_Destroy(parseIncPath, Dir_Destroy);
                   2558:     Lst_Destroy(includes, NOFREE);     /* Should be empty now */
                   2559: }
1.11      millert  2560:
1.1       deraadt  2561:
                   2562: /*-
                   2563:  *-----------------------------------------------------------------------
                   2564:  * Parse_MainName --
                   2565:  *     Return a Lst of the main target to create for main()'s sake. If
                   2566:  *     no such target exists, we Punt with an obnoxious error message.
                   2567:  *
                   2568:  * Results:
                   2569:  *     A Lst of the single node to create.
                   2570:  *
                   2571:  * Side Effects:
                   2572:  *     None.
                   2573:  *
                   2574:  *-----------------------------------------------------------------------
                   2575:  */
                   2576: Lst
                   2577: Parse_MainName()
                   2578: {
1.14      millert  2579:     Lst           listmain;    /* result list */
1.1       deraadt  2580:
1.14      millert  2581:     listmain = Lst_Init (FALSE);
1.1       deraadt  2582:
                   2583:     if (mainNode == NILGNODE) {
1.8       deraadt  2584:        Punt ("no target to make.");
1.1       deraadt  2585:        /*NOTREACHED*/
                   2586:     } else if (mainNode->type & OP_DOUBLEDEP) {
1.14      millert  2587:        (void) Lst_AtEnd (listmain, (ClientData)mainNode);
                   2588:        Lst_Concat(listmain, mainNode->cohorts, LST_CONCNEW);
1.1       deraadt  2589:     }
                   2590:     else
1.14      millert  2591:        (void) Lst_AtEnd (listmain, (ClientData)mainNode);
                   2592:     return (listmain);
1.1       deraadt  2593: }