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