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