diff options
Diffstat (limited to 'unix/tclUnixChan.c')
-rw-r--r-- | unix/tclUnixChan.c | 936 |
1 files changed, 456 insertions, 480 deletions
diff --git a/unix/tclUnixChan.c b/unix/tclUnixChan.c index dd9a3b3..2e1131c 100644 --- a/unix/tclUnixChan.c +++ b/unix/tclUnixChan.c @@ -10,14 +10,14 @@ * See the file "license.terms" for information on usage and redistribution * of this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tclUnixChan.c,v 1.33 2002/05/13 14:06:22 dkf Exp $ + * RCS: @(#) $Id: tclUnixChan.c,v 1.34 2002/05/14 10:46:52 dkf Exp $ */ -#include "tclInt.h" /* Internal definitions for Tcl. */ -#include "tclPort.h" /* Portability features for Tcl. */ +#include "tclInt.h" /* Internal definitions for Tcl. */ +#include "tclPort.h" /* Portability features for Tcl. */ /* - * sys/ioctl.h has already been included by tclPort.h. Including termios.h + * sys/ioctl.h has already been included by tclPort.h. Including termios.h * or termio.h causes a bunch of warning messages because some duplicate * (but not contradictory) #defines exist in termios.h and/or termio.h */ @@ -46,11 +46,11 @@ #ifdef USE_TERMIOS # include <termios.h> # ifdef HAVE_SYS_IOCTL_H -# include <sys/ioctl.h> -# endif +# include <sys/ioctl.h> +# endif /* HAVE_SYS_IOCTL_H */ # ifdef HAVE_SYS_MODEM_H -# include <sys/modem.h> -# endif +# include <sys/modem.h> +# endif /* HAVE_SYS_MODEM_H */ # define IOSTATE struct termios # define GETIOSTATE(fd, statePtr) tcgetattr((fd), (statePtr)) # define SETIOSTATE(fd, statePtr) tcsetattr((fd), TCSADRAIN, (statePtr)) @@ -59,38 +59,42 @@ /* * TIP #35 introduced a different on exit flush/close behavior that * doesn't work correctly with standard channels on all systems. - * The problem is tcflush throws away waiting channel data. This may + * The problem is tcflush throws away waiting channel data. This may * be necessary for true serial channels that may block, but isn't * correct in the standard case. This might be replaced with tcdrain * instead, but that can block. For now, we revert to making this do * nothing, and TtyOutputProc being the same old FileOutputProc. * -- hobbs [Bug #525783] */ -#define BAD_TIP35_FLUSH 0 -#if BAD_TIP35_FLUSH -# define TTYFLUSH(fd) tcflush((fd), TCIOFLUSH); -#else -# define TTYFLUSH(fd) -#endif +# define BAD_TIP35_FLUSH 0 +# if BAD_TIP35_FLUSH +# define TTYFLUSH(fd) tcflush((fd), TCIOFLUSH); +# else +# define TTYFLUSH(fd) +# endif /* BAD_TIP35_FLUSH */ # ifdef FIONREAD # define GETREADQUEUE(fd, int) ioctl((fd), FIONREAD, &(int)) # elif defined(FIORDCHK) # define GETREADQUEUE(fd, int) int = ioctl((fd), FIORDCHK, NULL) -# endif +# endif /* FIONREAD */ # ifdef TIOCOUTQ # define GETWRITEQUEUE(fd, int) ioctl((fd), TIOCOUTQ, &(int)) -# endif +# endif /* TIOCOUTQ */ # if defined(TIOCSBRK) && defined(TIOCCBRK) -# define SETBREAK(fd, flag) \ - if (flag) { \ - ioctl((fd), TIOCSBRK, NULL);\ - } else { \ - ioctl((fd), TIOCCBRK, NULL);\ +/* + * Can't use ?: operator below because that messes up types on either + * Linux or Solaris (the two are mutually exclusive!) + */ +# define SETBREAK(fd, flag) \ + if (flag) { \ + ioctl((fd), TIOCSBRK, NULL); \ + } else { \ + ioctl((fd), TIOCCBRK, NULL); \ } -# endif +# endif /* TIOCSBRK&TIOCCBRK */ # if !defined(CRTSCTS) && defined(CNEW_RTSCTS) # define CRTSCTS CNEW_RTSCTS -# endif +# endif /* !CRTSCTS&CNEW_RTSCTS */ #else /* !USE_TERMIOS */ #ifdef USE_TERMIO @@ -125,7 +129,7 @@ typedef struct FileState { #ifdef DEPRECATED struct FileState *nextPtr; /* Pointer to next file in list of all * file channels. */ -#endif +#endif /* DEPRECATED */ } FileState; #ifdef SUPPORTS_TTY @@ -137,7 +141,7 @@ typedef struct FileState { typedef struct TtyState { FileState fs; /* Per-instance state of the file - * descriptor. Must be the first field. */ + * descriptor. Must be the first field. */ int stateUpdated; /* Flag to say if the state has been * modified and needs resetting. */ IOSTATE savedState; /* Initial state of device. Used to reset @@ -148,7 +152,7 @@ typedef struct TtyState { * The following structure is used to set or get the serial port * attributes in a platform-independant manner. */ - + typedef struct TtyAttrs { int baud; int parity; @@ -158,18 +162,24 @@ typedef struct TtyAttrs { #endif /* !SUPPORTS_TTY */ +#define UNSUPPORTED_OPTION(detail) \ + if (interp) { \ + Tcl_AppendResult(interp, (detail), \ + " not supported for this platform", (char *) NULL); \ + } + #ifdef DEPRECATED typedef struct ThreadSpecificData { /* * List of all file channels currently open. This is per thread and is * used to match up fd's to channels, which rarely occurs. */ - + FileState *firstFilePtr; } ThreadSpecificData; static Tcl_ThreadDataKey dataKey; -#endif +#endif /* DEPRECATED */ /* * This structure describes per-instance state of a tcp based channel. @@ -200,14 +210,14 @@ typedef struct TcpState { * the connection request will fail. */ -#ifndef SOMAXCONN -#define SOMAXCONN 100 -#endif +#ifndef SOMAXCONN +# define SOMAXCONN 100 +#endif /* SOMAXCONN */ -#if (SOMAXCONN < 100) -#undef SOMAXCONN -#define SOMAXCONN 100 -#endif +#if (SOMAXCONN < 100) +# undef SOMAXCONN +# define SOMAXCONN 100 +#endif /* SOMAXCONN < 100 */ /* * The following defines how much buffer space the kernel should maintain @@ -227,36 +237,36 @@ static int CreateSocketAddress _ANSI_ARGS_( (struct sockaddr_in *sockaddrPtr, CONST char *host, int port)); static int FileBlockModeProc _ANSI_ARGS_(( - ClientData instanceData, int mode)); + ClientData instanceData, int mode)); static int FileCloseProc _ANSI_ARGS_((ClientData instanceData, Tcl_Interp *interp)); static int FileGetHandleProc _ANSI_ARGS_((ClientData instanceData, - int direction, ClientData *handlePtr)); + int direction, ClientData *handlePtr)); static int FileInputProc _ANSI_ARGS_((ClientData instanceData, - char *buf, int toRead, int *errorCode)); + char *buf, int toRead, int *errorCode)); static int FileOutputProc _ANSI_ARGS_(( ClientData instanceData, CONST char *buf, int toWrite, int *errorCode)); static Tcl_WideInt FileSeekProc _ANSI_ARGS_((ClientData instanceData, Tcl_WideInt offset, int mode, int *errorCode)); static void FileWatchProc _ANSI_ARGS_((ClientData instanceData, - int mask)); + int mask)); static void TcpAccept _ANSI_ARGS_((ClientData data, int mask)); static int TcpBlockModeProc _ANSI_ARGS_((ClientData data, - int mode)); + int mode)); static int TcpCloseProc _ANSI_ARGS_((ClientData instanceData, Tcl_Interp *interp)); static int TcpGetHandleProc _ANSI_ARGS_((ClientData instanceData, - int direction, ClientData *handlePtr)); + int direction, ClientData *handlePtr)); static int TcpGetOptionProc _ANSI_ARGS_((ClientData instanceData, Tcl_Interp *interp, CONST char *optionName, Tcl_DString *dsPtr)); static int TcpInputProc _ANSI_ARGS_((ClientData instanceData, - char *buf, int toRead, int *errorCode)); + char *buf, int toRead, int *errorCode)); static int TcpOutputProc _ANSI_ARGS_((ClientData instanceData, - CONST char *buf, int toWrite, int *errorCode)); + CONST char *buf, int toWrite, int *errorCode)); static void TcpWatchProc _ANSI_ARGS_((ClientData instanceData, - int mask)); + int mask)); #ifdef SUPPORTS_TTY static int TtyCloseProc _ANSI_ARGS_((ClientData instanceData, Tcl_Interp *interp)); @@ -266,8 +276,10 @@ static int TtyGetOptionProc _ANSI_ARGS_((ClientData instanceData, Tcl_Interp *interp, CONST char *optionName, Tcl_DString *dsPtr)); static FileState * TtyInit _ANSI_ARGS_((int fd, int initialize)); +#if BAD_TIP35_FLUSH static int TtyOutputProc _ANSI_ARGS_((ClientData instanceData, CONST char *buf, int toWrite, int *errorCode)); +#endif /* BAD_TIP35_FLUSH */ static int TtyParseMode _ANSI_ARGS_((Tcl_Interp *interp, CONST char *mode, int *speedPtr, int *parityPtr, int *dataPtr, int *stopPtr)); @@ -278,7 +290,7 @@ static int TtySetOptionProc _ANSI_ARGS_((ClientData instanceData, CONST char *value)); #endif /* SUPPORTS_TTY */ static int WaitForConnect _ANSI_ARGS_((TcpState *statePtr, - int *errorCodePtr)); + int *errorCodePtr)); /* * This structure describes the channel type structure for file based IO: @@ -314,9 +326,9 @@ static Tcl_ChannelType ttyChannelType = { FileInputProc, /* Input proc. */ #if BAD_TIP35_FLUSH TtyOutputProc, /* Output proc. */ -#else +#else /* !BAD_TIP35_FLUSH */ FileOutputProc, /* Output proc. */ -#endif +#endif /* BAD_TIP35_FLUSH */ NULL, /* Seek proc. */ TtySetOptionProc, /* Set option proc. */ TtyGetOptionProc, /* Get option proc. */ @@ -374,8 +386,8 @@ static int FileBlockModeProc(instanceData, mode) ClientData instanceData; /* File state. */ int mode; /* The mode to set. Can be one of - * TCL_MODE_BLOCKING or - * TCL_MODE_NONBLOCKING. */ + * TCL_MODE_BLOCKING or + * TCL_MODE_NONBLOCKING. */ { FileState *fsPtr = (FileState *) instanceData; int curStatus; @@ -391,7 +403,7 @@ FileBlockModeProc(instanceData, mode) return errno; } curStatus = fcntl(fsPtr->fd, F_GETFL); -#else +#else /* USE_FIONBIO */ if (mode == TCL_MODE_BLOCKING) { curStatus = 0; } else { @@ -400,7 +412,7 @@ FileBlockModeProc(instanceData, mode) if (ioctl(fsPtr->fd, (int) FIONBIO, &curStatus) < 0) { return errno; } -#endif +#endif /* !USE_FIONBIO */ return 0; } @@ -427,15 +439,15 @@ FileInputProc(instanceData, buf, toRead, errorCodePtr) ClientData instanceData; /* File state. */ char *buf; /* Where to store data read. */ int toRead; /* How much space is available - * in the buffer? */ + * in the buffer? */ int *errorCodePtr; /* Where to store error code. */ { FileState *fsPtr = (FileState *) instanceData; int bytesRead; /* How many bytes were actually - * read from the input device? */ + * read from the input device? */ *errorCodePtr = 0; - + /* * Assume there is always enough input available. This will block * appropriately, and read will unblock as soon as a short read is @@ -445,7 +457,7 @@ FileInputProc(instanceData, buf, toRead, errorCodePtr) bytesRead = read(fsPtr->fd, buf, (size_t) toRead); if (bytesRead > -1) { - return bytesRead; + return bytesRead; } *errorCodePtr = errno; return -1; @@ -461,7 +473,7 @@ FileInputProc(instanceData, buf, toRead, errorCodePtr) * * Results: * The number of bytes written is returned or -1 on error. An - * output argument contains a POSIX error code if an error occurred, + * output argument contains a POSIX error code if an error occurred, * or zero. * * Side effects: @@ -483,17 +495,18 @@ FileOutputProc(instanceData, buf, toWrite, errorCodePtr) *errorCodePtr = 0; if (toWrite == 0) { - /* SF Tcl Bug 465765. - * Do not try to write nothing into a file. STREAM based - * implementations will considers this as EOF (if there is a - * pipe behind the file). - */ + /* + * SF Tcl Bug 465765. + * Do not try to write nothing into a file. STREAM based + * implementations will considers this as EOF (if there is a + * pipe behind the file). + */ - return 0; + return 0; } written = write(fsPtr->fd, buf, (size_t) toWrite); if (written > -1) { - return written; + return written; } *errorCodePtr = errno; return -1; @@ -522,13 +535,11 @@ FileCloseProc(instanceData, interp) Tcl_Interp *interp; /* For error reporting - unused. */ { FileState *fsPtr = (FileState *) instanceData; -#ifdef DEPRECATED - FileState **nextPtrPtr; -#endif int errorCode = 0; #ifdef DEPRECATED + FileState **nextPtrPtr; ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); -#endif +#endif /* DEPRECATED */ Tcl_DeleteFileHandler(fsPtr->fd); /* @@ -549,7 +560,7 @@ FileCloseProc(instanceData, interp) break; } } -#endif +#endif /* DEPRECATED */ ckfree((char *) fsPtr); return errorCode; } @@ -579,9 +590,9 @@ FileSeekProc(instanceData, offset, mode, errorCodePtr) ClientData instanceData; /* File state. */ Tcl_WideInt offset; /* Offset to seek to. */ int mode; /* Relative to where - * should we seek? Can be - * one of SEEK_START, - * SEEK_SET or SEEK_END. */ + * should we seek? Can be + * one of SEEK_START, + * SEEK_SET or SEEK_END. */ int *errorCodePtr; /* To store error code. */ { FileState *fsPtr = (FileState *) instanceData; @@ -614,8 +625,8 @@ static void FileWatchProc(instanceData, mask) ClientData instanceData; /* The file state. */ int mask; /* Events of interest; an OR-ed - * combination of TCL_READABLE, - * TCL_WRITABLE and TCL_EXCEPTION. */ + * combination of TCL_READABLE, + * TCL_WRITABLE and TCL_EXCEPTION. */ { FileState *fsPtr = (FileState *) instanceData; @@ -694,17 +705,17 @@ TtyCloseProc(instanceData, interp) { #if BAD_TIP35_FLUSH TtyState *ttyPtr = (TtyState *) instanceData; -#endif +#endif /* BAD_TIP35_FLUSH */ #ifdef TTYFLUSH TTYFLUSH(ttyPtr->fs.fd); -#endif +#endif /* TTYFLUSH */ #if 0 /* * TIP#35 agreed to remove the unsave so that TCL could be used as a * simple stty. * It would be cleaner to remove all the stuff related to - * TtyState.stateUpdated - * TtyState.savedState + * TtyState.stateUpdated + * TtyState.savedState * Then the structure TtyState would be the same as FileState. * IMO this cleanup could better be done for the final 8.4 release * after nobody complained about the missing unsave. -- schroedter @@ -726,7 +737,7 @@ TtyCloseProc(instanceData, interp) * * Results: * The number of bytes written is returned or -1 on error. An - * output argument contains a POSIX error code if an error occurred, + * output argument contains a POSIX error code if an error occurred, * or zero. * * Side effects: @@ -736,6 +747,7 @@ TtyCloseProc(instanceData, interp) *---------------------------------------------------------------------- */ +#if BAD_TIP35_FLUSH static int TtyOutputProc(instanceData, buf, toWrite, errorCodePtr) ClientData instanceData; /* File state. */ @@ -743,7 +755,7 @@ TtyOutputProc(instanceData, buf, toWrite, errorCodePtr) int toWrite; /* How many bytes to write? */ int *errorCodePtr; /* Where to store error code. */ { - if ( TclInExit() ) { + if (TclInExit()) { /* * Do not write data during Tcl exit. * Serial port may block preventing Tcl from exit. @@ -753,6 +765,7 @@ TtyOutputProc(instanceData, buf, toWrite, errorCodePtr) return FileOutputProc(instanceData, buf, toWrite, errorCodePtr); } } +#endif /* BAD_TIP35_FLUSH */ #ifdef USE_TERMIOS /* @@ -766,27 +779,27 @@ TtyOutputProc(instanceData, buf, toWrite, errorCodePtr) */ static void TtyModemStatusStr(status, dsPtr) - int status; /* RS232 modem status */ - Tcl_DString *dsPtr; /* Where to store string */ + int status; /* RS232 modem status */ + Tcl_DString *dsPtr; /* Where to store string */ { #ifdef TIOCM_CTS Tcl_DStringAppendElement(dsPtr, "CTS"); - Tcl_DStringAppendElement(dsPtr, (status & TIOCM_CTS) ? "1" : "0" ); -#endif + Tcl_DStringAppendElement(dsPtr, (status & TIOCM_CTS) ? "1" : "0"); +#endif /* TIOCM_CTS */ #ifdef TIOCM_DSR Tcl_DStringAppendElement(dsPtr, "DSR"); - Tcl_DStringAppendElement(dsPtr, (status & TIOCM_DSR) ? "1" : "0" ); -#endif + Tcl_DStringAppendElement(dsPtr, (status & TIOCM_DSR) ? "1" : "0"); +#endif /* TIOCM_DSR */ #ifdef TIOCM_RNG Tcl_DStringAppendElement(dsPtr, "RING"); - Tcl_DStringAppendElement(dsPtr, (status & TIOCM_RNG) ? "1" : "0" ); -#endif + Tcl_DStringAppendElement(dsPtr, (status & TIOCM_RNG) ? "1" : "0"); +#endif /* TIOCM_RNG */ #ifdef TIOCM_CD Tcl_DStringAppendElement(dsPtr, "DCD"); - Tcl_DStringAppendElement(dsPtr, (status & TIOCM_CD) ? "1" : "0" ); -#endif + Tcl_DStringAppendElement(dsPtr, (status & TIOCM_CD) ? "1" : "0"); +#endif /* TIOCM_CD */ } -#endif +#endif /* USE_TERMIOS */ /* *---------------------------------------------------------------------- @@ -801,7 +814,7 @@ TtyModemStatusStr(status, dsPtr) * * Side effects: * May modify an option on a device. - * Sets Error message if needed (by calling Tcl_BadChannelOption). + * Sets Error message if needed (by calling Tcl_BadChannelOption). * *---------------------------------------------------------------------- */ @@ -820,7 +833,7 @@ TtySetOptionProc(instanceData, interp, optionName, value) int flag, control, argc; CONST char **argv; IOSTATE iostate; -#endif +#endif /* USE_TERMIOS */ len = strlen(optionName); vlen = strlen(value); @@ -856,7 +869,7 @@ TtySetOptionProc(instanceData, interp, optionName, value) iostate.c_iflag &= ~(IXON | IXOFF | IXANY); #ifdef CRTSCTS iostate.c_cflag &= ~CRTSCTS; -#endif +#endif /* CRTSCTS */ if (strncasecmp(value, "NONE", vlen) == 0) { /* leave all handshake options disabled */ } else if (strncasecmp(value, "XONXOFF", vlen) == 0) { @@ -864,28 +877,20 @@ TtySetOptionProc(instanceData, interp, optionName, value) } else if (strncasecmp(value, "RTSCTS", vlen) == 0) { #ifdef CRTSCTS iostate.c_cflag |= CRTSCTS; -#else - if (interp) { - Tcl_AppendResult(interp, "-handshake RTSCTS ", - "not supported for this platform", - (char *) NULL); - } +#else /* !CRTSTS */ + UNSUPPORTED_OPTION("-handshake RTSCTS"); return TCL_ERROR; -#endif +#endif /* CRTSCTS */ } else if (strncasecmp(value, "DTRDSR", vlen) == 0) { - if (interp) { - Tcl_AppendResult(interp, "-handshake DTRDSR ", - "not supported for this platform", - (char *) NULL); - } + UNSUPPORTED_OPTION("-handshake DTRDSR"); return TCL_ERROR; - } else { + } else { if (interp) { Tcl_AppendResult(interp, "bad value for -handshake: ", - "must be one of xonxoff, rtscts, dtrdsr or none", - (char *) NULL); - return TCL_ERROR; + "must be one of xonxoff, rtscts, dtrdsr or none", + (char *) NULL); } + return TCL_ERROR; } SETIOSTATE(fsPtr->fd, &iostate); return TCL_OK; @@ -895,14 +900,13 @@ TtySetOptionProc(instanceData, interp, optionName, value) * Option -xchar {\x11 \x13} */ if ((len > 1) && (strncmp(optionName, "-xchar", len) == 0)) { - GETIOSTATE(fsPtr->fd, &iostate); if (Tcl_SplitList(interp, value, &argc, &argv) == TCL_ERROR) { return TCL_ERROR; } if (argc == 2) { iostate.c_cc[VSTART] = argv[0][0]; - iostate.c_cc[VSTOP] = argv[1][0]; + iostate.c_cc[VSTOP] = argv[1][0]; } else { if (interp) { Tcl_AppendResult(interp, @@ -910,19 +914,19 @@ TtySetOptionProc(instanceData, interp, optionName, value) (char *) NULL); } return TCL_ERROR; - } + } SETIOSTATE(fsPtr->fd, &iostate); return TCL_OK; } /* - * Option -timeout msec - */ + * Option -timeout msec + */ if ((len > 2) && (strncmp(optionName, "-timeout", len) == 0)) { int msec; GETIOSTATE(fsPtr->fd, &iostate); - if ( Tcl_GetInt(interp, value, &msec) != TCL_OK ) { + if (Tcl_GetInt(interp, value, &msec) != TCL_OK) { return TCL_ERROR; } iostate.c_cc[VMIN] = 0; @@ -935,15 +939,14 @@ TtySetOptionProc(instanceData, interp, optionName, value) * Option -ttycontrol {DTR 1 RTS 0 BREAK 0} */ if ((len > 4) && (strncmp(optionName, "-ttycontrol", len) == 0)) { - if (Tcl_SplitList(interp, value, &argc, &argv) == TCL_ERROR) { return TCL_ERROR; } if ((argc % 2) == 1) { if (interp) { Tcl_AppendResult(interp, - "bad value for -ttycontrol: should be a list of", - "signal,value pairs", (char *) NULL); + "bad value for -ttycontrol: should be a list of", + "signal,value pairs", (char *) NULL); } return TCL_ERROR; } @@ -955,50 +958,38 @@ TtySetOptionProc(instanceData, interp, optionName, value) } if (strncasecmp(argv[0], "DTR", strlen(argv[0])) == 0) { #ifdef TIOCM_DTR - if (flag ) { + if (flag) { control |= TIOCM_DTR; } else { control &= ~TIOCM_DTR; } -#else - if (interp) { - Tcl_AppendResult(interp, "-ttycontrol DTR ", - "not supported for this platform", - (char *) NULL); - } +#else /* !TIOCM_DTR */ + UNSUPPORTED_OPTION("-ttycontrol DTR"); return TCL_ERROR; -#endif +#endif /* TIOCM_DTR */ } else if (strncasecmp(argv[0], "RTS", strlen(argv[0])) == 0) { #ifdef TIOCM_RTS - if (flag ) { + if (flag) { control |= TIOCM_RTS; } else { control &= ~TIOCM_RTS; } -#else - if (interp) { - Tcl_AppendResult(interp, "-ttycontrol RTS ", - "not supported for this platform", - (char *) NULL); - } +#else /* !TIOCM_RTS*/ + UNSUPPORTED_OPTION("-ttycontrol RTS"); return TCL_ERROR; -#endif +#endif /* TIOCM_RTS*/ } else if (strncasecmp(argv[0], "BREAK", strlen(argv[0])) == 0) { #ifdef SETBREAK SETBREAK(fsPtr->fd, flag); -#else - if (interp) { - Tcl_AppendResult(interp, "-ttycontrol BREAK ", - "not supported for this platform", - (char *) NULL); - } +#else /* !SETBREAK */ + UNSUPPORTED_OPTION("-ttycontrol BREAK"); return TCL_ERROR; -#endif +#endif /* SETBREAK */ } else { if (interp) { Tcl_AppendResult(interp, - "bad signal for -ttycontrol: must be ", - "DTR, RTS or BREAK", (char *) NULL); + "bad signal for -ttycontrol: must be ", + "DTR, RTS or BREAK", (char *) NULL); } return TCL_ERROR; } @@ -1007,14 +998,14 @@ TtySetOptionProc(instanceData, interp, optionName, value) SETCONTROL(fsPtr->fd, &control); return TCL_OK; - } + } return Tcl_BadChannelOption(interp, optionName, - "mode handshake timeout ttycontrol xchar "); + "mode handshake timeout ttycontrol xchar "); -#else /* not USE_TERMIOS */ +#else /* !USE_TERMIOS */ return Tcl_BadChannelOption(interp, optionName, "mode"); -#endif +#endif /* USE_TERMIOS */ } /* @@ -1034,7 +1025,7 @@ TtySetOptionProc(instanceData, interp, optionName, value) * Side effects: * The string returned by this function is in static storage and * may be reused at any time subsequent to the call. - * Sets Error message if needed (by calling Tcl_BadChannelOption). + * Sets Error message if needed (by calling Tcl_BadChannelOption). * *---------------------------------------------------------------------- */ @@ -1060,8 +1051,7 @@ TtyGetOptionProc(instanceData, interp, optionName, dsPtr) if (len == 0) { Tcl_DStringAppendElement(dsPtr, "-mode"); } - if ((len == 0) || - ((len > 2) && (strncmp(optionName, "-mode", len) == 0))) { + if (len==0 || (len>2 && strncmp(optionName, "-mode", len)==0)) { valid = 1; TtyGetAttributes(fsPtr->fd, &tty); sprintf(buf, "%d,%c,%d,%d", tty.baud, tty.parity, tty.data, tty.stop); @@ -1076,9 +1066,7 @@ TtyGetOptionProc(instanceData, interp, optionName, dsPtr) Tcl_DStringAppendElement(dsPtr, "-xchar"); Tcl_DStringStartSublist(dsPtr); } - if ((len == 0) || - ((len > 1) && (strncmp(optionName, "-xchar", len) == 0))) { - + if (len==0 || (len>1 && strncmp(optionName, "-xchar", len)==0)) { IOSTATE iostate; valid = 1; @@ -1097,16 +1085,16 @@ TtyGetOptionProc(instanceData, interp, optionName, dsPtr) * option is readonly and returned by [fconfigure chan -queue] * but not returned by unnamed [fconfigure chan] */ - if ( (len > 1) && (strncmp(optionName, "-queue", len) == 0) ) { + if ((len > 1) && (strncmp(optionName, "-queue", len) == 0)) { int inQueue=0, outQueue=0; int inBuffered, outBuffered; - valid = 1; + valid = 1; #ifdef GETREADQUEUE GETREADQUEUE(fsPtr->fd, inQueue); -#endif +#endif /* GETREADQUEUE */ #ifdef GETWRITEQUEUE GETWRITEQUEUE(fsPtr->fd, outQueue); -#endif +#endif /* GETWRITEQUEUE */ inBuffered = Tcl_InputBuffered(fsPtr->channel); outBuffered = Tcl_OutputBuffered(fsPtr->channel); @@ -1121,13 +1109,13 @@ TtyGetOptionProc(instanceData, interp, optionName, dsPtr) * option is readonly and returned by [fconfigure chan -ttystatus] * but not returned by unnamed [fconfigure chan] */ - if ( (len > 4) && (strncmp(optionName, "-ttystatus", len) == 0) ) { + if ((len > 4) && (strncmp(optionName, "-ttystatus", len) == 0)) { int status; valid = 1; GETCONTROL(fsPtr->fd, &status); TtyModemStatusStr(status, dsPtr); } -#endif +#endif /* USE_TERMIOS */ if (valid) { return TCL_OK; @@ -1135,9 +1123,9 @@ TtyGetOptionProc(instanceData, interp, optionName, dsPtr) return Tcl_BadChannelOption(interp, optionName, #ifdef USE_TERMIOS "mode queue ttystatus xchar"); -#else +#else /* !USE_TERMIOS */ "mode"); -#endif +#endif /* USE_TERMIOS */ } } @@ -1145,13 +1133,13 @@ TtyGetOptionProc(instanceData, interp, optionName, dsPtr) #ifdef B4800 # if (B4800 == 4800) # define DIRECT_BAUD -# endif -#endif +# endif /* B4800 == 4800 */ +#endif /* B4800 */ #ifdef DIRECT_BAUD # define TtyGetSpeed(baud) ((unsigned) (baud)) # define TtyGetBaud(speed) ((int) (speed)) -#else +#else /* !DIRECT_BAUD */ static struct {int baud; unsigned long speed;} speeds[] = { #ifdef B0 @@ -1267,10 +1255,10 @@ TtyGetSpeed(baud) int baud; /* The baud rate to look up. */ { int bestIdx, bestDiff, i, diff; - + bestIdx = 0; bestDiff = 1000000; - + /* * If the baud rate does not correspond to one of the known mask values, * choose the mask value whose baud rate is closest to the specified @@ -1312,7 +1300,7 @@ TtyGetBaud(speed) unsigned long speed; /* Speed mask value to look up. */ { int i; - + for (i = 0; speeds[i].baud >= 0; i++) { if (speeds[i].speed == speed) { return speeds[i].baud; @@ -1321,7 +1309,7 @@ TtyGetBaud(speed) return 0; } -#endif /* !DIRECT_BAUD */ +#endif /* !DIRECT_BAUD */ /* @@ -1339,7 +1327,7 @@ TtyGetBaud(speed) * *--------------------------------------------------------------------------- */ - + static void TtyGetAttributes(fd, ttyPtr) int fd; /* Open file descriptor for serial port to @@ -1354,27 +1342,27 @@ TtyGetAttributes(fd, ttyPtr) #ifdef USE_TERMIOS baud = TtyGetBaud(cfgetospeed(&iostate)); - + parity = 'n'; #ifdef PAREXT switch ((int) (iostate.c_cflag & (PARENB | PARODD | PAREXT))) { case PARENB : parity = 'e'; break; - case PARENB | PARODD : parity = 'o'; break; + case PARENB | PARODD : parity = 'o'; break; case PARENB | PAREXT : parity = 's'; break; - case PARENB | PARODD | PAREXT : parity = 'm'; break; + case PARENB | PARODD | PAREXT : parity = 'm'; break; } -#else /* !PAREXT */ +#else /* !PAREXT */ switch ((int) (iostate.c_cflag & (PARENB | PARODD))) { case PARENB : parity = 'e'; break; - case PARENB | PARODD : parity = 'o'; break; + case PARENB | PARODD : parity = 'o'; break; } -#endif /* !PAREXT */ +#endif /* !PAREXT */ data = iostate.c_cflag & CSIZE; data = (data == CS5) ? 5 : (data == CS6) ? 6 : (data == CS7) ? 7 : 8; stop = (iostate.c_cflag & CSTOPB) ? 2 : 1; -#endif /* USE_TERMIOS */ +#endif /* USE_TERMIOS */ #ifdef USE_TERMIO baud = TtyGetBaud(iostate.c_cflag & CBAUD); @@ -1382,16 +1370,16 @@ TtyGetAttributes(fd, ttyPtr) parity = 'n'; switch (iostate.c_cflag & (PARENB | PARODD | PAREXT)) { case PARENB : parity = 'e'; break; - case PARENB | PARODD : parity = 'o'; break; + case PARENB | PARODD : parity = 'o'; break; case PARENB | PAREXT : parity = 's'; break; - case PARENB | PARODD | PAREXT : parity = 'm'; break; + case PARENB | PARODD | PAREXT : parity = 'm'; break; } data = iostate.c_cflag & CSIZE; data = (data == CS5) ? 5 : (data == CS6) ? 6 : (data == CS7) ? 7 : 8; stop = (iostate.c_cflag & CSTOPB) ? 2 : 1; -#endif /* USE_TERMIO */ +#endif /* USE_TERMIO */ #ifdef USE_SGTTY baud = TtyGetBaud(iostate.sg_ospeed); @@ -1406,7 +1394,7 @@ TtyGetAttributes(fd, ttyPtr) data = (iostate.sg_flags & (EVENP | ODDP)) ? 7 : 8; stop = 1; -#endif /* USE_SGTTY */ +#endif /* USE_SGTTY */ ttyPtr->baud = baud; ttyPtr->parity = parity; @@ -1429,7 +1417,7 @@ TtyGetAttributes(fd, ttyPtr) * *--------------------------------------------------------------------------- */ - + static void TtySetAttributes(fd, ttyPtr) int fd; /* Open file descriptor for serial port to @@ -1455,7 +1443,7 @@ TtySetAttributes(fd, ttyPtr) if ((parity == 'm') || (parity == 's')) { flag |= PAREXT; } -#endif +#endif /* PAREXT */ if ((parity == 'm') || (parity == 'o')) { flag |= PARODD; } @@ -1538,7 +1526,7 @@ TtySetAttributes(fd, ttyPtr) * *--------------------------------------------------------------------------- */ - + static int TtyParseMode(interp, mode, speedPtr, parityPtr, dataPtr, stopPtr) Tcl_Interp *interp; /* If non-NULL, interp for error return. */ @@ -1571,7 +1559,7 @@ TtyParseMode(interp, mode, speedPtr, parityPtr, dataPtr, stopPtr) strchr("noems", parity) == NULL #else strchr("noe", parity) == NULL -#endif +#endif /* PAREXT|USE_TERMIO */ ) { if (interp != NULL) { Tcl_AppendResult(interp, bad, @@ -1579,7 +1567,7 @@ TtyParseMode(interp, mode, speedPtr, parityPtr, dataPtr, stopPtr) " parity: should be n, o, e, m, or s", #else " parity: should be n, o, or e", -#endif +#endif /* PAREXT|USE_TERMIO */ NULL); } return TCL_ERROR; @@ -1641,11 +1629,11 @@ TtyInit(fd, initialize) #if defined(USE_TERMIOS) || defined(USE_TERMIO) if (iostate.c_iflag != IGNBRK || - iostate.c_oflag != 0 || - iostate.c_lflag != 0 || - iostate.c_cflag & CREAD || - iostate.c_cc[VMIN] != 1 || - iostate.c_cc[VTIME] != 0) { + iostate.c_oflag != 0 || + iostate.c_lflag != 0 || + iostate.c_cflag & CREAD || + iostate.c_cc[VMIN] != 1 || + iostate.c_cc[VTIME] != 0) { ttyPtr->stateUpdated = 1; } iostate.c_iflag = IGNBRK; @@ -1658,7 +1646,7 @@ TtyInit(fd, initialize) #ifdef USE_SGTTY if ((iostate.sg_flags & (EVENP | ODDP)) || - !(iostate.sg_flags & RAW)) { + !(iostate.sg_flags & RAW)) { ttyPtr->stateUpdated = 1; } iostate.sg_flags &= (EVENP | ODDP); @@ -1700,13 +1688,13 @@ TtyInit(fd, initialize) Tcl_Channel TclpOpenFileChannel(interp, pathPtr, modeString, permissions) Tcl_Interp *interp; /* Interpreter for error reporting; - * can be NULL. */ + * can be NULL. */ Tcl_Obj *pathPtr; /* Name of file to open. */ CONST char *modeString; /* A list of POSIX open modes or - * a string such as "rw". */ + * a string such as "rw". */ int permissions; /* If the open involves creating a - * file, with what modes to create - * it? */ + * file, with what modes to create + * it? */ { int fd, seekFlag, mode, channelPermissions; FileState *fsPtr; @@ -1715,14 +1703,14 @@ TclpOpenFileChannel(interp, pathPtr, modeString, permissions) Tcl_ChannelType *channelTypePtr; #ifdef SUPPORTS_TTY int ctl_tty; -#endif +#endif /* SUPPORTS_TTY */ #ifdef DEPRECATED ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); -#endif +#endif /* DEPRECATED */ mode = TclGetOpenMode(interp, modeString, &seekFlag); if (mode == -1) { - return NULL; + return NULL; } switch (mode & (O_RDONLY | O_WRONLY | O_RDWR)) { case O_RDONLY: @@ -1735,9 +1723,9 @@ TclpOpenFileChannel(interp, pathPtr, modeString, permissions) channelPermissions = (TCL_READABLE | TCL_WRITABLE); break; default: - /* - * This may occurr if modeString was "", for example. - */ + /* + * This may occurr if modeString was "", for example. + */ panic("TclpOpenFileChannel: invalid mode value"); return NULL; } @@ -1749,26 +1737,26 @@ TclpOpenFileChannel(interp, pathPtr, modeString, permissions) fd = Tcl_PlatformOpen(native, mode, permissions); #ifdef SUPPORTS_TTY ctl_tty = (strcmp (native, "/dev/tty") == 0); -#endif +#endif /* SUPPORTS_TTY */ if (fd < 0) { - if (interp != (Tcl_Interp *) NULL) { - Tcl_AppendResult(interp, "couldn't open \"", - Tcl_GetString(pathPtr), "\": ", - Tcl_PosixError(interp), (char *) NULL); - } - return NULL; + if (interp != (Tcl_Interp *) NULL) { + Tcl_AppendResult(interp, "couldn't open \"", + Tcl_GetString(pathPtr), "\": ", + Tcl_PosixError(interp), (char *) NULL); + } + return NULL; } /* * Set close-on-exec flag on the fd so that child processes will not * inherit this fd. */ - + fcntl(fd, F_SETFD, FD_CLOEXEC); - + sprintf(channelName, "file%d", fd); - + #ifdef SUPPORTS_TTY if (!ctl_tty && isatty(fd)) { /* @@ -1778,7 +1766,7 @@ TclpOpenFileChannel(interp, pathPtr, modeString, permissions) * were sent to the serial port, the remote device would echo it, * then the serial driver would echo it back to the device, etc. */ - + translation = "auto crlf"; channelTypePtr = &ttyChannelType; fsPtr = TtyInit(fd, 1); @@ -1793,23 +1781,23 @@ TclpOpenFileChannel(interp, pathPtr, modeString, permissions) #ifdef DEPRECATED fsPtr->nextPtr = tsdPtr->firstFilePtr; tsdPtr->firstFilePtr = fsPtr; -#endif +#endif /* DEPRECATED */ fsPtr->validMask = channelPermissions | TCL_EXCEPTION; fsPtr->fd = fd; - + fsPtr->channel = Tcl_CreateChannel(channelTypePtr, channelName, (ClientData) fsPtr, channelPermissions); if (seekFlag) { - if (Tcl_Seek(fsPtr->channel, (Tcl_WideInt)0, + if (Tcl_Seek(fsPtr->channel, (Tcl_WideInt)0, SEEK_END) < (Tcl_WideInt)0) { - if (interp != (Tcl_Interp *) NULL) { - Tcl_AppendResult(interp, "couldn't seek to end of file on \"", - channelName, "\": ", Tcl_PosixError(interp), NULL); - } - Tcl_Close(NULL, fsPtr->channel); - return NULL; - } + if (interp != (Tcl_Interp *) NULL) { + Tcl_AppendResult(interp, "couldn't seek to end of file on \"", + channelName, "\": ", Tcl_PosixError(interp), NULL); + } + Tcl_Close(NULL, fsPtr->channel); + return NULL; + } } if (translation != NULL) { @@ -1820,7 +1808,7 @@ TclpOpenFileChannel(interp, pathPtr, modeString, permissions) * command. So, by default, newlines are translated to "\r\n" on * output to avoid "bug" reports that the serial port isn't working. */ - + if (Tcl_SetChannelOption(interp, fsPtr->channel, "-translation", translation) != TCL_OK) { Tcl_Close(NULL, fsPtr->channel); @@ -1851,7 +1839,7 @@ Tcl_Channel Tcl_MakeFileChannel(handle, mode) ClientData handle; /* OS level handle. */ int mode; /* ORed combination of TCL_READABLE and - * TCL_WRITABLE to indicate file mode. */ + * TCL_WRITABLE to indicate file mode. */ { FileState *fsPtr; char channelName[16 + TCL_INTEGER_SPACE]; @@ -1859,12 +1847,12 @@ Tcl_MakeFileChannel(handle, mode) Tcl_ChannelType *channelTypePtr; #ifdef DEPRECATED ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); -#endif +#endif /* DEPRECATED */ int socketType = 0; size_t argLength = sizeof(int); if (mode == 0) { - return NULL; + return NULL; } @@ -1880,7 +1868,7 @@ Tcl_MakeFileChannel(handle, mode) fsPtr->channel : NULL; } } -#endif +#endif /* DEPRECATED */ #ifdef SUPPORTS_TTY if (isatty(fd)) { @@ -1888,9 +1876,9 @@ Tcl_MakeFileChannel(handle, mode) channelTypePtr = &ttyChannelType; sprintf(channelName, "serial%d", fd); } else -#endif +#endif /* SUPPORTS_TTY */ if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (VOID *)&socketType, - &argLength) == 0 && socketType == SOCK_STREAM) { + &argLength) == 0 && socketType == SOCK_STREAM) { /* * The mode parameter gets lost here, unfortunately. */ @@ -1904,11 +1892,11 @@ Tcl_MakeFileChannel(handle, mode) #ifdef DEPRECATED fsPtr->nextPtr = tsdPtr->firstFilePtr; tsdPtr->firstFilePtr = fsPtr; -#endif +#endif /* DEPRECATED */ fsPtr->fd = fd; fsPtr->validMask = mode | TCL_EXCEPTION; fsPtr->channel = Tcl_CreateChannel(channelTypePtr, channelName, - (ClientData) fsPtr, mode); + (ClientData) fsPtr, mode); return fsPtr->channel; } @@ -1935,41 +1923,39 @@ static int TcpBlockModeProc(instanceData, mode) ClientData instanceData; /* Socket state. */ int mode; /* The mode to set. Can be one of - * TCL_MODE_BLOCKING or - * TCL_MODE_NONBLOCKING. */ + * TCL_MODE_BLOCKING or + * TCL_MODE_NONBLOCKING. */ { TcpState *statePtr = (TcpState *) instanceData; int setting; - -#ifndef USE_FIONBIO + +#ifndef USE_FIONBIO setting = fcntl(statePtr->fd, F_GETFL); if (mode == TCL_MODE_BLOCKING) { - statePtr->flags &= (~(TCP_ASYNC_SOCKET)); - setting &= (~(O_NONBLOCK)); + statePtr->flags &= (~(TCP_ASYNC_SOCKET)); + setting &= (~(O_NONBLOCK)); } else { - statePtr->flags |= TCP_ASYNC_SOCKET; - setting |= O_NONBLOCK; + statePtr->flags |= TCP_ASYNC_SOCKET; + setting |= O_NONBLOCK; } if (fcntl(statePtr->fd, F_SETFL, setting) < 0) { - return errno; + return errno; } -#endif - -#ifdef USE_FIONBIO +#else /* USE_FIONBIO */ if (mode == TCL_MODE_BLOCKING) { - statePtr->flags &= (~(TCP_ASYNC_SOCKET)); - setting = 0; - if (ioctl(statePtr->fd, (int) FIONBIO, &setting) == -1) { - return errno; - } + statePtr->flags &= (~(TCP_ASYNC_SOCKET)); + setting = 0; + if (ioctl(statePtr->fd, (int) FIONBIO, &setting) == -1) { + return errno; + } } else { - statePtr->flags |= TCP_ASYNC_SOCKET; - setting = 1; - if (ioctl(statePtr->fd, (int) FIONBIO, &setting) == -1) { - return errno; - } + statePtr->flags |= TCP_ASYNC_SOCKET; + setting = 1; + if (ioctl(statePtr->fd, (int) FIONBIO, &setting) == -1) { + return errno; + } } -#endif +#endif /* !USE_FIONBIO */ return 0; } @@ -2004,37 +1990,35 @@ WaitForConnect(statePtr, errorCodePtr) * If an asynchronous connect is in progress, attempt to wait for it * to complete before reading. */ - + if (statePtr->flags & TCP_ASYNC_CONNECT) { - if (statePtr->flags & TCP_ASYNC_SOCKET) { - timeOut = 0; - } else { - timeOut = -1; - } - errno = 0; - state = TclUnixWaitForFile(statePtr->fd, + if (statePtr->flags & TCP_ASYNC_SOCKET) { + timeOut = 0; + } else { + timeOut = -1; + } + errno = 0; + state = TclUnixWaitForFile(statePtr->fd, TCL_WRITABLE | TCL_EXCEPTION, timeOut); - if (!(statePtr->flags & TCP_ASYNC_SOCKET)) { -#ifndef USE_FIONBIO - flags = fcntl(statePtr->fd, F_GETFL); - flags &= (~(O_NONBLOCK)); - (void) fcntl(statePtr->fd, F_SETFL, flags); -#endif - -#ifdef USE_FIONBIO - flags = 0; - (void) ioctl(statePtr->fd, FIONBIO, &flags); -#endif - } - if (state & TCL_EXCEPTION) { - return -1; - } - if (state & TCL_WRITABLE) { - statePtr->flags &= (~(TCP_ASYNC_CONNECT)); - } else if (timeOut == 0) { - *errorCodePtr = errno = EWOULDBLOCK; - return -1; - } + if (!(statePtr->flags & TCP_ASYNC_SOCKET)) { +#ifndef USE_FIONBIO + flags = fcntl(statePtr->fd, F_GETFL); + flags &= (~(O_NONBLOCK)); + (void) fcntl(statePtr->fd, F_SETFL, flags); +#else /* USE_FIONBIO */ + flags = 0; + (void) ioctl(statePtr->fd, FIONBIO, &flags); +#endif /* !USE_FIONBIO */ + } + if (state & TCL_EXCEPTION) { + return -1; + } + if (state & TCL_WRITABLE) { + statePtr->flags &= (~(TCP_ASYNC_CONNECT)); + } else if (timeOut == 0) { + *errorCodePtr = errno = EWOULDBLOCK; + return -1; + } } return 0; } @@ -2067,7 +2051,7 @@ TcpInputProc(instanceData, buf, bufSize, errorCodePtr) ClientData instanceData; /* Socket state. */ char *buf; /* Where to store data read. */ int bufSize; /* How much space is available - * in the buffer? */ + * in the buffer? */ int *errorCodePtr; /* Where to store error code. */ { TcpState *statePtr = (TcpState *) instanceData; @@ -2076,19 +2060,18 @@ TcpInputProc(instanceData, buf, bufSize, errorCodePtr) *errorCodePtr = 0; state = WaitForConnect(statePtr, errorCodePtr); if (state != 0) { - return -1; + return -1; } bytesRead = recv(statePtr->fd, buf, (size_t) bufSize, 0); if (bytesRead > -1) { - return bytesRead; + return bytesRead; } if (errno == ECONNRESET) { + /* + * Turn ECONNRESET into a soft EOF condition. + */ - /* - * Turn ECONNRESET into a soft EOF condition. - */ - - return 0; + return 0; } *errorCodePtr = errno; return -1; @@ -2129,11 +2112,11 @@ TcpOutputProc(instanceData, buf, toWrite, errorCodePtr) *errorCodePtr = 0; state = WaitForConnect(statePtr, errorCodePtr); if (state != 0) { - return -1; + return -1; } written = send(statePtr->fd, buf, (size_t) toWrite, 0); if (written > -1) { - return written; + return written; } *errorCodePtr = errno; return -1; @@ -2197,7 +2180,7 @@ TcpCloseProc(instanceData, interp) * * Results: * A standard Tcl result. The value of the specified option or a - * list of all options and their values is returned in the + * list of all options and their values is returned in the * supplied DString. Sets Error message if needed. * * Side effects: @@ -2208,13 +2191,13 @@ TcpCloseProc(instanceData, interp) static int TcpGetOptionProc(instanceData, interp, optionName, dsPtr) - ClientData instanceData; /* Socket state. */ - Tcl_Interp *interp; /* For error reporting - can be NULL. */ + ClientData instanceData; /* Socket state. */ + Tcl_Interp *interp; /* For error reporting - can be NULL. */ CONST char *optionName; /* Name of the option to * retrieve the value for, or * NULL to get all options and * their values. */ - Tcl_DString *dsPtr; /* Where to store the computed + Tcl_DString *dsPtr; /* Where to store the computed * value; initialized by caller. */ { TcpState *statePtr = (TcpState *) instanceData; @@ -2226,14 +2209,14 @@ TcpGetOptionProc(instanceData, interp, optionName, dsPtr) char buf[TCL_INTEGER_SPACE]; if (optionName != (char *) NULL) { - len = strlen(optionName); + len = strlen(optionName); } if ((len > 1) && (optionName[1] == 'e') && (strncmp(optionName, "-error", len) == 0)) { size_t optlen = sizeof(int); int err, ret; - + ret = getsockopt(statePtr->fd, SOL_SOCKET, SO_ERROR, (char *)&err, &optlen); if (ret < 0) { @@ -2246,92 +2229,90 @@ TcpGetOptionProc(instanceData, interp, optionName, dsPtr) } if ((len == 0) || - ((len > 1) && (optionName[1] == 'p') && - (strncmp(optionName, "-peername", len) == 0))) { - if (getpeername(statePtr->fd, (struct sockaddr *) &peername, + ((len > 1) && (optionName[1] == 'p') && + (strncmp(optionName, "-peername", len) == 0))) { + if (getpeername(statePtr->fd, (struct sockaddr *) &peername, &size) >= 0) { - if (len == 0) { - Tcl_DStringAppendElement(dsPtr, "-peername"); - Tcl_DStringStartSublist(dsPtr); - } - Tcl_DStringAppendElement(dsPtr, inet_ntoa(peername.sin_addr)); - hostEntPtr = gethostbyaddr( /* INTL: Native. */ + if (len == 0) { + Tcl_DStringAppendElement(dsPtr, "-peername"); + Tcl_DStringStartSublist(dsPtr); + } + Tcl_DStringAppendElement(dsPtr, inet_ntoa(peername.sin_addr)); + hostEntPtr = gethostbyaddr( /* INTL: Native. */ (char *) &peername.sin_addr, sizeof(peername.sin_addr), AF_INET); - if (hostEntPtr != NULL) { + if (hostEntPtr != NULL) { Tcl_DString ds; Tcl_ExternalToUtfDString(NULL, hostEntPtr->h_name, -1, &ds); - Tcl_DStringAppendElement(dsPtr, Tcl_DStringValue(&ds)); - } else { - Tcl_DStringAppendElement(dsPtr, inet_ntoa(peername.sin_addr)); - } - TclFormatInt(buf, ntohs(peername.sin_port)); - Tcl_DStringAppendElement(dsPtr, buf); - if (len == 0) { - Tcl_DStringEndSublist(dsPtr); - } else { - return TCL_OK; - } - } else { - /* - * getpeername failed - but if we were asked for all the options - * (len==0), don't flag an error at that point because it could - * be an fconfigure request on a server socket. (which have - * no peer). same must be done on win&mac. - */ - - if (len) { - if (interp) { - Tcl_AppendResult(interp, "can't get peername: ", - Tcl_PosixError(interp), - (char *) NULL); - } - return TCL_ERROR; - } - } + Tcl_DStringAppendElement(dsPtr, Tcl_DStringValue(&ds)); + } else { + Tcl_DStringAppendElement(dsPtr, inet_ntoa(peername.sin_addr)); + } + TclFormatInt(buf, ntohs(peername.sin_port)); + Tcl_DStringAppendElement(dsPtr, buf); + if (len == 0) { + Tcl_DStringEndSublist(dsPtr); + } else { + return TCL_OK; + } + } else { + /* + * getpeername failed - but if we were asked for all the options + * (len==0), don't flag an error at that point because it could + * be an fconfigure request on a server socket. (which have + * no peer). same must be done on win&mac. + */ + + if (len) { + if (interp) { + Tcl_AppendResult(interp, "can't get peername: ", + Tcl_PosixError(interp), (char *) NULL); + } + return TCL_ERROR; + } + } } if ((len == 0) || - ((len > 1) && (optionName[1] == 's') && - (strncmp(optionName, "-sockname", len) == 0))) { - if (getsockname(statePtr->fd, (struct sockaddr *) &sockname, &size) - >= 0) { - if (len == 0) { - Tcl_DStringAppendElement(dsPtr, "-sockname"); - Tcl_DStringStartSublist(dsPtr); - } - Tcl_DStringAppendElement(dsPtr, inet_ntoa(sockname.sin_addr)); - hostEntPtr = gethostbyaddr( /* INTL: Native. */ + ((len > 1) && (optionName[1] == 's') && + (strncmp(optionName, "-sockname", len) == 0))) { + if (getsockname(statePtr->fd, (struct sockaddr *) &sockname, + &size) >= 0) { + if (len == 0) { + Tcl_DStringAppendElement(dsPtr, "-sockname"); + Tcl_DStringStartSublist(dsPtr); + } + Tcl_DStringAppendElement(dsPtr, inet_ntoa(sockname.sin_addr)); + hostEntPtr = gethostbyaddr( /* INTL: Native. */ (char *) &sockname.sin_addr, - sizeof(sockname.sin_addr), AF_INET); - if (hostEntPtr != (struct hostent *) NULL) { + sizeof(sockname.sin_addr), AF_INET); + if (hostEntPtr != (struct hostent *) NULL) { Tcl_DString ds; Tcl_ExternalToUtfDString(NULL, hostEntPtr->h_name, -1, &ds); - Tcl_DStringAppendElement(dsPtr, Tcl_DStringValue(&ds)); - } else { - Tcl_DStringAppendElement(dsPtr, inet_ntoa(sockname.sin_addr)); - } - TclFormatInt(buf, ntohs(sockname.sin_port)); - Tcl_DStringAppendElement(dsPtr, buf); - if (len == 0) { - Tcl_DStringEndSublist(dsPtr); - } else { - return TCL_OK; - } - } else { + Tcl_DStringAppendElement(dsPtr, Tcl_DStringValue(&ds)); + } else { + Tcl_DStringAppendElement(dsPtr, inet_ntoa(sockname.sin_addr)); + } + TclFormatInt(buf, ntohs(sockname.sin_port)); + Tcl_DStringAppendElement(dsPtr, buf); + if (len == 0) { + Tcl_DStringEndSublist(dsPtr); + } else { + return TCL_OK; + } + } else { if (interp) { Tcl_AppendResult(interp, "can't get sockname: ", - Tcl_PosixError(interp), - (char *) NULL); + Tcl_PosixError(interp), (char *) NULL); } return TCL_ERROR; } } if (len > 0) { - return Tcl_BadChannelOption(interp, optionName, "peername sockname"); + return Tcl_BadChannelOption(interp, optionName, "peername sockname"); } return TCL_OK; @@ -2358,8 +2339,8 @@ static void TcpWatchProc(instanceData, mask) ClientData instanceData; /* The socket state. */ int mask; /* Events of interest; an OR-ed - * combination of TCL_READABLE, - * TCL_WRITABLE and TCL_EXCEPTION. */ + * combination of TCL_READABLE, + * TCL_WRITABLE and TCL_EXCEPTION. */ { TcpState *statePtr = (TcpState *) instanceData; @@ -2440,8 +2421,8 @@ CreateSocket(interp, port, host, server, myaddr, myport, async) CONST char *myaddr; /* Optional client-side address */ int myport; /* Optional client-side port */ int async; /* If nonzero and creating a client socket, - * attempt to do an async connect. Otherwise - * do a synchronous connect or bind. */ + * attempt to do an async connect. Otherwise + * do a synchronous connect or bind. */ { int status, sock, asyncConnect, curState, origState; struct sockaddr_in sockaddr; /* socket address */ @@ -2469,7 +2450,7 @@ CreateSocket(interp, port, host, server, myaddr, myport, async) */ fcntl(sock, F_SETFD, FD_CLOEXEC); - + /* * Set kernel space buffering */ @@ -2479,17 +2460,16 @@ CreateSocket(interp, port, host, server, myaddr, myport, async) asyncConnect = 0; status = 0; if (server) { - /* * Set up to reuse server addresses automatically and bind to the * specified port. */ - + status = 1; (void) setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &status, sizeof(status)); status = bind(sock, (struct sockaddr *) &sockaddr, - sizeof(struct sockaddr)); + sizeof(struct sockaddr)); if (status != -1) { status = listen(sock, SOMAXCONN); } @@ -2497,7 +2477,7 @@ CreateSocket(interp, port, host, server, myaddr, myport, async) if (myaddr != NULL || myport != 0) { curState = 1; (void) setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, - (char *) &curState, sizeof(curState)); + (char *) &curState, sizeof(curState)); status = bind(sock, (struct sockaddr *) &mysockaddr, sizeof(struct sockaddr)); if (status < 0) { @@ -2512,28 +2492,26 @@ CreateSocket(interp, port, host, server, myaddr, myport, async) * being informed when the connect completes. */ - if (async) { -#ifndef USE_FIONBIO - origState = fcntl(sock, F_GETFL); - curState = origState | O_NONBLOCK; - status = fcntl(sock, F_SETFL, curState); -#endif - -#ifdef USE_FIONBIO - curState = 1; - status = ioctl(sock, FIONBIO, &curState); -#endif - } else { - status = 0; - } - if (status > -1) { - status = connect(sock, (struct sockaddr *) &sockaddr, - sizeof(sockaddr)); - if (status < 0) { - if (errno == EINPROGRESS) { - asyncConnect = 1; - status = 0; - } + if (async) { +#ifndef USE_FIONBIO + origState = fcntl(sock, F_GETFL); + curState = origState | O_NONBLOCK; + status = fcntl(sock, F_SETFL, curState); +#else /* USE_FIONBIO */ + curState = 1; + status = ioctl(sock, FIONBIO, &curState); +#endif /* !USE_FIONBIO */ + } else { + status = 0; + } + if (status > -1) { + status = connect(sock, (struct sockaddr *) &sockaddr, + sizeof(sockaddr)); + if (status < 0) { + if (errno == EINPROGRESS) { + asyncConnect = 1; + status = 0; + } } else { /* * Here we are if the connect succeeds. In case of an @@ -2547,27 +2525,25 @@ CreateSocket(interp, port, host, server, myaddr, myport, async) origState = fcntl(sock, F_GETFL); curState = origState & ~(O_NONBLOCK); status = fcntl(sock, F_SETFL, curState); -#endif - -#ifdef USE_FIONBIO +#else /* USE_FIONBIO */ curState = 0; status = ioctl(sock, FIONBIO, &curState); -#endif +#endif /* !USE_FIONBIO */ } } - } + } } bindError: if (status < 0) { - if (interp != NULL) { - Tcl_AppendResult(interp, "couldn't open socket: ", - Tcl_PosixError(interp), (char *) NULL); - } - if (sock != -1) { - close(sock); - } - return NULL; + if (interp != NULL) { + Tcl_AppendResult(interp, "couldn't open socket: ", + Tcl_PosixError(interp), (char *) NULL); + } + if (sock != -1) { + close(sock); + } + return NULL; } /* @@ -2577,7 +2553,7 @@ bindError: statePtr = (TcpState *) ckalloc((unsigned) sizeof(TcpState)); statePtr->flags = 0; if (asyncConnect) { - statePtr->flags = TCP_ASYNC_CONNECT; + statePtr->flags = TCP_ASYNC_CONNECT; } statePtr->fd = sock; @@ -2585,7 +2561,7 @@ bindError: addressError: if (sock != -1) { - close(sock); + close(sock); } if (interp != NULL) { Tcl_AppendResult(interp, "couldn't open socket: ", @@ -2634,36 +2610,36 @@ CreateSocketAddress(sockaddrPtr, host, port) } else { native = Tcl_UtfToExternalDString(NULL, host, -1, &ds); } - addr.s_addr = inet_addr(native); /* INTL: Native. */ + addr.s_addr = inet_addr(native); /* INTL: Native. */ /* * This is 0xFFFFFFFF to ensure that it compares as a 32bit -1 * on either 32 or 64 bits systems. */ - if (addr.s_addr == 0xFFFFFFFF) { - hostent = gethostbyname(native); /* INTL: Native. */ - if (hostent != NULL) { - memcpy((VOID *) &addr, - (VOID *) hostent->h_addr_list[0], - (size_t) hostent->h_length); - } else { + if (addr.s_addr == 0xFFFFFFFF) { + hostent = gethostbyname(native); /* INTL: Native. */ + if (hostent != NULL) { + memcpy((VOID *) &addr, + (VOID *) hostent->h_addr_list[0], + (size_t) hostent->h_length); + } else { #ifdef EHOSTUNREACH - errno = EHOSTUNREACH; -#else + errno = EHOSTUNREACH; +#else /* !EHOSTUNREACH */ #ifdef ENXIO - errno = ENXIO; -#endif -#endif + errno = ENXIO; +#endif /* ENXIO */ +#endif /* EHOSTUNREACH */ if (native != NULL) { Tcl_DStringFree(&ds); } - return 0; /* error */ - } - } + return 0; /* error */ + } + } if (native != NULL) { Tcl_DStringFree(&ds); } } - + /* * NOTE: On 64 bit machines the assignment below is rumored to not * do the right thing. Please report errors related to this if you @@ -2683,7 +2659,7 @@ CreateSocketAddress(sockaddrPtr, host, port) * Opens a TCP client socket and creates a channel around it. * * Results: - * The channel or NULL if failed. An error message is returned + * The channel or NULL if failed. An error message is returned * in the interpreter on failure. * * Side effects: @@ -2700,8 +2676,8 @@ Tcl_OpenTcpClient(interp, port, host, myaddr, myport, async) CONST char *myaddr; /* Client-side address */ int myport; /* Client-side port */ int async; /* If nonzero, attempt to do an - * asynchronous connect. Otherwise - * we do a blocking connect. */ + * asynchronous connect. Otherwise + * we do a blocking connect. */ { TcpState *statePtr; char channelName[16 + TCL_INTEGER_SPACE]; @@ -2721,11 +2697,11 @@ Tcl_OpenTcpClient(interp, port, host, myaddr, myport, async) sprintf(channelName, "sock%d", statePtr->fd); statePtr->channel = Tcl_CreateChannel(&tcpChannelType, channelName, - (ClientData) statePtr, (TCL_READABLE | TCL_WRITABLE)); + (ClientData) statePtr, (TCL_READABLE | TCL_WRITABLE)); if (Tcl_SetChannelOption(interp, statePtr->channel, "-translation", "auto crlf") == TCL_ERROR) { - Tcl_Close((Tcl_Interp *) NULL, statePtr->channel); - return NULL; + Tcl_Close((Tcl_Interp *) NULL, statePtr->channel); + return NULL; } return statePtr->channel; } @@ -2759,13 +2735,13 @@ Tcl_MakeTcpClientChannel(sock) statePtr->acceptProcData = (ClientData) NULL; sprintf(channelName, "sock%d", statePtr->fd); - + statePtr->channel = Tcl_CreateChannel(&tcpChannelType, channelName, - (ClientData) statePtr, (TCL_READABLE | TCL_WRITABLE)); + (ClientData) statePtr, (TCL_READABLE | TCL_WRITABLE)); if (Tcl_SetChannelOption((Tcl_Interp *) NULL, statePtr->channel, "-translation", "auto crlf") == TCL_ERROR) { - Tcl_Close((Tcl_Interp *) NULL, statePtr->channel); - return NULL; + Tcl_Close((Tcl_Interp *) NULL, statePtr->channel); + return NULL; } return statePtr->channel; } @@ -2791,11 +2767,11 @@ Tcl_MakeTcpClientChannel(sock) Tcl_Channel Tcl_OpenTcpServer(interp, port, myHost, acceptProc, acceptProcData) Tcl_Interp *interp; /* For error reporting - may be - * NULL. */ + * NULL. */ int port; /* Port number to open. */ CONST char *myHost; /* Name of local host. */ Tcl_TcpAcceptProc *acceptProc; /* Callback for accepting connections - * from new clients. */ + * from new clients. */ ClientData acceptProcData; /* Data for the callback. */ { TcpState *statePtr; @@ -2819,10 +2795,10 @@ Tcl_OpenTcpServer(interp, port, myHost, acceptProc, acceptProcData) */ Tcl_CreateFileHandler(statePtr->fd, TCL_READABLE, TcpAccept, - (ClientData) statePtr); + (ClientData) statePtr); sprintf(channelName, "sock%d", statePtr->fd); statePtr->channel = Tcl_CreateChannel(&tcpChannelType, channelName, - (ClientData) statePtr, 0); + (ClientData) statePtr, 0); return statePtr->channel; } @@ -2830,7 +2806,7 @@ Tcl_OpenTcpServer(interp, port, myHost, acceptProc, acceptProcData) *---------------------------------------------------------------------- * * TcpAccept -- - * Accept a TCP socket connection. This is called by the event loop. + * Accept a TCP socket connection. This is called by the event loop. * * Results: * None. @@ -2860,7 +2836,7 @@ TcpAccept(data, mask) len = sizeof(struct sockaddr_in); newsock = accept(sockState->fd, (struct sockaddr *) &addr, &len); if (newsock < 0) { - return; + return; } /* @@ -2869,14 +2845,14 @@ TcpAccept(data, mask) */ (void) fcntl(newsock, F_SETFD, FD_CLOEXEC); - + newSockState = (TcpState *) ckalloc((unsigned) sizeof(TcpState)); newSockState->flags = 0; newSockState->fd = newsock; newSockState->acceptProc = NULL; newSockState->acceptProcData = NULL; - + sprintf(channelName, "sock%d", newsock); newSockState->channel = Tcl_CreateChannel(&tcpChannelType, channelName, (ClientData) newSockState, (TCL_READABLE | TCL_WRITABLE)); @@ -2919,33 +2895,33 @@ TclpGetDefaultStdChannel(type) char *bufMode = NULL; switch (type) { - case TCL_STDIN: - if ((Tcl_PlatformSeek(0, (Tcl_SeekOffset) 0, + case TCL_STDIN: + if ((Tcl_PlatformSeek(0, (Tcl_SeekOffset) 0, SEEK_CUR) == (Tcl_SeekOffset)-1) && (errno == EBADF)) { - return (Tcl_Channel) NULL; - } + return (Tcl_Channel) NULL; + } fd = 0; mode = TCL_READABLE; - bufMode = "line"; - break; - case TCL_STDOUT: - if ((Tcl_PlatformSeek(1, (Tcl_SeekOffset) 0, + bufMode = "line"; + break; + case TCL_STDOUT: + if ((Tcl_PlatformSeek(1, (Tcl_SeekOffset) 0, SEEK_CUR) == (Tcl_SeekOffset)-1) && (errno == EBADF)) { - return (Tcl_Channel) NULL; - } + return (Tcl_Channel) NULL; + } fd = 1; mode = TCL_WRITABLE; - bufMode = "line"; - break; - case TCL_STDERR: - if ((Tcl_PlatformSeek(2, (Tcl_SeekOffset) 0, + bufMode = "line"; + break; + case TCL_STDERR: + if ((Tcl_PlatformSeek(2, (Tcl_SeekOffset) 0, SEEK_CUR) == (Tcl_SeekOffset)-1) && (errno == EBADF)) { - return (Tcl_Channel) NULL; - } + return (Tcl_Channel) NULL; + } fd = 2; mode = TCL_WRITABLE; bufMode = "none"; - break; + break; default: panic("TclGetDefaultStdChannel: Unexpected channel type"); break; @@ -3000,8 +2976,8 @@ Tcl_GetOpenFile(interp, string, forWriting, checkUsage, filePtr) int checkUsage; /* 1 means verify that the file was opened * in a mode that allows the access specified * by "forWriting". Ignored, we always - * check that the channel is open for the - * requested mode. */ + * check that the channel is open for the + * requested mode. */ ClientData *filePtr; /* Store pointer to FILE structure here. */ { Tcl_Channel chan; @@ -3010,19 +2986,19 @@ Tcl_GetOpenFile(interp, string, forWriting, checkUsage, filePtr) ClientData data; int fd; FILE *f; - + chan = Tcl_GetChannel(interp, string, &chanMode); if (chan == (Tcl_Channel) NULL) { - return TCL_ERROR; + return TCL_ERROR; } if ((forWriting) && ((chanMode & TCL_WRITABLE) == 0)) { - Tcl_AppendResult(interp, - "\"", string, "\" wasn't opened for writing", (char *) NULL); - return TCL_ERROR; + Tcl_AppendResult(interp, + "\"", string, "\" wasn't opened for writing", (char *) NULL); + return TCL_ERROR; } else if ((!(forWriting)) && ((chanMode & TCL_READABLE) == 0)) { - Tcl_AppendResult(interp, - "\"", string, "\" wasn't opened for reading", (char *) NULL); - return TCL_ERROR; + Tcl_AppendResult(interp, + "\"", string, "\" wasn't opened for reading", (char *) NULL); + return TCL_ERROR; } /* @@ -3035,10 +3011,10 @@ Tcl_GetOpenFile(interp, string, forWriting, checkUsage, filePtr) if ((chanTypePtr == &fileChannelType) #ifdef SUPPORTS_TTY || (chanTypePtr == &ttyChannelType) -#endif /* SUPPORTS_TTY */ +#endif /* SUPPORTS_TTY */ || (chanTypePtr == &tcpChannelType) || (strcmp(chanTypePtr->typeName, "pipe") == 0)) { - if (Tcl_GetChannelHandle(chan, + if (Tcl_GetChannelHandle(chan, (forWriting ? TCL_WRITABLE : TCL_READABLE), (ClientData*) &data) == TCL_OK) { fd = (int) data; @@ -3048,7 +3024,7 @@ Tcl_GetOpenFile(interp, string, forWriting, checkUsage, filePtr) * truncate an existing file if the file is being opened * for writing.... */ - + f = fdopen(fd, (forWriting ? "w" : "r")); if (f == NULL) { Tcl_AppendResult(interp, "cannot get a FILE * for \"", string, @@ -3061,8 +3037,8 @@ Tcl_GetOpenFile(interp, string, forWriting, checkUsage, filePtr) } Tcl_AppendResult(interp, "\"", string, - "\" cannot be used to get a FILE *", (char *) NULL); - return TCL_ERROR; + "\" cannot be used to get a FILE *", (char *) NULL); + return TCL_ERROR; } /* @@ -3141,7 +3117,7 @@ TclUnixWaitForFile(fd, mask, timeout) memset((VOID *) readyMasks, 0, 3*MASK_SIZE*sizeof(fd_mask)); index = fd/(NBBY*sizeof(fd_mask)); bit = 1 << (fd%(NBBY*sizeof(fd_mask))); - + /* * Loop in a mini-event loop of our own, waiting for either the * file to become ready or a timeout to occur. @@ -3160,7 +3136,7 @@ TclUnixWaitForFile(fd, mask, timeout) blockTime.tv_usec = 0; } } - + /* * Set the appropriate bit in the ready masks for the fd. */ |