[PATCH v3 4/7] Add internal functions to search for alternate doc types
[notmuch-archives.git] / aa / 91b34bc40b6b1abad68dca52c423e8e85e7c95
1 Return-Path: <bremner@tethera.net>\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 53235431FBF\r
6         for <notmuch@notmuchmail.org>; Sun, 23 Dec 2012 17:40:14 -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: 0\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
12         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 KrKXEzMdKkt9 for <notmuch@notmuchmail.org>;\r
16         Sun, 23 Dec 2012 17:40:12 -0800 (PST)\r
17 Received: from tesseract.cs.unb.ca (tesseract.cs.unb.ca [131.202.240.238])\r
18         (using TLSv1 with cipher AES256-SHA (256/256 bits))\r
19         (No client certificate requested)\r
20         by olra.theworths.org (Postfix) with ESMTPS id 919E0431FCB\r
21         for <notmuch@notmuchmail.org>; Sun, 23 Dec 2012 17:40:02 -0800 (PST)\r
22 Received: from fctnnbsc30w-156034082078.dhcp-dynamic.fibreop.nb.bellaliant.net\r
23         ([156.34.82.78] helo=zancas.localnet)\r
24         by tesseract.cs.unb.ca with esmtpsa\r
25         (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.72)\r
26         (envelope-from <bremner@tethera.net>) id 1Tmx1K-0008Kj-SZ\r
27         for notmuch@notmuchmail.org; Sun, 23 Dec 2012 21:40:01 -0400\r
28 Received: from bremner by zancas.localnet with local (Exim 4.80)\r
29         (envelope-from <bremner@tethera.net>) id 1Tmx1F-0002nD-C6\r
30         for notmuch@notmuchmail.org; Sun, 23 Dec 2012 21:39:53 -0400\r
31 From: david@tethera.net\r
32 To: notmuch@notmuchmail.org\r
33 Subject: v9 of batch tagging\r
34 Date: Sun, 23 Dec 2012 21:39:26 -0400\r
35 Message-Id: <1356313183-9266-1-git-send-email-david@tethera.net>\r
36 X-Mailer: git-send-email 1.7.10.4\r
37 X-Spam_bar: -\r
38 X-BeenThere: notmuch@notmuchmail.org\r
39 X-Mailman-Version: 2.1.13\r
40 Precedence: list\r
41 List-Id: "Use and development of the notmuch mail system."\r
42         <notmuch.notmuchmail.org>\r
43 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
44         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
45 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
46 List-Post: <mailto:notmuch@notmuchmail.org>\r
47 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
48 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
49         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
50 X-List-Received-Date: Mon, 24 Dec 2012 01:40:14 -0000\r
51 \r
52 This obsoletes \r
53 \r
54      id:1356095307-22895-1-git-send-email-david@tethera.net\r
55 \r
56 The main changes since v8 are the rebasing against the notmuch-restore\r
57 fixes in master, and the rewrite of the query (pre)-processing\r
58 unhex_and_quote. This incorporates the changes of\r
59 \r
60       id:1356231570-28232-1-git-send-email-david@tethera.net\r
61 \r
62 and  now handles '()'  (cf. id:87a9t5p4dz.fsf@qmul.ac.uk)\r
63 \r
64 With respect to \r
65 \r
66 ,----\r
67 | Finally, I don't know if a query can contain a : without being a\r
68 | prefix query. If it can that could end up being misquoted.\r
69 `----\r
70 \r
71 This is pretty easy to work around by encoding that :. I think unless\r
72 it is a problem in practice I prefer not to keep an explicity list of\r
73 prefixes here; recognizing prefixes should really be a service from\r
74 libnotmuch.\r
75 \r
76 I dropped two patches (strnspn and hex_invariant), but picked up a new\r
77 strtok variation. Probably the name strtok_len2 could be improved\r
78 (and I see there is a typo in the patch subject).\r
79 \r
80  [Patch v9 05/17] util/string-util: add a new string tokenized\r
81 \r
82 Finally I added a test for the new parenthesis handling.\r
83 \r
84 [Patch v9 17/17] test/tagging: add test for handling of parens\r
85 \r
86 Fixup wise, the tests needed to be adjusted a bit for () being delimiters, \r
87 and the man page as well.\r
88 \r
89 I added the fclose in id:87wqw9hf9a.fsf@oiva.home.nikula.org\r
90 \r
91 And I modified the return value per id:87zk15hi7f.fsf@oiva.home.nikula.org\r
92 \r
93 Here is the interdiff for unhex_and_quote:\r
94 \r
95 commit 67c6aee87db5c7da25529e1c0feb64e422abb4b7\r
96 Author: David Bremner <bremner@unb.ca>\r
97 Date:   Sat Dec 22 22:49:02 2012 -0400\r
98 \r
99     simplify unhex_and_quote, support parens\r
100     \r
101     the overgeneral definition of a prefix can be replaced by lower case\r
102     alphabetic, and still work fine with current notmuch query syntax.\r
103     \r
104     use () as delimiters in unhex_and_quote, preserve delimiters\r
105 \r
106 diff --git a/tag-util.c b/tag-util.c\r
107 index 6f62fe6..91f3603 100644\r
108 --- a/tag-util.c\r
109 +++ b/tag-util.c\r
110 @@ -56,6 +56,21 @@ illegal_tag (const char *tag, notmuch_bool_t remove)\r
111      return NULL;\r
112  }\r
113  \r
114 +/* Factor out the boilerplate to append a token to the query string.\r
115 + * For use in unhex_and_quote */\r
116 +\r
117 +static tag_parse_status_t\r
118 +append_tok (const char *tok, size_t tok_len,\r
119 +           const char *line_for_error, char **query_string)\r
120 +{\r
121 +\r
122 +    *query_string = talloc_strndup_append_buffer (*query_string, tok, tok_len);\r
123 +    if (*query_string == NULL)\r
124 +       return line_error (TAG_PARSE_OUT_OF_MEMORY, line_for_error, "aborting");\r
125 +\r
126 +    return TAG_PARSE_SUCCESS;\r
127 +}\r
128 +\r
129  /* Input is a hex encoded string, presumed to be a query for Xapian.\r
130   *\r
131   * Space delimited tokens are decoded and quoted, with '*' and prefixes\r
132 @@ -67,45 +82,41 @@ unhex_and_quote (void *ctx, char *encoded, const char *line_for_error,\r
133  {\r
134      char *tok = encoded;\r
135      size_t tok_len = 0;\r
136 +    size_t delim_len = 0;\r
137      char *buf = NULL;\r
138      size_t buf_len = 0;\r
139      tag_parse_status_t ret = TAG_PARSE_SUCCESS;\r
140  \r
141      *query_string = talloc_strdup (ctx, "");\r
142  \r
143 -    while ((tok = strtok_len (tok + tok_len, " ", &tok_len)) != NULL) {\r
144 +    while ((tok = strtok_len2 (tok + tok_len + delim_len, " ()",\r
145 +                              &tok_len, &delim_len)) != NULL) {\r
146  \r
147         size_t prefix_len;\r
148         char delim = *(tok + tok_len);\r
149  \r
150 -       *(tok + tok_len++) = '\0';\r
151 +       *(tok + tok_len) = '\0';\r
152  \r
153 -       prefix_len = hex_invariant (tok, tok_len);\r
154 +       /* The following matches a superset of prefixes currently\r
155 +        * used by notmuch */\r
156 +       prefix_len = strspn (tok, "abcdefghijklmnopqrstuvwxyz");\r
157  \r
158 -       if ((strcmp (tok, "*") == 0) || prefix_len >= tok_len - 1) {\r
159 +       if ((strcmp (tok, "*") == 0) || prefix_len == tok_len) {\r
160  \r
161             /* pass some things through without quoting or decoding.\r
162              * Note for '*' this is mandatory.\r
163              */\r
164  \r
165 -           if (! (*query_string = talloc_asprintf_append_buffer (\r
166 -                      *query_string, "%s%c", tok, delim))) {\r
167 -\r
168 -               ret = line_error (TAG_PARSE_OUT_OF_MEMORY,\r
169 -                                 line_for_error, "aborting");\r
170 -               goto DONE;\r
171 -           }\r
172 +           ret = append_tok (tok, tok_len, line_for_error, query_string);\r
173 +           if (ret) goto DONE;\r
174  \r
175         } else {\r
176             /* potential prefix: one for ':', then something after */\r
177 -           if ((tok_len - prefix_len > 2) && *(tok + prefix_len) == ':') {\r
178 -               if (! (*query_string = talloc_strndup_append (*query_string,\r
179 -                                                             tok,\r
180 -                                                             prefix_len + 1))) {\r
181 -                   ret = line_error (TAG_PARSE_OUT_OF_MEMORY,\r
182 -                                     line_for_error, "aborting");\r
183 -                   goto DONE;\r
184 -               }\r
185 +           if ((tok_len - prefix_len >= 2) && *(tok + prefix_len) == ':') {\r
186 +               ret = append_tok (tok, prefix_len + 1,\r
187 +                                 line_for_error, query_string);\r
188 +               if (ret) goto DONE;\r
189 +\r
190                 tok += prefix_len + 1;\r
191                 tok_len -= prefix_len + 1;\r
192             }\r
193 @@ -122,13 +133,15 @@ unhex_and_quote (void *ctx, char *encoded, const char *line_for_error,\r
194                 goto DONE;\r
195             }\r
196  \r
197 -           if (! (*query_string = talloc_asprintf_append_buffer (\r
198 -                      *query_string, "%s%c", buf, delim))) {\r
199 -               ret = line_error (TAG_PARSE_OUT_OF_MEMORY,\r
200 -                                 line_for_error, "aborting");\r
201 -               goto DONE;\r
202 -           }\r
203 +           ret = append_tok (buf, buf_len, line_for_error, query_string);\r
204 +           if (ret) goto DONE;\r
205         }\r
206 +       /* restore the string */\r
207 +       *(tok + tok_len) = delim;\r
208 +\r
209 +       /* copy any delimiters */\r
210 +       ret = append_tok (tok + tok_len, delim_len, line_for_error, query_string);\r
211 +       if (ret) goto DONE;\r
212      }\r
213  \r
214    DONE:\r
215 \r