home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / less3292.zip / ttyin.c < prev    next >
C/C++ Source or Header  |  1996-08-21  |  4KB  |  178 lines

  1. /*
  2.  * Copyright (c) 1984,1985,1989,1994,1995,1996  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 MSDOS_COMPILER==WIN32C
  34. #include "windows.h"
  35. extern char WIN32getch();
  36. static DWORD console_mode;
  37. #endif
  38.  
  39. static int tty;
  40. extern int sigs;
  41.  
  42. /*
  43.  * Open keyboard for input.
  44.  */
  45.     public void
  46. open_getchr()
  47. {
  48. #if MSDOS_COMPILER==WIN32C
  49.     /* Need this to let child processes inherit our console handle */
  50.     SECURITY_ATTRIBUTES sa;
  51.     memset(&sa, 0, sizeof(SECURITY_ATTRIBUTES));
  52.     sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  53.     sa.bInheritHandle = TRUE;
  54.     tty = (int) CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE,
  55.             FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, 
  56.             OPEN_EXISTING, 0L, NULL);
  57.     GetConsoleMode((HANDLE)tty, &console_mode);
  58.     /* Make sure we get Ctrl+C events. */
  59.     SetConsoleMode((HANDLE)tty, ENABLE_PROCESSED_INPUT);
  60. #else
  61. #if MSDOS_COMPILER || OS2
  62.     extern int fd0;
  63.     /*
  64.      * Open a new handle to CON: in binary mode 
  65.      * for unbuffered keyboard read.
  66.      */
  67.      fd0 = dup(0);
  68.      close(0);
  69.      tty = open("CON", OPEN_READ);
  70. #if MSDOS_COMPILER==DJGPPC
  71.     /*
  72.      * Setting stdin to binary causes Ctrl-C to not
  73.      * raise SIGINT.  We must undo that side-effect.
  74.      */
  75.     (void) __djgpp_set_ctrl_c(1);
  76. #endif
  77. #else
  78.     /*
  79.      * Try /dev/tty.
  80.      * If that doesn't work, use file descriptor 2,
  81.      * which in Unix is usually attached to the screen,
  82.      * but also usually lets you read from the keyboard.
  83.      */
  84.     tty = open("/dev/tty", OPEN_READ);
  85.     if (tty < 0)
  86.         tty = 2;
  87. #endif
  88. #endif
  89. }
  90.  
  91. /*
  92.  * Close the keyboard.
  93.  */
  94.     public void
  95. close_getchr()
  96. {
  97. #if MSDOS_COMPILER==WIN32C
  98.     SetConsoleMode((HANDLE)tty, console_mode);
  99.     CloseHandle((HANDLE)tty);
  100. #endif
  101. }
  102.  
  103. /*
  104.  * Get a character from the keyboard.
  105.  */
  106.     public int
  107. getchr()
  108. {
  109.     char c;
  110.     int result;
  111.  
  112.     do
  113.     {
  114. #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
  115.         /*
  116.          * In raw read, we don't see ^C so look here for it.
  117.          */
  118.         flush();
  119. #if MSDOS_COMPILER==WIN32C
  120.         if (ABORT_SIGS())
  121.             return (READ_INTR);
  122.         c = WIN32getch(tty);
  123. #else
  124.         c = getch();
  125. #endif
  126.         result = 1;
  127.         if (c == '\003')
  128.             return (READ_INTR);
  129. #else
  130. #if OS2
  131.     {
  132.         static int scan = -1;
  133.         flush();
  134.         if (scan >= 0)
  135.         {
  136.             c = scan;
  137.             scan = -1;
  138.         } else
  139.         {
  140.             if ((c = _read_kbd(0, 1, 0)) == -1)
  141.                 return (READ_INTR);
  142.             if (c == '\0')
  143.             {
  144.                 /*
  145.                  * Zero is usually followed by another byte,
  146.                  * since certain keys send two bytes.
  147.                  */
  148.                 scan = _read_kbd(0, 0, 0);
  149.             }
  150.         }
  151.         result = 1;
  152.     }
  153. #else
  154.         result = iread(tty, &c, sizeof(char));
  155.         if (result == READ_INTR)
  156.             return (READ_INTR);
  157.         if (result < 0)
  158.         {
  159.             /*
  160.              * Don't call error() here,
  161.              * because error calls getchr!
  162.              */
  163.             quit(QUIT_ERROR);
  164.         }
  165. #endif
  166. #endif
  167.         /*
  168.          * Various parts of the program cannot handle
  169.          * an input character of '\0'.
  170.          * If a '\0' was actually typed, convert it to '\340' here.
  171.          */
  172.         if (c == '\0')
  173.             c = '\340';
  174.     } while (result != 1);
  175.  
  176.     return (c & 0377);
  177. }
  178.