| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 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 |
static Tcl_Interp *gStdoutInterp = NULL; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
void TkConsoleCreate _ANSI_ARGS_((void)); |
| 41 |
int TkConsoleInit _ANSI_ARGS_((Tcl_Interp *interp)); |
| 42 |
void TkConsolePrint _ANSI_ARGS_((Tcl_Interp *interp, |
| 43 |
int devId, char *buffer, long size)); |
| 44 |
|
| 45 |
static int ConsoleCmd _ANSI_ARGS_((ClientData clientData, |
| 46 |
Tcl_Interp *interp, int argc, char **argv)); |
| 47 |
static void ConsoleDeleteProc _ANSI_ARGS_((ClientData clientData)); |
| 48 |
static void ConsoleEventProc _ANSI_ARGS_((ClientData clientData, |
| 49 |
XEvent *eventPtr)); |
| 50 |
static int InterpreterCmd _ANSI_ARGS_((ClientData clientData, |
| 51 |
Tcl_Interp *interp, int argc, char **argv)); |
| 52 |
|
| 53 |
static int ConsoleInput _ANSI_ARGS_((ClientData instanceData, |
| 54 |
char *buf, int toRead, int *errorCode)); |
| 55 |
static int ConsoleOutput _ANSI_ARGS_((ClientData instanceData, |
| 56 |
char *buf, int toWrite, int *errorCode)); |
| 57 |
static int ConsoleClose _ANSI_ARGS_((ClientData instanceData, |
| 58 |
Tcl_Interp *interp)); |
| 59 |
static void ConsoleWatch _ANSI_ARGS_((ClientData instanceData, |
| 60 |
int mask)); |
| 61 |
static int ConsoleHandle _ANSI_ARGS_((ClientData instanceData, |
| 62 |
int direction, ClientData *handlePtr)); |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
static Tcl_ChannelType consoleChannelType = { |
| 69 |
"console", |
| 70 |
NULL, |
| 71 |
ConsoleClose, |
| 72 |
ConsoleInput, |
| 73 |
ConsoleOutput, |
| 74 |
NULL, |
| 75 |
NULL, |
| 76 |
NULL, |
| 77 |
ConsoleWatch, |
| 78 |
ConsoleHandle, |
| 79 |
}; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
void |
| 101 |
TkConsoleCreate() |
| 102 |
{ |
| 103 |
Tcl_Channel consoleChannel; |
| 104 |
|
| 105 |
consoleChannel = Tcl_CreateChannel(&consoleChannelType, "console0", |
| 106 |
(ClientData) TCL_STDIN, TCL_READABLE); |
| 107 |
if (consoleChannel != NULL) { |
| 108 |
Tcl_SetChannelOption(NULL, consoleChannel, "-translation", "lf"); |
| 109 |
Tcl_SetChannelOption(NULL, consoleChannel, "-buffering", "none"); |
| 110 |
} |
| 111 |
Tcl_SetStdChannel(consoleChannel, TCL_STDIN); |
| 112 |
consoleChannel = Tcl_CreateChannel(&consoleChannelType, "console1", |
| 113 |
(ClientData) TCL_STDOUT, TCL_WRITABLE); |
| 114 |
if (consoleChannel != NULL) { |
| 115 |
Tcl_SetChannelOption(NULL, consoleChannel, "-translation", "lf"); |
| 116 |
Tcl_SetChannelOption(NULL, consoleChannel, "-buffering", "none"); |
| 117 |
} |
| 118 |
Tcl_SetStdChannel(consoleChannel, TCL_STDOUT); |
| 119 |
consoleChannel = Tcl_CreateChannel(&consoleChannelType, "console2", |
| 120 |
(ClientData) TCL_STDERR, TCL_WRITABLE); |
| 121 |
if (consoleChannel != NULL) { |
| 122 |
Tcl_SetChannelOption(NULL, consoleChannel, "-translation", "lf"); |
| 123 |
Tcl_SetChannelOption(NULL, consoleChannel, "-buffering", "none"); |
| 124 |
} |
| 125 |
Tcl_SetStdChannel(consoleChannel, TCL_STDERR); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
int |
| 147 |
TkConsoleInit(interp) |
| 148 |
Tcl_Interp *interp; |
| 149 |
{ |
| 150 |
Tcl_Interp *consoleInterp; |
| 151 |
ConsoleInfo *info; |
| 152 |
Tk_Window mainWindow = Tk_MainWindow(interp); |
| 153 |
#ifdef MAC_TCL |
| 154 |
static char initCmd[] = "source -rsrc {Console}"; |
| 155 |
#else |
| 156 |
static char initCmd[] = "source [file join $tk_library console.tcl]"; |
| 157 |
#endif |
| 158 |
|
| 159 |
consoleInterp = Tcl_CreateInterp(); |
| 160 |
if (consoleInterp == NULL) { |
| 161 |
goto error; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
if (Tcl_Init(consoleInterp) != TCL_OK) { |
| 169 |
goto error; |
| 170 |
} |
| 171 |
if (Tk_Init(consoleInterp) != TCL_OK) { |
| 172 |
goto error; |
| 173 |
} |
| 174 |
gStdoutInterp = interp; |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
info = (ConsoleInfo *) ckalloc(sizeof(ConsoleInfo)); |
| 180 |
info->interp = interp; |
| 181 |
info->consoleInterp = consoleInterp; |
| 182 |
Tcl_CreateCommand(interp, "console", ConsoleCmd, (ClientData) info, |
| 183 |
(Tcl_CmdDeleteProc *) ConsoleDeleteProc); |
| 184 |
Tcl_CreateCommand(consoleInterp, "consoleinterp", InterpreterCmd, |
| 185 |
(ClientData) info, (Tcl_CmdDeleteProc *) NULL); |
| 186 |
|
| 187 |
Tk_CreateEventHandler(mainWindow, StructureNotifyMask, ConsoleEventProc, |
| 188 |
(ClientData) info); |
| 189 |
|
| 190 |
Tcl_Preserve((ClientData) consoleInterp); |
| 191 |
if (Tcl_Eval(consoleInterp, initCmd) == TCL_ERROR) { |
| 192 |
|
| 193 |
printf("Eval error: %s", consoleInterp->result); |
| 194 |
} |
| 195 |
Tcl_Release((ClientData) consoleInterp); |
| 196 |
return TCL_OK; |
| 197 |
|
| 198 |
error: |
| 199 |
if (consoleInterp != NULL) { |
| 200 |
Tcl_DeleteInterp(consoleInterp); |
| 201 |
} |
| 202 |
return TCL_ERROR; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
static int |
| 224 |
ConsoleOutput(instanceData, buf, toWrite, errorCode) |
| 225 |
ClientData instanceData; |
| 226 |
char *buf; |
| 227 |
int toWrite; |
| 228 |
int *errorCode; |
| 229 |
{ |
| 230 |
*errorCode = 0; |
| 231 |
Tcl_SetErrno(0); |
| 232 |
|
| 233 |
if (gStdoutInterp != NULL) { |
| 234 |
TkConsolePrint(gStdoutInterp, (int) instanceData, buf, toWrite); |
| 235 |
} |
| 236 |
|
| 237 |
return toWrite; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
static int |
| 258 |
ConsoleInput(instanceData, buf, bufSize, errorCode) |
| 259 |
ClientData instanceData; |
| 260 |
char *buf; |
| 261 |
int bufSize; |
| 262 |
|
| 263 |
int *errorCode; |
| 264 |
{ |
| 265 |
return 0; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
static int |
| 286 |
ConsoleClose(instanceData, interp) |
| 287 |
ClientData instanceData; |
| 288 |
Tcl_Interp *interp; |
| 289 |
{ |
| 290 |
return 0; |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
static void |
| 313 |
ConsoleWatch(instanceData, mask) |
| 314 |
ClientData instanceData; |
| 315 |
int mask; |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
{ |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
static int |
| 342 |
ConsoleHandle(instanceData, direction, handlePtr) |
| 343 |
ClientData instanceData; |
| 344 |
int direction; |
| 345 |
|
| 346 |
|
| 347 |
ClientData *handlePtr; |
| 348 |
{ |
| 349 |
return TCL_ERROR; |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
static int |
| 370 |
ConsoleCmd(clientData, interp, argc, argv) |
| 371 |
ClientData clientData; |
| 372 |
Tcl_Interp *interp; |
| 373 |
int argc; |
| 374 |
char **argv; |
| 375 |
{ |
| 376 |
ConsoleInfo *info = (ConsoleInfo *) clientData; |
| 377 |
char c; |
| 378 |
int length; |
| 379 |
int result; |
| 380 |
Tcl_Interp *consoleInterp; |
| 381 |
|
| 382 |
if (argc < 2) { |
| 383 |
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 384 |
" option ?arg arg ...?\"", (char *) NULL); |
| 385 |
return TCL_ERROR; |
| 386 |
} |
| 387 |
|
| 388 |
c = argv[1][0]; |
| 389 |
length = strlen(argv[1]); |
| 390 |
result = TCL_OK; |
| 391 |
consoleInterp = info->consoleInterp; |
| 392 |
Tcl_Preserve((ClientData) consoleInterp); |
| 393 |
if ((c == 't') && (strncmp(argv[1], "title", length)) == 0) { |
| 394 |
Tcl_DString dString; |
| 395 |
|
| 396 |
Tcl_DStringInit(&dString); |
| 397 |
Tcl_DStringAppend(&dString, "wm title . ", -1); |
| 398 |
if (argc == 3) { |
| 399 |
Tcl_DStringAppendElement(&dString, argv[2]); |
| 400 |
} |
| 401 |
Tcl_Eval(consoleInterp, Tcl_DStringValue(&dString)); |
| 402 |
Tcl_DStringFree(&dString); |
| 403 |
} else if ((c == 'h') && (strncmp(argv[1], "hide", length)) == 0) { |
| 404 |
Tcl_Eval(info->consoleInterp, "wm withdraw ."); |
| 405 |
} else if ((c == 's') && (strncmp(argv[1], "show", length)) == 0) { |
| 406 |
Tcl_Eval(info->consoleInterp, "wm deiconify ."); |
| 407 |
} else if ((c == 'e') && (strncmp(argv[1], "eval", length)) == 0) { |
| 408 |
if (argc == 3) { |
| 409 |
Tcl_Eval(info->consoleInterp, argv[2]); |
| 410 |
} else { |
| 411 |
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 412 |
" eval command\"", (char *) NULL); |
| 413 |
return TCL_ERROR; |
| 414 |
} |
| 415 |
} else { |
| 416 |
Tcl_AppendResult(interp, "bad option \"", argv[1], |
| 417 |
"\": should be hide, show, or title", |
| 418 |
(char *) NULL); |
| 419 |
result = TCL_ERROR; |
| 420 |
} |
| 421 |
Tcl_Release((ClientData) consoleInterp); |
| 422 |
return result; |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
static int |
| 443 |
InterpreterCmd(clientData, interp, argc, argv) |
| 444 |
ClientData clientData; |
| 445 |
Tcl_Interp *interp; |
| 446 |
int argc; |
| 447 |
char **argv; |
| 448 |
{ |
| 449 |
ConsoleInfo *info = (ConsoleInfo *) clientData; |
| 450 |
char c; |
| 451 |
int length; |
| 452 |
int result; |
| 453 |
Tcl_Interp *otherInterp; |
| 454 |
|
| 455 |
if (argc < 2) { |
| 456 |
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 457 |
" option ?arg arg ...?\"", (char *) NULL); |
| 458 |
return TCL_ERROR; |
| 459 |
} |
| 460 |
|
| 461 |
c = argv[1][0]; |
| 462 |
length = strlen(argv[1]); |
| 463 |
otherInterp = info->interp; |
| 464 |
Tcl_Preserve((ClientData) otherInterp); |
| 465 |
if ((c == 'e') && (strncmp(argv[1], "eval", length)) == 0) { |
| 466 |
result = Tcl_GlobalEval(otherInterp, argv[2]); |
| 467 |
Tcl_AppendResult(interp, otherInterp->result, (char *) NULL); |
| 468 |
} else if ((c == 'r') && (strncmp(argv[1], "record", length)) == 0) { |
| 469 |
Tcl_RecordAndEval(otherInterp, argv[2], TCL_EVAL_GLOBAL); |
| 470 |
result = TCL_OK; |
| 471 |
Tcl_AppendResult(interp, otherInterp->result, (char *) NULL); |
| 472 |
} else { |
| 473 |
Tcl_AppendResult(interp, "bad option \"", argv[1], |
| 474 |
"\": should be eval or record", |
| 475 |
(char *) NULL); |
| 476 |
result = TCL_ERROR; |
| 477 |
} |
| 478 |
Tcl_Release((ClientData) otherInterp); |
| 479 |
return result; |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
void |
| 500 |
ConsoleDeleteProc(clientData) |
| 501 |
ClientData clientData; |
| 502 |
{ |
| 503 |
ConsoleInfo *info = (ConsoleInfo *) clientData; |
| 504 |
|
| 505 |
Tcl_DeleteInterp(info->consoleInterp); |
| 506 |
info->consoleInterp = NULL; |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
static void |
| 529 |
ConsoleEventProc(clientData, eventPtr) |
| 530 |
ClientData clientData; |
| 531 |
XEvent *eventPtr; |
| 532 |
{ |
| 533 |
ConsoleInfo *info = (ConsoleInfo *) clientData; |
| 534 |
Tcl_Interp *consoleInterp; |
| 535 |
|
| 536 |
if (eventPtr->type == DestroyNotify) { |
| 537 |
consoleInterp = info->consoleInterp; |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
if (consoleInterp == (Tcl_Interp *) NULL) { |
| 547 |
return; |
| 548 |
} |
| 549 |
Tcl_Preserve((ClientData) consoleInterp); |
| 550 |
Tcl_Eval(consoleInterp, "tkConsoleExit"); |
| 551 |
Tcl_Release((ClientData) consoleInterp); |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
void |
| 574 |
TkConsolePrint(interp, devId, buffer, size) |
| 575 |
Tcl_Interp *interp; |
| 576 |
int devId; |
| 577 |
|
| 578 |
char *buffer; |
| 579 |
long size; |
| 580 |
{ |
| 581 |
Tcl_DString command, output; |
| 582 |
Tcl_CmdInfo cmdInfo; |
| 583 |
char *cmd; |
| 584 |
ConsoleInfo *info; |
| 585 |
Tcl_Interp *consoleInterp; |
| 586 |
int result; |
| 587 |
|
| 588 |
if (interp == NULL) { |
| 589 |
return; |
| 590 |
} |
| 591 |
|
| 592 |
if (devId == TCL_STDERR) { |
| 593 |
cmd = "tkConsoleOutput stderr "; |
| 594 |
} else { |
| 595 |
cmd = "tkConsoleOutput stdout "; |
| 596 |
} |
| 597 |
|
| 598 |
result = Tcl_GetCommandInfo(interp, "console", &cmdInfo); |
| 599 |
if (result == 0) { |
| 600 |
return; |
| 601 |
} |
| 602 |
info = (ConsoleInfo *) cmdInfo.clientData; |
| 603 |
|
| 604 |
Tcl_DStringInit(&output); |
| 605 |
Tcl_DStringAppend(&output, buffer, size); |
| 606 |
|
| 607 |
Tcl_DStringInit(&command); |
| 608 |
Tcl_DStringAppend(&command, cmd, strlen(cmd)); |
| 609 |
Tcl_DStringAppendElement(&command, output.string); |
| 610 |
|
| 611 |
consoleInterp = info->consoleInterp; |
| 612 |
Tcl_Preserve((ClientData) consoleInterp); |
| 613 |
Tcl_Eval(consoleInterp, command.string); |
| 614 |
Tcl_Release((ClientData) consoleInterp); |
| 615 |
|
| 616 |
Tcl_DStringFree(&command); |
| 617 |
Tcl_DStringFree(&output); |
| 618 |
} |