doc: Change email address to maan@tuebingen.mpg.de
[paraslash.git] / recv_common.c
1 /*
2 * Copyright (C) 2006-2014 Andre Noll <maan@tuebingen.mpg.de>
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
11 #include "para.h"
12 #include "list.h"
13 #include "sched.h"
14 #include "ggo.h"
15 #include "buffer_tree.h"
16 #include "recv.h"
17 #include "string.h"
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;
35 void *conf;
36
37 if (options) {
38 argc = create_shifted_argv(options, " \t", &argv);
39 if (argc < 0)
40 return NULL;
41 } else {
42 argc = 1;
43 argv = para_malloc(2 * sizeof(char*));
44 argv[1] = NULL;
45 }
46 argv[0] = make_message("%s_recv", r->name);
47 conf = r->parse_config(argc, argv);
48 free_argv(argv);
49 return conf;
50 }
51
52 /**
53 * Check if the given string is a valid receiver specifier.
54 *
55 * \param \ra string of the form receiver_name:options
56 * \param receiver_num contains the number of the receiver upon success
57 *
58 * This function checks whether \a ra starts with the name of a supported
59 * paraslash receiver, optinally followed by a colon and any options for that
60 * receiver. If a valid receiver name was found the remaining part of \a ra is
61 * passed to the receiver's config parser.
62 *
63 * \return On success, a pointer to the receiver-specific gengetopt args info
64 * struct is returned and \a receiver_num contains the number of the receiver.
65 * On errors, the function returns \p NULL.
66 */
67 void *check_receiver_arg(char *ra, int *receiver_num)
68 {
69 int j;
70
71 PARA_DEBUG_LOG("checking %s\n", ra);
72 for (j = 0; receivers[j].name; j++) {
73 const char *name = receivers[j].name;
74 size_t len = strlen(name);
75 char c;
76 if (strlen(ra) < len)
77 continue;
78 if (strncmp(name, ra, len))
79 continue;
80 c = ra[len];
81 if (c && c != ' ')
82 continue;
83 if (c && !receivers[j].parse_config)
84 return NULL;
85 *receiver_num = j;
86 return parse_receiver_args(j, c? ra + len + 1: NULL);
87 }
88 PARA_ERROR_LOG("receiver not found\n");
89 return NULL;
90 }
91
92 /**
93 * Print out the help texts to all receivers.
94 *
95 * \param flags Passed to \ref ggo_print_help().
96 */
97 void print_receiver_helps(unsigned flags)
98 {
99 int i;
100
101 printf_or_die("\nAvailable receivers: ");
102 FOR_EACH_RECEIVER(i)
103 printf_or_die("%s%s", i? " " : "", receivers[i].name);
104 printf_or_die("\n");
105 FOR_EACH_RECEIVER(i) {
106 struct receiver *r = receivers + i;
107 if (!r->help.short_help)
108 continue;
109 printf_or_die("\n%s: %s", r->name,
110 r->help.purpose);
111 ggo_print_help(&r->help, flags);
112 }
113 }
114
115 /**
116 * Simple pre-select hook, used by all receivers.
117 *
118 * \param s Scheduler info.
119 * \param rn The receiver node.
120 *
121 * This requests a minimal delay from the scheduler if the status of the buffer
122 * tree node indicates an error/eof condition. No file descriptors are added to
123 * the fd sets of \a s.
124 *
125 * \return The status of the btr node of the receiver node, i.e. the return
126 * value of the underlying call to \ref btr_node_status().
127 */
128 int generic_recv_pre_select(struct sched *s, struct receiver_node *rn)
129 {
130 int ret = btr_node_status(rn->btrn, 0, BTR_NT_ROOT);
131
132 if (ret < 0)
133 sched_min_delay(s);
134 return ret;
135 }