Commit 976ba811 976ba811a8df6c0befb1052c47910130c173d0fb by Jakob Kaivo

readline tab completion for mail

1 parent 7eb89932
......@@ -35,35 +35,19 @@ mail_help (int argc, char **argv)
}
else
{
int status = 0;
int command = 0;
while (++command < argc)
int status = 0, cmd = 0;
while (++cmd < argc)
{
char *cmd = argv[command];
int i = 0, printed = 0, sl = 0, ll = 0, len = strlen (cmd);
while (mail_command_table[i].shortname != 0 && printed == 0)
{
sl = strlen (mail_command_table[i].shortname);
ll = strlen (mail_command_table[i].longname);
if (sl == len && !strcmp (mail_command_table[i].shortname, cmd))
{
printed = 1;
printf ("%s\n", mail_command_table[i].synopsis);
}
else if (sl < len && !strncmp (mail_command_table[i].longname,
cmd, len))
{
printed = 1;
printf ("%s\n", mail_command_table[i].synopsis);
}
i++;
}
if (printed == 0)
struct mail_command_entry entry = util_find_entry (argv[cmd]);
if (entry.synopsis != NULL)
printf ("%s\n", entry.synopsis);
else
{
printf ("Unknown command: %s\n", cmd);
status = 1;
printf ("Unknown command: %s\n", argv[cmd]);
}
}
return status;
}
return 1;
}
......
......@@ -113,7 +113,7 @@ static struct argp argp = { options, parse_opt, args_doc, doc };
int
main (int argc, char **argv)
{
char *command = NULL;
char *command = NULL, *cmd = NULL;
char *from[] = { "from", "*" };
struct arguments args;
......@@ -163,12 +163,32 @@ main (int argc, char **argv)
mail_from (1, from);
cursor = realcursor;
/* Initialize readline */
rl_readline_name = "mail";
rl_attempted_completion_function = (CPPFunction *)util_command_completion;
while (1)
{
int len;
free (command);
command = readline ("? ");
util_do_command (command);
add_history (command);
len = strlen (command);
while (command[len-1] == '\\')
{
char *buf;
char *command2 = readline ("> ");
command[len-1] = '\0';
buf = malloc ((len + strlen (command2)) * sizeof (char));
strcpy (buf, command);
strcat (buf, command2);
free (command);
command = buf;
len = strlen (command);
}
cmd = util_stripwhite (command);
util_do_command (cmd);
add_history (cmd);
}
}
......
......@@ -110,8 +110,11 @@ int util_get_argcv __P((const char *command, int *argc, char ***argv));
int util_expand_msglist __P((const int argc, char **argv, int **list));
int util_do_command __P((const char *cmd));
int util_msglist_command __P((int (*func)(int, char**), int argc, char **argv));
int* util_command_get __P((char *cmd));
Function* util_command_get __P((char *cmd));
int util_free_argv __P((int argc, char **argv));
char **util_command_completion __P((char *cmd, int start, int end));
char *util_command_generator __P((char *text, int state));
char *util_stripwhite __P((char *string));
#ifdef __cplusplus
}
......
......@@ -35,10 +35,12 @@ extern "C" {
struct mail_command_entry {
char *shortname;
char *longname;
int (*func) __P((int, char**));
Function *func;
char *synopsis;
};
struct mail_command_entry util_find_entry __P((char *cmd));
static struct mail_command_entry mail_command_table[] = {
{ "a", "alias", mail_alias,
"a[lias] [alias [address...]]" },
......
......@@ -166,15 +166,19 @@ util_do_command (const char *cmd)
int argc = 0;
char **argv = NULL;
int status = 0;
int (*command) (int, char**) = NULL;
Function *command;
if (cmd[0] == '#')
return 0;
if (cmd)
{
if (util_get_argcv (cmd, &argc, &argv) != 0)
return util_free_argv (argc, argv);
command = util_command_get (argv[0]);
}
else
command = util_command_get ("quit");
if (command != NULL)
status = command (argc, argv);
......@@ -218,23 +222,89 @@ util_msglist_command (int (*func)(int, char**), int argc, char **argv)
/*
* returns the function to run for command
*/
int *
Function *
util_command_get (char *cmd)
{
int i = 0;
struct mail_command_entry entry = util_find_entry (cmd);
return entry.func;
}
/*
* returns the mail_command_entry structure for the command matching cmd
*/
struct mail_command_entry
util_find_entry (char *cmd)
{
int i = 0, ll = 0, sl = 0;
int len = strlen (cmd);
int sl, ll;
while (mail_command_table[i].shortname != 0)
{
sl = strlen (mail_command_table[i].shortname);
ll = strlen (mail_command_table[i].longname);
if (sl == len && !strcmp (mail_command_table[i].shortname, cmd))
return mail_command_table[i].func;
return mail_command_table[i];
else if (sl < len && !strncmp (mail_command_table[i].longname, cmd, len))
return mail_command_table[i].func;
return mail_command_table[i];
i++;
}
return mail_command_table[i];
}
/*
* readline tab completion
*/
char **
util_command_completion (char *cmd, int start, int end)
{
if (start == 0)
return completion_matches (cmd, util_command_generator);
return NULL;
}
/*
* more readline
*/
char *
util_command_generator (char *text, int state)
{
static int i, len;
char *name;
if (!state)
{
i = 0;
len = strlen (text);
}
while ((name = mail_command_table[i].longname))
{
i++;
/*if (strlen (mail_command_table[i].shortname) > strlen(name))
name = mail_command_table[i].shortname; */
if (strncmp (name, text, len) == 0)
return (strdup(name));
}
return NULL;
}
/*
* removes whitespace from the beginning and end of a string
*/
char *
util_stripwhite (char *string)
{
register char *s, *t;
for (s = string; whitespace (*s); s++)
;
if (*s == 0)
return s;
t = s + strlen (s) - 1;
while (t > s && whitespace (*t))
t--;
*++t = '\0';
return s;
}
......