+ 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;
+}