rc.c revision 224014
1217309Snwhitehorn/* 2224014Snwhitehorn * $Id: rc.c,v 1.47 2011/06/20 22:30:04 tom Exp $ 3217309Snwhitehorn * 4217309Snwhitehorn * rc.c -- routines for processing the configuration file 5217309Snwhitehorn * 6224014Snwhitehorn * Copyright 2000-2010,2011 Thomas E. Dickey 7217309Snwhitehorn * 8217309Snwhitehorn * This program is free software; you can redistribute it and/or modify 9217309Snwhitehorn * it under the terms of the GNU Lesser General Public License, version 2.1 10217309Snwhitehorn * as published by the Free Software Foundation. 11217309Snwhitehorn * 12217309Snwhitehorn * This program is distributed in the hope that it will be useful, but 13217309Snwhitehorn * WITHOUT ANY WARRANTY; without even the implied warranty of 14217309Snwhitehorn * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15217309Snwhitehorn * Lesser General Public License for more details. 16217309Snwhitehorn * 17217309Snwhitehorn * You should have received a copy of the GNU Lesser General Public 18217309Snwhitehorn * License along with this program; if not, write to 19217309Snwhitehorn * Free Software Foundation, Inc. 20217309Snwhitehorn * 51 Franklin St., Fifth Floor 21217309Snwhitehorn * Boston, MA 02110, USA. 22217309Snwhitehorn * 23217309Snwhitehorn * An earlier version of this program lists as authors 24217309Snwhitehorn * Savio Lam (lam836@cs.cuhk.hk) 25217309Snwhitehorn */ 26217309Snwhitehorn 27217309Snwhitehorn#include <dialog.h> 28217309Snwhitehorn 29217309Snwhitehorn#include <dlg_keys.h> 30217309Snwhitehorn 31217309Snwhitehorn#ifdef HAVE_COLOR 32217309Snwhitehorn#include <dlg_colors.h> 33217309Snwhitehorn 34217309Snwhitehorn/* 35217309Snwhitehorn * For matching color names with color values 36217309Snwhitehorn */ 37217309Snwhitehornstatic const color_names_st color_names[] = 38217309Snwhitehorn{ 39217309Snwhitehorn#ifdef HAVE_USE_DEFAULT_COLORS 40217309Snwhitehorn {"DEFAULT", -1}, 41217309Snwhitehorn#endif 42217309Snwhitehorn {"BLACK", COLOR_BLACK}, 43217309Snwhitehorn {"RED", COLOR_RED}, 44217309Snwhitehorn {"GREEN", COLOR_GREEN}, 45217309Snwhitehorn {"YELLOW", COLOR_YELLOW}, 46217309Snwhitehorn {"BLUE", COLOR_BLUE}, 47217309Snwhitehorn {"MAGENTA", COLOR_MAGENTA}, 48217309Snwhitehorn {"CYAN", COLOR_CYAN}, 49217309Snwhitehorn {"WHITE", COLOR_WHITE}, 50217309Snwhitehorn}; /* color names */ 51217309Snwhitehorn#define COLOR_COUNT (sizeof(color_names) / sizeof(color_names[0])) 52217309Snwhitehorn#endif /* HAVE_COLOR */ 53217309Snwhitehorn 54217309Snwhitehorn#define GLOBALRC "/etc/dialogrc" 55217309Snwhitehorn#define DIALOGRC ".dialogrc" 56217309Snwhitehorn 57217309Snwhitehorn/* Types of values */ 58217309Snwhitehorn#define VAL_INT 0 59217309Snwhitehorn#define VAL_STR 1 60217309Snwhitehorn#define VAL_BOOL 2 61217309Snwhitehorn 62217309Snwhitehorn/* Type of line in configuration file */ 63217309Snwhitehorntypedef enum { 64217309Snwhitehorn LINE_ERROR = -1, 65217309Snwhitehorn LINE_EQUALS, 66217309Snwhitehorn LINE_EMPTY 67217309Snwhitehorn} PARSE_LINE; 68217309Snwhitehorn 69217309Snwhitehorn/* number of configuration variables */ 70217309Snwhitehorn#define VAR_COUNT (sizeof(vars) / sizeof(vars_st)) 71217309Snwhitehorn 72217309Snwhitehorn/* check if character is white space */ 73217309Snwhitehorn#define whitespace(c) (c == ' ' || c == TAB) 74217309Snwhitehorn 75217309Snwhitehorn/* check if character is string quoting characters */ 76217309Snwhitehorn#define isquote(c) (c == '"' || c == '\'') 77217309Snwhitehorn 78217309Snwhitehorn/* get last character of string */ 79217309Snwhitehorn#define lastch(str) str[strlen(str)-1] 80217309Snwhitehorn 81217309Snwhitehorn/* 82217309Snwhitehorn * Configuration variables 83217309Snwhitehorn */ 84217309Snwhitehorntypedef struct { 85217309Snwhitehorn const char *name; /* name of configuration variable as in DIALOGRC */ 86217309Snwhitehorn void *var; /* address of actual variable to change */ 87217309Snwhitehorn int type; /* type of value */ 88217309Snwhitehorn const char *comment; /* comment to put in "rc" file */ 89217309Snwhitehorn} vars_st; 90217309Snwhitehorn 91217309Snwhitehorn/* 92217309Snwhitehorn * This table should contain only references to dialog_state, since dialog_vars 93217309Snwhitehorn * is reset specially in dialog.c before each widget. 94217309Snwhitehorn */ 95217309Snwhitehornstatic const vars_st vars[] = 96217309Snwhitehorn{ 97217309Snwhitehorn {"aspect", 98217309Snwhitehorn &dialog_state.aspect_ratio, 99217309Snwhitehorn VAL_INT, 100217309Snwhitehorn "Set aspect-ration."}, 101217309Snwhitehorn 102217309Snwhitehorn {"separate_widget", 103217309Snwhitehorn &dialog_state.separate_str, 104217309Snwhitehorn VAL_STR, 105217309Snwhitehorn "Set separator (for multiple widgets output)."}, 106217309Snwhitehorn 107217309Snwhitehorn {"tab_len", 108217309Snwhitehorn &dialog_state.tab_len, 109217309Snwhitehorn VAL_INT, 110217309Snwhitehorn "Set tab-length (for textbox tab-conversion)."}, 111217309Snwhitehorn 112217309Snwhitehorn {"visit_items", 113217309Snwhitehorn &dialog_state.visit_items, 114217309Snwhitehorn VAL_BOOL, 115217309Snwhitehorn "Make tab-traversal for checklist, etc., include the list."}, 116217309Snwhitehorn 117217309Snwhitehorn#ifdef HAVE_COLOR 118217309Snwhitehorn {"use_shadow", 119217309Snwhitehorn &dialog_state.use_shadow, 120217309Snwhitehorn VAL_BOOL, 121217309Snwhitehorn "Shadow dialog boxes? This also turns on color."}, 122217309Snwhitehorn 123217309Snwhitehorn {"use_colors", 124217309Snwhitehorn &dialog_state.use_colors, 125217309Snwhitehorn VAL_BOOL, 126217309Snwhitehorn "Turn color support ON or OFF"}, 127217309Snwhitehorn#endif /* HAVE_COLOR */ 128217309Snwhitehorn}; /* vars */ 129217309Snwhitehorn 130217309Snwhitehornstatic int 131217309Snwhitehornskip_whitespace(char *str, int n) 132217309Snwhitehorn{ 133217309Snwhitehorn while (whitespace(str[n]) && str[n] != '\0') 134217309Snwhitehorn n++; 135217309Snwhitehorn return n; 136217309Snwhitehorn} 137217309Snwhitehorn 138217309Snwhitehornstatic int 139217309Snwhitehornskip_keyword(char *str, int n) 140217309Snwhitehorn{ 141217309Snwhitehorn while (isalnum(UCH(str[n])) && str[n] != '\0') 142217309Snwhitehorn n++; 143217309Snwhitehorn return n; 144217309Snwhitehorn} 145217309Snwhitehorn 146217309Snwhitehornstatic int 147217309Snwhitehornfind_vars(char *name) 148217309Snwhitehorn{ 149217309Snwhitehorn int result = -1; 150217309Snwhitehorn unsigned i; 151217309Snwhitehorn 152217309Snwhitehorn for (i = 0; i < VAR_COUNT; i++) { 153217309Snwhitehorn if (dlg_strcmp(vars[i].name, name) == 0) { 154217309Snwhitehorn result = (int) i; 155217309Snwhitehorn break; 156217309Snwhitehorn } 157217309Snwhitehorn } 158217309Snwhitehorn return result; 159217309Snwhitehorn} 160217309Snwhitehorn 161217309Snwhitehorn#ifdef HAVE_COLOR 162217309Snwhitehornstatic int 163217309Snwhitehornfind_color(char *name) 164217309Snwhitehorn{ 165217309Snwhitehorn int result = -1; 166217309Snwhitehorn int i; 167217309Snwhitehorn int limit = dlg_color_count(); 168217309Snwhitehorn 169217309Snwhitehorn for (i = 0; i < limit; i++) { 170217309Snwhitehorn if (dlg_strcmp(dlg_color_table[i].name, name) == 0) { 171217309Snwhitehorn result = i; 172217309Snwhitehorn break; 173217309Snwhitehorn } 174217309Snwhitehorn } 175217309Snwhitehorn return result; 176217309Snwhitehorn} 177217309Snwhitehorn 178217309Snwhitehorn/* 179217309Snwhitehorn * Convert an attribute to a string representation like this: 180217309Snwhitehorn * 181217309Snwhitehorn * "(foreground,background,highlight)" 182217309Snwhitehorn */ 183217309Snwhitehornstatic char * 184217309Snwhitehornattr_to_str(char *str, int fg, int bg, int hl) 185217309Snwhitehorn{ 186217309Snwhitehorn int i; 187217309Snwhitehorn 188217309Snwhitehorn strcpy(str, "("); 189217309Snwhitehorn /* foreground */ 190217309Snwhitehorn for (i = 0; fg != color_names[i].value; i++) ; 191217309Snwhitehorn strcat(str, color_names[i].name); 192217309Snwhitehorn strcat(str, ","); 193217309Snwhitehorn 194217309Snwhitehorn /* background */ 195217309Snwhitehorn for (i = 0; bg != color_names[i].value; i++) ; 196217309Snwhitehorn strcat(str, color_names[i].name); 197217309Snwhitehorn 198217309Snwhitehorn /* highlight */ 199217309Snwhitehorn strcat(str, hl ? ",ON)" : ",OFF)"); 200217309Snwhitehorn 201217309Snwhitehorn return str; 202217309Snwhitehorn} 203217309Snwhitehorn 204217309Snwhitehorn/* 205217309Snwhitehorn * Extract the foreground, background and highlight values from an attribute 206217309Snwhitehorn * represented as a string in this form: 207217309Snwhitehorn * 208217309Snwhitehorn * "(foreground,background,highlight)" 209217309Snwhitehorn */ 210217309Snwhitehornstatic int 211217309Snwhitehornstr_to_attr(char *str, int *fg, int *bg, int *hl) 212217309Snwhitehorn{ 213217309Snwhitehorn int i = 0, get_fg = 1; 214217309Snwhitehorn unsigned j; 215217309Snwhitehorn char tempstr[MAX_LEN + 1], *part; 216217309Snwhitehorn 217217309Snwhitehorn if (str[0] != '(' || lastch(str) != ')') 218217309Snwhitehorn return -1; /* invalid representation */ 219217309Snwhitehorn 220217309Snwhitehorn /* remove the parenthesis */ 221217309Snwhitehorn strcpy(tempstr, str + 1); 222217309Snwhitehorn lastch(tempstr) = '\0'; 223217309Snwhitehorn 224217309Snwhitehorn /* get foreground and background */ 225217309Snwhitehorn 226217309Snwhitehorn while (1) { 227217309Snwhitehorn /* skip white space before fg/bg string */ 228217309Snwhitehorn i = skip_whitespace(tempstr, i); 229217309Snwhitehorn if (tempstr[i] == '\0') 230217309Snwhitehorn return -1; /* invalid representation */ 231217309Snwhitehorn part = tempstr + i; /* set 'part' to start of fg/bg string */ 232217309Snwhitehorn 233217309Snwhitehorn /* find end of fg/bg string */ 234217309Snwhitehorn while (!whitespace(tempstr[i]) && tempstr[i] != ',' 235217309Snwhitehorn && tempstr[i] != '\0') 236217309Snwhitehorn i++; 237217309Snwhitehorn 238217309Snwhitehorn if (tempstr[i] == '\0') 239217309Snwhitehorn return -1; /* invalid representation */ 240217309Snwhitehorn else if (whitespace(tempstr[i])) { /* not yet ',' */ 241217309Snwhitehorn tempstr[i++] = '\0'; 242217309Snwhitehorn 243217309Snwhitehorn /* skip white space before ',' */ 244217309Snwhitehorn i = skip_whitespace(tempstr, i); 245217309Snwhitehorn if (tempstr[i] != ',') 246217309Snwhitehorn return -1; /* invalid representation */ 247217309Snwhitehorn } 248217309Snwhitehorn tempstr[i++] = '\0'; /* skip the ',' */ 249217309Snwhitehorn for (j = 0; j < COLOR_COUNT && dlg_strcmp(part, color_names[j].name); 250217309Snwhitehorn j++) ; 251217309Snwhitehorn if (j == COLOR_COUNT) /* invalid color name */ 252217309Snwhitehorn return -1; 253217309Snwhitehorn if (get_fg) { 254217309Snwhitehorn *fg = color_names[j].value; 255217309Snwhitehorn get_fg = 0; /* next we have to get the background */ 256217309Snwhitehorn } else { 257217309Snwhitehorn *bg = color_names[j].value; 258217309Snwhitehorn break; 259217309Snwhitehorn } 260217309Snwhitehorn } /* got foreground and background */ 261217309Snwhitehorn 262217309Snwhitehorn /* get highlight */ 263217309Snwhitehorn 264217309Snwhitehorn /* skip white space before highlight string */ 265217309Snwhitehorn i = skip_whitespace(tempstr, i); 266217309Snwhitehorn if (tempstr[i] == '\0') 267217309Snwhitehorn return -1; /* invalid representation */ 268217309Snwhitehorn part = tempstr + i; /* set 'part' to start of highlight string */ 269217309Snwhitehorn 270217309Snwhitehorn /* trim trailing white space from highlight string */ 271217309Snwhitehorn i = (int) strlen(part) - 1; 272217309Snwhitehorn while (whitespace(part[i]) && i > 0) 273217309Snwhitehorn i--; 274217309Snwhitehorn part[i + 1] = '\0'; 275217309Snwhitehorn 276217309Snwhitehorn if (!dlg_strcmp(part, "ON")) 277217309Snwhitehorn *hl = TRUE; 278217309Snwhitehorn else if (!dlg_strcmp(part, "OFF")) 279217309Snwhitehorn *hl = FALSE; 280217309Snwhitehorn else 281217309Snwhitehorn return -1; /* invalid highlight value */ 282217309Snwhitehorn 283217309Snwhitehorn return 0; 284217309Snwhitehorn} 285217309Snwhitehorn#endif /* HAVE_COLOR */ 286217309Snwhitehorn 287217309Snwhitehorn/* 288217309Snwhitehorn * Check if the line begins with a special keyword; if so, return true while 289217309Snwhitehorn * pointing params to its parameters. 290217309Snwhitehorn */ 291217309Snwhitehornstatic int 292217309Snwhitehornbegins_with(char *line, const char *keyword, char **params) 293217309Snwhitehorn{ 294217309Snwhitehorn int i = skip_whitespace(line, 0); 295217309Snwhitehorn int j = skip_keyword(line, i); 296217309Snwhitehorn 297217309Snwhitehorn if ((j - i) == (int) strlen(keyword)) { 298217309Snwhitehorn char save = line[j]; 299217309Snwhitehorn line[j] = 0; 300217309Snwhitehorn if (!dlg_strcmp(keyword, line + i)) { 301217309Snwhitehorn *params = line + skip_whitespace(line, j + 1); 302217309Snwhitehorn return 1; 303217309Snwhitehorn } 304217309Snwhitehorn line[j] = save; 305217309Snwhitehorn } 306217309Snwhitehorn 307217309Snwhitehorn return 0; 308217309Snwhitehorn} 309217309Snwhitehorn 310217309Snwhitehorn/* 311217309Snwhitehorn * Parse a line in the configuration file 312217309Snwhitehorn * 313217309Snwhitehorn * Each line is of the form: "variable = value". On exit, 'var' will contain 314217309Snwhitehorn * the variable name, and 'value' will contain the value string. 315217309Snwhitehorn * 316217309Snwhitehorn * Return values: 317217309Snwhitehorn * 318217309Snwhitehorn * LINE_EMPTY - line is blank or comment 319217309Snwhitehorn * LINE_EQUALS - line contains "variable = value" 320217309Snwhitehorn * LINE_ERROR - syntax error in line 321217309Snwhitehorn */ 322217309Snwhitehornstatic PARSE_LINE 323217309Snwhitehornparse_line(char *line, char **var, char **value) 324217309Snwhitehorn{ 325217309Snwhitehorn int i = 0; 326217309Snwhitehorn 327217309Snwhitehorn /* ignore white space at beginning of line */ 328217309Snwhitehorn i = skip_whitespace(line, i); 329217309Snwhitehorn 330217309Snwhitehorn if (line[i] == '\0') /* line is blank */ 331217309Snwhitehorn return LINE_EMPTY; 332217309Snwhitehorn else if (line[i] == '#') /* line is comment */ 333217309Snwhitehorn return LINE_EMPTY; 334217309Snwhitehorn else if (line[i] == '=') /* variable names cannot start with a '=' */ 335217309Snwhitehorn return LINE_ERROR; 336217309Snwhitehorn 337217309Snwhitehorn /* set 'var' to variable name */ 338217309Snwhitehorn *var = line + i++; /* skip to next character */ 339217309Snwhitehorn 340217309Snwhitehorn /* find end of variable name */ 341217309Snwhitehorn while (!whitespace(line[i]) && line[i] != '=' && line[i] != '\0') 342217309Snwhitehorn i++; 343217309Snwhitehorn 344217309Snwhitehorn if (line[i] == '\0') /* syntax error */ 345217309Snwhitehorn return LINE_ERROR; 346217309Snwhitehorn else if (line[i] == '=') 347217309Snwhitehorn line[i++] = '\0'; 348217309Snwhitehorn else { 349217309Snwhitehorn line[i++] = '\0'; 350217309Snwhitehorn 351217309Snwhitehorn /* skip white space before '=' */ 352217309Snwhitehorn i = skip_whitespace(line, i); 353217309Snwhitehorn 354217309Snwhitehorn if (line[i] != '=') /* syntax error */ 355217309Snwhitehorn return LINE_ERROR; 356217309Snwhitehorn else 357217309Snwhitehorn i++; /* skip the '=' */ 358217309Snwhitehorn } 359217309Snwhitehorn 360217309Snwhitehorn /* skip white space after '=' */ 361217309Snwhitehorn i = skip_whitespace(line, i); 362217309Snwhitehorn 363217309Snwhitehorn if (line[i] == '\0') 364217309Snwhitehorn return LINE_ERROR; 365217309Snwhitehorn else 366217309Snwhitehorn *value = line + i; /* set 'value' to value string */ 367217309Snwhitehorn 368217309Snwhitehorn /* trim trailing white space from 'value' */ 369217309Snwhitehorn i = (int) strlen(*value) - 1; 370217309Snwhitehorn while (whitespace((*value)[i]) && i > 0) 371217309Snwhitehorn i--; 372217309Snwhitehorn (*value)[i + 1] = '\0'; 373217309Snwhitehorn 374217309Snwhitehorn return LINE_EQUALS; /* no syntax error in line */ 375217309Snwhitehorn} 376217309Snwhitehorn 377217309Snwhitehorn/* 378217309Snwhitehorn * Create the configuration file 379217309Snwhitehorn */ 380217309Snwhitehornvoid 381217309Snwhitehorndlg_create_rc(const char *filename) 382217309Snwhitehorn{ 383217309Snwhitehorn unsigned i; 384217309Snwhitehorn FILE *rc_file; 385217309Snwhitehorn 386217309Snwhitehorn if ((rc_file = fopen(filename, "wt")) == NULL) 387217309Snwhitehorn dlg_exiterr("Error opening file for writing in dlg_create_rc()."); 388217309Snwhitehorn 389217309Snwhitehorn fprintf(rc_file, "#\n\ 390217309Snwhitehorn# Run-time configuration file for dialog\n\ 391217309Snwhitehorn#\n\ 392217309Snwhitehorn# Automatically generated by \"dialog --create-rc <file>\"\n\ 393217309Snwhitehorn#\n\ 394217309Snwhitehorn#\n\ 395217309Snwhitehorn# Types of values:\n\ 396217309Snwhitehorn#\n\ 397217309Snwhitehorn# Number - <number>\n\ 398217309Snwhitehorn# String - \"string\"\n\ 399217309Snwhitehorn# Boolean - <ON|OFF>\n" 400217309Snwhitehorn#ifdef HAVE_COLOR 401217309Snwhitehorn "\ 402217309Snwhitehorn# Attribute - (foreground,background,highlight?)\n" 403217309Snwhitehorn#endif 404217309Snwhitehorn ); 405217309Snwhitehorn 406217309Snwhitehorn /* Print an entry for each configuration variable */ 407217309Snwhitehorn for (i = 0; i < VAR_COUNT; i++) { 408217309Snwhitehorn fprintf(rc_file, "\n# %s\n", vars[i].comment); 409217309Snwhitehorn switch (vars[i].type) { 410217309Snwhitehorn case VAL_INT: 411217309Snwhitehorn fprintf(rc_file, "%s = %d\n", vars[i].name, 412217309Snwhitehorn *((int *) vars[i].var)); 413217309Snwhitehorn break; 414217309Snwhitehorn case VAL_STR: 415217309Snwhitehorn fprintf(rc_file, "%s = \"%s\"\n", vars[i].name, 416217309Snwhitehorn (char *) vars[i].var); 417217309Snwhitehorn break; 418217309Snwhitehorn case VAL_BOOL: 419217309Snwhitehorn fprintf(rc_file, "%s = %s\n", vars[i].name, 420217309Snwhitehorn *((bool *) vars[i].var) ? "ON" : "OFF"); 421217309Snwhitehorn break; 422217309Snwhitehorn } 423217309Snwhitehorn } 424217309Snwhitehorn#ifdef HAVE_COLOR 425217309Snwhitehorn for (i = 0; i < (unsigned) dlg_color_count(); ++i) { 426217309Snwhitehorn char buffer[MAX_LEN + 1]; 427217309Snwhitehorn 428217309Snwhitehorn fprintf(rc_file, "\n# %s\n", dlg_color_table[i].comment); 429217309Snwhitehorn fprintf(rc_file, "%s = %s\n", dlg_color_table[i].name, 430217309Snwhitehorn attr_to_str(buffer, 431217309Snwhitehorn dlg_color_table[i].fg, 432217309Snwhitehorn dlg_color_table[i].bg, 433217309Snwhitehorn dlg_color_table[i].hilite)); 434217309Snwhitehorn } 435217309Snwhitehorn#endif /* HAVE_COLOR */ 436217309Snwhitehorn dlg_dump_keys(rc_file); 437217309Snwhitehorn 438217309Snwhitehorn (void) fclose(rc_file); 439217309Snwhitehorn} 440217309Snwhitehorn 441217309Snwhitehorn/* 442217309Snwhitehorn * Parse the configuration file and set up variables 443217309Snwhitehorn */ 444217309Snwhitehornint 445217309Snwhitehorndlg_parse_rc(void) 446217309Snwhitehorn{ 447217309Snwhitehorn int i; 448217309Snwhitehorn int l = 1; 449217309Snwhitehorn PARSE_LINE parse; 450217309Snwhitehorn char str[MAX_LEN + 1]; 451217309Snwhitehorn char *var; 452217309Snwhitehorn char *value; 453217309Snwhitehorn char *tempptr; 454217309Snwhitehorn int result = 0; 455217309Snwhitehorn FILE *rc_file = 0; 456217309Snwhitehorn char *params; 457217309Snwhitehorn 458217309Snwhitehorn /* 459217309Snwhitehorn * At startup, dialog determines the settings to use as follows: 460217309Snwhitehorn * 461217309Snwhitehorn * a) if the environment variable $DIALOGRC is set, its value determines 462217309Snwhitehorn * the name of the configuration file. 463217309Snwhitehorn * 464217309Snwhitehorn * b) if the file in (a) can't be found, use the file $HOME/.dialogrc 465217309Snwhitehorn * as the configuration file. 466217309Snwhitehorn * 467217309Snwhitehorn * c) if the file in (b) can't be found, try using the GLOBALRC file. 468217309Snwhitehorn * Usually this will be /etc/dialogrc. 469217309Snwhitehorn * 470217309Snwhitehorn * d) if the file in (c) cannot be found, use the compiled-in defaults. 471217309Snwhitehorn */ 472217309Snwhitehorn 473217309Snwhitehorn /* try step (a) */ 474217309Snwhitehorn if ((tempptr = getenv("DIALOGRC")) != NULL) 475217309Snwhitehorn rc_file = fopen(tempptr, "rt"); 476217309Snwhitehorn 477217309Snwhitehorn if (rc_file == NULL) { /* step (a) failed? */ 478217309Snwhitehorn /* try step (b) */ 479217309Snwhitehorn if ((tempptr = getenv("HOME")) != NULL 480217309Snwhitehorn && strlen(tempptr) < MAX_LEN - (sizeof(DIALOGRC) + 3)) { 481217309Snwhitehorn if (tempptr[0] == '\0' || lastch(tempptr) == '/') 482217309Snwhitehorn sprintf(str, "%s%s", tempptr, DIALOGRC); 483217309Snwhitehorn else 484217309Snwhitehorn sprintf(str, "%s/%s", tempptr, DIALOGRC); 485224014Snwhitehorn rc_file = fopen(tempptr = str, "rt"); 486217309Snwhitehorn } 487217309Snwhitehorn } 488217309Snwhitehorn 489217309Snwhitehorn if (rc_file == NULL) { /* step (b) failed? */ 490217309Snwhitehorn /* try step (c) */ 491217309Snwhitehorn strcpy(str, GLOBALRC); 492224014Snwhitehorn if ((rc_file = fopen(tempptr = str, "rt")) == NULL) 493217309Snwhitehorn return 0; /* step (c) failed, use default values */ 494217309Snwhitehorn } 495217309Snwhitehorn 496224014Snwhitehorn DLG_TRACE(("opened rc file \"%s\"\n", tempptr)); 497217309Snwhitehorn /* Scan each line and set variables */ 498217309Snwhitehorn while ((result == 0) && (fgets(str, MAX_LEN, rc_file) != NULL)) { 499224014Snwhitehorn DLG_TRACE(("rc:%s", str)); 500217309Snwhitehorn if (*str == '\0' || lastch(str) != '\n') { 501217309Snwhitehorn /* ignore rest of file if line too long */ 502217309Snwhitehorn fprintf(stderr, "\nParse error: line %d of configuration" 503217309Snwhitehorn " file too long.\n", l); 504217309Snwhitehorn result = -1; /* parse aborted */ 505217309Snwhitehorn break; 506217309Snwhitehorn } 507217309Snwhitehorn 508217309Snwhitehorn lastch(str) = '\0'; 509217309Snwhitehorn if (begins_with(str, "bindkey", ¶ms)) { 510217309Snwhitehorn dlg_parse_bindkey(params); 511217309Snwhitehorn continue; 512217309Snwhitehorn } 513217309Snwhitehorn parse = parse_line(str, &var, &value); /* parse current line */ 514217309Snwhitehorn 515217309Snwhitehorn switch (parse) { 516217309Snwhitehorn case LINE_EMPTY: /* ignore blank lines and comments */ 517217309Snwhitehorn break; 518217309Snwhitehorn case LINE_EQUALS: 519217309Snwhitehorn /* search table for matching config variable name */ 520217309Snwhitehorn if ((i = find_vars(var)) >= 0) { 521217309Snwhitehorn switch (vars[i].type) { 522217309Snwhitehorn case VAL_INT: 523217309Snwhitehorn *((int *) vars[i].var) = atoi(value); 524217309Snwhitehorn break; 525217309Snwhitehorn case VAL_STR: 526217309Snwhitehorn if (!isquote(value[0]) || !isquote(lastch(value)) 527217309Snwhitehorn || strlen(value) < 2) { 528217309Snwhitehorn fprintf(stderr, "\nParse error: string value " 529217309Snwhitehorn "expected at line %d of configuration " 530217309Snwhitehorn "file.\n", l); 531217309Snwhitehorn result = -1; /* parse aborted */ 532217309Snwhitehorn } else { 533217309Snwhitehorn /* remove the (") quotes */ 534217309Snwhitehorn value++; 535217309Snwhitehorn lastch(value) = '\0'; 536217309Snwhitehorn strcpy((char *) vars[i].var, value); 537217309Snwhitehorn } 538217309Snwhitehorn break; 539217309Snwhitehorn case VAL_BOOL: 540217309Snwhitehorn if (!dlg_strcmp(value, "ON")) 541217309Snwhitehorn *((bool *) vars[i].var) = TRUE; 542217309Snwhitehorn else if (!dlg_strcmp(value, "OFF")) 543217309Snwhitehorn *((bool *) vars[i].var) = FALSE; 544217309Snwhitehorn else { 545217309Snwhitehorn fprintf(stderr, "\nParse error: boolean value " 546217309Snwhitehorn "expected at line %d of configuration " 547217309Snwhitehorn "file (found %s).\n", l, value); 548217309Snwhitehorn result = -1; /* parse aborted */ 549217309Snwhitehorn } 550217309Snwhitehorn break; 551217309Snwhitehorn } 552217309Snwhitehorn#ifdef HAVE_COLOR 553217309Snwhitehorn } else if ((i = find_color(var)) >= 0) { 554217309Snwhitehorn int fg = 0; 555217309Snwhitehorn int bg = 0; 556217309Snwhitehorn int hl = 0; 557217309Snwhitehorn if (str_to_attr(value, &fg, &bg, &hl) == -1) { 558217309Snwhitehorn fprintf(stderr, "\nParse error: attribute " 559217309Snwhitehorn "value expected at line %d of configuration " 560217309Snwhitehorn "file.\n", l); 561217309Snwhitehorn result = -1; /* parse aborted */ 562217309Snwhitehorn } else { 563217309Snwhitehorn dlg_color_table[i].fg = fg; 564217309Snwhitehorn dlg_color_table[i].bg = bg; 565217309Snwhitehorn dlg_color_table[i].hilite = hl; 566217309Snwhitehorn } 567217309Snwhitehorn } else { 568217309Snwhitehorn#endif /* HAVE_COLOR */ 569217309Snwhitehorn fprintf(stderr, "\nParse error: unknown variable " 570217309Snwhitehorn "at line %d of configuration file:\n\t%s\n", l, var); 571217309Snwhitehorn result = -1; /* parse aborted */ 572217309Snwhitehorn } 573217309Snwhitehorn break; 574217309Snwhitehorn case LINE_ERROR: 575217309Snwhitehorn fprintf(stderr, "\nParse error: syntax error at line %d of " 576217309Snwhitehorn "configuration file.\n", l); 577217309Snwhitehorn result = -1; /* parse aborted */ 578217309Snwhitehorn break; 579217309Snwhitehorn } 580217309Snwhitehorn l++; /* next line */ 581217309Snwhitehorn } 582217309Snwhitehorn 583217309Snwhitehorn (void) fclose(rc_file); 584217309Snwhitehorn return result; 585217309Snwhitehorn} 586