Home | History | Annotate | Line # | Download | only in expr
expr.y revision 1.39.12.1
      1  1.39.12.1  pgoyette /* $NetBSD: expr.y,v 1.39.12.1 2018/06/25 07:25:04 pgoyette Exp $ */
      2       1.17  jdolecek 
      3       1.17  jdolecek /*_
      4       1.17  jdolecek  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5       1.17  jdolecek  * All rights reserved.
      6        1.2       cgd  *
      7       1.17  jdolecek  * This code is derived from software contributed to The NetBSD Foundation
      8       1.29     grant  * by Jaromir Dolecek <jdolecek (at) NetBSD.org> and J.T. Conklin <jtc (at) NetBSD.org>.
      9       1.11       jtc  *
     10       1.17  jdolecek  * Redistribution and use in source and binary forms, with or without
     11       1.17  jdolecek  * modification, are permitted provided that the following conditions
     12       1.17  jdolecek  * are met:
     13       1.17  jdolecek  * 1. Redistributions of source code must retain the above copyright
     14       1.17  jdolecek  *    notice, this list of conditions and the following disclaimer.
     15       1.17  jdolecek  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.17  jdolecek  *    notice, this list of conditions and the following disclaimer in the
     17       1.17  jdolecek  *    documentation and/or other materials provided with the distribution.
     18       1.34    martin  *
     19       1.34    martin  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.34    martin  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.34    martin  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.34    martin  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.34    martin  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.34    martin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.34    martin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.34    martin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.34    martin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.34    martin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.34    martin  * POSSIBILITY OF SUCH DAMAGE.
     30        1.1       cgd  */
     31       1.12       jtc 
     32       1.17  jdolecek %{
     33       1.17  jdolecek #include <sys/cdefs.h>
     34       1.17  jdolecek #ifndef lint
     35  1.39.12.1  pgoyette __RCSID("$NetBSD: expr.y,v 1.39.12.1 2018/06/25 07:25:04 pgoyette Exp $");
     36       1.17  jdolecek #endif /* not lint */
     37       1.17  jdolecek 
     38       1.17  jdolecek #include <sys/types.h>
     39       1.28       wiz 
     40       1.17  jdolecek #include <err.h>
     41       1.17  jdolecek #include <errno.h>
     42       1.17  jdolecek #include <limits.h>
     43       1.17  jdolecek #include <locale.h>
     44       1.17  jdolecek #include <regex.h>
     45       1.17  jdolecek #include <stdarg.h>
     46        1.1       cgd #include <stdio.h>
     47        1.4       cgd #include <stdlib.h>
     48        1.4       cgd #include <string.h>
     49        1.1       cgd 
     50       1.23  jdolecek static const char * const *av;
     51        1.1       cgd 
     52       1.37     joerg static void yyerror(const char *, ...) __dead;
     53       1.17  jdolecek static int yylex(void);
     54       1.17  jdolecek static int is_zero_or_null(const char *);
     55       1.17  jdolecek static int is_integer(const char *);
     56       1.26       jmc static int64_t perform_arith_op(const char *, const char *, const char *);
     57       1.26       jmc 
     58       1.17  jdolecek #define YYSTYPE	const char *
     59        1.1       cgd 
     60       1.17  jdolecek %}
     61       1.20  jdolecek %token STRING
     62       1.22   thorpej %left SPEC_OR
     63       1.22   thorpej %left SPEC_AND
     64       1.26       jmc %left COMPARE
     65       1.26       jmc %left ADD_SUB_OPERATOR
     66       1.26       jmc %left MUL_DIV_MOD_OPERATOR
     67       1.26       jmc %left SPEC_REG
     68       1.31  jdolecek %left LENGTH
     69       1.17  jdolecek %left LEFT_PARENT RIGHT_PARENT
     70        1.1       cgd 
     71        1.1       cgd %%
     72        1.1       cgd 
     73       1.17  jdolecek exp:	expr = {
     74       1.17  jdolecek 		(void) printf("%s\n", $1);
     75       1.17  jdolecek 		return (is_zero_or_null($1));
     76       1.17  jdolecek 		}
     77        1.1       cgd 	;
     78        1.1       cgd 
     79       1.23  jdolecek expr:	item { $$ = $1; }
     80       1.17  jdolecek 	| expr SPEC_OR expr = {
     81       1.17  jdolecek 		/*
     82       1.17  jdolecek 		 * Return evaluation of first expression if it is neither
     83       1.17  jdolecek 		 * an empty string nor zero; otherwise, returns the evaluation
     84       1.17  jdolecek 		 * of second expression.
     85       1.17  jdolecek 		 */
     86       1.17  jdolecek 		if (!is_zero_or_null($1))
     87       1.17  jdolecek 			$$ = $1;
     88       1.17  jdolecek 		else
     89       1.17  jdolecek 			$$ = $3;
     90       1.17  jdolecek 		}
     91       1.17  jdolecek 	| expr SPEC_AND expr = {
     92       1.17  jdolecek 		/*
     93       1.17  jdolecek 		 * Returns the evaluation of first expr if neither expression
     94       1.17  jdolecek 		 * evaluates to an empty string or zero; otherwise, returns
     95       1.17  jdolecek 		 * zero.
     96       1.17  jdolecek 		 */
     97       1.17  jdolecek 		if (!is_zero_or_null($1) && !is_zero_or_null($3))
     98       1.17  jdolecek 			$$ = $1;
     99       1.17  jdolecek 		else
    100       1.17  jdolecek 			$$ = "0";
    101       1.17  jdolecek 		}
    102       1.17  jdolecek 	| expr SPEC_REG expr = {
    103       1.17  jdolecek 		/*
    104       1.17  jdolecek 		 * The ``:'' operator matches first expr against the second,
    105       1.17  jdolecek 		 * which must be a regular expression.
    106       1.17  jdolecek 		 */
    107       1.17  jdolecek 		regex_t rp;
    108       1.17  jdolecek 		regmatch_t rm[2];
    109       1.17  jdolecek 		int eval;
    110       1.17  jdolecek 
    111       1.17  jdolecek 		/* compile regular expression */
    112       1.30  jdolecek 		if ((eval = regcomp(&rp, $3, REG_BASIC)) != 0) {
    113       1.18  jdolecek 			char errbuf[256];
    114       1.17  jdolecek 			(void)regerror(eval, &rp, errbuf, sizeof(errbuf));
    115       1.17  jdolecek 			yyerror("%s", errbuf);
    116       1.17  jdolecek 			/* NOT REACHED */
    117        1.1       cgd 		}
    118        1.1       cgd 
    119       1.17  jdolecek 		/* compare string against pattern --  remember that patterns
    120       1.17  jdolecek 		   are anchored to the beginning of the line */
    121       1.17  jdolecek 		if (regexec(&rp, $1, 2, rm, 0) == 0 && rm[0].rm_so == 0) {
    122       1.17  jdolecek 			char *val;
    123       1.17  jdolecek 			if (rm[1].rm_so >= 0) {
    124       1.19  jdolecek 				(void) asprintf(&val, "%.*s",
    125       1.18  jdolecek 					(int) (rm[1].rm_eo - rm[1].rm_so),
    126       1.18  jdolecek 					$1 + rm[1].rm_so);
    127       1.17  jdolecek 			} else {
    128       1.17  jdolecek 				(void) asprintf(&val, "%d",
    129       1.17  jdolecek 					(int)(rm[0].rm_eo - rm[0].rm_so));
    130       1.17  jdolecek 			}
    131       1.33    rumble 			if (val == NULL)
    132       1.33    rumble 				err(1, NULL);
    133       1.17  jdolecek 			$$ = val;
    134       1.17  jdolecek 		} else {
    135       1.17  jdolecek 			if (rp.re_nsub == 0) {
    136       1.17  jdolecek 				$$ = "0";
    137       1.17  jdolecek 			} else {
    138       1.17  jdolecek 				$$ = "";
    139       1.17  jdolecek 			}
    140       1.17  jdolecek 		}
    141        1.1       cgd 
    142       1.17  jdolecek 		}
    143       1.26       jmc 	| expr ADD_SUB_OPERATOR expr = {
    144       1.26       jmc 		/* Returns the results of addition, subtraction */
    145       1.26       jmc 		char *val;
    146       1.26       jmc 		int64_t res;
    147       1.26       jmc 
    148       1.26       jmc 		res = perform_arith_op($1, $2, $3);
    149       1.26       jmc 		(void) asprintf(&val, "%lld", (long long int) res);
    150       1.33    rumble 		if (val == NULL)
    151       1.33    rumble 			err(1, NULL);
    152       1.26       jmc 		$$ = val;
    153       1.26       jmc                 }
    154       1.26       jmc 
    155       1.26       jmc 	| expr MUL_DIV_MOD_OPERATOR expr = {
    156       1.26       jmc 		/*
    157       1.26       jmc 		 * Returns the results of multiply, divide or remainder of
    158       1.26       jmc 		 * numeric-valued arguments.
    159       1.17  jdolecek 		 */
    160       1.17  jdolecek 		char *val;
    161       1.26       jmc 		int64_t res;
    162        1.4       cgd 
    163       1.26       jmc 		res = perform_arith_op($1, $2, $3);
    164       1.17  jdolecek 		(void) asprintf(&val, "%lld", (long long int) res);
    165       1.33    rumble 		if (val == NULL)
    166       1.33    rumble 			err(1, NULL);
    167       1.17  jdolecek 		$$ = val;
    168        1.1       cgd 
    169       1.17  jdolecek 		}
    170       1.17  jdolecek 	| expr COMPARE expr = {
    171       1.17  jdolecek 		/*
    172       1.17  jdolecek 		 * Returns the results of integer comparison if both arguments
    173       1.17  jdolecek 		 * are integers; otherwise, returns the results of string
    174       1.17  jdolecek 		 * comparison using the locale-specific collation sequence.
    175       1.17  jdolecek 		 * The result of each comparison is 1 if the specified relation
    176       1.17  jdolecek 		 * is true, or 0 if the relation is false.
    177       1.17  jdolecek 		 */
    178       1.17  jdolecek 
    179       1.17  jdolecek 		int64_t l, r;
    180       1.17  jdolecek 		int res;
    181       1.17  jdolecek 
    182       1.32     lukem 		res = 0;
    183       1.32     lukem 
    184       1.17  jdolecek 		/*
    185       1.17  jdolecek 		 * Slight hack to avoid differences in the compare code
    186       1.17  jdolecek 		 * between string and numeric compare.
    187       1.17  jdolecek 		 */
    188       1.17  jdolecek 		if (is_integer($1) && is_integer($3)) {
    189       1.17  jdolecek 			/* numeric comparison */
    190       1.17  jdolecek 			l = strtoll($1, NULL, 10);
    191       1.17  jdolecek 			r = strtoll($3, NULL, 10);
    192       1.17  jdolecek 		} else {
    193       1.17  jdolecek 			/* string comparison */
    194       1.17  jdolecek 			l = strcoll($1, $3);
    195       1.17  jdolecek 			r = 0;
    196       1.17  jdolecek 		}
    197        1.4       cgd 
    198       1.17  jdolecek 		switch($2[0]) {
    199       1.17  jdolecek 		case '=': /* equal */
    200       1.17  jdolecek 			res = (l == r);
    201       1.17  jdolecek 			break;
    202       1.17  jdolecek 		case '>': /* greater or greater-equal */
    203       1.17  jdolecek 			if ($2[1] == '=')
    204       1.17  jdolecek 				res = (l >= r);
    205       1.17  jdolecek 			else
    206       1.17  jdolecek 				res = (l > r);
    207       1.17  jdolecek 			break;
    208       1.17  jdolecek 		case '<': /* lower or lower-equal */
    209       1.17  jdolecek 			if ($2[1] == '=')
    210       1.17  jdolecek 				res = (l <= r);
    211       1.17  jdolecek 			else
    212       1.17  jdolecek 				res = (l < r);
    213       1.17  jdolecek 			break;
    214       1.17  jdolecek 		case '!': /* not equal */
    215       1.17  jdolecek 			/* the check if this is != was done in yylex() */
    216       1.17  jdolecek 			res = (l != r);
    217       1.17  jdolecek 		}
    218        1.1       cgd 
    219       1.17  jdolecek 		$$ = (res) ? "1" : "0";
    220        1.4       cgd 
    221       1.17  jdolecek 		}
    222       1.17  jdolecek 	| LEFT_PARENT expr RIGHT_PARENT { $$ = $2; }
    223       1.31  jdolecek 	| LENGTH expr {
    224       1.31  jdolecek 		/*
    225       1.31  jdolecek 		 * Return length of 'expr' in bytes.
    226       1.31  jdolecek 		 */
    227       1.31  jdolecek 		char *ln;
    228       1.31  jdolecek 
    229       1.31  jdolecek 		asprintf(&ln, "%ld", (long) strlen($2));
    230       1.33    rumble 		if (ln == NULL)
    231       1.33    rumble 			err(1, NULL);
    232       1.31  jdolecek 		$$ = ln;
    233       1.31  jdolecek 		}
    234       1.17  jdolecek 	;
    235        1.4       cgd 
    236       1.17  jdolecek item:	STRING
    237       1.26       jmc 	| ADD_SUB_OPERATOR
    238       1.26       jmc 	| MUL_DIV_MOD_OPERATOR
    239       1.17  jdolecek 	| COMPARE
    240       1.17  jdolecek 	| SPEC_OR
    241       1.17  jdolecek 	| SPEC_AND
    242       1.17  jdolecek 	| SPEC_REG
    243       1.31  jdolecek 	| LENGTH
    244       1.17  jdolecek 	;
    245       1.17  jdolecek %%
    246        1.1       cgd 
    247       1.17  jdolecek /*
    248       1.17  jdolecek  * Returns 1 if the string is empty or contains only numeric zero.
    249       1.17  jdolecek  */
    250       1.17  jdolecek static int
    251       1.17  jdolecek is_zero_or_null(const char *str)
    252        1.1       cgd {
    253       1.17  jdolecek 	char *endptr;
    254        1.4       cgd 
    255       1.17  jdolecek 	return str[0] == '\0'
    256       1.17  jdolecek 		|| ( strtoll(str, &endptr, 10) == 0LL
    257       1.17  jdolecek 			&& endptr[0] == '\0');
    258        1.1       cgd }
    259        1.1       cgd 
    260       1.17  jdolecek /*
    261       1.17  jdolecek  * Returns 1 if the string is an integer.
    262       1.17  jdolecek  */
    263       1.17  jdolecek static int
    264       1.17  jdolecek is_integer(const char *str)
    265        1.1       cgd {
    266       1.17  jdolecek 	char *endptr;
    267        1.4       cgd 
    268       1.17  jdolecek 	(void) strtoll(str, &endptr, 10);
    269       1.17  jdolecek 	/* note we treat empty string as valid number */
    270       1.17  jdolecek 	return (endptr[0] == '\0');
    271        1.1       cgd }
    272        1.1       cgd 
    273       1.26       jmc static int64_t
    274       1.26       jmc perform_arith_op(const char *left, const char *op, const char *right)
    275       1.26       jmc {
    276  1.39.12.1  pgoyette 	int64_t res, l, r;
    277       1.26       jmc 
    278       1.32     lukem 	res = 0;
    279       1.32     lukem 
    280       1.26       jmc 	if (!is_integer(left)) {
    281       1.26       jmc 		yyerror("non-integer argument '%s'", left);
    282       1.26       jmc 		/* NOTREACHED */
    283       1.26       jmc 	}
    284       1.26       jmc 	if (!is_integer(right)) {
    285       1.26       jmc 		yyerror("non-integer argument '%s'", right);
    286       1.26       jmc 		/* NOTREACHED */
    287       1.26       jmc 	}
    288       1.26       jmc 
    289       1.26       jmc 	errno = 0;
    290       1.26       jmc 	l = strtoll(left, NULL, 10);
    291       1.26       jmc 	if (errno == ERANGE) {
    292       1.26       jmc 		yyerror("value '%s' is %s is %lld", left,
    293       1.26       jmc 		    (l > 0) ? "too big, maximum" : "too small, minimum",
    294       1.26       jmc 		    (l > 0) ? LLONG_MAX : LLONG_MIN);
    295       1.26       jmc 		/* NOTREACHED */
    296       1.26       jmc 	}
    297       1.26       jmc 
    298       1.26       jmc 	errno = 0;
    299       1.26       jmc 	r = strtoll(right, NULL, 10);
    300       1.26       jmc 	if (errno == ERANGE) {
    301       1.26       jmc 		yyerror("value '%s' is %s is %lld", right,
    302       1.26       jmc 		    (l > 0) ? "too big, maximum" : "too small, minimum",
    303       1.26       jmc 	  	    (l > 0) ? LLONG_MAX : LLONG_MIN);
    304       1.26       jmc 		/* NOTREACHED */
    305       1.26       jmc 	}
    306       1.26       jmc 
    307       1.26       jmc 	switch(op[0]) {
    308       1.26       jmc 	case '+':
    309  1.39.12.1  pgoyette 		/*
    310  1.39.12.1  pgoyette 		 * Check for over-& underflow.
    311       1.27       jmc 		 */
    312  1.39.12.1  pgoyette 		if ((l >= 0 && r <= INT64_MAX - l) ||
    313  1.39.12.1  pgoyette 		    (l <= 0 && r >= INT64_MIN - l)) {
    314  1.39.12.1  pgoyette 			res = l + r;
    315  1.39.12.1  pgoyette 		} else {
    316       1.26       jmc 			yyerror("integer overflow or underflow occurred for "
    317       1.26       jmc                             "operation '%s %s %s'", left, op, right);
    318  1.39.12.1  pgoyette 		}
    319       1.26       jmc 		break;
    320       1.26       jmc 	case '-':
    321  1.39.12.1  pgoyette 		/*
    322  1.39.12.1  pgoyette 		 * Check for over-& underflow.
    323       1.27       jmc 		 */
    324  1.39.12.1  pgoyette 		if ((r > 0 && l < INT64_MIN + r) ||
    325  1.39.12.1  pgoyette 		    (r < 0 && l > INT64_MAX + r)) {
    326       1.26       jmc 			yyerror("integer overflow or underflow occurred for "
    327       1.26       jmc 			    "operation '%s %s %s'", left, op, right);
    328  1.39.12.1  pgoyette 		} else {
    329  1.39.12.1  pgoyette 			res = l - r;
    330  1.39.12.1  pgoyette 		}
    331       1.26       jmc 		break;
    332       1.26       jmc 	case '/':
    333  1.39.12.1  pgoyette 		if (r == 0)
    334       1.26       jmc 			yyerror("second argument to '%s' must not be zero", op);
    335  1.39.12.1  pgoyette 		if (l == INT64_MIN && r == -1)
    336  1.39.12.1  pgoyette 			yyerror("integer overflow or underflow occurred for "
    337  1.39.12.1  pgoyette 			    "operation '%s %s %s'", left, op, right);
    338       1.26       jmc 		res = l / r;
    339       1.26       jmc 
    340       1.26       jmc 		break;
    341       1.26       jmc 	case '%':
    342       1.27       jmc 		if (r == 0)
    343       1.26       jmc 			yyerror("second argument to '%s' must not be zero", op);
    344  1.39.12.1  pgoyette 		if (l == INT64_MIN && r == -1)
    345  1.39.12.1  pgoyette 			yyerror("integer overflow or underflow occurred for "
    346  1.39.12.1  pgoyette 			    "operation '%s %s %s'", left, op, right);
    347       1.26       jmc 		res = l % r;
    348       1.26       jmc 		break;
    349       1.26       jmc 	case '*':
    350       1.27       jmc 		/*
    351  1.39.12.1  pgoyette 		 * Check for over-& underflow.
    352       1.27       jmc 		 */
    353  1.39.12.1  pgoyette 
    354  1.39.12.1  pgoyette 		/* Simplify the conditions */
    355  1.39.12.1  pgoyette 		if (l < 0 && r < 0 && l != INT64_MIN && r != INT64_MIN) {
    356  1.39.12.1  pgoyette 			l = -l;
    357  1.39.12.1  pgoyette 			r = -r;
    358  1.39.12.1  pgoyette 		}
    359  1.39.12.1  pgoyette 
    360  1.39.12.1  pgoyette 		if ((l < 0 && r < 0) ||
    361  1.39.12.1  pgoyette 		    ((l != 0 && r != 0) &&
    362  1.39.12.1  pgoyette 		     (((l > 0 && r > 0) && (l > INT64_MAX / r)) ||
    363  1.39.12.1  pgoyette 		     ((((l < 0 && r > 0) || (l > 0 && r < 0)) &&
    364  1.39.12.1  pgoyette 		      (r != -1 && (l < INT64_MIN / r))))))) {
    365       1.27       jmc 			yyerror("integer overflow or underflow occurred for "
    366       1.27       jmc 			    "operation '%s %s %s'", left, op, right);
    367       1.27       jmc 			/* NOTREACHED */
    368  1.39.12.1  pgoyette 		} else {
    369  1.39.12.1  pgoyette 			res = l * r;
    370  1.39.12.1  pgoyette 		}
    371       1.26       jmc 		break;
    372       1.26       jmc 	}
    373       1.26       jmc 	return res;
    374       1.26       jmc }
    375        1.4       cgd 
    376       1.23  jdolecek static const char *x = "|&=<>+-*/%:()";
    377       1.23  jdolecek static const int x_token[] = {
    378       1.26       jmc 	SPEC_OR, SPEC_AND, COMPARE, COMPARE, COMPARE, ADD_SUB_OPERATOR,
    379       1.26       jmc 	ADD_SUB_OPERATOR, MUL_DIV_MOD_OPERATOR, MUL_DIV_MOD_OPERATOR,
    380       1.26       jmc 	MUL_DIV_MOD_OPERATOR, SPEC_REG, LEFT_PARENT, RIGHT_PARENT
    381       1.17  jdolecek };
    382        1.1       cgd 
    383       1.23  jdolecek static int handle_ddash = 1;
    384       1.23  jdolecek 
    385       1.17  jdolecek int
    386       1.17  jdolecek yylex(void)
    387        1.1       cgd {
    388       1.17  jdolecek 	const char *p = *av++;
    389       1.23  jdolecek 	int retval;
    390        1.4       cgd 
    391       1.23  jdolecek 	if (!p)
    392       1.23  jdolecek 		retval = 0;
    393       1.23  jdolecek 	else if (p[1] == '\0') {
    394       1.17  jdolecek 		const char *w = strchr(x, p[0]);
    395       1.17  jdolecek 		if (w) {
    396       1.17  jdolecek 			retval = x_token[w-x];
    397       1.17  jdolecek 		} else {
    398       1.17  jdolecek 			retval = STRING;
    399       1.17  jdolecek 		}
    400       1.17  jdolecek 	} else if (p[1] == '=' && p[2] == '\0'
    401       1.17  jdolecek 			&& (p[0] == '>' || p[0] == '<' || p[0] == '!'))
    402       1.17  jdolecek 		retval = COMPARE;
    403       1.23  jdolecek 	else if (handle_ddash && p[0] == '-' && p[1] == '-' && p[2] == '\0') {
    404       1.23  jdolecek 		/* ignore "--" if passed as first argument and isn't followed
    405       1.23  jdolecek 		 * by another STRING */
    406       1.23  jdolecek 		retval = yylex();
    407       1.23  jdolecek 		if (retval != STRING && retval != LEFT_PARENT
    408       1.23  jdolecek 		    && retval != RIGHT_PARENT) {
    409       1.23  jdolecek 			/* is not followed by string or parenthesis, use as
    410       1.23  jdolecek 			 * STRING */
    411       1.23  jdolecek 			retval = STRING;
    412       1.23  jdolecek 			av--;	/* was increased in call to yylex() above */
    413       1.23  jdolecek 			p = "--";
    414       1.23  jdolecek 		} else {
    415       1.23  jdolecek 			/* "--" is to be ignored */
    416       1.23  jdolecek 			p = yylval;
    417       1.23  jdolecek 		}
    418       1.31  jdolecek 	} else if (strcmp(p, "length") == 0)
    419       1.31  jdolecek 		retval = LENGTH;
    420       1.31  jdolecek 	else
    421       1.17  jdolecek 		retval = STRING;
    422        1.1       cgd 
    423       1.23  jdolecek 	handle_ddash = 0;
    424       1.17  jdolecek 	yylval = p;
    425        1.4       cgd 
    426       1.17  jdolecek 	return retval;
    427        1.1       cgd }
    428        1.4       cgd 
    429       1.17  jdolecek /*
    430       1.17  jdolecek  * Print error message and exit with error 2 (syntax error).
    431       1.17  jdolecek  */
    432       1.38     joerg static __printflike(1, 2) void
    433       1.17  jdolecek yyerror(const char *fmt, ...)
    434        1.1       cgd {
    435       1.17  jdolecek 	va_list arg;
    436        1.4       cgd 
    437       1.17  jdolecek 	va_start(arg, fmt);
    438       1.17  jdolecek 	verrx(2, fmt, arg);
    439       1.17  jdolecek 	va_end(arg);
    440        1.1       cgd }
    441        1.1       cgd 
    442       1.17  jdolecek int
    443       1.23  jdolecek main(int argc, const char * const *argv)
    444        1.1       cgd {
    445       1.28       wiz 	setprogname(argv[0]);
    446       1.28       wiz 	(void)setlocale(LC_ALL, "");
    447        1.6       jtc 
    448       1.36     joerg 	if (argc == 1) {
    449       1.36     joerg 		(void)fprintf(stderr, "usage: %s expression\n",
    450       1.36     joerg 		    getprogname());
    451       1.36     joerg 		exit(2);
    452       1.36     joerg 	}
    453       1.36     joerg 
    454       1.36     joerg 	av = argv + 1;
    455        1.5       cgd 
    456  1.39.12.1  pgoyette 	return yyparse();
    457        1.1       cgd }
    458