xref: /onnv-gate/usr/src/cmd/tnf/prex/expr.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 (c) 1994, by Sun Microsytems, Inc.
24*0Sstevel@tonic-gate  */
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate /*
29*0Sstevel@tonic-gate  * Includes
30*0Sstevel@tonic-gate  */
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate /* we need to define this to get strtok_r from string.h */
33*0Sstevel@tonic-gate /* SEEMS LIKE A BUG TO ME */
34*0Sstevel@tonic-gate #define	_REENTRANT
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #ifndef DEBUG
37*0Sstevel@tonic-gate #define	NDEBUG	1
38*0Sstevel@tonic-gate #endif
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #include <stdio.h>
41*0Sstevel@tonic-gate #include <stdlib.h>
42*0Sstevel@tonic-gate #include <string.h>
43*0Sstevel@tonic-gate #include <libgen.h>
44*0Sstevel@tonic-gate #include <assert.h>
45*0Sstevel@tonic-gate #include "spec.h"
46*0Sstevel@tonic-gate #include "expr.h"
47*0Sstevel@tonic-gate #include "new.h"
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate /*
51*0Sstevel@tonic-gate  * Typedefs
52*0Sstevel@tonic-gate  */
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate typedef enum {
55*0Sstevel@tonic-gate 	MATCH_NONE = 0,
56*0Sstevel@tonic-gate 	MATCH_FALSE,
57*0Sstevel@tonic-gate 	MATCH_TRUE
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate }			   match_t;
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate /*
64*0Sstevel@tonic-gate  * Declarations
65*0Sstevel@tonic-gate  */
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate static boolean_t matchattrs(expr_t * expr_p, const char *attrs);
68*0Sstevel@tonic-gate static void matchvals(spec_t * spec_p, char *attrstr,
69*0Sstevel@tonic-gate 	char *valstr, void *calldatap);
70*0Sstevel@tonic-gate static void matched(spec_t * spec_p, char *valstr, void *calldatap);
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate /* ---------------------------------------------------------------- */
74*0Sstevel@tonic-gate /* ----------------------- Public Functions ----------------------- */
75*0Sstevel@tonic-gate /* ---------------------------------------------------------------- */
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate /*
78*0Sstevel@tonic-gate  * expr() - builds an expr
79*0Sstevel@tonic-gate  */
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate expr_t		 *
expr(spec_t * left_p,spec_t * right_p)82*0Sstevel@tonic-gate expr(spec_t * left_p,
83*0Sstevel@tonic-gate 	spec_t * right_p)
84*0Sstevel@tonic-gate {
85*0Sstevel@tonic-gate 	expr_t		 *new_p;
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	new_p = new(expr_t);
88*0Sstevel@tonic-gate 	queue_init(&new_p->qn);
89*0Sstevel@tonic-gate 	new_p->left_p = left_p;
90*0Sstevel@tonic-gate 	new_p->right_p = right_p;
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	return (new_p);
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate }				/* end expr */
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate /*
98*0Sstevel@tonic-gate  * expr_dup() - duplicates an expression list
99*0Sstevel@tonic-gate  */
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate expr_t		 *
expr_dup(expr_t * list_p)102*0Sstevel@tonic-gate expr_dup(expr_t * list_p)
103*0Sstevel@tonic-gate {
104*0Sstevel@tonic-gate 	expr_t		 *expr_p;
105*0Sstevel@tonic-gate 	expr_t		 *head_p;
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	if (!list_p)
108*0Sstevel@tonic-gate 		return (NULL);
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 	/* copy the first node */
111*0Sstevel@tonic-gate 	head_p = expr(spec_dup(list_p->left_p),
112*0Sstevel@tonic-gate 		spec_dup(list_p->right_p));
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	/* append each additional node */
115*0Sstevel@tonic-gate 	expr_p = list_p;
116*0Sstevel@tonic-gate 	while (expr_p = (expr_t *) queue_next(&list_p->qn, &expr_p->qn)) {
117*0Sstevel@tonic-gate 		expr_t		 *new_p;
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 		new_p = expr(spec_dup(expr_p->left_p),
120*0Sstevel@tonic-gate 			spec_dup(expr_p->right_p));
121*0Sstevel@tonic-gate 		(void) queue_append(&head_p->qn, &new_p->qn);
122*0Sstevel@tonic-gate 	}
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 	return (head_p);
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate }				/* end expr_dup */
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate /*
130*0Sstevel@tonic-gate  * expr_destroy() - destroys an expression list
131*0Sstevel@tonic-gate  */
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate void
expr_destroy(expr_t * list_p)134*0Sstevel@tonic-gate expr_destroy(expr_t * list_p)
135*0Sstevel@tonic-gate {
136*0Sstevel@tonic-gate 	expr_t		 *expr_p;
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	while (expr_p = (expr_t *) queue_next(&list_p->qn, &list_p->qn)) {
139*0Sstevel@tonic-gate 		(void) queue_remove(&expr_p->qn);
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 		if (expr_p->left_p)
142*0Sstevel@tonic-gate 			spec_destroy(expr_p->left_p);
143*0Sstevel@tonic-gate 		if (expr_p->right_p)
144*0Sstevel@tonic-gate 			spec_destroy(expr_p->right_p);
145*0Sstevel@tonic-gate 		free(expr_p);
146*0Sstevel@tonic-gate 	}
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	if (list_p->left_p)
149*0Sstevel@tonic-gate 		spec_destroy(list_p->left_p);
150*0Sstevel@tonic-gate 	if (list_p->right_p)
151*0Sstevel@tonic-gate 		spec_destroy(list_p->right_p);
152*0Sstevel@tonic-gate 	free(list_p);
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate }				/* end expr_destroy */
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate /*
158*0Sstevel@tonic-gate  * expr_list() - append a expr_t to a list
159*0Sstevel@tonic-gate  */
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate expr_t		 *
expr_list(expr_t * h,expr_t * f)162*0Sstevel@tonic-gate expr_list(expr_t * h,
163*0Sstevel@tonic-gate 	expr_t * f)
164*0Sstevel@tonic-gate {
165*0Sstevel@tonic-gate 	/* queue append handles the NULL cases OK */
166*0Sstevel@tonic-gate 	return ((expr_t *) queue_append(&h->qn, &f->qn));
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate }				/* end expr_list */
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate /*
172*0Sstevel@tonic-gate  * expr_print() - pretty prints an expr list
173*0Sstevel@tonic-gate  */
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate void
expr_print(FILE * stream,expr_t * list_p)176*0Sstevel@tonic-gate expr_print(FILE * stream,
177*0Sstevel@tonic-gate 	expr_t * list_p)
178*0Sstevel@tonic-gate {
179*0Sstevel@tonic-gate 	expr_t		 *expr_p = NULL;
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	while ((expr_p = (expr_t *) queue_next(&list_p->qn, &expr_p->qn))) {
182*0Sstevel@tonic-gate 		spec_print(stream, expr_p->left_p);
183*0Sstevel@tonic-gate 		(void) fprintf(stream, "=");
184*0Sstevel@tonic-gate 		spec_print(stream, expr_p->right_p);
185*0Sstevel@tonic-gate 		(void) fprintf(stream, " ");
186*0Sstevel@tonic-gate 	}
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate }				/* end expr_print */
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate /*
192*0Sstevel@tonic-gate  * expr_match() - figures out whether a probe matches in an expression list
193*0Sstevel@tonic-gate  */
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate boolean_t
expr_match(expr_t * list_p,const char * attrs)196*0Sstevel@tonic-gate expr_match(expr_t * list_p,
197*0Sstevel@tonic-gate 	const char *attrs)
198*0Sstevel@tonic-gate {
199*0Sstevel@tonic-gate 	expr_t		 *expr_p = NULL;
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	while ((expr_p = (expr_t *) queue_next(&list_p->qn, &expr_p->qn))) {
202*0Sstevel@tonic-gate 		if (matchattrs(expr_p, attrs))
203*0Sstevel@tonic-gate 			return (B_TRUE);
204*0Sstevel@tonic-gate 	}
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 	return (B_FALSE);
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate }				/* end expr_match */
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate /* ---------------------------------------------------------------- */
212*0Sstevel@tonic-gate /* ----------------------- Private Functions ---------------------- */
213*0Sstevel@tonic-gate /* ---------------------------------------------------------------- */
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate typedef struct matchargs {
216*0Sstevel@tonic-gate 	spec_t		 *spec_p;
217*0Sstevel@tonic-gate 	boolean_t	   match;
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate }			   matchargs_t;
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate static		  boolean_t
matchattrs(expr_t * expr_p,const char * attrs)222*0Sstevel@tonic-gate matchattrs(expr_t * expr_p,
223*0Sstevel@tonic-gate 	const char *attrs)
224*0Sstevel@tonic-gate {
225*0Sstevel@tonic-gate 	matchargs_t	 args;
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 	args.spec_p = expr_p->right_p;
228*0Sstevel@tonic-gate 	args.match = B_FALSE;
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate 	spec_attrtrav(expr_p->left_p,
231*0Sstevel@tonic-gate 		(char *) attrs, matchvals, (void *) &args);
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate 	return (args.match);
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate }				/* end matchattrs */
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate /*ARGSUSED*/
239*0Sstevel@tonic-gate static void
matchvals(spec_t * spec_p,char * attrstr,char * valstr,void * calldatap)240*0Sstevel@tonic-gate matchvals(spec_t * spec_p,
241*0Sstevel@tonic-gate 	char *attrstr,
242*0Sstevel@tonic-gate 	char *valstr,
243*0Sstevel@tonic-gate 	void *calldatap)
244*0Sstevel@tonic-gate {
245*0Sstevel@tonic-gate 	matchargs_t	*args_p = (matchargs_t *) calldatap;
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 	spec_valtrav(args_p->spec_p, valstr, matched, calldatap);
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate }				/* matchvals */
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate /*ARGSUSED*/
253*0Sstevel@tonic-gate static void
matched(spec_t * spec_p,char * valstr,void * calldatap)254*0Sstevel@tonic-gate matched(spec_t * spec_p,
255*0Sstevel@tonic-gate 	char *valstr,
256*0Sstevel@tonic-gate 	void *calldatap)
257*0Sstevel@tonic-gate {
258*0Sstevel@tonic-gate 	matchargs_t	*args_p = (matchargs_t *) calldatap;
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 	args_p->match = B_TRUE;
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate }				/* end matched */
263