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