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