From d66fac1ce6945cd9678282056da4d2f4fabb4adc Mon Sep 17 00:00:00 2001 From: bapt Date: Sat, 24 May 2014 23:46:41 +0000 Subject: merge libucl 20140514 this version brings xpath-like interface for ucl objects --- contrib/libucl/src/ucl_internal.h | 2 + contrib/libucl/src/ucl_parser.c | 55 ++++++++++++++++++++--- contrib/libucl/src/ucl_util.c | 95 +++++++++++++++++++++++++++++++++------ 3 files changed, 133 insertions(+), 19 deletions(-) (limited to 'contrib/libucl/src') diff --git a/contrib/libucl/src/ucl_internal.h b/contrib/libucl/src/ucl_internal.h index 9a35dce..0e3ecd0 100644 --- a/contrib/libucl/src/ucl_internal.h +++ b/contrib/libucl/src/ucl_internal.h @@ -197,6 +197,8 @@ struct ucl_parser { struct ucl_chunk *chunks; struct ucl_pubkey *keys; struct ucl_variable *variables; + ucl_variable_handler var_handler; + void *var_data; UT_string *err; }; diff --git a/contrib/libucl/src/ucl_parser.c b/contrib/libucl/src/ucl_parser.c index d5a085e..b4fe5af 100644 --- a/contrib/libucl/src/ucl_parser.c +++ b/contrib/libucl/src/ucl_parser.c @@ -236,6 +236,9 @@ ucl_check_variable_safe (struct ucl_parser *parser, const char *ptr, size_t rema size_t *out_len, bool strict, bool *found) { struct ucl_variable *var; + unsigned char *dst; + size_t dstlen; + bool need_free = false; LL_FOREACH (parser->variables, var) { if (strict) { @@ -258,6 +261,19 @@ ucl_check_variable_safe (struct ucl_parser *parser, const char *ptr, size_t rema } } + /* XXX: can only handle ${VAR} */ + if (!(*found) && parser->var_handler != NULL && strict) { + /* Call generic handler */ + if (parser->var_handler (ptr, remain, &dst, &dstlen, &need_free, + parser->var_data)) { + *found = true; + if (need_free) { + free (dst); + } + return (ptr + remain); + } + } + return ptr; } @@ -271,7 +287,8 @@ ucl_check_variable_safe (struct ucl_parser *parser, const char *ptr, size_t rema * @return */ static const char * -ucl_check_variable (struct ucl_parser *parser, const char *ptr, size_t remain, size_t *out_len, bool *vars_found) +ucl_check_variable (struct ucl_parser *parser, const char *ptr, + size_t remain, size_t *out_len, bool *vars_found) { const char *p, *end, *ret = ptr; bool found = false; @@ -282,7 +299,8 @@ ucl_check_variable (struct ucl_parser *parser, const char *ptr, size_t remain, s end = ptr + remain; while (p < end) { if (*p == '}') { - ret = ucl_check_variable_safe (parser, ptr + 1, p - ptr - 1, out_len, true, &found); + ret = ucl_check_variable_safe (parser, ptr + 1, p - ptr - 1, + out_len, true, &found); if (found) { /* {} must be excluded actually */ ret ++; @@ -328,10 +346,13 @@ static const char * ucl_expand_single_variable (struct ucl_parser *parser, const char *ptr, size_t remain, unsigned char **dest) { - unsigned char *d = *dest; + unsigned char *d = *dest, *dst; const char *p = ptr + 1, *ret; struct ucl_variable *var; + size_t dstlen; + bool need_free = false; bool found = false; + bool strict = false; ret = ptr + 1; remain --; @@ -343,6 +364,7 @@ ucl_expand_single_variable (struct ucl_parser *parser, const char *ptr, } else if (*p == '{') { p ++; + strict = true; ret += 2; remain -= 2; } @@ -359,9 +381,22 @@ ucl_expand_single_variable (struct ucl_parser *parser, const char *ptr, } } if (!found) { - memcpy (d, ptr, 2); - d += 2; - ret --; + if (strict && parser->var_handler != NULL) { + if (parser->var_handler (ptr, remain, &dst, &dstlen, &need_free, + parser->var_data)) { + memcpy (d, dst, dstlen); + ret += dstlen; + d += remain; + found = true; + } + } + + /* Leave variable as is */ + if (!found) { + memcpy (d, ptr, 2); + d += 2; + ret --; + } } *dest = d; @@ -1873,6 +1908,14 @@ ucl_parser_register_variable (struct ucl_parser *parser, const char *var, } } +void +ucl_parser_set_variables_handler (struct ucl_parser *parser, + ucl_variable_handler handler, void *ud) +{ + parser->var_handler = handler; + parser->var_data = ud; +} + bool ucl_parser_add_chunk (struct ucl_parser *parser, const unsigned char *data, size_t len) diff --git a/contrib/libucl/src/ucl_util.c b/contrib/libucl/src/ucl_util.c index 9178795..63f5e62 100644 --- a/contrib/libucl/src/ucl_util.c +++ b/contrib/libucl/src/ucl_util.c @@ -1330,20 +1330,10 @@ ucl_object_find_keyl (const ucl_object_t *obj, const char *key, size_t klen) const ucl_object_t * ucl_object_find_key (const ucl_object_t *obj, const char *key) { - size_t klen; - const ucl_object_t *ret; - ucl_object_t srch; - - if (obj == NULL || obj->type != UCL_OBJECT || key == NULL) { + if (key == NULL) return NULL; - } - - klen = strlen (key); - srch.key = key; - srch.keylen = klen; - ret = ucl_hash_search_obj (obj->value.ov, &srch); - return ret; + return ucl_object_find_keyl (obj, key, strlen(key)); } const ucl_object_t* @@ -1396,6 +1386,58 @@ ucl_iterate_object (const ucl_object_t *obj, ucl_object_iter_t *iter, bool expan return NULL; } +const ucl_object_t * +ucl_lookup_path (const ucl_object_t *top, const char *path_in) { + const ucl_object_t *o = NULL, *found; + const char *p, *c; + char *err_str; + unsigned index; + + if (path_in == NULL || top == NULL) { + return NULL; + } + + found = NULL; + p = path_in; + + /* Skip leading dots */ + while (*p == '.') { + p ++; + } + + c = p; + while (*p != '\0') { + p ++; + if (*p == '.' || *p == '\0') { + if (p > c) { + switch (top->type) { + case UCL_ARRAY: + /* Key should be an int */ + index = strtoul (c, &err_str, 10); + if (err_str != NULL && (*err_str != '.' && *err_str != '\0')) { + return NULL; + } + o = ucl_array_find_index (top, index); + break; + default: + o = ucl_object_find_keyl (top, c, p - c); + break; + } + if (o == NULL) { + return NULL; + } + top = o; + } + if (*p != '\0') { + c = p + 1; + } + } + } + found = o; + + return found; +} + ucl_object_t * ucl_object_new (void) @@ -1411,7 +1453,7 @@ ucl_object_new (void) } ucl_object_t * -ucl_object_typed_new (unsigned int type) +ucl_object_typed_new (ucl_type_t type) { ucl_object_t *new; new = malloc (sizeof (ucl_object_t)); @@ -1423,6 +1465,12 @@ ucl_object_typed_new (unsigned int type) return new; } +ucl_type_t +ucl_object_type (const ucl_object_t *obj) +{ + return obj->type; +} + ucl_object_t* ucl_object_fromstring (const char *str) { @@ -1591,6 +1639,27 @@ ucl_array_pop_first (ucl_object_t *top) return ucl_array_delete (top, __DECONST(ucl_object_t *, ucl_array_head (top))); } +const ucl_object_t * +ucl_array_find_index (const ucl_object_t *top, unsigned int index) +{ + ucl_object_iter_t it = NULL; + const ucl_object_t *ret; + + if (top == NULL || top->type != UCL_ARRAY || top->len == 0 || + (index + 1) > top->len) { + return NULL; + } + + while ((ret = ucl_iterate_object (top, &it, true)) != NULL) { + if (index == 0) { + return ret; + } + --index; + } + + return NULL; +} + ucl_object_t * ucl_elt_append (ucl_object_t *head, ucl_object_t *elt) { -- cgit v1.1