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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
|
/*
* tkConsole.c --
*
* This file implements a Tcl console for systems that may not otherwise
* have access to a console. It uses the Text widget and provides special
* access via a console command.
*
* Copyright © 1995-1996 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "tkInt.h"
/*
* Each console is associated with an instance of the ConsoleInfo struct.
* It keeps track of what interp holds the Tk application that displays
* the console, and what interp is controlled by the interactions in that
* console. A refCount permits the struct to be shared as instance data
* by commands and by channels.
*/
typedef struct ConsoleInfo {
Tcl_Interp *consoleInterp; /* Interpreter displaying the console. */
Tcl_Interp *interp; /* Interpreter controlled by console. */
size_t refCount;
} ConsoleInfo;
/*
* Each console channel holds an instance of the ChannelData struct as
* its instance data. It contains ConsoleInfo, so the channel can work
* with the appropriate console window, and a type value to distinguish
* the stdout channel from the stderr channel.
*/
typedef struct ChannelData {
ConsoleInfo *info;
int type; /* TCL_STDOUT or TCL_STDERR */
} ChannelData;
/*
* Prototypes for local procedures defined in this file:
*/
static int ConsoleClose(void *instanceData, Tcl_Interp *interp);
static int Console2Close(void *instanceData, Tcl_Interp *interp, int flags);
static void ConsoleDeleteProc(void *clientData);
static void ConsoleEventProc(void *clientData, XEvent *eventPtr);
static int ConsoleHandle(void *instanceData, int direction,
void **handlePtr);
static int ConsoleInput(void *instanceData, char *buf, int toRead,
int *errorCode);
static Tcl_ObjCmdProc ConsoleObjCmd;
static int ConsoleOutput(void *instanceData, const char *buf,
int toWrite, int *errorCode);
static void ConsoleWatch(void *instanceData, int mask);
static void DeleteConsoleInterp(void *clientData);
static void InterpDeleteProc(void *clientData, Tcl_Interp *interp);
static Tcl_ObjCmdProc InterpreterObjCmd;
/*
* This structure describes the channel type structure for file based IO:
*/
static const Tcl_ChannelType consoleChannelType = {
"console", /* Type name. */
TCL_CHANNEL_VERSION_5, /* v5 channel */
(Tcl_DriverCloseProc *)ConsoleClose, /* Close proc. */
ConsoleInput, /* Input proc. */
ConsoleOutput, /* Output proc. */
NULL, /* Seek proc. */
NULL, /* Set option proc. */
NULL, /* Get option proc. */
ConsoleWatch, /* Watch for events on console. */
ConsoleHandle, /* Get a handle from the device. */
Console2Close, /* close2proc. */
NULL, /* Always non-blocking.*/
NULL, /* flush proc. */
NULL, /* handler proc. */
NULL, /* wide seek proc */
NULL, /* thread action proc */
NULL
};
#ifdef _WIN32
#include <windows.h>
/*
*----------------------------------------------------------------------
*
* ShouldUseConsoleChannel
*
* Check to see if console window should be used for a given standard
* channel.
*
* Results:
* None.
*
* Side effects:
* Creates the console channel and installs it as the standard channels.
*
*----------------------------------------------------------------------
*/
static int
ShouldUseConsoleChannel(
int type)
{
DWORD handleId; /* Standard handle to retrieve. */
DCB dcb;
DWORD consoleParams;
DWORD fileType;
HANDLE handle;
switch (type) {
case TCL_STDIN:
handleId = STD_INPUT_HANDLE;
break;
case TCL_STDOUT:
handleId = STD_OUTPUT_HANDLE;
break;
case TCL_STDERR:
handleId = STD_ERROR_HANDLE;
break;
default:
return 0;
break;
}
handle = GetStdHandle(handleId);
/*
* Note that we need to check for 0 because Windows will return 0 if this
* is not a console mode application, even though this is not a valid
* handle.
*/
if ((handle == INVALID_HANDLE_VALUE) || (handle == 0)) {
return 1;
}
/*
* Win2K BUG: GetStdHandle(STD_OUTPUT_HANDLE) can return what appears to
* be a valid handle. See TclpGetDefaultStdChannel() for this change
* implemented. We didn't change it here because GetFileType() [below]
* will catch this with FILE_TYPE_UNKNOWN and appropriately return a value
* of 1, anyways.
*
* char dummyBuff[1];
* DWORD dummyWritten;
*
* if ((type == TCL_STDOUT)
* && !WriteFile(handle, dummyBuff, 0, &dummyWritten, NULL)) {
* return 1;
* }
*/
fileType = GetFileType(handle);
/*
* If the file is a character device, we need to try to figure out whether
* it is a serial port, a console, or something else. We test for the
* console case first because this is more common.
*/
if (fileType == FILE_TYPE_CHAR) {
dcb.DCBlength = sizeof(DCB);
if (!GetConsoleMode(handle, &consoleParams) &&
!GetCommState(handle, &dcb)) {
/*
* Don't use a CHAR type channel for stdio, otherwise Tk runs into
* trouble with the MS DevStudio debugger.
*/
return 1;
}
} else if (fileType == FILE_TYPE_UNKNOWN) {
return 1;
} else if (Tcl_GetStdChannel(type) == NULL) {
return 1;
}
return 0;
}
#else
/*
* Mac should always use a console channel, Unix should if it's trying to
*/
#define ShouldUseConsoleChannel(chan) (1)
#endif
/*
*----------------------------------------------------------------------
*
* Tk_InitConsoleChannels --
*
* Create the console channels and install them as the standard channels.
* All I/O will be discarded until Tk_CreateConsoleWindow is called to
* attach the console to a text widget.
*
* Results:
* None.
*
* Side effects:
* Creates the console channel and installs it as the standard channels.
*
*----------------------------------------------------------------------
*/
void
Tk_InitConsoleChannels(
Tcl_Interp *interp)
{
static Tcl_ThreadDataKey consoleInitKey;
int *consoleInitPtr, doIn, doOut, doErr;
ConsoleInfo *info;
Tcl_Channel consoleChannel;
/*
* Ensure that we are getting a compatible version of Tcl.
*/
if (Tcl_InitStubs(interp, "8.7-", 0) == NULL) {
return;
}
consoleInitPtr = (int *)Tcl_GetThreadData(&consoleInitKey, (int) sizeof(int));
if (*consoleInitPtr) {
/*
* We've already initialized console channels in this thread.
*/
return;
}
*consoleInitPtr = 1;
doIn = ShouldUseConsoleChannel(TCL_STDIN);
doOut = ShouldUseConsoleChannel(TCL_STDOUT);
doErr = ShouldUseConsoleChannel(TCL_STDERR);
if (!(doIn || doOut || doErr)) {
/*
* No std channels should be tied to the console; thus, no need to
* create the console.
*/
return;
}
/*
* At least one std channel wants to be tied to the console, so create the
* interp for it to live in.
*/
info = (ConsoleInfo *) ckalloc(sizeof(ConsoleInfo));
info->consoleInterp = NULL;
info->interp = NULL;
info->refCount = 0;
if (doIn) {
ChannelData *data = (ChannelData *)ckalloc(sizeof(ChannelData));
data->info = info;
data->info->refCount++;
data->type = TCL_STDIN;
consoleChannel = Tcl_CreateChannel(&consoleChannelType, "console0",
data, TCL_READABLE);
if (consoleChannel != NULL) {
Tcl_SetChannelOption(NULL, consoleChannel, "-translation", "lf");
Tcl_SetChannelOption(NULL, consoleChannel, "-buffering", "none");
Tcl_SetChannelOption(NULL, consoleChannel, "-encoding", "utf-8");
}
Tcl_SetStdChannel(consoleChannel, TCL_STDIN);
Tcl_RegisterChannel(NULL, consoleChannel);
}
if (doOut) {
ChannelData *data = (ChannelData *)ckalloc(sizeof(ChannelData));
data->info = info;
data->info->refCount++;
data->type = TCL_STDOUT;
consoleChannel = Tcl_CreateChannel(&consoleChannelType, "console1",
data, TCL_WRITABLE);
if (consoleChannel != NULL) {
Tcl_SetChannelOption(NULL, consoleChannel, "-translation", "lf");
Tcl_SetChannelOption(NULL, consoleChannel, "-buffering", "none");
Tcl_SetChannelOption(NULL, consoleChannel, "-encoding", "utf-8");
}
Tcl_SetStdChannel(consoleChannel, TCL_STDOUT);
Tcl_RegisterChannel(NULL, consoleChannel);
}
if (doErr) {
ChannelData *data = (ChannelData *)ckalloc(sizeof(ChannelData));
data->info = info;
data->info->refCount++;
data->type = TCL_STDERR;
consoleChannel = Tcl_CreateChannel(&consoleChannelType, "console2",
data, TCL_WRITABLE);
if (consoleChannel != NULL) {
Tcl_SetChannelOption(NULL, consoleChannel, "-translation", "lf");
Tcl_SetChannelOption(NULL, consoleChannel, "-buffering", "none");
Tcl_SetChannelOption(NULL, consoleChannel, "-encoding", "utf-8");
}
Tcl_SetStdChannel(consoleChannel, TCL_STDERR);
Tcl_RegisterChannel(NULL, consoleChannel);
}
}
/*
*----------------------------------------------------------------------
*
* Tk_CreateConsoleWindow --
*
* Initialize the console. This code actually creates a new application
* and associated interpreter. This effectively hides the implementation
* from the main application.
*
* Results:
* None.
*
* Side effects:
* A new console it created.
*
*----------------------------------------------------------------------
*/
int
Tk_CreateConsoleWindow(
Tcl_Interp *interp) /* Interpreter to use for prompting. */
{
Tcl_Channel chan;
ConsoleInfo *info;
Tk_Window mainWindow;
Tcl_Command token;
int result = TCL_OK;
int haveConsoleChannel = 1;
/* Init an interp with Tcl and Tk */
Tcl_Interp *consoleInterp = Tcl_CreateInterp();
if (Tcl_Init(consoleInterp) != TCL_OK) {
Tcl_Obj *result_obj = Tcl_GetObjResult(consoleInterp);
Tcl_SetObjResult(interp, result_obj);
goto error;
}
if (Tk_Init(consoleInterp) != TCL_OK) {
Tcl_Obj *result_obj = Tcl_GetObjResult(consoleInterp);
Tcl_SetObjResult(interp, result_obj);
goto error;
}
/*
* Fetch the instance data from whatever std channel is a
* console channel. If none, create fresh instance data.
*/
if (Tcl_GetChannelType(chan = Tcl_GetStdChannel(TCL_STDIN))
== &consoleChannelType) {
} else if (Tcl_GetChannelType(chan = Tcl_GetStdChannel(TCL_STDOUT))
== &consoleChannelType) {
} else if (Tcl_GetChannelType(chan = Tcl_GetStdChannel(TCL_STDERR))
== &consoleChannelType) {
} else {
haveConsoleChannel = 0;
}
if (haveConsoleChannel) {
ChannelData *data = (ChannelData *) Tcl_GetChannelInstanceData(chan);
info = data->info;
if (info->consoleInterp) {
/*
* New ConsoleInfo for a new console window.
*/
info = (ConsoleInfo *)ckalloc(sizeof(ConsoleInfo));
info->refCount = 0;
/*
* Update any console channels to make use of the new console.
*/
if (Tcl_GetChannelType(chan = Tcl_GetStdChannel(TCL_STDIN))
== &consoleChannelType) {
data = (ChannelData *) Tcl_GetChannelInstanceData(chan);
data->info->refCount--;
data->info = info;
data->info->refCount++;
}
if (Tcl_GetChannelType(chan = Tcl_GetStdChannel(TCL_STDOUT))
== &consoleChannelType) {
data = (ChannelData *) Tcl_GetChannelInstanceData(chan);
data->info->refCount--;
data->info = info;
data->info->refCount++;
}
if (Tcl_GetChannelType(chan = Tcl_GetStdChannel(TCL_STDERR))
== &consoleChannelType) {
data = (ChannelData *) Tcl_GetChannelInstanceData(chan);
data->info->refCount--;
data->info = info;
data->info->refCount++;
}
}
} else {
info = (ConsoleInfo *)ckalloc(sizeof(ConsoleInfo));
info->refCount = 0;
}
info->consoleInterp = consoleInterp;
info->interp = interp;
Tcl_CallWhenDeleted(consoleInterp, InterpDeleteProc, info);
info->refCount++;
Tcl_CreateThreadExitHandler(DeleteConsoleInterp, consoleInterp);
/*
* Add console commands to the interp
*/
token = Tcl_CreateObjCommand(interp, "console", ConsoleObjCmd, info,
ConsoleDeleteProc);
info->refCount++;
/*
* We don't have to count the ref held by the [consoleinterp] command
* in the consoleInterp. The ref held by the consoleInterp delete
* handler takes care of us.
*/
Tcl_CreateObjCommand(consoleInterp, "consoleinterp", InterpreterObjCmd,
info, NULL);
mainWindow = Tk_MainWindow(interp);
if (mainWindow) {
Tk_CreateEventHandler(mainWindow, StructureNotifyMask,
ConsoleEventProc, info);
info->refCount++;
}
Tcl_Preserve(consoleInterp);
result = Tcl_EvalEx(consoleInterp, "source $tk_library/console.tcl",
TCL_INDEX_NONE, TCL_EVAL_GLOBAL);
if (result == TCL_ERROR) {
Tcl_SetReturnOptions(interp,
Tcl_GetReturnOptions(consoleInterp, result));
Tcl_SetObjResult(interp, Tcl_GetObjResult(consoleInterp));
}
Tcl_Release(consoleInterp);
if (result == TCL_ERROR) {
Tcl_DeleteCommandFromToken(interp, token);
mainWindow = Tk_MainWindow(interp);
if (mainWindow) {
Tk_DeleteEventHandler(mainWindow, StructureNotifyMask,
ConsoleEventProc, info);
if (info->refCount-- <= 1) {
ckfree(info);
}
}
goto error;
}
return TCL_OK;
error:
Tcl_AddErrorInfo(interp, "\n (creating console window)");
if (!Tcl_InterpDeleted(consoleInterp)) {
Tcl_DeleteInterp(consoleInterp);
}
return TCL_ERROR;
}
/*
*----------------------------------------------------------------------
*
* ConsoleOutput--
*
* Writes the given output on the IO channel. Returns count of how many
* characters were actually written, and an error indication.
*
* Results:
* A count of how many characters were written is returned and an error
* indication is returned in an output argument.
*
* Side effects:
* Writes output on the actual channel.
*
*----------------------------------------------------------------------
*/
static int
ConsoleOutput(
void *instanceData, /* Indicates which device to use. */
const char *buf, /* The data buffer. */
int toWrite, /* How many bytes to write? */
int *errorCode) /* Where to store error code. */
{
ChannelData *data = (ChannelData *)instanceData;
ConsoleInfo *info = data->info;
*errorCode = 0;
Tcl_SetErrno(0);
if (info) {
Tcl_Interp *consoleInterp = info->consoleInterp;
if (consoleInterp && !Tcl_InterpDeleted(consoleInterp)) {
Tcl_DString ds;
Tcl_Encoding utf8 = Tcl_GetEncoding(NULL, "utf-8");
/*
* Not checking for utf8 == NULL. Did not check for TCL_ERROR
* from Tcl_SetChannelOption() in Tk_InitConsoleChannels() either.
* Assumption is utf-8 Tcl_Encoding is reliably present.
*/
const char *bytes = Tcl_ExternalToUtfDString(utf8, buf, toWrite, &ds);
Tcl_Size numBytes = Tcl_DStringLength(&ds);
Tcl_Obj *cmd = Tcl_NewStringObj("tk::ConsoleOutput", TCL_INDEX_NONE);
Tcl_FreeEncoding(utf8);
if (data->type == TCL_STDERR) {
Tcl_ListObjAppendElement(NULL, cmd,
Tcl_NewStringObj("stderr", TCL_INDEX_NONE));
} else {
Tcl_ListObjAppendElement(NULL, cmd,
Tcl_NewStringObj("stdout", TCL_INDEX_NONE));
}
Tcl_ListObjAppendElement(NULL, cmd,
Tcl_NewStringObj(bytes, numBytes));
Tcl_DStringFree(&ds);
Tcl_IncrRefCount(cmd);
Tcl_EvalObjEx(consoleInterp, cmd, TCL_EVAL_GLOBAL);
Tcl_DecrRefCount(cmd);
}
}
return toWrite;
}
/*
*----------------------------------------------------------------------
*
* ConsoleInput --
*
* Read input from the console. Not currently implemented.
*
* Results:
* Always returns EOF.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
ConsoleInput(
TCL_UNUSED(void *),
TCL_UNUSED(char *), /* Where to store data read. */
TCL_UNUSED(int), /* How much space is available in the
* buffer? */
TCL_UNUSED(int *)) /* Where to store error code. */
{
return 0; /* Always return EOF. */
}
/*
*----------------------------------------------------------------------
*
* ConsoleClose/Console2Close --
*
* Closes the IO channel.
*
* Results:
* Always returns 0 (success).
*
* Side effects:
* Frees the dummy file associated with the channel.
*
*----------------------------------------------------------------------
*/
static int
ConsoleClose(
void *instanceData,
TCL_UNUSED(Tcl_Interp *))
{
ChannelData *data = (ChannelData *)instanceData;
ConsoleInfo *info = data->info;
if (info) {
if (info->refCount-- <= 1) {
/*
* Assuming the Tcl_Interp * fields must already be NULL.
*/
ckfree(info);
}
}
ckfree(data);
return 0;
}
static int
Console2Close(
void *instanceData, /* Unused. */
Tcl_Interp *interp, /* Unused. */
int flags)
{
if ((flags&(TCL_CLOSE_READ|TCL_CLOSE_WRITE))==0) {
return ConsoleClose(instanceData, interp);
}
return EINVAL;
}
/*
*----------------------------------------------------------------------
*
* ConsoleWatch --
*
* Called by the notifier to set up the console device so that events
* will be noticed. Since there are no events on the console, this
* routine just returns without doing anything.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
ConsoleWatch(
TCL_UNUSED(void *), /* Device ID for the channel. */
TCL_UNUSED(int)) /* OR-ed combination of TCL_READABLE,
* TCL_WRITABLE and TCL_EXCEPTION, for the
* events we are interested in. */
{
}
/*
*----------------------------------------------------------------------
*
* ConsoleHandle --
*
* Invoked by the generic IO layer to get a handle from a channel.
* Because console channels are not devices, this function always fails.
*
* Results:
* Always returns TCL_ERROR.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
ConsoleHandle(
TCL_UNUSED(void *), /* Device ID for the channel. */
TCL_UNUSED(int), /* TCL_READABLE or TCL_WRITABLE to indicate
* which direction of the channel is being
* requested. */
TCL_UNUSED(void **)) /* Where to store handle */
{
return TCL_ERROR;
}
/*
*----------------------------------------------------------------------
*
* ConsoleObjCmd --
*
* The console command implements a Tcl interface to the various console
* options.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*----------------------------------------------------------------------
*/
static int
ConsoleObjCmd(
void *clientData, /* Access to the console interp */
Tcl_Interp *interp, /* Current interpreter */
int objc, /* Number of arguments */
Tcl_Obj *const objv[]) /* Argument objects */
{
int index, result;
static const char *const options[] = {
"eval", "hide", "show", "title", NULL};
enum option {CON_EVAL, CON_HIDE, CON_SHOW, CON_TITLE};
Tcl_Obj *cmd = NULL;
ConsoleInfo *info = (ConsoleInfo *)clientData;
Tcl_Interp *consoleInterp = info->consoleInterp;
if (objc < 2) {
Tcl_WrongNumArgs(interp, 1, objv, "option ?arg?");
return TCL_ERROR;
}
if (Tcl_GetIndexFromObjStruct(interp, objv[1], options,
sizeof(char *), "option", 0, &index) != TCL_OK) {
return TCL_ERROR;
}
switch ((enum option) index) {
case CON_EVAL:
if (objc != 3) {
Tcl_WrongNumArgs(interp, 2, objv, "script");
return TCL_ERROR;
}
cmd = objv[2];
break;
case CON_HIDE:
if (objc != 2) {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
return TCL_ERROR;
}
cmd = Tcl_NewStringObj("wm withdraw .", TCL_INDEX_NONE);
break;
case CON_SHOW:
if (objc != 2) {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
return TCL_ERROR;
}
cmd = Tcl_NewStringObj("wm deiconify .", TCL_INDEX_NONE);
break;
case CON_TITLE:
if (objc > 3) {
Tcl_WrongNumArgs(interp, 2, objv, "?title?");
return TCL_ERROR;
}
cmd = Tcl_NewStringObj("wm title .", TCL_INDEX_NONE);
if (objc == 3) {
Tcl_ListObjAppendElement(NULL, cmd, objv[2]);
}
break;
default:
CLANG_ASSERT(0);
}
Tcl_IncrRefCount(cmd);
if (consoleInterp && !Tcl_InterpDeleted(consoleInterp)) {
Tcl_Preserve(consoleInterp);
result = Tcl_EvalObjEx(consoleInterp, cmd, TCL_EVAL_GLOBAL);
Tcl_SetReturnOptions(interp,
Tcl_GetReturnOptions(consoleInterp, result));
Tcl_SetObjResult(interp, Tcl_GetObjResult(consoleInterp));
Tcl_Release(consoleInterp);
} else {
Tcl_SetObjResult(interp, Tcl_NewStringObj(
"no active console interp", TCL_INDEX_NONE));
Tcl_SetErrorCode(interp, "TK", "CONSOLE", "NONE", NULL);
result = TCL_ERROR;
}
Tcl_DecrRefCount(cmd);
return result;
}
/*
*----------------------------------------------------------------------
*
* InterpreterObjCmd --
*
* This command allows the console interp to communicate with the main
* interpreter.
*
* Results:
* A standard Tcl result.
*
*----------------------------------------------------------------------
*/
static int
InterpreterObjCmd(
void *clientData, /* */
Tcl_Interp *interp, /* Current interpreter */
int objc, /* Number of arguments */
Tcl_Obj *const objv[]) /* Argument objects */
{
int index, result = TCL_OK;
static const char *const options[] = {"eval", "record", NULL};
enum option {OTHER_EVAL, OTHER_RECORD};
ConsoleInfo *info = (ConsoleInfo *)clientData;
Tcl_Interp *otherInterp = info->interp;
if (objc < 2) {
Tcl_WrongNumArgs(interp, 1, objv, "option arg");
return TCL_ERROR;
}
if (Tcl_GetIndexFromObjStruct(interp, objv[1], options,
sizeof(char *), "option", 0, &index) != TCL_OK) {
return TCL_ERROR;
}
if (objc != 3) {
Tcl_WrongNumArgs(interp, 2, objv, "script");
return TCL_ERROR;
}
if ((otherInterp == NULL) || Tcl_InterpDeleted(otherInterp)) {
Tcl_SetObjResult(interp, Tcl_NewStringObj(
"no active parent interp", TCL_INDEX_NONE));
Tcl_SetErrorCode(interp, "TK", "CONSOLE", "NO_INTERP", NULL);
return TCL_ERROR;
}
Tcl_Preserve(otherInterp);
switch ((enum option) index) {
case OTHER_EVAL:
result = Tcl_EvalObjEx(otherInterp, objv[2], TCL_EVAL_GLOBAL);
/*
* TODO: Should exceptions be filtered here?
*/
Tcl_SetReturnOptions(interp,
Tcl_GetReturnOptions(otherInterp, result));
Tcl_SetObjResult(interp, Tcl_GetObjResult(otherInterp));
break;
case OTHER_RECORD:
Tcl_RecordAndEvalObj(otherInterp, objv[2], TCL_EVAL_GLOBAL);
/*
* By not setting result, we discard any exceptions or errors here and
* always return TCL_OK. All the caller wants is the interp result to
* display, whether that's result or error message.
*/
Tcl_SetObjResult(interp, Tcl_GetObjResult(otherInterp));
break;
}
Tcl_Release(otherInterp);
return result;
}
/*
*----------------------------------------------------------------------
*
* DeleteConsoleInterp --
*
* Thread exit handler to destroy a console interp when the thread it
* lives in gets torn down.
*
*----------------------------------------------------------------------
*/
static void
DeleteConsoleInterp(
void *clientData)
{
Tcl_Interp *interp = (Tcl_Interp *)clientData;
Tcl_DeleteInterp(interp);
}
/*
*----------------------------------------------------------------------
*
* InterpDeleteProc --
*
* React when the interp in which the console is displayed is deleted for
* any reason.
*
* Results:
* None.
*
* Side effects:
* A new console it created.
*
*----------------------------------------------------------------------
*/
static void
InterpDeleteProc(
void *clientData,
Tcl_Interp *interp)
{
ConsoleInfo *info = (ConsoleInfo *)clientData;
if (info->consoleInterp == interp) {
Tcl_DeleteThreadExitHandler(DeleteConsoleInterp, info->consoleInterp);
info->consoleInterp = NULL;
}
if (info->refCount-- <= 1) {
ckfree(info);
}
}
/*
*----------------------------------------------------------------------
*
* ConsoleDeleteProc --
*
* If the console command is deleted we destroy the console window and
* all associated data structures.
*
* Results:
* None.
*
* Side effects:
* A new console it created.
*
*----------------------------------------------------------------------
*/
static void
ConsoleDeleteProc(
void *clientData)
{
ConsoleInfo *info = (ConsoleInfo *)clientData;
if (info->consoleInterp) {
Tcl_DeleteInterp(info->consoleInterp);
}
if (info->refCount-- <= 1) {
ckfree(info);
}
}
/*
*----------------------------------------------------------------------
*
* ConsoleEventProc --
*
* This event function is registered on the main window of the child
* interpreter. If the user or a running script causes the main window to
* be destroyed, then we need to inform the console interpreter by
* invoking "::tk::ConsoleExit".
*
* Results:
* None.
*
* Side effects:
* Invokes the "::tk::ConsoleExit" command in the console interp.
*
*----------------------------------------------------------------------
*/
static void
ConsoleEventProc(
void *clientData,
XEvent *eventPtr)
{
if (eventPtr->type == DestroyNotify) {
ConsoleInfo *info = (ConsoleInfo *)clientData;
Tcl_Interp *consoleInterp = info->consoleInterp;
if (consoleInterp && !Tcl_InterpDeleted(consoleInterp)) {
Tcl_EvalEx(consoleInterp, "tk::ConsoleExit", TCL_INDEX_NONE, TCL_EVAL_GLOBAL);
}
if (info->refCount-- <= 1) {
ckfree(info);
}
}
}
/*
* Local Variables:
* mode: c
* c-basic-offset: 4
* fill-column: 78
* End:
*/
|