xref: /onnv-gate/usr/src/cmd/fm/modules/common/eversholt/eval.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  *
26*0Sstevel@tonic-gate  * eval.c -- constraint evaluation module
27*0Sstevel@tonic-gate  *
28*0Sstevel@tonic-gate  * this module evaluates constraints.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <stdio.h>
34*0Sstevel@tonic-gate #include <stdlib.h>
35*0Sstevel@tonic-gate #include <ctype.h>
36*0Sstevel@tonic-gate #include <string.h>
37*0Sstevel@tonic-gate #include "alloc.h"
38*0Sstevel@tonic-gate #include "out.h"
39*0Sstevel@tonic-gate #include "stable.h"
40*0Sstevel@tonic-gate #include "literals.h"
41*0Sstevel@tonic-gate #include "lut.h"
42*0Sstevel@tonic-gate #include "tree.h"
43*0Sstevel@tonic-gate #include "ptree.h"
44*0Sstevel@tonic-gate #include "itree.h"
45*0Sstevel@tonic-gate #include "eval.h"
46*0Sstevel@tonic-gate #include "config.h"
47*0Sstevel@tonic-gate #include "platform.h"
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate static struct node *eval_dup(struct node *np, struct lut *ex,
51*0Sstevel@tonic-gate 			    struct node *epnames[]);
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate /*
54*0Sstevel@tonic-gate  * begins_with -- return true if rhs path begins with everything in lhs path
55*0Sstevel@tonic-gate  */
56*0Sstevel@tonic-gate static int
57*0Sstevel@tonic-gate begins_with(struct node *lhs, struct node *rhs)
58*0Sstevel@tonic-gate {
59*0Sstevel@tonic-gate 	int lnum;
60*0Sstevel@tonic-gate 	int rnum;
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate 	if (lhs == NULL)
63*0Sstevel@tonic-gate 		return (1);	/* yep -- it all matched */
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 	if (rhs == NULL)
66*0Sstevel@tonic-gate 		return (0);	/* nope, ran out of rhs first */
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate 	ASSERTeq(lhs->t, T_NAME, ptree_nodetype2str);
69*0Sstevel@tonic-gate 	ASSERTeq(rhs->t, T_NAME, ptree_nodetype2str);
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate 	if (lhs->u.name.s != rhs->u.name.s)
72*0Sstevel@tonic-gate 		return (0);	/* nope, different component names */
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 	if (lhs->u.name.child && lhs->u.name.child->t == T_NUM)
75*0Sstevel@tonic-gate 		lnum = (int)lhs->u.name.child->u.ull;
76*0Sstevel@tonic-gate 	else
77*0Sstevel@tonic-gate 		out(O_DIE, "begins_with: unexpected lhs child");
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate 	if (rhs->u.name.child && rhs->u.name.child->t == T_NUM)
80*0Sstevel@tonic-gate 		rnum = (int)rhs->u.name.child->u.ull;
81*0Sstevel@tonic-gate 	else
82*0Sstevel@tonic-gate 		out(O_DIE, "begins_with: unexpected rhs child");
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 	if (lnum != rnum)
85*0Sstevel@tonic-gate 		return (0);	/* nope, instance numbers were different */
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	return (begins_with(lhs->u.name.next, rhs->u.name.next));
88*0Sstevel@tonic-gate }
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate /*
91*0Sstevel@tonic-gate  * evaluate a variety of functions and place result in valuep.  return 1 if
92*0Sstevel@tonic-gate  * function evaluation was successful; 0 if otherwise (e.g., the case of an
93*0Sstevel@tonic-gate  * invalid argument to the function)
94*0Sstevel@tonic-gate  */
95*0Sstevel@tonic-gate /*ARGSUSED*/
96*0Sstevel@tonic-gate static int
97*0Sstevel@tonic-gate eval_func(struct node *funcnp, struct lut *ex, struct node *epnames[],
98*0Sstevel@tonic-gate     struct node *np, struct lut **globals,
99*0Sstevel@tonic-gate     struct config *croot, struct arrow *arrowp, int try, struct evalue *valuep)
100*0Sstevel@tonic-gate {
101*0Sstevel@tonic-gate 	const char *funcname = funcnp->u.func.s;
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	if (funcname == L_within) {
104*0Sstevel@tonic-gate 		/* within()'s are not really constraints -- always true */
105*0Sstevel@tonic-gate 		valuep->t = UINT64;
106*0Sstevel@tonic-gate 		valuep->v = 1;
107*0Sstevel@tonic-gate 		return (1);
108*0Sstevel@tonic-gate 	} else if (funcname == L_is_under) {
109*0Sstevel@tonic-gate 		struct node *lhs;
110*0Sstevel@tonic-gate 		struct node *rhs;
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 		if (np->u.expr.left->t == T_NAME)
113*0Sstevel@tonic-gate 			lhs = np->u.expr.left;
114*0Sstevel@tonic-gate 		else if (np->u.expr.left->u.func.s == L_fru)
115*0Sstevel@tonic-gate 			lhs = eval_fru(np->u.expr.left->u.func.arglist);
116*0Sstevel@tonic-gate 		else if (np->u.expr.left->u.func.s == L_asru)
117*0Sstevel@tonic-gate 			lhs = eval_asru(np->u.expr.left->u.func.arglist);
118*0Sstevel@tonic-gate 		else
119*0Sstevel@tonic-gate 			out(O_DIE, "is_under: unexpected lhs type: %s",
120*0Sstevel@tonic-gate 			    ptree_nodetype2str(np->u.expr.left->t));
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 		if (np->u.expr.right->t == T_NAME)
123*0Sstevel@tonic-gate 			rhs = np->u.expr.right;
124*0Sstevel@tonic-gate 		else if (np->u.expr.right->u.func.s == L_fru)
125*0Sstevel@tonic-gate 			rhs = eval_fru(np->u.expr.right->u.func.arglist);
126*0Sstevel@tonic-gate 		else if (np->u.expr.right->u.func.s == L_asru)
127*0Sstevel@tonic-gate 			rhs = eval_asru(np->u.expr.right->u.func.arglist);
128*0Sstevel@tonic-gate 		else
129*0Sstevel@tonic-gate 			out(O_DIE, "is_under: unexpected rhs type: %s",
130*0Sstevel@tonic-gate 			    ptree_nodetype2str(np->u.expr.right->t));
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 		/* eval_dup will expand wildcards, iterators, etc... */
133*0Sstevel@tonic-gate 		lhs = eval_dup(lhs, ex, epnames);
134*0Sstevel@tonic-gate 		rhs = eval_dup(rhs, ex, epnames);
135*0Sstevel@tonic-gate 		valuep->t = UINT64;
136*0Sstevel@tonic-gate 		valuep->v = begins_with(lhs, rhs);
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 		out(O_ALTFP|O_VERB2|O_NONL, "eval_func:is_under(");
139*0Sstevel@tonic-gate 		ptree_name_iter(O_ALTFP|O_VERB2|O_NONL, lhs);
140*0Sstevel@tonic-gate 		out(O_ALTFP|O_VERB2|O_NONL, ",");
141*0Sstevel@tonic-gate 		ptree_name_iter(O_ALTFP|O_VERB2|O_NONL, rhs);
142*0Sstevel@tonic-gate 		out(O_ALTFP|O_VERB2, ") returned %d", (int)valuep->v);
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 		tree_free(lhs);
145*0Sstevel@tonic-gate 		tree_free(rhs);
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 		return (1);
148*0Sstevel@tonic-gate 	}
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	if (try)
151*0Sstevel@tonic-gate 		return (0);
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 	if (funcname == L_fru) {
154*0Sstevel@tonic-gate 		valuep->t = NODEPTR;
155*0Sstevel@tonic-gate 		valuep->v = (unsigned long long)eval_fru(np);
156*0Sstevel@tonic-gate 		return (1);
157*0Sstevel@tonic-gate 	} else if (funcname == L_asru) {
158*0Sstevel@tonic-gate 		valuep->t = NODEPTR;
159*0Sstevel@tonic-gate 		valuep->v = (unsigned long long)eval_asru(np);
160*0Sstevel@tonic-gate 		return (1);
161*0Sstevel@tonic-gate 	} else if (funcname == L_call) {
162*0Sstevel@tonic-gate 		return (! platform_call(np, globals, croot, arrowp, valuep));
163*0Sstevel@tonic-gate 	} else if (funcname == L_is_connected) {
164*0Sstevel@tonic-gate 		return (! config_is_connected(np, croot, valuep));
165*0Sstevel@tonic-gate 	} else if (funcname == L_is_on) {
166*0Sstevel@tonic-gate 		return (! config_is_on(np, croot, valuep));
167*0Sstevel@tonic-gate 	} else if (funcname == L_is_present) {
168*0Sstevel@tonic-gate 		return (! config_is_present(np, croot, valuep));
169*0Sstevel@tonic-gate 	} else if (funcname == L_is_type) {
170*0Sstevel@tonic-gate 		return (! config_is_type(np, croot, valuep));
171*0Sstevel@tonic-gate 	} else if (funcname == L_confprop) {
172*0Sstevel@tonic-gate 		return (! config_confprop(np, croot, valuep));
173*0Sstevel@tonic-gate 	} else if (funcname == L_envprop) {
174*0Sstevel@tonic-gate 		outfl(O_DIE, np->file, np->line,
175*0Sstevel@tonic-gate 		    "eval_func: %s not yet supported", funcname);
176*0Sstevel@tonic-gate 	} else if (funcname == L_payloadprop) {
177*0Sstevel@tonic-gate 		outfl(O_ALTFP|O_VERB|O_NONL, np->file, np->line,
178*0Sstevel@tonic-gate 		    "payloadprop(\"%s\") ", np->u.quote.s);
179*0Sstevel@tonic-gate 		if (funcnp->u.func.cachedval != NULL) {
180*0Sstevel@tonic-gate 			*valuep = *(struct evalue *)(funcnp->u.func.cachedval);
181*0Sstevel@tonic-gate 			out(O_ALTFP|O_VERB, "cached: %llu", valuep->v);
182*0Sstevel@tonic-gate 			return (1);
183*0Sstevel@tonic-gate 		} else if (platform_payloadprop(np, valuep)) {
184*0Sstevel@tonic-gate 			/* platform_payloadprop() returned false, pass it on */
185*0Sstevel@tonic-gate 			out(O_ALTFP|O_VERB, "failed.");
186*0Sstevel@tonic-gate 			return (0);
187*0Sstevel@tonic-gate 		} else {
188*0Sstevel@tonic-gate 			/* got back true, cache the value */
189*0Sstevel@tonic-gate 			funcnp->u.func.cachedval =
190*0Sstevel@tonic-gate 			    MALLOC(sizeof (struct evalue));
191*0Sstevel@tonic-gate 			*(struct evalue *)(funcnp->u.func.cachedval) =
192*0Sstevel@tonic-gate 			    *valuep;
193*0Sstevel@tonic-gate 			out(O_ALTFP|O_VERB, "returned: %llu", valuep->v);
194*0Sstevel@tonic-gate 			return (1);
195*0Sstevel@tonic-gate 		}
196*0Sstevel@tonic-gate 	} else
197*0Sstevel@tonic-gate 		outfl(O_DIE, np->file, np->line,
198*0Sstevel@tonic-gate 		    "eval_func: unexpected func: %s", funcname);
199*0Sstevel@tonic-gate 	/*NOTREACHED*/
200*0Sstevel@tonic-gate }
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate static struct node *
203*0Sstevel@tonic-gate eval_wildcardedname(struct node *np, struct lut *ex, struct node *epnames[])
204*0Sstevel@tonic-gate {
205*0Sstevel@tonic-gate 	struct node *npstart, *npend, *npref, *newnp;
206*0Sstevel@tonic-gate 	struct node *np1, *np2, *retp;
207*0Sstevel@tonic-gate 	int i;
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 	if (epnames == NULL || epnames[0] == NULL)
210*0Sstevel@tonic-gate 		return (NULL);
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	for (i = 0; epnames[i] != NULL; i++) {
213*0Sstevel@tonic-gate 		if (tree_namecmp(np, epnames[i]) == 0)
214*0Sstevel@tonic-gate 			return (NULL);
215*0Sstevel@tonic-gate 	}
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 	/*
218*0Sstevel@tonic-gate 	 * get to this point if np does not match any of the entries in
219*0Sstevel@tonic-gate 	 * epnames.  check if np is a path that must preceded by a wildcard
220*0Sstevel@tonic-gate 	 * portion.  for this case we must first determine which epnames[]
221*0Sstevel@tonic-gate 	 * entry should be used for wildcarding.
222*0Sstevel@tonic-gate 	 */
223*0Sstevel@tonic-gate 	npstart = NULL;
224*0Sstevel@tonic-gate 	for (i = 0; epnames[i] != NULL; i++) {
225*0Sstevel@tonic-gate 		for (npref = epnames[i]; npref; npref = npref->u.name.next) {
226*0Sstevel@tonic-gate 			if (npref->u.name.s == np->u.name.s) {
227*0Sstevel@tonic-gate 				for (np1 = npref, np2 = np;
228*0Sstevel@tonic-gate 				    np1 != NULL && np2 != NULL;
229*0Sstevel@tonic-gate 				    np1 = np1->u.name.next,
230*0Sstevel@tonic-gate 					    np2 = np2->u.name.next) {
231*0Sstevel@tonic-gate 					if (np1->u.name.s != np2->u.name.s)
232*0Sstevel@tonic-gate 						break;
233*0Sstevel@tonic-gate 				}
234*0Sstevel@tonic-gate 				if (np2 == NULL) {
235*0Sstevel@tonic-gate 					npstart = epnames[i];
236*0Sstevel@tonic-gate 					npend = npref;
237*0Sstevel@tonic-gate 					if (np1 == NULL)
238*0Sstevel@tonic-gate 						break;
239*0Sstevel@tonic-gate 				}
240*0Sstevel@tonic-gate 			}
241*0Sstevel@tonic-gate 		}
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 		if (npstart != NULL)
244*0Sstevel@tonic-gate 			break;
245*0Sstevel@tonic-gate 	}
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 	if (npstart == NULL) {
248*0Sstevel@tonic-gate 		/* no match; np is not a path to be wildcarded */
249*0Sstevel@tonic-gate 		return (NULL);
250*0Sstevel@tonic-gate 	}
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 	/*
253*0Sstevel@tonic-gate 	 * dup (npstart -- npend) which is the wildcarded portion.  all
254*0Sstevel@tonic-gate 	 * children should be T_NUMs.
255*0Sstevel@tonic-gate 	 */
256*0Sstevel@tonic-gate 	retp = NULL;
257*0Sstevel@tonic-gate 	for (npref = npstart;
258*0Sstevel@tonic-gate 	    ! (npref == NULL || npref == npend);
259*0Sstevel@tonic-gate 	    npref = npref->u.name.next) {
260*0Sstevel@tonic-gate 		newnp = newnode(T_NAME, np->file, np->line);
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate 		newnp->u.name.t = npref->u.name.t;
263*0Sstevel@tonic-gate 		newnp->u.name.s = npref->u.name.s;
264*0Sstevel@tonic-gate 		newnp->u.name.last = newnp;
265*0Sstevel@tonic-gate 		newnp->u.name.it = npref->u.name.it;
266*0Sstevel@tonic-gate 		newnp->u.name.cp = npref->u.name.cp;
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate 		ASSERT(npref->u.name.child != NULL);
269*0Sstevel@tonic-gate 		ASSERT(npref->u.name.child->t == T_NUM);
270*0Sstevel@tonic-gate 		newnp->u.name.child = newnode(T_NUM, np->file, np->line);
271*0Sstevel@tonic-gate 		newnp->u.name.child->u.ull = npref->u.name.child->u.ull;
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 		if (retp == NULL) {
274*0Sstevel@tonic-gate 			retp = newnp;
275*0Sstevel@tonic-gate 		} else {
276*0Sstevel@tonic-gate 			retp->u.name.last->u.name.next = newnp;
277*0Sstevel@tonic-gate 			retp->u.name.last = newnp;
278*0Sstevel@tonic-gate 		}
279*0Sstevel@tonic-gate 	}
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate 	ASSERT(retp != NULL);
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate 	/* now append the nonwildcarded portion */
284*0Sstevel@tonic-gate 	retp = tree_name_append(retp, eval_dup(np, ex, NULL));
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 	return (retp);
287*0Sstevel@tonic-gate }
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate static struct node *
290*0Sstevel@tonic-gate eval_dup(struct node *np, struct lut *ex, struct node *epnames[])
291*0Sstevel@tonic-gate {
292*0Sstevel@tonic-gate 	struct node *newnp;
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate 	if (np == NULL)
295*0Sstevel@tonic-gate 		return (NULL);
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate 	switch (np->t) {
298*0Sstevel@tonic-gate 	case T_GLOBID:
299*0Sstevel@tonic-gate 		return (tree_globid(np->u.globid.s, np->file, np->line));
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate 	case T_ASSIGN:
302*0Sstevel@tonic-gate 	case T_CONDIF:
303*0Sstevel@tonic-gate 	case T_CONDELSE:
304*0Sstevel@tonic-gate 	case T_NE:
305*0Sstevel@tonic-gate 	case T_EQ:
306*0Sstevel@tonic-gate 	case T_LT:
307*0Sstevel@tonic-gate 	case T_LE:
308*0Sstevel@tonic-gate 	case T_GT:
309*0Sstevel@tonic-gate 	case T_GE:
310*0Sstevel@tonic-gate 	case T_BITAND:
311*0Sstevel@tonic-gate 	case T_BITOR:
312*0Sstevel@tonic-gate 	case T_BITXOR:
313*0Sstevel@tonic-gate 	case T_BITNOT:
314*0Sstevel@tonic-gate 	case T_LSHIFT:
315*0Sstevel@tonic-gate 	case T_RSHIFT:
316*0Sstevel@tonic-gate 	case T_LIST:
317*0Sstevel@tonic-gate 	case T_AND:
318*0Sstevel@tonic-gate 	case T_OR:
319*0Sstevel@tonic-gate 	case T_NOT:
320*0Sstevel@tonic-gate 	case T_ADD:
321*0Sstevel@tonic-gate 	case T_SUB:
322*0Sstevel@tonic-gate 	case T_MUL:
323*0Sstevel@tonic-gate 	case T_DIV:
324*0Sstevel@tonic-gate 	case T_MOD:
325*0Sstevel@tonic-gate 		return (tree_expr(np->t,
326*0Sstevel@tonic-gate 				    eval_dup(np->u.expr.left, ex, epnames),
327*0Sstevel@tonic-gate 				    eval_dup(np->u.expr.right, ex, epnames)));
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate 	case T_NAME: {
330*0Sstevel@tonic-gate 		struct iterinfo *iterinfop;
331*0Sstevel@tonic-gate 		struct node *newchild = NULL;
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate 		iterinfop = lut_lookup(ex, (void *)np->u.name.s, NULL);
334*0Sstevel@tonic-gate 		if (iterinfop != NULL) {
335*0Sstevel@tonic-gate 			/* explicit iterator; not part of pathname */
336*0Sstevel@tonic-gate 			newnp = newnode(T_NUM, np->file, np->line);
337*0Sstevel@tonic-gate 			newnp->u.ull = iterinfop->num;
338*0Sstevel@tonic-gate 			return (newnp);
339*0Sstevel@tonic-gate 		}
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate 		/* see if np is a path with wildcard portion */
342*0Sstevel@tonic-gate 		newnp = eval_wildcardedname(np, ex, epnames);
343*0Sstevel@tonic-gate 		if (newnp != NULL)
344*0Sstevel@tonic-gate 			return (newnp);
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate 		/* turn off wildcarding for child */
347*0Sstevel@tonic-gate 		newchild = eval_dup(np->u.name.child, ex, NULL);
348*0Sstevel@tonic-gate 
349*0Sstevel@tonic-gate 		if (newchild != NULL) {
350*0Sstevel@tonic-gate 			if (newchild->t != T_NUM) {
351*0Sstevel@tonic-gate 				/*
352*0Sstevel@tonic-gate 				 * not a number, eh?  we must resolve this
353*0Sstevel@tonic-gate 				 * to a number.
354*0Sstevel@tonic-gate 				 */
355*0Sstevel@tonic-gate 				struct evalue value;
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate 				if (eval_expr(newchild, ex, epnames,
358*0Sstevel@tonic-gate 				    NULL, NULL, NULL, 1, &value) == 0 ||
359*0Sstevel@tonic-gate 				    value.t != UINT64) {
360*0Sstevel@tonic-gate 					outfl(O_DIE, np->file, np->line,
361*0Sstevel@tonic-gate 					    "eval_dup: could not resolve "
362*0Sstevel@tonic-gate 					    "iterator of %s", np->u.name.s);
363*0Sstevel@tonic-gate 				}
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate 				tree_free(newchild);
366*0Sstevel@tonic-gate 				newchild = newnode(T_NUM, np->file, np->line);
367*0Sstevel@tonic-gate 				newchild->u.ull = value.v;
368*0Sstevel@tonic-gate 			}
369*0Sstevel@tonic-gate 
370*0Sstevel@tonic-gate 			newnp = newnode(np->t, np->file, np->line);
371*0Sstevel@tonic-gate 			newnp->u.name.s = np->u.name.s;
372*0Sstevel@tonic-gate 			newnp->u.name.it = np->u.name.it;
373*0Sstevel@tonic-gate 			newnp->u.name.cp = np->u.name.cp;
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate 			newnp->u.name.last = newnp;
376*0Sstevel@tonic-gate 			newnp->u.name.child = newchild;
377*0Sstevel@tonic-gate 
378*0Sstevel@tonic-gate 			if (np->u.name.next != NULL) {
379*0Sstevel@tonic-gate 				/* turn off wildcarding for next */
380*0Sstevel@tonic-gate 				return (tree_name_append(newnp,
381*0Sstevel@tonic-gate 					eval_dup(np->u.name.next, ex, NULL)));
382*0Sstevel@tonic-gate 			} else {
383*0Sstevel@tonic-gate 				return (newnp);
384*0Sstevel@tonic-gate 			}
385*0Sstevel@tonic-gate 		} else {
386*0Sstevel@tonic-gate 			outfl(O_DIE, np->file, np->line,
387*0Sstevel@tonic-gate 			    "eval_dup: internal error: \"%s\" is neither "
388*0Sstevel@tonic-gate 			    "an iterator nor a pathname", np->u.name.s);
389*0Sstevel@tonic-gate 		}
390*0Sstevel@tonic-gate 		/*NOTREACHED*/
391*0Sstevel@tonic-gate 		break;
392*0Sstevel@tonic-gate 	}
393*0Sstevel@tonic-gate 
394*0Sstevel@tonic-gate 	case T_FUNC:
395*0Sstevel@tonic-gate 		return (tree_func(np->u.func.s,
396*0Sstevel@tonic-gate 		    eval_dup(np->u.func.arglist, ex, epnames),
397*0Sstevel@tonic-gate 		    np->file, np->line));
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate 	case T_QUOTE:
400*0Sstevel@tonic-gate 		newnp = newnode(T_QUOTE, np->file, np->line);
401*0Sstevel@tonic-gate 		newnp->u.quote.s = np->u.quote.s;
402*0Sstevel@tonic-gate 		return (newnp);
403*0Sstevel@tonic-gate 
404*0Sstevel@tonic-gate 	case T_NUM:
405*0Sstevel@tonic-gate 		newnp = newnode(T_NUM, np->file, np->line);
406*0Sstevel@tonic-gate 		newnp->u.ull = np->u.ull;
407*0Sstevel@tonic-gate 		return (newnp);
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate 	default:
410*0Sstevel@tonic-gate 		outfl(O_DIE, np->file, np->line,
411*0Sstevel@tonic-gate 		    "eval_dup: unexpected node type: %s",
412*0Sstevel@tonic-gate 		    ptree_nodetype2str(np->t));
413*0Sstevel@tonic-gate 	}
414*0Sstevel@tonic-gate 	/*NOTREACHED*/
415*0Sstevel@tonic-gate }
416*0Sstevel@tonic-gate 
417*0Sstevel@tonic-gate /*
418*0Sstevel@tonic-gate  * eval_potential -- see if constraint is potentially true
419*0Sstevel@tonic-gate  *
420*0Sstevel@tonic-gate  * this function is used at instance tree creation time to see if
421*0Sstevel@tonic-gate  * any constraints are already known to be false.  if this function
422*0Sstevel@tonic-gate  * returns false, then the constraint will always be false and there's
423*0Sstevel@tonic-gate  * no need to include the propagation arrow in the instance tree.
424*0Sstevel@tonic-gate  *
425*0Sstevel@tonic-gate  * if this routine returns true, either the constraint is known to
426*0Sstevel@tonic-gate  * be always true (so there's no point in attaching the constraint
427*0Sstevel@tonic-gate  * to the propagation arrow in the instance tree), or the constraint
428*0Sstevel@tonic-gate  * contains "deferred" expressions like global variables or poller calls
429*0Sstevel@tonic-gate  * and so it must be evaluated during calls to fme_eval().  in this last
430*0Sstevel@tonic-gate  * case, where a constraint needs to be attached to the propagation arrow
431*0Sstevel@tonic-gate  * in the instance tree, this routine returns a newly created constraint
432*0Sstevel@tonic-gate  * in *newc where all the non-deferred things have been filled in.
433*0Sstevel@tonic-gate  *
434*0Sstevel@tonic-gate  * so in summary:
435*0Sstevel@tonic-gate  *
436*0Sstevel@tonic-gate  *	return of false: constraint can never be true, *newc will be NULL.
437*0Sstevel@tonic-gate  *
438*0Sstevel@tonic-gate  *	return of true with *newc unchanged: constraint will always be true.
439*0Sstevel@tonic-gate  *
440*0Sstevel@tonic-gate  *	return of true with *newc changed: use new constraint in *newc.
441*0Sstevel@tonic-gate  *
442*0Sstevel@tonic-gate  * the lookup table for all explicit iterators, ex, is passed in.
443*0Sstevel@tonic-gate  *
444*0Sstevel@tonic-gate  * *newc can either be NULL on entry, or if can contain constraints from
445*0Sstevel@tonic-gate  * previous calls to eval_potential() (i.e. for building up an instance
446*0Sstevel@tonic-gate  * tree constraint from several potential constraints).  if *newc already
447*0Sstevel@tonic-gate  * contains constraints, anything added to it will be joined by adding
448*0Sstevel@tonic-gate  * a T_AND node at the top of *newc.
449*0Sstevel@tonic-gate  */
450*0Sstevel@tonic-gate int
451*0Sstevel@tonic-gate eval_potential(struct node *np, struct lut *ex, struct node *epnames[],
452*0Sstevel@tonic-gate 	    struct node **newc)
453*0Sstevel@tonic-gate {
454*0Sstevel@tonic-gate 	struct node *newnp;
455*0Sstevel@tonic-gate 	struct evalue value;
456*0Sstevel@tonic-gate 
457*0Sstevel@tonic-gate 	if (eval_expr(np, ex, epnames, NULL, NULL, NULL, 1, &value) == 0) {
458*0Sstevel@tonic-gate 		/*
459*0Sstevel@tonic-gate 		 * couldn't eval expression because
460*0Sstevel@tonic-gate 		 * it contains deferred items.  make
461*0Sstevel@tonic-gate 		 * a duplicate expression with all the
462*0Sstevel@tonic-gate 		 * non-deferred items expanded.
463*0Sstevel@tonic-gate 		 */
464*0Sstevel@tonic-gate 		newnp = eval_dup(np, ex, epnames);
465*0Sstevel@tonic-gate 
466*0Sstevel@tonic-gate 		if (*newc == NULL) {
467*0Sstevel@tonic-gate 			/*
468*0Sstevel@tonic-gate 			 * constraint is potentially true if deferred
469*0Sstevel@tonic-gate 			 * expression in newnp is true.  *newc was NULL
470*0Sstevel@tonic-gate 			 * so new constraint is just the one in newnp.
471*0Sstevel@tonic-gate 			 */
472*0Sstevel@tonic-gate 			*newc = newnp;
473*0Sstevel@tonic-gate 			return (1);
474*0Sstevel@tonic-gate 		} else {
475*0Sstevel@tonic-gate 			/*
476*0Sstevel@tonic-gate 			 * constraint is potentially true if deferred
477*0Sstevel@tonic-gate 			 * expression in newnp is true.  *newc already
478*0Sstevel@tonic-gate 			 * contained a constraint so add an AND with the
479*0Sstevel@tonic-gate 			 * constraint in newnp.
480*0Sstevel@tonic-gate 			 */
481*0Sstevel@tonic-gate 			*newc = tree_expr(T_AND, *newc, newnp);
482*0Sstevel@tonic-gate 			return (1);
483*0Sstevel@tonic-gate 		}
484*0Sstevel@tonic-gate 	} else if (value.t == UNDEFINED) {
485*0Sstevel@tonic-gate 		/* constraint can never be true */
486*0Sstevel@tonic-gate 		return (0);
487*0Sstevel@tonic-gate 	} else if (value.t == UINT64 && value.v == 0) {
488*0Sstevel@tonic-gate 		/* constraint can never be true */
489*0Sstevel@tonic-gate 		return (0);
490*0Sstevel@tonic-gate 	} else {
491*0Sstevel@tonic-gate 		/* constraint is always true (nothing deferred to eval) */
492*0Sstevel@tonic-gate 		return (1);
493*0Sstevel@tonic-gate 	}
494*0Sstevel@tonic-gate }
495*0Sstevel@tonic-gate 
496*0Sstevel@tonic-gate static int
497*0Sstevel@tonic-gate check_expr_args(struct evalue *lp, struct evalue *rp, enum datatype dtype,
498*0Sstevel@tonic-gate 		struct node *np)
499*0Sstevel@tonic-gate {
500*0Sstevel@tonic-gate 	if (dtype != UNDEFINED && lp->t != dtype) {
501*0Sstevel@tonic-gate 		outfl(O_OK, np->file, np->line,
502*0Sstevel@tonic-gate 			"invalid datatype of argument for operation %s",
503*0Sstevel@tonic-gate 			ptree_nodetype2str(np->t));
504*0Sstevel@tonic-gate 		return (1);
505*0Sstevel@tonic-gate 	}
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate 	if (rp != NULL && lp->t != rp->t) {
508*0Sstevel@tonic-gate 		outfl(O_OK, np->file, np->line,
509*0Sstevel@tonic-gate 			"mismatch in datatype of arguments for operation %s",
510*0Sstevel@tonic-gate 			ptree_nodetype2str(np->t));
511*0Sstevel@tonic-gate 		return (1);
512*0Sstevel@tonic-gate 	}
513*0Sstevel@tonic-gate 
514*0Sstevel@tonic-gate 	return (0);
515*0Sstevel@tonic-gate }
516*0Sstevel@tonic-gate 
517*0Sstevel@tonic-gate /*
518*0Sstevel@tonic-gate  * eval_expr -- evaluate expression into *valuep
519*0Sstevel@tonic-gate  *
520*0Sstevel@tonic-gate  * the meaning of the return value depends on the input value of try.
521*0Sstevel@tonic-gate  *
522*0Sstevel@tonic-gate  * for try == 1: if any deferred items are encounted, bail out and return
523*0Sstevel@tonic-gate  * false.  returns true if we made it through entire expression without
524*0Sstevel@tonic-gate  * hitting any deferred items.
525*0Sstevel@tonic-gate  *
526*0Sstevel@tonic-gate  * for try == 0: return true if all operations were performed successfully.
527*0Sstevel@tonic-gate  * return false if otherwise.  for example, any of the following conditions
528*0Sstevel@tonic-gate  * will result in a false return value:
529*0Sstevel@tonic-gate  *   - attempted use of an uninitialized global variable
530*0Sstevel@tonic-gate  *   - failure in function evaluation
531*0Sstevel@tonic-gate  *   - illegal arithmetic operation (argument out of range)
532*0Sstevel@tonic-gate  */
533*0Sstevel@tonic-gate int
534*0Sstevel@tonic-gate eval_expr(struct node *np, struct lut *ex, struct node *epnames[],
535*0Sstevel@tonic-gate 	struct lut **globals, struct config *croot, struct arrow *arrowp,
536*0Sstevel@tonic-gate 	int try, struct evalue *valuep)
537*0Sstevel@tonic-gate {
538*0Sstevel@tonic-gate 	struct evalue *gval;
539*0Sstevel@tonic-gate 	struct evalue lval;
540*0Sstevel@tonic-gate 	struct evalue rval;
541*0Sstevel@tonic-gate 
542*0Sstevel@tonic-gate 	if (np == NULL) {
543*0Sstevel@tonic-gate 		valuep->t = UINT64;
544*0Sstevel@tonic-gate 		valuep->v = 1;	/* no constraint means "true" */
545*0Sstevel@tonic-gate 		return (1);
546*0Sstevel@tonic-gate 	}
547*0Sstevel@tonic-gate 
548*0Sstevel@tonic-gate 	valuep->t = UNDEFINED;
549*0Sstevel@tonic-gate 
550*0Sstevel@tonic-gate 	switch (np->t) {
551*0Sstevel@tonic-gate 	case T_GLOBID:
552*0Sstevel@tonic-gate 		if (try)
553*0Sstevel@tonic-gate 			return (0);
554*0Sstevel@tonic-gate 
555*0Sstevel@tonic-gate 		/*
556*0Sstevel@tonic-gate 		 * only handle case of getting (and not setting) the value
557*0Sstevel@tonic-gate 		 * of a global variable
558*0Sstevel@tonic-gate 		 */
559*0Sstevel@tonic-gate 		gval = lut_lookup(*globals, (void *)np->u.globid.s, NULL);
560*0Sstevel@tonic-gate 		if (gval == NULL) {
561*0Sstevel@tonic-gate 			valuep->t = UNDEFINED;
562*0Sstevel@tonic-gate 			return (0);
563*0Sstevel@tonic-gate 		} else {
564*0Sstevel@tonic-gate 			valuep->t = gval->t;
565*0Sstevel@tonic-gate 			valuep->v = gval->v;
566*0Sstevel@tonic-gate 			return (1);
567*0Sstevel@tonic-gate 		}
568*0Sstevel@tonic-gate 
569*0Sstevel@tonic-gate 	case T_ASSIGN:
570*0Sstevel@tonic-gate 		if (try)
571*0Sstevel@tonic-gate 			return (0);
572*0Sstevel@tonic-gate 
573*0Sstevel@tonic-gate 		/*
574*0Sstevel@tonic-gate 		 * first evaluate rhs, then try to store value in lhs which
575*0Sstevel@tonic-gate 		 * should be a global variable
576*0Sstevel@tonic-gate 		 */
577*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
578*0Sstevel@tonic-gate 			    arrowp, try, &rval))
579*0Sstevel@tonic-gate 			return (0);
580*0Sstevel@tonic-gate 
581*0Sstevel@tonic-gate 		ASSERT(np->u.expr.left->t == T_GLOBID);
582*0Sstevel@tonic-gate 		gval = lut_lookup(*globals,
583*0Sstevel@tonic-gate 				(void *)np->u.expr.left->u.globid.s, NULL);
584*0Sstevel@tonic-gate 
585*0Sstevel@tonic-gate 		if (gval == NULL) {
586*0Sstevel@tonic-gate 			gval = MALLOC(sizeof (*gval));
587*0Sstevel@tonic-gate 			*globals = lut_add(*globals,
588*0Sstevel@tonic-gate 					(void *) np->u.expr.left->u.globid.s,
589*0Sstevel@tonic-gate 					gval, NULL);
590*0Sstevel@tonic-gate 		}
591*0Sstevel@tonic-gate 
592*0Sstevel@tonic-gate 		gval->t = rval.t;
593*0Sstevel@tonic-gate 		gval->v = rval.v;
594*0Sstevel@tonic-gate 		valuep->t = rval.t;
595*0Sstevel@tonic-gate 		valuep->v = rval.v;
596*0Sstevel@tonic-gate 		return (1);
597*0Sstevel@tonic-gate 
598*0Sstevel@tonic-gate 	case T_EQ:
599*0Sstevel@tonic-gate #define	IMPLICIT_ASSIGN_IN_EQ
600*0Sstevel@tonic-gate #ifdef IMPLICIT_ASSIGN_IN_EQ
601*0Sstevel@tonic-gate 		/*
602*0Sstevel@tonic-gate 		 * if lhs is an uninitialized global variable, perform
603*0Sstevel@tonic-gate 		 * an assignment.
604*0Sstevel@tonic-gate 		 *
605*0Sstevel@tonic-gate 		 * one insidious side effect of implicit assignment is
606*0Sstevel@tonic-gate 		 * that the "==" operator does not return a Boolean if
607*0Sstevel@tonic-gate 		 * implicit assignment was performed.
608*0Sstevel@tonic-gate 		 */
609*0Sstevel@tonic-gate 		if (try == 0 &&
610*0Sstevel@tonic-gate 		    np->u.expr.left->t == T_GLOBID &&
611*0Sstevel@tonic-gate 		    (gval = lut_lookup(*globals,
612*0Sstevel@tonic-gate 			(void *)np->u.expr.left->u.globid.s, NULL)) == NULL) {
613*0Sstevel@tonic-gate 			if (!eval_expr(np->u.expr.right, ex, epnames, globals,
614*0Sstevel@tonic-gate 					croot, arrowp, try, &rval))
615*0Sstevel@tonic-gate 				return (0);
616*0Sstevel@tonic-gate 
617*0Sstevel@tonic-gate 			gval = MALLOC(sizeof (*gval));
618*0Sstevel@tonic-gate 			*globals = lut_add(*globals,
619*0Sstevel@tonic-gate 					(void *) np->u.expr.left->u.globid.s,
620*0Sstevel@tonic-gate 					gval, NULL);
621*0Sstevel@tonic-gate 
622*0Sstevel@tonic-gate 			gval->t = rval.t;
623*0Sstevel@tonic-gate 			gval->v = rval.v;
624*0Sstevel@tonic-gate 			valuep->t = rval.t;
625*0Sstevel@tonic-gate 			valuep->v = rval.v;
626*0Sstevel@tonic-gate 			return (1);
627*0Sstevel@tonic-gate 		}
628*0Sstevel@tonic-gate #endif  /* IMPLICIT_ASSIGN_IN_EQ */
629*0Sstevel@tonic-gate 
630*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
631*0Sstevel@tonic-gate 				arrowp, try, &lval))
632*0Sstevel@tonic-gate 			return (0);
633*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
634*0Sstevel@tonic-gate 				arrowp, try, &rval))
635*0Sstevel@tonic-gate 			return (0);
636*0Sstevel@tonic-gate 		if (check_expr_args(&lval, &rval, UNDEFINED, np))
637*0Sstevel@tonic-gate 			return (0);
638*0Sstevel@tonic-gate 
639*0Sstevel@tonic-gate 		valuep->t = UINT64;
640*0Sstevel@tonic-gate 		valuep->v = (lval.v == rval.v);
641*0Sstevel@tonic-gate 		return (1);
642*0Sstevel@tonic-gate 
643*0Sstevel@tonic-gate 	case T_LT:
644*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
645*0Sstevel@tonic-gate 				arrowp, try, &lval))
646*0Sstevel@tonic-gate 			return (0);
647*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
648*0Sstevel@tonic-gate 				arrowp, try, &rval))
649*0Sstevel@tonic-gate 			return (0);
650*0Sstevel@tonic-gate 		if (check_expr_args(&lval, &rval, UNDEFINED, np))
651*0Sstevel@tonic-gate 			return (0);
652*0Sstevel@tonic-gate 
653*0Sstevel@tonic-gate 		valuep->t = UINT64;
654*0Sstevel@tonic-gate 		valuep->v = (lval.v < rval.v);
655*0Sstevel@tonic-gate 		return (1);
656*0Sstevel@tonic-gate 
657*0Sstevel@tonic-gate 	case T_LE:
658*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
659*0Sstevel@tonic-gate 				arrowp, try, &lval))
660*0Sstevel@tonic-gate 			return (0);
661*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
662*0Sstevel@tonic-gate 				arrowp, try, &rval))
663*0Sstevel@tonic-gate 			return (0);
664*0Sstevel@tonic-gate 		if (check_expr_args(&lval, &rval, UNDEFINED, np))
665*0Sstevel@tonic-gate 			return (0);
666*0Sstevel@tonic-gate 
667*0Sstevel@tonic-gate 		valuep->t = UINT64;
668*0Sstevel@tonic-gate 		valuep->v = (lval.v <= rval.v);
669*0Sstevel@tonic-gate 		return (1);
670*0Sstevel@tonic-gate 
671*0Sstevel@tonic-gate 	case T_GT:
672*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
673*0Sstevel@tonic-gate 				arrowp, try, &lval))
674*0Sstevel@tonic-gate 			return (0);
675*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
676*0Sstevel@tonic-gate 				arrowp, try, &rval))
677*0Sstevel@tonic-gate 			return (0);
678*0Sstevel@tonic-gate 		if (check_expr_args(&lval, &rval, UNDEFINED, np))
679*0Sstevel@tonic-gate 			return (0);
680*0Sstevel@tonic-gate 
681*0Sstevel@tonic-gate 		valuep->t = UINT64;
682*0Sstevel@tonic-gate 		valuep->v = (lval.v > rval.v);
683*0Sstevel@tonic-gate 		return (1);
684*0Sstevel@tonic-gate 
685*0Sstevel@tonic-gate 	case T_GE:
686*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
687*0Sstevel@tonic-gate 				arrowp, try, &lval))
688*0Sstevel@tonic-gate 			return (0);
689*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
690*0Sstevel@tonic-gate 				arrowp, try, &rval))
691*0Sstevel@tonic-gate 			return (0);
692*0Sstevel@tonic-gate 		if (check_expr_args(&lval, &rval, UNDEFINED, np))
693*0Sstevel@tonic-gate 			return (0);
694*0Sstevel@tonic-gate 
695*0Sstevel@tonic-gate 		valuep->t = UINT64;
696*0Sstevel@tonic-gate 		valuep->v = (lval.v >= rval.v);
697*0Sstevel@tonic-gate 		return (1);
698*0Sstevel@tonic-gate 
699*0Sstevel@tonic-gate 	case T_BITAND:
700*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
701*0Sstevel@tonic-gate 				arrowp, try, &lval))
702*0Sstevel@tonic-gate 			return (0);
703*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
704*0Sstevel@tonic-gate 				arrowp, try, &rval))
705*0Sstevel@tonic-gate 			return (0);
706*0Sstevel@tonic-gate 		if (check_expr_args(&lval, &rval, UINT64, np))
707*0Sstevel@tonic-gate 			return (0);
708*0Sstevel@tonic-gate 
709*0Sstevel@tonic-gate 		valuep->t = lval.t;
710*0Sstevel@tonic-gate 		valuep->v = (lval.v & rval.v);
711*0Sstevel@tonic-gate 		return (1);
712*0Sstevel@tonic-gate 
713*0Sstevel@tonic-gate 	case T_BITOR:
714*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
715*0Sstevel@tonic-gate 				arrowp, try, &lval))
716*0Sstevel@tonic-gate 			return (0);
717*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
718*0Sstevel@tonic-gate 				arrowp, try, &rval))
719*0Sstevel@tonic-gate 			return (0);
720*0Sstevel@tonic-gate 		if (check_expr_args(&lval, &rval, UINT64, np))
721*0Sstevel@tonic-gate 			return (0);
722*0Sstevel@tonic-gate 
723*0Sstevel@tonic-gate 		valuep->t = lval.t;
724*0Sstevel@tonic-gate 		valuep->v = (lval.v | rval.v);
725*0Sstevel@tonic-gate 		return (1);
726*0Sstevel@tonic-gate 
727*0Sstevel@tonic-gate 	case T_BITXOR:
728*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
729*0Sstevel@tonic-gate 				arrowp, try, &lval))
730*0Sstevel@tonic-gate 			return (0);
731*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
732*0Sstevel@tonic-gate 				arrowp, try, &rval))
733*0Sstevel@tonic-gate 			return (0);
734*0Sstevel@tonic-gate 		if (check_expr_args(&lval, &rval, UINT64, np))
735*0Sstevel@tonic-gate 			return (0);
736*0Sstevel@tonic-gate 
737*0Sstevel@tonic-gate 		valuep->t = lval.t;
738*0Sstevel@tonic-gate 		valuep->v = (lval.v ^ rval.v);
739*0Sstevel@tonic-gate 		return (1);
740*0Sstevel@tonic-gate 
741*0Sstevel@tonic-gate 	case T_BITNOT:
742*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
743*0Sstevel@tonic-gate 				arrowp, try, &lval))
744*0Sstevel@tonic-gate 			return (0);
745*0Sstevel@tonic-gate 		ASSERT(np->u.expr.right == NULL);
746*0Sstevel@tonic-gate 		if (check_expr_args(&lval, NULL, UINT64, np))
747*0Sstevel@tonic-gate 			return (0);
748*0Sstevel@tonic-gate 
749*0Sstevel@tonic-gate 		valuep->t = UINT64;
750*0Sstevel@tonic-gate 		valuep->v = ~ lval.v;
751*0Sstevel@tonic-gate 		return (1);
752*0Sstevel@tonic-gate 
753*0Sstevel@tonic-gate 	case T_LSHIFT:
754*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
755*0Sstevel@tonic-gate 				arrowp, try, &lval))
756*0Sstevel@tonic-gate 			return (0);
757*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
758*0Sstevel@tonic-gate 				arrowp, try, &rval))
759*0Sstevel@tonic-gate 			return (0);
760*0Sstevel@tonic-gate 		if (check_expr_args(&lval, &rval, UINT64, np))
761*0Sstevel@tonic-gate 			return (0);
762*0Sstevel@tonic-gate 
763*0Sstevel@tonic-gate 		valuep->t = UINT64;
764*0Sstevel@tonic-gate 		valuep->v = (lval.v << rval.v);
765*0Sstevel@tonic-gate 		return (1);
766*0Sstevel@tonic-gate 
767*0Sstevel@tonic-gate 	case T_RSHIFT:
768*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
769*0Sstevel@tonic-gate 				arrowp, try, &lval))
770*0Sstevel@tonic-gate 			return (0);
771*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
772*0Sstevel@tonic-gate 				arrowp, try, &rval))
773*0Sstevel@tonic-gate 			return (0);
774*0Sstevel@tonic-gate 		if (check_expr_args(&lval, &rval, UINT64, np))
775*0Sstevel@tonic-gate 			return (0);
776*0Sstevel@tonic-gate 
777*0Sstevel@tonic-gate 		valuep->t = UINT64;
778*0Sstevel@tonic-gate 		valuep->v = (lval.v >> rval.v);
779*0Sstevel@tonic-gate 		return (1);
780*0Sstevel@tonic-gate 
781*0Sstevel@tonic-gate 	case T_CONDIF: {
782*0Sstevel@tonic-gate 		struct node *retnp;
783*0Sstevel@tonic-gate 		int dotrue = 0;
784*0Sstevel@tonic-gate 
785*0Sstevel@tonic-gate 		/*
786*0Sstevel@tonic-gate 		 * evaluate
787*0Sstevel@tonic-gate 		 *	expression ? stmtA [ : stmtB ]
788*0Sstevel@tonic-gate 		 *
789*0Sstevel@tonic-gate 		 * first see if expression is true or false, then determine
790*0Sstevel@tonic-gate 		 * if stmtA (or stmtB, if it exists) should be evaluated.
791*0Sstevel@tonic-gate 		 *
792*0Sstevel@tonic-gate 		 * "dotrue = 1" means stmtA should be evaluated.
793*0Sstevel@tonic-gate 		 */
794*0Sstevel@tonic-gate 		if (eval_expr(np->u.expr.left, ex, epnames, globals, croot,
795*0Sstevel@tonic-gate 				arrowp, try, &lval) &&
796*0Sstevel@tonic-gate 		    lval.t != UNDEFINED && lval.v != 0)
797*0Sstevel@tonic-gate 			dotrue = 1;
798*0Sstevel@tonic-gate 
799*0Sstevel@tonic-gate 		ASSERT(np->u.expr.right != NULL);
800*0Sstevel@tonic-gate 		if (np->u.expr.right->t == T_CONDELSE) {
801*0Sstevel@tonic-gate 			if (dotrue)
802*0Sstevel@tonic-gate 				retnp = np->u.expr.right->u.expr.left;
803*0Sstevel@tonic-gate 			else
804*0Sstevel@tonic-gate 				retnp = np->u.expr.right->u.expr.right;
805*0Sstevel@tonic-gate 		} else {
806*0Sstevel@tonic-gate 			/* no ELSE clause */
807*0Sstevel@tonic-gate 			if (dotrue)
808*0Sstevel@tonic-gate 				retnp = np->u.expr.right;
809*0Sstevel@tonic-gate 			else {
810*0Sstevel@tonic-gate 				valuep->t = UINT64;
811*0Sstevel@tonic-gate 				valuep->v = 0;
812*0Sstevel@tonic-gate 				return (0);
813*0Sstevel@tonic-gate 			}
814*0Sstevel@tonic-gate 		}
815*0Sstevel@tonic-gate 
816*0Sstevel@tonic-gate 		if (!eval_expr(retnp, ex, epnames, globals, croot,
817*0Sstevel@tonic-gate 			    arrowp, try, valuep))
818*0Sstevel@tonic-gate 			return (0);
819*0Sstevel@tonic-gate 		return (1);
820*0Sstevel@tonic-gate 	}
821*0Sstevel@tonic-gate 
822*0Sstevel@tonic-gate 	case T_CONDELSE:
823*0Sstevel@tonic-gate 		/*
824*0Sstevel@tonic-gate 		 * shouldn't get here, since T_CONDELSE is supposed to be
825*0Sstevel@tonic-gate 		 * evaluated as part of T_CONDIF
826*0Sstevel@tonic-gate 		 */
827*0Sstevel@tonic-gate 		out(O_ALTFP|O_DIE, "eval_expr: wrong context for operation %s",
828*0Sstevel@tonic-gate 		    ptree_nodetype2str(np->t));
829*0Sstevel@tonic-gate 		return (0);
830*0Sstevel@tonic-gate 
831*0Sstevel@tonic-gate 	case T_NE:
832*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
833*0Sstevel@tonic-gate 				arrowp, try, &lval))
834*0Sstevel@tonic-gate 			return (0);
835*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
836*0Sstevel@tonic-gate 				arrowp, try, &rval))
837*0Sstevel@tonic-gate 			return (0);
838*0Sstevel@tonic-gate 		if (check_expr_args(&lval, &rval, UNDEFINED, np))
839*0Sstevel@tonic-gate 			return (0);
840*0Sstevel@tonic-gate 
841*0Sstevel@tonic-gate 		valuep->t = UINT64;
842*0Sstevel@tonic-gate 		valuep->v = (lval.v != rval.v);
843*0Sstevel@tonic-gate 		return (1);
844*0Sstevel@tonic-gate 
845*0Sstevel@tonic-gate 	case T_LIST:
846*0Sstevel@tonic-gate 	case T_AND:
847*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
848*0Sstevel@tonic-gate 				arrowp, try, valuep))
849*0Sstevel@tonic-gate 			return (0);
850*0Sstevel@tonic-gate 		if (valuep->v == 0) {
851*0Sstevel@tonic-gate 			valuep->t = UINT64;
852*0Sstevel@tonic-gate 			return (1);
853*0Sstevel@tonic-gate 		}
854*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
855*0Sstevel@tonic-gate 				arrowp, try, valuep))
856*0Sstevel@tonic-gate 			return (0);
857*0Sstevel@tonic-gate 		valuep->t = UINT64;
858*0Sstevel@tonic-gate 		valuep->v = valuep->v == 0 ? 0 : 1;
859*0Sstevel@tonic-gate 		return (1);
860*0Sstevel@tonic-gate 
861*0Sstevel@tonic-gate 	case T_OR:
862*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
863*0Sstevel@tonic-gate 				arrowp, try, valuep))
864*0Sstevel@tonic-gate 			return (0);
865*0Sstevel@tonic-gate 		if (valuep->v != 0) {
866*0Sstevel@tonic-gate 			valuep->t = UINT64;
867*0Sstevel@tonic-gate 			valuep->v = 1;
868*0Sstevel@tonic-gate 			return (1);
869*0Sstevel@tonic-gate 		}
870*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
871*0Sstevel@tonic-gate 				arrowp, try, valuep))
872*0Sstevel@tonic-gate 			return (0);
873*0Sstevel@tonic-gate 		valuep->t = UINT64;
874*0Sstevel@tonic-gate 		valuep->v = valuep->v == 0 ? 0 : 1;
875*0Sstevel@tonic-gate 		return (1);
876*0Sstevel@tonic-gate 
877*0Sstevel@tonic-gate 	case T_NOT:
878*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
879*0Sstevel@tonic-gate 				arrowp, try, valuep))
880*0Sstevel@tonic-gate 			return (0);
881*0Sstevel@tonic-gate 		valuep->t = UINT64;
882*0Sstevel@tonic-gate 		valuep->v = ! valuep->v;
883*0Sstevel@tonic-gate 		return (1);
884*0Sstevel@tonic-gate 
885*0Sstevel@tonic-gate 	case T_ADD:
886*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
887*0Sstevel@tonic-gate 				arrowp, try, &lval))
888*0Sstevel@tonic-gate 			return (0);
889*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
890*0Sstevel@tonic-gate 				arrowp, try, &rval))
891*0Sstevel@tonic-gate 			return (0);
892*0Sstevel@tonic-gate 		if (check_expr_args(&lval, &rval, UINT64, np))
893*0Sstevel@tonic-gate 			return (0);
894*0Sstevel@tonic-gate 
895*0Sstevel@tonic-gate 		valuep->t = lval.t;
896*0Sstevel@tonic-gate 		valuep->v = lval.v + rval.v;
897*0Sstevel@tonic-gate 		return (1);
898*0Sstevel@tonic-gate 
899*0Sstevel@tonic-gate 	case T_SUB:
900*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
901*0Sstevel@tonic-gate 				arrowp, try, &lval))
902*0Sstevel@tonic-gate 			return (0);
903*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
904*0Sstevel@tonic-gate 				arrowp, try, &rval))
905*0Sstevel@tonic-gate 			return (0);
906*0Sstevel@tonic-gate 		if (check_expr_args(&lval, &rval, UINT64, np))
907*0Sstevel@tonic-gate 			return (0);
908*0Sstevel@tonic-gate 
909*0Sstevel@tonic-gate 		/* since valuep is unsigned, return false if lval.v < rval.v */
910*0Sstevel@tonic-gate 		if (lval.v < rval.v) {
911*0Sstevel@tonic-gate 			out(O_ERR, "eval_expr: T_SUB result is out of range");
912*0Sstevel@tonic-gate 			valuep->t = UNDEFINED;
913*0Sstevel@tonic-gate 			return (0);
914*0Sstevel@tonic-gate 		}
915*0Sstevel@tonic-gate 
916*0Sstevel@tonic-gate 		valuep->t = lval.t;
917*0Sstevel@tonic-gate 		valuep->v = lval.v - rval.v;
918*0Sstevel@tonic-gate 		return (1);
919*0Sstevel@tonic-gate 
920*0Sstevel@tonic-gate 	case T_MUL:
921*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
922*0Sstevel@tonic-gate 				arrowp, try, &lval))
923*0Sstevel@tonic-gate 			return (0);
924*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
925*0Sstevel@tonic-gate 				arrowp, try, &rval))
926*0Sstevel@tonic-gate 			return (0);
927*0Sstevel@tonic-gate 		if (check_expr_args(&lval, &rval, UINT64, np))
928*0Sstevel@tonic-gate 			return (0);
929*0Sstevel@tonic-gate 
930*0Sstevel@tonic-gate 		valuep->t = lval.t;
931*0Sstevel@tonic-gate 		valuep->v = lval.v * rval.v;
932*0Sstevel@tonic-gate 		return (1);
933*0Sstevel@tonic-gate 
934*0Sstevel@tonic-gate 	case T_DIV:
935*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
936*0Sstevel@tonic-gate 				arrowp, try, &lval))
937*0Sstevel@tonic-gate 			return (0);
938*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
939*0Sstevel@tonic-gate 				arrowp, try, &rval))
940*0Sstevel@tonic-gate 			return (0);
941*0Sstevel@tonic-gate 		if (check_expr_args(&lval, &rval, UINT64, np))
942*0Sstevel@tonic-gate 			return (0);
943*0Sstevel@tonic-gate 
944*0Sstevel@tonic-gate 		/* return false if dividing by zero */
945*0Sstevel@tonic-gate 		if (rval.v == 0) {
946*0Sstevel@tonic-gate 			out(O_ERR, "eval_expr: T_DIV division by zero");
947*0Sstevel@tonic-gate 			valuep->t = UNDEFINED;
948*0Sstevel@tonic-gate 			return (0);
949*0Sstevel@tonic-gate 		}
950*0Sstevel@tonic-gate 
951*0Sstevel@tonic-gate 		valuep->t = lval.t;
952*0Sstevel@tonic-gate 		valuep->v = lval.v / rval.v;
953*0Sstevel@tonic-gate 		return (1);
954*0Sstevel@tonic-gate 
955*0Sstevel@tonic-gate 	case T_MOD:
956*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.left, ex, epnames, globals, croot,
957*0Sstevel@tonic-gate 				arrowp, try, &lval))
958*0Sstevel@tonic-gate 			return (0);
959*0Sstevel@tonic-gate 		if (!eval_expr(np->u.expr.right, ex, epnames, globals, croot,
960*0Sstevel@tonic-gate 				arrowp, try, &rval))
961*0Sstevel@tonic-gate 			return (0);
962*0Sstevel@tonic-gate 		if (check_expr_args(&lval, &rval, UINT64, np))
963*0Sstevel@tonic-gate 			return (0);
964*0Sstevel@tonic-gate 
965*0Sstevel@tonic-gate 		/* return false if dividing by zero */
966*0Sstevel@tonic-gate 		if (rval.v == 0) {
967*0Sstevel@tonic-gate 			out(O_ERR, "eval_expr: T_MOD division by zero");
968*0Sstevel@tonic-gate 			valuep->t = UNDEFINED;
969*0Sstevel@tonic-gate 			return (0);
970*0Sstevel@tonic-gate 		}
971*0Sstevel@tonic-gate 
972*0Sstevel@tonic-gate 		valuep->t = lval.t;
973*0Sstevel@tonic-gate 		valuep->v = lval.v % rval.v;
974*0Sstevel@tonic-gate 		return (1);
975*0Sstevel@tonic-gate 
976*0Sstevel@tonic-gate 	case T_NAME:
977*0Sstevel@tonic-gate 		if (try) {
978*0Sstevel@tonic-gate 			struct iterinfo *iterinfop;
979*0Sstevel@tonic-gate 
980*0Sstevel@tonic-gate 			/*
981*0Sstevel@tonic-gate 			 * at itree_create() time, we can expand simple
982*0Sstevel@tonic-gate 			 * iterators.  anything else we'll punt on.
983*0Sstevel@tonic-gate 			 */
984*0Sstevel@tonic-gate 			iterinfop = lut_lookup(ex, (void *)np->u.name.s, NULL);
985*0Sstevel@tonic-gate 			if (iterinfop != NULL) {
986*0Sstevel@tonic-gate 				/* explicit iterator; not part of pathname */
987*0Sstevel@tonic-gate 				valuep->t = UINT64;
988*0Sstevel@tonic-gate 				valuep->v = (unsigned long long)iterinfop->num;
989*0Sstevel@tonic-gate 				return (1);
990*0Sstevel@tonic-gate 			}
991*0Sstevel@tonic-gate 			return (0);
992*0Sstevel@tonic-gate 		}
993*0Sstevel@tonic-gate 
994*0Sstevel@tonic-gate 		/* return address of struct node */
995*0Sstevel@tonic-gate 		valuep->t = NODEPTR;
996*0Sstevel@tonic-gate 		valuep->v = (unsigned long long)np;
997*0Sstevel@tonic-gate 		return (1);
998*0Sstevel@tonic-gate 
999*0Sstevel@tonic-gate 	case T_QUOTE:
1000*0Sstevel@tonic-gate 		valuep->t = STRING;
1001*0Sstevel@tonic-gate 		valuep->v = (unsigned long long)np->u.quote.s;
1002*0Sstevel@tonic-gate 		return (1);
1003*0Sstevel@tonic-gate 
1004*0Sstevel@tonic-gate 	case T_FUNC:
1005*0Sstevel@tonic-gate 		return (eval_func(np, ex, epnames, np->u.func.arglist,
1006*0Sstevel@tonic-gate 				globals, croot, arrowp, try, valuep));
1007*0Sstevel@tonic-gate 
1008*0Sstevel@tonic-gate 	case T_NUM:
1009*0Sstevel@tonic-gate 		valuep->t = UINT64;
1010*0Sstevel@tonic-gate 		valuep->v = np->u.ull;
1011*0Sstevel@tonic-gate 		return (1);
1012*0Sstevel@tonic-gate 
1013*0Sstevel@tonic-gate 	default:
1014*0Sstevel@tonic-gate 		outfl(O_DIE, np->file, np->line,
1015*0Sstevel@tonic-gate 		    "eval_expr: unexpected node type: %s",
1016*0Sstevel@tonic-gate 		    ptree_nodetype2str(np->t));
1017*0Sstevel@tonic-gate 	}
1018*0Sstevel@tonic-gate 	/*NOTREACHED*/
1019*0Sstevel@tonic-gate }
1020*0Sstevel@tonic-gate 
1021*0Sstevel@tonic-gate /*
1022*0Sstevel@tonic-gate  * eval_fru() and eval_asru() don't do much, but are called from a number
1023*0Sstevel@tonic-gate  * of places.
1024*0Sstevel@tonic-gate  */
1025*0Sstevel@tonic-gate struct node *
1026*0Sstevel@tonic-gate eval_fru(struct node *np)
1027*0Sstevel@tonic-gate {
1028*0Sstevel@tonic-gate 	ASSERT(np->t == T_NAME);
1029*0Sstevel@tonic-gate 	return (np);
1030*0Sstevel@tonic-gate }
1031*0Sstevel@tonic-gate 
1032*0Sstevel@tonic-gate struct node *
1033*0Sstevel@tonic-gate eval_asru(struct node *np)
1034*0Sstevel@tonic-gate {
1035*0Sstevel@tonic-gate 	ASSERT(np->t == T_NAME);
1036*0Sstevel@tonic-gate 	return (np);
1037*0Sstevel@tonic-gate }
1038