tw.color.c revision 167465
1167465Smp/* $Header: /p/tcsh/cvsroot/tcsh/tw.color.c,v 1.24 2006/03/02 18:46:45 christos Exp $ */
259243Sobrien/*
359243Sobrien * tw.color.c: builtin color ls-F
459243Sobrien */
559243Sobrien/*-
659243Sobrien * Copyright (c) 1998 The Regents of the University of California.
759243Sobrien * All rights reserved.
859243Sobrien *
959243Sobrien * Redistribution and use in source and binary forms, with or without
1059243Sobrien * modification, are permitted provided that the following conditions
1159243Sobrien * are met:
1259243Sobrien * 1. Redistributions of source code must retain the above copyright
1359243Sobrien *    notice, this list of conditions and the following disclaimer.
1459243Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1559243Sobrien *    notice, this list of conditions and the following disclaimer in the
1659243Sobrien *    documentation and/or other materials provided with the distribution.
17100616Smp * 3. Neither the name of the University nor the names of its contributors
1859243Sobrien *    may be used to endorse or promote products derived from this software
1959243Sobrien *    without specific prior written permission.
2059243Sobrien *
2159243Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2259243Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2359243Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2459243Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2559243Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2659243Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2759243Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2859243Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2959243Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3059243Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3159243Sobrien * SUCH DAMAGE.
3259243Sobrien */
3359243Sobrien#include "sh.h"
3459243Sobrien
35167465SmpRCSID("$tcsh: tw.color.c,v 1.24 2006/03/02 18:46:45 christos Exp $")
3659243Sobrien
3759243Sobrien#include "tw.h"
3859243Sobrien#include "ed.h"
3959243Sobrien#include "tc.h"
4059243Sobrien
4159243Sobrien#ifdef COLOR_LS_F
4259243Sobrien
4359243Sobrientypedef struct {
44167465Smp    const char	 *s;
45167465Smp    size_t len;
4659243Sobrien} Str;
4759243Sobrien
4859243Sobrien
4959243Sobrien#define VAR(suffix,variable,defaultcolor) \
5059243Sobrien{ \
5159243Sobrien    suffix, variable, { defaultcolor, sizeof(defaultcolor) - 1 }, \
5259243Sobrien      { defaultcolor, sizeof(defaultcolor) - 1 } \
5359243Sobrien}
5459243Sobrien#define NOS '\0' /* no suffix */
5559243Sobrien
5659243Sobrientypedef struct {
5759243Sobrien    const char suffix;
5859243Sobrien    const char *variable;
59167465Smp    Str	    color;
60167465Smp    Str	    defaultcolor;
6159243Sobrien} Variable;
6259243Sobrien
6359243Sobrienstatic Variable variables[] = {
6459243Sobrien    VAR('/', "di", "01;34"),	/* Directory */
6559243Sobrien    VAR('@', "ln", "01;36"),	/* Symbolic link */
6659243Sobrien    VAR('&', "or", ""),		/* Orphanned symbolic link (defaults to ln) */
6759243Sobrien    VAR('|', "pi", "33"),	/* Named pipe (FIFO) */
6859243Sobrien    VAR('=', "so", "01;35"),	/* Socket */
6983098Smp    VAR('>', "do", "01;35"),	/* Door (solaris fast ipc mechanism)  */
7059243Sobrien    VAR('#', "bd", "01;33"),	/* Block device */
7159243Sobrien    VAR('%', "cd", "01;33"),	/* Character device */
7259243Sobrien    VAR('*', "ex", "01;32"),	/* Executable file */
7359243Sobrien    VAR(NOS, "fi", "0"),	/* Regular file */
7459243Sobrien    VAR(NOS, "no", "0"),	/* Normal (non-filename) text */
7559243Sobrien    VAR(NOS, "mi", ""),		/* Missing file (defaults to fi) */
7669408Sache#ifdef IS_ASCII
7769408Sache    VAR(NOS, "lc", "\033["),	/* Left code (ASCII) */
7869408Sache#else
7959243Sobrien    VAR(NOS, "lc", "\x27["),	/* Left code (EBCDIC)*/
8069408Sache#endif
8159243Sobrien    VAR(NOS, "rc", "m"),	/* Right code */
8259243Sobrien    VAR(NOS, "ec", ""),		/* End code (replaces lc+no+rc) */
83167465Smp    VAR(NOS, "su", ""),		/* Setuid file (u+s) */
84167465Smp    VAR(NOS, "sg", ""),		/* Setgid file (g+s) */
85167465Smp    VAR(NOS, "tw", ""),		/* Sticky and other writable dir (+t,o+w) */
86167465Smp    VAR(NOS, "ow", ""),		/* Other writable dir (o+w) but not sticky */
87167465Smp    VAR(NOS, "st", ""),		/* Sticky dir (+t) but not other writable */
8859243Sobrien};
8959243Sobrien
9059243Sobrienenum FileType {
9183098Smp    VDir, VSym, VOrph, VPipe, VSock, VDoor, VBlock, VChr, VExe,
9259243Sobrien    VFile, VNormal, VMiss, VLeft, VRight, VEnd
9359243Sobrien};
9459243Sobrien
9559243Sobrien#define nvariables (sizeof(variables)/sizeof(variables[0]))
9659243Sobrien
9759243Sobrientypedef struct {
98167465Smp    Str	    extension;	/* file extension */
99167465Smp    Str	    color;	/* color string */
10059243Sobrien} Extension;
10159243Sobrien
10259243Sobrienstatic Extension *extensions = NULL;
103145479Smpstatic size_t nextensions = 0;
10459243Sobrien
10559243Sobrienstatic char *colors = NULL;
106145479Smpint	     color_context_ls = FALSE;	/* do colored ls */
107167465Smpstatic int  color_context_lsmF = FALSE; /* do colored ls-F */
10859243Sobrien
109167465Smpstatic int getstring (char **, const Char **, Str *, int);
110167465Smpstatic void put_color (const Str *);
111167465Smpstatic void print_color (const Char *, size_t, Char);
11259243Sobrien
11359243Sobrien/* set_color_context():
11459243Sobrien */
11559243Sobrienvoid
116167465Smpset_color_context(void)
11759243Sobrien{
11859243Sobrien    struct varent *vp = adrof(STRcolor);
11959243Sobrien
120100616Smp    if (vp == NULL || vp->vec == NULL) {
12159243Sobrien	color_context_ls = FALSE;
12259243Sobrien	color_context_lsmF = FALSE;
123100616Smp    } else if (!vp->vec[0] || vp->vec[0][0] == '\0') {
12459243Sobrien	color_context_ls = TRUE;
12559243Sobrien	color_context_lsmF = TRUE;
126100616Smp    } else {
12759243Sobrien	size_t i;
12859243Sobrien
12959243Sobrien	color_context_ls = FALSE;
13059243Sobrien	color_context_lsmF = FALSE;
13159243Sobrien	for (i = 0; vp->vec[i]; i++)
13259243Sobrien	    if (Strcmp(vp->vec[i], STRls) == 0)
13359243Sobrien		color_context_ls = TRUE;
13459243Sobrien	    else if (Strcmp(vp->vec[i], STRlsmF) == 0)
13559243Sobrien		color_context_lsmF = TRUE;
13659243Sobrien    }
13759243Sobrien}
13859243Sobrien
13959243Sobrien
14059243Sobrien/* getstring():
14159243Sobrien */
142167465Smpstatic	int
143167465Smpgetstring(char **dp, const Char **sp, Str *pd, int f)
14459243Sobrien{
14559243Sobrien    const Char *s = *sp;
14659243Sobrien    char *d = *dp;
147145479Smp    eChar sc;
14859243Sobrien
149145479Smp    while (*s && (*s & CHAR) != (Char)f && (*s & CHAR) != ':') {
15059243Sobrien	if ((*s & CHAR) == '\\' || (*s & CHAR) == '^') {
151145479Smp	    if ((sc = parseescape(&s)) == CHAR_ERR)
15259243Sobrien		return 0;
15359243Sobrien	}
15459243Sobrien	else
155145479Smp	    sc = *s++ & CHAR;
156145479Smp	d += one_wctomb(d, sc);
15759243Sobrien    }
15859243Sobrien
15959243Sobrien    pd->s = *dp;
160167465Smp    pd->len = d - *dp;
16159243Sobrien    *sp = s;
16259243Sobrien    *dp = d;
163145479Smp    return *s == (Char)f;
16459243Sobrien}
16559243Sobrien
16659243Sobrien
16759243Sobrien/* parseLS_COLORS():
16859243Sobrien *	Parse the LS_COLORS environment variable
16959243Sobrien */
17059243Sobrienvoid
171167465SmpparseLS_COLORS(const Char *value)
17259243Sobrien{
173145479Smp    size_t  i, len;
174167465Smp    const Char	 *v;		/* pointer in value */
17559243Sobrien    char   *c;			/* pointer in colors */
176145479Smp    Extension *volatile e;	/* pointer in extensions */
17759415Sobrien    jmp_buf_t osetexit;
178167465Smp    size_t omark;
17959243Sobrien
180100616Smp    (void) &e;
181100616Smp
18259243Sobrien    /* init */
183167465Smp    xfree(extensions);
18459243Sobrien    for (i = 0; i < nvariables; i++)
18559243Sobrien	variables[i].color = variables[i].defaultcolor;
18659243Sobrien    colors = NULL;
18759243Sobrien    extensions = NULL;
18859243Sobrien    nextensions = 0;
18959243Sobrien
19059243Sobrien    if (value == NULL)
19159243Sobrien	return;
19259243Sobrien
19359243Sobrien    len = Strlen(value);
19459243Sobrien    /* allocate memory */
19559243Sobrien    i = 1;
19659243Sobrien    for (v = value; *v; v++)
19759243Sobrien	if ((*v & CHAR) == ':')
19859243Sobrien	    i++;
199167465Smp    extensions = xmalloc(len + i * sizeof(Extension));
20059243Sobrien    colors = i * sizeof(Extension) + (char *)extensions;
20159243Sobrien    nextensions = 0;
20259243Sobrien
20359243Sobrien    /* init pointers */
20459243Sobrien    v = value;
20559243Sobrien    c = colors;
20659243Sobrien    e = &extensions[0];
20759243Sobrien
20859415Sobrien    /* Prevent from crashing if unknown parameters are given. */
20959415Sobrien
210167465Smp    omark = cleanup_push_mark();
21159415Sobrien    getexit(osetexit);
21259415Sobrien
21359415Sobrien    if (setexit() == 0) {
214167465Smp
21559243Sobrien    /* parse */
21659243Sobrien    while (*v) {
21759243Sobrien	switch (*v & CHAR) {
21859243Sobrien	case ':':
21959243Sobrien	    v++;
22059243Sobrien	    continue;
22159243Sobrien
22259243Sobrien	case '*':		/* :*ext=color: */
22359243Sobrien	    v++;
22459243Sobrien	    if (getstring(&c, &v, &e->extension, '=') &&
22559243Sobrien		0 < e->extension.len) {
22659243Sobrien		v++;
22759243Sobrien		getstring(&c, &v, &e->color, ':');
22859243Sobrien		e++;
22959243Sobrien		continue;
23059243Sobrien	    }
23159243Sobrien	    break;
23259243Sobrien
23359243Sobrien	default:		/* :vl=color: */
23459243Sobrien	    if (v[0] && v[1] && (v[2] & CHAR) == '=') {
23559243Sobrien		for (i = 0; i < nvariables; i++)
236145479Smp		    if ((Char)variables[i].variable[0] == (v[0] & CHAR) &&
237145479Smp			(Char)variables[i].variable[1] == (v[1] & CHAR))
23859243Sobrien			break;
23959243Sobrien		if (i < nvariables) {
24059243Sobrien		    v += 3;
24159243Sobrien		    getstring(&c, &v, &variables[i].color, ':');
24259243Sobrien		    continue;
24359243Sobrien		}
24459243Sobrien		else
24559243Sobrien		    stderror(ERR_BADCOLORVAR, v[0], v[1]);
24659243Sobrien	    }
24759243Sobrien	    break;
24859243Sobrien	}
24959243Sobrien	while (*v && (*v & CHAR) != ':')
25059243Sobrien	    v++;
25159243Sobrien    }
25259415Sobrien    }
25359243Sobrien
254167465Smp    cleanup_pop_mark(omark);
25559415Sobrien    resexit(osetexit);
25659415Sobrien
257167465Smp    nextensions = e - extensions;
25859243Sobrien}
25959243Sobrien
26059243Sobrien/* put_color():
26159243Sobrien */
26259243Sobrienstatic void
263167465Smpput_color(const Str *color)
26459243Sobrien{
26559243Sobrien    size_t  i;
266167465Smp    const char	 *c = color->s;
267167465Smp    int	   original_output_raw = output_raw;
26859243Sobrien
26959243Sobrien    output_raw = TRUE;
270167465Smp    cleanup_push(&original_output_raw, output_raw_restore);
27159243Sobrien    for (i = color->len; 0 < i; i--)
27259243Sobrien	xputchar(*c++);
273167465Smp    cleanup_until(&original_output_raw);
27459243Sobrien}
27559243Sobrien
27659243Sobrien
27759243Sobrien/* print_color():
27859243Sobrien */
27959243Sobrienstatic void
280167465Smpprint_color(const Char *fname, size_t len, Char suffix)
28159243Sobrien{
282145479Smp    size_t  i;
28359243Sobrien    char   *filename = short2str(fname);
28459243Sobrien    char   *last = filename + len;
285167465Smp    Str	   *color = &variables[VFile].color;
28659243Sobrien
28759243Sobrien    switch (suffix) {
28859243Sobrien    case '>':			/* File is a symbolic link pointing to
28959243Sobrien				 * a directory */
290167465Smp	color = &variables[VDir].color;
291167465Smp	break;
29259243Sobrien    case '+':			/* File is a hidden directory [aix] or
29359243Sobrien				 * context dependent [hpux] */
29459243Sobrien    case ':':			/* File is network special [hpux] */
295167465Smp	break;
29659243Sobrien    default:
29759243Sobrien	for (i = 0; i < nvariables; i++)
29859243Sobrien	    if (variables[i].suffix != NOS &&
299145479Smp		(Char)variables[i].suffix == suffix) {
30059243Sobrien		color = &variables[i].color;
30159243Sobrien		break;
30259243Sobrien	    }
30359243Sobrien	if (i == nvariables) {
30459243Sobrien	    for (i = 0; i < nextensions; i++)
305167465Smp		if (len >= extensions[i].extension.len
306167465Smp		    && strncmp(last - extensions[i].extension.len,
307167465Smp			       extensions[i].extension.s,
308167465Smp			       extensions[i].extension.len) == 0) {
30959243Sobrien		  color = &extensions[i].color;
31059243Sobrien		break;
31159243Sobrien	    }
31259243Sobrien	}
31359243Sobrien	break;
31459243Sobrien    }
31559243Sobrien
31659243Sobrien    put_color(&variables[VLeft].color);
31759243Sobrien    put_color(color);
31859243Sobrien    put_color(&variables[VRight].color);
31959243Sobrien}
32059243Sobrien
32159243Sobrien
32259243Sobrien/* print_with_color():
32359243Sobrien */
32459243Sobrienvoid
325167465Smpprint_with_color(const Char *filename, size_t len, Char suffix)
32659243Sobrien{
32759243Sobrien    if (color_context_lsmF &&
32859243Sobrien	(haderr ? (didfds ? is2atty : isdiagatty) :
32959243Sobrien	 (didfds ? is1atty : isoutatty))) {
33059243Sobrien	print_color(filename, len, suffix);
33159243Sobrien	xprintf("%S", filename);
33259243Sobrien	if (0 < variables[VEnd].color.len)
33359243Sobrien	    put_color(&variables[VEnd].color);
33459243Sobrien	else {
33559243Sobrien	    put_color(&variables[VLeft].color);
33659243Sobrien	    put_color(&variables[VNormal].color);
33759243Sobrien	    put_color(&variables[VRight].color);
33859243Sobrien	}
33959243Sobrien    }
34059243Sobrien    else
341145479Smp	xprintf("%S", filename);
342145479Smp    xputwchar(suffix);
34359243Sobrien}
34459243Sobrien
34559243Sobrien
34659243Sobrien#endif /* COLOR_LS_F */
347