How can I read keyboard without stopping

Brandon S. Allbery allbery at ncoast.UUCP
Thu Aug 18 09:37:43 AEST 1988


As quoted from <1246 at mcgill-vision.UUCP> by mouse at mcgill-vision.UUCP (der Mouse):
+---------------
| In article <813 at ms3.UUCP>, isns02 at ms3.UUCP (Harris Reavin) writes:
| > I would like to know if it is possible to get input data from the
| > keyboard while my program is constantly looping and displaying output
| > to the screen.  The input comes from a call to "popen()".
| 
| Now wait a minute!  If you used popen(), your input is coming from
| another program, and only indirectly (if at all) from the keyboard.
+---------------

I think he meant the program is copying from a popen()'ed stream to the
screen but *also* wants to catch keypresses.

+---------------
| 2) Non-blocking I/O.  BSD systems, and I believe recent SV systems,
|    support the notion of "non-blocking" reads.  What this amounts to is
|    you make a call to turn this mode on, and then any read for which no
|    characters are available will return an error indication instead of
|    blocking.
+---------------

*All* System III and System V systems have it.  You're only out of luck if
you want to port to V6/V7 or Minix (and you probably have bigger problems
than non-blocking reads in that case!).

+---------------
|    Bad:	- may not interact well with curses.  Many programs
| 		  (and library packages) are not prepared to handle
| 		  "would block" errors.
+---------------

Note that System III/V returns 0 and leaves errno unchanged; BSD returns -1
and sets errno to EWOULDBLOCK.

+---------------
| 3) FIONREAD.  BSD systems have an ioctl you can apply to a terminal
|    line to get the number of characters queued available for a read().
|    SV may have something similar; I don't know.
+---------------

System V does not.  Xenix has rdchk(fd).  (I wish AT&T would pick it up!)

+---------------
| 4) select().  BSD systems, and some "SV with BSD extensions" systems (I
|    know of at least one) have the select() syscall.  This allows you to
+---------------

If you are one of the elite with a System V whose terminal drivers are
STREAMS modules (not many are as yet, although it's supposed to be in SVR4)
you can use poll() the same way that BSD uses select().  Another wish of mine
is for poll() to work on non-STREAMS fd's -- how would BSD be if select()
only worked on sockets?

++Brandon
-- 
Brandon S. Allbery, uunet!marque!ncoast!allbery			DELPHI: ALLBERY
	    For comp.sources.misc send mail to ncoast!sources-misc



More information about the Comp.unix.wizards mailing list