Corrected logical errors in tests for error conditions.
[pspp] / src / data / data-in.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "data-in.h"
20
21 #include <ctype.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <math.h>
25 #include <stdarg.h>
26 #include <stdbool.h>
27 #include <stddef.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #include "calendar.h"
33 #include "dictionary.h"
34 #include "format.h"
35 #include "identifier.h"
36 #include "libpspp/assertion.h"
37 #include "libpspp/compiler.h"
38 #include "libpspp/i18n.h"
39 #include "libpspp/integer-format.h"
40 #include "libpspp/misc.h"
41 #include "libpspp/str.h"
42 #include "settings.h"
43 #include "value.h"
44
45 #include "gl/c-ctype.h"
46 #include "gl/c-strtod.h"
47 #include "gl/minmax.h"
48 #include "gl/xalloc.h"
49
50 #include "gettext.h"
51 #define _(msgid) gettext (msgid)
52 \f
53 /* Information about parsing one data field. */
54 struct data_in
55   {
56     struct substring input;     /* Source. */
57     enum fmt_type format;       /* Input format. */
58
59     union value *output;        /* Destination. */
60     int width;                  /* Output width. */
61   };
62
63 typedef char *data_in_parser_func (struct data_in *);
64 #define FMT(NAME, METHOD, IMIN, OMIN, IO, CATEGORY) \
65         static data_in_parser_func parse_##METHOD;
66 #include "format.def"
67
68 static void default_result (struct data_in *);
69 static bool trim_spaces_and_check_missing (struct data_in *);
70
71 static int hexit_value (int c);
72 \f
73 /* Parses the characters in INPUT, which are encoded in the given
74    INPUT_ENCODING, according to FORMAT.
75
76    Stores the parsed representation in OUTPUT, which the caller must have
77    initialized with the given WIDTH (0 for a numeric field, otherwise the
78    string width).  If FORMAT is FMT_A, then OUTPUT_ENCODING must specify the
79    correct encoding for OUTPUT (normally obtained via dict_get_encoding()). */
80 char *
81 data_in (struct substring input, const char *input_encoding,
82          enum fmt_type format,
83          union value *output, int width, const char *output_encoding)
84 {
85   static data_in_parser_func *const handlers[FMT_NUMBER_OF_FORMATS] =
86     {
87 #define FMT(NAME, METHOD, IMIN, OMIN, IO, CATEGORY) parse_##METHOD,
88 #include "format.def"
89     };
90
91   struct data_in i;
92
93   enum fmt_category cat;
94   const char *dest_encoding;
95   char *s;
96   char *error;
97
98   assert ((width != 0) == fmt_is_string (format));
99
100   i.format = format;
101
102   i.output = output;
103   i.width = width;
104
105   if (ss_is_empty (input))
106     {
107       default_result (&i);
108       return NULL;
109     }
110
111   cat = fmt_get_category (format);
112   if (cat & (FMT_CAT_BASIC | FMT_CAT_HEXADECIMAL
113              | FMT_CAT_DATE | FMT_CAT_TIME | FMT_CAT_DATE_COMPONENT))
114     {
115       /* We're going to parse these into numbers.  For this purpose we want to
116          deal with them in the local "C" encoding.  Any character not in that
117          encoding wouldn't be valid anyhow. */
118       dest_encoding = C_ENCODING;
119     }
120   else if (cat & (FMT_CAT_BINARY | FMT_CAT_LEGACY))
121     {
122       /* Don't recode these binary formats at all, since they are not text. */
123       dest_encoding = NULL;
124     }
125   else
126     {
127       assert (cat == FMT_CAT_STRING);
128       if (format == FMT_AHEX)
129         {
130           /* We want the hex digits in the local "C" encoding, even though the
131              result may not be in that encoding. */
132           dest_encoding = C_ENCODING;
133         }
134       else
135         {
136           /* Use the final output encoding. */
137           dest_encoding = output_encoding;
138         }
139     }
140
141   if (dest_encoding != NULL)
142     {
143       i.input = recode_substring_pool (dest_encoding, input_encoding, input,
144                                        NULL);
145       s = i.input.string;
146     }
147   else
148     {
149       i.input = input;
150       s = NULL;
151     }
152
153   error = handlers[i.format] (&i);
154   if (error != NULL)
155     default_result (&i);
156
157   free (s);
158
159   return error;
160 }
161
162 bool
163 data_in_msg (struct substring input, const char *input_encoding,
164              enum fmt_type format,
165              union value *output, int width, const char *output_encoding)
166 {
167   char *error = data_in (input, input_encoding, format,
168                          output, width, output_encoding);
169   if (error != NULL)
170     {
171       msg (SW,_("Data is not valid as format %s: %s"),
172            fmt_name (format), error);
173       free (error);
174       return false;
175     }
176   else
177     return true;
178 }
179
180 static bool
181 number_has_implied_decimals (const char *s, enum fmt_type type)
182 {
183   int decimal = settings_get_style (type)->decimal;
184   bool got_digit = false;
185   for (;;)
186     {
187       switch (*s)
188         {
189         case '0': case '1': case '2': case '3': case '4':
190         case '5': case '6': case '7': case '8': case '9':
191           got_digit = true;
192           break;
193
194         case '+': case '-':
195           if (got_digit)
196             return false;
197           break;
198
199         case 'e': case 'E': case 'd': case 'D':
200           return false;
201
202         case '.': case ',':
203           if (*s == decimal)
204             return false;
205           break;
206
207         case '\0':
208           return true;
209
210         default:
211           break;
212         }
213
214       s++;
215     }
216 }
217
218 static bool
219 has_implied_decimals (struct substring input, const char *input_encoding,
220                       enum fmt_type format)
221 {
222   bool retval;
223   char *s;
224
225   switch (format)
226     {
227     case FMT_F:
228     case FMT_COMMA:
229     case FMT_DOT:
230     case FMT_DOLLAR:
231     case FMT_PCT:
232     case FMT_E:
233     case FMT_Z:
234       break;
235
236     case FMT_N:
237     case FMT_IB:
238     case FMT_PIB:
239     case FMT_P:
240     case FMT_PK:
241       return true;
242
243     default:
244       return false;
245     }
246
247   s = recode_string (C_ENCODING, input_encoding,
248                      ss_data (input), ss_length (input));
249   retval = (format == FMT_Z
250             ? strchr (s, '.') == NULL
251             : number_has_implied_decimals (s, format));
252   free (s);
253
254   return retval;
255 }
256
257 /* In some cases, when no decimal point is explicitly included in numeric
258    input, its position is implied by the number of decimal places in the input
259    format.  In such a case, this function may be called just after data_in().
260    Its arguments are a subset of that function's arguments plus D, the number
261    of decimal places associated with FORMAT.
262
263    If it is appropriate, this function modifies the numeric value in OUTPUT. */
264 void
265 data_in_imply_decimals (struct substring input, const char *input_encoding,
266                         enum fmt_type format, int d, union value *output)
267 {
268   if (d > 0 && output->f != SYSMIS
269       && has_implied_decimals (input, input_encoding, format))
270     output->f /= pow (10., d);
271 }
272 \f
273 /* Format parsers. */
274
275 /* Parses F, COMMA, DOT, DOLLAR, PCT, and E input formats. */
276 static char *
277 parse_number (struct data_in *i)
278 {
279   const struct fmt_number_style *style =
280     settings_get_style (i->format);
281
282   struct string tmp;
283
284   int save_errno;
285   char *tail;
286
287   if  (fmt_get_category (i->format) == FMT_CAT_CUSTOM)
288     {
289       style = settings_get_style (FMT_F);
290     }
291
292   /* Trim spaces and check for missing value representation. */
293   if (trim_spaces_and_check_missing (i))
294     return NULL;
295
296   ds_init_empty (&tmp);
297   ds_extend (&tmp, 64);
298
299   /* Prefix character may precede sign. */
300   if (style->prefix.s[0] != '\0')
301     {
302       ss_match_byte (&i->input, style->prefix.s[0]);
303       ss_ltrim (&i->input, ss_cstr (CC_SPACES));
304     }
305
306   /* Sign. */
307   if (ss_match_byte (&i->input, '-'))
308     {
309       ds_put_byte (&tmp, '-');
310       ss_ltrim (&i->input, ss_cstr (CC_SPACES));
311     }
312   else
313     {
314       ss_match_byte (&i->input, '+');
315       ss_ltrim (&i->input, ss_cstr (CC_SPACES));
316     }
317
318   /* Prefix character may follow sign. */
319   if (style->prefix.s[0] != '\0')
320     {
321       ss_match_byte (&i->input, style->prefix.s[0]);
322       ss_ltrim (&i->input, ss_cstr (CC_SPACES));
323     }
324
325   /* Digits before decimal point. */
326   while (c_isdigit (ss_first (i->input)))
327     {
328       ds_put_byte (&tmp, ss_get_byte (&i->input));
329       if (style->grouping != 0)
330         ss_match_byte (&i->input, style->grouping);
331     }
332
333   /* Decimal point and following digits. */
334   if (ss_match_byte (&i->input, style->decimal))
335     {
336       ds_put_byte (&tmp, '.');
337       while (c_isdigit (ss_first (i->input)))
338         ds_put_byte (&tmp, ss_get_byte (&i->input));
339     }
340
341   /* Exponent. */
342   if (!ds_is_empty (&tmp)
343       && !ss_is_empty (i->input)
344       && strchr ("eEdD-+", ss_first (i->input)))
345     {
346       ds_put_byte (&tmp, 'e');
347
348       if (strchr ("eEdD", ss_first (i->input)))
349         {
350           ss_advance (&i->input, 1);
351           ss_match_byte (&i->input, ' ');
352         }
353
354       if (ss_first (i->input) == '-' || ss_first (i->input) == '+')
355         {
356           if (ss_get_byte (&i->input) == '-')
357             ds_put_byte (&tmp, '-');
358           ss_match_byte (&i->input, ' ');
359         }
360
361       while (c_isdigit (ss_first (i->input)))
362         ds_put_byte (&tmp, ss_get_byte (&i->input));
363     }
364
365   /* Suffix character. */
366   if (style->suffix.s[0] != '\0')
367     ss_match_byte (&i->input, style->suffix.s[0]);
368
369   if (!ss_is_empty (i->input))
370     {
371       char *error;
372       if (ds_is_empty (&tmp))
373         error = xstrdup (_("Field contents are not numeric."));
374       else
375         error = xstrdup (_("Number followed by garbage."));
376       ds_destroy (&tmp);
377       return error;
378     }
379
380   /* Let c_strtod() do the conversion. */
381   save_errno = errno;
382   errno = 0;
383   i->output->f = c_strtod (ds_cstr (&tmp), &tail);
384   if (*tail != '\0')
385     {
386       errno = save_errno;
387       ds_destroy (&tmp);
388       return xstrdup (_("Invalid numeric syntax."));
389     }
390   else if (errno == ERANGE)
391     {
392       if (fabs (i->output->f) > 1)
393         {
394           i->output->f = SYSMIS;
395           ds_destroy (&tmp);
396           return xstrdup (_("Too-large number set to system-missing."));
397         }
398       else
399         {
400           i->output->f = 0.0;
401           ds_destroy (&tmp);
402           return xstrdup (_("Too-small number set to zero."));
403         }
404     }
405   else
406     errno = save_errno;
407
408   ds_destroy (&tmp);
409   return NULL;
410 }
411
412 /* Parses N format. */
413 static char *
414 parse_N (struct data_in *i)
415 {
416   int c;
417
418   i->output->f = 0;
419   while ((c = ss_get_byte (&i->input)) != EOF)
420     {
421       if (!c_isdigit (c))
422         return xstrdup (_("All characters in field must be digits."));
423       i->output->f = i->output->f * 10.0 + (c - '0');
424     }
425
426   return NULL;
427 }
428
429 /* Parses PIBHEX format. */
430 static char *
431 parse_PIBHEX (struct data_in *i)
432 {
433   double n;
434   int c;
435
436   n = 0.0;
437
438   while ((c = ss_get_byte (&i->input)) != EOF)
439     {
440       if (!c_isxdigit (c))
441         return xstrdup (_("Unrecognized character in field."));
442       n = n * 16.0 + hexit_value (c);
443     }
444
445   i->output->f = n;
446   return NULL;
447 }
448
449 /* Parses RBHEX format. */
450 static char *
451 parse_RBHEX (struct data_in *i)
452 {
453   double d;
454   size_t j;
455
456   memset (&d, 0, sizeof d);
457   for (j = 0; !ss_is_empty (i->input) && j < sizeof d; j++)
458     {
459       int hi = ss_get_byte (&i->input);
460       int lo = ss_get_byte (&i->input);
461       if (lo == EOF)
462         return xstrdup (_("Field must have even length."));
463       else if (!c_isxdigit (hi) || !c_isxdigit (lo))
464         return xstrdup (_("Field must contain only hex digits."));
465       ((unsigned char *) &d)[j] = 16 * hexit_value (hi) + hexit_value (lo);
466     }
467
468   i->output->f = d;
469
470   return NULL;
471 }
472
473 /* Digits for Z format. */
474 static const char z_digits[] = "0123456789{ABCDEFGHI}JKLMNOPQR";
475
476 /* Returns true if C is a Z format digit, false otherwise. */
477 static bool
478 is_z_digit (int c)
479 {
480   return c > 0 && strchr (z_digits, c) != NULL;
481 }
482
483 /* Returns the (absolute value of the) value of C as a Z format
484    digit. */
485 static int
486 z_digit_value (int c)
487 {
488   assert (is_z_digit (c));
489   return (strchr (z_digits, c) - z_digits) % 10;
490 }
491
492 /* Returns true if Z format digit C represents a negative value,
493    false otherwise. */
494 static bool
495 is_negative_z_digit (int c)
496 {
497   assert (is_z_digit (c));
498   return (strchr (z_digits, c) - z_digits) >= 20;
499 }
500
501 /* Parses Z format. */
502 static char *
503 parse_Z (struct data_in *i)
504 {
505   struct string tmp;
506
507   int save_errno;
508
509   bool got_dot = false;
510   bool got_final_digit = false;
511
512   /* Trim spaces and check for missing value representation. */
513   if (trim_spaces_and_check_missing (i))
514     return NULL;
515
516   ds_init_empty (&tmp);
517   ds_extend (&tmp, 64);
518
519   ds_put_byte (&tmp, '+');
520   while (!ss_is_empty (i->input))
521     {
522       int c = ss_get_byte (&i->input);
523       if (c_isdigit (c) && !got_final_digit)
524         ds_put_byte (&tmp, c);
525       else if (is_z_digit (c) && !got_final_digit)
526         {
527           ds_put_byte (&tmp, z_digit_value (c) + '0');
528           if (is_negative_z_digit (c))
529             ds_data (&tmp)[0] = '-';
530           got_final_digit = true;
531         }
532       else if (c == '.' && !got_dot)
533         {
534           ds_put_byte (&tmp, '.');
535           got_dot = true;
536         }
537       else
538         {
539           ds_destroy (&tmp);
540           return xstrdup (_("Invalid zoned decimal syntax."));
541         }
542     }
543
544   if (!ss_is_empty (i->input))
545     {
546       char *error;
547
548       if (ds_length (&tmp) == 1)
549         error = xstrdup (_("Field contents are not numeric."));
550       else
551         error = xstrdup (_("Number followed by garbage."));
552
553       ds_destroy (&tmp);
554       return error;
555     }
556
557   /* Let c_strtod() do the conversion. */
558   save_errno = errno;
559   errno = 0;
560   i->output->f = c_strtod (ds_cstr (&tmp), NULL);
561   if (errno == ERANGE)
562     {
563       if (fabs (i->output->f) > 1)
564         {
565           i->output->f = SYSMIS;
566           ds_destroy (&tmp);
567           return xstrdup (_("Too-large number set to system-missing."));
568         }
569       else
570         {
571           i->output->f = 0.0;
572           ds_destroy (&tmp);
573           return xstrdup (_("Too-small number set to zero."));
574         }
575     }
576   else
577     errno = save_errno;
578
579   ds_destroy (&tmp);
580   return NULL;
581 }
582
583 /* Parses IB format. */
584 static char *
585 parse_IB (struct data_in *i)
586 {
587   size_t bytes;
588   uint64_t value;
589   uint64_t sign_bit;
590
591   bytes = MIN (8, ss_length (i->input));
592   value = integer_get (settings_get_input_integer_format (), ss_data (i->input), bytes);
593
594   sign_bit = UINT64_C(1) << (8 * bytes - 1);
595   if (!(value & sign_bit))
596     i->output->f = value;
597   else
598     {
599       /* Sign-extend to full 64 bits. */
600       value -= sign_bit << 1;
601       i->output->f = -(double) -value;
602     }
603
604   return NULL;
605 }
606
607 /* Parses PIB format. */
608 static char *
609 parse_PIB (struct data_in *i)
610 {
611   i->output->f = integer_get (settings_get_input_integer_format (), ss_data (i->input),
612                               MIN (8, ss_length (i->input)));
613
614   return NULL;
615 }
616
617 /* Consumes the first character of S.  Stores its high 4 bits in
618    HIGH_NIBBLE and its low 4 bits in LOW_NIBBLE. */
619 static void
620 get_nibbles (struct substring *s, int *high_nibble, int *low_nibble)
621 {
622   int c = ss_get_byte (s);
623   assert (c != EOF);
624   *high_nibble = (c >> 4) & 15;
625   *low_nibble = c & 15;
626 }
627
628 /* Parses P format. */
629 static char *
630 parse_P (struct data_in *i)
631 {
632   int high_nibble, low_nibble;
633
634   i->output->f = 0.0;
635
636   while (ss_length (i->input) > 1)
637     {
638       get_nibbles (&i->input, &high_nibble, &low_nibble);
639       if (high_nibble > 9 || low_nibble > 9)
640         return xstrdup (_("Invalid syntax for P field."));
641       i->output->f = (100 * i->output->f) + (10 * high_nibble) + low_nibble;
642     }
643
644   get_nibbles (&i->input, &high_nibble, &low_nibble);
645   if (high_nibble > 9)
646     return xstrdup (_("Invalid syntax for P field."));
647   i->output->f = (10 * i->output->f) + high_nibble;
648   if (low_nibble < 10)
649     i->output->f = (10 * i->output->f) + low_nibble;
650   else if (low_nibble == 0xb || low_nibble == 0xd)
651     i->output->f = -i->output->f;
652
653   return NULL;
654 }
655
656 /* Parses PK format. */
657 static char *
658 parse_PK (struct data_in *i)
659 {
660   i->output->f = 0.0;
661   while (!ss_is_empty (i->input))
662     {
663       int high_nibble, low_nibble;
664
665       get_nibbles (&i->input, &high_nibble, &low_nibble);
666       if (high_nibble > 9 || low_nibble > 9)
667         {
668           i->output->f = SYSMIS;
669           return NULL;
670         }
671       i->output->f = (100 * i->output->f) + (10 * high_nibble) + low_nibble;
672     }
673
674   return NULL;
675 }
676
677 /* Parses RB format. */
678 static char *
679 parse_RB (struct data_in *i)
680 {
681   enum float_format ff = settings_get_input_float_format ();
682   size_t size = float_get_size (ff);
683   if (ss_length (i->input) >= size)
684     float_convert (ff, ss_data (i->input),
685                    FLOAT_NATIVE_DOUBLE, &i->output->f);
686   else
687     i->output->f = SYSMIS;
688
689   return NULL;
690 }
691
692 /* Parses A format. */
693 static char *
694 parse_A (struct data_in *i)
695 {
696   /* This is equivalent to buf_copy_rpad, except that we posibly
697      do a character set recoding in the middle. */
698   uint8_t *dst = value_str_rw (i->output, i->width);
699   size_t dst_size = i->width;
700   const char *src = ss_data (i->input);
701   size_t src_size = ss_length (i->input);
702
703   memcpy (dst, src, MIN (src_size, dst_size));
704
705   if (dst_size > src_size)
706     memset (&dst[src_size], ' ', dst_size - src_size);
707
708   return NULL;
709 }
710
711 /* Parses AHEX format. */
712 static char *
713 parse_AHEX (struct data_in *i)
714 {
715   uint8_t *s = value_str_rw (i->output, i->width);
716   size_t j;
717
718   for (j = 0; ; j++)
719     {
720       int hi = ss_get_byte (&i->input);
721       int lo = ss_get_byte (&i->input);
722       if (hi == EOF)
723         break;
724       else if (lo == EOF)
725         return xstrdup (_("Field must have even length."));
726
727       if (!c_isxdigit (hi) || !c_isxdigit (lo))
728         return xstrdup (_("Field must contain only hex digits."));
729
730       if (j < i->width)
731         s[j] = hexit_value (hi) * 16 + hexit_value (lo);
732     }
733
734   memset (&s[j], ' ', i->width - j);
735
736   return NULL;
737 }
738 \f
739 /* Date & time format components. */
740
741 /* Sign of a time value. */
742 enum time_sign
743   {
744     SIGN_NO_TIME,       /* No time yet encountered. */
745     SIGN_POSITIVE,      /* Positive time. */
746     SIGN_NEGATIVE       /* Negative time. */
747   };
748
749 /* Parses a signed decimal integer from at most the first
750    MAX_DIGITS characters in I, storing the result into *RESULT.
751    Returns true if successful, false if no integer was
752    present. */
753 static char * WARN_UNUSED_RESULT
754 parse_int (struct data_in *i, long *result, size_t max_digits)
755 {
756   struct substring head = ss_head (i->input, max_digits);
757   size_t n = ss_get_long (&head, result);
758   if (n)
759     {
760       ss_advance (&i->input, n);
761       return NULL;
762     }
763   else
764     return xstrdup (_("Syntax error in date field."));
765 }
766
767 /* Parses a date integer between 1 and 31 from I, storing it into
768    *DAY.
769    Returns true if successful, false if no date was present. */
770 static char *
771 parse_day (struct data_in *i, long *day)
772 {
773   char *error = parse_int (i, day, SIZE_MAX);
774   if (error != NULL)
775     return error;
776   if (*day >= 1 && *day <= 31)
777     return NULL;
778
779   return xasprintf (_("Day (%ld) must be between 1 and 31."), *day);
780 }
781
782 /* Parses an integer from the beginning of I.
783    Adds SECONDS_PER_UNIT times the absolute value of the integer
784    to *TIME.
785    If *TIME_SIGN is SIGN_NO_TIME, allows a sign to precede the
786    time and sets *TIME_SIGN.  Otherwise, does not allow a sign.
787    Returns true if successful, false if no integer was present. */
788 static char *
789 parse_time_units (struct data_in *i, double seconds_per_unit,
790                   enum time_sign *time_sign, double *time)
791
792 {
793   char *error;
794   long units;
795
796   if (*time_sign == SIGN_NO_TIME)
797     {
798       if (ss_match_byte (&i->input, '-'))
799         *time_sign = SIGN_NEGATIVE;
800       else
801         {
802           ss_match_byte (&i->input, '+');
803           *time_sign = SIGN_POSITIVE;
804         }
805     }
806   error = parse_int (i, &units, SIZE_MAX);
807   if (error != NULL)
808     return error;
809   if (units < 0)
810     return xstrdup (_("Syntax error in date field."));
811   *time += units * seconds_per_unit;
812   return NULL;
813 }
814
815 /* Parses a data delimiter from the beginning of I.
816    Returns true if successful, false if no delimiter was
817    present. */
818 static char *
819 parse_date_delimiter (struct data_in *i)
820 {
821   if (ss_ltrim (&i->input, ss_cstr ("-/.," CC_SPACES)))
822     return NULL;
823
824   return xstrdup (_("Delimiter expected between fields in date."));
825 }
826
827 /* Parses spaces at the beginning of I. */
828 static void
829 parse_spaces (struct data_in *i)
830 {
831   ss_ltrim (&i->input, ss_cstr (CC_SPACES));
832 }
833
834 static struct substring
835 parse_name_token (struct data_in *i)
836 {
837   struct substring token;
838   ss_get_bytes (&i->input, ss_span (i->input, ss_cstr (CC_LETTERS)), &token);
839   return token;
840 }
841
842 /* Reads a name from I and sets *OUTPUT to the value associated
843    with that name.  If ALLOW_SUFFIXES is true, then names that
844    begin with one of the names are accepted; otherwise, only
845    exact matches (except for case) are allowed.
846    Returns true if successful, false otherwise. */
847 static bool
848 match_name (struct substring token, const char *const *names, long *output)
849 {
850   int i;
851
852   for (i = 1; *names != NULL; i++)
853     if (ss_equals_case (ss_cstr (*names++), token))
854       {
855         *output = i;
856         return true;
857       }
858
859   return false;
860 }
861
862 /* Parses a month name or number from the beginning of I,
863    storing the month (in range 1...12) into *MONTH.
864    Returns true if successful, false if no month was present. */
865 static char *
866 parse_month (struct data_in *i, long *month)
867 {
868   if (c_isdigit (ss_first (i->input)))
869     {
870       char *error = parse_int (i, month, SIZE_MAX);
871       if (error != NULL)
872         return error;
873       if (*month >= 1 && *month <= 12)
874         return NULL;
875     }
876   else
877     {
878       static const char *const english_names[] =
879         {
880           "jan", "feb", "mar", "apr", "may", "jun",
881           "jul", "aug", "sep", "oct", "nov", "dec",
882           NULL,
883         };
884
885       static const char *const roman_names[] =
886         {
887           "i", "ii", "iii", "iv", "v", "vi",
888           "vii", "viii", "ix", "x", "xi", "xii",
889           NULL,
890         };
891
892       struct substring token = parse_name_token (i);
893       if (match_name (ss_head (token, 3), english_names, month)
894           || match_name (ss_head (token, 4), roman_names, month))
895         return NULL;
896     }
897
898   return xstrdup (_("Unrecognized month format.  Months may be specified "
899                     "as Arabic or Roman numerals or as at least 3 letters "
900                     "of their English names."));
901 }
902
903 /* Parses a year of at most MAX_DIGITS from the beginning of I,
904    storing a "4-digit" year into *YEAR. */
905 static char *
906 parse_year (struct data_in *i, long *year, size_t max_digits)
907 {
908   char *error = parse_int (i, year, max_digits);
909   if (error != NULL)
910     return error;
911
912   if (*year >= 0 && *year <= 99)
913     {
914       int epoch = settings_get_epoch ();
915       int epoch_century = ROUND_DOWN (epoch, 100);
916       int epoch_offset = epoch - epoch_century;
917       if (*year >= epoch_offset)
918         *year += epoch_century;
919       else
920         *year += epoch_century + 100;
921     }
922   if (*year >= 1582 && *year <= 19999)
923     return NULL;
924
925   return xasprintf (_("Year (%ld) must be between 1582 and 19999."), *year);
926 }
927
928 /* Returns true if input in I has been exhausted,
929    false otherwise. */
930 static char *
931 parse_trailer (struct data_in *i)
932 {
933   if (ss_is_empty (i->input))
934     return NULL;
935
936   return xasprintf (_("Trailing garbage `%.*s' following date."),
937                     (int) ss_length (i->input), ss_data (i->input));
938 }
939
940 /* Parses a 3-digit Julian day-of-year value from I into *YDAY.
941    Returns true if successful, false on failure. */
942 static char *
943 parse_yday (struct data_in *i, long *yday)
944 {
945   struct substring num_s;
946   long num;
947
948   ss_get_bytes (&i->input, 3, &num_s);
949   if (ss_span (num_s, ss_cstr (CC_DIGITS)) != 3)
950     return xstrdup (_("Julian day must have exactly three digits."));
951   else if (!ss_get_long (&num_s, &num) || num < 1 || num > 366)
952     return xasprintf (_("Julian day (%ld) must be between 1 and 366."), num);
953
954   *yday = num;
955   return NULL;
956 }
957
958 /* Parses a quarter-of-year integer between 1 and 4 from I.
959    Stores the corresponding month into *MONTH.
960    Returns true if successful, false if no quarter was present. */
961 static char *
962 parse_quarter (struct data_in *i, long int *month)
963 {
964   long quarter;
965   char *error;
966
967   error = parse_int (i, &quarter, SIZE_MAX);
968   if (error != NULL)
969     return error;
970   if (quarter >= 1 && quarter <= 4)
971     {
972       *month = (quarter - 1) * 3 + 1;
973       return NULL;
974     }
975
976   return xasprintf (_("Quarter (%ld) must be between 1 and 4."), quarter);
977 }
978
979 /* Parses a week-of-year integer between 1 and 53 from I,
980    Stores the corresponding year-of-day into *YDAY.
981    Returns true if successful, false if no week was present. */
982 static char *
983 parse_week (struct data_in *i, long int *yday)
984 {
985   char *error;
986   long week;
987
988   error = parse_int (i, &week, SIZE_MAX);
989   if (error != NULL)
990     return error;
991   if (week >= 1 && week <= 53)
992     {
993       *yday = (week - 1) * 7 + 1;
994       return NULL;
995     }
996
997   return xasprintf (_("Week (%ld) must be between 1 and 53."), week);
998 }
999
1000 /* Parses a time delimiter from the beginning of I.
1001    Returns true if successful, false if no delimiter was
1002    present. */
1003 static char *
1004 parse_time_delimiter (struct data_in *i)
1005 {
1006   if (ss_ltrim (&i->input, ss_cstr (":" CC_SPACES)) > 0)
1007     return NULL;
1008
1009   return xstrdup (_("Delimiter expected between fields in time."));
1010 }
1011
1012 /* Parses minutes and optional seconds from the beginning of I.
1013    The time is converted into seconds, which are added to
1014    *TIME.
1015    Returns true if successful, false if an error was found. */
1016 static char *
1017 parse_minute_second (struct data_in *i, double *time)
1018 {
1019   long minute;
1020   char buf[64];
1021   char *error;
1022   char *cp;
1023
1024   /* Parse minutes. */
1025   error = parse_int (i, &minute, SIZE_MAX);
1026   if (error != NULL)
1027     return error;
1028   if (minute < 0 || minute > 59)
1029     return xasprintf (_("Minute (%ld) must be between 0 and 59."), minute);
1030   *time += 60. * minute;
1031
1032   /* Check for seconds. */
1033   if (ss_ltrim (&i->input, ss_cstr (":" CC_SPACES)) == 0
1034       || !c_isdigit (ss_first (i->input)))
1035    return NULL;
1036
1037   /* Parse seconds. */
1038   cp = buf;
1039   while (c_isdigit (ss_first (i->input)))
1040     *cp++ = ss_get_byte (&i->input);
1041   if (ss_match_byte (&i->input, settings_get_decimal_char (FMT_F)))
1042     *cp++ = '.';
1043   while (c_isdigit (ss_first (i->input)))
1044     *cp++ = ss_get_byte (&i->input);
1045   *cp = '\0';
1046
1047   *time += c_strtod (buf, NULL);
1048
1049   return NULL;
1050 }
1051
1052 /* Parses a weekday name from the beginning of I,
1053    storing a value of 1=Sunday...7=Saturday into *WEEKDAY.
1054    Returns true if successful, false if an error was found. */
1055 static char *
1056 parse_weekday (struct data_in *i, long *weekday)
1057 {
1058   static const char *const weekday_names[] =
1059     {
1060       "su", "mo", "tu", "we", "th", "fr", "sa",
1061       NULL,
1062     };
1063
1064   struct substring token = parse_name_token (i);
1065   bool ok = match_name (ss_head (token, 2), weekday_names, weekday);
1066   if (!ok)
1067     return xstrdup (_("Unrecognized weekday name.  At least the first two "
1068                       "letters of an English weekday name must be "
1069                       "specified."));
1070   return NULL;
1071 }
1072 \f
1073 /* Date & time formats. */
1074
1075 /* Parses WKDAY format. */
1076 static char *
1077 parse_WKDAY (struct data_in *i)
1078 {
1079   long weekday;
1080   char *error;
1081
1082   if (trim_spaces_and_check_missing (i))
1083     return NULL;
1084
1085   error = parse_weekday (i, &weekday);
1086   if (error == NULL)
1087     error = parse_trailer (i);
1088
1089   i->output->f = weekday;
1090   return error;
1091 }
1092
1093 /* Parses MONTH format. */
1094 static char *
1095 parse_MONTH (struct data_in *i)
1096 {
1097   long month;
1098   char *error;
1099
1100   if (trim_spaces_and_check_missing (i))
1101     return NULL;
1102
1103   error = parse_month (i, &month);
1104   if (error == NULL)
1105     error = parse_trailer (i);
1106
1107   i->output->f = month;
1108   return error;
1109 }
1110
1111 /* Parses DATE, ADATE, EDATE, JDATE, SDATE, QYR, MOYR, KWYR,
1112    DATETIME, TIME and DTIME formats. */
1113 static char *
1114 parse_date (struct data_in *i)
1115 {
1116   long int year = INT_MIN;
1117   long int month = 1;
1118   long int day = 1;
1119   long int yday = 1;
1120   double time = 0, date = 0;
1121   enum time_sign time_sign = SIGN_NO_TIME;
1122
1123   const char *template = fmt_date_template (i->format, 0);
1124   size_t template_width = strlen (template);
1125   char *error;
1126
1127   if (trim_spaces_and_check_missing (i))
1128     return NULL;
1129
1130   while (*template != '\0')
1131     {
1132       unsigned char ch = *template;
1133       int count = 1;
1134
1135       while (template[count] == ch)
1136         count++;
1137       template += count;
1138
1139       switch (ch)
1140         {
1141         case 'd':
1142           error = count < 3 ? parse_day (i, &day) : parse_yday (i, &yday);
1143           break;
1144         case 'm':
1145           error = parse_month (i, &month);
1146           break;
1147         case 'y':
1148           {
1149             size_t max_digits;
1150             if (!c_isalpha (*template))
1151               max_digits = SIZE_MAX;
1152             else
1153               {
1154                 if (ss_length (i->input) >= template_width + 2)
1155                   max_digits = 4;
1156                 else
1157                   max_digits = 2;
1158               }
1159             error = parse_year (i, &year, max_digits);
1160           }
1161           break;
1162         case 'q':
1163           error = parse_quarter (i, &month);
1164           break;
1165         case 'w':
1166           error = parse_week (i, &yday);
1167           break;
1168         case 'D':
1169           error = parse_time_units (i, 60. * 60. * 24., &time_sign, &time);
1170           break;
1171         case 'H':
1172           error = parse_time_units (i, 60. * 60., &time_sign, &time);
1173           break;
1174         case 'M':
1175           error = parse_minute_second (i, &time);
1176           break;
1177         case '-':
1178         case '/':
1179         case '.':
1180           error = parse_date_delimiter (i);
1181           break;
1182         case ':':
1183           error = parse_time_delimiter (i);
1184         case ' ':
1185           if (i->format != FMT_MOYR)
1186             {
1187               parse_spaces (i);
1188               error = NULL;
1189             }
1190           else
1191             error = parse_date_delimiter (i);
1192           break;
1193         default:
1194           assert (count == 1);
1195           if (!ss_match_byte (&i->input, c_toupper (ch))
1196               && !ss_match_byte (&i->input, c_tolower (ch)))
1197             error = xasprintf (_("`%c' expected in date field."), ch);
1198           else
1199             error = NULL;
1200           break;
1201         }
1202       if (error != NULL)
1203         return error;
1204     }
1205   error = parse_trailer (i);
1206   if (error != NULL)
1207     return error;
1208
1209   if (year != INT_MIN)
1210     {
1211       char *error;
1212       double ofs;
1213
1214       ofs = calendar_gregorian_to_offset (year, month, day, &error);
1215       if (ofs == SYSMIS)
1216         return error;
1217       date = (yday - 1 + ofs) * 60. * 60. * 24.;
1218     }
1219   else
1220     date = 0.;
1221   i->output->f = date + (time_sign == SIGN_NEGATIVE ? -time : time);
1222
1223   return NULL;
1224 }
1225 \f
1226 /* Utility functions. */
1227
1228 /* Sets the default result for I.
1229    For a numeric format, this is the value set on SET BLANKS
1230    (typically system-missing); for a string format, it is all
1231    spaces. */
1232 static void
1233 default_result (struct data_in *i)
1234 {
1235   if (fmt_is_string (i->format))
1236     memset (value_str_rw (i->output, i->width), ' ', i->width);
1237   else
1238     i->output->f = settings_get_blanks ();
1239 }
1240
1241 /* Trims leading and trailing spaces from I.
1242    If the result is empty, or a single period character, then
1243    sets the default result and returns true; otherwise, returns
1244    false. */
1245 static bool
1246 trim_spaces_and_check_missing (struct data_in *i)
1247 {
1248   ss_trim (&i->input, ss_cstr (" "));
1249   if (ss_is_empty (i->input) || ss_equals (i->input, ss_cstr (".")))
1250     {
1251       default_result (i);
1252       return true;
1253     }
1254   return false;
1255 }
1256
1257 /* Returns the integer value of hex digit C. */
1258 static int
1259 hexit_value (int c)
1260 {
1261   const char s[] = "0123456789abcdef";
1262   const char *cp = strchr (s, c_tolower ((unsigned char) c));
1263
1264   assert (cp != NULL);
1265   return cp - s;
1266 }