unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Jani Nikula <jani@nikula.org>
To: notmuch@notmuchmail.org
Subject: [PATCH v2 1/6] cli: abstract subcommand finding into a new function
Date: Sun,  3 Mar 2013 23:55:05 +0200	[thread overview]
Message-ID: <a3d3c306c0c1f0d118819e4cfd143a4bc1f8457e.1362347362.git.jani@nikula.org> (raw)
In-Reply-To: <cover.1362347362.git.jani@nikula.org>
In-Reply-To: <cover.1362347362.git.jani@nikula.org>

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

  reply	other threads:[~2013-03-03 21:55 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-03 21:55 [PATCH v2 0/6] notmuch cli config changes Jani Nikula
2013-03-03 21:55 ` Jani Nikula [this message]
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

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=a3d3c306c0c1f0d118819e4cfd143a4bc1f8457e.1362347362.git.jani@nikula.org \
    --to=jani@nikula.org \
    --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).