From: Andre Noll Date: Sat, 6 Aug 2011 20:32:49 +0000 (+0200) Subject: write_common: Make check_writer_arg() exit on syntax errors. X-Git-Tag: v0.4.8~4^2 X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=commitdiff_plain;h=eb992f2fc3ff5b6bd1c0b91d473b07675fe5b927 write_common: Make check_writer_arg() exit on syntax errors. This function parses the argument of the --writer option to extract the name of the writer, which is the first word of the argument. If this word is not the name of a supported writer, the function returns NULL to indicate that the given writer does not exist. Otherwise check_writer_arg() calls the ->parse_config_or_die method of this writer which parses the remaining part of the argument and exits on errors. It is more consistent and simpler to let this function exit also in case the writer was not found. This simplifies the callers a bit since they do not have to check the return value any more. It also allows to kill E_WRITE_COMMON_SYNTAX. --- diff --git a/audiod.c b/audiod.c index 5af79b04..c78df5b8 100644 --- a/audiod.c +++ b/audiod.c @@ -776,16 +776,13 @@ static int parse_writer_args(void) ret = parse_stream_command(conf.writer_arg[i], &cmd); if (ret < 0) - goto out; + return ret; af_mask = ret; FOR_EACH_AUDIO_FORMAT(j) { a = afi + j; if ((af_mask & (1 << j)) == 0) /* no match */ continue; - ret = -E_WRITE_COMMON_SYNTAX; - wconf = check_writer_arg(cmd, &writer_num); - if (!wconf) - goto out; + wconf = check_writer_arg_or_die(cmd, &writer_num); nw = a->num_writers; a->writer_nums = para_realloc(a->writer_nums, (nw + 1) * sizeof(int)); a->writer_conf = para_realloc(a->writer_conf, (nw + 1) * sizeof(void *)); @@ -810,9 +807,7 @@ static int parse_writer_args(void) a->writer_conf[0] = w->parse_config_or_die(""); a->num_writers = 1; } - ret = 1; -out: - return ret; + return 1; } static int parse_receiver_args(void) diff --git a/error.h b/error.h index 1e4bf044..79ead6db 100644 --- a/error.h +++ b/error.h @@ -441,7 +441,6 @@ extern const char **para_errlist[]; #define WRITE_COMMON_ERRORS \ - PARA_ERROR(WRITE_COMMON_SYNTAX, "syntax error in write option"), \ PARA_ERROR(WRITE_COMMON_EOF, "end of file"), \ diff --git a/write.c b/write.c index fcb477f8..3b5f2d19 100644 --- a/write.c +++ b/write.c @@ -185,19 +185,16 @@ __noreturn static void print_help_and_die(void) * * \return Standard. */ -static int setup_writer_node(const char *arg, struct btr_node *parent, +static void setup_writer_node(const char *arg, struct btr_node *parent, struct writer_node *wn) { if (arg) - wn->conf = check_writer_arg(arg, &wn->writer_num); + wn->conf = check_writer_arg_or_die(arg, &wn->writer_num); else { wn->writer_num = DEFAULT_WRITER; wn->conf = writers[DEFAULT_WRITER].parse_config_or_die(""); } - if (!wn->conf) - return -E_WRITE_COMMON_SYNTAX; register_writer_node(wn, parent); - return 1; } static int setup_and_schedule(void) @@ -225,20 +222,14 @@ static int setup_and_schedule(void) register_task(&cwt->task); if (!conf.writer_given) { - i = 0; wns = para_calloc(sizeof(*wns)); - ret = setup_writer_node(NULL, cwt->btrn, wns); - if (ret < 0) - goto out; + setup_writer_node(NULL, cwt->btrn, wns); i = 1; } else { wns = para_calloc(conf.writer_given * sizeof(*wns)); - for (i = 0; i < conf.writer_given; i++) { - ret = setup_writer_node(conf.writer_arg[i], - cwt->btrn, wns + i); - if (ret < 0) - goto out; - } + for (i = 0; i < conf.writer_given; i++) + setup_writer_node(conf.writer_arg[i], cwt->btrn, + wns + i); } s.default_timeout.tv_sec = 10; @@ -258,7 +249,6 @@ static int setup_and_schedule(void) } } } -out: for (i--; i >= 0; i--) { struct writer_node *wn = wns + i; struct writer *w = writers + wn->writer_num; diff --git a/write_common.c b/write_common.c index 2c149dfd..b8569936 100644 --- a/write_common.c +++ b/write_common.c @@ -47,14 +47,13 @@ void writer_init(void) * * \return On success, a pointer to the gengetopt args info struct is returned * and \a writer_num contains the number of the writer. Otherwise this function - * returns \p NULL. + * prints an error message and calls exit(). */ -void *check_writer_arg(const char *wa, int *writer_num) +void *check_writer_arg_or_die(const char *wa, int *writer_num) { int i; - *writer_num = -E_WRITE_COMMON_SYNTAX; - PARA_INFO_LOG("checking %s\n", wa); + PARA_INFO_LOG("checking %s\n", wa); FOR_EACH_WRITER(i) { const char *name = writer_names[i]; size_t len = strlen(name); @@ -69,8 +68,8 @@ void *check_writer_arg(const char *wa, int *writer_num) *writer_num = i; return writers[i].parse_config_or_die(c? wa + len + 1 : ""); } - PARA_ERROR_LOG("writer not found\n"); - return NULL; + PARA_EMERG_LOG("invalid writer %s\n", wa); + exit(EXIT_FAILURE); } /** diff --git a/write_common.h b/write_common.h index fc702ca0..00ded8f3 100644 --- a/write_common.h +++ b/write_common.h @@ -7,7 +7,7 @@ /** \file write_common.h Exported symbols from write_common.c. */ void writer_init(void); -void *check_writer_arg(const char *wa, int *writer_num); +void *check_writer_arg_or_die(const char *wa, int *writer_num); void print_writer_helps(int detailed); void register_writer_node(struct writer_node *wn, struct btr_node *parent); void get_btr_sample_rate(struct btr_node *btrn, int32_t *result);