Merge commit 'fml/master' into next
[paraslash.git] / recv_common.c
index 39a8408..a568939 100644 (file)
@@ -1,32 +1,33 @@
 /*
 /*
- * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2006-2009 Andre Noll <maan@systemlinux.org>
  *
  *
- *     This program is free software; you can redistribute it and/or modify
- *     it under the terms of the GNU General Public License as published by
- *     the Free Software Foundation; either version 2 of the License, or
- *     (at your option) any later version.
- *
- *     This program is distributed in the hope that it will be useful,
- *     but WITHOUT ANY WARRANTY; without even the implied warranty of
- *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *     GNU General Public License for more details.
- *
- *     You should have received a copy of the GNU General Public License
- *     along with this program; if not, write to the Free Software
- *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
 /** \file recv_common.c common functions of para_recv and para_audiod */
 
  */
 
 /** \file recv_common.c common functions of para_recv and para_audiod */
 
-#include "para.h"
+#include <regex.h>
 
 
+#include "para.h"
+#include "list.h"
+#include "sched.h"
+#include "ggo.h"
 #include "recv.h"
 #include "string.h"
 
 #include "recv.h"
 #include "string.h"
 
-void (*crypt_function_recv)(unsigned long len, const unsigned char *indata, unsigned char *outdata) = NULL;
-void (*crypt_function_send)(unsigned long len, const unsigned char *indata, unsigned char *outdata) = NULL;
-
 DEFINE_RECEIVER_ARRAY;
 DEFINE_RECEIVER_ARRAY;
+
+/**
+ * Call the init function of each paraslash receiver.
+ */
+void recv_init(void)
+{
+       int i;
+
+       FOR_EACH_RECEIVER(i)
+               receivers[i].init(&receivers[i]);
+}
+
 static void *parse_receiver_args(int receiver_num, char *options)
 {
        struct receiver *r = &receivers[receiver_num];
 static void *parse_receiver_args(int receiver_num, char *options)
 {
        struct receiver *r = &receivers[receiver_num];
@@ -35,15 +36,17 @@ static void *parse_receiver_args(int receiver_num, char *options)
        void *conf;
 
 
        void *conf;
 
 
-//     PARA_DEBUG_LOG("%s, options: %s\n", r->name,
-//             options? options : "(none)");
+       PARA_DEBUG_LOG("%s, options: %s\n", r->name,
+               options? options : "(none)");
        if (options) {
        if (options) {
-//             PARA_DEBUG_LOG("%s options: %s\n", name, options);
-               argc = split_args(options, &argv, ' ');
-//             PARA_DEBUG_LOG("argc = %d, argv[0]: %s\n", fn->argc, fn->argv[0]);
-               for (i = argc; i >= 0; i--)
+               PARA_DEBUG_LOG("options: %s\n", options);
+               argc = create_argv(options, " \t", &argv);
+               if (argc < 0)
+                       return NULL;
+               for (i = argc - 1; i >= 0; i--)
                        argv[i + 1] = argv[i];
                        argv[i + 1] = argv[i];
-               argc += 2;
+               argv[0] = NULL;
+               argc++;
        } else {
                argc = 1;
                argv = para_malloc(2 * sizeof(char*));
        } else {
                argc = 1;
                argv = para_malloc(2 * sizeof(char*));
@@ -51,20 +54,32 @@ static void *parse_receiver_args(int receiver_num, char *options)
                argv[1] = NULL;
        }
        conf = r->parse_config(argc, argv);
                argv[1] = NULL;
        }
        conf = r->parse_config(argc, argv);
-       if (!conf) {
-               for (i = 0; i < argc; i++)
-                       free(argv[i]);
-               free(argv);
-               return NULL;
-       }
+       for (i = 1; i < argc; i++)
+               free(argv[i]);
+       free(argv);
        return conf;
 }
 
        return conf;
 }
 
+/**
+ * check if given string is a valid command line for any receiver
+ *
+ * \param \ra string of the form receiver_name:options
+ * \param receiver_num contains the number of the receiver upon success
+ *
+ * This function checks whether \a ra starts with the name of a supported
+ * paraslash receiver, optinally followed by a colon and any options for that
+ * receiver. If a valid receiver name was found and further are present, the
+ * remaining part of \a ra is passed to that receiver's config parser.
+ *
+ * \return On success, a pointer to the gengetopt args info struct is returned
+ * and \a receiver_num contains the number of the receiver. Otherwise this function
+ * returns \p NULL.
+ */
 void *check_receiver_arg(char *ra, int *receiver_num)
 {
        int j;
 
 void *check_receiver_arg(char *ra, int *receiver_num)
 {
        int j;
 
-//     PARA_DEBUG_LOG("checking %s\n", ra);
+       PARA_DEBUG_LOG("checking %s\n", ra);
        for (j = 0; receivers[j].name; j++) {
                const char *name = receivers[j].name;
                size_t len = strlen(name);
        for (j = 0; receivers[j].name; j++) {
                const char *name = receivers[j].name;
                size_t len = strlen(name);
@@ -81,6 +96,28 @@ void *check_receiver_arg(char *ra, int *receiver_num)
                *receiver_num = j;
                return parse_receiver_args(j, c? ra + len + 1: NULL);
        }
                *receiver_num = j;
                return parse_receiver_args(j, c? ra + len + 1: NULL);
        }
-       PARA_ERROR_LOG("%s", "receiver not found\n");
+       PARA_ERROR_LOG("receiver not found\n");
        return NULL;
 }
        return NULL;
 }
+
+/**
+ * Print out the help texts to all receivers.
+ *
+ * \param detailed Whether the detailed help should be printed.
+ */
+void print_receiver_helps(int detailed)
+{
+       int i;
+
+       printf_or_die("\nAvailable receivers: \n\t");
+       FOR_EACH_RECEIVER(i)
+               printf_or_die("%s%s", i? " " : "", receivers[i].name);
+       printf_or_die("\n\n");
+       FOR_EACH_RECEIVER(i) {
+               struct receiver *r = receivers + i;
+               if (!r->help.short_help)
+                       continue;
+               printf_or_die("Options for %s:\n", r->name);
+               ggo_print_help(&r->help, detailed);
+       }
+}