bug#24232: [PATCH] ls: postpone installation of signal handlers

2016-09-06 Thread Pádraig Brady
On 06/09/16 18:18, Jim Meyering wrote:
> On Tue, Sep 6, 2016 at 10:05 AM, Paul Eggert  wrote:
>> On 09/06/2016 08:59 AM, Pádraig Brady wrote:
>>>
>>> Will push later.
>>
>>
>> Before pushing, can you please change the name of "sigs" back to "sig"? I
>> prefer the old name, as "sig[i]" clearly means "signal i", whereas "sigs[i]"
>> is more confusing. (This is one of my pet peeves about array names.) Thanks.
> 
> I agree.
> A good argument for "why?" is that we optimize for readability of the
> more frequent use with an index, sig[i], rather than for the sole
> declaration, or the less frequent uses of the array name without an
> index.

Ok done.

I consolidated signal handler setup and restoration to a single function
to allow that.





bug#24232: [PATCH] ls: postpone installation of signal handlers

2016-09-06 Thread Jim Meyering
On Tue, Sep 6, 2016 at 10:05 AM, Paul Eggert  wrote:
> On 09/06/2016 08:59 AM, Pádraig Brady wrote:
>>
>> Will push later.
>
>
> Before pushing, can you please change the name of "sigs" back to "sig"? I
> prefer the old name, as "sig[i]" clearly means "signal i", whereas "sigs[i]"
> is more confusing. (This is one of my pet peeves about array names.) Thanks.

I agree.
A good argument for "why?" is that we optimize for readability of the
more frequent use with an index, sig[i], rather than for the sole
declaration, or the less frequent uses of the array name without an
index.





bug#24232: [PATCH] ls: postpone installation of signal handlers

2016-09-06 Thread Paul Eggert

On 09/06/2016 08:59 AM, Pádraig Brady wrote:

Will push later.


Before pushing, can you please change the name of "sigs" back to "sig"? 
I prefer the old name, as "sig[i]" clearly means "signal i", whereas 
"sigs[i]" is more confusing. (This is one of my pet peeves about array 
names.) Thanks.







bug#24232: [PATCH] ls: postpone installation of signal handlers

2016-09-06 Thread Pádraig Brady
On 06/09/16 16:38, Kamil Dudka wrote:
> ... until they are actually needed.  That is right before the first
> escape sequence is printed to a terminal.

Cool, that's a good improvement.
Note I moved the NEWS from "bugs" to "improvements".
Also I tweaked a long line to avoid `make syntax-check` failure.

Will push later.

thanks!
Pádraig






bug#24232: [PATCH] ls: postpone installation of signal handlers

2016-09-06 Thread Kamil Dudka
... until they are actually needed.  That is right before the first
escape sequence is printed to a terminal.

* src/ls.c (sigs): Moved from main() to global scope to make it
available in put_indicator(), too.  Renamed to prevent identifier
clashes with parameters of subsequent function definitions.
(nsigs): Likewise.
(caught_sig): Likewise.
(main): Code installing signal handlers moved to put_indicator() in
order not to keep default signal handling untouched as long as possible.
Adjusted condition for restoring signal handlers to reflect the change.
(put_indicator): Install signal handlers if called for the very first
time.  It uses the same code that was in main() prior to this commit.
* NEWS: Mention the change.

See https://bugzilla.redhat.com/1365933
---
 NEWS |   3 ++
 src/ls.c | 167 +++
 2 files changed, 86 insertions(+), 84 deletions(-)

diff --git a/NEWS b/NEWS
index 736b95e..d4af96f 100644
--- a/NEWS
+++ b/NEWS
@@ -18,6 +18,9 @@ GNU coreutils NEWS-*- 
outline -*-
   System V style platforms where this information is available only
   in the global variable 'tzname'. [bug introduced in coreutils-8.24]
 
+  ls is now fully responsive to signals until the first escape sequence is
+  written to a terminal.
+
   nl now resets numbering for each page section rather than just for each page.
   [This bug was present in "the beginning".]
 
diff --git a/src/ls.c b/src/ls.c
index 3572060..c438f61 100644
--- a/src/ls.c
+++ b/src/ls.c
@@ -753,6 +753,36 @@ static char const *long_time_format[2] =
 N_("%b %e %H:%M")
   };
 
+/* The signals that are trapped, and the number of such signals.  */
+static int const sigs[] =
+{
+  /* This one is handled specially.  */
+  SIGTSTP,
+
+  /* The usual suspects.  */
+  SIGALRM, SIGHUP, SIGINT, SIGPIPE, SIGQUIT, SIGTERM,
+#ifdef SIGPOLL
+  SIGPOLL,
+#endif
+#ifdef SIGPROF
+  SIGPROF,
+#endif
+#ifdef SIGVTALRM
+  SIGVTALRM,
+#endif
+#ifdef SIGXCPU
+  SIGXCPU,
+#endif
+#ifdef SIGXFSZ
+  SIGXFSZ,
+#endif
+};
+enum { nsigs = ARRAY_CARDINALITY (sigs) };
+
+#if ! SA_NOCLDSTOP
+static bool caught_sig[nsigs];
+#endif
+
 /* The set of signals that are caught.  */
 
 static sigset_t caught_signals;
@@ -1251,36 +1281,6 @@ main (int argc, char **argv)
   struct pending *thispend;
   int n_files;
 
-  /* The signals that are trapped, and the number of such signals.  */
-  static int const sig[] =
-{
-  /* This one is handled specially.  */
-  SIGTSTP,
-
-  /* The usual suspects.  */
-  SIGALRM, SIGHUP, SIGINT, SIGPIPE, SIGQUIT, SIGTERM,
-#ifdef SIGPOLL
-  SIGPOLL,
-#endif
-#ifdef SIGPROF
-  SIGPROF,
-#endif
-#ifdef SIGVTALRM
-  SIGVTALRM,
-#endif
-#ifdef SIGXCPU
-  SIGXCPU,
-#endif
-#ifdef SIGXFSZ
-  SIGXFSZ,
-#endif
-};
-  enum { nsigs = ARRAY_CARDINALITY (sig) };
-
-#if ! SA_NOCLDSTOP
-  bool caught_sig[nsigs];
-#endif
-
   initialize_main (&argc, &argv);
   set_program_name (argv[0]);
   setlocale (LC_ALL, "");
@@ -1314,46 +1314,6 @@ main (int argc, char **argv)
   || (is_colored (C_EXEC) && color_symlink_as_referent)
   || (is_colored (C_MISSING) && format == long_format))
 check_symlink_color = true;
-
-  /* If the standard output is a controlling terminal, watch out
- for signals, so that the colors can be restored to the
- default state if "ls" is suspended or interrupted.  */
-
-  if (0 <= tcgetpgrp (STDOUT_FILENO))
-{
-  int j;
-#if SA_NOCLDSTOP
-  struct sigaction act;
-
-  sigemptyset (&caught_signals);
-  for (j = 0; j < nsigs; j++)
-{
-  sigaction (sig[j], NULL, &act);
-  if (act.sa_handler != SIG_IGN)
-sigaddset (&caught_signals, sig[j]);
-}
-
-  act.sa_mask = caught_signals;
-  act.sa_flags = SA_RESTART;
-
-  for (j = 0; j < nsigs; j++)
-if (sigismember (&caught_signals, sig[j]))
-  {
-act.sa_handler = sig[j] == SIGTSTP ? stophandler : sighandler;
-sigaction (sig[j], &act, NULL);
-  }
-#else
-  for (j = 0; j < nsigs; j++)
-{
-  caught_sig[j] = (signal (sig[j], SIG_IGN) != SIG_IGN);
-  if (caught_sig[j])
-{
-  signal (sig[j], sig[j] == SIGTSTP ? stophandler : 
sighandler);
-  siginterrupt (sig[j], 0);
-}
-}
-#endif
-}
 }
 
   if (dereference == DEREF_UNDEFINED)
@@ -1466,31 +1426,29 @@ main (int argc, char **argv)
   print_dir_name = true;
 }
 
-  if (print_with_color)
+  if (print_with_color && used_color)
 {
   int j;
 
-  if (used_color)
-{
-  /* Skip the restore when it would be a no-op, i.e.,
- when left is "\033[" and right is "m".  */
-  if (!(color_indicator[C_LEFT].len == 2
-&& m