--- /dev/null
+Return-Path: <novalazy@gmail.com>\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 C8495431FBD\r
+ for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 17:17:10 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.799\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.799 tagged_above=-999 required=5\r
+ tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
+ FREEMAIL_FROM=0.001, 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 FvIGBmFpKB+q for <notmuch@notmuchmail.org>;\r
+ Sat, 24 Nov 2012 17:17:10 -0800 (PST)\r
+Received: from mail-da0-f53.google.com (mail-da0-f53.google.com\r
+ [209.85.210.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id D0B36431FCB\r
+ for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 17:17:09 -0800 (PST)\r
+Received: by mail-da0-f53.google.com with SMTP id x6so2305618dac.26\r
+ for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 17:17:09 -0800 (PST)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;\r
+ h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references;\r
+ bh=PHYy4HmuoDFHpFufvJXb0a38WQiJAMf1vpNxzYtVRzQ=;\r
+ b=aFLkwOcYdoayx6xo+dD8Ky6tgn8c+WBQrn8FG6fMtMaG6SxoTqakGKWnX7KOVSBrHs\r
+ ktRBJTbdDewlNQRopBagqcp3gCEkmm+k1gQ4x1Bpnz1BAm6+3hT9AH+QVuiDviUhr0/8\r
+ 4mHKJycdK5NXpYUZhDOr0hc0ElNSXbcg9/wzU/Pls8lJy2k13wKuNozp0P+rsk2BPlqN\r
+ VjIA/d2UrkpOVqGVxNG6J3UT799Z3C74yLciLVn3djirrZa1JFY1matVdoSVpyruiELu\r
+ ay6XG1UKtbfssr0y/gexIPc2rYxc15Yz65WXWf5Mdo2KH0Kol1sTqrh4DcrROVj0XpaW\r
+ 5Kmw==\r
+Received: by 10.66.78.169 with SMTP id c9mr22193987pax.30.1353806229604;\r
+ Sat, 24 Nov 2012 17:17:09 -0800 (PST)\r
+Received: from localhost (215.42.233.220.static.exetel.com.au.\r
+ [220.233.42.215])\r
+ by mx.google.com with ESMTPS id gl9sm6222142pbc.51.2012.11.24.17.17.06\r
+ (version=TLSv1/SSLv3 cipher=OTHER);\r
+ Sat, 24 Nov 2012 17:17:08 -0800 (PST)\r
+From: Peter Wang <novalazy@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 01/20] tag: factor out tag operation parsing\r
+Date: Sun, 25 Nov 2012 12:16:27 +1100\r
+Message-Id: <1353806206-29133-2-git-send-email-novalazy@gmail.com>\r
+X-Mailer: git-send-email 1.7.12.1\r
+In-Reply-To: <1353806206-29133-1-git-send-email-novalazy@gmail.com>\r
+References: <1353806206-29133-1-git-send-email-novalazy@gmail.com>\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, 25 Nov 2012 01:17:11 -0000\r
+\r
+Factor out parsing of +tag, -tag operations from argv\r
+into a separate function.\r
+---\r
+ notmuch-tag.c | 66 +++++++++++++++++++++++++++++++++++++----------------------\r
+ 1 file changed, 41 insertions(+), 25 deletions(-)\r
+\r
+diff --git a/notmuch-tag.c b/notmuch-tag.c\r
+index 88d559b..35a76db 100644\r
+--- a/notmuch-tag.c\r
++++ b/notmuch-tag.c\r
+@@ -167,11 +167,48 @@ tag_query (void *ctx, notmuch_database_t *notmuch, const char *query_string,\r
+ return interrupted;\r
+ }\r
+ \r
++/* Parse +tag and -tag operations between argv[i] and argv[argc-1].\r
++ * The array tag_ops must be at least argc - i elements long.\r
++ * Returns the index into argv where parsing stopped, or -1 on error. */\r
++static int\r
++parse_tag_operations (int i, int argc, char *argv[],\r
++ tag_operation_t *tag_ops, int *tag_ops_count)\r
++{\r
++ *tag_ops_count = 0;\r
++\r
++ for (; i < argc; i++) {\r
++ if (strcmp (argv[i], "--") == 0) {\r
++ i++;\r
++ break;\r
++ }\r
++ if (argv[i][0] == '+' || argv[i][0] == '-') {\r
++ if (argv[i][0] == '+' && argv[i][1] == '\0') {\r
++ fprintf (stderr, "Error: tag names cannot be empty.\n");\r
++ return -1;\r
++ }\r
++ if (argv[i][0] == '+' && argv[i][1] == '-') {\r
++ /* This disallows adding the non-removable tag "-" and\r
++ * enables notmuch tag to take long options in the\r
++ * future. */\r
++ fprintf (stderr, "Error: tag names must not start with '-'.\n");\r
++ return -1;\r
++ }\r
++ tag_ops[*tag_ops_count].tag = argv[i] + 1;\r
++ tag_ops[*tag_ops_count].remove = (argv[i][0] == '-');\r
++ (*tag_ops_count)++;\r
++ } else {\r
++ break;\r
++ }\r
++ }\r
++\r
++ return i;\r
++}\r
++\r
+ int\r
+ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
+ {\r
+ tag_operation_t *tag_ops;\r
+- int tag_ops_count = 0;\r
++ int tag_ops_count;\r
+ char *query_string;\r
+ notmuch_config_t *config;\r
+ notmuch_database_t *notmuch;\r
+@@ -197,30 +234,9 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
+ return 1;\r
+ }\r
+ \r
+- for (i = 0; i < argc; i++) {\r
+- if (strcmp (argv[i], "--") == 0) {\r
+- i++;\r
+- break;\r
+- }\r
+- if (argv[i][0] == '+' || argv[i][0] == '-') {\r
+- if (argv[i][0] == '+' && argv[i][1] == '\0') {\r
+- fprintf (stderr, "Error: tag names cannot be empty.\n");\r
+- return 1;\r
+- }\r
+- if (argv[i][0] == '+' && argv[i][1] == '-') {\r
+- /* This disallows adding the non-removable tag "-" and\r
+- * enables notmuch tag to take long options in the\r
+- * future. */\r
+- fprintf (stderr, "Error: tag names must not start with '-'.\n");\r
+- return 1;\r
+- }\r
+- tag_ops[tag_ops_count].tag = argv[i] + 1;\r
+- tag_ops[tag_ops_count].remove = (argv[i][0] == '-');\r
+- tag_ops_count++;\r
+- } else {\r
+- break;\r
+- }\r
+- }\r
++ i = parse_tag_operations (0, argc, argv, tag_ops, &tag_ops_count);\r
++ if (i < 0)\r
++ return 1;\r
+ \r
+ tag_ops[tag_ops_count].tag = NULL;\r
+ \r
+-- \r
+1.7.12.1\r
+\r