[PATCH 5/6] cli: move config open/close to main() from subcommands

Jani Nikula jani at nikula.org
Tue Jan 29 13:46:23 PST 2013


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.

This also makes notmuch config the talloc context for subcommands.
---
 notmuch-client.h  |   30 +++++++++++---------------
 notmuch-config.c  |   40 +++++++----------------------------
 notmuch-count.c   |   11 +++-------
 notmuch-dump.c    |    7 +-----
 notmuch-new.c     |   17 ++++++---------
 notmuch-reply.c   |   15 +++++--------
 notmuch-restore.c |   11 +++-------
 notmuch-search.c  |   15 +++++--------
 notmuch-setup.c   |   17 ++++++---------
 notmuch-show.c    |   15 +++++--------
 notmuch-tag.c     |   15 +++++--------
 notmuch.c         |   61 +++++++++++++++++++++++++++--------------------------
 12 files changed, 91 insertions(+), 163 deletions(-)

diff --git a/notmuch-client.h b/notmuch-client.h
index b3dcb21..45749a6 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -150,6 +150,8 @@ chomp_newline (char *str)
  */
 extern int notmuch_format_version;
 
+typedef struct _notmuch_config notmuch_config_t;
+
 /* Commands that support structured output should support the
  * following argument
  *  { NOTMUCH_OPT_INT, &notmuch_format_version, "format-version", 0, 0 }
@@ -169,40 +171,34 @@ int
 notmuch_crypto_cleanup (notmuch_crypto_t *crypto);
 
 int
-notmuch_count_command (void *ctx, int argc, char *argv[]);
-
-int
-notmuch_dump_command (void *ctx, int argc, char *argv[]);
+notmuch_count_command (notmuch_config_t *config, int argc, char *argv[]);
 
 int
-notmuch_new_command (void *ctx, int argc, char *argv[]);
+notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[]);
 
 int
-notmuch_reply_command (void *ctx, int argc, char *argv[]);
+notmuch_new_command (notmuch_config_t *config, int argc, char *argv[]);
 
 int
-notmuch_restore_command (void *ctx, int argc, char *argv[]);
+notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[]);
 
 int
-notmuch_search_command (void *ctx, int argc, char *argv[]);
+notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[]);
 
 int
-notmuch_setup_command (void *ctx, int argc, char *argv[]);
+notmuch_search_command (notmuch_config_t *config, int argc, char *argv[]);
 
 int
-notmuch_show_command (void *ctx, int argc, char *argv[]);
+notmuch_setup_command (notmuch_config_t *config, int argc, char *argv[]);
 
 int
-notmuch_tag_command (void *ctx, int argc, char *argv[]);
+notmuch_show_command (notmuch_config_t *config, int argc, char *argv[]);
 
 int
-notmuch_search_tags_command (void *ctx, int argc, char *argv[]);
+notmuch_tag_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[]);
+notmuch_config_command (notmuch_config_t *config, int argc, char *argv[]);
 
 const char *
 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 */
 
-typedef struct _notmuch_config notmuch_config_t;
-
 notmuch_config_t *
 notmuch_config_open (void *ctx,
 		     const char *filename,
diff --git a/notmuch-config.c b/notmuch-config.c
index 247fbe4..48312e3 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -705,14 +705,8 @@ _item_split (char *item, char **group, char **key)
 }
 
 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) {
 	printf ("%s\n", notmuch_config_get_database_path (config));
     } else if (strcmp(item, "user.name") == 0) {
@@ -756,25 +750,17 @@ notmuch_config_command_get (void *ctx, char *item)
 	g_strfreev (value);
     }
 
-    notmuch_config_close (config);
-
     return 0;
 }
 
 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;
-    int ret;
 
     if (_item_split (item, &group, &key))
 	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
      * configuration file.
      *
@@ -795,23 +781,15 @@ notmuch_config_command_set (void *ctx, char *item, int argc, char *argv[])
 	break;
     }
 
-    ret = notmuch_config_save (config);
-    notmuch_config_close (config);
-
-    return ret;
+    return notmuch_config_save (config);
 }
 
 static int
-notmuch_config_command_list (void *ctx)
+notmuch_config_command_list (notmuch_config_t *config)
 {
-    notmuch_config_t *config;
     char **groups;
     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);
     if (groups == NULL)
 	return 1;
@@ -841,13 +819,11 @@ notmuch_config_command_list (void *ctx)
 
     g_strfreev (groups);
 
-    notmuch_config_close (config);
-
     return 0;
 }
 
 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 */
 
@@ -862,16 +838,16 @@ notmuch_config_command (void *ctx, int argc, char *argv[])
 		     "one argument.\n");
 	    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) {
 	if (argc < 2) {
 	    fprintf (stderr, "Error: notmuch config set requires at least "
 		     "one argument.\n");
 	    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) {
-	return notmuch_config_command_list (ctx);
+	return notmuch_config_command_list (config);
     }
 
     fprintf (stderr, "Unrecognized argument for notmuch config: %s\n",
diff --git a/notmuch-count.c b/notmuch-count.c
index 61722ed..390794f 100644
--- a/notmuch-count.c
+++ b/notmuch-count.c
@@ -33,9 +33,8 @@ enum {
 };
 
 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_query_t *query;
     char *query_str;
@@ -62,22 +61,18 @@ notmuch_count_command (void *ctx, int argc, char *argv[])
 	return 1;
     }
 
-    config = notmuch_config_open (ctx, NULL, FALSE);
-    if (config == NULL)
-	return 1;
-
     if (notmuch_database_open (notmuch_config_get_database_path (config),
 			       NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
 	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) {
 	fprintf (stderr, "Out of memory.\n");
 	return 1;
     }
 
     if (*query_str == '\0') {
-	query_str = talloc_strdup (ctx, "");
+	query_str = talloc_strdup (config, "");
     }
 
     query = notmuch_query_create (notmuch, query_str);
diff --git a/notmuch-dump.c b/notmuch-dump.c
index 845a67e..2024e30 100644
--- a/notmuch-dump.c
+++ b/notmuch-dump.c
@@ -23,9 +23,8 @@
 #include "string-util.h"
 
 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_query_t *query;
     FILE *output = stdout;
@@ -34,10 +33,6 @@ notmuch_dump_command (unused (void *ctx), int argc, char *argv[])
     notmuch_tags_t *tags;
     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),
 			       NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
 	return 1;
diff --git a/notmuch-new.c b/notmuch-new.c
index 4915418..faa33f1 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -840,9 +840,8 @@ _remove_directory (void *ctx,
 }
 
 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;
     add_files_state_t add_files_state;
     double elapsed;
@@ -875,10 +874,6 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
 	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_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);
@@ -890,7 +885,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
 	    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)) {
 	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;
     gettimeofday (&add_files_state.tv_start, NULL);
 
-    add_files_state.removed_files = _filename_list_create (ctx);
-    add_files_state.removed_directories = _filename_list_create (ctx);
-    add_files_state.directory_mtimes = _filename_list_create (ctx);
+    add_files_state.removed_files = _filename_list_create (config);
+    add_files_state.removed_directories = _filename_list_create (config);
+    add_files_state.directory_mtimes = _filename_list_create (config);
 
     if (! debugger_is_active () && add_files_state.output_is_a_tty
 	&& ! add_files_state.verbose) {
@@ -970,7 +965,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
 
     gettimeofday (&tv_start, NULL);
     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)
 	    goto DONE;
 	if (do_print_progress) {
diff --git a/notmuch-reply.c b/notmuch-reply.c
index 9da42b9..e151f78 100644
--- a/notmuch-reply.c
+++ b/notmuch-reply.c
@@ -702,9 +702,8 @@ enum {
 };
 
 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_query_t *query;
     char *query_string;
@@ -752,21 +751,17 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])
 	reply_format_func = notmuch_reply_format_headers_only;
     } else if (format == FORMAT_JSON) {
 	reply_format_func = notmuch_reply_format_sprinter;
-	sp = sprinter_json_create (ctx, stdout);
+	sp = sprinter_json_create (config, stdout);
     } else if (format == FORMAT_SEXP) {
 	reply_format_func = notmuch_reply_format_sprinter;
-	sp = sprinter_sexp_create (ctx, stdout);
+	sp = sprinter_sexp_create (config, stdout);
     } else {
 	reply_format_func = notmuch_reply_format_default;
     }
 
     notmuch_exit_if_unsupported_format ();
 
-    config = notmuch_config_open (ctx, NULL, FALSE);
-    if (config == NULL)
-	return 1;
-
-    query_string = query_string_from_args (ctx, argc-opt_index, argv+opt_index);
+    query_string = query_string_from_args (config, argc-opt_index, argv+opt_index);
     if (query_string == NULL) {
 	fprintf (stderr, "Out of memory\n");
 	return 1;
@@ -787,7 +782,7 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])
 	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;
 
     notmuch_crypto_cleanup (&params.crypto);
diff --git a/notmuch-restore.c b/notmuch-restore.c
index dd2507f..1419621 100644
--- a/notmuch-restore.c
+++ b/notmuch-restore.c
@@ -120,9 +120,8 @@ parse_sup_line (void *ctx, char *line,
 }
 
 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_bool_t accumulate = FALSE;
     tag_op_flag_t flags = 0;
@@ -139,10 +138,6 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
     int opt_index;
     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),
 			       NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
 	return 1;
@@ -187,7 +182,7 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
 	return 1;
     }
 
-    tag_ops = tag_op_list_create (ctx);
+    tag_ops = tag_op_list_create (config);
     if (tag_ops == NULL) {
 	fprintf (stderr, "Out of memory.\n");
 	return 1;
@@ -226,7 +221,7 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
 	if (line_ctx != NULL)
 	    talloc_free (line_ctx);
 
-	line_ctx = talloc_new (ctx);
+	line_ctx = talloc_new (config);
 	if (input_format == DUMP_FORMAT_SUP) {
 	    ret = parse_sup_line (line_ctx, line, &query_string, tag_ops);
 	} else {
diff --git a/notmuch-search.c b/notmuch-search.c
index fac6663..e658639 100644
--- a/notmuch-search.c
+++ b/notmuch-search.c
@@ -290,9 +290,8 @@ enum {
 };
 
 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_query_t *query;
     char *query_str;
@@ -349,20 +348,20 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
 
     switch (format_sel) {
     case NOTMUCH_FORMAT_TEXT:
-	format = sprinter_text_create (ctx, stdout);
+	format = sprinter_text_create (config, stdout);
 	break;
     case NOTMUCH_FORMAT_TEXT0:
 	if (output == OUTPUT_SUMMARY) {
 	    fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
 	    return 1;
 	}
-	format = sprinter_text0_create (ctx, stdout);
+	format = sprinter_text0_create (config, stdout);
 	break;
     case NOTMUCH_FORMAT_JSON:
-	format = sprinter_json_create (ctx, stdout);
+	format = sprinter_json_create (config, stdout);
 	break;
     case NOTMUCH_FORMAT_SEXP:
-	format = sprinter_sexp_create (ctx, stdout);
+	format = sprinter_sexp_create (config, stdout);
 	break;
     default:
 	/* this should never happen */
@@ -371,10 +370,6 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
 
     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),
 			       NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
 	return 1;
diff --git a/notmuch-setup.c b/notmuch-setup.c
index 72d862a..475248b 100644
--- a/notmuch-setup.c
+++ b/notmuch-setup.c
@@ -120,12 +120,11 @@ parse_tag_list (void *ctx, char *response)
 }
 
 int
-notmuch_setup_command (unused (void *ctx),
+notmuch_setup_command (notmuch_config_t *config,
 		       unused (int argc), unused (char *argv[]))
 {
     char *response = NULL;
     size_t response_size = 0;
-    notmuch_config_t *config;
     const char **old_other_emails;
     size_t old_other_emails_len;
     GPtrArray *other_emails;
@@ -146,8 +145,6 @@ notmuch_setup_command (unused (void *ctx),
 	chomp_newline (response);				\
     } while (0)
 
-    config = notmuch_config_open (ctx, NULL, TRUE);
-
     if (notmuch_config_is_new (config))
 	welcome_message_pre_setup ();
 
@@ -167,16 +164,16 @@ notmuch_setup_command (unused (void *ctx),
     for (i = 0; i < old_other_emails_len; i++) {
 	prompt ("Additional email address [%s]: ", old_other_emails[i]);
 	if (strlen (response))
-	    g_ptr_array_add (other_emails, talloc_strdup (ctx, response));
+	    g_ptr_array_add (other_emails, talloc_strdup (config, response));
 	else
-	    g_ptr_array_add (other_emails, talloc_strdup (ctx,
+	    g_ptr_array_add (other_emails, talloc_strdup (config,
 							 old_other_emails[i]));
     }
 
     do {
 	prompt ("Additional email address [Press 'Enter' if none]: ");
 	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));
     if (other_emails->len)
 	notmuch_config_set_user_other_email (config,
@@ -190,7 +187,7 @@ notmuch_setup_command (unused (void *ctx),
     if (strlen (response)) {
 	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);
     }
 
@@ -201,7 +198,7 @@ notmuch_setup_command (unused (void *ctx),
     prompt ("]: ");
 
     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,
 				     tags->len);
@@ -217,7 +214,7 @@ notmuch_setup_command (unused (void *ctx),
     prompt ("]: ");
 
     if (strlen (response)) {
-	GPtrArray *tags = parse_tag_list (ctx, response);
+	GPtrArray *tags = parse_tag_list (config, response);
 
 	notmuch_config_set_search_exclude_tags (config,
 						(const char **) tags->pdata,
diff --git a/notmuch-show.c b/notmuch-show.c
index 5ae5d7d..c2ec122 100644
--- a/notmuch-show.c
+++ b/notmuch-show.c
@@ -1056,9 +1056,8 @@ enum {
 };
 
 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_query_t *query;
     char *query_string;
@@ -1176,11 +1175,7 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
     else
 	params.entire_thread = FALSE;
 
-    config = notmuch_config_open (ctx, NULL, FALSE);
-    if (config == NULL)
-	return 1;
-
-    query_string = query_string_from_args (ctx, argc-opt_index, argv+opt_index);
+    query_string = query_string_from_args (config, argc-opt_index, argv+opt_index);
     if (query_string == NULL) {
 	fprintf (stderr, "Out of memory\n");
 	return 1;
@@ -1202,11 +1197,11 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
     }
 
     /* 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 (params.part >= 0)
-	ret = do_show_single (ctx, query, format, sprinter, &params);
+	ret = do_show_single (config, query, format, sprinter, &params);
     else {
 	/* We always apply set the exclude flag. The
 	 * 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;
 	}
 
-	ret = do_show (ctx, query, format, sprinter, &params);
+	ret = do_show (config, query, format, sprinter, &params);
     }
 
     notmuch_crypto_cleanup (&params.crypto);
diff --git a/notmuch-tag.c b/notmuch-tag.c
index 148e856..0e73197 100644
--- a/notmuch-tag.c
+++ b/notmuch-tag.c
@@ -178,11 +178,10 @@ tag_file (void *ctx, notmuch_database_t *notmuch, tag_op_flag_t flags,
 }
 
 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;
     char *query_string = NULL;
-    notmuch_config_t *config;
     notmuch_database_t *notmuch;
     struct sigaction action;
     tag_op_flag_t tag_flags = TAG_FLAG_NONE;
@@ -225,21 +224,17 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])
 	    return 1;
 	}
     } else {
-	tag_ops = tag_op_list_create (ctx);
+	tag_ops = tag_op_list_create (config);
 	if (tag_ops == NULL) {
 	    fprintf (stderr, "Out of memory.\n");
 	    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))
 	    return 1;
     }
 
-    config = notmuch_config_open (ctx, NULL, FALSE);
-    if (config == NULL)
-	return 1;
-
     if (notmuch_database_open (notmuch_config_get_database_path (config),
 			       NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
 	return 1;
@@ -248,9 +243,9 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])
 	tag_flags |= TAG_FLAG_MAILDIR_SYNC;
 
     if (batch)
-	ret = tag_file (ctx, notmuch, tag_flags, input);
+	ret = tag_file (config, notmuch, tag_flags, input);
     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);
 
diff --git a/notmuch.c b/notmuch.c
index 67d5772..f4bfeaa 100644
--- a/notmuch.c
+++ b/notmuch.c
@@ -22,56 +22,57 @@
 
 #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 {
     const char *name;
     command_function_t function;
+    notmuch_bool_t create_config;
     const char *arguments;
     const char *summary;
 } command_t;
 
 static int
-notmuch_help_command (void *ctx, int argc, char *argv[]);
+notmuch_help_command (notmuch_config_t *config, int argc, char *argv[]);
 
 static int
-notmuch_command (void *ctx, int argc, char *argv[]);
+notmuch_command (notmuch_config_t *config, int argc, char *argv[]);
 
 static command_t commands[] = {
-    { NULL, notmuch_command,
+    { NULL, notmuch_command, TRUE,
       NULL,
       "Notmuch main command." },
-    { "setup", notmuch_setup_command,
+    { "setup", notmuch_setup_command, TRUE,
       NULL,
       "Interactively setup notmuch for first use." },
-    { "new", notmuch_new_command,
+    { "new", notmuch_new_command, FALSE,
       "[options...]",
       "Find and import new messages to the notmuch database." },
-    { "search", notmuch_search_command,
+    { "search", notmuch_search_command, FALSE,
       "[options...] <search-terms> [...]",
       "Search for messages matching the given search terms." },
-    { "show", notmuch_show_command,
+    { "show", notmuch_show_command, FALSE,
       "<search-terms> [...]",
       "Show all messages matching the search terms." },
-    { "count", notmuch_count_command,
+    { "count", notmuch_count_command, FALSE,
       "[options...] <search-terms> [...]",
       "Count messages matching the search terms." },
-    { "reply", notmuch_reply_command,
+    { "reply", notmuch_reply_command, FALSE,
       "[options...] <search-terms> [...]",
       "Construct a reply template for a set of messages." },
-    { "tag", notmuch_tag_command,
+    { "tag", notmuch_tag_command, FALSE,
       "+<tag>|-<tag> [...] [--] <search-terms> [...]" ,
       "Add/remove tags for all messages matching the search terms." },
-    { "dump", notmuch_dump_command,
+    { "dump", notmuch_dump_command, FALSE,
       "[<filename>] [--] [<search-terms>]",
       "Create a plain-text dump of the tags for each message." },
-    { "restore", notmuch_restore_command,
+    { "restore", notmuch_restore_command, FALSE,
       "[--accumulate] [<filename>]",
       "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 or set settings in the notmuch configuration file." },
-    { "help", notmuch_help_command,
+    { "help", notmuch_help_command, FALSE,
       "[<command>]",
       "This message, or more detailed help for the named command." }
 };
@@ -155,7 +156,7 @@ exec_man (const char *page)
 }
 
 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;
 
@@ -178,7 +179,7 @@ notmuch_help_command (void *ctx, int argc, char *argv[])
 
     command = find_command (argv[0]);
     if (command) {
-	char *page = talloc_asprintf (ctx, "notmuch-%s", command->name);
+	char *page = talloc_asprintf (config, "notmuch-%s", command->name);
 	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.
  */
 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;
     struct stat st;
 
-    config = notmuch_config_open (ctx, NULL, TRUE);
-
     /* 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. */
-    if (notmuch_config_is_new (config)) {
-	notmuch_config_close (config);
-	return notmuch_setup_command (ctx, 0, NULL);
-    }
+    if (notmuch_config_is_new (config))
+	return notmuch_setup_command (config, 0, NULL);
 
     /* 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");
     if (stat (db_path, &st)) {
-	notmuch_config_close (config);
 	if (errno != ENOENT) {
 	    fprintf (stderr, "Error looking for notmuch database at %s: %s\n",
 		     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_primary_email (config));
 
-    notmuch_config_close (config);
-
     return 0;
 }
 
@@ -264,6 +258,7 @@ main (int argc, char *argv[])
     char *talloc_report;
     const char *command_name = NULL;
     command_t *command;
+    notmuch_config_t *config;
     notmuch_bool_t print_help=FALSE, print_version=FALSE;
     int opt_index;
     int ret = 0;
@@ -308,7 +303,13 @@ main (int argc, char *argv[])
 	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);
 
     /* in the future support for this environment variable may
      * be supplemented or replaced by command line arguments
-- 
1.7.10.4



More information about the notmuch mailing list