2009-10-17 17:26:58 +02:00
|
|
|
/* notmuch - Not much of an email program, (just index and search)
|
|
|
|
*
|
|
|
|
* Copyright © 2009 Carl Worth
|
2009-11-06 19:00:38 +01:00
|
|
|
* Copyright © 2009 Keith Packard
|
2009-10-17 17:26:58 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2016-06-02 18:26:14 +02:00
|
|
|
* along with this program. If not, see https://www.gnu.org/licenses/ .
|
2009-10-17 17:26:58 +02:00
|
|
|
*
|
2009-11-06 19:00:38 +01:00
|
|
|
* Authors: Carl Worth <cworth@cworth.org>
|
|
|
|
* Keith Packard <keithp@keithp.com>
|
2009-10-17 17:26:58 +02:00
|
|
|
*/
|
|
|
|
|
2009-11-10 21:03:05 +01:00
|
|
|
#include "notmuch-client.h"
|
2009-10-17 17:26:58 +02:00
|
|
|
|
2014-01-10 22:28:53 +01:00
|
|
|
/*
|
|
|
|
* Notmuch subcommand hook.
|
|
|
|
*
|
|
|
|
* The return value will be used as notmuch exit status code,
|
2018-01-02 16:32:38 +01:00
|
|
|
* preferably EXIT_SUCCESS or EXIT_FAILURE.
|
2014-01-10 22:28:53 +01:00
|
|
|
*/
|
2013-03-03 22:55:09 +01:00
|
|
|
typedef int (*command_function_t) (notmuch_config_t *config, int argc, char *argv[]);
|
2009-11-18 08:37:21 +01:00
|
|
|
|
|
|
|
typedef struct command {
|
|
|
|
const char *name;
|
|
|
|
command_function_t function;
|
2017-02-26 20:12:38 +01:00
|
|
|
notmuch_config_mode_t config_mode;
|
2009-11-18 08:37:21 +01:00
|
|
|
const char *summary;
|
|
|
|
} command_t;
|
|
|
|
|
2009-10-28 08:58:26 +01:00
|
|
|
static int
|
2013-03-03 22:55:09 +01:00
|
|
|
notmuch_help_command (notmuch_config_t *config, int argc, char *argv[]);
|
2009-10-28 08:58:26 +01:00
|
|
|
|
2013-03-03 22:55:06 +01:00
|
|
|
static int
|
2013-03-03 22:55:09 +01:00
|
|
|
notmuch_command (notmuch_config_t *config, int argc, char *argv[]);
|
2013-03-03 22:55:06 +01:00
|
|
|
|
2015-04-05 15:13:03 +02:00
|
|
|
static int
|
|
|
|
_help_for (const char *topic);
|
|
|
|
|
2017-10-07 10:44:04 +02:00
|
|
|
static bool print_version = false, print_help = false;
|
cli: use designated initializers for opt desc
Several changes at once, just to not have to change the same lines
several times over:
- Use designated initializers to initialize opt desc arrays.
- Only initialize the needed fields.
- Remove arg_id (short options) as unused.
- Replace opt_type and output_var with several type safe output
variables, where the output variable being non-NULL determines the
type. Introduce checks to ensure only one is set. The downside is
some waste of const space per argument; this could be saved by
retaining opt_type and using a union, but that's still pretty
verbose.
- Fix some variables due to the type safety. Mostly a good thing, but
leads to some enums being changed to ints. This is pedantically
correct, but somewhat annoying. We could also cast, but that defeats
the purpose a bit.
- Terminate the opt desc arrays using {}.
The output variable type safety and the ability to add new fields for
just some output types or arguments are the big wins. For example, if
we wanted to add a variable to set when the argument is present, we
could do so for just the arguments that need it.
Beauty is in the eye of the beholder, but I think this looks nice when
defining the arguments, and reduces some of the verbosity we have
there.
2017-10-01 22:53:11 +02:00
|
|
|
const char *notmuch_requested_db_uuid = NULL;
|
2015-04-05 15:13:03 +02:00
|
|
|
|
|
|
|
const notmuch_opt_desc_t notmuch_shared_options [] = {
|
cli: use designated initializers for opt desc
Several changes at once, just to not have to change the same lines
several times over:
- Use designated initializers to initialize opt desc arrays.
- Only initialize the needed fields.
- Remove arg_id (short options) as unused.
- Replace opt_type and output_var with several type safe output
variables, where the output variable being non-NULL determines the
type. Introduce checks to ensure only one is set. The downside is
some waste of const space per argument; this could be saved by
retaining opt_type and using a union, but that's still pretty
verbose.
- Fix some variables due to the type safety. Mostly a good thing, but
leads to some enums being changed to ints. This is pedantically
correct, but somewhat annoying. We could also cast, but that defeats
the purpose a bit.
- Terminate the opt desc arrays using {}.
The output variable type safety and the ability to add new fields for
just some output types or arguments are the big wins. For example, if
we wanted to add a variable to set when the argument is present, we
could do so for just the arguments that need it.
Beauty is in the eye of the beholder, but I think this looks nice when
defining the arguments, and reduces some of the verbosity we have
there.
2017-10-01 22:53:11 +02:00
|
|
|
{ .opt_bool = &print_version, .name = "version" },
|
|
|
|
{ .opt_bool = &print_help, .name = "help" },
|
|
|
|
{ .opt_string = ¬much_requested_db_uuid, .name = "uuid" },
|
|
|
|
{ }
|
2015-04-05 15:13:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* any subcommand wanting to support these options should call
|
|
|
|
* inherit notmuch_shared_options and call
|
|
|
|
* notmuch_process_shared_options (subcommand_name);
|
|
|
|
*/
|
|
|
|
void
|
2019-06-13 12:31:01 +02:00
|
|
|
notmuch_process_shared_options (const char *subcommand_name)
|
|
|
|
{
|
2015-04-05 15:13:03 +02:00
|
|
|
if (print_version) {
|
2019-06-13 12:31:01 +02:00
|
|
|
printf ("notmuch " STRINGIFY (NOTMUCH_VERSION) "\n");
|
2015-04-05 15:13:03 +02:00
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (print_help) {
|
|
|
|
int ret = _help_for (subcommand_name);
|
|
|
|
exit (ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-05 22:34:55 +02:00
|
|
|
/* This is suitable for subcommands that do not actually open the
|
|
|
|
* database.
|
|
|
|
*/
|
2019-06-13 12:31:01 +02:00
|
|
|
int
|
|
|
|
notmuch_minimal_options (const char *subcommand_name,
|
|
|
|
int argc, char **argv)
|
2015-04-05 22:34:55 +02:00
|
|
|
{
|
|
|
|
int opt_index;
|
|
|
|
|
|
|
|
notmuch_opt_desc_t options[] = {
|
cli: use designated initializers for opt desc
Several changes at once, just to not have to change the same lines
several times over:
- Use designated initializers to initialize opt desc arrays.
- Only initialize the needed fields.
- Remove arg_id (short options) as unused.
- Replace opt_type and output_var with several type safe output
variables, where the output variable being non-NULL determines the
type. Introduce checks to ensure only one is set. The downside is
some waste of const space per argument; this could be saved by
retaining opt_type and using a union, but that's still pretty
verbose.
- Fix some variables due to the type safety. Mostly a good thing, but
leads to some enums being changed to ints. This is pedantically
correct, but somewhat annoying. We could also cast, but that defeats
the purpose a bit.
- Terminate the opt desc arrays using {}.
The output variable type safety and the ability to add new fields for
just some output types or arguments are the big wins. For example, if
we wanted to add a variable to set when the argument is present, we
could do so for just the arguments that need it.
Beauty is in the eye of the beholder, but I think this looks nice when
defining the arguments, and reduces some of the verbosity we have
there.
2017-10-01 22:53:11 +02:00
|
|
|
{ .opt_inherit = notmuch_shared_options },
|
|
|
|
{ }
|
2015-04-05 22:34:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
opt_index = parse_arguments (argc, argv, options, 1);
|
|
|
|
|
|
|
|
if (opt_index < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* We can't use argv here as it is sometimes NULL */
|
|
|
|
notmuch_process_shared_options (subcommand_name);
|
|
|
|
return opt_index;
|
|
|
|
}
|
2015-04-05 15:13:03 +02:00
|
|
|
|
2017-10-21 04:25:44 +02:00
|
|
|
|
|
|
|
struct _notmuch_client_indexing_cli_choices indexing_cli_choices = { };
|
2019-06-13 12:31:01 +02:00
|
|
|
const notmuch_opt_desc_t notmuch_shared_indexing_options [] = {
|
2017-12-08 07:23:52 +01:00
|
|
|
{ .opt_keyword = &indexing_cli_choices.decrypt_policy,
|
|
|
|
.present = &indexing_cli_choices.decrypt_policy_set, .keywords =
|
2019-06-13 12:31:01 +02:00
|
|
|
(notmuch_keyword_t []){ { "false", NOTMUCH_DECRYPT_FALSE },
|
|
|
|
{ "true", NOTMUCH_DECRYPT_TRUE },
|
|
|
|
{ "auto", NOTMUCH_DECRYPT_AUTO },
|
|
|
|
{ "nostash", NOTMUCH_DECRYPT_NOSTASH },
|
|
|
|
{ 0, 0 } },
|
2017-12-08 07:23:50 +01:00
|
|
|
.name = "decrypt" },
|
2017-10-21 04:25:44 +02:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
notmuch_status_t
|
2019-05-02 15:19:41 +02:00
|
|
|
notmuch_process_shared_indexing_options (notmuch_database_t *notmuch)
|
2017-10-21 04:25:44 +02:00
|
|
|
{
|
|
|
|
if (indexing_cli_choices.opts == NULL)
|
|
|
|
indexing_cli_choices.opts = notmuch_database_get_default_indexopts (notmuch);
|
2017-12-08 07:23:51 +01:00
|
|
|
if (indexing_cli_choices.decrypt_policy_set) {
|
2017-10-21 04:25:44 +02:00
|
|
|
notmuch_status_t status;
|
|
|
|
if (indexing_cli_choices.opts == NULL)
|
|
|
|
return NOTMUCH_STATUS_OUT_OF_MEMORY;
|
2017-12-08 07:23:51 +01:00
|
|
|
status = notmuch_indexopts_set_decrypt_policy (indexing_cli_choices.opts, indexing_cli_choices.decrypt_policy);
|
2017-10-21 04:25:44 +02:00
|
|
|
if (status != NOTMUCH_STATUS_SUCCESS) {
|
2017-12-08 07:23:52 +01:00
|
|
|
fprintf (stderr, "Error: Failed to set index decryption policy to %d. (%s)\n",
|
|
|
|
indexing_cli_choices.decrypt_policy, notmuch_status_to_string (status));
|
2017-10-21 04:25:44 +02:00
|
|
|
notmuch_indexopts_destroy (indexing_cli_choices.opts);
|
|
|
|
indexing_cli_choices.opts = NULL;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NOTMUCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-24 21:00:27 +02:00
|
|
|
static command_t commands[] = {
|
2017-02-26 20:12:38 +01:00
|
|
|
{ NULL, notmuch_command, NOTMUCH_CONFIG_OPEN | NOTMUCH_CONFIG_CREATE,
|
2013-03-03 22:55:06 +01:00
|
|
|
"Notmuch main command." },
|
2017-02-26 20:12:38 +01:00
|
|
|
{ "setup", notmuch_setup_command, NOTMUCH_CONFIG_OPEN | NOTMUCH_CONFIG_CREATE,
|
2015-05-27 19:53:52 +02:00
|
|
|
"Interactively set up notmuch for first use." },
|
2017-02-26 20:12:38 +01:00
|
|
|
{ "new", notmuch_new_command, NOTMUCH_CONFIG_OPEN,
|
2011-12-22 02:45:59 +01:00
|
|
|
"Find and import new messages to the notmuch database." },
|
2017-02-26 20:12:38 +01:00
|
|
|
{ "insert", notmuch_insert_command, NOTMUCH_CONFIG_OPEN,
|
2013-06-23 14:20:45 +02:00
|
|
|
"Add a new message into the maildir and notmuch database." },
|
2017-02-26 20:12:38 +01:00
|
|
|
{ "search", notmuch_search_command, NOTMUCH_CONFIG_OPEN,
|
2011-12-22 02:45:59 +01:00
|
|
|
"Search for messages matching the given search terms." },
|
2017-02-26 20:12:38 +01:00
|
|
|
{ "address", notmuch_address_command, NOTMUCH_CONFIG_OPEN,
|
2014-11-05 01:25:55 +01:00
|
|
|
"Get addresses from messages matching the given search terms." },
|
2017-02-26 20:12:38 +01:00
|
|
|
{ "show", notmuch_show_command, NOTMUCH_CONFIG_OPEN,
|
2011-12-22 02:45:59 +01:00
|
|
|
"Show all messages matching the search terms." },
|
2017-02-26 20:12:38 +01:00
|
|
|
{ "count", notmuch_count_command, NOTMUCH_CONFIG_OPEN,
|
2011-12-22 02:45:59 +01:00
|
|
|
"Count messages matching the search terms." },
|
2017-02-26 20:12:38 +01:00
|
|
|
{ "reply", notmuch_reply_command, NOTMUCH_CONFIG_OPEN,
|
2011-12-22 02:45:59 +01:00
|
|
|
"Construct a reply template for a set of messages." },
|
2017-02-26 20:12:38 +01:00
|
|
|
{ "tag", notmuch_tag_command, NOTMUCH_CONFIG_OPEN,
|
2011-12-22 02:45:59 +01:00
|
|
|
"Add/remove tags for all messages matching the search terms." },
|
2017-02-26 20:12:38 +01:00
|
|
|
{ "dump", notmuch_dump_command, NOTMUCH_CONFIG_OPEN,
|
2011-12-22 02:45:59 +01:00
|
|
|
"Create a plain-text dump of the tags for each message." },
|
2017-02-26 20:12:38 +01:00
|
|
|
{ "restore", notmuch_restore_command, NOTMUCH_CONFIG_OPEN,
|
2011-12-22 02:45:59 +01:00
|
|
|
"Restore the tags from the given dump file (see 'dump')." },
|
2017-02-26 20:12:38 +01:00
|
|
|
{ "compact", notmuch_compact_command, NOTMUCH_CONFIG_OPEN,
|
2013-10-02 22:30:47 +02:00
|
|
|
"Compact the notmuch database." },
|
2017-06-04 14:32:35 +02:00
|
|
|
{ "reindex", notmuch_reindex_command, NOTMUCH_CONFIG_OPEN,
|
|
|
|
"Re-index all messages matching the search terms." },
|
2017-02-26 20:12:38 +01:00
|
|
|
{ "config", notmuch_config_command, NOTMUCH_CONFIG_OPEN,
|
2011-12-22 02:45:59 +01:00
|
|
|
"Get or set settings in the notmuch configuration file." },
|
2017-12-04 19:43:10 +01:00
|
|
|
#if WITH_EMACS
|
|
|
|
{ "emacs-mua", NULL, 0,
|
|
|
|
"send mail with notmuch and emacs." },
|
|
|
|
#endif
|
2017-02-26 20:12:38 +01:00
|
|
|
{ "help", notmuch_help_command, NOTMUCH_CONFIG_CREATE, /* create but don't save config */
|
2011-12-22 02:45:59 +01:00
|
|
|
"This message, or more detailed help for the named command." }
|
2009-10-17 17:26:58 +02:00
|
|
|
};
|
|
|
|
|
2014-03-22 14:06:58 +01:00
|
|
|
typedef struct help_topic {
|
|
|
|
const char *name;
|
|
|
|
const char *summary;
|
|
|
|
} help_topic_t;
|
|
|
|
|
|
|
|
static help_topic_t help_topics[] = {
|
|
|
|
{ "search-terms",
|
|
|
|
"Common search term syntax." },
|
|
|
|
{ "hooks",
|
|
|
|
"Hooks that will be run before or after certain commands." },
|
2017-10-26 23:28:12 +02:00
|
|
|
{ "properties",
|
|
|
|
"Message property conventions and documentation." },
|
2014-03-22 14:06:58 +01:00
|
|
|
};
|
|
|
|
|
2013-03-03 22:55:05 +01:00
|
|
|
static command_t *
|
|
|
|
find_command (const char *name)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE (commands); i++)
|
2019-06-13 12:31:01 +02:00
|
|
|
if ((! name && ! commands[i].name) ||
|
2013-03-03 22:55:06 +01:00
|
|
|
(name && commands[i].name && strcmp (name, commands[i].name) == 0))
|
2013-03-03 22:55:05 +01:00
|
|
|
return &commands[i];
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-12-16 04:17:23 +01:00
|
|
|
int notmuch_format_version;
|
|
|
|
|
2009-10-25 23:58:05 +01:00
|
|
|
static void
|
2009-11-18 02:01:16 +01:00
|
|
|
usage (FILE *out)
|
2009-10-17 17:26:58 +02:00
|
|
|
{
|
|
|
|
command_t *command;
|
2014-03-22 14:06:58 +01:00
|
|
|
help_topic_t *topic;
|
2009-10-25 23:39:53 +01:00
|
|
|
unsigned int i;
|
2009-10-17 17:26:58 +02:00
|
|
|
|
2010-04-02 20:53:22 +02:00
|
|
|
fprintf (out,
|
|
|
|
"Usage: notmuch --help\n"
|
2010-04-02 20:55:09 +02:00
|
|
|
" notmuch --version\n"
|
2010-04-02 20:53:22 +02:00
|
|
|
" notmuch <command> [args...]\n");
|
2009-11-18 02:01:16 +01:00
|
|
|
fprintf (out, "\n");
|
2010-04-02 21:12:18 +02:00
|
|
|
fprintf (out, "The available commands are as follows:\n");
|
2009-11-18 02:01:16 +01:00
|
|
|
fprintf (out, "\n");
|
2009-10-17 17:26:58 +02:00
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE (commands); i++) {
|
|
|
|
command = &commands[i];
|
|
|
|
|
2013-03-03 22:55:06 +01:00
|
|
|
if (command->name)
|
2014-03-22 14:06:58 +01:00
|
|
|
fprintf (out, " %-12s %s\n", command->name, command->summary);
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf (out, "\n");
|
|
|
|
fprintf (out, "Additional help topics are as follows:\n");
|
|
|
|
fprintf (out, "\n");
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE (help_topics); i++) {
|
|
|
|
topic = &help_topics[i];
|
|
|
|
fprintf (out, " %-12s %s\n", topic->name, topic->summary);
|
2009-10-17 17:26:58 +02:00
|
|
|
}
|
2009-10-28 08:58:26 +01:00
|
|
|
|
2010-04-02 21:12:18 +02:00
|
|
|
fprintf (out, "\n");
|
2009-11-18 02:01:16 +01:00
|
|
|
fprintf (out,
|
2014-03-22 14:06:58 +01:00
|
|
|
"Use \"notmuch help <command or topic>\" for more details "
|
|
|
|
"on each command or topic.\n\n");
|
2009-10-28 08:58:26 +01:00
|
|
|
}
|
|
|
|
|
2012-12-16 04:17:23 +01:00
|
|
|
void
|
|
|
|
notmuch_exit_if_unsupported_format (void)
|
|
|
|
{
|
|
|
|
if (notmuch_format_version > NOTMUCH_FORMAT_CUR) {
|
|
|
|
fprintf (stderr, "\
|
|
|
|
A caller requested output format version %d, but the installed notmuch\n\
|
|
|
|
CLI only supports up to format version %d. You may need to upgrade your\n\
|
|
|
|
notmuch CLI.\n",
|
|
|
|
notmuch_format_version, NOTMUCH_FORMAT_CUR);
|
|
|
|
exit (NOTMUCH_EXIT_FORMAT_TOO_NEW);
|
|
|
|
} else if (notmuch_format_version < NOTMUCH_FORMAT_MIN) {
|
|
|
|
fprintf (stderr, "\
|
|
|
|
A caller requested output format version %d, which is no longer supported\n\
|
|
|
|
by the notmuch CLI (it requires at least version %d). You may need to\n\
|
|
|
|
upgrade your notmuch front-end.\n",
|
|
|
|
notmuch_format_version, NOTMUCH_FORMAT_MIN);
|
|
|
|
exit (NOTMUCH_EXIT_FORMAT_TOO_OLD);
|
2013-10-24 17:19:02 +02:00
|
|
|
} else if (notmuch_format_version < NOTMUCH_FORMAT_MIN_ACTIVE) {
|
2012-12-16 04:17:23 +01:00
|
|
|
/* Warn about old version requests so compatibility issues are
|
|
|
|
* less likely when we drop support for a deprecated format
|
|
|
|
* versions. */
|
|
|
|
fprintf (stderr, "\
|
|
|
|
A caller requested deprecated output format version %d, which may not\n\
|
|
|
|
be supported in the future.\n", notmuch_format_version);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-06 00:39:55 +02:00
|
|
|
void
|
|
|
|
notmuch_exit_if_unmatched_db_uuid (notmuch_database_t *notmuch)
|
|
|
|
{
|
|
|
|
const char *uuid = NULL;
|
|
|
|
|
2019-06-13 12:31:01 +02:00
|
|
|
if (! notmuch_requested_db_uuid)
|
2015-04-06 00:39:55 +02:00
|
|
|
return;
|
|
|
|
IGNORE_RESULT (notmuch_database_get_revision (notmuch, &uuid));
|
|
|
|
|
2019-06-13 12:31:01 +02:00
|
|
|
if (strcmp (notmuch_requested_db_uuid, uuid) != 0) {
|
2015-04-06 00:39:55 +02:00
|
|
|
fprintf (stderr, "Error: requested database revision %s does not match %s\n",
|
|
|
|
notmuch_requested_db_uuid, uuid);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-22 02:45:59 +01:00
|
|
|
static void
|
|
|
|
exec_man (const char *page)
|
|
|
|
{
|
|
|
|
if (execlp ("man", "man", page, (char *) NULL)) {
|
|
|
|
perror ("exec man");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-28 08:58:26 +01:00
|
|
|
static int
|
2015-04-05 16:59:18 +02:00
|
|
|
_help_for (const char *topic_name)
|
2009-10-28 08:58:26 +01:00
|
|
|
{
|
|
|
|
command_t *command;
|
2014-03-22 14:06:58 +01:00
|
|
|
help_topic_t *topic;
|
|
|
|
unsigned int i;
|
2009-10-28 08:58:26 +01:00
|
|
|
|
2019-06-13 12:31:01 +02:00
|
|
|
if (! topic_name) {
|
2009-11-18 02:01:16 +01:00
|
|
|
printf ("The notmuch mail system.\n\n");
|
|
|
|
usage (stdout);
|
2014-01-10 22:28:53 +01:00
|
|
|
return EXIT_SUCCESS;
|
2009-10-28 08:58:26 +01:00
|
|
|
}
|
|
|
|
|
2015-04-05 16:59:18 +02:00
|
|
|
if (strcmp (topic_name, "help") == 0) {
|
2011-12-22 02:45:59 +01:00
|
|
|
printf ("The notmuch help system.\n\n"
|
|
|
|
"\tNotmuch uses the man command to display help. In case\n"
|
|
|
|
"\tof difficulties check that MANPATH includes the pages\n"
|
|
|
|
"\tinstalled by notmuch.\n\n"
|
|
|
|
"\tTry \"notmuch help\" for a list of topics.\n");
|
2014-01-10 22:28:53 +01:00
|
|
|
return EXIT_SUCCESS;
|
2011-12-22 02:45:59 +01:00
|
|
|
}
|
|
|
|
|
2015-04-05 16:59:18 +02:00
|
|
|
command = find_command (topic_name);
|
2013-03-03 22:55:05 +01:00
|
|
|
if (command) {
|
2015-04-05 15:24:42 +02:00
|
|
|
char *page = talloc_asprintf (NULL, "notmuch-%s", command->name);
|
2013-03-03 22:55:05 +01:00
|
|
|
exec_man (page);
|
2009-10-28 08:58:26 +01:00
|
|
|
}
|
|
|
|
|
2014-03-22 14:06:58 +01:00
|
|
|
for (i = 0; i < ARRAY_SIZE (help_topics); i++) {
|
|
|
|
topic = &help_topics[i];
|
2015-04-05 16:59:18 +02:00
|
|
|
if (strcmp (topic_name, topic->name) == 0) {
|
2015-04-05 15:24:42 +02:00
|
|
|
char *page = talloc_asprintf (NULL, "notmuch-%s", topic->name);
|
2014-03-22 14:06:58 +01:00
|
|
|
exec_man (page);
|
|
|
|
}
|
2009-11-18 08:37:21 +01:00
|
|
|
}
|
|
|
|
|
2009-10-28 08:58:26 +01:00
|
|
|
fprintf (stderr,
|
|
|
|
"\nSorry, %s is not a known command. There's not much I can do to help.\n\n",
|
2015-04-05 16:59:18 +02:00
|
|
|
topic_name);
|
2014-01-10 22:28:53 +01:00
|
|
|
return EXIT_FAILURE;
|
2009-10-17 17:26:58 +02:00
|
|
|
}
|
2009-11-12 02:01:55 +01:00
|
|
|
|
2015-04-05 16:59:18 +02:00
|
|
|
static int
|
2019-06-13 12:31:01 +02:00
|
|
|
notmuch_help_command (unused (notmuch_config_t *config), int argc, char *argv[])
|
2015-04-05 16:59:18 +02:00
|
|
|
{
|
2015-04-05 22:34:55 +02:00
|
|
|
int opt_index;
|
|
|
|
|
|
|
|
opt_index = notmuch_minimal_options ("help", argc, argv);
|
|
|
|
if (opt_index < 0)
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
/* skip at least subcommand argument */
|
2019-06-13 12:31:01 +02:00
|
|
|
argc -= opt_index;
|
|
|
|
argv += opt_index;
|
2015-04-05 16:59:18 +02:00
|
|
|
|
|
|
|
if (argc == 0) {
|
|
|
|
return _help_for (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _help_for (argv[0]);
|
|
|
|
}
|
|
|
|
|
2009-11-12 02:01:55 +01:00
|
|
|
/* Handle the case of "notmuch" being invoked with no command
|
2009-11-12 02:33:31 +01:00
|
|
|
* argument. For now we just call notmuch_setup_command, but we plan
|
|
|
|
* to be more clever about this in the future.
|
2009-11-12 02:01:55 +01:00
|
|
|
*/
|
|
|
|
static int
|
2013-03-03 22:55:09 +01:00
|
|
|
notmuch_command (notmuch_config_t *config,
|
2019-05-30 21:56:14 +02:00
|
|
|
unused(int argc), unused(char **argv))
|
2009-11-12 02:01:55 +01:00
|
|
|
{
|
2009-11-12 02:56:59 +01:00
|
|
|
char *db_path;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
/* If the user has never configured notmuch, then run
|
|
|
|
* notmuch_setup_command which will give a nice welcome message,
|
|
|
|
* and interactively guide the user through the configuration. */
|
2013-03-03 22:55:09 +01:00
|
|
|
if (notmuch_config_is_new (config))
|
|
|
|
return notmuch_setup_command (config, 0, NULL);
|
2009-11-12 02:56:59 +01:00
|
|
|
|
|
|
|
/* Notmuch is already configured, but is there a database? */
|
2013-03-03 22:55:09 +01:00
|
|
|
db_path = talloc_asprintf (config, "%s/%s",
|
2009-11-12 02:56:59 +01:00
|
|
|
notmuch_config_get_database_path (config),
|
|
|
|
".notmuch");
|
|
|
|
if (stat (db_path, &st)) {
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
fprintf (stderr, "Error looking for notmuch database at %s: %s\n",
|
|
|
|
db_path, strerror (errno));
|
2014-01-10 22:28:53 +01:00
|
|
|
return EXIT_FAILURE;
|
2009-11-12 02:56:59 +01:00
|
|
|
}
|
|
|
|
printf ("Notmuch is configured, but there's not yet a database at\n\n\t%s\n\n",
|
|
|
|
db_path);
|
|
|
|
printf ("You probably want to run \"notmuch new\" now to create that database.\n\n"
|
|
|
|
"Note that the first run of \"notmuch new\" can take a very long time\n"
|
|
|
|
"and that the resulting database will use roughly the same amount of\n"
|
|
|
|
"storage space as the email being indexed.\n\n");
|
2014-01-10 22:28:53 +01:00
|
|
|
return EXIT_SUCCESS;
|
2009-11-12 02:56:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
printf ("Notmuch is configured and appears to have a database. Excellent!\n\n"
|
|
|
|
"At this point you can start exploring the functionality of notmuch by\n"
|
|
|
|
"using commands such as:\n\n"
|
|
|
|
"\tnotmuch search tag:inbox\n\n"
|
|
|
|
"\tnotmuch search to:\"%s\"\n\n"
|
|
|
|
"\tnotmuch search from:\"%s\"\n\n"
|
|
|
|
"\tnotmuch search subject:\"my favorite things\"\n\n"
|
|
|
|
"See \"notmuch help search\" for more details.\n\n"
|
|
|
|
"You can also use \"notmuch show\" with any of the thread IDs resulting\n"
|
|
|
|
"from a search. Finally, you may want to explore using a more sophisticated\n"
|
|
|
|
"interface to notmuch such as the emacs interface implemented in notmuch.el\n"
|
2016-06-02 18:26:14 +02:00
|
|
|
"or any other interface described at https://notmuchmail.org\n\n"
|
2009-11-12 04:50:15 +01:00
|
|
|
"And don't forget to run \"notmuch new\" whenever new mail arrives.\n\n"
|
2009-11-12 02:56:59 +01:00
|
|
|
"Have fun, and may your inbox never have much mail.\n\n",
|
|
|
|
notmuch_config_get_user_name (config),
|
|
|
|
notmuch_config_get_user_primary_email (config));
|
|
|
|
|
2014-01-10 22:28:53 +01:00
|
|
|
return EXIT_SUCCESS;
|
2009-11-12 02:01:55 +01:00
|
|
|
}
|
|
|
|
|
2016-10-22 14:50:05 +02:00
|
|
|
/*
|
|
|
|
* Try to run subcommand in argv[0] as notmuch- prefixed external
|
|
|
|
* command. argv must be NULL terminated (argv passed to main always
|
|
|
|
* is).
|
|
|
|
*
|
|
|
|
* Does not return if the external command is found and
|
2017-10-07 10:44:04 +02:00
|
|
|
* executed. Return true if external command is not found. Return
|
|
|
|
* false on errors.
|
2016-10-22 14:50:05 +02:00
|
|
|
*/
|
2019-06-13 12:31:01 +02:00
|
|
|
static bool
|
|
|
|
try_external_command (char *argv[])
|
2016-10-22 14:50:05 +02:00
|
|
|
{
|
|
|
|
char *old_argv0 = argv[0];
|
2017-10-07 10:44:04 +02:00
|
|
|
bool ret = true;
|
2016-10-22 14:50:05 +02:00
|
|
|
|
|
|
|
argv[0] = talloc_asprintf (NULL, "notmuch-%s", old_argv0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This will only return on errors. Not finding an external
|
|
|
|
* command (ENOENT) is not an error from our perspective.
|
|
|
|
*/
|
|
|
|
execvp (argv[0], argv);
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
fprintf (stderr, "Error: Running external command '%s' failed: %s\n",
|
2019-06-13 12:31:01 +02:00
|
|
|
argv[0], strerror (errno));
|
2017-10-07 10:44:04 +02:00
|
|
|
ret = false;
|
2016-10-22 14:50:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
talloc_free (argv[0]);
|
|
|
|
argv[0] = old_argv0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-10-17 17:26:58 +02:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
2009-11-12 02:01:55 +01:00
|
|
|
void *local;
|
2013-03-03 22:55:05 +01:00
|
|
|
char *talloc_report;
|
2013-03-03 22:55:06 +01:00
|
|
|
const char *command_name = NULL;
|
2009-10-17 17:26:58 +02:00
|
|
|
command_t *command;
|
cli: use designated initializers for opt desc
Several changes at once, just to not have to change the same lines
several times over:
- Use designated initializers to initialize opt desc arrays.
- Only initialize the needed fields.
- Remove arg_id (short options) as unused.
- Replace opt_type and output_var with several type safe output
variables, where the output variable being non-NULL determines the
type. Introduce checks to ensure only one is set. The downside is
some waste of const space per argument; this could be saved by
retaining opt_type and using a union, but that's still pretty
verbose.
- Fix some variables due to the type safety. Mostly a good thing, but
leads to some enums being changed to ints. This is pedantically
correct, but somewhat annoying. We could also cast, but that defeats
the purpose a bit.
- Terminate the opt desc arrays using {}.
The output variable type safety and the ability to add new fields for
just some output types or arguments are the big wins. For example, if
we wanted to add a variable to set when the argument is present, we
could do so for just the arguments that need it.
Beauty is in the eye of the beholder, but I think this looks nice when
defining the arguments, and reduces some of the verbosity we have
there.
2017-10-01 22:53:11 +02:00
|
|
|
const char *config_file_name = NULL;
|
2014-01-10 22:28:54 +01:00
|
|
|
notmuch_config_t *config = NULL;
|
2013-01-19 19:25:54 +01:00
|
|
|
int opt_index;
|
2014-01-10 22:28:53 +01:00
|
|
|
int ret;
|
2013-01-19 19:25:54 +01:00
|
|
|
|
|
|
|
notmuch_opt_desc_t options[] = {
|
cli: use designated initializers for opt desc
Several changes at once, just to not have to change the same lines
several times over:
- Use designated initializers to initialize opt desc arrays.
- Only initialize the needed fields.
- Remove arg_id (short options) as unused.
- Replace opt_type and output_var with several type safe output
variables, where the output variable being non-NULL determines the
type. Introduce checks to ensure only one is set. The downside is
some waste of const space per argument; this could be saved by
retaining opt_type and using a union, but that's still pretty
verbose.
- Fix some variables due to the type safety. Mostly a good thing, but
leads to some enums being changed to ints. This is pedantically
correct, but somewhat annoying. We could also cast, but that defeats
the purpose a bit.
- Terminate the opt desc arrays using {}.
The output variable type safety and the ability to add new fields for
just some output types or arguments are the big wins. For example, if
we wanted to add a variable to set when the argument is present, we
could do so for just the arguments that need it.
Beauty is in the eye of the beholder, but I think this looks nice when
defining the arguments, and reduces some of the verbosity we have
there.
2017-10-01 22:53:11 +02:00
|
|
|
{ .opt_string = &config_file_name, .name = "config" },
|
|
|
|
{ .opt_inherit = notmuch_shared_options },
|
|
|
|
{ }
|
2013-01-19 19:25:54 +01:00
|
|
|
};
|
2009-10-17 17:26:58 +02:00
|
|
|
|
2011-01-25 14:40:35 +01:00
|
|
|
talloc_enable_null_tracking ();
|
|
|
|
|
2009-11-12 02:01:55 +01:00
|
|
|
local = talloc_new (NULL);
|
|
|
|
|
2019-05-02 15:19:42 +02:00
|
|
|
g_mime_init ();
|
2019-06-13 12:31:01 +02:00
|
|
|
#if ! GLIB_CHECK_VERSION (2, 35, 1)
|
2011-07-26 01:38:58 +02:00
|
|
|
g_type_init ();
|
2013-06-07 20:41:13 +02:00
|
|
|
#endif
|
2009-11-11 19:54:12 +01:00
|
|
|
|
2012-12-16 04:17:23 +01:00
|
|
|
/* Globally default to the current output format version. */
|
|
|
|
notmuch_format_version = NOTMUCH_FORMAT_CUR;
|
|
|
|
|
2013-01-19 19:25:54 +01:00
|
|
|
opt_index = parse_arguments (argc, argv, options, 1);
|
|
|
|
if (opt_index < 0) {
|
2014-01-10 22:28:53 +01:00
|
|
|
ret = EXIT_FAILURE;
|
|
|
|
goto DONE;
|
2013-01-19 19:25:54 +01:00
|
|
|
}
|
|
|
|
|
2013-03-03 22:55:06 +01:00
|
|
|
if (opt_index < argc)
|
|
|
|
command_name = argv[opt_index];
|
|
|
|
|
2015-06-15 17:46:44 +02:00
|
|
|
notmuch_process_shared_options (command_name);
|
|
|
|
|
2013-03-03 22:55:06 +01:00
|
|
|
command = find_command (command_name);
|
2017-12-04 19:43:10 +01:00
|
|
|
/* if command->function is NULL, try external command */
|
2019-06-13 12:31:01 +02:00
|
|
|
if (! command || ! command->function) {
|
2016-10-22 14:50:05 +02:00
|
|
|
/* This won't return if the external command is found. */
|
2019-06-13 12:31:01 +02:00
|
|
|
if (try_external_command (argv + opt_index))
|
2016-10-22 14:50:05 +02:00
|
|
|
fprintf (stderr, "Error: Unknown command '%s' (see \"notmuch help\")\n",
|
|
|
|
command_name);
|
2014-01-10 22:28:53 +01:00
|
|
|
ret = EXIT_FAILURE;
|
|
|
|
goto DONE;
|
2013-03-03 22:55:05 +01:00
|
|
|
}
|
2012-12-17 04:08:49 +01:00
|
|
|
|
2017-02-26 20:12:38 +01:00
|
|
|
config = notmuch_config_open (local, config_file_name, command->config_mode);
|
2019-06-13 12:31:01 +02:00
|
|
|
if (! config) {
|
2014-01-10 22:28:53 +01:00
|
|
|
ret = EXIT_FAILURE;
|
|
|
|
goto DONE;
|
|
|
|
}
|
2013-03-03 22:55:09 +01:00
|
|
|
|
|
|
|
ret = (command->function)(config, argc - opt_index, argv + opt_index);
|
|
|
|
|
2014-01-10 22:28:54 +01:00
|
|
|
DONE:
|
|
|
|
if (config)
|
|
|
|
notmuch_config_close (config);
|
2013-03-03 22:55:05 +01:00
|
|
|
|
|
|
|
talloc_report = getenv ("NOTMUCH_TALLOC_REPORT");
|
|
|
|
if (talloc_report && strcmp (talloc_report, "") != 0) {
|
|
|
|
/* this relies on the previous call to
|
|
|
|
* talloc_enable_null_tracking
|
|
|
|
*/
|
|
|
|
|
|
|
|
FILE *report = fopen (talloc_report, "w");
|
|
|
|
if (report) {
|
|
|
|
talloc_report_full (NULL, report);
|
|
|
|
} else {
|
|
|
|
ret = 1;
|
|
|
|
fprintf (stderr, "ERROR: unable to write talloc log. ");
|
|
|
|
perror (talloc_report);
|
2012-12-17 04:08:49 +01:00
|
|
|
}
|
2009-10-17 17:26:58 +02:00
|
|
|
}
|
|
|
|
|
2009-11-01 00:40:47 +01:00
|
|
|
talloc_free (local);
|
|
|
|
|
2013-03-03 22:55:05 +01:00
|
|
|
return ret;
|
2009-10-17 17:26:58 +02:00
|
|
|
}
|