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