#include "expect_cf.h"
#include <stdio.h>
#include <setjmp.h>
#ifdef HAVE_INTTYPES_H
# include <inttypes.h>
#endif
#include <sys/types.h>
#include <sys/ioctl.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#ifdef CRAY
# ifndef TCSETCTTY
# if defined(HAVE_TERMIOS)
# include <termios.h>
# else
# include <termio.h>
# endif
# endif
#endif
#ifdef HAVE_SYS_FCNTL_H
# include <sys/fcntl.h>
#else
# include <fcntl.h>
#endif
#ifdef HAVE_STRREDIR_H
#include <sys/strredir.h>
# ifdef SRIOCSREDIR
# undef TIOCCONS
# endif
#endif
#include <signal.h>
#include "string.h"
#include <errno.h>
#include "exp_rename.h"
#define EXP_AVOID_INCLUDING_TCL_H
#include "expect.h"
#include "exp_int.h"
#include "exp_printify.h"
#ifdef NO_STDLIB_H
#include "../compat/stdlib.h"
#else
#include <stdlib.h>
#endif
#define EXP_MATCH_MAX 2000
char *exp_buffer = 0;
char *exp_buffer_end = 0;
char *exp_match = 0;
char *exp_match_end = 0;
int exp_match_max = EXP_MATCH_MAX;
int exp_full_buffer = FALSE;
int exp_remove_nulls = TRUE;
int exp_timeout = 10;
int exp_pty_timeout = 5;
int exp_autoallocpty = TRUE;
int exp_pty[2];
int exp_pid;
char *exp_stty_init = 0;
int exp_ttycopy = TRUE;
int exp_ttyinit = TRUE;
int exp_console = FALSE;
void (*exp_child_exec_prelude)() = 0;
jmp_buf exp_readenv;
int exp_reading = FALSE;
void debuglog();
int getptymaster();
int getptyslave();
int Exp_StringMatch();
#define sysreturn(x) return(errno = x, -1)
void exp_init_pty();
extern char *TclGetRegError();
extern void TclRegError();
char *Tcl_ErrnoMsg();
static unsigned int bufsiz = 2*EXP_MATCH_MAX;
static struct f {
int valid;
char *buffer;
char *buffer_end;
char *match_end;
int msize;
} *fs = 0;
static int fd_alloc_max = -1;
static struct f *
fdfp2f(fd,fp)
int fd;
FILE *fp;
{
if (fd == -1) return(fs + fileno(fp));
else return(fs + fd);
}
static struct f *
fd_new(fd)
int fd;
{
int i, low;
struct f *fp;
struct f *newfs;
if (fd > fd_alloc_max) {
if (!fs) {
newfs = (struct f *)malloc(sizeof(struct f)*(fd+1));
low = 0;
} else {
newfs = (struct f *)realloc((char *)fs,sizeof(struct f)*(fd+1));
low = fd_alloc_max+1;
}
fs = newfs;
fd_alloc_max = fd;
for (i = low; i <= fd_alloc_max; i++) {
fs[i].valid = FALSE;
}
}
fp = fs+fd;
if (!fp->valid) {
fp->buffer = malloc((unsigned)(bufsiz+1));
if (!fp->buffer) return 0;
fp->msize = bufsiz;
fp->valid = TRUE;
}
fp->buffer_end = fp->buffer;
fp->match_end = fp->buffer;
return fp;
}
int
exp_spawnv(file,argv)
char *file;
char *argv[];
{
int cc;
int errorfd;
int ttyfd;
int sync_fds[2];
int sync2_fds[2];
char sync_byte;
#ifdef PTYTRAP_DIES
int slave_write_ioctls = 1;
#endif
static int first_time = TRUE;
if (first_time) {
first_time = FALSE;
exp_init_pty();
exp_init_tty();
}
if (!file || !argv) sysreturn(EINVAL);
if (!argv[0] || strcmp(file,argv[0])) {
debuglog("expect: warning: file (%s) != argv[0] (%s)\n",
file,
argv[0]?argv[0]:"");
}
#ifdef PTYTRAP_DIES
#if defined(TIOCSCTTY) && !defined(CIBAUD) && !defined(sun) && !defined(hp9000s300)
slave_write_ioctls++;
#endif
#endif
if (exp_autoallocpty) {
if (0 > (exp_pty[0] = getptymaster())) sysreturn(ENODEV);
}
fcntl(exp_pty[0],F_SETFD,1);
#ifdef PTYTRAP_DIES
exp_slave_control(exp_pty[0],1);*/
#endif
if (!fd_new(exp_pty[0])) {
errno = ENOMEM;
return -1;
}
if (-1 == (pipe(sync_fds))) {
return -1;
}
if (-1 == (pipe(sync2_fds))) {
return -1;
}
if ((exp_pid = fork()) == -1) return(-1);
if (exp_pid) {
close(sync_fds[1]);
close(sync2_fds[0]);
if (!exp_autoallocpty) close(exp_pty[1]);
#ifdef PTYTRAP_DIES
#ifdef HAVE_PTYTRAP
if (exp_autoallocpty) {
while (slave_write_ioctls) {
int cc;
cc = exp_wait_for_slave_open(exp_pty[0]);
#if defined(TIOCSCTTY) && !defined(CIBAUD) && !defined(sun) && !defined(hp9000s300)
if (cc == TIOCSCTTY) slave_write_ioctls = 0;
#endif
if (cc & IOC_IN) slave_write_ioctls--;
else if (cc == -1) {
printf("failed to trap slave pty");
return -1;
}
}
}
#endif
#endif
debuglog("parent: waiting for sync byte\r\n");
cc = read(sync_fds[0],&sync_byte,1);
if (cc == -1) {
fprintf(stderr,"parent sync byte read: %s\r\n",Tcl_ErrnoMsg(errno));
exit(-1);
}
exp_slave_control(exp_pty[0],1);
debuglog("parent: telling child to go ahead\r\n");
cc = write(sync2_fds[1]," ",1);
if (cc == -1) {
errorlog("parent sync byte write: %s\r\n",Tcl_ErrnoMsg(errno));
exit(-1);
}
debuglog("parent: now unsynchronized from child\r\n");
close(sync_fds[0]);
close(sync2_fds[1]);
return(exp_pty[0]);
}
close(sync_fds[0]);
close(sync2_fds[1]);
#ifdef CRAY
(void) close(exp_pty[0]);
#endif
#if defined(POSIX) && !defined(ultrix)
#define DO_SETSID
#endif
#ifdef __convex__
#define DO_SETSID
#endif
#ifdef DO_SETSID
setsid();
#else
#ifdef SYSV3
#ifndef CRAY
setpgrp();
#endif
#else
#ifdef MIPS_BSD
# include <sysv/sys.s>
syscall(SYS_setpgrp);
#endif
setpgrp(0,0);
#ifdef TIOCNOTTY
ttyfd = open("/dev/tty", O_RDWR);
if (ttyfd >= 0) {
(void) ioctl(ttyfd, TIOCNOTTY, (char *)0);
(void) close(ttyfd);
}
#endif
#endif
#endif
errorfd = fcntl(2,F_DUPFD,3);
#define restore_error_fd {close(2);fcntl(errorfd,F_DUPFD,2);}
if (exp_autoallocpty) {
close(0);
close(1);
close(2);
if (0 > (exp_pty[1] = getptyslave(exp_ttycopy,exp_ttyinit,
exp_stty_init))) {
restore_error_fd
fprintf(stderr,"open(slave pty): %s\n",Tcl_ErrnoMsg(errno));
exit(-1);
}
if (exp_pty[1] != 0) {
restore_error_fd
fprintf(stderr,"getptyslave: slave = %d but expected 0\n",
exp_pty[1]);
exit(-1);
}
} else {
if (exp_pty[1] != 0) {
close(0); fcntl(exp_pty[1],F_DUPFD,0);
}
close(1); fcntl(0,F_DUPFD,1);
close(2); fcntl(0,F_DUPFD,1);
close(exp_pty[1]);
}
#if defined(TIOCSCTTY) && !defined(sun) && !defined(hpux)
#ifdef __QNX__
if (tcsetct(0, getpid()) == -1) {
#else
if (ioctl(0,TIOCSCTTY,(char *)0) < 0) {
#endif
restore_error_fd
fprintf(stderr,"failed to get controlling terminal using TIOCSCTTY");
exit(-1);
}
#endif
#ifdef CRAY
(void) setsid();
(void) ioctl(0,TCSETCTTY,0);
(void) close(0);
if (open("/dev/tty", O_RDWR) < 0) {
restore_error_fd
fprintf(stderr,"open(/dev/tty): %s\r\n",Tcl_ErrnoMsg(errno));
exit(-1);
}
(void) close(1);
(void) close(2);
(void) dup(0);
(void) dup(0);
setptyutmp();
#ifdef _CRAY2
if ((pid = fork()) == -1) {
restore_error_fd
fprintf(stderr,"second fork: %s\r\n",Tcl_ErrnoMsg(errno));
exit(-1);
}
if (pid) {
int status;
int timeout;
char *t;
timeout = exp_pty_timeout;
while (wait(&status) < 0 && errno == EINTR)
;
sleep(timeout);
if (WIFSIGNALED(status))
kill(getpid(), WTERMSIG(status));
exit(WEXITSTATUS(status));
}
#endif
#endif
if (exp_console) {
#ifdef SRIOCSREDIR
int fd;
if ((fd = open("/dev/console", O_RDONLY)) == -1) {
restore_error_fd
fprintf(stderr, "spawn %s: cannot open console, check permissions of /dev/console\n",argv[0]);
exit(-1);
}
if (ioctl(fd, SRIOCSREDIR, 0) == -1) {
restore_error_fd
fprintf(stderr, "spawn %s: cannot redirect console, check permissions of /dev/console\n",argv[0]);
}
close(fd);
#endif
#ifdef TIOCCONS
int on = 1;
if (ioctl(0,TIOCCONS,(char *)&on) == -1) {
restore_error_fd
fprintf(stderr, "spawn %s: cannot open console, check permissions of /dev/console\n",argv[0]);
exit(-1);
}
#endif
}
cc = write(sync_fds[1]," ",1);
if (cc == -1) {
restore_error_fd
fprintf(stderr,"child: sync byte write: %s\r\n",Tcl_ErrnoMsg(errno));
exit(-1);
}
close(sync_fds[1]);
cc = read(sync2_fds[0],&sync_byte,1);
if (cc == -1) {
restore_error_fd
errorlog("child: sync byte read: %s\r\n",Tcl_ErrnoMsg(errno));
exit(-1);
}
close(sync2_fds[0]);
if (exp_close_in_child) (*exp_close_in_child)();
if (exp_child_exec_prelude) (*exp_child_exec_prelude)();
(void) execvp(file,argv);
fprintf(stderr,"execvp(%s): %s\n",file,Tcl_ErrnoMsg(errno));
exit(-1);
}
int
exp_spawnl TCL_VARARGS_DEF(char *,arg1)
{
va_list args;
int i;
char *arg, **argv;
arg = TCL_VARARGS_START(char *,arg1,args);
for (i=1;;i++) {
arg = va_arg(args,char *);
if (!arg) break;
}
va_end(args);
if (i == 0) sysreturn(EINVAL);
if (!(argv = (char **)malloc((i+1)*sizeof(char *)))) sysreturn(ENOMEM);
argv[0] = TCL_VARARGS_START(char *,arg1,args);
for (i=1;;i++) {
argv[i] = va_arg(args,char *);
if (!argv[i]) break;
}
i = exp_spawnv(argv[0],argv+1);
free((char *)argv);
return(i);
}
int
exp_spawnfd(fd)
int fd;
{
if (!fd_new(fd)) {
errno = ENOMEM;
return -1;
}
return fd;
}
static int
rm_nulls(s,c)
char *s;
int c;
{
char *s2 = s;
int count = 0;
int i;
for (i=0;i<c;i++,s++) {
if (0 == *s) {
count++;
continue;
}
if (count) *s2 = *s;
s2++;
}
return(count);
}
static int i_read_errno;
static void
sigalarm_handler(n)
int n;
{
#ifdef REARM_SIG
signal(SIGALRM,sigalarm_handler);
#endif
longjmp(exp_readenv,1);
}
static int
i_read(fd,fp,buffer,length,timeout)
int fd;
FILE *fp;
char *buffer;
int length;
int timeout;
{
int cc = -2;
if (timeout > 0) alarm(timeout);
if (EXP_ABORT != setjmp(exp_readenv)) {
exp_reading = TRUE;
if (fd == -1) {
int c;
c = getc(fp);
if (c == EOF) {
if (feof(fp)) cc = 0;
else cc = -1;
} else {
buffer[0] = c;
cc = 1;
}
} else {
#ifndef HAVE_PTYTRAP
cc = read(fd,buffer,length);
#else
# include <sys/ptyio.h>
fd_set rdrs;
fd_set excep;
restart:
FD_ZERO(&rdrs);
FD_ZERO(&excep);
FD_SET(fd,&rdrs);
FD_SET(fd,&excep);
if (-1 == (cc = select(fd+1,
(SELECT_MASK_TYPE *)&rdrs,
(SELECT_MASK_TYPE *)0,
(SELECT_MASK_TYPE *)&excep,
(struct timeval *)0))) {
if (errno == EINTR) goto restart;
}
if (FD_ISSET(fd,&rdrs)) {
cc = read(fd,buffer,length);
} else if (FD_ISSET(fd,&excep)) {
struct request_info ioctl_info;
ioctl(fd,TIOCREQCHECK,&ioctl_info);
if (ioctl_info.request == TIOCCLOSE) {
cc = 0;
} else {
ioctl(fd, TIOCREQSET, &ioctl_info);
goto restart;
}
}
#endif
}
#if 0
else {
if (!(cc = fread(buffer,1,length,fp))) {
if (ferror(fp)) cc = -1;
}
}
#endif
i_read_errno = errno;
}
exp_reading = FALSE;
if (timeout > 0) alarm(0);
return(cc);
}
static int
expectv(fd,fp,ecases)
int fd;
FILE *fp;
struct exp_case *ecases;
{
int cc = 0;
int buf_length;
int old_length;
int first_time = TRUE;
int polled = 0;
struct exp_case *ec;
time_t current_time;
time_t end_time;
int remtime;
struct f *f;
int return_val;
int sys_error = 0;
#define return_normally(x) {return_val = x; goto cleanup;}
#define return_errno(x) {sys_error = x; goto cleanup;}
f = fdfp2f(fd,fp);
if (!f) return_errno(ENOMEM);
exp_buffer = f->buffer;
exp_buffer_end = f->buffer_end;
exp_match_end = f->match_end;
buf_length = exp_buffer_end - exp_match_end;
if (buf_length) {
memmove(exp_buffer,exp_match_end,buf_length);
}
exp_buffer_end = exp_buffer + buf_length;
*exp_buffer_end = '\0';
if (!ecases) return_errno(EINVAL);
for (ec=ecases;ec->type != exp_end;ec++) {
if ((ec->type == exp_regexp) && !ec->re) {
TclRegError((char *)0);
if (!(ec->re = TclRegComp(ec->pattern))) {
fprintf(stderr,"regular expression %s is bad: %s",ec->pattern,TclGetRegError());
return_errno(EINVAL);
}
}
}
bufsiz = 2*exp_match_max;
if (f->msize != bufsiz) {
if (buf_length > bufsiz) {
memmove(exp_buffer,exp_buffer+(buf_length - bufsiz)+1,
bufsiz-1);
buf_length = bufsiz-1;
}
exp_buffer = realloc(exp_buffer,bufsiz+1);
if (!exp_buffer) return_errno(ENOMEM);
exp_buffer[buf_length] = '\0';
exp_buffer_end = exp_buffer + buf_length;
f->msize = bufsiz;
}
if (fd == -1) fflush(fp);
if (exp_timeout != -1) signal(SIGALRM,sigalarm_handler);
remtime = exp_timeout;
time(¤t_time);
end_time = current_time + remtime;
for (;;) {
if (buf_length == bufsiz) {
int first_half, second_half;
if (exp_full_buffer) {
debuglog("expect: full buffer\r\n");
exp_match = exp_buffer;
exp_match_end = exp_buffer + buf_length;
exp_buffer_end = exp_match_end;
return_normally(EXP_FULLBUFFER);
}
first_half = bufsiz/2;
second_half = bufsiz - first_half;
memcpy(exp_buffer,exp_buffer+first_half,second_half);
buf_length = second_half;
exp_buffer_end = exp_buffer + second_half;
}
if (first_time) {
first_time = FALSE;
goto after_read;
}
if ((exp_timeout >= 0) && ((remtime < 0) || polled)) {
debuglog("expect: timeout\r\n");
exp_match_end = exp_buffer;
return_normally(EXP_TIMEOUT);
}
if (exp_timeout == 0) {
polled = 1;
}
cc = i_read(fd,fp,
exp_buffer_end,
bufsiz - buf_length,
remtime);
if (cc == 0) {
debuglog("expect: eof\r\n");
return_normally(EXP_EOF);
} else if (cc == -1) {
if (i_read_errno == EIO) {
debuglog("expect: eof\r\n");
return_normally(EXP_EOF);
}
debuglog("expect: error (errno = %d)\r\n",i_read_errno);
return_errno(i_read_errno);
} else if (cc == -2) {
debuglog("expect: timeout\r\n");
exp_match_end = exp_buffer;
return_normally(EXP_TIMEOUT);
}
old_length = buf_length;
buf_length += cc;
exp_buffer_end += buf_length;
if (logfile_all || (loguser && logfile)) {
fwrite(exp_buffer + old_length,1,cc,logfile);
}
if (loguser) fwrite(exp_buffer + old_length,1,cc,stdout);
if (debugfile) fwrite(exp_buffer + old_length,1,cc,debugfile);
if (debugfile) fflush(debugfile);
if (loguser) {
fflush(stdout);
if (logfile) fflush(logfile);
}
if (exp_remove_nulls) {
buf_length -= rm_nulls(exp_buffer + old_length, cc);
}
exp_buffer_end = exp_buffer + buf_length;
*exp_buffer_end = '\0';
exp_match_end = exp_buffer;
after_read:
debuglog("expect: does {%s} match ",exp_printify(exp_buffer));
for (ec=ecases;ec->type != exp_end;ec++) {
int matched = -1;
debuglog("{%s}? ",exp_printify(ec->pattern));
if (ec->type == exp_glob) {
int offset;
matched = Exp_StringMatch(exp_buffer,ec->pattern,&offset);
if (matched >= 0) {
exp_match = exp_buffer + offset;
exp_match_end = exp_match + matched;
}
} else if (ec->type == exp_exact) {
char *p = strstr(exp_buffer,ec->pattern);
if (p) {
matched = 1;
exp_match = p;
exp_match_end = p + strlen(ec->pattern);
}
} else if (ec->type == exp_null) {
char *p;
for (p=exp_buffer;p<exp_buffer_end;p++) {
if (*p == 0) {
matched = 1;
exp_match = p;
exp_match_end = p+1;
}
}
} else {
TclRegError((char *)0);
if (TclRegExec(ec->re,exp_buffer,exp_buffer)) {
matched = 1;
exp_match = ec->re->startp[0];
exp_match_end = ec->re->endp[0];
} else if (TclGetRegError()) {
fprintf(stderr,"r.e. match (pattern %s) failed: %s",ec->pattern,TclGetRegError());
}
}
if (matched != -1) {
debuglog("yes\nexp_buffer is {%s}\n",
exp_printify(exp_buffer));
return_normally(ec->value);
} else debuglog("no\n");
}
if (exp_timeout > 0) {
time(¤t_time);
remtime = end_time - current_time;
}
}
cleanup:
f->buffer = exp_buffer;
f->buffer_end = exp_buffer_end;
f->match_end = exp_match_end;
if (fd == -1) fflush(fp);
if (sys_error) {
errno = sys_error;
return -1;
}
return return_val;
}
int
exp_fexpectv(fp,ecases)
FILE *fp;
struct exp_case *ecases;
{
return(expectv(-1,fp,ecases));
}
int
exp_expectv(fd,ecases)
int fd;
struct exp_case *ecases;
{
return(expectv(fd,(FILE *)0,ecases));
}
int
exp_expectl TCL_VARARGS_DEF(int,arg1)
{
va_list args;
int fd;
struct exp_case *ec, *ecases;
int i;
enum exp_type type;
fd = TCL_VARARGS_START(int,arg1,args);
for (i=0;;i++) {
type = va_arg(args,enum exp_type);
if (type == exp_end) break;
if ((int)type < 0 || (int)type >= (int)exp_bogus) {
fprintf(stderr,"bad type (set %d) in exp_expectl\n",i);
sysreturn(EINVAL);
}
va_arg(args,char *);
if (type == exp_compiled) {
va_arg(args,regexp *);
}
va_arg(args,int);
}
va_end(args);
if (!(ecases = (struct exp_case *)
malloc((1+i)*sizeof(struct exp_case))))
sysreturn(ENOMEM);
fd = TCL_VARARGS_START(int,arg1,args);
for (ec=ecases;;ec++) {
ec->type = va_arg(args,enum exp_type);
if (ec->type == exp_end) break;
ec->pattern = va_arg(args,char *);
if (ec->type == exp_compiled) {
ec->re = va_arg(args,regexp *);
} else {
ec->re = 0;
}
ec->value = va_arg(args,int);
}
va_end(args);
i = expectv(fd,(FILE *)0,ecases);
for (ec=ecases;ec->type != exp_end;ec++) {
if (ec->type == exp_regexp) {
free((char *)ec->re);
}
}
free((char *)ecases);
return(i);
}
int
exp_fexpectl TCL_VARARGS_DEF(FILE *,arg1)
{
va_list args;
FILE *fp;
struct exp_case *ec, *ecases;
int i;
enum exp_type type;
fp = TCL_VARARGS_START(FILE *,arg1,args);
for (i=0;;i++) {
type = va_arg(args,enum exp_type);
if (type == exp_end) break;
if ((int)type < 0 || (int)type >= (int)exp_bogus) {
fprintf(stderr,"bad type (set %d) in exp_expectl\n",i);
sysreturn(EINVAL);
}
va_arg(args,char *);
if (type == exp_compiled) {
va_arg(args,regexp *);
}
va_arg(args,int);
}
va_end(args);
if (!(ecases = (struct exp_case *)
malloc((1+i)*sizeof(struct exp_case))))
sysreturn(ENOMEM);
#if 0
va_start(args);
va_arg(args,FILE *);
#endif
(void) TCL_VARARGS_START(FILE *,arg1,args);
for (ec=ecases;;ec++) {
ec->type = va_arg(args,enum exp_type);
if (ec->type == exp_end) break;
ec->pattern = va_arg(args,char *);
if (ec->type == exp_compiled) {
ec->re = va_arg(args,regexp *);
} else {
ec->re = 0;
}
ec->value = va_arg(args,int);
}
va_end(args);
i = expectv(-1,fp,ecases);
for (ec=ecases;ec->type != exp_end;ec++) {
if (ec->type == exp_regexp) {
free((char *)ec->re);
}
}
free((char *)ecases);
return(i);
}
FILE *
exp_popen(program)
char *program;
{
FILE *fp;
int ec;
if (0 > (ec = exp_spawnl("sh","sh","-c",program,(char *)0))) return(0);
if (!(fp = fdopen(ec,"r+"))) return(0);
setbuf(fp,(char *)0);
return(fp);
}
int
exp_disconnect()
{
int ttyfd;
#ifndef EALREADY
#define EALREADY 37
#endif
if (exp_disconnected) sysreturn(EALREADY);
exp_disconnected = TRUE;
freopen("/dev/null","r",stdin);
freopen("/dev/null","w",stdout);
freopen("/dev/null","w",stderr);
#ifdef POSIX
setsid();
#else
#ifdef SYSV3
setpgrp();
signal(SIGHUP,SIG_IGN);
if (fork()) exit(0);
#else
#ifdef MIPS_BSD
# include <sysv/sys.s>
syscall(SYS_setpgrp);
#endif
setpgrp(0,getpid());
#ifdef TIOCNOTTY
ttyfd = open("/dev/tty", O_RDWR);
if (ttyfd >= 0) {
(void) ioctl(ttyfd, TIOCNOTTY, (char *)0);
(void) close(ttyfd);
}
#endif
#endif
#endif
return(0);
}