subr.c revision 1.17 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.17 1995/10/05 07:16:25 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 } else {
397 SET(iflag, BRKINT|IXON|IMAXBEL);
398 SET(lflag, ISIG|IEXTEN);
399 if (ISSET(flags, CBREAK))
400 CLR(lflag, ICANON);
401 else
402 SET(lflag, ICANON);
403 }
404
405 switch (ISSET(flags, ANYP)) {
406 case EVENP:
407 SET(iflag, INPCK);
408 CLR(cflag, PARODD);
409 break;
410 case ODDP:
411 SET(iflag, INPCK);
412 SET(cflag, PARODD);
413 break;
414 default:
415 CLR(iflag, INPCK);
416 break;
417 }
418
419 /* Nothing we can do with CRTBS. */
420 if (ISSET(flags, PRTERA))
421 SET(lflag, ECHOPRT);
422 else
423 CLR(lflag, ECHOPRT);
424 if (ISSET(flags, CRTERA))
425 SET(lflag, ECHOE);
426 else
427 CLR(lflag, ECHOE);
428 /* Nothing we can do with TILDE. */
429 if (ISSET(flags, MDMBUF))
430 SET(cflag, MDMBUF);
431 else
432 CLR(cflag, MDMBUF);
433 if (ISSET(flags, NOHANG))
434 CLR(cflag, HUPCL);
435 else
436 SET(cflag, HUPCL);
437 if (ISSET(flags, CRTKIL))
438 SET(lflag, ECHOKE);
439 else
440 CLR(lflag, ECHOKE);
441 if (ISSET(flags, CTLECH))
442 SET(lflag, ECHOCTL);
443 else
444 CLR(lflag, ECHOCTL);
445 if (!ISSET(flags, DECCTQ))
446 SET(iflag, IXANY);
447 else
448 CLR(iflag, IXANY);
449 CLR(lflag, TOSTOP|FLUSHO|PENDIN|NOFLSH);
450 SET(lflag, ISSET(flags, TOSTOP|FLUSHO|PENDIN|NOFLSH));
451
452 if (ISSET(flags, RAW|LITOUT|PASS8)) {
453 CLR(cflag, CSIZE|PARENB);
454 SET(cflag, CS8);
455 if (!ISSET(flags, RAW|PASS8))
456 SET(iflag, ISTRIP);
457 else
458 CLR(iflag, ISTRIP);
459 if (!ISSET(flags, RAW|LITOUT))
460 SET(oflag, OPOST);
461 else
462 CLR(oflag, OPOST);
463 } else {
464 CLR(cflag, CSIZE);
465 SET(cflag, CS7|PARENB);
466 SET(iflag, ISTRIP);
467 SET(oflag, OPOST);
468 }
469
470 tmode.c_iflag = iflag;
471 tmode.c_oflag = oflag;
472 tmode.c_cflag = cflag;
473 tmode.c_lflag = lflag;
474 }
475 #endif
476
477 #ifdef XXX_DELAY
478 struct delayval {
479 unsigned delay; /* delay in ms */
480 int bits;
481 };
482
483 /*
484 * below are random guesses, I can't be bothered checking
485 */
486
487 struct delayval crdelay[] = {
488 { 1, CR1 },
489 { 2, CR2 },
490 { 3, CR3 },
491 { 83, CR1 },
492 { 166, CR2 },
493 { 0, CR3 },
494 };
495
496 struct delayval nldelay[] = {
497 { 1, NL1 }, /* special, calculated */
498 { 2, NL2 },
499 { 3, NL3 },
500 { 100, NL2 },
501 { 0, NL3 },
502 };
503
504 struct delayval bsdelay[] = {
505 { 1, BS1 },
506 { 0, 0 },
507 };
508
509 struct delayval ffdelay[] = {
510 { 1, FF1 },
511 { 1750, FF1 },
512 { 0, FF1 },
513 };
514
515 struct delayval tbdelay[] = {
516 { 1, TAB1 },
517 { 2, TAB2 },
518 { 3, XTABS }, /* this is expand tabs */
519 { 100, TAB1 },
520 { 0, TAB2 },
521 };
522
523 int
524 delaybits()
525 {
526 register int f;
527
528 f = adelay(CD, crdelay);
529 f |= adelay(ND, nldelay);
530 f |= adelay(FD, ffdelay);
531 f |= adelay(TD, tbdelay);
532 f |= adelay(BD, bsdelay);
533 return (f);
534 }
535
536 int
537 adelay(ms, dp)
538 register ms;
539 register struct delayval *dp;
540 {
541 if (ms == 0)
542 return (0);
543 while (dp->delay && ms > dp->delay)
544 dp++;
545 return (dp->bits);
546 }
547 #endif
548
549 char editedhost[32];
550
551 void
552 edithost(pat)
553 register char *pat;
554 {
555 register char *host = HN;
556 register char *res = editedhost;
557
558 if (!pat)
559 pat = "";
560 while (*pat) {
561 switch (*pat) {
562
563 case '#':
564 if (*host)
565 host++;
566 break;
567
568 case '@':
569 if (*host)
570 *res++ = *host++;
571 break;
572
573 default:
574 *res++ = *pat;
575 break;
576
577 }
578 if (res == &editedhost[sizeof editedhost - 1]) {
579 *res = '\0';
580 return;
581 }
582 pat++;
583 }
584 if (*host)
585 strncpy(res, host, sizeof editedhost - (res - editedhost) - 1);
586 else
587 *res = '\0';
588 editedhost[sizeof editedhost - 1] = '\0';
589 }
590
591 void
592 makeenv(env)
593 char *env[];
594 {
595 static char termbuf[128] = "TERM=";
596 register char *p, *q;
597 register char **ep;
598
599 ep = env;
600 if (TT && *TT) {
601 strcat(termbuf, TT);
602 *ep++ = termbuf;
603 }
604 if (p = EV) {
605 q = p;
606 while (q = strchr(q, ',')) {
607 *q++ = '\0';
608 *ep++ = p;
609 p = q;
610 }
611 if (*p)
612 *ep++ = p;
613 }
614 *ep = (char *)0;
615 }
616
617 /*
618 * This speed select mechanism is written for the Develcon DATASWITCH.
619 * The Develcon sends a string of the form "B{speed}\n" at a predefined
620 * baud rate. This string indicates the user's actual speed.
621 * The routine below returns the terminal type mapped from derived speed.
622 */
623 struct portselect {
624 char *ps_baud;
625 char *ps_type;
626 } portspeeds[] = {
627 { "B110", "std.110" },
628 { "B134", "std.134" },
629 { "B150", "std.150" },
630 { "B300", "std.300" },
631 { "B600", "std.600" },
632 { "B1200", "std.1200" },
633 { "B2400", "std.2400" },
634 { "B4800", "std.4800" },
635 { "B9600", "std.9600" },
636 { "B19200", "std.19200" },
637 { 0 }
638 };
639
640 char *
641 portselector()
642 {
643 char c, baud[20], *type = "default";
644 register struct portselect *ps;
645 int len;
646
647 alarm(5*60);
648 for (len = 0; len < sizeof (baud) - 1; len++) {
649 if (read(STDIN_FILENO, &c, 1) <= 0)
650 break;
651 c &= 0177;
652 if (c == '\n' || c == '\r')
653 break;
654 if (c == 'B')
655 len = 0; /* in case of leading garbage */
656 baud[len] = c;
657 }
658 baud[len] = '\0';
659 for (ps = portspeeds; ps->ps_baud; ps++)
660 if (strcmp(ps->ps_baud, baud) == 0) {
661 type = ps->ps_type;
662 break;
663 }
664 sleep(2); /* wait for connection to complete */
665 return (type);
666 }
667
668 /*
669 * This auto-baud speed select mechanism is written for the Micom 600
670 * portselector. Selection is done by looking at how the character '\r'
671 * is garbled at the different speeds.
672 */
673 #include <sys/time.h>
674
675 char *
676 autobaud()
677 {
678 int rfds;
679 struct timeval timeout;
680 char c, *type = "9600-baud";
681
682 (void)tcflush(0, TCIOFLUSH);
683 rfds = 1 << 0;
684 timeout.tv_sec = 5;
685 timeout.tv_usec = 0;
686 if (select(32, (fd_set *)&rfds, (fd_set *)NULL,
687 (fd_set *)NULL, &timeout) <= 0)
688 return (type);
689 if (read(STDIN_FILENO, &c, sizeof(char)) != sizeof(char))
690 return (type);
691 timeout.tv_sec = 0;
692 timeout.tv_usec = 20;
693 (void) select(32, (fd_set *)NULL, (fd_set *)NULL,
694 (fd_set *)NULL, &timeout);
695 (void)tcflush(0, TCIOFLUSH);
696 switch (c & 0377) {
697
698 case 0200: /* 300-baud */
699 type = "300-baud";
700 break;
701
702 case 0346: /* 1200-baud */
703 type = "1200-baud";
704 break;
705
706 case 015: /* 2400-baud */
707 case 0215:
708 type = "2400-baud";
709 break;
710
711 default: /* 4800-baud */
712 type = "4800-baud";
713 break;
714
715 case 0377: /* 9600-baud */
716 type = "9600-baud";
717 break;
718 }
719 return (type);
720 }
721