Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
options: now that options are simple name/value tuples make them kvlist_t
Baptiste Daroussin committed 22 days ago
commit a25ae6485ef9eb3f260eeed0158c2dce7a4006d6
parent 903337f
11 files changed +51 -73
modified libpkg/pkg.c
@@ -406,11 +406,29 @@ pkg_each(dirs, struct pkg_dir, dirs);
pkg_each(files, struct pkg_file, files);
pkg_each(deps, struct pkg_dep, depends);
pkg_each(rdeps, struct pkg_dep, rdepends);
-
pkg_each(options, struct pkg_option, options);
pkg_each(conflicts, struct pkg_conflict, conflicts);
pkg_each(config_files, struct pkg_config_file, config_files);

int
+
pkg_options(const struct pkg *p, struct pkg_kv **kv)
+
{
+
	assert(p != NULL);
+
	if (p->options.len == 0)
+
		return (EPKG_END);
+
	if (*kv == NULL) {
+
		*kv = p->options.d[0];
+
		return (EPKG_OK);
+
	}
+
	vec_foreach(p->options, i) {
+
		if (p->options.d[i] == *kv && i + 1 < p->options.len) {
+
			*kv = p->options.d[i + 1];
+
			return (EPKG_OK);
+
		}
+
	}
+
	return (EPKG_END);
+
}
+

+
int
pkg_adduser(struct pkg *pkg, const char *name)
{
	return (pkg_addstring(&pkg->users, name, "user"));
@@ -785,33 +803,26 @@ pkg_appendscript(struct pkg *pkg, const char *cmd, pkg_script type)
int
pkg_addoption(struct pkg *pkg, const char *key, const char *value)
{
-
	struct pkg_option	*o = NULL;
+
	struct pkg_kv *kv;

	assert(pkg != NULL);
	assert(key != NULL && key[0] != '\0');
	assert(value != NULL && value[0] != '\0');

-
	/* There might be a default or description for the option
-
	   already, so we only count it as a duplicate if the value
-
	   field is already set. Which implies there could be a
-
	   default value or description for an option but no actual
-
	   value. */
-

	dbg(2,"adding options: %s = %s", key, value);
-
	if (pkghash_get(pkg->optionshash, key) != NULL) {
+
	kv = pkg_kv_new(key, value);
+
	if (pkg_kv_insert_sorted(&pkg->options, kv) != NULL) {
+
		pkg_kv_free(kv);
		if (ctx.developer_mode) {
-
			pkg_emit_error("duplicate options listing: %s, fatal (developer mode)", key);
+
			pkg_emit_error("duplicate options listing: %s, "
+
			    "fatal (developer mode)", key);
			return (EPKG_FATAL);
		} else {
-
			pkg_emit_error("duplicate options listing: %s, ignoring", key);
+
			pkg_emit_error("duplicate options listing: %s, "
+
			    "ignoring", key);
			return (EPKG_OK);
		}
	}
-
	o = xcalloc(1, sizeof(*o));
-
	o->key = xstrdup(key);
-
	o->value = xstrdup(value);
-
	pkghash_safe_add(pkg->optionshash, o->key, o, NULL);
-
	DL_APPEND(pkg->options, o);

	return (EPKG_OK);
}
@@ -1064,7 +1075,7 @@ pkg_list_count(const struct pkg *pkg, pkg_list list)
	case PKG_RDEPS:
		return (pkghash_count(pkg->rdepshash));
	case PKG_OPTIONS:
-
		return (pkghash_count(pkg->optionshash));
+
		return (vec_len(&pkg->options));
	case PKG_FILES:
		return (pkghash_count(pkg->filehash));
	case PKG_DIRS:
@@ -1113,9 +1124,7 @@ pkg_list_free(struct pkg *pkg, pkg_list list) {
		pkg->flags &= ~PKG_LOAD_RDEPS;
		break;
	case PKG_OPTIONS:
-
		DL_FREE(pkg->options, pkg_option_free);
-
		pkghash_destroy(pkg->optionshash);
-
		pkg->optionshash = NULL;
+
		vec_free_and_free(&pkg->options, pkg_kv_free);
		pkg->flags &= ~PKG_LOAD_OPTIONS;
		break;
	case PKG_FILES:
@@ -1997,12 +2006,6 @@ pkg_dir_cmp(struct pkg_dir *a, struct pkg_dir *b)
}

static int
-
pkg_option_cmp(struct pkg_option *a, struct pkg_option *b)
-
{
-
	return (strcmp(a->key, b->key));
-
}
-

-
static int
pkg_cf_cmp(struct pkg_config_file *a, struct pkg_config_file *b)
{
	return (strcmp(a->path, b->path));
@@ -2018,7 +2021,7 @@ pkg_lists_sort(struct pkg *p)
	DL_SORT(p->depends, pkg_dep_cmp);
	DL_SORT(p->files, pkg_file_cmp);
	DL_SORT(p->dirs, pkg_dir_cmp);
-
	DL_SORT(p->options, pkg_option_cmp);
+
	pkg_kv_sort(&p->options);
	DL_SORT(p->config_files, pkg_cf_cmp);
}

modified libpkg/pkg.h.in
@@ -108,7 +108,6 @@ struct pkg_dep;
struct pkg_conflict;
struct pkg_file;
struct pkg_dir;
-
struct pkg_option;
struct pkg_license;
struct pkg_config_file;
struct pkg_create;
@@ -702,7 +701,7 @@ int pkg_dirs(const struct pkg *pkg, struct pkg_dir **dir);
 * @param  option Must be set to NULL for the first call.
 * @return An error code.
 */
-
int pkg_options(const struct pkg *, struct pkg_option **option);
+
int pkg_options(const struct pkg *, struct pkg_kv **option);

/**
 * Iterates over the conflicts registered in the package.
modified libpkg/pkg_attributes.c
@@ -96,20 +96,6 @@ pkg_script_get(struct pkg const * const p, pkg_script i)
}

/*
-
 * Option
-
 */
-
void
-
pkg_option_free(struct pkg_option *option)
-
{
-
	if (option == NULL)
-
		return;
-

-
	free(option->key);
-
	free(option->value);
-
	free(option);
-
}
-

-
/*
 * Conflicts
 */
void
modified libpkg/pkg_checksum.c
@@ -205,7 +205,6 @@ pkg_checksum_generate(struct pkg *pkg, char *dest, size_t destlen,
	size_t blen;
	struct kv *entries = NULL;
	charv_t tofree = vec_init();
-
	struct pkg_option *option = NULL;
	struct pkg_dep *dep = NULL;
	struct pkg_file *f = NULL;
	bool is_group = false;
@@ -226,8 +225,9 @@ pkg_checksum_generate(struct pkg *pkg, char *dest, size_t destlen,

	LL_APPEND(entries, kv_new("vital", pkg->vital ? "1" : "0"));

-
	while (pkg_options(pkg, &option) == EPKG_OK) {
-
		LL_APPEND(entries, kv_new(option->key, option->value));
+
	vec_foreach(pkg->options, oi) {
+
		LL_APPEND(entries, kv_new(pkg->options.d[oi]->key,
+
		    pkg->options.d[oi]->value));
	}

	vec_foreach(pkg->shlibs_required, i) {
modified libpkg/pkg_jobs.c
@@ -1001,7 +1001,7 @@ bool
pkg_jobs_need_upgrade(charv_t *system_shlibs, struct pkg *rp, struct pkg *lp)
{
	int ret, ret1, ret2;
-
	struct pkg_option *lo = NULL, *ro = NULL;
+
	struct pkg_kv *lo = NULL, *ro = NULL;
	struct pkg_dep *ld = NULL, *rd = NULL;
	struct pkg_conflict *lc = NULL, *rc = NULL;

modified libpkg/pkg_manifest.c
@@ -936,7 +936,6 @@ pkg_emit_object(struct pkg *pkg, short flags)
{
	struct pkg_kv		*kv;
	struct pkg_dep		*dep      = NULL;
-
	struct pkg_option	*option   = NULL;
	struct pkg_file		*file     = NULL;
	struct pkg_dir		*dir      = NULL;
	struct pkg_conflict	*conflict = NULL;
@@ -1132,13 +1131,14 @@ pkg_emit_object(struct pkg *pkg, short flags)

	dbg(4, "Emitting options");
	map = NULL;
-
	while (pkg_options(pkg, &option) == EPKG_OK) {
-
		dbg(4, "Emitting option: %s", option->value);
+
	vec_foreach(pkg->options, oi) {
+
		struct pkg_kv *opt = pkg->options.d[oi];
+
		dbg(4, "Emitting option: %s", opt->value);
		if (map == NULL)
			map = ucl_object_typed_new(UCL_OBJECT);
		ucl_object_insert_key(map,
-
		    ucl_object_fromstring(option->value),
-
		    option->key, 0, false);
+
		    ucl_object_fromstring(opt->value),
+
		    opt->key, 0, false);
	}
	if (map)
		ucl_object_insert_key(top, map, "options", 7, false);
modified libpkg/pkg_printf.c
@@ -100,10 +100,8 @@
 * N  pkg          Reponame
 *
 * O  pkg          List of options
-
 * On pkg_option   Option name (key)
-
 * Ov pkg_option   Option value
-
 * Od pkg_option   Option default value (if known)
-
 * OD pkg_option   Option description
+
 * On pkg_kv       Option name (key)
+
 * Ov pkg_kv       Option value
 *
 * P pkg
 * Q
@@ -1451,7 +1449,6 @@ format_options(xstring *buf, const void *data, struct percent_esc *p)
	if (p->flags & (PP_ALTERNATE_FORM1|PP_ALTERNATE_FORM2))
		return (list_count(buf, pkg_list_count(pkg, PKG_OPTIONS), p));
	else {
-
		struct pkg_option	*opt = NULL;
		int			 count;

		set_list_defaults(p, "%On %Ov\n", "");
@@ -1459,13 +1456,13 @@ format_options(xstring *buf, const void *data, struct percent_esc *p)
		count = 1;
		fflush(p->sep_fmt->fp);
		fflush(p->item_fmt->fp);
-
		while (pkg_options(pkg, &opt) == EPKG_OK) {
+
		vec_foreach(pkg->options, i) {
			if (count > 1)
				iterate_item(buf, pkg, p->sep_fmt->buf,
-
					     opt, count, PP_O);
+
					     pkg->options.d[i], count, PP_O);

			iterate_item(buf, pkg, p->item_fmt->buf,
-
				     opt, count, PP_O);
+
				     pkg->options.d[i], count, PP_O);
			count++;
		}
	}
@@ -1478,7 +1475,7 @@ format_options(xstring *buf, const void *data, struct percent_esc *p)
xstring *
format_option_name(xstring *buf, const void *data, struct percent_esc *p)
{
-
	const struct pkg_option	*option = data;
+
	const struct pkg_kv	*option = data;

	return (string_val(buf, option == NULL ? NULL: option->key, p));
}
@@ -1489,7 +1486,7 @@ format_option_name(xstring *buf, const void *data, struct percent_esc *p)
xstring *
format_option_value(xstring *buf, const void *data, struct percent_esc *p)
{
-
	const struct pkg_option	*option = data;
+
	const struct pkg_kv	*option = data;

	return (string_val(buf, option == NULL ? NULL: option->value, p));
}
modified libpkg/pkgdb.c
@@ -1736,7 +1736,7 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int forced,
	struct pkg_dep		*dep = NULL;
	struct pkg_file		*file = NULL;
	struct pkg_dir		*dir = NULL;
-
	struct pkg_option	*option = NULL;
+
	struct pkg_kv		*option = NULL;
	struct pkg_conflict	*conflict = NULL;
	struct pkg_config_file	*cf = NULL;
	struct pkgdb_it		*it = NULL;
modified libpkg/private/pkg.h
@@ -224,8 +224,7 @@ struct pkg {
	struct pkg_file		*files;
	pkghash			*dirhash;
	struct pkg_dir		*dirs;
-
	pkghash			*optionshash;
-
	struct pkg_option	*options;
+
	kvlist_t		 options;
	charv_t		 users;
	charv_t		 groups;
	charv_t		 shlibs_required;
@@ -410,11 +409,6 @@ struct pkg_dir {
	struct pkg_dir	*next, *prev;
};

-
struct pkg_option {
-
	char	*key;
-
	char	*value;
-
	struct pkg_option *next, *prev;
-
};

struct http_mirror;

@@ -739,7 +733,6 @@ DEFINE_VEC_INSERT_SORTED_PROTO(kvlist_t, pkg_kv, struct pkg_kv *);
void pkg_dep_free(struct pkg_dep *);
void pkg_file_free(struct pkg_file *);
void pkg_dir_free(struct pkg_dir *);
-
void pkg_option_free(struct pkg_option *);
void pkg_conflict_free(struct pkg_conflict *);
void pkg_config_file_free(struct pkg_config_file *);

modified libpkg/repo/binary/update.c
@@ -139,7 +139,7 @@ pkg_repo_binary_add_pkg(struct pkg *pkg, sqlite3 *sqlite, bool forced)
{
	int			 ret;
	struct pkg_dep		*dep      = NULL;
-
	struct pkg_option	*option   = NULL;
+
	struct pkg_kv		*option   = NULL;
	struct pkg_kv		*kv;
	const char		*arch;
	int64_t			 package_id;
modified src/query.c
@@ -405,7 +405,7 @@ print_query(struct pkg *pkg, char *qstr, char multiline)
{
	xstring			*output;
	struct pkg_dep		*dep    = NULL;
-
	struct pkg_option	*option = NULL;
+
	struct pkg_kv		*option = NULL;
	struct pkg_file		*file   = NULL;
	struct pkg_dir		*dir    = NULL;
	const char		*str = NULL;