Debian Codegraph @

generic/bltWatch.c

raw symbols SCIP
1 /*
2 * bltWatch.c --
3 *
4 * This module implements watch procedure callbacks for Tcl
5 * commands and procedures.
6 *
7 * Copyright 1994-1998 Lucent Technologies, Inc.
8 *
9 * Permission to use, copy, modify, and distribute this software and
10 * its documentation for any purpose and without fee is hereby
11 * granted, provided that the above copyright notice appear in all
12 * copies and that both that the copyright notice and warranty
13 * disclaimer appear in supporting documentation, and that the names
14 * of Lucent Technologies any of their entities not be used in
15 * advertising or publicity pertaining to distribution of the software
16 * without specific, written prior permission.
17 *
18 * Lucent Technologies disclaims all warranties with regard to this
19 * software, including all implied warranties of merchantability and
20 * fitness. In no event shall Lucent Technologies be liable for any
21 * special, indirect or consequential damages or any damages
22 * whatsoever resulting from loss of use, data or profits, whether in
23 * an action of contract, negligence or other tortuous action, arising
24 * out of or in connection with the use or performance of this
25 * software.
26 *
27 * The "watch" command was created by George Howlett.
28 */
29
30 #include "bltInt.h"
31 #include <bltHash.h>
32 #include "bltSwitch.h"
33
34 #define UNKNOWN_RETURN_CODE 5
35 static char *codeNames[] =
36 {
37 "OK", "ERROR", "RETURN", "BREAK", "CONTINUE"
38 };
39
40 #define WATCH_MAX_LEVEL 10000 /* Maximum depth of Tcl traces. */
41
42 enum WatchStates {
43 WATCH_STATE_DONT_CARE = -1, /* Select watch regardless of state */
44 WATCH_STATE_IDLE = 0, /* */
45 WATCH_STATE_ACTIVE = 1
46 };
47
48 typedef struct {
49 Tcl_Interp *interp; /* Interpreter associated with the watch */
50 Blt_Uid nameId; /* Watch identifier */
51
52 /* User-configurable fields */
53 enum WatchStates state; /* Current state of watch: either
54 * WATCH_STATE_IDLE or WATCH_STATE_ACTIVE */
55 int maxLevel; /* Maximum depth of tracing allowed */
56 char **preCmd; /* Procedure to be invoked before the
57 * command is executed (but after
58 * substitutions have occurred). */
59 char **postCmd; /* Procedure to be invoked after the command
60 * is executed. */
61 Tcl_Trace trace; /* Trace handler which activates "pre"
62 * command procedures */
63 Tcl_AsyncHandler asyncHandle; /* Async handler which triggers the
64 * "post" command procedure (if one
65 * exists) */
66 int active; /* Indicates if a trace is currently
67 * active. This prevents recursive
68 * tracing of the "pre" and "post"
69 * procedures. */
70 int level; /* Current level of traced command. */
71 char *cmdPtr; /* Command string before substitutions.
72 * Points to a original command buffer. */
73 char *args; /* Tcl list of the command after
74 * substitutions. List is malloc-ed by
75 * Tcl_Merge. Must be freed in handler
76 * procs */
77 } Watch;
78
79 typedef struct {
80 Blt_Uid nameId; /* Name identifier of the watch */
81 Tcl_Interp *interp; /* Interpreter associated with the
82 * watch */
83 } WatchKey;
84
85 static Blt_HashTable watchTable;
86 static int refCount = 0;
87
88 static Blt_SwitchSpec switchSpecs[] =
89 {
90 {BLT_SWITCH_LIST, "-precmd", Blt_Offset(Watch, preCmd), 0},
91 {BLT_SWITCH_LIST, "-postcmd", Blt_Offset(Watch, postCmd), 0},
92 {BLT_SWITCH_BOOLEAN, "-active", Blt_Offset(Watch, state), 0},
93 {BLT_SWITCH_INT_NONNEGATIVE, "-maxlevel", Blt_Offset(Watch, maxLevel), 0},
94 {BLT_SWITCH_END, NULL, 0, 0}
95 };
96
97 static Tcl_CmdTraceProc PreCmdProc;
98 static Tcl_AsyncProc PostCmdProc;
99 static Tcl_CmdProc WatchCmd;
100 static Tcl_CmdDeleteProc WatchDeleteCmd;
101
102 /*
103 *----------------------------------------------------------------------
104 *
105 * PreCmdProc --
106 *
107 * Procedure callback for Tcl_Trace. Gets called before the
108 * command is executed, but after substitutions have occurred.
109 * If a watch procedure is active, it evals a Tcl command.
110 * Activates the "precmd" callback, if one exists.
111 *
112 * Stashes some information for the "pre" callback: command
113 * string, substituted argument list, and current level.
114 *
115 * Format of "pre" proc:
116 *
117 * proc beforeCmd { level cmdStr argList } {
118 *
119 * }
120 *
121 *
122 * Results:
123 * None.
124 *
125 * Side Effects:
126 * A Tcl_AsyncHandler may be triggered, if a "post" procedure is
127 * defined.
128 *
129 *----------------------------------------------------------------------
130 */
131 /*ARGSUSED*/
132 static void
133 PreCmdProc(clientData, interp, level, command, cmdProc, cmdClientData,
134 argc, argv)
135 ClientData clientData; /* Not used. */
136 Tcl_Interp *interp; /* Not used. */
137 int level; /* Current level */
138 char *command; /* Command before substitution */
139 Tcl_CmdProc *cmdProc; /* Not used. */
140 ClientData cmdClientData; /* Not used. */
141 int argc;
142 char **argv; /* Command after parsing, but before
143 * evaluation */
144 {
145 Watch *watchPtr = clientData;
146
147 if (watchPtr->active) {
148 return; /* Don't re-enter from Tcl_Eval below */
149 }
150 watchPtr->cmdPtr = command;
151 watchPtr->level = level;
152 /*
153 * There's no guarantee that the calls to PreCmdProc will match
154 * up with PostCmdProc. So free up argument lists that are still
155 * hanging around before allocating a new one.
156 */
157 if (watchPtr->args != NULL) {
158 Blt_Free(watchPtr->args);
159 }
160 watchPtr->args = Tcl_Merge(argc, argv);
161
162 if (watchPtr->preCmd != NULL) {
163 Tcl_DString buffer;
164 char string[200];
165 int status;
166 register char **p;
167
168 /* Create the "pre" command procedure call */
169 Tcl_DStringInit(&buffer);
170 for (p = watchPtr->preCmd; *p != NULL; p++) {
171 Tcl_DStringAppendElement(&buffer, *p);
172 }
173 sprintf(string, "%d", watchPtr->level);
174 Tcl_DStringAppendElement(&buffer, string);
175 Tcl_DStringAppendElement(&buffer, watchPtr->cmdPtr);
176 Tcl_DStringAppendElement(&buffer, watchPtr->args);
177
178 watchPtr->active = 1;
179 status = Tcl_Eval(interp, Tcl_DStringValue(&buffer));
180 watchPtr->active = 0;
181
182 Tcl_DStringFree(&buffer);
183 if (status != TCL_OK) {
184 fprintf(stderr, "%s failed: %s\n", watchPtr->preCmd[0],
185 Tcl_GetStringResult(interp));
186 }
187 }
188 /* Set the trigger for the "post" command procedure */
189 if (watchPtr->postCmd != NULL) {
190 Tcl_AsyncMark(watchPtr->asyncHandle);
191 }
192 }
193
194 /*
195 *----------------------------------------------------------------------
196 *
197 * PostCmdProc --
198 *
199 * Procedure callback for Tcl_AsyncHandler. Gets called after
200 * the command has executed. It tests for a "post" command, but
201 * you really can't get here, if one doesn't exist.
202 *
203 * Save the current contents of interp->result before calling
204 * the "post" command, and restore it afterwards.
205 *
206 * Format of "post" proc:
207 *
208 * proc afterCmd { level cmdStr argList retCode results } {
209 *
210 * }
211 *
212 * Results:
213 * None.
214 *
215 * Side Effects:
216 * Memory for argument list is released.
217 *
218 *----------------------------------------------------------------------
219 */
220 /*ARGSUSED*/
221 static int
222 PostCmdProc(clientData, interp, code)
223 ClientData clientData; /* Not used. */
224 Tcl_Interp *interp; /* Not used. */
225 int code; /* Completion code of command */
226 {
227 Watch *watchPtr = clientData;
228
229 if (watchPtr->active) {
230 return code;
231 }
232 if (watchPtr->postCmd != NULL) {
233 int status;
234 Tcl_DString buffer;
235 char string[200];
236 char *results;
237 register char **p;
238 char *retCode;
239 char *errorCode, *errorInfo;
240 errorInfo = errorCode = NULL;
241
242 results = "NO INTERPRETER AVAILABLE";
243
244 /*
245 * ----------------------------------------------------
246 *
247 * Save the state of the interpreter.
248 *
249 * ----------------------------------------------------
250 */
251 if (interp != NULL) {
252 errorInfo = (char *)Tcl_GetVar2(interp, "errorInfo", (char *)NULL,
253 TCL_GLOBAL_ONLY);
254 if (errorInfo != NULL) {
255 errorInfo = Blt_Strdup(errorInfo);
256 }
257 errorCode = (char *)Tcl_GetVar2(interp, "errorCode", (char *)NULL,
258 TCL_GLOBAL_ONLY);
259 if (errorCode != NULL) {
260 errorCode = Blt_Strdup(errorCode);
261 }
262 results = Blt_Strdup(Tcl_GetStringResult(interp));
263 }
264 /* Create the "post" command procedure call */
265 Tcl_DStringInit(&buffer);
266 for (p = watchPtr->postCmd; *p != NULL; p++) {
267 Tcl_DStringAppendElement(&buffer, *p);
268 }
269 sprintf(string, "%d", watchPtr->level);
270 Tcl_DStringAppendElement(&buffer, string);
271 Tcl_DStringAppendElement(&buffer, watchPtr->cmdPtr);
272 Tcl_DStringAppendElement(&buffer, watchPtr->args);
273 if (code < UNKNOWN_RETURN_CODE) {
274 retCode = codeNames[code];
275 } else {
276 sprintf(string, "%d", code);
277 retCode = string;
278 }
279 Tcl_DStringAppendElement(&buffer, retCode);
280 Tcl_DStringAppendElement(&buffer, results);
281
282 watchPtr->active = 1;
283 status = Tcl_Eval(watchPtr->interp, Tcl_DStringValue(&buffer));
284 watchPtr->active = 0;
285
286 Tcl_DStringFree(&buffer);
287 Blt_Free(watchPtr->args);
288 watchPtr->args = NULL;
289
290 if (status != TCL_OK) {
291 fprintf(stderr, "%s failed: %s\n", watchPtr->postCmd[0],
292 Tcl_GetStringResult(watchPtr->interp));
293 }
294 /*
295 * ----------------------------------------------------
296 *
297 * Restore the state of the interpreter.
298 *
299 * ----------------------------------------------------
300 */
301 if (interp != NULL) {
302 if (errorInfo != NULL) {
303 Tcl_SetVar2(interp, "errorInfo", (char *)NULL, errorInfo,
304 TCL_GLOBAL_ONLY);
305 Blt_Free(errorInfo);
306 }
307 if (errorCode != NULL) {
308 Tcl_SetVar2(interp, "errorCode", (char *)NULL, errorCode,
309 TCL_GLOBAL_ONLY);
310 Blt_Free(errorCode);
311 }
312 Tcl_SetResult(interp, results, TCL_DYNAMIC);
313 }
314 }
315 return code;
316 }
317
318 /*
319 *----------------------------------------------------------------------
320 *
321 * NewWatch --
322 *
323 * Creates a new watch. Uses the nameId and interpreter
324 * address to create a unique hash key. The new watch is
325 * registered into the "watchTable" hash table. Also creates a
326 * Tcl_AsyncHandler for triggering "post" events.
327 *
328 * Results:
329 * If memory for the watch could be allocated, a pointer to
330 * the new watch is returned. Otherwise NULL, and interp->result
331 * points to an error message.
332 *
333 * Side Effects:
334 * A new Tcl_AsyncHandler is created. A new hash table entry
335 * is created. Memory the watch structure is allocated.
336 *
337 *----------------------------------------------------------------------
338 */
339 static Watch *
340 NewWatch(interp, name)
341 Tcl_Interp *interp;
342 char *name;
343 {
344 Watch *watchPtr;
345 WatchKey key;
346 Blt_HashEntry *hPtr;
347 int dummy;
348
349 watchPtr = Blt_Calloc(1, sizeof(Watch));
350 if (watchPtr == NULL) {
351 Tcl_AppendResult(interp, "can't allocate watch structure", (char *)NULL);
352 return NULL;
353 }
354 watchPtr->state = WATCH_STATE_ACTIVE;
355 watchPtr->maxLevel = WATCH_MAX_LEVEL;
356 watchPtr->nameId = Blt_GetUid(name);
357 watchPtr->interp = interp;
358 watchPtr->asyncHandle = Tcl_AsyncCreate(PostCmdProc, watchPtr);
359 key.interp = interp;
360 key.nameId = watchPtr->nameId;
361 hPtr = Blt_CreateHashEntry(&watchTable, (char *)&key, &dummy);
362 Blt_SetHashValue(hPtr, watchPtr);
363 return watchPtr;
364 }
365
366 /*
367 *----------------------------------------------------------------------
368 *
369 * DestroyWatch --
370 *
371 * Removes the watch. The resources used by the watch
372 * are released.
373 * 1) If the watch is active, its trace is deleted.
374 * 2) Memory for command strings is free-ed.
375 * 3) Entry is removed from watch registry.
376 * 4) Async handler is deleted.
377 * 5) Memory for watch itself is released.
378 *
379 * Results:
380 * None.
381 *
382 * Side Effects:
383 * Everything associated with the watch is freed.
384 *
385 *----------------------------------------------------------------------
386 */
387 static void
388 DestroyWatch(watchPtr)
389 Watch *watchPtr;
390 {
391 WatchKey key;
392 Blt_HashEntry *hPtr;
393
394 Tcl_AsyncDelete(watchPtr->asyncHandle);
395 if (watchPtr->state == WATCH_STATE_ACTIVE) {
396 Tcl_DeleteTrace(watchPtr->interp, watchPtr->trace);
397 }
398 if (watchPtr->preCmd != NULL) {
399 Blt_Free(watchPtr->preCmd);
400 }
401 if (watchPtr->postCmd != NULL) {
402 Blt_Free(watchPtr->postCmd);
403 }
404 if (watchPtr->args != NULL) {
405 Blt_Free(watchPtr->args);
406 }
407 key.interp = watchPtr->interp;
408 key.nameId = watchPtr->nameId;
409 hPtr = Blt_FindHashEntry(&watchTable, (char *)&key);
410 Blt_DeleteHashEntry(&watchTable, hPtr);
411 Blt_FreeUid(key.nameId);
412 Blt_Free(watchPtr);
413 }
414
415 /*
416 *----------------------------------------------------------------------
417 *
418 * NameToWatch --
419 *
420 * Searches for the watch represented by the watch name and its
421 * associated interpreter in its directory.
422 *
423 * Results:
424 * If found, the pointer to the watch structure is returned,
425 * otherwise NULL. If requested, interp-result will be filled
426 * with an error message.
427 *
428 *----------------------------------------------------------------------
429 */
430 static Watch *
431 NameToWatch(interp, name, flags)
432 Tcl_Interp *interp;
433 char *name;
434 int flags;
435 {
436 WatchKey key;
437 Blt_HashEntry *hPtr;
438
439 key.interp = interp;
440 key.nameId = Blt_FindUid(name);
441 if (key.nameId != NULL) {
442 hPtr = Blt_FindHashEntry(&watchTable, (char *)&key);
443 if (hPtr != NULL) {
444 return (Watch *) Blt_GetHashValue(hPtr);
445 }
446 }
447 if (flags & TCL_LEAVE_ERR_MSG) {
448 Tcl_AppendResult(interp, "can't find any watch named \"", name, "\"",
449 (char *)NULL);
450 }
451 return NULL;
452 }
453
454 /*
455 *----------------------------------------------------------------------
456 *
457 * ListWatches --
458 *
459 * Creates a list of all watches in the interpreter. The
460 * list search may be restricted to selected states by
461 * setting "state" to something other than WATCH_STATE_DONT_CARE.
462 *
463 * Results:
464 * A standard Tcl result. Interp->result will contain a list
465 * of all watches matches the state criteria.
466 *
467 *----------------------------------------------------------------------
468 */
469 static int
470 ListWatches(interp, state)
471 Tcl_Interp *interp;
472 enum WatchStates state; /* Active flag */
473 {
474 Blt_HashEntry *hPtr;
475 Blt_HashSearch cursor;
476 register Watch *watchPtr;
477
478 for (hPtr = Blt_FirstHashEntry(&watchTable, &cursor);
479 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
480 watchPtr = (Watch *)Blt_GetHashValue(hPtr);
481 if (watchPtr->interp == interp) {
482 if ((state == WATCH_STATE_DONT_CARE) ||
483 (state == watchPtr->state)) {
484 Tcl_AppendElement(interp, (char *)watchPtr->nameId);
485 }
486 }
487 }
488 return TCL_OK;
489 }
490 /*
491 *----------------------------------------------------------------------
492 *
493 * ConfigWatch --
494 *
495 * Processes argument list of switches and values, setting
496 * Watch fields.
497 *
498 * Results:
499 * If found, the pointer to the watch structure is returned,
500 * otherwise NULL. If requested, interp-result will be filled
501 * with an error message.
502 *
503 *----------------------------------------------------------------------
504 */
505 static int
506 ConfigWatch(watchPtr, interp, argc, argv)
507 Watch *watchPtr;
508 Tcl_Interp *interp;
509 int argc;
510 char *argv[];
511 {
512 if (Blt_ProcessSwitches(interp, switchSpecs, argc, argv, (char *)watchPtr,
513 0) < 0) {
514 return TCL_ERROR;
515 }
516 /*
517 * If the watch's max depth changed or its state, reset the traces.
518 */
519 if (watchPtr->trace != (Tcl_Trace) 0) {
520 Tcl_DeleteTrace(interp, watchPtr->trace);
521 watchPtr->trace = (Tcl_Trace) 0;
522 }
523 if (watchPtr->state == WATCH_STATE_ACTIVE) {
524 watchPtr->trace = Tcl_CreateTrace(interp, watchPtr->maxLevel,
525 PreCmdProc, watchPtr);
526 }
527 return TCL_OK;
528 }
529
530 /* Tcl interface routines */
531 /*
532 *----------------------------------------------------------------------
533 *
534 * CreateOp --
535 *
536 * Creates a new watch and processes any extra switches.
537 *
538 * Results:
539 * A standard Tcl result.
540 *
541 * Side Effects:
542 * A new watch is created.
543 *
544 *----------------------------------------------------------------------
545 */
546 /*ARGSUSED*/
547 static int
548 CreateOp(clientData, interp, argc, argv)
549 ClientData clientData; /* Not used. */
550 Tcl_Interp *interp;
551 int argc;
552 char **argv;
553 {
554 register Watch *watchPtr;
555
556 watchPtr = NameToWatch(interp, argv[2], 0);
557 if (watchPtr != NULL) {
558 Tcl_AppendResult(interp, "a watch \"", argv[2], "\" already exists",
559 (char *)NULL);
560 return TCL_ERROR;
561 }
562 watchPtr = NewWatch(interp, argv[2]);
563 if (watchPtr == NULL) {
564 return TCL_ERROR; /* Can't create new watch */
565 }
566 return ConfigWatch(watchPtr, interp, argc - 3, argv + 3);
567 }
568
569 /*
570 *----------------------------------------------------------------------
571 *
572 * DeleteOp --
573 *
574 * Deletes the watch.
575 *
576 * Results:
577 * A standard Tcl result.
578 *
579 *----------------------------------------------------------------------
580 */
581 /*ARGSUSED*/
582 static int
583 DeleteOp(clientData, interp, argc, argv)
584 ClientData clientData; /* Not used. */
585 Tcl_Interp *interp;
586 int argc;
587 char **argv;
588 {
589 register Watch *watchPtr;
590
591 watchPtr = NameToWatch(interp, argv[2], TCL_LEAVE_ERR_MSG);
592 if (watchPtr == NULL) {
593 return TCL_ERROR;
594 }
595 DestroyWatch(watchPtr);
596 return TCL_OK;
597 }
598
599 /*
600 *----------------------------------------------------------------------
601 *
602 * ActivateOp --
603 *
604 * Activate/deactivates the named watch.
605 *
606 * Results:
607 * A standard Tcl result.
608 *
609 *----------------------------------------------------------------------
610 */
611 /*ARGSUSED*/
612 static int
613 ActivateOp(clientData, interp, argc, argv)
614 ClientData clientData; /* Not used. */
615 Tcl_Interp *interp;
616 int argc;
617 char **argv;
618 {
619 register Watch *watchPtr;
620 enum WatchStates state;
621
622 state = (argv[1][0] == 'a') ? WATCH_STATE_ACTIVE : WATCH_STATE_IDLE;
623 watchPtr = NameToWatch(interp, argv[2], TCL_LEAVE_ERR_MSG);
624 if (watchPtr == NULL) {
625 return TCL_ERROR;
626 }
627 if (state != watchPtr->state) {
628 if (watchPtr->trace == (Tcl_Trace) 0) {
629 watchPtr->trace = Tcl_CreateTrace(interp, watchPtr->maxLevel,
630 PreCmdProc, watchPtr);
631 } else {
632 Tcl_DeleteTrace(interp, watchPtr->trace);
633 watchPtr->trace = (Tcl_Trace) 0;
634 }
635 watchPtr->state = state;
636 }
637 return TCL_OK;
638 }
639
640 /*
641 *----------------------------------------------------------------------
642 *
643 * NamesOp --
644 *
645 * Returns the names of all watches in the interpreter.
646 *
647 * Results:
648 * A standard Tcl result.
649 *
650 *----------------------------------------------------------------------
651 */
652 /*ARGSUSED*/
653 static int
654 NamesOp(clientData, interp, argc, argv)
655 ClientData clientData; /* Not used. */
656 Tcl_Interp *interp;
657 int argc;
658 char **argv;
659 {
660 enum WatchStates state;
661
662 state = WATCH_STATE_DONT_CARE;
663 if (argc == 3) {
664 char c;
665 c = argv[2][0];
666 if ((c == 'a') && (strcmp(argv[2], "active") == 0)) {
667 state = WATCH_STATE_ACTIVE;
668 } else if ((c == 'i') && (strcmp(argv[2], "idle") == 0)) {
669 state = WATCH_STATE_IDLE;
670 } else if ((c == 'i') && (strcmp(argv[2], "ignore") == 0)) {
671 state = WATCH_STATE_DONT_CARE;
672 } else {
673 Tcl_AppendResult(interp, "bad state \"", argv[2], "\" should be \
674 \"active\", \"idle\", or \"ignore\"", (char *)NULL);
675 return TCL_ERROR;
676 }
677 }
678 return ListWatches(interp, state);
679 }
680
681 /*
682 *----------------------------------------------------------------------
683 *
684 * ConfigureOp --
685 *
686 * Convert the range of the pixel values allowed into a list.
687 *
688 * Results:
689 * The string representation of the limits is returned.
690 *
691 *----------------------------------------------------------------------
692 */
693 /*ARGSUSED*/
694 static int
695 ConfigureOp(clientData, interp, argc, argv)
696 ClientData clientData; /* Not used. */
697 Tcl_Interp *interp;
698 int argc;
699 char **argv;
700 {
701 register Watch *watchPtr;
702
703 watchPtr = NameToWatch(interp, argv[2], TCL_LEAVE_ERR_MSG);
704 if (watchPtr == NULL) {
705 return TCL_ERROR;
706 }
707 return ConfigWatch(watchPtr, interp, argc - 3, argv + 3);
708 }
709
710 /*
711 *----------------------------------------------------------------------
712 *
713 * InfoOp --
714 *
715 * Convert the limits of the pixel values allowed into a list.
716 *
717 * Results:
718 * The string representation of the limits is returned.
719 *
720 *----------------------------------------------------------------------
721 */
722 /*ARGSUSED*/
723 static int
724 InfoOp(clientData, interp, argc, argv)
725 ClientData clientData; /* Not used. */
726 Tcl_Interp *interp;
727 int argc;
728 char **argv;
729 {
730 register Watch *watchPtr;
731 char string[200];
732 register char **p;
733
734 watchPtr = NameToWatch(interp, argv[2], TCL_LEAVE_ERR_MSG);
735 if (watchPtr == NULL) {
736 return TCL_ERROR;
737 }
738 if (watchPtr->preCmd != NULL) {
739 Tcl_AppendResult(interp, "-precmd", (char *)NULL);
740 for (p = watchPtr->preCmd; *p != NULL; p++) {
741 Tcl_AppendResult(interp, " ", *p, (char *)NULL);
742 }
743 }
744 if (watchPtr->postCmd != NULL) {
745 Tcl_AppendResult(interp, "-postcmd", (char *)NULL);
746 for (p = watchPtr->postCmd; *p != NULL; p++) {
747 Tcl_AppendResult(interp, " ", *p, (char *)NULL);
748 }
749 }
750 sprintf(string, "%d", watchPtr->maxLevel);
751 Tcl_AppendResult(interp, "-maxlevel ", string, " ", (char *)NULL);
752 Tcl_AppendResult(interp, "-active ",
753 (watchPtr->state == WATCH_STATE_ACTIVE)
754 ? "true" : "false", " ", (char *)NULL);
755 return TCL_OK;
756 }
757
758 /*
759 *--------------------------------------------------------------
760 *
761 * WatchCmd --
762 *
763 * This procedure is invoked to process the Tcl "blt_watch"
764 * command. See the user documentation for details on what
765 * it does.
766 *
767 * Results:
768 * A standard Tcl result.
769 *
770 * Side effects:
771 * See the user documentation.
772 *
773 *--------------------------------------------------------------
774 */
775
776 static Blt_OpSpec watchOps[] =
777 {
778 {"activate", 1, (Blt_Op)ActivateOp, 3, 3, "watchName",},
779 {"configure", 2, (Blt_Op)ConfigureOp, 3, 0,
780 "watchName ?options...?"},
781 {"create", 2, (Blt_Op)CreateOp, 3, 0, "watchName ?switches?",},
782 {"deactivate", 3, (Blt_Op)ActivateOp, 3, 3, "watchName",},
783 {"delete", 3, (Blt_Op)DeleteOp, 3, 3, "watchName",},
784 {"info", 1, (Blt_Op)InfoOp, 3, 3, "watchName",},
785 {"names", 1, (Blt_Op)NamesOp, 2, 3, "?state?",},
786 };
787 static int nWatchOps = sizeof(watchOps) / sizeof(Blt_OpSpec);
788
789 /*ARGSUSED*/
790 static int
791 WatchCmd(clientData, interp, argc, argv)
792 ClientData clientData; /* Not used. */
793 Tcl_Interp *interp;
794 int argc;
795 char **argv;
796 {
797 Blt_Op proc;
798 int result;
799
800 proc = Blt_GetOp(interp, nWatchOps, watchOps, BLT_OP_ARG1, argc, argv, 0);
801 if (proc == NULL) {
802 return TCL_ERROR;
803 }
804 result = (*proc) (clientData, interp, argc, argv);
805 return result;
806 }
807
808 /* ARGSUSED */
809 static void
810 WatchDeleteCmd(clientData)
811 ClientData clientData; /* Not Used. */
812 {
813 refCount--;
814 if (refCount == 0) {
815 Blt_DeleteHashTable(&watchTable);
816 }
817 }
818
819 /* Public initialization routine */
820 /*
821 *--------------------------------------------------------------
822 *
823 * Blt_WatchInit --
824 *
825 * This procedure is invoked to initialize the Tcl command
826 * "blt_watch".
827 *
828 * Results:
829 * None.
830 *
831 * Side effects:
832 * Creates the new command and adds a new entry into a
833 * global Tcl associative array.
834 *
835 *--------------------------------------------------------------
836 */
837 int
838 Blt_WatchInit(interp)
839 Tcl_Interp *interp;
840 {
841 static Blt_CmdSpec cmdSpec =
842 {"watch", WatchCmd, WatchDeleteCmd};
843
844 if (refCount == 0) {
845 Blt_InitHashTable(&watchTable, sizeof(WatchKey) / sizeof(int));
846 }
847 refCount++;
848
849 if (Blt_InitCmd(interp, "blt", &cmdSpec) == NULL) {
850 return TCL_ERROR;
851 }
852 return TCL_OK;
853 }