1 /* $OpenBSD: expr.c,v 1.14 2002/04/26 16:15:16 espie Exp $ */
2 /* $NetBSD: expr.c,v 1.7 1995/09/28 05:37:31 tls Exp $ */
5 * Copyright (c) 1989, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Ozan Yigit at York University.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static char sccsid[] = "@(#)expr.c 8.2 (Berkeley) 4/29/95";
41 static char rcsid[] = "$OpenBSD: expr.c,v 1.14 2002/04/26 16:15:16 espie Exp $";
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
49 #include <sys/types.h>
58 * expression evaluator: performs a standard recursive
59 * descent parse to evaluate any expression permissible
60 * within the following grammar:
64 * | lor "?" query ":" query
65 * lor : land { "||" land }
66 * land : bor { "&&" bor }
67 * bor : xor { "|" xor }
68 * xor : band { "^" eqrel }
69 * band : eqrel { "&" eqrel }
70 * eqrel : nerel { ("==" | "!=") nerel }
71 * nerel : shift { ("<" | ">" | "<=" | ">=") shift }
72 * shift : primary { ("<<" | ">>") primary }
73 * primary : term { ("+" | "-") term }
74 * term : exp { ("*" | "/" | "%") exp }
75 * exp : unary { "**" unary }
77 * | ("+" | "-" | "~" | "!") unary
86 * This expression evaluator is lifted from a public-domain
87 * C Pre-Processor included with the DECUS C Compiler distribution.
88 * It is hacked somewhat to be suitable for m4.
90 * Originally by: Mike Lutz
104 static const char *nxtch; /* Parser scan pointer */
105 static const char *where;
107 static int query(int mayeval);
108 static int lor(int mayeval);
109 static int land(int mayeval);
110 static int bor(int mayeval);
111 static int xor(int mayeval);
112 static int band(int mayeval);
113 static int eqrel(int mayeval);
114 static int nerel(int mayeval);
115 static int shift(int mayeval);
116 static int primary(int mayeval);
117 static int term(int mayeval);
118 static int exp(int mayeval);
119 static int unary(int mayeval);
120 static int factor(int mayeval);
121 static int constant(int mayeval);
122 static int num(int mayeval);
123 static int geteqrel(int mayeval);
124 static int skipws(void);
125 static void experr(const char *);
131 static jmp_buf expjump;
135 * ungetch - Put back the last character examined.
136 * getch - return the next character from expr string.
138 #define ungetch() nxtch--
139 #define getch() *nxtch++
142 expr(const char *expbuf)
148 if (setjmp(expjump) != 0)
155 printf("m4: ill-formed expression.\n");
160 * query : lor | lor '?' query ':' query
165 int result, true_val, false_val;
167 result = lor(mayeval);
168 if (skipws() != '?') {
173 true_val = query(result);
175 experr("bad query: missing \":\"");
177 false_val = query(!result);
178 return result ? true_val : false_val;
182 * lor : land { '||' land }
190 while ((c = skipws()) == '|') {
191 if (getch() != '|') {
206 * land : not { '&&' not }
214 while ((c = skipws()) == '&') {
215 if (getch() != '&') {
230 * bor : xor { "|" xor }
238 while ((c = skipws()) == '|') {
251 * xor : band { "^" band }
259 while ((c = skipws()) == '^') {
268 * band : eqrel { "&" eqrel }
276 while ((c = skipws()) == '&') {
289 * eqrel : nerel { ("==" | "!=" ) nerel }
297 while ((c = skipws()) == '!' || c == '=') {
298 if ((cr = getch()) != '=') {
317 * nerel : shift { ("<=" | ">=" | "<" | ">") shift }
325 while ((c = skipws()) == '<' || c == '>') {
326 if ((cr = getch()) != '=') {
333 vl = (cr == '\0') ? (vl < vr) : (vl <= vr);
336 vl = (cr == '\0') ? (vl > vr) : (vl >= vr);
345 * shift : primary { ("<<" | ">>") primary }
352 vl = primary(mayeval);
353 while (((c = skipws()) == '<' || c == '>') && getch() == c) {
354 vr = primary(mayeval);
362 if (c == '<' || c == '>')
369 * primary : term { ("+" | "-") term }
377 while ((c = skipws()) == '+' || c == '-') {
391 * term : exp { ("*" | "/" | "%") exp }
399 while ((c = skipws()) == '*' || c == '/' || c == '%') {
410 errx(1, "division by zero in eval.");
418 errx(1, "modulo zero in eval.");
429 * exp : unary { "**" exp }
437 while ((c = skipws()) == '*') {
438 if (getch() != '*') {
454 * unary : factor | ("+" | "-" | "~" | "!") unary
461 if ((c = skipws()) == '+' || c == '-' || c == '~' || c == '!') {
462 val = unary(mayeval);
477 return factor(mayeval);
481 * factor : constant | '(' query ')'
488 if (skipws() == '(') {
489 val = query(mayeval);
491 experr("bad factor: missing \")\"");
496 return constant(mayeval);
500 * constant: num | 'char'
501 * Note: constant() handles multi-byte constants
504 constant(int mayeval)
511 if (skipws() != '\'') {
515 for (i = 0; i < (ssize_t)sizeof(int); i++) {
516 if ((c = getch()) == '\'') {
521 switch (c = getch()) {
552 if (i == 0 || getch() != '\'')
553 experr("illegal character constant");
554 for (value = 0; --i >= 0;) {
562 * num : digit | num digit
575 if (c == 'x' || c == 'X') {
590 case '0': case '1': case '2': case '3':
591 case '4': case '5': case '6': case '7':
595 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
597 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
600 rval += c - 'a' + 10;
614 experr("bad constant");
620 * Skip over any white space and return terminating char.
627 while ((c = getch()) <= ' ' && c > EOS)
633 * resets environment to eval(), prints an error
634 * and forces eval to return FALSE.
637 experr(const char *msg)
639 printf("m4: %s in expr %s.\n", msg, where);
640 longjmp(expjump, -1);