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