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