]> git.cameronkatri.com Git - opendircolors.git/commitdiff
All works
authorCameron Katri <me@cameronkatri.com>
Mon, 26 Apr 2021 20:40:28 +0000 (16:40 -0400)
committerCameron Katri <me@cameronkatri.com>
Mon, 26 Apr 2021 20:40:28 +0000 (16:40 -0400)
.gitignore
Makefile
common.c [new file with mode: 0644]
common.h [new file with mode: 0644]
dirconvert.c
opendircolors.c

index 94053f2925089b71528f389c6d95a6598589dd8c..5afb1267187c72f890db316ff6bb813854979539 100644 (file)
@@ -1 +1,2 @@
 obj/*
+*.core
index 187a51581b2d3c925e92716ab8b07693481c6183..557bf5c6d23a545f1b97676ae528dba64bdc92b2 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 PROGS= opendircolors dirconvert
-SRCS.opendircolors=    opendircolors.c
-SRCS.dirconvert=       dirconvert.c
+SRCS.opendircolors=    opendircolors.c common.c
+SRCS.dirconvert=       dirconvert.c common.c
 #TODO: Write manpage
 MAN=   
 
diff --git a/common.c b/common.c
new file mode 100644 (file)
index 0000000..f3a100b
--- /dev/null
+++ b/common.c
@@ -0,0 +1,78 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2021
+ *     Cameron Katri.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY CAMERON KATRI AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL CAMERON KATRI OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <ctype.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "common.h"
+
+char
+numtocol(char c, bool bold)
+{
+       char buf = col[strtol(&c, NULL, 10)];
+       if (bold)
+               return (toupper(buf));
+       else
+               return (buf);
+}
+
+char *
+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);
+                                               }
+                                       }
+                               }
+                       }
+               }
+       }
+       char *ret = strdup(out);
+       return (ret);
+}
diff --git a/common.h b/common.h
new file mode 100644 (file)
index 0000000..b82776b
--- /dev/null
+++ b/common.h
@@ -0,0 +1,39 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2021
+ *     Cameron Katri.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY CAMERON KATRI AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL CAMERON KATRI OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef 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);
+
+#endif
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
index f6ec9c2d2f37fa804293d1c8f0ef35ae57bab673..726ad3f7259ee7c7053502859d8cd80f37863f69 100644 (file)
 #include <string.h>
 #include <sysexits.h>
 
+#include "common.h"
+
 void usage(void);
 
-static const struct option long_options[] = { { "help", no_argument, NULL,
-                                                 'h' },
-       { "bourne-shell", no_argument, NULL, 'b' },
-       { "sh", no_argument, NULL, 'b' },
-       { "print-database", no_argument, NULL, 'p' },
-       { NULL, no_argument, NULL, 0 } };
+#define MAXKEYLEN 21
+
+static const struct option long_options[] = {      /* no clang-format */
+       { "help", no_argument, NULL, 'h' },         /**/
+       { "bourne-shell", no_argument, NULL, 'b' }, /**/
+       { "sh", no_argument, NULL, 'b' },           /**/
+       { "csh", no_argument, NULL, 'c' },          /**/
+       { NULL, no_argument, NULL, 0 }
+};
+
+static const char *long_types[38] = { "NORMAL", "NORM", "FILE", "RESET", "DIR",
+       "LNK", "LINK", "SYMLINK", "ORPHAN", "MISSING", "FIFO", "PIPE", "SOCK",
+       "BLK", "BLOCK", "CHR", "CHAR", "DOOR", "EXEC", "LEFT", "LEFTCODE",
+       "RIGHT", "RIGHTCODE", "END", "ENDCODE", "SUID", "SETUID", "SGID",
+       "SETGID", "STICKY", "OTHER_WRITABLE", "OWR", "STICKY_OTHER_WRITABLE",
+       "OWT", "CAPABILITY", "MULTIHARDLINK", "CLRTOEOL", NULL };
+
+static const char *short_types[38] = { "no", "no", "fi", "rs", "di", "ln", "ln",
+       "ln", "or", "mi", "pi", "pi", "so", "bd", "bd", "cd", "cd", "do", "ex",
+       "lc", "lc", "rc", "rc", "ec", "ec", "su", "su", "sg", "sg", "st", "ow",
+       "ow", "tw", "tw", "ca", "mh", "cl", NULL };
 
 int
 main(int argc, char **argv)
 {
-       /* TODO */
-       usage();
+       int ch;
+       char *prefix = "LS_COLORS='";
+       char *suffix = "';\nexport LS_COLORS";
+       char *lsprefix = "LSCOLORS='";
+       char *lssuffix = "';\nexport LSCOLORS";
+
+       while (
+           (ch = getopt_long(argc, argv, "hbc", long_options, NULL)) != -1) {
+               switch (ch) {
+               case 'b':
+                       prefix = "LS_COLORS='";
+                       suffix = "';\nexport LS_COLORS";
+                       lsprefix = "LSCOLORS='";
+                       lssuffix = "';\nexport LSCOLORS";
+                       break;
+               case 'c':
+                       prefix = "setenv LS_COLORS '";
+                       suffix = "'";
+                       lsprefix = "setenv LSCOLORS '";
+                       lssuffix = "'";
+                       break;
+               case 'h':
+               case '?':
+               default:
+                       usage();
+               }
+       }
+       argc -= optind;
+       argv += optind;
+
+       char *path = *argv != NULL ? *argv++ : "-";
+       FILE *fd = fopen(path, "r");
+
+       char *line = NULL;
+       size_t linecap = 0;
+       ssize_t linelen;
+       char *out = strdup("");
+       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);
+                       continue;
+                       ;
+               } else if (*line == '*') {
+                       sprintf(out + strlen(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);
+                               break;
+                       }
+               }
+       }
+       free(line);
+       printf("%s%s%s\n", prefix, out, suffix);
+       printf("%s%s%s\n", lsprefix, tolscolors(out), lssuffix);
+       free(out);
 
        /* NOTREACHED */
-       return 0;
+       return (0);
 }
 
 void
 usage(void)
 {
-       (void)fprintf(stderr, "usage: %s [-bchp] [FILE]\n", getprogname());
+       (void)fprintf(stderr, "usage: %s [-bch] [FILE]\n", getprogname());
        exit(EX_USAGE);
 }