]> git.tuebingen.mpg.de Git - paraslash.git/blob - write.c
paraslash 0.7.3
[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_monitor(struct sched *s, void *context)
58 {
59         struct write_task *wt = context;
60         check_wav_pre_monitor(s, wt->cwc);
61 }
62
63 static int write_post_monitor(__a_unused struct sched *s, void *context)
64 {
65         struct write_task *wt = context;
66         return check_wav_post_monitor(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_monitor = write_pre_monitor,
87                 .post_monitor = write_post_monitor,
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 = 10500;
100         ret = schedule(&s);
101         if (ret >= 0) {
102                 int j, ts;
103                 for (j = 0; j < n; j++) {
104                         struct writer_node *wn = wns + j;
105                         ts = task_status(wn->task);
106                         assert(ts < 0);
107                         if (ts != -E_WRITE_COMMON_EOF && ts != -E_BTR_EOF) {
108                                 const char *name = writer_name(wn->wid);
109                                 PARA_ERROR_LOG("%s: %s\n", name,
110                                         para_strerror(-ts));
111                                 if (ret >= 0)
112                                         ret = ts;
113                         }
114                 }
115         }
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));
122         }
123         free(wns);
124         check_wav_shutdown(wt.cwc);
125         sched_shutdown(&s);
126         return ret;
127 }
128
129 /**
130  * Para_write's main function.
131  *
132  * \param argc The usual argument counter.
133  * \param argv The usual argument vector.
134  *
135  * It sets up and starts the tasks and the buffer tree nodes determined by
136  * command line options.
137  *
138  * \return \p EXIT_SUCCESS or EXIT_FAILURE
139  */
140 int main(int argc, char *argv[])
141 {
142         int ret;
143         struct lls_parse_result *lpr;
144         char *errctx;
145
146         ret = lls(lls_parse(argc, argv, CMD_PTR, &lpr, &errctx));
147         if (ret < 0)
148                 goto out;
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);
154 out:
155         if (ret < 0) {
156                 if (errctx)
157                         PARA_ERROR_LOG("%s\n", errctx);
158                 free(errctx);
159                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
160         }
161         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
162 }