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"
26 /** Array of error strings. */
29 static char *socket_name;
30 static struct lls_parse_result *lpr;
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)))
40 INIT_STDERR_LOGGING(loglevel);
42 static char *concat_args(unsigned argc, char * const *argv)
47 for (i = 0; i < argc; i++) {
48 const char *arg = argv? argv[i] : lls_input(i, lpr);
49 buf = para_strcat(buf, arg);
51 buf = para_strcat(buf, "\n");
56 static int connect_audiod(const char *sname, char *args)
60 ret = connect_local_socket(sname);
64 ret = send_cred_buffer(fd, args);
69 PARA_ERROR_LOG("could not connect %s\n", sname);
78 #include "buffer_tree.h"
79 #include "interactive.h"
81 static struct sched sched;
85 struct btr_node *btrn;
89 static struct i9e_completer audiod_completers[];
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);
98 static void help_completer(struct i9e_completion_info *ci,
99 struct i9e_completion_result *cr)
101 char *opts[] = {LSG_AUDIOD_CMD_HELP_OPTS, NULL};
103 if (ci->word[0] == '-') {
104 i9e_complete_option(opts, ci, cr);
107 cr->matches = i9e_complete_commands(ci->word, audiod_completers);
110 static void version_completer(struct i9e_completion_info *ci,
111 struct i9e_completion_result *cr)
113 char *opts[] = {LSG_AUDIOD_CMD_VERSION_OPTS, NULL};
115 if (ci->word_num <= 2 && ci->word && ci->word[0] == '-')
116 i9e_complete_option(opts, ci, cr);
119 static void stat_completer(struct i9e_completion_info *ci,
120 struct i9e_completion_result *cr)
122 char *sia[] = {STATUS_ITEMS NULL};
123 char *opts[] = {LSG_AUDIOD_CMD_STAT_OPTS, NULL};
125 if (ci->word_num <= 2 && ci->word && ci->word[0] == '-')
126 i9e_complete_option(opts, ci, cr);
128 i9e_extract_completions(ci->word, sia, &cr->matches);
131 static void grab_completer(struct i9e_completion_info *ci,
132 struct i9e_completion_result *cr)
134 char *opts[] = {LSG_AUDIOD_CMD_GRAB_OPTS, NULL};
135 i9e_complete_option(opts, ci, cr);
138 static struct i9e_completer audiod_completers[] = {
139 #define LSG_AUDIOD_CMD_CMD(_name) {.name = #_name, \
140 .completer = _name ## _completer}
141 LSG_AUDIOD_CMD_SUBCOMMANDS
142 #undef LSG_AUDIOD_CMD_CMD
146 static void audioc_pre_select(struct sched *s, void *context)
148 struct audioc_task *at = context;
149 int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
153 para_fd_set(at->fd, &s->rfds, &s->max_fileno);
156 static int audioc_post_select(struct sched *s, void *context)
159 struct audioc_task *at = context;
160 int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
165 if (!FD_ISSET(at->fd, &s->rfds))
167 bufsize = PARA_MAX(1024U, OPT_UINT32_VAL(BUFSIZE));
168 buf = para_malloc(bufsize);
169 ret = recv_bin_buffer(at->fd, buf, bufsize);
170 PARA_DEBUG_LOG("recv: %d\n", ret);
175 btr_add_output(buf, ret, at->btrn);
180 btr_remove_node(&at->btrn);
186 static struct audioc_task audioc_task, *at = &audioc_task;
188 static int audioc_i9e_line_handler(char *line)
193 PARA_DEBUG_LOG("line: %s\n", line);
194 ret = create_argv(line, " ", &argv);
198 args = concat_args(argc, argv);
202 ret = connect_audiod(socket_name, args);
207 ret = mark_fd_nonblocking(at->fd);
210 at->btrn = btr_new_node(&(struct btr_node_description)
211 EMBRACE(.name = "audioc line handler"));
212 at->task = task_register(&(struct task_info) {
214 .pre_select = audioc_pre_select,
215 .post_select = audioc_post_select,
218 i9e_attach_to_stdout(at->btrn);
225 __noreturn static void interactive_session(void)
229 struct sigaction act;
230 struct i9e_client_info ici = {
232 .prompt = "para_audioc> ",
233 .line_handler = audioc_i9e_line_handler,
234 .loglevel = loglevel,
235 .completers = audiod_completers,
238 PARA_NOTICE_LOG("\n%s\n", version_text("audioc"));
239 if (OPT_GIVEN(HISTORY_FILE))
240 history_file = para_strdup(OPT_STRING_VAL(HISTORY_FILE));
242 char *home = para_homedir();
243 history_file = make_message("%s/.paraslash/audioc.history",
247 ici.history_file = history_file;
249 act.sa_handler = i9e_signal_dispatch;
250 sigemptyset(&act.sa_mask);
252 sigaction(SIGINT, &act, NULL);
253 sched.select_function = i9e_select;
255 sched.default_timeout.tv_sec = 1;
256 ret = i9e_open(&ici, &sched);
260 ret = schedule(&sched);
261 sched_shutdown(&sched);
263 para_log = stderr_log;
268 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
269 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
272 __noreturn static void print_completions(void)
274 int ret = i9e_print_completions(audiod_completers);
275 exit(ret <= 0? EXIT_FAILURE : EXIT_SUCCESS);
278 #else /* HAVE_READLINE */
280 __noreturn static void interactive_session(void)
282 PARA_EMERG_LOG("interactive sessions not available\n");
286 __noreturn static void print_completions(void)
288 PARA_EMERG_LOG("command completion not available\n");
292 #endif /* HAVE_READLINE */
294 static void handle_help_flag(void)
298 if (OPT_GIVEN(DETAILED_HELP))
299 help = lls_long_help(CMD_PTR);
300 else if (OPT_GIVEN(HELP))
301 help = lls_short_help(CMD_PTR);
304 printf("%s\n", help);
310 * The client program to connect to para_audiod.
312 * \param argc Usual argument count.
313 * \param argv Usual argument vector.
315 * It connects to the "well-known" local socket to communicate with
316 * para_audiod. Authentication is performed by sending a ucred buffer
317 * containing the user id to the local socket.
319 * Any data received from the socket is written to stdout.
321 * \return EXIT_SUCCESS or EXIT_FAILURE.
323 * \sa \ref send_cred_buffer(), para_audioc(1), para_audiod(1).
325 int main(int argc, char *argv[])
328 char *buf, *args, *errctx = NULL;
332 ret = lls(lls_parse(argc, argv, CMD_PTR, &lpr, &errctx));
335 version_handle_flag("audioc", OPT_GIVEN(VERSION));
337 ret = lsu_merge_config_file_options(NULL, "audioc.conf",
338 &lpr, CMD_PTR, audioc_suite, 0 /* default flags */);
341 loglevel = OPT_UINT32_VAL(LOGLEVEL);
342 if (OPT_GIVEN(COMPLETE))
344 if (OPT_GIVEN(SOCKET))
345 socket_name = para_strdup(OPT_STRING_VAL(SOCKET));
347 char *hn = para_hostname();
348 socket_name = make_message("/var/paraslash/audiod_socket.%s",
352 num_inputs = lls_num_inputs(lpr);
354 interactive_session();
356 args = concat_args(num_inputs, NULL);
357 ret = connect_audiod(socket_name, args);
363 ret = mark_fd_blocking(STDOUT_FILENO);
366 bufsize = PARA_MAX(1024U, OPT_UINT32_VAL(BUFSIZE));
367 buf = para_malloc(bufsize);
369 size_t n = ret = recv_bin_buffer(fd, buf, bufsize);
372 ret = write_all(STDOUT_FILENO, buf, n);
376 lls_free_parse_result(lpr, CMD_PTR);
379 PARA_ERROR_LOG("%s\n", errctx);
382 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
383 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;