1 /* $Header: /src/pub/tcsh/tw.color.c,v 1.11 2002/06/25 19:02:11 christos Exp $ */
3 * tw.color.c: builtin color ls-F
6 * Copyright (c) 1998 The Regents of the University of California.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 RCSID("$Id: tw.color.c,v 1.11 2002/06/25 19:02:11 christos Exp $")
49 #define VAR(suffix,variable,defaultcolor) \
51 suffix, variable, { defaultcolor, sizeof(defaultcolor) - 1 }, \
52 { defaultcolor, sizeof(defaultcolor) - 1 } \
54 #define NOS '\0' /* no suffix */
63 static Variable variables
[] = {
64 VAR('/', "di", "01;34"), /* Directory */
65 VAR('@', "ln", "01;36"), /* Symbolic link */
66 VAR('&', "or", ""), /* Orphanned symbolic link (defaults to ln) */
67 VAR('|', "pi", "33"), /* Named pipe (FIFO) */
68 VAR('=', "so", "01;35"), /* Socket */
69 VAR('>', "do", "01;35"), /* Door (solaris fast ipc mechanism) */
70 VAR('#', "bd", "01;33"), /* Block device */
71 VAR('%', "cd", "01;33"), /* Character device */
72 VAR('*', "ex", "01;32"), /* Executable file */
73 VAR(NOS
, "fi", "0"), /* Regular file */
74 VAR(NOS
, "no", "0"), /* Normal (non-filename) text */
75 VAR(NOS
, "mi", ""), /* Missing file (defaults to fi) */
77 VAR(NOS
, "lc", "\033["), /* Left code (ASCII) */
79 VAR(NOS
, "lc", "\x27["), /* Left code (EBCDIC)*/
81 VAR(NOS
, "rc", "m"), /* Right code */
82 VAR(NOS
, "ec", ""), /* End code (replaces lc+no+rc) */
86 VDir
, VSym
, VOrph
, VPipe
, VSock
, VDoor
, VBlock
, VChr
, VExe
,
87 VFile
, VNormal
, VMiss
, VLeft
, VRight
, VEnd
90 #define nvariables (sizeof(variables)/sizeof(variables[0]))
93 Str extension
; /* file extension */
94 Str color
; /* color string */
97 static Extension
*extensions
= NULL
;
98 static int nextensions
= 0;
100 static char *colors
= NULL
;
101 bool color_context_ls
= FALSE
; /* do colored ls */
102 static bool color_context_lsmF
= FALSE
; /* do colored ls-F */
104 static bool getstring
__P((char **, const Char
**, Str
*, int));
105 static void put_color
__P((Str
*));
106 static void print_color
__P((Char
*, size_t, int));
108 /* set_color_context():
113 struct varent
*vp
= adrof(STRcolor
);
115 if (vp
== NULL
|| vp
->vec
== NULL
) {
116 color_context_ls
= FALSE
;
117 color_context_lsmF
= FALSE
;
118 } else if (!vp
->vec
[0] || vp
->vec
[0][0] == '\0') {
119 color_context_ls
= TRUE
;
120 color_context_lsmF
= TRUE
;
124 color_context_ls
= FALSE
;
125 color_context_lsmF
= FALSE
;
126 for (i
= 0; vp
->vec
[i
]; i
++)
127 if (Strcmp(vp
->vec
[i
], STRls
) == 0)
128 color_context_ls
= TRUE
;
129 else if (Strcmp(vp
->vec
[i
], STRlsmF
) == 0)
130 color_context_lsmF
= TRUE
;
138 getstring(dp
, sp
, pd
, f
)
139 char **dp
; /* dest buffer */
140 const Char
**sp
; /* source buffer */
141 Str
*pd
; /* pointer to dest buffer */
142 int f
; /* final character */
148 while (*s
&& (*s
& CHAR
) != f
&& (*s
& CHAR
) != ':') {
149 if ((*s
& CHAR
) == '\\' || (*s
& CHAR
) == '^') {
150 if ((sc
= parseescape(&s
)) == -1)
160 pd
->len
= (int) (d
- *dp
);
168 * Parse the LS_COLORS environment variable
171 parseLS_COLORS(value
)
172 Char
*value
; /* LS_COLOR variable's value */
176 const Char
*v
; /* pointer in value */
177 char *c
; /* pointer in colors */
178 Extension
*e
; /* pointer in extensions */
185 xfree((ptr_t
) extensions
);
186 for (i
= 0; i
< nvariables
; i
++)
187 variables
[i
].color
= variables
[i
].defaultcolor
;
196 /* allocate memory */
198 for (v
= value
; *v
; v
++)
199 if ((*v
& CHAR
) == ':')
201 extensions
= (Extension
*) xmalloc((size_t) (len
+ i
* sizeof(Extension
)));
202 colors
= i
* sizeof(Extension
) + (char *)extensions
;
210 /* Prevent from crashing if unknown parameters are given. */
214 if (setexit() == 0) {
223 case '*': /* :*ext=color: */
225 if (getstring(&c
, &v
, &e
->extension
, '=') &&
226 0 < e
->extension
.len
) {
228 getstring(&c
, &v
, &e
->color
, ':');
234 default: /* :vl=color: */
235 if (v
[0] && v
[1] && (v
[2] & CHAR
) == '=') {
236 for (i
= 0; i
< nvariables
; i
++)
237 if (variables
[i
].variable
[0] == (v
[0] & CHAR
) &&
238 variables
[i
].variable
[1] == (v
[1] & CHAR
))
240 if (i
< nvariables
) {
242 getstring(&c
, &v
, &variables
[i
].color
, ':');
246 stderror(ERR_BADCOLORVAR
, v
[0], v
[1]);
250 while (*v
&& (*v
& CHAR
) != ':')
257 nextensions
= (int) (e
- extensions
);
267 extern bool output_raw
; /* PWP: in sh.print.c */
270 bool original_output_raw
= output_raw
;
273 for (i
= color
->len
; 0 < i
; i
--)
275 output_raw
= original_output_raw
;
282 print_color(fname
, len
, suffix
)
288 char *filename
= short2str(fname
);
289 char *last
= filename
+ len
;
290 Str
*color
= &variables
[VFile
].color
;
293 case '>': /* File is a symbolic link pointing to
295 color
= &variables
[VDir
].color
;
297 case '+': /* File is a hidden directory [aix] or
298 * context dependent [hpux] */
299 case ':': /* File is network special [hpux] */
302 for (i
= 0; i
< nvariables
; i
++)
303 if (variables
[i
].suffix
!= NOS
&&
304 variables
[i
].suffix
== suffix
) {
305 color
= &variables
[i
].color
;
308 if (i
== nvariables
) {
309 for (i
= 0; i
< nextensions
; i
++)
310 if (strncmp(last
- extensions
[i
].extension
.len
,
311 extensions
[i
].extension
.s
,
312 extensions
[i
].extension
.len
) == 0) {
313 color
= &extensions
[i
].color
;
320 put_color(&variables
[VLeft
].color
);
322 put_color(&variables
[VRight
].color
);
326 /* print_with_color():
329 print_with_color(filename
, len
, suffix
)
334 if (color_context_lsmF
&&
335 (haderr
? (didfds
? is2atty
: isdiagatty
) :
336 (didfds
? is1atty
: isoutatty
))) {
337 print_color(filename
, len
, suffix
);
338 xprintf("%S", filename
);
339 if (0 < variables
[VEnd
].color
.len
)
340 put_color(&variables
[VEnd
].color
);
342 put_color(&variables
[VLeft
].color
);
343 put_color(&variables
[VNormal
].color
);
344 put_color(&variables
[VRight
].color
);
349 xprintf("%S%c", filename
, suffix
);
353 #endif /* COLOR_LS_F */