[PATCH v2 1/6] cli: abstract subcommand finding into a new function
authorJani Nikula <jani@nikula.org>
Sun, 3 Mar 2013 21:55:05 +0000 (23:55 +0200)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:53:54 +0000 (09:53 -0800)
a0/d964f5ca76a7558e6557a44560dd477ec21f05 [new file with mode: 0644]

diff --git a/a0/d964f5ca76a7558e6557a44560dd477ec21f05 b/a0/d964f5ca76a7558e6557a44560dd477ec21f05
new file mode 100644 (file)
index 0000000..4788e52
--- /dev/null
@@ -0,0 +1,201 @@
+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 64D34431FC7\r
+       for <notmuch@notmuchmail.org>; Sun,  3 Mar 2013 13:55:22 -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 mKNPant9o1wr for <notmuch@notmuchmail.org>;\r
+       Sun,  3 Mar 2013 13:55:17 -0800 (PST)\r
+Received: from mail-lb0-f176.google.com (mail-lb0-f176.google.com\r
+       [209.85.217.176]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 9E3ED431FAF\r
+       for <notmuch@notmuchmail.org>; Sun,  3 Mar 2013 13:55:17 -0800 (PST)\r
+Received: by mail-lb0-f176.google.com with SMTP id s4so3423317lbc.7\r
+       for <notmuch@notmuchmail.org>; Sun, 03 Mar 2013 13:55:16 -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:cc:subject:date:message-id:x-mailer:in-reply-to\r
+       :references:in-reply-to:references:x-gm-message-state;\r
+       bh=xqu8g7GrmnFPNTesByK6E25F/+SB9aDiPNRFZ89INrk=;\r
+       b=gwbKA49TNfzTAfybaXe/8AIJcV21WF8ktSw2dkus0lifOuyEMUbE9Dlgs3vAqaqbqx\r
+       6dJpH26P21wxTmvoZ2oymv0Q7UiM5Gqcv4G8nAWyTymmHgHfC4TPBILdkDqYhtAe37+L\r
+       +i53IxEpOjfy45VNOQmd4XYmiOugOAClu/NJpX7VavUSm6IWfovNvfBqHAmDrUMmitw4\r
+       RRkwmrUoEwcSXMXXuy2APHK/dfGNk+KnwH4VlVRrXgVLfcYlSG8ueWvkdt50k/5OfBy8\r
+       oqsSQe76IPp4N8QTlq38hAdykNQ/Yuu9pkVFAtaq+puR0xJPf6jdnZx8U5BOj9Bnc1Xh\r
+       Vjmg==\r
+X-Received: by 10.112.88.35 with SMTP id bd3mr3672025lbb.56.1362347716050;\r
+       Sun, 03 Mar 2013 13:55:16 -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 v7sm6609588lbg.13.2013.03.03.13.55.14\r
+       (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
+       Sun, 03 Mar 2013 13:55:15 -0800 (PST)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 1/6] cli: abstract subcommand finding into a new function\r
+Date: Sun,  3 Mar 2013 23:55:05 +0200\r
+Message-Id:\r
+ <a3d3c306c0c1f0d118819e4cfd143a4bc1f8457e.1362347362.git.jani@nikula.org>\r
+X-Mailer: git-send-email 1.7.10.4\r
+In-Reply-To: <cover.1362347362.git.jani@nikula.org>\r
+References: <cover.1362347362.git.jani@nikula.org>\r
+In-Reply-To: <cover.1362347362.git.jani@nikula.org>\r
+References: <cover.1362347362.git.jani@nikula.org>\r
+X-Gm-Message-State:\r
+ ALoCoQlKP2nQ68kPj5Dz9xtsnYvtNnHbPBMJY4UK9qVA2nj56Hxl/cwzZoOreqYbRKP+04I+BpwU\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: Sun, 03 Mar 2013 21:55:23 -0000\r
+\r
+Clean up code.\r
+---\r
+ notmuch.c |   78 +++++++++++++++++++++++++++++++------------------------------\r
+ 1 file changed, 40 insertions(+), 38 deletions(-)\r
+\r
+diff --git a/notmuch.c b/notmuch.c\r
+index cfb009b..3dff6d4 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
+@@ -247,10 +255,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
+@@ -285,39 +294,32 @@ 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
+-\r
+-          talloc_report = getenv ("NOTMUCH_TALLOC_REPORT");\r
+-\r
+-          /* this relies on the previous call to\r
+-           * talloc_enable_null_tracking */\r
+-\r
+-          if (talloc_report && strcmp (talloc_report, "") != 0) {\r
+-              FILE *report = fopen (talloc_report, "w");\r
+-              if (report) {\r
+-                  talloc_report_full (NULL, report);\r
+-              } else {\r
+-                  ret = 1;\r
+-                  fprintf (stderr, "ERROR: unable to write talloc log. ");\r
+-                  perror (talloc_report);\r
+-              }\r
+-          }\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
+-          return ret;\r
++    ret = (command->function)(local, argc - opt_index, argv + opt_index);\r
++\r
++    talloc_report = getenv ("NOTMUCH_TALLOC_REPORT");\r
++    if (talloc_report && strcmp (talloc_report, "") != 0) {\r
++      /* this relies on the previous call to\r
++       * talloc_enable_null_tracking\r
++       */\r
++\r
++      FILE *report = fopen (talloc_report, "w");\r
++      if (report) {\r
++          talloc_report_full (NULL, report);\r
++      } else {\r
++          ret = 1;\r
++          fprintf (stderr, "ERROR: unable to write talloc log. ");\r
++          perror (talloc_report);\r
+       }\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