diff options
| author | Simon J. Gerraty <sjg@FreeBSD.org> | 2022-03-09 21:33:03 +0000 |
|---|---|---|
| committer | Dag-Erling Smørgrav <des@FreeBSD.org> | 2022-11-02 09:44:46 +0000 |
| commit | 9ba17e2d1c2ffc9d00ce020d53523365a0d85f1b (patch) | |
| tree | b6b47dfef2764fa0c1c10466b25be2fdf16c4e17 /usr.bin/script | |
| parent | 7302f8697124e5b3b24b03bbea5d0e0eaa69c6fc (diff) | |
Diffstat (limited to 'usr.bin/script')
| -rw-r--r-- | usr.bin/script/script.1 | 18 | ||||
| -rw-r--r-- | usr.bin/script/script.c | 43 |
2 files changed, 51 insertions, 10 deletions
diff --git a/usr.bin/script/script.1 b/usr.bin/script/script.1 index 04fa75fc1612..36523148fa90 100644 --- a/usr.bin/script/script.1 +++ b/usr.bin/script/script.1 @@ -28,7 +28,7 @@ .\" @(#)script.1 8.1 (Berkeley) 6/6/93 .\" $FreeBSD$ .\" -.Dd January 5, 2021 +.Dd March 9, 2022 .Dt SCRIPT 1 .Os .Sh NAME @@ -43,6 +43,7 @@ .Nm .Fl p .Op Fl deq +.Op Fl T Ar fmt .Op Ar file .Sh DESCRIPTION The @@ -119,6 +120,21 @@ causes to flush after every character I/O event. The default interval is 30 seconds. +.It Fl T Ar fmt +Implies +.Fl p , +but just reports the time-stamp of each output. +This is very useful for assessing the timing of events. +.Pp +If +.Ar fmt +does not contain any +.Ql % +characters, it indicates the default format: +.Ql %n@ %s [%Y-%m-%d %T]%n , +which is useful for both tools and humans to read, should be used. +Note that time-stamps will only be output when different from the +previous one. .El .Pp The script ends when the forked shell (or command) exits (a diff --git a/usr.bin/script/script.c b/usr.bin/script/script.c index 9c18dc73390f..430f48ad63df 100644 --- a/usr.bin/script/script.c +++ b/usr.bin/script/script.c @@ -89,6 +89,13 @@ static TAILQ_HEAD(, buf_elm) obuf_list = TAILQ_HEAD_INITIALIZER(obuf_list); static struct termios tt; +#ifndef TSTAMP_FMT +/* useful for tool and human reading */ +# define TSTAMP_FMT "%n@ %s [%Y-%m-%d %T]%n" +#endif +static const char *tstamp_fmt = TSTAMP_FMT; +static int tflg; + static void done(int) __dead2; static void doshell(char **); static void finish(void); @@ -121,7 +128,7 @@ main(int argc, char *argv[]) warning. (not needed w/clang) */ showexit = 0; - while ((ch = getopt(argc, argv, "adeFfkpqrt:")) != -1) + while ((ch = getopt(argc, argv, "adeFfkpqrT:t:")) != -1) switch(ch) { case 'a': aflg = 1; @@ -154,6 +161,11 @@ main(int argc, char *argv[]) if (flushtime < 0) err(1, "invalid flush time %d", flushtime); break; + case 'T': + tflg = pflg = 1; + if (strchr(optarg, '%')) + tstamp_fmt = optarg; + break; case '?': default: usage(); @@ -511,12 +523,14 @@ playback(FILE *fp) off_t nread, save_len; size_t l; time_t tclock; + time_t lclock; int reg; if (fstat(fileno(fp), &pst) == -1) err(1, "fstat failed"); reg = S_ISREG(pst.st_mode); + lclock = 0; for (nread = 0; !reg || nread < pst.st_size; nread += save_len) { if (fread(&stamp, sizeof(stamp), 1, fp) != 1) { @@ -559,15 +573,26 @@ playback(FILE *fp) (void)consume(fp, stamp.scr_len, buf, reg); break; case 'o': - tsi.tv_sec = tso.tv_sec - tsi.tv_sec; - tsi.tv_nsec = tso.tv_nsec - tsi.tv_nsec; - if (tsi.tv_nsec < 0) { - tsi.tv_sec -= 1; - tsi.tv_nsec += 1000000000; + if (tflg) { + if (stamp.scr_len == 0) + continue; + if (tclock - lclock > 0) { + l = strftime(buf, sizeof buf, tstamp_fmt, + localtime(&tclock)); + (void)write(STDOUT_FILENO, buf, l); + } + lclock = tclock; + } else { + tsi.tv_sec = tso.tv_sec - tsi.tv_sec; + tsi.tv_nsec = tso.tv_nsec - tsi.tv_nsec; + if (tsi.tv_nsec < 0) { + tsi.tv_sec -= 1; + tsi.tv_nsec += 1000000000; + } + if (usesleep) + (void)nanosleep(&tsi, NULL); + tsi = tso; } - if (usesleep) - (void)nanosleep(&tsi, NULL); - tsi = tso; while (stamp.scr_len > 0) { l = MIN(DEF_BUF, stamp.scr_len); if (fread(buf, sizeof(char), l, fp) != l) |
