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