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