1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file audioc.c The client program used to connect to para_audiod. */
5 #include <netinet/in.h>
6 #include <sys/socket.h>
15 #include "audiod_cmd.lsg.h"
16 #include "audioc.lsg.h"
25 /** Array of error strings. */
28 static char *socket_name
;
29 static struct lls_parse_result
*lpr
;
31 #define CMD_PTR (lls_cmd(0, audioc_suite))
32 #define OPT_RESULT(_name) \
33 (lls_opt_result(LSG_AUDIOC_PARA_AUDIOC_OPT_ ## _name, lpr))
34 #define OPT_GIVEN(_name) (lls_opt_given(OPT_RESULT(_name)))
35 #define OPT_STRING_VAL(_name) (lls_string_val(0, OPT_RESULT(_name)))
36 #define OPT_UINT32_VAL(_name) (lls_uint32_val(0, OPT_RESULT(_name)))
39 INIT_STDERR_LOGGING(loglevel
);
41 static char *concat_args(unsigned argc
, char * const *argv
)
46 for (i
= 0; i
< argc
; i
++) {
47 const char *arg
= argv
? argv
[i
] : lls_input(i
, lpr
);
48 buf
= para_strcat(buf
, arg
);
50 buf
= para_strcat(buf
, "\n");
55 static int connect_audiod(const char *sname
, char *args
)
59 ret
= connect_local_socket(sname
);
63 ret
= send_cred_buffer(fd
, args
);
68 PARA_ERROR_LOG("could not connect %s\n", sname
);
77 #include "buffer_tree.h"
78 #include "interactive.h"
80 static struct sched sched
;
84 struct btr_node
*btrn
;
88 static struct i9e_completer audiod_completers
[];
90 I9E_DUMMY_COMPLETER(cycle
);
91 I9E_DUMMY_COMPLETER(off
);
92 I9E_DUMMY_COMPLETER(on
);
93 I9E_DUMMY_COMPLETER(sb
);
94 I9E_DUMMY_COMPLETER(tasks
);
95 I9E_DUMMY_COMPLETER(term
);
97 static void help_completer(struct i9e_completion_info
*ci
,
98 struct i9e_completion_result
*cr
)
100 char *opts
[] = {LSG_AUDIOD_CMD_HELP_OPTS
, NULL
};
102 if (ci
->word
[0] == '-') {
103 i9e_complete_option(opts
, ci
, cr
);
106 cr
->matches
= i9e_complete_commands(ci
->word
, audiod_completers
);
109 static void version_completer(struct i9e_completion_info
*ci
,
110 struct i9e_completion_result
*cr
)
112 char *opts
[] = {LSG_AUDIOD_CMD_VERSION_OPTS
, NULL
};
114 if (ci
->word_num
<= 2 && ci
->word
&& ci
->word
[0] == '-')
115 i9e_complete_option(opts
, ci
, cr
);
118 static void stat_completer(struct i9e_completion_info
*ci
,
119 struct i9e_completion_result
*cr
)
121 char *sia
[] = {STATUS_ITEMS NULL
};
122 char *opts
[] = {LSG_AUDIOD_CMD_STAT_OPTS
, NULL
};
124 if (ci
->word_num
<= 2 && ci
->word
&& ci
->word
[0] == '-')
125 i9e_complete_option(opts
, ci
, cr
);
127 i9e_extract_completions(ci
->word
, sia
, &cr
->matches
);
130 static void grab_completer(struct i9e_completion_info
*ci
,
131 struct i9e_completion_result
*cr
)
133 char *opts
[] = {LSG_AUDIOD_CMD_GRAB_OPTS
, NULL
};
134 i9e_complete_option(opts
, ci
, cr
);
137 static struct i9e_completer audiod_completers
[] = {
138 #define LSG_AUDIOD_CMD_CMD(_name) {.name = #_name, \
139 .completer = _name ## _completer}
140 LSG_AUDIOD_CMD_SUBCOMMANDS
141 #undef LSG_AUDIOD_CMD_CMD
145 static void audioc_pre_select(struct sched
*s
, void *context
)
147 struct audioc_task
*at
= context
;
148 int ret
= btr_node_status(at
->btrn
, 0, BTR_NT_ROOT
);
152 para_fd_set(at
->fd
, &s
->rfds
, &s
->max_fileno
);
155 static int audioc_post_select(struct sched
*s
, void *context
)
158 struct audioc_task
*at
= context
;
159 int ret
= btr_node_status(at
->btrn
, 0, BTR_NT_ROOT
);
164 if (!FD_ISSET(at
->fd
, &s
->rfds
))
166 bufsize
= PARA_MAX(1024U, OPT_UINT32_VAL(BUFSIZE
));
167 buf
= para_malloc(bufsize
);
168 ret
= recv_bin_buffer(at
->fd
, buf
, bufsize
);
169 PARA_DEBUG_LOG("recv: %d\n", ret
);
174 btr_add_output(buf
, ret
, at
->btrn
);
179 btr_remove_node(&at
->btrn
);
185 static struct audioc_task audioc_task
, *at
= &audioc_task
;
187 static int audioc_i9e_line_handler(char *line
)
192 PARA_DEBUG_LOG("line: %s\n", line
);
193 ret
= create_argv(line
, " ", &argv
);
197 args
= concat_args(argc
, argv
);
201 ret
= connect_audiod(socket_name
, args
);
206 ret
= mark_fd_nonblocking(at
->fd
);
209 at
->btrn
= btr_new_node(&(struct btr_node_description
)
210 EMBRACE(.name
= "audioc line handler"));
211 at
->task
= task_register(&(struct task_info
) {
213 .pre_select
= audioc_pre_select
,
214 .post_select
= audioc_post_select
,
217 i9e_attach_to_stdout(at
->btrn
);
224 __noreturn
static void interactive_session(void)
228 struct sigaction act
;
229 struct i9e_client_info ici
= {
231 .prompt
= "para_audioc> ",
232 .line_handler
= audioc_i9e_line_handler
,
233 .loglevel
= loglevel
,
234 .completers
= audiod_completers
,
237 PARA_NOTICE_LOG("\n%s\n", version_text("audioc"));
238 if (OPT_GIVEN(HISTORY_FILE
))
239 history_file
= para_strdup(OPT_STRING_VAL(HISTORY_FILE
));
241 char *home
= para_homedir();
242 history_file
= make_message("%s/.paraslash/audioc.history",
246 ici
.history_file
= history_file
;
248 act
.sa_handler
= i9e_signal_dispatch
;
249 sigemptyset(&act
.sa_mask
);
251 sigaction(SIGINT
, &act
, NULL
);
252 sched
.select_function
= i9e_select
;
254 sched
.default_timeout
.tv_sec
= 1;
255 ret
= i9e_open(&ici
, &sched
);
259 ret
= schedule(&sched
);
260 sched_shutdown(&sched
);
262 para_log
= stderr_log
;
267 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
268 exit(ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
);
271 __noreturn
static void print_completions(void)
273 int ret
= i9e_print_completions(audiod_completers
);
274 exit(ret
<= 0? EXIT_FAILURE
: EXIT_SUCCESS
);
277 #else /* HAVE_READLINE */
279 __noreturn
static void interactive_session(void)
281 PARA_EMERG_LOG("interactive sessions not available\n");
285 __noreturn
static void print_completions(void)
287 PARA_EMERG_LOG("command completion not available\n");
291 #endif /* HAVE_READLINE */
293 static char *configfile_exists(void)
296 char *home
= para_homedir();
298 config_file
= make_message("%s/.paraslash/audioc.conf", home
);
300 if (file_exists(config_file
))
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
;