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"
27 /** Array of error strings. */
30 /** The gengetopt structure containing command line args. */
31 static struct audioc_args_info conf
;
32 static char *socket_name
;
36 INIT_STDERR_LOGGING(loglevel
);
38 static char *concat_args(unsigned argc
, char * const *argv
)
43 for (i
= 0; i
< argc
; i
++) {
44 buf
= para_strcat(buf
, argv
[i
]);
46 buf
= para_strcat(buf
, "\n");
51 static int connect_audiod(const char *sname
, char *args
)
55 ret
= connect_local_socket(sname
);
59 ret
= send_cred_buffer(fd
, args
);
64 PARA_ERROR_LOG("could not connect %s\n", sname
);
73 #include "buffer_tree.h"
74 #include "interactive.h"
75 #include "audiod.completion.h"
77 static struct sched sched
;
81 struct btr_node
*btrn
;
85 static struct i9e_completer audiod_completers
[];
87 I9E_DUMMY_COMPLETER(cycle
);
88 I9E_DUMMY_COMPLETER(off
);
89 I9E_DUMMY_COMPLETER(on
);
90 I9E_DUMMY_COMPLETER(sb
);
91 I9E_DUMMY_COMPLETER(tasks
);
92 I9E_DUMMY_COMPLETER(term
);
94 static void help_completer(struct i9e_completion_info
*ci
,
95 struct i9e_completion_result
*result
)
97 result
->matches
= i9e_complete_commands(ci
->word
, audiod_completers
);
100 static void version_completer(struct i9e_completion_info
*ci
,
101 struct i9e_completion_result
*cr
)
103 char *opts
[] = {"-v", NULL
};
105 if (ci
->word_num
<= 2 && ci
->word
&& ci
->word
[0] == '-')
106 i9e_complete_option(opts
, ci
, cr
);
109 static void stat_completer(struct i9e_completion_info
*ci
,
110 struct i9e_completion_result
*cr
)
112 char *sia
[] = {STATUS_ITEM_ARRAY NULL
};
113 char *opts
[] = {"-p", NULL
};
115 if (ci
->word_num
<= 2 && ci
->word
&& ci
->word
[0] == '-')
116 i9e_complete_option(opts
, ci
, cr
);
118 i9e_extract_completions(ci
->word
, sia
, &cr
->matches
);
121 static void grab_completer(struct i9e_completion_info
*ci
,
122 struct i9e_completion_result
*cr
)
124 char *opts
[] = {"-ms", "-ms", "-ma", "-p=", "-n=", "-o", NULL
};
125 i9e_complete_option(opts
, ci
, cr
);
128 static struct i9e_completer audiod_completers
[] = {
133 static void audioc_pre_select(struct sched
*s
, void *context
)
135 struct audioc_task
*at
= context
;
136 int ret
= btr_node_status(at
->btrn
, 0, BTR_NT_ROOT
);
140 para_fd_set(at
->fd
, &s
->rfds
, &s
->max_fileno
);
143 static int audioc_post_select(struct sched
*s
, void *context
)
146 struct audioc_task
*at
= context
;
147 int ret
= btr_node_status(at
->btrn
, 0, BTR_NT_ROOT
);
151 if (!FD_ISSET(at
->fd
, &s
->rfds
))
153 buf
= para_malloc(conf
.bufsize_arg
);
154 ret
= recv_bin_buffer(at
->fd
, buf
, conf
.bufsize_arg
);
155 PARA_DEBUG_LOG("recv: %d\n", ret
);
160 btr_add_output(buf
, ret
, at
->btrn
);
165 btr_remove_node(&at
->btrn
);
171 static struct audioc_task audioc_task
, *at
= &audioc_task
;
173 static int audioc_i9e_line_handler(char *line
)
178 PARA_DEBUG_LOG("line: %s\n", line
);
179 ret
= create_argv(line
, " ", &conf
.inputs
);
182 conf
.inputs_num
= ret
;
183 args
= concat_args(conf
.inputs_num
, conf
.inputs
);
184 free_argv(conf
.inputs
);
187 conf
.inputs_num
= 0; /* required for audioc_cmdline_parser_free() */
188 ret
= connect_audiod(socket_name
, args
);
192 ret
= mark_fd_nonblocking(at
->fd
);
197 at
->btrn
= btr_new_node(&(struct btr_node_description
)
198 EMBRACE(.name
= "audioc line handler"));
199 at
->task
= task_register(&(struct task_info
) {
201 .pre_select
= audioc_pre_select
,
202 .post_select
= audioc_post_select
,
205 i9e_attach_to_stdout(at
->btrn
);
214 __noreturn
static void interactive_session(void)
218 struct sigaction act
;
219 struct i9e_client_info ici
= {
221 .prompt
= "para_audioc> ",
222 .line_handler
= audioc_i9e_line_handler
,
223 .loglevel
= loglevel
,
224 .completers
= audiod_completers
,
226 PARA_NOTICE_LOG("\n%s\n", version_text("audioc"));
227 if (conf
.history_file_given
)
228 history_file
= para_strdup(conf
.history_file_arg
);
230 char *home
= para_homedir();
231 history_file
= make_message("%s/.paraslash/audioc.history",
235 ici
.history_file
= history_file
;
237 act
.sa_handler
= i9e_signal_dispatch
;
238 sigemptyset(&act
.sa_mask
);
240 sigaction(SIGINT
, &act
, NULL
);
241 sched
.select_function
= i9e_select
;
243 sched
.default_timeout
.tv_sec
= 1;
244 ret
= i9e_open(&ici
, &sched
);
248 ret
= schedule(&sched
);
249 sched_shutdown(&sched
);
251 para_log
= stderr_log
;
254 audioc_cmdline_parser_free(&conf
);
256 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
257 exit(ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
);
260 __noreturn
static void print_completions(void)
262 int ret
= i9e_print_completions(audiod_completers
);
263 exit(ret
<= 0? EXIT_FAILURE
: EXIT_SUCCESS
);
266 #else /* HAVE_READLINE */
268 __noreturn
static void interactive_session(void)
270 PARA_EMERG_LOG("interactive sessions not available\n");
274 __noreturn
static void print_completions(void)
276 PARA_EMERG_LOG("command completion not available\n");
280 #endif /* HAVE_READLINE */
282 static char *configfile_exists(void)
286 char *home
= para_homedir();
288 config_file
= make_message("%s/.paraslash/audioc.conf", home
);
290 if (!stat(config_file
, &statbuf
))
296 __noreturn
static void print_help_and_die(void)
298 struct ggo_help h
= DEFINE_GGO_HELP(audioc
);
299 bool d
= conf
.detailed_help_given
;
301 ggo_print_help(&h
, d
? GPH_STANDARD_FLAGS_DETAILED
: GPH_STANDARD_FLAGS
);
306 * The client program to connect to para_audiod.
308 * \param argc Usual argument count.
309 * \param argv Usual argument vector.
311 * It connects to the "well-known" local socket to communicate with
312 * para_audiod. Authentication is performed by sending a ucred buffer
313 * containing the user id to the local socket.
315 * Any data received from the socket is written to stdout.
317 * \return EXIT_SUCCESS or EXIT_FAILURE.
319 * \sa send_cred_buffer(), para_audioc(1), para_audiod(1).
321 int main(int argc
, char *argv
[])
324 char *cf
, *buf
= NULL
, *args
= NULL
;
327 audioc_cmdline_parser(argc
, argv
, &conf
);
328 loglevel
= get_loglevel_by_name(conf
.loglevel_arg
);
329 version_handle_flag("audioc", conf
.version_given
);
330 if (conf
.help_given
|| conf
.detailed_help_given
)
331 print_help_and_die();
332 cf
= configfile_exists();
334 struct audioc_cmdline_parser_params params
= {
338 .check_ambiguity
= 0,
342 audioc_cmdline_parser_config_file(cf
, &conf
, ¶ms
);
344 loglevel
= get_loglevel_by_name(conf
.loglevel_arg
);
346 if (conf
.socket_given
)
347 socket_name
= para_strdup(conf
.socket_arg
);
349 char *hn
= para_hostname();
350 socket_name
= make_message("/var/paraslash/audiod_socket.%s",
355 if (conf
.complete_given
)
358 if (conf
.inputs_num
== 0)
359 interactive_session();
360 args
= concat_args(conf
.inputs_num
, conf
.inputs
);
362 ret
= connect_audiod(socket_name
, args
);
367 ret
= mark_fd_blocking(STDOUT_FILENO
);
370 bufsize
= conf
.bufsize_arg
;
371 buf
= para_malloc(bufsize
);
373 size_t n
= ret
= recv_bin_buffer(fd
, buf
, bufsize
);
376 ret
= write_all(STDOUT_FILENO
, buf
, n
);
382 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
383 return ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
;