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

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