hack.tty.c revision 1.7 1 1.7 itojun /* $NetBSD: hack.tty.c,v 1.7 2000/07/10 10:19:25 itojun 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.7 itojun __RCSID("$NetBSD: hack.tty.c,v 1.7 2000/07/10 10:19:25 itojun 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.6 christos #include "hack.h"
54 1.6 christos #include "extern.h"
55 1.1 cgd
56 1.1 cgd /*
57 1.1 cgd * Some systems may have getchar() return EOF for various reasons, and
58 1.1 cgd * we should not quit before seeing at least NR_OF_EOFS consecutive EOFs.
59 1.1 cgd */
60 1.1 cgd #ifndef BSD
61 1.1 cgd #define NR_OF_EOFS 20
62 1.6 christos #endif /* BSD */
63 1.1 cgd
64 1.6 christos static char erase_char, kill_char;
65 1.6 christos static boolean settty_needed = FALSE;
66 1.6 christos struct termios inittyb, curttyb;
67 1.1 cgd
68 1.1 cgd /*
69 1.1 cgd * Get initial state of terminal, set ospeed (for termcap routines)
70 1.1 cgd * and switch off tab expansion if necessary.
71 1.1 cgd * Called by startup() in termcap.c and after returning from ! or ^Z
72 1.1 cgd */
73 1.6 christos void
74 1.6 christos gettty()
75 1.6 christos {
76 1.6 christos extern speed_t ospeed;
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.6 christos 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 #ifdef __STDC__
147 1.6 christos error(const char *fmt, ...)
148 1.6 christos #else
149 1.6 christos error(va_alist)
150 1.6 christos va_dcl
151 1.6 christos #endif
152 1.6 christos {
153 1.6 christos va_list ap;
154 1.6 christos #ifndef __STDC__
155 1.6 christos const char *fmt;
156 1.6 christos
157 1.6 christos va_start(ap);
158 1.6 christos fmt = va_arg(ap, const char *);
159 1.6 christos #else
160 1.6 christos va_start(ap, fmt);
161 1.6 christos #endif
162 1.6 christos if (settty_needed)
163 1.1 cgd settty((char *) 0);
164 1.6 christos vprintf(fmt, ap);
165 1.6 christos va_end(ap);
166 1.1 cgd putchar('\n');
167 1.1 cgd exit(1);
168 1.1 cgd }
169 1.1 cgd
170 1.1 cgd /*
171 1.1 cgd * Read a line closed with '\n' into the array char bufp[BUFSZ].
172 1.1 cgd * (The '\n' is not stored. The string is closed with a '\0'.)
173 1.1 cgd * Reading can be interrupted by an escape ('\033') - now the
174 1.1 cgd * resulting string is "\033".
175 1.1 cgd */
176 1.6 christos void
177 1.1 cgd getlin(bufp)
178 1.6 christos char *bufp;
179 1.1 cgd {
180 1.6 christos char *obufp = bufp;
181 1.6 christos int c;
182 1.1 cgd
183 1.6 christos flags.toplin = 2; /* nonempty, no --More-- required */
184 1.6 christos for (;;) {
185 1.1 cgd (void) fflush(stdout);
186 1.6 christos if ((c = getchar()) == EOF) {
187 1.1 cgd *bufp = 0;
188 1.1 cgd return;
189 1.1 cgd }
190 1.6 christos if (c == '\033') {
191 1.1 cgd *obufp = c;
192 1.1 cgd obufp[1] = 0;
193 1.1 cgd return;
194 1.1 cgd }
195 1.6 christos if (c == erase_char || c == '\b') {
196 1.6 christos if (bufp != obufp) {
197 1.1 cgd bufp--;
198 1.6 christos putstr("\b \b"); /* putsym converts \b */
199 1.6 christos } else
200 1.6 christos bell();
201 1.6 christos } else if (c == '\n') {
202 1.1 cgd *bufp = 0;
203 1.1 cgd return;
204 1.6 christos } else if (' ' <= c && c < '\177') {
205 1.6 christos /*
206 1.6 christos * avoid isprint() - some people don't have it ' ' is
207 1.6 christos * not always a printing char
208 1.6 christos */
209 1.1 cgd *bufp = c;
210 1.1 cgd bufp[1] = 0;
211 1.1 cgd putstr(bufp);
212 1.6 christos if (bufp - obufp < BUFSZ - 1 && bufp - obufp < COLNO)
213 1.1 cgd bufp++;
214 1.6 christos } else if (c == kill_char || c == '\177') { /* Robert Viduya */
215 1.6 christos /* this test last - @ might be the kill_char */
216 1.6 christos while (bufp != obufp) {
217 1.1 cgd bufp--;
218 1.1 cgd putstr("\b \b");
219 1.1 cgd }
220 1.1 cgd } else
221 1.1 cgd bell();
222 1.1 cgd }
223 1.1 cgd }
224 1.1 cgd
225 1.6 christos void
226 1.6 christos getret()
227 1.6 christos {
228 1.1 cgd cgetret("");
229 1.1 cgd }
230 1.1 cgd
231 1.6 christos void
232 1.1 cgd cgetret(s)
233 1.6 christos char *s;
234 1.1 cgd {
235 1.1 cgd putsym('\n');
236 1.6 christos if (flags.standout)
237 1.1 cgd standoutbeg();
238 1.1 cgd putstr("Hit ");
239 1.1 cgd putstr(flags.cbreak ? "space" : "return");
240 1.1 cgd putstr(" to continue: ");
241 1.6 christos if (flags.standout)
242 1.1 cgd standoutend();
243 1.1 cgd xwaitforspace(s);
244 1.1 cgd }
245 1.1 cgd
246 1.6 christos char morc; /* tell the outside world what char he used */
247 1.1 cgd
248 1.6 christos void
249 1.1 cgd xwaitforspace(s)
250 1.6 christos char *s; /* chars allowed besides space or return */
251 1.1 cgd {
252 1.6 christos int c;
253 1.1 cgd
254 1.1 cgd morc = 0;
255 1.1 cgd
256 1.6 christos while ((c = readchar()) != '\n') {
257 1.6 christos if (flags.cbreak) {
258 1.6 christos if (c == ' ')
259 1.6 christos break;
260 1.6 christos if (s && strchr(s, c)) {
261 1.6 christos morc = c;
262 1.6 christos break;
263 1.6 christos }
264 1.6 christos bell();
265 1.1 cgd }
266 1.1 cgd }
267 1.1 cgd }
268 1.1 cgd
269 1.6 christos char *
270 1.1 cgd parse()
271 1.1 cgd {
272 1.6 christos static char inputline[COLNO];
273 1.6 christos int foo;
274 1.1 cgd
275 1.1 cgd flags.move = 1;
276 1.6 christos if (!Invisible)
277 1.6 christos curs_on_u();
278 1.6 christos else
279 1.6 christos home();
280 1.6 christos while ((foo = readchar()) >= '0' && foo <= '9')
281 1.6 christos multi = 10 * multi + foo - '0';
282 1.6 christos if (multi) {
283 1.1 cgd multi--;
284 1.1 cgd save_cm = inputline;
285 1.1 cgd }
286 1.1 cgd inputline[0] = foo;
287 1.1 cgd inputline[1] = 0;
288 1.6 christos if (foo == 'f' || foo == 'F') {
289 1.1 cgd inputline[1] = getchar();
290 1.1 cgd #ifdef QUEST
291 1.6 christos if (inputline[1] == foo)
292 1.6 christos inputline[2] = getchar();
293 1.6 christos else
294 1.6 christos #endif /* QUEST */
295 1.6 christos inputline[2] = 0;
296 1.1 cgd }
297 1.6 christos if (foo == 'm' || foo == 'M') {
298 1.1 cgd inputline[1] = getchar();
299 1.1 cgd inputline[2] = 0;
300 1.1 cgd }
301 1.1 cgd clrlin();
302 1.6 christos return (inputline);
303 1.1 cgd }
304 1.1 cgd
305 1.1 cgd char
306 1.6 christos readchar()
307 1.6 christos {
308 1.6 christos int sym;
309 1.1 cgd
310 1.1 cgd (void) fflush(stdout);
311 1.6 christos if ((sym = getchar()) == EOF)
312 1.1 cgd #ifdef NR_OF_EOFS
313 1.6 christos { /*
314 1.6 christos * Some SYSV systems seem to return EOFs for various reasons
315 1.6 christos * (?like when one hits break or for interrupted systemcalls?),
316 1.6 christos * and we must see several before we quit.
317 1.6 christos */
318 1.6 christos int cnt = NR_OF_EOFS;
319 1.1 cgd while (cnt--) {
320 1.6 christos clearerr(stdin); /* omit if clearerr is
321 1.6 christos * undefined */
322 1.6 christos if ((sym = getchar()) != EOF)
323 1.6 christos goto noteof;
324 1.1 cgd }
325 1.1 cgd end_of_input();
326 1.6 christos noteof: ;
327 1.1 cgd }
328 1.1 cgd #else
329 1.1 cgd end_of_input();
330 1.6 christos #endif /* NR_OF_EOFS */
331 1.6 christos if (flags.toplin == 1)
332 1.1 cgd flags.toplin = 2;
333 1.6 christos return ((char) sym);
334 1.1 cgd }
335 1.1 cgd
336 1.6 christos void
337 1.1 cgd end_of_input()
338 1.1 cgd {
339 1.1 cgd settty("End of input?\n");
340 1.1 cgd clearlocks();
341 1.1 cgd exit(0);
342 1.1 cgd }
343