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

1.59      espie       1: /*     $OpenPackages$ */
1.94    ! espie       2: /*     $OpenBSD: parse.c,v 1.93 2007/09/18 08:24:56 espie Exp $        */
1.13      millert     3: /*     $NetBSD: parse.c,v 1.29 1997/03/10 21:20:04 christos Exp $      */
1.1       deraadt     4:
                      5: /*
1.59      espie       6:  * Copyright (c) 1999 Marc Espie.
                      7:  *
                      8:  * Extensive code changes for the OpenBSD project.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
                     20:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
                     21:  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
                     22:  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
                     23:  * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
                     24:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
                     25:  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     26:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     27:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     28:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
                     29:  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     30:  */
                     31: /*
1.11      millert    32:  * Copyright (c) 1988, 1989, 1990, 1993
                     33:  *     The Regents of the University of California.  All rights reserved.
1.1       deraadt    34:  * Copyright (c) 1989 by Berkeley Softworks
                     35:  * All rights reserved.
                     36:  *
                     37:  * This code is derived from software contributed to Berkeley by
                     38:  * Adam de Boor.
                     39:  *
                     40:  * Redistribution and use in source and binary forms, with or without
                     41:  * modification, are permitted provided that the following conditions
                     42:  * are met:
                     43:  * 1. Redistributions of source code must retain the above copyright
                     44:  *    notice, this list of conditions and the following disclaimer.
                     45:  * 2. Redistributions in binary form must reproduce the above copyright
                     46:  *    notice, this list of conditions and the following disclaimer in the
                     47:  *    documentation and/or other materials provided with the distribution.
1.68      millert    48:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    49:  *    may be used to endorse or promote products derived from this software
                     50:  *    without specific prior written permission.
                     51:  *
                     52:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     53:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     54:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     55:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     56:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     57:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     58:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     59:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     60:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     61:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     62:  * SUCH DAMAGE.
                     63:  */
                     64:
1.59      espie      65: #include <assert.h>
1.61      espie      66: #include <ctype.h>
1.1       deraadt    67: #include <stdio.h>
1.62      espie      68: #include <stdlib.h>
1.61      espie      69: #include <string.h>
                     70: #include "config.h"
                     71: #include "defines.h"
1.1       deraadt    72: #include "dir.h"
1.81      espie      73: #include "direxpand.h"
1.1       deraadt    74: #include "job.h"
                     75: #include "buf.h"
1.61      espie      76: #include "for.h"
1.50      espie      77: #include "lowparse.h"
1.61      espie      78: #include "arch.h"
                     79: #include "cond.h"
                     80: #include "suff.h"
                     81: #include "parse.h"
                     82: #include "var.h"
                     83: #include "targ.h"
                     84: #include "error.h"
                     85: #include "str.h"
                     86: #include "main.h"
                     87: #include "gnode.h"
                     88: #include "memory.h"
                     89: #include "extern.h"
                     90: #include "lst.h"
                     91: #include "parsevar.h"
1.63      espie      92: #include "stats.h"
                     93: #include "garray.h"
1.92      espie      94: #include "node_int.h"
                     95: #include "nodehashconsts.h"
1.63      espie      96:
                     97:
1.92      espie      98: /* gsources and gtargets should be local to some functions, but they're
                     99:  * set as persistent arrays for performance reasons.
                    100:  */
1.63      espie     101: static struct growableArray gsources, gtargets;
                    102: #define SOURCES_SIZE   128
                    103: #define TARGETS_SIZE   32
1.61      espie     104:
1.89      espie     105: static LIST    theUserIncPath;/* list of directories for "..." includes */
1.61      espie     106: static LIST    theSysIncPath;  /* list of directories for <...> includes */
1.89      espie     107: Lst systemIncludePath = &theSysIncPath;
                    108: Lst userIncludePath = &theUserIncPath;
1.59      espie     109:
1.20      espie     110: #ifdef CLEANUP
1.59      espie     111: static LIST        targCmds;   /* command lines for targets */
1.20      espie     112: #endif
1.59      espie     113:
1.1       deraadt   114: static GNode       *mainNode;  /* The main target to create. This is the
                    115:                                 * first target on the first dependency
                    116:                                 * line in the first makefile */
                    117: /*-
1.92      espie     118:  * specType contains the special TYPE of the current target. It is
                    119:  * SPECIAL_NONE if the target is unspecial. If it *is* special, however,
                    120:  * the children are linked as children of the parent but not vice versa.
                    121:  * This variable is set in ParseDoDependency
1.1       deraadt   122:  */
                    123:
1.92      espie     124: static int specType;
1.3       deraadt   125: static int waiting;
1.1       deraadt   126:
                    127: /*
1.33      espie     128:  * Predecessor node for handling .ORDER. Initialized to NULL when .ORDER
1.1       deraadt   129:  * seen, then set to each successive source on the line.
                    130:  */
                    131: static GNode   *predecessor;
                    132:
1.59      espie     133: static void ParseLinkSrc(GNode *, GNode *);
1.92      espie     134: static int ParseDoOp(GNode **, int);
1.63      espie     135: static int ParseAddDep(GNode *, GNode *);
1.92      espie     136: static void ParseDoSrc(struct growableArray *, struct growableArray *, int,
                    137:     const char *, const char *);
1.59      espie     138: static int ParseFindMain(void *, void *);
                    139: static void ParseClearPath(void *);
1.77      espie     140:
1.92      espie     141: static void add_target_node(const char *, const char *);
                    142: static void add_target_nodes(const char *, const char *);
                    143: static void apply_op(struct growableArray *, int, GNode *);
                    144: static void ParseDoDependency(const char *);
1.59      espie     145: static void ParseAddCmd(void *, void *);
                    146: static void ParseHasCommands(void *);
1.79      espie     147: static bool handle_poison(const char *);
                    148: static bool handle_for_loop(Buffer, const char *);
                    149: static bool handle_undef(const char *);
1.61      espie     150: #define ParseReadLoopLine(linebuf) Parse_ReadUnparsedLine(linebuf, "for loop")
1.79      espie     151: static bool handle_bsd_command(Buffer, Buffer, const char *);
1.59      espie     152: static char *strip_comments(Buffer, const char *);
1.79      espie     153: static char *resolve_include_filename(const char *, bool);
                    154: static void handle_include_file(const char *, const char *, bool, bool);
                    155: static bool lookup_bsd_include(const char *);
                    156: static void lookup_sysv_style_include(const char *, const char *, bool);
                    157: static void lookup_sysv_include(const char *, const char *);
                    158: static void lookup_conditional_include(const char *, const char *);
1.91      espie     159: static bool parse_as_special_line(Buffer, Buffer, const char *);
1.92      espie     160:
                    161: static const char *parse_do_targets(Lst, int *, const char *);
1.91      espie     162: static void parse_target_line(struct growableArray *, const char *,
                    163:     const char *);
1.1       deraadt   164:
1.92      espie     165: static void finish_commands(struct growableArray *);
                    166: static void parse_commands(struct growableArray *, const char *);
                    167: static void create_special_nodes(void);
                    168: static bool found_delimiter(const char *);
                    169: static int handle_special_targets(Lst);
1.93      espie     170: static void dump_targets(void);
1.92      espie     171:
                    172: #define        SPECIAL_EXEC            4
                    173: #define SPECIAL_IGNORE         5
                    174: #define SPECIAL_INCLUDES       6
                    175: #define        SPECIAL_INVISIBLE       8
                    176: #define SPECIAL_JOIN           9
                    177: #define SPECIAL_LIBS           10
                    178: #define SPECIAL_MADE           11
                    179: #define SPECIAL_MAIN           12
                    180: #define SPECIAL_MAKE           13
                    181: #define SPECIAL_MFLAGS         14
                    182: #define        SPECIAL_NOTMAIN         15
                    183: #define        SPECIAL_NOTPARALLEL     16
                    184: #define        SPECIAL_NULL            17
                    185: #define        SPECIAL_OPTIONAL        18
                    186: #define SPECIAL_ORDER          19
                    187: #define SPECIAL_PARALLEL       20
                    188: #define SPECIAL_PHONY          22
                    189: #define SPECIAL_PRECIOUS       23
                    190: #define SPECIAL_SILENT         25
                    191: #define SPECIAL_SINGLESHELL    26
                    192: #define SPECIAL_SUFFIXES       27
                    193: #define        SPECIAL_USE             28
                    194: #define SPECIAL_WAIT           29
                    195: #define SPECIAL_NOPATH         30
                    196: #define SPECIAL_ERROR          31
                    197:
                    198:
                    199: #define P(k) k, sizeof(k), K_##k
                    200:
                    201: static struct {
                    202:        const char *keyword;
                    203:        size_t sz;
                    204:        uint32_t hv;
                    205:        int type;
                    206:        int special_op;
                    207: } specials[] = {
                    208:     { P(NODE_EXEC),    SPECIAL_EXEC | SPECIAL_TARGETSOURCE,    OP_EXEC, },
                    209:     { P(NODE_IGNORE),  SPECIAL_IGNORE | SPECIAL_TARGETSOURCE,  OP_IGNORE, },
                    210:     { P(NODE_INCLUDES),        SPECIAL_INCLUDES | SPECIAL_TARGET,      0, },
                    211:     { P(NODE_INVISIBLE),SPECIAL_INVISIBLE | SPECIAL_TARGETSOURCE,OP_INVISIBLE, },
                    212:     { P(NODE_JOIN),    SPECIAL_JOIN | SPECIAL_TARGETSOURCE,    OP_JOIN, },
                    213:     { P(NODE_LIBS),    SPECIAL_LIBS | SPECIAL_TARGET,          0, },
                    214:     { P(NODE_MADE),    SPECIAL_MADE | SPECIAL_TARGETSOURCE,    OP_MADE, },
                    215:     { P(NODE_MAIN),    SPECIAL_MAIN | SPECIAL_TARGET,          0, },
                    216:     { P(NODE_MAKE),    SPECIAL_MAKE | SPECIAL_TARGETSOURCE,    OP_MAKE, },
                    217:     { P(NODE_MAKEFLAGS),       SPECIAL_MFLAGS | SPECIAL_TARGET,        0, },
                    218:     { P(NODE_MFLAGS),  SPECIAL_MFLAGS | SPECIAL_TARGET,        0, },
                    219:     { P(NODE_NOTMAIN), SPECIAL_NOTMAIN | SPECIAL_TARGETSOURCE, OP_NOTMAIN, },
                    220:     { P(NODE_NOTPARALLEL),SPECIAL_NOTPARALLEL | SPECIAL_TARGET,        0, },
                    221:     { P(NODE_NO_PARALLEL),SPECIAL_NOTPARALLEL | SPECIAL_TARGET,        0, },
                    222:     { P(NODE_NULL),    SPECIAL_NULL | SPECIAL_TARGET,          0, },
                    223:     { P(NODE_OPTIONAL),        SPECIAL_OPTIONAL | SPECIAL_TARGETSOURCE,OP_OPTIONAL, },
                    224:     { P(NODE_ORDER),   SPECIAL_ORDER | SPECIAL_TARGET,         0, },
                    225:     { P(NODE_PARALLEL),        SPECIAL_PARALLEL | SPECIAL_TARGET,      0, },
                    226:     { P(NODE_PATH),    SPECIAL_PATH | SPECIAL_TARGET,          0, },
                    227:     { P(NODE_PHONY),   SPECIAL_PHONY | SPECIAL_TARGETSOURCE,   OP_PHONY, },
                    228:     { P(NODE_PRECIOUS),        SPECIAL_PRECIOUS | SPECIAL_TARGETSOURCE,OP_PRECIOUS, },
                    229:     { P(NODE_RECURSIVE),SPECIAL_MAKE | SPECIAL_TARGETSOURCE,   OP_MAKE, },
                    230:     { P(NODE_SILENT),  SPECIAL_SILENT | SPECIAL_TARGETSOURCE,  OP_SILENT, },
                    231:     { P(NODE_SINGLESHELL),SPECIAL_SINGLESHELL | SPECIAL_TARGET,        0, },
                    232:     { P(NODE_SUFFIXES),        SPECIAL_SUFFIXES | SPECIAL_TARGET,      0, },
                    233:     { P(NODE_USE),     SPECIAL_USE | SPECIAL_TARGETSOURCE,     OP_USE, },
                    234:     { P(NODE_WAIT),    SPECIAL_WAIT | SPECIAL_TARGETSOURCE,    0 },
                    235: #if 0
                    236:        { P(NODE_NOPATH),       SPECIAL_NOPATH, },
                    237: #endif
                    238: };
                    239:
                    240: #undef P
                    241:
                    242: static void
                    243: create_special_nodes()
1.1       deraadt   244: {
1.92      espie     245:        unsigned int i;
                    246:
                    247:        for (i = 0; i < sizeof(specials)/sizeof(specials[0]); i++) {
                    248:                GNode *gn = Targ_FindNodeh(specials[i].keyword,
                    249:                    specials[i].sz, specials[i].hv, TARG_CREATE);
                    250:                gn->special = specials[i].type;
                    251:                gn->special_op = specials[i].special_op;
1.1       deraadt   252:        }
                    253: }
1.92      espie     254:
1.1       deraadt   255: /*-
                    256:  *---------------------------------------------------------------------
                    257:  * ParseLinkSrc  --
1.59      espie     258:  *     Link the parent node to its new child. Used by
1.1       deraadt   259:  *     ParseDoDependency. If the specType isn't 'Not', the parent
                    260:  *     isn't linked as a parent of the child.
                    261:  *
                    262:  * Side Effects:
                    263:  *     New elements are added to the parents list of cgn and the
                    264:  *     children list of cgn. the unmade field of pgn is updated
                    265:  *     to reflect the additional child.
                    266:  *---------------------------------------------------------------------
                    267:  */
1.41      espie     268: static void
1.94    ! espie     269: ParseLinkSrc(GNode *pgn, GNode *cgn)
        !           270: {
        !           271:        if (Lst_AddNew(&pgn->children, cgn)) {
        !           272:                if (specType == SPECIAL_NONE)
        !           273:                        Lst_AtEnd(&cgn->parents, pgn);
        !           274:                pgn->unmade++;
        !           275:        }
1.1       deraadt   276: }
                    277:
                    278: /*-
                    279:  *---------------------------------------------------------------------
                    280:  * ParseDoOp  --
                    281:  *     Apply the parsed operator to the given target node. Used in a
1.92      espie     282:  *     Array_Find call by ParseDoDependency once all targets have
1.1       deraadt   283:  *     been found and their operator parsed. If the previous and new
                    284:  *     operators are incompatible, a major error is taken.
                    285:  *
                    286:  * Side Effects:
                    287:  *     The type field of the node is altered to reflect any new bits in
                    288:  *     the op.
                    289:  *---------------------------------------------------------------------
                    290:  */
                    291: static int
1.94    ! espie     292: ParseDoOp(GNode **gnp, int op)
        !           293: {
        !           294:        GNode *gn = *gnp;
        !           295:        /*
        !           296:         * If the dependency mask of the operator and the node don't match and
        !           297:         * the node has actually had an operator applied to it before, and the
        !           298:         * operator actually has some dependency information in it, complain.
        !           299:         */
        !           300:        if (((op & OP_OPMASK) != (gn->type & OP_OPMASK)) &&
        !           301:            !OP_NOP(gn->type) && !OP_NOP(op)) {
        !           302:                Parse_Error(PARSE_FATAL, "Inconsistent operator for %s",
        !           303:                    gn->name);
        !           304:                return 0;
        !           305:        }
        !           306:
        !           307:        if (op == OP_DOUBLEDEP && ((gn->type & OP_OPMASK) == OP_DOUBLEDEP)) {
        !           308:                /* If the node was the object of a :: operator, we need to
        !           309:                 * create a new instance of it for the children and commands on
        !           310:                 * this dependency line. The new instance is placed on the
        !           311:                 * 'cohorts' list of the initial one (note the initial one is
        !           312:                 * not on its own cohorts list) and the new instance is linked
        !           313:                 * to all parents of the initial instance.  */
        !           314:                GNode *cohort;
        !           315:                LstNode ln;
        !           316:
        !           317:                cohort = Targ_NewGN(gn->name);
        !           318:                /* Duplicate links to parents so graph traversal is simple.
        !           319:                 * Perhaps some type bits should be duplicated?
        !           320:                 *
        !           321:                 * Make the cohort invisible as well to avoid duplicating it
        !           322:                 * into other variables. True, parents of this target won't
        !           323:                 * tend to do anything with their local variables, but better
        !           324:                 * safe than sorry.  */
        !           325:                for (ln = Lst_First(&gn->parents); ln != NULL; ln = Lst_Adv(ln))
        !           326:                        ParseLinkSrc((GNode *)Lst_Datum(ln), cohort);
        !           327:                cohort->type = OP_DOUBLEDEP|OP_INVISIBLE;
        !           328:                Lst_AtEnd(&gn->cohorts, cohort);
        !           329:
        !           330:                /* Replace the node in the targets list with the new copy */
        !           331:                *gnp = cohort;
        !           332:                gn = cohort;
        !           333:        }
        !           334:        /* We don't want to nuke any previous flags (whatever they were) so we
        !           335:         * just OR the new operator into the old.  */
        !           336:        gn->type |= op;
        !           337:        return 1;
1.1       deraadt   338: }
                    339:
1.11      millert   340: /*-
1.3       deraadt   341:  *---------------------------------------------------------------------
1.59      espie     342:  * ParseAddDep --
1.3       deraadt   343:  *     Check if the pair of GNodes given needs to be synchronized.
                    344:  *     This has to be when two nodes are on different sides of a
                    345:  *     .WAIT directive.
                    346:  *
                    347:  * Results:
1.40      espie     348:  *     Returns 0 if the two targets need to be ordered, 1 otherwise.
1.59      espie     349:  *     If it returns 0, the search can stop.
1.3       deraadt   350:  *
                    351:  * Side Effects:
                    352:  *     A dependency can be added between the two nodes.
1.11      millert   353:  *
1.3       deraadt   354:  *---------------------------------------------------------------------
                    355:  */
1.13      millert   356: static int
1.69      espie     357: ParseAddDep(GNode *p, GNode *s)
1.3       deraadt   358: {
1.94    ! espie     359:        if (p->order < s->order) {
        !           360:                /* XXX: This can cause loops, and loops can cause unmade
        !           361:                 * targets, but checking is tedious, and the debugging output
        !           362:                 * can show the problem.  */
        !           363:                Lst_AtEnd(&p->successors, s);
        !           364:                Lst_AtEnd(&s->preds, p);
        !           365:                return 1;
        !           366:        } else
        !           367:                return 0;
1.92      espie     368: }
                    369:
                    370: static void
                    371: apply_op(struct growableArray *targets, int op, GNode *gn)
                    372: {
                    373:        if (op)
                    374:                gn->type |= op;
1.86      espie     375:        else
1.92      espie     376:                Array_ForEach(targets, ParseLinkSrc, gn);
1.3       deraadt   377: }
                    378:
1.1       deraadt   379: /*-
                    380:  *---------------------------------------------------------------------
                    381:  * ParseDoSrc  --
                    382:  *     Given the name of a source, figure out if it is an attribute
                    383:  *     and apply it to the targets if it is. Else decide if there is
                    384:  *     some attribute which should be applied *to* the source because
                    385:  *     of some special target and apply it if so. Otherwise, make the
                    386:  *     source be a child of the targets in the list 'targets'
                    387:  *
                    388:  * Side Effects:
                    389:  *     Operator bits may be added to the list of targets or to the source.
                    390:  *     The targets may have a new source added to their lists of children.
                    391:  *---------------------------------------------------------------------
                    392:  */
                    393: static void
1.69      espie     394: ParseDoSrc(
1.92      espie     395:     struct growableArray *targets,
                    396:     struct growableArray *sources,
1.69      espie     397:     int        tOp,    /* operator (if any) from special targets */
1.92      espie     398:     const char *src,   /* name of the source to handle */
                    399:     const char *esrc)
1.1       deraadt   400: {
1.92      espie     401:        GNode *gn = Targ_FindNodei(src, esrc, TARG_CREATE);
                    402:        if ((gn->special & SPECIAL_SOURCE) != 0) {
                    403:                if (gn->special_op) {
                    404:                        Array_FindP(targets, ParseDoOp, gn->special_op);
                    405:                        return;
                    406:                } else {
                    407:                        assert((gn->special & SPECIAL_MASK) == SPECIAL_WAIT);
                    408:                        waiting++;
                    409:                        return;
1.86      espie     410:                }
1.1       deraadt   411:        }
1.3       deraadt   412:
1.86      espie     413:        switch (specType) {
1.92      espie     414:        case SPECIAL_MAIN:
1.86      espie     415:                /*
                    416:                 * If we have noted the existence of a .MAIN, it means we need
                    417:                 * to add the sources of said target to the list of things
1.92      espie     418:                 * to create.  Note that this will only be invoked if the user
                    419:                 * didn't specify a target on the command line. This is to
                    420:                 * allow #ifmake's to succeed, or something...
1.86      espie     421:                 */
1.92      espie     422:                Lst_AtEnd(create, gn->name);
1.86      espie     423:                /*
                    424:                 * Add the name to the .TARGETS variable as well, so the user
                    425:                 * can employ that, if desired.
                    426:                 */
1.92      espie     427:                Var_Append(".TARGETS", gn->name);
1.86      espie     428:                return;
1.3       deraadt   429:
1.92      espie     430:        case SPECIAL_ORDER:
1.86      espie     431:                /*
                    432:                 * Create proper predecessor/successor links between the
                    433:                 * previous source and the current one.
                    434:                 */
                    435:                if (predecessor != NULL) {
                    436:                        Lst_AtEnd(&predecessor->successors, gn);
                    437:                        Lst_AtEnd(&gn->preds, predecessor);
                    438:                }
                    439:                predecessor = gn;
                    440:                break;
1.3       deraadt   441:
1.86      espie     442:        default:
                    443:                /*
                    444:                 * In the case of a source that was the object of a :: operator,
                    445:                 * the attribute is applied to all of its instances (as kept in
                    446:                 * the 'cohorts' list of the node) or all the cohorts are linked
                    447:                 * to all the targets.
                    448:                 */
1.92      espie     449:                apply_op(targets, tOp, gn);
1.86      espie     450:                if ((gn->type & OP_OPMASK) == OP_DOUBLEDEP) {
                    451:                        LstNode ln;
                    452:
1.92      espie     453:                        for (ln=Lst_First(&gn->cohorts); ln != NULL;
1.86      espie     454:                            ln = Lst_Adv(ln)){
1.92      espie     455:                                apply_op(targets, tOp,
                    456:                                    (GNode *)Lst_Datum(ln));
1.86      espie     457:                        }
1.1       deraadt   458:                }
1.86      espie     459:                break;
1.1       deraadt   460:        }
1.3       deraadt   461:
1.86      espie     462:        gn->order = waiting;
1.92      espie     463:        Array_AtEnd(sources, gn);
                    464:        if (waiting)
                    465:                Array_Find(sources, ParseAddDep, gn);
1.1       deraadt   466: }
                    467:
                    468: /*-
                    469:  *-----------------------------------------------------------------------
                    470:  * ParseFindMain --
                    471:  *     Find a real target in the list and set it to be the main one.
                    472:  *     Called by ParseDoDependency when a main target hasn't been found
                    473:  *     yet.
                    474:  *
                    475:  * Results:
1.40      espie     476:  *     1 if main not found yet, 0 if it is.
1.1       deraadt   477:  *
                    478:  * Side Effects:
1.92      espie     479:  *     mainNode is changed and.
1.1       deraadt   480:  *-----------------------------------------------------------------------
                    481:  */
                    482: static int
1.94    ! espie     483: ParseFindMain(void *gnp, void *dummy UNUSED)
        !           484: {
        !           485:        GNode *gn = (GNode *)gnp;
        !           486:        if ((gn->type & OP_NOTARGET) == 0 && gn->special == SPECIAL_NONE) {
        !           487:                mainNode = gn;
        !           488:                return 0;
        !           489:        } else {
        !           490:                return 1;
        !           491:        }
1.1       deraadt   492: }
                    493:
                    494: /*-
                    495:  *-----------------------------------------------------------------------
                    496:  * ParseClearPath --
1.59      espie     497:  *     Reinit path to an empty path
1.1       deraadt   498:  *-----------------------------------------------------------------------
                    499:  */
1.41      espie     500: static void
1.69      espie     501: ParseClearPath(void *p)
1.1       deraadt   502: {
1.94    ! espie     503:        Lst path = (Lst)p;
1.59      espie     504:
1.94    ! espie     505:        Lst_Destroy(path, Dir_Destroy);
        !           506:        Lst_Init(path);
1.1       deraadt   507: }
                    508:
1.77      espie     509: static void
1.92      espie     510: add_target_node(const char *line, const char *end)
1.77      espie     511: {
                    512:        GNode *gn;
                    513:
1.92      espie     514:        gn = Suff_ParseAsTransform(line, end);
                    515:
                    516:        if (gn == NULL) {
                    517:                gn = Targ_FindNodei(line, end, TARG_CREATE);
                    518:                gn->type &= ~OP_DUMMY;
                    519:        }
1.77      espie     520:
                    521:        if (gn != NULL)
                    522:                Array_AtEnd(&gtargets, gn);
                    523: }
                    524:
                    525: static void
1.92      espie     526: add_target_nodes(const char *line, const char *end)
1.77      espie     527: {
                    528:
1.92      espie     529:        if (Dir_HasWildcardsi(line, end)) {
1.77      espie     530:                /*
                    531:                 * Targets are to be sought only in the current directory,
                    532:                 * so create an empty path for the thing. Note we need to
                    533:                 * use Dir_Destroy in the destruction of the path as the
                    534:                 * Dir module could have added a directory to the path...
                    535:                 */
                    536:                char *targName;
                    537:                LIST emptyPath;
                    538:                LIST curTargs;
                    539:
                    540:                Lst_Init(&emptyPath);
                    541:                Lst_Init(&curTargs);
1.92      espie     542:                Dir_Expandi(line, end, &emptyPath, &curTargs);
1.77      espie     543:                Lst_Destroy(&emptyPath, Dir_Destroy);
                    544:                while ((targName = (char *)Lst_DeQueue(&curTargs)) != NULL) {
1.92      espie     545:                        add_target_node(targName, targName + strlen(targName));
1.77      espie     546:                }
                    547:                Lst_Destroy(&curTargs, NOFREE);
                    548:        } else {
1.92      espie     549:                add_target_node(line, end);
                    550:        }
                    551: }
                    552:
                    553: /* special target line check: a proper delimiter is a ':' or '!', but
                    554:  * we don't want to end a target on such a character if there is a better
                    555:  * match later on.
                    556:  * By "better" I mean one that is followed by whitespace. This allows the
                    557:  * user to have targets like:
                    558:  *    fie::fi:fo: fum
                    559:  * where "fie::fi:fo" is the target.  In real life this is used for perl5
                    560:  * library man pages where "::" separates an object from its class.  Ie:
                    561:  * "File::Spec::Unix".
                    562:  * This behaviour is also consistent with other versions of make.
                    563:  */
                    564: static bool
                    565: found_delimiter(const char *s)
                    566: {
                    567:        if (*s == '!' || *s == ':') {
                    568:                const char *p = s + 1;
                    569:
                    570:                if (*s == ':' && *p == ':')
                    571:                        p++;
                    572:
                    573:                /* Found the best match already. */
                    574:                if (isspace(*p) || *p == '\0')
                    575:                        return true;
                    576:
                    577:                do {
                    578:                        p += strcspn(p, "!:");
                    579:                        if (*p == '\0')
                    580:                            break;
                    581:                        p++;
                    582:                } while (!isspace(*p));
                    583:
                    584:                /* No better match later on... */
                    585:                if (*p == '\0')
                    586:                        return true;
                    587:        }
                    588:        return false;
                    589: }
                    590:
                    591: static const char *
                    592: parse_do_targets(Lst paths, int *op, const char *line)
                    593: {
                    594:        const char *cp;
                    595:
                    596:        do {
                    597:                for (cp = line; *cp && !isspace(*cp) && *cp != '(';) {
                    598:                        if (*cp == '$')
                    599:                                /* Must be a dynamic source (would have been
                    600:                                 * expanded otherwise), so call the Var module
                    601:                                 * to parse the puppy so we can safely advance
                    602:                                 * beyond it...There should be no errors in
                    603:                                 * this, as they would have been discovered in
                    604:                                 * the initial Var_Subst and we wouldn't be
                    605:                                 * here.  */
                    606:                                Var_ParseSkip(&cp, NULL);
                    607:                        else {
                    608:                                if (found_delimiter(cp))
                    609:                                        break;
                    610:                                cp++;
                    611:                        }
                    612:                }
                    613:
                    614:                if (*cp == '(') {
                    615:                        LIST temp;
                    616:                        Lst_Init(&temp);
                    617:                        /* Archives must be handled specially to make sure the
                    618:                         * OP_ARCHV flag is set in their 'type' field, for one
                    619:                         * thing, and because things like "archive(file1.o
                    620:                         * file2.o file3.o)" are permissible.
                    621:                         * Arch_ParseArchive will set 'line' to be the first
                    622:                         * non-blank after the archive-spec. It creates/finds
                    623:                         * nodes for the members and places them on the given
                    624:                         * list, returning true if all went well and false if
                    625:                         * there was an error in the specification. On error,
                    626:                         * line should remain untouched.  */
                    627:                        if (!Arch_ParseArchive(&line, &temp, NULL)) {
                    628:                                Parse_Error(PARSE_FATAL,
                    629:                                     "Error in archive specification: \"%s\"",
                    630:                                     line);
                    631:                                return NULL;
                    632:                        } else {
                    633:                                AppendList2Array(&temp, &gtargets);
                    634:                                Lst_Destroy(&temp, NOFREE);
                    635:                                cp = line;
                    636:                                continue;
                    637:                        }
                    638:                }
                    639:                if (*cp == '\0') {
                    640:                        /* Ending a dependency line without an operator is a
                    641:                         * Bozo no-no */
                    642:                        Parse_Error(PARSE_FATAL, "Need an operator");
                    643:                        return NULL;
                    644:                }
                    645:                /*
                    646:                 * Have word in line. Get or create its nodes and stick it at
                    647:                 * the end of the targets list
                    648:                 */
                    649:                if (*line != '\0')
                    650:                        add_target_nodes(line, cp);
                    651:
                    652:                while (isspace(*cp))
                    653:                        cp++;
                    654:                line = cp;
                    655:        } while (*line != '!' && *line != ':' && *line);
                    656:        *op = handle_special_targets(paths);
                    657:        return cp;
                    658: }
                    659:
                    660: static void
                    661: dump_targets()
                    662: {
                    663:        size_t i;
                    664:        for (i = 0; i < gtargets.n; i++)
                    665:                fprintf(stderr, "%s", gtargets.a[i]->name);
                    666:        fprintf(stderr, "\n");
                    667: }
                    668:
                    669: static int
                    670: handle_special_targets(Lst paths)
                    671: {
                    672:        size_t i;
                    673:        int seen_path = 0;
                    674:        int seen_special = 0;
                    675:        int seen_normal = 0;
                    676:        int type;
                    677:
                    678:        for (i = 0; i < gtargets.n; i++) {
                    679:                type = gtargets.a[i]->special;
                    680:                if ((type & SPECIAL_MASK) == SPECIAL_PATH) {
                    681:                        seen_path++;
                    682:                        Lst_AtEnd(paths, find_suffix_path(gtargets.a[i]));
                    683:                } else if ((type & SPECIAL_TARGET) != 0)
                    684:                        seen_special++;
                    685:                else
                    686:                        seen_normal++;
                    687:        }
                    688:        if ((seen_path != 0) + (seen_special != 0) + (seen_normal != 0) > 1) {
                    689:                Parse_Error(PARSE_FATAL, "Wrong mix of special targets");
                    690:                dump_targets();
                    691:                specType = SPECIAL_ERROR;
                    692:                return 0;
                    693:        }
                    694:        if (seen_normal != 0) {
                    695:                specType = SPECIAL_NONE;
                    696:                return 0;
                    697:        }
                    698:        else if (seen_path != 0) {
                    699:                specType = SPECIAL_PATH;
                    700:                return 0;
                    701:        } else if (seen_special == 0) {
                    702:                specType = SPECIAL_NONE;
                    703:                return 0;
                    704:        } else if (seen_special != 1) {
1.94    ! espie     705:                Parse_Error(PARSE_FATAL,
        !           706:                    "Mixing special targets is not allowed");
1.92      espie     707:                dump_targets();
                    708:                return 0;
                    709:        } else if (seen_special == 1) {
                    710:                specType = gtargets.a[0]->special & SPECIAL_MASK;
                    711:                switch (specType) {
                    712:                case SPECIAL_MAIN:
                    713:                        if (!Lst_IsEmpty(create)) {
                    714:                                specType = SPECIAL_NONE;
                    715:                        }
                    716:                        break;
                    717:                case SPECIAL_NOTPARALLEL:
                    718:                {
                    719:                        extern int  maxJobs;
                    720:
                    721:                        maxJobs = 1;
                    722:                        break;
                    723:                }
                    724:                case SPECIAL_SINGLESHELL:
                    725:                        compatMake = 1;
                    726:                        break;
                    727:                case SPECIAL_ORDER:
                    728:                        predecessor = NULL;
                    729:                        break;
                    730:                default:
                    731:                        break;
                    732:                }
                    733:                return gtargets.a[0]->special_op;
                    734:        } else {
                    735:                /* we're allowed to have 0 target */
                    736:                specType = SPECIAL_NONE;
                    737:                return 0;
1.77      espie     738:        }
                    739: }
                    740:
1.1       deraadt   741: /*-
                    742:  *---------------------------------------------------------------------
                    743:  * ParseDoDependency  --
                    744:  *     Parse the dependency line in line.
                    745:  *
                    746:  * Side Effects:
                    747:  *     The nodes of the sources are linked as children to the nodes of the
                    748:  *     targets. Some nodes may be created.
                    749:  *
                    750:  *     We parse a dependency line by first extracting words from the line and
                    751:  * finding nodes in the list of all targets with that name. This is done
                    752:  * until a character is encountered which is an operator character. Currently
                    753:  * these are only ! and :. At this point the operator is parsed and the
                    754:  * pointer into the line advanced until the first source is encountered.
1.59      espie     755:  *     The parsed operator is applied to each node in the 'targets' list,
1.1       deraadt   756:  * which is where the nodes found for the targets are kept, by means of
                    757:  * the ParseDoOp function.
                    758:  *     The sources are read in much the same way as the targets were except
                    759:  * that now they are expanded using the wildcarding scheme of the C-Shell
                    760:  * and all instances of the resulting words in the list of all targets
                    761:  * are found. Each of the resulting nodes is then linked to each of the
                    762:  * targets as one of its children.
                    763:  *     Certain targets are handled specially. These are the ones detailed
                    764:  * by the specType variable.
                    765:  *     The storing of transformation rules is also taken care of here.
                    766:  * A target is recognized as a transformation rule by calling
                    767:  * Suff_IsTransform. If it is a transformation rule, its node is gotten
                    768:  * from the suffix module via Suff_AddTransform rather than the standard
                    769:  * Targ_FindNode in the target module.
                    770:  *---------------------------------------------------------------------
                    771:  */
                    772: static void
1.92      espie     773: ParseDoDependency(const char *line)    /* the line to parse */
1.1       deraadt   774: {
1.92      espie     775:        const char *cp;         /* our current position */
                    776:        int op;                 /* the operator on the line */
                    777:        LIST paths;             /* List of search paths to alter when parsing
                    778:                                * a list of .PATH targets */
                    779:        int tOp;                /* operator from special target */
1.59      espie     780:
                    781:
1.92      espie     782:        waiting = 0;
                    783:        Lst_Init(&paths);
1.59      espie     784:
1.92      espie     785:        Array_Reset(&gsources);
1.1       deraadt   786:
1.92      espie     787:        cp = parse_do_targets(&paths, &tOp, line);
                    788:        if (cp == NULL || specType == SPECIAL_ERROR)
                    789:                return;
1.1       deraadt   790:
1.92      espie     791:        /* Have now parsed all the target names. Must parse the operator next.
                    792:         * The result is left in op .  */
                    793:        if (*cp == '!') {
                    794:                op = OP_FORCE;
                    795:        } else if (*cp == ':') {
                    796:                if (cp[1] == ':') {
                    797:                        op = OP_DOUBLEDEP;
                    798:                        cp++;
                    799:                } else {
                    800:                        op = OP_DEPENDS;
1.1       deraadt   801:                }
1.92      espie     802:        } else {
                    803:                Parse_Error(PARSE_FATAL, "Missing dependency operator");
1.1       deraadt   804:                return;
1.92      espie     805:        }
                    806:
                    807:        cp++;                   /* Advance beyond operator */
1.11      millert   808:
1.92      espie     809:        Array_FindP(&gtargets, ParseDoOp, op);
1.1       deraadt   810:
1.92      espie     811:        /*
                    812:         * Get to the first source
                    813:         */
                    814:        while (isspace(*cp))
                    815:                cp++;
                    816:        line = cp;
                    817:
                    818:        /*
                    819:         * Several special targets take different actions if present with no
                    820:         * sources:
                    821:         *      a .SUFFIXES line with no sources clears out all old suffixes
                    822:         *      a .PRECIOUS line makes all targets precious
                    823:         *      a .IGNORE line ignores errors for all targets
                    824:         *      a .SILENT line creates silence when making all targets
                    825:         *      a .PATH removes all directories from the search path(s).
                    826:         */
                    827:        if (!*line) {
1.1       deraadt   828:                switch (specType) {
1.92      espie     829:                case SPECIAL_SUFFIXES:
                    830:                        Suff_ClearSuffixes();
1.1       deraadt   831:                        break;
1.92      espie     832:                case SPECIAL_PRECIOUS:
                    833:                        allPrecious = true;
1.1       deraadt   834:                        break;
1.92      espie     835:                case SPECIAL_IGNORE:
                    836:                        ignoreErrors = true;
1.1       deraadt   837:                        break;
1.92      espie     838:                case SPECIAL_SILENT:
                    839:                        beSilent = true;
1.1       deraadt   840:                        break;
1.92      espie     841:                case SPECIAL_PATH:
                    842:                        Lst_Every(&paths, ParseClearPath);
1.1       deraadt   843:                        break;
1.92      espie     844:                default:
1.1       deraadt   845:                        break;
                    846:                }
1.92      espie     847:        } else if (specType == SPECIAL_MFLAGS) {
                    848:                /*Call on functions in main.c to deal with these arguments */
                    849:                Main_ParseArgLine(line);
                    850:                return;
                    851:        } else if (specType == SPECIAL_NOTPARALLEL ||
                    852:            specType == SPECIAL_SINGLESHELL) {
                    853:                return;
1.1       deraadt   854:        }
1.11      millert   855:
1.1       deraadt   856:        /*
1.92      espie     857:         * NOW GO FOR THE SOURCES
1.1       deraadt   858:         */
1.92      espie     859:        if (specType == SPECIAL_SUFFIXES || specType == SPECIAL_PATH ||
                    860:            specType == SPECIAL_INCLUDES || specType == SPECIAL_LIBS ||
                    861:            specType == SPECIAL_NULL) {
                    862:                while (*line) {
                    863:                    /*
                    864:                     * If the target was one that doesn't take files as its
                    865:                     * sources but takes something like suffixes, we take each
                    866:                     * space-separated word on the line as a something and deal
                    867:                     * with it accordingly.
                    868:                     *
                    869:                     * If the target was .SUFFIXES, we take each source as a
                    870:                     * suffix and add it to the list of suffixes maintained by
                    871:                     * the Suff module.
                    872:                     *
                    873:                     * If the target was a .PATH, we add the source as a
                    874:                     * directory to search on the search path.
                    875:                     *
                    876:                     * If it was .INCLUDES, the source is taken to be the
                    877:                     * suffix of files which will be #included and whose search
                    878:                     * path should be present in the .INCLUDES variable.
                    879:                     *
                    880:                     * If it was .LIBS, the source is taken to be the suffix of
                    881:                     * files which are considered libraries and whose search
                    882:                     * path should be present in the .LIBS variable.
                    883:                     *
                    884:                     * If it was .NULL, the source is the suffix to use when a
                    885:                     * file has no valid suffix.
                    886:                     */
                    887:                    while (*cp && !isspace(*cp))
                    888:                            cp++;
                    889:                    switch (specType) {
                    890:                    case SPECIAL_SUFFIXES:
                    891:                            Suff_AddSuffixi(line, cp);
                    892:                            break;
                    893:                    case SPECIAL_PATH:
                    894:                            {
                    895:                            LstNode ln;
                    896:
                    897:                            for (ln = Lst_First(&paths); ln != NULL;
                    898:                                ln = Lst_Adv(ln))
                    899:                                    Dir_AddDiri((Lst)Lst_Datum(ln), line, cp);
                    900:                            break;
                    901:                            }
                    902:                    case SPECIAL_INCLUDES:
                    903:                            Suff_AddIncludei(line, cp);
                    904:                            break;
                    905:                    case SPECIAL_LIBS:
                    906:                            Suff_AddLibi(line, cp);
                    907:                            break;
                    908:                    case SPECIAL_NULL:
                    909:                            Suff_SetNulli(line, cp);
                    910:                            break;
                    911:                    default:
                    912:                            break;
                    913:                    }
                    914:                    if (*cp != '\0')
                    915:                        cp++;
                    916:                    while (isspace(*cp))
                    917:                        cp++;
                    918:                    line = cp;
1.1       deraadt   919:                }
1.92      espie     920:                Lst_Destroy(&paths, NOFREE);
1.1       deraadt   921:        } else {
1.92      espie     922:                while (*line) {
                    923:                        /*
                    924:                         * The targets take real sources, so we must beware of
                    925:                         * archive specifications (i.e. things with left
                    926:                         * parentheses in them) and handle them accordingly.
                    927:                         */
                    928:                        while (*cp && !isspace(*cp)) {
                    929:                                if (*cp == '(' && cp > line && cp[-1] != '$') {
                    930:                                        /*
                    931:                                         * Only stop for a left parenthesis if
                    932:                                         * it isn't at the start of a word
                    933:                                         * (that'll be for variable changes
                    934:                                         * later) and isn't preceded by a
                    935:                                         * dollar sign (a dynamic source).
                    936:                                         */
                    937:                                        break;
                    938:                                } else {
                    939:                                        cp++;
                    940:                                }
                    941:                        }
1.1       deraadt   942:
1.92      espie     943:                        if (*cp == '(') {
                    944:                                GNode *gn;
                    945:                                LIST sources;   /* list of archive source
                    946:                                                 * names after expansion */
1.1       deraadt   947:
1.92      espie     948:                                Lst_Init(&sources);
                    949:                                if (!Arch_ParseArchive(&line, &sources, NULL)) {
                    950:                                        Parse_Error(PARSE_FATAL,
                    951:                                            "Error in source archive spec \"%s\"",
                    952:                                            line);
                    953:                                        return;
                    954:                                }
1.1       deraadt   955:
1.92      espie     956:                                while ((gn = (GNode *)Lst_DeQueue(&sources)) !=
                    957:                                    NULL)
                    958:                                        ParseDoSrc(&gtargets, &gsources, tOp,
                    959:                                            gn->name, NULL);
                    960:                                cp = line;
                    961:                        } else {
                    962:                                const char *endSrc = cp;
1.1       deraadt   963:
1.92      espie     964:                                ParseDoSrc(&gtargets, &gsources, tOp, line,
                    965:                                    endSrc);
                    966:                                if (*cp)
                    967:                                        cp++;
                    968:                        }
                    969:                        while (isspace(*cp))
                    970:                                cp++;
                    971:                        line = cp;
                    972:                }
1.1       deraadt   973:        }
1.11      millert   974:
1.92      espie     975:        if (mainNode == NULL) {
                    976:                /* If we have yet to decide on a main target to make, in the
                    977:                 * absence of any user input, we want the first target on
                    978:                 * the first dependency line that is actually a real target
                    979:                 * (i.e. isn't a .USE or .EXEC rule) to be made.  */
                    980:                Array_Find(&gtargets, ParseFindMain, NULL);
1.1       deraadt   981:        }
                    982: }
                    983:
                    984: /*-
1.59      espie     985:  * ParseAddCmd --
1.1       deraadt   986:  *     Lst_ForEach function to add a command line to all targets
                    987:  *
                    988:  * Side Effects:
                    989:  *     A new element is added to the commands list of the node.
                    990:  */
1.41      espie     991: static void
1.94    ! espie     992: ParseAddCmd(void *gnp, void *cmd)
1.1       deraadt   993: {
1.86      espie     994:        GNode *gn = (GNode *)gnp;
                    995:        /* if target already supplied, ignore commands */
                    996:        if (!(gn->type & OP_HAS_COMMANDS)) {
                    997:                Lst_AtEnd(&gn->commands, cmd);
                    998:                if (!gn->lineno) {
                    999:                        gn->lineno = Parse_Getlineno();
                   1000:                        gn->fname = Parse_Getfilename();
                   1001:                }
1.39      espie    1002:        }
1.1       deraadt  1003: }
                   1004:
                   1005: /*-
                   1006:  *-----------------------------------------------------------------------
                   1007:  * ParseHasCommands --
                   1008:  *     Callback procedure for Parse_File when destroying the list of
                   1009:  *     targets on the last dependency line. Marks a target as already
                   1010:  *     having commands if it does, to keep from having shell commands
                   1011:  *     on multiple dependency lines.
                   1012:  *
                   1013:  * Side Effects:
                   1014:  *     OP_HAS_COMMANDS may be set for the target.
                   1015:  *-----------------------------------------------------------------------
                   1016:  */
                   1017: static void
1.69      espie    1018: ParseHasCommands(void *gnp)        /* Node to examine */
1.1       deraadt  1019: {
1.86      espie    1020:        GNode *gn = (GNode *)gnp;
1.92      espie    1021:        if (!Lst_IsEmpty(&gn->commands))
1.86      espie    1022:                gn->type |= OP_HAS_COMMANDS;
1.1       deraadt  1023: }
                   1024:
1.79      espie    1025:
1.91      espie    1026: /* Strip comments from line. Build a copy in buffer if necessary, */
                   1027: static char *
                   1028: strip_comments(Buffer copy, const char *line)
                   1029: {
                   1030:        const char *comment;
                   1031:        const char *p;
                   1032:
                   1033:        comment = strchr(line, '#');
                   1034:        assert(comment != line);
                   1035:        if (comment == NULL)
                   1036:                return (char *)line;
                   1037:        else {
                   1038:                Buf_Reset(copy);
                   1039:
                   1040:                for (p = line; *p != '\0'; p++) {
                   1041:                        if (*p == '\\') {
                   1042:                                if (p[1] == '#') {
                   1043:                                        Buf_Addi(copy, line, p);
                   1044:                                        Buf_AddChar(copy, '#');
                   1045:                                        line = p+2;
                   1046:                                }
                   1047:                                if (p[1] != '\0')
                   1048:                                        p++;
                   1049:                        } else if (*p == '#')
                   1050:                                break;
                   1051:                }
                   1052:                Buf_Addi(copy, line, p);
                   1053:                Buf_KillTrailingSpaces(copy);
                   1054:                return Buf_Retrieve(copy);
                   1055:        }
                   1056: }
                   1057:
                   1058:
                   1059:
1.79      espie    1060: /***
                   1061:  *** Support for various include constructs
                   1062:  ***/
                   1063:
                   1064:
1.1       deraadt  1065: void
1.79      espie    1066: Parse_AddIncludeDir(const char *dir)
1.1       deraadt  1067: {
1.89      espie    1068:        Dir_AddDir(userIncludePath, dir);
1.1       deraadt  1069: }
                   1070:
1.79      espie    1071: static char *
                   1072: resolve_include_filename(const char *file, bool isSystem)
1.1       deraadt  1073: {
1.79      espie    1074:        char *fullname;
1.87      espie    1075:
1.79      espie    1076:        /* Look up system files on the system path first */
                   1077:        if (isSystem) {
1.89      espie    1078:                fullname = Dir_FindFileNoDot(file, systemIncludePath);
1.79      espie    1079:                if (fullname)
                   1080:                        return fullname;
                   1081:        }
                   1082:
                   1083:        /* Handle non-system non-absolute files... */
                   1084:        if (!isSystem && file[0] != '/') {
1.85      espie    1085:                /* ... by looking first under the same directory as the
1.79      espie    1086:                 * current file */
                   1087:                char *slash;
                   1088:                const char *fname;
                   1089:
                   1090:                fname = Parse_Getfilename();
                   1091:
                   1092:                slash = strrchr(fname, '/');
                   1093:                if (slash != NULL) {
                   1094:                        char *newName;
                   1095:
1.85      espie    1096:                        newName = Str_concati(fname, slash, file,
1.79      espie    1097:                            strchr(file, '\0'), '/');
1.89      espie    1098:                        fullname = Dir_FindFile(newName, userIncludePath);
1.79      espie    1099:                        if (fullname == NULL)
1.83      espie    1100:                                fullname = Dir_FindFile(newName, defaultPath);
1.79      espie    1101:                        free(newName);
                   1102:                        if (fullname)
                   1103:                                return fullname;
                   1104:                }
                   1105:        }
1.1       deraadt  1106:
1.79      espie    1107:        /* Now look first on the -I search path, then on the .PATH
                   1108:         * search path, if not found in a -I directory.
                   1109:         * XXX: Suffix specific?  */
1.89      espie    1110:        fullname = Dir_FindFile(file, userIncludePath);
1.79      espie    1111:        if (fullname)
                   1112:                return fullname;
1.83      espie    1113:        fullname = Dir_FindFile(file, defaultPath);
1.79      espie    1114:        if (fullname)
                   1115:                return fullname;
1.1       deraadt  1116:
1.79      espie    1117:        /* Still haven't found the makefile. Look for it on the system
                   1118:         * path as a last resort (if we haven't already). */
                   1119:        if (isSystem)
                   1120:                return NULL;
                   1121:        else
1.89      espie    1122:                return Dir_FindFile(file, systemIncludePath);
1.1       deraadt  1123: }
                   1124:
                   1125: static void
1.85      espie    1126: handle_include_file(const char *name, const char *ename, bool isSystem,
1.79      espie    1127:     bool errIfNotFound)
1.1       deraadt  1128: {
1.79      espie    1129:        char *file;
                   1130:        char *fullname;
                   1131:
                   1132:        /* Substitute for any variables in the file name before trying to
                   1133:         * find the thing. */
                   1134:        file = Var_Substi(name, ename, NULL, false);
                   1135:
                   1136:        fullname = resolve_include_filename(file, isSystem);
                   1137:        if (fullname == NULL && errIfNotFound)
                   1138:                Parse_Error(PARSE_FATAL, "Could not find %s", file);
                   1139:        free(file);
                   1140:
1.1       deraadt  1141:
1.79      espie    1142:        if (fullname != NULL) {
                   1143:                FILE *f;
1.59      espie    1144:
1.79      espie    1145:                f = fopen(fullname, "r");
                   1146:                if (f == NULL && errIfNotFound)
                   1147:                        Parse_Error(PARSE_FATAL, "Cannot open %s", fullname);
                   1148:                else
                   1149:                        Parse_FromFile(fullname, f);
                   1150:        }
1.59      espie    1151: }
                   1152:
1.79      espie    1153: /* .include <file> (system) or .include "file" (normal) */
                   1154: static bool
                   1155: lookup_bsd_include(const char *file)
1.59      espie    1156: {
1.85      espie    1157:        char endc;
1.79      espie    1158:        const char *efile;
                   1159:        bool isSystem;
                   1160:
                   1161:        /* find starting delimiter */
                   1162:        while (isspace(*file))
                   1163:                file++;
                   1164:
                   1165:        /* determine type of file */
                   1166:        if (*file == '<') {
                   1167:                isSystem = true;
                   1168:                endc = '>';
                   1169:        } else if (*file == '"') {
                   1170:                isSystem = false;
                   1171:                endc = '"';
                   1172:        } else {
                   1173:                Parse_Error(PARSE_WARNING,
                   1174:                    ".include filename must be delimited by '\"' or '<'");
                   1175:                return false;
                   1176:        }
1.1       deraadt  1177:
1.79      espie    1178:        /* delimit file name between file and efile */
                   1179:        for (efile = ++file; *efile != endc; efile++) {
                   1180:                if (*efile == '\0') {
                   1181:                        Parse_Error(PARSE_WARNING,
                   1182:                             "Unclosed .include filename. '%c' expected", endc);
                   1183:                        return false;
                   1184:                }
                   1185:        }
                   1186:        handle_include_file(file, efile, isSystem, true);
                   1187:        return true;
                   1188: }
1.1       deraadt  1189:
                   1190:
1.79      espie    1191: static void
1.85      espie    1192: lookup_sysv_style_include(const char *file, const char *directive,
1.79      espie    1193:     bool errIfMissing)
1.50      espie    1194: {
1.79      espie    1195:        const char *efile;
1.50      espie    1196:
1.79      espie    1197:        /* find beginning of name */
                   1198:        while (isspace(*file))
                   1199:                file++;
                   1200:        if (*file == '\0') {
                   1201:                Parse_Error(PARSE_FATAL, "Filename missing from \"%s\"",
                   1202:                    directive);
                   1203:                return;
1.50      espie    1204:        }
1.79      espie    1205:        /* sys5 delimits file up to next blank character or end of line */
                   1206:        for (efile = file; *efile != '\0' && !isspace(*efile);)
                   1207:                efile++;
1.1       deraadt  1208:
1.79      espie    1209:        handle_include_file(file, efile, true, errIfMissing);
1.70      espie    1210: }
                   1211:
1.79      espie    1212:
                   1213: /* system V construct:  include file */
1.70      espie    1214: static void
1.79      espie    1215: lookup_sysv_include(const char *file, const char *directive)
1.70      espie    1216: {
1.79      espie    1217:        lookup_sysv_style_include(file, directive, true);
                   1218: }
1.1       deraadt  1219:
                   1220:
1.79      espie    1221: /* sinclude file and -include file */
                   1222: static void
                   1223: lookup_conditional_include(const char *file, const char *directive)
                   1224: {
                   1225:        lookup_sysv_style_include(file, directive, false);
1.1       deraadt  1226: }
                   1227:
1.59      espie    1228:
1.91      espie    1229: /***
                   1230:  ***   BSD-specific . constructs
                   1231:  ***   They all follow the same pattern:
                   1232:  ***    if the syntax matches BSD stuff, then we're committed to handle
                   1233:  ***   them and report fatal errors (like, include file not existing)
                   1234:  ***    otherwise, we return false, and hope somebody else will handle it.
                   1235:  ***/
                   1236:
1.79      espie    1237: static bool
                   1238: handle_poison(const char *line)
1.72      espie    1239: {
1.79      espie    1240:        const char *p = line;
1.72      espie    1241:        int type = POISON_NORMAL;
                   1242:        bool not = false;
                   1243:        bool paren_to_match = false;
1.79      espie    1244:        const char *name, *ename;
1.72      espie    1245:
                   1246:        while (isspace(*p))
                   1247:                p++;
                   1248:        if (*p == '!') {
                   1249:                not = true;
                   1250:                p++;
                   1251:        }
                   1252:        while (isspace(*p))
                   1253:                p++;
                   1254:        if (strncmp(p, "defined", 7) == 0) {
                   1255:                type = POISON_DEFINED;
                   1256:                p += 7;
                   1257:        } else if (strncmp(p, "empty", 5) == 0) {
                   1258:                type = POISON_EMPTY;
                   1259:                p += 5;
                   1260:        }
                   1261:        while (isspace(*p))
                   1262:                p++;
                   1263:        if (*p == '(') {
                   1264:                paren_to_match = true;
                   1265:                p++;
                   1266:        }
                   1267:        while (isspace(*p))
                   1268:                p++;
                   1269:        name = ename = p;
                   1270:        while (*p != '\0' && !isspace(*p)) {
                   1271:                if (*p == ')' && paren_to_match) {
                   1272:                        paren_to_match = false;
                   1273:                        p++;
                   1274:                        break;
                   1275:                }
                   1276:                p++;
                   1277:                ename = p;
                   1278:        }
                   1279:        while (isspace(*p))
                   1280:                p++;
                   1281:        switch(type) {
                   1282:        case POISON_NORMAL:
                   1283:        case POISON_EMPTY:
                   1284:                if (not)
                   1285:                        type = POISON_INVALID;
                   1286:                break;
                   1287:        case POISON_DEFINED:
                   1288:                if (not)
                   1289:                        type = POISON_NOT_DEFINED;
                   1290:                else
                   1291:                        type = POISON_INVALID;
                   1292:                break;
                   1293:        }
1.79      espie    1294:        if ((*p != '\0' && *p != '#') || type == POISON_INVALID) {
1.85      espie    1295:                Parse_Error(PARSE_WARNING, "Invalid syntax for .poison: %s",
1.72      espie    1296:                    line);
1.79      espie    1297:                return false;
                   1298:        } else {
                   1299:                Var_MarkPoisoned(name, ename, type);
                   1300:                return true;
                   1301:        }
1.72      espie    1302: }
1.59      espie    1303:
1.92      espie    1304:
1.61      espie    1305: static bool
1.79      espie    1306: handle_for_loop(Buffer linebuf, const char *line)
1.59      espie    1307: {
1.79      espie    1308:        For *loop;
1.59      espie    1309:
1.91      espie    1310:        loop = For_Eval(line);
1.79      espie    1311:        if (loop != NULL) {
                   1312:                bool ok;
                   1313:                do {
                   1314:                        /* Find the matching endfor.  */
                   1315:                        line = ParseReadLoopLine(linebuf);
                   1316:                        if (line == NULL) {
                   1317:                            Parse_Error(PARSE_FATAL,
                   1318:                                 "Unexpected end of file in for loop.\n");
                   1319:                            return false;
                   1320:                        }
                   1321:                        ok = For_Accumulate(loop, line);
                   1322:                } while (ok);
                   1323:                For_Run(loop);
                   1324:                return true;
                   1325:        } else
                   1326:                return false;
                   1327: }
1.59      espie    1328:
1.79      espie    1329: static bool
                   1330: handle_undef(const char *line)
                   1331: {
                   1332:        const char *eline;
1.59      espie    1333:
1.79      espie    1334:        while (isspace(*line))
                   1335:                line++;
                   1336:        for (eline = line; !isspace(*eline) && *eline != '\0';)
                   1337:                eline++;
                   1338:        Var_Deletei(line, eline);
1.61      espie    1339:        return true;
1.79      espie    1340: }
1.67      espie    1341:
1.92      espie    1342: /* global hub for the construct */
1.79      espie    1343: static bool
                   1344: handle_bsd_command(Buffer linebuf, Buffer copy, const char *line)
                   1345: {
                   1346:        char *stripped;
1.59      espie    1347:
1.71      tedu     1348:        while (isspace(*line))
1.79      espie    1349:                line++;
                   1350:
1.91      espie    1351:        /* delegate basic classification to the conditional module */
1.79      espie    1352:        switch (Cond_Eval(line)) {
                   1353:        case COND_SKIP:
                   1354:                /* Skip to next conditional that evaluates to COND_PARSE.  */
                   1355:                do {
                   1356:                        line = Parse_ReadNextConditionalLine(linebuf);
                   1357:                        if (line != NULL) {
                   1358:                                while (isspace(*line))
                   1359:                                        line++;
                   1360:                                        stripped = strip_comments(copy, line);
                   1361:                        }
                   1362:                } while (line != NULL && Cond_Eval(stripped) != COND_PARSE);
                   1363:                /* FALLTHROUGH */
                   1364:        case COND_PARSE:
                   1365:                return true;
1.85      espie    1366:        case COND_ISFOR:
1.91      espie    1367:                return handle_for_loop(linebuf, line + 3);
1.79      espie    1368:        case COND_ISINCLUDE:
                   1369:                return lookup_bsd_include(line + 7);
                   1370:        case COND_ISPOISON:
1.91      espie    1371:                return handle_poison(line + 6);
1.85      espie    1372:        case COND_ISUNDEF:
1.79      espie    1373:                return handle_undef(line + 5);
                   1374:        default:
                   1375:                break;
                   1376:        }
1.67      espie    1377:
1.79      espie    1378:        return false;
1.59      espie    1379: }
                   1380:
1.91      espie    1381: /***
                   1382:  *** handle a group of commands
                   1383:  ***/
                   1384:
1.1       deraadt  1385: static void
1.91      espie    1386: finish_commands(struct growableArray *targets)
1.1       deraadt  1387: {
1.91      espie    1388:        Array_Every(targets, ParseHasCommands);
                   1389:        Array_Reset(targets);
1.1       deraadt  1390: }
1.11      millert  1391:
1.59      espie    1392: static void
1.91      espie    1393: parse_commands(struct growableArray *targets, const char *line)
1.59      espie    1394: {
1.86      espie    1395:        /* add the command to the list of
                   1396:         * commands of all targets in the dependency spec */
                   1397:        char *cmd = estrdup(line);
1.59      espie    1398:
1.91      espie    1399:        Array_ForEach(targets, ParseAddCmd, cmd);
1.59      espie    1400: #ifdef CLEANUP
1.86      espie    1401:        Lst_AtEnd(&targCmds, cmd);
1.59      espie    1402: #endif
                   1403: }
1.1       deraadt  1404:
1.91      espie    1405: static bool
                   1406: parse_as_special_line(Buffer buf, Buffer copy, const char *line)
                   1407: {
                   1408:        if (*line == '.' && handle_bsd_command(buf, copy, line+1))
                   1409:                return true;
                   1410:        if (FEATURES(FEATURE_SYSVINCLUDE) &&
                   1411:            strncmp(line, "include", 7) == 0 &&
                   1412:            isspace(line[7]) &&
                   1413:            strchr(line, ':') == NULL) {
                   1414:            /* It's an S3/S5-style "include".  */
                   1415:                lookup_sysv_include(line + 7, "include");
                   1416:                return true;
                   1417:        }
                   1418:        if (FEATURES(FEATURE_CONDINCLUDE) &&
                   1419:            strncmp(line, "sinclude", 8) == 0 &&
                   1420:            isspace(line[8]) &&
                   1421:            strchr(line, ':') == NULL) {
                   1422:                lookup_conditional_include(line+8, "sinclude");
                   1423:                return true;
                   1424:        }
                   1425:        if (FEATURES(FEATURE_CONDINCLUDE) &&
                   1426:            strncmp(line, "-include", 8) == 0 &&
                   1427:            isspace(line[8]) &&
                   1428:            strchr(line, ':') == NULL) {
                   1429:                lookup_conditional_include(line+8, "-include");
                   1430:                return true;
                   1431:        }
                   1432:        return false;
                   1433: }
1.1       deraadt  1434:
1.91      espie    1435: static void
                   1436: parse_target_line(struct growableArray *targets, const char *line,
                   1437:     const char *stripped)
                   1438: {
                   1439:        size_t pos;
                   1440:        char *end;
                   1441:        char *cp;
                   1442:        char *dep;
                   1443:
                   1444:        /* let's start a new set of commands */
                   1445:        Array_Reset(targets);
                   1446:
                   1447:        /* XXX this is a dirty heuristic to handle target: dep ; commands */
                   1448:        dep = NULL;
                   1449:        /* First we need to find eventual dependencies */
                   1450:        pos = strcspn(stripped, ":!");
                   1451:        /* go over :!, and find ;  */
                   1452:        if (stripped[pos] != '\0' &&
                   1453:            (end = strchr(stripped+pos+1, ';')) != NULL) {
                   1454:                if (line != stripped)
                   1455:                        /* find matching ; in original... The
                   1456:                         * original might be slightly longer.  */
                   1457:                        dep = strchr(line+(end-stripped), ';');
1.59      espie    1458:                else
1.91      espie    1459:                        dep = end;
                   1460:                /* kill end of line. */
                   1461:                *end = '\0';
                   1462:        }
                   1463:        /* We now know it's a dependency line so it needs to
                   1464:         * have all variables expanded before being parsed.
                   1465:         */
                   1466:        cp = Var_Subst(stripped, NULL, false);
                   1467:        ParseDoDependency(cp);
                   1468:        free(cp);
                   1469:
                   1470:        /* Parse dependency if it's not empty. */
                   1471:        if (dep != NULL) {
                   1472:                do {
                   1473:                        dep++;
                   1474:                } while (isspace(*dep));
                   1475:                if (*dep != '\0')
                   1476:                        parse_commands(targets, dep);
                   1477:        }
                   1478: }
                   1479:
                   1480: void
                   1481: Parse_File(const char *filename, FILE *stream)
                   1482: {
                   1483:        char *line;
                   1484:        bool expectingCommands = false;
1.11      millert  1485:
1.91      espie    1486:        /* somewhat permanent spaces to shave time */
                   1487:        BUFFER buf;
                   1488:        BUFFER copy;
                   1489:
                   1490:        Buf_Init(&buf, MAKE_BSIZE);
                   1491:        Buf_Init(&copy, MAKE_BSIZE);
                   1492:
                   1493:        Parse_FromFile(filename, stream);
                   1494:        do {
                   1495:                while ((line = Parse_ReadNormalLine(&buf)) != NULL) {
                   1496:                        if (*line == '\t') {
                   1497:                                if (expectingCommands)
                   1498:                                        parse_commands(&gtargets, line+1);
1.59      espie    1499:                                else
1.91      espie    1500:                                        Parse_Error(PARSE_FATAL,
                   1501:                                            "Unassociated shell command \"%s\"",
                   1502:                                             line);
                   1503:                        } else {
                   1504:                                const char *stripped = strip_comments(&copy,
                   1505:                                    line);
                   1506:                                if (!parse_as_special_line(&buf, &copy,
                   1507:                                    stripped)) {
                   1508:                                        if (expectingCommands)
                   1509:                                                finish_commands(&gtargets);
                   1510:                                        if (Parse_As_Var_Assignment(stripped))
                   1511:                                                expectingCommands = false;
                   1512:                                        else {
                   1513:                                                parse_target_line(&gtargets,
                   1514:                                                    line, stripped);
                   1515:                                                expectingCommands = true;
                   1516:                                        }
                   1517:                                }
1.59      espie    1518:                        }
1.1       deraadt  1519:                }
1.91      espie    1520:        } while (Parse_NextFile());
1.1       deraadt  1521:
1.91      espie    1522:        if (expectingCommands)
                   1523:                finish_commands(&gtargets);
                   1524:        /* Make sure conditionals are clean.  */
                   1525:        Cond_End();
                   1526:
                   1527:        Parse_ReportErrors();
                   1528:        Buf_Destroy(&buf);
                   1529:        Buf_Destroy(&copy);
1.1       deraadt  1530: }
                   1531:
                   1532: void
1.69      espie    1533: Parse_Init(void)
1.1       deraadt  1534: {
1.79      espie    1535:        mainNode = NULL;
1.89      espie    1536:        Static_Lst_Init(userIncludePath);
                   1537:        Static_Lst_Init(systemIncludePath);
1.79      espie    1538:        Array_Init(&gtargets, TARGETS_SIZE);
1.92      espie    1539:        Array_Init(&gsources, SOURCES_SIZE);
                   1540:        create_special_nodes();
1.88      espie    1541:
1.79      espie    1542:        LowParse_Init();
1.20      espie    1543: #ifdef CLEANUP
1.79      espie    1544:        Static_Lst_Init(&targCmds);
1.20      espie    1545: #endif
1.1       deraadt  1546: }
                   1547:
1.61      espie    1548: #ifdef CLEANUP
1.1       deraadt  1549: void
1.69      espie    1550: Parse_End(void)
1.1       deraadt  1551: {
1.79      espie    1552:        Lst_Destroy(&targCmds, (SimpleProc)free);
1.89      espie    1553:        Lst_Destroy(systemIncludePath, Dir_Destroy);
                   1554:        Lst_Destroy(userIncludePath, Dir_Destroy);
1.79      espie    1555:        LowParse_End();
1.61      espie    1556: }
1.20      espie    1557: #endif
1.11      millert  1558:
1.1       deraadt  1559:
1.44      espie    1560: void
1.69      espie    1561: Parse_MainName(Lst listmain)   /* result list */
1.1       deraadt  1562: {
1.94    ! espie    1563:        if (mainNode == NULL) {
        !          1564:                Punt("no target to make.");
        !          1565:                /*NOTREACHED*/
        !          1566:        } else if (mainNode->type & OP_DOUBLEDEP) {
        !          1567:                Lst_AtEnd(listmain, mainNode);
        !          1568:                Lst_Concat(listmain, &mainNode->cohorts);
        !          1569:        }
        !          1570:        else
        !          1571:                Lst_AtEnd(listmain, mainNode);
1.1       deraadt  1572: }