Fix config file handling for audiod and clean up init functions.
[paraslash.git] / audiod.c
index 6477c54..e3fd49d 100644 (file)
--- a/audiod.c
+++ b/audiod.c
@@ -193,6 +193,7 @@ static int want_colors(void)
 
 static void parse_config_or_die(void)
 {
+       int ret;
        char *config_file;
        struct audiod_cmdline_parser_params params = {
                .override = 0,
@@ -209,14 +210,13 @@ static void parse_config_or_die(void)
                config_file = make_message("%s/.paraslash/audiod.conf", home);
                free(home);
        }
-       if (conf.config_file_given && !file_exists(config_file)) {
+       ret = file_exists(config_file);
+       if (conf.config_file_given && !ret) {
                PARA_EMERG_LOG("can not read config file %s\n", config_file);
                goto err;
        }
-       if (audiod_cmdline_parser_config_file(config_file, &conf, &params)) {
-               PARA_EMERG_LOG("parse error in config file\n");
-               goto err;
-       }
+       if (ret)
+               audiod_cmdline_parser_config_file(config_file, &conf, &params);
        free(config_file);
        daemon_set_loglevel(conf.loglevel_arg);
        return;
@@ -596,13 +596,12 @@ static int add_filter(int format, char *cmdline)
        return filter_num;
 }
 
-static int init_writers(void)
+static int parse_writer_args(void)
 {
        int i, ret, nw;
        char *cmd;
        struct audio_format_info *a;
 
-       init_supported_writers();
        nw = PARA_MAX(1U, conf.writer_given);
        PARA_INFO_LOG("maximal number of writers: %d\n", nw);
        FOR_EACH_AUDIO_FORMAT(i) {
@@ -635,16 +634,12 @@ out:
        return ret;
 }
 
-static int init_receivers(void)
+static int parse_receiver_args(void)
 {
        int i, ret, receiver_num;
        char *cmd = NULL;
        struct audio_format_info *a;
 
-       for (i = 0; receivers[i].name; i++) {
-               PARA_INFO_LOG("initializing %s receiver\n", receivers[i].name);
-               receivers[i].init(&receivers[i]);
-       }
        for (i = conf.receiver_given - 1; i >= 0; i--) {
                char *arg = conf.receiver_arg[i];
                char *recv_arg = strchr(arg, ':');
@@ -715,11 +710,10 @@ out:
        return ret;
 }
 
-static int init_filters(void)
+static int parse_filter_args(void)
 {
        int i, ret, nf;
 
-       filter_init(filters);
        nf = PARA_MAX(1U, conf.filter_given);
        PARA_INFO_LOG("maximal number of filters: %d\n", nf);
        FOR_EACH_AUDIO_FORMAT(i) {
@@ -748,17 +742,17 @@ out:
        return ret;
 }
 
-static int init_stream_io(void)
+static int parse_stream_args(void)
 {
        int ret;
 
-       ret = init_writers();
+       ret = parse_receiver_args();
        if (ret < 0)
                return ret;
-       ret = init_receivers();
+       ret = parse_filter_args();
        if (ret < 0)
                return ret;
-       ret = init_filters();
+       ret = parse_writer_args();
        if (ret < 0)
                return ret;
        return 1;
@@ -1124,12 +1118,18 @@ int main(int argc, char *argv[])
        };
 
        valid_fd_012();
-       audiod_cmdline_parser_ext(argc, argv, &conf, &params);
+       if (audiod_cmdline_parser_ext(argc, argv, &conf, &params))
+               exit(EXIT_FAILURE);
        HANDLE_VERSION_FLAG("audiod", conf);
+       /* init receivers/filters/writers early to make help work */
+       recv_init();
+       filter_init();
+       writer_init();
        if (conf.help_given || conf.detailed_help_given)
                print_help_and_die();
        drop_privileges_or_die(conf.user_arg, conf.group_arg);
        parse_config_or_die();
+       init_colors_or_die();
        daemon_set_flag(DF_LOG_TIME);
        daemon_set_flag(DF_LOG_HOSTNAME);
        daemon_set_flag(DF_LOG_LL);
@@ -1137,10 +1137,9 @@ int main(int argc, char *argv[])
                daemon_set_logfile(conf.logfile_arg);
                daemon_open_log_or_die();
        }
-       init_colors_or_die();
-       i = init_stream_io();
-       if (i < 0) {
-               PARA_EMERG_LOG("init stream io error: %s\n", para_strerror(-i));
+       ret = parse_stream_args();
+       if (ret < 0) {
+               PARA_EMERG_LOG("%s\n", para_strerror(-ret));
                exit(EXIT_FAILURE);
        }
        log_welcome("para_audiod");