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"
19 #include "audiod_cmd.lsg.h"
29 /** Array of error strings. */
32 /** The gengetopt structure containing command line args. */
33 static struct audioc_args_info conf;
34 static char *socket_name;
38 INIT_STDERR_LOGGING(loglevel);
40 static char *concat_args(unsigned argc, char * const *argv)
45 for (i = 0; i < argc; i++) {
46 buf = para_strcat(buf, argv[i]);
48 buf = para_strcat(buf, "\n");
53 static int connect_audiod(const char *sname, char *args)
57 ret = connect_local_socket(sname);
61 ret = send_cred_buffer(fd, args);
66 PARA_ERROR_LOG("could not connect %s\n", sname);
75 #include "buffer_tree.h"
76 #include "interactive.h"
78 static struct sched sched;
82 struct btr_node *btrn;
86 static struct i9e_completer audiod_completers[];
88 I9E_DUMMY_COMPLETER(cycle);
89 I9E_DUMMY_COMPLETER(off);
90 I9E_DUMMY_COMPLETER(on);
91 I9E_DUMMY_COMPLETER(sb);
92 I9E_DUMMY_COMPLETER(tasks);
93 I9E_DUMMY_COMPLETER(term);
95 static void help_completer(struct i9e_completion_info *ci,
96 struct i9e_completion_result *result)
98 result->matches = i9e_complete_commands(ci->word, audiod_completers);
101 static void version_completer(struct i9e_completion_info *ci,
102 struct i9e_completion_result *cr)
104 char *opts[] = {LSG_AUDIOD_CMD_VERSION_OPTS, NULL};
106 if (ci->word_num <= 2 && ci->word && ci->word[0] == '-')
107 i9e_complete_option(opts, ci, cr);
110 static void stat_completer(struct i9e_completion_info *ci,
111 struct i9e_completion_result *cr)
113 char *sia[] = {STATUS_ITEM_ARRAY NULL};
114 char *opts[] = {LSG_AUDIOD_CMD_STAT_OPTS, NULL};
116 if (ci->word_num <= 2 && ci->word && ci->word[0] == '-')
117 i9e_complete_option(opts, ci, cr);
119 i9e_extract_completions(ci->word, sia, &cr->matches);
122 static void grab_completer(struct i9e_completion_info *ci,
123 struct i9e_completion_result *cr)
125 char *opts[] = {LSG_AUDIOD_CMD_GRAB_OPTS, NULL};
126 i9e_complete_option(opts, ci, cr);
129 I9E_DUMMY_COMPLETER(SUPERCOMMAND_UNAVAILABLE);
130 static struct i9e_completer audiod_completers[] = {
131 #define LSG_AUDIOD_CMD_CMD(_name) {.name = #_name, \
132 .completer = _name ## _completer}
133 LSG_AUDIOD_CMD_SUBCOMMANDS
134 #undef LSG_AUDIOD_CMD_CMD
138 static void audioc_pre_select(struct sched *s, void *context)
140 struct audioc_task *at = context;
141 int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
145 para_fd_set(at->fd, &s->rfds, &s->max_fileno);
148 static int audioc_post_select(struct sched *s, void *context)
151 struct audioc_task *at = context;
152 int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
156 if (!FD_ISSET(at->fd, &s->rfds))
158 buf = para_malloc(conf.bufsize_arg);
159 ret = recv_bin_buffer(at->fd, buf, conf.bufsize_arg);
160 PARA_DEBUG_LOG("recv: %d\n", ret);
165 btr_add_output(buf, ret, at->btrn);
170 btr_remove_node(&at->btrn);
176 static struct audioc_task audioc_task, *at = &audioc_task;
178 static int audioc_i9e_line_handler(char *line)
183 PARA_DEBUG_LOG("line: %s\n", line);
184 ret = create_argv(line, " ", &conf.inputs);
187 conf.inputs_num = ret;
188 args = concat_args(conf.inputs_num, conf.inputs);
189 free_argv(conf.inputs);
192 conf.inputs_num = 0; /* required for audioc_cmdline_parser_free() */
193 ret = connect_audiod(socket_name, args);
197 ret = mark_fd_nonblocking(at->fd);
202 at->btrn = btr_new_node(&(struct btr_node_description)
203 EMBRACE(.name = "audioc line handler"));
204 at->task = task_register(&(struct task_info) {
206 .pre_select = audioc_pre_select,
207 .post_select = audioc_post_select,
210 i9e_attach_to_stdout(at->btrn);
219 __noreturn static void interactive_session(void)
223 struct sigaction act;
224 struct i9e_client_info ici = {
226 .prompt = "para_audioc> ",
227 .line_handler = audioc_i9e_line_handler,
228 .loglevel = loglevel,
229 .completers = audiod_completers,
231 PARA_NOTICE_LOG("\n%s\n", version_text("audioc"));
232 if (conf.history_file_given)
233 history_file = para_strdup(conf.history_file_arg);
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;
259 audioc_cmdline_parser_free(&conf);
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)
291 char *home = para_homedir();
293 config_file = make_message("%s/.paraslash/audioc.conf", home);
295 if (!stat(config_file, &statbuf))
301 __noreturn static void print_help_and_die(void)
303 struct ggo_help h = DEFINE_GGO_HELP(audioc);
304 bool d = conf.detailed_help_given;
306 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
311 * The client program to connect to para_audiod.
313 * \param argc Usual argument count.
314 * \param argv Usual argument vector.
316 * It connects to the "well-known" local socket to communicate with
317 * para_audiod. Authentication is performed by sending a ucred buffer
318 * containing the user id to the local socket.
320 * Any data received from the socket is written to stdout.
322 * \return EXIT_SUCCESS or EXIT_FAILURE.
324 * \sa send_cred_buffer(), para_audioc(1), para_audiod(1).
326 int main(int argc, char *argv[])
329 char *cf, *buf = NULL, *args = NULL;
332 audioc_cmdline_parser(argc, argv, &conf);
333 loglevel = get_loglevel_by_name(conf.loglevel_arg);
334 version_handle_flag("audioc", conf.version_given);
335 if (conf.help_given || conf.detailed_help_given)
336 print_help_and_die();
337 cf = configfile_exists();
339 struct audioc_cmdline_parser_params params = {
343 .check_ambiguity = 0,
347 audioc_cmdline_parser_config_file(cf, &conf, ¶ms);
349 loglevel = get_loglevel_by_name(conf.loglevel_arg);
351 if (conf.socket_given)
352 socket_name = para_strdup(conf.socket_arg);
354 char *hn = para_hostname();
355 socket_name = make_message("/var/paraslash/audiod_socket.%s",
360 if (conf.complete_given)
363 if (conf.inputs_num == 0)
364 interactive_session();
365 args = concat_args(conf.inputs_num, conf.inputs);
367 ret = connect_audiod(socket_name, args);
372 ret = mark_fd_blocking(STDOUT_FILENO);
375 bufsize = conf.bufsize_arg;
376 buf = para_malloc(bufsize);
378 size_t n = ret = recv_bin_buffer(fd, buf, bufsize);
381 ret = write_all(STDOUT_FILENO, buf, n);
387 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
388 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;