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");
45 static int connect_audiod(const char *sname
, char *args
)
49 ret
= connect_local_socket(sname
);
53 ret
= send_cred_buffer(fd
, args
);
58 PARA_ERROR_LOG("could not connect %s\n", sname
);
67 #include "buffer_tree.h"
68 #include "interactive.h"
69 #include "audiod_completion.h"
71 static struct sched sched
;
75 struct btr_node
*btrn
;
79 static struct i9e_completer audiod_completers
[];
81 I9E_DUMMY_COMPLETER(cycle
);
82 I9E_DUMMY_COMPLETER(off
);
83 I9E_DUMMY_COMPLETER(on
);
84 I9E_DUMMY_COMPLETER(sb
);
85 I9E_DUMMY_COMPLETER(tasks
);
86 I9E_DUMMY_COMPLETER(term
);
88 static void help_completer(struct i9e_completion_info
*ci
,
89 struct i9e_completion_result
*result
)
91 result
->matches
= i9e_complete_commands(ci
->word
, audiod_completers
);
94 static void stat_completer(struct i9e_completion_info
*ci
,
95 struct i9e_completion_result
*cr
)
97 char *sia
[] = {STATUS_ITEM_ARRAY NULL
};
98 char *opts
[] = {"-p", NULL
};
100 if (ci
->word_num
<= 2 && ci
->word
&& ci
->word
[0] == '-')
101 i9e_complete_option(opts
, ci
, cr
);
103 i9e_extract_completions(ci
->word
, sia
, &cr
->matches
);
106 static void grab_completer(struct i9e_completion_info
*ci
,
107 struct i9e_completion_result
*cr
)
109 char *opts
[] = {"-ms", "-ms", "-ma", "-p=", "-n=", "-o", NULL
};
110 i9e_complete_option(opts
, ci
, cr
);
113 static struct i9e_completer audiod_completers
[] = {
118 static void audioc_pre_select(struct sched
*s
, struct task
*t
)
120 struct audioc_task
*at
= container_of(t
, struct audioc_task
, task
);
121 int ret
= btr_node_status(at
->btrn
, 0, BTR_NT_ROOT
);
125 para_fd_set(at
->fd
, &s
->rfds
, &s
->max_fileno
);
128 static int audioc_post_select(struct sched
*s
, struct task
*t
)
131 struct audioc_task
*at
= container_of(t
, struct audioc_task
, task
);
132 int ret
= btr_node_status(at
->btrn
, 0, BTR_NT_ROOT
);
136 if (!FD_ISSET(at
->fd
, &s
->rfds
))
138 buf
= para_malloc(conf
.bufsize_arg
);
139 ret
= recv_bin_buffer(at
->fd
, buf
, conf
.bufsize_arg
);
140 PARA_DEBUG_LOG("recv: %d\n", ret
);
145 btr_add_output(buf
, ret
, at
->btrn
);
150 btr_remove_node(&at
->btrn
);
156 static struct audioc_task audioc_task
= {
158 .pre_select
= audioc_pre_select
,
159 .post_select
= audioc_post_select
,
160 .status
= "audioc task"
162 }, *at
= &audioc_task
;
164 static int audioc_i9e_line_handler(char *line
)
171 PARA_DEBUG_LOG("line: %s\n", line
);
172 ret
= create_argv(line
, " ", &conf
.inputs
);
175 conf
.inputs_num
= ret
;
176 args
= concat_args(conf
.inputs_num
, conf
.inputs
);
177 free_argv(conf
.inputs
);
178 conf
.inputs_num
= 0; /* required for audioc_cmdline_parser_free() */
179 ret
= connect_audiod(socket_name
, args
);
183 ret
= mark_fd_nonblocking(at
->fd
);
188 at
->btrn
= btr_new_node(&(struct btr_node_description
)
189 EMBRACE(.name
= "audioc line handler"));
191 register_task(&sched
, &at
->task
);
192 i9e_attach_to_stdout(at
->btrn
);
201 __noreturn
static void interactive_session(void)
205 struct sigaction act
;
206 struct i9e_client_info ici
= {
208 .prompt
= "para_audioc> ",
209 .line_handler
= audioc_i9e_line_handler
,
210 .loglevel
= loglevel
,
211 .completers
= audiod_completers
,
213 PARA_NOTICE_LOG("\n%s\n", VERSION_TEXT("audioc"));
214 if (conf
.history_file_given
)
215 history_file
= para_strdup(conf
.history_file_arg
);
217 char *home
= para_homedir();
218 history_file
= make_message("%s/.paraslash/audioc.history",
222 ici
.history_file
= history_file
;
224 act
.sa_handler
= i9e_signal_dispatch
;
225 sigemptyset(&act
.sa_mask
);
227 sigaction(SIGINT
, &act
, NULL
);
228 sched
.select_function
= i9e_select
;
230 sched
.default_timeout
.tv_sec
= 1;
231 ret
= i9e_open(&ici
, &sched
);
235 ret
= schedule(&sched
);
237 para_log
= stderr_log
;
240 audioc_cmdline_parser_free(&conf
);
242 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
243 exit(ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
);
246 __noreturn
static void print_completions(void)
248 int ret
= i9e_print_completions(audiod_completers
);
249 exit(ret
<= 0? EXIT_FAILURE
: EXIT_SUCCESS
);
252 #else /* HAVE_READLINE */
254 __noreturn
static void interactive_session(void)
256 PARA_EMERG_LOG("interactive sessions not available\n");
260 __noreturn
static void print_completions(void)
262 PARA_EMERG_LOG("command completion not available\n");
266 #endif /* HAVE_READLINE */
268 static char *configfile_exists(void)
272 char *home
= para_homedir();
274 config_file
= make_message("%s/.paraslash/audioc.conf", home
);
276 if (!stat(config_file
, &statbuf
))
283 * The client program to connect to para_audiod.
285 * \param argc Usual argument count.
286 * \param argv Usual argument vector.
288 * It connects to the "well-known" local socket to communicate with
289 * para_audiod. Authentication is performed by sending a ucred buffer
290 * containing the user id to the local socket.
292 * Any data received from the socket is written to stdout.
294 * \return EXIT_SUCCESS or EXIT_FAILURE.
296 * \sa send_cred_buffer(), para_audioc(1), para_audiod(1).
298 int main(int argc
, char *argv
[])
300 int ret
= -E_AUDIOC_SYNTAX
, fd
;
301 char *cf
, *buf
= NULL
, *args
= NULL
;
304 if (audioc_cmdline_parser(argc
, argv
, &conf
))
306 HANDLE_VERSION_FLAG("audioc", conf
);
307 cf
= configfile_exists();
309 struct audioc_cmdline_parser_params params
= {
315 ret
= audioc_cmdline_parser_config_file(cf
, &conf
, ¶ms
);
318 fprintf(stderr
, "parse error in config file\n");
322 loglevel
= get_loglevel_by_name(conf
.loglevel_arg
);
323 if (conf
.socket_given
)
324 socket_name
= para_strdup(conf
.socket_arg
);
326 char *hn
= para_hostname();
327 socket_name
= make_message("/var/paraslash/audiod_socket.%s",
332 if (conf
.complete_given
)
335 if (conf
.inputs_num
== 0)
336 interactive_session();
337 args
= concat_args(conf
.inputs_num
, conf
.inputs
);
339 ret
= connect_audiod(socket_name
, args
);
344 ret
= mark_fd_blocking(STDOUT_FILENO
);
347 bufsize
= conf
.bufsize_arg
;
348 buf
= para_malloc(bufsize
);
350 size_t n
= ret
= recv_bin_buffer(fd
, buf
, bufsize
);
353 ret
= write_all(STDOUT_FILENO
, buf
, n
);
359 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
360 return ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
;