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