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