afh/play: Include supported audio formats in help output.
[paraslash.git] / audioc.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 audioc.c The client program used to connect to para_audiod. */
8
9 #include <regex.h>
10 #include <sys/types.h>
11 #include <stdbool.h>
12 #include <signal.h>
13
14 #include "audioc.cmdline.h"
15 #include "para.h"
16 #include "error.h"
17 #include "net.h"
18 #include "string.h"
19 #include "fd.h"
20 #include "ggo.h"
21 #include "version.h"
22
23 INIT_AUDIOC_ERRLISTS;
24
25 /** The gengetopt structure containing command line args. */
26 static struct audioc_args_info conf;
27 static char *socket_name;
28
29
30 static int loglevel;
31 INIT_STDERR_LOGGING(loglevel);
32
33 static char *concat_args(unsigned argc, char * const *argv)
34 {
35         int i;
36         char *buf = NULL;
37
38         for (i = 0; i < argc; i++) {
39                 buf = para_strcat(buf, argv[i]);
40                 if (i != argc - 1)
41                         buf = para_strcat(buf, "\n");
42         }
43         return buf;
44 }
45
46 #ifdef HAVE_READLINE
47 #include "list.h"
48 #include "sched.h"
49 #include "buffer_tree.h"
50 #include "interactive.h"
51 #include "audiod_completion.h"
52
53 static struct sched sched;
54
55 struct audioc_task {
56         int fd;
57         struct btr_node *btrn;
58         struct task task;
59 };
60
61 static struct i9e_completer audiod_completers[];
62
63 I9E_DUMMY_COMPLETER(cycle);
64 I9E_DUMMY_COMPLETER(off);
65 I9E_DUMMY_COMPLETER(on);
66 I9E_DUMMY_COMPLETER(sb);
67 I9E_DUMMY_COMPLETER(tasks);
68 I9E_DUMMY_COMPLETER(term);
69
70 static void help_completer(struct i9e_completion_info *ci,
71                 struct i9e_completion_result *result)
72 {
73         result->matches = i9e_complete_commands(ci->word, audiod_completers);
74 }
75
76 static void stat_completer(struct i9e_completion_info *ci,
77                 struct i9e_completion_result *cr)
78 {
79         char *sia[] = {STATUS_ITEM_ARRAY NULL};
80         char *opts[] = {"-p", NULL};
81
82         if (ci->word_num <= 2 && ci->word && ci->word[0] == '-')
83                 i9e_complete_option(opts, ci, cr);
84         else
85                 i9e_extract_completions(ci->word, sia, &cr->matches);
86 }
87
88 static void grab_completer(struct i9e_completion_info *ci,
89                 struct i9e_completion_result *cr)
90 {
91         char *opts[] = {"-ms", "-ms", "-ma", "-p=", "-n=", "-o", NULL};
92         i9e_complete_option(opts, ci, cr);
93 }
94
95 static struct i9e_completer audiod_completers[] = {
96         AUDIOD_COMPLETERS
97         {.name = NULL}
98 };
99
100 static void audioc_pre_select(struct sched *s, struct task *t)
101 {
102         struct audioc_task *at = container_of(t, struct audioc_task, task);
103         int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
104
105         if (ret < 0)
106                 sched_min_delay(s);
107         para_fd_set(at->fd, &s->rfds, &s->max_fileno);
108 }
109
110 static int audioc_post_select(struct sched *s, struct task *t)
111 {
112         char *buf = NULL;
113         struct audioc_task *at = container_of(t, struct audioc_task, task);
114         int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
115
116         if (ret < 0)
117                 goto out;
118         if (!FD_ISSET(at->fd, &s->rfds))
119                 return 0;
120         buf = para_malloc(conf.bufsize_arg);
121         ret = recv_bin_buffer(at->fd, buf, conf.bufsize_arg);
122         PARA_DEBUG_LOG("recv: %d\n", ret);
123         if (ret == 0)
124                 ret = -E_AUDIOC_EOF;
125         if (ret < 0)
126                 goto out;
127         btr_add_output(buf, ret, at->btrn);
128         return 0;
129 out:
130         if (ret < 0) {
131                 free(buf);
132                 btr_remove_node(&at->btrn);
133                 close(at->fd);
134         }
135         return ret;
136 }
137
138 static struct audioc_task audioc_task = {
139         .task = {
140                 .pre_select = audioc_pre_select,
141                 .post_select = audioc_post_select,
142                 .status = "audioc task"
143         },
144 }, *at = &audioc_task;
145
146 static int audioc_i9e_line_handler(char *line)
147 {
148         char *args = NULL;
149         int ret;
150         if (!line || !*line)
151                 return 0;
152
153         PARA_DEBUG_LOG("line: %s\n", line);
154         ret = create_argv(line, " ", &conf.inputs);
155         if (ret < 0)
156                 return ret;
157         conf.inputs_num = ret;
158         args = concat_args(conf.inputs_num, conf.inputs);
159         free_argv(conf.inputs);
160         conf.inputs_num = 0; /* required for audioc_cmdline_parser_free() */
161         ret = connect_local_socket(socket_name);
162         if (ret < 0)
163                 goto out;
164         at->fd = ret;
165         ret = mark_fd_nonblocking(at->fd);
166         if (ret < 0)
167                 goto close;
168         ret = send_cred_buffer(at->fd, args);
169         if (ret < 0)
170                 goto close;
171         free(args);
172         args = NULL;
173         at->btrn = btr_new_node(&(struct btr_node_description)
174                 EMBRACE(.name = "audioc line handler"));
175         at->task.error = 0;
176         register_task(&sched, &at->task);
177         i9e_attach_to_stdout(at->btrn);
178         return 1;
179 close:
180         close(at->fd);
181 out:
182         free(args);
183         return ret;
184 }
185
186 __noreturn static void interactive_session(void)
187 {
188         int ret;
189         char *history_file;
190         struct sigaction act;
191         struct i9e_client_info ici = {
192                 .fds = {0, 1, 2},
193                 .prompt = "para_audioc> ",
194                 .line_handler = audioc_i9e_line_handler,
195                 .loglevel = loglevel,
196                 .completers = audiod_completers,
197         };
198         PARA_NOTICE_LOG("\n%s\n", version_text("audioc"));
199         if (conf.history_file_given)
200                 history_file = para_strdup(conf.history_file_arg);
201         else {
202                 char *home = para_homedir();
203                 history_file = make_message("%s/.paraslash/audioc.history",
204                         home);
205                 free(home);
206         }
207         ici.history_file = history_file;
208
209         act.sa_handler = i9e_signal_dispatch;
210         sigemptyset(&act.sa_mask);
211         act.sa_flags = 0;
212         sigaction(SIGINT, &act, NULL);
213         sched.select_function = i9e_select;
214
215         sched.default_timeout.tv_sec = 1;
216         ret = i9e_open(&ici, &sched);
217         if (ret < 0)
218                 goto out;
219         para_log = i9e_log;
220         ret = schedule(&sched);
221         i9e_close();
222         para_log = stderr_log;
223 out:
224         free(history_file);
225         audioc_cmdline_parser_free(&conf);
226         if (ret < 0)
227                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
228         exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
229 }
230
231 __noreturn static void print_completions(void)
232 {
233         int ret = i9e_print_completions(audiod_completers);
234         exit(ret <= 0? EXIT_FAILURE : EXIT_SUCCESS);
235 }
236
237 #else /* HAVE_READLINE */
238
239 __noreturn static void interactive_session(void)
240 {
241         PARA_EMERG_LOG("interactive sessions not available\n");
242         exit(EXIT_FAILURE);
243 }
244
245 __noreturn static void print_completions(void)
246 {
247         PARA_EMERG_LOG("command completion not available\n");
248         exit(EXIT_FAILURE);
249 }
250
251 #endif /* HAVE_READLINE */
252
253 static char *configfile_exists(void)
254 {
255         char *config_file;
256         struct stat statbuf;
257         char *home = para_homedir();
258
259         config_file = make_message("%s/.paraslash/audioc.conf", home);
260         free(home);
261         if (!stat(config_file, &statbuf))
262                 return config_file;
263         free(config_file);
264         return NULL;
265 }
266
267 __noreturn static void print_help_and_die(void)
268 {
269         struct ggo_help h = DEFINE_GGO_HELP(audioc);
270         bool d = conf.detailed_help_given;
271
272         ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
273         exit(0);
274 }
275
276 /**
277  * The client program to connect to para_audiod.
278  *
279  * \param argc Usual argument count.
280  * \param argv Usual argument vector.
281  *
282  * It connects to the "well-known" local socket to communicate with
283  * para_audiod. Authentication is performed by sending a ucred buffer
284  * containing the user id to the local socket.
285  *
286  * Any data received from the socket is written to stdout.
287  *
288  * \return EXIT_SUCCESS or EXIT_FAILURE.
289  *
290  * \sa send_cred_buffer(), para_audioc(1), para_audiod(1).
291  */
292 int main(int argc, char *argv[])
293 {
294         int ret, fd;
295         char *cf, *buf = NULL, *args = NULL;
296         size_t bufsize;
297
298         audioc_cmdline_parser(argc, argv, &conf);
299         loglevel = get_loglevel_by_name(conf.loglevel_arg);
300         version_handle_flag("audioc", conf.version_given);
301         if (conf.help_given || conf.detailed_help_given)
302                 print_help_and_die();
303         cf = configfile_exists();
304         if (cf) {
305                 struct audioc_cmdline_parser_params params = {
306                         .override = 0,
307                         .initialize = 0,
308                         .check_required = 0,
309                         .check_ambiguity = 0,
310                         .print_errors = 1,
311
312                 };
313                 audioc_cmdline_parser_config_file(cf, &conf, &params);
314                 free(cf);
315                 loglevel = get_loglevel_by_name(conf.loglevel_arg);
316         }
317         if (conf.socket_given)
318                 socket_name = para_strdup(conf.socket_arg);
319         else {
320                 char *hn = para_hostname();
321                 socket_name = make_message("/var/paraslash/audiod_socket.%s",
322                         hn);
323                 free(hn);
324         }
325
326         if (conf.complete_given)
327                 print_completions();
328
329         if (conf.inputs_num == 0)
330                 interactive_session();
331         args = concat_args(conf.inputs_num, conf.inputs);
332
333         ret = connect_local_socket(socket_name);
334         free(socket_name);
335         if (ret < 0) {
336                 PARA_EMERG_LOG("failed to connect to local socket\n");
337                 goto out;
338         }
339         fd = ret;
340         ret = send_cred_buffer(fd, args);
341         if (ret < 0)
342                 goto out;
343         bufsize = conf.bufsize_arg;
344         buf = para_malloc(bufsize);
345         ret = mark_fd_blocking(STDOUT_FILENO);
346         if (ret < 0)
347                 goto out;
348         do {
349                 size_t n = ret = recv_bin_buffer(fd, buf, bufsize);
350                 if (ret <= 0)
351                         break;
352                 ret = write_all(STDOUT_FILENO, buf, n);
353         } while (ret >= 0);
354 out:
355         free(buf);
356         free(args);
357         if (ret < 0)
358                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
359         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
360 }