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