1 /* $Header: /src/pub/tcsh/tw.color.c,v 1.18 2005/03/03 16:40:53 kim 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 35 RCSID("$Id: tw.color.c,v 1.18 2005/03/03 16:40:53 kim Exp $") 36 37 #include "tw.h" 38 #include "ed.h" 39 #include "tc.h" 40 41 #ifdef COLOR_LS_F 42 43 typedef struct { 44 const char *s; 45 int 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 56 typedef struct { 57 const char suffix; 58 const char *variable; 59 Str color; 60 Str defaultcolor; 61 } Variable; 62 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) */ 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 }; 84 85 enum FileType { 86 VDir, VSym, VOrph, VPipe, VSock, VDoor, VBlock, VChr, VExe, 87 VFile, VNormal, VMiss, VLeft, VRight, VEnd 88 }; 89 90 #define nvariables (sizeof(variables)/sizeof(variables[0])) 91 92 typedef struct { 93 Str extension; /* file extension */ 94 Str color; /* color string */ 95 } Extension; 96 97 static Extension *extensions = NULL; 98 static size_t nextensions = 0; 99 100 static char *colors = NULL; 101 int color_context_ls = FALSE; /* do colored ls */ 102 static int color_context_lsmF = FALSE; /* do colored ls-F */ 103 104 static int getstring __P((char **, const Char **, Str *, int)); 105 static void put_color __P((Str *)); 106 static void print_color __P((Char *, size_t, Char)); 107 108 /* set_color_context(): 109 */ 110 void 111 set_color_context() 112 { 113 struct varent *vp = adrof(STRcolor); 114 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; 121 } else { 122 size_t i; 123 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; 131 } 132 } 133 134 135 /* getstring(): 136 */ 137 static int 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 */ 143 { 144 const Char *s = *sp; 145 char *d = *dp; 146 eChar sc; 147 148 while (*s && (*s & CHAR) != (Char)f && (*s & CHAR) != ':') { 149 if ((*s & CHAR) == '\\' || (*s & CHAR) == '^') { 150 if ((sc = parseescape(&s)) == CHAR_ERR) 151 return 0; 152 } 153 else 154 sc = *s++ & CHAR; 155 d += one_wctomb(d, sc); 156 } 157 158 pd->s = *dp; 159 pd->len = (int) (d - *dp); 160 *sp = s; 161 *dp = d; 162 return *s == (Char)f; 163 } 164 165 166 /* parseLS_COLORS(): 167 * Parse the LS_COLORS environment variable 168 */ 169 void 170 parseLS_COLORS(value) 171 Char *value; /* LS_COLOR variable's 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 179 (void) &e; 180 181 /* init */ 182 if (extensions) 183 xfree((ptr_t) 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 = (Extension *) xmalloc((size_t) (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 getexit(osetexit); 211 212 if (setexit() == 0) { 213 214 /* parse */ 215 while (*v) { 216 switch (*v & CHAR) { 217 case ':': 218 v++; 219 continue; 220 221 case '*': /* :*ext=color: */ 222 v++; 223 if (getstring(&c, &v, &e->extension, '=') && 224 0 < e->extension.len) { 225 v++; 226 getstring(&c, &v, &e->color, ':'); 227 e++; 228 continue; 229 } 230 break; 231 232 default: /* :vl=color: */ 233 if (v[0] && v[1] && (v[2] & CHAR) == '=') { 234 for (i = 0; i < nvariables; i++) 235 if ((Char)variables[i].variable[0] == (v[0] & CHAR) && 236 (Char)variables[i].variable[1] == (v[1] & CHAR)) 237 break; 238 if (i < nvariables) { 239 v += 3; 240 getstring(&c, &v, &variables[i].color, ':'); 241 continue; 242 } 243 else 244 stderror(ERR_BADCOLORVAR, v[0], v[1]); 245 } 246 break; 247 } 248 while (*v && (*v & CHAR) != ':') 249 v++; 250 } 251 } 252 253 resexit(osetexit); 254 255 nextensions = (int) (e - extensions); 256 } 257 258 259 /* put_color(): 260 */ 261 static void 262 put_color(color) 263 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 for (i = color->len; 0 < i; i--) 271 xputchar(*c++); 272 output_raw = original_output_raw; 273 } 274 275 276 /* print_color(): 277 */ 278 static void 279 print_color(fname, len, suffix) 280 Char *fname; 281 size_t len; 282 Char suffix; 283 { 284 size_t i; 285 char *filename = short2str(fname); 286 char *last = filename + len; 287 Str *color = &variables[VFile].color; 288 289 switch (suffix) { 290 case '>': /* File is a symbolic link pointing to 291 * a directory */ 292 color = &variables[VDir].color; 293 break; 294 case '+': /* File is a hidden directory [aix] or 295 * context dependent [hpux] */ 296 case ':': /* File is network special [hpux] */ 297 break; 298 default: 299 for (i = 0; i < nvariables; i++) 300 if (variables[i].suffix != NOS && 301 (Char)variables[i].suffix == suffix) { 302 color = &variables[i].color; 303 break; 304 } 305 if (i == nvariables) { 306 for (i = 0; i < nextensions; i++) 307 if (strncmp(last - extensions[i].extension.len, 308 extensions[i].extension.s, 309 extensions[i].extension.len) == 0) { 310 color = &extensions[i].color; 311 break; 312 } 313 } 314 break; 315 } 316 317 put_color(&variables[VLeft].color); 318 put_color(color); 319 put_color(&variables[VRight].color); 320 } 321 322 323 /* print_with_color(): 324 */ 325 void 326 print_with_color(filename, len, suffix) 327 Char *filename; 328 size_t len; 329 Char suffix; 330 { 331 if (color_context_lsmF && 332 (haderr ? (didfds ? is2atty : isdiagatty) : 333 (didfds ? is1atty : isoutatty))) { 334 print_color(filename, len, suffix); 335 xprintf("%S", filename); 336 if (0 < variables[VEnd].color.len) 337 put_color(&variables[VEnd].color); 338 else { 339 put_color(&variables[VLeft].color); 340 put_color(&variables[VNormal].color); 341 put_color(&variables[VRight].color); 342 } 343 } 344 else 345 xprintf("%S", filename); 346 xputwchar(suffix); 347 } 348 349 350 #endif /* COLOR_LS_F */ 351