453824ace679f97feaa24c0d74b61ebdd83b049f
[paraslash.git] / write.c
1 /*
2 * Copyright (C) 2005-2012 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 static int setup_and_schedule(void)
79 {
80 int i, ret;
81 struct check_wav_task *cwt;
82 struct btr_node *cwt_btrn;
83 struct writer_node *wns;
84 static struct sched s;
85 struct wav_params wp;
86
87 loglevel = get_loglevel_by_name(conf.loglevel_arg);
88 sit.btrn = btr_new_node(&(struct btr_node_description)
89 EMBRACE(.name = "stdin"));
90 stdin_set_defaults(&sit);
91 register_task(&s, &sit.task);
92
93 COPY_WAV_PARMS(&wp, &conf);
94 cwt = check_wav_init(&s, sit.btrn, &wp, &cwt_btrn);
95 if (!conf.writer_given) {
96 wns = para_calloc(sizeof(*wns));
97 setup_writer_node(NULL, cwt_btrn, wns, &s);
98 i = 1;
99 } else {
100 wns = para_calloc(conf.writer_given * sizeof(*wns));
101 for (i = 0; i < conf.writer_given; i++)
102 setup_writer_node(conf.writer_arg[i], cwt_btrn,
103 wns + i, &s);
104 }
105
106 s.default_timeout.tv_sec = 10;
107 s.default_timeout.tv_usec = 50000;
108 ret = schedule(&s);
109 if (ret >= 0) {
110 int j;
111 for (j = 0; j < i; j++) {
112 struct task *t = &wns[j].task;
113 assert(t->error < 0);
114 if (t->error != -E_WRITE_COMMON_EOF
115 && t->error != -E_BTR_EOF) {
116 PARA_ERROR_LOG("%s: %s\n", t->status,
117 para_strerror(-t->error));
118 if (ret >= 0)
119 ret = t->error;
120 }
121 }
122 }
123 for (i--; i >= 0; i--) {
124 struct writer_node *wn = wns + i;
125 struct writer *w = writers + wn->writer_num;
126
127 w->close(wn);
128 btr_remove_node(&wn->btrn);
129 w->free_config(wn->conf);
130 free(wn->conf);
131 }
132 free(wns);
133 check_wav_shutdown(cwt);
134 return ret;
135 }
136
137 /**
138 * Para_write's main function.
139 *
140 * \param argc The usual argument counter.
141 * \param argv The usual argument vector.
142 *
143 * It sets up and starts the tasks and the buffer tree nodes determined by
144 * command line options.
145 *
146 * \return \p EXIT_SUCCESS or EXIT_FAILURE
147 */
148 int main(int argc, char *argv[])
149 {
150 int ret;
151
152 writer_init();
153 write_cmdline_parser(argc, argv, &conf);
154 HANDLE_VERSION_FLAG("write", conf);
155 if (conf.help_given || conf.detailed_help_given)
156 print_help_and_die();
157
158 ret = setup_and_schedule();
159 if (ret < 0) {
160 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
161 exit(EXIT_FAILURE);
162 }
163 exit(EXIT_SUCCESS);
164 }