1 /* @(#)hack.tty.c 8.1 (Berkeley) 5/31/93 */
2 /* $NetBSD: hack.tty.c,v 1.16 2011/08/06 20:42:43 dholland Exp $ */
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
36 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions are
42 * - Redistributions of source code must retain the above copyright notice,
43 * this list of conditions and the following disclaimer.
45 * - Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
49 * - Neither the name of the Stichting Centrum voor Wiskunde en
50 * Informatica, nor the names of its contributors may be used to endorse or
51 * promote products derived from this software without specific prior
54 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
55 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
57 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
58 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
59 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
60 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
61 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
62 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
63 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
64 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68 * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
69 * All rights reserved.
71 * Redistribution and use in source and binary forms, with or without
72 * modification, are permitted provided that the following conditions
74 * 1. Redistributions of source code must retain the above copyright
75 * notice, this list of conditions and the following disclaimer.
76 * 2. Redistributions in binary form must reproduce the above copyright
77 * notice, this list of conditions and the following disclaimer in the
78 * documentation and/or other materials provided with the distribution.
79 * 3. The name of the author may not be used to endorse or promote products
80 * derived from this software without specific prior written permission.
82 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
83 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
84 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
85 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
86 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
87 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
88 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
89 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
90 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
91 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
94 /* hack.tty.c - version 1.0.3 */
96 * With thanks to the people who sent code for SYSV - hpscdi!jon,
97 * arnold@ucsf-cgl, wcs@bo95b, cbcephus!pds and others.
106 * Some systems may have getchar() return EOF for various reasons, and
107 * we should not quit before seeing at least NR_OF_EOFS consecutive EOFs.
110 #define NR_OF_EOFS 20
113 static char erase_char, kill_char;
114 static boolean settty_needed = FALSE;
115 static struct termios inittyb, curttyb;
117 static void setctty(void);
120 * Get initial state of terminal, set ospeed (for termcap routines)
121 * and switch off tab expansion if necessary.
122 * Called by startup() in termcap.c and after returning from ! or ^Z
127 if (tcgetattr(0, &inittyb) < 0)
128 perror("Hack (gettty)");
130 ospeed = cfgetospeed(&inittyb);
131 erase_char = inittyb.c_cc[VERASE];
132 kill_char = inittyb.c_cc[VKILL];
135 /* do not expand tabs - they might be needed inside a cm sequence */
136 if (curttyb.c_oflag & OXTABS) {
137 curttyb.c_oflag &= ~OXTABS;
140 settty_needed = TRUE;
143 /* reset terminal to original state */
145 settty(const char *s)
151 (void) fflush(stdout);
152 if (tcsetattr(0, TCSADRAIN, &inittyb) < 0)
153 perror("Hack (settty)");
154 flags.echo = (inittyb.c_lflag & ECHO) ? ON : OFF;
155 flags.cbreak = (inittyb.c_lflag & ICANON) ? OFF : ON;
162 if (tcsetattr(0, TCSADRAIN, &curttyb) < 0)
163 perror("Hack (setctty)");
173 /* Should use (ECHO|CRMOD) here instead of ECHO */
174 if (curttyb.c_lflag & ECHO) {
175 curttyb.c_lflag &= ~ECHO;
178 if (curttyb.c_lflag & ICANON) {
179 curttyb.c_lflag &= ~ICANON;
180 /* be satisfied with one character; no timeout */
181 curttyb.c_cc[VMIN] = 1;
182 curttyb.c_cc[VTIME] = 0;
195 error(const char *fmt, ...)
209 * Read a line closed with '\n' into the array char bufp[BUFSZ].
210 * (The '\n' is not stored. The string is closed with a '\0'.)
211 * Reading can be interrupted by an escape ('\033') - now the
212 * resulting string is "\033".
220 flags.toplin = 2; /* nonempty, no --More-- required */
222 (void) fflush(stdout);
223 if ((c = getchar()) == EOF) {
232 if (c == erase_char || c == '\b') {
235 putstr("\b \b"); /* putsym converts \b */
238 } else if (c == '\n') {
241 } else if (' ' <= c && c < '\177') {
243 * avoid isprint() - some people don't have it ' ' is
244 * not always a printing char
249 if (bufp - obufp < BUFSZ - 1 && bufp - obufp < COLNO)
251 } else if (c == kill_char || c == '\177') { /* Robert Viduya */
252 /* this test last - @ might be the kill_char */
253 while (bufp != obufp) {
269 cgetret(const char *s)
275 putstr(flags.cbreak ? "space" : "return");
276 putstr(" to continue: ");
282 char morc; /* tell the outside world what char he used */
284 /* s = chars allowed besides space or return */
286 xwaitforspace(const char *s)
292 while ((c = readchar()) != '\n') {
296 if (s && strchr(s, c)) {
308 static char inputline[COLNO];
316 while ((foo = readchar()) >= '0' && foo <= '9')
317 multi = 10 * multi + foo - '0';
324 if (foo == 'f' || foo == 'F') {
325 inputline[1] = getchar();
327 if (inputline[1] == foo)
328 inputline[2] = getchar();
333 if (foo == 'm' || foo == 'M') {
334 inputline[1] = getchar();
346 (void) fflush(stdout);
347 if ((sym = getchar()) == EOF)
350 * Some SYSV systems seem to return EOFs for various reasons
351 * (?like when one hits break or for interrupted systemcalls?),
352 * and we must see several before we quit.
354 int cnt = NR_OF_EOFS;
356 clearerr(stdin); /* omit if clearerr is
358 if ((sym = getchar()) != EOF)
366 #endif /* NR_OF_EOFS */
367 if (flags.toplin == 1)
375 settty("End of input?\n");