/*
- * Copyright (C) 2005-2011 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
*
* Licensed under the GPL v2. For licencing details see COPYING.
*/
-/** \file audioc.c the client program used to connect to para_audiod */
+/** \file audioc.c The client program used to connect to para_audiod. */
+#include <netinet/in.h>
+#include <sys/socket.h>
#include <regex.h>
#include <sys/types.h>
-#include <dirent.h>
+#include <arpa/inet.h>
+#include <sys/un.h>
+#include <netdb.h>
+#include <signal.h>
+#include <lopsub.h>
+
+#include "audiod_cmd.lsg.h"
+#include "audioc.lsg.h"
-#include "audioc.cmdline.h"
#include "para.h"
#include "error.h"
#include "net.h"
#include "fd.h"
#include "version.h"
-INIT_AUDIOC_ERRLISTS;
+/** Array of error strings. */
+DEFINE_PARA_ERRLIST;
+
+static char *socket_name;
+static struct lls_parse_result *lpr;
-/** The gengetopt structure containing command line args. */
-static struct audioc_args_info conf;
+#define CMD_PTR (lls_cmd(0, audioc_suite))
+#define OPT_RESULT(_name) \
+ (lls_opt_result(LSG_AUDIOC_PARA_AUDIOC_OPT_ ## _name, lpr))
+#define OPT_GIVEN(_name) (lls_opt_given(OPT_RESULT(_name)))
+#define OPT_STRING_VAL(_name) (lls_string_val(0, OPT_RESULT(_name)))
+#define OPT_UINT32_VAL(_name) (lls_uint32_val(0, OPT_RESULT(_name)))
static int loglevel;
INIT_STDERR_LOGGING(loglevel);
static char *concat_args(unsigned argc, char * const *argv)
{
- int i; char *buf = NULL;
+ int i;
+ char *buf = NULL;
+
for (i = 0; i < argc; i++) {
- buf = para_strcat(buf, argv[i]);
+ const char *arg = argv? argv[i] : lls_input(i, lpr);
+ buf = para_strcat(buf, arg);
if (i != argc - 1)
buf = para_strcat(buf, "\n");
}
return buf;
}
-static char *configfile_exists(void)
+static int connect_audiod(const char *sname, char *args)
{
- static char *config_file;
- struct stat statbuf;
+ int fd = -1, ret;
+
+ ret = connect_local_socket(sname);
+ if (ret < 0)
+ goto fail;
+ fd = ret;
+ ret = send_cred_buffer(fd, args);
+ if (ret < 0)
+ goto fail;
+ return fd;
+fail:
+ PARA_ERROR_LOG("could not connect %s\n", sname);
+ if (fd >= 0)
+ close(fd);
+ return ret;
+}
+
+#ifdef HAVE_READLINE
+#include "list.h"
+#include "sched.h"
+#include "buffer_tree.h"
+#include "interactive.h"
+
+static struct sched sched;
+
+struct audioc_task {
+ int fd;
+ struct btr_node *btrn;
+ struct task *task;
+};
+
+static struct i9e_completer audiod_completers[];
+
+I9E_DUMMY_COMPLETER(cycle);
+I9E_DUMMY_COMPLETER(off);
+I9E_DUMMY_COMPLETER(on);
+I9E_DUMMY_COMPLETER(sb);
+I9E_DUMMY_COMPLETER(tasks);
+I9E_DUMMY_COMPLETER(term);
+
+static void help_completer(struct i9e_completion_info *ci,
+ struct i9e_completion_result *result)
+{
+ result->matches = i9e_complete_commands(ci->word, audiod_completers);
+}
+
+static void version_completer(struct i9e_completion_info *ci,
+ struct i9e_completion_result *cr)
+{
+ char *opts[] = {LSG_AUDIOD_CMD_VERSION_OPTS, NULL};
+
+ if (ci->word_num <= 2 && ci->word && ci->word[0] == '-')
+ i9e_complete_option(opts, ci, cr);
+}
+
+static void stat_completer(struct i9e_completion_info *ci,
+ struct i9e_completion_result *cr)
+{
+ char *sia[] = {STATUS_ITEM_ARRAY NULL};
+ char *opts[] = {LSG_AUDIOD_CMD_STAT_OPTS, NULL};
+
+ if (ci->word_num <= 2 && ci->word && ci->word[0] == '-')
+ i9e_complete_option(opts, ci, cr);
+ else
+ i9e_extract_completions(ci->word, sia, &cr->matches);
+}
+
+static void grab_completer(struct i9e_completion_info *ci,
+ struct i9e_completion_result *cr)
+{
+ char *opts[] = {LSG_AUDIOD_CMD_GRAB_OPTS, NULL};
+ i9e_complete_option(opts, ci, cr);
+}
+
+I9E_DUMMY_COMPLETER(SUPERCOMMAND_UNAVAILABLE);
+static struct i9e_completer audiod_completers[] = {
+#define LSG_AUDIOD_CMD_CMD(_name) {.name = #_name, \
+ .completer = _name ## _completer}
+ LSG_AUDIOD_CMD_SUBCOMMANDS
+#undef LSG_AUDIOD_CMD_CMD
+ {.name = NULL}
+};
+
+static void audioc_pre_select(struct sched *s, void *context)
+{
+ struct audioc_task *at = context;
+ int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
+
+ if (ret < 0)
+ sched_min_delay(s);
+ para_fd_set(at->fd, &s->rfds, &s->max_fileno);
+}
+static int audioc_post_select(struct sched *s, void *context)
+{
+ char *buf = NULL;
+ struct audioc_task *at = context;
+ int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT);
+ size_t bufsize;
+
+ if (ret < 0)
+ goto out;
+ if (!FD_ISSET(at->fd, &s->rfds))
+ return 0;
+ bufsize = PARA_MAX(1024U, OPT_UINT32_VAL(BUFSIZE));
+ buf = para_malloc(bufsize);
+ ret = recv_bin_buffer(at->fd, buf, bufsize);
+ PARA_DEBUG_LOG("recv: %d\n", ret);
+ if (ret == 0)
+ ret = -E_AUDIOC_EOF;
+ if (ret < 0)
+ goto out;
+ btr_add_output(buf, ret, at->btrn);
+ return 0;
+out:
+ if (ret < 0) {
+ free(buf);
+ btr_remove_node(&at->btrn);
+ close(at->fd);
+ }
+ return ret;
+}
- if (!config_file) {
+static struct audioc_task audioc_task, *at = &audioc_task;
+
+static int audioc_i9e_line_handler(char *line)
+{
+ int argc, ret;
+ char *args, **argv;
+
+ PARA_DEBUG_LOG("line: %s\n", line);
+ ret = create_argv(line, " ", &argv);
+ if (ret < 0)
+ return ret;
+ argc = ret;
+ args = concat_args(argc, argv);
+ free_argv(argv);
+ if (!args)
+ return 0;
+ ret = connect_audiod(socket_name, args);
+ free(args);
+ if (ret < 0)
+ return ret;
+ at->fd = ret;
+ ret = mark_fd_nonblocking(at->fd);
+ if (ret < 0)
+ goto close;
+ at->btrn = btr_new_node(&(struct btr_node_description)
+ EMBRACE(.name = "audioc line handler"));
+ at->task = task_register(&(struct task_info) {
+ .name = "audioc",
+ .pre_select = audioc_pre_select,
+ .post_select = audioc_post_select,
+ .context = at,
+ }, &sched);
+ i9e_attach_to_stdout(at->btrn);
+ return 1;
+close:
+ close(at->fd);
+ return ret;
+}
+
+__noreturn static void interactive_session(void)
+{
+ int ret;
+ char *history_file;
+ struct sigaction act;
+ struct i9e_client_info ici = {
+ .fds = {0, 1, 2},
+ .prompt = "para_audioc> ",
+ .line_handler = audioc_i9e_line_handler,
+ .loglevel = loglevel,
+ .completers = audiod_completers,
+ };
+
+ PARA_NOTICE_LOG("\n%s\n", version_text("audioc"));
+ if (OPT_GIVEN(HISTORY_FILE))
+ history_file = para_strdup(OPT_STRING_VAL(HISTORY_FILE));
+ else {
char *home = para_homedir();
- config_file = make_message("%s/.paraslash/audioc.conf", home);
+ history_file = make_message("%s/.paraslash/audioc.history",
+ home);
free(home);
}
+ ici.history_file = history_file;
+
+ act.sa_handler = i9e_signal_dispatch;
+ sigemptyset(&act.sa_mask);
+ act.sa_flags = 0;
+ sigaction(SIGINT, &act, NULL);
+ sched.select_function = i9e_select;
+
+ sched.default_timeout.tv_sec = 1;
+ ret = i9e_open(&ici, &sched);
+ if (ret < 0)
+ goto out;
+ para_log = i9e_log;
+ ret = schedule(&sched);
+ sched_shutdown(&sched);
+ i9e_close();
+ para_log = stderr_log;
+out:
+ free(history_file);
+ free(socket_name);
+ if (ret < 0)
+ PARA_ERROR_LOG("%s\n", para_strerror(-ret));
+ exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
+}
+
+__noreturn static void print_completions(void)
+{
+ int ret = i9e_print_completions(audiod_completers);
+ exit(ret <= 0? EXIT_FAILURE : EXIT_SUCCESS);
+}
+
+#else /* HAVE_READLINE */
+
+__noreturn static void interactive_session(void)
+{
+ PARA_EMERG_LOG("interactive sessions not available\n");
+ exit(EXIT_FAILURE);
+}
+
+__noreturn static void print_completions(void)
+{
+ PARA_EMERG_LOG("command completion not available\n");
+ exit(EXIT_FAILURE);
+}
+
+#endif /* HAVE_READLINE */
+
+static char *configfile_exists(void)
+{
+ char *config_file;
+ struct stat statbuf;
+ char *home = para_homedir();
+
+ config_file = make_message("%s/.paraslash/audioc.conf", home);
+ free(home);
if (!stat(config_file, &statbuf))
return config_file;
+ free(config_file);
return NULL;
}
+static void handle_help_flag(void)
+{
+ char *help;
+
+ if (OPT_GIVEN(DETAILED_HELP))
+ help = lls_long_help(CMD_PTR);
+ else if (OPT_GIVEN(HELP))
+ help = lls_short_help(CMD_PTR);
+ else
+ return;
+ printf("%s\n", help);
+ free(help);
+ exit(EXIT_SUCCESS);
+}
+
/**
- * the client program to connect to para_audiod
+ * The client program to connect to para_audiod.
*
- * \param argc usual argument count
- * \param argv usual argument vector
+ * \param argc Usual argument count.
+ * \param argv Usual argument vector.
*
- * It creates a temporary local socket in order to communicate with para_audiod.
- * Authentication consists in sending a ucred buffer that contains the user id.
+ * It connects to the "well-known" local socket to communicate with
+ * para_audiod. Authentication is performed by sending a ucred buffer
+ * containing the user id to the local socket.
*
- * Any output received through the local socket is sent to stdout.
+ * Any data received from the socket is written to stdout.
*
- * \return EXIT_SUCCESS or EXIT_FAILURE
+ * \return EXIT_SUCCESS or EXIT_FAILURE.
*
* \sa send_cred_buffer(), para_audioc(1), para_audiod(1).
*/
int main(int argc, char *argv[])
{
- int ret = -E_AUDIOC_SYNTAX, fd;
- char *cf, *buf = NULL, *args;
+ const struct lls_command *cmd = CMD_PTR;
+ int ret, fd;
+ char *cf = NULL, *buf, *args, *errctx = NULL;
size_t bufsize;
+ struct lls_parse_result *lpr1, *lpr2, *lpr3;
+ unsigned num_inputs;
- if (audioc_cmdline_parser(argc, argv, &conf))
- goto out;
- HANDLE_VERSION_FLAG("audioc", conf);
+ ret = lls(lls_parse(argc, argv, cmd, &lpr1, &errctx));
+ if (ret < 0)
+ goto fail;
+ lpr = lpr1;
+ loglevel = OPT_UINT32_VAL(LOGLEVEL);
+ version_handle_flag("audioc", OPT_GIVEN(VERSION));
+ handle_help_flag();
cf = configfile_exists();
if (cf) {
- struct audioc_cmdline_parser_params params = {
- .override = 0,
- .initialize = 0,
- .check_required = 0,
- .check_ambiguity = 0
- };
- if (audioc_cmdline_parser_config_file(cf, &conf, ¶ms)) {
- fprintf(stderr, "parse error in config file\n");
- exit(EXIT_FAILURE);
+ void *map;
+ size_t sz;
+ int cf_argc;
+ char **cf_argv;
+ ret = mmap_full_file(cf, O_RDONLY, &map, &sz, NULL);
+ if (ret != -E_EMPTY) {
+ if (ret < 0)
+ goto out;
+ ret = lls(lls_convert_config(map, sz, NULL, &cf_argv,
+ &errctx));
+ para_munmap(map, sz);
+ if (ret < 0) {
+ PARA_ERROR_LOG("syntax error in %s\n", cf);
+ goto out;
+ }
+ cf_argc = ret;
+ ret = lls(lls_parse(cf_argc, cf_argv, cmd, &lpr2,
+ &errctx));
+ lls_free_argv(cf_argv);
+ if (ret < 0) {
+ PARA_ERROR_LOG("parse error in %s\n", cf);
+ goto out;
+ }
+ ret = lls(lls_merge(lpr1, lpr2, cmd, &lpr3, &errctx));
+ lls_free_parse_result(lpr2, cmd);
+ if (ret < 0)
+ goto out;
+ lls_free_parse_result(lpr1, cmd);
+ lpr = lpr3;
+ loglevel = OPT_UINT32_VAL(LOGLEVEL);
}
}
- loglevel = get_loglevel_by_name(conf.loglevel_arg);
- args = conf.inputs_num?
- concat_args(conf.inputs_num, conf.inputs) :
- para_strdup("stat");
-
- if (conf.socket_given)
- ret = connect_local_socket(conf.socket_arg);
+ if (OPT_GIVEN(COMPLETE))
+ print_completions();
+ if (OPT_GIVEN(SOCKET))
+ socket_name = para_strdup(OPT_STRING_VAL(SOCKET));
else {
- char *hn = para_hostname(), *socket_name = make_message(
- "/var/paraslash/audiod_socket.%s", hn);
- ret = connect_local_socket(socket_name);
+ char *hn = para_hostname();
+ socket_name = make_message("/var/paraslash/audiod_socket.%s",
+ hn);
free(hn);
- free(socket_name);
}
- if (ret < 0) {
- PARA_EMERG_LOG("failed to connect to local socket\n");
+ num_inputs = lls_num_inputs(lpr);
+ if (num_inputs == 0)
+ interactive_session();
+
+ args = concat_args(num_inputs, NULL);
+ ret = connect_audiod(socket_name, args);
+ free(socket_name);
+ free(args);
+ if (ret < 0)
goto out;
- }
fd = ret;
- ret = send_cred_buffer(fd, args);
+ ret = mark_fd_blocking(STDOUT_FILENO);
if (ret < 0)
goto out;
- bufsize = conf.bufsize_arg;
+ bufsize = PARA_MAX(1024U, OPT_UINT32_VAL(BUFSIZE));
buf = para_malloc(bufsize);
do {
size_t n = ret = recv_bin_buffer(fd, buf, bufsize);
if (ret <= 0)
break;
- ret = write_all(STDOUT_FILENO, buf, &n);
+ ret = write_all(STDOUT_FILENO, buf, n);
} while (ret >= 0);
+ free(buf);
out:
+ lls_free_parse_result(lpr, cmd);
+ free(cf);
+fail:
+ if (errctx)
+ PARA_ERROR_LOG("%s\n", errctx);
+ free(errctx);
if (ret < 0)
PARA_ERROR_LOG("%s\n", para_strerror(-ret));
return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;