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