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

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

CVSweb <webmaster@jp.NetBSD.org>