aacdec: Prefer NeAACDecInit() over NeAACDecInit2().
[paraslash.git] / filter.c
1 /*
2  * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
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 "buffer_tree.h"
17 #include "filter.h"
18 #include "string.h"
19 #include "stdin.h"
20 #include "stdout.h"
21 #include "error.h"
22 #include "version.h"
23
24 /** Array of error strings. */
25 DEFINE_PARA_ERRLIST;
26
27 /** The list of all status items used by para_{server,audiod,gui}. */
28 const char *status_item_list[] = {STATUS_ITEM_ARRAY};
29
30 /**
31  * Dummy version which only contains NULL pointers.
32  *
33  * This is used by the amp filter which first tries to obtain the amplification
34  * value from an element in this array.
35  */
36 char *stat_item_values[NUM_STAT_ITEMS] = {NULL};
37
38 /** The task that reads from stdin. */
39 static struct stdin_task stdin_task_struct;
40 /** pointer to the stdin task. */
41 static struct stdin_task *sit = &stdin_task_struct;
42
43 /** The task that writes converted data to stdout. */
44 static struct stdout_task stdout_task_struct;
45 /** Pointer to the stdout task. */
46 static struct stdout_task *sot = &stdout_task_struct;
47
48 /** Gengetopt struct that holds the command line args. */
49 static struct filter_args_info conf;
50
51 static int loglevel;
52 INIT_STDERR_LOGGING(loglevel);
53
54 __noreturn static void print_help_and_die(void)
55 {
56         struct ggo_help h = DEFINE_GGO_HELP(filter);
57         bool d = conf.detailed_help_given;
58
59         ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
60         print_filter_helps(d? GPH_MODULE_FLAGS_DETAILED : GPH_MODULE_FLAGS);
61         exit(EXIT_SUCCESS);
62 }
63
64 static int parse_config(void)
65 {
66         static char *cf; /* config file */
67         struct stat statbuf;
68
69         version_handle_flag("filter", conf.version_given);
70         if (conf.help_given || conf.detailed_help_given)
71                 print_help_and_die();
72         if (!cf) {
73                 char *home = para_homedir();
74                 cf = make_message("%s/.paraslash/filter.conf", home);
75                 free(home);
76         }
77         if (!stat(cf, &statbuf)) {
78                 struct filter_cmdline_parser_params params = {
79                         .override = 0,
80                         .initialize = 0,
81                         .check_required = 0,
82                         .check_ambiguity = 0,
83                         .print_errors = 1
84                 };
85                 filter_cmdline_parser_config_file(cf, &conf, &params);
86                 loglevel = get_loglevel_by_name(conf.loglevel_arg);
87         }
88         if (!conf.filter_given)
89                 return -E_NO_FILTERS;
90         return 1;
91 }
92
93 /**
94  * The main function of para_filter.
95  *
96  * Para_filter reads data from stdin, converts it by using a chain
97  * of filters (specified on the command line) and writes the resulting
98  * data to stdout.
99  *
100  * \param argc Number of command line options.
101  * \param argv Vector of arguments.
102  *
103  * \return \a EXIT_SUCCESS on success, EXIT_FAILURE on errors.
104  */
105 int main(int argc, char *argv[])
106 {
107         static struct sched s;
108         int i, ret;
109         const struct filter *f;
110         struct btr_node *parent;
111         struct filter_node **fns;
112
113         filter_cmdline_parser(argc, argv, &conf); /* aborts on errors */
114         loglevel = get_loglevel_by_name(conf.loglevel_arg);
115         filter_init();
116         ret = parse_config();
117         if (ret < 0)
118                 goto out;
119         sit->btrn = btr_new_node(&(struct btr_node_description)
120                 EMBRACE(.name = "stdin"));
121         stdin_task_register(sit, &s);
122
123         fns = para_malloc(conf.filter_given * sizeof(*fns));
124         for (i = 0, parent = sit->btrn; i < conf.filter_given; i++) {
125                 char *fa = conf.filter_arg[i];
126                 struct filter_node *fn;
127                 struct task_info ti;
128
129                 fn = fns[i] = para_calloc(sizeof(*fn));
130                 ret = check_filter_arg(fa, &fn->conf);
131                 if (ret < 0) {
132                         free(fn);
133                         goto out_cleanup;
134                 }
135                 fn->filter_num = ret;
136                 f = filter_get(fn->filter_num);
137                 PARA_DEBUG_LOG("filter #%d: %s\n", i, f->name);
138                 fn->btrn = btr_new_node(&(struct btr_node_description)
139                         EMBRACE(.name = f->name, .parent = parent,
140                         .handler = f->execute, .context = fn));
141                 ti.name = f->name;
142                 ti.pre_select = f->pre_select;
143                 ti.post_select = f->post_select;
144                 ti.context = fn;
145                 if (f->open)
146                         f->open(fn);
147                 fn->task = task_register(&ti, &s);
148                 parent = fn->btrn;
149         }
150         sot->btrn = btr_new_node(&(struct btr_node_description)
151                 EMBRACE(.name = "stdout", .parent = parent));
152         stdout_task_register(sot, &s);
153
154         s.default_timeout.tv_sec = 1;
155         s.default_timeout.tv_usec = 0;
156         btr_log_tree(sit->btrn, LL_INFO);
157         ret = schedule(&s);
158         sched_shutdown(&s);
159 out_cleanup:
160         for (i--; i >= 0; i--) {
161                 struct filter_node *fn = fns[i];
162
163                 f = filter_get(fn->filter_num);
164                 if (f->close)
165                         f->close(fn);
166                 btr_remove_node(&fn->btrn);
167                 if (f->free_config)
168                         f->free_config(fn->conf);
169                 free(fn);
170         }
171         free(fns);
172         btr_remove_node(&sit->btrn);
173         btr_remove_node(&sot->btrn);
174 out:
175         if (ret < 0)
176                 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
177         exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
178 }