]> git.cameronkatri.com Git - opendircolors.git/blob - convert.c
004e8c48cbcb63132d075923f8179720282acf52
[opendircolors.git] / convert.c
1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2021
5 * Cameron Katri. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY CAMERON KATRI AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL CAMERON KATRI OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <ctype.h>
30 #include <stdbool.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sysexits.h>
35
36 const char *types[11] = { "di", "ln", "so", "pi", "ex", "bd", "cd", "su", "sg",
37 "tw", "ow" };
38 const char col[8] = "abcdefgh";
39
40 int tolscolors(char *);
41 int tols_colors(char *);
42 void usage(void);
43 char numtocol(char, bool);
44
45 int
46 main(int argc, char **argv)
47 {
48 if (argc != 2)
49 usage();
50
51 if (strchr(argv[1], '='))
52 return tolscolors(argv[1]);
53 else
54 return tols_colors(argv[1]);
55
56 /* NOTREACHED */
57 return 0;
58 }
59
60 int
61 tolscolors(char *dircolor)
62 {
63 char *ent, *buf, *val, *val2;
64 char out[22] = "xxxxxxxxxxxxxxxxxxxxxx";
65 bool bold = false;
66
67 while ((ent = buf = strsep(&dircolor, ":")) != NULL) {
68 for (int i = 0; i < 11; i++) {
69 if (strncmp(ent, types[i], strlen(types[i])) == 0) {
70 bold = false;
71 while ((val = strsep(&buf, "=")) != NULL) {
72 while ((val2 = strsep(&val, ";")) !=
73 NULL) {
74 if (strcmp(val2, "01") == 0) {
75 bold = true;
76 } else if (val2[0] == '3') {
77 out[2 * i] = numtocol(
78 val2[1], bold);
79 } else if (val2[0] == '4') {
80 out[2 * i + 1] =
81 numtocol(
82 val2[1], NULL);
83 }
84 }
85 }
86 }
87 }
88 }
89 printf("%s\n", out);
90 return 0;
91 }
92
93 char
94 numtocol(char c, bool bold)
95 {
96 char buf = col[strtol(&c, NULL, 10)];
97 if (bold)
98 return toupper(buf);
99 else
100 return buf;
101 }
102
103 int
104 tols_colors(char *lscolors)
105 {
106 char *ls_out = strdup("");
107
108 for (int i = 0; i < 11; i++) {
109 if (lscolors[2 * i] == 'x' && lscolors[2 * i + 1] == 'x')
110 continue;
111 sprintf(ls_out + strlen(ls_out), "%s=", types[i]);
112 if (isupper(lscolors[2 * i]))
113 sprintf(ls_out + strlen(ls_out), "01;");
114 if (tolower(lscolors[2 * i]) == 'x')
115 sprintf(ls_out + strlen(ls_out), "00");
116 else if (tolower(lscolors[2 * i] != 'x'))
117 sprintf(ls_out + strlen(ls_out), "3%i",
118 (int)(strchr(col, tolower(lscolors[2 * i])) - col));
119 if (tolower(lscolors[2 * i + 1]) != 'x')
120 sprintf(ls_out + strlen(ls_out), ";4%i",
121 (int)(strchr(col, tolower(lscolors[2 * i])) - col));
122 sprintf(ls_out + strlen(ls_out), ":");
123 }
124 printf("%s\n", ls_out);
125 free(ls_out);
126 return 0;
127 }
128
129 void
130 usage(void)
131 {
132 (void)fprintf(stderr, "%s\n", "usage: lscolors LSCOLORS|LS_COLORS ");
133 exit(EX_USAGE);
134 }