]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - hack/hack.tty.c
1 /* $NetBSD: hack.tty.c,v 1.12 2003/08/07 09:37:19 agc 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.12 2003/08/07 09:37:19 agc 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 struct termios inittyb
, curttyb
;
126 * Get initial state of terminal, set ospeed (for termcap routines)
127 * and switch off tab expansion if necessary.
128 * Called by startup() in termcap.c and after returning from ! or ^Z
133 if (tcgetattr(0, &inittyb
) < 0)
134 perror("Hack (gettty)");
136 ospeed
= cfgetospeed(&inittyb
);
137 erase_char
= inittyb
.c_cc
[VERASE
];
138 kill_char
= inittyb
.c_cc
[VKILL
];
141 /* do not expand tabs - they might be needed inside a cm sequence */
142 if (curttyb
.c_oflag
& OXTABS
) {
143 curttyb
.c_oflag
&= ~OXTABS
;
146 settty_needed
= TRUE
;
149 /* reset terminal to original state */
158 (void) fflush(stdout
);
159 if (tcsetattr(0, TCSADRAIN
, &inittyb
) < 0)
160 perror("Hack (settty)");
161 flags
.echo
= (inittyb
.c_lflag
& ECHO
) ? ON
: OFF
;
162 flags
.cbreak
= (inittyb
.c_lflag
& ICANON
) ? OFF
: ON
;
169 if (tcsetattr(0, TCSADRAIN
, &curttyb
) < 0)
170 perror("Hack (setctty)");
180 /* Should use (ECHO|CRMOD) here instead of ECHO */
181 if (curttyb
.c_lflag
& ECHO
) {
182 curttyb
.c_lflag
&= ~ECHO
;
185 if (curttyb
.c_lflag
& ICANON
) {
186 curttyb
.c_lflag
&= ~ICANON
;
187 /* be satisfied with one character; no timeout */
188 curttyb
.c_cc
[VMIN
] = 1;
189 curttyb
.c_cc
[VTIME
] = 0;
202 error(const char *fmt
, ...)
216 * Read a line closed with '\n' into the array char bufp[BUFSZ].
217 * (The '\n' is not stored. The string is closed with a '\0'.)
218 * Reading can be interrupted by an escape ('\033') - now the
219 * 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
) {
284 putstr(flags
.cbreak
? "space" : "return");
285 putstr(" to continue: ");
291 char morc
; /* tell the outside world what char he used */
295 const char *s
; /* chars allowed besides space or return */
301 while ((c
= readchar()) != '\n') {
305 if (s
&& strchr(s
, c
)) {
317 static char inputline
[COLNO
];
325 while ((foo
= readchar()) >= '0' && foo
<= '9')
326 multi
= 10 * multi
+ foo
- '0';
333 if (foo
== 'f' || foo
== 'F') {
334 inputline
[1] = getchar();
336 if (inputline
[1] == foo
)
337 inputline
[2] = getchar();
342 if (foo
== 'm' || foo
== 'M') {
343 inputline
[1] = getchar();
355 (void) fflush(stdout
);
356 if ((sym
= getchar()) == EOF
)
359 * Some SYSV systems seem to return EOFs for various reasons
360 * (?like when one hits break or for interrupted systemcalls?),
361 * and we must see several before we quit.
363 int cnt
= NR_OF_EOFS
;
365 clearerr(stdin
); /* omit if clearerr is
367 if ((sym
= getchar()) != EOF
)
375 #endif /* NR_OF_EOFS */
376 if (flags
.toplin
== 1)
384 settty("End of input?\n");