diff --git a/src/mu-cmd-cfind.c b/src/mu-cmd-cfind.c index 6674438a..1218a102 100644 --- a/src/mu-cmd-cfind.c +++ b/src/mu-cmd-cfind.c @@ -31,52 +31,15 @@ #include "mu-contacts.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 -print_header (OutputFormat format) +print_header (MuConfigFormat format) { switch (format) { - case FORMAT_BBDB: + case MU_CONFIG_FORMAT_BBDB: g_print (";; -*-coding: utf-8-emacs;-*-\n" ";;; file-version: 6\n"); break; - case FORMAT_MUTT_AB: + case MU_CONFIG_FORMAT_MUTT_AB: g_print ("Matching addresses in the mu database:\n"); break; default: @@ -84,8 +47,6 @@ print_header (OutputFormat format) } } - - static void 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 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); + mu_util_print_encoded ("alias %s %s <%s>\n", + nick, name, email); + g_free (nick); - nick = mu_str_guess_nick (name); - mu_util_print_encoded ("alias %s %s <%s>\n", - nick, name, email); - - g_free (nick); - } } static void each_contact_wl (const char *email, const char *name) { - if (name) { - gchar *nick; - nick = mu_str_guess_nick (name); - mu_util_print_encoded ("%s \"%s\" \"%s\"\n", - email, nick, name); - g_free (nick); - } + gchar *nick; + + if (!name) + return; + + nick = mu_str_guess_nick (name); + mu_util_print_encoded ("%s \"%s\" \"%s\"\n", + email, nick, name); + g_free (nick); } @@ -153,14 +117,19 @@ print_plain (const char *email, const char *name, gboolean color) fputs (" ", stdout); } - if (color) fputs (MU_COLOR_GREEN, stdout); + if (color) + fputs (MU_COLOR_GREEN, stdout); + mu_util_fputs_encoded (email, stdout); - if (color) fputs (MU_COLOR_DEFAULT, stdout); + + if (color) + fputs (MU_COLOR_DEFAULT, stdout); + fputs ("\n", stdout); } struct _ECData { - OutputFormat format; + MuConfigFormat format; gboolean color; }; typedef struct _ECData ECData; @@ -171,14 +140,23 @@ each_contact (const char *email, const char *name, time_t tstamp, ECData *ecdata) { switch (ecdata->format) { - case FORMAT_MUTT_ALIAS: each_contact_mutt_alias (email, name); break; - case FORMAT_MUTT_AB: + case MU_CONFIG_FORMAT_MUTT_ALIAS: + each_contact_mutt_alias (email, name); + break; + case MU_CONFIG_FORMAT_MUTT_AB: mu_util_print_encoded ("%s\t%s\t\n", - email, name ? name : ""); break; - case FORMAT_WL: each_contact_wl (email, name); break; - case FORMAT_ORG_CONTACT: each_contact_org_contact (email, name); break; - case FORMAT_BBDB: each_contact_bbdb (email, name, tstamp); break; - case FORMAT_CSV: + email, name ? name : ""); + break; + case MU_CONFIG_FORMAT_WL: + each_contact_wl (email, name); + 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); break; default: @@ -188,7 +166,7 @@ each_contact (const char *email, const char *name, time_t tstamp, static MuExitCode -run_cmd_cfind (const char* pattern, OutputFormat format, +run_cmd_cfind (const char* pattern, MuConfigFormat format, gboolean color) { gboolean rv; @@ -219,17 +197,19 @@ run_cmd_cfind (const char* pattern, OutputFormat format, } -MuExitCode -mu_cmd_cfind (MuConfig *opts) +static gboolean +cfind_params_valid (MuConfig *opts) { - OutputFormat format; - - g_return_val_if_fail (opts, MU_EXITCODE_ERROR); - g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_CFIND, - MU_EXITCODE_ERROR); - - format = get_output_format (opts->formatstr); - if (format == FORMAT_NONE) { + switch (opts->format) { + case MU_CONFIG_FORMAT_PLAIN: + case MU_CONFIG_FORMAT_MUTT_ALIAS: + case MU_CONFIG_FORMAT_MUTT_AB: + case MU_CONFIG_FORMAT_WL: + case MU_CONFIG_FORMAT_BBDB: + case MU_CONFIG_FORMAT_CSV: + case MU_CONFIG_FORMAT_ORG_CONTACT: + break; + default: g_warning ("invalid output format %s", opts->formatstr ? opts->formatstr : ""); return FALSE; @@ -238,9 +218,23 @@ mu_cmd_cfind (MuConfig *opts) /* only one pattern allowed */ if (opts->params[1] && opts->params[2]) { g_warning ("usage: mu cfind [options] []"); - 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); } diff --git a/src/mu-cmd-find.c b/src/mu-cmd-find.c index 1b9d67f5..f93ce5f2 100644 --- a/src/mu-cmd-find.c +++ b/src/mu-cmd-find.c @@ -45,17 +45,6 @@ #include "mu-cmd.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, gboolean clearlinks, size_t *count); @@ -66,30 +55,6 @@ static gboolean output_plain (MuMsgIter *iter, const char *fields, gboolean summary,gboolean threads, 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 upgrade_warning (void) { @@ -142,22 +107,20 @@ sort_field_from_string (const char* fieldstr) static gboolean -output_query_results (MuMsgIter *iter, MuConfig *opts, - OutputFormat format, size_t *count) +output_query_results (MuMsgIter *iter, MuConfig *opts, size_t *count) { - switch (format) { - - case FORMAT_LINKS: + switch (opts->format) { + case MU_CONFIG_FORMAT_LINKS: return output_links (iter, opts->linksdir, opts->clearlinks, count); - case FORMAT_PLAIN: + case MU_CONFIG_FORMAT_PLAIN: return output_plain (iter, opts->fields, opts->summary, opts->threads, opts->color, count); - case FORMAT_XML: + case MU_CONFIG_FORMAT_XML: return output_xml (iter, count); - case FORMAT_JSON: + case MU_CONFIG_FORMAT_JSON: return output_json (iter, count); - case FORMAT_SEXP: + case MU_CONFIG_FORMAT_SEXP: return output_sexp (iter, count); default: g_assert_not_reached (); @@ -196,7 +159,7 @@ run_query (MuQuery *xapian, const gchar *query, MuConfig *opts, size_t *count) static gboolean process_query (MuQuery *xapian, const gchar *query, MuConfig *opts, - OutputFormat format, size_t *count) + size_t *count) { MuMsgIter *iter; gboolean rv; @@ -205,7 +168,7 @@ process_query (MuQuery *xapian, const gchar *query, MuConfig *opts, if (!iter) return FALSE; - rv = output_query_results (iter, opts, format, count); + rv = output_query_results (iter, opts, count); if (rv && count && *count == 0) g_warning ("no matching messages found"); @@ -277,25 +240,29 @@ exec_cmd_on_query (MuQuery *xapian, const gchar *query, MuConfig *opts, - static gboolean format_params_valid (MuConfig *opts) { - OutputFormat format; - - format = get_output_format (opts->formatstr); - if (format == FORMAT_NONE) { + switch (opts->format) { + case MU_CONFIG_FORMAT_PLAIN: + case MU_CONFIG_FORMAT_SEXP: + 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", opts->formatstr ? opts->formatstr : ""); return FALSE; } - if (format == FORMAT_LINKS && !opts->linksdir) { + if (opts->format == MU_CONFIG_FORMAT_LINKS && !opts->linksdir) { g_warning ("missing --linksdir argument"); 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"); return FALSE; } @@ -858,7 +825,19 @@ print_attr_sexp (const char* elm, const char *str, gboolean nl) 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 output_sexp (MuMsgIter *iter, size_t *count) @@ -866,12 +845,9 @@ output_sexp (MuMsgIter *iter, size_t *count) MuMsgIter *myiter; size_t mycount; g_return_val_if_fail (iter, FALSE); - for (myiter = iter, mycount = 0; !mu_msg_iter_is_done (myiter); mu_msg_iter_next (myiter), ++mycount) { - - unsigned date, date_high, date_low; MuMsg *msg; if (!(msg = mu_msg_iter_get_msg (iter, NULL))) /* don't unref */ @@ -879,20 +855,13 @@ output_sexp (MuMsgIter *iter, size_t *count) if (mycount != 0) 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); print_attr_sexp ("from", mu_msg_get_from (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 ("subject", mu_msg_get_subject (msg),TRUE); - g_print (" :date %u\n", date); - g_print (" :date-high %u\n", date_high); - g_print (" :date-low %u\n", date_low); + output_sexp_date (mu_msg_get_date (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 ("path", mu_msg_get_path (msg),TRUE); @@ -917,7 +886,6 @@ mu_cmd_find (MuConfig *opts) gboolean rv; gchar *query; size_t count = 0; - OutputFormat format; g_return_val_if_fail (opts, 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)) return MU_EXITCODE_ERROR; - format = get_output_format (opts->formatstr); xapian = get_query_obj(); query = get_query (opts); if (!xapian ||!query) return MU_EXITCODE_ERROR; - if (format == FORMAT_XQUERY) + if (opts->format == MU_CONFIG_FORMAT_XQUERY) rv = print_xapian_query (xapian, query, &count); else if (opts->exec) rv = exec_cmd_on_query (xapian, query, opts, &count); else - rv = process_query (xapian, query, opts, format, &count); + rv = process_query (xapian, query, opts, &count); mu_query_destroy (xapian); g_free (query); diff --git a/src/mu-cmd.c b/src/mu-cmd.c index b963b319..7190cf77 100644 --- a/src/mu-cmd.c +++ b/src/mu-cmd.c @@ -164,7 +164,7 @@ handle_msg (const char *fname, MuConfig *opts) g_error_free (err); return MU_EXITCODE_ERROR; } - + if (view_msg (msg, NULL, opts->summary, opts->color)) rv = MU_EXITCODE_OK; else @@ -175,6 +175,29 @@ handle_msg (const char *fname, MuConfig *opts) 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] []"); + 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 : ""); + return FALSE; + } + + return TRUE; +} + MuExitCode mu_cmd_view (MuConfig *opts) @@ -184,12 +207,9 @@ mu_cmd_view (MuConfig *opts) g_return_val_if_fail (opts, MU_EXITCODE_ERROR); g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_VIEW, MU_EXITCODE_ERROR); - - /* note: params[0] will be 'view' */ - if (!opts->params[0] || !opts->params[1]) { - g_warning ("usage: mu view [options] []"); + + if (!view_params_valid(opts)) return MU_EXITCODE_ERROR; - } for (i = 1; opts->params[i]; ++i) { diff --git a/src/mu-config.c b/src/mu-config.c index bceab6bf..c512927c 100644 --- a/src/mu-config.c +++ b/src/mu-config.c @@ -31,6 +31,41 @@ #include "mu-config.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 set_group_mu_defaults (MuConfig *opts) { @@ -141,7 +176,10 @@ set_group_find_defaults (MuConfig *opts) } 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) { gchar *old = opts->linksdir; @@ -215,7 +253,10 @@ static void set_group_cfind_defaults (MuConfig *opts) { 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 * 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}, {"terminate", 0, 0, G_OPTION_ARG_NONE, &opts->terminator, "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} }; @@ -410,6 +462,7 @@ mu_config_new (int *argcp, char ***argvp) set_group_index_defaults (config); set_group_find_defaults (config); set_group_cfind_defaults (config); + set_group_view_defaults (config); /* set_group_mkdir_defaults (config); */ return config; diff --git a/src/mu-config.h b/src/mu-config.h index ff05fefe..063f4ee3 100644 --- a/src/mu-config.h +++ b/src/mu-config.h @@ -32,23 +32,36 @@ G_BEGIN_DECLS /* env var; if non-empty, color are enabled for some commands */ #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' */ -#define MU_CONFIG_FORMAT_MUTT_ALIAS "mutt-alias" /* mutt alias style */ -#define MU_CONFIG_FORMAT_MUTT_AB "mutt-ab" /* mutt ext abook */ -#define MU_CONFIG_FORMAT_WL "wl" /* Wanderlust abook */ -#define MU_CONFIG_FORMAT_CSV "csv" /* comma-sep'd values */ -#define MU_CONFIG_FORMAT_ORG_CONTACT "org-contact" /* org-contact */ -#define MU_CONFIG_FORMAT_BBDB "bbdb" /* BBDB */ +enum _MuConfigFormat { + MU_CONFIG_FORMAT_UNKNOWN = 0, + + /* for cfind, find, view */ + MU_CONFIG_FORMAT_PLAIN, /* plain output */ + + /* 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 { + MU_CONFIG_CMD_UNKNOWN = 0, + MU_CONFIG_CMD_INDEX, MU_CONFIG_CMD_FIND, MU_CONFIG_CMD_CLEANUP, @@ -57,8 +70,6 @@ enum _MuConfigCmd { MU_CONFIG_CMD_EXTRACT, MU_CONFIG_CMD_CFIND, MU_CONFIG_CMD_NONE, - - MU_CONFIG_CMD_UNKNOWN }; typedef enum _MuConfigCmd MuConfigCmd; @@ -101,8 +112,11 @@ struct _MuConfig { gboolean threads; /* show message threads */ gboolean summary; /* include a summary? */ char *bookmark; /* use bookmark */ - char *formatstr; /* output type - * (plain,links,xml,json,sexp) */ + char *formatstr; /* output type for find + * (plain,links,xml,json,sexp) + * and view (plain, sexp) and cfind + */ + MuConfigFormat format; /* the decoded formatstr */ char *exec; /* command to execute on the * files for the matched * messages */