From 1e9ec55034bebad529affe2454f13de5275fabf1 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sat, 5 Mar 2011 15:07:49 +0200 Subject: [PATCH] * use differerent exit codes for error, no matches as >0 matches; as per man page --- src/mu-cmd-cfind.c | 25 ++++++++++++++++--------- src/mu-contacts.c | 8 +++++++- src/mu-contacts.h | 3 ++- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/mu-cmd-cfind.c b/src/mu-cmd-cfind.c index 3fd9ccb8..728e8be2 100644 --- a/src/mu-cmd-cfind.c +++ b/src/mu-cmd-cfind.c @@ -101,6 +101,8 @@ mu_cmd_cfind (MuConfig *opts) { OutputFormat format; MuContacts *contacts; + gboolean rv; + size_t num; g_return_val_if_fail (opts, MU_EXITCODE_ERROR); g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_CFIND, @@ -112,11 +114,12 @@ mu_cmd_cfind (MuConfig *opts) opts->formatstr ? opts->formatstr : ""); return FALSE; } - - /* if (!opts->params[1]) { */ - /* g_warning ("usage: mu cfind [OPTIONS] []"); */ - /* return MU_EXITCODE_ERROR; */ - /* } */ + + /* only one pattern allowed */ + if (opts->params[1] && opts->params[2]) { + g_warning ("usage: mu cfind [OPTIONS] []"); + return MU_EXITCODE_ERROR; + } contacts = mu_contacts_new (mu_runtime_contacts_cache_file()); if (!contacts) { @@ -124,10 +127,14 @@ mu_cmd_cfind (MuConfig *opts) return MU_EXITCODE_ERROR; } - mu_contacts_foreach (contacts, (MuContactsForeachFunc)each_contact, - GINT_TO_POINTER(format), opts->params[1]); - mu_contacts_destroy (contacts); + rv = mu_contacts_foreach (contacts, (MuContactsForeachFunc)each_contact, + GINT_TO_POINTER(format), opts->params[1], &num); - return MU_OK; + mu_contacts_destroy (contacts); + + if (rv) + return (num == 0) ? MU_EXITCODE_NO_MATCHES : MU_EXITCODE_OK; + else + return MU_EXITCODE_ERROR; } diff --git a/src/mu-contacts.c b/src/mu-contacts.c index 6cdc624a..007e4cc2 100644 --- a/src/mu-contacts.c +++ b/src/mu-contacts.c @@ -137,6 +137,7 @@ struct _EachContactData { MuContactsForeachFunc _func; gpointer _user_data; GRegex *_rx; + size_t _num; }; typedef struct _EachContactData EachContactData; @@ -156,11 +157,12 @@ each_contact (const char* email, ContactInfo *ci, EachContactData *ecdata) } ecdata->_func (email, ci->_name, ci->_tstamp, ecdata->_user_data); + ++ecdata->_num; } gboolean mu_contacts_foreach (MuContacts *self, MuContactsForeachFunc func, - gpointer user_data, const char *pattern) + gpointer user_data, const char *pattern, size_t *num) { EachContactData ecdata; @@ -183,12 +185,16 @@ mu_contacts_foreach (MuContacts *self, MuContactsForeachFunc func, ecdata._func = func; ecdata._user_data = user_data; + ecdata._num = 0; g_hash_table_foreach (self->_hash, (GHFunc) each_contact, &ecdata); if (ecdata._rx) g_regex_unref (ecdata._rx); + if (num) + *num = ecdata._num; + return TRUE; } diff --git a/src/mu-contacts.h b/src/mu-contacts.h index 36c60bde..89246223 100644 --- a/src/mu-contacts.h +++ b/src/mu-contacts.h @@ -79,12 +79,13 @@ typedef void (*MuContactsForeachFunc) (const char *email, const char *name, time * @param user_data user data to pass to the callback * @param pattern a regular expression which matches either the e-mail * or name, to filter out contacts, or NULL to not do any filtering. + * @param num receives the number of contacts found, or NULL * * @return TRUE if the function succeeded, or FALSE if the provide * regular expression was invalid (and not NULL) */ gboolean mu_contacts_foreach (MuContacts *contacts, MuContactsForeachFunc func, - gpointer user_data, const char* pattern); + gpointer user_data, const char* pattern, size_t *num); G_END_DECLS