1/* $NetBSD: subr.c,v 1.34 2013/08/11 05:42:41 dholland 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. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32#include <sys/cdefs.h> 33#ifndef lint 34#if 0 35static char sccsid[] = "from: @(#)subr.c 8.1 (Berkeley) 6/4/93"; 36#else 37__RCSID("$NetBSD: subr.c,v 1.34 2013/08/11 05:42:41 dholland Exp $"); 38#endif 39#endif /* not lint */ 40 41/* 42 * Melbourne getty. 43 */ 44#define COMPAT_43 45#include <sys/param.h> 46#include <sys/ioctl.h> 47 48#include <stdlib.h> 49#include <string.h> 50#include <termios.h> 51#include <unistd.h> 52#include <poll.h> 53#include <util.h> 54 55#include "extern.h" 56#include "gettytab.h" 57#include "pathnames.h" 58 59extern struct termios tmode, omode; 60 61static void compatflags(long); 62 63/* 64 * Get a table entry. 65 */ 66void 67gettable(const char *name, char *buf) 68{ 69 struct gettystrs *sp; 70 struct gettynums *np; 71 struct gettyflags *fp; 72 long n; 73 const char *dba[2]; 74 dba[0] = _PATH_GETTYTAB; 75 dba[1] = NULL; 76 77 if (cgetent(&buf, dba, name) != 0) 78 return; 79 80 for (sp = gettystrs; sp->field; sp++) 81 (void)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 111void 112gendefaults(void) 113{ 114 struct gettystrs *sp; 115 struct gettynums *np; 116 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 131void 132setdefaults(void) 133{ 134 struct gettystrs *sp; 135 struct gettynums *np; 136 struct gettyflags *fp; 137 138 for (sp = gettystrs; sp->field; sp++) 139 if (!sp->value) 140 sp->value = sp->defalt ? estrdup(sp->defalt) : NULL; 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 149static char ** 150charnames[] = { 151 &ER, &KL, &IN, &QU, &XN, &XF, &ET, &BK, 152 &SU, &DS, &RP, &FL, &WE, &LN, &ST, &B2, 0 153}; 154 155static cc_t * 156charvars[] = { 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], &tmode.c_cc[VSTATUS], 162 &tmode.c_cc[VEOL2], 0 163}; 164 165void 166setchars(void) 167{ 168 int i; 169 char *p; 170 171 for (i = 0; charnames[i]; i++) { 172 p = *charnames[i]; 173 if (p && *p) 174 *charvars[i] = *p; 175 else 176 *charvars[i] = _POSIX_VDISABLE; 177 } 178} 179 180/* Macros to clear/set/test flags. */ 181#define SET(t, f) (t) |= (f) 182#define CLR(t, f) (t) &= ~(f) 183#define ISSET(t, f) ((t) & (f)) 184 185void 186setflags(int n) 187{ 188 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 355out: 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 */ 366void 367compatflags(long flags) 368{ 369 tcflag_t iflag, oflag, cflag, lflag; 370 371 iflag = BRKINT|ICRNL|IMAXBEL|IXON|IXANY; 372 oflag = OPOST|ONLCR|OXTABS; 373 cflag = CREAD; 374 lflag = ICANON|ISIG|IEXTEN; 375 376 if (ISSET(flags, TANDEM)) 377 SET(iflag, IXOFF); 378 else 379 CLR(iflag, IXOFF); 380 if (ISSET(flags, ECHO)) 381 SET(lflag, ECHO); 382 else 383 CLR(lflag, ECHO); 384 if (ISSET(flags, CRMOD)) { 385 SET(iflag, ICRNL); 386 SET(oflag, ONLCR); 387 } else { 388 CLR(iflag, ICRNL); 389 CLR(oflag, ONLCR); 390 } 391 if (ISSET(flags, XTABS)) 392 SET(oflag, OXTABS); 393 else 394 CLR(oflag, OXTABS); 395 396 397 if (ISSET(flags, RAW)) { 398 iflag &= IXOFF; 399 CLR(lflag, ISIG|ICANON|IEXTEN); 400 CLR(cflag, PARENB); 401 } else { 402 SET(iflag, BRKINT|IXON|IMAXBEL); 403 SET(lflag, ISIG|IEXTEN); 404 if (ISSET(flags, CBREAK)) 405 CLR(lflag, ICANON); 406 else 407 SET(lflag, ICANON); 408 switch (ISSET(flags, ANYP)) { 409 case 0: 410 CLR(cflag, PARENB); 411 break; 412 case ANYP: 413 SET(cflag, PARENB); 414 CLR(iflag, INPCK); 415 break; 416 case EVENP: 417 SET(cflag, PARENB); 418 SET(iflag, INPCK); 419 CLR(cflag, PARODD); 420 break; 421 case ODDP: 422 SET(cflag, PARENB); 423 SET(iflag, INPCK); 424 SET(cflag, PARODD); 425 break; 426 } 427 } 428 429 /* Nothing we can do with CRTBS. */ 430 if (ISSET(flags, PRTERA)) 431 SET(lflag, ECHOPRT); 432 else 433 CLR(lflag, ECHOPRT); 434 if (ISSET(flags, CRTERA)) 435 SET(lflag, ECHOE); 436 else 437 CLR(lflag, ECHOE); 438 /* Nothing we can do with TILDE. */ 439 if (ISSET(flags, MDMBUF)) 440 SET(cflag, MDMBUF); 441 else 442 CLR(cflag, MDMBUF); 443 if (ISSET(flags, NOHANG)) 444 CLR(cflag, HUPCL); 445 else 446 SET(cflag, HUPCL); 447 if (ISSET(flags, CRTKIL)) 448 SET(lflag, ECHOKE); 449 else 450 CLR(lflag, ECHOKE); 451 if (ISSET(flags, CTLECH)) 452 SET(lflag, ECHOCTL); 453 else 454 CLR(lflag, ECHOCTL); 455 if (!ISSET(flags, DECCTQ)) 456 SET(iflag, IXANY); 457 else 458 CLR(iflag, IXANY); 459 CLR(lflag, TOSTOP|FLUSHO|PENDIN|NOFLSH); 460 SET(lflag, ISSET(flags, TOSTOP|FLUSHO|PENDIN|NOFLSH)); 461 462 if (ISSET(flags, RAW|LITOUT|PASS8)) { 463 CLR(cflag, CSIZE); 464 SET(cflag, CS8); 465 if (!ISSET(flags, RAW|PASS8)) 466 SET(iflag, ISTRIP); 467 else 468 CLR(iflag, ISTRIP); 469 if (!ISSET(flags, RAW|LITOUT)) 470 SET(oflag, OPOST); 471 else 472 CLR(oflag, OPOST); 473 } else { 474 CLR(cflag, CSIZE); 475 SET(cflag, CS7); 476 SET(iflag, ISTRIP); 477 SET(oflag, OPOST); 478 } 479 480 tmode.c_iflag = iflag; 481 tmode.c_oflag = oflag; 482 tmode.c_cflag = cflag; 483 tmode.c_lflag = lflag; 484} 485#endif 486 487#ifdef XXX_DELAY 488struct delayval { 489 unsigned delay; /* delay in ms */ 490 int bits; 491}; 492 493/* 494 * below are random guesses, I can't be bothered checking 495 */ 496 497struct delayval crdelay[] = { 498 { 1, CR1 }, 499 { 2, CR2 }, 500 { 3, CR3 }, 501 { 83, CR1 }, 502 { 166, CR2 }, 503 { 0, CR3 }, 504}; 505 506struct delayval nldelay[] = { 507 { 1, NL1 }, /* special, calculated */ 508 { 2, NL2 }, 509 { 3, NL3 }, 510 { 100, NL2 }, 511 { 0, NL3 }, 512}; 513 514struct delayval bsdelay[] = { 515 { 1, BS1 }, 516 { 0, 0 }, 517}; 518 519struct delayval ffdelay[] = { 520 { 1, FF1 }, 521 { 1750, FF1 }, 522 { 0, FF1 }, 523}; 524 525struct delayval tbdelay[] = { 526 { 1, TAB1 }, 527 { 2, TAB2 }, 528 { 3, XTABS }, /* this is expand tabs */ 529 { 100, TAB1 }, 530 { 0, TAB2 }, 531}; 532 533int 534delaybits(void) 535{ 536 int f; 537 538 f = adelay(CD, crdelay); 539 f |= adelay(ND, nldelay); 540 f |= adelay(FD, ffdelay); 541 f |= adelay(TD, tbdelay); 542 f |= adelay(BD, bsdelay); 543 return (f); 544} 545 546int 547adelay(int ms, struct delayval *dp) 548{ 549 if (ms == 0) 550 return (0); 551 while (dp->delay && ms > dp->delay) 552 dp++; 553 return (dp->bits); 554} 555#endif 556 557char editedhost[MAXHOSTNAMELEN]; 558 559void 560edithost(const char *pat) 561{ 562 char *host = HN; 563 char *res = editedhost; 564 565 if (!pat) 566 pat = ""; 567 while (*pat) { 568 switch (*pat) { 569 570 case '#': 571 if (*host) 572 host++; 573 break; 574 575 case '@': 576 if (*host) 577 *res++ = *host++; 578 break; 579 580 default: 581 *res++ = *pat; 582 break; 583 584 } 585 if (res == &editedhost[sizeof editedhost - 1]) { 586 *res = '\0'; 587 return; 588 } 589 pat++; 590 } 591 if (*host) 592 (void)strncpy(res, host, 593 sizeof editedhost - (res - editedhost) - 1); 594 else 595 *res = '\0'; 596 editedhost[sizeof editedhost - 1] = '\0'; 597} 598 599void 600makeenv(char *env[]) 601{ 602 static char termbuf[128] = "TERM="; 603 char *p, *q; 604 char **ep; 605 606 ep = env; 607 if (TT && *TT) { 608 (void)strlcat(termbuf, TT, sizeof(termbuf)); 609 *ep++ = termbuf; 610 } 611 if ((p = EV) != NULL) { 612 q = p; 613 while ((q = strchr(q, ',')) != NULL) { 614 *q++ = '\0'; 615 *ep++ = p; 616 p = q; 617 } 618 if (*p) 619 *ep++ = p; 620 } 621 *ep = (char *)0; 622} 623 624/* 625 * This speed select mechanism is written for the Develcon DATASWITCH. 626 * The Develcon sends a string of the form "B{speed}\n" at a predefined 627 * baud rate. This string indicates the user's actual speed. 628 * The routine below returns the terminal type mapped from derived speed. 629 */ 630struct portselect { 631 const char *ps_baud; 632 const char *ps_type; 633} portspeeds[] = { 634 { "B110", "std.110" }, 635 { "B134", "std.134" }, 636 { "B150", "std.150" }, 637 { "B300", "std.300" }, 638 { "B600", "std.600" }, 639 { "B1200", "std.1200" }, 640 { "B2400", "std.2400" }, 641 { "B4800", "std.4800" }, 642 { "B9600", "std.9600" }, 643 { "B19200", "std.19200" }, 644 { NULL, NULL } 645}; 646 647const char * 648portselector(void) 649{ 650 char c, baud[20]; 651 const char *type = "default"; 652 struct portselect *ps; 653 size_t len; 654 655 (void)alarm(5*60); 656 for (len = 0; len < sizeof (baud) - 1; len++) { 657 if (read(STDIN_FILENO, &c, 1) <= 0) 658 break; 659 c &= 0177; 660 if (c == '\n' || c == '\r') 661 break; 662 if (c == 'B') 663 len = 0; /* in case of leading garbage */ 664 baud[len] = c; 665 } 666 baud[len] = '\0'; 667 for (ps = portspeeds; ps->ps_baud; ps++) 668 if (strcmp(ps->ps_baud, baud) == 0) { 669 type = ps->ps_type; 670 break; 671 } 672 (void)sleep(2); /* wait for connection to complete */ 673 return (type); 674} 675 676/* 677 * This auto-baud speed select mechanism is written for the Micom 600 678 * portselector. Selection is done by looking at how the character '\r' 679 * is garbled at the different speeds. 680 */ 681#include <sys/time.h> 682 683const char * 684autobaud(void) 685{ 686 struct pollfd set[1]; 687 struct timespec timeout; 688 char c; 689 const char *type = "9600-baud"; 690 691 (void)tcflush(0, TCIOFLUSH); 692 set[0].fd = STDIN_FILENO; 693 set[0].events = POLLIN; 694 if (poll(set, 1, 5000) <= 0) 695 return (type); 696 if (read(STDIN_FILENO, &c, 1) != 1) 697 return (type); 698 timeout.tv_sec = 0; 699 timeout.tv_nsec = 20000; 700 (void)nanosleep(&timeout, NULL); 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