Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Convert the plugin configuration parser to libucl
Baptiste Daroussin committed 12 years ago
commit 9bd5449a5b13119c16ad4004e75aaaaa0bb44fed
parent f6986b4
3 files changed +27 -182
modified libpkg/pkg_config.c
@@ -345,49 +345,6 @@ connect_evpipe(const char *evpipe) {
}

static void
-
parse_config_sequence(yaml_document_t *doc, yaml_node_t *seq, struct pkg_config *conf)
-
{
-
	yaml_node_item_t *item = seq->data.sequence.items.start;
-
	yaml_node_t *val;
-
	struct pkg_config_value *v;
-

-
	while (item < seq->data.sequence.items.top) {
-
		val = yaml_document_get_node(doc, *item);
-
		if (val->type != YAML_SCALAR_NODE) {
-
			++item;
-
			continue;
-
		}
-
		v = malloc(sizeof(struct pkg_config_value));
-
		v->value = strdup(val->data.scalar.value);
-
		HASH_ADD_STR(conf->list, value, v);
-
		++item;
-
	}
-
}
-

-
static void
-
parse_config_mapping(yaml_document_t *doc, yaml_node_t *map, struct pkg_config *conf)
-
{
-
	yaml_node_pair_t *subpair = map->data.mapping.pairs.start;
-
	yaml_node_t *subkey, *subval;
-
	struct pkg_config_kv *kv;
-

-
	while (subpair < map->data.mapping.pairs.top) {
-
		subkey = yaml_document_get_node(doc, subpair->key);
-
		subval = yaml_document_get_node(doc, subpair->value);
-
		if (subkey->type != YAML_SCALAR_NODE ||
-
		    subval->type != YAML_SCALAR_NODE) {
-
			++subpair;
-
			continue;
-
		}
-
		kv = malloc(sizeof(struct pkg_config_kv));
-
		kv->key = strdup(subkey->data.scalar.value);
-
		kv->value = strdup(subval->data.scalar.value);
-
		HASH_ADD_STR(conf->kvlist, value, kv);
-
		++subpair;
-
	}
-
}
-

-
static void
obj_walk_array(ucl_object_t *obj, struct pkg_config *conf)
{
	struct pkg_config_value *v;
@@ -420,7 +377,7 @@ obj_walk_object(ucl_object_t *obj, struct pkg_config *conf)
	}
}

-
static void
+
void
pkg_object_walk(ucl_object_t *obj, struct pkg_config *conf_by_key)
{
	ucl_object_t *sub, *tmp;
@@ -492,117 +449,6 @@ pkg_object_walk(ucl_object_t *obj, struct pkg_config *conf_by_key)
	sbuf_delete(b);
}

-
void
-
pkg_config_parse(yaml_document_t *doc, yaml_node_t *node, struct pkg_config *conf_by_key)
-
{
-
	struct pkg_config *conf;
-
	yaml_node_pair_t *pair;
-
	const char *errstr = NULL;
-
	int64_t newint;
-
	struct sbuf *b = sbuf_new_auto();
-

-
	pair = node->data.mapping.pairs.start;
-
	while (pair < node->data.mapping.pairs.top) {
-
		yaml_node_t *key = yaml_document_get_node(doc, pair->key);
-
		yaml_node_t *val = yaml_document_get_node(doc, pair->value);
-

-
		if (key->data.scalar.length <= 0) {
-
			/*
-
			 * ignoring silently empty keys can be empty lines or user mistakes
-
			 */
-
			++pair;
-
			continue;
-
		}
-

-
		if (val->type == YAML_NO_NODE ||
-
		    (val->type == YAML_SCALAR_NODE &&
-
		     val->data.scalar.length <= 0)) {
-
			/*
-
			 * silently skip on purpose to allow user to leave
-
			 * empty lines for examples without complaining
-
			 */
-
			++pair;
-
			continue;
-
		}
-
		sbuf_clear(b);
-
		for (size_t i = 0; i < strlen(key->data.scalar.value); ++i)
-
			sbuf_putc(b, toupper(key->data.scalar.value[i]));
-

-
		sbuf_finish(b);
-
		HASH_FIND(hhkey, conf_by_key, sbuf_data(b), (size_t)sbuf_len(b), conf);
-
		if (conf != NULL) {
-
			switch (conf->type) {
-
			case PKG_CONFIG_STRING:
-
				if (val->type != YAML_SCALAR_NODE) {
-
					pkg_emit_error("Expecting a string for key %s,"
-
					    " ignoring...", key->data.scalar.value);
-
				}
-
				/* ignore if already set from env */
-
				if (!conf->fromenv) {
-
					free(conf->string);
-
					conf->string = strdup(val->data.scalar.value);
-
				}
-
				break;
-
			case PKG_CONFIG_INTEGER:
-
				if (val->type != YAML_SCALAR_NODE) {
-
					pkg_emit_error("Expecting an integer for key %s,"
-
					    " ignoring...", key->data.scalar.value);
-
				}
-
				/* ignore if already set from env */
-
				if (!conf->fromenv) {
-
					newint = strtonum(val->data.scalar.value, 0, INT64_MAX, &errstr);
-
					if (errstr != NULL) {
-
						pkg_emit_error("Expecting an integer for key %s"
-
						    " ignoring...", key->data.scalar.value);
-
					}
-
					conf->integer = newint;
-
				}
-
				break;
-
			case PKG_CONFIG_BOOL:
-
				if (val->type != YAML_SCALAR_NODE) {
-
					pkg_emit_error("Expecting an integer for key %s,"
-
					    " ignoring...", key->data.scalar.value);
-
				}
-
				/* ignore if already set from env */
-
				if (!conf->fromenv) {
-
					if (val->data.scalar.value != NULL && (
-
					    strcmp(val->data.scalar.value, "1") == 0 ||
-
					    strcasecmp(val->data.scalar.value, "yes") == 0 ||
-
					    strcasecmp(val->data.scalar.value, "true") == 0 ||
-
					    strcasecmp(val->data.scalar.value, "on") == 0)) {
-
						conf->boolean = true;
-
					} else {
-
						conf->boolean = false;
-
					}
-
				}
-
				break;
-
			case PKG_CONFIG_KVLIST:
-
				if (val->type != YAML_MAPPING_NODE) {
-
					pkg_emit_error("Expecting a key/value list for key %s,"
-
					    " ignoring...", key->data.scalar.value);
-
				}
-
				parse_config_mapping(doc, val, conf);
-
				break;
-
			case PKG_CONFIG_LIST:
-
				if (!conf->fromenv) {
-
					if (val->type != YAML_SEQUENCE_NODE) {
-
						pkg_emit_error("Expecting a string list for key %s,"
-
						    " ignoring...", key->data.scalar.value);
-
					}
-
					parse_config_sequence(doc, val, conf);
-
				}
-
				break;
-
			}
-
		}
-
		/*
-
		 * unknown values are just silently ignored, because we don't
-
		 * care about them
-
		 */
-
		++pair;
-
	}
-
	sbuf_delete(b);
-
}
-

static char *
subst_packagesite_str(const char *oldstr)
{
@@ -1215,6 +1061,9 @@ parsed:
	disable_plugins_if_static();

	parsed = true;
+
	ucl_obj_free(obj);
+
	ucl_parser_free(p);
+

	pkg_debug(1, "%s", "pkg initialized");

	subst_packagesite();
modified libpkg/plugins.c
@@ -39,6 +39,7 @@
#include <string.h>
#include <assert.h>
#include <unistd.h>
+
#include <ucl.h>

#include "pkg.h"
#include "private/pkg.h"
@@ -536,45 +537,39 @@ pkg_plugin_parse(struct pkg_plugin *p)
	char confpath[MAXPATHLEN];
	const char *path;
	const char *plugname;
-
	FILE *fp;
+
	struct ucl_parser *pr;
+
	ucl_object_t *obj;
+
	UT_string *err;

-
	yaml_parser_t parser;
-
	yaml_document_t doc;
-
	yaml_node_t *node;
+
	pr = ucl_parser_new(0);

	pkg_config_string(PKG_CONFIG_PLUGINS_CONF_DIR, &path);
	plugname = pkg_plugin_get(p, PKG_PLUGIN_NAME);

	snprintf(confpath, sizeof(confpath), "%s/%s.conf", path, plugname);

-
	if ((fp = fopen(confpath, "r")) == NULL) {
-
		if (errno != ENOENT) {
-
			pkg_emit_errno("fopen", confpath);
-
			return (EPKG_FATAL);
+
	if (!ucl_parser_add_file(pr, confpath, &err)) {
+
		if (errno == ENOENT) {
+
			ucl_parser_free(pr);
+
			p->parsed = true;
+
			return (EPKG_OK);
		}
-
		p->parsed = true;
-
		return (EPKG_OK);
-
	}
+
		pkg_emit_error("%s\n", utstring_body(err));
+
		utstring_free(err);
+
		err = NULL;
+
		ucl_parser_free(pr);

-
	yaml_parser_initialize(&parser);
-
	yaml_parser_set_input_file(&parser, fp);
-
	yaml_parser_load(&parser, &doc);
-

-
	node = yaml_document_get_root_node(&doc);
-
	if (node != NULL) {
-
		if (node->type != YAML_MAPPING_NODE) {
-
			pkg_emit_error("Invalid configuration format, ignoring the configuration file");
-
		} else {
-
			pkg_config_parse(&doc, node, p->conf_by_key);
-
		}
+
		return (EPKG_FATAL);
	}

-
	yaml_document_delete(&doc);
-
	yaml_parser_delete(&parser);
-

-
	fclose(fp);
+
	obj = ucl_parser_get_object(pr, &err);
+
	if (obj->type == UCL_OBJECT)
+
		pkg_object_walk(obj->value.ov, p->conf_by_key);

	p->parsed = true;
+
	ucl_obj_free(obj);
+
	ucl_parser_free(pr);
+

	return (EPKG_OK);
}

modified libpkg/private/pkg.h
@@ -40,6 +40,7 @@
#include <stdbool.h>
#include <uthash.h>
#include <utlist.h>
+
#include <ucl.h>

#include <yaml.h>
#include "private/utils.h"
@@ -401,7 +402,7 @@ int pkgdb_register_finale(struct pkgdb *db, int retcode);

int pkg_register_shlibs(struct pkg *pkg, const char *root);

-
void pkg_config_parse(yaml_document_t *doc, yaml_node_t *node, struct pkg_config *conf_by_key);
+
void pkg_object_walk(ucl_object_t *o, struct pkg_config *conf_by_key);

int pkg_emit_manifest_sbuf(struct pkg*, struct sbuf *, short, char **);
int pkg_emit_filelist(struct pkg *, FILE *);