xref: /onnv-gate/usr/src/cmd/fm/modules/common/eversholt/eval.c (revision 2318:4b70b73e147f)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51717Swesolows  * Common Development and Distribution License (the "License").
61717Swesolows  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211717Swesolows 
220Sstevel@tonic-gate /*
231414Scindi  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  *
260Sstevel@tonic-gate  * eval.c -- constraint evaluation module
270Sstevel@tonic-gate  *
280Sstevel@tonic-gate  * this module evaluates constraints.
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <stdio.h>
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <ctype.h>
360Sstevel@tonic-gate #include <string.h>
370Sstevel@tonic-gate #include "alloc.h"
380Sstevel@tonic-gate #include "out.h"
390Sstevel@tonic-gate #include "stable.h"
400Sstevel@tonic-gate #include "literals.h"
410Sstevel@tonic-gate #include "lut.h"
420Sstevel@tonic-gate #include "tree.h"
430Sstevel@tonic-gate #include "ptree.h"
440Sstevel@tonic-gate #include "itree.h"
451414Scindi #include "ipath.h"
460Sstevel@tonic-gate #include "eval.h"
470Sstevel@tonic-gate #include "config.h"
480Sstevel@tonic-gate #include "platform.h"
491414Scindi #include "fme.h"
501414Scindi #include "stats.h"
510Sstevel@tonic-gate 
520Sstevel@tonic-gate static struct node *eval_dup(struct node *np, struct lut *ex,
531414Scindi     struct node *epnames[]);
541414Scindi static int check_expr_args(struct evalue *lp, struct evalue *rp,
551414Scindi     enum datatype dtype, struct node *np);
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate  * begins_with -- return true if rhs path begins with everything in lhs path
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate static int
610Sstevel@tonic-gate begins_with(struct node *lhs, struct node *rhs)
620Sstevel@tonic-gate {
630Sstevel@tonic-gate 	int lnum;
640Sstevel@tonic-gate 	int rnum;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	if (lhs == NULL)
670Sstevel@tonic-gate 		return (1);	/* yep -- it all matched */
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	if (rhs == NULL)
700Sstevel@tonic-gate 		return (0);	/* nope, ran out of rhs first */
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	ASSERTeq(lhs->t, T_NAME, ptree_nodetype2str);
730Sstevel@tonic-gate 	ASSERTeq(rhs->t, T_NAME, ptree_nodetype2str);
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	if (lhs->u.name.s != rhs->u.name.s)
760Sstevel@tonic-gate 		return (0);	/* nope, different component names */
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	if (lhs->u.name.child && lhs->u.name.child->t == T_NUM)
790Sstevel@tonic-gate 		lnum = (int)lhs->u.name.child->u.ull;
800Sstevel@tonic-gate 	else
810Sstevel@tonic-gate 		out(O_DIE, "begins_with: unexpected lhs child");
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	if (rhs->u.name.child && rhs->u.name.child->t == T_NUM)
840Sstevel@tonic-gate 		rnum = (int)rhs->u.name.child->u.ull;
850Sstevel@tonic-gate 	else
860Sstevel@tonic-gate 		out(O_DIE, "begins_with: unexpected rhs child");
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	if (lnum != rnum)
890Sstevel@tonic-gate 		return (0);	/* nope, instance numbers were different */
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	return (begins_with(lhs->u.name.next, rhs->u.name.next));
920Sstevel@tonic-gate }
930Sstevel@tonic-gate 
940Sstevel@tonic-gate /*
950Sstevel@tonic-gate  * evaluate a variety of functions and place result in valuep.  return 1 if
960Sstevel@tonic-gate  * function evaluation was successful; 0 if otherwise (e.g., the case of an
970Sstevel@tonic-gate  * invalid argument to the function)
980Sstevel@tonic-gate  */
990Sstevel@tonic-gate /*ARGSUSED*/
1000Sstevel@tonic-gate static int
1010Sstevel@tonic-gate eval_func(struct node *funcnp, struct lut *ex, struct node *epnames[],
1020Sstevel@tonic-gate     struct node *np, struct lut **globals,
1030Sstevel@tonic-gate     struct config *croot, struct arrow *arrowp, int try, struct evalue *valuep)
1040Sstevel@tonic-gate {
1050Sstevel@tonic-gate 	const char *funcname = funcnp->u.func.s;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	if (funcname == L_within) {
1080Sstevel@tonic-gate 		/* within()'s are not really constraints -- always true */
1090Sstevel@tonic-gate 		valuep->t = UINT64;
1100Sstevel@tonic-gate 		valuep->v = 1;
1110Sstevel@tonic-gate 		return (1);
1120Sstevel@tonic-gate 	} else if (funcname == L_is_under) {
1130Sstevel@tonic-gate 		struct node *lhs;
1140Sstevel@tonic-gate 		struct node *rhs;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 		if (np->u.expr.left->t == T_NAME)
1170Sstevel@tonic-gate 			lhs = np->u.expr.left;
1180Sstevel@tonic-gate 		else if (np->u.expr.left->u.func.s == L_fru)
1190Sstevel@tonic-gate 			lhs = eval_fru(np->u.expr.left->u.func.arglist);
1200Sstevel@tonic-gate 		else if (np->u.expr.left->u.func.s == L_asru)
1210Sstevel@tonic-gate 			lhs = eval_asru(np->u.expr.left->u.func.arglist);
1220Sstevel@tonic-gate 		else
1230Sstevel@tonic-gate 			out(O_DIE, "is_under: unexpected lhs type: %s",
1240Sstevel@tonic-gate 			    ptree_nodetype2str(np->u.expr.left->t));
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 		if (np->u.expr.right->t == T_NAME)
1270Sstevel@tonic-gate 			rhs = np->u.expr.right;
1280Sstevel@tonic-gate 		else if (np->u.expr.right->u.func.s == L_fru)
1290Sstevel@tonic-gate 			rhs = eval_fru(np->u.expr.right->u.func.arglist);
1300Sstevel@tonic-gate 		else if (np->u.expr.right->u.func.s == L_asru)
1310Sstevel@tonic-gate 			rhs = eval_asru(np->u.expr.right->u.func.arglist);
1320Sstevel@tonic-gate 		else
1330Sstevel@tonic-gate 			out(O_DIE, "is_under: unexpected rhs type: %s",
1340Sstevel@tonic-gate 			    ptree_nodetype2str(np->u.expr.right->t));
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 		/* eval_dup will expand wildcards, iterators, etc... */
1370Sstevel@tonic-gate 		lhs = eval_dup(lhs, ex, epnames);
1380Sstevel@tonic-gate 		rhs = eval_dup(rhs, ex, epnames);
1390Sstevel@tonic-gate 		valuep->t = UINT64;
1400Sstevel@tonic-gate 		valuep->v = begins_with(lhs, rhs);
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 		out(O_ALTFP|O_VERB2|O_NONL, "eval_func:is_under(");
1430Sstevel@tonic-gate 		ptree_name_iter(O_ALTFP|O_VERB2|O_NONL, lhs);
1440Sstevel@tonic-gate 		out(O_ALTFP|O_VERB2|O_NONL, ",");
1450Sstevel@tonic-gate 		ptree_name_iter(O_ALTFP|O_VERB2|O_NONL, rhs);
1460Sstevel@tonic-gate 		out(O_ALTFP|O_VERB2, ") returned %d", (int)valuep->v);
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 		tree_free(lhs);
1490Sstevel@tonic-gate 		tree_free(rhs);
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 		return (1);
152*2318Sstephh 	} else if (funcname == L_confprop) {
153*2318Sstephh 		struct config *cp;
154*2318Sstephh 		struct node *lhs;
155*2318Sstephh 		char *path;
156*2318Sstephh 		const char *s;
157*2318Sstephh 
158*2318Sstephh 		if (np->u.expr.left->u.func.s == L_fru)
159*2318Sstephh 			lhs = eval_fru(np->u.expr.left->u.func.arglist);
160*2318Sstephh 		else if (np->u.expr.left->u.func.s == L_asru)
161*2318Sstephh 			lhs = eval_asru(np->u.expr.left->u.func.arglist);
162*2318Sstephh 		else
163*2318Sstephh 			out(O_DIE, "confprop: unexpected lhs type: %s",
164*2318Sstephh 			    ptree_nodetype2str(np->u.expr.left->t));
165*2318Sstephh 
166*2318Sstephh 		/* for now s will point to a quote [see addconfigprop()] */
167*2318Sstephh 		ASSERT(np->u.expr.right->t == T_QUOTE);
168*2318Sstephh 
169*2318Sstephh 		/* eval_dup will expand wildcards, iterators, etc... */
170*2318Sstephh 		lhs = eval_dup(lhs, ex, epnames);
171*2318Sstephh 		path = ipath2str(NULL, ipath(lhs));
172*2318Sstephh 		cp = config_lookup(croot, path, 0);
173*2318Sstephh 		tree_free(lhs);
174*2318Sstephh 		FREE((void *)path);
175*2318Sstephh 		if (cp == NULL)
176*2318Sstephh 			return (0);
177*2318Sstephh 		s = config_getprop(cp, np->u.expr.right->u.quote.s);
178*2318Sstephh 		if (s == NULL)
179*2318Sstephh 			return (0);
180*2318Sstephh 		valuep->v = (uintptr_t)stable(s);
181*2318Sstephh 		valuep->t = STRING;
182*2318Sstephh 		return (1);
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	if (try)
1860Sstevel@tonic-gate 		return (0);
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	if (funcname == L_fru) {
1890Sstevel@tonic-gate 		valuep->t = NODEPTR;
1901717Swesolows 		valuep->v = (uintptr_t)eval_fru(np);
1910Sstevel@tonic-gate 		return (1);
1920Sstevel@tonic-gate 	} else if (funcname == L_asru) {
1930Sstevel@tonic-gate 		valuep->t = NODEPTR;
1941717Swesolows 		valuep->v = (uintptr_t)eval_asru(np);
1950Sstevel@tonic-gate 		return (1);
1961414Scindi 	} else if (funcname == L_defined) {
1971414Scindi 		ASSERTeq(np->t, T_GLOBID, ptree_nodetype2str);
1981414Scindi 		valuep->t = UINT64;
1991414Scindi 		valuep->v = (lut_lookup(*globals,
2001414Scindi 		    (void *)np->u.globid.s, NULL) != NULL);
2011414Scindi 		return (1);
2020Sstevel@tonic-gate 	} else if (funcname == L_call) {
2030Sstevel@tonic-gate 		return (! platform_call(np, globals, croot, arrowp, valuep));
2040Sstevel@tonic-gate 	} else if (funcname == L_is_connected) {
2050Sstevel@tonic-gate 		return (! config_is_connected(np, croot, valuep));
2060Sstevel@tonic-gate 	} else if (funcname == L_is_on) {
2070Sstevel@tonic-gate 		return (! config_is_on(np, croot, valuep));
2080Sstevel@tonic-gate 	} else if (funcname == L_is_present) {
2090Sstevel@tonic-gate 		return (! config_is_present(np, croot, valuep));
2100Sstevel@tonic-gate 	} else if (funcname == L_is_type) {
2110Sstevel@tonic-gate 		return (! config_is_type(np, croot, valuep));
2120Sstevel@tonic-gate 	} else if (funcname == L_envprop) {
2130Sstevel@tonic-gate 		outfl(O_DIE, np->file, np->line,
2140Sstevel@tonic-gate 		    "eval_func: %s not yet supported", funcname);
2150Sstevel@tonic-gate 	} else if (funcname == L_payloadprop) {
2161414Scindi 		outfl(O_ALTFP|O_VERB2|O_NONL, np->file, np->line,
2170Sstevel@tonic-gate 		    "payloadprop(\"%s\") ", np->u.quote.s);
218186Sdb35262 
2191414Scindi 		if (platform_payloadprop(np, valuep)) {
2201414Scindi 			/* platform_payloadprop() returned false */
2211414Scindi 			out(O_ALTFP|O_VERB2, "not found.");
2221414Scindi 			return (0);
2231414Scindi 		} else {
224186Sdb35262 			switch (valuep->t) {
225186Sdb35262 			case UINT64:
226186Sdb35262 			case NODEPTR:
2271414Scindi 				out(O_ALTFP|O_VERB2, "found: %llu", valuep->v);
228186Sdb35262 				break;
229186Sdb35262 			case STRING:
2301414Scindi 				out(O_ALTFP|O_VERB2, "found: \"%s\"",
2311717Swesolows 				    (char *)(uintptr_t)valuep->v);
232186Sdb35262 				break;
233186Sdb35262 			default:
2341414Scindi 				out(O_ALTFP|O_VERB2, "found: undefined");
235186Sdb35262 				break;
236186Sdb35262 			}
2371414Scindi 			return (1);
2381414Scindi 		}
2391414Scindi 	} else if (funcname == L_setpayloadprop) {
2401414Scindi 		struct evalue *payloadvalp;
2411414Scindi 
2421414Scindi 		ASSERTinfo(np->t == T_LIST, ptree_nodetype2str(np->t));
2431414Scindi 		ASSERTinfo(np->u.expr.left->t == T_QUOTE,
2441414Scindi 		    ptree_nodetype2str(np->u.expr.left->t));
2451414Scindi 
2461414Scindi 		outfl(O_ALTFP|O_VERB2|O_NONL, np->file, np->line,
2471414Scindi 		    "setpayloadprop: %s: %s=",
2481414Scindi 		    arrowp->tail->myevent->enode->u.event.ename->u.name.s,
2491414Scindi 		    np->u.expr.left->u.quote.s);
2501414Scindi 		ptree_name_iter(O_ALTFP|O_VERB2|O_NONL, np->u.expr.right);
2511414Scindi 
2521414Scindi 		/*
2531414Scindi 		 * allocate a struct evalue to hold the payload property's
2541414Scindi 		 * value, unless we've been here already, in which case we
2551414Scindi 		 * might calculate a different value, but we'll store it
2561414Scindi 		 * in the already-allocated struct evalue.
2571414Scindi 		 */
2581414Scindi 		if ((payloadvalp = (struct evalue *)lut_lookup(
2591414Scindi 		    arrowp->tail->myevent->payloadprops,
2601414Scindi 		    (void *)np->u.expr.left->u.quote.s, NULL)) == NULL) {
2611414Scindi 			payloadvalp = MALLOC(sizeof (*payloadvalp));
2621414Scindi 		}
2631414Scindi 
2641414Scindi 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
2651414Scindi 		    arrowp, try, payloadvalp)) {
2661414Scindi 			out(O_ALTFP|O_VERB2, " (cannot eval, using zero)");
2671414Scindi 			payloadvalp->t = UINT64;
2681414Scindi 			payloadvalp->v = 0;
2691414Scindi 		} else {
2701414Scindi 			if (payloadvalp->t == UINT64)
2711414Scindi 				out(O_ALTFP|O_VERB2,
2721414Scindi 				    " (%llu)", payloadvalp->v);
2731414Scindi 			else
2741717Swesolows 				out(O_ALTFP|O_VERB2, " (\"%s\")",
2751717Swesolows 				    (char *)(uintptr_t)payloadvalp->v);
2761414Scindi 		}
2771414Scindi 
2781414Scindi 		/* add to table of payload properties for current problem */
2791414Scindi 		arrowp->tail->myevent->payloadprops =
2801414Scindi 		    lut_add(arrowp->tail->myevent->payloadprops,
2811414Scindi 		    (void *)np->u.expr.left->u.quote.s,
2821414Scindi 		    (void *)payloadvalp, NULL);
2831414Scindi 
2841414Scindi 		/* function is always true */
2851414Scindi 		valuep->t = UINT64;
2861414Scindi 		valuep->v = 1;
2871414Scindi 		return (1);
2881414Scindi 	} else if (funcname == L_payloadprop_defined) {
2891414Scindi 		outfl(O_ALTFP|O_VERB2|O_NONL, np->file, np->line,
2901414Scindi 		    "payloadprop_defined(\"%s\") ", np->u.quote.s);
2911414Scindi 
2921414Scindi 		if (platform_payloadprop(np, NULL)) {
2931414Scindi 			/* platform_payloadprop() returned false */
2941414Scindi 			valuep->v = 0;
2951414Scindi 			out(O_ALTFP|O_VERB2, "not found.");
2961414Scindi 		} else {
2971414Scindi 			valuep->v = 1;
2981414Scindi 			out(O_ALTFP|O_VERB2, "found.");
2991414Scindi 		}
3001414Scindi 		valuep->t = UINT64;
3011414Scindi 		return (1);
3021414Scindi 	} else if (funcname == L_payloadprop_contains) {
3031414Scindi 		int nvals;
3041414Scindi 		struct evalue *vals;
3051414Scindi 		struct evalue cmpval;
3061414Scindi 
3071414Scindi 		ASSERTinfo(np->t == T_LIST, ptree_nodetype2str(np->t));
3081414Scindi 		ASSERTinfo(np->u.expr.left->t == T_QUOTE,
3091414Scindi 		    ptree_nodetype2str(np->u.expr.left->t));
3101414Scindi 
3111414Scindi 		outfl(O_ALTFP|O_VERB2|O_NONL, np->file, np->line,
3121414Scindi 		    "payloadprop_contains(\"%s\", ",
3131414Scindi 		    np->u.expr.left->u.quote.s);
3141414Scindi 		ptree_name_iter(O_ALTFP|O_VERB2|O_NONL, np->u.expr.right);
3151414Scindi 		outfl(O_ALTFP|O_VERB2|O_NONL, np->file, np->line, ") ");
3161414Scindi 
3171414Scindi 		/* evaluate the expression we're comparing against */
3181414Scindi 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
3191414Scindi 		    arrowp, try, &cmpval)) {
3201414Scindi 			out(O_ALTFP|O_VERB2|O_NONL,
3211414Scindi 			    "(cannot eval, using zero) ");
3221414Scindi 			cmpval.t = UINT64;
3231414Scindi 			cmpval.v = 0;
3241414Scindi 		} else {
3251414Scindi 			if (cmpval.t == UINT64)
3261414Scindi 				out(O_ALTFP|O_VERB2,
3271414Scindi 				    "(%llu) ", cmpval.v);
3281414Scindi 			else
3291414Scindi 				out(O_ALTFP|O_VERB2,
3301717Swesolows 				    "(\"%s\") ", (char *)(uintptr_t)cmpval.v);
3311414Scindi 		}
3321414Scindi 
3331414Scindi 		/* get the payload values and check for a match */
3341414Scindi 		vals = platform_payloadprop_values(np->u.expr.left->u.quote.s,
3351414Scindi 		    &nvals);
3361414Scindi 		valuep->t = UINT64;
3371414Scindi 		valuep->v = 0;
3381414Scindi 		if (nvals == 0) {
3391414Scindi 			out(O_ALTFP|O_VERB2, "not found.");
3401414Scindi 		} else {
3411414Scindi 			struct evalue preval;
3421414Scindi 			int i;
3431414Scindi 
3441414Scindi 			out(O_ALTFP|O_VERB2|O_NONL, "found %d values ", nvals);
3451414Scindi 
3461414Scindi 			for (i = 0; i < nvals; i++) {
3471414Scindi 
3481414Scindi 				preval.t = vals[i].t;
3491414Scindi 				preval.v = vals[i].v;
3501414Scindi 
3511414Scindi 				if (check_expr_args(&vals[i], &cmpval,
3521414Scindi 				    UNDEFINED, np))
3531414Scindi 					continue;
3541414Scindi 
3551414Scindi 				/*
3561414Scindi 				 * If we auto-converted the value to a
3571414Scindi 				 * string, we need to free the
3581414Scindi 				 * original tree value.
3591414Scindi 				 */
3601414Scindi 				if (preval.t == NODEPTR &&
3611717Swesolows 				    ((struct node *)(uintptr_t)(preval.v))->t ==
3621717Swesolows 				    T_NAME) {
3631717Swesolows 					tree_free((struct node *)(uintptr_t)
3641717Swesolows 					    preval.v);
3651414Scindi 				}
3661414Scindi 
3671414Scindi 				if (vals[i].v == cmpval.v) {
3681414Scindi 					valuep->v = 1;
3691414Scindi 					break;
3701414Scindi 				}
3711414Scindi 			}
372186Sdb35262 
3731414Scindi 			if (valuep->v)
3741414Scindi 				out(O_ALTFP|O_VERB2, "match.");
3751414Scindi 			else
3761414Scindi 				out(O_ALTFP|O_VERB2, "no match.");
377186Sdb35262 
3781414Scindi 			for (i = 0; i < nvals; i++) {
3791414Scindi 				if (vals[i].t == NODEPTR) {
3801717Swesolows 					tree_free((struct node *)(uintptr_t)
3811717Swesolows 					    vals[i].v);
3821414Scindi 					break;
3831414Scindi 				}
384186Sdb35262 			}
3851414Scindi 			FREE(vals);
3861414Scindi 		}
3871414Scindi 		return (1);
3881414Scindi 	} else if (funcname == L_confcall) {
3891414Scindi 		return (!platform_confcall(np, globals, croot, arrowp, valuep));
3901414Scindi 	} else if (funcname == L_count) {
3911414Scindi 		struct stats *statp;
3921423Sgavinm 		struct istat_entry ent;
393186Sdb35262 
3941414Scindi 		ASSERTinfo(np->t == T_EVENT, ptree_nodetype2str(np->t));
3951414Scindi 
3961423Sgavinm 		ent.ename = np->u.event.ename->u.name.s;
3971423Sgavinm 		ent.ipath = ipath(np->u.event.epname);
3981423Sgavinm 
3991414Scindi 		valuep->t = UINT64;
4001414Scindi 		if ((statp = (struct stats *)
4011423Sgavinm 		    lut_lookup(Istats, &ent, (lut_cmp)istat_cmp)) == NULL)
4021414Scindi 			valuep->v = 0;
4031414Scindi 		else
4041414Scindi 			valuep->v = stats_counter_value(statp);
4051414Scindi 
4061414Scindi 		return (1);
4070Sstevel@tonic-gate 	} else
4080Sstevel@tonic-gate 		outfl(O_DIE, np->file, np->line,
4090Sstevel@tonic-gate 		    "eval_func: unexpected func: %s", funcname);
4100Sstevel@tonic-gate 	/*NOTREACHED*/
4111717Swesolows 	return (0);
4120Sstevel@tonic-gate }
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate static struct node *
4150Sstevel@tonic-gate eval_wildcardedname(struct node *np, struct lut *ex, struct node *epnames[])
4160Sstevel@tonic-gate {
4170Sstevel@tonic-gate 	struct node *npstart, *npend, *npref, *newnp;
4180Sstevel@tonic-gate 	struct node *np1, *np2, *retp;
4190Sstevel@tonic-gate 	int i;
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	if (epnames == NULL || epnames[0] == NULL)
4220Sstevel@tonic-gate 		return (NULL);
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	for (i = 0; epnames[i] != NULL; i++) {
4250Sstevel@tonic-gate 		if (tree_namecmp(np, epnames[i]) == 0)
4260Sstevel@tonic-gate 			return (NULL);
4270Sstevel@tonic-gate 	}
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	/*
4300Sstevel@tonic-gate 	 * get to this point if np does not match any of the entries in
4310Sstevel@tonic-gate 	 * epnames.  check if np is a path that must preceded by a wildcard
4320Sstevel@tonic-gate 	 * portion.  for this case we must first determine which epnames[]
4330Sstevel@tonic-gate 	 * entry should be used for wildcarding.
4340Sstevel@tonic-gate 	 */
4350Sstevel@tonic-gate 	npstart = NULL;
4360Sstevel@tonic-gate 	for (i = 0; epnames[i] != NULL; i++) {
4370Sstevel@tonic-gate 		for (npref = epnames[i]; npref; npref = npref->u.name.next) {
4380Sstevel@tonic-gate 			if (npref->u.name.s == np->u.name.s) {
4390Sstevel@tonic-gate 				for (np1 = npref, np2 = np;
4400Sstevel@tonic-gate 				    np1 != NULL && np2 != NULL;
4410Sstevel@tonic-gate 				    np1 = np1->u.name.next,
4420Sstevel@tonic-gate 					    np2 = np2->u.name.next) {
4430Sstevel@tonic-gate 					if (np1->u.name.s != np2->u.name.s)
4440Sstevel@tonic-gate 						break;
4450Sstevel@tonic-gate 				}
4460Sstevel@tonic-gate 				if (np2 == NULL) {
4470Sstevel@tonic-gate 					npstart = epnames[i];
4480Sstevel@tonic-gate 					npend = npref;
4490Sstevel@tonic-gate 					if (np1 == NULL)
4500Sstevel@tonic-gate 						break;
4510Sstevel@tonic-gate 				}
4520Sstevel@tonic-gate 			}
4530Sstevel@tonic-gate 		}
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 		if (npstart != NULL)
4560Sstevel@tonic-gate 			break;
4570Sstevel@tonic-gate 	}
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	if (npstart == NULL) {
4600Sstevel@tonic-gate 		/* no match; np is not a path to be wildcarded */
4610Sstevel@tonic-gate 		return (NULL);
4620Sstevel@tonic-gate 	}
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	/*
4650Sstevel@tonic-gate 	 * dup (npstart -- npend) which is the wildcarded portion.  all
4660Sstevel@tonic-gate 	 * children should be T_NUMs.
4670Sstevel@tonic-gate 	 */
4680Sstevel@tonic-gate 	retp = NULL;
4690Sstevel@tonic-gate 	for (npref = npstart;
4700Sstevel@tonic-gate 	    ! (npref == NULL || npref == npend);
4710Sstevel@tonic-gate 	    npref = npref->u.name.next) {
4720Sstevel@tonic-gate 		newnp = newnode(T_NAME, np->file, np->line);
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 		newnp->u.name.t = npref->u.name.t;
4750Sstevel@tonic-gate 		newnp->u.name.s = npref->u.name.s;
4760Sstevel@tonic-gate 		newnp->u.name.last = newnp;
4770Sstevel@tonic-gate 		newnp->u.name.it = npref->u.name.it;
4780Sstevel@tonic-gate 		newnp->u.name.cp = npref->u.name.cp;
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 		ASSERT(npref->u.name.child != NULL);
4810Sstevel@tonic-gate 		ASSERT(npref->u.name.child->t == T_NUM);
4820Sstevel@tonic-gate 		newnp->u.name.child = newnode(T_NUM, np->file, np->line);
4830Sstevel@tonic-gate 		newnp->u.name.child->u.ull = npref->u.name.child->u.ull;
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 		if (retp == NULL) {
4860Sstevel@tonic-gate 			retp = newnp;
4870Sstevel@tonic-gate 		} else {
4880Sstevel@tonic-gate 			retp->u.name.last->u.name.next = newnp;
4890Sstevel@tonic-gate 			retp->u.name.last = newnp;
4900Sstevel@tonic-gate 		}
4910Sstevel@tonic-gate 	}
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	ASSERT(retp != NULL);
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	/* now append the nonwildcarded portion */
4960Sstevel@tonic-gate 	retp = tree_name_append(retp, eval_dup(np, ex, NULL));
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	return (retp);
4990Sstevel@tonic-gate }
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate static struct node *
5020Sstevel@tonic-gate eval_dup(struct node *np, struct lut *ex, struct node *epnames[])
5030Sstevel@tonic-gate {
5040Sstevel@tonic-gate 	struct node *newnp;
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	if (np == NULL)
5070Sstevel@tonic-gate 		return (NULL);
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	switch (np->t) {
5100Sstevel@tonic-gate 	case T_GLOBID:
5110Sstevel@tonic-gate 		return (tree_globid(np->u.globid.s, np->file, np->line));
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	case T_ASSIGN:
5140Sstevel@tonic-gate 	case T_CONDIF:
5150Sstevel@tonic-gate 	case T_CONDELSE:
5160Sstevel@tonic-gate 	case T_NE:
5170Sstevel@tonic-gate 	case T_EQ:
5180Sstevel@tonic-gate 	case T_LT:
5190Sstevel@tonic-gate 	case T_LE:
5200Sstevel@tonic-gate 	case T_GT:
5210Sstevel@tonic-gate 	case T_GE:
5220Sstevel@tonic-gate 	case T_BITAND:
5230Sstevel@tonic-gate 	case T_BITOR:
5240Sstevel@tonic-gate 	case T_BITXOR:
5250Sstevel@tonic-gate 	case T_BITNOT:
5260Sstevel@tonic-gate 	case T_LSHIFT:
5270Sstevel@tonic-gate 	case T_RSHIFT:
5280Sstevel@tonic-gate 	case T_LIST:
5290Sstevel@tonic-gate 	case T_AND:
5300Sstevel@tonic-gate 	case T_OR:
5310Sstevel@tonic-gate 	case T_NOT:
5320Sstevel@tonic-gate 	case T_ADD:
5330Sstevel@tonic-gate 	case T_SUB:
5340Sstevel@tonic-gate 	case T_MUL:
5350Sstevel@tonic-gate 	case T_DIV:
5360Sstevel@tonic-gate 	case T_MOD:
5370Sstevel@tonic-gate 		return (tree_expr(np->t,
5380Sstevel@tonic-gate 				    eval_dup(np->u.expr.left, ex, epnames),
5390Sstevel@tonic-gate 				    eval_dup(np->u.expr.right, ex, epnames)));
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	case T_NAME: {
5420Sstevel@tonic-gate 		struct iterinfo *iterinfop;
5430Sstevel@tonic-gate 		struct node *newchild = NULL;
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 		iterinfop = lut_lookup(ex, (void *)np->u.name.s, NULL);
5460Sstevel@tonic-gate 		if (iterinfop != NULL) {
5470Sstevel@tonic-gate 			/* explicit iterator; not part of pathname */
5480Sstevel@tonic-gate 			newnp = newnode(T_NUM, np->file, np->line);
5490Sstevel@tonic-gate 			newnp->u.ull = iterinfop->num;
5500Sstevel@tonic-gate 			return (newnp);
5510Sstevel@tonic-gate 		}
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 		/* see if np is a path with wildcard portion */
5540Sstevel@tonic-gate 		newnp = eval_wildcardedname(np, ex, epnames);
5550Sstevel@tonic-gate 		if (newnp != NULL)
5560Sstevel@tonic-gate 			return (newnp);
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 		/* turn off wildcarding for child */
5590Sstevel@tonic-gate 		newchild = eval_dup(np->u.name.child, ex, NULL);
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 		if (newchild != NULL) {
5620Sstevel@tonic-gate 			if (newchild->t != T_NUM) {
5630Sstevel@tonic-gate 				/*
5640Sstevel@tonic-gate 				 * not a number, eh?  we must resolve this
5650Sstevel@tonic-gate 				 * to a number.
5660Sstevel@tonic-gate 				 */
5670Sstevel@tonic-gate 				struct evalue value;
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 				if (eval_expr(newchild, ex, epnames,
5700Sstevel@tonic-gate 				    NULL, NULL, NULL, 1, &value) == 0 ||
5710Sstevel@tonic-gate 				    value.t != UINT64) {
5720Sstevel@tonic-gate 					outfl(O_DIE, np->file, np->line,
5730Sstevel@tonic-gate 					    "eval_dup: could not resolve "
5740Sstevel@tonic-gate 					    "iterator of %s", np->u.name.s);
5750Sstevel@tonic-gate 				}
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 				tree_free(newchild);
5780Sstevel@tonic-gate 				newchild = newnode(T_NUM, np->file, np->line);
5790Sstevel@tonic-gate 				newchild->u.ull = value.v;
5800Sstevel@tonic-gate 			}
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 			newnp = newnode(np->t, np->file, np->line);
5830Sstevel@tonic-gate 			newnp->u.name.s = np->u.name.s;
5840Sstevel@tonic-gate 			newnp->u.name.it = np->u.name.it;
5850Sstevel@tonic-gate 			newnp->u.name.cp = np->u.name.cp;
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 			newnp->u.name.last = newnp;
5880Sstevel@tonic-gate 			newnp->u.name.child = newchild;
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 			if (np->u.name.next != NULL) {
5910Sstevel@tonic-gate 				/* turn off wildcarding for next */
5920Sstevel@tonic-gate 				return (tree_name_append(newnp,
5930Sstevel@tonic-gate 					eval_dup(np->u.name.next, ex, NULL)));
5940Sstevel@tonic-gate 			} else {
5950Sstevel@tonic-gate 				return (newnp);
5960Sstevel@tonic-gate 			}
5970Sstevel@tonic-gate 		} else {
5980Sstevel@tonic-gate 			outfl(O_DIE, np->file, np->line,
5990Sstevel@tonic-gate 			    "eval_dup: internal error: \"%s\" is neither "
6000Sstevel@tonic-gate 			    "an iterator nor a pathname", np->u.name.s);
6010Sstevel@tonic-gate 		}
6020Sstevel@tonic-gate 		/*NOTREACHED*/
6030Sstevel@tonic-gate 		break;
6040Sstevel@tonic-gate 	}
6050Sstevel@tonic-gate 
6061414Scindi 	case T_EVENT:
6071414Scindi 		newnp = newnode(T_NAME, np->file, np->line);
6081414Scindi 
6091414Scindi 		newnp->u.name.t = np->u.event.ename->u.name.t;
6101414Scindi 		newnp->u.name.s = np->u.event.ename->u.name.s;
6111414Scindi 		newnp->u.name.it = np->u.event.ename->u.name.it;
6121414Scindi 		newnp->u.name.last = newnp;
6131414Scindi 
6141414Scindi 		return (tree_event(newnp,
6151414Scindi 		    eval_dup(np->u.event.epname, ex, epnames),
6161414Scindi 		    eval_dup(np->u.event.eexprlist, ex, epnames)));
6171414Scindi 
6180Sstevel@tonic-gate 	case T_FUNC:
6190Sstevel@tonic-gate 		return (tree_func(np->u.func.s,
6200Sstevel@tonic-gate 		    eval_dup(np->u.func.arglist, ex, epnames),
6210Sstevel@tonic-gate 		    np->file, np->line));
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	case T_QUOTE:
6240Sstevel@tonic-gate 		newnp = newnode(T_QUOTE, np->file, np->line);
6250Sstevel@tonic-gate 		newnp->u.quote.s = np->u.quote.s;
6260Sstevel@tonic-gate 		return (newnp);
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	case T_NUM:
6290Sstevel@tonic-gate 		newnp = newnode(T_NUM, np->file, np->line);
6300Sstevel@tonic-gate 		newnp->u.ull = np->u.ull;
6310Sstevel@tonic-gate 		return (newnp);
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	default:
6340Sstevel@tonic-gate 		outfl(O_DIE, np->file, np->line,
6350Sstevel@tonic-gate 		    "eval_dup: unexpected node type: %s",
6360Sstevel@tonic-gate 		    ptree_nodetype2str(np->t));
6370Sstevel@tonic-gate 	}
6380Sstevel@tonic-gate 	/*NOTREACHED*/
6391717Swesolows 	return (0);
6400Sstevel@tonic-gate }
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate /*
6430Sstevel@tonic-gate  * eval_potential -- see if constraint is potentially true
6440Sstevel@tonic-gate  *
6450Sstevel@tonic-gate  * this function is used at instance tree creation time to see if
6460Sstevel@tonic-gate  * any constraints are already known to be false.  if this function
6470Sstevel@tonic-gate  * returns false, then the constraint will always be false and there's
6480Sstevel@tonic-gate  * no need to include the propagation arrow in the instance tree.
6490Sstevel@tonic-gate  *
6500Sstevel@tonic-gate  * if this routine returns true, either the constraint is known to
6510Sstevel@tonic-gate  * be always true (so there's no point in attaching the constraint
6520Sstevel@tonic-gate  * to the propagation arrow in the instance tree), or the constraint
6530Sstevel@tonic-gate  * contains "deferred" expressions like global variables or poller calls
6540Sstevel@tonic-gate  * and so it must be evaluated during calls to fme_eval().  in this last
6550Sstevel@tonic-gate  * case, where a constraint needs to be attached to the propagation arrow
6560Sstevel@tonic-gate  * in the instance tree, this routine returns a newly created constraint
6570Sstevel@tonic-gate  * in *newc where all the non-deferred things have been filled in.
6580Sstevel@tonic-gate  *
6590Sstevel@tonic-gate  * so in summary:
6600Sstevel@tonic-gate  *
6610Sstevel@tonic-gate  *	return of false: constraint can never be true, *newc will be NULL.
6620Sstevel@tonic-gate  *
6630Sstevel@tonic-gate  *	return of true with *newc unchanged: constraint will always be true.
6640Sstevel@tonic-gate  *
6650Sstevel@tonic-gate  *	return of true with *newc changed: use new constraint in *newc.
6660Sstevel@tonic-gate  *
6670Sstevel@tonic-gate  * the lookup table for all explicit iterators, ex, is passed in.
6680Sstevel@tonic-gate  *
6690Sstevel@tonic-gate  * *newc can either be NULL on entry, or if can contain constraints from
6700Sstevel@tonic-gate  * previous calls to eval_potential() (i.e. for building up an instance
6710Sstevel@tonic-gate  * tree constraint from several potential constraints).  if *newc already
6720Sstevel@tonic-gate  * contains constraints, anything added to it will be joined by adding
6730Sstevel@tonic-gate  * a T_AND node at the top of *newc.
6740Sstevel@tonic-gate  */
6750Sstevel@tonic-gate int
6760Sstevel@tonic-gate eval_potential(struct node *np, struct lut *ex, struct node *epnames[],
677*2318Sstephh 	    struct node **newc, struct config *croot)
6780Sstevel@tonic-gate {
6790Sstevel@tonic-gate 	struct node *newnp;
6800Sstevel@tonic-gate 	struct evalue value;
6810Sstevel@tonic-gate 
682*2318Sstephh 	if (eval_expr(np, ex, epnames, NULL, croot, NULL, 1, &value) == 0) {
6830Sstevel@tonic-gate 		/*
6840Sstevel@tonic-gate 		 * couldn't eval expression because
6850Sstevel@tonic-gate 		 * it contains deferred items.  make
6860Sstevel@tonic-gate 		 * a duplicate expression with all the
6870Sstevel@tonic-gate 		 * non-deferred items expanded.
6880Sstevel@tonic-gate 		 */
6890Sstevel@tonic-gate 		newnp = eval_dup(np, ex, epnames);
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 		if (*newc == NULL) {
6920Sstevel@tonic-gate 			/*
6930Sstevel@tonic-gate 			 * constraint is potentially true if deferred
6940Sstevel@tonic-gate 			 * expression in newnp is true.  *newc was NULL
6950Sstevel@tonic-gate 			 * so new constraint is just the one in newnp.
6960Sstevel@tonic-gate 			 */
6970Sstevel@tonic-gate 			*newc = newnp;
6980Sstevel@tonic-gate 			return (1);
6990Sstevel@tonic-gate 		} else {
7000Sstevel@tonic-gate 			/*
7010Sstevel@tonic-gate 			 * constraint is potentially true if deferred
7020Sstevel@tonic-gate 			 * expression in newnp is true.  *newc already
7030Sstevel@tonic-gate 			 * contained a constraint so add an AND with the
7040Sstevel@tonic-gate 			 * constraint in newnp.
7050Sstevel@tonic-gate 			 */
7060Sstevel@tonic-gate 			*newc = tree_expr(T_AND, *newc, newnp);
7070Sstevel@tonic-gate 			return (1);
7080Sstevel@tonic-gate 		}
7090Sstevel@tonic-gate 	} else if (value.t == UNDEFINED) {
7100Sstevel@tonic-gate 		/* constraint can never be true */
7110Sstevel@tonic-gate 		return (0);
7120Sstevel@tonic-gate 	} else if (value.t == UINT64 && value.v == 0) {
7130Sstevel@tonic-gate 		/* constraint can never be true */
7140Sstevel@tonic-gate 		return (0);
7150Sstevel@tonic-gate 	} else {
7160Sstevel@tonic-gate 		/* constraint is always true (nothing deferred to eval) */
7170Sstevel@tonic-gate 		return (1);
7180Sstevel@tonic-gate 	}
7190Sstevel@tonic-gate }
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate static int
7220Sstevel@tonic-gate check_expr_args(struct evalue *lp, struct evalue *rp, enum datatype dtype,
7230Sstevel@tonic-gate 		struct node *np)
7240Sstevel@tonic-gate {
7251414Scindi 	/* auto-convert T_NAMES to strings */
7261717Swesolows 	if (lp->t == NODEPTR && ((struct node *)(uintptr_t)(lp->v))->t ==
7271717Swesolows 	    T_NAME) {
7281717Swesolows 		char *s = ipath2str(NULL,
7291717Swesolows 		    ipath((struct node *)(uintptr_t)lp->v));
7301414Scindi 		lp->t = STRING;
7311717Swesolows 		lp->v = (uintptr_t)stable(s);
7321414Scindi 		FREE(s);
7331414Scindi 		out(O_ALTFP|O_VERB2, "convert lhs path to \"%s\"",
7341717Swesolows 		    (char *)(uintptr_t)lp->v);
7351414Scindi 	}
7361414Scindi 	if (rp != NULL &&
7371717Swesolows 	    rp->t == NODEPTR && ((struct node *)(uintptr_t)(rp->v))->t ==
7381717Swesolows 	    T_NAME) {
7391717Swesolows 		char *s = ipath2str(NULL,
7401717Swesolows 		    ipath((struct node *)(uintptr_t)rp->v));
7411414Scindi 		rp->t = STRING;
7421717Swesolows 		rp->v = (uintptr_t)stable(s);
7431414Scindi 		FREE(s);
7441414Scindi 		out(O_ALTFP|O_VERB2, "convert rhs path to \"%s\"",
7451717Swesolows 		    (char *)(uintptr_t)rp->v);
7461414Scindi 	}
7471414Scindi 
7481414Scindi 	/* auto-convert strings to numbers */
7491414Scindi 	if (dtype == UINT64) {
7501414Scindi 		if (lp->t == STRING) {
7511414Scindi 			lp->t = UINT64;
7521717Swesolows 			lp->v = strtoull((char *)(uintptr_t)lp->v, NULL, 0);
7531414Scindi 		}
7541414Scindi 		if (rp != NULL && rp->t == STRING) {
7551414Scindi 			rp->t = UINT64;
7561717Swesolows 			rp->v = strtoull((char *)(uintptr_t)rp->v, NULL, 0);
7571414Scindi 		}
7581414Scindi 	}
7591414Scindi 
7600Sstevel@tonic-gate 	if (dtype != UNDEFINED && lp->t != dtype) {
7610Sstevel@tonic-gate 		outfl(O_OK, np->file, np->line,
7620Sstevel@tonic-gate 			"invalid datatype of argument for operation %s",
7630Sstevel@tonic-gate 			ptree_nodetype2str(np->t));
7640Sstevel@tonic-gate 		return (1);
7650Sstevel@tonic-gate 	}
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	if (rp != NULL && lp->t != rp->t) {
7680Sstevel@tonic-gate 		outfl(O_OK, np->file, np->line,
7690Sstevel@tonic-gate 			"mismatch in datatype of arguments for operation %s",
7700Sstevel@tonic-gate 			ptree_nodetype2str(np->t));
7710Sstevel@tonic-gate 		return (1);
7720Sstevel@tonic-gate 	}
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 	return (0);
7750Sstevel@tonic-gate }
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate /*
7780Sstevel@tonic-gate  * eval_expr -- evaluate expression into *valuep
7790Sstevel@tonic-gate  *
7800Sstevel@tonic-gate  * the meaning of the return value depends on the input value of try.
7810Sstevel@tonic-gate  *
7820Sstevel@tonic-gate  * for try == 1: if any deferred items are encounted, bail out and return
7830Sstevel@tonic-gate  * false.  returns true if we made it through entire expression without
7840Sstevel@tonic-gate  * hitting any deferred items.
7850Sstevel@tonic-gate  *
7860Sstevel@tonic-gate  * for try == 0: return true if all operations were performed successfully.
7870Sstevel@tonic-gate  * return false if otherwise.  for example, any of the following conditions
7880Sstevel@tonic-gate  * will result in a false return value:
7890Sstevel@tonic-gate  *   - attempted use of an uninitialized global variable
7900Sstevel@tonic-gate  *   - failure in function evaluation
7910Sstevel@tonic-gate  *   - illegal arithmetic operation (argument out of range)
7920Sstevel@tonic-gate  */
7930Sstevel@tonic-gate int
7940Sstevel@tonic-gate eval_expr(struct node *np, struct lut *ex, struct node *epnames[],
7950Sstevel@tonic-gate 	struct lut **globals, struct config *croot, struct arrow *arrowp,
7960Sstevel@tonic-gate 	int try, struct evalue *valuep)
7970Sstevel@tonic-gate {
7980Sstevel@tonic-gate 	struct evalue *gval;
7990Sstevel@tonic-gate 	struct evalue lval;
8000Sstevel@tonic-gate 	struct evalue rval;
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 	if (np == NULL) {
8030Sstevel@tonic-gate 		valuep->t = UINT64;
8040Sstevel@tonic-gate 		valuep->v = 1;	/* no constraint means "true" */
8050Sstevel@tonic-gate 		return (1);
8060Sstevel@tonic-gate 	}
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	valuep->t = UNDEFINED;
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	switch (np->t) {
8110Sstevel@tonic-gate 	case T_GLOBID:
8120Sstevel@tonic-gate 		if (try)
8130Sstevel@tonic-gate 			return (0);
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 		/*
8160Sstevel@tonic-gate 		 * only handle case of getting (and not setting) the value
8170Sstevel@tonic-gate 		 * of a global variable
8180Sstevel@tonic-gate 		 */
8190Sstevel@tonic-gate 		gval = lut_lookup(*globals, (void *)np->u.globid.s, NULL);
8200Sstevel@tonic-gate 		if (gval == NULL) {
8210Sstevel@tonic-gate 			valuep->t = UNDEFINED;
8220Sstevel@tonic-gate 			return (0);
8230Sstevel@tonic-gate 		} else {
8240Sstevel@tonic-gate 			valuep->t = gval->t;
8250Sstevel@tonic-gate 			valuep->v = gval->v;
8260Sstevel@tonic-gate 			return (1);
8270Sstevel@tonic-gate 		}
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	case T_ASSIGN:
8300Sstevel@tonic-gate 		if (try)
8310Sstevel@tonic-gate 			return (0);
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 		/*
8340Sstevel@tonic-gate 		 * first evaluate rhs, then try to store value in lhs which
8350Sstevel@tonic-gate 		 * should be a global variable
8360Sstevel@tonic-gate 		 */
8370Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
8380Sstevel@tonic-gate 			    arrowp, try, &rval))
8390Sstevel@tonic-gate 			return (0);
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 		ASSERT(np->u.expr.left->t == T_GLOBID);
8420Sstevel@tonic-gate 		gval = lut_lookup(*globals,
8430Sstevel@tonic-gate 				(void *)np->u.expr.left->u.globid.s, NULL);
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 		if (gval == NULL) {
8460Sstevel@tonic-gate 			gval = MALLOC(sizeof (*gval));
8470Sstevel@tonic-gate 			*globals = lut_add(*globals,
8480Sstevel@tonic-gate 					(void *) np->u.expr.left->u.globid.s,
8490Sstevel@tonic-gate 					gval, NULL);
8500Sstevel@tonic-gate 		}
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 		gval->t = rval.t;
8530Sstevel@tonic-gate 		gval->v = rval.v;
8541414Scindi 
8551414Scindi 		if (gval->t == UINT64) {
8561414Scindi 			out(O_ALTFP|O_VERB2,
8571414Scindi 			    "assign $%s=%llu",
8581414Scindi 			    np->u.expr.left->u.globid.s, gval->v);
8591414Scindi 		} else {
8601414Scindi 			out(O_ALTFP|O_VERB2,
8611414Scindi 			    "assign $%s=\"%s\"",
8621717Swesolows 			    np->u.expr.left->u.globid.s,
8631717Swesolows 			    (char *)(uintptr_t)gval->v);
8641414Scindi 		}
8651414Scindi 
8661414Scindi 		/*
8671414Scindi 		 * but always return true -- an assignment should not
8681414Scindi 		 * cause a constraint to be false.
8691414Scindi 		 */
8701414Scindi 		valuep->t = UINT64;
8711414Scindi 		valuep->v = 1;
8720Sstevel@tonic-gate 		return (1);
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	case T_EQ:
8750Sstevel@tonic-gate #define	IMPLICIT_ASSIGN_IN_EQ
8760Sstevel@tonic-gate #ifdef IMPLICIT_ASSIGN_IN_EQ
8770Sstevel@tonic-gate 		/*
8780Sstevel@tonic-gate 		 * if lhs is an uninitialized global variable, perform
8790Sstevel@tonic-gate 		 * an assignment.
8800Sstevel@tonic-gate 		 *
8810Sstevel@tonic-gate 		 * one insidious side effect of implicit assignment is
8820Sstevel@tonic-gate 		 * that the "==" operator does not return a Boolean if
8830Sstevel@tonic-gate 		 * implicit assignment was performed.
8840Sstevel@tonic-gate 		 */
8850Sstevel@tonic-gate 		if (try == 0 &&
8860Sstevel@tonic-gate 		    np->u.expr.left->t == T_GLOBID &&
8870Sstevel@tonic-gate 		    (gval = lut_lookup(*globals,
8880Sstevel@tonic-gate 			(void *)np->u.expr.left->u.globid.s, NULL)) == NULL) {
8890Sstevel@tonic-gate 			if (!eval_expr(np->u.expr.right, ex, epnames, globals,
8900Sstevel@tonic-gate 					croot, arrowp, try, &rval))
8910Sstevel@tonic-gate 				return (0);
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 			gval = MALLOC(sizeof (*gval));
8940Sstevel@tonic-gate 			*globals = lut_add(*globals,
8950Sstevel@tonic-gate 					(void *) np->u.expr.left->u.globid.s,
8960Sstevel@tonic-gate 					gval, NULL);
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 			gval->t = rval.t;
8990Sstevel@tonic-gate 			gval->v = rval.v;
9000Sstevel@tonic-gate 			valuep->t = rval.t;
9010Sstevel@tonic-gate 			valuep->v = rval.v;
9020Sstevel@tonic-gate 			return (1);
9030Sstevel@tonic-gate 		}
9040Sstevel@tonic-gate #endif  /* IMPLICIT_ASSIGN_IN_EQ */
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
9070Sstevel@tonic-gate 				arrowp, try, &lval))
9080Sstevel@tonic-gate 			return (0);
9090Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
9100Sstevel@tonic-gate 				arrowp, try, &rval))
9110Sstevel@tonic-gate 			return (0);
9120Sstevel@tonic-gate 		if (check_expr_args(&lval, &rval, UNDEFINED, np))
9130Sstevel@tonic-gate 			return (0);
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 		valuep->t = UINT64;
9160Sstevel@tonic-gate 		valuep->v = (lval.v == rval.v);
9170Sstevel@tonic-gate 		return (1);
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 	case T_LT:
9200Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
9210Sstevel@tonic-gate 				arrowp, try, &lval))
9220Sstevel@tonic-gate 			return (0);
9230Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
9240Sstevel@tonic-gate 				arrowp, try, &rval))
9250Sstevel@tonic-gate 			return (0);
9261414Scindi 		if (check_expr_args(&lval, &rval, UINT64, np))
9270Sstevel@tonic-gate 			return (0);
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 		valuep->t = UINT64;
9300Sstevel@tonic-gate 		valuep->v = (lval.v < rval.v);
9310Sstevel@tonic-gate 		return (1);
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 	case T_LE:
9340Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
9350Sstevel@tonic-gate 				arrowp, try, &lval))
9360Sstevel@tonic-gate 			return (0);
9370Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
9380Sstevel@tonic-gate 				arrowp, try, &rval))
9390Sstevel@tonic-gate 			return (0);
9401414Scindi 		if (check_expr_args(&lval, &rval, UINT64, np))
9410Sstevel@tonic-gate 			return (0);
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 		valuep->t = UINT64;
9440Sstevel@tonic-gate 		valuep->v = (lval.v <= rval.v);
9450Sstevel@tonic-gate 		return (1);
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 	case T_GT:
9480Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
9490Sstevel@tonic-gate 				arrowp, try, &lval))
9500Sstevel@tonic-gate 			return (0);
9510Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
9520Sstevel@tonic-gate 				arrowp, try, &rval))
9530Sstevel@tonic-gate 			return (0);
9541414Scindi 		if (check_expr_args(&lval, &rval, UINT64, np))
9550Sstevel@tonic-gate 			return (0);
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 		valuep->t = UINT64;
9580Sstevel@tonic-gate 		valuep->v = (lval.v > rval.v);
9590Sstevel@tonic-gate 		return (1);
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 	case T_GE:
9620Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
9630Sstevel@tonic-gate 				arrowp, try, &lval))
9640Sstevel@tonic-gate 			return (0);
9650Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
9660Sstevel@tonic-gate 				arrowp, try, &rval))
9670Sstevel@tonic-gate 			return (0);
9681414Scindi 		if (check_expr_args(&lval, &rval, UINT64, np))
9690Sstevel@tonic-gate 			return (0);
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 		valuep->t = UINT64;
9720Sstevel@tonic-gate 		valuep->v = (lval.v >= rval.v);
9730Sstevel@tonic-gate 		return (1);
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 	case T_BITAND:
9760Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
9770Sstevel@tonic-gate 				arrowp, try, &lval))
9780Sstevel@tonic-gate 			return (0);
9790Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
9800Sstevel@tonic-gate 				arrowp, try, &rval))
9810Sstevel@tonic-gate 			return (0);
9820Sstevel@tonic-gate 		if (check_expr_args(&lval, &rval, UINT64, np))
9830Sstevel@tonic-gate 			return (0);
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 		valuep->t = lval.t;
9860Sstevel@tonic-gate 		valuep->v = (lval.v & rval.v);
9870Sstevel@tonic-gate 		return (1);
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 	case T_BITOR:
9900Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
9910Sstevel@tonic-gate 				arrowp, try, &lval))
9920Sstevel@tonic-gate 			return (0);
9930Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
9940Sstevel@tonic-gate 				arrowp, try, &rval))
9950Sstevel@tonic-gate 			return (0);
9960Sstevel@tonic-gate 		if (check_expr_args(&lval, &rval, UINT64, np))
9970Sstevel@tonic-gate 			return (0);
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate 		valuep->t = lval.t;
10000Sstevel@tonic-gate 		valuep->v = (lval.v | rval.v);
10010Sstevel@tonic-gate 		return (1);
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 	case T_BITXOR:
10040Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
10050Sstevel@tonic-gate 				arrowp, try, &lval))
10060Sstevel@tonic-gate 			return (0);
10070Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
10080Sstevel@tonic-gate 				arrowp, try, &rval))
10090Sstevel@tonic-gate 			return (0);
10100Sstevel@tonic-gate 		if (check_expr_args(&lval, &rval, UINT64, np))
10110Sstevel@tonic-gate 			return (0);
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 		valuep->t = lval.t;
10140Sstevel@tonic-gate 		valuep->v = (lval.v ^ rval.v);
10150Sstevel@tonic-gate 		return (1);
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 	case T_BITNOT:
10180Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
10190Sstevel@tonic-gate 				arrowp, try, &lval))
10200Sstevel@tonic-gate 			return (0);
10210Sstevel@tonic-gate 		ASSERT(np->u.expr.right == NULL);
10220Sstevel@tonic-gate 		if (check_expr_args(&lval, NULL, UINT64, np))
10230Sstevel@tonic-gate 			return (0);
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 		valuep->t = UINT64;
10260Sstevel@tonic-gate 		valuep->v = ~ lval.v;
10270Sstevel@tonic-gate 		return (1);
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 	case T_LSHIFT:
10300Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
10310Sstevel@tonic-gate 				arrowp, try, &lval))
10320Sstevel@tonic-gate 			return (0);
10330Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
10340Sstevel@tonic-gate 				arrowp, try, &rval))
10350Sstevel@tonic-gate 			return (0);
10360Sstevel@tonic-gate 		if (check_expr_args(&lval, &rval, UINT64, np))
10370Sstevel@tonic-gate 			return (0);
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 		valuep->t = UINT64;
10400Sstevel@tonic-gate 		valuep->v = (lval.v << rval.v);
10410Sstevel@tonic-gate 		return (1);
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	case T_RSHIFT:
10440Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
10450Sstevel@tonic-gate 				arrowp, try, &lval))
10460Sstevel@tonic-gate 			return (0);
10470Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
10480Sstevel@tonic-gate 				arrowp, try, &rval))
10490Sstevel@tonic-gate 			return (0);
10500Sstevel@tonic-gate 		if (check_expr_args(&lval, &rval, UINT64, np))
10510Sstevel@tonic-gate 			return (0);
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 		valuep->t = UINT64;
10540Sstevel@tonic-gate 		valuep->v = (lval.v >> rval.v);
10550Sstevel@tonic-gate 		return (1);
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 	case T_CONDIF: {
10580Sstevel@tonic-gate 		struct node *retnp;
10590Sstevel@tonic-gate 		int dotrue = 0;
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 		/*
10620Sstevel@tonic-gate 		 * evaluate
10630Sstevel@tonic-gate 		 *	expression ? stmtA [ : stmtB ]
10640Sstevel@tonic-gate 		 *
10650Sstevel@tonic-gate 		 * first see if expression is true or false, then determine
10660Sstevel@tonic-gate 		 * if stmtA (or stmtB, if it exists) should be evaluated.
10670Sstevel@tonic-gate 		 *
10680Sstevel@tonic-gate 		 * "dotrue = 1" means stmtA should be evaluated.
10690Sstevel@tonic-gate 		 */
10700Sstevel@tonic-gate 		if (eval_expr(np->u.expr.left, ex, epnames, globals, croot,
10710Sstevel@tonic-gate 				arrowp, try, &lval) &&
10720Sstevel@tonic-gate 		    lval.t != UNDEFINED && lval.v != 0)
10730Sstevel@tonic-gate 			dotrue = 1;
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 		ASSERT(np->u.expr.right != NULL);
10760Sstevel@tonic-gate 		if (np->u.expr.right->t == T_CONDELSE) {
10770Sstevel@tonic-gate 			if (dotrue)
10780Sstevel@tonic-gate 				retnp = np->u.expr.right->u.expr.left;
10790Sstevel@tonic-gate 			else
10800Sstevel@tonic-gate 				retnp = np->u.expr.right->u.expr.right;
10810Sstevel@tonic-gate 		} else {
10820Sstevel@tonic-gate 			/* no ELSE clause */
10830Sstevel@tonic-gate 			if (dotrue)
10840Sstevel@tonic-gate 				retnp = np->u.expr.right;
10850Sstevel@tonic-gate 			else {
10860Sstevel@tonic-gate 				valuep->t = UINT64;
10870Sstevel@tonic-gate 				valuep->v = 0;
10880Sstevel@tonic-gate 				return (0);
10890Sstevel@tonic-gate 			}
10900Sstevel@tonic-gate 		}
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate 		if (!eval_expr(retnp, ex, epnames, globals, croot,
10930Sstevel@tonic-gate 			    arrowp, try, valuep))
10940Sstevel@tonic-gate 			return (0);
10950Sstevel@tonic-gate 		return (1);
10960Sstevel@tonic-gate 	}
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 	case T_CONDELSE:
10990Sstevel@tonic-gate 		/*
11000Sstevel@tonic-gate 		 * shouldn't get here, since T_CONDELSE is supposed to be
11010Sstevel@tonic-gate 		 * evaluated as part of T_CONDIF
11020Sstevel@tonic-gate 		 */
11030Sstevel@tonic-gate 		out(O_ALTFP|O_DIE, "eval_expr: wrong context for operation %s",
11040Sstevel@tonic-gate 		    ptree_nodetype2str(np->t));
11050Sstevel@tonic-gate 		return (0);
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 	case T_NE:
11080Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
11090Sstevel@tonic-gate 				arrowp, try, &lval))
11100Sstevel@tonic-gate 			return (0);
11110Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
11120Sstevel@tonic-gate 				arrowp, try, &rval))
11130Sstevel@tonic-gate 			return (0);
11140Sstevel@tonic-gate 		if (check_expr_args(&lval, &rval, UNDEFINED, np))
11150Sstevel@tonic-gate 			return (0);
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 		valuep->t = UINT64;
11180Sstevel@tonic-gate 		valuep->v = (lval.v != rval.v);
11190Sstevel@tonic-gate 		return (1);
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 	case T_LIST:
11220Sstevel@tonic-gate 	case T_AND:
11230Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
11240Sstevel@tonic-gate 				arrowp, try, valuep))
11250Sstevel@tonic-gate 			return (0);
11260Sstevel@tonic-gate 		if (valuep->v == 0) {
11270Sstevel@tonic-gate 			valuep->t = UINT64;
11280Sstevel@tonic-gate 			return (1);
11290Sstevel@tonic-gate 		}
11300Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
11310Sstevel@tonic-gate 				arrowp, try, valuep))
11320Sstevel@tonic-gate 			return (0);
11330Sstevel@tonic-gate 		valuep->t = UINT64;
11340Sstevel@tonic-gate 		valuep->v = valuep->v == 0 ? 0 : 1;
11350Sstevel@tonic-gate 		return (1);
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 	case T_OR:
11380Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
11390Sstevel@tonic-gate 				arrowp, try, valuep))
11400Sstevel@tonic-gate 			return (0);
11410Sstevel@tonic-gate 		if (valuep->v != 0) {
11420Sstevel@tonic-gate 			valuep->t = UINT64;
11430Sstevel@tonic-gate 			valuep->v = 1;
11440Sstevel@tonic-gate 			return (1);
11450Sstevel@tonic-gate 		}
11460Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
11470Sstevel@tonic-gate 				arrowp, try, valuep))
11480Sstevel@tonic-gate 			return (0);
11490Sstevel@tonic-gate 		valuep->t = UINT64;
11500Sstevel@tonic-gate 		valuep->v = valuep->v == 0 ? 0 : 1;
11510Sstevel@tonic-gate 		return (1);
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 	case T_NOT:
11540Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
11550Sstevel@tonic-gate 				arrowp, try, valuep))
11560Sstevel@tonic-gate 			return (0);
11570Sstevel@tonic-gate 		valuep->t = UINT64;
11580Sstevel@tonic-gate 		valuep->v = ! valuep->v;
11590Sstevel@tonic-gate 		return (1);
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 	case T_ADD:
11620Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
11630Sstevel@tonic-gate 				arrowp, try, &lval))
11640Sstevel@tonic-gate 			return (0);
11650Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
11660Sstevel@tonic-gate 				arrowp, try, &rval))
11670Sstevel@tonic-gate 			return (0);
11680Sstevel@tonic-gate 		if (check_expr_args(&lval, &rval, UINT64, np))
11690Sstevel@tonic-gate 			return (0);
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 		valuep->t = lval.t;
11720Sstevel@tonic-gate 		valuep->v = lval.v + rval.v;
11730Sstevel@tonic-gate 		return (1);
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 	case T_SUB:
11760Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
11770Sstevel@tonic-gate 				arrowp, try, &lval))
11780Sstevel@tonic-gate 			return (0);
11790Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
11800Sstevel@tonic-gate 				arrowp, try, &rval))
11810Sstevel@tonic-gate 			return (0);
11820Sstevel@tonic-gate 		if (check_expr_args(&lval, &rval, UINT64, np))
11830Sstevel@tonic-gate 			return (0);
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 		/* since valuep is unsigned, return false if lval.v < rval.v */
11860Sstevel@tonic-gate 		if (lval.v < rval.v) {
11870Sstevel@tonic-gate 			out(O_ERR, "eval_expr: T_SUB result is out of range");
11880Sstevel@tonic-gate 			valuep->t = UNDEFINED;
11890Sstevel@tonic-gate 			return (0);
11900Sstevel@tonic-gate 		}
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 		valuep->t = lval.t;
11930Sstevel@tonic-gate 		valuep->v = lval.v - rval.v;
11940Sstevel@tonic-gate 		return (1);
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 	case T_MUL:
11970Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
11980Sstevel@tonic-gate 				arrowp, try, &lval))
11990Sstevel@tonic-gate 			return (0);
12000Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
12010Sstevel@tonic-gate 				arrowp, try, &rval))
12020Sstevel@tonic-gate 			return (0);
12030Sstevel@tonic-gate 		if (check_expr_args(&lval, &rval, UINT64, np))
12040Sstevel@tonic-gate 			return (0);
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 		valuep->t = lval.t;
12070Sstevel@tonic-gate 		valuep->v = lval.v * rval.v;
12080Sstevel@tonic-gate 		return (1);
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 	case T_DIV:
12110Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
12120Sstevel@tonic-gate 				arrowp, try, &lval))
12130Sstevel@tonic-gate 			return (0);
12140Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
12150Sstevel@tonic-gate 				arrowp, try, &rval))
12160Sstevel@tonic-gate 			return (0);
12170Sstevel@tonic-gate 		if (check_expr_args(&lval, &rval, UINT64, np))
12180Sstevel@tonic-gate 			return (0);
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate 		/* return false if dividing by zero */
12210Sstevel@tonic-gate 		if (rval.v == 0) {
12220Sstevel@tonic-gate 			out(O_ERR, "eval_expr: T_DIV division by zero");
12230Sstevel@tonic-gate 			valuep->t = UNDEFINED;
12240Sstevel@tonic-gate 			return (0);
12250Sstevel@tonic-gate 		}
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate 		valuep->t = lval.t;
12280Sstevel@tonic-gate 		valuep->v = lval.v / rval.v;
12290Sstevel@tonic-gate 		return (1);
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 	case T_MOD:
12320Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
12330Sstevel@tonic-gate 				arrowp, try, &lval))
12340Sstevel@tonic-gate 			return (0);
12350Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
12360Sstevel@tonic-gate 				arrowp, try, &rval))
12370Sstevel@tonic-gate 			return (0);
12380Sstevel@tonic-gate 		if (check_expr_args(&lval, &rval, UINT64, np))
12390Sstevel@tonic-gate 			return (0);
12400Sstevel@tonic-gate 
12410Sstevel@tonic-gate 		/* return false if dividing by zero */
12420Sstevel@tonic-gate 		if (rval.v == 0) {
12430Sstevel@tonic-gate 			out(O_ERR, "eval_expr: T_MOD division by zero");
12440Sstevel@tonic-gate 			valuep->t = UNDEFINED;
12450Sstevel@tonic-gate 			return (0);
12460Sstevel@tonic-gate 		}
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 		valuep->t = lval.t;
12490Sstevel@tonic-gate 		valuep->v = lval.v % rval.v;
12500Sstevel@tonic-gate 		return (1);
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 	case T_NAME:
12530Sstevel@tonic-gate 		if (try) {
12540Sstevel@tonic-gate 			struct iterinfo *iterinfop;
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 			/*
12570Sstevel@tonic-gate 			 * at itree_create() time, we can expand simple
12580Sstevel@tonic-gate 			 * iterators.  anything else we'll punt on.
12590Sstevel@tonic-gate 			 */
12600Sstevel@tonic-gate 			iterinfop = lut_lookup(ex, (void *)np->u.name.s, NULL);
12610Sstevel@tonic-gate 			if (iterinfop != NULL) {
12620Sstevel@tonic-gate 				/* explicit iterator; not part of pathname */
12630Sstevel@tonic-gate 				valuep->t = UINT64;
12640Sstevel@tonic-gate 				valuep->v = (unsigned long long)iterinfop->num;
12650Sstevel@tonic-gate 				return (1);
12660Sstevel@tonic-gate 			}
12670Sstevel@tonic-gate 			return (0);
12680Sstevel@tonic-gate 		}
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 		/* return address of struct node */
12710Sstevel@tonic-gate 		valuep->t = NODEPTR;
12721717Swesolows 		valuep->v = (uintptr_t)np;
12730Sstevel@tonic-gate 		return (1);
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate 	case T_QUOTE:
12760Sstevel@tonic-gate 		valuep->t = STRING;
12771717Swesolows 		valuep->v = (uintptr_t)np->u.quote.s;
12780Sstevel@tonic-gate 		return (1);
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 	case T_FUNC:
12810Sstevel@tonic-gate 		return (eval_func(np, ex, epnames, np->u.func.arglist,
12820Sstevel@tonic-gate 				globals, croot, arrowp, try, valuep));
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 	case T_NUM:
12850Sstevel@tonic-gate 		valuep->t = UINT64;
12860Sstevel@tonic-gate 		valuep->v = np->u.ull;
12870Sstevel@tonic-gate 		return (1);
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate 	default:
12900Sstevel@tonic-gate 		outfl(O_DIE, np->file, np->line,
12910Sstevel@tonic-gate 		    "eval_expr: unexpected node type: %s",
12920Sstevel@tonic-gate 		    ptree_nodetype2str(np->t));
12930Sstevel@tonic-gate 	}
12940Sstevel@tonic-gate 	/*NOTREACHED*/
12951717Swesolows 	return (0);
12960Sstevel@tonic-gate }
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate /*
12990Sstevel@tonic-gate  * eval_fru() and eval_asru() don't do much, but are called from a number
13000Sstevel@tonic-gate  * of places.
13010Sstevel@tonic-gate  */
13020Sstevel@tonic-gate struct node *
13030Sstevel@tonic-gate eval_fru(struct node *np)
13040Sstevel@tonic-gate {
13050Sstevel@tonic-gate 	ASSERT(np->t == T_NAME);
13060Sstevel@tonic-gate 	return (np);
13070Sstevel@tonic-gate }
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate struct node *
13100Sstevel@tonic-gate eval_asru(struct node *np)
13110Sstevel@tonic-gate {
13120Sstevel@tonic-gate 	ASSERT(np->t == T_NAME);
13130Sstevel@tonic-gate 	return (np);
13140Sstevel@tonic-gate }
1315