- 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
This commit is contained in:
okir 2003-12-03 12:07:01 +00:00
parent 8684aff59c
commit ff9a45569a
6 changed files with 58 additions and 16 deletions

View File

@ -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;

View File

@ -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);

View File

@ -28,6 +28,7 @@
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include <errno.h>
#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;
}

View File

@ -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
*/

View File

@ -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;
}

View File

@ -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;
}