From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 4BC64429E2E for ; Fri, 28 Oct 2011 20:05:11 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -2.3 X-Spam-Level: X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_MED=-2.3] 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 0W-1LZ-wD-9C for ; Fri, 28 Oct 2011 20:05:10 -0700 (PDT) Received: from tempo.its.unb.ca (tempo.its.unb.ca [131.202.1.21]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 06728429E31 for ; Fri, 28 Oct 2011 20:05:03 -0700 (PDT) Received: from zancas.localnet (fctnnbsc36w-156034064058.pppoe-dynamic.High-Speed.nb.bellaliant.net [156.34.64.58]) (authenticated bits=0) by tempo.its.unb.ca (8.13.8/8.13.8) with ESMTP id p9T34xWM016363 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO); Sat, 29 Oct 2011 00:04:59 -0300 Received: from bremner by zancas.localnet with local (Exim 4.76) (envelope-from ) id 1RJzEB-0003DD-2I; Sat, 29 Oct 2011 00:04:59 -0300 From: David Bremner To: notmuch@notmuchmail.org Subject: [PATCH 2/4] notmuch-restore: implement argument parsing for --match Date: Sat, 29 Oct 2011 00:04:49 -0300 Message-Id: <1319857491-12298-3-git-send-email-david@tethera.net> X-Mailer: git-send-email 1.7.6.3 In-Reply-To: <1319857491-12298-1-git-send-email-david@tethera.net> References: <1319406673-7208-1-git-send-email-david@tethera.net> <1319857491-12298-1-git-send-email-david@tethera.net> Cc: David Bremner 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: Sat, 29 Oct 2011 03:05:11 -0000 From: David Bremner - recognize the --match option - require an argument - check the argument is a correct regex. Currently the arguments are ignored after parsing. Note that we have to be a bit careful to avoid creating a resource leak here by error returning before calling regfree. On the other hand, notmuch is probably shutting down at that point, so it may not matter much. --- notmuch-restore.c | 18 ++++++++++++++---- test/dump-restore | 3 --- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/notmuch-restore.c b/notmuch-restore.c index 13b4325..e5ac162 100644 --- a/notmuch-restore.c +++ b/notmuch-restore.c @@ -29,11 +29,12 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[]) notmuch_database_t *notmuch; notmuch_bool_t synchronize_flags; notmuch_bool_t accumulate = FALSE; + notmuch_bool_t match_enabled = FALSE; FILE *input = stdin; char *line = NULL; size_t line_size; ssize_t line_len; - regex_t regex; + regex_t input_regex, match_regex; int rerr; config = notmuch_config_open (ctx, NULL, NULL); @@ -49,6 +50,7 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[]) struct option options[] = { { "accumulate", no_argument, 0, 'a' }, + { "match", required_argument, 0, 'm' }, { 0, 0, 0, 0} }; @@ -60,6 +62,11 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[]) case 'a': accumulate = 1; break; + case 'm': + match_enabled = TRUE; + if ( xregcomp (&match_regex, optarg, REG_EXTENDED) ) + return 1; + break; case '?': return 1; break; @@ -88,7 +95,7 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[]) * non-space characters for the message-id, then one or more * spaces, then a list of space-separated tags as a sequence of * characters within literal '(' and ')'. */ - if ( xregcomp (®ex, + if ( xregcomp (&input_regex, "^([^ ]+) \\(([^)]*)\\)$", REG_EXTENDED) ) INTERNAL_ERROR("compile time constant regex failed."); @@ -103,7 +110,7 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[]) chomp_newline (line); - rerr = xregexec (®ex, line, 3, match, 0); + rerr = xregexec (&input_regex, line, 3, match, 0); if (rerr == REG_NOMATCH) { fprintf (stderr, "Warning: Ignoring invalid input line: %s\n", @@ -186,7 +193,10 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[]) free (file_tags); } - regfree (®ex); + regfree (&input_regex); + + if (match_enabled) + regfree (&match_regex); if (line) free (line); diff --git a/test/dump-restore b/test/dump-restore index c176b52..c6089f9 100755 --- a/test/dump-restore +++ b/test/dump-restore @@ -55,16 +55,13 @@ test_expect_success 'restore extra argument' \ test_begin_subtest 'restore --match #missing arg' -test_subtest_known_broken test_expect_equal "restore: option '--match' requires an argument"\ "$(notmuch restore --match 2>&1)" test_begin_subtest 'restore --match=' -test_subtest_known_broken test_expect_equal 'compiling regex notmuch.*[: Invalid regular expression'\ "$(notmuch restore --match='notmuch.*[' 2>&1)" -test_subtest_known_broken test_expect_success 'restore --match=' \ 'notmuch restore --match="notmuch.*" < /dev/null > /dev/null 2>&1' -- 1.7.6.3