* use differerent exit codes for error, no matches as >0 matches; as per man page

This commit is contained in:
Dirk-Jan C. Binnema 2011-03-05 15:07:49 +02:00
parent f14f8f45d7
commit 1e9ec55034
3 changed files with 25 additions and 11 deletions

View File

@ -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 : "<none>");
return FALSE;
}
/* if (!opts->params[1]) { */
/* g_warning ("usage: mu cfind [OPTIONS] [<ptrn>]"); */
/* return MU_EXITCODE_ERROR; */
/* } */
/* only one pattern allowed */
if (opts->params[1] && opts->params[2]) {
g_warning ("usage: mu cfind [OPTIONS] [<ptrn>]");
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;
}

View File

@ -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;
}

View File

@ -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