Re: [PATCH] emacs: wash: make word-wrap bound message width
[notmuch-archives.git] / 2d / e340d7ea81d9a633f313c32ed730a590a56716
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 1A706431FB6\r
6         for <notmuch@notmuchmail.org>; Tue, 25 Dec 2012 19:49:09 -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 VSbalYhC53QM for <notmuch@notmuchmail.org>;\r
16         Tue, 25 Dec 2012 19:49:03 -0800 (PST)\r
17 Received: from dmz-mailsec-scanner-2.mit.edu (DMZ-MAILSEC-SCANNER-2.MIT.EDU\r
18         [18.9.25.13])\r
19         by olra.theworths.org (Postfix) with ESMTP id 64052431FBC\r
20         for <notmuch@notmuchmail.org>; Tue, 25 Dec 2012 19:48:59 -0800 (PST)\r
21 X-AuditID: 1209190d-b7efd6d000001a82-6f-50da73aa80f0\r
22 Received: from mailhub-auth-4.mit.edu ( [18.7.62.39])\r
23         by dmz-mailsec-scanner-2.mit.edu (Symantec Messaging Gateway) with SMTP\r
24         id 50.41.06786.AA37AD05; Tue, 25 Dec 2012 22:48:58 -0500 (EST)\r
25 Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
26         by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id qBQ3mvD0019979; \r
27         Tue, 25 Dec 2012 22:48:57 -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 qBQ3mrI0013181\r
32         (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
33         Tue, 25 Dec 2012 22:48:55 -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 1Tnhz8-0002yw-Kh; Tue, 25 Dec 2012 22:48:50 -0500\r
37 From: Austin Clements <amdragon@MIT.EDU>\r
38 To: notmuch@notmuchmail.org\r
39 Subject: [PATCH v2 1/5] util: Factor out boolean term quoting routine\r
40 Date: Tue, 25 Dec 2012 22:48:39 -0500\r
41 Message-Id: <1356493723-11085-2-git-send-email-amdragon@mit.edu>\r
42 X-Mailer: git-send-email 1.7.10.4\r
43 In-Reply-To: <1356493723-11085-1-git-send-email-amdragon@mit.edu>\r
44 References: <1356493723-11085-1-git-send-email-amdragon@mit.edu>\r
45 X-Brightmail-Tracker:\r
46  H4sIAAAAAAAAA+NgFjrNIsWRmVeSWpSXmKPExsUixG6nrruq+FaAwc+tChY3WrsZLZqmO1us\r
47         nstjcf3mTGYHFo+ds+6ye9y6/5rd49mqW8weWw69Zw5gieKySUnNySxLLdK3S+DKWLsls+CJ\r
48         fkXP3R9MDYxXlbsYOTkkBEwk3qxczAxhi0lcuLeerYuRi0NIYB+jxL+lK9lBEkICGxglbr0v\r
49         hUhcZJI48eIdK4Qzl1Fiw72TrCBVbAIaEtv2L2cEsUUEpCV23p0NFmcWyJN4+Ggr0CQODmEB\r
50         V4l7H0RATBYBVYmGtfIgFbwCDhL/Du5igjhCUaL72QQ2kBJOAUeJnQ/qIE5wkJj48DzTBEb+\r
51         BYwMqxhlU3KrdHMTM3OKU5N1i5MT8/JSi3SN9HIzS/RSU0o3MYLCjFOSdwfju4NKhxgFOBiV\r
52         eHg3fL8ZIMSaWFZcmXuIUZKDSUmUd3vBrQAhvqT8lMqMxOKM+KLSnNTiQ4wSHMxKIrzOH4HK\r
53         eVMSK6tSi/JhUtIcLErivFdSbvoLCaQnlqRmp6YWpBbBZGU4OJQkeMuKgIYKFqWmp1akZeaU\r
54         IKSZODhBhvMADU8GqeEtLkjMLc5Mh8ifYlSUEucNAkkIgCQySvPgemFp4BWjONArwryuIFU8\r
55         wBQC1/0KaDAT0OBYvhsgg0sSEVJSDYyqx+8VJHZdcmdx8rW99z43+dvW1/WRNyqPBjkcZggr\r
56         z17ku6Q2syvrt8rfL5U7rR+eb+ue8e2Qb4+SjHdSq9dUvbRtwkx14XyyHtO/MQYzcy0yPpaw\r
57         Pfb26s7ZAYzf3vNMTTH+9omd/cIkHvWl5vE/c7/NeHzJLd2oxLm5RzTNRfXzb80bf5VYijMS\r
58         DbWYi4oTASXkZR/eAgAA\r
59 X-BeenThere: notmuch@notmuchmail.org\r
60 X-Mailman-Version: 2.1.13\r
61 Precedence: list\r
62 List-Id: "Use and development of the notmuch mail system."\r
63         <notmuch.notmuchmail.org>\r
64 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
65         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
66 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
67 List-Post: <mailto:notmuch@notmuchmail.org>\r
68 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
69 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
70         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
71 X-List-Received-Date: Wed, 26 Dec 2012 03:49:10 -0000\r
72 \r
73 From: Austin Clements <amdragon@MIT.EDU>\r
74 \r
75 This is now a generic boolean term quoting function.  It performs\r
76 minimal quoting to produce user-friendly queries.\r
77 \r
78 This could live in tag-util as well, but it is really nothing specific\r
79 to tags (although the conventions are specific to Xapian).\r
80 \r
81 The API is changed from "caller-allocates" to "readline-like".  The\r
82 scan for max tag length is pushed down into the quoting routine.\r
83 Furthermore, this now combines the term prefix with the quoted term;\r
84 arguably this is just as easy to do in the caller, but this will\r
85 nicely parallel the boolean term parsing function to be introduced\r
86 shortly.\r
87 \r
88 This is an amalgamation of code written by David Bremner and myself.\r
89 ---\r
90  notmuch-tag.c      |   48 ++++++++++++---------------------------\r
91  util/string-util.c |   64 ++++++++++++++++++++++++++++++++++++++++++++++++++++\r
92  util/string-util.h |    9 ++++++++\r
93  3 files changed, 87 insertions(+), 34 deletions(-)\r
94 \r
95 diff --git a/notmuch-tag.c b/notmuch-tag.c\r
96 index 88d559b..fc9d43a 100644\r
97 --- a/notmuch-tag.c\r
98 +++ b/notmuch-tag.c\r
99 @@ -19,6 +19,7 @@\r
100   */\r
101  \r
102  #include "notmuch-client.h"\r
103 +#include "string-util.h"\r
104  \r
105  static volatile sig_atomic_t interrupted;\r
106  \r
107 @@ -35,25 +36,6 @@ handle_sigint (unused (int sig))\r
108      interrupted = 1;\r
109  }\r
110  \r
111 -static char *\r
112 -_escape_tag (char *buf, const char *tag)\r
113 -{\r
114 -    const char *in = tag;\r
115 -    char *out = buf;\r
116 -\r
117 -    /* Boolean terms surrounded by double quotes can contain any\r
118 -     * character.  Double quotes are quoted by doubling them. */\r
119 -    *out++ = '"';\r
120 -    while (*in) {\r
121 -       if (*in == '"')\r
122 -           *out++ = '"';\r
123 -       *out++ = *in++;\r
124 -    }\r
125 -    *out++ = '"';\r
126 -    *out = 0;\r
127 -    return buf;\r
128 -}\r
129 -\r
130  typedef struct {\r
131      const char *tag;\r
132      notmuch_bool_t remove;\r
133 @@ -71,25 +53,16 @@ _optimize_tag_query (void *ctx, const char *orig_query_string,\r
134       * parenthesize and the exclusion part of the query must not use\r
135       * the '-' operator (though the NOT operator is fine). */\r
136  \r
137 -    char *escaped, *query_string;\r
138 +    char *escaped = NULL;\r
139 +    size_t escaped_len = 0;\r
140 +    char *query_string;\r
141      const char *join = "";\r
142 -    int i;\r
143 -    unsigned int max_tag_len = 0;\r
144 +    size_t i;\r
145  \r
146      /* Don't optimize if there are no tag changes. */\r
147      if (tag_ops[0].tag == NULL)\r
148         return talloc_strdup (ctx, orig_query_string);\r
149  \r
150 -    /* Allocate a buffer for escaping tags.  This is large enough to\r
151 -     * hold a fully escaped tag with every character doubled plus\r
152 -     * enclosing quotes and a NUL. */\r
153 -    for (i = 0; tag_ops[i].tag; i++)\r
154 -       if (strlen (tag_ops[i].tag) > max_tag_len)\r
155 -           max_tag_len = strlen (tag_ops[i].tag);\r
156 -    escaped = talloc_array (ctx, char, max_tag_len * 2 + 3);\r
157 -    if (! escaped)\r
158 -       return NULL;\r
159 -\r
160      /* Build the new query string */\r
161      if (strcmp (orig_query_string, "*") == 0)\r
162         query_string = talloc_strdup (ctx, "(");\r
163 @@ -97,10 +70,17 @@ _optimize_tag_query (void *ctx, const char *orig_query_string,\r
164         query_string = talloc_asprintf (ctx, "( %s ) and (", orig_query_string);\r
165  \r
166      for (i = 0; tag_ops[i].tag && query_string; i++) {\r
167 +       /* XXX in case of OOM, query_string will be deallocated when\r
168 +        * ctx is, which might be at shutdown */\r
169 +       if (make_boolean_term (ctx,\r
170 +                              "tag", tag_ops[i].tag,\r
171 +                              &escaped, &escaped_len))\r
172 +           return NULL;\r
173 +\r
174         query_string = talloc_asprintf_append_buffer (\r
175 -           query_string, "%s%stag:%s", join,\r
176 +           query_string, "%s%s%s", join,\r
177             tag_ops[i].remove ? "" : "not ",\r
178 -           _escape_tag (escaped, tag_ops[i].tag));\r
179 +           escaped);\r
180         join = " or ";\r
181      }\r
182  \r
183 diff --git a/util/string-util.c b/util/string-util.c\r
184 index 44f8cd3..e4bea21 100644\r
185 --- a/util/string-util.c\r
186 +++ b/util/string-util.c\r
187 @@ -20,6 +20,7 @@\r
188  \r
189  \r
190  #include "string-util.h"\r
191 +#include "talloc.h"\r
192  \r
193  char *\r
194  strtok_len (char *s, const char *delim, size_t *len)\r
195 @@ -32,3 +33,66 @@ strtok_len (char *s, const char *delim, size_t *len)\r
196  \r
197      return *len ? s : NULL;\r
198  }\r
199 +\r
200 +int\r
201 +make_boolean_term (void *ctx, const char *prefix, const char *term,\r
202 +                  char **buf, size_t *len)\r
203 +{\r
204 +    const char *in;\r
205 +    char *out;\r
206 +    size_t needed = 3;\r
207 +    int need_quoting = 0;\r
208 +\r
209 +    /* Do we need quoting?  To be paranoid, we quote anything\r
210 +     * containing a quote, even though it only matters at the\r
211 +     * beginning, and anything containing non-ASCII text. */\r
212 +    for (in = term; *in && !need_quoting; in++)\r
213 +       if (*in <= ' ' || *in == ')' || *in == '"' || (unsigned char)*in > 127)\r
214 +           need_quoting = 1;\r
215 +\r
216 +    if (need_quoting)\r
217 +       for (in = term; *in; in++)\r
218 +           needed += (*in == '"') ? 2 : 1;\r
219 +    else\r
220 +       needed = strlen (term) + 1;\r
221 +\r
222 +    /* Reserve space for the prefix */\r
223 +    if (prefix)\r
224 +       needed += strlen (prefix) + 1;\r
225 +\r
226 +    if ((*buf == NULL) || (needed > *len)) {\r
227 +       *len = 2 * needed;\r
228 +       *buf = talloc_realloc (ctx, *buf, char, *len);\r
229 +    }\r
230 +\r
231 +    if (! *buf)\r
232 +       return 1;\r
233 +\r
234 +    out = *buf;\r
235 +\r
236 +    /* Copy in the prefix */\r
237 +    if (prefix) {\r
238 +       strcpy (out, prefix);\r
239 +       out += strlen (prefix);\r
240 +       *out++ = ':';\r
241 +    }\r
242 +\r
243 +    if (! need_quoting) {\r
244 +       strcpy (out, term);\r
245 +       return 0;\r
246 +    }\r
247 +\r
248 +    /* Quote term by enclosing it in double quotes and doubling any\r
249 +     * internal double quotes. */\r
250 +    *out++ = '"';\r
251 +    in = term;\r
252 +    while (*in) {\r
253 +       if (*in == '"')\r
254 +           *out++ = '"';\r
255 +       *out++ = *in++;\r
256 +    }\r
257 +    *out++ = '"';\r
258 +    *out = '\0';\r
259 +\r
260 +    return 0;\r
261 +}\r
262 diff --git a/util/string-util.h b/util/string-util.h\r
263 index ac7676c..7475e2c 100644\r
264 --- a/util/string-util.h\r
265 +++ b/util/string-util.h\r
266 @@ -19,4 +19,13 @@\r
267  \r
268  char *strtok_len (char *s, const char *delim, size_t *len);\r
269  \r
270 +/* Construct a boolean term query with the specified prefix (e.g.,\r
271 + * "id") and search term, quoting term as necessary.\r
272 + *\r
273 + * Output is into buf; it may be talloc_realloced.\r
274 + * Return: 0 on success, non-zero on memory allocation failure.\r
275 + */\r
276 +int make_boolean_term (void *talloc_ctx, const char *prefix, const char *term,\r
277 +                      char **buf, size_t *len);\r
278 +\r
279  #endif\r
280 -- \r
281 1.7.10.4\r
282 \r