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>
18 #include "audioc.cmdline.h"
29 /** The gengetopt structure containing command line args. */
30 static struct audioc_args_info conf;
31 static char *socket_name;
35 INIT_STDERR_LOGGING(loglevel);
37 static char *concat_args(unsigned argc, char * const *argv)
42 for (i = 0; i < argc; i++) {
43 buf = para_strcat(buf, argv[i]);
45 buf = para_strcat(buf, "\n");
50 static int connect_audiod(const char *sname, char *args)
54 ret = connect_local_socket(sname);
58 ret = send_cred_buffer(fd, args);
63 PARA_ERROR_LOG("could not connect %s\n", sname);
72 #include "buffer_tree.h"
73 #include "interactive.h"
74 #include "audiod.completion.h"
76 static struct sched sched;
80 struct btr_node *btrn;
84 static struct i9e_completer audiod_completers[];
86 I9E_DUMMY_COMPLETER(cycle);
87 I9E_DUMMY_COMPLETER(off);
88 I9E_DUMMY_COMPLETER(on);
89 I9E_DUMMY_COMPLETER(sb);
90 I9E_DUMMY_COMPLETER(tasks);
91 I9E_DUMMY_COMPLETER(term);
93 static void help_completer(struct i9e_completion_info *ci,
94 struct i9e_completion_result *result)
96 result->matches = i9e_complete_commands(ci->word, audiod_completers);
99 static void version_completer(struct i9e_completion_info *ci,
100 struct i9e_completion_result *cr)
102 char *opts[] = {"-v", NULL};
104 if (ci->word_num <= 2 && ci->word && ci->word[0] == '-')
105 i9e_complete_option(opts, ci, cr);
108 static void stat_completer(struct i9e_completion_info *ci,
109 struct i9e_completion_result *cr)
111 char *sia[] = {STATUS_ITEM_ARRAY NULL};
112 char *opts[] = {"-p", NULL};
114 if (ci->word_num <= 2 && ci->word && ci->word[0] == '-')
115 i9e_complete_option(opts, ci, cr);
117 i9e_extract_completions(ci->word, sia, &cr->matches);
120 static void grab_completer(struct i9e_completion_info *ci,
121 struct i9e_completion_result *cr)
123 char *opts[] = {"-ms", "-ms", "-ma", "-p=", "-n=", "-o", NULL};
124 i9e_complete_option(opts, ci, cr);
127 static struct i9e_completer audiod_completers[] = {
132 static void audioc_pre_select(struct sched *s, void *context)
134 struct audioc_task *at = context;
135 int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
139 para_fd_set(at->fd, &s->rfds, &s->max_fileno);
142 static int audioc_post_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 if (!FD_ISSET(at->fd, &s->rfds))
152 buf = para_malloc(conf.bufsize_arg);
153 ret = recv_bin_buffer(at->fd, buf, conf.bufsize_arg);
154 PARA_DEBUG_LOG("recv: %d\n", ret);
159 btr_add_output(buf, ret, at->btrn);
164 btr_remove_node(&at->btrn);
170 static struct audioc_task audioc_task, *at = &audioc_task;
172 static int audioc_i9e_line_handler(char *line)
177 PARA_DEBUG_LOG("line: %s\n", line);
178 ret = create_argv(line, " ", &conf.inputs);
181 conf.inputs_num = ret;
182 args = concat_args(conf.inputs_num, conf.inputs);
183 free_argv(conf.inputs);
186 conf.inputs_num = 0; /* required for audioc_cmdline_parser_free() */
187 ret = connect_audiod(socket_name, args);
191 ret = mark_fd_nonblocking(at->fd);
196 at->btrn = btr_new_node(&(struct btr_node_description)
197 EMBRACE(.name = "audioc line handler"));
198 at->task = task_register(&(struct task_info) {
200 .pre_select = audioc_pre_select,
201 .post_select = audioc_post_select,
204 i9e_attach_to_stdout(at->btrn);
213 __noreturn static void interactive_session(void)
217 struct sigaction act;
218 struct i9e_client_info ici = {
220 .prompt = "para_audioc> ",
221 .line_handler = audioc_i9e_line_handler,
222 .loglevel = loglevel,
223 .completers = audiod_completers,
225 PARA_NOTICE_LOG("\n%s\n", version_text("audioc"));
226 if (conf.history_file_given)
227 history_file = para_strdup(conf.history_file_arg);
229 char *home = para_homedir();
230 history_file = make_message("%s/.paraslash/audioc.history",
234 ici.history_file = history_file;
236 act.sa_handler = i9e_signal_dispatch;
237 sigemptyset(&act.sa_mask);
239 sigaction(SIGINT, &act, NULL);
240 sched.select_function = i9e_select;
242 sched.default_timeout.tv_sec = 1;
243 ret = i9e_open(&ici, &sched);
247 ret = schedule(&sched);
248 sched_shutdown(&sched);
250 para_log = stderr_log;
253 audioc_cmdline_parser_free(&conf);
255 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
256 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
259 __noreturn static void print_completions(void)
261 int ret = i9e_print_completions(audiod_completers);
262 exit(ret <= 0? EXIT_FAILURE : EXIT_SUCCESS);
265 #else /* HAVE_READLINE */
267 __noreturn static void interactive_session(void)
269 PARA_EMERG_LOG("interactive sessions not available\n");
273 __noreturn static void print_completions(void)
275 PARA_EMERG_LOG("command completion not available\n");
279 #endif /* HAVE_READLINE */
281 static char *configfile_exists(void)
285 char *home = para_homedir();
287 config_file = make_message("%s/.paraslash/audioc.conf", home);
289 if (!stat(config_file, &statbuf))
295 __noreturn static void print_help_and_die(void)
297 struct ggo_help h = DEFINE_GGO_HELP(audioc);
298 bool d = conf.detailed_help_given;
300 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
305 * The client program to connect to para_audiod.
307 * \param argc Usual argument count.
308 * \param argv Usual argument vector.
310 * It connects to the "well-known" local socket to communicate with
311 * para_audiod. Authentication is performed by sending a ucred buffer
312 * containing the user id to the local socket.
314 * Any data received from the socket is written to stdout.
316 * \return EXIT_SUCCESS or EXIT_FAILURE.
318 * \sa send_cred_buffer(), para_audioc(1), para_audiod(1).
320 int main(int argc, char *argv[])
323 char *cf, *buf = NULL, *args = NULL;
326 audioc_cmdline_parser(argc, argv, &conf);
327 loglevel = get_loglevel_by_name(conf.loglevel_arg);
328 version_handle_flag("audioc", conf.version_given);
329 if (conf.help_given || conf.detailed_help_given)
330 print_help_and_die();
331 cf = configfile_exists();
333 struct audioc_cmdline_parser_params params = {
337 .check_ambiguity = 0,
341 audioc_cmdline_parser_config_file(cf, &conf, ¶ms);
343 loglevel = get_loglevel_by_name(conf.loglevel_arg);
345 if (conf.socket_given)
346 socket_name = para_strdup(conf.socket_arg);
348 char *hn = para_hostname();
349 socket_name = make_message("/var/paraslash/audiod_socket.%s",
354 if (conf.complete_given)
357 if (conf.inputs_num == 0)
358 interactive_session();
359 args = concat_args(conf.inputs_num, conf.inputs);
361 ret = connect_audiod(socket_name, args);
366 ret = mark_fd_blocking(STDOUT_FILENO);
369 bufsize = conf.bufsize_arg;
370 buf = para_malloc(bufsize);
372 size_t n = ret = recv_bin_buffer(fd, buf, bufsize);
375 ret = write_all(STDOUT_FILENO, buf, n);
381 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
382 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;