2 * Copyright (C) 2005-2013 Andre Noll <maan@systemlinux.org>
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 "audioc.cmdline.h"
30 /** The gengetopt structure containing command line args. */
31 static struct audioc_args_info conf;
32 static char *socket_name;
36 INIT_STDERR_LOGGING(loglevel);
38 static char *concat_args(unsigned argc, char * const *argv)
43 for (i = 0; i < argc; i++) {
44 buf = para_strcat(buf, argv[i]);
46 buf = para_strcat(buf, "\n");
51 static int connect_audiod(const char *sname, char *args)
55 ret = connect_local_socket(sname);
59 ret = send_cred_buffer(fd, args);
64 PARA_ERROR_LOG("could not connect %s\n", sname);
73 #include "buffer_tree.h"
74 #include "interactive.h"
75 #include "audiod_completion.h"
77 static struct sched sched;
81 struct btr_node *btrn;
85 static struct i9e_completer audiod_completers[];
87 I9E_DUMMY_COMPLETER(cycle);
88 I9E_DUMMY_COMPLETER(off);
89 I9E_DUMMY_COMPLETER(on);
90 I9E_DUMMY_COMPLETER(sb);
91 I9E_DUMMY_COMPLETER(tasks);
92 I9E_DUMMY_COMPLETER(term);
94 static void help_completer(struct i9e_completion_info *ci,
95 struct i9e_completion_result *result)
97 result->matches = i9e_complete_commands(ci->word, audiod_completers);
100 static void stat_completer(struct i9e_completion_info *ci,
101 struct i9e_completion_result *cr)
103 char *sia[] = {STATUS_ITEM_ARRAY NULL};
104 char *opts[] = {"-p", NULL};
106 if (ci->word_num <= 2 && ci->word && ci->word[0] == '-')
107 i9e_complete_option(opts, ci, cr);
109 i9e_extract_completions(ci->word, sia, &cr->matches);
112 static void grab_completer(struct i9e_completion_info *ci,
113 struct i9e_completion_result *cr)
115 char *opts[] = {"-ms", "-ms", "-ma", "-p=", "-n=", "-o", NULL};
116 i9e_complete_option(opts, ci, cr);
119 static struct i9e_completer audiod_completers[] = {
124 static void audioc_pre_select(struct sched *s, struct task *t)
126 struct audioc_task *at = container_of(t, struct audioc_task, task);
127 int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
131 para_fd_set(at->fd, &s->rfds, &s->max_fileno);
134 static int audioc_post_select(struct sched *s, struct task *t)
137 struct audioc_task *at = container_of(t, struct audioc_task, task);
138 int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
142 if (!FD_ISSET(at->fd, &s->rfds))
144 buf = para_malloc(conf.bufsize_arg);
145 ret = recv_bin_buffer(at->fd, buf, conf.bufsize_arg);
146 PARA_DEBUG_LOG("recv: %d\n", ret);
151 btr_add_output(buf, ret, at->btrn);
156 btr_remove_node(&at->btrn);
162 static struct audioc_task audioc_task = {
164 .pre_select = audioc_pre_select,
165 .post_select = audioc_post_select,
166 .status = "audioc task"
168 }, *at = &audioc_task;
170 static int audioc_i9e_line_handler(char *line)
175 PARA_DEBUG_LOG("line: %s\n", line);
176 ret = create_argv(line, " ", &conf.inputs);
179 conf.inputs_num = ret;
180 args = concat_args(conf.inputs_num, conf.inputs);
181 free_argv(conf.inputs);
184 conf.inputs_num = 0; /* required for audioc_cmdline_parser_free() */
185 ret = connect_audiod(socket_name, args);
189 ret = mark_fd_nonblocking(at->fd);
194 at->btrn = btr_new_node(&(struct btr_node_description)
195 EMBRACE(.name = "audioc line handler"));
197 register_task(&sched, &at->task);
198 i9e_attach_to_stdout(at->btrn);
207 __noreturn static void interactive_session(void)
211 struct sigaction act;
212 struct i9e_client_info ici = {
214 .prompt = "para_audioc> ",
215 .line_handler = audioc_i9e_line_handler,
216 .loglevel = loglevel,
217 .completers = audiod_completers,
219 PARA_NOTICE_LOG("\n%s\n", version_text("audioc"));
220 if (conf.history_file_given)
221 history_file = para_strdup(conf.history_file_arg);
223 char *home = para_homedir();
224 history_file = make_message("%s/.paraslash/audioc.history",
228 ici.history_file = history_file;
230 act.sa_handler = i9e_signal_dispatch;
231 sigemptyset(&act.sa_mask);
233 sigaction(SIGINT, &act, NULL);
234 sched.select_function = i9e_select;
236 sched.default_timeout.tv_sec = 1;
237 ret = i9e_open(&ici, &sched);
241 ret = schedule(&sched);
243 para_log = stderr_log;
246 audioc_cmdline_parser_free(&conf);
248 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
249 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
252 __noreturn static void print_completions(void)
254 int ret = i9e_print_completions(audiod_completers);
255 exit(ret <= 0? EXIT_FAILURE : EXIT_SUCCESS);
258 #else /* HAVE_READLINE */
260 __noreturn static void interactive_session(void)
262 PARA_EMERG_LOG("interactive sessions not available\n");
266 __noreturn static void print_completions(void)
268 PARA_EMERG_LOG("command completion not available\n");
272 #endif /* HAVE_READLINE */
274 static char *configfile_exists(void)
278 char *home = para_homedir();
280 config_file = make_message("%s/.paraslash/audioc.conf", home);
282 if (!stat(config_file, &statbuf))
288 __noreturn static void print_help_and_die(void)
290 struct ggo_help h = DEFINE_GGO_HELP(audioc);
291 bool d = conf.detailed_help_given;
293 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
298 * The client program to connect to para_audiod.
300 * \param argc Usual argument count.
301 * \param argv Usual argument vector.
303 * It connects to the "well-known" local socket to communicate with
304 * para_audiod. Authentication is performed by sending a ucred buffer
305 * containing the user id to the local socket.
307 * Any data received from the socket is written to stdout.
309 * \return EXIT_SUCCESS or EXIT_FAILURE.
311 * \sa send_cred_buffer(), para_audioc(1), para_audiod(1).
313 int main(int argc, char *argv[])
316 char *cf, *buf = NULL, *args = NULL;
319 audioc_cmdline_parser(argc, argv, &conf);
320 loglevel = get_loglevel_by_name(conf.loglevel_arg);
321 version_handle_flag("audioc", conf.version_given);
322 if (conf.help_given || conf.detailed_help_given)
323 print_help_and_die();
324 cf = configfile_exists();
326 struct audioc_cmdline_parser_params params = {
330 .check_ambiguity = 0,
334 audioc_cmdline_parser_config_file(cf, &conf, ¶ms);
336 loglevel = get_loglevel_by_name(conf.loglevel_arg);
338 if (conf.socket_given)
339 socket_name = para_strdup(conf.socket_arg);
341 char *hn = para_hostname();
342 socket_name = make_message("/var/paraslash/audiod_socket.%s",
347 if (conf.complete_given)
350 if (conf.inputs_num == 0)
351 interactive_session();
352 args = concat_args(conf.inputs_num, conf.inputs);
354 ret = connect_audiod(socket_name, args);
359 ret = mark_fd_blocking(STDOUT_FILENO);
362 bufsize = conf.bufsize_arg;
363 buf = para_malloc(bufsize);
365 size_t n = ret = recv_bin_buffer(fd, buf, bufsize);
368 ret = write_all(STDOUT_FILENO, buf, n);
374 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
375 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;