Example #1
0
static void
show_tsearch(const void *nodep, const VISIT which, const int depth)
{
    const CACHE *p = *(CACHE * const *) nodep;
    (void) depth;
    if (which == postorder || which == leaf) {
	DLG_TRACE(("# cache %p %p:%s\n", p, p->string, p->string));
    }
}
Example #2
0
static int
read_data(char *buffer, FILE *fp)
{
    int result;

    if (feof(fp)) {
	result = 0;
    } else if (fgets(buffer, MY_LEN, fp) != 0) {
	DLG_TRACE(("read_data:%s", buffer));
	dlg_trim_string(buffer);
	result = 1;
    } else {
	result = -1;
    }
    return result;
}
Example #3
0
static void
trace_cache(const char *fn, int ln)
{
    DLG_TRACE(("# trace_cache %s@%d\n", fn, ln));
    twalk(sorted_cache, show_tsearch);
}
Example #4
0
/*
 * Parse the configuration file and set up variables
 */
int
dlg_parse_rc(void)
{
    int i;
    int l = 1;
    PARSE_LINE parse;
    char str[MAX_LEN + 1];
    char *var;
    char *value;
    char *tempptr;
    int result = 0;
    FILE *rc_file = 0;
    char *params;

    /*
     *  At startup, dialog determines the settings to use as follows:
     *
     *  a) if the environment variable $DIALOGRC is set, its value determines
     *     the name of the configuration file.
     *
     *  b) if the file in (a) can't be found, use the file $HOME/.dialogrc
     *     as the configuration file.
     *
     *  c) if the file in (b) can't be found, try using the GLOBALRC file.
     *     Usually this will be /etc/dialogrc.
     *
     *  d) if the file in (c) cannot be found, use the compiled-in defaults.
     */

    /* try step (a) */
    if ((tempptr = getenv("DIALOGRC")) != NULL)
        rc_file = fopen(tempptr, "rt");

    if (rc_file == NULL) {	/* step (a) failed? */
        /* try step (b) */
        if ((tempptr = getenv("HOME")) != NULL
                && strlen(tempptr) < MAX_LEN - (sizeof(DIALOGRC) + 3)) {
            if (tempptr[0] == '\0' || lastch(tempptr) == '/')
                sprintf(str, "%s%s", tempptr, DIALOGRC);
            else
                sprintf(str, "%s/%s", tempptr, DIALOGRC);
            rc_file = fopen(tempptr = str, "rt");
        }
    }

    if (rc_file == NULL) {	/* step (b) failed? */
        /* try step (c) */
        strcpy(str, GLOBALRC);
        if ((rc_file = fopen(tempptr = str, "rt")) == NULL)
            return 0;		/* step (c) failed, use default values */
    }

    DLG_TRACE(("opened rc file \"%s\"\n", tempptr));
    /* Scan each line and set variables */
    while ((result == 0) && (fgets(str, MAX_LEN, rc_file) != NULL)) {
        DLG_TRACE(("rc:%s", str));
        if (*str == '\0' || lastch(str) != '\n') {
            /* ignore rest of file if line too long */
            fprintf(stderr, "\nParse error: line %d of configuration"
                    " file too long.\n", l);
            result = -1;	/* parse aborted */
            break;
        }

        lastch(str) = '\0';
        if (begins_with(str, "bindkey", &params)) {
            if (!dlg_parse_bindkey(params)) {
                fprintf(stderr, "\nParse error: line %d of configuration\n", l);
                result = -1;
            }
            continue;
        }
        parse = parse_line(str, &var, &value);	/* parse current line */

        switch (parse) {
        case LINE_EMPTY:	/* ignore blank lines and comments */
            break;
        case LINE_EQUALS:
            /* search table for matching config variable name */
            if ((i = find_vars(var)) >= 0) {
                switch (vars[i].type) {
                case VAL_INT:
                    *((int *) vars[i].var) = atoi(value);
                    break;
                case VAL_STR:
                    if (!isquote(value[0]) || !isquote(lastch(value))
                            || strlen(value) < 2) {
                        fprintf(stderr, "\nParse error: string value "
                                "expected at line %d of configuration "
                                "file.\n", l);
                        result = -1;	/* parse aborted */
                    } else {
                        /* remove the (") quotes */
                        value++;
                        lastch(value) = '\0';
                        strcpy((char *) vars[i].var, value);
                    }
                    break;
                case VAL_BOOL:
                    if (!dlg_strcmp(value, "ON"))
                        *((bool *) vars[i].var) = TRUE;
                    else if (!dlg_strcmp(value, "OFF"))
                        *((bool *) vars[i].var) = FALSE;
                    else {
                        fprintf(stderr, "\nParse error: boolean value "
                                "expected at line %d of configuration "
                                "file (found %s).\n", l, value);
                        result = -1;	/* parse aborted */
                    }
                    break;
                }
#ifdef HAVE_COLOR
            } else if ((i = find_color(var)) >= 0) {
                int fg = 0;
                int bg = 0;
                int hl = 0;
                if (str_to_attr(value, &fg, &bg, &hl) == -1) {
                    fprintf(stderr, "\nParse error: attribute "
                            "value expected at line %d of configuration "
                            "file.\n", l);
                    result = -1;	/* parse aborted */
                } else {
                    dlg_color_table[i].fg = fg;
                    dlg_color_table[i].bg = bg;
                    dlg_color_table[i].hilite = hl;
                }
            } else {
#endif /* HAVE_COLOR */
                fprintf(stderr, "\nParse error: unknown variable "
                        "at line %d of configuration file:\n\t%s\n", l, var);
                result = -1;	/* parse aborted */
            }
            break;
        case LINE_ERROR:
            fprintf(stderr, "\nParse error: syntax error at line %d of "
                    "configuration file.\n", l);
            result = -1;	/* parse aborted */
            break;
        }
        l++;			/* next line */
    }

    (void) fclose(rc_file);
    return result;
}