2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)hack.tty.c 8.1 (Berkeley) 5/31/93
34 * $FreeBSD: src/games/hack/hack.tty.c,v 1.6.2.1 2000/07/20 10:35:07 kris Exp $
35 * $DragonFly: src/games/hack/hack.tty.c,v 1.2 2003/06/17 04:25:24 dillon Exp $
38 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
39 /* hack.tty.c - version 1.0.3 */
40 /* With thanks to the people who sent code for SYSV - hpscdi!jon,
41 arnold@ucsf-cgl, wcs@bo95b, cbcephus!pds and others. */
47 * The distinctions here are not BSD - rest but rather USG - rest, as
48 * BSD still has the old sgttyb structure, but SYSV has termio. Thus:
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.
68 #define termstruct termio
69 #define kill_sym c_cc[VKILL]
70 #define erase_sym c_cc[VERASE]
72 #define tabflgs c_oflag
73 #define echoflgs c_lflag
74 #define cbrkflgs c_lflag
75 #define CBRKMASK ICANON
76 #define CBRKON ! /* reverse condition */
77 #define OSPEED(x) ((x).c_cflag & CBAUD)
78 #define GTTY(x) (ioctl(0, TCGETA, x))
79 #define STTY(x) (ioctl(0, TCSETA, x)) /* TCSETAF? TCSETAW? */
84 #define termstruct sgttyb
85 #define kill_sym sg_kill
86 #define erase_sym sg_erase
88 #define tabflgs sg_flags
89 #define echoflgs sg_flags
90 #define cbrkflgs sg_flags
91 #define CBRKMASK CBREAK
92 #define CBRKON /* empty */
93 #define OSPEED(x) (x).sg_ospeed
94 #define GTTY(x) (gtty(0, x))
95 #define STTY(x) (stty(0, x))
102 static char erase_char, kill_char;
103 static boolean settty_needed = FALSE;
104 struct termstruct inittyb, curttyb;
107 * Get initial state of terminal, set ospeed (for termcap routines)
108 * and switch off tab expansion if necessary.
109 * Called by startup() in termcap.c and after returning from ! or ^Z
112 if(GTTY(&inittyb) < 0)
113 perror("Hack (gettty)");
116 ospeed = OSPEED(inittyb);
118 erase_char = inittyb.erase_sym;
119 kill_char = inittyb.kill_sym;
122 /* do not expand tabs - they might be needed inside a cm sequence */
123 if(curttyb.tabflgs & EXTABS) {
124 curttyb.tabflgs &= ~EXTABS;
127 settty_needed = TRUE;
130 /* reset terminal to original state */
134 if(s) printf("%s", s);
135 (void) fflush(stdout);
136 if(STTY(&inittyb) < 0)
137 perror("Hack (settty)");
138 flags.echo = (inittyb.echoflgs & ECHO) ? ON : OFF;
139 flags.cbreak = (CBRKON(inittyb.cbrkflgs & CBRKMASK)) ? ON : OFF;
144 if(STTY(&curttyb) < 0)
145 perror("Hack (setctty)");
150 int ef = 0; /* desired value of flags & ECHO */
151 int cf = CBRKON(CBRKMASK); /* desired value of flags & CBREAK */
155 /* Should use (ECHO|CRMOD) here instead of ECHO */
156 if((curttyb.echoflgs & ECHO) != ef){
157 curttyb.echoflgs &= ~ECHO;
158 /* curttyb.echoflgs |= ef; */
161 if((curttyb.cbrkflgs & CBRKMASK) != cf){
162 curttyb.cbrkflgs &= ~CBRKMASK;
163 curttyb.cbrkflgs |= cf;
165 /* be satisfied with one character; no timeout */
166 curttyb.c_cc[VMIN] = 1; /* was VEOF */
167 curttyb.c_cc[VTIME] = 0; /* was VEOL */
180 error(s,x,y) char *s; {
189 * Read a line closed with '\n' into the array char bufp[BUFSZ].
190 * (The '\n' is not stored. The string is closed with a '\0'.)
191 * Reading can be interrupted by an escape ('\033') - now the
192 * resulting string is "\033".
200 flags.toplin = 2; /* nonempty, no --More-- required */
202 (void) fflush(stdout);
203 if((c = getchar()) == EOF) {
212 if(c == erase_char || c == '\b') {
215 putstr("\b \b"); /* putsym converts \b */
217 } else if(c == '\n') {
220 } else if(' ' <= c && c < '\177') {
221 /* avoid isprint() - some people don't have it
222 ' ' is not always a printing char */
226 if(bufp-obufp < BUFSZ-1 && bufp-obufp < COLNO)
228 } else if(c == kill_char || c == '\177') { /* Robert Viduya */
229 /* this test last - @ might be the kill_char */
230 while(bufp != obufp) {
250 putstr(flags.cbreak ? "space" : "return");
251 putstr(" to continue: ");
257 char morc; /* tell the outside world what char he used */
260 char *s; /* chars allowed besides space or return */
266 while((c = readchar()) != '\n') {
269 if(s && index(s,c)) {
281 static char inputline[COLNO];
285 if(!Invisible) curs_on_u(); else home();
286 while((foo = readchar()) >= '0' && foo <= '9')
287 multi = 10*multi+foo-'0';
294 if(foo == 'f' || foo == 'F'){
295 inputline[1] = getchar();
297 if(inputline[1] == foo) inputline[2] = getchar(); else
301 if(foo == 'm' || foo == 'M'){
302 inputline[1] = getchar();
313 (void) fflush(stdout);
314 if((sym = getchar()) == EOF)
317 * Some SYSV systems seem to return EOFs for various reasons
318 * (?like when one hits break or for interrupted systemcalls?),
319 * and we must see several before we quit.
321 int cnt = NR_OF_EOFS;
323 clearerr(stdin); /* omit if clearerr is undefined */
324 if((sym = getchar()) != EOF) goto noteof;
332 if(flags.toplin == 1)
339 settty("End of input?\n");