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