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

1.413   ! rillig      1: /*     $NetBSD: parse.c,v 1.412 2020/10/30 15:39:17 rillig 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.402     rillig     71: /*
                     72:  * Parsing of makefiles.
                     73:  *
                     74:  * Parse_File is the main entry point and controls most of the other
                     75:  * functions in this module.
1.1       cgd        76:  *
1.402     rillig     77:  * The directories for the .include "..." directive are kept in
                     78:  * 'parseIncPath', while those for .include <...> are kept in 'sysIncPath'.
                     79:  * The targets currently being defined are kept in 'targets'.
1.1       cgd        80:  *
                     81:  * Interface:
1.402     rillig     82:  *     Parse_Init      Initialize the module
1.338     rillig     83:  *
1.383     rillig     84:  *     Parse_End       Clean up the module
1.338     rillig     85:  *
1.402     rillig     86:  *     Parse_File      Parse a top-level makefile.  Included files are
                     87:  *                     handled by Parse_include_file though.
                     88:  *
                     89:  *     Parse_IsVar     Return TRUE if the given line is a variable
                     90:  *                     assignment. Used by MainParseArgs to determine if
                     91:  *                     an argument is a target or a variable assignment.
                     92:  *                     Used internally for pretty much the same thing.
                     93:  *
                     94:  *     Parse_Error     Report a parse error, a warning or an informational
                     95:  *                     message.
1.338     rillig     96:  *
1.402     rillig     97:  *     Parse_MainName  Returns a list of the main target to create.
1.1       cgd        98:  */
                     99:
1.170     dholland  100: #include <sys/types.h>
                    101: #include <sys/mman.h>
1.171     dholland  102: #include <sys/stat.h>
1.84      wiz       103: #include <errno.h>
1.5       cgd       104: #include <stdarg.h>
1.225     maya      105: #include <stdint.h>
1.84      wiz       106:
1.177     nakayama  107: #ifndef MAP_FILE
                    108: #define MAP_FILE 0
                    109: #endif
1.170     dholland  110: #ifndef MAP_COPY
                    111: #define MAP_COPY MAP_PRIVATE
                    112: #endif
                    113:
1.1       cgd       114: #include "make.h"
1.5       cgd       115: #include "dir.h"
                    116: #include "job.h"
1.1       cgd       117: #include "pathnames.h"
                    118:
1.303     rillig    119: /*     "@(#)parse.c    8.3 (Berkeley) 3/19/94" */
1.413   ! rillig    120: MAKE_RCSID("$NetBSD: parse.c,v 1.412 2020/10/30 15:39:17 rillig Exp $");
1.303     rillig    121:
1.248     rillig    122: /* types and constants */
1.166     dholland  123:
1.1       cgd       124: /*
1.166     dholland  125:  * Structure for a file being read ("included file")
1.1       cgd       126:  */
                    127: typedef struct IFile {
1.338     rillig    128:     char *fname;               /* name of file */
                    129:     Boolean fromForLoop;       /* simulated .include by the .for loop */
                    130:     int lineno;                        /* current line number in file */
                    131:     int first_lineno;          /* line number of start of text */
1.369     rillig    132:     unsigned int cond_depth;   /* 'if' nesting when file opened */
1.338     rillig    133:     Boolean depending;         /* state of doing_depend on EOF */
1.403     rillig    134:
                    135:     /* The buffer from which the file's content is read. */
                    136:     char *buf_freeIt;
                    137:     char *buf_ptr;             /* next char to be read */
                    138:     char *buf_end;
                    139:
1.338     rillig    140:     char *(*nextbuf)(void *, size_t *); /* Function to get more data */
                    141:     void *nextbuf_arg;         /* Opaque arg for nextbuf() */
                    142:     struct loadedfile *lf;     /* loadedfile object, if any */
1.5       cgd       143: } IFile;
1.1       cgd       144:
1.166     dholland  145: /*
                    146:  * Tokens for target attributes
1.1       cgd       147:  */
1.386     rillig    148: typedef enum ParseSpecial {
1.406     rillig    149:     SP_ATTRIBUTE,      /* Generic attribute */
1.404     rillig    150:     SP_BEGIN,          /* .BEGIN */
                    151:     SP_DEFAULT,                /* .DEFAULT */
                    152:     SP_DELETE_ON_ERROR,        /* .DELETE_ON_ERROR */
                    153:     SP_END,            /* .END */
                    154:     SP_ERROR,          /* .ERROR */
                    155:     SP_IGNORE,         /* .IGNORE */
                    156:     SP_INCLUDES,       /* .INCLUDES; not mentioned in the manual page */
                    157:     SP_INTERRUPT,      /* .INTERRUPT */
                    158:     SP_LIBS,           /* .LIBS; not mentioned in the manual page */
1.406     rillig    159:     SP_MAIN,           /* .MAIN and we don't have anything user-specified to
                    160:                         * make */
1.404     rillig    161:     SP_META,           /* .META */
                    162:     SP_MFLAGS,         /* .MFLAGS or .MAKEFLAGS */
                    163:     SP_NOMETA,         /* .NOMETA */
                    164:     SP_NOMETA_CMP,     /* .NOMETA_CMP */
                    165:     SP_NOPATH,         /* .NOPATH */
                    166:     SP_NOT,            /* Not special */
                    167:     SP_NOTPARALLEL,    /* .NOTPARALLEL or .NO_PARALLEL */
                    168:     SP_NULL,           /* .NULL; not mentioned in the manual page */
                    169:     SP_OBJDIR,         /* .OBJDIR */
                    170:     SP_ORDER,          /* .ORDER */
                    171:     SP_PARALLEL,       /* .PARALLEL; not mentioned in the manual page */
                    172:     SP_PATH,           /* .PATH or .PATH.suffix */
                    173:     SP_PHONY,          /* .PHONY */
1.48      sjg       174: #ifdef POSIX
1.404     rillig    175:     SP_POSIX,          /* .POSIX; not mentioned in the manual page */
1.48      sjg       176: #endif
1.404     rillig    177:     SP_PRECIOUS,       /* .PRECIOUS */
                    178:     SP_SHELL,          /* .SHELL */
                    179:     SP_SILENT,         /* .SILENT */
                    180:     SP_SINGLESHELL,    /* .SINGLESHELL; not mentioned in the manual page */
                    181:     SP_STALE,          /* .STALE */
                    182:     SP_SUFFIXES,       /* .SUFFIXES */
1.406     rillig    183:     SP_WAIT            /* .WAIT */
1.1       cgd       184: } ParseSpecial;
                    185:
1.368     rillig    186: typedef List SearchPathList;
1.392     rillig    187: typedef ListNode SearchPathListNode;
1.368     rillig    188:
1.248     rillig    189: /* result data */
1.166     dholland  190:
                    191: /*
                    192:  * The main target to create. This is the first target on the first
                    193:  * dependency line in the first makefile.
                    194:  */
                    195: static GNode *mainNode;
                    196:
1.248     rillig    197: /* eval state */
1.166     dholland  198:
1.336     rillig    199: /* During parsing, the targets from the currently active dependency line,
                    200:  * or NULL if the current line does not belong to a dependency line, for
                    201:  * example because it is a variable assignment.
1.318     rillig    202:  *
                    203:  * See unit-tests/deptgt.mk, keyword "parse.c:targets". */
1.322     rillig    204: static GNodeList *targets;
1.166     dholland  205:
                    206: #ifdef CLEANUP
1.372     rillig    207: /* All shell commands for all targets, in no particular order and possibly
                    208:  * with duplicates.  Kept in a separate list since the commands from .USE or
                    209:  * .USEBEFORE nodes are shared with other GNodes, thereby giving up the
                    210:  * easily understandable ownership over the allocated strings. */
1.322     rillig    211: static StringList *targCmds;
1.166     dholland  212: #endif
                    213:
                    214: /*
1.152     dsl       215:  * Predecessor node for handling .ORDER. Initialized to NULL when .ORDER
1.1       cgd       216:  * seen, then set to each successive source on the line.
                    217:  */
1.407     rillig    218: static GNode *order_pred;
1.1       cgd       219:
1.248     rillig    220: /* parser state */
1.166     dholland  221:
                    222: /* number of fatal errors */
                    223: static int fatals = 0;
                    224:
                    225: /*
                    226:  * Variables for doing includes
                    227:  */
                    228:
1.408     rillig    229: /* The include chain of makefiles.  At the bottom is the top-level makefile
                    230:  * from the command line, and on top of that, there are the included files or
                    231:  * .for loops, up to and including the current file.
1.319     rillig    232:  *
                    233:  * This data could be used to print stack traces on parse errors.  As of
                    234:  * 2020-09-14, this is not done though.  It seems quite simple to print the
                    235:  * tuples (fname:lineno:fromForLoop), from top to bottom.  This simple idea is
                    236:  * made complicated by the fact that the .for loops also use this stack for
                    237:  * storing information.
                    238:  *
                    239:  * The lineno fields of the IFiles with fromForLoop == TRUE look confusing,
                    240:  * which is demonstrated by the test 'include-main.mk'.  They seem sorted
                    241:  * backwards since they tell the number of completely parsed lines, which for
                    242:  * a .for loop is right after the terminating .endfor.  To compensate for this
                    243:  * confusion, there is another field first_lineno pointing at the start of the
                    244:  * .for loop, 1-based for human consumption.
                    245:  *
                    246:  * To make the stack trace intuitive, the entry below the first .for loop must
                    247:  * be ignored completely since neither its lineno nor its first_lineno is
1.408     rillig    248:  * useful.  Instead, the topmost of each chain of .for loop needs to be
                    249:  * printed twice, once with its first_lineno and once with its lineno.
1.319     rillig    250:  *
1.408     rillig    251:  * As of 2020-10-28, using the above rules, the stack trace for the .info line
1.319     rillig    252:  * in include-subsub.mk would be:
                    253:  *
1.408     rillig    254:  *     includes[5]:    include-subsub.mk:4
1.319     rillig    255:  *                     (lineno, from an .include)
                    256:  *     includes[4]:    include-sub.mk:32
                    257:  *                     (lineno, from a .for loop below an .include)
                    258:  *     includes[4]:    include-sub.mk:31
                    259:  *                     (first_lineno, from a .for loop, lineno == 32)
                    260:  *     includes[3]:    include-sub.mk:30
                    261:  *                     (first_lineno, from a .for loop, lineno == 33)
                    262:  *     includes[2]:    include-sub.mk:29
                    263:  *                     (first_lineno, from a .for loop, lineno == 34)
                    264:  *     includes[1]:    include-sub.mk:35
                    265:  *                     (not printed since it is below a .for loop)
                    266:  *     includes[0]:    include-main.mk:27
                    267:  */
1.408     rillig    268: static Vector /* of IFile */ includes;
1.400     rillig    269:
                    270: static IFile *
                    271: GetInclude(size_t i)
                    272: {
1.408     rillig    273:     return Vector_Get(&includes, i);
                    274: }
                    275:
                    276: /* The file that is currently being read. */
                    277: static IFile *
                    278: CurFile(void)
                    279: {
                    280:     return GetInclude(includes.len - 1);
1.400     rillig    281: }
1.166     dholland  282:
1.409     rillig    283: /* include paths */
1.322     rillig    284: SearchPath *parseIncPath;      /* dirs for "..." includes */
                    285: SearchPath *sysIncPath;                /* dirs for <...> includes */
1.409     rillig    286: SearchPath *defSysIncPath;             /* default for sysIncPath */
1.166     dholland  287:
1.248     rillig    288: /* parser tables */
1.166     dholland  289:
1.1       cgd       290: /*
                    291:  * The parseKeywords table is searched using binary search when deciding
                    292:  * if a target or source is special. The 'spec' field is the ParseSpecial
1.404     rillig    293:  * type of the keyword (SP_NOT if the keyword isn't special as a target) while
1.1       cgd       294:  * the 'op' field is the operator to apply to the list of targets if the
                    295:  * keyword is used as a source ("0" if the keyword isn't special as a source)
                    296:  */
1.168     dholland  297: static const struct {
1.338     rillig    298:     const char   *name;                /* Name of keyword */
                    299:     ParseSpecial  spec;                /* Type when used as a target */
                    300:     GNodeType    op;           /* Operator when used as a source */
1.1       cgd       301: } parseKeywords[] = {
1.404     rillig    302:     { ".BEGIN",                SP_BEGIN,       0 },
                    303:     { ".DEFAULT",      SP_DEFAULT,     0 },
                    304:     { ".DELETE_ON_ERROR", SP_DELETE_ON_ERROR, 0 },
                    305:     { ".END",          SP_END,         0 },
                    306:     { ".ERROR",                SP_ERROR,       0 },
                    307:     { ".EXEC",         SP_ATTRIBUTE,   OP_EXEC },
                    308:     { ".IGNORE",       SP_IGNORE,      OP_IGNORE },
                    309:     { ".INCLUDES",     SP_INCLUDES,    0 },
                    310:     { ".INTERRUPT",    SP_INTERRUPT,   0 },
                    311:     { ".INVISIBLE",    SP_ATTRIBUTE,   OP_INVISIBLE },
                    312:     { ".JOIN",         SP_ATTRIBUTE,   OP_JOIN },
                    313:     { ".LIBS",         SP_LIBS,        0 },
                    314:     { ".MADE",         SP_ATTRIBUTE,   OP_MADE },
                    315:     { ".MAIN",         SP_MAIN,        0 },
                    316:     { ".MAKE",         SP_ATTRIBUTE,   OP_MAKE },
                    317:     { ".MAKEFLAGS",    SP_MFLAGS,      0 },
                    318:     { ".META",         SP_META,        OP_META },
                    319:     { ".MFLAGS",       SP_MFLAGS,      0 },
                    320:     { ".NOMETA",       SP_NOMETA,      OP_NOMETA },
                    321:     { ".NOMETA_CMP",   SP_NOMETA_CMP,  OP_NOMETA_CMP },
                    322:     { ".NOPATH",       SP_NOPATH,      OP_NOPATH },
                    323:     { ".NOTMAIN",      SP_ATTRIBUTE,   OP_NOTMAIN },
                    324:     { ".NOTPARALLEL",  SP_NOTPARALLEL, 0 },
                    325:     { ".NO_PARALLEL",  SP_NOTPARALLEL, 0 },
                    326:     { ".NULL",         SP_NULL,        0 },
                    327:     { ".OBJDIR",       SP_OBJDIR,      0 },
                    328:     { ".OPTIONAL",     SP_ATTRIBUTE,   OP_OPTIONAL },
                    329:     { ".ORDER",                SP_ORDER,       0 },
                    330:     { ".PARALLEL",     SP_PARALLEL,    0 },
                    331:     { ".PATH",         SP_PATH,        0 },
                    332:     { ".PHONY",                SP_PHONY,       OP_PHONY },
1.48      sjg       333: #ifdef POSIX
1.404     rillig    334:     { ".POSIX",                SP_POSIX,       0 },
1.48      sjg       335: #endif
1.404     rillig    336:     { ".PRECIOUS",     SP_PRECIOUS,    OP_PRECIOUS },
                    337:     { ".RECURSIVE",    SP_ATTRIBUTE,   OP_MAKE },
                    338:     { ".SHELL",                SP_SHELL,       0 },
                    339:     { ".SILENT",       SP_SILENT,      OP_SILENT },
                    340:     { ".SINGLESHELL",  SP_SINGLESHELL, 0 },
                    341:     { ".STALE",                SP_STALE,       0 },
                    342:     { ".SUFFIXES",     SP_SUFFIXES,    0 },
                    343:     { ".USE",          SP_ATTRIBUTE,   OP_USE },
                    344:     { ".USEBEFORE",    SP_ATTRIBUTE,   OP_USEBEFORE },
                    345:     { ".WAIT",         SP_WAIT,        0 },
1.1       cgd       346: };
                    347:
1.248     rillig    348: /* file loader */
1.170     dholland  349:
                    350: struct loadedfile {
                    351:        const char *path;               /* name, for error reports */
                    352:        char *buf;                      /* contents buffer */
                    353:        size_t len;                     /* length of contents */
                    354:        size_t maplen;                  /* length of mmap area, or 0 */
                    355:        Boolean used;                   /* XXX: have we used the data yet */
                    356: };
                    357:
                    358: static struct loadedfile *
                    359: loadedfile_create(const char *path)
                    360: {
                    361:        struct loadedfile *lf;
                    362:
                    363:        lf = bmake_malloc(sizeof(*lf));
1.242     rillig    364:        lf->path = path == NULL ? "(stdin)" : path;
1.170     dholland  365:        lf->buf = NULL;
                    366:        lf->len = 0;
                    367:        lf->maplen = 0;
                    368:        lf->used = FALSE;
                    369:        return lf;
                    370: }
                    371:
                    372: static void
                    373: loadedfile_destroy(struct loadedfile *lf)
                    374: {
                    375:        if (lf->buf != NULL) {
                    376:                if (lf->maplen > 0) {
                    377:                        munmap(lf->buf, lf->maplen);
                    378:                } else {
                    379:                        free(lf->buf);
                    380:                }
                    381:        }
                    382:        free(lf);
                    383: }
                    384:
                    385: /*
                    386:  * nextbuf() operation for loadedfile, as needed by the weird and twisted
                    387:  * logic below. Once that's cleaned up, we can get rid of lf->used...
                    388:  */
                    389: static char *
                    390: loadedfile_nextbuf(void *x, size_t *len)
                    391: {
                    392:        struct loadedfile *lf = x;
                    393:
                    394:        if (lf->used) {
                    395:                return NULL;
                    396:        }
                    397:        lf->used = TRUE;
                    398:        *len = lf->len;
                    399:        return lf->buf;
                    400: }
                    401:
                    402: /*
                    403:  * Try to get the size of a file.
                    404:  */
1.272     rillig    405: static Boolean
1.170     dholland  406: load_getsize(int fd, size_t *ret)
                    407: {
                    408:        struct stat st;
                    409:
                    410:        if (fstat(fd, &st) < 0) {
1.272     rillig    411:                return FALSE;
1.170     dholland  412:        }
                    413:
                    414:        if (!S_ISREG(st.st_mode)) {
1.272     rillig    415:                return FALSE;
1.170     dholland  416:        }
                    417:
                    418:        /*
                    419:         * st_size is an off_t, which is 64 bits signed; *ret is
                    420:         * size_t, which might be 32 bits unsigned or 64 bits
                    421:         * unsigned. Rather than being elaborate, just punt on
                    422:         * files that are more than 2^31 bytes. We should never
                    423:         * see a makefile that size in practice...
                    424:         *
                    425:         * While we're at it reject negative sizes too, just in case.
                    426:         */
                    427:        if (st.st_size < 0 || st.st_size > 0x7fffffff) {
1.272     rillig    428:                return FALSE;
1.170     dholland  429:        }
                    430:
1.396     rillig    431:        *ret = (size_t)st.st_size;
1.272     rillig    432:        return TRUE;
1.170     dholland  433: }
                    434:
                    435: /*
                    436:  * Read in a file.
                    437:  *
                    438:  * Until the path search logic can be moved under here instead of
                    439:  * being in the caller in another source file, we need to have the fd
                    440:  * passed in already open. Bleh.
                    441:  *
                    442:  * If the path is NULL use stdin and (to insure against fd leaks)
                    443:  * assert that the caller passed in -1.
                    444:  */
                    445: static struct loadedfile *
                    446: loadfile(const char *path, int fd)
                    447: {
                    448:        struct loadedfile *lf;
1.369     rillig    449:        static unsigned long pagesize = 0;
1.170     dholland  450:        ssize_t result;
                    451:        size_t bufpos;
                    452:
                    453:        lf = loadedfile_create(path);
                    454:
                    455:        if (path == NULL) {
                    456:                assert(fd == -1);
                    457:                fd = STDIN_FILENO;
                    458:        } else {
                    459: #if 0 /* notyet */
                    460:                fd = open(path, O_RDONLY);
                    461:                if (fd < 0) {
                    462:                        ...
                    463:                        Error("%s: %s", path, strerror(errno));
                    464:                        exit(1);
                    465:                }
                    466: #endif
                    467:        }
                    468:
1.272     rillig    469:        if (load_getsize(fd, &lf->len)) {
1.170     dholland  470:                /* found a size, try mmap */
1.227     sjg       471:                if (pagesize == 0)
1.369     rillig    472:                        pagesize = (unsigned long)sysconf(_SC_PAGESIZE);
                    473:                if (pagesize == 0 || pagesize == (unsigned long)-1) {
1.170     dholland  474:                        pagesize = 0x1000;
                    475:                }
                    476:                /* round size up to a page */
                    477:                lf->maplen = pagesize * ((lf->len + pagesize - 1)/pagesize);
                    478:
                    479:                /*
                    480:                 * XXX hack for dealing with empty files; remove when
                    481:                 * we're no longer limited by interfacing to the old
                    482:                 * logic elsewhere in this file.
                    483:                 */
                    484:                if (lf->maplen == 0) {
                    485:                        lf->maplen = pagesize;
                    486:                }
                    487:
                    488:                /*
                    489:                 * FUTURE: remove PROT_WRITE when the parser no longer
                    490:                 * needs to scribble on the input.
                    491:                 */
                    492:                lf->buf = mmap(NULL, lf->maplen, PROT_READ|PROT_WRITE,
                    493:                               MAP_FILE|MAP_COPY, fd, 0);
                    494:                if (lf->buf != MAP_FAILED) {
                    495:                        /* succeeded */
1.178     dsl       496:                        if (lf->len == lf->maplen && lf->buf[lf->len - 1] != '\n') {
1.220     maya      497:                                char *b = bmake_malloc(lf->len + 1);
1.178     dsl       498:                                b[lf->len] = '\n';
                    499:                                memcpy(b, lf->buf, lf->len++);
                    500:                                munmap(lf->buf, lf->maplen);
                    501:                                lf->maplen = 0;
                    502:                                lf->buf = b;
                    503:                        }
1.170     dholland  504:                        goto done;
                    505:                }
                    506:        }
                    507:
                    508:        /* cannot mmap; load the traditional way */
                    509:
                    510:        lf->maplen = 0;
                    511:        lf->len = 1024;
                    512:        lf->buf = bmake_malloc(lf->len);
                    513:
                    514:        bufpos = 0;
                    515:        while (1) {
                    516:                assert(bufpos <= lf->len);
                    517:                if (bufpos == lf->len) {
1.221     riastrad  518:                        if (lf->len > SIZE_MAX/2) {
                    519:                                errno = EFBIG;
                    520:                                Error("%s: file too large", path);
                    521:                                exit(1);
                    522:                        }
1.170     dholland  523:                        lf->len *= 2;
                    524:                        lf->buf = bmake_realloc(lf->buf, lf->len);
                    525:                }
1.221     riastrad  526:                assert(bufpos < lf->len);
1.170     dholland  527:                result = read(fd, lf->buf + bufpos, lf->len - bufpos);
                    528:                if (result < 0) {
                    529:                        Error("%s: read error: %s", path, strerror(errno));
                    530:                        exit(1);
                    531:                }
                    532:                if (result == 0) {
                    533:                        break;
                    534:                }
1.369     rillig    535:                bufpos += (size_t)result;
1.170     dholland  536:        }
                    537:        assert(bufpos <= lf->len);
1.173     dholland  538:        lf->len = bufpos;
1.170     dholland  539:
                    540:        /* truncate malloc region to actual length (maybe not useful) */
1.172     dholland  541:        if (lf->len > 0) {
1.218     sjg       542:                /* as for mmap case, ensure trailing \n */
                    543:                if (lf->buf[lf->len - 1] != '\n')
                    544:                        lf->len++;
1.172     dholland  545:                lf->buf = bmake_realloc(lf->buf, lf->len);
1.218     sjg       546:                lf->buf[lf->len - 1] = '\n';
1.172     dholland  547:        }
1.170     dholland  548:
                    549: done:
                    550:        if (path != NULL) {
                    551:                close(fd);
                    552:        }
                    553:        return lf;
                    554: }
                    555:
1.248     rillig    556: /* old code */
1.77      christos  557:
1.279     rillig    558: /* Check if the current character is escaped on the current line. */
                    559: static Boolean
1.84      wiz       560: ParseIsEscaped(const char *line, const char *c)
1.77      christos  561: {
1.280     rillig    562:     Boolean active = FALSE;
1.77      christos  563:     for (;;) {
                    564:        if (line == c)
                    565:            return active;
                    566:        if (*--c != '\\')
                    567:            return active;
                    568:        active = !active;
                    569:     }
                    570: }
                    571:
1.278     rillig    572: /* Add the filename and lineno to the GNode so that we remember where it
                    573:  * was first defined. */
                    574: static void
                    575: ParseMark(GNode *gn)
                    576: {
1.408     rillig    577:     IFile *curFile = CurFile();
1.278     rillig    578:     gn->fname = curFile->fname;
                    579:     gn->lineno = curFile->lineno;
                    580: }
                    581:
1.368     rillig    582: /* Look in the table of keywords for one matching the given string.
                    583:  * Return the index of the keyword, or -1 if it isn't there. */
1.1       cgd       584: static int
1.120     dsl       585: ParseFindKeyword(const char *str)
1.1       cgd       586: {
1.374     rillig    587:     int start, end, cur;
                    588:     int diff;
1.27      christos  589:
1.1       cgd       590:     start = 0;
1.375     rillig    591:     end = sizeof parseKeywords / sizeof parseKeywords[0] - 1;
1.1       cgd       592:
                    593:     do {
1.375     rillig    594:        cur = start + (end - start) / 2;
1.103     christos  595:        diff = strcmp(str, parseKeywords[cur].name);
1.1       cgd       596:
                    597:        if (diff == 0) {
1.235     rillig    598:            return cur;
1.1       cgd       599:        } else if (diff < 0) {
                    600:            end = cur - 1;
                    601:        } else {
                    602:            start = cur + 1;
                    603:        }
                    604:     } while (start <= end);
1.235     rillig    605:     return -1;
1.1       cgd       606: }
                    607:
1.291     rillig    608: static void
1.305     rillig    609: PrintLocation(FILE *f, const char *filename, size_t lineno)
1.291     rillig    610: {
                    611:        char dirbuf[MAXPATHLEN+1];
1.305     rillig    612:        const char *dir, *base;
1.413   ! rillig    613:        void *dir_freeIt, *base_freeIt;
1.291     rillig    614:
1.305     rillig    615:        if (*filename == '/' || strcmp(filename, "(stdin)") == 0) {
                    616:                (void)fprintf(f, "\"%s\" line %zu: ", filename, lineno);
1.299     rillig    617:                return;
                    618:        }
1.291     rillig    619:
1.299     rillig    620:        /* Find out which makefile is the culprit.
                    621:         * We try ${.PARSEDIR} and apply realpath(3) if not absolute. */
1.291     rillig    622:
1.299     rillig    623:        dir = Var_Value(".PARSEDIR", VAR_GLOBAL, &dir_freeIt);
                    624:        if (dir == NULL)
                    625:                dir = ".";
                    626:        if (*dir != '/')
                    627:                dir = realpath(dir, dirbuf);
                    628:
1.305     rillig    629:        base = Var_Value(".PARSEFILE", VAR_GLOBAL, &base_freeIt);
                    630:        if (base == NULL) {
                    631:                const char *slash = strrchr(filename, '/');
                    632:                base = slash != NULL ? slash + 1 : filename;
1.299     rillig    633:        }
                    634:
1.305     rillig    635:        (void)fprintf(f, "\"%s/%s\" line %zu: ", dir, base, lineno);
                    636:        bmake_free(base_freeIt);
1.299     rillig    637:        bmake_free(dir_freeIt);
1.291     rillig    638: }
                    639:
1.300     rillig    640: /* Print a parse error message, including location information.
1.1       cgd       641:  *
1.300     rillig    642:  * Increment "fatals" if the level is PARSE_FATAL, and continue parsing
                    643:  * until the end of the current top-level makefile, then exit (see
1.368     rillig    644:  * Parse_File). */
1.38      christos  645: static void
1.398     rillig    646: ParseVErrorInternal(FILE *f, const char *cfname, size_t clineno,
                    647:                    ParseErrorLevel type, const char *fmt, va_list ap)
1.38      christos  648: {
1.56      christos  649:        static Boolean fatal_warning_error_printed = FALSE;
                    650:
1.148     sjg       651:        (void)fprintf(f, "%s: ", progname);
1.63      christos  652:
1.291     rillig    653:        if (cfname != NULL)
                    654:                PrintLocation(f, cfname, clineno);
1.38      christos  655:        if (type == PARSE_WARNING)
1.127     dsl       656:                (void)fprintf(f, "warning: ");
                    657:        (void)vfprintf(f, fmt, ap);
                    658:        (void)fprintf(f, "\n");
                    659:        (void)fflush(f);
1.300     rillig    660:
1.226     sjg       661:        if (type == PARSE_INFO)
                    662:                return;
1.401     rillig    663:        if (type == PARSE_FATAL || opts.parseWarnFatal)
1.300     rillig    664:                fatals++;
1.401     rillig    665:        if (opts.parseWarnFatal && !fatal_warning_error_printed) {
1.56      christos  666:                Error("parsing warnings being treated as errors");
                    667:                fatal_warning_error_printed = TRUE;
                    668:        }
1.38      christos  669: }
                    670:
1.203     joerg     671: static void
1.398     rillig    672: ParseErrorInternal(const char *cfname, size_t clineno, ParseErrorLevel type,
1.374     rillig    673:                   const char *fmt, ...)
1.203     joerg     674: {
                    675:        va_list ap;
                    676:
                    677:        va_start(ap, fmt);
                    678:        (void)fflush(stdout);
                    679:        ParseVErrorInternal(stderr, cfname, clineno, type, fmt, ap);
                    680:        va_end(ap);
                    681:
1.401     rillig    682:        if (opts.debug_file != stderr && opts.debug_file != stdout) {
1.203     joerg     683:                va_start(ap, fmt);
1.401     rillig    684:                ParseVErrorInternal(opts.debug_file, cfname, clineno, type,
                    685:                                    fmt, ap);
1.203     joerg     686:                va_end(ap);
                    687:        }
                    688: }
                    689:
1.289     rillig    690: /* External interface to ParseErrorInternal; uses the default filename and
                    691:  * line number.
1.38      christos  692:  *
1.289     rillig    693:  * Fmt is given without a trailing newline. */
1.1       cgd       694: void
1.398     rillig    695: Parse_Error(ParseErrorLevel type, const char *fmt, ...)
1.1       cgd       696: {
                    697:        va_list ap;
1.156     dsl       698:        const char *fname;
                    699:        size_t lineno;
1.84      wiz       700:
1.408     rillig    701:        if (includes.len == 0) {
1.156     dsl       702:                fname = NULL;
                    703:                lineno = 0;
                    704:        } else {
1.408     rillig    705:                IFile *curFile = CurFile();
1.156     dsl       706:                fname = curFile->fname;
1.369     rillig    707:                lineno = (size_t)curFile->lineno;
1.148     sjg       708:        }
1.156     dsl       709:
                    710:        va_start(ap, fmt);
1.163     sjg       711:        (void)fflush(stdout);
1.156     dsl       712:        ParseVErrorInternal(stderr, fname, lineno, type, fmt, ap);
1.1       cgd       713:        va_end(ap);
1.127     dsl       714:
1.401     rillig    715:        if (opts.debug_file != stderr && opts.debug_file != stdout) {
1.127     dsl       716:                va_start(ap, fmt);
1.401     rillig    717:                ParseVErrorInternal(opts.debug_file, fname, lineno, type,
                    718:                                    fmt, ap);
1.127     dsl       719:                va_end(ap);
                    720:        }
1.1       cgd       721: }
                    722:
1.161     sjg       723:
1.301     rillig    724: /* Parse a .info .warning or .error directive.
1.161     sjg       725:  *
1.301     rillig    726:  * The input is the line minus the ".".  We substitute variables, print the
                    727:  * message and exit(1) (for .error) or just print a warning if the directive
                    728:  * is malformed.
1.161     sjg       729:  */
1.164     sjg       730: static Boolean
1.377     rillig    731: ParseMessage(const char *directive)
1.161     sjg       732: {
1.377     rillig    733:     const char *p = directive;
                    734:     int mtype = *p == 'i' ? PARSE_INFO :
                    735:                *p == 'w' ? PARSE_WARNING : PARSE_FATAL;
                    736:     char *arg;
1.164     sjg       737:
1.377     rillig    738:     while (ch_isalpha(*p))
                    739:        p++;
                    740:     if (!ch_isspace(*p))
                    741:        return FALSE;           /* missing argument */
1.161     sjg       742:
1.377     rillig    743:     cpp_skip_whitespace(&p);
1.411     rillig    744:     (void)Var_Subst(p, VAR_CMDLINE, VARE_WANTRES, &arg);
1.377     rillig    745:     /* TODO: handle errors */
1.161     sjg       746:
1.377     rillig    747:     Parse_Error(mtype, "%s", arg);
                    748:     free(arg);
1.161     sjg       749:
                    750:     if (mtype == PARSE_FATAL) {
1.339     sjg       751:        PrintOnError(NULL, NULL);
1.161     sjg       752:        exit(1);
                    753:     }
1.164     sjg       754:     return TRUE;
1.161     sjg       755: }
                    756:
1.316     rillig    757: /* Add the child to the parent's children.
1.1       cgd       758:  *
1.382     rillig    759:  * Additionally, add the parent to the child's parents, but only if the
                    760:  * target is not special.  An example for such a special target is .END,
                    761:  * which does not need to be informed once the child target has been made. */
                    762: static void
                    763: LinkSource(GNode *pgn, GNode *cgn, Boolean isSpecial)
                    764: {
1.268     rillig    765:     if ((pgn->type & OP_DOUBLEDEP) && !Lst_IsEmpty(pgn->cohorts))
1.394     rillig    766:        pgn = pgn->cohorts->last->datum;
1.316     rillig    767:
1.268     rillig    768:     Lst_Append(pgn->children, cgn);
1.343     rillig    769:     pgn->unmade++;
1.316     rillig    770:
1.382     rillig    771:     /* Special targets like .END don't need any children. */
                    772:     if (!isSpecial)
1.268     rillig    773:        Lst_Append(cgn->parents, pgn);
1.316     rillig    774:
1.109     dsl       775:     if (DEBUG(PARSE)) {
1.342     rillig    776:        debug_printf("# %s: added child %s - %s\n",
                    777:                     __func__, pgn->name, cgn->name);
1.109     dsl       778:        Targ_PrintNode(pgn, 0);
                    779:        Targ_PrintNode(cgn, 0);
                    780:     }
1.1       cgd       781: }
                    782:
1.382     rillig    783: /* Add the node to each target from the current dependency group. */
                    784: static void
                    785: LinkToTargets(GNode *gn, Boolean isSpecial)
                    786: {
                    787:     GNodeListNode *ln;
                    788:     for (ln = targets->first; ln != NULL; ln = ln->next)
                    789:        LinkSource(ln->datum, gn, isSpecial);
                    790: }
                    791:
1.337     rillig    792: static Boolean
                    793: TryApplyDependencyOperator(GNode *gn, GNodeType op)
1.1       cgd       794: {
                    795:     /*
1.397     rillig    796:      * If the node occurred on the left-hand side of a dependency and the
                    797:      * operator also defines a dependency, they must match.
1.1       cgd       798:      */
1.397     rillig    799:     if ((op & OP_OPMASK) && (gn->type & OP_OPMASK) &&
                    800:        ((op & OP_OPMASK) != (gn->type & OP_OPMASK)))
1.1       cgd       801:     {
1.97      christos  802:        Parse_Error(PARSE_FATAL, "Inconsistent operator for %s", gn->name);
1.337     rillig    803:        return FALSE;
1.1       cgd       804:     }
                    805:
1.242     rillig    806:     if (op == OP_DOUBLEDEP && (gn->type & OP_OPMASK) == OP_DOUBLEDEP) {
1.1       cgd       807:        /*
                    808:         * If the node was the object of a :: operator, we need to create a
                    809:         * new instance of it for the children and commands on this dependency
                    810:         * line. The new instance is placed on the 'cohorts' list of the
                    811:         * initial one (note the initial one is not on its own cohorts list)
                    812:         * and the new instance is linked to all parents of the initial
                    813:         * instance.
                    814:         */
1.374     rillig    815:        GNode *cohort;
1.27      christos  816:
1.33      mycroft   817:        /*
1.46      mycroft   818:         * Propagate copied bits to the initial node.  They'll be propagated
                    819:         * back to the rest of the cohorts later.
1.33      mycroft   820:         */
1.46      mycroft   821:        gn->type |= op & ~OP_OPMASK;
1.33      mycroft   822:
1.332     rillig    823:        cohort = Targ_NewInternalNode(gn->name);
1.186     christos  824:        if (doing_depend)
                    825:            ParseMark(cohort);
1.1       cgd       826:        /*
                    827:         * Make the cohort invisible as well to avoid duplicating it into
                    828:         * other variables. True, parents of this target won't tend to do
                    829:         * anything with their local variables, but better safe than
1.46      mycroft   830:         * sorry. (I think this is pointless now, since the relevant list
                    831:         * traversals will no longer see this node anyway. -mycroft)
1.1       cgd       832:         */
1.46      mycroft   833:        cohort->type = op | OP_INVISIBLE;
1.268     rillig    834:        Lst_Append(gn->cohorts, cohort);
1.83      pk        835:        cohort->centurion = gn;
1.343     rillig    836:        gn->unmade_cohorts++;
1.120     dsl       837:        snprintf(cohort->cohort_num, sizeof cohort->cohort_num, "#%d",
1.387     rillig    838:                 (unsigned int)gn->unmade_cohorts % 1000000);
1.46      mycroft   839:     } else {
1.1       cgd       840:        /*
1.46      mycroft   841:         * We don't want to nuke any previous flags (whatever they were) so we
                    842:         * just OR the new operator into the old
1.1       cgd       843:         */
1.46      mycroft   844:        gn->type |= op;
1.1       cgd       845:     }
1.33      mycroft   846:
1.337     rillig    847:     return TRUE;
                    848: }
                    849:
                    850: static void
                    851: ApplyDependencyOperator(GNodeType op)
                    852: {
                    853:     GNodeListNode *ln;
                    854:     for (ln = targets->first; ln != NULL; ln = ln->next)
1.373     rillig    855:        if (!TryApplyDependencyOperator(ln->datum, op))
1.337     rillig    856:            break;
1.1       cgd       857: }
                    858:
1.368     rillig    859: static Boolean
                    860: ParseDoSrcKeyword(const char *src, ParseSpecial specType)
1.1       cgd       861: {
1.120     dsl       862:     static int wait_number = 0;
                    863:     char wait_src[16];
1.368     rillig    864:     GNode *gn;
1.1       cgd       865:
1.290     rillig    866:     if (*src == '.' && ch_isupper(src[1])) {
1.1       cgd       867:        int keywd = ParseFindKeyword(src);
                    868:        if (keywd != -1) {
1.18      christos  869:            int op = parseKeywords[keywd].op;
                    870:            if (op != 0) {
1.337     rillig    871:                ApplyDependencyOperator(op);
1.368     rillig    872:                return TRUE;
1.18      christos  873:            }
1.404     rillig    874:            if (parseKeywords[keywd].spec == SP_WAIT) {
1.120     dsl       875:                /*
                    876:                 * We add a .WAIT node in the dependency list.
                    877:                 * After any dynamic dependencies (and filename globbing)
                    878:                 * have happened, it is given a dependency on the each
                    879:                 * previous child back to and previous .WAIT node.
                    880:                 * The next child won't be scheduled until the .WAIT node
                    881:                 * is built.
                    882:                 * We give each .WAIT node a unique name (mainly for diag).
                    883:                 */
                    884:                snprintf(wait_src, sizeof wait_src, ".WAIT_%u", ++wait_number);
1.332     rillig    885:                gn = Targ_NewInternalNode(wait_src);
1.186     christos  886:                if (doing_depend)
                    887:                    ParseMark(gn);
1.120     dsl       888:                gn->type = OP_WAIT | OP_PHONY | OP_DEPENDS | OP_NOTMAIN;
1.404     rillig    889:                LinkToTargets(gn, specType != SP_NOT);
1.368     rillig    890:                return TRUE;
1.18      christos  891:            }
1.1       cgd       892:        }
                    893:     }
1.368     rillig    894:     return FALSE;
                    895: }
1.18      christos  896:
1.368     rillig    897: static void
                    898: ParseDoSrcMain(const char *src)
                    899: {
                    900:     /*
                    901:      * If we have noted the existence of a .MAIN, it means we need
                    902:      * to add the sources of said target to the list of things
                    903:      * to create. The string 'src' is likely to be free, so we
                    904:      * must make a new copy of it. Note that this will only be
                    905:      * invoked if the user didn't specify a target on the command
                    906:      * line. This is to allow #ifmake's to succeed, or something...
                    907:      */
1.401     rillig    908:     Lst_Append(opts.create, bmake_strdup(src));
1.368     rillig    909:     /*
                    910:      * Add the name to the .TARGETS variable as well, so the user can
                    911:      * employ that, if desired.
                    912:      */
                    913:     Var_Append(".TARGETS", src, VAR_GLOBAL);
                    914: }
1.18      christos  915:
1.368     rillig    916: static void
                    917: ParseDoSrcOrder(const char *src)
                    918: {
                    919:     GNode *gn;
                    920:     /*
                    921:      * Create proper predecessor/successor links between the previous
                    922:      * source and the current one.
                    923:      */
                    924:     gn = Targ_GetNode(src);
                    925:     if (doing_depend)
                    926:        ParseMark(gn);
1.407     rillig    927:     if (order_pred != NULL) {
                    928:        Lst_Append(order_pred->order_succ, gn);
                    929:        Lst_Append(gn->order_pred, order_pred);
1.368     rillig    930:        if (DEBUG(PARSE)) {
                    931:            debug_printf("# %s: added Order dependency %s - %s\n",
1.407     rillig    932:                         __func__, order_pred->name, gn->name);
                    933:            Targ_PrintNode(order_pred, 0);
1.368     rillig    934:            Targ_PrintNode(gn, 0);
1.1       cgd       935:        }
1.368     rillig    936:     }
                    937:     /*
                    938:      * The current source now becomes the predecessor for the next one.
                    939:      */
1.407     rillig    940:     order_pred = gn;
1.368     rillig    941: }
                    942:
                    943: static void
                    944: ParseDoSrcOther(const char *src, GNodeType tOp, ParseSpecial specType)
                    945: {
                    946:     GNode *gn;
1.363     rillig    947:
1.368     rillig    948:     /*
                    949:      * If the source is not an attribute, we need to find/create
                    950:      * a node for it. After that we can apply any operator to it
                    951:      * from a special target or link it to its parents, as
                    952:      * appropriate.
                    953:      *
                    954:      * In the case of a source that was the object of a :: operator,
                    955:      * the attribute is applied to all of its instances (as kept in
                    956:      * the 'cohorts' list of the node) or all the cohorts are linked
                    957:      * to all the targets.
                    958:      */
1.18      christos  959:
1.368     rillig    960:     /* Find/create the 'src' node and attach to all targets */
                    961:     gn = Targ_GetNode(src);
                    962:     if (doing_depend)
                    963:        ParseMark(gn);
                    964:     if (tOp) {
                    965:        gn->type |= tOp;
                    966:     } else {
1.404     rillig    967:        LinkToTargets(gn, specType != SP_NOT);
1.18      christos  968:     }
1.1       cgd       969: }
                    970:
1.368     rillig    971: /* Given the name of a source in a dependency line, figure out if it is an
                    972:  * attribute (such as .SILENT) and apply it to the targets if it is. Else
                    973:  * decide if there is some attribute which should be applied *to* the source
                    974:  * because of some special target (such as .PHONY) and apply it if so.
                    975:  * Otherwise, make the source a child of the targets in the list 'targets'.
                    976:  *
                    977:  * Input:
                    978:  *     tOp             operator (if any) from special targets
                    979:  *     src             name of the source to handle
                    980:  */
                    981: static void
                    982: ParseDoSrc(GNodeType tOp, const char *src, ParseSpecial specType)
                    983: {
                    984:     if (ParseDoSrcKeyword(src, specType))
1.373     rillig    985:        return;
1.368     rillig    986:
1.404     rillig    987:     if (specType == SP_MAIN)
1.373     rillig    988:        ParseDoSrcMain(src);
1.404     rillig    989:     else if (specType == SP_ORDER)
1.373     rillig    990:        ParseDoSrcOrder(src);
1.368     rillig    991:     else
1.373     rillig    992:        ParseDoSrcOther(src, tOp, specType);
1.368     rillig    993: }
                    994:
1.335     rillig    995: /* If we have yet to decide on a main target to make, in the absence of any
                    996:  * user input, we want the first target on the first dependency line that is
                    997:  * actually a real target (i.e. isn't a .USE or .EXEC rule) to be made. */
                    998: static void
                    999: FindMainTarget(void)
1.1       cgd      1000: {
1.335     rillig   1001:     GNodeListNode *ln;
                   1002:
                   1003:     if (mainNode != NULL)
1.373     rillig   1004:        return;
1.335     rillig   1005:
                   1006:     for (ln = targets->first; ln != NULL; ln = ln->next) {
                   1007:        GNode *gn = ln->datum;
                   1008:        if (!(gn->type & OP_NOTARGET)) {
                   1009:            mainNode = gn;
                   1010:            Targ_SetMain(gn);
                   1011:            return;
                   1012:        }
1.1       cgd      1013:     }
                   1014: }
                   1015:
1.311     rillig   1016: /*
                   1017:  * We got to the end of the line while we were still looking at targets.
                   1018:  *
                   1019:  * Ending a dependency line without an operator is a Bozo no-no.  As a
                   1020:  * heuristic, this is also often triggered by undetected conflicts from
                   1021:  * cvs/rcs merges.
                   1022:  */
                   1023: static void
1.395     rillig   1024: ParseErrorNoDependency(const char *lstart)
1.311     rillig   1025: {
1.395     rillig   1026:     if ((strncmp(lstart, "<<<<<<", 6) == 0) ||
                   1027:        (strncmp(lstart, "======", 6) == 0) ||
                   1028:        (strncmp(lstart, ">>>>>>", 6) == 0))
1.311     rillig   1029:        Parse_Error(PARSE_FATAL,
                   1030:                    "Makefile appears to contain unresolved cvs/rcs/??? merge conflicts");
                   1031:     else if (lstart[0] == '.') {
                   1032:        const char *dirstart = lstart + 1;
                   1033:        const char *dirend;
1.368     rillig   1034:        cpp_skip_whitespace(&dirstart);
1.311     rillig   1035:        dirend = dirstart;
                   1036:        while (ch_isalnum(*dirend) || *dirend == '-')
                   1037:            dirend++;
                   1038:        Parse_Error(PARSE_FATAL, "Unknown directive \"%.*s\"",
                   1039:                    (int)(dirend - dirstart), dirstart);
                   1040:     } else
                   1041:        Parse_Error(PARSE_FATAL, "Need an operator");
                   1042: }
                   1043:
1.314     rillig   1044: static void
                   1045: ParseDependencyTargetWord(/*const*/ char **pp, const char *lstart)
                   1046: {
                   1047:     /*const*/ char *cp = *pp;
                   1048:
                   1049:     while (*cp != '\0') {
                   1050:        if ((ch_isspace(*cp) || *cp == '!' || *cp == ':' || *cp == '(') &&
                   1051:            !ParseIsEscaped(lstart, cp))
                   1052:            break;
                   1053:
                   1054:        if (*cp == '$') {
                   1055:            /*
                   1056:             * Must be a dynamic source (would have been expanded
                   1057:             * otherwise), so call the Var module to parse the puppy
                   1058:             * so we can safely advance beyond it...There should be
                   1059:             * no errors in this, as they would have been discovered
                   1060:             * in the initial Var_Subst and we wouldn't be here.
                   1061:             */
                   1062:            const char *nested_p = cp;
                   1063:            const char *nested_val;
1.374     rillig   1064:            void *freeIt;
1.314     rillig   1065:
1.411     rillig   1066:            (void)Var_Parse(&nested_p, VAR_CMDLINE, VARE_UNDEFERR|VARE_WANTRES,
1.314     rillig   1067:                            &nested_val, &freeIt);
                   1068:            /* TODO: handle errors */
                   1069:            free(freeIt);
                   1070:            cp += nested_p - cp;
                   1071:        } else
                   1072:            cp++;
                   1073:     }
                   1074:
                   1075:     *pp = cp;
                   1076: }
                   1077:
1.368     rillig   1078: /*
                   1079:  * Certain special targets have special semantics:
                   1080:  *     .PATH           Have to set the dirSearchPath
                   1081:  *                     variable too
                   1082:  *     .MAIN           Its sources are only used if
                   1083:  *                     nothing has been specified to
                   1084:  *                     create.
                   1085:  *     .DEFAULT        Need to create a node to hang
                   1086:  *                     commands on, but we don't want
                   1087:  *                     it in the graph, nor do we want
                   1088:  *                     it to be the Main Target, so we
                   1089:  *                     create it, set OP_NOTMAIN and
                   1090:  *                     add it to the list, setting
                   1091:  *                     DEFAULT to the new node for
                   1092:  *                     later use. We claim the node is
                   1093:  *                     A transformation rule to make
                   1094:  *                     life easier later, when we'll
                   1095:  *                     use Make_HandleUse to actually
                   1096:  *                     apply the .DEFAULT commands.
                   1097:  *     .PHONY          The list of targets
                   1098:  *     .NOPATH         Don't search for file in the path
                   1099:  *     .STALE
                   1100:  *     .BEGIN
                   1101:  *     .END
                   1102:  *     .ERROR
                   1103:  *     .DELETE_ON_ERROR
                   1104:  *     .INTERRUPT      Are not to be considered the
                   1105:  *                     main target.
                   1106:  *     .NOTPARALLEL    Make only one target at a time.
                   1107:  *     .SINGLESHELL    Create a shell for each command.
                   1108:  *     .ORDER          Must set initial predecessor to NULL
                   1109:  */
                   1110: static void
1.395     rillig   1111: ParseDoDependencyTargetSpecial(ParseSpecial *inout_specType,
                   1112:                               const char *line,
                   1113:                               SearchPathList **inout_paths)
1.368     rillig   1114: {
                   1115:     switch (*inout_specType) {
1.404     rillig   1116:     case SP_PATH:
1.368     rillig   1117:        if (*inout_paths == NULL) {
1.385     rillig   1118:            *inout_paths = Lst_New();
1.368     rillig   1119:        }
                   1120:        Lst_Append(*inout_paths, dirSearchPath);
                   1121:        break;
1.404     rillig   1122:     case SP_MAIN:
1.401     rillig   1123:        if (!Lst_IsEmpty(opts.create)) {
1.404     rillig   1124:            *inout_specType = SP_NOT;
1.368     rillig   1125:        }
                   1126:        break;
1.404     rillig   1127:     case SP_BEGIN:
                   1128:     case SP_END:
                   1129:     case SP_STALE:
                   1130:     case SP_ERROR:
                   1131:     case SP_INTERRUPT: {
1.368     rillig   1132:        GNode *gn = Targ_GetNode(line);
                   1133:        if (doing_depend)
                   1134:            ParseMark(gn);
                   1135:        gn->type |= OP_NOTMAIN|OP_SPECIAL;
                   1136:        Lst_Append(targets, gn);
                   1137:        break;
                   1138:     }
1.404     rillig   1139:     case SP_DEFAULT: {
1.368     rillig   1140:        GNode *gn = Targ_NewGN(".DEFAULT");
                   1141:        gn->type |= OP_NOTMAIN|OP_TRANSFORM;
                   1142:        Lst_Append(targets, gn);
                   1143:        DEFAULT = gn;
                   1144:        break;
                   1145:     }
1.404     rillig   1146:     case SP_DELETE_ON_ERROR:
1.368     rillig   1147:        deleteOnError = TRUE;
                   1148:        break;
1.404     rillig   1149:     case SP_NOTPARALLEL:
1.401     rillig   1150:        opts.maxJobs = 1;
1.368     rillig   1151:        break;
1.404     rillig   1152:     case SP_SINGLESHELL:
1.401     rillig   1153:        opts.compatMake = TRUE;
1.368     rillig   1154:        break;
1.404     rillig   1155:     case SP_ORDER:
1.407     rillig   1156:        order_pred = NULL;
1.368     rillig   1157:        break;
                   1158:     default:
                   1159:        break;
                   1160:     }
                   1161: }
                   1162:
                   1163: /*
                   1164:  * .PATH<suffix> has to be handled specially.
                   1165:  * Call on the suffix module to give us a path to modify.
                   1166:  */
                   1167: static Boolean
1.395     rillig   1168: ParseDoDependencyTargetPath(const char *line, SearchPathList **inout_paths)
1.368     rillig   1169: {
                   1170:     SearchPath *path;
                   1171:
                   1172:     path = Suff_GetPath(&line[5]);
                   1173:     if (path == NULL) {
                   1174:        Parse_Error(PARSE_FATAL,
                   1175:                    "Suffix '%s' not defined (yet)",
                   1176:                    &line[5]);
                   1177:        return FALSE;
                   1178:     } else {
                   1179:        if (*inout_paths == NULL) {
1.385     rillig   1180:            *inout_paths = Lst_New();
1.368     rillig   1181:        }
                   1182:        Lst_Append(*inout_paths, path);
                   1183:     }
                   1184:     return TRUE;
                   1185: }
                   1186:
                   1187: /*
                   1188:  * See if it's a special target and if so set specType to match it.
1.364     rillig   1189:  */
1.368     rillig   1190: static Boolean
1.395     rillig   1191: ParseDoDependencyTarget(const char *line, ParseSpecial *inout_specType,
                   1192:                        GNodeType *out_tOp, SearchPathList **inout_paths)
1.364     rillig   1193: {
1.368     rillig   1194:     int keywd;
1.364     rillig   1195:
1.368     rillig   1196:     if (!(*line == '.' && ch_isupper(line[1])))
                   1197:        return TRUE;
1.364     rillig   1198:
                   1199:     /*
1.368     rillig   1200:      * See if the target is a special target that must have it
                   1201:      * or its sources handled specially.
1.364     rillig   1202:      */
1.368     rillig   1203:     keywd = ParseFindKeyword(line);
                   1204:     if (keywd != -1) {
1.404     rillig   1205:        if (*inout_specType == SP_PATH && parseKeywords[keywd].spec != SP_PATH) {
1.368     rillig   1206:            Parse_Error(PARSE_FATAL, "Mismatched special targets");
                   1207:            return FALSE;
                   1208:        }
                   1209:
                   1210:        *inout_specType = parseKeywords[keywd].spec;
                   1211:        *out_tOp = parseKeywords[keywd].op;
                   1212:
                   1213:        ParseDoDependencyTargetSpecial(inout_specType, line, inout_paths);
                   1214:
                   1215:     } else if (strncmp(line, ".PATH", 5) == 0) {
1.404     rillig   1216:        *inout_specType = SP_PATH;
1.368     rillig   1217:        if (!ParseDoDependencyTargetPath(line, inout_paths))
                   1218:            return FALSE;
                   1219:     }
                   1220:     return TRUE;
                   1221: }
                   1222:
                   1223: static void
1.395     rillig   1224: ParseDoDependencyTargetMundane(char *line, StringList *curTargs)
1.368     rillig   1225: {
                   1226:     if (Dir_HasWildcards(line)) {
                   1227:        /*
                   1228:         * Targets are to be sought only in the current directory,
                   1229:         * so create an empty path for the thing. Note we need to
                   1230:         * use Dir_Destroy in the destruction of the path as the
                   1231:         * Dir module could have added a directory to the path...
                   1232:         */
1.385     rillig   1233:        SearchPath *emptyPath = Lst_New();
1.368     rillig   1234:
                   1235:        Dir_Expand(line, emptyPath, curTargs);
                   1236:
                   1237:        Lst_Destroy(emptyPath, Dir_Destroy);
                   1238:     } else {
                   1239:        /*
                   1240:         * No wildcards, but we want to avoid code duplication,
                   1241:         * so create a list with the word on it.
                   1242:         */
                   1243:        Lst_Append(curTargs, line);
                   1244:     }
                   1245:
                   1246:     /* Apply the targets. */
                   1247:
1.374     rillig   1248:     while (!Lst_IsEmpty(curTargs)) {
1.368     rillig   1249:        char *targName = Lst_Dequeue(curTargs);
                   1250:        GNode *gn = Suff_IsTransform(targName)
                   1251:                    ? Suff_AddTransform(targName)
                   1252:                    : Targ_GetNode(targName);
                   1253:        if (doing_depend)
                   1254:            ParseMark(gn);
                   1255:
                   1256:        Lst_Append(targets, gn);
                   1257:     }
                   1258: }
                   1259:
                   1260: static void
                   1261: ParseDoDependencyTargetExtraWarn(char **pp, const char *lstart)
                   1262: {
                   1263:     Boolean warning = FALSE;
                   1264:     char *cp = *pp;
                   1265:
                   1266:     while (*cp && (ParseIsEscaped(lstart, cp) ||
                   1267:                   (*cp != '!' && *cp != ':'))) {
                   1268:        if (ParseIsEscaped(lstart, cp) ||
                   1269:            (*cp != ' ' && *cp != '\t')) {
                   1270:            warning = TRUE;
                   1271:        }
                   1272:        cp++;
                   1273:     }
                   1274:     if (warning) {
                   1275:        Parse_Error(PARSE_WARNING, "Extra target ignored");
                   1276:     }
                   1277:     *pp = cp;
                   1278: }
                   1279:
                   1280: static void
1.395     rillig   1281: ParseDoDependencyCheckSpec(ParseSpecial specType)
1.368     rillig   1282: {
1.374     rillig   1283:     switch (specType) {
1.368     rillig   1284:     default:
                   1285:        Parse_Error(PARSE_WARNING,
                   1286:                    "Special and mundane targets don't mix. Mundane ones ignored");
                   1287:        break;
1.404     rillig   1288:     case SP_DEFAULT:
                   1289:     case SP_STALE:
                   1290:     case SP_BEGIN:
                   1291:     case SP_END:
                   1292:     case SP_ERROR:
                   1293:     case SP_INTERRUPT:
1.368     rillig   1294:        /*
                   1295:         * These four create nodes on which to hang commands, so
                   1296:         * targets shouldn't be empty...
                   1297:         */
1.404     rillig   1298:     case SP_NOT:
1.368     rillig   1299:        /*
                   1300:         * Nothing special here -- targets can be empty if it wants.
                   1301:         */
                   1302:        break;
                   1303:     }
                   1304: }
                   1305:
                   1306: static Boolean
1.395     rillig   1307: ParseDoDependencyParseOp(char **pp, const char *lstart, GNodeType *out_op)
1.368     rillig   1308: {
                   1309:     const char *cp = *pp;
                   1310:
                   1311:     if (*cp == '!') {
                   1312:        *out_op = OP_FORCE;
                   1313:        (*pp)++;
                   1314:        return TRUE;
                   1315:     }
                   1316:
                   1317:     if (*cp == ':') {
                   1318:        if (cp[1] == ':') {
                   1319:            *out_op = OP_DOUBLEDEP;
                   1320:            (*pp) += 2;
                   1321:        } else {
                   1322:            *out_op = OP_DEPENDS;
                   1323:            (*pp)++;
                   1324:        }
                   1325:        return TRUE;
                   1326:     }
1.364     rillig   1327:
1.368     rillig   1328:     {
                   1329:        const char *msg = lstart[0] == '.' ? "Unknown directive"
                   1330:                                           : "Missing dependency operator";
                   1331:        Parse_Error(PARSE_FATAL, "%s", msg);
                   1332:        return FALSE;
                   1333:     }
                   1334: }
1.364     rillig   1335:
1.368     rillig   1336: static void
1.392     rillig   1337: ClearPaths(SearchPathList *paths)
                   1338: {
                   1339:     if (paths != NULL) {
                   1340:        SearchPathListNode *ln;
                   1341:        for (ln = paths->first; ln != NULL; ln = ln->next)
                   1342:            Dir_ClearPath(ln->datum);
                   1343:     }
                   1344:
                   1345:     Dir_SetPATH();
                   1346: }
                   1347:
                   1348: static void
1.395     rillig   1349: ParseDoDependencySourcesEmpty(ParseSpecial specType, SearchPathList *paths)
1.368     rillig   1350: {
                   1351:     switch (specType) {
1.404     rillig   1352:     case SP_SUFFIXES:
1.368     rillig   1353:        Suff_ClearSuffixes();
                   1354:        break;
1.404     rillig   1355:     case SP_PRECIOUS:
1.368     rillig   1356:        allPrecious = TRUE;
                   1357:        break;
1.404     rillig   1358:     case SP_IGNORE:
1.401     rillig   1359:        opts.ignoreErrors = TRUE;
1.368     rillig   1360:        break;
1.404     rillig   1361:     case SP_SILENT:
1.401     rillig   1362:        opts.beSilent = TRUE;
1.368     rillig   1363:        break;
1.404     rillig   1364:     case SP_PATH:
1.392     rillig   1365:        ClearPaths(paths);
1.368     rillig   1366:        break;
                   1367: #ifdef POSIX
1.404     rillig   1368:     case SP_POSIX:
1.368     rillig   1369:        Var_Set("%POSIX", "1003.2", VAR_GLOBAL);
                   1370:        break;
                   1371: #endif
                   1372:     default:
                   1373:        break;
                   1374:     }
                   1375: }
1.364     rillig   1376:
1.393     rillig   1377: static void
                   1378: AddToPaths(const char *dir, SearchPathList *paths)
                   1379: {
                   1380:     if (paths != NULL) {
                   1381:        SearchPathListNode *ln;
                   1382:        for (ln = paths->first; ln != NULL; ln = ln->next)
                   1383:            (void)Dir_AddDir(ln->datum, dir);
                   1384:     }
                   1385: }
                   1386:
1.368     rillig   1387: /*
                   1388:  * If the target was one that doesn't take files as its sources
                   1389:  * but takes something like suffixes, we take each
                   1390:  * space-separated word on the line as a something and deal
                   1391:  * with it accordingly.
                   1392:  *
                   1393:  * If the target was .SUFFIXES, we take each source as a
                   1394:  * suffix and add it to the list of suffixes maintained by the
                   1395:  * Suff module.
                   1396:  *
                   1397:  * If the target was a .PATH, we add the source as a directory
                   1398:  * to search on the search path.
                   1399:  *
                   1400:  * If it was .INCLUDES, the source is taken to be the suffix of
                   1401:  * files which will be #included and whose search path should
                   1402:  * be present in the .INCLUDES variable.
                   1403:  *
                   1404:  * If it was .LIBS, the source is taken to be the suffix of
                   1405:  * files which are considered libraries and whose search path
                   1406:  * should be present in the .LIBS variable.
                   1407:  *
                   1408:  * If it was .NULL, the source is the suffix to use when a file
                   1409:  * has no valid suffix.
                   1410:  *
                   1411:  * If it was .OBJDIR, the source is a new definition for .OBJDIR,
                   1412:  * and will cause make to do a new chdir to that path.
                   1413:  */
                   1414: static void
1.395     rillig   1415: ParseDoDependencySourceSpecial(ParseSpecial specType, char *word,
                   1416:                               SearchPathList *paths)
1.368     rillig   1417: {
                   1418:     switch (specType) {
1.404     rillig   1419:     case SP_SUFFIXES:
1.395     rillig   1420:        Suff_AddSuffix(word, &mainNode);
1.368     rillig   1421:        break;
1.404     rillig   1422:     case SP_PATH:
1.395     rillig   1423:        AddToPaths(word, paths);
1.368     rillig   1424:        break;
1.404     rillig   1425:     case SP_INCLUDES:
1.395     rillig   1426:        Suff_AddInclude(word);
1.368     rillig   1427:        break;
1.404     rillig   1428:     case SP_LIBS:
1.395     rillig   1429:        Suff_AddLib(word);
1.368     rillig   1430:        break;
1.404     rillig   1431:     case SP_NULL:
1.395     rillig   1432:        Suff_SetNull(word);
1.368     rillig   1433:        break;
1.404     rillig   1434:     case SP_OBJDIR:
1.395     rillig   1435:        Main_SetObjdir("%s", word);
1.368     rillig   1436:        break;
                   1437:     default:
                   1438:        break;
                   1439:     }
                   1440: }
1.364     rillig   1441:
1.368     rillig   1442: static Boolean
1.395     rillig   1443: ParseDoDependencyTargets(char **inout_cp,
                   1444:                         char **inout_line,
                   1445:                         const char *lstart,
                   1446:                         ParseSpecial *inout_specType,
                   1447:                         GNodeType *inout_tOp,
                   1448:                         SearchPathList **inout_paths,
                   1449:                         StringList *curTargs)
1.368     rillig   1450: {
                   1451:     char *cp = *inout_cp;
                   1452:     char *line = *inout_line;
                   1453:     char savec;
1.204     dholland 1454:
1.336     rillig   1455:     for (;;) {
1.204     dholland 1456:        /*
                   1457:         * Here LINE points to the beginning of the next word, and
                   1458:         * LSTART points to the actual beginning of the line.
                   1459:         */
                   1460:
                   1461:        /* Find the end of the next word. */
1.314     rillig   1462:        cp = line;
                   1463:        ParseDependencyTargetWord(&cp, lstart);
1.121     dsl      1464:
1.204     dholland 1465:        /*
                   1466:         * If the word is followed by a left parenthesis, it's the
                   1467:         * name of an object file inside an archive (ar file).
                   1468:         */
1.310     rillig   1469:        if (!ParseIsEscaped(lstart, cp) && *cp == '(') {
1.1       cgd      1470:            /*
1.203     joerg    1471:             * Archives must be handled specially to make sure the OP_ARCHV
                   1472:             * flag is set in their 'type' field, for one thing, and because
                   1473:             * things like "archive(file1.o file2.o file3.o)" are permissible.
                   1474:             * Arch_ParseArchive will set 'line' to be the first non-blank
                   1475:             * after the archive-spec. It creates/finds nodes for the members
1.272     rillig   1476:             * and places them on the given list, returning TRUE if all
                   1477:             * went well and FALSE if there was an error in the
1.203     joerg    1478:             * specification. On error, line should remain untouched.
1.1       cgd      1479:             */
1.411     rillig   1480:            if (!Arch_ParseArchive(&line, targets, VAR_CMDLINE)) {
1.97      christos 1481:                Parse_Error(PARSE_FATAL,
1.368     rillig   1482:                            "Error in archive specification: \"%s\"", line);
                   1483:                return FALSE;
1.203     joerg    1484:            } else {
1.204     dholland 1485:                /* Done with this word; on to the next. */
1.213     matthias 1486:                cp = line;
1.1       cgd      1487:                continue;
1.203     joerg    1488:            }
1.1       cgd      1489:        }
1.27      christos 1490:
1.1       cgd      1491:        if (!*cp) {
1.395     rillig   1492:            ParseErrorNoDependency(lstart);
1.368     rillig   1493:            return FALSE;
1.1       cgd      1494:        }
1.204     dholland 1495:
                   1496:        /* Insert a null terminator. */
                   1497:        savec = *cp;
1.1       cgd      1498:        *cp = '\0';
1.118     dsl      1499:
1.368     rillig   1500:        if (!ParseDoDependencyTarget(line, inout_specType, inout_tOp,
                   1501:                                     inout_paths))
                   1502:            return FALSE;
                   1503:
1.1       cgd      1504:        /*
1.368     rillig   1505:         * Have word in line. Get or create its node and stick it at
                   1506:         * the end of the targets list
1.1       cgd      1507:         */
1.404     rillig   1508:        if (*inout_specType == SP_NOT && *line != '\0') {
1.368     rillig   1509:            ParseDoDependencyTargetMundane(line, curTargs);
1.404     rillig   1510:        } else if (*inout_specType == SP_PATH && *line != '.' && *line != '\0') {
1.368     rillig   1511:            Parse_Error(PARSE_WARNING, "Extra target (%s) ignored", line);
                   1512:        }
                   1513:
                   1514:        /* Don't need the inserted null terminator any more. */
                   1515:        *cp = savec;
                   1516:
                   1517:        /*
                   1518:         * If it is a special type and not .PATH, it's the only target we
                   1519:         * allow on this line...
                   1520:         */
1.404     rillig   1521:        if (*inout_specType != SP_NOT && *inout_specType != SP_PATH) {
1.368     rillig   1522:            ParseDoDependencyTargetExtraWarn(&cp, lstart);
                   1523:        } else {
                   1524:            pp_skip_whitespace(&cp);
                   1525:        }
                   1526:        line = cp;
                   1527:        if (*line == '\0')
                   1528:            break;
                   1529:        if ((*line == '!' || *line == ':') && !ParseIsEscaped(lstart, line))
                   1530:            break;
                   1531:     }
                   1532:
                   1533:     *inout_cp = cp;
                   1534:     *inout_line = line;
                   1535:     return TRUE;
                   1536: }
1.27      christos 1537:
1.368     rillig   1538: static void
1.395     rillig   1539: ParseDoDependencySourcesSpecial(char *start, char *end,
1.368     rillig   1540:                                ParseSpecial specType, SearchPathList *paths)
                   1541: {
                   1542:     char savec;
1.204     dholland 1543:
1.395     rillig   1544:     while (*start) {
                   1545:        while (*end && !ch_isspace(*end))
                   1546:            end++;
                   1547:        savec = *end;
                   1548:        *end = '\0';
                   1549:        ParseDoDependencySourceSpecial(specType, start, paths);
                   1550:        *end = savec;
                   1551:        if (savec != '\0')
                   1552:            end++;
                   1553:        pp_skip_whitespace(&end);
                   1554:        start = end;
1.368     rillig   1555:     }
                   1556: }
                   1557:
                   1558: static Boolean
1.395     rillig   1559: ParseDoDependencySourcesMundane(char *start, char *end,
1.374     rillig   1560:                                ParseSpecial specType, GNodeType tOp)
1.368     rillig   1561: {
1.395     rillig   1562:     while (*start) {
1.368     rillig   1563:        /*
                   1564:         * The targets take real sources, so we must beware of archive
                   1565:         * specifications (i.e. things with left parentheses in them)
                   1566:         * and handle them accordingly.
                   1567:         */
1.395     rillig   1568:        for (; *end && !ch_isspace(*end); end++) {
                   1569:            if (*end == '(' && end > start && end[-1] != '$') {
1.367     rillig   1570:                /*
1.368     rillig   1571:                 * Only stop for a left parenthesis if it isn't at the
                   1572:                 * start of a word (that'll be for variable changes
                   1573:                 * later) and isn't preceded by a dollar sign (a dynamic
                   1574:                 * source).
1.367     rillig   1575:                 */
1.368     rillig   1576:                break;
                   1577:            }
                   1578:        }
                   1579:
1.395     rillig   1580:        if (*end == '(') {
1.385     rillig   1581:            GNodeList *sources = Lst_New();
1.411     rillig   1582:            if (!Arch_ParseArchive(&start, sources, VAR_CMDLINE)) {
1.368     rillig   1583:                Parse_Error(PARSE_FATAL,
1.395     rillig   1584:                            "Error in source archive spec \"%s\"", start);
1.368     rillig   1585:                return FALSE;
                   1586:            }
                   1587:
                   1588:            while (!Lst_IsEmpty(sources)) {
                   1589:                GNode *gn = Lst_Dequeue(sources);
                   1590:                ParseDoSrc(tOp, gn->name, specType);
                   1591:            }
                   1592:            Lst_Free(sources);
1.395     rillig   1593:            end = start;
1.368     rillig   1594:        } else {
1.395     rillig   1595:            if (*end) {
                   1596:                *end = '\0';
                   1597:                end++;
1.368     rillig   1598:            }
                   1599:
1.395     rillig   1600:            ParseDoSrc(tOp, start, specType);
1.368     rillig   1601:        }
1.395     rillig   1602:        pp_skip_whitespace(&end);
                   1603:        start = end;
1.368     rillig   1604:     }
                   1605:     return TRUE;
                   1606: }
1.1       cgd      1607:
1.368     rillig   1608: /* Parse a dependency line consisting of targets, followed by a dependency
                   1609:  * operator, optionally followed by sources.
                   1610:  *
                   1611:  * The nodes of the sources are linked as children to the nodes of the
                   1612:  * targets. Nodes are created as necessary.
                   1613:  *
                   1614:  * The operator is applied to each node in the global 'targets' list,
                   1615:  * which is where the nodes found for the targets are kept, by means of
                   1616:  * the ParseDoOp function.
                   1617:  *
                   1618:  * The sources are parsed in much the same way as the targets, except
                   1619:  * that they are expanded using the wildcarding scheme of the C-Shell,
                   1620:  * and all instances of the resulting words in the list of all targets
                   1621:  * are found. Each of the resulting nodes is then linked to each of the
                   1622:  * targets as one of its children.
                   1623:  *
                   1624:  * Certain targets and sources such as .PHONY or .PRECIOUS are handled
                   1625:  * specially. These are the ones detailed by the specType variable.
                   1626:  *
                   1627:  * The storing of transformation rules such as '.c.o' is also taken care of
                   1628:  * here. A target is recognized as a transformation rule by calling
                   1629:  * Suff_IsTransform. If it is a transformation rule, its node is gotten
                   1630:  * from the suffix module via Suff_AddTransform rather than the standard
                   1631:  * Targ_FindNode in the target module.
                   1632:  */
                   1633: static void
                   1634: ParseDoDependency(char *line)
                   1635: {
                   1636:     char *cp;                  /* our current position */
                   1637:     GNodeType op;              /* the operator on the line */
                   1638:     SearchPathList *paths;     /* search paths to alter when parsing
                   1639:                                 * a list of .PATH targets */
                   1640:     int tOp;                   /* operator from special target */
                   1641:     StringList *curTargs;      /* target names to be found and added
                   1642:                                 * to the targets list */
1.374     rillig   1643:     char *lstart = line;
1.366     rillig   1644:
1.368     rillig   1645:     /*
1.404     rillig   1646:      * specType contains the SPECial TYPE of the current target. It is SP_NOT
1.368     rillig   1647:      * if the target is unspecial. If it *is* special, however, the children
                   1648:      * are linked as children of the parent but not vice versa.
                   1649:      */
1.404     rillig   1650:     ParseSpecial specType = SP_NOT;
1.366     rillig   1651:
1.368     rillig   1652:     DEBUG1(PARSE, "ParseDoDependency(%s)\n", line);
                   1653:     tOp = 0;
1.366     rillig   1654:
1.368     rillig   1655:     paths = NULL;
1.366     rillig   1656:
1.385     rillig   1657:     curTargs = Lst_New();
1.365     rillig   1658:
1.368     rillig   1659:     /*
                   1660:      * First, grind through the targets.
                   1661:      */
                   1662:     if (!ParseDoDependencyTargets(&cp, &line, lstart, &specType, &tOp, &paths,
                   1663:                                  curTargs))
                   1664:        goto out;
1.365     rillig   1665:
1.1       cgd      1666:     /*
                   1667:      * Don't need the list of target names anymore...
                   1668:      */
1.268     rillig   1669:     Lst_Free(curTargs);
1.112     christos 1670:     curTargs = NULL;
1.1       cgd      1671:
1.368     rillig   1672:     if (!Lst_IsEmpty(targets))
1.373     rillig   1673:        ParseDoDependencyCheckSpec(specType);
1.1       cgd      1674:
                   1675:     /*
1.368     rillig   1676:      * Have now parsed all the target names. Must parse the operator next.
1.1       cgd      1677:      */
1.368     rillig   1678:     if (!ParseDoDependencyParseOp(&cp, lstart, &op))
1.373     rillig   1679:        goto out;
1.1       cgd      1680:
1.204     dholland 1681:     /*
                   1682:      * Apply the operator to the target. This is how we remember which
                   1683:      * operator a target was defined with. It fails if the operator
                   1684:      * used isn't consistent across all references.
                   1685:      */
1.337     rillig   1686:     ApplyDependencyOperator(op);
1.1       cgd      1687:
                   1688:     /*
1.204     dholland 1689:      * Onward to the sources.
                   1690:      *
                   1691:      * LINE will now point to the first source word, if any, or the
                   1692:      * end of the string if not.
1.1       cgd      1693:      */
1.368     rillig   1694:     pp_skip_whitespace(&cp);
1.1       cgd      1695:     line = cp;
                   1696:
                   1697:     /*
                   1698:      * Several special targets take different actions if present with no
                   1699:      * sources:
                   1700:      * a .SUFFIXES line with no sources clears out all old suffixes
                   1701:      * a .PRECIOUS line makes all targets precious
                   1702:      * a .IGNORE line ignores errors for all targets
                   1703:      * a .SILENT line creates silence when making all targets
                   1704:      * a .PATH removes all directories from the search path(s).
                   1705:      */
                   1706:     if (!*line) {
1.373     rillig   1707:        ParseDoDependencySourcesEmpty(specType, paths);
1.404     rillig   1708:     } else if (specType == SP_MFLAGS) {
1.1       cgd      1709:        /*
                   1710:         * Call on functions in main.c to deal with these arguments and
                   1711:         * set the initial character to a null-character so the loop to
                   1712:         * get sources won't get anything
                   1713:         */
1.97      christos 1714:        Main_ParseArgLine(line);
1.1       cgd      1715:        *line = '\0';
1.404     rillig   1716:     } else if (specType == SP_SHELL) {
1.272     rillig   1717:        if (!Job_ParseShell(line)) {
1.97      christos 1718:            Parse_Error(PARSE_FATAL, "improper shell specification");
1.112     christos 1719:            goto out;
1.1       cgd      1720:        }
                   1721:        *line = '\0';
1.404     rillig   1722:     } else if (specType == SP_NOTPARALLEL || specType == SP_SINGLESHELL ||
                   1723:               specType == SP_DELETE_ON_ERROR) {
1.1       cgd      1724:        *line = '\0';
                   1725:     }
1.27      christos 1726:
1.1       cgd      1727:     /*
1.27      christos 1728:      * NOW GO FOR THE SOURCES
1.1       cgd      1729:      */
1.404     rillig   1730:     if (specType == SP_SUFFIXES || specType == SP_PATH ||
                   1731:        specType == SP_INCLUDES || specType == SP_LIBS ||
                   1732:        specType == SP_NULL || specType == SP_OBJDIR)
1.1       cgd      1733:     {
1.373     rillig   1734:        ParseDoDependencySourcesSpecial(line, cp, specType, paths);
1.1       cgd      1735:        if (paths) {
1.268     rillig   1736:            Lst_Free(paths);
1.222     riastrad 1737:            paths = NULL;
1.1       cgd      1738:        }
1.404     rillig   1739:        if (specType == SP_PATH)
1.85      sjg      1740:            Dir_SetPATH();
1.1       cgd      1741:     } else {
1.222     riastrad 1742:        assert(paths == NULL);
1.373     rillig   1743:        if (!ParseDoDependencySourcesMundane(line, cp, specType, tOp))
                   1744:            goto out;
1.1       cgd      1745:     }
1.27      christos 1746:
1.335     rillig   1747:     FindMainTarget();
1.1       cgd      1748:
1.112     christos 1749: out:
1.263     rillig   1750:     if (paths != NULL)
1.268     rillig   1751:        Lst_Free(paths);
1.263     rillig   1752:     if (curTargs != NULL)
1.268     rillig   1753:        Lst_Free(curTargs);
1.1       cgd      1754: }
                   1755:
1.389     rillig   1756: typedef struct VarAssignParsed {
                   1757:     const char *nameStart;     /* unexpanded */
                   1758:     const char *nameEnd;       /* before operator adjustment */
                   1759:     const char *eq;            /* the '=' of the assignment operator */
                   1760: } VarAssignParsed;
                   1761:
1.388     rillig   1762: /* Determine the assignment operator and adjust the end of the variable
                   1763:  * name accordingly. */
                   1764: static void
1.389     rillig   1765: AdjustVarassignOp(const VarAssignParsed *pvar, const char *value,
                   1766:                  VarAssign *out_var)
1.388     rillig   1767: {
1.389     rillig   1768:     const char *op = pvar->eq;
                   1769:     const char * const name = pvar->nameStart;
1.388     rillig   1770:     VarAssignOp type;
                   1771:
                   1772:     if (op > name && op[-1] == '+') {
                   1773:        type = VAR_APPEND;
                   1774:        op--;
                   1775:
                   1776:     } else if (op > name && op[-1] == '?') {
                   1777:        op--;
                   1778:        type = VAR_DEFAULT;
                   1779:
                   1780:     } else if (op > name && op[-1] == ':') {
                   1781:        op--;
                   1782:        type = VAR_SUBST;
                   1783:
                   1784:     } else if (op > name && op[-1] == '!') {
                   1785:        op--;
                   1786:        type = VAR_SHELL;
                   1787:
                   1788:     } else {
                   1789:        type = VAR_NORMAL;
                   1790: #ifdef SUNSHCMD
                   1791:        while (op > name && ch_isspace(op[-1]))
                   1792:            op--;
                   1793:
                   1794:        if (op >= name + 3 && op[-3] == ':' && op[-2] == 's' && op[-1] == 'h') {
                   1795:            type = VAR_SHELL;
                   1796:            op -= 3;
                   1797:        }
                   1798: #endif
                   1799:     }
                   1800:
                   1801:     {
1.389     rillig   1802:        const char *nameEnd = pvar->nameEnd < op ? pvar->nameEnd : op;
                   1803:        out_var->varname = bmake_strsedup(pvar->nameStart, nameEnd);
                   1804:        out_var->op = type;
                   1805:        out_var->value = value;
1.388     rillig   1806:     }
                   1807: }
                   1808:
1.368     rillig   1809: /* Parse a variable assignment, consisting of a single-word variable name,
                   1810:  * optional whitespace, an assignment operator, optional whitespace and the
                   1811:  * variable value.
1.84      wiz      1812:  *
1.410     rillig   1813:  * Note: There is a lexical ambiguity with assignment modifier characters
                   1814:  * in variable names. This routine interprets the character before the =
                   1815:  * as a modifier. Therefore, an assignment like
                   1816:  *     C++=/usr/bin/CC
                   1817:  * is interpreted as "C+ +=" instead of "C++ =".
                   1818:  *
1.368     rillig   1819:  * Used for both lines in a file and command line arguments. */
1.1       cgd      1820: Boolean
1.368     rillig   1821: Parse_IsVar(const char *p, VarAssign *out_var)
1.1       cgd      1822: {
1.389     rillig   1823:     VarAssignParsed pvar;
1.368     rillig   1824:     const char *firstSpace = NULL;
1.154     dsl      1825:     char ch;
1.16      christos 1826:     int level = 0;
1.1       cgd      1827:
1.368     rillig   1828:     /* Skip to variable name */
                   1829:     while (*p == ' ' || *p == '\t')
                   1830:        p++;
                   1831:
                   1832:     /* During parsing, the '+' of the '+=' operator is initially parsed
                   1833:      * as part of the variable name.  It is later corrected, as is the ':sh'
                   1834:      * modifier. Of these two (nameEnd and op), the earlier one determines the
                   1835:      * actual end of the variable name. */
1.389     rillig   1836:     pvar.nameStart = p;
1.368     rillig   1837: #ifdef CLEANUP
1.389     rillig   1838:     pvar.nameEnd = NULL;
                   1839:     pvar.eq = NULL;
1.368     rillig   1840: #endif
1.356     rillig   1841:
1.154     dsl      1842:     /* Scan for one of the assignment operators outside a variable expansion */
1.368     rillig   1843:     while ((ch = *p++) != 0) {
1.154     dsl      1844:        if (ch == '(' || ch == '{') {
1.16      christos 1845:            level++;
1.154     dsl      1846:            continue;
                   1847:        }
                   1848:        if (ch == ')' || ch == '}') {
1.16      christos 1849:            level--;
1.154     dsl      1850:            continue;
                   1851:        }
1.368     rillig   1852:
1.154     dsl      1853:        if (level != 0)
                   1854:            continue;
1.368     rillig   1855:
                   1856:        if (ch == ' ' || ch == '\t')
                   1857:            if (firstSpace == NULL)
1.373     rillig   1858:                firstSpace = p - 1;
1.368     rillig   1859:        while (ch == ' ' || ch == '\t')
                   1860:            ch = *p++;
                   1861:
1.191     sjg      1862: #ifdef SUNSHCMD
1.368     rillig   1863:        if (ch == ':' && strncmp(p, "sh", 2) == 0) {
                   1864:            p += 2;
1.191     sjg      1865:            continue;
                   1866:        }
                   1867: #endif
1.368     rillig   1868:        if (ch == '=') {
1.389     rillig   1869:            pvar.eq = p - 1;
                   1870:            pvar.nameEnd = firstSpace != NULL ? firstSpace : p - 1;
1.368     rillig   1871:            cpp_skip_whitespace(&p);
1.389     rillig   1872:            AdjustVarassignOp(&pvar, p, out_var);
1.154     dsl      1873:            return TRUE;
1.368     rillig   1874:        }
                   1875:        if (*p == '=' && (ch == '+' || ch == ':' || ch == '?' || ch == '!')) {
1.389     rillig   1876:            pvar.eq = p;
                   1877:            pvar.nameEnd = firstSpace != NULL ? firstSpace : p;
1.368     rillig   1878:            p++;
                   1879:            cpp_skip_whitespace(&p);
1.389     rillig   1880:            AdjustVarassignOp(&pvar, p, out_var);
1.154     dsl      1881:            return TRUE;
1.368     rillig   1882:        }
                   1883:        if (firstSpace != NULL)
1.154     dsl      1884:            return FALSE;
                   1885:     }
1.1       cgd      1886:
1.154     dsl      1887:     return FALSE;
1.1       cgd      1888: }
                   1889:
1.368     rillig   1890: static void
                   1891: VarCheckSyntax(VarAssignOp type, const char *uvalue, GNode *ctxt)
                   1892: {
1.243     sjg      1893:     if (DEBUG(LINT)) {
1.368     rillig   1894:        if (type != VAR_SUBST && strchr(uvalue, '$') != NULL) {
                   1895:            /* Check for syntax errors such as unclosed expressions or
                   1896:             * unknown modifiers. */
1.325     rillig   1897:            char *expandedValue;
1.243     sjg      1898:
1.368     rillig   1899:            (void)Var_Subst(uvalue, ctxt, VARE_NONE, &expandedValue);
1.323     rillig   1900:            /* TODO: handle errors */
1.325     rillig   1901:            free(expandedValue);
1.243     sjg      1902:        }
                   1903:     }
1.368     rillig   1904: }
                   1905:
1.390     rillig   1906: static void
1.391     rillig   1907: VarAssign_EvalSubst(const char *name, const char *uvalue, GNode *ctxt,
1.390     rillig   1908:                    const char **out_avalue, void **out_avalue_freeIt)
                   1909: {
                   1910:     const char *avalue = uvalue;
                   1911:     char *evalue;
                   1912:     /*
                   1913:      * Allow variables in the old value to be undefined, but leave their
                   1914:      * expressions alone -- this is done by forcing oldVars to be false.
                   1915:      * XXX: This can cause recursive variables, but that's not hard to do,
                   1916:      * and this allows someone to do something like
                   1917:      *
                   1918:      *  CFLAGS = $(.INCLUDES)
                   1919:      *  CFLAGS := -I.. $(CFLAGS)
                   1920:      *
                   1921:      * And not get an error.
                   1922:      */
                   1923:     Boolean oldOldVars = oldVars;
                   1924:
                   1925:     oldVars = FALSE;
                   1926:
                   1927:     /*
                   1928:      * make sure that we set the variable the first time to nothing
                   1929:      * so that it gets substituted!
                   1930:      */
                   1931:     if (!Var_Exists(name, ctxt))
                   1932:        Var_Set(name, "", ctxt);
                   1933:
                   1934:     (void)Var_Subst(uvalue, ctxt, VARE_WANTRES|VARE_ASSIGN, &evalue);
                   1935:     /* TODO: handle errors */
                   1936:     oldVars = oldOldVars;
                   1937:     avalue = evalue;
                   1938:     Var_Set(name, avalue, ctxt);
                   1939:
                   1940:     *out_avalue = avalue;
                   1941:     *out_avalue_freeIt = evalue;
                   1942: }
                   1943:
                   1944: static void
1.391     rillig   1945: VarAssign_EvalShell(const char *name, const char *uvalue, GNode *ctxt,
1.390     rillig   1946:                    const char **out_avalue, void **out_avalue_freeIt)
                   1947: {
                   1948:     const char *cmd, *errfmt;
                   1949:     char *cmdOut;
                   1950:     void *cmd_freeIt = NULL;
                   1951:
                   1952:     cmd = uvalue;
                   1953:     if (strchr(cmd, '$') != NULL) {
                   1954:        char *ecmd;
1.411     rillig   1955:        (void)Var_Subst(cmd, VAR_CMDLINE, VARE_UNDEFERR | VARE_WANTRES, &ecmd);
1.390     rillig   1956:        /* TODO: handle errors */
                   1957:        cmd = cmd_freeIt = ecmd;
                   1958:     }
                   1959:
                   1960:     cmdOut = Cmd_Exec(cmd, &errfmt);
1.391     rillig   1961:     Var_Set(name, cmdOut, ctxt);
1.390     rillig   1962:     *out_avalue = *out_avalue_freeIt = cmdOut;
                   1963:
                   1964:     if (errfmt)
                   1965:        Parse_Error(PARSE_WARNING, errfmt, cmd);
                   1966:
                   1967:     free(cmd_freeIt);
                   1968: }
                   1969:
1.391     rillig   1970: /* Perform a variable assignment.
                   1971:  *
                   1972:  * The actual value of the variable is returned in *out_avalue and
                   1973:  * *out_avalue_freeIt.  Especially for VAR_SUBST and VAR_SHELL this can differ
                   1974:  * from the literal value.
                   1975:  *
                   1976:  * Return whether the assignment was actually done.  The assignment is only
                   1977:  * skipped if the operator is '?=' and the variable already exists. */
1.368     rillig   1978: static Boolean
1.391     rillig   1979: VarAssign_Eval(const char *name, VarAssignOp op, const char *uvalue,
                   1980:               GNode *ctxt, const char **out_avalue, void **out_avalue_freeIt)
1.368     rillig   1981: {
                   1982:     const char *avalue = uvalue;
                   1983:     void *avalue_freeIt = NULL;
1.353     rillig   1984:
1.391     rillig   1985:     if (op == VAR_APPEND) {
1.368     rillig   1986:        Var_Append(name, uvalue, ctxt);
1.391     rillig   1987:     } else if (op == VAR_SUBST) {
1.412     rillig   1988:        VarAssign_EvalSubst(name, uvalue, ctxt, &avalue, &avalue_freeIt);
1.391     rillig   1989:     } else if (op == VAR_SHELL) {
1.412     rillig   1990:        VarAssign_EvalShell(name, uvalue, ctxt, &avalue, &avalue_freeIt);
1.1       cgd      1991:     } else {
1.391     rillig   1992:        if (op == VAR_DEFAULT && Var_Exists(name, ctxt)) {
1.368     rillig   1993:            *out_avalue_freeIt = NULL;
                   1994:            return FALSE;
                   1995:        }
                   1996:
                   1997:        /* Normal assignment -- just do it. */
                   1998:        Var_Set(name, uvalue, ctxt);
1.1       cgd      1999:     }
1.368     rillig   2000:
                   2001:     *out_avalue = avalue;
                   2002:     *out_avalue_freeIt = avalue_freeIt;
                   2003:     return TRUE;
                   2004: }
                   2005:
                   2006: static void
                   2007: VarAssignSpecial(const char *name, const char *avalue)
                   2008: {
                   2009:     if (strcmp(name, MAKEOVERRIDES) == 0)
1.74      tv       2010:        Main_ExportMAKEFLAGS(FALSE);    /* re-export MAKEFLAGS */
1.368     rillig   2011:     else if (strcmp(name, ".CURDIR") == 0) {
1.85      sjg      2012:        /*
1.321     rillig   2013:         * Someone is being (too?) clever...
1.85      sjg      2014:         * Let's pretend they know what they are doing and
1.321     rillig   2015:         * re-initialize the 'cur' CachedDir.
1.85      sjg      2016:         */
1.368     rillig   2017:        Dir_InitCur(avalue);
1.85      sjg      2018:        Dir_SetPATH();
1.368     rillig   2019:     } else if (strcmp(name, MAKE_JOB_PREFIX) == 0) {
1.135     sjg      2020:        Job_SetPrefix();
1.368     rillig   2021:     } else if (strcmp(name, MAKE_EXPORTED) == 0) {
                   2022:        Var_Export(avalue, FALSE);
1.85      sjg      2023:     }
1.368     rillig   2024: }
                   2025:
1.410     rillig   2026: /* Perform the variable variable assignment in the given context. */
1.368     rillig   2027: void
                   2028: Parse_DoVar(VarAssign *var, GNode *ctxt)
                   2029: {
                   2030:     const char *avalue;                /* actual value (maybe expanded) */
                   2031:     void *avalue_freeIt;
                   2032:
                   2033:     VarCheckSyntax(var->op, var->value, ctxt);
1.391     rillig   2034:     if (VarAssign_Eval(var->varname, var->op, var->value, ctxt,
                   2035:                       &avalue, &avalue_freeIt))
1.368     rillig   2036:        VarAssignSpecial(var->varname, avalue);
                   2037:
                   2038:     free(avalue_freeIt);
                   2039:     free(var->varname);
1.1       cgd      2040: }
1.23      christos 2041:
1.200     christos 2042:
1.203     joerg    2043: /*
1.195     christos 2044:  * ParseMaybeSubMake --
1.338     rillig   2045:  *     Scan the command string to see if it a possible submake node
1.195     christos 2046:  * Input:
                   2047:  *     cmd             the command to scan
                   2048:  * Results:
                   2049:  *     TRUE if the command is possibly a submake, FALSE if not.
                   2050:  */
                   2051: static Boolean
                   2052: ParseMaybeSubMake(const char *cmd)
                   2053: {
1.197     justin   2054:     size_t i;
1.195     christos 2055:     static struct {
                   2056:        const char *name;
                   2057:        size_t len;
                   2058:     } vals[] = {
                   2059: #define MKV(A) {       A, sizeof(A) - 1        }
                   2060:        MKV("${MAKE}"),
                   2061:        MKV("${.MAKE}"),
                   2062:        MKV("$(MAKE)"),
                   2063:        MKV("$(.MAKE)"),
                   2064:        MKV("make"),
                   2065:     };
1.375     rillig   2066:     for (i = 0; i < sizeof vals / sizeof vals[0]; i++) {
1.195     christos 2067:        char *ptr;
                   2068:        if ((ptr = strstr(cmd, vals[i].name)) == NULL)
                   2069:            continue;
1.290     rillig   2070:        if ((ptr == cmd || !ch_isalnum(ptr[-1]))
                   2071:            && !ch_isalnum(ptr[vals[i].len]))
1.195     christos 2072:            return TRUE;
                   2073:     }
                   2074:     return FALSE;
                   2075: }
                   2076:
1.327     rillig   2077: /* Append the command to the target node.
1.1       cgd      2078:  *
1.327     rillig   2079:  * The node may be marked as a submake node if the command is determined to
                   2080:  * be that. */
                   2081: static void
                   2082: ParseAddCmd(GNode *gn, char *cmd)
1.9       jtc      2083: {
1.120     dsl      2084:     /* Add to last (ie current) cohort for :: targets */
1.327     rillig   2085:     if ((gn->type & OP_DOUBLEDEP) && gn->cohorts->last != NULL)
                   2086:        gn = gn->cohorts->last->datum;
1.120     dsl      2087:
1.203     joerg    2088:     /* if target already supplied, ignore commands */
                   2089:     if (!(gn->type & OP_HAS_COMMANDS)) {
1.268     rillig   2090:        Lst_Append(gn->commands, cmd);
1.203     joerg    2091:        if (ParseMaybeSubMake(cmd))
                   2092:            gn->type |= OP_SUBMAKE;
                   2093:        ParseMark(gn);
                   2094:     } else {
1.327     rillig   2095: #if 0
1.203     joerg    2096:        /* XXX: We cannot do this until we fix the tree */
1.268     rillig   2097:        Lst_Append(gn->commands, cmd);
1.203     joerg    2098:        Parse_Error(PARSE_WARNING,
                   2099:                     "overriding commands for target \"%s\"; "
                   2100:                     "previous commands defined at %s: %d ignored",
                   2101:                     gn->name, gn->fname, gn->lineno);
                   2102: #else
                   2103:        Parse_Error(PARSE_WARNING,
1.374     rillig   2104:                    "duplicate script for target \"%s\" ignored",
                   2105:                    gn->name);
1.369     rillig   2106:        ParseErrorInternal(gn->fname, (size_t)gn->lineno, PARSE_WARNING,
1.374     rillig   2107:                           "using previous script for \"%s\" defined here",
                   2108:                           gn->name);
1.203     joerg    2109: #endif
1.200     christos 2110:     }
1.1       cgd      2111: }
                   2112:
1.284     rillig   2113: /* Add a directory to the path searched for included makefiles bracketed
                   2114:  * by double-quotes. */
1.1       cgd      2115: void
1.344     rillig   2116: Parse_AddIncludeDir(const char *dir)
1.1       cgd      2117: {
1.103     christos 2118:     (void)Dir_AddDir(parseIncPath, dir);
1.1       cgd      2119: }
                   2120:
1.284     rillig   2121: /* Push to another file.
1.1       cgd      2122:  *
1.284     rillig   2123:  * The input is the line minus the '.'. A file spec is a string enclosed in
                   2124:  * <> or "". The <> file is looked for only in sysIncPath. The "" file is
                   2125:  * first searched in the parsedir and then in the directories specified by
                   2126:  * the -I command line options.
1.1       cgd      2127:  */
                   2128: static void
1.210     sjg      2129: Parse_include_file(char *file, Boolean isSystem, Boolean depinc, int silent)
1.1       cgd      2130: {
1.170     dholland 2131:     struct loadedfile *lf;
1.374     rillig   2132:     char *fullname;            /* full pathname of file */
                   2133:     char *newName;
                   2134:     char *prefEnd, *incdir;
                   2135:     int fd;
                   2136:     int i;
1.1       cgd      2137:
                   2138:     /*
                   2139:      * Now we know the file's name and its search path, we attempt to
                   2140:      * find the durn thing. A return of NULL indicates the file don't
                   2141:      * exist.
                   2142:      */
1.147     joerg    2143:     fullname = file[0] == '/' ? bmake_strdup(file) : NULL;
1.76      reinoud  2144:
1.140     dsl      2145:     if (fullname == NULL && !isSystem) {
1.1       cgd      2146:        /*
                   2147:         * Include files contained in double-quotes are first searched for
                   2148:         * relative to the including file's location. We don't want to
                   2149:         * cd there, of course, so we just tack on the old file's
                   2150:         * leading path components and call Dir_FindFile to see if
                   2151:         * we can locate the beast.
                   2152:         */
                   2153:
1.408     rillig   2154:        incdir = bmake_strdup(CurFile()->fname);
1.140     dsl      2155:        prefEnd = strrchr(incdir, '/');
1.105     christos 2156:        if (prefEnd != NULL) {
1.1       cgd      2157:            *prefEnd = '\0';
1.140     dsl      2158:            /* Now do lexical processing of leading "../" on the filename */
                   2159:            for (i = 0; strncmp(file + i, "../", 3) == 0; i += 3) {
                   2160:                prefEnd = strrchr(incdir + 1, '/');
                   2161:                if (prefEnd == NULL || strcmp(prefEnd, "/..") == 0)
                   2162:                    break;
                   2163:                *prefEnd = '\0';
                   2164:            }
1.251     rillig   2165:            newName = str_concat3(incdir, "/", file + i);
1.97      christos 2166:            fullname = Dir_FindFile(newName, parseIncPath);
1.140     dsl      2167:            if (fullname == NULL)
1.1       cgd      2168:                fullname = Dir_FindFile(newName, dirSearchPath);
1.103     christos 2169:            free(newName);
1.1       cgd      2170:        }
1.140     dsl      2171:        free(incdir);
                   2172:
1.142     sjg      2173:        if (fullname == NULL) {
1.76      reinoud  2174:            /*
1.246     rillig   2175:             * Makefile wasn't found in same directory as included makefile.
1.76      reinoud  2176:             * Search for it first on the -I search path,
                   2177:             * then on the .PATH search path, if not found in a -I directory.
1.142     sjg      2178:             * If we have a suffix specific path we should use that.
1.76      reinoud  2179:             */
1.142     sjg      2180:            char *suff;
1.322     rillig   2181:            SearchPath *suffPath = NULL;
1.142     sjg      2182:
1.145     christos 2183:            if ((suff = strrchr(file, '.'))) {
1.142     sjg      2184:                suffPath = Suff_GetPath(suff);
1.152     dsl      2185:                if (suffPath != NULL) {
1.142     sjg      2186:                    fullname = Dir_FindFile(file, suffPath);
                   2187:                }
                   2188:            }
1.105     christos 2189:            if (fullname == NULL) {
1.142     sjg      2190:                fullname = Dir_FindFile(file, parseIncPath);
                   2191:                if (fullname == NULL) {
                   2192:                    fullname = Dir_FindFile(file, dirSearchPath);
                   2193:                }
1.76      reinoud  2194:            }
1.142     sjg      2195:        }
1.1       cgd      2196:     }
                   2197:
1.76      reinoud  2198:     /* Looking for a system file or file still not found */
1.105     christos 2199:     if (fullname == NULL) {
1.1       cgd      2200:        /*
1.76      reinoud  2201:         * Look for it on the system path
1.1       cgd      2202:         */
1.409     rillig   2203:        SearchPath *path = Lst_IsEmpty(sysIncPath) ? defSysIncPath : sysIncPath;
1.375     rillig   2204:        fullname = Dir_FindFile(file, path);
1.1       cgd      2205:     }
                   2206:
1.105     christos 2207:     if (fullname == NULL) {
1.38      christos 2208:        if (!silent)
1.97      christos 2209:            Parse_Error(PARSE_FATAL, "Could not find %s", file);
1.1       cgd      2210:        return;
                   2211:     }
                   2212:
1.125     dsl      2213:     /* Actually open the file... */
                   2214:     fd = open(fullname, O_RDONLY);
                   2215:     if (fd == -1) {
1.38      christos 2216:        if (!silent)
1.97      christos 2217:            Parse_Error(PARSE_FATAL, "Cannot open %s", fullname);
1.140     dsl      2218:        free(fullname);
1.125     dsl      2219:        return;
1.123     dsl      2220:     }
1.125     dsl      2221:
1.170     dholland 2222:     /* load it */
                   2223:     lf = loadfile(fullname, fd);
                   2224:
1.125     dsl      2225:     /* Start reading from this file next */
1.170     dholland 2226:     Parse_SetInput(fullname, 0, -1, loadedfile_nextbuf, lf);
1.408     rillig   2227:     CurFile()->lf = lf;
1.210     sjg      2228:     if (depinc)
1.374     rillig   2229:        doing_depend = depinc;  /* only turn it on */
1.123     dsl      2230: }
                   2231:
                   2232: static void
                   2233: ParseDoInclude(char *line)
                   2234: {
1.374     rillig   2235:     char endc;                 /* the character which ends the file spec */
                   2236:     char *cp;                  /* current position in file spec */
                   2237:     int silent = *line != 'i';
1.375     rillig   2238:     char *file = line + (silent ? 8 : 7);
1.123     dsl      2239:
                   2240:     /* Skip to delimiter character so we know where to look */
                   2241:     while (*file == ' ' || *file == '\t')
                   2242:        file++;
                   2243:
                   2244:     if (*file != '"' && *file != '<') {
                   2245:        Parse_Error(PARSE_FATAL,
1.374     rillig   2246:                    ".include filename must be delimited by '\"' or '<'");
1.123     dsl      2247:        return;
                   2248:     }
                   2249:
                   2250:     /*
                   2251:      * Set the search path on which to find the include file based on the
                   2252:      * characters which bracket its name. Angle-brackets imply it's
                   2253:      * a system Makefile while double-quotes imply it's a user makefile
                   2254:      */
                   2255:     if (*file == '<') {
                   2256:        endc = '>';
                   2257:     } else {
                   2258:        endc = '"';
                   2259:     }
                   2260:
                   2261:     /* Skip to matching delimiter */
                   2262:     for (cp = ++file; *cp && *cp != endc; cp++)
                   2263:        continue;
                   2264:
                   2265:     if (*cp != endc) {
                   2266:        Parse_Error(PARSE_FATAL,
1.374     rillig   2267:                    "Unclosed %cinclude filename. '%c' expected",
                   2268:                    '.', endc);
1.123     dsl      2269:        return;
1.1       cgd      2270:     }
1.123     dsl      2271:     *cp = '\0';
                   2272:
                   2273:     /*
                   2274:      * Substitute for any variables in the file name before trying to
                   2275:      * find the thing.
                   2276:      */
1.411     rillig   2277:     (void)Var_Subst(file, VAR_CMDLINE, VARE_WANTRES, &file);
1.323     rillig   2278:     /* TODO: handle errors */
1.123     dsl      2279:
1.242     rillig   2280:     Parse_include_file(file, endc == '>', *line == 'd', silent);
1.123     dsl      2281:     free(file);
1.1       cgd      2282: }
                   2283:
1.281     rillig   2284: /* Split filename into dirname + basename, then assign these to the
                   2285:  * given variables. */
1.193     christos 2286: static void
1.281     rillig   2287: SetFilenameVars(const char *filename, const char *dirvar, const char *filevar)
1.193     christos 2288: {
1.281     rillig   2289:     const char *slash, *dirname, *basename;
                   2290:     void *freeIt;
                   2291:
                   2292:     slash = strrchr(filename, '/');
                   2293:     if (slash == NULL) {
                   2294:        dirname = curdir;
                   2295:        basename = filename;
                   2296:        freeIt = NULL;
                   2297:     } else {
                   2298:        dirname = freeIt = bmake_strsedup(filename, slash);
                   2299:        basename = slash + 1;
                   2300:     }
1.193     christos 2301:
1.281     rillig   2302:     Var_Set(dirvar, dirname, VAR_GLOBAL);
                   2303:     Var_Set(filevar, basename, VAR_GLOBAL);
1.193     christos 2304:
1.341     rillig   2305:     DEBUG5(PARSE, "%s: ${%s} = `%s' ${%s} = `%s'\n",
                   2306:           __func__, dirvar, dirname, filevar, basename);
1.281     rillig   2307:     free(freeIt);
                   2308: }
                   2309:
                   2310: /* Return the immediately including file.
                   2311:  *
                   2312:  * This is made complicated since the .for loop is implemented as a special
                   2313:  * kind of .include; see For_Run. */
                   2314: static const char *
                   2315: GetActuallyIncludingFile(void)
                   2316: {
                   2317:     size_t i;
1.408     rillig   2318:     const IFile *incs = GetInclude(0);
1.193     christos 2319:
1.408     rillig   2320:     for (i = includes.len; i >= 2; i--)
                   2321:        if (!incs[i - 1].fromForLoop)
                   2322:            return incs[i - 2].fname;
1.282     rillig   2323:     return NULL;
1.193     christos 2324: }
1.281     rillig   2325:
1.285     rillig   2326: /* Set .PARSEDIR, .PARSEFILE, .INCLUDEDFROMDIR and .INCLUDEDFROMFILE. */
1.44      aidan    2327: static void
1.122     dsl      2328: ParseSetParseFile(const char *filename)
1.44      aidan    2329: {
1.281     rillig   2330:     const char *including;
1.44      aidan    2331:
1.281     rillig   2332:     SetFilenameVars(filename, ".PARSEDIR", ".PARSEFILE");
                   2333:
                   2334:     including = GetActuallyIncludingFile();
                   2335:     if (including != NULL) {
                   2336:        SetFilenameVars(including,
                   2337:                        ".INCLUDEDFROMDIR", ".INCLUDEDFROMFILE");
1.44      aidan    2338:     } else {
1.281     rillig   2339:        Var_Delete(".INCLUDEDFROMDIR", VAR_GLOBAL);
                   2340:        Var_Delete(".INCLUDEDFROMFILE", VAR_GLOBAL);
1.44      aidan    2341:     }
                   2342: }
                   2343:
1.284     rillig   2344: /* Track the makefiles we read - so makefiles can set dependencies on them.
                   2345:  * Avoid adding anything more than once. */
1.184     sjg      2346: static void
                   2347: ParseTrackInput(const char *name)
                   2348: {
1.413   ! rillig   2349:     void *fp = NULL;
1.236     rillig   2350:
1.244     rillig   2351:     const char *old = Var_Value(MAKE_MAKEFILES, VAR_GLOBAL, &fp);
1.184     sjg      2352:     if (old) {
1.244     rillig   2353:        size_t name_len = strlen(name);
                   2354:        const char *ep = old + strlen(old) - name_len;
1.184     sjg      2355:        /* does it contain name? */
                   2356:        for (; old != NULL; old = strchr(old, ' ')) {
                   2357:            if (*old == ' ')
                   2358:                old++;
1.206     sjg      2359:            if (old >= ep)
1.374     rillig   2360:                break;          /* cannot contain name */
                   2361:            if (memcmp(old, name, name_len) == 0 &&
                   2362:                (old[name_len] == '\0' || old[name_len] == ' '))
1.184     sjg      2363:                goto cleanup;
                   2364:        }
                   2365:     }
1.374     rillig   2366:     Var_Append(MAKE_MAKEFILES, name, VAR_GLOBAL);
                   2367: cleanup:
1.245     rillig   2368:     bmake_free(fp);
1.184     sjg      2369: }
1.137     sjg      2370:
1.44      aidan    2371:
1.276     rillig   2372: /* Start Parsing from the given source.
1.5       cgd      2373:  *
1.276     rillig   2374:  * The given file is added to the includes stack. */
1.5       cgd      2375: void
1.170     dholland 2376: Parse_SetInput(const char *name, int line, int fd,
1.374     rillig   2377:               char *(*nextbuf)(void *, size_t *), void *arg)
1.5       cgd      2378: {
1.408     rillig   2379:     IFile *curFile;
1.155     dsl      2380:     char *buf;
1.170     dholland 2381:     size_t len;
1.281     rillig   2382:     Boolean fromForLoop = name == NULL;
1.155     dsl      2383:
1.281     rillig   2384:     if (fromForLoop)
1.408     rillig   2385:        name = CurFile()->fname;
1.139     dsl      2386:     else
1.184     sjg      2387:        ParseTrackInput(name);
1.127     dsl      2388:
                   2389:     if (DEBUG(PARSE))
1.342     rillig   2390:        debug_printf("%s: file %s, line %d, fd %d, nextbuf %s, arg %p\n",
                   2391:                     __func__, name, line, fd,
                   2392:                     nextbuf == loadedfile_nextbuf ? "loadedfile" : "other",
                   2393:                     arg);
1.127     dsl      2394:
1.155     dsl      2395:     if (fd == -1 && nextbuf == NULL)
1.125     dsl      2396:        /* sanity */
                   2397:        return;
                   2398:
1.408     rillig   2399:     curFile = Vector_Push(&includes);
1.125     dsl      2400:
                   2401:     /*
                   2402:      * Once the previous state has been saved, we can get down to reading
                   2403:      * the new file. We set up the name of the file to be the absolute
                   2404:      * name of the include file so error messages refer to the right
                   2405:      * place.
                   2406:      */
1.189     sjg      2407:     curFile->fname = bmake_strdup(name);
1.281     rillig   2408:     curFile->fromForLoop = fromForLoop;
1.125     dsl      2409:     curFile->lineno = line;
1.155     dsl      2410:     curFile->first_lineno = line;
                   2411:     curFile->nextbuf = nextbuf;
                   2412:     curFile->nextbuf_arg = arg;
1.170     dholland 2413:     curFile->lf = NULL;
1.212     sjg      2414:     curFile->depending = doing_depend; /* restore this on EOF */
1.170     dholland 2415:
                   2416:     assert(nextbuf != NULL);
1.125     dsl      2417:
1.170     dholland 2418:     /* Get first block of input data */
                   2419:     buf = curFile->nextbuf(curFile->nextbuf_arg, &len);
                   2420:     if (buf == NULL) {
1.246     rillig   2421:        /* Was all a waste of time ... */
1.189     sjg      2422:        if (curFile->fname)
                   2423:            free(curFile->fname);
1.170     dholland 2424:        free(curFile);
                   2425:        return;
1.125     dsl      2426:     }
1.403     rillig   2427:     curFile->buf_freeIt = buf;
                   2428:     curFile->buf_ptr = buf;
                   2429:     curFile->buf_end = buf + len;
1.5       cgd      2430:
1.155     dsl      2431:     curFile->cond_depth = Cond_save_depth();
                   2432:     ParseSetParseFile(name);
1.5       cgd      2433: }
                   2434:
1.375     rillig   2435: /* Check if the directive is an include directive. */
1.228     christos 2436: static Boolean
1.298     rillig   2437: IsInclude(const char *dir, Boolean sysv)
1.228     christos 2438: {
1.298     rillig   2439:        if (dir[0] == 's' || dir[0] == '-' || (dir[0] == 'd' && !sysv))
                   2440:                dir++;
1.228     christos 2441:
1.298     rillig   2442:        if (strncmp(dir, "include", 7) != 0)
1.228     christos 2443:                return FALSE;
                   2444:
1.248     rillig   2445:        /* Space is not mandatory for BSD .include */
1.298     rillig   2446:        return !sysv || ch_isspace(dir[7]);
1.228     christos 2447: }
                   2448:
                   2449:
1.5       cgd      2450: #ifdef SYSVINCLUDE
1.284     rillig   2451: /* Check if the line is a SYSV include directive. */
1.228     christos 2452: static Boolean
                   2453: IsSysVInclude(const char *line)
                   2454: {
                   2455:        const char *p;
                   2456:
                   2457:        if (!IsInclude(line, TRUE))
                   2458:                return FALSE;
                   2459:
1.375     rillig   2460:        /* Avoid interpreting a dependency line as an include */
1.228     christos 2461:        for (p = line; (p = strchr(p, ':')) != NULL;) {
                   2462:                if (*++p == '\0') {
                   2463:                        /* end of line -> dependency */
                   2464:                        return FALSE;
                   2465:                }
1.290     rillig   2466:                if (*p == ':' || ch_isspace(*p)) {
1.228     christos 2467:                        /* :: operator or ': ' -> dependency */
                   2468:                        return FALSE;
                   2469:                }
                   2470:        }
                   2471:        return TRUE;
                   2472: }
                   2473:
1.284     rillig   2474: /* Push to another file.  The line points to the word "include". */
1.5       cgd      2475: static void
1.84      wiz      2476: ParseTraditionalInclude(char *line)
1.5       cgd      2477: {
1.374     rillig   2478:     char *cp;                  /* current position in file spec */
                   2479:     int done = 0;
                   2480:     int silent = line[0] != 'i';
                   2481:     char *file = line + (silent ? 8 : 7);
                   2482:     char *all_files;
1.5       cgd      2483:
1.341     rillig   2484:     DEBUG2(PARSE, "%s: %s\n", __func__, file);
1.111     ginsbach 2485:
1.368     rillig   2486:     pp_skip_whitespace(&file);
1.5       cgd      2487:
1.111     ginsbach 2488:     /*
                   2489:      * Substitute for any variables in the file name before trying to
                   2490:      * find the thing.
                   2491:      */
1.411     rillig   2492:     (void)Var_Subst(file, VAR_CMDLINE, VARE_WANTRES, &all_files);
1.323     rillig   2493:     /* TODO: handle errors */
1.111     ginsbach 2494:
1.5       cgd      2495:     if (*file == '\0') {
1.374     rillig   2496:        Parse_Error(PARSE_FATAL, "Filename missing from \"include\"");
1.224     riastrad 2497:        goto out;
1.5       cgd      2498:     }
                   2499:
1.123     dsl      2500:     for (file = all_files; !done; file = cp + 1) {
                   2501:        /* Skip to end of line or next whitespace */
1.290     rillig   2502:        for (cp = file; *cp && !ch_isspace(*cp); cp++)
1.38      christos 2503:            continue;
                   2504:
                   2505:        if (*cp)
                   2506:            *cp = '\0';
                   2507:        else
                   2508:            done = 1;
                   2509:
1.210     sjg      2510:        Parse_include_file(file, FALSE, FALSE, silent);
1.5       cgd      2511:     }
1.224     riastrad 2512: out:
1.123     dsl      2513:     free(all_files);
1.5       cgd      2514: }
                   2515: #endif
                   2516:
1.183     sjg      2517: #ifdef GMAKEEXPORT
1.375     rillig   2518: /* Parse "export <variable>=<value>", and actually export it. */
1.182     christos 2519: static void
                   2520: ParseGmakeExport(char *line)
                   2521: {
1.375     rillig   2522:     char *variable = line + 6;
1.374     rillig   2523:     char *value;
1.182     christos 2524:
1.341     rillig   2525:     DEBUG2(PARSE, "%s: %s\n", __func__, variable);
1.182     christos 2526:
1.368     rillig   2527:     pp_skip_whitespace(&variable);
1.182     christos 2528:
                   2529:     for (value = variable; *value && *value != '='; value++)
                   2530:        continue;
                   2531:
                   2532:     if (*value != '=') {
                   2533:        Parse_Error(PARSE_FATAL,
1.284     rillig   2534:                    "Variable/Value missing from \"export\"");
1.182     christos 2535:        return;
                   2536:     }
1.374     rillig   2537:     *value++ = '\0';           /* terminate variable */
1.182     christos 2538:
                   2539:     /*
1.183     sjg      2540:      * Expand the value before putting it in the environment.
1.182     christos 2541:      */
1.411     rillig   2542:     (void)Var_Subst(value, VAR_CMDLINE, VARE_WANTRES, &value);
1.323     rillig   2543:     /* TODO: handle errors */
                   2544:
1.182     christos 2545:     setenv(variable, value, 1);
1.223     riastrad 2546:     free(value);
1.182     christos 2547: }
                   2548: #endif
                   2549:
1.284     rillig   2550: /* Called when EOF is reached in the current file. If we were reading an
                   2551:  * include file, the includes stack is popped and things set up to go back
                   2552:  * to reading the previous file at the previous location.
1.1       cgd      2553:  *
                   2554:  * Results:
1.376     rillig   2555:  *     TRUE to continue parsing, i.e. it had only reached the end of an
                   2556:  *     included file, FALSE if the main file has been parsed completely.
1.1       cgd      2557:  */
1.376     rillig   2558: static Boolean
1.123     dsl      2559: ParseEOF(void)
1.1       cgd      2560: {
1.155     dsl      2561:     char *ptr;
1.170     dholland 2562:     size_t len;
1.408     rillig   2563:     IFile *curFile = CurFile();
1.170     dholland 2564:
                   2565:     assert(curFile->nextbuf != NULL);
1.155     dsl      2566:
1.210     sjg      2567:     doing_depend = curFile->depending; /* restore this */
1.170     dholland 2568:     /* get next input buffer, if any */
                   2569:     ptr = curFile->nextbuf(curFile->nextbuf_arg, &len);
1.403     rillig   2570:     curFile->buf_ptr = ptr;
                   2571:     curFile->buf_freeIt = ptr;
                   2572:     curFile->buf_end = ptr + len;
1.170     dholland 2573:     curFile->lineno = curFile->first_lineno;
                   2574:     if (ptr != NULL) {
                   2575:        /* Iterate again */
1.376     rillig   2576:        return TRUE;
1.155     dsl      2577:     }
                   2578:
1.132     dsl      2579:     /* Ensure the makefile (or loop) didn't have mismatched conditionals */
                   2580:     Cond_restore_depth(curFile->cond_depth);
1.131     dsl      2581:
1.170     dholland 2582:     if (curFile->lf != NULL) {
1.374     rillig   2583:        loadedfile_destroy(curFile->lf);
                   2584:        curFile->lf = NULL;
1.170     dholland 2585:     }
                   2586:
1.125     dsl      2587:     /* Dispose of curFile info */
                   2588:     /* Leak curFile->fname because all the gnodes have pointers to it */
1.403     rillig   2589:     free(curFile->buf_freeIt);
1.408     rillig   2590:     Vector_Pop(&includes);
1.125     dsl      2591:
1.400     rillig   2592:     if (includes.len == 0) {
1.125     dsl      2593:        /* We've run out of input */
1.44      aidan    2594:        Var_Delete(".PARSEDIR", VAR_GLOBAL);
                   2595:        Var_Delete(".PARSEFILE", VAR_GLOBAL);
1.194     christos 2596:        Var_Delete(".INCLUDEDFROMDIR", VAR_GLOBAL);
                   2597:        Var_Delete(".INCLUDEDFROMFILE", VAR_GLOBAL);
1.376     rillig   2598:        return FALSE;
1.1       cgd      2599:     }
                   2600:
1.408     rillig   2601:     curFile = CurFile();
1.341     rillig   2602:     DEBUG2(PARSE, "ParseEOF: returning to file %s, line %d\n",
                   2603:           curFile->fname, curFile->lineno);
1.127     dsl      2604:
1.125     dsl      2605:     ParseSetParseFile(curFile->fname);
1.376     rillig   2606:     return TRUE;
1.1       cgd      2607: }
                   2608:
1.127     dsl      2609: #define PARSE_RAW 1
                   2610: #define PARSE_SKIP 2
                   2611:
                   2612: static char *
1.368     rillig   2613: ParseGetLine(int flags)
1.5       cgd      2614: {
1.408     rillig   2615:     IFile *cf = CurFile();
1.127     dsl      2616:     char *ptr;
1.125     dsl      2617:     char ch;
1.127     dsl      2618:     char *line;
                   2619:     char *line_end;
                   2620:     char *escaped;
                   2621:     char *comment;
                   2622:     char *tp;
1.125     dsl      2623:
1.127     dsl      2624:     /* Loop through blank lines and comment lines */
1.125     dsl      2625:     for (;;) {
1.127     dsl      2626:        cf->lineno++;
1.403     rillig   2627:        line = cf->buf_ptr;
1.127     dsl      2628:        ptr = line;
                   2629:        line_end = line;
                   2630:        escaped = NULL;
                   2631:        comment = NULL;
                   2632:        for (;;) {
1.403     rillig   2633:            /* XXX: can buf_end ever be null? */
                   2634:            if (cf->buf_end != NULL && ptr == cf->buf_end) {
1.170     dholland 2635:                /* end of buffer */
                   2636:                ch = 0;
                   2637:                break;
                   2638:            }
1.127     dsl      2639:            ch = *ptr;
                   2640:            if (ch == 0 || (ch == '\\' && ptr[1] == 0)) {
1.403     rillig   2641:                /* XXX: can buf_end ever be null? */
                   2642:                if (cf->buf_end == NULL)
1.127     dsl      2643:                    /* End of string (aka for loop) data */
                   2644:                    break;
1.190     sjg      2645:                /* see if there is more we can parse */
1.403     rillig   2646:                while (ptr++ < cf->buf_end) {
1.190     sjg      2647:                    if ((ch = *ptr) == '\n') {
                   2648:                        if (ptr > line && ptr[-1] == '\\')
                   2649:                            continue;
                   2650:                        Parse_Error(PARSE_WARNING,
1.374     rillig   2651:                                    "Zero byte read from file, "
                   2652:                                    "skipping rest of line.");
1.190     sjg      2653:                        break;
                   2654:                    }
                   2655:                }
1.170     dholland 2656:                if (cf->nextbuf != NULL) {
                   2657:                    /*
                   2658:                     * End of this buffer; return EOF and outer logic
                   2659:                     * will get the next one. (eww)
                   2660:                     */
1.127     dsl      2661:                    break;
                   2662:                }
1.170     dholland 2663:                Parse_Error(PARSE_FATAL, "Zero byte read from file");
                   2664:                return NULL;
1.127     dsl      2665:            }
                   2666:
                   2667:            if (ch == '\\') {
                   2668:                /* Don't treat next character as special, remember first one */
                   2669:                if (escaped == NULL)
                   2670:                    escaped = ptr;
                   2671:                if (ptr[1] == '\n')
                   2672:                    cf->lineno++;
                   2673:                ptr += 2;
                   2674:                line_end = ptr;
                   2675:                continue;
                   2676:            }
                   2677:            if (ch == '#' && comment == NULL) {
                   2678:                /* Remember first '#' for comment stripping */
1.181     sjg      2679:                /* Unless previous char was '[', as in modifier :[#] */
                   2680:                if (!(ptr > line && ptr[-1] == '['))
                   2681:                    comment = line_end;
1.127     dsl      2682:            }
                   2683:            ptr++;
1.125     dsl      2684:            if (ch == '\n')
1.127     dsl      2685:                break;
1.290     rillig   2686:            if (!ch_isspace(ch))
1.127     dsl      2687:                /* We are not interested in trailing whitespace */
                   2688:                line_end = ptr;
                   2689:        }
1.125     dsl      2690:
1.127     dsl      2691:        /* Save next 'to be processed' location */
1.403     rillig   2692:        cf->buf_ptr = ptr;
1.127     dsl      2693:
                   2694:        /* Check we have a non-comment, non-blank line */
                   2695:        if (line_end == line || comment == line) {
                   2696:            if (ch == 0)
                   2697:                /* At end of file */
                   2698:                return NULL;
                   2699:            /* Parse another line */
1.125     dsl      2700:            continue;
1.127     dsl      2701:        }
1.125     dsl      2702:
1.127     dsl      2703:        /* We now have a line of data */
                   2704:        *line_end = 0;
1.27      christos 2705:
1.127     dsl      2706:        if (flags & PARSE_RAW) {
                   2707:            /* Leave '\' (etc) in line buffer (eg 'for' lines) */
                   2708:            return line;
                   2709:        }
1.5       cgd      2710:
1.127     dsl      2711:        if (flags & PARSE_SKIP) {
                   2712:            /* Completely ignore non-directives */
                   2713:            if (line[0] != '.')
                   2714:                continue;
                   2715:            /* We could do more of the .else/.elif/.endif checks here */
                   2716:        }
                   2717:        break;
                   2718:     }
1.5       cgd      2719:
1.127     dsl      2720:     /* Brutally ignore anything after a non-escaped '#' in non-commands */
                   2721:     if (comment != NULL && line[0] != '\t') {
                   2722:        line_end = comment;
                   2723:        *line_end = 0;
                   2724:     }
1.5       cgd      2725:
1.127     dsl      2726:     /* If we didn't see a '\\' then the in-situ data is fine */
1.368     rillig   2727:     if (escaped == NULL)
1.127     dsl      2728:        return line;
1.5       cgd      2729:
1.127     dsl      2730:     /* Remove escapes from '\n' and '#' */
                   2731:     tp = ptr = escaped;
                   2732:     escaped = line;
                   2733:     for (; ; *tp++ = ch) {
                   2734:        ch = *ptr++;
                   2735:        if (ch != '\\') {
                   2736:            if (ch == 0)
                   2737:                break;
                   2738:            continue;
                   2739:        }
1.5       cgd      2740:
1.127     dsl      2741:        ch = *ptr++;
                   2742:        if (ch == 0) {
                   2743:            /* Delete '\\' at end of buffer */
                   2744:            tp--;
                   2745:            break;
                   2746:        }
1.27      christos 2747:
1.130     dsl      2748:        if (ch == '#' && line[0] != '\t')
                   2749:            /* Delete '\\' from before '#' on non-command lines */
1.127     dsl      2750:            continue;
1.27      christos 2751:
1.127     dsl      2752:        if (ch != '\n') {
                   2753:            /* Leave '\\' in buffer for later */
                   2754:            *tp++ = '\\';
                   2755:            /* Make sure we don't delete an escaped ' ' from the line end */
                   2756:            escaped = tp + 1;
                   2757:            continue;
                   2758:        }
1.27      christos 2759:
1.203     joerg    2760:        /* Escaped '\n' replace following whitespace with a single ' ' */
                   2761:        while (ptr[0] == ' ' || ptr[0] == '\t')
                   2762:            ptr++;
                   2763:        ch = ' ';
1.127     dsl      2764:     }
1.27      christos 2765:
1.127     dsl      2766:     /* Delete any trailing spaces - eg from empty continuations */
1.290     rillig   2767:     while (tp > escaped && ch_isspace(tp[-1]))
1.127     dsl      2768:        tp--;
1.27      christos 2769:
1.127     dsl      2770:     *tp = 0;
1.5       cgd      2771:     return line;
                   2772: }
1.1       cgd      2773:
1.284     rillig   2774: /* Read an entire line from the input file. Called only by Parse_File.
1.1       cgd      2775:  *
                   2776:  * Results:
1.284     rillig   2777:  *     A line without its newline.
1.1       cgd      2778:  *
                   2779:  * Side Effects:
                   2780:  *     Only those associated with reading a character
                   2781:  */
                   2782: static char *
1.84      wiz      2783: ParseReadLine(void)
1.1       cgd      2784: {
1.338     rillig   2785:     char *line;                        /* Result */
                   2786:     int lineno;                        /* Saved line # */
                   2787:     int rval;
1.1       cgd      2788:
1.5       cgd      2789:     for (;;) {
1.368     rillig   2790:        line = ParseGetLine(0);
1.127     dsl      2791:        if (line == NULL)
                   2792:            return NULL;
1.1       cgd      2793:
1.127     dsl      2794:        if (line[0] != '.')
                   2795:            return line;
1.121     dsl      2796:
                   2797:        /*
1.127     dsl      2798:         * The line might be a conditional. Ask the conditional module
                   2799:         * about it and act accordingly
1.121     dsl      2800:         */
1.302     rillig   2801:        switch (Cond_EvalLine(line)) {
1.127     dsl      2802:        case COND_SKIP:
                   2803:            /* Skip to next conditional that evaluates to COND_PARSE.  */
                   2804:            do {
1.368     rillig   2805:                line = ParseGetLine(PARSE_SKIP);
1.302     rillig   2806:            } while (line && Cond_EvalLine(line) != COND_PARSE);
1.127     dsl      2807:            if (line == NULL)
1.121     dsl      2808:                break;
1.127     dsl      2809:            continue;
                   2810:        case COND_PARSE:
                   2811:            continue;
                   2812:        case COND_INVALID:    /* Not a conditional line */
1.151     dsl      2813:            /* Check for .for loops */
                   2814:            rval = For_Eval(line);
                   2815:            if (rval == 0)
                   2816:                /* Not a .for line */
1.1       cgd      2817:                break;
1.151     dsl      2818:            if (rval < 0)
                   2819:                /* Syntax error - error printed, ignore line */
                   2820:                continue;
                   2821:            /* Start of a .for loop */
1.408     rillig   2822:            lineno = CurFile()->lineno;
1.151     dsl      2823:            /* Accumulate loop lines until matching .endfor */
1.121     dsl      2824:            do {
1.368     rillig   2825:                line = ParseGetLine(PARSE_RAW);
1.121     dsl      2826:                if (line == NULL) {
                   2827:                    Parse_Error(PARSE_FATAL,
1.374     rillig   2828:                                "Unexpected end of file in for loop.");
1.5       cgd      2829:                    break;
1.121     dsl      2830:                }
1.151     dsl      2831:            } while (For_Accum(line));
1.127     dsl      2832:            /* Stash each iteration as a new 'input file' */
                   2833:            For_Run(lineno);
                   2834:            /* Read next line from for-loop buffer */
                   2835:            continue;
1.1       cgd      2836:        }
1.235     rillig   2837:        return line;
1.1       cgd      2838:     }
                   2839: }
                   2840:
1.331     rillig   2841: static void
1.329     rillig   2842: FinishDependencyGroup(void)
1.1       cgd      2843: {
1.330     rillig   2844:     if (targets != NULL) {
1.371     rillig   2845:        GNodeListNode *ln;
                   2846:        for (ln = targets->first; ln != NULL; ln = ln->next) {
                   2847:            GNode *gn = ln->datum;
                   2848:
                   2849:            Suff_EndTransform(gn);
                   2850:
                   2851:            /* Mark the target as already having commands if it does, to
                   2852:             * keep from having shell commands on multiple dependency lines. */
                   2853:            if (!Lst_IsEmpty(gn->commands))
                   2854:                gn->type |= OP_HAS_COMMANDS;
                   2855:        }
                   2856:
                   2857:        Lst_Free(targets);
1.336     rillig   2858:        targets = NULL;
1.1       cgd      2859:     }
                   2860: }
1.27      christos 2861:
1.327     rillig   2862: /* Add the command to each target from the current dependency spec. */
1.326     rillig   2863: static void
1.375     rillig   2864: ParseLine_ShellCommand(const char *p)
1.326     rillig   2865: {
1.375     rillig   2866:     cpp_skip_whitespace(&p);
                   2867:     if (*p == '\0')
1.327     rillig   2868:        return;                 /* skip empty commands */
                   2869:
1.330     rillig   2870:     if (targets == NULL) {
1.375     rillig   2871:        Parse_Error(PARSE_FATAL, "Unassociated shell command \"%s\"", p);
1.327     rillig   2872:        return;
1.330     rillig   2873:     }
1.327     rillig   2874:
                   2875:     {
1.375     rillig   2876:        char *cmd = bmake_strdup(p);
1.327     rillig   2877:        GNodeListNode *ln;
                   2878:
                   2879:        for (ln = targets->first; ln != NULL; ln = ln->next) {
                   2880:            GNode *gn = ln->datum;
                   2881:            ParseAddCmd(gn, cmd);
                   2882:        }
1.326     rillig   2883: #ifdef CLEANUP
1.327     rillig   2884:        Lst_Append(targCmds, cmd);
1.326     rillig   2885: #endif
                   2886:     }
                   2887: }
1.1       cgd      2888:
1.379     rillig   2889: static Boolean
                   2890: ParseDirective(char *line)
                   2891: {
                   2892:     char *cp;
                   2893:
                   2894:     if (*line == '.') {
                   2895:        /*
                   2896:         * Lines that begin with the special character may be
                   2897:         * include or undef directives.
                   2898:         * On the other hand they can be suffix rules (.c.o: ...)
                   2899:         * or just dependencies for filenames that start '.'.
                   2900:         */
                   2901:        cp = line + 1;
                   2902:        pp_skip_whitespace(&cp);
                   2903:        if (IsInclude(cp, FALSE)) {
                   2904:            ParseDoInclude(cp);
                   2905:            return TRUE;
                   2906:        }
                   2907:        if (strncmp(cp, "undef", 5) == 0) {
                   2908:            const char *varname;
                   2909:            cp += 5;
                   2910:            pp_skip_whitespace(&cp);
                   2911:            varname = cp;
                   2912:            for (; !ch_isspace(*cp) && *cp != '\0'; cp++)
                   2913:                continue;
                   2914:            *cp = '\0';
                   2915:            Var_Delete(varname, VAR_GLOBAL);
                   2916:            /* TODO: undefine all variables, not only the first */
                   2917:            /* TODO: use Str_Words, like everywhere else */
                   2918:            return TRUE;
                   2919:        } else if (strncmp(cp, "export", 6) == 0) {
                   2920:            cp += 6;
                   2921:            pp_skip_whitespace(&cp);
                   2922:            Var_Export(cp, TRUE);
                   2923:            return TRUE;
                   2924:        } else if (strncmp(cp, "unexport", 8) == 0) {
                   2925:            Var_UnExport(cp);
                   2926:            return TRUE;
                   2927:        } else if (strncmp(cp, "info", 4) == 0 ||
                   2928:                   strncmp(cp, "error", 5) == 0 ||
                   2929:                   strncmp(cp, "warning", 7) == 0) {
                   2930:            if (ParseMessage(cp))
                   2931:                return TRUE;
                   2932:        }
                   2933:     }
                   2934:     return FALSE;
                   2935: }
                   2936:
                   2937: static Boolean
                   2938: ParseVarassign(const char *line)
                   2939: {
                   2940:     VarAssign var;
                   2941:     if (Parse_IsVar(line, &var)) {
                   2942:        FinishDependencyGroup();
                   2943:        Parse_DoVar(&var, VAR_GLOBAL);
                   2944:        return TRUE;
                   2945:     }
                   2946:     return FALSE;
                   2947: }
                   2948:
1.380     rillig   2949: static char *
                   2950: FindSemicolon(char *p)
                   2951: {
                   2952:     int level = 0;
                   2953:
                   2954:     for (; *p != '\0'; p++) {
                   2955:        if (*p == '\\' && p[1] != '\0') {
                   2956:            p++;
                   2957:            continue;
                   2958:        }
                   2959:
                   2960:        if (*p == '$' && (p[1] == '(' || p[1] == '{')) {
                   2961:            level++;
                   2962:            continue;
                   2963:        }
                   2964:
                   2965:        if (level > 0 && (*p == ')' || *p == '}')) {
                   2966:            level--;
                   2967:            continue;
                   2968:        }
                   2969:
                   2970:        if (level == 0 && *p == ';') {
                   2971:            break;
                   2972:        }
                   2973:     }
                   2974:     return p;
                   2975: }
                   2976:
1.379     rillig   2977: /* dependency  -> target... op [source...]
                   2978:  * op          -> ':' | '::' | '!' */
                   2979: static void
1.380     rillig   2980: ParseDependency(char *line)
1.379     rillig   2981: {
                   2982:     VarEvalFlags eflags;
                   2983:     char *expanded_line;
1.380     rillig   2984:     const char *shellcmd = NULL;
1.379     rillig   2985:
                   2986:     /*
                   2987:      * For some reason - probably to make the parser impossible -
                   2988:      * a ';' can be used to separate commands from dependencies.
                   2989:      * Attempt to avoid ';' inside substitution patterns.
                   2990:      */
                   2991:     {
1.412     rillig   2992:        char *semicolon = FindSemicolon(line);
1.380     rillig   2993:        if (*semicolon != '\0') {
                   2994:            /* Terminate the dependency list at the ';' */
                   2995:            *semicolon = '\0';
                   2996:            shellcmd = semicolon + 1;
1.379     rillig   2997:        }
                   2998:     }
                   2999:
                   3000:     /*
                   3001:      * We now know it's a dependency line so it needs to have all
                   3002:      * variables expanded before being parsed.
                   3003:      *
                   3004:      * XXX: Ideally the dependency line would first be split into
                   3005:      * its left-hand side, dependency operator and right-hand side,
                   3006:      * and then each side would be expanded on its own.  This would
                   3007:      * allow for the left-hand side to allow only defined variables
                   3008:      * and to allow variables on the right-hand side to be undefined
                   3009:      * as well.
                   3010:      *
                   3011:      * Parsing the line first would also prevent that targets
                   3012:      * generated from variable expressions are interpreted as the
                   3013:      * dependency operator, such as in "target${:U:} middle: source",
                   3014:      * in which the middle is interpreted as a source, not a target.
                   3015:      */
                   3016:
                   3017:     /* In lint mode, allow undefined variables to appear in
                   3018:      * dependency lines.
                   3019:      *
                   3020:      * Ideally, only the right-hand side would allow undefined
                   3021:      * variables since it is common to have no dependencies.
                   3022:      * Having undefined variables on the left-hand side is more
                   3023:      * unusual though.  Since both sides are expanded in a single
                   3024:      * pass, there is not much choice what to do here.
                   3025:      *
                   3026:      * In normal mode, it does not matter whether undefined
                   3027:      * variables are allowed or not since as of 2020-09-14,
                   3028:      * Var_Parse does not print any parse errors in such a case.
                   3029:      * It simply returns the special empty string var_Error,
                   3030:      * which cannot be detected in the result of Var_Subst. */
                   3031:     eflags = DEBUG(LINT) ? VARE_WANTRES : VARE_UNDEFERR | VARE_WANTRES;
1.411     rillig   3032:     (void)Var_Subst(line, VAR_CMDLINE, eflags, &expanded_line);
1.379     rillig   3033:     /* TODO: handle errors */
                   3034:
                   3035:     /* Need a fresh list for the target nodes */
                   3036:     if (targets != NULL)
                   3037:        Lst_Free(targets);
1.385     rillig   3038:     targets = Lst_New();
1.379     rillig   3039:
                   3040:     ParseDoDependency(expanded_line);
                   3041:     free(expanded_line);
1.380     rillig   3042:
                   3043:     if (shellcmd != NULL)
                   3044:        ParseLine_ShellCommand(shellcmd);
1.379     rillig   3045: }
                   3046:
1.381     rillig   3047: static void
                   3048: ParseLine(char *line)
                   3049: {
                   3050:     if (ParseDirective(line))
                   3051:        return;
                   3052:
                   3053:     if (*line == '\t') {
                   3054:        ParseLine_ShellCommand(line + 1);
                   3055:        return;
                   3056:     }
                   3057:
                   3058: #ifdef SYSVINCLUDE
                   3059:     if (IsSysVInclude(line)) {
                   3060:        /*
                   3061:         * It's an S3/S5-style "include".
                   3062:         */
                   3063:        ParseTraditionalInclude(line);
                   3064:        return;
                   3065:     }
                   3066: #endif
                   3067:
                   3068: #ifdef GMAKEEXPORT
                   3069:     if (strncmp(line, "export", 6) == 0 && ch_isspace(line[6]) &&
                   3070:        strchr(line, ':') == NULL) {
                   3071:        /*
                   3072:         * It's a Gmake "export".
                   3073:         */
                   3074:        ParseGmakeExport(line);
                   3075:        return;
                   3076:     }
                   3077: #endif
                   3078:
                   3079:     if (ParseVarassign(line))
                   3080:        return;
                   3081:
                   3082:     FinishDependencyGroup();
                   3083:
                   3084:     ParseDependency(line);
                   3085: }
                   3086:
1.297     rillig   3087: /* Parse a top-level makefile into its component parts, incorporating them
                   3088:  * into the global dependency graph.
1.1       cgd      3089:  *
1.84      wiz      3090:  * Input:
1.297     rillig   3091:  *     name            The name of the file being read
                   3092:  *     fd              The open file to parse; will be closed at the end
1.1       cgd      3093:  */
                   3094: void
1.125     dsl      3095: Parse_File(const char *name, int fd)
1.1       cgd      3096: {
1.374     rillig   3097:     char *line;                        /* the line we're working on */
1.170     dholland 3098:     struct loadedfile *lf;
                   3099:
                   3100:     lf = loadfile(name, fd);
1.1       cgd      3101:
1.330     rillig   3102:     assert(targets == NULL);
1.1       cgd      3103:     fatals = 0;
1.44      aidan    3104:
1.242     rillig   3105:     if (name == NULL)
                   3106:        name = "(stdin)";
1.170     dholland 3107:
                   3108:     Parse_SetInput(name, 0, -1, loadedfile_nextbuf, lf);
1.408     rillig   3109:     CurFile()->lf = lf;
1.1       cgd      3110:
                   3111:     do {
1.381     rillig   3112:        while ((line = ParseReadLine()) != NULL) {
1.408     rillig   3113:            DEBUG2(PARSE, "ParseReadLine (%d): '%s'\n",
                   3114:                   CurFile()->lineno, line);
1.381     rillig   3115:            ParseLine(line);
1.1       cgd      3116:        }
                   3117:        /*
1.27      christos 3118:         * Reached EOF, but it may be just EOF of an include file...
1.1       cgd      3119:         */
1.376     rillig   3120:     } while (ParseEOF());
1.1       cgd      3121:
1.329     rillig   3122:     FinishDependencyGroup();
1.328     rillig   3123:
1.1       cgd      3124:     if (fatals) {
1.163     sjg      3125:        (void)fflush(stdout);
1.63      christos 3126:        (void)fprintf(stderr,
1.374     rillig   3127:                      "%s: Fatal errors encountered -- cannot continue",
                   3128:                      progname);
1.161     sjg      3129:        PrintOnError(NULL, NULL);
1.103     christos 3130:        exit(1);
1.1       cgd      3131:     }
                   3132: }
                   3133:
1.383     rillig   3134: /* Initialize the parsing module. */
1.5       cgd      3135: void
1.84      wiz      3136: Parse_Init(void)
1.1       cgd      3137: {
1.152     dsl      3138:     mainNode = NULL;
1.385     rillig   3139:     parseIncPath = Lst_New();
                   3140:     sysIncPath = Lst_New();
1.409     rillig   3141:     defSysIncPath = Lst_New();
1.408     rillig   3142:     Vector_Init(&includes, sizeof(IFile));
1.45      mycroft  3143: #ifdef CLEANUP
1.385     rillig   3144:     targCmds = Lst_New();
1.45      mycroft  3145: #endif
1.1       cgd      3146: }
1.9       jtc      3147:
1.383     rillig   3148: /* Clean up the parsing module. */
1.9       jtc      3149: void
1.84      wiz      3150: Parse_End(void)
1.9       jtc      3151: {
1.45      mycroft  3152: #ifdef CLEANUP
1.268     rillig   3153:     Lst_Destroy(targCmds, free);
1.336     rillig   3154:     assert(targets == NULL);
1.409     rillig   3155:     Lst_Destroy(defSysIncPath, Dir_Destroy);
1.268     rillig   3156:     Lst_Destroy(sysIncPath, Dir_Destroy);
                   3157:     Lst_Destroy(parseIncPath, Dir_Destroy);
1.400     rillig   3158:     assert(includes.len == 0);
                   3159:     Vector_Done(&includes);
1.45      mycroft  3160: #endif
1.9       jtc      3161: }
1.27      christos 3162:
1.1       cgd      3163:
                   3164: /*-
                   3165:  *-----------------------------------------------------------------------
                   3166:  * Parse_MainName --
                   3167:  *     Return a Lst of the main target to create for main()'s sake. If
                   3168:  *     no such target exists, we Punt with an obnoxious error message.
                   3169:  *
                   3170:  * Results:
                   3171:  *     A Lst of the single node to create.
                   3172:  *
                   3173:  * Side Effects:
                   3174:  *     None.
                   3175:  *
                   3176:  *-----------------------------------------------------------------------
                   3177:  */
1.322     rillig   3178: GNodeList *
1.84      wiz      3179: Parse_MainName(void)
1.1       cgd      3180: {
1.322     rillig   3181:     GNodeList *mainList;
1.1       cgd      3182:
1.385     rillig   3183:     mainList = Lst_New();
1.1       cgd      3184:
1.152     dsl      3185:     if (mainNode == NULL) {
1.97      christos 3186:        Punt("no target to make.");
1.246     rillig   3187:        /*NOTREACHED*/
1.1       cgd      3188:     } else if (mainNode->type & OP_DOUBLEDEP) {
1.268     rillig   3189:        Lst_Append(mainList, mainNode);
                   3190:        Lst_AppendAll(mainList, mainNode->cohorts);
1.374     rillig   3191:     } else
1.268     rillig   3192:        Lst_Append(mainList, mainNode);
1.81      pk       3193:     Var_Append(".TARGETS", mainNode->name, VAR_GLOBAL);
1.235     rillig   3194:     return mainList;
1.59      christos 3195: }

CVSweb <webmaster@jp.NetBSD.org>