Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / miscellany / less-177 / ttyin.c
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 static int tty;
14
15 /*
16  * Open keyboard for input.
17  */
18         public void
19 open_getchr()
20 {
21 #if __MDDOS__
22         /*
23          * Open a new handle to CON: in binary mode 
24          * for unbuffered keyboard read.
25          */
26         tty = open("CON", O_RDONLY|O_BINARY);
27 #else
28         /*
29          * Try /dev/tty.
30          * If that doesn't work, use file descriptor 2,
31          * which in Unix is usually attached to the screen,
32          * but also usually lets you read from the keyboard.
33          */
34         tty = open("/dev/tty", 0);
35         if (tty < 0)
36                 tty = 2;
37 #endif
38 }
39
40 /*
41  * Get a character from the keyboard.
42  */
43         public int
44 getchr()
45 {
46         char c;
47         int result;
48
49         do
50         {
51                 result = iread(tty, &c, sizeof(char));
52                 if (result == READ_INTR)
53                         return (READ_INTR);
54                 if (result < 0)
55                 {
56                         /*
57                          * Don't call error() here,
58                          * because error calls getchr!
59                          */
60                         quit(1);
61                 }
62 #if __MSDOS__
63                 /*
64                  * In raw read, we don't see ^C so look here for it.
65                  */
66                 if (c == '\003')
67                         raise(SIGINT);
68 #endif
69                 /*
70                  * Various parts of the program cannot handle
71                  * an input character of '\0'.
72                  * If a '\0' was actually typed, convert it to '\200' here.
73                  */
74                 if (c == '\0')
75                         c = '\200';
76         } while (result != 1);
77
78         return (c);
79 }