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