unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: David Bremner <david@tethera.net>
To: notmuch@notmuchmail.org
Cc: David Bremner <david@tethera.net>
Subject: [PATCH 01/36] CLI: make variable n_requested_db_uuid file scope.
Date: Tue, 24 Aug 2021 08:17:10 -0700	[thread overview]
Message-ID: <20210824151745.2941868-2-david@tethera.net> (raw)
In-Reply-To: <20210824151745.2941868-1-david@tethera.net>

It turns out that now that we pass an open database into the
subcommands, it is easy to check any requested uuid against the
database at the same time as we process the other shared
arguments. This results in overall less boilerplate code, as well as
making a CLI scope function and variable file scope in notmuch.c.
---
 notmuch-client.h     |  4 +---
 notmuch-compact.c    |  7 +------
 notmuch-config.c     |  4 ----
 notmuch-count.c      |  4 +---
 notmuch-dump.c       |  4 +---
 notmuch-insert.c     |  4 +---
 notmuch-new.c        |  4 +---
 notmuch-reindex.c    |  4 +---
 notmuch-reply.c      |  4 +---
 notmuch-restore.c    |  3 +--
 notmuch-search.c     |  6 ++----
 notmuch-setup.c      |  4 ----
 notmuch-show.c       |  4 +---
 notmuch-tag.c        |  4 +---
 notmuch.c            | 24 ++++++++++++++++++------
 test/random-corpus.c |  3 ++-
 16 files changed, 33 insertions(+), 54 deletions(-)

diff --git a/notmuch-client.h b/notmuch-client.h
index 8643a63f..f820791f 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -485,11 +485,9 @@ print_status_gzbytes (const char *loc,
 
 #include "command-line-arguments.h"
 
-extern const char *notmuch_requested_db_uuid;
 extern const notmuch_opt_desc_t notmuch_shared_options [];
-void notmuch_exit_if_unmatched_db_uuid (notmuch_database_t *notmuch);
 
-void notmuch_process_shared_options (const char *subcommand_name);
+void notmuch_process_shared_options (notmuch_database_t *notmuch, const char *subcommand_name);
 int notmuch_minimal_options (const char *subcommand_name,
 			     int argc, char **argv);
 
diff --git a/notmuch-compact.c b/notmuch-compact.c
index 2648434e..40ffb428 100644
--- a/notmuch-compact.c
+++ b/notmuch-compact.c
@@ -45,12 +45,7 @@ notmuch_compact_command (notmuch_database_t *notmuch, int argc, char *argv[])
     if (opt_index < 0)
 	return EXIT_FAILURE;
 
-    if (notmuch_requested_db_uuid) {
-	fprintf (stderr, "Error: --uuid not implemented for compact\n");
-	return EXIT_FAILURE;
-    }
-
-    notmuch_process_shared_options (argv[0]);
+    notmuch_process_shared_options (NULL, argv[0]);
 
     if (! quiet)
 	printf ("Compacting database...\n");
diff --git a/notmuch-config.c b/notmuch-config.c
index 4de55e5f..80a207f6 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -708,10 +708,6 @@ notmuch_config_command (notmuch_database_t *notmuch, int argc, char *argv[])
     if (opt_index < 0)
 	return EXIT_FAILURE;
 
-    if (notmuch_requested_db_uuid)
-	fprintf (stderr, "Warning: ignoring --uuid=%s\n",
-		 notmuch_requested_db_uuid);
-
     /* skip at least subcommand argument */
     argc -= opt_index;
     argv += opt_index;
diff --git a/notmuch-count.c b/notmuch-count.c
index 5ac4292b..e8c545e3 100644
--- a/notmuch-count.c
+++ b/notmuch-count.c
@@ -182,7 +182,7 @@ notmuch_count_command (notmuch_database_t *notmuch, int argc, char *argv[])
     if (opt_index < 0)
 	return EXIT_FAILURE;
 
-    notmuch_process_shared_options (argv[0]);
+    notmuch_process_shared_options (notmuch, argv[0]);
 
     if (input_file_name) {
 	batch = true;
@@ -201,8 +201,6 @@ notmuch_count_command (notmuch_database_t *notmuch, int argc, char *argv[])
 	return EXIT_FAILURE;
     }
 
-    notmuch_exit_if_unmatched_db_uuid (notmuch);
-
     query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
     if (query_str == NULL) {
 	fprintf (stderr, "Out of memory.\n");
diff --git a/notmuch-dump.c b/notmuch-dump.c
index ae89e4da..5c8213be 100644
--- a/notmuch-dump.c
+++ b/notmuch-dump.c
@@ -366,8 +366,6 @@ notmuch_dump_command (notmuch_database_t *notmuch, int argc, char *argv[])
     const char *query_str = NULL;
     int ret;
 
-    notmuch_exit_if_unmatched_db_uuid (notmuch);
-
     const char *output_file_name = NULL;
     int opt_index;
 
@@ -394,7 +392,7 @@ notmuch_dump_command (notmuch_database_t *notmuch, int argc, char *argv[])
     if (opt_index < 0)
 	return EXIT_FAILURE;
 
-    notmuch_process_shared_options (argv[0]);
+    notmuch_process_shared_options (notmuch, argv[0]);
 
     if (include == 0)
 	include = DUMP_INCLUDE_CONFIG | DUMP_INCLUDE_TAGS | DUMP_INCLUDE_PROPERTIES;
diff --git a/notmuch-insert.c b/notmuch-insert.c
index e3d87e4a..72e2e35f 100644
--- a/notmuch-insert.c
+++ b/notmuch-insert.c
@@ -478,7 +478,7 @@ notmuch_insert_command (notmuch_database_t *notmuch, int argc, char *argv[])
     if (opt_index < 0)
 	return EXIT_FAILURE;
 
-    notmuch_process_shared_options (argv[0]);
+    notmuch_process_shared_options (notmuch, argv[0]);
 
     mail_root = notmuch_config_get (notmuch, NOTMUCH_CONFIG_MAIL_ROOT);
 
@@ -550,8 +550,6 @@ notmuch_insert_command (notmuch_database_t *notmuch, int argc, char *argv[])
 	return EXIT_FAILURE;
     }
 
-    notmuch_exit_if_unmatched_db_uuid (notmuch);
-
     status = notmuch_process_shared_indexing_options (notmuch);
     if (status != NOTMUCH_STATUS_SUCCESS) {
 	fprintf (stderr, "Error: Failed to process index options. (%s)\n",
diff --git a/notmuch-new.c b/notmuch-new.c
index 1ee498fa..b7a5f2ea 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -1142,7 +1142,7 @@ notmuch_new_command (notmuch_database_t *notmuch, int argc, char *argv[])
     if (opt_index < 0)
 	return EXIT_FAILURE;
 
-    notmuch_process_shared_options (argv[0]);
+    notmuch_process_shared_options (notmuch, argv[0]);
 
     /* quiet trumps verbose */
     if (quiet)
@@ -1197,8 +1197,6 @@ notmuch_new_command (notmuch_database_t *notmuch, int argc, char *argv[])
 	    return EXIT_FAILURE;
     }
 
-    notmuch_exit_if_unmatched_db_uuid (notmuch);
-
     if (notmuch_database_get_revision (notmuch, NULL) == 0) {
 	int count = 0;
 	count_files (mail_root, &count, &add_files_state);
diff --git a/notmuch-reindex.c b/notmuch-reindex.c
index a7380a4b..b40edbb6 100644
--- a/notmuch-reindex.c
+++ b/notmuch-reindex.c
@@ -108,9 +108,7 @@ notmuch_reindex_command (notmuch_database_t *notmuch, int argc, char *argv[])
     if (opt_index < 0)
 	return EXIT_FAILURE;
 
-    notmuch_process_shared_options (argv[0]);
-
-    notmuch_exit_if_unmatched_db_uuid (notmuch);
+    notmuch_process_shared_options (notmuch, argv[0]);
 
     status = notmuch_process_shared_indexing_options (notmuch);
     if (status != NOTMUCH_STATUS_SUCCESS) {
diff --git a/notmuch-reply.c b/notmuch-reply.c
index 08140799..5d5f95a3 100644
--- a/notmuch-reply.c
+++ b/notmuch-reply.c
@@ -743,7 +743,7 @@ notmuch_reply_command (notmuch_database_t *notmuch, int argc, char *argv[])
     if (opt_index < 0)
 	return EXIT_FAILURE;
 
-    notmuch_process_shared_options (argv[0]);
+    notmuch_process_shared_options (notmuch, argv[0]);
 
     notmuch_exit_if_unsupported_format ();
 
@@ -758,8 +758,6 @@ notmuch_reply_command (notmuch_database_t *notmuch, int argc, char *argv[])
 	return EXIT_FAILURE;
     }
 
-    notmuch_exit_if_unmatched_db_uuid (notmuch);
-
     query = notmuch_query_create (notmuch, query_string);
     if (query == NULL) {
 	fprintf (stderr, "Out of memory\n");
diff --git a/notmuch-restore.c b/notmuch-restore.c
index 1a81212f..1cce004a 100644
--- a/notmuch-restore.c
+++ b/notmuch-restore.c
@@ -272,8 +272,7 @@ notmuch_restore_command (notmuch_database_t *notmuch, int argc, char *argv[])
 	goto DONE;
     }
 
-    notmuch_process_shared_options (argv[0]);
-    notmuch_exit_if_unmatched_db_uuid (notmuch);
+    notmuch_process_shared_options (notmuch, argv[0]);
 
     if (include == 0) {
 	include = DUMP_INCLUDE_CONFIG | DUMP_INCLUDE_PROPERTIES | DUMP_INCLUDE_TAGS;
diff --git a/notmuch-search.c b/notmuch-search.c
index 244817a9..375a247d 100644
--- a/notmuch-search.c
+++ b/notmuch-search.c
@@ -709,8 +709,6 @@ _notmuch_search_prepare (search_context_t *ctx, int argc, char *argv[])
 
     notmuch_exit_if_unsupported_format ();
 
-    notmuch_exit_if_unmatched_db_uuid (ctx->notmuch);
-
     query_str = query_string_from_args (ctx->notmuch, argc, argv);
     if (query_str == NULL) {
 	fprintf (stderr, "Out of memory.\n");
@@ -827,7 +825,7 @@ notmuch_search_command (notmuch_database_t *notmuch, int argc, char *argv[])
     if (opt_index < 0)
 	return EXIT_FAILURE;
 
-    notmuch_process_shared_options (argv[0]);
+    notmuch_process_shared_options (notmuch, argv[0]);
 
     if (ctx->output != OUTPUT_FILES && ctx->output != OUTPUT_MESSAGES &&
 	ctx->dupe != -1) {
@@ -893,7 +891,7 @@ notmuch_address_command (notmuch_database_t *notmuch, int argc, char *argv[])
     if (opt_index < 0)
 	return EXIT_FAILURE;
 
-    notmuch_process_shared_options (argv[0]);
+    notmuch_process_shared_options (notmuch, argv[0]);
 
     if (! (ctx->output & (OUTPUT_SENDER | OUTPUT_RECIPIENTS)))
 	ctx->output |= OUTPUT_SENDER;
diff --git a/notmuch-setup.c b/notmuch-setup.c
index 28f4b7de..9382e279 100644
--- a/notmuch-setup.c
+++ b/notmuch-setup.c
@@ -147,10 +147,6 @@ notmuch_setup_command (notmuch_database_t *notmuch,
     if (notmuch_minimal_options ("setup", argc, argv) < 0)
 	return EXIT_FAILURE;
 
-    if (notmuch_requested_db_uuid)
-	fprintf (stderr, "Warning: ignoring --uuid=%s\n",
-		 notmuch_requested_db_uuid);
-
     config = notmuch_conffile_open (notmuch,
 				    notmuch_config_path (notmuch), true);
     if (! config)
diff --git a/notmuch-show.c b/notmuch-show.c
index c8f1a40f..667fbee8 100644
--- a/notmuch-show.c
+++ b/notmuch-show.c
@@ -1287,7 +1287,7 @@ notmuch_show_command (notmuch_database_t *notmuch, int argc, char *argv[])
     if (opt_index < 0)
 	return EXIT_FAILURE;
 
-    notmuch_process_shared_options (argv[0]);
+    notmuch_process_shared_options (notmuch, argv[0]);
 
     /* explicit decryption implies verification */
     if (params.crypto.decrypt == NOTMUCH_DECRYPT_NOSTASH ||
@@ -1353,8 +1353,6 @@ notmuch_show_command (notmuch_database_t *notmuch, int argc, char *argv[])
 	}
     }
 
-    notmuch_exit_if_unmatched_db_uuid (notmuch);
-
     query_string = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
     if (query_string == NULL) {
 	fprintf (stderr, "Out of memory\n");
diff --git a/notmuch-tag.c b/notmuch-tag.c
index de428c8e..aa886032 100644
--- a/notmuch-tag.c
+++ b/notmuch-tag.c
@@ -220,7 +220,7 @@ notmuch_tag_command (notmuch_database_t *notmuch, int argc, char *argv[])
     if (opt_index < 0)
 	return EXIT_FAILURE;
 
-    notmuch_process_shared_options (argv[0]);
+    notmuch_process_shared_options (notmuch, argv[0]);
 
     if (input_file_name) {
 	batch = true;
@@ -261,8 +261,6 @@ notmuch_tag_command (notmuch_database_t *notmuch, int argc, char *argv[])
 	}
     }
 
-    notmuch_exit_if_unmatched_db_uuid (notmuch);
-
     if (print_status_database (
 	    "notmuch restore",
 	    notmuch,
diff --git a/notmuch.c b/notmuch.c
index 1404b70c..3824bf19 100644
--- a/notmuch.c
+++ b/notmuch.c
@@ -49,8 +49,11 @@ notmuch_command (notmuch_database_t *notmuch, int argc, char *argv[]);
 static int
 _help_for (const char *topic);
 
+static void
+notmuch_exit_if_unmatched_db_uuid (notmuch_database_t *notmuch);
+
 static bool print_version = false, print_help = false;
-const char *notmuch_requested_db_uuid = NULL;
+static const char *notmuch_requested_db_uuid = NULL;
 
 const notmuch_opt_desc_t notmuch_shared_options [] = {
     { .opt_bool = &print_version, .name = "version" },
@@ -61,10 +64,11 @@ const notmuch_opt_desc_t notmuch_shared_options [] = {
 
 /* any subcommand wanting to support these options should call
  * inherit notmuch_shared_options and call
- * notmuch_process_shared_options (subcommand_name);
+ * notmuch_process_shared_options (notmuch, subcommand_name);
+ * with notmuch = an open database, or NULL.
  */
 void
-notmuch_process_shared_options (const char *subcommand_name)
+notmuch_process_shared_options (notmuch_database_t *notmuch, const char *subcommand_name)
 {
     if (print_version) {
 	printf ("notmuch " STRINGIFY (NOTMUCH_VERSION) "\n");
@@ -75,6 +79,14 @@ notmuch_process_shared_options (const char *subcommand_name)
 	int ret = _help_for (subcommand_name);
 	exit (ret);
     }
+
+    if (notmuch) {
+	notmuch_exit_if_unmatched_db_uuid (notmuch);
+    } else {
+	if (notmuch_requested_db_uuid)
+	    fprintf (stderr, "Warning: ignoring --uuid=%s\n",
+		     notmuch_requested_db_uuid);
+    }
 }
 
 /* This is suitable for subcommands that do not actually open the
@@ -97,7 +109,7 @@ notmuch_minimal_options (const char *subcommand_name,
 	return -1;
 
     /* We can't use argv here as it is sometimes NULL */
-    notmuch_process_shared_options (subcommand_name);
+    notmuch_process_shared_options (NULL, subcommand_name);
     return opt_index;
 }
 
@@ -280,7 +292,7 @@ be supported in the future.\n", notmuch_format_version);
     }
 }
 
-void
+static void
 notmuch_exit_if_unmatched_db_uuid (notmuch_database_t *notmuch)
 {
     const char *uuid = NULL;
@@ -480,7 +492,7 @@ main (int argc, char *argv[])
     if (opt_index < argc)
 	command_name = argv[opt_index];
 
-    notmuch_process_shared_options (command_name);
+    notmuch_process_shared_options (NULL, command_name);
 
     command = find_command (command_name);
     /* if command->function is NULL, try external command */
diff --git a/test/random-corpus.c b/test/random-corpus.c
index 7cde22c5..8ae08971 100644
--- a/test/random-corpus.c
+++ b/test/random-corpus.c
@@ -122,7 +122,8 @@ const notmuch_opt_desc_t notmuch_shared_options[] = {
 const char *notmuch_requested_db_uuid = NULL;
 
 void
-notmuch_process_shared_options (unused (const char *dummy))
+notmuch_process_shared_options (unused (notmuch_database_t *notmuch),
+				unused (const char *dummy))
 {
 }
 
-- 
2.32.0

  reply	other threads:[~2021-08-24 15:22 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-24 15:17 v5 sexp query parser David Bremner
2021-08-24 15:17 ` David Bremner [this message]
2021-08-24 15:17 ` [PATCH 02/36] configure: optional library sfsexp David Bremner
2021-08-24 15:17 ` [PATCH 03/36] lib: split notmuch_query_create David Bremner
2021-08-24 15:17 ` [PATCH 04/36] lib: define notmuch_query_create_with_syntax David Bremner
2021-08-24 15:17 ` [PATCH 05/36] CLI/search+address: support sexpr queries David Bremner
2021-08-24 15:17 ` [PATCH 06/36] lib: add new status code for query syntax errors David Bremner
2021-08-24 15:17 ` [PATCH 07/36] lib/parse-sexp: parse single terms and the empty list David Bremner
2021-08-24 15:17 ` [PATCH 08/36] lib: leave stemmer object accessible David Bremner
2021-08-24 15:17 ` [PATCH 09/36] lib/parse-sexp: stem unquoted atoms David Bremner
2021-08-24 15:17 ` [PATCH 10/36] lib/parse-sexp: support and, not, and or David Bremner
2021-08-24 15:17 ` [PATCH 11/36] lib/parse-sexp: support subject field David Bremner
2021-08-24 15:17 ` [PATCH 12/36] util/unicode: allow calling from C++ David Bremner
2021-08-24 15:17 ` [PATCH 13/36] lib/parse-sexp: support phrase queries David Bremner
2021-08-24 15:17 ` [PATCH 14/36] lib/parse-sexp: add term prefix backed fields David Bremner
2021-08-24 15:17 ` [PATCH 15/36] lib/parse-sexp: 'starts-with' wildcard searches David Bremner
2021-08-24 15:17 ` [PATCH 16/36] lib/parse-sexp: add '*' as syntactic sugar for '(starts-with "")' David Bremner
2021-08-24 15:17 ` [PATCH 17/36] lib/parse-sexp: handle unprefixed terms David Bremner
2021-08-24 15:17 ` [PATCH 18/36] lib/query: generalize exclude handling to s-expression queries David Bremner
2021-08-24 15:17 ` [PATCH 19/36] lib: factor out query construction from regexp David Bremner
2021-08-24 15:17 ` [PATCH 20/36] lib/parse-sexp: support regular expressions David Bremner
2021-08-24 15:17 ` [PATCH 21/36] lib: generate actual Xapian query for "*" and "" David Bremner
2021-08-24 15:17 ` [PATCH 22/36] lib/query: factor out _notmuch_query_string_to_xapian_query David Bremner
2021-08-24 15:17 ` [PATCH 23/36] lib/thread-fp: factor out query expansion, rewrite in Xapian David Bremner
2021-08-24 15:17 ` [PATCH 24/36] lib/parse-sexp: expand queries David Bremner
2021-08-24 15:17 ` [PATCH 25/36] lib/parse-sexp: support infix subqueries David Bremner
2021-08-24 15:17 ` [PATCH 26/36] lib/parse-sexp: parse user headers David Bremner
2021-08-24 15:17 ` [PATCH 27/36] lib: factor out expansion of saved queries David Bremner
2021-08-24 15:17 ` [PATCH 28/36] lib/parse-sexp: handle " David Bremner
2021-08-24 15:17 ` [PATCH 29/36] CLI/config support saving s-expression queries David Bremner
2021-08-24 15:17 ` [PATCH 30/36] lib/parse-sexp: support saved " David Bremner
2021-08-24 15:17 ` [PATCH 31/36] lib/parse-sexp: thread environment argument through parser David Bremner
2021-08-24 15:17 ` [PATCH 32/36] lib/parse-sexp: apply macros David Bremner
2021-08-24 15:17 ` [PATCH 33/36] CLI: move query syntax to shared option David Bremner
2021-08-24 15:17 ` [PATCH 34/36] CLI/{count, dump, reindex, reply, show}: enable sexp queries David Bremner
2021-08-24 15:17 ` [PATCH 35/36] CLI/tag: " David Bremner
2021-08-24 15:17 ` [PATCH 36/36] doc/sexp-queries: update synopsis and description David Bremner
2021-09-05 19:31 ` v5 sexp query parser David Bremner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210824151745.2941868-2-david@tethera.net \
    --to=david@tethera.net \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).