summaryrefslogtreecommitdiff
path: root/libmailutils/datetime/scantime.c
blob: 670052bb34f0e8d886c931a8a81bd15f2d28f41f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 1999, 2000, 2001, 2002, 2007, 2009, 2010, 2011 Free
   Software Foundation, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 3 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General
   Public License along with this library.  If not, see
   <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mailutils/diag.h>
#include <mailutils/datetime.h>
#include <mailutils/util.h>
#include <mailutils/stream.h>
#include <mailutils/errno.h>
#include <mailutils/cstr.h>
#include <mailutils/cctype.h>

static int
_mu_short_weekday_string (const char *str)
{
  int i;
  
  for (i = 0; i < 7; i++)
    {
      if (mu_c_strncasecmp (str, _mu_datetime_short_wday[i], 3) == 0)
	return i;
    }
  return -1;
}

static int
_mu_full_weekday_string (const char *str, char **endp)
{
  int i;
  
  for (i = 0; i < 7; i++)
    {
      if (mu_c_strcasecmp (str, _mu_datetime_full_wday[i]) == 0)
	{
	  if (endp)
	    *endp = (char*) (str + strlen (_mu_datetime_full_wday[i]));
	  return i;
	}
    }
  return -1;
}

static int
_mu_short_month_string (const char *str)
{  
  int i;
  
  for (i = 0; i < 12; i++)
    {
      if (mu_c_strncasecmp (str, _mu_datetime_short_month[i], 3) == 0)
	return i;
    }
  return -1;
}

static int
_mu_full_month_string (const char *str, char **endp)
{  
  int i;
  
  for (i = 0; i < 12; i++)
    {
      if (mu_c_strcasecmp (str, _mu_datetime_full_month[i]) == 0)
	{
	  if (endp)
	    *endp = (char*) (str + strlen (_mu_datetime_full_month[i]));
	  return i;
	}
    }
  return -1;
}

int
get_num (const char *str, char **endp, int ndig, int minval, int maxval,
	 int *pn)
{
  int x = 0;
  int i;
  
  errno = 0;
  for (i = 0; i < ndig && *str && mu_isdigit (*str); str++, i++)
    x = x * 10 + *str - '0';

  *endp = (char*) str;
  if (i == 0)
    return -1;
  else if (pn)
    *pn = i;
  else if (i != ndig)
    return -1;
  if (x < minval || x > maxval)
    return -1;
  return x;
}

#define DT_YEAR  0x01
#define DT_MONTH 0x02
#define DT_MDAY  0x04
#define DT_WDAY  0x08
#define DT_HOUR  0x10
#define DT_MIN   0x20
#define DT_SEC   0x40

#define ST_NON   -1
#define ST_OPT   0
#define ST_ALT   1

struct save_input
{
  int state;
  const char *input;
};

static int
push_input (mu_list_t *plist, int state, const char *input)
{
  mu_list_t list = *plist;
  struct save_input *inp = malloc (sizeof (*inp));
  if (!inp)
    return ENOMEM;
  if (!list)
    {
      int rc = mu_list_create (&list);
      if (rc)
	{
	  free (inp);
	  return rc;
	}
      mu_list_set_destroy_item (list, mu_list_free_item);
      *plist = list;
    }
  inp->state = state;
  inp->input = input;
  return mu_list_push (list, (void*)inp);
}

static int
peek_state (mu_list_t list, int *state, const char **input)
{
  int rc;
  struct save_input *inp;

  rc = mu_list_tail (list, (void**)&inp);
  if (rc)
    return rc;
  *state = inp->state;
  if (input)
    *input = inp->input;
  return 0;
}      

static int
pop_input (mu_list_t list, int *state, const char **input)
{
  int rc;
  struct save_input *inp;

  rc = mu_list_pop (list, (void**)&inp);
  if (rc)
    return rc;
  *state = inp->state;
  if (input)
    *input = inp->input;
  return 0;
}

static int
bracket_to_state (int c)
{
  switch (c)
    {
    case '[':
    case ']':
      return ST_OPT;
    case '(':
    case ')':
      return ST_ALT;
    }
  return ST_NON;
}

static int
state_to_closing_bracket (int st)
{
  switch (st)
    {
    case ST_OPT:
      return ']';
    case ST_ALT:
      return ')';
    }
  return '?';
}

static int
scan_recovery (const char *fmt, mu_list_t *plist, int skip_alt,
	       const char **endp,
	       const char **input)
{
  int c, rc = 0;
  int nesting_level = 1;
  int st;
  const char *p;
  
  while (*fmt)
    {
      c = *fmt++;
      
      if (c == '%')
	{
	  c = *fmt++;
	  if (!c)
	    {
	      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
			("%s:%d: error in format: %% at the end of input",
			 __FILE__, __LINE__));
	      rc = MU_ERR_FORMAT;
	      break;
	    }
	      
	  switch (c)
	    {
	    case '[':
	    case '(':
	      nesting_level++;
	      rc = push_input (plist, bracket_to_state (c), NULL);
	      break;
	      
	    case ')':
	    case ']':
	      rc = pop_input (*plist, &st, &p);
	      if (rc || st != bracket_to_state (c))
		{
		  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
			    ("%s:%d: error in format: %%%c out of context",
			     __FILE__, __LINE__, c));
		  rc = MU_ERR_FORMAT;
		  break;
		}
	      if (--nesting_level == 0)
		{
		  *endp = fmt;
		  if (skip_alt)
		    return 0;
		  *input = p;
		  if (st == ST_ALT)
		    {
		      if (*fmt == '%' && (fmt[1] == '|' || fmt[1] == ']'))
			return 0;
		      return MU_ERR_PARSE; /* No match found */
		    }
		  return 0;
		}
	      break;

	    case '|':
	      if (skip_alt)
		continue;
	      if (nesting_level == 1)
		{
		  *endp = fmt;
		  return peek_state (*plist, &st, input);
		}
	      break;

	    case '\\':
	      if (*++fmt == 0)
		{
		  peek_state (*plist, &st, NULL);
		  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
			    ("%s:%d: error in format: missing closing %%%c",
			     __FILE__, __LINE__,
			     state_to_closing_bracket (st)));
		  return MU_ERR_FORMAT;		  
		}
	    }
	}
    }
  
  peek_state (*plist, &st, NULL);
  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
	    ("%s:%d: error in format: missing closing %%%c",
	     __FILE__, __LINE__,
	     state_to_closing_bracket (st)));
  return MU_ERR_FORMAT;		  
}

int
mu_scan_datetime (const char *input, const char *fmt,
		  struct tm *tm, struct mu_timezone *tz, char **endp)
{
  int rc = 0;
  char *p;
  int n;
  int c;
  int st;
  int recovery = 0;
  int eof_ok = 0;
  int datetime_parts = 0;
  mu_list_t save_input_list = NULL;
  
  memset (tm, 0, sizeof *tm);
#ifdef HAVE_STRUCT_TM_TM_ISDST
  tm->tm_isdst = -1;	/* unknown. */
#endif
  /* provide default timezone, in case it is not supplied in input */
  if (tz)
    {
      memset (tz, 0, sizeof *tz);
      tz->utc_offset = mu_utc_offset ();
    }

  /* Skip leading whitespace */
  input = mu_str_skip_class (input, MU_CTYPE_BLANK);
  for (; *fmt && rc == 0; fmt++)
    {
      if (mu_isspace (*fmt))
	{
	  fmt = mu_str_skip_class (fmt, MU_CTYPE_BLANK);
	  input = mu_str_skip_class (input, MU_CTYPE_BLANK);
	  if (!*fmt)
	    break;
	}
      eof_ok = 0;
      
      if (*fmt == '%')
	{
	  c = *++fmt;
	  if (!c)
	    {
	      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
			("%s:%d: error in format: %% at the end of input",
			 __FILE__, __LINE__));
	      rc = MU_ERR_FORMAT;
	      break;
	    }
	  
	  switch (c)
	    {
	    case 'a':
	      /* The abbreviated weekday name. */
	      n = _mu_short_weekday_string (input);
	      if (n == -1)
		rc = MU_ERR_PARSE;
	      else
		{
		  tm->tm_wday = n;
		  datetime_parts |= DT_WDAY;
		  input += 3;
		}
	      break;
		  
	    case 'A':
	      /* The full weekday name. */
	      n = _mu_full_weekday_string (input, &p);
	      if (n == -1)
		rc = MU_ERR_PARSE;
	      else
		{
		  tm->tm_wday = n;
		  datetime_parts |= DT_WDAY;
		  input = p;
		}
	      break;
	      
	    case 'b':
	      /* The abbreviated month name. */
	      n = _mu_short_month_string (input);
	      if (n == -1)
		rc = MU_ERR_PARSE;
	      else
		{
		  tm->tm_mon = n;
		  datetime_parts |= DT_MONTH;
		  input += 3;
		}
	      break;

	    case 'B':
	      /* The full month name. */
	      n = _mu_full_month_string (input, &p);
	      if (n == -1)
		rc = MU_ERR_PARSE;
	      else
		{
		  tm->tm_mon = n;
		  datetime_parts |= DT_MONTH;
		  input = p;
		}
	      break;
	      
	    case 'd':
	      /* The day of the month as a decimal number (range 01 to 31). */
	      n = get_num (input, &p, 2, 1, 31, NULL);
	      if (n == -1)
		rc = MU_ERR_PARSE;
	      else
		{
		  tm->tm_mday = n;
		  datetime_parts |= DT_MDAY;
		  input = p;
		}
	      break;
	      
	    case 'e':
	      /* Like %d, the day of the month as a decimal number, but a
		 leading zero is replaced by a space. */
	      {
		int ndig;
		
		n = get_num (input, &p, 2, 1, 31, &ndig);
		if (n == -1)
		  rc = MU_ERR_PARSE;
		else
		  {
		    tm->tm_mday = n;
		    datetime_parts |= DT_MDAY;
		    input = p;
		  }
	      }
	      break;
	      
	    case 'H':
	      /* The hour as a decimal number using a 24-hour clock (range
		 00 to 23). */
	      n = get_num (input, &p, 2, 0, 23, NULL);
	      if (n == -1)
		rc = MU_ERR_PARSE;
	      else
		{
		  tm->tm_hour = n;
		  datetime_parts |= DT_HOUR;
		  input = p;
		}
	      break;
	      
	    case 'm':
	      /* The month as a decimal number (range 01 to 12). */
	      n = get_num (input, &p, 2, 1, 12, NULL);
	      if (n == -1)
		rc = MU_ERR_PARSE;
	      else
		{
		  tm->tm_mon = n - 1;
		  datetime_parts |= DT_MONTH;
		  input = p;
		}
	      break;
	      
	    case 'M':
	      /* The minute as a decimal number (range 00 to 59). */
	      n = get_num (input, &p, 2, 0, 59, NULL);
	      if (n == -1)
		rc = MU_ERR_PARSE;
	      else
		{
		  tm->tm_min = n;
		  datetime_parts |= DT_MIN;
		  input = p;
		}
	      break;
	      
	    case 'S':
	      /* The second as a decimal number (range 00 to 60) */
	      n = get_num (input, &p, 2, 0, 60, NULL);
	      if (n == -1)
		rc = MU_ERR_PARSE;
	      else
		{
		  tm->tm_sec = n;
		  datetime_parts |= DT_SEC;
		  input = p;
		}
	      break;
	      
	    case 'Y':
	      /* The year as a decimal number including the century. */
	      errno = 0;
	      n = strtoul (input, &p, 10);
	      if (errno || p == input)
		rc = MU_ERR_PARSE;
	      else
		{
		  tm->tm_year = n - 1900;
		  datetime_parts |= DT_YEAR;
		  input = p;
		}
	      break;
		  
	    case 'z':
	      /* The time-zone as hour offset from GMT */
	      {
		int sign = 1;
		int hr;
		
		if (*input == '+')
		  input++;
		else if (*input == '-')
		  {
		    input++;
		    sign = -1;
		  }
		n = get_num (input, &p, 2, 0, 11, NULL);
		if (n == -1)
		  rc = MU_ERR_PARSE;
		else
		  {
		    input = p;
		    hr = n;
		    n = get_num (input, &p, 2, 0, 59, NULL);
		    if (n == -1)
		      rc = MU_ERR_PARSE;
		    else
		      {
			input = p;
			if (tz)
			  tz->utc_offset = sign * (hr * 60 + n) * 60;
		      }
		  }
	      }
	      break;
		      
	    case '%':
	      if (*input == '%')
		input++;
	      else
		rc = MU_ERR_PARSE;
	      break;

	      rc = push_input (&save_input_list, ST_ALT, (void*)input);
	      break;
	      
	    case '(':
	    case '[':
	      rc = push_input (&save_input_list, bracket_to_state (c),
			       (void*)input);
	      break;

	    case ')':
	    case ']':
	      if (pop_input (save_input_list, &st, NULL))
		{
		  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
			    ("%s:%d: error in format: unbalanced %%%c near %s",
			     __FILE__, __LINE__, c, fmt));
		  rc = MU_ERR_FORMAT;
		}
	      else if (st != bracket_to_state (c))
		{
		  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
			    ("%s:%d: error in format: %%%c out of context",
			     __FILE__, __LINE__, c));
		  rc = MU_ERR_FORMAT;
		}
	      break;

	    case '|':
	      rc = scan_recovery (fmt, &save_input_list, 1, &fmt, NULL);
	      if (rc == 0)
		fmt--;
	      break;
	      
	    case '$':
	      eof_ok = 1;
	      break;

	    case '\\':
	      c = *++fmt;
	      if (!c)
		{
		  mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
			    ("%s:%d: error in format: %% at the end of input",
			     __FILE__, __LINE__));
		  rc = MU_ERR_FORMAT;
		}
	      else if (c == *input)
		input++;
	      else
		rc = MU_ERR_PARSE;
	      break;

	    case '?':
	      input++;
	      break;
	      
	    default:
	      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
			("%s:%d: error in format: unrecognized conversion type"
			 " near %s",
			 __FILE__, __LINE__, fmt));
	      rc = MU_ERR_FORMAT;
	      break;
	    }

	  if (eof_ok && rc == 0 && *input == 0)
	    break;
	}
      else if (!recovery && *input != *fmt)
	rc = MU_ERR_PARSE;
      else
	input++;

      if (rc == MU_ERR_PARSE && !mu_list_is_empty (save_input_list))
	{
	  rc = scan_recovery (fmt, &save_input_list, 0, &fmt, &input);
	  if (rc == 0)
	    --fmt;
	}
    }

  if (!mu_list_is_empty (save_input_list))
    {
      mu_debug (MU_DEBCAT_MAILBOX, MU_DEBUG_ERROR,
		("%s:%d: error in format: closing bracket missing",
		 __FILE__, __LINE__));
      rc = MU_ERR_FORMAT;
    }
  mu_list_destroy (&save_input_list);
  
  if (rc == 0 && recovery)
    rc = MU_ERR_PARSE;
  
  if (!eof_ok && rc == 0 && *input == 0 && *fmt)
    rc = MU_ERR_PARSE;

  if ((datetime_parts & (DT_YEAR|DT_MONTH|DT_MDAY)) ==
      (DT_YEAR|DT_MONTH|DT_MDAY))
    {
      if (!(datetime_parts & DT_WDAY))
	tm->tm_wday = mu_datetime_dayofweek (tm->tm_year + 1900,
					     tm->tm_mon + 1, tm->tm_mday);
      tm->tm_yday = mu_datetime_dayofyear (tm->tm_year + 1900,
					   tm->tm_mon + 1, tm->tm_mday) - 1;
    }
  
  if (endp)
    *endp = (char*) input;
  
  return rc;
}

Return to:

Send suggestions and report system problems to the System administrator.