audiod.c: Fix stat task restarting.
[paraslash.git] / filter.c
1 /*
2  * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6 /** \file filter.c the stand-alone filter program */
7
8 #include "para.h"
9
10 #include "filter.cmdline.h"
11 #include "list.h"
12 #include "sched.h"
13 #include "filter.h"
14 #include "string.h"
15 #include "stdin.h"
16 #include "stdout.h"
17 #include "error.h"
18
19 /** init the array of errors for para_filter */
20 INIT_FILTER_ERRLISTS;
21
22 /** the task that reads from stdin */
23 static struct stdin_task stdin_task_struct;
24 /** pointer to the stdin task */
25 static struct stdin_task *sit = &stdin_task_struct;
26
27 /** the task that filters the data */
28 static struct filter_chain filter_chain_struct;
29 /** pointer to the filter chain */
30 static struct filter_chain *fc = &filter_chain_struct;
31
32 /** the task that writes converted data to stdout */
33 static struct stdout_task stdout_task_struct;
34 /** pointer to the stdout task */
35 static struct stdout_task *sot = &stdout_task_struct;
36
37 /** gengetopt struct that holds the command line args */
38 static struct filter_args_info conf;
39
40 INIT_STDERR_LOGGING(conf.loglevel_arg);
41
42 static void open_filters(void)
43 {
44         struct filter_node *fn;
45
46         list_for_each_entry(fn, &fc->filters, node) {
47                 fn->filter->open(fn);
48                 PARA_INFO_LOG("opened %s filter\n", fn->filter->name);
49                 fc->outbuf = fn->buf;
50                 fc->out_loaded = &fn->loaded;
51         }
52 }
53
54 static int init_filter_chain(void)
55 {
56         int i, filter_num;
57         struct filter_node *fn;
58
59         INIT_LIST_HEAD(&fc->filters);
60
61         fc->inbuf = sit->buf;
62         fc->in_loaded = &sit->loaded;
63         fc->input_error = &sit->task.error;
64         fc->task.error = 0;
65         fc->output_error = &sot->task.error;
66         fc->task.pre_select = filter_pre_select;
67         sprintf(fc->task.status, "filter chain");
68
69         for (i = 0; i < conf.filter_given; i++) {
70                 char *fa = conf.filter_arg[i];
71                 fn = para_calloc(sizeof(struct filter_node));
72                 filter_num = check_filter_arg(fa, &fn->conf);
73                 if (filter_num < 0) {
74                         free(fn);
75                         return filter_num;
76                 }
77                 fn->fc = fc;
78                 INIT_LIST_HEAD(&fn->callbacks);
79                 fn->filter = &filters[filter_num];
80                 PARA_DEBUG_LOG("adding %s to filter chain\n", fn->filter->name);
81                 list_add_tail(&fn->node, &fc->filters);
82         }
83         if (list_empty(&fc->filters))
84                 return -E_NO_FILTERS;
85         open_filters();
86         return 1;
87 }
88
89 static int parse_config(int argc, char *argv[])
90 {
91         static char *cf; /* config file */
92         struct stat statbuf;
93         int i;
94
95         if (filter_cmdline_parser(argc, argv, &conf))
96                 return -E_FILTER_SYNTAX;
97         HANDLE_VERSION_FLAG("filter", conf);
98         if (!cf) {
99                 char *home = para_homedir();
100                 cf = make_message("%s/.paraslash/filter.conf", home);
101                 free(home);
102         }
103         if (!stat(cf, &statbuf)) {
104                 struct filter_cmdline_parser_params params = {
105                         .override = 0,
106                         .initialize = 0,
107                         .check_required = 0,
108                         .check_ambiguity = 0
109                 };
110                 if (filter_cmdline_parser_config_file(cf, &conf, &params))
111                         return -E_FILTER_SYNTAX;
112         }
113         if (!conf.list_filters_given)
114                 return 1;
115         printf("available filters: ");
116         for (i = 0; filters[i].name; i++)
117                 printf("%s%s%s", i? " " : "", filters[i].name,
118                         filters[i].parse_config? "*": "");
119         printf("\nFilters marked with \"*\" have further command line options. Try\n"
120                 "\tpara_filter -f '<filtername> -h'\nfor more information.\n");
121         exit(EXIT_SUCCESS);
122 }
123
124 /**
125  * para_filter's main function.
126  *
127  * para_filter reads data from stdin, converts it by using a chain
128  * of filters (specified on the command line) and writes the resulting
129  * data to stdout.
130  *
131  * \param argc number of command line options
132  * \param argv vector of arguments
133  *
134  * \return \a EXIT_SUCCESS on success, EXIT_FAILURE on errors.
135  */
136 int main(int argc, char *argv[])
137 {
138         int ret;
139         struct sched s;
140
141         stdin_set_defaults(sit);
142         sit->buf = para_malloc(sit->bufsize),
143
144         filter_init(filters);
145         ret = parse_config(argc, argv);
146         if (ret < 0)
147                 goto out;
148         ret = init_filter_chain();
149         if (ret < 0)
150                 goto out;
151
152         stdout_set_defaults(sot);
153         sot->buf = fc->outbuf;
154         sot->loaded = fc->out_loaded;
155         sot->input_error = &fc->task.error;
156
157         register_task(&sit->task);
158         register_task(&fc->task);
159         register_task(&sot->task);
160         s.default_timeout.tv_sec = 1;
161         s.default_timeout.tv_usec = 0;
162         ret = schedule(&s);
163 out:
164         free(sit->buf);
165         close_filters(fc);
166         if (ret < 0)
167                 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
168         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
169 }