From: david@tethera.net
To: notmuch@notmuchmail.org
Cc: David Bremner <bremner@debian.org>
Subject: [Patch v7 04/14] notmuch-tag: factor out double quoting routine
Date: Fri, 14 Dec 2012 09:34:12 -0400 [thread overview]
Message-ID: <1355492062-7546-5-git-send-email-david@tethera.net> (raw)
In-Reply-To: <1355492062-7546-1-git-send-email-david@tethera.net>
From: David Bremner <bremner@debian.org>
This could live in tag-util as well, but it is really nothing specific
to tags (although the conventions are arguable specific to Xapian).
The API is changed from "caller-allocates" to "readline-like". The scan for
max tag length is pushed down into the double quoting routine.
---
notmuch-tag.c | 50 ++++++++++++++++----------------------------------
util/string-util.c | 34 ++++++++++++++++++++++++++++++++++
util/string-util.h | 8 ++++++++
3 files changed, 58 insertions(+), 34 deletions(-)
diff --git a/notmuch-tag.c b/notmuch-tag.c
index 0965ee7..13f2268 100644
--- a/notmuch-tag.c
+++ b/notmuch-tag.c
@@ -20,6 +20,7 @@
#include "notmuch-client.h"
#include "tag-util.h"
+#include "string-util.h"
static volatile sig_atomic_t interrupted;
@@ -37,25 +38,6 @@ handle_sigint (unused (int sig))
}
static char *
-_escape_tag (char *buf, const char *tag)
-{
- const char *in = tag;
- char *out = buf;
-
- /* Boolean terms surrounded by double quotes can contain any
- * character. Double quotes are quoted by doubling them. */
- *out++ = '"';
- while (*in) {
- if (*in == '"')
- *out++ = '"';
- *out++ = *in++;
- }
- *out++ = '"';
- *out = 0;
- return buf;
-}
-
-static char *
_optimize_tag_query (void *ctx, const char *orig_query_string,
const tag_op_list_t *list)
{
@@ -67,44 +49,44 @@ _optimize_tag_query (void *ctx, const char *orig_query_string,
* parenthesize and the exclusion part of the query must not use
* the '-' operator (though the NOT operator is fine). */
- char *escaped, *query_string;
+ char *escaped = NULL;
+ size_t escaped_len = 0;
+ char *query_string;
const char *join = "";
size_t i;
- unsigned int max_tag_len = 0;
/* Don't optimize if there are no tag changes. */
if (tag_op_list_size (list) == 0)
return talloc_strdup (ctx, orig_query_string);
- /* Allocate a buffer for escaping tags. This is large enough to
- * hold a fully escaped tag with every character doubled plus
- * enclosing quotes and a NUL. */
- for (i = 0; i < tag_op_list_size (list); i++)
- if (strlen (tag_op_list_tag (list, i)) > max_tag_len)
- max_tag_len = strlen (tag_op_list_tag (list, i));
-
- escaped = talloc_array (ctx, char, max_tag_len * 2 + 3);
- if (! escaped)
- return NULL;
-
/* Build the new query string */
if (strcmp (orig_query_string, "*") == 0)
query_string = talloc_strdup (ctx, "(");
else
query_string = talloc_asprintf (ctx, "( %s ) and (", orig_query_string);
+
+ /* Boolean terms surrounded by double quotes can contain any
+ * character. Double quotes are quoted by doubling them. */
+
for (i = 0; i < tag_op_list_size (list) && query_string; i++) {
+ double_quote_str (ctx,
+ tag_op_list_tag (list, i),
+ &escaped, &escaped_len);
+
query_string = talloc_asprintf_append_buffer (
query_string, "%s%stag:%s", join,
tag_op_list_isremove (list, i) ? "" : "not ",
- _escape_tag (escaped, tag_op_list_tag (list, i)));
+ escaped);
join = " or ";
}
if (query_string)
query_string = talloc_strdup_append_buffer (query_string, ")");
- talloc_free (escaped);
+ if (escaped)
+ talloc_free (escaped);
+
return query_string;
}
diff --git a/util/string-util.c b/util/string-util.c
index 44f8cd3..ea7c25b 100644
--- a/util/string-util.c
+++ b/util/string-util.c
@@ -20,6 +20,7 @@
#include "string-util.h"
+#include "talloc.h"
char *
strtok_len (char *s, const char *delim, size_t *len)
@@ -32,3 +33,36 @@ strtok_len (char *s, const char *delim, size_t *len)
return *len ? s : NULL;
}
+
+
+int
+double_quote_str (void *ctx, const char *str,
+ char **buf, size_t *len)
+{
+ const char *in;
+ char *out;
+ size_t needed = 3;
+
+ for (in = str; *in; in++)
+ needed += (*in == '"') ? 2 : 1;
+
+ if (needed > *len)
+ *buf = talloc_realloc (ctx, *buf, char, 2*needed);
+
+ if (! *buf)
+ return 1;
+
+ out = *buf;
+
+ *out++ = '"';
+ in = str;
+ while (*in) {
+ if (*in == '"')
+ *out++ = '"';
+ *out++ = *in++;
+ }
+ *out++ = '"';
+ *out = 0;
+
+ return 0;
+}
diff --git a/util/string-util.h b/util/string-util.h
index ac7676c..b593bc7 100644
--- a/util/string-util.h
+++ b/util/string-util.h
@@ -19,4 +19,12 @@
char *strtok_len (char *s, const char *delim, size_t *len);
+/* Copy str to dest, surrounding with double quotes.
+ * Any internal double-quotes are doubled, i.e. a"b -> "a""b"
+ *
+ * Output is into buf; it may be talloc_realloced
+ * return 0 on success, non-zero on failure.
+ */
+int double_quote_str (void *talloc_ctx, const char *str,
+ char **buf, size_t *len);
#endif
--
1.7.10.4
next prev parent reply other threads:[~2012-12-14 13:34 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-12-14 13:34 v7 batch tagging series david
2012-12-14 13:34 ` [Patch v7 01/14] parse_tag_line: use enum for return value david
2012-12-14 13:34 ` [Patch v7 02/14] tag-util: factor out rules for illegal tags, use in parse_tag_line david
2012-12-14 13:34 ` [Patch v7 03/14] notmuch-tag.c: convert to use tag-utils david
2012-12-14 13:34 ` david [this message]
2012-12-15 17:55 ` [Patch v7 04/14] notmuch-tag: factor out double quoting routine Mark Walters
2012-12-15 22:20 ` Jani Nikula
2012-12-14 13:34 ` [Patch v7 05/14] quote_and_decode_query: new function to quote hex-decoded queries david
2012-12-15 17:49 ` Mark Walters
2012-12-15 18:58 ` David Bremner
2012-12-15 20:09 ` [PATCH] fixup for hex encoding desription in notmuch-tag.1 david
2012-12-15 23:10 ` Jani Nikula
2012-12-15 23:21 ` [Patch v7 05/14] quote_and_decode_query: new function to quote hex-decoded queries Jani Nikula
2012-12-16 3:39 ` David Bremner
2012-12-14 13:34 ` [Patch v7 06/14] notmuch-restore: move query handling for batch restore to parser david
2012-12-15 17:54 ` Mark Walters
2012-12-15 18:18 ` David Bremner
2012-12-15 19:21 ` [PATCH] fixup: clarify TAG_FLAG_ID_ONLY comments and name david
2012-12-15 23:04 ` [Patch v7 06/14] notmuch-restore: move query handling for batch restore to parser Jani Nikula
2012-12-14 13:34 ` [Patch v7 07/14] cli: add support for batch tagging operations to "notmuch tag" david
2012-12-15 23:14 ` Jani Nikula
2012-12-16 0:23 ` [PATCH] " david
2012-12-14 13:34 ` [Patch v7 08/14] test/tagging: add test for error messages of tag --batch david
2012-12-14 13:34 ` [Patch v7 09/14] test/tagging: add basic tests for batch tagging functionality david
2012-12-14 13:34 ` [Patch v7 10/14] test/tagging: add tests for exotic tags david
2012-12-14 13:34 ` [Patch v7 11/14] test/tagging: add test for exotic message-ids and batch tagging david
2012-12-14 13:34 ` [Patch v7 12/14] test/tagging: add test for compound queries with " david
2012-12-14 13:34 ` [Patch v7 13/14] notmuch-tag.1: tidy synopsis formatting david
2012-12-14 13:34 ` [Patch v7 14/14] man: document notmuch tag --batch, --input options david
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://notmuchmail.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1355492062-7546-5-git-send-email-david@tethera.net \
--to=david@tethera.net \
--cc=bremner@debian.org \
--cc=notmuch@notmuchmail.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://yhetil.org/notmuch.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).