2 * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file write.c Paraslash's standalone wav/raw player. */
10 #include <sys/types.h>
13 #include "write_cmd.lsg.h"
16 #include "write.cmdline.h"
21 #include "buffer_tree.h"
26 #include "check_wav.h"
28 /** Array of error strings. */
31 static struct write_args_info conf;
33 static struct stdin_task sit;
36 INIT_STDERR_LOGGING(loglevel)
38 __noreturn static void print_help_and_die(void)
40 struct ggo_help h = DEFINE_GGO_HELP(write);
41 bool d = conf.detailed_help_given;
43 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
44 print_writer_helps(d);
50 struct check_wav_context *cwc;
53 static void write_pre_select(struct sched *s, void *context)
55 struct write_task *wt = context;
56 check_wav_pre_select(s, wt->cwc);
59 static int write_post_select(__a_unused struct sched *s, void *context)
61 struct write_task *wt = context;
62 return check_wav_post_select(wt->cwc);
65 static int setup_and_schedule(void)
68 struct btr_node *cw_btrn;
69 struct writer_node *wns;
70 static struct sched s;
74 sit.btrn = btr_new_node(&(struct btr_node_description)
75 EMBRACE(.name = "stdin"));
76 stdin_task_register(&sit, &s);
78 COPY_WAV_PARMS(&wp, &conf);
79 wt.cwc = check_wav_init(sit.btrn, NULL, &wp, &cw_btrn);
80 wt.task = task_register(&(struct task_info) {
82 .pre_select = write_pre_select,
83 .post_select = write_post_select,
87 n = conf.writer_given > 0? conf.writer_given : 1;
88 wns = para_calloc(n * sizeof(*wns));
89 for (i = 0; i < n; i++) {
90 char *arg = i < conf.writer_given? conf.writer_arg[i] : NULL;
91 wns[i].wid = check_writer_arg_or_die(arg, &wns[i].lpr);
92 register_writer_node(wns + i, cw_btrn, &s);
94 s.default_timeout.tv_sec = 10;
95 s.default_timeout.tv_usec = 50000;
99 for (j = 0; j < n; j++) {
100 struct writer_node *wn = wns + j;
101 ts = task_status(wn->task);
103 if (ts != -E_WRITE_COMMON_EOF && ts != -E_BTR_EOF) {
104 const char *name = writer_name(wn->wid);
105 PARA_ERROR_LOG("%s: %s\n", name,
112 for (i = n - 1; i >= 0; i--) {
113 struct writer_node *wn = wns + i;
114 writer_get(wn->wid)->close(wn);
115 btr_remove_node(&wn->btrn);
116 lls_free_parse_result(wns[i].lpr,
117 lls_cmd(wn->wid, write_cmd_suite));
120 check_wav_shutdown(wt.cwc);
126 * Para_write's main function.
128 * \param argc The usual argument counter.
129 * \param argv The usual argument vector.
131 * It sets up and starts the tasks and the buffer tree nodes determined by
132 * command line options.
134 * \return \p EXIT_SUCCESS or EXIT_FAILURE
136 int main(int argc, char *argv[])
140 write_cmdline_parser(argc, argv, &conf);
141 loglevel = get_loglevel_by_name(conf.loglevel_arg);
142 version_handle_flag("write", conf.version_given);
143 if (conf.help_given || conf.detailed_help_given)
144 print_help_and_die();
146 ret = setup_and_schedule();
148 PARA_ERROR_LOG("%s\n", para_strerror(-ret));