2 * Copyright (C) 1984-2009 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information about less, or for information on how to
8 * contact the author, see the README file.
13 * Routines dealing with getting input from the keyboard (i.e. from the user).
21 #if MSDOS_COMPILER==WIN32C
23 extern char WIN32getch();
24 static DWORD console_mode
;
32 * Open keyboard for input.
37 #if MSDOS_COMPILER==WIN32C
38 /* Need this to let child processes inherit our console handle */
39 SECURITY_ATTRIBUTES sa
;
40 memset(&sa
, 0, sizeof(SECURITY_ATTRIBUTES
));
41 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
42 sa
.bInheritHandle
= TRUE
;
43 tty
= (int) CreateFile("CONIN$", GENERIC_READ
,
45 OPEN_EXISTING
, 0L, NULL
);
46 GetConsoleMode((HANDLE
)tty
, &console_mode
);
47 /* Make sure we get Ctrl+C events. */
48 SetConsoleMode((HANDLE
)tty
, ENABLE_PROCESSED_INPUT
);
53 * Open a new handle to CON: in binary mode
54 * for unbuffered keyboard read.
58 tty
= open("CON", OPEN_READ
);
59 #if MSDOS_COMPILER==DJGPPC
61 * Setting stdin to binary causes Ctrl-C to not
62 * raise SIGINT. We must undo that side-effect.
64 (void) __djgpp_set_ctrl_c(1);
69 * If that doesn't work, use file descriptor 2,
70 * which in Unix is usually attached to the screen,
71 * but also usually lets you read from the keyboard.
74 /* The __open() system call translates "/dev/tty" to "con". */
75 tty
= __open("/dev/tty", OPEN_READ
);
77 tty
= open("/dev/tty", OPEN_READ
);
91 #if MSDOS_COMPILER==WIN32C
92 SetConsoleMode((HANDLE
)tty
, console_mode
);
93 CloseHandle((HANDLE
)tty
);
98 * Get a character from the keyboard.
108 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
110 * In raw read, we don't see ^C so look here for it.
113 #if MSDOS_COMPILER==WIN32C
124 result
= iread(tty
, &c
, sizeof(char));
125 if (result
== READ_INTR
)
130 * Don't call error() here,
131 * because error calls getchr!
136 #if 0 /* allow entering arbitrary hex chars for testing */
137 /* ctrl-A followed by two hex chars makes a byte */
141 if (c
== CONTROL('A'))
150 if (c
>= '0' && c
<= '9')
152 else if (c
>= 'a' && c
<= 'f')
154 else if (c
>= 'A' && c
<= 'F')
158 hex_value
= (hex_value
<< 4) | v
;
169 * Various parts of the program cannot handle
170 * an input character of '\0'.
171 * If a '\0' was actually typed, convert it to '\340' here.
175 } while (result
!= 1);