mixer.c: Fix doxygen \file description.
[paraslash.git] / recv_common.c
index 280f5bc40a7b6703ca737efe3e4f9e57baf30bde..948de4745dda8c62bce88f31ec8362650ae85de0 100644 (file)
-/*
- * Copyright (C) 2006-2014 Andre Noll <maan@systemlinux.org>
- *
- * Licensed under the GPL v2. For licencing details see COPYING.
- */
+/* Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
 
 /** \file recv_common.c common functions of para_recv and para_audiod */
 
 #include <regex.h>
+#include <inttypes.h>
+#include <lopsub.h>
 
+#include "recv_cmd.lsg.h"
 #include "para.h"
+#include "error.h"
 #include "list.h"
 #include "sched.h"
-#include "ggo.h"
 #include "buffer_tree.h"
 #include "recv.h"
 #include "string.h"
 
 /**
  * Call the init function of each paraslash receiver.
+ *
+ * Receivers employ the user_data feature of the lopsub library: Each receiver
+ * of the recv_cmd suite defines a struct receiver as its user data.
+ * recv_init() obtains a pointer to this structure by calling lls_user_data().
+ * If the receiver has an init function (i.e., if ->init is not NULL), ->init()
+ * is called to initialize the 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];
-       char **argv;
-       int argc;
-       void *conf;
-
-       if (options) {
-               argc = create_shifted_argv(options, " \t", &argv);
-               if (argc < 0)
-                       return NULL;
-       } else {
-               argc = 1;
-               argv = para_malloc(2 * sizeof(char*));
-               argv[1] = NULL;
+       FOR_EACH_RECEIVER(i) {
+               const struct lls_command *cmd = RECV_CMD(i);
+               const struct receiver *r = lls_user_data(cmd);
+               if (r && r->init)
+                       r->init();
        }
-       argv[0] = make_message("%s_recv", r->name);
-       conf = r->parse_config(argc, argv);
-       free_argv(argv);
-       return conf;
 }
 
 /**
  * Check if the given string is a valid receiver specifier.
  *
- * \param \ra string of the form receiver_name:options
- * \param receiver_num contains the number of the receiver upon success
+ * \param ra string of the form receiver_name [options...]
+ * \param lprp Filled in on success, undefined else.
+ *
+ * This function checks whether \a ra starts with the name of a receiver,
+ * optionally followed by options for that receiver. If a valid receiver name
+ * was found the remaining part of \a ra is passed to the receiver's config
+ * parser.
  *
- * 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 the remaining part of \a ra is
- * passed to the receiver's config parser.
+ * If a NULL pointer or an empty string is passed as the first argument, the
+ * hhtp receiver with no options is assumed.
  *
- * \return On success, a pointer to the receiver-specific gengetopt args info
- * struct is returned and \a receiver_num contains the number of the receiver.
- * On errors, the function returns \p NULL.
+ * \return On success the number of the receiver is returned. On errors, the
+ * function calls exit(EXIT_FAILURE).
  */
-void *check_receiver_arg(char *ra, int *receiver_num)
+int check_receiver_arg(const char *ra, struct lls_parse_result **lprp)
 {
-       int j;
+       int ret, argc, receiver_num;
+       char *errctx = NULL, **argv;
+       const struct lls_command *cmd;
 
-       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);
-               char c;
-               if (strlen(ra) < len)
-                       continue;
-               if (strncmp(name, ra, len))
-                       continue;
-               c = ra[len];
-               if (c && c != ' ')
-                       continue;
-               if (c && !receivers[j].parse_config)
-                       return NULL;
-               *receiver_num = j;
-               return parse_receiver_args(j, c? ra + len + 1: NULL);
+       *lprp = NULL;
+       if (!ra || !*ra) {
+               argc = 1;
+               argv = para_malloc(2 * sizeof(char*));
+               argv[0] = para_strdup("http");
+               argv[1] = NULL;
+       } else {
+               ret = create_argv(ra, " \t\n", &argv);
+               if (ret < 0) {
+                       PARA_EMERG_LOG("%s\n", para_strerror(-ret));
+                       exit(EXIT_FAILURE);
+               }
+               argc = ret;
        }
-       PARA_ERROR_LOG("receiver not found\n");
-       return NULL;
+       ret = lls(lls_lookup_subcmd(argv[0], recv_cmd_suite, &errctx));
+       if (ret < 0) {
+               PARA_EMERG_LOG("%s: %s\n", errctx? errctx : argv[0],
+                       para_strerror(-ret));
+               exit(EXIT_FAILURE);
+       }
+       receiver_num = ret;
+       cmd = RECV_CMD(receiver_num);
+       ret = lls(lls_parse(argc, argv, cmd, lprp, &errctx));
+       if (ret < 0) {
+               if (errctx)
+                       PARA_ERROR_LOG("%s\n", errctx);
+               PARA_EMERG_LOG("%s\n", para_strerror(-ret));
+               exit(EXIT_FAILURE);
+       }
+       ret = receiver_num;
+       free_argv(argv);
+       return ret;
 }
 
 /**
  * Print out the help texts to all receivers.
  *
- * \param flags Passed to \ref ggo_print_help().
+ * \param detailed Whether to print the short or the detailed help.
  */
-void print_receiver_helps(unsigned flags)
+void print_receiver_helps(bool detailed)
 {
        int i;
 
-       printf_or_die("\nAvailable receivers: ");
-       FOR_EACH_RECEIVER(i)
-               printf_or_die("%s%s", i? " " : "", receivers[i].name);
-       printf_or_die("\n");
+       printf("\nAvailable receivers: ");
+       FOR_EACH_RECEIVER(i) {
+               const struct lls_command *cmd = RECV_CMD(i);
+               printf("%s%s", i? " " : "", lls_command_name(cmd));
+       }
+       printf("\n\n");
        FOR_EACH_RECEIVER(i) {
-               struct receiver *r = receivers + i;
-               if (!r->help.short_help)
+               const struct lls_command *cmd = RECV_CMD(i);
+               char *help = detailed? lls_long_help(cmd) : lls_short_help(cmd);
+               if (!help)
                        continue;
-               printf_or_die("\n%s: %s", r->name,
-                       r->help.purpose);
-               ggo_print_help(&r->help, flags);
+               printf("%s\n", help);
+               free(help);
        }
 }