xref: /minix3/usr.bin/make/cond.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: cond.c,v 1.68 2015/05/05 21:51:09 sjg Exp $	*/
22e2caf59SThomas Veerman 
32e2caf59SThomas Veerman /*
42e2caf59SThomas Veerman  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
52e2caf59SThomas Veerman  * All rights reserved.
62e2caf59SThomas Veerman  *
72e2caf59SThomas Veerman  * This code is derived from software contributed to Berkeley by
82e2caf59SThomas Veerman  * Adam de Boor.
92e2caf59SThomas Veerman  *
102e2caf59SThomas Veerman  * Redistribution and use in source and binary forms, with or without
112e2caf59SThomas Veerman  * modification, are permitted provided that the following conditions
122e2caf59SThomas Veerman  * are met:
132e2caf59SThomas Veerman  * 1. Redistributions of source code must retain the above copyright
142e2caf59SThomas Veerman  *    notice, this list of conditions and the following disclaimer.
152e2caf59SThomas Veerman  * 2. Redistributions in binary form must reproduce the above copyright
162e2caf59SThomas Veerman  *    notice, this list of conditions and the following disclaimer in the
172e2caf59SThomas Veerman  *    documentation and/or other materials provided with the distribution.
182e2caf59SThomas Veerman  * 3. Neither the name of the University nor the names of its contributors
192e2caf59SThomas Veerman  *    may be used to endorse or promote products derived from this software
202e2caf59SThomas Veerman  *    without specific prior written permission.
212e2caf59SThomas Veerman  *
222e2caf59SThomas Veerman  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
232e2caf59SThomas Veerman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242e2caf59SThomas Veerman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
252e2caf59SThomas Veerman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
262e2caf59SThomas Veerman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
272e2caf59SThomas Veerman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
282e2caf59SThomas Veerman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
292e2caf59SThomas Veerman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
302e2caf59SThomas Veerman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312e2caf59SThomas Veerman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322e2caf59SThomas Veerman  * SUCH DAMAGE.
332e2caf59SThomas Veerman  */
342e2caf59SThomas Veerman 
352e2caf59SThomas Veerman /*
362e2caf59SThomas Veerman  * Copyright (c) 1988, 1989 by Adam de Boor
372e2caf59SThomas Veerman  * Copyright (c) 1989 by Berkeley Softworks
382e2caf59SThomas Veerman  * All rights reserved.
392e2caf59SThomas Veerman  *
402e2caf59SThomas Veerman  * This code is derived from software contributed to Berkeley by
412e2caf59SThomas Veerman  * Adam de Boor.
422e2caf59SThomas Veerman  *
432e2caf59SThomas Veerman  * Redistribution and use in source and binary forms, with or without
442e2caf59SThomas Veerman  * modification, are permitted provided that the following conditions
452e2caf59SThomas Veerman  * are met:
462e2caf59SThomas Veerman  * 1. Redistributions of source code must retain the above copyright
472e2caf59SThomas Veerman  *    notice, this list of conditions and the following disclaimer.
482e2caf59SThomas Veerman  * 2. Redistributions in binary form must reproduce the above copyright
492e2caf59SThomas Veerman  *    notice, this list of conditions and the following disclaimer in the
502e2caf59SThomas Veerman  *    documentation and/or other materials provided with the distribution.
512e2caf59SThomas Veerman  * 3. All advertising materials mentioning features or use of this software
522e2caf59SThomas Veerman  *    must display the following acknowledgement:
532e2caf59SThomas Veerman  *	This product includes software developed by the University of
542e2caf59SThomas Veerman  *	California, Berkeley and its contributors.
552e2caf59SThomas Veerman  * 4. Neither the name of the University nor the names of its contributors
562e2caf59SThomas Veerman  *    may be used to endorse or promote products derived from this software
572e2caf59SThomas Veerman  *    without specific prior written permission.
582e2caf59SThomas Veerman  *
592e2caf59SThomas Veerman  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
602e2caf59SThomas Veerman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
612e2caf59SThomas Veerman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
622e2caf59SThomas Veerman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
632e2caf59SThomas Veerman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
642e2caf59SThomas Veerman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
652e2caf59SThomas Veerman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
662e2caf59SThomas Veerman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
672e2caf59SThomas Veerman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
682e2caf59SThomas Veerman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
692e2caf59SThomas Veerman  * SUCH DAMAGE.
702e2caf59SThomas Veerman  */
712e2caf59SThomas Veerman 
722e2caf59SThomas Veerman #ifndef MAKE_NATIVE
73*0a6a1f1dSLionel Sambuc static char rcsid[] = "$NetBSD: cond.c,v 1.68 2015/05/05 21:51:09 sjg Exp $";
742e2caf59SThomas Veerman #else
752e2caf59SThomas Veerman #include <sys/cdefs.h>
762e2caf59SThomas Veerman #ifndef lint
772e2caf59SThomas Veerman #if 0
782e2caf59SThomas Veerman static char sccsid[] = "@(#)cond.c	8.2 (Berkeley) 1/2/94";
792e2caf59SThomas Veerman #else
80*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: cond.c,v 1.68 2015/05/05 21:51:09 sjg Exp $");
812e2caf59SThomas Veerman #endif
822e2caf59SThomas Veerman #endif /* not lint */
832e2caf59SThomas Veerman #endif
842e2caf59SThomas Veerman 
852e2caf59SThomas Veerman /*-
862e2caf59SThomas Veerman  * cond.c --
872e2caf59SThomas Veerman  *	Functions to handle conditionals in a makefile.
882e2caf59SThomas Veerman  *
892e2caf59SThomas Veerman  * Interface:
902e2caf59SThomas Veerman  *	Cond_Eval 	Evaluate the conditional in the passed line.
912e2caf59SThomas Veerman  *
922e2caf59SThomas Veerman  */
932e2caf59SThomas Veerman 
942e2caf59SThomas Veerman #include    <ctype.h>
952e2caf59SThomas Veerman #include    <errno.h>    /* For strtoul() error checking */
962e2caf59SThomas Veerman 
972e2caf59SThomas Veerman #include    "make.h"
982e2caf59SThomas Veerman #include    "hash.h"
992e2caf59SThomas Veerman #include    "dir.h"
1002e2caf59SThomas Veerman #include    "buf.h"
1012e2caf59SThomas Veerman 
1022e2caf59SThomas Veerman /*
1032e2caf59SThomas Veerman  * The parsing of conditional expressions is based on this grammar:
1042e2caf59SThomas Veerman  *	E -> F || E
1052e2caf59SThomas Veerman  *	E -> F
1062e2caf59SThomas Veerman  *	F -> T && F
1072e2caf59SThomas Veerman  *	F -> T
1082e2caf59SThomas Veerman  *	T -> defined(variable)
1092e2caf59SThomas Veerman  *	T -> make(target)
1102e2caf59SThomas Veerman  *	T -> exists(file)
1112e2caf59SThomas Veerman  *	T -> empty(varspec)
1122e2caf59SThomas Veerman  *	T -> target(name)
1132e2caf59SThomas Veerman  *	T -> commands(name)
1142e2caf59SThomas Veerman  *	T -> symbol
1152e2caf59SThomas Veerman  *	T -> $(varspec) op value
1162e2caf59SThomas Veerman  *	T -> $(varspec) == "string"
1172e2caf59SThomas Veerman  *	T -> $(varspec) != "string"
1182e2caf59SThomas Veerman  *	T -> "string"
1192e2caf59SThomas Veerman  *	T -> ( E )
1202e2caf59SThomas Veerman  *	T -> ! T
1212e2caf59SThomas Veerman  *	op -> == | != | > | < | >= | <=
1222e2caf59SThomas Veerman  *
1232e2caf59SThomas Veerman  * 'symbol' is some other symbol to which the default function (condDefProc)
1242e2caf59SThomas Veerman  * is applied.
1252e2caf59SThomas Veerman  *
1262e2caf59SThomas Veerman  * Tokens are scanned from the 'condExpr' string. The scanner (CondToken)
1272e2caf59SThomas Veerman  * will return TOK_AND for '&' and '&&', TOK_OR for '|' and '||',
1282e2caf59SThomas Veerman  * TOK_NOT for '!', TOK_LPAREN for '(', TOK_RPAREN for ')' and will evaluate
1292e2caf59SThomas Veerman  * the other terminal symbols, using either the default function or the
1302e2caf59SThomas Veerman  * function given in the terminal, and return the result as either TOK_TRUE
1312e2caf59SThomas Veerman  * or TOK_FALSE.
1322e2caf59SThomas Veerman  *
1332e2caf59SThomas Veerman  * TOK_FALSE is 0 and TOK_TRUE 1 so we can directly assign C comparisons.
1342e2caf59SThomas Veerman  *
1352e2caf59SThomas Veerman  * All Non-Terminal functions (CondE, CondF and CondT) return TOK_ERROR on
1362e2caf59SThomas Veerman  * error.
1372e2caf59SThomas Veerman  */
1382e2caf59SThomas Veerman typedef enum {
1392e2caf59SThomas Veerman     TOK_FALSE = 0, TOK_TRUE = 1, TOK_AND, TOK_OR, TOK_NOT,
1402e2caf59SThomas Veerman     TOK_LPAREN, TOK_RPAREN, TOK_EOF, TOK_NONE, TOK_ERROR
1412e2caf59SThomas Veerman } Token;
1422e2caf59SThomas Veerman 
1432e2caf59SThomas Veerman /*-
1442e2caf59SThomas Veerman  * Structures to handle elegantly the different forms of #if's. The
1452e2caf59SThomas Veerman  * last two fields are stored in condInvert and condDefProc, respectively.
1462e2caf59SThomas Veerman  */
1472e2caf59SThomas Veerman static void CondPushBack(Token);
1482e2caf59SThomas Veerman static int CondGetArg(char **, char **, const char *);
1492e2caf59SThomas Veerman static Boolean CondDoDefined(int, const char *);
1502e2caf59SThomas Veerman static int CondStrMatch(const void *, const void *);
1512e2caf59SThomas Veerman static Boolean CondDoMake(int, const char *);
1522e2caf59SThomas Veerman static Boolean CondDoExists(int, const char *);
1532e2caf59SThomas Veerman static Boolean CondDoTarget(int, const char *);
1542e2caf59SThomas Veerman static Boolean CondDoCommands(int, const char *);
1552e2caf59SThomas Veerman static Boolean CondCvtArg(char *, double *);
1562e2caf59SThomas Veerman static Token CondToken(Boolean);
1572e2caf59SThomas Veerman static Token CondT(Boolean);
1582e2caf59SThomas Veerman static Token CondF(Boolean);
1592e2caf59SThomas Veerman static Token CondE(Boolean);
1602e2caf59SThomas Veerman static int do_Cond_EvalExpression(Boolean *);
1612e2caf59SThomas Veerman 
1622e2caf59SThomas Veerman static const struct If {
1632e2caf59SThomas Veerman     const char	*form;	      /* Form of if */
1642e2caf59SThomas Veerman     int		formlen;      /* Length of form */
1652e2caf59SThomas Veerman     Boolean	doNot;	      /* TRUE if default function should be negated */
1662e2caf59SThomas Veerman     Boolean	(*defProc)(int, const char *); /* Default function to apply */
1672e2caf59SThomas Veerman } ifs[] = {
1682e2caf59SThomas Veerman     { "def",	  3,	  FALSE,  CondDoDefined },
1692e2caf59SThomas Veerman     { "ndef",	  4,	  TRUE,	  CondDoDefined },
1702e2caf59SThomas Veerman     { "make",	  4,	  FALSE,  CondDoMake },
1712e2caf59SThomas Veerman     { "nmake",	  5,	  TRUE,	  CondDoMake },
1722e2caf59SThomas Veerman     { "",	  0,	  FALSE,  CondDoDefined },
1732e2caf59SThomas Veerman     { NULL,	  0,	  FALSE,  NULL }
1742e2caf59SThomas Veerman };
1752e2caf59SThomas Veerman 
1762e2caf59SThomas Veerman static const struct If *if_info;        /* Info for current statement */
1772e2caf59SThomas Veerman static char 	  *condExpr;	    	/* The expression to parse */
1782e2caf59SThomas Veerman static Token	  condPushBack=TOK_NONE;	/* Single push-back token used in
1792e2caf59SThomas Veerman 					 * parsing */
1802e2caf59SThomas Veerman 
1812e2caf59SThomas Veerman static unsigned int	cond_depth = 0;  	/* current .if nesting level */
1822e2caf59SThomas Veerman static unsigned int	cond_min_depth = 0;  	/* depth at makefile open */
1832e2caf59SThomas Veerman 
184*0a6a1f1dSLionel Sambuc /*
185*0a6a1f1dSLionel Sambuc  * Indicate when we should be strict about lhs of comparisons.
186*0a6a1f1dSLionel Sambuc  * TRUE when Cond_EvalExpression is called from Cond_Eval (.if etc)
187*0a6a1f1dSLionel Sambuc  * FALSE when Cond_EvalExpression is called from var.c:ApplyModifiers
188*0a6a1f1dSLionel Sambuc  * since lhs is already expanded and we cannot tell if
189*0a6a1f1dSLionel Sambuc  * it was a variable reference or not.
190*0a6a1f1dSLionel Sambuc  */
191*0a6a1f1dSLionel Sambuc static Boolean lhsStrict;
192*0a6a1f1dSLionel Sambuc 
1932e2caf59SThomas Veerman static int
istoken(const char * str,const char * tok,size_t len)1942e2caf59SThomas Veerman istoken(const char *str, const char *tok, size_t len)
1952e2caf59SThomas Veerman {
1962e2caf59SThomas Veerman 	return strncmp(str, tok, len) == 0 && !isalpha((unsigned char)str[len]);
1972e2caf59SThomas Veerman }
1982e2caf59SThomas Veerman 
1992e2caf59SThomas Veerman /*-
2002e2caf59SThomas Veerman  *-----------------------------------------------------------------------
2012e2caf59SThomas Veerman  * CondPushBack --
2022e2caf59SThomas Veerman  *	Push back the most recent token read. We only need one level of
2032e2caf59SThomas Veerman  *	this, so the thing is just stored in 'condPushback'.
2042e2caf59SThomas Veerman  *
2052e2caf59SThomas Veerman  * Input:
2062e2caf59SThomas Veerman  *	t		Token to push back into the "stream"
2072e2caf59SThomas Veerman  *
2082e2caf59SThomas Veerman  * Results:
2092e2caf59SThomas Veerman  *	None.
2102e2caf59SThomas Veerman  *
2112e2caf59SThomas Veerman  * Side Effects:
2122e2caf59SThomas Veerman  *	condPushback is overwritten.
2132e2caf59SThomas Veerman  *
2142e2caf59SThomas Veerman  *-----------------------------------------------------------------------
2152e2caf59SThomas Veerman  */
2162e2caf59SThomas Veerman static void
CondPushBack(Token t)2172e2caf59SThomas Veerman CondPushBack(Token t)
2182e2caf59SThomas Veerman {
2192e2caf59SThomas Veerman     condPushBack = t;
2202e2caf59SThomas Veerman }
2212e2caf59SThomas Veerman 
2222e2caf59SThomas Veerman /*-
2232e2caf59SThomas Veerman  *-----------------------------------------------------------------------
2242e2caf59SThomas Veerman  * CondGetArg --
2252e2caf59SThomas Veerman  *	Find the argument of a built-in function.
2262e2caf59SThomas Veerman  *
2272e2caf59SThomas Veerman  * Input:
2282e2caf59SThomas Veerman  *	parens		TRUE if arg should be bounded by parens
2292e2caf59SThomas Veerman  *
2302e2caf59SThomas Veerman  * Results:
2312e2caf59SThomas Veerman  *	The length of the argument and the address of the argument.
2322e2caf59SThomas Veerman  *
2332e2caf59SThomas Veerman  * Side Effects:
2342e2caf59SThomas Veerman  *	The pointer is set to point to the closing parenthesis of the
2352e2caf59SThomas Veerman  *	function call.
2362e2caf59SThomas Veerman  *
2372e2caf59SThomas Veerman  *-----------------------------------------------------------------------
2382e2caf59SThomas Veerman  */
2392e2caf59SThomas Veerman static int
CondGetArg(char ** linePtr,char ** argPtr,const char * func)2402e2caf59SThomas Veerman CondGetArg(char **linePtr, char **argPtr, const char *func)
2412e2caf59SThomas Veerman {
2422e2caf59SThomas Veerman     char	  *cp;
2432e2caf59SThomas Veerman     int	    	  argLen;
2442e2caf59SThomas Veerman     Buffer	  buf;
2452e2caf59SThomas Veerman     int           paren_depth;
2462e2caf59SThomas Veerman     char          ch;
2472e2caf59SThomas Veerman 
2482e2caf59SThomas Veerman     cp = *linePtr;
2492e2caf59SThomas Veerman     if (func != NULL)
2502e2caf59SThomas Veerman 	/* Skip opening '(' - verfied by caller */
2512e2caf59SThomas Veerman 	cp++;
2522e2caf59SThomas Veerman 
2532e2caf59SThomas Veerman     if (*cp == '\0') {
2542e2caf59SThomas Veerman 	/*
2552e2caf59SThomas Veerman 	 * No arguments whatsoever. Because 'make' and 'defined' aren't really
2562e2caf59SThomas Veerman 	 * "reserved words", we don't print a message. I think this is better
2572e2caf59SThomas Veerman 	 * than hitting the user with a warning message every time s/he uses
2582e2caf59SThomas Veerman 	 * the word 'make' or 'defined' at the beginning of a symbol...
2592e2caf59SThomas Veerman 	 */
2602e2caf59SThomas Veerman 	*argPtr = NULL;
2612e2caf59SThomas Veerman 	return (0);
2622e2caf59SThomas Veerman     }
2632e2caf59SThomas Veerman 
2642e2caf59SThomas Veerman     while (*cp == ' ' || *cp == '\t') {
2652e2caf59SThomas Veerman 	cp++;
2662e2caf59SThomas Veerman     }
2672e2caf59SThomas Veerman 
2682e2caf59SThomas Veerman     /*
2692e2caf59SThomas Veerman      * Create a buffer for the argument and start it out at 16 characters
2702e2caf59SThomas Veerman      * long. Why 16? Why not?
2712e2caf59SThomas Veerman      */
2722e2caf59SThomas Veerman     Buf_Init(&buf, 16);
2732e2caf59SThomas Veerman 
2742e2caf59SThomas Veerman     paren_depth = 0;
2752e2caf59SThomas Veerman     for (;;) {
2762e2caf59SThomas Veerman 	ch = *cp;
2772e2caf59SThomas Veerman 	if (ch == 0 || ch == ' ' || ch == '\t')
2782e2caf59SThomas Veerman 	    break;
2792e2caf59SThomas Veerman 	if ((ch == '&' || ch == '|') && paren_depth == 0)
2802e2caf59SThomas Veerman 	    break;
2812e2caf59SThomas Veerman 	if (*cp == '$') {
2822e2caf59SThomas Veerman 	    /*
2832e2caf59SThomas Veerman 	     * Parse the variable spec and install it as part of the argument
2842e2caf59SThomas Veerman 	     * if it's valid. We tell Var_Parse to complain on an undefined
2852e2caf59SThomas Veerman 	     * variable, so we don't do it too. Nor do we return an error,
2862e2caf59SThomas Veerman 	     * though perhaps we should...
2872e2caf59SThomas Veerman 	     */
2882e2caf59SThomas Veerman 	    char  	*cp2;
2892e2caf59SThomas Veerman 	    int		len;
2902e2caf59SThomas Veerman 	    void	*freeIt;
2912e2caf59SThomas Veerman 
2922e2caf59SThomas Veerman 	    cp2 = Var_Parse(cp, VAR_CMD, TRUE, &len, &freeIt);
2932e2caf59SThomas Veerman 	    Buf_AddBytes(&buf, strlen(cp2), cp2);
2942e2caf59SThomas Veerman 	    if (freeIt)
2952e2caf59SThomas Veerman 		free(freeIt);
2962e2caf59SThomas Veerman 	    cp += len;
2972e2caf59SThomas Veerman 	    continue;
2982e2caf59SThomas Veerman 	}
2992e2caf59SThomas Veerman 	if (ch == '(')
3002e2caf59SThomas Veerman 	    paren_depth++;
3012e2caf59SThomas Veerman 	else
3022e2caf59SThomas Veerman 	    if (ch == ')' && --paren_depth < 0)
3032e2caf59SThomas Veerman 		break;
3042e2caf59SThomas Veerman 	Buf_AddByte(&buf, *cp);
3052e2caf59SThomas Veerman 	cp++;
3062e2caf59SThomas Veerman     }
3072e2caf59SThomas Veerman 
3082e2caf59SThomas Veerman     *argPtr = Buf_GetAll(&buf, &argLen);
3092e2caf59SThomas Veerman     Buf_Destroy(&buf, FALSE);
3102e2caf59SThomas Veerman 
3112e2caf59SThomas Veerman     while (*cp == ' ' || *cp == '\t') {
3122e2caf59SThomas Veerman 	cp++;
3132e2caf59SThomas Veerman     }
3142e2caf59SThomas Veerman 
3152e2caf59SThomas Veerman     if (func != NULL && *cp++ != ')') {
3162e2caf59SThomas Veerman 	Parse_Error(PARSE_WARNING, "Missing closing parenthesis for %s()",
3172e2caf59SThomas Veerman 		     func);
3182e2caf59SThomas Veerman 	return (0);
3192e2caf59SThomas Veerman     }
3202e2caf59SThomas Veerman 
3212e2caf59SThomas Veerman     *linePtr = cp;
3222e2caf59SThomas Veerman     return (argLen);
3232e2caf59SThomas Veerman }
3242e2caf59SThomas Veerman 
3252e2caf59SThomas Veerman /*-
3262e2caf59SThomas Veerman  *-----------------------------------------------------------------------
3272e2caf59SThomas Veerman  * CondDoDefined --
3282e2caf59SThomas Veerman  *	Handle the 'defined' function for conditionals.
3292e2caf59SThomas Veerman  *
3302e2caf59SThomas Veerman  * Results:
3312e2caf59SThomas Veerman  *	TRUE if the given variable is defined.
3322e2caf59SThomas Veerman  *
3332e2caf59SThomas Veerman  * Side Effects:
3342e2caf59SThomas Veerman  *	None.
3352e2caf59SThomas Veerman  *
3362e2caf59SThomas Veerman  *-----------------------------------------------------------------------
3372e2caf59SThomas Veerman  */
3382e2caf59SThomas Veerman static Boolean
CondDoDefined(int argLen MAKE_ATTR_UNUSED,const char * arg)3392bc7c627SLionel Sambuc CondDoDefined(int argLen MAKE_ATTR_UNUSED, const char *arg)
3402e2caf59SThomas Veerman {
3412e2caf59SThomas Veerman     char    *p1;
3422e2caf59SThomas Veerman     Boolean result;
3432e2caf59SThomas Veerman 
3442e2caf59SThomas Veerman     if (Var_Value(arg, VAR_CMD, &p1) != NULL) {
3452e2caf59SThomas Veerman 	result = TRUE;
3462e2caf59SThomas Veerman     } else {
3472e2caf59SThomas Veerman 	result = FALSE;
3482e2caf59SThomas Veerman     }
3492e2caf59SThomas Veerman     if (p1)
3502e2caf59SThomas Veerman 	free(p1);
3512e2caf59SThomas Veerman     return (result);
3522e2caf59SThomas Veerman }
3532e2caf59SThomas Veerman 
3542e2caf59SThomas Veerman /*-
3552e2caf59SThomas Veerman  *-----------------------------------------------------------------------
3562e2caf59SThomas Veerman  * CondStrMatch --
3572e2caf59SThomas Veerman  *	Front-end for Str_Match so it returns 0 on match and non-zero
3582e2caf59SThomas Veerman  *	on mismatch. Callback function for CondDoMake via Lst_Find
3592e2caf59SThomas Veerman  *
3602e2caf59SThomas Veerman  * Results:
3612e2caf59SThomas Veerman  *	0 if string matches pattern
3622e2caf59SThomas Veerman  *
3632e2caf59SThomas Veerman  * Side Effects:
3642e2caf59SThomas Veerman  *	None
3652e2caf59SThomas Veerman  *
3662e2caf59SThomas Veerman  *-----------------------------------------------------------------------
3672e2caf59SThomas Veerman  */
3682e2caf59SThomas Veerman static int
CondStrMatch(const void * string,const void * pattern)3692e2caf59SThomas Veerman CondStrMatch(const void *string, const void *pattern)
3702e2caf59SThomas Veerman {
3712e2caf59SThomas Veerman     return(!Str_Match(string, pattern));
3722e2caf59SThomas Veerman }
3732e2caf59SThomas Veerman 
3742e2caf59SThomas Veerman /*-
3752e2caf59SThomas Veerman  *-----------------------------------------------------------------------
3762e2caf59SThomas Veerman  * CondDoMake --
3772e2caf59SThomas Veerman  *	Handle the 'make' function for conditionals.
3782e2caf59SThomas Veerman  *
3792e2caf59SThomas Veerman  * Results:
3802e2caf59SThomas Veerman  *	TRUE if the given target is being made.
3812e2caf59SThomas Veerman  *
3822e2caf59SThomas Veerman  * Side Effects:
3832e2caf59SThomas Veerman  *	None.
3842e2caf59SThomas Veerman  *
3852e2caf59SThomas Veerman  *-----------------------------------------------------------------------
3862e2caf59SThomas Veerman  */
3872e2caf59SThomas Veerman static Boolean
CondDoMake(int argLen MAKE_ATTR_UNUSED,const char * arg)3882bc7c627SLionel Sambuc CondDoMake(int argLen MAKE_ATTR_UNUSED, const char *arg)
3892e2caf59SThomas Veerman {
3902e2caf59SThomas Veerman     return Lst_Find(create, arg, CondStrMatch) != NULL;
3912e2caf59SThomas Veerman }
3922e2caf59SThomas Veerman 
3932e2caf59SThomas Veerman /*-
3942e2caf59SThomas Veerman  *-----------------------------------------------------------------------
3952e2caf59SThomas Veerman  * CondDoExists --
3962e2caf59SThomas Veerman  *	See if the given file exists.
3972e2caf59SThomas Veerman  *
3982e2caf59SThomas Veerman  * Results:
3992e2caf59SThomas Veerman  *	TRUE if the file exists and FALSE if it does not.
4002e2caf59SThomas Veerman  *
4012e2caf59SThomas Veerman  * Side Effects:
4022e2caf59SThomas Veerman  *	None.
4032e2caf59SThomas Veerman  *
4042e2caf59SThomas Veerman  *-----------------------------------------------------------------------
4052e2caf59SThomas Veerman  */
4062e2caf59SThomas Veerman static Boolean
CondDoExists(int argLen MAKE_ATTR_UNUSED,const char * arg)4072bc7c627SLionel Sambuc CondDoExists(int argLen MAKE_ATTR_UNUSED, const char *arg)
4082e2caf59SThomas Veerman {
4092e2caf59SThomas Veerman     Boolean result;
4102e2caf59SThomas Veerman     char    *path;
4112e2caf59SThomas Veerman 
4122e2caf59SThomas Veerman     path = Dir_FindFile(arg, dirSearchPath);
4132e2caf59SThomas Veerman     if (DEBUG(COND)) {
4142e2caf59SThomas Veerman 	fprintf(debug_file, "exists(%s) result is \"%s\"\n",
4152e2caf59SThomas Veerman 	       arg, path ? path : "");
4162e2caf59SThomas Veerman     }
4172e2caf59SThomas Veerman     if (path != NULL) {
4182e2caf59SThomas Veerman 	result = TRUE;
4192e2caf59SThomas Veerman 	free(path);
4202e2caf59SThomas Veerman     } else {
4212e2caf59SThomas Veerman 	result = FALSE;
4222e2caf59SThomas Veerman     }
4232e2caf59SThomas Veerman     return (result);
4242e2caf59SThomas Veerman }
4252e2caf59SThomas Veerman 
4262e2caf59SThomas Veerman /*-
4272e2caf59SThomas Veerman  *-----------------------------------------------------------------------
4282e2caf59SThomas Veerman  * CondDoTarget --
4292e2caf59SThomas Veerman  *	See if the given node exists and is an actual target.
4302e2caf59SThomas Veerman  *
4312e2caf59SThomas Veerman  * Results:
4322e2caf59SThomas Veerman  *	TRUE if the node exists as a target and FALSE if it does not.
4332e2caf59SThomas Veerman  *
4342e2caf59SThomas Veerman  * Side Effects:
4352e2caf59SThomas Veerman  *	None.
4362e2caf59SThomas Veerman  *
4372e2caf59SThomas Veerman  *-----------------------------------------------------------------------
4382e2caf59SThomas Veerman  */
4392e2caf59SThomas Veerman static Boolean
CondDoTarget(int argLen MAKE_ATTR_UNUSED,const char * arg)4402bc7c627SLionel Sambuc CondDoTarget(int argLen MAKE_ATTR_UNUSED, const char *arg)
4412e2caf59SThomas Veerman {
4422e2caf59SThomas Veerman     GNode   *gn;
4432e2caf59SThomas Veerman 
4442e2caf59SThomas Veerman     gn = Targ_FindNode(arg, TARG_NOCREATE);
4452e2caf59SThomas Veerman     return (gn != NULL) && !OP_NOP(gn->type);
4462e2caf59SThomas Veerman }
4472e2caf59SThomas Veerman 
4482e2caf59SThomas Veerman /*-
4492e2caf59SThomas Veerman  *-----------------------------------------------------------------------
4502e2caf59SThomas Veerman  * CondDoCommands --
4512e2caf59SThomas Veerman  *	See if the given node exists and is an actual target with commands
4522e2caf59SThomas Veerman  *	associated with it.
4532e2caf59SThomas Veerman  *
4542e2caf59SThomas Veerman  * Results:
4552e2caf59SThomas Veerman  *	TRUE if the node exists as a target and has commands associated with
4562e2caf59SThomas Veerman  *	it and FALSE if it does not.
4572e2caf59SThomas Veerman  *
4582e2caf59SThomas Veerman  * Side Effects:
4592e2caf59SThomas Veerman  *	None.
4602e2caf59SThomas Veerman  *
4612e2caf59SThomas Veerman  *-----------------------------------------------------------------------
4622e2caf59SThomas Veerman  */
4632e2caf59SThomas Veerman static Boolean
CondDoCommands(int argLen MAKE_ATTR_UNUSED,const char * arg)4642bc7c627SLionel Sambuc CondDoCommands(int argLen MAKE_ATTR_UNUSED, const char *arg)
4652e2caf59SThomas Veerman {
4662e2caf59SThomas Veerman     GNode   *gn;
4672e2caf59SThomas Veerman 
4682e2caf59SThomas Veerman     gn = Targ_FindNode(arg, TARG_NOCREATE);
4692e2caf59SThomas Veerman     return (gn != NULL) && !OP_NOP(gn->type) && !Lst_IsEmpty(gn->commands);
4702e2caf59SThomas Veerman }
4712e2caf59SThomas Veerman 
4722e2caf59SThomas Veerman /*-
4732e2caf59SThomas Veerman  *-----------------------------------------------------------------------
4742e2caf59SThomas Veerman  * CondCvtArg --
4752e2caf59SThomas Veerman  *	Convert the given number into a double.
4762e2caf59SThomas Veerman  *	We try a base 10 or 16 integer conversion first, if that fails
4772e2caf59SThomas Veerman  *	then we try a floating point conversion instead.
4782e2caf59SThomas Veerman  *
4792e2caf59SThomas Veerman  * Results:
4802e2caf59SThomas Veerman  *	Sets 'value' to double value of string.
4812e2caf59SThomas Veerman  *	Returns 'true' if the convertion suceeded
4822e2caf59SThomas Veerman  *
4832e2caf59SThomas Veerman  *-----------------------------------------------------------------------
4842e2caf59SThomas Veerman  */
4852e2caf59SThomas Veerman static Boolean
CondCvtArg(char * str,double * value)4862e2caf59SThomas Veerman CondCvtArg(char *str, double *value)
4872e2caf59SThomas Veerman {
4882e2caf59SThomas Veerman     char *eptr, ech;
4892e2caf59SThomas Veerman     unsigned long l_val;
4902e2caf59SThomas Veerman     double d_val;
4912e2caf59SThomas Veerman 
4922e2caf59SThomas Veerman     errno = 0;
4932e2caf59SThomas Veerman     l_val = strtoul(str, &eptr, str[1] == 'x' ? 16 : 10);
4942e2caf59SThomas Veerman     ech = *eptr;
4952e2caf59SThomas Veerman     if (ech == 0 && errno != ERANGE) {
4962e2caf59SThomas Veerman 	d_val = str[0] == '-' ? -(double)-l_val : (double)l_val;
4972e2caf59SThomas Veerman     } else {
4982e2caf59SThomas Veerman 	if (ech != 0 && ech != '.' && ech != 'e' && ech != 'E')
4992e2caf59SThomas Veerman 	    return FALSE;
5002e2caf59SThomas Veerman 	d_val = strtod(str, &eptr);
5012e2caf59SThomas Veerman 	if (*eptr)
5022e2caf59SThomas Veerman 	    return FALSE;
5032e2caf59SThomas Veerman     }
5042e2caf59SThomas Veerman 
5052e2caf59SThomas Veerman     *value = d_val;
5062e2caf59SThomas Veerman     return TRUE;
5072e2caf59SThomas Veerman }
5082e2caf59SThomas Veerman 
5092e2caf59SThomas Veerman /*-
5102e2caf59SThomas Veerman  *-----------------------------------------------------------------------
5112e2caf59SThomas Veerman  * CondGetString --
5122e2caf59SThomas Veerman  *	Get a string from a variable reference or an optionally quoted
5132e2caf59SThomas Veerman  *	string.  This is called for the lhs and rhs of string compares.
5142e2caf59SThomas Veerman  *
5152e2caf59SThomas Veerman  * Results:
5162e2caf59SThomas Veerman  *	Sets freeIt if needed,
5172e2caf59SThomas Veerman  *	Sets quoted if string was quoted,
5182e2caf59SThomas Veerman  *	Returns NULL on error,
5192e2caf59SThomas Veerman  *	else returns string - absent any quotes.
5202e2caf59SThomas Veerman  *
5212e2caf59SThomas Veerman  * Side Effects:
5222e2caf59SThomas Veerman  *	Moves condExpr to end of this token.
5232e2caf59SThomas Veerman  *
5242e2caf59SThomas Veerman  *
5252e2caf59SThomas Veerman  *-----------------------------------------------------------------------
5262e2caf59SThomas Veerman  */
5272e2caf59SThomas Veerman /* coverity:[+alloc : arg-*2] */
5282e2caf59SThomas Veerman static char *
CondGetString(Boolean doEval,Boolean * quoted,void ** freeIt,Boolean strictLHS)529*0a6a1f1dSLionel Sambuc CondGetString(Boolean doEval, Boolean *quoted, void **freeIt, Boolean strictLHS)
5302e2caf59SThomas Veerman {
5312e2caf59SThomas Veerman     Buffer buf;
5322e2caf59SThomas Veerman     char *cp;
5332e2caf59SThomas Veerman     char *str;
5342e2caf59SThomas Veerman     int	len;
5352e2caf59SThomas Veerman     int qt;
5362e2caf59SThomas Veerman     char *start;
5372e2caf59SThomas Veerman 
5382e2caf59SThomas Veerman     Buf_Init(&buf, 0);
5392e2caf59SThomas Veerman     str = NULL;
5402e2caf59SThomas Veerman     *freeIt = NULL;
5412e2caf59SThomas Veerman     *quoted = qt = *condExpr == '"' ? 1 : 0;
5422e2caf59SThomas Veerman     if (qt)
5432e2caf59SThomas Veerman 	condExpr++;
5442e2caf59SThomas Veerman     for (start = condExpr; *condExpr && str == NULL; condExpr++) {
5452e2caf59SThomas Veerman 	switch (*condExpr) {
5462e2caf59SThomas Veerman 	case '\\':
5472e2caf59SThomas Veerman 	    if (condExpr[1] != '\0') {
5482e2caf59SThomas Veerman 		condExpr++;
5492e2caf59SThomas Veerman 		Buf_AddByte(&buf, *condExpr);
5502e2caf59SThomas Veerman 	    }
5512e2caf59SThomas Veerman 	    break;
5522e2caf59SThomas Veerman 	case '"':
5532e2caf59SThomas Veerman 	    if (qt) {
5542e2caf59SThomas Veerman 		condExpr++;		/* we don't want the quotes */
5552e2caf59SThomas Veerman 		goto got_str;
5562e2caf59SThomas Veerman 	    } else
5572e2caf59SThomas Veerman 		Buf_AddByte(&buf, *condExpr); /* likely? */
5582e2caf59SThomas Veerman 	    break;
5592e2caf59SThomas Veerman 	case ')':
5602e2caf59SThomas Veerman 	case '!':
5612e2caf59SThomas Veerman 	case '=':
5622e2caf59SThomas Veerman 	case '>':
5632e2caf59SThomas Veerman 	case '<':
5642e2caf59SThomas Veerman 	case ' ':
5652e2caf59SThomas Veerman 	case '\t':
5662e2caf59SThomas Veerman 	    if (!qt)
5672e2caf59SThomas Veerman 		goto got_str;
5682e2caf59SThomas Veerman 	    else
5692e2caf59SThomas Veerman 		Buf_AddByte(&buf, *condExpr);
5702e2caf59SThomas Veerman 	    break;
5712e2caf59SThomas Veerman 	case '$':
5722e2caf59SThomas Veerman 	    /* if we are in quotes, then an undefined variable is ok */
5732e2caf59SThomas Veerman 	    str = Var_Parse(condExpr, VAR_CMD, (qt ? 0 : doEval),
5742e2caf59SThomas Veerman 			    &len, freeIt);
5752e2caf59SThomas Veerman 	    if (str == var_Error) {
5762e2caf59SThomas Veerman 		if (*freeIt) {
5772e2caf59SThomas Veerman 		    free(*freeIt);
5782e2caf59SThomas Veerman 		    *freeIt = NULL;
5792e2caf59SThomas Veerman 		}
5802e2caf59SThomas Veerman 		/*
5812e2caf59SThomas Veerman 		 * Even if !doEval, we still report syntax errors, which
5822e2caf59SThomas Veerman 		 * is what getting var_Error back with !doEval means.
5832e2caf59SThomas Veerman 		 */
5842e2caf59SThomas Veerman 		str = NULL;
5852e2caf59SThomas Veerman 		goto cleanup;
5862e2caf59SThomas Veerman 	    }
5872e2caf59SThomas Veerman 	    condExpr += len;
5882e2caf59SThomas Veerman 	    /*
5892e2caf59SThomas Veerman 	     * If the '$' was first char (no quotes), and we are
5902e2caf59SThomas Veerman 	     * followed by space, the operator or end of expression,
5912e2caf59SThomas Veerman 	     * we are done.
5922e2caf59SThomas Veerman 	     */
5932e2caf59SThomas Veerman 	    if ((condExpr == start + len) &&
5942e2caf59SThomas Veerman 		(*condExpr == '\0' ||
5952e2caf59SThomas Veerman 		 isspace((unsigned char) *condExpr) ||
5962e2caf59SThomas Veerman 		 strchr("!=><)", *condExpr))) {
5972e2caf59SThomas Veerman 		goto cleanup;
5982e2caf59SThomas Veerman 	    }
5992e2caf59SThomas Veerman 	    /*
6002e2caf59SThomas Veerman 	     * Nope, we better copy str to buf
6012e2caf59SThomas Veerman 	     */
6022e2caf59SThomas Veerman 	    for (cp = str; *cp; cp++) {
6032e2caf59SThomas Veerman 		Buf_AddByte(&buf, *cp);
6042e2caf59SThomas Veerman 	    }
6052e2caf59SThomas Veerman 	    if (*freeIt) {
6062e2caf59SThomas Veerman 		free(*freeIt);
6072e2caf59SThomas Veerman 		*freeIt = NULL;
6082e2caf59SThomas Veerman 	    }
6092e2caf59SThomas Veerman 	    str = NULL;			/* not finished yet */
6102e2caf59SThomas Veerman 	    condExpr--;			/* don't skip over next char */
6112e2caf59SThomas Veerman 	    break;
6122e2caf59SThomas Veerman 	default:
613*0a6a1f1dSLionel Sambuc 	    if (strictLHS && !qt && *start != '$' &&
614*0a6a1f1dSLionel Sambuc 		!isdigit((unsigned char) *start)) {
615*0a6a1f1dSLionel Sambuc 		/* lhs must be quoted, a variable reference or number */
616*0a6a1f1dSLionel Sambuc 		if (*freeIt) {
617*0a6a1f1dSLionel Sambuc 		    free(*freeIt);
618*0a6a1f1dSLionel Sambuc 		    *freeIt = NULL;
619*0a6a1f1dSLionel Sambuc 		}
620*0a6a1f1dSLionel Sambuc 		str = NULL;
621*0a6a1f1dSLionel Sambuc 		goto cleanup;
622*0a6a1f1dSLionel Sambuc 	    }
6232e2caf59SThomas Veerman 	    Buf_AddByte(&buf, *condExpr);
6242e2caf59SThomas Veerman 	    break;
6252e2caf59SThomas Veerman 	}
6262e2caf59SThomas Veerman     }
6272e2caf59SThomas Veerman  got_str:
6282e2caf59SThomas Veerman     str = Buf_GetAll(&buf, NULL);
6292e2caf59SThomas Veerman     *freeIt = str;
6302e2caf59SThomas Veerman  cleanup:
6312e2caf59SThomas Veerman     Buf_Destroy(&buf, FALSE);
6322e2caf59SThomas Veerman     return str;
6332e2caf59SThomas Veerman }
6342e2caf59SThomas Veerman 
6352e2caf59SThomas Veerman /*-
6362e2caf59SThomas Veerman  *-----------------------------------------------------------------------
6372e2caf59SThomas Veerman  * CondToken --
6382e2caf59SThomas Veerman  *	Return the next token from the input.
6392e2caf59SThomas Veerman  *
6402e2caf59SThomas Veerman  * Results:
6412e2caf59SThomas Veerman  *	A Token for the next lexical token in the stream.
6422e2caf59SThomas Veerman  *
6432e2caf59SThomas Veerman  * Side Effects:
6442e2caf59SThomas Veerman  *	condPushback will be set back to TOK_NONE if it is used.
6452e2caf59SThomas Veerman  *
6462e2caf59SThomas Veerman  *-----------------------------------------------------------------------
6472e2caf59SThomas Veerman  */
6482e2caf59SThomas Veerman static Token
compare_expression(Boolean doEval)6492e2caf59SThomas Veerman compare_expression(Boolean doEval)
6502e2caf59SThomas Veerman {
6512e2caf59SThomas Veerman     Token	t;
6522e2caf59SThomas Veerman     char	*lhs;
6532e2caf59SThomas Veerman     char	*rhs;
6542e2caf59SThomas Veerman     char	*op;
6552e2caf59SThomas Veerman     void	*lhsFree;
6562e2caf59SThomas Veerman     void	*rhsFree;
6572e2caf59SThomas Veerman     Boolean lhsQuoted;
6582e2caf59SThomas Veerman     Boolean rhsQuoted;
6592e2caf59SThomas Veerman     double  	left, right;
6602e2caf59SThomas Veerman 
6612e2caf59SThomas Veerman     t = TOK_ERROR;
6622e2caf59SThomas Veerman     rhs = NULL;
6632e2caf59SThomas Veerman     lhsFree = rhsFree = FALSE;
6642e2caf59SThomas Veerman     lhsQuoted = rhsQuoted = FALSE;
6652e2caf59SThomas Veerman 
6662e2caf59SThomas Veerman     /*
6672e2caf59SThomas Veerman      * Parse the variable spec and skip over it, saving its
6682e2caf59SThomas Veerman      * value in lhs.
6692e2caf59SThomas Veerman      */
670*0a6a1f1dSLionel Sambuc     lhs = CondGetString(doEval, &lhsQuoted, &lhsFree, lhsStrict);
6712e2caf59SThomas Veerman     if (!lhs)
6722e2caf59SThomas Veerman 	goto done;
6732e2caf59SThomas Veerman 
6742e2caf59SThomas Veerman     /*
6752e2caf59SThomas Veerman      * Skip whitespace to get to the operator
6762e2caf59SThomas Veerman      */
6772e2caf59SThomas Veerman     while (isspace((unsigned char) *condExpr))
6782e2caf59SThomas Veerman 	condExpr++;
6792e2caf59SThomas Veerman 
6802e2caf59SThomas Veerman     /*
6812e2caf59SThomas Veerman      * Make sure the operator is a valid one. If it isn't a
6822e2caf59SThomas Veerman      * known relational operator, pretend we got a
6832e2caf59SThomas Veerman      * != 0 comparison.
6842e2caf59SThomas Veerman      */
6852e2caf59SThomas Veerman     op = condExpr;
6862e2caf59SThomas Veerman     switch (*condExpr) {
6872e2caf59SThomas Veerman 	case '!':
6882e2caf59SThomas Veerman 	case '=':
6892e2caf59SThomas Veerman 	case '<':
6902e2caf59SThomas Veerman 	case '>':
6912e2caf59SThomas Veerman 	    if (condExpr[1] == '=') {
6922e2caf59SThomas Veerman 		condExpr += 2;
6932e2caf59SThomas Veerman 	    } else {
6942e2caf59SThomas Veerman 		condExpr += 1;
6952e2caf59SThomas Veerman 	    }
6962e2caf59SThomas Veerman 	    break;
6972e2caf59SThomas Veerman 	default:
6982e2caf59SThomas Veerman 	    if (!doEval) {
6992e2caf59SThomas Veerman 		t = TOK_FALSE;
7002e2caf59SThomas Veerman 		goto done;
7012e2caf59SThomas Veerman 	    }
7022e2caf59SThomas Veerman 	    /* For .ifxxx "..." check for non-empty string. */
7032e2caf59SThomas Veerman 	    if (lhsQuoted) {
7042e2caf59SThomas Veerman 		t = lhs[0] != 0;
7052e2caf59SThomas Veerman 		goto done;
7062e2caf59SThomas Veerman 	    }
7072e2caf59SThomas Veerman 	    /* For .ifxxx <number> compare against zero */
7082e2caf59SThomas Veerman 	    if (CondCvtArg(lhs, &left)) {
7092e2caf59SThomas Veerman 		t = left != 0.0;
7102e2caf59SThomas Veerman 		goto done;
7112e2caf59SThomas Veerman 	    }
7122e2caf59SThomas Veerman 	    /* For .if ${...} check for non-empty string (defProc is ifdef). */
7132e2caf59SThomas Veerman 	    if (if_info->form[0] == 0) {
7142e2caf59SThomas Veerman 		t = lhs[0] != 0;
7152e2caf59SThomas Veerman 		goto done;
7162e2caf59SThomas Veerman 	    }
7172e2caf59SThomas Veerman 	    /* Otherwise action default test ... */
7182e2caf59SThomas Veerman 	    t = if_info->defProc(strlen(lhs), lhs) != if_info->doNot;
7192e2caf59SThomas Veerman 	    goto done;
7202e2caf59SThomas Veerman     }
7212e2caf59SThomas Veerman 
7222e2caf59SThomas Veerman     while (isspace((unsigned char)*condExpr))
7232e2caf59SThomas Veerman 	condExpr++;
7242e2caf59SThomas Veerman 
7252e2caf59SThomas Veerman     if (*condExpr == '\0') {
7262e2caf59SThomas Veerman 	Parse_Error(PARSE_WARNING,
7272e2caf59SThomas Veerman 		    "Missing right-hand-side of operator");
7282e2caf59SThomas Veerman 	goto done;
7292e2caf59SThomas Veerman     }
7302e2caf59SThomas Veerman 
731*0a6a1f1dSLionel Sambuc     rhs = CondGetString(doEval, &rhsQuoted, &rhsFree, FALSE);
7322e2caf59SThomas Veerman     if (!rhs)
7332e2caf59SThomas Veerman 	goto done;
7342e2caf59SThomas Veerman 
7352e2caf59SThomas Veerman     if (rhsQuoted || lhsQuoted) {
7362e2caf59SThomas Veerman do_string_compare:
7372e2caf59SThomas Veerman 	if (((*op != '!') && (*op != '=')) || (op[1] != '=')) {
7382e2caf59SThomas Veerman 	    Parse_Error(PARSE_WARNING,
7392e2caf59SThomas Veerman     "String comparison operator should be either == or !=");
7402e2caf59SThomas Veerman 	    goto done;
7412e2caf59SThomas Veerman 	}
7422e2caf59SThomas Veerman 
7432e2caf59SThomas Veerman 	if (DEBUG(COND)) {
7442e2caf59SThomas Veerman 	    fprintf(debug_file, "lhs = \"%s\", rhs = \"%s\", op = %.2s\n",
7452e2caf59SThomas Veerman 		   lhs, rhs, op);
7462e2caf59SThomas Veerman 	}
7472e2caf59SThomas Veerman 	/*
7482e2caf59SThomas Veerman 	 * Null-terminate rhs and perform the comparison.
7492e2caf59SThomas Veerman 	 * t is set to the result.
7502e2caf59SThomas Veerman 	 */
7512e2caf59SThomas Veerman 	if (*op == '=') {
7522e2caf59SThomas Veerman 	    t = strcmp(lhs, rhs) == 0;
7532e2caf59SThomas Veerman 	} else {
7542e2caf59SThomas Veerman 	    t = strcmp(lhs, rhs) != 0;
7552e2caf59SThomas Veerman 	}
7562e2caf59SThomas Veerman     } else {
7572e2caf59SThomas Veerman 	/*
7582e2caf59SThomas Veerman 	 * rhs is either a float or an integer. Convert both the
7592e2caf59SThomas Veerman 	 * lhs and the rhs to a double and compare the two.
7602e2caf59SThomas Veerman 	 */
7612e2caf59SThomas Veerman 
7622e2caf59SThomas Veerman 	if (!CondCvtArg(lhs, &left) || !CondCvtArg(rhs, &right))
7632e2caf59SThomas Veerman 	    goto do_string_compare;
7642e2caf59SThomas Veerman 
7652e2caf59SThomas Veerman 	if (DEBUG(COND)) {
7662e2caf59SThomas Veerman 	    fprintf(debug_file, "left = %f, right = %f, op = %.2s\n", left,
7672e2caf59SThomas Veerman 		   right, op);
7682e2caf59SThomas Veerman 	}
7692e2caf59SThomas Veerman 	switch(op[0]) {
7702e2caf59SThomas Veerman 	case '!':
7712e2caf59SThomas Veerman 	    if (op[1] != '=') {
7722e2caf59SThomas Veerman 		Parse_Error(PARSE_WARNING,
7732e2caf59SThomas Veerman 			    "Unknown operator");
7742e2caf59SThomas Veerman 		goto done;
7752e2caf59SThomas Veerman 	    }
7762e2caf59SThomas Veerman 	    t = (left != right);
7772e2caf59SThomas Veerman 	    break;
7782e2caf59SThomas Veerman 	case '=':
7792e2caf59SThomas Veerman 	    if (op[1] != '=') {
7802e2caf59SThomas Veerman 		Parse_Error(PARSE_WARNING,
7812e2caf59SThomas Veerman 			    "Unknown operator");
7822e2caf59SThomas Veerman 		goto done;
7832e2caf59SThomas Veerman 	    }
7842e2caf59SThomas Veerman 	    t = (left == right);
7852e2caf59SThomas Veerman 	    break;
7862e2caf59SThomas Veerman 	case '<':
7872e2caf59SThomas Veerman 	    if (op[1] == '=') {
7882e2caf59SThomas Veerman 		t = (left <= right);
7892e2caf59SThomas Veerman 	    } else {
7902e2caf59SThomas Veerman 		t = (left < right);
7912e2caf59SThomas Veerman 	    }
7922e2caf59SThomas Veerman 	    break;
7932e2caf59SThomas Veerman 	case '>':
7942e2caf59SThomas Veerman 	    if (op[1] == '=') {
7952e2caf59SThomas Veerman 		t = (left >= right);
7962e2caf59SThomas Veerman 	    } else {
7972e2caf59SThomas Veerman 		t = (left > right);
7982e2caf59SThomas Veerman 	    }
7992e2caf59SThomas Veerman 	    break;
8002e2caf59SThomas Veerman 	}
8012e2caf59SThomas Veerman     }
8022e2caf59SThomas Veerman 
8032e2caf59SThomas Veerman done:
8042e2caf59SThomas Veerman     if (lhsFree)
8052e2caf59SThomas Veerman 	free(lhsFree);
8062e2caf59SThomas Veerman     if (rhsFree)
8072e2caf59SThomas Veerman 	free(rhsFree);
8082e2caf59SThomas Veerman     return t;
8092e2caf59SThomas Veerman }
8102e2caf59SThomas Veerman 
8112e2caf59SThomas Veerman static int
get_mpt_arg(char ** linePtr,char ** argPtr,const char * func MAKE_ATTR_UNUSED)8122bc7c627SLionel Sambuc get_mpt_arg(char **linePtr, char **argPtr, const char *func MAKE_ATTR_UNUSED)
8132e2caf59SThomas Veerman {
8142e2caf59SThomas Veerman     /*
8152e2caf59SThomas Veerman      * Use Var_Parse to parse the spec in parens and return
8162e2caf59SThomas Veerman      * TOK_TRUE if the resulting string is empty.
8172e2caf59SThomas Veerman      */
8182e2caf59SThomas Veerman     int	    length;
8192e2caf59SThomas Veerman     void    *freeIt;
8202e2caf59SThomas Veerman     char    *val;
8212e2caf59SThomas Veerman     char    *cp = *linePtr;
8222e2caf59SThomas Veerman 
8232e2caf59SThomas Veerman     /* We do all the work here and return the result as the length */
8242e2caf59SThomas Veerman     *argPtr = NULL;
8252e2caf59SThomas Veerman 
8262e2caf59SThomas Veerman     val = Var_Parse(cp - 1, VAR_CMD, FALSE, &length, &freeIt);
8272e2caf59SThomas Veerman     /*
8282e2caf59SThomas Veerman      * Advance *linePtr to beyond the closing ). Note that
8292e2caf59SThomas Veerman      * we subtract one because 'length' is calculated from 'cp - 1'.
8302e2caf59SThomas Veerman      */
8312e2caf59SThomas Veerman     *linePtr = cp - 1 + length;
8322e2caf59SThomas Veerman 
8332e2caf59SThomas Veerman     if (val == var_Error) {
8342e2caf59SThomas Veerman 	free(freeIt);
8352e2caf59SThomas Veerman 	return -1;
8362e2caf59SThomas Veerman     }
8372e2caf59SThomas Veerman 
8382e2caf59SThomas Veerman     /* A variable is empty when it just contains spaces... 4/15/92, christos */
8392e2caf59SThomas Veerman     while (isspace(*(unsigned char *)val))
8402e2caf59SThomas Veerman 	val++;
8412e2caf59SThomas Veerman 
8422e2caf59SThomas Veerman     /*
8432e2caf59SThomas Veerman      * For consistency with the other functions we can't generate the
8442e2caf59SThomas Veerman      * true/false here.
8452e2caf59SThomas Veerman      */
8462e2caf59SThomas Veerman     length = *val ? 2 : 1;
8472e2caf59SThomas Veerman     if (freeIt)
8482e2caf59SThomas Veerman 	free(freeIt);
8492e2caf59SThomas Veerman     return length;
8502e2caf59SThomas Veerman }
8512e2caf59SThomas Veerman 
8522e2caf59SThomas Veerman static Boolean
CondDoEmpty(int arglen,const char * arg MAKE_ATTR_UNUSED)8532bc7c627SLionel Sambuc CondDoEmpty(int arglen, const char *arg MAKE_ATTR_UNUSED)
8542e2caf59SThomas Veerman {
8552e2caf59SThomas Veerman     return arglen == 1;
8562e2caf59SThomas Veerman }
8572e2caf59SThomas Veerman 
8582e2caf59SThomas Veerman static Token
compare_function(Boolean doEval)8592e2caf59SThomas Veerman compare_function(Boolean doEval)
8602e2caf59SThomas Veerman {
8612e2caf59SThomas Veerman     static const struct fn_def {
8622e2caf59SThomas Veerman 	const char  *fn_name;
8632e2caf59SThomas Veerman 	int         fn_name_len;
8642e2caf59SThomas Veerman         int         (*fn_getarg)(char **, char **, const char *);
8652e2caf59SThomas Veerman 	Boolean     (*fn_proc)(int, const char *);
8662e2caf59SThomas Veerman     } fn_defs[] = {
8672e2caf59SThomas Veerman 	{ "defined",   7, CondGetArg, CondDoDefined },
8682e2caf59SThomas Veerman 	{ "make",      4, CondGetArg, CondDoMake },
8692e2caf59SThomas Veerman 	{ "exists",    6, CondGetArg, CondDoExists },
8702e2caf59SThomas Veerman 	{ "empty",     5, get_mpt_arg, CondDoEmpty },
8712e2caf59SThomas Veerman 	{ "target",    6, CondGetArg, CondDoTarget },
8722e2caf59SThomas Veerman 	{ "commands",  8, CondGetArg, CondDoCommands },
8732e2caf59SThomas Veerman 	{ NULL,        0, NULL, NULL },
8742e2caf59SThomas Veerman     };
8752e2caf59SThomas Veerman     const struct fn_def *fn_def;
8762e2caf59SThomas Veerman     Token	t;
8772e2caf59SThomas Veerman     char	*arg = NULL;
8782e2caf59SThomas Veerman     int	arglen;
8792e2caf59SThomas Veerman     char *cp = condExpr;
8802e2caf59SThomas Veerman     char *cp1;
8812e2caf59SThomas Veerman 
8822e2caf59SThomas Veerman     for (fn_def = fn_defs; fn_def->fn_name != NULL; fn_def++) {
8832e2caf59SThomas Veerman 	if (!istoken(cp, fn_def->fn_name, fn_def->fn_name_len))
8842e2caf59SThomas Veerman 	    continue;
8852e2caf59SThomas Veerman 	cp += fn_def->fn_name_len;
8862e2caf59SThomas Veerman 	/* There can only be whitespace before the '(' */
8872e2caf59SThomas Veerman 	while (isspace(*(unsigned char *)cp))
8882e2caf59SThomas Veerman 	    cp++;
8892e2caf59SThomas Veerman 	if (*cp != '(')
8902e2caf59SThomas Veerman 	    break;
8912e2caf59SThomas Veerman 
8922e2caf59SThomas Veerman 	arglen = fn_def->fn_getarg(&cp, &arg, fn_def->fn_name);
8932e2caf59SThomas Veerman 	if (arglen <= 0) {
8942e2caf59SThomas Veerman 	    condExpr = cp;
8952e2caf59SThomas Veerman 	    return arglen < 0 ? TOK_ERROR : TOK_FALSE;
8962e2caf59SThomas Veerman 	}
8972e2caf59SThomas Veerman 	/* Evaluate the argument using the required function. */
8982e2caf59SThomas Veerman 	t = !doEval || fn_def->fn_proc(arglen, arg);
8992e2caf59SThomas Veerman 	if (arg)
9002e2caf59SThomas Veerman 	    free(arg);
9012e2caf59SThomas Veerman 	condExpr = cp;
9022e2caf59SThomas Veerman 	return t;
9032e2caf59SThomas Veerman     }
9042e2caf59SThomas Veerman 
9052e2caf59SThomas Veerman     /* Push anything numeric through the compare expression */
9062e2caf59SThomas Veerman     cp = condExpr;
9072e2caf59SThomas Veerman     if (isdigit((unsigned char)cp[0]) || strchr("+-", cp[0]))
9082e2caf59SThomas Veerman 	return compare_expression(doEval);
9092e2caf59SThomas Veerman 
9102e2caf59SThomas Veerman     /*
9112e2caf59SThomas Veerman      * Most likely we have a naked token to apply the default function to.
9122e2caf59SThomas Veerman      * However ".if a == b" gets here when the "a" is unquoted and doesn't
9132e2caf59SThomas Veerman      * start with a '$'. This surprises people.
9142e2caf59SThomas Veerman      * If what follows the function argument is a '=' or '!' then the syntax
9152e2caf59SThomas Veerman      * would be invalid if we did "defined(a)" - so instead treat as an
9162e2caf59SThomas Veerman      * expression.
9172e2caf59SThomas Veerman      */
9182e2caf59SThomas Veerman     arglen = CondGetArg(&cp, &arg, NULL);
9192e2caf59SThomas Veerman     for (cp1 = cp; isspace(*(unsigned char *)cp1); cp1++)
9202e2caf59SThomas Veerman 	continue;
9212e2caf59SThomas Veerman     if (*cp1 == '=' || *cp1 == '!')
9222e2caf59SThomas Veerman 	return compare_expression(doEval);
9232e2caf59SThomas Veerman     condExpr = cp;
9242e2caf59SThomas Veerman 
9252e2caf59SThomas Veerman     /*
9262e2caf59SThomas Veerman      * Evaluate the argument using the default function.
9272e2caf59SThomas Veerman      * This path always treats .if as .ifdef. To get here the character
9282e2caf59SThomas Veerman      * after .if must have been taken literally, so the argument cannot
9292e2caf59SThomas Veerman      * be empty - even if it contained a variable expansion.
9302e2caf59SThomas Veerman      */
9312e2caf59SThomas Veerman     t = !doEval || if_info->defProc(arglen, arg) != if_info->doNot;
9322e2caf59SThomas Veerman     if (arg)
9332e2caf59SThomas Veerman 	free(arg);
9342e2caf59SThomas Veerman     return t;
9352e2caf59SThomas Veerman }
9362e2caf59SThomas Veerman 
9372e2caf59SThomas Veerman static Token
CondToken(Boolean doEval)9382e2caf59SThomas Veerman CondToken(Boolean doEval)
9392e2caf59SThomas Veerman {
9402e2caf59SThomas Veerman     Token t;
9412e2caf59SThomas Veerman 
9422e2caf59SThomas Veerman     t = condPushBack;
9432e2caf59SThomas Veerman     if (t != TOK_NONE) {
9442e2caf59SThomas Veerman 	condPushBack = TOK_NONE;
9452e2caf59SThomas Veerman 	return t;
9462e2caf59SThomas Veerman     }
9472e2caf59SThomas Veerman 
9482e2caf59SThomas Veerman     while (*condExpr == ' ' || *condExpr == '\t') {
9492e2caf59SThomas Veerman 	condExpr++;
9502e2caf59SThomas Veerman     }
9512e2caf59SThomas Veerman 
9522e2caf59SThomas Veerman     switch (*condExpr) {
9532e2caf59SThomas Veerman 
9542e2caf59SThomas Veerman     case '(':
9552e2caf59SThomas Veerman 	condExpr++;
9562e2caf59SThomas Veerman 	return TOK_LPAREN;
9572e2caf59SThomas Veerman 
9582e2caf59SThomas Veerman     case ')':
9592e2caf59SThomas Veerman 	condExpr++;
9602e2caf59SThomas Veerman 	return TOK_RPAREN;
9612e2caf59SThomas Veerman 
9622e2caf59SThomas Veerman     case '|':
9632e2caf59SThomas Veerman 	if (condExpr[1] == '|') {
9642e2caf59SThomas Veerman 	    condExpr++;
9652e2caf59SThomas Veerman 	}
9662e2caf59SThomas Veerman 	condExpr++;
9672e2caf59SThomas Veerman 	return TOK_OR;
9682e2caf59SThomas Veerman 
9692e2caf59SThomas Veerman     case '&':
9702e2caf59SThomas Veerman 	if (condExpr[1] == '&') {
9712e2caf59SThomas Veerman 	    condExpr++;
9722e2caf59SThomas Veerman 	}
9732e2caf59SThomas Veerman 	condExpr++;
9742e2caf59SThomas Veerman 	return TOK_AND;
9752e2caf59SThomas Veerman 
9762e2caf59SThomas Veerman     case '!':
9772e2caf59SThomas Veerman 	condExpr++;
9782e2caf59SThomas Veerman 	return TOK_NOT;
9792e2caf59SThomas Veerman 
9802e2caf59SThomas Veerman     case '#':
9812e2caf59SThomas Veerman     case '\n':
9822e2caf59SThomas Veerman     case '\0':
9832e2caf59SThomas Veerman 	return TOK_EOF;
9842e2caf59SThomas Veerman 
9852e2caf59SThomas Veerman     case '"':
9862e2caf59SThomas Veerman     case '$':
9872e2caf59SThomas Veerman 	return compare_expression(doEval);
9882e2caf59SThomas Veerman 
9892e2caf59SThomas Veerman     default:
9902e2caf59SThomas Veerman 	return compare_function(doEval);
9912e2caf59SThomas Veerman     }
9922e2caf59SThomas Veerman }
9932e2caf59SThomas Veerman 
9942e2caf59SThomas Veerman /*-
9952e2caf59SThomas Veerman  *-----------------------------------------------------------------------
9962e2caf59SThomas Veerman  * CondT --
9972e2caf59SThomas Veerman  *	Parse a single term in the expression. This consists of a terminal
9982e2caf59SThomas Veerman  *	symbol or TOK_NOT and a terminal symbol (not including the binary
9992e2caf59SThomas Veerman  *	operators):
10002e2caf59SThomas Veerman  *	    T -> defined(variable) | make(target) | exists(file) | symbol
10012e2caf59SThomas Veerman  *	    T -> ! T | ( E )
10022e2caf59SThomas Veerman  *
10032e2caf59SThomas Veerman  * Results:
10042e2caf59SThomas Veerman  *	TOK_TRUE, TOK_FALSE or TOK_ERROR.
10052e2caf59SThomas Veerman  *
10062e2caf59SThomas Veerman  * Side Effects:
10072e2caf59SThomas Veerman  *	Tokens are consumed.
10082e2caf59SThomas Veerman  *
10092e2caf59SThomas Veerman  *-----------------------------------------------------------------------
10102e2caf59SThomas Veerman  */
10112e2caf59SThomas Veerman static Token
CondT(Boolean doEval)10122e2caf59SThomas Veerman CondT(Boolean doEval)
10132e2caf59SThomas Veerman {
10142e2caf59SThomas Veerman     Token   t;
10152e2caf59SThomas Veerman 
10162e2caf59SThomas Veerman     t = CondToken(doEval);
10172e2caf59SThomas Veerman 
10182e2caf59SThomas Veerman     if (t == TOK_EOF) {
10192e2caf59SThomas Veerman 	/*
10202e2caf59SThomas Veerman 	 * If we reached the end of the expression, the expression
10212e2caf59SThomas Veerman 	 * is malformed...
10222e2caf59SThomas Veerman 	 */
10232e2caf59SThomas Veerman 	t = TOK_ERROR;
10242e2caf59SThomas Veerman     } else if (t == TOK_LPAREN) {
10252e2caf59SThomas Veerman 	/*
10262e2caf59SThomas Veerman 	 * T -> ( E )
10272e2caf59SThomas Veerman 	 */
10282e2caf59SThomas Veerman 	t = CondE(doEval);
10292e2caf59SThomas Veerman 	if (t != TOK_ERROR) {
10302e2caf59SThomas Veerman 	    if (CondToken(doEval) != TOK_RPAREN) {
10312e2caf59SThomas Veerman 		t = TOK_ERROR;
10322e2caf59SThomas Veerman 	    }
10332e2caf59SThomas Veerman 	}
10342e2caf59SThomas Veerman     } else if (t == TOK_NOT) {
10352e2caf59SThomas Veerman 	t = CondT(doEval);
10362e2caf59SThomas Veerman 	if (t == TOK_TRUE) {
10372e2caf59SThomas Veerman 	    t = TOK_FALSE;
10382e2caf59SThomas Veerman 	} else if (t == TOK_FALSE) {
10392e2caf59SThomas Veerman 	    t = TOK_TRUE;
10402e2caf59SThomas Veerman 	}
10412e2caf59SThomas Veerman     }
10422e2caf59SThomas Veerman     return (t);
10432e2caf59SThomas Veerman }
10442e2caf59SThomas Veerman 
10452e2caf59SThomas Veerman /*-
10462e2caf59SThomas Veerman  *-----------------------------------------------------------------------
10472e2caf59SThomas Veerman  * CondF --
10482e2caf59SThomas Veerman  *	Parse a conjunctive factor (nice name, wot?)
10492e2caf59SThomas Veerman  *	    F -> T && F | T
10502e2caf59SThomas Veerman  *
10512e2caf59SThomas Veerman  * Results:
10522e2caf59SThomas Veerman  *	TOK_TRUE, TOK_FALSE or TOK_ERROR
10532e2caf59SThomas Veerman  *
10542e2caf59SThomas Veerman  * Side Effects:
10552e2caf59SThomas Veerman  *	Tokens are consumed.
10562e2caf59SThomas Veerman  *
10572e2caf59SThomas Veerman  *-----------------------------------------------------------------------
10582e2caf59SThomas Veerman  */
10592e2caf59SThomas Veerman static Token
CondF(Boolean doEval)10602e2caf59SThomas Veerman CondF(Boolean doEval)
10612e2caf59SThomas Veerman {
10622e2caf59SThomas Veerman     Token   l, o;
10632e2caf59SThomas Veerman 
10642e2caf59SThomas Veerman     l = CondT(doEval);
10652e2caf59SThomas Veerman     if (l != TOK_ERROR) {
10662e2caf59SThomas Veerman 	o = CondToken(doEval);
10672e2caf59SThomas Veerman 
10682e2caf59SThomas Veerman 	if (o == TOK_AND) {
10692e2caf59SThomas Veerman 	    /*
10702e2caf59SThomas Veerman 	     * F -> T && F
10712e2caf59SThomas Veerman 	     *
10722e2caf59SThomas Veerman 	     * If T is TOK_FALSE, the whole thing will be TOK_FALSE, but we have to
10732e2caf59SThomas Veerman 	     * parse the r.h.s. anyway (to throw it away).
10742e2caf59SThomas Veerman 	     * If T is TOK_TRUE, the result is the r.h.s., be it an TOK_ERROR or no.
10752e2caf59SThomas Veerman 	     */
10762e2caf59SThomas Veerman 	    if (l == TOK_TRUE) {
10772e2caf59SThomas Veerman 		l = CondF(doEval);
10782e2caf59SThomas Veerman 	    } else {
10792e2caf59SThomas Veerman 		(void)CondF(FALSE);
10802e2caf59SThomas Veerman 	    }
10812e2caf59SThomas Veerman 	} else {
10822e2caf59SThomas Veerman 	    /*
10832e2caf59SThomas Veerman 	     * F -> T
10842e2caf59SThomas Veerman 	     */
10852e2caf59SThomas Veerman 	    CondPushBack(o);
10862e2caf59SThomas Veerman 	}
10872e2caf59SThomas Veerman     }
10882e2caf59SThomas Veerman     return (l);
10892e2caf59SThomas Veerman }
10902e2caf59SThomas Veerman 
10912e2caf59SThomas Veerman /*-
10922e2caf59SThomas Veerman  *-----------------------------------------------------------------------
10932e2caf59SThomas Veerman  * CondE --
10942e2caf59SThomas Veerman  *	Main expression production.
10952e2caf59SThomas Veerman  *	    E -> F || E | F
10962e2caf59SThomas Veerman  *
10972e2caf59SThomas Veerman  * Results:
10982e2caf59SThomas Veerman  *	TOK_TRUE, TOK_FALSE or TOK_ERROR.
10992e2caf59SThomas Veerman  *
11002e2caf59SThomas Veerman  * Side Effects:
11012e2caf59SThomas Veerman  *	Tokens are, of course, consumed.
11022e2caf59SThomas Veerman  *
11032e2caf59SThomas Veerman  *-----------------------------------------------------------------------
11042e2caf59SThomas Veerman  */
11052e2caf59SThomas Veerman static Token
CondE(Boolean doEval)11062e2caf59SThomas Veerman CondE(Boolean doEval)
11072e2caf59SThomas Veerman {
11082e2caf59SThomas Veerman     Token   l, o;
11092e2caf59SThomas Veerman 
11102e2caf59SThomas Veerman     l = CondF(doEval);
11112e2caf59SThomas Veerman     if (l != TOK_ERROR) {
11122e2caf59SThomas Veerman 	o = CondToken(doEval);
11132e2caf59SThomas Veerman 
11142e2caf59SThomas Veerman 	if (o == TOK_OR) {
11152e2caf59SThomas Veerman 	    /*
11162e2caf59SThomas Veerman 	     * E -> F || E
11172e2caf59SThomas Veerman 	     *
11182e2caf59SThomas Veerman 	     * A similar thing occurs for ||, except that here we make sure
11192e2caf59SThomas Veerman 	     * the l.h.s. is TOK_FALSE before we bother to evaluate the r.h.s.
11202e2caf59SThomas Veerman 	     * Once again, if l is TOK_FALSE, the result is the r.h.s. and once
11212e2caf59SThomas Veerman 	     * again if l is TOK_TRUE, we parse the r.h.s. to throw it away.
11222e2caf59SThomas Veerman 	     */
11232e2caf59SThomas Veerman 	    if (l == TOK_FALSE) {
11242e2caf59SThomas Veerman 		l = CondE(doEval);
11252e2caf59SThomas Veerman 	    } else {
11262e2caf59SThomas Veerman 		(void)CondE(FALSE);
11272e2caf59SThomas Veerman 	    }
11282e2caf59SThomas Veerman 	} else {
11292e2caf59SThomas Veerman 	    /*
11302e2caf59SThomas Veerman 	     * E -> F
11312e2caf59SThomas Veerman 	     */
11322e2caf59SThomas Veerman 	    CondPushBack(o);
11332e2caf59SThomas Veerman 	}
11342e2caf59SThomas Veerman     }
11352e2caf59SThomas Veerman     return (l);
11362e2caf59SThomas Veerman }
11372e2caf59SThomas Veerman 
11382e2caf59SThomas Veerman /*-
11392e2caf59SThomas Veerman  *-----------------------------------------------------------------------
11402e2caf59SThomas Veerman  * Cond_EvalExpression --
11412e2caf59SThomas Veerman  *	Evaluate an expression in the passed line. The expression
11422e2caf59SThomas Veerman  *	consists of &&, ||, !, make(target), defined(variable)
11432e2caf59SThomas Veerman  *	and parenthetical groupings thereof.
11442e2caf59SThomas Veerman  *
11452e2caf59SThomas Veerman  * Results:
11462e2caf59SThomas Veerman  *	COND_PARSE	if the condition was valid grammatically
11472e2caf59SThomas Veerman  *	COND_INVALID  	if not a valid conditional.
11482e2caf59SThomas Veerman  *
11492e2caf59SThomas Veerman  *	(*value) is set to the boolean value of the condition
11502e2caf59SThomas Veerman  *
11512e2caf59SThomas Veerman  * Side Effects:
11522e2caf59SThomas Veerman  *	None.
11532e2caf59SThomas Veerman  *
11542e2caf59SThomas Veerman  *-----------------------------------------------------------------------
11552e2caf59SThomas Veerman  */
11562e2caf59SThomas Veerman int
Cond_EvalExpression(const struct If * info,char * line,Boolean * value,int eprint,Boolean strictLHS)1157*0a6a1f1dSLionel Sambuc Cond_EvalExpression(const struct If *info, char *line, Boolean *value, int eprint, Boolean strictLHS)
11582e2caf59SThomas Veerman {
11592e2caf59SThomas Veerman     static const struct If *dflt_info;
11602e2caf59SThomas Veerman     const struct If *sv_if_info = if_info;
11612e2caf59SThomas Veerman     char *sv_condExpr = condExpr;
11622e2caf59SThomas Veerman     Token sv_condPushBack = condPushBack;
11632e2caf59SThomas Veerman     int rval;
11642e2caf59SThomas Veerman 
1165*0a6a1f1dSLionel Sambuc     lhsStrict = strictLHS;
1166*0a6a1f1dSLionel Sambuc 
11672e2caf59SThomas Veerman     while (*line == ' ' || *line == '\t')
11682e2caf59SThomas Veerman 	line++;
11692e2caf59SThomas Veerman 
11702e2caf59SThomas Veerman     if (info == NULL && (info = dflt_info) == NULL) {
11712e2caf59SThomas Veerman 	/* Scan for the entry for .if - it can't be first */
11722e2caf59SThomas Veerman 	for (info = ifs; ; info++)
11732e2caf59SThomas Veerman 	    if (info->form[0] == 0)
11742e2caf59SThomas Veerman 		break;
11752e2caf59SThomas Veerman 	dflt_info = info;
11762e2caf59SThomas Veerman     }
11772e2caf59SThomas Veerman 
11782e2caf59SThomas Veerman     if_info = info != NULL ? info : ifs + 4;
11792e2caf59SThomas Veerman     condExpr = line;
11802e2caf59SThomas Veerman     condPushBack = TOK_NONE;
11812e2caf59SThomas Veerman 
11822e2caf59SThomas Veerman     rval = do_Cond_EvalExpression(value);
11832e2caf59SThomas Veerman 
11842e2caf59SThomas Veerman     if (rval == COND_INVALID && eprint)
11852e2caf59SThomas Veerman 	Parse_Error(PARSE_FATAL, "Malformed conditional (%s)", line);
11862e2caf59SThomas Veerman 
11872e2caf59SThomas Veerman     if_info = sv_if_info;
11882e2caf59SThomas Veerman     condExpr = sv_condExpr;
11892e2caf59SThomas Veerman     condPushBack = sv_condPushBack;
11902e2caf59SThomas Veerman 
11912e2caf59SThomas Veerman     return rval;
11922e2caf59SThomas Veerman }
11932e2caf59SThomas Veerman 
11942e2caf59SThomas Veerman static int
do_Cond_EvalExpression(Boolean * value)11952e2caf59SThomas Veerman do_Cond_EvalExpression(Boolean *value)
11962e2caf59SThomas Veerman {
11972e2caf59SThomas Veerman 
11982e2caf59SThomas Veerman     switch (CondE(TRUE)) {
11992e2caf59SThomas Veerman     case TOK_TRUE:
12002e2caf59SThomas Veerman 	if (CondToken(TRUE) == TOK_EOF) {
12012e2caf59SThomas Veerman 	    *value = TRUE;
12022e2caf59SThomas Veerman 	    return COND_PARSE;
12032e2caf59SThomas Veerman 	}
12042e2caf59SThomas Veerman 	break;
12052e2caf59SThomas Veerman     case TOK_FALSE:
12062e2caf59SThomas Veerman 	if (CondToken(TRUE) == TOK_EOF) {
12072e2caf59SThomas Veerman 	    *value = FALSE;
12082e2caf59SThomas Veerman 	    return COND_PARSE;
12092e2caf59SThomas Veerman 	}
12102e2caf59SThomas Veerman 	break;
12112e2caf59SThomas Veerman     default:
12122e2caf59SThomas Veerman     case TOK_ERROR:
12132e2caf59SThomas Veerman 	break;
12142e2caf59SThomas Veerman     }
12152e2caf59SThomas Veerman 
12162e2caf59SThomas Veerman     return COND_INVALID;
12172e2caf59SThomas Veerman }
12182e2caf59SThomas Veerman 
12192e2caf59SThomas Veerman 
12202e2caf59SThomas Veerman /*-
12212e2caf59SThomas Veerman  *-----------------------------------------------------------------------
12222e2caf59SThomas Veerman  * Cond_Eval --
12232e2caf59SThomas Veerman  *	Evaluate the conditional in the passed line. The line
12242e2caf59SThomas Veerman  *	looks like this:
12252e2caf59SThomas Veerman  *	    .<cond-type> <expr>
12262e2caf59SThomas Veerman  *	where <cond-type> is any of if, ifmake, ifnmake, ifdef,
12272e2caf59SThomas Veerman  *	ifndef, elif, elifmake, elifnmake, elifdef, elifndef
12282e2caf59SThomas Veerman  *	and <expr> consists of &&, ||, !, make(target), defined(variable)
12292e2caf59SThomas Veerman  *	and parenthetical groupings thereof.
12302e2caf59SThomas Veerman  *
12312e2caf59SThomas Veerman  * Input:
12322e2caf59SThomas Veerman  *	line		Line to parse
12332e2caf59SThomas Veerman  *
12342e2caf59SThomas Veerman  * Results:
12352e2caf59SThomas Veerman  *	COND_PARSE	if should parse lines after the conditional
12362e2caf59SThomas Veerman  *	COND_SKIP	if should skip lines after the conditional
12372e2caf59SThomas Veerman  *	COND_INVALID  	if not a valid conditional.
12382e2caf59SThomas Veerman  *
12392e2caf59SThomas Veerman  * Side Effects:
12402e2caf59SThomas Veerman  *	None.
12412e2caf59SThomas Veerman  *
12422e2caf59SThomas Veerman  * Note that the states IF_ACTIVE and ELSE_ACTIVE are only different in order
12432e2caf59SThomas Veerman  * to detect splurious .else lines (as are SKIP_TO_ELSE and SKIP_TO_ENDIF)
12442e2caf59SThomas Veerman  * otherwise .else could be treated as '.elif 1'.
12452e2caf59SThomas Veerman  *
12462e2caf59SThomas Veerman  *-----------------------------------------------------------------------
12472e2caf59SThomas Veerman  */
12482e2caf59SThomas Veerman int
Cond_Eval(char * line)12492e2caf59SThomas Veerman Cond_Eval(char *line)
12502e2caf59SThomas Veerman {
12512bc7c627SLionel Sambuc #define	    MAXIF      128	/* maximum depth of .if'ing */
125284d9c625SLionel Sambuc #define	    MAXIF_BUMP  32	/* how much to grow by */
12532e2caf59SThomas Veerman     enum if_states {
12542e2caf59SThomas Veerman 	IF_ACTIVE,		/* .if or .elif part active */
12552e2caf59SThomas Veerman 	ELSE_ACTIVE,		/* .else part active */
12562e2caf59SThomas Veerman 	SEARCH_FOR_ELIF,	/* searching for .elif/else to execute */
12572e2caf59SThomas Veerman 	SKIP_TO_ELSE,           /* has been true, but not seen '.else' */
12582e2caf59SThomas Veerman 	SKIP_TO_ENDIF		/* nothing else to execute */
12592e2caf59SThomas Veerman     };
126084d9c625SLionel Sambuc     static enum if_states *cond_state = NULL;
126184d9c625SLionel Sambuc     static unsigned int max_if_depth = MAXIF;
12622e2caf59SThomas Veerman 
12632e2caf59SThomas Veerman     const struct If *ifp;
12642e2caf59SThomas Veerman     Boolean 	    isElif;
12652e2caf59SThomas Veerman     Boolean 	    value;
12662e2caf59SThomas Veerman     int	    	    level;  	/* Level at which to report errors. */
12672e2caf59SThomas Veerman     enum if_states  state;
12682e2caf59SThomas Veerman 
12692e2caf59SThomas Veerman     level = PARSE_FATAL;
127084d9c625SLionel Sambuc     if (!cond_state) {
127184d9c625SLionel Sambuc 	cond_state = bmake_malloc(max_if_depth * sizeof(*cond_state));
127284d9c625SLionel Sambuc 	cond_state[0] = IF_ACTIVE;
127384d9c625SLionel Sambuc     }
12742e2caf59SThomas Veerman     /* skip leading character (the '.') and any whitespace */
12752e2caf59SThomas Veerman     for (line++; *line == ' ' || *line == '\t'; line++)
12762e2caf59SThomas Veerman 	continue;
12772e2caf59SThomas Veerman 
12782e2caf59SThomas Veerman     /* Find what type of if we're dealing with.  */
12792e2caf59SThomas Veerman     if (line[0] == 'e') {
12802e2caf59SThomas Veerman 	if (line[1] != 'l') {
12812e2caf59SThomas Veerman 	    if (!istoken(line + 1, "ndif", 4))
12822e2caf59SThomas Veerman 		return COND_INVALID;
12832e2caf59SThomas Veerman 	    /* End of conditional section */
12842e2caf59SThomas Veerman 	    if (cond_depth == cond_min_depth) {
12852e2caf59SThomas Veerman 		Parse_Error(level, "if-less endif");
12862e2caf59SThomas Veerman 		return COND_PARSE;
12872e2caf59SThomas Veerman 	    }
12882e2caf59SThomas Veerman 	    /* Return state for previous conditional */
12892e2caf59SThomas Veerman 	    cond_depth--;
12902e2caf59SThomas Veerman 	    return cond_state[cond_depth] <= ELSE_ACTIVE ? COND_PARSE : COND_SKIP;
12912e2caf59SThomas Veerman 	}
12922e2caf59SThomas Veerman 
12932e2caf59SThomas Veerman 	/* Quite likely this is 'else' or 'elif' */
12942e2caf59SThomas Veerman 	line += 2;
12952e2caf59SThomas Veerman 	if (istoken(line, "se", 2)) {
12962e2caf59SThomas Veerman 	    /* It is else... */
12972e2caf59SThomas Veerman 	    if (cond_depth == cond_min_depth) {
12982e2caf59SThomas Veerman 		Parse_Error(level, "if-less else");
12992e2caf59SThomas Veerman 		return COND_PARSE;
13002e2caf59SThomas Veerman 	    }
13012e2caf59SThomas Veerman 
13022e2caf59SThomas Veerman 	    state = cond_state[cond_depth];
13032e2caf59SThomas Veerman 	    switch (state) {
13042e2caf59SThomas Veerman 	    case SEARCH_FOR_ELIF:
13052e2caf59SThomas Veerman 		state = ELSE_ACTIVE;
13062e2caf59SThomas Veerman 		break;
13072e2caf59SThomas Veerman 	    case ELSE_ACTIVE:
13082e2caf59SThomas Veerman 	    case SKIP_TO_ENDIF:
13092e2caf59SThomas Veerman 		Parse_Error(PARSE_WARNING, "extra else");
13102e2caf59SThomas Veerman 		/* FALLTHROUGH */
13112e2caf59SThomas Veerman 	    default:
13122e2caf59SThomas Veerman 	    case IF_ACTIVE:
13132e2caf59SThomas Veerman 	    case SKIP_TO_ELSE:
13142e2caf59SThomas Veerman 		state = SKIP_TO_ENDIF;
13152e2caf59SThomas Veerman 		break;
13162e2caf59SThomas Veerman 	    }
13172e2caf59SThomas Veerman 	    cond_state[cond_depth] = state;
13182e2caf59SThomas Veerman 	    return state <= ELSE_ACTIVE ? COND_PARSE : COND_SKIP;
13192e2caf59SThomas Veerman 	}
13202e2caf59SThomas Veerman 	/* Assume for now it is an elif */
13212e2caf59SThomas Veerman 	isElif = TRUE;
13222e2caf59SThomas Veerman     } else
13232e2caf59SThomas Veerman 	isElif = FALSE;
13242e2caf59SThomas Veerman 
13252e2caf59SThomas Veerman     if (line[0] != 'i' || line[1] != 'f')
13262e2caf59SThomas Veerman 	/* Not an ifxxx or elifxxx line */
13272e2caf59SThomas Veerman 	return COND_INVALID;
13282e2caf59SThomas Veerman 
13292e2caf59SThomas Veerman     /*
13302e2caf59SThomas Veerman      * Figure out what sort of conditional it is -- what its default
13312e2caf59SThomas Veerman      * function is, etc. -- by looking in the table of valid "ifs"
13322e2caf59SThomas Veerman      */
13332e2caf59SThomas Veerman     line += 2;
13342e2caf59SThomas Veerman     for (ifp = ifs; ; ifp++) {
13352e2caf59SThomas Veerman 	if (ifp->form == NULL)
13362e2caf59SThomas Veerman 	    return COND_INVALID;
13372e2caf59SThomas Veerman 	if (istoken(ifp->form, line, ifp->formlen)) {
13382e2caf59SThomas Veerman 	    line += ifp->formlen;
13392e2caf59SThomas Veerman 	    break;
13402e2caf59SThomas Veerman 	}
13412e2caf59SThomas Veerman     }
13422e2caf59SThomas Veerman 
13432e2caf59SThomas Veerman     /* Now we know what sort of 'if' it is... */
13442e2caf59SThomas Veerman 
13452e2caf59SThomas Veerman     if (isElif) {
13462e2caf59SThomas Veerman 	if (cond_depth == cond_min_depth) {
13472e2caf59SThomas Veerman 	    Parse_Error(level, "if-less elif");
13482e2caf59SThomas Veerman 	    return COND_PARSE;
13492e2caf59SThomas Veerman 	}
13502e2caf59SThomas Veerman 	state = cond_state[cond_depth];
13512e2caf59SThomas Veerman 	if (state == SKIP_TO_ENDIF || state == ELSE_ACTIVE) {
13522e2caf59SThomas Veerman 	    Parse_Error(PARSE_WARNING, "extra elif");
13532e2caf59SThomas Veerman 	    cond_state[cond_depth] = SKIP_TO_ENDIF;
13542e2caf59SThomas Veerman 	    return COND_SKIP;
13552e2caf59SThomas Veerman 	}
13562e2caf59SThomas Veerman 	if (state != SEARCH_FOR_ELIF) {
13572e2caf59SThomas Veerman 	    /* Either just finished the 'true' block, or already SKIP_TO_ELSE */
13582e2caf59SThomas Veerman 	    cond_state[cond_depth] = SKIP_TO_ELSE;
13592e2caf59SThomas Veerman 	    return COND_SKIP;
13602e2caf59SThomas Veerman 	}
13612e2caf59SThomas Veerman     } else {
13622e2caf59SThomas Veerman 	/* Normal .if */
136384d9c625SLionel Sambuc 	if (cond_depth + 1 >= max_if_depth) {
136484d9c625SLionel Sambuc 	    /*
136584d9c625SLionel Sambuc 	     * This is rare, but not impossible.
136684d9c625SLionel Sambuc 	     * In meta mode, dirdeps.mk (only runs at level 0)
136784d9c625SLionel Sambuc 	     * can need more than the default.
136884d9c625SLionel Sambuc 	     */
136984d9c625SLionel Sambuc 	    max_if_depth += MAXIF_BUMP;
137084d9c625SLionel Sambuc 	    cond_state = bmake_realloc(cond_state, max_if_depth *
137184d9c625SLionel Sambuc 		sizeof(*cond_state));
13722e2caf59SThomas Veerman 	}
13732e2caf59SThomas Veerman 	state = cond_state[cond_depth];
13742e2caf59SThomas Veerman 	cond_depth++;
13752e2caf59SThomas Veerman 	if (state > ELSE_ACTIVE) {
13762e2caf59SThomas Veerman 	    /* If we aren't parsing the data, treat as always false */
13772e2caf59SThomas Veerman 	    cond_state[cond_depth] = SKIP_TO_ELSE;
13782e2caf59SThomas Veerman 	    return COND_SKIP;
13792e2caf59SThomas Veerman 	}
13802e2caf59SThomas Veerman     }
13812e2caf59SThomas Veerman 
13822e2caf59SThomas Veerman     /* And evaluate the conditional expresssion */
1383*0a6a1f1dSLionel Sambuc     if (Cond_EvalExpression(ifp, line, &value, 1, TRUE) == COND_INVALID) {
13842e2caf59SThomas Veerman 	/* Syntax error in conditional, error message already output. */
13852e2caf59SThomas Veerman 	/* Skip everything to matching .endif */
13862e2caf59SThomas Veerman 	cond_state[cond_depth] = SKIP_TO_ELSE;
13872e2caf59SThomas Veerman 	return COND_SKIP;
13882e2caf59SThomas Veerman     }
13892e2caf59SThomas Veerman 
13902e2caf59SThomas Veerman     if (!value) {
13912e2caf59SThomas Veerman 	cond_state[cond_depth] = SEARCH_FOR_ELIF;
13922e2caf59SThomas Veerman 	return COND_SKIP;
13932e2caf59SThomas Veerman     }
13942e2caf59SThomas Veerman     cond_state[cond_depth] = IF_ACTIVE;
13952e2caf59SThomas Veerman     return COND_PARSE;
13962e2caf59SThomas Veerman }
13972e2caf59SThomas Veerman 
13982e2caf59SThomas Veerman 
13992e2caf59SThomas Veerman 
14002e2caf59SThomas Veerman /*-
14012e2caf59SThomas Veerman  *-----------------------------------------------------------------------
14022e2caf59SThomas Veerman  * Cond_End --
14032e2caf59SThomas Veerman  *	Make sure everything's clean at the end of a makefile.
14042e2caf59SThomas Veerman  *
14052e2caf59SThomas Veerman  * Results:
14062e2caf59SThomas Veerman  *	None.
14072e2caf59SThomas Veerman  *
14082e2caf59SThomas Veerman  * Side Effects:
14092e2caf59SThomas Veerman  *	Parse_Error will be called if open conditionals are around.
14102e2caf59SThomas Veerman  *
14112e2caf59SThomas Veerman  *-----------------------------------------------------------------------
14122e2caf59SThomas Veerman  */
14132e2caf59SThomas Veerman void
Cond_restore_depth(unsigned int saved_depth)14142e2caf59SThomas Veerman Cond_restore_depth(unsigned int saved_depth)
14152e2caf59SThomas Veerman {
14162e2caf59SThomas Veerman     int open_conds = cond_depth - cond_min_depth;
14172e2caf59SThomas Veerman 
14182e2caf59SThomas Veerman     if (open_conds != 0 || saved_depth > cond_depth) {
14192e2caf59SThomas Veerman 	Parse_Error(PARSE_FATAL, "%d open conditional%s", open_conds,
14202e2caf59SThomas Veerman 		    open_conds == 1 ? "" : "s");
14212e2caf59SThomas Veerman 	cond_depth = cond_min_depth;
14222e2caf59SThomas Veerman     }
14232e2caf59SThomas Veerman 
14242e2caf59SThomas Veerman     cond_min_depth = saved_depth;
14252e2caf59SThomas Veerman }
14262e2caf59SThomas Veerman 
14272e2caf59SThomas Veerman unsigned int
Cond_save_depth(void)14282e2caf59SThomas Veerman Cond_save_depth(void)
14292e2caf59SThomas Veerman {
14302e2caf59SThomas Veerman     int depth = cond_min_depth;
14312e2caf59SThomas Veerman 
14322e2caf59SThomas Veerman     cond_min_depth = cond_depth;
14332e2caf59SThomas Veerman     return depth;
14342e2caf59SThomas Veerman }
1435