* mu-config.c: cosmetics

This commit is contained in:
Dirk-Jan C. Binnema 2010-12-07 23:17:05 +02:00
parent 373973d813
commit b7e3786af7
1 changed files with 217 additions and 249 deletions

View File

@ -27,69 +27,60 @@
#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) if (!opts->muhome)
opts->muhome = mu_util_guess_mu_homedir (); opts->muhome = mu_util_guess_mu_homedir();
exp = mu_util_dir_expand (opts->muhome); exp = mu_util_dir_expand(opts->muhome);
if (exp) { if (exp) {
g_free (opts->muhome); g_free(opts->muhome);
opts->muhome = exp; opts->muhome = exp;
} }
} }
static GOptionGroup *config_options_group_mu(MuConfigOptions * opts)
static GOptionGroup*
config_options_group_mu (MuConfigOptions *opts)
{ {
GOptionGroup *og; GOptionGroup *og;
GOptionEntry entries[] = { GOptionEntry entries[] = {
{ "debug", 'd', 0, G_OPTION_ARG_NONE, &opts->debug, {"debug", 'd', 0, G_OPTION_ARG_NONE, &opts->debug,
"print debug output to standard error", NULL }, "print debug output to standard error", NULL},
{ "quiet", 'q', 0, G_OPTION_ARG_NONE, &opts->quiet, {"quiet", 'q', 0, G_OPTION_ARG_NONE, &opts->quiet,
"don't give any progress information", NULL }, "don't give any progress information", NULL},
{"version", 'v', 0, G_OPTION_ARG_NONE, &opts->version, {"version", 'v', 0, G_OPTION_ARG_NONE, &opts->version,
"display version and copyright information", NULL}, "display version and copyright information", NULL},
{"muhome", 0, 0, G_OPTION_ARG_FILENAME, &opts->muhome, {"muhome", 0, 0, G_OPTION_ARG_FILENAME, &opts->muhome,
"specify an alternative mu directory", NULL}, "specify an alternative mu directory", NULL},
{"log-stderr", 0, 0, G_OPTION_ARG_NONE, &opts->log_stderr, {"log-stderr", 0, 0, G_OPTION_ARG_NONE, &opts->log_stderr,
"log to standard error", NULL}, "log to standard error", 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}
}; };
og = g_option_group_new ("mu", "general mu options", og = g_option_group_new("mu", "general mu options", "", NULL, NULL);
"", NULL, NULL); g_option_group_add_entries(og, entries);
g_option_group_add_entries (og, entries);
return og; return og;
} }
static void set_group_index_defaults(MuConfigOptions * opts)
static void
set_group_index_defaults (MuConfigOptions *opts)
{ {
gchar *exp; gchar *exp;
if (!opts->maildir) if (!opts->maildir)
opts->maildir = mu_util_guess_maildir(); opts->maildir = mu_util_guess_maildir();
exp = mu_util_dir_expand (opts->maildir); exp = mu_util_dir_expand(opts->maildir);
if (exp) { if (exp) {
g_free (opts->maildir); g_free(opts->maildir);
opts->maildir = exp; opts->maildir = exp;
} }
} }
static GOptionGroup *config_options_group_index(MuConfigOptions * opts)
static GOptionGroup*
config_options_group_index (MuConfigOptions *opts)
{ {
GOptionGroup *og; GOptionGroup *og;
GOptionEntry entries[] = { GOptionEntry entries[] = {
@ -100,23 +91,22 @@ config_options_group_index (MuConfigOptions *opts)
{"rebuild", 0, 0, G_OPTION_ARG_NONE, &opts->rebuild, {"rebuild", 0, 0, G_OPTION_ARG_NONE, &opts->rebuild,
"rebuild the database from scratch", NULL}, "rebuild the database from scratch", NULL},
{"autoupgrade", 0, 0, G_OPTION_ARG_NONE, &opts->autoupgrade, {"autoupgrade", 0, 0, G_OPTION_ARG_NONE, &opts->autoupgrade,
"automatically upgrade the database with new mu versions", NULL}, "automatically upgrade the database with new mu versions",
NULL},
{"nocleanup", 0, 0, G_OPTION_ARG_NONE, &opts->nocleanup, {"nocleanup", 0, 0, G_OPTION_ARG_NONE, &opts->nocleanup,
"don't clean up the database after indexing", NULL}, "don't clean up the database after indexing", NULL},
{ NULL, 0, 0, 0, NULL, NULL, NULL } {NULL, 0, 0, 0, NULL, NULL, NULL}
}; };
og = g_option_group_new ("index", og = g_option_group_new("index",
"options for the 'index' command", "options for the 'index' command",
"", NULL, NULL); "", NULL, NULL);
g_option_group_add_entries (og, entries); g_option_group_add_entries(og, entries);
return og; return og;
} }
static void set_group_find_defaults(MuConfigOptions * opts)
static void
set_group_find_defaults (MuConfigOptions *opts)
{ {
/* note, when no fields are specified, we use /* note, when no fields are specified, we use
* date-from-subject, and sort descending by date. If fields * date-from-subject, and sort descending by date. If fields
@ -131,7 +121,7 @@ set_group_find_defaults (MuConfigOptions *opts)
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);
if (!opts->linksdir) /* we'll check the dir later */ if (!opts->linksdir) /* we'll check the dir later */
opts->linksdir = old; opts->linksdir = old;
else else
@ -143,9 +133,7 @@ set_group_find_defaults (MuConfigOptions *opts)
opts->summary_len = 0; opts->summary_len = 0;
} }
static GOptionGroup *config_options_group_find(MuConfigOptions * opts)
static GOptionGroup*
config_options_group_find (MuConfigOptions *opts)
{ {
GOptionGroup *og; GOptionGroup *og;
GOptionEntry entries[] = { GOptionEntry entries[] = {
@ -162,46 +150,41 @@ config_options_group_find (MuConfigOptions *opts)
{"summary-len", 'k', 0, G_OPTION_ARG_INT, &opts->summary_len, {"summary-len", 'k', 0, G_OPTION_ARG_INT, &opts->summary_len,
"max number of lines for summary", NULL}, "max number of lines for summary", NULL},
{"linksdir", 0, 0, G_OPTION_ARG_STRING, &opts->linksdir, {"linksdir", 0, 0, G_OPTION_ARG_STRING, &opts->linksdir,
"output as symbolic links to a target maildir", NULL }, "output as symbolic links to a target maildir", NULL},
{"clearlinks", 0, 0, G_OPTION_ARG_NONE, &opts->clearlinks, {"clearlinks", 0, 0, G_OPTION_ARG_NONE, &opts->clearlinks,
"clear old links before filling a linksdir", NULL}, "clear old links before filling a linksdir", NULL},
{ NULL, 0, 0, 0, NULL, NULL, NULL } {NULL, 0, 0, 0, NULL, NULL, NULL}
}; };
og = g_option_group_new ("find", og = g_option_group_new("find",
"options for the 'find' command", "options for the 'find' command",
"", NULL, NULL); "", NULL, NULL);
g_option_group_add_entries (og, entries); g_option_group_add_entries(og, entries);
return og; return og;
} }
static GOptionGroup *config_options_group_mkdir(MuConfigOptions * opts)
static GOptionGroup*
config_options_group_mkdir (MuConfigOptions *opts)
{ {
GOptionGroup *og; GOptionGroup *og;
GOptionEntry entries[] = { GOptionEntry entries[] = {
{"mode", 0, 0, G_OPTION_ARG_INT, &opts->dirmode, {"mode", 0, 0, G_OPTION_ARG_INT, &opts->dirmode,
"set the mode (as in chmod), in octal notation", NULL}, "set the mode (as in chmod), in octal notation", NULL},
{ NULL, 0, 0, 0, NULL, NULL, NULL } {NULL, 0, 0, 0, NULL, NULL, NULL}
}; };
/* set dirmode before, because '0000' is a valid mode */ /* set dirmode before, because '0000' is a valid mode */
opts->dirmode = 0755; opts->dirmode = 0755;
og = g_option_group_new ("mkdir", og = g_option_group_new("mkdir",
"options for the 'mkdir' command", "options for the 'mkdir' command",
"", NULL, NULL); "", NULL, NULL);
g_option_group_add_entries (og, entries); g_option_group_add_entries(og, entries);
return og; return og;
} }
static GOptionGroup *config_options_group_extract(MuConfigOptions * opts)
static GOptionGroup*
config_options_group_extract (MuConfigOptions *opts)
{ {
GOptionGroup *og; GOptionGroup *og;
GOptionEntry entries[] = { GOptionEntry entries[] = {
@ -216,88 +199,73 @@ config_options_group_extract (MuConfigOptions *opts)
"target directory for saving", NULL}, "target directory for saving", NULL},
{"overwrite", 0, 0, G_OPTION_ARG_NONE, &opts->overwrite, {"overwrite", 0, 0, G_OPTION_ARG_NONE, &opts->overwrite,
"overwrite existing files", NULL}, "overwrite existing files", NULL},
{ NULL, 0, 0, 0, NULL, NULL, NULL } {NULL, 0, 0, 0, NULL, NULL, NULL}
}; };
opts->targetdir = g_strdup("."); /* default is the current dir */ opts->targetdir = g_strdup("."); /* default is the current dir */
og = g_option_group_new ("extract", og = g_option_group_new("extract",
"options for the 'extract' command", "options for the 'extract' command",
"", NULL, NULL); "", NULL, NULL);
g_option_group_add_entries (og, entries); g_option_group_add_entries(og, entries);
return og; return og;
} }
static gboolean parse_params(MuConfigOptions * opts, int *argcp, char ***argvp)
static gboolean
parse_params (MuConfigOptions *opts, int *argcp, char ***argvp)
{ {
GError *err; GError *err;
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)); g_option_context_add_group(context, config_options_group_index(opts));
g_option_context_add_group (context, g_option_context_add_group(context, config_options_group_find(opts));
config_options_group_index (opts)); g_option_context_add_group(context, config_options_group_mkdir(opts));
g_option_context_add_group (context, g_option_context_add_group(context, config_options_group_extract(opts));
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; err = NULL;
rv = g_option_context_parse (context, argcp, argvp, &err); rv = g_option_context_parse(context, argcp, argvp, &err);
if (!rv) { if (!rv) {
g_warning ("error in options: %s\n", err->message); g_warning("error in options: %s\n", err->message);
g_error_free (err); g_error_free(err);
} }
g_option_context_free (context); g_option_context_free(context);
/* fill in the defaults if user did not specify */ /* fill in the defaults if user did not specify */
set_group_mu_defaults (opts); set_group_mu_defaults(opts);
set_group_index_defaults (opts); set_group_index_defaults(opts);
set_group_find_defaults (opts); set_group_find_defaults(opts);
/* set_group_mkdir_defaults (opts); */ /* set_group_mkdir_defaults (opts); */
return rv; return rv;
} }
gboolean mu_config_init(MuConfigOptions * opts, int *argcp, char ***argvp)
gboolean
mu_config_init (MuConfigOptions *opts, int *argcp, char ***argvp)
{ {
g_return_val_if_fail (opts, FALSE); g_return_val_if_fail(opts, FALSE);
memset (opts, 0, sizeof(MuConfigOptions)); memset(opts, 0, sizeof(MuConfigOptions));
/* defaults are set in parse_params */ /* defaults are set in parse_params */
if (argcp && argvp) if (argcp && argvp)
if (!parse_params (opts, argcp, argvp)) if (!parse_params(opts, argcp, argvp))
return FALSE; return FALSE;
return TRUE; return TRUE;
} }
void void mu_config_uninit(MuConfigOptions * opts)
mu_config_uninit (MuConfigOptions *opts)
{ {
g_return_if_fail (opts); g_return_if_fail(opts);
g_free (opts->muhome); g_free(opts->muhome);
g_free (opts->maildir); g_free(opts->maildir);
g_free (opts->linksdir); g_free(opts->linksdir);
g_free (opts->targetdir); g_free(opts->targetdir);
g_strfreev (opts->params); g_strfreev(opts->params);
} }