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. */
10 #include <sys/types.h>
14 #include "audioc.cmdline.h"
24 /** The gengetopt structure containing command line args. */
25 static struct audioc_args_info conf;
26 static char *socket_name;
30 INIT_STDERR_LOGGING(loglevel);
32 static char *concat_args(unsigned argc, char * const *argv)
37 for (i = 0; i < argc; i++) {
38 buf = para_strcat(buf, argv[i]);
40 buf = para_strcat(buf, "\n");
48 #include "buffer_tree.h"
49 #include "interactive.h"
50 #include "audiod_completion.h"
52 static struct sched sched;
56 struct btr_node *btrn;
60 static struct i9e_completer audiod_completers[];
62 I9E_DUMMY_COMPLETER(cycle);
63 I9E_DUMMY_COMPLETER(off);
64 I9E_DUMMY_COMPLETER(on);
65 I9E_DUMMY_COMPLETER(sb);
66 I9E_DUMMY_COMPLETER(tasks);
67 I9E_DUMMY_COMPLETER(term);
69 static void help_completer(struct i9e_completion_info *ci,
70 struct i9e_completion_result *result)
72 result->matches = i9e_complete_commands(ci->word, audiod_completers);
75 static void stat_completer(struct i9e_completion_info *ci,
76 struct i9e_completion_result *cr)
78 char *sia[] = {STATUS_ITEM_ARRAY NULL};
79 char *opts[] = {"-p", NULL};
81 if (ci->word_num <= 2 && ci->word && ci->word[0] == '-')
82 i9e_complete_option(opts, ci, cr);
84 i9e_extract_completions(ci->word, sia, &cr->matches);
87 static void grab_completer(struct i9e_completion_info *ci,
88 struct i9e_completion_result *cr)
90 char *opts[] = {"-ms", "-ms", "-ma", "-p=", "-n=", "-o", NULL};
91 i9e_complete_option(opts, ci, cr);
94 static struct i9e_completer audiod_completers[] = {
99 static void audioc_pre_select(struct sched *s, struct task *t)
101 struct audioc_task *at = container_of(t, struct audioc_task, task);
102 int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
106 para_fd_set(at->fd, &s->rfds, &s->max_fileno);
109 static int audioc_post_select(struct sched *s, struct task *t)
112 struct audioc_task *at = container_of(t, struct audioc_task, task);
113 int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
117 if (!FD_ISSET(at->fd, &s->rfds))
119 buf = para_malloc(conf.bufsize_arg);
120 ret = recv_bin_buffer(at->fd, buf, conf.bufsize_arg);
121 PARA_DEBUG_LOG("recv: %d\n", ret);
126 btr_add_output(buf, ret, at->btrn);
131 btr_remove_node(&at->btrn);
137 static struct audioc_task audioc_task = {
139 .pre_select = audioc_pre_select,
140 .new_post_select = audioc_post_select,
142 .status = "audioc task"
144 }, *at = &audioc_task;
146 static int audioc_i9e_line_handler(char *line)
153 PARA_DEBUG_LOG("line: %s\n", line);
154 ret = create_argv(line, " ", &conf.inputs);
157 conf.inputs_num = ret;
158 args = concat_args(conf.inputs_num, conf.inputs);
159 free_argv(conf.inputs);
160 conf.inputs_num = 0; /* required for audioc_cmdline_parser_free() */
161 ret = connect_local_socket(socket_name);
165 ret = mark_fd_nonblocking(at->fd);
168 ret = send_cred_buffer(at->fd, args);
173 at->btrn = btr_new_node(&(struct btr_node_description)
174 EMBRACE(.name = "audioc line handler"));
176 register_task(&sched, &at->task);
177 i9e_attach_to_stdout(at->btrn);
186 __noreturn static void interactive_session(void)
190 struct sigaction act;
191 struct i9e_client_info ici = {
193 .prompt = "para_audioc> ",
194 .line_handler = audioc_i9e_line_handler,
195 .loglevel = loglevel,
196 .completers = audiod_completers,
198 PARA_NOTICE_LOG("\n%s\n", VERSION_TEXT("audioc"));
199 if (conf.history_file_given)
200 history_file = para_strdup(conf.history_file_arg);
202 char *home = para_homedir();
203 history_file = make_message("%s/.paraslash/audioc.history",
207 ici.history_file = history_file;
209 act.sa_handler = i9e_signal_dispatch;
210 sigemptyset(&act.sa_mask);
212 sigaction(SIGINT, &act, NULL);
213 sched.select_function = i9e_select;
215 sched.default_timeout.tv_sec = 1;
216 ret = i9e_open(&ici, &sched);
220 ret = schedule(&sched);
222 para_log = stderr_log;
225 audioc_cmdline_parser_free(&conf);
227 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
228 exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
231 __noreturn static void print_completions(void)
233 int ret = i9e_print_completions(audiod_completers);
234 exit(ret <= 0? EXIT_FAILURE : EXIT_SUCCESS);
237 #else /* HAVE_READLINE */
239 __noreturn static void interactive_session(void)
241 PARA_EMERG_LOG("interactive sessions not available\n");
245 __noreturn static void print_completions(void)
247 PARA_EMERG_LOG("command completion not available\n");
251 #endif /* HAVE_READLINE */
253 static char *configfile_exists(void)
257 char *home = para_homedir();
259 config_file = make_message("%s/.paraslash/audioc.conf", home);
261 if (!stat(config_file, &statbuf))
268 * The client program to connect to para_audiod.
270 * \param argc Usual argument count.
271 * \param argv Usual argument vector.
273 * It connects to the "well-known" local socket to communicate with
274 * para_audiod. Authentication is performed by sending a ucred buffer
275 * containing the user id to the local socket.
277 * Any data received from the socket is written to stdout.
279 * \return EXIT_SUCCESS or EXIT_FAILURE.
281 * \sa send_cred_buffer(), para_audioc(1), para_audiod(1).
283 int main(int argc, char *argv[])
285 int ret = -E_AUDIOC_SYNTAX, fd;
286 char *cf, *buf = NULL, *args = NULL;
289 if (audioc_cmdline_parser(argc, argv, &conf))
291 HANDLE_VERSION_FLAG("audioc", conf);
292 cf = configfile_exists();
294 struct audioc_cmdline_parser_params params = {
300 ret = audioc_cmdline_parser_config_file(cf, &conf, ¶ms);
303 fprintf(stderr, "parse error in config file\n");
307 loglevel = get_loglevel_by_name(conf.loglevel_arg);
308 if (conf.socket_given)
309 socket_name = para_strdup(conf.socket_arg);
311 char *hn = para_hostname();
312 socket_name = make_message("/var/paraslash/audiod_socket.%s",
317 if (conf.complete_given)
320 if (conf.inputs_num == 0)
321 interactive_session();
322 args = concat_args(conf.inputs_num, conf.inputs);
324 ret = connect_local_socket(socket_name);
327 PARA_EMERG_LOG("failed to connect to local socket\n");
331 ret = send_cred_buffer(fd, args);
334 bufsize = conf.bufsize_arg;
335 buf = para_malloc(bufsize);
336 ret = mark_fd_blocking(STDOUT_FILENO);
340 size_t n = ret = recv_bin_buffer(fd, buf, bufsize);
343 ret = write_all(STDOUT_FILENO, buf, n);
349 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
350 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;