#include "sh.h"
RCSID("$Id: tw.color.c,v 1.18 2005/03/03 16:40:53 kim Exp $")
#include "tw.h"
#include "ed.h"
#include "tc.h"
#ifdef COLOR_LS_F
typedef struct {
const char *s;
int len;
} Str;
#define VAR(suffix,variable,defaultcolor) \
{ \
suffix, variable, { defaultcolor, sizeof(defaultcolor) - 1 }, \
{ defaultcolor, sizeof(defaultcolor) - 1 } \
}
#define NOS '\0'
typedef struct {
const char suffix;
const char *variable;
Str color;
Str defaultcolor;
} Variable;
static Variable variables[] = {
VAR('/', "di", "01;34"),
VAR('@', "ln", "01;36"),
VAR('&', "or", ""),
VAR('|', "pi", "33"),
VAR('=', "so", "01;35"),
VAR('>', "do", "01;35"),
VAR('#', "bd", "01;33"),
VAR('%', "cd", "01;33"),
VAR('*', "ex", "01;32"),
VAR(NOS, "fi", "0"),
VAR(NOS, "no", "0"),
VAR(NOS, "mi", ""),
#ifdef IS_ASCII
VAR(NOS, "lc", "\033["),
#else
VAR(NOS, "lc", "\x27["),
#endif
VAR(NOS, "rc", "m"),
VAR(NOS, "ec", ""),
};
enum FileType {
VDir, VSym, VOrph, VPipe, VSock, VDoor, VBlock, VChr, VExe,
VFile, VNormal, VMiss, VLeft, VRight, VEnd
};
#define nvariables (sizeof(variables)/sizeof(variables[0]))
typedef struct {
Str extension;
Str color;
} Extension;
static Extension *extensions = NULL;
static size_t nextensions = 0;
static char *colors = NULL;
int color_context_ls = FALSE;
static int color_context_lsmF = FALSE;
static int getstring __P((char **, const Char **, Str *, int));
static void put_color __P((Str *));
static void print_color __P((Char *, size_t, Char));
void
set_color_context()
{
struct varent *vp = adrof(STRcolor);
if (vp == NULL || vp->vec == NULL) {
color_context_ls = FALSE;
color_context_lsmF = FALSE;
} else if (!vp->vec[0] || vp->vec[0][0] == '\0') {
color_context_ls = TRUE;
color_context_lsmF = TRUE;
} else {
size_t i;
color_context_ls = FALSE;
color_context_lsmF = FALSE;
for (i = 0; vp->vec[i]; i++)
if (Strcmp(vp->vec[i], STRls) == 0)
color_context_ls = TRUE;
else if (Strcmp(vp->vec[i], STRlsmF) == 0)
color_context_lsmF = TRUE;
}
}
static int
getstring(dp, sp, pd, f)
char **dp;
const Char **sp;
Str *pd;
int f;
{
const Char *s = *sp;
char *d = *dp;
eChar sc;
while (*s && (*s & CHAR) != (Char)f && (*s & CHAR) != ':') {
if ((*s & CHAR) == '\\' || (*s & CHAR) == '^') {
if ((sc = parseescape(&s)) == CHAR_ERR)
return 0;
}
else
sc = *s++ & CHAR;
d += one_wctomb(d, sc);
}
pd->s = *dp;
pd->len = (int) (d - *dp);
*sp = s;
*dp = d;
return *s == (Char)f;
}
void
parseLS_COLORS(value)
Char *value;
{
size_t i, len;
const Char *v;
char *c;
Extension *volatile e;
jmp_buf_t osetexit;
(void) &e;
if (extensions)
xfree((ptr_t) extensions);
for (i = 0; i < nvariables; i++)
variables[i].color = variables[i].defaultcolor;
colors = NULL;
extensions = NULL;
nextensions = 0;
if (value == NULL)
return;
len = Strlen(value);
i = 1;
for (v = value; *v; v++)
if ((*v & CHAR) == ':')
i++;
extensions = (Extension *) xmalloc((size_t) (len + i * sizeof(Extension)));
colors = i * sizeof(Extension) + (char *)extensions;
nextensions = 0;
v = value;
c = colors;
e = &extensions[0];
getexit(osetexit);
if (setexit() == 0) {
while (*v) {
switch (*v & CHAR) {
case ':':
v++;
continue;
case '*':
v++;
if (getstring(&c, &v, &e->extension, '=') &&
0 < e->extension.len) {
v++;
getstring(&c, &v, &e->color, ':');
e++;
continue;
}
break;
default:
if (v[0] && v[1] && (v[2] & CHAR) == '=') {
for (i = 0; i < nvariables; i++)
if ((Char)variables[i].variable[0] == (v[0] & CHAR) &&
(Char)variables[i].variable[1] == (v[1] & CHAR))
break;
if (i < nvariables) {
v += 3;
getstring(&c, &v, &variables[i].color, ':');
continue;
}
else
stderror(ERR_BADCOLORVAR, v[0], v[1]);
}
break;
}
while (*v && (*v & CHAR) != ':')
v++;
}
}
resexit(osetexit);
nextensions = (int) (e - extensions);
}
static void
put_color(color)
Str *color;
{
size_t i;
const char *c = color->s;
int original_output_raw = output_raw;
output_raw = TRUE;
for (i = color->len; 0 < i; i--)
xputchar(*c++);
output_raw = original_output_raw;
}
static void
print_color(fname, len, suffix)
Char *fname;
size_t len;
Char suffix;
{
size_t i;
char *filename = short2str(fname);
char *last = filename + len;
Str *color = &variables[VFile].color;
switch (suffix) {
case '>':
color = &variables[VDir].color;
break;
case '+':
case ':':
break;
default:
for (i = 0; i < nvariables; i++)
if (variables[i].suffix != NOS &&
(Char)variables[i].suffix == suffix) {
color = &variables[i].color;
break;
}
if (i == nvariables) {
for (i = 0; i < nextensions; i++)
if (strncmp(last - extensions[i].extension.len,
extensions[i].extension.s,
extensions[i].extension.len) == 0) {
color = &extensions[i].color;
break;
}
}
break;
}
put_color(&variables[VLeft].color);
put_color(color);
put_color(&variables[VRight].color);
}
void
print_with_color(filename, len, suffix)
Char *filename;
size_t len;
Char suffix;
{
if (color_context_lsmF &&
(haderr ? (didfds ? is2atty : isdiagatty) :
(didfds ? is1atty : isoutatty))) {
print_color(filename, len, suffix);
xprintf("%S", filename);
if (0 < variables[VEnd].color.len)
put_color(&variables[VEnd].color);
else {
put_color(&variables[VLeft].color);
put_color(&variables[VNormal].color);
put_color(&variables[VRight].color);
}
}
else
xprintf("%S", filename);
xputwchar(suffix);
}
#endif