+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_cmd.lsg.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, void *context)
+{
+ struct exec_task *et = context;
+ 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, void *context)
+{
+ struct exec_task *et = context;
+ 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;
+}
+
+/* Called from the line handler and the completers. This overwrites ct->lpr. */
+static int create_merged_lpr(const char *line)
+{
+ const struct lls_command *cmd = CLIENT_CMD_PTR;
+ int argc, ret;
+ char *cmdline, **argv, *errctx;
+ struct lls_parse_result *argv_lpr;
+ static struct lls_parse_result *orig_lpr;
+
+ if (!orig_lpr)
+ orig_lpr = ct->lpr;
+ ct->lpr = NULL;
+ cmdline = make_message("-- %s", line);
+ ret = create_shifted_argv(cmdline, " ", &argv);
+ free(cmdline);
+ if (ret < 0)
+ return ret;
+ argc = ret;
+ if (argc == 2) { /* no words (only blanks in line) */
+ free_argv(argv);
+ return 0;
+ }
+ argv[0] = para_strdup("--");
+ /*
+ * The original lpr for the interactive session has no non-option
+ * arguments. We create a fresh lpr from the words in "line" and merge
+ * it with the orignal lpr.
+ */
+ ret = lls(lls_parse(argc, argv, cmd, &argv_lpr, &errctx));
+ free_argv(argv);
+ if (ret < 0)
+ goto fail;
+ ret = lls(lls_merge(orig_lpr, argv_lpr, cmd, &ct->lpr, &errctx));
+ lls_free_parse_result(argv_lpr, cmd);
+ if (ret < 0)
+ goto fail;
+ return 1;
+fail:
+ if (errctx)
+ PARA_ERROR_LOG("%s\n", para_strerror(-ret));
+ free(errctx);
+ assert(ret < 0);
+ return ret;
+}
+
+/* called from completers */
+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 = {
+ .result_buf = para_strdup(""),
+ .result_size = 1,
+ };
+ struct lls_parse_result *old_lpr = ct->lpr;
+
+ *result = NULL;
+ ret = create_merged_lpr(cmd);
+ if (ret <= 0)
+ goto out;
+ exec_task.btrn = btr_new_node(&(struct btr_node_description)
+ EMBRACE(.name = "exec_collect"));
+ exec_task.task = task_register(&(struct task_info) {
+ .name = "client exec",
+ .pre_select = exec_pre_select,
+ .post_select = exec_post_select,
+ .context = &exec_task,
+ }, &command_sched);
+ ret = client_connect(ct, &command_sched, NULL, exec_task.btrn);
+ if (ret < 0)
+ goto out;
+ schedule(&command_sched);
+ sched_shutdown(&command_sched);
+ lls_free_parse_result(ct->lpr, CLIENT_CMD_PTR);
+ ct->lpr = old_lpr;
+ *result = exec_task.result_buf;
+ btr_remove_node(&exec_task.btrn);
+ 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[] = {LSG_SERVER_CMD_LSIMG_OPTS, 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(stop);
+I9E_DUMMY_COMPLETER(addatt);
+I9E_DUMMY_COMPLETER(init);
+I9E_DUMMY_COMPLETER(tasks);
+
+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)