]> git.cameronkatri.com Git - opendircolors.git/commitdiff
Revamp to improve everything, and support D30547
authorCameron Katri <me@cameronkatri.com>
Sun, 30 May 2021 19:59:03 +0000 (15:59 -0400)
committerCameron Katri <me@cameronkatri.com>
Sun, 30 May 2021 19:59:03 +0000 (15:59 -0400)
https://reviews.freebsd.org/D30547

common.c
common.h
dirconvert.1
dirconvert.c
opendircolors.c

index afbb27ef31386d20158622cd163ed505481d21e8..bbabfc99b9373489059f43410b0b445fe8aa7541 100644 (file)
--- a/common.c
+++ b/common.c
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <err.h>
 
 #include "common.h"
 
-char
-numtocol(char c, bool bold)
+struct color *
+parseansi(char *ansi, struct color *color)
 {
-       char buf = col[strtol(&c, NULL, 10)];
-       if (bold)
-               return (toupper(buf));
-       else
-               return (buf);
-}
+       long long num;
+       char *token;
+       const char *errstr;
+       bool bold = false, underline = false;
 
-char *
-tolscolors(char *dircolor)
-{
-       char *ent, *buf, *val, *val2;
-       char out[22] = "xxxxxxxxxxxxxxxxxxxxxx";
-       bool bold = false;
+       color->fg = 'x';
+       color->bg = 'x';
 
-       while ((ent = buf = strsep(&dircolor, ":")) != NULL) {
-               for (int i = 0; i < 11; i++) {
-                       if (strncmp(ent, types[i], strlen(types[i])) == 0) {
-                               bold = false;
-                               while ((val = strsep(&buf, "=")) != NULL) {
-                                       while ((val2 = strsep(&val, ";")) !=
-                                           NULL) {
-                                               if (strcmp(val2, "01") == 0) {
-                                                       bold = true;
-                                               } else if (val2[0] == '3') {
-                                                       out[2 * i] = numtocol(
-                                                           val2[1], bold);
-                                               } else if (val2[0] == '4') {
-                                                       out[2 * i + 1] =
-                                                           numtocol(
-                                                               val2[1], NULL);
-                                               }
-                                       }
-                               }
-                       }
+       while ((token = strsep(&ansi, ";")) != NULL) {
+               num = strtonum(token, 0, 47, NULL);
+               switch (num) {
+                       case 1:
+                               bold = true;
+                               break;
+                       case 4:
+                               underline = true;
+                               break;
+                       default:
+                               if (num <= 37)
+                                       color->fg = (char)((num - 30) + 97);
+                               else if (num <= 47)
+                                       color->bg = (char)((num - 40) + 97);
                }
        }
-       char *ret = strdup(out);
-       return (ret);
+       if (bold)
+               color->fg = toupper(color->fg);
+       if (underline)
+               color->bg = toupper(color->bg);
+
+       return color;
 }
index b82776b05a557e52cfc51731a90491759229dfe8..fa181d8ec973d0021ffae6b639b1bb74fad24205 100644 (file)
--- a/common.h
+++ b/common.h
 #define COMMON_H
 #include <stdbool.h>
 
-static const char *types[11] = { "di", "ln", "so", "pi", "ex", "bd", "cd", "su",
-       "sg", "tw", "ow" };
-static const char col[8] = "abcdefgh";
-char *tolscolors(char *);
-char numtocol(char, bool);
+#define MAXKEYLEN 21
+
+struct color {
+       char fg;
+       char bg;
+};
+
+struct color *parseansi(char *, struct color*);
 
 #endif
index c92cc32487b3f650d38e6123fad3f67ecedb0205..267a47219bba1107d08fb01756e798f077d2b1dd 100644 (file)
@@ -40,7 +40,6 @@ If the input is a single dash
 .Pq Sq Fl
 .Nm
 reads the first line from the standard input.
-.El
 .Sh EXIT STATUS
 .Ex -std
 .Sh EXAMPLE
index 41982c14ce57235cc3f9d4f83549aaf22b2459bb..b2bf6e6a4736957327de88ad14d5935177f32644 100644 (file)
@@ -28,6 +28,7 @@
 
 #include <ctype.h>
 #include <libgen.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include "common.h"
 
+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)
@@ -75,14 +80,14 @@ tols_colors(char *lscolors)
                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);
@@ -90,6 +95,28 @@ tols_colors(char *lscolors)
        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(const char *progname)
 {
index 21855baacc53ec21c217bbb20eb84118f12fdf0d..9be81f6f9ea47f1e1324a2c794f8d7ae89f034d2 100644 (file)
  * SUCH DAMAGE.
  */
 
+#include <ctype.h>
 #include <err.h>
 #include <errno.h>
 #include <getopt.h>
 #include <libgen.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -37,9 +39,7 @@
 
 #include "common.h"
 
-void usage(const char *);
-
-#define MAXKEYLEN 21
+static void usage(const char *);
 
 static const struct option long_options[] = {      /* no clang-format */
        { "help", no_argument, NULL, 'h' },         /**/
@@ -62,6 +62,11 @@ static const char *short_types[38] = { "no", "no", "fi", "rs", "di", "ln", "ln",
        "lc", "lc", "rc", "rc", "ec", "ec", "su", "su", "sg", "sg", "st", "ow",
        "ow", "tw", "tw", "ca", "mh", "cl", NULL };
 
+static const int indexes[37] = { -1, -1, -1, -1, 0, 1, 1,
+       1, -1, -1, 3, 3, 2, 5, 5, 6, 6, -1, 4,
+       -1, -1, -1, -1, -1, -1, 7, 7, 8, 8, -1, 10,
+       10, 9, 9, -1, -1, -1 };
+
 int
 main(int argc, char **argv)
 {
@@ -110,39 +115,46 @@ main(int argc, char **argv)
        char *line = NULL;
        size_t linecap = 0;
        ssize_t linelen;
-       char *out = strdup("");
+       char *ls_out = strdup("");
+       char lsout[22] = "xxxxxxxxxxxxxxxxxxxxxx";
+       struct color color;
+
        while ((linelen = getline(&line, &linecap, fd)) > 0) {
                if (*line == '#' || *line == '\n')
                        continue;
                char fmttype[MAXKEYLEN] = "", val[MAXKEYLEN] = "";
                sscanf(line, "%s %s\n", fmttype, val);
                if (*line == '.') {
-                       sprintf(out + strlen(out), "*%s=%s:", fmttype, val);
+                       sprintf(ls_out + strlen(ls_out), "*%s=%s:", fmttype, val);
                        continue;
                } else if (*line == '*') {
-                       sprintf(out + strlen(out), "%s=%s:", fmttype, val);
+                       sprintf(ls_out + strlen(ls_out), "%s=%s:", fmttype, val);
                        continue;
                }
                for (int i = 0; i < 37; i++) {
                        if (strcmp(fmttype, long_types[i]) == 0) {
-                               sprintf(out + strlen(out),
-                                   "%s=%s:", short_types[i], val);
+                               sprintf(ls_out + strlen(ls_out), "%s=%s:", short_types[i], val);
+                               parseansi(val, &color);
+                               if (indexes[i] >= 0) {
+                                       lsout[2 * indexes[i]] = color.fg;
+                                       lsout[2 * indexes[i] + 1] = color.bg;
+                               }
                                break;
                        }
                }
        }
        fclose(fd);
 
-       fprintf(stdout, "%s%s%s\n", prefix, out, suffix);
-       fprintf(stdout, "%s%s%s\n", lsprefix, tolscolors(out), lssuffix);
+       fprintf(stdout, "%s%s%s\n", prefix, ls_out, suffix);
+       fprintf(stdout, "%s%s%s\n", lsprefix, lsout, lssuffix);
 
        free(line);
-       free(out);
+       free(ls_out);
 
        return (0);
 }
 
-void
+static void
 usage(const char *progname)
 {
        char *path;