]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
script: add more info to script header
authorKarel Zak <kzak@redhat.com>
Mon, 14 May 2018 10:51:50 +0000 (12:51 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 14 May 2018 10:51:50 +0000 (12:51 +0200)
This patch introduces [...] to store extra information about terminal
to the typescript header. For example:

  Script started on 2018-05-14 12:52:32+02:00 [TERM="xterm-256color" TTY="/dev/pts/3" COLS="190" LINES="53"]

or

  Script started on 2018-05-14 12:54:01+02:00 [<not executed on terminal>]

if stdout is not terminal.

Addresses: https://github.com/karelzak/util-linux/issues/583
Signed-off-by: Karel Zak <kzak@redhat.com>
Documentation/TODO
include/ttyutils.h
lib/ttyutils.c
term-utils/script.c

index 2153a810c42a1096e4edc8a8eb64684258504749..4dbc288c980cefa6b7f78034fa52c41279bcfc88 100644 (file)
@@ -15,12 +15,6 @@ column
 
 script
 ------
-   - (!) add terminal type ($TERM), columns and lines to the header line, something like:
-
-               Script started on 2018-03-05 13:02:08+0100 [term="xterm-256color", lines=53, columns=190]
-
-     see https://github.com/karelzak/util-linux/issues/583
-
    - (!) add [exit=<command-exit-code>] to the "done" typescript message
 
    - think about optional "event" records in timing file to save information
index a9baab34bfe2a30225ab14cabba9c94921429a5e..b3ed4faf180296a20bffea011cdcd448f8039267 100644 (file)
@@ -52,6 +52,7 @@ struct chardata {
 
 extern int get_terminal_dimension(int *cols, int *lines);
 extern int get_terminal_width(int default_width);
+extern int get_terminal_type(const char **type);
 extern int get_terminal_name(const char **path, const char **name,
                             const char **number);
 
index 6a1e0e0f36992233dd01d4395afe8d7dd5ce0163..00a7903caba5ac88ed99382adaf98edcda0cbcd1 100644 (file)
@@ -113,6 +113,13 @@ int get_terminal_name(const char **path,
        return 0;
 }
 
+int get_terminal_type(const char **type)
+{
+       *type = getenv("TERM");
+       if (*type)
+               return -EINVAL;
+       return 0;
+}
 
 #ifdef TEST_PROGRAM_TTYUTILS
 # include <stdlib.h>
index 20c35d955a9b6ffad5f34885d2a285f8dd0d7b85..b7bec7e76084e4af0af4502dfc87a28e95a8ed2f 100644 (file)
@@ -182,6 +182,36 @@ static void __attribute__((__noreturn__)) usage(void)
        exit(EXIT_SUCCESS);
 }
 
+static void typescript_message_start(const struct script_control *ctl, time_t *tvec)
+{
+       char buf[FORMAT_TIMESTAMP_MAX];
+       int cols = 0, lines = 0;
+       const char *tty = NULL, *term = NULL;
+
+       if (!ctl->typescriptfp)
+               return;
+
+       strtime_iso(tvec, ISO_TIMESTAMP, buf, sizeof(buf));
+
+       fprintf(ctl->typescriptfp, _("Script started on %s ["), buf);
+
+       if (ctl->isterm) {
+               get_terminal_dimension(&cols, &lines);
+               get_terminal_name(&tty, NULL, NULL);
+               get_terminal_type(&term);
+
+               if (term)
+                       fprintf(ctl->typescriptfp, "TERM=\"%s\" ", term);
+               if (tty)
+                       fprintf(ctl->typescriptfp, "TTY=\"%s\" ", tty);
+
+               fprintf(ctl->typescriptfp, "COLS=\"%d\" LINES=\"%d\"", cols, lines);
+       } else
+               fprintf(ctl->typescriptfp, _("<not executed on terminal>"));
+
+       fputs("]\n", ctl->typescriptfp);
+}
+
 static void die_if_link(const struct script_control *ctl)
 {
        struct stat s;
@@ -496,11 +526,9 @@ static void do_io(struct script_control *ctl)
        }
 
 
-       if (ctl->typescriptfp) {
-               char buf[FORMAT_TIMESTAMP_MAX];
-               strtime_iso(&tvec, ISO_TIMESTAMP, buf, sizeof(buf));
-               fprintf(ctl->typescriptfp, _("Script started on %s\n"), buf);
-       }
+       if (ctl->typescriptfp)
+               typescript_message_start(ctl, &tvec);
+
        gettime_monotonic(&ctl->oldtime);
 
        while (!ctl->die) {