1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file write.c Paraslash's standalone wav/raw player. */
9 #include "write_cmd.lsg.h"
10 #include "write.lsg.h"
16 #include "buffer_tree.h"
21 #include "check_wav.h"
23 /** Array of error strings. */
26 #define CMD_PTR (lls_cmd(0, write_suite))
27 #define OPT_RESULT(_name, _lpr) \
28 (lls_opt_result(LSG_WRITE_PARA_WRITE_OPT_ ## _name, _lpr))
29 #define OPT_GIVEN(_name, _lpr) (lls_opt_given(OPT_RESULT(_name, _lpr)))
30 #define OPT_UINT32_VAL(_name, _lpr) (lls_uint32_val(0, OPT_RESULT(_name, _lpr)))
32 static struct stdin_task sit;
34 INIT_STDERR_LOGGING(loglevel)
36 static void handle_help_flag(struct lls_parse_result *lpr)
40 if (OPT_GIVEN(DETAILED_HELP, lpr))
41 help = lls_long_help(CMD_PTR);
42 else if (OPT_GIVEN(HELP, lpr))
43 help = lls_short_help(CMD_PTR);
48 print_writer_helps(OPT_GIVEN(DETAILED_HELP, lpr));
54 struct check_wav_context *cwc;
57 static void write_pre_monitor(struct sched *s, void *context)
59 struct write_task *wt = context;
60 check_wav_pre_monitor(s, wt->cwc);
63 static int write_post_monitor(__a_unused struct sched *s, void *context)
65 struct write_task *wt = context;
66 return check_wav_post_monitor(wt->cwc);
69 static int setup_and_schedule(struct lls_parse_result *lpr)
71 int i, n, ret, writer_given = OPT_GIVEN(WRITER, lpr);
72 struct btr_node *cw_btrn;
73 struct writer_node *wns;
74 static struct sched s;
78 sit.btrn = btr_new_node(&(struct btr_node_description)
79 EMBRACE(.name = "stdin"));
80 stdin_task_register(&sit, &s);
82 LLS_COPY_WAV_PARMS(&wp, LSG_WRITE_PARA_WRITE, lpr);
83 wt.cwc = check_wav_init(sit.btrn, NULL, &wp, &cw_btrn);
84 wt.task = task_register(&(struct task_info) {
86 .pre_monitor = write_pre_monitor,
87 .post_monitor = write_post_monitor,
91 n = writer_given? writer_given : 1;
92 wns = arr_zalloc(n, sizeof(*wns));
93 for (i = 0; i < n; i++) {
94 const char *arg = i < writer_given?
95 lls_string_val(i, OPT_RESULT(WRITER, lpr)) : NULL;
96 wns[i].wid = check_writer_arg_or_die(arg, &wns[i].lpr);
97 register_writer_node(wns + i, cw_btrn, &s);
99 s.default_timeout = 10500;
103 for (j = 0; j < n; j++) {
104 struct writer_node *wn = wns + j;
105 ts = task_status(wn->task);
108 const char *name = writer_name(wn->wid);
109 PARA_ERROR_LOG("%s: %s\n", name,
116 for (i = n - 1; i >= 0; i--) {
117 struct writer_node *wn = wns + i;
118 writer_get(wn->wid)->close(wn);
119 btr_remove_node(&wn->btrn);
120 lls_free_parse_result(wns[i].lpr,
121 lls_cmd(wn->wid, write_cmd_suite));
124 check_wav_shutdown(wt.cwc);
130 * Para_write's main function.
132 * \param argc The usual argument counter.
133 * \param argv The usual argument vector.
135 * It sets up and starts the tasks and the buffer tree nodes determined by
136 * command line options.
138 * \return \p EXIT_SUCCESS or EXIT_FAILURE
140 int main(int argc, char *argv[])
143 struct lls_parse_result *lpr;
146 ret = lls(lls_parse(argc, argv, CMD_PTR, &lpr, &errctx));
149 loglevel = OPT_UINT32_VAL(LOGLEVEL, lpr);
150 version_handle_flag("write", OPT_GIVEN(VERSION, lpr));
151 handle_help_flag(lpr);
152 ret = setup_and_schedule(lpr);
153 lls_free_parse_result(lpr, CMD_PTR);
157 PARA_ERROR_LOG("%s\n", errctx);
159 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
161 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;