home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / unix / bsd / 11175 < prev    next >
Encoding:
Internet Message Format  |  1993-01-12  |  3.3 KB

  1. Path: sparky!uunet!europa.asd.contel.com!howland.reston.ans.net!spool.mu.edu!agate!dog.ee.lbl.gov!horse.ee.lbl.gov!torek
  2. From: torek@horse.ee.lbl.gov (Chris Torek)
  3. Newsgroups: comp.unix.bsd
  4. Subject: Re: a unix terminal question
  5. Date: 12 Jan 1993 21:05:35 GMT
  6. Organization: Lawrence Berkeley Laboratory, Berkeley CA
  7. Lines: 75
  8. Message-ID: <28370@dog.ee.lbl.gov>
  9. References: <1iql6kINNisk@ub.d.umn.edu> <1993Jan11.215312.2080@fcom.cc.utah.edu> <uonuhjc@zola.esd.sgi.com>
  10. NNTP-Posting-Host: 128.3.112.15
  11.  
  12. (I note that this article would have gone to both comp.lang.c and
  13. comp.unix.bsd.  It is rare for an article ever to belong in both
  14. groups.  This particular stuff really should not be in comp.lang.c at
  15. all, so I have redirected it.)
  16.  
  17. In article <uonuhjc@zola.esd.sgi.com> dps@delhi.esd.sgi.com (D.P. Suresh)
  18. writes:
  19. >select() or poll() in itself will not get you the desired result
  20. >[because the O/S is doing line gathering and you must disable this too].
  21.  
  22. >    /* Create proper environment for select() */
  23. >    FD_ZERO( &readfds );
  24. >    FD_ZERO( &writefds );
  25. >    FD_ZERO( &exceptfds );
  26. >    FD_SET( fileno(stdin), &readfds );
  27.  
  28. Since no write or exception file descriptors will be used, none need be
  29. provided.  Select() allows (fd_set *)NULL arguments, meaning `nothing
  30. of interest here'.
  31.  
  32. >    /* We shall specify 0.5 sec as the waiting time */
  33. >    timeout.tv_sec  = 0;    /*   0 seconds */
  34. >    timeout.tv_usec = 500;    /* 500 microseconds */
  35.  
  36. tv_usec is indeed microseconds, so 500 us is 0.0005 seconds, not 0.5.
  37. (Most current UNIX systems will round this up to their actual internal
  38. timeout rate of 100 Hz, or .01 seconds.)
  39.  
  40. >    /* Put tty in raw mode */
  41. >    ioctl(fileno(stdout), TCGETA, &otty);
  42.     ...
  43. >    ioctl(fileno(stderr), TCSETAW, &ntty);
  44.  
  45. Here I have to wonder why the tty modes are obtained from stdout and
  46. set on stderr when we are going to read stdin.  If the reading is to be
  47. from fileno(stdin), the tty operations should be done there as well,
  48. since their purpose is to affect this reading.
  49.  
  50. In more complex tty control programs (such as editors), the goal may be
  51. both to affect input and output.  In this case it is a bit more
  52. reasonable to try both stdin and stdout.  In any case it seems wrong to
  53. touch stderr.  The wise and cautious programmer might consider:
  54.  
  55.     iocfd = STDIN_FILENO;        /* or simply 0 */
  56.     if (ioctl(iocfd, TCGETA, &otty) < 0) {
  57.         iocfd = STDOUT_FILENO;    /* 1---these should be in <unistd.h> */
  58.         if (ioctl(iocfd, TCGETA, &otty) < 0) {
  59.             /* disable tty operations, i/o is pipes or similar */
  60.             ttycontrol = 0;
  61.             return;
  62.         }
  63.     }
  64.     ...
  65.     (void)ioctl(iocfd, TCSETAW, &ntty);
  66.  
  67. In this particular case (reading stdin only), the attempts using
  68. STDOUT_FILENO are not advised.
  69.  
  70. On BSD systems, one should also use care lest code like this be stopped
  71. (via, e.g., ^Z) between the `get' and `set' phases and the tty settings
  72. be changed.  Careful use of signal handlers and, if necessary, setjmp
  73. and longjmp can make tty alteration appear atomic.
  74.  
  75. >This feature should be used wisely. One could incur quite a lot of
  76. >performance penalties as expressed by terry.
  77.  
  78. Indeed, particularly if the keyboard mode is changed twice on every call
  79. to this checking function.
  80.  
  81. Incidentally, note that select() for reading returns true at EOF
  82. (because reading EOF does not block).  Callers should be prepared for
  83. this.
  84. -- 
  85. In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 510 486 5427)
  86. Berkeley, CA        Domain:    torek@ee.lbl.gov
  87.