home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / less2912.zip / ttyin.c < prev    next >
C/C++ Source or Header  |  1995-04-10  |  3KB  |  128 lines

  1. /*
  2.  * Copyright (c) 1984,1985,1989,1994,1995  Mark Nudelman
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice in the documentation and/or other materials provided with 
  12.  *    the distribution.
  13.  *
  14.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
  15.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  17.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
  18.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
  19.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
  20.  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
  21.  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
  22.  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
  23.  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 
  24.  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25.  */
  26.  
  27.  
  28. /*
  29.  * Routines dealing with getting input from the keyboard (i.e. from the user).
  30.  */
  31.  
  32. #include "less.h"
  33.  
  34. static int tty;
  35.  
  36. /*
  37.  * Open keyboard for input.
  38.  */
  39.     public void
  40. open_getchr()
  41. {
  42. #if MSOFTC || OS2
  43.     extern int fd0;
  44.     /*
  45.      * Open a new handle to CON: in binary mode 
  46.      * for unbuffered keyboard read.
  47.      */
  48.      fd0 = dup(0);
  49.      close(0);
  50.      tty = OPEN_TTYIN();
  51. #else
  52.     /*
  53.      * Try /dev/tty.
  54.      * If that doesn't work, use file descriptor 2,
  55.      * which in Unix is usually attached to the screen,
  56.      * but also usually lets you read from the keyboard.
  57.      */
  58.     tty = OPEN_TTYIN();
  59.     if (tty < 0)
  60.         tty = 2;
  61. #endif
  62. }
  63.  
  64. /*
  65.  * Get a character from the keyboard.
  66.  */
  67.     public int
  68. getchr()
  69. {
  70.     char c;
  71.     int result;
  72.  
  73.     do
  74.     {
  75. #if MSOFTC
  76.         /*
  77.          * In raw read, we don't see ^C so look here for it.
  78.          */
  79.         flush();
  80.         c = getch();
  81.         result = 1;
  82.         if (c == '\003')
  83.             return (READ_INTR);
  84. #else
  85. #if OS2
  86.           static int scan = -1;
  87.         flush();
  88.         if (scan != -1)
  89.         {
  90.             c = scan;
  91.             scan = -1;
  92.         }
  93.         else
  94.         {
  95.               /* while ( _read_kbd(0, 0, 0) != -1)
  96.                    continue; */
  97.         if ((c = _read_kbd(0, 1, 0)) == -1)
  98.                   return READ_INTR;
  99.             if (c == 0 && (scan = _read_kbd(0, 0, 0)) != -1)
  100.                   c = 0xE0;
  101.         }
  102.         result = 1;
  103. #else
  104.         result = iread(tty, &c, sizeof(char));
  105.         if (result == READ_INTR)
  106.             return (READ_INTR);
  107.         if (result < 0)
  108.         {
  109.             /*
  110.              * Don't call error() here,
  111.              * because error calls getchr!
  112.              */
  113.             quit(QUIT_ERROR);
  114.         }
  115. #endif
  116. #endif
  117.         /*
  118.          * Various parts of the program cannot handle
  119.          * an input character of '\0'.
  120.          * If a '\0' was actually typed, convert it to '\340' here.
  121.          */
  122.         if (c == '\0')
  123.             c = '\340';
  124.     } while (result != 1);
  125.  
  126.     return (c & 0377);
  127. }
  128.