1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright (c) 1994, by Sun Microsytems, Inc.
24*7c478bd9Sstevel@tonic-gate */
25*7c478bd9Sstevel@tonic-gate
26*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
27*7c478bd9Sstevel@tonic-gate
28*7c478bd9Sstevel@tonic-gate /*
29*7c478bd9Sstevel@tonic-gate * Includes
30*7c478bd9Sstevel@tonic-gate */
31*7c478bd9Sstevel@tonic-gate
32*7c478bd9Sstevel@tonic-gate #ifndef DEBUG
33*7c478bd9Sstevel@tonic-gate #define NDEBUG 1
34*7c478bd9Sstevel@tonic-gate #endif
35*7c478bd9Sstevel@tonic-gate
36*7c478bd9Sstevel@tonic-gate #include <stdio.h>
37*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
38*7c478bd9Sstevel@tonic-gate #include <assert.h>
39*7c478bd9Sstevel@tonic-gate #include <libintl.h>
40*7c478bd9Sstevel@tonic-gate #include "cmd.h"
41*7c478bd9Sstevel@tonic-gate #include "set.h"
42*7c478bd9Sstevel@tonic-gate #include "fcn.h"
43*7c478bd9Sstevel@tonic-gate #include "new.h"
44*7c478bd9Sstevel@tonic-gate #include "source.h"
45*7c478bd9Sstevel@tonic-gate
46*7c478bd9Sstevel@tonic-gate
47*7c478bd9Sstevel@tonic-gate /*
48*7c478bd9Sstevel@tonic-gate * Globals
49*7c478bd9Sstevel@tonic-gate */
50*7c478bd9Sstevel@tonic-gate
51*7c478bd9Sstevel@tonic-gate static queue_node_t g_cmdlist = {
52*7c478bd9Sstevel@tonic-gate &g_cmdlist,
53*7c478bd9Sstevel@tonic-gate &g_cmdlist};
54*7c478bd9Sstevel@tonic-gate
55*7c478bd9Sstevel@tonic-gate
56*7c478bd9Sstevel@tonic-gate /*
57*7c478bd9Sstevel@tonic-gate * cmd_set() - creates a cmd using a named set and adds it to the global list
58*7c478bd9Sstevel@tonic-gate */
59*7c478bd9Sstevel@tonic-gate
60*7c478bd9Sstevel@tonic-gate cmd_t *
cmd_set(char * setname_p,cmd_kind_t kind,char * fcnname_p)61*7c478bd9Sstevel@tonic-gate cmd_set(char *setname_p, cmd_kind_t kind, char *fcnname_p)
62*7c478bd9Sstevel@tonic-gate {
63*7c478bd9Sstevel@tonic-gate cmd_t *new_p;
64*7c478bd9Sstevel@tonic-gate set_t *set_p;
65*7c478bd9Sstevel@tonic-gate
66*7c478bd9Sstevel@tonic-gate set_p = set_find(setname_p);
67*7c478bd9Sstevel@tonic-gate if (!set_p) {
68*7c478bd9Sstevel@tonic-gate semantic_err(gettext("no set named \"$%s\""), setname_p);
69*7c478bd9Sstevel@tonic-gate return (NULL);
70*7c478bd9Sstevel@tonic-gate }
71*7c478bd9Sstevel@tonic-gate if (kind == CMD_CONNECT && !fcn_find(fcnname_p)) {
72*7c478bd9Sstevel@tonic-gate semantic_err(gettext("no function named \"&%s\""), fcnname_p);
73*7c478bd9Sstevel@tonic-gate return (NULL);
74*7c478bd9Sstevel@tonic-gate }
75*7c478bd9Sstevel@tonic-gate new_p = new(cmd_t);
76*7c478bd9Sstevel@tonic-gate queue_init(&new_p->qn);
77*7c478bd9Sstevel@tonic-gate #ifdef LATEBINDSETS
78*7c478bd9Sstevel@tonic-gate new_p->isnamed = B_TRUE;
79*7c478bd9Sstevel@tonic-gate new_p->expr.setname_p = setname_p;
80*7c478bd9Sstevel@tonic-gate #else
81*7c478bd9Sstevel@tonic-gate new_p->isnamed = B_FALSE;
82*7c478bd9Sstevel@tonic-gate new_p->expr.expr_p = expr_dup(set_p->exprlist_p);
83*7c478bd9Sstevel@tonic-gate #endif
84*7c478bd9Sstevel@tonic-gate new_p->isnew = B_TRUE;
85*7c478bd9Sstevel@tonic-gate new_p->kind = kind;
86*7c478bd9Sstevel@tonic-gate new_p->fcnname_p = fcnname_p;
87*7c478bd9Sstevel@tonic-gate
88*7c478bd9Sstevel@tonic-gate (void) queue_append(&g_cmdlist, &new_p->qn);
89*7c478bd9Sstevel@tonic-gate return (new_p);
90*7c478bd9Sstevel@tonic-gate
91*7c478bd9Sstevel@tonic-gate } /* end cmd_set */
92*7c478bd9Sstevel@tonic-gate
93*7c478bd9Sstevel@tonic-gate
94*7c478bd9Sstevel@tonic-gate /*
95*7c478bd9Sstevel@tonic-gate * cmd_expr() - creates a cmd using a set and adds it to the global list
96*7c478bd9Sstevel@tonic-gate */
97*7c478bd9Sstevel@tonic-gate
98*7c478bd9Sstevel@tonic-gate cmd_t *
cmd_expr(expr_t * expr_p,cmd_kind_t kind,char * fcnname_p)99*7c478bd9Sstevel@tonic-gate cmd_expr(expr_t * expr_p, cmd_kind_t kind, char *fcnname_p)
100*7c478bd9Sstevel@tonic-gate {
101*7c478bd9Sstevel@tonic-gate cmd_t *new_p;
102*7c478bd9Sstevel@tonic-gate
103*7c478bd9Sstevel@tonic-gate if (kind == CMD_CONNECT && !fcn_find(fcnname_p)) {
104*7c478bd9Sstevel@tonic-gate semantic_err(gettext("no function named \"&%s\""), fcnname_p);
105*7c478bd9Sstevel@tonic-gate return (NULL);
106*7c478bd9Sstevel@tonic-gate }
107*7c478bd9Sstevel@tonic-gate new_p = new(cmd_t);
108*7c478bd9Sstevel@tonic-gate queue_init(&new_p->qn);
109*7c478bd9Sstevel@tonic-gate new_p->isnamed = B_FALSE;
110*7c478bd9Sstevel@tonic-gate new_p->expr.expr_p = expr_p;
111*7c478bd9Sstevel@tonic-gate new_p->isnew = B_TRUE;
112*7c478bd9Sstevel@tonic-gate new_p->kind = kind;
113*7c478bd9Sstevel@tonic-gate new_p->fcnname_p = fcnname_p;
114*7c478bd9Sstevel@tonic-gate
115*7c478bd9Sstevel@tonic-gate (void) queue_append(&g_cmdlist, &new_p->qn);
116*7c478bd9Sstevel@tonic-gate return (new_p);
117*7c478bd9Sstevel@tonic-gate
118*7c478bd9Sstevel@tonic-gate } /* end cmd */
119*7c478bd9Sstevel@tonic-gate
120*7c478bd9Sstevel@tonic-gate
121*7c478bd9Sstevel@tonic-gate #if 0
122*7c478bd9Sstevel@tonic-gate /*
123*7c478bd9Sstevel@tonic-gate * cmd_destroy()
124*7c478bd9Sstevel@tonic-gate */
125*7c478bd9Sstevel@tonic-gate
126*7c478bd9Sstevel@tonic-gate static void
127*7c478bd9Sstevel@tonic-gate cmd_destroy(cmd_t * cmd_p)
128*7c478bd9Sstevel@tonic-gate {
129*7c478bd9Sstevel@tonic-gate if (!cmd_p)
130*7c478bd9Sstevel@tonic-gate return;
131*7c478bd9Sstevel@tonic-gate
132*7c478bd9Sstevel@tonic-gate if (!queue_isempty(&cmd_p->qn))
133*7c478bd9Sstevel@tonic-gate (void) queue_remove(&cmd_p->qn);
134*7c478bd9Sstevel@tonic-gate
135*7c478bd9Sstevel@tonic-gate if (!cmd_p->isnamed)
136*7c478bd9Sstevel@tonic-gate expr_destroy(cmd_p->expr.expr_p);
137*7c478bd9Sstevel@tonic-gate
138*7c478bd9Sstevel@tonic-gate free(cmd_p);
139*7c478bd9Sstevel@tonic-gate
140*7c478bd9Sstevel@tonic-gate } /* end cmd_destroy */
141*7c478bd9Sstevel@tonic-gate #endif
142*7c478bd9Sstevel@tonic-gate
143*7c478bd9Sstevel@tonic-gate
144*7c478bd9Sstevel@tonic-gate /*
145*7c478bd9Sstevel@tonic-gate * cmd_list() - pretty prints the global cmdlist
146*7c478bd9Sstevel@tonic-gate */
147*7c478bd9Sstevel@tonic-gate
148*7c478bd9Sstevel@tonic-gate void
cmd_list(void)149*7c478bd9Sstevel@tonic-gate cmd_list(void)
150*7c478bd9Sstevel@tonic-gate {
151*7c478bd9Sstevel@tonic-gate cmd_t *cmd_p;
152*7c478bd9Sstevel@tonic-gate int i = 0;
153*7c478bd9Sstevel@tonic-gate char *str_p;
154*7c478bd9Sstevel@tonic-gate
155*7c478bd9Sstevel@tonic-gate cmd_p = (cmd_t *) & g_cmdlist;
156*7c478bd9Sstevel@tonic-gate while ((cmd_p = (cmd_t *) queue_next(&g_cmdlist, &cmd_p->qn))) {
157*7c478bd9Sstevel@tonic-gate switch (cmd_p->kind) {
158*7c478bd9Sstevel@tonic-gate case CMD_ENABLE:
159*7c478bd9Sstevel@tonic-gate str_p = "enable ";
160*7c478bd9Sstevel@tonic-gate break;
161*7c478bd9Sstevel@tonic-gate case CMD_DISABLE:
162*7c478bd9Sstevel@tonic-gate str_p = "disable";
163*7c478bd9Sstevel@tonic-gate break;
164*7c478bd9Sstevel@tonic-gate case CMD_CONNECT:
165*7c478bd9Sstevel@tonic-gate str_p = "connect";
166*7c478bd9Sstevel@tonic-gate break;
167*7c478bd9Sstevel@tonic-gate case CMD_CLEAR:
168*7c478bd9Sstevel@tonic-gate str_p = "clear ";
169*7c478bd9Sstevel@tonic-gate break;
170*7c478bd9Sstevel@tonic-gate case CMD_TRACE:
171*7c478bd9Sstevel@tonic-gate str_p = "trace ";
172*7c478bd9Sstevel@tonic-gate break;
173*7c478bd9Sstevel@tonic-gate case CMD_UNTRACE:
174*7c478bd9Sstevel@tonic-gate str_p = "untrace";
175*7c478bd9Sstevel@tonic-gate break;
176*7c478bd9Sstevel@tonic-gate default:
177*7c478bd9Sstevel@tonic-gate str_p = "???????";
178*7c478bd9Sstevel@tonic-gate break;
179*7c478bd9Sstevel@tonic-gate }
180*7c478bd9Sstevel@tonic-gate (void) printf("[%d] %s ", i++, str_p);
181*7c478bd9Sstevel@tonic-gate
182*7c478bd9Sstevel@tonic-gate if (cmd_p->kind == CMD_CONNECT) {
183*7c478bd9Sstevel@tonic-gate (void) printf("&%s ", cmd_p->fcnname_p);
184*7c478bd9Sstevel@tonic-gate }
185*7c478bd9Sstevel@tonic-gate if (!cmd_p->isnamed) {
186*7c478bd9Sstevel@tonic-gate expr_print(stdout, cmd_p->expr.expr_p);
187*7c478bd9Sstevel@tonic-gate }
188*7c478bd9Sstevel@tonic-gate
189*7c478bd9Sstevel@tonic-gate (void) printf("\n");
190*7c478bd9Sstevel@tonic-gate }
191*7c478bd9Sstevel@tonic-gate
192*7c478bd9Sstevel@tonic-gate } /* end cmd_list */
193*7c478bd9Sstevel@tonic-gate
194*7c478bd9Sstevel@tonic-gate
195*7c478bd9Sstevel@tonic-gate /*
196*7c478bd9Sstevel@tonic-gate * cmd_traverse() - calls the suppied traversal function on each command.
197*7c478bd9Sstevel@tonic-gate */
198*7c478bd9Sstevel@tonic-gate
199*7c478bd9Sstevel@tonic-gate tnfctl_errcode_t
cmd_traverse(cmd_traverse_func_t percmdfunc,void * calldata_p)200*7c478bd9Sstevel@tonic-gate cmd_traverse(cmd_traverse_func_t percmdfunc, void *calldata_p)
201*7c478bd9Sstevel@tonic-gate {
202*7c478bd9Sstevel@tonic-gate cmd_t *cmd_p;
203*7c478bd9Sstevel@tonic-gate tnfctl_errcode_t err = TNFCTL_ERR_NONE;
204*7c478bd9Sstevel@tonic-gate
205*7c478bd9Sstevel@tonic-gate cmd_p = (cmd_t *) & g_cmdlist;
206*7c478bd9Sstevel@tonic-gate while ((cmd_p = (cmd_t *) queue_next(&g_cmdlist, &cmd_p->qn))) {
207*7c478bd9Sstevel@tonic-gate expr_t *expr_p;
208*7c478bd9Sstevel@tonic-gate fcn_t *fcn_p;
209*7c478bd9Sstevel@tonic-gate
210*7c478bd9Sstevel@tonic-gate if (!cmd_p->isnamed) {
211*7c478bd9Sstevel@tonic-gate expr_p = cmd_p->expr.expr_p;
212*7c478bd9Sstevel@tonic-gate }
213*7c478bd9Sstevel@tonic-gate
214*7c478bd9Sstevel@tonic-gate if (cmd_p->kind == CMD_CONNECT) {
215*7c478bd9Sstevel@tonic-gate fcn_p = fcn_find(cmd_p->fcnname_p);
216*7c478bd9Sstevel@tonic-gate assert(fcn_p);
217*7c478bd9Sstevel@tonic-gate }
218*7c478bd9Sstevel@tonic-gate else
219*7c478bd9Sstevel@tonic-gate fcn_p = NULL;
220*7c478bd9Sstevel@tonic-gate
221*7c478bd9Sstevel@tonic-gate err = (*percmdfunc) (expr_p,
222*7c478bd9Sstevel@tonic-gate cmd_p->kind,
223*7c478bd9Sstevel@tonic-gate fcn_p, cmd_p->isnew, calldata_p);
224*7c478bd9Sstevel@tonic-gate if (err)
225*7c478bd9Sstevel@tonic-gate return (err);
226*7c478bd9Sstevel@tonic-gate }
227*7c478bd9Sstevel@tonic-gate return (err);
228*7c478bd9Sstevel@tonic-gate } /* end cmd_traverse */
229*7c478bd9Sstevel@tonic-gate
230*7c478bd9Sstevel@tonic-gate
231*7c478bd9Sstevel@tonic-gate /*
232*7c478bd9Sstevel@tonic-gate * cmd_traverse() - calls the suppied traversal function on each command.
233*7c478bd9Sstevel@tonic-gate */
234*7c478bd9Sstevel@tonic-gate
235*7c478bd9Sstevel@tonic-gate tnfctl_errcode_t
cmd_callback(cmd_t * cmd_p,cmd_traverse_func_t percmdfunc,void * calldata_p)236*7c478bd9Sstevel@tonic-gate cmd_callback(cmd_t *cmd_p, cmd_traverse_func_t percmdfunc, void *calldata_p)
237*7c478bd9Sstevel@tonic-gate {
238*7c478bd9Sstevel@tonic-gate tnfctl_errcode_t err = TNFCTL_ERR_NONE;
239*7c478bd9Sstevel@tonic-gate expr_t *expr_p;
240*7c478bd9Sstevel@tonic-gate fcn_t *fcn_p;
241*7c478bd9Sstevel@tonic-gate
242*7c478bd9Sstevel@tonic-gate if (!cmd_p->isnamed) {
243*7c478bd9Sstevel@tonic-gate expr_p = cmd_p->expr.expr_p;
244*7c478bd9Sstevel@tonic-gate }
245*7c478bd9Sstevel@tonic-gate
246*7c478bd9Sstevel@tonic-gate if (cmd_p->kind == CMD_CONNECT) {
247*7c478bd9Sstevel@tonic-gate fcn_p = fcn_find(cmd_p->fcnname_p);
248*7c478bd9Sstevel@tonic-gate assert(fcn_p);
249*7c478bd9Sstevel@tonic-gate }
250*7c478bd9Sstevel@tonic-gate else
251*7c478bd9Sstevel@tonic-gate fcn_p = NULL;
252*7c478bd9Sstevel@tonic-gate
253*7c478bd9Sstevel@tonic-gate err = (*percmdfunc) (expr_p, cmd_p->kind, fcn_p, cmd_p->isnew,
254*7c478bd9Sstevel@tonic-gate calldata_p);
255*7c478bd9Sstevel@tonic-gate
256*7c478bd9Sstevel@tonic-gate return (err);
257*7c478bd9Sstevel@tonic-gate }
258*7c478bd9Sstevel@tonic-gate
259*7c478bd9Sstevel@tonic-gate #ifdef NOTNEEDED
260*7c478bd9Sstevel@tonic-gate /*
261*7c478bd9Sstevel@tonic-gate * cmd_mark() - mark all of the commands in the global list as old
262*7c478bd9Sstevel@tonic-gate */
263*7c478bd9Sstevel@tonic-gate
264*7c478bd9Sstevel@tonic-gate void
cmd_mark(void)265*7c478bd9Sstevel@tonic-gate cmd_mark(void)
266*7c478bd9Sstevel@tonic-gate {
267*7c478bd9Sstevel@tonic-gate cmd_t *cmd_p;
268*7c478bd9Sstevel@tonic-gate
269*7c478bd9Sstevel@tonic-gate cmd_p = (cmd_t *) & g_cmdlist;
270*7c478bd9Sstevel@tonic-gate while ((cmd_p = (cmd_t *) queue_next(&g_cmdlist, &cmd_p->qn))) {
271*7c478bd9Sstevel@tonic-gate cmd_p->isnew = B_FALSE;
272*7c478bd9Sstevel@tonic-gate }
273*7c478bd9Sstevel@tonic-gate
274*7c478bd9Sstevel@tonic-gate } /* end cmd_mark */
275*7c478bd9Sstevel@tonic-gate
276*7c478bd9Sstevel@tonic-gate /*
277*7c478bd9Sstevel@tonic-gate * cmd_delete() -
278*7c478bd9Sstevel@tonic-gate */
279*7c478bd9Sstevel@tonic-gate
280*7c478bd9Sstevel@tonic-gate void
cmd_delete(int cmdnum)281*7c478bd9Sstevel@tonic-gate cmd_delete(int cmdnum)
282*7c478bd9Sstevel@tonic-gate {
283*7c478bd9Sstevel@tonic-gate cmd_t *cmd_p;
284*7c478bd9Sstevel@tonic-gate int i = 0;
285*7c478bd9Sstevel@tonic-gate
286*7c478bd9Sstevel@tonic-gate cmd_p = (cmd_t *) & g_cmdlist;
287*7c478bd9Sstevel@tonic-gate while ((cmd_p = (cmd_t *) queue_next(&g_cmdlist, &cmd_p->qn))) {
288*7c478bd9Sstevel@tonic-gate if (cmdnum == i) {
289*7c478bd9Sstevel@tonic-gate cmd_destroy(cmd_p);
290*7c478bd9Sstevel@tonic-gate return;
291*7c478bd9Sstevel@tonic-gate }
292*7c478bd9Sstevel@tonic-gate i++;
293*7c478bd9Sstevel@tonic-gate }
294*7c478bd9Sstevel@tonic-gate
295*7c478bd9Sstevel@tonic-gate } /* end cmd_delete */
296*7c478bd9Sstevel@tonic-gate #endif
297