subr.c revision 1.27 1 1.27 itojun /* $NetBSD: subr.c,v 1.27 2002/09/23 03:32:34 itojun Exp $ */
2 1.19 thorpej
3 1.1 cgd /*
4 1.9 pk * Copyright (c) 1983, 1993
5 1.9 pk * 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.22 mikel #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.19 thorpej #if 0
39 1.19 thorpej static char sccsid[] = "from: @(#)subr.c 8.1 (Berkeley) 6/4/93";
40 1.19 thorpej #else
41 1.27 itojun __RCSID("$NetBSD: subr.c,v 1.27 2002/09/23 03:32:34 itojun Exp $");
42 1.19 thorpej #endif
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd /*
46 1.1 cgd * Melbourne getty.
47 1.1 cgd */
48 1.7 pk #define COMPAT_43
49 1.20 thorpej #include <sys/param.h>
50 1.22 mikel #include <sys/ioctl.h>
51 1.22 mikel
52 1.4 cgd #include <stdlib.h>
53 1.1 cgd #include <string.h>
54 1.7 pk #include <termios.h>
55 1.22 mikel #include <unistd.h>
56 1.27 itojun #include <poll.h>
57 1.4 cgd
58 1.22 mikel #include "extern.h"
59 1.1 cgd #include "gettytab.h"
60 1.4 cgd #include "pathnames.h"
61 1.1 cgd
62 1.9 pk extern struct termios tmode, omode;
63 1.1 cgd
64 1.25 thorpej static void compatflags(long);
65 1.9 pk
66 1.1 cgd /*
67 1.1 cgd * Get a table entry.
68 1.1 cgd */
69 1.9 pk void
70 1.25 thorpej gettable(char *name, char *buf)
71 1.1 cgd {
72 1.23 mrg struct gettystrs *sp;
73 1.23 mrg struct gettynums *np;
74 1.23 mrg struct gettyflags *fp;
75 1.4 cgd long n;
76 1.4 cgd char *dba[2];
77 1.4 cgd dba[0] = _PATH_GETTYTAB;
78 1.4 cgd dba[1] = 0;
79 1.1 cgd
80 1.4 cgd if (cgetent(&buf, dba, name) != 0)
81 1.1 cgd return;
82 1.1 cgd
83 1.1 cgd for (sp = gettystrs; sp->field; sp++)
84 1.4 cgd cgetstr(buf, sp->field, &sp->value);
85 1.1 cgd for (np = gettynums; np->field; np++) {
86 1.4 cgd if (cgetnum(buf, np->field, &n) == -1)
87 1.1 cgd np->set = 0;
88 1.1 cgd else {
89 1.1 cgd np->set = 1;
90 1.1 cgd np->value = n;
91 1.1 cgd }
92 1.1 cgd }
93 1.1 cgd for (fp = gettyflags; fp->field; fp++) {
94 1.4 cgd if (cgetcap(buf, fp->field, ':') == NULL)
95 1.1 cgd fp->set = 0;
96 1.1 cgd else {
97 1.1 cgd fp->set = 1;
98 1.4 cgd fp->value = 1 ^ fp->invrt;
99 1.1 cgd }
100 1.1 cgd }
101 1.4 cgd #ifdef DEBUG
102 1.4 cgd printf("name=\"%s\", buf=\"%s\"\n", name, buf);
103 1.4 cgd for (sp = gettystrs; sp->field; sp++)
104 1.4 cgd printf("cgetstr: %s=%s\n", sp->field, sp->value);
105 1.4 cgd for (np = gettynums; np->field; np++)
106 1.4 cgd printf("cgetnum: %s=%d\n", np->field, np->value);
107 1.4 cgd for (fp = gettyflags; fp->field; fp++)
108 1.9 pk printf("cgetflags: %s='%c' set='%c'\n", fp->field,
109 1.9 pk fp->value + '0', fp->set + '0');
110 1.4 cgd exit(1);
111 1.4 cgd #endif /* DEBUG */
112 1.1 cgd }
113 1.1 cgd
114 1.9 pk void
115 1.25 thorpej gendefaults(void)
116 1.1 cgd {
117 1.23 mrg struct gettystrs *sp;
118 1.23 mrg struct gettynums *np;
119 1.23 mrg struct gettyflags *fp;
120 1.1 cgd
121 1.1 cgd for (sp = gettystrs; sp->field; sp++)
122 1.1 cgd if (sp->value)
123 1.1 cgd sp->defalt = sp->value;
124 1.1 cgd for (np = gettynums; np->field; np++)
125 1.1 cgd if (np->set)
126 1.1 cgd np->defalt = np->value;
127 1.1 cgd for (fp = gettyflags; fp->field; fp++)
128 1.1 cgd if (fp->set)
129 1.1 cgd fp->defalt = fp->value;
130 1.1 cgd else
131 1.1 cgd fp->defalt = fp->invrt;
132 1.1 cgd }
133 1.1 cgd
134 1.9 pk void
135 1.25 thorpej setdefaults(void)
136 1.1 cgd {
137 1.23 mrg struct gettystrs *sp;
138 1.23 mrg struct gettynums *np;
139 1.23 mrg struct gettyflags *fp;
140 1.1 cgd
141 1.1 cgd for (sp = gettystrs; sp->field; sp++)
142 1.1 cgd if (!sp->value)
143 1.1 cgd sp->value = sp->defalt;
144 1.1 cgd for (np = gettynums; np->field; np++)
145 1.1 cgd if (!np->set)
146 1.1 cgd np->value = np->defalt;
147 1.1 cgd for (fp = gettyflags; fp->field; fp++)
148 1.1 cgd if (!fp->set)
149 1.1 cgd fp->value = fp->defalt;
150 1.1 cgd }
151 1.1 cgd
152 1.1 cgd static char **
153 1.1 cgd charnames[] = {
154 1.1 cgd &ER, &KL, &IN, &QU, &XN, &XF, &ET, &BK,
155 1.1 cgd &SU, &DS, &RP, &FL, &WE, &LN, 0
156 1.1 cgd };
157 1.1 cgd
158 1.1 cgd static char *
159 1.1 cgd charvars[] = {
160 1.7 pk &tmode.c_cc[VERASE], &tmode.c_cc[VKILL], &tmode.c_cc[VINTR],
161 1.7 pk &tmode.c_cc[VQUIT], &tmode.c_cc[VSTART], &tmode.c_cc[VSTOP],
162 1.7 pk &tmode.c_cc[VEOF], &tmode.c_cc[VEOL], &tmode.c_cc[VSUSP],
163 1.7 pk &tmode.c_cc[VDSUSP], &tmode.c_cc[VREPRINT], &tmode.c_cc[VDISCARD],
164 1.7 pk &tmode.c_cc[VWERASE], &tmode.c_cc[VLNEXT], 0
165 1.1 cgd };
166 1.1 cgd
167 1.9 pk void
168 1.25 thorpej setchars(void)
169 1.1 cgd {
170 1.23 mrg int i;
171 1.23 mrg char *p;
172 1.1 cgd
173 1.1 cgd for (i = 0; charnames[i]; i++) {
174 1.1 cgd p = *charnames[i];
175 1.1 cgd if (p && *p)
176 1.1 cgd *charvars[i] = *p;
177 1.1 cgd else
178 1.12 mycroft *charvars[i] = _POSIX_VDISABLE;
179 1.1 cgd }
180 1.1 cgd }
181 1.1 cgd
182 1.17 mycroft /* Macros to clear/set/test flags. */
183 1.17 mycroft #define SET(t, f) (t) |= (f)
184 1.17 mycroft #define CLR(t, f) (t) &= ~(f)
185 1.17 mycroft #define ISSET(t, f) ((t) & (f))
186 1.17 mycroft
187 1.7 pk void
188 1.25 thorpej setflags(int n)
189 1.1 cgd {
190 1.23 mrg tcflag_t iflag, oflag, cflag, lflag;
191 1.1 cgd
192 1.7 pk #ifdef COMPAT_43
193 1.1 cgd switch (n) {
194 1.1 cgd case 0:
195 1.7 pk if (F0set) {
196 1.7 pk compatflags(F0);
197 1.7 pk return;
198 1.7 pk }
199 1.1 cgd break;
200 1.1 cgd case 1:
201 1.7 pk if (F1set) {
202 1.7 pk compatflags(F1);
203 1.7 pk return;
204 1.7 pk }
205 1.1 cgd break;
206 1.1 cgd default:
207 1.7 pk if (F2set) {
208 1.7 pk compatflags(F2);
209 1.7 pk return;
210 1.7 pk }
211 1.1 cgd break;
212 1.1 cgd }
213 1.7 pk #endif
214 1.1 cgd
215 1.7 pk switch (n) {
216 1.7 pk case 0:
217 1.7 pk if (C0set && I0set && L0set && O0set) {
218 1.7 pk tmode.c_cflag = C0;
219 1.7 pk tmode.c_iflag = I0;
220 1.7 pk tmode.c_lflag = L0;
221 1.7 pk tmode.c_oflag = O0;
222 1.7 pk return;
223 1.7 pk }
224 1.7 pk break;
225 1.7 pk case 1:
226 1.7 pk if (C1set && I1set && L1set && O1set) {
227 1.7 pk tmode.c_cflag = C1;
228 1.7 pk tmode.c_iflag = I1;
229 1.7 pk tmode.c_lflag = L1;
230 1.7 pk tmode.c_oflag = O1;
231 1.7 pk return;
232 1.7 pk }
233 1.7 pk break;
234 1.7 pk default:
235 1.7 pk if (C2set && I2set && L2set && O2set) {
236 1.7 pk tmode.c_cflag = C2;
237 1.7 pk tmode.c_iflag = I2;
238 1.7 pk tmode.c_lflag = L2;
239 1.7 pk tmode.c_oflag = O2;
240 1.7 pk return;
241 1.7 pk }
242 1.7 pk break;
243 1.7 pk }
244 1.1 cgd
245 1.9 pk iflag = omode.c_iflag;
246 1.16 mycroft oflag = omode.c_oflag;
247 1.9 pk cflag = omode.c_cflag;
248 1.9 pk lflag = omode.c_lflag;
249 1.7 pk
250 1.7 pk if (NP) {
251 1.17 mycroft CLR(cflag, CSIZE|PARENB);
252 1.17 mycroft SET(cflag, CS8);
253 1.17 mycroft CLR(iflag, ISTRIP|INPCK|IGNPAR);
254 1.17 mycroft } else if (AP || EP || OP) {
255 1.17 mycroft CLR(cflag, CSIZE);
256 1.17 mycroft SET(cflag, CS7|PARENB);
257 1.17 mycroft SET(iflag, ISTRIP);
258 1.17 mycroft if (OP && !EP) {
259 1.17 mycroft SET(iflag, INPCK|IGNPAR);
260 1.17 mycroft SET(cflag, PARODD);
261 1.17 mycroft if (AP)
262 1.17 mycroft CLR(iflag, INPCK);
263 1.17 mycroft } else if (EP && !OP) {
264 1.17 mycroft SET(iflag, INPCK|IGNPAR);
265 1.17 mycroft CLR(cflag, PARODD);
266 1.17 mycroft if (AP)
267 1.17 mycroft CLR(iflag, INPCK);
268 1.22 mikel } else if (AP || (EP && OP)) {
269 1.17 mycroft CLR(iflag, INPCK|IGNPAR);
270 1.17 mycroft CLR(cflag, PARODD);
271 1.17 mycroft }
272 1.9 pk } /* else, leave as is */
273 1.1 cgd
274 1.7 pk #if 0
275 1.1 cgd if (UC)
276 1.1 cgd f |= LCASE;
277 1.7 pk #endif
278 1.1 cgd
279 1.7 pk if (HC)
280 1.17 mycroft SET(cflag, HUPCL);
281 1.9 pk else
282 1.17 mycroft CLR(cflag, HUPCL);
283 1.7 pk
284 1.13 pk if (MB)
285 1.17 mycroft SET(cflag, MDMBUF);
286 1.13 pk else
287 1.17 mycroft CLR(cflag, MDMBUF);
288 1.13 pk
289 1.7 pk if (NL) {
290 1.17 mycroft SET(iflag, ICRNL);
291 1.17 mycroft SET(oflag, ONLCR|OPOST);
292 1.15 mycroft } else {
293 1.17 mycroft CLR(iflag, ICRNL);
294 1.17 mycroft CLR(oflag, ONLCR);
295 1.7 pk }
296 1.1 cgd
297 1.16 mycroft if (!HT)
298 1.17 mycroft SET(oflag, OXTABS|OPOST);
299 1.16 mycroft else
300 1.17 mycroft CLR(oflag, OXTABS);
301 1.16 mycroft
302 1.7 pk #ifdef XXX_DELAY
303 1.17 mycroft SET(f, delaybits());
304 1.7 pk #endif
305 1.1 cgd
306 1.1 cgd if (n == 1) { /* read mode flags */
307 1.7 pk if (RW) {
308 1.7 pk iflag = 0;
309 1.17 mycroft CLR(oflag, OPOST);
310 1.17 mycroft CLR(cflag, CSIZE|PARENB);
311 1.17 mycroft SET(cflag, CS8);
312 1.7 pk lflag = 0;
313 1.7 pk } else {
314 1.17 mycroft CLR(lflag, ICANON);
315 1.7 pk }
316 1.7 pk goto out;
317 1.1 cgd }
318 1.1 cgd
319 1.1 cgd if (n == 0)
320 1.7 pk goto out;
321 1.1 cgd
322 1.7 pk #if 0
323 1.1 cgd if (CB)
324 1.17 mycroft SET(f, CRTBS);
325 1.7 pk #endif
326 1.1 cgd
327 1.1 cgd if (CE)
328 1.17 mycroft SET(lflag, ECHOE);
329 1.16 mycroft else
330 1.17 mycroft CLR(lflag, ECHOE);
331 1.1 cgd
332 1.1 cgd if (CK)
333 1.17 mycroft SET(lflag, ECHOKE);
334 1.16 mycroft else
335 1.17 mycroft CLR(lflag, ECHOKE);
336 1.1 cgd
337 1.1 cgd if (PE)
338 1.17 mycroft SET(lflag, ECHOPRT);
339 1.16 mycroft else
340 1.17 mycroft CLR(lflag, ECHOPRT);
341 1.1 cgd
342 1.1 cgd if (EC)
343 1.17 mycroft SET(lflag, ECHO);
344 1.16 mycroft else
345 1.17 mycroft CLR(lflag, ECHO);
346 1.1 cgd
347 1.1 cgd if (XC)
348 1.17 mycroft SET(lflag, ECHOCTL);
349 1.16 mycroft else
350 1.17 mycroft CLR(lflag, ECHOCTL);
351 1.1 cgd
352 1.1 cgd if (DX)
353 1.17 mycroft SET(lflag, IXANY);
354 1.16 mycroft else
355 1.17 mycroft CLR(lflag, IXANY);
356 1.1 cgd
357 1.7 pk out:
358 1.7 pk tmode.c_iflag = iflag;
359 1.7 pk tmode.c_oflag = oflag;
360 1.7 pk tmode.c_cflag = cflag;
361 1.7 pk tmode.c_lflag = lflag;
362 1.7 pk }
363 1.7 pk
364 1.7 pk #ifdef COMPAT_43
365 1.7 pk /*
366 1.7 pk * Old TTY => termios, snatched from <sys/kern/tty_compat.c>
367 1.7 pk */
368 1.7 pk void
369 1.25 thorpej compatflags(long flags)
370 1.7 pk {
371 1.23 mrg tcflag_t iflag, oflag, cflag, lflag;
372 1.7 pk
373 1.17 mycroft iflag = BRKINT|ICRNL|IMAXBEL|IXON|IXANY;
374 1.17 mycroft oflag = OPOST|ONLCR|OXTABS;
375 1.17 mycroft cflag = CREAD;
376 1.17 mycroft lflag = ICANON|ISIG|IEXTEN;
377 1.17 mycroft
378 1.17 mycroft if (ISSET(flags, TANDEM))
379 1.17 mycroft SET(iflag, IXOFF);
380 1.17 mycroft else
381 1.17 mycroft CLR(iflag, IXOFF);
382 1.17 mycroft if (ISSET(flags, ECHO))
383 1.17 mycroft SET(lflag, ECHO);
384 1.17 mycroft else
385 1.17 mycroft CLR(lflag, ECHO);
386 1.17 mycroft if (ISSET(flags, CRMOD)) {
387 1.17 mycroft SET(iflag, ICRNL);
388 1.17 mycroft SET(oflag, ONLCR);
389 1.8 pk } else {
390 1.17 mycroft CLR(iflag, ICRNL);
391 1.17 mycroft CLR(oflag, ONLCR);
392 1.8 pk }
393 1.17 mycroft if (ISSET(flags, XTABS))
394 1.17 mycroft SET(oflag, OXTABS);
395 1.8 pk else
396 1.17 mycroft CLR(oflag, OXTABS);
397 1.8 pk
398 1.17 mycroft
399 1.17 mycroft if (ISSET(flags, RAW)) {
400 1.8 pk iflag &= IXOFF;
401 1.17 mycroft CLR(lflag, ISIG|ICANON|IEXTEN);
402 1.18 mycroft CLR(cflag, PARENB);
403 1.7 pk } else {
404 1.17 mycroft SET(iflag, BRKINT|IXON|IMAXBEL);
405 1.17 mycroft SET(lflag, ISIG|IEXTEN);
406 1.17 mycroft if (ISSET(flags, CBREAK))
407 1.17 mycroft CLR(lflag, ICANON);
408 1.7 pk else
409 1.17 mycroft SET(lflag, ICANON);
410 1.18 mycroft switch (ISSET(flags, ANYP)) {
411 1.18 mycroft case 0:
412 1.18 mycroft CLR(cflag, PARENB);
413 1.18 mycroft break;
414 1.18 mycroft case ANYP:
415 1.18 mycroft SET(cflag, PARENB);
416 1.18 mycroft CLR(iflag, INPCK);
417 1.18 mycroft break;
418 1.18 mycroft case EVENP:
419 1.18 mycroft SET(cflag, PARENB);
420 1.18 mycroft SET(iflag, INPCK);
421 1.18 mycroft CLR(cflag, PARODD);
422 1.18 mycroft break;
423 1.18 mycroft case ODDP:
424 1.18 mycroft SET(cflag, PARENB);
425 1.18 mycroft SET(iflag, INPCK);
426 1.18 mycroft SET(cflag, PARODD);
427 1.18 mycroft break;
428 1.18 mycroft }
429 1.8 pk }
430 1.8 pk
431 1.17 mycroft /* Nothing we can do with CRTBS. */
432 1.17 mycroft if (ISSET(flags, PRTERA))
433 1.17 mycroft SET(lflag, ECHOPRT);
434 1.17 mycroft else
435 1.17 mycroft CLR(lflag, ECHOPRT);
436 1.17 mycroft if (ISSET(flags, CRTERA))
437 1.17 mycroft SET(lflag, ECHOE);
438 1.17 mycroft else
439 1.17 mycroft CLR(lflag, ECHOE);
440 1.17 mycroft /* Nothing we can do with TILDE. */
441 1.17 mycroft if (ISSET(flags, MDMBUF))
442 1.17 mycroft SET(cflag, MDMBUF);
443 1.17 mycroft else
444 1.17 mycroft CLR(cflag, MDMBUF);
445 1.17 mycroft if (ISSET(flags, NOHANG))
446 1.17 mycroft CLR(cflag, HUPCL);
447 1.17 mycroft else
448 1.17 mycroft SET(cflag, HUPCL);
449 1.17 mycroft if (ISSET(flags, CRTKIL))
450 1.17 mycroft SET(lflag, ECHOKE);
451 1.17 mycroft else
452 1.17 mycroft CLR(lflag, ECHOKE);
453 1.17 mycroft if (ISSET(flags, CTLECH))
454 1.17 mycroft SET(lflag, ECHOCTL);
455 1.17 mycroft else
456 1.17 mycroft CLR(lflag, ECHOCTL);
457 1.17 mycroft if (!ISSET(flags, DECCTQ))
458 1.17 mycroft SET(iflag, IXANY);
459 1.17 mycroft else
460 1.17 mycroft CLR(iflag, IXANY);
461 1.17 mycroft CLR(lflag, TOSTOP|FLUSHO|PENDIN|NOFLSH);
462 1.17 mycroft SET(lflag, ISSET(flags, TOSTOP|FLUSHO|PENDIN|NOFLSH));
463 1.17 mycroft
464 1.17 mycroft if (ISSET(flags, RAW|LITOUT|PASS8)) {
465 1.18 mycroft CLR(cflag, CSIZE);
466 1.17 mycroft SET(cflag, CS8);
467 1.17 mycroft if (!ISSET(flags, RAW|PASS8))
468 1.17 mycroft SET(iflag, ISTRIP);
469 1.8 pk else
470 1.17 mycroft CLR(iflag, ISTRIP);
471 1.17 mycroft if (!ISSET(flags, RAW|LITOUT))
472 1.17 mycroft SET(oflag, OPOST);
473 1.8 pk else
474 1.17 mycroft CLR(oflag, OPOST);
475 1.8 pk } else {
476 1.17 mycroft CLR(cflag, CSIZE);
477 1.18 mycroft SET(cflag, CS7);
478 1.17 mycroft SET(iflag, ISTRIP);
479 1.17 mycroft SET(oflag, OPOST);
480 1.7 pk }
481 1.7 pk
482 1.7 pk tmode.c_iflag = iflag;
483 1.7 pk tmode.c_oflag = oflag;
484 1.7 pk tmode.c_cflag = cflag;
485 1.7 pk tmode.c_lflag = lflag;
486 1.1 cgd }
487 1.7 pk #endif
488 1.1 cgd
489 1.7 pk #ifdef XXX_DELAY
490 1.1 cgd struct delayval {
491 1.1 cgd unsigned delay; /* delay in ms */
492 1.1 cgd int bits;
493 1.1 cgd };
494 1.1 cgd
495 1.1 cgd /*
496 1.1 cgd * below are random guesses, I can't be bothered checking
497 1.1 cgd */
498 1.1 cgd
499 1.1 cgd struct delayval crdelay[] = {
500 1.9 pk { 1, CR1 },
501 1.9 pk { 2, CR2 },
502 1.9 pk { 3, CR3 },
503 1.9 pk { 83, CR1 },
504 1.9 pk { 166, CR2 },
505 1.9 pk { 0, CR3 },
506 1.1 cgd };
507 1.1 cgd
508 1.1 cgd struct delayval nldelay[] = {
509 1.9 pk { 1, NL1 }, /* special, calculated */
510 1.9 pk { 2, NL2 },
511 1.9 pk { 3, NL3 },
512 1.9 pk { 100, NL2 },
513 1.9 pk { 0, NL3 },
514 1.1 cgd };
515 1.1 cgd
516 1.1 cgd struct delayval bsdelay[] = {
517 1.9 pk { 1, BS1 },
518 1.9 pk { 0, 0 },
519 1.1 cgd };
520 1.1 cgd
521 1.1 cgd struct delayval ffdelay[] = {
522 1.9 pk { 1, FF1 },
523 1.9 pk { 1750, FF1 },
524 1.9 pk { 0, FF1 },
525 1.1 cgd };
526 1.1 cgd
527 1.1 cgd struct delayval tbdelay[] = {
528 1.9 pk { 1, TAB1 },
529 1.9 pk { 2, TAB2 },
530 1.9 pk { 3, XTABS }, /* this is expand tabs */
531 1.9 pk { 100, TAB1 },
532 1.9 pk { 0, TAB2 },
533 1.1 cgd };
534 1.1 cgd
535 1.9 pk int
536 1.25 thorpej delaybits(void)
537 1.1 cgd {
538 1.23 mrg int f;
539 1.1 cgd
540 1.1 cgd f = adelay(CD, crdelay);
541 1.1 cgd f |= adelay(ND, nldelay);
542 1.1 cgd f |= adelay(FD, ffdelay);
543 1.1 cgd f |= adelay(TD, tbdelay);
544 1.1 cgd f |= adelay(BD, bsdelay);
545 1.1 cgd return (f);
546 1.1 cgd }
547 1.1 cgd
548 1.9 pk int
549 1.25 thorpej adelay(int ms, struct delayval *dp)
550 1.1 cgd {
551 1.1 cgd if (ms == 0)
552 1.1 cgd return (0);
553 1.1 cgd while (dp->delay && ms > dp->delay)
554 1.1 cgd dp++;
555 1.1 cgd return (dp->bits);
556 1.1 cgd }
557 1.7 pk #endif
558 1.1 cgd
559 1.20 thorpej char editedhost[MAXHOSTNAMELEN];
560 1.1 cgd
561 1.9 pk void
562 1.25 thorpej edithost(char *pat)
563 1.1 cgd {
564 1.23 mrg char *host = HN;
565 1.23 mrg char *res = editedhost;
566 1.1 cgd
567 1.1 cgd if (!pat)
568 1.1 cgd pat = "";
569 1.1 cgd while (*pat) {
570 1.1 cgd switch (*pat) {
571 1.1 cgd
572 1.1 cgd case '#':
573 1.1 cgd if (*host)
574 1.1 cgd host++;
575 1.1 cgd break;
576 1.1 cgd
577 1.1 cgd case '@':
578 1.1 cgd if (*host)
579 1.1 cgd *res++ = *host++;
580 1.1 cgd break;
581 1.1 cgd
582 1.1 cgd default:
583 1.1 cgd *res++ = *pat;
584 1.1 cgd break;
585 1.1 cgd
586 1.1 cgd }
587 1.1 cgd if (res == &editedhost[sizeof editedhost - 1]) {
588 1.1 cgd *res = '\0';
589 1.1 cgd return;
590 1.1 cgd }
591 1.1 cgd pat++;
592 1.1 cgd }
593 1.1 cgd if (*host)
594 1.1 cgd strncpy(res, host, sizeof editedhost - (res - editedhost) - 1);
595 1.1 cgd else
596 1.1 cgd *res = '\0';
597 1.1 cgd editedhost[sizeof editedhost - 1] = '\0';
598 1.1 cgd }
599 1.1 cgd
600 1.9 pk void
601 1.25 thorpej makeenv(char *env[])
602 1.1 cgd {
603 1.1 cgd static char termbuf[128] = "TERM=";
604 1.23 mrg char *p, *q;
605 1.23 mrg char **ep;
606 1.1 cgd
607 1.1 cgd ep = env;
608 1.1 cgd if (TT && *TT) {
609 1.24 mjl strlcat(termbuf, TT, sizeof(termbuf));
610 1.1 cgd *ep++ = termbuf;
611 1.1 cgd }
612 1.22 mikel if ((p = EV) != NULL) {
613 1.1 cgd q = p;
614 1.22 mikel while ((q = strchr(q, ',')) != NULL) {
615 1.1 cgd *q++ = '\0';
616 1.1 cgd *ep++ = p;
617 1.1 cgd p = q;
618 1.1 cgd }
619 1.1 cgd if (*p)
620 1.1 cgd *ep++ = p;
621 1.1 cgd }
622 1.1 cgd *ep = (char *)0;
623 1.1 cgd }
624 1.1 cgd
625 1.1 cgd /*
626 1.1 cgd * This speed select mechanism is written for the Develcon DATASWITCH.
627 1.1 cgd * The Develcon sends a string of the form "B{speed}\n" at a predefined
628 1.1 cgd * baud rate. This string indicates the user's actual speed.
629 1.1 cgd * The routine below returns the terminal type mapped from derived speed.
630 1.1 cgd */
631 1.1 cgd struct portselect {
632 1.1 cgd char *ps_baud;
633 1.1 cgd char *ps_type;
634 1.1 cgd } portspeeds[] = {
635 1.1 cgd { "B110", "std.110" },
636 1.1 cgd { "B134", "std.134" },
637 1.1 cgd { "B150", "std.150" },
638 1.1 cgd { "B300", "std.300" },
639 1.1 cgd { "B600", "std.600" },
640 1.1 cgd { "B1200", "std.1200" },
641 1.1 cgd { "B2400", "std.2400" },
642 1.1 cgd { "B4800", "std.4800" },
643 1.1 cgd { "B9600", "std.9600" },
644 1.1 cgd { "B19200", "std.19200" },
645 1.1 cgd { 0 }
646 1.1 cgd };
647 1.1 cgd
648 1.1 cgd char *
649 1.25 thorpej portselector(void)
650 1.1 cgd {
651 1.1 cgd char c, baud[20], *type = "default";
652 1.23 mrg struct portselect *ps;
653 1.1 cgd int len;
654 1.1 cgd
655 1.1 cgd alarm(5*60);
656 1.1 cgd for (len = 0; len < sizeof (baud) - 1; len++) {
657 1.1 cgd if (read(STDIN_FILENO, &c, 1) <= 0)
658 1.1 cgd break;
659 1.1 cgd c &= 0177;
660 1.1 cgd if (c == '\n' || c == '\r')
661 1.1 cgd break;
662 1.1 cgd if (c == 'B')
663 1.1 cgd len = 0; /* in case of leading garbage */
664 1.1 cgd baud[len] = c;
665 1.1 cgd }
666 1.1 cgd baud[len] = '\0';
667 1.1 cgd for (ps = portspeeds; ps->ps_baud; ps++)
668 1.1 cgd if (strcmp(ps->ps_baud, baud) == 0) {
669 1.1 cgd type = ps->ps_type;
670 1.1 cgd break;
671 1.1 cgd }
672 1.1 cgd sleep(2); /* wait for connection to complete */
673 1.1 cgd return (type);
674 1.1 cgd }
675 1.1 cgd
676 1.1 cgd /*
677 1.1 cgd * This auto-baud speed select mechanism is written for the Micom 600
678 1.1 cgd * portselector. Selection is done by looking at how the character '\r'
679 1.1 cgd * is garbled at the different speeds.
680 1.1 cgd */
681 1.1 cgd #include <sys/time.h>
682 1.1 cgd
683 1.1 cgd char *
684 1.25 thorpej autobaud(void)
685 1.1 cgd {
686 1.26 mycroft struct pollfd set[1];
687 1.26 mycroft struct timespec timeout;
688 1.1 cgd char c, *type = "9600-baud";
689 1.1 cgd
690 1.14 pk (void)tcflush(0, TCIOFLUSH);
691 1.26 mycroft set[0].fd = STDIN_FILENO;
692 1.26 mycroft set[0].events = POLLIN;
693 1.26 mycroft if (poll(set, 1, 5000) <= 0)
694 1.1 cgd return (type);
695 1.24 mjl if (read(STDIN_FILENO, &c, 1) != 1)
696 1.1 cgd return (type);
697 1.1 cgd timeout.tv_sec = 0;
698 1.26 mycroft timeout.tv_nsec = 20000;
699 1.26 mycroft (void)nanosleep(&timeout, NULL);
700 1.14 pk (void)tcflush(0, TCIOFLUSH);
701 1.1 cgd switch (c & 0377) {
702 1.1 cgd
703 1.1 cgd case 0200: /* 300-baud */
704 1.1 cgd type = "300-baud";
705 1.1 cgd break;
706 1.1 cgd
707 1.1 cgd case 0346: /* 1200-baud */
708 1.1 cgd type = "1200-baud";
709 1.1 cgd break;
710 1.1 cgd
711 1.1 cgd case 015: /* 2400-baud */
712 1.1 cgd case 0215:
713 1.1 cgd type = "2400-baud";
714 1.1 cgd break;
715 1.1 cgd
716 1.1 cgd default: /* 4800-baud */
717 1.1 cgd type = "4800-baud";
718 1.1 cgd break;
719 1.1 cgd
720 1.1 cgd case 0377: /* 9600-baud */
721 1.1 cgd type = "9600-baud";
722 1.1 cgd break;
723 1.1 cgd }
724 1.1 cgd return (type);
725 1.1 cgd }
726