17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57257d1b4Sraf  * Common Development and Distribution License (the "License").
67257d1b4Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217257d1b4Sraf 
227c478bd9Sstevel@tonic-gate /*
237257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277257d1b4Sraf #include "lint.h"
287c478bd9Sstevel@tonic-gate #include <ctype.h>
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include "libc.h"
327c478bd9Sstevel@tonic-gate #include "gettext.h"
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include "plural_parser.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * 31   28    24    20    16    12     8     4     0
387c478bd9Sstevel@tonic-gate  * +-----+-----+-----+-----+-----+-----+-----+-----+
397c478bd9Sstevel@tonic-gate  * |opnum| priority  |        operator             |
407c478bd9Sstevel@tonic-gate  * +-----+-----+-----+-----+-----+-----+-----+-----+
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate static const unsigned int	operator[] = {
437c478bd9Sstevel@tonic-gate 	0x00000000,		/* NULL */
447c478bd9Sstevel@tonic-gate 	0x00000001,		/* INIT */
457c478bd9Sstevel@tonic-gate 	0x00100002,		/* EXP */
467c478bd9Sstevel@tonic-gate 	0x00200003,		/* NUM */
477c478bd9Sstevel@tonic-gate 	0x00300004,		/* VAR */
487c478bd9Sstevel@tonic-gate 	0x30400005,		/* CONDC */
497c478bd9Sstevel@tonic-gate 	0x30500006,		/* CONDQ */
507c478bd9Sstevel@tonic-gate 	0x20600007,		/* OR */
517c478bd9Sstevel@tonic-gate 	0x20700008,		/* AND */
527c478bd9Sstevel@tonic-gate 	0x20800009,		/* EQ */
537c478bd9Sstevel@tonic-gate 	0x2080000a,		/* NEQ */
547c478bd9Sstevel@tonic-gate 	0x2090000b,		/* GT */
557c478bd9Sstevel@tonic-gate 	0x2090000c,		/* LT */
567c478bd9Sstevel@tonic-gate 	0x2090000d,		/* GE */
577c478bd9Sstevel@tonic-gate 	0x2090000e,		/* LE */
587c478bd9Sstevel@tonic-gate 	0x20a0000f,		/* ADD */
597c478bd9Sstevel@tonic-gate 	0x20a00010,		/* SUB */
607c478bd9Sstevel@tonic-gate 	0x20b00011,		/* MUL */
617c478bd9Sstevel@tonic-gate 	0x20b00012,		/* DIV */
627c478bd9Sstevel@tonic-gate 	0x20b00013,		/* MOD */
637c478bd9Sstevel@tonic-gate 	0x10c00014,		/* NOT */
647c478bd9Sstevel@tonic-gate 	0x00d00015,		/* LPAR */
657c478bd9Sstevel@tonic-gate 	0x00e00016,		/* RPAR */
667c478bd9Sstevel@tonic-gate 	0x00000017		/* ERR */
677c478bd9Sstevel@tonic-gate };
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #define	STACKFREE \
707c478bd9Sstevel@tonic-gate 	{ \
717c478bd9Sstevel@tonic-gate 		while (stk->index > 0) \
727c478bd9Sstevel@tonic-gate 			freeexpr(stk->ptr[--stk->index]); \
737c478bd9Sstevel@tonic-gate 		free(stk->ptr); \
747c478bd9Sstevel@tonic-gate 	}
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #ifdef	PARSE_DEBUG
777c478bd9Sstevel@tonic-gate static const char	*type_name[] = {
787c478bd9Sstevel@tonic-gate 	"T_NULL",
797c478bd9Sstevel@tonic-gate 	"T_INIT", "T_EXP",	"T_NUM", "T_VAR", "T_CONDC", "T_CONDQ",
807c478bd9Sstevel@tonic-gate 	"T_LOR", "T_LAND", "T_EQ", "T_NEQ", "T_GT", "T_LT", "T_GE", "T_LE",
817c478bd9Sstevel@tonic-gate 	"T_ADD", "T_SUB", "T_MUL", "T_DIV", "T_MOD", "T_LNOT", "T_LPAR",
827c478bd9Sstevel@tonic-gate 	"T_RPAR", "T_ERR"
837c478bd9Sstevel@tonic-gate };
847c478bd9Sstevel@tonic-gate #endif
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate static void	freeexpr(struct expr *);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate static struct expr *
stack_push(struct stack * stk,struct expr * exp)897c478bd9Sstevel@tonic-gate stack_push(struct stack *stk, struct expr *exp)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate #ifdef	PARSE_DEBUG
927c478bd9Sstevel@tonic-gate 	printf("--- stack_push ---\n");
937c478bd9Sstevel@tonic-gate 	printf("   type: %s\n", type_name[GETTYPE(exp->op)]);
947c478bd9Sstevel@tonic-gate 	printf("   flag: %s\n", type_name[GETTYPE(exp->flag)]);
957c478bd9Sstevel@tonic-gate 	printf("------------------\n");
967c478bd9Sstevel@tonic-gate #endif
977c478bd9Sstevel@tonic-gate 	stk->ptr[stk->index++] = exp;
987c478bd9Sstevel@tonic-gate 	if (stk->index == MAX_STACK_SIZE) {
997c478bd9Sstevel@tonic-gate 		/* overflow */
1007c478bd9Sstevel@tonic-gate 		freeexpr(exp);
1017c478bd9Sstevel@tonic-gate 		STACKFREE;
1027c478bd9Sstevel@tonic-gate 		return (NULL);
1037c478bd9Sstevel@tonic-gate 	}
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	return (exp);
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate static struct expr *
stack_pop(struct stack * stk,struct expr * exp_a,struct expr * exp_b)109*00ae5933SToomas Soome stack_pop(struct stack *stk, struct expr *exp_a, struct expr *exp_b)
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate 	if (stk->index == 0) {
1127c478bd9Sstevel@tonic-gate 		/* no item */
1137c478bd9Sstevel@tonic-gate 		if (exp_a)
1147c478bd9Sstevel@tonic-gate 			freeexpr(exp_a);
1157c478bd9Sstevel@tonic-gate 		if (exp_b)
1167c478bd9Sstevel@tonic-gate 			freeexpr(exp_b);
1177c478bd9Sstevel@tonic-gate 		STACKFREE;
1187c478bd9Sstevel@tonic-gate 		return (NULL);
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate #ifdef	PARSE_DEBUG
1217c478bd9Sstevel@tonic-gate 	printf("--- stack_pop ---\n");
1227c478bd9Sstevel@tonic-gate 	printf("   type: %s\n",
123*00ae5933SToomas Soome 	    type_name[GETTYPE((stk->ptr[stk->index - 1])->op)]);
1247c478bd9Sstevel@tonic-gate 	printf("   flag: %s\n",
125*00ae5933SToomas Soome 	    type_name[GETTYPE((stk->ptr[stk->index - 1])->flag)]);
1267c478bd9Sstevel@tonic-gate 	printf("-----------------\n");
1277c478bd9Sstevel@tonic-gate #endif
1287c478bd9Sstevel@tonic-gate 	return (stk->ptr[--stk->index]);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate static void
freeexpr(struct expr * e)1327c478bd9Sstevel@tonic-gate freeexpr(struct expr *e)
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate #ifdef	PARSE_DEBUG
1357c478bd9Sstevel@tonic-gate 	printf("--- freeexpr ---\n");
1367c478bd9Sstevel@tonic-gate 	printf("   type: %s\n", type_name[GETTYPE(e->op)]);
1377c478bd9Sstevel@tonic-gate 	printf("----------------\n");
1387c478bd9Sstevel@tonic-gate #endif
1397c478bd9Sstevel@tonic-gate 	switch (GETOPNUM(e->op)) {
1407c478bd9Sstevel@tonic-gate 	case TRINARY:
1417c478bd9Sstevel@tonic-gate 		if (e->nodes[2])
1427c478bd9Sstevel@tonic-gate 			freeexpr(e->nodes[2]);
1437c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
1447c478bd9Sstevel@tonic-gate 	case BINARY:
1457c478bd9Sstevel@tonic-gate 		if (e->nodes[1])
1467c478bd9Sstevel@tonic-gate 			freeexpr(e->nodes[1]);
1477c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
1487c478bd9Sstevel@tonic-gate 	case UNARY:
1497c478bd9Sstevel@tonic-gate 		if (e->nodes[0])
1507c478bd9Sstevel@tonic-gate 			freeexpr(e->nodes[0]);
1517c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
1527c478bd9Sstevel@tonic-gate 	default:
1537c478bd9Sstevel@tonic-gate 		break;
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 	free(e);
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate static struct expr *
setop1(unsigned int op,unsigned int num,struct stack * stk,unsigned int flag)1597c478bd9Sstevel@tonic-gate setop1(unsigned int op, unsigned int num,
160*00ae5933SToomas Soome     struct stack *stk, unsigned int flag)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	struct expr	*newitem;
1637c478bd9Sstevel@tonic-gate 	unsigned int	type;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	type = GETTYPE(op);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate #ifdef	PARSE_DEBUG
1687c478bd9Sstevel@tonic-gate 	printf("---setop1---\n");
1697c478bd9Sstevel@tonic-gate 	printf("   op type: %s\n", type_name[type]);
1707c478bd9Sstevel@tonic-gate 	printf("-----------\n");
1717c478bd9Sstevel@tonic-gate #endif
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	newitem = (struct expr *)calloc(1, sizeof (struct expr));
1747c478bd9Sstevel@tonic-gate 	if (!newitem) {
1757c478bd9Sstevel@tonic-gate 		STACKFREE;
1767c478bd9Sstevel@tonic-gate 		return (NULL);
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 	newitem->op = op;
1797c478bd9Sstevel@tonic-gate 	if (type == T_NUM)
1807c478bd9Sstevel@tonic-gate 		newitem->num = num;
1817c478bd9Sstevel@tonic-gate 	newitem->flag = flag;
1827c478bd9Sstevel@tonic-gate 	return (newitem);
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate static struct expr *
setop_reduce(unsigned int n,unsigned int op,struct stack * stk,struct expr * exp1,struct expr * exp2,struct expr * exp3)1867c478bd9Sstevel@tonic-gate setop_reduce(unsigned int n, unsigned int op, struct stack *stk,
187*00ae5933SToomas Soome     struct expr *exp1, struct expr *exp2, struct expr *exp3)
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate 	struct expr	*newitem;
1907c478bd9Sstevel@tonic-gate #ifdef	PARSE_DEBUG
1917c478bd9Sstevel@tonic-gate 	unsigned int	type;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	type = GETTYPE(op);
1947c478bd9Sstevel@tonic-gate 	printf("---setop_reduce---\n");
1957c478bd9Sstevel@tonic-gate 	printf("   n: %d\n", n);
1967c478bd9Sstevel@tonic-gate 	printf("   op type: %s\n", type_name[type]);
1977c478bd9Sstevel@tonic-gate 	switch (n) {
1987c478bd9Sstevel@tonic-gate 	case TRINARY:
199*00ae5933SToomas Soome 		printf("   exp3 type: %s\n", type_name[GETTYPE(exp3->op)]);
2007c478bd9Sstevel@tonic-gate 	case BINARY:
201*00ae5933SToomas Soome 		printf("   exp2 type: %s\n", type_name[GETTYPE(exp2->op)]);
2027c478bd9Sstevel@tonic-gate 	case UNARY:
203*00ae5933SToomas Soome 		printf("   exp1 type: %s\n", type_name[GETTYPE(exp1->op)]);
2047c478bd9Sstevel@tonic-gate 	case NARY:
2057c478bd9Sstevel@tonic-gate 		break;
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 	printf("-----------\n");
2087c478bd9Sstevel@tonic-gate #endif
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	newitem = (struct expr *)calloc(1, sizeof (struct expr));
2117c478bd9Sstevel@tonic-gate 	if (!newitem) {
2127c478bd9Sstevel@tonic-gate 		if (exp1)
2137c478bd9Sstevel@tonic-gate 			freeexpr(exp1);
2147c478bd9Sstevel@tonic-gate 		if (exp2)
2157c478bd9Sstevel@tonic-gate 			freeexpr(exp2);
2167c478bd9Sstevel@tonic-gate 		if (exp3)
2177c478bd9Sstevel@tonic-gate 			freeexpr(exp3);
2187c478bd9Sstevel@tonic-gate 		STACKFREE;
2197c478bd9Sstevel@tonic-gate 		return (NULL);
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 	newitem->op = op;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	switch (n) {
2247c478bd9Sstevel@tonic-gate 	case TRINARY:
2257c478bd9Sstevel@tonic-gate 		newitem->nodes[2] = exp3;
2267c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
2277c478bd9Sstevel@tonic-gate 	case BINARY:
2287c478bd9Sstevel@tonic-gate 		newitem->nodes[1] = exp2;
2297c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
2307c478bd9Sstevel@tonic-gate 	case UNARY:
2317c478bd9Sstevel@tonic-gate 		newitem->nodes[0] = exp1;
2327c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
2337c478bd9Sstevel@tonic-gate 	case NARY:
2347c478bd9Sstevel@tonic-gate 		break;
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 	return (newitem);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate static int
reduce(struct expr ** nexp,unsigned int n,struct expr * exp,struct stack * stk)2407c478bd9Sstevel@tonic-gate reduce(struct expr **nexp, unsigned int n, struct expr *exp, struct stack *stk)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	struct expr	*exp_op, *exp1, *exp2, *exp3;
2437c478bd9Sstevel@tonic-gate 	unsigned int	tmp_flag;
2447c478bd9Sstevel@tonic-gate 	unsigned int	oop;
2457c478bd9Sstevel@tonic-gate #ifdef	PARSE_DEBUG
2467c478bd9Sstevel@tonic-gate 	printf("---reduce---\n");
2477c478bd9Sstevel@tonic-gate 	printf("   n: %d\n", n);
2487c478bd9Sstevel@tonic-gate 	printf("-----------\n");
2497c478bd9Sstevel@tonic-gate #endif
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	switch (n) {
2527c478bd9Sstevel@tonic-gate 	case UNARY:
2537c478bd9Sstevel@tonic-gate 		/* unary operator */
2547c478bd9Sstevel@tonic-gate 		exp1 = exp;
2557c478bd9Sstevel@tonic-gate 		exp_op = stack_pop(stk, exp1, NULL);
2567c478bd9Sstevel@tonic-gate 		if (!exp_op)
2577c478bd9Sstevel@tonic-gate 			return (1);
2587c478bd9Sstevel@tonic-gate 		tmp_flag = exp_op->flag;
2597c478bd9Sstevel@tonic-gate 		oop = exp_op->op;
2607c478bd9Sstevel@tonic-gate 		freeexpr(exp_op);
2617c478bd9Sstevel@tonic-gate 		*nexp = setop_reduce(UNARY, oop, stk, exp1, NULL, NULL);
2627c478bd9Sstevel@tonic-gate 		if (!*nexp)
2637c478bd9Sstevel@tonic-gate 			return (-1);
2647c478bd9Sstevel@tonic-gate 		(*nexp)->flag = tmp_flag;
2657c478bd9Sstevel@tonic-gate 		return (0);
2667c478bd9Sstevel@tonic-gate 	case BINARY:
2677c478bd9Sstevel@tonic-gate 		/* binary operator */
2687c478bd9Sstevel@tonic-gate 		exp2 = exp;
2697c478bd9Sstevel@tonic-gate 		exp_op = stack_pop(stk, exp2, NULL);
2707c478bd9Sstevel@tonic-gate 		if (!exp_op)
2717c478bd9Sstevel@tonic-gate 			return (1);
2727c478bd9Sstevel@tonic-gate 		exp1 = stack_pop(stk, exp_op, exp2);
2737c478bd9Sstevel@tonic-gate 		if (!exp1)
2747c478bd9Sstevel@tonic-gate 			return (1);
2757c478bd9Sstevel@tonic-gate 		tmp_flag = exp1->flag;
2767c478bd9Sstevel@tonic-gate 		oop = exp_op->op;
2777c478bd9Sstevel@tonic-gate 		freeexpr(exp_op);
2787c478bd9Sstevel@tonic-gate 		*nexp = setop_reduce(BINARY, oop, stk, exp1, exp2, NULL);
2797c478bd9Sstevel@tonic-gate 		if (!*nexp)
2807c478bd9Sstevel@tonic-gate 			return (-1);
2817c478bd9Sstevel@tonic-gate 		(*nexp)->flag = tmp_flag;
2827c478bd9Sstevel@tonic-gate 		return (0);
2837c478bd9Sstevel@tonic-gate 	case TRINARY:
2847c478bd9Sstevel@tonic-gate 		/* trinary operator: conditional */
2857c478bd9Sstevel@tonic-gate 		exp3 = exp;
2867c478bd9Sstevel@tonic-gate 		exp_op = stack_pop(stk, exp3, NULL);
2877c478bd9Sstevel@tonic-gate 		if (!exp_op)
2887c478bd9Sstevel@tonic-gate 			return (1);
2897c478bd9Sstevel@tonic-gate 		freeexpr(exp_op);
2907c478bd9Sstevel@tonic-gate 		exp2 = stack_pop(stk, exp3, NULL);
2917c478bd9Sstevel@tonic-gate 		if (!exp2)
2927c478bd9Sstevel@tonic-gate 			return (1);
2937c478bd9Sstevel@tonic-gate 		exp_op = stack_pop(stk, exp2, exp3);
2947c478bd9Sstevel@tonic-gate 		if (!exp_op)
2957c478bd9Sstevel@tonic-gate 			return (1);
2967c478bd9Sstevel@tonic-gate 		if (GETTYPE(exp_op->op) != T_CONDQ) {
2977c478bd9Sstevel@tonic-gate 			/* parse failed */
2987c478bd9Sstevel@tonic-gate 			freeexpr(exp_op);
2997c478bd9Sstevel@tonic-gate 			freeexpr(exp2);
3007c478bd9Sstevel@tonic-gate 			freeexpr(exp3);
3017c478bd9Sstevel@tonic-gate 			STACKFREE;
3027c478bd9Sstevel@tonic-gate 			return (1);
3037c478bd9Sstevel@tonic-gate 		}
3047c478bd9Sstevel@tonic-gate 		oop = exp_op->op;
3057c478bd9Sstevel@tonic-gate 		freeexpr(exp_op);
3067c478bd9Sstevel@tonic-gate 		exp1 = stack_pop(stk, exp2, exp3);
3077c478bd9Sstevel@tonic-gate 		if (!exp1)
3087c478bd9Sstevel@tonic-gate 			return (1);
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 		tmp_flag = exp1->flag;
3117c478bd9Sstevel@tonic-gate 		*nexp = setop_reduce(TRINARY, oop, stk, exp1, exp2, exp3);
3127c478bd9Sstevel@tonic-gate 		if (!*nexp)
3137c478bd9Sstevel@tonic-gate 			return (-1);
3147c478bd9Sstevel@tonic-gate 		(*nexp)->flag = tmp_flag;
3157c478bd9Sstevel@tonic-gate 		return (0);
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
3187c478bd9Sstevel@tonic-gate 	return (0);	/* keep gcc happy */
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate static unsigned int
gettoken(const char ** pstr,unsigned int * num,int which)3227c478bd9Sstevel@tonic-gate gettoken(const char **pstr, unsigned int *num, int which)
3237c478bd9Sstevel@tonic-gate {
3247c478bd9Sstevel@tonic-gate 	unsigned char	*sp = *(unsigned char **)pstr;
3257c478bd9Sstevel@tonic-gate 	unsigned int	n;
3267c478bd9Sstevel@tonic-gate 	unsigned int	ret;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	while (*sp && ((*sp == ' ') || (*sp == '\t')))
3297c478bd9Sstevel@tonic-gate 		sp++;
3307c478bd9Sstevel@tonic-gate 	if (!*sp) {
3317c478bd9Sstevel@tonic-gate 		if (which == GET_TOKEN)
3327c478bd9Sstevel@tonic-gate 			*pstr = (const char *)sp;
3337c478bd9Sstevel@tonic-gate 		return (T_NULL);
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	if (isdigit(*sp)) {
3377c478bd9Sstevel@tonic-gate 		n = *sp - '0';
3387c478bd9Sstevel@tonic-gate 		sp++;
3397c478bd9Sstevel@tonic-gate 		while (isdigit(*sp)) {
3407c478bd9Sstevel@tonic-gate 			n *= 10;
3417c478bd9Sstevel@tonic-gate 			n += *sp - '0';
3427c478bd9Sstevel@tonic-gate 			sp++;
3437c478bd9Sstevel@tonic-gate 		}
3447c478bd9Sstevel@tonic-gate 		*num = n;
3457c478bd9Sstevel@tonic-gate 		ret = T_NUM;
3467c478bd9Sstevel@tonic-gate 	} else if (*sp == 'n') {
3477c478bd9Sstevel@tonic-gate 		sp++;
3487c478bd9Sstevel@tonic-gate 		ret = T_VAR;
3497c478bd9Sstevel@tonic-gate 	} else if (*sp == '(') {
3507c478bd9Sstevel@tonic-gate 		sp++;
3517c478bd9Sstevel@tonic-gate 		ret = T_LPAR;
3527c478bd9Sstevel@tonic-gate 	} else if (*sp == ')') {
3537c478bd9Sstevel@tonic-gate 		sp++;
3547c478bd9Sstevel@tonic-gate 		ret = T_RPAR;
3557c478bd9Sstevel@tonic-gate 	} else if (*sp == '!') {
3567c478bd9Sstevel@tonic-gate 		sp++;
3577c478bd9Sstevel@tonic-gate 		if (*sp == '=') {
3587c478bd9Sstevel@tonic-gate 			sp++;
3597c478bd9Sstevel@tonic-gate 			ret = T_NEQ;
3607c478bd9Sstevel@tonic-gate 		} else {
3617c478bd9Sstevel@tonic-gate 			ret = T_LNOT;
3627c478bd9Sstevel@tonic-gate 		}
3637c478bd9Sstevel@tonic-gate 	} else if (*sp == '*') {
3647c478bd9Sstevel@tonic-gate 		sp++;
3657c478bd9Sstevel@tonic-gate 		ret = T_MUL;
3667c478bd9Sstevel@tonic-gate 	} else if (*sp == '/') {
3677c478bd9Sstevel@tonic-gate 		sp++;
3687c478bd9Sstevel@tonic-gate 		ret = T_DIV;
3697c478bd9Sstevel@tonic-gate 	} else if (*sp == '%') {
3707c478bd9Sstevel@tonic-gate 		sp++;
3717c478bd9Sstevel@tonic-gate 		ret = T_MOD;
3727c478bd9Sstevel@tonic-gate 	} else if (*sp == '+') {
3737c478bd9Sstevel@tonic-gate 		sp++;
3747c478bd9Sstevel@tonic-gate 		ret = T_ADD;
3757c478bd9Sstevel@tonic-gate 	} else if (*sp == '-') {
3767c478bd9Sstevel@tonic-gate 		sp++;
3777c478bd9Sstevel@tonic-gate 		ret = T_SUB;
3787c478bd9Sstevel@tonic-gate 	} else if (*sp == '<') {
3797c478bd9Sstevel@tonic-gate 		sp++;
3807c478bd9Sstevel@tonic-gate 		if (*sp == '=') {
3817c478bd9Sstevel@tonic-gate 			sp++;
3827c478bd9Sstevel@tonic-gate 			ret = T_LE;
3837c478bd9Sstevel@tonic-gate 		} else {
3847c478bd9Sstevel@tonic-gate 			ret = T_LT;
3857c478bd9Sstevel@tonic-gate 		}
3867c478bd9Sstevel@tonic-gate 	} else if (*sp == '>') {
3877c478bd9Sstevel@tonic-gate 		sp++;
3887c478bd9Sstevel@tonic-gate 		if (*sp == '=') {
3897c478bd9Sstevel@tonic-gate 			sp++;
3907c478bd9Sstevel@tonic-gate 			ret = T_GE;
3917c478bd9Sstevel@tonic-gate 		} else {
3927c478bd9Sstevel@tonic-gate 			ret = T_GT;
3937c478bd9Sstevel@tonic-gate 		}
3947c478bd9Sstevel@tonic-gate 	} else if (*sp == '=') {
3957c478bd9Sstevel@tonic-gate 		sp++;
3967c478bd9Sstevel@tonic-gate 		if (*sp == '=') {
3977c478bd9Sstevel@tonic-gate 			sp++;
3987c478bd9Sstevel@tonic-gate 			ret = T_EQ;
3997c478bd9Sstevel@tonic-gate 		} else {
4007c478bd9Sstevel@tonic-gate 			ret = T_ERR;
4017c478bd9Sstevel@tonic-gate 		}
4027c478bd9Sstevel@tonic-gate 	} else if (*sp == '&') {
4037c478bd9Sstevel@tonic-gate 		sp++;
4047c478bd9Sstevel@tonic-gate 		if (*sp == '&') {
4057c478bd9Sstevel@tonic-gate 			sp++;
4067c478bd9Sstevel@tonic-gate 			ret = T_LAND;
4077c478bd9Sstevel@tonic-gate 		} else {
4087c478bd9Sstevel@tonic-gate 			ret = T_ERR;
4097c478bd9Sstevel@tonic-gate 		}
4107c478bd9Sstevel@tonic-gate 	} else if (*sp == '|') {
4117c478bd9Sstevel@tonic-gate 		sp++;
4127c478bd9Sstevel@tonic-gate 		if (*sp == '|') {
4137c478bd9Sstevel@tonic-gate 			sp++;
4147c478bd9Sstevel@tonic-gate 			ret = T_LOR;
4157c478bd9Sstevel@tonic-gate 		} else {
4167c478bd9Sstevel@tonic-gate 			ret = T_ERR;
4177c478bd9Sstevel@tonic-gate 		}
4187c478bd9Sstevel@tonic-gate 	} else if (*sp == '?') {
4197c478bd9Sstevel@tonic-gate 		sp++;
4207c478bd9Sstevel@tonic-gate 		ret = T_CONDQ;
4217c478bd9Sstevel@tonic-gate 	} else if (*sp == ':') {
4227c478bd9Sstevel@tonic-gate 		sp++;
4237c478bd9Sstevel@tonic-gate 		ret = T_CONDC;
4247c478bd9Sstevel@tonic-gate 	} else if ((*sp == '\n') || (*sp == ';')) {
4257c478bd9Sstevel@tonic-gate 		ret = T_NULL;
4267c478bd9Sstevel@tonic-gate 	} else {
4277c478bd9Sstevel@tonic-gate 		ret = T_ERR;
4287c478bd9Sstevel@tonic-gate 	}
4297c478bd9Sstevel@tonic-gate 	if (which == GET_TOKEN)
4307c478bd9Sstevel@tonic-gate 		*pstr = (const char *)sp;
4317c478bd9Sstevel@tonic-gate 	return (operator[ret]);
4327c478bd9Sstevel@tonic-gate }
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate /*
4357c478bd9Sstevel@tonic-gate  * plural_expr
4367c478bd9Sstevel@tonic-gate  *
4377c478bd9Sstevel@tonic-gate  * INPUT
4387c478bd9Sstevel@tonic-gate  * str: string to parse
4397c478bd9Sstevel@tonic-gate  *
4407c478bd9Sstevel@tonic-gate  * OUTPUT
4417c478bd9Sstevel@tonic-gate  * e: parsed expression
4427c478bd9Sstevel@tonic-gate  *
4437c478bd9Sstevel@tonic-gate  * RETURN
4447c478bd9Sstevel@tonic-gate  * -1: Error happend (malloc failed)
4457c478bd9Sstevel@tonic-gate  *  1: Parse failed (invalid expression)
4467c478bd9Sstevel@tonic-gate  *  0: Parse succeeded
4477c478bd9Sstevel@tonic-gate  */
4487c478bd9Sstevel@tonic-gate int
plural_expr(struct expr ** e,const char * plural_string)4497c478bd9Sstevel@tonic-gate plural_expr(struct expr **e, const char *plural_string)
4507c478bd9Sstevel@tonic-gate {
4517c478bd9Sstevel@tonic-gate 	const char	*pstr = plural_string;
4527c478bd9Sstevel@tonic-gate 	struct stack	*stk, stkbuf;
4537c478bd9Sstevel@tonic-gate 	struct expr	*exp, *nexp, *exp_op, *ret;
4547c478bd9Sstevel@tonic-gate 	int	par, result;
4557c478bd9Sstevel@tonic-gate 	unsigned int	flag, ftype, fprio, fopnum, tmp_flag;
4567c478bd9Sstevel@tonic-gate 	unsigned int	ntype, nprio, ptype, popnum;
4577c478bd9Sstevel@tonic-gate 	unsigned int	op, nop, num, type, opnum;
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	stk = &stkbuf;
4607c478bd9Sstevel@tonic-gate 	stk->index = 0;
461*00ae5933SToomas Soome 	stk->ptr = malloc(sizeof (struct expr *) * MAX_STACK_SIZE);
4627c478bd9Sstevel@tonic-gate 	if (!stk->ptr) {
4637c478bd9Sstevel@tonic-gate 		/* malloc failed */
4647c478bd9Sstevel@tonic-gate 		return (-1);
4657c478bd9Sstevel@tonic-gate 	}
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	flag = operator[T_INIT];
4687c478bd9Sstevel@tonic-gate 	par = 0;
4697c478bd9Sstevel@tonic-gate 	while ((op = gettoken(&pstr, &num, GET_TOKEN)) != T_NULL) {
4707c478bd9Sstevel@tonic-gate 		type = GETTYPE(op);
4717c478bd9Sstevel@tonic-gate 		opnum = GETOPNUM(op);
4727c478bd9Sstevel@tonic-gate 		ftype = GETTYPE(flag);
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate #ifdef	PARSE_DEBUG
4757c478bd9Sstevel@tonic-gate 		printf("*** %s ***\n", type_name[type]);
4767c478bd9Sstevel@tonic-gate 		printf("   flag: %s\n", type_name[ftype]);
4777c478bd9Sstevel@tonic-gate 		printf("   par: %d\n", par);
4787c478bd9Sstevel@tonic-gate 		printf("***********\n");
4797c478bd9Sstevel@tonic-gate #endif
4807c478bd9Sstevel@tonic-gate 		if (type == T_ERR) {
4817c478bd9Sstevel@tonic-gate 			/* parse failed */
4827c478bd9Sstevel@tonic-gate 			STACKFREE;
4837c478bd9Sstevel@tonic-gate 			return (1);
4847c478bd9Sstevel@tonic-gate 		}
4857c478bd9Sstevel@tonic-gate 		if (opnum == BINARY) {
4867c478bd9Sstevel@tonic-gate 			/* binary operation */
4877c478bd9Sstevel@tonic-gate 			if (ftype != T_EXP) {
4887c478bd9Sstevel@tonic-gate 				/* parse failed */
4897c478bd9Sstevel@tonic-gate #ifdef	PARSE_DEBUG
4907c478bd9Sstevel@tonic-gate 				printf("ERR: T_EXP is not followed by %s\n",
491*00ae5933SToomas Soome 				    type_name[type]);
4927c478bd9Sstevel@tonic-gate #endif
4937c478bd9Sstevel@tonic-gate 				STACKFREE;
4947c478bd9Sstevel@tonic-gate 				return (1);
4957c478bd9Sstevel@tonic-gate 			}
4967c478bd9Sstevel@tonic-gate 			exp = setop1(op, 0, stk, flag);
4977c478bd9Sstevel@tonic-gate 			if (!exp)
4987c478bd9Sstevel@tonic-gate 				return (-1);
4997c478bd9Sstevel@tonic-gate 			ret = stack_push(stk, exp);
5007c478bd9Sstevel@tonic-gate 			if (!ret)
5017c478bd9Sstevel@tonic-gate 				return (1);
5027c478bd9Sstevel@tonic-gate 			flag = op;
5037c478bd9Sstevel@tonic-gate 			continue;			/* while-loop */
5047c478bd9Sstevel@tonic-gate 		}
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 		if (type == T_CONDQ) {
5077c478bd9Sstevel@tonic-gate 			/* conditional operation: '?' */
5087c478bd9Sstevel@tonic-gate 			if (ftype != T_EXP) {
5097c478bd9Sstevel@tonic-gate 				/* parse failed */
5107c478bd9Sstevel@tonic-gate #ifdef	PARSE_DEBUG
5117c478bd9Sstevel@tonic-gate 				printf("ERR: T_EXP is not followed by %s\n",
512*00ae5933SToomas Soome 				    type_name[type]);
5137c478bd9Sstevel@tonic-gate #endif
5147c478bd9Sstevel@tonic-gate 				STACKFREE;
5157c478bd9Sstevel@tonic-gate 				return (1);
5167c478bd9Sstevel@tonic-gate 			}
5177c478bd9Sstevel@tonic-gate 			exp = setop1(op, 0, stk, flag);
5187c478bd9Sstevel@tonic-gate 			if (!exp)
5197c478bd9Sstevel@tonic-gate 				return (-1);
5207c478bd9Sstevel@tonic-gate 			ret = stack_push(stk, exp);
5217c478bd9Sstevel@tonic-gate 			if (!ret)
5227c478bd9Sstevel@tonic-gate 				return (1);
5237c478bd9Sstevel@tonic-gate 			flag = op;
5247c478bd9Sstevel@tonic-gate 			continue;			/* while-loop */
5257c478bd9Sstevel@tonic-gate 		}
5267c478bd9Sstevel@tonic-gate 		if (type == T_CONDC) {
5277c478bd9Sstevel@tonic-gate 			/* conditional operation: ':' */
5287c478bd9Sstevel@tonic-gate 			if (ftype != T_EXP) {
5297c478bd9Sstevel@tonic-gate 				/* parse failed */
5307c478bd9Sstevel@tonic-gate #ifdef	PARSE_DEBUG
5317c478bd9Sstevel@tonic-gate 				printf("ERR: T_EXP is not followed by %s\n",
532*00ae5933SToomas Soome 				    type_name[type]);
5337c478bd9Sstevel@tonic-gate #endif
5347c478bd9Sstevel@tonic-gate 				STACKFREE;
5357c478bd9Sstevel@tonic-gate 				return (1);
5367c478bd9Sstevel@tonic-gate 			}
5377c478bd9Sstevel@tonic-gate 			exp = setop1(op, 0, stk, flag);
5387c478bd9Sstevel@tonic-gate 			if (!exp)
5397c478bd9Sstevel@tonic-gate 				return (-1);
5407c478bd9Sstevel@tonic-gate 			ret = stack_push(stk, exp);
5417c478bd9Sstevel@tonic-gate 			if (!ret)
5427c478bd9Sstevel@tonic-gate 				return (1);
5437c478bd9Sstevel@tonic-gate 			flag = op;
5447c478bd9Sstevel@tonic-gate 			continue;			/* while-loop */
5457c478bd9Sstevel@tonic-gate 		}
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 		if (type == T_LPAR) {
5487c478bd9Sstevel@tonic-gate 			/* left parenthesis */
5497c478bd9Sstevel@tonic-gate 			if (ftype == T_EXP) {
5507c478bd9Sstevel@tonic-gate 				/* parse failed */
5517c478bd9Sstevel@tonic-gate #ifdef	PARSE_DEBUG
5527c478bd9Sstevel@tonic-gate 				printf("ERR: T_EXP is followed by %s\n",
553*00ae5933SToomas Soome 				    type_name[type]);
5547c478bd9Sstevel@tonic-gate #endif
5557c478bd9Sstevel@tonic-gate 				STACKFREE;
5567c478bd9Sstevel@tonic-gate 				return (1);
5577c478bd9Sstevel@tonic-gate 			}
5587c478bd9Sstevel@tonic-gate 			exp = setop1(op, 0, stk, flag);
5597c478bd9Sstevel@tonic-gate 			if (!exp)
5607c478bd9Sstevel@tonic-gate 				return (-1);
5617c478bd9Sstevel@tonic-gate 			ret = stack_push(stk, exp);
5627c478bd9Sstevel@tonic-gate 			if (!ret)
5637c478bd9Sstevel@tonic-gate 				return (1);
5647c478bd9Sstevel@tonic-gate 			par++;
5657c478bd9Sstevel@tonic-gate 			flag = op;
5667c478bd9Sstevel@tonic-gate 			continue;			/* while-loop */
5677c478bd9Sstevel@tonic-gate 		}
5687c478bd9Sstevel@tonic-gate 		if (type == T_RPAR) {
5697c478bd9Sstevel@tonic-gate 			/* right parenthesis */
5707c478bd9Sstevel@tonic-gate 			if (ftype != T_EXP) {
5717c478bd9Sstevel@tonic-gate 				/* parse failed */
5727c478bd9Sstevel@tonic-gate #ifdef	PARSE_DEBUG
5737c478bd9Sstevel@tonic-gate 				printf("ERR: T_EXP is not followed by %s\n",
574*00ae5933SToomas Soome 				    type_name[type]);
5757c478bd9Sstevel@tonic-gate #endif
5767c478bd9Sstevel@tonic-gate 				STACKFREE;
5777c478bd9Sstevel@tonic-gate 				return (1);
5787c478bd9Sstevel@tonic-gate 			}
5797c478bd9Sstevel@tonic-gate 			par--;
5807c478bd9Sstevel@tonic-gate 			if (par < 0) {
5817c478bd9Sstevel@tonic-gate 				/* parse failed */
5827c478bd9Sstevel@tonic-gate #ifdef	PARSE_DEBUG
5837c478bd9Sstevel@tonic-gate 				printf("ERR: too much T_RPAR\n");
5847c478bd9Sstevel@tonic-gate #endif
5857c478bd9Sstevel@tonic-gate 				STACKFREE;
5867c478bd9Sstevel@tonic-gate 				return (1);
5877c478bd9Sstevel@tonic-gate 			}
5887c478bd9Sstevel@tonic-gate 			exp = stack_pop(stk, NULL, NULL);
5897c478bd9Sstevel@tonic-gate 			if (!exp)
5907c478bd9Sstevel@tonic-gate 				return (1);
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate #ifdef	PARSE_DEBUG
5937c478bd9Sstevel@tonic-gate 			printf("======================== RPAR for loop in\n");
5947c478bd9Sstevel@tonic-gate #endif
5957c478bd9Sstevel@tonic-gate 			for (; ; ) {
5967c478bd9Sstevel@tonic-gate 				ptype = GETTYPE(exp->flag);
5977c478bd9Sstevel@tonic-gate 				popnum = GETOPNUM(exp->flag);
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate #ifdef	PARSE_DEBUG
6007c478bd9Sstevel@tonic-gate 				printf("=========== exp->flag: %s\n",
601*00ae5933SToomas Soome 				    type_name[ptype]);
6027c478bd9Sstevel@tonic-gate #endif
6037c478bd9Sstevel@tonic-gate 				if (ptype == T_LPAR) {
6047c478bd9Sstevel@tonic-gate 					exp_op = stack_pop(stk, exp, NULL);
6057c478bd9Sstevel@tonic-gate 					if (!exp_op)
6067c478bd9Sstevel@tonic-gate 						return (1);
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 					tmp_flag = exp_op->flag;
6097c478bd9Sstevel@tonic-gate 					freeexpr(exp_op);
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 					exp->flag = tmp_flag;
6127c478bd9Sstevel@tonic-gate 					flag = tmp_flag;
6137c478bd9Sstevel@tonic-gate 					break;	/* break from for-loop */
6147c478bd9Sstevel@tonic-gate 				}
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 				if ((popnum == BINARY) ||
617*00ae5933SToomas Soome 				    (ptype == T_LNOT) ||
618*00ae5933SToomas Soome 				    (ptype == T_CONDC)) {
6197c478bd9Sstevel@tonic-gate 					result = reduce(&nexp, popnum,
620*00ae5933SToomas Soome 					    exp, stk);
6217c478bd9Sstevel@tonic-gate 					if (result)
6227c478bd9Sstevel@tonic-gate 						return (result);
6237c478bd9Sstevel@tonic-gate 					exp = nexp;
6247c478bd9Sstevel@tonic-gate 					continue;	/* for-loop */
6257c478bd9Sstevel@tonic-gate 				}
6267c478bd9Sstevel@tonic-gate 				/* parse failed */
6277c478bd9Sstevel@tonic-gate 				freeexpr(exp);
6287c478bd9Sstevel@tonic-gate 				STACKFREE;
6297c478bd9Sstevel@tonic-gate 				return (1);
630*00ae5933SToomas Soome 			}		/* for-loop */
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate #ifdef	PARSE_DEBUG
6337c478bd9Sstevel@tonic-gate printf("========================= RPAR for loop out\n");
6347c478bd9Sstevel@tonic-gate #endif
6357c478bd9Sstevel@tonic-gate 			/*
6367c478bd9Sstevel@tonic-gate 			 * Needs to check if exp can be reduced or not
6377c478bd9Sstevel@tonic-gate 			 */
6387c478bd9Sstevel@tonic-gate 			goto exp_check;
6397c478bd9Sstevel@tonic-gate 		}
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 		if (type == T_LNOT) {
6427c478bd9Sstevel@tonic-gate 			if (ftype == T_EXP) {
6437c478bd9Sstevel@tonic-gate 				/* parse failed */
6447c478bd9Sstevel@tonic-gate #ifdef	PARSE_DEBUG
6457c478bd9Sstevel@tonic-gate 				printf("ERR: T_EXP is followed by %s\n",
646*00ae5933SToomas Soome 				    type_name[type]);
6477c478bd9Sstevel@tonic-gate #endif
6487c478bd9Sstevel@tonic-gate 				STACKFREE;
6497c478bd9Sstevel@tonic-gate 				return (1);
6507c478bd9Sstevel@tonic-gate 			}
6517c478bd9Sstevel@tonic-gate 			exp = setop1(op, 0, stk, flag);
6527c478bd9Sstevel@tonic-gate 			if (!exp)
6537c478bd9Sstevel@tonic-gate 				return (-1);
6547c478bd9Sstevel@tonic-gate 			ret = stack_push(stk, exp);
6557c478bd9Sstevel@tonic-gate 			if (!ret)
6567c478bd9Sstevel@tonic-gate 				return (1);
6577c478bd9Sstevel@tonic-gate 			flag = op;
6587c478bd9Sstevel@tonic-gate 			continue;			/* while-loop */
6597c478bd9Sstevel@tonic-gate 		}
6607c478bd9Sstevel@tonic-gate 		if ((type == T_NUM) || (type == T_VAR)) {
6617c478bd9Sstevel@tonic-gate 			exp = setop1(op, type == T_NUM ? num : 0, stk, flag);
6627c478bd9Sstevel@tonic-gate 			if (!exp)
6637c478bd9Sstevel@tonic-gate 				return (-1);
6647c478bd9Sstevel@tonic-gate exp_check:
6657c478bd9Sstevel@tonic-gate 			ftype = GETTYPE(flag);
6667c478bd9Sstevel@tonic-gate 			if ((ftype == T_INIT) || (ftype == T_LPAR)) {
6677c478bd9Sstevel@tonic-gate 				/*
6687c478bd9Sstevel@tonic-gate 				 * if this NUM/VAR is the first EXP,
6697c478bd9Sstevel@tonic-gate 				 * just push this
6707c478bd9Sstevel@tonic-gate 				 */
6717c478bd9Sstevel@tonic-gate 				exp->flag = flag;
6727c478bd9Sstevel@tonic-gate 				ret = stack_push(stk, exp);
6737c478bd9Sstevel@tonic-gate 				if (!ret)
6747c478bd9Sstevel@tonic-gate 					return (1);
6757c478bd9Sstevel@tonic-gate 				flag = operator[T_EXP];
6767c478bd9Sstevel@tonic-gate 				continue;		/* while-loop */
6777c478bd9Sstevel@tonic-gate 			}
6787c478bd9Sstevel@tonic-gate 			if (ftype == T_EXP) {
6797c478bd9Sstevel@tonic-gate 				/*
6807c478bd9Sstevel@tonic-gate 				 * parse failed
6817c478bd9Sstevel@tonic-gate 				 * NUM/VAR cannot be seen just after
6827c478bd9Sstevel@tonic-gate 				 * T_EXP
6837c478bd9Sstevel@tonic-gate 				 */
6847c478bd9Sstevel@tonic-gate 				freeexpr(exp);
6857c478bd9Sstevel@tonic-gate 				STACKFREE;
6867c478bd9Sstevel@tonic-gate 				return (1);
6877c478bd9Sstevel@tonic-gate 			}
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 			nop = gettoken(&pstr, &num, PEEK_TOKEN);
6907c478bd9Sstevel@tonic-gate 			if (nop != T_NULL) {
6917c478bd9Sstevel@tonic-gate 				ntype = GETTYPE(nop);
6927c478bd9Sstevel@tonic-gate 				nprio = GETPRIO(nop);
6937c478bd9Sstevel@tonic-gate 			} else {
6947c478bd9Sstevel@tonic-gate 				(void) gettoken(&pstr, &num, GET_TOKEN);
6957c478bd9Sstevel@tonic-gate 				ntype = T_INIT;
6967c478bd9Sstevel@tonic-gate 				nprio = 0;
6977c478bd9Sstevel@tonic-gate 			}
6987c478bd9Sstevel@tonic-gate #ifdef	PARSE_DEBUG
6997c478bd9Sstevel@tonic-gate printf("========================== T_NUM/T_VAR for loop in\n");
7007c478bd9Sstevel@tonic-gate #endif
7017c478bd9Sstevel@tonic-gate 			for (; ; ) {
7027c478bd9Sstevel@tonic-gate 				ftype = GETTYPE(flag);
7037c478bd9Sstevel@tonic-gate 				fopnum = GETOPNUM(flag);
7047c478bd9Sstevel@tonic-gate 				fprio = GETPRIO(flag);
7057c478bd9Sstevel@tonic-gate #ifdef	PARSE_DEBUG
7067c478bd9Sstevel@tonic-gate 				printf("========= flag: %s\n",
707*00ae5933SToomas Soome 				    type_name[ftype]);
7087c478bd9Sstevel@tonic-gate #endif
7097c478bd9Sstevel@tonic-gate 				if ((ftype == T_INIT) || (ftype == T_LPAR)) {
7107c478bd9Sstevel@tonic-gate 					exp->flag = flag;
7117c478bd9Sstevel@tonic-gate 					ret = stack_push(stk, exp);
7127c478bd9Sstevel@tonic-gate 					if (!ret)
7137c478bd9Sstevel@tonic-gate 						return (1);
7147c478bd9Sstevel@tonic-gate 					flag = operator[T_EXP];
7157c478bd9Sstevel@tonic-gate 					break;		/* exit from for-loop */
7167c478bd9Sstevel@tonic-gate 				}
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 				if (ftype == T_LNOT) {
7197c478bd9Sstevel@tonic-gate 					/* LNOT is the strongest */
7207c478bd9Sstevel@tonic-gate 					result = reduce(&nexp, UNARY, exp, stk);
7217c478bd9Sstevel@tonic-gate 					if (result)
7227c478bd9Sstevel@tonic-gate 						return (result);
7237c478bd9Sstevel@tonic-gate 					exp = nexp;
7247c478bd9Sstevel@tonic-gate 					flag = nexp->flag;
7257c478bd9Sstevel@tonic-gate 					continue;	/* for-loop */
7267c478bd9Sstevel@tonic-gate 				}
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 				if (fopnum == BINARY) {
7297c478bd9Sstevel@tonic-gate 					/*
7307c478bd9Sstevel@tonic-gate 					 * binary operation
7317c478bd9Sstevel@tonic-gate 					 * T_MUL, T_ADD,  T_CMP,
7327c478bd9Sstevel@tonic-gate 					 * T_EQ,  T_LAND, T_LOR
7337c478bd9Sstevel@tonic-gate 					 */
7347c478bd9Sstevel@tonic-gate 					if ((ntype == T_RPAR) ||
735*00ae5933SToomas Soome 					    (nprio <= fprio)) {
7367c478bd9Sstevel@tonic-gate 						/* reduce */
7377c478bd9Sstevel@tonic-gate 						result = reduce(&nexp, BINARY,
738*00ae5933SToomas Soome 						    exp, stk);
7397c478bd9Sstevel@tonic-gate 						if (result)
7407c478bd9Sstevel@tonic-gate 							return (result);
7417c478bd9Sstevel@tonic-gate 						exp = nexp;
7427c478bd9Sstevel@tonic-gate 						flag = nexp->flag;
7437c478bd9Sstevel@tonic-gate 						continue; /* for-loop */
7447c478bd9Sstevel@tonic-gate 					}
7457c478bd9Sstevel@tonic-gate 					/* shift */
7467c478bd9Sstevel@tonic-gate 					exp->flag = flag;
7477c478bd9Sstevel@tonic-gate 					ret = stack_push(stk, exp);
7487c478bd9Sstevel@tonic-gate 					if (!ret)
7497c478bd9Sstevel@tonic-gate 						return (1);
7507c478bd9Sstevel@tonic-gate 					flag = operator[T_EXP];
7517c478bd9Sstevel@tonic-gate 					break;		/* exit from for loop */
7527c478bd9Sstevel@tonic-gate 				}
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 				if (ftype == T_CONDQ) {
7557c478bd9Sstevel@tonic-gate 					/*
7567c478bd9Sstevel@tonic-gate 					 * CONDQ is the weakest
7577c478bd9Sstevel@tonic-gate 					 * always shift
7587c478bd9Sstevel@tonic-gate 					 */
7597c478bd9Sstevel@tonic-gate 					exp->flag = flag;
7607c478bd9Sstevel@tonic-gate 					ret = stack_push(stk, exp);
7617c478bd9Sstevel@tonic-gate 					if (!ret)
7627c478bd9Sstevel@tonic-gate 						return (1);
7637c478bd9Sstevel@tonic-gate 					flag = operator[T_EXP];
7647c478bd9Sstevel@tonic-gate 					break;		/* exit from for loop */
7657c478bd9Sstevel@tonic-gate 				}
7667c478bd9Sstevel@tonic-gate 				if (ftype == T_CONDC) {
7677c478bd9Sstevel@tonic-gate 					if (nprio <= fprio) {
7687c478bd9Sstevel@tonic-gate 						/* reduce */
7697c478bd9Sstevel@tonic-gate 						result = reduce(&nexp, TRINARY,
770*00ae5933SToomas Soome 						    exp, stk);
7717c478bd9Sstevel@tonic-gate 						if (result)
7727c478bd9Sstevel@tonic-gate 							return (result);
7737c478bd9Sstevel@tonic-gate 						exp = nexp;
7747c478bd9Sstevel@tonic-gate 						flag = nexp->flag;
7757c478bd9Sstevel@tonic-gate 						continue; /* for-loop */
7767c478bd9Sstevel@tonic-gate 					}
7777c478bd9Sstevel@tonic-gate 					/* shift */
7787c478bd9Sstevel@tonic-gate 					exp->flag = flag;
7797c478bd9Sstevel@tonic-gate 					ret = stack_push(stk, exp);
7807c478bd9Sstevel@tonic-gate 					if (!ret)
7817c478bd9Sstevel@tonic-gate 						return (1);
7827c478bd9Sstevel@tonic-gate 					flag = operator[T_EXP];
7837c478bd9Sstevel@tonic-gate 					break;		/* exit from for-loop */
7847c478bd9Sstevel@tonic-gate 				}
7857c478bd9Sstevel@tonic-gate 				/* parse failed */
7867c478bd9Sstevel@tonic-gate 				freeexpr(exp);
7877c478bd9Sstevel@tonic-gate 				STACKFREE;
7887c478bd9Sstevel@tonic-gate 				return (1);
7897c478bd9Sstevel@tonic-gate 			}
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate #ifdef	PARSE_DEBUG
7927c478bd9Sstevel@tonic-gate printf("======================= T_NUM/T_VAR for loop out\n");
7937c478bd9Sstevel@tonic-gate #endif
7947c478bd9Sstevel@tonic-gate 			continue;			/* while-loop */
7957c478bd9Sstevel@tonic-gate 		}
7967c478bd9Sstevel@tonic-gate 		/* parse failed */
7977c478bd9Sstevel@tonic-gate 		STACKFREE;
7987c478bd9Sstevel@tonic-gate 		return (1);
799*00ae5933SToomas Soome 	}	/* while-loop */
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	if (GETTYPE(flag) != T_EXP) {
8027c478bd9Sstevel@tonic-gate 		/* parse failed */
8037c478bd9Sstevel@tonic-gate #ifdef	PARSE_DEBUG
8047c478bd9Sstevel@tonic-gate 		printf("XXXX ERROR: flag is not T_INIT\n");
8057c478bd9Sstevel@tonic-gate 		printf("========= flag: %s\n", type_name[GETTYPE(flag)]);
8067c478bd9Sstevel@tonic-gate #endif
8077c478bd9Sstevel@tonic-gate 		STACKFREE;
8087c478bd9Sstevel@tonic-gate 		return (1);
8097c478bd9Sstevel@tonic-gate 	} else {
8107c478bd9Sstevel@tonic-gate 		exp = stack_pop(stk, NULL, NULL);
8117c478bd9Sstevel@tonic-gate 		if (!exp)
8127c478bd9Sstevel@tonic-gate 			return (1);
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 		if (GETTYPE(exp->flag) != T_INIT) {
8157c478bd9Sstevel@tonic-gate 			/* parse failed */
8167c478bd9Sstevel@tonic-gate #ifdef	PARSE_DEBUG
8177c478bd9Sstevel@tonic-gate 			printf("ERR: flag for the result is not T_INIT\n");
8187c478bd9Sstevel@tonic-gate 			printf("      %s observed\n",
819*00ae5933SToomas Soome 			    type_name[GETTYPE(exp->flag)]);
8207c478bd9Sstevel@tonic-gate #endif
8217c478bd9Sstevel@tonic-gate 			freeexpr(exp);
8227c478bd9Sstevel@tonic-gate 			STACKFREE;
8237c478bd9Sstevel@tonic-gate 			return (1);
8247c478bd9Sstevel@tonic-gate 		}
8257c478bd9Sstevel@tonic-gate 		if (stk->index > 0) {
8267c478bd9Sstevel@tonic-gate 			/*
8277c478bd9Sstevel@tonic-gate 			 * exp still remains in stack.
8287c478bd9Sstevel@tonic-gate 			 * parse failed
8297c478bd9Sstevel@tonic-gate 			 */
830*00ae5933SToomas Soome 			while ((nexp = stack_pop(stk, NULL, NULL)) != NULL)
8317c478bd9Sstevel@tonic-gate 				freeexpr(nexp);
8327c478bd9Sstevel@tonic-gate 			freeexpr(exp);
8337c478bd9Sstevel@tonic-gate 			return (1);
8347c478bd9Sstevel@tonic-gate 		}
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 		/* parse succeeded */
8377c478bd9Sstevel@tonic-gate 		*e = exp;
8387c478bd9Sstevel@tonic-gate 		STACKFREE;
8397c478bd9Sstevel@tonic-gate 		return (0);
8407c478bd9Sstevel@tonic-gate 	}
8417c478bd9Sstevel@tonic-gate }
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate unsigned int
plural_eval(struct expr * exp,unsigned int n)8447c478bd9Sstevel@tonic-gate plural_eval(struct expr *exp, unsigned int n)
8457c478bd9Sstevel@tonic-gate {
8467c478bd9Sstevel@tonic-gate 	unsigned int	e1, e2;
8477c478bd9Sstevel@tonic-gate 	unsigned int	type, opnum;
8487c478bd9Sstevel@tonic-gate #ifdef GETTEXT_DEBUG
849*00ae5933SToomas Soome 	(void) printf("*************** plural_eval(%p, %d)\n", exp, n);
8507c478bd9Sstevel@tonic-gate 	printexpr(exp, 0);
8517c478bd9Sstevel@tonic-gate #endif
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	type = GETTYPE(exp->op);
8547c478bd9Sstevel@tonic-gate 	opnum = GETOPNUM(exp->op);
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	switch (opnum) {
8577c478bd9Sstevel@tonic-gate 	case NARY:
8587c478bd9Sstevel@tonic-gate 		if (type == T_NUM) {
8597c478bd9Sstevel@tonic-gate 			return (exp->num);
8607c478bd9Sstevel@tonic-gate 		} else if (type == T_VAR) {
8617c478bd9Sstevel@tonic-gate 			return (n);
8627c478bd9Sstevel@tonic-gate 		}
8637c478bd9Sstevel@tonic-gate 		break;
8647c478bd9Sstevel@tonic-gate 	case UNARY:
8657c478bd9Sstevel@tonic-gate 		/* T_LNOT */
8667c478bd9Sstevel@tonic-gate 		e1 = plural_eval(exp->nodes[0], n);
8677c478bd9Sstevel@tonic-gate 		return (!e1);
8687c478bd9Sstevel@tonic-gate 	case BINARY:
8697c478bd9Sstevel@tonic-gate 		e1 = plural_eval(exp->nodes[0], n);
8707c478bd9Sstevel@tonic-gate 		/* optimization for T_LOR and T_LAND */
8717c478bd9Sstevel@tonic-gate 		if (type == T_LOR) {
8727c478bd9Sstevel@tonic-gate 			return (e1 || plural_eval(exp->nodes[1], n));
8737c478bd9Sstevel@tonic-gate 		} else if (type == T_LAND) {
8747c478bd9Sstevel@tonic-gate 			return (e1 && plural_eval(exp->nodes[1], n));
8757c478bd9Sstevel@tonic-gate 		}
8767c478bd9Sstevel@tonic-gate 		e2 = plural_eval(exp->nodes[1], n);
8777c478bd9Sstevel@tonic-gate 		switch (type) {
8787c478bd9Sstevel@tonic-gate 		case T_EQ:
8797c478bd9Sstevel@tonic-gate 			return (e1 == e2);
8807c478bd9Sstevel@tonic-gate 		case T_NEQ:
8817c478bd9Sstevel@tonic-gate 			return (e1 != e2);
8827c478bd9Sstevel@tonic-gate 		case T_GT:
8837c478bd9Sstevel@tonic-gate 			return (e1 > e2);
8847c478bd9Sstevel@tonic-gate 		case T_LT:
8857c478bd9Sstevel@tonic-gate 			return (e1 < e2);
8867c478bd9Sstevel@tonic-gate 		case T_GE:
8877c478bd9Sstevel@tonic-gate 			return (e1 >= e2);
8887c478bd9Sstevel@tonic-gate 		case T_LE:
8897c478bd9Sstevel@tonic-gate 			return (e1 <= e2);
8907c478bd9Sstevel@tonic-gate 		case T_ADD:
8917c478bd9Sstevel@tonic-gate 			return (e1 + e2);
8927c478bd9Sstevel@tonic-gate 		case T_SUB:
8937c478bd9Sstevel@tonic-gate 			return (e1 - e2);
8947c478bd9Sstevel@tonic-gate 		case T_MUL:
8957c478bd9Sstevel@tonic-gate 			return (e1 * e2);
8967c478bd9Sstevel@tonic-gate 		case T_DIV:
8977c478bd9Sstevel@tonic-gate 			if (e2 != 0)
8987c478bd9Sstevel@tonic-gate 				return (e1 / e2);
8997c478bd9Sstevel@tonic-gate 			break;
9007c478bd9Sstevel@tonic-gate 		case T_MOD:
9017c478bd9Sstevel@tonic-gate 			if (e2 != 0)
9027c478bd9Sstevel@tonic-gate 				return (e1 % e2);
9037c478bd9Sstevel@tonic-gate 			break;
9047c478bd9Sstevel@tonic-gate 		}
9057c478bd9Sstevel@tonic-gate 		break;
9067c478bd9Sstevel@tonic-gate 	case TRINARY:
9077c478bd9Sstevel@tonic-gate 		/* T_CONDQ */
9087c478bd9Sstevel@tonic-gate 		e1 = plural_eval(exp->nodes[0], n);
9097c478bd9Sstevel@tonic-gate 		if (e1) {
9107c478bd9Sstevel@tonic-gate 			return (plural_eval(exp->nodes[1], n));
9117c478bd9Sstevel@tonic-gate 		} else {
9127c478bd9Sstevel@tonic-gate 			return (plural_eval(exp->nodes[2], n));
9137c478bd9Sstevel@tonic-gate 		}
9147c478bd9Sstevel@tonic-gate 	}
9157c478bd9Sstevel@tonic-gate 	/* should not be here */
9167c478bd9Sstevel@tonic-gate 	return (0);
9177c478bd9Sstevel@tonic-gate }
918