mirror of https://github.com/djcb/mu.git
* centralize formatstr parsing, cleanup
This commit is contained in:
parent
63076fe222
commit
0a6161a71c
|
@ -31,52 +31,15 @@
|
||||||
#include "mu-contacts.h"
|
#include "mu-contacts.h"
|
||||||
#include "mu-runtime.h"
|
#include "mu-runtime.h"
|
||||||
|
|
||||||
enum _OutputFormat {
|
|
||||||
FORMAT_PLAIN,
|
|
||||||
FORMAT_MUTT_ALIAS,
|
|
||||||
FORMAT_MUTT_AB, /* mutt external address book */
|
|
||||||
FORMAT_WL,
|
|
||||||
FORMAT_BBDB,
|
|
||||||
FORMAT_CSV,
|
|
||||||
FORMAT_ORG_CONTACT,
|
|
||||||
|
|
||||||
FORMAT_NONE
|
|
||||||
};
|
|
||||||
typedef enum _OutputFormat OutputFormat;
|
|
||||||
|
|
||||||
static OutputFormat
|
|
||||||
get_output_format (const char *formatstr)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
struct {
|
|
||||||
const char* name;
|
|
||||||
OutputFormat format;
|
|
||||||
} formats [] = {
|
|
||||||
{MU_CONFIG_FORMAT_PLAIN, FORMAT_PLAIN},
|
|
||||||
{MU_CONFIG_FORMAT_MUTT_ALIAS, FORMAT_MUTT_ALIAS},
|
|
||||||
{MU_CONFIG_FORMAT_MUTT_AB, FORMAT_MUTT_AB},
|
|
||||||
{MU_CONFIG_FORMAT_WL, FORMAT_WL},
|
|
||||||
{MU_CONFIG_FORMAT_BBDB, FORMAT_BBDB},
|
|
||||||
{MU_CONFIG_FORMAT_CSV, FORMAT_CSV},
|
|
||||||
{MU_CONFIG_FORMAT_ORG_CONTACT, FORMAT_ORG_CONTACT}
|
|
||||||
};
|
|
||||||
|
|
||||||
for (i = 0; i != G_N_ELEMENTS(formats); i++)
|
|
||||||
if (g_strcmp0 (formats[i].name, formatstr) == 0)
|
|
||||||
return formats[i].format;
|
|
||||||
|
|
||||||
return FORMAT_NONE;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
print_header (OutputFormat format)
|
print_header (MuConfigFormat format)
|
||||||
{
|
{
|
||||||
switch (format) {
|
switch (format) {
|
||||||
case FORMAT_BBDB:
|
case MU_CONFIG_FORMAT_BBDB:
|
||||||
g_print (";; -*-coding: utf-8-emacs;-*-\n"
|
g_print (";; -*-coding: utf-8-emacs;-*-\n"
|
||||||
";;; file-version: 6\n");
|
";;; file-version: 6\n");
|
||||||
break;
|
break;
|
||||||
case FORMAT_MUTT_AB:
|
case MU_CONFIG_FORMAT_MUTT_AB:
|
||||||
g_print ("Matching addresses in the mu database:\n");
|
g_print ("Matching addresses in the mu database:\n");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
@ -84,8 +47,6 @@ print_header (OutputFormat format)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
each_contact_bbdb (const char *email, const char *name, time_t tstamp)
|
each_contact_bbdb (const char *email, const char *name, time_t tstamp)
|
||||||
{
|
{
|
||||||
|
@ -110,28 +71,31 @@ each_contact_bbdb (const char *email, const char *name, time_t tstamp)
|
||||||
static void
|
static void
|
||||||
each_contact_mutt_alias (const char *email, const char *name)
|
each_contact_mutt_alias (const char *email, const char *name)
|
||||||
{
|
{
|
||||||
if (name) {
|
|
||||||
gchar *nick;
|
gchar *nick;
|
||||||
|
|
||||||
|
if (!name)
|
||||||
|
return;
|
||||||
|
|
||||||
nick = mu_str_guess_nick (name);
|
nick = mu_str_guess_nick (name);
|
||||||
mu_util_print_encoded ("alias %s %s <%s>\n",
|
mu_util_print_encoded ("alias %s %s <%s>\n",
|
||||||
nick, name, email);
|
nick, name, email);
|
||||||
|
|
||||||
g_free (nick);
|
g_free (nick);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
each_contact_wl (const char *email, const char *name)
|
each_contact_wl (const char *email, const char *name)
|
||||||
{
|
{
|
||||||
if (name) {
|
|
||||||
gchar *nick;
|
gchar *nick;
|
||||||
|
|
||||||
|
if (!name)
|
||||||
|
return;
|
||||||
|
|
||||||
nick = mu_str_guess_nick (name);
|
nick = mu_str_guess_nick (name);
|
||||||
mu_util_print_encoded ("%s \"%s\" \"%s\"\n",
|
mu_util_print_encoded ("%s \"%s\" \"%s\"\n",
|
||||||
email, nick, name);
|
email, nick, name);
|
||||||
g_free (nick);
|
g_free (nick);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -153,14 +117,19 @@ print_plain (const char *email, const char *name, gboolean color)
|
||||||
fputs (" ", stdout);
|
fputs (" ", stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (color) fputs (MU_COLOR_GREEN, stdout);
|
if (color)
|
||||||
|
fputs (MU_COLOR_GREEN, stdout);
|
||||||
|
|
||||||
mu_util_fputs_encoded (email, stdout);
|
mu_util_fputs_encoded (email, stdout);
|
||||||
if (color) fputs (MU_COLOR_DEFAULT, stdout);
|
|
||||||
|
if (color)
|
||||||
|
fputs (MU_COLOR_DEFAULT, stdout);
|
||||||
|
|
||||||
fputs ("\n", stdout);
|
fputs ("\n", stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct _ECData {
|
struct _ECData {
|
||||||
OutputFormat format;
|
MuConfigFormat format;
|
||||||
gboolean color;
|
gboolean color;
|
||||||
};
|
};
|
||||||
typedef struct _ECData ECData;
|
typedef struct _ECData ECData;
|
||||||
|
@ -171,14 +140,23 @@ each_contact (const char *email, const char *name, time_t tstamp,
|
||||||
ECData *ecdata)
|
ECData *ecdata)
|
||||||
{
|
{
|
||||||
switch (ecdata->format) {
|
switch (ecdata->format) {
|
||||||
case FORMAT_MUTT_ALIAS: each_contact_mutt_alias (email, name); break;
|
case MU_CONFIG_FORMAT_MUTT_ALIAS:
|
||||||
case FORMAT_MUTT_AB:
|
each_contact_mutt_alias (email, name);
|
||||||
|
break;
|
||||||
|
case MU_CONFIG_FORMAT_MUTT_AB:
|
||||||
mu_util_print_encoded ("%s\t%s\t\n",
|
mu_util_print_encoded ("%s\t%s\t\n",
|
||||||
email, name ? name : ""); break;
|
email, name ? name : "");
|
||||||
case FORMAT_WL: each_contact_wl (email, name); break;
|
break;
|
||||||
case FORMAT_ORG_CONTACT: each_contact_org_contact (email, name); break;
|
case MU_CONFIG_FORMAT_WL:
|
||||||
case FORMAT_BBDB: each_contact_bbdb (email, name, tstamp); break;
|
each_contact_wl (email, name);
|
||||||
case FORMAT_CSV:
|
break;
|
||||||
|
case MU_CONFIG_FORMAT_ORG_CONTACT:
|
||||||
|
each_contact_org_contact (email, name);
|
||||||
|
break;
|
||||||
|
case MU_CONFIG_FORMAT_BBDB:
|
||||||
|
each_contact_bbdb (email, name, tstamp);
|
||||||
|
break;
|
||||||
|
case MU_CONFIG_FORMAT_CSV:
|
||||||
mu_util_print_encoded ("%s,%s\n", name ? name : "", email);
|
mu_util_print_encoded ("%s,%s\n", name ? name : "", email);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
@ -188,7 +166,7 @@ each_contact (const char *email, const char *name, time_t tstamp,
|
||||||
|
|
||||||
|
|
||||||
static MuExitCode
|
static MuExitCode
|
||||||
run_cmd_cfind (const char* pattern, OutputFormat format,
|
run_cmd_cfind (const char* pattern, MuConfigFormat format,
|
||||||
gboolean color)
|
gboolean color)
|
||||||
{
|
{
|
||||||
gboolean rv;
|
gboolean rv;
|
||||||
|
@ -219,17 +197,19 @@ run_cmd_cfind (const char* pattern, OutputFormat format,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MuExitCode
|
static gboolean
|
||||||
mu_cmd_cfind (MuConfig *opts)
|
cfind_params_valid (MuConfig *opts)
|
||||||
{
|
{
|
||||||
OutputFormat format;
|
switch (opts->format) {
|
||||||
|
case MU_CONFIG_FORMAT_PLAIN:
|
||||||
g_return_val_if_fail (opts, MU_EXITCODE_ERROR);
|
case MU_CONFIG_FORMAT_MUTT_ALIAS:
|
||||||
g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_CFIND,
|
case MU_CONFIG_FORMAT_MUTT_AB:
|
||||||
MU_EXITCODE_ERROR);
|
case MU_CONFIG_FORMAT_WL:
|
||||||
|
case MU_CONFIG_FORMAT_BBDB:
|
||||||
format = get_output_format (opts->formatstr);
|
case MU_CONFIG_FORMAT_CSV:
|
||||||
if (format == FORMAT_NONE) {
|
case MU_CONFIG_FORMAT_ORG_CONTACT:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
g_warning ("invalid output format %s",
|
g_warning ("invalid output format %s",
|
||||||
opts->formatstr ? opts->formatstr : "<none>");
|
opts->formatstr ? opts->formatstr : "<none>");
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
@ -238,9 +218,23 @@ mu_cmd_cfind (MuConfig *opts)
|
||||||
/* only one pattern allowed */
|
/* only one pattern allowed */
|
||||||
if (opts->params[1] && opts->params[2]) {
|
if (opts->params[1] && opts->params[2]) {
|
||||||
g_warning ("usage: mu cfind [options] [<ptrn>]");
|
g_warning ("usage: mu cfind [options] [<ptrn>]");
|
||||||
return MU_EXITCODE_ERROR;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
return run_cmd_cfind (opts->params[1], format, opts->color);
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
MuExitCode
|
||||||
|
mu_cmd_cfind (MuConfig *opts)
|
||||||
|
{
|
||||||
|
g_return_val_if_fail (opts, MU_EXITCODE_ERROR);
|
||||||
|
g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_CFIND,
|
||||||
|
MU_EXITCODE_ERROR);
|
||||||
|
|
||||||
|
if (!cfind_params_valid (opts))
|
||||||
|
return MU_EXITCODE_ERROR;
|
||||||
|
|
||||||
|
return run_cmd_cfind (opts->params[1], opts->format, opts->color);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,17 +45,6 @@
|
||||||
#include "mu-cmd.h"
|
#include "mu-cmd.h"
|
||||||
#include "mu-threader.h"
|
#include "mu-threader.h"
|
||||||
|
|
||||||
enum _OutputFormat {
|
|
||||||
FORMAT_JSON,
|
|
||||||
FORMAT_LINKS,
|
|
||||||
FORMAT_PLAIN,
|
|
||||||
FORMAT_SEXP,
|
|
||||||
FORMAT_XML,
|
|
||||||
FORMAT_XQUERY,
|
|
||||||
|
|
||||||
FORMAT_NONE
|
|
||||||
};
|
|
||||||
typedef enum _OutputFormat OutputFormat;
|
|
||||||
|
|
||||||
static gboolean output_links (MuMsgIter *iter, const char* linksdir,
|
static gboolean output_links (MuMsgIter *iter, const char* linksdir,
|
||||||
gboolean clearlinks, size_t *count);
|
gboolean clearlinks, size_t *count);
|
||||||
|
@ -66,30 +55,6 @@ static gboolean output_plain (MuMsgIter *iter, const char *fields,
|
||||||
gboolean summary,gboolean threads,
|
gboolean summary,gboolean threads,
|
||||||
gboolean color, size_t *count);
|
gboolean color, size_t *count);
|
||||||
|
|
||||||
static OutputFormat
|
|
||||||
get_output_format (const char *formatstr)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
struct {
|
|
||||||
const char* name;
|
|
||||||
OutputFormat format;
|
|
||||||
} formats [] = {
|
|
||||||
{MU_CONFIG_FORMAT_JSON, FORMAT_JSON},
|
|
||||||
{MU_CONFIG_FORMAT_LINKS, FORMAT_LINKS},
|
|
||||||
{MU_CONFIG_FORMAT_PLAIN, FORMAT_PLAIN},
|
|
||||||
{MU_CONFIG_FORMAT_SEXP, FORMAT_SEXP},
|
|
||||||
{MU_CONFIG_FORMAT_XML, FORMAT_XML},
|
|
||||||
{MU_CONFIG_FORMAT_XQUERY, FORMAT_XQUERY}
|
|
||||||
};
|
|
||||||
|
|
||||||
for (i = 0; i != G_N_ELEMENTS(formats); i++)
|
|
||||||
if (strcmp (formats[i].name, formatstr) == 0)
|
|
||||||
return formats[i].format;
|
|
||||||
|
|
||||||
return FORMAT_NONE;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
upgrade_warning (void)
|
upgrade_warning (void)
|
||||||
{
|
{
|
||||||
|
@ -142,22 +107,20 @@ sort_field_from_string (const char* fieldstr)
|
||||||
|
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
output_query_results (MuMsgIter *iter, MuConfig *opts,
|
output_query_results (MuMsgIter *iter, MuConfig *opts, size_t *count)
|
||||||
OutputFormat format, size_t *count)
|
|
||||||
{
|
{
|
||||||
switch (format) {
|
switch (opts->format) {
|
||||||
|
case MU_CONFIG_FORMAT_LINKS:
|
||||||
case FORMAT_LINKS:
|
|
||||||
return output_links (iter, opts->linksdir, opts->clearlinks,
|
return output_links (iter, opts->linksdir, opts->clearlinks,
|
||||||
count);
|
count);
|
||||||
case FORMAT_PLAIN:
|
case MU_CONFIG_FORMAT_PLAIN:
|
||||||
return output_plain (iter, opts->fields, opts->summary,
|
return output_plain (iter, opts->fields, opts->summary,
|
||||||
opts->threads, opts->color, count);
|
opts->threads, opts->color, count);
|
||||||
case FORMAT_XML:
|
case MU_CONFIG_FORMAT_XML:
|
||||||
return output_xml (iter, count);
|
return output_xml (iter, count);
|
||||||
case FORMAT_JSON:
|
case MU_CONFIG_FORMAT_JSON:
|
||||||
return output_json (iter, count);
|
return output_json (iter, count);
|
||||||
case FORMAT_SEXP:
|
case MU_CONFIG_FORMAT_SEXP:
|
||||||
return output_sexp (iter, count);
|
return output_sexp (iter, count);
|
||||||
default:
|
default:
|
||||||
g_assert_not_reached ();
|
g_assert_not_reached ();
|
||||||
|
@ -196,7 +159,7 @@ run_query (MuQuery *xapian, const gchar *query, MuConfig *opts, size_t *count)
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
process_query (MuQuery *xapian, const gchar *query, MuConfig *opts,
|
process_query (MuQuery *xapian, const gchar *query, MuConfig *opts,
|
||||||
OutputFormat format, size_t *count)
|
size_t *count)
|
||||||
{
|
{
|
||||||
MuMsgIter *iter;
|
MuMsgIter *iter;
|
||||||
gboolean rv;
|
gboolean rv;
|
||||||
|
@ -205,7 +168,7 @@ process_query (MuQuery *xapian, const gchar *query, MuConfig *opts,
|
||||||
if (!iter)
|
if (!iter)
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
rv = output_query_results (iter, opts, format, count);
|
rv = output_query_results (iter, opts, count);
|
||||||
|
|
||||||
if (rv && count && *count == 0)
|
if (rv && count && *count == 0)
|
||||||
g_warning ("no matching messages found");
|
g_warning ("no matching messages found");
|
||||||
|
@ -277,25 +240,29 @@ exec_cmd_on_query (MuQuery *xapian, const gchar *query, MuConfig *opts,
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
format_params_valid (MuConfig *opts)
|
format_params_valid (MuConfig *opts)
|
||||||
{
|
{
|
||||||
OutputFormat format;
|
switch (opts->format) {
|
||||||
|
case MU_CONFIG_FORMAT_PLAIN:
|
||||||
format = get_output_format (opts->formatstr);
|
case MU_CONFIG_FORMAT_SEXP:
|
||||||
if (format == FORMAT_NONE) {
|
case MU_CONFIG_FORMAT_LINKS:
|
||||||
|
case MU_CONFIG_FORMAT_XML:
|
||||||
|
case MU_CONFIG_FORMAT_JSON:
|
||||||
|
case MU_CONFIG_FORMAT_XQUERY:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
g_warning ("invalid output format %s",
|
g_warning ("invalid output format %s",
|
||||||
opts->formatstr ? opts->formatstr : "<none>");
|
opts->formatstr ? opts->formatstr : "<none>");
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (format == FORMAT_LINKS && !opts->linksdir) {
|
if (opts->format == MU_CONFIG_FORMAT_LINKS && !opts->linksdir) {
|
||||||
g_warning ("missing --linksdir argument");
|
g_warning ("missing --linksdir argument");
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opts->linksdir && format != FORMAT_LINKS) {
|
if (opts->linksdir && opts->format != MU_CONFIG_FORMAT_LINKS) {
|
||||||
g_warning ("--linksdir is only valid with --format=links");
|
g_warning ("--linksdir is only valid with --format=links");
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
@ -858,7 +825,19 @@ print_attr_sexp (const char* elm, const char *str, gboolean nl)
|
||||||
g_free (esc);
|
g_free (esc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
output_sexp_date (time_t t)
|
||||||
|
{
|
||||||
|
unsigned date_high, date_low;
|
||||||
|
|
||||||
|
/* emacs likes it's date in a particular way... */
|
||||||
|
date_high = t >> 16;
|
||||||
|
date_low = t & 0xffff;
|
||||||
|
|
||||||
|
g_print (" :date %u\n", (unsigned)t);
|
||||||
|
g_print (" :date-high %u\n", date_high);
|
||||||
|
g_print (" :date-low %u\n", date_low);
|
||||||
|
}
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
output_sexp (MuMsgIter *iter, size_t *count)
|
output_sexp (MuMsgIter *iter, size_t *count)
|
||||||
|
@ -867,12 +846,9 @@ output_sexp (MuMsgIter *iter, size_t *count)
|
||||||
size_t mycount;
|
size_t mycount;
|
||||||
g_return_val_if_fail (iter, FALSE);
|
g_return_val_if_fail (iter, FALSE);
|
||||||
|
|
||||||
|
|
||||||
for (myiter = iter, mycount = 0; !mu_msg_iter_is_done (myiter);
|
for (myiter = iter, mycount = 0; !mu_msg_iter_is_done (myiter);
|
||||||
mu_msg_iter_next (myiter), ++mycount) {
|
mu_msg_iter_next (myiter), ++mycount) {
|
||||||
|
|
||||||
unsigned date, date_high, date_low;
|
|
||||||
|
|
||||||
MuMsg *msg;
|
MuMsg *msg;
|
||||||
if (!(msg = mu_msg_iter_get_msg (iter, NULL))) /* don't unref */
|
if (!(msg = mu_msg_iter_get_msg (iter, NULL))) /* don't unref */
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
@ -880,19 +856,12 @@ output_sexp (MuMsgIter *iter, size_t *count)
|
||||||
if (mycount != 0)
|
if (mycount != 0)
|
||||||
g_print ("\n");
|
g_print ("\n");
|
||||||
|
|
||||||
/* emacs likes it's date in a particular way... */
|
|
||||||
date = (unsigned) mu_msg_get_date (msg);
|
|
||||||
date_high = date >> 16;
|
|
||||||
date_low = date & 0xffff;
|
|
||||||
|
|
||||||
g_print ("(%u\n", (unsigned)mycount);
|
g_print ("(%u\n", (unsigned)mycount);
|
||||||
print_attr_sexp ("from", mu_msg_get_from (msg),TRUE);
|
print_attr_sexp ("from", mu_msg_get_from (msg),TRUE);
|
||||||
print_attr_sexp ("to", mu_msg_get_to (msg),TRUE);
|
print_attr_sexp ("to", mu_msg_get_to (msg),TRUE);
|
||||||
print_attr_sexp ("cc", mu_msg_get_cc (msg),TRUE);
|
print_attr_sexp ("cc", mu_msg_get_cc (msg),TRUE);
|
||||||
print_attr_sexp ("subject", mu_msg_get_subject (msg),TRUE);
|
print_attr_sexp ("subject", mu_msg_get_subject (msg),TRUE);
|
||||||
g_print (" :date %u\n", date);
|
output_sexp_date (mu_msg_get_date (msg));
|
||||||
g_print (" :date-high %u\n", date_high);
|
|
||||||
g_print (" :date-low %u\n", date_low);
|
|
||||||
g_print (" :size %u\n", (unsigned) mu_msg_get_size (msg));
|
g_print (" :size %u\n", (unsigned) mu_msg_get_size (msg));
|
||||||
print_attr_sexp ("msgid", mu_msg_get_msgid (msg),TRUE);
|
print_attr_sexp ("msgid", mu_msg_get_msgid (msg),TRUE);
|
||||||
print_attr_sexp ("path", mu_msg_get_path (msg),TRUE);
|
print_attr_sexp ("path", mu_msg_get_path (msg),TRUE);
|
||||||
|
@ -917,7 +886,6 @@ mu_cmd_find (MuConfig *opts)
|
||||||
gboolean rv;
|
gboolean rv;
|
||||||
gchar *query;
|
gchar *query;
|
||||||
size_t count = 0;
|
size_t count = 0;
|
||||||
OutputFormat format;
|
|
||||||
|
|
||||||
g_return_val_if_fail (opts, FALSE);
|
g_return_val_if_fail (opts, FALSE);
|
||||||
g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_FIND, FALSE);
|
g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_FIND, FALSE);
|
||||||
|
@ -925,19 +893,18 @@ mu_cmd_find (MuConfig *opts)
|
||||||
if (!query_params_valid (opts) || !format_params_valid(opts))
|
if (!query_params_valid (opts) || !format_params_valid(opts))
|
||||||
return MU_EXITCODE_ERROR;
|
return MU_EXITCODE_ERROR;
|
||||||
|
|
||||||
format = get_output_format (opts->formatstr);
|
|
||||||
xapian = get_query_obj();
|
xapian = get_query_obj();
|
||||||
query = get_query (opts);
|
query = get_query (opts);
|
||||||
|
|
||||||
if (!xapian ||!query)
|
if (!xapian ||!query)
|
||||||
return MU_EXITCODE_ERROR;
|
return MU_EXITCODE_ERROR;
|
||||||
|
|
||||||
if (format == FORMAT_XQUERY)
|
if (opts->format == MU_CONFIG_FORMAT_XQUERY)
|
||||||
rv = print_xapian_query (xapian, query, &count);
|
rv = print_xapian_query (xapian, query, &count);
|
||||||
else if (opts->exec)
|
else if (opts->exec)
|
||||||
rv = exec_cmd_on_query (xapian, query, opts, &count);
|
rv = exec_cmd_on_query (xapian, query, opts, &count);
|
||||||
else
|
else
|
||||||
rv = process_query (xapian, query, opts, format, &count);
|
rv = process_query (xapian, query, opts, &count);
|
||||||
|
|
||||||
mu_query_destroy (xapian);
|
mu_query_destroy (xapian);
|
||||||
g_free (query);
|
g_free (query);
|
||||||
|
|
28
src/mu-cmd.c
28
src/mu-cmd.c
|
@ -175,6 +175,29 @@ handle_msg (const char *fname, MuConfig *opts)
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static gboolean
|
||||||
|
view_params_valid (MuConfig *opts)
|
||||||
|
{
|
||||||
|
/* note: params[0] will be 'view' */
|
||||||
|
if (!opts->params[0] || !opts->params[1]) {
|
||||||
|
g_warning ("usage: mu view [options] <file> [<files>]");
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
switch (opts->format) {
|
||||||
|
case MU_CONFIG_FORMAT_PLAIN:
|
||||||
|
case MU_CONFIG_FORMAT_SEXP:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
g_warning ("invalid output format %s",
|
||||||
|
opts->formatstr ? opts->formatstr : "<none>");
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
MuExitCode
|
MuExitCode
|
||||||
mu_cmd_view (MuConfig *opts)
|
mu_cmd_view (MuConfig *opts)
|
||||||
|
@ -185,11 +208,8 @@ mu_cmd_view (MuConfig *opts)
|
||||||
g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_VIEW,
|
g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_VIEW,
|
||||||
MU_EXITCODE_ERROR);
|
MU_EXITCODE_ERROR);
|
||||||
|
|
||||||
/* note: params[0] will be 'view' */
|
if (!view_params_valid(opts))
|
||||||
if (!opts->params[0] || !opts->params[1]) {
|
|
||||||
g_warning ("usage: mu view [options] <file> [<files>]");
|
|
||||||
return MU_EXITCODE_ERROR;
|
return MU_EXITCODE_ERROR;
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 1; opts->params[i]; ++i) {
|
for (i = 1; opts->params[i]; ++i) {
|
||||||
|
|
||||||
|
|
|
@ -31,6 +31,41 @@
|
||||||
#include "mu-config.h"
|
#include "mu-config.h"
|
||||||
#include "mu-cmd.h"
|
#include "mu-cmd.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static MuConfigFormat
|
||||||
|
get_output_format (const char *formatstr)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
struct {
|
||||||
|
const char* name;
|
||||||
|
MuConfigFormat format;
|
||||||
|
} formats [] = {
|
||||||
|
{"mutt-alias", MU_CONFIG_FORMAT_MUTT_ALIAS},
|
||||||
|
{"mutt-ab", MU_CONFIG_FORMAT_MUTT_AB},
|
||||||
|
{"wl", MU_CONFIG_FORMAT_WL},
|
||||||
|
{"csv", MU_CONFIG_FORMAT_CSV},
|
||||||
|
{"org-contact", MU_CONFIG_FORMAT_ORG_CONTACT},
|
||||||
|
{"bbdb", MU_CONFIG_FORMAT_BBDB},
|
||||||
|
{"json", MU_CONFIG_FORMAT_JSON,},
|
||||||
|
{"links", MU_CONFIG_FORMAT_LINKS},
|
||||||
|
{"plain", MU_CONFIG_FORMAT_PLAIN},
|
||||||
|
{"sexp", MU_CONFIG_FORMAT_SEXP},
|
||||||
|
{"xml", MU_CONFIG_FORMAT_XML},
|
||||||
|
{"xquery", MU_CONFIG_FORMAT_XQUERY}
|
||||||
|
};
|
||||||
|
|
||||||
|
for (i = 0; i != G_N_ELEMENTS(formats); i++)
|
||||||
|
if (strcmp (formats[i].name, formatstr) == 0)
|
||||||
|
return formats[i].format;
|
||||||
|
|
||||||
|
return MU_CONFIG_FORMAT_UNKNOWN;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
set_group_mu_defaults (MuConfig *opts)
|
set_group_mu_defaults (MuConfig *opts)
|
||||||
{
|
{
|
||||||
|
@ -141,7 +176,10 @@ set_group_find_defaults (MuConfig *opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!opts->formatstr) /* by default, use plain output */
|
if (!opts->formatstr) /* by default, use plain output */
|
||||||
opts->formatstr = MU_CONFIG_FORMAT_PLAIN;
|
opts->format = MU_CONFIG_FORMAT_PLAIN;
|
||||||
|
else
|
||||||
|
opts->format =
|
||||||
|
get_output_format (opts->formatstr);
|
||||||
|
|
||||||
if (opts->linksdir) {
|
if (opts->linksdir) {
|
||||||
gchar *old = opts->linksdir;
|
gchar *old = opts->linksdir;
|
||||||
|
@ -215,7 +253,10 @@ static void
|
||||||
set_group_cfind_defaults (MuConfig *opts)
|
set_group_cfind_defaults (MuConfig *opts)
|
||||||
{
|
{
|
||||||
if (!opts->formatstr) /* by default, use plain output */
|
if (!opts->formatstr) /* by default, use plain output */
|
||||||
opts->formatstr = MU_CONFIG_FORMAT_PLAIN;
|
opts->format = MU_CONFIG_FORMAT_PLAIN;
|
||||||
|
else
|
||||||
|
opts->format = get_output_format (opts->formatstr);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -238,6 +279,15 @@ config_options_group_cfind (MuConfig *opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void
|
||||||
|
set_group_view_defaults (MuConfig *opts)
|
||||||
|
{
|
||||||
|
if (!opts->formatstr) /* by default, use plain output */
|
||||||
|
opts->format = MU_CONFIG_FORMAT_PLAIN;
|
||||||
|
else
|
||||||
|
opts->format = get_output_format (opts->formatstr);
|
||||||
|
}
|
||||||
|
|
||||||
static GOptionGroup *
|
static GOptionGroup *
|
||||||
config_options_group_view (MuConfig *opts)
|
config_options_group_view (MuConfig *opts)
|
||||||
{
|
{
|
||||||
|
@ -247,6 +297,8 @@ config_options_group_view (MuConfig *opts)
|
||||||
"only show a short summary of the message (false)", NULL},
|
"only show a short summary of the message (false)", NULL},
|
||||||
{"terminate", 0, 0, G_OPTION_ARG_NONE, &opts->terminator,
|
{"terminate", 0, 0, G_OPTION_ARG_NONE, &opts->terminator,
|
||||||
"terminate messages with ascii-0x07 (\\f, form-feed)", NULL},
|
"terminate messages with ascii-0x07 (\\f, form-feed)", NULL},
|
||||||
|
{"format", 'o', 0, G_OPTION_ARG_STRING, &opts->formatstr,
|
||||||
|
"output format ('plain'(*), 'sexp')", NULL},
|
||||||
{NULL, 0, 0, 0, NULL, NULL, NULL}
|
{NULL, 0, 0, 0, NULL, NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -410,6 +462,7 @@ mu_config_new (int *argcp, char ***argvp)
|
||||||
set_group_index_defaults (config);
|
set_group_index_defaults (config);
|
||||||
set_group_find_defaults (config);
|
set_group_find_defaults (config);
|
||||||
set_group_cfind_defaults (config);
|
set_group_cfind_defaults (config);
|
||||||
|
set_group_view_defaults (config);
|
||||||
/* set_group_mkdir_defaults (config); */
|
/* set_group_mkdir_defaults (config); */
|
||||||
|
|
||||||
return config;
|
return config;
|
||||||
|
|
|
@ -32,23 +32,36 @@ G_BEGIN_DECLS
|
||||||
/* env var; if non-empty, color are enabled for some commands */
|
/* env var; if non-empty, color are enabled for some commands */
|
||||||
#define MU_COLORS "MU_COLORS"
|
#define MU_COLORS "MU_COLORS"
|
||||||
|
|
||||||
/* output formats for 'mu find' */
|
|
||||||
#define MU_CONFIG_FORMAT_PLAIN "plain" /* plain text output */
|
|
||||||
#define MU_CONFIG_FORMAT_LINKS "links" /* output as symlinks */
|
|
||||||
#define MU_CONFIG_FORMAT_XML "xml" /* output xml */
|
|
||||||
#define MU_CONFIG_FORMAT_JSON "json" /* output json */
|
|
||||||
#define MU_CONFIG_FORMAT_SEXP "sexp" /* output sexps */
|
|
||||||
#define MU_CONFIG_FORMAT_XQUERY "xquery" /* output the xapian query */
|
|
||||||
|
|
||||||
/* output formats for 'mu cfind' */
|
enum _MuConfigFormat {
|
||||||
#define MU_CONFIG_FORMAT_MUTT_ALIAS "mutt-alias" /* mutt alias style */
|
MU_CONFIG_FORMAT_UNKNOWN = 0,
|
||||||
#define MU_CONFIG_FORMAT_MUTT_AB "mutt-ab" /* mutt ext abook */
|
|
||||||
#define MU_CONFIG_FORMAT_WL "wl" /* Wanderlust abook */
|
/* for cfind, find, view */
|
||||||
#define MU_CONFIG_FORMAT_CSV "csv" /* comma-sep'd values */
|
MU_CONFIG_FORMAT_PLAIN, /* plain output */
|
||||||
#define MU_CONFIG_FORMAT_ORG_CONTACT "org-contact" /* org-contact */
|
|
||||||
#define MU_CONFIG_FORMAT_BBDB "bbdb" /* BBDB */
|
/* for cfind */
|
||||||
|
MU_CONFIG_FORMAT_MUTT_ALIAS, /* mutt alias style */
|
||||||
|
MU_CONFIG_FORMAT_MUTT_AB, /* mutt ext abook */
|
||||||
|
MU_CONFIG_FORMAT_WL, /* Wanderlust abook */
|
||||||
|
MU_CONFIG_FORMAT_CSV, /* comma-sep'd values */
|
||||||
|
MU_CONFIG_FORMAT_ORG_CONTACT, /* org-contact */
|
||||||
|
MU_CONFIG_FORMAT_BBDB, /* BBDB */
|
||||||
|
|
||||||
|
/* for find, view */
|
||||||
|
MU_CONFIG_FORMAT_SEXP, /* output sexps */
|
||||||
|
|
||||||
|
/* for find */
|
||||||
|
MU_CONFIG_FORMAT_LINKS, /* output as symlinks */
|
||||||
|
MU_CONFIG_FORMAT_XML, /* output xml */
|
||||||
|
MU_CONFIG_FORMAT_JSON, /* output json */
|
||||||
|
MU_CONFIG_FORMAT_XQUERY, /* output the xapian query */
|
||||||
|
};
|
||||||
|
typedef enum _MuConfigFormat MuConfigFormat;
|
||||||
|
|
||||||
|
|
||||||
enum _MuConfigCmd {
|
enum _MuConfigCmd {
|
||||||
|
MU_CONFIG_CMD_UNKNOWN = 0,
|
||||||
|
|
||||||
MU_CONFIG_CMD_INDEX,
|
MU_CONFIG_CMD_INDEX,
|
||||||
MU_CONFIG_CMD_FIND,
|
MU_CONFIG_CMD_FIND,
|
||||||
MU_CONFIG_CMD_CLEANUP,
|
MU_CONFIG_CMD_CLEANUP,
|
||||||
|
@ -57,8 +70,6 @@ enum _MuConfigCmd {
|
||||||
MU_CONFIG_CMD_EXTRACT,
|
MU_CONFIG_CMD_EXTRACT,
|
||||||
MU_CONFIG_CMD_CFIND,
|
MU_CONFIG_CMD_CFIND,
|
||||||
MU_CONFIG_CMD_NONE,
|
MU_CONFIG_CMD_NONE,
|
||||||
|
|
||||||
MU_CONFIG_CMD_UNKNOWN
|
|
||||||
};
|
};
|
||||||
typedef enum _MuConfigCmd MuConfigCmd;
|
typedef enum _MuConfigCmd MuConfigCmd;
|
||||||
|
|
||||||
|
@ -101,8 +112,11 @@ struct _MuConfig {
|
||||||
gboolean threads; /* show message threads */
|
gboolean threads; /* show message threads */
|
||||||
gboolean summary; /* include a summary? */
|
gboolean summary; /* include a summary? */
|
||||||
char *bookmark; /* use bookmark */
|
char *bookmark; /* use bookmark */
|
||||||
char *formatstr; /* output type
|
char *formatstr; /* output type for find
|
||||||
* (plain,links,xml,json,sexp) */
|
* (plain,links,xml,json,sexp)
|
||||||
|
* and view (plain, sexp) and cfind
|
||||||
|
*/
|
||||||
|
MuConfigFormat format; /* the decoded formatstr */
|
||||||
char *exec; /* command to execute on the
|
char *exec; /* command to execute on the
|
||||||
* files for the matched
|
* files for the matched
|
||||||
* messages */
|
* messages */
|
||||||
|
|
Loading…
Reference in New Issue