2 * Copyright (C) 2005-2011 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file filter.c The stand-alone filter program. */
12 #include "filter.cmdline.h"
16 #include "buffer_tree.h"
24 /** The list of all status items used by para_{server,audiod,gui}. */
25 const char *status_item_list
[] = {STATUS_ITEM_ARRAY
};
27 char *stat_item_values
[NUM_STAT_ITEMS
] = {NULL
};
29 /** Initialize the array of errors for para_filter. */
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
;
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
;
42 /** Gengetopt struct that holds the command line args. */
43 static struct filter_args_info conf
;
46 INIT_STDERR_LOGGING(loglevel
);
48 __noreturn
static void print_help_and_die(void)
50 int d
= conf
.detailed_help_given
;
51 const char **p
= d
? filter_args_info_detailed_help
52 : filter_args_info_help
;
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
);
58 printf_or_die("%s\n", *p
);
59 print_filter_helps(d
);
63 static int parse_config(int argc
, char *argv
[])
65 static char *cf
; /* config file */
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
)
73 loglevel
= get_loglevel_by_name(conf
.loglevel_arg
);
75 char *home
= para_homedir();
76 cf
= make_message("%s/.paraslash/filter.conf", home
);
79 if (!stat(cf
, &statbuf
)) {
80 struct filter_cmdline_parser_params params
= {
87 if (filter_cmdline_parser_config_file(cf
, &conf
, ¶ms
))
88 return -E_FILTER_SYNTAX
;
90 if (!conf
.filter_given
)
96 * The main function of para_filter.
98 * Para_filter reads data from stdin, converts it by using a chain
99 * of filters (specified on the command line) and writes the resulting
102 * \param argc Number of command line options.
103 * \param argv Vector of arguments.
105 * \return \a EXIT_SUCCESS on success, EXIT_FAILURE on errors.
107 int main(int argc
, char *argv
[])
109 static struct sched s
;
112 struct btr_node
*parent
;
113 struct filter_node
**fns
;
116 ret
= parse_config(argc
, argv
);
119 sit
->btrn
= btr_new_node(&(struct btr_node_description
)
120 EMBRACE(.name
= "stdin"));
121 stdin_set_defaults(sit
);
122 register_task(&s
, &sit
->task
);
124 fns
= para_malloc(conf
.filter_given
* sizeof(*fns
));
125 for (i
= 0, parent
= sit
->btrn
; i
< conf
.filter_given
; i
++) {
126 char *fa
= conf
.filter_arg
[i
];
127 struct filter_node
*fn
;
129 fn
= fns
[i
] = para_calloc(sizeof(*fn
));
130 ret
= check_filter_arg(fa
, &fn
->conf
);
135 fn
->filter_num
= ret
;
136 f
= filters
+ fn
->filter_num
;
137 sprintf(fn
->task
.status
, "%s", f
->name
);
138 PARA_DEBUG_LOG("filter #%d: %s\n", i
, f
->name
);
139 fn
->btrn
= btr_new_node(&(struct btr_node_description
)
140 EMBRACE(.name
= f
->name
, .parent
= parent
,
141 .handler
= f
->execute
, .context
= fn
));
142 fn
->task
.pre_select
= f
->pre_select
;
143 fn
->task
.post_select
= f
->post_select
;
145 register_task(&s
, &fn
->task
);
148 sot
->btrn
= btr_new_node(&(struct btr_node_description
)
149 EMBRACE(.name
= "stdout", .parent
= parent
));
150 stdout_set_defaults(sot
);
151 register_task(&s
, &sot
->task
);
153 s
.default_timeout
.tv_sec
= 1;
154 s
.default_timeout
.tv_usec
= 0;
155 btr_log_tree(sit
->btrn
, LL_INFO
);
158 for (i
--; i
>= 0; i
--) {
159 struct filter_node
*fn
= fns
[i
];
161 f
= filters
+ fn
->filter_num
;
164 btr_free_node(fn
->btrn
);
169 btr_free_node(sit
->btrn
);
170 btr_free_node(sot
->btrn
);
173 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
174 exit(ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
);