From bb991c66000ea867d0fcbd692ffdc5473eb785fb Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Thu, 4 Nov 2010 08:59:57 +0100 Subject: [PATCH] write: Simplify config parsers. These functions all call the gengetopt parser which aborts on errors. It is therefore pointless to check the return value. Document this fact and make it explicit by renaming ->parse_config of struct writer to ->parse_config_or_die(). --- alsa_write.c | 17 +++++------------ file_write.c | 18 +++++++----------- oss_write.c | 13 ++++--------- osx_write.c | 18 ++++++------------ write.h | 14 ++++++++------ write_common.c | 7 +++---- 6 files changed, 33 insertions(+), 54 deletions(-) diff --git a/alsa_write.c b/alsa_write.c index bcabe53c..aa772daa 100644 --- a/alsa_write.c +++ b/alsa_write.c @@ -279,21 +279,14 @@ err: t->error = ret; } -__malloc static void *alsa_parse_config(const char *options) +__malloc static void *alsa_parse_config_or_die(const char *options) { - int ret; - struct alsa_write_args_info *conf - = para_calloc(sizeof(struct alsa_write_args_info)); + struct alsa_write_args_info *conf = para_calloc(sizeof(*conf)); PARA_INFO_LOG("options: %s, %zd\n", options, strcspn(options, " \t")); - ret = alsa_cmdline_parser_string(options, conf, "alsa_write"); - if (ret) - goto err_out; - PARA_INFO_LOG("help given: %d\n", conf->help_given); + /* exits on errors */ + alsa_cmdline_parser_string(options, conf, "alsa_write"); return conf; -err_out: - free(conf); - return NULL; } static void alsa_free_config(void *conf) @@ -317,7 +310,7 @@ void alsa_write_init(struct writer *w) w->close = alsa_close; w->pre_select = alsa_write_pre_select; w->post_select = alsa_write_post_select; - w->parse_config = alsa_parse_config; + w->parse_config_or_die = alsa_parse_config_or_die; w->shutdown = NULL; /* nothing to do */ w->free_config = alsa_free_config; w->help = (struct ggo_help) { diff --git a/file_write.c b/file_write.c index 0cee535a..4a4dc66e 100644 --- a/file_write.c +++ b/file_write.c @@ -129,17 +129,13 @@ out: t->error = ret; } -__malloc static void *file_write_parse_config(const char *options) +__malloc static void *file_write_parse_config_or_die(const char *options) { - struct file_write_args_info *conf - = para_calloc(sizeof(struct file_write_args_info)); - int ret = file_cmdline_parser_string(options, conf, "file_write"); - - PARA_INFO_LOG("conf->filename_given: %d\n", conf->filename_given); - if (!ret) - return conf; - free(conf); - return NULL; + struct file_write_args_info *conf = para_calloc(sizeof(*conf)); + + /* exits on errors */ + file_cmdline_parser_string(options, conf, "file_write"); + return conf; } static void file_write_free_config(void *conf) @@ -156,7 +152,7 @@ void file_write_init(struct writer *w) w->open = file_write_open; w->pre_select = file_write_pre_select; w->post_select = file_write_post_select; - w->parse_config = file_write_parse_config; + w->parse_config_or_die = file_write_parse_config_or_die; w->free_config = file_write_free_config; w->close = file_write_close; w->shutdown = NULL; /* nothing to do */ diff --git a/oss_write.c b/oss_write.c index b82b3968..5bc41bcc 100644 --- a/oss_write.c +++ b/oss_write.c @@ -209,18 +209,13 @@ static int oss_open(struct writer_node *wn) return 1; } -__malloc static void *oss_parse_config(const char *options) +__malloc static void *oss_parse_config_or_die(const char *options) { - int ret; struct oss_write_args_info *conf = para_calloc(sizeof(*conf)); - ret = oss_cmdline_parser_string(options, conf, "oss_write"); - if (ret) - goto err_out; + /* exits on errors */ + oss_cmdline_parser_string(options, conf, "oss_write"); return conf; -err_out: - free(conf); - return NULL; } static void oss_free_config(void *conf) @@ -244,7 +239,7 @@ void oss_write_init(struct writer *w) w->close = oss_close; w->pre_select = oss_pre_select; w->post_select = oss_post_select; - w->parse_config = oss_parse_config; + w->parse_config_or_die = oss_parse_config_or_die; w->free_config = oss_free_config; w->shutdown = NULL; w->help = (struct ggo_help) { diff --git a/osx_write.c b/osx_write.c index cfd02e74..b0dfa89b 100644 --- a/osx_write.c +++ b/osx_write.c @@ -279,19 +279,13 @@ e0: return ret; } -__malloc static void *osx_write_parse_config(const char *options) +__malloc static void *osx_write_parse_config_or_die(const char *options) { - struct osx_write_args_info *conf - = para_calloc(sizeof(struct osx_write_args_info)); - PARA_INFO_LOG("options: %s\n", options); - int ret = osx_cmdline_parser_string(options, conf, "osx_write"); - if (ret) - goto err_out; - return conf; -err_out: - free(conf); - return NULL; + struct osx_write_args_info *conf = para_calloc(sizeof(*conf)); + /* exits on errors */ + osx_cmdline_parser_string(options, conf, "osx_write"); + return conf; } static void osx_free_config(void *conf) @@ -377,7 +371,7 @@ void osx_write_init(struct writer *w) w->close = osx_write_close; w->pre_select = osx_write_pre_select; w->post_select = osx_write_post_select; - w->parse_config = osx_write_parse_config; + w->parse_config_or_die = osx_write_parse_config_or_die; w->free_config = osx_free_config; w->shutdown = NULL; /* nothing to do */ w->help = (struct ggo_help) { diff --git a/write.h b/write.h index 1361fcf1..fd0f4f6b 100644 --- a/write.h +++ b/write.h @@ -36,16 +36,18 @@ struct writer { /** * The command line parser of the writer. * - * It should check whether the command line options given by \a options are - * valid. On success, it should return a pointer to the writer-specific - * configuration data determined by \a options. Note that this might be called - * more than once with different values of \a options. \sa \ref free_config(). + * It should check whether the command line options given by \a options + * are valid and return a pointer to the writer-specific configuration + * data determined by \a options. This function must either succeed or + * call exit(). Note that parse_config_or_die() might be called more + * than once with different values of \a options. \sa \ref + * free_config(). */ - void *(*parse_config)(const char *options); + void *(*parse_config_or_die)(const char *options); /** * Dellocate all configuration resources. * - * This should free whatever was allocated by \ref parse_config(). + * This should free whatever was allocated by \ref parse_config_or_die(). */ void (*free_config)(void *config); /** diff --git a/write_common.c b/write_common.c index 93562d0d..5352356b 100644 --- a/write_common.c +++ b/write_common.c @@ -66,10 +66,10 @@ void *check_writer_arg(const char *wa, int *writer_num) c = wa[len]; if (c && c != ' ') continue; - if (c && !writers[i].parse_config) + if (c && !writers[i].parse_config_or_die) return NULL; *writer_num = i; - return writers[i].parse_config(c? wa + len + 1 : ""); + return writers[i].parse_config_or_die(c? wa + len + 1 : ""); } PARA_ERROR_LOG("writer not found\n"); return NULL; @@ -115,7 +115,7 @@ int setup_writer_node(const char *arg, struct btr_node *parent, wn->conf = check_writer_arg(arg, &wn->writer_num); else { wn->writer_num = DEFAULT_WRITER; - wn->conf = writers[DEFAULT_WRITER].parse_config(""); + wn->conf = writers[DEFAULT_WRITER].parse_config_or_die(""); } if (!wn->conf) return -E_WRITE_COMMON_SYNTAX; @@ -123,7 +123,6 @@ int setup_writer_node(const char *arg, struct btr_node *parent, return 1; } - /** * Print the help text of all writers to stdout. * -- 2.39.2