add more features found in other unices

This commit is contained in:
christos 2009-10-17 19:05:54 +00:00
parent 63b86ac36f
commit ab107e386c
2 changed files with 62 additions and 23 deletions

View File

@ -1,4 +1,4 @@
.\" $NetBSD: script.1,v 1.10 2003/08/07 11:15:48 agc Exp $
.\" $NetBSD: script.1,v 1.11 2009/10/17 19:05:54 christos Exp $
.\"
.\" Copyright (c) 1980, 1990, 1993
.\" The Regents of the University of California. All rights reserved.
@ -29,7 +29,7 @@
.\"
.\" @(#)script.1 8.1 (Berkeley) 6/6/93
.\"
.Dd June 6, 1993
.Dd October 17, 2009
.Dt SCRIPT 1
.Os
.Sh NAME
@ -37,7 +37,8 @@
.Nd make typescript of terminal session
.Sh SYNOPSIS
.Nm
.Op Fl adpr
.Op Fl adfpqr
.Op Fl c Ar command
.Op Ar file
.Sh DESCRIPTION
.Nm
@ -64,10 +65,21 @@ Append the output to
or
.Pa typescript ,
retaining the prior contents.
.It Fl c Ar command
Run the named
.Ar command
instead of the shell.
Useful for capturing the output of a program that behaves differently when
associated with a tty.
.It Fl d
Don't sleep between records when playing back a timestamped session.
.It Fl f
Flush output after each write. This is useful for watching the script
output in real time.
.It Fl p
Play back a recorded session in real time.
.It Fl q
Be quiet, and don't output started and ended lines.
.It Fl r
Record a session with input, output, and timestamping.
.El

View File

@ -1,4 +1,4 @@
/* $NetBSD: script.c,v 1.17 2009/04/13 07:15:32 lukem Exp $ */
/* $NetBSD: script.c,v 1.18 2009/10/17 19:05:54 christos Exp $ */
/*
* Copyright (c) 1980, 1992, 1993
@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1980, 1992, 1993\
#if 0
static char sccsid[] = "@(#)script.c 8.1 (Berkeley) 6/6/93";
#endif
__RCSID("$NetBSD: script.c,v 1.17 2009/04/13 07:15:32 lukem Exp $");
__RCSID("$NetBSD: script.c,v 1.18 2009/10/17 19:05:54 christos Exp $");
#endif /* not lint */
#include <sys/types.h>
@ -78,13 +78,14 @@ int master, slave;
int child, subchild;
int outcc;
int usesleep, rawout;
int quiet, flush;
const char *fname;
struct termios tt;
void done(void);
void dooutput(void);
void doshell(void);
void doshell(const char *);
void fail(void);
void finish(int);
int main(int, char **);
@ -101,28 +102,42 @@ main(int argc, char *argv[])
struct winsize win;
int aflg, pflg, ch;
char ibuf[BUFSIZ];
const char *command;
aflg = 0;
pflg = 0;
usesleep = 1;
rawout = 0;
while ((ch = getopt(argc, argv, "adpr")) != -1)
quiet = 0;
flush = 0;
command = NULL;
while ((ch = getopt(argc, argv, "ac:dfpqr")) != -1)
switch(ch) {
case 'a':
aflg = 1;
break;
case 'c':
command = optarg;
break;
case 'd':
usesleep = 0;
break;
case 'f':
flush = 1;
break;
case 'p':
pflg = 1;
break;
case 'q':
quiet = 1;
break;
case 'r':
rawout = 1;
break;
case '?':
default:
(void)fprintf(stderr, "usage: %s [-adpr] [file]\n",
(void)fprintf(stderr,
"Usage: %s [-c <command>][-adfpqr] [file]\n",
getprogname());
exit(1);
}
@ -145,6 +160,7 @@ main(int argc, char *argv[])
if (openpty(&master, &slave, NULL, &tt, &win) == -1)
err(1, "openpty");
if (!quiet)
(void)printf("Script started, output file is %s\n", fname);
rtt = tt;
cfmakeraw(&rtt);
@ -166,7 +182,7 @@ main(int argc, char *argv[])
if (child)
dooutput();
else
doshell();
doshell(command);
}
if (!rawout)
@ -196,7 +212,7 @@ finish(int signo)
}
void
dooutput()
dooutput(void)
{
struct itimerval value;
int cc;
@ -207,7 +223,7 @@ dooutput()
tvec = time(NULL);
if (rawout)
record(fscript, NULL, 0, 's');
else
else if (!quiet)
(void)fprintf(fscript, "Script started on %s", ctime(&tvec));
(void)signal(SIGALRM, scriptflush);
@ -225,6 +241,8 @@ dooutput()
else
(void)fwrite(obuf, 1, cc, fscript);
outcc += cc;
if (flush)
(void)fflush(fscript);
}
done();
}
@ -239,19 +257,23 @@ scriptflush(int signo)
}
void
doshell()
doshell(const char *command)
{
const char *shell;
shell = getenv("SHELL");
if (shell == NULL)
shell = _PATH_BSHELL;
(void)close(master);
(void)fclose(fscript);
login_tty(slave);
if (command == NULL) {
shell = getenv("SHELL");
if (shell == NULL)
shell = _PATH_BSHELL;
execl(shell, shell, "-i", NULL);
warn("execl %s", shell);
command = shell;
} else
execlp(command, command, NULL);
warn("execl %s", command);
fail();
}
@ -272,13 +294,14 @@ done()
tvec = time(NULL);
if (rawout)
record(fscript, NULL, 0, 'e');
else
else if (!quiet)
(void)fprintf(fscript,"\nScript done on %s",
ctime(&tvec));
(void)fclose(fscript);
(void)close(master);
} else {
(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
if (!quiet)
(void)printf("Script done, output file is %s\n", fname);
}
exit(0);
@ -370,12 +393,16 @@ playback(FILE *fp)
switch (stamp.scr_direction) {
case 's':
(void)printf("Script started on %s", ctime(&tclock));
if (!quiet)
(void)printf("Script started on %s",
ctime(&tclock));
tsi = tso;
(void)consume(fp, stamp.scr_len, buf, reg);
break;
case 'e':
(void)printf("\nScript done on %s", ctime(&tclock));
if (!quiet)
(void)printf("\nScript done on %s",
ctime(&tclock));
(void)consume(fp, stamp.scr_len, buf, reg);
break;
case 'i':