2 * Copyright (C) 2005-2009 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. */
11 #include "filter.cmdline.h"
21 /** The list of all status items used by para_{server,audiod,gui}. */
22 const char *status_item_list
[] = {STATUS_ITEM_ARRAY
};
24 char *stat_item_values
[NUM_STAT_ITEMS
] = {NULL
};
26 /** Initialize the array of errors for para_filter. */
29 /** The task that reads from stdin. */
30 static struct stdin_task stdin_task_struct
;
31 /** pointer to the stdin task. */
32 static struct stdin_task
*sit
= &stdin_task_struct
;
34 /** The task that filters the data. */
35 static struct filter_chain filter_chain_struct
;
36 /** Pointer to the filter chain. */
37 static struct filter_chain
*fc
= &filter_chain_struct
;
39 /** The task that writes converted data to stdout. */
40 static struct stdout_task stdout_task_struct
;
41 /** Pointer to the stdout task. */
42 static struct stdout_task
*sot
= &stdout_task_struct
;
44 /** Gengetopt struct that holds the command line args. */
45 static struct filter_args_info conf
;
47 INIT_STDERR_LOGGING(conf
.loglevel_arg
);
49 static void open_filters(void)
52 struct filter_node
*fn
;
54 FOR_EACH_FILTER_NODE(fn
, fc
, i
) {
55 struct filter
*f
= filters
+ fn
->filter_num
;
57 PARA_INFO_LOG("opened %s filter\n", f
->name
);
59 fc
->out_loaded
= &fn
->loaded
;
63 static int init_filter_chain(void)
66 struct filter_node
*fn
;
68 if (!conf
.filter_given
)
70 fc
->num_filters
= conf
.filter_given
;
71 fc
->filter_nodes
= para_malloc(fc
->num_filters
* sizeof(struct filter_node
));
73 fc
->in_loaded
= &sit
->loaded
;
74 fc
->input_error
= &sit
->task
.error
;
76 fc
->output_error
= &sot
->task
.error
;
77 fc
->task
.pre_select
= filter_pre_select
;
78 sprintf(fc
->task
.status
, "filter chain");
80 FOR_EACH_FILTER_NODE(fn
, fc
, i
) {
81 char *fa
= conf
.filter_arg
[i
];
82 fn
= fc
->filter_nodes
+ i
;
83 ret
= check_filter_arg(fa
, &fn
->conf
);
88 INIT_LIST_HEAD(&fn
->callbacks
);
89 PARA_DEBUG_LOG("filter #%d: %s\n", i
, filters
[fn
->filter_num
].name
);
94 free(fc
->filter_nodes
);
98 __noreturn
static void print_help_and_die(void)
100 int d
= conf
.detailed_help_given
;
101 const char **p
= d
? filter_args_info_detailed_help
102 : filter_args_info_help
;
104 printf_or_die("%s\n\n", FILTER_CMDLINE_PARSER_PACKAGE
"-"
105 FILTER_CMDLINE_PARSER_VERSION
);
106 printf_or_die("%s\n\n", filter_args_info_usage
);
108 printf_or_die("%s\n", *p
);
109 print_filter_helps(d
);
113 static int parse_config(int argc
, char *argv
[])
115 static char *cf
; /* config file */
118 if (filter_cmdline_parser(argc
, argv
, &conf
))
119 return -E_FILTER_SYNTAX
;
120 HANDLE_VERSION_FLAG("filter", conf
);
121 if (conf
.help_given
|| conf
.detailed_help_given
)
122 print_help_and_die();
124 char *home
= para_homedir();
125 cf
= make_message("%s/.paraslash/filter.conf", home
);
128 if (!stat(cf
, &statbuf
)) {
129 struct filter_cmdline_parser_params params
= {
135 if (filter_cmdline_parser_config_file(cf
, &conf
, ¶ms
))
136 return -E_FILTER_SYNTAX
;
142 * The main function of para_filter.
144 * Para_filter reads data from stdin, converts it by using a chain
145 * of filters (specified on the command line) and writes the resulting
148 * \param argc Number of command line options.
149 * \param argv Vector of arguments.
151 * \return \a EXIT_SUCCESS on success, EXIT_FAILURE on errors.
153 int main(int argc
, char *argv
[])
156 static struct sched s
;
158 stdin_set_defaults(sit
);
159 sit
->buf
= para_malloc(sit
->bufsize
),
161 filter_init(filters
);
162 ret
= parse_config(argc
, argv
);
165 ret
= init_filter_chain();
168 sit
->output_error
= &fc
->task
.error
;
170 stdout_set_defaults(sot
);
171 sot
->buf
= fc
->outbuf
;
172 sot
->loaded
= fc
->out_loaded
;
173 sot
->input_error
= &fc
->task
.error
;
175 register_task(&sit
->task
);
176 register_task(&fc
->task
);
177 register_task(&sot
->task
);
178 s
.default_timeout
.tv_sec
= 1;
179 s
.default_timeout
.tv_usec
= 0;
185 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
186 return ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
;