Convert para_recv to lopsub.
[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);
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         struct lls_parse_result *filter_lpr;
113
114         filter_cmdline_parser(argc, argv, &conf); /* aborts on errors */
115         loglevel = get_loglevel_by_name(conf.loglevel_arg);
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                 const char *name;
127                 struct filter_node *fn;
128                 struct task_info ti;
129
130                 fn = fns[i] = para_calloc(sizeof(*fn));
131                 fn->filter_num = filter_setup(fa, &fn->conf, &filter_lpr);
132                 name = filter_name(fn->filter_num);
133                 fn->lpr = filter_lpr;
134                 PARA_DEBUG_LOG("filter #%d: %s\n", i, name);
135                 f = filter_get(fn->filter_num);
136                 fn->btrn = btr_new_node(&(struct btr_node_description)
137                         EMBRACE(.name = name, .parent = parent,
138                         .handler = f->execute, .context = fn));
139                 ti.name = name;
140                 ti.pre_select = f->pre_select;
141                 ti.post_select = f->post_select;
142                 ti.context = fn;
143                 if (f->open)
144                         f->open(fn);
145                 fn->task = task_register(&ti, &s);
146                 parent = fn->btrn;
147         }
148         sot->btrn = btr_new_node(&(struct btr_node_description)
149                 EMBRACE(.name = "stdout", .parent = parent));
150         stdout_task_register(sot, &s);
151
152         s.default_timeout.tv_sec = 1;
153         s.default_timeout.tv_usec = 0;
154         btr_log_tree(sit->btrn, LL_INFO);
155         ret = schedule(&s);
156         sched_shutdown(&s);
157         for (i--; i >= 0; i--) {
158                 struct filter_node *fn = fns[i];
159
160                 f = filter_get(fn->filter_num);
161                 if (f->close)
162                         f->close(fn);
163                 btr_remove_node(&fn->btrn);
164                 if (f->teardown)
165                         f->teardown(fn->lpr, fn->conf);
166                 free(fn);
167         }
168         free(fns);
169         btr_remove_node(&sit->btrn);
170         btr_remove_node(&sot->btrn);
171 out:
172         if (ret < 0)
173                 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
174         exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
175 }