Add *_command_list.c to .gitignore.
[paraslash.git] / recv_common.c
1 /*
2  * Copyright (C) 2006-2009 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file recv_common.c common functions of para_recv and para_audiod */
8
9 #include "para.h"
10
11 #include "list.h"
12 #include "sched.h"
13 #include "ggo.h"
14 #include "recv.h"
15 #include "string.h"
16
17 DEFINE_RECEIVER_ARRAY;
18
19 /**
20  * Call the init function of each paraslash receiver.
21  */
22 void recv_init(void)
23 {
24         int i;
25
26         FOR_EACH_RECEIVER(i)
27                 receivers[i].init(&receivers[i]);
28 }
29
30 static void *parse_receiver_args(int receiver_num, char *options)
31 {
32         struct receiver *r = &receivers[receiver_num];
33         char **argv;
34         int argc, i;
35         void *conf;
36
37
38 //      PARA_DEBUG_LOG("%s, options: %s\n", r->name,
39 //              options? options : "(none)");
40         if (options) {
41                 PARA_DEBUG_LOG("options: %s\n", options);
42                 argc = split_args(options, &argv, " \t");
43                 for (i = argc - 1; i >= 0; i--)
44                         argv[i + 1] = argv[i];
45                 argv[0] = para_strdup(r->name);
46                 argc += 1;
47                 PARA_DEBUG_LOG("argc = %d, argv[0]: %s\n", argc, argv[0]);
48         } else {
49                 argc = 1;
50                 argv = para_malloc(2 * sizeof(char*));
51                 argv[0] = NULL;
52                 argv[1] = NULL;
53         }
54         conf = r->parse_config(argc, argv);
55         free(argv[0]);
56         free(argv);
57         return conf;
58 }
59
60 /**
61  * check if given string is a valid command line for any receiver
62  *
63  * \param \ra string of the form receiver_name:options
64  * \param receiver_num contains the number of the receiver upon success
65  *
66  * This function checks whether \a ra starts with the name of a supported
67  * paraslash receiver, optinally followed by a colon and any options for that
68  * receiver. If a valid receiver name was found and further are present, the
69  * remaining part of \a ra is passed to that receiver's config parser.
70  *
71  * \return On success, a pointer to the gengetopt args info struct is returned
72  * and \a receiver_num contains the number of the receiver. Otherwise this function
73  * returns \p NULL.
74  */
75 void *check_receiver_arg(char *ra, int *receiver_num)
76 {
77         int j;
78
79         PARA_DEBUG_LOG("checking %s\n", ra);
80         for (j = 0; receivers[j].name; j++) {
81                 const char *name = receivers[j].name;
82                 size_t len = strlen(name);
83                 char c;
84                 if (strlen(ra) < len)
85                         continue;
86                 if (strncmp(name, ra, len))
87                         continue;
88                 c = ra[len];
89                 if (c && c != ' ')
90                         continue;
91                 if (c && !receivers[j].parse_config)
92                         return NULL;
93                 *receiver_num = j;
94                 return parse_receiver_args(j, c? ra + len + 1: NULL);
95         }
96         PARA_ERROR_LOG("receiver not found\n");
97         return NULL;
98 }
99
100 /**
101  * Print out the help texts to all receivers.
102  *
103  * \param detailed Whether the detailed help should be printed.
104  */
105 void print_receiver_helps(int detailed)
106 {
107         int i;
108
109         printf_or_die("\nAvailable receivers: \n\t");
110         FOR_EACH_RECEIVER(i)
111                 printf_or_die("%s%s", i? " " : "", receivers[i].name);
112         printf_or_die("\n\n");
113         FOR_EACH_RECEIVER(i) {
114                 struct receiver *r = receivers + i;
115                 if (!r->help.short_help)
116                         continue;
117                 printf_or_die("Options for %s:\n", r->name);
118                 ggo_print_help(&r->help, detailed);
119         }
120 }