Merge branch 'refs/heads/t/logo-0.6'
[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 return ret;
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 return ret;
221 }
222
223 __noreturn static void interactive_session(void)
224 {
225 int ret;
226 char *history_file;
227 struct sigaction act;
228 struct i9e_client_info ici = {
229 .fds = {0, 1, 2},
230 .prompt = "para_audioc> ",
231 .line_handler = audioc_i9e_line_handler,
232 .loglevel = loglevel,
233 .completers = audiod_completers,
234 };
235
236 PARA_NOTICE_LOG("\n%s\n", version_text("audioc"));
237 if (OPT_GIVEN(HISTORY_FILE))
238 history_file = para_strdup(OPT_STRING_VAL(HISTORY_FILE));
239 else {
240 char *home = para_homedir();
241 history_file = make_message("%s/.paraslash/audioc.history",
242 home);
243 free(home);
244 }
245 ici.history_file = history_file;
246
247 act.sa_handler = i9e_signal_dispatch;
248 sigemptyset(&act.sa_mask);
249 act.sa_flags = 0;
250 sigaction(SIGINT, &act, NULL);
251 sched.select_function = i9e_select;
252
253 sched.default_timeout.tv_sec = 1;
254 ret = i9e_open(&ici, &sched);
255 if (ret < 0)
256 goto out;
257 para_log = i9e_log;
258 ret = schedule(&sched);
259 sched_shutdown(&sched);
260 i9e_close();
261 para_log = stderr_log;
262 out:
263 free(history_file);
264 free(socket_name);
265 if (ret < 0)
266 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
267 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
268 }
269
270 __noreturn static void print_completions(void)
271 {
272 int ret = i9e_print_completions(audiod_completers);
273 exit(ret <= 0? EXIT_FAILURE : EXIT_SUCCESS);
274 }
275
276 #else /* HAVE_READLINE */
277
278 __noreturn static void interactive_session(void)
279 {
280 PARA_EMERG_LOG("interactive sessions not available\n");
281 exit(EXIT_FAILURE);
282 }
283
284 __noreturn static void print_completions(void)
285 {
286 PARA_EMERG_LOG("command completion not available\n");
287 exit(EXIT_FAILURE);
288 }
289
290 #endif /* HAVE_READLINE */
291
292 static char *configfile_exists(void)
293 {
294 char *config_file;
295 struct stat statbuf;
296 char *home = para_homedir();
297
298 config_file = make_message("%s/.paraslash/audioc.conf", home);
299 free(home);
300 if (!stat(config_file, &statbuf))
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 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 }