diff options
Diffstat (limited to 'fd.c')
-rw-r--r-- | fd.c | 166 |
1 files changed, 89 insertions, 77 deletions
@@ -23,10 +23,44 @@ * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * PERFORMANCE OF THIS SOFTWARE. * * + * * + * Copyright 2002 Sun Microsystems, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, and/or sell copies of the Software, and to permit persons + * to whom the Software is furnished to do so, provided that the above + * copyright notice(s) and this permission notice appear in all copies of + * the Software and that both the above copyright notice(s) and this + * permission notice appear in supporting documentation. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT + * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * Except as contained in this notice, the name of a copyright holder + * shall not be used in advertising or otherwise to promote the sale, use + * or other dealings in this Software without prior written authorization + * of the copyright holder. + * \* *********************************************************** */ #include "scope.h" +#ifdef SYSV +#include <unistd.h> +#define getdtablesize() sysconf(_SC_OPEN_MAX) +#define bzero(s,l) memset(s, 0, l) +#define bcopy(s,d,l) memmove(d,s,l) +#endif /* All of this code is to support the handling of file descriptors (FD). @@ -42,6 +76,7 @@ /* */ /* ************************************************************ */ +void InitializeFD() { register short i; @@ -49,23 +84,25 @@ InitializeFD() enterprocedure("InitializeFD"); /* get the number of file descriptors the system will let us use */ MaxFD = getdtablesize(); - if (MaxFD > StaticMaxFD) - { - fprintf(stderr, "Recompile with larger StaticMaxFD value %d\n", MaxFD); - MaxFD = StaticMaxFD; - } + if (MaxFD > FD_SETSIZE) { + MaxFD = FD_SETSIZE; + } /* allocate space for a File Descriptor (FD) Table */ FDD = (struct FDDescriptor *) Malloc ((long)(MaxFD * sizeof (struct FDDescriptor))); + if (FDD == NULL) { + panic("Can't allocate memory!"); + } + bzero(FDD, (MaxFD * sizeof (struct FDDescriptor))); /* be sure all fd's are closed and marked not busy */ for (i = 0; i < MaxFD; i++) { /* 0, 1, 2 are special (stdin, stdout, stderr) */ if (i > 2) - (void)close(i); - FDD[i].Busy = false; + close(i); + /* FDD[i].Busy = false; - not needed since false==0*/ } /* save one FD for single file input or output like debugging */ @@ -76,16 +113,18 @@ InitializeFD() FD_ZERO(&ReadDescriptors); HighestFD = 0; - UsingFD(fileno(stdin), (int (*)())NULL); - UsingFD(fileno(stdout), (int (*)())NULL); - UsingFD(fileno(stderr), (int (*)())NULL); + UsingFD(fileno(stdin), (void (*)(int))NULL, NULL); + UsingFD(fileno(stdout), (void (*)(int))NULL, NULL); + UsingFD(fileno(stderr), (void (*)(int))NULL, NULL); } /* ************************************************************ */ -UsingFD(fd, Handler) +void +UsingFD(fd, Handler, trans_conn) FD fd; - int (*Handler)(); + void (*Handler)(int); + XtransConnInfo trans_conn; { if (FDD[fd].Busy) NotUsingFD(fd); @@ -93,10 +132,13 @@ UsingFD(fd, Handler) FDD[fd].Busy = true; FDD[fd].InputHandler = Handler; +#ifdef USE_XTRANS + FDD[fd].trans_conn = trans_conn; +#endif if (Handler == NULL) - FD_CLR(fd, &ReadDescriptors); + FD_CLR(fd,&ReadDescriptors) /* clear fd bit */ ; else - FD_SET(fd, &ReadDescriptors); + FD_SET(fd,&ReadDescriptors) /* set fd bit */ ; if (fd > HighestFD) HighestFD = fd; @@ -109,6 +151,7 @@ UsingFD(fd, Handler) /* ************************************************************ */ +void NotUsingFD(fd) FD fd; { @@ -118,7 +161,7 @@ NotUsingFD(fd) nFDsInUse -= 1; FDD[fd].Busy = false; - FD_CLR(fd, &ReadDescriptors); + FD_CLR(fd,&ReadDescriptors) /* clear fd bit */ ; while (!FDD[HighestFD].Busy && HighestFD > 0) HighestFD -= 1; @@ -128,21 +171,30 @@ NotUsingFD(fd) /* ************************************************************ */ +#ifdef USE_XTRANS +XtransConnInfo GetXTransConnInfo(FD fd) +{ + return FDD[fd].trans_conn; +} +#endif + +/* ************************************************************ */ + +static void EOFonFD(fd) FD fd; { enterprocedure("EOFonFD"); debug(128,(stderr, "EOF on %d\n", fd)); - (void)close(fd); +#ifdef USE_XTRANS + if (FDD[fd].trans_conn) + _X11TransClose(FDD[fd].trans_conn); + else +#endif + close(fd); NotUsingFD(fd); } -Boolean ValidFD(fd) - FD fd; -{ - enterprocedure("ValidFD"); - return(FDD[fd].Busy); -} /* ************************************************************ */ /* */ @@ -150,8 +202,7 @@ Boolean ValidFD(fd) /* */ /* ************************************************************ */ -#include <sys/time.h> /* for struct timeval * */ -#include <errno.h> /* for EINTR, EADDRINUSE, ... */ +#include <errno.h> /* for EINTR, EADDRINUSE, ... */ extern int errno; @@ -161,19 +212,19 @@ MainLoop() while (true) { - fd_set rfds, wfds, xfds; + fd_set rfds, xfds; short nfds; short fd; /* wait for something */ rfds = ReadDescriptors; - FD_ZERO(&wfds); xfds = rfds; debug(128,(stderr, "select %d, rfds = 0%o\n", HighestFD + 1, rfds)); - nfds = select(HighestFD + 1, &rfds, &wfds, &xfds, (struct timeval *)NULL); - debug(128,(stderr, "select nfds = 0%o, rfds = 0%o, 0%o, xfds 0%o\n", - nfds, rfds, wfds, xfds)); + nfds = select(HighestFD + 1, (struct fd_set *) &rfds, + (struct fd_set *) NULL, (struct fd_set *) &xfds, NULL); + debug(128,(stderr, "select nfds = 0%o, rfds = 0%o, xfds 0%o\n", + nfds, rfds, xfds)); if (nfds < 0) { @@ -206,58 +257,19 @@ MainLoop() starvation of later clients by earlier clients */ - if (!FD_ISSET(fd,&rfds)) + if (FD_ISSET(fd,&rfds) == 0) continue; nfds -= 1; - HandleInput(fd); + if (FDD[fd].InputHandler == NULL) + { + panic("FD selected with no handler"); + debug(1,(stderr, "FD %d has NULL handler\n", fd)); + } + else + (FDD[fd].InputHandler)(fd); } } -} - -/* ************************************************************ */ -/* */ -/* */ -/* ************************************************************ */ - -Boolean InputAvailable(fd) -FD fd; -{ - fd_set rfds; - int nfds; - struct timeval timeout; - - enterprocedure("InputAvailable"); - FD_ZERO(&rfds); - FD_SET(fd,&rfds); - - /* use zero-valued time out */ - timeout.tv_sec = 0; - timeout.tv_usec = 0; - - debug(128,(stderr, "select %d, rfds = 0%o\n", HighestFD + 1, rfds)); - nfds = select(HighestFD + 1, &rfds, (fd_set *)NULL, (fd_set *)NULL, &timeout); - debug(128,(stderr, "select nfds = 0%o, rfds = 0%o\n", nfds, rfds)); - - if (nfds <= 0 || !FD_ISSET(fd,&rfds)) - return(false); - - if (FD_ISSET(fd,&rfds)) - return(true); - - return(false); -} - -HandleInput(fd) -FD fd; -{ - enterprocedure("HandleInput"); - if (FDD[fd].InputHandler == NULL) - { - panic("FD selected with no handler"); - debug(1,(stderr, "FD %d has NULL handler\n", fd)); - } - else - (FDD[fd].InputHandler)(fd); + return 0; } |