more small audiod cleanups.
[paraslash.git] / filter.c
1 /*
2 * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18 /** \file filter.c the stand-alone filter program */
19
20 #include "para.h"
21
22 #include "filter.cmdline.h"
23 #include "list.h"
24 #include "sched.h"
25 #include "filter.h"
26 #include "string.h"
27 #include "stdin.h"
28 #include "stdout.h"
29 #include "error.h"
30
31 INIT_FILTER_ERRLISTS;
32
33 #define INBUF_SIZE 32 * 1024
34
35 static struct stdin_task stdin_task_struct;
36 static struct stdin_task *sit = &stdin_task_struct;
37 static struct filter_chain filter_chain_struct;
38 static struct filter_chain *fc = &filter_chain_struct;
39 static struct stdout_task stdout_task_struct;
40 static struct stdout_task *sot = &stdout_task_struct;
41
42 struct gengetopt_args_info conf;
43
44 __printf_2_3 void para_log(int ll, const char* fmt,...)
45 {
46 va_list argp;
47
48 /* ignore log message if loglevel is not high enough */
49 if (ll < conf.loglevel_arg)
50 return;
51 va_start(argp, fmt);
52 vfprintf(stderr, fmt, argp);
53 va_end(argp);
54 }
55
56 void filter_event_handler(struct task *t)
57 {
58 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
59 unregister_task(t);
60 }
61
62 static void open_filters(void)
63 {
64 struct filter_node *fn;
65
66 list_for_each_entry(fn, &fc->filters, node) {
67 fn->filter->open(fn);
68 PARA_INFO_LOG("opened %s filter\n", fn->filter->name);
69 fc->outbuf = fn->buf;
70 fc->out_loaded = &fn->loaded;
71 }
72 }
73
74
75 static int init_filter_chain(void)
76 {
77 int i, filter_num;
78 struct filter_node *fn;
79
80 INIT_LIST_HEAD(&fc->filters);
81
82 fc->inbuf = sit->buf;
83 fc->in_loaded = &sit->loaded;
84 fc->input_eof = &sit->eof;
85 fc->eof = 0;
86 fc->output_eof = &sot->eof;
87 fc->task.private_data = fc;
88 fc->task.pre_select = filter_pre_select;
89 fc->task.event_handler = filter_event_handler;
90 sprintf(fc->task.status, "filter chain");
91
92 for (i = 0; i < conf.filter_given; i++) {
93 char *fa = conf.filter_arg[i];
94 fn = para_calloc(sizeof(struct filter_node));
95 filter_num = check_filter_arg(fa, &fn->conf);
96 if (filter_num < 0) {
97 free(fn);
98 return filter_num;
99 }
100 fn->fc = fc;
101 INIT_LIST_HEAD(&fn->callbacks);
102 fn->filter = &filters[filter_num];
103 PARA_DEBUG_LOG("adding %s to filter chain\n", fn->filter->name);
104 list_add_tail(&fn->node, &fc->filters);
105 }
106 if (list_empty(&fc->filters))
107 return -E_NO_FILTERS;
108 open_filters();
109 return 1;
110 }
111
112 static int parse_config(int argc, char *argv[])
113 {
114 static char *cf; /* config file */
115 struct stat statbuf;
116 int i;
117
118 if (cmdline_parser(argc, argv, &conf))
119 return -E_FILTER_SYNTAX;
120 if (!cf) {
121 char *home = para_homedir();
122 cf = make_message("%s/.paraslash/filter.conf", home);
123 free(home);
124 }
125 if (!stat(cf, &statbuf)) {
126 if (cmdline_parser_configfile(cf, &conf, 0, 0, 0))
127 return -E_FILTER_SYNTAX;
128 }
129 if (!conf.list_filters_given)
130 return 1;
131 printf("available filters: ");
132 for (i = 0; filters[i].name; i++)
133 printf("%s%s%s", i? " " : "", filters[i].name,
134 filters[i].parse_config? "*": "");
135 printf("\nFilters marked with \"*\" have further command line options. Try\n"
136 "\tpara_filter -f '<filtername> -h'\nfor more information.\n");
137 exit(EXIT_SUCCESS);
138 }
139
140 int main(int argc, char *argv[])
141 {
142 int ret;
143 struct sched s;
144
145 init_sched();
146 stdin_set_defaults(sit);
147 sit->buf = para_malloc(sit->bufsize),
148
149 filter_init(filters);
150 ret = parse_config(argc, argv);
151 if (ret < 0)
152 goto out;
153 ret = init_filter_chain();
154 if (ret < 0)
155 goto out;
156
157 stdout_set_defaults(sot);
158 PARA_EMERG_LOG("fc->output_eof: %d\n", *fc->output_eof);
159 sot->buf = fc->outbuf;
160 sot->loaded = fc->out_loaded;
161 sot->input_eof = &fc->eof;
162
163 register_task(&sot->task);
164 register_task(&fc->task);
165 register_task(&sit->task);
166 s.default_timeout.tv_sec = 1;
167 s.default_timeout.tv_usec = 0;
168 PARA_EMERG_LOG("fc->output_eof: %d\n", *fc->output_eof);
169 ret = sched(&s);
170 out:
171 free(sit->buf);
172 close_filters(fc);
173 if (ret < 0)
174 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
175 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
176 }