8ebceb44fd574f9a06c91d9a4c23d897827fc9a0
[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 <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
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", FILTER_CMDLINE_PARSER_PACKAGE "-"
55 FILTER_CMDLINE_PARSER_VERSION);
56 printf_or_die("%s\n\n", filter_args_info_usage);
57 for (; *p; p++)
58 printf_or_die("%s\n", *p);
59 print_filter_helps(d);
60 exit(0);
61 }
62
63 static int parse_config(int argc, char *argv[])
64 {
65 static char *cf; /* config file */
66 struct stat statbuf;
67
68 if (filter_cmdline_parser(argc, argv, &conf))
69 return -E_FILTER_SYNTAX;
70 HANDLE_VERSION_FLAG("filter", conf);
71 if (conf.help_given || conf.detailed_help_given)
72 print_help_and_die();
73 loglevel = get_loglevel_by_name(conf.loglevel_arg);
74 if (!cf) {
75 char *home = para_homedir();
76 cf = make_message("%s/.paraslash/filter.conf", home);
77 free(home);
78 }
79 if (!stat(cf, &statbuf)) {
80 struct filter_cmdline_parser_params params = {
81 .override = 0,
82 .initialize = 0,
83 .check_required = 0,
84 .check_ambiguity = 0,
85 .print_errors = 1
86 };
87 if (filter_cmdline_parser_config_file(cf, &conf, &params))
88 return -E_FILTER_SYNTAX;
89 }
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 struct filter *f;
110 struct btr_node *parent;
111 struct filter_node **fns;
112
113 filter_init();
114 ret = parse_config(argc, argv);
115 if (ret < 0)
116 goto out;
117 sit->btrn = btr_new_node("stdin", NULL, NULL, NULL);
118 stdin_set_defaults(sit);
119 register_task(&sit->task);
120
121 fns = para_malloc(conf.filter_given * sizeof(*fns));
122 for (i = 0, parent = sit->btrn; i < conf.filter_given; i++) {
123 char *fa = conf.filter_arg[i];
124 struct filter_node *fn;
125
126 fn = fns[i] = para_calloc(sizeof(*fn));
127 ret = check_filter_arg(fa, &fn->conf);
128 if (ret < 0) {
129 free(fn);
130 goto out_cleanup;
131 }
132 fn->filter_num = ret;
133 f = filters + fn->filter_num;
134 sprintf(fn->task.status, "%s", f->name);
135 PARA_DEBUG_LOG("filter #%d: %s\n", i, f->name);
136 fn->btrn = btr_new_node(f->name, parent, f->execute, fn);
137 fn->task.pre_select = f->pre_select;
138 fn->task.post_select = f->post_select;
139 f->open(fn);
140 register_task(&fn->task);
141 parent = fn->btrn;
142 }
143 sot->btrn = btr_new_node("stdout", parent, NULL, NULL);
144 stdout_set_defaults(sot);
145 register_task(&sot->task);
146
147 s.default_timeout.tv_sec = 1;
148 s.default_timeout.tv_usec = 0;
149 btr_log_tree(sit->btrn, LL_INFO);
150 ret = schedule(&s);
151 out_cleanup:
152 for (i--; i >= 0; i--) {
153 struct filter_node *fn = fns[i];
154
155 f = filters + fn->filter_num;
156 f->close(fn);
157 btr_free_node(fn->btrn);
158 free(fn->conf);
159 free(fn);
160 }
161 free(fns);
162 btr_free_node(sit->btrn);
163 btr_free_node(sot->btrn);
164 out:
165 if (ret < 0)
166 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
167 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
168 }