2 * tw.color.c: builtin color ls-F
5 * Copyright (c) 1998 The Regents of the University of California.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 #define VAR(suffix,variable,defaultcolor) \
47 suffix, variable, { defaultcolor, sizeof(defaultcolor) - 1 }, \
48 { defaultcolor, sizeof(defaultcolor) - 1 } \
50 #define NOS '\0' /* no suffix */
59 static Variable variables[] = {
60 VAR('/', "di", "01;34"), /* Directory */
61 VAR('@', "ln", "01;36"), /* Symbolic link */
62 VAR('&', "or", ""), /* Orphanned symbolic link (defaults to ln) */
63 VAR('|', "pi", "33"), /* Named pipe (FIFO) */
64 VAR('=', "so", "01;35"), /* Socket */
65 VAR('>', "do", "01;35"), /* Door (solaris fast ipc mechanism) */
66 VAR('#', "bd", "01;33"), /* Block device */
67 VAR('%', "cd", "01;33"), /* Character device */
68 VAR('*', "ex", "01;32"), /* Executable file */
69 VAR(NOS, "fi", "0"), /* Regular file */
70 VAR(NOS, "no", "0"), /* Normal (non-filename) text */
71 VAR(NOS, "mi", ""), /* Missing file (defaults to fi) */
73 VAR(NOS, "lc", "\033["), /* Left code (ASCII) */
75 VAR(NOS, "lc", "\x27["), /* Left code (EBCDIC)*/
77 VAR(NOS, "rc", "m"), /* Right code */
78 VAR(NOS, "ec", ""), /* End code (replaces lc+no+rc) */
79 VAR(NOS, "su", ""), /* Setuid file (u+s) */
80 VAR(NOS, "sg", ""), /* Setgid file (g+s) */
81 VAR(NOS, "tw", ""), /* Sticky and other writable dir (+t,o+w) */
82 VAR(NOS, "ow", ""), /* Other writable dir (o+w) but not sticky */
83 VAR(NOS, "st", ""), /* Sticky dir (+t) but not other writable */
84 VAR(NOS, "rs", "0"), /* Reset to normal color */
85 VAR(NOS, "hl", "44;37"), /* Reg file extra hard links, obsolete? */
86 VAR(NOS, "mh", "44;37"), /* Reg file extra hard links */
87 VAR(NOS, "ca", "30;41"), /* File with capability */
90 #define nvariables (sizeof(variables)/sizeof(variables[0]))
93 VDir, VSym, VOrph, VPipe, VSock, VDoor, VBlock, VChr, VExe,
94 VFile, VNormal, VMiss, VLeft, VRight, VEnd, VSuid, VSgid, VSticky,
95 VOther, Vstird, VReset, Vhard, Vhard2, VCap
99 * Map from LSCOLORS entry index to Variable array index
101 static const uint8_t map[] = {
102 VDir, /* Directory */
103 VSym, /* Symbolic Link */
105 VPipe, /* Named Pipe */
106 VExe, /* Executable */
107 VBlock, /* Block Special */
108 VChr, /* Character Special */
109 VSuid, /* Setuid Executable */
110 VSgid, /* Setgid Executable */
111 VSticky, /* Directory writable to others and sticky */
112 VOther, /* Directory writable to others but not sticky */
118 ANSI_RESET_ON = 0, /* reset colors/styles (white on black) */
119 ANSI_BOLD_ON = 1, /* bold on */
120 ANSI_ITALICS_ON = 3, /* italics on */
121 ANSI_UNDERLINE_ON = 4, /* underline on */
122 ANSI_INVERSE_ON = 7, /* inverse on */
123 ANSI_STRIKETHROUGH_ON = 9, /* strikethrough on */
124 ANSI_BOLD_OFF = 21, /* bold off */
125 ANSI_ITALICS_OFF = 23, /* italics off */
126 ANSI_UNDERLINE_OFF = 24, /* underline off */
127 ANSI_INVERSE_OFF = 27, /* inverse off */
128 ANSI_STRIKETHROUGH_OFF = 29,/* strikethrough off */
129 ANSI_FG_BLACK = 30, /* fg black */
130 ANSI_FG_RED = 31, /* fg red */
131 ANSI_FG_GREEN = 32, /* fg green */
132 ANSI_FG_YELLOW = 33, /* fg yellow */
133 ANSI_FG_BLUE = 34, /* fg blue */
134 ANSI_FG_MAGENTA = 35, /* fg magenta */
135 ANSI_FG_CYAN = 36, /* fg cyan */
136 ANSI_FG_WHITE = 37, /* fg white */
137 ANSI_FG_DEFAULT = 39, /* fg default (white) */
138 ANSI_BG_BLACK = 40, /* bg black */
139 ANSI_BG_RED = 41, /* bg red */
140 ANSI_BG_GREEN = 42, /* bg green */
141 ANSI_BG_YELLOW = 43, /* bg yellow */
142 ANSI_BG_BLUE = 44, /* bg blue */
143 ANSI_BG_MAGENTA = 45, /* bg magenta */
144 ANSI_BG_CYAN = 46, /* bg cyan */
145 ANSI_BG_WHITE = 47, /* bg white */
146 ANSI_BG_DEFAULT = 49, /* bg default (black) */
148 #define TCSH_BOLD 0x80
151 Str extension; /* file extension */
152 Str color; /* color string */
155 static Extension *extensions = NULL;
156 static size_t nextensions = 0;
158 static char *colors = NULL;
159 int color_context_ls = FALSE; /* do colored ls */
160 static int color_context_lsmF = FALSE; /* do colored ls-F */
161 int color_as_referent = FALSE; /* ln=target in LS_COLORS */
163 static int getstring (char **, const Char **, Str *, int);
164 static void put_color (const Str *);
165 static void print_color (const Char *, size_t, Char);
167 /* set_color_context():
170 set_color_context(void)
172 struct varent *vp = adrof(STRcolor);
174 if (vp == NULL || vp->vec == NULL) {
175 color_context_ls = FALSE;
176 color_context_lsmF = FALSE;
177 } else if (!vp->vec[0] || vp->vec[0][0] == '\0') {
178 color_context_ls = TRUE;
179 color_context_lsmF = TRUE;
183 color_context_ls = FALSE;
184 color_context_lsmF = FALSE;
185 for (i = 0; vp->vec[i]; i++)
186 if (Strcmp(vp->vec[i], STRls) == 0)
187 color_context_ls = TRUE;
188 else if (Strcmp(vp->vec[i], STRlsmF) == 0)
189 color_context_lsmF = TRUE;
197 getstring(char **dp, const Char **sp, Str *pd, int f)
203 while (*s && (*s & CHAR) != (Char)f && (*s & CHAR) != ':') {
204 if ((*s & CHAR) == '\\' || (*s & CHAR) == '^') {
205 if ((sc = parseescape(&s, TRUE)) == CHAR_ERR)
210 d += one_wctomb(d, sc);
217 return *s == (Char)f;
221 init(size_t colorlen, size_t extnum)
226 for (i = 0; i < nvariables; i++)
227 variables[i].color = variables[i].defaultcolor;
228 if (colorlen == 0 && extnum == 0) {
232 extensions = xmalloc(colorlen + extnum * sizeof(*extensions));
233 colors = extnum * sizeof(*extensions) + (char *)extensions;
241 static const char ccolors[] = "abcdefghx";
247 if (x == '\0' || (p = strchr(ccolors, x)) == NULL)
249 return 30 + (p - ccolors);
253 makecolor(char **c, int fg, int bg, Str *v)
257 l = xsnprintf(*c, 12, "%.2d;%.2d;%.2d;%.2d", ANSI_BOLD_ON,
258 fg & ~TCSH_BOLD, (10 + bg) & ~TCSH_BOLD, ANSI_BOLD_OFF);
260 l = xsnprintf(*c, 6, "%.2d;%.2d",
261 fg & ~TCSH_BOLD, (10 + bg) & ~TCSH_BOLD);
270 * Parse the LSCOLORS environment variable
272 static const Char *xv; /* setjmp clobbering */
274 parseLSCOLORS(const Char *value)
288 clen = len * 12; /* "??;??;??;??\0" */
291 /* Prevent from crashing if unknown parameters are given. */
292 omark = cleanup_push_mark();
297 if (setexit() == 0) {
302 for (i = 0; i < len; i++) {
305 stderror(ERR_BADCOLORVAR, v[-1], '?');
309 stderror(ERR_BADCOLORVAR, '?', v[-1]);
310 makecolor(&c, fg, bg, &variables[map[i]].color);
314 cleanup_pop_mark(omark);
319 * Parse the LS_COLORS environment variable
322 parseLS_COLORS(const Char *value)
325 const Char *v; /* pointer in value */
326 char *c; /* pointer in colors */
327 Extension *volatile e; /* pointer in extensions */
340 /* allocate memory */
342 for (v = value; *v; v++)
343 if ((*v & CHAR) == ':')
353 /* Prevent from crashing if unknown parameters are given. */
355 omark = cleanup_push_mark();
358 if (setexit() == 0) {
367 case '*': /* :*ext=color: */
369 if (getstring(&c, &v, &e->extension, '=') &&
370 0 < e->extension.len) {
372 getstring(&c, &v, &e->color, ':');
378 default: /* :vl=color: */
379 if (v[0] && v[1] && (v[2] & CHAR) == '=') {
380 for (i = 0; i < nvariables; i++)
381 if ((Char)variables[i].variable[0] == (v[0] & CHAR) &&
382 (Char)variables[i].variable[1] == (v[1] & CHAR))
384 if (i < nvariables) {
386 getstring(&c, &v, &variables[i].color, ':');
388 color_as_referent = strcasecmp(
389 variables[VSym].color.s, "target") == 0;
393 stderror(ERR_BADCOLORVAR, v[0], v[1]);
397 while (*v && (*v & CHAR) != ':')
402 cleanup_pop_mark(omark);
405 nextensions = e - extensions;
411 put_color(const Str *colorp)
414 const char *c = colorp->s;
415 int original_output_raw = output_raw;
418 cleanup_push(&original_output_raw, output_raw_restore);
419 for (i = colorp->len; 0 < i; i--)
421 cleanup_until(&original_output_raw);
428 print_color(const Char *fname, size_t len, Char suffix)
431 char *filename = short2str(fname);
432 char *last = filename + len;
433 Str *colorp = &variables[VFile].color;
436 case '>': /* File is a symbolic link pointing to
438 colorp = &variables[VDir].color;
440 case '+': /* File is a hidden directory [aix] or
441 * context dependent [hpux] */
442 case ':': /* File is network special [hpux] */
445 for (i = 0; i < nvariables; i++)
446 if (variables[i].suffix != NOS &&
447 (Char)variables[i].suffix == suffix) {
448 colorp = &variables[i].color;
451 if (i == nvariables) {
452 for (i = 0; i < nextensions; i++)
453 if (len >= extensions[i].extension.len
454 && strncmp(last - extensions[i].extension.len,
455 extensions[i].extension.s,
456 extensions[i].extension.len) == 0) {
457 colorp = &extensions[i].color;
464 put_color(&variables[VLeft].color);
466 put_color(&variables[VRight].color);
470 /* print_with_color():
473 print_with_color(const Char *filename, size_t len, Char suffix)
475 if (color_context_lsmF &&
476 (haderr ? (didfds ? is2atty : isdiagatty) :
477 (didfds ? is1atty : isoutatty))) {
479 if (suffix == '@' && color_as_referent) {
480 char *f = short2str(filename);
482 char buf[MAXPATHLEN + 1];
485 ssize_t b = readlink(f, buf, MAXPATHLEN);
492 c = filetype(STRNULL, str2short(buf));
496 print_color(filename, len, c);
498 print_color(filename, len, suffix);
499 xprintf("%" TCSH_S, filename);
500 if (0 < variables[VEnd].color.len)
501 put_color(&variables[VEnd].color);
503 put_color(&variables[VLeft].color);
504 put_color(&variables[VNormal].color);
505 put_color(&variables[VRight].color);
508 xprintf("%" TCSH_S, filename);
513 #endif /* COLOR_LS_F */