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