diff options
Diffstat (limited to 'src/lookup.c')
-rw-r--r-- | src/lookup.c | 252 |
1 files changed, 126 insertions, 126 deletions
diff --git a/src/lookup.c b/src/lookup.c index 7cb95df..8c0df3f 100644 --- a/src/lookup.c +++ b/src/lookup.c @@ -1,4 +1,4 @@ -/* grecs - Gray's Extensible Configuration System +/* argot - Gray's Extensible Configuration System Copyright (C) 2007-2016 Sergey Poznyakoff Grecs is free software; you can redistribute it and/or modify it @@ -20,16 +20,16 @@ #include <string.h> #include <errno.h> #include <ctype.h> -#include "grecs.h" +#include "argot.h" #include "wordsplit.h" #include <fnmatch.h> #include <stdlib.h> static int -_grecs_list_eq(struct grecs_value *a, struct grecs_value *b) +_argot_list_eq(struct argot_value *a, struct argot_value *b) { - struct grecs_list_entry *aent, *bent; - if (grecs_list_size(a->v.list) != grecs_list_size(b->v.list)) + struct argot_list_entry *aent, *bent; + if (argot_list_size(a->v.list) != argot_list_size(b->v.list)) return 0; for (aent = a->v.list->head, bent = b->v.list->head;; @@ -38,7 +38,7 @@ _grecs_list_eq(struct grecs_value *a, struct grecs_value *b) return bent == NULL; if (!bent) return 0; - if (!grecs_value_eq(aent->data, bent->data)) + if (!argot_value_eq(aent->data, bent->data)) return 0; } /*notreached*/ @@ -46,7 +46,7 @@ _grecs_list_eq(struct grecs_value *a, struct grecs_value *b) } static int -_grecs_array_eq(struct grecs_value *a, struct grecs_value *b) +_argot_array_eq(struct argot_value *a, struct argot_value *b) { size_t i; @@ -54,39 +54,39 @@ _grecs_array_eq(struct grecs_value *a, struct grecs_value *b) return 0; for (i = 0; i < a->v.arg.c; i++) - if (!grecs_value_eq(a->v.arg.v[i], b->v.arg.v[i])) + if (!argot_value_eq(a->v.arg.v[i], b->v.arg.v[i])) return 0; return 1; } /* Return 1 if configuration value A equals B */ int -grecs_value_eq(struct grecs_value *a, struct grecs_value *b) +argot_value_eq(struct argot_value *a, struct argot_value *b) { if (a == 0 || b == 0) return a == b; if (a->type != b->type) return 0; switch (a->type) { - case GRECS_TYPE_STRING: + case ARGOT_TYPE_STRING: if (a->v.string == NULL) return b->v.string == NULL; return strcmp(a->v.string, b->v.string) == 0; - case GRECS_TYPE_LIST: - return _grecs_list_eq(a, b); + case ARGOT_TYPE_LIST: + return _argot_list_eq(a, b); - case GRECS_TYPE_ARRAY: - return _grecs_array_eq(a, b); + case ARGOT_TYPE_ARRAY: + return _argot_array_eq(a, b); } return 0; } static int -_grecs_list_match(struct grecs_value *pat, struct grecs_value *b, int flags) +_argot_list_match(struct argot_value *pat, struct argot_value *b, int flags) { - struct grecs_list_entry *aent, *bent; - if (grecs_list_size(pat->v.list) != grecs_list_size(b->v.list)) + struct argot_list_entry *aent, *bent; + if (argot_list_size(pat->v.list) != argot_list_size(b->v.list)) return 0; for (aent = pat->v.list->head, bent = b->v.list->head;; @@ -95,7 +95,7 @@ _grecs_list_match(struct grecs_value *pat, struct grecs_value *b, int flags) return bent == NULL; if (!bent) return 0; - if (!grecs_value_match(aent->data, bent->data, flags)) + if (!argot_value_match(aent->data, bent->data, flags)) return 0; } /*notreached*/ @@ -103,7 +103,7 @@ _grecs_list_match(struct grecs_value *pat, struct grecs_value *b, int flags) } static int -_grecs_array_match(struct grecs_value *pat, struct grecs_value *b, int flags) +_argot_array_match(struct argot_value *pat, struct argot_value *b, int flags) { size_t i; @@ -111,61 +111,61 @@ _grecs_array_match(struct grecs_value *pat, struct grecs_value *b, int flags) return 0; for (i = 0; i < pat->v.arg.c; i++) - if (!grecs_value_match(pat->v.arg.v[i], b->v.arg.v[i], flags)) + if (!argot_value_match(pat->v.arg.v[i], b->v.arg.v[i], flags)) return 0; return 1; } int -grecs_value_match(struct grecs_value *pat, struct grecs_value *b, int flags) +argot_value_match(struct argot_value *pat, struct argot_value *b, int flags) { if (pat == 0 || b == 0) return pat == b; if (pat->type != b->type) { - if (pat->type != GRECS_TYPE_STRING) + if (pat->type != ARGOT_TYPE_STRING) return 0; switch (b->type) { - case GRECS_TYPE_LIST: - b = grecs_list_index(b->v.list, 0); + case ARGOT_TYPE_LIST: + b = argot_list_index(b->v.list, 0); break; - case GRECS_TYPE_ARRAY: + case ARGOT_TYPE_ARRAY: b = b->v.arg.v[0]; } } switch (pat->type) { - case GRECS_TYPE_STRING: + case ARGOT_TYPE_STRING: if (pat->v.string == NULL) return b->v.string == NULL; return fnmatch(pat->v.string, b->v.string, flags) == 0; - case GRECS_TYPE_LIST: - return _grecs_list_match(pat, b, flags); + case ARGOT_TYPE_LIST: + return _argot_list_match(pat, b, flags); - case GRECS_TYPE_ARRAY: - return _grecs_array_match(pat, b, flags); + case ARGOT_TYPE_ARRAY: + return _argot_array_match(pat, b, flags); } return 0; } -struct grecs_match_buf { +struct argot_match_buf { int argc; /* number of path components */ char **argv; /* array of path components */ int argi; /* Index of the current component */ - struct grecs_value **labelv; /* Component labels */ - struct grecs_node *root; /* Root node */ - struct grecs_node *node; /* Last found node */ + struct argot_value **labelv; /* Component labels */ + struct argot_node *root; /* Root node */ + struct argot_node *node; /* Last found node */ }; #define ISWC(c,w) ((c)[0] == (w) && (c)[1] == 0) -grecs_match_buf_t -grecs_match_buf_create(int argc, char **argv, struct grecs_value **labelv) +argot_match_buf_t +argot_match_buf_create(int argc, char **argv, struct argot_value **labelv) { int i; - struct grecs_match_buf *buf = grecs_zalloc(sizeof(*buf)); + struct argot_match_buf *buf = argot_zalloc(sizeof(*buf)); buf->argc = argc; buf->argv = argv; @@ -179,7 +179,7 @@ grecs_match_buf_create(int argc, char **argv, struct grecs_value **labelv) for (j = i + 1; j < buf->argc && ISWC(buf->argv[j], '*'); j++) { free(buf->argv[j]); - grecs_value_free_content(buf->labelv[i]); + argot_value_free_content(buf->labelv[i]); } j -= i; if (j > 1) { @@ -198,63 +198,63 @@ grecs_match_buf_create(int argc, char **argv, struct grecs_value **labelv) } size_t -grecs_match_buf_get_args(grecs_match_buf_t buf, char ***argv) +argot_match_buf_get_args(argot_match_buf_t buf, char ***argv) { if (argv) *argv = buf->argv; return buf->argc; } -struct grecs_node * -grecs_match_buf_get_node(grecs_match_buf_t buf) +struct argot_node * +argot_match_buf_get_node(argot_match_buf_t buf) { return buf->node; } -struct grecs_node * -grecs_match_buf_get_root(grecs_match_buf_t buf) +struct argot_node * +argot_match_buf_get_root(argot_match_buf_t buf) { return buf->root; } void -grecs_match_buf_set_root(grecs_match_buf_t buf, struct grecs_node *root) +argot_match_buf_set_root(argot_match_buf_t buf, struct argot_node *root) { buf->root = root; } static void -grecs_match_buf_free_contents(struct grecs_match_buf *buf) +argot_match_buf_free_contents(struct argot_match_buf *buf) { size_t i; for (i = 0; i < buf->argc; i++) { free(buf->argv[i]); - grecs_value_free(buf->labelv[i]); + argot_value_free(buf->labelv[i]); } free(buf->argv); free(buf->labelv); } void -grecs_match_buf_free(struct grecs_match_buf *buf) +argot_match_buf_free(struct argot_match_buf *buf) { if (buf) { - grecs_match_buf_free_contents(buf); + argot_match_buf_free_contents(buf); free(buf); } } -static struct grecs_value * +static struct argot_value * parse_label(const char *str) { - struct grecs_value *val = NULL; + struct argot_value *val = NULL; size_t i; struct wordsplit ws; size_t len = strlen (str); if (len > 1 && str[0] == '(' && str[len-1] == ')') { - struct grecs_list *lst; + struct argot_list *lst; ws.ws_delim = ","; if (wordsplit_len (str + 1, len - 2, &ws, @@ -263,32 +263,32 @@ parse_label(const char *str) return NULL; } - lst = grecs_value_list_create(); + lst = argot_value_list_create(); for (i = 0; i < ws.ws_wordc; i++) { - struct grecs_value *p = grecs_zalloc(sizeof(*p)); - p->type = GRECS_TYPE_STRING; + struct argot_value *p = argot_zalloc(sizeof(*p)); + p->type = ARGOT_TYPE_STRING; p->v.string = ws.ws_wordv[i]; - grecs_list_append(lst, p); + argot_list_append(lst, p); } - val = grecs_malloc(sizeof(*val)); - val->type = GRECS_TYPE_LIST; + val = argot_malloc(sizeof(*val)); + val->type = ARGOT_TYPE_LIST; val->v.list = lst; } else { if (wordsplit(str, &ws, WRDSF_DEFFLAGS)) return NULL; - val = grecs_zalloc(sizeof(*val)); + val = argot_zalloc(sizeof(*val)); if (ws.ws_wordc == 1) { - val->type = GRECS_TYPE_STRING; + val->type = ARGOT_TYPE_STRING; val->v.string = ws.ws_wordv[0]; } else { - val->type = GRECS_TYPE_ARRAY; + val->type = ARGOT_TYPE_ARRAY; val->v.arg.c = ws.ws_wordc; - val->v.arg.v = grecs_calloc(ws.ws_wordc, + val->v.arg.v = argot_calloc(ws.ws_wordc, sizeof(val->v.arg.v[0])); for (i = 0; i < ws.ws_wordc; i++) { val->v.arg.v[i] = - grecs_zalloc(sizeof(*val->v.arg.v[0])); - val->v.arg.v[i]->type = GRECS_TYPE_STRING; + argot_zalloc(sizeof(*val->v.arg.v[0])); + val->v.arg.v[i]->type = ARGOT_TYPE_STRING; val->v.arg.v[i]->v.string = ws.ws_wordv[i]; } } @@ -300,7 +300,7 @@ parse_label(const char *str) static int split_cfg_path(const char *path, int *pargc, char ***pargv, - grecs_value_t ***pvalv) + argot_value_t ***pvalv) { int argc; char **argv; @@ -344,9 +344,9 @@ split_cfg_path(const char *path, int *pargc, char ***pargv, *pargc = argc; if (pvalv) { int i; - grecs_value_t **valv; + argot_value_t **valv; - valv = grecs_calloc(argc, sizeof(valv[0])); + valv = argot_calloc(argc, sizeof(valv[0])); for (i = 0; i < argc; i++) { char *p = strchr(argv[i], '='); if (p) { @@ -360,50 +360,50 @@ split_cfg_path(const char *path, int *pargc, char ***pargv, } -enum grecs_tree_recurse_res -grecs_node_exact_match(enum grecs_tree_recurse_op op, - struct grecs_node *node, void *data) +enum argot_tree_recurse_res +argot_node_exact_match(enum argot_tree_recurse_op op, + struct argot_node *node, void *data) { int match = 0; - struct grecs_match_buf *buf = data; + struct argot_match_buf *buf = data; - if (node->type == grecs_node_root) - return grecs_tree_recurse_ok; - if (op == grecs_tree_recurse_post) { + if (node->type == argot_node_root) + return argot_tree_recurse_ok; + if (op == argot_tree_recurse_post) { if (buf->argi == 0) - return grecs_tree_recurse_stop; + return argot_tree_recurse_stop; --buf->argi; - return grecs_tree_recurse_ok; + return argot_tree_recurse_ok; } if (strcmp(buf->argv[buf->argi], node->ident) == 0 && (!buf->labelv[buf->argi] || - grecs_value_eq(buf->labelv[buf->argi], node->v.value))) { + argot_value_eq(buf->labelv[buf->argi], node->v.value))) { if (buf->argi + 1 == buf->argc) { buf->node = node; - return grecs_tree_recurse_stop; + return argot_tree_recurse_stop; } match = 1; } if (match) { - if (op == grecs_tree_recurse_pre) { + if (op == argot_tree_recurse_pre) { if (buf->argi + 1 == buf->argc) - return grecs_tree_recurse_skip; + return argot_tree_recurse_skip; buf->argi++; } - return grecs_tree_recurse_ok; + return argot_tree_recurse_ok; } - return node->type == grecs_node_block ? - grecs_tree_recurse_skip : grecs_tree_recurse_ok; + return node->type == argot_node_block ? + argot_tree_recurse_skip : argot_tree_recurse_ok; } -struct grecs_node * -grecs_find_node(struct grecs_node *node, const char *path) +struct argot_node * +argot_find_node(struct argot_node *node, const char *path) { int rc; - struct grecs_match_buf buf; + struct argot_match_buf buf; if (strcmp(path, ".") == 0) return node; @@ -412,18 +412,18 @@ grecs_find_node(struct grecs_node *node, const char *path) return NULL; buf.argi = 0; buf.node = NULL; - grecs_tree_recurse(node, grecs_node_exact_match, &buf); - grecs_match_buf_free_contents(&buf); + argot_tree_recurse(node, argot_node_exact_match, &buf); + argot_match_buf_free_contents(&buf); return buf.node; } static void -fixup_loci(struct grecs_node *node, - grecs_locus_t const *plocus, - struct grecs_locus_point const *endp) +fixup_loci(struct argot_node *node, + argot_locus_t const *plocus, + struct argot_locus_point const *endp) { - grecs_locus_t loc = *plocus; + argot_locus_t loc = *plocus; for (; node; node = node->down) { node->idloc = loc; @@ -434,24 +434,24 @@ fixup_loci(struct grecs_node *node, } } -struct grecs_node * -grecs_node_from_path_locus(const char *path, const char *value, - grecs_locus_t *plocus, grecs_locus_t *vallocus) +struct argot_node * +argot_node_from_path_locus(const char *path, const char *value, + argot_locus_t *plocus, argot_locus_t *vallocus) { int rc; int i; int argc; char **argv; - struct grecs_node *dn = NULL; + struct argot_node *dn = NULL; rc = split_cfg_path(path, &argc, &argv, NULL); if (rc) return NULL; - dn = grecs_node_create(grecs_node_stmt, NULL); + dn = argot_node_create(argot_node_stmt, NULL); dn->ident = argv[argc - 1]; if (value) { - struct grecs_value *gval = parse_label(value); + struct argot_value *gval = parse_label(value); if (vallocus) gval->locus = *vallocus; dn->v.value = gval; @@ -459,8 +459,8 @@ grecs_node_from_path_locus(const char *path, const char *value, dn->v.value = NULL; for (i = argc - 2; i >= 0; i--) { - struct grecs_value *label = NULL; - struct grecs_node *node; + struct argot_value *label = NULL; + struct argot_node *node; char *p, *q = argv[i]; do { @@ -475,7 +475,7 @@ grecs_node_from_path_locus(const char *path, const char *value, break; } while (*q); - node = grecs_node_create(grecs_node_block, plocus); + node = argot_node_create(argot_node_block, plocus); node->ident = argv[i]; if (label) node->v.value = label; @@ -494,23 +494,23 @@ grecs_node_from_path_locus(const char *path, const char *value, return dn; } -struct grecs_node * -grecs_node_from_path(const char *path, const char *value) +struct argot_node * +argot_node_from_path(const char *path, const char *value) { - return grecs_node_from_path_locus(path, value, NULL, NULL); + return argot_node_from_path_locus(path, value, NULL, NULL); } static int -is_root(struct grecs_match_buf *buf, struct grecs_node *node) +is_root(struct argot_match_buf *buf, struct argot_node *node) { - return (buf->root == node || node->type == grecs_node_root); + return (buf->root == node || node->type == argot_node_root); } static int -grecs_match(struct grecs_match_buf *buf) +argot_match(struct argot_match_buf *buf) { - struct grecs_node *node; + struct argot_node *node; int wcard = 0; buf->argi = buf->argc - 1; @@ -528,7 +528,7 @@ grecs_match(struct grecs_match_buf *buf) strcmp(buf->argv[buf->argi], node->ident) == 0) /* FIXME: */ && (!buf->labelv[buf->argi] || - grecs_value_match(buf->labelv[buf->argi], + argot_value_match(buf->labelv[buf->argi], node->v.value, 0))) { wcard = 0; node = node->up; @@ -544,56 +544,56 @@ grecs_match(struct grecs_match_buf *buf) return 0; } -struct grecs_node * -grecs_match_next(struct grecs_match_buf *buf) +struct argot_node * +argot_match_next(struct argot_match_buf *buf) { if (!buf) return NULL; - while ((buf->node = grecs_next_node(buf->node))) - if (grecs_match(buf)) + while ((buf->node = argot_next_node(buf->node))) + if (argot_match(buf)) break; return buf->node; } -struct grecs_node * -grecs_match_buf_first(struct grecs_match_buf *buf, struct grecs_node *tree) +struct argot_node * +argot_match_buf_first(struct argot_match_buf *buf, struct argot_node *tree) { - struct grecs_node *node; + struct argot_node *node; buf->argi = 0; buf->root = tree; - buf->node = grecs_tree_first_node(tree); + buf->node = argot_tree_first_node(tree); if (!buf->node) return NULL; - if (grecs_match(buf)) + if (argot_match(buf)) node = buf->node; else - node = grecs_match_next(buf); + node = argot_match_next(buf); return node; } -struct grecs_node * -grecs_match_first(struct grecs_node *tree, const char *pattern, - struct grecs_match_buf **pbuf) +struct argot_node * +argot_match_first(struct argot_node *tree, const char *pattern, + struct argot_match_buf **pbuf) { - struct grecs_node *node; - struct grecs_match_buf *buf; + struct argot_node *node; + struct argot_match_buf *buf; if (strcmp(pattern, ".") == 0) { *pbuf = NULL; return tree; } - buf = grecs_zalloc(sizeof(*buf)); + buf = argot_zalloc(sizeof(*buf)); if (split_cfg_path(pattern, &buf->argc, &buf->argv, &buf->labelv)) { free(buf); return NULL; } - node = grecs_match_buf_first(buf, tree); + node = argot_match_buf_first(buf, tree); if (node) *pbuf = buf; else { - grecs_match_buf_free(buf); + argot_match_buf_free(buf); *pbuf = NULL; } return node; |