/contrib/tcsh/tw.color.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 350 lines · 252 code · 46 blank · 52 comment · 65 complexity · 728213fa46d464c8232f81b6fc4c96ee MD5 · raw file

  1. /* $Header: /p/tcsh/cvsroot/tcsh/tw.color.c,v 1.27 2010/08/19 05:52:19 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. RCSID("$tcsh: tw.color.c,v 1.27 2010/08/19 05:52:19 christos Exp $")
  35. #include "tw.h"
  36. #include "ed.h"
  37. #include "tc.h"
  38. #ifdef COLOR_LS_F
  39. typedef struct {
  40. const char *s;
  41. size_t len;
  42. } Str;
  43. #define VAR(suffix,variable,defaultcolor) \
  44. { \
  45. suffix, variable, { defaultcolor, sizeof(defaultcolor) - 1 }, \
  46. { defaultcolor, sizeof(defaultcolor) - 1 } \
  47. }
  48. #define NOS '\0' /* no suffix */
  49. typedef struct {
  50. const char suffix;
  51. const char *variable;
  52. Str color;
  53. Str defaultcolor;
  54. } Variable;
  55. static Variable variables[] = {
  56. VAR('/', "di", "01;34"), /* Directory */
  57. VAR('@', "ln", "01;36"), /* Symbolic link */
  58. VAR('&', "or", ""), /* Orphanned symbolic link (defaults to ln) */
  59. VAR('|', "pi", "33"), /* Named pipe (FIFO) */
  60. VAR('=', "so", "01;35"), /* Socket */
  61. VAR('>', "do", "01;35"), /* Door (solaris fast ipc mechanism) */
  62. VAR('#', "bd", "01;33"), /* Block device */
  63. VAR('%', "cd", "01;33"), /* Character device */
  64. VAR('*', "ex", "01;32"), /* Executable file */
  65. VAR(NOS, "fi", "0"), /* Regular file */
  66. VAR(NOS, "no", "0"), /* Normal (non-filename) text */
  67. VAR(NOS, "mi", ""), /* Missing file (defaults to fi) */
  68. #ifdef IS_ASCII
  69. VAR(NOS, "lc", "\033["), /* Left code (ASCII) */
  70. #else
  71. VAR(NOS, "lc", "\x27["), /* Left code (EBCDIC)*/
  72. #endif
  73. VAR(NOS, "rc", "m"), /* Right code */
  74. VAR(NOS, "ec", ""), /* End code (replaces lc+no+rc) */
  75. VAR(NOS, "su", ""), /* Setuid file (u+s) */
  76. VAR(NOS, "sg", ""), /* Setgid file (g+s) */
  77. VAR(NOS, "tw", ""), /* Sticky and other writable dir (+t,o+w) */
  78. VAR(NOS, "ow", ""), /* Other writable dir (o+w) but not sticky */
  79. VAR(NOS, "st", ""), /* Sticky dir (+t) but not other writable */
  80. VAR(NOS, "rs", "0"), /* Reset to normal color */
  81. VAR(NOS, "hl", "44;37"), /* Reg file extra hard links, obsolete? */
  82. VAR(NOS, "mh", "44;37"), /* Reg file extra hard links */
  83. VAR(NOS, "ca", "30;41"), /* File with capability */
  84. };
  85. enum FileType {
  86. VDir, VSym, VOrph, VPipe, VSock, VDoor, VBlock, VChr, VExe,
  87. VFile, VNormal, VMiss, VLeft, VRight, VEnd
  88. };
  89. #define nvariables (sizeof(variables)/sizeof(variables[0]))
  90. typedef struct {
  91. Str extension; /* file extension */
  92. Str color; /* color string */
  93. } Extension;
  94. static Extension *extensions = NULL;
  95. static size_t nextensions = 0;
  96. static char *colors = NULL;
  97. int color_context_ls = FALSE; /* do colored ls */
  98. static int color_context_lsmF = FALSE; /* do colored ls-F */
  99. static int getstring (char **, const Char **, Str *, int);
  100. static void put_color (const Str *);
  101. static void print_color (const Char *, size_t, Char);
  102. /* set_color_context():
  103. */
  104. void
  105. set_color_context(void)
  106. {
  107. struct varent *vp = adrof(STRcolor);
  108. if (vp == NULL || vp->vec == NULL) {
  109. color_context_ls = FALSE;
  110. color_context_lsmF = FALSE;
  111. } else if (!vp->vec[0] || vp->vec[0][0] == '\0') {
  112. color_context_ls = TRUE;
  113. color_context_lsmF = TRUE;
  114. } else {
  115. size_t i;
  116. color_context_ls = FALSE;
  117. color_context_lsmF = FALSE;
  118. for (i = 0; vp->vec[i]; i++)
  119. if (Strcmp(vp->vec[i], STRls) == 0)
  120. color_context_ls = TRUE;
  121. else if (Strcmp(vp->vec[i], STRlsmF) == 0)
  122. color_context_lsmF = TRUE;
  123. }
  124. }
  125. /* getstring():
  126. */
  127. static int
  128. getstring(char **dp, const Char **sp, Str *pd, int f)
  129. {
  130. const Char *s = *sp;
  131. char *d = *dp;
  132. eChar sc;
  133. while (*s && (*s & CHAR) != (Char)f && (*s & CHAR) != ':') {
  134. if ((*s & CHAR) == '\\' || (*s & CHAR) == '^') {
  135. if ((sc = parseescape(&s)) == CHAR_ERR)
  136. return 0;
  137. }
  138. else
  139. sc = *s++ & CHAR;
  140. d += one_wctomb(d, sc);
  141. }
  142. pd->s = *dp;
  143. pd->len = d - *dp;
  144. *sp = s;
  145. *dp = d;
  146. return *s == (Char)f;
  147. }
  148. /* parseLS_COLORS():
  149. * Parse the LS_COLORS environment variable
  150. */
  151. void
  152. parseLS_COLORS(const Char *value)
  153. {
  154. size_t i, len;
  155. const Char *v; /* pointer in value */
  156. char *c; /* pointer in colors */
  157. Extension *volatile e; /* pointer in extensions */
  158. jmp_buf_t osetexit;
  159. size_t omark;
  160. (void) &e;
  161. /* init */
  162. xfree(extensions);
  163. for (i = 0; i < nvariables; i++)
  164. variables[i].color = variables[i].defaultcolor;
  165. colors = NULL;
  166. extensions = NULL;
  167. nextensions = 0;
  168. if (value == NULL)
  169. return;
  170. len = Strlen(value);
  171. /* allocate memory */
  172. i = 1;
  173. for (v = value; *v; v++)
  174. if ((*v & CHAR) == ':')
  175. i++;
  176. extensions = xmalloc(len + i * sizeof(Extension));
  177. colors = i * sizeof(Extension) + (char *)extensions;
  178. nextensions = 0;
  179. /* init pointers */
  180. v = value;
  181. c = colors;
  182. e = &extensions[0];
  183. /* Prevent from crashing if unknown parameters are given. */
  184. omark = cleanup_push_mark();
  185. getexit(osetexit);
  186. if (setexit() == 0) {
  187. /* parse */
  188. while (*v) {
  189. switch (*v & CHAR) {
  190. case ':':
  191. v++;
  192. continue;
  193. case '*': /* :*ext=color: */
  194. v++;
  195. if (getstring(&c, &v, &e->extension, '=') &&
  196. 0 < e->extension.len) {
  197. v++;
  198. getstring(&c, &v, &e->color, ':');
  199. e++;
  200. continue;
  201. }
  202. break;
  203. default: /* :vl=color: */
  204. if (v[0] && v[1] && (v[2] & CHAR) == '=') {
  205. for (i = 0; i < nvariables; i++)
  206. if ((Char)variables[i].variable[0] == (v[0] & CHAR) &&
  207. (Char)variables[i].variable[1] == (v[1] & CHAR))
  208. break;
  209. if (i < nvariables) {
  210. v += 3;
  211. getstring(&c, &v, &variables[i].color, ':');
  212. continue;
  213. }
  214. else
  215. stderror(ERR_BADCOLORVAR, v[0], v[1]);
  216. }
  217. break;
  218. }
  219. while (*v && (*v & CHAR) != ':')
  220. v++;
  221. }
  222. }
  223. cleanup_pop_mark(omark);
  224. resexit(osetexit);
  225. nextensions = e - extensions;
  226. }
  227. /* put_color():
  228. */
  229. static void
  230. put_color(const Str *color)
  231. {
  232. size_t i;
  233. const char *c = color->s;
  234. int original_output_raw = output_raw;
  235. output_raw = TRUE;
  236. cleanup_push(&original_output_raw, output_raw_restore);
  237. for (i = color->len; 0 < i; i--)
  238. xputchar(*c++);
  239. cleanup_until(&original_output_raw);
  240. }
  241. /* print_color():
  242. */
  243. static void
  244. print_color(const Char *fname, size_t len, Char suffix)
  245. {
  246. size_t i;
  247. char *filename = short2str(fname);
  248. char *last = filename + len;
  249. Str *color = &variables[VFile].color;
  250. switch (suffix) {
  251. case '>': /* File is a symbolic link pointing to
  252. * a directory */
  253. color = &variables[VDir].color;
  254. break;
  255. case '+': /* File is a hidden directory [aix] or
  256. * context dependent [hpux] */
  257. case ':': /* File is network special [hpux] */
  258. break;
  259. default:
  260. for (i = 0; i < nvariables; i++)
  261. if (variables[i].suffix != NOS &&
  262. (Char)variables[i].suffix == suffix) {
  263. color = &variables[i].color;
  264. break;
  265. }
  266. if (i == nvariables) {
  267. for (i = 0; i < nextensions; i++)
  268. if (len >= extensions[i].extension.len
  269. && strncmp(last - extensions[i].extension.len,
  270. extensions[i].extension.s,
  271. extensions[i].extension.len) == 0) {
  272. color = &extensions[i].color;
  273. break;
  274. }
  275. }
  276. break;
  277. }
  278. put_color(&variables[VLeft].color);
  279. put_color(color);
  280. put_color(&variables[VRight].color);
  281. }
  282. /* print_with_color():
  283. */
  284. void
  285. print_with_color(const Char *filename, size_t len, Char suffix)
  286. {
  287. if (color_context_lsmF &&
  288. (haderr ? (didfds ? is2atty : isdiagatty) :
  289. (didfds ? is1atty : isoutatty))) {
  290. print_color(filename, len, suffix);
  291. xprintf("%S", filename);
  292. if (0 < variables[VEnd].color.len)
  293. put_color(&variables[VEnd].color);
  294. else {
  295. put_color(&variables[VLeft].color);
  296. put_color(&variables[VNormal].color);
  297. put_color(&variables[VRight].color);
  298. }
  299. }
  300. else
  301. xprintf("%S", filename);
  302. xputwchar(suffix);
  303. }
  304. #endif /* COLOR_LS_F */