Merge branch 'maint'
[paraslash.git] / recv_common.c
1 /*
2  * Copyright (C) 2006-2010 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 <regex.h>
10 #include <stdbool.h>
11
12 #include "para.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "ggo.h"
16 #include "recv.h"
17 #include "string.h"
18 #include "buffer_tree.h"
19
20 DEFINE_RECEIVER_ARRAY;
21
22 /**
23  * Call the init function of each paraslash receiver.
24  */
25 void recv_init(void)
26 {
27         int i;
28
29         FOR_EACH_RECEIVER(i)
30                 receivers[i].init(&receivers[i]);
31 }
32
33 static void *parse_receiver_args(int receiver_num, char *options)
34 {
35         struct receiver *r = &receivers[receiver_num];
36         char **argv;
37         int argc, i;
38         void *conf;
39
40
41         PARA_DEBUG_LOG("%s, options: %s\n", r->name,
42                 options? options : "(none)");
43         if (options) {
44                 PARA_DEBUG_LOG("options: %s\n", options);
45                 argc = create_argv(options, " \t", &argv);
46                 if (argc < 0)
47                         return NULL;
48                 for (i = argc - 1; i >= 0; i--)
49                         argv[i + 1] = argv[i];
50                 argc++;
51         } else {
52                 argc = 1;
53                 argv = para_malloc(2 * sizeof(char*));
54                 argv[1] = NULL;
55         }
56         argv[0] = make_message("%s_recv", r->name);
57         conf = r->parse_config(argc, argv);
58         for (i = 0; i < argc; i++)
59                 free(argv[i]);
60         free(argv);
61         return conf;
62 }
63
64 /**
65  * check if given string is a valid command line for any receiver
66  *
67  * \param \ra string of the form receiver_name:options
68  * \param receiver_num contains the number of the receiver upon success
69  *
70  * This function checks whether \a ra starts with the name of a supported
71  * paraslash receiver, optinally followed by a colon and any options for that
72  * receiver. If a valid receiver name was found and further are present, the
73  * remaining part of \a ra is passed to that receiver's config parser.
74  *
75  * \return On success, a pointer to the gengetopt args info struct is returned
76  * and \a receiver_num contains the number of the receiver. Otherwise this function
77  * returns \p NULL.
78  */
79 void *check_receiver_arg(char *ra, int *receiver_num)
80 {
81         int j;
82
83         PARA_DEBUG_LOG("checking %s\n", ra);
84         for (j = 0; receivers[j].name; j++) {
85                 const char *name = receivers[j].name;
86                 size_t len = strlen(name);
87                 char c;
88                 if (strlen(ra) < len)
89                         continue;
90                 if (strncmp(name, ra, len))
91                         continue;
92                 c = ra[len];
93                 if (c && c != ' ')
94                         continue;
95                 if (c && !receivers[j].parse_config)
96                         return NULL;
97                 *receiver_num = j;
98                 return parse_receiver_args(j, c? ra + len + 1: NULL);
99         }
100         PARA_ERROR_LOG("receiver not found\n");
101         return NULL;
102 }
103
104 /**
105  * Print out the help texts to all receivers.
106  *
107  * \param detailed Whether the detailed help should be printed.
108  */
109 void print_receiver_helps(int detailed)
110 {
111         int i;
112
113         printf_or_die("\nAvailable receivers: \n\t");
114         FOR_EACH_RECEIVER(i)
115                 printf_or_die("%s%s", i? " " : "", receivers[i].name);
116         printf_or_die("\n\n");
117         FOR_EACH_RECEIVER(i) {
118                 struct receiver *r = receivers + i;
119                 if (!r->help.short_help)
120                         continue;
121                 printf_or_die("Options for %s:\n", r->name);
122                 ggo_print_help(&r->help, detailed);
123         }
124 }
125
126 int generic_recv_pre_select(struct sched *s, struct task *t)
127 {
128         struct receiver_node *rn = container_of(t, struct receiver_node, task);
129         int ret = btr_node_status(rn->btrn, 0, BTR_NT_ROOT);
130
131         t->error = 0;
132         if (ret < 0) {
133                 s->timeout.tv_sec = 0;
134                 s->timeout.tv_usec = 1;
135         }
136         return ret;
137 }