1 /* $NetBSD: ttyin.c,v 1.3 2013/09/04 19:44:21 tron Exp $ */
4 * Copyright (C) 1984-2012 Mark Nudelman
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Less License, as specified in the README file.
9 * For more information, see the README file.
14 * Routines dealing with getting input from the keyboard (i.e. from the user).
22 #if MSDOS_COMPILER==WIN32C
24 extern char WIN32getch();
25 static DWORD console_mode
;
33 * Open keyboard for input.
38 #if MSDOS_COMPILER==WIN32C
39 /* Need this to let child processes inherit our console handle */
40 SECURITY_ATTRIBUTES sa
;
41 memset(&sa
, 0, sizeof(SECURITY_ATTRIBUTES
));
42 sa
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
43 sa
.bInheritHandle
= TRUE
;
44 tty
= (int) CreateFile("CONIN$", GENERIC_READ
,
46 OPEN_EXISTING
, 0L, NULL
);
47 GetConsoleMode((HANDLE
)tty
, &console_mode
);
48 /* Make sure we get Ctrl+C events. */
49 SetConsoleMode((HANDLE
)tty
, ENABLE_PROCESSED_INPUT
);
54 * Open a new handle to CON: in binary mode
55 * for unbuffered keyboard read.
59 tty
= open("CON", OPEN_READ
);
60 #if MSDOS_COMPILER==DJGPPC
62 * Setting stdin to binary causes Ctrl-C to not
63 * raise SIGINT. We must undo that side-effect.
65 (void) __djgpp_set_ctrl_c(1);
70 * If that doesn't work, use file descriptor 2,
71 * which in Unix is usually attached to the screen,
72 * but also usually lets you read from the keyboard.
75 /* The __open() system call translates "/dev/tty" to "con". */
76 tty
= __open("/dev/tty", OPEN_READ
);
78 tty
= open("/dev/tty", OPEN_READ
);
92 #if MSDOS_COMPILER==WIN32C
93 SetConsoleMode((HANDLE
)tty
, console_mode
);
94 CloseHandle((HANDLE
)tty
);
99 * Get a character from the keyboard.
109 #if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
111 * In raw read, we don't see ^C so look here for it.
114 #if MSDOS_COMPILER==WIN32C
125 result
= iread(tty
, &c
, sizeof(char));
126 if (result
== READ_INTR
)
131 * Don't call error() here,
132 * because error calls getchr!
137 #if 0 /* allow entering arbitrary hex chars for testing */
138 /* ctrl-A followed by two hex chars makes a byte */
142 if (c
== CONTROL('A'))
151 if (c
>= '0' && c
<= '9')
153 else if (c
>= 'a' && c
<= 'f')
155 else if (c
>= 'A' && c
<= 'F')
159 hex_value
= (hex_value
<< 4) | v
;
170 * Various parts of the program cannot handle
171 * an input character of '\0'.
172 * If a '\0' was actually typed, convert it to '\340' here.
176 } while (result
!= 1);