unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
* [PATCH v2 0/6] notmuch cli config changes
@ 2013-03-03 21:55 Jani Nikula
  2013-03-03 21:55 ` [PATCH v2 1/6] cli: abstract subcommand finding into a new function Jani Nikula
                   ` (6 more replies)
  0 siblings, 7 replies; 11+ messages in thread
From: Jani Nikula @ 2013-03-03 21:55 UTC (permalink / raw)
  To: notmuch

This is v2 of id:cover.1359495450.git.jani@nikula.org, being mostly just
rebase and commit message amendments, and a slight reordering of a few
patches. The only functional change is to allow notmuch help to be run
without a config file which was broken in v1.

BR,
Jani.


Jani Nikula (6):
  cli: abstract subcommand finding into a new function
  cli: plug main notmuch command into subcommand machinery
  cli: config: keep track of whether the config is newly created
  cli: config: make notmuch_config_open() "is new" parameter input only
  cli: move config open/close to main() from subcommands
  cli: add top level --config=FILE option

 notmuch-client.h     |   35 ++++++------
 notmuch-config.c     |   73 +++++++++----------------
 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      |   22 ++++----
 notmuch-show.c       |   15 ++----
 notmuch-tag.c        |   15 ++----
 notmuch.c            |  144 +++++++++++++++++++++++++++-----------------------
 test/random-corpus.c |    2 +-
 13 files changed, 162 insertions(+), 220 deletions(-)

-- 
1.7.10.4

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v2 1/6] cli: abstract subcommand finding into a new function
  2013-03-03 21:55 [PATCH v2 0/6] notmuch cli config changes Jani Nikula
@ 2013-03-03 21:55 ` Jani Nikula
  2013-03-03 21:55 ` [PATCH v2 2/6] cli: plug main notmuch command into subcommand machinery Jani Nikula
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 11+ messages in thread
From: Jani Nikula @ 2013-03-03 21:55 UTC (permalink / raw)
  To: notmuch

Clean up code.
---
 notmuch.c |   78 +++++++++++++++++++++++++++++++------------------------------
 1 file changed, 40 insertions(+), 38 deletions(-)

diff --git a/notmuch.c b/notmuch.c
index cfb009b..3dff6d4 100644
--- a/notmuch.c
+++ b/notmuch.c
@@ -70,6 +70,18 @@ static command_t commands[] = {
       "This message, or more detailed help for the named command." }
 };
 
+static command_t *
+find_command (const char *name)
+{
+    size_t i;
+
+    for (i = 0; i < ARRAY_SIZE (commands); i++)
+	if (strcmp (name, commands[i].name) == 0)
+	    return &commands[i];
+
+    return NULL;
+}
+
 int notmuch_format_version;
 
 static void
@@ -139,7 +151,6 @@ static int
 notmuch_help_command (void *ctx, int argc, char *argv[])
 {
     command_t *command;
-    unsigned int i;
 
     argc--; argv++; /* Ignore "help" */
 
@@ -158,13 +169,10 @@ notmuch_help_command (void *ctx, int argc, char *argv[])
 	return 0;
     }
 
-    for (i = 0; i < ARRAY_SIZE (commands); i++) {
-	command = &commands[i];
-
-	if (strcmp (argv[0], command->name) == 0) {
-	    char *page = talloc_asprintf (ctx, "notmuch-%s", command->name);
-	    exec_man (page);
-	}
+    command = find_command (argv[0]);
+    if (command) {
+	char *page = talloc_asprintf (ctx, "notmuch-%s", command->name);
+	exec_man (page);
     }
 
     if (strcmp (argv[0], "search-terms") == 0) {
@@ -247,10 +255,11 @@ int
 main (int argc, char *argv[])
 {
     void *local;
+    char *talloc_report;
     command_t *command;
-    unsigned int i;
     notmuch_bool_t print_help=FALSE, print_version=FALSE;
     int opt_index;
+    int ret = 0;
 
     notmuch_opt_desc_t options[] = {
 	{ NOTMUCH_OPT_BOOLEAN, &print_help, "help", 'h', 0 },
@@ -285,39 +294,32 @@ main (int argc, char *argv[])
 	return 0;
     }
 
-    for (i = 0; i < ARRAY_SIZE (commands); i++) {
-	command = &commands[i];
-
-	if (strcmp (argv[opt_index], command->name) == 0) {
-	    int ret;
-	    char *talloc_report;
-
-	    ret = (command->function)(local, argc - opt_index, argv + opt_index);
-
-	    talloc_report = getenv ("NOTMUCH_TALLOC_REPORT");
-
-	    /* this relies on the previous call to
-	     * talloc_enable_null_tracking */
-
-	    if (talloc_report && strcmp (talloc_report, "") != 0) {
-		FILE *report = fopen (talloc_report, "w");
-		if (report) {
-		    talloc_report_full (NULL, report);
-		} else {
-		    ret = 1;
-		    fprintf (stderr, "ERROR: unable to write talloc log. ");
-		    perror (talloc_report);
-		}
-	    }
+    command = find_command (argv[opt_index]);
+    if (!command) {
+	fprintf (stderr, "Error: Unknown command '%s' (see \"notmuch help\")\n",
+		 argv[opt_index]);
+	return 1;
+    }
 
-	    return ret;
+    ret = (command->function)(local, argc - opt_index, argv + opt_index);
+
+    talloc_report = getenv ("NOTMUCH_TALLOC_REPORT");
+    if (talloc_report && strcmp (talloc_report, "") != 0) {
+	/* this relies on the previous call to
+	 * talloc_enable_null_tracking
+	 */
+
+	FILE *report = fopen (talloc_report, "w");
+	if (report) {
+	    talloc_report_full (NULL, report);
+	} else {
+	    ret = 1;
+	    fprintf (stderr, "ERROR: unable to write talloc log. ");
+	    perror (talloc_report);
 	}
     }
 
-    fprintf (stderr, "Error: Unknown command '%s' (see \"notmuch help\")\n",
-	     argv[1]);
-
     talloc_free (local);
 
-    return 1;
+    return ret;
 }
-- 
1.7.10.4

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v2 2/6] cli: plug main notmuch command into subcommand machinery
  2013-03-03 21:55 [PATCH v2 0/6] notmuch cli config changes Jani Nikula
  2013-03-03 21:55 ` [PATCH v2 1/6] cli: abstract subcommand finding into a new function Jani Nikula
@ 2013-03-03 21:55 ` Jani Nikula
  2013-03-03 21:55 ` [PATCH v2 3/6] cli: config: keep track of whether the config is newly created Jani Nikula
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 11+ messages in thread
From: Jani Nikula @ 2013-03-03 21:55 UTC (permalink / raw)
  To: notmuch

This allows top level arguments to be added to notmuch in a way that
doesn't require special handling for the plain notmuch command without
a subcommand.
---
 notmuch.c |   26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)

diff --git a/notmuch.c b/notmuch.c
index 3dff6d4..ec2f20f 100644
--- a/notmuch.c
+++ b/notmuch.c
@@ -34,7 +34,13 @@ typedef struct command {
 static int
 notmuch_help_command (void *ctx, int argc, char *argv[]);
 
+static int
+notmuch_command (void *ctx, int argc, char *argv[]);
+
 static command_t commands[] = {
+    { NULL, notmuch_command,
+      NULL,
+      "Notmuch main command." },
     { "setup", notmuch_setup_command,
       NULL,
       "Interactively setup notmuch for first use." },
@@ -76,7 +82,8 @@ find_command (const char *name)
     size_t i;
 
     for (i = 0; i < ARRAY_SIZE (commands); i++)
-	if (strcmp (name, commands[i].name) == 0)
+	if ((!name && !commands[i].name) ||
+	    (name && commands[i].name && strcmp (name, commands[i].name) == 0))
 	    return &commands[i];
 
     return NULL;
@@ -101,8 +108,8 @@ usage (FILE *out)
     for (i = 0; i < ARRAY_SIZE (commands); i++) {
 	command = &commands[i];
 
-	fprintf (out, "  %-11s  %s\n",
-		 command->name, command->summary);
+	if (command->name)
+	    fprintf (out, "  %-11s  %s\n", command->name, command->summary);
     }
 
     fprintf (out, "\n");
@@ -192,7 +199,7 @@ notmuch_help_command (void *ctx, int argc, char *argv[])
  * to be more clever about this in the future.
  */
 static int
-notmuch (void *ctx)
+notmuch_command (void *ctx, unused(int argc), unused(char *argv[]))
 {
     notmuch_config_t *config;
     notmuch_bool_t is_new;
@@ -256,6 +263,7 @@ main (int argc, char *argv[])
 {
     void *local;
     char *talloc_report;
+    const char *command_name = NULL;
     command_t *command;
     notmuch_bool_t print_help=FALSE, print_version=FALSE;
     int opt_index;
@@ -277,9 +285,6 @@ main (int argc, char *argv[])
     /* Globally default to the current output format version. */
     notmuch_format_version = NOTMUCH_FORMAT_CUR;
 
-    if (argc == 1)
-	return notmuch (local);
-
     opt_index = parse_arguments (argc, argv, options, 1);
     if (opt_index < 0) {
 	/* diagnostics already printed */
@@ -294,10 +299,13 @@ main (int argc, char *argv[])
 	return 0;
     }
 
-    command = find_command (argv[opt_index]);
+    if (opt_index < argc)
+	command_name = argv[opt_index];
+
+    command = find_command (command_name);
     if (!command) {
 	fprintf (stderr, "Error: Unknown command '%s' (see \"notmuch help\")\n",
-		 argv[opt_index]);
+		 command_name);
 	return 1;
     }
 
-- 
1.7.10.4

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v2 3/6] cli: config: keep track of whether the config is newly created
  2013-03-03 21:55 [PATCH v2 0/6] notmuch cli config changes Jani Nikula
  2013-03-03 21:55 ` [PATCH v2 1/6] cli: abstract subcommand finding into a new function Jani Nikula
  2013-03-03 21:55 ` [PATCH v2 2/6] cli: plug main notmuch command into subcommand machinery Jani Nikula
@ 2013-03-03 21:55 ` Jani Nikula
  2013-03-03 21:55 ` [PATCH v2 4/6] cli: config: make notmuch_config_open() "is new" parameter input only Jani Nikula
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 11+ messages in thread
From: Jani Nikula @ 2013-03-03 21:55 UTC (permalink / raw)
  To: notmuch

Keep track of whether the config is newly created, and add
notmuch_config_is_new() accessor function to query this.

This is to support anyone with a config handle to check this, instead
of just whoever called notmuch_config_open().
---
 notmuch-client.h |    3 +++
 notmuch-config.c |   11 +++++++++++
 2 files changed, 14 insertions(+)

diff --git a/notmuch-client.h b/notmuch-client.h
index 5f28836..07367e0 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -256,6 +256,9 @@ notmuch_config_close (notmuch_config_t *config);
 int
 notmuch_config_save (notmuch_config_t *config);
 
+notmuch_bool_t
+notmuch_config_is_new (notmuch_config_t *config);
+
 const char *
 notmuch_config_get_database_path (notmuch_config_t *config);
 
diff --git a/notmuch-config.c b/notmuch-config.c
index b5c2066..e733e92 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -104,6 +104,7 @@ static const char search_config_comment[] =
 struct _notmuch_config {
     char *filename;
     GKeyFile *key_file;
+    notmuch_bool_t is_new;
 
     char *database_path;
     char *user_name;
@@ -266,6 +267,7 @@ notmuch_config_open (void *ctx,
 
     config->key_file = g_key_file_new ();
 
+    config->is_new = FALSE;
     config->database_path = NULL;
     config->user_name = NULL;
     config->user_primary_email = NULL;
@@ -435,6 +437,8 @@ notmuch_config_open (void *ctx,
     if (is_new_ret)
 	*is_new_ret = is_new;
 
+    config->is_new = is_new;
+
     return config;
 }
 
@@ -482,6 +486,13 @@ notmuch_config_save (notmuch_config_t *config)
     return 0;
 }
 
+notmuch_bool_t
+notmuch_config_is_new (notmuch_config_t *config)
+{
+    return config->is_new;
+}
+
+
 static const char **
 _config_get_list (notmuch_config_t *config,
 		  const char *section, const char *key,
-- 
1.7.10.4

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v2 4/6] cli: config: make notmuch_config_open() "is new" parameter input only
  2013-03-03 21:55 [PATCH v2 0/6] notmuch cli config changes Jani Nikula
                   ` (2 preceding siblings ...)
  2013-03-03 21:55 ` [PATCH v2 3/6] cli: config: keep track of whether the config is newly created Jani Nikula
@ 2013-03-03 21:55 ` Jani Nikula
  2013-03-07 13:44   ` David Bremner
  2013-03-03 21:55 ` [PATCH v2 5/6] cli: move config open/close to main() from subcommands Jani Nikula
                   ` (2 subsequent siblings)
  6 siblings, 1 reply; 11+ messages in thread
From: Jani Nikula @ 2013-03-03 21:55 UTC (permalink / raw)
  To: notmuch

We now have a notmuch_config_is_new() function to query whether a
config was created or not. Change the notmuch_config_open() is_new
parameter into boolean create_new to determine whether the function
should create a new config if one doesn't exist. This reduces the
complexity of the API.
---
 notmuch-client.h     |    2 +-
 notmuch-config.c     |   32 +++++++++++---------------------
 notmuch-count.c      |    2 +-
 notmuch-dump.c       |    2 +-
 notmuch-new.c        |    2 +-
 notmuch-reply.c      |    2 +-
 notmuch-restore.c    |    2 +-
 notmuch-search.c     |    2 +-
 notmuch-setup.c      |    7 +++----
 notmuch-show.c       |    2 +-
 notmuch-tag.c        |    2 +-
 notmuch.c            |    5 ++---
 test/random-corpus.c |    2 +-
 13 files changed, 26 insertions(+), 38 deletions(-)

diff --git a/notmuch-client.h b/notmuch-client.h
index 07367e0..b3dcb21 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -248,7 +248,7 @@ typedef struct _notmuch_config notmuch_config_t;
 notmuch_config_t *
 notmuch_config_open (void *ctx,
 		     const char *filename,
-		     notmuch_bool_t *is_new_ret);
+		     notmuch_bool_t create_new);
 
 void
 notmuch_config_close (notmuch_config_t *config);
diff --git a/notmuch-config.c b/notmuch-config.c
index e733e92..247fbe4 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -233,10 +233,9 @@ get_username_from_passwd_file (void *ctx)
 notmuch_config_t *
 notmuch_config_open (void *ctx,
 		     const char *filename,
-		     notmuch_bool_t *is_new_ret)
+		     notmuch_bool_t create_new)
 {
     GError *error = NULL;
-    int is_new = 0;
     size_t tmp;
     char *notmuch_config_env = NULL;
     int file_had_database_group;
@@ -245,9 +244,6 @@ notmuch_config_open (void *ctx,
     int file_had_maildir_group;
     int file_had_search_group;
 
-    if (is_new_ret)
-	*is_new_ret = 0;
-
     notmuch_config_t *config = talloc (ctx, notmuch_config_t);
     if (config == NULL) {
 	fprintf (stderr, "Out of memory.\n");
@@ -286,17 +282,16 @@ notmuch_config_open (void *ctx,
 				     G_KEY_FILE_KEEP_COMMENTS,
 				     &error))
     {
-	/* If the caller passed a non-NULL value for is_new_ret, then
-	 * the caller is prepared for a default configuration file in
-	 * the case of FILE NOT FOUND. Otherwise, any read failure is
-	 * an error.
+	/* If create_new is true, then the caller is prepared for a
+	 * default configuration file in the case of FILE NOT
+	 * FOUND. Otherwise, any read failure is an error.
 	 */
-	if (is_new_ret &&
+	if (create_new &&
 	    error->domain == G_FILE_ERROR &&
 	    error->code == G_FILE_ERROR_NOENT)
 	{
 	    g_error_free (error);
-	    is_new = 1;
+	    config->is_new = TRUE;
 	}
 	else
 	{
@@ -379,7 +374,7 @@ notmuch_config_open (void *ctx,
     }
 
     if (notmuch_config_get_search_exclude_tags (config, &tmp) == NULL) {
-	if (is_new) {
+	if (config->is_new) {
 	    const char *tags[] = { "deleted", "spam" };
 	    notmuch_config_set_search_exclude_tags (config, tags, 2);
 	} else {
@@ -399,7 +394,7 @@ notmuch_config_open (void *ctx,
     /* Whenever we know of configuration sections that don't appear in
      * the configuration file, we add some comments to help the user
      * understand what can be done. */
-    if (is_new)
+    if (config->is_new)
     {
 	g_key_file_set_comment (config->key_file, NULL, NULL,
 				toplevel_config_comment, NULL);
@@ -434,11 +429,6 @@ notmuch_config_open (void *ctx,
 				search_config_comment, NULL);
     }
 
-    if (is_new_ret)
-	*is_new_ret = is_new;
-
-    config->is_new = is_new;
-
     return config;
 }
 
@@ -719,7 +709,7 @@ notmuch_config_command_get (void *ctx, char *item)
 {
     notmuch_config_t *config;
 
-    config = notmuch_config_open (ctx, NULL, NULL);
+    config = notmuch_config_open (ctx, NULL, FALSE);
     if (config == NULL)
 	return 1;
 
@@ -781,7 +771,7 @@ notmuch_config_command_set (void *ctx, char *item, int argc, char *argv[])
     if (_item_split (item, &group, &key))
 	return 1;
 
-    config = notmuch_config_open (ctx, NULL, NULL);
+    config = notmuch_config_open (ctx, NULL, FALSE);
     if (config == NULL)
 	return 1;
 
@@ -818,7 +808,7 @@ notmuch_config_command_list (void *ctx)
     char **groups;
     size_t g, groups_length;
 
-    config = notmuch_config_open (ctx, NULL, NULL);
+    config = notmuch_config_open (ctx, NULL, FALSE);
     if (config == NULL)
 	return 1;
 
diff --git a/notmuch-count.c b/notmuch-count.c
index 2f98128..61722ed 100644
--- a/notmuch-count.c
+++ b/notmuch-count.c
@@ -62,7 +62,7 @@ notmuch_count_command (void *ctx, int argc, char *argv[])
 	return 1;
     }
 
-    config = notmuch_config_open (ctx, NULL, NULL);
+    config = notmuch_config_open (ctx, NULL, FALSE);
     if (config == NULL)
 	return 1;
 
diff --git a/notmuch-dump.c b/notmuch-dump.c
index a3244e0..845a67e 100644
--- a/notmuch-dump.c
+++ b/notmuch-dump.c
@@ -34,7 +34,7 @@ notmuch_dump_command (unused (void *ctx), int argc, char *argv[])
     notmuch_tags_t *tags;
     const char *query_str = "";
 
-    config = notmuch_config_open (ctx, NULL, NULL);
+    config = notmuch_config_open (ctx, NULL, FALSE);
     if (config == NULL)
 	return 1;
 
diff --git a/notmuch-new.c b/notmuch-new.c
index feb9c32..4915418 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -875,7 +875,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
 	return 1;
     }
 
-    config = notmuch_config_open (ctx, NULL, NULL);
+    config = notmuch_config_open (ctx, NULL, FALSE);
     if (config == NULL)
 	return 1;
 
diff --git a/notmuch-reply.c b/notmuch-reply.c
index 22c58ff..9da42b9 100644
--- a/notmuch-reply.c
+++ b/notmuch-reply.c
@@ -762,7 +762,7 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])
 
     notmuch_exit_if_unsupported_format ();
 
-    config = notmuch_config_open (ctx, NULL, NULL);
+    config = notmuch_config_open (ctx, NULL, FALSE);
     if (config == NULL)
 	return 1;
 
diff --git a/notmuch-restore.c b/notmuch-restore.c
index cf26a42..dd2507f 100644
--- a/notmuch-restore.c
+++ b/notmuch-restore.c
@@ -139,7 +139,7 @@ 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, NULL);
+    config = notmuch_config_open (ctx, NULL, FALSE);
     if (config == NULL)
 	return 1;
 
diff --git a/notmuch-search.c b/notmuch-search.c
index 0b0a879..fac6663 100644
--- a/notmuch-search.c
+++ b/notmuch-search.c
@@ -371,7 +371,7 @@ notmuch_search_command (void *ctx, int argc, char *argv[])
 
     notmuch_exit_if_unsupported_format ();
 
-    config = notmuch_config_open (ctx, NULL, NULL);
+    config = notmuch_config_open (ctx, NULL, FALSE);
     if (config == NULL)
 	return 1;
 
diff --git a/notmuch-setup.c b/notmuch-setup.c
index 94d0aa7..72d862a 100644
--- a/notmuch-setup.c
+++ b/notmuch-setup.c
@@ -130,7 +130,6 @@ notmuch_setup_command (unused (void *ctx),
     size_t old_other_emails_len;
     GPtrArray *other_emails;
     unsigned int i;
-    int is_new;
     const char **new_tags;
     size_t new_tags_len;
     const char **search_exclude_tags;
@@ -147,9 +146,9 @@ notmuch_setup_command (unused (void *ctx),
 	chomp_newline (response);				\
     } while (0)
 
-    config = notmuch_config_open (ctx, NULL, &is_new);
+    config = notmuch_config_open (ctx, NULL, TRUE);
 
-    if (is_new)
+    if (notmuch_config_is_new (config))
 	welcome_message_pre_setup ();
 
     prompt ("Your full name [%s]: ", notmuch_config_get_user_name (config));
@@ -229,7 +228,7 @@ notmuch_setup_command (unused (void *ctx),
 
 
     if (! notmuch_config_save (config)) {
-	if (is_new)
+	if (notmuch_config_is_new (config))
 	  welcome_message_post_setup ();
 	return 0;
     } else {
diff --git a/notmuch-show.c b/notmuch-show.c
index cbfc2d1..5ae5d7d 100644
--- a/notmuch-show.c
+++ b/notmuch-show.c
@@ -1176,7 +1176,7 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
     else
 	params.entire_thread = FALSE;
 
-    config = notmuch_config_open (ctx, NULL, NULL);
+    config = notmuch_config_open (ctx, NULL, FALSE);
     if (config == NULL)
 	return 1;
 
diff --git a/notmuch-tag.c b/notmuch-tag.c
index d9daf8f..148e856 100644
--- a/notmuch-tag.c
+++ b/notmuch-tag.c
@@ -236,7 +236,7 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])
 	    return 1;
     }
 
-    config = notmuch_config_open (ctx, NULL, NULL);
+    config = notmuch_config_open (ctx, NULL, FALSE);
     if (config == NULL)
 	return 1;
 
diff --git a/notmuch.c b/notmuch.c
index ec2f20f..e434d03 100644
--- a/notmuch.c
+++ b/notmuch.c
@@ -202,16 +202,15 @@ static int
 notmuch_command (void *ctx, unused(int argc), unused(char *argv[]))
 {
     notmuch_config_t *config;
-    notmuch_bool_t is_new;
     char *db_path;
     struct stat st;
 
-    config = notmuch_config_open (ctx, NULL, &is_new);
+    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 (is_new) {
+    if (notmuch_config_is_new (config)) {
 	notmuch_config_close (config);
 	return notmuch_setup_command (ctx, 0, NULL);
     }
diff --git a/test/random-corpus.c b/test/random-corpus.c
index 8b7748e..790193d 100644
--- a/test/random-corpus.c
+++ b/test/random-corpus.c
@@ -160,7 +160,7 @@ main (int argc, char **argv)
 	exit (1);
     }
 
-    config = notmuch_config_open (ctx, config_path, NULL);
+    config = notmuch_config_open (ctx, config_path, FALSE);
     if (config == NULL)
 	return 1;
 
-- 
1.7.10.4

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v2 5/6] cli: move config open/close to main() from subcommands
  2013-03-03 21:55 [PATCH v2 0/6] notmuch cli config changes Jani Nikula
                   ` (3 preceding siblings ...)
  2013-03-03 21:55 ` [PATCH v2 4/6] cli: config: make notmuch_config_open() "is new" parameter input only Jani Nikula
@ 2013-03-03 21:55 ` Jani Nikula
  2013-03-08 12:05   ` David Bremner
  2013-03-03 21:55 ` [PATCH v2 6/6] cli: add top level --config=FILE option Jani Nikula
  2013-03-04 19:53 ` [PATCH] man: document the notmuch --config=FILE global option Jani Nikula
  6 siblings, 1 reply; 11+ messages in thread
From: Jani Nikula @ 2013-03-03 21:55 UTC (permalink / raw)
  To: notmuch

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, &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 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-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, TRUE, /* create but don't save config */
       "[<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);
 
     talloc_report = getenv ("NOTMUCH_TALLOC_REPORT");
     if (talloc_report && strcmp (talloc_report, "") != 0) {
-- 
1.7.10.4

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v2 6/6] cli: add top level --config=FILE option
  2013-03-03 21:55 [PATCH v2 0/6] notmuch cli config changes Jani Nikula
                   ` (4 preceding siblings ...)
  2013-03-03 21:55 ` [PATCH v2 5/6] cli: move config open/close to main() from subcommands Jani Nikula
@ 2013-03-03 21:55 ` Jani Nikula
  2013-03-04 19:53 ` [PATCH] man: document the notmuch --config=FILE global option Jani Nikula
  6 siblings, 0 replies; 11+ messages in thread
From: Jani Nikula @ 2013-03-03 21:55 UTC (permalink / raw)
  To: notmuch

Let the user specify the config file on the command line.
---
 notmuch.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/notmuch.c b/notmuch.c
index 3241e23..f51a84f 100644
--- a/notmuch.c
+++ b/notmuch.c
@@ -258,6 +258,7 @@ main (int argc, char *argv[])
     char *talloc_report;
     const char *command_name = NULL;
     command_t *command;
+    char *config_file_name = NULL;
     notmuch_config_t *config;
     notmuch_bool_t print_help=FALSE, print_version=FALSE;
     int opt_index;
@@ -266,6 +267,7 @@ main (int argc, char *argv[])
     notmuch_opt_desc_t options[] = {
 	{ NOTMUCH_OPT_BOOLEAN, &print_help, "help", 'h', 0 },
 	{ NOTMUCH_OPT_BOOLEAN, &print_version, "version", 'v', 0 },
+	{ NOTMUCH_OPT_STRING, &config_file_name, "config", 'c', 0 },
 	{ 0, 0, 0, 0, 0 }
     };
 
@@ -303,7 +305,7 @@ main (int argc, char *argv[])
 	return 1;
     }
 
-    config = notmuch_config_open (local, NULL, command->create_config);
+    config = notmuch_config_open (local, config_file_name, command->create_config);
     if (!config)
 	return 1;
 
-- 
1.7.10.4

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH] man: document the notmuch --config=FILE global option
  2013-03-03 21:55 [PATCH v2 0/6] notmuch cli config changes Jani Nikula
                   ` (5 preceding siblings ...)
  2013-03-03 21:55 ` [PATCH v2 6/6] cli: add top level --config=FILE option Jani Nikula
@ 2013-03-04 19:53 ` Jani Nikula
  6 siblings, 0 replies; 11+ messages in thread
From: Jani Nikula @ 2013-03-04 19:53 UTC (permalink / raw)
  To: notmuch

---
 man/man1/notmuch.1 |    9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/man/man1/notmuch.1 b/man/man1/notmuch.1
index e645a35..923fefe 100644
--- a/man/man1/notmuch.1
+++ b/man/man1/notmuch.1
@@ -70,6 +70,15 @@ Print a synopsis of available commands and exit.
 Print the installed version of notmuch, and exit.
 .RE
 
+.RS 4
+.TP 4
+.B \-\-config=FILE
+
+Specify the configuration file to use. This overrides any
+configuration file specified by ${NOTMUCH_CONFIG}.
+
+.RE
+
 .SH COMMANDS
 
 
-- 
1.7.10.4

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 4/6] cli: config: make notmuch_config_open() "is new" parameter input only
  2013-03-03 21:55 ` [PATCH v2 4/6] cli: config: make notmuch_config_open() "is new" parameter input only Jani Nikula
@ 2013-03-07 13:44   ` David Bremner
  2013-03-07 14:38     ` Tomi Ollila
  0 siblings, 1 reply; 11+ messages in thread
From: David Bremner @ 2013-03-07 13:44 UTC (permalink / raw)
  To: Jani Nikula, notmuch

Jani Nikula <jani@nikula.org> writes:

> We now have a notmuch_config_is_new() function to query whether a
> config was created or not. Change the notmuch_config_open() is_new
> parameter into boolean create_new to determine whether the function
> should create a new config if one doesn't exist. This reduces the
> complexity of the API.

I have pushed the first 4 patches in this series. I'd like a second
(third?) pair of eyes on 5/6.

d

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 4/6] cli: config: make notmuch_config_open() "is new" parameter input only
  2013-03-07 13:44   ` David Bremner
@ 2013-03-07 14:38     ` Tomi Ollila
  0 siblings, 0 replies; 11+ messages in thread
From: Tomi Ollila @ 2013-03-07 14:38 UTC (permalink / raw)
  To: David Bremner, Jani Nikula, notmuch

On Thu, Mar 07 2013, David Bremner <david@tethera.net> wrote:

> Jani Nikula <jani@nikula.org> writes:
>
>> We now have a notmuch_config_is_new() function to query whether a
>> config was created or not. Change the notmuch_config_open() is_new
>> parameter into boolean create_new to determine whether the function
>> should create a new config if one doesn't exist. This reduces the
>> complexity of the API.
>
> I have pushed the first 4 patches in this series. I'd like a second
> (third?) pair of eyes on 5/6.

The changes in 5/6 (and 6/6 & manpage) LGTM. Using config for the context
is good. The reply context of this mail has been produced by notmuch
with these patches applied.

> d

Tomi

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 5/6] cli: move config open/close to main() from subcommands
  2013-03-03 21:55 ` [PATCH v2 5/6] cli: move config open/close to main() from subcommands Jani Nikula
@ 2013-03-08 12:05   ` David Bremner
  0 siblings, 0 replies; 11+ messages in thread
From: David Bremner @ 2013-03-08 12:05 UTC (permalink / raw)
  To: Jani Nikula, notmuch

Jani Nikula <jani@nikula.org> writes:

> 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.
>

I pushed patchs 5 through 7 of this 6 patch series.

- I'll see if I can figure out the elisp needed to work around the
  "Tramp stomped on my environment variables" problem.

- Is this NEWS worthy? 

- What about a unit test?

- What about updating the tests to use this feature, is that a good idea? 


d

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2013-03-08 12:05 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-03-03 21:55 [PATCH v2 0/6] notmuch cli config changes Jani Nikula
2013-03-03 21:55 ` [PATCH v2 1/6] cli: abstract subcommand finding into a new function Jani Nikula
2013-03-03 21:55 ` [PATCH v2 2/6] cli: plug main notmuch command into subcommand machinery Jani Nikula
2013-03-03 21:55 ` [PATCH v2 3/6] cli: config: keep track of whether the config is newly created Jani Nikula
2013-03-03 21:55 ` [PATCH v2 4/6] cli: config: make notmuch_config_open() "is new" parameter input only Jani Nikula
2013-03-07 13:44   ` David Bremner
2013-03-07 14:38     ` Tomi Ollila
2013-03-03 21:55 ` [PATCH v2 5/6] cli: move config open/close to main() from subcommands Jani Nikula
2013-03-08 12:05   ` David Bremner
2013-03-03 21:55 ` [PATCH v2 6/6] cli: add top level --config=FILE option Jani Nikula
2013-03-04 19:53 ` [PATCH] man: document the notmuch --config=FILE global option Jani Nikula

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).