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"
26 /** Array of error strings. */
29 static char *socket_name
;
30 static struct lls_parse_result
*lpr
;
32 #define CMD_PTR (lls_cmd(0, audioc_suite))
33 #define OPT_RESULT(_name) \
34 (lls_opt_result(LSG_AUDIOC_PARA_AUDIOC_OPT_ ## _name, lpr))
35 #define OPT_GIVEN(_name) (lls_opt_given(OPT_RESULT(_name)))
36 #define OPT_STRING_VAL(_name) (lls_string_val(0, OPT_RESULT(_name)))
37 #define OPT_UINT32_VAL(_name) (lls_uint32_val(0, OPT_RESULT(_name)))
40 INIT_STDERR_LOGGING(loglevel
);
42 static char *concat_args(unsigned argc
, char * const *argv
)
47 for (i
= 0; i
< argc
; i
++) {
48 const char *arg
= argv
? argv
[i
] : lls_input(i
, lpr
);
49 buf
= para_strcat(buf
, arg
);
51 buf
= para_strcat(buf
, "\n");
56 static int connect_audiod(const char *sname
, char *args
)
60 ret
= connect_local_socket(sname
);
64 ret
= send_cred_buffer(fd
, args
);
69 PARA_ERROR_LOG("could not connect %s\n", sname
);
78 #include "buffer_tree.h"
79 #include "interactive.h"
81 static struct sched sched
;
85 struct btr_node
*btrn
;
89 static struct i9e_completer audiod_completers
[];
91 I9E_DUMMY_COMPLETER(cycle
);
92 I9E_DUMMY_COMPLETER(off
);
93 I9E_DUMMY_COMPLETER(on
);
94 I9E_DUMMY_COMPLETER(sb
);
95 I9E_DUMMY_COMPLETER(tasks
);
96 I9E_DUMMY_COMPLETER(term
);
98 static void help_completer(struct i9e_completion_info
*ci
,
99 struct i9e_completion_result
*cr
)
101 char *opts
[] = {LSG_AUDIOD_CMD_HELP_OPTS
, NULL
};
103 if (ci
->word
[0] == '-') {
104 i9e_complete_option(opts
, ci
, cr
);
107 cr
->matches
= i9e_complete_commands(ci
->word
, audiod_completers
);
110 static void ll_completer(struct i9e_completion_info
*ci
,
111 struct i9e_completion_result
*cr
)
113 i9e_ll_completer(ci
, cr
);
116 static void version_completer(struct i9e_completion_info
*ci
,
117 struct i9e_completion_result
*cr
)
119 char *opts
[] = {LSG_AUDIOD_CMD_VERSION_OPTS
, NULL
};
121 if (ci
->word_num
<= 2 && ci
->word
&& ci
->word
[0] == '-')
122 i9e_complete_option(opts
, ci
, cr
);
125 static void stat_completer(struct i9e_completion_info
*ci
,
126 struct i9e_completion_result
*cr
)
128 char *sia
[] = {STATUS_ITEMS NULL
};
129 char *opts
[] = {LSG_AUDIOD_CMD_STAT_OPTS
, NULL
};
131 if (ci
->word_num
<= 2 && ci
->word
&& ci
->word
[0] == '-')
132 i9e_complete_option(opts
, ci
, cr
);
134 i9e_extract_completions(ci
->word
, sia
, &cr
->matches
);
137 static void grab_completer(struct i9e_completion_info
*ci
,
138 struct i9e_completion_result
*cr
)
140 char *opts
[] = {LSG_AUDIOD_CMD_GRAB_OPTS
, NULL
};
141 i9e_complete_option(opts
, ci
, cr
);
144 static struct i9e_completer audiod_completers
[] = {
145 #define LSG_AUDIOD_CMD_CMD(_name) {.name = #_name, \
146 .completer = _name ## _completer}
147 LSG_AUDIOD_CMD_SUBCOMMANDS
148 #undef LSG_AUDIOD_CMD_CMD
152 static void audioc_pre_monitor(struct sched
*s
, void *context
)
154 struct audioc_task
*at
= context
;
155 int ret
= btr_node_status(at
->btrn
, 0, BTR_NT_ROOT
);
159 sched_monitor_readfd(at
->fd
, s
);
162 static int audioc_post_monitor(struct sched
*s
, void *context
)
165 struct audioc_task
*at
= context
;
166 int ret
= btr_node_status(at
->btrn
, 0, BTR_NT_ROOT
);
171 if (!sched_read_ok(at
->fd
, s
))
173 bufsize
= PARA_MAX(1024U, OPT_UINT32_VAL(BUFSIZE
));
174 buf
= alloc(bufsize
);
175 ret
= recv_bin_buffer(at
->fd
, buf
, bufsize
);
176 PARA_DEBUG_LOG("recv: %d\n", ret
);
181 btr_add_output(buf
, ret
, at
->btrn
);
186 btr_remove_node(&at
->btrn
);
192 static struct audioc_task audioc_task
, *at
= &audioc_task
;
194 static int audioc_i9e_line_handler(char *line
)
199 PARA_DEBUG_LOG("line: %s\n", line
);
200 ret
= create_argv(line
, " ", &argv
);
204 args
= concat_args(argc
, argv
);
208 ret
= connect_audiod(socket_name
, args
);
213 ret
= mark_fd_nonblocking(at
->fd
);
216 at
->btrn
= btr_new_node(&(struct btr_node_description
)
217 EMBRACE(.name
= "audioc line handler"));
218 at
->task
= task_register(&(struct task_info
) {
220 .pre_monitor
= audioc_pre_monitor
,
221 .post_monitor
= audioc_post_monitor
,
224 i9e_attach_to_stdout(at
->btrn
);
231 __noreturn
static void interactive_session(void)
235 struct sigaction act
;
236 struct i9e_client_info ici
= {
238 .prompt
= "para_audioc> ",
239 .line_handler
= audioc_i9e_line_handler
,
240 .loglevel
= loglevel
,
241 .completers
= audiod_completers
,
244 PARA_NOTICE_LOG("\n%s\n", version_text("audioc"));
245 if (OPT_GIVEN(HISTORY_FILE
))
246 history_file
= para_strdup(OPT_STRING_VAL(HISTORY_FILE
));
248 char *home
= para_homedir();
249 history_file
= make_message("%s/.paraslash/audioc.history",
253 ici
.history_file
= history_file
;
255 act
.sa_handler
= i9e_signal_dispatch
;
256 sigemptyset(&act
.sa_mask
);
258 sigaction(SIGINT
, &act
, NULL
);
259 sched
.poll_function
= i9e_poll
;
261 sched
.default_timeout
= 1000;
262 ret
= i9e_open(&ici
, &sched
);
266 ret
= schedule(&sched
);
267 sched_shutdown(&sched
);
269 para_log
= stderr_log
;
274 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
275 exit(ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
);
278 __noreturn
static void print_completions(void)
280 int ret
= i9e_print_completions(audiod_completers
);
281 exit(ret
<= 0? EXIT_FAILURE
: EXIT_SUCCESS
);
284 #else /* HAVE_READLINE */
286 __noreturn
static void interactive_session(void)
288 PARA_EMERG_LOG("interactive sessions not available\n");
292 __noreturn
static void print_completions(void)
294 PARA_EMERG_LOG("command completion not available\n");
298 #endif /* HAVE_READLINE */
300 static void handle_help_flag(void)
304 if (OPT_GIVEN(DETAILED_HELP
))
305 help
= lls_long_help(CMD_PTR
);
306 else if (OPT_GIVEN(HELP
))
307 help
= lls_short_help(CMD_PTR
);
310 printf("%s\n", help
);
316 * The client program to connect to para_audiod.
318 * \param argc Usual argument count.
319 * \param argv Usual argument vector.
321 * It connects to the "well-known" local socket to communicate with
322 * para_audiod. Authentication is performed by sending a ucred buffer
323 * containing the user id to the local socket.
325 * Any data received from the socket is written to stdout.
327 * \return EXIT_SUCCESS or EXIT_FAILURE.
329 * \sa \ref send_cred_buffer(), para_audioc(1), para_audiod(1).
331 int main(int argc
, char *argv
[])
334 char *buf
, *args
, *errctx
= NULL
;
338 ret
= lls(lls_parse(argc
, argv
, CMD_PTR
, &lpr
, &errctx
));
341 version_handle_flag("audioc", OPT_GIVEN(VERSION
));
343 ret
= lsu_merge_config_file_options(NULL
, "audioc.conf",
344 &lpr
, CMD_PTR
, audioc_suite
, 0 /* default flags */);
347 loglevel
= OPT_UINT32_VAL(LOGLEVEL
);
348 if (OPT_GIVEN(COMPLETE
))
350 if (OPT_GIVEN(SOCKET
))
351 socket_name
= para_strdup(OPT_STRING_VAL(SOCKET
));
353 char *hn
= para_hostname();
354 socket_name
= make_message("/var/paraslash/audiod_socket.%s",
358 num_inputs
= lls_num_inputs(lpr
);
360 interactive_session();
362 args
= concat_args(num_inputs
, NULL
);
363 ret
= connect_audiod(socket_name
, args
);
369 ret
= mark_fd_blocking(STDOUT_FILENO
);
372 bufsize
= PARA_MAX(1024U, OPT_UINT32_VAL(BUFSIZE
));
373 buf
= alloc(bufsize
);
375 size_t n
= ret
= recv_bin_buffer(fd
, buf
, bufsize
);
378 ret
= write_all(STDOUT_FILENO
, buf
, n
);
382 lls_free_parse_result(lpr
, CMD_PTR
);
385 PARA_ERROR_LOG("%s\n", errctx
);
388 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
389 return ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
;