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