+ 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"
+#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 int 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 0;
+ 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 0;
+out:
+ if (ret < 0) {
+ free(buf);
+ btr_remove_node(&at->btrn);
+ close(at->fd);
+ }
+ 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 int audioc_i9e_line_handler(char *line)
+{
+ char *args = NULL;
+ int ret;
+
+ 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);
+ if (!args)
+ return 0;
+ conf.inputs_num = 0; /* required for audioc_cmdline_parser_free() */
+ ret = connect_audiod(socket_name, args);
+ if (ret < 0)
+ goto out;
+ 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);
+ i9e_attach_to_stdout(at->btrn);
+ return 1;
+close:
+ close(at->fd);
+out:
+ free(args);
+ return ret;
+}