raw = toml_raw_in(toml, opt->name);
if (raw == NULL)
- return pr_err("TOML boolean '%s' was not found.", opt->name);
+ return 0;
if (toml_rtob(raw, &value) == -1)
return pr_err("Cannot parse '%s' as a boolean.", raw);
char *string;
int error;
- string = NULL;
- error = parse_toml_string(opt, toml, &string);
+ error = parse_toml_string(toml, opt->name, &string);
if (error)
return error;
+ if (string == NULL)
+ return 0;
error = parse_argv_filename_format(opt, string, _result);
#include "log.h"
#include "config/str.h"
+static void
+__free_out_file(struct config_out_file *file)
+{
+ if (file->fd != NULL) {
+ fclose(file->fd);
+ file->fd = NULL;
+ }
+
+ free(file->file_name);
+ file->file_name = NULL;
+}
+
static void
print_out_file(struct group_fields const *group,
struct option_field const *field, void *value)
struct config_out_file *file = _result;
int error;
- field->type->free(file);
+ __free_out_file(file);
file->file_name = strdup(file_name);
if (file->file_name == NULL)
char *file_name;
int error;
- file_name = NULL;
- error = parse_toml_string(opt, toml, &file_name);
+ error = parse_toml_string(toml, opt->name, &file_name);
if (error)
return error;
+ if (file_name == NULL)
+ return 0;
error = parse_argv_out_file(opt, file_name, _result);
}
static void
-free_out_file(void *_file)
+free_out_file(void *file)
{
- struct config_out_file *file = _file;
-
- if (file->fd != NULL) {
- fclose(file->fd);
- file->fd = NULL;
- }
-
- free(file->file_name);
- file->file_name = NULL;
+ __free_out_file(file);
}
const struct global_type gt_out_file = {
#include "log.h"
static void
-print_string(struct group_fields const *group, struct option_field const *field,
+__string_free(char **string)
+{
+ free(*string);
+ *string = NULL;
+}
+
+static void
+string_print(struct group_fields const *group, struct option_field const *field,
void *value)
{
pr_info("%s.%s: %s", group->name, field->name, *((char **) value));
}
static int
-parse_argv_string(struct option_field const *field, char const *str,
+string_parse_argv(struct option_field const *field, char const *str,
void *_result)
{
char **result = _result;
- /* Remove the previous value (usually the default). */
- field->type->free(result);
-
if (field->type->has_arg != required_argument || str == NULL) {
return pr_err("String options ('%s' in this case) require an argument.",
field->name);
}
+ /* Remove the previous value (usually the default). */
+ __string_free(result);
+
/* tomlc99 frees @str early, so work with a copy. */
*result = strdup(str);
return ((*result) != NULL) ? 0 : pr_enomem();
}
-int
-parse_toml_string(struct option_field const *opt, struct toml_table_t *toml,
+static int
+string_parse_toml(struct option_field const *opt, struct toml_table_t *toml,
void *_result)
{
- const char *raw;
- char *value;
+ char *tmp;
char **result;
+ int error;
- /* Remove the previous value (usually the default). */
- opt->type->free(_result);
-
- raw = toml_raw_in(toml, opt->name);
- if (raw == NULL)
- return pr_err("TOML string '%s' was not found.", opt->name);
- if (toml_rtos(raw, &value) == -1)
- return pr_err("Cannot parse '%s' as a string.", raw);
+ error = parse_toml_string(toml, opt->name, &tmp);
+ if (error)
+ return error;
+ if (tmp == NULL)
+ return 0;
result = _result;
- *result = value;
+ __string_free(result);
+ *result = tmp;
return 0;
}
static void
-free_string(void *_string)
+string_free(void *string)
{
- char **string = _string;
- free(*string);
- *string = NULL;
+ __string_free(string);
}
const struct global_type gt_string = {
.has_arg = required_argument,
.size = sizeof(char *),
- .print = print_string,
- .parse.argv = parse_argv_string,
- .parse.toml = parse_toml_string,
- .free = free_string,
+ .print = string_print,
+ .parse.argv = string_parse_argv,
+ .parse.toml = string_parse_toml,
+ .free = string_free,
.arg_doc = "<string>",
};
+
+int
+parse_toml_string(struct toml_table_t *toml, char const *name, char **result)
+{
+ const char *raw;
+ char *value;
+
+ raw = toml_raw_in(toml, name);
+ if (raw == NULL) {
+ *result = NULL;
+ return 0;
+ }
+ if (toml_rtos(raw, &value) == -1)
+ return pr_err("Cannot parse '%s' as a string.", raw);
+
+ *result = value;
+ return 0;
+}
extern const struct global_type gt_string;
-int parse_toml_string(struct option_field const *, struct toml_table_t *,
- void *);
+int parse_toml_string(struct toml_table_t *, char const *, char **);
#endif /* SRC_CONFIG_STR_H_ */
free(array->array);
}
+static void
+__string_array_free(struct string_array *array)
+{
+ string_array_cleanup(array);
+ array->array = NULL;
+ array->length = 0;
+}
+
static void
string_array_print(struct group_fields const *group,
struct option_field const *field, void *_value)
struct string_array *result = _result;
int error;
- /* Remove the previous value (usually the default). */
- opt->type->free(_result);
-
array = toml_array_in(toml, opt->name);
if (array == NULL)
- return pr_err("TOML array '%s' was not found.", opt->name);
+ return 0;
array_len = toml_array_nelem(array);
+ /* Remove the previous value (usually the default). */
+ __string_array_free(result);
+
result->array = malloc(array_len * sizeof(char *));
if (result->array == NULL)
return pr_enomem();
}
static void
-string_array_free(void *_array)
+string_array_free(void *array)
{
- struct string_array *array = _array;
-
- string_array_cleanup(array);
-
- array->array = NULL;
- array->length = 0;
+ __string_array_free(array);
}
const struct global_type gt_string_array = {
int error;
char *string;
- string = NULL;
- error = parse_toml_string(opt, toml, &string);
+ error = parse_toml_string(toml, opt->name, &string);
if (error)
return error;
+ if (string == NULL)
+ return 0;
error = parse_argv_sync_strategy(opt, string, _result);
argv_parse_function argv;
/**
* Converts from a TOML node to this data type.
+ * If the node is not present in the file, this function should
+ * do nothing.
* Optional if there are no fields of this type that are read
* from TOML files.
*/
raw = toml_raw_in(toml, opt->name);
if (raw == NULL)
- return pr_err("TOML integer '%s' was not found.", opt->name);
+ return 0;
if (toml_rtoi(raw, &value) == -1)
return pr_err("Cannot parse '%s' as an integer.", raw);