diff options
Diffstat (limited to 'unix/tclUnixChan.c')
-rw-r--r-- | unix/tclUnixChan.c | 309 |
1 files changed, 160 insertions, 149 deletions
diff --git a/unix/tclUnixChan.c b/unix/tclUnixChan.c index 1bd8099..ab440de 100644 --- a/unix/tclUnixChan.c +++ b/unix/tclUnixChan.c @@ -10,7 +10,7 @@ * 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.78 2007/07/02 19:18:11 dgp Exp $ + * RCS: @(#) $Id: tclUnixChan.c,v 1.79 2007/07/30 13:42:24 dkf Exp $ */ #include "tclInt.h" /* Internal definitions for Tcl. */ @@ -128,6 +128,15 @@ #endif /* !USE_TERMIOS */ /* + * Helper macros to make parts of this file clearer. The macros do exactly + * what they say on the tin. :-) They also only ever refer to their arguments + * once, and so can be used without regard to side effects. + */ + +#define SET_BITS(var, bits) ((var) |= (bits)) +#define CLEAR_BITS(var, bits) ((var) &= ~(bits)) + +/* * This structure describes per-instance state of a file based channel. */ @@ -224,9 +233,9 @@ typedef struct TcpState { * Static routines for this file: */ -static TcpState * CreateSocket(Tcl_Interp *interp, - int port, const char *host, int server, - const char *myaddr, int myport, int async); +static TcpState * CreateSocket(Tcl_Interp *interp, int port, + const char *host, int server, const char *myaddr, + int myport, int async); static int CreateSocketAddress(struct sockaddr_in *sockaddrPtr, const char *host, int port); static int FileBlockModeProc(ClientData instanceData, int mode); @@ -234,12 +243,12 @@ static int FileCloseProc(ClientData instanceData, Tcl_Interp *interp); static int FileGetHandleProc(ClientData instanceData, int direction, ClientData *handlePtr); -static int FileInputProc(ClientData instanceData, - char *buf, int toRead, int *errorCode); +static int FileInputProc(ClientData instanceData, char *buf, + int toRead, int *errorCode); static int FileOutputProc(ClientData instanceData, const char *buf, int toWrite, int *errorCode); -static int FileSeekProc(ClientData instanceData, - long offset, int mode, int *errorCode); +static int FileSeekProc(ClientData instanceData, long offset, + int mode, int *errorCode); static int FileTruncateProc(ClientData instanceData, Tcl_WideInt length); static Tcl_WideInt FileWideSeekProc(ClientData instanceData, @@ -254,8 +263,8 @@ static int TcpGetHandleProc(ClientData instanceData, static int TcpGetOptionProc(ClientData instanceData, Tcl_Interp *interp, const char *optionName, Tcl_DString *dsPtr); -static int TcpInputProc(ClientData instanceData, - char *buf, int toRead, int *errorCode); +static int TcpInputProc(ClientData instanceData, char *buf, + int toRead, int *errorCode); static int TcpOutputProc(ClientData instanceData, const char *buf, int toWrite, int *errorCode); static void TcpWatchProc(ClientData instanceData, int mask); @@ -277,8 +286,8 @@ static int TtyOutputProc(ClientData instanceData, const char *buf, int toWrite, int *errorCode); #endif /* BAD_TIP35_FLUSH */ static int TtyParseMode(Tcl_Interp *interp, const char *mode, - int *speedPtr, int *parityPtr, - int *dataPtr, int *stopPtr); + int *speedPtr, int *parityPtr, int *dataPtr, + int *stopPtr); static void TtySetAttributes(int fd, TtyAttrs *ttyPtr); static int TtySetOptionProc(ClientData instanceData, Tcl_Interp *interp, const char *optionName, @@ -399,9 +408,9 @@ FileBlockModeProc( #ifndef USE_FIONBIO curStatus = fcntl(fsPtr->fd, F_GETFL); if (mode == TCL_MODE_BLOCKING) { - curStatus &= (~(O_NONBLOCK)); + CLEAR_BITS(curStatus, O_NONBLOCK); } else { - curStatus |= O_NONBLOCK; + SET_BITS(curStatus, O_NONBLOCK); } if (fcntl(fsPtr->fd, F_SETFL, curStatus) < 0) { return errno; @@ -592,7 +601,7 @@ FileSeekProc( oldLoc = TclOSseek(fsPtr->fd, (Tcl_SeekOffset) 0, SEEK_CUR); if (oldLoc == Tcl_LongAsWide(-1)) { /* - * Bad things are happening. Error out... + * Bad things are happening. Error out... */ *errorCodePtr = errno; @@ -679,12 +688,12 @@ FileWatchProc( FileState *fsPtr = (FileState *) instanceData; /* - * Make sure we only register for events that are valid on this file. - * Note that we are passing Tcl_NotifyChannel directly to - * Tcl_CreateFileHandler with the channel pointer as the client data. + * Make sure we only register for events that are valid on this file. Note + * that we are passing Tcl_NotifyChannel directly to Tcl_CreateFileHandler + * with the channel pointer as the client data. */ - mask &= fsPtr->validMask; + CLEAR_BITS(mask, fsPtr->validMask); if (mask) { Tcl_CreateFileHandler(fsPtr->fd, mask, (Tcl_FileProc *) Tcl_NotifyChannel, @@ -716,16 +725,15 @@ static int FileGetHandleProc( ClientData instanceData, /* The file state. */ int direction, /* TCL_READABLE or TCL_WRITABLE */ - ClientData *handlePtr) /* Where to store the handle. */ + ClientData *handlePtr) /* Where to store the handle. */ { FileState *fsPtr = (FileState *) instanceData; if (direction & fsPtr->validMask) { *handlePtr = (ClientData) INT2PTR(fsPtr->fd); return TCL_OK; - } else { - return TCL_ERROR; } + return TCL_ERROR; } #ifdef SUPPORTS_TTY @@ -806,14 +814,14 @@ TtyOutputProc( { if (TclInExit()) { /* - * Do not write data during Tcl exit. Serial port may block - * preventing Tcl from exit. + * Do not write data during Tcl exit. Serial port may block preventing + * Tcl from exit. */ return toWrite; - } else { - return FileOutputProc(instanceData, buf, toWrite, errorCodePtr); } + + return FileOutputProc(instanceData, buf, toWrite, errorCodePtr); } #endif /* BAD_TIP35_FLUSH */ @@ -830,8 +838,8 @@ TtyOutputProc( static void TtyModemStatusStr( - 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"); @@ -919,17 +927,17 @@ TtySetOptionProc( */ GETIOSTATE(fsPtr->fd, &iostate); - iostate.c_iflag &= ~(IXON | IXOFF | IXANY); + CLEAR_BITS(iostate.c_iflag, IXON | IXOFF | IXANY); #ifdef CRTSCTS - iostate.c_cflag &= ~CRTSCTS; + CLEAR_BITS(iostate.c_cflag, CRTSCTS); #endif /* CRTSCTS */ if (strncasecmp(value, "NONE", vlen) == 0) { /* leave all handshake options disabled */ } else if (strncasecmp(value, "XONXOFF", vlen) == 0) { - iostate.c_iflag |= (IXON | IXOFF | IXANY); + SET_BITS(iostate.c_iflag, IXON | IXOFF | IXANY); } else if (strncasecmp(value, "RTSCTS", vlen) == 0) { #ifdef CRTSCTS - iostate.c_cflag |= CRTSCTS; + SET_BITS(iostate.c_cflag, CRTSCTS); #else /* !CRTSTS */ UNSUPPORTED_OPTION("-handshake RTSCTS"); return TCL_ERROR; @@ -963,9 +971,8 @@ TtySetOptionProc( iostate.c_cc[VSTOP] = argv[1][0]; } else { if (interp) { - Tcl_AppendResult(interp, - "bad value for -xchar: should be a list of two elements", - NULL); + Tcl_AppendResult(interp, "bad value for -xchar: " + "should be a list of two elements", NULL); } ckfree((char *) argv); return TCL_ERROR; @@ -986,8 +993,8 @@ TtySetOptionProc( if (Tcl_GetInt(interp, value, &msec) != TCL_OK) { return TCL_ERROR; } - iostate.c_cc[VMIN] = 0; - iostate.c_cc[VTIME] = (msec == 0) ? 0 : (msec < 100) ? 1 : (msec+50)/100; + iostate.c_cc[VMIN] = 0; + iostate.c_cc[VTIME] = (msec==0) ? 0 : (msec<100) ? 1 : (msec+50)/100; SETIOSTATE(fsPtr->fd, &iostate); return TCL_OK; } @@ -998,14 +1005,14 @@ TtySetOptionProc( if ((len > 4) && (strncmp(optionName, "-ttycontrol", len) == 0)) { int i; + 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", NULL); + Tcl_AppendResult(interp, "bad value for -ttycontrol: " + "should be a list of signal,value pairs", NULL); } ckfree((char *) argv); return TCL_ERROR; @@ -1020,9 +1027,9 @@ TtySetOptionProc( if (strncasecmp(argv[i], "DTR", strlen(argv[i])) == 0) { #ifdef TIOCM_DTR if (flag) { - control |= TIOCM_DTR; + SET_BITS(control, TIOCM_DTR); } else { - control &= ~TIOCM_DTR; + CLEAR_BITS(control, TIOCM_DTR); } #else /* !TIOCM_DTR */ UNSUPPORTED_OPTION("-ttycontrol DTR"); @@ -1032,9 +1039,9 @@ TtySetOptionProc( } else if (strncasecmp(argv[i], "RTS", strlen(argv[i])) == 0) { #ifdef TIOCM_RTS if (flag) { - control |= TIOCM_RTS; + SET_BITS(control, TIOCM_RTS); } else { - control &= ~TIOCM_RTS; + CLEAR_BITS(control, TIOCM_RTS); } #else /* !TIOCM_RTS*/ UNSUPPORTED_OPTION("-ttycontrol RTS"); @@ -1066,7 +1073,7 @@ TtySetOptionProc( } return Tcl_BadChannelOption(interp, optionName, - "mode handshake timeout ttycontrol xchar "); + "mode handshake timeout ttycontrol xchar"); #else /* !USE_TERMIOS */ return Tcl_BadChannelOption(interp, optionName, "mode"); @@ -1079,7 +1086,7 @@ TtySetOptionProc( * TtyGetOptionProc -- * * Gets a mode associated with an IO channel. If the optionName arg is - * non NULL, retrieves the value of that option. If the optionName arg is + * non-NULL, retrieves the value of that option. If the optionName arg is * NULL, retrieves a list of alternating option names and values for the * given channel. * @@ -1089,7 +1096,7 @@ TtySetOptionProc( * * 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 + * reused at any time subsequent to the call. Sets error message if * needed (by calling Tcl_BadChannelOption). * *---------------------------------------------------------------------- @@ -1104,9 +1111,8 @@ TtyGetOptionProc( { FileState *fsPtr = (FileState *) instanceData; unsigned int len; - char buf[3 * TCL_INTEGER_SPACE + 16]; - TtyAttrs tty; - int valid = 0; /* flag if valid option parsed */ + char buf[3*TCL_INTEGER_SPACE + 16]; + int valid = 0; /* Flag if valid option parsed. */ if (optionName == NULL) { len = 0; @@ -1117,6 +1123,8 @@ TtyGetOptionProc( Tcl_DStringAppendElement(dsPtr, "-mode"); } if (len==0 || (len>2 && strncmp(optionName, "-mode", len)==0)) { + TtyAttrs tty; + valid = 1; TtyGetAttributes(fsPtr->fd, &tty); sprintf(buf, "%d,%c,%d,%d", tty.baud, tty.parity, tty.data, tty.stop); @@ -1125,7 +1133,7 @@ TtyGetOptionProc( #ifdef USE_TERMIOS /* - * get option -xchar + * Get option -xchar */ if (len == 0) { @@ -1134,8 +1142,8 @@ TtyGetOptionProc( } if (len==0 || (len>1 && strncmp(optionName, "-xchar", len)==0)) { IOSTATE iostate; - valid = 1; + valid = 1; GETIOSTATE(fsPtr->fd, &iostate); sprintf(buf, "%c", iostate.c_cc[VSTART]); Tcl_DStringAppendElement(dsPtr, buf); @@ -1147,14 +1155,14 @@ TtyGetOptionProc( } /* - * get option -queue - * option is readonly and returned by [fconfigure chan -queue] but not - * returned by unnamed [fconfigure chan] + * Get option -queue + * Option is readonly and returned by [fconfigure chan -queue] but not + * returned by unnamed [fconfigure chan]. */ if ((len > 1) && (strncmp(optionName, "-queue", len) == 0)) { - int inQueue=0, outQueue=0; - int inBuffered, outBuffered; + int inQueue=0, outQueue=0, inBuffered, outBuffered; + valid = 1; #ifdef GETREADQUEUE GETREADQUEUE(fsPtr->fd, inQueue); @@ -1162,7 +1170,7 @@ TtyGetOptionProc( #ifdef GETWRITEQUEUE GETWRITEQUEUE(fsPtr->fd, outQueue); #endif /* GETWRITEQUEUE */ - inBuffered = Tcl_InputBuffered(fsPtr->channel); + inBuffered = Tcl_InputBuffered(fsPtr->channel); outBuffered = Tcl_OutputBuffered(fsPtr->channel); sprintf(buf, "%d", inBuffered+inQueue); @@ -1172,12 +1180,13 @@ TtyGetOptionProc( } /* - * get option -ttystatus - * option is readonly and returned by [fconfigure chan -ttystatus] but not - * returned by unnamed [fconfigure chan] + * Get option -ttystatus + * Option is readonly and returned by [fconfigure chan -ttystatus] but not + * returned by unnamed [fconfigure chan]. */ if ((len > 4) && (strncmp(optionName, "-ttystatus", len) == 0)) { int status; + valid = 1; GETCONTROL(fsPtr->fd, &status); TtyModemStatusStr(status, dsPtr); @@ -1186,20 +1195,17 @@ TtyGetOptionProc( if (valid) { return TCL_OK; - } else { - return Tcl_BadChannelOption(interp, optionName, + } + return Tcl_BadChannelOption(interp, optionName, "mode" #ifdef USE_TERMIOS - "mode queue ttystatus xchar" -#else /* !USE_TERMIOS */ - "mode" + " queue ttystatus xchar" #endif /* USE_TERMIOS */ ); - } } #ifdef DIRECT_BAUD -# define TtyGetSpeed(baud) ((unsigned) (baud)) -# define TtyGetBaud(speed) ((int) (speed)) +# define TtyGetSpeed(baud) ((unsigned) (baud)) +# define TtyGetBaud(speed) ((int) (speed)) #else /* !DIRECT_BAUD */ static struct {int baud; unsigned long speed;} speeds[] = { @@ -1348,7 +1354,7 @@ TtyGetSpeed( * get the baus rate that corresponds to that mask value. * * Results: - * As above. If the mask value was not recognized, 0 is returned. + * As above. If the mask value was not recognized, 0 is returned. * * Side effects: * None. @@ -1496,25 +1502,28 @@ TtySetAttributes( flag = 0; parity = ttyPtr->parity; if (parity != 'n') { - flag |= PARENB; + SET_BITS(flag, PARENB); #ifdef PAREXT - iostate.c_cflag &= ~PAREXT; + CLEAR_BITS(iostate.c_cflag, PAREXT); if ((parity == 'm') || (parity == 's')) { - flag |= PAREXT; + SET_BITS(flag, PAREXT); } #endif /* PAREXT */ if ((parity == 'm') || (parity == 'o')) { - flag |= PARODD; + SET_BITS(flag, PARODD); } } data = ttyPtr->data; - flag |= (data == 5) ? CS5 : (data == 6) ? CS6 : (data == 7) ? CS7 : CS8; + SET_BITS(flag, + (data == 5) ? CS5 : + (data == 6) ? CS6 : + (data == 7) ? CS7 : CS8); if (ttyPtr->stop == 2) { - flag |= CSTOPB; + SET_BITS(flag, CSTOPB); } - iostate.c_cflag &= ~(PARENB | PARODD | CSIZE | CSTOPB); - iostate.c_cflag |= flag; + CLEAR_BITS(iostate.c_cflag, PARENB | PARODD | CSIZE | CSTOPB); + SET_BITS(iostate.c_cflag, flag); #endif /* USE_TERMIOS */ @@ -1522,28 +1531,31 @@ TtySetAttributes( int parity, data, flag; GETIOSTATE(fd, &iostate); - iostate.c_cflag &= ~CBAUD; - iostate.c_cflag |= TtyGetSpeed(ttyPtr->baud); + CLEAR_BITS(iostate.c_cflag, CBAUD); + SET_BITS(iostate.c_cflag, TtyGetSpeed(ttyPtr->baud)); flag = 0; parity = ttyPtr->parity; if (parity != 'n') { - flag |= PARENB; + SET_BITS(flag, PARENB); if ((parity == 'm') || (parity == 's')) { - flag |= PAREXT; + SET_BITS(flag, PAREXT); } if ((parity == 'm') || (parity == 'o')) { - flag |= PARODD; + SET_BITS(flag, PARODD); } } data = ttyPtr->data; - flag |= (data == 5) ? CS5 : (data == 6) ? CS6 : (data == 7) ? CS7 : CS8; + SET_BITS(flag, + (data == 5) ? CS5 : + (data == 6) ? CS6 : + (data == 7) ? CS7 : CS8); if (ttyPtr->stop == 2) { - flag |= CSTOPB; + SET_BITS(flag, CSTOPB); } - iostate.c_cflag &= ~(PARENB | PARODD | PAREXT | CSIZE | CSTOPB); - iostate.c_cflag |= flag; + CLEAR_BITS(iostate.c_cflag, PARENB | PARODD | PAREXT | CSIZE | CSTOPB); + SET_BITS(iostate.c_cflag, flag); #endif /* USE_TERMIO */ @@ -1556,11 +1568,11 @@ TtySetAttributes( parity = ttyPtr->parity; if (parity == 'e') { - iostate.sg_flags &= ~ODDP; - iostate.sg_flags |= EVENP; + CLEAR_BITS(iostate.sg_flags, ODDP); + SET_BITS(iostate.sg_flags, EVENP); } else if (parity == 'o') { - iostate.sg_flags &= ~EVENP; - iostate.sg_flags |= ODDP; + CLEAR_BITS(iostate.sg_flags, EVENP); + SET_BITS(iostate.sg_flags, ODDP); } #endif /* USE_SGTTY */ @@ -1597,7 +1609,7 @@ TtyParseMode( { int i, end; char parity; - static char *bad = "bad value for -mode"; + static const char *bad = "bad value for -mode"; i = sscanf(mode, "%d,%c,%d,%d%n", speedPtr, &parity, dataPtr, stopPtr, &end); @@ -1611,7 +1623,7 @@ TtyParseMode( /* * Only allow setting mark/space parity on platforms that support it Make - * sure to allow for the case where strchr is a macro. [Bug: 5089] + * sure to allow for the case where strchr is a macro. [Bug: 5089] */ #if defined(PAREXT) || defined(USE_TERMIO) @@ -1654,9 +1666,9 @@ TtyParseMode( * * Given file descriptor that refers to a serial port, initialize the * serial port to a set of sane values so that Tcl can talk to a device - * located on the serial port. Note that no initialization happens if - * the initialize flag is not set; this is necessary for the correct - * handling of UNIX console TTYs at startup. + * located on the serial port. Note that no initialization happens if the + * initialize flag is not set; this is necessary for the correct handling + * of UNIX console TTYs at startup. * * Results: * A pointer to a FileState suitable for use with Tcl_CreateChannel and @@ -1664,7 +1676,7 @@ TtyParseMode( * * Side effects: * Serial device initialized to non-blocking raw mode, similar to sockets - * (if initialize flag is non-zero.) All other modes can be simulated on + * (if initialize flag is non-zero.) All other modes can be simulated on * top of this in Tcl. * *--------------------------------------------------------------------------- @@ -1696,7 +1708,7 @@ TtyInit( iostate.c_iflag = IGNBRK; iostate.c_oflag = 0; iostate.c_lflag = 0; - iostate.c_cflag |= CREAD; + SET_BITS(iostate.c_cflag, CREAD); iostate.c_cc[VMIN] = 1; iostate.c_cc[VTIME] = 0; #endif /* USE_TERMIOS|USE_TERMIO */ @@ -1706,8 +1718,8 @@ TtyInit( !(iostate.sg_flags & RAW)) { ttyPtr->stateUpdated = 1; } - iostate.sg_flags &= (EVENP | ODDP); - iostate.sg_flags |= RAW; + CLEAR_BITS(iostate.sg_flags, EVENP | ODDP); + SET_BITS(iostate.sg_flags, RAW); #endif /* USE_SGTTY */ /* @@ -1782,7 +1794,7 @@ TclpOpenFileChannel( } #ifdef DJGPP - mode |= O_BINARY; + SET_BITS(mode, O_BINARY); #endif fd = TclOSopen(native, mode, permissions); @@ -1947,24 +1959,24 @@ TcpBlockModeProc( #ifndef USE_FIONBIO setting = fcntl(statePtr->fd, F_GETFL); if (mode == TCL_MODE_BLOCKING) { - statePtr->flags &= (~(TCP_ASYNC_SOCKET)); - setting &= (~(O_NONBLOCK)); + CLEAR_BITS(statePtr->flags, TCP_ASYNC_SOCKET); + CLEAR_BITS(setting, O_NONBLOCK); } else { - statePtr->flags |= TCP_ASYNC_SOCKET; - setting |= O_NONBLOCK; + SET_BITS(statePtr->flags, TCP_ASYNC_SOCKET); + SET_BITS(setting, O_NONBLOCK); } if (fcntl(statePtr->fd, F_SETFL, setting) < 0) { return errno; } #else /* USE_FIONBIO */ if (mode == TCL_MODE_BLOCKING) { - statePtr->flags &= (~(TCP_ASYNC_SOCKET)); + CLEAR_BITS(statePtr->flags, TCP_ASYNC_SOCKET); setting = 0; if (ioctl(statePtr->fd, (int) FIONBIO, &setting) == -1) { return errno; } } else { - statePtr->flags |= TCP_ASYNC_SOCKET; + SET_BITS(statePtr->flags, TCP_ASYNC_SOCKET); setting = 1; if (ioctl(statePtr->fd, (int) FIONBIO, &setting) == -1) { return errno; @@ -2018,7 +2030,7 @@ WaitForConnect( if (!(statePtr->flags & TCP_ASYNC_SOCKET)) { #ifndef USE_FIONBIO flags = fcntl(statePtr->fd, F_GETFL); - flags &= (~(O_NONBLOCK)); + CLEAR_BITS(flags, O_NONBLOCK); (void) fcntl(statePtr->fd, F_SETFL, flags); #else /* USE_FIONBIO */ flags = 0; @@ -2029,7 +2041,7 @@ WaitForConnect( return -1; } if (state & TCL_WRITABLE) { - statePtr->flags &= (~(TCP_ASYNC_CONNECT)); + CLEAR_BITS(statePtr->flags, TCP_ASYNC_CONNECT); } else if (timeOut == 0) { *errorCodePtr = errno = EWOULDBLOCK; return -1; @@ -2205,13 +2217,13 @@ TcpCloseProc( static int TcpGetOptionProc( - 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 value; - * initialized by caller. */ + 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 value; + * initialized by caller. */ { TcpState *statePtr = (TcpState *) instanceData; struct sockaddr_in sockname; @@ -2360,9 +2372,9 @@ TcpWatchProc( TcpState *statePtr = (TcpState *) instanceData; /* - * Make sure we don't mess with server sockets since they will never - * be readable or writable at the Tcl level. This keeps Tcl scripts - * from interfering with the -accept behavior. + * Make sure we don't mess with server sockets since they will never be + * readable or writable at the Tcl level. This keeps Tcl scripts from + * interfering with the -accept behavior. */ if (!statePtr->acceptProc) { @@ -2399,7 +2411,7 @@ static int TcpGetHandleProc( ClientData instanceData, /* The socket state. */ int direction, /* Not used. */ - ClientData *handlePtr) /* Where to store the handle. */ + ClientData *handlePtr) /* Where to store the handle. */ { TcpState *statePtr = (TcpState *) instanceData; @@ -2446,11 +2458,11 @@ CreateSocket( sock = -1; origState = 0; - if (! CreateSocketAddress(&sockaddr, host, port)) { + if (!CreateSocketAddress(&sockaddr, host, port)) { goto addressError; } if ((myaddr != NULL || myport != 0) && - ! CreateSocketAddress(&mysockaddr, myaddr, myport)) { + !CreateSocketAddress(&mysockaddr, myaddr, myport)) { goto addressError; } @@ -2509,8 +2521,8 @@ CreateSocket( if (async) { #ifndef USE_FIONBIO - origState = fcntl(sock, F_GETFL); - curState = origState | O_NONBLOCK; + curState = fcntl(sock, F_GETFL); + SET_BITS(curState, O_NONBLOCK); status = fcntl(sock, F_SETFL, curState); #else /* USE_FIONBIO */ curState = 1; @@ -2531,15 +2543,15 @@ CreateSocket( /* * Here we are if the connect succeeds. In case of an * asynchronous connect we have to reset the channel to - * blocking mode. This appears to happen not very often, but + * blocking mode. This appears to happen not very often, but * e.g. on a HP 9000/800 under HP-UX B.11.00 we enter this * stage. [Bug: 4388] */ if (async) { #ifndef USE_FIONBIO - origState = fcntl(sock, F_GETFL); - curState = origState & ~(O_NONBLOCK); + curState = fcntl(sock, F_GETFL); + CLEAR_BITS(curState, O_NONBLOCK); status = fcntl(sock, F_SETFL, curState); #else /* USE_FIONBIO */ curState = 0; @@ -2612,7 +2624,7 @@ CreateSocketAddress( struct hostent *hostent; /* Host database entry */ struct in_addr addr; /* For 64/32 bit madness */ - (void) memset((void *) sockaddrPtr, '\0', sizeof(struct sockaddr_in)); + (void) memset(sockaddrPtr, '\0', sizeof(struct sockaddr_in)); sockaddrPtr->sin_family = AF_INET; sockaddrPtr->sin_port = htons((unsigned short) (port & 0xFFFF)); if (host == NULL) { @@ -2627,17 +2639,15 @@ CreateSocketAddress( native = Tcl_UtfToExternalDString(NULL, host, -1, &ds); } 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. + * 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 (hostent == NULL) { #ifdef EHOSTUNREACH errno = EHOSTUNREACH; #else /* !EHOSTUNREACH */ @@ -2648,8 +2658,11 @@ CreateSocketAddress( if (native != NULL) { Tcl_DStringFree(&ds); } - return 0; /* error */ + return 0; /* Error. */ } + + memcpy(&addr, (void *) hostent->h_addr_list[0], + (size_t) hostent->h_length); } if (native != NULL) { Tcl_DStringFree(&ds); @@ -2659,12 +2672,12 @@ CreateSocketAddress( /* * NOTE: On 64 bit machines the assignment below is rumored to not do the * right thing. Please report errors related to this if you observe - * incorrect behavior on 64 bit machines such as DEC Alphas. Should we + * incorrect behavior on 64 bit machines such as DEC Alphas. Should we * modify this code to do an explicit memcpy? */ sockaddrPtr->sin_addr.s_addr = addr.s_addr; - return 1; /* Success. */ + return 1; /* Success. */ } /* @@ -3032,10 +3045,9 @@ Tcl_GetOpenFile( ClientData *filePtr) /* Store pointer to FILE structure here. */ { Tcl_Channel chan; - int chanMode; + int chanMode, fd; const Tcl_ChannelType *chanTypePtr; ClientData data; - int fd; FILE *f; chan = Tcl_GetChannel(interp, chanID, &chanMode); @@ -3106,7 +3118,7 @@ Tcl_GetOpenFile( * present on file at the time of the return. This function will not * return until either "timeout" milliseconds have elapsed or at least * one of the conditions given by mask has occurred for file (a return - * value of 0 means that a timeout occurred). No normal events will be + * value of 0 means that a timeout occurred). No normal events will be * serviced during the execution of this function. * * Side effects: @@ -3166,9 +3178,9 @@ TclUnixWaitForFile( Tcl_Panic("TclWaitForFile can't handle file id %d", fd); /* must never get here, or readyMasks overrun will occur below */ } - memset((void *) readyMasks, 0, 3*MASK_SIZE*sizeof(fd_mask)); - index = fd/(NBBY*sizeof(fd_mask)); - bit = ((fd_mask)1) << (fd%(NBBY*sizeof(fd_mask))); + memset(readyMasks, 0, 3*MASK_SIZE*sizeof(fd_mask)); + index = fd / (NBBY*sizeof(fd_mask)); + bit = ((fd_mask)1) << (fd % (NBBY*sizeof(fd_mask))); /* * Loop in a mini-event loop of our own, waiting for either the file to @@ -3219,13 +3231,13 @@ TclUnixWaitForFile( (SELECT_MASK *) maskp[2], timeoutPtr); if (numFound == 1) { if (readyMasks[index] & bit) { - result |= TCL_READABLE; + SET_BITS(result, TCL_READABLE); } if ((readyMasks+MASK_SIZE)[index] & bit) { - result |= TCL_WRITABLE; + SET_BITS(result, TCL_WRITABLE); } if ((readyMasks+2*(MASK_SIZE))[index] & bit) { - result |= TCL_EXCEPTION; + SET_BITS(result, TCL_EXCEPTION); } result &= mask; if (result) { @@ -3245,8 +3257,7 @@ TclUnixWaitForFile( Tcl_GetTime(&now); if ((abortTime.sec < now.sec) - || ((abortTime.sec == now.sec) - && (abortTime.usec <= now.usec))) { + || (abortTime.sec==now.sec && abortTime.usec<=now.usec)) { break; } } |