Commit c9e2cdac c9e2cdac6f0c33f1175f19ca9095bc26f5a099ac by Sergey Poznyakoff

Improve mu shell interface.

* mu/mu.h (mutool_command) <doc>: Rename to docstring.
<argdoc>: New member.
* mu/pop.c (com_apop): 2nd arg (password) is optional.  If not
supplied, ask for it using the mu_getpass function.
(pop_comtab): Add argdocs.  Use N_ wrappers around translatable
strings.
* mu/shell.c (default_comtab: Add argdocs.  Use N_ wrappers around
translatable strings.
(print_comtab): Rewrite.
(shell_history): Use pager.
1 parent c2b45bde
......@@ -24,7 +24,8 @@ struct mutool_command
int argmax; /* Max. allowed number of arguments (-1 means not
limited */
mutool_action_t func; /* Function to call to do the job. */
const char *doc; /* Documentation for this function. */
const char *argdoc; /* Documentation for the arguments */
const char *docstring;/* Documentation for this function. */
};
......
......@@ -304,13 +304,25 @@ static int
com_apop (int argc, char **argv)
{
int status;
char *pwd, *passbuf = NULL;
status = mu_pop3_apop (pop3, argv[1], argv[2]);
if (argc == 3)
pwd = argv[2];
else
{
status = mu_getpass (mustrin, mustrout, "Password:", &passbuf);
if (status)
return status;
pwd = passbuf;
}
status = mu_pop3_apop (pop3, argv[1], pwd);
if (status == 0)
{
username = strdup (argv[1]);
pop_session_status = pop_session_logged_in;
}
free (passbuf);
return status;
}
......@@ -694,40 +706,61 @@ com_quit (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
}
struct mutool_command pop_comtab[] = {
{ "apop", 3, 3, com_apop,
"Authenticate with APOP: APOP user secret" },
{ "apop", 2, 3, com_apop,
N_("USER [PASS]"),
N_("authenticate with APOP") },
{ "capa", 1, -1, com_capa,
"List capabilities: capa [-reread] [names...]" },
/* TRANSLATORS: -reread is a keyword; do not translate. */
N_("[-reread] [NAME...]"),
N_("list server capabilities") },
{ "disconnect", 1, 1,
com_disconnect, "Close connection: disconnect" },
com_disconnect,
NULL,
N_("close connection") },
{ "dele", 2, 2, com_dele,
"Mark message: DELE msgno" },
N_("NUMBER"),
N_("mark message for deletion") },
{ "list", 1, 2, com_list,
"List messages: LIST [msgno]" },
N_("[NUMBER]"),
N_("list messages") },
{ "noop", 1, 1, com_noop,
"Send no operation: NOOP" },
NULL,
N_("send a \"no operation\"") },
{ "pass", 1, 2, com_pass,
"Send passwd: PASS [passwd]" },
N_("[PASSWORD]"),
N_("send password") },
{ "connect", 1, 4, com_connect,
"Open connection: connect [-tls] hostname port" },
/* TRANSLATORS: --tls is a keyword. */
N_("[-tls] HOSTNAME [PORT]"),
N_("open connection") },
{ "quit", 1, 1, com_quit,
"Go to Update state : QUIT" },
NULL,
N_("quit pop3 session") },
{ "retr", 2, 2, com_retr,
"Dowload message: RETR msgno" },
"NUMBER",
N_("retrieve a message") },
{ "rset", 1, 1, com_rset,
"Unmark all messages: RSET" },
NULL,
N_("remove deletion marks") },
{ "stat", 1, 1, com_stat,
"Get the size and count of mailbox : STAT" },
NULL,
N_("get the mailbox size and number of messages in it") },
{ "stls", 1, 1, com_stls,
"Start TLS negotiation" },
NULL,
N_("start TLS negotiation") },
{ "top", 2, 3, com_top,
"Get the header of message: TOP msgno [lines]" },
"MSGNO [NUMBER]",
N_("display message headers and first NUMBER (default 5) lines of"
" its body") },
{ "uidl", 1, 2, com_uidl,
"Get the unique id of message: UIDL [msgno]" },
N_("[NUMBER]"),
N_("show unique message identifiers") },
{ "user", 2, 2, com_user,
"send login: USER user" },
N_("NAME"),
N_("send login") },
{ "verbose", 1, 4, com_verbose,
"Enable Protocol tracing: verbose [on|off|mask|unmask] [x1 [x2]]" },
"[on|off|mask|unmask] [secret [payload]]",
N_("control the protocol tracing") },
{ NULL }
};
......
......@@ -55,21 +55,89 @@ static int shell_history (int, char **);
#endif
struct mutool_command default_comtab[] = {
{ "prompt", -1, -1, shell_prompt, "set command prompt" },
{ "exit", 1, 1, shell_exit, "exit program" },
{ "help", 1, 2, shell_help, "display this text" },
{ "?", 1, 1, shell_help, "synonym for `help'" },
{ "prompt", -1, -1, shell_prompt,
N_("STRING"),
N_("set command prompt") },
{ "exit", 1, 1, shell_exit, NULL, N_("exit program") },
{ "help", 1, 2, shell_help,
N_("[COMMAND]"),
N_("display this text") },
{ "?", 1, 1, shell_help,
N_("[COMMAND]"),
N_("synonym for `help'") },
#ifdef WITH_READLINE
{ "history", 1, 1, shell_history, "show command history" },
{ "history", 1, 1, shell_history,
NULL,
N_("show command history") },
#endif
{ NULL }
};
#define DESCRCOL 25
#define NUMCOLS 80
#define DESCRWIDTH (NUMCOLS - DESCRCOL)
/* Print a single comtab entry */
static void
print_comtab (mu_stream_t stream, struct mutool_command *tab)
{
mu_stream_printf (stream, "%s\t\t%s\n", tab->name, tab->doc);
size_t size = 0;
const char *text;
if (tab->docstring == NULL)
return;
mu_stream_printf (stream, "%s ", tab->name);
size += strlen (tab->name) + 1;
if (tab->argdoc)
{
text = gettext (tab->argdoc);
mu_stream_printf (stream, "%s", text);
size += strlen (text);
}
if (size >= DESCRCOL)
mu_stream_printf (stream, "\n%-*s", DESCRCOL, "");
else
mu_stream_printf (stream, "%-*s", (int) (DESCRCOL - size), "");
text = gettext (tab->docstring);
size = strlen (text);
while (*text)
{
size_t len = size;
if (len > DESCRWIDTH)
{
/* Fold the text */
size_t n = 0;
while (n < len)
{
size_t delta;
char *p;
p = mu_str_skip_cset_comp (text + n, " \t");
delta = p - (text + n);
if (n + delta > DESCRWIDTH)
break;
n += delta;
p = mu_str_skip_cset (text + n, " \t");
delta = p - (text + n);
if (n + delta > DESCRWIDTH)
break;
n += delta;
}
len = n;
}
mu_stream_write (stream, text, len, NULL);
text += len;
size -= len;
mu_stream_write (stream, "\n", 1, NULL);
if (size)
mu_stream_printf (stream, "%-*s", DESCRCOL, "");
}
}
/* Print a single comtab entry.
......@@ -347,11 +415,13 @@ shell_history (int argc, char **argv)
{
int i;
HIST_ENTRY **hlist;
mu_stream_t out = mutool_open_pager ();
hlist = history_list ();
for (i = 0; i < history_length; i++)
mu_stream_printf (mustrout, "%4d) %s\n", i + 1, hlist[i]->line);
mu_stream_printf (out, "%4d) %s\n", i + 1, hlist[i]->line);
mu_stream_destroy (&out);
return 0;
}
......