[PATCH 2/4] notmuch-restore: implement argument parsing for --match

David Bremner david at tethera.net
Fri Oct 28 20:04:49 PDT 2011


From: David Bremner <bremner at debian.org>

- 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 (&regex,
+    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 (&regex, 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 (&regex);
+    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=<bad regex>'
-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=<good regex>' \
     'notmuch restore --match="notmuch.*" < /dev/null > /dev/null 2>&1'
 
-- 
1.7.6.3



More information about the notmuch mailing list