Merge branch 'vendor/LESS' into less_update
[dragonfly.git] / games / hack / hack.tty.c
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.
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 $
35  * $DragonFly: src/games/hack/hack.tty.c,v 1.4 2006/08/21 19:45:32 pavalos Exp $
36  */
37
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
43 #include <termios.h>
44 #include "hack.h"
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.
49  * FIXME: is this still valid nowadays?
50  */
51 #define NR_OF_EOFS      20
52
53 static char erase_char, kill_char;
54 static boolean settty_needed = FALSE;
55 struct termios inittyb, curttyb;
56
57 static void     setctty(void);
58
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  */
64 void
65 gettty(void)
66 {
67         if(tcgetattr(fileno(stdin),&inittyb) < 0)
68                 perror("Hack (gettty)");
69         curttyb = inittyb;
70         erase_char = inittyb.c_cc[VERASE];
71         kill_char = inittyb.c_cc[VKILL];
72         getioctls();
73
74         /* do not expand tabs - they might be needed inside a cm sequence */
75         if(curttyb.c_oflag & OXTABS) {
76                 curttyb.c_oflag &= ~OXTABS;
77                 setctty();
78         }
79         settty_needed = TRUE;
80 }
81
82 /* reset terminal to original state */
83 void
84 settty(const char *s)
85 {
86         clear_screen();
87         end_screen();
88         if(s) printf("%s", s);
89         fflush(stdout);
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;
94         setioctls();
95 }
96
97 static void
98 setctty(void)
99 {
100         if(tcsetattr(fileno(stdin),TCSANOW,&curttyb) < 0)
101                 perror("Hack (setctty)");
102 }
103
104 void
105 setftty(void)
106 {
107 u_long ef = 0;                  /* desired value of flags & ECHO */
108 u_long cf = !(ICANON);          /* desired value of flags & CBREAK */
109 int change = 0;
110         flags.cbreak = ON;
111         flags.echo = OFF;
112         /* Should use (ECHO|CRMOD) here instead of ECHO */
113         if((curttyb.c_lflag & ECHO) != ef){
114                 curttyb.c_lflag &= ~ECHO;
115                 change++;
116         }
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 */
123                 change++;
124         }
125         if(change){
126                 setctty();
127         }
128         start_screen();
129 }
130
131
132 /* fatal error */
133 /*VARARGS1*/
134 void
135 error(const char *s, ...)
136 {
137         va_list ap;
138         if(settty_needed)
139                 settty(NULL);
140         va_start(ap, s);
141         vprintf(s, ap);
142         va_end(ap);
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  */
153 void
154 getlin(char *bufp)
155 {
156         char *obufp = bufp;
157         int c;
158
159         flags.toplin = 2;               /* nonempty, no --More-- required */
160         for(;;) {
161                 fflush(stdout);
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
198 void
199 getret(void)
200 {
201         cgetret("");
202 }
203
204 void
205 cgetret(const char *s)
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
218 char morc;      /* tell the outside world what char he used */
219
220 void
221 xwaitforspace(const char *s)    /* chars allowed besides space or return */
222 {
223 int 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
239 char *
240 parse(void)
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
259 #endif /* QUEST */
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
270 char
271 readchar(void)
272 {
273         int sym;
274
275         fflush(stdout);
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();
293 #endif /* NR_OF_EOFS */
294         if(flags.toplin == 1)
295                 flags.toplin = 2;
296         return((char) sym);
297 }
298
299 void
300 end_of_input(void)
301 {
302         settty("End of input?\n");
303         clearlocks();
304         exit(0);
305 }