diff --git a/src/libopensc/ctx.c b/src/libopensc/ctx.c index effdaaf8..4e28674f 100644 --- a/src/libopensc/ctx.c +++ b/src/libopensc/ctx.c @@ -150,13 +150,12 @@ static int load_parameters(struct sc_context *ctx, const char *app, scconf_block **blocks = NULL; int i, err = 0; - blocks = scconf_find_blocks(ctx->conf, NULL, "app"); + blocks = scconf_find_blocks(ctx->conf, NULL, "app", NULL); for (i = 0; blocks[i]; i++) { - scconf_block *block = blocks[i]; + const scconf_block *block = blocks[i]; const scconf_list *list; - const char *val; - const char *s_internal = "internal"; - + const char *s_internal = "internal", *val; + if (!block->name && strcmp(block->name->data, app)) continue; val = scconf_find_value_first(block, "debug"); diff --git a/src/scconf/scconf.c b/src/scconf/scconf.c index e6e620c2..f1e827e0 100644 --- a/src/scconf/scconf.c +++ b/src/scconf/scconf.c @@ -60,40 +60,48 @@ void scconf_deinit(scconf_context * config) config = NULL; } -scconf_block *scconf_find_block(scconf_context * config, scconf_block * block, - const char *key) +const scconf_block *scconf_find_block(scconf_context * config, const scconf_block * block, const char *item_name) { scconf_item *item; if (!block) { block = config->root; } + if (!item_name) { + return NULL; + } for (item = block->items; item; item = item->next) { if (item->type == SCCONF_ITEM_TYPE_BLOCK && - strcasecmp(key, item->key) == 0) { + strcasecmp(item_name, item->key) == 0) { return item->value.block; } } return NULL; } -scconf_block **scconf_find_blocks(scconf_context * config, scconf_block * block, - const char *key) +scconf_block **scconf_find_blocks(scconf_context * config, const scconf_block * block, const char *item_name, const char *key) { scconf_block **blocks = NULL; - scconf_item *item; int alloc_size, size; + scconf_item *item; if (!block) { block = config->root; } + if (!item_name) { + return NULL; + } size = 0; alloc_size = 10; blocks = realloc(blocks, sizeof(scconf_block *) * alloc_size); for (item = block->items; item; item = item->next) { if (item->type == SCCONF_ITEM_TYPE_BLOCK && - strcasecmp(key, item->key) == 0) { + strcasecmp(item_name, item->key) == 0) { + if (key && item->value.block->name) { + if (strcasecmp(key, item->value.block->name->data)) + continue; + } if (size + 1 >= alloc_size) { alloc_size *= 2; blocks = realloc(blocks, sizeof(scconf_block *) * alloc_size); @@ -105,24 +113,30 @@ scconf_block **scconf_find_blocks(scconf_context * config, scconf_block * block, return blocks; } -scconf_list *scconf_find_value(scconf_block * block, const char *key) +const scconf_list *scconf_find_value(const scconf_block * block, const char *option) { scconf_item *item; + if (!block) { + return NULL; + } for (item = block->items; item; item = item->next) { if (item->type == SCCONF_ITEM_TYPE_VALUE && - strcasecmp(key, item->key) == 0) { + strcasecmp(option, item->key) == 0) { return item->value.list; } } return NULL; } -char *scconf_find_value_first(scconf_block * block, const char *key) +const char *scconf_find_value_first(const scconf_block * block, const char *option) { - scconf_list *list; + const scconf_list *list; - list = scconf_find_value(block, key); + if (!block) { + return NULL; + } + list = scconf_find_value(block, option); return !list ? NULL : list->data; } @@ -182,7 +196,7 @@ void scconf_block_destroy(scconf_block * block) } } -int scconf_list_array_length(scconf_list * list) +int scconf_list_array_length(const scconf_list * list) { int len = 0; @@ -190,11 +204,10 @@ int scconf_list_array_length(scconf_list * list) len++; list = list->next; } - return len; } -int scconf_list_strings_length(scconf_list * list) +int scconf_list_strings_length(const scconf_list * list) { int len = 0; @@ -202,15 +215,17 @@ int scconf_list_strings_length(scconf_list * list) len += strlen(list->data) + 1; list = list->next; } - return len; } -char *scconf_list_strdup(scconf_list * list, const char *filler) +char *scconf_list_strdup(const scconf_list * list, const char *filler) { char *buf = NULL; int len = 0; + if (!list) { + return NULL; + } len = scconf_list_strings_length(list); if (filler) { len += scconf_list_array_length(list) * (strlen(filler) + 1); diff --git a/src/scconf/scconf.h b/src/scconf/scconf.h index 89f4d62c..beac07be 100644 --- a/src/scconf/scconf.h +++ b/src/scconf/scconf.h @@ -61,38 +61,65 @@ typedef struct { scconf_block *root; } scconf_context; +/* Init configuration + * The filename can be NULL + */ extern scconf_context *scconf_init(const char *filename); + +/* Free configuration + */ extern void scconf_deinit(scconf_context * config); -/* Returns 1 = ok, 0 = error, -1 = error opening config file */ +/* Parse configuration + * Returns 1 = ok, 0 = error, -1 = error opening config file + */ extern int scconf_parse(scconf_context * config); -/* Write config to file. - * if filename is NULL, use the config->filename. +/* Write config to a file + * If the filename is NULL, use the config->filename * Returns 0 = ok, else = errno */ extern int scconf_write(scconf_context * config, const char *filename); -/* Find config block by key. If block is NULL, the root block is used. */ -extern scconf_block *scconf_find_block(scconf_context * config, scconf_block * block, const char *key); -extern scconf_block **scconf_find_blocks(scconf_context * config, scconf_block * block, const char *key); +/* Find a config by the item_name + * If the block is NULL, the root block is used + */ +extern const scconf_block *scconf_find_block(scconf_context * config, const scconf_block * block, const char *item_name); -/* Find value from block */ -extern scconf_list *scconf_find_value(scconf_block * block, const char *key); -extern char *scconf_find_value_first(scconf_block * block, const char *key); +/* Find a config by the item_name + * If the block is NULL, the root block is used + * The key can be used to specify what the blocks first name should be. + */ +extern scconf_block **scconf_find_blocks(scconf_context * config, const scconf_block * block, const char *item_name, const char *key); -/* Free list/block structure */ +/* Get a list of values for option + */ +extern const scconf_list *scconf_find_value(const scconf_block * block, const char *option); + +/* Return the first value of the option + */ +extern const char *scconf_find_value_first(const scconf_block * block, const char *option); + +/* Free list structure + */ extern void scconf_list_destroy(scconf_list * list); + +/* Free block structure + */ extern void scconf_block_destroy(scconf_block * block); -/* Return the length of an list array */ -extern int scconf_list_array_length(scconf_list * list); +/* Return the length of an list array + */ +extern int scconf_list_array_length(const scconf_list * list); -/* Return the combined length of the strings on all arrays */ -extern int scconf_list_strings_length(scconf_list * list); +/* Return the combined length of the strings on all arrays + */ +extern int scconf_list_strings_length(const scconf_list * list); -/* Return allocated string that contains all strings in a list */ -extern char *scconf_list_strdup(scconf_list * list, const char *filler); +/* Return an allocated string that contains all + * the strings in a list separated by the filler + */ +extern char *scconf_list_strdup(const scconf_list * list, const char *filler); #ifdef __cplusplus } diff --git a/src/scconf/test-conf.c b/src/scconf/test-conf.c index 1d50e6e5..0e5e4b13 100644 --- a/src/scconf/test-conf.c +++ b/src/scconf/test-conf.c @@ -33,10 +33,10 @@ void print_ldap_block(scconf_context * conf, scconf_block * block) scconf_block **blocks = NULL; unsigned int i; - blocks = scconf_find_blocks(conf, block, "ldap"); + blocks = scconf_find_blocks(conf, block, "ldap", NULL); for (i = 0; blocks[i]; i++) { - scconf_block *block = blocks[i]; - scconf_list *list, *tmp; + const scconf_block *block = blocks[i]; + const scconf_list *list, *tmp; printf("LDAP entry[%s]\n", !block->name ? "Default" : block->name->data); printf("ldaphost: %s\n", scconf_find_value_first(block, "ldaphost")); diff --git a/src/scldap/scldap.c b/src/scldap/scldap.c index 90012750..e1558072 100644 --- a/src/scldap/scldap.c +++ b/src/scldap/scldap.c @@ -48,11 +48,11 @@ static void scldap_parse_block(scldap_context * ctx, scconf_block * block, const char fallback[32]; unsigned int i; - blocks = scconf_find_blocks(ctx->conf, block, "ldap"); + blocks = scconf_find_blocks(ctx->conf, block, "ldap", NULL); for (i = 0; blocks[i]; i++) { - scconf_block *block = blocks[i]; - scconf_list *list = NULL, *tmp = NULL; - char *val = NULL; + const scconf_block *block = blocks[i]; + const scconf_list *list = NULL, *tmp = NULL; + const char *val = NULL; size_t len = 0; if (ctx->entries >= SCLDAP_MAX_ENTRIES) @@ -142,7 +142,7 @@ scldap_context *scldap_parse_parameters(const char *filename) scldap_parse_block(ctx, NULL, NULL); /* Parse card specific LDAP blocks */ - blocks = scconf_find_blocks(ctx->conf, NULL, "card"); + blocks = scconf_find_blocks(ctx->conf, NULL, "card", NULL); for (i = 0; blocks[i]; i++) { scconf_block *block = blocks[i]; char *name = NULL;