* 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>
#include "common.h"
-void usage(void);
+static void usage(const char *);
-#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' }, /**/
+// clang-format off
+static const struct option long_options[] = {
+ { "help", no_argument, NULL, 'h' },
+ { "bourne-shell", no_argument, NULL, 0 },
+ { "sh", no_argument, NULL, 'b' },
+ { "csh", no_argument, NULL, 'c' },
+ { "c-shell", no_argument, NULL, 'c' },
{ NULL, no_argument, NULL, 0 }
};
+// clang-format on
static const char *long_types[38] = { "NORMAL", "NORM", "FILE", "RESET", "DIR",
"LNK", "LINK", "SYMLINK", "ORPHAN", "MISSING", "FIFO", "PIPE", "SOCK",
"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)
{
int ch;
+ FILE *fd;
char *prefix = "LS_COLORS='";
- char *suffix = "';\nexport LS_COLORS";
+ char *suffix = "';\nexport LS_COLORS;";
char *lsprefix = "LSCOLORS='";
- char *lssuffix = "';\nexport 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";
+ suffix = "';\nexport LS_COLORS;";
lsprefix = "LSCOLORS='";
- lssuffix = "';\nexport LSCOLORS";
+ lssuffix = "';\nexport LSCOLORS;";
break;
case 'c':
prefix = "setenv LS_COLORS '";
case 'h':
case '?':
default:
- usage();
+ usage(argv[0]);
}
}
argc -= optind;
argv += optind;
- char *path = *argv != NULL ? *argv++ : "-";
- FILE *fd = fopen(path, "r");
+ if (argc != 1)
+ usage(argv[1]);
+
+ char *path = *argv;
+ if (strcmp(path, "-") == 0)
+ fd = stdin;
+ else if ((fd = fopen(path, "r")) == NULL) {
+ warnx("%s: %s\n", path, strerror(errno));
+ return (errno);
+ }
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, ls_out, suffix);
+ fprintf(stdout, "%s%s%s\n", lsprefix, lsout, lssuffix);
+
free(line);
- printf("%s%s%s\n", prefix, out, suffix);
- printf("%s%s%s\n", lsprefix, tolscolors(out), lssuffix);
- free(out);
+ free(ls_out);
- /* NOTREACHED */
return (0);
}
-void
-usage(void)
+static void
+usage(const char *progname)
{
- (void)fprintf(stderr, "usage: %s [-bch] [FILE]\n", getprogname());
+ char *path;
+ path = strdup(progname);
+
+ (void)fprintf(stderr, "usage: %s [-bch] [FILE]\n", basename(path));
exit(EX_USAGE);
}