Merge branch 'maint'
[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 static struct i9e_completer audiod_completers[] = {
136 #define LSG_AUDIOD_CMD_CMD(_name) {.name = #_name, \
137 .completer = _name ## _completer}
138 LSG_AUDIOD_CMD_SUBCOMMANDS
139 #undef LSG_AUDIOD_CMD_CMD
140 {.name = NULL}
141 };
142
143 static void audioc_pre_select(struct sched *s, void *context)
144 {
145 struct audioc_task *at = context;
146 int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
147
148 if (ret < 0)
149 sched_min_delay(s);
150 para_fd_set(at->fd, &s->rfds, &s->max_fileno);
151 }
152
153 static int audioc_post_select(struct sched *s, void *context)
154 {
155 char *buf = NULL;
156 struct audioc_task *at = context;
157 int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
158 size_t bufsize;
159
160 if (ret < 0)
161 goto out;
162 if (!FD_ISSET(at->fd, &s->rfds))
163 return 0;
164 bufsize = PARA_MAX(1024U, OPT_UINT32_VAL(BUFSIZE));
165 buf = para_malloc(bufsize);
166 ret = recv_bin_buffer(at->fd, buf, bufsize);
167 PARA_DEBUG_LOG("recv: %d\n", ret);
168 if (ret == 0)
169 ret = -E_AUDIOC_EOF;
170 if (ret < 0)
171 goto out;
172 btr_add_output(buf, ret, at->btrn);
173 return 0;
174 out:
175 if (ret < 0) {
176 free(buf);
177 btr_remove_node(&at->btrn);
178 close(at->fd);
179 }
180 return ret;
181 }
182
183 static struct audioc_task audioc_task, *at = &audioc_task;
184
185 static int audioc_i9e_line_handler(char *line)
186 {
187 int argc, ret;
188 char *args, **argv;
189
190 PARA_DEBUG_LOG("line: %s\n", line);
191 ret = create_argv(line, " ", &argv);
192 if (ret < 0)
193 return ret;
194 argc = ret;
195 args = concat_args(argc, argv);
196 free_argv(argv);
197 if (!args)
198 return 0;
199 ret = connect_audiod(socket_name, args);
200 free(args);
201 if (ret < 0)
202 return ret;
203 at->fd = ret;
204 ret = mark_fd_nonblocking(at->fd);
205 if (ret < 0)
206 goto close;
207 at->btrn = btr_new_node(&(struct btr_node_description)
208 EMBRACE(.name = "audioc line handler"));
209 at->task = task_register(&(struct task_info) {
210 .name = "audioc",
211 .pre_select = audioc_pre_select,
212 .post_select = audioc_post_select,
213 .context = at,
214 }, &sched);
215 i9e_attach_to_stdout(at->btrn);
216 return 1;
217 close:
218 close(at->fd);
219 return ret;
220 }
221
222 __noreturn static void interactive_session(void)
223 {
224 int ret;
225 char *history_file;
226 struct sigaction act;
227 struct i9e_client_info ici = {
228 .fds = {0, 1, 2},
229 .prompt = "para_audioc> ",
230 .line_handler = audioc_i9e_line_handler,
231 .loglevel = loglevel,
232 .completers = audiod_completers,
233 };
234
235 PARA_NOTICE_LOG("\n%s\n", version_text("audioc"));
236 if (OPT_GIVEN(HISTORY_FILE))
237 history_file = para_strdup(OPT_STRING_VAL(HISTORY_FILE));
238 else {
239 char *home = para_homedir();
240 history_file = make_message("%s/.paraslash/audioc.history",
241 home);
242 free(home);
243 }
244 ici.history_file = history_file;
245
246 act.sa_handler = i9e_signal_dispatch;
247 sigemptyset(&act.sa_mask);
248 act.sa_flags = 0;
249 sigaction(SIGINT, &act, NULL);
250 sched.select_function = i9e_select;
251
252 sched.default_timeout.tv_sec = 1;
253 ret = i9e_open(&ici, &sched);
254 if (ret < 0)
255 goto out;
256 para_log = i9e_log;
257 ret = schedule(&sched);
258 sched_shutdown(&sched);
259 i9e_close();
260 para_log = stderr_log;
261 out:
262 free(history_file);
263 free(socket_name);
264 if (ret < 0)
265 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
266 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
267 }
268
269 __noreturn static void print_completions(void)
270 {
271 int ret = i9e_print_completions(audiod_completers);
272 exit(ret <= 0? EXIT_FAILURE : EXIT_SUCCESS);
273 }
274
275 #else /* HAVE_READLINE */
276
277 __noreturn static void interactive_session(void)
278 {
279 PARA_EMERG_LOG("interactive sessions not available\n");
280 exit(EXIT_FAILURE);
281 }
282
283 __noreturn static void print_completions(void)
284 {
285 PARA_EMERG_LOG("command completion not available\n");
286 exit(EXIT_FAILURE);
287 }
288
289 #endif /* HAVE_READLINE */
290
291 static char *configfile_exists(void)
292 {
293 char *config_file;
294 struct stat statbuf;
295 char *home = para_homedir();
296
297 config_file = make_message("%s/.paraslash/audioc.conf", home);
298 free(home);
299 if (!stat(config_file, &statbuf))
300 return config_file;
301 free(config_file);
302 return NULL;
303 }
304
305 static void handle_help_flag(void)
306 {
307 char *help;
308
309 if (OPT_GIVEN(DETAILED_HELP))
310 help = lls_long_help(CMD_PTR);
311 else if (OPT_GIVEN(HELP))
312 help = lls_short_help(CMD_PTR);
313 else
314 return;
315 printf("%s\n", help);
316 free(help);
317 exit(EXIT_SUCCESS);
318 }
319
320 /**
321 * The client program to connect to para_audiod.
322 *
323 * \param argc Usual argument count.
324 * \param argv Usual argument vector.
325 *
326 * It connects to the "well-known" local socket to communicate with
327 * para_audiod. Authentication is performed by sending a ucred buffer
328 * containing the user id to the local socket.
329 *
330 * Any data received from the socket is written to stdout.
331 *
332 * \return EXIT_SUCCESS or EXIT_FAILURE.
333 *
334 * \sa \ref send_cred_buffer(), para_audioc(1), para_audiod(1).
335 */
336 int main(int argc, char *argv[])
337 {
338 const struct lls_command *cmd = CMD_PTR;
339 int ret, fd;
340 char *cf = NULL, *buf, *args, *errctx = NULL;
341 size_t bufsize;
342 struct lls_parse_result *lpr1, *lpr2, *lpr3;
343 unsigned num_inputs;
344
345 ret = lls(lls_parse(argc, argv, cmd, &lpr1, &errctx));
346 if (ret < 0)
347 goto fail;
348 lpr = lpr1;
349 loglevel = OPT_UINT32_VAL(LOGLEVEL);
350 version_handle_flag("audioc", OPT_GIVEN(VERSION));
351 handle_help_flag();
352 cf = configfile_exists();
353 if (cf) {
354 void *map;
355 size_t sz;
356 int cf_argc;
357 char **cf_argv;
358 ret = mmap_full_file(cf, O_RDONLY, &map, &sz, NULL);
359 if (ret != -E_EMPTY) {
360 if (ret < 0)
361 goto out;
362 ret = lls(lls_convert_config(map, sz, NULL, &cf_argv,
363 &errctx));
364 para_munmap(map, sz);
365 if (ret < 0) {
366 PARA_ERROR_LOG("syntax error in %s\n", cf);
367 goto out;
368 }
369 cf_argc = ret;
370 ret = lls(lls_parse(cf_argc, cf_argv, cmd, &lpr2,
371 &errctx));
372 lls_free_argv(cf_argv);
373 if (ret < 0) {
374 PARA_ERROR_LOG("parse error in %s\n", cf);
375 goto out;
376 }
377 ret = lls(lls_merge(lpr1, lpr2, cmd, &lpr3, &errctx));
378 lls_free_parse_result(lpr2, cmd);
379 if (ret < 0)
380 goto out;
381 lls_free_parse_result(lpr1, cmd);
382 lpr = lpr3;
383 loglevel = OPT_UINT32_VAL(LOGLEVEL);
384 }
385 }
386 if (OPT_GIVEN(COMPLETE))
387 print_completions();
388 if (OPT_GIVEN(SOCKET))
389 socket_name = para_strdup(OPT_STRING_VAL(SOCKET));
390 else {
391 char *hn = para_hostname();
392 socket_name = make_message("/var/paraslash/audiod_socket.%s",
393 hn);
394 free(hn);
395 }
396 num_inputs = lls_num_inputs(lpr);
397 if (num_inputs == 0)
398 interactive_session();
399
400 args = concat_args(num_inputs, NULL);
401 ret = connect_audiod(socket_name, args);
402 free(socket_name);
403 free(args);
404 if (ret < 0)
405 goto out;
406 fd = ret;
407 ret = mark_fd_blocking(STDOUT_FILENO);
408 if (ret < 0)
409 goto out;
410 bufsize = PARA_MAX(1024U, OPT_UINT32_VAL(BUFSIZE));
411 buf = para_malloc(bufsize);
412 do {
413 size_t n = ret = recv_bin_buffer(fd, buf, bufsize);
414 if (ret <= 0)
415 break;
416 ret = write_all(STDOUT_FILENO, buf, n);
417 } while (ret >= 0);
418 free(buf);
419 out:
420 lls_free_parse_result(lpr, cmd);
421 free(cf);
422 fail:
423 if (errctx)
424 PARA_ERROR_LOG("%s\n", errctx);
425 free(errctx);
426 if (ret < 0)
427 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
428 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
429 }