zsh-users
 help / color / mirror / code / Atom feed
From: Vincent Lefevre <vincent@vinc17.org>
To: zsh-users@sunsite.dk
Subject: Re: coloring STDERR to terminal
Date: Sat, 31 Jul 2004 01:44:05 +0200	[thread overview]
Message-ID: <20040730234405.GS2892@ay.vinc17.org> (raw)
In-Reply-To: <20040730115024.GA25889@spiegl.de>

[-- Attachment #1: Type: text/plain, Size: 1086 bytes --]

On 2004-07-30 13:50:24 +0200, Andy Spiegl wrote:
> As you can see the question doesn't appear at all!

AFAIK, this is due to the "read line", that waits for a whole line.
When I reimplemented stderr coloring, I fixed that (and other problems
too) by writing a small C program "colorize" (attached) instead of zsh
code.

So, you should do the following:

  exec 2>>(colorize `tput bold; tput setaf 1` `tput sgr0` > /dev/tty &)

for instance, by putting that in your zshrc.

But I no longer use it since too many programs use stderr for things
other than error or warning messages, and sometimes not in a consistent
way. I don't know why, perhaps because developpers think that stderr
means tty (in case stdout is redirected); but if they want to write to
the tty, they should really do it instead of using stderr.

-- 
Vincent Lefèvre <vincent@vinc17.org> - Web: <http://www.vinc17.org/>
100% validated (X)HTML - Acorn / RISC OS / ARM, free software, YP17,
Championnat International des Jeux Mathématiques et Logiques, etc.
Work: CR INRIA - computer arithmetic / SPACES project at LORIA

[-- Attachment #2: colorize.c --]
[-- Type: text/plain, Size: 2687 bytes --]

/* $Id: colorize.c 3816 2004-07-03 17:01:32Z lefevre $
 *
 * Colorize the standard input. Written for zsh stderr coloring.
 */

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/types.h>

#define BUFFSIZE 512

static volatile sig_atomic_t usr1;

static void sigusr1(int sig)
{
  usr1 = 1;
}

static void writepid(char *tmpfile)
{
  FILE *f;

  f = fopen(tmpfile, "w");
  if (f == NULL)
    {
      perror("colorize (fopen)");
      exit(EXIT_FAILURE);
    }
  fprintf(f, "%ld\n", (long) getpid());
  if (fclose(f) != 0)
    {
      perror("colorize (fclose)");
      exit(EXIT_FAILURE);
    }
}

int main(int argc, char **argv)
{
  pid_t zshpid = 0;
  char *begstr, *endstr;
  fd_set rfds;
  int ret;

  if (argc != 3 && argc != 5)
    {
      fprintf(stderr,
              "Usage: colorize <begstr> <endstr> [ <zshpid> <tmpfile> ]\n");
      exit(EXIT_FAILURE);
    }

  /* Assume that the arguments are correct. Anyway, it is not possible
     to check them entirely. */
  begstr = argv[1];
  endstr = argv[2];
  if (argc == 5)
    {
      /* To do the synchronization with the zsh prompt output...
         Seems to be useless in practice, hence the argc == 3 case. */
      zshpid = atol(argv[3]);
      signal(SIGUSR1, sigusr1);
      writepid(argv[4]);
    }

  fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK);

  /* To watch stdin (fd 0). */
  FD_ZERO(&rfds);
  FD_SET(0, &rfds);

  for (;;)
    {
      ret = select(1, &rfds, NULL, NULL, NULL);

      if (ret < 0 && errno != EINTR)
        {
          perror("colorize (pselect)");
          exit(EXIT_FAILURE);
        }

      if (ret > 0)
        {
          static unsigned char buffer[BUFFSIZE];
          static int dontcol = 0;
          ssize_t n;

          while ((n = read(0, buffer, BUFFSIZE)) >= 0)
            {
              ssize_t i;

              if (n == 0)
                return 0;  /* stdin has been closed */
              for (i = 0; i < n; i++)
                {
                  if (buffer[i] == 27)
                    dontcol = 1;
                  if (buffer[i] == '\n')
                    dontcol = 0;
                  if (!dontcol)
                    fputs(begstr, stdout);
                  putchar(buffer[i]);
                  if (!dontcol)
                    fputs(endstr, stdout);
                }
            }
          fflush(stdout);
        }

      if (usr1)
        {
          usr1 = 0;
          if (kill(zshpid, SIGUSR1) != 0)
            {
              perror("colorize (kill)");
              exit(EXIT_FAILURE);
            }
        }
    }
}

      reply	other threads:[~2004-07-30 23:46 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-06-27 23:22 Atom 'Smasher'
2004-06-28  0:06 ` ari
2004-06-28  0:36   ` Atom 'Smasher'
2004-06-29 15:43 ` Bart Schaefer
2004-06-29 16:08   ` Vincent Lefevre
2004-06-29 17:14     ` Bart Schaefer
2004-06-30  7:09       ` Vincent Lefevre
2004-06-30 10:52         ` Bart Schaefer
2004-06-30 11:43           ` Vincent Lefevre
2004-06-30 12:01             ` Vincent Lefevre
2004-06-30 16:56             ` Bart Schaefer
2004-07-01 18:14               ` Vincent Lefevre
2004-07-02  0:11                 ` Bart Schaefer
2004-07-02 12:42                   ` Vincent Lefevre
2004-07-02 21:32                     ` Bart Schaefer
2004-07-20  9:10                     ` Atom 'Smasher'
2004-07-20 16:10                       ` Bart Schaefer
2004-07-20 19:27                         ` Atom 'Smasher'
2004-07-20 21:15                           ` Bart Schaefer
2004-07-20 23:30                             ` Wayne Davison
2004-07-21  3:15                               ` Bart Schaefer
2004-07-21  6:23                                 ` Wayne Davison
2004-07-21  7:30                                   ` Bart Schaefer
2004-07-21 13:19                                   ` Vincent Lefevre
2004-07-30 11:50                     ` Andy Spiegl
2004-07-30 23:44                       ` Vincent Lefevre [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20040730234405.GS2892@ay.vinc17.org \
    --to=vincent@vinc17.org \
    --cc=zsh-users@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).