summaryrefslogtreecommitdiff
path: root/usr.bin/less/ttyin.c
blob: cfe051365f595938d2283b37dda357c22b7f4cad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
 * Copyright (C) 1984-2012  Mark Nudelman
 * Modified for use with illumos by Garrett D'Amore.
 * Copyright 2014 Garrett D'Amore <garrett@damore.org>
 *
 * You may distribute under the terms of either the GNU General Public
 * License or the Less License, as specified in the README file.
 *
 * For more information, see the README file.
 */

/*
 * Routines dealing with getting input from the keyboard (i.e. from the user).
 */

#include "less.h"

int tty;
extern int utf_mode;

/*
 * Open keyboard for input.
 */
void
open_getchr(void)
{
	/*
	 * Try /dev/tty.
	 * If that doesn't work, use file descriptor 2,
	 * which in Unix is usually attached to the screen,
	 * but also usually lets you read from the keyboard.
	 */
	tty = open("/dev/tty", O_RDONLY);
	if (tty == -1)
		tty = STDERR_FILENO;
}

/*
 * Get a character from the keyboard.
 */
int
getchr(void)
{
	unsigned char c;
	int result;

	do {
		result = iread(tty, &c, sizeof (char));
		if (result == READ_INTR)
			return (READ_INTR);
		if (result < 0) {
			/*
			 * Don't call error() here,
			 * because error calls getchr!
			 */
			quit(QUIT_ERROR);
		}
		/*
		 * Various parts of the program cannot handle
		 * an input character of '\0'.
		 * If a '\0' was actually typed, convert it to '\340' here.
		 */
		if (c == '\0')
			c = 0340;
	} while (result != 1);

	return (c & 0xFF);
}