hack.tty.c revision 1.14 1 1.14 dholland /* $NetBSD: hack.tty.c,v 1.14 2009/08/12 07:28:41 dholland Exp $ */
2 1.6 christos
3 1.1 cgd /*-
4 1.3 cgd * Copyright (c) 1988, 1993
5 1.3 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.12 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.6 christos #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.3 cgd #if 0
35 1.6 christos static char sccsid[] = "@(#)hack.tty.c 8.1 (Berkeley) 5/31/93";
36 1.3 cgd #else
37 1.14 dholland __RCSID("$NetBSD: hack.tty.c,v 1.14 2009/08/12 07:28:41 dholland Exp $");
38 1.3 cgd #endif
39 1.6 christos #endif /* not lint */
40 1.1 cgd
41 1.11 jsm /*
42 1.11 jsm * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
43 1.11 jsm * Amsterdam
44 1.11 jsm * All rights reserved.
45 1.11 jsm *
46 1.11 jsm * Redistribution and use in source and binary forms, with or without
47 1.11 jsm * modification, are permitted provided that the following conditions are
48 1.11 jsm * met:
49 1.11 jsm *
50 1.11 jsm * - Redistributions of source code must retain the above copyright notice,
51 1.11 jsm * this list of conditions and the following disclaimer.
52 1.11 jsm *
53 1.11 jsm * - Redistributions in binary form must reproduce the above copyright
54 1.11 jsm * notice, this list of conditions and the following disclaimer in the
55 1.11 jsm * documentation and/or other materials provided with the distribution.
56 1.11 jsm *
57 1.11 jsm * - Neither the name of the Stichting Centrum voor Wiskunde en
58 1.11 jsm * Informatica, nor the names of its contributors may be used to endorse or
59 1.11 jsm * promote products derived from this software without specific prior
60 1.11 jsm * written permission.
61 1.11 jsm *
62 1.11 jsm * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
63 1.11 jsm * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
64 1.11 jsm * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
65 1.11 jsm * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
66 1.11 jsm * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
67 1.11 jsm * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
68 1.11 jsm * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
69 1.11 jsm * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
70 1.11 jsm * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
71 1.11 jsm * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
72 1.11 jsm * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
73 1.11 jsm */
74 1.11 jsm
75 1.11 jsm /*
76 1.11 jsm * Copyright (c) 1982 Jay Fenlason <hack (at) gnu.org>
77 1.11 jsm * All rights reserved.
78 1.11 jsm *
79 1.11 jsm * Redistribution and use in source and binary forms, with or without
80 1.11 jsm * modification, are permitted provided that the following conditions
81 1.11 jsm * are met:
82 1.11 jsm * 1. Redistributions of source code must retain the above copyright
83 1.11 jsm * notice, this list of conditions and the following disclaimer.
84 1.11 jsm * 2. Redistributions in binary form must reproduce the above copyright
85 1.11 jsm * notice, this list of conditions and the following disclaimer in the
86 1.11 jsm * documentation and/or other materials provided with the distribution.
87 1.11 jsm * 3. The name of the author may not be used to endorse or promote products
88 1.11 jsm * derived from this software without specific prior written permission.
89 1.11 jsm *
90 1.11 jsm * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
91 1.11 jsm * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
92 1.11 jsm * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
93 1.11 jsm * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
94 1.11 jsm * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
95 1.11 jsm * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
96 1.11 jsm * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
97 1.11 jsm * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
98 1.11 jsm * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
99 1.11 jsm * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
100 1.11 jsm */
101 1.11 jsm
102 1.1 cgd /* hack.tty.c - version 1.0.3 */
103 1.6 christos /*
104 1.6 christos * With thanks to the people who sent code for SYSV - hpscdi!jon,
105 1.6 christos * arnold@ucsf-cgl, wcs@bo95b, cbcephus!pds and others.
106 1.6 christos */
107 1.1 cgd
108 1.6 christos #include <termios.h>
109 1.8 christos #include <termcap.h>
110 1.6 christos #include "hack.h"
111 1.6 christos #include "extern.h"
112 1.1 cgd
113 1.1 cgd /*
114 1.1 cgd * Some systems may have getchar() return EOF for various reasons, and
115 1.1 cgd * we should not quit before seeing at least NR_OF_EOFS consecutive EOFs.
116 1.1 cgd */
117 1.1 cgd #ifndef BSD
118 1.1 cgd #define NR_OF_EOFS 20
119 1.6 christos #endif /* BSD */
120 1.1 cgd
121 1.6 christos static char erase_char, kill_char;
122 1.6 christos static boolean settty_needed = FALSE;
123 1.14 dholland static struct termios inittyb, curttyb;
124 1.14 dholland
125 1.14 dholland static void setctty(void);
126 1.1 cgd
127 1.1 cgd /*
128 1.1 cgd * Get initial state of terminal, set ospeed (for termcap routines)
129 1.1 cgd * and switch off tab expansion if necessary.
130 1.1 cgd * Called by startup() in termcap.c and after returning from ! or ^Z
131 1.1 cgd */
132 1.6 christos void
133 1.13 dholland gettty(void)
134 1.6 christos {
135 1.6 christos if (tcgetattr(0, &inittyb) < 0)
136 1.1 cgd perror("Hack (gettty)");
137 1.1 cgd curttyb = inittyb;
138 1.4 mycroft ospeed = cfgetospeed(&inittyb);
139 1.4 mycroft erase_char = inittyb.c_cc[VERASE];
140 1.4 mycroft kill_char = inittyb.c_cc[VKILL];
141 1.1 cgd getioctls();
142 1.1 cgd
143 1.1 cgd /* do not expand tabs - they might be needed inside a cm sequence */
144 1.6 christos if (curttyb.c_oflag & OXTABS) {
145 1.4 mycroft curttyb.c_oflag &= ~OXTABS;
146 1.1 cgd setctty();
147 1.1 cgd }
148 1.1 cgd settty_needed = TRUE;
149 1.1 cgd }
150 1.1 cgd
151 1.1 cgd /* reset terminal to original state */
152 1.6 christos void
153 1.13 dholland settty(const char *s)
154 1.6 christos {
155 1.1 cgd clear_screen();
156 1.1 cgd end_screen();
157 1.6 christos if (s)
158 1.7 itojun printf("%s", s);
159 1.1 cgd (void) fflush(stdout);
160 1.6 christos if (tcsetattr(0, TCSADRAIN, &inittyb) < 0)
161 1.1 cgd perror("Hack (settty)");
162 1.4 mycroft flags.echo = (inittyb.c_lflag & ECHO) ? ON : OFF;
163 1.4 mycroft flags.cbreak = (inittyb.c_lflag & ICANON) ? OFF : ON;
164 1.1 cgd setioctls();
165 1.1 cgd }
166 1.1 cgd
167 1.14 dholland static void
168 1.13 dholland setctty(void)
169 1.6 christos {
170 1.6 christos if (tcsetattr(0, TCSADRAIN, &curttyb) < 0)
171 1.1 cgd perror("Hack (setctty)");
172 1.1 cgd }
173 1.1 cgd
174 1.1 cgd
175 1.6 christos void
176 1.13 dholland setftty(void)
177 1.6 christos {
178 1.6 christos int change = 0;
179 1.1 cgd flags.cbreak = ON;
180 1.1 cgd flags.echo = OFF;
181 1.1 cgd /* Should use (ECHO|CRMOD) here instead of ECHO */
182 1.6 christos if (curttyb.c_lflag & ECHO) {
183 1.4 mycroft curttyb.c_lflag &= ~ECHO;
184 1.1 cgd change++;
185 1.1 cgd }
186 1.6 christos if (curttyb.c_lflag & ICANON) {
187 1.4 mycroft curttyb.c_lflag &= ~ICANON;
188 1.1 cgd /* be satisfied with one character; no timeout */
189 1.4 mycroft curttyb.c_cc[VMIN] = 1;
190 1.4 mycroft curttyb.c_cc[VTIME] = 0;
191 1.1 cgd change++;
192 1.1 cgd }
193 1.6 christos if (change) {
194 1.1 cgd setctty();
195 1.1 cgd }
196 1.1 cgd start_screen();
197 1.1 cgd }
198 1.1 cgd
199 1.1 cgd
200 1.1 cgd /* fatal error */
201 1.6 christos /* VARARGS1 */
202 1.6 christos void
203 1.6 christos error(const char *fmt, ...)
204 1.6 christos {
205 1.6 christos va_list ap;
206 1.6 christos
207 1.6 christos va_start(ap, fmt);
208 1.6 christos if (settty_needed)
209 1.1 cgd settty((char *) 0);
210 1.6 christos vprintf(fmt, ap);
211 1.6 christos va_end(ap);
212 1.1 cgd putchar('\n');
213 1.1 cgd exit(1);
214 1.1 cgd }
215 1.1 cgd
216 1.1 cgd /*
217 1.1 cgd * Read a line closed with '\n' into the array char bufp[BUFSZ].
218 1.1 cgd * (The '\n' is not stored. The string is closed with a '\0'.)
219 1.1 cgd * Reading can be interrupted by an escape ('\033') - now the
220 1.1 cgd * resulting string is "\033".
221 1.1 cgd */
222 1.6 christos void
223 1.13 dholland getlin(char *bufp)
224 1.1 cgd {
225 1.6 christos char *obufp = bufp;
226 1.6 christos int c;
227 1.1 cgd
228 1.6 christos flags.toplin = 2; /* nonempty, no --More-- required */
229 1.6 christos for (;;) {
230 1.1 cgd (void) fflush(stdout);
231 1.6 christos if ((c = getchar()) == EOF) {
232 1.1 cgd *bufp = 0;
233 1.1 cgd return;
234 1.1 cgd }
235 1.6 christos if (c == '\033') {
236 1.1 cgd *obufp = c;
237 1.1 cgd obufp[1] = 0;
238 1.1 cgd return;
239 1.1 cgd }
240 1.6 christos if (c == erase_char || c == '\b') {
241 1.6 christos if (bufp != obufp) {
242 1.1 cgd bufp--;
243 1.6 christos putstr("\b \b"); /* putsym converts \b */
244 1.6 christos } else
245 1.6 christos bell();
246 1.6 christos } else if (c == '\n') {
247 1.1 cgd *bufp = 0;
248 1.1 cgd return;
249 1.6 christos } else if (' ' <= c && c < '\177') {
250 1.6 christos /*
251 1.6 christos * avoid isprint() - some people don't have it ' ' is
252 1.6 christos * not always a printing char
253 1.6 christos */
254 1.1 cgd *bufp = c;
255 1.1 cgd bufp[1] = 0;
256 1.1 cgd putstr(bufp);
257 1.6 christos if (bufp - obufp < BUFSZ - 1 && bufp - obufp < COLNO)
258 1.1 cgd bufp++;
259 1.6 christos } else if (c == kill_char || c == '\177') { /* Robert Viduya */
260 1.6 christos /* this test last - @ might be the kill_char */
261 1.6 christos while (bufp != obufp) {
262 1.1 cgd bufp--;
263 1.1 cgd putstr("\b \b");
264 1.1 cgd }
265 1.1 cgd } else
266 1.1 cgd bell();
267 1.1 cgd }
268 1.1 cgd }
269 1.1 cgd
270 1.6 christos void
271 1.13 dholland getret(void)
272 1.6 christos {
273 1.1 cgd cgetret("");
274 1.1 cgd }
275 1.1 cgd
276 1.6 christos void
277 1.13 dholland cgetret(const char *s)
278 1.1 cgd {
279 1.1 cgd putsym('\n');
280 1.6 christos if (flags.standout)
281 1.1 cgd standoutbeg();
282 1.1 cgd putstr("Hit ");
283 1.1 cgd putstr(flags.cbreak ? "space" : "return");
284 1.1 cgd putstr(" to continue: ");
285 1.6 christos if (flags.standout)
286 1.1 cgd standoutend();
287 1.1 cgd xwaitforspace(s);
288 1.1 cgd }
289 1.1 cgd
290 1.6 christos char morc; /* tell the outside world what char he used */
291 1.1 cgd
292 1.13 dholland /* s = chars allowed besides space or return */
293 1.6 christos void
294 1.13 dholland xwaitforspace(const char *s)
295 1.1 cgd {
296 1.6 christos int c;
297 1.1 cgd
298 1.1 cgd morc = 0;
299 1.1 cgd
300 1.6 christos while ((c = readchar()) != '\n') {
301 1.6 christos if (flags.cbreak) {
302 1.6 christos if (c == ' ')
303 1.6 christos break;
304 1.6 christos if (s && strchr(s, c)) {
305 1.6 christos morc = c;
306 1.6 christos break;
307 1.6 christos }
308 1.6 christos bell();
309 1.1 cgd }
310 1.1 cgd }
311 1.1 cgd }
312 1.1 cgd
313 1.6 christos char *
314 1.13 dholland parse(void)
315 1.1 cgd {
316 1.6 christos static char inputline[COLNO];
317 1.6 christos int foo;
318 1.1 cgd
319 1.1 cgd flags.move = 1;
320 1.6 christos if (!Invisible)
321 1.6 christos curs_on_u();
322 1.6 christos else
323 1.6 christos home();
324 1.6 christos while ((foo = readchar()) >= '0' && foo <= '9')
325 1.6 christos multi = 10 * multi + foo - '0';
326 1.6 christos if (multi) {
327 1.1 cgd multi--;
328 1.1 cgd save_cm = inputline;
329 1.1 cgd }
330 1.1 cgd inputline[0] = foo;
331 1.1 cgd inputline[1] = 0;
332 1.6 christos if (foo == 'f' || foo == 'F') {
333 1.1 cgd inputline[1] = getchar();
334 1.1 cgd #ifdef QUEST
335 1.6 christos if (inputline[1] == foo)
336 1.6 christos inputline[2] = getchar();
337 1.6 christos else
338 1.6 christos #endif /* QUEST */
339 1.6 christos inputline[2] = 0;
340 1.1 cgd }
341 1.6 christos if (foo == 'm' || foo == 'M') {
342 1.1 cgd inputline[1] = getchar();
343 1.1 cgd inputline[2] = 0;
344 1.1 cgd }
345 1.1 cgd clrlin();
346 1.6 christos return (inputline);
347 1.1 cgd }
348 1.1 cgd
349 1.1 cgd char
350 1.13 dholland readchar(void)
351 1.6 christos {
352 1.6 christos int sym;
353 1.1 cgd
354 1.1 cgd (void) fflush(stdout);
355 1.6 christos if ((sym = getchar()) == EOF)
356 1.1 cgd #ifdef NR_OF_EOFS
357 1.6 christos { /*
358 1.6 christos * Some SYSV systems seem to return EOFs for various reasons
359 1.6 christos * (?like when one hits break or for interrupted systemcalls?),
360 1.6 christos * and we must see several before we quit.
361 1.6 christos */
362 1.6 christos int cnt = NR_OF_EOFS;
363 1.1 cgd while (cnt--) {
364 1.6 christos clearerr(stdin); /* omit if clearerr is
365 1.6 christos * undefined */
366 1.6 christos if ((sym = getchar()) != EOF)
367 1.6 christos goto noteof;
368 1.1 cgd }
369 1.1 cgd end_of_input();
370 1.6 christos noteof: ;
371 1.1 cgd }
372 1.1 cgd #else
373 1.1 cgd end_of_input();
374 1.6 christos #endif /* NR_OF_EOFS */
375 1.6 christos if (flags.toplin == 1)
376 1.1 cgd flags.toplin = 2;
377 1.6 christos return ((char) sym);
378 1.1 cgd }
379 1.1 cgd
380 1.6 christos void
381 1.13 dholland end_of_input(void)
382 1.1 cgd {
383 1.1 cgd settty("End of input?\n");
384 1.1 cgd clearlocks();
385 1.1 cgd exit(0);
386 1.1 cgd }
387