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>
19 #include "audiod_cmd.lsg.h"
20 #include "audioc.lsg.h"
29 /** Array of error strings. */
32 static char *socket_name
;
33 static struct lls_parse_result
*lpr
;
35 #define CMD_PTR (lls_cmd(0, audioc_suite))
36 #define OPT_RESULT(_name) \
37 (lls_opt_result(LSG_AUDIOC_PARA_AUDIOC_OPT_ ## _name, lpr))
38 #define OPT_GIVEN(_name) (lls_opt_given(OPT_RESULT(_name)))
39 #define OPT_STRING_VAL(_name) (lls_string_val(0, OPT_RESULT(_name)))
40 #define OPT_UINT32_VAL(_name) (lls_uint32_val(0, OPT_RESULT(_name)))
43 INIT_STDERR_LOGGING(loglevel
);
45 static char *concat_args(unsigned argc
, char * const *argv
)
50 for (i
= 0; i
< argc
; i
++) {
51 const char *arg
= argv
? argv
[i
] : lls_input(i
, lpr
);
52 buf
= para_strcat(buf
, arg
);
54 buf
= para_strcat(buf
, "\n");
59 static int connect_audiod(const char *sname
, char *args
)
63 ret
= connect_local_socket(sname
);
67 ret
= send_cred_buffer(fd
, args
);
72 PARA_ERROR_LOG("could not connect %s\n", sname
);
81 #include "buffer_tree.h"
82 #include "interactive.h"
84 static struct sched sched
;
88 struct btr_node
*btrn
;
92 static struct i9e_completer audiod_completers
[];
94 I9E_DUMMY_COMPLETER(cycle
);
95 I9E_DUMMY_COMPLETER(off
);
96 I9E_DUMMY_COMPLETER(on
);
97 I9E_DUMMY_COMPLETER(sb
);
98 I9E_DUMMY_COMPLETER(tasks
);
99 I9E_DUMMY_COMPLETER(term
);
101 static void help_completer(struct i9e_completion_info
*ci
,
102 struct i9e_completion_result
*result
)
104 result
->matches
= i9e_complete_commands(ci
->word
, audiod_completers
);
107 static void version_completer(struct i9e_completion_info
*ci
,
108 struct i9e_completion_result
*cr
)
110 char *opts
[] = {LSG_AUDIOD_CMD_VERSION_OPTS
, NULL
};
112 if (ci
->word_num
<= 2 && ci
->word
&& ci
->word
[0] == '-')
113 i9e_complete_option(opts
, ci
, cr
);
116 static void stat_completer(struct i9e_completion_info
*ci
,
117 struct i9e_completion_result
*cr
)
119 char *sia
[] = {STATUS_ITEM_ARRAY NULL
};
120 char *opts
[] = {LSG_AUDIOD_CMD_STAT_OPTS
, NULL
};
122 if (ci
->word_num
<= 2 && ci
->word
&& ci
->word
[0] == '-')
123 i9e_complete_option(opts
, ci
, cr
);
125 i9e_extract_completions(ci
->word
, sia
, &cr
->matches
);
128 static void grab_completer(struct i9e_completion_info
*ci
,
129 struct i9e_completion_result
*cr
)
131 char *opts
[] = {LSG_AUDIOD_CMD_GRAB_OPTS
, NULL
};
132 i9e_complete_option(opts
, ci
, cr
);
135 I9E_DUMMY_COMPLETER(SUPERCOMMAND_UNAVAILABLE
);
136 static struct i9e_completer audiod_completers
[] = {
137 #define LSG_AUDIOD_CMD_CMD(_name) {.name = #_name, \
138 .completer = _name ## _completer}
139 LSG_AUDIOD_CMD_SUBCOMMANDS
140 #undef LSG_AUDIOD_CMD_CMD
144 static void audioc_pre_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 para_fd_set(at
->fd
, &s
->rfds
, &s
->max_fileno
);
154 static int audioc_post_select(struct sched
*s
, void *context
)
157 struct audioc_task
*at
= context
;
158 int ret
= btr_node_status(at
->btrn
, 0, BTR_NT_ROOT
);
163 if (!FD_ISSET(at
->fd
, &s
->rfds
))
165 bufsize
= PARA_MAX(1024U, OPT_UINT32_VAL(BUFSIZE
));
166 buf
= para_malloc(bufsize
);
167 ret
= recv_bin_buffer(at
->fd
, buf
, bufsize
);
168 PARA_DEBUG_LOG("recv: %d\n", ret
);
173 btr_add_output(buf
, ret
, at
->btrn
);
178 btr_remove_node(&at
->btrn
);
184 static struct audioc_task audioc_task
, *at
= &audioc_task
;
186 static int audioc_i9e_line_handler(char *line
)
191 PARA_DEBUG_LOG("line: %s\n", line
);
192 ret
= create_argv(line
, " ", &argv
);
196 args
= concat_args(argc
, argv
);
200 ret
= connect_audiod(socket_name
, args
);
205 ret
= mark_fd_nonblocking(at
->fd
);
208 at
->btrn
= btr_new_node(&(struct btr_node_description
)
209 EMBRACE(.name
= "audioc line handler"));
210 at
->task
= task_register(&(struct task_info
) {
212 .pre_select
= audioc_pre_select
,
213 .post_select
= audioc_post_select
,
216 i9e_attach_to_stdout(at
->btrn
);
223 __noreturn
static void interactive_session(void)
227 struct sigaction act
;
228 struct i9e_client_info ici
= {
230 .prompt
= "para_audioc> ",
231 .line_handler
= audioc_i9e_line_handler
,
232 .loglevel
= loglevel
,
233 .completers
= audiod_completers
,
236 PARA_NOTICE_LOG("\n%s\n", version_text("audioc"));
237 if (OPT_GIVEN(HISTORY_FILE
))
238 history_file
= para_strdup(OPT_STRING_VAL(HISTORY_FILE
));
240 char *home
= para_homedir();
241 history_file
= make_message("%s/.paraslash/audioc.history",
245 ici
.history_file
= history_file
;
247 act
.sa_handler
= i9e_signal_dispatch
;
248 sigemptyset(&act
.sa_mask
);
250 sigaction(SIGINT
, &act
, NULL
);
251 sched
.select_function
= i9e_select
;
253 sched
.default_timeout
.tv_sec
= 1;
254 ret
= i9e_open(&ici
, &sched
);
258 ret
= schedule(&sched
);
259 sched_shutdown(&sched
);
261 para_log
= stderr_log
;
266 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
267 exit(ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
);
270 __noreturn
static void print_completions(void)
272 int ret
= i9e_print_completions(audiod_completers
);
273 exit(ret
<= 0? EXIT_FAILURE
: EXIT_SUCCESS
);
276 #else /* HAVE_READLINE */
278 __noreturn
static void interactive_session(void)
280 PARA_EMERG_LOG("interactive sessions not available\n");
284 __noreturn
static void print_completions(void)
286 PARA_EMERG_LOG("command completion not available\n");
290 #endif /* HAVE_READLINE */
292 static char *configfile_exists(void)
296 char *home
= para_homedir();
298 config_file
= make_message("%s/.paraslash/audioc.conf", home
);
300 if (!stat(config_file
, &statbuf
))
306 static void handle_help_flag(void)
310 if (OPT_GIVEN(DETAILED_HELP
))
311 help
= lls_long_help(CMD_PTR
);
312 else if (OPT_GIVEN(HELP
))
313 help
= lls_short_help(CMD_PTR
);
316 printf("%s\n", help
);
322 * The client program to connect to para_audiod.
324 * \param argc Usual argument count.
325 * \param argv Usual argument vector.
327 * It connects to the "well-known" local socket to communicate with
328 * para_audiod. Authentication is performed by sending a ucred buffer
329 * containing the user id to the local socket.
331 * Any data received from the socket is written to stdout.
333 * \return EXIT_SUCCESS or EXIT_FAILURE.
335 * \sa \ref send_cred_buffer(), para_audioc(1), para_audiod(1).
337 int main(int argc
, char *argv
[])
339 const struct lls_command
*cmd
= CMD_PTR
;
341 char *cf
= NULL
, *buf
, *args
, *errctx
= NULL
;
343 struct lls_parse_result
*lpr1
, *lpr2
, *lpr3
;
346 ret
= lls(lls_parse(argc
, argv
, cmd
, &lpr1
, &errctx
));
350 loglevel
= OPT_UINT32_VAL(LOGLEVEL
);
351 version_handle_flag("audioc", OPT_GIVEN(VERSION
));
353 cf
= configfile_exists();
359 ret
= mmap_full_file(cf
, O_RDONLY
, &map
, &sz
, NULL
);
360 if (ret
!= -E_EMPTY
) {
363 ret
= lls(lls_convert_config(map
, sz
, NULL
, &cf_argv
,
365 para_munmap(map
, sz
);
367 PARA_ERROR_LOG("syntax error in %s\n", cf
);
371 ret
= lls(lls_parse(cf_argc
, cf_argv
, cmd
, &lpr2
,
373 lls_free_argv(cf_argv
);
375 PARA_ERROR_LOG("parse error in %s\n", cf
);
378 ret
= lls(lls_merge(lpr1
, lpr2
, cmd
, &lpr3
, &errctx
));
379 lls_free_parse_result(lpr2
, cmd
);
382 lls_free_parse_result(lpr1
, cmd
);
384 loglevel
= OPT_UINT32_VAL(LOGLEVEL
);
387 if (OPT_GIVEN(COMPLETE
))
389 if (OPT_GIVEN(SOCKET
))
390 socket_name
= para_strdup(OPT_STRING_VAL(SOCKET
));
392 char *hn
= para_hostname();
393 socket_name
= make_message("/var/paraslash/audiod_socket.%s",
397 num_inputs
= lls_num_inputs(lpr
);
399 interactive_session();
401 args
= concat_args(num_inputs
, NULL
);
402 ret
= connect_audiod(socket_name
, args
);
408 ret
= mark_fd_blocking(STDOUT_FILENO
);
411 bufsize
= PARA_MAX(1024U, OPT_UINT32_VAL(BUFSIZE
));
412 buf
= para_malloc(bufsize
);
414 size_t n
= ret
= recv_bin_buffer(fd
, buf
, bufsize
);
417 ret
= write_all(STDOUT_FILENO
, buf
, n
);
421 lls_free_parse_result(lpr
, cmd
);
425 PARA_ERROR_LOG("%s\n", errctx
);
428 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
429 return ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
;