2 * $Id: rc.c,v 1.60 2020/11/25 00:06:40 tom Exp $
4 * rc.c -- routines for processing the configuration file
6 * Copyright 2000-2019,2020 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>
33 #include <dlg_internals.h>
42 #define MAX_TOKEN MIN_TOKEN
45 #define UNKNOWN_COLOR -2
48 * For matching color names with color values
50 static const color_names_st color_names[] =
52 #ifdef HAVE_USE_DEFAULT_COLORS
55 {"BLACK", COLOR_BLACK},
57 {"GREEN", COLOR_GREEN},
58 {"YELLOW", COLOR_YELLOW},
60 {"MAGENTA", COLOR_MAGENTA},
62 {"WHITE", COLOR_WHITE},
64 #define COLOR_COUNT TableSize(color_names)
65 #endif /* HAVE_COLOR */
67 #define GLOBALRC "/etc/dialogrc"
68 #define DIALOGRC ".dialogrc"
75 /* Type of line in configuration file */
82 /* number of configuration variables */
83 #define VAR_COUNT TableSize(vars)
85 /* check if character is string quoting characters */
86 #define isquote(c) ((c) == '"' || (c) == '\'')
88 /* get last character of string */
89 #define lastch(str) str[strlen(str)-1]
92 * Configuration variables
95 const char *name; /* name of configuration variable as in DIALOGRC */
96 void *var; /* address of actual variable to change */
97 int type; /* type of value */
98 const char *comment; /* comment to put in "rc" file */
102 * This table should contain only references to dialog_state, since dialog_vars
103 * is reset specially in dialog.c before each widget.
105 static const vars_st vars[] =
108 &dialog_state.aspect_ratio,
110 "Set aspect-ration."},
113 &dialog_state.separate_str,
115 "Set separator (for multiple widgets output)."},
118 &dialog_state.tab_len,
120 "Set tab-length (for textbox tab-conversion)."},
123 &dialog_state.visit_items,
125 "Make tab-traversal for checklist, etc., include the list."},
129 &dialog_state.use_shadow,
131 "Shadow dialog boxes? This also turns on color."},
134 &dialog_state.use_colors,
136 "Turn color support ON or OFF"},
137 #endif /* HAVE_COLOR */
141 skip_whitespace(char *str, int n)
143 while (isblank(UCH(str[n])) && str[n] != '\0')
149 skip_keyword(char *str, int n)
151 while (isalnum(UCH(str[n])) && str[n] != '\0')
157 trim_token(char **tok)
159 char *tmp = *tok + skip_whitespace(*tok, 0);
163 while (*tmp != '\0' && !isblank(UCH(*tmp)))
170 from_boolean(const char *str)
174 if (str != NULL && *str != '\0') {
175 if (!dlg_strcmp(str, "ON")) {
177 } else if (!dlg_strcmp(str, "OFF")) {
185 from_color_name(const char *str)
187 int code = UNKNOWN_COLOR;
189 if (str != NULL && *str != '\0') {
192 for (i = 0; i < COLOR_COUNT; ++i) {
193 if (!dlg_strcmp(str, color_names[i].name)) {
194 code = color_names[i].value;
203 find_vars(char *name)
208 for (i = 0; i < VAR_COUNT; i++) {
209 if (dlg_strcmp(vars[i].name, name) == 0) {
219 find_color(char *name)
223 int limit = dlg_color_count();
225 for (i = 0; i < limit; i++) {
226 if (dlg_strcmp(dlg_color_table[i].name, name) == 0) {
235 to_color_name(int code)
237 const char *result = "?";
239 for (n = 0; n < TableSize(color_names); ++n) {
240 if (code == color_names[n].value) {
241 result = color_names[n].name;
251 return code ? "ON" : "OFF";
255 * Extract the foreground, background and highlight values from an attribute
256 * represented as a string in one of these forms:
258 * "(foreground,background,highlight,underline,reverse)"
259 * "(foreground,background,highlight,underline)"
260 * "(foreground,background,highlight)"
264 str_to_attr(char *str, DIALOG_COLORS * result)
266 char *tokens[MAX_TOKEN + 1];
267 char tempstr[MAX_LEN + 1];
270 size_t tok_count = 0;
272 memset(result, 0, sizeof(*result));
277 if (str[0] != L_PAREN || lastch(str) != R_PAREN) {
280 if ((ret = find_color(str)) >= 0) {
281 *result = dlg_color_table[ret];
284 /* invalid representation */
288 /* remove the parenthesis */
290 if (have > MAX_LEN) {
295 memcpy(tempstr, str + 1, have);
296 tempstr[have] = '\0';
298 /* parse comma-separated tokens, allow up to
299 * one more than max tokens to detect extras */
300 while (tok_count < TableSize(tokens)) {
302 tokens[tok_count++] = &tempstr[i];
304 while (tempstr[i] != '\0' && tempstr[i] != ',')
307 if (tempstr[i] == '\0')
313 if (tok_count < MIN_TOKEN || tok_count > MAX_TOKEN) {
314 /* invalid representation */
318 for (i = 0; i < tok_count; ++i)
319 trim_token(&tokens[i]);
322 if (UNKNOWN_COLOR == (result->fg = from_color_name(tokens[0]))
323 || UNKNOWN_COLOR == (result->bg = from_color_name(tokens[1]))
324 || UNKNOWN_COLOR == (result->hilite = from_boolean(tokens[2]))
326 || (tok_count >= 4 && (result->ul = from_boolean(tokens[3])) == -1)
327 || (tok_count >= 5 && (result->rv = from_boolean(tokens[4])) == -1)
328 #endif /* HAVE_RC_FILE2 */
330 /* invalid representation */
336 #endif /* HAVE_COLOR */
339 * Check if the line begins with a special keyword; if so, return true while
340 * pointing params to its parameters.
343 begins_with(char *line, const char *keyword, char **params)
345 int i = skip_whitespace(line, 0);
346 int j = skip_keyword(line, i);
348 if ((j - i) == (int) strlen(keyword)) {
351 if (!dlg_strcmp(keyword, line + i)) {
352 *params = line + skip_whitespace(line, j + 1);
362 * Parse a line in the configuration file
364 * Each line is of the form: "variable = value". On exit, 'var' will contain
365 * the variable name, and 'value' will contain the value string.
369 * LINE_EMPTY - line is blank or comment
370 * LINE_EQUALS - line contains "variable = value"
371 * LINE_ERROR - syntax error in line
374 parse_line(char *line, char **var, char **value)
378 /* ignore white space at beginning of line */
379 i = skip_whitespace(line, i);
381 if (line[i] == '\0') /* line is blank */
383 else if (line[i] == '#') /* line is comment */
385 else if (line[i] == '=') /* variable names cannot start with a '=' */
388 /* set 'var' to variable name */
389 *var = line + i++; /* skip to next character */
391 /* find end of variable name */
392 while (!isblank(UCH(line[i])) && line[i] != '=' && line[i] != '\0')
395 if (line[i] == '\0') /* syntax error */
397 else if (line[i] == '=')
402 /* skip white space before '=' */
403 i = skip_whitespace(line, i);
405 if (line[i] != '=') /* syntax error */
408 i++; /* skip the '=' */
411 /* skip white space after '=' */
412 i = skip_whitespace(line, i);
417 *value = line + i; /* set 'value' to value string */
419 /* trim trailing white space from 'value' */
420 i = (int) strlen(*value) - 1;
421 while (isblank(UCH((*value)[i])) && i > 0)
423 (*value)[i + 1] = '\0';
425 return LINE_EQUALS; /* no syntax error in line */
429 * Create the configuration file
432 dlg_create_rc(const char *filename)
437 if ((rc_file = fopen(filename, "wt")) == NULL)
438 dlg_exiterr("Error opening file for writing in dlg_create_rc().");
440 fprintf(rc_file, "#\n\
441 # Run-time configuration file for dialog\n\
443 # Automatically generated by \"dialog --create-rc <file>\"\n\
446 # Types of values:\n\
448 # Number - <number>\n\
449 # String - \"string\"\n\
450 # Boolean - <ON|OFF>\n"
454 # Attribute - (foreground,background,highlight?,underline?,reverse?)\n"
455 #else /* HAVE_RC_FILE2 */
457 # Attribute - (foreground,background,highlight?)\n"
458 #endif /* HAVE_RC_FILE2 */
459 #endif /* HAVE_COLOR */
462 /* Print an entry for each configuration variable */
463 for (i = 0; i < VAR_COUNT; i++) {
464 fprintf(rc_file, "\n# %s\n", vars[i].comment);
465 switch (vars[i].type) {
467 fprintf(rc_file, "%s = %d\n", vars[i].name,
468 *((int *) vars[i].var));
471 fprintf(rc_file, "%s = \"%s\"\n", vars[i].name,
472 (char *) vars[i].var);
475 fprintf(rc_file, "%s = %s\n", vars[i].name,
476 *((bool *) vars[i].var) ? "ON" : "OFF");
481 for (i = 0; i < (unsigned) dlg_color_count(); ++i) {
485 fprintf(rc_file, "\n# %s\n", dlg_color_table[i].comment);
486 for (j = 0; j != i; ++j) {
487 if (dlg_color_table[i].fg == dlg_color_table[j].fg
488 && dlg_color_table[i].bg == dlg_color_table[j].bg
489 && dlg_color_table[i].hilite == dlg_color_table[j].hilite) {
490 fprintf(rc_file, "%s = %s\n",
491 dlg_color_table[i].name,
492 dlg_color_table[j].name);
499 fprintf(rc_file, "%s = %c", dlg_color_table[i].name, L_PAREN);
500 fprintf(rc_file, "%s", to_color_name(dlg_color_table[i].fg));
501 fprintf(rc_file, ",%s", to_color_name(dlg_color_table[i].bg));
502 fprintf(rc_file, ",%s", to_boolean(dlg_color_table[i].hilite));
504 if (dlg_color_table[i].ul || dlg_color_table[i].rv)
505 fprintf(rc_file, ",%s", to_boolean(dlg_color_table[i].ul));
506 if (dlg_color_table[i].rv)
507 fprintf(rc_file, ",%s", to_boolean(dlg_color_table[i].rv));
508 #endif /* HAVE_RC_FILE2 */
509 fprintf(rc_file, "%c\n", R_PAREN);
512 #endif /* HAVE_COLOR */
513 dlg_dump_keys(rc_file);
515 (void) fclose(rc_file);
519 report_error(const char *filename, int line_no, const char *msg)
521 fprintf(stderr, "%s:%d: %s\n", filename, line_no, msg);
522 dlg_trace_msg("%s:%d: %s\n", filename, line_no, msg);
526 * Parse the configuration file and set up variables
534 char str[MAX_LEN + 1];
543 * At startup, dialog determines the settings to use as follows:
545 * a) if the environment variable $DIALOGRC is set, its value determines
546 * the name of the configuration file.
548 * b) if the file in (a) can't be found, use the file $HOME/.dialogrc
549 * as the configuration file.
551 * c) if the file in (b) can't be found, try using the GLOBALRC file.
552 * Usually this will be /etc/dialogrc.
554 * d) if the file in (c) cannot be found, use the compiled-in defaults.
558 if ((filename = dlg_getenv_str("DIALOGRC")) != NULL)
559 rc_file = fopen(filename, "rt");
561 if (rc_file == NULL) { /* step (a) failed? */
563 if ((filename = dlg_getenv_str("HOME")) != NULL
564 && strlen(filename) < MAX_LEN - (sizeof(DIALOGRC) + 3)) {
565 if (filename[0] == '\0' || lastch(filename) == '/')
566 sprintf(str, "%s%s", filename, DIALOGRC);
568 sprintf(str, "%s/%s", filename, DIALOGRC);
569 rc_file = fopen(filename = str, "rt");
573 if (rc_file == NULL) { /* step (b) failed? */
575 strcpy(str, GLOBALRC);
576 if ((rc_file = fopen(filename = str, "rt")) == NULL)
577 return 0; /* step (c) failed, use default values */
580 DLG_TRACE(("# opened rc file \"%s\"\n", filename));
581 /* Scan each line and set variables */
582 while ((result == 0) && (fgets(str, MAX_LEN, rc_file) != NULL)) {
583 DLG_TRACE(("#\t%s", str));
584 if (*str == '\0' || lastch(str) != '\n') {
585 /* ignore rest of file if line too long */
586 report_error(filename, l, "line too long");
587 result = -1; /* parse aborted */
592 if (begins_with(str, "bindkey", ¶ms)) {
593 if (!dlg_parse_bindkey(params)) {
594 report_error(filename, l, "invalid bindkey");
599 parse = parse_line(str, &var, &value); /* parse current line */
602 case LINE_EMPTY: /* ignore blank lines and comments */
605 /* search table for matching config variable name */
606 if ((i = find_vars(var)) >= 0) {
607 switch (vars[i].type) {
609 *((int *) vars[i].var) = atoi(value);
612 if (!isquote(value[0]) || !isquote(lastch(value))
613 || strlen(value) < 2) {
614 report_error(filename, l, "expected string value");
615 result = -1; /* parse aborted */
617 /* remove the (") quotes */
619 lastch(value) = '\0';
620 strcpy((char *) vars[i].var, value);
624 if (!dlg_strcmp(value, "ON"))
625 *((bool *) vars[i].var) = TRUE;
626 else if (!dlg_strcmp(value, "OFF"))
627 *((bool *) vars[i].var) = FALSE;
629 report_error(filename, l, "expected boolean value");
630 result = -1; /* parse aborted */
635 } else if ((i = find_color(var)) >= 0) {
637 if (str_to_attr(value, &temp) == -1) {
638 report_error(filename, l, "expected attribute value");
639 result = -1; /* parse aborted */
641 dlg_color_table[i].fg = temp.fg;
642 dlg_color_table[i].bg = temp.bg;
643 dlg_color_table[i].hilite = temp.hilite;
645 dlg_color_table[i].ul = temp.ul;
646 dlg_color_table[i].rv = temp.rv;
647 #endif /* HAVE_RC_FILE2 */
650 #endif /* HAVE_COLOR */
651 report_error(filename, l, "unknown variable");
652 result = -1; /* parse aborted */
656 report_error(filename, l, "syntax error");
657 result = -1; /* parse aborted */
663 (void) fclose(rc_file);