summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2023-05-30 14:28:55 -0700
committerVito Caputo <vcaputo@pengaru.com>2023-05-30 14:28:55 -0700
commitdb3f0619cca328f77fd935a5a9e581ba9b1d9ff9 (patch)
tree91588332cccbd25572e1125ff2128f35e175183c
parent3b43a50c3582f8f9bcd914ac886a94502b868657 (diff)
til: make til_module_randomize_setup() always produce a setup
Preparatory for required til_setup_t @ til_module_create_context() This basically brings til_module_randomize_setup() inline with til_module_setup_finalize() in that it will still produce a minimal til_setup_t even if there is no til_module_t.setup() method. A future commit will do something about the orphaned til_settings_t within til_module_randomize_setup() to get a full path on the produced setup - which will likely simultaneously bring us into a world where one can influence the randomized settings externally as well. Influenced in the sense of potentialy making some of those settings statically configured while leaving others to be (re)randomized at the time of til_module_randomize_setup() executing.
-rw-r--r--src/til.c83
1 files changed, 46 insertions, 37 deletions
diff --git a/src/til.c b/src/til.c
index 65d1c8c..15015f6 100644
--- a/src/til.c
+++ b/src/til.c
@@ -324,20 +324,17 @@ int til_module_setup(const til_settings_t *settings, til_setting_t **res_setting
/* TODO: rename to til_module_setup_randomize() */
/* originally taken from rtv, this randomizes a module's setup @res_setup, args @res_arg
- * returns 0 on no setup, 1 on setup successful with results stored @res_*, -errno on error.
+ * returns 0 on on setup successful with results stored @res_*, -errno on error.
*/
int til_module_randomize_setup(const til_module_t *module, unsigned seed, til_setup_t **res_setup, char **res_arg)
{
til_settings_t *settings;
til_setting_t *setting;
const til_setting_desc_t *desc;
- int r = 1;
+ int r;
assert(module);
- if (!module->setup)
- return 0;
-
/* FIXME TODO:
* this seems wrong for two reasons:
* 1. there's no parent settings to attach this to, and there really shouldn't be such
@@ -355,52 +352,64 @@ int til_module_randomize_setup(const til_module_t *module, unsigned seed, til_se
if (!settings)
return -ENOMEM;
- for (setting = NULL; module->setup(settings, &setting, &desc, res_setup) > 0; setting = NULL) {
- assert(desc);
+ if (!module->setup) {
+ til_setup_t *setup;
+
+ setup = til_setup_new(settings, sizeof(*setup), NULL);
+ if (!setup)
+ r = -ENOMEM;
+ else
+ *res_setup = setup;
+ } else {
+ for (setting = NULL; module->setup(settings, &setting, &desc, res_setup) > 0; setting = NULL) {
+ assert(desc);
- if (!setting) {
- if (desc->spec.random) {
- char *value;
+ if (!setting) {
+ if (desc->spec.random) {
+ char *value;
- value = desc->spec.random(rand_r(&seed));
- setting = til_settings_add_value(desc->container, desc->spec.key, value, desc);
- free(value);
- } else if (desc->spec.values) {
- int n;
+ value = desc->spec.random(rand_r(&seed));
+ setting = til_settings_add_value(desc->container, desc->spec.key, value, desc);
+ free(value);
+ } else if (desc->spec.values) {
+ int n;
- for (n = 0; desc->spec.values[n]; n++);
+ for (n = 0; desc->spec.values[n]; n++);
- n = rand_r(&seed) % n;
+ n = rand_r(&seed) % n;
- setting = til_settings_add_value(desc->container, desc->spec.key, desc->spec.values[n], desc);
- } else {
- setting = til_settings_add_value(desc->container, desc->spec.key, desc->spec.preferred, desc);
+ setting = til_settings_add_value(desc->container, desc->spec.key, desc->spec.values[n], desc);
+ } else {
+ setting = til_settings_add_value(desc->container, desc->spec.key, desc->spec.preferred, desc);
+ }
}
- }
- assert(setting);
+ assert(setting);
- if (desc->spec.as_nested_settings && !setting->value_as_nested_settings) {
- char *label = NULL;
+ if (desc->spec.as_nested_settings && !setting->value_as_nested_settings) {
+ char *label = NULL;
- if (!desc->spec.key) {
- /* generate a positional label for bare-value specs */
- r = til_settings_label_setting(desc->container, setting, &label);
- if (r < 0)
- return r;
- }
+ if (!desc->spec.key) {
+ /* generate a positional label for bare-value specs */
+ r = til_settings_label_setting(desc->container, setting, &label);
+ if (r < 0)
+ break;
+ }
- setting->value_as_nested_settings = til_settings_new(desc->container, desc->spec.key ? : label, setting->value);
- free(label);
+ setting->value_as_nested_settings = til_settings_new(desc->container, desc->spec.key ? : label, setting->value);
+ free(label);
- if (!setting->value_as_nested_settings)
- return -ENOMEM;
- }
+ if (!setting->value_as_nested_settings) {
+ r = -ENOMEM;
+ break;
+ }
+ }
- setting->desc = desc;
+ setting->desc = desc;
+ }
}
- if (res_arg) {
+ if (res_arg && r == 0) {
char *arg;
arg = til_settings_as_arg(settings);
© All Rights Reserved