NetBSD/usr.bin/tip/cmds.c

930 lines
19 KiB
C
Raw Normal View History

2014-07-12 09:28:07 +04:00
/* $NetBSD: cmds.c,v 1.37 2014/07/12 05:28:07 mlelstv Exp $ */
1993-03-21 12:45:37 +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.
* 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.
*/
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
#if 0
static char sccsid[] = "@(#)cmds.c 8.1 (Berkeley) 6/6/93";
#endif
2014-07-12 09:28:07 +04:00
__RCSID("$NetBSD: cmds.c,v 1.37 2014/07/12 05:28:07 mlelstv Exp $");
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#include "tip.h"
#include "pathnames.h"
/*
* tip
*
* miscellaneous commands
*/
int quant[] = { 60, 60, 24 };
char null = '\0';
2004-04-24 02:11:44 +04:00
const char *sep[] = { "second", "minute", "hour" };
static char *argv[10]; /* argument vector for take and put */
1993-03-21 12:45:37 +03:00
int args(char *, char **);
int anyof(char *, const char *);
void execute(char *);
__dead static void intcopy(int);
void prtime(const char *, time_t);
void stopsnd(int);
void transfer(char *, int, const char *);
void transmit(FILE *, const char *, char *);
1993-03-21 12:45:37 +03:00
/*
* FTP - remote ==> local
* get a file from the remote host
*/
void
getfl(char c)
1993-03-21 12:45:37 +03:00
{
char buf[256], *cp;
2006-12-14 20:09:43 +03:00
(void)putchar(c);
1993-03-21 12:45:37 +03:00
/*
* get the UNIX receiving file's name
*/
if (prompt("Local file name? ", copyname, sizeof copyname))
1993-03-21 12:45:37 +03:00
return;
cp = expand(copyname);
if ((sfd = open(cp, O_RDWR|O_CREAT, 0666)) < 0) {
2006-12-14 20:09:43 +03:00
(void)printf("\r\n%s: cannot create\r\n", copyname);
1993-03-21 12:45:37 +03:00
return;
}
1993-03-21 12:45:37 +03:00
/*
* collect parameters
*/
if (prompt("List command for remote system? ", buf,
sizeof buf)) {
2006-12-14 20:09:43 +03:00
(void)unlink(copyname);
1993-03-21 12:45:37 +03:00
return;
}
transfer(buf, sfd, value(EOFREAD));
}
/*
* Cu-like take command
*/
/* ARGSUSED */
void
2006-12-14 20:09:43 +03:00
cu_take(char dummy __unused)
1993-03-21 12:45:37 +03:00
{
int fd, argc;
char line[BUFSIZ], *cp;
1993-03-21 12:45:37 +03:00
if (prompt("[take] ", copyname, sizeof copyname))
1993-03-21 12:45:37 +03:00
return;
if ((argc = args(copyname, argv)) < 1 || argc > 2) {
2006-12-14 20:09:43 +03:00
(void)printf("usage: <take> from [to]\r\n");
1993-03-21 12:45:37 +03:00
return;
}
if (argc == 1)
argv[1] = argv[0];
cp = expand(argv[1]);
if ((fd = open(cp, O_RDWR|O_CREAT, 0666)) < 0) {
2006-12-14 20:09:43 +03:00
(void)printf("\r\n%s: cannot create\r\n", argv[1]);
1993-03-21 12:45:37 +03:00
return;
}
1997-02-11 12:24:03 +03:00
(void)snprintf(line, sizeof line, "cat %s;echo \01", argv[0]);
1993-03-21 12:45:37 +03:00
transfer(line, fd, "\01");
}
static jmp_buf intbuf;
/*
* Bulk transfer routine --
* used by getfl(), cu_take(), and pipefile()
*/
void
transfer(char *buf, int fd, const char *eofchars)
1993-03-21 12:45:37 +03:00
{
int ct;
1993-03-21 12:45:37 +03:00
char c, buffer[BUFSIZ];
2006-12-14 17:18:03 +03:00
char * volatile p;
int cnt, eof;
1993-03-21 12:45:37 +03:00
time_t start;
sig_t f;
char r;
1993-03-21 12:45:37 +03:00
2006-12-14 17:18:03 +03:00
p = buffer;
xpwrite(FD, buf, strlen(buf));
1993-03-21 12:45:37 +03:00
quit = 0;
2006-12-14 20:09:43 +03:00
(void)write(attndes[1], "W", 1); /* Put TIPOUT into a wait state */
(void)read(repdes[0], (char *)&ccc, 1); /* Wait until read process stops */
1993-03-21 12:45:37 +03:00
/*
* finish command
*/
r = '\r';
xpwrite(FD, &r, 1);
1993-03-21 12:45:37 +03:00
do
2006-12-14 20:09:43 +03:00
(void)read(FD, &c, 1);
while ((c&STRIP_PAR) != '\n');
2006-12-14 20:09:43 +03:00
(void)tcsetattr(0, TCSAFLUSH, &defchars);
1993-03-21 12:45:37 +03:00
(void) setjmp(intbuf);
f = signal(SIGINT, intcopy);
start = time(0);
for (ct = 0; !quit;) {
eof = read(FD, &c, 1) <= 0;
c &= STRIP_PAR;
1993-03-21 12:45:37 +03:00
if (quit)
continue;
if (eof || any(c, eofchars))
break;
if (c == 0)
continue; /* ignore nulls */
if (c == '\r')
continue;
*p++ = c;
if (c == '\n' && boolean(value(VERBOSE)))
2006-12-14 20:09:43 +03:00
(void)printf("\r%d", ++ct);
1993-03-21 12:45:37 +03:00
if ((cnt = (p-buffer)) == number(value(FRAMESIZE))) {
2006-04-03 08:53:58 +04:00
if (write(fd, buffer, (size_t)cnt) != cnt) {
2006-12-14 20:09:43 +03:00
(void)printf("\r\nwrite error\r\n");
1993-03-21 12:45:37 +03:00
quit = 1;
}
p = buffer;
}
}
if ((cnt = (p-buffer)) != 0)
2006-04-03 08:53:58 +04:00
if (write(fd, buffer, (size_t)cnt) != cnt)
2006-12-14 20:09:43 +03:00
(void)printf("\r\nwrite error\r\n");
1993-03-21 12:45:37 +03:00
if (boolean(value(VERBOSE)))
prtime(" lines transferred in ", time(0)-start);
2006-04-03 08:53:58 +04:00
(void)tcsetattr(0, TCSAFLUSH, &term);
(void)write(fildes[1], (char *)&ccc, 1);
(void)signal(SIGINT, f);
(void)close(fd);
1993-03-21 12:45:37 +03:00
}
/*
* FTP - remote ==> local process
* send remote input to local process via pipe
*/
/* ARGSUSED */
void
2006-12-14 20:09:43 +03:00
pipefile(char dummy __unused)
1993-03-21 12:45:37 +03:00
{
int cpid, pdes[2];
char buf[256];
int status, p;
if (prompt("Local command? ", buf, sizeof buf))
1993-03-21 12:45:37 +03:00
return;
if (pipe(pdes)) {
2006-12-14 20:09:43 +03:00
(void)printf("can't establish pipe\r\n");
1993-03-21 12:45:37 +03:00
return;
}
if ((cpid = fork()) < 0) {
2006-12-14 20:09:43 +03:00
(void)printf("can't fork!\r\n");
1993-03-21 12:45:37 +03:00
return;
} else if (cpid) {
if (prompt("List command for remote system? ", buf,
sizeof buf)) {
2006-12-14 20:09:43 +03:00
(void)close(pdes[0]);
(void)close(pdes[1]);
(void)kill(cpid, SIGKILL);
1993-03-21 12:45:37 +03:00
} else {
2006-12-14 20:09:43 +03:00
(void)close(pdes[0]);
(void)signal(SIGPIPE, intcopy);
1993-03-21 12:45:37 +03:00
transfer(buf, pdes[1], value(EOFREAD));
2006-12-14 20:09:43 +03:00
(void)signal(SIGPIPE, SIG_DFL);
1993-03-21 12:45:37 +03:00
while ((p = wait(&status)) > 0 && p != cpid)
;
}
} else {
2006-12-14 20:09:43 +03:00
(void)dup2(pdes[0], 0);
(void)close(pdes[0]);
(void)closefrom(3);
1993-03-21 12:45:37 +03:00
execute(buf);
2006-12-14 20:09:43 +03:00
(void)printf("can't execl!\r\n");
1993-03-21 12:45:37 +03:00
exit(0);
}
}
/*
* Interrupt service routine for FTP
*/
/* ARGSUSED */
1993-03-21 12:45:37 +03:00
void
2006-12-14 20:09:43 +03:00
stopsnd(int dummy __unused)
1993-03-21 12:45:37 +03:00
{
stop = 1;
2006-12-14 20:09:43 +03:00
(void)signal(SIGINT, SIG_IGN);
1993-03-21 12:45:37 +03:00
}
/*
* FTP - local ==> remote
* send local file to remote host
* terminate transmission with pseudo EOF sequence
*/
void
sendfile(char cc)
1993-03-21 12:45:37 +03:00
{
FILE *fd;
char *fnamex;
2006-12-14 20:09:43 +03:00
(void)putchar(cc);
1993-03-21 12:45:37 +03:00
/*
* get file name
*/
if (prompt("Local file name? ", fname, sizeof fname))
1993-03-21 12:45:37 +03:00
return;
/*
* look up file
*/
fnamex = expand(fname);
if ((fd = fopen(fnamex, "r")) == NULL) {
2006-12-14 20:09:43 +03:00
(void)printf("%s: cannot open\r\n", fname);
1993-03-21 12:45:37 +03:00
return;
}
transmit(fd, value(EOFWRITE), NULL);
if (!boolean(value(ECHOCHECK)))
2006-12-14 20:09:43 +03:00
(void)tcdrain(FD);
1993-03-21 12:45:37 +03:00
}
/*
* Bulk transfer routine to remote host --
* used by sendfile() and cu_put()
*/
void
transmit(FILE *fd, const char *eofchars, char *command)
1993-03-21 12:45:37 +03:00
{
2004-04-24 02:11:44 +04:00
const char *pc;
char lastc;
2006-04-03 08:53:58 +04:00
int c;
int ccount, lcount;
1993-03-21 12:45:37 +03:00
time_t start_t, stop_t;
sig_t f;
2006-12-14 20:09:43 +03:00
(void)write(attndes[1], "W", 1); /* put TIPOUT into a wait state */
1993-03-21 12:45:37 +03:00
stop = 0;
f = signal(SIGINT, stopsnd);
2006-12-14 20:09:43 +03:00
(void)tcsetattr(0, TCSAFLUSH, &defchars);
(void)read(repdes[0], (char *)&ccc, 1);
1993-03-21 12:45:37 +03:00
if (command != NULL) {
for (pc = command; *pc; pc++)
2006-04-03 08:53:58 +04:00
sendchar(*pc);
1993-03-21 12:45:37 +03:00
if (boolean(value(ECHOCHECK)))
2006-04-03 08:53:58 +04:00
(void)read(FD, &c, (size_t)1); /* trailing \n */
1993-03-21 12:45:37 +03:00
else {
2006-12-14 20:09:43 +03:00
(void)tcdrain(FD);
(void)sleep(5); /* wait for remote stty to take effect */
1993-03-21 12:45:37 +03:00
}
}
lcount = 0;
lastc = '\0';
start_t = time(0);
/* CONSTCOND */
1993-03-21 12:45:37 +03:00
while (1) {
ccount = 0;
do {
c = getc(fd);
if (stop)
goto out;
if (c == EOF)
goto out;
if (c == 0177 && !boolean(value(RAWFTP)))
continue;
lastc = c;
if (c < 040) {
if (c == '\n') {
if (!boolean(value(RAWFTP)))
c = '\r';
}
else if (c == '\t') {
if (!boolean(value(RAWFTP))) {
if (boolean(value(TABEXPAND))) {
2006-04-03 08:53:58 +04:00
sendchar(' ');
1993-03-21 12:45:37 +03:00
while ((++ccount % 8) != 0)
2006-04-03 08:53:58 +04:00
sendchar(' ');
1993-03-21 12:45:37 +03:00
continue;
}
}
} else
if (!boolean(value(RAWFTP)))
continue;
}
2006-04-03 08:53:58 +04:00
sendchar(c);
1993-03-21 12:45:37 +03:00
} while (c != '\r' && !boolean(value(RAWFTP)));
if (boolean(value(VERBOSE)))
2006-12-14 20:09:43 +03:00
(void)printf("\r%d", ++lcount);
1993-03-21 12:45:37 +03:00
if (boolean(value(ECHOCHECK))) {
timedout = 0;
2006-12-14 20:09:43 +03:00
(void)alarm((unsigned int)number(value(ETIMEOUT)));
1993-03-21 12:45:37 +03:00
do { /* wait for prompt */
2006-12-14 20:09:43 +03:00
(void)read(FD, &c, (size_t)1);
1993-03-21 12:45:37 +03:00
if (timedout || stop) {
if (timedout)
2006-12-14 20:09:43 +03:00
(void)printf(
"\r\ntimed out at eol\r\n");
2006-12-14 20:09:43 +03:00
(void)alarm(0);
1993-03-21 12:45:37 +03:00
goto out;
}
} while ((c&STRIP_PAR) != character(value(PROMPT)));
2006-12-14 20:09:43 +03:00
(void)alarm(0);
1993-03-21 12:45:37 +03:00
}
}
out:
if (lastc != '\n' && !boolean(value(RAWFTP)))
2006-04-03 08:53:58 +04:00
sendchar('\r');
if (eofchars) {
for (pc = eofchars; *pc; pc++)
2006-04-03 08:53:58 +04:00
sendchar(*pc);
}
1993-03-21 12:45:37 +03:00
stop_t = time(0);
2006-12-14 20:09:43 +03:00
(void)fclose(fd);
(void)signal(SIGINT, f);
if (boolean(value(VERBOSE))) {
1993-03-21 12:45:37 +03:00
if (boolean(value(RAWFTP)))
prtime(" chars transferred in ", stop_t-start_t);
else
prtime(" lines transferred in ", stop_t-start_t);
}
2006-12-14 20:09:43 +03:00
(void)write(fildes[1], (char *)&ccc, 1);
(void)tcsetattr(0, TCSAFLUSH, &term);
1993-03-21 12:45:37 +03:00
}
/*
* Cu-like put command
*/
/* ARGSUSED */
void
2006-12-14 20:09:43 +03:00
cu_put(char dummy __unused)
1993-03-21 12:45:37 +03:00
{
FILE *fd;
char line[BUFSIZ];
int argc;
char *copynamex;
if (prompt("[put] ", copyname, sizeof copyname))
1993-03-21 12:45:37 +03:00
return;
if ((argc = args(copyname, argv)) < 1 || argc > 2) {
2006-12-14 20:09:43 +03:00
(void)printf("usage: <put> from [to]\r\n");
1993-03-21 12:45:37 +03:00
return;
}
if (argc == 1)
argv[1] = argv[0];
copynamex = expand(argv[0]);
if ((fd = fopen(copynamex, "r")) == NULL) {
2006-12-14 20:09:43 +03:00
(void)printf("%s: cannot open\r\n", copynamex);
1993-03-21 12:45:37 +03:00
return;
}
if (boolean(value(ECHOCHECK)))
1997-02-11 12:24:03 +03:00
(void)snprintf(line, sizeof line, "cat>%s\r", argv[1]);
1993-03-21 12:45:37 +03:00
else
1997-02-11 12:24:03 +03:00
(void)snprintf(line, sizeof line, "stty -echo;cat>%s;stty echo\r", argv[1]);
1993-03-21 12:45:37 +03:00
transmit(fd, "\04", line);
}
/*
* FTP - send single character
* wait for echo & handle timeout
*/
void
2006-04-03 08:53:58 +04:00
sendchar(char c)
1993-03-21 12:45:37 +03:00
{
char cc;
int retry = 0;
cc = c;
xpwrite(FD, &cc, 1);
1993-03-21 12:45:37 +03:00
#ifdef notdef
if (number(value(CDELAY)) > 0 && c != '\r')
nap(number(value(CDELAY)));
#endif
if (!boolean(value(ECHOCHECK))) {
#ifdef notdef
if (number(value(LDELAY)) > 0 && c == '\r')
nap(number(value(LDELAY)));
#endif
return;
}
tryagain:
timedout = 0;
2006-12-14 20:09:43 +03:00
(void)alarm((unsigned int)number(value(ETIMEOUT)));
(void)read(FD, &cc, 1);
(void)alarm(0);
1993-03-21 12:45:37 +03:00
if (timedout) {
2006-12-14 20:09:43 +03:00
(void)printf("\r\ntimeout error (%s)\r\n", ctrl(c));
1993-03-21 12:45:37 +03:00
if (retry++ > 3)
return;
xpwrite(FD, &null, 1); /* poke it */
1993-03-21 12:45:37 +03:00
goto tryagain;
}
}
/* ARGSUSED */
1993-03-21 12:45:37 +03:00
void
2006-12-14 20:09:43 +03:00
alrmtimeout(int dummy __unused)
1993-03-21 12:45:37 +03:00
{
2006-12-14 20:09:43 +03:00
(void)signal(SIGALRM, alrmtimeout);
1993-03-21 12:45:37 +03:00
timedout = 1;
}
/*
* Stolen from consh() -- puts a remote file on the output of a local command.
* Identical to consh() except for where stdout goes.
*/
void
pipeout(char c)
1993-03-21 12:45:37 +03:00
{
char buf[256];
int cpid, status, p;
time_t start = 0;
1993-03-21 12:45:37 +03:00
2006-12-14 20:09:43 +03:00
(void)putchar(c);
if (prompt("Local command? ", buf, sizeof buf))
1993-03-21 12:45:37 +03:00
return;
2006-12-14 20:09:43 +03:00
(void)write(attndes[1], "W", 1); /* put TIPOUT into a wait state */
(void)signal(SIGINT, SIG_IGN);
(void)signal(SIGQUIT, SIG_IGN);
(void)tcsetattr(0, TCSAFLUSH, &defchars);
(void)read(repdes[0], (char *)&ccc, 1);
1993-03-21 12:45:37 +03:00
/*
* Set up file descriptors in the child and
* let it go...
*/
if ((cpid = fork()) < 0)
2006-12-14 20:09:43 +03:00
(void)printf("can't fork!\r\n");
1993-03-21 12:45:37 +03:00
else if (cpid) {
start = time(0);
while ((p = wait(&status)) > 0 && p != cpid)
;
} else {
2006-12-14 20:09:43 +03:00
(void)dup2(FD, 1);
(void)closefrom(3);
(void)signal(SIGINT, SIG_DFL);
(void)signal(SIGQUIT, SIG_DFL);
1993-03-21 12:45:37 +03:00
execute(buf);
2006-12-14 20:09:43 +03:00
(void)printf("can't find `%s'\r\n", buf);
1993-03-21 12:45:37 +03:00
exit(0);
}
if (boolean(value(VERBOSE)))
prtime("away for ", time(0)-start);
2006-12-14 20:09:43 +03:00
(void)write(fildes[1], (char *)&ccc, 1);
(void)tcsetattr(0, TCSAFLUSH, &term);
(void)signal(SIGINT, SIG_DFL);
(void)signal(SIGQUIT, SIG_DFL);
1993-03-21 12:45:37 +03:00
}
/*
* Fork a program with:
* 0 <-> remote tty in
* 1 <-> remote tty out
* 2 <-> local tty out
*/
void
consh(char c)
1993-03-21 12:45:37 +03:00
{
char buf[256];
int cpid, status, p;
time_t start = 0;
1993-03-21 12:45:37 +03:00
2006-12-14 20:09:43 +03:00
(void)putchar(c);
if (prompt("Local command? ", buf, sizeof buf))
1993-03-21 12:45:37 +03:00
return;
2006-12-14 20:09:43 +03:00
(void)write(attndes[1], "W", 1); /* put TIPOUT into a wait state */
(void)signal(SIGINT, SIG_IGN);
(void)signal(SIGQUIT, SIG_IGN);
(void)tcsetattr(0, TCSAFLUSH, &defchars);
(void)read(repdes[0], (char *)&ccc, 1);
1993-03-21 12:45:37 +03:00
/*
* Set up file descriptors in the child and
* let it go...
*/
if ((cpid = fork()) < 0)
2006-12-14 20:09:43 +03:00
(void)printf("can't fork!\r\n");
1993-03-21 12:45:37 +03:00
else if (cpid) {
start = time(0);
while ((p = wait(&status)) > 0 && p != cpid)
;
} else {
2006-12-14 20:09:43 +03:00
(void)dup2(FD, 0);
(void)dup2(FD, 1);
(void)closefrom(3);
(void)signal(SIGINT, SIG_DFL);
(void)signal(SIGQUIT, SIG_DFL);
1993-03-21 12:45:37 +03:00
execute(buf);
2006-12-14 20:09:43 +03:00
(void)printf("can't find `%s'\r\n", buf);
1993-03-21 12:45:37 +03:00
exit(0);
}
if (boolean(value(VERBOSE)))
prtime("away for ", time(0)-start);
2006-12-14 20:09:43 +03:00
(void)write(fildes[1], (char *)&ccc, 1);
(void)tcsetattr(0, TCSAFLUSH, &term);
(void)signal(SIGINT, SIG_DFL);
(void)signal(SIGQUIT, SIG_DFL);
1993-03-21 12:45:37 +03:00
}
/*
* Escape to local shell
*/
/* ARGSUSED */
void
2006-12-14 20:09:43 +03:00
shell(char dummy __unused)
1993-03-21 12:45:37 +03:00
{
int shpid, status;
2004-04-24 02:11:44 +04:00
const char *cp;
1993-03-21 12:45:37 +03:00
2006-12-14 20:09:43 +03:00
(void)printf("[sh]\r\n");
(void)signal(SIGINT, SIG_IGN);
(void)signal(SIGQUIT, SIG_IGN);
1993-03-21 12:45:37 +03:00
unraw();
switch (shpid = fork()) {
default:
1993-03-21 12:45:37 +03:00
while (shpid != wait(&status));
raw();
2006-12-14 20:09:43 +03:00
(void)printf("\r\n!\r\n");
(void)signal(SIGINT, SIG_DFL);
(void)signal(SIGQUIT, SIG_DFL);
break;
case 0:
2006-12-14 20:09:43 +03:00
(void)signal(SIGQUIT, SIG_DFL);
(void)signal(SIGINT, SIG_DFL);
if ((cp = strrchr(value(SHELL), '/')) == NULL)
1993-03-21 12:45:37 +03:00
cp = value(SHELL);
else
cp++;
2006-12-14 20:09:43 +03:00
(void)execl(value(SHELL), cp, NULL);
(void)fprintf(stderr, "\r\n");
err(1, "can't execl");
/* NOTREACHED */
case -1:
2006-12-14 20:09:43 +03:00
(void)fprintf(stderr, "\r\n");
err(1, "can't fork");
/* NOTREACHED */
1993-03-21 12:45:37 +03:00
}
}
/*
* TIPIN portion of scripting
* initiate the conversation with TIPOUT
*/
void
setscript(void)
1993-03-21 12:45:37 +03:00
{
char c;
/*
* enable TIPOUT side for dialogue
*/
2006-12-14 20:09:43 +03:00
(void)write(attndes[1], "S", 1);
if (boolean(value(SCRIPT)) && strlen(value(RECORD)))
2006-12-14 20:09:43 +03:00
(void)write(fildes[1], value(RECORD), strlen(value(RECORD)));
(void)write(fildes[1], "\n", 1);
1993-03-21 12:45:37 +03:00
/*
* wait for TIPOUT to finish
*/
2006-12-14 20:09:43 +03:00
(void)read(repdes[0], &c, 1);
1993-03-21 12:45:37 +03:00
if (c == 'n')
2006-12-14 20:09:43 +03:00
(void)printf("can't create %s\r\n", (char *)value(RECORD));
1993-03-21 12:45:37 +03:00
}
/*
* Change current working directory of
* local portion of tip
*/
/* ARGSUSED */
void
2006-12-14 20:09:43 +03:00
chdirectory(char dummy __unused)
1993-03-21 12:45:37 +03:00
{
2004-04-24 02:11:44 +04:00
char dirnam[80];
const char *cp = dirnam;
1993-03-21 12:45:37 +03:00
2004-04-24 02:11:44 +04:00
if (prompt("[cd] ", dirnam, sizeof dirnam)) {
1993-03-21 12:45:37 +03:00
if (stoprompt)
return;
cp = value(HOME);
}
if (chdir(cp) < 0)
2006-12-14 20:09:43 +03:00
(void)printf("%s: bad directory\r\n", cp);
(void)printf("!\r\n");
1993-03-21 12:45:37 +03:00
}
void
tipabort(const char *msg)
1993-03-21 12:45:37 +03:00
{
2006-12-14 20:09:43 +03:00
(void)kill(pid, SIGTERM);
1993-03-21 12:45:37 +03:00
disconnect(msg);
if (msg != NULL)
2006-12-14 20:09:43 +03:00
(void)printf("\r\n%s", msg);
(void)printf("\r\n[EOT]\r\n");
1993-03-21 12:45:37 +03:00
unraw();
exit(0);
}
/* ARGSUSED */
void
2006-12-14 20:09:43 +03:00
finish(char dummy __unused)
1993-03-21 12:45:37 +03:00
{
2004-04-24 02:11:44 +04:00
const char *dismsg;
1993-03-21 12:45:37 +03:00
dismsg = value(DISCONNECT);
if (dismsg != NULL && dismsg[0] != '\0') {
2006-12-14 20:09:43 +03:00
(void)write(FD, dismsg, strlen(dismsg));
(void)sleep(5);
1993-03-21 12:45:37 +03:00
}
tipabort(NULL);
1993-03-21 12:45:37 +03:00
}
/* ARGSUSED */
1993-03-21 12:45:37 +03:00
void
2006-12-14 20:09:43 +03:00
intcopy(int dummy __unused)
1993-03-21 12:45:37 +03:00
{
1993-03-21 12:45:37 +03:00
raw();
quit = 1;
longjmp(intbuf, 1);
}
void
execute(char *s)
1993-03-21 12:45:37 +03:00
{
2004-04-24 02:11:44 +04:00
const char *cp;
1993-03-21 12:45:37 +03:00
if ((cp = strrchr(value(SHELL), '/')) == NULL)
1993-03-21 12:45:37 +03:00
cp = value(SHELL);
else
cp++;
2006-12-14 20:09:43 +03:00
(void)execl(value(SHELL), cp, "-c", s, NULL);
1993-03-21 12:45:37 +03:00
}
int
args(char *buf, char *a[])
1993-03-21 12:45:37 +03:00
{
char *p = buf, *start;
char **parg = a;
int n = 0;
1993-03-21 12:45:37 +03:00
do {
while (*p && (*p == ' ' || *p == '\t'))
p++;
start = p;
if (*p)
*parg = p;
while (*p && (*p != ' ' && *p != '\t'))
p++;
if (p != start)
parg++, n++;
if (*p)
*p++ = '\0';
} while (*p);
return(n);
}
void
prtime(const char *s, time_t a)
1993-03-21 12:45:37 +03:00
{
int i;
1993-03-21 12:45:37 +03:00
int nums[3];
for (i = 0; i < 3; i++) {
nums[i] = (int)(a % quant[i]);
a /= quant[i];
}
2006-12-14 20:09:43 +03:00
(void)printf("%s", s);
1993-03-21 12:45:37 +03:00
while (--i >= 0)
if (nums[i] || (i == 0 && nums[1] == 0 && nums[2] == 0))
2006-12-14 20:09:43 +03:00
(void)printf("%d %s%c ", nums[i], sep[i],
1993-03-21 12:45:37 +03:00
nums[i] == 1 ? '\0' : 's');
2006-12-14 20:09:43 +03:00
(void)printf("\r\n!\r\n");
1993-03-21 12:45:37 +03:00
}
/* ARGSUSED */
void
2006-12-14 20:09:43 +03:00
variable(char dummy __unused)
1993-03-21 12:45:37 +03:00
{
char buf[256];
if (prompt("[set] ", buf, sizeof buf))
1993-03-21 12:45:37 +03:00
return;
vlex(buf);
if (vtable[BEAUTIFY].v_access&CHANGED) {
vtable[BEAUTIFY].v_access &= ~CHANGED;
2006-12-14 20:09:43 +03:00
(void)write(attndes[1], "B", 1); /* Tell TIPOUT to toggle */
1993-03-21 12:45:37 +03:00
}
if (vtable[SCRIPT].v_access&CHANGED) {
vtable[SCRIPT].v_access &= ~CHANGED;
setscript();
/*
* So that "set record=blah script" doesn't
* cause two transactions to occur.
*/
if (vtable[RECORD].v_access&CHANGED)
vtable[RECORD].v_access &= ~CHANGED;
}
if (vtable[RECORD].v_access&CHANGED) {
vtable[RECORD].v_access &= ~CHANGED;
if (boolean(value(SCRIPT)))
setscript();
}
if (vtable[TAND].v_access&CHANGED) {
vtable[TAND].v_access &= ~CHANGED;
if (boolean(value(TAND)))
tandem("on");
else
tandem("off");
}
if (vtable[LECHO].v_access&CHANGED) {
vtable[LECHO].v_access &= ~CHANGED;
HD = boolean(value(LECHO));
}
if (vtable[PARITY].v_access&CHANGED) {
vtable[PARITY].v_access &= ~CHANGED;
setparity(NULL); /* XXX what is the correct arg? */
1993-03-21 12:45:37 +03:00
}
if (vtable[HARDWAREFLOW].v_access&CHANGED) {
vtable[HARDWAREFLOW].v_access &= ~CHANGED;
if (boolean(value(HARDWAREFLOW)))
hardwareflow("on");
else
hardwareflow("off");
}
1993-03-21 12:45:37 +03:00
}
/*
* Turn tandem mode on or off for remote tty.
*/
void
tandem(const char *option)
1993-03-21 12:45:37 +03:00
{
struct termios rmtty;
1993-03-21 12:45:37 +03:00
2006-12-14 20:09:43 +03:00
(void)tcgetattr(FD, &rmtty);
if (strcmp(option, "on") == 0) {
2014-07-12 09:28:07 +04:00
rmtty.c_iflag |= IXON|IXOFF;
term.c_iflag |= IXON|IXOFF;
1993-03-21 12:45:37 +03:00
} else {
2014-07-12 09:28:07 +04:00
rmtty.c_iflag &= ~(IXON|IXOFF);
term.c_iflag &= ~(IXON|IXOFF);
1993-03-21 12:45:37 +03:00
}
2006-12-14 20:09:43 +03:00
(void)tcsetattr(FD, TCSADRAIN, &rmtty);
(void)tcsetattr(0, TCSADRAIN, &term);
1993-03-21 12:45:37 +03:00
}
/*
* Turn hardware flow control on or off for remote tty.
*/
void
hardwareflow(const char *option)
{
struct termios rmtty;
2006-12-14 20:09:43 +03:00
(void)tcgetattr(FD, &rmtty);
if (strcmp(option, "on") == 0)
rmtty.c_cflag |= CRTSCTS;
else
rmtty.c_cflag &= ~CRTSCTS;
2006-12-14 20:09:43 +03:00
(void)tcsetattr(FD, TCSADRAIN, &rmtty);
}
1993-03-21 12:45:37 +03:00
/*
* Send a break.
*/
/* ARGSUSED */
void
2006-12-14 20:09:43 +03:00
genbrk(char dummy __unused)
1993-03-21 12:45:37 +03:00
{
2006-12-14 20:09:43 +03:00
(void)ioctl(FD, TIOCSBRK, NULL);
(void)sleep(1);
(void)ioctl(FD, TIOCCBRK, NULL);
1993-03-21 12:45:37 +03:00
}
/*
* Suspend tip
*/
void
suspend(char c)
1993-03-21 12:45:37 +03:00
{
unraw();
2006-12-14 20:09:43 +03:00
(void)kill(c == CTRL('y') ? getpid() : 0, SIGTSTP);
1993-03-21 12:45:37 +03:00
raw();
}
/*
* expand a file name if it includes shell meta characters
*/
char *
2006-10-22 20:45:35 +04:00
expand(char aname[])
1993-03-21 12:45:37 +03:00
{
static char xname[BUFSIZ];
2006-12-14 17:18:03 +03:00
char * volatile name;
1993-03-21 12:45:37 +03:00
char cmdbuf[BUFSIZ];
2004-04-24 02:11:44 +04:00
int mypid, l;
char *cp;
const char *Shell;
int s, pivec[2];
1993-03-21 12:45:37 +03:00
2006-12-14 17:18:03 +03:00
name = aname;
1993-03-21 12:45:37 +03:00
if (!anyof(name, "~{[*?$`'\"\\"))
return(name);
if (pipe(pivec) < 0) {
warn("pipe");
1993-03-21 12:45:37 +03:00
return(name);
}
1997-02-11 12:24:03 +03:00
(void)snprintf(cmdbuf, sizeof cmdbuf, "echo %s", name);
2004-04-24 02:11:44 +04:00
if ((mypid = vfork()) == 0) {
1993-03-21 12:45:37 +03:00
Shell = value(SHELL);
if (Shell == NULL)
1993-03-21 12:45:37 +03:00
Shell = _PATH_BSHELL;
2006-12-14 20:09:43 +03:00
(void)close(pivec[0]);
(void)close(1);
(void)dup(pivec[1]);
(void)close(pivec[1]);
(void)close(2);
(void)execl(Shell, Shell, "-c", cmdbuf, NULL);
1993-03-21 12:45:37 +03:00
_exit(1);
}
2004-04-24 02:11:44 +04:00
if (mypid == -1) {
warn("fork");
2006-12-14 20:09:43 +03:00
(void)close(pivec[0]);
(void)close(pivec[1]);
return(NULL);
1993-03-21 12:45:37 +03:00
}
2006-12-14 20:09:43 +03:00
(void)close(pivec[1]);
1993-03-21 12:45:37 +03:00
l = read(pivec[0], xname, BUFSIZ);
2006-12-14 20:09:43 +03:00
(void)close(pivec[0]);
while (wait(&s) != mypid)
continue;
1993-03-21 12:45:37 +03:00
s &= 0377;
if (s != 0 && s != SIGPIPE) {
2006-12-14 20:09:43 +03:00
(void)fprintf(stderr, "\"Echo\" failed\n");
return(NULL);
1993-03-21 12:45:37 +03:00
}
if (l < 0) {
warn("read");
return(NULL);
1993-03-21 12:45:37 +03:00
}
if (l == 0) {
2006-12-14 20:09:43 +03:00
(void)fprintf(stderr, "\"%s\": No match\n", name);
return(NULL);
1993-03-21 12:45:37 +03:00
}
if (l == BUFSIZ) {
2006-12-14 20:09:43 +03:00
(void)fprintf(stderr, "Buffer overflow expanding \"%s\"\n", name);
return(NULL);
1993-03-21 12:45:37 +03:00
}
xname[l] = 0;
for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
;
*++cp = '\0';
return(xname);
}
/*
* Are any of the characters in the two strings the same?
*/
int
anyof(char *s1, const char *s2)
1993-03-21 12:45:37 +03:00
{
int c;
1993-03-21 12:45:37 +03:00
2006-04-03 08:53:58 +04:00
while ((c = *s1++) != '\0')
1993-03-21 12:45:37 +03:00
if (any(c, s2))
return(1);
return(0);
}