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