mirror of
https://github.com/tmate-io/tmate.git
synced 2025-08-19 09:52:06 +02:00
Simple tab completion of option names in command prompt.
This commit is contained in:
46
cmd.c
46
cmd.c
@@ -1,4 +1,4 @@
|
||||
/* $Id: cmd.c,v 1.52 2008-06-20 17:31:48 nicm Exp $ */
|
||||
/* $Id: cmd.c,v 1.53 2008-06-21 14:11:39 nicm Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
|
||||
@@ -70,6 +70,50 @@ const struct cmd_entry *cmd_table[] = {
|
||||
NULL
|
||||
};
|
||||
|
||||
char *
|
||||
cmd_complete(const char *s)
|
||||
{
|
||||
const struct cmd_entry **entryp;
|
||||
ARRAY_DECL(, const char *) list;
|
||||
char *prefix;
|
||||
u_int i;
|
||||
size_t j;
|
||||
|
||||
if (*s == '\0')
|
||||
return (xstrdup(s));
|
||||
|
||||
/* First, build a list of all the possible matches. */
|
||||
ARRAY_INIT(&list);
|
||||
for (entryp = cmd_table; *entryp != NULL; entryp++) {
|
||||
if (strncmp((*entryp)->name, s, strlen(s)) != 0)
|
||||
continue;
|
||||
ARRAY_ADD(&list, (*entryp)->name);
|
||||
}
|
||||
|
||||
/* If none, bail now with the original string. */
|
||||
if (ARRAY_LENGTH(&list) == 0) {
|
||||
ARRAY_FREE(&list);
|
||||
return (xstrdup(s));
|
||||
}
|
||||
|
||||
/* Now loop through the list and find the longest common prefix. */
|
||||
prefix = xstrdup(ARRAY_FIRST(&list));
|
||||
for (i = 1; i < ARRAY_LENGTH(&list); i++) {
|
||||
s = ARRAY_ITEM(&list, i);
|
||||
|
||||
j = strlen(s);
|
||||
if (j > strlen(prefix))
|
||||
j = strlen(prefix);
|
||||
for (; j > 0; j--) {
|
||||
if (prefix[j - 1] != s[j - 1])
|
||||
prefix[j - 1] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
ARRAY_FREE(&list);
|
||||
return (prefix);
|
||||
}
|
||||
|
||||
struct cmd *
|
||||
cmd_parse(int argc, char **argv, char **cause)
|
||||
{
|
||||
|
Reference in New Issue
Block a user