xref: /titanic_52/usr/src/cmd/tnf/prex/expr.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1994, by Sun Microsytems, Inc.
24*7c478bd9Sstevel@tonic-gate  */
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate /*
29*7c478bd9Sstevel@tonic-gate  * Includes
30*7c478bd9Sstevel@tonic-gate  */
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate /* we need to define this to get strtok_r from string.h */
33*7c478bd9Sstevel@tonic-gate /* SEEMS LIKE A BUG TO ME */
34*7c478bd9Sstevel@tonic-gate #define	_REENTRANT
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #ifndef DEBUG
37*7c478bd9Sstevel@tonic-gate #define	NDEBUG	1
38*7c478bd9Sstevel@tonic-gate #endif
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #include <stdio.h>
41*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
42*7c478bd9Sstevel@tonic-gate #include <string.h>
43*7c478bd9Sstevel@tonic-gate #include <libgen.h>
44*7c478bd9Sstevel@tonic-gate #include <assert.h>
45*7c478bd9Sstevel@tonic-gate #include "spec.h"
46*7c478bd9Sstevel@tonic-gate #include "expr.h"
47*7c478bd9Sstevel@tonic-gate #include "new.h"
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate /*
51*7c478bd9Sstevel@tonic-gate  * Typedefs
52*7c478bd9Sstevel@tonic-gate  */
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate typedef enum {
55*7c478bd9Sstevel@tonic-gate 	MATCH_NONE = 0,
56*7c478bd9Sstevel@tonic-gate 	MATCH_FALSE,
57*7c478bd9Sstevel@tonic-gate 	MATCH_TRUE
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate }			   match_t;
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate /*
64*7c478bd9Sstevel@tonic-gate  * Declarations
65*7c478bd9Sstevel@tonic-gate  */
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate static boolean_t matchattrs(expr_t * expr_p, const char *attrs);
68*7c478bd9Sstevel@tonic-gate static void matchvals(spec_t * spec_p, char *attrstr,
69*7c478bd9Sstevel@tonic-gate 	char *valstr, void *calldatap);
70*7c478bd9Sstevel@tonic-gate static void matched(spec_t * spec_p, char *valstr, void *calldatap);
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate /* ---------------------------------------------------------------- */
74*7c478bd9Sstevel@tonic-gate /* ----------------------- Public Functions ----------------------- */
75*7c478bd9Sstevel@tonic-gate /* ---------------------------------------------------------------- */
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate /*
78*7c478bd9Sstevel@tonic-gate  * expr() - builds an expr
79*7c478bd9Sstevel@tonic-gate  */
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate expr_t		 *
82*7c478bd9Sstevel@tonic-gate expr(spec_t * left_p,
83*7c478bd9Sstevel@tonic-gate 	spec_t * right_p)
84*7c478bd9Sstevel@tonic-gate {
85*7c478bd9Sstevel@tonic-gate 	expr_t		 *new_p;
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	new_p = new(expr_t);
88*7c478bd9Sstevel@tonic-gate 	queue_init(&new_p->qn);
89*7c478bd9Sstevel@tonic-gate 	new_p->left_p = left_p;
90*7c478bd9Sstevel@tonic-gate 	new_p->right_p = right_p;
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	return (new_p);
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate }				/* end expr */
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate /*
98*7c478bd9Sstevel@tonic-gate  * expr_dup() - duplicates an expression list
99*7c478bd9Sstevel@tonic-gate  */
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate expr_t		 *
102*7c478bd9Sstevel@tonic-gate expr_dup(expr_t * list_p)
103*7c478bd9Sstevel@tonic-gate {
104*7c478bd9Sstevel@tonic-gate 	expr_t		 *expr_p;
105*7c478bd9Sstevel@tonic-gate 	expr_t		 *head_p;
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	if (!list_p)
108*7c478bd9Sstevel@tonic-gate 		return (NULL);
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	/* copy the first node */
111*7c478bd9Sstevel@tonic-gate 	head_p = expr(spec_dup(list_p->left_p),
112*7c478bd9Sstevel@tonic-gate 		spec_dup(list_p->right_p));
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	/* append each additional node */
115*7c478bd9Sstevel@tonic-gate 	expr_p = list_p;
116*7c478bd9Sstevel@tonic-gate 	while (expr_p = (expr_t *) queue_next(&list_p->qn, &expr_p->qn)) {
117*7c478bd9Sstevel@tonic-gate 		expr_t		 *new_p;
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 		new_p = expr(spec_dup(expr_p->left_p),
120*7c478bd9Sstevel@tonic-gate 			spec_dup(expr_p->right_p));
121*7c478bd9Sstevel@tonic-gate 		(void) queue_append(&head_p->qn, &new_p->qn);
122*7c478bd9Sstevel@tonic-gate 	}
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 	return (head_p);
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate }				/* end expr_dup */
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate /*
130*7c478bd9Sstevel@tonic-gate  * expr_destroy() - destroys an expression list
131*7c478bd9Sstevel@tonic-gate  */
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate void
134*7c478bd9Sstevel@tonic-gate expr_destroy(expr_t * list_p)
135*7c478bd9Sstevel@tonic-gate {
136*7c478bd9Sstevel@tonic-gate 	expr_t		 *expr_p;
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 	while (expr_p = (expr_t *) queue_next(&list_p->qn, &list_p->qn)) {
139*7c478bd9Sstevel@tonic-gate 		(void) queue_remove(&expr_p->qn);
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 		if (expr_p->left_p)
142*7c478bd9Sstevel@tonic-gate 			spec_destroy(expr_p->left_p);
143*7c478bd9Sstevel@tonic-gate 		if (expr_p->right_p)
144*7c478bd9Sstevel@tonic-gate 			spec_destroy(expr_p->right_p);
145*7c478bd9Sstevel@tonic-gate 		free(expr_p);
146*7c478bd9Sstevel@tonic-gate 	}
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 	if (list_p->left_p)
149*7c478bd9Sstevel@tonic-gate 		spec_destroy(list_p->left_p);
150*7c478bd9Sstevel@tonic-gate 	if (list_p->right_p)
151*7c478bd9Sstevel@tonic-gate 		spec_destroy(list_p->right_p);
152*7c478bd9Sstevel@tonic-gate 	free(list_p);
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate }				/* end expr_destroy */
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate /*
158*7c478bd9Sstevel@tonic-gate  * expr_list() - append a expr_t to a list
159*7c478bd9Sstevel@tonic-gate  */
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate expr_t		 *
162*7c478bd9Sstevel@tonic-gate expr_list(expr_t * h,
163*7c478bd9Sstevel@tonic-gate 	expr_t * f)
164*7c478bd9Sstevel@tonic-gate {
165*7c478bd9Sstevel@tonic-gate 	/* queue append handles the NULL cases OK */
166*7c478bd9Sstevel@tonic-gate 	return ((expr_t *) queue_append(&h->qn, &f->qn));
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate }				/* end expr_list */
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate /*
172*7c478bd9Sstevel@tonic-gate  * expr_print() - pretty prints an expr list
173*7c478bd9Sstevel@tonic-gate  */
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate void
176*7c478bd9Sstevel@tonic-gate expr_print(FILE * stream,
177*7c478bd9Sstevel@tonic-gate 	expr_t * list_p)
178*7c478bd9Sstevel@tonic-gate {
179*7c478bd9Sstevel@tonic-gate 	expr_t		 *expr_p = NULL;
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	while ((expr_p = (expr_t *) queue_next(&list_p->qn, &expr_p->qn))) {
182*7c478bd9Sstevel@tonic-gate 		spec_print(stream, expr_p->left_p);
183*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stream, "=");
184*7c478bd9Sstevel@tonic-gate 		spec_print(stream, expr_p->right_p);
185*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stream, " ");
186*7c478bd9Sstevel@tonic-gate 	}
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate }				/* end expr_print */
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate /*
192*7c478bd9Sstevel@tonic-gate  * expr_match() - figures out whether a probe matches in an expression list
193*7c478bd9Sstevel@tonic-gate  */
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate boolean_t
196*7c478bd9Sstevel@tonic-gate expr_match(expr_t * list_p,
197*7c478bd9Sstevel@tonic-gate 	const char *attrs)
198*7c478bd9Sstevel@tonic-gate {
199*7c478bd9Sstevel@tonic-gate 	expr_t		 *expr_p = NULL;
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate 	while ((expr_p = (expr_t *) queue_next(&list_p->qn, &expr_p->qn))) {
202*7c478bd9Sstevel@tonic-gate 		if (matchattrs(expr_p, attrs))
203*7c478bd9Sstevel@tonic-gate 			return (B_TRUE);
204*7c478bd9Sstevel@tonic-gate 	}
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	return (B_FALSE);
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate }				/* end expr_match */
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate /* ---------------------------------------------------------------- */
212*7c478bd9Sstevel@tonic-gate /* ----------------------- Private Functions ---------------------- */
213*7c478bd9Sstevel@tonic-gate /* ---------------------------------------------------------------- */
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate typedef struct matchargs {
216*7c478bd9Sstevel@tonic-gate 	spec_t		 *spec_p;
217*7c478bd9Sstevel@tonic-gate 	boolean_t	   match;
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate }			   matchargs_t;
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate static		  boolean_t
222*7c478bd9Sstevel@tonic-gate matchattrs(expr_t * expr_p,
223*7c478bd9Sstevel@tonic-gate 	const char *attrs)
224*7c478bd9Sstevel@tonic-gate {
225*7c478bd9Sstevel@tonic-gate 	matchargs_t	 args;
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 	args.spec_p = expr_p->right_p;
228*7c478bd9Sstevel@tonic-gate 	args.match = B_FALSE;
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate 	spec_attrtrav(expr_p->left_p,
231*7c478bd9Sstevel@tonic-gate 		(char *) attrs, matchvals, (void *) &args);
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 	return (args.match);
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate }				/* end matchattrs */
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
239*7c478bd9Sstevel@tonic-gate static void
240*7c478bd9Sstevel@tonic-gate matchvals(spec_t * spec_p,
241*7c478bd9Sstevel@tonic-gate 	char *attrstr,
242*7c478bd9Sstevel@tonic-gate 	char *valstr,
243*7c478bd9Sstevel@tonic-gate 	void *calldatap)
244*7c478bd9Sstevel@tonic-gate {
245*7c478bd9Sstevel@tonic-gate 	matchargs_t	*args_p = (matchargs_t *) calldatap;
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 	spec_valtrav(args_p->spec_p, valstr, matched, calldatap);
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate }				/* matchvals */
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
253*7c478bd9Sstevel@tonic-gate static void
254*7c478bd9Sstevel@tonic-gate matched(spec_t * spec_p,
255*7c478bd9Sstevel@tonic-gate 	char *valstr,
256*7c478bd9Sstevel@tonic-gate 	void *calldatap)
257*7c478bd9Sstevel@tonic-gate {
258*7c478bd9Sstevel@tonic-gate 	matchargs_t	*args_p = (matchargs_t *) calldatap;
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 	args_p->match = B_TRUE;
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate }				/* end matched */
263