backgammon(6)/hack(6)/larn(6): replace sgtty with termios
[dragonfly.git] / games / hack / hack.tty.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
1de703da
MD
32 *
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 $
c7106d58 35 * $DragonFly: src/games/hack/hack.tty.c,v 1.4 2006/08/21 19:45:32 pavalos Exp $
984263bc
MD
36 */
37
984263bc
MD
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. */
42
a579280a
SW
43#include <termios.h>
44#include "hack.h"
984263bc
MD
45
46/*
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.
a579280a 49 * FIXME: is this still valid nowadays?
984263bc 50 */
984263bc 51#define NR_OF_EOFS 20
984263bc 52
984263bc
MD
53static char erase_char, kill_char;
54static boolean settty_needed = FALSE;
a579280a 55struct termios inittyb, curttyb;
984263bc 56
c7106d58
PA
57static void setctty(void);
58
984263bc
MD
59/*
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
63 */
c7106d58
PA
64void
65gettty(void)
66{
a579280a 67 if(tcgetattr(fileno(stdin),&inittyb) < 0)
984263bc
MD
68 perror("Hack (gettty)");
69 curttyb = inittyb;
a579280a
SW
70 erase_char = inittyb.c_cc[VERASE];
71 kill_char = inittyb.c_cc[VKILL];
984263bc
MD
72 getioctls();
73
74 /* do not expand tabs - they might be needed inside a cm sequence */
a579280a
SW
75 if(curttyb.c_oflag & OXTABS) {
76 curttyb.c_oflag &= ~OXTABS;
984263bc
MD
77 setctty();
78 }
79 settty_needed = TRUE;
80}
81
82/* reset terminal to original state */
c7106d58
PA
83void
84settty(const char *s)
85{
984263bc
MD
86 clear_screen();
87 end_screen();
88 if(s) printf("%s", s);
c7106d58 89 fflush(stdout);
a579280a 90 if(tcsetattr(fileno(stdin),TCSANOW,&inittyb) < 0)
984263bc 91 perror("Hack (settty)");
a579280a
SW
92 flags.echo = (inittyb.c_lflag & ECHO) ? ON : OFF;
93 flags.cbreak = (!(inittyb.c_lflag & ICANON)) ? ON : OFF;
984263bc
MD
94 setioctls();
95}
96
c7106d58
PA
97static void
98setctty(void)
99{
a579280a 100 if(tcsetattr(fileno(stdin),TCSANOW,&curttyb) < 0)
984263bc
MD
101 perror("Hack (setctty)");
102}
103
c7106d58
PA
104void
105setftty(void)
106{
a579280a
SW
107u_long ef = 0; /* desired value of flags & ECHO */
108u_long cf = !(ICANON); /* desired value of flags & CBREAK */
984263bc
MD
109int change = 0;
110 flags.cbreak = ON;
111 flags.echo = OFF;
112 /* Should use (ECHO|CRMOD) here instead of ECHO */
a579280a
SW
113 if((curttyb.c_lflag & ECHO) != ef){
114 curttyb.c_lflag &= ~ECHO;
984263bc
MD
115 change++;
116 }
a579280a
SW
117 if((curttyb.c_lflag & ICANON) != cf){
118 curttyb.c_lflag &= ~ICANON;
119 curttyb.c_lflag |= cf;
984263bc
MD
120 /* be satisfied with one character; no timeout */
121 curttyb.c_cc[VMIN] = 1; /* was VEOF */
122 curttyb.c_cc[VTIME] = 0; /* was VEOL */
984263bc
MD
123 change++;
124 }
125 if(change){
126 setctty();
127 }
128 start_screen();
129}
130
131
132/* fatal error */
133/*VARARGS1*/
c7106d58
PA
134void
135error(const char *s, ...)
136{
137 va_list ap;
984263bc 138 if(settty_needed)
902ec341 139 settty(NULL);
c7106d58
PA
140 va_start(ap, s);
141 vprintf(s, ap);
142 va_end(ap);
984263bc
MD
143 putchar('\n');
144 exit(1);
145}
146
147/*
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".
152 */
c7106d58
PA
153void
154getlin(char *bufp)
984263bc
MD
155{
156 char *obufp = bufp;
157 int c;
158
159 flags.toplin = 2; /* nonempty, no --More-- required */
160 for(;;) {
c7106d58 161 fflush(stdout);
984263bc
MD
162 if((c = getchar()) == EOF) {
163 *bufp = 0;
164 return;
165 }
166 if(c == '\033') {
167 *obufp = c;
168 obufp[1] = 0;
169 return;
170 }
171 if(c == erase_char || c == '\b') {
172 if(bufp != obufp) {
173 bufp--;
174 putstr("\b \b"); /* putsym converts \b */
175 } else bell();
176 } else if(c == '\n') {
177 *bufp = 0;
178 return;
179 } else if(' ' <= c && c < '\177') {
180 /* avoid isprint() - some people don't have it
181 ' ' is not always a printing char */
182 *bufp = c;
183 bufp[1] = 0;
184 putstr(bufp);
185 if(bufp-obufp < BUFSZ-1 && bufp-obufp < COLNO)
186 bufp++;
187 } else if(c == kill_char || c == '\177') { /* Robert Viduya */
188 /* this test last - @ might be the kill_char */
189 while(bufp != obufp) {
190 bufp--;
191 putstr("\b \b");
192 }
193 } else
194 bell();
195 }
196}
197
c7106d58
PA
198void
199getret(void)
200{
984263bc
MD
201 cgetret("");
202}
203
c7106d58
PA
204void
205cgetret(const char *s)
984263bc
MD
206{
207 putsym('\n');
208 if(flags.standout)
209 standoutbeg();
210 putstr("Hit ");
211 putstr(flags.cbreak ? "space" : "return");
212 putstr(" to continue: ");
213 if(flags.standout)
214 standoutend();
215 xwaitforspace(s);
216}
217
218char morc; /* tell the outside world what char he used */
219
c7106d58
PA
220void
221xwaitforspace(const char *s) /* chars allowed besides space or return */
984263bc
MD
222{
223int c;
224
225 morc = 0;
226
227 while((c = readchar()) != '\n') {
228 if(flags.cbreak) {
229 if(c == ' ') break;
230 if(s && index(s,c)) {
231 morc = c;
232 break;
233 }
234 bell();
235 }
236 }
237}
238
239char *
c7106d58 240parse(void)
984263bc
MD
241{
242 static char inputline[COLNO];
243 int foo;
244
245 flags.move = 1;
246 if(!Invisible) curs_on_u(); else home();
247 while((foo = readchar()) >= '0' && foo <= '9')
248 multi = 10*multi+foo-'0';
249 if(multi) {
250 multi--;
251 save_cm = inputline;
252 }
253 inputline[0] = foo;
254 inputline[1] = 0;
255 if(foo == 'f' || foo == 'F'){
256 inputline[1] = getchar();
257#ifdef QUEST
258 if(inputline[1] == foo) inputline[2] = getchar(); else
304d60d9 259#endif /* QUEST */
984263bc
MD
260 inputline[2] = 0;
261 }
262 if(foo == 'm' || foo == 'M'){
263 inputline[1] = getchar();
264 inputline[2] = 0;
265 }
266 clrlin();
267 return(inputline);
268}
269
270char
c7106d58
PA
271readchar(void)
272{
984263bc
MD
273 int sym;
274
c7106d58 275 fflush(stdout);
984263bc
MD
276 if((sym = getchar()) == EOF)
277#ifdef NR_OF_EOFS
278 { /*
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.
282 */
283 int cnt = NR_OF_EOFS;
284 while (cnt--) {
285 clearerr(stdin); /* omit if clearerr is undefined */
286 if((sym = getchar()) != EOF) goto noteof;
287 }
288 end_of_input();
289 noteof: ;
290 }
291#else
292 end_of_input();
304d60d9 293#endif /* NR_OF_EOFS */
984263bc
MD
294 if(flags.toplin == 1)
295 flags.toplin = 2;
296 return((char) sym);
297}
298
c7106d58
PA
299void
300end_of_input(void)
984263bc
MD
301{
302 settty("End of input?\n");
303 clearlocks();
304 exit(0);
305}