2 * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file audioc.c The client program used to connect to para_audiod. */
9 #include <netinet/in.h>
10 #include <sys/socket.h>
12 #include <sys/types.h>
13 #include <arpa/inet.h>
19 #include "audiod_cmd.lsg.h"
20 #include "audioc.lsg.h"
29 /** Array of error strings. */
32 static char *socket_name;
33 static struct lls_parse_result *lpr;
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)))
43 INIT_STDERR_LOGGING(loglevel);
45 static char *concat_args(unsigned argc, char * const *argv)
50 for (i = 0; i < argc; i++) {
51 const char *arg = argv? argv[i] : lls_input(i, lpr);
52 buf = para_strcat(buf, arg);
54 buf = para_strcat(buf, "\n");
59 static int connect_audiod(const char *sname, char *args)
63 ret = connect_local_socket(sname);
67 ret = send_cred_buffer(fd, args);
72 PARA_ERROR_LOG("could not connect %s\n", sname);
81 #include "buffer_tree.h"
82 #include "interactive.h"
84 static struct sched sched;
88 struct btr_node *btrn;
92 static struct i9e_completer audiod_completers[];
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);
101 static void help_completer(struct i9e_completion_info *ci,
102 struct i9e_completion_result *result)
104 result->matches = i9e_complete_commands(ci->word, audiod_completers);
107 static void version_completer(struct i9e_completion_info *ci,
108 struct i9e_completion_result *cr)
110 char *opts[] = {LSG_AUDIOD_CMD_VERSION_OPTS, NULL};
112 if (ci->word_num <= 2 && ci->word && ci->word[0] == '-')
113 i9e_complete_option(opts, ci, cr);
116 static void stat_completer(struct i9e_completion_info *ci,
117 struct i9e_completion_result *cr)
119 char *sia[] = {STATUS_ITEMS NULL};
120 char *opts[] = {LSG_AUDIOD_CMD_STAT_OPTS, NULL};
122 if (ci->word_num <= 2 && ci->word && ci->word[0] == '-')
123 i9e_complete_option(opts, ci, cr);
125 i9e_extract_completions(ci->word, sia, &cr->matches);
128 static void grab_completer(struct i9e_completion_info *ci,
129 struct i9e_completion_result *cr)
131 char *opts[] = {LSG_AUDIOD_CMD_GRAB_OPTS, NULL};
132 i9e_complete_option(opts, ci, cr);
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
143 static void audioc_pre_select(struct sched *s, void *context)
145 struct audioc_task *at = context;
146 int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
150 para_fd_set(at->fd, &s->rfds, &s->max_fileno);
153 static int audioc_post_select(struct sched *s, void *context)
156 struct audioc_task *at = context;
157 int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
162 if (!FD_ISSET(at->fd, &s->rfds))
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);
172 btr_add_output(buf, ret, at->btrn);
177 btr_remove_node(&at->btrn);
183 static struct audioc_task audioc_task, *at = &audioc_task;
185 static int audioc_i9e_line_handler(char *line)
190 PARA_DEBUG_LOG("line: %s\n", line);
191 ret = create_argv(line, " ", &argv);
195 args = concat_args(argc, argv);
199 ret = connect_audiod(socket_name, args);
204 ret = mark_fd_nonblocking(at->fd);
207 at->btrn = btr_new_node(&(struct btr_node_description)
208 EMBRACE(.name = "audioc line handler"));
209 at->task = task_register(&(struct task_info) {
211 .pre_select = audioc_pre_select,
212 .post_select = audioc_post_select,
215 i9e_attach_to_stdout(at->btrn);
222 __noreturn static void interactive_session(void)
226 struct sigaction act;
227 struct i9e_client_info ici = {
229 .prompt = "para_audioc> ",
230 .line_handler = audioc_i9e_line_handler,
231 .loglevel = loglevel,
232 .completers = audiod_completers,
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));
239 char *home = para_homedir();
240 history_file = make_message("%s/.paraslash/audioc.history",
244 ici.history_file = history_file;
246 act.sa_handler = i9e_signal_dispatch;
247 sigemptyset(&act.sa_mask);
249 sigaction(SIGINT, &act, NULL);
250 sched.select_function = i9e_select;
252 sched.default_timeout.tv_sec = 1;
253 ret = i9e_open(&ici, &sched);
257 ret = schedule(&sched);
258 sched_shutdown(&sched);
260 para_log = stderr_log;
265 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
266 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
269 __noreturn static void print_completions(void)
271 int ret = i9e_print_completions(audiod_completers);
272 exit(ret <= 0? EXIT_FAILURE : EXIT_SUCCESS);
275 #else /* HAVE_READLINE */
277 __noreturn static void interactive_session(void)
279 PARA_EMERG_LOG("interactive sessions not available\n");
283 __noreturn static void print_completions(void)
285 PARA_EMERG_LOG("command completion not available\n");
289 #endif /* HAVE_READLINE */
291 static char *configfile_exists(void)
295 char *home = para_homedir();
297 config_file = make_message("%s/.paraslash/audioc.conf", home);
299 if (!stat(config_file, &statbuf))
305 static void handle_help_flag(void)
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);
315 printf("%s\n", help);
321 * The client program to connect to para_audiod.
323 * \param argc Usual argument count.
324 * \param argv Usual argument vector.
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.
330 * Any data received from the socket is written to stdout.
332 * \return EXIT_SUCCESS or EXIT_FAILURE.
334 * \sa \ref send_cred_buffer(), para_audioc(1), para_audiod(1).
336 int main(int argc, char *argv[])
338 const struct lls_command *cmd = CMD_PTR;
340 char *cf = NULL, *buf, *args, *errctx = NULL;
342 struct lls_parse_result *lpr1, *lpr2, *lpr3;
345 ret = lls(lls_parse(argc, argv, cmd, &lpr1, &errctx));
349 loglevel = OPT_UINT32_VAL(LOGLEVEL);
350 version_handle_flag("audioc", OPT_GIVEN(VERSION));
352 cf = configfile_exists();
358 ret = mmap_full_file(cf, O_RDONLY, &map, &sz, NULL);
359 if (ret != -E_EMPTY) {
362 ret = lls(lls_convert_config(map, sz, NULL, &cf_argv,
364 para_munmap(map, sz);
366 PARA_ERROR_LOG("syntax error in %s\n", cf);
370 ret = lls(lls_parse(cf_argc, cf_argv, cmd, &lpr2,
372 lls_free_argv(cf_argv);
374 PARA_ERROR_LOG("parse error in %s\n", cf);
377 ret = lls(lls_merge(lpr1, lpr2, cmd, &lpr3, &errctx));
378 lls_free_parse_result(lpr2, cmd);
381 lls_free_parse_result(lpr1, cmd);
383 loglevel = OPT_UINT32_VAL(LOGLEVEL);
386 if (OPT_GIVEN(COMPLETE))
388 if (OPT_GIVEN(SOCKET))
389 socket_name = para_strdup(OPT_STRING_VAL(SOCKET));
391 char *hn = para_hostname();
392 socket_name = make_message("/var/paraslash/audiod_socket.%s",
396 num_inputs = lls_num_inputs(lpr);
398 interactive_session();
400 args = concat_args(num_inputs, NULL);
401 ret = connect_audiod(socket_name, args);
407 ret = mark_fd_blocking(STDOUT_FILENO);
410 bufsize = PARA_MAX(1024U, OPT_UINT32_VAL(BUFSIZE));
411 buf = para_malloc(bufsize);
413 size_t n = ret = recv_bin_buffer(fd, buf, bufsize);
416 ret = write_all(STDOUT_FILENO, buf, n);
420 lls_free_parse_result(lpr, cmd);
424 PARA_ERROR_LOG("%s\n", errctx);
427 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
428 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;