--- /dev/null
+Return-Path: <jani@nikula.org>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id E85CA431FC0\r
+ for <notmuch@notmuchmail.org>; Tue, 29 Jan 2013 13:46:51 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+ tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id 4MM6nB70lV-X for <notmuch@notmuchmail.org>;\r
+ Tue, 29 Jan 2013 13:46:51 -0800 (PST)\r
+Received: from mail-la0-f45.google.com (mail-la0-f45.google.com\r
+ [209.85.215.45]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 43325431FC7\r
+ for <notmuch@notmuchmail.org>; Tue, 29 Jan 2013 13:46:50 -0800 (PST)\r
+Received: by mail-la0-f45.google.com with SMTP id er20so647502lab.18\r
+ for <notmuch@notmuchmail.org>; Tue, 29 Jan 2013 13:46:48 -0800 (PST)\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+ d=google.com; s=20120113;\r
+ h=x-received:from:to:subject:date:message-id:x-mailer:in-reply-to\r
+ :references:in-reply-to:references:x-gm-message-state;\r
+ bh=4qDQAjZfBxw+vY+TowfikkHP/5T9Zn3JBgUl8IWByd0=;\r
+ b=LxS8Q186Dv8+exSpgYvTTIxH9pHaTGgrhm3X6ltjeOlbcc2KKkcynb4f4suhIaLnNm\r
+ CZvf1twbcaXWQjN4tYe84xdwN7GrfraJWEvLnyZ9FFBS0ek7P0gGChbCl1Ajqh1EPNac\r
+ GuPoUSLJeCnaQhfCvifYVj6HSd/ghQ194qZzKJ9Uqix5azNf1dWCw2xRo9hraiSkDK9z\r
+ o3p0imkotHoNmEMjQ/fMDeIVsSbkThlXmwCLQqx5AEfnBvwVp52fRqKOhNnA0NQx7i2Z\r
+ +0gNjHKQRiMB4QPfO4zoq5R6YMUfMQq3FYJKr5OiW7NR2uRLJHqZgWMuFqUBEtCaHRm6\r
+ kjiw==\r
+X-Received: by 10.112.38.164 with SMTP id h4mr1031947lbk.123.1359496008558;\r
+ Tue, 29 Jan 2013 13:46:48 -0800 (PST)\r
+Received: from localhost (dsl-hkibrasgw4-50df51-27.dhcp.inet.fi.\r
+ [80.223.81.27])\r
+ by mx.google.com with ESMTPS id q4sm5590461lbj.15.2013.01.29.13.46.46\r
+ (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
+ Tue, 29 Jan 2013 13:46:47 -0800 (PST)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 3/6] cli: abstract subcommand finding into a new function\r
+Date: Tue, 29 Jan 2013 23:46:21 +0200\r
+Message-Id:\r
+ <4fa07d936647d316f642c8f30b763d316604ea5f.1359495450.git.jani@nikula.org>\r
+X-Mailer: git-send-email 1.7.10.4\r
+In-Reply-To: <cover.1359495450.git.jani@nikula.org>\r
+References: <cover.1359495450.git.jani@nikula.org>\r
+In-Reply-To: <cover.1359495450.git.jani@nikula.org>\r
+References: <cover.1359495450.git.jani@nikula.org>\r
+X-Gm-Message-State:\r
+ ALoCoQkR3nDUsllGMSZG26Z6cqxy+Q2aboFlmB/+qbEGMNWhm6WpjHM3+3qDTMGsni0AeoOoK5c0\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Tue, 29 Jan 2013 21:46:52 -0000\r
+\r
+Clean up code.\r
+---\r
+ notmuch.c | 68 +++++++++++++++++++++++++++++++------------------------------\r
+ 1 file changed, 35 insertions(+), 33 deletions(-)\r
+\r
+diff --git a/notmuch.c b/notmuch.c\r
+index b413b53..c47b998 100644\r
+--- a/notmuch.c\r
++++ b/notmuch.c\r
+@@ -70,6 +70,18 @@ static command_t commands[] = {\r
+ "This message, or more detailed help for the named command." }\r
+ };\r
+ \r
++static command_t *\r
++find_command (const char *name)\r
++{\r
++ size_t i;\r
++\r
++ for (i = 0; i < ARRAY_SIZE (commands); i++)\r
++ if (strcmp (name, commands[i].name) == 0)\r
++ return &commands[i];\r
++\r
++ return NULL;\r
++}\r
++\r
+ int notmuch_format_version;\r
+ \r
+ static void\r
+@@ -139,7 +151,6 @@ static int\r
+ notmuch_help_command (void *ctx, int argc, char *argv[])\r
+ {\r
+ command_t *command;\r
+- unsigned int i;\r
+ \r
+ argc--; argv++; /* Ignore "help" */\r
+ \r
+@@ -158,13 +169,10 @@ notmuch_help_command (void *ctx, int argc, char *argv[])\r
+ return 0;\r
+ }\r
+ \r
+- for (i = 0; i < ARRAY_SIZE (commands); i++) {\r
+- command = &commands[i];\r
+-\r
+- if (strcmp (argv[0], command->name) == 0) {\r
+- char *page = talloc_asprintf (ctx, "notmuch-%s", command->name);\r
+- exec_man (page);\r
+- }\r
++ command = find_command (argv[0]);\r
++ if (command) {\r
++ char *page = talloc_asprintf (ctx, "notmuch-%s", command->name);\r
++ exec_man (page);\r
+ }\r
+ \r
+ if (strcmp (argv[0], "search-terms") == 0) {\r
+@@ -246,10 +254,11 @@ int\r
+ main (int argc, char *argv[])\r
+ {\r
+ void *local;\r
++ char *talloc_report;\r
+ command_t *command;\r
+- unsigned int i;\r
+ notmuch_bool_t print_help=FALSE, print_version=FALSE;\r
+ int opt_index;\r
++ int ret = 0;\r
+ \r
+ notmuch_opt_desc_t options[] = {\r
+ { NOTMUCH_OPT_BOOLEAN, &print_help, "help", 'h', 0 },\r
+@@ -284,37 +293,30 @@ main (int argc, char *argv[])\r
+ return 0;\r
+ }\r
+ \r
+- for (i = 0; i < ARRAY_SIZE (commands); i++) {\r
+- command = &commands[i];\r
+-\r
+- if (strcmp (argv[opt_index], command->name) == 0) {\r
+- int ret;\r
+- char *talloc_report;\r
+-\r
+- ret = (command->function)(local, argc - opt_index, argv + opt_index);\r
++ command = find_command (argv[opt_index]);\r
++ if (!command) {\r
++ fprintf (stderr, "Error: Unknown command '%s' (see \"notmuch help\")\n",\r
++ argv[opt_index]);\r
++ return 1;\r
++ }\r
+ \r
+- /* in the future support for this environment variable may\r
+- * be supplemented or replaced by command line arguments\r
+- * --leak-report and/or --leak-report-full */\r
++ ret = (command->function)(local, argc - opt_index, argv + opt_index);\r
+ \r
+- talloc_report = getenv ("NOTMUCH_TALLOC_REPORT");\r
++ /* in the future support for this environment variable may\r
++ * be supplemented or replaced by command line arguments\r
++ * --leak-report and/or --leak-report-full */\r
+ \r
+- /* this relies on the previous call to\r
+- * talloc_enable_null_tracking */\r
++ talloc_report = getenv ("NOTMUCH_TALLOC_REPORT");\r
+ \r
+- if (talloc_report && strcmp (talloc_report, "") != 0) {\r
+- FILE *report = fopen (talloc_report, "w");\r
+- talloc_report_full (NULL, report);\r
+- }\r
++ /* this relies on the previous call to\r
++ * talloc_enable_null_tracking */\r
+ \r
+- return ret;\r
+- }\r
++ if (talloc_report && strcmp (talloc_report, "") != 0) {\r
++ FILE *report = fopen (talloc_report, "w");\r
++ talloc_report_full (NULL, report);\r
+ }\r
+ \r
+- fprintf (stderr, "Error: Unknown command '%s' (see \"notmuch help\")\n",\r
+- argv[1]);\r
+-\r
+ talloc_free (local);\r
+ \r
+- return 1;\r
++ return ret;\r
+ }\r
+-- \r
+1.7.10.4\r
+\r