Yet another version. Luckily we all use threaded mailreaders, right?

This obsoletes 

     id:1354979276-20099-1-git-send-email-david at tethera.net

One trivial new patch

    [Patch v5 06/11] notmuch-restore: normalize case of error messages.

And a fairly extensive reworking of the error reporting in
parse_tag_line. This version introduces an enum for return values of
parse_tag_line; this could in the future also be used for
parse_sup_line.


commit 55f38bbd034bf8bfbe126cd598cf6085f5f30bf1
Author: David Bremner <bremner at debian.org>
Date:   Sat Dec 8 17:47:21 2012 -0400

    tag-util.h: add enum for tag parse status

diff --git a/tag-util.h b/tag-util.h
index 581207a..e828992 100644
--- a/tag-util.h
+++ b/tag-util.h
@@ -30,6 +30,23 @@ typedef enum {

 } tag_op_flag_t;

+/* These should obey the convention that fatal errors are negative,
+ * skipped lines are positive.
+ */
+typedef enum {
+    TAG_PARSE_OUT_OF_MEMORY = -1,
+
+    /* Line parsed successfuly. */
+    TAG_PARSE_SUCCESS = 0,
+
+    /* Line has a syntax error */
+    TAG_PARSE_INVALID = 1,
+
+    /* Line was blank or a comment */
+    TAG_PARSE_SKIPPED = 2
+
+} tag_parse_status_t;
+
 /* Parse a string of the following format:
  *
  * +<tag>|-<tag> [...] [--] <search-terms>
@@ -45,16 +62,12 @@ typedef enum {
  * Leading and trailing space ' ' is ignored. Empty lines and lines
  * beginning with '#' are ignored.
  *
- * Returns:    0       OK,
- *             1       skipped (invalid) line
- *             2       skipped (valid) line
- *             -1      fatal(ish) error.
  *
  * Output Parameters:
  *     ops     contains a list of tag operations
  *     query_str the search terms.
  */
-int
+tag_parse_status_t
 parse_tag_line (void *ctx, char *line,
                tag_op_flag_t flags,
                char **query_str, tag_op_list_t *ops);

commit da6f5cb79c526b8229fb2dbda0ecdce568c2a47c
Author: David Bremner <bremner at debian.org>
Date:   Sat Dec 8 17:48:02 2012 -0400

    tag-util.h: uncrustify comments

diff --git a/tag-util.h b/tag-util.h
index e828992..99b0fa0 100644
--- a/tag-util.h
+++ b/tag-util.h
@@ -20,7 +20,7 @@ typedef enum {

     /* Don't try to avoid database operations. Useful when we
      * know that message passed needs these operations.
-      */
+     */
     TAG_FLAG_PRE_OPTIMIZED = (1 << 2),

     /* Accept strange tags that might be user error;
@@ -118,9 +118,9 @@ void
 tag_op_list_reset (tag_op_list_t *list);


- /*
-  *   return the i'th tag in the list
-  */
+/*
+ *   return the i'th tag in the list
+ */

 const char *
 tag_op_list_tag (const tag_op_list_t *list, size_t i);

commit e8b27272340c4ba869f1d39e3264f78368d51d86
Author: David Bremner <bremner at debian.org>
Date:   Sat Dec 8 17:53:53 2012 -0400

    fixup for error message test

diff --git a/test/dump-restore b/test/dump-restore
index b267792..8a86782 100755
--- a/test/dump-restore
+++ b/test/dump-restore
@@ -183,7 +183,13 @@ test_expect_equal_file EXPECTED.$test_count 
OUTPUT.$test_count

 test_begin_subtest 'restore: checking error messages'
 notmuch restore <<EOF 2>OUTPUT
+# the next line has a space
+ 
+a
++0
 +a +b
+# trailing whitespace
++a +b 
 +c +d --
 # this is a harmless comment, do not yell about it.

@@ -197,15 +203,14 @@ notmuch restore <<EOF 2>OUTPUT
 EOF

 cat <<EOF > EXPECTED
-Warning: no query string: +a +b
-Warning: Ignoring invalid input line +a +b
-Warning: no query string: +c +d --
-Warning: Ignoring invalid input line +c +d --
-Warning: Hex decoding of tag %zz failed
-Warning: Ignoring invalid input line +%zz -- id:whatever
-Warning: Hex decoding of query id:%yy failed
-Warning: Ignoring invalid input line +e +f id:%yy
-Warning: Cannot apply tags to missing message: foo and bar
+Warning: unsupported query: a
+Warning: no query string [+0]
+Warning: no query string [+a +b]
+Warning: missing query string [+a +b ]
+Warning: no query string after -- [+c +d --]
+Warning: hex decoding of tag %zz failed [+%zz -- id:whatever]
+Warning: hex decoding of query id:%yy failed [+e +f id:%yy]
+Warning: cannot apply tags to missing message: foo and bar
 EOF

 test_expect_equal_file EXPECTED OUTPUT

commit 74287d9f7fdd998932ef95ca5846b9905df19e67
Author: David Bremner <bremner at debian.org>
Date:   Sat Dec 8 17:57:10 2012 -0400

    notmuch-restore: normalize case of error messages.

    In English, (unlike German) one does not capitalize the first word
    after a colon.

diff --git a/notmuch-restore.c b/notmuch-restore.c
index 44bf88d..dba882b 100644
--- a/notmuch-restore.c
+++ b/notmuch-restore.c
@@ -39,7 +39,7 @@ tag_message (unused (void *ctx),

     status = notmuch_database_find_message (notmuch, message_id, &message);
     if (status || message == NULL) {
-       fprintf (stderr, "Warning: Cannot apply tags to %smessage: %s\n",
+       fprintf (stderr, "Warning: cannot apply tags to %smessage: %s\n",
                 message ? "" : "missing ", message_id);
        if (status)
            fprintf (stderr, "%s\n", notmuch_status_to_string (status));
@@ -214,7 +214,7 @@ notmuch_restore_command (unused (void *ctx), int argc, char 
*argv[])

            if (ret == 0) {
                if (strncmp ("id:", query_string, 3) != 0) {
-                   fprintf (stderr, "Unsupported query: %s\n", query_string);
+                   fprintf (stderr, "Warning: unsupported query: %s\n", 
query_string);
                    continue;
                }
                /* delete id: from front of string; tag_message

commit b9d76086b9ca30019dc829b1a106d3f4743cbeb7
Author: David Bremner <bremner at debian.org>
Date:   Sat Dec 8 18:02:26 2012 -0400

    tag-util.c: refactor error handling.

diff --git a/tag-util.c b/tag-util.c
index b68ea50..e7233ab 100644
--- a/tag-util.c
+++ b/tag-util.c
@@ -16,6 +16,21 @@ struct _tag_op_list_t {
     size_t size;
 };

+static int
+line_error (tag_parse_status_t status,
+           const char *line,
+           const char *format, ...)
+{
+    va_list va_args;
+
+    va_start (va_args, format);
+
+    fprintf (stderr, status < 0 ? "Error: " : "Warning: ");
+    vfprintf (stderr, format, va_args);
+    fprintf (stderr, " [%s]\n", line);
+    return status;
+}
+
 int
 parse_tag_line (void *ctx, char *line,
                tag_op_flag_t flags,
@@ -55,8 +70,11 @@ parse_tag_line (void *ctx, char *line,
        /* Optional explicit end of tags marker. */
        if (tok_len == 2 && strncmp (tok, "--", tok_len) == 0) {
            tok = strtok_len (tok + tok_len, " ", &tok_len);
-           if (tok == NULL)
-               fprintf (stderr, "Warning: no query string: %s\n", 
line_for_error);
+           if (tok == NULL) {
+               ret = line_error (TAG_PARSE_INVALID, line_for_error,
+                                 "no query string after --");
+               goto DONE;
+           }
            break;
        }

@@ -66,8 +84,8 @@ parse_tag_line (void *ctx, char *line,

        /* If tag is terminated by NUL, there's no query string. */
        if (*(tok + tok_len) == '\0') {
-           fprintf (stderr, "Warning: no query string: %s\n", line_for_error);
-           ret = 1;
+           ret = line_error (TAG_PARSE_INVALID, line_for_error,
+                             "no query string");
            goto DONE;
        }

@@ -79,47 +97,42 @@ parse_tag_line (void *ctx, char *line,

        /* Maybe refuse empty tags. */
        if (! (flags & TAG_FLAG_BE_GENEROUS) && *tag == '\0') {
-           fprintf (stderr, "Warning: empty tag: %s\n", line_for_error);
-           ret = 1;
+           ret = line_error (TAG_PARSE_INVALID, line_for_error,
+                             "empty tag");
            goto DONE;
        }

        /* Decode tag. */
        if (hex_decode_inplace (tag) != HEX_SUCCESS) {
-           fprintf (stderr, "Warning: Hex decoding of tag %s failed\n",
-                    tag);
-           ret = 1;
+           ret = line_error (TAG_PARSE_INVALID, line_for_error,
+                             "hex decoding of tag %s failed", tag);
            goto DONE;
        }

        if (tag_op_list_append (ctx, tag_ops, tag, remove)) {
-           /* diagnostics already printed */
-           ret = -1;
+           ret = line_error (TAG_PARSE_OUT_OF_MEMORY, line_for_error,
+                             "aborting");
            goto DONE;
        }
     }

     if (tok == NULL) {
-       ret = 1;
+       /* use a different error message for testing */
+       ret = line_error (TAG_PARSE_INVALID, line_for_error,
+                         "missing query string");
        goto DONE;
     }

     /* tok now points to the query string */
     if (hex_decode_inplace (tok) != HEX_SUCCESS) {
-       fprintf (stderr, "Warning: Hex decoding of query %s failed\n",
-                tok);
-       ret = 1;
+       ret = line_error (TAG_PARSE_INVALID, line_for_error,
+                         "hex decoding of query %s failed", tok);
        goto DONE;
     }

     *query_string = tok;

   DONE:
-    if ((ret % 2) != 0)
-       fprintf (stderr, "%s invalid input line %s\n",
-                ret == 1 ? "Warning: Ignoring" : "Error: Failing at",
-                line_for_error);
-
     talloc_free (line_for_error);
     return ret;
 }

Reply via email to