1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file audioc.c The client program used to connect to para_audiod. */
5 #include <netinet/in.h>
6 #include <sys/socket.h>
15 #include "audiod_cmd.lsg.h"
16 #include "audioc.lsg.h"
25 /** Array of error strings. */
28 static char *socket_name;
29 static struct lls_parse_result *lpr;
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)))
39 INIT_STDERR_LOGGING(loglevel);
41 static char *concat_args(unsigned argc, char * const *argv)
46 for (i = 0; i < argc; i++) {
47 const char *arg = argv? argv[i] : lls_input(i, lpr);
48 buf = para_strcat(buf, arg);
50 buf = para_strcat(buf, "\n");
55 static int connect_audiod(const char *sname, char *args)
59 ret = connect_local_socket(sname);
63 ret = send_cred_buffer(fd, args);
68 PARA_ERROR_LOG("could not connect %s\n", sname);
77 #include "buffer_tree.h"
78 #include "interactive.h"
80 static struct sched sched;
84 struct btr_node *btrn;
88 static struct i9e_completer audiod_completers[];
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);
97 static void help_completer(struct i9e_completion_info *ci,
98 struct i9e_completion_result *result)
100 result->matches = i9e_complete_commands(ci->word, audiod_completers);
103 static void version_completer(struct i9e_completion_info *ci,
104 struct i9e_completion_result *cr)
106 char *opts[] = {LSG_AUDIOD_CMD_VERSION_OPTS, NULL};
108 if (ci->word_num <= 2 && ci->word && ci->word[0] == '-')
109 i9e_complete_option(opts, ci, cr);
112 static void stat_completer(struct i9e_completion_info *ci,
113 struct i9e_completion_result *cr)
115 char *sia[] = {STATUS_ITEMS NULL};
116 char *opts[] = {LSG_AUDIOD_CMD_STAT_OPTS, NULL};
118 if (ci->word_num <= 2 && ci->word && ci->word[0] == '-')
119 i9e_complete_option(opts, ci, cr);
121 i9e_extract_completions(ci->word, sia, &cr->matches);
124 static void grab_completer(struct i9e_completion_info *ci,
125 struct i9e_completion_result *cr)
127 char *opts[] = {LSG_AUDIOD_CMD_GRAB_OPTS, NULL};
128 i9e_complete_option(opts, ci, cr);
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
139 static void audioc_pre_select(struct sched *s, void *context)
141 struct audioc_task *at = context;
142 int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
146 para_fd_set(at->fd, &s->rfds, &s->max_fileno);
149 static int audioc_post_select(struct sched *s, void *context)
152 struct audioc_task *at = context;
153 int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
158 if (!FD_ISSET(at->fd, &s->rfds))
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);
168 btr_add_output(buf, ret, at->btrn);
173 btr_remove_node(&at->btrn);
179 static struct audioc_task audioc_task, *at = &audioc_task;
181 static int audioc_i9e_line_handler(char *line)
186 PARA_DEBUG_LOG("line: %s\n", line);
187 ret = create_argv(line, " ", &argv);
191 args = concat_args(argc, argv);
195 ret = connect_audiod(socket_name, args);
200 ret = mark_fd_nonblocking(at->fd);
203 at->btrn = btr_new_node(&(struct btr_node_description)
204 EMBRACE(.name = "audioc line handler"));
205 at->task = task_register(&(struct task_info) {
207 .pre_select = audioc_pre_select,
208 .post_select = audioc_post_select,
211 i9e_attach_to_stdout(at->btrn);
218 __noreturn static void interactive_session(void)
222 struct sigaction act;
223 struct i9e_client_info ici = {
225 .prompt = "para_audioc> ",
226 .line_handler = audioc_i9e_line_handler,
227 .loglevel = loglevel,
228 .completers = audiod_completers,
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));
235 char *home = para_homedir();
236 history_file = make_message("%s/.paraslash/audioc.history",
240 ici.history_file = history_file;
242 act.sa_handler = i9e_signal_dispatch;
243 sigemptyset(&act.sa_mask);
245 sigaction(SIGINT, &act, NULL);
246 sched.select_function = i9e_select;
248 sched.default_timeout.tv_sec = 1;
249 ret = i9e_open(&ici, &sched);
253 ret = schedule(&sched);
254 sched_shutdown(&sched);
256 para_log = stderr_log;
261 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
262 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
265 __noreturn static void print_completions(void)
267 int ret = i9e_print_completions(audiod_completers);
268 exit(ret <= 0? EXIT_FAILURE : EXIT_SUCCESS);
271 #else /* HAVE_READLINE */
273 __noreturn static void interactive_session(void)
275 PARA_EMERG_LOG("interactive sessions not available\n");
279 __noreturn static void print_completions(void)
281 PARA_EMERG_LOG("command completion not available\n");
285 #endif /* HAVE_READLINE */
287 static char *configfile_exists(void)
290 char *home = para_homedir();
292 config_file = make_message("%s/.paraslash/audioc.conf", home);
294 if (file_exists(config_file))
300 static void handle_help_flag(void)
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);
310 printf("%s\n", help);
316 * The client program to connect to para_audiod.
318 * \param argc Usual argument count.
319 * \param argv Usual argument vector.
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.
325 * Any data received from the socket is written to stdout.
327 * \return EXIT_SUCCESS or EXIT_FAILURE.
329 * \sa \ref send_cred_buffer(), para_audioc(1), para_audiod(1).
331 int main(int argc, char *argv[])
333 const struct lls_command *cmd = CMD_PTR;
335 char *cf = NULL, *buf, *args, *errctx = NULL;
337 struct lls_parse_result *lpr1, *lpr2, *lpr3;
340 ret = lls(lls_parse(argc, argv, cmd, &lpr1, &errctx));
344 loglevel = OPT_UINT32_VAL(LOGLEVEL);
345 version_handle_flag("audioc", OPT_GIVEN(VERSION));
347 cf = configfile_exists();
353 ret = mmap_full_file(cf, O_RDONLY, &map, &sz, NULL);
354 if (ret != -E_EMPTY) {
357 ret = lls(lls_convert_config(map, sz, NULL, &cf_argv,
359 para_munmap(map, sz);
361 PARA_ERROR_LOG("syntax error in %s\n", cf);
365 ret = lls(lls_parse(cf_argc, cf_argv, cmd, &lpr2,
367 lls_free_argv(cf_argv);
369 PARA_ERROR_LOG("parse error in %s\n", cf);
372 ret = lls(lls_merge(lpr1, lpr2, cmd, &lpr3, &errctx));
373 lls_free_parse_result(lpr2, cmd);
376 lls_free_parse_result(lpr1, cmd);
378 loglevel = OPT_UINT32_VAL(LOGLEVEL);
381 if (OPT_GIVEN(COMPLETE))
383 if (OPT_GIVEN(SOCKET))
384 socket_name = para_strdup(OPT_STRING_VAL(SOCKET));
386 char *hn = para_hostname();
387 socket_name = make_message("/var/paraslash/audiod_socket.%s",
391 num_inputs = lls_num_inputs(lpr);
393 interactive_session();
395 args = concat_args(num_inputs, NULL);
396 ret = connect_audiod(socket_name, args);
402 ret = mark_fd_blocking(STDOUT_FILENO);
405 bufsize = PARA_MAX(1024U, OPT_UINT32_VAL(BUFSIZE));
406 buf = para_malloc(bufsize);
408 size_t n = ret = recv_bin_buffer(fd, buf, bufsize);
411 ret = write_all(STDOUT_FILENO, buf, n);
415 lls_free_parse_result(lpr, cmd);
419 PARA_ERROR_LOG("%s\n", errctx);
422 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
423 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;