~ubuntu-branches/ubuntu/quantal/maildir-utils/quantal

« back to all changes in this revision

Viewing changes to src/mu-config.c

  • Committer: Package Import Robot
  • Author(s): Norbert Preining
  • Date: 2012-03-15 08:45:56 UTC
  • mfrom: (1.2.5)
  • Revision ID: package-import@ubuntu.com-20120315084556-vqdvw8sgodo24aji
Tags: 0.9.8.2-1
* several new upstream versions
* b-d on libgmime-2.6-dev (Closes: #664001, #664006)
* bump standards version to 3.9.3, no changes necessary
* switch to source format 3.0 (quilt): debian/control, debian/rules
* maildir-utils depends on dpkg/install-info (lintian warning)
* fix man page lintian warnings

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/
 
2
 
2
3
/*
3
 
** Copyright (C) 2008-2011 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
 
4
** Copyright (C) 2008-2012 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
4
5
**
5
6
** This program is free software; you can redistribute it and/or modify it
6
7
** under the terms of the GNU General Public License as published by the
31
32
#include "mu-config.h"
32
33
#include "mu-cmd.h"
33
34
 
 
35
 
 
36
 
 
37
static MuConfigFormat
 
38
get_output_format (const char *formatstr)
 
39
{
 
40
        int i;
 
41
        struct {
 
42
                const char*     name;
 
43
                MuConfigFormat  format;
 
44
        } formats [] = {
 
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}
 
56
        };
 
57
 
 
58
        for (i = 0; i != G_N_ELEMENTS(formats); i++)
 
59
                if (strcmp (formats[i].name, formatstr) == 0)
 
60
                        return formats[i].format;
 
61
 
 
62
        return MU_CONFIG_FORMAT_UNKNOWN;
 
63
}
 
64
 
 
65
 
34
66
static void
35
67
set_group_mu_defaults (MuConfig *opts)
36
68
{
45
77
                opts->muhome = exp;
46
78
        }
47
79
 
 
80
        /* check for the MU_NOCOLOR env var; but in any case don't
 
81
         * use colors unless we're writing to a tty */
 
82
        if (g_getenv (MU_NOCOLOR) != NULL)
 
83
                opts->nocolor = TRUE;
48
84
 
49
 
        /* check for the MU_COLORS env var; but in any case don't use
50
 
         * colors unless we're writing to a tty */
51
 
        
52
 
        if (g_getenv (MU_COLORS) != NULL)
53
 
                opts->color = TRUE;
54
 
        
55
85
        if (!isatty(fileno(stdout)))
56
 
                opts->color = FALSE;
 
86
                opts->nocolor = TRUE;
57
87
 
58
88
}
59
89
 
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},
77
 
                
 
105
                {"nocolor", 0, 0, G_OPTION_ARG_NONE, &opts->nocolor,
 
106
                 "don't use ANSI-colors in some output (false)", NULL},
 
107
 
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}
87
117
}
88
118
 
89
119
static void
90
 
set_group_index_defaults (MuConfig * opts)
 
120
set_group_index_defaults (MuConfig *opts)
91
121
{
92
 
        /* note: opts->maildir is handled in mu-cmd, as we need to
93
 
         * have a MuIndex entry for mu_index_last_used_maildir ()
94
 
         */
95
 
        opts->xbatchsize = 0;
 
122
        char *exp;
 
123
 
 
124
        if (!opts->maildir)
 
125
                opts->maildir = mu_util_guess_maildir ();
 
126
 
 
127
        if (opts->maildir) {
 
128
                exp = mu_util_dir_expand(opts->maildir);
 
129
                if (exp) {
 
130
                        g_free(opts->maildir);
 
131
                        opts->maildir = exp;
 
132
                }
 
133
        }
96
134
}
97
135
 
98
136
static GOptionGroup*
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;
140
 
                }
141
177
        }
142
178
 
143
179
        if (!opts->formatstr) /* by default, use plain output */
144
 
                opts->formatstr = MU_CONFIG_FORMAT_PLAIN;
145
 
                
 
180
                opts->format = MU_CONFIG_FORMAT_PLAIN;
 
181
        else
 
182
                opts->format =
 
183
                        get_output_format (opts->formatstr);
 
184
 
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}
183
225
        };
184
226
 
215
257
set_group_cfind_defaults (MuConfig *opts)
216
258
{
217
259
        if (!opts->formatstr) /* by default, use plain output */
218
 
                opts->formatstr = MU_CONFIG_FORMAT_PLAIN;
 
260
                opts->format = MU_CONFIG_FORMAT_PLAIN;
 
261
        else
 
262
                opts->format  = get_output_format (opts->formatstr);
 
263
 
219
264
}
220
265
 
221
266
 
229
274
                 "'org-contact', 'csv')", NULL},
230
275
                {NULL, 0, 0, 0, NULL, NULL, NULL}
231
276
        };
232
 
                
 
277
 
233
278
        og = g_option_group_new("cfind", "options for the 'cfind' command",
234
279
                                "", NULL, NULL);
235
280
        g_option_group_add_entries(og, entries);
238
283
}
239
284
 
240
285
 
 
286
static void
 
287
set_group_view_defaults (MuConfig *opts)
 
288
{
 
289
        if (!opts->formatstr) /* by default, use plain output */
 
290
                opts->format = MU_CONFIG_FORMAT_PLAIN;
 
291
        else
 
292
                opts->format  = get_output_format (opts->formatstr);
 
293
}
 
294
 
241
295
static GOptionGroup *
242
296
config_options_group_view (MuConfig *opts)
243
297
{
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}
251
307
        };
252
 
                
 
308
 
253
309
        og = g_option_group_new("view", "options for the 'view' command",
254
310
                                "", NULL, NULL);
255
311
        g_option_group_add_entries(og, entries);
291
347
}
292
348
 
293
349
 
294
 
static gboolean  
 
350
static GOptionGroup*
 
351
config_options_group_server (MuConfig * opts)
 
352
{
 
353
        GOptionGroup *og;
 
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}
 
358
        };
 
359
 
 
360
        og = g_option_group_new("server",
 
361
                                "options for the 'server' command",
 
362
                                "", NULL, NULL);
 
363
        g_option_group_add_entries(og, entries);
 
364
 
 
365
        return og;
 
366
}
 
367
 
 
368
 
 
369
 
 
370
static gboolean
295
371
parse_cmd (MuConfig *opts, int *argcp, char ***argvp)
296
372
{
297
373
        int i;
298
 
        typedef struct {
 
374
        struct {
299
375
                const gchar*    _name;
300
376
                MuConfigCmd     _cmd;
301
 
        } Cmd;
302
 
                
303
 
        Cmd cmd_map[] = {
 
377
        } cmd_map[] = {
 
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 }
311
387
        };
312
 
                
 
388
 
313
389
        opts->cmd        = MU_CONFIG_CMD_NONE;
314
390
        opts->cmdstr = NULL;
315
 
                
 
391
 
316
392
        if (*argcp < 2) /* no command found at all */
317
393
                return TRUE;
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
321
397
                 * etc.)*/
322
398
                return TRUE;
323
 
                
 
399
 
324
400
        opts->cmd    = MU_CONFIG_CMD_UNKNOWN;
325
401
        opts->cmdstr = (*argvp)[1];
326
402
 
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;
330
406
 
337
413
{
338
414
        GOptionGroup *group;
339
415
 
340
 
        group = NULL;
341
 
                
342
416
        switch (opts->cmd) {
343
417
        case MU_CONFIG_CMD_INDEX:
344
 
                group = config_options_group_index (opts);
345
 
                break;
 
418
                group = config_options_group_index (opts);      break;
346
419
        case MU_CONFIG_CMD_FIND:
347
 
                group = config_options_group_find (opts);
348
 
                break;
 
420
                group = config_options_group_find (opts);       break;
349
421
        case MU_CONFIG_CMD_MKDIR:
350
 
                group = config_options_group_mkdir (opts);
351
 
                break;
 
422
                group = config_options_group_mkdir (opts);      break;
352
423
        case MU_CONFIG_CMD_EXTRACT:
353
 
                group = config_options_group_extract (opts);
354
 
                break;
 
424
                group = config_options_group_extract (opts);    break;
355
425
        case MU_CONFIG_CMD_CFIND:
356
 
                group = config_options_group_cfind (opts);
357
 
                break;
 
426
                group = config_options_group_cfind (opts);      break;
358
427
        case MU_CONFIG_CMD_VIEW:
359
 
                group = config_options_group_view (opts);
360
 
                break;
361
 
        default: break;
 
428
                group = config_options_group_view (opts);       break;
 
429
        case MU_CONFIG_CMD_SERVER:
 
430
                group = config_options_group_server (opts);     break;
 
431
        default:
 
432
                return; /* no group to add */
362
433
        }
363
434
 
364
 
        if (group)
365
 
                g_option_context_add_group(context, group);
 
435
        g_option_context_add_group(context, group);
366
436
}
367
437
 
368
438
 
372
442
        GError *err = NULL;
373
443
        GOptionContext *context;
374
444
        gboolean rv;
375
 
                
 
445
 
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));
379
449
 
380
450
        add_context_group (context, opts);
381
 
                
 
451
 
382
452
        rv = g_option_context_parse (context, argcp, argvp, &err);
383
453
        g_option_context_free (context);
384
454
        if (!rv) {
388
458
        }
389
459
        return TRUE;
390
460
}
391
 
                                
 
461
 
392
462
 
393
463
MuConfig*
394
464
mu_config_new (int *argcp, char ***argvp)
396
466
        MuConfig *config;
397
467
 
398
468
        g_return_val_if_fail (argcp && argvp, NULL);
399
 
                
 
469
 
400
470
        config = g_new0 (MuConfig, 1);
401
 
                        
 
471
 
402
472
        if (!parse_cmd (config, argcp, argvp) ||
403
473
            !parse_params(config, argcp, argvp)) {
404
474
                mu_config_destroy (config);
405
475
                return NULL;
406
476
        }
407
 
                
 
477
 
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); */
414
 
                
 
485
 
415
486
        return config;
416
487
}
417
488
 
420
491
{
421
492
        if (!opts)
422
493
                return;
423
 
                
 
494
 
424
495
        g_free (opts->muhome);
425
496
        g_free (opts->maildir);
426
497
        g_free (opts->linksdir);
427
498
        g_free (opts->targetdir);
 
499
 
428
500
        g_strfreev (opts->params);
429
501
        g_free (opts);
430
502
}
431
503
 
432
504
 
433
 
static void
434
 
show_usage (gboolean noerror)
435
 
{
436
 
        const char* usage=
437
 
                "usage: mu command [options] [parameters]\n"
438
 
                "where command is one of index, find, cfind, view, mkdir, cleanup "
439
 
                "or extract\n\n"
440
 
                "see the mu, mu-<command> or mu-easy manpages for "
441
 
                "more information\n";
442
 
 
443
 
        if (noerror)
444
 
                g_print ("%s", usage);
445
 
        else
446
 
                g_printerr ("%s", usage);
447
 
}
448
 
 
449
 
static void
450
 
show_version (void)
451
 
{
452
 
        g_print ("mu (mail indexer/searcher) version " VERSION "\n"
453
 
                 "Copyright (C) 2008-2011 Dirk-Jan C. Binnema (GPLv3+)\n");
454
 
}
455
 
 
456
 
 
457
 
MuExitCode
458
 
mu_config_execute (MuConfig *opts)
459
 
{
460
 
        g_return_val_if_fail (opts, MU_EXITCODE_ERROR);
461
 
                
462
 
        if (opts->version) {
463
 
                show_version ();
464
 
                return MU_EXITCODE_OK;
465
 
        }
466
 
                
467
 
        if (!opts->params||!opts->params[0]) {/* no command? */
468
 
                show_version ();
469
 
                g_print ("\n");
470
 
                show_usage (TRUE);
471
 
                return MU_EXITCODE_ERROR;
472
 
        }
473
 
                
474
 
        switch (opts->cmd) {
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);
484
 
                show_usage (FALSE);
485
 
                return MU_EXITCODE_ERROR;
486
 
        default:
487
 
                g_return_val_if_reached (MU_EXITCODE_ERROR);
488
 
        }
489
 
}
490
 
 
491
505
guint
492
506
mu_config_param_num (MuConfig *conf)
493
507
{
494
508
        guint u;
495
 
        
 
509
 
496
510
        g_return_val_if_fail (conf, 0);
497
 
        
498
511
        for (u = 0; conf->params[u]; ++u);
499
512
 
500
513
        return u;