xref: /illumos-gate/usr/src/cmd/tnf/prex/set.c (revision 7c478bd9)
1 /*
2  * CDDL HEADER START
3  *
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.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1994, by Sun Microsytems, Inc.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Includes
30  */
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include "queue.h"
36 #include "set.h"
37 #include "new.h"
38 
39 
40 /*
41  * Globals
42  */
43 
44 static queue_node_t g_setlist = {
45 	&g_setlist,
46 &g_setlist};
47 
48 
49 /*
50  * Forward Declarations
51  */
52 
53 static void			set_destroy(set_t * set_p);
54 static void			set_print(FILE * stream, set_t * set_p);
55 
56 
57 /*
58  * set() - creates a set
59  */
60 
61 set_t		  *
set(char * setname_p,expr_t * exprlist_p)62 set(char *setname_p, expr_t * exprlist_p)
63 {
64 	set_t		  *new_p;
65 	set_t		  *old_p;
66 
67 	/* does this setname exist already? */
68 	old_p = set_find(setname_p);
69 	if (old_p)
70 		set_destroy(old_p);
71 
72 	/* create a new set */
73 	new_p = new(set_t);
74 	queue_init(&new_p->qn);
75 	new_p->setname_p = setname_p;
76 	new_p->exprlist_p = exprlist_p;
77 
78 	/* append the new set to the global list */
79 	(void) queue_append(&g_setlist, &new_p->qn);
80 
81 	return (new_p);
82 
83 }				/* end set */
84 
85 
86 /*
87  * set_destroy() - destroys a set and related resources
88  */
89 
90 static void
set_destroy(set_t * set_p)91 set_destroy(set_t * set_p)
92 {
93 	if (!set_p)
94 		return;
95 
96 	/* remove ourselves from any list */
97 	if (!queue_isempty(&set_p->qn))
98 		(void) queue_remove(&set_p->qn);
99 
100 	if (set_p->setname_p)
101 		free(set_p->setname_p);
102 
103 	/* destroy the exprlist */
104 	expr_destroy(set_p->exprlist_p);
105 
106 	free(set_p);
107 
108 }				/* end set_destroy */
109 
110 
111 /*
112  * set_list() - pretty prints the global setlist
113  */
114 
115 void
set_list(void)116 set_list(void)
117 {
118 	set_t		  *set_p;
119 
120 	set_p = (set_t *) & g_setlist;
121 	while ((set_p = (set_t *) queue_next(&g_setlist, &set_p->qn))) {
122 		(void) printf("$%-8s ", set_p->setname_p);
123 		set_print(stdout, set_p);
124 		(void) printf("\n");
125 	}
126 
127 }				/* end set_list */
128 
129 
130 /*
131  * set_print() - pretty prints a set
132  */
133 
134 static void
set_print(FILE * stream,set_t * set_p)135 set_print(FILE * stream, set_t * set_p)
136 {
137 	if (!set_p)
138 		return;
139 
140 	expr_print(stream, set_p->exprlist_p);
141 
142 }				/* end set_print */
143 
144 
145 #ifdef OLD
146 /*
147  * set_match() - discerns whether a probe is in a set
148  */
149 
150 boolean_t
set_match(set_t * set_p,const char * name,const char * keys)151 set_match(set_t * set_p, const char *name, const char *keys)
152 {
153 	if (!set_p)
154 		return (B_FALSE);
155 
156 	return (expr_match(set_p->exprlist_p, name, keys));
157 
158 }				/* end set_match */
159 #endif
160 
161 
162 /*
163  * set_find() - finds a set by name
164  */
165 
166 set_t		  *
set_find(char * setname_p)167 set_find(char *setname_p)
168 {
169 	set_t		  *set_p;
170 
171 	if (!setname_p)
172 		return (NULL);
173 
174 	set_p = (set_t *) & g_setlist;
175 	while ((set_p = (set_t *) queue_next(&g_setlist, &set_p->qn)))
176 		if (strcmp(setname_p, set_p->setname_p) == 0)
177 			return (set_p);
178 
179 	return (NULL);
180 
181 }				/* end set_find */
182