database error
[notmuch-archives.git] / bf / 44516c2d50d126cadfe1814ebeffadc0dadb20
1 Return-Path: <jani@nikula.org>\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 9E5C6431FD9\r
6         for <notmuch@notmuchmail.org>; Tue, 30 Oct 2012 13:33:15 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
9 X-Spam-Flag: NO\r
10 X-Spam-Score: -0.7\r
11 X-Spam-Level: \r
12 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
13         tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
14 Received: from olra.theworths.org ([127.0.0.1])\r
15         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
16         with ESMTP id 8PnWgvqyyK22 for <notmuch@notmuchmail.org>;\r
17         Tue, 30 Oct 2012 13:33:12 -0700 (PDT)\r
18 Received: from mail-lb0-f181.google.com (mail-lb0-f181.google.com\r
19         [209.85.217.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
20         (No client certificate requested)\r
21         by olra.theworths.org (Postfix) with ESMTPS id 81F8C431FDC\r
22         for <notmuch@notmuchmail.org>; Tue, 30 Oct 2012 13:32:56 -0700 (PDT)\r
23 Received: by mail-lb0-f181.google.com with SMTP id gg6so567600lbb.26\r
24         for <notmuch@notmuchmail.org>; Tue, 30 Oct 2012 13:32:56 -0700 (PDT)\r
25 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
26         d=google.com; s=20120113;\r
27         h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references\r
28         :in-reply-to:references:mime-version:content-type\r
29         :content-transfer-encoding:x-gm-message-state;\r
30         bh=vMILaKO6smR7vl2i4sM9jBz5pOCabcqzPmPfd6V5VVo=;\r
31         b=RHK+BGp5teXvSQFmYinkRREeL/kiCl5w7i6W4rmmlLSi4dNnbYs5mqLa4n1WV4tnE+\r
32         a2XPwhExvdsQIrelXS+yH69QIIDQs1MerHUE+U24ZomDINw3E2MLDC19EAKlzkG33+rm\r
33         sEDQA/HKurUdAHuFu23Qpe+z0XdLPICsADhOD3GtRHOlhflfaXLD+ya0JVLtOcmf1JEL\r
34         wGF1494vKs2jLH3QpBT/pYYiX+6Z7eYrPLhxZCRW2kigT2hgDV25xmbIdDnXFuoQBZpd\r
35         NfuTwkNqcE2ZfpIabVG3ly6G1Ka8BuN1G+e9VBcKVBUBp4G9XjXKjLG+3hPFgrZRjuTn\r
36         2Nww==\r
37 Received: by 10.152.104.148 with SMTP id ge20mr30985253lab.51.1351629175980;\r
38         Tue, 30 Oct 2012 13:32:55 -0700 (PDT)\r
39 Received: from localhost (dsl-hkibrasgw4-fe51df00-27.dhcp.inet.fi.\r
40         [80.223.81.27])\r
41         by mx.google.com with ESMTPS id jk8sm665831lab.7.2012.10.30.13.32.54\r
42         (version=SSLv3 cipher=OTHER); Tue, 30 Oct 2012 13:32:55 -0700 (PDT)\r
43 From: Jani Nikula <jani@nikula.org>\r
44 To: notmuch@notmuchmail.org\r
45 Subject:\r
46  [PATCH v6 3/9] test: add new test tool parse-time for date/time parser\r
47 Date: Tue, 30 Oct 2012 22:32:34 +0200\r
48 Message-Id:\r
49  <fe26c805b9e26bba9d93f705539faf13c12eb019.1351626272.git.jani@nikula.org>\r
50 X-Mailer: git-send-email 1.7.10.4\r
51 In-Reply-To: <cover.1351626272.git.jani@nikula.org>\r
52 References: <cover.1351626272.git.jani@nikula.org>\r
53 In-Reply-To: <cover.1351626272.git.jani@nikula.org>\r
54 References: <cover.1351626272.git.jani@nikula.org>\r
55 MIME-Version: 1.0\r
56 Content-Type: text/plain; charset=UTF-8\r
57 Content-Transfer-Encoding: 8bit\r
58 X-Gm-Message-State:\r
59  ALoCoQlbgj4AxEGxVu2A+zxoD7Paw61REWkLR5YurUqXGNjrRYj/NYbWbgSjqzT2UJ7KHYgsEViN\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: Tue, 30 Oct 2012 20:33:16 -0000\r
73 \r
74 Add a smoke testing tool to support testing the date/time parser\r
75 module directly and independent of the rest of notmuch.\r
76 \r
77 Credits to Michal Sojka <sojkam1@fel.cvut.cz> for the stdin parsing\r
78 idea and consequent massive improvement in testability.\r
79 ---\r
80  test/Makefile.local |    7 +-\r
81  test/basic          |    2 +-\r
82  test/parse-time.c   |  314 +++++++++++++++++++++++++++++++++++++++++++++++++++\r
83  3 files changed, 321 insertions(+), 2 deletions(-)\r
84  create mode 100644 test/parse-time.c\r
85 \r
86 diff --git a/test/Makefile.local b/test/Makefile.local\r
87 index 45df4c7..9ae130a 100644\r
88 --- a/test/Makefile.local\r
89 +++ b/test/Makefile.local\r
90 @@ -19,9 +19,13 @@ $(dir)/smtp-dummy: $(smtp_dummy_modules)\r
91  $(dir)/symbol-test: $(dir)/symbol-test.o\r
92         $(call quiet,CXX) $^ -o $@ -Llib -lnotmuch $(XAPIAN_LDFLAGS)\r
93  \r
94 +$(dir)/parse-time: $(dir)/parse-time.o parse-time-string/parse-time-string.o\r
95 +       $(call quiet,CC) $^ -o $@\r
96 +\r
97  .PHONY: test check\r
98  \r
99 -test-binaries: $(dir)/arg-test $(dir)/smtp-dummy $(dir)/symbol-test\r
100 +test-binaries: $(dir)/arg-test $(dir)/smtp-dummy $(dir)/symbol-test \\r
101 +       $(dir)/parse-time\r
102  \r
103  test:  all test-binaries\r
104         @${dir}/notmuch-test $(OPTIONS)\r
105 @@ -32,4 +36,5 @@ SRCS := $(SRCS) $(smtp_dummy_srcs)\r
106  CLEAN := $(CLEAN) $(dir)/smtp-dummy $(dir)/smtp-dummy.o \\r
107          $(dir)/symbol-test $(dir)/symbol-test.o \\r
108          $(dir)/arg-test $(dir)/arg-test.o \\r
109 +        $(dir)/parse-time $(dir)/parse-time.o \\r
110          $(dir)/corpus.mail $(dir)/test-results $(dir)/tmp.*\r
111 diff --git a/test/basic b/test/basic\r
112 index 3b635c8..c47197c 100755\r
113 --- a/test/basic\r
114 +++ b/test/basic\r
115 @@ -54,7 +54,7 @@ test_begin_subtest 'Ensure that all available tests will be run by notmuch-test'\r
116  eval $(sed -n -e '/^TESTS="$/,/^"$/p' $TEST_DIRECTORY/notmuch-test)\r
117  tests_in_suite=$(for i in $TESTS; do echo $i; done | sort)\r
118  available=$(find "$TEST_DIRECTORY" -maxdepth 1 -type f -perm +111 | \\r
119 -    sed -r -e "s,.*/,," -e "/^(aggregate-results.sh|notmuch-test|smtp-dummy|test-verbose|symbol-test|arg-test)$/d" | \\r
120 +    sed -r -e "s,.*/,," -e "/^(aggregate-results.sh|notmuch-test|smtp-dummy|test-verbose|symbol-test|arg-test|parse-time)$/d" | \\r
121      sort)\r
122  test_expect_equal "$tests_in_suite" "$available"\r
123  \r
124 diff --git a/test/parse-time.c b/test/parse-time.c\r
125 new file mode 100644\r
126 index 0000000..901a4dd\r
127 --- /dev/null\r
128 +++ b/test/parse-time.c\r
129 @@ -0,0 +1,314 @@\r
130 +/*\r
131 + * parse time string - user friendly date and time parser\r
132 + * Copyright © 2012 Jani Nikula\r
133 + *\r
134 + * This program is free software: you can redistribute it and/or modify\r
135 + * it under the terms of the GNU General Public License as published by\r
136 + * the Free Software Foundation, either version 2 of the License, or\r
137 + * (at your option) any later version.\r
138 + *\r
139 + * This program is distributed in the hope that it will be useful,\r
140 + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
141 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
142 + * GNU General Public License for more details.\r
143 + *\r
144 + * You should have received a copy of the GNU General Public License\r
145 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
146 + *\r
147 + * Author: Jani Nikula <jani@nikula.org>\r
148 + */\r
149 +\r
150 +#include <assert.h>\r
151 +#include <ctype.h>\r
152 +#include <getopt.h>\r
153 +#include <stdio.h>\r
154 +#include <stdlib.h>\r
155 +#include <string.h>\r
156 +\r
157 +#include "parse-time-string.h"\r
158 +\r
159 +#define ARRAY_SIZE(a) (sizeof (a) / sizeof (a[0]))\r
160 +\r
161 +static const char *parse_time_error_strings[] = {\r
162 +    [PARSE_TIME_OK]                    = "OK",\r
163 +    [PARSE_TIME_ERR]                   = "ERR",\r
164 +    [PARSE_TIME_ERR_LIB]               = "LIB",\r
165 +    [PARSE_TIME_ERR_ALREADYSET]                = "ALREADYSET",\r
166 +    [PARSE_TIME_ERR_FORMAT]            = "FORMAT",\r
167 +    [PARSE_TIME_ERR_DATEFORMAT]                = "DATEFORMAT",\r
168 +    [PARSE_TIME_ERR_TIMEFORMAT]                = "TIMEFORMAT",\r
169 +    [PARSE_TIME_ERR_INVALIDDATE]       = "INVALIDDATE",\r
170 +    [PARSE_TIME_ERR_INVALIDTIME]       = "INVALIDTIME",\r
171 +    [PARSE_TIME_ERR_KEYWORD]           = "KEYWORD",\r
172 +};\r
173 +\r
174 +static const char *\r
175 +parse_time_strerror (unsigned int errnum)\r
176 +{\r
177 +    if (errnum < ARRAY_SIZE (parse_time_error_strings))\r
178 +       return parse_time_error_strings[errnum];\r
179 +    else\r
180 +       return NULL;\r
181 +}\r
182 +\r
183 +/*\r
184 + * concat argv[start]...argv[end - 1], separating them by a single\r
185 + * space, to a malloced string\r
186 + */\r
187 +static char *\r
188 +concat_args (int start, int end, char *argv[])\r
189 +{\r
190 +    int i;\r
191 +    size_t len = 1;\r
192 +    char *p;\r
193 +\r
194 +    for (i = start; i < end; i++)\r
195 +       len += strlen (argv[i]) + 1;\r
196 +\r
197 +    p = malloc (len);\r
198 +    if (!p)\r
199 +       return NULL;\r
200 +\r
201 +    *p = 0;\r
202 +\r
203 +    for (i = start; i < end; i++) {\r
204 +       if (i != start)\r
205 +           strcat (p, " ");\r
206 +       strcat (p, argv[i]);\r
207 +    }\r
208 +\r
209 +    return p;\r
210 +}\r
211 +\r
212 +#define DEFAULT_FORMAT "%a %b %d %T %z %Y"\r
213 +\r
214 +static void\r
215 +usage (const char *name)\r
216 +{\r
217 +    printf ("Usage: %s [options ...] [<date/time>]\n\n", name);\r
218 +    printf (\r
219 +       "Parse <date/time> and display it in given format. If <date/time> is\n"\r
220 +       "not given, parse each line in stdin according to:\n\n"\r
221 +       "  <date/time> [(==>|==_>|==^>|==^^>)<ignored>] [#<comment>]\n\n"\r
222 +       "and produce output:\n\n"\r
223 +       "  <date/time> (==>|==_>|==^>|==^^>) <time in --format=FMT> [#<comment>]\n\n"\r
224 +       "preserving whitespace and comment in input. The operators ==>, ==_>,\n"\r
225 +       "==^>, and ==^^> define rounding as no rounding, round down, round up\n"\r
226 +       "inclusive, and round up, respectively.\n\n"\r
227 +\r
228 +       "  -f, --format=FMT output format, FMT according to strftime(3)\n"\r
229 +       "                   (default: \"%s\")\n"\r
230 +       "  -r, --ref=N      use N seconds since epoch as reference time\n"\r
231 +       "                   (default: now)\n"\r
232 +       "  -u, --^          round result up inclusive (default: no rounding)\n"\r
233 +       "  -U, --^^         round result up (default: no rounding)\n"\r
234 +       "  -d, --_          round result down (default: no rounding)\n"\r
235 +       "  -h, --help       print this help\n",\r
236 +       DEFAULT_FORMAT);\r
237 +}\r
238 +\r
239 +struct {\r
240 +    const char *operator;\r
241 +    int round;\r
242 +} operators[] = {\r
243 +    { "==>",   PARSE_TIME_NO_ROUND },\r
244 +    { "==_>",  PARSE_TIME_ROUND_DOWN },\r
245 +    { "==^>",  PARSE_TIME_ROUND_UP_INCLUSIVE },\r
246 +    { "==^^>", PARSE_TIME_ROUND_UP },\r
247 +};\r
248 +\r
249 +static const char *\r
250 +find_operator_in_string (char *str, char **ptr, int *round)\r
251 +{\r
252 +    const char *oper = NULL;\r
253 +    unsigned int i;\r
254 +\r
255 +    for (i = 0; i < ARRAY_SIZE (operators); i++) {\r
256 +       char *p = strstr (str, operators[i].operator);\r
257 +       if (p) {\r
258 +           if (round)\r
259 +               *round = operators[i].round;\r
260 +           if (ptr)\r
261 +               *ptr = p;\r
262 +\r
263 +           oper = operators[i].operator;\r
264 +           break;\r
265 +       }\r
266 +    }\r
267 +\r
268 +    return oper;\r
269 +}\r
270 +\r
271 +static const char *\r
272 +get_operator (int round)\r
273 +{\r
274 +    const char *oper = NULL;\r
275 +    unsigned int i;\r
276 +\r
277 +    for (i = 0; i < ARRAY_SIZE(operators); i++) {\r
278 +       if (round == operators[i].round) {\r
279 +           oper = operators[i].operator;\r
280 +           break;\r
281 +       }\r
282 +    }\r
283 +\r
284 +    return oper;\r
285 +}\r
286 +\r
287 +static int\r
288 +parse_stdin (FILE *infile, time_t *ref, int round, const char *format)\r
289 +{\r
290 +    char *input = NULL;\r
291 +    char result[1024];\r
292 +    size_t inputsize;\r
293 +    ssize_t len;\r
294 +    struct tm tm;\r
295 +    time_t t;\r
296 +    int r;\r
297 +\r
298 +    while ((len = getline (&input, &inputsize, infile)) != -1) {\r
299 +       const char *oper;\r
300 +       char *trail, *tmp;\r
301 +\r
302 +       /* trail is trailing whitespace and (optional) comment */\r
303 +       trail = strchr (input, '#');\r
304 +       if (!trail)\r
305 +           trail = input + len;\r
306 +\r
307 +       while (trail > input && isspace ((unsigned char) *(trail-1)))\r
308 +           trail--;\r
309 +\r
310 +       if (trail == input) {\r
311 +           printf ("%s", input);\r
312 +           continue;\r
313 +       }\r
314 +\r
315 +       tmp = strdup (trail);\r
316 +       if (!tmp) {\r
317 +           fprintf (stderr, "strdup() failed\n");\r
318 +           continue;\r
319 +       }\r
320 +       *trail = '\0';\r
321 +       trail = tmp;\r
322 +\r
323 +       /* operator */\r
324 +       oper = find_operator_in_string (input, &tmp, &round);\r
325 +       if (oper) {\r
326 +           *tmp = '\0';\r
327 +       } else {\r
328 +           oper = get_operator (round);\r
329 +           assert (oper);\r
330 +       }\r
331 +\r
332 +       r = parse_time_string (input, &t, ref, round);\r
333 +       if (!r) {\r
334 +           if (!localtime_r (&t, &tm)) {\r
335 +               fprintf (stderr, "localtime_r() failed\n");\r
336 +               free (trail);\r
337 +               continue;\r
338 +           }\r
339 +\r
340 +           strftime (result, sizeof (result), format, &tm);\r
341 +       } else {\r
342 +           const char *errstr = parse_time_strerror (r);\r
343 +           if (errstr)\r
344 +               snprintf (result, sizeof (result), "ERROR: %s", errstr);\r
345 +           else\r
346 +               snprintf (result, sizeof (result), "ERROR: %d", r);\r
347 +       }\r
348 +\r
349 +       printf ("%s%s %s%s", input, oper, result, trail);\r
350 +       free (trail);\r
351 +    }\r
352 +\r
353 +    free (input);\r
354 +\r
355 +    return 0;\r
356 +}\r
357 +\r
358 +int\r
359 +main (int argc, char *argv[])\r
360 +{\r
361 +    int r;\r
362 +    struct tm tm;\r
363 +    time_t result;\r
364 +    time_t now;\r
365 +    time_t *nowp = NULL;\r
366 +    char *argstr;\r
367 +    int round = PARSE_TIME_NO_ROUND;\r
368 +    char buf[1024];\r
369 +    const char *format = DEFAULT_FORMAT;\r
370 +    struct option options[] = {\r
371 +       { "help",       no_argument,            NULL,   'h' },\r
372 +       { "^",          no_argument,            NULL,   'u' },\r
373 +       { "^^",         no_argument,            NULL,   'U' },\r
374 +       { "_",          no_argument,            NULL,   'd' },\r
375 +       { "format",     required_argument,      NULL,   'f' },\r
376 +       { "ref",        required_argument,      NULL,   'r' },\r
377 +       { NULL, 0, NULL, 0 },\r
378 +    };\r
379 +\r
380 +    for (;;) {\r
381 +       int c;\r
382 +\r
383 +       c = getopt_long (argc, argv, "huUdf:r:", options, NULL);\r
384 +       if (c == -1)\r
385 +           break;\r
386 +\r
387 +       switch (c) {\r
388 +       case 'f':\r
389 +           /* output format */\r
390 +           format = optarg;\r
391 +           break;\r
392 +       case 'u':\r
393 +           round = PARSE_TIME_ROUND_UP_INCLUSIVE;\r
394 +           break;\r
395 +       case 'U':\r
396 +           round = PARSE_TIME_ROUND_UP;\r
397 +           break;\r
398 +       case 'd':\r
399 +           round = PARSE_TIME_ROUND_DOWN;\r
400 +           break;\r
401 +       case 'r':\r
402 +           /* specify now in seconds since epoch */\r
403 +           now = (time_t) strtol (optarg, NULL, 10);\r
404 +           if (now >= (time_t) 0)\r
405 +               nowp = &now;\r
406 +           break;\r
407 +       case 'h':\r
408 +       case '?':\r
409 +       default:\r
410 +           usage (argv[0]);\r
411 +           return 1;\r
412 +       }\r
413 +    }\r
414 +\r
415 +    if (optind == argc)\r
416 +       return parse_stdin (stdin, nowp, round, format);\r
417 +\r
418 +    argstr = concat_args (optind, argc, argv);\r
419 +    if (!argstr)\r
420 +       return 1;\r
421 +\r
422 +    r = parse_time_string (argstr, &result, nowp, round);\r
423 +\r
424 +    free (argstr);\r
425 +\r
426 +    if (r) {\r
427 +       const char *errstr = parse_time_strerror (r);\r
428 +       if (errstr)\r
429 +           fprintf (stderr, "ERROR: %s\n", errstr);\r
430 +       else\r
431 +           fprintf (stderr, "ERROR: %d\n", r);\r
432 +\r
433 +       return r;\r
434 +    }\r
435 +\r
436 +    if (!localtime_r (&result, &tm))\r
437 +       return 1;\r
438 +\r
439 +    strftime (buf, sizeof (buf), format, &tm);\r
440 +    printf ("%s\n", buf);\r
441 +\r
442 +    return 0;\r
443 +}\r
444 -- \r
445 1.7.10.4\r
446 \r