]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - hack/hack.tty.c
1 /* $NetBSD: hack.tty.c,v 1.11 2003/04/02 18:36:41 jsm 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
39 static char sccsid
[] = "@(#)hack.tty.c 8.1 (Berkeley) 5/31/93";
41 __RCSID("$NetBSD: hack.tty.c,v 1.11 2003/04/02 18:36:41 jsm Exp $");
46 * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
48 * All rights reserved.
50 * Redistribution and use in source and binary forms, with or without
51 * modification, are permitted provided that the following conditions are
54 * - Redistributions of source code must retain the above copyright notice,
55 * this list of conditions and the following disclaimer.
57 * - Redistributions in binary form must reproduce the above copyright
58 * notice, this list of conditions and the following disclaimer in the
59 * documentation and/or other materials provided with the distribution.
61 * - Neither the name of the Stichting Centrum voor Wiskunde en
62 * Informatica, nor the names of its contributors may be used to endorse or
63 * promote products derived from this software without specific prior
66 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
67 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
68 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
69 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
70 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
71 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
72 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
73 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
74 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
75 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
76 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
80 * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
81 * All rights reserved.
83 * Redistribution and use in source and binary forms, with or without
84 * modification, are permitted provided that the following conditions
86 * 1. Redistributions of source code must retain the above copyright
87 * notice, this list of conditions and the following disclaimer.
88 * 2. Redistributions in binary form must reproduce the above copyright
89 * notice, this list of conditions and the following disclaimer in the
90 * documentation and/or other materials provided with the distribution.
91 * 3. The name of the author may not be used to endorse or promote products
92 * derived from this software without specific prior written permission.
94 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
95 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
96 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
97 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
98 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
99 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
100 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
101 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
102 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
103 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
106 /* hack.tty.c - version 1.0.3 */
108 * With thanks to the people who sent code for SYSV - hpscdi!jon,
109 * arnold@ucsf-cgl, wcs@bo95b, cbcephus!pds and others.
118 * Some systems may have getchar() return EOF for various reasons, and
119 * we should not quit before seeing at least NR_OF_EOFS consecutive EOFs.
122 #define NR_OF_EOFS 20
125 static char erase_char
, kill_char
;
126 static boolean settty_needed
= FALSE
;
127 struct termios inittyb
, curttyb
;
130 * Get initial state of terminal, set ospeed (for termcap routines)
131 * and switch off tab expansion if necessary.
132 * Called by startup() in termcap.c and after returning from ! or ^Z
137 if (tcgetattr(0, &inittyb
) < 0)
138 perror("Hack (gettty)");
140 ospeed
= cfgetospeed(&inittyb
);
141 erase_char
= inittyb
.c_cc
[VERASE
];
142 kill_char
= inittyb
.c_cc
[VKILL
];
145 /* do not expand tabs - they might be needed inside a cm sequence */
146 if (curttyb
.c_oflag
& OXTABS
) {
147 curttyb
.c_oflag
&= ~OXTABS
;
150 settty_needed
= TRUE
;
153 /* reset terminal to original state */
162 (void) fflush(stdout
);
163 if (tcsetattr(0, TCSADRAIN
, &inittyb
) < 0)
164 perror("Hack (settty)");
165 flags
.echo
= (inittyb
.c_lflag
& ECHO
) ? ON
: OFF
;
166 flags
.cbreak
= (inittyb
.c_lflag
& ICANON
) ? OFF
: ON
;
173 if (tcsetattr(0, TCSADRAIN
, &curttyb
) < 0)
174 perror("Hack (setctty)");
184 /* Should use (ECHO|CRMOD) here instead of ECHO */
185 if (curttyb
.c_lflag
& ECHO
) {
186 curttyb
.c_lflag
&= ~ECHO
;
189 if (curttyb
.c_lflag
& ICANON
) {
190 curttyb
.c_lflag
&= ~ICANON
;
191 /* be satisfied with one character; no timeout */
192 curttyb
.c_cc
[VMIN
] = 1;
193 curttyb
.c_cc
[VTIME
] = 0;
206 error(const char *fmt
, ...)
220 * Read a line closed with '\n' into the array char bufp[BUFSZ].
221 * (The '\n' is not stored. The string is closed with a '\0'.)
222 * Reading can be interrupted by an escape ('\033') - now the
223 * resulting string is "\033".
232 flags
.toplin
= 2; /* nonempty, no --More-- required */
234 (void) fflush(stdout
);
235 if ((c
= getchar()) == EOF
) {
244 if (c
== erase_char
|| c
== '\b') {
247 putstr("\b \b"); /* putsym converts \b */
250 } else if (c
== '\n') {
253 } else if (' ' <= c
&& c
< '\177') {
255 * avoid isprint() - some people don't have it ' ' is
256 * not always a printing char
261 if (bufp
- obufp
< BUFSZ
- 1 && bufp
- obufp
< COLNO
)
263 } else if (c
== kill_char
|| c
== '\177') { /* Robert Viduya */
264 /* this test last - @ might be the kill_char */
265 while (bufp
!= obufp
) {
288 putstr(flags
.cbreak
? "space" : "return");
289 putstr(" to continue: ");
295 char morc
; /* tell the outside world what char he used */
299 const char *s
; /* chars allowed besides space or return */
305 while ((c
= readchar()) != '\n') {
309 if (s
&& strchr(s
, c
)) {
321 static char inputline
[COLNO
];
329 while ((foo
= readchar()) >= '0' && foo
<= '9')
330 multi
= 10 * multi
+ foo
- '0';
337 if (foo
== 'f' || foo
== 'F') {
338 inputline
[1] = getchar();
340 if (inputline
[1] == foo
)
341 inputline
[2] = getchar();
346 if (foo
== 'm' || foo
== 'M') {
347 inputline
[1] = getchar();
359 (void) fflush(stdout
);
360 if ((sym
= getchar()) == EOF
)
363 * Some SYSV systems seem to return EOFs for various reasons
364 * (?like when one hits break or for interrupted systemcalls?),
365 * and we must see several before we quit.
367 int cnt
= NR_OF_EOFS
;
369 clearerr(stdin
); /* omit if clearerr is
371 if ((sym
= getchar()) != EOF
)
379 #endif /* NR_OF_EOFS */
380 if (flags
.toplin
== 1)
388 settty("End of input?\n");