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. */
12 #include "filter.cmdline.h"
22 /** The list of all status items used by para_{server,audiod,gui}. */
23 const char *status_item_list
[] = {STATUS_ITEM_ARRAY
};
25 char *stat_item_values
[NUM_STAT_ITEMS
] = {NULL
};
27 /** Initialize the array of errors for para_filter. */
30 /** The task that reads from stdin. */
31 static struct stdin_task stdin_task_struct
;
32 /** pointer to the stdin task. */
33 static struct stdin_task
*sit
= &stdin_task_struct
;
35 /** The task that filters the data. */
36 static struct filter_chain filter_chain_struct
;
37 /** Pointer to the filter chain. */
38 static struct filter_chain
*fc
= &filter_chain_struct
;
40 /** The task that writes converted data to stdout. */
41 static struct stdout_task stdout_task_struct
;
42 /** Pointer to the stdout task. */
43 static struct stdout_task
*sot
= &stdout_task_struct
;
45 /** Gengetopt struct that holds the command line args. */
46 static struct filter_args_info conf
;
49 INIT_STDERR_LOGGING(loglevel
);
51 static void open_filters(void)
54 struct filter_node
*fn
;
56 FOR_EACH_FILTER_NODE(fn
, fc
, i
) {
57 struct filter
*f
= filters
+ fn
->filter_num
;
59 PARA_INFO_LOG("opened %s filter\n", f
->name
);
60 fc
->outbufp
= &fn
->buf
;
61 fc
->out_loaded
= &fn
->loaded
;
65 static int init_filter_chain(void)
68 struct filter_node
*fn
;
70 if (!conf
.filter_given
)
72 fc
->num_filters
= conf
.filter_given
;
73 fc
->filter_nodes
= para_malloc(fc
->num_filters
* sizeof(struct filter_node
));
74 fc
->inbufp
= &sit
->buf
;
75 fc
->in_loaded
= &sit
->loaded
;
76 fc
->input_error
= &sit
->task
.error
;
78 fc
->output_error
= &sot
->task
.error
;
79 fc
->task
.pre_select
= filter_pre_select
;
80 sprintf(fc
->task
.status
, "filter chain");
82 FOR_EACH_FILTER_NODE(fn
, fc
, i
) {
83 char *fa
= conf
.filter_arg
[i
];
84 fn
= fc
->filter_nodes
+ i
;
85 ret
= check_filter_arg(fa
, &fn
->conf
);
90 INIT_LIST_HEAD(&fn
->callbacks
);
91 PARA_DEBUG_LOG("filter #%d: %s\n", i
, filters
[fn
->filter_num
].name
);
96 free(fc
->filter_nodes
);
100 __noreturn
static void print_help_and_die(void)
102 int d
= conf
.detailed_help_given
;
103 const char **p
= d
? filter_args_info_detailed_help
104 : filter_args_info_help
;
106 printf_or_die("%s\n\n", FILTER_CMDLINE_PARSER_PACKAGE
"-"
107 FILTER_CMDLINE_PARSER_VERSION
);
108 printf_or_die("%s\n\n", filter_args_info_usage
);
110 printf_or_die("%s\n", *p
);
111 print_filter_helps(d
);
115 static int parse_config(int argc
, char *argv
[])
117 static char *cf
; /* config file */
120 if (filter_cmdline_parser(argc
, argv
, &conf
))
121 return -E_FILTER_SYNTAX
;
122 HANDLE_VERSION_FLAG("filter", conf
);
123 if (conf
.help_given
|| conf
.detailed_help_given
)
124 print_help_and_die();
126 char *home
= para_homedir();
127 cf
= make_message("%s/.paraslash/filter.conf", home
);
130 if (!stat(cf
, &statbuf
)) {
131 struct filter_cmdline_parser_params params
= {
137 if (filter_cmdline_parser_config_file(cf
, &conf
, ¶ms
))
138 return -E_FILTER_SYNTAX
;
144 * The main function of para_filter.
146 * Para_filter reads data from stdin, converts it by using a chain
147 * of filters (specified on the command line) and writes the resulting
150 * \param argc Number of command line options.
151 * \param argv Vector of arguments.
153 * \return \a EXIT_SUCCESS on success, EXIT_FAILURE on errors.
155 int main(int argc
, char *argv
[])
158 static struct sched s
;
160 stdin_set_defaults(sit
);
161 sit
->buf
= para_malloc(sit
->bufsize
),
164 ret
= parse_config(argc
, argv
);
167 loglevel
= get_loglevel_by_name(conf
.loglevel_arg
);
168 ret
= init_filter_chain();
171 sit
->output_error
= &fc
->task
.error
;
173 stdout_set_defaults(sot
);
174 sot
->bufp
= fc
->outbufp
;
175 sot
->loaded
= fc
->out_loaded
;
176 sot
->input_error
= &fc
->task
.error
;
178 register_task(&sit
->task
);
179 register_task(&fc
->task
);
180 register_task(&sot
->task
);
181 s
.default_timeout
.tv_sec
= 1;
182 s
.default_timeout
.tv_usec
= 0;
188 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
189 return ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
;