2 * $Id: rc.c,v 1.53 2018/05/31 20:32:15 tom Exp $
4 * rc.c -- routines for processing the configuration file
6 * Copyright 2000-2012,2018 Thomas E. Dickey
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License, version 2.1
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to
19 * Free Software Foundation, Inc.
20 * 51 Franklin St., Fifth Floor
21 * Boston, MA 02110, USA.
23 * An earlier version of this program lists as authors
24 * Savio Lam (lam836@cs.cuhk.hk)
32 #include <dlg_colors.h>
35 * For matching color names with color values
37 static const color_names_st color_names[] =
39 #ifdef HAVE_USE_DEFAULT_COLORS
42 {"BLACK", COLOR_BLACK},
44 {"GREEN", COLOR_GREEN},
45 {"YELLOW", COLOR_YELLOW},
47 {"MAGENTA", COLOR_MAGENTA},
49 {"WHITE", COLOR_WHITE},
51 #define COLOR_COUNT (sizeof(color_names) / sizeof(color_names[0]))
52 #endif /* HAVE_COLOR */
54 #define GLOBALRC "/etc/dialogrc"
55 #define DIALOGRC ".dialogrc"
62 /* Type of line in configuration file */
69 /* number of configuration variables */
70 #define VAR_COUNT (sizeof(vars) / sizeof(vars_st))
72 /* check if character is string quoting characters */
73 #define isquote(c) ((c) == '"' || (c) == '\'')
75 /* get last character of string */
76 #define lastch(str) str[strlen(str)-1]
79 * Configuration variables
82 const char *name; /* name of configuration variable as in DIALOGRC */
83 void *var; /* address of actual variable to change */
84 int type; /* type of value */
85 const char *comment; /* comment to put in "rc" file */
89 * This table should contain only references to dialog_state, since dialog_vars
90 * is reset specially in dialog.c before each widget.
92 static const vars_st vars[] =
95 &dialog_state.aspect_ratio,
97 "Set aspect-ration."},
100 &dialog_state.separate_str,
102 "Set separator (for multiple widgets output)."},
105 &dialog_state.tab_len,
107 "Set tab-length (for textbox tab-conversion)."},
110 &dialog_state.visit_items,
112 "Make tab-traversal for checklist, etc., include the list."},
116 &dialog_state.use_shadow,
118 "Shadow dialog boxes? This also turns on color."},
121 &dialog_state.use_colors,
123 "Turn color support ON or OFF"},
124 #endif /* HAVE_COLOR */
128 skip_whitespace(char *str, int n)
130 while (isblank(UCH(str[n])) && str[n] != '\0')
136 skip_keyword(char *str, int n)
138 while (isalnum(UCH(str[n])) && str[n] != '\0')
144 find_vars(char *name)
149 for (i = 0; i < VAR_COUNT; i++) {
150 if (dlg_strcmp(vars[i].name, name) == 0) {
160 find_color(char *name)
164 int limit = dlg_color_count();
166 for (i = 0; i < limit; i++) {
167 if (dlg_strcmp(dlg_color_table[i].name, name) == 0) {
176 * Convert an attribute to a string representation like this:
178 * "(foreground,background,highlight)"
181 attr_to_str(char *str, int fg, int bg, int hl)
187 for (i = 0; fg != color_names[i].value; i++) ;
188 strcat(str, color_names[i].name);
192 for (i = 0; bg != color_names[i].value; i++) ;
193 strcat(str, color_names[i].name);
196 strcat(str, hl ? ",ON)" : ",OFF)");
202 * Extract the foreground, background and highlight values from an attribute
203 * represented as a string in one of two forms:
205 * "(foreground,background,highlight)"
209 str_to_attr(char *str, int *fg, int *bg, int *hl)
211 int i = 0, get_fg = 1;
213 char tempstr[MAX_LEN + 1], *part;
216 if (str[0] != '(' || lastch(str) != ')') {
217 if ((i = find_color(str)) >= 0) {
218 *fg = dlg_color_table[i].fg;
219 *bg = dlg_color_table[i].bg;
220 *hl = dlg_color_table[i].hilite;
223 return -1; /* invalid representation */
226 /* remove the parenthesis */
228 if (have > MAX_LEN) {
233 memcpy(tempstr, str + 1, have);
234 tempstr[have] = '\0';
236 /* get foreground and background */
239 /* skip white space before fg/bg string */
240 i = skip_whitespace(tempstr, i);
241 if (tempstr[i] == '\0')
242 return -1; /* invalid representation */
243 part = tempstr + i; /* set 'part' to start of fg/bg string */
245 /* find end of fg/bg string */
246 while (!isblank(UCH(tempstr[i])) && tempstr[i] != ','
247 && tempstr[i] != '\0')
250 if (tempstr[i] == '\0')
251 return -1; /* invalid representation */
252 else if (isblank(UCH(tempstr[i]))) { /* not yet ',' */
255 /* skip white space before ',' */
256 i = skip_whitespace(tempstr, i);
257 if (tempstr[i] != ',')
258 return -1; /* invalid representation */
260 tempstr[i++] = '\0'; /* skip the ',' */
261 for (j = 0; j < COLOR_COUNT && dlg_strcmp(part, color_names[j].name);
263 if (j == COLOR_COUNT) /* invalid color name */
266 *fg = color_names[j].value;
267 get_fg = 0; /* next we have to get the background */
269 *bg = color_names[j].value;
272 } /* got foreground and background */
276 /* skip white space before highlight string */
277 i = skip_whitespace(tempstr, i);
278 if (tempstr[i] == '\0')
279 return -1; /* invalid representation */
280 part = tempstr + i; /* set 'part' to start of highlight string */
282 /* trim trailing white space from highlight string */
283 i = (int) strlen(part) - 1;
284 while (isblank(UCH(part[i])) && i > 0)
288 if (!dlg_strcmp(part, "ON"))
290 else if (!dlg_strcmp(part, "OFF"))
293 return -1; /* invalid highlight value */
297 #endif /* HAVE_COLOR */
300 * Check if the line begins with a special keyword; if so, return true while
301 * pointing params to its parameters.
304 begins_with(char *line, const char *keyword, char **params)
306 int i = skip_whitespace(line, 0);
307 int j = skip_keyword(line, i);
309 if ((j - i) == (int) strlen(keyword)) {
312 if (!dlg_strcmp(keyword, line + i)) {
313 *params = line + skip_whitespace(line, j + 1);
323 * Parse a line in the configuration file
325 * Each line is of the form: "variable = value". On exit, 'var' will contain
326 * the variable name, and 'value' will contain the value string.
330 * LINE_EMPTY - line is blank or comment
331 * LINE_EQUALS - line contains "variable = value"
332 * LINE_ERROR - syntax error in line
335 parse_line(char *line, char **var, char **value)
339 /* ignore white space at beginning of line */
340 i = skip_whitespace(line, i);
342 if (line[i] == '\0') /* line is blank */
344 else if (line[i] == '#') /* line is comment */
346 else if (line[i] == '=') /* variable names cannot start with a '=' */
349 /* set 'var' to variable name */
350 *var = line + i++; /* skip to next character */
352 /* find end of variable name */
353 while (!isblank(UCH(line[i])) && line[i] != '=' && line[i] != '\0')
356 if (line[i] == '\0') /* syntax error */
358 else if (line[i] == '=')
363 /* skip white space before '=' */
364 i = skip_whitespace(line, i);
366 if (line[i] != '=') /* syntax error */
369 i++; /* skip the '=' */
372 /* skip white space after '=' */
373 i = skip_whitespace(line, i);
378 *value = line + i; /* set 'value' to value string */
380 /* trim trailing white space from 'value' */
381 i = (int) strlen(*value) - 1;
382 while (isblank(UCH((*value)[i])) && i > 0)
384 (*value)[i + 1] = '\0';
386 return LINE_EQUALS; /* no syntax error in line */
390 * Create the configuration file
393 dlg_create_rc(const char *filename)
398 if ((rc_file = fopen(filename, "wt")) == NULL)
399 dlg_exiterr("Error opening file for writing in dlg_create_rc().");
401 fprintf(rc_file, "#\n\
402 # Run-time configuration file for dialog\n\
404 # Automatically generated by \"dialog --create-rc <file>\"\n\
407 # Types of values:\n\
409 # Number - <number>\n\
410 # String - \"string\"\n\
411 # Boolean - <ON|OFF>\n"
414 # Attribute - (foreground,background,highlight?)\n"
418 /* Print an entry for each configuration variable */
419 for (i = 0; i < VAR_COUNT; i++) {
420 fprintf(rc_file, "\n# %s\n", vars[i].comment);
421 switch (vars[i].type) {
423 fprintf(rc_file, "%s = %d\n", vars[i].name,
424 *((int *) vars[i].var));
427 fprintf(rc_file, "%s = \"%s\"\n", vars[i].name,
428 (char *) vars[i].var);
431 fprintf(rc_file, "%s = %s\n", vars[i].name,
432 *((bool *) vars[i].var) ? "ON" : "OFF");
437 for (i = 0; i < (unsigned) dlg_color_count(); ++i) {
438 char buffer[MAX_LEN + 1];
442 fprintf(rc_file, "\n# %s\n", dlg_color_table[i].comment);
443 for (j = 0; j != i; ++j) {
444 if (dlg_color_table[i].fg == dlg_color_table[j].fg
445 && dlg_color_table[i].bg == dlg_color_table[j].bg
446 && dlg_color_table[i].hilite == dlg_color_table[j].hilite) {
447 fprintf(rc_file, "%s = %s\n",
448 dlg_color_table[i].name,
449 dlg_color_table[j].name);
456 fprintf(rc_file, "%s = %s\n", dlg_color_table[i].name,
458 dlg_color_table[i].fg,
459 dlg_color_table[i].bg,
460 dlg_color_table[i].hilite));
463 #endif /* HAVE_COLOR */
464 dlg_dump_keys(rc_file);
466 (void) fclose(rc_file);
470 * Parse the configuration file and set up variables
478 char str[MAX_LEN + 1];
487 * At startup, dialog determines the settings to use as follows:
489 * a) if the environment variable $DIALOGRC is set, its value determines
490 * the name of the configuration file.
492 * b) if the file in (a) can't be found, use the file $HOME/.dialogrc
493 * as the configuration file.
495 * c) if the file in (b) can't be found, try using the GLOBALRC file.
496 * Usually this will be /etc/dialogrc.
498 * d) if the file in (c) cannot be found, use the compiled-in defaults.
502 if ((tempptr = getenv("DIALOGRC")) != NULL)
503 rc_file = fopen(tempptr, "rt");
505 if (rc_file == NULL) { /* step (a) failed? */
507 if ((tempptr = getenv("HOME")) != NULL
508 && strlen(tempptr) < MAX_LEN - (sizeof(DIALOGRC) + 3)) {
509 if (tempptr[0] == '\0' || lastch(tempptr) == '/')
510 sprintf(str, "%s%s", tempptr, DIALOGRC);
512 sprintf(str, "%s/%s", tempptr, DIALOGRC);
513 rc_file = fopen(tempptr = str, "rt");
517 if (rc_file == NULL) { /* step (b) failed? */
519 strcpy(str, GLOBALRC);
520 if ((rc_file = fopen(tempptr = str, "rt")) == NULL)
521 return 0; /* step (c) failed, use default values */
524 DLG_TRACE(("# opened rc file \"%s\"\n", tempptr));
525 /* Scan each line and set variables */
526 while ((result == 0) && (fgets(str, MAX_LEN, rc_file) != NULL)) {
527 DLG_TRACE(("#\t%s", str));
528 if (*str == '\0' || lastch(str) != '\n') {
529 /* ignore rest of file if line too long */
530 fprintf(stderr, "\nParse error: line %d of configuration"
531 " file too long.\n", l);
532 result = -1; /* parse aborted */
537 if (begins_with(str, "bindkey", ¶ms)) {
538 if (!dlg_parse_bindkey(params)) {
539 fprintf(stderr, "\nParse error: line %d of configuration\n", l);
544 parse = parse_line(str, &var, &value); /* parse current line */
547 case LINE_EMPTY: /* ignore blank lines and comments */
550 /* search table for matching config variable name */
551 if ((i = find_vars(var)) >= 0) {
552 switch (vars[i].type) {
554 *((int *) vars[i].var) = atoi(value);
557 if (!isquote(value[0]) || !isquote(lastch(value))
558 || strlen(value) < 2) {
559 fprintf(stderr, "\nParse error: string value "
560 "expected at line %d of configuration "
562 result = -1; /* parse aborted */
564 /* remove the (") quotes */
566 lastch(value) = '\0';
567 strcpy((char *) vars[i].var, value);
571 if (!dlg_strcmp(value, "ON"))
572 *((bool *) vars[i].var) = TRUE;
573 else if (!dlg_strcmp(value, "OFF"))
574 *((bool *) vars[i].var) = FALSE;
576 fprintf(stderr, "\nParse error: boolean value "
577 "expected at line %d of configuration "
578 "file (found %s).\n", l, value);
579 result = -1; /* parse aborted */
584 } else if ((i = find_color(var)) >= 0) {
588 if (str_to_attr(value, &fg, &bg, &hl) == -1) {
589 fprintf(stderr, "\nParse error: attribute "
590 "value expected at line %d of configuration "
592 result = -1; /* parse aborted */
594 dlg_color_table[i].fg = fg;
595 dlg_color_table[i].bg = bg;
596 dlg_color_table[i].hilite = hl;
599 #endif /* HAVE_COLOR */
600 fprintf(stderr, "\nParse error: unknown variable "
601 "at line %d of configuration file:\n\t%s\n", l, var);
602 result = -1; /* parse aborted */
606 fprintf(stderr, "\nParse error: syntax error at line %d of "
607 "configuration file.\n", l);
608 result = -1; /* parse aborted */
614 (void) fclose(rc_file);