1 /* $NetBSD: subr.c,v 1.19 1996/07/31 20:40:36 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 #if 0 38 static char sccsid[] = "from: @(#)subr.c 8.1 (Berkeley) 6/4/93"; 39 #else 40 static char rcsid[] = "$NetBSD: subr.c,v 1.19 1996/07/31 20:40:36 thorpej Exp $"; 41 #endif 42 #endif /* not lint */ 43 44 /* 45 * Melbourne getty. 46 */ 47 #define COMPAT_43 48 #include <stdlib.h> 49 #include <unistd.h> 50 #include <string.h> 51 #include <termios.h> 52 #include <sys/ioctl.h> 53 54 #include "gettytab.h" 55 #include "pathnames.h" 56 #include "extern.h" 57 58 extern struct termios tmode, omode; 59 60 static void compatflags __P((long)); 61 62 /* 63 * Get a table entry. 64 */ 65 void 66 gettable(name, buf) 67 char *name, *buf; 68 { 69 register struct gettystrs *sp; 70 register struct gettynums *np; 71 register struct gettyflags *fp; 72 long n; 73 char *dba[2]; 74 dba[0] = _PATH_GETTYTAB; 75 dba[1] = 0; 76 77 if (cgetent(&buf, dba, name) != 0) 78 return; 79 80 for (sp = gettystrs; sp->field; sp++) 81 cgetstr(buf, sp->field, &sp->value); 82 for (np = gettynums; np->field; np++) { 83 if (cgetnum(buf, np->field, &n) == -1) 84 np->set = 0; 85 else { 86 np->set = 1; 87 np->value = n; 88 } 89 } 90 for (fp = gettyflags; fp->field; fp++) { 91 if (cgetcap(buf, fp->field, ':') == NULL) 92 fp->set = 0; 93 else { 94 fp->set = 1; 95 fp->value = 1 ^ fp->invrt; 96 } 97 } 98 #ifdef DEBUG 99 printf("name=\"%s\", buf=\"%s\"\n", name, buf); 100 for (sp = gettystrs; sp->field; sp++) 101 printf("cgetstr: %s=%s\n", sp->field, sp->value); 102 for (np = gettynums; np->field; np++) 103 printf("cgetnum: %s=%d\n", np->field, np->value); 104 for (fp = gettyflags; fp->field; fp++) 105 printf("cgetflags: %s='%c' set='%c'\n", fp->field, 106 fp->value + '0', fp->set + '0'); 107 exit(1); 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 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[32]; 561 562 void 563 edithost(pat) 564 register char *pat; 565 { 566 register 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 void 603 makeenv(env) 604 char *env[]; 605 { 606 static char termbuf[128] = "TERM="; 607 register char *p, *q; 608 register char **ep; 609 610 ep = env; 611 if (TT && *TT) { 612 strcat(termbuf, TT); 613 *ep++ = termbuf; 614 } 615 if (p = EV) { 616 q = p; 617 while (q = strchr(q, ',')) { 618 *q++ = '\0'; 619 *ep++ = p; 620 p = q; 621 } 622 if (*p) 623 *ep++ = p; 624 } 625 *ep = (char *)0; 626 } 627 628 /* 629 * This speed select mechanism is written for the Develcon DATASWITCH. 630 * The Develcon sends a string of the form "B{speed}\n" at a predefined 631 * baud rate. This string indicates the user's actual speed. 632 * The routine below returns the terminal type mapped from derived speed. 633 */ 634 struct portselect { 635 char *ps_baud; 636 char *ps_type; 637 } portspeeds[] = { 638 { "B110", "std.110" }, 639 { "B134", "std.134" }, 640 { "B150", "std.150" }, 641 { "B300", "std.300" }, 642 { "B600", "std.600" }, 643 { "B1200", "std.1200" }, 644 { "B2400", "std.2400" }, 645 { "B4800", "std.4800" }, 646 { "B9600", "std.9600" }, 647 { "B19200", "std.19200" }, 648 { 0 } 649 }; 650 651 char * 652 portselector() 653 { 654 char c, baud[20], *type = "default"; 655 register struct portselect *ps; 656 int len; 657 658 alarm(5*60); 659 for (len = 0; len < sizeof (baud) - 1; len++) { 660 if (read(STDIN_FILENO, &c, 1) <= 0) 661 break; 662 c &= 0177; 663 if (c == '\n' || c == '\r') 664 break; 665 if (c == 'B') 666 len = 0; /* in case of leading garbage */ 667 baud[len] = c; 668 } 669 baud[len] = '\0'; 670 for (ps = portspeeds; ps->ps_baud; ps++) 671 if (strcmp(ps->ps_baud, baud) == 0) { 672 type = ps->ps_type; 673 break; 674 } 675 sleep(2); /* wait for connection to complete */ 676 return (type); 677 } 678 679 /* 680 * This auto-baud speed select mechanism is written for the Micom 600 681 * portselector. Selection is done by looking at how the character '\r' 682 * is garbled at the different speeds. 683 */ 684 #include <sys/time.h> 685 686 char * 687 autobaud() 688 { 689 int rfds; 690 struct timeval timeout; 691 char c, *type = "9600-baud"; 692 693 (void)tcflush(0, TCIOFLUSH); 694 rfds = 1 << 0; 695 timeout.tv_sec = 5; 696 timeout.tv_usec = 0; 697 if (select(32, (fd_set *)&rfds, (fd_set *)NULL, 698 (fd_set *)NULL, &timeout) <= 0) 699 return (type); 700 if (read(STDIN_FILENO, &c, sizeof(char)) != sizeof(char)) 701 return (type); 702 timeout.tv_sec = 0; 703 timeout.tv_usec = 20; 704 (void) select(32, (fd_set *)NULL, (fd_set *)NULL, 705 (fd_set *)NULL, &timeout); 706 (void)tcflush(0, TCIOFLUSH); 707 switch (c & 0377) { 708 709 case 0200: /* 300-baud */ 710 type = "300-baud"; 711 break; 712 713 case 0346: /* 1200-baud */ 714 type = "1200-baud"; 715 break; 716 717 case 015: /* 2400-baud */ 718 case 0215: 719 type = "2400-baud"; 720 break; 721 722 default: /* 4800-baud */ 723 type = "4800-baud"; 724 break; 725 726 case 0377: /* 9600-baud */ 727 type = "9600-baud"; 728 break; 729 } 730 return (type); 731 } 732