1 /* -*- mode: c; c-file-style: "gnu" -*-
2 * ccze-dump.c -- Dump internal color table
3 * Copyright (C) 2002, 2003 Gergely Nagy <algernon@bonehunter.rulez.org>
4 *
5 * This file is part of ccze.
6 *
7 * ccze is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * ccze is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 * License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #define CCZE_DUMP 1
23
24 #include <ccze.h>
25 #ifdef HAVE_ARGP_H
26 # include <argp.h>
27 #endif
28
29 #include "ccze-color.c"
30 #include "ccze-compat.h"
31
32 ccze_config_t ccze_config = {
33 .scroll = 1,
34 .convdate = 0,
35 .remfac = 0,
36 .wcol = 1,
37 .slookup = 1,
38 .rcfile = NULL,
39 .cssfile = NULL,
40 .transparent = 1,
41 .pluginlist_len = 0,
42 .pluginlist_alloc = 10,
43 .color_argv_len = 0,
44 .color_argv_alloc = 10,
45 .mode = CCZE_MODE_CURSES
46 };
47
48 const char *argp_program_name = "ccze-dump";
49 const char *argp_program_version = "ccze-dump (ccze 0.1." PATCHLEVEL ")";
50 const char *argp_program_bug_address = "<algernon@bonehunter.rulez.org>";
51 static struct argp_option options[] = {
52 {NULL, 0, NULL, 0, "", 1},
53 {"rcfile", 'F', "FILE", 0, "Read configuration from FILE", 1},
54 {"load", 'l', NULL, 0, "Load default configuration files", 1},
55 {NULL, 0, NULL, 0, NULL, 0}
56 };
57
58 static error_t parse_opt (int key, char *arg, struct argp_state *state);
59 static struct argp argp =
60 {options, parse_opt, 0, "ccze -- cheer up 'yer logs.", NULL, NULL, NULL};
61
62 static int ccze_loaddefs = 0;
63
64 static error_t
parse_opt(int key,char * arg,struct argp_state * state)65 parse_opt (int key, char *arg, struct argp_state *state)
66 {
67 switch (key)
68 {
69 case 'F':
70 ccze_config.rcfile = arg;
71 break;
72 case 'l':
73 ccze_loaddefs = 1;
74 break;
75 default:
76 return ARGP_ERR_UNKNOWN;
77 }
78 return 0;
79 }
80
81 static char *
ccze_dump_color_get_attrib(int color)82 ccze_dump_color_get_attrib (int color)
83 {
84 char *str = (char *)ccze_calloc (100, sizeof (char));
85
86 if (color & A_BOLD)
87 strcat (str, "bold ");
88 if (color & A_UNDERLINE)
89 strcat (str, "underline ");
90 if (color & A_REVERSE)
91 strcat (str, "reverse ");
92 if (color & A_BLINK)
93 strcat (str, "blink ");
94
95 return str;
96 }
97
98 static char *
ccze_dump_color_to_name(int color)99 ccze_dump_color_to_name (int color)
100 {
101 int my_color = ccze_color_strip_attrib (color);
102
103 if (my_color < COLOR_PAIR (8))
104 return ccze_color_to_name_simple (my_color);
105 else
106 {
107 int i,j;
108 char *str, *cj, *ci;
109
110 j = (my_color >> 8) % 8;
111 i = (my_color >> 8) / 8;
112 cj = ccze_color_to_name_simple (COLOR_PAIR (j));
113 ci = ccze_color_to_name_simple (COLOR_PAIR (i));
114 asprintf (&str, "%s on_%s", cj, ci);
115 return str;
116 }
117 }
118
119 static char *
ccze_dump_color_comment(int cidx)120 ccze_dump_color_comment (int cidx)
121 {
122 return ccze_color_keyword_map[cidx].comment;
123 }
124
125 static int
ccze_dump_color_hidden(int cidx)126 ccze_dump_color_hidden (int cidx)
127 {
128 return !ccze_color_keyword_map[cidx].settable;
129 }
130
131 static int
ccze_dump_color_to_idx(ccze_color_t color)132 ccze_dump_color_to_idx (ccze_color_t color)
133 {
134 size_t cidx;
135
136 for (cidx = 0; cidx < sizeof (ccze_color_keyword_map); cidx++)
137 if (ccze_color_keyword_map[cidx].idx == color)
138 return cidx;
139 return 0;
140 }
141
142 int
main(int argc,char * argv[])143 main (int argc, char *argv[])
144 {
145 ccze_color_t cidx;
146 char line[256];
147 int color;
148 size_t llen;
149
150 argp_parse (&argp, argc, argv, 0, 0, NULL);
151
152 ccze_color_init ();
153
154 if (ccze_config.rcfile)
155 ccze_color_load (ccze_config.rcfile);
156 else if (ccze_loaddefs)
157 {
158 char *home, *homerc;
159
160 ccze_color_load (SYSCONFDIR "/colorizerc");
161 ccze_color_load (SYSCONFDIR "/cczerc");
162 home = getenv ("HOME");
163 if (home)
164 {
165 asprintf (&homerc, "%s/.colorizerc", home);
166 ccze_color_load (homerc);
167 free (homerc);
168 asprintf (&homerc, "%s/.cczerc", home);
169 ccze_color_load (homerc);
170 free (homerc);
171 }
172 }
173
174 printf ("# Configuration file for ccze\n#\n");
175 printf ("# Available 'pre' attributes: bold, underline, underscore, "
176 "blink, reverse\n");
177 printf ("# Available colors: black, red, green, yellow, blue, magenta, "
178 "cyan, white\n");
179 printf ("# Available bgcolors: on_black, on_red, on_green, on_yellow, "
180 "on_blue, on_magenta, on_cyan, on_white\n#\n");
181 printf ("# You can also use item names in color definition, like:\n#\n");
182 printf ("# default blue\n# date 'default'\n#\n");
183 printf ("# Here you defined default color to blue, and date color to "
184 "default value's color, so\n");
185 printf ("# your date color is blue. (You can only use predefined item "
186 "names!)\n\n");
187 printf ("# item color # comment (what is "
188 "color, or why it's that ;)\n\n");
189
190 /* Dump colors */
191 for (cidx = CCZE_COLOR_DATE; cidx < CCZE_COLOR_LAST; cidx++)
192 {
193 if (ccze_dump_color_hidden (cidx))
194 continue;
195
196 color = ccze_color (cidx);
197
198 strcpy (line, ccze_color_lookup_name (cidx));
199 llen = strlen (line);
200 memset (&line[llen], ' ', 16 - llen);
201 line[16]='\0';
202 strcat (line, ccze_dump_color_get_attrib (color));
203 strcat (line, ccze_dump_color_to_name (color));
204 llen = strlen (line);
205 memset (&line[llen], ' ', 42 - llen);
206 line[40]='#';
207 line[42]='\0';
208 strcat (line, ccze_dump_color_comment (ccze_dump_color_to_idx (cidx)));
209
210 printf ("%s\n", line);
211 }
212
213 /* CSS codes */
214 printf ("\n# CSS codes for the HTML output\n");
215 for (color = 0; color < 8; color++)
216 {
217 strcpy (line, "css");
218 strcat (line, ccze_colorname_map[color].name);
219 llen = strlen (line);
220 memset (&line[llen], ' ', 16 - llen);
221 line[16]='\0';
222 strcat (line, ccze_csscolor_normal_map[color]);
223 printf ("%s\n", line);
224
225 strcpy (line, "cssbold");
226 strcat (line, ccze_colorname_map[color].name);
227 llen = strlen (line);
228 memset (&line[llen], ' ', 16 - llen);
229 line[16]='\0';
230 strcat (line, ccze_csscolor_bold_map[color]);
231 printf ("%s\n", line);
232 }
233
234 strcpy (line, "cssbody");
235 llen = strlen (line);
236 memset (&line[llen], ' ', 16 - llen);
237 line[16]='\0';
238 strcat (line, ccze_cssbody_color ());
239 printf ("%s\n", line);
240
241 return 0;
242 }
243