Artifact
f815ee9a9fe50724cd243dced252ff6dd4fdf109:
Attachment "tout5.c" to
ticket [3307281fff]
added by
anonymous
2011-05-25 12:14:34.
0000: 23 69 6e 63 6c 75 64 65 20 3c 74 63 6c 2e 68 3e #include <tcl.h>
0010: 0d 0a 0d 0a 63 68 61 72 20 68 61 72 64 5f 63 6f ....char hard_co
0020: 64 65 64 5f 73 63 72 69 70 74 20 5b 5d 20 3d 20 ded_script [] =
0030: 22 5c 0d 0a 70 75 74 73 20 5c 22 69 6e 69 74 69 "\..puts \"initi
0040: 61 6c 69 7a 69 6e 67 5c 22 5c 6e 5c 0d 0a 5c 6e alizing\"\n\..\n
0050: 5c 0d 0a 70 72 6f 63 20 64 6f 69 74 5f 6f 6b 20 \..proc doit_ok
0060: 7b 7d 20 7b 5c 6e 5c 0d 0a 20 20 70 75 74 73 20 {} {\n\.. puts
0070: 73 74 64 6f 75 74 20 5c 22 70 72 6f 63 20 64 6f stdout \"proc do
0080: 69 74 5f 6f 6b 20 63 61 6c 6c 65 64 20 28 74 68 it_ok called (th
0090: 69 73 20 77 69 6c 6c 20 67 65 74 20 72 65 64 69 is will get redi
00a0: 72 65 63 74 65 64 29 2e 5c 22 5c 6e 5c 0d 0a 7d rected).\"\n\..}
00b0: 5c 6e 5c 0d 0a 70 72 6f 63 20 64 6f 69 74 5f 6e \n\..proc doit_n
00c0: 6f 74 6f 6b 20 7b 7d 20 7b 5c 6e 5c 0d 0a 20 20 otok {} {\n\..
00d0: 70 75 74 73 20 5c 22 70 72 6f 63 20 64 6f 69 74 puts \"proc doit
00e0: 5f 6e 6f 74 6f 6b 20 63 61 6c 6c 65 64 20 28 74 _notok called (t
00f0: 68 69 73 20 77 6f 6e 74 20 67 65 74 20 72 65 64 his wont get red
0100: 69 72 65 63 74 65 64 29 2e 5c 22 5c 6e 5c 0d 0a irected).\"\n\..
0110: 7d 5c 6e 22 3b 0d 0a 0d 0a 69 6e 74 20 54 63 6c }\n";....int Tcl
0120: 5f 41 70 70 49 6e 69 74 28 54 63 6c 5f 49 6e 74 _AppInit(Tcl_Int
0130: 65 72 70 2a 20 69 6e 74 65 72 70 29 0d 0a 7b 0d erp* interp)..{.
0140: 0a 20 20 54 63 6c 5f 43 68 61 6e 6e 65 6c 20 6f . Tcl_Channel o
0150: 6c 64 5f 73 74 64 6f 75 74 20 3d 20 54 63 6c 5f ld_stdout = Tcl_
0160: 47 65 74 53 74 64 43 68 61 6e 6e 65 6c 28 54 43 GetStdChannel(TC
0170: 4c 5f 53 54 44 4f 55 54 29 3b 0d 0a 20 20 54 63 L_STDOUT);.. Tc
0180: 6c 5f 45 76 61 6c 28 69 6e 74 65 72 70 2c 20 68 l_Eval(interp, h
0190: 61 72 64 5f 63 6f 64 65 64 5f 73 63 72 69 70 74 ard_coded_script
01a0: 29 3b 0d 0a 0d 0a 20 20 54 63 6c 5f 43 68 61 6e );.... Tcl_Chan
01b0: 6e 65 6c 20 63 68 20 3d 20 54 63 6c 5f 4f 70 65 nel ch = Tcl_Ope
01c0: 6e 46 69 6c 65 43 68 61 6e 6e 65 6c 28 69 6e 74 nFileChannel(int
01d0: 65 72 70 2c 20 22 74 6f 75 74 64 65 6d 6f 2e 74 erp, "toutdemo.t
01e0: 78 74 22 2c 20 22 77 22 2c 20 30 36 34 34 29 3b xt", "w", 0644);
01f0: 0d 0a 20 20 54 63 6c 5f 52 65 67 69 73 74 65 72 .. Tcl_Register
0200: 43 68 61 6e 6e 65 6c 28 69 6e 74 65 72 70 2c 20 Channel(interp,
0210: 63 68 29 3b 0d 0a 0d 0a 20 20 54 63 6c 5f 53 65 ch);.... Tcl_Se
0220: 74 53 74 64 43 68 61 6e 6e 65 6c 28 63 68 2c 20 tStdChannel(ch,
0230: 54 43 4c 5f 53 54 44 4f 55 54 29 3b 0d 0a 0d 0a TCL_STDOUT);....
0240: 20 20 2f 2f 20 74 68 69 73 20 6f 75 74 70 75 74 // this output
0250: 20 67 65 74 73 20 72 65 64 69 72 65 63 74 65 64 gets redirected
0260: 2e 0d 0a 20 20 2f 2f 20 74 68 69 73 20 68 61 73 ... // this has
0270: 20 27 73 74 64 6f 75 74 27 20 6c 69 73 74 65 64 'stdout' listed
0280: 20 61 73 20 66 69 6c 65 20 68 61 6e 64 6c 65 2e as file handle.
0290: 0d 0a 20 20 54 63 6c 5f 45 76 61 6c 28 69 6e 74 .. Tcl_Eval(int
02a0: 65 72 70 2c 20 22 64 6f 69 74 5f 6f 6b 22 29 3b erp, "doit_ok");
02b0: 0d 0a 20 20 2f 2f 20 74 68 69 73 20 6f 75 74 70 .. // this outp
02c0: 75 74 20 73 68 6f 75 6c 64 20 61 6c 73 6f 20 67 ut should also g
02d0: 65 74 20 72 65 64 69 72 65 63 74 65 64 2c 20 62 et redirected, b
02e0: 75 74 20 69 74 20 64 6f 65 73 6e 27 74 2e 0d 0a ut it doesn't...
02f0: 20 20 2f 2f 20 74 68 69 73 20 68 61 73 20 70 75 // this has pu
0300: 74 73 20 77 69 74 68 6f 75 74 20 27 73 74 64 6f ts without 'stdo
0310: 75 74 27 20 61 73 20 66 69 6c 65 20 68 61 6e 64 ut' as file hand
0320: 6c 65 0d 0a 20 20 54 63 6c 5f 45 76 61 6c 28 69 le.. Tcl_Eval(i
0330: 6e 74 65 72 70 2c 20 22 64 6f 69 74 5f 6e 6f 74 nterp, "doit_not
0340: 6f 6b 22 29 3b 0d 0a 0d 0a 20 20 54 63 6c 5f 53 ok");.... Tcl_S
0350: 65 74 53 74 64 43 68 61 6e 6e 65 6c 28 6f 6c 64 etStdChannel(old
0360: 5f 73 74 64 6f 75 74 2c 20 54 43 4c 5f 53 54 44 _stdout, TCL_STD
0370: 4f 55 54 29 3b 0d 0a 20 20 54 63 6c 5f 45 76 61 OUT);.. Tcl_Eva
0380: 6c 28 69 6e 74 65 72 70 2c 20 22 70 75 74 73 20 l(interp, "puts
0390: 7b 6e 6f 77 20 65 78 69 74 20 74 68 69 73 20 75 {now exit this u
03a0: 73 69 6e 67 20 5e 44 20 61 6e 64 20 63 61 74 20 sing ^D and cat
03b0: 74 6f 75 74 64 65 6d 6f 2e 74 78 74 2e 7d 22 29 toutdemo.txt.}")
03c0: 3b 0d 0a 20 20 72 65 74 75 72 6e 20 54 43 4c 5f ;.. return TCL_
03d0: 4f 4b 3b 0d 0a 7d 0d 0a 0d 0a 69 6e 74 20 6d 61 OK;..}....int ma
03e0: 69 6e 28 69 6e 74 20 61 72 67 63 2c 20 63 68 61 in(int argc, cha
03f0: 72 2a 20 61 72 67 76 5b 5d 29 0d 0a 7b 0d 0a 20 r* argv[])..{..
0400: 20 54 63 6c 5f 4d 61 69 6e 28 61 72 67 63 2c 20 Tcl_Main(argc,
0410: 61 72 67 76 2c 20 54 63 6c 5f 41 70 70 49 6e 69 argv, Tcl_AppIni
0420: 74 29 3b 0d 0a 20 20 72 65 74 75 72 6e 20 30 3b t);.. return 0;
0430: 0d 0a 7d 0d 0a ..}..