[PATCH 4/4] Update NEWS for user.other_name
[notmuch-archives.git] / 6b / 2bfc484baad36a2478e5526b625e23175e1593
1 Return-Path: <amdragon@mit.edu>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 0C0DA431FC0\r
6         for <notmuch@notmuchmail.org>; Sun, 30 Dec 2012 22:42:54 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 1.151\r
10 X-Spam-Level: *\r
11 X-Spam-Status: No, score=1.151 tagged_above=-999 required=5\r
12         tests=[FUZZY_AMBIEN=1.851, RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id L6Tz7jduiYti for <notmuch@notmuchmail.org>;\r
16         Sun, 30 Dec 2012 22:42:52 -0800 (PST)\r
17 Received: from dmz-mailsec-scanner-4.mit.edu (DMZ-MAILSEC-SCANNER-4.MIT.EDU\r
18         [18.9.25.15])\r
19         by olra.theworths.org (Postfix) with ESMTP id 906D1431FBC\r
20         for <notmuch@notmuchmail.org>; Sun, 30 Dec 2012 22:42:52 -0800 (PST)\r
21 X-AuditID: 1209190f-b7f016d000000e07-b1-50e133ec16aa\r
22 Received: from mailhub-auth-2.mit.edu ( [18.7.62.36])\r
23         by dmz-mailsec-scanner-4.mit.edu (Symantec Messaging Gateway) with SMTP\r
24         id 7C.5C.03591.CE331E05; Mon, 31 Dec 2012 01:42:52 -0500 (EST)\r
25 Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
26         by mailhub-auth-2.mit.edu (8.13.8/8.9.2) with ESMTP id qBV6goLM027841; \r
27         Mon, 31 Dec 2012 01:42:50 -0500\r
28 Received: from drake.dyndns.org (c-76-21-105-205.hsd1.ca.comcast.net\r
29         [76.21.105.205]) (authenticated bits=0)\r
30         (User authenticated as amdragon@ATHENA.MIT.EDU)\r
31         by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id qBV6gk1b009879\r
32         (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
33         Mon, 31 Dec 2012 01:42:48 -0500 (EST)\r
34 Received: from amthrax by drake.dyndns.org with local (Exim 4.77)\r
35         (envelope-from <amdragon@mit.edu>)\r
36         id 1TpZ5B-0000if-Sr; Mon, 31 Dec 2012 01:42:45 -0500\r
37 From: Austin Clements <amdragon@MIT.EDU>\r
38 To: notmuch@notmuchmail.org\r
39 Subject: [PATCH v4 1/5] util: Factor out boolean term quoting routine\r
40 Date: Mon, 31 Dec 2012 01:42:38 -0500\r
41 Message-Id: <1356936162-2589-2-git-send-email-amdragon@mit.edu>\r
42 X-Mailer: git-send-email 1.7.10.4\r
43 In-Reply-To: <1356936162-2589-1-git-send-email-amdragon@mit.edu>\r
44 References: <1356936162-2589-1-git-send-email-amdragon@mit.edu>\r
45 X-Brightmail-Tracker:\r
46  H4sIAAAAAAAAA+NgFrrFIsWRmVeSWpSXmKPExsUixG6novvG+GGAQfNJYYsbrd2MFk3TnS1W\r
47         z+WxuH5zJrPFm5XzWB1YPXbOusvucfjrQhaPW/dfs3s8W3WL2WPLoffMAaxRXDYpqTmZZalF\r
48         +nYJXBlzr65lL9hjVHF772KWBsa3ql2MnBwSAiYSu94sZYewxSQu3FvP1sXIxSEksI9RYuuU\r
49         XawQzgZGiW/b7zFBOBeZJJbMaGaHcOYySpw71sAI0s8moCGxbf9yMFtEQFpi593ZYO3MAk2M\r
50         Etc2zWUDSQgLuErsm9jLCmKzCKhK3Gq+ARTn4OAVsJdY9KoQ4g5Fie5nE8DKOQUcJHY+m8UC\r
51         YgsBlezee55xAiP/AkaGVYyyKblVurmJmTnFqcm6xcmJeXmpRbomermZJXqpKaWbGEFByCnJ\r
52         v4Px20GlQ4wCHIxKPLwcTA8DhFgTy4orcw8xSnIwKYnyKgFDWIgvKT+lMiOxOCO+qDQntfgQ\r
53         owQHs5II71stoBxvSmJlVWpRPkxKmoNFSZz3aspNfyGB9MSS1OzU1ILUIpisDAeHkgTvBiOg\r
54         RsGi1PTUirTMnBKENBMHJ8hwHqDhH0FqeIsLEnOLM9Mh8qcYFaXEeZ+DJARAEhmleXC9sCTx\r
55         ilEc6BVh3vMgVTzABAPX/QpoMBPQYC2GByCDSxIRUlINjAvvM36+d2BaeOPttb9jn2zsWeSr\r
56         YmX0J2CCbtK6b68nL7jduNpdyHrZl/us9W97DnU+8w/v09kRMzX2Nfcc5qs7RAuzPpSopbUX\r
57         hUScibFcumtyro/Lkcwp+z5/sjpVV2IZ775ArnzRh5aw4IP7SvqeHGAVft0nbBE8K8Lkq9fJ\r
58         puO37htvYlBiKc5INNRiLipOBACC+jl47QIAAA==\r
59 Cc: tomi.ollila@iki.fi\r
60 X-BeenThere: notmuch@notmuchmail.org\r
61 X-Mailman-Version: 2.1.13\r
62 Precedence: list\r
63 List-Id: "Use and development of the notmuch mail system."\r
64         <notmuch.notmuchmail.org>\r
65 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
66         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
67 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
68 List-Post: <mailto:notmuch@notmuchmail.org>\r
69 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
70 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
71         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
72 X-List-Received-Date: Mon, 31 Dec 2012 06:42:54 -0000\r
73 \r
74 From: Austin Clements <amdragon@MIT.EDU>\r
75 \r
76 This is now a generic boolean term quoting function.  It performs\r
77 minimal quoting to produce user-friendly queries.\r
78 \r
79 This could live in tag-util as well, but it is really nothing specific\r
80 to tags (although the conventions are specific to Xapian).\r
81 \r
82 The API is changed from "caller-allocates" to "readline-like".  The\r
83 scan for max tag length is pushed down into the quoting routine.\r
84 Furthermore, this now combines the term prefix with the quoted term;\r
85 arguably this is just as easy to do in the caller, but this will\r
86 nicely parallel the boolean term parsing function to be introduced\r
87 shortly.\r
88 \r
89 This is an amalgamation of code written by David Bremner and myself.\r
90 ---\r
91  notmuch-tag.c      |   48 ++++++++++++---------------------------\r
92  util/string-util.c |   64 ++++++++++++++++++++++++++++++++++++++++++++++++++++\r
93  util/string-util.h |   14 ++++++++++++\r
94  3 files changed, 92 insertions(+), 34 deletions(-)\r
95 \r
96 diff --git a/notmuch-tag.c b/notmuch-tag.c\r
97 index 88d559b..fc9d43a 100644\r
98 --- a/notmuch-tag.c\r
99 +++ b/notmuch-tag.c\r
100 @@ -19,6 +19,7 @@\r
101   */\r
102  \r
103  #include "notmuch-client.h"\r
104 +#include "string-util.h"\r
105  \r
106  static volatile sig_atomic_t interrupted;\r
107  \r
108 @@ -35,25 +36,6 @@ handle_sigint (unused (int sig))\r
109      interrupted = 1;\r
110  }\r
111  \r
112 -static char *\r
113 -_escape_tag (char *buf, const char *tag)\r
114 -{\r
115 -    const char *in = tag;\r
116 -    char *out = buf;\r
117 -\r
118 -    /* Boolean terms surrounded by double quotes can contain any\r
119 -     * character.  Double quotes are quoted by doubling them. */\r
120 -    *out++ = '"';\r
121 -    while (*in) {\r
122 -       if (*in == '"')\r
123 -           *out++ = '"';\r
124 -       *out++ = *in++;\r
125 -    }\r
126 -    *out++ = '"';\r
127 -    *out = 0;\r
128 -    return buf;\r
129 -}\r
130 -\r
131  typedef struct {\r
132      const char *tag;\r
133      notmuch_bool_t remove;\r
134 @@ -71,25 +53,16 @@ _optimize_tag_query (void *ctx, const char *orig_query_string,\r
135       * parenthesize and the exclusion part of the query must not use\r
136       * the '-' operator (though the NOT operator is fine). */\r
137  \r
138 -    char *escaped, *query_string;\r
139 +    char *escaped = NULL;\r
140 +    size_t escaped_len = 0;\r
141 +    char *query_string;\r
142      const char *join = "";\r
143 -    int i;\r
144 -    unsigned int max_tag_len = 0;\r
145 +    size_t i;\r
146  \r
147      /* Don't optimize if there are no tag changes. */\r
148      if (tag_ops[0].tag == NULL)\r
149         return talloc_strdup (ctx, orig_query_string);\r
150  \r
151 -    /* Allocate a buffer for escaping tags.  This is large enough to\r
152 -     * hold a fully escaped tag with every character doubled plus\r
153 -     * enclosing quotes and a NUL. */\r
154 -    for (i = 0; tag_ops[i].tag; i++)\r
155 -       if (strlen (tag_ops[i].tag) > max_tag_len)\r
156 -           max_tag_len = strlen (tag_ops[i].tag);\r
157 -    escaped = talloc_array (ctx, char, max_tag_len * 2 + 3);\r
158 -    if (! escaped)\r
159 -       return NULL;\r
160 -\r
161      /* Build the new query string */\r
162      if (strcmp (orig_query_string, "*") == 0)\r
163         query_string = talloc_strdup (ctx, "(");\r
164 @@ -97,10 +70,17 @@ _optimize_tag_query (void *ctx, const char *orig_query_string,\r
165         query_string = talloc_asprintf (ctx, "( %s ) and (", orig_query_string);\r
166  \r
167      for (i = 0; tag_ops[i].tag && query_string; i++) {\r
168 +       /* XXX in case of OOM, query_string will be deallocated when\r
169 +        * ctx is, which might be at shutdown */\r
170 +       if (make_boolean_term (ctx,\r
171 +                              "tag", tag_ops[i].tag,\r
172 +                              &escaped, &escaped_len))\r
173 +           return NULL;\r
174 +\r
175         query_string = talloc_asprintf_append_buffer (\r
176 -           query_string, "%s%stag:%s", join,\r
177 +           query_string, "%s%s%s", join,\r
178             tag_ops[i].remove ? "" : "not ",\r
179 -           _escape_tag (escaped, tag_ops[i].tag));\r
180 +           escaped);\r
181         join = " or ";\r
182      }\r
183  \r
184 diff --git a/util/string-util.c b/util/string-util.c\r
185 index 44f8cd3..e4bea21 100644\r
186 --- a/util/string-util.c\r
187 +++ b/util/string-util.c\r
188 @@ -20,6 +20,7 @@\r
189  \r
190  \r
191  #include "string-util.h"\r
192 +#include "talloc.h"\r
193  \r
194  char *\r
195  strtok_len (char *s, const char *delim, size_t *len)\r
196 @@ -32,3 +33,66 @@ strtok_len (char *s, const char *delim, size_t *len)\r
197  \r
198      return *len ? s : NULL;\r
199  }\r
200 +\r
201 +int\r
202 +make_boolean_term (void *ctx, const char *prefix, const char *term,\r
203 +                  char **buf, size_t *len)\r
204 +{\r
205 +    const char *in;\r
206 +    char *out;\r
207 +    size_t needed = 3;\r
208 +    int need_quoting = 0;\r
209 +\r
210 +    /* Do we need quoting?  To be paranoid, we quote anything\r
211 +     * containing a quote, even though it only matters at the\r
212 +     * beginning, and anything containing non-ASCII text. */\r
213 +    for (in = term; *in && !need_quoting; in++)\r
214 +       if (*in <= ' ' || *in == ')' || *in == '"' || (unsigned char)*in > 127)\r
215 +           need_quoting = 1;\r
216 +\r
217 +    if (need_quoting)\r
218 +       for (in = term; *in; in++)\r
219 +           needed += (*in == '"') ? 2 : 1;\r
220 +    else\r
221 +       needed = strlen (term) + 1;\r
222 +\r
223 +    /* Reserve space for the prefix */\r
224 +    if (prefix)\r
225 +       needed += strlen (prefix) + 1;\r
226 +\r
227 +    if ((*buf == NULL) || (needed > *len)) {\r
228 +       *len = 2 * needed;\r
229 +       *buf = talloc_realloc (ctx, *buf, char, *len);\r
230 +    }\r
231 +\r
232 +    if (! *buf)\r
233 +       return 1;\r
234 +\r
235 +    out = *buf;\r
236 +\r
237 +    /* Copy in the prefix */\r
238 +    if (prefix) {\r
239 +       strcpy (out, prefix);\r
240 +       out += strlen (prefix);\r
241 +       *out++ = ':';\r
242 +    }\r
243 +\r
244 +    if (! need_quoting) {\r
245 +       strcpy (out, term);\r
246 +       return 0;\r
247 +    }\r
248 +\r
249 +    /* Quote term by enclosing it in double quotes and doubling any\r
250 +     * internal double quotes. */\r
251 +    *out++ = '"';\r
252 +    in = term;\r
253 +    while (*in) {\r
254 +       if (*in == '"')\r
255 +           *out++ = '"';\r
256 +       *out++ = *in++;\r
257 +    }\r
258 +    *out++ = '"';\r
259 +    *out = '\0';\r
260 +\r
261 +    return 0;\r
262 +}\r
263 diff --git a/util/string-util.h b/util/string-util.h\r
264 index ac7676c..b8844a3 100644\r
265 --- a/util/string-util.h\r
266 +++ b/util/string-util.h\r
267 @@ -19,4 +19,18 @@\r
268  \r
269  char *strtok_len (char *s, const char *delim, size_t *len);\r
270  \r
271 +/* Construct a boolean term query with the specified prefix (e.g.,\r
272 + * "id") and search term, quoting term as necessary.  Specifically, if\r
273 + * term contains any non-printable ASCII characters, non-ASCII\r
274 + * characters, close parenthesis or double quotes, it will be enclosed\r
275 + * in double quotes and any internal double quotes will be doubled\r
276 + * (e.g. a"b -> "a""b").  The result will be a valid notmuch query and\r
277 + * can be parsed by parse_boolean_term.\r
278 + *\r
279 + * Output is into buf; it may be talloc_realloced.\r
280 + * Return: 0 on success, non-zero on memory allocation failure.\r
281 + */\r
282 +int make_boolean_term (void *talloc_ctx, const char *prefix, const char *term,\r
283 +                      char **buf, size_t *len);\r
284 +\r
285  #endif\r
286 -- \r
287 1.7.10.4\r
288 \r