Annotation of src/usr.bin/make/cond.c, Revision 1.34
1.24 espie 1: /* $OpenPackages$ */
1.34 ! espie 2: /* $OpenBSD: cond.c,v 1.33 2007/03/20 03:50:39 tedu Exp $ */
1.3 millert 3: /* $NetBSD: cond.c,v 1.7 1996/11/06 17:59:02 christos Exp $ */
1.1 deraadt 4:
5: /*
6: * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
7: * Copyright (c) 1988, 1989 by Adam de Boor
8: * Copyright (c) 1989 by Berkeley Softworks
9: * All rights reserved.
10: *
11: * This code is derived from software contributed to Berkeley by
12: * Adam de Boor.
13: *
14: * Redistribution and use in source and binary forms, with or without
15: * modification, are permitted provided that the following conditions
16: * are met:
17: * 1. Redistributions of source code must retain the above copyright
18: * notice, this list of conditions and the following disclaimer.
19: * 2. Redistributions in binary form must reproduce the above copyright
20: * notice, this list of conditions and the following disclaimer in the
21: * documentation and/or other materials provided with the distribution.
1.29 millert 22: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 23: * may be used to endorse or promote products derived from this software
24: * without specific prior written permission.
25: *
26: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36: * SUCH DAMAGE.
37: */
38:
1.25 espie 39: #include <ctype.h>
40: #include <stddef.h>
41: #include <stdio.h>
1.31 espie 42: #include <stdint.h>
1.26 espie 43: #include <stdlib.h>
1.25 espie 44: #include <string.h>
45: #include "config.h"
46: #include "defines.h"
47: #include "dir.h"
48: #include "buf.h"
49: #include "cond.h"
1.28 espie 50: #include "cond_int.h"
51: #include "condhashconsts.h"
1.25 espie 52: #include "error.h"
53: #include "var.h"
54: #include "varname.h"
55: #include "targ.h"
56: #include "lowparse.h"
57: #include "str.h"
58: #include "main.h"
59: #include "gnode.h"
60: #include "lst.h"
1.28 espie 61: #include "ohash.h"
1.1 deraadt 62:
1.24 espie 63:
64: /* The parsing of conditional expressions is based on this grammar:
1.1 deraadt 65: * E -> F || E
66: * E -> F
67: * F -> T && F
68: * F -> T
69: * T -> defined(variable)
70: * T -> make(target)
71: * T -> exists(file)
72: * T -> empty(varspec)
73: * T -> target(name)
74: * T -> symbol
75: * T -> $(varspec) op value
76: * T -> $(varspec) == "string"
77: * T -> $(varspec) != "string"
1.32 espie 78: * T -> "string" == "string"
79: * T -> "string" != "string"
1.1 deraadt 80: * T -> ( E )
81: * T -> ! T
82: * op -> == | != | > | < | >= | <=
83: *
84: * 'symbol' is some other symbol to which the default function (condDefProc)
85: * is applied.
86: *
87: * Tokens are scanned from the 'condExpr' string. The scanner (CondToken)
88: * will return And for '&' and '&&', Or for '|' and '||', Not for '!',
89: * LParen for '(', RParen for ')' and will evaluate the other terminal
90: * symbols, using either the default function or the function given in the
1.25 espie 91: * terminal, and return the result as either true or False.
1.1 deraadt 92: *
1.24 espie 93: * All Non-Terminal functions (CondE, CondF and CondT) return Err on error. */
1.1 deraadt 94: typedef enum {
1.24 espie 95: False = 0, True = 1, And, Or, Not, LParen, RParen, EndOfFile, None, Err
1.1 deraadt 96: } Token;
97:
98: /*-
99: * Structures to handle elegantly the different forms of #if's. The
100: * last two fields are stored in condInvert and condDefProc, respectively.
101: */
1.25 espie 102: static bool CondGetArg(const char **, struct Name *,
103: const char *, bool);
104: static bool CondDoDefined(struct Name *);
105: static bool CondDoMake(struct Name *);
106: static bool CondDoExists(struct Name *);
107: static bool CondDoTarget(struct Name *);
108: static bool CondCvtArg(const char *, double *);
109: static Token CondToken(bool);
110: static Token CondT(bool);
111: static Token CondF(bool);
112: static Token CondE(bool);
113: static Token CondHandleVarSpec(bool);
114: static Token CondHandleDefault(bool);
1.32 espie 115: static Token CondHandleComparison(char *, bool, bool);
116: static Token CondHandleString(bool);
1.24 espie 117: static const char *find_cond(const char *);
118:
1.1 deraadt 119:
1.28 espie 120: struct If {
121: bool isElse; /* true for else forms */
122: bool doNot; /* true for embedded negation */
1.25 espie 123: bool (*defProc)(struct Name *);
1.28 espie 124: /* function to apply */
1.1 deraadt 125: };
126:
1.28 espie 127: static struct If ifs[] = {
128: { false, false, CondDoDefined }, /* if, ifdef */
129: { false, true, CondDoDefined }, /* ifndef */
130: { false, false, CondDoMake }, /* ifmake */
131: { false, true, CondDoMake }, /* ifnmake */
132: { true, false, CondDoDefined }, /* elif, elifdef */
133: { true, true, CondDoDefined }, /* elifndef */
134: { true, false, CondDoMake }, /* elifmake */
135: { true, true, CondDoMake }, /* elifnmake */
136: { true, false, NULL }
137: };
138:
139: #define COND_IF_INDEX 0
140: #define COND_IFDEF_INDEX 0
141: #define COND_IFNDEF_INDEX 1
142: #define COND_IFMAKE_INDEX 2
143: #define COND_IFNMAKE_INDEX 3
144: #define COND_ELIF_INDEX 4
145: #define COND_ELIFDEF_INDEX 4
146: #define COND_ELIFNDEF_INDEX 5
147: #define COND_ELIFMAKE_INDEX 6
148: #define COND_ELIFNMAKE_INDEX 7
149: #define COND_ELSE_INDEX 8
150:
1.25 espie 151: static bool condInvert; /* Invert the default function */
152: static bool (*condDefProc) /* Default function to apply */
1.24 espie 153: (struct Name *);
154: static const char *condExpr; /* The expression to parse */
1.1 deraadt 155: static Token condPushBack=None; /* Single push-back token used in
156: * parsing */
157:
1.24 espie 158: #define MAXIF 30 /* greatest depth of #if'ing */
1.1 deraadt 159:
1.16 espie 160: static struct {
1.25 espie 161: bool value;
1.24 espie 162: unsigned long lineno;
163: const char *filename;
164: } condStack[MAXIF]; /* Stack of conditionals */
165: static int condTop = MAXIF; /* Top-most conditional */
166: static int skipIfLevel=0; /* Depth of skipped conditionals */
1.25 espie 167: static bool skipLine = false; /* Whether the parse module is skipping
1.1 deraadt 168: * lines */
169:
1.24 espie 170: static const char *
1.30 espie 171: find_cond(const char *p)
1.24 espie 172: {
173: for (;;p++) {
174: if (strchr(" \t)&|$", *p) != NULL)
175: return p;
176: }
1.1 deraadt 177: }
1.24 espie 178:
179:
1.1 deraadt 180: /*-
181: *-----------------------------------------------------------------------
182: * CondGetArg --
183: * Find the argument of a built-in function.
184: *
185: * Results:
1.25 espie 186: * true if evaluation went okay
1.1 deraadt 187: *
188: * Side Effects:
1.10 espie 189: * The line pointer is set to point to the closing parenthesis of the
1.24 espie 190: * function call. The argument is filled.
1.1 deraadt 191: *-----------------------------------------------------------------------
192: */
1.25 espie 193: static bool
1.30 espie 194: CondGetArg(const char **linePtr, struct Name *arg, const char *func,
195: bool parens) /* true if arg should be bounded by parens */
1.1 deraadt 196: {
1.24 espie 197: const char *cp;
1.1 deraadt 198:
199: cp = *linePtr;
200: if (parens) {
1.24 espie 201: while (*cp != '(' && *cp != '\0')
1.1 deraadt 202: cp++;
1.24 espie 203: if (*cp == '(')
1.1 deraadt 204: cp++;
205: }
206:
207: if (*cp == '\0') {
1.24 espie 208: /* No arguments whatsoever. Because 'make' and 'defined' aren't really
1.1 deraadt 209: * "reserved words", we don't print a message. I think this is better
210: * than hitting the user with a warning message every time s/he uses
1.24 espie 211: * the word 'make' or 'defined' at the beginning of a symbol... */
212: arg->s = cp;
213: arg->e = cp;
1.25 espie 214: arg->tofree = false;
215: return false;
1.1 deraadt 216: }
217:
1.24 espie 218: while (*cp == ' ' || *cp == '\t')
1.1 deraadt 219: cp++;
220:
221:
1.25 espie 222: cp = VarName_Get(cp, arg, NULL, true, find_cond);
1.1 deraadt 223:
1.24 espie 224: while (*cp == ' ' || *cp == '\t')
1.1 deraadt 225: cp++;
226: if (parens && *cp != ')') {
1.24 espie 227: Parse_Error(PARSE_WARNING, "Missing closing parenthesis for %s()",
1.1 deraadt 228: func);
1.25 espie 229: return false;
1.24 espie 230: } else if (parens)
231: /* Advance pointer past close parenthesis. */
1.1 deraadt 232: cp++;
1.3 millert 233:
1.1 deraadt 234: *linePtr = cp;
1.25 espie 235: return true;
1.1 deraadt 236: }
1.24 espie 237:
1.1 deraadt 238: /*-
239: *-----------------------------------------------------------------------
240: * CondDoDefined --
241: * Handle the 'defined' function for conditionals.
242: *
243: * Results:
1.25 espie 244: * true if the given variable is defined.
1.1 deraadt 245: *-----------------------------------------------------------------------
246: */
1.25 espie 247: static bool
1.30 espie 248: CondDoDefined(struct Name *arg)
1.1 deraadt 249: {
1.34 ! espie 250: return Var_Definedi(arg->s, arg->e);
1.1 deraadt 251: }
1.24 espie 252:
1.1 deraadt 253: /*-
254: *-----------------------------------------------------------------------
255: * CondDoMake --
256: * Handle the 'make' function for conditionals.
257: *
258: * Results:
1.25 espie 259: * true if the given target is being made.
1.1 deraadt 260: *-----------------------------------------------------------------------
261: */
1.25 espie 262: static bool
1.30 espie 263: CondDoMake(struct Name *arg)
1.1 deraadt 264: {
1.24 espie 265: LstNode ln;
1.1 deraadt 266:
1.25 espie 267: for (ln = Lst_First(create); ln != NULL; ln = Lst_Adv(ln)) {
268: char *s = (char *)Lst_Datum(ln);
269: if (Str_Matchi(s, strchr(s, '\0'), arg->s, arg->e))
270: return true;
1.1 deraadt 271: }
1.24 espie 272:
1.25 espie 273: return false;
1.1 deraadt 274: }
1.24 espie 275:
1.1 deraadt 276: /*-
277: *-----------------------------------------------------------------------
278: * CondDoExists --
279: * See if the given file exists.
280: *
281: * Results:
1.25 espie 282: * true if the file exists and false if it does not.
1.1 deraadt 283: *-----------------------------------------------------------------------
284: */
1.25 espie 285: static bool
1.30 espie 286: CondDoExists(struct Name *arg)
1.1 deraadt 287: {
1.25 espie 288: bool result;
1.1 deraadt 289: char *path;
290:
1.25 espie 291: path = Dir_FindFilei(arg->s, arg->e, dirSearchPath);
1.24 espie 292: if (path != NULL) {
1.25 espie 293: result = true;
1.1 deraadt 294: free(path);
295: } else {
1.25 espie 296: result = false;
1.1 deraadt 297: }
1.24 espie 298: return result;
1.1 deraadt 299: }
1.24 espie 300:
1.1 deraadt 301: /*-
302: *-----------------------------------------------------------------------
303: * CondDoTarget --
304: * See if the given node exists and is an actual target.
305: *
306: * Results:
1.25 espie 307: * true if the node exists as a target and false if it does not.
1.1 deraadt 308: *-----------------------------------------------------------------------
309: */
1.25 espie 310: static bool
1.30 espie 311: CondDoTarget(struct Name *arg)
1.1 deraadt 312: {
313: GNode *gn;
314:
1.25 espie 315: gn = Targ_FindNodei(arg->s, arg->e, TARG_NOCREATE);
1.24 espie 316: if (gn != NULL && !OP_NOP(gn->type))
1.25 espie 317: return true;
1.24 espie 318: else
1.25 espie 319: return false;
1.1 deraadt 320: }
321:
1.24 espie 322:
1.1 deraadt 323: /*-
324: *-----------------------------------------------------------------------
325: * CondCvtArg --
326: * Convert the given number into a double. If the number begins
327: * with 0x, it is interpreted as a hexadecimal integer
328: * and converted to a double from there. All other strings just have
329: * strtod called on them.
330: *
331: * Results:
332: * Sets 'value' to double value of string.
333: * Returns true if the string was a valid number, false o.w.
334: *
335: * Side Effects:
336: * Can change 'value' even if string is not a valid number.
337: *-----------------------------------------------------------------------
338: */
1.25 espie 339: static bool
1.30 espie 340: CondCvtArg(const char *str, double *value)
1.1 deraadt 341: {
1.24 espie 342: if (*str == '0' && str[1] == 'x') {
343: long i;
1.1 deraadt 344:
345: for (str += 2, i = 0; *str; str++) {
346: int x;
1.24 espie 347: if (isdigit(*str))
1.1 deraadt 348: x = *str - '0';
1.24 espie 349: else if (isxdigit(*str))
350: x = 10 + *str - isupper(*str) ? 'A' : 'a';
1.1 deraadt 351: else
1.25 espie 352: return false;
1.1 deraadt 353: i = (i << 4) + x;
354: }
355: *value = (double) i;
1.25 espie 356: return true;
1.1 deraadt 357: }
358: else {
359: char *eptr;
360: *value = strtod(str, &eptr);
361: return *eptr == '\0';
362: }
363: }
1.24 espie 364:
365:
366: static Token
1.30 espie 367: CondHandleVarSpec(bool doEval)
1.24 espie 368: {
369: char *lhs;
370: size_t varSpecLen;
1.25 espie 371: bool doFree;
1.24 espie 372:
373: /* Parse the variable spec and skip over it, saving its
374: * value in lhs. */
375: lhs = Var_Parse(condExpr, NULL, doEval,&varSpecLen,&doFree);
376: if (lhs == var_Error)
377: /* Even if !doEval, we still report syntax errors, which
378: * is what getting var_Error back with !doEval means. */
379: return Err;
380: condExpr += varSpecLen;
381:
382: if (!isspace(*condExpr) &&
383: strchr("!=><", *condExpr) == NULL) {
384: BUFFER buf;
385:
386: Buf_Init(&buf, 0);
387:
388: Buf_AddString(&buf, lhs);
389:
390: if (doFree)
391: free(lhs);
392:
393: for (;*condExpr && !isspace(*condExpr); condExpr++)
394: Buf_AddChar(&buf, *condExpr);
395:
1.32 espie 396: lhs = Var_Subst(Buf_Retrieve(&buf), NULL, doEval);
397: Buf_Destroy(&buf);
1.25 espie 398: doFree = true;
1.24 espie 399: }
400:
1.32 espie 401: return CondHandleComparison(lhs, doFree, doEval);
402: }
403:
404: static Token
405: CondHandleString(bool doEval)
406: {
407: char *lhs;
408: const char *begin;
409: BUFFER buf;
410:
411: /* find the extent of the string */
412: begin = ++condExpr;
413: while (*condExpr && *condExpr != '"') {
414: condExpr++;
415: }
416:
417: Buf_Init(&buf, 0);
418: Buf_Addi(&buf, begin, condExpr);
419: if (*condExpr == '"')
420: condExpr++;
421: lhs = Var_Subst(Buf_Retrieve(&buf), NULL, doEval);
422: Buf_Destroy(&buf);
423: return CondHandleComparison(lhs, true, doEval);
424: }
425:
426: static Token
427: CondHandleComparison(char *lhs, bool doFree, bool doEval)
428: {
429: Token t;
430: const char *rhs;
431: const char *op;
432:
433: t = Err;
1.24 espie 434: /* Skip whitespace to get to the operator. */
435: while (isspace(*condExpr))
436: condExpr++;
437:
438: /* Make sure the operator is a valid one. If it isn't a
439: * known relational operator, pretend we got a
440: * != 0 comparison. */
441: op = condExpr;
442: switch (*condExpr) {
443: case '!':
444: case '=':
445: case '<':
446: case '>':
447: if (condExpr[1] == '=')
448: condExpr += 2;
449: else
450: condExpr += 1;
451: break;
452: default:
453: op = "!=";
454: rhs = "0";
455:
456: goto do_compare;
457: }
458: while (isspace(*condExpr))
459: condExpr++;
460: if (*condExpr == '\0') {
461: Parse_Error(PARSE_WARNING,
462: "Missing right-hand-side of operator");
463: goto error;
464: }
465: rhs = condExpr;
466: do_compare:
467: if (*rhs == '"') {
468: /* Doing a string comparison. Only allow == and != for
469: * operators. */
470: char *string;
471: const char *cp;
472: int qt;
473: BUFFER buf;
474:
475: do_string_compare:
476: if ((*op != '!' && *op != '=') || op[1] != '=') {
477: Parse_Error(PARSE_WARNING,
478: "String comparison operator should be either == or !=");
479: goto error;
480: }
481:
482: Buf_Init(&buf, 0);
483: qt = *rhs == '"' ? 1 : 0;
484:
485: for (cp = &rhs[qt];
486: ((qt && *cp != '"') ||
487: (!qt && strchr(" \t)", *cp) == NULL)) &&
488: *cp != '\0';) {
489: if (*cp == '$') {
490: size_t len;
491:
1.25 espie 492: if (Var_ParseBuffer(&buf, cp, NULL, doEval, &len)) {
1.24 espie 493: cp += len;
494: continue;
495: }
496: } else if (*cp == '\\' && cp[1] != '\0')
497: /* Backslash escapes things -- skip over next
498: * character, if it exists. */
499: cp++;
500: Buf_AddChar(&buf, *cp++);
501: }
502:
503: string = Buf_Retrieve(&buf);
504:
505: if (DEBUG(COND))
506: printf("lhs = \"%s\", rhs = \"%s\", op = %.2s\n",
507: lhs, string, op);
508: /* Null-terminate rhs and perform the comparison.
509: * t is set to the result. */
510: if (*op == '=')
511: t = strcmp(lhs, string) ? False : True;
512: else
513: t = strcmp(lhs, string) ? True : False;
514: free(string);
515: if (rhs == condExpr) {
516: if (!qt && *cp == ')')
517: condExpr = cp;
518: else if (*cp == '\0')
519: condExpr = cp;
520: else
521: condExpr = cp + 1;
522: }
523: } else {
524: /* rhs is either a float or an integer. Convert both the
525: * lhs and the rhs to a double and compare the two. */
526: double left, right;
527: char *string;
528:
529: if (!CondCvtArg(lhs, &left))
530: goto do_string_compare;
531: if (*rhs == '$') {
532: size_t len;
1.25 espie 533: bool freeIt;
1.24 espie 534:
535: string = Var_Parse(rhs, NULL, doEval,&len,&freeIt);
536: if (string == var_Error)
537: right = 0.0;
538: else {
539: if (!CondCvtArg(string, &right)) {
540: if (freeIt)
541: free(string);
542: goto do_string_compare;
543: }
544: if (freeIt)
545: free(string);
546: if (rhs == condExpr)
547: condExpr += len;
548: }
549: } else {
550: if (!CondCvtArg(rhs, &right))
551: goto do_string_compare;
552: if (rhs == condExpr) {
553: /* Skip over the right-hand side. */
554: while (!isspace(*condExpr) &&
555: *condExpr != '\0')
556: condExpr++;
557:
558: }
559: }
560:
561: if (DEBUG(COND))
562: printf("left = %f, right = %f, op = %.2s\n", left,
563: right, op);
564: switch (op[0]) {
565: case '!':
566: if (op[1] != '=') {
567: Parse_Error(PARSE_WARNING,
568: "Unknown operator");
569: goto error;
570: }
571: t = left != right ? True : False;
572: break;
573: case '=':
574: if (op[1] != '=') {
575: Parse_Error(PARSE_WARNING,
576: "Unknown operator");
577: goto error;
578: }
579: t = left == right ? True : False;
580: break;
581: case '<':
582: if (op[1] == '=')
583: t = left <= right ? True : False;
584: else
585: t = left < right ? True : False;
586: break;
587: case '>':
588: if (op[1] == '=')
589: t = left >= right ? True : False;
590: else
591: t = left > right ? True : False;
592: break;
593: }
594: }
595: error:
596: if (doFree)
597: free(lhs);
598: return t;
599: }
600:
601: #define S(s) s, sizeof(s)-1
602: static struct operator {
603: const char *s;
604: size_t len;
1.25 espie 605: bool (*proc)(struct Name *);
1.24 espie 606: } ops[] = {
607: {S("defined"), CondDoDefined},
608: {S("make"), CondDoMake},
609: {S("exists"), CondDoExists},
610: {S("target"), CondDoTarget},
611: {NULL, 0, NULL}
612: };
613: static Token
1.30 espie 614: CondHandleDefault(bool doEval)
1.24 espie 615: {
1.25 espie 616: bool t;
617: bool (*evalProc)(struct Name *);
618: bool invert = false;
1.24 espie 619: struct Name arg;
620: size_t arglen;
621:
622: evalProc = NULL;
623: if (strncmp(condExpr, "empty", 5) == 0) {
624: /* Use Var_Parse to parse the spec in parens and return
625: * True if the resulting string is empty. */
626: size_t length;
1.25 espie 627: bool doFree;
1.24 espie 628: char *val;
629:
630: condExpr += 5;
631:
632: for (arglen = 0; condExpr[arglen] != '(' && condExpr[arglen] != '\0';)
633: arglen++;
634:
635: if (condExpr[arglen] != '\0') {
636: val = Var_Parse(&condExpr[arglen - 1], NULL,
637: doEval, &length, &doFree);
638: if (val == var_Error)
639: t = Err;
640: else {
641: /* A variable is empty when it just contains
642: * spaces... 4/15/92, christos */
643: char *p;
1.33 tedu 644: for (p = val; isspace(*p); p++)
1.24 espie 645: continue;
646: t = *p == '\0' ? True : False;
647: }
648: if (doFree)
649: free(val);
650: /* Advance condExpr to beyond the closing ). Note that
651: * we subtract one from arglen + length b/c length
652: * is calculated from condExpr[arglen - 1]. */
653: condExpr += arglen + length - 1;
654: return t;
655: } else
656: condExpr -= 5;
657: } else {
658: struct operator *op;
659:
660: for (op = ops; op != NULL; op++)
661: if (strncmp(condExpr, op->s, op->len) == 0) {
662: condExpr += op->len;
1.25 espie 663: if (CondGetArg(&condExpr, &arg, op->s, true))
1.24 espie 664: evalProc = op->proc;
665: else
666: condExpr -= op->len;
667: break;
668: }
669: }
670: if (evalProc == NULL) {
671: /* The symbol is itself the argument to the default
672: * function. We advance condExpr to the end of the symbol
673: * by hand (the next whitespace, closing paren or
674: * binary operator) and set to invert the evaluation
1.25 espie 675: * function if condInvert is true. */
1.24 espie 676: invert = condInvert;
677: evalProc = condDefProc;
678: /* XXX should we ignore problems now ? */
1.25 espie 679: CondGetArg(&condExpr, &arg, "", false);
1.24 espie 680: }
681:
682: /* Evaluate the argument using the set function. If invert
1.25 espie 683: * is true, we invert the sense of the function. */
1.24 espie 684: t = (!doEval || (*evalProc)(&arg) ?
685: (invert ? False : True) :
686: (invert ? True : False));
1.25 espie 687: VarName_Free(&arg);
1.24 espie 688: return t;
689: }
690:
1.1 deraadt 691: /*-
692: *-----------------------------------------------------------------------
693: * CondToken --
694: * Return the next token from the input.
695: *
696: * Results:
697: * A Token for the next lexical token in the stream.
698: *
699: * Side Effects:
700: * condPushback will be set back to None if it is used.
701: *-----------------------------------------------------------------------
702: */
703: static Token
1.30 espie 704: CondToken(bool doEval)
1.1 deraadt 705: {
706:
1.24 espie 707: if (condPushBack != None) {
708: Token t;
709:
710: t = condPushBack;
711: condPushBack = None;
712: return t;
713: }
714:
715: while (*condExpr == ' ' || *condExpr == '\t')
716: condExpr++;
717: switch (*condExpr) {
718: case '(':
1.1 deraadt 719: condExpr++;
1.24 espie 720: return LParen;
721: case ')':
722: condExpr++;
723: return RParen;
724: case '|':
725: if (condExpr[1] == '|')
1.1 deraadt 726: condExpr++;
1.24 espie 727: condExpr++;
728: return Or;
729: case '&':
730: if (condExpr[1] == '&')
1.1 deraadt 731: condExpr++;
1.24 espie 732: condExpr++;
733: return And;
734: case '!':
735: condExpr++;
736: return Not;
737: case '\n':
738: case '\0':
739: return EndOfFile;
1.32 espie 740: case '"':
741: return CondHandleString(doEval);
1.24 espie 742: case '$':
743: return CondHandleVarSpec(doEval);
744: default:
745: return CondHandleDefault(doEval);
1.1 deraadt 746: }
747: }
1.24 espie 748:
1.1 deraadt 749: /*-
750: *-----------------------------------------------------------------------
751: * CondT --
752: * Parse a single term in the expression. This consists of a terminal
753: * symbol or Not and a terminal symbol (not including the binary
754: * operators):
755: * T -> defined(variable) | make(target) | exists(file) | symbol
756: * T -> ! T | ( E )
757: *
758: * Results:
759: * True, False or Err.
760: *
761: * Side Effects:
762: * Tokens are consumed.
763: *-----------------------------------------------------------------------
764: */
765: static Token
1.30 espie 766: CondT(bool doEval)
1.1 deraadt 767: {
768: Token t;
769:
770: t = CondToken(doEval);
771:
1.24 espie 772: if (t == EndOfFile)
773: /* If we reached the end of the expression, the expression
774: * is malformed... */
1.1 deraadt 775: t = Err;
1.24 espie 776: else if (t == LParen) {
777: /* T -> ( E ). */
1.1 deraadt 778: t = CondE(doEval);
1.24 espie 779: if (t != Err)
780: if (CondToken(doEval) != RParen)
1.1 deraadt 781: t = Err;
782: } else if (t == Not) {
783: t = CondT(doEval);
1.24 espie 784: if (t == True)
1.1 deraadt 785: t = False;
1.24 espie 786: else if (t == False)
1.1 deraadt 787: t = True;
788: }
1.24 espie 789: return t;
1.1 deraadt 790: }
1.24 espie 791:
1.1 deraadt 792: /*-
793: *-----------------------------------------------------------------------
794: * CondF --
795: * Parse a conjunctive factor (nice name, wot?)
796: * F -> T && F | T
797: *
798: * Results:
799: * True, False or Err
800: *
801: * Side Effects:
802: * Tokens are consumed.
803: *-----------------------------------------------------------------------
804: */
805: static Token
1.30 espie 806: CondF(bool doEval)
1.1 deraadt 807: {
808: Token l, o;
809:
810: l = CondT(doEval);
811: if (l != Err) {
812: o = CondToken(doEval);
813:
814: if (o == And) {
1.24 espie 815: /* F -> T && F
1.1 deraadt 816: *
817: * If T is False, the whole thing will be False, but we have to
818: * parse the r.h.s. anyway (to throw it away).
1.24 espie 819: * If T is True, the result is the r.h.s., be it an Err or no. */
820: if (l == True)
1.1 deraadt 821: l = CondF(doEval);
1.24 espie 822: else
1.25 espie 823: (void)CondF(false);
1.24 espie 824: } else
825: /* F -> T. */
826: condPushBack = o;
1.1 deraadt 827: }
1.24 espie 828: return l;
1.1 deraadt 829: }
1.24 espie 830:
1.1 deraadt 831: /*-
832: *-----------------------------------------------------------------------
833: * CondE --
834: * Main expression production.
835: * E -> F || E | F
836: *
837: * Results:
838: * True, False or Err.
839: *
840: * Side Effects:
841: * Tokens are, of course, consumed.
842: *-----------------------------------------------------------------------
843: */
844: static Token
1.30 espie 845: CondE(bool doEval)
1.1 deraadt 846: {
847: Token l, o;
848:
849: l = CondF(doEval);
850: if (l != Err) {
851: o = CondToken(doEval);
852:
853: if (o == Or) {
1.24 espie 854: /* E -> F || E
1.1 deraadt 855: *
856: * A similar thing occurs for ||, except that here we make sure
857: * the l.h.s. is False before we bother to evaluate the r.h.s.
858: * Once again, if l is False, the result is the r.h.s. and once
1.24 espie 859: * again if l is True, we parse the r.h.s. to throw it away. */
860: if (l == False)
1.1 deraadt 861: l = CondE(doEval);
1.24 espie 862: else
1.25 espie 863: (void)CondE(false);
1.24 espie 864: } else
865: /* E -> F. */
866: condPushBack = o;
1.1 deraadt 867: }
1.24 espie 868: return l;
1.1 deraadt 869: }
1.24 espie 870:
1.28 espie 871: /* Evaluate conditional in line.
872: * returns COND_SKIP, COND_PARSE, COND_INVALID, COND_ISFOR, COND_ISINCLUDE,
873: * COND_ISUNDEF.
874: * A conditional line looks like this:
875: * <cond-type> <expr>
1.1 deraadt 876: * where <cond-type> is any of if, ifmake, ifnmake, ifdef,
877: * ifndef, elif, elifmake, elifnmake, elifdef, elifndef
878: * and <expr> consists of &&, ||, !, make(target), defined(variable)
879: * and parenthetical groupings thereof.
880: */
881: int
1.28 espie 882: Cond_Eval(const char *line)
1.1 deraadt 883: {
1.28 espie 884: /* find end of keyword */
885: const char *end;
1.31 espie 886: uint32_t k;
1.28 espie 887: size_t len;
888: struct If *ifp;
889: bool value = false;
890: int level; /* Level at which to report errors. */
1.1 deraadt 891:
892: level = PARSE_FATAL;
893:
1.28 espie 894: for (end = line; islower(*end); end++)
895: ;
896: /* quick path: recognize special targets early on */
897: if (*end == '.' || *end == ':')
898: return COND_INVALID;
899: len = end - line;
900: k = ohash_interval(line, &end);
901: switch(k % MAGICSLOTS2) {
902: case K_COND_IF % MAGICSLOTS2:
903: if (k == K_COND_IF && len == strlen(COND_IF) &&
904: strncmp(line, COND_IF, len) == 0) {
905: ifp = ifs + COND_IF_INDEX;
906: } else
907: return COND_INVALID;
908: break;
909: case K_COND_IFDEF % MAGICSLOTS2:
910: if (k == K_COND_IFDEF && len == strlen(COND_IFDEF) &&
911: strncmp(line, COND_IFDEF, len) == 0) {
912: ifp = ifs + COND_IFDEF_INDEX;
913: } else
914: return COND_INVALID;
915: break;
916: case K_COND_IFNDEF % MAGICSLOTS2:
917: if (k == K_COND_IFNDEF && len == strlen(COND_IFNDEF) &&
918: strncmp(line, COND_IFNDEF, len) == 0) {
919: ifp = ifs + COND_IFNDEF_INDEX;
920: } else
921: return COND_INVALID;
922: break;
923: case K_COND_IFMAKE % MAGICSLOTS2:
924: if (k == K_COND_IFMAKE && len == strlen(COND_IFMAKE) &&
925: strncmp(line, COND_IFMAKE, len) == 0) {
926: ifp = ifs + COND_IFMAKE_INDEX;
927: } else
928: return COND_INVALID;
929: break;
930: case K_COND_IFNMAKE % MAGICSLOTS2:
931: if (k == K_COND_IFNMAKE && len == strlen(COND_IFNMAKE) &&
932: strncmp(line, COND_IFNMAKE, len) == 0) {
933: ifp = ifs + COND_IFNMAKE_INDEX;
934: } else
935: return COND_INVALID;
936: break;
937: case K_COND_ELIF % MAGICSLOTS2:
938: if (k == K_COND_ELIF && len == strlen(COND_ELIF) &&
939: strncmp(line, COND_ELIF, len) == 0) {
940: ifp = ifs + COND_ELIF_INDEX;
941: } else
942: return COND_INVALID;
943: break;
944: case K_COND_ELIFDEF % MAGICSLOTS2:
945: if (k == K_COND_ELIFDEF && len == strlen(COND_ELIFDEF) &&
946: strncmp(line, COND_ELIFDEF, len) == 0) {
947: ifp = ifs + COND_ELIFDEF_INDEX;
948: } else
949: return COND_INVALID;
950: break;
951: case K_COND_ELIFNDEF % MAGICSLOTS2:
952: if (k == K_COND_ELIFNDEF && len == strlen(COND_ELIFNDEF) &&
953: strncmp(line, COND_ELIFNDEF, len) == 0) {
954: ifp = ifs + COND_ELIFNDEF_INDEX;
955: } else
956: return COND_INVALID;
957: break;
958: case K_COND_ELIFMAKE % MAGICSLOTS2:
959: if (k == K_COND_ELIFMAKE && len == strlen(COND_ELIFMAKE) &&
960: strncmp(line, COND_ELIFMAKE, len) == 0) {
961: ifp = ifs + COND_ELIFMAKE_INDEX;
962: } else
963: return COND_INVALID;
964: break;
965: case K_COND_ELIFNMAKE % MAGICSLOTS2:
966: if (k == K_COND_ELIFNMAKE && len == strlen(COND_ELIFNMAKE) &&
967: strncmp(line, COND_ELIFNMAKE, len) == 0) {
968: ifp = ifs + COND_ELIFNMAKE_INDEX;
969: } else
970: return COND_INVALID;
971: break;
972: case K_COND_ELSE % MAGICSLOTS2:
973: /* valid conditional whose value is the inverse
974: * of the previous if we parsed. */
975: if (k == K_COND_ELSE && len == strlen(COND_ELSE) &&
976: strncmp(line, COND_ELSE, len) == 0) {
977: if (condTop == MAXIF) {
978: Parse_Error(level, "if-less else");
979: return COND_INVALID;
980: } else if (skipIfLevel == 0) {
981: value = !condStack[condTop].value;
982: ifp = ifs + COND_ELSE_INDEX;
983: } else
984: return COND_SKIP;
985: } else
986: return COND_INVALID;
987: break;
988: case K_COND_ENDIF % MAGICSLOTS2:
989: if (k == K_COND_ENDIF && len == strlen(COND_ENDIF) &&
990: strncmp(line, COND_ENDIF, len) == 0) {
1.24 espie 991: /* End of a conditional section. If skipIfLevel is non-zero, that
1.1 deraadt 992: * conditional was skipped, so lines following it should also be
993: * skipped. Hence, we return COND_SKIP. Otherwise, the conditional
994: * was read so succeeding lines should be parsed (think about it...)
995: * so we return COND_PARSE, unless this endif isn't paired with
1.24 espie 996: * a decent if. */
1.28 espie 997: if (skipIfLevel != 0) {
998: skipIfLevel -= 1;
999: return COND_SKIP;
1.1 deraadt 1000: } else {
1.28 espie 1001: if (condTop == MAXIF) {
1002: Parse_Error(level, "if-less endif");
1003: return COND_INVALID;
1004: } else {
1005: skipLine = false;
1006: condTop += 1;
1007: return COND_PARSE;
1008: }
1.1 deraadt 1009: }
1.28 espie 1010: } else
1011: return COND_INVALID;
1012: break;
1013: /* Recognize other keywords there, to simplify parser's task */
1014: case K_COND_FOR % MAGICSLOTS2:
1015: if (k == K_COND_FOR && len == strlen(COND_FOR) &&
1016: strncmp(line, COND_FOR, len) == 0)
1017: return COND_ISFOR;
1018: else
1019: return COND_INVALID;
1020: case K_COND_UNDEF % MAGICSLOTS2:
1021: if (k == K_COND_UNDEF && len == strlen(COND_UNDEF) &&
1022: strncmp(line, COND_UNDEF, len) == 0)
1023: return COND_ISUNDEF;
1.34 ! espie 1024: else
! 1025: return COND_INVALID;
! 1026: case K_COND_POISON % MAGICSLOTS2:
! 1027: if (k == K_COND_POISON && len == strlen(COND_POISON) &&
! 1028: strncmp(line, COND_POISON, len) == 0)
! 1029: return COND_ISPOISON;
1.28 espie 1030: else
1031: return COND_INVALID;
1032: case K_COND_INCLUDE % MAGICSLOTS2:
1033: if (k == K_COND_INCLUDE && len == strlen(COND_INCLUDE) &&
1034: strncmp(line, COND_INCLUDE, len) == 0)
1035: return COND_ISINCLUDE;
1036: else
1037: return COND_INVALID;
1038: default:
1039: /* Not a valid conditional type. No error... */
1040: return COND_INVALID;
1.1 deraadt 1041: }
1042:
1.28 espie 1043: if (ifp->isElse) {
1044: if (condTop == MAXIF) {
1045: Parse_Error(level, "if-less elif");
1.24 espie 1046: return COND_INVALID;
1.28 espie 1047: } else if (skipIfLevel != 0) {
1048: /* If skipping this conditional, just ignore the whole thing.
1049: * If we don't, the user might be employing a variable that's
1050: * undefined, for which there's an enclosing ifdef that
1051: * we're skipping... */
1.24 espie 1052: return COND_SKIP;
1.1 deraadt 1053: }
1.28 espie 1054: } else if (skipLine) {
1055: /* Don't even try to evaluate a conditional that's not an else if
1056: * we're skipping things... */
1057: skipIfLevel += 1;
1058: return COND_SKIP;
1059: }
1.1 deraadt 1060:
1.28 espie 1061: if (ifp->defProc) {
1.24 espie 1062: /* Initialize file-global variables for parsing. */
1.1 deraadt 1063: condDefProc = ifp->defProc;
1064: condInvert = ifp->doNot;
1.3 millert 1065:
1.28 espie 1066: line += len;
1.3 millert 1067:
1.24 espie 1068: while (*line == ' ' || *line == '\t')
1.1 deraadt 1069: line++;
1.3 millert 1070:
1.1 deraadt 1071: condExpr = line;
1072: condPushBack = None;
1.3 millert 1073:
1.25 espie 1074: switch (CondE(true)) {
1.1 deraadt 1075: case True:
1.25 espie 1076: if (CondToken(true) == EndOfFile) {
1077: value = true;
1.1 deraadt 1078: break;
1079: }
1080: goto err;
1.20 espie 1081: /* FALLTHROUGH */
1.1 deraadt 1082: case False:
1.25 espie 1083: if (CondToken(true) == EndOfFile) {
1084: value = false;
1.1 deraadt 1085: break;
1086: }
1.20 espie 1087: /* FALLTHROUGH */
1.1 deraadt 1088: case Err:
1089: err:
1.24 espie 1090: Parse_Error(level, "Malformed conditional (%s)", line);
1091: return COND_INVALID;
1.1 deraadt 1092: default:
1093: break;
1094: }
1095: }
1.28 espie 1096:
1097: if (!ifp->isElse)
1.1 deraadt 1098: condTop -= 1;
1.24 espie 1099: else if (skipIfLevel != 0 || condStack[condTop].value) {
1100: /* If this is an else-type conditional, it should only take effect
1.25 espie 1101: * if its corresponding if was evaluated and false. If its if was
1102: * true or skipped, we return COND_SKIP (and start skipping in case
1.1 deraadt 1103: * we weren't already), leaving the stack unmolested so later elif's
1.24 espie 1104: * don't screw up... */
1.25 espie 1105: skipLine = true;
1.24 espie 1106: return COND_SKIP;
1.1 deraadt 1107: }
1108:
1109: if (condTop < 0) {
1.24 espie 1110: /* This is the one case where we can definitely proclaim a fatal
1111: * error. If we don't, we're hosed. */
1112: Parse_Error(PARSE_FATAL, "Too many nested if's. %d max.", MAXIF);
1.27 espie 1113: condTop = 0;
1.24 espie 1114: return COND_INVALID;
1.1 deraadt 1115: } else {
1.16 espie 1116: condStack[condTop].value = value;
1117: condStack[condTop].lineno = Parse_Getlineno();
1118: condStack[condTop].filename = Parse_Getfilename();
1.1 deraadt 1119: skipLine = !value;
1.24 espie 1120: return value ? COND_PARSE : COND_SKIP;
1.1 deraadt 1121: }
1122: }
1.24 espie 1123:
1.1 deraadt 1124: void
1.30 espie 1125: Cond_End(void)
1.1 deraadt 1126: {
1.16 espie 1127: int i;
1128:
1.1 deraadt 1129: if (condTop != MAXIF) {
1.27 espie 1130: Parse_Error(PARSE_FATAL, "%s%d open conditional%s",
1131: condTop == 0 ? "at least ": "", MAXIF-condTop,
1132: MAXIF-condTop == 1 ? "" : "s");
1.16 espie 1133: for (i = MAXIF-1; i >= condTop; i--) {
1134: fprintf(stderr, "\t at line %lu of %s\n", condStack[i].lineno,
1135: condStack[i].filename);
1136: }
1.1 deraadt 1137: }
1138: condTop = MAXIF;
1139: }