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