sl@0
|
1 |
/*
|
sl@0
|
2 |
* tclUnixChan.c
|
sl@0
|
3 |
*
|
sl@0
|
4 |
* Common channel driver for Unix channels based on files, command
|
sl@0
|
5 |
* pipes and TCP sockets.
|
sl@0
|
6 |
*
|
sl@0
|
7 |
* Copyright (c) 1995-1997 Sun Microsystems, Inc.
|
sl@0
|
8 |
* Copyright (c) 1998-1999 by Scriptics Corporation.
|
sl@0
|
9 |
* Portions Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiaries. All rights reserved.
|
sl@0
|
10 |
*
|
sl@0
|
11 |
* See the file "license.terms" for information on usage and redistribution
|
sl@0
|
12 |
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* RCS: @(#) $Id: tclUnixChan.c,v 1.42.2.10 2006/11/28 16:29:48 kennykb Exp $
|
sl@0
|
15 |
*/
|
sl@0
|
16 |
|
sl@0
|
17 |
#include "tclInt.h" /* Internal definitions for Tcl. */
|
sl@0
|
18 |
#include "tclPort.h" /* Portability features for Tcl. */
|
sl@0
|
19 |
#include "tclIO.h" /* To get Channel type declaration. */
|
sl@0
|
20 |
|
sl@0
|
21 |
/*
|
sl@0
|
22 |
* sys/ioctl.h has already been included by tclPort.h. Including termios.h
|
sl@0
|
23 |
* or termio.h causes a bunch of warning messages because some duplicate
|
sl@0
|
24 |
* (but not contradictory) #defines exist in termios.h and/or termio.h
|
sl@0
|
25 |
*/
|
sl@0
|
26 |
#undef NL0
|
sl@0
|
27 |
#undef NL1
|
sl@0
|
28 |
#undef CR0
|
sl@0
|
29 |
#undef CR1
|
sl@0
|
30 |
#undef CR2
|
sl@0
|
31 |
#undef CR3
|
sl@0
|
32 |
#undef TAB0
|
sl@0
|
33 |
#undef TAB1
|
sl@0
|
34 |
#undef TAB2
|
sl@0
|
35 |
#undef XTABS
|
sl@0
|
36 |
#undef BS0
|
sl@0
|
37 |
#undef BS1
|
sl@0
|
38 |
#undef FF0
|
sl@0
|
39 |
#undef FF1
|
sl@0
|
40 |
#undef ECHO
|
sl@0
|
41 |
#undef NOFLSH
|
sl@0
|
42 |
#undef TOSTOP
|
sl@0
|
43 |
#undef FLUSHO
|
sl@0
|
44 |
#undef PENDIN
|
sl@0
|
45 |
|
sl@0
|
46 |
#define SUPPORTS_TTY
|
sl@0
|
47 |
|
sl@0
|
48 |
#ifdef USE_TERMIOS
|
sl@0
|
49 |
# include <termios.h>
|
sl@0
|
50 |
# ifdef HAVE_SYS_IOCTL_H
|
sl@0
|
51 |
# include <sys/ioctl.h>
|
sl@0
|
52 |
# endif /* HAVE_SYS_IOCTL_H */
|
sl@0
|
53 |
# ifdef HAVE_SYS_MODEM_H
|
sl@0
|
54 |
# include <sys/modem.h>
|
sl@0
|
55 |
# endif /* HAVE_SYS_MODEM_H */
|
sl@0
|
56 |
# define IOSTATE struct termios
|
sl@0
|
57 |
# define GETIOSTATE(fd, statePtr) tcgetattr((fd), (statePtr))
|
sl@0
|
58 |
# define SETIOSTATE(fd, statePtr) tcsetattr((fd), TCSADRAIN, (statePtr))
|
sl@0
|
59 |
# define GETCONTROL(fd, intPtr) ioctl((fd), TIOCMGET, (intPtr))
|
sl@0
|
60 |
# define SETCONTROL(fd, intPtr) ioctl((fd), TIOCMSET, (intPtr))
|
sl@0
|
61 |
/*
|
sl@0
|
62 |
* TIP #35 introduced a different on exit flush/close behavior that
|
sl@0
|
63 |
* doesn't work correctly with standard channels on all systems.
|
sl@0
|
64 |
* The problem is tcflush throws away waiting channel data. This may
|
sl@0
|
65 |
* be necessary for true serial channels that may block, but isn't
|
sl@0
|
66 |
* correct in the standard case. This might be replaced with tcdrain
|
sl@0
|
67 |
* instead, but that can block. For now, we revert to making this do
|
sl@0
|
68 |
* nothing, and TtyOutputProc being the same old FileOutputProc.
|
sl@0
|
69 |
* -- hobbs [Bug #525783]
|
sl@0
|
70 |
*/
|
sl@0
|
71 |
# define BAD_TIP35_FLUSH 0
|
sl@0
|
72 |
# if BAD_TIP35_FLUSH
|
sl@0
|
73 |
# define TTYFLUSH(fd) tcflush((fd), TCIOFLUSH);
|
sl@0
|
74 |
# else
|
sl@0
|
75 |
# define TTYFLUSH(fd)
|
sl@0
|
76 |
# endif /* BAD_TIP35_FLUSH */
|
sl@0
|
77 |
# ifdef FIONREAD
|
sl@0
|
78 |
# define GETREADQUEUE(fd, int) ioctl((fd), FIONREAD, &(int))
|
sl@0
|
79 |
# elif defined(FIORDCHK)
|
sl@0
|
80 |
# define GETREADQUEUE(fd, int) int = ioctl((fd), FIORDCHK, NULL)
|
sl@0
|
81 |
# endif /* FIONREAD */
|
sl@0
|
82 |
# ifdef TIOCOUTQ
|
sl@0
|
83 |
# define GETWRITEQUEUE(fd, int) ioctl((fd), TIOCOUTQ, &(int))
|
sl@0
|
84 |
# endif /* TIOCOUTQ */
|
sl@0
|
85 |
# if defined(TIOCSBRK) && defined(TIOCCBRK)
|
sl@0
|
86 |
/*
|
sl@0
|
87 |
* Can't use ?: operator below because that messes up types on either
|
sl@0
|
88 |
* Linux or Solaris (the two are mutually exclusive!)
|
sl@0
|
89 |
*/
|
sl@0
|
90 |
# define SETBREAK(fd, flag) \
|
sl@0
|
91 |
if (flag) { \
|
sl@0
|
92 |
ioctl((fd), TIOCSBRK, NULL); \
|
sl@0
|
93 |
} else { \
|
sl@0
|
94 |
ioctl((fd), TIOCCBRK, NULL); \
|
sl@0
|
95 |
}
|
sl@0
|
96 |
# endif /* TIOCSBRK&TIOCCBRK */
|
sl@0
|
97 |
# if !defined(CRTSCTS) && defined(CNEW_RTSCTS)
|
sl@0
|
98 |
# define CRTSCTS CNEW_RTSCTS
|
sl@0
|
99 |
# endif /* !CRTSCTS&CNEW_RTSCTS */
|
sl@0
|
100 |
#else /* !USE_TERMIOS */
|
sl@0
|
101 |
|
sl@0
|
102 |
#ifdef USE_TERMIO
|
sl@0
|
103 |
# include <termio.h>
|
sl@0
|
104 |
# define IOSTATE struct termio
|
sl@0
|
105 |
# define GETIOSTATE(fd, statePtr) ioctl((fd), TCGETA, (statePtr))
|
sl@0
|
106 |
# define SETIOSTATE(fd, statePtr) ioctl((fd), TCSETAW, (statePtr))
|
sl@0
|
107 |
#else /* !USE_TERMIO */
|
sl@0
|
108 |
|
sl@0
|
109 |
#ifdef USE_SGTTY
|
sl@0
|
110 |
# include <sgtty.h>
|
sl@0
|
111 |
# define IOSTATE struct sgttyb
|
sl@0
|
112 |
# define GETIOSTATE(fd, statePtr) ioctl((fd), TIOCGETP, (statePtr))
|
sl@0
|
113 |
# define SETIOSTATE(fd, statePtr) ioctl((fd), TIOCSETP, (statePtr))
|
sl@0
|
114 |
#else /* !USE_SGTTY */
|
sl@0
|
115 |
# undef SUPPORTS_TTY
|
sl@0
|
116 |
#endif /* !USE_SGTTY */
|
sl@0
|
117 |
|
sl@0
|
118 |
#endif /* !USE_TERMIO */
|
sl@0
|
119 |
#endif /* !USE_TERMIOS */
|
sl@0
|
120 |
|
sl@0
|
121 |
/*
|
sl@0
|
122 |
* This structure describes per-instance state of a file based channel.
|
sl@0
|
123 |
*/
|
sl@0
|
124 |
|
sl@0
|
125 |
typedef struct FileState {
|
sl@0
|
126 |
Tcl_Channel channel; /* Channel associated with this file. */
|
sl@0
|
127 |
int fd; /* File handle. */
|
sl@0
|
128 |
int validMask; /* OR'ed combination of TCL_READABLE,
|
sl@0
|
129 |
* TCL_WRITABLE, or TCL_EXCEPTION: indicates
|
sl@0
|
130 |
* which operations are valid on the file. */
|
sl@0
|
131 |
#ifdef DEPRECATED
|
sl@0
|
132 |
struct FileState *nextPtr; /* Pointer to next file in list of all
|
sl@0
|
133 |
* file channels. */
|
sl@0
|
134 |
#endif /* DEPRECATED */
|
sl@0
|
135 |
} FileState;
|
sl@0
|
136 |
|
sl@0
|
137 |
#ifdef SUPPORTS_TTY
|
sl@0
|
138 |
|
sl@0
|
139 |
/*
|
sl@0
|
140 |
* The following structure describes per-instance state of a tty-based
|
sl@0
|
141 |
* channel.
|
sl@0
|
142 |
*/
|
sl@0
|
143 |
|
sl@0
|
144 |
typedef struct TtyState {
|
sl@0
|
145 |
FileState fs; /* Per-instance state of the file
|
sl@0
|
146 |
* descriptor. Must be the first field. */
|
sl@0
|
147 |
int stateUpdated; /* Flag to say if the state has been
|
sl@0
|
148 |
* modified and needs resetting. */
|
sl@0
|
149 |
IOSTATE savedState; /* Initial state of device. Used to reset
|
sl@0
|
150 |
* state when device closed. */
|
sl@0
|
151 |
} TtyState;
|
sl@0
|
152 |
|
sl@0
|
153 |
/*
|
sl@0
|
154 |
* The following structure is used to set or get the serial port
|
sl@0
|
155 |
* attributes in a platform-independant manner.
|
sl@0
|
156 |
*/
|
sl@0
|
157 |
|
sl@0
|
158 |
typedef struct TtyAttrs {
|
sl@0
|
159 |
int baud;
|
sl@0
|
160 |
int parity;
|
sl@0
|
161 |
int data;
|
sl@0
|
162 |
int stop;
|
sl@0
|
163 |
} TtyAttrs;
|
sl@0
|
164 |
|
sl@0
|
165 |
#endif /* !SUPPORTS_TTY */
|
sl@0
|
166 |
|
sl@0
|
167 |
#define UNSUPPORTED_OPTION(detail) \
|
sl@0
|
168 |
if (interp) { \
|
sl@0
|
169 |
Tcl_AppendResult(interp, (detail), \
|
sl@0
|
170 |
" not supported for this platform", (char *) NULL); \
|
sl@0
|
171 |
}
|
sl@0
|
172 |
|
sl@0
|
173 |
#ifdef DEPRECATED
|
sl@0
|
174 |
typedef struct ThreadSpecificData {
|
sl@0
|
175 |
/*
|
sl@0
|
176 |
* List of all file channels currently open. This is per thread and is
|
sl@0
|
177 |
* used to match up fd's to channels, which rarely occurs.
|
sl@0
|
178 |
*/
|
sl@0
|
179 |
|
sl@0
|
180 |
FileState *firstFilePtr;
|
sl@0
|
181 |
} ThreadSpecificData;
|
sl@0
|
182 |
|
sl@0
|
183 |
static Tcl_ThreadDataKey dataKey;
|
sl@0
|
184 |
#endif /* DEPRECATED */
|
sl@0
|
185 |
|
sl@0
|
186 |
/*
|
sl@0
|
187 |
* This structure describes per-instance state of a tcp based channel.
|
sl@0
|
188 |
*/
|
sl@0
|
189 |
|
sl@0
|
190 |
typedef struct TcpState {
|
sl@0
|
191 |
Tcl_Channel channel; /* Channel associated with this file. */
|
sl@0
|
192 |
int fd; /* The socket itself. */
|
sl@0
|
193 |
int flags; /* ORed combination of the bitfields
|
sl@0
|
194 |
* defined below. */
|
sl@0
|
195 |
Tcl_TcpAcceptProc *acceptProc;
|
sl@0
|
196 |
/* Proc to call on accept. */
|
sl@0
|
197 |
ClientData acceptProcData; /* The data for the accept proc. */
|
sl@0
|
198 |
} TcpState;
|
sl@0
|
199 |
|
sl@0
|
200 |
/*
|
sl@0
|
201 |
* These bits may be ORed together into the "flags" field of a TcpState
|
sl@0
|
202 |
* structure.
|
sl@0
|
203 |
*/
|
sl@0
|
204 |
|
sl@0
|
205 |
#define TCP_ASYNC_SOCKET (1<<0) /* Asynchronous socket. */
|
sl@0
|
206 |
#define TCP_ASYNC_CONNECT (1<<1) /* Async connect in progress. */
|
sl@0
|
207 |
|
sl@0
|
208 |
/*
|
sl@0
|
209 |
* The following defines the maximum length of the listen queue. This is
|
sl@0
|
210 |
* the number of outstanding yet-to-be-serviced requests for a connection
|
sl@0
|
211 |
* on a server socket, more than this number of outstanding requests and
|
sl@0
|
212 |
* the connection request will fail.
|
sl@0
|
213 |
*/
|
sl@0
|
214 |
|
sl@0
|
215 |
#ifndef SOMAXCONN
|
sl@0
|
216 |
# define SOMAXCONN 100
|
sl@0
|
217 |
#endif /* SOMAXCONN */
|
sl@0
|
218 |
|
sl@0
|
219 |
#if (SOMAXCONN < 100)
|
sl@0
|
220 |
# undef SOMAXCONN
|
sl@0
|
221 |
# define SOMAXCONN 100
|
sl@0
|
222 |
#endif /* SOMAXCONN < 100 */
|
sl@0
|
223 |
|
sl@0
|
224 |
/*
|
sl@0
|
225 |
* The following defines how much buffer space the kernel should maintain
|
sl@0
|
226 |
* for a socket.
|
sl@0
|
227 |
*/
|
sl@0
|
228 |
|
sl@0
|
229 |
#define SOCKET_BUFSIZE 4096
|
sl@0
|
230 |
|
sl@0
|
231 |
/*
|
sl@0
|
232 |
* Static routines for this file:
|
sl@0
|
233 |
*/
|
sl@0
|
234 |
|
sl@0
|
235 |
static TcpState * CreateSocket _ANSI_ARGS_((Tcl_Interp *interp,
|
sl@0
|
236 |
int port, CONST char *host, int server,
|
sl@0
|
237 |
CONST char *myaddr, int myport, int async));
|
sl@0
|
238 |
static int CreateSocketAddress _ANSI_ARGS_(
|
sl@0
|
239 |
(struct sockaddr_in *sockaddrPtr,
|
sl@0
|
240 |
CONST char *host, int port));
|
sl@0
|
241 |
static int FileBlockModeProc _ANSI_ARGS_((
|
sl@0
|
242 |
ClientData instanceData, int mode));
|
sl@0
|
243 |
static int FileCloseProc _ANSI_ARGS_((ClientData instanceData,
|
sl@0
|
244 |
Tcl_Interp *interp));
|
sl@0
|
245 |
static int FileGetHandleProc _ANSI_ARGS_((ClientData instanceData,
|
sl@0
|
246 |
int direction, ClientData *handlePtr));
|
sl@0
|
247 |
static int FileInputProc _ANSI_ARGS_((ClientData instanceData,
|
sl@0
|
248 |
char *buf, int toRead, int *errorCode));
|
sl@0
|
249 |
static int FileOutputProc _ANSI_ARGS_((
|
sl@0
|
250 |
ClientData instanceData, CONST char *buf,
|
sl@0
|
251 |
int toWrite, int *errorCode));
|
sl@0
|
252 |
static int FileSeekProc _ANSI_ARGS_((ClientData instanceData,
|
sl@0
|
253 |
long offset, int mode, int *errorCode));
|
sl@0
|
254 |
#ifdef DEPRECATED
|
sl@0
|
255 |
static void FileThreadActionProc _ANSI_ARGS_ ((
|
sl@0
|
256 |
ClientData instanceData, int action));
|
sl@0
|
257 |
#endif
|
sl@0
|
258 |
static Tcl_WideInt FileWideSeekProc _ANSI_ARGS_((ClientData instanceData,
|
sl@0
|
259 |
Tcl_WideInt offset, int mode, int *errorCode));
|
sl@0
|
260 |
static void FileWatchProc _ANSI_ARGS_((ClientData instanceData,
|
sl@0
|
261 |
int mask));
|
sl@0
|
262 |
static void TcpAccept _ANSI_ARGS_((ClientData data, int mask));
|
sl@0
|
263 |
static int TcpBlockModeProc _ANSI_ARGS_((ClientData data,
|
sl@0
|
264 |
int mode));
|
sl@0
|
265 |
static int TcpCloseProc _ANSI_ARGS_((ClientData instanceData,
|
sl@0
|
266 |
Tcl_Interp *interp));
|
sl@0
|
267 |
static int TcpGetHandleProc _ANSI_ARGS_((ClientData instanceData,
|
sl@0
|
268 |
int direction, ClientData *handlePtr));
|
sl@0
|
269 |
static int TcpGetOptionProc _ANSI_ARGS_((ClientData instanceData,
|
sl@0
|
270 |
Tcl_Interp *interp, CONST char *optionName,
|
sl@0
|
271 |
Tcl_DString *dsPtr));
|
sl@0
|
272 |
static int TcpInputProc _ANSI_ARGS_((ClientData instanceData,
|
sl@0
|
273 |
char *buf, int toRead, int *errorCode));
|
sl@0
|
274 |
static int TcpOutputProc _ANSI_ARGS_((ClientData instanceData,
|
sl@0
|
275 |
CONST char *buf, int toWrite, int *errorCode));
|
sl@0
|
276 |
static void TcpWatchProc _ANSI_ARGS_((ClientData instanceData,
|
sl@0
|
277 |
int mask));
|
sl@0
|
278 |
#ifdef SUPPORTS_TTY
|
sl@0
|
279 |
static int TtyCloseProc _ANSI_ARGS_((ClientData instanceData,
|
sl@0
|
280 |
Tcl_Interp *interp));
|
sl@0
|
281 |
static void TtyGetAttributes _ANSI_ARGS_((int fd,
|
sl@0
|
282 |
TtyAttrs *ttyPtr));
|
sl@0
|
283 |
static int TtyGetOptionProc _ANSI_ARGS_((ClientData instanceData,
|
sl@0
|
284 |
Tcl_Interp *interp, CONST char *optionName,
|
sl@0
|
285 |
Tcl_DString *dsPtr));
|
sl@0
|
286 |
static FileState * TtyInit _ANSI_ARGS_((int fd, int initialize));
|
sl@0
|
287 |
#if BAD_TIP35_FLUSH
|
sl@0
|
288 |
static int TtyOutputProc _ANSI_ARGS_((ClientData instanceData,
|
sl@0
|
289 |
CONST char *buf, int toWrite, int *errorCode));
|
sl@0
|
290 |
#endif /* BAD_TIP35_FLUSH */
|
sl@0
|
291 |
static int TtyParseMode _ANSI_ARGS_((Tcl_Interp *interp,
|
sl@0
|
292 |
CONST char *mode, int *speedPtr, int *parityPtr,
|
sl@0
|
293 |
int *dataPtr, int *stopPtr));
|
sl@0
|
294 |
static void TtySetAttributes _ANSI_ARGS_((int fd,
|
sl@0
|
295 |
TtyAttrs *ttyPtr));
|
sl@0
|
296 |
static int TtySetOptionProc _ANSI_ARGS_((ClientData instanceData,
|
sl@0
|
297 |
Tcl_Interp *interp, CONST char *optionName,
|
sl@0
|
298 |
CONST char *value));
|
sl@0
|
299 |
#endif /* SUPPORTS_TTY */
|
sl@0
|
300 |
static int WaitForConnect _ANSI_ARGS_((TcpState *statePtr,
|
sl@0
|
301 |
int *errorCodePtr));
|
sl@0
|
302 |
static Tcl_Channel MakeTcpClientChannelMode _ANSI_ARGS_(
|
sl@0
|
303 |
(ClientData tcpSocket,
|
sl@0
|
304 |
int mode));
|
sl@0
|
305 |
|
sl@0
|
306 |
|
sl@0
|
307 |
/*
|
sl@0
|
308 |
* This structure describes the channel type structure for file based IO:
|
sl@0
|
309 |
*/
|
sl@0
|
310 |
|
sl@0
|
311 |
static Tcl_ChannelType fileChannelType = {
|
sl@0
|
312 |
"file", /* Type name. */
|
sl@0
|
313 |
TCL_CHANNEL_VERSION_4, /* v4 channel */
|
sl@0
|
314 |
FileCloseProc, /* Close proc. */
|
sl@0
|
315 |
FileInputProc, /* Input proc. */
|
sl@0
|
316 |
FileOutputProc, /* Output proc. */
|
sl@0
|
317 |
FileSeekProc, /* Seek proc. */
|
sl@0
|
318 |
NULL, /* Set option proc. */
|
sl@0
|
319 |
NULL, /* Get option proc. */
|
sl@0
|
320 |
FileWatchProc, /* Initialize notifier. */
|
sl@0
|
321 |
FileGetHandleProc, /* Get OS handles out of channel. */
|
sl@0
|
322 |
NULL, /* close2proc. */
|
sl@0
|
323 |
FileBlockModeProc, /* Set blocking or non-blocking mode.*/
|
sl@0
|
324 |
NULL, /* flush proc. */
|
sl@0
|
325 |
NULL, /* handler proc. */
|
sl@0
|
326 |
FileWideSeekProc, /* wide seek proc. */
|
sl@0
|
327 |
#ifdef DEPRECATED
|
sl@0
|
328 |
FileThreadActionProc, /* thread actions */
|
sl@0
|
329 |
#else
|
sl@0
|
330 |
NULL,
|
sl@0
|
331 |
#endif
|
sl@0
|
332 |
};
|
sl@0
|
333 |
|
sl@0
|
334 |
#ifdef SUPPORTS_TTY
|
sl@0
|
335 |
/*
|
sl@0
|
336 |
* This structure describes the channel type structure for serial IO.
|
sl@0
|
337 |
* Note that this type is a subclass of the "file" type.
|
sl@0
|
338 |
*/
|
sl@0
|
339 |
|
sl@0
|
340 |
static Tcl_ChannelType ttyChannelType = {
|
sl@0
|
341 |
"tty", /* Type name. */
|
sl@0
|
342 |
TCL_CHANNEL_VERSION_4, /* v4 channel */
|
sl@0
|
343 |
TtyCloseProc, /* Close proc. */
|
sl@0
|
344 |
FileInputProc, /* Input proc. */
|
sl@0
|
345 |
#if BAD_TIP35_FLUSH
|
sl@0
|
346 |
TtyOutputProc, /* Output proc. */
|
sl@0
|
347 |
#else /* !BAD_TIP35_FLUSH */
|
sl@0
|
348 |
FileOutputProc, /* Output proc. */
|
sl@0
|
349 |
#endif /* BAD_TIP35_FLUSH */
|
sl@0
|
350 |
NULL, /* Seek proc. */
|
sl@0
|
351 |
TtySetOptionProc, /* Set option proc. */
|
sl@0
|
352 |
TtyGetOptionProc, /* Get option proc. */
|
sl@0
|
353 |
FileWatchProc, /* Initialize notifier. */
|
sl@0
|
354 |
FileGetHandleProc, /* Get OS handles out of channel. */
|
sl@0
|
355 |
NULL, /* close2proc. */
|
sl@0
|
356 |
FileBlockModeProc, /* Set blocking or non-blocking mode.*/
|
sl@0
|
357 |
NULL, /* flush proc. */
|
sl@0
|
358 |
NULL, /* handler proc. */
|
sl@0
|
359 |
NULL, /* wide seek proc. */
|
sl@0
|
360 |
NULL, /* thread action proc. */
|
sl@0
|
361 |
};
|
sl@0
|
362 |
#endif /* SUPPORTS_TTY */
|
sl@0
|
363 |
|
sl@0
|
364 |
/*
|
sl@0
|
365 |
* This structure describes the channel type structure for TCP socket
|
sl@0
|
366 |
* based IO:
|
sl@0
|
367 |
*/
|
sl@0
|
368 |
|
sl@0
|
369 |
static Tcl_ChannelType tcpChannelType = {
|
sl@0
|
370 |
"tcp", /* Type name. */
|
sl@0
|
371 |
TCL_CHANNEL_VERSION_4, /* v4 channel */
|
sl@0
|
372 |
TcpCloseProc, /* Close proc. */
|
sl@0
|
373 |
TcpInputProc, /* Input proc. */
|
sl@0
|
374 |
TcpOutputProc, /* Output proc. */
|
sl@0
|
375 |
NULL, /* Seek proc. */
|
sl@0
|
376 |
NULL, /* Set option proc. */
|
sl@0
|
377 |
TcpGetOptionProc, /* Get option proc. */
|
sl@0
|
378 |
TcpWatchProc, /* Initialize notifier. */
|
sl@0
|
379 |
TcpGetHandleProc, /* Get OS handles out of channel. */
|
sl@0
|
380 |
NULL, /* close2proc. */
|
sl@0
|
381 |
TcpBlockModeProc, /* Set blocking or non-blocking mode.*/
|
sl@0
|
382 |
NULL, /* flush proc. */
|
sl@0
|
383 |
NULL, /* handler proc. */
|
sl@0
|
384 |
NULL, /* wide seek proc. */
|
sl@0
|
385 |
NULL, /* thread action proc. */
|
sl@0
|
386 |
};
|
sl@0
|
387 |
|
sl@0
|
388 |
|
sl@0
|
389 |
/*
|
sl@0
|
390 |
*----------------------------------------------------------------------
|
sl@0
|
391 |
*
|
sl@0
|
392 |
* FileBlockModeProc --
|
sl@0
|
393 |
*
|
sl@0
|
394 |
* Helper procedure to set blocking and nonblocking modes on a
|
sl@0
|
395 |
* file based channel. Invoked by generic IO level code.
|
sl@0
|
396 |
*
|
sl@0
|
397 |
* Results:
|
sl@0
|
398 |
* 0 if successful, errno when failed.
|
sl@0
|
399 |
*
|
sl@0
|
400 |
* Side effects:
|
sl@0
|
401 |
* Sets the device into blocking or non-blocking mode.
|
sl@0
|
402 |
*
|
sl@0
|
403 |
*----------------------------------------------------------------------
|
sl@0
|
404 |
*/
|
sl@0
|
405 |
|
sl@0
|
406 |
/* ARGSUSED */
|
sl@0
|
407 |
static int
|
sl@0
|
408 |
FileBlockModeProc(instanceData, mode)
|
sl@0
|
409 |
ClientData instanceData; /* File state. */
|
sl@0
|
410 |
int mode; /* The mode to set. Can be one of
|
sl@0
|
411 |
* TCL_MODE_BLOCKING or
|
sl@0
|
412 |
* TCL_MODE_NONBLOCKING. */
|
sl@0
|
413 |
{
|
sl@0
|
414 |
FileState *fsPtr = (FileState *) instanceData;
|
sl@0
|
415 |
int curStatus;
|
sl@0
|
416 |
|
sl@0
|
417 |
#ifndef USE_FIONBIO
|
sl@0
|
418 |
curStatus = fcntl(fsPtr->fd, F_GETFL);
|
sl@0
|
419 |
if (mode == TCL_MODE_BLOCKING) {
|
sl@0
|
420 |
curStatus &= (~(O_NONBLOCK));
|
sl@0
|
421 |
} else {
|
sl@0
|
422 |
curStatus |= O_NONBLOCK;
|
sl@0
|
423 |
}
|
sl@0
|
424 |
if (fcntl(fsPtr->fd, F_SETFL, curStatus) < 0) {
|
sl@0
|
425 |
return errno;
|
sl@0
|
426 |
}
|
sl@0
|
427 |
curStatus = fcntl(fsPtr->fd, F_GETFL);
|
sl@0
|
428 |
#else /* USE_FIONBIO */
|
sl@0
|
429 |
if (mode == TCL_MODE_BLOCKING) {
|
sl@0
|
430 |
curStatus = 0;
|
sl@0
|
431 |
} else {
|
sl@0
|
432 |
curStatus = 1;
|
sl@0
|
433 |
}
|
sl@0
|
434 |
if (ioctl(fsPtr->fd, (int) FIONBIO, &curStatus) < 0) {
|
sl@0
|
435 |
return errno;
|
sl@0
|
436 |
}
|
sl@0
|
437 |
#endif /* !USE_FIONBIO */
|
sl@0
|
438 |
return 0;
|
sl@0
|
439 |
}
|
sl@0
|
440 |
|
sl@0
|
441 |
/*
|
sl@0
|
442 |
*----------------------------------------------------------------------
|
sl@0
|
443 |
*
|
sl@0
|
444 |
* FileInputProc --
|
sl@0
|
445 |
*
|
sl@0
|
446 |
* This procedure is invoked from the generic IO level to read
|
sl@0
|
447 |
* input from a file based channel.
|
sl@0
|
448 |
*
|
sl@0
|
449 |
* Results:
|
sl@0
|
450 |
* The number of bytes read is returned or -1 on error. An output
|
sl@0
|
451 |
* argument contains a POSIX error code if an error occurs, or zero.
|
sl@0
|
452 |
*
|
sl@0
|
453 |
* Side effects:
|
sl@0
|
454 |
* Reads input from the input device of the channel.
|
sl@0
|
455 |
*
|
sl@0
|
456 |
*----------------------------------------------------------------------
|
sl@0
|
457 |
*/
|
sl@0
|
458 |
|
sl@0
|
459 |
static int
|
sl@0
|
460 |
FileInputProc(instanceData, buf, toRead, errorCodePtr)
|
sl@0
|
461 |
ClientData instanceData; /* File state. */
|
sl@0
|
462 |
char *buf; /* Where to store data read. */
|
sl@0
|
463 |
int toRead; /* How much space is available
|
sl@0
|
464 |
* in the buffer? */
|
sl@0
|
465 |
int *errorCodePtr; /* Where to store error code. */
|
sl@0
|
466 |
{
|
sl@0
|
467 |
FileState *fsPtr = (FileState *) instanceData;
|
sl@0
|
468 |
int bytesRead; /* How many bytes were actually
|
sl@0
|
469 |
* read from the input device? */
|
sl@0
|
470 |
|
sl@0
|
471 |
*errorCodePtr = 0;
|
sl@0
|
472 |
|
sl@0
|
473 |
/*
|
sl@0
|
474 |
* Assume there is always enough input available. This will block
|
sl@0
|
475 |
* appropriately, and read will unblock as soon as a short read is
|
sl@0
|
476 |
* possible, if the channel is in blocking mode. If the channel is
|
sl@0
|
477 |
* nonblocking, the read will never block.
|
sl@0
|
478 |
*/
|
sl@0
|
479 |
|
sl@0
|
480 |
bytesRead = read(fsPtr->fd, buf, (size_t) toRead);
|
sl@0
|
481 |
if (bytesRead > -1) {
|
sl@0
|
482 |
return bytesRead;
|
sl@0
|
483 |
}
|
sl@0
|
484 |
*errorCodePtr = errno;
|
sl@0
|
485 |
return -1;
|
sl@0
|
486 |
}
|
sl@0
|
487 |
|
sl@0
|
488 |
/*
|
sl@0
|
489 |
*----------------------------------------------------------------------
|
sl@0
|
490 |
*
|
sl@0
|
491 |
* FileOutputProc--
|
sl@0
|
492 |
*
|
sl@0
|
493 |
* This procedure is invoked from the generic IO level to write
|
sl@0
|
494 |
* output to a file channel.
|
sl@0
|
495 |
*
|
sl@0
|
496 |
* Results:
|
sl@0
|
497 |
* The number of bytes written is returned or -1 on error. An
|
sl@0
|
498 |
* output argument contains a POSIX error code if an error occurred,
|
sl@0
|
499 |
* or zero.
|
sl@0
|
500 |
*
|
sl@0
|
501 |
* Side effects:
|
sl@0
|
502 |
* Writes output on the output device of the channel.
|
sl@0
|
503 |
*
|
sl@0
|
504 |
*----------------------------------------------------------------------
|
sl@0
|
505 |
*/
|
sl@0
|
506 |
|
sl@0
|
507 |
static int
|
sl@0
|
508 |
FileOutputProc(instanceData, buf, toWrite, errorCodePtr)
|
sl@0
|
509 |
ClientData instanceData; /* File state. */
|
sl@0
|
510 |
CONST char *buf; /* The data buffer. */
|
sl@0
|
511 |
int toWrite; /* How many bytes to write? */
|
sl@0
|
512 |
int *errorCodePtr; /* Where to store error code. */
|
sl@0
|
513 |
{
|
sl@0
|
514 |
FileState *fsPtr = (FileState *) instanceData;
|
sl@0
|
515 |
int written;
|
sl@0
|
516 |
|
sl@0
|
517 |
*errorCodePtr = 0;
|
sl@0
|
518 |
|
sl@0
|
519 |
if (toWrite == 0) {
|
sl@0
|
520 |
/*
|
sl@0
|
521 |
* SF Tcl Bug 465765.
|
sl@0
|
522 |
* Do not try to write nothing into a file. STREAM based
|
sl@0
|
523 |
* implementations will considers this as EOF (if there is a
|
sl@0
|
524 |
* pipe behind the file).
|
sl@0
|
525 |
*/
|
sl@0
|
526 |
|
sl@0
|
527 |
return 0;
|
sl@0
|
528 |
}
|
sl@0
|
529 |
written = write(fsPtr->fd, buf, (size_t) toWrite);
|
sl@0
|
530 |
if (written > -1) {
|
sl@0
|
531 |
return written;
|
sl@0
|
532 |
}
|
sl@0
|
533 |
*errorCodePtr = errno;
|
sl@0
|
534 |
return -1;
|
sl@0
|
535 |
}
|
sl@0
|
536 |
|
sl@0
|
537 |
/*
|
sl@0
|
538 |
*----------------------------------------------------------------------
|
sl@0
|
539 |
*
|
sl@0
|
540 |
* FileCloseProc --
|
sl@0
|
541 |
*
|
sl@0
|
542 |
* This procedure is called from the generic IO level to perform
|
sl@0
|
543 |
* channel-type-specific cleanup when a file based channel is closed.
|
sl@0
|
544 |
*
|
sl@0
|
545 |
* Results:
|
sl@0
|
546 |
* 0 if successful, errno if failed.
|
sl@0
|
547 |
*
|
sl@0
|
548 |
* Side effects:
|
sl@0
|
549 |
* Closes the device of the channel.
|
sl@0
|
550 |
*
|
sl@0
|
551 |
*----------------------------------------------------------------------
|
sl@0
|
552 |
*/
|
sl@0
|
553 |
|
sl@0
|
554 |
static int
|
sl@0
|
555 |
FileCloseProc(instanceData, interp)
|
sl@0
|
556 |
ClientData instanceData; /* File state. */
|
sl@0
|
557 |
Tcl_Interp *interp; /* For error reporting - unused. */
|
sl@0
|
558 |
{
|
sl@0
|
559 |
FileState *fsPtr = (FileState *) instanceData;
|
sl@0
|
560 |
int errorCode = 0;
|
sl@0
|
561 |
#ifdef DEPRECATED
|
sl@0
|
562 |
FileState **nextPtrPtr;
|
sl@0
|
563 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
|
sl@0
|
564 |
#endif /* DEPRECATED */
|
sl@0
|
565 |
Tcl_DeleteFileHandler(fsPtr->fd);
|
sl@0
|
566 |
|
sl@0
|
567 |
/*
|
sl@0
|
568 |
* Do not close standard channels while in thread-exit.
|
sl@0
|
569 |
*/
|
sl@0
|
570 |
|
sl@0
|
571 |
if (!TclInThreadExit()
|
sl@0
|
572 |
|| ((fsPtr->fd != 0) && (fsPtr->fd != 1) && (fsPtr->fd != 2))) {
|
sl@0
|
573 |
if (close(fsPtr->fd) < 0) {
|
sl@0
|
574 |
errorCode = errno;
|
sl@0
|
575 |
}
|
sl@0
|
576 |
}
|
sl@0
|
577 |
ckfree((char *) fsPtr);
|
sl@0
|
578 |
return errorCode;
|
sl@0
|
579 |
}
|
sl@0
|
580 |
|
sl@0
|
581 |
/*
|
sl@0
|
582 |
*----------------------------------------------------------------------
|
sl@0
|
583 |
*
|
sl@0
|
584 |
* FileSeekProc --
|
sl@0
|
585 |
*
|
sl@0
|
586 |
* This procedure is called by the generic IO level to move the
|
sl@0
|
587 |
* access point in a file based channel.
|
sl@0
|
588 |
*
|
sl@0
|
589 |
* Results:
|
sl@0
|
590 |
* -1 if failed, the new position if successful. An output
|
sl@0
|
591 |
* argument contains the POSIX error code if an error occurred,
|
sl@0
|
592 |
* or zero.
|
sl@0
|
593 |
*
|
sl@0
|
594 |
* Side effects:
|
sl@0
|
595 |
* Moves the location at which the channel will be accessed in
|
sl@0
|
596 |
* future operations.
|
sl@0
|
597 |
*
|
sl@0
|
598 |
*----------------------------------------------------------------------
|
sl@0
|
599 |
*/
|
sl@0
|
600 |
|
sl@0
|
601 |
static int
|
sl@0
|
602 |
FileSeekProc(instanceData, offset, mode, errorCodePtr)
|
sl@0
|
603 |
ClientData instanceData; /* File state. */
|
sl@0
|
604 |
long offset; /* Offset to seek to. */
|
sl@0
|
605 |
int mode; /* Relative to where should we seek? Can be
|
sl@0
|
606 |
* one of SEEK_START, SEEK_SET or SEEK_END. */
|
sl@0
|
607 |
int *errorCodePtr; /* To store error code. */
|
sl@0
|
608 |
{
|
sl@0
|
609 |
FileState *fsPtr = (FileState *) instanceData;
|
sl@0
|
610 |
Tcl_WideInt oldLoc, newLoc;
|
sl@0
|
611 |
|
sl@0
|
612 |
/*
|
sl@0
|
613 |
* Save our current place in case we need to roll-back the seek.
|
sl@0
|
614 |
*/
|
sl@0
|
615 |
oldLoc = TclOSseek(fsPtr->fd, (Tcl_SeekOffset) 0, SEEK_CUR);
|
sl@0
|
616 |
if (oldLoc == Tcl_LongAsWide(-1)) {
|
sl@0
|
617 |
/*
|
sl@0
|
618 |
* Bad things are happening. Error out...
|
sl@0
|
619 |
*/
|
sl@0
|
620 |
*errorCodePtr = errno;
|
sl@0
|
621 |
return -1;
|
sl@0
|
622 |
}
|
sl@0
|
623 |
|
sl@0
|
624 |
newLoc = TclOSseek(fsPtr->fd, (Tcl_SeekOffset) offset, mode);
|
sl@0
|
625 |
|
sl@0
|
626 |
/*
|
sl@0
|
627 |
* Check for expressability in our return type, and roll-back otherwise.
|
sl@0
|
628 |
*/
|
sl@0
|
629 |
if (newLoc > Tcl_LongAsWide(INT_MAX)) {
|
sl@0
|
630 |
*errorCodePtr = EOVERFLOW;
|
sl@0
|
631 |
TclOSseek(fsPtr->fd, (Tcl_SeekOffset) oldLoc, SEEK_SET);
|
sl@0
|
632 |
return -1;
|
sl@0
|
633 |
} else {
|
sl@0
|
634 |
*errorCodePtr = (newLoc == Tcl_LongAsWide(-1)) ? errno : 0;
|
sl@0
|
635 |
}
|
sl@0
|
636 |
return (int) Tcl_WideAsLong(newLoc);
|
sl@0
|
637 |
}
|
sl@0
|
638 |
|
sl@0
|
639 |
/*
|
sl@0
|
640 |
*----------------------------------------------------------------------
|
sl@0
|
641 |
*
|
sl@0
|
642 |
* FileWideSeekProc --
|
sl@0
|
643 |
*
|
sl@0
|
644 |
* This procedure is called by the generic IO level to move the
|
sl@0
|
645 |
* access point in a file based channel, with offsets expressed
|
sl@0
|
646 |
* as wide integers.
|
sl@0
|
647 |
*
|
sl@0
|
648 |
* Results:
|
sl@0
|
649 |
* -1 if failed, the new position if successful. An output
|
sl@0
|
650 |
* argument contains the POSIX error code if an error occurred,
|
sl@0
|
651 |
* or zero.
|
sl@0
|
652 |
*
|
sl@0
|
653 |
* Side effects:
|
sl@0
|
654 |
* Moves the location at which the channel will be accessed in
|
sl@0
|
655 |
* future operations.
|
sl@0
|
656 |
*
|
sl@0
|
657 |
*----------------------------------------------------------------------
|
sl@0
|
658 |
*/
|
sl@0
|
659 |
|
sl@0
|
660 |
static Tcl_WideInt
|
sl@0
|
661 |
FileWideSeekProc(instanceData, offset, mode, errorCodePtr)
|
sl@0
|
662 |
ClientData instanceData; /* File state. */
|
sl@0
|
663 |
Tcl_WideInt offset; /* Offset to seek to. */
|
sl@0
|
664 |
int mode; /* Relative to where should we seek? Can be
|
sl@0
|
665 |
* one of SEEK_START, SEEK_CUR or SEEK_END. */
|
sl@0
|
666 |
int *errorCodePtr; /* To store error code. */
|
sl@0
|
667 |
{
|
sl@0
|
668 |
FileState *fsPtr = (FileState *) instanceData;
|
sl@0
|
669 |
Tcl_WideInt newLoc;
|
sl@0
|
670 |
|
sl@0
|
671 |
newLoc = TclOSseek(fsPtr->fd, (Tcl_SeekOffset) offset, mode);
|
sl@0
|
672 |
|
sl@0
|
673 |
*errorCodePtr = (newLoc == -1) ? errno : 0;
|
sl@0
|
674 |
return newLoc;
|
sl@0
|
675 |
}
|
sl@0
|
676 |
|
sl@0
|
677 |
/*
|
sl@0
|
678 |
*----------------------------------------------------------------------
|
sl@0
|
679 |
*
|
sl@0
|
680 |
* FileWatchProc --
|
sl@0
|
681 |
*
|
sl@0
|
682 |
* Initialize the notifier to watch the fd from this channel.
|
sl@0
|
683 |
*
|
sl@0
|
684 |
* Results:
|
sl@0
|
685 |
* None.
|
sl@0
|
686 |
*
|
sl@0
|
687 |
* Side effects:
|
sl@0
|
688 |
* Sets up the notifier so that a future event on the channel will
|
sl@0
|
689 |
* be seen by Tcl.
|
sl@0
|
690 |
*
|
sl@0
|
691 |
*----------------------------------------------------------------------
|
sl@0
|
692 |
*/
|
sl@0
|
693 |
|
sl@0
|
694 |
static void
|
sl@0
|
695 |
FileWatchProc(instanceData, mask)
|
sl@0
|
696 |
ClientData instanceData; /* The file state. */
|
sl@0
|
697 |
int mask; /* Events of interest; an OR-ed
|
sl@0
|
698 |
* combination of TCL_READABLE,
|
sl@0
|
699 |
* TCL_WRITABLE and TCL_EXCEPTION. */
|
sl@0
|
700 |
{
|
sl@0
|
701 |
FileState *fsPtr = (FileState *) instanceData;
|
sl@0
|
702 |
|
sl@0
|
703 |
/*
|
sl@0
|
704 |
* Make sure we only register for events that are valid on this file.
|
sl@0
|
705 |
* Note that we are passing Tcl_NotifyChannel directly to
|
sl@0
|
706 |
* Tcl_CreateFileHandler with the channel pointer as the client data.
|
sl@0
|
707 |
*/
|
sl@0
|
708 |
|
sl@0
|
709 |
mask &= fsPtr->validMask;
|
sl@0
|
710 |
if (mask) {
|
sl@0
|
711 |
Tcl_CreateFileHandler(fsPtr->fd, mask,
|
sl@0
|
712 |
(Tcl_FileProc *) Tcl_NotifyChannel,
|
sl@0
|
713 |
(ClientData) fsPtr->channel);
|
sl@0
|
714 |
} else {
|
sl@0
|
715 |
Tcl_DeleteFileHandler(fsPtr->fd);
|
sl@0
|
716 |
}
|
sl@0
|
717 |
}
|
sl@0
|
718 |
|
sl@0
|
719 |
/*
|
sl@0
|
720 |
*----------------------------------------------------------------------
|
sl@0
|
721 |
*
|
sl@0
|
722 |
* FileGetHandleProc --
|
sl@0
|
723 |
*
|
sl@0
|
724 |
* Called from Tcl_GetChannelHandle to retrieve OS handles from
|
sl@0
|
725 |
* a file based channel.
|
sl@0
|
726 |
*
|
sl@0
|
727 |
* Results:
|
sl@0
|
728 |
* Returns TCL_OK with the fd in handlePtr, or TCL_ERROR if
|
sl@0
|
729 |
* there is no handle for the specified direction.
|
sl@0
|
730 |
*
|
sl@0
|
731 |
* Side effects:
|
sl@0
|
732 |
* None.
|
sl@0
|
733 |
*
|
sl@0
|
734 |
*----------------------------------------------------------------------
|
sl@0
|
735 |
*/
|
sl@0
|
736 |
|
sl@0
|
737 |
static int
|
sl@0
|
738 |
FileGetHandleProc(instanceData, direction, handlePtr)
|
sl@0
|
739 |
ClientData instanceData; /* The file state. */
|
sl@0
|
740 |
int direction; /* TCL_READABLE or TCL_WRITABLE */
|
sl@0
|
741 |
ClientData *handlePtr; /* Where to store the handle. */
|
sl@0
|
742 |
{
|
sl@0
|
743 |
FileState *fsPtr = (FileState *) instanceData;
|
sl@0
|
744 |
|
sl@0
|
745 |
if (direction & fsPtr->validMask) {
|
sl@0
|
746 |
*handlePtr = (ClientData) fsPtr->fd;
|
sl@0
|
747 |
return TCL_OK;
|
sl@0
|
748 |
} else {
|
sl@0
|
749 |
return TCL_ERROR;
|
sl@0
|
750 |
}
|
sl@0
|
751 |
}
|
sl@0
|
752 |
|
sl@0
|
753 |
#ifdef SUPPORTS_TTY
|
sl@0
|
754 |
|
sl@0
|
755 |
/*
|
sl@0
|
756 |
*----------------------------------------------------------------------
|
sl@0
|
757 |
*
|
sl@0
|
758 |
* TtyCloseProc --
|
sl@0
|
759 |
*
|
sl@0
|
760 |
* This procedure is called from the generic IO level to perform
|
sl@0
|
761 |
* channel-type-specific cleanup when a tty based channel is closed.
|
sl@0
|
762 |
*
|
sl@0
|
763 |
* Results:
|
sl@0
|
764 |
* 0 if successful, errno if failed.
|
sl@0
|
765 |
*
|
sl@0
|
766 |
* Side effects:
|
sl@0
|
767 |
* Closes the device of the channel.
|
sl@0
|
768 |
*
|
sl@0
|
769 |
*----------------------------------------------------------------------
|
sl@0
|
770 |
*/
|
sl@0
|
771 |
static int
|
sl@0
|
772 |
TtyCloseProc(instanceData, interp)
|
sl@0
|
773 |
ClientData instanceData; /* Tty state. */
|
sl@0
|
774 |
Tcl_Interp *interp; /* For error reporting - unused. */
|
sl@0
|
775 |
{
|
sl@0
|
776 |
#if BAD_TIP35_FLUSH
|
sl@0
|
777 |
TtyState *ttyPtr = (TtyState *) instanceData;
|
sl@0
|
778 |
#endif /* BAD_TIP35_FLUSH */
|
sl@0
|
779 |
#ifdef TTYFLUSH
|
sl@0
|
780 |
TTYFLUSH(ttyPtr->fs.fd);
|
sl@0
|
781 |
#endif /* TTYFLUSH */
|
sl@0
|
782 |
#if 0
|
sl@0
|
783 |
/*
|
sl@0
|
784 |
* TIP#35 agreed to remove the unsave so that TCL could be used as a
|
sl@0
|
785 |
* simple stty.
|
sl@0
|
786 |
* It would be cleaner to remove all the stuff related to
|
sl@0
|
787 |
* TtyState.stateUpdated
|
sl@0
|
788 |
* TtyState.savedState
|
sl@0
|
789 |
* Then the structure TtyState would be the same as FileState.
|
sl@0
|
790 |
* IMO this cleanup could better be done for the final 8.4 release
|
sl@0
|
791 |
* after nobody complained about the missing unsave. -- schroedter
|
sl@0
|
792 |
*/
|
sl@0
|
793 |
if (ttyPtr->stateUpdated) {
|
sl@0
|
794 |
SETIOSTATE(ttyPtr->fs.fd, &ttyPtr->savedState);
|
sl@0
|
795 |
}
|
sl@0
|
796 |
#endif
|
sl@0
|
797 |
return FileCloseProc(instanceData, interp);
|
sl@0
|
798 |
}
|
sl@0
|
799 |
|
sl@0
|
800 |
/*
|
sl@0
|
801 |
*----------------------------------------------------------------------
|
sl@0
|
802 |
*
|
sl@0
|
803 |
* TtyOutputProc--
|
sl@0
|
804 |
*
|
sl@0
|
805 |
* This procedure is invoked from the generic IO level to write
|
sl@0
|
806 |
* output to a TTY channel.
|
sl@0
|
807 |
*
|
sl@0
|
808 |
* Results:
|
sl@0
|
809 |
* The number of bytes written is returned or -1 on error. An
|
sl@0
|
810 |
* output argument contains a POSIX error code if an error occurred,
|
sl@0
|
811 |
* or zero.
|
sl@0
|
812 |
*
|
sl@0
|
813 |
* Side effects:
|
sl@0
|
814 |
* Writes output on the output device of the channel
|
sl@0
|
815 |
* if the channel is not designated to be closed.
|
sl@0
|
816 |
*
|
sl@0
|
817 |
*----------------------------------------------------------------------
|
sl@0
|
818 |
*/
|
sl@0
|
819 |
|
sl@0
|
820 |
#if BAD_TIP35_FLUSH
|
sl@0
|
821 |
static int
|
sl@0
|
822 |
TtyOutputProc(instanceData, buf, toWrite, errorCodePtr)
|
sl@0
|
823 |
ClientData instanceData; /* File state. */
|
sl@0
|
824 |
CONST char *buf; /* The data buffer. */
|
sl@0
|
825 |
int toWrite; /* How many bytes to write? */
|
sl@0
|
826 |
int *errorCodePtr; /* Where to store error code. */
|
sl@0
|
827 |
{
|
sl@0
|
828 |
if (TclInExit()) {
|
sl@0
|
829 |
/*
|
sl@0
|
830 |
* Do not write data during Tcl exit.
|
sl@0
|
831 |
* Serial port may block preventing Tcl from exit.
|
sl@0
|
832 |
*/
|
sl@0
|
833 |
return toWrite;
|
sl@0
|
834 |
} else {
|
sl@0
|
835 |
return FileOutputProc(instanceData, buf, toWrite, errorCodePtr);
|
sl@0
|
836 |
}
|
sl@0
|
837 |
}
|
sl@0
|
838 |
#endif /* BAD_TIP35_FLUSH */
|
sl@0
|
839 |
|
sl@0
|
840 |
#ifdef USE_TERMIOS
|
sl@0
|
841 |
/*
|
sl@0
|
842 |
*----------------------------------------------------------------------
|
sl@0
|
843 |
*
|
sl@0
|
844 |
* TtyModemStatusStr --
|
sl@0
|
845 |
*
|
sl@0
|
846 |
* Converts a RS232 modem status list of readable flags
|
sl@0
|
847 |
*
|
sl@0
|
848 |
*----------------------------------------------------------------------
|
sl@0
|
849 |
*/
|
sl@0
|
850 |
static void
|
sl@0
|
851 |
TtyModemStatusStr(status, dsPtr)
|
sl@0
|
852 |
int status; /* RS232 modem status */
|
sl@0
|
853 |
Tcl_DString *dsPtr; /* Where to store string */
|
sl@0
|
854 |
{
|
sl@0
|
855 |
#ifdef TIOCM_CTS
|
sl@0
|
856 |
Tcl_DStringAppendElement(dsPtr, "CTS");
|
sl@0
|
857 |
Tcl_DStringAppendElement(dsPtr, (status & TIOCM_CTS) ? "1" : "0");
|
sl@0
|
858 |
#endif /* TIOCM_CTS */
|
sl@0
|
859 |
#ifdef TIOCM_DSR
|
sl@0
|
860 |
Tcl_DStringAppendElement(dsPtr, "DSR");
|
sl@0
|
861 |
Tcl_DStringAppendElement(dsPtr, (status & TIOCM_DSR) ? "1" : "0");
|
sl@0
|
862 |
#endif /* TIOCM_DSR */
|
sl@0
|
863 |
#ifdef TIOCM_RNG
|
sl@0
|
864 |
Tcl_DStringAppendElement(dsPtr, "RING");
|
sl@0
|
865 |
Tcl_DStringAppendElement(dsPtr, (status & TIOCM_RNG) ? "1" : "0");
|
sl@0
|
866 |
#endif /* TIOCM_RNG */
|
sl@0
|
867 |
#ifdef TIOCM_CD
|
sl@0
|
868 |
Tcl_DStringAppendElement(dsPtr, "DCD");
|
sl@0
|
869 |
Tcl_DStringAppendElement(dsPtr, (status & TIOCM_CD) ? "1" : "0");
|
sl@0
|
870 |
#endif /* TIOCM_CD */
|
sl@0
|
871 |
}
|
sl@0
|
872 |
#endif /* USE_TERMIOS */
|
sl@0
|
873 |
|
sl@0
|
874 |
/*
|
sl@0
|
875 |
*----------------------------------------------------------------------
|
sl@0
|
876 |
*
|
sl@0
|
877 |
* TtySetOptionProc --
|
sl@0
|
878 |
*
|
sl@0
|
879 |
* Sets an option on a channel.
|
sl@0
|
880 |
*
|
sl@0
|
881 |
* Results:
|
sl@0
|
882 |
* A standard Tcl result. Also sets the interp's result on error if
|
sl@0
|
883 |
* interp is not NULL.
|
sl@0
|
884 |
*
|
sl@0
|
885 |
* Side effects:
|
sl@0
|
886 |
* May modify an option on a device.
|
sl@0
|
887 |
* Sets Error message if needed (by calling Tcl_BadChannelOption).
|
sl@0
|
888 |
*
|
sl@0
|
889 |
*----------------------------------------------------------------------
|
sl@0
|
890 |
*/
|
sl@0
|
891 |
|
sl@0
|
892 |
static int
|
sl@0
|
893 |
TtySetOptionProc(instanceData, interp, optionName, value)
|
sl@0
|
894 |
ClientData instanceData; /* File state. */
|
sl@0
|
895 |
Tcl_Interp *interp; /* For error reporting - can be NULL. */
|
sl@0
|
896 |
CONST char *optionName; /* Which option to set? */
|
sl@0
|
897 |
CONST char *value; /* New value for option. */
|
sl@0
|
898 |
{
|
sl@0
|
899 |
FileState *fsPtr = (FileState *) instanceData;
|
sl@0
|
900 |
unsigned int len, vlen;
|
sl@0
|
901 |
TtyAttrs tty;
|
sl@0
|
902 |
#ifdef USE_TERMIOS
|
sl@0
|
903 |
int flag, control, argc;
|
sl@0
|
904 |
CONST char **argv;
|
sl@0
|
905 |
IOSTATE iostate;
|
sl@0
|
906 |
#endif /* USE_TERMIOS */
|
sl@0
|
907 |
|
sl@0
|
908 |
len = strlen(optionName);
|
sl@0
|
909 |
vlen = strlen(value);
|
sl@0
|
910 |
|
sl@0
|
911 |
/*
|
sl@0
|
912 |
* Option -mode baud,parity,databits,stopbits
|
sl@0
|
913 |
*/
|
sl@0
|
914 |
if ((len > 2) && (strncmp(optionName, "-mode", len) == 0)) {
|
sl@0
|
915 |
if (TtyParseMode(interp, value, &tty.baud, &tty.parity, &tty.data,
|
sl@0
|
916 |
&tty.stop) != TCL_OK) {
|
sl@0
|
917 |
return TCL_ERROR;
|
sl@0
|
918 |
}
|
sl@0
|
919 |
/*
|
sl@0
|
920 |
* system calls results should be checked there. -- dl
|
sl@0
|
921 |
*/
|
sl@0
|
922 |
|
sl@0
|
923 |
TtySetAttributes(fsPtr->fd, &tty);
|
sl@0
|
924 |
((TtyState *) fsPtr)->stateUpdated = 1;
|
sl@0
|
925 |
return TCL_OK;
|
sl@0
|
926 |
}
|
sl@0
|
927 |
|
sl@0
|
928 |
#ifdef USE_TERMIOS
|
sl@0
|
929 |
|
sl@0
|
930 |
/*
|
sl@0
|
931 |
* Option -handshake none|xonxoff|rtscts|dtrdsr
|
sl@0
|
932 |
*/
|
sl@0
|
933 |
if ((len > 1) && (strncmp(optionName, "-handshake", len) == 0)) {
|
sl@0
|
934 |
/*
|
sl@0
|
935 |
* Reset all handshake options
|
sl@0
|
936 |
* DTR and RTS are ON by default
|
sl@0
|
937 |
*/
|
sl@0
|
938 |
GETIOSTATE(fsPtr->fd, &iostate);
|
sl@0
|
939 |
iostate.c_iflag &= ~(IXON | IXOFF | IXANY);
|
sl@0
|
940 |
#ifdef CRTSCTS
|
sl@0
|
941 |
iostate.c_cflag &= ~CRTSCTS;
|
sl@0
|
942 |
#endif /* CRTSCTS */
|
sl@0
|
943 |
if (strncasecmp(value, "NONE", vlen) == 0) {
|
sl@0
|
944 |
/* leave all handshake options disabled */
|
sl@0
|
945 |
} else if (strncasecmp(value, "XONXOFF", vlen) == 0) {
|
sl@0
|
946 |
iostate.c_iflag |= (IXON | IXOFF | IXANY);
|
sl@0
|
947 |
} else if (strncasecmp(value, "RTSCTS", vlen) == 0) {
|
sl@0
|
948 |
#ifdef CRTSCTS
|
sl@0
|
949 |
iostate.c_cflag |= CRTSCTS;
|
sl@0
|
950 |
#else /* !CRTSTS */
|
sl@0
|
951 |
UNSUPPORTED_OPTION("-handshake RTSCTS");
|
sl@0
|
952 |
return TCL_ERROR;
|
sl@0
|
953 |
#endif /* CRTSCTS */
|
sl@0
|
954 |
} else if (strncasecmp(value, "DTRDSR", vlen) == 0) {
|
sl@0
|
955 |
UNSUPPORTED_OPTION("-handshake DTRDSR");
|
sl@0
|
956 |
return TCL_ERROR;
|
sl@0
|
957 |
} else {
|
sl@0
|
958 |
if (interp) {
|
sl@0
|
959 |
Tcl_AppendResult(interp, "bad value for -handshake: ",
|
sl@0
|
960 |
"must be one of xonxoff, rtscts, dtrdsr or none",
|
sl@0
|
961 |
(char *) NULL);
|
sl@0
|
962 |
}
|
sl@0
|
963 |
return TCL_ERROR;
|
sl@0
|
964 |
}
|
sl@0
|
965 |
SETIOSTATE(fsPtr->fd, &iostate);
|
sl@0
|
966 |
return TCL_OK;
|
sl@0
|
967 |
}
|
sl@0
|
968 |
|
sl@0
|
969 |
/*
|
sl@0
|
970 |
* Option -xchar {\x11 \x13}
|
sl@0
|
971 |
*/
|
sl@0
|
972 |
if ((len > 1) && (strncmp(optionName, "-xchar", len) == 0)) {
|
sl@0
|
973 |
GETIOSTATE(fsPtr->fd, &iostate);
|
sl@0
|
974 |
if (Tcl_SplitList(interp, value, &argc, &argv) == TCL_ERROR) {
|
sl@0
|
975 |
return TCL_ERROR;
|
sl@0
|
976 |
}
|
sl@0
|
977 |
if (argc == 2) {
|
sl@0
|
978 |
iostate.c_cc[VSTART] = argv[0][0];
|
sl@0
|
979 |
iostate.c_cc[VSTOP] = argv[1][0];
|
sl@0
|
980 |
} else {
|
sl@0
|
981 |
if (interp) {
|
sl@0
|
982 |
Tcl_AppendResult(interp,
|
sl@0
|
983 |
"bad value for -xchar: should be a list of two elements",
|
sl@0
|
984 |
(char *) NULL);
|
sl@0
|
985 |
}
|
sl@0
|
986 |
ckfree((char *) argv);
|
sl@0
|
987 |
return TCL_ERROR;
|
sl@0
|
988 |
}
|
sl@0
|
989 |
SETIOSTATE(fsPtr->fd, &iostate);
|
sl@0
|
990 |
ckfree((char *) argv);
|
sl@0
|
991 |
return TCL_OK;
|
sl@0
|
992 |
}
|
sl@0
|
993 |
|
sl@0
|
994 |
/*
|
sl@0
|
995 |
* Option -timeout msec
|
sl@0
|
996 |
*/
|
sl@0
|
997 |
if ((len > 2) && (strncmp(optionName, "-timeout", len) == 0)) {
|
sl@0
|
998 |
int msec;
|
sl@0
|
999 |
|
sl@0
|
1000 |
GETIOSTATE(fsPtr->fd, &iostate);
|
sl@0
|
1001 |
if (Tcl_GetInt(interp, value, &msec) != TCL_OK) {
|
sl@0
|
1002 |
return TCL_ERROR;
|
sl@0
|
1003 |
}
|
sl@0
|
1004 |
iostate.c_cc[VMIN] = 0;
|
sl@0
|
1005 |
iostate.c_cc[VTIME] = (msec == 0) ? 0 : (msec < 100) ? 1 : (msec+50)/100;
|
sl@0
|
1006 |
SETIOSTATE(fsPtr->fd, &iostate);
|
sl@0
|
1007 |
return TCL_OK;
|
sl@0
|
1008 |
}
|
sl@0
|
1009 |
|
sl@0
|
1010 |
/*
|
sl@0
|
1011 |
* Option -ttycontrol {DTR 1 RTS 0 BREAK 0}
|
sl@0
|
1012 |
*/
|
sl@0
|
1013 |
if ((len > 4) && (strncmp(optionName, "-ttycontrol", len) == 0)) {
|
sl@0
|
1014 |
int i;
|
sl@0
|
1015 |
if (Tcl_SplitList(interp, value, &argc, &argv) == TCL_ERROR) {
|
sl@0
|
1016 |
return TCL_ERROR;
|
sl@0
|
1017 |
}
|
sl@0
|
1018 |
if ((argc % 2) == 1) {
|
sl@0
|
1019 |
if (interp) {
|
sl@0
|
1020 |
Tcl_AppendResult(interp,
|
sl@0
|
1021 |
"bad value for -ttycontrol: should be a list of",
|
sl@0
|
1022 |
"signal,value pairs", (char *) NULL);
|
sl@0
|
1023 |
}
|
sl@0
|
1024 |
ckfree((char *) argv);
|
sl@0
|
1025 |
return TCL_ERROR;
|
sl@0
|
1026 |
}
|
sl@0
|
1027 |
|
sl@0
|
1028 |
GETCONTROL(fsPtr->fd, &control);
|
sl@0
|
1029 |
for (i = 0; i < argc-1; i += 2) {
|
sl@0
|
1030 |
if (Tcl_GetBoolean(interp, argv[i+1], &flag) == TCL_ERROR) {
|
sl@0
|
1031 |
ckfree((char *) argv);
|
sl@0
|
1032 |
return TCL_ERROR;
|
sl@0
|
1033 |
}
|
sl@0
|
1034 |
if (strncasecmp(argv[i], "DTR", strlen(argv[i])) == 0) {
|
sl@0
|
1035 |
#ifdef TIOCM_DTR
|
sl@0
|
1036 |
if (flag) {
|
sl@0
|
1037 |
control |= TIOCM_DTR;
|
sl@0
|
1038 |
} else {
|
sl@0
|
1039 |
control &= ~TIOCM_DTR;
|
sl@0
|
1040 |
}
|
sl@0
|
1041 |
#else /* !TIOCM_DTR */
|
sl@0
|
1042 |
UNSUPPORTED_OPTION("-ttycontrol DTR");
|
sl@0
|
1043 |
ckfree((char *) argv);
|
sl@0
|
1044 |
return TCL_ERROR;
|
sl@0
|
1045 |
#endif /* TIOCM_DTR */
|
sl@0
|
1046 |
} else if (strncasecmp(argv[i], "RTS", strlen(argv[i])) == 0) {
|
sl@0
|
1047 |
#ifdef TIOCM_RTS
|
sl@0
|
1048 |
if (flag) {
|
sl@0
|
1049 |
control |= TIOCM_RTS;
|
sl@0
|
1050 |
} else {
|
sl@0
|
1051 |
control &= ~TIOCM_RTS;
|
sl@0
|
1052 |
}
|
sl@0
|
1053 |
#else /* !TIOCM_RTS*/
|
sl@0
|
1054 |
UNSUPPORTED_OPTION("-ttycontrol RTS");
|
sl@0
|
1055 |
ckfree((char *) argv);
|
sl@0
|
1056 |
return TCL_ERROR;
|
sl@0
|
1057 |
#endif /* TIOCM_RTS*/
|
sl@0
|
1058 |
} else if (strncasecmp(argv[i], "BREAK", strlen(argv[i])) == 0) {
|
sl@0
|
1059 |
#ifdef SETBREAK
|
sl@0
|
1060 |
SETBREAK(fsPtr->fd, flag);
|
sl@0
|
1061 |
#else /* !SETBREAK */
|
sl@0
|
1062 |
UNSUPPORTED_OPTION("-ttycontrol BREAK");
|
sl@0
|
1063 |
ckfree((char *) argv);
|
sl@0
|
1064 |
return TCL_ERROR;
|
sl@0
|
1065 |
#endif /* SETBREAK */
|
sl@0
|
1066 |
} else {
|
sl@0
|
1067 |
if (interp) {
|
sl@0
|
1068 |
Tcl_AppendResult(interp, "bad signal \"", argv[i],
|
sl@0
|
1069 |
"\" for -ttycontrol: must be ",
|
sl@0
|
1070 |
"DTR, RTS or BREAK", (char *) NULL);
|
sl@0
|
1071 |
}
|
sl@0
|
1072 |
ckfree((char *) argv);
|
sl@0
|
1073 |
return TCL_ERROR;
|
sl@0
|
1074 |
}
|
sl@0
|
1075 |
} /* -ttycontrol options loop */
|
sl@0
|
1076 |
|
sl@0
|
1077 |
SETCONTROL(fsPtr->fd, &control);
|
sl@0
|
1078 |
ckfree((char *) argv);
|
sl@0
|
1079 |
return TCL_OK;
|
sl@0
|
1080 |
}
|
sl@0
|
1081 |
|
sl@0
|
1082 |
return Tcl_BadChannelOption(interp, optionName,
|
sl@0
|
1083 |
"mode handshake timeout ttycontrol xchar ");
|
sl@0
|
1084 |
|
sl@0
|
1085 |
#else /* !USE_TERMIOS */
|
sl@0
|
1086 |
return Tcl_BadChannelOption(interp, optionName, "mode");
|
sl@0
|
1087 |
#endif /* USE_TERMIOS */
|
sl@0
|
1088 |
}
|
sl@0
|
1089 |
|
sl@0
|
1090 |
/*
|
sl@0
|
1091 |
*----------------------------------------------------------------------
|
sl@0
|
1092 |
*
|
sl@0
|
1093 |
* TtyGetOptionProc --
|
sl@0
|
1094 |
*
|
sl@0
|
1095 |
* Gets a mode associated with an IO channel. If the optionName arg
|
sl@0
|
1096 |
* is non NULL, retrieves the value of that option. If the optionName
|
sl@0
|
1097 |
* arg is NULL, retrieves a list of alternating option names and
|
sl@0
|
1098 |
* values for the given channel.
|
sl@0
|
1099 |
*
|
sl@0
|
1100 |
* Results:
|
sl@0
|
1101 |
* A standard Tcl result. Also sets the supplied DString to the
|
sl@0
|
1102 |
* string value of the option(s) returned.
|
sl@0
|
1103 |
*
|
sl@0
|
1104 |
* Side effects:
|
sl@0
|
1105 |
* The string returned by this function is in static storage and
|
sl@0
|
1106 |
* may be reused at any time subsequent to the call.
|
sl@0
|
1107 |
* Sets Error message if needed (by calling Tcl_BadChannelOption).
|
sl@0
|
1108 |
*
|
sl@0
|
1109 |
*----------------------------------------------------------------------
|
sl@0
|
1110 |
*/
|
sl@0
|
1111 |
|
sl@0
|
1112 |
static int
|
sl@0
|
1113 |
TtyGetOptionProc(instanceData, interp, optionName, dsPtr)
|
sl@0
|
1114 |
ClientData instanceData; /* File state. */
|
sl@0
|
1115 |
Tcl_Interp *interp; /* For error reporting - can be NULL. */
|
sl@0
|
1116 |
CONST char *optionName; /* Option to get. */
|
sl@0
|
1117 |
Tcl_DString *dsPtr; /* Where to store value(s). */
|
sl@0
|
1118 |
{
|
sl@0
|
1119 |
FileState *fsPtr = (FileState *) instanceData;
|
sl@0
|
1120 |
unsigned int len;
|
sl@0
|
1121 |
char buf[3 * TCL_INTEGER_SPACE + 16];
|
sl@0
|
1122 |
TtyAttrs tty;
|
sl@0
|
1123 |
int valid = 0; /* flag if valid option parsed */
|
sl@0
|
1124 |
|
sl@0
|
1125 |
if (optionName == NULL) {
|
sl@0
|
1126 |
len = 0;
|
sl@0
|
1127 |
} else {
|
sl@0
|
1128 |
len = strlen(optionName);
|
sl@0
|
1129 |
}
|
sl@0
|
1130 |
if (len == 0) {
|
sl@0
|
1131 |
Tcl_DStringAppendElement(dsPtr, "-mode");
|
sl@0
|
1132 |
}
|
sl@0
|
1133 |
if (len==0 || (len>2 && strncmp(optionName, "-mode", len)==0)) {
|
sl@0
|
1134 |
valid = 1;
|
sl@0
|
1135 |
TtyGetAttributes(fsPtr->fd, &tty);
|
sl@0
|
1136 |
sprintf(buf, "%d,%c,%d,%d", tty.baud, tty.parity, tty.data, tty.stop);
|
sl@0
|
1137 |
Tcl_DStringAppendElement(dsPtr, buf);
|
sl@0
|
1138 |
}
|
sl@0
|
1139 |
|
sl@0
|
1140 |
#ifdef USE_TERMIOS
|
sl@0
|
1141 |
/*
|
sl@0
|
1142 |
* get option -xchar
|
sl@0
|
1143 |
*/
|
sl@0
|
1144 |
if (len == 0) {
|
sl@0
|
1145 |
Tcl_DStringAppendElement(dsPtr, "-xchar");
|
sl@0
|
1146 |
Tcl_DStringStartSublist(dsPtr);
|
sl@0
|
1147 |
}
|
sl@0
|
1148 |
if (len==0 || (len>1 && strncmp(optionName, "-xchar", len)==0)) {
|
sl@0
|
1149 |
IOSTATE iostate;
|
sl@0
|
1150 |
valid = 1;
|
sl@0
|
1151 |
|
sl@0
|
1152 |
GETIOSTATE(fsPtr->fd, &iostate);
|
sl@0
|
1153 |
sprintf(buf, "%c", iostate.c_cc[VSTART]);
|
sl@0
|
1154 |
Tcl_DStringAppendElement(dsPtr, buf);
|
sl@0
|
1155 |
sprintf(buf, "%c", iostate.c_cc[VSTOP]);
|
sl@0
|
1156 |
Tcl_DStringAppendElement(dsPtr, buf);
|
sl@0
|
1157 |
}
|
sl@0
|
1158 |
if (len == 0) {
|
sl@0
|
1159 |
Tcl_DStringEndSublist(dsPtr);
|
sl@0
|
1160 |
}
|
sl@0
|
1161 |
|
sl@0
|
1162 |
/*
|
sl@0
|
1163 |
* get option -queue
|
sl@0
|
1164 |
* option is readonly and returned by [fconfigure chan -queue]
|
sl@0
|
1165 |
* but not returned by unnamed [fconfigure chan]
|
sl@0
|
1166 |
*/
|
sl@0
|
1167 |
if ((len > 1) && (strncmp(optionName, "-queue", len) == 0)) {
|
sl@0
|
1168 |
int inQueue=0, outQueue=0;
|
sl@0
|
1169 |
int inBuffered, outBuffered;
|
sl@0
|
1170 |
valid = 1;
|
sl@0
|
1171 |
#ifdef GETREADQUEUE
|
sl@0
|
1172 |
GETREADQUEUE(fsPtr->fd, inQueue);
|
sl@0
|
1173 |
#endif /* GETREADQUEUE */
|
sl@0
|
1174 |
#ifdef GETWRITEQUEUE
|
sl@0
|
1175 |
GETWRITEQUEUE(fsPtr->fd, outQueue);
|
sl@0
|
1176 |
#endif /* GETWRITEQUEUE */
|
sl@0
|
1177 |
inBuffered = Tcl_InputBuffered(fsPtr->channel);
|
sl@0
|
1178 |
outBuffered = Tcl_OutputBuffered(fsPtr->channel);
|
sl@0
|
1179 |
|
sl@0
|
1180 |
sprintf(buf, "%d", inBuffered+inQueue);
|
sl@0
|
1181 |
Tcl_DStringAppendElement(dsPtr, buf);
|
sl@0
|
1182 |
sprintf(buf, "%d", outBuffered+outQueue);
|
sl@0
|
1183 |
Tcl_DStringAppendElement(dsPtr, buf);
|
sl@0
|
1184 |
}
|
sl@0
|
1185 |
|
sl@0
|
1186 |
/*
|
sl@0
|
1187 |
* get option -ttystatus
|
sl@0
|
1188 |
* option is readonly and returned by [fconfigure chan -ttystatus]
|
sl@0
|
1189 |
* but not returned by unnamed [fconfigure chan]
|
sl@0
|
1190 |
*/
|
sl@0
|
1191 |
if ((len > 4) && (strncmp(optionName, "-ttystatus", len) == 0)) {
|
sl@0
|
1192 |
int status;
|
sl@0
|
1193 |
valid = 1;
|
sl@0
|
1194 |
GETCONTROL(fsPtr->fd, &status);
|
sl@0
|
1195 |
TtyModemStatusStr(status, dsPtr);
|
sl@0
|
1196 |
}
|
sl@0
|
1197 |
#endif /* USE_TERMIOS */
|
sl@0
|
1198 |
|
sl@0
|
1199 |
if (valid) {
|
sl@0
|
1200 |
return TCL_OK;
|
sl@0
|
1201 |
} else {
|
sl@0
|
1202 |
return Tcl_BadChannelOption(interp, optionName,
|
sl@0
|
1203 |
#ifdef USE_TERMIOS
|
sl@0
|
1204 |
"mode queue ttystatus xchar");
|
sl@0
|
1205 |
#else /* !USE_TERMIOS */
|
sl@0
|
1206 |
"mode");
|
sl@0
|
1207 |
#endif /* USE_TERMIOS */
|
sl@0
|
1208 |
}
|
sl@0
|
1209 |
}
|
sl@0
|
1210 |
|
sl@0
|
1211 |
#undef DIRECT_BAUD
|
sl@0
|
1212 |
#ifdef B4800
|
sl@0
|
1213 |
# if (B4800 == 4800)
|
sl@0
|
1214 |
# define DIRECT_BAUD
|
sl@0
|
1215 |
# endif /* B4800 == 4800 */
|
sl@0
|
1216 |
#endif /* B4800 */
|
sl@0
|
1217 |
|
sl@0
|
1218 |
#ifdef DIRECT_BAUD
|
sl@0
|
1219 |
# define TtyGetSpeed(baud) ((unsigned) (baud))
|
sl@0
|
1220 |
# define TtyGetBaud(speed) ((int) (speed))
|
sl@0
|
1221 |
#else /* !DIRECT_BAUD */
|
sl@0
|
1222 |
|
sl@0
|
1223 |
static struct {int baud; unsigned long speed;} speeds[] = {
|
sl@0
|
1224 |
#ifdef B0
|
sl@0
|
1225 |
{0, B0},
|
sl@0
|
1226 |
#endif
|
sl@0
|
1227 |
#ifdef B50
|
sl@0
|
1228 |
{50, B50},
|
sl@0
|
1229 |
#endif
|
sl@0
|
1230 |
#ifdef B75
|
sl@0
|
1231 |
{75, B75},
|
sl@0
|
1232 |
#endif
|
sl@0
|
1233 |
#ifdef B110
|
sl@0
|
1234 |
{110, B110},
|
sl@0
|
1235 |
#endif
|
sl@0
|
1236 |
#ifdef B134
|
sl@0
|
1237 |
{134, B134},
|
sl@0
|
1238 |
#endif
|
sl@0
|
1239 |
#ifdef B150
|
sl@0
|
1240 |
{150, B150},
|
sl@0
|
1241 |
#endif
|
sl@0
|
1242 |
#ifdef B200
|
sl@0
|
1243 |
{200, B200},
|
sl@0
|
1244 |
#endif
|
sl@0
|
1245 |
#ifdef B300
|
sl@0
|
1246 |
{300, B300},
|
sl@0
|
1247 |
#endif
|
sl@0
|
1248 |
#ifdef B600
|
sl@0
|
1249 |
{600, B600},
|
sl@0
|
1250 |
#endif
|
sl@0
|
1251 |
#ifdef B1200
|
sl@0
|
1252 |
{1200, B1200},
|
sl@0
|
1253 |
#endif
|
sl@0
|
1254 |
#ifdef B1800
|
sl@0
|
1255 |
{1800, B1800},
|
sl@0
|
1256 |
#endif
|
sl@0
|
1257 |
#ifdef B2400
|
sl@0
|
1258 |
{2400, B2400},
|
sl@0
|
1259 |
#endif
|
sl@0
|
1260 |
#ifdef B4800
|
sl@0
|
1261 |
{4800, B4800},
|
sl@0
|
1262 |
#endif
|
sl@0
|
1263 |
#ifdef B9600
|
sl@0
|
1264 |
{9600, B9600},
|
sl@0
|
1265 |
#endif
|
sl@0
|
1266 |
#ifdef B14400
|
sl@0
|
1267 |
{14400, B14400},
|
sl@0
|
1268 |
#endif
|
sl@0
|
1269 |
#ifdef B19200
|
sl@0
|
1270 |
{19200, B19200},
|
sl@0
|
1271 |
#endif
|
sl@0
|
1272 |
#ifdef EXTA
|
sl@0
|
1273 |
{19200, EXTA},
|
sl@0
|
1274 |
#endif
|
sl@0
|
1275 |
#ifdef B28800
|
sl@0
|
1276 |
{28800, B28800},
|
sl@0
|
1277 |
#endif
|
sl@0
|
1278 |
#ifdef B38400
|
sl@0
|
1279 |
{38400, B38400},
|
sl@0
|
1280 |
#endif
|
sl@0
|
1281 |
#ifdef EXTB
|
sl@0
|
1282 |
{38400, EXTB},
|
sl@0
|
1283 |
#endif
|
sl@0
|
1284 |
#ifdef B57600
|
sl@0
|
1285 |
{57600, B57600},
|
sl@0
|
1286 |
#endif
|
sl@0
|
1287 |
#ifdef _B57600
|
sl@0
|
1288 |
{57600, _B57600},
|
sl@0
|
1289 |
#endif
|
sl@0
|
1290 |
#ifdef B76800
|
sl@0
|
1291 |
{76800, B76800},
|
sl@0
|
1292 |
#endif
|
sl@0
|
1293 |
#ifdef B115200
|
sl@0
|
1294 |
{115200, B115200},
|
sl@0
|
1295 |
#endif
|
sl@0
|
1296 |
#ifdef _B115200
|
sl@0
|
1297 |
{115200, _B115200},
|
sl@0
|
1298 |
#endif
|
sl@0
|
1299 |
#ifdef B153600
|
sl@0
|
1300 |
{153600, B153600},
|
sl@0
|
1301 |
#endif
|
sl@0
|
1302 |
#ifdef B230400
|
sl@0
|
1303 |
{230400, B230400},
|
sl@0
|
1304 |
#endif
|
sl@0
|
1305 |
#ifdef B307200
|
sl@0
|
1306 |
{307200, B307200},
|
sl@0
|
1307 |
#endif
|
sl@0
|
1308 |
#ifdef B460800
|
sl@0
|
1309 |
{460800, B460800},
|
sl@0
|
1310 |
#endif
|
sl@0
|
1311 |
{-1, 0}
|
sl@0
|
1312 |
};
|
sl@0
|
1313 |
|
sl@0
|
1314 |
/*
|
sl@0
|
1315 |
*---------------------------------------------------------------------------
|
sl@0
|
1316 |
*
|
sl@0
|
1317 |
* TtyGetSpeed --
|
sl@0
|
1318 |
*
|
sl@0
|
1319 |
* Given a baud rate, get the mask value that should be stored in
|
sl@0
|
1320 |
* the termios, termio, or sgttyb structure in order to select that
|
sl@0
|
1321 |
* baud rate.
|
sl@0
|
1322 |
*
|
sl@0
|
1323 |
* Results:
|
sl@0
|
1324 |
* As above.
|
sl@0
|
1325 |
*
|
sl@0
|
1326 |
* Side effects:
|
sl@0
|
1327 |
* None.
|
sl@0
|
1328 |
*
|
sl@0
|
1329 |
*---------------------------------------------------------------------------
|
sl@0
|
1330 |
*/
|
sl@0
|
1331 |
|
sl@0
|
1332 |
static unsigned long
|
sl@0
|
1333 |
TtyGetSpeed(baud)
|
sl@0
|
1334 |
int baud; /* The baud rate to look up. */
|
sl@0
|
1335 |
{
|
sl@0
|
1336 |
int bestIdx, bestDiff, i, diff;
|
sl@0
|
1337 |
|
sl@0
|
1338 |
bestIdx = 0;
|
sl@0
|
1339 |
bestDiff = 1000000;
|
sl@0
|
1340 |
|
sl@0
|
1341 |
/*
|
sl@0
|
1342 |
* If the baud rate does not correspond to one of the known mask values,
|
sl@0
|
1343 |
* choose the mask value whose baud rate is closest to the specified
|
sl@0
|
1344 |
* baud rate.
|
sl@0
|
1345 |
*/
|
sl@0
|
1346 |
|
sl@0
|
1347 |
for (i = 0; speeds[i].baud >= 0; i++) {
|
sl@0
|
1348 |
diff = speeds[i].baud - baud;
|
sl@0
|
1349 |
if (diff < 0) {
|
sl@0
|
1350 |
diff = -diff;
|
sl@0
|
1351 |
}
|
sl@0
|
1352 |
if (diff < bestDiff) {
|
sl@0
|
1353 |
bestIdx = i;
|
sl@0
|
1354 |
bestDiff = diff;
|
sl@0
|
1355 |
}
|
sl@0
|
1356 |
}
|
sl@0
|
1357 |
return speeds[bestIdx].speed;
|
sl@0
|
1358 |
}
|
sl@0
|
1359 |
|
sl@0
|
1360 |
/*
|
sl@0
|
1361 |
*---------------------------------------------------------------------------
|
sl@0
|
1362 |
*
|
sl@0
|
1363 |
* TtyGetBaud --
|
sl@0
|
1364 |
*
|
sl@0
|
1365 |
* Given a speed mask value from a termios, termio, or sgttyb
|
sl@0
|
1366 |
* structure, get the baus rate that corresponds to that mask value.
|
sl@0
|
1367 |
*
|
sl@0
|
1368 |
* Results:
|
sl@0
|
1369 |
* As above. If the mask value was not recognized, 0 is returned.
|
sl@0
|
1370 |
*
|
sl@0
|
1371 |
* Side effects:
|
sl@0
|
1372 |
* None.
|
sl@0
|
1373 |
*
|
sl@0
|
1374 |
*---------------------------------------------------------------------------
|
sl@0
|
1375 |
*/
|
sl@0
|
1376 |
|
sl@0
|
1377 |
static int
|
sl@0
|
1378 |
TtyGetBaud(speed)
|
sl@0
|
1379 |
unsigned long speed; /* Speed mask value to look up. */
|
sl@0
|
1380 |
{
|
sl@0
|
1381 |
int i;
|
sl@0
|
1382 |
|
sl@0
|
1383 |
for (i = 0; speeds[i].baud >= 0; i++) {
|
sl@0
|
1384 |
if (speeds[i].speed == speed) {
|
sl@0
|
1385 |
return speeds[i].baud;
|
sl@0
|
1386 |
}
|
sl@0
|
1387 |
}
|
sl@0
|
1388 |
return 0;
|
sl@0
|
1389 |
}
|
sl@0
|
1390 |
|
sl@0
|
1391 |
#endif /* !DIRECT_BAUD */
|
sl@0
|
1392 |
|
sl@0
|
1393 |
|
sl@0
|
1394 |
/*
|
sl@0
|
1395 |
*---------------------------------------------------------------------------
|
sl@0
|
1396 |
*
|
sl@0
|
1397 |
* TtyGetAttributes --
|
sl@0
|
1398 |
*
|
sl@0
|
1399 |
* Get the current attributes of the specified serial device.
|
sl@0
|
1400 |
*
|
sl@0
|
1401 |
* Results:
|
sl@0
|
1402 |
* None.
|
sl@0
|
1403 |
*
|
sl@0
|
1404 |
* Side effects:
|
sl@0
|
1405 |
* None.
|
sl@0
|
1406 |
*
|
sl@0
|
1407 |
*---------------------------------------------------------------------------
|
sl@0
|
1408 |
*/
|
sl@0
|
1409 |
|
sl@0
|
1410 |
static void
|
sl@0
|
1411 |
TtyGetAttributes(fd, ttyPtr)
|
sl@0
|
1412 |
int fd; /* Open file descriptor for serial port to
|
sl@0
|
1413 |
* be queried. */
|
sl@0
|
1414 |
TtyAttrs *ttyPtr; /* Buffer filled with serial port
|
sl@0
|
1415 |
* attributes. */
|
sl@0
|
1416 |
{
|
sl@0
|
1417 |
IOSTATE iostate;
|
sl@0
|
1418 |
int baud, parity, data, stop;
|
sl@0
|
1419 |
|
sl@0
|
1420 |
GETIOSTATE(fd, &iostate);
|
sl@0
|
1421 |
|
sl@0
|
1422 |
#ifdef USE_TERMIOS
|
sl@0
|
1423 |
baud = TtyGetBaud(cfgetospeed(&iostate));
|
sl@0
|
1424 |
|
sl@0
|
1425 |
parity = 'n';
|
sl@0
|
1426 |
#ifdef PAREXT
|
sl@0
|
1427 |
switch ((int) (iostate.c_cflag & (PARENB | PARODD | PAREXT))) {
|
sl@0
|
1428 |
case PARENB : parity = 'e'; break;
|
sl@0
|
1429 |
case PARENB | PARODD : parity = 'o'; break;
|
sl@0
|
1430 |
case PARENB | PAREXT : parity = 's'; break;
|
sl@0
|
1431 |
case PARENB | PARODD | PAREXT : parity = 'm'; break;
|
sl@0
|
1432 |
}
|
sl@0
|
1433 |
#else /* !PAREXT */
|
sl@0
|
1434 |
switch ((int) (iostate.c_cflag & (PARENB | PARODD))) {
|
sl@0
|
1435 |
case PARENB : parity = 'e'; break;
|
sl@0
|
1436 |
case PARENB | PARODD : parity = 'o'; break;
|
sl@0
|
1437 |
}
|
sl@0
|
1438 |
#endif /* !PAREXT */
|
sl@0
|
1439 |
|
sl@0
|
1440 |
data = iostate.c_cflag & CSIZE;
|
sl@0
|
1441 |
data = (data == CS5) ? 5 : (data == CS6) ? 6 : (data == CS7) ? 7 : 8;
|
sl@0
|
1442 |
|
sl@0
|
1443 |
stop = (iostate.c_cflag & CSTOPB) ? 2 : 1;
|
sl@0
|
1444 |
#endif /* USE_TERMIOS */
|
sl@0
|
1445 |
|
sl@0
|
1446 |
#ifdef USE_TERMIO
|
sl@0
|
1447 |
baud = TtyGetBaud(iostate.c_cflag & CBAUD);
|
sl@0
|
1448 |
|
sl@0
|
1449 |
parity = 'n';
|
sl@0
|
1450 |
switch (iostate.c_cflag & (PARENB | PARODD | PAREXT)) {
|
sl@0
|
1451 |
case PARENB : parity = 'e'; break;
|
sl@0
|
1452 |
case PARENB | PARODD : parity = 'o'; break;
|
sl@0
|
1453 |
case PARENB | PAREXT : parity = 's'; break;
|
sl@0
|
1454 |
case PARENB | PARODD | PAREXT : parity = 'm'; break;
|
sl@0
|
1455 |
}
|
sl@0
|
1456 |
|
sl@0
|
1457 |
data = iostate.c_cflag & CSIZE;
|
sl@0
|
1458 |
data = (data == CS5) ? 5 : (data == CS6) ? 6 : (data == CS7) ? 7 : 8;
|
sl@0
|
1459 |
|
sl@0
|
1460 |
stop = (iostate.c_cflag & CSTOPB) ? 2 : 1;
|
sl@0
|
1461 |
#endif /* USE_TERMIO */
|
sl@0
|
1462 |
|
sl@0
|
1463 |
#ifdef USE_SGTTY
|
sl@0
|
1464 |
baud = TtyGetBaud(iostate.sg_ospeed);
|
sl@0
|
1465 |
|
sl@0
|
1466 |
parity = 'n';
|
sl@0
|
1467 |
if (iostate.sg_flags & EVENP) {
|
sl@0
|
1468 |
parity = 'e';
|
sl@0
|
1469 |
} else if (iostate.sg_flags & ODDP) {
|
sl@0
|
1470 |
parity = 'o';
|
sl@0
|
1471 |
}
|
sl@0
|
1472 |
|
sl@0
|
1473 |
data = (iostate.sg_flags & (EVENP | ODDP)) ? 7 : 8;
|
sl@0
|
1474 |
|
sl@0
|
1475 |
stop = 1;
|
sl@0
|
1476 |
#endif /* USE_SGTTY */
|
sl@0
|
1477 |
|
sl@0
|
1478 |
ttyPtr->baud = baud;
|
sl@0
|
1479 |
ttyPtr->parity = parity;
|
sl@0
|
1480 |
ttyPtr->data = data;
|
sl@0
|
1481 |
ttyPtr->stop = stop;
|
sl@0
|
1482 |
}
|
sl@0
|
1483 |
|
sl@0
|
1484 |
/*
|
sl@0
|
1485 |
*---------------------------------------------------------------------------
|
sl@0
|
1486 |
*
|
sl@0
|
1487 |
* TtySetAttributes --
|
sl@0
|
1488 |
*
|
sl@0
|
1489 |
* Set the current attributes of the specified serial device.
|
sl@0
|
1490 |
*
|
sl@0
|
1491 |
* Results:
|
sl@0
|
1492 |
* None.
|
sl@0
|
1493 |
*
|
sl@0
|
1494 |
* Side effects:
|
sl@0
|
1495 |
* None.
|
sl@0
|
1496 |
*
|
sl@0
|
1497 |
*---------------------------------------------------------------------------
|
sl@0
|
1498 |
*/
|
sl@0
|
1499 |
|
sl@0
|
1500 |
static void
|
sl@0
|
1501 |
TtySetAttributes(fd, ttyPtr)
|
sl@0
|
1502 |
int fd; /* Open file descriptor for serial port to
|
sl@0
|
1503 |
* be modified. */
|
sl@0
|
1504 |
TtyAttrs *ttyPtr; /* Buffer containing new attributes for
|
sl@0
|
1505 |
* serial port. */
|
sl@0
|
1506 |
{
|
sl@0
|
1507 |
IOSTATE iostate;
|
sl@0
|
1508 |
|
sl@0
|
1509 |
#ifdef USE_TERMIOS
|
sl@0
|
1510 |
int parity, data, flag;
|
sl@0
|
1511 |
|
sl@0
|
1512 |
GETIOSTATE(fd, &iostate);
|
sl@0
|
1513 |
cfsetospeed(&iostate, TtyGetSpeed(ttyPtr->baud));
|
sl@0
|
1514 |
cfsetispeed(&iostate, TtyGetSpeed(ttyPtr->baud));
|
sl@0
|
1515 |
|
sl@0
|
1516 |
flag = 0;
|
sl@0
|
1517 |
parity = ttyPtr->parity;
|
sl@0
|
1518 |
if (parity != 'n') {
|
sl@0
|
1519 |
flag |= PARENB;
|
sl@0
|
1520 |
#ifdef PAREXT
|
sl@0
|
1521 |
iostate.c_cflag &= ~PAREXT;
|
sl@0
|
1522 |
if ((parity == 'm') || (parity == 's')) {
|
sl@0
|
1523 |
flag |= PAREXT;
|
sl@0
|
1524 |
}
|
sl@0
|
1525 |
#endif /* PAREXT */
|
sl@0
|
1526 |
if ((parity == 'm') || (parity == 'o')) {
|
sl@0
|
1527 |
flag |= PARODD;
|
sl@0
|
1528 |
}
|
sl@0
|
1529 |
}
|
sl@0
|
1530 |
data = ttyPtr->data;
|
sl@0
|
1531 |
flag |= (data == 5) ? CS5 : (data == 6) ? CS6 : (data == 7) ? CS7 : CS8;
|
sl@0
|
1532 |
if (ttyPtr->stop == 2) {
|
sl@0
|
1533 |
flag |= CSTOPB;
|
sl@0
|
1534 |
}
|
sl@0
|
1535 |
|
sl@0
|
1536 |
iostate.c_cflag &= ~(PARENB | PARODD | CSIZE | CSTOPB);
|
sl@0
|
1537 |
iostate.c_cflag |= flag;
|
sl@0
|
1538 |
|
sl@0
|
1539 |
#endif /* USE_TERMIOS */
|
sl@0
|
1540 |
|
sl@0
|
1541 |
#ifdef USE_TERMIO
|
sl@0
|
1542 |
int parity, data, flag;
|
sl@0
|
1543 |
|
sl@0
|
1544 |
GETIOSTATE(fd, &iostate);
|
sl@0
|
1545 |
iostate.c_cflag &= ~CBAUD;
|
sl@0
|
1546 |
iostate.c_cflag |= TtyGetSpeed(ttyPtr->baud);
|
sl@0
|
1547 |
|
sl@0
|
1548 |
flag = 0;
|
sl@0
|
1549 |
parity = ttyPtr->parity;
|
sl@0
|
1550 |
if (parity != 'n') {
|
sl@0
|
1551 |
flag |= PARENB;
|
sl@0
|
1552 |
if ((parity == 'm') || (parity == 's')) {
|
sl@0
|
1553 |
flag |= PAREXT;
|
sl@0
|
1554 |
}
|
sl@0
|
1555 |
if ((parity == 'm') || (parity == 'o')) {
|
sl@0
|
1556 |
flag |= PARODD;
|
sl@0
|
1557 |
}
|
sl@0
|
1558 |
}
|
sl@0
|
1559 |
data = ttyPtr->data;
|
sl@0
|
1560 |
flag |= (data == 5) ? CS5 : (data == 6) ? CS6 : (data == 7) ? CS7 : CS8;
|
sl@0
|
1561 |
if (ttyPtr->stop == 2) {
|
sl@0
|
1562 |
flag |= CSTOPB;
|
sl@0
|
1563 |
}
|
sl@0
|
1564 |
|
sl@0
|
1565 |
iostate.c_cflag &= ~(PARENB | PARODD | PAREXT | CSIZE | CSTOPB);
|
sl@0
|
1566 |
iostate.c_cflag |= flag;
|
sl@0
|
1567 |
|
sl@0
|
1568 |
#endif /* USE_TERMIO */
|
sl@0
|
1569 |
|
sl@0
|
1570 |
#ifdef USE_SGTTY
|
sl@0
|
1571 |
int parity;
|
sl@0
|
1572 |
|
sl@0
|
1573 |
GETIOSTATE(fd, &iostate);
|
sl@0
|
1574 |
iostate.sg_ospeed = TtyGetSpeed(ttyPtr->baud);
|
sl@0
|
1575 |
iostate.sg_ispeed = TtyGetSpeed(ttyPtr->baud);
|
sl@0
|
1576 |
|
sl@0
|
1577 |
parity = ttyPtr->parity;
|
sl@0
|
1578 |
if (parity == 'e') {
|
sl@0
|
1579 |
iostate.sg_flags &= ~ODDP;
|
sl@0
|
1580 |
iostate.sg_flags |= EVENP;
|
sl@0
|
1581 |
} else if (parity == 'o') {
|
sl@0
|
1582 |
iostate.sg_flags &= ~EVENP;
|
sl@0
|
1583 |
iostate.sg_flags |= ODDP;
|
sl@0
|
1584 |
}
|
sl@0
|
1585 |
#endif /* USE_SGTTY */
|
sl@0
|
1586 |
|
sl@0
|
1587 |
SETIOSTATE(fd, &iostate);
|
sl@0
|
1588 |
}
|
sl@0
|
1589 |
|
sl@0
|
1590 |
/*
|
sl@0
|
1591 |
*---------------------------------------------------------------------------
|
sl@0
|
1592 |
*
|
sl@0
|
1593 |
* TtyParseMode --
|
sl@0
|
1594 |
*
|
sl@0
|
1595 |
* Parse the "-mode" argument to the fconfigure command. The argument
|
sl@0
|
1596 |
* is of the form baud,parity,data,stop.
|
sl@0
|
1597 |
*
|
sl@0
|
1598 |
* Results:
|
sl@0
|
1599 |
* The return value is TCL_OK if the argument was successfully
|
sl@0
|
1600 |
* parsed, TCL_ERROR otherwise. If TCL_ERROR is returned, an
|
sl@0
|
1601 |
* error message is left in the interp's result (if interp is non-NULL).
|
sl@0
|
1602 |
*
|
sl@0
|
1603 |
* Side effects:
|
sl@0
|
1604 |
* None.
|
sl@0
|
1605 |
*
|
sl@0
|
1606 |
*---------------------------------------------------------------------------
|
sl@0
|
1607 |
*/
|
sl@0
|
1608 |
|
sl@0
|
1609 |
static int
|
sl@0
|
1610 |
TtyParseMode(interp, mode, speedPtr, parityPtr, dataPtr, stopPtr)
|
sl@0
|
1611 |
Tcl_Interp *interp; /* If non-NULL, interp for error return. */
|
sl@0
|
1612 |
CONST char *mode; /* Mode string to be parsed. */
|
sl@0
|
1613 |
int *speedPtr; /* Filled with baud rate from mode string. */
|
sl@0
|
1614 |
int *parityPtr; /* Filled with parity from mode string. */
|
sl@0
|
1615 |
int *dataPtr; /* Filled with data bits from mode string. */
|
sl@0
|
1616 |
int *stopPtr; /* Filled with stop bits from mode string. */
|
sl@0
|
1617 |
{
|
sl@0
|
1618 |
int i, end;
|
sl@0
|
1619 |
char parity;
|
sl@0
|
1620 |
static char *bad = "bad value for -mode";
|
sl@0
|
1621 |
|
sl@0
|
1622 |
i = sscanf(mode, "%d,%c,%d,%d%n", speedPtr, &parity, dataPtr,
|
sl@0
|
1623 |
stopPtr, &end);
|
sl@0
|
1624 |
if ((i != 4) || (mode[end] != '\0')) {
|
sl@0
|
1625 |
if (interp != NULL) {
|
sl@0
|
1626 |
Tcl_AppendResult(interp, bad, ": should be baud,parity,data,stop",
|
sl@0
|
1627 |
NULL);
|
sl@0
|
1628 |
}
|
sl@0
|
1629 |
return TCL_ERROR;
|
sl@0
|
1630 |
}
|
sl@0
|
1631 |
/*
|
sl@0
|
1632 |
* Only allow setting mark/space parity on platforms that support it
|
sl@0
|
1633 |
* Make sure to allow for the case where strchr is a macro.
|
sl@0
|
1634 |
* [Bug: 5089]
|
sl@0
|
1635 |
*/
|
sl@0
|
1636 |
if (
|
sl@0
|
1637 |
#if defined(PAREXT) || defined(USE_TERMIO)
|
sl@0
|
1638 |
strchr("noems", parity) == NULL
|
sl@0
|
1639 |
#else
|
sl@0
|
1640 |
strchr("noe", parity) == NULL
|
sl@0
|
1641 |
#endif /* PAREXT|USE_TERMIO */
|
sl@0
|
1642 |
) {
|
sl@0
|
1643 |
if (interp != NULL) {
|
sl@0
|
1644 |
Tcl_AppendResult(interp, bad,
|
sl@0
|
1645 |
#if defined(PAREXT) || defined(USE_TERMIO)
|
sl@0
|
1646 |
" parity: should be n, o, e, m, or s",
|
sl@0
|
1647 |
#else
|
sl@0
|
1648 |
" parity: should be n, o, or e",
|
sl@0
|
1649 |
#endif /* PAREXT|USE_TERMIO */
|
sl@0
|
1650 |
NULL);
|
sl@0
|
1651 |
}
|
sl@0
|
1652 |
return TCL_ERROR;
|
sl@0
|
1653 |
}
|
sl@0
|
1654 |
*parityPtr = parity;
|
sl@0
|
1655 |
if ((*dataPtr < 5) || (*dataPtr > 8)) {
|
sl@0
|
1656 |
if (interp != NULL) {
|
sl@0
|
1657 |
Tcl_AppendResult(interp, bad, " data: should be 5, 6, 7, or 8",
|
sl@0
|
1658 |
NULL);
|
sl@0
|
1659 |
}
|
sl@0
|
1660 |
return TCL_ERROR;
|
sl@0
|
1661 |
}
|
sl@0
|
1662 |
if ((*stopPtr < 0) || (*stopPtr > 2)) {
|
sl@0
|
1663 |
if (interp != NULL) {
|
sl@0
|
1664 |
Tcl_AppendResult(interp, bad, " stop: should be 1 or 2", NULL);
|
sl@0
|
1665 |
}
|
sl@0
|
1666 |
return TCL_ERROR;
|
sl@0
|
1667 |
}
|
sl@0
|
1668 |
return TCL_OK;
|
sl@0
|
1669 |
}
|
sl@0
|
1670 |
|
sl@0
|
1671 |
/*
|
sl@0
|
1672 |
*---------------------------------------------------------------------------
|
sl@0
|
1673 |
*
|
sl@0
|
1674 |
* TtyInit --
|
sl@0
|
1675 |
*
|
sl@0
|
1676 |
* Given file descriptor that refers to a serial port,
|
sl@0
|
1677 |
* initialize the serial port to a set of sane values so that
|
sl@0
|
1678 |
* Tcl can talk to a device located on the serial port.
|
sl@0
|
1679 |
* Note that no initialization happens if the initialize flag
|
sl@0
|
1680 |
* is not set; this is necessary for the correct handling of
|
sl@0
|
1681 |
* UNIX console TTYs at startup.
|
sl@0
|
1682 |
*
|
sl@0
|
1683 |
* Results:
|
sl@0
|
1684 |
* A pointer to a FileState suitable for use with Tcl_CreateChannel
|
sl@0
|
1685 |
* and the ttyChannelType structure.
|
sl@0
|
1686 |
*
|
sl@0
|
1687 |
* Side effects:
|
sl@0
|
1688 |
* Serial device initialized to non-blocking raw mode, similar to
|
sl@0
|
1689 |
* sockets (if initialize flag is non-zero.) All other modes can
|
sl@0
|
1690 |
* be simulated on top of this in Tcl.
|
sl@0
|
1691 |
*
|
sl@0
|
1692 |
*---------------------------------------------------------------------------
|
sl@0
|
1693 |
*/
|
sl@0
|
1694 |
|
sl@0
|
1695 |
static FileState *
|
sl@0
|
1696 |
TtyInit(fd, initialize)
|
sl@0
|
1697 |
int fd; /* Open file descriptor for serial port to
|
sl@0
|
1698 |
* be initialized. */
|
sl@0
|
1699 |
int initialize;
|
sl@0
|
1700 |
{
|
sl@0
|
1701 |
TtyState *ttyPtr;
|
sl@0
|
1702 |
|
sl@0
|
1703 |
ttyPtr = (TtyState *) ckalloc((unsigned) sizeof(TtyState));
|
sl@0
|
1704 |
GETIOSTATE(fd, &ttyPtr->savedState);
|
sl@0
|
1705 |
ttyPtr->stateUpdated = 0;
|
sl@0
|
1706 |
if (initialize) {
|
sl@0
|
1707 |
IOSTATE iostate = ttyPtr->savedState;
|
sl@0
|
1708 |
|
sl@0
|
1709 |
#if defined(USE_TERMIOS) || defined(USE_TERMIO)
|
sl@0
|
1710 |
if (iostate.c_iflag != IGNBRK ||
|
sl@0
|
1711 |
iostate.c_oflag != 0 ||
|
sl@0
|
1712 |
iostate.c_lflag != 0 ||
|
sl@0
|
1713 |
iostate.c_cflag & CREAD ||
|
sl@0
|
1714 |
iostate.c_cc[VMIN] != 1 ||
|
sl@0
|
1715 |
iostate.c_cc[VTIME] != 0) {
|
sl@0
|
1716 |
ttyPtr->stateUpdated = 1;
|
sl@0
|
1717 |
}
|
sl@0
|
1718 |
iostate.c_iflag = IGNBRK;
|
sl@0
|
1719 |
iostate.c_oflag = 0;
|
sl@0
|
1720 |
iostate.c_lflag = 0;
|
sl@0
|
1721 |
iostate.c_cflag |= CREAD;
|
sl@0
|
1722 |
iostate.c_cc[VMIN] = 1;
|
sl@0
|
1723 |
iostate.c_cc[VTIME] = 0;
|
sl@0
|
1724 |
#endif /* USE_TERMIOS|USE_TERMIO */
|
sl@0
|
1725 |
|
sl@0
|
1726 |
#ifdef USE_SGTTY
|
sl@0
|
1727 |
if ((iostate.sg_flags & (EVENP | ODDP)) ||
|
sl@0
|
1728 |
!(iostate.sg_flags & RAW)) {
|
sl@0
|
1729 |
ttyPtr->stateUpdated = 1;
|
sl@0
|
1730 |
}
|
sl@0
|
1731 |
iostate.sg_flags &= (EVENP | ODDP);
|
sl@0
|
1732 |
iostate.sg_flags |= RAW;
|
sl@0
|
1733 |
#endif /* USE_SGTTY */
|
sl@0
|
1734 |
|
sl@0
|
1735 |
/*
|
sl@0
|
1736 |
* Only update if we're changing anything to avoid possible
|
sl@0
|
1737 |
* blocking.
|
sl@0
|
1738 |
*/
|
sl@0
|
1739 |
if (ttyPtr->stateUpdated) {
|
sl@0
|
1740 |
SETIOSTATE(fd, &iostate);
|
sl@0
|
1741 |
}
|
sl@0
|
1742 |
}
|
sl@0
|
1743 |
|
sl@0
|
1744 |
return &ttyPtr->fs;
|
sl@0
|
1745 |
}
|
sl@0
|
1746 |
#endif /* SUPPORTS_TTY */
|
sl@0
|
1747 |
|
sl@0
|
1748 |
/*
|
sl@0
|
1749 |
*----------------------------------------------------------------------
|
sl@0
|
1750 |
*
|
sl@0
|
1751 |
* TclpOpenFileChannel --
|
sl@0
|
1752 |
*
|
sl@0
|
1753 |
* Open an file based channel on Unix systems.
|
sl@0
|
1754 |
*
|
sl@0
|
1755 |
* Results:
|
sl@0
|
1756 |
* The new channel or NULL. If NULL, the output argument
|
sl@0
|
1757 |
* errorCodePtr is set to a POSIX error and an error message is
|
sl@0
|
1758 |
* left in the interp's result if interp is not NULL.
|
sl@0
|
1759 |
*
|
sl@0
|
1760 |
* Side effects:
|
sl@0
|
1761 |
* May open the channel and may cause creation of a file on the
|
sl@0
|
1762 |
* file system.
|
sl@0
|
1763 |
*
|
sl@0
|
1764 |
*----------------------------------------------------------------------
|
sl@0
|
1765 |
*/
|
sl@0
|
1766 |
|
sl@0
|
1767 |
Tcl_Channel
|
sl@0
|
1768 |
TclpOpenFileChannel(interp, pathPtr, mode, permissions)
|
sl@0
|
1769 |
Tcl_Interp *interp; /* Interpreter for error reporting;
|
sl@0
|
1770 |
* can be NULL. */
|
sl@0
|
1771 |
Tcl_Obj *pathPtr; /* Name of file to open. */
|
sl@0
|
1772 |
int mode; /* POSIX open mode. */
|
sl@0
|
1773 |
int permissions; /* If the open involves creating a
|
sl@0
|
1774 |
* file, with what modes to create
|
sl@0
|
1775 |
* it? */
|
sl@0
|
1776 |
{
|
sl@0
|
1777 |
int fd, channelPermissions;
|
sl@0
|
1778 |
FileState *fsPtr;
|
sl@0
|
1779 |
CONST char *native, *translation;
|
sl@0
|
1780 |
char channelName[16 + TCL_INTEGER_SPACE];
|
sl@0
|
1781 |
Tcl_ChannelType *channelTypePtr;
|
sl@0
|
1782 |
#ifdef SUPPORTS_TTY
|
sl@0
|
1783 |
int ctl_tty;
|
sl@0
|
1784 |
#endif /* SUPPORTS_TTY */
|
sl@0
|
1785 |
#ifdef DEPRECATED
|
sl@0
|
1786 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
|
sl@0
|
1787 |
#endif /* DEPRECATED */
|
sl@0
|
1788 |
|
sl@0
|
1789 |
switch (mode & (O_RDONLY | O_WRONLY | O_RDWR)) {
|
sl@0
|
1790 |
case O_RDONLY:
|
sl@0
|
1791 |
channelPermissions = TCL_READABLE;
|
sl@0
|
1792 |
break;
|
sl@0
|
1793 |
case O_WRONLY:
|
sl@0
|
1794 |
channelPermissions = TCL_WRITABLE;
|
sl@0
|
1795 |
break;
|
sl@0
|
1796 |
case O_RDWR:
|
sl@0
|
1797 |
channelPermissions = (TCL_READABLE | TCL_WRITABLE);
|
sl@0
|
1798 |
break;
|
sl@0
|
1799 |
default:
|
sl@0
|
1800 |
/*
|
sl@0
|
1801 |
* This may occurr if modeString was "", for example.
|
sl@0
|
1802 |
*/
|
sl@0
|
1803 |
panic("TclpOpenFileChannel: invalid mode value");
|
sl@0
|
1804 |
return NULL;
|
sl@0
|
1805 |
}
|
sl@0
|
1806 |
|
sl@0
|
1807 |
native = Tcl_FSGetNativePath(pathPtr);
|
sl@0
|
1808 |
if (native == NULL) {
|
sl@0
|
1809 |
return NULL;
|
sl@0
|
1810 |
}
|
sl@0
|
1811 |
fd = TclOSopen(native, mode, permissions);
|
sl@0
|
1812 |
#ifdef SUPPORTS_TTY
|
sl@0
|
1813 |
ctl_tty = (strcmp (native, "/dev/tty") == 0);
|
sl@0
|
1814 |
#endif /* SUPPORTS_TTY */
|
sl@0
|
1815 |
|
sl@0
|
1816 |
if (fd < 0) {
|
sl@0
|
1817 |
if (interp != (Tcl_Interp *) NULL) {
|
sl@0
|
1818 |
Tcl_AppendResult(interp, "couldn't open \"",
|
sl@0
|
1819 |
Tcl_GetString(pathPtr), "\": ",
|
sl@0
|
1820 |
Tcl_PosixError(interp), (char *) NULL);
|
sl@0
|
1821 |
}
|
sl@0
|
1822 |
return NULL;
|
sl@0
|
1823 |
}
|
sl@0
|
1824 |
|
sl@0
|
1825 |
/*
|
sl@0
|
1826 |
* Set close-on-exec flag on the fd so that child processes will not
|
sl@0
|
1827 |
* inherit this fd.
|
sl@0
|
1828 |
*/
|
sl@0
|
1829 |
|
sl@0
|
1830 |
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
sl@0
|
1831 |
|
sl@0
|
1832 |
sprintf(channelName, "file%d", fd);
|
sl@0
|
1833 |
|
sl@0
|
1834 |
#ifdef SUPPORTS_TTY
|
sl@0
|
1835 |
if (!ctl_tty && isatty(fd)) {
|
sl@0
|
1836 |
/*
|
sl@0
|
1837 |
* Initialize the serial port to a set of sane parameters.
|
sl@0
|
1838 |
* Especially important if the remote device is set to echo and
|
sl@0
|
1839 |
* the serial port driver was also set to echo -- as soon as a char
|
sl@0
|
1840 |
* were sent to the serial port, the remote device would echo it,
|
sl@0
|
1841 |
* then the serial driver would echo it back to the device, etc.
|
sl@0
|
1842 |
*/
|
sl@0
|
1843 |
|
sl@0
|
1844 |
translation = "auto crlf";
|
sl@0
|
1845 |
channelTypePtr = &ttyChannelType;
|
sl@0
|
1846 |
fsPtr = TtyInit(fd, 1);
|
sl@0
|
1847 |
} else
|
sl@0
|
1848 |
#endif /* SUPPORTS_TTY */
|
sl@0
|
1849 |
{
|
sl@0
|
1850 |
translation = NULL;
|
sl@0
|
1851 |
channelTypePtr = &fileChannelType;
|
sl@0
|
1852 |
fsPtr = (FileState *) ckalloc((unsigned) sizeof(FileState));
|
sl@0
|
1853 |
}
|
sl@0
|
1854 |
|
sl@0
|
1855 |
#ifdef DEPRECATED
|
sl@0
|
1856 |
if (channelTypePtr == &fileChannelType) {
|
sl@0
|
1857 |
/* TIP #218. Removed the code inserting the new structure
|
sl@0
|
1858 |
* into the global list. This is now handled in the thread
|
sl@0
|
1859 |
* action callbacks, and only there.
|
sl@0
|
1860 |
*/
|
sl@0
|
1861 |
fsPtr->nextPtr = NULL;
|
sl@0
|
1862 |
}
|
sl@0
|
1863 |
#endif /* DEPRECATED */
|
sl@0
|
1864 |
fsPtr->validMask = channelPermissions | TCL_EXCEPTION;
|
sl@0
|
1865 |
fsPtr->fd = fd;
|
sl@0
|
1866 |
|
sl@0
|
1867 |
fsPtr->channel = Tcl_CreateChannel(channelTypePtr, channelName,
|
sl@0
|
1868 |
(ClientData) fsPtr, channelPermissions);
|
sl@0
|
1869 |
|
sl@0
|
1870 |
if (translation != NULL) {
|
sl@0
|
1871 |
/*
|
sl@0
|
1872 |
* Gotcha. Most modems need a "\r" at the end of the command
|
sl@0
|
1873 |
* sequence. If you just send "at\n", the modem will not respond
|
sl@0
|
1874 |
* with "OK" because it never got a "\r" to actually invoke the
|
sl@0
|
1875 |
* command. So, by default, newlines are translated to "\r\n" on
|
sl@0
|
1876 |
* output to avoid "bug" reports that the serial port isn't working.
|
sl@0
|
1877 |
*/
|
sl@0
|
1878 |
|
sl@0
|
1879 |
if (Tcl_SetChannelOption(interp, fsPtr->channel, "-translation",
|
sl@0
|
1880 |
translation) != TCL_OK) {
|
sl@0
|
1881 |
Tcl_Close(NULL, fsPtr->channel);
|
sl@0
|
1882 |
return NULL;
|
sl@0
|
1883 |
}
|
sl@0
|
1884 |
}
|
sl@0
|
1885 |
|
sl@0
|
1886 |
return fsPtr->channel;
|
sl@0
|
1887 |
}
|
sl@0
|
1888 |
|
sl@0
|
1889 |
/*
|
sl@0
|
1890 |
*----------------------------------------------------------------------
|
sl@0
|
1891 |
*
|
sl@0
|
1892 |
* Tcl_MakeFileChannel --
|
sl@0
|
1893 |
*
|
sl@0
|
1894 |
* Makes a Tcl_Channel from an existing OS level file handle.
|
sl@0
|
1895 |
*
|
sl@0
|
1896 |
* Results:
|
sl@0
|
1897 |
* The Tcl_Channel created around the preexisting OS level file handle.
|
sl@0
|
1898 |
*
|
sl@0
|
1899 |
* Side effects:
|
sl@0
|
1900 |
* None.
|
sl@0
|
1901 |
*
|
sl@0
|
1902 |
*----------------------------------------------------------------------
|
sl@0
|
1903 |
*/
|
sl@0
|
1904 |
|
sl@0
|
1905 |
EXPORT_C Tcl_Channel
|
sl@0
|
1906 |
Tcl_MakeFileChannel(handle, mode)
|
sl@0
|
1907 |
ClientData handle; /* OS level handle. */
|
sl@0
|
1908 |
int mode; /* ORed combination of TCL_READABLE and
|
sl@0
|
1909 |
* TCL_WRITABLE to indicate file mode. */
|
sl@0
|
1910 |
{
|
sl@0
|
1911 |
FileState *fsPtr;
|
sl@0
|
1912 |
char channelName[16 + TCL_INTEGER_SPACE];
|
sl@0
|
1913 |
int fd = (int) handle;
|
sl@0
|
1914 |
Tcl_ChannelType *channelTypePtr;
|
sl@0
|
1915 |
#ifdef DEPRECATED
|
sl@0
|
1916 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
|
sl@0
|
1917 |
#endif /* DEPRECATED */
|
sl@0
|
1918 |
struct sockaddr sockaddr;
|
sl@0
|
1919 |
socklen_t sockaddrLen = sizeof(sockaddr);
|
sl@0
|
1920 |
|
sl@0
|
1921 |
if (mode == 0) {
|
sl@0
|
1922 |
return NULL;
|
sl@0
|
1923 |
}
|
sl@0
|
1924 |
|
sl@0
|
1925 |
|
sl@0
|
1926 |
/*
|
sl@0
|
1927 |
* Look to see if a channel with this fd and the same mode already exists.
|
sl@0
|
1928 |
* If the fd is used, but the mode doesn't match, return NULL.
|
sl@0
|
1929 |
*/
|
sl@0
|
1930 |
|
sl@0
|
1931 |
#ifdef DEPRECATED
|
sl@0
|
1932 |
for (fsPtr = tsdPtr->firstFilePtr; fsPtr != NULL; fsPtr = fsPtr->nextPtr) {
|
sl@0
|
1933 |
if (fsPtr->fd == fd) {
|
sl@0
|
1934 |
return ((mode|TCL_EXCEPTION) == fsPtr->validMask) ?
|
sl@0
|
1935 |
fsPtr->channel : NULL;
|
sl@0
|
1936 |
}
|
sl@0
|
1937 |
}
|
sl@0
|
1938 |
#endif /* DEPRECATED */
|
sl@0
|
1939 |
|
sl@0
|
1940 |
sockaddr.sa_family = AF_UNSPEC;
|
sl@0
|
1941 |
|
sl@0
|
1942 |
#ifdef SUPPORTS_TTY
|
sl@0
|
1943 |
if (isatty(fd)) {
|
sl@0
|
1944 |
fsPtr = TtyInit(fd, 0);
|
sl@0
|
1945 |
channelTypePtr = &ttyChannelType;
|
sl@0
|
1946 |
sprintf(channelName, "serial%d", fd);
|
sl@0
|
1947 |
} else
|
sl@0
|
1948 |
#endif /* SUPPORTS_TTY */
|
sl@0
|
1949 |
if (getsockname(fd, (struct sockaddr *)&sockaddr, &sockaddrLen) == 0
|
sl@0
|
1950 |
&& sockaddrLen > 0
|
sl@0
|
1951 |
&& sockaddr.sa_family == AF_INET) {
|
sl@0
|
1952 |
return MakeTcpClientChannelMode((ClientData) fd, mode);
|
sl@0
|
1953 |
} else {
|
sl@0
|
1954 |
channelTypePtr = &fileChannelType;
|
sl@0
|
1955 |
fsPtr = (FileState *) ckalloc((unsigned) sizeof(FileState));
|
sl@0
|
1956 |
sprintf(channelName, "file%d", fd);
|
sl@0
|
1957 |
}
|
sl@0
|
1958 |
|
sl@0
|
1959 |
#ifdef DEPRECATED
|
sl@0
|
1960 |
if (channelTypePtr == &fileChannelType) {
|
sl@0
|
1961 |
fsPtr->nextPtr = tsdPtr->firstFilePtr;
|
sl@0
|
1962 |
tsdPtr->firstFilePtr = fsPtr;
|
sl@0
|
1963 |
}
|
sl@0
|
1964 |
#endif /* DEPRECATED */
|
sl@0
|
1965 |
fsPtr->fd = fd;
|
sl@0
|
1966 |
fsPtr->validMask = mode | TCL_EXCEPTION;
|
sl@0
|
1967 |
fsPtr->channel = Tcl_CreateChannel(channelTypePtr, channelName,
|
sl@0
|
1968 |
(ClientData) fsPtr, mode);
|
sl@0
|
1969 |
|
sl@0
|
1970 |
return fsPtr->channel;
|
sl@0
|
1971 |
}
|
sl@0
|
1972 |
|
sl@0
|
1973 |
/*
|
sl@0
|
1974 |
*----------------------------------------------------------------------
|
sl@0
|
1975 |
*
|
sl@0
|
1976 |
* TcpBlockModeProc --
|
sl@0
|
1977 |
*
|
sl@0
|
1978 |
* This procedure is invoked by the generic IO level to set blocking
|
sl@0
|
1979 |
* and nonblocking mode on a TCP socket based channel.
|
sl@0
|
1980 |
*
|
sl@0
|
1981 |
* Results:
|
sl@0
|
1982 |
* 0 if successful, errno when failed.
|
sl@0
|
1983 |
*
|
sl@0
|
1984 |
* Side effects:
|
sl@0
|
1985 |
* Sets the device into blocking or nonblocking mode.
|
sl@0
|
1986 |
*
|
sl@0
|
1987 |
*----------------------------------------------------------------------
|
sl@0
|
1988 |
*/
|
sl@0
|
1989 |
|
sl@0
|
1990 |
/* ARGSUSED */
|
sl@0
|
1991 |
static int
|
sl@0
|
1992 |
TcpBlockModeProc(instanceData, mode)
|
sl@0
|
1993 |
ClientData instanceData; /* Socket state. */
|
sl@0
|
1994 |
int mode; /* The mode to set. Can be one of
|
sl@0
|
1995 |
* TCL_MODE_BLOCKING or
|
sl@0
|
1996 |
* TCL_MODE_NONBLOCKING. */
|
sl@0
|
1997 |
{
|
sl@0
|
1998 |
TcpState *statePtr = (TcpState *) instanceData;
|
sl@0
|
1999 |
int setting;
|
sl@0
|
2000 |
|
sl@0
|
2001 |
#ifndef USE_FIONBIO
|
sl@0
|
2002 |
setting = fcntl(statePtr->fd, F_GETFL);
|
sl@0
|
2003 |
if (mode == TCL_MODE_BLOCKING) {
|
sl@0
|
2004 |
statePtr->flags &= (~(TCP_ASYNC_SOCKET));
|
sl@0
|
2005 |
setting &= (~(O_NONBLOCK));
|
sl@0
|
2006 |
} else {
|
sl@0
|
2007 |
statePtr->flags |= TCP_ASYNC_SOCKET;
|
sl@0
|
2008 |
setting |= O_NONBLOCK;
|
sl@0
|
2009 |
}
|
sl@0
|
2010 |
if (fcntl(statePtr->fd, F_SETFL, setting) < 0) {
|
sl@0
|
2011 |
return errno;
|
sl@0
|
2012 |
}
|
sl@0
|
2013 |
#else /* USE_FIONBIO */
|
sl@0
|
2014 |
if (mode == TCL_MODE_BLOCKING) {
|
sl@0
|
2015 |
statePtr->flags &= (~(TCP_ASYNC_SOCKET));
|
sl@0
|
2016 |
setting = 0;
|
sl@0
|
2017 |
if (ioctl(statePtr->fd, (int) FIONBIO, &setting) == -1) {
|
sl@0
|
2018 |
return errno;
|
sl@0
|
2019 |
}
|
sl@0
|
2020 |
} else {
|
sl@0
|
2021 |
statePtr->flags |= TCP_ASYNC_SOCKET;
|
sl@0
|
2022 |
setting = 1;
|
sl@0
|
2023 |
if (ioctl(statePtr->fd, (int) FIONBIO, &setting) == -1) {
|
sl@0
|
2024 |
return errno;
|
sl@0
|
2025 |
}
|
sl@0
|
2026 |
}
|
sl@0
|
2027 |
#endif /* !USE_FIONBIO */
|
sl@0
|
2028 |
|
sl@0
|
2029 |
return 0;
|
sl@0
|
2030 |
}
|
sl@0
|
2031 |
|
sl@0
|
2032 |
/*
|
sl@0
|
2033 |
*----------------------------------------------------------------------
|
sl@0
|
2034 |
*
|
sl@0
|
2035 |
* WaitForConnect --
|
sl@0
|
2036 |
*
|
sl@0
|
2037 |
* Waits for a connection on an asynchronously opened socket to
|
sl@0
|
2038 |
* be completed.
|
sl@0
|
2039 |
*
|
sl@0
|
2040 |
* Results:
|
sl@0
|
2041 |
* None.
|
sl@0
|
2042 |
*
|
sl@0
|
2043 |
* Side effects:
|
sl@0
|
2044 |
* The socket is connected after this function returns.
|
sl@0
|
2045 |
*
|
sl@0
|
2046 |
*----------------------------------------------------------------------
|
sl@0
|
2047 |
*/
|
sl@0
|
2048 |
|
sl@0
|
2049 |
static int
|
sl@0
|
2050 |
WaitForConnect(statePtr, errorCodePtr)
|
sl@0
|
2051 |
TcpState *statePtr; /* State of the socket. */
|
sl@0
|
2052 |
int *errorCodePtr; /* Where to store errors? */
|
sl@0
|
2053 |
{
|
sl@0
|
2054 |
int timeOut; /* How long to wait. */
|
sl@0
|
2055 |
int state; /* Of calling TclWaitForFile. */
|
sl@0
|
2056 |
int flags; /* fcntl flags for the socket. */
|
sl@0
|
2057 |
|
sl@0
|
2058 |
/*
|
sl@0
|
2059 |
* If an asynchronous connect is in progress, attempt to wait for it
|
sl@0
|
2060 |
* to complete before reading.
|
sl@0
|
2061 |
*/
|
sl@0
|
2062 |
|
sl@0
|
2063 |
if (statePtr->flags & TCP_ASYNC_CONNECT) {
|
sl@0
|
2064 |
if (statePtr->flags & TCP_ASYNC_SOCKET) {
|
sl@0
|
2065 |
timeOut = 0;
|
sl@0
|
2066 |
} else {
|
sl@0
|
2067 |
timeOut = -1;
|
sl@0
|
2068 |
}
|
sl@0
|
2069 |
errno = 0;
|
sl@0
|
2070 |
state = TclUnixWaitForFile(statePtr->fd,
|
sl@0
|
2071 |
TCL_WRITABLE | TCL_EXCEPTION, timeOut);
|
sl@0
|
2072 |
if (!(statePtr->flags & TCP_ASYNC_SOCKET)) {
|
sl@0
|
2073 |
#ifndef USE_FIONBIO
|
sl@0
|
2074 |
flags = fcntl(statePtr->fd, F_GETFL);
|
sl@0
|
2075 |
flags &= (~(O_NONBLOCK));
|
sl@0
|
2076 |
(void) fcntl(statePtr->fd, F_SETFL, flags);
|
sl@0
|
2077 |
#else /* USE_FIONBIO */
|
sl@0
|
2078 |
flags = 0;
|
sl@0
|
2079 |
(void) ioctl(statePtr->fd, FIONBIO, &flags);
|
sl@0
|
2080 |
#endif /* !USE_FIONBIO */
|
sl@0
|
2081 |
}
|
sl@0
|
2082 |
if (state & TCL_EXCEPTION) {
|
sl@0
|
2083 |
return -1;
|
sl@0
|
2084 |
}
|
sl@0
|
2085 |
if (state & TCL_WRITABLE) {
|
sl@0
|
2086 |
statePtr->flags &= (~(TCP_ASYNC_CONNECT));
|
sl@0
|
2087 |
} else if (timeOut == 0) {
|
sl@0
|
2088 |
*errorCodePtr = errno = EWOULDBLOCK;
|
sl@0
|
2089 |
return -1;
|
sl@0
|
2090 |
}
|
sl@0
|
2091 |
}
|
sl@0
|
2092 |
return 0;
|
sl@0
|
2093 |
}
|
sl@0
|
2094 |
|
sl@0
|
2095 |
/*
|
sl@0
|
2096 |
*----------------------------------------------------------------------
|
sl@0
|
2097 |
*
|
sl@0
|
2098 |
* TcpInputProc --
|
sl@0
|
2099 |
*
|
sl@0
|
2100 |
* This procedure is invoked by the generic IO level to read input
|
sl@0
|
2101 |
* from a TCP socket based channel.
|
sl@0
|
2102 |
*
|
sl@0
|
2103 |
* NOTE: We cannot share code with FilePipeInputProc because here
|
sl@0
|
2104 |
* we must use recv to obtain the input from the channel, not read.
|
sl@0
|
2105 |
*
|
sl@0
|
2106 |
* Results:
|
sl@0
|
2107 |
* The number of bytes read is returned or -1 on error. An output
|
sl@0
|
2108 |
* argument contains the POSIX error code on error, or zero if no
|
sl@0
|
2109 |
* error occurred.
|
sl@0
|
2110 |
*
|
sl@0
|
2111 |
* Side effects:
|
sl@0
|
2112 |
* Reads input from the input device of the channel.
|
sl@0
|
2113 |
*
|
sl@0
|
2114 |
*----------------------------------------------------------------------
|
sl@0
|
2115 |
*/
|
sl@0
|
2116 |
|
sl@0
|
2117 |
/* ARGSUSED */
|
sl@0
|
2118 |
static int
|
sl@0
|
2119 |
TcpInputProc(instanceData, buf, bufSize, errorCodePtr)
|
sl@0
|
2120 |
ClientData instanceData; /* Socket state. */
|
sl@0
|
2121 |
char *buf; /* Where to store data read. */
|
sl@0
|
2122 |
int bufSize; /* How much space is available
|
sl@0
|
2123 |
* in the buffer? */
|
sl@0
|
2124 |
int *errorCodePtr; /* Where to store error code. */
|
sl@0
|
2125 |
{
|
sl@0
|
2126 |
TcpState *statePtr = (TcpState *) instanceData;
|
sl@0
|
2127 |
int bytesRead, state;
|
sl@0
|
2128 |
|
sl@0
|
2129 |
*errorCodePtr = 0;
|
sl@0
|
2130 |
state = WaitForConnect(statePtr, errorCodePtr);
|
sl@0
|
2131 |
if (state != 0) {
|
sl@0
|
2132 |
return -1;
|
sl@0
|
2133 |
}
|
sl@0
|
2134 |
bytesRead = recv(statePtr->fd, buf, (size_t) bufSize, 0);
|
sl@0
|
2135 |
if (bytesRead > -1) {
|
sl@0
|
2136 |
return bytesRead;
|
sl@0
|
2137 |
}
|
sl@0
|
2138 |
if (errno == ECONNRESET) {
|
sl@0
|
2139 |
/*
|
sl@0
|
2140 |
* Turn ECONNRESET into a soft EOF condition.
|
sl@0
|
2141 |
*/
|
sl@0
|
2142 |
|
sl@0
|
2143 |
return 0;
|
sl@0
|
2144 |
}
|
sl@0
|
2145 |
*errorCodePtr = errno;
|
sl@0
|
2146 |
return -1;
|
sl@0
|
2147 |
}
|
sl@0
|
2148 |
|
sl@0
|
2149 |
/*
|
sl@0
|
2150 |
*----------------------------------------------------------------------
|
sl@0
|
2151 |
*
|
sl@0
|
2152 |
* TcpOutputProc --
|
sl@0
|
2153 |
*
|
sl@0
|
2154 |
* This procedure is invoked by the generic IO level to write output
|
sl@0
|
2155 |
* to a TCP socket based channel.
|
sl@0
|
2156 |
*
|
sl@0
|
2157 |
* NOTE: We cannot share code with FilePipeOutputProc because here
|
sl@0
|
2158 |
* we must use send, not write, to get reliable error reporting.
|
sl@0
|
2159 |
*
|
sl@0
|
2160 |
* Results:
|
sl@0
|
2161 |
* The number of bytes written is returned. An output argument is
|
sl@0
|
2162 |
* set to a POSIX error code if an error occurred, or zero.
|
sl@0
|
2163 |
*
|
sl@0
|
2164 |
* Side effects:
|
sl@0
|
2165 |
* Writes output on the output device of the channel.
|
sl@0
|
2166 |
*
|
sl@0
|
2167 |
*----------------------------------------------------------------------
|
sl@0
|
2168 |
*/
|
sl@0
|
2169 |
|
sl@0
|
2170 |
static int
|
sl@0
|
2171 |
TcpOutputProc(instanceData, buf, toWrite, errorCodePtr)
|
sl@0
|
2172 |
ClientData instanceData; /* Socket state. */
|
sl@0
|
2173 |
CONST char *buf; /* The data buffer. */
|
sl@0
|
2174 |
int toWrite; /* How many bytes to write? */
|
sl@0
|
2175 |
int *errorCodePtr; /* Where to store error code. */
|
sl@0
|
2176 |
{
|
sl@0
|
2177 |
TcpState *statePtr = (TcpState *) instanceData;
|
sl@0
|
2178 |
int written;
|
sl@0
|
2179 |
int state; /* Of waiting for connection. */
|
sl@0
|
2180 |
|
sl@0
|
2181 |
*errorCodePtr = 0;
|
sl@0
|
2182 |
state = WaitForConnect(statePtr, errorCodePtr);
|
sl@0
|
2183 |
if (state != 0) {
|
sl@0
|
2184 |
return -1;
|
sl@0
|
2185 |
}
|
sl@0
|
2186 |
written = send(statePtr->fd, buf, (size_t) toWrite, 0);
|
sl@0
|
2187 |
if (written > -1) {
|
sl@0
|
2188 |
return written;
|
sl@0
|
2189 |
}
|
sl@0
|
2190 |
*errorCodePtr = errno;
|
sl@0
|
2191 |
return -1;
|
sl@0
|
2192 |
}
|
sl@0
|
2193 |
|
sl@0
|
2194 |
/*
|
sl@0
|
2195 |
*----------------------------------------------------------------------
|
sl@0
|
2196 |
*
|
sl@0
|
2197 |
* TcpCloseProc --
|
sl@0
|
2198 |
*
|
sl@0
|
2199 |
* This procedure is invoked by the generic IO level to perform
|
sl@0
|
2200 |
* channel-type-specific cleanup when a TCP socket based channel
|
sl@0
|
2201 |
* is closed.
|
sl@0
|
2202 |
*
|
sl@0
|
2203 |
* Results:
|
sl@0
|
2204 |
* 0 if successful, the value of errno if failed.
|
sl@0
|
2205 |
*
|
sl@0
|
2206 |
* Side effects:
|
sl@0
|
2207 |
* Closes the socket of the channel.
|
sl@0
|
2208 |
*
|
sl@0
|
2209 |
*----------------------------------------------------------------------
|
sl@0
|
2210 |
*/
|
sl@0
|
2211 |
|
sl@0
|
2212 |
/* ARGSUSED */
|
sl@0
|
2213 |
static int
|
sl@0
|
2214 |
TcpCloseProc(instanceData, interp)
|
sl@0
|
2215 |
ClientData instanceData; /* The socket to close. */
|
sl@0
|
2216 |
Tcl_Interp *interp; /* For error reporting - unused. */
|
sl@0
|
2217 |
{
|
sl@0
|
2218 |
TcpState *statePtr = (TcpState *) instanceData;
|
sl@0
|
2219 |
int errorCode = 0;
|
sl@0
|
2220 |
|
sl@0
|
2221 |
/*
|
sl@0
|
2222 |
* Delete a file handler that may be active for this socket if this
|
sl@0
|
2223 |
* is a server socket - the file handler was created automatically
|
sl@0
|
2224 |
* by Tcl as part of the mechanism to accept new client connections.
|
sl@0
|
2225 |
* Channel handlers are already deleted in the generic IO channel
|
sl@0
|
2226 |
* closing code that called this function, so we do not have to
|
sl@0
|
2227 |
* delete them here.
|
sl@0
|
2228 |
*/
|
sl@0
|
2229 |
|
sl@0
|
2230 |
Tcl_DeleteFileHandler(statePtr->fd);
|
sl@0
|
2231 |
|
sl@0
|
2232 |
if (close(statePtr->fd) < 0) {
|
sl@0
|
2233 |
errorCode = errno;
|
sl@0
|
2234 |
}
|
sl@0
|
2235 |
ckfree((char *) statePtr);
|
sl@0
|
2236 |
|
sl@0
|
2237 |
return errorCode;
|
sl@0
|
2238 |
}
|
sl@0
|
2239 |
|
sl@0
|
2240 |
/*
|
sl@0
|
2241 |
*----------------------------------------------------------------------
|
sl@0
|
2242 |
*
|
sl@0
|
2243 |
* TcpGetOptionProc --
|
sl@0
|
2244 |
*
|
sl@0
|
2245 |
* Computes an option value for a TCP socket based channel, or a
|
sl@0
|
2246 |
* list of all options and their values.
|
sl@0
|
2247 |
*
|
sl@0
|
2248 |
* Note: This code is based on code contributed by John Haxby.
|
sl@0
|
2249 |
*
|
sl@0
|
2250 |
* Results:
|
sl@0
|
2251 |
* A standard Tcl result. The value of the specified option or a
|
sl@0
|
2252 |
* list of all options and their values is returned in the
|
sl@0
|
2253 |
* supplied DString. Sets Error message if needed.
|
sl@0
|
2254 |
*
|
sl@0
|
2255 |
* Side effects:
|
sl@0
|
2256 |
* None.
|
sl@0
|
2257 |
*
|
sl@0
|
2258 |
*----------------------------------------------------------------------
|
sl@0
|
2259 |
*/
|
sl@0
|
2260 |
|
sl@0
|
2261 |
static int
|
sl@0
|
2262 |
TcpGetOptionProc(instanceData, interp, optionName, dsPtr)
|
sl@0
|
2263 |
ClientData instanceData; /* Socket state. */
|
sl@0
|
2264 |
Tcl_Interp *interp; /* For error reporting - can be NULL. */
|
sl@0
|
2265 |
CONST char *optionName; /* Name of the option to
|
sl@0
|
2266 |
* retrieve the value for, or
|
sl@0
|
2267 |
* NULL to get all options and
|
sl@0
|
2268 |
* their values. */
|
sl@0
|
2269 |
Tcl_DString *dsPtr; /* Where to store the computed
|
sl@0
|
2270 |
* value; initialized by caller. */
|
sl@0
|
2271 |
{
|
sl@0
|
2272 |
TcpState *statePtr = (TcpState *) instanceData;
|
sl@0
|
2273 |
struct sockaddr_in sockname;
|
sl@0
|
2274 |
struct sockaddr_in peername;
|
sl@0
|
2275 |
struct hostent *hostEntPtr;
|
sl@0
|
2276 |
socklen_t size = sizeof(struct sockaddr_in);
|
sl@0
|
2277 |
size_t len = 0;
|
sl@0
|
2278 |
char buf[TCL_INTEGER_SPACE];
|
sl@0
|
2279 |
|
sl@0
|
2280 |
if (optionName != (char *) NULL) {
|
sl@0
|
2281 |
len = strlen(optionName);
|
sl@0
|
2282 |
}
|
sl@0
|
2283 |
|
sl@0
|
2284 |
if ((len > 1) && (optionName[1] == 'e') &&
|
sl@0
|
2285 |
(strncmp(optionName, "-error", len) == 0)) {
|
sl@0
|
2286 |
socklen_t optlen = sizeof(int);
|
sl@0
|
2287 |
int err, ret;
|
sl@0
|
2288 |
err = 0;
|
sl@0
|
2289 |
|
sl@0
|
2290 |
|
sl@0
|
2291 |
ret = getsockopt(statePtr->fd, SOL_SOCKET, SO_ERROR,
|
sl@0
|
2292 |
(char *)&err, &optlen);
|
sl@0
|
2293 |
if (ret < 0) {
|
sl@0
|
2294 |
err = errno;
|
sl@0
|
2295 |
}
|
sl@0
|
2296 |
if (err != 0) {
|
sl@0
|
2297 |
Tcl_DStringAppend(dsPtr, Tcl_ErrnoMsg(err), -1);
|
sl@0
|
2298 |
}
|
sl@0
|
2299 |
return TCL_OK;
|
sl@0
|
2300 |
}
|
sl@0
|
2301 |
|
sl@0
|
2302 |
if ((len == 0) ||
|
sl@0
|
2303 |
((len > 1) && (optionName[1] == 'p') &&
|
sl@0
|
2304 |
(strncmp(optionName, "-peername", len) == 0))) {
|
sl@0
|
2305 |
if (getpeername(statePtr->fd, (struct sockaddr *) &peername,
|
sl@0
|
2306 |
&size) >= 0) {
|
sl@0
|
2307 |
if (len == 0) {
|
sl@0
|
2308 |
Tcl_DStringAppendElement(dsPtr, "-peername");
|
sl@0
|
2309 |
Tcl_DStringStartSublist(dsPtr);
|
sl@0
|
2310 |
}
|
sl@0
|
2311 |
Tcl_DStringAppendElement(dsPtr, inet_ntoa(peername.sin_addr));
|
sl@0
|
2312 |
hostEntPtr = TclpGetHostByAddr( /* INTL: Native. */
|
sl@0
|
2313 |
(char *) &peername.sin_addr,
|
sl@0
|
2314 |
sizeof(peername.sin_addr), AF_INET);
|
sl@0
|
2315 |
if (hostEntPtr != (struct hostent *) NULL) {
|
sl@0
|
2316 |
Tcl_DString ds;
|
sl@0
|
2317 |
|
sl@0
|
2318 |
Tcl_ExternalToUtfDString(NULL, hostEntPtr->h_name, -1, &ds);
|
sl@0
|
2319 |
Tcl_DStringAppendElement(dsPtr, Tcl_DStringValue(&ds));
|
sl@0
|
2320 |
Tcl_DStringFree(&ds);
|
sl@0
|
2321 |
} else {
|
sl@0
|
2322 |
Tcl_DStringAppendElement(dsPtr, inet_ntoa(peername.sin_addr));
|
sl@0
|
2323 |
}
|
sl@0
|
2324 |
TclFormatInt(buf, ntohs(peername.sin_port));
|
sl@0
|
2325 |
Tcl_DStringAppendElement(dsPtr, buf);
|
sl@0
|
2326 |
if (len == 0) {
|
sl@0
|
2327 |
Tcl_DStringEndSublist(dsPtr);
|
sl@0
|
2328 |
} else {
|
sl@0
|
2329 |
return TCL_OK;
|
sl@0
|
2330 |
}
|
sl@0
|
2331 |
} else {
|
sl@0
|
2332 |
/*
|
sl@0
|
2333 |
* getpeername failed - but if we were asked for all the options
|
sl@0
|
2334 |
* (len==0), don't flag an error at that point because it could
|
sl@0
|
2335 |
* be an fconfigure request on a server socket. (which have
|
sl@0
|
2336 |
* no peer). same must be done on win&mac.
|
sl@0
|
2337 |
*/
|
sl@0
|
2338 |
|
sl@0
|
2339 |
if (len) {
|
sl@0
|
2340 |
if (interp) {
|
sl@0
|
2341 |
Tcl_AppendResult(interp, "can't get peername: ",
|
sl@0
|
2342 |
Tcl_PosixError(interp), (char *) NULL);
|
sl@0
|
2343 |
}
|
sl@0
|
2344 |
return TCL_ERROR;
|
sl@0
|
2345 |
}
|
sl@0
|
2346 |
}
|
sl@0
|
2347 |
}
|
sl@0
|
2348 |
|
sl@0
|
2349 |
if ((len == 0) ||
|
sl@0
|
2350 |
((len > 1) && (optionName[1] == 's') &&
|
sl@0
|
2351 |
(strncmp(optionName, "-sockname", len) == 0))) {
|
sl@0
|
2352 |
if (getsockname(statePtr->fd, (struct sockaddr *) &sockname,
|
sl@0
|
2353 |
&size) >= 0) {
|
sl@0
|
2354 |
if (len == 0) {
|
sl@0
|
2355 |
Tcl_DStringAppendElement(dsPtr, "-sockname");
|
sl@0
|
2356 |
Tcl_DStringStartSublist(dsPtr);
|
sl@0
|
2357 |
}
|
sl@0
|
2358 |
Tcl_DStringAppendElement(dsPtr, inet_ntoa(sockname.sin_addr));
|
sl@0
|
2359 |
hostEntPtr = TclpGetHostByAddr( /* INTL: Native. */
|
sl@0
|
2360 |
(char *) &sockname.sin_addr,
|
sl@0
|
2361 |
sizeof(sockname.sin_addr), AF_INET);
|
sl@0
|
2362 |
if (hostEntPtr != (struct hostent *) NULL) {
|
sl@0
|
2363 |
Tcl_DString ds;
|
sl@0
|
2364 |
|
sl@0
|
2365 |
Tcl_ExternalToUtfDString(NULL, hostEntPtr->h_name, -1, &ds);
|
sl@0
|
2366 |
Tcl_DStringAppendElement(dsPtr, Tcl_DStringValue(&ds));
|
sl@0
|
2367 |
Tcl_DStringFree(&ds);
|
sl@0
|
2368 |
} else {
|
sl@0
|
2369 |
Tcl_DStringAppendElement(dsPtr, inet_ntoa(sockname.sin_addr));
|
sl@0
|
2370 |
}
|
sl@0
|
2371 |
TclFormatInt(buf, ntohs(sockname.sin_port));
|
sl@0
|
2372 |
Tcl_DStringAppendElement(dsPtr, buf);
|
sl@0
|
2373 |
if (len == 0) {
|
sl@0
|
2374 |
Tcl_DStringEndSublist(dsPtr);
|
sl@0
|
2375 |
} else {
|
sl@0
|
2376 |
return TCL_OK;
|
sl@0
|
2377 |
}
|
sl@0
|
2378 |
} else {
|
sl@0
|
2379 |
if (interp) {
|
sl@0
|
2380 |
Tcl_AppendResult(interp, "can't get sockname: ",
|
sl@0
|
2381 |
Tcl_PosixError(interp), (char *) NULL);
|
sl@0
|
2382 |
}
|
sl@0
|
2383 |
return TCL_ERROR;
|
sl@0
|
2384 |
}
|
sl@0
|
2385 |
}
|
sl@0
|
2386 |
|
sl@0
|
2387 |
if (len > 0) {
|
sl@0
|
2388 |
return Tcl_BadChannelOption(interp, optionName, "peername sockname");
|
sl@0
|
2389 |
}
|
sl@0
|
2390 |
|
sl@0
|
2391 |
return TCL_OK;
|
sl@0
|
2392 |
}
|
sl@0
|
2393 |
|
sl@0
|
2394 |
/*
|
sl@0
|
2395 |
*----------------------------------------------------------------------
|
sl@0
|
2396 |
*
|
sl@0
|
2397 |
* TcpWatchProc --
|
sl@0
|
2398 |
*
|
sl@0
|
2399 |
* Initialize the notifier to watch the fd from this channel.
|
sl@0
|
2400 |
*
|
sl@0
|
2401 |
* Results:
|
sl@0
|
2402 |
* None.
|
sl@0
|
2403 |
*
|
sl@0
|
2404 |
* Side effects:
|
sl@0
|
2405 |
* Sets up the notifier so that a future event on the channel will
|
sl@0
|
2406 |
* be seen by Tcl.
|
sl@0
|
2407 |
*
|
sl@0
|
2408 |
*----------------------------------------------------------------------
|
sl@0
|
2409 |
*/
|
sl@0
|
2410 |
|
sl@0
|
2411 |
static void
|
sl@0
|
2412 |
TcpWatchProc(instanceData, mask)
|
sl@0
|
2413 |
ClientData instanceData; /* The socket state. */
|
sl@0
|
2414 |
int mask; /* Events of interest; an OR-ed
|
sl@0
|
2415 |
* combination of TCL_READABLE,
|
sl@0
|
2416 |
* TCL_WRITABLE and TCL_EXCEPTION. */
|
sl@0
|
2417 |
{
|
sl@0
|
2418 |
TcpState *statePtr = (TcpState *) instanceData;
|
sl@0
|
2419 |
|
sl@0
|
2420 |
/*
|
sl@0
|
2421 |
* Make sure we don't mess with server sockets since they will never
|
sl@0
|
2422 |
* be readable or writable at the Tcl level. This keeps Tcl scripts
|
sl@0
|
2423 |
* from interfering with the -accept behavior.
|
sl@0
|
2424 |
*/
|
sl@0
|
2425 |
|
sl@0
|
2426 |
if (!statePtr->acceptProc) {
|
sl@0
|
2427 |
if (mask) {
|
sl@0
|
2428 |
Tcl_CreateFileHandler(statePtr->fd, mask,
|
sl@0
|
2429 |
(Tcl_FileProc *) Tcl_NotifyChannel,
|
sl@0
|
2430 |
(ClientData) statePtr->channel);
|
sl@0
|
2431 |
} else {
|
sl@0
|
2432 |
Tcl_DeleteFileHandler(statePtr->fd);
|
sl@0
|
2433 |
}
|
sl@0
|
2434 |
}
|
sl@0
|
2435 |
}
|
sl@0
|
2436 |
|
sl@0
|
2437 |
/*
|
sl@0
|
2438 |
*----------------------------------------------------------------------
|
sl@0
|
2439 |
*
|
sl@0
|
2440 |
* TcpGetHandleProc --
|
sl@0
|
2441 |
*
|
sl@0
|
2442 |
* Called from Tcl_GetChannelHandle to retrieve OS handles from inside
|
sl@0
|
2443 |
* a TCP socket based channel.
|
sl@0
|
2444 |
*
|
sl@0
|
2445 |
* Results:
|
sl@0
|
2446 |
* Returns TCL_OK with the fd in handlePtr, or TCL_ERROR if
|
sl@0
|
2447 |
* there is no handle for the specified direction.
|
sl@0
|
2448 |
*
|
sl@0
|
2449 |
* Side effects:
|
sl@0
|
2450 |
* None.
|
sl@0
|
2451 |
*
|
sl@0
|
2452 |
*----------------------------------------------------------------------
|
sl@0
|
2453 |
*/
|
sl@0
|
2454 |
|
sl@0
|
2455 |
/* ARGSUSED */
|
sl@0
|
2456 |
static int
|
sl@0
|
2457 |
TcpGetHandleProc(instanceData, direction, handlePtr)
|
sl@0
|
2458 |
ClientData instanceData; /* The socket state. */
|
sl@0
|
2459 |
int direction; /* Not used. */
|
sl@0
|
2460 |
ClientData *handlePtr; /* Where to store the handle. */
|
sl@0
|
2461 |
{
|
sl@0
|
2462 |
TcpState *statePtr = (TcpState *) instanceData;
|
sl@0
|
2463 |
|
sl@0
|
2464 |
*handlePtr = (ClientData)statePtr->fd;
|
sl@0
|
2465 |
return TCL_OK;
|
sl@0
|
2466 |
}
|
sl@0
|
2467 |
|
sl@0
|
2468 |
/*
|
sl@0
|
2469 |
*----------------------------------------------------------------------
|
sl@0
|
2470 |
*
|
sl@0
|
2471 |
* CreateSocket --
|
sl@0
|
2472 |
*
|
sl@0
|
2473 |
* This function opens a new socket in client or server mode
|
sl@0
|
2474 |
* and initializes the TcpState structure.
|
sl@0
|
2475 |
*
|
sl@0
|
2476 |
* Results:
|
sl@0
|
2477 |
* Returns a new TcpState, or NULL with an error in the interp's
|
sl@0
|
2478 |
* result, if interp is not NULL.
|
sl@0
|
2479 |
*
|
sl@0
|
2480 |
* Side effects:
|
sl@0
|
2481 |
* Opens a socket.
|
sl@0
|
2482 |
*
|
sl@0
|
2483 |
*----------------------------------------------------------------------
|
sl@0
|
2484 |
*/
|
sl@0
|
2485 |
|
sl@0
|
2486 |
static TcpState *
|
sl@0
|
2487 |
CreateSocket(interp, port, host, server, myaddr, myport, async)
|
sl@0
|
2488 |
Tcl_Interp *interp; /* For error reporting; can be NULL. */
|
sl@0
|
2489 |
int port; /* Port number to open. */
|
sl@0
|
2490 |
CONST char *host; /* Name of host on which to open port.
|
sl@0
|
2491 |
* NULL implies INADDR_ANY */
|
sl@0
|
2492 |
int server; /* 1 if socket should be a server socket,
|
sl@0
|
2493 |
* else 0 for a client socket. */
|
sl@0
|
2494 |
CONST char *myaddr; /* Optional client-side address */
|
sl@0
|
2495 |
int myport; /* Optional client-side port */
|
sl@0
|
2496 |
int async; /* If nonzero and creating a client socket,
|
sl@0
|
2497 |
* attempt to do an async connect. Otherwise
|
sl@0
|
2498 |
* do a synchronous connect or bind. */
|
sl@0
|
2499 |
{
|
sl@0
|
2500 |
int status, sock, asyncConnect, curState, origState;
|
sl@0
|
2501 |
struct sockaddr_in sockaddr; /* socket address */
|
sl@0
|
2502 |
struct sockaddr_in mysockaddr; /* Socket address for client */
|
sl@0
|
2503 |
TcpState *statePtr;
|
sl@0
|
2504 |
|
sl@0
|
2505 |
sock = -1;
|
sl@0
|
2506 |
origState = 0;
|
sl@0
|
2507 |
if (! CreateSocketAddress(&sockaddr, host, port)) {
|
sl@0
|
2508 |
goto addressError;
|
sl@0
|
2509 |
}
|
sl@0
|
2510 |
if ((myaddr != NULL || myport != 0) &&
|
sl@0
|
2511 |
! CreateSocketAddress(&mysockaddr, myaddr, myport)) {
|
sl@0
|
2512 |
goto addressError;
|
sl@0
|
2513 |
}
|
sl@0
|
2514 |
|
sl@0
|
2515 |
sock = socket(AF_INET, SOCK_STREAM, 0);
|
sl@0
|
2516 |
if (sock < 0) {
|
sl@0
|
2517 |
goto addressError;
|
sl@0
|
2518 |
}
|
sl@0
|
2519 |
|
sl@0
|
2520 |
/*
|
sl@0
|
2521 |
* Set the close-on-exec flag so that the socket will not get
|
sl@0
|
2522 |
* inherited by child processes.
|
sl@0
|
2523 |
*/
|
sl@0
|
2524 |
|
sl@0
|
2525 |
fcntl(sock, F_SETFD, FD_CLOEXEC);
|
sl@0
|
2526 |
|
sl@0
|
2527 |
/*
|
sl@0
|
2528 |
* Set kernel space buffering
|
sl@0
|
2529 |
*/
|
sl@0
|
2530 |
|
sl@0
|
2531 |
TclSockMinimumBuffers(sock, SOCKET_BUFSIZE);
|
sl@0
|
2532 |
|
sl@0
|
2533 |
asyncConnect = 0;
|
sl@0
|
2534 |
status = 0;
|
sl@0
|
2535 |
if (server) {
|
sl@0
|
2536 |
/*
|
sl@0
|
2537 |
* Set up to reuse server addresses automatically and bind to the
|
sl@0
|
2538 |
* specified port.
|
sl@0
|
2539 |
*/
|
sl@0
|
2540 |
|
sl@0
|
2541 |
status = 1;
|
sl@0
|
2542 |
(void) setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &status,
|
sl@0
|
2543 |
sizeof(status));
|
sl@0
|
2544 |
status = bind(sock, (struct sockaddr *) &sockaddr,
|
sl@0
|
2545 |
sizeof(struct sockaddr));
|
sl@0
|
2546 |
if (status != -1) {
|
sl@0
|
2547 |
status = listen(sock, SOMAXCONN);
|
sl@0
|
2548 |
}
|
sl@0
|
2549 |
} else {
|
sl@0
|
2550 |
if (myaddr != NULL || myport != 0) {
|
sl@0
|
2551 |
curState = 1;
|
sl@0
|
2552 |
(void) setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
|
sl@0
|
2553 |
(char *) &curState, sizeof(curState));
|
sl@0
|
2554 |
status = bind(sock, (struct sockaddr *) &mysockaddr,
|
sl@0
|
2555 |
sizeof(struct sockaddr));
|
sl@0
|
2556 |
if (status < 0) {
|
sl@0
|
2557 |
goto bindError;
|
sl@0
|
2558 |
}
|
sl@0
|
2559 |
}
|
sl@0
|
2560 |
|
sl@0
|
2561 |
/*
|
sl@0
|
2562 |
* Attempt to connect. The connect may fail at present with an
|
sl@0
|
2563 |
* EINPROGRESS but at a later time it will complete. The caller
|
sl@0
|
2564 |
* will set up a file handler on the socket if she is interested in
|
sl@0
|
2565 |
* being informed when the connect completes.
|
sl@0
|
2566 |
*/
|
sl@0
|
2567 |
|
sl@0
|
2568 |
if (async) {
|
sl@0
|
2569 |
#ifndef USE_FIONBIO
|
sl@0
|
2570 |
origState = fcntl(sock, F_GETFL);
|
sl@0
|
2571 |
curState = origState | O_NONBLOCK;
|
sl@0
|
2572 |
status = fcntl(sock, F_SETFL, curState);
|
sl@0
|
2573 |
#else /* USE_FIONBIO */
|
sl@0
|
2574 |
curState = 1;
|
sl@0
|
2575 |
status = ioctl(sock, FIONBIO, &curState);
|
sl@0
|
2576 |
#endif /* !USE_FIONBIO */
|
sl@0
|
2577 |
} else {
|
sl@0
|
2578 |
status = 0;
|
sl@0
|
2579 |
}
|
sl@0
|
2580 |
if (status > -1) {
|
sl@0
|
2581 |
status = connect(sock, (struct sockaddr *) &sockaddr,
|
sl@0
|
2582 |
sizeof(sockaddr));
|
sl@0
|
2583 |
if (status < 0) {
|
sl@0
|
2584 |
if (errno == EINPROGRESS) {
|
sl@0
|
2585 |
asyncConnect = 1;
|
sl@0
|
2586 |
status = 0;
|
sl@0
|
2587 |
}
|
sl@0
|
2588 |
} else {
|
sl@0
|
2589 |
/*
|
sl@0
|
2590 |
* Here we are if the connect succeeds. In case of an
|
sl@0
|
2591 |
* asynchronous connect we have to reset the channel to
|
sl@0
|
2592 |
* blocking mode. This appears to happen not very often,
|
sl@0
|
2593 |
* but e.g. on a HP 9000/800 under HP-UX B.11.00 we enter
|
sl@0
|
2594 |
* this stage. [Bug: 4388]
|
sl@0
|
2595 |
*/
|
sl@0
|
2596 |
if (async) {
|
sl@0
|
2597 |
#ifndef USE_FIONBIO
|
sl@0
|
2598 |
origState = fcntl(sock, F_GETFL);
|
sl@0
|
2599 |
curState = origState & ~(O_NONBLOCK);
|
sl@0
|
2600 |
status = fcntl(sock, F_SETFL, curState);
|
sl@0
|
2601 |
#else /* USE_FIONBIO */
|
sl@0
|
2602 |
curState = 0;
|
sl@0
|
2603 |
status = ioctl(sock, FIONBIO, &curState);
|
sl@0
|
2604 |
#endif /* !USE_FIONBIO */
|
sl@0
|
2605 |
}
|
sl@0
|
2606 |
}
|
sl@0
|
2607 |
}
|
sl@0
|
2608 |
}
|
sl@0
|
2609 |
|
sl@0
|
2610 |
bindError:
|
sl@0
|
2611 |
if (status < 0) {
|
sl@0
|
2612 |
if (interp != NULL) {
|
sl@0
|
2613 |
Tcl_AppendResult(interp, "couldn't open socket: ",
|
sl@0
|
2614 |
Tcl_PosixError(interp), (char *) NULL);
|
sl@0
|
2615 |
}
|
sl@0
|
2616 |
if (sock != -1) {
|
sl@0
|
2617 |
close(sock);
|
sl@0
|
2618 |
}
|
sl@0
|
2619 |
return NULL;
|
sl@0
|
2620 |
}
|
sl@0
|
2621 |
|
sl@0
|
2622 |
/*
|
sl@0
|
2623 |
* Allocate a new TcpState for this socket.
|
sl@0
|
2624 |
*/
|
sl@0
|
2625 |
|
sl@0
|
2626 |
statePtr = (TcpState *) ckalloc((unsigned) sizeof(TcpState));
|
sl@0
|
2627 |
statePtr->flags = 0;
|
sl@0
|
2628 |
if (asyncConnect) {
|
sl@0
|
2629 |
statePtr->flags = TCP_ASYNC_CONNECT;
|
sl@0
|
2630 |
}
|
sl@0
|
2631 |
statePtr->fd = sock;
|
sl@0
|
2632 |
|
sl@0
|
2633 |
return statePtr;
|
sl@0
|
2634 |
|
sl@0
|
2635 |
addressError:
|
sl@0
|
2636 |
if (sock != -1) {
|
sl@0
|
2637 |
close(sock);
|
sl@0
|
2638 |
}
|
sl@0
|
2639 |
if (interp != NULL) {
|
sl@0
|
2640 |
Tcl_AppendResult(interp, "couldn't open socket: ",
|
sl@0
|
2641 |
Tcl_PosixError(interp), (char *) NULL);
|
sl@0
|
2642 |
}
|
sl@0
|
2643 |
return NULL;
|
sl@0
|
2644 |
}
|
sl@0
|
2645 |
|
sl@0
|
2646 |
/*
|
sl@0
|
2647 |
*----------------------------------------------------------------------
|
sl@0
|
2648 |
*
|
sl@0
|
2649 |
* CreateSocketAddress --
|
sl@0
|
2650 |
*
|
sl@0
|
2651 |
* This function initializes a sockaddr structure for a host and port.
|
sl@0
|
2652 |
*
|
sl@0
|
2653 |
* Results:
|
sl@0
|
2654 |
* 1 if the host was valid, 0 if the host could not be converted to
|
sl@0
|
2655 |
* an IP address.
|
sl@0
|
2656 |
*
|
sl@0
|
2657 |
* Side effects:
|
sl@0
|
2658 |
* Fills in the *sockaddrPtr structure.
|
sl@0
|
2659 |
*
|
sl@0
|
2660 |
*----------------------------------------------------------------------
|
sl@0
|
2661 |
*/
|
sl@0
|
2662 |
|
sl@0
|
2663 |
static int
|
sl@0
|
2664 |
CreateSocketAddress(sockaddrPtr, host, port)
|
sl@0
|
2665 |
struct sockaddr_in *sockaddrPtr; /* Socket address */
|
sl@0
|
2666 |
CONST char *host; /* Host. NULL implies INADDR_ANY */
|
sl@0
|
2667 |
int port; /* Port number */
|
sl@0
|
2668 |
{
|
sl@0
|
2669 |
struct hostent *hostent; /* Host database entry */
|
sl@0
|
2670 |
struct in_addr addr; /* For 64/32 bit madness */
|
sl@0
|
2671 |
|
sl@0
|
2672 |
#ifdef __SYMBIAN32__
|
sl@0
|
2673 |
if (host && !strcmp(host, "localhost")) {
|
sl@0
|
2674 |
char* loc = strstr(host, "localhost");
|
sl@0
|
2675 |
memcpy(loc, "127.0.0.1", 9);
|
sl@0
|
2676 |
}
|
sl@0
|
2677 |
#endif
|
sl@0
|
2678 |
(void) memset((VOID *) sockaddrPtr, '\0', sizeof(struct sockaddr_in));
|
sl@0
|
2679 |
sockaddrPtr->sin_family = AF_INET;
|
sl@0
|
2680 |
sockaddrPtr->sin_port = htons((unsigned short) (port & 0xFFFF));
|
sl@0
|
2681 |
if (host == NULL) {
|
sl@0
|
2682 |
addr.s_addr = INADDR_ANY;
|
sl@0
|
2683 |
} else {
|
sl@0
|
2684 |
Tcl_DString ds;
|
sl@0
|
2685 |
CONST char *native;
|
sl@0
|
2686 |
|
sl@0
|
2687 |
if (host == NULL) {
|
sl@0
|
2688 |
native = NULL;
|
sl@0
|
2689 |
} else {
|
sl@0
|
2690 |
native = Tcl_UtfToExternalDString(NULL, host, -1, &ds);
|
sl@0
|
2691 |
}
|
sl@0
|
2692 |
addr.s_addr = inet_addr(native); /* INTL: Native. */
|
sl@0
|
2693 |
/*
|
sl@0
|
2694 |
* This is 0xFFFFFFFF to ensure that it compares as a 32bit -1
|
sl@0
|
2695 |
* on either 32 or 64 bits systems.
|
sl@0
|
2696 |
*/
|
sl@0
|
2697 |
if (addr.s_addr == 0xFFFFFFFF) {
|
sl@0
|
2698 |
hostent = TclpGetHostByName(native); /* INTL: Native. */
|
sl@0
|
2699 |
if (hostent != (struct hostent *) NULL) {
|
sl@0
|
2700 |
memcpy((VOID *) &addr,
|
sl@0
|
2701 |
(VOID *) hostent->h_addr_list[0],
|
sl@0
|
2702 |
(size_t) hostent->h_length);
|
sl@0
|
2703 |
} else {
|
sl@0
|
2704 |
#ifdef EHOSTUNREACH
|
sl@0
|
2705 |
errno = EHOSTUNREACH;
|
sl@0
|
2706 |
#else /* !EHOSTUNREACH */
|
sl@0
|
2707 |
#ifdef ENXIO
|
sl@0
|
2708 |
errno = ENXIO;
|
sl@0
|
2709 |
#endif /* ENXIO */
|
sl@0
|
2710 |
#endif /* EHOSTUNREACH */
|
sl@0
|
2711 |
if (native != NULL) {
|
sl@0
|
2712 |
Tcl_DStringFree(&ds);
|
sl@0
|
2713 |
}
|
sl@0
|
2714 |
return 0; /* error */
|
sl@0
|
2715 |
}
|
sl@0
|
2716 |
}
|
sl@0
|
2717 |
if (native != NULL) {
|
sl@0
|
2718 |
Tcl_DStringFree(&ds);
|
sl@0
|
2719 |
}
|
sl@0
|
2720 |
}
|
sl@0
|
2721 |
|
sl@0
|
2722 |
/*
|
sl@0
|
2723 |
* NOTE: On 64 bit machines the assignment below is rumored to not
|
sl@0
|
2724 |
* do the right thing. Please report errors related to this if you
|
sl@0
|
2725 |
* observe incorrect behavior on 64 bit machines such as DEC Alphas.
|
sl@0
|
2726 |
* Should we modify this code to do an explicit memcpy?
|
sl@0
|
2727 |
*/
|
sl@0
|
2728 |
|
sl@0
|
2729 |
sockaddrPtr->sin_addr.s_addr = addr.s_addr;
|
sl@0
|
2730 |
return 1; /* Success. */
|
sl@0
|
2731 |
}
|
sl@0
|
2732 |
|
sl@0
|
2733 |
/*
|
sl@0
|
2734 |
*----------------------------------------------------------------------
|
sl@0
|
2735 |
*
|
sl@0
|
2736 |
* Tcl_OpenTcpClient --
|
sl@0
|
2737 |
*
|
sl@0
|
2738 |
* Opens a TCP client socket and creates a channel around it.
|
sl@0
|
2739 |
*
|
sl@0
|
2740 |
* Results:
|
sl@0
|
2741 |
* The channel or NULL if failed. An error message is returned
|
sl@0
|
2742 |
* in the interpreter on failure.
|
sl@0
|
2743 |
*
|
sl@0
|
2744 |
* Side effects:
|
sl@0
|
2745 |
* Opens a client socket and creates a new channel.
|
sl@0
|
2746 |
*
|
sl@0
|
2747 |
*----------------------------------------------------------------------
|
sl@0
|
2748 |
*/
|
sl@0
|
2749 |
|
sl@0
|
2750 |
EXPORT_C Tcl_Channel
|
sl@0
|
2751 |
Tcl_OpenTcpClient(interp, port, host, myaddr, myport, async)
|
sl@0
|
2752 |
Tcl_Interp *interp; /* For error reporting; can be NULL. */
|
sl@0
|
2753 |
int port; /* Port number to open. */
|
sl@0
|
2754 |
CONST char *host; /* Host on which to open port. */
|
sl@0
|
2755 |
CONST char *myaddr; /* Client-side address */
|
sl@0
|
2756 |
int myport; /* Client-side port */
|
sl@0
|
2757 |
int async; /* If nonzero, attempt to do an
|
sl@0
|
2758 |
* asynchronous connect. Otherwise
|
sl@0
|
2759 |
* we do a blocking connect. */
|
sl@0
|
2760 |
{
|
sl@0
|
2761 |
TcpState *statePtr;
|
sl@0
|
2762 |
char channelName[16 + TCL_INTEGER_SPACE];
|
sl@0
|
2763 |
|
sl@0
|
2764 |
/*
|
sl@0
|
2765 |
* Create a new client socket and wrap it in a channel.
|
sl@0
|
2766 |
*/
|
sl@0
|
2767 |
|
sl@0
|
2768 |
statePtr = CreateSocket(interp, port, host, 0, myaddr, myport, async);
|
sl@0
|
2769 |
if (statePtr == NULL) {
|
sl@0
|
2770 |
return NULL;
|
sl@0
|
2771 |
}
|
sl@0
|
2772 |
|
sl@0
|
2773 |
statePtr->acceptProc = NULL;
|
sl@0
|
2774 |
statePtr->acceptProcData = (ClientData) NULL;
|
sl@0
|
2775 |
|
sl@0
|
2776 |
sprintf(channelName, "sock%d", statePtr->fd);
|
sl@0
|
2777 |
|
sl@0
|
2778 |
statePtr->channel = Tcl_CreateChannel(&tcpChannelType, channelName,
|
sl@0
|
2779 |
(ClientData) statePtr, (TCL_READABLE | TCL_WRITABLE));
|
sl@0
|
2780 |
if (Tcl_SetChannelOption(interp, statePtr->channel, "-translation",
|
sl@0
|
2781 |
"auto crlf") == TCL_ERROR) {
|
sl@0
|
2782 |
Tcl_Close((Tcl_Interp *) NULL, statePtr->channel);
|
sl@0
|
2783 |
return NULL;
|
sl@0
|
2784 |
}
|
sl@0
|
2785 |
return statePtr->channel;
|
sl@0
|
2786 |
}
|
sl@0
|
2787 |
|
sl@0
|
2788 |
/*
|
sl@0
|
2789 |
*----------------------------------------------------------------------
|
sl@0
|
2790 |
*
|
sl@0
|
2791 |
* Tcl_MakeTcpClientChannel --
|
sl@0
|
2792 |
*
|
sl@0
|
2793 |
* Creates a Tcl_Channel from an existing client TCP socket.
|
sl@0
|
2794 |
*
|
sl@0
|
2795 |
* Results:
|
sl@0
|
2796 |
* The Tcl_Channel wrapped around the preexisting TCP socket.
|
sl@0
|
2797 |
*
|
sl@0
|
2798 |
* Side effects:
|
sl@0
|
2799 |
* None.
|
sl@0
|
2800 |
*
|
sl@0
|
2801 |
*----------------------------------------------------------------------
|
sl@0
|
2802 |
*/
|
sl@0
|
2803 |
|
sl@0
|
2804 |
EXPORT_C Tcl_Channel
|
sl@0
|
2805 |
Tcl_MakeTcpClientChannel(sock)
|
sl@0
|
2806 |
ClientData sock; /* The socket to wrap up into a channel. */
|
sl@0
|
2807 |
{
|
sl@0
|
2808 |
return MakeTcpClientChannelMode(sock, (TCL_READABLE | TCL_WRITABLE));
|
sl@0
|
2809 |
}
|
sl@0
|
2810 |
|
sl@0
|
2811 |
/*
|
sl@0
|
2812 |
*----------------------------------------------------------------------
|
sl@0
|
2813 |
*
|
sl@0
|
2814 |
* MakeTcpClientChannelMode --
|
sl@0
|
2815 |
*
|
sl@0
|
2816 |
* Creates a Tcl_Channel from an existing client TCP socket
|
sl@0
|
2817 |
* with given mode.
|
sl@0
|
2818 |
*
|
sl@0
|
2819 |
* Results:
|
sl@0
|
2820 |
* The Tcl_Channel wrapped around the preexisting TCP socket.
|
sl@0
|
2821 |
*
|
sl@0
|
2822 |
* Side effects:
|
sl@0
|
2823 |
* None.
|
sl@0
|
2824 |
*
|
sl@0
|
2825 |
*----------------------------------------------------------------------
|
sl@0
|
2826 |
*/
|
sl@0
|
2827 |
|
sl@0
|
2828 |
static Tcl_Channel
|
sl@0
|
2829 |
MakeTcpClientChannelMode(sock, mode)
|
sl@0
|
2830 |
ClientData sock; /* The socket to wrap up into a channel. */
|
sl@0
|
2831 |
int mode; /* ORed combination of TCL_READABLE and
|
sl@0
|
2832 |
* TCL_WRITABLE to indicate file mode. */
|
sl@0
|
2833 |
{
|
sl@0
|
2834 |
TcpState *statePtr;
|
sl@0
|
2835 |
char channelName[16 + TCL_INTEGER_SPACE];
|
sl@0
|
2836 |
|
sl@0
|
2837 |
statePtr = (TcpState *) ckalloc((unsigned) sizeof(TcpState));
|
sl@0
|
2838 |
statePtr->fd = (int) sock;
|
sl@0
|
2839 |
statePtr->flags = 0;
|
sl@0
|
2840 |
statePtr->acceptProc = NULL;
|
sl@0
|
2841 |
statePtr->acceptProcData = (ClientData) NULL;
|
sl@0
|
2842 |
|
sl@0
|
2843 |
sprintf(channelName, "sock%d", statePtr->fd);
|
sl@0
|
2844 |
|
sl@0
|
2845 |
statePtr->channel = Tcl_CreateChannel(&tcpChannelType, channelName,
|
sl@0
|
2846 |
(ClientData) statePtr, mode);
|
sl@0
|
2847 |
if (Tcl_SetChannelOption((Tcl_Interp *) NULL, statePtr->channel,
|
sl@0
|
2848 |
"-translation", "auto crlf") == TCL_ERROR) {
|
sl@0
|
2849 |
Tcl_Close((Tcl_Interp *) NULL, statePtr->channel);
|
sl@0
|
2850 |
return NULL;
|
sl@0
|
2851 |
}
|
sl@0
|
2852 |
return statePtr->channel;
|
sl@0
|
2853 |
}
|
sl@0
|
2854 |
|
sl@0
|
2855 |
/*
|
sl@0
|
2856 |
*----------------------------------------------------------------------
|
sl@0
|
2857 |
*
|
sl@0
|
2858 |
* Tcl_OpenTcpServer --
|
sl@0
|
2859 |
*
|
sl@0
|
2860 |
* Opens a TCP server socket and creates a channel around it.
|
sl@0
|
2861 |
*
|
sl@0
|
2862 |
* Results:
|
sl@0
|
2863 |
* The channel or NULL if failed. If an error occurred, an
|
sl@0
|
2864 |
* error message is left in the interp's result if interp is
|
sl@0
|
2865 |
* not NULL.
|
sl@0
|
2866 |
*
|
sl@0
|
2867 |
* Side effects:
|
sl@0
|
2868 |
* Opens a server socket and creates a new channel.
|
sl@0
|
2869 |
*
|
sl@0
|
2870 |
*----------------------------------------------------------------------
|
sl@0
|
2871 |
*/
|
sl@0
|
2872 |
|
sl@0
|
2873 |
EXPORT_C Tcl_Channel
|
sl@0
|
2874 |
Tcl_OpenTcpServer(interp, port, myHost, acceptProc, acceptProcData)
|
sl@0
|
2875 |
Tcl_Interp *interp; /* For error reporting - may be
|
sl@0
|
2876 |
* NULL. */
|
sl@0
|
2877 |
int port; /* Port number to open. */
|
sl@0
|
2878 |
CONST char *myHost; /* Name of local host. */
|
sl@0
|
2879 |
Tcl_TcpAcceptProc *acceptProc; /* Callback for accepting connections
|
sl@0
|
2880 |
* from new clients. */
|
sl@0
|
2881 |
ClientData acceptProcData; /* Data for the callback. */
|
sl@0
|
2882 |
{
|
sl@0
|
2883 |
TcpState *statePtr;
|
sl@0
|
2884 |
char channelName[16 + TCL_INTEGER_SPACE];
|
sl@0
|
2885 |
|
sl@0
|
2886 |
/*
|
sl@0
|
2887 |
* Create a new client socket and wrap it in a channel.
|
sl@0
|
2888 |
*/
|
sl@0
|
2889 |
|
sl@0
|
2890 |
statePtr = CreateSocket(interp, port, myHost, 1, NULL, 0, 0);
|
sl@0
|
2891 |
if (statePtr == NULL) {
|
sl@0
|
2892 |
return NULL;
|
sl@0
|
2893 |
}
|
sl@0
|
2894 |
|
sl@0
|
2895 |
statePtr->acceptProc = acceptProc;
|
sl@0
|
2896 |
statePtr->acceptProcData = acceptProcData;
|
sl@0
|
2897 |
|
sl@0
|
2898 |
/*
|
sl@0
|
2899 |
* Set up the callback mechanism for accepting connections
|
sl@0
|
2900 |
* from new clients.
|
sl@0
|
2901 |
*/
|
sl@0
|
2902 |
|
sl@0
|
2903 |
Tcl_CreateFileHandler(statePtr->fd, TCL_READABLE, TcpAccept,
|
sl@0
|
2904 |
(ClientData) statePtr);
|
sl@0
|
2905 |
sprintf(channelName, "sock%d", statePtr->fd);
|
sl@0
|
2906 |
statePtr->channel = Tcl_CreateChannel(&tcpChannelType, channelName,
|
sl@0
|
2907 |
(ClientData) statePtr, 0);
|
sl@0
|
2908 |
return statePtr->channel;
|
sl@0
|
2909 |
}
|
sl@0
|
2910 |
|
sl@0
|
2911 |
/*
|
sl@0
|
2912 |
*----------------------------------------------------------------------
|
sl@0
|
2913 |
*
|
sl@0
|
2914 |
* TcpAccept --
|
sl@0
|
2915 |
* Accept a TCP socket connection. This is called by the event loop.
|
sl@0
|
2916 |
*
|
sl@0
|
2917 |
* Results:
|
sl@0
|
2918 |
* None.
|
sl@0
|
2919 |
*
|
sl@0
|
2920 |
* Side effects:
|
sl@0
|
2921 |
* Creates a new connection socket. Calls the registered callback
|
sl@0
|
2922 |
* for the connection acceptance mechanism.
|
sl@0
|
2923 |
*
|
sl@0
|
2924 |
*----------------------------------------------------------------------
|
sl@0
|
2925 |
*/
|
sl@0
|
2926 |
|
sl@0
|
2927 |
/* ARGSUSED */
|
sl@0
|
2928 |
static void
|
sl@0
|
2929 |
TcpAccept(data, mask)
|
sl@0
|
2930 |
ClientData data; /* Callback token. */
|
sl@0
|
2931 |
int mask; /* Not used. */
|
sl@0
|
2932 |
{
|
sl@0
|
2933 |
TcpState *sockState; /* Client data of server socket. */
|
sl@0
|
2934 |
int newsock; /* The new client socket */
|
sl@0
|
2935 |
TcpState *newSockState; /* State for new socket. */
|
sl@0
|
2936 |
struct sockaddr_in addr; /* The remote address */
|
sl@0
|
2937 |
socklen_t len; /* For accept interface */
|
sl@0
|
2938 |
char channelName[16 + TCL_INTEGER_SPACE];
|
sl@0
|
2939 |
|
sl@0
|
2940 |
sockState = (TcpState *) data;
|
sl@0
|
2941 |
|
sl@0
|
2942 |
len = sizeof(struct sockaddr_in);
|
sl@0
|
2943 |
newsock = accept(sockState->fd, (struct sockaddr *) &addr, &len);
|
sl@0
|
2944 |
if (newsock < 0) {
|
sl@0
|
2945 |
return;
|
sl@0
|
2946 |
}
|
sl@0
|
2947 |
|
sl@0
|
2948 |
/*
|
sl@0
|
2949 |
* Set close-on-exec flag to prevent the newly accepted socket from
|
sl@0
|
2950 |
* being inherited by child processes.
|
sl@0
|
2951 |
*/
|
sl@0
|
2952 |
|
sl@0
|
2953 |
(void) fcntl(newsock, F_SETFD, FD_CLOEXEC);
|
sl@0
|
2954 |
|
sl@0
|
2955 |
newSockState = (TcpState *) ckalloc((unsigned) sizeof(TcpState));
|
sl@0
|
2956 |
|
sl@0
|
2957 |
newSockState->flags = 0;
|
sl@0
|
2958 |
newSockState->fd = newsock;
|
sl@0
|
2959 |
newSockState->acceptProc = NULL;
|
sl@0
|
2960 |
newSockState->acceptProcData = NULL;
|
sl@0
|
2961 |
|
sl@0
|
2962 |
sprintf(channelName, "sock%d", newsock);
|
sl@0
|
2963 |
newSockState->channel = Tcl_CreateChannel(&tcpChannelType, channelName,
|
sl@0
|
2964 |
(ClientData) newSockState, (TCL_READABLE | TCL_WRITABLE));
|
sl@0
|
2965 |
|
sl@0
|
2966 |
Tcl_SetChannelOption(NULL, newSockState->channel, "-translation",
|
sl@0
|
2967 |
"auto crlf");
|
sl@0
|
2968 |
|
sl@0
|
2969 |
if (sockState->acceptProc != NULL) {
|
sl@0
|
2970 |
(*sockState->acceptProc)(sockState->acceptProcData,
|
sl@0
|
2971 |
newSockState->channel, inet_ntoa(addr.sin_addr),
|
sl@0
|
2972 |
ntohs(addr.sin_port));
|
sl@0
|
2973 |
}
|
sl@0
|
2974 |
}
|
sl@0
|
2975 |
|
sl@0
|
2976 |
/*
|
sl@0
|
2977 |
*----------------------------------------------------------------------
|
sl@0
|
2978 |
*
|
sl@0
|
2979 |
* TclpGetDefaultStdChannel --
|
sl@0
|
2980 |
*
|
sl@0
|
2981 |
* Creates channels for standard input, standard output or standard
|
sl@0
|
2982 |
* error output if they do not already exist.
|
sl@0
|
2983 |
*
|
sl@0
|
2984 |
* Results:
|
sl@0
|
2985 |
* Returns the specified default standard channel, or NULL.
|
sl@0
|
2986 |
*
|
sl@0
|
2987 |
* Side effects:
|
sl@0
|
2988 |
* May cause the creation of a standard channel and the underlying
|
sl@0
|
2989 |
* file.
|
sl@0
|
2990 |
*
|
sl@0
|
2991 |
*----------------------------------------------------------------------
|
sl@0
|
2992 |
*/
|
sl@0
|
2993 |
|
sl@0
|
2994 |
Tcl_Channel
|
sl@0
|
2995 |
TclpGetDefaultStdChannel(type)
|
sl@0
|
2996 |
int type; /* One of TCL_STDIN, TCL_STDOUT, TCL_STDERR. */
|
sl@0
|
2997 |
{
|
sl@0
|
2998 |
Tcl_Channel channel = NULL;
|
sl@0
|
2999 |
int fd = 0; /* Initializations needed to prevent */
|
sl@0
|
3000 |
int mode = 0; /* compiler warning (used before set). */
|
sl@0
|
3001 |
char *bufMode = NULL;
|
sl@0
|
3002 |
|
sl@0
|
3003 |
/*
|
sl@0
|
3004 |
* Some #def's to make the code a little clearer!
|
sl@0
|
3005 |
*/
|
sl@0
|
3006 |
#define ZERO_OFFSET ((Tcl_SeekOffset) 0)
|
sl@0
|
3007 |
#define ERROR_OFFSET ((Tcl_SeekOffset) -1)
|
sl@0
|
3008 |
|
sl@0
|
3009 |
switch (type) {
|
sl@0
|
3010 |
case TCL_STDIN:
|
sl@0
|
3011 |
if ((TclOSseek(0, ZERO_OFFSET, SEEK_CUR) == ERROR_OFFSET)
|
sl@0
|
3012 |
&& (errno == EBADF)) {
|
sl@0
|
3013 |
return (Tcl_Channel) NULL;
|
sl@0
|
3014 |
}
|
sl@0
|
3015 |
fd = 0;
|
sl@0
|
3016 |
mode = TCL_READABLE;
|
sl@0
|
3017 |
bufMode = "line";
|
sl@0
|
3018 |
break;
|
sl@0
|
3019 |
case TCL_STDOUT:
|
sl@0
|
3020 |
if ((TclOSseek(1, ZERO_OFFSET, SEEK_CUR) == ERROR_OFFSET)
|
sl@0
|
3021 |
&& (errno == EBADF)) {
|
sl@0
|
3022 |
return (Tcl_Channel) NULL;
|
sl@0
|
3023 |
}
|
sl@0
|
3024 |
fd = 1;
|
sl@0
|
3025 |
mode = TCL_WRITABLE;
|
sl@0
|
3026 |
bufMode = "line";
|
sl@0
|
3027 |
break;
|
sl@0
|
3028 |
case TCL_STDERR:
|
sl@0
|
3029 |
if ((TclOSseek(2, ZERO_OFFSET, SEEK_CUR) == ERROR_OFFSET)
|
sl@0
|
3030 |
&& (errno == EBADF)) {
|
sl@0
|
3031 |
return (Tcl_Channel) NULL;
|
sl@0
|
3032 |
}
|
sl@0
|
3033 |
fd = 2;
|
sl@0
|
3034 |
mode = TCL_WRITABLE;
|
sl@0
|
3035 |
bufMode = "none";
|
sl@0
|
3036 |
break;
|
sl@0
|
3037 |
default:
|
sl@0
|
3038 |
panic("TclGetDefaultStdChannel: Unexpected channel type");
|
sl@0
|
3039 |
break;
|
sl@0
|
3040 |
}
|
sl@0
|
3041 |
|
sl@0
|
3042 |
#undef ZERO_OFFSET
|
sl@0
|
3043 |
#undef ERROR_OFFSET
|
sl@0
|
3044 |
|
sl@0
|
3045 |
channel = Tcl_MakeFileChannel((ClientData) fd, mode);
|
sl@0
|
3046 |
if (channel == NULL) {
|
sl@0
|
3047 |
return NULL;
|
sl@0
|
3048 |
}
|
sl@0
|
3049 |
|
sl@0
|
3050 |
/*
|
sl@0
|
3051 |
* Set up the normal channel options for stdio handles.
|
sl@0
|
3052 |
*/
|
sl@0
|
3053 |
|
sl@0
|
3054 |
if (Tcl_GetChannelType(channel) == &fileChannelType) {
|
sl@0
|
3055 |
Tcl_SetChannelOption(NULL, channel, "-translation", "auto");
|
sl@0
|
3056 |
} else {
|
sl@0
|
3057 |
Tcl_SetChannelOption(NULL, channel, "-translation", "auto crlf");
|
sl@0
|
3058 |
}
|
sl@0
|
3059 |
Tcl_SetChannelOption(NULL, channel, "-buffering", bufMode);
|
sl@0
|
3060 |
return channel;
|
sl@0
|
3061 |
}
|
sl@0
|
3062 |
|
sl@0
|
3063 |
/*
|
sl@0
|
3064 |
*----------------------------------------------------------------------
|
sl@0
|
3065 |
*
|
sl@0
|
3066 |
* Tcl_GetOpenFile --
|
sl@0
|
3067 |
*
|
sl@0
|
3068 |
* Given a name of a channel registered in the given interpreter,
|
sl@0
|
3069 |
* returns a FILE * for it.
|
sl@0
|
3070 |
*
|
sl@0
|
3071 |
* Results:
|
sl@0
|
3072 |
* A standard Tcl result. If the channel is registered in the given
|
sl@0
|
3073 |
* interpreter and it is managed by the "file" channel driver, and
|
sl@0
|
3074 |
* it is open for the requested mode, then the output parameter
|
sl@0
|
3075 |
* filePtr is set to a FILE * for the underlying file. On error, the
|
sl@0
|
3076 |
* filePtr is not set, TCL_ERROR is returned and an error message is
|
sl@0
|
3077 |
* left in the interp's result.
|
sl@0
|
3078 |
*
|
sl@0
|
3079 |
* Side effects:
|
sl@0
|
3080 |
* May invoke fdopen to create the FILE * for the requested file.
|
sl@0
|
3081 |
*
|
sl@0
|
3082 |
*----------------------------------------------------------------------
|
sl@0
|
3083 |
*/
|
sl@0
|
3084 |
|
sl@0
|
3085 |
EXPORT_C int
|
sl@0
|
3086 |
Tcl_GetOpenFile(interp, string, forWriting, checkUsage, filePtr)
|
sl@0
|
3087 |
Tcl_Interp *interp; /* Interpreter in which to find file. */
|
sl@0
|
3088 |
CONST char *string; /* String that identifies file. */
|
sl@0
|
3089 |
int forWriting; /* 1 means the file is going to be used
|
sl@0
|
3090 |
* for writing, 0 means for reading. */
|
sl@0
|
3091 |
int checkUsage; /* 1 means verify that the file was opened
|
sl@0
|
3092 |
* in a mode that allows the access specified
|
sl@0
|
3093 |
* by "forWriting". Ignored, we always
|
sl@0
|
3094 |
* check that the channel is open for the
|
sl@0
|
3095 |
* requested mode. */
|
sl@0
|
3096 |
ClientData *filePtr; /* Store pointer to FILE structure here. */
|
sl@0
|
3097 |
{
|
sl@0
|
3098 |
Tcl_Channel chan;
|
sl@0
|
3099 |
int chanMode;
|
sl@0
|
3100 |
Tcl_ChannelType *chanTypePtr;
|
sl@0
|
3101 |
ClientData data;
|
sl@0
|
3102 |
int fd;
|
sl@0
|
3103 |
FILE *f;
|
sl@0
|
3104 |
|
sl@0
|
3105 |
chan = Tcl_GetChannel(interp, string, &chanMode);
|
sl@0
|
3106 |
if (chan == (Tcl_Channel) NULL) {
|
sl@0
|
3107 |
return TCL_ERROR;
|
sl@0
|
3108 |
}
|
sl@0
|
3109 |
if ((forWriting) && ((chanMode & TCL_WRITABLE) == 0)) {
|
sl@0
|
3110 |
Tcl_AppendResult(interp,
|
sl@0
|
3111 |
"\"", string, "\" wasn't opened for writing", (char *) NULL);
|
sl@0
|
3112 |
return TCL_ERROR;
|
sl@0
|
3113 |
} else if ((!(forWriting)) && ((chanMode & TCL_READABLE) == 0)) {
|
sl@0
|
3114 |
Tcl_AppendResult(interp,
|
sl@0
|
3115 |
"\"", string, "\" wasn't opened for reading", (char *) NULL);
|
sl@0
|
3116 |
return TCL_ERROR;
|
sl@0
|
3117 |
}
|
sl@0
|
3118 |
|
sl@0
|
3119 |
/*
|
sl@0
|
3120 |
* We allow creating a FILE * out of file based, pipe based and socket
|
sl@0
|
3121 |
* based channels. We currently do not allow any other channel types,
|
sl@0
|
3122 |
* because it is likely that stdio will not know what to do with them.
|
sl@0
|
3123 |
*/
|
sl@0
|
3124 |
|
sl@0
|
3125 |
chanTypePtr = Tcl_GetChannelType(chan);
|
sl@0
|
3126 |
if ((chanTypePtr == &fileChannelType)
|
sl@0
|
3127 |
#ifdef SUPPORTS_TTY
|
sl@0
|
3128 |
|| (chanTypePtr == &ttyChannelType)
|
sl@0
|
3129 |
#endif /* SUPPORTS_TTY */
|
sl@0
|
3130 |
|| (chanTypePtr == &tcpChannelType)
|
sl@0
|
3131 |
|| (strcmp(chanTypePtr->typeName, "pipe") == 0)) {
|
sl@0
|
3132 |
if (Tcl_GetChannelHandle(chan,
|
sl@0
|
3133 |
(forWriting ? TCL_WRITABLE : TCL_READABLE),
|
sl@0
|
3134 |
(ClientData*) &data) == TCL_OK) {
|
sl@0
|
3135 |
fd = (int) data;
|
sl@0
|
3136 |
|
sl@0
|
3137 |
/*
|
sl@0
|
3138 |
* The call to fdopen below is probably dangerous, since it will
|
sl@0
|
3139 |
* truncate an existing file if the file is being opened
|
sl@0
|
3140 |
* for writing....
|
sl@0
|
3141 |
*/
|
sl@0
|
3142 |
|
sl@0
|
3143 |
f = fdopen(fd, (forWriting ? "w" : "r"));
|
sl@0
|
3144 |
if (f == NULL) {
|
sl@0
|
3145 |
Tcl_AppendResult(interp, "cannot get a FILE * for \"", string,
|
sl@0
|
3146 |
"\"", (char *) NULL);
|
sl@0
|
3147 |
return TCL_ERROR;
|
sl@0
|
3148 |
}
|
sl@0
|
3149 |
*filePtr = (ClientData) f;
|
sl@0
|
3150 |
return TCL_OK;
|
sl@0
|
3151 |
}
|
sl@0
|
3152 |
}
|
sl@0
|
3153 |
|
sl@0
|
3154 |
Tcl_AppendResult(interp, "\"", string,
|
sl@0
|
3155 |
"\" cannot be used to get a FILE *", (char *) NULL);
|
sl@0
|
3156 |
return TCL_ERROR;
|
sl@0
|
3157 |
}
|
sl@0
|
3158 |
|
sl@0
|
3159 |
/*
|
sl@0
|
3160 |
*----------------------------------------------------------------------
|
sl@0
|
3161 |
*
|
sl@0
|
3162 |
* TclUnixWaitForFile --
|
sl@0
|
3163 |
*
|
sl@0
|
3164 |
* This procedure waits synchronously for a file to become readable
|
sl@0
|
3165 |
* or writable, with an optional timeout.
|
sl@0
|
3166 |
*
|
sl@0
|
3167 |
* Results:
|
sl@0
|
3168 |
* The return value is an OR'ed combination of TCL_READABLE,
|
sl@0
|
3169 |
* TCL_WRITABLE, and TCL_EXCEPTION, indicating the conditions
|
sl@0
|
3170 |
* that are present on file at the time of the return. This
|
sl@0
|
3171 |
* procedure will not return until either "timeout" milliseconds
|
sl@0
|
3172 |
* have elapsed or at least one of the conditions given by mask
|
sl@0
|
3173 |
* has occurred for file (a return value of 0 means that a timeout
|
sl@0
|
3174 |
* occurred). No normal events will be serviced during the
|
sl@0
|
3175 |
* execution of this procedure.
|
sl@0
|
3176 |
*
|
sl@0
|
3177 |
* Side effects:
|
sl@0
|
3178 |
* Time passes.
|
sl@0
|
3179 |
*
|
sl@0
|
3180 |
*----------------------------------------------------------------------
|
sl@0
|
3181 |
*/
|
sl@0
|
3182 |
|
sl@0
|
3183 |
int
|
sl@0
|
3184 |
TclUnixWaitForFile(fd, mask, timeout)
|
sl@0
|
3185 |
int fd; /* Handle for file on which to wait. */
|
sl@0
|
3186 |
int mask; /* What to wait for: OR'ed combination of
|
sl@0
|
3187 |
* TCL_READABLE, TCL_WRITABLE, and
|
sl@0
|
3188 |
* TCL_EXCEPTION. */
|
sl@0
|
3189 |
int timeout; /* Maximum amount of time to wait for one
|
sl@0
|
3190 |
* of the conditions in mask to occur, in
|
sl@0
|
3191 |
* milliseconds. A value of 0 means don't
|
sl@0
|
3192 |
* wait at all, and a value of -1 means
|
sl@0
|
3193 |
* wait forever. */
|
sl@0
|
3194 |
{
|
sl@0
|
3195 |
Tcl_Time abortTime = {0, 0}, now; /* silence gcc 4 warning */
|
sl@0
|
3196 |
struct timeval blockTime, *timeoutPtr;
|
sl@0
|
3197 |
int index, numFound, result = 0;
|
sl@0
|
3198 |
fd_mask bit;
|
sl@0
|
3199 |
fd_mask readyMasks[3*MASK_SIZE];
|
sl@0
|
3200 |
/* This array reflects the readable/writable
|
sl@0
|
3201 |
* conditions that were found to exist by the
|
sl@0
|
3202 |
* last call to select. */
|
sl@0
|
3203 |
|
sl@0
|
3204 |
/*
|
sl@0
|
3205 |
* If there is a non-zero finite timeout, compute the time when
|
sl@0
|
3206 |
* we give up.
|
sl@0
|
3207 |
*/
|
sl@0
|
3208 |
|
sl@0
|
3209 |
if (timeout > 0) {
|
sl@0
|
3210 |
Tcl_GetTime(&now);
|
sl@0
|
3211 |
abortTime.sec = now.sec + timeout/1000;
|
sl@0
|
3212 |
abortTime.usec = now.usec + (timeout%1000)*1000;
|
sl@0
|
3213 |
if (abortTime.usec >= 1000000) {
|
sl@0
|
3214 |
abortTime.usec -= 1000000;
|
sl@0
|
3215 |
abortTime.sec += 1;
|
sl@0
|
3216 |
}
|
sl@0
|
3217 |
timeoutPtr = &blockTime;
|
sl@0
|
3218 |
} else if (timeout == 0) {
|
sl@0
|
3219 |
timeoutPtr = &blockTime;
|
sl@0
|
3220 |
blockTime.tv_sec = 0;
|
sl@0
|
3221 |
blockTime.tv_usec = 0;
|
sl@0
|
3222 |
} else {
|
sl@0
|
3223 |
timeoutPtr = NULL;
|
sl@0
|
3224 |
}
|
sl@0
|
3225 |
|
sl@0
|
3226 |
/*
|
sl@0
|
3227 |
* Initialize the ready masks and compute the mask offsets.
|
sl@0
|
3228 |
*/
|
sl@0
|
3229 |
|
sl@0
|
3230 |
if (fd >= FD_SETSIZE) {
|
sl@0
|
3231 |
panic("TclWaitForFile can't handle file id %d", fd);
|
sl@0
|
3232 |
}
|
sl@0
|
3233 |
memset((VOID *) readyMasks, 0, 3*MASK_SIZE*sizeof(fd_mask));
|
sl@0
|
3234 |
index = fd/(NBBY*sizeof(fd_mask));
|
sl@0
|
3235 |
bit = ((fd_mask) 1) << (fd%(NBBY*sizeof(fd_mask)));
|
sl@0
|
3236 |
|
sl@0
|
3237 |
/*
|
sl@0
|
3238 |
* Loop in a mini-event loop of our own, waiting for either the
|
sl@0
|
3239 |
* file to become ready or a timeout to occur.
|
sl@0
|
3240 |
*/
|
sl@0
|
3241 |
|
sl@0
|
3242 |
while (1) {
|
sl@0
|
3243 |
if (timeout > 0) {
|
sl@0
|
3244 |
blockTime.tv_sec = abortTime.sec - now.sec;
|
sl@0
|
3245 |
blockTime.tv_usec = abortTime.usec - now.usec;
|
sl@0
|
3246 |
if (blockTime.tv_usec < 0) {
|
sl@0
|
3247 |
blockTime.tv_sec -= 1;
|
sl@0
|
3248 |
blockTime.tv_usec += 1000000;
|
sl@0
|
3249 |
}
|
sl@0
|
3250 |
if (blockTime.tv_sec < 0) {
|
sl@0
|
3251 |
blockTime.tv_sec = 0;
|
sl@0
|
3252 |
blockTime.tv_usec = 0;
|
sl@0
|
3253 |
}
|
sl@0
|
3254 |
}
|
sl@0
|
3255 |
|
sl@0
|
3256 |
/*
|
sl@0
|
3257 |
* Set the appropriate bit in the ready masks for the fd.
|
sl@0
|
3258 |
*/
|
sl@0
|
3259 |
|
sl@0
|
3260 |
if (mask & TCL_READABLE) {
|
sl@0
|
3261 |
readyMasks[index] |= bit;
|
sl@0
|
3262 |
}
|
sl@0
|
3263 |
if (mask & TCL_WRITABLE) {
|
sl@0
|
3264 |
(readyMasks+MASK_SIZE)[index] |= bit;
|
sl@0
|
3265 |
}
|
sl@0
|
3266 |
if (mask & TCL_EXCEPTION) {
|
sl@0
|
3267 |
(readyMasks+2*(MASK_SIZE))[index] |= bit;
|
sl@0
|
3268 |
}
|
sl@0
|
3269 |
|
sl@0
|
3270 |
/*
|
sl@0
|
3271 |
* Wait for the event or a timeout.
|
sl@0
|
3272 |
*/
|
sl@0
|
3273 |
|
sl@0
|
3274 |
numFound = select(fd+1, (SELECT_MASK *) &readyMasks[0],
|
sl@0
|
3275 |
(SELECT_MASK *) &readyMasks[MASK_SIZE],
|
sl@0
|
3276 |
(SELECT_MASK *) &readyMasks[2*MASK_SIZE], timeoutPtr);
|
sl@0
|
3277 |
if (numFound == 1) {
|
sl@0
|
3278 |
if (readyMasks[index] & bit) {
|
sl@0
|
3279 |
result |= TCL_READABLE;
|
sl@0
|
3280 |
}
|
sl@0
|
3281 |
if ((readyMasks+MASK_SIZE)[index] & bit) {
|
sl@0
|
3282 |
result |= TCL_WRITABLE;
|
sl@0
|
3283 |
}
|
sl@0
|
3284 |
if ((readyMasks+2*(MASK_SIZE))[index] & bit) {
|
sl@0
|
3285 |
result |= TCL_EXCEPTION;
|
sl@0
|
3286 |
}
|
sl@0
|
3287 |
result &= mask;
|
sl@0
|
3288 |
if (result) {
|
sl@0
|
3289 |
break;
|
sl@0
|
3290 |
}
|
sl@0
|
3291 |
}
|
sl@0
|
3292 |
if (timeout == 0) {
|
sl@0
|
3293 |
break;
|
sl@0
|
3294 |
}
|
sl@0
|
3295 |
if (timeout < 0) {
|
sl@0
|
3296 |
continue;
|
sl@0
|
3297 |
}
|
sl@0
|
3298 |
|
sl@0
|
3299 |
/*
|
sl@0
|
3300 |
* The select returned early, so we need to recompute the timeout.
|
sl@0
|
3301 |
*/
|
sl@0
|
3302 |
|
sl@0
|
3303 |
Tcl_GetTime(&now);
|
sl@0
|
3304 |
if ((abortTime.sec < now.sec)
|
sl@0
|
3305 |
|| ((abortTime.sec == now.sec)
|
sl@0
|
3306 |
&& (abortTime.usec <= now.usec))) {
|
sl@0
|
3307 |
break;
|
sl@0
|
3308 |
}
|
sl@0
|
3309 |
}
|
sl@0
|
3310 |
return result;
|
sl@0
|
3311 |
}
|
sl@0
|
3312 |
|
sl@0
|
3313 |
#ifdef DEPRECATED
|
sl@0
|
3314 |
/*
|
sl@0
|
3315 |
*----------------------------------------------------------------------
|
sl@0
|
3316 |
*
|
sl@0
|
3317 |
* FileThreadActionProc --
|
sl@0
|
3318 |
*
|
sl@0
|
3319 |
* Insert or remove any thread local refs to this channel.
|
sl@0
|
3320 |
*
|
sl@0
|
3321 |
* Results:
|
sl@0
|
3322 |
* None.
|
sl@0
|
3323 |
*
|
sl@0
|
3324 |
* Side effects:
|
sl@0
|
3325 |
* Changes thread local list of valid channels.
|
sl@0
|
3326 |
*
|
sl@0
|
3327 |
*----------------------------------------------------------------------
|
sl@0
|
3328 |
*/
|
sl@0
|
3329 |
|
sl@0
|
3330 |
static void
|
sl@0
|
3331 |
FileThreadActionProc (instanceData, action)
|
sl@0
|
3332 |
ClientData instanceData;
|
sl@0
|
3333 |
int action;
|
sl@0
|
3334 |
{
|
sl@0
|
3335 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
|
sl@0
|
3336 |
FileState *fsPtr = (FileState *) instanceData;
|
sl@0
|
3337 |
|
sl@0
|
3338 |
if (action == TCL_CHANNEL_THREAD_INSERT) {
|
sl@0
|
3339 |
fsPtr->nextPtr = tsdPtr->firstFilePtr;
|
sl@0
|
3340 |
tsdPtr->firstFilePtr = fsPtr;
|
sl@0
|
3341 |
} else {
|
sl@0
|
3342 |
FileState **nextPtrPtr;
|
sl@0
|
3343 |
int removed = 0;
|
sl@0
|
3344 |
|
sl@0
|
3345 |
for (nextPtrPtr = &(tsdPtr->firstFilePtr); (*nextPtrPtr) != NULL;
|
sl@0
|
3346 |
nextPtrPtr = &((*nextPtrPtr)->nextPtr)) {
|
sl@0
|
3347 |
if ((*nextPtrPtr) == fsPtr) {
|
sl@0
|
3348 |
(*nextPtrPtr) = fsPtr->nextPtr;
|
sl@0
|
3349 |
removed = 1;
|
sl@0
|
3350 |
break;
|
sl@0
|
3351 |
}
|
sl@0
|
3352 |
}
|
sl@0
|
3353 |
|
sl@0
|
3354 |
/*
|
sl@0
|
3355 |
* This could happen if the channel was created in one
|
sl@0
|
3356 |
* thread and then moved to another without updating
|
sl@0
|
3357 |
* the thread local data in each thread.
|
sl@0
|
3358 |
*/
|
sl@0
|
3359 |
|
sl@0
|
3360 |
if (!removed) {
|
sl@0
|
3361 |
panic("file info ptr not on thread channel list");
|
sl@0
|
3362 |
}
|
sl@0
|
3363 |
}
|
sl@0
|
3364 |
}
|
sl@0
|
3365 |
#endif /* DEPRECATED */
|
sl@0
|
3366 |
|