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