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

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

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

CVSweb <webmaster@jp.NetBSD.org>