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