Merge branch 'maint'
[paraslash.git] / write.c
1 /*
2 * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file write.c Paraslash's standalone wav/raw player. */
8
9 #include <regex.h>
10 #include <sys/types.h>
11 #include <lopsub.h>
12
13 #include "write_cmd.lsg.h"
14 #include "write.lsg.h"
15 #include "para.h"
16 #include "string.h"
17 #include "list.h"
18 #include "sched.h"
19 #include "stdin.h"
20 #include "buffer_tree.h"
21 #include "write.h"
22 #include "fd.h"
23 #include "error.h"
24 #include "version.h"
25 #include "check_wav.h"
26
27 /** Array of error strings. */
28 DEFINE_PARA_ERRLIST;
29
30 #define CMD_PTR (lls_cmd(0, write_suite))
31 #define OPT_RESULT(_name, _lpr) \
32 (lls_opt_result(LSG_WRITE_PARA_WRITE_OPT_ ## _name, _lpr))
33 #define OPT_GIVEN(_name, _lpr) (lls_opt_given(OPT_RESULT(_name, _lpr)))
34 #define OPT_UINT32_VAL(_name, _lpr) (lls_uint32_val(0, OPT_RESULT(_name, _lpr)))
35
36 static struct stdin_task sit;
37 static int loglevel;
38 INIT_STDERR_LOGGING(loglevel)
39
40 static void handle_help_flag(struct lls_parse_result *lpr)
41 {
42 char *help;
43
44 if (OPT_GIVEN(DETAILED_HELP, lpr))
45 help = lls_long_help(CMD_PTR);
46 else if (OPT_GIVEN(HELP, lpr))
47 help = lls_short_help(CMD_PTR);
48 else
49 return;
50 printf("%s\n", help);
51 free(help);
52 print_writer_helps(OPT_GIVEN(DETAILED_HELP, lpr));
53 exit(EXIT_SUCCESS);
54 }
55
56 struct write_task {
57 struct task *task;
58 struct check_wav_context *cwc;
59 };
60
61 static void write_pre_select(struct sched *s, void *context)
62 {
63 struct write_task *wt = context;
64 check_wav_pre_select(s, wt->cwc);
65 }
66
67 static int write_post_select(__a_unused struct sched *s, void *context)
68 {
69 struct write_task *wt = context;
70 return check_wav_post_select(wt->cwc);
71 }
72
73 static int setup_and_schedule(struct lls_parse_result *lpr)
74 {
75 int i, n, ret, writer_given = OPT_GIVEN(WRITER, lpr);
76 struct btr_node *cw_btrn;
77 struct writer_node *wns;
78 static struct sched s;
79 struct wav_params wp;
80 struct write_task wt;
81
82 sit.btrn = btr_new_node(&(struct btr_node_description)
83 EMBRACE(.name = "stdin"));
84 stdin_task_register(&sit, &s);
85
86 LLS_COPY_WAV_PARMS(&wp, LSG_WRITE_PARA_WRITE, lpr);
87 wt.cwc = check_wav_init(sit.btrn, NULL, &wp, &cw_btrn);
88 wt.task = task_register(&(struct task_info) {
89 .name = "write",
90 .pre_select = write_pre_select,
91 .post_select = write_post_select,
92 .context = &wt,
93 }, &s);
94
95 n = writer_given? writer_given : 1;
96 wns = para_calloc(n * sizeof(*wns));
97 for (i = 0; i < n; i++) {
98 const char *arg = i < writer_given?
99 lls_string_val(i, OPT_RESULT(WRITER, lpr)) : NULL;
100 wns[i].wid = check_writer_arg_or_die(arg, &wns[i].lpr);
101 register_writer_node(wns + i, cw_btrn, &s);
102 }
103 s.default_timeout.tv_sec = 10;
104 s.default_timeout.tv_usec = 50000;
105 ret = schedule(&s);
106 if (ret >= 0) {
107 int j, ts;
108 for (j = 0; j < n; j++) {
109 struct writer_node *wn = wns + j;
110 ts = task_status(wn->task);
111 assert(ts < 0);
112 if (ts != -E_WRITE_COMMON_EOF && ts != -E_BTR_EOF) {
113 const char *name = writer_name(wn->wid);
114 PARA_ERROR_LOG("%s: %s\n", name,
115 para_strerror(-ts));
116 if (ret >= 0)
117 ret = ts;
118 }
119 }
120 }
121 for (i = n - 1; i >= 0; i--) {
122 struct writer_node *wn = wns + i;
123 writer_get(wn->wid)->close(wn);
124 btr_remove_node(&wn->btrn);
125 lls_free_parse_result(wns[i].lpr,
126 lls_cmd(wn->wid, write_cmd_suite));
127 }
128 free(wns);
129 check_wav_shutdown(wt.cwc);
130 sched_shutdown(&s);
131 return ret;
132 }
133
134 /**
135 * Para_write's main function.
136 *
137 * \param argc The usual argument counter.
138 * \param argv The usual argument vector.
139 *
140 * It sets up and starts the tasks and the buffer tree nodes determined by
141 * command line options.
142 *
143 * \return \p EXIT_SUCCESS or EXIT_FAILURE
144 */
145 int main(int argc, char *argv[])
146 {
147 int ret;
148 struct lls_parse_result *lpr;
149 char *errctx;
150
151 ret = lls(lls_parse(argc, argv, CMD_PTR, &lpr, &errctx));
152 if (ret < 0)
153 goto out;
154 loglevel = OPT_UINT32_VAL(LOGLEVEL, lpr);
155 version_handle_flag("write", OPT_GIVEN(VERSION, lpr));
156 handle_help_flag(lpr);
157 ret = setup_and_schedule(lpr);
158 lls_free_parse_result(lpr, CMD_PTR);
159 out:
160 if (ret < 0) {
161 if (errctx)
162 PARA_ERROR_LOG("%s\n", errctx);
163 free(errctx);
164 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
165 }
166 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
167 }