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.4 2006/08/21 19:45:32 pavalos 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 * Some systems may have getchar() return EOF for various reasons, and
48 * we should not quit before seeing at least NR_OF_EOFS consecutive EOFs.
49 * FIXME: is this still valid nowadays?
53 static char erase_char, kill_char;
54 static boolean settty_needed = FALSE;
55 struct termios inittyb, curttyb;
57 static void setctty(void);
60 * Get initial state of terminal, set ospeed (for termcap routines)
61 * and switch off tab expansion if necessary.
62 * Called by startup() in termcap.c and after returning from ! or ^Z
67 if(tcgetattr(fileno(stdin),&inittyb) < 0)
68 perror("Hack (gettty)");
70 erase_char = inittyb.c_cc[VERASE];
71 kill_char = inittyb.c_cc[VKILL];
74 /* do not expand tabs - they might be needed inside a cm sequence */
75 if(curttyb.c_oflag & OXTABS) {
76 curttyb.c_oflag &= ~OXTABS;
82 /* reset terminal to original state */
88 if(s) printf("%s", s);
90 if(tcsetattr(fileno(stdin),TCSANOW,&inittyb) < 0)
91 perror("Hack (settty)");
92 flags.echo = (inittyb.c_lflag & ECHO) ? ON : OFF;
93 flags.cbreak = (!(inittyb.c_lflag & ICANON)) ? ON : OFF;
100 if(tcsetattr(fileno(stdin),TCSANOW,&curttyb) < 0)
101 perror("Hack (setctty)");
107 u_long ef = 0; /* desired value of flags & ECHO */
108 u_long cf = !(ICANON); /* desired value of flags & CBREAK */
112 /* Should use (ECHO|CRMOD) here instead of ECHO */
113 if((curttyb.c_lflag & ECHO) != ef){
114 curttyb.c_lflag &= ~ECHO;
117 if((curttyb.c_lflag & ICANON) != cf){
118 curttyb.c_lflag &= ~ICANON;
119 curttyb.c_lflag |= cf;
120 /* be satisfied with one character; no timeout */
121 curttyb.c_cc[VMIN] = 1; /* was VEOF */
122 curttyb.c_cc[VTIME] = 0; /* was VEOL */
135 error(const char *s, ...)
148 * Read a line closed with '\n' into the array char bufp[BUFSZ].
149 * (The '\n' is not stored. The string is closed with a '\0'.)
150 * Reading can be interrupted by an escape ('\033') - now the
151 * resulting string is "\033".
159 flags.toplin = 2; /* nonempty, no --More-- required */
162 if((c = getchar()) == EOF) {
171 if(c == erase_char || c == '\b') {
174 putstr("\b \b"); /* putsym converts \b */
176 } else if(c == '\n') {
179 } else if(' ' <= c && c < '\177') {
180 /* avoid isprint() - some people don't have it
181 ' ' is not always a printing char */
185 if(bufp-obufp < BUFSZ-1 && bufp-obufp < COLNO)
187 } else if(c == kill_char || c == '\177') { /* Robert Viduya */
188 /* this test last - @ might be the kill_char */
189 while(bufp != obufp) {
205 cgetret(const char *s)
211 putstr(flags.cbreak ? "space" : "return");
212 putstr(" to continue: ");
218 char morc; /* tell the outside world what char he used */
221 xwaitforspace(const char *s) /* chars allowed besides space or return */
227 while((c = readchar()) != '\n') {
230 if(s && index(s,c)) {
242 static char inputline[COLNO];
246 if(!Invisible) curs_on_u(); else home();
247 while((foo = readchar()) >= '0' && foo <= '9')
248 multi = 10*multi+foo-'0';
255 if(foo == 'f' || foo == 'F'){
256 inputline[1] = getchar();
258 if(inputline[1] == foo) inputline[2] = getchar(); else
262 if(foo == 'm' || foo == 'M'){
263 inputline[1] = getchar();
276 if((sym = getchar()) == EOF)
279 * Some SYSV systems seem to return EOFs for various reasons
280 * (?like when one hits break or for interrupted systemcalls?),
281 * and we must see several before we quit.
283 int cnt = NR_OF_EOFS;
285 clearerr(stdin); /* omit if clearerr is undefined */
286 if((sym = getchar()) != EOF) goto noteof;
293 #endif /* NR_OF_EOFS */
294 if(flags.toplin == 1)
302 settty("End of input?\n");