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