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