Artifact
e0ce9a1e419ae54df0241781ba91f9c6b6917cfa:
Attachment "c.c" to
ticket [3554250fff]
added by
stwo
2012-08-05 02:27:22.
0000: 23 69 6e 63 6c 75 64 65 20 3c 74 63 6c 2e 68 3e #include <tcl.h>
0010: 0a 23 64 65 66 69 6e 65 20 46 4e 20 22 74 65 73 .#define FN "tes
0020: 74 66 69 6c 65 22 0a 0a 2f 2a 20 54 68 69 73 20 tfile"../* This
0030: 73 63 72 69 70 74 20 77 69 6c 6c 20 77 6f 72 6b script will work
0040: 20 69 6e 20 61 6c 6c 20 54 63 6c 20 76 65 72 73 in all Tcl vers
0050: 69 6f 6e 73 2c 20 61 6e 64 20 74 68 65 20 66 69 ions, and the fi
0060: 6c 65 20 77 69 6c 6c 20 62 65 20 64 65 6c 65 74 le will be delet
0070: 65 64 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ed. */.#define S
0080: 43 52 49 50 54 20 22 61 66 74 65 72 20 31 30 30 CRIPT "after 100
0090: 30 20 7b 70 75 74 73 20 61 3b 63 6c 6f 73 65 20 0 {puts a;close
00a0: 61 3b 70 75 74 73 20 61 61 3b 73 65 74 20 74 20 a;puts aa;set t
00b0: 74 7d 3b 20 76 77 61 69 74 20 74 3b 20 61 66 74 t}; vwait t; aft
00c0: 65 72 20 31 30 30 30 20 7b 70 75 74 73 20 62 3b er 1000 {puts b;
00d0: 65 78 69 74 7d 3b 20 76 77 61 69 74 20 74 22 0a exit}; vwait t".
00e0: 0a 2f 2a 20 54 68 69 73 20 73 63 72 69 70 74 20 ./* This script
00f0: 77 69 6c 6c 20 77 6f 72 6b 20 69 6e 20 54 63 6c will work in Tcl
0100: 20 38 2e 35 2e 31 31 2c 20 61 6e 64 20 74 68 65 8.5.11, and the
0110: 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 64 65 file will be de
0120: 6c 65 74 65 64 2e 20 2a 2f 0a 2f 2a 20 54 68 69 leted. */./* Thi
0130: 73 20 73 63 72 69 70 74 20 77 69 6c 6c 20 63 61 s script will ca
0140: 75 73 65 20 61 20 73 65 67 66 61 75 6c 74 20 69 use a segfault i
0150: 6e 20 54 63 6c 20 38 2e 35 2e 31 32 20 61 6e 64 n Tcl 8.5.12 and
0160: 20 38 2e 36 2c 20 61 6e 64 20 74 68 65 20 66 69 8.6, and the fi
0170: 6c 65 20 77 6f 6e 27 74 20 62 65 20 64 65 6c 65 le won't be dele
0180: 74 65 64 2e 20 2a 2f 0a 2f 2a 23 64 65 66 69 6e ted. */./*#defin
0190: 65 20 53 43 52 49 50 54 20 22 61 66 74 65 72 20 e SCRIPT "after
01a0: 31 30 30 30 20 7b 70 75 74 73 20 62 3b 65 78 69 1000 {puts b;exi
01b0: 74 7d 3b 20 76 77 61 69 74 20 74 22 2a 2f 0a 0a t}; vwait t"*/..
01c0: 73 74 61 74 69 63 20 69 6e 74 20 58 43 6c 6f 73 static int XClos
01d0: 65 50 72 6f 63 20 28 43 6c 69 65 6e 74 44 61 74 eProc (ClientDat
01e0: 61 20 69 6e 73 74 61 6e 63 65 44 61 74 61 2c 20 a instanceData,
01f0: 54 63 6c 5f 49 6e 74 65 72 70 20 2a 69 6e 74 65 Tcl_Interp *inte
0200: 72 70 29 20 7b 0a 09 54 63 6c 5f 4f 62 6a 20 2a rp) {..Tcl_Obj *
0210: 6f 20 3d 20 54 63 6c 5f 4e 65 77 53 74 72 69 6e o = Tcl_NewStrin
0220: 67 4f 62 6a 28 46 4e 2c 20 2d 31 29 3b 0a 09 54 gObj(FN, -1);..T
0230: 63 6c 5f 49 6e 63 72 52 65 66 43 6f 75 6e 74 28 cl_IncrRefCount(
0240: 6f 29 3b 20 54 63 6c 5f 46 53 44 65 6c 65 74 65 o); Tcl_FSDelete
0250: 46 69 6c 65 28 6f 29 3b 20 54 63 6c 5f 44 65 63 File(o); Tcl_Dec
0260: 72 52 65 66 43 6f 75 6e 74 28 6f 29 3b 0a 09 72 rRefCount(o);..r
0270: 65 74 75 72 6e 20 30 3b 0a 7d 0a 73 74 61 74 69 eturn 0;.}.stati
0280: 63 20 69 6e 74 20 58 49 6e 70 75 74 50 72 6f 63 c int XInputProc
0290: 20 28 43 6c 69 65 6e 74 44 61 74 61 20 69 6e 73 (ClientData ins
02a0: 74 61 6e 63 65 44 61 74 61 2c 20 63 68 61 72 20 tanceData, char
02b0: 2a 62 75 66 2c 20 69 6e 74 20 74 6f 52 65 61 64 *buf, int toRead
02c0: 2c 20 69 6e 74 20 2a 65 72 72 6f 72 43 6f 64 65 , int *errorCode
02d0: 29 20 7b 20 72 65 74 75 72 6e 20 30 3b 20 7d 0a ) { return 0; }.
02e0: 73 74 61 74 69 63 20 69 6e 74 20 58 4f 75 74 70 static int XOutp
02f0: 75 74 50 72 6f 63 20 28 43 6c 69 65 6e 74 44 61 utProc (ClientDa
0300: 74 61 20 69 6e 73 74 61 6e 63 65 44 61 74 61 2c ta instanceData,
0310: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 62 75 66 const char *buf
0320: 2c 20 69 6e 74 20 74 6f 57 72 69 74 65 2c 20 69 , int toWrite, i
0330: 6e 74 20 2a 65 72 72 6f 72 43 6f 64 65 29 20 7b nt *errorCode) {
0340: 20 72 65 74 75 72 6e 20 74 6f 57 72 69 74 65 3b return toWrite;
0350: 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 58 }.static void X
0360: 57 61 74 63 68 50 72 6f 63 20 28 43 6c 69 65 6e WatchProc (Clien
0370: 74 44 61 74 61 20 69 6e 73 74 61 6e 63 65 44 61 tData instanceDa
0380: 74 61 2c 20 69 6e 74 20 6d 61 73 6b 29 20 7b 7d ta, int mask) {}
0390: 0a 73 74 61 74 69 63 20 69 6e 74 20 58 47 65 74 .static int XGet
03a0: 48 61 6e 64 6c 65 50 72 6f 63 20 28 43 6c 69 65 HandleProc (Clie
03b0: 6e 74 44 61 74 61 20 69 6e 73 74 61 6e 63 65 44 ntData instanceD
03c0: 61 74 61 2c 20 69 6e 74 20 64 69 72 65 63 74 69 ata, int directi
03d0: 6f 6e 2c 20 43 6c 69 65 6e 74 44 61 74 61 20 2a on, ClientData *
03e0: 68 61 6e 64 6c 65 50 74 72 29 20 7b 20 72 65 74 handlePtr) { ret
03f0: 75 72 6e 20 30 3b 20 7d 0a 73 74 61 74 69 63 20 urn 0; }.static
0400: 54 63 6c 5f 43 68 61 6e 6e 65 6c 54 79 70 65 20 Tcl_ChannelType
0410: 78 43 68 61 6e 6e 65 6c 54 79 70 65 20 3d 20 7b xChannelType = {
0420: 0a 09 28 63 68 61 72 20 2a 29 20 22 78 22 2c 20 ..(char *) "x",
0430: 54 43 4c 5f 43 48 41 4e 4e 45 4c 5f 56 45 52 53 TCL_CHANNEL_VERS
0440: 49 4f 4e 5f 32 2c 20 58 43 6c 6f 73 65 50 72 6f ION_2, XClosePro
0450: 63 2c 20 58 49 6e 70 75 74 50 72 6f 63 2c 20 58 c, XInputProc, X
0460: 4f 75 74 70 75 74 50 72 6f 63 2c 0a 09 4e 55 4c OutputProc,..NUL
0470: 4c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 58 L, NULL, NULL, X
0480: 57 61 74 63 68 50 72 6f 63 2c 20 58 47 65 74 48 WatchProc, XGetH
0490: 61 6e 64 6c 65 50 72 6f 63 2c 20 4e 55 4c 4c 2c andleProc, NULL,
04a0: 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 4e 55 4c NULL, NULL, NUL
04b0: 4c 0a 7d 3b 0a 69 6e 74 20 6d 61 69 6e 20 28 69 L.};.int main (i
04c0: 6e 74 20 61 72 67 63 2c 20 63 68 61 72 20 2a 61 nt argc, char *a
04d0: 72 67 76 5b 5d 29 20 7b 0a 09 54 63 6c 5f 49 6e rgv[]) {..Tcl_In
04e0: 74 65 72 70 20 2a 69 6e 3b 0a 09 54 63 6c 5f 43 terp *in;..Tcl_C
04f0: 68 61 6e 6e 65 6c 20 63 68 61 6e 3b 0a 09 54 63 hannel chan;..Tc
0500: 6c 5f 46 69 6e 64 45 78 65 63 75 74 61 62 6c 65 l_FindExecutable
0510: 28 61 72 67 76 5b 30 5d 29 3b 0a 09 54 63 6c 5f (argv[0]);..Tcl_
0520: 49 6e 69 74 28 69 6e 20 3d 20 54 63 6c 5f 43 72 Init(in = Tcl_Cr
0530: 65 61 74 65 49 6e 74 65 72 70 28 29 29 3b 0a 09 eateInterp());..
0540: 63 68 61 6e 20 3d 20 54 63 6c 5f 43 72 65 61 74 chan = Tcl_Creat
0550: 65 43 68 61 6e 6e 65 6c 28 26 78 43 68 61 6e 6e eChannel(&xChann
0560: 65 6c 54 79 70 65 2c 20 22 61 22 2c 20 4e 55 4c elType, "a", NUL
0570: 4c 2c 20 54 43 4c 5f 52 45 41 44 41 42 4c 45 7c L, TCL_READABLE|
0580: 54 43 4c 5f 57 52 49 54 41 42 4c 45 29 3b 0a 09 TCL_WRITABLE);..
0590: 54 63 6c 5f 52 65 67 69 73 74 65 72 43 68 61 6e Tcl_RegisterChan
05a0: 6e 65 6c 28 69 6e 2c 20 63 68 61 6e 29 3b 0a 09 nel(in, chan);..
05b0: 54 63 6c 5f 45 76 61 6c 28 69 6e 2c 20 53 43 52 Tcl_Eval(in, SCR
05c0: 49 50 54 29 3b 0a 09 72 65 74 75 72 6e 20 30 3b IPT);..return 0;
05d0: 0a 7d 0a .}.