hack.tty.c revision 1.10 1 1.10 wiz /* $NetBSD: hack.tty.c,v 1.10 2002/05/26 00:12:12 wiz 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.10 wiz __RCSID("$NetBSD: hack.tty.c,v 1.10 2002/05/26 00:12:12 wiz Exp $");
42 1.3 cgd #endif
43 1.6 christos #endif /* not lint */
44 1.1 cgd
45 1.1 cgd /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
46 1.1 cgd /* hack.tty.c - version 1.0.3 */
47 1.6 christos /*
48 1.6 christos * With thanks to the people who sent code for SYSV - hpscdi!jon,
49 1.6 christos * arnold@ucsf-cgl, wcs@bo95b, cbcephus!pds and others.
50 1.6 christos */
51 1.1 cgd
52 1.6 christos #include <termios.h>
53 1.8 christos #include <termcap.h>
54 1.6 christos #include "hack.h"
55 1.6 christos #include "extern.h"
56 1.1 cgd
57 1.1 cgd /*
58 1.1 cgd * Some systems may have getchar() return EOF for various reasons, and
59 1.1 cgd * we should not quit before seeing at least NR_OF_EOFS consecutive EOFs.
60 1.1 cgd */
61 1.1 cgd #ifndef BSD
62 1.1 cgd #define NR_OF_EOFS 20
63 1.6 christos #endif /* BSD */
64 1.1 cgd
65 1.6 christos static char erase_char, kill_char;
66 1.6 christos static boolean settty_needed = FALSE;
67 1.6 christos struct termios inittyb, curttyb;
68 1.1 cgd
69 1.1 cgd /*
70 1.1 cgd * Get initial state of terminal, set ospeed (for termcap routines)
71 1.1 cgd * and switch off tab expansion if necessary.
72 1.1 cgd * Called by startup() in termcap.c and after returning from ! or ^Z
73 1.1 cgd */
74 1.6 christos void
75 1.6 christos gettty()
76 1.6 christos {
77 1.6 christos if (tcgetattr(0, &inittyb) < 0)
78 1.1 cgd perror("Hack (gettty)");
79 1.1 cgd curttyb = inittyb;
80 1.4 mycroft ospeed = cfgetospeed(&inittyb);
81 1.4 mycroft erase_char = inittyb.c_cc[VERASE];
82 1.4 mycroft kill_char = inittyb.c_cc[VKILL];
83 1.1 cgd getioctls();
84 1.1 cgd
85 1.1 cgd /* do not expand tabs - they might be needed inside a cm sequence */
86 1.6 christos if (curttyb.c_oflag & OXTABS) {
87 1.4 mycroft curttyb.c_oflag &= ~OXTABS;
88 1.1 cgd setctty();
89 1.1 cgd }
90 1.1 cgd settty_needed = TRUE;
91 1.1 cgd }
92 1.1 cgd
93 1.1 cgd /* reset terminal to original state */
94 1.6 christos void
95 1.6 christos settty(s)
96 1.9 jsm const char *s;
97 1.6 christos {
98 1.1 cgd clear_screen();
99 1.1 cgd end_screen();
100 1.6 christos if (s)
101 1.7 itojun printf("%s", s);
102 1.1 cgd (void) fflush(stdout);
103 1.6 christos if (tcsetattr(0, TCSADRAIN, &inittyb) < 0)
104 1.1 cgd perror("Hack (settty)");
105 1.4 mycroft flags.echo = (inittyb.c_lflag & ECHO) ? ON : OFF;
106 1.4 mycroft flags.cbreak = (inittyb.c_lflag & ICANON) ? OFF : ON;
107 1.1 cgd setioctls();
108 1.1 cgd }
109 1.1 cgd
110 1.6 christos void
111 1.6 christos setctty()
112 1.6 christos {
113 1.6 christos if (tcsetattr(0, TCSADRAIN, &curttyb) < 0)
114 1.1 cgd perror("Hack (setctty)");
115 1.1 cgd }
116 1.1 cgd
117 1.1 cgd
118 1.6 christos void
119 1.6 christos setftty()
120 1.6 christos {
121 1.6 christos int change = 0;
122 1.1 cgd flags.cbreak = ON;
123 1.1 cgd flags.echo = OFF;
124 1.1 cgd /* Should use (ECHO|CRMOD) here instead of ECHO */
125 1.6 christos if (curttyb.c_lflag & ECHO) {
126 1.4 mycroft curttyb.c_lflag &= ~ECHO;
127 1.1 cgd change++;
128 1.1 cgd }
129 1.6 christos if (curttyb.c_lflag & ICANON) {
130 1.4 mycroft curttyb.c_lflag &= ~ICANON;
131 1.1 cgd /* be satisfied with one character; no timeout */
132 1.4 mycroft curttyb.c_cc[VMIN] = 1;
133 1.4 mycroft curttyb.c_cc[VTIME] = 0;
134 1.1 cgd change++;
135 1.1 cgd }
136 1.6 christos if (change) {
137 1.1 cgd setctty();
138 1.1 cgd }
139 1.1 cgd start_screen();
140 1.1 cgd }
141 1.1 cgd
142 1.1 cgd
143 1.1 cgd /* fatal error */
144 1.6 christos /* VARARGS1 */
145 1.6 christos void
146 1.6 christos error(const char *fmt, ...)
147 1.6 christos {
148 1.6 christos va_list ap;
149 1.6 christos
150 1.6 christos va_start(ap, fmt);
151 1.6 christos if (settty_needed)
152 1.1 cgd settty((char *) 0);
153 1.6 christos vprintf(fmt, ap);
154 1.6 christos va_end(ap);
155 1.1 cgd putchar('\n');
156 1.1 cgd exit(1);
157 1.1 cgd }
158 1.1 cgd
159 1.1 cgd /*
160 1.1 cgd * Read a line closed with '\n' into the array char bufp[BUFSZ].
161 1.1 cgd * (The '\n' is not stored. The string is closed with a '\0'.)
162 1.1 cgd * Reading can be interrupted by an escape ('\033') - now the
163 1.1 cgd * resulting string is "\033".
164 1.1 cgd */
165 1.6 christos void
166 1.1 cgd getlin(bufp)
167 1.6 christos char *bufp;
168 1.1 cgd {
169 1.6 christos char *obufp = bufp;
170 1.6 christos int c;
171 1.1 cgd
172 1.6 christos flags.toplin = 2; /* nonempty, no --More-- required */
173 1.6 christos for (;;) {
174 1.1 cgd (void) fflush(stdout);
175 1.6 christos if ((c = getchar()) == EOF) {
176 1.1 cgd *bufp = 0;
177 1.1 cgd return;
178 1.1 cgd }
179 1.6 christos if (c == '\033') {
180 1.1 cgd *obufp = c;
181 1.1 cgd obufp[1] = 0;
182 1.1 cgd return;
183 1.1 cgd }
184 1.6 christos if (c == erase_char || c == '\b') {
185 1.6 christos if (bufp != obufp) {
186 1.1 cgd bufp--;
187 1.6 christos putstr("\b \b"); /* putsym converts \b */
188 1.6 christos } else
189 1.6 christos bell();
190 1.6 christos } else if (c == '\n') {
191 1.1 cgd *bufp = 0;
192 1.1 cgd return;
193 1.6 christos } else if (' ' <= c && c < '\177') {
194 1.6 christos /*
195 1.6 christos * avoid isprint() - some people don't have it ' ' is
196 1.6 christos * not always a printing char
197 1.6 christos */
198 1.1 cgd *bufp = c;
199 1.1 cgd bufp[1] = 0;
200 1.1 cgd putstr(bufp);
201 1.6 christos if (bufp - obufp < BUFSZ - 1 && bufp - obufp < COLNO)
202 1.1 cgd bufp++;
203 1.6 christos } else if (c == kill_char || c == '\177') { /* Robert Viduya */
204 1.6 christos /* this test last - @ might be the kill_char */
205 1.6 christos while (bufp != obufp) {
206 1.1 cgd bufp--;
207 1.1 cgd putstr("\b \b");
208 1.1 cgd }
209 1.1 cgd } else
210 1.1 cgd bell();
211 1.1 cgd }
212 1.1 cgd }
213 1.1 cgd
214 1.6 christos void
215 1.6 christos getret()
216 1.6 christos {
217 1.1 cgd cgetret("");
218 1.1 cgd }
219 1.1 cgd
220 1.6 christos void
221 1.1 cgd cgetret(s)
222 1.9 jsm const char *s;
223 1.1 cgd {
224 1.1 cgd putsym('\n');
225 1.6 christos if (flags.standout)
226 1.1 cgd standoutbeg();
227 1.1 cgd putstr("Hit ");
228 1.1 cgd putstr(flags.cbreak ? "space" : "return");
229 1.1 cgd putstr(" to continue: ");
230 1.6 christos if (flags.standout)
231 1.1 cgd standoutend();
232 1.1 cgd xwaitforspace(s);
233 1.1 cgd }
234 1.1 cgd
235 1.6 christos char morc; /* tell the outside world what char he used */
236 1.1 cgd
237 1.6 christos void
238 1.1 cgd xwaitforspace(s)
239 1.9 jsm const char *s; /* chars allowed besides space or return */
240 1.1 cgd {
241 1.6 christos int c;
242 1.1 cgd
243 1.1 cgd morc = 0;
244 1.1 cgd
245 1.6 christos while ((c = readchar()) != '\n') {
246 1.6 christos if (flags.cbreak) {
247 1.6 christos if (c == ' ')
248 1.6 christos break;
249 1.6 christos if (s && strchr(s, c)) {
250 1.6 christos morc = c;
251 1.6 christos break;
252 1.6 christos }
253 1.6 christos bell();
254 1.1 cgd }
255 1.1 cgd }
256 1.1 cgd }
257 1.1 cgd
258 1.6 christos char *
259 1.1 cgd parse()
260 1.1 cgd {
261 1.6 christos static char inputline[COLNO];
262 1.6 christos int foo;
263 1.1 cgd
264 1.1 cgd flags.move = 1;
265 1.6 christos if (!Invisible)
266 1.6 christos curs_on_u();
267 1.6 christos else
268 1.6 christos home();
269 1.6 christos while ((foo = readchar()) >= '0' && foo <= '9')
270 1.6 christos multi = 10 * multi + foo - '0';
271 1.6 christos if (multi) {
272 1.1 cgd multi--;
273 1.1 cgd save_cm = inputline;
274 1.1 cgd }
275 1.1 cgd inputline[0] = foo;
276 1.1 cgd inputline[1] = 0;
277 1.6 christos if (foo == 'f' || foo == 'F') {
278 1.1 cgd inputline[1] = getchar();
279 1.1 cgd #ifdef QUEST
280 1.6 christos if (inputline[1] == foo)
281 1.6 christos inputline[2] = getchar();
282 1.6 christos else
283 1.6 christos #endif /* QUEST */
284 1.6 christos inputline[2] = 0;
285 1.1 cgd }
286 1.6 christos if (foo == 'm' || foo == 'M') {
287 1.1 cgd inputline[1] = getchar();
288 1.1 cgd inputline[2] = 0;
289 1.1 cgd }
290 1.1 cgd clrlin();
291 1.6 christos return (inputline);
292 1.1 cgd }
293 1.1 cgd
294 1.1 cgd char
295 1.6 christos readchar()
296 1.6 christos {
297 1.6 christos int sym;
298 1.1 cgd
299 1.1 cgd (void) fflush(stdout);
300 1.6 christos if ((sym = getchar()) == EOF)
301 1.1 cgd #ifdef NR_OF_EOFS
302 1.6 christos { /*
303 1.6 christos * Some SYSV systems seem to return EOFs for various reasons
304 1.6 christos * (?like when one hits break or for interrupted systemcalls?),
305 1.6 christos * and we must see several before we quit.
306 1.6 christos */
307 1.6 christos int cnt = NR_OF_EOFS;
308 1.1 cgd while (cnt--) {
309 1.6 christos clearerr(stdin); /* omit if clearerr is
310 1.6 christos * undefined */
311 1.6 christos if ((sym = getchar()) != EOF)
312 1.6 christos goto noteof;
313 1.1 cgd }
314 1.1 cgd end_of_input();
315 1.6 christos noteof: ;
316 1.1 cgd }
317 1.1 cgd #else
318 1.1 cgd end_of_input();
319 1.6 christos #endif /* NR_OF_EOFS */
320 1.6 christos if (flags.toplin == 1)
321 1.1 cgd flags.toplin = 2;
322 1.6 christos return ((char) sym);
323 1.1 cgd }
324 1.1 cgd
325 1.6 christos void
326 1.1 cgd end_of_input()
327 1.1 cgd {
328 1.1 cgd settty("End of input?\n");
329 1.1 cgd clearlocks();
330 1.1 cgd exit(0);
331 1.1 cgd }
332