1 /* $NetBSD: hack.tty.c,v 1.13 2009/06/07 18:30:39 dholland Exp $ */
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)hack.tty.c 8.1 (Berkeley) 5/31/93";
37 __RCSID("$NetBSD: hack.tty.c,v 1.13 2009/06/07 18:30:39 dholland Exp $");
42 * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
44 * All rights reserved.
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions are
50 * - Redistributions of source code must retain the above copyright notice,
51 * this list of conditions and the following disclaimer.
53 * - Redistributions in binary form must reproduce the above copyright
54 * notice, this list of conditions and the following disclaimer in the
55 * documentation and/or other materials provided with the distribution.
57 * - Neither the name of the Stichting Centrum voor Wiskunde en
58 * Informatica, nor the names of its contributors may be used to endorse or
59 * promote products derived from this software without specific prior
62 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
63 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
64 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
65 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
66 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
67 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
68 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
69 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
70 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
71 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
72 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
76 * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
77 * All rights reserved.
79 * Redistribution and use in source and binary forms, with or without
80 * modification, are permitted provided that the following conditions
82 * 1. Redistributions of source code must retain the above copyright
83 * notice, this list of conditions and the following disclaimer.
84 * 2. Redistributions in binary form must reproduce the above copyright
85 * notice, this list of conditions and the following disclaimer in the
86 * documentation and/or other materials provided with the distribution.
87 * 3. The name of the author may not be used to endorse or promote products
88 * derived from this software without specific prior written permission.
90 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
91 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
92 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
93 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
94 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
95 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
96 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
97 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
98 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
99 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
102 /* hack.tty.c - version 1.0.3 */
104 * With thanks to the people who sent code for SYSV - hpscdi!jon,
105 * arnold@ucsf-cgl, wcs@bo95b, cbcephus!pds and others.
114 * Some systems may have getchar() return EOF for various reasons, and
115 * we should not quit before seeing at least NR_OF_EOFS consecutive EOFs.
118 #define NR_OF_EOFS 20
121 static char erase_char
, kill_char
;
122 static boolean settty_needed
= FALSE
;
123 static struct termios inittyb
, curttyb
;
125 static void setctty(void);
128 * Get initial state of terminal, set ospeed (for termcap routines)
129 * and switch off tab expansion if necessary.
130 * Called by startup() in termcap.c and after returning from ! or ^Z
135 if (tcgetattr(0, &inittyb
) < 0)
136 perror("Hack (gettty)");
138 ospeed
= cfgetospeed(&inittyb
);
139 erase_char
= inittyb
.c_cc
[VERASE
];
140 kill_char
= inittyb
.c_cc
[VKILL
];
143 /* do not expand tabs - they might be needed inside a cm sequence */
144 if (curttyb
.c_oflag
& OXTABS
) {
145 curttyb
.c_oflag
&= ~OXTABS
;
148 settty_needed
= TRUE
;
151 /* reset terminal to original state */
153 settty(const char *s
)
159 (void) fflush(stdout
);
160 if (tcsetattr(0, TCSADRAIN
, &inittyb
) < 0)
161 perror("Hack (settty)");
162 flags
.echo
= (inittyb
.c_lflag
& ECHO
) ? ON
: OFF
;
163 flags
.cbreak
= (inittyb
.c_lflag
& ICANON
) ? OFF
: ON
;
170 if (tcsetattr(0, TCSADRAIN
, &curttyb
) < 0)
171 perror("Hack (setctty)");
181 /* Should use (ECHO|CRMOD) here instead of ECHO */
182 if (curttyb
.c_lflag
& ECHO
) {
183 curttyb
.c_lflag
&= ~ECHO
;
186 if (curttyb
.c_lflag
& ICANON
) {
187 curttyb
.c_lflag
&= ~ICANON
;
188 /* be satisfied with one character; no timeout */
189 curttyb
.c_cc
[VMIN
] = 1;
190 curttyb
.c_cc
[VTIME
] = 0;
203 error(const char *fmt
, ...)
217 * Read a line closed with '\n' into the array char bufp[BUFSZ].
218 * (The '\n' is not stored. The string is closed with a '\0'.)
219 * Reading can be interrupted by an escape ('\033') - now the
220 * resulting string is "\033".
228 flags
.toplin
= 2; /* nonempty, no --More-- required */
230 (void) fflush(stdout
);
231 if ((c
= getchar()) == EOF
) {
240 if (c
== erase_char
|| c
== '\b') {
243 putstr("\b \b"); /* putsym converts \b */
246 } else if (c
== '\n') {
249 } else if (' ' <= c
&& c
< '\177') {
251 * avoid isprint() - some people don't have it ' ' is
252 * not always a printing char
257 if (bufp
- obufp
< BUFSZ
- 1 && bufp
- obufp
< COLNO
)
259 } else if (c
== kill_char
|| c
== '\177') { /* Robert Viduya */
260 /* this test last - @ might be the kill_char */
261 while (bufp
!= obufp
) {
277 cgetret(const char *s
)
283 putstr(flags
.cbreak
? "space" : "return");
284 putstr(" to continue: ");
290 char morc
; /* tell the outside world what char he used */
292 /* s = chars allowed besides space or return */
294 xwaitforspace(const char *s
)
300 while ((c
= readchar()) != '\n') {
304 if (s
&& strchr(s
, c
)) {
316 static char inputline
[COLNO
];
324 while ((foo
= readchar()) >= '0' && foo
<= '9')
325 multi
= 10 * multi
+ foo
- '0';
332 if (foo
== 'f' || foo
== 'F') {
333 inputline
[1] = getchar();
335 if (inputline
[1] == foo
)
336 inputline
[2] = getchar();
341 if (foo
== 'm' || foo
== 'M') {
342 inputline
[1] = getchar();
354 (void) fflush(stdout
);
355 if ((sym
= getchar()) == EOF
)
358 * Some SYSV systems seem to return EOFs for various reasons
359 * (?like when one hits break or for interrupted systemcalls?),
360 * and we must see several before we quit.
362 int cnt
= NR_OF_EOFS
;
364 clearerr(stdin
); /* omit if clearerr is
366 if ((sym
= getchar()) != EOF
)
374 #endif /* NR_OF_EOFS */
375 if (flags
.toplin
== 1)
383 settty("End of input?\n");