]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
Make filter config parsers return int.
authorAndre Noll <maan@systemlinux.org>
Sun, 30 Nov 2008 16:41:50 +0000 (17:41 +0100)
committerAndre Noll <maan@systemlinux.org>
Sun, 30 Nov 2008 16:41:50 +0000 (17:41 +0100)
This way, we can distinguish between syntax errors and invalid config
values (failed sanity checks).

amp_filter.c
compress.c
error.h
filter.h
filter_chain.c
oggdec.c

index 750205ba69ea5ae747aa37290d57503005b0d49f..22784955513ead8b5ba21db76af799e5fadbf66a 100644 (file)
@@ -50,20 +50,23 @@ static void amp_close(struct filter_node *fn)
        free(fn->buf);
 }
 
-static void *amp_parse_config(int argc, char **argv)
+static int amp_parse_config(int argc, char **argv, void **config)
 {
-       struct amp_filter_args_info *conf = para_calloc(sizeof(*conf));
+       struct amp_filter_args_info *amp_conf = para_calloc(sizeof(*amp_conf));
+       int ret = -E_AMP_SYNTAX;
 
-       if (amp_cmdline_parser(argc, argv, conf))
+       if (amp_cmdline_parser(argc, argv, amp_conf))
                goto err;
-       if (conf->amp_arg < 0)
+       ret = -ERRNO_TO_PARA_ERROR(EINVAL);
+       if (amp_conf->amp_arg < 0)
                goto err;
-       PARA_NOTICE_LOG("amplification: %u (scaling factor: %1.2f)\n", conf->amp_arg,
-               conf->amp_arg / 64.0 + 1.0);
-       return conf;
+       PARA_NOTICE_LOG("amplification: %u (scaling factor: %1.2f)\n",
+               amp_conf->amp_arg, amp_conf->amp_arg / 64.0 + 1.0);
+       *config = amp_conf;
+       return 1;
 err:
-       free(conf);
-       return NULL;
+       free(amp_conf);
+       return ret;
 }
 
 static void amp_open(struct filter_node *fn)
index c30c998a33eb3354968f6801fe4890aea43e3b19..22dcfd333c145a4a77e49ba3826c43b61828241b 100644 (file)
@@ -16,6 +16,7 @@
 #include "sched.h"
 #include "filter.h"
 #include "string.h"
+#include "error.h"
 
 /** The size of the output data buffer. */
 #define COMPRESS_CHUNK_SIZE 40960
@@ -83,13 +84,21 @@ static void close_compress(struct filter_node *fn)
        free(fn->buf);
 }
 
-static void *compress_parse_config(int argc, char **argv)
+/** TODO: Add sanity checks */
+static int compress_parse_config(int argc, char **argv, void **config)
 {
-       struct compress_filter_args_info *ret = para_calloc(sizeof(struct compress_filter_args_info));
-       if (!compress_cmdline_parser(argc, argv, ret))
-               return ret;
-       free(ret);
-       return NULL;
+       int ret;
+       struct compress_filter_args_info *compress_conf
+               = para_calloc(sizeof(*compress_conf));
+
+       ret = -E_COMPRESS_SYNTAX;
+       if (compress_cmdline_parser(argc, argv, compress_conf))
+               goto err;
+       *config = compress_conf;
+       return 1;
+err:
+       free(compress_conf);
+       return  ret;
 }
 
 static void open_compress(struct filter_node *fn)
diff --git a/error.h b/error.h
index f0a2510da3c8425d4c514b3cbf2da977b7e877f6..cacc83e9c26d79d59f4a66505c9bfd278b8ff2f0 100644 (file)
--- a/error.h
+++ b/error.h
@@ -14,7 +14,6 @@ DEFINE_ERRLIST_OBJECT_ENUM;
 /* these do not need error handling (yet) */
 #define SERVER_ERRORS
 #define WAV_ERRORS
-#define COMPRESS_ERRORS
 #define TIME_ERRORS
 #define CLOSE_ON_FORK_ERRORS
 #define DAEMON_ERRORS
@@ -27,13 +26,20 @@ DEFINE_ERRLIST_OBJECT_ENUM;
 #define RECV_ERRORS
 #define STDOUT_ERRORS
 #define IPC_ERRORS
-#define AMP_FILTER_ERRORS
 #define DCCP_SEND_ERRORS
 #define HTTP_SEND_ERRORS
 
 
 extern const char **para_errlist[];
 
+#define COMPRESS_ERRORS \
+       PARA_ERROR(COMPRESS_SYNTAX, "syntax error in compress filter config"), \
+
+
+#define AMP_FILTER_ERRORS \
+       PARA_ERROR(AMP_SYNTAX, "syntax error in amp filter config"), \
+
+
 #define SEND_COMMON_ERRORS \
        PARA_ERROR(MAX_CLIENTS, "maximal number of clients exceeded"), \
 
@@ -252,6 +258,7 @@ extern const char **para_errlist[];
        PARA_ERROR(OGGDEC_BADHEADER, "invalid vorbis bitstream header"), \
        PARA_ERROR(OGGDEC_FAULT, "bug or heap/stack corruption"), \
        PARA_ERROR(OGGDEC_BADLINK, "invalid stream section or requested link corrupt"), \
+       PARA_ERROR(OGGDEC_SYNTAX, "syntax error in oggdec config"), \
 
 
 #define GRAB_CLIENT_ERRORS \
index 8c1c9e0d431c26e20a01fff76a2f3edb5e57c054..2d1c150afdda6add9bb498c625889fdcbdda5ea3 100644 (file)
--- a/filter.h
+++ b/filter.h
@@ -191,14 +191,16 @@ struct filter {
        /**
         * A pointer to the filter's command line parser.
         *
-        * If this optional function pointer is not NULL, any filter options are passed
-        * from the main program to this command line parser once at application
-        * startup. The command line parser should check its command line options given
-        * by \a argc and \a argv and abort on errors. On success, it should return a
-        * pointer to the filter-specific configuration data determined by \a argc and
-        * \a argv.
+        * If this optional function pointer is not NULL, any filter options
+        * are passed from the main program to this command line parser once at
+        * application startup. The command line parser should check its
+        * command line options given by \a argc and \a argv and abort on
+        * errors. Success must be indicated by a non-negative return value. In
+        * this case the function should return a pointer to the
+        * filter-specific configuration data determined by \a argc and \a
+        * argv. On failure, a negative paraslash error code must be returned.
         */
-       void *(*parse_config)(int argc, char **argv);
+       int (*parse_config)(int argc, char **argv, void **config);
 };
 
 void close_filters(struct filter_chain *fc);
index fa8637efc189ed8ebae3a3f8db2a72c540713cbc..def0a81381f42c48a0a7b076f037872c2123af9c 100644 (file)
@@ -198,7 +198,7 @@ void close_filters(struct filter_chain *fc)
 static int parse_filter_args(int filter_num, char *options, void **conf)
 {
        struct filter *f = &filters[filter_num];
-       int i, argc = 2;
+       int ret, i, argc = 2;
        char **argv;
 
 //     PARA_DEBUG_LOG("%s, options: %s, parser: %p\n", f->name,
@@ -212,10 +212,10 @@ static int parse_filter_args(int filter_num, char *options, void **conf)
                argv[i + 1] = argv[i];
        argv[0] = para_strdup(f->name);
        argc += 1;
-       *conf = f->parse_config(argc, argv);
+       ret = f->parse_config(argc, argv, conf);
        free(argv[0]);
        free(argv);
-       return *conf? filter_num : -E_BAD_FILTER_OPTIONS;
+       return ret < 0? ret : filter_num;
 }
 
 /**
index e22ea1e464fa083c108c1474c37c802fa6b64659..46fb17ed5808e1c425ad6e9b2da178474f815d4a 100644 (file)
--- a/oggdec.c
+++ b/oggdec.c
@@ -163,15 +163,16 @@ static ssize_t ogg_convert(char *inbuffer, size_t len, struct filter_node *fn)
        return pod->converted;
 }
 
-static void *oggdec_parse_config(int argc, char **argv)
+static int oggdec_parse_config(int argc, char **argv, void **config)
 {
        int ret;
        struct oggdec_filter_args_info *ogg_conf;
 
        ogg_conf = para_calloc(sizeof(*ogg_conf));
-       ret = oggdec_cmdline_parser(argc, argv, ogg_conf);
-       if (ret)
+       ret = -E_OGGDEC_SYNTAX;
+       if (oggdec_cmdline_parser(argc, argv, ogg_conf))
                goto err;
+       ret = -ERRNO_TO_PARA_ERROR(EINVAL);
        if (ogg_conf->bufsize_arg < 0)
                goto err;
        if (ogg_conf->bufsize_arg >= INT_MAX / 1024)
@@ -180,10 +181,11 @@ static void *oggdec_parse_config(int argc, char **argv)
                goto err;
        if (ogg_conf->initial_buffer_arg >= INT_MAX / 1024)
                goto err;
-       return ogg_conf;
+       *config = ogg_conf;
+       return 1;
 err:
        free(ogg_conf);
-       return NULL;
+       return ret;
 }
 
 /**