2 * Copyright (C) 2005-2008 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"
20 char *stat_item_values
[NUM_STAT_ITEMS
] = {NULL
};
22 /** Initialize the array of errors for para_filter. */
25 /** The task that reads from stdin. */
26 static struct stdin_task stdin_task_struct
;
27 /** pointer to the stdin task. */
28 static struct stdin_task
*sit
= &stdin_task_struct
;
30 /** The task that filters the data. */
31 static struct filter_chain filter_chain_struct
;
32 /** Pointer to the filter chain. */
33 static struct filter_chain
*fc
= &filter_chain_struct
;
35 /** The task that writes converted data to stdout. */
36 static struct stdout_task stdout_task_struct
;
37 /** Pointer to the stdout task. */
38 static struct stdout_task
*sot
= &stdout_task_struct
;
40 /** Gengetopt struct that holds the command line args. */
41 static struct filter_args_info conf
;
43 INIT_STDERR_LOGGING(conf
.loglevel_arg
);
45 static void open_filters(void)
48 struct filter_node
*fn
;
50 FOR_EACH_FILTER_NODE(fn
, fc
, i
) {
51 struct filter
*f
= filters
+ fn
->filter_num
;
53 PARA_INFO_LOG("opened %s filter\n", f
->name
);
55 fc
->out_loaded
= &fn
->loaded
;
59 static int init_filter_chain(void)
62 struct filter_node
*fn
;
64 if (!conf
.filter_given
)
66 fc
->num_filters
= conf
.filter_given
;
67 fc
->filter_nodes
= para_malloc(fc
->num_filters
* sizeof(struct filter_node
));
69 fc
->in_loaded
= &sit
->loaded
;
70 fc
->input_error
= &sit
->task
.error
;
72 fc
->output_error
= &sot
->task
.error
;
73 fc
->task
.pre_select
= filter_pre_select
;
74 sprintf(fc
->task
.status
, "filter chain");
76 FOR_EACH_FILTER_NODE(fn
, fc
, i
) {
77 char *fa
= conf
.filter_arg
[i
];
78 fn
= fc
->filter_nodes
+ i
;
79 ret
= check_filter_arg(fa
, &fn
->conf
);
84 INIT_LIST_HEAD(&fn
->callbacks
);
85 PARA_DEBUG_LOG("filter #%d: %s\n", i
, filters
[fn
->filter_num
].name
);
90 free(fc
->filter_nodes
);
94 static int parse_config(int argc
, char *argv
[])
96 static char *cf
; /* config file */
100 if (filter_cmdline_parser(argc
, argv
, &conf
))
101 return -E_FILTER_SYNTAX
;
102 HANDLE_VERSION_FLAG("filter", conf
);
104 char *home
= para_homedir();
105 cf
= make_message("%s/.paraslash/filter.conf", home
);
108 if (!stat(cf
, &statbuf
)) {
109 struct filter_cmdline_parser_params params
= {
115 if (filter_cmdline_parser_config_file(cf
, &conf
, ¶ms
))
116 return -E_FILTER_SYNTAX
;
118 if (!conf
.list_filters_given
)
120 printf("available filters: ");
121 for (i
= 0; filters
[i
].name
; i
++)
122 printf("%s%s%s", i
? " " : "", filters
[i
].name
,
123 filters
[i
].parse_config
? "*": "");
124 printf("\nFilters marked with \"*\" have further command line options. Try\n"
125 "\tpara_filter -f '<filtername> -h'\nfor more information.\n");
130 * The main function of para_filter.
132 * Para_filter reads data from stdin, converts it by using a chain
133 * of filters (specified on the command line) and writes the resulting
136 * \param argc Number of command line options.
137 * \param argv Vector of arguments.
139 * \return \a EXIT_SUCCESS on success, EXIT_FAILURE on errors.
141 int main(int argc
, char *argv
[])
144 static struct sched s
;
146 stdin_set_defaults(sit
);
147 sit
->buf
= para_malloc(sit
->bufsize
),
149 filter_init(filters
);
150 ret
= parse_config(argc
, argv
);
153 ret
= init_filter_chain();
157 stdout_set_defaults(sot
);
158 sot
->buf
= fc
->outbuf
;
159 sot
->loaded
= fc
->out_loaded
;
160 sot
->input_error
= &fc
->task
.error
;
162 register_task(&sit
->task
);
163 register_task(&fc
->task
);
164 register_task(&sot
->task
);
165 s
.default_timeout
.tv_sec
= 1;
166 s
.default_timeout
.tv_usec
= 0;
172 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
173 return ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
;