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