示例#1
0
static gchar *
command_tab_completion (const gchar *text, gint state)
{
	static GList *suffixes;
	static gint count;
	static command_trie_match_type_t match;

	/* The first time, compute the list of valid suffixes for the input */
	if (!state) {
		command_action_t *action;
		gchar **args, **tokens;
		gchar *buffer = rl_line_buffer;

		suffixes = NULL;
		while (*buffer == ' ' && *buffer != '\0') ++buffer; /* skip initial spaces */
		args = tokens = g_strsplit (buffer, " ", 0);
		count = g_strv_length (tokens);
		match = cli_context_complete_command (readline_cli_ctx, &args, &count,
		                                    &action, &suffixes);
		g_strfreev (tokens);
	}

	/* Return each valid suffix, one by one, on subsequent calls */
	while (suffixes) {
		gchar *suffix = NULL, *s;
		gint len;

		s = (gchar *) suffixes->data;
		len = strlen (text) + strlen (s);

		if (len > 0 && (count == 0 || (match == COMMAND_TRIE_MATCH_NONE && count <= 1))) {
			suffix = (gchar *) malloc (len + 1);
			snprintf (suffix, len + 1, "%s%s", text, s);
		}

		g_free (s);
		suffixes = g_list_delete_link (suffixes, suffixes);

		if (suffix != NULL) {
			return suffix;
		}
	}

	return NULL;
}
示例#2
0
command_trie_match_type_t
cli_context_find_command (cli_context_t *ctx, gchar ***argv, gint *argc,
                        command_action_t **action)
{
	return cli_context_complete_command (ctx, argv, argc, action, NULL);
}