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