Convert writers to lopsub.
[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 "para.h"
15 #include "string.h"
16 #include "write.cmdline.h"
17 #include "list.h"
18 #include "sched.h"
19 #include "ggo.h"
20 #include "stdin.h"
21 #include "buffer_tree.h"
22 #include "write.h"
23 #include "fd.h"
24 #include "error.h"
25 #include "version.h"
26 #include "check_wav.h"
27
28 /** Array of error strings. */
29 DEFINE_PARA_ERRLIST;
30
31 static struct write_args_info conf;
32
33 static struct stdin_task sit;
34
35 static int loglevel;
36 INIT_STDERR_LOGGING(loglevel)
37
38 __noreturn static void print_help_and_die(void)
39 {
40 struct ggo_help h = DEFINE_GGO_HELP(write);
41 bool d = conf.detailed_help_given;
42
43 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
44 print_writer_helps(d);
45 exit(0);
46 }
47
48 struct write_task {
49 struct task *task;
50 struct check_wav_context *cwc;
51 };
52
53 static void write_pre_select(struct sched *s, void *context)
54 {
55 struct write_task *wt = context;
56 check_wav_pre_select(s, wt->cwc);
57 }
58
59 static int write_post_select(__a_unused struct sched *s, void *context)
60 {
61 struct write_task *wt = context;
62 return check_wav_post_select(wt->cwc);
63 }
64
65 static int setup_and_schedule(void)
66 {
67 int i, n, ret;
68 struct btr_node *cw_btrn;
69 struct writer_node *wns;
70 static struct sched s;
71 struct wav_params wp;
72 struct write_task wt;
73
74 sit.btrn = btr_new_node(&(struct btr_node_description)
75 EMBRACE(.name = "stdin"));
76 stdin_task_register(&sit, &s);
77
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) {
81 .name = "write",
82 .pre_select = write_pre_select,
83 .post_select = write_post_select,
84 .context = &wt,
85 }, &s);
86
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);
93 }
94 s.default_timeout.tv_sec = 10;
95 s.default_timeout.tv_usec = 50000;
96 ret = schedule(&s);
97 if (ret >= 0) {
98 int j, ts;
99 for (j = 0; j < n; j++) {
100 struct writer_node *wn = wns + j;
101 ts = task_status(wn->task);
102 assert(ts < 0);
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,
106 para_strerror(-ts));
107 if (ret >= 0)
108 ret = ts;
109 }
110 }
111 }
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));
118 }
119 free(wns);
120 check_wav_shutdown(wt.cwc);
121 sched_shutdown(&s);
122 return ret;
123 }
124
125 /**
126 * Para_write's main function.
127 *
128 * \param argc The usual argument counter.
129 * \param argv The usual argument vector.
130 *
131 * It sets up and starts the tasks and the buffer tree nodes determined by
132 * command line options.
133 *
134 * \return \p EXIT_SUCCESS or EXIT_FAILURE
135 */
136 int main(int argc, char *argv[])
137 {
138 int ret;
139
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();
145
146 ret = setup_and_schedule();
147 if (ret < 0) {
148 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
149 exit(EXIT_FAILURE);
150 }
151 exit(EXIT_SUCCESS);
152 }