summaryrefslogtreecommitdiffstats
path: root/term-utils/script.c
diff options
context:
space:
mode:
authorSami Kerola2017-06-24 12:48:08 +0200
committerKarel Zak2017-06-26 15:23:31 +0200
commit8198052c9e8ee685c9405fae5f37e2a9e7572c0f (patch)
tree1f243f95f7d3709d9142b6e44dac3e4a603fbf90 /term-utils/script.c
parentuuidparse: add new command (diff)
downloadkernel-qcow2-util-linux-8198052c9e8ee685c9405fae5f37e2a9e7572c0f.tar.gz
kernel-qcow2-util-linux-8198052c9e8ee685c9405fae5f37e2a9e7572c0f.tar.xz
kernel-qcow2-util-linux-8198052c9e8ee685c9405fae5f37e2a9e7572c0f.zip
script: ensure typescript and timing errors do not break terminal
Earlier when typescript file failed new line after the error did not cause carriage return. Here is an example how prompt> travels to wrong place: prompt> script 0500-perms/typescript Script started, file is 0500-perms/typescript script: cannot open 0500-perms/typescript: Permission denied prompt> But that wasn't quite as bad as what happen with timing file, that at failure left terminal to state where a reset(1) run was needed. [kzak@redhat.com: - move code to restore_tty()] Signed-off-by: Sami Kerola <kerolasa@iki.fi> Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'term-utils/script.c')
-rw-r--r--term-utils/script.c29
1 files changed, 22 insertions, 7 deletions
diff --git a/term-utils/script.c b/term-utils/script.c
index f2fc2f59c..19e9976d9 100644
--- a/term-utils/script.c
+++ b/term-utils/script.c
@@ -190,12 +190,23 @@ static void die_if_link(const struct script_control *ctl)
"Program not started."), ctl->fname);
}
+static void restore_tty(struct script_control *ctl, int mode)
+{
+ struct termios rtt;
+
+ if (!ctl->isterm)
+ return;
+
+ rtt = ctl->attrs;
+ tcsetattr(STDIN_FILENO, mode, &rtt);
+}
+
static void __attribute__((__noreturn__)) done(struct script_control *ctl)
{
DBG(MISC, ul_debug("done!"));
- if (ctl->isterm)
- tcsetattr(STDIN_FILENO, TCSADRAIN, &ctl->attrs);
+ restore_tty(ctl, TCSADRAIN);
+
if (!ctl->quiet && ctl->typescriptfp)
printf(_("Script done, file is %s\n"), ctl->fname);
#ifdef HAVE_LIBUTEMPTER
@@ -423,15 +434,19 @@ static void do_io(struct script_control *ctl)
if ((ctl->typescriptfp =
fopen(ctl->fname, ctl->append ? "a" UL_CLOEXECSTR : "w" UL_CLOEXECSTR)) == NULL) {
+
+ restore_tty(ctl, TCSANOW);
warn(_("cannot open %s"), ctl->fname);
fail(ctl);
}
if (ctl->timing) {
- if (!ctl->tname) {
- if (!(ctl->timingfp = fopen("/dev/stderr", "w" UL_CLOEXECSTR)))
- err(EXIT_FAILURE, _("cannot open %s"), "/dev/stderr");
- } else if (!(ctl->timingfp = fopen(ctl->tname, "w" UL_CLOEXECSTR)))
- err(EXIT_FAILURE, _("cannot open %s"), ctl->tname);
+ const char *tname = ctl->tname ? ctl->tname : "/dev/stderr";
+
+ if (!(ctl->timingfp = fopen(tname, "w" UL_CLOEXECSTR))) {
+ restore_tty(ctl, TCSANOW);
+ warn(_("cannot open %s"), tname);
+ fail(ctl);
+ }
}