static command_trie_t * command_trie_find_node (command_trie_t *trie, gchar *input, gboolean auto_complete) { command_trie_t *node = NULL; GList *l; /* FIXME: If this happens when a parent command is given, make it nice! */ if (input == NULL) { return NULL; } if (*input == 0) { /* End of token, return current action, or unique completion */ if (command_trie_valid_match (trie)) { node = trie; } else if (auto_complete) { node = command_trie_find_leaf (trie); } } else { /* Recurse in next trie node */ l = g_list_find_custom (trie->next, input, command_trie_elem_cmp); if (l != NULL) { node = command_trie_find_node ((command_trie_t *) l->data, input + 1, auto_complete); } } return node; }
/* Given a trie node, try to find a unique leaf, else return NULL. */ static command_trie_t* command_trie_find_leaf (command_trie_t *trie) { GList *succ; command_trie_t *leaf = NULL; if (trie) { succ = g_list_first (trie->next); if (succ && succ->next == NULL && !command_trie_valid_match (trie)) { /* Not a matching node, a single successor: recurse */ leaf = command_trie_find_leaf ((command_trie_t *) succ->data); } else if (!succ && command_trie_valid_match (trie)) { leaf = trie; } } return leaf; }
static gboolean command_trie_action_set (command_trie_t* node, command_action_t *action) { /* There is already an action registered for that node! */ if (command_trie_valid_match (node)) { return FALSE; } else { node->match.type = COMMAND_TRIE_MATCH_ACTION; node->match.action = action; return TRUE; } }