xref: /freebsd/contrib/bmake/cond.c (revision 96fbe51956c9df8bdb8317413b1c487b14e4ee68)
1 /*	$NetBSD: cond.c,v 1.214 2020/11/13 09:01:59 rillig Exp $	*/
2 
3 /*
4  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Adam de Boor.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 /*
36  * Copyright (c) 1988, 1989 by Adam de Boor
37  * Copyright (c) 1989 by Berkeley Softworks
38  * All rights reserved.
39  *
40  * This code is derived from software contributed to Berkeley by
41  * Adam de Boor.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *	This product includes software developed by the University of
54  *	California, Berkeley and its contributors.
55  * 4. Neither the name of the University nor the names of its contributors
56  *    may be used to endorse or promote products derived from this software
57  *    without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  */
71 
72 /* Handling of conditionals in a makefile.
73  *
74  * Interface:
75  *	Cond_EvalLine   Evaluate the conditional directive, such as
76  *			'.if <cond>', '.elifnmake <cond>', '.else', '.endif'.
77  *
78  *	Cond_EvalCondition
79  *			Evaluate the conditional, which is either the argument
80  *			of one of the .if directives or the condition in a
81  *			':?then:else' variable modifier.
82  *
83  *	Cond_save_depth
84  *	Cond_restore_depth
85  *			Save and restore the nesting of the conditions, at
86  *			the start and end of including another makefile, to
87  *			ensure that in each makefile the conditional
88  *			directives are well-balanced.
89  */
90 
91 #include <errno.h>
92 
93 #include "make.h"
94 #include "dir.h"
95 
96 /*	"@(#)cond.c	8.2 (Berkeley) 1/2/94"	*/
97 MAKE_RCSID("$NetBSD: cond.c,v 1.214 2020/11/13 09:01:59 rillig Exp $");
98 
99 /*
100  * The parsing of conditional expressions is based on this grammar:
101  *	E -> F || E
102  *	E -> F
103  *	F -> T && F
104  *	F -> T
105  *	T -> defined(variable)
106  *	T -> make(target)
107  *	T -> exists(file)
108  *	T -> empty(varspec)
109  *	T -> target(name)
110  *	T -> commands(name)
111  *	T -> symbol
112  *	T -> $(varspec) op value
113  *	T -> $(varspec) == "string"
114  *	T -> $(varspec) != "string"
115  *	T -> "string"
116  *	T -> ( E )
117  *	T -> ! T
118  *	op -> == | != | > | < | >= | <=
119  *
120  * 'symbol' is some other symbol to which the default function is applied.
121  *
122  * The tokens are scanned by CondToken, which returns:
123  *	TOK_AND		for '&' or '&&'
124  *	TOK_OR		for '|' or '||'
125  *	TOK_NOT		for '!'
126  *	TOK_LPAREN	for '('
127  *	TOK_RPAREN	for ')'
128  * Other terminal symbols are evaluated using either the default function or
129  * the function given in the terminal, they return either TOK_TRUE or
130  * TOK_FALSE.
131  *
132  * TOK_FALSE is 0 and TOK_TRUE 1 so we can directly assign C comparisons.
133  *
134  * All non-terminal functions (CondParser_Expr, CondParser_Factor and
135  * CondParser_Term) return either TOK_FALSE, TOK_TRUE, or TOK_ERROR on error.
136  */
137 typedef enum Token {
138     TOK_FALSE = 0, TOK_TRUE = 1, TOK_AND, TOK_OR, TOK_NOT,
139     TOK_LPAREN, TOK_RPAREN, TOK_EOF, TOK_NONE, TOK_ERROR
140 } Token;
141 
142 typedef struct CondParser {
143     const struct If *if_info;	/* Info for current statement */
144     const char *p;		/* The remaining condition to parse */
145     Token curr;			/* Single push-back token used in parsing */
146 
147     /* Whether an error message has already been printed for this condition.
148      * The first available error message is usually the most specific one,
149      * therefore it makes sense to suppress the standard "Malformed
150      * conditional" message. */
151     Boolean printedError;
152 } CondParser;
153 
154 static Token CondParser_Expr(CondParser *par, Boolean);
155 
156 static unsigned int cond_depth = 0;	/* current .if nesting level */
157 static unsigned int cond_min_depth = 0;	/* depth at makefile open */
158 
159 /*
160  * Indicate when we should be strict about lhs of comparisons.
161  * In strict mode, the lhs must be a variable expression or a string literal
162  * in quotes. In non-strict mode it may also be an unquoted string literal.
163  *
164  * TRUE when CondEvalExpression is called from Cond_EvalLine (.if etc)
165  * FALSE when CondEvalExpression is called from ApplyModifier_IfElse
166  * since lhs is already expanded, and at that point we cannot tell if
167  * it was a variable reference or not.
168  */
169 static Boolean lhsStrict;
170 
171 static int
172 is_token(const char *str, const char *tok, size_t len)
173 {
174     return strncmp(str, tok, len) == 0 && !ch_isalpha(str[len]);
175 }
176 
177 static Token
178 ToToken(Boolean cond)
179 {
180     return cond ? TOK_TRUE : TOK_FALSE;
181 }
182 
183 /* Push back the most recent token read. We only need one level of this. */
184 static void
185 CondParser_PushBack(CondParser *par, Token t)
186 {
187     assert(par->curr == TOK_NONE);
188     assert(t != TOK_NONE);
189 
190     par->curr = t;
191 }
192 
193 static void
194 CondParser_SkipWhitespace(CondParser *par)
195 {
196     cpp_skip_whitespace(&par->p);
197 }
198 
199 /* Parse the argument of a built-in function.
200  *
201  * Arguments:
202  *	*pp initially points at the '(',
203  *	upon successful return it points right after the ')'.
204  *
205  *	*out_arg receives the argument as string.
206  *
207  *	func says whether the argument belongs to an actual function, or
208  *	whether the parsed argument is passed to the default function.
209  *
210  * Return the length of the argument, or 0 on error. */
211 static size_t
212 ParseFuncArg(const char **pp, Boolean doEval, const char *func,
213 	     char **out_arg) {
214     const char *p = *pp;
215     Buffer argBuf;
216     int paren_depth;
217     size_t argLen;
218 
219     if (func != NULL)
220 	p++;			/* Skip opening '(' - verified by caller */
221 
222     if (*p == '\0') {
223 	*out_arg = NULL;	/* Missing closing parenthesis: */
224 	return 0;		/* .if defined( */
225     }
226 
227     cpp_skip_hspace(&p);
228 
229     Buf_InitSize(&argBuf, 16);
230 
231     paren_depth = 0;
232     for (;;) {
233 	char ch = *p;
234 	if (ch == '\0' || ch == ' ' || ch == '\t')
235 	    break;
236 	if ((ch == '&' || ch == '|') && paren_depth == 0)
237 	    break;
238 	if (*p == '$') {
239 	    /*
240 	     * Parse the variable spec and install it as part of the argument
241 	     * if it's valid. We tell Var_Parse to complain on an undefined
242 	     * variable, so we don't need to do it. Nor do we return an error,
243 	     * though perhaps we should...
244 	     */
245 	    void *nestedVal_freeIt;
246 	    VarEvalFlags eflags = doEval ? VARE_WANTRES | VARE_UNDEFERR
247 					 : VARE_NONE;
248 	    const char *nestedVal;
249 	    (void)Var_Parse(&p, VAR_CMDLINE, eflags, &nestedVal,
250 			    &nestedVal_freeIt);
251 	    /* TODO: handle errors */
252 	    Buf_AddStr(&argBuf, nestedVal);
253 	    free(nestedVal_freeIt);
254 	    continue;
255 	}
256 	if (ch == '(')
257 	    paren_depth++;
258 	else if (ch == ')' && --paren_depth < 0)
259 	    break;
260 	Buf_AddByte(&argBuf, *p);
261 	p++;
262     }
263 
264     *out_arg = Buf_GetAll(&argBuf, &argLen);
265     Buf_Destroy(&argBuf, FALSE);
266 
267     cpp_skip_hspace(&p);
268 
269     if (func != NULL && *p++ != ')') {
270 	Parse_Error(PARSE_WARNING, "Missing closing parenthesis for %s()",
271 		    func);
272 	/* The PARSE_FATAL is done as a follow-up by CondEvalExpression. */
273 	return 0;
274     }
275 
276     *pp = p;
277     return argLen;
278 }
279 
280 /* Test whether the given variable is defined. */
281 static Boolean
282 FuncDefined(size_t argLen MAKE_ATTR_UNUSED, const char *arg)
283 {
284     void *freeIt;
285     Boolean result = Var_Value(arg, VAR_CMDLINE, &freeIt) != NULL;
286     bmake_free(freeIt);
287     return result;
288 }
289 
290 /* See if the given target is being made. */
291 static Boolean
292 FuncMake(size_t argLen MAKE_ATTR_UNUSED, const char *arg)
293 {
294     StringListNode *ln;
295 
296     for (ln = opts.create->first; ln != NULL; ln = ln->next)
297 	if (Str_Match(ln->datum, arg))
298 	    return TRUE;
299     return FALSE;
300 }
301 
302 /* See if the given file exists. */
303 static Boolean
304 FuncExists(size_t argLen MAKE_ATTR_UNUSED, const char *arg)
305 {
306     Boolean result;
307     char *path;
308 
309     path = Dir_FindFile(arg, dirSearchPath);
310     DEBUG2(COND, "exists(%s) result is \"%s\"\n",
311 	   arg, path != NULL ? path : "");
312     result = path != NULL;
313     free(path);
314     return result;
315 }
316 
317 /* See if the given node exists and is an actual target. */
318 static Boolean
319 FuncTarget(size_t argLen MAKE_ATTR_UNUSED, const char *arg)
320 {
321     GNode *gn = Targ_FindNode(arg);
322     return gn != NULL && GNode_IsTarget(gn);
323 }
324 
325 /* See if the given node exists and is an actual target with commands
326  * associated with it. */
327 static Boolean
328 FuncCommands(size_t argLen MAKE_ATTR_UNUSED, const char *arg)
329 {
330     GNode *gn = Targ_FindNode(arg);
331     return gn != NULL && GNode_IsTarget(gn) && !Lst_IsEmpty(gn->commands);
332 }
333 
334 /*
335  * Convert the given number into a double.
336  * We try a base 10 or 16 integer conversion first, if that fails
337  * then we try a floating point conversion instead.
338  *
339  * Results:
340  *	Returns TRUE if the conversion succeeded.
341  *	Sets 'out_value' to the converted number.
342  */
343 static Boolean
344 TryParseNumber(const char *str, double *out_value)
345 {
346     char *end;
347     unsigned long ul_val;
348     double dbl_val;
349 
350     errno = 0;
351     if (str[0] == '\0') {	/* XXX: why is an empty string a number? */
352 	*out_value = 0.0;
353 	return TRUE;
354     }
355 
356     ul_val = strtoul(str, &end, str[1] == 'x' ? 16 : 10);
357     if (*end == '\0' && errno != ERANGE) {
358 	*out_value = str[0] == '-' ? -(double)-ul_val : (double)ul_val;
359 	return TRUE;
360     }
361 
362     if (*end != '\0' && *end != '.' && *end != 'e' && *end != 'E')
363 	return FALSE;		/* skip the expensive strtod call */
364     dbl_val = strtod(str, &end);
365     if (*end != '\0')
366 	return FALSE;
367 
368     *out_value = dbl_val;
369     return TRUE;
370 }
371 
372 static Boolean
373 is_separator(char ch)
374 {
375     return ch == '\0' || ch_isspace(ch) || strchr("!=><)", ch) != NULL;
376 }
377 
378 /*-
379  * Parse a string from a variable reference or an optionally quoted
380  * string.  This is called for the lhs and rhs of string comparisons.
381  *
382  * Results:
383  *	Returns the string, absent any quotes, or NULL on error.
384  *	Sets out_quoted if the string was quoted.
385  *	Sets out_freeIt.
386  */
387 /* coverity:[+alloc : arg-*4] */
388 static const char *
389 CondParser_String(CondParser *par, Boolean doEval, Boolean strictLHS,
390 		  Boolean *out_quoted, void **out_freeIt)
391 {
392     Buffer buf;
393     const char *str;
394     Boolean atStart;
395     const char *nested_p;
396     Boolean quoted;
397     const char *start;
398     VarEvalFlags eflags;
399     VarParseResult parseResult;
400 
401     Buf_Init(&buf);
402     str = NULL;
403     *out_freeIt = NULL;
404     *out_quoted = quoted = par->p[0] == '"';
405     start = par->p;
406     if (quoted)
407 	par->p++;
408     while (par->p[0] != '\0' && str == NULL) {
409 	switch (par->p[0]) {
410 	case '\\':
411 	    par->p++;
412 	    if (par->p[0] != '\0') {
413 		Buf_AddByte(&buf, par->p[0]);
414 		par->p++;
415 	    }
416 	    continue;
417 	case '"':
418 	    if (quoted) {
419 		par->p++;	/* skip the closing quote */
420 		goto got_str;
421 	    }
422 	    Buf_AddByte(&buf, par->p[0]); /* likely? */
423 	    par->p++;
424 	    continue;
425 	case ')':		/* see is_separator */
426 	case '!':
427 	case '=':
428 	case '>':
429 	case '<':
430 	case ' ':
431 	case '\t':
432 	    if (!quoted)
433 		goto got_str;
434 	    Buf_AddByte(&buf, par->p[0]);
435 	    par->p++;
436 	    continue;
437 	case '$':
438 	    /* if we are in quotes, an undefined variable is ok */
439 	    eflags = doEval && !quoted ? VARE_WANTRES | VARE_UNDEFERR :
440 		     doEval ? VARE_WANTRES :
441 		     VARE_NONE;
442 
443 	    nested_p = par->p;
444 	    atStart = nested_p == start;
445 	    parseResult = Var_Parse(&nested_p, VAR_CMDLINE, eflags, &str,
446 				    out_freeIt);
447 	    /* TODO: handle errors */
448 	    if (str == var_Error) {
449 		if (parseResult & VPR_ANY_MSG)
450 		    par->printedError = TRUE;
451 		if (*out_freeIt != NULL) {
452 		    /* XXX: Can there be any situation in which a returned
453 		     * var_Error requires freeIt? */
454 		    free(*out_freeIt);
455 		    *out_freeIt = NULL;
456 		}
457 		/*
458 		 * Even if !doEval, we still report syntax errors, which
459 		 * is what getting var_Error back with !doEval means.
460 		 */
461 		str = NULL;
462 		goto cleanup;
463 	    }
464 	    par->p = nested_p;
465 
466 	    /*
467 	     * If the '$' started the string literal (which means no quotes),
468 	     * and the variable expression is followed by a space, looks like
469 	     * a comparison operator or is the end of the expression, we are
470 	     * done.
471 	     */
472 	    if (atStart && is_separator(par->p[0]))
473 		goto cleanup;
474 
475 	    Buf_AddStr(&buf, str);
476 	    if (*out_freeIt) {
477 		free(*out_freeIt);
478 		*out_freeIt = NULL;
479 	    }
480 	    str = NULL;		/* not finished yet */
481 	    continue;
482 	default:
483 	    if (strictLHS && !quoted && *start != '$' && !ch_isdigit(*start)) {
484 		/* lhs must be quoted, a variable reference or number */
485 		str = NULL;
486 		goto cleanup;
487 	    }
488 	    Buf_AddByte(&buf, par->p[0]);
489 	    par->p++;
490 	    continue;
491 	}
492     }
493 got_str:
494     *out_freeIt = Buf_GetAll(&buf, NULL);
495     str = *out_freeIt;
496 cleanup:
497     Buf_Destroy(&buf, FALSE);
498     return str;
499 }
500 
501 struct If {
502     const char *form;		/* Form of if */
503     size_t formlen;		/* Length of form */
504     Boolean doNot;		/* TRUE if default function should be negated */
505     Boolean (*defProc)(size_t, const char *); /* Default function to apply */
506 };
507 
508 /* The different forms of .if directives. */
509 static const struct If ifs[] = {
510     { "def",   3, FALSE, FuncDefined },
511     { "ndef",  4, TRUE,  FuncDefined },
512     { "make",  4, FALSE, FuncMake },
513     { "nmake", 5, TRUE,  FuncMake },
514     { "",      0, FALSE, FuncDefined },
515     { NULL,    0, FALSE, NULL }
516 };
517 enum { PLAIN_IF_INDEX = 4 };
518 
519 static Boolean
520 If_Eval(const struct If *if_info, const char *arg, size_t arglen)
521 {
522     Boolean res = if_info->defProc(arglen, arg);
523     return if_info->doNot ? !res : res;
524 }
525 
526 /* Evaluate a "comparison without operator", such as in ".if ${VAR}" or
527  * ".if 0". */
528 static Boolean
529 EvalNotEmpty(CondParser *par, const char *value, Boolean quoted)
530 {
531     double num;
532 
533     /* For .ifxxx "...", check for non-empty string. */
534     if (quoted)
535 	return value[0] != '\0';
536 
537     /* For .ifxxx <number>, compare against zero */
538     if (TryParseNumber(value, &num))
539 	return num != 0.0;
540 
541     /* For .if ${...}, check for non-empty string.  This is different from
542      * the evaluation function from that .if variant, which would test
543      * whether a variable of the given name were defined. */
544     /* XXX: Whitespace should count as empty, just as in ParseEmptyArg. */
545     if (par->if_info->form[0] == '\0')
546 	return value[0] != '\0';
547 
548     /* For the other variants of .ifxxx ${...}, use its default function. */
549     return If_Eval(par->if_info, value, strlen(value));
550 }
551 
552 /* Evaluate a numerical comparison, such as in ".if ${VAR} >= 9". */
553 static Token
554 EvalCompareNum(double lhs, const char *op, double rhs)
555 {
556     DEBUG3(COND, "lhs = %f, rhs = %f, op = %.2s\n", lhs, rhs, op);
557 
558     switch (op[0]) {
559     case '!':
560 	if (op[1] != '=') {
561 	    Parse_Error(PARSE_WARNING, "Unknown operator");
562 	    /* The PARSE_FATAL is done as a follow-up by CondEvalExpression. */
563 	    return TOK_ERROR;
564 	}
565 	return ToToken(lhs != rhs);
566     case '=':
567 	if (op[1] != '=') {
568 	    Parse_Error(PARSE_WARNING, "Unknown operator");
569 	    /* The PARSE_FATAL is done as a follow-up by CondEvalExpression. */
570 	    return TOK_ERROR;
571 	}
572 	return ToToken(lhs == rhs);
573     case '<':
574 	return ToToken(op[1] == '=' ? lhs <= rhs : lhs < rhs);
575     case '>':
576 	return ToToken(op[1] == '=' ? lhs >= rhs : lhs > rhs);
577     }
578     return TOK_ERROR;
579 }
580 
581 static Token
582 EvalCompareStr(const char *lhs, const char *op, const char *rhs)
583 {
584     if (!((op[0] == '!' || op[0] == '=') && op[1] == '=')) {
585 	Parse_Error(PARSE_WARNING,
586 		    "String comparison operator must be either == or !=");
587 	/* The PARSE_FATAL is done as a follow-up by CondEvalExpression. */
588 	return TOK_ERROR;
589     }
590 
591     DEBUG3(COND, "lhs = \"%s\", rhs = \"%s\", op = %.2s\n", lhs, rhs, op);
592     return ToToken((*op == '=') == (strcmp(lhs, rhs) == 0));
593 }
594 
595 /* Evaluate a comparison, such as "${VAR} == 12345". */
596 static Token
597 EvalCompare(const char *lhs, Boolean lhsQuoted, const char *op,
598 	    const char *rhs, Boolean rhsQuoted)
599 {
600     double left, right;
601 
602     if (!rhsQuoted && !lhsQuoted)
603 	if (TryParseNumber(lhs, &left) && TryParseNumber(rhs, &right))
604 	    return EvalCompareNum(left, op, right);
605 
606     return EvalCompareStr(lhs, op, rhs);
607 }
608 
609 /* Parse a comparison condition such as:
610  *
611  *	0
612  *	${VAR:Mpattern}
613  *	${VAR} == value
614  *	${VAR:U0} < 12345
615  */
616 static Token
617 CondParser_Comparison(CondParser *par, Boolean doEval)
618 {
619     Token t = TOK_ERROR;
620     const char *lhs, *op, *rhs;
621     void *lhs_freeIt, *rhs_freeIt;
622     Boolean lhsQuoted, rhsQuoted;
623 
624     /*
625      * Parse the variable spec and skip over it, saving its
626      * value in lhs.
627      */
628     lhs = CondParser_String(par, doEval, lhsStrict, &lhsQuoted, &lhs_freeIt);
629     if (lhs == NULL)
630 	goto done_lhs;
631 
632     CondParser_SkipWhitespace(par);
633 
634     op = par->p;
635     switch (par->p[0]) {
636     case '!':
637     case '=':
638     case '<':
639     case '>':
640 	if (par->p[1] == '=')
641 	    par->p += 2;
642 	else
643 	    par->p++;
644 	break;
645     default:
646 	/* Unknown operator, compare against an empty string or 0. */
647 	t = ToToken(doEval && EvalNotEmpty(par, lhs, lhsQuoted));
648 	goto done_lhs;
649     }
650 
651     CondParser_SkipWhitespace(par);
652 
653     if (par->p[0] == '\0') {
654 	Parse_Error(PARSE_WARNING, "Missing right-hand-side of operator");
655 	/* The PARSE_FATAL is done as a follow-up by CondEvalExpression. */
656 	goto done_lhs;
657     }
658 
659     rhs = CondParser_String(par, doEval, FALSE, &rhsQuoted, &rhs_freeIt);
660     if (rhs == NULL)
661 	goto done_rhs;
662 
663     if (!doEval) {
664 	t = TOK_FALSE;
665 	goto done_rhs;
666     }
667 
668     t = EvalCompare(lhs, lhsQuoted, op, rhs, rhsQuoted);
669 
670 done_rhs:
671     free(rhs_freeIt);
672 done_lhs:
673     free(lhs_freeIt);
674     return t;
675 }
676 
677 /* The argument to empty() is a variable name, optionally followed by
678  * variable modifiers. */
679 static size_t
680 ParseEmptyArg(const char **pp, Boolean doEval,
681 	      const char *func MAKE_ATTR_UNUSED, char **out_arg)
682 {
683     void *val_freeIt;
684     const char *val;
685     size_t magic_res;
686 
687     /* We do all the work here and return the result as the length */
688     *out_arg = NULL;
689 
690     (*pp)--;			/* Make (*pp)[1] point to the '('. */
691     (void)Var_Parse(pp, VAR_CMDLINE, doEval ? VARE_WANTRES : VARE_NONE,
692 		    &val, &val_freeIt);
693     /* TODO: handle errors */
694     /* If successful, *pp points beyond the closing ')' now. */
695 
696     if (val == var_Error) {
697 	free(val_freeIt);
698 	return (size_t)-1;
699     }
700 
701     /* A variable is empty when it just contains spaces... 4/15/92, christos */
702     cpp_skip_whitespace(&val);
703 
704     /*
705      * For consistency with the other functions we can't generate the
706      * true/false here.
707      */
708     magic_res = *val != '\0' ? 2 : 1;
709     free(val_freeIt);
710     return magic_res;
711 }
712 
713 static Boolean
714 FuncEmpty(size_t arglen, const char *arg MAKE_ATTR_UNUSED)
715 {
716     /* Magic values ahead, see ParseEmptyArg. */
717     return arglen == 1;
718 }
719 
720 static Boolean
721 CondParser_Func(CondParser *par, Boolean doEval, Token *out_token)
722 {
723     static const struct fn_def {
724 	const char *fn_name;
725 	size_t fn_name_len;
726 	size_t (*fn_parse)(const char **, Boolean, const char *, char **);
727 	Boolean (*fn_eval)(size_t, const char *);
728     } fns[] = {
729 	{ "defined",  7, ParseFuncArg,  FuncDefined },
730 	{ "make",     4, ParseFuncArg,  FuncMake },
731 	{ "exists",   6, ParseFuncArg,  FuncExists },
732 	{ "empty",    5, ParseEmptyArg, FuncEmpty },
733 	{ "target",   6, ParseFuncArg,  FuncTarget },
734 	{ "commands", 8, ParseFuncArg,  FuncCommands }
735     };
736     const struct fn_def *fn;
737     char *arg = NULL;
738     size_t arglen;
739     const char *cp = par->p;
740     const struct fn_def *fns_end = fns + sizeof fns / sizeof fns[0];
741 
742     for (fn = fns; fn != fns_end; fn++) {
743 	if (!is_token(cp, fn->fn_name, fn->fn_name_len))
744 	    continue;
745 
746 	cp += fn->fn_name_len;
747 	cpp_skip_whitespace(&cp);
748 	if (*cp != '(')
749 	    break;
750 
751 	arglen = fn->fn_parse(&cp, doEval, fn->fn_name, &arg);
752 	if (arglen == 0 || arglen == (size_t)-1) {
753 	    par->p = cp;
754 	    *out_token = arglen == 0 ? TOK_FALSE : TOK_ERROR;
755 	    return TRUE;
756 	}
757 
758 	/* Evaluate the argument using the required function. */
759 	*out_token = ToToken(!doEval || fn->fn_eval(arglen, arg));
760 	free(arg);
761 	par->p = cp;
762 	return TRUE;
763     }
764 
765     return FALSE;
766 }
767 
768 /* Parse a function call, a number, a variable expression or a string
769  * literal. */
770 static Token
771 CondParser_LeafToken(CondParser *par, Boolean doEval)
772 {
773     Token t;
774     char *arg = NULL;
775     size_t arglen;
776     const char *cp = par->p;
777     const char *cp1;
778 
779     if (CondParser_Func(par, doEval, &t))
780 	return t;
781 
782     /* Push anything numeric through the compare expression */
783     cp = par->p;
784     if (ch_isdigit(cp[0]) || cp[0] == '-' || cp[0] == '+')
785 	return CondParser_Comparison(par, doEval);
786 
787     /*
788      * Most likely we have a naked token to apply the default function to.
789      * However ".if a == b" gets here when the "a" is unquoted and doesn't
790      * start with a '$'. This surprises people.
791      * If what follows the function argument is a '=' or '!' then the syntax
792      * would be invalid if we did "defined(a)" - so instead treat as an
793      * expression.
794      */
795     arglen = ParseFuncArg(&cp, doEval, NULL, &arg);
796     cp1 = cp;
797     cpp_skip_whitespace(&cp1);
798     if (*cp1 == '=' || *cp1 == '!')
799 	return CondParser_Comparison(par, doEval);
800     par->p = cp;
801 
802     /*
803      * Evaluate the argument using the default function.
804      * This path always treats .if as .ifdef. To get here, the character
805      * after .if must have been taken literally, so the argument cannot
806      * be empty - even if it contained a variable expansion.
807      */
808     t = ToToken(!doEval || If_Eval(par->if_info, arg, arglen));
809     free(arg);
810     return t;
811 }
812 
813 /* Return the next token or comparison result from the parser. */
814 static Token
815 CondParser_Token(CondParser *par, Boolean doEval)
816 {
817     Token t;
818 
819     t = par->curr;
820     if (t != TOK_NONE) {
821 	par->curr = TOK_NONE;
822 	return t;
823     }
824 
825     cpp_skip_hspace(&par->p);
826 
827     switch (par->p[0]) {
828 
829     case '(':
830 	par->p++;
831 	return TOK_LPAREN;
832 
833     case ')':
834 	par->p++;
835 	return TOK_RPAREN;
836 
837     case '|':
838 	par->p++;
839 	if (par->p[0] == '|')
840 	    par->p++;
841 	else if (opts.lint) {
842 	    Parse_Error(PARSE_FATAL, "Unknown operator '|'");
843 	    par->printedError = TRUE;
844 	    return TOK_ERROR;
845 	}
846 	return TOK_OR;
847 
848     case '&':
849 	par->p++;
850 	if (par->p[0] == '&')
851 	    par->p++;
852 	else if (opts.lint) {
853 	    Parse_Error(PARSE_FATAL, "Unknown operator '&'");
854 	    par->printedError = TRUE;
855 	    return TOK_ERROR;
856 	}
857 	return TOK_AND;
858 
859     case '!':
860 	par->p++;
861 	return TOK_NOT;
862 
863     case '#':			/* XXX: see unit-tests/cond-token-plain.mk */
864     case '\n':			/* XXX: why should this end the condition? */
865 				/* Probably obsolete now, from 1993-03-21. */
866     case '\0':
867 	return TOK_EOF;
868 
869     case '"':
870     case '$':
871 	return CondParser_Comparison(par, doEval);
872 
873     default:
874 	return CondParser_LeafToken(par, doEval);
875     }
876 }
877 
878 /* Parse a single term in the expression. This consists of a terminal symbol
879  * or TOK_NOT and a term (not including the binary operators):
880  *
881  *	T -> defined(variable) | make(target) | exists(file) | symbol
882  *	T -> ! T | ( E )
883  *
884  * Results:
885  *	TOK_TRUE, TOK_FALSE or TOK_ERROR.
886  */
887 static Token
888 CondParser_Term(CondParser *par, Boolean doEval)
889 {
890     Token t;
891 
892     t = CondParser_Token(par, doEval);
893 
894     if (t == TOK_EOF) {
895 	/*
896 	 * If we reached the end of the expression, the expression
897 	 * is malformed...
898 	 */
899 	t = TOK_ERROR;
900     } else if (t == TOK_LPAREN) {
901 	/*
902 	 * T -> ( E )
903 	 */
904 	t = CondParser_Expr(par, doEval);
905 	if (t != TOK_ERROR) {
906 	    if (CondParser_Token(par, doEval) != TOK_RPAREN) {
907 		t = TOK_ERROR;
908 	    }
909 	}
910     } else if (t == TOK_NOT) {
911 	t = CondParser_Term(par, doEval);
912 	if (t == TOK_TRUE) {
913 	    t = TOK_FALSE;
914 	} else if (t == TOK_FALSE) {
915 	    t = TOK_TRUE;
916 	}
917     }
918     return t;
919 }
920 
921 /* Parse a conjunctive factor (nice name, wot?)
922  *
923  *	F -> T && F | T
924  *
925  * Results:
926  *	TOK_TRUE, TOK_FALSE or TOK_ERROR
927  */
928 static Token
929 CondParser_Factor(CondParser *par, Boolean doEval)
930 {
931     Token l, o;
932 
933     l = CondParser_Term(par, doEval);
934     if (l != TOK_ERROR) {
935 	o = CondParser_Token(par, doEval);
936 
937 	if (o == TOK_AND) {
938 	    /*
939 	     * F -> T && F
940 	     *
941 	     * If T is TOK_FALSE, the whole thing will be TOK_FALSE, but we
942 	     * have to parse the r.h.s. anyway (to throw it away).
943 	     * If T is TOK_TRUE, the result is the r.h.s., be it a TOK_ERROR
944 	     * or not.
945 	     */
946 	    if (l == TOK_TRUE) {
947 		l = CondParser_Factor(par, doEval);
948 	    } else {
949 		(void)CondParser_Factor(par, FALSE);
950 	    }
951 	} else {
952 	    /*
953 	     * F -> T
954 	     */
955 	    CondParser_PushBack(par, o);
956 	}
957     }
958     return l;
959 }
960 
961 /* Main expression production.
962  *
963  *	E -> F || E | F
964  *
965  * Results:
966  *	TOK_TRUE, TOK_FALSE or TOK_ERROR.
967  */
968 static Token
969 CondParser_Expr(CondParser *par, Boolean doEval)
970 {
971     Token l, o;
972 
973     l = CondParser_Factor(par, doEval);
974     if (l != TOK_ERROR) {
975 	o = CondParser_Token(par, doEval);
976 
977 	if (o == TOK_OR) {
978 	    /*
979 	     * E -> F || E
980 	     *
981 	     * A similar thing occurs for ||, except that here we make sure
982 	     * the l.h.s. is TOK_FALSE before we bother to evaluate the r.h.s.
983 	     * Once again, if l is TOK_FALSE, the result is the r.h.s. and once
984 	     * again if l is TOK_TRUE, we parse the r.h.s. to throw it away.
985 	     */
986 	    if (l == TOK_FALSE) {
987 		l = CondParser_Expr(par, doEval);
988 	    } else {
989 		(void)CondParser_Expr(par, FALSE);
990 	    }
991 	} else {
992 	    /*
993 	     * E -> F
994 	     */
995 	    CondParser_PushBack(par, o);
996 	}
997     }
998     return l;
999 }
1000 
1001 static CondEvalResult
1002 CondParser_Eval(CondParser *par, Boolean *value)
1003 {
1004     Token res;
1005 
1006     DEBUG1(COND, "CondParser_Eval: %s\n", par->p);
1007 
1008     res = CondParser_Expr(par, TRUE);
1009     if (res != TOK_FALSE && res != TOK_TRUE)
1010 	return COND_INVALID;
1011 
1012     if (CondParser_Token(par, TRUE /* XXX: Why TRUE? */) != TOK_EOF)
1013 	return COND_INVALID;
1014 
1015     *value = res == TOK_TRUE;
1016     return COND_PARSE;
1017 }
1018 
1019 /* Evaluate the condition, including any side effects from the variable
1020  * expressions in the condition. The condition consists of &&, ||, !,
1021  * function(arg), comparisons and parenthetical groupings thereof.
1022  *
1023  * Results:
1024  *	COND_PARSE	if the condition was valid grammatically
1025  *	COND_INVALID	if not a valid conditional.
1026  *
1027  *	(*value) is set to the boolean value of the condition
1028  */
1029 static CondEvalResult
1030 CondEvalExpression(const struct If *info, const char *cond, Boolean *value,
1031 		    Boolean eprint, Boolean strictLHS)
1032 {
1033     CondParser par;
1034     CondEvalResult rval;
1035 
1036     lhsStrict = strictLHS;
1037 
1038     cpp_skip_hspace(&cond);
1039 
1040     par.if_info = info != NULL ? info : ifs + PLAIN_IF_INDEX;
1041     par.p = cond;
1042     par.curr = TOK_NONE;
1043     par.printedError = FALSE;
1044 
1045     rval = CondParser_Eval(&par, value);
1046 
1047     if (rval == COND_INVALID && eprint && !par.printedError)
1048 	Parse_Error(PARSE_FATAL, "Malformed conditional (%s)", cond);
1049 
1050     return rval;
1051 }
1052 
1053 /* Evaluate a condition in a :? modifier, such as
1054  * ${"${VAR}" == value:?yes:no}. */
1055 CondEvalResult
1056 Cond_EvalCondition(const char *cond, Boolean *out_value)
1057 {
1058 	return CondEvalExpression(NULL, cond, out_value, FALSE, FALSE);
1059 }
1060 
1061 /* Evaluate the conditional directive in the line, which is one of:
1062  *
1063  *	.if <cond>
1064  *	.ifmake <cond>
1065  *	.ifnmake <cond>
1066  *	.ifdef <cond>
1067  *	.ifndef <cond>
1068  *	.elif <cond>
1069  *	.elifmake <cond>
1070  *	.elifnmake <cond>
1071  *	.elifdef <cond>
1072  *	.elifndef <cond>
1073  *	.else
1074  *	.endif
1075  *
1076  * In these directives, <cond> consists of &&, ||, !, function(arg),
1077  * comparisons, expressions, bare words, numbers and strings, and
1078  * parenthetical groupings thereof.
1079  *
1080  * Results:
1081  *	COND_PARSE	to continue parsing the lines that follow the
1082  *			conditional (when <cond> evaluates to TRUE)
1083  *	COND_SKIP	to skip the lines after the conditional
1084  *			(when <cond> evaluates to FALSE, or when a previous
1085  *			branch has already been taken)
1086  *	COND_INVALID	if the conditional was not valid, either because of
1087  *			a syntax error or because some variable was undefined
1088  *			or because the condition could not be evaluated
1089  */
1090 CondEvalResult
1091 Cond_EvalLine(const char *const line)
1092 {
1093     typedef enum IfState {
1094 
1095 	/* None of the previous <cond> evaluated to TRUE. */
1096 	IFS_INITIAL	= 0,
1097 
1098 	/* The previous <cond> evaluated to TRUE.
1099 	 * The lines following this condition are interpreted. */
1100 	IFS_ACTIVE	= 1 << 0,
1101 
1102 	/* The previous directive was an '.else'. */
1103 	IFS_SEEN_ELSE	= 1 << 1,
1104 
1105 	/* One of the previous <cond> evaluated to TRUE. */
1106 	IFS_WAS_ACTIVE	= 1 << 2
1107 
1108     } IfState;
1109 
1110     static enum IfState *cond_states = NULL;
1111     static unsigned int cond_states_cap = 128;
1112 
1113     const struct If *ifp;
1114     Boolean isElif;
1115     Boolean value;
1116     IfState state;
1117     const char *p = line;
1118 
1119     if (cond_states == NULL) {
1120 	cond_states = bmake_malloc(cond_states_cap * sizeof *cond_states);
1121 	cond_states[0] = IFS_ACTIVE;
1122     }
1123 
1124     p++;		/* skip the leading '.' */
1125     cpp_skip_hspace(&p);
1126 
1127     /* Parse the name of the directive, such as 'if', 'elif', 'endif'. */
1128     if (p[0] == 'e') {
1129 	if (p[1] != 'l') {
1130 	    if (!is_token(p + 1, "ndif", 4)) {
1131 		/* Unknown directive.  It might still be a transformation
1132 		 * rule like '.elisp.scm', therefore no error message here. */
1133 		return COND_INVALID;
1134 	    }
1135 
1136 	    /* It is an '.endif'. */
1137 	    /* TODO: check for extraneous <cond> */
1138 
1139 	    if (cond_depth == cond_min_depth) {
1140 		Parse_Error(PARSE_FATAL, "if-less endif");
1141 		return COND_PARSE;
1142 	    }
1143 
1144 	    /* Return state for previous conditional */
1145 	    cond_depth--;
1146 	    return cond_states[cond_depth] & IFS_ACTIVE
1147 		   ? COND_PARSE : COND_SKIP;
1148 	}
1149 
1150 	/* Quite likely this is 'else' or 'elif' */
1151 	p += 2;
1152 	if (is_token(p, "se", 2)) {	/* It is an 'else'. */
1153 
1154 	    if (opts.lint && p[2] != '\0')
1155 		Parse_Error(PARSE_FATAL,
1156 			    "The .else directive does not take arguments.");
1157 
1158 	    if (cond_depth == cond_min_depth) {
1159 		Parse_Error(PARSE_FATAL, "if-less else");
1160 		return COND_PARSE;
1161 	    }
1162 
1163 	    state = cond_states[cond_depth];
1164 	    if (state == IFS_INITIAL) {
1165 		state = IFS_ACTIVE | IFS_SEEN_ELSE;
1166 	    } else {
1167 		if (state & IFS_SEEN_ELSE)
1168 		    Parse_Error(PARSE_WARNING, "extra else");
1169 		state = IFS_WAS_ACTIVE | IFS_SEEN_ELSE;
1170 	    }
1171 	    cond_states[cond_depth] = state;
1172 
1173 	    return state & IFS_ACTIVE ? COND_PARSE : COND_SKIP;
1174 	}
1175 	/* Assume for now it is an elif */
1176 	isElif = TRUE;
1177     } else
1178 	isElif = FALSE;
1179 
1180     if (p[0] != 'i' || p[1] != 'f') {
1181 	/* Unknown directive.  It might still be a transformation rule like
1182 	 * '.elisp.scm', therefore no error message here. */
1183 	return COND_INVALID;	/* Not an ifxxx or elifxxx line */
1184     }
1185 
1186     /*
1187      * Figure out what sort of conditional it is -- what its default
1188      * function is, etc. -- by looking in the table of valid "ifs"
1189      */
1190     p += 2;
1191     for (ifp = ifs;; ifp++) {
1192 	if (ifp->form == NULL) {
1193 	    /* TODO: Add error message about unknown directive,
1194 	     * since there is no other known directive that starts with 'el'
1195 	     * or 'if'.
1196 	     * Example: .elifx 123 */
1197 	    return COND_INVALID;
1198 	}
1199 	if (is_token(p, ifp->form, ifp->formlen)) {
1200 	    p += ifp->formlen;
1201 	    break;
1202 	}
1203     }
1204 
1205     /* Now we know what sort of 'if' it is... */
1206 
1207     if (isElif) {
1208 	if (cond_depth == cond_min_depth) {
1209 	    Parse_Error(PARSE_FATAL, "if-less elif");
1210 	    return COND_PARSE;
1211 	}
1212 	state = cond_states[cond_depth];
1213 	if (state & IFS_SEEN_ELSE) {
1214 	    Parse_Error(PARSE_WARNING, "extra elif");
1215 	    cond_states[cond_depth] = IFS_WAS_ACTIVE | IFS_SEEN_ELSE;
1216 	    return COND_SKIP;
1217 	}
1218 	if (state != IFS_INITIAL) {
1219 	    cond_states[cond_depth] = IFS_WAS_ACTIVE;
1220 	    return COND_SKIP;
1221 	}
1222     } else {
1223 	/* Normal .if */
1224 	if (cond_depth + 1 >= cond_states_cap) {
1225 	    /*
1226 	     * This is rare, but not impossible.
1227 	     * In meta mode, dirdeps.mk (only runs at level 0)
1228 	     * can need more than the default.
1229 	     */
1230 	    cond_states_cap += 32;
1231 	    cond_states = bmake_realloc(cond_states,
1232 					cond_states_cap * sizeof *cond_states);
1233 	}
1234 	state = cond_states[cond_depth];
1235 	cond_depth++;
1236 	if (!(state & IFS_ACTIVE)) {
1237 	    /* If we aren't parsing the data, treat as always false */
1238 	    cond_states[cond_depth] = IFS_WAS_ACTIVE;
1239 	    return COND_SKIP;
1240 	}
1241     }
1242 
1243     /* And evaluate the conditional expression */
1244     if (CondEvalExpression(ifp, p, &value, TRUE, TRUE) == COND_INVALID) {
1245 	/* Syntax error in conditional, error message already output. */
1246 	/* Skip everything to matching .endif */
1247 	/* XXX: An extra '.else' is not detected in this case. */
1248 	cond_states[cond_depth] = IFS_WAS_ACTIVE;
1249 	return COND_SKIP;
1250     }
1251 
1252     if (!value) {
1253 	cond_states[cond_depth] = IFS_INITIAL;
1254 	return COND_SKIP;
1255     }
1256     cond_states[cond_depth] = IFS_ACTIVE;
1257     return COND_PARSE;
1258 }
1259 
1260 void
1261 Cond_restore_depth(unsigned int saved_depth)
1262 {
1263     unsigned int open_conds = cond_depth - cond_min_depth;
1264 
1265     if (open_conds != 0 || saved_depth > cond_depth) {
1266 	Parse_Error(PARSE_FATAL, "%u open conditional%s", open_conds,
1267 		    open_conds == 1 ? "" : "s");
1268 	cond_depth = cond_min_depth;
1269     }
1270 
1271     cond_min_depth = saved_depth;
1272 }
1273 
1274 unsigned int
1275 Cond_save_depth(void)
1276 {
1277     unsigned int depth = cond_min_depth;
1278 
1279     cond_min_depth = cond_depth;
1280     return depth;
1281 }
1282