tw.color.c revision 100616
1100616Smp/* $Header: /src/pub/tcsh/tw.color.c,v 1.11 2002/06/25 19:02:11 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
35100616SmpRCSID("$Id: tw.color.c,v 1.11 2002/06/25 19:02:11 christos Exp $")
3659243Sobrien
3759243Sobrien#include "tw.h"
3859243Sobrien#include "ed.h"
3959243Sobrien#include "tc.h"
4059243Sobrien
4159243Sobrien#ifdef COLOR_LS_F
4259243Sobrien
4359243Sobrientypedef struct {
4459243Sobrien    char   *s;
4559243Sobrien    int     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;
5959243Sobrien    Str     color;
6059243Sobrien    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) */
8359243Sobrien};
8459243Sobrien
8559243Sobrienenum FileType {
8683098Smp    VDir, VSym, VOrph, VPipe, VSock, VDoor, VBlock, VChr, VExe,
8759243Sobrien    VFile, VNormal, VMiss, VLeft, VRight, VEnd
8859243Sobrien};
8959243Sobrien
9059243Sobrien#define nvariables (sizeof(variables)/sizeof(variables[0]))
9159243Sobrien
9259243Sobrientypedef struct {
9359243Sobrien    Str     extension;	/* file extension */
9459243Sobrien    Str     color;	/* color string */
9559243Sobrien} Extension;
9659243Sobrien
9759243Sobrienstatic Extension *extensions = NULL;
9859243Sobrienstatic int nextensions = 0;
9959243Sobrien
10059243Sobrienstatic char *colors = NULL;
10159243Sobrienbool	     color_context_ls = FALSE;	/* do colored ls */
10259243Sobrienstatic bool  color_context_lsmF = FALSE;	/* do colored ls-F */
10359243Sobrien
10459243Sobrienstatic bool getstring __P((char **, const Char **, Str *, int));
10559243Sobrienstatic void put_color __P((Str *));
10659243Sobrienstatic void print_color __P((Char *, size_t, int));
10759243Sobrien
10859243Sobrien/* set_color_context():
10959243Sobrien */
11059243Sobrienvoid
11159243Sobrienset_color_context()
11259243Sobrien{
11359243Sobrien    struct varent *vp = adrof(STRcolor);
11459243Sobrien
115100616Smp    if (vp == NULL || vp->vec == NULL) {
11659243Sobrien	color_context_ls = FALSE;
11759243Sobrien	color_context_lsmF = FALSE;
118100616Smp    } else if (!vp->vec[0] || vp->vec[0][0] == '\0') {
11959243Sobrien	color_context_ls = TRUE;
12059243Sobrien	color_context_lsmF = TRUE;
121100616Smp    } else {
12259243Sobrien	size_t i;
12359243Sobrien
12459243Sobrien	color_context_ls = FALSE;
12559243Sobrien	color_context_lsmF = FALSE;
12659243Sobrien	for (i = 0; vp->vec[i]; i++)
12759243Sobrien	    if (Strcmp(vp->vec[i], STRls) == 0)
12859243Sobrien		color_context_ls = TRUE;
12959243Sobrien	    else if (Strcmp(vp->vec[i], STRlsmF) == 0)
13059243Sobrien		color_context_lsmF = TRUE;
13159243Sobrien    }
13259243Sobrien}
13359243Sobrien
13459243Sobrien
13559243Sobrien/* getstring():
13659243Sobrien */
13759243Sobrienstatic  bool
13859243Sobriengetstring(dp, sp, pd, f)
13959243Sobrien    char        **dp;		/* dest buffer */
14059243Sobrien    const Char  **sp;		/* source buffer */
14159243Sobrien    Str          *pd;		/* pointer to dest buffer */
14259243Sobrien    int           f;		/* final character */
14359243Sobrien{
14459243Sobrien    const Char *s = *sp;
14559243Sobrien    char *d = *dp;
14659243Sobrien    int sc;
14759243Sobrien
14859243Sobrien    while (*s && (*s & CHAR) != f && (*s & CHAR) != ':') {
14959243Sobrien	if ((*s & CHAR) == '\\' || (*s & CHAR) == '^') {
15059243Sobrien	    if ((sc = parseescape(&s)) == -1)
15159243Sobrien		return 0;
15259243Sobrien	    else
15359243Sobrien		*d++ = (char) sc;
15459243Sobrien	}
15559243Sobrien	else
15659243Sobrien	    *d++ = *s++ & CHAR;
15759243Sobrien    }
15859243Sobrien
15959243Sobrien    pd->s = *dp;
16059243Sobrien    pd->len = (int) (d - *dp);
16159243Sobrien    *sp = s;
16259243Sobrien    *dp = d;
16359243Sobrien    return *s == f;
16459243Sobrien}
16559243Sobrien
16659243Sobrien
16759243Sobrien/* parseLS_COLORS():
16859243Sobrien *	Parse the LS_COLORS environment variable
16959243Sobrien */
17059243Sobrienvoid
17159243SobrienparseLS_COLORS(value)
17259243Sobrien    Char   *value;		/* LS_COLOR variable's value */
17359243Sobrien{
17459243Sobrien    int     i;
17559243Sobrien    size_t  len;
17659243Sobrien    const Char   *v;		/* pointer in value */
17759243Sobrien    char   *c;			/* pointer in colors */
17859243Sobrien    Extension *e;		/* pointer in extensions */
17959415Sobrien    jmp_buf_t osetexit;
18059243Sobrien
181100616Smp    (void) &e;
182100616Smp
18359243Sobrien    /* init */
18459243Sobrien    if (extensions)
18559243Sobrien        xfree((ptr_t) extensions);
18659243Sobrien    for (i = 0; i < nvariables; i++)
18759243Sobrien	variables[i].color = variables[i].defaultcolor;
18859243Sobrien    colors = NULL;
18959243Sobrien    extensions = NULL;
19059243Sobrien    nextensions = 0;
19159243Sobrien
19259243Sobrien    if (value == NULL)
19359243Sobrien	return;
19459243Sobrien
19559243Sobrien    len = Strlen(value);
19659243Sobrien    /* allocate memory */
19759243Sobrien    i = 1;
19859243Sobrien    for (v = value; *v; v++)
19959243Sobrien	if ((*v & CHAR) == ':')
20059243Sobrien	    i++;
20159243Sobrien    extensions = (Extension *) xmalloc((size_t) (len + i * sizeof(Extension)));
20259243Sobrien    colors = i * sizeof(Extension) + (char *)extensions;
20359243Sobrien    nextensions = 0;
20459243Sobrien
20559243Sobrien    /* init pointers */
20659243Sobrien    v = value;
20759243Sobrien    c = colors;
20859243Sobrien    e = &extensions[0];
20959243Sobrien
21059415Sobrien    /* Prevent from crashing if unknown parameters are given. */
21159415Sobrien
21259415Sobrien    getexit(osetexit);
21359415Sobrien
21459415Sobrien    if (setexit() == 0) {
21559415Sobrien
21659243Sobrien    /* parse */
21759243Sobrien    while (*v) {
21859243Sobrien	switch (*v & CHAR) {
21959243Sobrien	case ':':
22059243Sobrien	    v++;
22159243Sobrien	    continue;
22259243Sobrien
22359243Sobrien	case '*':		/* :*ext=color: */
22459243Sobrien	    v++;
22559243Sobrien	    if (getstring(&c, &v, &e->extension, '=') &&
22659243Sobrien		0 < e->extension.len) {
22759243Sobrien		v++;
22859243Sobrien		getstring(&c, &v, &e->color, ':');
22959243Sobrien		e++;
23059243Sobrien		continue;
23159243Sobrien	    }
23259243Sobrien	    break;
23359243Sobrien
23459243Sobrien	default:		/* :vl=color: */
23559243Sobrien	    if (v[0] && v[1] && (v[2] & CHAR) == '=') {
23659243Sobrien		for (i = 0; i < nvariables; i++)
23759243Sobrien		    if (variables[i].variable[0] == (v[0] & CHAR) &&
23859243Sobrien			variables[i].variable[1] == (v[1] & CHAR))
23959243Sobrien			break;
24059243Sobrien		if (i < nvariables) {
24159243Sobrien		    v += 3;
24259243Sobrien		    getstring(&c, &v, &variables[i].color, ':');
24359243Sobrien		    continue;
24459243Sobrien		}
24559243Sobrien		else
24659243Sobrien		    stderror(ERR_BADCOLORVAR, v[0], v[1]);
24759243Sobrien	    }
24859243Sobrien	    break;
24959243Sobrien	}
25059243Sobrien	while (*v && (*v & CHAR) != ':')
25159243Sobrien	    v++;
25259243Sobrien    }
25359415Sobrien    }
25459243Sobrien
25559415Sobrien    resexit(osetexit);
25659415Sobrien
25759243Sobrien    nextensions = (int) (e - extensions);
25859243Sobrien}
25959243Sobrien
26059243Sobrien
26159243Sobrien/* put_color():
26259243Sobrien */
26359243Sobrienstatic void
26459243Sobrienput_color(color)
26559243Sobrien    Str    *color;
26659243Sobrien{
26759243Sobrien    extern bool output_raw;	/* PWP: in sh.print.c */
26859243Sobrien    size_t  i;
26959243Sobrien    char   *c = color->s;
27059243Sobrien    bool    original_output_raw = output_raw;
27159243Sobrien
27259243Sobrien    output_raw = TRUE;
27359243Sobrien    for (i = color->len; 0 < i; i--)
27459243Sobrien	xputchar(*c++);
27559243Sobrien    output_raw = original_output_raw;
27659243Sobrien}
27759243Sobrien
27859243Sobrien
27959243Sobrien/* print_color():
28059243Sobrien */
28159243Sobrienstatic void
28259243Sobrienprint_color(fname, len, suffix)
28359243Sobrien    Char   *fname;
28459243Sobrien    size_t  len;
28559243Sobrien    int     suffix;
28659243Sobrien{
28759243Sobrien    int     i;
28859243Sobrien    char   *filename = short2str(fname);
28959243Sobrien    char   *last = filename + len;
29059243Sobrien    Str    *color = &variables[VFile].color;
29159243Sobrien
29259243Sobrien    switch (suffix) {
29359243Sobrien    case '>':			/* File is a symbolic link pointing to
29459243Sobrien				 * a directory */
29559243Sobrien        color = &variables[VDir].color;
29659243Sobrien	    break;
29759243Sobrien    case '+':			/* File is a hidden directory [aix] or
29859243Sobrien				 * context dependent [hpux] */
29959243Sobrien    case ':':			/* File is network special [hpux] */
30059243Sobrien        break;
30159243Sobrien    default:
30259243Sobrien	for (i = 0; i < nvariables; i++)
30359243Sobrien	    if (variables[i].suffix != NOS &&
30459243Sobrien		variables[i].suffix == suffix) {
30559243Sobrien		color = &variables[i].color;
30659243Sobrien		break;
30759243Sobrien	    }
30859243Sobrien	if (i == nvariables) {
30959243Sobrien	    for (i = 0; i < nextensions; i++)
31059243Sobrien	        if (strncmp(last - extensions[i].extension.len,
31159243Sobrien			    extensions[i].extension.s,
31259243Sobrien			    extensions[i].extension.len) == 0) {
31359243Sobrien		  color = &extensions[i].color;
31459243Sobrien		break;
31559243Sobrien	    }
31659243Sobrien	}
31759243Sobrien	break;
31859243Sobrien    }
31959243Sobrien
32059243Sobrien    put_color(&variables[VLeft].color);
32159243Sobrien    put_color(color);
32259243Sobrien    put_color(&variables[VRight].color);
32359243Sobrien}
32459243Sobrien
32559243Sobrien
32659243Sobrien/* print_with_color():
32759243Sobrien */
32859243Sobrienvoid
32959243Sobrienprint_with_color(filename, len, suffix)
33059243Sobrien    Char   *filename;
33159243Sobrien    size_t  len;
33259243Sobrien    int    suffix;
33359243Sobrien{
33459243Sobrien    if (color_context_lsmF &&
33559243Sobrien	(haderr ? (didfds ? is2atty : isdiagatty) :
33659243Sobrien	 (didfds ? is1atty : isoutatty))) {
33759243Sobrien	print_color(filename, len, suffix);
33859243Sobrien	xprintf("%S", filename);
33959243Sobrien	if (0 < variables[VEnd].color.len)
34059243Sobrien	    put_color(&variables[VEnd].color);
34159243Sobrien	else {
34259243Sobrien	    put_color(&variables[VLeft].color);
34359243Sobrien	    put_color(&variables[VNormal].color);
34459243Sobrien	    put_color(&variables[VRight].color);
34559243Sobrien	}
34659243Sobrien	xputchar(suffix);
34759243Sobrien    }
34859243Sobrien    else
34959243Sobrien	xprintf("%S%c", filename, suffix);
35059243Sobrien}
35159243Sobrien
35259243Sobrien
35359243Sobrien#endif /* COLOR_LS_F */
354