Debian Codegraph @

unix.c

raw
1 /**************************************************************************
2 * This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is *
3 * provided by Jonathan and Jovehacks without charge and without *
4 * warranty. You may copy, modify, and/or distribute JOVE, provided that *
5 * this notice is included in all the source files and documentation. *
6 **************************************************************************/
7
8 #include "jove.h"
9
10 #ifdef UNIX /* the body is the rest of the file */
11
12 #include "fp.h"
13
14 #include "chars.h"
15 #include "term.h" /* ospeed */
16 #include "ttystate.h"
17
18
19 struct termios sg[2];
20
21 #ifdef USE_TIOCSLTC
22 struct ltchars ls[2];
23 #endif /* USE_TIOCSLTC */
24
25 /* Set tty to original (if !n) or JOVE (if n) modes.
26 * This is designed to be idempotent: it can be called
27 * several times with the same argument without damage.
28 *
29 * If STICKY_TTYSTATE is defined, the "original" state is sampled only once.
30 * Normally, this is also sampled each time ttysetattr re-enters JOVE mode.
31 * STICKY_TTYSTATE is useful if processes run while JOVE is suspended
32 * cannot be trusted to leave the tty in a good state.
33 * ConvexOS is currently an example: some of its programs turn on ISTRIP
34 * without provocation.
35 */
36
37 /*
38 * Modern terminals (which are almost always terminal
39 * emulators) are generally fine with ^S and ^Q, so follow GNU
40 * Emacs lead and assume that by default.
41 */
42 jbool OKXonXoff = YES; /* VAR: XON/XOFF can be used as ordinary chars */
43 ZXchar IntChar = CTL(']'); /* VAR: ttysetattr sets this to generate SIGINT */
44
45 void
46 ttysetattr(jbool n) /* `n' is also used as subscript! */
47 {
48 static jbool keep_saved = NO;
49
50 if (!keep_saved) {
51 /* Save the current tty settings:
52 * do the ioctls or whatever to fill in NO half
53 * of each appropriate tty state pair.
54 * NOTE: previously, the tty was not in JOVE mode.
55 * NOTE: the nested tangle of ifdefs is intended to follow
56 * the structure of the definitions in ttystate.c.
57 */
58 (void) tcgetattr(0, &sg[NO]);
59
60 #ifdef USE_TIOCSLTC
61 (void) ioctl(0, TIOCGLTC, &ls[NO]);
62 #endif /* USE_TIOCSLTC */
63
64 /* extract some info from results */
65
66 #ifdef TAB3
67 TABS = (sg[NO].c_oflag & TABDLY) != TAB3;
68 #endif
69 ospeed = cfgetospeed(&sg[NO]);
70
71 #ifdef STICKY_TTYSTATE
72 /* keep saved copy of ttystate until JOVE quits */
73 keep_saved = YES;
74 #endif
75 }
76 #ifndef STICKY_TTYSTATE
77 /* Keep saved copy of ttystate next time iff we are switching to JOVE mode.
78 * In other words, don't replace saved copy next time if we will
79 * be in JOVE mode -- the wrong mode to save.
80 */
81 keep_saved = n;
82 #endif
83
84 /* Fill in YES half of each appropriate tty state pair.
85 * They are filled in as late as possible so that each will
86 * reflect the latest settings of controling variables.
87 * NOTE: the nested tangle of ifdefs is intended to follow
88 * the structure of the definitions in ttystate.c.
89 */
90
91 sg[YES] = sg[NO];
92
93 if (OKXonXoff)
94 sg[YES].c_iflag &= ~(IXON | IXOFF);
95 sg[YES].c_iflag &= ~(INLCR|ICRNL|IGNCR | (MetaKey? ISTRIP : 0));
96 sg[YES].c_lflag &= ~(ICANON|ECHO);
97 sg[YES].c_oflag &= ~(OPOST);
98
99 /* Set all those c_cc elements that we must.
100 * For peculiar systems, one might wish to predefine JVDISABLE
101 * in the configuration. For example, on some unnamed
102 * versions of the Convex OS, it would be good to
103 * define it as (sg[YES].c_cc[VDISABLE]), saving a system call.
104 * Note that the only uses of JVDISABLE are in this block,
105 * so the macro may safely refer to things in this context.
106 */
107 {
108 #ifndef JVDISABLE
109 # ifdef _POSIX_VDISABLE
110 # define JVDISABLE _POSIX_VDISABLE
111 # else /* !_POSIX_VDISABLE */
112 # ifdef _PC_VDISABLE
113 /* Cache the result of fpathconf to reduce the number of syscalls.
114 * We don't handle the error return (-1) because there isn't
115 * anything better to do with it.
116 */
117 cc_t jvd = fpathconf(0, _PC_VDISABLE);
118 # define JVDISABLE jvd
119 # else /* !_PC_VDISABLE */
120 # define JVDISABLE 0
121 # endif /* !_PC_VDISABLE */
122 # endif /* !_POSIX_VDISABLE */
123 #endif /* JVDISABLE */
124
125 sg[YES].c_cc[VINTR] = IntChar;
126
127 #ifdef VQUIT
128 sg[YES].c_cc[VQUIT] = JVDISABLE;
129 #endif
130 /* VERASE, VKILL, VEOL2 irrelevant */
131 /* Beware aliasing! VMIN is VEOF and VTIME is VEOL */
132 #ifdef VSWTCH
133 sg[YES].c_cc[VSWTCH] = JVDISABLE;
134 #endif
135
136 /* Under at least one system (SunOS 4.0), <termio.h>
137 * mistakenly defines the extra V symbols of <termios.h>
138 * without extending the c_cc array in struct termio
139 * to hold them! This is why the following goo is doubly
140 * ifdefed. It turns out that we don't use <termio.h>
141 * on SunOS 4.0, so the problem may be moot.
142 */
143
144 #ifdef VSUSP
145 sg[YES].c_cc[VSUSP] = JVDISABLE;
146 #endif
147 #ifdef VDSUSP
148 sg[YES].c_cc[VDSUSP] = JVDISABLE;
149 #endif
150 #ifdef VDISCARD
151 /* ??? Under Solaris 2.1 needs VDISCARD disabled, or it will
152 * be processed by the tty driver, but not under SysVR4!
153 */
154 sg[YES].c_cc[VDISCARD] = JVDISABLE; /* flush output */
155 #endif
156 #ifdef VLNEXT
157 sg[YES].c_cc[VLNEXT] = JVDISABLE; /* literal next char */
158 #endif
159
160 sg[YES].c_cc[VMIN] = 1;
161 sg[YES].c_cc[VTIME] = 1;
162 }
163
164 #ifdef USE_TIOCSLTC
165 ls[YES] = ls[NO];
166 ls[YES].t_suspc = (char) -1;
167 ls[YES].t_dsuspc = (char) -1;
168 ls[YES].t_flushc = (char) -1;
169 ls[YES].t_lnextc = (char) -1;
170 #endif /* USE_TIOCSLTC */
171
172 /* Set tty state according to appropriate entry of each state pair.
173 * NOTE: the nested tangle of ifdefs is intended to follow
174 * the structure of the definitions in ttystate.c.
175 */
176
177 do {} while (tcsetattr(0, TCSADRAIN, &sg[n]) < 0 && errno == EINTR);
178
179 #ifdef USE_TIOCSLTC
180 (void) ioctl(0, TIOCSLTC, &ls[n]);
181 #endif /* USE_TIOCSLTC */
182
183 }
184
185 /* Determine the number of characters to buffer at each baud rate. The
186 * lower the number, the quicker the response when new input arrives. Of
187 * course the lower the number, the more prone the program is to stop in
188 * output. Decide what matters most to you. This sets ScrBufSize to the right
189 * number or chars, and initializes `jstdout'.
190 */
191 void
192 settout(void)
193 {
194 int speed_chars;
195
196 static const struct {
197 unsigned int bsize;
198 unsigned int brate;
199 } speeds[] = {
200
201 #ifdef B0
202 { 1, B0 },
203 #endif
204 #ifdef B50
205 { 1, B50 },
206 #endif
207 #ifdef B75
208 { 1, B75 },
209 #endif
210 #ifdef B110
211 { 1, B110 },
212 #endif
213 #ifdef B134
214 { 1, B134 },
215 #endif
216 #ifdef B150
217 { 1, B150 },
218 #endif
219 #ifdef B200
220 { 1, B200 },
221 #endif
222 #ifdef B300
223 { 2, B300 },
224 #endif
225 #ifdef B600
226 { 4, B600 },
227 #endif
228 #ifdef B900
229 { 6, B900 },
230 #endif
231 #ifdef B1200
232 { 8, B1200 },
233 #endif
234 #ifdef B1800
235 { 16, B1800 },
236 #endif
237 #ifdef B2400
238 { 32, B2400 },
239 #endif
240 #ifdef B3600
241 { 64, B3600 },
242 #endif
243 #ifdef B4800
244 { 128, B4800 },
245 #endif
246 #ifdef B7200
247 { 256, B7200 },
248 #endif
249 #ifdef B9600
250 { 256, B9600 },
251 #endif
252 #ifdef EXTA
253 { 512, EXTA },
254 #endif
255 #ifdef B19200
256 { 512, B19200 },
257 #endif
258 #ifdef EXTB
259 { 1024, EXTB },
260 #endif
261 #ifdef B38400
262 { 1024, B38400 },
263 #endif
264 #ifdef EXT
265 { 1024, EXT }
266 #endif
267 };
268 int i;
269 for (i = 0; ; i++) {
270 if (i == elemsof(speeds)) {
271 speed_chars = 512;
272 ospeed = B9600; /* XXX */
273 break;
274 }
275 if (speeds[i].brate == (unsigned short) ospeed) {
276 speed_chars = speeds[i].bsize;
277 break;
278 }
279 }
280
281 flushscreen(); /* flush the one character buffer */
282 ScrBufSize = jmin(MAXTTYBUF, speed_chars * jmax(LI / 24, 1));
283 #ifndef NO_JSTDOUT
284 jstdout = fd_open("/dev/tty", F_WRITE|F_LOCKED, 1, (char *)NULL, ScrBufSize);
285 #endif
286 }
287
288 void
289 ttsize(void)
290 {
291 /* ??? We really ought to wait until the screen is big enough:
292 * at least three lines high (one line each for buffer, mode,
293 * and message) and at least twelve columns wide (eight for
294 * line number, one for content, two for overflow indicators,
295 * and one blank at end).
296 */
297 #ifdef TIOCGWINSZ
298 struct winsize win;
299
300 if (ioctl(0, TIOCGWINSZ, &win) == 0
301 && win.ws_col >= 12
302 && win.ws_row >= 3)
303 {
304 CO = jmin(win.ws_col, MAXCOLS);
305 LI = win.ws_row;
306 }
307 #else /* !TIOCGWINSZ */
308 # ifdef BTL_BLIT
309 struct jwinsize jwin;
310
311 if (ioctl(0, JWINSIZE, &jwin) == 0
312 && jwin.bytesx >= 12
313 && jwin.bytesy >= 3)
314 {
315 CO = jmin(jwin.bytesx, MAXCOLS);
316 LI = jwin.bytesy;
317 }
318 # endif /* BTL_BLIT */
319 #endif /* !TIOCGWINSZ */
320 ILI = LI - 1;
321 }
322
323 #endif /* UNIX */