Remove man_util.bash.
[paraslash.git] / audioc.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 audioc.c The client program used to connect to para_audiod. */
8
9 #include <netinet/in.h>
10 #include <sys/socket.h>
11 #include <regex.h>
12 #include <sys/types.h>
13 #include <arpa/inet.h>
14 #include <sys/un.h>
15 #include <netdb.h>
16 #include <signal.h>
17 #include <lopsub.h>
18
19 #include "audiod_cmd.lsg.h"
20 #include "audioc.lsg.h"
21
22 #include "para.h"
23 #include "error.h"
24 #include "net.h"
25 #include "string.h"
26 #include "fd.h"
27 #include "version.h"
28
29 /** Array of error strings. */
30 DEFINE_PARA_ERRLIST;
31
32 static char *socket_name;
33 static struct lls_parse_result *lpr;
34
35 #define CMD_PTR (lls_cmd(0, audioc_suite))
36 #define OPT_RESULT(_name) \
37         (lls_opt_result(LSG_AUDIOC_PARA_AUDIOC_OPT_ ## _name, lpr))
38 #define OPT_GIVEN(_name) (lls_opt_given(OPT_RESULT(_name)))
39 #define OPT_STRING_VAL(_name) (lls_string_val(0, OPT_RESULT(_name)))
40 #define OPT_UINT32_VAL(_name) (lls_uint32_val(0, OPT_RESULT(_name)))
41
42 static int loglevel;
43 INIT_STDERR_LOGGING(loglevel);
44
45 static char *concat_args(unsigned argc, char * const *argv)
46 {
47         int i;
48         char *buf = NULL;
49
50         for (i = 0; i < argc; i++) {
51                 const char *arg = argv? argv[i] : lls_input(i, lpr);
52                 buf = para_strcat(buf, arg);
53                 if (i != argc - 1)
54                         buf = para_strcat(buf, "\n");
55         }
56         return buf;
57 }
58
59 static int connect_audiod(const char *sname, char *args)
60 {
61         int fd = -1, ret;
62
63         ret = connect_local_socket(sname);
64         if (ret < 0)
65                 goto fail;
66         fd = ret;
67         ret = send_cred_buffer(fd, args);
68         if (ret < 0)
69                 goto fail;
70         return fd;
71 fail:
72         PARA_ERROR_LOG("could not connect %s\n", sname);
73         if (fd >= 0)
74                 close(fd);
75         return ret;
76 }
77
78 #ifdef HAVE_READLINE
79 #include "list.h"
80 #include "sched.h"
81 #include "buffer_tree.h"
82 #include "interactive.h"
83
84 static struct sched sched;
85
86 struct audioc_task {
87         int fd;
88         struct btr_node *btrn;
89         struct task *task;
90 };
91
92 static struct i9e_completer audiod_completers[];
93
94 I9E_DUMMY_COMPLETER(cycle);
95 I9E_DUMMY_COMPLETER(off);
96 I9E_DUMMY_COMPLETER(on);
97 I9E_DUMMY_COMPLETER(sb);
98 I9E_DUMMY_COMPLETER(tasks);
99 I9E_DUMMY_COMPLETER(term);
100
101 static void help_completer(struct i9e_completion_info *ci,
102                 struct i9e_completion_result *result)
103 {
104         result->matches = i9e_complete_commands(ci->word, audiod_completers);
105 }
106
107 static void version_completer(struct i9e_completion_info *ci,
108                 struct i9e_completion_result *cr)
109 {
110         char *opts[] = {LSG_AUDIOD_CMD_VERSION_OPTS, NULL};
111
112         if (ci->word_num <= 2 && ci->word && ci->word[0] == '-')
113                 i9e_complete_option(opts, ci, cr);
114 }
115
116 static void stat_completer(struct i9e_completion_info *ci,
117                 struct i9e_completion_result *cr)
118 {
119         char *sia[] = {STATUS_ITEM_ARRAY NULL};
120         char *opts[] = {LSG_AUDIOD_CMD_STAT_OPTS, NULL};
121
122         if (ci->word_num <= 2 && ci->word && ci->word[0] == '-')
123                 i9e_complete_option(opts, ci, cr);
124         else
125                 i9e_extract_completions(ci->word, sia, &cr->matches);
126 }
127
128 static void grab_completer(struct i9e_completion_info *ci,
129                 struct i9e_completion_result *cr)
130 {
131         char *opts[] = {LSG_AUDIOD_CMD_GRAB_OPTS, NULL};
132         i9e_complete_option(opts, ci, cr);
133 }
134
135 I9E_DUMMY_COMPLETER(SUPERCOMMAND_UNAVAILABLE);
136 static struct i9e_completer audiod_completers[] = {
137 #define LSG_AUDIOD_CMD_CMD(_name) {.name = #_name, \
138         .completer = _name ## _completer}
139         LSG_AUDIOD_CMD_SUBCOMMANDS
140 #undef LSG_AUDIOD_CMD_CMD
141         {.name = NULL}
142 };
143
144 static void audioc_pre_select(struct sched *s, void *context)
145 {
146         struct audioc_task *at = context;
147         int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
148
149         if (ret < 0)
150                 sched_min_delay(s);
151         para_fd_set(at->fd, &s->rfds, &s->max_fileno);
152 }
153
154 static int audioc_post_select(struct sched *s, void *context)
155 {
156         char *buf = NULL;
157         struct audioc_task *at = context;
158         int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
159         size_t bufsize;
160
161         if (ret < 0)
162                 goto out;
163         if (!FD_ISSET(at->fd, &s->rfds))
164                 return 0;
165         bufsize = PARA_MAX(1024U, OPT_UINT32_VAL(BUFSIZE));
166         buf = para_malloc(bufsize);
167         ret = recv_bin_buffer(at->fd, buf, bufsize);
168         PARA_DEBUG_LOG("recv: %d\n", ret);
169         if (ret == 0)
170                 ret = -E_AUDIOC_EOF;
171         if (ret < 0)
172                 goto out;
173         btr_add_output(buf, ret, at->btrn);
174         return 0;
175 out:
176         if (ret < 0) {
177                 free(buf);
178                 btr_remove_node(&at->btrn);
179                 close(at->fd);
180         }
181         return ret;
182 }
183
184 static struct audioc_task audioc_task, *at = &audioc_task;
185
186 static int audioc_i9e_line_handler(char *line)
187 {
188         int argc, ret;
189         char *args, **argv;
190
191         PARA_DEBUG_LOG("line: %s\n", line);
192         ret = create_argv(line, " ", &argv);
193         if (ret < 0)
194                 return ret;
195         argc = ret;
196         args = concat_args(argc, argv);
197         free_argv(argv);
198         if (!args)
199                 return 0;
200         ret = connect_audiod(socket_name, args);
201         free(args);
202         if (ret < 0)
203                 goto out;
204         at->fd = ret;
205         ret = mark_fd_nonblocking(at->fd);
206         if (ret < 0)
207                 goto close;
208         at->btrn = btr_new_node(&(struct btr_node_description)
209                 EMBRACE(.name = "audioc line handler"));
210         at->task = task_register(&(struct task_info) {
211                 .name = "audioc",
212                 .pre_select = audioc_pre_select,
213                 .post_select = audioc_post_select,
214                 .context = at,
215         }, &sched);
216         i9e_attach_to_stdout(at->btrn);
217         return 1;
218 close:
219         close(at->fd);
220 out:
221         free(args);
222         return ret;
223 }
224
225 __noreturn static void interactive_session(void)
226 {
227         int ret;
228         char *history_file;
229         struct sigaction act;
230         struct i9e_client_info ici = {
231                 .fds = {0, 1, 2},
232                 .prompt = "para_audioc> ",
233                 .line_handler = audioc_i9e_line_handler,
234                 .loglevel = loglevel,
235                 .completers = audiod_completers,
236         };
237
238         PARA_NOTICE_LOG("\n%s\n", version_text("audioc"));
239         if (OPT_GIVEN(HISTORY_FILE))
240                 history_file = para_strdup(OPT_STRING_VAL(HISTORY_FILE));
241         else {
242                 char *home = para_homedir();
243                 history_file = make_message("%s/.paraslash/audioc.history",
244                         home);
245                 free(home);
246         }
247         ici.history_file = history_file;
248
249         act.sa_handler = i9e_signal_dispatch;
250         sigemptyset(&act.sa_mask);
251         act.sa_flags = 0;
252         sigaction(SIGINT, &act, NULL);
253         sched.select_function = i9e_select;
254
255         sched.default_timeout.tv_sec = 1;
256         ret = i9e_open(&ici, &sched);
257         if (ret < 0)
258                 goto out;
259         para_log = i9e_log;
260         ret = schedule(&sched);
261         sched_shutdown(&sched);
262         i9e_close();
263         para_log = stderr_log;
264 out:
265         free(history_file);
266         free(socket_name);
267         if (ret < 0)
268                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
269         exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
270 }
271
272 __noreturn static void print_completions(void)
273 {
274         int ret = i9e_print_completions(audiod_completers);
275         exit(ret <= 0? EXIT_FAILURE : EXIT_SUCCESS);
276 }
277
278 #else /* HAVE_READLINE */
279
280 __noreturn static void interactive_session(void)
281 {
282         PARA_EMERG_LOG("interactive sessions not available\n");
283         exit(EXIT_FAILURE);
284 }
285
286 __noreturn static void print_completions(void)
287 {
288         PARA_EMERG_LOG("command completion not available\n");
289         exit(EXIT_FAILURE);
290 }
291
292 #endif /* HAVE_READLINE */
293
294 static char *configfile_exists(void)
295 {
296         char *config_file;
297         struct stat statbuf;
298         char *home = para_homedir();
299
300         config_file = make_message("%s/.paraslash/audioc.conf", home);
301         free(home);
302         if (!stat(config_file, &statbuf))
303                 return config_file;
304         free(config_file);
305         return NULL;
306 }
307
308 static void handle_help_flag(void)
309 {
310         char *help;
311
312         if (OPT_GIVEN(DETAILED_HELP))
313                 help = lls_long_help(CMD_PTR);
314         else if (OPT_GIVEN(HELP))
315                 help = lls_short_help(CMD_PTR);
316         else
317                 return;
318         printf("%s\n", help);
319         free(help);
320         exit(EXIT_SUCCESS);
321 }
322
323 /**
324  * The client program to connect to para_audiod.
325  *
326  * \param argc Usual argument count.
327  * \param argv Usual argument vector.
328  *
329  * It connects to the "well-known" local socket to communicate with
330  * para_audiod. Authentication is performed by sending a ucred buffer
331  * containing the user id to the local socket.
332  *
333  * Any data received from the socket is written to stdout.
334  *
335  * \return EXIT_SUCCESS or EXIT_FAILURE.
336  *
337  * \sa send_cred_buffer(), para_audioc(1), para_audiod(1).
338  */
339 int main(int argc, char *argv[])
340 {
341         const struct lls_command *cmd = CMD_PTR;
342         int ret, fd;
343         char *cf = NULL, *buf, *args, *errctx = NULL;
344         size_t bufsize;
345         struct lls_parse_result *lpr1, *lpr2, *lpr3;
346         unsigned num_inputs;
347
348         ret = lls(lls_parse(argc, argv, cmd, &lpr1, &errctx));
349         if (ret < 0)
350                 goto fail;
351         lpr = lpr1;
352         loglevel = OPT_UINT32_VAL(LOGLEVEL);
353         version_handle_flag("audioc", OPT_GIVEN(VERSION));
354         handle_help_flag();
355         cf = configfile_exists();
356         if (cf) {
357                 void *map;
358                 size_t sz;
359                 int cf_argc;
360                 char **cf_argv;
361                 ret = mmap_full_file(cf, O_RDONLY, &map, &sz, NULL);
362                 if (ret != -E_EMPTY) {
363                         if (ret < 0)
364                                 goto out;
365                         ret = lls(lls_convert_config(map, sz, NULL, &cf_argv,
366                                 &errctx));
367                         para_munmap(map, sz);
368                         if (ret < 0) {
369                                 PARA_ERROR_LOG("syntax error in %s\n", cf);
370                                 goto out;
371                         }
372                         cf_argc = ret;
373                         ret = lls(lls_parse(cf_argc, cf_argv, cmd, &lpr2,
374                                 &errctx));
375                         lls_free_argv(cf_argv);
376                         if (ret < 0) {
377                                 PARA_ERROR_LOG("parse error in %s\n", cf);
378                                 goto out;
379                         }
380                         ret = lls(lls_merge(lpr1, lpr2, cmd, &lpr3, &errctx));
381                         lls_free_parse_result(lpr2, cmd);
382                         if (ret < 0)
383                                 goto out;
384                         lls_free_parse_result(lpr1, cmd);
385                         lpr = lpr3;
386                         loglevel = OPT_UINT32_VAL(LOGLEVEL);
387                 }
388         }
389         if (OPT_GIVEN(COMPLETE))
390                 print_completions();
391         if (OPT_GIVEN(SOCKET))
392                 socket_name = para_strdup(OPT_STRING_VAL(SOCKET));
393         else {
394                 char *hn = para_hostname();
395                 socket_name = make_message("/var/paraslash/audiod_socket.%s",
396                         hn);
397                 free(hn);
398         }
399         num_inputs = lls_num_inputs(lpr);
400         if (num_inputs == 0)
401                 interactive_session();
402
403         args = concat_args(num_inputs, NULL);
404         ret = connect_audiod(socket_name, args);
405         free(socket_name);
406         free(args);
407         if (ret < 0)
408                 goto out;
409         fd = ret;
410         ret = mark_fd_blocking(STDOUT_FILENO);
411         if (ret < 0)
412                 goto out;
413         bufsize = PARA_MAX(1024U, OPT_UINT32_VAL(BUFSIZE));
414         buf = para_malloc(bufsize);
415         do {
416                 size_t n = ret = recv_bin_buffer(fd, buf, bufsize);
417                 if (ret <= 0)
418                         break;
419                 ret = write_all(STDOUT_FILENO, buf, n);
420         } while (ret >= 0);
421         free(buf);
422 out:
423         lls_free_parse_result(lpr, cmd);
424         free(cf);
425 fail:
426         if (errctx)
427                 PARA_ERROR_LOG("%s\n", errctx);
428         free(errctx);
429         if (ret < 0)
430                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
431         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
432 }