home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / os2 / less / ttyin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-27  |  2.2 KB  |  126 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. #ifdef OS2
  14. #undef NULL
  15. #undef VOID
  16. #undef CONTROL
  17. #define INCL_DOSPROCESS
  18. #define INCL_SUB
  19. #define INCL_NOPM
  20. #include <os2.h>
  21. #include <stdio.h>
  22. #endif
  23.  
  24. static int tty;
  25.  
  26. /*
  27.  * Open keyboard for input.
  28.  */
  29.     public void
  30. open_getchr()
  31. {
  32. #if __MSDOS__
  33.     /*
  34.      * Open a new handle to CON: in binary mode
  35.      * for unbuffered keyboard read.
  36.      */
  37.     tty = open("CON", O_RDONLY|O_BINARY);
  38. #else
  39.     /*
  40.      * Try /dev/tty.
  41.      * If that doesn't work, use file descriptor 2,
  42.      * which in Unix is usually attached to the screen,
  43.      * but also usually lets you read from the keyboard.
  44.      */
  45.     tty = open("/dev/tty", 0);
  46.     if (tty < 0)
  47.         tty = 2;
  48. #endif
  49. }
  50.  
  51. /*
  52.  * Get a character from the keyboard.
  53.  */
  54.     public int
  55. getchr()
  56. {
  57.         int c;
  58.     int result;
  59.  
  60.         flush();
  61.  
  62.     do
  63.     {
  64. #ifdef OS2
  65.                 static int ext, scan;
  66.                 KBDKEYINFO ki;
  67.  
  68.                 if ( ext )
  69.                 {
  70.                   ext = 0;
  71.                   c = scan;
  72.                 }
  73.                 else
  74.                 {
  75.                   KbdCharIn(&ki, IO_WAIT, 0);
  76.                   c = ki.chChar;
  77.  
  78.                   if ( c == 0 || c == 0xE0 )
  79.                   {
  80.                     c = 0xE0;
  81.                     ext = 1;
  82.                     scan = ki.chScan;
  83.                   }
  84.                 }
  85.                 result = 1;
  86. #else
  87.         result = iread(tty, &c, sizeof(char));
  88.         if (result == READ_INTR)
  89.             return (READ_INTR);
  90. #endif
  91.         if (result < 0)
  92.         {
  93.             /*
  94.              * Don't call error() here,
  95.              * because error calls getchr!
  96.              */
  97.             quit(1);
  98.         }
  99. #if __MSDOS__
  100.         /*
  101.          * In raw read, we don't see ^C so look here for it.
  102.          */
  103.         if (c == '\003')
  104.             raise(SIGINT);
  105. #endif
  106.         /*
  107.          * Various parts of the program cannot handle
  108.          * an input character of '\0'.
  109.          * If a '\0' was actually typed, convert it to '\200' here.
  110.          */
  111.         if (c == '\0')
  112.             c = '\200';
  113.     } while (result != 1);
  114.  
  115.     return (c);
  116. }
  117.  
  118.  
  119. #ifdef OS2
  120. sleep(x)
  121. int x;
  122. {
  123.   DosSleep(1000L * x);
  124. }
  125. #endif
  126.