31
32
#include "mu-config.h"
32
33
#include "mu-cmd.h"
38
get_output_format (const char *formatstr)
43
MuConfigFormat format;
45
{"mutt-alias", MU_CONFIG_FORMAT_MUTT_ALIAS},
46
{"mutt-ab", MU_CONFIG_FORMAT_MUTT_AB},
47
{"wl", MU_CONFIG_FORMAT_WL},
48
{"csv", MU_CONFIG_FORMAT_CSV},
49
{"org-contact", MU_CONFIG_FORMAT_ORG_CONTACT},
50
{"bbdb", MU_CONFIG_FORMAT_BBDB},
51
{"links", MU_CONFIG_FORMAT_LINKS},
52
{"plain", MU_CONFIG_FORMAT_PLAIN},
53
{"sexp", MU_CONFIG_FORMAT_SEXP},
54
{"xml", MU_CONFIG_FORMAT_XML},
55
{"xquery", MU_CONFIG_FORMAT_XQUERY}
58
for (i = 0; i != G_N_ELEMENTS(formats); i++)
59
if (strcmp (formats[i].name, formatstr) == 0)
60
return formats[i].format;
62
return MU_CONFIG_FORMAT_UNKNOWN;
35
67
set_group_mu_defaults (MuConfig *opts)
72
102
"specify an alternative mu directory", NULL},
73
103
{"log-stderr", 0, 0, G_OPTION_ARG_NONE, &opts->log_stderr,
74
104
"log to standard error (false)", NULL},
75
{"color", 0, 0, G_OPTION_ARG_NONE, &opts->color,
76
"use ANSI-colors in some output (false)", NULL},
105
{"nocolor", 0, 0, G_OPTION_ARG_NONE, &opts->nocolor,
106
"don't use ANSI-colors in some output (false)", NULL},
78
108
{G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY,
79
109
&opts->params, "parameters", NULL},
80
110
{NULL, 0, 0, 0, NULL, NULL, NULL}
134
172
* *are* specified, we sort in ascending order. */
135
173
if (!opts->fields) {
136
174
opts->fields = "d f s";
137
if (!opts->sortfield) {
175
if (!opts->sortfield)
138
176
opts->sortfield = "d";
139
opts->descending = TRUE;
143
179
if (!opts->formatstr) /* by default, use plain output */
144
opts->formatstr = MU_CONFIG_FORMAT_PLAIN;
180
opts->format = MU_CONFIG_FORMAT_PLAIN;
183
get_output_format (opts->formatstr);
146
185
if (opts->linksdir) {
147
186
gchar *old = opts->linksdir;
148
187
opts->linksdir = mu_util_dir_expand(opts->linksdir);
166
205
"show message threads", NULL},
167
206
{"bookmark", 'b', 0, G_OPTION_ARG_STRING, &opts->bookmark,
168
207
"use a bookmarked query", NULL},
169
{"descending", 'z', 0, G_OPTION_ARG_NONE, &opts->descending,
170
"sort in descending order (z -> a)", NULL},
208
{"reverse", 'z', 0, G_OPTION_ARG_NONE, &opts->reverse,
209
"sort in reverse (descending) order (z -> a)", NULL},
171
210
{"summary", 'k', 0, G_OPTION_ARG_NONE, &opts->summary,
172
211
"include a short summary of the message (false)", NULL},
173
212
{"linksdir", 0, 0, G_OPTION_ARG_STRING, &opts->linksdir,
176
215
"clear old links before filling a linksdir (false)", NULL},
177
216
{"format", 'o', 0, G_OPTION_ARG_STRING, &opts->formatstr,
178
217
"output format ('plain'(*), 'links', 'xml',"
179
"'json', 'sexp', 'xquery')", NULL},
218
"'sexp', 'xquery')", NULL},
180
219
{"exec", 'e', 0, G_OPTION_ARG_STRING, &opts->exec,
181
220
"execute command on each match message", NULL},
221
{"include-unreable", 0, 0, G_OPTION_ARG_NONE,
222
&opts->include_unreadable,
223
"don't ignore messages without a disk file (false)", NULL},
182
224
{NULL, 0, 0, 0, NULL, NULL, NULL}
229
274
"'org-contact', 'csv')", NULL},
230
275
{NULL, 0, 0, 0, NULL, NULL, NULL}
233
278
og = g_option_group_new("cfind", "options for the 'cfind' command",
235
280
g_option_group_add_entries(og, entries);
247
301
"only show a short summary of the message (false)", NULL},
248
302
{"terminate", 0, 0, G_OPTION_ARG_NONE, &opts->terminator,
249
303
"terminate messages with ascii-0x07 (\\f, form-feed)", NULL},
304
{"format", 'o', 0, G_OPTION_ARG_STRING, &opts->formatstr,
305
"output format ('plain'(*), 'sexp')", NULL},
250
306
{NULL, 0, 0, 0, NULL, NULL, NULL}
253
309
og = g_option_group_new("view", "options for the 'view' command",
255
311
g_option_group_add_entries(og, entries);
351
config_options_group_server (MuConfig * opts)
354
GOptionEntry entries[] = {
355
{"maildir", 'm', 0, G_OPTION_ARG_FILENAME, &opts->maildir,
356
"top of the maildir", NULL},
357
{NULL, 0, 0, 0, NULL, NULL, NULL}
360
og = g_option_group_new("server",
361
"options for the 'server' command",
363
g_option_group_add_entries(og, entries);
295
371
parse_cmd (MuConfig *opts, int *argcp, char ***argvp)
299
375
const gchar* _name;
300
376
MuConfigCmd _cmd;
378
{ "cfind", MU_CONFIG_CMD_CFIND },
379
{ "extract", MU_CONFIG_CMD_EXTRACT },
380
{ "find", MU_CONFIG_CMD_FIND },
304
381
{ "index", MU_CONFIG_CMD_INDEX },
305
{ "find", MU_CONFIG_CMD_FIND },
306
{ "cleanup", MU_CONFIG_CMD_CLEANUP },
307
382
{ "mkdir", MU_CONFIG_CMD_MKDIR },
308
383
{ "view", MU_CONFIG_CMD_VIEW },
309
{ "extract", MU_CONFIG_CMD_EXTRACT },
310
{ "cfind", MU_CONFIG_CMD_CFIND },
384
{ "add", MU_CONFIG_CMD_ADD },
385
{ "remove", MU_CONFIG_CMD_REMOVE },
386
{ "server", MU_CONFIG_CMD_SERVER }
313
389
opts->cmd = MU_CONFIG_CMD_NONE;
314
390
opts->cmdstr = NULL;
316
392
if (*argcp < 2) /* no command found at all */
318
else if ((**argvp)[1] == '-')
394
else if ((**argvp)[1] == '-')
319
395
/* if the first param starts with '-', there is no
320
396
* command, just some option (like --version, --help
324
400
opts->cmd = MU_CONFIG_CMD_UNKNOWN;
325
401
opts->cmdstr = (*argvp)[1];
327
for (i = 0; i != G_N_ELEMENTS(cmd_map); ++i)
403
for (i = 0; i != G_N_ELEMENTS(cmd_map); ++i)
328
404
if (strcmp (opts->cmdstr, cmd_map[i]._name) == 0)
329
405
opts->cmd = cmd_map[i]._cmd;
338
414
GOptionGroup *group;
342
416
switch (opts->cmd) {
343
417
case MU_CONFIG_CMD_INDEX:
344
group = config_options_group_index (opts);
418
group = config_options_group_index (opts); break;
346
419
case MU_CONFIG_CMD_FIND:
347
group = config_options_group_find (opts);
420
group = config_options_group_find (opts); break;
349
421
case MU_CONFIG_CMD_MKDIR:
350
group = config_options_group_mkdir (opts);
422
group = config_options_group_mkdir (opts); break;
352
423
case MU_CONFIG_CMD_EXTRACT:
353
group = config_options_group_extract (opts);
424
group = config_options_group_extract (opts); break;
355
425
case MU_CONFIG_CMD_CFIND:
356
group = config_options_group_cfind (opts);
426
group = config_options_group_cfind (opts); break;
358
427
case MU_CONFIG_CMD_VIEW:
359
group = config_options_group_view (opts);
428
group = config_options_group_view (opts); break;
429
case MU_CONFIG_CMD_SERVER:
430
group = config_options_group_server (opts); break;
432
return; /* no group to add */
365
g_option_context_add_group(context, group);
435
g_option_context_add_group(context, group);
372
442
GError *err = NULL;
373
443
GOptionContext *context;
376
446
context = g_option_context_new("- mu general option");
377
447
g_option_context_set_main_group(context,
378
448
config_options_group_mu(opts));
380
450
add_context_group (context, opts);
382
452
rv = g_option_context_parse (context, argcp, argvp, &err);
383
453
g_option_context_free (context);
396
466
MuConfig *config;
398
468
g_return_val_if_fail (argcp && argvp, NULL);
400
470
config = g_new0 (MuConfig, 1);
402
472
if (!parse_cmd (config, argcp, argvp) ||
403
473
!parse_params(config, argcp, argvp)) {
404
474
mu_config_destroy (config);
408
478
/* fill in the defaults if user did not specify */
409
479
set_group_mu_defaults (config);
410
480
set_group_index_defaults (config);
411
481
set_group_find_defaults (config);
412
482
set_group_cfind_defaults (config);
483
set_group_view_defaults (config);
413
484
/* set_group_mkdir_defaults (config); */
424
495
g_free (opts->muhome);
425
496
g_free (opts->maildir);
426
497
g_free (opts->linksdir);
427
498
g_free (opts->targetdir);
428
500
g_strfreev (opts->params);
434
show_usage (gboolean noerror)
437
"usage: mu command [options] [parameters]\n"
438
"where command is one of index, find, cfind, view, mkdir, cleanup "
440
"see the mu, mu-<command> or mu-easy manpages for "
441
"more information\n";
444
g_print ("%s", usage);
446
g_printerr ("%s", usage);
452
g_print ("mu (mail indexer/searcher) version " VERSION "\n"
453
"Copyright (C) 2008-2011 Dirk-Jan C. Binnema (GPLv3+)\n");
458
mu_config_execute (MuConfig *opts)
460
g_return_val_if_fail (opts, MU_EXITCODE_ERROR);
464
return MU_EXITCODE_OK;
467
if (!opts->params||!opts->params[0]) {/* no command? */
471
return MU_EXITCODE_ERROR;
475
case MU_CONFIG_CMD_CLEANUP: return mu_cmd_cleanup (opts);
476
case MU_CONFIG_CMD_EXTRACT: return mu_cmd_extract (opts);
477
case MU_CONFIG_CMD_FIND: return mu_cmd_find (opts);
478
case MU_CONFIG_CMD_INDEX: return mu_cmd_index (opts);
479
case MU_CONFIG_CMD_MKDIR: return mu_cmd_mkdir (opts);
480
case MU_CONFIG_CMD_VIEW: return mu_cmd_view (opts);
481
case MU_CONFIG_CMD_CFIND: return mu_cmd_cfind (opts);
482
case MU_CONFIG_CMD_UNKNOWN:
483
g_printerr ("mu: unknown command '%s'\n\n", opts->cmdstr);
485
return MU_EXITCODE_ERROR;
487
g_return_val_if_reached (MU_EXITCODE_ERROR);
492
506
mu_config_param_num (MuConfig *conf)
496
510
g_return_val_if_fail (conf, 0);
498
511
for (u = 0; conf->params[u]; ++u);