+static int client_loglevel = LL_ERROR;
+DEFINE_STDERR_LOGGER(stderr_log, client_loglevel);
+__printf_2_3 void (*para_log)(int, const char*, ...) = stderr_log;
+
+#ifdef HAVE_READLINE
+#include "interactive.h"
+#include "server_completion.h"
+#include "afs_completion.h"
+
+struct exec_task {
+ struct task task;
+ struct btr_node *btrn;
+ char *result_buf;
+ size_t result_size;
+};
+
+static void exec_pre_select(struct sched *s, struct task *t)
+{
+ struct exec_task *et = container_of(t, struct exec_task, task);
+ int ret = btr_node_status(et->btrn, 0, BTR_NT_LEAF);
+
+ if (ret != 0)
+ sched_min_delay(s);
+}
+
+static int exec_post_select(__a_unused struct sched *s, struct task *t)
+{
+ struct exec_task *et = container_of(t, struct exec_task, task);
+ struct btr_node *btrn = et->btrn;
+ char *buf;
+ size_t sz;
+ int ret;
+
+ ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
+ if (ret <= 0)
+ return ret;
+ sz = btr_next_buffer(btrn, &buf);
+ if (sz <= 1)
+ goto out;
+ et->result_buf = para_realloc(et->result_buf, et->result_size + sz - 1);
+ memcpy(et->result_buf + et->result_size - 1, buf, sz - 1);
+ et->result_size += sz - 1;
+ et->result_buf[et->result_size - 1] = '\0';
+out:
+ btr_consume(btrn, sz);
+ return 0;
+}
+
+static int make_client_argv(const char *line)
+{
+ int ret;
+
+ free_argv(ct->conf.inputs);
+ ret = create_argv(line, " ", &ct->conf.inputs);
+ if (ret >= 0)
+ ct->conf.inputs_num = ret;
+ return ret;
+}
+
+static int execute_client_command(const char *cmd, char **result)
+{
+ int ret;
+ struct sched command_sched = {.default_timeout = {.tv_sec = 1}};
+ struct exec_task exec_task = {
+ .task = {
+ .pre_select = exec_pre_select,
+ .post_select = exec_post_select,
+ .status = "client exec task",
+ },
+ .result_buf = para_strdup(""),
+ .result_size = 1,
+ };
+ *result = NULL;
+ ret = make_client_argv(cmd);
+ if (ret < 0)
+ goto out;
+ exec_task.btrn = btr_new_node(&(struct btr_node_description)
+ EMBRACE(.name = "exec_collect"));
+ register_task(&command_sched, &exec_task.task);
+ ret = client_connect(ct, &command_sched, NULL, exec_task.btrn);
+ if (ret < 0)
+ goto out;
+ schedule(&command_sched);
+ *result = exec_task.result_buf;
+ btr_remove_node(&exec_task.btrn);
+ client_disconnect(ct);
+ ret = 1;
+out:
+ btr_remove_node(&exec_task.btrn);
+ if (ret < 0)
+ free(exec_task.result_buf);
+ return ret;
+}
+
+static int extract_matches_from_command(const char *word, char *cmd,
+ char ***matches)
+{
+ char *buf, **sl;
+ int ret;
+
+ ret = execute_client_command(cmd, &buf);
+ if (ret < 0)
+ return ret;
+ ret = create_argv(buf, "\n", &sl);
+ free(buf);
+ if (ret < 0)
+ return ret;
+ ret = i9e_extract_completions(word, sl, matches);
+ free_argv(sl);
+ return ret;
+}
+
+static int complete_attributes(const char *word, char ***matches)
+{
+ return extract_matches_from_command(word, "lsatt", matches);
+}
+
+static void complete_addblob(__a_unused const char *blob_type,
+ __a_unused struct i9e_completion_info *ci,
+ __a_unused struct i9e_completion_result *cr)
+{
+ cr->filename_completion_desired = true;
+}
+
+static void generic_blob_complete(const char *blob_type,
+ struct i9e_completion_info *ci,
+ struct i9e_completion_result *cr)
+{
+ char cmd[20];
+ sprintf(cmd, "ls%s", blob_type);
+ extract_matches_from_command(ci->word, cmd, &cr->matches);
+}
+
+static void complete_catblob(const char *blob_type,
+ struct i9e_completion_info *ci,
+ struct i9e_completion_result *cr)
+{
+ generic_blob_complete(blob_type, ci, cr);
+}
+
+static void complete_lsblob(const char *blob_type,
+ struct i9e_completion_info *ci,
+ struct i9e_completion_result *cr)
+{
+ char *opts[] = {"-i", "-l", "-r", NULL};
+
+ if (ci->word[0] == '-')
+ return i9e_complete_option(opts, ci, cr);
+ generic_blob_complete(blob_type, ci, cr);
+}
+
+static void complete_rmblob(const char *blob_type,
+ struct i9e_completion_info *ci,
+ struct i9e_completion_result *cr)
+{
+ generic_blob_complete(blob_type, ci, cr);
+}
+
+static void complete_mvblob(const char *blob_type,
+ struct i9e_completion_info *ci,
+ struct i9e_completion_result *cr)
+{
+ generic_blob_complete(blob_type, ci, cr);
+}
+
+/* these don't need any completions */
+I9E_DUMMY_COMPLETER(ff);
+I9E_DUMMY_COMPLETER(hup);
+I9E_DUMMY_COMPLETER(jmp);
+I9E_DUMMY_COMPLETER(next);
+I9E_DUMMY_COMPLETER(nomore);
+I9E_DUMMY_COMPLETER(pause);
+I9E_DUMMY_COMPLETER(play);
+I9E_DUMMY_COMPLETER(si);
+I9E_DUMMY_COMPLETER(term);
+I9E_DUMMY_COMPLETER(version);
+I9E_DUMMY_COMPLETER(stop);
+I9E_DUMMY_COMPLETER(addatt);
+I9E_DUMMY_COMPLETER(init);
+
+static struct i9e_completer completers[];
+
+static void help_completer(struct i9e_completion_info *ci,
+ struct i9e_completion_result *result)
+{
+ result->matches = i9e_complete_commands(ci->word, completers);
+}
+
+static void stat_completer(struct i9e_completion_info *ci,
+ struct i9e_completion_result *cr)
+{
+ char *opts[] = {"-n=", "-p", NULL};
+ //PARA_CRIT_LOG("word: %s\n", ci->word);
+ i9e_complete_option(opts, ci, cr);
+}
+
+static void sender_completer(struct i9e_completion_info *ci,
+ struct i9e_completion_result *cr)
+{
+ char *senders[] = {"http", "dccp", "udp", NULL};
+ char *http_cmds[] = {"on", "off", "allow", "deny", "help", NULL};
+ char *dccp_cmds[] = {"on", "off", "allow", "deny", "help", NULL};
+ char *udp_cmds[] ={"on", "off", "add", "delete", "help", NULL};
+ char *sender;
+ char **cmds;
+
+ //PARA_CRIT_LOG("wn: %d\n", ci->word_num);
+ if (ci->word_num == 0 || ci->word_num > 3)
+ return;
+ if (ci->word_num == 1 || (ci->word_num == 2 && *ci->word != '\0')) {
+ i9e_extract_completions(ci->word, senders, &cr->matches);
+ return;
+ }
+ sender = ci->argv[1];
+ //PARA_CRIT_LOG("sender: %s\n", sender);
+ if (strcmp(sender, "http") == 0)
+ cmds = http_cmds;
+ else if (strcmp(sender, "dccp") == 0)
+ cmds = dccp_cmds;
+ else if (strcmp(sender, "udp") == 0)
+ cmds = udp_cmds;
+ else
+ return;
+ i9e_extract_completions(ci->word, cmds, &cr->matches);
+}
+
+static void add_completer(struct i9e_completion_info *ci,
+ struct i9e_completion_result *cr)
+{
+ char *opts[] = {"-a", "-l", "-f", "-v", "--", NULL};
+
+ if (ci->word[0] == '-')
+ i9e_complete_option(opts, ci, cr);
+ cr->filename_completion_desired = true;
+}
+
+static void ls_completer(struct i9e_completion_info *ci,
+ struct i9e_completion_result *cr)
+{
+ char *opts[] = {
+ "--", "-l", "-ls", "-ll", "-lv", "-lp", "-lm", "-lc", "-p",
+ "-a", "-r", "-d", "-sp", "-sl", "-ss", "-sn", "-sf", "-sc",
+ "-si", "-sy", "-sb", "-sd", "-sa", NULL
+ };
+ if (ci->word[0] == '-')
+ i9e_complete_option(opts, ci, cr);
+ cr->filename_completion_desired = true;
+}
+
+static void setatt_completer(struct i9e_completion_info *ci,
+ struct i9e_completion_result *cr)
+{
+ char *buf, **sl;
+ int i, ret, num_atts;
+
+ if (ci->word_num == 0)
+ return;
+
+ if (*ci->word == '/' || *ci->word == '\0')
+ cr->filename_completion_desired = true;
+ if (*ci->word == '/')
+ return;
+ ret = execute_client_command("lsatt", &buf);
+ if (ret < 0)
+ return;
+ ret = create_argv(buf, "\n", &sl);
+ if (ret < 0)
+ goto out;
+ num_atts = ret;
+ sl = para_realloc(sl, (2 * num_atts + 1) * sizeof(char *));
+ for (i = 0; i < num_atts; i++) {
+ char *orig = sl[i];
+ sl[i] = make_message("%s+", orig);
+ sl[num_atts + i] = make_message("%s-", orig);
+ free(orig);
+ }
+ sl[2 * num_atts] = NULL;
+ i9e_extract_completions(ci->word, sl, &cr->matches);
+out:
+ free(buf);
+ free_argv(sl);
+}
+
+static void lsatt_completer(struct i9e_completion_info *ci,
+ struct i9e_completion_result *cr)
+{
+ char *opts[] = {"-i", "-l", "-r", NULL};
+
+ if (ci->word[0] == '-')
+ i9e_complete_option(opts, ci, cr);
+ else
+ complete_attributes(ci->word, &cr->matches);
+}
+
+static void mvatt_completer(struct i9e_completion_info *ci,
+ struct i9e_completion_result *cr)
+{
+ complete_attributes(ci->word, &cr->matches);
+}
+
+static void rmatt_completer(struct i9e_completion_info *ci,
+ struct i9e_completion_result *cr)