2005-05-21 21:18:51 +04:00
|
|
|
/* $NetBSD: subr.c,v 1.31 2005/05/21 17:18:51 christos Exp $ */
|
1996-08-01 00:33:02 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1994-08-18 00:10:27 +04:00
|
|
|
* Copyright (c) 1983, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 13:46:37 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-08-06 11:22:24 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1996-08-01 00:33:02 +04:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "from: @(#)subr.c 8.1 (Berkeley) 6/4/93";
|
|
|
|
#else
|
2005-05-21 21:18:51 +04:00
|
|
|
__RCSID("$NetBSD: subr.c,v 1.31 2005/05/21 17:18:51 christos Exp $");
|
1996-08-01 00:33:02 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Melbourne getty.
|
|
|
|
*/
|
1994-04-29 02:12:28 +04:00
|
|
|
#define COMPAT_43
|
1996-09-27 06:15:35 +04:00
|
|
|
#include <sys/param.h>
|
1997-08-06 11:22:24 +04:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
1994-03-09 07:59:55 +03:00
|
|
|
#include <stdlib.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <string.h>
|
1994-04-29 02:12:28 +04:00
|
|
|
#include <termios.h>
|
1997-08-06 11:22:24 +04:00
|
|
|
#include <unistd.h>
|
2002-09-23 07:32:34 +04:00
|
|
|
#include <poll.h>
|
1994-03-09 07:59:55 +03:00
|
|
|
|
1997-08-06 11:22:24 +04:00
|
|
|
#include "extern.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "gettytab.h"
|
1994-03-09 07:59:55 +03:00
|
|
|
#include "pathnames.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-08-18 00:10:27 +04:00
|
|
|
extern struct termios tmode, omode;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2001-11-10 20:35:54 +03:00
|
|
|
static void compatflags(long);
|
1994-08-18 00:10:27 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* Get a table entry.
|
|
|
|
*/
|
1994-08-18 00:10:27 +04:00
|
|
|
void
|
2001-11-10 20:35:54 +03:00
|
|
|
gettable(char *name, char *buf)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1998-03-30 05:52:02 +04:00
|
|
|
struct gettystrs *sp;
|
|
|
|
struct gettynums *np;
|
|
|
|
struct gettyflags *fp;
|
1994-03-09 07:59:55 +03:00
|
|
|
long n;
|
2004-04-24 01:04:25 +04:00
|
|
|
const char *dba[2];
|
1994-03-09 07:59:55 +03:00
|
|
|
dba[0] = _PATH_GETTYTAB;
|
|
|
|
dba[1] = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-03-09 07:59:55 +03:00
|
|
|
if (cgetent(&buf, dba, name) != 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
for (sp = gettystrs; sp->field; sp++)
|
1994-03-09 07:59:55 +03:00
|
|
|
cgetstr(buf, sp->field, &sp->value);
|
1993-03-21 12:45:37 +03:00
|
|
|
for (np = gettynums; np->field; np++) {
|
1994-03-09 07:59:55 +03:00
|
|
|
if (cgetnum(buf, np->field, &n) == -1)
|
1993-03-21 12:45:37 +03:00
|
|
|
np->set = 0;
|
|
|
|
else {
|
|
|
|
np->set = 1;
|
|
|
|
np->value = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (fp = gettyflags; fp->field; fp++) {
|
1994-03-09 07:59:55 +03:00
|
|
|
if (cgetcap(buf, fp->field, ':') == NULL)
|
1993-03-21 12:45:37 +03:00
|
|
|
fp->set = 0;
|
|
|
|
else {
|
|
|
|
fp->set = 1;
|
1994-03-09 07:59:55 +03:00
|
|
|
fp->value = 1 ^ fp->invrt;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
1994-03-09 07:59:55 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf("name=\"%s\", buf=\"%s\"\n", name, buf);
|
|
|
|
for (sp = gettystrs; sp->field; sp++)
|
|
|
|
printf("cgetstr: %s=%s\n", sp->field, sp->value);
|
|
|
|
for (np = gettynums; np->field; np++)
|
|
|
|
printf("cgetnum: %s=%d\n", np->field, np->value);
|
|
|
|
for (fp = gettyflags; fp->field; fp++)
|
1994-08-18 00:10:27 +04:00
|
|
|
printf("cgetflags: %s='%c' set='%c'\n", fp->field,
|
|
|
|
fp->value + '0', fp->set + '0');
|
1994-03-09 07:59:55 +03:00
|
|
|
exit(1);
|
|
|
|
#endif /* DEBUG */
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1994-08-18 00:10:27 +04:00
|
|
|
void
|
2001-11-10 20:35:54 +03:00
|
|
|
gendefaults(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1998-03-30 05:52:02 +04:00
|
|
|
struct gettystrs *sp;
|
|
|
|
struct gettynums *np;
|
|
|
|
struct gettyflags *fp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
for (sp = gettystrs; sp->field; sp++)
|
|
|
|
if (sp->value)
|
|
|
|
sp->defalt = sp->value;
|
|
|
|
for (np = gettynums; np->field; np++)
|
|
|
|
if (np->set)
|
|
|
|
np->defalt = np->value;
|
|
|
|
for (fp = gettyflags; fp->field; fp++)
|
|
|
|
if (fp->set)
|
|
|
|
fp->defalt = fp->value;
|
|
|
|
else
|
|
|
|
fp->defalt = fp->invrt;
|
|
|
|
}
|
|
|
|
|
1994-08-18 00:10:27 +04:00
|
|
|
void
|
2001-11-10 20:35:54 +03:00
|
|
|
setdefaults(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1998-03-30 05:52:02 +04:00
|
|
|
struct gettystrs *sp;
|
|
|
|
struct gettynums *np;
|
|
|
|
struct gettyflags *fp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
for (sp = gettystrs; sp->field; sp++)
|
|
|
|
if (!sp->value)
|
|
|
|
sp->value = sp->defalt;
|
|
|
|
for (np = gettynums; np->field; np++)
|
|
|
|
if (!np->set)
|
|
|
|
np->value = np->defalt;
|
|
|
|
for (fp = gettyflags; fp->field; fp++)
|
|
|
|
if (!fp->set)
|
|
|
|
fp->value = fp->defalt;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char **
|
|
|
|
charnames[] = {
|
|
|
|
&ER, &KL, &IN, &QU, &XN, &XF, &ET, &BK,
|
2005-05-21 21:18:51 +04:00
|
|
|
&SU, &DS, &RP, &FL, &WE, &LN, &ST, &B2, 0
|
1993-03-21 12:45:37 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static char *
|
|
|
|
charvars[] = {
|
1994-04-29 02:12:28 +04:00
|
|
|
&tmode.c_cc[VERASE], &tmode.c_cc[VKILL], &tmode.c_cc[VINTR],
|
|
|
|
&tmode.c_cc[VQUIT], &tmode.c_cc[VSTART], &tmode.c_cc[VSTOP],
|
|
|
|
&tmode.c_cc[VEOF], &tmode.c_cc[VEOL], &tmode.c_cc[VSUSP],
|
|
|
|
&tmode.c_cc[VDSUSP], &tmode.c_cc[VREPRINT], &tmode.c_cc[VDISCARD],
|
2005-05-21 21:18:51 +04:00
|
|
|
&tmode.c_cc[VWERASE], &tmode.c_cc[VLNEXT], &tmode.c_cc[VSTATUS],
|
|
|
|
&tmode.c_cc[VEOL2], 0
|
1993-03-21 12:45:37 +03:00
|
|
|
};
|
|
|
|
|
1994-08-18 00:10:27 +04:00
|
|
|
void
|
2001-11-10 20:35:54 +03:00
|
|
|
setchars(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1998-03-30 05:52:02 +04:00
|
|
|
int i;
|
|
|
|
char *p;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
for (i = 0; charnames[i]; i++) {
|
|
|
|
p = *charnames[i];
|
|
|
|
if (p && *p)
|
|
|
|
*charvars[i] = *p;
|
|
|
|
else
|
1994-08-24 20:39:24 +04:00
|
|
|
*charvars[i] = _POSIX_VDISABLE;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-10-05 10:16:25 +03:00
|
|
|
/* Macros to clear/set/test flags. */
|
|
|
|
#define SET(t, f) (t) |= (f)
|
|
|
|
#define CLR(t, f) (t) &= ~(f)
|
|
|
|
#define ISSET(t, f) ((t) & (f))
|
|
|
|
|
1994-04-29 02:12:28 +04:00
|
|
|
void
|
2001-11-10 20:35:54 +03:00
|
|
|
setflags(int n)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1998-03-30 05:52:02 +04:00
|
|
|
tcflag_t iflag, oflag, cflag, lflag;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-04-29 02:12:28 +04:00
|
|
|
#ifdef COMPAT_43
|
1993-03-21 12:45:37 +03:00
|
|
|
switch (n) {
|
|
|
|
case 0:
|
1994-04-29 02:12:28 +04:00
|
|
|
if (F0set) {
|
|
|
|
compatflags(F0);
|
|
|
|
return;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case 1:
|
1994-04-29 02:12:28 +04:00
|
|
|
if (F1set) {
|
|
|
|
compatflags(F1);
|
|
|
|
return;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
default:
|
1994-04-29 02:12:28 +04:00
|
|
|
if (F2set) {
|
|
|
|
compatflags(F2);
|
|
|
|
return;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
}
|
1994-04-29 02:12:28 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-04-29 02:12:28 +04:00
|
|
|
switch (n) {
|
|
|
|
case 0:
|
|
|
|
if (C0set && I0set && L0set && O0set) {
|
|
|
|
tmode.c_cflag = C0;
|
|
|
|
tmode.c_iflag = I0;
|
|
|
|
tmode.c_lflag = L0;
|
|
|
|
tmode.c_oflag = O0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (C1set && I1set && L1set && O1set) {
|
|
|
|
tmode.c_cflag = C1;
|
|
|
|
tmode.c_iflag = I1;
|
|
|
|
tmode.c_lflag = L1;
|
|
|
|
tmode.c_oflag = O1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (C2set && I2set && L2set && O2set) {
|
|
|
|
tmode.c_cflag = C2;
|
|
|
|
tmode.c_iflag = I2;
|
|
|
|
tmode.c_lflag = L2;
|
|
|
|
tmode.c_oflag = O2;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-08-18 00:10:27 +04:00
|
|
|
iflag = omode.c_iflag;
|
1995-10-05 03:54:45 +03:00
|
|
|
oflag = omode.c_oflag;
|
1994-08-18 00:10:27 +04:00
|
|
|
cflag = omode.c_cflag;
|
|
|
|
lflag = omode.c_lflag;
|
1994-04-29 02:12:28 +04:00
|
|
|
|
|
|
|
if (NP) {
|
1995-10-05 10:16:25 +03:00
|
|
|
CLR(cflag, CSIZE|PARENB);
|
|
|
|
SET(cflag, CS8);
|
|
|
|
CLR(iflag, ISTRIP|INPCK|IGNPAR);
|
|
|
|
} else if (AP || EP || OP) {
|
|
|
|
CLR(cflag, CSIZE);
|
|
|
|
SET(cflag, CS7|PARENB);
|
|
|
|
SET(iflag, ISTRIP);
|
|
|
|
if (OP && !EP) {
|
|
|
|
SET(iflag, INPCK|IGNPAR);
|
|
|
|
SET(cflag, PARODD);
|
|
|
|
if (AP)
|
|
|
|
CLR(iflag, INPCK);
|
|
|
|
} else if (EP && !OP) {
|
|
|
|
SET(iflag, INPCK|IGNPAR);
|
|
|
|
CLR(cflag, PARODD);
|
|
|
|
if (AP)
|
|
|
|
CLR(iflag, INPCK);
|
1997-08-06 11:22:24 +04:00
|
|
|
} else if (AP || (EP && OP)) {
|
1995-10-05 10:16:25 +03:00
|
|
|
CLR(iflag, INPCK|IGNPAR);
|
|
|
|
CLR(cflag, PARODD);
|
|
|
|
}
|
1994-08-18 00:10:27 +04:00
|
|
|
} /* else, leave as is */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-04-29 02:12:28 +04:00
|
|
|
#if 0
|
1993-03-21 12:45:37 +03:00
|
|
|
if (UC)
|
|
|
|
f |= LCASE;
|
1994-04-29 02:12:28 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (HC)
|
1995-10-05 10:16:25 +03:00
|
|
|
SET(cflag, HUPCL);
|
1994-08-18 00:10:27 +04:00
|
|
|
else
|
1995-10-05 10:16:25 +03:00
|
|
|
CLR(cflag, HUPCL);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-09-05 12:52:49 +04:00
|
|
|
if (MB)
|
1995-10-05 10:16:25 +03:00
|
|
|
SET(cflag, MDMBUF);
|
1994-09-05 12:52:49 +04:00
|
|
|
else
|
1995-10-05 10:16:25 +03:00
|
|
|
CLR(cflag, MDMBUF);
|
1994-09-05 12:52:49 +04:00
|
|
|
|
1994-04-29 02:12:28 +04:00
|
|
|
if (NL) {
|
1995-10-05 10:16:25 +03:00
|
|
|
SET(iflag, ICRNL);
|
|
|
|
SET(oflag, ONLCR|OPOST);
|
1995-10-05 03:40:33 +03:00
|
|
|
} else {
|
1995-10-05 10:16:25 +03:00
|
|
|
CLR(iflag, ICRNL);
|
|
|
|
CLR(oflag, ONLCR);
|
1994-04-29 02:12:28 +04:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1995-10-05 03:54:45 +03:00
|
|
|
if (!HT)
|
1995-10-05 10:16:25 +03:00
|
|
|
SET(oflag, OXTABS|OPOST);
|
1995-10-05 03:54:45 +03:00
|
|
|
else
|
1995-10-05 10:16:25 +03:00
|
|
|
CLR(oflag, OXTABS);
|
1995-10-05 03:54:45 +03:00
|
|
|
|
1994-04-29 02:12:28 +04:00
|
|
|
#ifdef XXX_DELAY
|
1995-10-05 10:16:25 +03:00
|
|
|
SET(f, delaybits());
|
1994-04-29 02:12:28 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (n == 1) { /* read mode flags */
|
1994-04-29 02:12:28 +04:00
|
|
|
if (RW) {
|
|
|
|
iflag = 0;
|
1995-10-05 10:16:25 +03:00
|
|
|
CLR(oflag, OPOST);
|
|
|
|
CLR(cflag, CSIZE|PARENB);
|
|
|
|
SET(cflag, CS8);
|
1994-04-29 02:12:28 +04:00
|
|
|
lflag = 0;
|
|
|
|
} else {
|
1995-10-05 10:16:25 +03:00
|
|
|
CLR(lflag, ICANON);
|
1994-04-29 02:12:28 +04:00
|
|
|
}
|
|
|
|
goto out;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (n == 0)
|
1994-04-29 02:12:28 +04:00
|
|
|
goto out;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-04-29 02:12:28 +04:00
|
|
|
#if 0
|
1993-03-21 12:45:37 +03:00
|
|
|
if (CB)
|
1995-10-05 10:16:25 +03:00
|
|
|
SET(f, CRTBS);
|
1994-04-29 02:12:28 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (CE)
|
1995-10-05 10:16:25 +03:00
|
|
|
SET(lflag, ECHOE);
|
1995-10-05 03:54:45 +03:00
|
|
|
else
|
1995-10-05 10:16:25 +03:00
|
|
|
CLR(lflag, ECHOE);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (CK)
|
1995-10-05 10:16:25 +03:00
|
|
|
SET(lflag, ECHOKE);
|
1995-10-05 03:54:45 +03:00
|
|
|
else
|
1995-10-05 10:16:25 +03:00
|
|
|
CLR(lflag, ECHOKE);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (PE)
|
1995-10-05 10:16:25 +03:00
|
|
|
SET(lflag, ECHOPRT);
|
1995-10-05 03:54:45 +03:00
|
|
|
else
|
1995-10-05 10:16:25 +03:00
|
|
|
CLR(lflag, ECHOPRT);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (EC)
|
1995-10-05 10:16:25 +03:00
|
|
|
SET(lflag, ECHO);
|
1995-10-05 03:54:45 +03:00
|
|
|
else
|
1995-10-05 10:16:25 +03:00
|
|
|
CLR(lflag, ECHO);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (XC)
|
1995-10-05 10:16:25 +03:00
|
|
|
SET(lflag, ECHOCTL);
|
1995-10-05 03:54:45 +03:00
|
|
|
else
|
1995-10-05 10:16:25 +03:00
|
|
|
CLR(lflag, ECHOCTL);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (DX)
|
1995-10-05 10:16:25 +03:00
|
|
|
SET(lflag, IXANY);
|
1995-10-05 03:54:45 +03:00
|
|
|
else
|
1995-10-05 10:16:25 +03:00
|
|
|
CLR(lflag, IXANY);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-04-29 02:12:28 +04:00
|
|
|
out:
|
|
|
|
tmode.c_iflag = iflag;
|
|
|
|
tmode.c_oflag = oflag;
|
|
|
|
tmode.c_cflag = cflag;
|
|
|
|
tmode.c_lflag = lflag;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef COMPAT_43
|
|
|
|
/*
|
|
|
|
* Old TTY => termios, snatched from <sys/kern/tty_compat.c>
|
|
|
|
*/
|
|
|
|
void
|
2001-11-10 20:35:54 +03:00
|
|
|
compatflags(long flags)
|
1994-04-29 02:12:28 +04:00
|
|
|
{
|
1998-03-30 05:52:02 +04:00
|
|
|
tcflag_t iflag, oflag, cflag, lflag;
|
1994-04-29 02:12:28 +04:00
|
|
|
|
1995-10-05 10:16:25 +03:00
|
|
|
iflag = BRKINT|ICRNL|IMAXBEL|IXON|IXANY;
|
|
|
|
oflag = OPOST|ONLCR|OXTABS;
|
|
|
|
cflag = CREAD;
|
|
|
|
lflag = ICANON|ISIG|IEXTEN;
|
1994-04-29 02:12:28 +04:00
|
|
|
|
1995-10-05 10:16:25 +03:00
|
|
|
if (ISSET(flags, TANDEM))
|
|
|
|
SET(iflag, IXOFF);
|
1994-08-15 19:46:44 +04:00
|
|
|
else
|
1995-10-05 10:16:25 +03:00
|
|
|
CLR(iflag, IXOFF);
|
|
|
|
if (ISSET(flags, ECHO))
|
|
|
|
SET(lflag, ECHO);
|
1994-08-15 19:46:44 +04:00
|
|
|
else
|
1995-10-05 10:16:25 +03:00
|
|
|
CLR(lflag, ECHO);
|
|
|
|
if (ISSET(flags, CRMOD)) {
|
|
|
|
SET(iflag, ICRNL);
|
|
|
|
SET(oflag, ONLCR);
|
1994-08-15 19:46:44 +04:00
|
|
|
} else {
|
1995-10-05 10:16:25 +03:00
|
|
|
CLR(iflag, ICRNL);
|
|
|
|
CLR(oflag, ONLCR);
|
1994-08-15 19:46:44 +04:00
|
|
|
}
|
1995-10-05 10:16:25 +03:00
|
|
|
if (ISSET(flags, XTABS))
|
|
|
|
SET(oflag, OXTABS);
|
1994-08-15 19:46:44 +04:00
|
|
|
else
|
1995-10-05 10:16:25 +03:00
|
|
|
CLR(oflag, OXTABS);
|
1994-08-15 19:46:44 +04:00
|
|
|
|
1995-10-05 10:16:25 +03:00
|
|
|
|
|
|
|
if (ISSET(flags, RAW)) {
|
1994-08-15 19:46:44 +04:00
|
|
|
iflag &= IXOFF;
|
1995-10-05 10:16:25 +03:00
|
|
|
CLR(lflag, ISIG|ICANON|IEXTEN);
|
1995-10-05 11:51:31 +03:00
|
|
|
CLR(cflag, PARENB);
|
1994-04-29 02:12:28 +04:00
|
|
|
} else {
|
1995-10-05 10:16:25 +03:00
|
|
|
SET(iflag, BRKINT|IXON|IMAXBEL);
|
|
|
|
SET(lflag, ISIG|IEXTEN);
|
|
|
|
if (ISSET(flags, CBREAK))
|
|
|
|
CLR(lflag, ICANON);
|
1994-04-29 02:12:28 +04:00
|
|
|
else
|
1995-10-05 10:16:25 +03:00
|
|
|
SET(lflag, ICANON);
|
1995-10-05 11:51:31 +03:00
|
|
|
switch (ISSET(flags, ANYP)) {
|
|
|
|
case 0:
|
|
|
|
CLR(cflag, PARENB);
|
|
|
|
break;
|
|
|
|
case ANYP:
|
|
|
|
SET(cflag, PARENB);
|
|
|
|
CLR(iflag, INPCK);
|
|
|
|
break;
|
|
|
|
case EVENP:
|
|
|
|
SET(cflag, PARENB);
|
|
|
|
SET(iflag, INPCK);
|
|
|
|
CLR(cflag, PARODD);
|
|
|
|
break;
|
|
|
|
case ODDP:
|
|
|
|
SET(cflag, PARENB);
|
|
|
|
SET(iflag, INPCK);
|
|
|
|
SET(cflag, PARODD);
|
|
|
|
break;
|
|
|
|
}
|
1994-08-15 19:46:44 +04:00
|
|
|
}
|
|
|
|
|
1995-10-05 10:16:25 +03:00
|
|
|
/* Nothing we can do with CRTBS. */
|
|
|
|
if (ISSET(flags, PRTERA))
|
|
|
|
SET(lflag, ECHOPRT);
|
1994-08-15 19:46:44 +04:00
|
|
|
else
|
1995-10-05 10:16:25 +03:00
|
|
|
CLR(lflag, ECHOPRT);
|
|
|
|
if (ISSET(flags, CRTERA))
|
|
|
|
SET(lflag, ECHOE);
|
1994-04-29 02:12:28 +04:00
|
|
|
else
|
1995-10-05 10:16:25 +03:00
|
|
|
CLR(lflag, ECHOE);
|
|
|
|
/* Nothing we can do with TILDE. */
|
|
|
|
if (ISSET(flags, MDMBUF))
|
|
|
|
SET(cflag, MDMBUF);
|
1994-08-15 19:46:44 +04:00
|
|
|
else
|
1995-10-05 10:16:25 +03:00
|
|
|
CLR(cflag, MDMBUF);
|
|
|
|
if (ISSET(flags, NOHANG))
|
|
|
|
CLR(cflag, HUPCL);
|
1994-08-15 19:46:44 +04:00
|
|
|
else
|
1995-10-05 10:16:25 +03:00
|
|
|
SET(cflag, HUPCL);
|
|
|
|
if (ISSET(flags, CRTKIL))
|
|
|
|
SET(lflag, ECHOKE);
|
1994-04-29 02:12:28 +04:00
|
|
|
else
|
1995-10-05 10:16:25 +03:00
|
|
|
CLR(lflag, ECHOKE);
|
|
|
|
if (ISSET(flags, CTLECH))
|
|
|
|
SET(lflag, ECHOCTL);
|
1994-04-29 02:12:28 +04:00
|
|
|
else
|
1995-10-05 10:16:25 +03:00
|
|
|
CLR(lflag, ECHOCTL);
|
|
|
|
if (!ISSET(flags, DECCTQ))
|
|
|
|
SET(iflag, IXANY);
|
1994-04-29 02:12:28 +04:00
|
|
|
else
|
1995-10-05 10:16:25 +03:00
|
|
|
CLR(iflag, IXANY);
|
|
|
|
CLR(lflag, TOSTOP|FLUSHO|PENDIN|NOFLSH);
|
|
|
|
SET(lflag, ISSET(flags, TOSTOP|FLUSHO|PENDIN|NOFLSH));
|
|
|
|
|
|
|
|
if (ISSET(flags, RAW|LITOUT|PASS8)) {
|
1995-10-05 11:51:31 +03:00
|
|
|
CLR(cflag, CSIZE);
|
1995-10-05 10:16:25 +03:00
|
|
|
SET(cflag, CS8);
|
|
|
|
if (!ISSET(flags, RAW|PASS8))
|
|
|
|
SET(iflag, ISTRIP);
|
1994-08-15 19:46:44 +04:00
|
|
|
else
|
1995-10-05 10:16:25 +03:00
|
|
|
CLR(iflag, ISTRIP);
|
|
|
|
if (!ISSET(flags, RAW|LITOUT))
|
|
|
|
SET(oflag, OPOST);
|
1994-08-15 19:46:44 +04:00
|
|
|
else
|
1995-10-05 10:16:25 +03:00
|
|
|
CLR(oflag, OPOST);
|
1994-08-15 19:46:44 +04:00
|
|
|
} else {
|
1995-10-05 10:16:25 +03:00
|
|
|
CLR(cflag, CSIZE);
|
1995-10-05 11:51:31 +03:00
|
|
|
SET(cflag, CS7);
|
1995-10-05 10:16:25 +03:00
|
|
|
SET(iflag, ISTRIP);
|
|
|
|
SET(oflag, OPOST);
|
1994-04-29 02:12:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
tmode.c_iflag = iflag;
|
|
|
|
tmode.c_oflag = oflag;
|
|
|
|
tmode.c_cflag = cflag;
|
|
|
|
tmode.c_lflag = lflag;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1994-04-29 02:12:28 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-04-29 02:12:28 +04:00
|
|
|
#ifdef XXX_DELAY
|
1993-03-21 12:45:37 +03:00
|
|
|
struct delayval {
|
|
|
|
unsigned delay; /* delay in ms */
|
|
|
|
int bits;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* below are random guesses, I can't be bothered checking
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct delayval crdelay[] = {
|
1994-08-18 00:10:27 +04:00
|
|
|
{ 1, CR1 },
|
|
|
|
{ 2, CR2 },
|
|
|
|
{ 3, CR3 },
|
|
|
|
{ 83, CR1 },
|
|
|
|
{ 166, CR2 },
|
|
|
|
{ 0, CR3 },
|
1993-03-21 12:45:37 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct delayval nldelay[] = {
|
1994-08-18 00:10:27 +04:00
|
|
|
{ 1, NL1 }, /* special, calculated */
|
|
|
|
{ 2, NL2 },
|
|
|
|
{ 3, NL3 },
|
|
|
|
{ 100, NL2 },
|
|
|
|
{ 0, NL3 },
|
1993-03-21 12:45:37 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct delayval bsdelay[] = {
|
1994-08-18 00:10:27 +04:00
|
|
|
{ 1, BS1 },
|
|
|
|
{ 0, 0 },
|
1993-03-21 12:45:37 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct delayval ffdelay[] = {
|
1994-08-18 00:10:27 +04:00
|
|
|
{ 1, FF1 },
|
|
|
|
{ 1750, FF1 },
|
|
|
|
{ 0, FF1 },
|
1993-03-21 12:45:37 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct delayval tbdelay[] = {
|
1994-08-18 00:10:27 +04:00
|
|
|
{ 1, TAB1 },
|
|
|
|
{ 2, TAB2 },
|
|
|
|
{ 3, XTABS }, /* this is expand tabs */
|
|
|
|
{ 100, TAB1 },
|
|
|
|
{ 0, TAB2 },
|
1993-03-21 12:45:37 +03:00
|
|
|
};
|
|
|
|
|
1994-08-18 00:10:27 +04:00
|
|
|
int
|
2001-11-10 20:35:54 +03:00
|
|
|
delaybits(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1998-03-30 05:52:02 +04:00
|
|
|
int f;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
f = adelay(CD, crdelay);
|
|
|
|
f |= adelay(ND, nldelay);
|
|
|
|
f |= adelay(FD, ffdelay);
|
|
|
|
f |= adelay(TD, tbdelay);
|
|
|
|
f |= adelay(BD, bsdelay);
|
|
|
|
return (f);
|
|
|
|
}
|
|
|
|
|
1994-08-18 00:10:27 +04:00
|
|
|
int
|
2001-11-10 20:35:54 +03:00
|
|
|
adelay(int ms, struct delayval *dp)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
if (ms == 0)
|
|
|
|
return (0);
|
|
|
|
while (dp->delay && ms > dp->delay)
|
|
|
|
dp++;
|
|
|
|
return (dp->bits);
|
|
|
|
}
|
1994-04-29 02:12:28 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1996-09-27 06:15:35 +04:00
|
|
|
char editedhost[MAXHOSTNAMELEN];
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-08-18 00:10:27 +04:00
|
|
|
void
|
2001-11-10 20:35:54 +03:00
|
|
|
edithost(char *pat)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1998-03-30 05:52:02 +04:00
|
|
|
char *host = HN;
|
|
|
|
char *res = editedhost;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (!pat)
|
|
|
|
pat = "";
|
|
|
|
while (*pat) {
|
|
|
|
switch (*pat) {
|
|
|
|
|
|
|
|
case '#':
|
|
|
|
if (*host)
|
|
|
|
host++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '@':
|
|
|
|
if (*host)
|
|
|
|
*res++ = *host++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
*res++ = *pat;
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
if (res == &editedhost[sizeof editedhost - 1]) {
|
|
|
|
*res = '\0';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pat++;
|
|
|
|
}
|
|
|
|
if (*host)
|
|
|
|
strncpy(res, host, sizeof editedhost - (res - editedhost) - 1);
|
|
|
|
else
|
|
|
|
*res = '\0';
|
|
|
|
editedhost[sizeof editedhost - 1] = '\0';
|
|
|
|
}
|
|
|
|
|
1994-08-18 00:10:27 +04:00
|
|
|
void
|
2001-11-10 20:35:54 +03:00
|
|
|
makeenv(char *env[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
static char termbuf[128] = "TERM=";
|
1998-03-30 05:52:02 +04:00
|
|
|
char *p, *q;
|
|
|
|
char **ep;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
ep = env;
|
|
|
|
if (TT && *TT) {
|
2000-09-19 20:17:48 +04:00
|
|
|
strlcat(termbuf, TT, sizeof(termbuf));
|
1993-03-21 12:45:37 +03:00
|
|
|
*ep++ = termbuf;
|
|
|
|
}
|
1997-08-06 11:22:24 +04:00
|
|
|
if ((p = EV) != NULL) {
|
1993-03-21 12:45:37 +03:00
|
|
|
q = p;
|
1997-08-06 11:22:24 +04:00
|
|
|
while ((q = strchr(q, ',')) != NULL) {
|
1993-03-21 12:45:37 +03:00
|
|
|
*q++ = '\0';
|
|
|
|
*ep++ = p;
|
|
|
|
p = q;
|
|
|
|
}
|
|
|
|
if (*p)
|
|
|
|
*ep++ = p;
|
|
|
|
}
|
|
|
|
*ep = (char *)0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This speed select mechanism is written for the Develcon DATASWITCH.
|
|
|
|
* The Develcon sends a string of the form "B{speed}\n" at a predefined
|
|
|
|
* baud rate. This string indicates the user's actual speed.
|
|
|
|
* The routine below returns the terminal type mapped from derived speed.
|
|
|
|
*/
|
|
|
|
struct portselect {
|
|
|
|
char *ps_baud;
|
|
|
|
char *ps_type;
|
|
|
|
} portspeeds[] = {
|
|
|
|
{ "B110", "std.110" },
|
|
|
|
{ "B134", "std.134" },
|
|
|
|
{ "B150", "std.150" },
|
|
|
|
{ "B300", "std.300" },
|
|
|
|
{ "B600", "std.600" },
|
|
|
|
{ "B1200", "std.1200" },
|
|
|
|
{ "B2400", "std.2400" },
|
|
|
|
{ "B4800", "std.4800" },
|
|
|
|
{ "B9600", "std.9600" },
|
|
|
|
{ "B19200", "std.19200" },
|
|
|
|
{ 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
char *
|
2001-11-10 20:35:54 +03:00
|
|
|
portselector(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
char c, baud[20], *type = "default";
|
1998-03-30 05:52:02 +04:00
|
|
|
struct portselect *ps;
|
1993-03-21 12:45:37 +03:00
|
|
|
int len;
|
|
|
|
|
|
|
|
alarm(5*60);
|
|
|
|
for (len = 0; len < sizeof (baud) - 1; len++) {
|
|
|
|
if (read(STDIN_FILENO, &c, 1) <= 0)
|
|
|
|
break;
|
|
|
|
c &= 0177;
|
|
|
|
if (c == '\n' || c == '\r')
|
|
|
|
break;
|
|
|
|
if (c == 'B')
|
|
|
|
len = 0; /* in case of leading garbage */
|
|
|
|
baud[len] = c;
|
|
|
|
}
|
|
|
|
baud[len] = '\0';
|
|
|
|
for (ps = portspeeds; ps->ps_baud; ps++)
|
|
|
|
if (strcmp(ps->ps_baud, baud) == 0) {
|
|
|
|
type = ps->ps_type;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sleep(2); /* wait for connection to complete */
|
|
|
|
return (type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This auto-baud speed select mechanism is written for the Micom 600
|
|
|
|
* portselector. Selection is done by looking at how the character '\r'
|
|
|
|
* is garbled at the different speeds.
|
|
|
|
*/
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
char *
|
2001-11-10 20:35:54 +03:00
|
|
|
autobaud(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2002-09-19 00:04:51 +04:00
|
|
|
struct pollfd set[1];
|
|
|
|
struct timespec timeout;
|
1993-03-21 12:45:37 +03:00
|
|
|
char c, *type = "9600-baud";
|
|
|
|
|
1994-09-05 13:09:43 +04:00
|
|
|
(void)tcflush(0, TCIOFLUSH);
|
2002-09-19 00:04:51 +04:00
|
|
|
set[0].fd = STDIN_FILENO;
|
|
|
|
set[0].events = POLLIN;
|
|
|
|
if (poll(set, 1, 5000) <= 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
return (type);
|
2000-09-19 20:17:48 +04:00
|
|
|
if (read(STDIN_FILENO, &c, 1) != 1)
|
1993-03-21 12:45:37 +03:00
|
|
|
return (type);
|
|
|
|
timeout.tv_sec = 0;
|
2002-09-19 00:04:51 +04:00
|
|
|
timeout.tv_nsec = 20000;
|
|
|
|
(void)nanosleep(&timeout, NULL);
|
1994-09-05 13:09:43 +04:00
|
|
|
(void)tcflush(0, TCIOFLUSH);
|
1993-03-21 12:45:37 +03:00
|
|
|
switch (c & 0377) {
|
|
|
|
|
|
|
|
case 0200: /* 300-baud */
|
|
|
|
type = "300-baud";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0346: /* 1200-baud */
|
|
|
|
type = "1200-baud";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 015: /* 2400-baud */
|
|
|
|
case 0215:
|
|
|
|
type = "2400-baud";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* 4800-baud */
|
|
|
|
type = "4800-baud";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0377: /* 9600-baud */
|
|
|
|
type = "9600-baud";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (type);
|
|
|
|
}
|