xref: /onnv-gate/usr/src/cmd/fm/eversholt/common/tree.c (revision 4436:35b72f77cdd9)
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
52869Sgavinm  * Common Development and Distribution License (the "License").
62869Sgavinm  * 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  */
210Sstevel@tonic-gate /*
22*4436Sstephh  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * tree.c -- routines for manipulating the prop tree
260Sstevel@tonic-gate  *
270Sstevel@tonic-gate  * the actions in escparse.y call these routines to construct
280Sstevel@tonic-gate  * the parse tree.  these routines, in turn, call the check_X()
290Sstevel@tonic-gate  * routines for semantic checking.
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <stdio.h>
350Sstevel@tonic-gate #include <stdlib.h>
360Sstevel@tonic-gate #include <ctype.h>
370Sstevel@tonic-gate #include <strings.h>
380Sstevel@tonic-gate #include <alloca.h>
390Sstevel@tonic-gate #include "alloc.h"
400Sstevel@tonic-gate #include "out.h"
410Sstevel@tonic-gate #include "stats.h"
420Sstevel@tonic-gate #include "stable.h"
430Sstevel@tonic-gate #include "literals.h"
440Sstevel@tonic-gate #include "lut.h"
450Sstevel@tonic-gate #include "esclex.h"
460Sstevel@tonic-gate #include "tree.h"
470Sstevel@tonic-gate #include "check.h"
480Sstevel@tonic-gate #include "ptree.h"
490Sstevel@tonic-gate 
500Sstevel@tonic-gate static struct node *Root;
510Sstevel@tonic-gate 
520Sstevel@tonic-gate static char *Newname;
530Sstevel@tonic-gate 
540Sstevel@tonic-gate static struct stats *Faultcount;
550Sstevel@tonic-gate static struct stats *Upsetcount;
560Sstevel@tonic-gate static struct stats *Defectcount;
570Sstevel@tonic-gate static struct stats *Errorcount;
580Sstevel@tonic-gate static struct stats *Ereportcount;
590Sstevel@tonic-gate static struct stats *SERDcount;
601414Scindi static struct stats *STATcount;
610Sstevel@tonic-gate static struct stats *ASRUcount;
620Sstevel@tonic-gate static struct stats *FRUcount;
630Sstevel@tonic-gate static struct stats *Configcount;
640Sstevel@tonic-gate static struct stats *Propcount;
650Sstevel@tonic-gate static struct stats *Maskcount;
660Sstevel@tonic-gate static struct stats *Nodecount;
670Sstevel@tonic-gate static struct stats *Namecount;
680Sstevel@tonic-gate static struct stats *Nodesize;
690Sstevel@tonic-gate 
70*4436Sstephh struct lut *Usedprops;
71*4436Sstephh 
720Sstevel@tonic-gate void
730Sstevel@tonic-gate tree_init(void)
740Sstevel@tonic-gate {
750Sstevel@tonic-gate 	Faultcount = stats_new_counter("parser.fault", "fault decls", 1);
760Sstevel@tonic-gate 	Upsetcount = stats_new_counter("parser.upset", "upset decls", 1);
770Sstevel@tonic-gate 	Defectcount = stats_new_counter("parser.defect", "defect decls", 1);
780Sstevel@tonic-gate 	Errorcount = stats_new_counter("parser.error", "error decls", 1);
790Sstevel@tonic-gate 	Ereportcount = stats_new_counter("parser.ereport", "ereport decls", 1);
800Sstevel@tonic-gate 	SERDcount = stats_new_counter("parser.SERD", "SERD engine decls", 1);
811414Scindi 	STATcount = stats_new_counter("parser.STAT", "STAT engine decls", 1);
820Sstevel@tonic-gate 	ASRUcount = stats_new_counter("parser.ASRU", "ASRU decls", 1);
830Sstevel@tonic-gate 	FRUcount = stats_new_counter("parser.FRU", "FRU decls", 1);
840Sstevel@tonic-gate 	Configcount = stats_new_counter("parser.config", "config stmts", 1);
850Sstevel@tonic-gate 	Propcount = stats_new_counter("parser.prop", "prop stmts", 1);
860Sstevel@tonic-gate 	Maskcount = stats_new_counter("parser.mask", "mask stmts", 1);
870Sstevel@tonic-gate 	Nodecount = stats_new_counter("parser.node", "nodes created", 1);
880Sstevel@tonic-gate 	Namecount = stats_new_counter("parser.name", "names created", 1);
890Sstevel@tonic-gate 	Nodesize =
900Sstevel@tonic-gate 	    stats_new_counter("parser.nodesize", "sizeof(struct node)", 1);
910Sstevel@tonic-gate 	stats_counter_add(Nodesize, sizeof (struct node));
920Sstevel@tonic-gate }
930Sstevel@tonic-gate 
940Sstevel@tonic-gate void
950Sstevel@tonic-gate tree_fini(void)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate 	stats_delete(Faultcount);
980Sstevel@tonic-gate 	stats_delete(Upsetcount);
990Sstevel@tonic-gate 	stats_delete(Defectcount);
1000Sstevel@tonic-gate 	stats_delete(Errorcount);
1010Sstevel@tonic-gate 	stats_delete(Ereportcount);
1020Sstevel@tonic-gate 	stats_delete(SERDcount);
1031414Scindi 	stats_delete(STATcount);
1040Sstevel@tonic-gate 	stats_delete(ASRUcount);
1050Sstevel@tonic-gate 	stats_delete(FRUcount);
1060Sstevel@tonic-gate 	stats_delete(Configcount);
1070Sstevel@tonic-gate 	stats_delete(Propcount);
1080Sstevel@tonic-gate 	stats_delete(Maskcount);
1090Sstevel@tonic-gate 	stats_delete(Nodecount);
1100Sstevel@tonic-gate 	stats_delete(Namecount);
1110Sstevel@tonic-gate 	stats_delete(Nodesize);
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	/* free entire parse tree */
1140Sstevel@tonic-gate 	tree_free(Root);
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	/* free up the luts we keep for decls */
1170Sstevel@tonic-gate 	lut_free(Faults, NULL, NULL);
1180Sstevel@tonic-gate 	Faults = NULL;
1190Sstevel@tonic-gate 	lut_free(Upsets, NULL, NULL);
1200Sstevel@tonic-gate 	Upsets = NULL;
1210Sstevel@tonic-gate 	lut_free(Defects, NULL, NULL);
1220Sstevel@tonic-gate 	Defects = NULL;
1230Sstevel@tonic-gate 	lut_free(Errors, NULL, NULL);
1240Sstevel@tonic-gate 	Errors = NULL;
1250Sstevel@tonic-gate 	lut_free(Ereports, NULL, NULL);
1260Sstevel@tonic-gate 	Ereports = NULL;
1270Sstevel@tonic-gate 	lut_free(Ereportenames, NULL, NULL);
1280Sstevel@tonic-gate 	Ereportenames = NULL;
1290Sstevel@tonic-gate 	lut_free(SERDs, NULL, NULL);
1300Sstevel@tonic-gate 	SERDs = NULL;
1311414Scindi 	lut_free(STATs, NULL, NULL);
1321414Scindi 	STATs = NULL;
1330Sstevel@tonic-gate 	lut_free(ASRUs, NULL, NULL);
1340Sstevel@tonic-gate 	ASRUs = NULL;
1350Sstevel@tonic-gate 	lut_free(FRUs, NULL, NULL);
1360Sstevel@tonic-gate 	FRUs = NULL;
1370Sstevel@tonic-gate 	lut_free(Configs, NULL, NULL);
1380Sstevel@tonic-gate 	Configs = NULL;
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	Props = Lastprops = NULL;
1410Sstevel@tonic-gate 	Masks = Lastmasks = NULL;
1420Sstevel@tonic-gate 	Problems = Lastproblems = NULL;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	if (Newname != NULL) {
1450Sstevel@tonic-gate 		FREE(Newname);
1460Sstevel@tonic-gate 		Newname = NULL;
1470Sstevel@tonic-gate 	}
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate 
150*4436Sstephh /*ARGSUSED*/
151*4436Sstephh static int
152*4436Sstephh nodesize(enum nodetype t, struct node *ret)
153*4436Sstephh {
154*4436Sstephh 	int size = sizeof (struct node);
155*4436Sstephh 
156*4436Sstephh 	switch (t) {
157*4436Sstephh 	case T_NAME:
158*4436Sstephh 		size += sizeof (ret->u.name) - sizeof (ret->u);
159*4436Sstephh 		break;
160*4436Sstephh 
161*4436Sstephh 	case T_GLOBID:
162*4436Sstephh 		size += sizeof (ret->u.globid) - sizeof (ret->u);
163*4436Sstephh 		break;
164*4436Sstephh 
165*4436Sstephh 	case T_TIMEVAL:
166*4436Sstephh 	case T_NUM:
167*4436Sstephh 		size += sizeof (ret->u.ull) - sizeof (ret->u);
168*4436Sstephh 		break;
169*4436Sstephh 
170*4436Sstephh 	case T_QUOTE:
171*4436Sstephh 		size += sizeof (ret->u.quote) - sizeof (ret->u);
172*4436Sstephh 		break;
173*4436Sstephh 
174*4436Sstephh 	case T_FUNC:
175*4436Sstephh 		size += sizeof (ret->u.func) - sizeof (ret->u);
176*4436Sstephh 		break;
177*4436Sstephh 
178*4436Sstephh 	case T_FAULT:
179*4436Sstephh 	case T_UPSET:
180*4436Sstephh 	case T_DEFECT:
181*4436Sstephh 	case T_ERROR:
182*4436Sstephh 	case T_EREPORT:
183*4436Sstephh 	case T_ASRU:
184*4436Sstephh 	case T_FRU:
185*4436Sstephh 	case T_SERD:
186*4436Sstephh 	case T_STAT:
187*4436Sstephh 	case T_CONFIG:
188*4436Sstephh 	case T_PROP:
189*4436Sstephh 	case T_MASK:
190*4436Sstephh 		size += sizeof (ret->u.stmt) - sizeof (ret->u);
191*4436Sstephh 		break;
192*4436Sstephh 
193*4436Sstephh 	case T_EVENT:
194*4436Sstephh 		size += sizeof (ret->u.event) - sizeof (ret->u);
195*4436Sstephh 		break;
196*4436Sstephh 
197*4436Sstephh 	case T_ARROW:
198*4436Sstephh 		size += sizeof (ret->u.arrow) - sizeof (ret->u);
199*4436Sstephh 		break;
200*4436Sstephh 
201*4436Sstephh 	default:
202*4436Sstephh 		size += sizeof (ret->u.expr) - sizeof (ret->u);
203*4436Sstephh 		break;
204*4436Sstephh 	}
205*4436Sstephh 	return (size);
206*4436Sstephh }
207*4436Sstephh 
2080Sstevel@tonic-gate struct node *
2090Sstevel@tonic-gate newnode(enum nodetype t, const char *file, int line)
2100Sstevel@tonic-gate {
211*4436Sstephh 	struct node *ret = NULL;
212*4436Sstephh 	int size = nodesize(t, ret);
2130Sstevel@tonic-gate 
214*4436Sstephh 	ret = alloc_xmalloc(size);
2150Sstevel@tonic-gate 	stats_counter_bump(Nodecount);
216*4436Sstephh 	bzero(ret, size);
2170Sstevel@tonic-gate 	ret->t = t;
2180Sstevel@tonic-gate 	ret->file = (file == NULL) ? "<nofile>" : file;
2190Sstevel@tonic-gate 	ret->line = line;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	return (ret);
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate /*ARGSUSED*/
2250Sstevel@tonic-gate void
2260Sstevel@tonic-gate tree_free(struct node *root)
2270Sstevel@tonic-gate {
2280Sstevel@tonic-gate 	if (root == NULL)
2290Sstevel@tonic-gate 		return;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	switch (root->t) {
2320Sstevel@tonic-gate 	case T_NAME:
2330Sstevel@tonic-gate 		tree_free(root->u.name.child);
2340Sstevel@tonic-gate 		tree_free(root->u.name.next);
2350Sstevel@tonic-gate 		break;
2360Sstevel@tonic-gate 	case T_FUNC:
2370Sstevel@tonic-gate 		tree_free(root->u.func.arglist);
2380Sstevel@tonic-gate 		break;
2390Sstevel@tonic-gate 	case T_AND:
2400Sstevel@tonic-gate 	case T_OR:
2410Sstevel@tonic-gate 	case T_EQ:
2420Sstevel@tonic-gate 	case T_NE:
2430Sstevel@tonic-gate 	case T_ADD:
2440Sstevel@tonic-gate 	case T_DIV:
2450Sstevel@tonic-gate 	case T_MOD:
2460Sstevel@tonic-gate 	case T_MUL:
2470Sstevel@tonic-gate 	case T_SUB:
2480Sstevel@tonic-gate 	case T_LT:
2490Sstevel@tonic-gate 	case T_LE:
2500Sstevel@tonic-gate 	case T_GT:
2510Sstevel@tonic-gate 	case T_GE:
2520Sstevel@tonic-gate 	case T_BITAND:
2530Sstevel@tonic-gate 	case T_BITOR:
2540Sstevel@tonic-gate 	case T_BITXOR:
2550Sstevel@tonic-gate 	case T_BITNOT:
2560Sstevel@tonic-gate 	case T_LSHIFT:
2570Sstevel@tonic-gate 	case T_RSHIFT:
2580Sstevel@tonic-gate 	case T_NVPAIR:
2590Sstevel@tonic-gate 	case T_ASSIGN:
2600Sstevel@tonic-gate 	case T_CONDIF:
2610Sstevel@tonic-gate 	case T_CONDELSE:
2620Sstevel@tonic-gate 	case T_LIST:
2630Sstevel@tonic-gate 		tree_free(root->u.expr.left);
2640Sstevel@tonic-gate 		tree_free(root->u.expr.right);
2650Sstevel@tonic-gate 		break;
2660Sstevel@tonic-gate 	case T_EVENT:
2670Sstevel@tonic-gate 		tree_free(root->u.event.ename);
2680Sstevel@tonic-gate 		tree_free(root->u.event.epname);
2690Sstevel@tonic-gate 		tree_free(root->u.event.eexprlist);
2700Sstevel@tonic-gate 		break;
2710Sstevel@tonic-gate 	case T_NOT:
2720Sstevel@tonic-gate 		tree_free(root->u.expr.left);
2730Sstevel@tonic-gate 		break;
2740Sstevel@tonic-gate 	case T_ARROW:
2750Sstevel@tonic-gate 		tree_free(root->u.arrow.lhs);
2760Sstevel@tonic-gate 		tree_free(root->u.arrow.nnp);
2770Sstevel@tonic-gate 		tree_free(root->u.arrow.knp);
2780Sstevel@tonic-gate 		tree_free(root->u.arrow.rhs);
2790Sstevel@tonic-gate 		break;
2800Sstevel@tonic-gate 	case T_PROP:
2810Sstevel@tonic-gate 	case T_MASK:
2820Sstevel@tonic-gate 		tree_free(root->u.stmt.np);
2830Sstevel@tonic-gate 		break;
2840Sstevel@tonic-gate 	case T_FAULT:
2850Sstevel@tonic-gate 	case T_UPSET:
2860Sstevel@tonic-gate 	case T_DEFECT:
2870Sstevel@tonic-gate 	case T_ERROR:
2880Sstevel@tonic-gate 	case T_EREPORT:
2890Sstevel@tonic-gate 	case T_ASRU:
2900Sstevel@tonic-gate 	case T_FRU:
2910Sstevel@tonic-gate 	case T_SERD:
2921414Scindi 	case T_STAT:
2930Sstevel@tonic-gate 	case T_CONFIG:
2940Sstevel@tonic-gate 		tree_free(root->u.stmt.np);
2950Sstevel@tonic-gate 		if (root->u.stmt.nvpairs)
2960Sstevel@tonic-gate 			tree_free(root->u.stmt.nvpairs);
2970Sstevel@tonic-gate 		if (root->u.stmt.lutp)
2980Sstevel@tonic-gate 			lut_free(root->u.stmt.lutp, NULL, NULL);
2990Sstevel@tonic-gate 		break;
3000Sstevel@tonic-gate 	case T_TIMEVAL:
3010Sstevel@tonic-gate 	case T_NUM:
3020Sstevel@tonic-gate 	case T_QUOTE:
3030Sstevel@tonic-gate 	case T_GLOBID:
3040Sstevel@tonic-gate 	case T_NOTHING:
3050Sstevel@tonic-gate 		break;
3060Sstevel@tonic-gate 	default:
3070Sstevel@tonic-gate 		out(O_DIE,
3080Sstevel@tonic-gate 		    "internal error: tree_free unexpected nodetype: %d",
3090Sstevel@tonic-gate 		    root->t);
3100Sstevel@tonic-gate 		/*NOTREACHED*/
3110Sstevel@tonic-gate 	}
312*4436Sstephh 	alloc_xfree((char *)root, nodesize(root->t, root));
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate static int
3160Sstevel@tonic-gate tree_treecmp(struct node *np1, struct node *np2, enum nodetype t,
3170Sstevel@tonic-gate 	    lut_cmp cmp_func)
3180Sstevel@tonic-gate {
3190Sstevel@tonic-gate 	if (np1 == NULL || np2 == NULL)
3200Sstevel@tonic-gate 		return (0);
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	if (np1->t != np2->t)
3230Sstevel@tonic-gate 		return (1);
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	ASSERT(cmp_func != NULL);
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	if (np1->t == t)
3280Sstevel@tonic-gate 		return ((*cmp_func)(np1, np2));
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	switch (np1->t) {
3310Sstevel@tonic-gate 	case T_NAME:
3320Sstevel@tonic-gate 		if (tree_treecmp(np1->u.name.child, np2->u.name.child, t,
333*4436Sstephh 		    cmp_func))
3340Sstevel@tonic-gate 			return (1);
3350Sstevel@tonic-gate 		return (tree_treecmp(np1->u.name.next, np2->u.name.next, t,
336*4436Sstephh 		    cmp_func));
3370Sstevel@tonic-gate 		/*NOTREACHED*/
3380Sstevel@tonic-gate 		break;
3390Sstevel@tonic-gate 	case T_FUNC:
3400Sstevel@tonic-gate 		return (tree_treecmp(np1->u.func.arglist, np2->u.func.arglist,
341*4436Sstephh 		    t, cmp_func));
3420Sstevel@tonic-gate 		/*NOTREACHED*/
3430Sstevel@tonic-gate 		break;
3440Sstevel@tonic-gate 	case T_AND:
3450Sstevel@tonic-gate 	case T_OR:
3460Sstevel@tonic-gate 	case T_EQ:
3470Sstevel@tonic-gate 	case T_NE:
3480Sstevel@tonic-gate 	case T_ADD:
3490Sstevel@tonic-gate 	case T_DIV:
3500Sstevel@tonic-gate 	case T_MOD:
3510Sstevel@tonic-gate 	case T_MUL:
3520Sstevel@tonic-gate 	case T_SUB:
3530Sstevel@tonic-gate 	case T_LT:
3540Sstevel@tonic-gate 	case T_LE:
3550Sstevel@tonic-gate 	case T_GT:
3560Sstevel@tonic-gate 	case T_GE:
3570Sstevel@tonic-gate 	case T_BITAND:
3580Sstevel@tonic-gate 	case T_BITOR:
3590Sstevel@tonic-gate 	case T_BITXOR:
3600Sstevel@tonic-gate 	case T_BITNOT:
3610Sstevel@tonic-gate 	case T_LSHIFT:
3620Sstevel@tonic-gate 	case T_RSHIFT:
3630Sstevel@tonic-gate 	case T_NVPAIR:
3640Sstevel@tonic-gate 	case T_ASSIGN:
3650Sstevel@tonic-gate 	case T_CONDIF:
3660Sstevel@tonic-gate 	case T_CONDELSE:
3670Sstevel@tonic-gate 	case T_LIST:
3680Sstevel@tonic-gate 		if (tree_treecmp(np1->u.expr.left, np2->u.expr.left, t,
369*4436Sstephh 		    cmp_func))
3700Sstevel@tonic-gate 			return (1);
3710Sstevel@tonic-gate 		return (tree_treecmp(np1->u.expr.right, np2->u.expr.right, t,
372*4436Sstephh 		    cmp_func));
3730Sstevel@tonic-gate 		/*NOTREACHED*/
3740Sstevel@tonic-gate 		break;
3750Sstevel@tonic-gate 	case T_EVENT:
3760Sstevel@tonic-gate 		if (tree_treecmp(np1->u.event.ename, np2->u.event.ename, t,
377*4436Sstephh 		    cmp_func))
3780Sstevel@tonic-gate 			return (1);
3790Sstevel@tonic-gate 		if (tree_treecmp(np1->u.event.epname, np2->u.event.epname, t,
380*4436Sstephh 		    cmp_func))
3810Sstevel@tonic-gate 			return (1);
3820Sstevel@tonic-gate 		return (tree_treecmp(np1->u.event.eexprlist,
383*4436Sstephh 		    np2->u.event.eexprlist, t, cmp_func));
3840Sstevel@tonic-gate 		/*NOTREACHED*/
3850Sstevel@tonic-gate 		break;
3860Sstevel@tonic-gate 	case T_NOT:
3870Sstevel@tonic-gate 		return (tree_treecmp(np1->u.expr.left, np2->u.expr.left, t,
388*4436Sstephh 		    cmp_func));
3890Sstevel@tonic-gate 		/*NOTREACHED*/
3900Sstevel@tonic-gate 		break;
3910Sstevel@tonic-gate 	case T_ARROW:
3920Sstevel@tonic-gate 		if (tree_treecmp(np1->u.arrow.lhs, np2->u.arrow.lhs, t,
393*4436Sstephh 		    cmp_func))
3940Sstevel@tonic-gate 			return (1);
3950Sstevel@tonic-gate 		if (tree_treecmp(np1->u.arrow.nnp, np2->u.arrow.nnp, t,
396*4436Sstephh 		    cmp_func))
3970Sstevel@tonic-gate 			return (1);
3980Sstevel@tonic-gate 		if (tree_treecmp(np1->u.arrow.knp, np2->u.arrow.knp, t,
399*4436Sstephh 		    cmp_func))
4000Sstevel@tonic-gate 			return (1);
4010Sstevel@tonic-gate 		return (tree_treecmp(np1->u.arrow.rhs, np2->u.arrow.rhs, t,
402*4436Sstephh 		    cmp_func));
4030Sstevel@tonic-gate 		/*NOTREACHED*/
4040Sstevel@tonic-gate 		break;
4050Sstevel@tonic-gate 	case T_PROP:
4060Sstevel@tonic-gate 	case T_MASK:
4070Sstevel@tonic-gate 		return (tree_treecmp(np1->u.stmt.np, np2->u.stmt.np, t,
408*4436Sstephh 		    cmp_func));
4090Sstevel@tonic-gate 		/*NOTREACHED*/
4100Sstevel@tonic-gate 		break;
4110Sstevel@tonic-gate 	case T_FAULT:
4120Sstevel@tonic-gate 	case T_UPSET:
4130Sstevel@tonic-gate 	case T_DEFECT:
4140Sstevel@tonic-gate 	case T_ERROR:
4150Sstevel@tonic-gate 	case T_EREPORT:
4160Sstevel@tonic-gate 	case T_ASRU:
4170Sstevel@tonic-gate 	case T_FRU:
4180Sstevel@tonic-gate 	case T_SERD:
4191414Scindi 	case T_STAT:
4200Sstevel@tonic-gate 		if (tree_treecmp(np1->u.stmt.np, np2->u.stmt.np, t, cmp_func))
4210Sstevel@tonic-gate 			return (1);
4220Sstevel@tonic-gate 		return (tree_treecmp(np1->u.stmt.nvpairs, np2->u.stmt.nvpairs,
423*4436Sstephh 		    t, cmp_func));
4240Sstevel@tonic-gate 		/*NOTREACHED*/
4250Sstevel@tonic-gate 		break;
4260Sstevel@tonic-gate 	case T_TIMEVAL:
4270Sstevel@tonic-gate 	case T_NUM:
4280Sstevel@tonic-gate 	case T_QUOTE:
4290Sstevel@tonic-gate 	case T_GLOBID:
4300Sstevel@tonic-gate 	case T_NOTHING:
4310Sstevel@tonic-gate 		break;
4320Sstevel@tonic-gate 	default:
4330Sstevel@tonic-gate 		out(O_DIE,
4340Sstevel@tonic-gate 		    "internal error: tree_treecmp unexpected nodetype: %d",
4350Sstevel@tonic-gate 		    np1->t);
4360Sstevel@tonic-gate 		/*NOTREACHED*/
4370Sstevel@tonic-gate 		break;
4380Sstevel@tonic-gate 	}
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	return (0);
4410Sstevel@tonic-gate }
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate struct node *
4440Sstevel@tonic-gate tree_root(struct node *np)
4450Sstevel@tonic-gate {
4460Sstevel@tonic-gate 	if (np)
4470Sstevel@tonic-gate 		Root = np;
4480Sstevel@tonic-gate 	return (Root);
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate struct node *
4520Sstevel@tonic-gate tree_nothing(void)
4530Sstevel@tonic-gate {
4540Sstevel@tonic-gate 	return (newnode(T_NOTHING, L_nofile, 0));
4550Sstevel@tonic-gate }
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate struct node *
4580Sstevel@tonic-gate tree_expr(enum nodetype t, struct node *left, struct node *right)
4590Sstevel@tonic-gate {
4600Sstevel@tonic-gate 	struct node *ret;
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	ASSERTinfo(left != NULL || right != NULL, ptree_nodetype2str(t));
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	ret = newnode(t,
4650Sstevel@tonic-gate 	    (left) ? left->file : right->file,
4660Sstevel@tonic-gate 	    (left) ? left->line : right->line);
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	ret->u.expr.left = left;
4690Sstevel@tonic-gate 	ret->u.expr.right = right;
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	check_expr(ret);
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	return (ret);
4740Sstevel@tonic-gate }
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate /*
4770Sstevel@tonic-gate  * ename_compress -- convert event class name in to more space-efficient form
4780Sstevel@tonic-gate  *
4790Sstevel@tonic-gate  * this routine is called after the parser has completed an "ename", which
4800Sstevel@tonic-gate  * is that part of an event that contains the class name (like ereport.x.y.z).
4810Sstevel@tonic-gate  * after this routine gets done with the ename, two things are true:
4820Sstevel@tonic-gate  *   1. the ename uses only a single struct node
4830Sstevel@tonic-gate  *   2. ename->u.name.s contains the *complete* class name, dots and all,
4840Sstevel@tonic-gate  *      entered into the string table.
4850Sstevel@tonic-gate  *
4860Sstevel@tonic-gate  * so in addition to saving space by using fewer struct nodes, this routine
4870Sstevel@tonic-gate  * allows consumers of the fault tree to assume the ename is a single
4880Sstevel@tonic-gate  * string, rather than a linked list of strings.
4890Sstevel@tonic-gate  */
4900Sstevel@tonic-gate static struct node *
4910Sstevel@tonic-gate ename_compress(struct node *ename)
4920Sstevel@tonic-gate {
4930Sstevel@tonic-gate 	char *buf;
4940Sstevel@tonic-gate 	char *cp;
4950Sstevel@tonic-gate 	int len = 0;
4960Sstevel@tonic-gate 	struct node *np;
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	if (ename == NULL)
4990Sstevel@tonic-gate 		return (ename);
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	ASSERT(ename->t == T_NAME);
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	if (ename->u.name.next == NULL)
5040Sstevel@tonic-gate 		return (ename);	/* no compression to be applied here */
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	for (np = ename; np != NULL; np = np->u.name.next) {
5070Sstevel@tonic-gate 		ASSERT(np->t == T_NAME);
5080Sstevel@tonic-gate 		len++;	/* room for '.' and final '\0' */
5090Sstevel@tonic-gate 		len += strlen(np->u.name.s);
5100Sstevel@tonic-gate 	}
5110Sstevel@tonic-gate 	cp = buf = alloca(len);
5120Sstevel@tonic-gate 	for (np = ename; np != NULL; np = np->u.name.next) {
5130Sstevel@tonic-gate 		ASSERT(np->t == T_NAME);
5140Sstevel@tonic-gate 		if (np != ename)
5150Sstevel@tonic-gate 			*cp++ = '.';
5160Sstevel@tonic-gate 		(void) strcpy(cp, np->u.name.s);
5170Sstevel@tonic-gate 		cp += strlen(cp);
5180Sstevel@tonic-gate 	}
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	ename->u.name.s = stable(buf);
5210Sstevel@tonic-gate 	tree_free(ename->u.name.next);
5220Sstevel@tonic-gate 	ename->u.name.next = NULL;
5230Sstevel@tonic-gate 	ename->u.name.last = ename;
5240Sstevel@tonic-gate 	return (ename);
5250Sstevel@tonic-gate }
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate struct node *
5280Sstevel@tonic-gate tree_event(struct node *ename, struct node *epname, struct node *eexprlist)
5290Sstevel@tonic-gate {
5300Sstevel@tonic-gate 	struct node *ret;
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	ASSERT(ename != NULL);
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	ret = newnode(T_EVENT, ename->file, ename->line);
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	ret->u.event.ename = ename_compress(ename);
5370Sstevel@tonic-gate 	ret->u.event.epname = epname;
5380Sstevel@tonic-gate 	ret->u.event.eexprlist = eexprlist;
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	check_event(ret);
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	return (ret);
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate struct node *
5460Sstevel@tonic-gate tree_name(const char *s, enum itertype it, const char *file, int line)
5470Sstevel@tonic-gate {
5480Sstevel@tonic-gate 	struct node *ret = newnode(T_NAME, file, line);
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	ASSERT(s != NULL);
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	stats_counter_bump(Namecount);
5530Sstevel@tonic-gate 	ret->u.name.t = N_UNSPEC;
5540Sstevel@tonic-gate 	ret->u.name.s = stable(s);
5550Sstevel@tonic-gate 	ret->u.name.it = it;
5560Sstevel@tonic-gate 	ret->u.name.last = ret;
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	if (it == IT_ENAME) {
5590Sstevel@tonic-gate 		/* PHASE2, possible optimization: convert to table driven */
5600Sstevel@tonic-gate 		if (s == L_fault)
5610Sstevel@tonic-gate 			ret->u.name.t = N_FAULT;
5620Sstevel@tonic-gate 		else if (s == L_upset)
5630Sstevel@tonic-gate 			ret->u.name.t = N_UPSET;
5640Sstevel@tonic-gate 		else if (s == L_defect)
5650Sstevel@tonic-gate 			ret->u.name.t = N_DEFECT;
5660Sstevel@tonic-gate 		else if (s == L_error)
5670Sstevel@tonic-gate 			ret->u.name.t = N_ERROR;
5680Sstevel@tonic-gate 		else if (s == L_ereport)
5690Sstevel@tonic-gate 			ret->u.name.t = N_EREPORT;
5700Sstevel@tonic-gate 		else if (s == L_serd)
5710Sstevel@tonic-gate 			ret->u.name.t = N_SERD;
5721414Scindi 		else if (s == L_stat)
5731414Scindi 			ret->u.name.t = N_STAT;
5740Sstevel@tonic-gate 		else
5750Sstevel@tonic-gate 			outfl(O_ERR, file, line, "unknown class: %s", s);
5760Sstevel@tonic-gate 	}
5770Sstevel@tonic-gate 	return (ret);
5780Sstevel@tonic-gate }
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate struct node *
5810Sstevel@tonic-gate tree_iname(const char *s, const char *file, int line)
5820Sstevel@tonic-gate {
5830Sstevel@tonic-gate 	struct node *ret;
5840Sstevel@tonic-gate 	char *ss;
5850Sstevel@tonic-gate 	char *ptr;
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	ASSERT(s != NULL && *s != '\0');
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 	ss = STRDUP(s);
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	ptr = &ss[strlen(ss) - 1];
5920Sstevel@tonic-gate 	if (!isdigit(*ptr)) {
5930Sstevel@tonic-gate 		outfl(O_ERR, file, line,
5940Sstevel@tonic-gate 		    "instanced name expected (i.e. \"x0/y1\")");
5950Sstevel@tonic-gate 		FREE(ss);
5960Sstevel@tonic-gate 		return (tree_name(s, IT_NONE, file, line));
5970Sstevel@tonic-gate 	}
5980Sstevel@tonic-gate 	while (ptr > ss && isdigit(*(ptr - 1)))
5990Sstevel@tonic-gate 		ptr--;
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	ret = newnode(T_NAME, file, line);
6020Sstevel@tonic-gate 	stats_counter_bump(Namecount);
6030Sstevel@tonic-gate 	ret->u.name.child = tree_num(ptr, file, line);
6040Sstevel@tonic-gate 	*ptr = '\0';
6050Sstevel@tonic-gate 	ret->u.name.t = N_UNSPEC;
6060Sstevel@tonic-gate 	ret->u.name.s = stable(ss);
6070Sstevel@tonic-gate 	ret->u.name.it = IT_NONE;
6080Sstevel@tonic-gate 	ret->u.name.last = ret;
6090Sstevel@tonic-gate 	FREE(ss);
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	return (ret);
6120Sstevel@tonic-gate }
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate struct node *
6150Sstevel@tonic-gate tree_globid(const char *s, const char *file, int line)
6160Sstevel@tonic-gate {
6170Sstevel@tonic-gate 	struct node *ret = newnode(T_GLOBID, file, line);
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	ASSERT(s != NULL);
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	ret->u.globid.s = stable(s);
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	return (ret);
6240Sstevel@tonic-gate }
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate struct node *
6270Sstevel@tonic-gate tree_name_append(struct node *np1, struct node *np2)
6280Sstevel@tonic-gate {
6290Sstevel@tonic-gate 	ASSERT(np1 != NULL && np2 != NULL);
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	if (np1->t != T_NAME)
6320Sstevel@tonic-gate 		outfl(O_DIE, np1->file, np1->line,
6330Sstevel@tonic-gate 		    "tree_name_append: internal error (np1 type %d)", np1->t);
6340Sstevel@tonic-gate 	if (np2->t != T_NAME)
6350Sstevel@tonic-gate 		outfl(O_DIE, np2->file, np2->line,
6360Sstevel@tonic-gate 		    "tree_name_append: internal error (np2 type %d)", np2->t);
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	ASSERT(np1->u.name.last != NULL);
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	np1->u.name.last->u.name.next = np2;
6410Sstevel@tonic-gate 	np1->u.name.last = np2;
6420Sstevel@tonic-gate 	return (np1);
6430Sstevel@tonic-gate }
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate /*
6460Sstevel@tonic-gate  * tree_name_repairdash -- repair a class name that contained a dash
6470Sstevel@tonic-gate  *
6480Sstevel@tonic-gate  * this routine is called by the parser when a dash is encountered
6490Sstevel@tonic-gate  * in a class name.  the event protocol allows the dashes but our
6500Sstevel@tonic-gate  * lexer considers them a separate token (arithmetic minus).  an extra
6510Sstevel@tonic-gate  * rule in the parser catches this case and calls this routine to fixup
6520Sstevel@tonic-gate  * the last component of the class name (so far) by constructing the
6530Sstevel@tonic-gate  * new stable entry for a name including the dash.
6540Sstevel@tonic-gate  */
6550Sstevel@tonic-gate struct node *
6560Sstevel@tonic-gate tree_name_repairdash(struct node *np, const char *s)
6570Sstevel@tonic-gate {
6580Sstevel@tonic-gate 	int len;
6590Sstevel@tonic-gate 	char *buf;
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	ASSERT(np != NULL && s != NULL);
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 	if (np->t != T_NAME)
6640Sstevel@tonic-gate 		outfl(O_DIE, np->file, np->line,
6650Sstevel@tonic-gate 		    "tree_name_repairdash: internal error (np type %d)",
6660Sstevel@tonic-gate 		    np->t);
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	ASSERT(np->u.name.last != NULL);
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	len = strlen(np->u.name.last->u.name.s) + 1 + strlen(s) + 1;
6710Sstevel@tonic-gate 	buf = MALLOC(len);
6720Sstevel@tonic-gate 	(void) snprintf(buf, len, "%s-%s", np->u.name.last->u.name.s, s);
6730Sstevel@tonic-gate 	np->u.name.last->u.name.s = stable(buf);
6740Sstevel@tonic-gate 	FREE(buf);
6750Sstevel@tonic-gate 	return (np);
6760Sstevel@tonic-gate }
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate struct node *
6792869Sgavinm tree_name_repairdash2(const char *s, struct node *np)
6802869Sgavinm {
6812869Sgavinm 	int len;
6822869Sgavinm 	char *buf;
6832869Sgavinm 
6842869Sgavinm 	ASSERT(np != NULL && s != NULL);
6852869Sgavinm 
6862869Sgavinm 	if (np->t != T_NAME)
6872869Sgavinm 		outfl(O_DIE, np->file, np->line,
6882869Sgavinm 		    "tree_name_repairdash: internal error (np type %d)",
6892869Sgavinm 		    np->t);
6902869Sgavinm 
6912869Sgavinm 	ASSERT(np->u.name.last != NULL);
6922869Sgavinm 
6932869Sgavinm 	len = strlen(np->u.name.last->u.name.s) + 1 + strlen(s) + 1;
6942869Sgavinm 	buf = MALLOC(len);
6952869Sgavinm 	(void) snprintf(buf, len, "%s-%s", s, np->u.name.last->u.name.s);
6962869Sgavinm 	np->u.name.last->u.name.s = stable(buf);
6972869Sgavinm 	FREE(buf);
6982869Sgavinm 	return (np);
6992869Sgavinm }
7002869Sgavinm 
7012869Sgavinm struct node *
7020Sstevel@tonic-gate tree_name_iterator(struct node *np1, struct node *np2)
7030Sstevel@tonic-gate {
7040Sstevel@tonic-gate 	ASSERT(np1 != NULL);
7050Sstevel@tonic-gate 	ASSERT(np2 != NULL);
7060Sstevel@tonic-gate 	ASSERTinfo(np1->t == T_NAME, ptree_nodetype2str(np1->t));
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	np1->u.name.child = np2;
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	check_name_iterator(np1);
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 	return (np1);
7130Sstevel@tonic-gate }
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate struct node *
7160Sstevel@tonic-gate tree_timeval(const char *s, const char *suffix, const char *file, int line)
7170Sstevel@tonic-gate {
7180Sstevel@tonic-gate 	struct node *ret = newnode(T_TIMEVAL, file, line);
7190Sstevel@tonic-gate 	const unsigned long long *ullp;
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	ASSERT(s != NULL);
7220Sstevel@tonic-gate 	ASSERT(suffix != NULL);
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	if ((ullp = lex_s2ullp_lut_lookup(Timesuffixlut, suffix)) == NULL) {
7250Sstevel@tonic-gate 		outfl(O_ERR, file, line,
7260Sstevel@tonic-gate 		    "unrecognized number suffix: %s", suffix);
7270Sstevel@tonic-gate 		/* still construct a valid timeval node so parsing continues */
7280Sstevel@tonic-gate 		ret->u.ull = 1;
7290Sstevel@tonic-gate 	} else {
7300Sstevel@tonic-gate 		ret->u.ull = (unsigned long long)strtoul(s, NULL, 0) * *ullp;
7310Sstevel@tonic-gate 	}
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 	return (ret);
7340Sstevel@tonic-gate }
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate struct node *
7370Sstevel@tonic-gate tree_num(const char *s, const char *file, int line)
7380Sstevel@tonic-gate {
7390Sstevel@tonic-gate 	struct node *ret = newnode(T_NUM, file, line);
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 	ret->u.ull = (unsigned long long)strtoul(s, NULL, 0);
7420Sstevel@tonic-gate 	return (ret);
7430Sstevel@tonic-gate }
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate struct node *
7460Sstevel@tonic-gate tree_quote(const char *s, const char *file, int line)
7470Sstevel@tonic-gate {
7480Sstevel@tonic-gate 	struct node *ret = newnode(T_QUOTE, file, line);
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	ret->u.quote.s = stable(s);
7510Sstevel@tonic-gate 	return (ret);
7520Sstevel@tonic-gate }
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate struct node *
7550Sstevel@tonic-gate tree_func(const char *s, struct node *np, const char *file, int line)
7560Sstevel@tonic-gate {
7570Sstevel@tonic-gate 	struct node *ret = newnode(T_FUNC, file, line);
758*4436Sstephh 	const char *ptr;
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	ret->u.func.s = s;
7610Sstevel@tonic-gate 	ret->u.func.arglist = np;
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	check_func(ret);
7640Sstevel@tonic-gate 
765*4436Sstephh 	/*
766*4436Sstephh 	 * keep track of the properties we're interested in so we can ignore the
767*4436Sstephh 	 * rest
768*4436Sstephh 	 */
769*4436Sstephh 	if (strcmp(s, L_confprop) == 0 || strcmp(s, L_confprop_defined) == 0) {
770*4436Sstephh 		ptr = stable(np->u.expr.right->u.quote.s);
771*4436Sstephh 		Usedprops = lut_add(Usedprops, (void *)ptr, (void *)ptr, NULL);
772*4436Sstephh 	} else if (strcmp(s, L_is_connected) == 0) {
773*4436Sstephh 		ptr = stable("connected");
774*4436Sstephh 		Usedprops = lut_add(Usedprops, (void *)ptr, (void *)ptr, NULL);
775*4436Sstephh 		ptr = stable("CONNECTED");
776*4436Sstephh 		Usedprops = lut_add(Usedprops, (void *)ptr, (void *)ptr, NULL);
777*4436Sstephh 	} else if (strcmp(s, L_is_type) == 0) {
778*4436Sstephh 		ptr = stable("type");
779*4436Sstephh 		Usedprops = lut_add(Usedprops, (void *)ptr, (void *)ptr, NULL);
780*4436Sstephh 		ptr = stable("TYPE");
781*4436Sstephh 		Usedprops = lut_add(Usedprops, (void *)ptr, (void *)ptr, NULL);
782*4436Sstephh 	} else if (strcmp(s, L_is_on) == 0) {
783*4436Sstephh 		ptr = stable("on");
784*4436Sstephh 		Usedprops = lut_add(Usedprops, (void *)ptr, (void *)ptr, NULL);
785*4436Sstephh 		ptr = stable("ON");
786*4436Sstephh 		Usedprops = lut_add(Usedprops, (void *)ptr, (void *)ptr, NULL);
787*4436Sstephh 	}
788*4436Sstephh 
7890Sstevel@tonic-gate 	return (ret);
7900Sstevel@tonic-gate }
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate /*
7930Sstevel@tonic-gate  * given a list from a prop or mask statement or a function argument,
7940Sstevel@tonic-gate  * convert all iterators to explicit iterators by inventing appropriate
7950Sstevel@tonic-gate  * iterator names.
7960Sstevel@tonic-gate  */
7970Sstevel@tonic-gate static void
7981414Scindi make_explicit(struct node *np, int eventonly)
7990Sstevel@tonic-gate {
8000Sstevel@tonic-gate 	struct node *pnp;	/* component of pathname */
8010Sstevel@tonic-gate 	struct node *pnp2;
8020Sstevel@tonic-gate 	int count;
8030Sstevel@tonic-gate 	static size_t namesz;
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	if (Newname == NULL) {
8060Sstevel@tonic-gate 		namesz = 200;
8070Sstevel@tonic-gate 		Newname = MALLOC(namesz);
8080Sstevel@tonic-gate 	}
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	if (np == NULL)
8110Sstevel@tonic-gate 		return;		/* all done */
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 	switch (np->t) {
8141414Scindi 		case T_ASSIGN:
8151414Scindi 		case T_CONDIF:
8161414Scindi 		case T_CONDELSE:
8171414Scindi 		case T_NE:
8181414Scindi 		case T_EQ:
8191414Scindi 		case T_LT:
8201414Scindi 		case T_LE:
8211414Scindi 		case T_GT:
8221414Scindi 		case T_GE:
8231414Scindi 		case T_BITAND:
8241414Scindi 		case T_BITOR:
8251414Scindi 		case T_BITXOR:
8261414Scindi 		case T_BITNOT:
8271414Scindi 		case T_LSHIFT:
8281414Scindi 		case T_RSHIFT:
8290Sstevel@tonic-gate 		case T_LIST:
8301414Scindi 		case T_AND:
8311414Scindi 		case T_OR:
8321414Scindi 		case T_NOT:
8331414Scindi 		case T_ADD:
8341414Scindi 		case T_SUB:
8351414Scindi 		case T_MUL:
8361414Scindi 		case T_DIV:
8371414Scindi 		case T_MOD:
8381414Scindi 			make_explicit(np->u.expr.left, eventonly);
8391414Scindi 			make_explicit(np->u.expr.right, eventonly);
8400Sstevel@tonic-gate 			break;
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 		case T_EVENT:
8431414Scindi 			make_explicit(np->u.event.epname, 0);
8441414Scindi 			make_explicit(np->u.event.eexprlist, 1);
8451414Scindi 			break;
8461414Scindi 
8471414Scindi 		case T_FUNC:
8481414Scindi 			make_explicit(np->u.func.arglist, eventonly);
8490Sstevel@tonic-gate 			break;
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 		case T_NAME:
8521414Scindi 			if (eventonly)
8531414Scindi 				return;
8540Sstevel@tonic-gate 			for (pnp = np; pnp != NULL; pnp = pnp->u.name.next)
8550Sstevel@tonic-gate 				if (pnp->u.name.child == NULL) {
8560Sstevel@tonic-gate 					/*
8570Sstevel@tonic-gate 					 * found implicit iterator.  convert
8580Sstevel@tonic-gate 					 * it to an explicit iterator by
8590Sstevel@tonic-gate 					 * using the name of the component
8600Sstevel@tonic-gate 					 * appended with '#' and the number
8610Sstevel@tonic-gate 					 * of times we've seen this same
8620Sstevel@tonic-gate 					 * component name in this path so far.
8630Sstevel@tonic-gate 					 */
8640Sstevel@tonic-gate 					count = 0;
8650Sstevel@tonic-gate 					for (pnp2 = np; pnp2 != NULL;
8660Sstevel@tonic-gate 					    pnp2 = pnp2->u.name.next)
8670Sstevel@tonic-gate 						if (pnp2 == pnp)
8680Sstevel@tonic-gate 							break;
8690Sstevel@tonic-gate 						else if (pnp2->u.name.s ==
8700Sstevel@tonic-gate 						    pnp->u.name.s)
8710Sstevel@tonic-gate 							count++;
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 					if (namesz < strlen(pnp->u.name.s) +
8740Sstevel@tonic-gate 					    100) {
8750Sstevel@tonic-gate 						namesz = strlen(pnp->u.name.s) +
8760Sstevel@tonic-gate 						    100;
8770Sstevel@tonic-gate 						FREE(Newname);
8780Sstevel@tonic-gate 						Newname = MALLOC(namesz);
8790Sstevel@tonic-gate 					}
8800Sstevel@tonic-gate 					/*
8810Sstevel@tonic-gate 					 * made up interator name is:
8820Sstevel@tonic-gate 					 *	name#ordinal
8830Sstevel@tonic-gate 					 * or
8840Sstevel@tonic-gate 					 *	name##ordinal
8850Sstevel@tonic-gate 					 * the first one is used for vertical
8860Sstevel@tonic-gate 					 * expansion, the second for horizontal.
8870Sstevel@tonic-gate 					 * either way, the '#' embedded in
8880Sstevel@tonic-gate 					 * the name makes it impossible to
8890Sstevel@tonic-gate 					 * collide with an actual iterator
8900Sstevel@tonic-gate 					 * given to us in the eversholt file.
8910Sstevel@tonic-gate 					 */
8920Sstevel@tonic-gate 					(void) snprintf(Newname, namesz,
8930Sstevel@tonic-gate 					    "%s#%s%d", pnp->u.name.s,
8940Sstevel@tonic-gate 					    (pnp->u.name.it == IT_HORIZONTAL) ?
8950Sstevel@tonic-gate 					    "#" : "", count);
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 					pnp->u.name.child = tree_name(Newname,
8980Sstevel@tonic-gate 					    IT_NONE, pnp->file, pnp->line);
8990Sstevel@tonic-gate 					pnp->u.name.childgen = 1;
9000Sstevel@tonic-gate 				}
9010Sstevel@tonic-gate 			break;
9020Sstevel@tonic-gate 	}
9030Sstevel@tonic-gate }
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate struct node *
9060Sstevel@tonic-gate tree_pname(struct node *np)
9070Sstevel@tonic-gate {
9081414Scindi 	make_explicit(np, 0);
9090Sstevel@tonic-gate 	return (np);
9100Sstevel@tonic-gate }
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate struct node *
9130Sstevel@tonic-gate tree_arrow(struct node *lhs, struct node *nnp, struct node *knp,
9140Sstevel@tonic-gate     struct node *rhs)
9150Sstevel@tonic-gate {
9160Sstevel@tonic-gate 	struct node *ret;
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	ASSERT(lhs != NULL || rhs != NULL);
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 	ret = newnode(T_ARROW,
9210Sstevel@tonic-gate 	    (lhs) ? lhs->file : rhs->file,
9220Sstevel@tonic-gate 	    (lhs) ? lhs->line : rhs->line);
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 	ret->u.arrow.lhs = lhs;
9250Sstevel@tonic-gate 	ret->u.arrow.nnp = nnp;
9260Sstevel@tonic-gate 	ret->u.arrow.knp = knp;
9270Sstevel@tonic-gate 	ret->u.arrow.rhs = rhs;
9280Sstevel@tonic-gate 
9291414Scindi 	make_explicit(lhs, 0);
9301414Scindi 	make_explicit(rhs, 0);
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 	check_arrow(ret);
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	return (ret);
9350Sstevel@tonic-gate }
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate static struct lut *
9380Sstevel@tonic-gate nvpair2lut(struct node *np, struct lut *lutp, enum nodetype t)
9390Sstevel@tonic-gate {
9400Sstevel@tonic-gate 	if (np) {
9410Sstevel@tonic-gate 		if (np->t == T_NVPAIR) {
9420Sstevel@tonic-gate 			ASSERTeq(np->u.expr.left->t, T_NAME,
9430Sstevel@tonic-gate 			    ptree_nodetype2str);
9440Sstevel@tonic-gate 			check_stmt_allowed_properties(t, np, lutp);
9450Sstevel@tonic-gate 			lutp = tree_s2np_lut_add(lutp,
9460Sstevel@tonic-gate 			    np->u.expr.left->u.name.s, np->u.expr.right);
9470Sstevel@tonic-gate 		} else if (np->t == T_LIST) {
9480Sstevel@tonic-gate 			lutp = nvpair2lut(np->u.expr.left, lutp, t);
9490Sstevel@tonic-gate 			lutp = nvpair2lut(np->u.expr.right, lutp, t);
9500Sstevel@tonic-gate 		} else
9510Sstevel@tonic-gate 			outfl(O_DIE, np->file, np->line,
9520Sstevel@tonic-gate 			    "internal error: nvpair2lut type %s",
9530Sstevel@tonic-gate 			    ptree_nodetype2str(np->t));
9540Sstevel@tonic-gate 	}
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 	return (lutp);
9570Sstevel@tonic-gate }
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate struct lut *
9600Sstevel@tonic-gate tree_s2np_lut_add(struct lut *root, const char *s, struct node *np)
9610Sstevel@tonic-gate {
9620Sstevel@tonic-gate 	return (lut_add(root, (void *)s, (void *)np, NULL));
9630Sstevel@tonic-gate }
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate struct node *
9660Sstevel@tonic-gate tree_s2np_lut_lookup(struct lut *root, const char *s)
9670Sstevel@tonic-gate {
9680Sstevel@tonic-gate 	return (struct node *)lut_lookup(root, (void *)s, NULL);
9690Sstevel@tonic-gate }
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate struct lut *
9720Sstevel@tonic-gate tree_name2np_lut_add(struct lut *root, struct node *namep, struct node *np)
9730Sstevel@tonic-gate {
9740Sstevel@tonic-gate 	return (lut_add(root, (void *)namep, (void *)np,
9750Sstevel@tonic-gate 	    (lut_cmp)tree_namecmp));
9760Sstevel@tonic-gate }
9770Sstevel@tonic-gate 
9780Sstevel@tonic-gate struct node *
9790Sstevel@tonic-gate tree_name2np_lut_lookup(struct lut *root, struct node *namep)
9800Sstevel@tonic-gate {
9810Sstevel@tonic-gate 	return (struct node *)
9820Sstevel@tonic-gate 	    lut_lookup(root, (void *)namep, (lut_cmp)tree_namecmp);
9830Sstevel@tonic-gate }
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate struct node *
9860Sstevel@tonic-gate tree_name2np_lut_lookup_name(struct lut *root, struct node *namep)
9870Sstevel@tonic-gate {
9880Sstevel@tonic-gate 	return (struct node *)
9890Sstevel@tonic-gate 	    lut_lookup_lhs(root, (void *)namep, (lut_cmp)tree_namecmp);
9900Sstevel@tonic-gate }
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate struct lut *
9930Sstevel@tonic-gate tree_event2np_lut_add(struct lut *root, struct node *enp, struct node *np)
9940Sstevel@tonic-gate {
9950Sstevel@tonic-gate 	return (lut_add(root, (void *)enp, (void *)np, (lut_cmp)tree_eventcmp));
9960Sstevel@tonic-gate }
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate struct node *
9990Sstevel@tonic-gate tree_event2np_lut_lookup(struct lut *root, struct node *enp)
10000Sstevel@tonic-gate {
10010Sstevel@tonic-gate 	return ((struct node *)
10020Sstevel@tonic-gate 	    lut_lookup(root, (void *)enp, (lut_cmp)tree_eventcmp));
10030Sstevel@tonic-gate }
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate struct node *
10060Sstevel@tonic-gate tree_event2np_lut_lookup_event(struct lut *root, struct node *enp)
10070Sstevel@tonic-gate {
10080Sstevel@tonic-gate 	return ((struct node *)
10090Sstevel@tonic-gate 	    lut_lookup_lhs(root, (void *)enp, (lut_cmp)tree_eventcmp));
10100Sstevel@tonic-gate }
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate static struct node *
10130Sstevel@tonic-gate dodecl(enum nodetype t, const char *file, int line,
10140Sstevel@tonic-gate     struct node *np, struct node *nvpairs, struct lut **lutpp,
10150Sstevel@tonic-gate     struct stats *countp, int justpath)
10160Sstevel@tonic-gate {
10170Sstevel@tonic-gate 	struct node *ret;
10180Sstevel@tonic-gate 	struct node *decl;
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 	/* allocate parse tree node */
10210Sstevel@tonic-gate 	ret = newnode(t, file, line);
10220Sstevel@tonic-gate 	ret->u.stmt.np = np;
10230Sstevel@tonic-gate 	ret->u.stmt.nvpairs = nvpairs;
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	/*
10260Sstevel@tonic-gate 	 * the global lut pointed to by lutpp (Faults, Defects, Upsets,
10270Sstevel@tonic-gate 	 * Errors, Ereports, Serds, FRUs, or ASRUs) keeps the first decl.
10280Sstevel@tonic-gate 	 * if this isn't the first declr, we merge the
10290Sstevel@tonic-gate 	 * nvpairs into the first decl so we have a
10300Sstevel@tonic-gate 	 * merged table to look up properties from.
10310Sstevel@tonic-gate 	 * if this is the first time we've seen this fault,
10320Sstevel@tonic-gate 	 * we add it to the global lut and start lutp
10330Sstevel@tonic-gate 	 * off with any nvpairs from this declaration statement.
10340Sstevel@tonic-gate 	 */
10350Sstevel@tonic-gate 	if (justpath && (decl = tree_name2np_lut_lookup(*lutpp, np)) == NULL) {
10360Sstevel@tonic-gate 		/* this is the first time name is declared */
10370Sstevel@tonic-gate 		stats_counter_bump(countp);
10380Sstevel@tonic-gate 		*lutpp = tree_name2np_lut_add(*lutpp, np, ret);
10390Sstevel@tonic-gate 		ret->u.stmt.lutp = nvpair2lut(nvpairs, NULL, t);
10400Sstevel@tonic-gate 	} else if (!justpath &&
10410Sstevel@tonic-gate 	    (decl = tree_event2np_lut_lookup(*lutpp, np)) == NULL) {
10420Sstevel@tonic-gate 		/* this is the first time event is declared */
10430Sstevel@tonic-gate 		stats_counter_bump(countp);
10440Sstevel@tonic-gate 		*lutpp = tree_event2np_lut_add(*lutpp, np, ret);
10450Sstevel@tonic-gate 		ret->u.stmt.lutp = nvpair2lut(nvpairs, NULL, t);
10460Sstevel@tonic-gate 	} else {
10470Sstevel@tonic-gate 		/* was declared before, just add new nvpairs to its lutp */
10480Sstevel@tonic-gate 		decl->u.stmt.lutp = nvpair2lut(nvpairs, decl->u.stmt.lutp, t);
10490Sstevel@tonic-gate 	}
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate 	return (ret);
10520Sstevel@tonic-gate }
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate /*ARGSUSED*/
10550Sstevel@tonic-gate static void
10560Sstevel@tonic-gate update_serd_refstmt(void *lhs, void *rhs, void *arg)
10570Sstevel@tonic-gate {
10580Sstevel@tonic-gate 	struct node *serd;
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 	ASSERT(rhs != NULL);
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 	serd = tree_s2np_lut_lookup(((struct node *)rhs)->u.stmt.lutp,
1063*4436Sstephh 	    L_engine);
10640Sstevel@tonic-gate 	if (serd == NULL)
10650Sstevel@tonic-gate 		return;
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 	ASSERT(serd->t == T_EVENT);
10680Sstevel@tonic-gate 	if (arg != NULL && tree_eventcmp(serd, (struct node *)arg) != 0)
10690Sstevel@tonic-gate 		return;
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 	serd = tree_event2np_lut_lookup(SERDs, serd);
10720Sstevel@tonic-gate 	if (serd != NULL)
10730Sstevel@tonic-gate 		serd->u.stmt.flags |= STMT_REF;
10740Sstevel@tonic-gate }
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate struct node *
10770Sstevel@tonic-gate tree_decl(enum nodetype t, struct node *np, struct node *nvpairs,
10780Sstevel@tonic-gate     const char *file, int line)
10790Sstevel@tonic-gate {
10800Sstevel@tonic-gate 	struct node *decl;
10810Sstevel@tonic-gate 	struct node *ret;
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 	ASSERT(np != NULL);
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	check_type_iterator(np);
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 	switch (t) {
10880Sstevel@tonic-gate 	case T_EVENT:
10890Sstevel@tonic-gate 		/* determine the type of event being declared */
10900Sstevel@tonic-gate 		ASSERT(np->u.event.ename->t == T_NAME);
10910Sstevel@tonic-gate 		switch (np->u.event.ename->u.name.t) {
10920Sstevel@tonic-gate 		case N_FAULT:
10930Sstevel@tonic-gate 			ret = dodecl(T_FAULT, file, line, np, nvpairs,
10940Sstevel@tonic-gate 			    &Faults, Faultcount, 0);
10950Sstevel@tonic-gate 			break;
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 		case N_UPSET:
10980Sstevel@tonic-gate 			ret = dodecl(T_UPSET, file, line, np, nvpairs,
10990Sstevel@tonic-gate 			    &Upsets, Upsetcount, 0);
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate 			/* increment serd statement reference */
11020Sstevel@tonic-gate 			decl = tree_event2np_lut_lookup(Upsets, np);
11030Sstevel@tonic-gate 			update_serd_refstmt(NULL, decl, NULL);
11040Sstevel@tonic-gate 			break;
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate 		case N_DEFECT:
11070Sstevel@tonic-gate 			ret = dodecl(T_DEFECT, file, line, np, nvpairs,
11080Sstevel@tonic-gate 			    &Defects, Defectcount, 0);
11090Sstevel@tonic-gate 			break;
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 		case N_ERROR:
11120Sstevel@tonic-gate 			ret = dodecl(T_ERROR, file, line, np, nvpairs,
11130Sstevel@tonic-gate 			    &Errors, Errorcount, 0);
11140Sstevel@tonic-gate 			break;
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 		case N_EREPORT:
11170Sstevel@tonic-gate 			ret = dodecl(T_EREPORT, file, line, np, nvpairs,
11180Sstevel@tonic-gate 			    &Ereports, Ereportcount, 0);
11190Sstevel@tonic-gate 			/*
11200Sstevel@tonic-gate 			 * keep a lut of just the enames, so that the DE
11210Sstevel@tonic-gate 			 * can subscribe to a uniqified list of event
11220Sstevel@tonic-gate 			 * classes.
11230Sstevel@tonic-gate 			 */
11240Sstevel@tonic-gate 			Ereportenames =
11250Sstevel@tonic-gate 			    tree_name2np_lut_add(Ereportenames,
11260Sstevel@tonic-gate 			    np->u.event.ename, np);
11270Sstevel@tonic-gate 			break;
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 		default:
11300Sstevel@tonic-gate 			outfl(O_ERR, file, line,
11310Sstevel@tonic-gate 			    "tree_decl: internal error, event name type %s",
11320Sstevel@tonic-gate 			    ptree_nametype2str(np->u.event.ename->u.name.t));
11330Sstevel@tonic-gate 		}
11340Sstevel@tonic-gate 		break;
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 	case T_ENGINE:
11370Sstevel@tonic-gate 		/* determine the type of engine being declared */
11380Sstevel@tonic-gate 		ASSERT(np->u.event.ename->t == T_NAME);
11390Sstevel@tonic-gate 		switch (np->u.event.ename->u.name.t) {
11400Sstevel@tonic-gate 		case N_SERD:
11410Sstevel@tonic-gate 			ret = dodecl(T_SERD, file, line, np, nvpairs,
11420Sstevel@tonic-gate 			    &SERDs, SERDcount, 0);
11430Sstevel@tonic-gate 			lut_walk(Upsets, update_serd_refstmt, np);
11440Sstevel@tonic-gate 			break;
11450Sstevel@tonic-gate 
11461414Scindi 		case N_STAT:
11471414Scindi 			ret = dodecl(T_STAT, file, line, np, nvpairs,
11481414Scindi 			    &STATs, STATcount, 0);
11491414Scindi 			break;
11501414Scindi 
11510Sstevel@tonic-gate 		default:
11520Sstevel@tonic-gate 			outfl(O_ERR, file, line,
11530Sstevel@tonic-gate 			    "tree_decl: internal error, engine name type %s",
11540Sstevel@tonic-gate 			    ptree_nametype2str(np->u.event.ename->u.name.t));
11550Sstevel@tonic-gate 		}
11560Sstevel@tonic-gate 		break;
11570Sstevel@tonic-gate 	case T_ASRU:
11580Sstevel@tonic-gate 		ret = dodecl(T_ASRU, file, line, np, nvpairs,
11590Sstevel@tonic-gate 		    &ASRUs, ASRUcount, 1);
11600Sstevel@tonic-gate 		break;
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 	case T_FRU:
11630Sstevel@tonic-gate 		ret = dodecl(T_FRU, file, line, np, nvpairs,
11640Sstevel@tonic-gate 		    &FRUs, FRUcount, 1);
11650Sstevel@tonic-gate 		break;
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate 	case T_CONFIG:
11680Sstevel@tonic-gate 		/*
11690Sstevel@tonic-gate 		 * config statements are different from above: they
11700Sstevel@tonic-gate 		 * are not merged at all (until the configuration cache
11710Sstevel@tonic-gate 		 * code does its own style of merging.  and the properties
11720Sstevel@tonic-gate 		 * are a free-for-all -- we don't check for allowed or
11730Sstevel@tonic-gate 		 * required config properties.
11740Sstevel@tonic-gate 		 */
11750Sstevel@tonic-gate 		ret = newnode(T_CONFIG, file, line);
11760Sstevel@tonic-gate 		ret->u.stmt.np = np;
11770Sstevel@tonic-gate 		ret->u.stmt.nvpairs = nvpairs;
11780Sstevel@tonic-gate 		ret->u.stmt.lutp = nvpair2lut(nvpairs, NULL, T_CONFIG);
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 		if (lut_lookup(Configs, np, (lut_cmp)tree_namecmp) == NULL)
11810Sstevel@tonic-gate 			stats_counter_bump(Configcount);
11820Sstevel@tonic-gate 
11830Sstevel@tonic-gate 		Configs = lut_add(Configs, (void *)np, (void *)ret, NULL);
11840Sstevel@tonic-gate 		break;
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 	default:
11870Sstevel@tonic-gate 		out(O_DIE, "tree_decl: internal error, type %s",
11880Sstevel@tonic-gate 		    ptree_nodetype2str(t));
11890Sstevel@tonic-gate 	}
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 	return (ret);
11920Sstevel@tonic-gate }
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate /* keep backpointers in arrows to the prop they belong to (used for scoping) */
11950Sstevel@tonic-gate static void
11960Sstevel@tonic-gate set_arrow_prop(struct node *prop, struct node *np)
11970Sstevel@tonic-gate {
11980Sstevel@tonic-gate 	if (np == NULL)
11990Sstevel@tonic-gate 		return;
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 	if (np->t == T_ARROW) {
12020Sstevel@tonic-gate 		np->u.arrow.prop = prop;
12030Sstevel@tonic-gate 		set_arrow_prop(prop, np->u.arrow.lhs);
12040Sstevel@tonic-gate 		/*
12050Sstevel@tonic-gate 		 * no need to recurse right or handle T_LIST since
12060Sstevel@tonic-gate 		 * T_ARROWs always cascade left and are at the top
12070Sstevel@tonic-gate 		 * of the parse tree.  (you can see this in the rule
12080Sstevel@tonic-gate 		 * for "propbody" in escparse.y.)
12090Sstevel@tonic-gate 		 */
12100Sstevel@tonic-gate 	}
12110Sstevel@tonic-gate }
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate struct node *
12140Sstevel@tonic-gate tree_stmt(enum nodetype t, struct node *np, const char *file, int line)
12150Sstevel@tonic-gate {
12160Sstevel@tonic-gate 	struct node *ret = newnode(t, file, line);
12170Sstevel@tonic-gate 	struct node *pp;
12180Sstevel@tonic-gate 	int inlist = 0;
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate 	ret->u.stmt.np = np;
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 	switch (t) {
12230Sstevel@tonic-gate 	case T_PROP:
1224854Srw145199 		check_proplists(t, np);
12250Sstevel@tonic-gate 		check_propnames(t, np, 0, 0);
12260Sstevel@tonic-gate 		check_propscope(np);
12270Sstevel@tonic-gate 		set_arrow_prop(ret, np);
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate 		for (pp = Props; pp; pp = pp->u.stmt.next) {
12300Sstevel@tonic-gate 			if (tree_treecmp(pp, ret, T_NAME,
1231*4436Sstephh 			    (lut_cmp)tree_namecmp) == 0) {
12320Sstevel@tonic-gate 				inlist = 1;
12330Sstevel@tonic-gate 				break;
12340Sstevel@tonic-gate 			}
12350Sstevel@tonic-gate 		}
12360Sstevel@tonic-gate 		if (inlist == 0)
12370Sstevel@tonic-gate 			stats_counter_bump(Propcount);
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate 		/* "Props" is a linked list of all prop statements */
12400Sstevel@tonic-gate 		if (Lastprops)
12410Sstevel@tonic-gate 			Lastprops->u.stmt.next = ret;
12420Sstevel@tonic-gate 		else
12430Sstevel@tonic-gate 			Props = ret;
12440Sstevel@tonic-gate 		Lastprops = ret;
12450Sstevel@tonic-gate 		break;
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate 	case T_MASK:
1248854Srw145199 		check_proplists(t, np);
12490Sstevel@tonic-gate 		check_propnames(t, np, 0, 0);
12500Sstevel@tonic-gate 		check_propscope(np);
12510Sstevel@tonic-gate 		set_arrow_prop(ret, np);
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate 		for (pp = Masks; pp; pp = pp->u.stmt.next) {
12540Sstevel@tonic-gate 			if (tree_treecmp(pp, ret, T_NAME,
1255*4436Sstephh 			    (lut_cmp)tree_namecmp) == 0) {
12560Sstevel@tonic-gate 				inlist = 1;
12570Sstevel@tonic-gate 				break;
12580Sstevel@tonic-gate 			}
12590Sstevel@tonic-gate 		}
12600Sstevel@tonic-gate 		if (inlist == 0)
12610Sstevel@tonic-gate 			stats_counter_bump(Maskcount);
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 		/* "Masks" is a linked list of all mask statements */
12640Sstevel@tonic-gate 		if (Lastmasks)
12650Sstevel@tonic-gate 			Lastmasks->u.stmt.next = ret;
12660Sstevel@tonic-gate 		else
12670Sstevel@tonic-gate 			Masks = ret;
12680Sstevel@tonic-gate 		Lastmasks = ret;
12690Sstevel@tonic-gate 		stats_counter_bump(Maskcount);
12700Sstevel@tonic-gate 		break;
12710Sstevel@tonic-gate 
12720Sstevel@tonic-gate 	default:
12730Sstevel@tonic-gate 		outfl(O_DIE, np->file, np->line,
12740Sstevel@tonic-gate 		    "tree_stmt: internal error (t %d)", t);
12750Sstevel@tonic-gate 	}
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 	return (ret);
12780Sstevel@tonic-gate }
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate void
12810Sstevel@tonic-gate tree_report()
12820Sstevel@tonic-gate {
12830Sstevel@tonic-gate 	/*
12840Sstevel@tonic-gate 	 * The only declarations with required properties
12850Sstevel@tonic-gate 	 * currently are faults and serds. Make sure the
12860Sstevel@tonic-gate 	 * the declarations have the required properties.
12870Sstevel@tonic-gate 	 */
12880Sstevel@tonic-gate 	lut_walk(Faults, (lut_cb)check_required_props, (void *)T_FAULT);
12890Sstevel@tonic-gate 	lut_walk(Upsets, (lut_cb)check_required_props, (void *)T_UPSET);
12900Sstevel@tonic-gate 	lut_walk(Errors, (lut_cb)check_required_props, (void *)T_ERROR);
12910Sstevel@tonic-gate 	lut_walk(Ereports, (lut_cb)check_required_props, (void *)T_EREPORT);
12920Sstevel@tonic-gate 	lut_walk(SERDs, (lut_cb)check_required_props, (void *)T_SERD);
12931414Scindi 	lut_walk(STATs, (lut_cb)check_required_props, (void *)T_STAT);
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate 	/*
12960Sstevel@tonic-gate 	 * we do this now rather than while building the parse
12970Sstevel@tonic-gate 	 * tree because it is inconvenient for the user if we
12980Sstevel@tonic-gate 	 * require SERD engines to be declared before used in
12990Sstevel@tonic-gate 	 * an upset "engine" property.
13000Sstevel@tonic-gate 	 */
13010Sstevel@tonic-gate 	lut_walk(Faults, (lut_cb)check_refcount, (void *)T_FAULT);
13020Sstevel@tonic-gate 	lut_walk(Upsets, (lut_cb)check_upset_engine, (void *)T_UPSET);
13030Sstevel@tonic-gate 	lut_walk(Upsets, (lut_cb)check_refcount, (void *)T_UPSET);
13040Sstevel@tonic-gate 	lut_walk(Errors, (lut_cb)check_refcount, (void *)T_ERROR);
13050Sstevel@tonic-gate 	lut_walk(Ereports, (lut_cb)check_refcount, (void *)T_EREPORT);
13060Sstevel@tonic-gate 	lut_walk(SERDs, (lut_cb)check_refcount, (void *)T_SERD);
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 	/* check for cycles */
13090Sstevel@tonic-gate 	lut_walk(Errors, (lut_cb)check_cycle, (void *)0);
13100Sstevel@tonic-gate }
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate /* compare two T_NAMES by only looking at components, not iterators */
13130Sstevel@tonic-gate int
13140Sstevel@tonic-gate tree_namecmp(struct node *np1, struct node *np2)
13150Sstevel@tonic-gate {
13160Sstevel@tonic-gate 	ASSERT(np1 != NULL);
13170Sstevel@tonic-gate 	ASSERT(np2 != NULL);
13180Sstevel@tonic-gate 	ASSERTinfo(np1->t == T_NAME, ptree_nodetype2str(np1->t));
13190Sstevel@tonic-gate 	ASSERTinfo(np2->t == T_NAME, ptree_nodetype2str(np1->t));
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate 	while (np1 && np2 && np1->u.name.s == np2->u.name.s) {
13220Sstevel@tonic-gate 		np1 = np1->u.name.next;
13230Sstevel@tonic-gate 		np2 = np2->u.name.next;
13240Sstevel@tonic-gate 	}
13250Sstevel@tonic-gate 	if (np1 == NULL)
13260Sstevel@tonic-gate 		if (np2 == NULL)
13270Sstevel@tonic-gate 			return (0);
13280Sstevel@tonic-gate 		else
13290Sstevel@tonic-gate 			return (-1);
13300Sstevel@tonic-gate 	else if (np2 == NULL)
13310Sstevel@tonic-gate 		return (1);
13320Sstevel@tonic-gate 	else
13330Sstevel@tonic-gate 		return (np2->u.name.s - np1->u.name.s);
13340Sstevel@tonic-gate }
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate int
13370Sstevel@tonic-gate tree_eventcmp(struct node *np1, struct node *np2)
13380Sstevel@tonic-gate {
13390Sstevel@tonic-gate 	int ret;
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate 	ASSERT(np1 != NULL);
13420Sstevel@tonic-gate 	ASSERT(np2 != NULL);
13430Sstevel@tonic-gate 	ASSERTinfo(np1->t == T_EVENT, ptree_nodetype2str(np1->t));
13440Sstevel@tonic-gate 	ASSERTinfo(np2->t == T_EVENT, ptree_nodetype2str(np2->t));
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate 	if ((ret = tree_namecmp(np1->u.event.ename,
1347*4436Sstephh 	    np2->u.event.ename)) == 0) {
13480Sstevel@tonic-gate 			if (np1->u.event.epname == NULL &&
1349*4436Sstephh 			    np2->u.event.epname == NULL)
13500Sstevel@tonic-gate 				return (0);
13510Sstevel@tonic-gate 			else if (np1->u.event.epname == NULL)
13520Sstevel@tonic-gate 				return (-1);
13530Sstevel@tonic-gate 			else if (np2->u.event.epname == NULL)
13540Sstevel@tonic-gate 				return (1);
13550Sstevel@tonic-gate 			else
13560Sstevel@tonic-gate 				return tree_namecmp(np1->u.event.epname,
1357*4436Sstephh 				    np2->u.event.epname);
13580Sstevel@tonic-gate 	} else
13590Sstevel@tonic-gate 	return (ret);
13600Sstevel@tonic-gate }
1361