2 * $Id: rc.c,v 1.49 2011/10/15 00:56:44 tom Exp $
4 * rc.c -- routines for processing the configuration file
6 * Copyright 2000-2010,2011 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 white space */
73 #define whitespace(c) (c == ' ' || c == TAB)
75 /* check if character is string quoting characters */
76 #define isquote(c) (c == '"' || c == '\'')
78 /* get last character of string */
79 #define lastch(str) str[strlen(str)-1]
82 * Configuration variables
85 const char *name; /* name of configuration variable as in DIALOGRC */
86 void *var; /* address of actual variable to change */
87 int type; /* type of value */
88 const char *comment; /* comment to put in "rc" file */
92 * This table should contain only references to dialog_state, since dialog_vars
93 * is reset specially in dialog.c before each widget.
95 static const vars_st vars[] =
98 &dialog_state.aspect_ratio,
100 "Set aspect-ration."},
103 &dialog_state.separate_str,
105 "Set separator (for multiple widgets output)."},
108 &dialog_state.tab_len,
110 "Set tab-length (for textbox tab-conversion)."},
113 &dialog_state.visit_items,
115 "Make tab-traversal for checklist, etc., include the list."},
119 &dialog_state.use_shadow,
121 "Shadow dialog boxes? This also turns on color."},
124 &dialog_state.use_colors,
126 "Turn color support ON or OFF"},
127 #endif /* HAVE_COLOR */
131 skip_whitespace(char *str, int n)
133 while (whitespace(str[n]) && str[n] != '\0')
139 skip_keyword(char *str, int n)
141 while (isalnum(UCH(str[n])) && str[n] != '\0')
147 find_vars(char *name)
152 for (i = 0; i < VAR_COUNT; i++) {
153 if (dlg_strcmp(vars[i].name, name) == 0) {
163 find_color(char *name)
167 int limit = dlg_color_count();
169 for (i = 0; i < limit; i++) {
170 if (dlg_strcmp(dlg_color_table[i].name, name) == 0) {
179 * Convert an attribute to a string representation like this:
181 * "(foreground,background,highlight)"
184 attr_to_str(char *str, int fg, int bg, int hl)
190 for (i = 0; fg != color_names[i].value; i++) ;
191 strcat(str, color_names[i].name);
195 for (i = 0; bg != color_names[i].value; i++) ;
196 strcat(str, color_names[i].name);
199 strcat(str, hl ? ",ON)" : ",OFF)");
205 * Extract the foreground, background and highlight values from an attribute
206 * represented as a string in one of two forms:
208 * "(foreground,background,highlight)"
212 str_to_attr(char *str, int *fg, int *bg, int *hl)
214 int i = 0, get_fg = 1;
216 char tempstr[MAX_LEN + 1], *part;
218 if (str[0] != '(' || lastch(str) != ')') {
219 if ((i = find_color(str)) >= 0) {
220 *fg = dlg_color_table[i].fg;
221 *bg = dlg_color_table[i].bg;
222 *hl = dlg_color_table[i].hilite;
225 return -1; /* invalid representation */
228 /* remove the parenthesis */
229 strcpy(tempstr, str + 1);
230 lastch(tempstr) = '\0';
232 /* get foreground and background */
235 /* skip white space before fg/bg string */
236 i = skip_whitespace(tempstr, i);
237 if (tempstr[i] == '\0')
238 return -1; /* invalid representation */
239 part = tempstr + i; /* set 'part' to start of fg/bg string */
241 /* find end of fg/bg string */
242 while (!whitespace(tempstr[i]) && tempstr[i] != ','
243 && tempstr[i] != '\0')
246 if (tempstr[i] == '\0')
247 return -1; /* invalid representation */
248 else if (whitespace(tempstr[i])) { /* not yet ',' */
251 /* skip white space before ',' */
252 i = skip_whitespace(tempstr, i);
253 if (tempstr[i] != ',')
254 return -1; /* invalid representation */
256 tempstr[i++] = '\0'; /* skip the ',' */
257 for (j = 0; j < COLOR_COUNT && dlg_strcmp(part, color_names[j].name);
259 if (j == COLOR_COUNT) /* invalid color name */
262 *fg = color_names[j].value;
263 get_fg = 0; /* next we have to get the background */
265 *bg = color_names[j].value;
268 } /* got foreground and background */
272 /* skip white space before highlight string */
273 i = skip_whitespace(tempstr, i);
274 if (tempstr[i] == '\0')
275 return -1; /* invalid representation */
276 part = tempstr + i; /* set 'part' to start of highlight string */
278 /* trim trailing white space from highlight string */
279 i = (int) strlen(part) - 1;
280 while (whitespace(part[i]) && i > 0)
284 if (!dlg_strcmp(part, "ON"))
286 else if (!dlg_strcmp(part, "OFF"))
289 return -1; /* invalid highlight value */
293 #endif /* HAVE_COLOR */
296 * Check if the line begins with a special keyword; if so, return true while
297 * pointing params to its parameters.
300 begins_with(char *line, const char *keyword, char **params)
302 int i = skip_whitespace(line, 0);
303 int j = skip_keyword(line, i);
305 if ((j - i) == (int) strlen(keyword)) {
308 if (!dlg_strcmp(keyword, line + i)) {
309 *params = line + skip_whitespace(line, j + 1);
319 * Parse a line in the configuration file
321 * Each line is of the form: "variable = value". On exit, 'var' will contain
322 * the variable name, and 'value' will contain the value string.
326 * LINE_EMPTY - line is blank or comment
327 * LINE_EQUALS - line contains "variable = value"
328 * LINE_ERROR - syntax error in line
331 parse_line(char *line, char **var, char **value)
335 /* ignore white space at beginning of line */
336 i = skip_whitespace(line, i);
338 if (line[i] == '\0') /* line is blank */
340 else if (line[i] == '#') /* line is comment */
342 else if (line[i] == '=') /* variable names cannot start with a '=' */
345 /* set 'var' to variable name */
346 *var = line + i++; /* skip to next character */
348 /* find end of variable name */
349 while (!whitespace(line[i]) && line[i] != '=' && line[i] != '\0')
352 if (line[i] == '\0') /* syntax error */
354 else if (line[i] == '=')
359 /* skip white space before '=' */
360 i = skip_whitespace(line, i);
362 if (line[i] != '=') /* syntax error */
365 i++; /* skip the '=' */
368 /* skip white space after '=' */
369 i = skip_whitespace(line, i);
374 *value = line + i; /* set 'value' to value string */
376 /* trim trailing white space from 'value' */
377 i = (int) strlen(*value) - 1;
378 while (whitespace((*value)[i]) && i > 0)
380 (*value)[i + 1] = '\0';
382 return LINE_EQUALS; /* no syntax error in line */
386 * Create the configuration file
389 dlg_create_rc(const char *filename)
394 if ((rc_file = fopen(filename, "wt")) == NULL)
395 dlg_exiterr("Error opening file for writing in dlg_create_rc().");
397 fprintf(rc_file, "#\n\
398 # Run-time configuration file for dialog\n\
400 # Automatically generated by \"dialog --create-rc <file>\"\n\
403 # Types of values:\n\
405 # Number - <number>\n\
406 # String - \"string\"\n\
407 # Boolean - <ON|OFF>\n"
410 # Attribute - (foreground,background,highlight?)\n"
414 /* Print an entry for each configuration variable */
415 for (i = 0; i < VAR_COUNT; i++) {
416 fprintf(rc_file, "\n# %s\n", vars[i].comment);
417 switch (vars[i].type) {
419 fprintf(rc_file, "%s = %d\n", vars[i].name,
420 *((int *) vars[i].var));
423 fprintf(rc_file, "%s = \"%s\"\n", vars[i].name,
424 (char *) vars[i].var);
427 fprintf(rc_file, "%s = %s\n", vars[i].name,
428 *((bool *) vars[i].var) ? "ON" : "OFF");
433 for (i = 0; i < (unsigned) dlg_color_count(); ++i) {
434 char buffer[MAX_LEN + 1];
438 fprintf(rc_file, "\n# %s\n", dlg_color_table[i].comment);
439 for (j = 0; j != i; ++j) {
440 if (dlg_color_table[i].fg == dlg_color_table[j].fg
441 && dlg_color_table[i].bg == dlg_color_table[j].bg
442 && dlg_color_table[i].hilite == dlg_color_table[j].hilite) {
443 fprintf(rc_file, "%s = %s\n",
444 dlg_color_table[i].name,
445 dlg_color_table[j].name);
452 fprintf(rc_file, "%s = %s\n", dlg_color_table[i].name,
454 dlg_color_table[i].fg,
455 dlg_color_table[i].bg,
456 dlg_color_table[i].hilite));
459 #endif /* HAVE_COLOR */
460 dlg_dump_keys(rc_file);
462 (void) fclose(rc_file);
466 * Parse the configuration file and set up variables
474 char str[MAX_LEN + 1];
483 * At startup, dialog determines the settings to use as follows:
485 * a) if the environment variable $DIALOGRC is set, its value determines
486 * the name of the configuration file.
488 * b) if the file in (a) can't be found, use the file $HOME/.dialogrc
489 * as the configuration file.
491 * c) if the file in (b) can't be found, try using the GLOBALRC file.
492 * Usually this will be /etc/dialogrc.
494 * d) if the file in (c) cannot be found, use the compiled-in defaults.
498 if ((tempptr = getenv("DIALOGRC")) != NULL)
499 rc_file = fopen(tempptr, "rt");
501 if (rc_file == NULL) { /* step (a) failed? */
503 if ((tempptr = getenv("HOME")) != NULL
504 && strlen(tempptr) < MAX_LEN - (sizeof(DIALOGRC) + 3)) {
505 if (tempptr[0] == '\0' || lastch(tempptr) == '/')
506 sprintf(str, "%s%s", tempptr, DIALOGRC);
508 sprintf(str, "%s/%s", tempptr, DIALOGRC);
509 rc_file = fopen(tempptr = str, "rt");
513 if (rc_file == NULL) { /* step (b) failed? */
515 strcpy(str, GLOBALRC);
516 if ((rc_file = fopen(tempptr = str, "rt")) == NULL)
517 return 0; /* step (c) failed, use default values */
520 DLG_TRACE(("opened rc file \"%s\"\n", tempptr));
521 /* Scan each line and set variables */
522 while ((result == 0) && (fgets(str, MAX_LEN, rc_file) != NULL)) {
523 DLG_TRACE(("rc:%s", str));
524 if (*str == '\0' || lastch(str) != '\n') {
525 /* ignore rest of file if line too long */
526 fprintf(stderr, "\nParse error: line %d of configuration"
527 " file too long.\n", l);
528 result = -1; /* parse aborted */
533 if (begins_with(str, "bindkey", ¶ms)) {
534 if (!dlg_parse_bindkey(params)) {
535 fprintf(stderr, "\nParse error: line %d of configuration\n", l);
540 parse = parse_line(str, &var, &value); /* parse current line */
543 case LINE_EMPTY: /* ignore blank lines and comments */
546 /* search table for matching config variable name */
547 if ((i = find_vars(var)) >= 0) {
548 switch (vars[i].type) {
550 *((int *) vars[i].var) = atoi(value);
553 if (!isquote(value[0]) || !isquote(lastch(value))
554 || strlen(value) < 2) {
555 fprintf(stderr, "\nParse error: string value "
556 "expected at line %d of configuration "
558 result = -1; /* parse aborted */
560 /* remove the (") quotes */
562 lastch(value) = '\0';
563 strcpy((char *) vars[i].var, value);
567 if (!dlg_strcmp(value, "ON"))
568 *((bool *) vars[i].var) = TRUE;
569 else if (!dlg_strcmp(value, "OFF"))
570 *((bool *) vars[i].var) = FALSE;
572 fprintf(stderr, "\nParse error: boolean value "
573 "expected at line %d of configuration "
574 "file (found %s).\n", l, value);
575 result = -1; /* parse aborted */
580 } else if ((i = find_color(var)) >= 0) {
584 if (str_to_attr(value, &fg, &bg, &hl) == -1) {
585 fprintf(stderr, "\nParse error: attribute "
586 "value expected at line %d of configuration "
588 result = -1; /* parse aborted */
590 dlg_color_table[i].fg = fg;
591 dlg_color_table[i].bg = bg;
592 dlg_color_table[i].hilite = hl;
595 #endif /* HAVE_COLOR */
596 fprintf(stderr, "\nParse error: unknown variable "
597 "at line %d of configuration file:\n\t%s\n", l, var);
598 result = -1; /* parse aborted */
602 fprintf(stderr, "\nParse error: syntax error at line %d of "
603 "configuration file.\n", l);
604 result = -1; /* parse aborted */
610 (void) fclose(rc_file);