cli: move config open/close to main() from subcommands

This allows specifying config file as a top level argument to notmuch,
and generally makes it possible to override config file options in
main(), without having to touch the subcommands.

If the config file does not exist, one will be created for the notmuch
main command and setup and help subcommands. Help is special in this
regard; the config is created just to avoid errors about missing
config, but it will not be saved.

This also makes notmuch config the talloc context for subcommands.
This commit is contained in:
Jani Nikula 2013-03-03 23:55:09 +02:00 committed by David Bremner
parent e76f6517de
commit 4ef2106792
12 changed files with 91 additions and 163 deletions

View file

@ -150,6 +150,8 @@ chomp_newline (char *str)
*/ */
extern int notmuch_format_version; extern int notmuch_format_version;
typedef struct _notmuch_config notmuch_config_t;
/* Commands that support structured output should support the /* Commands that support structured output should support the
* following argument * following argument
* { NOTMUCH_OPT_INT, &notmuch_format_version, "format-version", 0, 0 } * { NOTMUCH_OPT_INT, &notmuch_format_version, "format-version", 0, 0 }
@ -169,40 +171,34 @@ int
notmuch_crypto_cleanup (notmuch_crypto_t *crypto); notmuch_crypto_cleanup (notmuch_crypto_t *crypto);
int int
notmuch_count_command (void *ctx, int argc, char *argv[]); notmuch_count_command (notmuch_config_t *config, int argc, char *argv[]);
int int
notmuch_dump_command (void *ctx, int argc, char *argv[]); notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[]);
int int
notmuch_new_command (void *ctx, int argc, char *argv[]); notmuch_new_command (notmuch_config_t *config, int argc, char *argv[]);
int int
notmuch_reply_command (void *ctx, int argc, char *argv[]); notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[]);
int int
notmuch_restore_command (void *ctx, int argc, char *argv[]); notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[]);
int int
notmuch_search_command (void *ctx, int argc, char *argv[]); notmuch_search_command (notmuch_config_t *config, int argc, char *argv[]);
int int
notmuch_setup_command (void *ctx, int argc, char *argv[]); notmuch_setup_command (notmuch_config_t *config, int argc, char *argv[]);
int int
notmuch_show_command (void *ctx, int argc, char *argv[]); notmuch_show_command (notmuch_config_t *config, int argc, char *argv[]);
int int
notmuch_tag_command (void *ctx, int argc, char *argv[]); notmuch_tag_command (notmuch_config_t *config, int argc, char *argv[]);
int int
notmuch_search_tags_command (void *ctx, int argc, char *argv[]); notmuch_config_command (notmuch_config_t *config, int argc, char *argv[]);
int
notmuch_cat_command (void *ctx, int argc, char *argv[]);
int
notmuch_config_command (void *ctx, int argc, char *argv[]);
const char * const char *
notmuch_time_relative_date (const void *ctx, time_t then); notmuch_time_relative_date (const void *ctx, time_t then);
@ -243,8 +239,6 @@ json_quote_str (const void *ctx, const char *str);
/* notmuch-config.c */ /* notmuch-config.c */
typedef struct _notmuch_config notmuch_config_t;
notmuch_config_t * notmuch_config_t *
notmuch_config_open (void *ctx, notmuch_config_open (void *ctx,
const char *filename, const char *filename,

View file

@ -705,14 +705,8 @@ _item_split (char *item, char **group, char **key)
} }
static int static int
notmuch_config_command_get (void *ctx, char *item) notmuch_config_command_get (notmuch_config_t *config, char *item)
{ {
notmuch_config_t *config;
config = notmuch_config_open (ctx, NULL, FALSE);
if (config == NULL)
return 1;
if (strcmp(item, "database.path") == 0) { if (strcmp(item, "database.path") == 0) {
printf ("%s\n", notmuch_config_get_database_path (config)); printf ("%s\n", notmuch_config_get_database_path (config));
} else if (strcmp(item, "user.name") == 0) { } else if (strcmp(item, "user.name") == 0) {
@ -756,25 +750,17 @@ notmuch_config_command_get (void *ctx, char *item)
g_strfreev (value); g_strfreev (value);
} }
notmuch_config_close (config);
return 0; return 0;
} }
static int static int
notmuch_config_command_set (void *ctx, char *item, int argc, char *argv[]) notmuch_config_command_set (notmuch_config_t *config, char *item, int argc, char *argv[])
{ {
notmuch_config_t *config;
char *group, *key; char *group, *key;
int ret;
if (_item_split (item, &group, &key)) if (_item_split (item, &group, &key))
return 1; return 1;
config = notmuch_config_open (ctx, NULL, FALSE);
if (config == NULL)
return 1;
/* With only the name of an item, we clear it from the /* With only the name of an item, we clear it from the
* configuration file. * configuration file.
* *
@ -795,23 +781,15 @@ notmuch_config_command_set (void *ctx, char *item, int argc, char *argv[])
break; break;
} }
ret = notmuch_config_save (config); return notmuch_config_save (config);
notmuch_config_close (config);
return ret;
} }
static int static int
notmuch_config_command_list (void *ctx) notmuch_config_command_list (notmuch_config_t *config)
{ {
notmuch_config_t *config;
char **groups; char **groups;
size_t g, groups_length; size_t g, groups_length;
config = notmuch_config_open (ctx, NULL, FALSE);
if (config == NULL)
return 1;
groups = g_key_file_get_groups (config->key_file, &groups_length); groups = g_key_file_get_groups (config->key_file, &groups_length);
if (groups == NULL) if (groups == NULL)
return 1; return 1;
@ -841,13 +819,11 @@ notmuch_config_command_list (void *ctx)
g_strfreev (groups); g_strfreev (groups);
notmuch_config_close (config);
return 0; return 0;
} }
int int
notmuch_config_command (void *ctx, int argc, char *argv[]) notmuch_config_command (notmuch_config_t *config, int argc, char *argv[])
{ {
argc--; argv++; /* skip subcommand argument */ argc--; argv++; /* skip subcommand argument */
@ -862,16 +838,16 @@ notmuch_config_command (void *ctx, int argc, char *argv[])
"one argument.\n"); "one argument.\n");
return 1; return 1;
} }
return notmuch_config_command_get (ctx, argv[1]); return notmuch_config_command_get (config, argv[1]);
} else if (strcmp (argv[0], "set") == 0) { } else if (strcmp (argv[0], "set") == 0) {
if (argc < 2) { if (argc < 2) {
fprintf (stderr, "Error: notmuch config set requires at least " fprintf (stderr, "Error: notmuch config set requires at least "
"one argument.\n"); "one argument.\n");
return 1; return 1;
} }
return notmuch_config_command_set (ctx, argv[1], argc - 2, argv + 2); return notmuch_config_command_set (config, argv[1], argc - 2, argv + 2);
} else if (strcmp (argv[0], "list") == 0) { } else if (strcmp (argv[0], "list") == 0) {
return notmuch_config_command_list (ctx); return notmuch_config_command_list (config);
} }
fprintf (stderr, "Unrecognized argument for notmuch config: %s\n", fprintf (stderr, "Unrecognized argument for notmuch config: %s\n",

View file

@ -33,9 +33,8 @@ enum {
}; };
int int
notmuch_count_command (void *ctx, int argc, char *argv[]) notmuch_count_command (notmuch_config_t *config, int argc, char *argv[])
{ {
notmuch_config_t *config;
notmuch_database_t *notmuch; notmuch_database_t *notmuch;
notmuch_query_t *query; notmuch_query_t *query;
char *query_str; char *query_str;
@ -62,22 +61,18 @@ notmuch_count_command (void *ctx, int argc, char *argv[])
return 1; return 1;
} }
config = notmuch_config_open (ctx, NULL, FALSE);
if (config == NULL)
return 1;
if (notmuch_database_open (notmuch_config_get_database_path (config), if (notmuch_database_open (notmuch_config_get_database_path (config),
NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch)) NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
return 1; return 1;
query_str = query_string_from_args (ctx, argc-opt_index, argv+opt_index); query_str = query_string_from_args (config, argc-opt_index, argv+opt_index);
if (query_str == NULL) { if (query_str == NULL) {
fprintf (stderr, "Out of memory.\n"); fprintf (stderr, "Out of memory.\n");
return 1; return 1;
} }
if (*query_str == '\0') { if (*query_str == '\0') {
query_str = talloc_strdup (ctx, ""); query_str = talloc_strdup (config, "");
} }
query = notmuch_query_create (notmuch, query_str); query = notmuch_query_create (notmuch, query_str);

View file

@ -23,9 +23,8 @@
#include "string-util.h" #include "string-util.h"
int int
notmuch_dump_command (unused (void *ctx), int argc, char *argv[]) notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])
{ {
notmuch_config_t *config;
notmuch_database_t *notmuch; notmuch_database_t *notmuch;
notmuch_query_t *query; notmuch_query_t *query;
FILE *output = stdout; FILE *output = stdout;
@ -34,10 +33,6 @@ notmuch_dump_command (unused (void *ctx), int argc, char *argv[])
notmuch_tags_t *tags; notmuch_tags_t *tags;
const char *query_str = ""; const char *query_str = "";
config = notmuch_config_open (ctx, NULL, FALSE);
if (config == NULL)
return 1;
if (notmuch_database_open (notmuch_config_get_database_path (config), if (notmuch_database_open (notmuch_config_get_database_path (config),
NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch)) NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
return 1; return 1;

View file

@ -840,9 +840,8 @@ _remove_directory (void *ctx,
} }
int int
notmuch_new_command (void *ctx, int argc, char *argv[]) notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])
{ {
notmuch_config_t *config;
notmuch_database_t *notmuch; notmuch_database_t *notmuch;
add_files_state_t add_files_state; add_files_state_t add_files_state;
double elapsed; double elapsed;
@ -875,10 +874,6 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
return 1; return 1;
} }
config = notmuch_config_open (ctx, NULL, FALSE);
if (config == NULL)
return 1;
add_files_state.new_tags = notmuch_config_get_new_tags (config, &add_files_state.new_tags_length); add_files_state.new_tags = notmuch_config_get_new_tags (config, &add_files_state.new_tags_length);
add_files_state.new_ignore = notmuch_config_get_new_ignore (config, &add_files_state.new_ignore_length); add_files_state.new_ignore = notmuch_config_get_new_ignore (config, &add_files_state.new_ignore_length);
add_files_state.synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config); add_files_state.synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
@ -890,7 +885,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
return ret; return ret;
} }
dot_notmuch_path = talloc_asprintf (ctx, "%s/%s", db_path, ".notmuch"); dot_notmuch_path = talloc_asprintf (config, "%s/%s", db_path, ".notmuch");
if (stat (dot_notmuch_path, &st)) { if (stat (dot_notmuch_path, &st)) {
int count; int count;
@ -941,9 +936,9 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
add_files_state.removed_messages = add_files_state.renamed_messages = 0; add_files_state.removed_messages = add_files_state.renamed_messages = 0;
gettimeofday (&add_files_state.tv_start, NULL); gettimeofday (&add_files_state.tv_start, NULL);
add_files_state.removed_files = _filename_list_create (ctx); add_files_state.removed_files = _filename_list_create (config);
add_files_state.removed_directories = _filename_list_create (ctx); add_files_state.removed_directories = _filename_list_create (config);
add_files_state.directory_mtimes = _filename_list_create (ctx); add_files_state.directory_mtimes = _filename_list_create (config);
if (! debugger_is_active () && add_files_state.output_is_a_tty if (! debugger_is_active () && add_files_state.output_is_a_tty
&& ! add_files_state.verbose) { && ! add_files_state.verbose) {
@ -970,7 +965,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
gettimeofday (&tv_start, NULL); gettimeofday (&tv_start, NULL);
for (f = add_files_state.removed_directories->head, i = 0; f && !interrupted; f = f->next, i++) { for (f = add_files_state.removed_directories->head, i = 0; f && !interrupted; f = f->next, i++) {
ret = _remove_directory (ctx, notmuch, f->filename, &add_files_state); ret = _remove_directory (config, notmuch, f->filename, &add_files_state);
if (ret) if (ret)
goto DONE; goto DONE;
if (do_print_progress) { if (do_print_progress) {

View file

@ -702,9 +702,8 @@ enum {
}; };
int int
notmuch_reply_command (void *ctx, int argc, char *argv[]) notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])
{ {
notmuch_config_t *config;
notmuch_database_t *notmuch; notmuch_database_t *notmuch;
notmuch_query_t *query; notmuch_query_t *query;
char *query_string; char *query_string;
@ -752,21 +751,17 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])
reply_format_func = notmuch_reply_format_headers_only; reply_format_func = notmuch_reply_format_headers_only;
} else if (format == FORMAT_JSON) { } else if (format == FORMAT_JSON) {
reply_format_func = notmuch_reply_format_sprinter; reply_format_func = notmuch_reply_format_sprinter;
sp = sprinter_json_create (ctx, stdout); sp = sprinter_json_create (config, stdout);
} else if (format == FORMAT_SEXP) { } else if (format == FORMAT_SEXP) {
reply_format_func = notmuch_reply_format_sprinter; reply_format_func = notmuch_reply_format_sprinter;
sp = sprinter_sexp_create (ctx, stdout); sp = sprinter_sexp_create (config, stdout);
} else { } else {
reply_format_func = notmuch_reply_format_default; reply_format_func = notmuch_reply_format_default;
} }
notmuch_exit_if_unsupported_format (); notmuch_exit_if_unsupported_format ();
config = notmuch_config_open (ctx, NULL, FALSE); query_string = query_string_from_args (config, argc-opt_index, argv+opt_index);
if (config == NULL)
return 1;
query_string = query_string_from_args (ctx, argc-opt_index, argv+opt_index);
if (query_string == NULL) { if (query_string == NULL) {
fprintf (stderr, "Out of memory\n"); fprintf (stderr, "Out of memory\n");
return 1; return 1;
@ -787,7 +782,7 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])
return 1; return 1;
} }
if (reply_format_func (ctx, config, query, &params, reply_all, sp) != 0) if (reply_format_func (config, config, query, &params, reply_all, sp) != 0)
return 1; return 1;
notmuch_crypto_cleanup (&params.crypto); notmuch_crypto_cleanup (&params.crypto);

View file

@ -120,9 +120,8 @@ parse_sup_line (void *ctx, char *line,
} }
int int
notmuch_restore_command (unused (void *ctx), int argc, char *argv[]) notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])
{ {
notmuch_config_t *config;
notmuch_database_t *notmuch; notmuch_database_t *notmuch;
notmuch_bool_t accumulate = FALSE; notmuch_bool_t accumulate = FALSE;
tag_op_flag_t flags = 0; tag_op_flag_t flags = 0;
@ -139,10 +138,6 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
int opt_index; int opt_index;
int input_format = DUMP_FORMAT_AUTO; int input_format = DUMP_FORMAT_AUTO;
config = notmuch_config_open (ctx, NULL, FALSE);
if (config == NULL)
return 1;
if (notmuch_database_open (notmuch_config_get_database_path (config), if (notmuch_database_open (notmuch_config_get_database_path (config),
NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch)) NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
return 1; return 1;
@ -187,7 +182,7 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
return 1; return 1;
} }
tag_ops = tag_op_list_create (ctx); tag_ops = tag_op_list_create (config);
if (tag_ops == NULL) { if (tag_ops == NULL) {
fprintf (stderr, "Out of memory.\n"); fprintf (stderr, "Out of memory.\n");
return 1; return 1;
@ -226,7 +221,7 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
if (line_ctx != NULL) if (line_ctx != NULL)
talloc_free (line_ctx); talloc_free (line_ctx);
line_ctx = talloc_new (ctx); line_ctx = talloc_new (config);
if (input_format == DUMP_FORMAT_SUP) { if (input_format == DUMP_FORMAT_SUP) {
ret = parse_sup_line (line_ctx, line, &query_string, tag_ops); ret = parse_sup_line (line_ctx, line, &query_string, tag_ops);
} else { } else {

View file

@ -290,9 +290,8 @@ enum {
}; };
int int
notmuch_search_command (void *ctx, int argc, char *argv[]) notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
{ {
notmuch_config_t *config;
notmuch_database_t *notmuch; notmuch_database_t *notmuch;
notmuch_query_t *query; notmuch_query_t *query;
char *query_str; char *query_str;
@ -349,20 +348,20 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
switch (format_sel) { switch (format_sel) {
case NOTMUCH_FORMAT_TEXT: case NOTMUCH_FORMAT_TEXT:
format = sprinter_text_create (ctx, stdout); format = sprinter_text_create (config, stdout);
break; break;
case NOTMUCH_FORMAT_TEXT0: case NOTMUCH_FORMAT_TEXT0:
if (output == OUTPUT_SUMMARY) { if (output == OUTPUT_SUMMARY) {
fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n"); fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
return 1; return 1;
} }
format = sprinter_text0_create (ctx, stdout); format = sprinter_text0_create (config, stdout);
break; break;
case NOTMUCH_FORMAT_JSON: case NOTMUCH_FORMAT_JSON:
format = sprinter_json_create (ctx, stdout); format = sprinter_json_create (config, stdout);
break; break;
case NOTMUCH_FORMAT_SEXP: case NOTMUCH_FORMAT_SEXP:
format = sprinter_sexp_create (ctx, stdout); format = sprinter_sexp_create (config, stdout);
break; break;
default: default:
/* this should never happen */ /* this should never happen */
@ -371,10 +370,6 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
notmuch_exit_if_unsupported_format (); notmuch_exit_if_unsupported_format ();
config = notmuch_config_open (ctx, NULL, FALSE);
if (config == NULL)
return 1;
if (notmuch_database_open (notmuch_config_get_database_path (config), if (notmuch_database_open (notmuch_config_get_database_path (config),
NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch)) NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
return 1; return 1;

View file

@ -120,12 +120,11 @@ parse_tag_list (void *ctx, char *response)
} }
int int
notmuch_setup_command (unused (void *ctx), notmuch_setup_command (notmuch_config_t *config,
unused (int argc), unused (char *argv[])) unused (int argc), unused (char *argv[]))
{ {
char *response = NULL; char *response = NULL;
size_t response_size = 0; size_t response_size = 0;
notmuch_config_t *config;
const char **old_other_emails; const char **old_other_emails;
size_t old_other_emails_len; size_t old_other_emails_len;
GPtrArray *other_emails; GPtrArray *other_emails;
@ -146,8 +145,6 @@ notmuch_setup_command (unused (void *ctx),
chomp_newline (response); \ chomp_newline (response); \
} while (0) } while (0)
config = notmuch_config_open (ctx, NULL, TRUE);
if (notmuch_config_is_new (config)) if (notmuch_config_is_new (config))
welcome_message_pre_setup (); welcome_message_pre_setup ();
@ -167,16 +164,16 @@ notmuch_setup_command (unused (void *ctx),
for (i = 0; i < old_other_emails_len; i++) { for (i = 0; i < old_other_emails_len; i++) {
prompt ("Additional email address [%s]: ", old_other_emails[i]); prompt ("Additional email address [%s]: ", old_other_emails[i]);
if (strlen (response)) if (strlen (response))
g_ptr_array_add (other_emails, talloc_strdup (ctx, response)); g_ptr_array_add (other_emails, talloc_strdup (config, response));
else else
g_ptr_array_add (other_emails, talloc_strdup (ctx, g_ptr_array_add (other_emails, talloc_strdup (config,
old_other_emails[i])); old_other_emails[i]));
} }
do { do {
prompt ("Additional email address [Press 'Enter' if none]: "); prompt ("Additional email address [Press 'Enter' if none]: ");
if (strlen (response)) if (strlen (response))
g_ptr_array_add (other_emails, talloc_strdup (ctx, response)); g_ptr_array_add (other_emails, talloc_strdup (config, response));
} while (strlen (response)); } while (strlen (response));
if (other_emails->len) if (other_emails->len)
notmuch_config_set_user_other_email (config, notmuch_config_set_user_other_email (config,
@ -190,7 +187,7 @@ notmuch_setup_command (unused (void *ctx),
if (strlen (response)) { if (strlen (response)) {
const char *absolute_path; const char *absolute_path;
absolute_path = make_path_absolute (ctx, response); absolute_path = make_path_absolute (config, response);
notmuch_config_set_database_path (config, absolute_path); notmuch_config_set_database_path (config, absolute_path);
} }
@ -201,7 +198,7 @@ notmuch_setup_command (unused (void *ctx),
prompt ("]: "); prompt ("]: ");
if (strlen (response)) { if (strlen (response)) {
GPtrArray *tags = parse_tag_list (ctx, response); GPtrArray *tags = parse_tag_list (config, response);
notmuch_config_set_new_tags (config, (const char **) tags->pdata, notmuch_config_set_new_tags (config, (const char **) tags->pdata,
tags->len); tags->len);
@ -217,7 +214,7 @@ notmuch_setup_command (unused (void *ctx),
prompt ("]: "); prompt ("]: ");
if (strlen (response)) { if (strlen (response)) {
GPtrArray *tags = parse_tag_list (ctx, response); GPtrArray *tags = parse_tag_list (config, response);
notmuch_config_set_search_exclude_tags (config, notmuch_config_set_search_exclude_tags (config,
(const char **) tags->pdata, (const char **) tags->pdata,

View file

@ -1056,9 +1056,8 @@ enum {
}; };
int int
notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[])) notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])
{ {
notmuch_config_t *config;
notmuch_database_t *notmuch; notmuch_database_t *notmuch;
notmuch_query_t *query; notmuch_query_t *query;
char *query_string; char *query_string;
@ -1176,11 +1175,7 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
else else
params.entire_thread = FALSE; params.entire_thread = FALSE;
config = notmuch_config_open (ctx, NULL, FALSE); query_string = query_string_from_args (config, argc-opt_index, argv+opt_index);
if (config == NULL)
return 1;
query_string = query_string_from_args (ctx, argc-opt_index, argv+opt_index);
if (query_string == NULL) { if (query_string == NULL) {
fprintf (stderr, "Out of memory\n"); fprintf (stderr, "Out of memory\n");
return 1; return 1;
@ -1202,11 +1197,11 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
} }
/* Create structure printer. */ /* Create structure printer. */
sprinter = format->new_sprinter(ctx, stdout); sprinter = format->new_sprinter(config, stdout);
/* If a single message is requested we do not use search_excludes. */ /* If a single message is requested we do not use search_excludes. */
if (params.part >= 0) if (params.part >= 0)
ret = do_show_single (ctx, query, format, sprinter, &params); ret = do_show_single (config, query, format, sprinter, &params);
else { else {
/* We always apply set the exclude flag. The /* We always apply set the exclude flag. The
* exclude=true|false option controls whether or not we return * exclude=true|false option controls whether or not we return
@ -1225,7 +1220,7 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
params.omit_excluded = FALSE; params.omit_excluded = FALSE;
} }
ret = do_show (ctx, query, format, sprinter, &params); ret = do_show (config, query, format, sprinter, &params);
} }
notmuch_crypto_cleanup (&params.crypto); notmuch_crypto_cleanup (&params.crypto);

View file

@ -178,11 +178,10 @@ tag_file (void *ctx, notmuch_database_t *notmuch, tag_op_flag_t flags,
} }
int int
notmuch_tag_command (void *ctx, int argc, char *argv[]) notmuch_tag_command (notmuch_config_t *config, int argc, char *argv[])
{ {
tag_op_list_t *tag_ops = NULL; tag_op_list_t *tag_ops = NULL;
char *query_string = NULL; char *query_string = NULL;
notmuch_config_t *config;
notmuch_database_t *notmuch; notmuch_database_t *notmuch;
struct sigaction action; struct sigaction action;
tag_op_flag_t tag_flags = TAG_FLAG_NONE; tag_op_flag_t tag_flags = TAG_FLAG_NONE;
@ -225,21 +224,17 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])
return 1; return 1;
} }
} else { } else {
tag_ops = tag_op_list_create (ctx); tag_ops = tag_op_list_create (config);
if (tag_ops == NULL) { if (tag_ops == NULL) {
fprintf (stderr, "Out of memory.\n"); fprintf (stderr, "Out of memory.\n");
return 1; return 1;
} }
if (parse_tag_command_line (ctx, argc - opt_index, argv + opt_index, if (parse_tag_command_line (config, argc - opt_index, argv + opt_index,
&query_string, tag_ops)) &query_string, tag_ops))
return 1; return 1;
} }
config = notmuch_config_open (ctx, NULL, FALSE);
if (config == NULL)
return 1;
if (notmuch_database_open (notmuch_config_get_database_path (config), if (notmuch_database_open (notmuch_config_get_database_path (config),
NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch)) NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
return 1; return 1;
@ -248,9 +243,9 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])
tag_flags |= TAG_FLAG_MAILDIR_SYNC; tag_flags |= TAG_FLAG_MAILDIR_SYNC;
if (batch) if (batch)
ret = tag_file (ctx, notmuch, tag_flags, input); ret = tag_file (config, notmuch, tag_flags, input);
else else
ret = tag_query (ctx, notmuch, query_string, tag_ops, tag_flags); ret = tag_query (config, notmuch, query_string, tag_ops, tag_flags);
notmuch_database_destroy (notmuch); notmuch_database_destroy (notmuch);

View file

@ -22,56 +22,57 @@
#include "notmuch-client.h" #include "notmuch-client.h"
typedef int (*command_function_t) (void *ctx, int argc, char *argv[]); typedef int (*command_function_t) (notmuch_config_t *config, int argc, char *argv[]);
typedef struct command { typedef struct command {
const char *name; const char *name;
command_function_t function; command_function_t function;
notmuch_bool_t create_config;
const char *arguments; const char *arguments;
const char *summary; const char *summary;
} command_t; } command_t;
static int static int
notmuch_help_command (void *ctx, int argc, char *argv[]); notmuch_help_command (notmuch_config_t *config, int argc, char *argv[]);
static int static int
notmuch_command (void *ctx, int argc, char *argv[]); notmuch_command (notmuch_config_t *config, int argc, char *argv[]);
static command_t commands[] = { static command_t commands[] = {
{ NULL, notmuch_command, { NULL, notmuch_command, TRUE,
NULL, NULL,
"Notmuch main command." }, "Notmuch main command." },
{ "setup", notmuch_setup_command, { "setup", notmuch_setup_command, TRUE,
NULL, NULL,
"Interactively setup notmuch for first use." }, "Interactively setup notmuch for first use." },
{ "new", notmuch_new_command, { "new", notmuch_new_command, FALSE,
"[options...]", "[options...]",
"Find and import new messages to the notmuch database." }, "Find and import new messages to the notmuch database." },
{ "search", notmuch_search_command, { "search", notmuch_search_command, FALSE,
"[options...] <search-terms> [...]", "[options...] <search-terms> [...]",
"Search for messages matching the given search terms." }, "Search for messages matching the given search terms." },
{ "show", notmuch_show_command, { "show", notmuch_show_command, FALSE,
"<search-terms> [...]", "<search-terms> [...]",
"Show all messages matching the search terms." }, "Show all messages matching the search terms." },
{ "count", notmuch_count_command, { "count", notmuch_count_command, FALSE,
"[options...] <search-terms> [...]", "[options...] <search-terms> [...]",
"Count messages matching the search terms." }, "Count messages matching the search terms." },
{ "reply", notmuch_reply_command, { "reply", notmuch_reply_command, FALSE,
"[options...] <search-terms> [...]", "[options...] <search-terms> [...]",
"Construct a reply template for a set of messages." }, "Construct a reply template for a set of messages." },
{ "tag", notmuch_tag_command, { "tag", notmuch_tag_command, FALSE,
"+<tag>|-<tag> [...] [--] <search-terms> [...]" , "+<tag>|-<tag> [...] [--] <search-terms> [...]" ,
"Add/remove tags for all messages matching the search terms." }, "Add/remove tags for all messages matching the search terms." },
{ "dump", notmuch_dump_command, { "dump", notmuch_dump_command, FALSE,
"[<filename>] [--] [<search-terms>]", "[<filename>] [--] [<search-terms>]",
"Create a plain-text dump of the tags for each message." }, "Create a plain-text dump of the tags for each message." },
{ "restore", notmuch_restore_command, { "restore", notmuch_restore_command, FALSE,
"[--accumulate] [<filename>]", "[--accumulate] [<filename>]",
"Restore the tags from the given dump file (see 'dump')." }, "Restore the tags from the given dump file (see 'dump')." },
{ "config", notmuch_config_command, { "config", notmuch_config_command, FALSE,
"[get|set] <section>.<item> [value ...]", "[get|set] <section>.<item> [value ...]",
"Get or set settings in the notmuch configuration file." }, "Get or set settings in the notmuch configuration file." },
{ "help", notmuch_help_command, { "help", notmuch_help_command, TRUE, /* create but don't save config */
"[<command>]", "[<command>]",
"This message, or more detailed help for the named command." } "This message, or more detailed help for the named command." }
}; };
@ -155,7 +156,7 @@ exec_man (const char *page)
} }
static int static int
notmuch_help_command (void *ctx, int argc, char *argv[]) notmuch_help_command (notmuch_config_t *config, int argc, char *argv[])
{ {
command_t *command; command_t *command;
@ -178,7 +179,7 @@ notmuch_help_command (void *ctx, int argc, char *argv[])
command = find_command (argv[0]); command = find_command (argv[0]);
if (command) { if (command) {
char *page = talloc_asprintf (ctx, "notmuch-%s", command->name); char *page = talloc_asprintf (config, "notmuch-%s", command->name);
exec_man (page); exec_man (page);
} }
@ -199,28 +200,23 @@ notmuch_help_command (void *ctx, int argc, char *argv[])
* to be more clever about this in the future. * to be more clever about this in the future.
*/ */
static int static int
notmuch_command (void *ctx, unused(int argc), unused(char *argv[])) notmuch_command (notmuch_config_t *config,
unused(int argc), unused(char *argv[]))
{ {
notmuch_config_t *config;
char *db_path; char *db_path;
struct stat st; struct stat st;
config = notmuch_config_open (ctx, NULL, TRUE);
/* If the user has never configured notmuch, then run /* If the user has never configured notmuch, then run
* notmuch_setup_command which will give a nice welcome message, * notmuch_setup_command which will give a nice welcome message,
* and interactively guide the user through the configuration. */ * and interactively guide the user through the configuration. */
if (notmuch_config_is_new (config)) { if (notmuch_config_is_new (config))
notmuch_config_close (config); return notmuch_setup_command (config, 0, NULL);
return notmuch_setup_command (ctx, 0, NULL);
}
/* Notmuch is already configured, but is there a database? */ /* Notmuch is already configured, but is there a database? */
db_path = talloc_asprintf (ctx, "%s/%s", db_path = talloc_asprintf (config, "%s/%s",
notmuch_config_get_database_path (config), notmuch_config_get_database_path (config),
".notmuch"); ".notmuch");
if (stat (db_path, &st)) { if (stat (db_path, &st)) {
notmuch_config_close (config);
if (errno != ENOENT) { if (errno != ENOENT) {
fprintf (stderr, "Error looking for notmuch database at %s: %s\n", fprintf (stderr, "Error looking for notmuch database at %s: %s\n",
db_path, strerror (errno)); db_path, strerror (errno));
@ -252,8 +248,6 @@ notmuch_command (void *ctx, unused(int argc), unused(char *argv[]))
notmuch_config_get_user_name (config), notmuch_config_get_user_name (config),
notmuch_config_get_user_primary_email (config)); notmuch_config_get_user_primary_email (config));
notmuch_config_close (config);
return 0; return 0;
} }
@ -264,6 +258,7 @@ main (int argc, char *argv[])
char *talloc_report; char *talloc_report;
const char *command_name = NULL; const char *command_name = NULL;
command_t *command; command_t *command;
notmuch_config_t *config;
notmuch_bool_t print_help=FALSE, print_version=FALSE; notmuch_bool_t print_help=FALSE, print_version=FALSE;
int opt_index; int opt_index;
int ret = 0; int ret = 0;
@ -308,7 +303,13 @@ main (int argc, char *argv[])
return 1; return 1;
} }
ret = (command->function)(local, argc - opt_index, argv + opt_index); config = notmuch_config_open (local, NULL, command->create_config);
if (!config)
return 1;
ret = (command->function)(config, argc - opt_index, argv + opt_index);
notmuch_config_close (config);
talloc_report = getenv ("NOTMUCH_TALLOC_REPORT"); talloc_report = getenv ("NOTMUCH_TALLOC_REPORT");
if (talloc_report && strcmp (talloc_report, "") != 0) { if (talloc_report && strcmp (talloc_report, "") != 0) {