xref: /freebsd/lib/libdpv/dialogrc.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
1 /*-
2  * Copyright (c) 2013-2015 Devin Teske <dteske@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/types.h>
28 
29 #include <err.h>
30 #include <errno.h>
31 #include <figpar.h>
32 #include <limits.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <string_m.h>
37 
38 #include "dialogrc.h"
39 
40 #define STR_BUFSIZE 255
41 
42 /* dialog(1) `.dialogrc' characteristics */
43 uint8_t use_colors = 1;
44 uint8_t use_shadow = 1;
45 char gauge_color[STR_BUFSIZE]	= "47b"; /* (BLUE,WHITE,ON) */
46 char separator[STR_BUFSIZE]	= "";
47 
48 /* Function prototypes */
49 static int setattr(struct figpar_config *, uint32_t, char *, char *);
50 static int setbool(struct figpar_config *, uint32_t, char *, char *);
51 static int setnum(struct figpar_config *, uint32_t, char *, char *);
52 static int setstr(struct figpar_config *, uint32_t, char *, char *);
53 
54 /*
55  * Anatomy of DIALOGRC (~/.dialogrc by default)
56  * NOTE: Must appear after private function prototypes (above)
57  * NB: Brace-initialization of union requires cast to *first* member of union
58  */
59 static struct figpar_config dialogrc_config[] = {
60     /* TYPE            DIRECTIVE                      DEFAULT        HANDLER */
61     {FIGPAR_TYPE_INT,  "aspect",                      {(void *)0},   &setnum},
62     {FIGPAR_TYPE_STR,  "separate_widget",             {separator},   &setstr},
63     {FIGPAR_TYPE_INT,  "tab_len",                     {(void *)0},   &setnum},
64     {FIGPAR_TYPE_BOOL, "visit_items",                 {(void *)0},   &setbool},
65     {FIGPAR_TYPE_BOOL, "use_shadow",                  {(void *)1},   &setbool},
66     {FIGPAR_TYPE_BOOL, "use_colors",                  {(void *)1},   &setbool},
67     {FIGPAR_TYPE_STR,  "screen_color",                {NULL},        &setattr},
68     {FIGPAR_TYPE_STR,  "shadow_color",                {NULL},        &setattr},
69     {FIGPAR_TYPE_STR,  "dialog_color",                {NULL},        &setattr},
70     {FIGPAR_TYPE_STR,  "title_color",                 {NULL},        &setattr},
71     {FIGPAR_TYPE_STR,  "border_color",                {NULL},        &setattr},
72     {FIGPAR_TYPE_STR,  "button_active_color",         {NULL},        &setattr},
73     {FIGPAR_TYPE_STR,  "button_inactive_color",       {NULL},        &setattr},
74     {FIGPAR_TYPE_STR,  "button_key_active_color",     {NULL},        &setattr},
75     {FIGPAR_TYPE_STR,  "button_key_inactive_color",   {NULL},        &setattr},
76     {FIGPAR_TYPE_STR,  "button_label_active_color",   {NULL},        &setattr},
77     {FIGPAR_TYPE_STR,  "button_label_inactive_color", {NULL},        &setattr},
78     {FIGPAR_TYPE_STR,  "inputbox_color",              {NULL},        &setattr},
79     {FIGPAR_TYPE_STR,  "inputbox_border_color",       {NULL},        &setattr},
80     {FIGPAR_TYPE_STR,  "searchbox_color",             {NULL},        &setattr},
81     {FIGPAR_TYPE_STR,  "searchbox_title_color",       {NULL},        &setattr},
82     {FIGPAR_TYPE_STR,  "searchbox_border_color",      {NULL},        &setattr},
83     {FIGPAR_TYPE_STR,  "position_indicator_color",    {NULL},        &setattr},
84     {FIGPAR_TYPE_STR,  "menubox_color",               {NULL},        &setattr},
85     {FIGPAR_TYPE_STR,  "menubox_border_color",        {NULL},        &setattr},
86     {FIGPAR_TYPE_STR,  "item_color",                  {NULL},        &setattr},
87     {FIGPAR_TYPE_STR,  "item_selected_color",         {NULL},        &setattr},
88     {FIGPAR_TYPE_STR,  "tag_color",                   {NULL},        &setattr},
89     {FIGPAR_TYPE_STR,  "tag_selected_color",          {NULL},        &setattr},
90     {FIGPAR_TYPE_STR,  "tag_key_color",               {NULL},        &setattr},
91     {FIGPAR_TYPE_STR,  "tag_key_selected_color",      {NULL},        &setattr},
92     {FIGPAR_TYPE_STR,  "check_color",                 {NULL},        &setattr},
93     {FIGPAR_TYPE_STR,  "check_selected_color",        {NULL},        &setattr},
94     {FIGPAR_TYPE_STR,  "uarrow_color",                {NULL},        &setattr},
95     {FIGPAR_TYPE_STR,  "darrow_color",                {NULL},        &setattr},
96     {FIGPAR_TYPE_STR,  "itemhelp_color",              {NULL},        &setattr},
97     {FIGPAR_TYPE_STR,  "form_active_text_color",      {NULL},        &setattr},
98     {FIGPAR_TYPE_STR,  "form_text_color",             {NULL},        &setattr},
99     {FIGPAR_TYPE_STR,  "form_item_readonly_color",    {NULL},        &setattr},
100     {FIGPAR_TYPE_STR,  "gauge_color",                 {gauge_color}, &setattr},
101     {0, NULL, {0}, NULL}
102 };
103 
104 /*
105  * figpar call-back for interpreting value as .dialogrc `Attribute'
106  */
107 static int
108 setattr(struct figpar_config *option, uint32_t line __unused,
109     char *directive __unused, char *value)
110 {
111 	char *cp = value;
112 	char *val;
113 	size_t len;
114 	char attrbuf[4];
115 
116 	if (option == NULL) {
117 		warnx("%s:%d:%s: Missing callback parameter", __FILE__,
118 		    __LINE__, __func__);
119 		return (-1); /* Abort processing */
120 	}
121 
122 	/* Allocate memory for the data if not already done */
123 	if (option->value.str == NULL) {
124 		if ((option->value.str = malloc(STR_BUFSIZE)) == NULL)
125 			return (-1);
126 	}
127 
128 	/*
129 	 * If the first character is left-parenthesis, the format is
130 	 * `(background,foreground,highlight)' otherwise, we should take it
131 	 * as a reference to another color.
132 	 */
133 	if (*cp != '(') {
134 		/* Copy the [current] value from the referenced color */
135 		val = dialogrc_config_option(cp)->value.str;
136 		if (val != NULL)
137 			snprintf(option->value.str, STR_BUFSIZE, "%s", val);
138 
139 		return (0);
140 	} else
141 		cp++;
142 
143 	strtolower(cp);
144 
145 	/* Initialize the attrbuf (fg,bg,hi,NUL) */
146 	attrbuf[0] = '0';
147 	attrbuf[1] = '0';
148 	attrbuf[2] = 'B'; /* \ZB = disable; \Zb = enable (see dialog(1)) */
149 	attrbuf[3] = '\0';
150 
151 	/* Interpret the foreground color */
152 	if      (strncmp(cp, "red,",     4) == 0) attrbuf[0] = '1';
153 	else if (strncmp(cp, "green,",   6) == 0) attrbuf[0] = '2';
154 	else if (strncmp(cp, "yellow,",  7) == 0) attrbuf[0] = '3';
155 	else if (strncmp(cp, "blue,",    5) == 0) attrbuf[0] = '4';
156 	else if (strncmp(cp, "magenta,", 8) == 0) attrbuf[0] = '5';
157 	else if (strncmp(cp, "cyan,",    5) == 0) attrbuf[0] = '6';
158 	else if (strncmp(cp, "white,",   6) == 0) attrbuf[0] = '7';
159 	else if (strncmp(cp, "black,",   6) == 0) attrbuf[0] = '8';
160 
161 	/* Advance to the background color */
162 	cp = strchr(cp, ',');
163 	if (cp == NULL)
164 		goto write_attrbuf;
165 	else
166 		cp++;
167 
168 	/* Interpret the background color */
169 	if      (strncmp(cp, "red,",     4) == 0) attrbuf[1] = '1';
170 	else if (strncmp(cp, "green,",   6) == 0) attrbuf[1] = '2';
171 	else if (strncmp(cp, "yellow,",  7) == 0) attrbuf[1] = '3';
172 	else if (strncmp(cp, "blue,",    5) == 0) attrbuf[1] = '4';
173 	else if (strncmp(cp, "magenta,", 8) == 0) attrbuf[1] = '5';
174 	else if (strncmp(cp, "cyan,",    5) == 0) attrbuf[1] = '6';
175 	else if (strncmp(cp, "white,",   6) == 0) attrbuf[1] = '7';
176 	else if (strncmp(cp, "black,",   6) == 0) attrbuf[1] = '8';
177 
178 	/* Advance to the highlight */
179 	cp = strchr(cp, ',');
180 	if (cp == NULL)
181 		goto write_attrbuf;
182 	else
183 		cp++;
184 
185 	/* Trim trailing parenthesis */
186 	len = strlen(cp);
187 	if (cp[len - 1] == ')')
188 		cp[len - 1] = '\0';
189 
190 	/* Interpret the highlight (initialized to off above) */
191 	if (strcmp(cp, "on") == 0 || strncmp(cp, "on,", 3) == 0)
192 		attrbuf[2] = 'b'; /* \Zb = enable bold (see dialog(1)) */
193 
194 write_attrbuf:
195 	sprintf(option->value.str, "%s", attrbuf);
196 
197 	return (0);
198 }
199 
200 /*
201  * figpar call-back for interpreting value as .dialogrc `Boolean'
202  */
203 static int
204 setbool(struct figpar_config *option, uint32_t line __unused,
205     char *directive __unused, char *value)
206 {
207 
208 	if (option == NULL) {
209 		warnx("%s:%d:%s: Missing callback parameter", __FILE__,
210 		    __LINE__, __func__);
211 		return (-1); /* Abort processing */
212 	}
213 
214 	/* Assume ON, check for OFF (case-insensitive) */
215 	option->value.boolean = 1;
216 	strtolower(value);
217 	if (strcmp(value, "off") == 0)
218 		option->value.boolean = 0;
219 
220 	return (0);
221 }
222 
223 /*
224  * figpar call-back for interpreting value as .dialogrc `Number'
225  */
226 static int
227 setnum(struct figpar_config *option, uint32_t line __unused,
228     char *directive __unused, char *value)
229 {
230 
231 	if (option == NULL) {
232 		warnx("%s:%d:%s: Missing callback parameter", __FILE__,
233 		    __LINE__, __func__);
234 		return (-1); /* Abort processing */
235 	}
236 
237 	/* Convert the string to a 32-bit signed integer */
238 	option->value.num = (int32_t)strtol(value, (char **)NULL, 10);
239 
240 	return (0);
241 }
242 
243 /*
244  * figpar call-back for interpreting value as .dialogrc `String'
245  */
246 static int
247 setstr(struct figpar_config *option, uint32_t line __unused,
248     char *directive __unused, char *value)
249 {
250 	size_t len;
251 
252 	if (option == NULL) {
253 		warnx("%s:%d:%s: Missing callback parameter", __FILE__,
254 		    __LINE__, __func__);
255 		return (-1); /* Abort processing */
256 	}
257 
258 	/* Allocate memory for the data if not already done */
259 	if (option->value.str == NULL) {
260 		if ((option->value.str = malloc(STR_BUFSIZE)) == NULL)
261 			return (-1);
262 	}
263 
264 	/* Trim leading quote */
265 	if (*value == '"')
266 		value++;
267 
268 	/* Write the data into the buffer */
269 	snprintf(option->value.str, STR_BUFSIZE, "%s", value);
270 
271 	/* Trim trailing quote */
272 	len = strlen(option->value.str);
273 	if (option->value.str[len - 1] == '"')
274 		option->value.str[len - 1] = '\0';
275 
276 	return (0);
277 }
278 
279 /*
280  * Parse (in order of preference) $DIALOGRC or `$HOME/.dialogrc'. Returns zero
281  * on success, -1 on failure (and errno should be consulted).
282  */
283 int
284 parse_dialogrc(void)
285 {
286 	char *cp;
287 	int res;
288 	size_t len;
289 	char path[PATH_MAX];
290 
291 	/* Allow $DIALOGRC to override `$HOME/.dialogrc' default */
292 	if ((cp = getenv(ENV_DIALOGRC)) != NULL && *cp != '\0')
293 		snprintf(path, PATH_MAX, "%s", cp);
294 	else if ((cp = getenv(ENV_HOME)) != NULL) {
295 		/* Copy $HOME into buffer and append trailing `/' if missing */
296 		snprintf(path, PATH_MAX, "%s", cp);
297 		len = strlen(path);
298 		cp = path + len;
299 		if (len > 0 && len < (PATH_MAX - 1) && *(cp - 1) != '/') {
300 			*cp++ = '/';
301 			*cp = '\0';
302 			len++;
303 		}
304 
305 		/* If we still have room, shove in the name of rc file */
306 		if (len < (PATH_MAX - 1))
307 			snprintf(cp, PATH_MAX - len, "%s", DIALOGRC);
308 	} else {
309 		/* Like dialog(1), don't process a file if $HOME is unset */
310 		errno = ENOENT;
311 		return (-1);
312 	}
313 
314 	/* Process file (either $DIALOGRC if set, or `$HOME/.dialogrc') */
315 	res = parse_config(dialogrc_config,
316 		path, NULL, FIGPAR_BREAK_ON_EQUALS);
317 
318 	/* Set some globals based on what we parsed */
319 	use_shadow = dialogrc_config_option("use_shadow")->value.boolean;
320 	use_colors = dialogrc_config_option("use_colors")->value.boolean;
321 	snprintf(gauge_color, STR_BUFSIZE, "%s",
322 	    dialogrc_config_option("gauge_color")->value.str);
323 
324 	return (res);
325 }
326 
327 /*
328  * Return a pointer to the `.dialogrc' config option specific to `directive' or
329  * static figpar_dummy_config (full of NULLs) if none found (see
330  * get_config_option(3); part of figpar(3)).
331  */
332 struct figpar_config *
333 dialogrc_config_option(const char *directive)
334 {
335 	return (get_config_option(dialogrc_config, directive));
336 }
337 
338 /*
339  * Free allocated items initialized by setattr() (via parse_config() callback
340  * matrix [dialogrc_config] used in parse_dialogrc() above).
341  */
342 void
343 dialogrc_free(void)
344 {
345 	char *value;
346 	uint32_t n;
347 
348 	for (n = 0; dialogrc_config[n].directive != NULL; n++) {
349 		if (dialogrc_config[n].action != &setattr)
350 			continue;
351 		value = dialogrc_config[n].value.str;
352 		if (value != NULL && value != gauge_color) {
353 			free(dialogrc_config[n].value.str);
354 			dialogrc_config[n].value.str = NULL;
355 		}
356 	}
357 }
358