Fix afs signal handling.
[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 filter_event_handler(struct task *t)
43 {
44         PARA_NOTICE_LOG("%s\n", para_strerror(-t->ret));
45         unregister_task(t);
46 }
47
48 static void open_filters(void)
49 {
50         struct filter_node *fn;
51
52         list_for_each_entry(fn, &fc->filters, node) {
53                 fn->filter->open(fn);
54                 PARA_INFO_LOG("opened %s filter\n", fn->filter->name);
55                 fc->outbuf = fn->buf;
56                 fc->out_loaded = &fn->loaded;
57         }
58 }
59
60 static int init_filter_chain(void)
61 {
62         int i, filter_num;
63         struct filter_node *fn;
64
65         INIT_LIST_HEAD(&fc->filters);
66
67         fc->inbuf = sit->buf;
68         fc->in_loaded = &sit->loaded;
69         fc->input_error = &sit->error;
70         fc->error = 0;
71         fc->output_error = &sot->error;
72         fc->task.private_data = fc;
73         fc->task.pre_select = filter_pre_select;
74         fc->task.event_handler = filter_event_handler;
75         sprintf(fc->task.status, "filter chain");
76
77         for (i = 0; i < conf.filter_given; i++) {
78                 char *fa = conf.filter_arg[i];
79                 fn = para_calloc(sizeof(struct filter_node));
80                 filter_num = check_filter_arg(fa, &fn->conf);
81                 if (filter_num < 0) {
82                         free(fn);
83                         return filter_num;
84                 }
85                 fn->fc = fc;
86                 INIT_LIST_HEAD(&fn->callbacks);
87                 fn->filter = &filters[filter_num];
88                 PARA_DEBUG_LOG("adding %s to filter chain\n", fn->filter->name);
89                 list_add_tail(&fn->node, &fc->filters);
90         }
91         if (list_empty(&fc->filters))
92                 return -E_NO_FILTERS;
93         open_filters();
94         return 1;
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 (i = 0; filters[i].name; 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  * para_filter's main function.
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         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
160         stdout_set_defaults(sot);
161         sot->buf = fc->outbuf;
162         sot->loaded = fc->out_loaded;
163         sot->input_error = &fc->error;
164
165         register_task(&sit->task);
166         register_task(&fc->task);
167         register_task(&sot->task);
168         s.default_timeout.tv_sec = 1;
169         s.default_timeout.tv_usec = 0;
170         ret = schedule(&s);
171 out:
172         free(sit->buf);
173         close_filters(fc);
174         if (ret < 0)
175                 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
176         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
177 }