-/*
- * Copyright (C) 2005-2013 Andre Noll <maan@systemlinux.org>
- *
- * Licensed under the GPL v2. For licencing details see COPYING.
- */
+/* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
/** \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 <stdbool.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 "lsu.h"
#include "net.h"
#include "string.h"
#include "fd.h"
-#include "ggo.h"
#include "version.h"
-INIT_AUDIOC_ERRLISTS;
+/** Array of error strings. */
+DEFINE_PARA_ERRLIST;
-/** The gengetopt structure containing command line args. */
-static struct audioc_args_info conf;
static char *socket_name;
+static struct lls_parse_result *lpr;
+#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);
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");
}
#include "sched.h"
#include "buffer_tree.h"
#include "interactive.h"
-#include "audiod_completion.h"
static struct sched sched;
struct audioc_task {
int fd;
struct btr_node *btrn;
- struct task task;
+ struct task *task;
};
static struct i9e_completer audiod_completers[];
I9E_DUMMY_COMPLETER(term);
static void help_completer(struct i9e_completion_info *ci,
- struct i9e_completion_result *result)
+ struct i9e_completion_result *cr)
+{
+ char *opts[] = {LSG_AUDIOD_CMD_HELP_OPTS, NULL};
+
+ if (ci->word[0] == '-') {
+ i9e_complete_option(opts, ci, cr);
+ return;
+ }
+ cr->matches = i9e_complete_commands(ci->word, audiod_completers);
+}
+
+static void ll_completer(struct i9e_completion_info *ci,
+ struct i9e_completion_result *cr)
{
- result->matches = i9e_complete_commands(ci->word, audiod_completers);
+ i9e_ll_completer(ci, cr);
+}
+
+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[] = {"-p", NULL};
+ char *sia[] = {STATUS_ITEMS 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);
static void grab_completer(struct i9e_completion_info *ci,
struct i9e_completion_result *cr)
{
- char *opts[] = {"-ms", "-ms", "-ma", "-p=", "-n=", "-o", NULL};
+ char *opts[] = {LSG_AUDIOD_CMD_GRAB_OPTS, NULL};
i9e_complete_option(opts, ci, cr);
}
static struct i9e_completer audiod_completers[] = {
- 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, struct task *t)
+static void audioc_pre_monitor(struct sched *s, void *context)
{
- struct audioc_task *at = container_of(t, struct audioc_task, task);
+ 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);
+ sched_monitor_readfd(at->fd, s);
}
-static int audioc_post_select(struct sched *s, struct task *t)
+static int audioc_post_monitor(struct sched *s, void *context)
{
char *buf = NULL;
- struct audioc_task *at = container_of(t, struct audioc_task, task);
+ 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))
+ if (!sched_read_ok(at->fd, s))
return 0;
- buf = para_malloc(conf.bufsize_arg);
- ret = recv_bin_buffer(at->fd, buf, conf.bufsize_arg);
+ bufsize = PARA_MAX(1024U, OPT_UINT32_VAL(BUFSIZE));
+ buf = alloc(bufsize);
+ ret = recv_bin_buffer(at->fd, buf, bufsize);
PARA_DEBUG_LOG("recv: %d\n", ret);
if (ret == 0)
ret = -E_AUDIOC_EOF;
return ret;
}
-static struct audioc_task audioc_task = {
- .task = {
- .pre_select = audioc_pre_select,
- .post_select = audioc_post_select,
- .status = "audioc task"
- },
-}, *at = &audioc_task;
+static struct audioc_task audioc_task, *at = &audioc_task;
static int audioc_i9e_line_handler(char *line)
{
- char *args = NULL;
- int ret;
+ int argc, ret;
+ char *args, **argv;
PARA_DEBUG_LOG("line: %s\n", line);
- ret = create_argv(line, " ", &conf.inputs);
+ ret = create_argv(line, " ", &argv);
if (ret < 0)
return ret;
- conf.inputs_num = ret;
- args = concat_args(conf.inputs_num, conf.inputs);
- free_argv(conf.inputs);
+ argc = ret;
+ args = concat_args(argc, argv);
+ free_argv(argv);
if (!args)
return 0;
- conf.inputs_num = 0; /* required for audioc_cmdline_parser_free() */
ret = connect_audiod(socket_name, args);
+ free(args);
if (ret < 0)
- goto out;
+ return ret;
at->fd = ret;
ret = mark_fd_nonblocking(at->fd);
if (ret < 0)
goto close;
- free(args);
- args = NULL;
at->btrn = btr_new_node(&(struct btr_node_description)
EMBRACE(.name = "audioc line handler"));
- at->task.error = 0;
- register_task(&sched, &at->task);
+ at->task = task_register(&(struct task_info) {
+ .name = "audioc",
+ .pre_monitor = audioc_pre_monitor,
+ .post_monitor = audioc_post_monitor,
+ .context = at,
+ }, &sched);
i9e_attach_to_stdout(at->btrn);
return 1;
close:
close(at->fd);
-out:
- free(args);
return ret;
}
.loglevel = loglevel,
.completers = audiod_completers,
};
+
PARA_NOTICE_LOG("\n%s\n", version_text("audioc"));
- if (conf.history_file_given)
- history_file = para_strdup(conf.history_file_arg);
+ if (OPT_GIVEN(HISTORY_FILE))
+ history_file = para_strdup(OPT_STRING_VAL(HISTORY_FILE));
else {
char *home = para_homedir();
history_file = make_message("%s/.paraslash/audioc.history",
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGINT, &act, NULL);
- sched.select_function = i9e_select;
+ sched.poll_function = i9e_poll;
- sched.default_timeout.tv_sec = 1;
+ sched.default_timeout = 1000;
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);
- audioc_cmdline_parser_free(&conf);
+ free(socket_name);
if (ret < 0)
PARA_ERROR_LOG("%s\n", para_strerror(-ret));
exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
#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;
-}
-
-__noreturn static void print_help_and_die(void)
+static void handle_help_flag(void)
{
- struct ggo_help h = DEFINE_GGO_HELP(audioc);
- bool d = conf.detailed_help_given;
+ char *help;
- ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
- exit(0);
+ 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);
}
/**
*
* \return EXIT_SUCCESS or EXIT_FAILURE.
*
- * \sa send_cred_buffer(), para_audioc(1), para_audiod(1).
+ * \sa \ref send_cred_buffer(), para_audioc(1), para_audiod(1).
*/
int main(int argc, char *argv[])
{
int ret, fd;
- char *cf, *buf = NULL, *args = NULL;
+ char *buf, *args, *errctx = NULL;
size_t bufsize;
+ unsigned num_inputs;
- audioc_cmdline_parser(argc, argv, &conf);
- loglevel = get_loglevel_by_name(conf.loglevel_arg);
- version_handle_flag("audioc", conf.version_given);
- if (conf.help_given || conf.detailed_help_given)
- print_help_and_die();
- cf = configfile_exists();
- if (cf) {
- struct audioc_cmdline_parser_params params = {
- .override = 0,
- .initialize = 0,
- .check_required = 0,
- .check_ambiguity = 0,
- .print_errors = 1,
-
- };
- audioc_cmdline_parser_config_file(cf, &conf, ¶ms);
- free(cf);
- loglevel = get_loglevel_by_name(conf.loglevel_arg);
- }
- if (conf.socket_given)
- socket_name = para_strdup(conf.socket_arg);
+ ret = lls(lls_parse(argc, argv, CMD_PTR, &lpr, &errctx));
+ if (ret < 0)
+ goto fail;
+ version_handle_flag("audioc", OPT_GIVEN(VERSION));
+ handle_help_flag();
+ ret = lsu_merge_config_file_options(NULL, "audioc.conf",
+ &lpr, CMD_PTR, audioc_suite, 0 /* default flags */);
+ if (ret < 0)
+ goto fail;
+ loglevel = OPT_UINT32_VAL(LOGLEVEL);
+ 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);
free(hn);
}
-
- if (conf.complete_given)
- print_completions();
-
- if (conf.inputs_num == 0)
+ num_inputs = lls_num_inputs(lpr);
+ if (num_inputs == 0)
interactive_session();
- args = concat_args(conf.inputs_num, conf.inputs);
+ 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 = mark_fd_blocking(STDOUT_FILENO);
if (ret < 0)
goto out;
- bufsize = conf.bufsize_arg;
- buf = para_malloc(bufsize);
+ bufsize = PARA_MAX(1024U, OPT_UINT32_VAL(BUFSIZE));
+ buf = alloc(bufsize);
do {
size_t n = ret = recv_bin_buffer(fd, buf, bufsize);
if (ret <= 0)
break;
ret = write_all(STDOUT_FILENO, buf, n);
} while (ret >= 0);
-out:
free(buf);
- free(args);
+out:
+ lls_free_parse_result(lpr, CMD_PTR);
+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;