From a535ae69516f69b594eb7199175f7f4559152a4e Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Mon, 5 Sep 2011 20:46:06 +0200 Subject: [PATCH] Interactive mode for para_audioc. This is in the same spirit as the previous commit which added interactive session support for para_client. It implements command completion and command line history for para_audioc. Just as for para_client the new completion code in audioc.c is compiled in only if libreadline was found by configure. In this case para_audioc starts an interactive session if no command was given at the command line. --- audioc.c | 235 ++++++++++++++++++++++++++++++++++++++++++++++++-- configure.ac | 2 + error.h | 1 + ggo/audioc.m4 | 9 +- ggo/makefile | 2 +- 5 files changed, 238 insertions(+), 11 deletions(-) diff --git a/audioc.c b/audioc.c index 6b1a64f9..73bc2cf3 100644 --- a/audioc.c +++ b/audioc.c @@ -8,6 +8,7 @@ #include #include +#include #include "audioc.cmdline.h" #include "para.h" @@ -21,6 +22,8 @@ INIT_AUDIOC_ERRLISTS; /** The gengetopt structure containing command line args. */ static struct audioc_args_info conf; +static char *socket_name; + static int loglevel; INIT_STDERR_LOGGING(loglevel); @@ -38,6 +41,215 @@ static char *concat_args(unsigned argc, char * const *argv) return buf; } +#ifdef HAVE_READLINE +#include "list.h" +#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; +}; + +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 stat_completer(struct i9e_completion_info *ci, + struct i9e_completion_result *cr) +{ + char *sia[] = {STATUS_ITEM_ARRAY NULL}; + char *opts[] = {"-p", 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[] = {"-ms", "-ms", "-ma", "-p=", "-n=", "-o", NULL}; + i9e_complete_option(opts, ci, cr); +} + +static struct i9e_completer audiod_completers[] = { + AUDIOD_COMPLETERS + {.name = NULL} +}; + +static void audioc_pre_select(struct sched *s, struct task *t) +{ + struct audioc_task *at = container_of(t, struct audioc_task, task); + 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 void audioc_post_select(struct sched *s, struct task *t) +{ + char *buf = NULL; + struct audioc_task *at = container_of(t, struct audioc_task, task); + int ret = btr_node_status(at->btrn, 0, BTR_NT_ROOT); + + if (ret < 0) + goto out; + if (!FD_ISSET(at->fd, &s->rfds)) + return; + buf = para_malloc(conf.bufsize_arg); + ret = recv_bin_buffer(at->fd, buf, conf.bufsize_arg); + 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; +out: + if (ret < 0) { + free(buf); + btr_remove_node(at->btrn); + btr_free_node(at->btrn); + at->btrn = NULL; + close(at->fd); + } + t->error = 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 int audioc_i9e_line_handler(char *line) +{ + char *args = NULL; + int ret; + if (!line || !*line) + return 0; + + PARA_DEBUG_LOG("line: %s\n", line); + ret = create_argv(line, " ", &conf.inputs); + if (ret < 0) + return ret; + conf.inputs_num = ret; + args = concat_args(conf.inputs_num, conf.inputs); + free_argv(conf.inputs); + conf.inputs_num = 0; /* required for audioc_cmdline_parser_free() */ + ret = connect_local_socket(socket_name); + if (ret < 0) + goto out; + at->fd = ret; + ret = mark_fd_nonblocking(at->fd); + if (ret < 0) + goto close; + ret = send_cred_buffer(at->fd, args); + 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); + i9e_attach_to_stdout(at->btrn); + return 1; +close: + close(at->fd); +out: + free(args); + 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 (conf.history_file_given) + history_file = para_strdup(conf.history_file_arg); + else { + char *home = para_homedir(); + 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); + i9e_close(); + para_log = stderr_log; +out: + free(history_file); + audioc_cmdline_parser_free(&conf); + 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) { static char *config_file; @@ -91,19 +303,24 @@ int main(int argc, char *argv[]) } } 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); + socket_name = para_strdup(conf.socket_arg); 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 (conf.complete_given) + print_completions(); + + if (conf.inputs_num == 0) + interactive_session(); + args = concat_args(conf.inputs_num, conf.inputs); + + ret = connect_local_socket(socket_name); + free(socket_name); if (ret < 0) { PARA_EMERG_LOG("failed to connect to local socket\n"); goto out; diff --git a/configure.ac b/configure.ac index 11c64067..f51c44e6 100644 --- a/configure.ac +++ b/configure.ac @@ -916,6 +916,8 @@ if test "$have_readline" = "yes"; then all_errlist_objs="$all_errlist_objs interactive" client_errlist_objs="$client_errlist_objs interactive" client_ldflags="$client_ldflags $readline_libs" + audioc_errlist_objs="$audioc_errlist_objs buffer_tree interactive sched" + audioc_ldflags="$audioc_ldflags $readline_libs" AC_SUBST(readline_cppflags) AC_DEFINE(HAVE_READLINE, 1, define to 1 to turn on readline support) else diff --git a/error.h b/error.h index 8a0a9c2e..55c0ab02 100644 --- a/error.h +++ b/error.h @@ -230,6 +230,7 @@ extern const char **para_errlist[]; #define AUDIOC_ERRORS \ PARA_ERROR(AUDIOC_SYNTAX, "audioc syntax error"), \ + PARA_ERROR(AUDIOC_EOF, "audioc: end of file"), \ #define CLIENT_COMMON_ERRORS \ diff --git a/ggo/audioc.m4 b/ggo/audioc.m4 index ad112e27..5ecc2a92 100644 --- a/ggo/audioc.m4 +++ b/ggo/audioc.m4 @@ -1,5 +1,5 @@ include(header.m4) -include(loglevel.m4) + option "socket" s #~~~~~~~~~~~~~~~~ "well-known socket (default=/var/paraslash/audiod.socket.$HOSTNAME)" @@ -13,3 +13,10 @@ option "bufsize" b int typestr="bytes" default="8192" optional + + +define(CURRENT_PROGRAM,para_audioc) +define(DEFAULT_HISTORY_FILE,~/.paraslash/audioc.history) +include(loglevel.m4) +include(history_file.m4) +include(complete.m4) diff --git a/ggo/makefile b/ggo/makefile index 367bd377..48fff2e4 100644 --- a/ggo/makefile +++ b/ggo/makefile @@ -54,7 +54,7 @@ $(ggo_dir)/server.ggo $(ggo_dir)/audiod.ggo: \ $(ggo_dir)/group.m4 $(ggo_dir)/log_timing.m4 $(ggo_dir)/afh.ggo: $(ggo_dir)/loglevel.m4 -$(ggo_dir)/audioc.ggo: $(ggo_dir)/loglevel.m4 +$(ggo_dir)/audioc.ggo: $(ggo_dir)/loglevel.m4 $(ggo_dir)/history_file.m4 $(ggo_dir)/complete.m4 $(ggo_dir)/filter.ggo: $(ggo_dir)/loglevel.m4 $(ggo_dir)/fsck.ggo: $(ggo_dir)/loglevel.m4 $(ggo_dir)/gui.ggo: $(ggo_dir)/loglevel.m4 -- 2.39.5