aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCameron Katri <me@cameronkatri.com>2021-04-26 16:40:28 -0400
committerCameron Katri <me@cameronkatri.com>2021-04-26 16:40:28 -0400
commit219b0151ae954faa63cdc0abed49bb4ddb53d75f (patch)
treef5ce6975b39b449146b1c327974f39b3430aedf3
parent2574ea03da54335dc1641696aa3bbf1cbe02f766 (diff)
downloadopendircolors-219b0151ae954faa63cdc0abed49bb4ddb53d75f.tar.gz
opendircolors-219b0151ae954faa63cdc0abed49bb4ddb53d75f.tar.zst
opendircolors-219b0151ae954faa63cdc0abed49bb4ddb53d75f.zip
All works
-rw-r--r--.gitignore1
-rw-r--r--Makefile4
-rw-r--r--common.c78
-rw-r--r--common.h39
-rw-r--r--dirconvert.c70
-rw-r--r--opendircolors.c97
6 files changed, 220 insertions, 69 deletions
diff --git a/.gitignore b/.gitignore
index 94053f2..5afb126 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
obj/*
+*.core
diff --git a/Makefile b/Makefile
index 187a515..557bf5c 100644
--- 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
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
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
diff --git a/dirconvert.c b/dirconvert.c
index 5f76258..5169a05 100644
--- a/dirconvert.c
+++ b/dirconvert.c
@@ -33,14 +33,10 @@
#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
diff --git a/opendircolors.c b/opendircolors.c
index f6ec9c2..726ad3f 100644
--- a/opendircolors.c
+++ b/opendircolors.c
@@ -32,28 +32,105 @@
#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);
}