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"
25 /** The gengetopt structure containing command line args. */
26 static struct audioc_args_info conf
;
27 static char *socket_name
;
31 INIT_STDERR_LOGGING(loglevel
);
33 static char *concat_args(unsigned argc
, char * const *argv
)
38 for (i
= 0; i
< argc
; i
++) {
39 buf
= para_strcat(buf
, argv
[i
]);
41 buf
= para_strcat(buf
, "\n");
46 static int connect_audiod(const char *sname
, char *args
)
50 ret
= connect_local_socket(sname
);
54 ret
= send_cred_buffer(fd
, args
);
59 PARA_ERROR_LOG("could not connect %s\n", sname
);
68 #include "buffer_tree.h"
69 #include "interactive.h"
70 #include "audiod_completion.h"
72 static struct sched sched
;
76 struct btr_node
*btrn
;
80 static struct i9e_completer audiod_completers
[];
82 I9E_DUMMY_COMPLETER(cycle
);
83 I9E_DUMMY_COMPLETER(off
);
84 I9E_DUMMY_COMPLETER(on
);
85 I9E_DUMMY_COMPLETER(sb
);
86 I9E_DUMMY_COMPLETER(tasks
);
87 I9E_DUMMY_COMPLETER(term
);
89 static void help_completer(struct i9e_completion_info
*ci
,
90 struct i9e_completion_result
*result
)
92 result
->matches
= i9e_complete_commands(ci
->word
, audiod_completers
);
95 static void stat_completer(struct i9e_completion_info
*ci
,
96 struct i9e_completion_result
*cr
)
98 char *sia
[] = {STATUS_ITEM_ARRAY NULL
};
99 char *opts
[] = {"-p", NULL
};
101 if (ci
->word_num
<= 2 && ci
->word
&& ci
->word
[0] == '-')
102 i9e_complete_option(opts
, ci
, cr
);
104 i9e_extract_completions(ci
->word
, sia
, &cr
->matches
);
107 static void grab_completer(struct i9e_completion_info
*ci
,
108 struct i9e_completion_result
*cr
)
110 char *opts
[] = {"-ms", "-ms", "-ma", "-p=", "-n=", "-o", NULL
};
111 i9e_complete_option(opts
, ci
, cr
);
114 static struct i9e_completer audiod_completers
[] = {
119 static void audioc_pre_select(struct sched
*s
, struct task
*t
)
121 struct audioc_task
*at
= container_of(t
, struct audioc_task
, task
);
122 int ret
= btr_node_status(at
->btrn
, 0, BTR_NT_ROOT
);
126 para_fd_set(at
->fd
, &s
->rfds
, &s
->max_fileno
);
129 static int audioc_post_select(struct sched
*s
, struct task
*t
)
132 struct audioc_task
*at
= container_of(t
, struct audioc_task
, task
);
133 int ret
= btr_node_status(at
->btrn
, 0, BTR_NT_ROOT
);
137 if (!FD_ISSET(at
->fd
, &s
->rfds
))
139 buf
= para_malloc(conf
.bufsize_arg
);
140 ret
= recv_bin_buffer(at
->fd
, buf
, conf
.bufsize_arg
);
141 PARA_DEBUG_LOG("recv: %d\n", ret
);
146 btr_add_output(buf
, ret
, at
->btrn
);
151 btr_remove_node(&at
->btrn
);
157 static struct audioc_task audioc_task
= {
159 .pre_select
= audioc_pre_select
,
160 .post_select
= audioc_post_select
,
161 .status
= "audioc task"
163 }, *at
= &audioc_task
;
165 static int audioc_i9e_line_handler(char *line
)
170 PARA_DEBUG_LOG("line: %s\n", line
);
171 ret
= create_argv(line
, " ", &conf
.inputs
);
174 conf
.inputs_num
= ret
;
175 args
= concat_args(conf
.inputs_num
, conf
.inputs
);
176 free_argv(conf
.inputs
);
179 conf
.inputs_num
= 0; /* required for audioc_cmdline_parser_free() */
180 ret
= connect_audiod(socket_name
, args
);
184 ret
= mark_fd_nonblocking(at
->fd
);
189 at
->btrn
= btr_new_node(&(struct btr_node_description
)
190 EMBRACE(.name
= "audioc line handler"));
192 register_task(&sched
, &at
->task
);
193 i9e_attach_to_stdout(at
->btrn
);
202 __noreturn
static void interactive_session(void)
206 struct sigaction act
;
207 struct i9e_client_info ici
= {
209 .prompt
= "para_audioc> ",
210 .line_handler
= audioc_i9e_line_handler
,
211 .loglevel
= loglevel
,
212 .completers
= audiod_completers
,
214 PARA_NOTICE_LOG("\n%s\n", version_text("audioc"));
215 if (conf
.history_file_given
)
216 history_file
= para_strdup(conf
.history_file_arg
);
218 char *home
= para_homedir();
219 history_file
= make_message("%s/.paraslash/audioc.history",
223 ici
.history_file
= history_file
;
225 act
.sa_handler
= i9e_signal_dispatch
;
226 sigemptyset(&act
.sa_mask
);
228 sigaction(SIGINT
, &act
, NULL
);
229 sched
.select_function
= i9e_select
;
231 sched
.default_timeout
.tv_sec
= 1;
232 ret
= i9e_open(&ici
, &sched
);
236 ret
= schedule(&sched
);
238 para_log
= stderr_log
;
241 audioc_cmdline_parser_free(&conf
);
243 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
244 exit(ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
);
247 __noreturn
static void print_completions(void)
249 int ret
= i9e_print_completions(audiod_completers
);
250 exit(ret
<= 0? EXIT_FAILURE
: EXIT_SUCCESS
);
253 #else /* HAVE_READLINE */
255 __noreturn
static void interactive_session(void)
257 PARA_EMERG_LOG("interactive sessions not available\n");
261 __noreturn
static void print_completions(void)
263 PARA_EMERG_LOG("command completion not available\n");
267 #endif /* HAVE_READLINE */
269 static char *configfile_exists(void)
273 char *home
= para_homedir();
275 config_file
= make_message("%s/.paraslash/audioc.conf", home
);
277 if (!stat(config_file
, &statbuf
))
283 __noreturn
static void print_help_and_die(void)
285 struct ggo_help h
= DEFINE_GGO_HELP(audioc
);
286 bool d
= conf
.detailed_help_given
;
288 ggo_print_help(&h
, d
? GPH_STANDARD_FLAGS_DETAILED
: GPH_STANDARD_FLAGS
);
293 * The client program to connect to para_audiod.
295 * \param argc Usual argument count.
296 * \param argv Usual argument vector.
298 * It connects to the "well-known" local socket to communicate with
299 * para_audiod. Authentication is performed by sending a ucred buffer
300 * containing the user id to the local socket.
302 * Any data received from the socket is written to stdout.
304 * \return EXIT_SUCCESS or EXIT_FAILURE.
306 * \sa send_cred_buffer(), para_audioc(1), para_audiod(1).
308 int main(int argc
, char *argv
[])
311 char *cf
, *buf
= NULL
, *args
= NULL
;
314 audioc_cmdline_parser(argc
, argv
, &conf
);
315 loglevel
= get_loglevel_by_name(conf
.loglevel_arg
);
316 version_handle_flag("audioc", conf
.version_given
);
317 if (conf
.help_given
|| conf
.detailed_help_given
)
318 print_help_and_die();
319 cf
= configfile_exists();
321 struct audioc_cmdline_parser_params params
= {
325 .check_ambiguity
= 0,
329 audioc_cmdline_parser_config_file(cf
, &conf
, ¶ms
);
331 loglevel
= get_loglevel_by_name(conf
.loglevel_arg
);
333 if (conf
.socket_given
)
334 socket_name
= para_strdup(conf
.socket_arg
);
336 char *hn
= para_hostname();
337 socket_name
= make_message("/var/paraslash/audiod_socket.%s",
342 if (conf
.complete_given
)
345 if (conf
.inputs_num
== 0)
346 interactive_session();
347 args
= concat_args(conf
.inputs_num
, conf
.inputs
);
349 ret
= connect_audiod(socket_name
, args
);
354 ret
= mark_fd_blocking(STDOUT_FILENO
);
357 bufsize
= conf
.bufsize_arg
;
358 buf
= para_malloc(bufsize
);
360 size_t n
= ret
= recv_bin_buffer(fd
, buf
, bufsize
);
363 ret
= write_all(STDOUT_FILENO
, buf
, n
);
369 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
370 return ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
;