* libuci - Library for the Unified Configuration Interface
* Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
*
- * this program is free software; you can redistribute it and/or modify
- * it under the terms of the gnu lesser general public license version 2.1
- * as published by the free software foundation
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1
+ * as published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- */
-
-/*
- * This file contains the code for parsing uci config files
+ * GNU Lesser General Public License for more details.
*/
-#include <ctype.h>
+#include <string.h>
+#include <stdint.h>
-#define LINEBUF 128
-#define LINEBUF_MAX 4096
+#include "uci.h"
-/*
- * Fetch a new line from the input stream and resize buffer if necessary
- */
-static void uci_getln(struct uci_context *ctx)
+void uci_parse_section(struct uci_section *s, const struct uci_parse_option *opts,
+ int n_opts, struct uci_option **tb)
{
- struct uci_parse_context *pctx = ctx->pctx;
- char *p;
- int ofs;
+ struct uci_element *e;
- if (pctx->buf == NULL) {
- pctx->buf = uci_malloc(ctx, LINEBUF);
- pctx->bufsz = LINEBUF;
- }
-
- ofs = 0;
- do {
- p = &pctx->buf[ofs];
- p[ofs] = 0;
-
- p = fgets(p, pctx->bufsz - ofs, pctx->file);
- if (!p || !p[ofs])
- return;
-
- ofs += strlen(p);
- if (pctx->buf[ofs - 1] == '\n') {
- pctx->line++;
- pctx->buf[ofs - 1] = 0;
- return;
- }
+ memset(tb, 0, n_opts * sizeof(*tb));
- if (pctx->bufsz > LINEBUF_MAX/2) {
- pctx->byte = LINEBUF_MAX;
- UCI_THROW(ctx, UCI_ERR_PARSE);
- }
+ uci_foreach_element(&s->options, e) {
+ struct uci_option *o = uci_to_option(e);
+ int i;
- pctx->bufsz *= 2;
- pctx->buf = uci_realloc(ctx, pctx->buf, pctx->bufsz);
- } while (1);
-}
+ for (i = 0; i < n_opts; i++) {
+ if (tb[i])
+ continue;
-/*
- * Clean up all extra memory used by the parser
- */
-static void uci_parse_cleanup(struct uci_context *ctx)
-{
- struct uci_parse_context *pctx;
-
- pctx = ctx->pctx;
- if (!pctx)
- return;
-
- if (pctx->buf)
- free(pctx->buf);
- if (pctx->file)
- fclose(pctx->file);
-
- free(pctx);
-}
+ if (strcmp(opts[i].name, o->e.name) != 0)
+ continue;
-static void skip_whitespace(char **str)
-{
- while (**str && isspace(**str))
- *str += 1;
-}
+ if (opts[i].type != o->type)
+ continue;
-static char *parse_double_quote(char **str)
-{
- char *val;
-
- *str += 1;
- val = *str;
- while (**str) {
-
- /* skip escaped characters */
- if (**str == '\\') {
- *str += 2;
- continue;
- }
-
- /* check for the end of the quoted string */
- if (**str == '"') {
- **str = 0;
- *str += 1;
- return val;
+ /* match found */
+ tb[i] = o;
+ break;
}
- *str += 1;
}
-
- return NULL;
}
-static char *parse_single_quote(char **str)
-{
- /* TODO: implement */
- return NULL;
-}
+//-----------------------------------------------------------------------------
+// MurmurHashNeutral2, by Austin Appleby
-static char *parse_unquoted(char **str)
+// Same as MurmurHash2, but endian- and alignment-neutral.
+static uint32_t hash_murmur2(uint32_t h, const void * key, int len)
{
- char *val;
-
- val = *str;
+ const unsigned char * data = key;
+ const uint32_t m = 0x5bd1e995;
+ const int r = 24;
- while (**str && !isspace(**str))
- *str += 1;
- **str = 0;
- *str += 1;
+ while(len >= 4)
+ {
+ unsigned int k;
- return val;
-}
+ k = data[0];
+ k |= data[1] << 8;
+ k |= data[2] << 16;
+ k |= data[3] << 24;
-static char *next_arg(char **str)
-{
- skip_whitespace(str);
-
- switch (**str) {
- case '"':
- return parse_double_quote(str);
- case '\'':
- return parse_single_quote(str);
- case 0:
- return NULL;
- default:
- return parse_unquoted(str);
- }
-}
+ k *= m;
+ k ^= k >> r;
+ k *= m;
-static void uci_parse_config(struct uci_context *ctx, char **str)
-{
- char *type, *name;
-
- *str += strlen(*str) + 1;
-
- if (!*str) {
- ctx->pctx->byte = *str - ctx->pctx->buf;
- UCI_THROW(ctx, UCI_ERR_PARSE);
- }
+ h *= m;
+ h ^= k;
- type = next_arg(str);
- if (!type) {
- ctx->pctx->byte = *str - ctx->pctx->buf;
- UCI_THROW(ctx, UCI_ERR_PARSE);
+ data += 4;
+ len -= 4;
}
-
- name = next_arg(str);
- DPRINTF("Section<%s>: %s\n", type, name);
-}
-static void uci_parse_option(struct uci_context *ctx, char **str)
-{
- char *name, *value;
-
- *str += strlen(*str) + 1;
-
- name = next_arg(str);
- if (!name) {
- ctx->pctx->byte = *str - ctx->pctx->buf;
- UCI_THROW(ctx, UCI_ERR_PARSE);
- }
+ switch(len)
+ {
+ case 3: h ^= data[2] << 16;
+ case 2: h ^= data[1] << 8;
+ case 1: h ^= data[0];
+ h *= m;
+ };
- value = next_arg(str);
- if (!value) {
- ctx->pctx->byte = *str - ctx->pctx->buf;
- UCI_THROW(ctx, UCI_ERR_PARSE);
- }
+ h ^= h >> 13;
+ h *= m;
+ h ^= h >> 15;
- DPRINTF("\tOption: %s=\"%s\"\n", name, value);
+ return h;
}
-static void uci_parse_line(struct uci_context *ctx)
+static uint32_t uci_hash_list(uint32_t h, const struct uci_list *list)
{
- struct uci_parse_context *pctx = ctx->pctx;
- char *word, *brk;
-
- for (word = strtok_r(pctx->buf, ";", &brk);
- word;
- word = strtok_r(NULL, ";", &brk)) {
-
- char *pbrk;
- word = strtok_r(word, " \t", &pbrk);
-
- switch(word[0]) {
- case 'c':
- if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
- uci_parse_config(ctx, &word);
- break;
- case 'o':
- if ((word[1] == 0) || !strcmp(word + 1, "ption"))
- uci_parse_option(ctx, &word);
- break;
- default:
- pctx->byte = word - pctx->buf;
- UCI_THROW(ctx, UCI_ERR_PARSE);
- break;
- }
+ const struct uci_element *e;
+
+ uci_foreach_element(list, e) {
+ h = hash_murmur2(h, e->name, strlen(e->name) + 1);
}
+ return h;
}
-int uci_parse(struct uci_context *ctx, const char *name)
+uint32_t uci_hash_options(struct uci_option **tb, int n_opts)
{
- struct uci_parse_context *pctx;
-
- UCI_HANDLE_ERR(ctx);
- UCI_ASSERT(ctx, name != NULL);
-
- /* make sure no memory from previous parse attempts is leaked */
- uci_parse_cleanup(ctx);
-
- pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
- ctx->pctx = pctx;
-
- /* TODO: use /etc/config/ */
- pctx->file = fopen(name, "r");
- if (!pctx->file)
- UCI_THROW(ctx, UCI_ERR_NOTFOUND);
-
- while (!feof(pctx->file)) {
- uci_getln(ctx);
- if (*(pctx->buf))
- uci_parse_line(ctx);
- }
+ uint32_t h = 0xdeadc0de;
+ int i;
- /* if no error happened, we can get rid of the parser context now */
- uci_parse_cleanup(ctx);
+ for (i = 0; i < n_opts; i++) {
+ const struct uci_option *o = tb[i];
+
+ if (!tb[i])
+ continue;
+
+ h = hash_murmur2(h, o->e.name, strlen(o->e.name) + 1);
+ h = hash_murmur2(h, &o->type, sizeof(o->type));
+
+ switch (tb[i]->type) {
+ case UCI_TYPE_STRING:
+ h = hash_murmur2(h, o->v.string, strlen(o->v.string) + 1);
+ break;
+ case UCI_TYPE_LIST:
+ h = uci_hash_list(h, &o->v.list);
+ break;
+ }
+ }
- return 0;
+ return h;
}