audiod: Avoid busy loop in standby/off mode.
[paraslash.git] / filter_common.c
1 /*
2  * Copyright (C) 2005-2010 Andre Noll <maan@systemlinux.org>
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 #include <dirent.h>
12 #include <stdbool.h>
13
14 #include "para.h"
15 #include "list.h"
16 #include "sched.h"
17 #include "fd.h"
18 #include "ggo.h"
19 #include "buffer_tree.h"
20 #include "filter.h"
21 #include "error.h"
22 #include "string.h"
23
24 /** The array of supported filters. */
25 struct filter filters[NUM_SUPPORTED_FILTERS] = {FILTER_ARRAY};
26
27 /**
28  * Call the init function of each supported filter.
29  * \sa filter::init
30  */
31 void filter_init(void)
32 {
33         int i;
34
35         FOR_EACH_SUPPORTED_FILTER(i)
36                 filters[i].init(filters + i);
37 }
38
39 /*
40  * If the filter has a command line parser and options is not NULL, run it.
41  * Returns filter_num on success, negative on errors
42  */
43 static int parse_filter_args(int filter_num, char *options, void **conf)
44 {
45         struct filter *f = &filters[filter_num];
46         int ret, i, argc = 2;
47         char **argv;
48
49 //      PARA_DEBUG_LOG("%s, options: %s, parser: %p\n", f->name,
50 //              options? options : "(none)", f->parse_config);
51         if (!f->parse_config)
52                 return strlen(options)? -E_BAD_FILTER_OPTIONS : filter_num;
53 //      PARA_DEBUG_LOG("options: %s\n", options);
54         argc = create_argv(options, " \t", &argv);
55         if (argc < 0)
56                 return -E_BAD_FILTER_OPTIONS;
57         PARA_DEBUG_LOG("argc = %d, argv[0]: %s\n", argc, argv[0]);
58         for (i = argc - 1; i >= 0; i--)
59                 argv[i + 1] = argv[i];
60         argv[0] = para_strdup(f->name);
61         argc++;
62         ret = f->parse_config(argc, argv, conf);
63         free(argv[argc - 1]);
64         argv[argc - 1] = NULL;
65         free_argv(argv);
66         return ret < 0? ret : filter_num;
67 }
68
69 /**
70  * Check the filter command line options.
71  *
72  * \param fa The command line options.
73  * \param conf Points to the filter configuration upon successful return.
74  *
75  * Check if \a fa starts with a the name of a supported filter, followed by
76  * a colon. If yes, call the command line parser of that filter.
77  *
78  * \return On success, the number of the filter is returned and \a conf
79  * is initialized to point to the filter configuration determined by \a fa.
80  * On errors, a negative value is returned.
81  *
82  * Note: If \a fa specifies a filter that has no command line parser success is
83  * returned, and \a conf is initialized to \p NULL.
84  *
85  * \sa filter::parse_config
86  */
87 int check_filter_arg(char *fa, void **conf)
88 {
89         int j;
90
91         *conf = NULL;
92 //      PARA_DEBUG_LOG("arg: %s\n", fa);
93         FOR_EACH_SUPPORTED_FILTER(j) {
94                 const char *name = filters[j].name;
95                 size_t len = strlen(name);
96                 char c;
97                 if (strlen(fa) < len)
98                         continue;
99                 if (strncmp(name, fa, len))
100                         continue;
101                 c = fa[len];
102                 if (c && c != ' ')
103                         continue;
104                 if (c && !filters[j].parse_config)
105                         return -E_BAD_FILTER_OPTIONS;
106                 return parse_filter_args(j, c? fa + len + 1 :
107                         fa + strlen(fa), conf);
108         }
109         return -E_UNSUPPORTED_FILTER;
110 }
111
112 void print_filter_helps(int detailed)
113 {
114         int i;
115
116         printf_or_die("\nAvailable filters: \n\t");
117         FOR_EACH_SUPPORTED_FILTER(i)
118                 printf_or_die("%s%s", i? " " : "", filters[i].name);
119         printf_or_die("\n\n");
120
121         FOR_EACH_SUPPORTED_FILTER(i) {
122                 struct filter *f = filters + i;
123
124                 if (!f->help.short_help)
125                         continue;
126                 printf_or_die("Options for %s:\n", f->name);
127                 ggo_print_help(&f->help, detailed);
128         }
129 }
130
131 void generic_filter_pre_select(struct sched *s, struct task *t)
132 {
133         struct filter_node *fn = container_of(t, struct filter_node, task);
134
135         t->error = 0;
136         if (btr_node_status(fn->btrn, fn->min_iqs, BTR_NT_INTERNAL) != 0)
137                 sched_min_delay(s);
138 }