Artifact 95be374a42e3cc1acbd9552a4821ac82532295d2:
- File
kitdll/buildsrc/kitdll-0.0/rechan.c
— part of check-in
[b11b735302]
at
2010-09-29 23:15:37
on branch trunk
— Created GNU autoconf script for KitDLL
Made KitDLL build a shared object
Added test driver (user: rkeene, size: 7711) [annotate] [blame] [check-ins using]
- File kitsh/buildsrc/kitsh-0.0/rechan.c — part of check-in [9315fecb01] at 2010-09-26 04:37:13 on branch trunk — Added kitsh code (user: rkeene, size: 7711) [annotate] [blame] [check-ins using]
0000: 2f 2a 20 57 72 69 74 74 65 6e 20 62 79 20 4d 61 /* Written by Ma 0010: 74 74 20 4e 65 77 6d 61 6e 20 61 6e 64 20 4a 65 tt Newman and Je 0020: 61 6e 2d 43 6c 61 75 64 65 20 57 69 70 70 6c 65 an-Claude Wipple 0030: 72 2c 20 61 73 20 70 61 72 74 20 6f 66 20 54 63 r, as part of Tc 0040: 6c 6b 69 74 2e 0a 20 2a 20 4d 61 72 63 68 20 32 lkit.. * March 2 0050: 30 30 33 20 2d 20 70 6c 61 63 65 64 20 69 6e 20 003 - placed in 0060: 74 68 65 20 70 75 62 6c 69 63 20 64 6f 6d 61 69 the public domai 0070: 6e 20 62 79 20 74 68 65 20 61 75 74 68 6f 72 73 n by the authors 0080: 2e 0a 20 2a 0a 20 2a 20 52 65 66 6c 65 63 74 69 .. *. * Reflecti 0090: 6e 67 20 63 68 61 6e 6e 65 6c 20 69 6e 74 65 72 ng channel inter 00a0: 66 61 63 65 0a 20 2a 2f 0a 0a 23 69 6e 63 6c 75 face. */..#inclu 00b0: 64 65 20 3c 74 63 6c 2e 68 3e 0a 0a 23 69 66 6e de <tcl.h>..#ifn 00c0: 64 65 66 20 54 43 4c 5f 44 45 43 4c 41 52 45 5f def TCL_DECLARE_ 00d0: 4d 55 54 45 58 0a 23 64 65 66 69 6e 65 20 54 43 MUTEX.#define TC 00e0: 4c 5f 44 45 43 4c 41 52 45 5f 4d 55 54 45 58 28 L_DECLARE_MUTEX( 00f0: 76 29 0a 23 64 65 66 69 6e 65 20 54 63 6c 5f 4d v).#define Tcl_M 0100: 75 74 65 78 4c 6f 63 6b 28 76 29 0a 23 64 65 66 utexLock(v).#def 0110: 69 6e 65 20 54 63 6c 5f 4d 75 74 65 78 55 6e 6c ine Tcl_MutexUnl 0120: 6f 63 6b 28 76 29 0a 23 65 6e 64 69 66 0a 0a 20 ock(v).#endif.. 0130: 20 73 74 61 74 69 63 20 69 6e 74 20 6d 6b 43 68 static int mkCh 0140: 61 6e 53 65 71 20 3d 20 30 3b 0a 20 20 54 43 4c anSeq = 0;. TCL 0150: 5f 44 45 43 4c 41 52 45 5f 4d 55 54 45 58 28 72 _DECLARE_MUTEX(r 0160: 65 63 68 61 6e 4d 75 74 65 78 29 0a 0a 2f 2a 20 echanMutex)../* 0170: 55 6e 63 6f 6d 6d 65 6e 74 20 66 6f 72 20 4c 69 Uncomment for Li 0180: 6e 75 78 20 6f 72 20 6f 74 68 65 72 20 6e 6f 6e nux or other non 0190: 2d 53 6f 6c 61 72 69 73 20 4f 53 27 73 20 66 6f -Solaris OS's fo 01a0: 72 20 6d 65 6d 63 70 79 20 64 65 63 6c 61 72 61 r memcpy declara 01b0: 74 69 6f 6e 20 2a 2f 0a 23 69 6e 63 6c 75 64 65 tion */.#include 01c0: 20 3c 6d 65 6d 6f 72 79 2e 68 3e 0a 0a 2f 2a 20 <memory.h>../* 01d0: 55 6e 63 6f 6d 6d 65 6e 74 20 66 6f 72 20 53 6f Uncomment for So 01e0: 6c 61 72 69 73 20 28 61 6e 64 20 63 6f 6d 6d 65 laris (and comme 01f0: 6e 74 20 61 62 6f 76 65 29 20 66 6f 72 20 6d 65 nt above) for me 0200: 6d 63 70 79 20 64 65 63 6c 61 72 61 74 69 6f 6e mcpy declaration 0210: 20 2a 2f 0a 2f 2a 20 23 69 6e 63 6c 75 64 65 20 */./* #include 0220: 3c 73 74 72 69 6e 67 2e 68 3e 20 2a 2f 0a 0a 23 <string.h> */..# 0230: 69 66 6e 64 65 66 20 45 49 4e 56 41 4c 0a 23 64 ifndef EINVAL.#d 0240: 65 66 69 6e 65 20 45 49 4e 56 41 4c 20 39 0a 23 efine EINVAL 9.# 0250: 65 6e 64 69 66 0a 0a 74 79 70 65 64 65 66 20 73 endif..typedef s 0260: 74 72 75 63 74 0a 7b 0a 20 20 54 63 6c 5f 43 68 truct.{. Tcl_Ch 0270: 61 6e 6e 65 6c 20 5f 63 68 61 6e 3b 0a 20 20 69 annel _chan;. i 0280: 6e 74 20 5f 76 61 6c 69 64 4d 61 73 6b 3b 0a 20 nt _validMask;. 0290: 20 69 6e 74 20 5f 77 61 74 63 68 4d 61 73 6b 3b int _watchMask; 02a0: 0a 20 20 54 63 6c 5f 49 6e 74 65 72 70 2a 20 5f . Tcl_Interp* _ 02b0: 69 6e 74 65 72 70 3b 0a 20 20 54 63 6c 5f 4f 62 interp;. Tcl_Ob 02c0: 6a 2a 20 5f 63 6f 6e 74 65 78 74 3b 0a 20 20 54 j* _context;. T 02d0: 63 6c 5f 4f 62 6a 2a 20 5f 73 65 65 6b 3b 0a 20 cl_Obj* _seek;. 02e0: 20 54 63 6c 5f 4f 62 6a 2a 20 5f 72 65 61 64 3b Tcl_Obj* _read; 02f0: 0a 20 20 54 63 6c 5f 4f 62 6a 2a 20 5f 77 72 69 . Tcl_Obj* _wri 0300: 74 65 3b 0a 20 20 54 63 6c 5f 4f 62 6a 2a 20 5f te;. Tcl_Obj* _ 0310: 6e 61 6d 65 3b 0a 20 20 54 63 6c 5f 54 69 6d 65 name;. Tcl_Time 0320: 72 54 6f 6b 65 6e 20 5f 74 69 6d 65 72 3b 0a 7d rToken _timer;.} 0330: 20 52 65 66 6c 65 63 74 69 6e 67 43 68 61 6e 6e ReflectingChann 0340: 65 6c 3b 0a 0a 73 74 61 74 69 63 20 52 65 66 6c el;..static Refl 0350: 65 63 74 69 6e 67 43 68 61 6e 6e 65 6c 2a 0a 72 ectingChannel*.r 0360: 63 43 72 65 61 74 65 20 28 54 63 6c 5f 49 6e 74 cCreate (Tcl_Int 0370: 65 72 70 2a 20 69 70 5f 2c 20 54 63 6c 5f 4f 62 erp* ip_, Tcl_Ob 0380: 6a 2a 20 63 6f 6e 74 65 78 74 5f 2c 20 69 6e 74 j* context_, int 0390: 20 6d 6f 64 65 5f 2c 20 63 6f 6e 73 74 20 63 68 mode_, const ch 03a0: 61 72 2a 20 6e 61 6d 65 5f 29 0a 7b 0a 20 20 52 ar* name_).{. R 03b0: 65 66 6c 65 63 74 69 6e 67 43 68 61 6e 6e 65 6c eflectingChannel 03c0: 2a 20 63 70 20 3d 20 28 52 65 66 6c 65 63 74 69 * cp = (Reflecti 03d0: 6e 67 43 68 61 6e 6e 65 6c 2a 29 20 54 63 6c 5f ngChannel*) Tcl_ 03e0: 41 6c 6c 6f 63 20 28 73 69 7a 65 6f 66 20 2a 63 Alloc (sizeof *c 03f0: 70 29 3b 0a 0a 20 20 63 70 2d 3e 5f 76 61 6c 69 p);.. cp->_vali 0400: 64 4d 61 73 6b 20 3d 20 6d 6f 64 65 5f 3b 0a 20 dMask = mode_;. 0410: 20 63 70 2d 3e 5f 77 61 74 63 68 4d 61 73 6b 20 cp->_watchMask 0420: 3d 20 30 3b 0a 20 20 63 70 2d 3e 5f 63 68 61 6e = 0;. cp->_chan 0430: 20 3d 20 30 3b 0a 20 20 63 70 2d 3e 5f 63 6f 6e = 0;. cp->_con 0440: 74 65 78 74 20 3d 20 63 6f 6e 74 65 78 74 5f 3b text = context_; 0450: 0a 20 20 63 70 2d 3e 5f 69 6e 74 65 72 70 20 3d . cp->_interp = 0460: 20 69 70 5f 3b 0a 20 20 63 70 2d 3e 5f 6e 61 6d ip_;. cp->_nam 0470: 65 20 3d 20 54 63 6c 5f 4e 65 77 53 74 72 69 6e e = Tcl_NewStrin 0480: 67 4f 62 6a 28 6e 61 6d 65 5f 2c 20 2d 31 29 3b gObj(name_, -1); 0490: 0a 20 20 63 70 2d 3e 5f 74 69 6d 65 72 20 3d 20 . cp->_timer = 04a0: 4e 55 4c 4c 3b 0a 0a 20 20 20 20 2f 2a 20 73 75 NULL;.. /* su 04b0: 70 70 6f 72 74 20 54 63 6c 5f 47 65 74 49 6e 64 pport Tcl_GetInd 04c0: 65 78 46 72 6f 6d 4f 62 6a 20 62 79 20 6b 65 65 exFromObj by kee 04d0: 70 69 6e 67 20 74 68 65 73 65 20 6f 62 6a 65 63 ping these objec 04e0: 74 69 66 69 65 64 20 2a 2f 0a 20 20 63 70 2d 3e tified */. cp-> 04f0: 5f 73 65 65 6b 20 3d 20 54 63 6c 5f 4e 65 77 53 _seek = Tcl_NewS 0500: 74 72 69 6e 67 4f 62 6a 28 22 73 65 65 6b 22 2c tringObj("seek", 0510: 20 2d 31 29 3b 0a 20 20 63 70 2d 3e 5f 72 65 61 -1);. cp->_rea 0520: 64 20 3d 20 54 63 6c 5f 4e 65 77 53 74 72 69 6e d = Tcl_NewStrin 0530: 67 4f 62 6a 28 22 72 65 61 64 22 2c 20 2d 31 29 gObj("read", -1) 0540: 3b 0a 20 20 63 70 2d 3e 5f 77 72 69 74 65 20 3d ;. cp->_write = 0550: 20 54 63 6c 5f 4e 65 77 53 74 72 69 6e 67 4f 62 Tcl_NewStringOb 0560: 6a 28 22 77 72 69 74 65 22 2c 20 2d 31 29 3b 0a j("write", -1);. 0570: 0a 20 20 54 63 6c 5f 49 6e 63 72 52 65 66 43 6f . Tcl_IncrRefCo 0580: 75 6e 74 28 63 70 2d 3e 5f 63 6f 6e 74 65 78 74 unt(cp->_context 0590: 29 3b 0a 20 20 54 63 6c 5f 49 6e 63 72 52 65 66 );. Tcl_IncrRef 05a0: 43 6f 75 6e 74 28 63 70 2d 3e 5f 73 65 65 6b 29 Count(cp->_seek) 05b0: 3b 0a 20 20 54 63 6c 5f 49 6e 63 72 52 65 66 43 ;. Tcl_IncrRefC 05c0: 6f 75 6e 74 28 63 70 2d 3e 5f 72 65 61 64 29 3b ount(cp->_read); 05d0: 0a 20 20 54 63 6c 5f 49 6e 63 72 52 65 66 43 6f . Tcl_IncrRefCo 05e0: 75 6e 74 28 63 70 2d 3e 5f 77 72 69 74 65 29 3b unt(cp->_write); 05f0: 0a 20 20 54 63 6c 5f 49 6e 63 72 52 65 66 43 6f . Tcl_IncrRefCo 0600: 75 6e 74 28 63 70 2d 3e 5f 6e 61 6d 65 29 3b 0a unt(cp->_name);. 0610: 0a 20 20 72 65 74 75 72 6e 20 63 70 3b 0a 7d 0a . return cp;.}. 0620: 0a 73 74 61 74 69 63 20 54 63 6c 5f 4f 62 6a 2a .static Tcl_Obj* 0630: 0a 72 63 42 75 69 6c 64 43 6d 64 4c 69 73 74 28 .rcBuildCmdList( 0640: 52 65 66 6c 65 63 74 69 6e 67 43 68 61 6e 6e 65 ReflectingChanne 0650: 6c 2a 20 63 68 61 6e 5f 2c 20 54 63 6c 5f 4f 62 l* chan_, Tcl_Ob 0660: 6a 2a 20 63 6d 64 5f 29 0a 7b 0a 20 20 54 63 6c j* cmd_).{. Tcl 0670: 5f 4f 62 6a 2a 20 76 65 63 20 3d 20 54 63 6c 5f _Obj* vec = Tcl_ 0680: 44 75 70 6c 69 63 61 74 65 4f 62 6a 28 63 68 61 DuplicateObj(cha 0690: 6e 5f 2d 3e 5f 63 6f 6e 74 65 78 74 29 3b 0a 20 n_->_context);. 06a0: 20 54 63 6c 5f 49 6e 63 72 52 65 66 43 6f 75 6e Tcl_IncrRefCoun 06b0: 74 28 76 65 63 29 3b 0a 0a 20 20 54 63 6c 5f 4c t(vec);.. Tcl_L 06c0: 69 73 74 4f 62 6a 41 70 70 65 6e 64 45 6c 65 6d istObjAppendElem 06d0: 65 6e 74 28 63 68 61 6e 5f 2d 3e 5f 69 6e 74 65 ent(chan_->_inte 06e0: 72 70 2c 20 76 65 63 2c 20 63 6d 64 5f 29 3b 0a rp, vec, cmd_);. 06f0: 20 20 54 63 6c 5f 4c 69 73 74 4f 62 6a 41 70 70 Tcl_ListObjApp 0700: 65 6e 64 45 6c 65 6d 65 6e 74 28 63 68 61 6e 5f endElement(chan_ 0710: 2d 3e 5f 69 6e 74 65 72 70 2c 20 76 65 63 2c 20 ->_interp, vec, 0720: 63 68 61 6e 5f 2d 3e 5f 6e 61 6d 65 29 3b 0a 0a chan_->_name);.. 0730: 20 20 72 65 74 75 72 6e 20 76 65 63 3b 20 2f 2a return vec; /* 0740: 20 77 69 74 68 20 72 65 66 63 6f 75 6e 74 20 31 with refcount 1 0750: 20 2a 2f 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e */.}..static in 0760: 74 0a 72 63 43 6c 6f 73 65 20 28 43 6c 69 65 6e t.rcClose (Clien 0770: 74 44 61 74 61 20 63 64 5f 2c 20 54 63 6c 5f 49 tData cd_, Tcl_I 0780: 6e 74 65 72 70 2a 20 69 6e 74 65 72 70 29 0a 7b nterp* interp).{ 0790: 0a 20 20 52 65 66 6c 65 63 74 69 6e 67 43 68 61 . ReflectingCha 07a0: 6e 6e 65 6c 2a 20 63 68 61 6e 20 3d 20 28 52 65 nnel* chan = (Re 07b0: 66 6c 65 63 74 69 6e 67 43 68 61 6e 6e 65 6c 2a flectingChannel* 07c0: 29 20 63 64 5f 3b 0a 20 20 69 6e 74 20 6e 20 3d ) cd_;. int n = 07d0: 20 2d 31 3b 0a 0a 20 20 54 63 6c 5f 53 61 76 65 -1;.. Tcl_Save 07e0: 64 52 65 73 75 6c 74 20 73 72 3b 0a 20 20 54 63 dResult sr;. Tc 07f0: 6c 5f 4f 62 6a 2a 20 63 6d 64 20 3d 20 72 63 42 l_Obj* cmd = rcB 0800: 75 69 6c 64 43 6d 64 4c 69 73 74 28 63 68 61 6e uildCmdList(chan 0810: 2c 20 54 63 6c 5f 4e 65 77 53 74 72 69 6e 67 4f , Tcl_NewStringO 0820: 62 6a 28 22 63 6c 6f 73 65 22 2c 20 2d 31 29 29 bj("close", -1)) 0830: 3b 0a 20 20 54 63 6c 5f 49 6e 74 65 72 70 2a 20 ;. Tcl_Interp* 0840: 69 70 20 3d 20 63 68 61 6e 2d 3e 5f 69 6e 74 65 ip = chan->_inte 0850: 72 70 3b 0a 0a 20 20 54 63 6c 5f 53 61 76 65 52 rp;.. Tcl_SaveR 0860: 65 73 75 6c 74 28 69 70 2c 20 26 73 72 29 3b 0a esult(ip, &sr);. 0870: 0a 20 20 69 66 20 28 54 63 6c 5f 45 76 61 6c 4f . if (Tcl_EvalO 0880: 62 6a 45 78 28 69 70 2c 20 63 6d 64 2c 20 54 43 bjEx(ip, cmd, TC 0890: 4c 5f 45 56 41 4c 5f 47 4c 4f 42 41 4c 20 7c 20 L_EVAL_GLOBAL | 08a0: 54 43 4c 5f 45 56 41 4c 5f 44 49 52 45 43 54 29 TCL_EVAL_DIRECT) 08b0: 20 3d 3d 20 54 43 4c 5f 4f 4b 29 0a 20 20 20 20 == TCL_OK). 08c0: 54 63 6c 5f 47 65 74 49 6e 74 46 72 6f 6d 4f 62 Tcl_GetIntFromOb 08d0: 6a 28 4e 55 4c 4c 2c 20 54 63 6c 5f 47 65 74 4f j(NULL, Tcl_GetO 08e0: 62 6a 52 65 73 75 6c 74 28 69 70 29 2c 20 26 6e bjResult(ip), &n 08f0: 29 3b 0a 0a 20 20 54 63 6c 5f 52 65 73 74 6f 72 );.. Tcl_Restor 0900: 65 52 65 73 75 6c 74 28 69 70 2c 20 26 73 72 29 eResult(ip, &sr) 0910: 3b 0a 20 20 54 63 6c 5f 44 65 63 72 52 65 66 43 ;. Tcl_DecrRefC 0920: 6f 75 6e 74 28 63 6d 64 29 3b 0a 0a 20 20 69 66 ount(cmd);.. if 0930: 20 28 63 68 61 6e 2d 3e 5f 74 69 6d 65 72 20 21 (chan->_timer ! 0940: 3d 20 4e 55 4c 4c 29 20 7b 0a 20 20 20 20 54 63 = NULL) {. Tc 0950: 6c 5f 44 65 6c 65 74 65 54 69 6d 65 72 48 61 6e l_DeleteTimerHan 0960: 64 6c 65 72 28 63 68 61 6e 2d 3e 5f 74 69 6d 65 dler(chan->_time 0970: 72 29 3b 0a 20 20 20 20 63 68 61 6e 2d 3e 5f 74 r);. chan->_t 0980: 69 6d 65 72 20 3d 20 4e 55 4c 4c 3b 0a 20 20 7d imer = NULL;. } 0990: 0a 0a 20 20 54 63 6c 5f 44 65 63 72 52 65 66 43 .. Tcl_DecrRefC 09a0: 6f 75 6e 74 28 63 68 61 6e 2d 3e 5f 63 6f 6e 74 ount(chan->_cont 09b0: 65 78 74 29 3b 0a 20 20 54 63 6c 5f 44 65 63 72 ext);. Tcl_Decr 09c0: 52 65 66 43 6f 75 6e 74 28 63 68 61 6e 2d 3e 5f RefCount(chan->_ 09d0: 73 65 65 6b 29 3b 0a 20 20 54 63 6c 5f 44 65 63 seek);. Tcl_Dec 09e0: 72 52 65 66 43 6f 75 6e 74 28 63 68 61 6e 2d 3e rRefCount(chan-> 09f0: 5f 72 65 61 64 29 3b 0a 20 20 54 63 6c 5f 44 65 _read);. Tcl_De 0a00: 63 72 52 65 66 43 6f 75 6e 74 28 63 68 61 6e 2d crRefCount(chan- 0a10: 3e 5f 77 72 69 74 65 29 3b 0a 20 20 54 63 6c 5f >_write);. Tcl_ 0a20: 44 65 63 72 52 65 66 43 6f 75 6e 74 28 63 68 61 DecrRefCount(cha 0a30: 6e 2d 3e 5f 6e 61 6d 65 29 3b 0a 20 20 54 63 6c n->_name);. Tcl 0a40: 5f 46 72 65 65 28 28 63 68 61 72 2a 29 20 63 68 _Free((char*) ch 0a50: 61 6e 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 54 an);.. return T 0a60: 43 4c 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63 CL_OK;.}..static 0a70: 20 69 6e 74 0a 72 63 49 6e 70 75 74 20 28 43 6c int.rcInput (Cl 0a80: 69 65 6e 74 44 61 74 61 20 63 64 5f 2c 20 63 68 ientData cd_, ch 0a90: 61 72 2a 20 62 75 66 2c 20 69 6e 74 20 74 6f 52 ar* buf, int toR 0aa0: 65 61 64 2c 20 69 6e 74 2a 20 65 72 72 6f 72 43 ead, int* errorC 0ab0: 6f 64 65 50 74 72 29 0a 7b 0a 20 20 52 65 66 6c odePtr).{. Refl 0ac0: 65 63 74 69 6e 67 43 68 61 6e 6e 65 6c 2a 20 63 ectingChannel* c 0ad0: 68 61 6e 20 3d 20 28 52 65 66 6c 65 63 74 69 6e han = (Reflectin 0ae0: 67 43 68 61 6e 6e 65 6c 2a 29 20 63 64 5f 3b 0a gChannel*) cd_;. 0af0: 20 20 69 6e 74 20 6e 20 3d 20 2d 31 3b 0a 0a 20 int n = -1;.. 0b00: 20 69 66 20 28 63 68 61 6e 2d 3e 5f 76 61 6c 69 if (chan->_vali 0b10: 64 4d 61 73 6b 20 26 20 54 43 4c 5f 52 45 41 44 dMask & TCL_READ 0b20: 41 42 4c 45 29 20 7b 0a 20 20 20 20 54 63 6c 5f ABLE) {. Tcl_ 0b30: 53 61 76 65 64 52 65 73 75 6c 74 20 73 72 3b 0a SavedResult sr;. 0b40: 20 20 20 20 54 63 6c 5f 4f 62 6a 2a 20 63 6d 64 Tcl_Obj* cmd 0b50: 20 3d 20 72 63 42 75 69 6c 64 43 6d 64 4c 69 73 = rcBuildCmdLis 0b60: 74 28 63 68 61 6e 2c 20 63 68 61 6e 2d 3e 5f 72 t(chan, chan->_r 0b70: 65 61 64 29 3b 0a 20 20 20 20 54 63 6c 5f 49 6e ead);. Tcl_In 0b80: 74 65 72 70 2a 20 69 70 20 3d 20 63 68 61 6e 2d terp* ip = chan- 0b90: 3e 5f 69 6e 74 65 72 70 3b 0a 0a 20 20 20 20 54 >_interp;.. T 0ba0: 63 6c 5f 4c 69 73 74 4f 62 6a 41 70 70 65 6e 64 cl_ListObjAppend 0bb0: 45 6c 65 6d 65 6e 74 28 4e 55 4c 4c 2c 20 63 6d Element(NULL, cm 0bc0: 64 2c 20 54 63 6c 5f 4e 65 77 49 6e 74 4f 62 6a d, Tcl_NewIntObj 0bd0: 28 74 6f 52 65 61 64 29 29 3b 0a 20 20 20 20 54 (toRead));. T 0be0: 63 6c 5f 53 61 76 65 52 65 73 75 6c 74 28 69 70 cl_SaveResult(ip 0bf0: 2c 20 26 73 72 29 3b 0a 0a 20 20 20 20 69 66 20 , &sr);.. if 0c00: 28 54 63 6c 5f 45 76 61 6c 4f 62 6a 45 78 28 69 (Tcl_EvalObjEx(i 0c10: 70 2c 20 63 6d 64 2c 20 54 43 4c 5f 45 56 41 4c p, cmd, TCL_EVAL 0c20: 5f 47 4c 4f 42 41 4c 20 7c 20 54 43 4c 5f 45 56 _GLOBAL | TCL_EV 0c30: 41 4c 5f 44 49 52 45 43 54 29 20 3d 3d 20 54 43 AL_DIRECT) == TC 0c40: 4c 5f 4f 4b 29 20 7b 0a 20 20 20 20 20 20 76 6f L_OK) {. vo 0c50: 69 64 2a 20 73 20 3d 20 54 63 6c 5f 47 65 74 42 id* s = Tcl_GetB 0c60: 79 74 65 41 72 72 61 79 46 72 6f 6d 4f 62 6a 28 yteArrayFromObj( 0c70: 54 63 6c 5f 47 65 74 4f 62 6a 52 65 73 75 6c 74 Tcl_GetObjResult 0c80: 28 69 70 29 2c 20 26 6e 29 3b 0a 20 20 20 20 20 (ip), &n);. 0c90: 20 69 66 20 28 30 20 3c 3d 20 6e 20 26 26 20 6e if (0 <= n && n 0ca0: 20 3c 3d 20 74 6f 52 65 61 64 29 0a 09 69 66 20 <= toRead)..if 0cb0: 28 6e 20 3e 20 30 29 0a 09 20 20 6d 65 6d 63 70 (n > 0).. memcp 0cc0: 79 28 62 75 66 2c 20 73 2c 20 6e 29 3b 0a 09 65 y(buf, s, n);..e 0cd0: 6c 73 65 0a 09 20 20 63 68 61 6e 2d 3e 5f 77 61 lse.. chan->_wa 0ce0: 74 63 68 4d 61 73 6b 20 26 3d 20 7e 54 43 4c 5f tchMask &= ~TCL_ 0cf0: 52 45 41 44 41 42 4c 45 3b 0a 20 20 20 20 20 20 READABLE;. 0d00: 65 6c 73 65 0a 09 6e 20 3d 20 2d 31 3b 0a 20 20 else..n = -1;. 0d10: 20 20 7d 0a 0a 20 20 20 20 54 63 6c 5f 52 65 73 }.. Tcl_Res 0d20: 74 6f 72 65 52 65 73 75 6c 74 28 69 70 2c 20 26 toreResult(ip, & 0d30: 73 72 29 3b 0a 20 20 20 20 54 63 6c 5f 44 65 63 sr);. Tcl_Dec 0d40: 72 52 65 66 43 6f 75 6e 74 28 63 6d 64 29 3b 0a rRefCount(cmd);. 0d50: 20 20 7d 0a 0a 20 20 69 66 20 28 6e 20 3c 20 30 }.. if (n < 0 0d60: 29 0a 20 20 20 20 2a 65 72 72 6f 72 43 6f 64 65 ). *errorCode 0d70: 50 74 72 20 3d 20 45 49 4e 56 41 4c 3b 0a 20 20 Ptr = EINVAL;. 0d80: 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 73 74 61 return n;.}..sta 0d90: 74 69 63 20 69 6e 74 0a 72 63 4f 75 74 70 75 74 tic int.rcOutput 0da0: 20 28 43 6c 69 65 6e 74 44 61 74 61 20 63 64 5f (ClientData cd_ 0db0: 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 20 62 75 , const char* bu 0dc0: 66 2c 20 69 6e 74 20 74 6f 57 72 69 74 65 2c 20 f, int toWrite, 0dd0: 69 6e 74 2a 20 65 72 72 6f 72 43 6f 64 65 50 74 int* errorCodePt 0de0: 72 29 0a 7b 0a 20 20 52 65 66 6c 65 63 74 69 6e r).{. Reflectin 0df0: 67 43 68 61 6e 6e 65 6c 2a 20 63 68 61 6e 20 3d gChannel* chan = 0e00: 20 28 52 65 66 6c 65 63 74 69 6e 67 43 68 61 6e (ReflectingChan 0e10: 6e 65 6c 2a 29 20 63 64 5f 3b 0a 20 20 69 6e 74 nel*) cd_;. int 0e20: 20 6e 20 3d 20 2d 31 3b 0a 0a 20 20 69 66 20 28 n = -1;.. if ( 0e30: 63 68 61 6e 2d 3e 5f 76 61 6c 69 64 4d 61 73 6b chan->_validMask 0e40: 20 26 20 54 43 4c 5f 57 52 49 54 41 42 4c 45 29 & TCL_WRITABLE) 0e50: 20 7b 0a 20 20 20 20 54 63 6c 5f 53 61 76 65 64 {. Tcl_Saved 0e60: 52 65 73 75 6c 74 20 73 72 3b 0a 20 20 20 20 54 Result sr;. T 0e70: 63 6c 5f 4f 62 6a 2a 20 63 6d 64 20 3d 20 72 63 cl_Obj* cmd = rc 0e80: 42 75 69 6c 64 43 6d 64 4c 69 73 74 28 63 68 61 BuildCmdList(cha 0e90: 6e 2c 20 63 68 61 6e 2d 3e 5f 77 72 69 74 65 29 n, chan->_write) 0ea0: 3b 0a 20 20 20 20 54 63 6c 5f 49 6e 74 65 72 70 ;. Tcl_Interp 0eb0: 2a 20 69 70 20 3d 20 63 68 61 6e 2d 3e 5f 69 6e * ip = chan->_in 0ec0: 74 65 72 70 3b 0a 0a 20 20 20 20 54 63 6c 5f 4c terp;.. Tcl_L 0ed0: 69 73 74 4f 62 6a 41 70 70 65 6e 64 45 6c 65 6d istObjAppendElem 0ee0: 65 6e 74 28 4e 55 4c 4c 2c 20 63 6d 64 2c 0a 09 ent(NULL, cmd,.. 0ef0: 20 20 20 20 20 20 09 09 54 63 6c 5f 4e 65 77 42 ..Tcl_NewB 0f00: 79 74 65 41 72 72 61 79 4f 62 6a 28 28 75 6e 73 yteArrayObj((uns 0f10: 69 67 6e 65 64 20 63 68 61 72 2a 29 20 62 75 66 igned char*) buf 0f20: 2c 20 74 6f 57 72 69 74 65 29 29 3b 0a 20 20 20 , toWrite));. 0f30: 20 54 63 6c 5f 53 61 76 65 52 65 73 75 6c 74 28 Tcl_SaveResult( 0f40: 69 70 2c 20 26 73 72 29 3b 0a 0a 20 20 20 20 69 ip, &sr);.. i 0f50: 66 20 28 54 63 6c 5f 45 76 61 6c 4f 62 6a 45 78 f (Tcl_EvalObjEx 0f60: 28 69 70 2c 20 63 6d 64 2c 20 54 43 4c 5f 45 56 (ip, cmd, TCL_EV 0f70: 41 4c 5f 47 4c 4f 42 41 4c 20 7c 20 54 43 4c 5f AL_GLOBAL | TCL_ 0f80: 45 56 41 4c 5f 44 49 52 45 43 54 29 20 3d 3d 20 EVAL_DIRECT) == 0f90: 54 43 4c 5f 4f 4b 20 26 26 0a 09 54 63 6c 5f 47 TCL_OK &&..Tcl_G 0fa0: 65 74 49 6e 74 46 72 6f 6d 4f 62 6a 28 4e 55 4c etIntFromObj(NUL 0fb0: 4c 2c 20 54 63 6c 5f 47 65 74 4f 62 6a 52 65 73 L, Tcl_GetObjRes 0fc0: 75 6c 74 28 69 70 29 2c 20 26 6e 29 20 3d 3d 20 ult(ip), &n) == 0fd0: 54 43 4c 5f 4f 4b 29 0a 20 20 20 20 20 20 69 66 TCL_OK). if 0fe0: 20 28 30 20 3c 3d 20 6e 20 26 26 20 6e 20 3c 3d (0 <= n && n <= 0ff0: 20 74 6f 57 72 69 74 65 29 0a 09 63 68 61 6e 2d toWrite)..chan- 1000: 3e 5f 77 61 74 63 68 4d 61 73 6b 20 3d 20 63 68 >_watchMask = ch 1010: 61 6e 2d 3e 5f 76 61 6c 69 64 4d 61 73 6b 3b 0a an->_validMask;. 1020: 20 20 20 20 20 20 65 6c 73 65 0a 09 6e 20 3d 20 else..n = 1030: 2d 31 3b 0a 0a 20 20 20 20 54 63 6c 5f 52 65 73 -1;.. Tcl_Res 1040: 74 6f 72 65 52 65 73 75 6c 74 28 69 70 2c 20 26 toreResult(ip, & 1050: 73 72 29 3b 0a 20 20 20 20 54 63 6c 5f 44 65 63 sr);. Tcl_Dec 1060: 72 52 65 66 43 6f 75 6e 74 28 63 6d 64 29 3b 0a rRefCount(cmd);. 1070: 20 20 7d 0a 0a 20 20 69 66 20 28 6e 20 3c 20 30 }.. if (n < 0 1080: 29 0a 20 20 20 20 2a 65 72 72 6f 72 43 6f 64 65 ). *errorCode 1090: 50 74 72 20 3d 20 45 49 4e 56 41 4c 3b 0a 20 20 Ptr = EINVAL;. 10a0: 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 73 74 61 return n;.}..sta 10b0: 74 69 63 20 69 6e 74 0a 72 63 53 65 65 6b 20 28 tic int.rcSeek ( 10c0: 43 6c 69 65 6e 74 44 61 74 61 20 63 64 5f 2c 20 ClientData cd_, 10d0: 6c 6f 6e 67 20 6f 66 66 73 65 74 2c 20 69 6e 74 long offset, int 10e0: 20 73 65 65 6b 4d 6f 64 65 2c 20 69 6e 74 2a 20 seekMode, int* 10f0: 65 72 72 6f 72 43 6f 64 65 50 74 72 29 0a 7b 0a errorCodePtr).{. 1100: 20 20 52 65 66 6c 65 63 74 69 6e 67 43 68 61 6e ReflectingChan 1110: 6e 65 6c 2a 20 63 68 61 6e 20 3d 20 28 52 65 66 nel* chan = (Ref 1120: 6c 65 63 74 69 6e 67 43 68 61 6e 6e 65 6c 2a 29 lectingChannel*) 1130: 20 63 64 5f 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 cd_;. int n = 1140: 2d 31 3b 0a 0a 20 20 54 63 6c 5f 53 61 76 65 64 -1;.. Tcl_Saved 1150: 52 65 73 75 6c 74 20 73 72 3b 0a 20 20 54 63 6c Result sr;. Tcl 1160: 5f 4f 62 6a 2a 20 63 6d 64 20 3d 20 72 63 42 75 _Obj* cmd = rcBu 1170: 69 6c 64 43 6d 64 4c 69 73 74 28 63 68 61 6e 2c ildCmdList(chan, 1180: 20 63 68 61 6e 2d 3e 5f 73 65 65 6b 29 3b 0a 20 chan->_seek);. 1190: 20 54 63 6c 5f 49 6e 74 65 72 70 2a 20 69 70 20 Tcl_Interp* ip 11a0: 3d 20 63 68 61 6e 2d 3e 5f 69 6e 74 65 72 70 3b = chan->_interp; 11b0: 0a 0a 20 20 54 63 6c 5f 4c 69 73 74 4f 62 6a 41 .. Tcl_ListObjA 11c0: 70 70 65 6e 64 45 6c 65 6d 65 6e 74 28 4e 55 4c ppendElement(NUL 11d0: 4c 2c 20 63 6d 64 2c 20 54 63 6c 5f 4e 65 77 4c L, cmd, Tcl_NewL 11e0: 6f 6e 67 4f 62 6a 28 6f 66 66 73 65 74 29 29 3b ongObj(offset)); 11f0: 0a 20 20 54 63 6c 5f 4c 69 73 74 4f 62 6a 41 70 . Tcl_ListObjAp 1200: 70 65 6e 64 45 6c 65 6d 65 6e 74 28 4e 55 4c 4c pendElement(NULL 1210: 2c 20 63 6d 64 2c 20 54 63 6c 5f 4e 65 77 49 6e , cmd, Tcl_NewIn 1220: 74 4f 62 6a 28 73 65 65 6b 4d 6f 64 65 29 29 3b tObj(seekMode)); 1230: 0a 20 20 54 63 6c 5f 53 61 76 65 52 65 73 75 6c . Tcl_SaveResul 1240: 74 28 69 70 2c 20 26 73 72 29 3b 0a 0a 20 20 69 t(ip, &sr);.. i 1250: 66 20 28 54 63 6c 5f 45 76 61 6c 4f 62 6a 45 78 f (Tcl_EvalObjEx 1260: 28 69 70 2c 20 63 6d 64 2c 20 54 43 4c 5f 45 56 (ip, cmd, TCL_EV 1270: 41 4c 5f 47 4c 4f 42 41 4c 20 7c 20 54 43 4c 5f AL_GLOBAL | TCL_ 1280: 45 56 41 4c 5f 44 49 52 45 43 54 29 20 3d 3d 20 EVAL_DIRECT) == 1290: 54 43 4c 5f 4f 4b 20 26 26 0a 20 20 20 20 20 20 TCL_OK &&. 12a0: 54 63 6c 5f 47 65 74 49 6e 74 46 72 6f 6d 4f 62 Tcl_GetIntFromOb 12b0: 6a 28 4e 55 4c 4c 2c 20 54 63 6c 5f 47 65 74 4f j(NULL, Tcl_GetO 12c0: 62 6a 52 65 73 75 6c 74 28 69 70 29 2c 20 26 6e bjResult(ip), &n 12d0: 29 20 3d 3d 20 54 43 4c 5f 4f 4b 29 0a 20 20 20 ) == TCL_OK). 12e0: 20 63 68 61 6e 2d 3e 5f 77 61 74 63 68 4d 61 73 chan->_watchMas 12f0: 6b 20 3d 20 63 68 61 6e 2d 3e 5f 76 61 6c 69 64 k = chan->_valid 1300: 4d 61 73 6b 3b 0a 0a 20 20 54 63 6c 5f 52 65 73 Mask;.. Tcl_Res 1310: 74 6f 72 65 52 65 73 75 6c 74 28 69 70 2c 20 26 toreResult(ip, & 1320: 73 72 29 3b 0a 20 20 54 63 6c 5f 44 65 63 72 52 sr);. Tcl_DecrR 1330: 65 66 43 6f 75 6e 74 28 63 6d 64 29 3b 0a 0a 20 efCount(cmd);.. 1340: 20 69 66 20 28 6e 20 3c 20 30 29 0a 20 20 20 20 if (n < 0). 1350: 2a 65 72 72 6f 72 43 6f 64 65 50 74 72 20 3d 20 *errorCodePtr = 1360: 45 49 4e 56 41 4c 3b 0a 20 20 72 65 74 75 72 6e EINVAL;. return 1370: 20 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f n;.}..static vo 1380: 69 64 0a 72 63 54 69 6d 65 72 50 72 6f 63 20 28 id.rcTimerProc ( 1390: 43 6c 69 65 6e 74 44 61 74 61 20 63 64 5f 29 0a ClientData cd_). 13a0: 7b 0a 20 20 52 65 66 6c 65 63 74 69 6e 67 43 68 {. ReflectingCh 13b0: 61 6e 6e 65 6c 2a 20 63 68 61 6e 20 3d 20 28 52 annel* chan = (R 13c0: 65 66 6c 65 63 74 69 6e 67 43 68 61 6e 6e 65 6c eflectingChannel 13d0: 2a 29 20 63 64 5f 3b 0a 0a 20 20 69 66 20 28 63 *) cd_;.. if (c 13e0: 68 61 6e 2d 3e 5f 74 69 6d 65 72 20 21 3d 20 4e han->_timer != N 13f0: 55 4c 4c 29 0a 20 20 20 20 54 63 6c 5f 44 65 6c ULL). Tcl_Del 1400: 65 74 65 54 69 6d 65 72 48 61 6e 64 6c 65 72 28 eteTimerHandler( 1410: 63 68 61 6e 2d 3e 5f 74 69 6d 65 72 29 3b 0a 20 chan->_timer);. 1420: 20 63 68 61 6e 2d 3e 5f 74 69 6d 65 72 20 3d 20 chan->_timer = 1430: 4e 55 4c 4c 3b 0a 20 20 54 63 6c 5f 4e 6f 74 69 NULL;. Tcl_Noti 1440: 66 79 43 68 61 6e 6e 65 6c 28 63 68 61 6e 2d 3e fyChannel(chan-> 1450: 5f 63 68 61 6e 2c 20 63 68 61 6e 2d 3e 5f 77 61 _chan, chan->_wa 1460: 74 63 68 4d 61 73 6b 29 3b 0a 7d 0a 0a 73 74 61 tchMask);.}..sta 1470: 74 69 63 20 76 6f 69 64 0a 72 63 57 61 74 63 68 tic void.rcWatch 1480: 43 68 61 6e 6e 65 6c 20 28 43 6c 69 65 6e 74 44 Channel (ClientD 1490: 61 74 61 20 63 64 5f 2c 20 69 6e 74 20 6d 61 73 ata cd_, int mas 14a0: 6b 29 0a 7b 0a 20 20 52 65 66 6c 65 63 74 69 6e k).{. Reflectin 14b0: 67 43 68 61 6e 6e 65 6c 2a 20 63 68 61 6e 20 3d gChannel* chan = 14c0: 20 28 52 65 66 6c 65 63 74 69 6e 67 43 68 61 6e (ReflectingChan 14d0: 6e 65 6c 2a 29 20 63 64 5f 3b 0a 0a 20 20 2f 2a nel*) cd_;.. /* 14e0: 20 44 65 63 20 32 30 30 31 3a 20 61 64 6f 70 74 Dec 2001: adopt 14f0: 69 6e 67 20 6c 6f 67 69 63 20 75 73 65 64 20 69 ing logic used i 1500: 6e 20 41 6e 64 72 65 61 73 20 4b 75 70 72 69 65 n Andreas Kuprie 1510: 73 27 20 6d 65 6d 63 68 61 6e 2c 20 69 2e 65 2e s' memchan, i.e. 1520: 20 74 69 6d 65 72 73 20 2a 2f 0a 0a 20 20 69 66 timers */.. if 1530: 20 28 6d 61 73 6b 29 20 7b 0a 20 20 20 20 63 68 (mask) {. ch 1540: 61 6e 2d 3e 5f 77 61 74 63 68 4d 61 73 6b 20 3d an->_watchMask = 1550: 20 6d 61 73 6b 20 26 20 63 68 61 6e 2d 3e 5f 76 mask & chan->_v 1560: 61 6c 69 64 4d 61 73 6b 3b 0a 20 20 20 20 69 66 alidMask;. if 1570: 20 28 63 68 61 6e 2d 3e 5f 77 61 74 63 68 4d 61 (chan->_watchMa 1580: 73 6b 20 26 26 20 63 68 61 6e 2d 3e 5f 74 69 6d sk && chan->_tim 1590: 65 72 20 3d 3d 20 4e 55 4c 4c 29 0a 20 20 20 20 er == NULL). 15a0: 20 20 63 68 61 6e 2d 3e 5f 74 69 6d 65 72 20 3d chan->_timer = 15b0: 20 54 63 6c 5f 43 72 65 61 74 65 54 69 6d 65 72 Tcl_CreateTimer 15c0: 48 61 6e 64 6c 65 72 28 35 2c 20 72 63 54 69 6d Handler(5, rcTim 15d0: 65 72 50 72 6f 63 2c 20 63 64 5f 29 3b 0a 20 20 erProc, cd_);. 15e0: 7d 20 65 6c 73 65 20 69 66 20 28 63 68 61 6e 2d } else if (chan- 15f0: 3e 5f 74 69 6d 65 72 20 21 3d 20 4e 55 4c 4c 29 >_timer != NULL) 1600: 20 7b 0a 20 20 20 20 54 63 6c 5f 44 65 6c 65 74 {. Tcl_Delet 1610: 65 54 69 6d 65 72 48 61 6e 64 6c 65 72 28 63 68 eTimerHandler(ch 1620: 61 6e 2d 3e 5f 74 69 6d 65 72 29 3b 0a 20 20 20 an->_timer);. 1630: 20 63 68 61 6e 2d 3e 5f 74 69 6d 65 72 20 3d 20 chan->_timer = 1640: 4e 55 4c 4c 3b 0a 20 20 7d 0a 7d 0a 0a 73 74 61 NULL;. }.}..sta 1650: 74 69 63 20 69 6e 74 0a 72 63 47 65 74 46 69 6c tic int.rcGetFil 1660: 65 20 28 43 6c 69 65 6e 74 44 61 74 61 20 63 64 e (ClientData cd 1670: 5f 2c 20 69 6e 74 20 64 69 72 65 63 74 69 6f 6e _, int direction 1680: 2c 20 43 6c 69 65 6e 74 44 61 74 61 2a 20 68 61 , ClientData* ha 1690: 6e 64 6c 65 50 74 72 29 0a 7b 0a 20 20 72 65 74 ndlePtr).{. ret 16a0: 75 72 6e 20 54 43 4c 5f 45 52 52 4f 52 3b 0a 7d urn TCL_ERROR;.} 16b0: 0a 0a 73 74 61 74 69 63 20 69 6e 74 0a 72 63 42 ..static int.rcB 16c0: 6c 6f 63 6b 20 28 43 6c 69 65 6e 74 44 61 74 61 lock (ClientData 16d0: 20 63 64 5f 2c 20 69 6e 74 20 6d 6f 64 65 29 0a cd_, int mode). 16e0: 7b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a {. return 0;.}. 16f0: 0a 73 74 61 74 69 63 20 54 63 6c 5f 43 68 61 6e .static Tcl_Chan 1700: 6e 65 6c 54 79 70 65 20 72 65 43 68 61 6e 6e 65 nelType reChanne 1710: 6c 54 79 70 65 20 3d 20 7b 0a 20 20 22 72 65 63 lType = {. "rec 1720: 68 61 6e 22 2c 20 20 20 20 20 20 20 2f 2a 20 54 han", /* T 1730: 79 70 65 20 6e 61 6d 65 2e 20 20 20 20 20 20 20 ype name. 1740: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 1750: 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a */. 1760: 20 20 28 54 63 6c 5f 43 68 61 6e 6e 65 6c 54 79 (Tcl_ChannelTy 1770: 70 65 56 65 72 73 69 6f 6e 29 20 72 63 42 6c 6f peVersion) rcBlo 1780: 63 6b 2c 20 2f 2a 20 53 65 74 20 62 6c 6f 63 6b ck, /* Set block 1790: 69 6e 67 2f 6e 6f 6e 62 6c 6f 63 6b 69 6e 67 20 ing/nonblocking 17a0: 62 65 68 61 76 69 6f 75 72 20 2a 2f 0a 20 20 72 behaviour */. r 17b0: 63 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 2f cClose, / 17c0: 2a 20 43 6c 6f 73 65 20 63 68 61 6e 6e 65 6c 2c * Close channel, 17d0: 20 63 6c 65 61 6e 20 69 6e 73 74 61 6e 63 65 20 clean instance 17e0: 64 61 74 61 20 20 20 20 20 20 20 20 20 20 20 20 data 17f0: 2a 2f 0a 20 20 72 63 49 6e 70 75 74 2c 20 20 20 */. rcInput, 1800: 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 72 /* Handle r 1810: 65 61 64 20 72 65 71 75 65 73 74 20 20 20 20 20 ead request 1820: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 1830: 20 20 20 20 20 20 2a 2f 0a 20 20 72 63 4f 75 74 */. rcOut 1840: 70 75 74 2c 20 20 20 20 20 20 20 2f 2a 20 48 61 put, /* Ha 1850: 6e 64 6c 65 20 77 72 69 74 65 20 72 65 71 75 65 ndle write reque 1860: 73 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 st 1870: 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 */. 1880: 20 72 63 53 65 65 6b 2c 20 20 20 20 20 20 20 20 rcSeek, 1890: 20 2f 2a 20 4d 6f 76 65 20 6c 6f 63 61 74 69 6f /* Move locatio 18a0: 6e 20 6f 66 20 61 63 63 65 73 73 20 70 6f 69 6e n of access poin 18b0: 74 2e 20 20 20 20 4e 55 4c 4c 27 61 62 6c 65 20 t. NULL'able 18c0: 20 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 */. 0, 18d0: 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 6f 70 /* Set op 18e0: 74 69 6f 6e 73 2e 20 20 20 20 20 20 20 20 20 20 tions. 18f0: 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c NULL 1900: 27 61 62 6c 65 20 20 20 2a 2f 0a 20 20 30 2c 20 'able */. 0, 1910: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /* 1920: 47 65 74 20 6f 70 74 69 6f 6e 73 2e 20 20 20 20 Get options. 1930: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 1940: 20 20 4e 55 4c 4c 27 61 62 6c 65 20 20 20 2a 2f NULL'able */ 1950: 0a 20 20 72 63 57 61 74 63 68 43 68 61 6e 6e 65 . rcWatchChanne 1960: 6c 2c 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 l, /* Initialize 1970: 20 6e 6f 74 69 66 69 65 72 20 20 20 20 20 20 20 notifier 1980: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 1990: 20 20 20 20 2a 2f 0a 20 20 72 63 47 65 74 46 69 */. rcGetFi 19a0: 6c 65 20 20 20 20 20 20 20 2f 2a 20 47 65 74 20 le /* Get 19b0: 4f 53 20 68 61 6e 64 6c 65 20 66 72 6f 6d 20 74 OS handle from t 19c0: 68 65 20 63 68 61 6e 6e 65 6c 2e 20 20 20 20 20 he channel. 19d0: 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 7d 3b 0a */.};. 19e0: 0a 73 74 61 74 69 63 20 69 6e 74 0a 63 6d 64 5f .static int.cmd_ 19f0: 72 65 63 68 61 6e 28 43 6c 69 65 6e 74 44 61 74 rechan(ClientDat 1a00: 61 20 63 64 5f 2c 20 54 63 6c 5f 49 6e 74 65 72 a cd_, Tcl_Inter 1a10: 70 2a 20 69 70 5f 2c 20 69 6e 74 20 6f 62 6a 63 p* ip_, int objc 1a20: 5f 2c 20 54 63 6c 5f 4f 62 6a 2a 63 6f 6e 73 74 _, Tcl_Obj*const 1a30: 2a 20 6f 62 6a 76 5f 29 0a 7b 0a 20 20 52 65 66 * objv_).{. Ref 1a40: 6c 65 63 74 69 6e 67 43 68 61 6e 6e 65 6c 20 2a lectingChannel * 1a50: 72 63 3b 0a 20 20 69 6e 74 20 6d 6f 64 65 3b 0a rc;. int mode;. 1a60: 20 20 63 68 61 72 20 62 75 66 66 65 72 20 5b 32 char buffer [2 1a70: 30 5d 3b 0a 0a 20 20 69 66 20 28 6f 62 6a 63 5f 0];.. if (objc_ 1a80: 20 21 3d 20 33 29 20 7b 0a 20 20 20 20 54 63 6c != 3) {. Tcl 1a90: 5f 57 72 6f 6e 67 4e 75 6d 41 72 67 73 28 69 70 _WrongNumArgs(ip 1aa0: 5f 2c 20 31 2c 20 6f 62 6a 76 5f 2c 20 22 63 6f _, 1, objv_, "co 1ab0: 6d 6d 61 6e 64 20 6d 6f 64 65 22 29 3b 0a 20 20 mmand mode");. 1ac0: 20 20 72 65 74 75 72 6e 20 54 43 4c 5f 45 52 52 return TCL_ERR 1ad0: 4f 52 3b 0a 20 20 7d 0a 0a 20 20 69 66 20 28 54 OR;. }.. if (T 1ae0: 63 6c 5f 4c 69 73 74 4f 62 6a 4c 65 6e 67 74 68 cl_ListObjLength 1af0: 28 69 70 5f 2c 20 6f 62 6a 76 5f 5b 31 5d 2c 20 (ip_, objv_[1], 1b00: 26 6d 6f 64 65 29 20 3d 3d 20 54 43 4c 5f 45 52 &mode) == TCL_ER 1b10: 52 4f 52 20 7c 7c 0a 20 20 20 20 20 20 54 63 6c ROR ||. Tcl 1b20: 5f 47 65 74 49 6e 74 46 72 6f 6d 4f 62 6a 28 69 _GetIntFromObj(i 1b30: 70 5f 2c 20 6f 62 6a 76 5f 5b 32 5d 2c 20 26 6d p_, objv_[2], &m 1b40: 6f 64 65 29 20 3d 3d 20 54 43 4c 5f 45 52 52 4f ode) == TCL_ERRO 1b50: 52 29 0a 20 20 20 20 72 65 74 75 72 6e 20 54 43 R). return TC 1b60: 4c 5f 45 52 52 4f 52 3b 0a 0a 20 20 54 63 6c 5f L_ERROR;.. Tcl_ 1b70: 4d 75 74 65 78 4c 6f 63 6b 28 26 72 65 63 68 61 MutexLock(&recha 1b80: 6e 4d 75 74 65 78 29 3b 0a 20 20 73 70 72 69 6e nMutex);. sprin 1b90: 74 66 28 62 75 66 66 65 72 2c 20 22 72 65 63 68 tf(buffer, "rech 1ba0: 61 6e 25 64 22 2c 20 2b 2b 6d 6b 43 68 61 6e 53 an%d", ++mkChanS 1bb0: 65 71 29 3b 0a 20 20 54 63 6c 5f 4d 75 74 65 78 eq);. Tcl_Mutex 1bc0: 55 6e 6c 6f 63 6b 28 26 72 65 63 68 61 6e 4d 75 Unlock(&rechanMu 1bd0: 74 65 78 29 3b 0a 0a 20 20 72 63 20 3d 20 72 63 tex);.. rc = rc 1be0: 43 72 65 61 74 65 20 28 69 70 5f 2c 20 6f 62 6a Create (ip_, obj 1bf0: 76 5f 5b 31 5d 2c 20 6d 6f 64 65 2c 20 62 75 66 v_[1], mode, buf 1c00: 66 65 72 29 3b 0a 20 20 72 63 2d 3e 5f 63 68 61 fer);. rc->_cha 1c10: 6e 20 3d 20 54 63 6c 5f 43 72 65 61 74 65 43 68 n = Tcl_CreateCh 1c20: 61 6e 6e 65 6c 28 26 72 65 43 68 61 6e 6e 65 6c annel(&reChannel 1c30: 54 79 70 65 2c 20 62 75 66 66 65 72 2c 20 28 43 Type, buffer, (C 1c40: 6c 69 65 6e 74 44 61 74 61 29 20 72 63 2c 20 6d lientData) rc, m 1c50: 6f 64 65 29 3b 0a 0a 20 20 54 63 6c 5f 52 65 67 ode);.. Tcl_Reg 1c60: 69 73 74 65 72 43 68 61 6e 6e 65 6c 28 69 70 5f isterChannel(ip_ 1c70: 2c 20 72 63 2d 3e 5f 63 68 61 6e 29 3b 0a 20 20 , rc->_chan);. 1c80: 54 63 6c 5f 53 65 74 43 68 61 6e 6e 65 6c 4f 70 Tcl_SetChannelOp 1c90: 74 69 6f 6e 28 69 70 5f 2c 20 72 63 2d 3e 5f 63 tion(ip_, rc->_c 1ca0: 68 61 6e 2c 20 22 2d 62 75 66 66 65 72 69 6e 67 han, "-buffering 1cb0: 22 2c 20 22 6e 6f 6e 65 22 29 3b 0a 20 20 54 63 ", "none");. Tc 1cc0: 6c 5f 53 65 74 43 68 61 6e 6e 65 6c 4f 70 74 69 l_SetChannelOpti 1cd0: 6f 6e 28 69 70 5f 2c 20 72 63 2d 3e 5f 63 68 61 on(ip_, rc->_cha 1ce0: 6e 2c 20 22 2d 62 6c 6f 63 6b 69 6e 67 22 2c 20 n, "-blocking", 1cf0: 22 30 22 29 3b 0a 0a 20 20 54 63 6c 5f 53 65 74 "0");.. Tcl_Set 1d00: 52 65 73 75 6c 74 28 69 70 5f 2c 20 62 75 66 66 Result(ip_, buff 1d10: 65 72 2c 20 54 43 4c 5f 56 4f 4c 41 54 49 4c 45 er, TCL_VOLATILE 1d20: 29 3b 0a 20 20 72 65 74 75 72 6e 20 54 43 4c 5f );. return TCL_ 1d30: 4f 4b 3b 0a 7d 0a 0a 44 4c 4c 45 58 50 4f 52 54 OK;.}..DLLEXPORT 1d40: 20 69 6e 74 20 52 65 63 68 61 6e 5f 49 6e 69 74 int Rechan_Init 1d50: 28 54 63 6c 5f 49 6e 74 65 72 70 2a 20 69 6e 74 (Tcl_Interp* int 1d60: 65 72 70 29 0a 7b 0a 20 20 20 20 69 66 20 28 21 erp).{. if (! 1d70: 54 63 6c 5f 49 6e 69 74 53 74 75 62 73 28 69 6e Tcl_InitStubs(in 1d80: 74 65 72 70 2c 20 22 38 2e 34 22 2c 20 30 29 29 terp, "8.4", 0)) 1d90: 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return 1da0: 54 43 4c 5f 45 52 52 4f 52 3b 0a 20 20 20 20 54 TCL_ERROR;. T 1db0: 63 6c 5f 43 72 65 61 74 65 4f 62 6a 43 6f 6d 6d cl_CreateObjComm 1dc0: 61 6e 64 28 69 6e 74 65 72 70 2c 20 22 72 65 63 and(interp, "rec 1dd0: 68 61 6e 22 2c 20 63 6d 64 5f 72 65 63 68 61 6e han", cmd_rechan 1de0: 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 72 65 74 , 0, 0);. ret 1df0: 75 72 6e 20 54 63 6c 5f 50 6b 67 50 72 6f 76 69 urn Tcl_PkgProvi 1e00: 64 65 28 69 6e 74 65 72 70 2c 20 22 72 65 63 68 de(interp, "rech 1e10: 61 6e 22 2c 20 22 31 2e 30 22 29 3b 0a 7d 0a an", "1.0");.}.