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