+ 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)
+{
+ char *opts[] = {LSG_SERVER_CMD_STAT_OPTS, NULL};
+ i9e_complete_option(opts, ci, cr);
+}
+
+static void version_completer(struct i9e_completion_info *ci,
+ struct i9e_completion_result *cr)
+{
+ char *opts[] = {LSG_SERVER_CMD_VERSION_OPTS, NULL};
+ 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;