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