Merge branch 't/test_man'
[paraslash.git] / write.c
1 /*
2 * Copyright (C) 2005-2014 Andre Noll <maan@systemlinux.org>
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
12 #include "para.h"
13 #include "string.h"
14 #include "write.cmdline.h"
15 #include "list.h"
16 #include "sched.h"
17 #include "ggo.h"
18 #include "stdin.h"
19 #include "buffer_tree.h"
20 #include "write.h"
21 #include "write_common.h"
22 #include "fd.h"
23 #include "error.h"
24 #include "version.h"
25 #include "check_wav.h"
26
27 INIT_WRITE_ERRLISTS;
28
29 static struct write_args_info conf;
30
31 static struct stdin_task sit;
32
33 static int loglevel;
34 INIT_STDERR_LOGGING(loglevel)
35
36 __noreturn static void print_help_and_die(void)
37 {
38 struct ggo_help h = DEFINE_GGO_HELP(write);
39 bool d = conf.detailed_help_given;
40
41 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
42 print_writer_helps(d? GPH_MODULE_FLAGS_DETAILED : GPH_MODULE_FLAGS);
43 exit(0);
44 }
45
46 /*
47 * Parse config and register a task for a writer node.
48 *
49 * \param arg Command line arguments.
50 * \param parent The new node will be a child of \a parent.
51 * \param wn The writer node.
52 *
53 * If arg is \p NULL, the OS-dependent default writer is used with no
54 * arguments. The default writers are alsa for Linux, osx for OS X, oss for
55 * *BSD, and the file writer if the default writer is not supported.
56 *
57 * Once the writer configuration has been retrieved from the ->parse_config
58 * callback a writer node is created, its buffer tree node is added to the
59 * buffer tree as a child of the given parent.
60 *
61 * Finally, the new writer node's task structure is initialized and registered
62 * to the paraslash scheduler.
63 *
64 * \return Standard.
65 */
66 static void setup_writer_node(const char *arg, struct btr_node *parent,
67 struct writer_node *wn, struct sched *s)
68 {
69 wn->conf = check_writer_arg_or_die(arg, &wn->writer_num);
70 register_writer_node(wn, parent, s);
71 }
72
73 struct write_task {
74 struct task task;
75 struct check_wav_context *cwc;
76 };
77
78 static void write_pre_select(struct sched *s, struct task *t)
79 {
80 struct write_task *wt = container_of(t, struct write_task, task);
81 check_wav_pre_select(s, wt->cwc);
82 }
83
84 static int write_post_select(__a_unused struct sched *s, struct task *t)
85 {
86 struct write_task *wt = container_of(t, struct write_task, task);
87 return check_wav_post_select(wt->cwc);
88 }
89
90 static int setup_and_schedule(void)
91 {
92 int i, ret;
93 struct btr_node *cw_btrn;
94 struct writer_node *wns;
95 static struct sched s;
96 struct wav_params wp;
97 struct write_task wt = {
98 .task = {
99 .pre_select = write_pre_select,
100 .post_select = write_post_select,
101 .status = "write task",
102 },
103 };
104
105 sit.btrn = btr_new_node(&(struct btr_node_description)
106 EMBRACE(.name = "stdin"));
107 stdin_set_defaults(&sit);
108 register_task(&s, &sit.task);
109
110 COPY_WAV_PARMS(&wp, &conf);
111 wt.cwc = check_wav_init(sit.btrn, NULL, &wp, &cw_btrn);
112 register_task(&s, &wt.task);
113 if (!conf.writer_given) {
114 wns = para_calloc(sizeof(*wns));
115 setup_writer_node(NULL, cw_btrn, wns, &s);
116 i = 1;
117 } else {
118 wns = para_calloc(conf.writer_given * sizeof(*wns));
119 for (i = 0; i < conf.writer_given; i++)
120 setup_writer_node(conf.writer_arg[i], cw_btrn,
121 wns + i, &s);
122 }
123
124 s.default_timeout.tv_sec = 10;
125 s.default_timeout.tv_usec = 50000;
126 ret = schedule(&s);
127 if (ret >= 0) {
128 int j;
129 for (j = 0; j < i; j++) {
130 struct task *t = &wns[j].task;
131 assert(t->error < 0);
132 if (t->error != -E_WRITE_COMMON_EOF
133 && t->error != -E_BTR_EOF) {
134 PARA_ERROR_LOG("%s: %s\n", t->status,
135 para_strerror(-t->error));
136 if (ret >= 0)
137 ret = t->error;
138 }
139 }
140 }
141 for (i--; i >= 0; i--) {
142 struct writer_node *wn = wns + i;
143 struct writer *w = writers + wn->writer_num;
144
145 w->close(wn);
146 btr_remove_node(&wn->btrn);
147 w->free_config(wn->conf);
148 free(wn->conf);
149 }
150 free(wns);
151 check_wav_shutdown(wt.cwc);
152 return ret;
153 }
154
155 /**
156 * Para_write's main function.
157 *
158 * \param argc The usual argument counter.
159 * \param argv The usual argument vector.
160 *
161 * It sets up and starts the tasks and the buffer tree nodes determined by
162 * command line options.
163 *
164 * \return \p EXIT_SUCCESS or EXIT_FAILURE
165 */
166 int main(int argc, char *argv[])
167 {
168 int ret;
169
170 write_cmdline_parser(argc, argv, &conf);
171 loglevel = get_loglevel_by_name(conf.loglevel_arg);
172 writer_init();
173 version_handle_flag("write", conf.version_given);
174 if (conf.help_given || conf.detailed_help_given)
175 print_help_and_die();
176
177 ret = setup_and_schedule();
178 if (ret < 0) {
179 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
180 exit(EXIT_FAILURE);
181 }
182 exit(EXIT_SUCCESS);
183 }