* mu-config: move command handling to mu-cmd

This commit is contained in:
Dirk-Jan C. Binnema
2011-08-30 21:58:35 +03:00
parent 0861319091
commit c7d7965f0d
2 changed files with 43 additions and 103 deletions

View File

@ -83,10 +83,10 @@ set_group_mu_defaults (MuConfig *opts)
/* check for the MU_COLORS env var; but in any case don't use /* check for the MU_COLORS env var; but in any case don't use
* colors unless we're writing to a tty */ * colors unless we're writing to a tty */
if (g_getenv (MU_COLORS) != NULL) if (g_getenv (MU_COLORS) != NULL)
opts->color = TRUE; opts->color = TRUE;
if (!isatty(fileno(stdout))) if (!isatty(fileno(stdout)))
opts->color = FALSE; opts->color = FALSE;
@ -109,7 +109,7 @@ config_options_group_mu (MuConfig *opts)
"log to standard error (false)", NULL}, "log to standard error (false)", NULL},
{"color", 0, 0, G_OPTION_ARG_NONE, &opts->color, {"color", 0, 0, G_OPTION_ARG_NONE, &opts->color,
"use ANSI-colors in some output (false)", NULL}, "use ANSI-colors in some output (false)", NULL},
{G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY,
&opts->params, "parameters", NULL}, &opts->params, "parameters", NULL},
{NULL, 0, 0, 0, NULL, NULL, NULL} {NULL, 0, 0, 0, NULL, NULL, NULL}
@ -174,7 +174,7 @@ set_group_find_defaults (MuConfig *opts)
opts->descending = TRUE; opts->descending = TRUE;
} else } else
opts->descending = FALSE; opts->descending = FALSE;
} }
if (!opts->formatstr) /* by default, use plain output */ if (!opts->formatstr) /* by default, use plain output */
@ -182,7 +182,7 @@ set_group_find_defaults (MuConfig *opts)
else else
opts->format = opts->format =
get_output_format (opts->formatstr); get_output_format (opts->formatstr);
if (opts->linksdir) { if (opts->linksdir) {
gchar *old = opts->linksdir; gchar *old = opts->linksdir;
opts->linksdir = mu_util_dir_expand(opts->linksdir); opts->linksdir = mu_util_dir_expand(opts->linksdir);
@ -275,7 +275,7 @@ config_options_group_cfind (MuConfig *opts)
"'org-contact', 'csv')", NULL}, "'org-contact', 'csv')", NULL},
{NULL, 0, 0, 0, NULL, NULL, NULL} {NULL, 0, 0, 0, NULL, NULL, NULL}
}; };
og = g_option_group_new("cfind", "options for the 'cfind' command", og = g_option_group_new("cfind", "options for the 'cfind' command",
"", NULL, NULL); "", NULL, NULL);
g_option_group_add_entries(og, entries); g_option_group_add_entries(og, entries);
@ -306,7 +306,7 @@ config_options_group_view (MuConfig *opts)
"output format ('plain'(*), 'sexp')", NULL}, "output format ('plain'(*), 'sexp')", NULL},
{NULL, 0, 0, 0, NULL, NULL, NULL} {NULL, 0, 0, 0, NULL, NULL, NULL}
}; };
og = g_option_group_new("view", "options for the 'view' command", og = g_option_group_new("view", "options for the 'view' command",
"", NULL, NULL); "", NULL, NULL);
g_option_group_add_entries(og, entries); g_option_group_add_entries(og, entries);
@ -330,7 +330,7 @@ config_options_group_mv (MuConfig *opts)
NULL}, NULL},
{NULL, 0, 0, 0, NULL, NULL, NULL} {NULL, 0, 0, 0, NULL, NULL, NULL}
}; };
og = g_option_group_new ("mv", "options for the 'mv' command", og = g_option_group_new ("mv", "options for the 'mv' command",
"", NULL, NULL); "", NULL, NULL);
g_option_group_add_entries(og, entries); g_option_group_add_entries(og, entries);
@ -392,7 +392,7 @@ config_options_group_server (MuConfig * opts)
static gboolean static gboolean
parse_cmd (MuConfig *opts, int *argcp, char ***argvp) parse_cmd (MuConfig *opts, int *argcp, char ***argvp)
{ {
int i; int i;
@ -412,22 +412,22 @@ parse_cmd (MuConfig *opts, int *argcp, char ***argvp)
{ "remove", MU_CONFIG_CMD_REMOVE }, { "remove", MU_CONFIG_CMD_REMOVE },
{ "server", MU_CONFIG_CMD_SERVER } { "server", MU_CONFIG_CMD_SERVER }
}; };
opts->cmd = MU_CONFIG_CMD_NONE; opts->cmd = MU_CONFIG_CMD_NONE;
opts->cmdstr = NULL; opts->cmdstr = NULL;
if (*argcp < 2) /* no command found at all */ if (*argcp < 2) /* no command found at all */
return TRUE; return TRUE;
else if ((**argvp)[1] == '-') else if ((**argvp)[1] == '-')
/* if the first param starts with '-', there is no /* if the first param starts with '-', there is no
* command, just some option (like --version, --help * command, just some option (like --version, --help
* etc.)*/ * etc.)*/
return TRUE; return TRUE;
opts->cmd = MU_CONFIG_CMD_UNKNOWN; opts->cmd = MU_CONFIG_CMD_UNKNOWN;
opts->cmdstr = (*argvp)[1]; opts->cmdstr = (*argvp)[1];
for (i = 0; i != G_N_ELEMENTS(cmd_map); ++i) for (i = 0; i != G_N_ELEMENTS(cmd_map); ++i)
if (strcmp (opts->cmdstr, cmd_map[i]._name) == 0) if (strcmp (opts->cmdstr, cmd_map[i]._name) == 0)
opts->cmd = cmd_map[i]._cmd; opts->cmd = cmd_map[i]._cmd;
@ -439,9 +439,9 @@ static void
add_context_group (GOptionContext *context, MuConfig *opts) add_context_group (GOptionContext *context, MuConfig *opts)
{ {
GOptionGroup *group; GOptionGroup *group;
group = NULL; group = NULL;
switch (opts->cmd) { switch (opts->cmd) {
case MU_CONFIG_CMD_INDEX: case MU_CONFIG_CMD_INDEX:
group = config_options_group_index (opts); break; group = config_options_group_index (opts); break;
@ -459,7 +459,7 @@ add_context_group (GOptionContext *context, MuConfig *opts)
group = config_options_group_view (opts); break; group = config_options_group_view (opts); break;
case MU_CONFIG_CMD_SERVER: case MU_CONFIG_CMD_SERVER:
group = config_options_group_server (opts); break; group = config_options_group_server (opts); break;
default: break; default: break;
} }
@ -474,13 +474,13 @@ parse_params (MuConfig *opts, int *argcp, char ***argvp)
GError *err = NULL; GError *err = NULL;
GOptionContext *context; GOptionContext *context;
gboolean rv; gboolean rv;
context = g_option_context_new("- mu general option"); context = g_option_context_new("- mu general option");
g_option_context_set_main_group(context, g_option_context_set_main_group(context,
config_options_group_mu(opts)); config_options_group_mu(opts));
add_context_group (context, opts); add_context_group (context, opts);
rv = g_option_context_parse (context, argcp, argvp, &err); rv = g_option_context_parse (context, argcp, argvp, &err);
g_option_context_free (context); g_option_context_free (context);
if (!rv) { if (!rv) {
@ -490,7 +490,7 @@ parse_params (MuConfig *opts, int *argcp, char ***argvp)
} }
return TRUE; return TRUE;
} }
MuConfig* MuConfig*
mu_config_new (int *argcp, char ***argvp) mu_config_new (int *argcp, char ***argvp)
@ -498,15 +498,15 @@ mu_config_new (int *argcp, char ***argvp)
MuConfig *config; MuConfig *config;
g_return_val_if_fail (argcp && argvp, NULL); g_return_val_if_fail (argcp && argvp, NULL);
config = g_new0 (MuConfig, 1); config = g_new0 (MuConfig, 1);
if (!parse_cmd (config, argcp, argvp) || if (!parse_cmd (config, argcp, argvp) ||
!parse_params(config, argcp, argvp)) { !parse_params(config, argcp, argvp)) {
mu_config_destroy (config); mu_config_destroy (config);
return NULL; return NULL;
} }
/* fill in the defaults if user did not specify */ /* fill in the defaults if user did not specify */
set_group_mu_defaults (config); set_group_mu_defaults (config);
set_group_index_defaults (config); set_group_index_defaults (config);
@ -514,7 +514,7 @@ mu_config_new (int *argcp, char ***argvp)
set_group_cfind_defaults (config); set_group_cfind_defaults (config);
set_group_view_defaults (config); set_group_view_defaults (config);
/* set_group_mkdir_defaults (config); */ /* set_group_mkdir_defaults (config); */
return config; return config;
} }
@ -523,7 +523,7 @@ mu_config_destroy (MuConfig *opts)
{ {
if (!opts) if (!opts)
return; return;
g_free (opts->muhome); g_free (opts->muhome);
g_free (opts->maildir); g_free (opts->maildir);
g_free (opts->linksdir); g_free (opts->linksdir);
@ -533,73 +533,13 @@ mu_config_destroy (MuConfig *opts)
} }
static void
show_usage (gboolean noerror)
{
const char* usage=
"usage: mu command [options] [parameters]\n"
"where command is one of index, find, cfind, view, mkdir, cleanup, "
"extract, mv, add, remove or server\n\n"
"see the mu, mu-<command> or mu-easy manpages for "
"more information\n";
if (noerror)
g_print ("%s", usage);
else
g_printerr ("%s", usage);
}
static void
show_version (void)
{
g_print ("mu (mail indexer/searcher) version " VERSION "\n"
"Copyright (C) 2008-2011 Dirk-Jan C. Binnema (GPLv3+)\n");
}
MuError
mu_config_execute (MuConfig *opts)
{
g_return_val_if_fail (opts, MU_ERROR_INTERNAL);
if (opts->version) {
show_version ();
return MU_OK;
}
if (!opts->params||!opts->params[0]) {/* no command? */
show_version ();
show_usage (TRUE);
return MU_ERROR_IN_PARAMETERS;
}
switch (opts->cmd) {
case MU_CONFIG_CMD_CFIND: return mu_cmd_cfind (opts);
case MU_CONFIG_CMD_CLEANUP: return mu_cmd_cleanup (opts);
case MU_CONFIG_CMD_EXTRACT: return mu_cmd_extract (opts);
case MU_CONFIG_CMD_FIND: return mu_cmd_find (opts);
case MU_CONFIG_CMD_INDEX: return mu_cmd_index (opts);
case MU_CONFIG_CMD_MKDIR: return mu_cmd_mkdir (opts);
case MU_CONFIG_CMD_MV: return mu_cmd_mv (opts);
case MU_CONFIG_CMD_VIEW: return mu_cmd_view (opts);
case MU_CONFIG_CMD_ADD: return mu_cmd_add (opts);
case MU_CONFIG_CMD_REMOVE: return mu_cmd_remove (opts);
case MU_CONFIG_CMD_SERVER: return mu_cmd_server (opts);
case MU_CONFIG_CMD_UNKNOWN:
show_usage (FALSE);
return MU_ERROR_IN_PARAMETERS;
default:
g_return_val_if_reached (MU_ERROR_INTERNAL);
}
}
guint guint
mu_config_param_num (MuConfig *conf) mu_config_param_num (MuConfig *conf)
{ {
guint u; guint u;
g_return_val_if_fail (conf, 0); g_return_val_if_fail (conf, 0);
for (u = 0; conf->params[u]; ++u); for (u = 0; conf->params[u]; ++u);
return u; return u;

View File

@ -38,7 +38,7 @@ enum _MuConfigFormat {
/* for cfind, find, view */ /* for cfind, find, view */
MU_CONFIG_FORMAT_PLAIN, /* plain output */ MU_CONFIG_FORMAT_PLAIN, /* plain output */
/* for cfind */ /* for cfind */
MU_CONFIG_FORMAT_MUTT_ALIAS, /* mutt alias style */ MU_CONFIG_FORMAT_MUTT_ALIAS, /* mutt alias style */
MU_CONFIG_FORMAT_MUTT_AB, /* mutt ext abook */ MU_CONFIG_FORMAT_MUTT_AB, /* mutt ext abook */
@ -60,7 +60,7 @@ typedef enum _MuConfigFormat MuConfigFormat;
enum _MuConfigCmd { enum _MuConfigCmd {
MU_CONFIG_CMD_UNKNOWN = 0, 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,
@ -87,7 +87,7 @@ struct _MuConfig {
* MU_CONFIG_CMD_NONE */ * MU_CONFIG_CMD_NONE */
const char *cmdstr; /* cmd string, for user const char *cmdstr; /* cmd string, for user
* info */ * info */
/* general options */ /* general options */
gboolean quiet; /* don't give any output */ gboolean quiet; /* don't give any output */
gboolean debug; /* spew out debug info */ gboolean debug; /* spew out debug info */
@ -96,7 +96,7 @@ struct _MuConfig {
gboolean log_stderr; /* log to stderr (not logfile) */ gboolean log_stderr; /* log to stderr (not logfile) */
gchar** params; /* parameters (for querying) */ gchar** params; /* parameters (for querying) */
gboolean color; /* use ansi-colors in some output */ gboolean color; /* use ansi-colors in some output */
/* options for indexing */ /* options for indexing */
char *maildir; /* where the mails are */ char *maildir; /* where the mails are */
gboolean nocleanup; /* don't cleanup del'd mails from db */ gboolean nocleanup; /* don't cleanup del'd mails from db */
@ -108,9 +108,9 @@ struct _MuConfig {
* commits, or 0 for * commits, or 0 for
* default */ * default */
int max_msg_size; /* maximum size for message files */ int max_msg_size; /* maximum size for message files */
/* options for querying 'find' (and view-> 'summary') */ /* options for querying 'find' (and view-> 'summary') */
char *fields; /* fields to show in output */ char *fields; /* fields to show in output */
char *sortfield; /* field to sort by (string) */ char *sortfield; /* field to sort by (string) */
gboolean descending; /* sort descending (z->a)? */ gboolean descending; /* sort descending (z->a)? */
gboolean threads; /* show message threads */ gboolean threads; /* show message threads */
@ -138,7 +138,7 @@ struct _MuConfig {
gboolean terminator; /* add separator \f between gboolean terminator; /* add separator \f between
* multiple messages in mu * multiple messages in mu
* view */ * view */
/* output to a maildir with symlinks */ /* output to a maildir with symlinks */
char *linksdir; /* maildir to output symlinks */ char *linksdir; /* maildir to output symlinks */
gboolean clearlinks; /* clear a linksdir before filling */ gboolean clearlinks; /* clear a linksdir before filling */
@ -152,34 +152,34 @@ struct _MuConfig {
char *targetdir; /* where to save the attachments */ char *targetdir; /* where to save the attachments */
gboolean overwrite; /* should we overwrite same-named files */ gboolean overwrite; /* should we overwrite same-named files */
gboolean play; /* after saving, try to 'play' gboolean play; /* after saving, try to 'play'
* (open) the attmnt using xdgopen */ * (open) the attmnt using xdgopen */
}; };
typedef struct _MuConfig MuConfig; typedef struct _MuConfig MuConfig;
/** /**
* create a new mu config object * create a new mu config object
* *
* set default values for the configuration options; when you call * set default values for the configuration options; when you call
* mu_config_init, you should also call mu_config_uninit when the data * mu_config_init, you should also call mu_config_uninit when the data
* is no longer needed. * is no longer needed.
* *
* @param opts options * @param opts options
*/ */
MuConfig *mu_config_new (int *argcp, char ***argvp) MuConfig *mu_config_new (int *argcp, char ***argvp)
G_GNUC_WARN_UNUSED_RESULT; G_GNUC_WARN_UNUSED_RESULT;
/** /**
* free the MuOptionsConfig structure; the the muhome and maildir * free the MuOptionsConfig structure; the the muhome and maildir
* members are heap-allocated, so must be freed. * members are heap-allocated, so must be freed.
* *
* @param opts a MuConfig struct, or NULL * @param opts a MuConfig struct, or NULL
*/ */
void mu_config_destroy (MuConfig *opts); void mu_config_destroy (MuConfig *opts);
/** /**
* execute the command / options in this config * execute the command / options in this config
* *
* @param opts the commands/options * @param opts the commands/options
* *
* @return a value denoting the success/failure of the execution; * @return a value denoting the success/failure of the execution;
* MU_ERROR_NONE (0) for success, non-zero for a failure. This is to used for * MU_ERROR_NONE (0) for success, non-zero for a failure. This is to used for
* the exit code of the process * the exit code of the process
@ -189,9 +189,9 @@ MuError mu_config_execute (MuConfig *opts);
/** /**
* count the number of non-option parameters * count the number of non-option parameters
* *
* @param conf a MuConfig instance * @param conf a MuConfig instance
* *
* @return the number of non-option parameters, or 0 in case of error * @return the number of non-option parameters, or 0 in case of error
*/ */
guint mu_config_param_num (MuConfig *conf); guint mu_config_param_num (MuConfig *conf);