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