2006-12-14 20:09:43 +03:00
|
|
|
/* $NetBSD: tip.c,v 1.46 2006/12/14 17:09:43 christos Exp $ */
|
1994-12-08 12:30:36 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1994-12-08 12:30:36 +03: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 15:13:06 +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-11-22 10:28:39 +03:00
|
|
|
#include <sys/cdefs.h>
|
2006-11-29 17:46:27 +03:00
|
|
|
#include <getopt.h>
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1997-11-22 10:28:39 +03:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1994-12-08 12:30:36 +03:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)tip.c 8.1 (Berkeley) 6/6/93";
|
|
|
|
#endif
|
2006-12-14 20:09:43 +03:00
|
|
|
__RCSID("$NetBSD: tip.c,v 1.46 2006/12/14 17:09:43 christos Exp $");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* tip - UNIX link to other systems
|
|
|
|
* tip [-v] [-speed] system-name
|
|
|
|
* or
|
2006-11-29 17:46:27 +03:00
|
|
|
* cu [options] [phone-number|"dir"]
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
|
|
|
#include "tip.h"
|
|
|
|
#include "pathnames.h"
|
|
|
|
|
2006-11-29 17:46:27 +03:00
|
|
|
static void tipusage(void);
|
|
|
|
|
2006-04-03 04:51:13 +04:00
|
|
|
int escape(void);
|
|
|
|
int main(int, char **);
|
|
|
|
void intprompt(int);
|
|
|
|
void tipin(void);
|
1997-11-22 10:28:39 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
char PNbuf[256]; /* This limits the size of a number */
|
|
|
|
|
2004-04-24 02:24:34 +04:00
|
|
|
static char path_phones[] = _PATH_PHONES;
|
|
|
|
|
1997-11-22 10:28:39 +03:00
|
|
|
int
|
2006-04-03 04:51:13 +04:00
|
|
|
main(int argc, char *argv[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2004-04-24 02:11:44 +04:00
|
|
|
char *System = NULL;
|
2006-11-29 17:46:27 +03:00
|
|
|
int c, i;
|
1997-11-22 10:28:39 +03:00
|
|
|
char *p;
|
2004-04-24 02:11:44 +04:00
|
|
|
const char *q;
|
1993-03-21 12:45:37 +03:00
|
|
|
char sbuf[12];
|
2006-11-29 17:46:27 +03:00
|
|
|
static char brbuf[16];
|
1997-12-17 19:56:30 +03:00
|
|
|
int fcarg;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
gid = getgid();
|
|
|
|
egid = getegid();
|
|
|
|
uid = getuid();
|
|
|
|
euid = geteuid();
|
1997-11-22 10:28:39 +03:00
|
|
|
if (equal(basename(argv[0]), "cu")) {
|
1993-03-21 12:45:37 +03:00
|
|
|
cumode = 1;
|
|
|
|
cumain(argc, argv);
|
|
|
|
goto cucommon;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc > 4) {
|
2006-11-29 17:46:27 +03:00
|
|
|
tipusage();
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
if (!isatty(0)) {
|
2006-12-14 20:09:43 +03:00
|
|
|
(void)fprintf(stderr, "%s: must be interactive\n", getprogname());
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2006-11-29 17:46:27 +03:00
|
|
|
while((c = getopt(argc, argv, "v0123456789")) != -1) {
|
|
|
|
switch(c) {
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
case 'v':
|
|
|
|
vflag++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
2006-12-14 20:09:43 +03:00
|
|
|
(void)snprintf(brbuf, sizeof(brbuf) -1, "%s%c", brbuf, c);
|
2006-11-29 17:46:27 +03:00
|
|
|
BR = atoi(brbuf);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2006-04-02 10:11:45 +04:00
|
|
|
warnx("%s, unknown option", argv[1]);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-29 17:46:27 +03:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc != 1)
|
|
|
|
tipusage();
|
|
|
|
else
|
|
|
|
System = argv[0];
|
|
|
|
|
|
|
|
|
2004-04-24 02:11:44 +04:00
|
|
|
if (System == NULL)
|
1993-03-21 12:45:37 +03:00
|
|
|
goto notnumber;
|
2004-04-24 02:11:44 +04:00
|
|
|
if (isalpha((unsigned char)*System))
|
1993-03-21 12:45:37 +03:00
|
|
|
goto notnumber;
|
|
|
|
/*
|
|
|
|
* System name is really a phone number...
|
|
|
|
* Copy the number then stomp on the original (in case the number
|
|
|
|
* is private, we don't want 'ps' or 'w' to find it).
|
|
|
|
*/
|
2004-04-24 02:11:44 +04:00
|
|
|
if (strlen(System) > sizeof PNbuf - 1) {
|
2006-04-02 10:11:45 +04:00
|
|
|
errx(1, "phone number too long (max = %d bytes)",
|
1997-11-23 07:03:04 +03:00
|
|
|
(int)sizeof(PNbuf) - 1);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2004-09-07 17:20:39 +04:00
|
|
|
(void)strlcpy(PNbuf, System, sizeof(PNbuf));
|
2004-04-24 02:11:44 +04:00
|
|
|
for (p = System; *p; p++)
|
1993-03-21 12:45:37 +03:00
|
|
|
*p = '\0';
|
|
|
|
PN = PNbuf;
|
1997-11-22 10:28:39 +03:00
|
|
|
(void)snprintf(sbuf, sizeof sbuf, "tip%d", (int)BR);
|
2004-04-24 02:11:44 +04:00
|
|
|
System = sbuf;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
notnumber:
|
|
|
|
(void)signal(SIGINT, cleanup);
|
|
|
|
(void)signal(SIGQUIT, cleanup);
|
|
|
|
(void)signal(SIGHUP, cleanup);
|
|
|
|
(void)signal(SIGTERM, cleanup);
|
|
|
|
|
2004-04-24 02:11:44 +04:00
|
|
|
if ((i = hunt(System)) == 0) {
|
2006-12-14 20:09:43 +03:00
|
|
|
(void)printf("all ports busy\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(3);
|
|
|
|
}
|
|
|
|
if (i == -1) {
|
2006-04-03 06:01:28 +04:00
|
|
|
errx(3, "link down\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
setbuf(stdout, NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Kludge, their's no easy way to get the initialization
|
|
|
|
* in the right order, so force it here
|
|
|
|
*/
|
1997-11-22 10:28:39 +03:00
|
|
|
if ((PH = getenv("PHONES")) == NULL)
|
2004-04-24 02:24:34 +04:00
|
|
|
PH = path_phones;
|
1993-03-21 12:45:37 +03:00
|
|
|
vinit(); /* init variables */
|
2006-04-02 08:28:38 +04:00
|
|
|
setparity("none"); /* set the parity table */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Hardwired connections require the
|
|
|
|
* line speed set before they make any transmissions
|
|
|
|
* (this is particularly true of things like a DF03-AC)
|
|
|
|
*/
|
2006-04-02 23:04:24 +04:00
|
|
|
if (HW) {
|
2006-04-03 09:08:40 +04:00
|
|
|
if (ttysetup((speed_t)number(value(BAUDRATE))) != 0) {
|
2006-04-03 06:01:28 +04:00
|
|
|
errx(3, "bad baud rate %d",
|
2006-04-02 23:04:24 +04:00
|
|
|
(int)number(value(BAUDRATE)));
|
|
|
|
}
|
|
|
|
}
|
2006-12-14 20:09:43 +03:00
|
|
|
if ((q = tip_connect()) != NULL) {
|
2006-04-03 06:01:28 +04:00
|
|
|
errx(1, "\07%s\n[EOT]\n", q);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2006-04-02 23:04:24 +04:00
|
|
|
if (!HW) {
|
2006-04-03 09:08:40 +04:00
|
|
|
if (ttysetup((speed_t)number(value(BAUDRATE))) != 0) {
|
2006-04-03 06:01:28 +04:00
|
|
|
errx(3, "bad baud rate %d",
|
2006-04-02 23:04:24 +04:00
|
|
|
(int)number(value(BAUDRATE)));
|
|
|
|
}
|
|
|
|
}
|
2006-04-03 04:51:13 +04:00
|
|
|
|
1997-12-17 19:56:30 +03:00
|
|
|
|
2006-04-03 18:54:16 +04:00
|
|
|
cucommon:
|
|
|
|
/*
|
|
|
|
* From here down the code is shared with
|
|
|
|
* the "cu" version of tip.
|
|
|
|
*/
|
|
|
|
|
1997-12-17 20:09:33 +03:00
|
|
|
/*
|
|
|
|
* Direct connections with no carrier require using O_NONBLOCK on
|
|
|
|
* open, but we don't want to keep O_NONBLOCK after open because it
|
|
|
|
* will cause busy waits.
|
|
|
|
*/
|
|
|
|
if (DC &&
|
|
|
|
((fcarg = fcntl(FD, F_GETFL, 0)) < 0 ||
|
|
|
|
fcntl(FD, F_SETFL, fcarg & ~O_NONBLOCK) < 0)) {
|
2006-04-03 06:01:28 +04:00
|
|
|
err(1, "can't clear O_NONBLOCK");
|
1997-12-17 19:56:30 +03:00
|
|
|
}
|
2006-04-03 04:51:13 +04:00
|
|
|
|
2006-12-14 20:09:43 +03:00
|
|
|
(void)tcgetattr(0, &defterm);
|
1995-10-29 03:49:38 +03:00
|
|
|
term = defterm;
|
|
|
|
term.c_lflag &= ~(ICANON|IEXTEN|ECHO);
|
|
|
|
term.c_iflag &= ~(INPCK|ICRNL);
|
|
|
|
term.c_oflag &= ~OPOST;
|
|
|
|
term.c_cc[VMIN] = 1;
|
|
|
|
term.c_cc[VTIME] = 0;
|
|
|
|
defchars = term;
|
|
|
|
term.c_cc[VINTR] = term.c_cc[VQUIT] = term.c_cc[VSUSP] =
|
2006-04-03 04:51:13 +04:00
|
|
|
term.c_cc[VDSUSP] = term.c_cc[VDISCARD] =
|
1995-10-29 03:49:38 +03:00
|
|
|
term.c_cc[VLNEXT] = _POSIX_VDISABLE;
|
1993-03-21 12:45:37 +03:00
|
|
|
raw();
|
|
|
|
|
2006-12-14 20:09:43 +03:00
|
|
|
(void)pipe(attndes);
|
|
|
|
(void)pipe(fildes);
|
|
|
|
(void)pipe(repdes);
|
1997-11-22 10:28:39 +03:00
|
|
|
(void)signal(SIGALRM, alrmtimeout);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Everything's set up now:
|
|
|
|
* connection established (hardwired or dialup)
|
|
|
|
* line conditioned (baud rate, mode, etc.)
|
|
|
|
* internal data structures (variables)
|
|
|
|
* so, fork one process for local side and one for remote.
|
|
|
|
*/
|
2006-12-14 20:09:43 +03:00
|
|
|
(void)printf("%s", cumode ? "Connected\r\n" : "\07connected\r\n");
|
1997-11-22 10:28:39 +03:00
|
|
|
switch (pid = fork()) {
|
|
|
|
default:
|
1993-03-21 12:45:37 +03:00
|
|
|
tipin();
|
1997-11-22 10:28:39 +03:00
|
|
|
break;
|
|
|
|
case 0:
|
1993-03-21 12:45:37 +03:00
|
|
|
tipout();
|
1997-11-22 10:28:39 +03:00
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
err(1, "can't fork");
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
/*NOTREACHED*/
|
1997-11-22 10:28:39 +03:00
|
|
|
exit(0); /* XXX: pacify gcc */
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
2006-11-29 17:46:27 +03:00
|
|
|
void
|
|
|
|
tipusage(void)
|
|
|
|
{
|
2006-12-14 20:09:43 +03:00
|
|
|
(void)fprintf(stderr, "usage: %s [-v] [-speed] system-name\n",
|
2006-11-29 17:46:27 +03:00
|
|
|
getprogname());
|
2006-12-14 20:09:43 +03:00
|
|
|
(void)fprintf(stderr, " %s [-v] [-speed] phone-number\n",
|
2006-11-29 17:46:27 +03:00
|
|
|
getprogname());
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
void
|
2006-04-03 08:53:58 +04:00
|
|
|
/*ARGSUSED*/
|
2006-12-14 20:09:43 +03:00
|
|
|
cleanup(int dummy __unused)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
if (odisc)
|
2006-12-14 20:09:43 +03:00
|
|
|
(void)ioctl(0, TIOCSETD, &odisc);
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* put the controlling keyboard into raw mode
|
|
|
|
*/
|
1997-11-22 10:28:39 +03:00
|
|
|
void
|
2006-04-03 04:51:13 +04:00
|
|
|
raw(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1998-07-12 13:59:29 +04:00
|
|
|
|
2006-12-14 20:09:43 +03:00
|
|
|
(void)tcsetattr(0, TCSADRAIN, &term);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* return keyboard to normal mode
|
|
|
|
*/
|
1997-11-22 10:28:39 +03:00
|
|
|
void
|
2006-04-03 04:51:13 +04:00
|
|
|
unraw(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1998-07-12 13:59:29 +04:00
|
|
|
|
2006-12-14 20:09:43 +03:00
|
|
|
(void)tcsetattr(0, TCSADRAIN, &defterm);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static jmp_buf promptbuf;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print string ``s'', then read a string
|
|
|
|
* in from the terminal. Handles signals & allows use of
|
|
|
|
* normal erase and kill characters.
|
|
|
|
*/
|
1997-11-22 10:28:39 +03:00
|
|
|
int
|
2006-12-14 17:18:03 +03:00
|
|
|
prompt(const char *s, char *volatile p, size_t l)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-11-22 10:28:39 +03:00
|
|
|
int c;
|
|
|
|
char *b = p;
|
1993-03-21 12:45:37 +03:00
|
|
|
sig_t oint, oquit;
|
|
|
|
|
|
|
|
stoprompt = 0;
|
|
|
|
oint = signal(SIGINT, intprompt);
|
|
|
|
oquit = signal(SIGQUIT, SIG_IGN);
|
|
|
|
unraw();
|
2006-12-14 20:09:43 +03:00
|
|
|
(void)printf("%s", s);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (setjmp(promptbuf) == 0)
|
1998-07-12 13:59:29 +04:00
|
|
|
while ((c = getchar()) != -1 && (*p = c) != '\n' &&
|
|
|
|
b + l > p)
|
1993-03-21 12:45:37 +03:00
|
|
|
p++;
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
raw();
|
|
|
|
(void)signal(SIGINT, oint);
|
|
|
|
(void)signal(SIGQUIT, oquit);
|
|
|
|
return (stoprompt || p == b);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Interrupt service routine during prompting
|
|
|
|
*/
|
|
|
|
void
|
2006-04-03 08:53:58 +04:00
|
|
|
/*ARGSUSED*/
|
2006-12-14 20:09:43 +03:00
|
|
|
intprompt(int dummy __unused)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
(void)signal(SIGINT, SIG_IGN);
|
|
|
|
stoprompt = 1;
|
2006-12-14 20:09:43 +03:00
|
|
|
(void)printf("\r\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
longjmp(promptbuf, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ****TIPIN TIPIN****
|
|
|
|
*/
|
1997-11-22 10:28:39 +03:00
|
|
|
void
|
2006-04-03 04:51:13 +04:00
|
|
|
tipin(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
char gch, bol = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Kinda klugey here...
|
|
|
|
* check for scripting being turned on from the .tiprc file,
|
|
|
|
* but be careful about just using setscript(), as we may
|
|
|
|
* send a SIGEMT before tipout has a chance to set up catching
|
|
|
|
* it; so wait a second, then setscript()
|
|
|
|
*/
|
|
|
|
if (boolean(value(SCRIPT))) {
|
2006-12-14 20:09:43 +03:00
|
|
|
(void)sleep(1);
|
1993-03-21 12:45:37 +03:00
|
|
|
setscript();
|
|
|
|
}
|
|
|
|
|
2006-04-03 08:53:58 +04:00
|
|
|
for (;;) {
|
1995-10-29 03:49:38 +03:00
|
|
|
gch = getchar()&STRIP_PAR;
|
1993-03-21 12:45:37 +03:00
|
|
|
if ((gch == character(value(ESCAPE))) && bol) {
|
|
|
|
if (!(gch = escape()))
|
|
|
|
continue;
|
2006-04-03 04:51:13 +04:00
|
|
|
} else if (!cumode &&
|
1997-11-22 11:29:58 +03:00
|
|
|
gch && gch == character(value(RAISECHAR))) {
|
1996-12-29 13:34:03 +03:00
|
|
|
setboolean(value(RAISE), !boolean(value(RAISE)));
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
} else if (gch == '\r') {
|
|
|
|
bol = 1;
|
1998-07-01 03:42:08 +04:00
|
|
|
xpwrite(FD, &gch, 1);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (boolean(value(HALFDUPLEX)))
|
2006-12-14 20:09:43 +03:00
|
|
|
(void)printf("\r\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
1997-11-22 11:29:58 +03:00
|
|
|
} else if (!cumode && gch && gch == character(value(FORCE)))
|
1995-10-29 03:49:38 +03:00
|
|
|
gch = getchar()&STRIP_PAR;
|
1993-03-21 12:45:37 +03:00
|
|
|
bol = any(gch, value(EOL));
|
1998-12-20 02:00:18 +03:00
|
|
|
if (boolean(value(RAISE)) && islower((unsigned char)gch))
|
2004-11-04 10:29:09 +03:00
|
|
|
gch = toupper((unsigned char)gch);
|
1998-07-01 03:42:08 +04:00
|
|
|
xpwrite(FD, &gch, 1);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (boolean(value(HALFDUPLEX)))
|
2006-12-14 20:09:43 +03:00
|
|
|
(void)printf("%c", gch);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Escape handler --
|
|
|
|
* called on recognition of ``escapec'' at the beginning of a line
|
|
|
|
*/
|
1997-11-22 10:28:39 +03:00
|
|
|
int
|
2006-04-03 04:51:13 +04:00
|
|
|
escape(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-11-22 10:28:39 +03:00
|
|
|
char gch;
|
|
|
|
esctable_t *p;
|
1993-03-21 12:45:37 +03:00
|
|
|
char c = character(value(ESCAPE));
|
|
|
|
|
1995-10-29 03:49:38 +03:00
|
|
|
gch = (getchar()&STRIP_PAR);
|
1993-03-21 12:45:37 +03:00
|
|
|
for (p = etable; p->e_char; p++)
|
|
|
|
if (p->e_char == gch) {
|
|
|
|
if ((p->e_flags&PRIV) && uid)
|
|
|
|
continue;
|
2006-12-14 20:09:43 +03:00
|
|
|
(void)printf("%s", ctrl(c));
|
1993-03-21 12:45:37 +03:00
|
|
|
(*p->e_func)(gch);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
/* ESCAPE ESCAPE forces ESCAPE */
|
|
|
|
if (c != gch)
|
1998-07-01 03:42:08 +04:00
|
|
|
xpwrite(FD, &c, 1);
|
1993-03-21 12:45:37 +03:00
|
|
|
return (gch);
|
|
|
|
}
|
|
|
|
|
1997-11-22 10:28:39 +03:00
|
|
|
int
|
2006-04-03 04:51:13 +04:00
|
|
|
any(char c, const char *p)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1998-07-12 13:59:29 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
while (p && *p)
|
|
|
|
if (*p++ == c)
|
|
|
|
return (1);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2006-04-03 04:51:13 +04:00
|
|
|
interp(const char *s)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
static char buf[256];
|
2004-04-24 02:11:44 +04:00
|
|
|
char *p = buf, c;
|
|
|
|
const char *q;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1998-07-12 13:59:29 +04:00
|
|
|
while ((c = *s++) != 0 && buf + sizeof buf - p > 2) {
|
1993-03-21 12:45:37 +03:00
|
|
|
for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++)
|
|
|
|
if (*q++ == c) {
|
|
|
|
*p++ = '\\'; *p++ = *q;
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
if (c < 040) {
|
|
|
|
*p++ = '^'; *p++ = c + 'A'-1;
|
|
|
|
} else if (c == 0177) {
|
|
|
|
*p++ = '^'; *p++ = '?';
|
|
|
|
} else
|
|
|
|
*p++ = c;
|
|
|
|
next:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
return (buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2006-04-03 04:51:13 +04:00
|
|
|
ctrl(char c)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
static char s[3];
|
|
|
|
|
|
|
|
if (c < 040 || c == 0177) {
|
|
|
|
s[0] = '^';
|
|
|
|
s[1] = c == 0177 ? '?' : c+'A'-1;
|
|
|
|
s[2] = '\0';
|
|
|
|
} else {
|
|
|
|
s[0] = c;
|
|
|
|
s[1] = '\0';
|
|
|
|
}
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Help command
|
|
|
|
*/
|
1997-11-22 10:28:39 +03:00
|
|
|
void
|
2006-04-03 04:51:13 +04:00
|
|
|
help(char c)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-11-22 10:28:39 +03:00
|
|
|
esctable_t *p;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2006-12-14 20:09:43 +03:00
|
|
|
(void)printf("%c\r\n", c);
|
1993-03-21 12:45:37 +03:00
|
|
|
for (p = etable; p->e_char; p++) {
|
|
|
|
if ((p->e_flags&PRIV) && uid)
|
|
|
|
continue;
|
2006-12-14 20:09:43 +03:00
|
|
|
(void)printf("%2s", ctrl(character(value(ESCAPE))));
|
|
|
|
(void)printf("%-2s %c %s\r\n", ctrl(p->e_char),
|
1993-03-21 12:45:37 +03:00
|
|
|
p->e_flags&EXP ? '*': ' ', p->e_help);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up the "remote" tty's state
|
|
|
|
*/
|
2006-04-02 23:04:24 +04:00
|
|
|
int
|
2006-04-03 08:53:58 +04:00
|
|
|
ttysetup(speed_t spd)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1995-10-29 03:49:38 +03:00
|
|
|
struct termios cntrl;
|
|
|
|
|
2006-12-14 20:09:43 +03:00
|
|
|
(void)tcgetattr(FD, &cntrl);
|
|
|
|
(void)cfsetospeed(&cntrl, spd);
|
|
|
|
(void)cfsetispeed(&cntrl, spd);
|
1995-10-29 03:49:38 +03:00
|
|
|
cntrl.c_cflag &= ~(CSIZE|PARENB);
|
|
|
|
cntrl.c_cflag |= CS8;
|
1997-05-14 04:20:01 +04:00
|
|
|
if (DC)
|
1997-04-20 04:03:05 +04:00
|
|
|
cntrl.c_cflag |= CLOCAL;
|
2006-03-29 16:37:59 +04:00
|
|
|
if (boolean(value(HARDWAREFLOW)))
|
2006-04-03 04:51:13 +04:00
|
|
|
cntrl.c_cflag |= CRTSCTS;
|
1995-10-29 03:49:38 +03:00
|
|
|
cntrl.c_iflag &= ~(ISTRIP|ICRNL);
|
|
|
|
cntrl.c_oflag &= ~OPOST;
|
|
|
|
cntrl.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
|
|
|
|
cntrl.c_cc[VMIN] = 1;
|
|
|
|
cntrl.c_cc[VTIME] = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (boolean(value(TAND)))
|
1995-10-29 03:49:38 +03:00
|
|
|
cntrl.c_iflag |= IXOFF;
|
2006-12-14 20:09:43 +03:00
|
|
|
return tcsetattr(FD, TCSAFLUSH, &cntrl);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static char partab[0200];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do a write to the remote machine with the correct parity.
|
|
|
|
* We are doing 8 bit wide output, so we just generate a character
|
|
|
|
* with the right parity and output it.
|
|
|
|
*/
|
1997-11-22 10:28:39 +03:00
|
|
|
void
|
2006-04-03 08:25:30 +04:00
|
|
|
xpwrite(int fd, char *buf, size_t n)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-11-22 10:28:39 +03:00
|
|
|
int i;
|
|
|
|
char *bp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
bp = buf;
|
1994-12-08 12:30:36 +03:00
|
|
|
if (bits8 == 0)
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
*bp = partab[(*bp) & 0177];
|
|
|
|
bp++;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1994-12-08 12:30:36 +03:00
|
|
|
if (write(fd, buf, n) < 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (errno == EIO)
|
|
|
|
tipabort("Lost carrier.");
|
|
|
|
/* this is questionable */
|
2006-04-02 10:11:45 +04:00
|
|
|
warn("write");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Build a parity table with appropriate high-order bit.
|
|
|
|
*/
|
1997-11-22 10:28:39 +03:00
|
|
|
void
|
2006-04-03 04:51:13 +04:00
|
|
|
setparity(const char *defparity)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-11-22 10:28:39 +03:00
|
|
|
int i, flip, clr, set;
|
2004-04-24 02:11:44 +04:00
|
|
|
const char *parity;
|
2004-04-24 02:24:34 +04:00
|
|
|
static char *curpar;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2006-04-03 08:53:58 +04:00
|
|
|
if (value(PARITY) == NULL || ((char *)value(PARITY))[0] == '\0') {
|
2004-04-24 02:24:34 +04:00
|
|
|
if (curpar != NULL)
|
|
|
|
free(curpar);
|
|
|
|
value(PARITY) = curpar = strdup(defparity);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
parity = value(PARITY);
|
|
|
|
if (equal(parity, "none")) {
|
|
|
|
bits8 = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bits8 = 0;
|
|
|
|
flip = 0;
|
|
|
|
clr = 0377;
|
|
|
|
set = 0;
|
|
|
|
if (equal(parity, "odd"))
|
|
|
|
flip = 0200; /* reverse bit 7 */
|
|
|
|
else if (equal(parity, "zero"))
|
|
|
|
clr = 0177; /* turn off bit 7 */
|
|
|
|
else if (equal(parity, "one"))
|
|
|
|
set = 0200; /* turn on bit 7 */
|
|
|
|
else if (!equal(parity, "even")) {
|
2006-12-14 20:09:43 +03:00
|
|
|
(void)fprintf(stderr, "%s: unknown parity value\r\n", parity);
|
|
|
|
(void)fflush(stderr);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
for (i = 0; i < 0200; i++)
|
1997-11-22 10:28:39 +03:00
|
|
|
partab[i] = ((evenpartab[i] ^ flip) | set) & clr;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|