File: tnmEvent.c

package info (click to toggle)
scotty 2.1.9-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 9,984 kB
  • ctags: 4,313
  • sloc: ansic: 35,946; sh: 12,591; tcl: 8,122; yacc: 2,442; makefile: 898; lex: 370
file content (332 lines) | stat: -rw-r--r-- 8,562 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
 * tnmEvent.c --
 *
 *	The event command allows to do event driven programming inside
 *	of Tcl scripts easily. The basic idea is that you can raise an
 *	event which will invoke all event handlers that match the
 *	events tag. The idea was born as network management scripts
 *	usually contain a part to detect error situations and scripts
 *	to handle errors It is easy to glue things together by binding
 *	(multiple) scripts to handle events.
 *
 * Copyright (c) 1995-1996 Technical University of Braunschweig.
 *
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 */

#include "tnmInt.h"
#include "tnmPort.h"

/*
  event create type <name>		;# creates an event type <name>
  event delete type <name>		;# deletes the event type <name>
  event types				;# returns the list of all event types

  event raise <type> ?arg? ?arg? ..	;# raises an even with arguments
  event bind <type> <script>		;# binds a script to an event type
  event bindings <type>			;# list all bindings for a type

  Questions: 

  o How to identify multiple bindings for the same event?
  o Should bindings have attributes?
  o Allow conditions over event binding attributes to fire an event?
 */

/*
 * Structure used to describe an event.
 */

typedef struct Event {
    Tcl_Interp *interp;		/* The Tcl interpreter to use. */
    char *cmd;			/* The command to evaluate. */
    char *args;			/* The arguments to the command. */
} Event;

/*
 * The following hash table keeps a record for each existing binding.
 */

static Tcl_HashTable tagTable;

/*
 * Every Tcl interpreter has an associated EventControl record. It
 * keeps track of the definitions valid for a single interpreter.
 */

static char tnmEventControl[] = "tnmEventControl";

typedef struct EventControl {
    Tcl_HashTable typeTable;
} EventControl;

/*
 * Forward declarations for procedures defined later in this file:
 */

static void
AssocDeleteProc	_ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp));

static void
EventProc	_ANSI_ARGS_((ClientData clientData));

static int
BindEvent	_ANSI_ARGS_((Tcl_Interp *interp, EventControl *control,
			     int argc, char **argv));

static int
RaiseEvent	_ANSI_ARGS_((Tcl_Interp *interp, EventControl *control,
			     int argc, char **argv));

/*
 *----------------------------------------------------------------------
 *
 * AssocDeleteProc --
 *
 *	This procedure is called when a Tcl interpreter gets destroyed
 *	so that we can clean up the data associated with this interpreter.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

static void
AssocDeleteProc(clientData, interp)
    ClientData clientData;
    Tcl_Interp *interp;
{
    EventControl *control = (EventControl *) clientData;

    if (control) {
	ckfree((char *) control);
    }
}

/*
 *----------------------------------------------------------------------
 *
 * EventProc --
 *
 *	This is the callback that actually handles a raised event.
 *	Issue a background error if the callback fails for some reason.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	The callback is evaluated which can cause side effects.
 *
 *----------------------------------------------------------------------
 */

static void
EventProc(clientData)
    ClientData clientData;
{
    Event *evPtr = (Event *) clientData;
    int code;
    char *cmd = ckalloc(strlen(evPtr->cmd) + strlen(evPtr->args) + 2);
    sprintf(cmd, "%s %s", evPtr->cmd, evPtr->args);

    Tcl_AllowExceptions(evPtr->interp);
    code = Tcl_GlobalEval(evPtr->interp, cmd);
    if (code == TCL_ERROR) {
	Tcl_AddErrorInfo(evPtr->interp, "\n    (event callback)");
	Tcl_BackgroundError(evPtr->interp);
    }

    ckfree(cmd);

    ckfree(evPtr->cmd);
    ckfree(evPtr->args);
    ckfree((char *) evPtr);
}

/*
 *----------------------------------------------------------------------
 *
 * BindEvent --
 *
 *	Create or return the event binding for a particular tag.
 *	Binding to an empty string will remove an existing binding.
 *
 * Results:
 *	A standard Tcl result.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

static int
BindEvent(interp, control, argc, argv)
    Tcl_Interp *interp;
    EventControl *control;
    int argc;
    char **argv;
{
    Tcl_HashEntry *entryPtr;

    if (argc == 2) {
        Tcl_HashSearch search;
        entryPtr = Tcl_FirstHashEntry(&control->typeTable, &search);
	while (entryPtr) {
	    Tcl_AppendElement(interp, Tcl_GetHashKey(&control->typeTable, entryPtr));
	    entryPtr = Tcl_NextHashEntry(&search);
	}
    } else if (argc == 3) {
        entryPtr = Tcl_FindHashEntry(&control->typeTable, argv[2]);
	if (entryPtr) {
	    Tcl_SetResult(interp, (char *) Tcl_GetHashValue(entryPtr), 
			  TCL_STATIC);
	}
    } else if (argc == 4) {
        int isNew, append = argv[3][0] == '+';
	char *newCmd, *oldCmd = NULL;
	if (append) {
	    argv[3]++;
	}
	entryPtr = Tcl_FindHashEntry(&control->typeTable, argv[2]);
	if (entryPtr) {
	    oldCmd = (char *) Tcl_GetHashValue(entryPtr);
	}
	if (argv[3][0] == '\0') {
	    if (entryPtr) {
	        Tcl_DeleteHashEntry(entryPtr);
	    }
	} else {
	    if (append && oldCmd) {
	        newCmd = ckalloc(strlen(oldCmd) + strlen(argv[3]) + 2);
		sprintf(newCmd, "%s\n%s", oldCmd, argv[3]);
	    } else {
	        newCmd = ckstrdup(argv[3]);
		if (! entryPtr) {
		   entryPtr = Tcl_CreateHashEntry(&control->typeTable, argv[2], &isNew);
		}
	    }
	    Tcl_SetHashValue(entryPtr, (ClientData) newCmd);
	}
	if (oldCmd) {
	    ckfree(oldCmd);
	}
    } else {
        Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
			 " bind ?pattern? ?command?\"", (char *) NULL);
	return TCL_ERROR;
    }

    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * RaiseEvent --
 *
 *	Create an event and prepares to triggers all event handlers
 *	that are created for the given tag. Some issues here:
 *	Should we allow a tag list? And should we allow to match the 
 *	tag against those tags in the tagTable?
 *
 * Results:
 *	A standard Tcl result.
 *
 * Side effects:
 *	Events are processed which can have side arbitrary effects.
 *
 *----------------------------------------------------------------------
 */

static int
RaiseEvent(interp, control, argc, argv)
    Tcl_Interp *interp;
    EventControl *control;
    int argc;
    char **argv;
{
    Tcl_HashEntry *entryPtr;

    if (argc < 3) {
        Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
			 " raise tag ?args?\"", (char *) NULL);
	return TCL_ERROR;
    }
    
    entryPtr = Tcl_FindHashEntry(&control->typeTable, argv[2]);
    if (entryPtr) {
        Event *evPtr = (Event *) ckalloc(sizeof(Event));
	evPtr->interp = interp;
	evPtr->cmd = ckstrdup((char *) Tcl_GetHashValue(entryPtr));
	evPtr->args = Tcl_Merge(argc-3, argv+3);
#if 0
	Tcl_CreateTimerHandler(0, EventProc, (ClientData) evPtr);
/*	Tcl_DoWhenIdle(EventProc, (ClientData) evPtr); */
#else
	EventProc((ClientData) evPtr);
	Tcl_ResetResult(interp);
#endif
    }
  
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * Tnm_EventCmd --
 *
 *	This procedure is invoked to process the "event" command.
 *	See the user documentation for details on what it does.
 *
 * Results:
 *	A standard Tcl result.
 *
 * Side effects:
 *	See the user documentation.
 *
 *----------------------------------------------------------------------
 */

int
Tnm_EventCmd(clientData, interp, argc, argv)
    ClientData clientData;
    Tcl_Interp *interp;
    int argc;
    char **argv;
{
    EventControl *control = (EventControl *) 
	Tcl_GetAssocData(interp, tnmEventControl, NULL);

    if (! control) {
	control = (EventControl *) ckalloc(sizeof(EventControl));
        Tcl_InitHashTable(&control->typeTable, TCL_STRING_KEYS);
	Tcl_SetAssocData(interp, tnmEventControl, AssocDeleteProc, 
			 (ClientData) control);
    }

    if (argc < 2) {
	Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
			 " option ?arg arg ...?\"", (char *) NULL);
	return TCL_ERROR;
    }

    if (strcmp(argv[1], "bind") == 0) {
	return BindEvent(interp, control, argc, argv);

    } else if (strcmp(argv[1], "raise") == 0) {
        return RaiseEvent(interp, control, argc, argv);

    }

    Tcl_AppendResult(interp, "bad option \"", argv[1], 
		     "\": should be bind, or raise",
		     (char *) NULL);
    return TCL_ERROR;
}