]> git.cameronkatri.com Git - opendircolors.git/blobdiff - dirconvert.c
All works
[opendircolors.git] / dirconvert.c
index 5f762584f3ab57181dd36bdd2a982899df2d5bf6..5169a0566b45909495ffb7e1f35160e9b91555f1 100644 (file)
 #include <string.h>
 #include <sysexits.h>
 
-const char *types[11] = { "di", "ln", "so", "pi", "ex", "bd", "cd", "su", "sg",
-       "tw", "ow" };
-const char col[8] = "abcdefgh";
+#include "common.h"
 
 void usage(void);
-int tolscolors(char *);
-int tols_colors(char *);
-char numtocol(char, bool);
+char *tols_colors(char *);
 
 int
 main(int argc, char **argv)
@@ -48,59 +44,19 @@ main(int argc, char **argv)
        if (argc != 2)
                usage();
 
-       if (strchr(argv[1], '='))
-               return tolscolors(argv[1]);
-       else
-               return tols_colors(argv[1]);
-
-       /* NOTREACHED */
-       return 0;
-}
-
-int
-tolscolors(char *dircolor)
-{
-       char *ent, *buf, *val, *val2;
-       char out[22] = "xxxxxxxxxxxxxxxxxxxxxx";
-       bool bold = false;
-
-       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);
-                                               }
-                                       }
-                               }
-                       }
-               }
+       if (strchr(argv[1], '=')) {
+               printf("%s\n", tolscolors(argv[1]));
+               return (0);
+       } else {
+               printf("%s\n", tols_colors(argv[1]));
+               return (0);
        }
-       printf("%s\n", out);
-       return 0;
-}
 
-char
-numtocol(char c, bool bold)
-{
-       char buf = col[strtol(&c, NULL, 10)];
-       if (bold)
-               return toupper(buf);
-       else
-               return buf;
+       /* NOTREACHED */
+       return (0);
 }
 
-int
+char *
 tols_colors(char *lscolors)
 {
        char *ls_out = strdup("");
@@ -121,9 +77,9 @@ tols_colors(char *lscolors)
                            (int)(strchr(col, tolower(lscolors[2 * i])) - col));
                sprintf(ls_out + strlen(ls_out), ":");
        }
-       printf("%s\n", ls_out);
+       char *ret = strdup(ls_out);
        free(ls_out);
-       return 0;
+       return (ret);
 }
 
 void