diff options
author | Nicholas Marriott <nicholas.marriott@gmail.com> | 2008-06-21 14:11:39 +0000 |
---|---|---|
committer | Nicholas Marriott <nicholas.marriott@gmail.com> | 2008-06-21 14:11:39 +0000 |
commit | e70e9513a82a817edf35c86cb8b7f5ff2736c771 (patch) | |
tree | b49787558eeefccf049972406233d5ed02155d00 /cmd.c | |
parent | 456ff329c37e4c9acf815d74353638e4829e868e (diff) | |
download | rtmux-e70e9513a82a817edf35c86cb8b7f5ff2736c771.tar.gz rtmux-e70e9513a82a817edf35c86cb8b7f5ff2736c771.tar.bz2 rtmux-e70e9513a82a817edf35c86cb8b7f5ff2736c771.zip |
Simple tab completion of option names in command prompt.
Diffstat (limited to 'cmd.c')
-rw-r--r-- | cmd.c | 46 |
1 files changed, 45 insertions, 1 deletions
@@ -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) { |