+static void stat_completer(struct i9e_completion_info *ci,
+ struct i9e_completion_result *cr)
+{
+ 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);
+ 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);
+}
+
+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;
+}
+
+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 {