home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / gdb-4.9 / gdb / nindy-share / demux.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-12  |  3.2 KB  |  102 lines

  1. /******************************************************************************
  2.  *  Copyright 1990, 1992 Free Software Foundation, Inc.
  3.  *
  4.  *  This code was donated by Intel Corp.
  5.  *
  6.  * This include file supports demultiplexing of input from two sources:
  7.  * stdin and one external source (normally the NINDY monitor).
  8.  *
  9.  * Its purpose is to promote portability of applications between different
  10.  * flavors (BSD and USG/SysV) of Unix.  As of this writing, it is used by the
  11.  * gdb960 remote communications module (remote.c) and the comm960 utility.
  12.  * 
  13.  * It is assumed that 'USG' is defined on the compiler invocation line if the
  14.  * code should compile and run on a USG/SysV system.  Otherwise, BSD is assumed.
  15.  *
  16.  * The application code must use all three of these macros:
  17.  *
  18.  *    DEMUX_DECL    Declares data structures needed by the other macros.
  19.  *
  20.  *    DEMUX_WAIT(fd)    Waits until input is available on either stdin or
  21.  *            file descriptor 'fd'.
  22.  *
  23.  *    DEMUX_READ(fd,bufp,bufsz)
  24.  *            Reads up to 'bufsz' bytes from file descriptor 'fd'
  25.  *            into buffer pointed at by character pointer 'bufp'.
  26.  *            Returns the number of bytes read, which will be 0
  27.  *            if there was no input pending on 'fd'. 'fd' should be
  28.  *            either 0 (stdin) or the same descriptor that was used
  29.  *            in the invocation of 'DEMUX_WAIT'.
  30.  *
  31.  * The following macro is also included:
  32.  *
  33.  *    TIME_INPUT(fd,timeout,retp)
  34.  *            Waits up to 'timeout' seconds for input on file
  35.  *            descriptor 'fd'.  Sets (*retp) to:
  36.  *                 1 if input is ready
  37.  *                 0 if timeout occurred
  38.  *                -1 if wait was interrupted by a signal
  39.  *
  40.  * WARNINGS ABOUT USG (System V) UNIX!!
  41.  *
  42.  *    The TIME_INPUT macro isn't implemented: it's a no-op.
  43.  *
  44.  *    The damned 'poll' call can't be used on normal tty's, so DEMUX_WAIT is
  45.  *    also a no-op: DEMUX_READ uses the FIONREAD ioctl if it's available;
  46.  *    otherwise the file descriptor is temporarily set for non-blocking input
  47.  *    and a read it done.
  48.  *
  49.  ******************************************************************************/
  50.  
  51. #ifdef USG
  52. #    include <fcntl.h>
  53.  
  54. #    define DEMUX_DECL        int _saveflags_; int _n_
  55. #    define DEMUX_WAIT(fd)
  56.  
  57.     /* Use non-blocking I/O */
  58. #    define DEMUX_READ(fd,bufp,bufsz) (            \
  59.             _saveflags_ = fcntl( fd, F_GETFL, 0 ),        \
  60.             fcntl( fd, F_SETFL, _saveflags_ | O_NDELAY ),    \
  61.             _n_ = read( fd, bufp, bufsz ),            \
  62.             fcntl( fd, F_SETFL, _saveflags_ ),        \
  63.             _n_ )
  64.  
  65. #    define TIME_INPUT(fd,timeout,retp)
  66.  
  67. #else    /* BSD */
  68.  
  69. #    include <sys/types.h>
  70. #    include <sys/time.h>
  71.  
  72. #    define DEMUX_DECL    fd_set _mux_
  73.  
  74. #    define DEMUX_WAIT(fd)    {                    \
  75.             FD_ZERO( &_mux_ );                \
  76.             FD_SET( 0, &_mux_ );                \
  77.             FD_SET( fd, &_mux_ );                \
  78.             if (select(fd+1,&_mux_,0,0,0) <= 0){        \
  79.                 FD_ZERO(&_mux_);            \
  80.             }                        \
  81.     }
  82.             /* Check return value of select in case of
  83.              * premature termination due to signal:  clear
  84.              * file descriptors in this case so DEMUX_READ
  85.              * doesn't mistakenly say there's input on them.
  86.              */
  87.  
  88. #    define DEMUX_READ(fd,bufp,bufsz) \
  89.             ( FD_ISSET(fd,&_mux_) ?    read(fd,bufp,bufsz) : 0 )
  90.  
  91. #    define TIME_INPUT(fd,timeout,retp)    {            \
  92.             fd_set _fds_;                    \
  93.             struct timeval _timeout_;            \
  94.             FD_ZERO( &_fds_ );                \
  95.             FD_SET( fd, &_fds_ );                \
  96.             _timeout_.tv_sec  = timeout;            \
  97.             _timeout_.tv_usec = 0;                \
  98.             *(retp) = select(fd+1,&_fds_,0,0,&_timeout_);    \
  99.     }
  100.  
  101. #endif
  102.