mirror of https://github.com/djcb/mu.git
* use differerent exit codes for error, no matches as >0 matches; as per man page
This commit is contained in:
parent
f14f8f45d7
commit
1e9ec55034
|
@ -101,6 +101,8 @@ mu_cmd_cfind (MuConfig *opts)
|
||||||
{
|
{
|
||||||
OutputFormat format;
|
OutputFormat format;
|
||||||
MuContacts *contacts;
|
MuContacts *contacts;
|
||||||
|
gboolean rv;
|
||||||
|
size_t num;
|
||||||
|
|
||||||
g_return_val_if_fail (opts, MU_EXITCODE_ERROR);
|
g_return_val_if_fail (opts, MU_EXITCODE_ERROR);
|
||||||
g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_CFIND,
|
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>");
|
opts->formatstr ? opts->formatstr : "<none>");
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if (!opts->params[1]) { */
|
/* only one pattern allowed */
|
||||||
/* g_warning ("usage: mu cfind [OPTIONS] [<ptrn>]"); */
|
if (opts->params[1] && opts->params[2]) {
|
||||||
/* return MU_EXITCODE_ERROR; */
|
g_warning ("usage: mu cfind [OPTIONS] [<ptrn>]");
|
||||||
/* } */
|
return MU_EXITCODE_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
contacts = mu_contacts_new (mu_runtime_contacts_cache_file());
|
contacts = mu_contacts_new (mu_runtime_contacts_cache_file());
|
||||||
if (!contacts) {
|
if (!contacts) {
|
||||||
|
@ -124,10 +127,14 @@ mu_cmd_cfind (MuConfig *opts)
|
||||||
return MU_EXITCODE_ERROR;
|
return MU_EXITCODE_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
mu_contacts_foreach (contacts, (MuContactsForeachFunc)each_contact,
|
rv = mu_contacts_foreach (contacts, (MuContactsForeachFunc)each_contact,
|
||||||
GINT_TO_POINTER(format), opts->params[1]);
|
GINT_TO_POINTER(format), opts->params[1], &num);
|
||||||
mu_contacts_destroy (contacts);
|
|
||||||
|
|
||||||
return MU_OK;
|
mu_contacts_destroy (contacts);
|
||||||
|
|
||||||
|
if (rv)
|
||||||
|
return (num == 0) ? MU_EXITCODE_NO_MATCHES : MU_EXITCODE_OK;
|
||||||
|
else
|
||||||
|
return MU_EXITCODE_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -137,6 +137,7 @@ struct _EachContactData {
|
||||||
MuContactsForeachFunc _func;
|
MuContactsForeachFunc _func;
|
||||||
gpointer _user_data;
|
gpointer _user_data;
|
||||||
GRegex *_rx;
|
GRegex *_rx;
|
||||||
|
size_t _num;
|
||||||
};
|
};
|
||||||
typedef struct _EachContactData EachContactData;
|
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->_func (email, ci->_name, ci->_tstamp, ecdata->_user_data);
|
||||||
|
++ecdata->_num;
|
||||||
}
|
}
|
||||||
|
|
||||||
gboolean
|
gboolean
|
||||||
mu_contacts_foreach (MuContacts *self, MuContactsForeachFunc func,
|
mu_contacts_foreach (MuContacts *self, MuContactsForeachFunc func,
|
||||||
gpointer user_data, const char *pattern)
|
gpointer user_data, const char *pattern, size_t *num)
|
||||||
{
|
{
|
||||||
EachContactData ecdata;
|
EachContactData ecdata;
|
||||||
|
|
||||||
|
@ -183,12 +185,16 @@ mu_contacts_foreach (MuContacts *self, MuContactsForeachFunc func,
|
||||||
|
|
||||||
ecdata._func = func;
|
ecdata._func = func;
|
||||||
ecdata._user_data = user_data;
|
ecdata._user_data = user_data;
|
||||||
|
ecdata._num = 0;
|
||||||
|
|
||||||
g_hash_table_foreach (self->_hash, (GHFunc) each_contact, &ecdata);
|
g_hash_table_foreach (self->_hash, (GHFunc) each_contact, &ecdata);
|
||||||
|
|
||||||
if (ecdata._rx)
|
if (ecdata._rx)
|
||||||
g_regex_unref (ecdata._rx);
|
g_regex_unref (ecdata._rx);
|
||||||
|
|
||||||
|
if (num)
|
||||||
|
*num = ecdata._num;
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 user_data user data to pass to the callback
|
||||||
* @param pattern a regular expression which matches either the e-mail
|
* @param pattern a regular expression which matches either the e-mail
|
||||||
* or name, to filter out contacts, or NULL to not do any filtering.
|
* 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
|
* @return TRUE if the function succeeded, or FALSE if the provide
|
||||||
* regular expression was invalid (and not NULL)
|
* regular expression was invalid (and not NULL)
|
||||||
*/
|
*/
|
||||||
gboolean mu_contacts_foreach (MuContacts *contacts, MuContactsForeachFunc func,
|
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
|
G_END_DECLS
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue