write: Improve help text of --writer.
[paraslash.git] / filter_common.c
1 /*
2  * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file filter_common.c Common helper functions for filter input/output. */
8
9 #include <regex.h>
10 #include <sys/types.h>
11
12 #include "para.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "fd.h"
16 #include "ggo.h"
17 #include "buffer_tree.h"
18 #include "filter.h"
19 #include "error.h"
20 #include "string.h"
21
22 /** Iterate over the array of supported filters. */
23 #define FOR_EACH_SUPPORTED_FILTER(j)  for (j = 0; j < NUM_SUPPORTED_FILTERS; j++)
24
25 /** The array of supported filters. */
26 static struct filter filters[NUM_SUPPORTED_FILTERS] = {FILTER_ARRAY};
27
28 /**
29  * Obtain a reference to a filter structure.
30  *
31  * \param filter_num Between zero and NUM_SUPPORTED_FILTERS, inclusively.
32  *
33  * \return Pointer to the filter identified by the given filter number.
34  *
35  * It is a fatal error if the given number is out of range. In this case
36  * the function aborts.
37  */
38 const struct filter *filter_get(int filter_num)
39 {
40         assert(filter_num >= 0);
41         assert(filter_num < NUM_SUPPORTED_FILTERS);
42         return filters + filter_num;
43 }
44
45 /**
46  * Call the init function of each supported filter.
47  * \sa filter::init
48  */
49 void filter_init(void)
50 {
51         int i;
52
53         FOR_EACH_SUPPORTED_FILTER(i)
54                 filter_get(i)->init((struct filter *)filter_get(i));
55 }
56
57 /*
58  * If the filter has a command line parser and options is not NULL, run it.
59  * Returns filter_num on success, negative on errors
60  */
61 static int parse_filter_args(int filter_num, char *options, void **conf)
62 {
63         const struct filter *f = filter_get(filter_num);
64         int ret, argc;
65         char **argv;
66
67         if (!f->parse_config)
68                 return strlen(options)? -E_BAD_FILTER_OPTIONS : filter_num;
69         argc = create_shifted_argv(options, " \t", &argv);
70         if (argc < 0)
71                 return -E_BAD_FILTER_OPTIONS;
72         argv[0] = para_strdup(f->name);
73         ret = f->parse_config(argc, argv, conf);
74         free_argv(argv);
75         return ret < 0? ret : filter_num;
76 }
77
78 /**
79  * Check the filter command line options.
80  *
81  * \param fa The command line options.
82  * \param conf Points to the filter configuration upon successful return.
83  *
84  * Check if \a fa starts with a the name of a supported filter, followed by
85  * a colon. If yes, call the command line parser of that filter.
86  *
87  * \return On success, the number of the filter is returned and \a conf
88  * is initialized to point to the filter configuration determined by \a fa.
89  * On errors, a negative value is returned.
90  *
91  * Note: If \a fa specifies a filter that has no command line parser success is
92  * returned, and \a conf is initialized to \p NULL.
93  *
94  * \sa filter::parse_config
95  */
96 int check_filter_arg(char *fa, void **conf)
97 {
98         int j;
99
100         *conf = NULL;
101 //      PARA_DEBUG_LOG("arg: %s\n", fa);
102         FOR_EACH_SUPPORTED_FILTER(j) {
103                 const char *name = filter_get(j)->name;
104                 size_t len = strlen(name);
105                 char c;
106                 if (strlen(fa) < len)
107                         continue;
108                 if (strncmp(name, fa, len))
109                         continue;
110                 c = fa[len];
111                 if (c && c != ' ')
112                         continue;
113                 if (c && !filter_get(j)->parse_config)
114                         return -E_BAD_FILTER_OPTIONS;
115                 return parse_filter_args(j, c? fa + len + 1 :
116                         fa + strlen(fa), conf);
117         }
118         return -E_UNSUPPORTED_FILTER;
119 }
120
121 /**
122  * Print help text of each filter to stdout.
123  *
124  * \param flags Passed to \ref ggo_print_help().
125  */
126 void print_filter_helps(unsigned flags)
127 {
128         int i, num = 0;
129
130         printf_or_die("\nAvailable filters: ");
131         FOR_EACH_SUPPORTED_FILTER(i) {
132                 if (num > 50) {
133                         printf_or_die("\n                  ");
134                         num = 0;
135                 }
136                 num += printf_or_die("%s%s", i? " " : "", filter_get(i)->name);
137         }
138         printf_or_die("\n");
139
140         FOR_EACH_SUPPORTED_FILTER(i) {
141                 struct filter *f = (struct filter *)filter_get(i);
142
143                 if (!f->help.short_help)
144                         continue;
145                 printf_or_die("\nOptions for %s (%s):", f->name,
146                         f->help.purpose);
147                 ggo_print_help(&f->help, flags);
148         }
149 }
150
151 /**
152  * Set select timeout of the scheduler.
153  *
154  * \param s The scheduler.
155  * \param context Pointer to the filter node (task context).
156  *
157  * This looks at the status of the btr node of the filter. If data is available
158  * in the input queue of the filter, or if an error occurred, a minimal timeout
159  * for the next select call is requested from the scheduler. Otherwise the
160  * scheduler timeout is left unchanged.
161  */
162 void generic_filter_pre_select(struct sched *s, void *context)
163 {
164         struct filter_node *fn = context;
165
166         if (btr_node_status(fn->btrn, fn->min_iqs, BTR_NT_INTERNAL) != 0)
167                 sched_min_delay(s);
168 }
169
170 #ifdef WORDS_BIGENDIAN
171 #define DECODER_SAMPLE_FORMAT SF_S16_BE
172 #else
173 #define DECODER_SAMPLE_FORMAT SF_S16_LE
174 #endif
175
176 /**
177  * Execute a btr command for a decoder.
178  *
179  * The buffer tree nodes of the writers ask the parent nodes about sample_rate,
180  * channels count and sample format. This function is called by all decoders to
181  * answer these queries.
182  *
183  * \param cmd The command to be executed by the child node.
184  * \param sample_rate Known to the decoder.
185  * \param channels Known to the decoder.
186  * \param result Ascii representation on the answer is stored here.
187  *
188  * \return Standard.
189  */
190 int decoder_execute(const char *cmd, unsigned sample_rate, unsigned channels,
191                 char **result)
192 {
193         if (!strcmp(cmd, "sample_rate")) {
194                 if (sample_rate == 0)
195                         return -E_BTR_NAVAIL;
196                 *result = make_message("%u", sample_rate);
197                 return 1;
198         }
199         if (!strcmp(cmd, "channels")) {
200                 if (channels == 0)
201                         return -E_BTR_NAVAIL;
202                 *result = make_message("%u", channels);
203                 return 1;
204         }
205         if (!strcmp(cmd, "sample_format")) {
206                 *result = make_message("%u", DECODER_SAMPLE_FORMAT);
207                 return 1;
208         }
209         return -ERRNO_TO_PARA_ERROR(ENOTSUP);
210 }