From rse@en1.engelschall.com  Tue Aug  8 05:55:14 2000
Return-Path: <rse@en1.engelschall.com>
Received: from slarti.muc.de (slarti.muc.de [193.149.48.10])
	by hub.freebsd.org (Postfix) with SMTP id B700337B59E
	for <FreeBSD-gnats-submit@freebsd.org>; Tue,  8 Aug 2000 05:55:09 -0700 (PDT)
	(envelope-from rse@en1.engelschall.com)
Received: (qmail 28606 invoked by uid 66); 8 Aug 2000 13:03:43 -0000
Received: from en by slarti with UUCP; Tue Aug  8 13:03:43 2000 -0000
Received: by en1.engelschall.com (Sendmail 8.11.0+)
	id e78CsKx72276; Tue, 8 Aug 2000 14:54:20 +0200 (CEST)
Message-Id: <200008081254.e78CsKx72276@en1.engelschall.com>
Date: Tue, 8 Aug 2000 14:54:20 +0200 (CEST)
From: "Ralf S. Engelschall" <rse@en1.engelschall.com>
Reply-To: rse@engelschall.com
To: FreeBSD-gnats-submit@freebsd.org
Subject: allowing old colors+paging combination with ls(1) again
X-Send-Pr-Version: 3.2

>Number:         20483
>Category:       bin
>Synopsis:       allowing old colors+paging combination with ls(1) again
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    joe
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Tue Aug 08 06:00:01 PDT 2000
>Closed-Date:    Fri Aug 18 02:17:55 PDT 2000
>Last-Modified:  Fri Aug 18 02:18:44 PDT 2000
>Originator:     Ralf S. Engelschall
>Release:        FreeBSD 4.1-STABLE i386
>Organization:
Engelschall, Germany.
>Environment:

FreeBSD en1.engelschall.com 4.1-STABLE FreeBSD 4.1-STABLE #0: Mon Aug  7
21:02:25 CEST 2000     rse@en1.engelschall.com:/usr/src/sys/compile/EN1  i386

>Description:

Since years I used the following stuff in my shell to get three directory
listing commands which both use color and (if any only if the listing is too
large) also the pager.

    ls () { colorls -G -C "$@"    | less -E -r }
    ll () { colorls -G -l "$@"    | less -E -r }
    lx () { colorls -G -l -a "$@" | less -E -r }

As you now, colorls and less were the beast from our ports, -G switched on the
colors and less' -E -r options did the magic for display: -E caused less to
automatically exit the first time it reached end-of-file, and -r forced less
to display the color sequences.

Now with FreeBSD 4.1 we got the nice color support built into our /bin/ls.
Fine, except for the problem that the great colors+paging combination is no
longer possible:

    ls () { /bin/ls -G -C "$@"    | less -E -r }
    ll () { /bin/ls -G -l "$@"    | less -E -r }
    lx () { /bin/ls -G -l -a "$@" | less -E -r }

The result is that all colors are gone. The reason is a simple isatty(3) call
in src/bin/ls/ls.c, line 205, which simple lets /bin/ls honor -G only if
stdout is connected to a tty. Hmmm... obviously a useful check, because things
like "ls *.c >filelist", etc. should not contain any color sequences.  But
OTOH this check seems a little-bit too restrictive and magic, because if I add
-G I really mean -G. And if I don't want any colors in "ls *.c
>How-To-Repeat:

Run "/bin/ls -G -C | less -E -r" and recognize that colors are gone,
because the pipe means ls' stdout is no longer connected to a tty.

>Fix:

I propose either this patch:

Index: ls.c
===================================================================
RCS file: /home/ncvs/src/bin/ls/ls.c,v
retrieving revision 1.42
diff -u -d -r1.42 ls.c
--- ls.c	2000/07/04 23:09:23	1.42
+++ ls.c	2000/08/08 12:44:55
@@ -202,7 +202,6 @@
 		        fts_options |= FTS_COMFOLLOW;
 			break;
 		case 'G':
-			if (isatty(STDOUT_FILENO))
 #ifdef COLORLS
 				if (tgetent(termcapbuf, getenv("TERM")) == 1) {
 					ansi_fgcol = tgetstr("AF", &bp);

or alternatively the following patch:

Index: ls.c
===================================================================
RCS file: /home/ncvs/src/bin/ls/ls.c,v
retrieving revision 1.42
diff -u -d -r1.42 ls.c
--- ls.c	2000/07/04 23:09:23	1.42
+++ ls.c	2000/08/08 12:47:57
@@ -202,7 +202,7 @@
 		        fts_options |= FTS_COMFOLLOW;
 			break;
 		case 'G':
-			if (isatty(STDOUT_FILENO))
+			if (isatty(STDOUT_FILENO) || getenv("LSCOLORS_FORCED") != NULL)
 #ifdef COLORLS
 				if (tgetent(termcapbuf, getenv("TERM")) == 1) {
 					ansi_fgcol = tgetstr("AF", &bp);

With the second patch, compatibility to the original intention ("be magic in
case of non-tty") is preserved and one is at least able to use the following
to achieve the old colors+paging combination:

    ls () { LSCOLORS_FORCED=1 /bin/ls -G -C "$@"    | less -E -r }
    ll () { LSCOLORS_FORCED=1 /bin/ls -G -l "$@"    | less -E -r }
    lx () { LSCOLORS_FORCED=1 /bin/ls -G -l -a "$@" | less -E -r }


>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->joe 
Responsible-Changed-By: joe 
Responsible-Changed-When: Tue Aug 8 06:52:02 PDT 2000 
Responsible-Changed-Why:  
I'll take this one 

http://www.freebsd.org/cgi/query-pr.cgi?pr=20483 

From: Sheldon Hearn <sheldonh@uunet.co.za>
To: joe@FreeBSD.ORG
Cc: freebsd-gnats-submit@FreeBSD.org
Subject: Re: bin/20483: allowing old colors+paging combination with ls(1) again 
Date: Tue, 08 Aug 2000 16:44:50 +0200

 On Tue, 08 Aug 2000 06:52:23 MST, joe@FreeBSD.ORG wrote:
 
 > Synopsis: allowing old colors+paging combination with ls(1) again
 > 
 > Responsible-Changed-From-To: freebsd-bugs->joe
 > Responsible-Changed-By: joe
 > Responsible-Changed-When: Tue Aug 8 06:52:02 PDT 2000
 > Responsible-Changed-Why: 
 > I'll take this one
 
 Looks closely related to PR 20291, which also includes a patch.
 
 Ciao,
 Sheldon.
 
State-Changed-From-To: open->suspended 
State-Changed-By: joe 
State-Changed-When: Sat Aug 12 15:42:30 PDT 2000 
State-Changed-Why:  
This is now fixed in -current. 

Colour support is enabled using the CLICOLOR variable instead of 
the -G flag (which has been depricated).  In addition you can 
force ls to output colour sequences if the output isn't directed 
to a tty by defining CLICOLOR_FORCE. 

Your fix is therefore: 

CLICOLOR=1; export CLICOLOR 
ls () { CLICOLOR_FORCE=1 /bin/ls -C "$@"    | less -E -r } 
ll () { CLICOLOR_FORCE=1 /bin/ls -l "$@"    | less -E -r } 
lx () { CLICOLOR_FORCE=1 /bin/ls -l -a "$@" | less -E -r } 

... or at least it will be when I backport the change to 4.x. 

This PR will remain suspended until the MFC has been done. 


http://www.freebsd.org/cgi/query-pr.cgi?pr=20483 
State-Changed-From-To: suspended->closed 
State-Changed-By: joe 
State-Changed-When: Fri Aug 18 02:17:55 PDT 2000 
State-Changed-Why:  
The CLICOLOR and CLICOLOR_FORCE environment changes have 
been backported to RELENG_4. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=20483 
>Unformatted:
 >filelist" I should be able to type "/bin/ls *.c >filelist" or
 I better should avoid playing around with overridden commands. 
 
 So, I would appreciate that we either just simply remove the isatty(3) check
 in ls.c because we treat it too extreme. Or we at least add another flag (oh
 no, not one more ;) or (certainly better) an env variable to ls(1) which
 forces the use of colors.
 
