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