| 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 |
#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 |
| 41 |
|
| 42 |
enum WatchStates { |
| 43 |
WATCH_STATE_DONT_CARE = -1, |
| 44 |
WATCH_STATE_IDLE = 0, |
| 45 |
WATCH_STATE_ACTIVE = 1 |
| 46 |
}; |
| 47 |
|
| 48 |
typedef struct { |
| 49 |
Tcl_Interp *interp; |
| 50 |
Blt_Uid nameId; |
| 51 |
|
| 52 |
|
| 53 |
enum WatchStates state; |
| 54 |
|
| 55 |
int maxLevel; |
| 56 |
char **preCmd; |
| 57 |
|
| 58 |
|
| 59 |
char **postCmd; |
| 60 |
|
| 61 |
Tcl_Trace trace; |
| 62 |
|
| 63 |
Tcl_AsyncHandler asyncHandle; |
| 64 |
|
| 65 |
|
| 66 |
int active; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
int level; |
| 71 |
char *cmdPtr; |
| 72 |
|
| 73 |
char *args; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
} Watch; |
| 78 |
|
| 79 |
typedef struct { |
| 80 |
Blt_Uid nameId; |
| 81 |
Tcl_Interp *interp; |
| 82 |
|
| 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 |
|
| 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 |
static void |
| 133 |
PreCmdProc(clientData, interp, level, command, cmdProc, cmdClientData, |
| 134 |
argc, argv) |
| 135 |
ClientData clientData; |
| 136 |
Tcl_Interp *interp; |
| 137 |
int level; |
| 138 |
char *command; |
| 139 |
Tcl_CmdProc *cmdProc; |
| 140 |
ClientData cmdClientData; |
| 141 |
int argc; |
| 142 |
char **argv; |
| 143 |
|
| 144 |
{ |
| 145 |
Watch *watchPtr = clientData; |
| 146 |
|
| 147 |
if (watchPtr->active) { |
| 148 |
return; |
| 149 |
} |
| 150 |
watchPtr->cmdPtr = command; |
| 151 |
watchPtr->level = level; |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 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 |
|
| 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 |
|
| 189 |
if (watchPtr->postCmd != NULL) { |
| 190 |
Tcl_AsyncMark(watchPtr->asyncHandle); |
| 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 |
static int |
| 222 |
PostCmdProc(clientData, interp, code) |
| 223 |
ClientData clientData; |
| 224 |
Tcl_Interp *interp; |
| 225 |
int code; |
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 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 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 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 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 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 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
static int |
| 470 |
ListWatches(interp, state) |
| 471 |
Tcl_Interp *interp; |
| 472 |
enum WatchStates state; |
| 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 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 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 |
|
| 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 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
static int |
| 548 |
CreateOp(clientData, interp, argc, argv) |
| 549 |
ClientData clientData; |
| 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; |
| 565 |
} |
| 566 |
return ConfigWatch(watchPtr, interp, argc - 3, argv + 3); |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
static int |
| 583 |
DeleteOp(clientData, interp, argc, argv) |
| 584 |
ClientData clientData; |
| 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 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
static int |
| 613 |
ActivateOp(clientData, interp, argc, argv) |
| 614 |
ClientData clientData; |
| 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 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
static int |
| 654 |
NamesOp(clientData, interp, argc, argv) |
| 655 |
ClientData clientData; |
| 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 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
static int |
| 695 |
ConfigureOp(clientData, interp, argc, argv) |
| 696 |
ClientData clientData; |
| 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 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
static int |
| 724 |
InfoOp(clientData, interp, argc, argv) |
| 725 |
ClientData clientData; |
| 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 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 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 |
|
| 790 |
static int |
| 791 |
WatchCmd(clientData, interp, argc, argv) |
| 792 |
ClientData clientData; |
| 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 |
|
| 809 |
static void |
| 810 |
WatchDeleteCmd(clientData) |
| 811 |
ClientData clientData; |
| 812 |
{ |
| 813 |
refCount--; |
| 814 |
if (refCount == 0) { |
| 815 |
Blt_DeleteHashTable(&watchTable); |
| 816 |
} |
| 817 |
} |
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 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 |
} |