home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / GNU / LES177AS.ZIP / TTYIN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-11  |  2.1 KB  |  133 lines

  1. /*
  2.  * Routines dealing with getting input from the keyboard (i.e. from the user).
  3.  */
  4.  
  5. #include "less.h"
  6.  
  7. extern int kill_char;
  8.  
  9. #ifndef TURBOC
  10.  
  11. #if __MSDOS__
  12. #include <io.h>
  13. #include <stdio.h>
  14. #include <fcntl.h>
  15. #include <signal.h>
  16. #endif
  17.  
  18. static int tty;
  19.  
  20. /*
  21.  * Open keyboard for input.
  22.  */
  23.     public void
  24. open_getchr()
  25. {
  26. #if __MDDOS__
  27.     /*
  28.      * Open a new handle to CON: in binary mode 
  29.      * for unbuffered keyboard read.
  30.      */
  31.     tty = open("CON", O_RDONLY|O_BINARY);
  32. #else
  33.     /*
  34.      * Try /dev/tty.
  35.      * If that doesn't work, use file descriptor 2,
  36.      * which in Unix is usually attached to the screen,
  37.      * but also usually lets you read from the keyboard.
  38.      */
  39.     tty = open("/dev/tty", 0);
  40.     if (tty < 0)
  41.         tty = 2;
  42. #endif
  43. }
  44.  
  45. /*
  46.  * Get a character from the keyboard.
  47.  */
  48.     public int
  49. getchr()
  50. {
  51.     char c;
  52.     int result;
  53.  
  54.     do
  55.     {
  56.         result = iread(tty, &c, sizeof(char));
  57.         if (result == READ_INTR)
  58.             return (READ_INTR);
  59.         if (result < 0)
  60.         {
  61.             /*
  62.              * Don't call error() here,
  63.              * because error calls getchr!
  64.              */
  65.             quit(1);
  66.         }
  67. #if __MSDOS__
  68.         /*
  69.          * In raw read, we don't see ^C so look here for it.
  70.          */
  71.         if (c == kill_char)
  72.             raise(SIGINT);
  73. #endif
  74.         /*
  75.          * Various parts of the program cannot handle
  76.          * an input character of '\0'.
  77.          * If a '\0' was actually typed, convert it to '\200' here.
  78.          */
  79.         if (c == '\0')
  80.             c = '\200';
  81.     } while (result != 1);
  82.  
  83.     return (c);
  84. }
  85. #else /* TURBOC */
  86.  
  87. #include <dos.h>
  88. #include <signal.h>
  89.  
  90. /*
  91.  * Open keyboard for input.
  92.  */
  93.     public void
  94. open_getchr()
  95. {
  96. }
  97.  
  98. /*
  99.  * Get a character directly from the PC BIOS.
  100.  */
  101.     public int
  102. getchr()
  103. {
  104.     int c;
  105.      static int extrac = 0;
  106.  
  107.      if (extrac) {
  108.          c = extrac;
  109.          extrac = 0;
  110.          return c;
  111.      }
  112.      flush ();
  113.      do {
  114.          asm {
  115.              mov    ah,0    /* call:  BIOS read char into ax */
  116.              int    16h
  117.          }
  118.         c = _AX;
  119.         if (c == 0) {
  120.             raise (SIGINT);
  121.             c = kill_char;
  122.         } else {
  123.             extrac = c >> 8;
  124.             if (c &= 0xff)
  125.                 extrac = 0;
  126.             else
  127.                 c = '\200';
  128.         }
  129.      } while (c == 0);
  130.     return c;
  131. }
  132. #endif    /* TURBOC */
  133.