home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 036 / less232.zip / TTYIN.C < prev    next >
C/C++ Source or Header  |  1994-09-24  |  3KB  |  131 lines

  1. /*
  2.  * Copyright (c) 1984,1985,1989,1994  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. #if HAVE_FCNTL_H
  34. #include <fcntl.h>
  35. #endif
  36.  
  37. static int tty;
  38.  
  39. /*
  40.  * Open keyboard for input.
  41.  */
  42.     public void
  43. open_getchr()
  44. {
  45. #if MSOFTC || OS2
  46.     extern int fd0;
  47.     /*
  48.      * Open a new handle to CON: in binary mode 
  49.      * for unbuffered keyboard read.
  50.      */
  51.      fd0 = dup(0);
  52.      close(0);
  53.      tty = open("CON", O_RDONLY|O_BINARY);
  54. #else
  55.     /*
  56.      * Try /dev/tty.
  57.      * If that doesn't work, use file descriptor 2,
  58.      * which in Unix is usually attached to the screen,
  59.      * but also usually lets you read from the keyboard.
  60.      */
  61.     tty = open("/dev/tty", 0);
  62.     if (tty < 0)
  63.         tty = 2;
  64. #endif
  65. }
  66.  
  67. /*
  68.  * Get a character from the keyboard.
  69.  */
  70.     public int
  71. getchr()
  72. {
  73.     char c;
  74.     int result;
  75.  
  76.     do
  77.     {
  78. #if MSOFTC
  79.         /*
  80.          * In raw read, we don't see ^C so look here for it.
  81.          */
  82.         flush();
  83.         c = getch();
  84.         result = 1;
  85.         if (c == '\003')
  86.             return (READ_INTR);
  87. #else
  88. #if OS2
  89.           static int scan = -1;
  90.         flush();
  91.         if ( scan != -1 )
  92.         {
  93.             c = scan;
  94.             scan = -1;
  95.             result = 1;
  96.         }
  97.         else
  98.         {
  99.               while ( _read_kbd(0, 0, 0) != -1);
  100.               if ( (c = _read_kbd(0, 1, 0)) == -1 )
  101.                   return READ_INTR;
  102.             if ( c == 0 && (scan = _read_kbd(0, 0, 0)) != -1 )
  103.                   c = 0xE0;
  104.             result = 1;
  105.         }
  106. #else
  107.         result = iread(tty, &c, sizeof(char));
  108.         if (result == READ_INTR)
  109.             return (READ_INTR);
  110.         if (result < 0)
  111.         {
  112.             /*
  113.              * Don't call error() here,
  114.              * because error calls getchr!
  115.              */
  116.             quit(1);
  117.         }
  118. #endif
  119. #endif
  120.         /*
  121.          * Various parts of the program cannot handle
  122.          * an input character of '\0'.
  123.          * If a '\0' was actually typed, convert it to '\340' here.
  124.          */
  125.         if (c == '\0')
  126.             c = '\340';
  127.     } while (result != 1);
  128.  
  129.     return (c & 0377);
  130. }
  131.