1994-08-16 23:36:45 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1992, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1996-11-01 06:45:43 +00:00
|
|
|
* Copyright (c) 1992, 1993, 1994, 1995, 1996
|
|
|
|
* Keith Bostic. All rights reserved.
|
1994-08-16 23:36:45 +00:00
|
|
|
*
|
1996-11-01 06:45:43 +00:00
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Brian Hirt.
|
1994-08-16 23:36:45 +00:00
|
|
|
*
|
1996-11-01 06:45:43 +00:00
|
|
|
* See the LICENSE file for redistribution information.
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
1994-08-16 23:36:45 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/queue.h>
|
1996-11-01 06:45:43 +00:00
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/stat.h>
|
1994-08-16 23:36:45 +00:00
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include <bitstring.h>
|
|
|
|
#include <errno.h>
|
1996-11-01 06:45:43 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <grp.h>
|
1994-08-16 23:36:45 +00:00
|
|
|
#include <limits.h>
|
2013-08-11 09:44:58 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
1994-08-16 23:36:45 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
2013-08-11 09:44:58 +00:00
|
|
|
#ifdef HAVE_LIBUTIL_H
|
|
|
|
#include <libutil.h>
|
2020-10-01 04:25:54 +00:00
|
|
|
#elif defined HAVE_PTY_H
|
|
|
|
#include <pty.h>
|
2013-08-11 09:44:58 +00:00
|
|
|
#else
|
|
|
|
#include <util.h>
|
|
|
|
#endif
|
1994-08-16 23:36:45 +00:00
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
#include "../common/common.h"
|
|
|
|
#include "../vi/vi.h"
|
1994-08-16 23:36:45 +00:00
|
|
|
#include "script.h"
|
1996-11-01 06:45:43 +00:00
|
|
|
#include "pathnames.h"
|
1994-08-16 23:36:45 +00:00
|
|
|
|
2015-04-10 13:26:05 +00:00
|
|
|
static void sscr_check(SCR *);
|
|
|
|
static int sscr_getprompt(SCR *);
|
|
|
|
static int sscr_init(SCR *);
|
|
|
|
static int sscr_insert(SCR *);
|
|
|
|
static int sscr_matchprompt(SCR *, char *, size_t, size_t *);
|
|
|
|
static int sscr_setprompt(SCR *, char *, size_t);
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ex_script -- : sc[ript][!] [file]
|
|
|
|
* Switch to script mode.
|
1996-11-01 06:45:43 +00:00
|
|
|
*
|
2015-04-10 13:26:05 +00:00
|
|
|
* PUBLIC: int ex_script(SCR *, EXCMD *);
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
|
|
|
int
|
2013-08-11 09:44:58 +00:00
|
|
|
ex_script(SCR *sp, EXCMD *cmdp)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
|
|
|
/* Vi only command. */
|
1996-11-01 06:45:43 +00:00
|
|
|
if (!F_ISSET(sp, SC_VI)) {
|
1994-08-16 23:36:45 +00:00
|
|
|
msgq(sp, M_ERR,
|
1996-11-01 06:45:43 +00:00
|
|
|
"150|The script command is only available in vi mode");
|
1994-08-16 23:36:45 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Switch to the new file. */
|
1996-11-01 06:45:43 +00:00
|
|
|
if (cmdp->argc != 0 && ex_edit(sp, cmdp))
|
1994-08-16 23:36:45 +00:00
|
|
|
return (1);
|
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
/* Create the shell, figure out the prompt. */
|
|
|
|
if (sscr_init(sp))
|
1994-08-16 23:36:45 +00:00
|
|
|
return (1);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sscr_init --
|
|
|
|
* Create a pty setup for a shell.
|
|
|
|
*/
|
|
|
|
static int
|
2013-08-11 09:44:58 +00:00
|
|
|
sscr_init(SCR *sp)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
|
|
|
SCRIPT *sc;
|
|
|
|
char *sh, *sh_path;
|
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
/* We're going to need a shell. */
|
|
|
|
if (opts_empty(sp, O_SHELL, 0))
|
|
|
|
return (1);
|
|
|
|
|
2020-09-09 07:57:59 +00:00
|
|
|
MALLOC_RET(sp, sc, sizeof(SCRIPT));
|
1994-08-16 23:36:45 +00:00
|
|
|
sp->script = sc;
|
|
|
|
sc->sh_prompt = NULL;
|
|
|
|
sc->sh_prompt_len = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There are two different processes running through this code.
|
|
|
|
* They are the shell and the parent.
|
|
|
|
*/
|
|
|
|
sc->sh_master = sc->sh_slave = -1;
|
|
|
|
|
|
|
|
if (tcgetattr(STDIN_FILENO, &sc->sh_term) == -1) {
|
|
|
|
msgq(sp, M_SYSERR, "tcgetattr");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Turn off output postprocessing and echo.
|
|
|
|
*/
|
|
|
|
sc->sh_term.c_oflag &= ~OPOST;
|
|
|
|
sc->sh_term.c_cflag &= ~(ECHO|ECHOE|ECHONL|ECHOK);
|
|
|
|
|
|
|
|
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &sc->sh_win) == -1) {
|
|
|
|
msgq(sp, M_SYSERR, "tcgetattr");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2013-08-11 09:44:58 +00:00
|
|
|
if (openpty(&sc->sh_master,
|
1994-08-16 23:36:45 +00:00
|
|
|
&sc->sh_slave, sc->sh_name, &sc->sh_term, &sc->sh_win) == -1) {
|
2013-08-11 09:44:58 +00:00
|
|
|
msgq(sp, M_SYSERR, "openpty");
|
1994-08-16 23:36:45 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1996-11-01 06:45:43 +00:00
|
|
|
* __TK__ huh?
|
1994-08-16 23:36:45 +00:00
|
|
|
* Don't use vfork() here, because the signal semantics differ from
|
|
|
|
* implementation to implementation.
|
|
|
|
*/
|
|
|
|
switch (sc->sh_pid = fork()) {
|
|
|
|
case -1: /* Error. */
|
|
|
|
msgq(sp, M_SYSERR, "fork");
|
|
|
|
err: if (sc->sh_master != -1)
|
|
|
|
(void)close(sc->sh_master);
|
|
|
|
if (sc->sh_slave != -1)
|
|
|
|
(void)close(sc->sh_slave);
|
|
|
|
return (1);
|
|
|
|
case 0: /* Utility. */
|
|
|
|
/*
|
|
|
|
* XXX
|
|
|
|
* So that shells that do command line editing turn it off.
|
|
|
|
*/
|
1996-11-01 06:45:43 +00:00
|
|
|
(void)setenv("TERM", "emacs", 1);
|
|
|
|
(void)setenv("TERMCAP", "emacs:", 1);
|
|
|
|
(void)setenv("EMACS", "t", 1);
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
(void)setsid();
|
|
|
|
#ifdef TIOCSCTTY
|
|
|
|
/*
|
|
|
|
* 4.4BSD allocates a controlling terminal using the TIOCSCTTY
|
|
|
|
* ioctl, not by opening a terminal device file. POSIX 1003.1
|
|
|
|
* doesn't define a portable way to do this. If TIOCSCTTY is
|
|
|
|
* not available, hope that the open does it.
|
|
|
|
*/
|
|
|
|
(void)ioctl(sc->sh_slave, TIOCSCTTY, 0);
|
|
|
|
#endif
|
|
|
|
(void)close(sc->sh_master);
|
|
|
|
(void)dup2(sc->sh_slave, STDIN_FILENO);
|
|
|
|
(void)dup2(sc->sh_slave, STDOUT_FILENO);
|
|
|
|
(void)dup2(sc->sh_slave, STDERR_FILENO);
|
|
|
|
(void)close(sc->sh_slave);
|
|
|
|
|
|
|
|
/* Assumes that all shells have -i. */
|
|
|
|
sh_path = O_STR(sp, O_SHELL);
|
|
|
|
if ((sh = strrchr(sh_path, '/')) == NULL)
|
|
|
|
sh = sh_path;
|
|
|
|
else
|
|
|
|
++sh;
|
|
|
|
execl(sh_path, sh, "-i", NULL);
|
1996-11-01 06:45:43 +00:00
|
|
|
msgq_str(sp, M_SYSERR, sh_path, "execl: %s");
|
1994-08-16 23:36:45 +00:00
|
|
|
_exit(127);
|
|
|
|
default: /* Parent. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
if (sscr_getprompt(sp))
|
1994-08-16 23:36:45 +00:00
|
|
|
return (1);
|
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
F_SET(sp, SC_SCRIPT);
|
|
|
|
F_SET(sp->gp, G_SCRWIN);
|
1994-08-16 23:36:45 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sscr_getprompt --
|
|
|
|
* Eat lines printed by the shell until a line with no trailing
|
|
|
|
* carriage return comes; set the prompt from that line.
|
|
|
|
*/
|
|
|
|
static int
|
2013-08-11 09:44:58 +00:00
|
|
|
sscr_getprompt(SCR *sp)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
2013-08-11 09:44:58 +00:00
|
|
|
EX_PRIVATE *exp;
|
1994-08-16 23:36:45 +00:00
|
|
|
struct timeval tv;
|
2013-08-11 09:44:58 +00:00
|
|
|
char *endp, *p, *t, buf[1024];
|
1994-08-16 23:36:45 +00:00
|
|
|
SCRIPT *sc;
|
|
|
|
fd_set fdset;
|
|
|
|
recno_t lline;
|
|
|
|
size_t llen, len;
|
|
|
|
int nr;
|
2013-08-11 09:44:58 +00:00
|
|
|
CHAR_T *wp;
|
|
|
|
size_t wlen;
|
|
|
|
|
|
|
|
exp = EXP(sp);
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
FD_ZERO(&fdset);
|
|
|
|
endp = buf;
|
|
|
|
len = sizeof(buf);
|
|
|
|
|
|
|
|
/* Wait up to a second for characters to read. */
|
|
|
|
tv.tv_sec = 5;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
sc = sp->script;
|
|
|
|
FD_SET(sc->sh_master, &fdset);
|
|
|
|
switch (select(sc->sh_master + 1, &fdset, NULL, NULL, &tv)) {
|
|
|
|
case -1: /* Error or interrupt. */
|
|
|
|
msgq(sp, M_SYSERR, "select");
|
|
|
|
goto prompterr;
|
|
|
|
case 0: /* Timeout */
|
|
|
|
msgq(sp, M_ERR, "Error: timed out");
|
|
|
|
goto prompterr;
|
|
|
|
case 1: /* Characters to read. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the characters. */
|
|
|
|
more: len = sizeof(buf) - (endp - buf);
|
|
|
|
switch (nr = read(sc->sh_master, endp, len)) {
|
|
|
|
case 0: /* EOF. */
|
|
|
|
msgq(sp, M_ERR, "Error: shell: EOF");
|
|
|
|
goto prompterr;
|
|
|
|
case -1: /* Error or interrupt. */
|
|
|
|
msgq(sp, M_SYSERR, "shell");
|
|
|
|
goto prompterr;
|
|
|
|
default:
|
|
|
|
endp += nr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If any complete lines, push them into the file. */
|
|
|
|
for (p = t = buf; p < endp; ++p) {
|
2013-08-11 09:44:58 +00:00
|
|
|
if (*p == '\r' || *p == '\n') {
|
|
|
|
if (CHAR2INT5(sp, exp->ibcw, t, p - t, wp, wlen))
|
|
|
|
goto conv_err;
|
1996-11-01 06:45:43 +00:00
|
|
|
if (db_last(sp, &lline) ||
|
2013-08-11 09:44:58 +00:00
|
|
|
db_append(sp, 0, lline, wp, wlen))
|
1994-08-16 23:36:45 +00:00
|
|
|
goto prompterr;
|
|
|
|
t = p + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p > buf) {
|
|
|
|
memmove(buf, t, endp - t);
|
|
|
|
endp = buf + (endp - t);
|
|
|
|
}
|
|
|
|
if (endp == buf)
|
|
|
|
goto more;
|
|
|
|
|
|
|
|
/* Wait up 1/10 of a second to make sure that we got it all. */
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 100000;
|
|
|
|
switch (select(sc->sh_master + 1, &fdset, NULL, NULL, &tv)) {
|
|
|
|
case -1: /* Error or interrupt. */
|
|
|
|
msgq(sp, M_SYSERR, "select");
|
|
|
|
goto prompterr;
|
|
|
|
case 0: /* Timeout */
|
|
|
|
break;
|
|
|
|
case 1: /* Characters to read. */
|
|
|
|
goto more;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Timed out, so theoretically we have a prompt. */
|
|
|
|
llen = endp - buf;
|
|
|
|
endp = buf;
|
|
|
|
|
|
|
|
/* Append the line into the file. */
|
2013-08-11 09:44:58 +00:00
|
|
|
if (CHAR2INT5(sp, exp->ibcw, buf, llen, wp, wlen))
|
|
|
|
goto conv_err;
|
|
|
|
if (db_last(sp, &lline) || db_append(sp, 0, lline, wp, wlen)) {
|
|
|
|
if (0)
|
|
|
|
conv_err: msgq(sp, M_ERR, "323|Invalid input. Truncated.");
|
1994-08-16 23:36:45 +00:00
|
|
|
prompterr: sscr_end(sp);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (sscr_setprompt(sp, buf, llen));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sscr_exec --
|
|
|
|
* Take a line and hand it off to the shell.
|
1996-11-01 06:45:43 +00:00
|
|
|
*
|
2015-04-10 13:26:05 +00:00
|
|
|
* PUBLIC: int sscr_exec(SCR *, recno_t);
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
|
|
|
int
|
2013-08-11 09:44:58 +00:00
|
|
|
sscr_exec(SCR *sp, recno_t lno)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
|
|
|
SCRIPT *sc;
|
|
|
|
recno_t last_lno;
|
|
|
|
size_t blen, len, last_len, tlen;
|
1996-11-01 06:45:43 +00:00
|
|
|
int isempty, matchprompt, nw, rval;
|
2013-08-11 09:44:58 +00:00
|
|
|
char *bp = NULL, *p;
|
|
|
|
CHAR_T *wp;
|
|
|
|
size_t wlen;
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
/* If there's a prompt on the last line, append the command. */
|
1996-11-01 06:45:43 +00:00
|
|
|
if (db_last(sp, &last_lno))
|
1994-08-16 23:36:45 +00:00
|
|
|
return (1);
|
2013-08-11 09:44:58 +00:00
|
|
|
if (db_get(sp, last_lno, DBG_FATAL, &wp, &wlen))
|
1994-08-16 23:36:45 +00:00
|
|
|
return (1);
|
2013-08-11 09:44:58 +00:00
|
|
|
INT2CHAR(sp, wp, wlen, p, last_len);
|
1994-08-16 23:36:45 +00:00
|
|
|
if (sscr_matchprompt(sp, p, last_len, &tlen) && tlen == 0) {
|
|
|
|
matchprompt = 1;
|
2013-08-11 09:44:58 +00:00
|
|
|
GET_SPACE_RETC(sp, bp, blen, last_len + 128);
|
1994-08-16 23:36:45 +00:00
|
|
|
memmove(bp, p, last_len);
|
|
|
|
} else
|
|
|
|
matchprompt = 0;
|
|
|
|
|
|
|
|
/* Get something to execute. */
|
2013-08-11 09:44:58 +00:00
|
|
|
if (db_eget(sp, lno, &wp, &wlen, &isempty)) {
|
1996-11-01 06:45:43 +00:00
|
|
|
if (isempty)
|
1994-08-16 23:36:45 +00:00
|
|
|
goto empty;
|
|
|
|
goto err1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Empty lines aren't interesting. */
|
2013-08-11 09:44:58 +00:00
|
|
|
if (wlen == 0)
|
1994-08-16 23:36:45 +00:00
|
|
|
goto empty;
|
2013-08-11 09:44:58 +00:00
|
|
|
INT2CHAR(sp, wp, wlen, p, len);
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
/* Delete any prompt. */
|
|
|
|
if (sscr_matchprompt(sp, p, len, &tlen)) {
|
|
|
|
if (tlen == len) {
|
1996-11-01 06:45:43 +00:00
|
|
|
empty: msgq(sp, M_BERR, "151|No command to execute");
|
1994-08-16 23:36:45 +00:00
|
|
|
goto err1;
|
|
|
|
}
|
|
|
|
p += (len - tlen);
|
|
|
|
len = tlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Push the line to the shell. */
|
|
|
|
sc = sp->script;
|
|
|
|
if ((nw = write(sc->sh_master, p, len)) != len)
|
|
|
|
goto err2;
|
|
|
|
rval = 0;
|
|
|
|
if (write(sc->sh_master, "\n", 1) != 1) {
|
|
|
|
err2: if (nw == 0)
|
|
|
|
errno = EIO;
|
|
|
|
msgq(sp, M_SYSERR, "shell");
|
|
|
|
goto err1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (matchprompt) {
|
2013-08-11 09:44:58 +00:00
|
|
|
ADD_SPACE_RETC(sp, bp, blen, last_len + len);
|
1994-08-16 23:36:45 +00:00
|
|
|
memmove(bp + last_len, p, len);
|
2013-08-11 09:44:58 +00:00
|
|
|
CHAR2INT(sp, bp, last_len + len, wp, wlen);
|
|
|
|
if (db_set(sp, last_lno, wp, wlen))
|
1994-08-16 23:36:45 +00:00
|
|
|
err1: rval = 1;
|
|
|
|
}
|
|
|
|
if (matchprompt)
|
|
|
|
FREE_SPACE(sp, bp, blen);
|
|
|
|
return (rval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sscr_input --
|
1996-11-01 06:45:43 +00:00
|
|
|
* Read any waiting shell input.
|
|
|
|
*
|
2015-04-10 13:26:05 +00:00
|
|
|
* PUBLIC: int sscr_input(SCR *);
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
|
|
|
int
|
2013-08-11 09:44:58 +00:00
|
|
|
sscr_input(SCR *sp)
|
1996-11-01 06:45:43 +00:00
|
|
|
{
|
|
|
|
GS *gp;
|
|
|
|
struct timeval poll;
|
|
|
|
fd_set rdfd;
|
|
|
|
int maxfd;
|
|
|
|
|
|
|
|
gp = sp->gp;
|
|
|
|
|
|
|
|
loop: maxfd = 0;
|
|
|
|
FD_ZERO(&rdfd);
|
|
|
|
poll.tv_sec = 0;
|
|
|
|
poll.tv_usec = 0;
|
|
|
|
|
|
|
|
/* Set up the input mask. */
|
2013-08-11 09:44:58 +00:00
|
|
|
TAILQ_FOREACH(sp, gp->dq, q)
|
1996-11-01 06:45:43 +00:00
|
|
|
if (F_ISSET(sp, SC_SCRIPT)) {
|
|
|
|
FD_SET(sp->script->sh_master, &rdfd);
|
|
|
|
if (sp->script->sh_master > maxfd)
|
|
|
|
maxfd = sp->script->sh_master;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for input. */
|
|
|
|
switch (select(maxfd + 1, &rdfd, NULL, NULL, &poll)) {
|
|
|
|
case -1:
|
|
|
|
msgq(sp, M_SYSERR, "select");
|
|
|
|
return (1);
|
|
|
|
case 0:
|
|
|
|
return (0);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the input. */
|
2013-08-11 09:44:58 +00:00
|
|
|
TAILQ_FOREACH(sp, gp->dq, q)
|
1996-11-01 06:45:43 +00:00
|
|
|
if (F_ISSET(sp, SC_SCRIPT) &&
|
2013-08-11 09:44:58 +00:00
|
|
|
FD_ISSET(sp->script->sh_master, &rdfd) &&
|
|
|
|
sscr_insert(sp))
|
1996-11-01 06:45:43 +00:00
|
|
|
return (1);
|
|
|
|
goto loop;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sscr_insert --
|
|
|
|
* Take a line from the shell and insert it into the file.
|
|
|
|
*/
|
|
|
|
static int
|
2013-08-11 09:44:58 +00:00
|
|
|
sscr_insert(SCR *sp)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
2013-08-11 09:44:58 +00:00
|
|
|
EX_PRIVATE *exp;
|
1994-08-16 23:36:45 +00:00
|
|
|
struct timeval tv;
|
2013-08-11 09:44:58 +00:00
|
|
|
char *endp, *p, *t;
|
1994-08-16 23:36:45 +00:00
|
|
|
SCRIPT *sc;
|
1996-11-01 06:45:43 +00:00
|
|
|
fd_set rdfd;
|
1994-08-16 23:36:45 +00:00
|
|
|
recno_t lno;
|
|
|
|
size_t blen, len, tlen;
|
|
|
|
int nr, rval;
|
|
|
|
char *bp;
|
2013-08-11 09:44:58 +00:00
|
|
|
CHAR_T *wp;
|
|
|
|
size_t wlen = 0;
|
|
|
|
|
|
|
|
exp = EXP(sp);
|
|
|
|
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
/* Find out where the end of the file is. */
|
1996-11-01 06:45:43 +00:00
|
|
|
if (db_last(sp, &lno))
|
1994-08-16 23:36:45 +00:00
|
|
|
return (1);
|
|
|
|
|
|
|
|
#define MINREAD 1024
|
2013-08-11 09:44:58 +00:00
|
|
|
GET_SPACE_RETC(sp, bp, blen, MINREAD);
|
1994-08-16 23:36:45 +00:00
|
|
|
endp = bp;
|
|
|
|
|
|
|
|
/* Read the characters. */
|
|
|
|
rval = 1;
|
|
|
|
sc = sp->script;
|
|
|
|
more: switch (nr = read(sc->sh_master, endp, MINREAD)) {
|
|
|
|
case 0: /* EOF; shell just exited. */
|
|
|
|
sscr_end(sp);
|
|
|
|
rval = 0;
|
|
|
|
goto ret;
|
|
|
|
case -1: /* Error or interrupt. */
|
|
|
|
msgq(sp, M_SYSERR, "shell");
|
|
|
|
goto ret;
|
|
|
|
default:
|
|
|
|
endp += nr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Append the lines into the file. */
|
|
|
|
for (p = t = bp; p < endp; ++p) {
|
2013-08-11 09:44:58 +00:00
|
|
|
if (*p == '\r' || *p == '\n') {
|
1994-08-16 23:36:45 +00:00
|
|
|
len = p - t;
|
2013-08-11 09:44:58 +00:00
|
|
|
if (CHAR2INT5(sp, exp->ibcw, t, len, wp, wlen))
|
|
|
|
goto conv_err;
|
|
|
|
if (db_append(sp, 1, lno++, wp, wlen))
|
1994-08-16 23:36:45 +00:00
|
|
|
goto ret;
|
|
|
|
t = p + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p > t) {
|
|
|
|
len = p - t;
|
|
|
|
/*
|
|
|
|
* If the last thing from the shell isn't another prompt, wait
|
|
|
|
* up to 1/10 of a second for more stuff to show up, so that
|
|
|
|
* we don't break the output into two separate lines. Don't
|
|
|
|
* want to hang indefinitely because some program is hanging,
|
|
|
|
* confused the shell, or whatever.
|
|
|
|
*/
|
|
|
|
if (!sscr_matchprompt(sp, t, len, &tlen) || tlen != 0) {
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 100000;
|
1996-11-01 06:45:43 +00:00
|
|
|
FD_ZERO(&rdfd);
|
|
|
|
FD_SET(sc->sh_master, &rdfd);
|
1994-08-16 23:36:45 +00:00
|
|
|
if (select(sc->sh_master + 1,
|
1996-11-01 06:45:43 +00:00
|
|
|
&rdfd, NULL, NULL, &tv) == 1) {
|
1994-08-16 23:36:45 +00:00
|
|
|
memmove(bp, t, len);
|
|
|
|
endp = bp + len;
|
|
|
|
goto more;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sscr_setprompt(sp, t, len))
|
|
|
|
return (1);
|
2013-08-11 09:44:58 +00:00
|
|
|
if (CHAR2INT5(sp, exp->ibcw, t, len, wp, wlen))
|
|
|
|
goto conv_err;
|
|
|
|
if (db_append(sp, 1, lno++, wp, wlen))
|
1994-08-16 23:36:45 +00:00
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The cursor moves to EOF. */
|
|
|
|
sp->lno = lno;
|
2013-08-11 09:44:58 +00:00
|
|
|
sp->cno = wlen ? wlen - 1 : 0;
|
1996-11-01 06:45:43 +00:00
|
|
|
rval = vs_refresh(sp, 1);
|
1994-08-16 23:36:45 +00:00
|
|
|
|
2013-08-11 09:44:58 +00:00
|
|
|
if (0)
|
|
|
|
conv_err: msgq(sp, M_ERR, "323|Invalid input. Truncated.");
|
|
|
|
|
1994-08-16 23:36:45 +00:00
|
|
|
ret: FREE_SPACE(sp, bp, blen);
|
|
|
|
return (rval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sscr_setprompt --
|
|
|
|
*
|
|
|
|
* Set the prompt to the last line we got from the shell.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static int
|
2013-08-11 09:44:58 +00:00
|
|
|
sscr_setprompt(SCR *sp, char *buf, size_t len)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
|
|
|
SCRIPT *sc;
|
|
|
|
|
|
|
|
sc = sp->script;
|
2020-09-09 07:57:59 +00:00
|
|
|
free(sc->sh_prompt);
|
|
|
|
MALLOC(sp, sc->sh_prompt, len + 1);
|
1994-08-16 23:36:45 +00:00
|
|
|
if (sc->sh_prompt == NULL) {
|
|
|
|
sscr_end(sp);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
memmove(sc->sh_prompt, buf, len);
|
|
|
|
sc->sh_prompt_len = len;
|
|
|
|
sc->sh_prompt[len] = '\0';
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sscr_matchprompt --
|
|
|
|
* Check to see if a line matches the prompt. Nul's indicate
|
|
|
|
* parts that can change, in both content and size.
|
|
|
|
*/
|
|
|
|
static int
|
2013-08-11 09:44:58 +00:00
|
|
|
sscr_matchprompt(SCR *sp, char *lp, size_t line_len, size_t *lenp)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
|
|
|
SCRIPT *sc;
|
|
|
|
size_t prompt_len;
|
|
|
|
char *pp;
|
|
|
|
|
|
|
|
sc = sp->script;
|
|
|
|
if (line_len < (prompt_len = sc->sh_prompt_len))
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
for (pp = sc->sh_prompt;
|
|
|
|
prompt_len && line_len; --prompt_len, --line_len) {
|
|
|
|
if (*pp == '\0') {
|
|
|
|
for (; prompt_len && *pp == '\0'; --prompt_len, ++pp);
|
|
|
|
if (!prompt_len)
|
|
|
|
return (0);
|
|
|
|
for (; line_len && *lp != *pp; --line_len, ++lp);
|
|
|
|
if (!line_len)
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (*pp++ != *lp++)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prompt_len)
|
|
|
|
return (0);
|
|
|
|
if (lenp != NULL)
|
|
|
|
*lenp = line_len;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sscr_end --
|
|
|
|
* End the pipe to a shell.
|
1996-11-01 06:45:43 +00:00
|
|
|
*
|
2015-04-10 13:26:05 +00:00
|
|
|
* PUBLIC: int sscr_end(SCR *);
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
|
|
|
int
|
2013-08-11 09:44:58 +00:00
|
|
|
sscr_end(SCR *sp)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
|
|
|
SCRIPT *sc;
|
|
|
|
|
|
|
|
if ((sc = sp->script) == NULL)
|
|
|
|
return (0);
|
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
/* Turn off the script flags. */
|
|
|
|
F_CLR(sp, SC_SCRIPT);
|
|
|
|
sscr_check(sp);
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
/* Close down the parent's file descriptors. */
|
|
|
|
if (sc->sh_master != -1)
|
|
|
|
(void)close(sc->sh_master);
|
|
|
|
if (sc->sh_slave != -1)
|
|
|
|
(void)close(sc->sh_slave);
|
|
|
|
|
|
|
|
/* This should have killed the child. */
|
1996-11-01 06:45:43 +00:00
|
|
|
(void)proc_wait(sp, (long)sc->sh_pid, "script-shell", 0, 0);
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
/* Free memory. */
|
1996-11-01 06:45:43 +00:00
|
|
|
free(sc->sh_prompt);
|
|
|
|
free(sc);
|
1994-08-16 23:36:45 +00:00
|
|
|
sp->script = NULL;
|
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sscr_check --
|
|
|
|
* Set/clear the global scripting bit.
|
|
|
|
*/
|
|
|
|
static void
|
2013-08-11 09:44:58 +00:00
|
|
|
sscr_check(SCR *sp)
|
1996-11-01 06:45:43 +00:00
|
|
|
{
|
|
|
|
GS *gp;
|
|
|
|
|
|
|
|
gp = sp->gp;
|
2013-08-11 09:44:58 +00:00
|
|
|
TAILQ_FOREACH(sp, gp->dq, q)
|
1996-11-01 06:45:43 +00:00
|
|
|
if (F_ISSET(sp, SC_SCRIPT)) {
|
|
|
|
F_SET(gp, G_SCRWIN);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
F_CLR(gp, G_SCRWIN);
|
|
|
|
}
|