8354 sync regcomp(3C) with upstream (fix make catalog)
[unleashed/tickless.git] / usr / src / cmd / tnf / prex / expr.c
blob46c14f621fcfe2c9ce75aeb0888cd85a8fb6654d
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright (c) 1994, by Sun Microsytems, Inc.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * Includes
32 /* we need to define this to get strtok_r from string.h */
33 /* SEEMS LIKE A BUG TO ME */
34 #define _REENTRANT
36 #ifndef DEBUG
37 #define NDEBUG 1
38 #endif
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <libgen.h>
44 #include <assert.h>
45 #include "spec.h"
46 #include "expr.h"
47 #include "new.h"
51 * Typedefs
54 typedef enum {
55 MATCH_NONE = 0,
56 MATCH_FALSE,
57 MATCH_TRUE
60 } match_t;
64 * Declarations
67 static boolean_t matchattrs(expr_t * expr_p, const char *attrs);
68 static void matchvals(spec_t * spec_p, char *attrstr,
69 char *valstr, void *calldatap);
70 static void matched(spec_t * spec_p, char *valstr, void *calldatap);
73 /* ---------------------------------------------------------------- */
74 /* ----------------------- Public Functions ----------------------- */
75 /* ---------------------------------------------------------------- */
78 * expr() - builds an expr
81 expr_t *
82 expr(spec_t * left_p,
83 spec_t * right_p)
85 expr_t *new_p;
87 new_p = new(expr_t);
88 queue_init(&new_p->qn);
89 new_p->left_p = left_p;
90 new_p->right_p = right_p;
92 return (new_p);
94 } /* end expr */
98 * expr_dup() - duplicates an expression list
101 expr_t *
102 expr_dup(expr_t * list_p)
104 expr_t *expr_p;
105 expr_t *head_p;
107 if (!list_p)
108 return (NULL);
110 /* copy the first node */
111 head_p = expr(spec_dup(list_p->left_p),
112 spec_dup(list_p->right_p));
114 /* append each additional node */
115 expr_p = list_p;
116 while (expr_p = (expr_t *) queue_next(&list_p->qn, &expr_p->qn)) {
117 expr_t *new_p;
119 new_p = expr(spec_dup(expr_p->left_p),
120 spec_dup(expr_p->right_p));
121 (void) queue_append(&head_p->qn, &new_p->qn);
124 return (head_p);
126 } /* end expr_dup */
130 * expr_destroy() - destroys an expression list
133 void
134 expr_destroy(expr_t * list_p)
136 expr_t *expr_p;
138 while (expr_p = (expr_t *) queue_next(&list_p->qn, &list_p->qn)) {
139 (void) queue_remove(&expr_p->qn);
141 if (expr_p->left_p)
142 spec_destroy(expr_p->left_p);
143 if (expr_p->right_p)
144 spec_destroy(expr_p->right_p);
145 free(expr_p);
148 if (list_p->left_p)
149 spec_destroy(list_p->left_p);
150 if (list_p->right_p)
151 spec_destroy(list_p->right_p);
152 free(list_p);
154 } /* end expr_destroy */
158 * expr_list() - append a expr_t to a list
161 expr_t *
162 expr_list(expr_t * h,
163 expr_t * f)
165 /* queue append handles the NULL cases OK */
166 return ((expr_t *) queue_append(&h->qn, &f->qn));
168 } /* end expr_list */
172 * expr_print() - pretty prints an expr list
175 void
176 expr_print(FILE * stream,
177 expr_t * list_p)
179 expr_t *expr_p = NULL;
181 while ((expr_p = (expr_t *) queue_next(&list_p->qn, &expr_p->qn))) {
182 spec_print(stream, expr_p->left_p);
183 (void) fprintf(stream, "=");
184 spec_print(stream, expr_p->right_p);
185 (void) fprintf(stream, " ");
188 } /* end expr_print */
192 * expr_match() - figures out whether a probe matches in an expression list
195 boolean_t
196 expr_match(expr_t * list_p,
197 const char *attrs)
199 expr_t *expr_p = NULL;
201 while ((expr_p = (expr_t *) queue_next(&list_p->qn, &expr_p->qn))) {
202 if (matchattrs(expr_p, attrs))
203 return (B_TRUE);
206 return (B_FALSE);
208 } /* end expr_match */
211 /* ---------------------------------------------------------------- */
212 /* ----------------------- Private Functions ---------------------- */
213 /* ---------------------------------------------------------------- */
215 typedef struct matchargs {
216 spec_t *spec_p;
217 boolean_t match;
219 } matchargs_t;
221 static boolean_t
222 matchattrs(expr_t * expr_p,
223 const char *attrs)
225 matchargs_t args;
227 args.spec_p = expr_p->right_p;
228 args.match = B_FALSE;
230 spec_attrtrav(expr_p->left_p,
231 (char *) attrs, matchvals, (void *) &args);
233 return (args.match);
235 } /* end matchattrs */
238 /*ARGSUSED*/
239 static void
240 matchvals(spec_t * spec_p,
241 char *attrstr,
242 char *valstr,
243 void *calldatap)
245 matchargs_t *args_p = (matchargs_t *) calldatap;
247 spec_valtrav(args_p->spec_p, valstr, matched, calldatap);
249 } /* matchvals */
252 /*ARGSUSED*/
253 static void
254 matched(spec_t * spec_p,
255 char *valstr,
256 void *calldatap)
258 matchargs_t *args_p = (matchargs_t *) calldatap;
260 args_p->match = B_TRUE;
262 } /* end matched */