From: Bruno Haible Date: Sun, 10 Feb 2019 02:01:13 +0000 (+0100) Subject: term-ostream: Prepare for robustness through signal handling. X-Git-Tag: v0.20~172 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6acda690d965a138e714d798d45e37ff807326ed;p=thirdparty%2Fgettext.git term-ostream: Prepare for robustness through signal handling. * gnulib-local/lib/term-ostream.oo.h (ttyctl_t): New type. (term_ostream_create): Add tty_control argument. * gnulib-local/lib/term-ostream.oo.c (term_ostream_create): Likewise. * gnulib-local/lib/term-styled-ostream.oo.h: Include term-ostream.h. (term_styled_ostream_create): Add tty_control argument. * gnulib-local/lib/term-styled-ostream.oo.c (term_styled_ostream_create): Likewise. * gnulib-local/tests/test-term-ostream.c (main): Use TTYCTL_AUTO. * gettext-tools/src/color.c (print_color_test): Likewise. * gettext-tools/src/write-catalog.c (msgdomain_list_print): Likewise. --- diff --git a/gettext-tools/src/color.c b/gettext-tools/src/color.c index b4451cd50..5f080c9ce 100644 --- a/gettext-tools/src/color.c +++ b/gettext-tools/src/color.c @@ -99,7 +99,7 @@ print_color_test () term_ostream_t stream; int i, row, col; - stream = term_ostream_create (1, "stdout"); + stream = term_ostream_create (1, "stdout", TTYCTL_AUTO); for (i = 0; i < 8; i++) colors[i].c = diff --git a/gettext-tools/src/write-catalog.c b/gettext-tools/src/write-catalog.c index 7090f0cf0..4bac4ba8e 100644 --- a/gettext-tools/src/write-catalog.c +++ b/gettext-tools/src/write-catalog.c @@ -246,7 +246,8 @@ message catalog has plural form translations, but the output format does not sup style_file_prepare ("PO_STYLE", "GETTEXTSTYLESDIR", relocate (GETTEXTSTYLESDIR), "po-default.css"); - stream = term_styled_ostream_create (fd, filename, style_file_name); + stream = + term_styled_ostream_create (fd, filename, TTYCTL_AUTO, style_file_name); if (stream == NULL) stream = fd_ostream_create (fd, filename, true); output_syntax->print (mdlp, stream, page_width, debug); diff --git a/gnulib-local/lib/term-ostream.oo.c b/gnulib-local/lib/term-ostream.oo.c index 5361ec301..b1753d98c 100644 --- a/gnulib-local/lib/term-ostream.oo.c +++ b/gnulib-local/lib/term-ostream.oo.c @@ -1,5 +1,5 @@ /* Output stream for attributed text, producing ANSI escape sequences. - Copyright (C) 2006-2008, 2017 Free Software Foundation, Inc. + Copyright (C) 2006-2008, 2017, 2019 Free Software Foundation, Inc. Written by Bruno Haible , 2006. This program is free software: you can redistribute it and/or modify @@ -1712,7 +1712,7 @@ xstrdup0 (const char *str) } term_ostream_t -term_ostream_create (int fd, const char *filename) +term_ostream_create (int fd, const char *filename, ttyctl_t tty_control) { term_ostream_t stream = XMALLOC (struct term_ostream_representation); const char *term; diff --git a/gnulib-local/lib/term-ostream.oo.h b/gnulib-local/lib/term-ostream.oo.h index 2e2f8c22c..336aff6b9 100644 --- a/gnulib-local/lib/term-ostream.oo.h +++ b/gnulib-local/lib/term-ostream.oo.h @@ -1,5 +1,5 @@ /* Output stream for attributed text, producing ANSI escape sequences. - Copyright (C) 2006 Free Software Foundation, Inc. + Copyright (C) 2006, 2019 Free Software Foundation, Inc. Written by Bruno Haible , 2006. This program is free software: you can redistribute it and/or modify @@ -89,6 +89,26 @@ methods: term_underline_t underline); }; +/* The amount of control to take over the underlying tty in order to avoid + garbled output on the screen, due to interleaved output of escape sequences + and output from the kernel (such as when the kernel echoes user's input + or when the kernel prints '^C' after the user pressed Ctrl-C). */ +typedef enum +{ + TTYCTL_AUTO = 0, /* Automatic best-possible choice. */ + TTYCTL_NONE, /* No control. + Result: Garbled output can occur, and the terminal can + be left in any state when the program is interrupted. */ + TTYCTL_PARTIAL, /* Signal handling. + Result: Garbled output can occur, but the terminal will + be left in the default state when the program is + interrupted. */ + TTYCTL_FULL /* Signal handling and disabling echo and flush-upon-signal. + Result: No garbled output, and the the terminal will + be left in the default state when the program is + interrupted. */ +} ttyctl_t; + #ifdef __cplusplus extern "C" { @@ -97,9 +117,11 @@ extern "C" { /* Create an output stream referring to the file descriptor FD. FILENAME is used only for error messages. + TTY_CONTROL specifies the amount of control to take over the underlying tty. The resulting stream will be line-buffered. Note that the resulting stream must be closed before FD can be closed. */ -extern term_ostream_t term_ostream_create (int fd, const char *filename); +extern term_ostream_t + term_ostream_create (int fd, const char *filename, ttyctl_t tty_control); #ifdef __cplusplus diff --git a/gnulib-local/lib/term-styled-ostream.oo.c b/gnulib-local/lib/term-styled-ostream.oo.c index d21486423..c0e7003ac 100644 --- a/gnulib-local/lib/term-styled-ostream.oo.c +++ b/gnulib-local/lib/term-styled-ostream.oo.c @@ -1,5 +1,5 @@ /* Output stream for CSS styled text, producing ANSI escape sequences. - Copyright (C) 2006-2007 Free Software Foundation, Inc. + Copyright (C) 2006-2007, 2019 Free Software Foundation, Inc. Written by Bruno Haible , 2006. This program is free software: you can redistribute it and/or modify @@ -598,7 +598,7 @@ term_styled_ostream::end_use_class (term_styled_ostream_t stream, /* Constructor. */ term_styled_ostream_t -term_styled_ostream_create (int fd, const char *filename, +term_styled_ostream_create (int fd, const char *filename, ttyctl_t tty_control, const char *css_filename) { term_styled_ostream_t stream = @@ -606,7 +606,7 @@ term_styled_ostream_create (int fd, const char *filename, CRStyleSheet *css_file_contents; stream->base.base.vtable = &term_styled_ostream_vtable; - stream->destination = term_ostream_create (fd, filename); + stream->destination = term_ostream_create (fd, filename, tty_control); if (cr_om_parser_simply_parse_file ((const guchar *) css_filename, CR_UTF_8, /* CR_AUTO is not supported */ diff --git a/gnulib-local/lib/term-styled-ostream.oo.h b/gnulib-local/lib/term-styled-ostream.oo.h index 8a6447a88..5918f2870 100644 --- a/gnulib-local/lib/term-styled-ostream.oo.h +++ b/gnulib-local/lib/term-styled-ostream.oo.h @@ -1,5 +1,5 @@ /* Output stream for CSS styled text, producing ANSI escape sequences. - Copyright (C) 2006 Free Software Foundation, Inc. + Copyright (C) 2006, 2019 Free Software Foundation, Inc. Written by Bruno Haible , 2006. This program is free software: you can redistribute it and/or modify @@ -19,6 +19,7 @@ #define _TERM_STYLED_OSTREAM_H #include "styled-ostream.h" +#include "term-ostream.h" struct term_styled_ostream : struct styled_ostream @@ -35,10 +36,12 @@ extern "C" { /* Create an output stream referring to the file descriptor FD, styled with the file CSS_FILENAME. FILENAME is used only for error messages. + TTY_CONTROL specifies the amount of control to take over the underlying tty. Note that the resulting stream must be closed before FD can be closed. Return NULL upon failure. */ extern term_styled_ostream_t term_styled_ostream_create (int fd, const char *filename, + ttyctl_t tty_control, const char *css_filename); diff --git a/gnulib-local/tests/test-term-ostream.c b/gnulib-local/tests/test-term-ostream.c index b8bdbd70b..211f60ead 100644 --- a/gnulib-local/tests/test-term-ostream.c +++ b/gnulib-local/tests/test-term-ostream.c @@ -26,7 +26,7 @@ main () term_ostream_t stream; int i, row, col; - stream = term_ostream_create (1, "stdout"); + stream = term_ostream_create (1, "stdout", TTYCTL_AUTO); for (i = 0; i < 8; i++) colors[i].c =