dotconf icon indicating copy to clipboard operation
dotconf copied to clipboard

Errors through "Include" are not relayed

Open sgielen opened this issue 12 years ago • 2 comments

When a parsing error is introduced in a DOTCONF_CB function by returning a non-NULL charptr, dotconf_command_loop() is supposed to return 0 indicating failure to parse. This works fine for the main configuration file, but not if errors are found in included configuration files. I will attach a test case shortly, where the following configuration file fails as expected:

# error.conf
Error 1

The following file succeeds as expected:

# ok.conf
Error 0

And the following file succeeds unexpectedly, while it does invoke the error handler:

# include_error.conf
Include "error.conf"

sgielen avatar Feb 19 '13 03:02 sgielen

The test case:

#include <stdio.h>
#include <dotconf.h>
#include <string.h>
#include <stdlib.h>

static DOTCONF_CB(option);

static const configoption_t options[] = {
    {"error", ARG_RAW, option, NULL, CTX_ALL},
    LAST_OPTION
};

FUNC_ERRORHANDLER(error_handler) {
    printf("Error handler executed\n");
    return 1;
}

void do_test(const char *file) {
    printf("\nOpening %s\n", file);
    configfile_t *c = dotconf_create(strdup(file), options, NULL, CASE_INSENSITIVE);
    c->errorhandler = (dotconf_errorhandler_t) error_handler;

    int res = dotconf_command_loop(c);
    dotconf_cleanup(c);
    if(res == 0)
        printf("dotconf_command_loop() failed\n");
}

int main() {
    do_test("error.conf");
    do_test("ok.conf");
    do_test("include_error.conf");
}

static DOTCONF_CB(option) {
    // find 0 or 1 in data
    int i;
    int len = strlen(cmd->data.str);
    for(i = 0; i < len; ++i) {
        if(cmd->data.str[i] == '0') return NULL;
        if(cmd->data.str[i] == '1') return "Error found";
    }
    return "No error found";
}

sgielen avatar Feb 19 '13 03:02 sgielen

A work-around is to keep error state in the context variable, setting it to "no error" before starting to parse, and setting it to "error" in the error handler. The error handler is called for every error regardless of what file they are in, so this allows you to see an error did occur even though dotconf_command_loop returns 0.

sgielen avatar Feb 19 '13 03:02 sgielen