On Sun, Mar 17, 2019 at 9:30 AM Bruno Haible <br...@clisp.org> wrote: > Hi Jim, > > You wrote in > <https://lists.gnu.org/archive/html/bug-gnulib/2019-01/msg00173.html>: > > > I too would start with coreutils, noting that one must be careful to > > handle signals, so that the terminal is not left in a bad state when > > output is interrupted between a pair of set/reset escape sequences. > > https://git.savannah.gnu.org/cgit/coreutils.git/tree/src/ls.c#n1303 > > Today I was surprised to see that while grep and recent diffutils also > > have --color support, grep lacks the signal-handling code that is > > present in both coreutils and diffutils. > > Thank you for mentioning the need for signal handling. I have implemented > a reliable signal handling now, in libtextstyle-0.7: > https://alpha.gnu.org/gnu/gettext/libtextstyle-0.7.tar.gz > https://haible.de/bruno/gnu/libtextstyle-0.7.tar.gz > > And I am surprised to see that the color/signal handling in coreutils and > diffutils, that you mention as starting points, implement only part of > what is needed to make things reliable. > > In libtextstyle, I offer three levels of tty control to the application: > > 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_NONE is what 'grep' uses. > > TTYCTL_PARTIAL is what 'ls' and 'diff' use. > > TTYCTL_FULL is what libtextstyle offers by default, and is - in my testing - > the only approach that does not produce garbled output.
Nice work! Thank you for doing such a thorough job. ... > 'ls' and 'diff' do not mitigate the problems (4) and (5). > > > If you factor some of that code into gnulib, that would be a fine > > opportunity to use it to fix grep. > > Would you be prepared to use the 'term-ostream' module [1][2] in 'diff' and > 'grep'? > > Or would it be better to separate the pure signal handling and tty cleanup > code from the code that looks up the appropriate escape sequences from > termlib/termcap, and create a module in gnulib with only the signal handling > and tty cleanup code? I'm happy to use whichever approach you prefer, especially if you provide the patch.