* mu-cfind: some cleanups / better help

This commit is contained in:
djcb
2012-10-24 23:47:47 +03:00
parent 7f20ab33b9
commit a01196499f
8 changed files with 260 additions and 260 deletions

View File

@ -23,6 +23,8 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "mu-cmd.h"
#include "mu-util.h"
@ -31,6 +33,127 @@
#include "mu-contacts.h"
#include "mu-runtime.h"
/**
* guess the last name for the given name; clearly,
* this is just a rough guess for setting an initial value.
*
* @param name a name
*
* @return the last name, as a newly allocated string (free with
* g_free)
*/
static gchar*
guess_last_name (const char *name)
{
const gchar *lastsp;
if (!name)
return g_strdup ("");
lastsp = g_strrstr (name, " ");
return g_strdup (lastsp ? lastsp + 1 : "");
}
/**
* guess the first name for the given name; clearly,
* this is just a rough guess for setting an initial value.
*
* @param name a name
*
* @return the first name, as a newly allocated string (free with
* g_free)
*/
static gchar*
guess_first_name (const char *name)
{
const gchar *lastsp;
if (!name)
return g_strdup ("");
lastsp = g_strrstr (name, " ");
if (lastsp)
return g_strndup (name, lastsp - name);
else
return g_strdup (name);
}
/**
* guess some nick name for the given name; if we can determine an
* first name, last name, the nick will be first name + the first char
* of the last name. otherwise, it's just the first name. clearly,
* this is just a rough guess for setting an initial value for nicks.
*
* @param name a name
*
* @return the guessed nick, as a newly allocated string (free with g_free)
*/
static gchar*
cleanup_str (const char* str)
{
gchar *s;
const gchar *cur;
unsigned i;
if (mu_str_is_empty(str))
return g_strdup ("");
s = g_new0 (char, strlen(str) + 1);
for (cur = str, i = 0; *cur; ++cur) {
if (ispunct(*cur) || isspace(*cur))
continue;
else
s[i++] = *cur;
}
return s;
}
static gchar*
guess_nick (const char* name)
{
gchar *fname, *lname, *nick;
gchar initial[7];
fname = guess_first_name (name);
lname = guess_last_name (name);
/* if there's no last name, use first name as the nick */
if (mu_str_is_empty(fname) || mu_str_is_empty(lname)) {
g_free (lname);
nick = fname;
goto leave;
}
memset (initial, 0, sizeof(initial));
/* couldn't we get an initial for the last name? */
if (g_unichar_to_utf8 (g_utf8_get_char (lname), initial) == 0) {
g_free (lname);
nick = fname;
goto leave;
}
nick = g_strdup_printf ("%s%s", fname, initial);
g_free (fname);
g_free (lname);
leave:
{
gchar *tmp;
tmp = cleanup_str (nick);
g_free (nick);
nick = tmp;
}
return nick;
}
static void
print_header (MuConfigFormat format)
{
@ -52,8 +175,8 @@ each_contact_bbdb (const char *email, const char *name, time_t tstamp)
{
char *fname, *lname, *now, *timestamp;
fname = mu_str_guess_first_name (name);
lname = mu_str_guess_last_name (name);
fname = guess_first_name (name);
lname = guess_last_name (name);
now = mu_date_str ("%Y-%m-%d", time(NULL));
timestamp = mu_date_str ("%Y-%m-%d", tstamp);
@ -76,7 +199,7 @@ each_contact_mutt_alias (const char *email, const char *name)
if (!name)
return;
nick = mu_str_guess_nick (name);
nick = guess_nick (name);
mu_util_print_encoded ("alias %s %s <%s>\n",
nick, name, email);
g_free (nick);
@ -92,7 +215,7 @@ each_contact_wl (const char *email, const char *name)
if (!name)
return;
nick = mu_str_guess_nick (name);
nick = guess_nick (name);
mu_util_print_encoded ("%s \"%s\" \"%s\"\n",
email, nick, name);
g_free (nick);

View File

@ -273,7 +273,7 @@ config_options_group_cfind (void)
GOptionGroup *og;
GOptionEntry entries[] = {
{"format", 'o', 0, G_OPTION_ARG_STRING, &MU_CONFIG.formatstr,
"output format ('plain'(*), 'mutt', 'wanderlust',"
"output format ('plain'(*), 'mutt', 'wl',"
"'org-contact', 'csv')", "<format>"},
{"personal", 0, 0, G_OPTION_ARG_NONE, &MU_CONFIG.personal,
"whether to only get 'personal' contacts", NULL},
@ -476,7 +476,7 @@ cmd_from_string (const char *str)
static gboolean
parse_cmd (int *argcp, char ***argvp)
parse_cmd (int *argcp, char ***argvp, GError **err)
{
MU_CONFIG.cmd = MU_CONFIG_CMD_NONE;
MU_CONFIG.cmdstr = NULL;
@ -492,6 +492,23 @@ parse_cmd (int *argcp, char ***argvp)
MU_CONFIG.cmdstr = (*argvp)[1];
MU_CONFIG.cmd = cmd_from_string (MU_CONFIG.cmdstr);
#ifndef BUILD_GUILE
if (MU_CONFIG.cmd == MU_CONFIG_CMD_SCRIPT) {
mu_util_g_set_error (err, MU_ERROR_IN_PARAMETERS,
"command 'script' not supported");
return FALSE;
}
#endif /*!BUILD_GUILE*/
#ifndef BUILD_CRYPTO
if (MU_CONFIG.cmd == MU_CONFIG_CMD_VERIFIY) {
mu_util_g_set_error (err, MU_ERROR_IN_PARAMETERS,
"command 'verify' not supported");
return FALSE;
}
#endif /*!BUILD_CRYPTO */
return TRUE;
}
@ -627,9 +644,8 @@ show_usage (void)
static gboolean
parse_params (int *argcp, char ***argvp)
parse_params (int *argcp, char ***argvp, GError **err)
{
GError *err;
GOptionContext *context;
GOptionGroup *group;
gboolean rv;
@ -648,44 +664,32 @@ parse_params (int *argcp, char ***argvp)
case MU_CONFIG_CMD_HELP:
/* 'help' is special; sucks in the options of the
* command after it */
rv = g_option_context_parse (context, argcp, argvp, &err) &&
rv = g_option_context_parse (context, argcp, argvp, err) &&
cmd_help ();
break;
case MU_CONFIG_CMD_SCRIPT:
/* script feeds the rest of the options to the script, so
* we accept all of them */
g_option_context_set_ignore_unknown_options (context, TRUE);
/* fall through */
default:
group = get_option_group (MU_CONFIG.cmd);
if (group)
g_option_context_add_group(context, group);
rv = g_option_context_parse (context, argcp, argvp, &err);
rv = g_option_context_parse (context, argcp, argvp, err);
}
g_option_context_free (context);
if (rv)
return TRUE;
/* something when wrong */
g_printerr ("mu: option error: %s\n", err ? err->message : "?");
g_clear_error (&err);
return FALSE;
return rv ? TRUE : FALSE;
}
MuConfig*
mu_config_init (int *argcp, char ***argvp)
mu_config_init (int *argcp, char ***argvp, GError **err)
{
g_return_val_if_fail (argcp && argvp, NULL);
memset (&MU_CONFIG, 0, sizeof(MU_CONFIG));
if (!parse_cmd (argcp, argvp))
if (!parse_cmd (argcp, argvp, err))
goto errexit;
if (!parse_params(argcp, argvp))
if (!parse_params(argcp, argvp, err))
goto errexit;
/* fill in the defaults if user did not specify */

View File

@ -188,12 +188,14 @@ typedef struct _MuConfig MuConfig;
* mu_config_init, you should also call mu_config_uninit when the data
* is no longer needed.
*
* Note that is _static_ data, ie., mu_config_init will always return
* the same pointer
* Note that this is _static_ data, ie., mu_config_init will always
* return the same pointer
*
* @param opts options
* @param argcp: pointer to argc
* @param argvp: pointer to argv
* @param err: receives error information
*/
MuConfig *mu_config_init (int *argcp, char ***argvp)
MuConfig *mu_config_init (int *argcp, char ***argvp, GError **err)
G_GNUC_WARN_UNUSED_RESULT;
/**
* free the MuConfig structure

View File

@ -27,10 +27,18 @@ files must be specified with an absolute path.
#BEGIN MU_CONFIG_CMD_CFIND
#STRING
mu cfind [options] [<pattern>]
mu cfind [options] [--format=<format>] [<pattern>]
#STRING
mu cfind is the mu command to find contacts in the mu database and export them
for use in other programs.
<format> is one of:
mutt-alias
mutt-ab
wl
csv
org-contact
bbdb
#END
#BEGIN MU_CONFIG_CMD_EXTRACT