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