save
[protos/libecoli.git] / lib / main-readline.c
index 1d64402..a63adfc 100644 (file)
@@ -45,6 +45,9 @@
 #include <ecoli_node_int.h>
 #include <ecoli_node_option.h>
 #include <ecoli_node_cmd.h>
+#include <ecoli_node_many.h>
+#include <ecoli_node_once.h>
+#include <ecoli_node_file.h>
 
 static struct ec_node *commands;
 
@@ -52,9 +55,13 @@ static char *my_completion_entry(const char *s, int state)
 {
        static struct ec_completed *c;
        static struct ec_completed_iter *iter;
-       static const struct ec_completed_elt *elt;
-       char *out_string;
+       static const struct ec_completed_item *item;
 
+       (void)s;
+
+       /* don't append a quote */
+       rl_completion_suppress_quote = 1;
+       rl_basic_quote_characters = "";
 
        if (state == 0) {
                char *line;
@@ -72,19 +79,32 @@ static char *my_completion_entry(const char *s, int state)
                        return NULL;
 
                ec_completed_iter_free(iter);
-               iter = ec_completed_iter(c, EC_MATCH);
+               iter = ec_completed_iter(c, EC_MATCH | EC_PARTIAL_MATCH);
                if (iter == NULL)
                        return NULL;
        }
 
-       elt = ec_completed_iter_next(iter);
-       if (elt == NULL)
+       item = ec_completed_iter_next(iter);
+       if (item == NULL)
                return NULL;
 
-       if (asprintf(&out_string, "%s%s", s, elt->add) < 0)
-               return NULL;
+       if (c->count_match == 1) {
 
-       return out_string;
+               /* don't add the trailing space for partial completions */
+               if (state == 0) {
+                       if (item->type == EC_MATCH)
+                               rl_completion_suppress_append = 0;
+                       else
+                               rl_completion_suppress_append = 1;
+               }
+
+               return strdup(item->str);
+       } else if (rl_completion_type == '?') {
+               /* on second try only show the display part */
+               return strdup(item->display);
+       }
+
+       return strdup(item->str);
 }
 
 static char **my_attempted_completion(const char *text, int start, int end)
@@ -99,21 +119,33 @@ static char **my_attempted_completion(const char *text, int start, int end)
 }
 
 /* this function builds the help string */
-static char *get_tk_help(const struct ec_node *node)
+static char *get_node_help(const struct ec_completed_node *compnode)
 {
-       const struct ec_node *node2;
+       const struct ec_completed_item *item;
+       const struct ec_node *node;
        char *help = NULL;
-       char *tk_help = NULL;
-
-       for (node2 = node;
-            node2 != NULL && tk_help == NULL;
-            node2 = ec_node_parent(node2))
-               tk_help = ec_keyval_get(ec_node_attrs(node2), "help");
+       const char *node_help = NULL;
+       const char *node_desc = NULL;
+       size_t i;
+
+       /* Since we display only one help per node, only look at the first item
+        * to get the path. The objective is to retrieve the most precise
+        * help for this node. */
+       item = TAILQ_FIRST(&compnode->items);
+       for (i = 0; i < item->pathlen; i++) {
+               node = item->path[i];
+               if (node_help == NULL)
+                       node_help = ec_keyval_get(ec_node_attrs(node), "help");
+               if (node_desc == NULL)
+                       node_desc = ec_node_desc(node);
+       }
 
-       if (tk_help == NULL)
-               tk_help = "";
+       if (node_help == NULL)
+               node_help = "";
+       if (node_desc == NULL)
+               return NULL;
 
-       if (asprintf(&help, "%-20s %s", ec_node_desc(node), tk_help) < 0)
+       if (asprintf(&help, "%-20s %s", node_desc, node_help) < 0)
                return NULL;
 
        return help;
@@ -121,8 +153,7 @@ static char *get_tk_help(const struct ec_node *node)
 
 static int show_help(int ignore, int invoking_key)
 {
-       const struct ec_completed_elt *elt;
-       struct ec_completed_iter *iter;
+       const struct ec_completed_node *compnode;
        struct ec_completed *c;
        struct ec_parsed *p;
        char *line;
@@ -151,7 +182,14 @@ static int show_help(int ignore, int invoking_key)
        if (c == NULL)
                return 1;
 
-       count = ec_completed_count(c, EC_MATCH | EC_NO_MATCH);
+       /* let's display one contextual help per node */
+       count = 0;
+       TAILQ_FOREACH(compnode, &c->nodes, next) {
+               if (TAILQ_EMPTY(&compnode->items))
+                       continue;
+               count++;
+       }
+
        helps = calloc(count + match + 1, sizeof(char *));
        if (helps == NULL)
                return 1;
@@ -159,17 +197,15 @@ static int show_help(int ignore, int invoking_key)
        if (match)
                helps[1] = "<return>";
 
-       iter = ec_completed_iter(c, EC_MATCH | EC_NO_MATCH);
-       if (iter == NULL)
-               goto fail;
-
        /* strangely, rl_display_match_list() expects first index at 1 */
-       for (i = match + 1, elt = ec_completed_iter_next(iter);
-            i < count + match + 1 && elt != NULL;
-            i++, elt = ec_completed_iter_next(iter)) {
-               helps[i] = get_tk_help(elt->node);
+       i = match + 1;
+       TAILQ_FOREACH(compnode, &c->nodes, next) {
+               if (TAILQ_EMPTY(&compnode->items))
+                       continue;
+               helps[i++] = get_node_help(compnode);
        }
 
+       ec_completed_dump(stdout, c);
        ec_completed_free(c);
 
        rl_display_match_list(helps, count + match, 1000); /* XXX 1000 */
@@ -178,10 +214,7 @@ static int show_help(int ignore, int invoking_key)
 
        return 0;
 
-fail:
-       free(helps);
-       // free helps[n] XXX
-       return 1;
+       // free helps[n] XXX on error ?
 }
 
 static int create_commands(void)
@@ -196,7 +229,7 @@ static int create_commands(void)
        cmd = EC_NODE_SEQ(NULL,
                ec_node_str(NULL, "hello"),
                EC_NODE_OR("name",
-                       ec_node_str(NULL, "john"),
+                       ec_node_str("john", "john"),
                        ec_node_str(NULL, "johnny"),
                        ec_node_str(NULL, "mike")
                ),
@@ -206,20 +239,27 @@ static int create_commands(void)
                goto fail;
        ec_keyval_set(ec_node_attrs(cmd), "help",
                "say hello to someone several times", NULL);
+       ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "john")),
+               "help", "specific help for john", NULL);
        ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "name")),
                "help", "the name of the person", NULL);
        ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "int")),
-               "help", "an integer", NULL);
+               "help", "an integer (0-10)", NULL);
        if (ec_node_or_add(cmdlist, cmd) < 0)
                goto fail;
 
 
-       cmd = EC_NODE_CMD(NULL, "good morning bob|bobby|michael [count]",
+       cmd = EC_NODE_CMD(NULL, "good morning name [count]",
+                       EC_NODE_CMD("name", "bob|bobby|michael"),
                        ec_node_int("count", 0, 10, 10));
        if (cmd == NULL)
                goto fail;
        ec_keyval_set(ec_node_attrs(cmd), "help",
                "say good morning to someone several times", NULL);
+       ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "name")), "help",
+               "the person to greet", NULL);
+       ec_keyval_set(ec_node_attrs(ec_node_find(cmd, "count")), "help",
+               "how many times to greet", NULL);
        if (ec_node_or_add(cmdlist, cmd) < 0)
                goto fail;
 
@@ -234,10 +274,36 @@ static int create_commands(void)
                goto fail;
 
 
+       cmd = EC_NODE_CMD(NULL, "eat vegetables",
+                       ec_node_many("vegetables",
+                               EC_NODE_OR(NULL,
+                                       ec_node_str(NULL, "potatoes"),
+                                       ec_node_once(NULL,
+                                               ec_node_str(NULL, "carrots")),
+                                       ec_node_once(NULL,
+                                               ec_node_str(NULL, "pumpkins"))),
+                       1, 0));
+       if (cmd == NULL)
+               goto fail;
+       ec_keyval_set(ec_node_attrs(cmd), "help",
+               "eat vegetables (take some more potatoes)", NULL);
+       if (ec_node_or_add(cmdlist, cmd) < 0)
+               goto fail;
+
+
        cmd = EC_NODE_SEQ(NULL,
                ec_node_str(NULL, "bye")
        );
-       ec_keyval_set(ec_node_attrs(cmd), "help", "say bye to someone", NULL);
+       ec_keyval_set(ec_node_attrs(cmd), "help", "say bye", NULL);
+       if (ec_node_or_add(cmdlist, cmd) < 0)
+               goto fail;
+
+
+       cmd = EC_NODE_SEQ(NULL,
+               ec_node_str(NULL, "load"),
+               ec_node("file", NULL)
+       );
+       ec_keyval_set(ec_node_attrs(cmd), "help", "load a file", NULL);
        if (ec_node_or_add(cmdlist, cmd) < 0)
                goto fail;
 
@@ -257,10 +323,8 @@ static int create_commands(void)
 int main(void)
 {
        struct ec_parsed *p;
-//     const char *name;
        char *line;
 
-
        if (create_commands() < 0)
                return 1;