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