subr.c revision 1.9 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.9 1994/08/17 20:10:40 pk 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] = '\377';
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 } else if (EP && !OP) {
249 BIS(iflag, ISTRIP|INPCK|IGNPAR);
250 BICS(cflag, CSIZE|PARODD, PARENB|CS7);
251 } else if (AP || EP && OP) {
252 BICS(iflag, INPCK|IGNPAR, ISTRIP);
253 BICS(cflag, CSIZE|PARODD, PARENB|CS7);
254 } /* else, leave as is */
255
256 #if 0
257 if (UC)
258 f |= LCASE;
259 #endif
260
261 if (HC)
262 cflag |= HUPCL;
263 else
264 cflag &= ~HUPCL;
265
266 if (NL) {
267 iflag |= ICRNL;
268 oflag |= ONLCR;
269 }
270
271 #ifdef XXX_DELAY
272 f |= delaybits();
273 #endif
274
275 if (n == 1) { /* read mode flags */
276 if (RW) {
277 iflag = 0;
278 oflag = 0;
279 cflag = CREAD|CS8;
280 lflag = 0;
281 } else {
282 lflag &= ~ICANON;
283 }
284 goto out;
285 }
286
287 if (HT)
288 oflag &= ~OXTABS;
289 else
290 oflag |= OXTABS;
291
292 if (n == 0)
293 goto out;
294
295 #if 0
296 if (CB)
297 f |= CRTBS;
298 #endif
299
300 if (CE)
301 lflag |= ECHOE;
302
303 if (CK)
304 lflag |= ECHOKE;
305
306 if (PE)
307 lflag |= ECHOPRT;
308
309 if (EC)
310 lflag |= ECHO;
311
312 if (XC)
313 lflag |= ECHOCTL;
314
315 if (DX)
316 lflag |= IXANY;
317
318 if (MB)
319 cflag |= MDMBUF;
320 else
321 cflag &= ~MDMBUF;
322
323 out:
324 tmode.c_iflag = iflag;
325 tmode.c_oflag = oflag;
326 tmode.c_cflag = cflag;
327 tmode.c_lflag = lflag;
328 }
329
330 #ifdef COMPAT_43
331 /*
332 * Old TTY => termios, snatched from <sys/kern/tty_compat.c>
333 */
334 void
335 compatflags(flags)
336 register long flags;
337 {
338 register tcflag_t iflag, oflag, cflag, lflag;
339
340 iflag = (BRKINT|ICRNL|IMAXBEL|IXON|IXANY);
341 oflag = (OPOST|ONLCR|OXTABS);
342 cflag = (CREAD);
343 lflag = (ICANON|ISIG|IEXTEN);
344
345 if (flags & TANDEM)
346 iflag |= IXOFF;
347 else
348 iflag &= ~IXOFF;
349 if (flags & ECHO)
350 lflag |= ECHO;
351 else
352 lflag &= ~ECHO;
353 if (flags & CRMOD) {
354 iflag |= ICRNL;
355 oflag |= ONLCR;
356 } else {
357 iflag &= ~ICRNL;
358 oflag &= ~ONLCR;
359 }
360 if (flags & XTABS)
361 oflag |= OXTABS;
362 else
363 oflag &= ~OXTABS;
364
365 if (flags & RAW) {
366 iflag &= IXOFF;
367 lflag &= ~(ISIG|ICANON|IEXTEN);
368 } else {
369 iflag |= BRKINT|IXON|IMAXBEL;
370 lflag |= ISIG|IEXTEN;
371 if (flags & CBREAK)
372 lflag &= ~ICANON;
373 else
374 lflag |= ICANON;
375 }
376
377 switch (flags & ANYP) {
378 case EVENP:
379 iflag |= INPCK;
380 cflag &= ~PARODD;
381 break;
382 case ODDP:
383 iflag |= INPCK;
384 cflag |= PARODD;
385 break;
386 default:
387 iflag &= ~INPCK;
388 break;
389 }
390
391 if (flags & (RAW|LITOUT|PASS8)) {
392 cflag &= ~(CSIZE|PARENB);
393 cflag |= CS8;
394 if ((flags & (RAW|PASS8)) == 0)
395 iflag |= ISTRIP;
396 else
397 iflag &= ~ISTRIP;
398 if ((flags & (RAW|LITOUT)) == 0)
399 oflag |= OPOST;
400 else
401 oflag &= ~OPOST;
402 } else {
403 cflag &= ~CSIZE;
404 cflag |= CS7|PARENB;
405 iflag |= ISTRIP;
406 oflag |= OPOST;
407 }
408
409 if (flags & PRTERA)
410 lflag |= ECHOPRT;
411 else
412 lflag &= ~ECHOPRT;
413 if (flags & CRTERA)
414 lflag |= ECHOE;
415 else
416 lflag &= ~ECHOE;
417 if (flags & MDMBUF)
418 cflag |= MDMBUF;
419 else
420 cflag &= ~MDMBUF;
421 if (flags & NOHANG)
422 cflag &= ~HUPCL;
423 else
424 cflag |= HUPCL;
425 if (flags & CRTKIL)
426 lflag |= ECHOKE;
427 else
428 lflag &= ~ECHOKE;
429 if (flags & CTLECH)
430 lflag |= ECHOCTL;
431 else
432 lflag &= ~ECHOCTL;
433 if ((flags & DECCTQ) == 0)
434 lflag |= IXANY;
435 else
436 lflag &= ~IXANY;
437 lflag &= ~(TOSTOP|FLUSHO|PENDIN|NOFLSH);
438 lflag |= flags & (TOSTOP|FLUSHO|PENDIN|NOFLSH);
439
440 if (flags & (RAW|LITOUT|PASS8)) {
441 cflag &= ~(CSIZE|PARENB);
442 cflag |= CS8;
443 if ((flags & (RAW|PASS8)) == 0)
444 iflag |= ISTRIP;
445 else
446 iflag &= ~ISTRIP;
447 if ((flags & (RAW|LITOUT)) == 0)
448 oflag |= OPOST;
449 else
450 oflag &= ~OPOST;
451 } else {
452 cflag &= ~CSIZE;
453 cflag |= CS7|PARENB;
454 iflag |= ISTRIP;
455 oflag |= OPOST;
456 }
457
458 tmode.c_iflag = iflag;
459 tmode.c_oflag = oflag;
460 tmode.c_cflag = cflag;
461 tmode.c_lflag = lflag;
462 }
463 #endif
464
465 #ifdef XXX_DELAY
466 struct delayval {
467 unsigned delay; /* delay in ms */
468 int bits;
469 };
470
471 /*
472 * below are random guesses, I can't be bothered checking
473 */
474
475 struct delayval crdelay[] = {
476 { 1, CR1 },
477 { 2, CR2 },
478 { 3, CR3 },
479 { 83, CR1 },
480 { 166, CR2 },
481 { 0, CR3 },
482 };
483
484 struct delayval nldelay[] = {
485 { 1, NL1 }, /* special, calculated */
486 { 2, NL2 },
487 { 3, NL3 },
488 { 100, NL2 },
489 { 0, NL3 },
490 };
491
492 struct delayval bsdelay[] = {
493 { 1, BS1 },
494 { 0, 0 },
495 };
496
497 struct delayval ffdelay[] = {
498 { 1, FF1 },
499 { 1750, FF1 },
500 { 0, FF1 },
501 };
502
503 struct delayval tbdelay[] = {
504 { 1, TAB1 },
505 { 2, TAB2 },
506 { 3, XTABS }, /* this is expand tabs */
507 { 100, TAB1 },
508 { 0, TAB2 },
509 };
510
511 int
512 delaybits()
513 {
514 register int f;
515
516 f = adelay(CD, crdelay);
517 f |= adelay(ND, nldelay);
518 f |= adelay(FD, ffdelay);
519 f |= adelay(TD, tbdelay);
520 f |= adelay(BD, bsdelay);
521 return (f);
522 }
523
524 int
525 adelay(ms, dp)
526 register ms;
527 register struct delayval *dp;
528 {
529 if (ms == 0)
530 return (0);
531 while (dp->delay && ms > dp->delay)
532 dp++;
533 return (dp->bits);
534 }
535 #endif
536
537 char editedhost[32];
538
539 void
540 edithost(pat)
541 register char *pat;
542 {
543 register char *host = HN;
544 register char *res = editedhost;
545
546 if (!pat)
547 pat = "";
548 while (*pat) {
549 switch (*pat) {
550
551 case '#':
552 if (*host)
553 host++;
554 break;
555
556 case '@':
557 if (*host)
558 *res++ = *host++;
559 break;
560
561 default:
562 *res++ = *pat;
563 break;
564
565 }
566 if (res == &editedhost[sizeof editedhost - 1]) {
567 *res = '\0';
568 return;
569 }
570 pat++;
571 }
572 if (*host)
573 strncpy(res, host, sizeof editedhost - (res - editedhost) - 1);
574 else
575 *res = '\0';
576 editedhost[sizeof editedhost - 1] = '\0';
577 }
578
579 struct speedtab {
580 int speed;
581 int uxname;
582 } speedtab[] = {
583 { 50, B50 },
584 { 75, B75 },
585 { 110, B110 },
586 { 134, B134 },
587 { 150, B150 },
588 { 200, B200 },
589 { 300, B300 },
590 { 600, B600 },
591 { 1200, B1200 },
592 { 1800, B1800 },
593 { 2400, B2400 },
594 { 4800, B4800 },
595 { 9600, B9600 },
596 { 19200, EXTA },
597 { 19, EXTA }, /* for people who say 19.2K */
598 { 38400, EXTB },
599 { 38, EXTB },
600 { 7200, EXTB }, /* alternative */
601 { 57600, B57600},
602 { 115200, B115200},
603 { 0 }
604 };
605
606 int
607 speed(val)
608 int val;
609 {
610 register struct speedtab *sp;
611
612 if (val <= 15)
613 return (val);
614
615 for (sp = speedtab; sp->speed; sp++)
616 if (sp->speed == val)
617 return (sp->uxname);
618
619 return (B300); /* default in impossible cases */
620 }
621
622 void
623 makeenv(env)
624 char *env[];
625 {
626 static char termbuf[128] = "TERM=";
627 register char *p, *q;
628 register char **ep;
629
630 ep = env;
631 if (TT && *TT) {
632 strcat(termbuf, TT);
633 *ep++ = termbuf;
634 }
635 if (p = EV) {
636 q = p;
637 while (q = strchr(q, ',')) {
638 *q++ = '\0';
639 *ep++ = p;
640 p = q;
641 }
642 if (*p)
643 *ep++ = p;
644 }
645 *ep = (char *)0;
646 }
647
648 /*
649 * This speed select mechanism is written for the Develcon DATASWITCH.
650 * The Develcon sends a string of the form "B{speed}\n" at a predefined
651 * baud rate. This string indicates the user's actual speed.
652 * The routine below returns the terminal type mapped from derived speed.
653 */
654 struct portselect {
655 char *ps_baud;
656 char *ps_type;
657 } portspeeds[] = {
658 { "B110", "std.110" },
659 { "B134", "std.134" },
660 { "B150", "std.150" },
661 { "B300", "std.300" },
662 { "B600", "std.600" },
663 { "B1200", "std.1200" },
664 { "B2400", "std.2400" },
665 { "B4800", "std.4800" },
666 { "B9600", "std.9600" },
667 { "B19200", "std.19200" },
668 { 0 }
669 };
670
671 char *
672 portselector()
673 {
674 char c, baud[20], *type = "default";
675 register struct portselect *ps;
676 int len;
677
678 alarm(5*60);
679 for (len = 0; len < sizeof (baud) - 1; len++) {
680 if (read(STDIN_FILENO, &c, 1) <= 0)
681 break;
682 c &= 0177;
683 if (c == '\n' || c == '\r')
684 break;
685 if (c == 'B')
686 len = 0; /* in case of leading garbage */
687 baud[len] = c;
688 }
689 baud[len] = '\0';
690 for (ps = portspeeds; ps->ps_baud; ps++)
691 if (strcmp(ps->ps_baud, baud) == 0) {
692 type = ps->ps_type;
693 break;
694 }
695 sleep(2); /* wait for connection to complete */
696 return (type);
697 }
698
699 /*
700 * This auto-baud speed select mechanism is written for the Micom 600
701 * portselector. Selection is done by looking at how the character '\r'
702 * is garbled at the different speeds.
703 */
704 #include <sys/time.h>
705
706 char *
707 autobaud()
708 {
709 int rfds;
710 struct timeval timeout;
711 char c, *type = "9600-baud";
712 int null = 0;
713
714 ioctl(0, TIOCFLUSH, &null);
715 rfds = 1 << 0;
716 timeout.tv_sec = 5;
717 timeout.tv_usec = 0;
718 if (select(32, (fd_set *)&rfds, (fd_set *)NULL,
719 (fd_set *)NULL, &timeout) <= 0)
720 return (type);
721 if (read(STDIN_FILENO, &c, sizeof(char)) != sizeof(char))
722 return (type);
723 timeout.tv_sec = 0;
724 timeout.tv_usec = 20;
725 (void) select(32, (fd_set *)NULL, (fd_set *)NULL,
726 (fd_set *)NULL, &timeout);
727 ioctl(0, TIOCFLUSH, &null);
728 switch (c & 0377) {
729
730 case 0200: /* 300-baud */
731 type = "300-baud";
732 break;
733
734 case 0346: /* 1200-baud */
735 type = "1200-baud";
736 break;
737
738 case 015: /* 2400-baud */
739 case 0215:
740 type = "2400-baud";
741 break;
742
743 default: /* 4800-baud */
744 type = "4800-baud";
745 break;
746
747 case 0377: /* 9600-baud */
748 type = "9600-baud";
749 break;
750 }
751 return (type);
752 }
753