tw.color.c revision 167465
1/* $Header: /p/tcsh/cvsroot/tcsh/tw.color.c,v 1.24 2006/03/02 18:46:45 christos Exp $ */
2/*
3 * tw.color.c: builtin color ls-F
4 */
5/*-
6 * Copyright (c) 1998 The Regents of the University of California.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 */
33#include "sh.h"
34
35RCSID("$tcsh: tw.color.c,v 1.24 2006/03/02 18:46:45 christos Exp $")
36
37#include "tw.h"
38#include "ed.h"
39#include "tc.h"
40
41#ifdef COLOR_LS_F
42
43typedef struct {
44    const char	 *s;
45    size_t len;
46} Str;
47
48
49#define VAR(suffix,variable,defaultcolor) \
50{ \
51    suffix, variable, { defaultcolor, sizeof(defaultcolor) - 1 }, \
52      { defaultcolor, sizeof(defaultcolor) - 1 } \
53}
54#define NOS '\0' /* no suffix */
55
56typedef struct {
57    const char suffix;
58    const char *variable;
59    Str	    color;
60    Str	    defaultcolor;
61} Variable;
62
63static 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) */
76#ifdef IS_ASCII
77    VAR(NOS, "lc", "\033["),	/* Left code (ASCII) */
78#else
79    VAR(NOS, "lc", "\x27["),	/* Left code (EBCDIC)*/
80#endif
81    VAR(NOS, "rc", "m"),	/* Right code */
82    VAR(NOS, "ec", ""),		/* End code (replaces lc+no+rc) */
83    VAR(NOS, "su", ""),		/* Setuid file (u+s) */
84    VAR(NOS, "sg", ""),		/* Setgid file (g+s) */
85    VAR(NOS, "tw", ""),		/* Sticky and other writable dir (+t,o+w) */
86    VAR(NOS, "ow", ""),		/* Other writable dir (o+w) but not sticky */
87    VAR(NOS, "st", ""),		/* Sticky dir (+t) but not other writable */
88};
89
90enum FileType {
91    VDir, VSym, VOrph, VPipe, VSock, VDoor, VBlock, VChr, VExe,
92    VFile, VNormal, VMiss, VLeft, VRight, VEnd
93};
94
95#define nvariables (sizeof(variables)/sizeof(variables[0]))
96
97typedef struct {
98    Str	    extension;	/* file extension */
99    Str	    color;	/* color string */
100} Extension;
101
102static Extension *extensions = NULL;
103static size_t nextensions = 0;
104
105static char *colors = NULL;
106int	     color_context_ls = FALSE;	/* do colored ls */
107static int  color_context_lsmF = FALSE; /* do colored ls-F */
108
109static int getstring (char **, const Char **, Str *, int);
110static void put_color (const Str *);
111static void print_color (const Char *, size_t, Char);
112
113/* set_color_context():
114 */
115void
116set_color_context(void)
117{
118    struct varent *vp = adrof(STRcolor);
119
120    if (vp == NULL || vp->vec == NULL) {
121	color_context_ls = FALSE;
122	color_context_lsmF = FALSE;
123    } else if (!vp->vec[0] || vp->vec[0][0] == '\0') {
124	color_context_ls = TRUE;
125	color_context_lsmF = TRUE;
126    } else {
127	size_t i;
128
129	color_context_ls = FALSE;
130	color_context_lsmF = FALSE;
131	for (i = 0; vp->vec[i]; i++)
132	    if (Strcmp(vp->vec[i], STRls) == 0)
133		color_context_ls = TRUE;
134	    else if (Strcmp(vp->vec[i], STRlsmF) == 0)
135		color_context_lsmF = TRUE;
136    }
137}
138
139
140/* getstring():
141 */
142static	int
143getstring(char **dp, const Char **sp, Str *pd, int f)
144{
145    const Char *s = *sp;
146    char *d = *dp;
147    eChar sc;
148
149    while (*s && (*s & CHAR) != (Char)f && (*s & CHAR) != ':') {
150	if ((*s & CHAR) == '\\' || (*s & CHAR) == '^') {
151	    if ((sc = parseescape(&s)) == CHAR_ERR)
152		return 0;
153	}
154	else
155	    sc = *s++ & CHAR;
156	d += one_wctomb(d, sc);
157    }
158
159    pd->s = *dp;
160    pd->len = d - *dp;
161    *sp = s;
162    *dp = d;
163    return *s == (Char)f;
164}
165
166
167/* parseLS_COLORS():
168 *	Parse the LS_COLORS environment variable
169 */
170void
171parseLS_COLORS(const Char *value)
172{
173    size_t  i, len;
174    const Char	 *v;		/* pointer in value */
175    char   *c;			/* pointer in colors */
176    Extension *volatile e;	/* pointer in extensions */
177    jmp_buf_t osetexit;
178    size_t omark;
179
180    (void) &e;
181
182    /* init */
183    xfree(extensions);
184    for (i = 0; i < nvariables; i++)
185	variables[i].color = variables[i].defaultcolor;
186    colors = NULL;
187    extensions = NULL;
188    nextensions = 0;
189
190    if (value == NULL)
191	return;
192
193    len = Strlen(value);
194    /* allocate memory */
195    i = 1;
196    for (v = value; *v; v++)
197	if ((*v & CHAR) == ':')
198	    i++;
199    extensions = xmalloc(len + i * sizeof(Extension));
200    colors = i * sizeof(Extension) + (char *)extensions;
201    nextensions = 0;
202
203    /* init pointers */
204    v = value;
205    c = colors;
206    e = &extensions[0];
207
208    /* Prevent from crashing if unknown parameters are given. */
209
210    omark = cleanup_push_mark();
211    getexit(osetexit);
212
213    if (setexit() == 0) {
214
215    /* parse */
216    while (*v) {
217	switch (*v & CHAR) {
218	case ':':
219	    v++;
220	    continue;
221
222	case '*':		/* :*ext=color: */
223	    v++;
224	    if (getstring(&c, &v, &e->extension, '=') &&
225		0 < e->extension.len) {
226		v++;
227		getstring(&c, &v, &e->color, ':');
228		e++;
229		continue;
230	    }
231	    break;
232
233	default:		/* :vl=color: */
234	    if (v[0] && v[1] && (v[2] & CHAR) == '=') {
235		for (i = 0; i < nvariables; i++)
236		    if ((Char)variables[i].variable[0] == (v[0] & CHAR) &&
237			(Char)variables[i].variable[1] == (v[1] & CHAR))
238			break;
239		if (i < nvariables) {
240		    v += 3;
241		    getstring(&c, &v, &variables[i].color, ':');
242		    continue;
243		}
244		else
245		    stderror(ERR_BADCOLORVAR, v[0], v[1]);
246	    }
247	    break;
248	}
249	while (*v && (*v & CHAR) != ':')
250	    v++;
251    }
252    }
253
254    cleanup_pop_mark(omark);
255    resexit(osetexit);
256
257    nextensions = e - extensions;
258}
259
260/* put_color():
261 */
262static void
263put_color(const Str *color)
264{
265    size_t  i;
266    const char	 *c = color->s;
267    int	   original_output_raw = output_raw;
268
269    output_raw = TRUE;
270    cleanup_push(&original_output_raw, output_raw_restore);
271    for (i = color->len; 0 < i; i--)
272	xputchar(*c++);
273    cleanup_until(&original_output_raw);
274}
275
276
277/* print_color():
278 */
279static void
280print_color(const Char *fname, size_t len, Char suffix)
281{
282    size_t  i;
283    char   *filename = short2str(fname);
284    char   *last = filename + len;
285    Str	   *color = &variables[VFile].color;
286
287    switch (suffix) {
288    case '>':			/* File is a symbolic link pointing to
289				 * a directory */
290	color = &variables[VDir].color;
291	break;
292    case '+':			/* File is a hidden directory [aix] or
293				 * context dependent [hpux] */
294    case ':':			/* File is network special [hpux] */
295	break;
296    default:
297	for (i = 0; i < nvariables; i++)
298	    if (variables[i].suffix != NOS &&
299		(Char)variables[i].suffix == suffix) {
300		color = &variables[i].color;
301		break;
302	    }
303	if (i == nvariables) {
304	    for (i = 0; i < nextensions; i++)
305		if (len >= extensions[i].extension.len
306		    && strncmp(last - extensions[i].extension.len,
307			       extensions[i].extension.s,
308			       extensions[i].extension.len) == 0) {
309		  color = &extensions[i].color;
310		break;
311	    }
312	}
313	break;
314    }
315
316    put_color(&variables[VLeft].color);
317    put_color(color);
318    put_color(&variables[VRight].color);
319}
320
321
322/* print_with_color():
323 */
324void
325print_with_color(const Char *filename, size_t len, Char suffix)
326{
327    if (color_context_lsmF &&
328	(haderr ? (didfds ? is2atty : isdiagatty) :
329	 (didfds ? is1atty : isoutatty))) {
330	print_color(filename, len, suffix);
331	xprintf("%S", filename);
332	if (0 < variables[VEnd].color.len)
333	    put_color(&variables[VEnd].color);
334	else {
335	    put_color(&variables[VLeft].color);
336	    put_color(&variables[VNormal].color);
337	    put_color(&variables[VRight].color);
338	}
339    }
340    else
341	xprintf("%S", filename);
342    xputwchar(suffix);
343}
344
345
346#endif /* COLOR_LS_F */
347