From ff9a45569adbcf2cfe964984d7f49a61c8dac526 Mon Sep 17 00:00:00 2001 From: okir Date: Wed, 3 Dec 2003 12:07:01 +0000 Subject: [PATCH] - scconf_parse and scconf_parse_string now return an error message if something went wrong git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@1655 c6295689-39f2-0310-b995-f0e70906c6a9 --- src/libopensc/ctx.c | 14 +++++++++++--- src/pkcs15init/profile.c | 7 +++++-- src/scconf/parse.c | 42 ++++++++++++++++++++++++++++++++++------ src/scconf/scconf.h | 4 ++-- src/scconf/test-conf.c | 5 +++-- src/scldap/scldap.c | 2 +- 6 files changed, 58 insertions(+), 16 deletions(-) diff --git a/src/libopensc/ctx.c b/src/libopensc/ctx.c index 6cae53fc..39593b42 100644 --- a/src/libopensc/ctx.c +++ b/src/libopensc/ctx.c @@ -333,7 +333,7 @@ void process_config_file(struct sc_context *ctx, struct _sc_ctx_options *opts) { int i, r, count = 0; scconf_block **blocks; - char *conf_path = OPENSC_CONF_PATH; + char *conf_path = OPENSC_CONF_PATH, *errmsg; #ifdef _WIN32 char temp_path[PATH_MAX]; #endif @@ -349,12 +349,20 @@ void process_config_file(struct sc_context *ctx, struct _sc_ctx_options *opts) ctx->conf = scconf_new(conf_path); if (ctx->conf == NULL) return; - r = scconf_parse(ctx->conf); + r = scconf_parse(ctx->conf, &errmsg); #ifdef OPENSC_CONFIG_STRING - if (r < 1) + /* Parse the string if config file didn't exist */ + if (r < 0) r = scconf_parse_string(ctx->conf, OPENSC_CONFIG_STRING); #endif if (r < 1) { + /* A negative return value means the config file isn't + * there, which is not an error. Nevertheless log this + * fact. */ + if (r < 0) + sc_debug(ctx, "scconf_parse failed: %s", errmsg); + else + sc_error(ctx, "scconf_parse failed: %s", errmsg); scconf_free(ctx->conf); ctx->conf = NULL; return; diff --git a/src/pkcs15init/profile.c b/src/pkcs15init/profile.c index c3a50ff2..e79c8fb6 100644 --- a/src/pkcs15init/profile.c +++ b/src/pkcs15init/profile.c @@ -279,16 +279,19 @@ int sc_profile_load(struct sc_profile *profile, const char *filename) { scconf_context *conf; + char *errmsg; int res = 0; if (!(filename = sc_profile_locate(filename))) return SC_ERROR_FILE_NOT_FOUND; conf = scconf_new(filename); - res = scconf_parse(conf); + res = scconf_parse(conf, &errmsg); if (res < 0) return SC_ERROR_FILE_NOT_FOUND; - if (res == 0) + if (res == 0) { + /* FIXME - we may want to display errmsg here. */ return SC_ERROR_SYNTAX_ERROR; + } res = process_conf(profile, conf); scconf_free(conf); diff --git a/src/scconf/parse.c b/src/scconf/parse.c index 203f0a1f..27bc1341 100644 --- a/src/scconf/parse.c +++ b/src/scconf/parse.c @@ -28,6 +28,7 @@ #ifdef HAVE_STRINGS_H #include #endif +#include #include "scconf.h" #include "internal.h" @@ -361,32 +362,61 @@ void scconf_parse_token(scconf_parser * parser, int token_type, const char *toke parser->last_token_type = token_type; } -int scconf_parse(scconf_context * config) +int scconf_parse(scconf_context * config, char **emesg) { + static char buffer[256]; scconf_parser p; + int r = 1; memset(&p, 0, sizeof(p)); p.config = config; p.block = config->root; p.line = 1; + if (emesg) + *emesg = NULL; if (!scconf_lex_parse(&p, config->filename)) { - return -1; + snprintf(buffer, sizeof(buffer), + "Unable to open \"%s\": %s", + config->filename, strerror(errno)); + r = -1; + } else if (p.error) { + strncpy(buffer, p.emesg, sizeof(buffer)-1); + r = 0; + } else { + r = 1; } - return p.error ? 0 : 1; + + if (r <= 0 && emesg) + *emesg = buffer; + return r; } -int scconf_parse_string(scconf_context * config, const char *string) +int scconf_parse_string(scconf_context * config, const char *string, char **emesg) { + static char buffer[256]; scconf_parser p; + int r; memset(&p, 0, sizeof(p)); p.config = config; p.block = config->root; p.line = 1; + if (emesg) + *emesg = NULL; if (!scconf_lex_parse_string(&p, string)) { - return -1; + snprintf(buffer, sizeof(buffer), + "Failed to parse configuration string"); + r = -1; + } else if (p.error) { + strncpy(buffer, p.emesg, sizeof(buffer)-1); + r = 0; + } else { + r = 1; } - return p.error ? 0 : 1; + + if (r <= 0 && emesg) + *emesg = buffer; + return r; } diff --git a/src/scconf/scconf.h b/src/scconf/scconf.h index 561451e8..fb37a385 100644 --- a/src/scconf/scconf.h +++ b/src/scconf/scconf.h @@ -98,12 +98,12 @@ extern void scconf_free(scconf_context * config); /* Parse configuration * Returns 1 = ok, 0 = error, -1 = error opening config file */ -extern int scconf_parse(scconf_context * config); +extern int scconf_parse(scconf_context * config, char **errmsg); /* Parse a static configuration string * Returns 1 = ok, 0 = error */ -extern int scconf_parse_string(scconf_context * config, const char *string); +extern int scconf_parse_string(scconf_context * config, const char *string, char **errmsg); /* Parse entries */ diff --git a/src/scconf/test-conf.c b/src/scconf/test-conf.c index dfd1625e..219e157b 100644 --- a/src/scconf/test-conf.c +++ b/src/scconf/test-conf.c @@ -121,6 +121,7 @@ int main(int argc, char **argv) {NULL} }; char *in = NULL, *out = NULL; + char *errmsg; int r; if (argc != 3) { @@ -135,8 +136,8 @@ int main(int argc, char **argv) printf("scconf_new failed\n"); return 1; } - if (scconf_parse(conf) < 1) { - printf("scconf_parse failed\n"); + if (scconf_parse(conf, &errmsg) < 1) { + printf("scconf_parse failed: %s\n", errmsg); scconf_free(conf); return 1; } diff --git a/src/scldap/scldap.c b/src/scldap/scldap.c index 1e6e44f3..65462cdc 100644 --- a/src/scldap/scldap.c +++ b/src/scldap/scldap.c @@ -173,7 +173,7 @@ scldap_context *scldap_parse_parameters(const char *filename) scldap_free_parameters(ctx); return NULL; } - if (scconf_parse(ctx->conf) < 1) { + if (scconf_parse(ctx->conf, NULL) < 1) { scldap_free_parameters(ctx); return NULL; }