mirror of https://github.com/djcb/mu.git
* mu-config.c: cosmetics
This commit is contained in:
parent
373973d813
commit
b7e3786af7
466
src/mu-config.c
466
src/mu-config.c
|
@ -22,282 +22,250 @@
|
||||||
#endif /*HAVE_CONFIG_H*/
|
#endif /*HAVE_CONFIG_H*/
|
||||||
|
|
||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
#include <string.h> /* memset */
|
#include <string.h> /* memset */
|
||||||
|
|
||||||
#include "mu-util.h"
|
#include "mu-util.h"
|
||||||
#include "mu-config.h"
|
#include "mu-config.h"
|
||||||
|
|
||||||
static void
|
static void set_group_mu_defaults(MuConfigOptions * opts)
|
||||||
set_group_mu_defaults (MuConfigOptions *opts)
|
|
||||||
{
|
{
|
||||||
gchar *exp;
|
gchar *exp;
|
||||||
|
|
||||||
if (!opts->muhome)
|
|
||||||
opts->muhome = mu_util_guess_mu_homedir ();
|
|
||||||
|
|
||||||
exp = mu_util_dir_expand (opts->muhome);
|
if (!opts->muhome)
|
||||||
if (exp) {
|
opts->muhome = mu_util_guess_mu_homedir();
|
||||||
g_free (opts->muhome);
|
|
||||||
opts->muhome = exp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
exp = mu_util_dir_expand(opts->muhome);
|
||||||
static GOptionGroup*
|
if (exp) {
|
||||||
config_options_group_mu (MuConfigOptions *opts)
|
g_free(opts->muhome);
|
||||||
{
|
opts->muhome = exp;
|
||||||
GOptionGroup *og;
|
|
||||||
GOptionEntry entries[] = {
|
|
||||||
{ "debug", 'd', 0, G_OPTION_ARG_NONE, &opts->debug,
|
|
||||||
"print debug output to standard error", NULL },
|
|
||||||
{ "quiet", 'q', 0, G_OPTION_ARG_NONE, &opts->quiet,
|
|
||||||
"don't give any progress information", NULL },
|
|
||||||
{"version", 'v', 0, G_OPTION_ARG_NONE, &opts->version,
|
|
||||||
"display version and copyright information", NULL},
|
|
||||||
{"muhome", 0, 0, G_OPTION_ARG_FILENAME, &opts->muhome,
|
|
||||||
"specify an alternative mu directory", NULL},
|
|
||||||
{"log-stderr", 0, 0, G_OPTION_ARG_NONE, &opts->log_stderr,
|
|
||||||
"log to standard error", NULL},
|
|
||||||
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY,
|
|
||||||
&opts->params, "parameters", NULL },
|
|
||||||
{ NULL, 0, 0, 0, NULL, NULL, NULL }
|
|
||||||
};
|
|
||||||
|
|
||||||
og = g_option_group_new ("mu", "general mu options",
|
|
||||||
"", NULL, NULL);
|
|
||||||
g_option_group_add_entries (og, entries);
|
|
||||||
|
|
||||||
return og;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
|
||||||
set_group_index_defaults (MuConfigOptions *opts)
|
|
||||||
{
|
|
||||||
gchar *exp;
|
|
||||||
|
|
||||||
if (!opts->maildir)
|
|
||||||
opts->maildir = mu_util_guess_maildir();
|
|
||||||
|
|
||||||
exp = mu_util_dir_expand (opts->maildir);
|
|
||||||
if (exp) {
|
|
||||||
g_free (opts->maildir);
|
|
||||||
opts->maildir = exp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static GOptionGroup*
|
|
||||||
config_options_group_index (MuConfigOptions *opts)
|
|
||||||
{
|
|
||||||
GOptionGroup *og;
|
|
||||||
GOptionEntry entries[] = {
|
|
||||||
{"maildir", 'm', 0, G_OPTION_ARG_FILENAME, &opts->maildir,
|
|
||||||
"top of the maildir", NULL},
|
|
||||||
{"reindex", 0, 0, G_OPTION_ARG_NONE, &opts->reindex,
|
|
||||||
"index even already indexed messages", NULL},
|
|
||||||
{"rebuild", 0, 0, G_OPTION_ARG_NONE, &opts->rebuild,
|
|
||||||
"rebuild the database from scratch", NULL},
|
|
||||||
{"autoupgrade", 0, 0, G_OPTION_ARG_NONE, &opts->autoupgrade,
|
|
||||||
"automatically upgrade the database with new mu versions", NULL},
|
|
||||||
{"nocleanup", 0, 0, G_OPTION_ARG_NONE, &opts->nocleanup,
|
|
||||||
"don't clean up the database after indexing", NULL},
|
|
||||||
{ NULL, 0, 0, 0, NULL, NULL, NULL }
|
|
||||||
};
|
|
||||||
|
|
||||||
og = g_option_group_new ("index",
|
|
||||||
"options for the 'index' command",
|
|
||||||
"", NULL, NULL);
|
|
||||||
g_option_group_add_entries (og, entries);
|
|
||||||
|
|
||||||
return og;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
|
||||||
set_group_find_defaults (MuConfigOptions *opts)
|
|
||||||
{
|
|
||||||
/* note, when no fields are specified, we use
|
|
||||||
* date-from-subject, and sort descending by date. If fields
|
|
||||||
* *are* specified, we sort in ascending order. */
|
|
||||||
if (!opts->fields) {
|
|
||||||
opts->fields = "d f s";
|
|
||||||
if (!opts->sortfield) {
|
|
||||||
opts->sortfield = "d";
|
|
||||||
opts->descending = TRUE;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (opts->linksdir) {
|
|
||||||
gchar *old = opts->linksdir;
|
|
||||||
opts->linksdir = mu_util_dir_expand (opts->linksdir);
|
|
||||||
if (!opts->linksdir) /* we'll check the dir later */
|
|
||||||
opts->linksdir = old;
|
|
||||||
else
|
|
||||||
g_free(old);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* FIXME: some warning when summary_len < 0? */
|
|
||||||
if (opts->summary_len < 1)
|
|
||||||
opts->summary_len = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static GOptionGroup *config_options_group_mu(MuConfigOptions * opts)
|
||||||
static GOptionGroup*
|
|
||||||
config_options_group_find (MuConfigOptions *opts)
|
|
||||||
{
|
{
|
||||||
GOptionGroup *og;
|
GOptionGroup *og;
|
||||||
GOptionEntry entries[] = {
|
GOptionEntry entries[] = {
|
||||||
{"xquery", 0, 0, G_OPTION_ARG_NONE, &opts->xquery,
|
{"debug", 'd', 0, G_OPTION_ARG_NONE, &opts->debug,
|
||||||
"print the Xapian query (for debugging)", NULL},
|
"print debug output to standard error", NULL},
|
||||||
{"fields", 'f', 0, G_OPTION_ARG_STRING, &opts->fields,
|
{"quiet", 'q', 0, G_OPTION_ARG_NONE, &opts->quiet,
|
||||||
"fields to display in the output", NULL},
|
"don't give any progress information", NULL},
|
||||||
{"sortfield", 's', 0, G_OPTION_ARG_STRING, &opts->sortfield,
|
{"version", 'v', 0, G_OPTION_ARG_NONE, &opts->version,
|
||||||
"field to sort on", NULL},
|
"display version and copyright information", NULL},
|
||||||
{"bookmark", 'b', 0, G_OPTION_ARG_STRING, &opts->bookmark,
|
{"muhome", 0, 0, G_OPTION_ARG_FILENAME, &opts->muhome,
|
||||||
"use a bookmarked query", NULL},
|
"specify an alternative mu directory", NULL},
|
||||||
{"descending", 'z', 0, G_OPTION_ARG_NONE, &opts->descending,
|
{"log-stderr", 0, 0, G_OPTION_ARG_NONE, &opts->log_stderr,
|
||||||
"sort in descending order (z -> a)", NULL},
|
"log to standard error", NULL},
|
||||||
{"summary-len", 'k', 0, G_OPTION_ARG_INT, &opts->summary_len,
|
{G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY,
|
||||||
"max number of lines for summary", NULL},
|
&opts->params, "parameters", NULL},
|
||||||
{"linksdir", 0, 0, G_OPTION_ARG_STRING, &opts->linksdir,
|
{NULL, 0, 0, 0, NULL, NULL, NULL}
|
||||||
"output as symbolic links to a target maildir", NULL },
|
};
|
||||||
{"clearlinks", 0, 0, G_OPTION_ARG_NONE, &opts->clearlinks,
|
|
||||||
"clear old links before filling a linksdir", NULL},
|
og = g_option_group_new("mu", "general mu options", "", NULL, NULL);
|
||||||
{ NULL, 0, 0, 0, NULL, NULL, NULL }
|
g_option_group_add_entries(og, entries);
|
||||||
};
|
|
||||||
|
return og;
|
||||||
og = g_option_group_new ("find",
|
|
||||||
"options for the 'find' command",
|
|
||||||
"", NULL, NULL);
|
|
||||||
g_option_group_add_entries (og, entries);
|
|
||||||
|
|
||||||
return og;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void set_group_index_defaults(MuConfigOptions * opts)
|
||||||
|
|
||||||
static GOptionGroup*
|
|
||||||
config_options_group_mkdir (MuConfigOptions *opts)
|
|
||||||
{
|
{
|
||||||
GOptionGroup *og;
|
gchar *exp;
|
||||||
GOptionEntry entries[] = {
|
|
||||||
{"mode", 0, 0, G_OPTION_ARG_INT, &opts->dirmode,
|
|
||||||
"set the mode (as in chmod), in octal notation", NULL},
|
|
||||||
{ NULL, 0, 0, 0, NULL, NULL, NULL }
|
|
||||||
};
|
|
||||||
|
|
||||||
/* set dirmode before, because '0000' is a valid mode */
|
if (!opts->maildir)
|
||||||
opts->dirmode = 0755;
|
opts->maildir = mu_util_guess_maildir();
|
||||||
|
|
||||||
og = g_option_group_new ("mkdir",
|
exp = mu_util_dir_expand(opts->maildir);
|
||||||
"options for the 'mkdir' command",
|
if (exp) {
|
||||||
"", NULL, NULL);
|
g_free(opts->maildir);
|
||||||
g_option_group_add_entries (og, entries);
|
opts->maildir = exp;
|
||||||
|
}
|
||||||
return og;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static GOptionGroup *config_options_group_index(MuConfigOptions * opts)
|
||||||
static GOptionGroup*
|
|
||||||
config_options_group_extract (MuConfigOptions *opts)
|
|
||||||
{
|
{
|
||||||
GOptionGroup *og;
|
GOptionGroup *og;
|
||||||
GOptionEntry entries[] = {
|
GOptionEntry entries[] = {
|
||||||
{"save-attachments", 'a', 0, G_OPTION_ARG_NONE,
|
{"maildir", 'm', 0, G_OPTION_ARG_FILENAME, &opts->maildir,
|
||||||
&opts->save_attachments,
|
"top of the maildir", NULL},
|
||||||
"save all attachments", NULL},
|
{"reindex", 0, 0, G_OPTION_ARG_NONE, &opts->reindex,
|
||||||
{"save-all", 0, 0, G_OPTION_ARG_NONE, &opts->save_all,
|
"index even already indexed messages", NULL},
|
||||||
"save all parts (incl. non-attachments)", NULL},
|
{"rebuild", 0, 0, G_OPTION_ARG_NONE, &opts->rebuild,
|
||||||
{"parts", 0, 0, G_OPTION_ARG_STRING, &opts->parts,
|
"rebuild the database from scratch", NULL},
|
||||||
"save specific parts (comma-separated list)", NULL},
|
{"autoupgrade", 0, 0, G_OPTION_ARG_NONE, &opts->autoupgrade,
|
||||||
{"target-dir", 0, 0, G_OPTION_ARG_FILENAME, &opts->targetdir,
|
"automatically upgrade the database with new mu versions",
|
||||||
"target directory for saving", NULL},
|
NULL},
|
||||||
{"overwrite", 0, 0, G_OPTION_ARG_NONE, &opts->overwrite,
|
{"nocleanup", 0, 0, G_OPTION_ARG_NONE, &opts->nocleanup,
|
||||||
"overwrite existing files", NULL},
|
"don't clean up the database after indexing", NULL},
|
||||||
{ NULL, 0, 0, 0, NULL, NULL, NULL }
|
{NULL, 0, 0, 0, NULL, NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
opts->targetdir = g_strdup("."); /* default is the current dir */
|
og = g_option_group_new("index",
|
||||||
|
"options for the 'index' command",
|
||||||
og = g_option_group_new ("extract",
|
"", NULL, NULL);
|
||||||
"options for the 'extract' command",
|
g_option_group_add_entries(og, entries);
|
||||||
"", NULL, NULL);
|
|
||||||
g_option_group_add_entries (og, entries);
|
return og;
|
||||||
|
|
||||||
return og;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void set_group_find_defaults(MuConfigOptions * opts)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static gboolean
|
|
||||||
parse_params (MuConfigOptions *opts, int *argcp, char ***argvp)
|
|
||||||
{
|
{
|
||||||
GError *err;
|
/* note, when no fields are specified, we use
|
||||||
GOptionContext *context;
|
* date-from-subject, and sort descending by date. If fields
|
||||||
gboolean rv;
|
* *are* specified, we sort in ascending order. */
|
||||||
|
if (!opts->fields) {
|
||||||
context = g_option_context_new ("- mu general option");
|
opts->fields = "d f s";
|
||||||
|
if (!opts->sortfield) {
|
||||||
|
opts->sortfield = "d";
|
||||||
|
opts->descending = TRUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
g_option_context_set_main_group (context,
|
if (opts->linksdir) {
|
||||||
config_options_group_mu (opts));
|
gchar *old = opts->linksdir;
|
||||||
g_option_context_add_group (context,
|
opts->linksdir = mu_util_dir_expand(opts->linksdir);
|
||||||
config_options_group_index (opts));
|
if (!opts->linksdir) /* we'll check the dir later */
|
||||||
g_option_context_add_group (context,
|
opts->linksdir = old;
|
||||||
config_options_group_find (opts));
|
else
|
||||||
g_option_context_add_group (context,
|
g_free(old);
|
||||||
config_options_group_mkdir (opts));
|
}
|
||||||
g_option_context_add_group (context,
|
|
||||||
config_options_group_extract (opts));
|
|
||||||
|
|
||||||
err = NULL;
|
/* FIXME: some warning when summary_len < 0? */
|
||||||
rv = g_option_context_parse (context, argcp, argvp, &err);
|
if (opts->summary_len < 1)
|
||||||
if (!rv) {
|
opts->summary_len = 0;
|
||||||
g_warning ("error in options: %s\n", err->message);
|
|
||||||
g_error_free (err);
|
|
||||||
}
|
|
||||||
|
|
||||||
g_option_context_free (context);
|
|
||||||
|
|
||||||
/* fill in the defaults if user did not specify */
|
|
||||||
set_group_mu_defaults (opts);
|
|
||||||
set_group_index_defaults (opts);
|
|
||||||
set_group_find_defaults (opts);
|
|
||||||
/* set_group_mkdir_defaults (opts); */
|
|
||||||
|
|
||||||
return rv;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static GOptionGroup *config_options_group_find(MuConfigOptions * opts)
|
||||||
gboolean
|
|
||||||
mu_config_init (MuConfigOptions *opts, int *argcp, char ***argvp)
|
|
||||||
{
|
{
|
||||||
g_return_val_if_fail (opts, FALSE);
|
GOptionGroup *og;
|
||||||
memset (opts, 0, sizeof(MuConfigOptions));
|
GOptionEntry entries[] = {
|
||||||
|
{"xquery", 0, 0, G_OPTION_ARG_NONE, &opts->xquery,
|
||||||
|
"print the Xapian query (for debugging)", NULL},
|
||||||
|
{"fields", 'f', 0, G_OPTION_ARG_STRING, &opts->fields,
|
||||||
|
"fields to display in the output", NULL},
|
||||||
|
{"sortfield", 's', 0, G_OPTION_ARG_STRING, &opts->sortfield,
|
||||||
|
"field to sort on", NULL},
|
||||||
|
{"bookmark", 'b', 0, G_OPTION_ARG_STRING, &opts->bookmark,
|
||||||
|
"use a bookmarked query", NULL},
|
||||||
|
{"descending", 'z', 0, G_OPTION_ARG_NONE, &opts->descending,
|
||||||
|
"sort in descending order (z -> a)", NULL},
|
||||||
|
{"summary-len", 'k', 0, G_OPTION_ARG_INT, &opts->summary_len,
|
||||||
|
"max number of lines for summary", NULL},
|
||||||
|
{"linksdir", 0, 0, G_OPTION_ARG_STRING, &opts->linksdir,
|
||||||
|
"output as symbolic links to a target maildir", NULL},
|
||||||
|
{"clearlinks", 0, 0, G_OPTION_ARG_NONE, &opts->clearlinks,
|
||||||
|
"clear old links before filling a linksdir", NULL},
|
||||||
|
{NULL, 0, 0, 0, NULL, NULL, NULL}
|
||||||
|
};
|
||||||
|
|
||||||
/* defaults are set in parse_params */
|
og = g_option_group_new("find",
|
||||||
|
"options for the 'find' command",
|
||||||
if (argcp && argvp)
|
"", NULL, NULL);
|
||||||
if (!parse_params (opts, argcp, argvp))
|
g_option_group_add_entries(og, entries);
|
||||||
return FALSE;
|
|
||||||
|
return og;
|
||||||
|
}
|
||||||
|
|
||||||
|
static GOptionGroup *config_options_group_mkdir(MuConfigOptions * opts)
|
||||||
|
{
|
||||||
|
GOptionGroup *og;
|
||||||
|
GOptionEntry entries[] = {
|
||||||
|
{"mode", 0, 0, G_OPTION_ARG_INT, &opts->dirmode,
|
||||||
|
"set the mode (as in chmod), in octal notation", NULL},
|
||||||
|
{NULL, 0, 0, 0, NULL, NULL, NULL}
|
||||||
|
};
|
||||||
|
|
||||||
|
/* set dirmode before, because '0000' is a valid mode */
|
||||||
|
opts->dirmode = 0755;
|
||||||
|
|
||||||
|
og = g_option_group_new("mkdir",
|
||||||
|
"options for the 'mkdir' command",
|
||||||
|
"", NULL, NULL);
|
||||||
|
g_option_group_add_entries(og, entries);
|
||||||
|
|
||||||
|
return og;
|
||||||
|
}
|
||||||
|
|
||||||
|
static GOptionGroup *config_options_group_extract(MuConfigOptions * opts)
|
||||||
|
{
|
||||||
|
GOptionGroup *og;
|
||||||
|
GOptionEntry entries[] = {
|
||||||
|
{"save-attachments", 'a', 0, G_OPTION_ARG_NONE,
|
||||||
|
&opts->save_attachments,
|
||||||
|
"save all attachments", NULL},
|
||||||
|
{"save-all", 0, 0, G_OPTION_ARG_NONE, &opts->save_all,
|
||||||
|
"save all parts (incl. non-attachments)", NULL},
|
||||||
|
{"parts", 0, 0, G_OPTION_ARG_STRING, &opts->parts,
|
||||||
|
"save specific parts (comma-separated list)", NULL},
|
||||||
|
{"target-dir", 0, 0, G_OPTION_ARG_FILENAME, &opts->targetdir,
|
||||||
|
"target directory for saving", NULL},
|
||||||
|
{"overwrite", 0, 0, G_OPTION_ARG_NONE, &opts->overwrite,
|
||||||
|
"overwrite existing files", NULL},
|
||||||
|
{NULL, 0, 0, 0, NULL, NULL, NULL}
|
||||||
|
};
|
||||||
|
|
||||||
|
opts->targetdir = g_strdup("."); /* default is the current dir */
|
||||||
|
|
||||||
|
og = g_option_group_new("extract",
|
||||||
|
"options for the 'extract' command",
|
||||||
|
"", NULL, NULL);
|
||||||
|
g_option_group_add_entries(og, entries);
|
||||||
|
|
||||||
|
return og;
|
||||||
|
}
|
||||||
|
|
||||||
|
static gboolean parse_params(MuConfigOptions * opts, int *argcp, char ***argvp)
|
||||||
|
{
|
||||||
|
GError *err;
|
||||||
|
GOptionContext *context;
|
||||||
|
gboolean rv;
|
||||||
|
|
||||||
|
context = g_option_context_new("- mu general option");
|
||||||
|
|
||||||
|
g_option_context_set_main_group(context, config_options_group_mu(opts));
|
||||||
|
g_option_context_add_group(context, config_options_group_index(opts));
|
||||||
|
g_option_context_add_group(context, config_options_group_find(opts));
|
||||||
|
g_option_context_add_group(context, config_options_group_mkdir(opts));
|
||||||
|
g_option_context_add_group(context, config_options_group_extract(opts));
|
||||||
|
|
||||||
|
err = NULL;
|
||||||
|
rv = g_option_context_parse(context, argcp, argvp, &err);
|
||||||
|
if (!rv) {
|
||||||
|
g_warning("error in options: %s\n", err->message);
|
||||||
|
g_error_free(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
g_option_context_free(context);
|
||||||
|
|
||||||
|
/* fill in the defaults if user did not specify */
|
||||||
|
set_group_mu_defaults(opts);
|
||||||
|
set_group_index_defaults(opts);
|
||||||
|
set_group_find_defaults(opts);
|
||||||
|
/* set_group_mkdir_defaults (opts); */
|
||||||
|
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
|
||||||
|
gboolean mu_config_init(MuConfigOptions * opts, int *argcp, char ***argvp)
|
||||||
|
{
|
||||||
|
g_return_val_if_fail(opts, FALSE);
|
||||||
|
memset(opts, 0, sizeof(MuConfigOptions));
|
||||||
|
|
||||||
|
/* defaults are set in parse_params */
|
||||||
|
|
||||||
|
if (argcp && argvp)
|
||||||
|
if (!parse_params(opts, argcp, argvp))
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
void mu_config_uninit(MuConfigOptions * opts)
|
||||||
|
{
|
||||||
|
g_return_if_fail(opts);
|
||||||
|
|
||||||
return TRUE;
|
g_free(opts->muhome);
|
||||||
|
g_free(opts->maildir);
|
||||||
|
g_free(opts->linksdir);
|
||||||
|
g_free(opts->targetdir);
|
||||||
|
|
||||||
|
g_strfreev(opts->params);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
mu_config_uninit (MuConfigOptions *opts)
|
|
||||||
{
|
|
||||||
g_return_if_fail (opts);
|
|
||||||
|
|
||||||
g_free (opts->muhome);
|
|
||||||
g_free (opts->maildir);
|
|
||||||
g_free (opts->linksdir);
|
|
||||||
g_free (opts->targetdir);
|
|
||||||
|
|
||||||
g_strfreev (opts->params);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue