1ea022d16SRodney W. Grimes /* 2cae66988SJoerg Wunsch * Copyright (c) 1983, 1993 3cae66988SJoerg Wunsch * The Regents of the University of California. All rights reserved. 4ea022d16SRodney W. Grimes * 5ea022d16SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6ea022d16SRodney W. Grimes * modification, are permitted provided that the following conditions 7ea022d16SRodney W. Grimes * are met: 8ea022d16SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9ea022d16SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10ea022d16SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11ea022d16SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12ea022d16SRodney W. Grimes * documentation and/or other materials provided with the distribution. 13*5efaea4cSChristian Brueffer * 3. Neither the name of the University nor the names of its contributors 14ea022d16SRodney W. Grimes * may be used to endorse or promote products derived from this software 15ea022d16SRodney W. Grimes * without specific prior written permission. 16ea022d16SRodney W. Grimes * 17ea022d16SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18ea022d16SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19ea022d16SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20ea022d16SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21ea022d16SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22ea022d16SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23ea022d16SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24ea022d16SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25ea022d16SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26ea022d16SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27ea022d16SRodney W. Grimes * SUCH DAMAGE. 28ea022d16SRodney W. Grimes */ 29ea022d16SRodney W. Grimes 30ea022d16SRodney W. Grimes #ifndef lint 31d748864dSPhilippe Charnier #if 0 32d748864dSPhilippe Charnier static char sccsid[] = "@(#)from: subr.c 8.1 (Berkeley) 6/4/93"; 33d748864dSPhilippe Charnier #endif 34d748864dSPhilippe Charnier static const char rcsid[] = 357f3dea24SPeter Wemm "$FreeBSD$"; 36ea022d16SRodney W. Grimes #endif /* not lint */ 37ea022d16SRodney W. Grimes 38ea022d16SRodney W. Grimes /* 39ea022d16SRodney W. Grimes * Melbourne getty. 40ea022d16SRodney W. Grimes */ 41cae66988SJoerg Wunsch #ifdef DEBUG 42cae66988SJoerg Wunsch #include <stdio.h> 43cae66988SJoerg Wunsch #endif 44d748864dSPhilippe Charnier #include <stdlib.h> 45d748864dSPhilippe Charnier #include <string.h> 46d748864dSPhilippe Charnier #include <termios.h> 47d748864dSPhilippe Charnier #include <unistd.h> 48d748864dSPhilippe Charnier #include <sys/ioctl.h> 49d748864dSPhilippe Charnier #include <sys/param.h> 50d748864dSPhilippe Charnier #include <sys/time.h> 51d748864dSPhilippe Charnier #include <syslog.h> 52ea022d16SRodney W. Grimes 53cae66988SJoerg Wunsch #include "gettytab.h" 54cae66988SJoerg Wunsch #include "pathnames.h" 55cae66988SJoerg Wunsch #include "extern.h" 56cae66988SJoerg Wunsch 57cae66988SJoerg Wunsch 58ea022d16SRodney W. Grimes 59ea022d16SRodney W. Grimes /* 60ea022d16SRodney W. Grimes * Get a table entry. 61ea022d16SRodney W. Grimes */ 62cae66988SJoerg Wunsch void 6395289b27SWarner Losh gettable(const char *name, char *buf) 64ea022d16SRodney W. Grimes { 6595289b27SWarner Losh struct gettystrs *sp; 6695289b27SWarner Losh struct gettynums *np; 6795289b27SWarner Losh struct gettyflags *fp; 68cae66988SJoerg Wunsch long n; 6904a59e67SDavid Nugent int l; 7004a59e67SDavid Nugent char *p; 7104a59e67SDavid Nugent char *msg = NULL; 72cae66988SJoerg Wunsch const char *dba[2]; 7304a59e67SDavid Nugent 7404a59e67SDavid Nugent static int firsttime = 1; 7504a59e67SDavid Nugent 76cae66988SJoerg Wunsch dba[0] = _PATH_GETTYTAB; 77cae66988SJoerg Wunsch dba[1] = 0; 78ea022d16SRodney W. Grimes 7904a59e67SDavid Nugent if (firsttime) { 8004a59e67SDavid Nugent /* 8104a59e67SDavid Nugent * we need to strdup() anything in the strings array 8204a59e67SDavid Nugent * initially in order to simplify things later 8304a59e67SDavid Nugent */ 84ea022d16SRodney W. Grimes for (sp = gettystrs; sp->field; sp++) 8504a59e67SDavid Nugent if (sp->value != NULL) { 8604a59e67SDavid Nugent /* handle these ones more carefully */ 8704a59e67SDavid Nugent if (sp >= &gettystrs[4] && sp <= &gettystrs[6]) 8804a59e67SDavid Nugent l = 2; 8904a59e67SDavid Nugent else 9004a59e67SDavid Nugent l = strlen(sp->value) + 1; 9104a59e67SDavid Nugent if ((p = malloc(l)) != NULL) { 9204a59e67SDavid Nugent strncpy(p, sp->value, l); 9304a59e67SDavid Nugent p[l-1] = '\0'; 9404a59e67SDavid Nugent } 9504a59e67SDavid Nugent /* 9604a59e67SDavid Nugent * replace, even if NULL, else we'll 9704a59e67SDavid Nugent * have problems with free()ing static mem 9804a59e67SDavid Nugent */ 9904a59e67SDavid Nugent sp->value = p; 10004a59e67SDavid Nugent } 10104a59e67SDavid Nugent firsttime = 0; 10204a59e67SDavid Nugent } 10304a59e67SDavid Nugent 10404a59e67SDavid Nugent switch (cgetent(&buf, (char **)dba, (char *)name)) { 10504a59e67SDavid Nugent case 1: 10604a59e67SDavid Nugent msg = "%s: couldn't resolve 'tc=' in gettytab '%s'"; 10704a59e67SDavid Nugent case 0: 10804a59e67SDavid Nugent break; 10904a59e67SDavid Nugent case -1: 11004a59e67SDavid Nugent msg = "%s: unknown gettytab entry '%s'"; 11104a59e67SDavid Nugent break; 11204a59e67SDavid Nugent case -2: 11304a59e67SDavid Nugent msg = "%s: retrieving gettytab entry '%s': %m"; 11404a59e67SDavid Nugent break; 11504a59e67SDavid Nugent case -3: 11604a59e67SDavid Nugent msg = "%s: recursive 'tc=' reference gettytab entry '%s'"; 11704a59e67SDavid Nugent break; 11804a59e67SDavid Nugent default: 11904a59e67SDavid Nugent msg = "%s: unexpected cgetent() error for entry '%s'"; 12004a59e67SDavid Nugent break; 12104a59e67SDavid Nugent } 12204a59e67SDavid Nugent 12304a59e67SDavid Nugent if (msg != NULL) { 12404a59e67SDavid Nugent syslog(LOG_ERR, msg, "getty", name); 12504a59e67SDavid Nugent return; 12604a59e67SDavid Nugent } 12704a59e67SDavid Nugent 12804a59e67SDavid Nugent for (sp = gettystrs; sp->field; sp++) { 1291cc15828SDavid Nugent if ((l = cgetstr(buf, (char*)sp->field, &p)) >= 0) { 13004a59e67SDavid Nugent if (sp->value) { 13104a59e67SDavid Nugent /* prefer existing value */ 13204a59e67SDavid Nugent if (strcmp(p, sp->value) != 0) 13304a59e67SDavid Nugent free(sp->value); 13404a59e67SDavid Nugent else { 13504a59e67SDavid Nugent free(p); 13604a59e67SDavid Nugent p = sp->value; 13704a59e67SDavid Nugent } 13804a59e67SDavid Nugent } 13904a59e67SDavid Nugent sp->value = p; 14004a59e67SDavid Nugent } else if (l == -1) { 14104a59e67SDavid Nugent free(sp->value); 14204a59e67SDavid Nugent sp->value = NULL; 14304a59e67SDavid Nugent } 14404a59e67SDavid Nugent } 14504a59e67SDavid Nugent 146ea022d16SRodney W. Grimes for (np = gettynums; np->field; np++) { 14704a59e67SDavid Nugent if (cgetnum(buf, (char*)np->field, &n) == -1) 148ea022d16SRodney W. Grimes np->set = 0; 149ea022d16SRodney W. Grimes else { 150ea022d16SRodney W. Grimes np->set = 1; 151ea022d16SRodney W. Grimes np->value = n; 152ea022d16SRodney W. Grimes } 153ea022d16SRodney W. Grimes } 15404a59e67SDavid Nugent 155ea022d16SRodney W. Grimes for (fp = gettyflags; fp->field; fp++) { 15604a59e67SDavid Nugent if (cgetcap(buf, (char *)fp->field, ':') == NULL) 157ea022d16SRodney W. Grimes fp->set = 0; 158ea022d16SRodney W. Grimes else { 159ea022d16SRodney W. Grimes fp->set = 1; 160cae66988SJoerg Wunsch fp->value = 1 ^ fp->invrt; 161ea022d16SRodney W. Grimes } 162ea022d16SRodney W. Grimes } 16304a59e67SDavid Nugent 164cae66988SJoerg Wunsch #ifdef DEBUG 165cae66988SJoerg Wunsch printf("name=\"%s\", buf=\"%s\"\r\n", name, buf); 166cae66988SJoerg Wunsch for (sp = gettystrs; sp->field; sp++) 1671cc15828SDavid Nugent printf("cgetstr: %s=%s\r\n", sp->field, sp->value); 168cae66988SJoerg Wunsch for (np = gettynums; np->field; np++) 169cae66988SJoerg Wunsch printf("cgetnum: %s=%d\r\n", np->field, np->value); 170cae66988SJoerg Wunsch for (fp = gettyflags; fp->field; fp++) 171cae66988SJoerg Wunsch printf("cgetflags: %s='%c' set='%c'\r\n", fp->field, 172cae66988SJoerg Wunsch fp->value + '0', fp->set + '0'); 173cae66988SJoerg Wunsch #endif /* DEBUG */ 174ea022d16SRodney W. Grimes } 175ea022d16SRodney W. Grimes 176cae66988SJoerg Wunsch void 17795289b27SWarner Losh gendefaults(void) 178ea022d16SRodney W. Grimes { 17995289b27SWarner Losh struct gettystrs *sp; 18095289b27SWarner Losh struct gettynums *np; 18195289b27SWarner Losh struct gettyflags *fp; 182ea022d16SRodney W. Grimes 183ea022d16SRodney W. Grimes for (sp = gettystrs; sp->field; sp++) 184ea022d16SRodney W. Grimes if (sp->value) 18504a59e67SDavid Nugent sp->defalt = strdup(sp->value); 186ea022d16SRodney W. Grimes for (np = gettynums; np->field; np++) 187ea022d16SRodney W. Grimes if (np->set) 188ea022d16SRodney W. Grimes np->defalt = np->value; 189ea022d16SRodney W. Grimes for (fp = gettyflags; fp->field; fp++) 190ea022d16SRodney W. Grimes if (fp->set) 191ea022d16SRodney W. Grimes fp->defalt = fp->value; 192ea022d16SRodney W. Grimes else 193ea022d16SRodney W. Grimes fp->defalt = fp->invrt; 194ea022d16SRodney W. Grimes } 195ea022d16SRodney W. Grimes 196cae66988SJoerg Wunsch void 19795289b27SWarner Losh setdefaults(void) 198ea022d16SRodney W. Grimes { 19995289b27SWarner Losh struct gettystrs *sp; 20095289b27SWarner Losh struct gettynums *np; 20195289b27SWarner Losh struct gettyflags *fp; 202ea022d16SRodney W. Grimes 203ea022d16SRodney W. Grimes for (sp = gettystrs; sp->field; sp++) 204ea022d16SRodney W. Grimes if (!sp->value) 20504a59e67SDavid Nugent sp->value = !sp->defalt ? sp->defalt 20604a59e67SDavid Nugent : strdup(sp->defalt); 207ea022d16SRodney W. Grimes for (np = gettynums; np->field; np++) 208ea022d16SRodney W. Grimes if (!np->set) 209ea022d16SRodney W. Grimes np->value = np->defalt; 210ea022d16SRodney W. Grimes for (fp = gettyflags; fp->field; fp++) 211ea022d16SRodney W. Grimes if (!fp->set) 212ea022d16SRodney W. Grimes fp->value = fp->defalt; 213ea022d16SRodney W. Grimes } 214ea022d16SRodney W. Grimes 215ea022d16SRodney W. Grimes static char ** 216ea022d16SRodney W. Grimes charnames[] = { 217ea022d16SRodney W. Grimes &ER, &KL, &IN, &QU, &XN, &XF, &ET, &BK, 218ea022d16SRodney W. Grimes &SU, &DS, &RP, &FL, &WE, &LN, 0 219ea022d16SRodney W. Grimes }; 220ea022d16SRodney W. Grimes 221ea022d16SRodney W. Grimes static char * 222ea022d16SRodney W. Grimes charvars[] = { 223cae66988SJoerg Wunsch &tmode.c_cc[VERASE], &tmode.c_cc[VKILL], &tmode.c_cc[VINTR], 224cae66988SJoerg Wunsch &tmode.c_cc[VQUIT], &tmode.c_cc[VSTART], &tmode.c_cc[VSTOP], 225cae66988SJoerg Wunsch &tmode.c_cc[VEOF], &tmode.c_cc[VEOL], &tmode.c_cc[VSUSP], 226cae66988SJoerg Wunsch &tmode.c_cc[VDSUSP], &tmode.c_cc[VREPRINT], &tmode.c_cc[VDISCARD], 227cae66988SJoerg Wunsch &tmode.c_cc[VWERASE], &tmode.c_cc[VLNEXT], 0 228ea022d16SRodney W. Grimes }; 229ea022d16SRodney W. Grimes 230cae66988SJoerg Wunsch void 23195289b27SWarner Losh setchars(void) 232ea022d16SRodney W. Grimes { 23395289b27SWarner Losh int i; 23495289b27SWarner Losh const char *p; 235ea022d16SRodney W. Grimes 236ea022d16SRodney W. Grimes for (i = 0; charnames[i]; i++) { 237ea022d16SRodney W. Grimes p = *charnames[i]; 238ea022d16SRodney W. Grimes if (p && *p) 239ea022d16SRodney W. Grimes *charvars[i] = *p; 240ea022d16SRodney W. Grimes else 241cae66988SJoerg Wunsch *charvars[i] = _POSIX_VDISABLE; 242ea022d16SRodney W. Grimes } 243ea022d16SRodney W. Grimes } 244ea022d16SRodney W. Grimes 245cae66988SJoerg Wunsch /* Macros to clear/set/test flags. */ 246cae66988SJoerg Wunsch #define SET(t, f) (t) |= (f) 247cae66988SJoerg Wunsch #define CLR(t, f) (t) &= ~(f) 248cae66988SJoerg Wunsch #define ISSET(t, f) ((t) & (f)) 249cae66988SJoerg Wunsch 250cae66988SJoerg Wunsch void 25195289b27SWarner Losh set_flags(int n) 252ea022d16SRodney W. Grimes { 25395289b27SWarner Losh tcflag_t iflag, oflag, cflag, lflag; 254cae66988SJoerg Wunsch 255ea022d16SRodney W. Grimes 256ea022d16SRodney W. Grimes switch (n) { 257ea022d16SRodney W. Grimes case 0: 258cae66988SJoerg Wunsch if (C0set && I0set && L0set && O0set) { 259cae66988SJoerg Wunsch tmode.c_cflag = C0; 260cae66988SJoerg Wunsch tmode.c_iflag = I0; 261cae66988SJoerg Wunsch tmode.c_lflag = L0; 262cae66988SJoerg Wunsch tmode.c_oflag = O0; 263cae66988SJoerg Wunsch return; 264cae66988SJoerg Wunsch } 265ea022d16SRodney W. Grimes break; 266ea022d16SRodney W. Grimes case 1: 267cae66988SJoerg Wunsch if (C1set && I1set && L1set && O1set) { 268cae66988SJoerg Wunsch tmode.c_cflag = C1; 269cae66988SJoerg Wunsch tmode.c_iflag = I1; 270cae66988SJoerg Wunsch tmode.c_lflag = L1; 271cae66988SJoerg Wunsch tmode.c_oflag = O1; 272cae66988SJoerg Wunsch return; 273cae66988SJoerg Wunsch } 274ea022d16SRodney W. Grimes break; 275ea022d16SRodney W. Grimes default: 276cae66988SJoerg Wunsch if (C2set && I2set && L2set && O2set) { 277cae66988SJoerg Wunsch tmode.c_cflag = C2; 278cae66988SJoerg Wunsch tmode.c_iflag = I2; 279cae66988SJoerg Wunsch tmode.c_lflag = L2; 280cae66988SJoerg Wunsch tmode.c_oflag = O2; 281cae66988SJoerg Wunsch return; 282cae66988SJoerg Wunsch } 283ea022d16SRodney W. Grimes break; 284ea022d16SRodney W. Grimes } 285ea022d16SRodney W. Grimes 286cae66988SJoerg Wunsch iflag = omode.c_iflag; 287cae66988SJoerg Wunsch oflag = omode.c_oflag; 288cae66988SJoerg Wunsch cflag = omode.c_cflag; 289cae66988SJoerg Wunsch lflag = omode.c_lflag; 290ea022d16SRodney W. Grimes 291cae66988SJoerg Wunsch if (NP) { 292cae66988SJoerg Wunsch CLR(cflag, CSIZE|PARENB); 293cae66988SJoerg Wunsch SET(cflag, CS8); 294cae66988SJoerg Wunsch CLR(iflag, ISTRIP|INPCK|IGNPAR); 295cae66988SJoerg Wunsch } else if (AP || EP || OP) { 296cae66988SJoerg Wunsch CLR(cflag, CSIZE); 297cae66988SJoerg Wunsch SET(cflag, CS7|PARENB); 298cae66988SJoerg Wunsch SET(iflag, ISTRIP); 299cae66988SJoerg Wunsch if (OP && !EP) { 300cae66988SJoerg Wunsch SET(iflag, INPCK|IGNPAR); 301cae66988SJoerg Wunsch SET(cflag, PARODD); 302ea022d16SRodney W. Grimes if (AP) 303cae66988SJoerg Wunsch CLR(iflag, INPCK); 304cae66988SJoerg Wunsch } else if (EP && !OP) { 305cae66988SJoerg Wunsch SET(iflag, INPCK|IGNPAR); 306cae66988SJoerg Wunsch CLR(cflag, PARODD); 307cae66988SJoerg Wunsch if (AP) 308cae66988SJoerg Wunsch CLR(iflag, INPCK); 309cae66988SJoerg Wunsch } else if (AP || (EP && OP)) { 310cae66988SJoerg Wunsch CLR(iflag, INPCK|IGNPAR); 311cae66988SJoerg Wunsch CLR(cflag, PARODD); 312cae66988SJoerg Wunsch } 313cae66988SJoerg Wunsch } /* else, leave as is */ 314ea022d16SRodney W. Grimes 315cae66988SJoerg Wunsch #if 0 316ea022d16SRodney W. Grimes if (UC) 317ea022d16SRodney W. Grimes f |= LCASE; 318cae66988SJoerg Wunsch #endif 319ea022d16SRodney W. Grimes 320cae66988SJoerg Wunsch if (HC) 321cae66988SJoerg Wunsch SET(cflag, HUPCL); 322ea022d16SRodney W. Grimes else 323cae66988SJoerg Wunsch CLR(cflag, HUPCL); 324cae66988SJoerg Wunsch 325cae66988SJoerg Wunsch if (MB) 326cae66988SJoerg Wunsch SET(cflag, MDMBUF); 327cae66988SJoerg Wunsch else 328cae66988SJoerg Wunsch CLR(cflag, MDMBUF); 329cae66988SJoerg Wunsch 330fe552114SDavid Nugent if (HW) 331fe552114SDavid Nugent SET(cflag, CRTSCTS); 332fe552114SDavid Nugent else 333fe552114SDavid Nugent CLR(cflag, CRTSCTS); 334fe552114SDavid Nugent 335cae66988SJoerg Wunsch if (NL) { 336cae66988SJoerg Wunsch SET(iflag, ICRNL); 337cae66988SJoerg Wunsch SET(oflag, ONLCR|OPOST); 338cae66988SJoerg Wunsch } else { 339cae66988SJoerg Wunsch CLR(iflag, ICRNL); 340cae66988SJoerg Wunsch CLR(oflag, ONLCR); 341ea022d16SRodney W. Grimes } 342ea022d16SRodney W. Grimes 343ea022d16SRodney W. Grimes if (!HT) 344cae66988SJoerg Wunsch SET(oflag, OXTABS|OPOST); 345cae66988SJoerg Wunsch else 346cae66988SJoerg Wunsch CLR(oflag, OXTABS); 347ea022d16SRodney W. Grimes 348cae66988SJoerg Wunsch #ifdef XXX_DELAY 349cae66988SJoerg Wunsch SET(f, delaybits()); 350cae66988SJoerg Wunsch #endif 351ea022d16SRodney W. Grimes 352cae66988SJoerg Wunsch if (n == 1) { /* read mode flags */ 353cae66988SJoerg Wunsch if (RW) { 354cae66988SJoerg Wunsch iflag = 0; 355cae66988SJoerg Wunsch CLR(oflag, OPOST); 356cae66988SJoerg Wunsch CLR(cflag, CSIZE|PARENB); 357cae66988SJoerg Wunsch SET(cflag, CS8); 358cae66988SJoerg Wunsch lflag = 0; 359cae66988SJoerg Wunsch } else { 360cae66988SJoerg Wunsch CLR(lflag, ICANON); 361cae66988SJoerg Wunsch } 362cae66988SJoerg Wunsch goto out; 363ea022d16SRodney W. Grimes } 364ea022d16SRodney W. Grimes 365cae66988SJoerg Wunsch if (n == 0) 366cae66988SJoerg Wunsch goto out; 367cae66988SJoerg Wunsch 368cae66988SJoerg Wunsch #if 0 369cae66988SJoerg Wunsch if (CB) 370cae66988SJoerg Wunsch SET(f, CRTBS); 371cae66988SJoerg Wunsch #endif 372cae66988SJoerg Wunsch 373cae66988SJoerg Wunsch if (CE) 374cae66988SJoerg Wunsch SET(lflag, ECHOE); 375cae66988SJoerg Wunsch else 376cae66988SJoerg Wunsch CLR(lflag, ECHOE); 377cae66988SJoerg Wunsch 378cae66988SJoerg Wunsch if (CK) 379cae66988SJoerg Wunsch SET(lflag, ECHOKE); 380cae66988SJoerg Wunsch else 381cae66988SJoerg Wunsch CLR(lflag, ECHOKE); 382cae66988SJoerg Wunsch 383cae66988SJoerg Wunsch if (PE) 384cae66988SJoerg Wunsch SET(lflag, ECHOPRT); 385cae66988SJoerg Wunsch else 386cae66988SJoerg Wunsch CLR(lflag, ECHOPRT); 387cae66988SJoerg Wunsch 388cae66988SJoerg Wunsch if (EC) 389cae66988SJoerg Wunsch SET(lflag, ECHO); 390cae66988SJoerg Wunsch else 391cae66988SJoerg Wunsch CLR(lflag, ECHO); 392cae66988SJoerg Wunsch 393cae66988SJoerg Wunsch if (XC) 394cae66988SJoerg Wunsch SET(lflag, ECHOCTL); 395cae66988SJoerg Wunsch else 396cae66988SJoerg Wunsch CLR(lflag, ECHOCTL); 397cae66988SJoerg Wunsch 398cae66988SJoerg Wunsch if (DX) 399cae66988SJoerg Wunsch SET(lflag, IXANY); 400cae66988SJoerg Wunsch else 401cae66988SJoerg Wunsch CLR(lflag, IXANY); 402cae66988SJoerg Wunsch 403cae66988SJoerg Wunsch out: 404cae66988SJoerg Wunsch tmode.c_iflag = iflag; 405cae66988SJoerg Wunsch tmode.c_oflag = oflag; 406cae66988SJoerg Wunsch tmode.c_cflag = cflag; 407cae66988SJoerg Wunsch tmode.c_lflag = lflag; 408cae66988SJoerg Wunsch } 409cae66988SJoerg Wunsch 410cae66988SJoerg Wunsch 411cae66988SJoerg Wunsch #ifdef XXX_DELAY 412ea022d16SRodney W. Grimes struct delayval { 413ea022d16SRodney W. Grimes unsigned delay; /* delay in ms */ 414ea022d16SRodney W. Grimes int bits; 415ea022d16SRodney W. Grimes }; 416ea022d16SRodney W. Grimes 417ea022d16SRodney W. Grimes /* 418ea022d16SRodney W. Grimes * below are random guesses, I can't be bothered checking 419ea022d16SRodney W. Grimes */ 420ea022d16SRodney W. Grimes 421ea022d16SRodney W. Grimes struct delayval crdelay[] = { 422cae66988SJoerg Wunsch { 1, CR1 }, 423cae66988SJoerg Wunsch { 2, CR2 }, 424cae66988SJoerg Wunsch { 3, CR3 }, 425cae66988SJoerg Wunsch { 83, CR1 }, 426cae66988SJoerg Wunsch { 166, CR2 }, 427cae66988SJoerg Wunsch { 0, CR3 }, 428ea022d16SRodney W. Grimes }; 429ea022d16SRodney W. Grimes 430ea022d16SRodney W. Grimes struct delayval nldelay[] = { 431cae66988SJoerg Wunsch { 1, NL1 }, /* special, calculated */ 432cae66988SJoerg Wunsch { 2, NL2 }, 433cae66988SJoerg Wunsch { 3, NL3 }, 434cae66988SJoerg Wunsch { 100, NL2 }, 435cae66988SJoerg Wunsch { 0, NL3 }, 436ea022d16SRodney W. Grimes }; 437ea022d16SRodney W. Grimes 438ea022d16SRodney W. Grimes struct delayval bsdelay[] = { 439cae66988SJoerg Wunsch { 1, BS1 }, 440cae66988SJoerg Wunsch { 0, 0 }, 441ea022d16SRodney W. Grimes }; 442ea022d16SRodney W. Grimes 443ea022d16SRodney W. Grimes struct delayval ffdelay[] = { 444cae66988SJoerg Wunsch { 1, FF1 }, 445cae66988SJoerg Wunsch { 1750, FF1 }, 446cae66988SJoerg Wunsch { 0, FF1 }, 447ea022d16SRodney W. Grimes }; 448ea022d16SRodney W. Grimes 449ea022d16SRodney W. Grimes struct delayval tbdelay[] = { 450cae66988SJoerg Wunsch { 1, TAB1 }, 451cae66988SJoerg Wunsch { 2, TAB2 }, 452cae66988SJoerg Wunsch { 3, XTABS }, /* this is expand tabs */ 453cae66988SJoerg Wunsch { 100, TAB1 }, 454cae66988SJoerg Wunsch { 0, TAB2 }, 455ea022d16SRodney W. Grimes }; 456ea022d16SRodney W. Grimes 457cae66988SJoerg Wunsch int 45895289b27SWarner Losh delaybits(void) 459ea022d16SRodney W. Grimes { 46095289b27SWarner Losh int f; 461ea022d16SRodney W. Grimes 462ea022d16SRodney W. Grimes f = adelay(CD, crdelay); 463ea022d16SRodney W. Grimes f |= adelay(ND, nldelay); 464ea022d16SRodney W. Grimes f |= adelay(FD, ffdelay); 465ea022d16SRodney W. Grimes f |= adelay(TD, tbdelay); 466ea022d16SRodney W. Grimes f |= adelay(BD, bsdelay); 467ea022d16SRodney W. Grimes return (f); 468ea022d16SRodney W. Grimes } 469ea022d16SRodney W. Grimes 470cae66988SJoerg Wunsch int 47195289b27SWarner Losh adelay(int ms, struct delayval *dp) 472ea022d16SRodney W. Grimes { 473ea022d16SRodney W. Grimes if (ms == 0) 474ea022d16SRodney W. Grimes return (0); 475ea022d16SRodney W. Grimes while (dp->delay && ms > dp->delay) 476ea022d16SRodney W. Grimes dp++; 477ea022d16SRodney W. Grimes return (dp->bits); 478ea022d16SRodney W. Grimes } 479cae66988SJoerg Wunsch #endif 480ea022d16SRodney W. Grimes 481c568fce9SAndrey A. Chernov char editedhost[MAXHOSTNAMELEN]; 482ea022d16SRodney W. Grimes 483cae66988SJoerg Wunsch void 48495289b27SWarner Losh edithost(const char *pat) 485ea022d16SRodney W. Grimes { 48695289b27SWarner Losh const char *host = HN; 48795289b27SWarner Losh char *res = editedhost; 488ea022d16SRodney W. Grimes 489ea022d16SRodney W. Grimes if (!pat) 490ea022d16SRodney W. Grimes pat = ""; 491ea022d16SRodney W. Grimes while (*pat) { 492ea022d16SRodney W. Grimes switch (*pat) { 493ea022d16SRodney W. Grimes 494ea022d16SRodney W. Grimes case '#': 495ea022d16SRodney W. Grimes if (*host) 496ea022d16SRodney W. Grimes host++; 497ea022d16SRodney W. Grimes break; 498ea022d16SRodney W. Grimes 499ea022d16SRodney W. Grimes case '@': 500ea022d16SRodney W. Grimes if (*host) 501ea022d16SRodney W. Grimes *res++ = *host++; 502ea022d16SRodney W. Grimes break; 503ea022d16SRodney W. Grimes 504ea022d16SRodney W. Grimes default: 505ea022d16SRodney W. Grimes *res++ = *pat; 506ea022d16SRodney W. Grimes break; 507ea022d16SRodney W. Grimes 508ea022d16SRodney W. Grimes } 509ea022d16SRodney W. Grimes if (res == &editedhost[sizeof editedhost - 1]) { 510ea022d16SRodney W. Grimes *res = '\0'; 511ea022d16SRodney W. Grimes return; 512ea022d16SRodney W. Grimes } 513ea022d16SRodney W. Grimes pat++; 514ea022d16SRodney W. Grimes } 515ea022d16SRodney W. Grimes if (*host) 516ea022d16SRodney W. Grimes strncpy(res, host, sizeof editedhost - (res - editedhost) - 1); 517ea022d16SRodney W. Grimes else 518ea022d16SRodney W. Grimes *res = '\0'; 519ea022d16SRodney W. Grimes editedhost[sizeof editedhost - 1] = '\0'; 520ea022d16SRodney W. Grimes } 521ea022d16SRodney W. Grimes 522cae66988SJoerg Wunsch static struct speedtab { 523ea022d16SRodney W. Grimes int speed; 524ea022d16SRodney W. Grimes int uxname; 525ea022d16SRodney W. Grimes } speedtab[] = { 526cae66988SJoerg Wunsch { 50, B50 }, 527cae66988SJoerg Wunsch { 75, B75 }, 528cae66988SJoerg Wunsch { 110, B110 }, 529cae66988SJoerg Wunsch { 134, B134 }, 530cae66988SJoerg Wunsch { 150, B150 }, 531cae66988SJoerg Wunsch { 200, B200 }, 532cae66988SJoerg Wunsch { 300, B300 }, 533cae66988SJoerg Wunsch { 600, B600 }, 534cae66988SJoerg Wunsch { 1200, B1200 }, 535cae66988SJoerg Wunsch { 1800, B1800 }, 536cae66988SJoerg Wunsch { 2400, B2400 }, 537cae66988SJoerg Wunsch { 4800, B4800 }, 538cae66988SJoerg Wunsch { 9600, B9600 }, 539cae66988SJoerg Wunsch { 19200, EXTA }, 540cae66988SJoerg Wunsch { 19, EXTA }, /* for people who say 19.2K */ 541cae66988SJoerg Wunsch { 38400, EXTB }, 542cae66988SJoerg Wunsch { 38, EXTB }, 543cae66988SJoerg Wunsch { 7200, EXTB }, /* alternative */ 544cae66988SJoerg Wunsch { 57600, B57600 }, 545cae66988SJoerg Wunsch { 115200, B115200 }, 546ee98a93fSPoul-Henning Kamp { 230400, B230400 }, 547cae66988SJoerg Wunsch { 0 } 548ea022d16SRodney W. Grimes }; 549ea022d16SRodney W. Grimes 550cae66988SJoerg Wunsch int 55195289b27SWarner Losh speed(int val) 552ea022d16SRodney W. Grimes { 55395289b27SWarner Losh struct speedtab *sp; 554ea022d16SRodney W. Grimes 555ee98a93fSPoul-Henning Kamp if (val <= B230400) 556ea022d16SRodney W. Grimes return (val); 557ea022d16SRodney W. Grimes 558ea022d16SRodney W. Grimes for (sp = speedtab; sp->speed; sp++) 559ea022d16SRodney W. Grimes if (sp->speed == val) 560ea022d16SRodney W. Grimes return (sp->uxname); 561ea022d16SRodney W. Grimes 562ea022d16SRodney W. Grimes return (B300); /* default in impossible cases */ 563ea022d16SRodney W. Grimes } 564ea022d16SRodney W. Grimes 565cae66988SJoerg Wunsch void 56695289b27SWarner Losh makeenv(char *env[]) 567ea022d16SRodney W. Grimes { 568ea022d16SRodney W. Grimes static char termbuf[128] = "TERM="; 56995289b27SWarner Losh char *p, *q; 57095289b27SWarner Losh char **ep; 571ea022d16SRodney W. Grimes 572ea022d16SRodney W. Grimes ep = env; 573ea022d16SRodney W. Grimes if (TT && *TT) { 5746e76e16fSKris Kennaway strlcat(termbuf, TT, sizeof(termbuf)); 575ea022d16SRodney W. Grimes *ep++ = termbuf; 576ea022d16SRodney W. Grimes } 577cae66988SJoerg Wunsch if ((p = EV)) { 578ea022d16SRodney W. Grimes q = p; 579cae66988SJoerg Wunsch while ((q = strchr(q, ','))) { 580ea022d16SRodney W. Grimes *q++ = '\0'; 581ea022d16SRodney W. Grimes *ep++ = p; 582ea022d16SRodney W. Grimes p = q; 583ea022d16SRodney W. Grimes } 584ea022d16SRodney W. Grimes if (*p) 585ea022d16SRodney W. Grimes *ep++ = p; 586ea022d16SRodney W. Grimes } 587ea022d16SRodney W. Grimes *ep = (char *)0; 588ea022d16SRodney W. Grimes } 589ea022d16SRodney W. Grimes 590ea022d16SRodney W. Grimes /* 591ea022d16SRodney W. Grimes * This speed select mechanism is written for the Develcon DATASWITCH. 592ea022d16SRodney W. Grimes * The Develcon sends a string of the form "B{speed}\n" at a predefined 593ea022d16SRodney W. Grimes * baud rate. This string indicates the user's actual speed. 594ea022d16SRodney W. Grimes * The routine below returns the terminal type mapped from derived speed. 595ea022d16SRodney W. Grimes */ 596ea022d16SRodney W. Grimes struct portselect { 597cae66988SJoerg Wunsch const char *ps_baud; 598cae66988SJoerg Wunsch const char *ps_type; 599ea022d16SRodney W. Grimes } portspeeds[] = { 600ea022d16SRodney W. Grimes { "B110", "std.110" }, 601ea022d16SRodney W. Grimes { "B134", "std.134" }, 602ea022d16SRodney W. Grimes { "B150", "std.150" }, 603ea022d16SRodney W. Grimes { "B300", "std.300" }, 604ea022d16SRodney W. Grimes { "B600", "std.600" }, 605ea022d16SRodney W. Grimes { "B1200", "std.1200" }, 606ea022d16SRodney W. Grimes { "B2400", "std.2400" }, 607ea022d16SRodney W. Grimes { "B4800", "std.4800" }, 608ea022d16SRodney W. Grimes { "B9600", "std.9600" }, 609ea022d16SRodney W. Grimes { "B19200", "std.19200" }, 610ea022d16SRodney W. Grimes { 0 } 611ea022d16SRodney W. Grimes }; 612ea022d16SRodney W. Grimes 613cae66988SJoerg Wunsch const char * 61495289b27SWarner Losh portselector(void) 615ea022d16SRodney W. Grimes { 616cae66988SJoerg Wunsch char c, baud[20]; 617cae66988SJoerg Wunsch const char *type = "default"; 61895289b27SWarner Losh struct portselect *ps; 619ea022d16SRodney W. Grimes int len; 620ea022d16SRodney W. Grimes 621ea022d16SRodney W. Grimes alarm(5*60); 622ea022d16SRodney W. Grimes for (len = 0; len < sizeof (baud) - 1; len++) { 623ea022d16SRodney W. Grimes if (read(STDIN_FILENO, &c, 1) <= 0) 624ea022d16SRodney W. Grimes break; 625ea022d16SRodney W. Grimes c &= 0177; 626ea022d16SRodney W. Grimes if (c == '\n' || c == '\r') 627ea022d16SRodney W. Grimes break; 628ea022d16SRodney W. Grimes if (c == 'B') 629ea022d16SRodney W. Grimes len = 0; /* in case of leading garbage */ 630ea022d16SRodney W. Grimes baud[len] = c; 631ea022d16SRodney W. Grimes } 632ea022d16SRodney W. Grimes baud[len] = '\0'; 633ea022d16SRodney W. Grimes for (ps = portspeeds; ps->ps_baud; ps++) 634ea022d16SRodney W. Grimes if (strcmp(ps->ps_baud, baud) == 0) { 635ea022d16SRodney W. Grimes type = ps->ps_type; 636ea022d16SRodney W. Grimes break; 637ea022d16SRodney W. Grimes } 638ea022d16SRodney W. Grimes sleep(2); /* wait for connection to complete */ 639ea022d16SRodney W. Grimes return (type); 640ea022d16SRodney W. Grimes } 641ea022d16SRodney W. Grimes 642ea022d16SRodney W. Grimes /* 643ea022d16SRodney W. Grimes * This auto-baud speed select mechanism is written for the Micom 600 644ea022d16SRodney W. Grimes * portselector. Selection is done by looking at how the character '\r' 645ea022d16SRodney W. Grimes * is garbled at the different speeds. 646ea022d16SRodney W. Grimes */ 647cae66988SJoerg Wunsch const char * 64895289b27SWarner Losh autobaud(void) 649ea022d16SRodney W. Grimes { 650ea022d16SRodney W. Grimes int rfds; 651ea022d16SRodney W. Grimes struct timeval timeout; 652cae66988SJoerg Wunsch char c; 653cae66988SJoerg Wunsch const char *type = "9600-baud"; 654ea022d16SRodney W. Grimes 655cae66988SJoerg Wunsch (void)tcflush(0, TCIOFLUSH); 656ea022d16SRodney W. Grimes rfds = 1 << 0; 657ea022d16SRodney W. Grimes timeout.tv_sec = 5; 658ea022d16SRodney W. Grimes timeout.tv_usec = 0; 659ea022d16SRodney W. Grimes if (select(32, (fd_set *)&rfds, (fd_set *)NULL, 660ea022d16SRodney W. Grimes (fd_set *)NULL, &timeout) <= 0) 661ea022d16SRodney W. Grimes return (type); 662ea022d16SRodney W. Grimes if (read(STDIN_FILENO, &c, sizeof(char)) != sizeof(char)) 663ea022d16SRodney W. Grimes return (type); 664ea022d16SRodney W. Grimes timeout.tv_sec = 0; 665ea022d16SRodney W. Grimes timeout.tv_usec = 20; 666ea022d16SRodney W. Grimes (void) select(32, (fd_set *)NULL, (fd_set *)NULL, 667ea022d16SRodney W. Grimes (fd_set *)NULL, &timeout); 668cae66988SJoerg Wunsch (void)tcflush(0, TCIOFLUSH); 669ea022d16SRodney W. Grimes switch (c & 0377) { 670ea022d16SRodney W. Grimes 671ea022d16SRodney W. Grimes case 0200: /* 300-baud */ 672ea022d16SRodney W. Grimes type = "300-baud"; 673ea022d16SRodney W. Grimes break; 674ea022d16SRodney W. Grimes 675ea022d16SRodney W. Grimes case 0346: /* 1200-baud */ 676ea022d16SRodney W. Grimes type = "1200-baud"; 677ea022d16SRodney W. Grimes break; 678ea022d16SRodney W. Grimes 679ea022d16SRodney W. Grimes case 015: /* 2400-baud */ 680ea022d16SRodney W. Grimes case 0215: 681ea022d16SRodney W. Grimes type = "2400-baud"; 682ea022d16SRodney W. Grimes break; 683ea022d16SRodney W. Grimes 684ea022d16SRodney W. Grimes default: /* 4800-baud */ 685ea022d16SRodney W. Grimes type = "4800-baud"; 686ea022d16SRodney W. Grimes break; 687ea022d16SRodney W. Grimes 688ea022d16SRodney W. Grimes case 0377: /* 9600-baud */ 689ea022d16SRodney W. Grimes type = "9600-baud"; 690ea022d16SRodney W. Grimes break; 691ea022d16SRodney W. Grimes } 692ea022d16SRodney W. Grimes return (type); 693ea022d16SRodney W. Grimes } 694