test-lib: Canonicalize test_dir.
[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 const struct filter *filter_get(int filter_num)
29 {
30         assert(filter_num >= 0);
31         assert(filter_num < NUM_SUPPORTED_FILTERS);
32         return filters + filter_num;
33 }
34
35 /**
36  * Call the init function of each supported filter.
37  * \sa filter::init
38  */
39 void filter_init(void)
40 {
41         int i;
42
43         FOR_EACH_SUPPORTED_FILTER(i)
44                 filter_get(i)->init((struct filter *)filter_get(i));
45 }
46
47 /*
48  * If the filter has a command line parser and options is not NULL, run it.
49  * Returns filter_num on success, negative on errors
50  */
51 static int parse_filter_args(int filter_num, char *options, void **conf)
52 {
53         const struct filter *f = filter_get(filter_num);
54         int ret, argc;
55         char **argv;
56
57         if (!f->parse_config)
58                 return strlen(options)? -E_BAD_FILTER_OPTIONS : filter_num;
59         argc = create_shifted_argv(options, " \t", &argv);
60         if (argc < 0)
61                 return -E_BAD_FILTER_OPTIONS;
62         argv[0] = para_strdup(f->name);
63         ret = f->parse_config(argc, argv, conf);
64         free_argv(argv);
65         return ret < 0? ret : filter_num;
66 }
67
68 /**
69  * Check the filter command line options.
70  *
71  * \param fa The command line options.
72  * \param conf Points to the filter configuration upon successful return.
73  *
74  * Check if \a fa starts with a the name of a supported filter, followed by
75  * a colon. If yes, call the command line parser of that filter.
76  *
77  * \return On success, the number of the filter is returned and \a conf
78  * is initialized to point to the filter configuration determined by \a fa.
79  * On errors, a negative value is returned.
80  *
81  * Note: If \a fa specifies a filter that has no command line parser success is
82  * returned, and \a conf is initialized to \p NULL.
83  *
84  * \sa filter::parse_config
85  */
86 int check_filter_arg(char *fa, void **conf)
87 {
88         int j;
89
90         *conf = NULL;
91 //      PARA_DEBUG_LOG("arg: %s\n", fa);
92         FOR_EACH_SUPPORTED_FILTER(j) {
93                 const char *name = filter_get(j)->name;
94                 size_t len = strlen(name);
95                 char c;
96                 if (strlen(fa) < len)
97                         continue;
98                 if (strncmp(name, fa, len))
99                         continue;
100                 c = fa[len];
101                 if (c && c != ' ')
102                         continue;
103                 if (c && !filter_get(j)->parse_config)
104                         return -E_BAD_FILTER_OPTIONS;
105                 return parse_filter_args(j, c? fa + len + 1 :
106                         fa + strlen(fa), conf);
107         }
108         return -E_UNSUPPORTED_FILTER;
109 }
110
111 /**
112  * Print help text of each filter to stdout.
113  *
114  * \param flags Passed to \ref ggo_print_help().
115  */
116 void print_filter_helps(unsigned flags)
117 {
118         int i, num = 0;
119
120         printf_or_die("\nAvailable filters: ");
121         FOR_EACH_SUPPORTED_FILTER(i) {
122                 if (num > 50) {
123                         printf_or_die("\n                  ");
124                         num = 0;
125                 }
126                 num += printf_or_die("%s%s", i? " " : "", filter_get(i)->name);
127         }
128         printf_or_die("\n");
129
130         FOR_EACH_SUPPORTED_FILTER(i) {
131                 struct filter *f = (struct filter *)filter_get(i);
132
133                 if (!f->help.short_help)
134                         continue;
135                 printf_or_die("\nOptions for %s (%s):", f->name,
136                         f->help.purpose);
137                 ggo_print_help(&f->help, flags);
138         }
139 }
140
141 /**
142  * Set select timeout of the scheduler.
143  *
144  * \param s The scheduler.
145  * \param context Pointer to the filter node (task context).
146  *
147  * This looks at the status of the btr node of the filter. If data is available
148  * in the input queue of the filter, or if an error occurred, a minimal timeout
149  * for the next select call is requested from the scheduler. Otherwise the
150  * scheduler timeout is left unchanged.
151  */
152 void generic_filter_pre_select(struct sched *s, void *context)
153 {
154         struct filter_node *fn = context;
155
156         if (btr_node_status(fn->btrn, fn->min_iqs, BTR_NT_INTERNAL) != 0)
157                 sched_min_delay(s);
158 }
159
160 #ifdef WORDS_BIGENDIAN
161 #define DECODER_SAMPLE_FORMAT SF_S16_BE
162 #else
163 #define DECODER_SAMPLE_FORMAT SF_S16_LE
164 #endif
165
166 /**
167  * Execute a btr command for a decoder.
168  *
169  * The buffer tree nodes of the writers ask the parent nodes about sample_rate,
170  * channels count and sample format. This function is called by all decoders to
171  * answer these queries.
172  *
173  * \param cmd The command to be executed by the child node.
174  * \param sample_rate Known to the decoder.
175  * \param channels Known to the decoder.
176  * \param result Ascii representation on the answer is stored here.
177  *
178  * \return Standard.
179  */
180 int decoder_execute(const char *cmd, unsigned sample_rate, unsigned channels,
181                 char **result)
182 {
183         if (!strcmp(cmd, "sample_rate")) {
184                 if (sample_rate == 0)
185                         return -E_BTR_NAVAIL;
186                 *result = make_message("%u", sample_rate);
187                 return 1;
188         }
189         if (!strcmp(cmd, "channels")) {
190                 if (channels == 0)
191                         return -E_BTR_NAVAIL;
192                 *result = make_message("%u", channels);
193                 return 1;
194         }
195         if (!strcmp(cmd, "sample_format")) {
196                 *result = make_message("%u", DECODER_SAMPLE_FORMAT);
197                 return 1;
198         }
199         return -ERRNO_TO_PARA_ERROR(ENOTSUP);
200 }