From 621c94bcb7ec501375ff1d70e0bf637a4cc237b3 Mon Sep 17 00:00:00 2001 From: Jani Nikula Date: Sun, 3 Mar 2013 23:55:09 +0200 Subject: [PATCH] [PATCH v2 5/6] cli: move config open/close to main() from subcommands --- ab/d1dd00e06314199c2ed213a4210320b2840e6f | 884 ++++++++++++++++++++++ 1 file changed, 884 insertions(+) create mode 100644 ab/d1dd00e06314199c2ed213a4210320b2840e6f diff --git a/ab/d1dd00e06314199c2ed213a4210320b2840e6f b/ab/d1dd00e06314199c2ed213a4210320b2840e6f new file mode 100644 index 000000000..b6def1597 --- /dev/null +++ b/ab/d1dd00e06314199c2ed213a4210320b2840e6f @@ -0,0 +1,884 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id C4130429E36 + for ; Sun, 3 Mar 2013 13:55:42 -0800 (PST) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" +X-Spam-Flag: NO +X-Spam-Score: -0.7 +X-Spam-Level: +X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 + tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id ywBCbdX2FNZV for ; + Sun, 3 Mar 2013 13:55:35 -0800 (PST) +Received: from mail-la0-f51.google.com (mail-la0-f51.google.com + [209.85.215.51]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id C7C37431FC4 + for ; Sun, 3 Mar 2013 13:55:28 -0800 (PST) +Received: by mail-la0-f51.google.com with SMTP id fo13so4452375lab.38 + for ; Sun, 03 Mar 2013 13:55:27 -0800 (PST) +X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; + d=google.com; s=20120113; + h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to + :references:in-reply-to:references:x-gm-message-state; + bh=O+JcirM1yFTlgiq9i/P1lfsmc7YoUJ92yBY8yiKiecg=; + b=jjmA21zLq2DXcRoKxCTFxgK5ZlmU+ft8SJ16pW23sXm4igeMzarAdgKxDWopKujZaz + DF9C437ZSc/kjYUIOLCU8hyg3YbxcO/ugseR0Z0txQsFjdzFAXCmqYxGYq3NhewXlIx0 + 4Xg9qlxsLQV/c27wXu4NrwGZbsFBNATqdCNYh6cjQCBQ2JXzvq339xvpb5YGd0lX5TGr + TbWNnguuQWCkn+tm5VXeXlxJe3R+JcoIoZY0EvOwQThqR7wOvYLiaRZ7TEteYALR3IVp + m0SJAZpvlGLosrrXUQASWle5PW+7r+TJAi+N4fKLjmu8YVsyGLITCvs4Zz/94OKdCfv0 + uW5w== +X-Received: by 10.112.23.232 with SMTP id p8mr3714912lbf.38.1362347727087; + Sun, 03 Mar 2013 13:55:27 -0800 (PST) +Received: from localhost (dsl-hkibrasgw4-50df51-27.dhcp.inet.fi. + [80.223.81.27]) + by mx.google.com with ESMTPS id gm20sm10730463lab.7.2013.03.03.13.55.25 + (version=TLSv1.2 cipher=RC4-SHA bits=128/128); + Sun, 03 Mar 2013 13:55:26 -0800 (PST) +From: Jani Nikula +To: notmuch@notmuchmail.org +Subject: [PATCH v2 5/6] cli: move config open/close to main() from subcommands +Date: Sun, 3 Mar 2013 23:55:09 +0200 +Message-Id: + <75b8b04e4d63d67ec79ffe2379829add96b37be0.1362347362.git.jani@nikula.org> +X-Mailer: git-send-email 1.7.10.4 +In-Reply-To: +References: +In-Reply-To: +References: +X-Gm-Message-State: + ALoCoQlzeLlLN4cbh8oEbSglSr32TjWfYsb8rIhIiVoprRoQPrNb6l1pNlICR64Pi6ZP9lHZW9Ci +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Sun, 03 Mar 2013 21:55:43 -0000 + +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. +--- + 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, ¬much_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, ¬much)) + 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, ¬much)) + 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, ¶ms, reply_all, sp) != 0) ++ if (reply_format_func (config, config, query, ¶ms, reply_all, sp) != 0) + return 1; + + notmuch_crypto_cleanup (¶ms.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, ¬much)) + 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, ¬much)) + 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, ¶ms); ++ ret = do_show_single (config, query, format, sprinter, ¶ms); + 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, ¶ms); ++ ret = do_show (config, query, format, sprinter, ¶ms); + } + + notmuch_crypto_cleanup (¶ms.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, ¬much)) + 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 e434d03..3241e23 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 for messages matching the given search terms." }, +- { "show", notmuch_show_command, ++ { "show", notmuch_show_command, FALSE, + " [...]", + "Show all messages matching the search terms." }, +- { "count", notmuch_count_command, ++ { "count", notmuch_count_command, FALSE, + "[options...] [...]", + "Count messages matching the search terms." }, +- { "reply", notmuch_reply_command, ++ { "reply", notmuch_reply_command, FALSE, + "[options...] [...]", + "Construct a reply template for a set of messages." }, +- { "tag", notmuch_tag_command, ++ { "tag", notmuch_tag_command, FALSE, + "+|- [...] [--] [...]" , + "Add/remove tags for all messages matching the search terms." }, +- { "dump", notmuch_dump_command, ++ { "dump", notmuch_dump_command, FALSE, + "[] [--] []", + "Create a plain-text dump of the tags for each message." }, +- { "restore", notmuch_restore_command, ++ { "restore", notmuch_restore_command, FALSE, + "[--accumulate] []", + "Restore the tags from the given dump file (see 'dump')." }, +- { "config", notmuch_config_command, ++ { "config", notmuch_config_command, FALSE, + "[get|set]
. [value ...]", + "Get or set settings in the notmuch configuration file." }, +- { "help", notmuch_help_command, ++ { "help", notmuch_help_command, TRUE, /* create but don't save config */ + "[]", + "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); + + talloc_report = getenv ("NOTMUCH_TALLOC_REPORT"); + if (talloc_report && strcmp (talloc_report, "") != 0) { +-- +1.7.10.4 + -- 2.26.2