home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2996 / ttyin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-06  |  1.2 KB  |  76 lines

  1. /*
  2.  * Routines dealing with getting input from the keyboard (i.e. from the user).
  3.  */
  4.  
  5. #include "less.h"
  6. #if __MSDOS__
  7. #include <io.h>
  8. #include <stdio.h>
  9. #include <fcntl.h>
  10. #include <signal.h>
  11. #endif
  12.  
  13. static int tty;
  14.  
  15. /*
  16.  * Open keyboard for input.
  17.  */
  18.     public void
  19. open_getchr()
  20. {
  21. #if __MDDOS__
  22.     /*
  23.      * Open a new handle to CON: in binary mode 
  24.      * for unbuffered keyboard read.
  25.      */
  26.     tty = open("CON", O_RDONLY|O_BINARY);
  27. #else
  28.     /*
  29.      * Just use file descriptor 2, which in Unix
  30.      * is usually attached to the screen and keyboard.
  31.      */
  32.     tty = 2;
  33. #endif
  34. }
  35.  
  36. /*
  37.  * Get a character from the keyboard.
  38.  */
  39.     public int
  40. getchr()
  41. {
  42.     char c;
  43.     int result;
  44.  
  45.     do
  46.     {
  47.         result = iread(tty, &c, sizeof(char));
  48.         if (result == READ_INTR)
  49.             return (READ_INTR);
  50.         if (result < 0)
  51.         {
  52.             /*
  53.              * Don't call error() here,
  54.              * because error calls getchr!
  55.              */
  56.             quit(1);
  57.         }
  58. #if __MSDOS__
  59.         /*
  60.          * In raw read, we don't see ^C so look here for it.
  61.          */
  62.         if (c == '\003')
  63.             raise(SIGINT);
  64. #endif
  65.         /*
  66.          * Various parts of the program cannot handle
  67.          * an input character of '\0'.
  68.          * If a '\0' was actually typed, convert it to '\200' here.
  69.          */
  70.         if (c == '\0')
  71.             c = '\200';
  72.     } while (result != 1);
  73.  
  74.     return (c);
  75. }
  76.