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