*/
#include <ctype.h>
+#include <libgen.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "common.h"
-void usage(void);
+static const char *types[11] = { "di", "ln", "so", "pi", "ex", "bd", "cd", "su",
+ "sg", "tw", "ow" };
+
+void usage(const char *);
char *tols_colors(char *);
+char *tolscolors(char *);
int
main(int argc, char **argv)
{
if (argc != 2)
- usage();
+ usage(argv[0]);
- if (strchr(argv[1], '=')) {
- printf("%s\n", tolscolors(argv[1]));
- return (0);
- } else {
- printf("%s\n", tols_colors(argv[1]));
- return (0);
- }
+ char *input = NULL;
+ if (strcmp(argv[1], "-") == 0) {
+ char *buf = NULL;
+ size_t linecap = 0;
+ getline(&buf, &linecap, stdin);
+ if (buf[sizeof(buf) - 1] == '\n')
+ buf[sizeof(buf) - 1] = '\0';
+ input = strdup(buf);
+ free(buf);
+ } else
+ input = argv[1];
+
+ if (strchr(input, '='))
+ fprintf(stdout, "%s\n", tolscolors(input));
+ else
+ fprintf(stdout, "%s\n", tols_colors(input));
- /* NOTREACHED */
return (0);
}
sprintf(ls_out + strlen(ls_out), "%s=", types[i]);
if (isupper(lscolors[2 * i]))
sprintf(ls_out + strlen(ls_out), "01;");
+ if (isupper(lscolors[2 * i + 1]))
+ sprintf(ls_out + strlen(ls_out), "04;");
if (tolower(lscolors[2 * i]) == 'x')
sprintf(ls_out + strlen(ls_out), "00");
else if (tolower(lscolors[2 * i] != 'x'))
- sprintf(ls_out + strlen(ls_out), "3%i",
- (int)(strchr(col, tolower(lscolors[2 * i])) - col));
+ sprintf(ls_out + strlen(ls_out), "3%i", (int)tolower(lscolors[2 * i]) - 97);
if (tolower(lscolors[2 * i + 1]) != 'x')
- sprintf(ls_out + strlen(ls_out), ";4%i",
- (int)(strchr(col, tolower(lscolors[2 * i])) - col));
+ sprintf(ls_out + strlen(ls_out), ";4%i", (int)tolower(lscolors[2 * i + 1]) - 97);
sprintf(ls_out + strlen(ls_out), ":");
}
char *ret = strdup(ls_out);
return (ret);
}
+char *
+tolscolors(char *dircolor)
+{
+ char *ent;
+ char key[2] = "", val[MAXKEYLEN] = "";
+ char out[22] = "xxxxxxxxxxxxxxxxxxxx";
+ struct color color;
+
+ while ((ent = strsep(&dircolor, ":")) != NULL) {
+ for (int i = 0; i < 11; i++) {
+ if (strncmp(ent, types[i], strlen(types[i])) == 0) {
+ sscanf(ent, "%c%c=%s", &key[0], &key[1], val);
+ parseansi(val, &color);
+ out[2 * i] = color.fg;
+ out[2 * i + 1] = color.bg;
+ }
+ }
+ }
+ char *ret = strdup(out);
+ return (ret);
+}
+
void
-usage(void)
+usage(const char *progname)
{
- (void)fprintf(stderr, "usage: %s LSCOLORS|LS_COLORS\n", getprogname());
+ char *path;
+ path = strdup(progname);
+
+ (void)fprintf(
+ stderr, "usage: %s LSCOLORS | LS_COLORS\n", basename(path));
exit(EX_USAGE);
}