xref: /illumos-gate/usr/src/cmd/tnf/prex/cmd.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 #ifndef DEBUG
33 #define	NDEBUG	1
34 #endif
35 
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <assert.h>
39 #include <libintl.h>
40 #include "cmd.h"
41 #include "set.h"
42 #include "fcn.h"
43 #include "new.h"
44 #include "source.h"
45 
46 
47 /*
48  * Globals
49  */
50 
51 static queue_node_t g_cmdlist = {
52 	&g_cmdlist,
53 &g_cmdlist};
54 
55 
56 /*
57  * cmd_set() - creates a cmd using a named set and adds it to the global list
58  */
59 
60 cmd_t *
cmd_set(char * setname_p,cmd_kind_t kind,char * fcnname_p)61 cmd_set(char *setname_p, cmd_kind_t kind, char *fcnname_p)
62 {
63 	cmd_t		  *new_p;
64 	set_t		  *set_p;
65 
66 	set_p = set_find(setname_p);
67 	if (!set_p) {
68 		semantic_err(gettext("no set named \"$%s\""), setname_p);
69 		return (NULL);
70 	}
71 	if (kind == CMD_CONNECT && !fcn_find(fcnname_p)) {
72 		semantic_err(gettext("no function named \"&%s\""), fcnname_p);
73 		return (NULL);
74 	}
75 	new_p = new(cmd_t);
76 	queue_init(&new_p->qn);
77 #ifdef LATEBINDSETS
78 	new_p->isnamed = B_TRUE;
79 	new_p->expr.setname_p = setname_p;
80 #else
81 	new_p->isnamed = B_FALSE;
82 	new_p->expr.expr_p = expr_dup(set_p->exprlist_p);
83 #endif
84 	new_p->isnew = B_TRUE;
85 	new_p->kind = kind;
86 	new_p->fcnname_p = fcnname_p;
87 
88 	(void) queue_append(&g_cmdlist, &new_p->qn);
89 	return (new_p);
90 
91 }				/* end cmd_set */
92 
93 
94 /*
95  * cmd_expr() - creates a cmd using a set and adds it to the global list
96  */
97 
98 cmd_t *
cmd_expr(expr_t * expr_p,cmd_kind_t kind,char * fcnname_p)99 cmd_expr(expr_t * expr_p, cmd_kind_t kind, char *fcnname_p)
100 {
101 	cmd_t		  *new_p;
102 
103 	if (kind == CMD_CONNECT && !fcn_find(fcnname_p)) {
104 		semantic_err(gettext("no function named \"&%s\""), fcnname_p);
105 		return (NULL);
106 	}
107 	new_p = new(cmd_t);
108 	queue_init(&new_p->qn);
109 	new_p->isnamed = B_FALSE;
110 	new_p->expr.expr_p = expr_p;
111 	new_p->isnew = B_TRUE;
112 	new_p->kind = kind;
113 	new_p->fcnname_p = fcnname_p;
114 
115 	(void) queue_append(&g_cmdlist, &new_p->qn);
116 	return (new_p);
117 
118 }				/* end cmd */
119 
120 
121 #if 0
122 /*
123  * cmd_destroy()
124  */
125 
126 static void
127 cmd_destroy(cmd_t * cmd_p)
128 {
129 	if (!cmd_p)
130 		return;
131 
132 	if (!queue_isempty(&cmd_p->qn))
133 		(void) queue_remove(&cmd_p->qn);
134 
135 	if (!cmd_p->isnamed)
136 		expr_destroy(cmd_p->expr.expr_p);
137 
138 	free(cmd_p);
139 
140 }				/* end cmd_destroy */
141 #endif
142 
143 
144 /*
145  * cmd_list() - pretty prints the global cmdlist
146  */
147 
148 void
cmd_list(void)149 cmd_list(void)
150 {
151 	cmd_t		  *cmd_p;
152 	int			 i = 0;
153 	char		   *str_p;
154 
155 	cmd_p = (cmd_t *) & g_cmdlist;
156 	while ((cmd_p = (cmd_t *) queue_next(&g_cmdlist, &cmd_p->qn))) {
157 		switch (cmd_p->kind) {
158 		case CMD_ENABLE:
159 			str_p = "enable ";
160 			break;
161 		case CMD_DISABLE:
162 			str_p = "disable";
163 			break;
164 		case CMD_CONNECT:
165 			str_p = "connect";
166 			break;
167 		case CMD_CLEAR:
168 			str_p = "clear  ";
169 			break;
170 		case CMD_TRACE:
171 			str_p = "trace  ";
172 			break;
173 		case CMD_UNTRACE:
174 			str_p = "untrace";
175 			break;
176 		default:
177 			str_p = "???????";
178 			break;
179 		}
180 		(void) printf("[%d] %s ", i++, str_p);
181 
182 		if (cmd_p->kind == CMD_CONNECT) {
183 			(void) printf("&%s ", cmd_p->fcnname_p);
184 		}
185 		if (!cmd_p->isnamed) {
186 			expr_print(stdout, cmd_p->expr.expr_p);
187 		}
188 
189 		(void) printf("\n");
190 	}
191 
192 }				/* end cmd_list */
193 
194 
195 /*
196  * cmd_traverse() - calls the suppied traversal function on each command.
197  */
198 
199 tnfctl_errcode_t
cmd_traverse(cmd_traverse_func_t percmdfunc,void * calldata_p)200 cmd_traverse(cmd_traverse_func_t percmdfunc, void *calldata_p)
201 {
202 	cmd_t			*cmd_p;
203 	tnfctl_errcode_t	err = TNFCTL_ERR_NONE;
204 
205 	cmd_p = (cmd_t *) & g_cmdlist;
206 	while ((cmd_p = (cmd_t *) queue_next(&g_cmdlist, &cmd_p->qn))) {
207 		expr_t		 *expr_p;
208 		fcn_t		  *fcn_p;
209 
210 		if (!cmd_p->isnamed) {
211 			expr_p = cmd_p->expr.expr_p;
212 		}
213 
214 		if (cmd_p->kind == CMD_CONNECT) {
215 			fcn_p = fcn_find(cmd_p->fcnname_p);
216 			assert(fcn_p);
217 		}
218 		else
219 			fcn_p = NULL;
220 
221 		err = (*percmdfunc) (expr_p,
222 			cmd_p->kind,
223 			fcn_p, cmd_p->isnew, calldata_p);
224 		if (err)
225 			return (err);
226 	}
227 	return (err);
228 }				/* end cmd_traverse */
229 
230 
231 /*
232  * cmd_traverse() - calls the suppied traversal function on each command.
233  */
234 
235 tnfctl_errcode_t
cmd_callback(cmd_t * cmd_p,cmd_traverse_func_t percmdfunc,void * calldata_p)236 cmd_callback(cmd_t *cmd_p, cmd_traverse_func_t percmdfunc, void *calldata_p)
237 {
238 	tnfctl_errcode_t	err = TNFCTL_ERR_NONE;
239 	expr_t			*expr_p;
240 	fcn_t			*fcn_p;
241 
242 	if (!cmd_p->isnamed) {
243 		expr_p = cmd_p->expr.expr_p;
244 	}
245 
246 	if (cmd_p->kind == CMD_CONNECT) {
247 		fcn_p = fcn_find(cmd_p->fcnname_p);
248 		assert(fcn_p);
249 	}
250 	else
251 		fcn_p = NULL;
252 
253 	err = (*percmdfunc) (expr_p, cmd_p->kind, fcn_p, cmd_p->isnew,
254 				calldata_p);
255 
256 	return (err);
257 }
258 
259 #ifdef NOTNEEDED
260 /*
261  * cmd_mark() - mark all of the commands in the global list as old
262  */
263 
264 void
cmd_mark(void)265 cmd_mark(void)
266 {
267 	cmd_t		  *cmd_p;
268 
269 	cmd_p = (cmd_t *) & g_cmdlist;
270 	while ((cmd_p = (cmd_t *) queue_next(&g_cmdlist, &cmd_p->qn))) {
271 		cmd_p->isnew = B_FALSE;
272 	}
273 
274 }				/* end cmd_mark */
275 
276 /*
277  * cmd_delete() -
278  */
279 
280 void
cmd_delete(int cmdnum)281 cmd_delete(int cmdnum)
282 {
283 	cmd_t		  *cmd_p;
284 	int			 i = 0;
285 
286 	cmd_p = (cmd_t *) & g_cmdlist;
287 	while ((cmd_p = (cmd_t *) queue_next(&g_cmdlist, &cmd_p->qn))) {
288 		if (cmdnum == i) {
289 			cmd_destroy(cmd_p);
290 			return;
291 		}
292 		i++;
293 	}
294 
295 }				/* end cmd_delete */
296 #endif
297