home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / less-321-src.tgz / tar.out / fsf / less / ttyin.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  4KB  |  168 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.  
  41. /*
  42.  * Open keyboard for input.
  43.  */
  44.     public void
  45. open_getchr()
  46. {
  47. #if MSDOS_COMPILER==WIN32C
  48.     /* Need this to let child processes inherit our console handle */
  49.     SECURITY_ATTRIBUTES sa;
  50.     memset(&sa, 0, sizeof(SECURITY_ATTRIBUTES));
  51.     sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  52.     sa.bInheritHandle = TRUE;
  53.     tty = (int) CreateFile("CONIN$", GENERIC_READ, 
  54.             FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, 
  55.             OPEN_EXISTING, 0L, NULL);
  56.     GetConsoleMode((HANDLE)tty, &console_mode);
  57.     /* Make sure we get Ctrl+C events. */
  58.     SetConsoleMode((HANDLE)tty, 0); /* doesn't work for some reason --jdp */
  59. #else
  60. #if MSDOS_COMPILER || OS2
  61.     extern int fd0;
  62.     /*
  63.      * Open a new handle to CON: in binary mode 
  64.      * for unbuffered keyboard read.
  65.      */
  66.      fd0 = dup(0);
  67.      close(0);
  68.      tty = OPEN_TTYIN();
  69. #else
  70.     /*
  71.      * Try /dev/tty.
  72.      * If that doesn't work, use file descriptor 2,
  73.      * which in Unix is usually attached to the screen,
  74.      * but also usually lets you read from the keyboard.
  75.      */
  76.     tty = OPEN_TTYIN();
  77.     if (tty < 0)
  78.         tty = 2;
  79. #endif
  80. #endif
  81. }
  82.  
  83. /*
  84.  * Close the keyboard.
  85.  */
  86.     public void
  87. close_getchr()
  88. {
  89. #if MSDOS_COMPILER==WIN32C
  90.     SetConsoleMode((HANDLE)tty, console_mode);
  91.     CloseHandle((HANDLE)tty);
  92. #endif
  93. }
  94.  
  95. /*
  96.  * Get a character from the keyboard.
  97.  */
  98.     public int
  99. getchr()
  100. {
  101.     char c;
  102.     int result;
  103.  
  104.     do
  105.     {
  106. #if MSDOS_COMPILER
  107.         /*
  108.          * In raw read, we don't see ^C so look here for it.
  109.          */
  110.         flush();
  111. #if MSDOS_COMPILER==WIN32C
  112.         c = WIN32getch(tty);
  113. #else
  114.         c = getch();
  115. #endif
  116.         result = 1;
  117.         if (c == '\003')
  118.             return (READ_INTR);
  119. #else
  120. #if OS2
  121.     {
  122.         static int scan = -1;
  123.         flush();
  124.         if (scan >= 0)
  125.         {
  126.             c = scan;
  127.             scan = -1;
  128.         } else
  129.         {
  130.             if ((c = _read_kbd(0, 1, 0)) == -1)
  131.                 return (READ_INTR);
  132.             if (c == '\0')
  133.             {
  134.                 /*
  135.                  * Zero is usually followed by another byte,
  136.                  * since certain keys send two bytes.
  137.                  */
  138.                 scan = _read_kbd(0, 0, 0);
  139.             }
  140.         }
  141.         result = 1;
  142.     }
  143. #else
  144.         result = iread(tty, &c, sizeof(char));
  145.         if (result == READ_INTR)
  146.             return (READ_INTR);
  147.         if (result < 0)
  148.         {
  149.             /*
  150.              * Don't call error() here,
  151.              * because error calls getchr!
  152.              */
  153.             quit(QUIT_ERROR);
  154.         }
  155. #endif
  156. #endif
  157.         /*
  158.          * Various parts of the program cannot handle
  159.          * an input character of '\0'.
  160.          * If a '\0' was actually typed, convert it to '\340' here.
  161.          */
  162.         if (c == '\0')
  163.             c = '\340';
  164.     } while (result != 1);
  165.  
  166.     return (c & 0377);
  167. }
  168.