| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "tk.h" |
| 17 |
#include <string.h> |
| 18 |
|
| 19 |
#include "tkInt.h" |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
typedef struct ConsoleInfo { |
| 28 |
Tcl_Interp *consoleInterp; |
| 29 |
Tcl_Interp *interp; |
| 30 |
} ConsoleInfo; |
| 31 |
|
| 32 |
typedef struct ThreadSpecificData { |
| 33 |
Tcl_Interp *gStdoutInterp; |
| 34 |
} ThreadSpecificData; |
| 35 |
static Tcl_ThreadDataKey dataKey; |
| 36 |
static int consoleInitialized = 0; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
TCL_DECLARE_MUTEX(consoleMutex) |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
static int ConsoleCmd _ANSI_ARGS_((ClientData clientData, |
| 51 |
Tcl_Interp *interp, int argc, CONST char **argv)); |
| 52 |
static void ConsoleDeleteProc _ANSI_ARGS_((ClientData clientData)); |
| 53 |
static void ConsoleEventProc _ANSI_ARGS_((ClientData clientData, |
| 54 |
XEvent *eventPtr)); |
| 55 |
static int InterpreterCmd _ANSI_ARGS_((ClientData clientData, |
| 56 |
Tcl_Interp *interp, int argc, CONST char **argv)); |
| 57 |
|
| 58 |
static int ConsoleInput _ANSI_ARGS_((ClientData instanceData, |
| 59 |
char *buf, int toRead, int *errorCode)); |
| 60 |
static int ConsoleOutput _ANSI_ARGS_((ClientData instanceData, |
| 61 |
CONST char *buf, int toWrite, int *errorCode)); |
| 62 |
static int ConsoleClose _ANSI_ARGS_((ClientData instanceData, |
| 63 |
Tcl_Interp *interp)); |
| 64 |
static void ConsoleWatch _ANSI_ARGS_((ClientData instanceData, |
| 65 |
int mask)); |
| 66 |
static int ConsoleHandle _ANSI_ARGS_((ClientData instanceData, |
| 67 |
int direction, ClientData *handlePtr)); |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
static Tcl_ChannelType consoleChannelType = { |
| 74 |
"console", |
| 75 |
NULL, |
| 76 |
ConsoleClose, |
| 77 |
ConsoleInput, |
| 78 |
ConsoleOutput, |
| 79 |
NULL, |
| 80 |
NULL, |
| 81 |
NULL, |
| 82 |
ConsoleWatch, |
| 83 |
ConsoleHandle, |
| 84 |
}; |
| 85 |
|
| 86 |
|
| 87 |
#ifdef __WIN32__ |
| 88 |
|
| 89 |
#include <windows.h> |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
static int ShouldUseConsoleChannel(type) |
| 109 |
int type; |
| 110 |
{ |
| 111 |
DWORD handleId; |
| 112 |
DCB dcb; |
| 113 |
DWORD consoleParams; |
| 114 |
DWORD fileType; |
| 115 |
int mode; |
| 116 |
char *bufMode; |
| 117 |
HANDLE handle; |
| 118 |
|
| 119 |
switch (type) { |
| 120 |
case TCL_STDIN: |
| 121 |
handleId = STD_INPUT_HANDLE; |
| 122 |
mode = TCL_READABLE; |
| 123 |
bufMode = "line"; |
| 124 |
break; |
| 125 |
case TCL_STDOUT: |
| 126 |
handleId = STD_OUTPUT_HANDLE; |
| 127 |
mode = TCL_WRITABLE; |
| 128 |
bufMode = "line"; |
| 129 |
break; |
| 130 |
case TCL_STDERR: |
| 131 |
handleId = STD_ERROR_HANDLE; |
| 132 |
mode = TCL_WRITABLE; |
| 133 |
bufMode = "none"; |
| 134 |
break; |
| 135 |
default: |
| 136 |
return 0; |
| 137 |
break; |
| 138 |
} |
| 139 |
|
| 140 |
handle = GetStdHandle(handleId); |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
if ((handle == INVALID_HANDLE_VALUE) || (handle == 0)) { |
| 149 |
return 1; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
fileType = GetFileType(handle); |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
if (fileType == FILE_TYPE_CHAR) { |
| 177 |
dcb.DCBlength = sizeof( DCB ) ; |
| 178 |
if (!GetConsoleMode(handle, &consoleParams) && |
| 179 |
!GetCommState(handle, &dcb)) { |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
return 1; |
| 186 |
} |
| 187 |
} else if (fileType == FILE_TYPE_UNKNOWN) { |
| 188 |
return 1; |
| 189 |
} else if (Tcl_GetStdChannel(type) == NULL) { |
| 190 |
return 1; |
| 191 |
} |
| 192 |
|
| 193 |
return 0; |
| 194 |
} |
| 195 |
#else |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
#define ShouldUseConsoleChannel(chan) (1) |
| 201 |
#endif |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
void |
| 223 |
Tk_InitConsoleChannels(interp) |
| 224 |
Tcl_Interp *interp; |
| 225 |
{ |
| 226 |
Tcl_Channel consoleChannel; |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
if (Tcl_InitStubs(interp, TCL_VERSION, 1) == NULL) { |
| 234 |
return; |
| 235 |
} |
| 236 |
|
| 237 |
Tcl_MutexLock(&consoleMutex); |
| 238 |
if (!consoleInitialized) { |
| 239 |
|
| 240 |
consoleInitialized = 1; |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
if (ShouldUseConsoleChannel(TCL_STDIN)) { |
| 256 |
consoleChannel = Tcl_CreateChannel(&consoleChannelType, "console0", |
| 257 |
(ClientData) TCL_STDIN, TCL_READABLE); |
| 258 |
if (consoleChannel != NULL) { |
| 259 |
Tcl_SetChannelOption(NULL, consoleChannel, |
| 260 |
"-translation", "lf"); |
| 261 |
Tcl_SetChannelOption(NULL, consoleChannel, |
| 262 |
"-buffering", "none"); |
| 263 |
Tcl_SetChannelOption(NULL, consoleChannel, |
| 264 |
"-encoding", "utf-8"); |
| 265 |
} |
| 266 |
Tcl_SetStdChannel(consoleChannel, TCL_STDIN); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
if (ShouldUseConsoleChannel(TCL_STDOUT)) { |
| 274 |
consoleChannel = Tcl_CreateChannel(&consoleChannelType, "console1", |
| 275 |
(ClientData) TCL_STDOUT, TCL_WRITABLE); |
| 276 |
if (consoleChannel != NULL) { |
| 277 |
Tcl_SetChannelOption(NULL, consoleChannel, |
| 278 |
"-translation", "lf"); |
| 279 |
Tcl_SetChannelOption(NULL, consoleChannel, |
| 280 |
"-buffering", "none"); |
| 281 |
Tcl_SetChannelOption(NULL, consoleChannel, |
| 282 |
"-encoding", "utf-8"); |
| 283 |
} |
| 284 |
Tcl_SetStdChannel(consoleChannel, TCL_STDOUT); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
if (ShouldUseConsoleChannel(TCL_STDERR)) { |
| 292 |
consoleChannel = Tcl_CreateChannel(&consoleChannelType, "console2", |
| 293 |
(ClientData) TCL_STDERR, TCL_WRITABLE); |
| 294 |
if (consoleChannel != NULL) { |
| 295 |
Tcl_SetChannelOption(NULL, consoleChannel, |
| 296 |
"-translation", "lf"); |
| 297 |
Tcl_SetChannelOption(NULL, consoleChannel, |
| 298 |
"-buffering", "none"); |
| 299 |
Tcl_SetChannelOption(NULL, consoleChannel, |
| 300 |
"-encoding", "utf-8"); |
| 301 |
} |
| 302 |
Tcl_SetStdChannel(consoleChannel, TCL_STDERR); |
| 303 |
} |
| 304 |
} |
| 305 |
Tcl_MutexUnlock(&consoleMutex); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
int |
| 327 |
Tk_CreateConsoleWindow(interp) |
| 328 |
Tcl_Interp *interp; |
| 329 |
{ |
| 330 |
Tcl_Interp *consoleInterp; |
| 331 |
ConsoleInfo *info; |
| 332 |
Tk_Window mainWindow = Tk_MainWindow(interp); |
| 333 |
ThreadSpecificData *tsdPtr = (ThreadSpecificData *) |
| 334 |
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData)); |
| 335 |
#ifdef MAC_TCL |
| 336 |
static char initCmd[] = "if {[catch {source $tk_library:console.tcl}]} {source -rsrc console}"; |
| 337 |
#else |
| 338 |
static char initCmd[] = "source $tk_library/console.tcl"; |
| 339 |
#endif |
| 340 |
|
| 341 |
consoleInterp = Tcl_CreateInterp(); |
| 342 |
if (consoleInterp == NULL) { |
| 343 |
goto error; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
if (Tcl_Init(consoleInterp) != TCL_OK) { |
| 351 |
goto error; |
| 352 |
} |
| 353 |
if (Tk_Init(consoleInterp) != TCL_OK) { |
| 354 |
goto error; |
| 355 |
} |
| 356 |
tsdPtr->gStdoutInterp = interp; |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
info = (ConsoleInfo *) ckalloc(sizeof(ConsoleInfo)); |
| 362 |
info->interp = interp; |
| 363 |
info->consoleInterp = consoleInterp; |
| 364 |
Tcl_CreateCommand(interp, "console", ConsoleCmd, (ClientData) info, |
| 365 |
(Tcl_CmdDeleteProc *) ConsoleDeleteProc); |
| 366 |
Tcl_CreateCommand(consoleInterp, "consoleinterp", InterpreterCmd, |
| 367 |
(ClientData) info, (Tcl_CmdDeleteProc *) NULL); |
| 368 |
|
| 369 |
Tk_CreateEventHandler(mainWindow, StructureNotifyMask, ConsoleEventProc, |
| 370 |
(ClientData) info); |
| 371 |
|
| 372 |
Tcl_Preserve((ClientData) consoleInterp); |
| 373 |
if (Tcl_Eval(consoleInterp, initCmd) == TCL_ERROR) { |
| 374 |
|
| 375 |
printf("Eval error: %s", Tcl_GetResult(consoleInterp)); |
| 376 |
} |
| 377 |
Tcl_Release((ClientData) consoleInterp); |
| 378 |
return TCL_OK; |
| 379 |
|
| 380 |
error: |
| 381 |
if (consoleInterp != NULL) { |
| 382 |
Tcl_DeleteInterp(consoleInterp); |
| 383 |
} |
| 384 |
return TCL_ERROR; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
static int |
| 406 |
ConsoleOutput(instanceData, buf, toWrite, errorCode) |
| 407 |
ClientData instanceData; |
| 408 |
CONST char *buf; |
| 409 |
int toWrite; |
| 410 |
int *errorCode; |
| 411 |
{ |
| 412 |
ThreadSpecificData *tsdPtr = (ThreadSpecificData *) |
| 413 |
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData)); |
| 414 |
|
| 415 |
*errorCode = 0; |
| 416 |
Tcl_SetErrno(0); |
| 417 |
|
| 418 |
if (tsdPtr->gStdoutInterp != NULL) { |
| 419 |
TkConsolePrint(tsdPtr->gStdoutInterp, (int) instanceData, buf, |
| 420 |
toWrite); |
| 421 |
} |
| 422 |
|
| 423 |
return toWrite; |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
static int |
| 444 |
ConsoleInput(instanceData, buf, bufSize, errorCode) |
| 445 |
ClientData instanceData; |
| 446 |
char *buf; |
| 447 |
int bufSize; |
| 448 |
|
| 449 |
int *errorCode; |
| 450 |
{ |
| 451 |
return 0; |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
static int |
| 472 |
ConsoleClose(instanceData, interp) |
| 473 |
ClientData instanceData; |
| 474 |
Tcl_Interp *interp; |
| 475 |
{ |
| 476 |
ThreadSpecificData *tsdPtr = (ThreadSpecificData *) |
| 477 |
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData)); |
| 478 |
tsdPtr->gStdoutInterp = NULL; |
| 479 |
return 0; |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
static void |
| 502 |
ConsoleWatch(instanceData, mask) |
| 503 |
ClientData instanceData; |
| 504 |
int mask; |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
{ |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
static int |
| 531 |
ConsoleHandle(instanceData, direction, handlePtr) |
| 532 |
ClientData instanceData; |
| 533 |
int direction; |
| 534 |
|
| 535 |
|
| 536 |
ClientData *handlePtr; |
| 537 |
{ |
| 538 |
return TCL_ERROR; |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
static int |
| 559 |
ConsoleCmd(clientData, interp, argc, argv) |
| 560 |
ClientData clientData; |
| 561 |
Tcl_Interp *interp; |
| 562 |
int argc; |
| 563 |
CONST char **argv; |
| 564 |
{ |
| 565 |
ConsoleInfo *info = (ConsoleInfo *) clientData; |
| 566 |
char c; |
| 567 |
size_t length; |
| 568 |
int result; |
| 569 |
Tcl_Interp *consoleInterp; |
| 570 |
Tcl_DString dString; |
| 571 |
|
| 572 |
if (argc < 2) { |
| 573 |
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 574 |
" option ?arg arg ...?\"", (char *) NULL); |
| 575 |
return TCL_ERROR; |
| 576 |
} |
| 577 |
|
| 578 |
c = argv[1][0]; |
| 579 |
length = strlen(argv[1]); |
| 580 |
result = TCL_OK; |
| 581 |
consoleInterp = info->consoleInterp; |
| 582 |
Tcl_Preserve((ClientData) consoleInterp); |
| 583 |
Tcl_DStringInit(&dString); |
| 584 |
|
| 585 |
if ((c == 't') && (strncmp(argv[1], "title", length)) == 0) { |
| 586 |
Tcl_DStringAppend(&dString, "wm title . ", -1); |
| 587 |
if (argc == 3) { |
| 588 |
Tcl_DStringAppendElement(&dString, argv[2]); |
| 589 |
} |
| 590 |
Tcl_Eval(consoleInterp, Tcl_DStringValue(&dString)); |
| 591 |
} else if ((c == 'h') && (strncmp(argv[1], "hide", length)) == 0) { |
| 592 |
Tcl_DStringAppend(&dString, "wm withdraw . ", -1); |
| 593 |
Tcl_Eval(consoleInterp, Tcl_DStringValue(&dString)); |
| 594 |
} else if ((c == 's') && (strncmp(argv[1], "show", length)) == 0) { |
| 595 |
Tcl_DStringAppend(&dString, "wm deiconify . ", -1); |
| 596 |
Tcl_Eval(consoleInterp, Tcl_DStringValue(&dString)); |
| 597 |
} else if ((c == 'e') && (strncmp(argv[1], "eval", length)) == 0) { |
| 598 |
if (argc == 3) { |
| 599 |
result = Tcl_Eval(consoleInterp, argv[2]); |
| 600 |
Tcl_AppendResult(interp, Tcl_GetStringResult(consoleInterp), |
| 601 |
(char *) NULL); |
| 602 |
} else { |
| 603 |
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 604 |
" eval command\"", (char *) NULL); |
| 605 |
result = TCL_ERROR; |
| 606 |
} |
| 607 |
} else { |
| 608 |
Tcl_AppendResult(interp, "bad option \"", argv[1], |
| 609 |
"\": should be hide, show, or title", |
| 610 |
(char *) NULL); |
| 611 |
result = TCL_ERROR; |
| 612 |
} |
| 613 |
Tcl_DStringFree(&dString); |
| 614 |
Tcl_Release((ClientData) consoleInterp); |
| 615 |
return result; |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
static int |
| 636 |
InterpreterCmd(clientData, interp, argc, argv) |
| 637 |
ClientData clientData; |
| 638 |
Tcl_Interp *interp; |
| 639 |
int argc; |
| 640 |
CONST char **argv; |
| 641 |
{ |
| 642 |
ConsoleInfo *info = (ConsoleInfo *) clientData; |
| 643 |
char c; |
| 644 |
size_t length; |
| 645 |
int result; |
| 646 |
Tcl_Interp *otherInterp; |
| 647 |
|
| 648 |
if (argc < 2) { |
| 649 |
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 650 |
" option ?arg arg ...?\"", (char *) NULL); |
| 651 |
return TCL_ERROR; |
| 652 |
} |
| 653 |
|
| 654 |
c = argv[1][0]; |
| 655 |
length = strlen(argv[1]); |
| 656 |
otherInterp = info->interp; |
| 657 |
Tcl_Preserve((ClientData) otherInterp); |
| 658 |
if ((c == 'e') && (strncmp(argv[1], "eval", length)) == 0) { |
| 659 |
result = Tcl_GlobalEval(otherInterp, argv[2]); |
| 660 |
Tcl_AppendResult(interp, Tcl_GetResult(otherInterp), (char *) NULL); |
| 661 |
} else if ((c == 'r') && (strncmp(argv[1], "record", length)) == 0) { |
| 662 |
Tcl_RecordAndEval(otherInterp, argv[2], TCL_EVAL_GLOBAL); |
| 663 |
result = TCL_OK; |
| 664 |
Tcl_ResetResult(interp); |
| 665 |
Tcl_AppendResult(interp, Tcl_GetResult(otherInterp), (char *) NULL); |
| 666 |
} else { |
| 667 |
Tcl_AppendResult(interp, "bad option \"", argv[1], |
| 668 |
"\": should be eval or record", |
| 669 |
(char *) NULL); |
| 670 |
result = TCL_ERROR; |
| 671 |
} |
| 672 |
Tcl_Release((ClientData) otherInterp); |
| 673 |
return result; |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
static void |
| 694 |
ConsoleDeleteProc(clientData) |
| 695 |
ClientData clientData; |
| 696 |
{ |
| 697 |
ConsoleInfo *info = (ConsoleInfo *) clientData; |
| 698 |
|
| 699 |
Tcl_DeleteInterp(info->consoleInterp); |
| 700 |
info->consoleInterp = NULL; |
| 701 |
} |
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
static void |
| 723 |
ConsoleEventProc(clientData, eventPtr) |
| 724 |
ClientData clientData; |
| 725 |
XEvent *eventPtr; |
| 726 |
{ |
| 727 |
ConsoleInfo *info = (ConsoleInfo *) clientData; |
| 728 |
Tcl_Interp *consoleInterp; |
| 729 |
Tcl_DString dString; |
| 730 |
|
| 731 |
if (eventPtr->type == DestroyNotify) { |
| 732 |
|
| 733 |
Tcl_DStringInit(&dString); |
| 734 |
|
| 735 |
consoleInterp = info->consoleInterp; |
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
if (consoleInterp == (Tcl_Interp *) NULL) { |
| 745 |
return; |
| 746 |
} |
| 747 |
Tcl_Preserve((ClientData) consoleInterp); |
| 748 |
Tcl_DStringAppend(&dString, "::tk::ConsoleExit", -1); |
| 749 |
Tcl_Eval(consoleInterp, Tcl_DStringValue(&dString)); |
| 750 |
Tcl_DStringFree(&dString); |
| 751 |
Tcl_Release((ClientData) consoleInterp); |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
void |
| 774 |
TkConsolePrint(interp, devId, buffer, size) |
| 775 |
Tcl_Interp *interp; |
| 776 |
int devId; |
| 777 |
|
| 778 |
CONST char *buffer; |
| 779 |
long size; |
| 780 |
{ |
| 781 |
Tcl_DString command, output; |
| 782 |
Tcl_CmdInfo cmdInfo; |
| 783 |
char *cmd; |
| 784 |
ConsoleInfo *info; |
| 785 |
Tcl_Interp *consoleInterp; |
| 786 |
int result; |
| 787 |
|
| 788 |
if (interp == NULL) { |
| 789 |
return; |
| 790 |
} |
| 791 |
|
| 792 |
if (devId == TCL_STDERR) { |
| 793 |
cmd = "::tk::ConsoleOutput stderr "; |
| 794 |
} else { |
| 795 |
cmd = "::tk::ConsoleOutput stdout "; |
| 796 |
} |
| 797 |
|
| 798 |
result = Tcl_GetCommandInfo(interp, "console", &cmdInfo); |
| 799 |
if (result == 0) { |
| 800 |
return; |
| 801 |
} |
| 802 |
info = (ConsoleInfo *) cmdInfo.clientData; |
| 803 |
|
| 804 |
Tcl_DStringInit(&output); |
| 805 |
Tcl_DStringAppend(&output, buffer, size); |
| 806 |
|
| 807 |
Tcl_DStringInit(&command); |
| 808 |
Tcl_DStringAppend(&command, cmd, (int) strlen(cmd)); |
| 809 |
Tcl_DStringAppendElement(&command, output.string); |
| 810 |
|
| 811 |
consoleInterp = info->consoleInterp; |
| 812 |
Tcl_Preserve((ClientData) consoleInterp); |
| 813 |
Tcl_Eval(consoleInterp, command.string); |
| 814 |
Tcl_Release((ClientData) consoleInterp); |
| 815 |
|
| 816 |
Tcl_DStringFree(&command); |
| 817 |
Tcl_DStringFree(&output); |
| 818 |
} |