]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - hack/hack.tty.c
1 /* $NetBSD: hack.tty.c,v 1.6 1997/10/19 16:59:17 christos 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.6 1997/10/19 16:59:17 christos Exp $");
45 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
46 /* hack.tty.c - version 1.0.3 */
48 * With thanks to the people who sent code for SYSV - hpscdi!jon,
49 * arnold@ucsf-cgl, wcs@bo95b, cbcephus!pds and others.
57 * Some systems may have getchar() return EOF for various reasons, and
58 * we should not quit before seeing at least NR_OF_EOFS consecutive EOFs.
64 static char erase_char
, kill_char
;
65 static boolean settty_needed
= FALSE
;
66 struct termios inittyb
, curttyb
;
69 * Get initial state of terminal, set ospeed (for termcap routines)
70 * and switch off tab expansion if necessary.
71 * Called by startup() in termcap.c and after returning from ! or ^Z
76 extern speed_t ospeed
;
77 if (tcgetattr(0, &inittyb
) < 0)
78 perror("Hack (gettty)");
80 ospeed
= cfgetospeed(&inittyb
);
81 erase_char
= inittyb
.c_cc
[VERASE
];
82 kill_char
= inittyb
.c_cc
[VKILL
];
85 /* do not expand tabs - they might be needed inside a cm sequence */
86 if (curttyb
.c_oflag
& OXTABS
) {
87 curttyb
.c_oflag
&= ~OXTABS
;
93 /* reset terminal to original state */
102 (void) fflush(stdout
);
103 if (tcsetattr(0, TCSADRAIN
, &inittyb
) < 0)
104 perror("Hack (settty)");
105 flags
.echo
= (inittyb
.c_lflag
& ECHO
) ? ON
: OFF
;
106 flags
.cbreak
= (inittyb
.c_lflag
& ICANON
) ? OFF
: ON
;
113 if (tcsetattr(0, TCSADRAIN
, &curttyb
) < 0)
114 perror("Hack (setctty)");
124 /* Should use (ECHO|CRMOD) here instead of ECHO */
125 if (curttyb
.c_lflag
& ECHO
) {
126 curttyb
.c_lflag
&= ~ECHO
;
129 if (curttyb
.c_lflag
& ICANON
) {
130 curttyb
.c_lflag
&= ~ICANON
;
131 /* be satisfied with one character; no timeout */
132 curttyb
.c_cc
[VMIN
] = 1;
133 curttyb
.c_cc
[VTIME
] = 0;
147 error(const char *fmt
, ...)
158 fmt
= va_arg(ap
, const char *);
171 * Read a line closed with '\n' into the array char bufp[BUFSZ].
172 * (The '\n' is not stored. The string is closed with a '\0'.)
173 * Reading can be interrupted by an escape ('\033') - now the
174 * resulting string is "\033".
183 flags
.toplin
= 2; /* nonempty, no --More-- required */
185 (void) fflush(stdout
);
186 if ((c
= getchar()) == EOF
) {
195 if (c
== erase_char
|| c
== '\b') {
198 putstr("\b \b"); /* putsym converts \b */
201 } else if (c
== '\n') {
204 } else if (' ' <= c
&& c
< '\177') {
206 * avoid isprint() - some people don't have it ' ' is
207 * not always a printing char
212 if (bufp
- obufp
< BUFSZ
- 1 && bufp
- obufp
< COLNO
)
214 } else if (c
== kill_char
|| c
== '\177') { /* Robert Viduya */
215 /* this test last - @ might be the kill_char */
216 while (bufp
!= obufp
) {
239 putstr(flags
.cbreak
? "space" : "return");
240 putstr(" to continue: ");
246 char morc
; /* tell the outside world what char he used */
250 char *s
; /* chars allowed besides space or return */
256 while ((c
= readchar()) != '\n') {
260 if (s
&& strchr(s
, c
)) {
272 static char inputline
[COLNO
];
280 while ((foo
= readchar()) >= '0' && foo
<= '9')
281 multi
= 10 * multi
+ foo
- '0';
288 if (foo
== 'f' || foo
== 'F') {
289 inputline
[1] = getchar();
291 if (inputline
[1] == foo
)
292 inputline
[2] = getchar();
297 if (foo
== 'm' || foo
== 'M') {
298 inputline
[1] = getchar();
310 (void) fflush(stdout
);
311 if ((sym
= getchar()) == EOF
)
314 * Some SYSV systems seem to return EOFs for various reasons
315 * (?like when one hits break or for interrupted systemcalls?),
316 * and we must see several before we quit.
318 int cnt
= NR_OF_EOFS
;
320 clearerr(stdin
); /* omit if clearerr is
322 if ((sym
= getchar()) != EOF
)
330 #endif /* NR_OF_EOFS */
331 if (flags
.toplin
== 1)
339 settty("End of input?\n");