Fix build on sparc64-linux-gnu.
[official-gcc.git] / libgfortran / io / read.c
blobf972858c146d0e60f7b7e6b5875686358e725201
1 /* Copyright (C) 2002-2018 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
3 F2003 I/O support contributed by Jerry DeLisle
5 This file is part of the GNU Fortran runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #include "io.h"
27 #include "fbuf.h"
28 #include "format.h"
29 #include "unix.h"
30 #include <string.h>
31 #include <ctype.h>
32 #include <assert.h>
33 #include "async.h"
35 typedef unsigned char uchar;
37 /* read.c -- Deal with formatted reads */
40 /* set_integer()-- All of the integer assignments come here to
41 actually place the value into memory. */
43 void
44 set_integer (void *dest, GFC_INTEGER_LARGEST value, int length)
46 NOTE ("set_integer: %lld %p", (long long int) value, dest);
47 switch (length)
49 #ifdef HAVE_GFC_INTEGER_16
50 /* length=10 comes about for kind=10 real/complex BOZ, cf. PR41711. */
51 case 10:
52 case 16:
54 GFC_INTEGER_16 tmp = value;
55 memcpy (dest, (void *) &tmp, length);
57 break;
58 #endif
59 case 8:
61 GFC_INTEGER_8 tmp = value;
62 memcpy (dest, (void *) &tmp, length);
64 break;
65 case 4:
67 GFC_INTEGER_4 tmp = value;
68 memcpy (dest, (void *) &tmp, length);
70 break;
71 case 2:
73 GFC_INTEGER_2 tmp = value;
74 memcpy (dest, (void *) &tmp, length);
76 break;
77 case 1:
79 GFC_INTEGER_1 tmp = value;
80 memcpy (dest, (void *) &tmp, length);
82 break;
83 default:
84 internal_error (NULL, "Bad integer kind");
89 /* Max signed value of size give by length argument. */
91 GFC_UINTEGER_LARGEST
92 si_max (int length)
94 #if defined HAVE_GFC_REAL_16 || defined HAVE_GFC_REAL_10
95 GFC_UINTEGER_LARGEST value;
96 #endif
98 switch (length)
100 #if defined HAVE_GFC_REAL_16 || defined HAVE_GFC_REAL_10
101 case 16:
102 case 10:
103 value = 1;
104 for (int n = 1; n < 4 * length; n++)
105 value = (value << 2) + 3;
106 return value;
107 #endif
108 case 8:
109 return GFC_INTEGER_8_HUGE;
110 case 4:
111 return GFC_INTEGER_4_HUGE;
112 case 2:
113 return GFC_INTEGER_2_HUGE;
114 case 1:
115 return GFC_INTEGER_1_HUGE;
116 default:
117 internal_error (NULL, "Bad integer kind");
122 /* convert_real()-- Convert a character representation of a floating
123 point number to the machine number. Returns nonzero if there is an
124 invalid input. Note: many architectures (e.g. IA-64, HP-PA)
125 require that the storage pointed to by the dest argument is
126 properly aligned for the type in question. */
129 convert_real (st_parameter_dt *dtp, void *dest, const char *buffer, int length)
131 char *endptr = NULL;
132 int round_mode, old_round_mode;
134 switch (dtp->u.p.current_unit->round_status)
136 case ROUND_COMPATIBLE:
137 /* FIXME: As NEAREST but round away from zero for a tie. */
138 case ROUND_UNSPECIFIED:
139 /* Should not occur. */
140 case ROUND_PROCDEFINED:
141 round_mode = ROUND_NEAREST;
142 break;
143 default:
144 round_mode = dtp->u.p.current_unit->round_status;
145 break;
148 old_round_mode = get_fpu_rounding_mode();
149 set_fpu_rounding_mode (round_mode);
151 switch (length)
153 case 4:
154 *((GFC_REAL_4*) dest) =
155 #if defined(HAVE_STRTOF)
156 gfc_strtof (buffer, &endptr);
157 #else
158 (GFC_REAL_4) gfc_strtod (buffer, &endptr);
159 #endif
160 break;
162 case 8:
163 *((GFC_REAL_8*) dest) = gfc_strtod (buffer, &endptr);
164 break;
166 #if defined(HAVE_GFC_REAL_10) && defined (HAVE_STRTOLD)
167 case 10:
168 *((GFC_REAL_10*) dest) = gfc_strtold (buffer, &endptr);
169 break;
170 #endif
172 #if defined(HAVE_GFC_REAL_16)
173 # if defined(GFC_REAL_16_IS_FLOAT128)
174 case 16:
175 *((GFC_REAL_16*) dest) = __qmath_(strtoflt128) (buffer, &endptr);
176 break;
177 # elif defined(HAVE_STRTOLD)
178 case 16:
179 *((GFC_REAL_16*) dest) = gfc_strtold (buffer, &endptr);
180 break;
181 # endif
182 #endif
184 default:
185 internal_error (&dtp->common, "Unsupported real kind during IO");
188 set_fpu_rounding_mode (old_round_mode);
190 if (buffer == endptr)
192 generate_error (&dtp->common, LIBERROR_READ_VALUE,
193 "Error during floating point read");
194 next_record (dtp, 1);
195 return 1;
198 return 0;
201 /* convert_infnan()-- Convert character INF/NAN representation to the
202 machine number. Note: many architectures (e.g. IA-64, HP-PA) require
203 that the storage pointed to by the dest argument is properly aligned
204 for the type in question. */
207 convert_infnan (st_parameter_dt *dtp, void *dest, const char *buffer,
208 int length)
210 const char *s = buffer;
211 int is_inf, plus = 1;
213 if (*s == '+')
214 s++;
215 else if (*s == '-')
217 s++;
218 plus = 0;
221 is_inf = *s == 'i';
223 switch (length)
225 case 4:
226 if (is_inf)
227 *((GFC_REAL_4*) dest) = plus ? __builtin_inff () : -__builtin_inff ();
228 else
229 *((GFC_REAL_4*) dest) = plus ? __builtin_nanf ("") : -__builtin_nanf ("");
230 break;
232 case 8:
233 if (is_inf)
234 *((GFC_REAL_8*) dest) = plus ? __builtin_inf () : -__builtin_inf ();
235 else
236 *((GFC_REAL_8*) dest) = plus ? __builtin_nan ("") : -__builtin_nan ("");
237 break;
239 #if defined(HAVE_GFC_REAL_10)
240 case 10:
241 if (is_inf)
242 *((GFC_REAL_10*) dest) = plus ? __builtin_infl () : -__builtin_infl ();
243 else
244 *((GFC_REAL_10*) dest) = plus ? __builtin_nanl ("") : -__builtin_nanl ("");
245 break;
246 #endif
248 #if defined(HAVE_GFC_REAL_16)
249 # if defined(GFC_REAL_16_IS_FLOAT128)
250 case 16:
251 *((GFC_REAL_16*) dest) = __qmath_(strtoflt128) (buffer, NULL);
252 break;
253 # else
254 case 16:
255 if (is_inf)
256 *((GFC_REAL_16*) dest) = plus ? __builtin_infl () : -__builtin_infl ();
257 else
258 *((GFC_REAL_16*) dest) = plus ? __builtin_nanl ("") : -__builtin_nanl ("");
259 break;
260 # endif
261 #endif
263 default:
264 internal_error (&dtp->common, "Unsupported real kind during IO");
267 return 0;
271 /* read_l()-- Read a logical value */
273 void
274 read_l (st_parameter_dt *dtp, const fnode *f, char *dest, int length)
276 char *p;
277 size_t w;
279 w = f->u.w;
281 p = read_block_form (dtp, &w);
283 if (p == NULL)
284 return;
286 while (*p == ' ')
288 if (--w == 0)
289 goto bad;
290 p++;
293 if (*p == '.')
295 if (--w == 0)
296 goto bad;
297 p++;
300 switch (*p)
302 case 't':
303 case 'T':
304 set_integer (dest, (GFC_INTEGER_LARGEST) 1, length);
305 break;
306 case 'f':
307 case 'F':
308 set_integer (dest, (GFC_INTEGER_LARGEST) 0, length);
309 break;
310 default:
311 bad:
312 generate_error (&dtp->common, LIBERROR_READ_VALUE,
313 "Bad value on logical read");
314 next_record (dtp, 1);
315 break;
320 static gfc_char4_t
321 read_utf8 (st_parameter_dt *dtp, size_t *nbytes)
323 static const uchar masks[6] = { 0x7F, 0x1F, 0x0F, 0x07, 0x02, 0x01 };
324 static const uchar patns[6] = { 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
325 size_t nb, nread;
326 gfc_char4_t c;
327 char *s;
329 *nbytes = 1;
331 s = read_block_form (dtp, nbytes);
332 if (s == NULL)
333 return 0;
335 /* If this is a short read, just return. */
336 if (*nbytes == 0)
337 return 0;
339 c = (uchar) s[0];
340 if (c < 0x80)
341 return c;
343 /* The number of leading 1-bits in the first byte indicates how many
344 bytes follow. */
345 for (nb = 2; nb < 7; nb++)
346 if ((c & ~masks[nb-1]) == patns[nb-1])
347 goto found;
348 goto invalid;
350 found:
351 c = (c & masks[nb-1]);
352 nread = nb - 1;
354 s = read_block_form (dtp, &nread);
355 if (s == NULL)
356 return 0;
357 /* Decode the bytes read. */
358 for (size_t i = 1; i < nb; i++)
360 gfc_char4_t n = *s++;
362 if ((n & 0xC0) != 0x80)
363 goto invalid;
365 c = ((c << 6) + (n & 0x3F));
368 /* Make sure the shortest possible encoding was used. */
369 if (c <= 0x7F && nb > 1) goto invalid;
370 if (c <= 0x7FF && nb > 2) goto invalid;
371 if (c <= 0xFFFF && nb > 3) goto invalid;
372 if (c <= 0x1FFFFF && nb > 4) goto invalid;
373 if (c <= 0x3FFFFFF && nb > 5) goto invalid;
375 /* Make sure the character is valid. */
376 if (c > 0x7FFFFFFF || (c >= 0xD800 && c <= 0xDFFF))
377 goto invalid;
379 return c;
381 invalid:
382 generate_error (&dtp->common, LIBERROR_READ_VALUE, "Invalid UTF-8 encoding");
383 return (gfc_char4_t) '?';
387 static void
388 read_utf8_char1 (st_parameter_dt *dtp, char *p, size_t len, size_t width)
390 gfc_char4_t c;
391 char *dest;
392 size_t nbytes, j;
394 len = (width < len) ? len : width;
396 dest = (char *) p;
398 /* Proceed with decoding one character at a time. */
399 for (j = 0; j < len; j++, dest++)
401 c = read_utf8 (dtp, &nbytes);
403 /* Check for a short read and if so, break out. */
404 if (nbytes == 0)
405 break;
407 *dest = c > 255 ? '?' : (uchar) c;
410 /* If there was a short read, pad the remaining characters. */
411 for (size_t i = j; i < len; i++)
412 *dest++ = ' ';
413 return;
416 static void
417 read_default_char1 (st_parameter_dt *dtp, char *p, size_t len, size_t width)
419 char *s;
420 size_t m;
422 s = read_block_form (dtp, &width);
424 if (s == NULL)
425 return;
426 if (width > len)
427 s += (width - len);
429 m = (width > len) ? len : width;
430 memcpy (p, s, m);
432 if (len > width)
433 memset (p + m, ' ', len - width);
437 static void
438 read_utf8_char4 (st_parameter_dt *dtp, void *p, size_t len, size_t width)
440 gfc_char4_t *dest;
441 size_t nbytes, j;
443 len = (width < len) ? len : width;
445 dest = (gfc_char4_t *) p;
447 /* Proceed with decoding one character at a time. */
448 for (j = 0; j < len; j++, dest++)
450 *dest = read_utf8 (dtp, &nbytes);
452 /* Check for a short read and if so, break out. */
453 if (nbytes == 0)
454 break;
457 /* If there was a short read, pad the remaining characters. */
458 for (size_t i = j; i < len; i++)
459 *dest++ = (gfc_char4_t) ' ';
460 return;
464 static void
465 read_default_char4 (st_parameter_dt *dtp, char *p, size_t len, size_t width)
467 size_t m, n;
468 gfc_char4_t *dest;
470 if (is_char4_unit(dtp))
472 gfc_char4_t *s4;
474 s4 = (gfc_char4_t *) read_block_form4 (dtp, &width);
476 if (s4 == NULL)
477 return;
478 if (width > len)
479 s4 += (width - len);
481 m = (width > len) ? len : width;
483 dest = (gfc_char4_t *) p;
485 for (n = 0; n < m; n++)
486 *dest++ = *s4++;
488 if (len > width)
490 for (n = 0; n < len - width; n++)
491 *dest++ = (gfc_char4_t) ' ';
494 else
496 char *s;
498 s = read_block_form (dtp, &width);
500 if (s == NULL)
501 return;
502 if (width > len)
503 s += (width - len);
505 m = (width > len) ? len : width;
507 dest = (gfc_char4_t *) p;
509 for (n = 0; n < m; n++, dest++, s++)
510 *dest = (unsigned char ) *s;
512 if (len > width)
514 for (n = 0; n < len - width; n++, dest++)
515 *dest = (unsigned char) ' ';
521 /* read_a()-- Read a character record into a KIND=1 character destination,
522 processing UTF-8 encoding if necessary. */
524 void
525 read_a (st_parameter_dt *dtp, const fnode *f, char *p, size_t length)
527 size_t w;
529 if (f->u.w == -1) /* '(A)' edit descriptor */
530 w = length;
531 else
532 w = f->u.w;
534 /* Read in w characters, treating comma as not a separator. */
535 dtp->u.p.sf_read_comma = 0;
537 if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
538 read_utf8_char1 (dtp, p, length, w);
539 else
540 read_default_char1 (dtp, p, length, w);
542 dtp->u.p.sf_read_comma =
543 dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA ? 0 : 1;
547 /* read_a_char4()-- Read a character record into a KIND=4 character destination,
548 processing UTF-8 encoding if necessary. */
550 void
551 read_a_char4 (st_parameter_dt *dtp, const fnode *f, char *p, size_t length)
553 size_t w;
555 if (f->u.w == -1) /* '(A)' edit descriptor */
556 w = length;
557 else
558 w = f->u.w;
560 /* Read in w characters, treating comma as not a separator. */
561 dtp->u.p.sf_read_comma = 0;
563 if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
564 read_utf8_char4 (dtp, p, length, w);
565 else
566 read_default_char4 (dtp, p, length, w);
568 dtp->u.p.sf_read_comma =
569 dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA ? 0 : 1;
572 /* eat_leading_spaces()-- Given a character pointer and a width,
573 ignore the leading spaces. */
575 static char *
576 eat_leading_spaces (size_t *width, char *p)
578 for (;;)
580 if (*width == 0 || *p != ' ')
581 break;
583 (*width)--;
584 p++;
587 return p;
591 static char
592 next_char (st_parameter_dt *dtp, char **p, size_t *w)
594 char c, *q;
596 if (*w == 0)
597 return '\0';
599 q = *p;
600 c = *q++;
601 *p = q;
603 (*w)--;
605 if (c != ' ')
606 return c;
607 if (dtp->u.p.blank_status != BLANK_UNSPECIFIED)
608 return ' '; /* return a blank to signal a null */
610 /* At this point, the rest of the field has to be trailing blanks */
612 while (*w > 0)
614 if (*q++ != ' ')
615 return '?';
616 (*w)--;
619 *p = q;
620 return '\0';
624 /* read_decimal()-- Read a decimal integer value. The values here are
625 signed values. */
627 void
628 read_decimal (st_parameter_dt *dtp, const fnode *f, char *dest, int length)
630 GFC_UINTEGER_LARGEST value, maxv, maxv_10;
631 GFC_INTEGER_LARGEST v;
632 size_t w;
633 int negative;
634 char c, *p;
636 w = f->u.w;
638 p = read_block_form (dtp, &w);
640 if (p == NULL)
641 return;
643 p = eat_leading_spaces (&w, p);
644 if (w == 0)
646 set_integer (dest, (GFC_INTEGER_LARGEST) 0, length);
647 return;
650 negative = 0;
652 switch (*p)
654 case '-':
655 negative = 1;
656 /* Fall through */
658 case '+':
659 p++;
660 if (--w == 0)
661 goto bad;
662 /* Fall through */
664 default:
665 break;
668 maxv = si_max (length);
669 if (negative)
670 maxv++;
671 maxv_10 = maxv / 10;
673 /* At this point we have a digit-string */
674 value = 0;
676 for (;;)
678 c = next_char (dtp, &p, &w);
679 if (c == '\0')
680 break;
682 if (c == ' ')
684 if (dtp->u.p.blank_status == BLANK_NULL)
686 /* Skip spaces. */
687 for ( ; w > 0; p++, w--)
688 if (*p != ' ') break;
689 continue;
691 if (dtp->u.p.blank_status == BLANK_ZERO) c = '0';
694 if (c < '0' || c > '9')
695 goto bad;
697 if (value > maxv_10)
698 goto overflow;
700 c -= '0';
701 value = 10 * value;
703 if (value > maxv - c)
704 goto overflow;
705 value += c;
708 if (negative)
709 v = -value;
710 else
711 v = value;
713 set_integer (dest, v, length);
714 return;
716 bad:
717 generate_error (&dtp->common, LIBERROR_READ_VALUE,
718 "Bad value during integer read");
719 next_record (dtp, 1);
720 return;
722 overflow:
723 generate_error (&dtp->common, LIBERROR_READ_OVERFLOW,
724 "Value overflowed during integer read");
725 next_record (dtp, 1);
730 /* read_radix()-- This function reads values for non-decimal radixes.
731 The difference here is that we treat the values here as unsigned
732 values for the purposes of overflow. If minus sign is present and
733 the top bit is set, the value will be incorrect. */
735 void
736 read_radix (st_parameter_dt *dtp, const fnode *f, char *dest, int length,
737 int radix)
739 GFC_UINTEGER_LARGEST value, maxv, maxv_r;
740 GFC_INTEGER_LARGEST v;
741 size_t w;
742 int negative;
743 char c, *p;
745 w = f->u.w;
747 p = read_block_form (dtp, &w);
749 if (p == NULL)
750 return;
752 p = eat_leading_spaces (&w, p);
753 if (w == 0)
755 set_integer (dest, (GFC_INTEGER_LARGEST) 0, length);
756 return;
759 /* Maximum unsigned value, assuming two's complement. */
760 maxv = 2 * si_max (length) + 1;
761 maxv_r = maxv / radix;
763 negative = 0;
764 value = 0;
766 switch (*p)
768 case '-':
769 negative = 1;
770 /* Fall through */
772 case '+':
773 p++;
774 if (--w == 0)
775 goto bad;
776 /* Fall through */
778 default:
779 break;
782 /* At this point we have a digit-string */
783 value = 0;
785 for (;;)
787 c = next_char (dtp, &p, &w);
788 if (c == '\0')
789 break;
790 if (c == ' ')
792 if (dtp->u.p.blank_status == BLANK_NULL) continue;
793 if (dtp->u.p.blank_status == BLANK_ZERO) c = '0';
796 switch (radix)
798 case 2:
799 if (c < '0' || c > '1')
800 goto bad;
801 break;
803 case 8:
804 if (c < '0' || c > '7')
805 goto bad;
806 break;
808 case 16:
809 switch (c)
811 case '0':
812 case '1':
813 case '2':
814 case '3':
815 case '4':
816 case '5':
817 case '6':
818 case '7':
819 case '8':
820 case '9':
821 break;
823 case 'a':
824 case 'b':
825 case 'c':
826 case 'd':
827 case 'e':
828 case 'f':
829 c = c - 'a' + '9' + 1;
830 break;
832 case 'A':
833 case 'B':
834 case 'C':
835 case 'D':
836 case 'E':
837 case 'F':
838 c = c - 'A' + '9' + 1;
839 break;
841 default:
842 goto bad;
845 break;
848 if (value > maxv_r)
849 goto overflow;
851 c -= '0';
852 value = radix * value;
854 if (maxv - c < value)
855 goto overflow;
856 value += c;
859 v = value;
860 if (negative)
861 v = -v;
863 set_integer (dest, v, length);
864 return;
866 bad:
867 generate_error (&dtp->common, LIBERROR_READ_VALUE,
868 "Bad value during integer read");
869 next_record (dtp, 1);
870 return;
872 overflow:
873 generate_error (&dtp->common, LIBERROR_READ_OVERFLOW,
874 "Value overflowed during integer read");
875 next_record (dtp, 1);
880 /* read_f()-- Read a floating point number with F-style editing, which
881 is what all of the other floating point descriptors behave as. The
882 tricky part is that optional spaces are allowed after an E or D,
883 and the implicit decimal point if a decimal point is not present in
884 the input. */
886 void
887 read_f (st_parameter_dt *dtp, const fnode *f, char *dest, int length)
889 #define READF_TMP 50
890 char tmp[READF_TMP];
891 size_t buf_size = 0;
892 size_t w;
893 int seen_dp, exponent;
894 int exponent_sign;
895 const char *p;
896 char *buffer;
897 char *out;
898 int seen_int_digit; /* Seen a digit before the decimal point? */
899 int seen_dec_digit; /* Seen a digit after the decimal point? */
901 seen_dp = 0;
902 seen_int_digit = 0;
903 seen_dec_digit = 0;
904 exponent_sign = 1;
905 exponent = 0;
906 w = f->u.w;
907 buffer = tmp;
909 /* Read in the next block. */
910 p = read_block_form (dtp, &w);
911 if (p == NULL)
912 return;
913 p = eat_leading_spaces (&w, (char*) p);
914 if (w == 0)
915 goto zero;
917 /* In this buffer we're going to re-format the number cleanly to be parsed
918 by convert_real in the end; this assures we're using strtod from the
919 C library for parsing and thus probably get the best accuracy possible.
920 This process may add a '+0.0' in front of the number as well as change the
921 exponent because of an implicit decimal point or the like. Thus allocating
922 strlen ("+0.0e-1000") == 10 characters plus one for NUL more than the
923 original buffer had should be enough. */
924 buf_size = w + 11;
925 if (buf_size > READF_TMP)
926 buffer = xmalloc (buf_size);
928 out = buffer;
930 /* Optional sign */
931 if (*p == '-' || *p == '+')
933 if (*p == '-')
934 *(out++) = '-';
935 ++p;
936 --w;
939 p = eat_leading_spaces (&w, (char*) p);
940 if (w == 0)
941 goto zero;
943 /* Check for Infinity or NaN. */
944 if (unlikely ((w >= 3 && (*p == 'i' || *p == 'I' || *p == 'n' || *p == 'N'))))
946 int seen_paren = 0;
947 char *save = out;
949 /* Scan through the buffer keeping track of spaces and parenthesis. We
950 null terminate the string as soon as we see a left paren or if we are
951 BLANK_NULL mode. Leading spaces have already been skipped above,
952 trailing spaces are ignored by converting to '\0'. A space
953 between "NaN" and the optional perenthesis is not permitted. */
954 while (w > 0)
956 *out = tolower (*p);
957 switch (*p)
959 case ' ':
960 if (dtp->u.p.blank_status == BLANK_ZERO)
962 *out = '0';
963 break;
965 *out = '\0';
966 if (seen_paren == 1)
967 goto bad_float;
968 break;
969 case '(':
970 seen_paren++;
971 *out = '\0';
972 break;
973 case ')':
974 if (seen_paren++ != 1)
975 goto bad_float;
976 break;
977 default:
978 if (!isalnum (*out))
979 goto bad_float;
981 --w;
982 ++p;
983 ++out;
986 *out = '\0';
988 if (seen_paren != 0 && seen_paren != 2)
989 goto bad_float;
991 if ((strcmp (save, "inf") == 0) || (strcmp (save, "infinity") == 0))
993 if (seen_paren)
994 goto bad_float;
996 else if (strcmp (save, "nan") != 0)
997 goto bad_float;
999 convert_infnan (dtp, dest, buffer, length);
1000 if (buf_size > READF_TMP)
1001 free (buffer);
1002 return;
1005 /* Process the mantissa string. */
1006 while (w > 0)
1008 switch (*p)
1010 case ',':
1011 if (dtp->u.p.current_unit->decimal_status != DECIMAL_COMMA)
1012 goto bad_float;
1013 /* Fall through. */
1014 case '.':
1015 if (seen_dp)
1016 goto bad_float;
1017 if (!seen_int_digit)
1018 *(out++) = '0';
1019 *(out++) = '.';
1020 seen_dp = 1;
1021 break;
1023 case ' ':
1024 if (dtp->u.p.blank_status == BLANK_ZERO)
1026 *(out++) = '0';
1027 goto found_digit;
1029 else if (dtp->u.p.blank_status == BLANK_NULL)
1030 break;
1031 else
1032 /* TODO: Should we check instead that there are only trailing
1033 blanks here, as is done below for exponents? */
1034 goto done;
1035 /* Fall through. */
1036 case '0':
1037 case '1':
1038 case '2':
1039 case '3':
1040 case '4':
1041 case '5':
1042 case '6':
1043 case '7':
1044 case '8':
1045 case '9':
1046 *(out++) = *p;
1047 found_digit:
1048 if (!seen_dp)
1049 seen_int_digit = 1;
1050 else
1051 seen_dec_digit = 1;
1052 break;
1054 case '-':
1055 case '+':
1056 goto exponent;
1058 case 'e':
1059 case 'E':
1060 case 'd':
1061 case 'D':
1062 case 'q':
1063 case 'Q':
1064 ++p;
1065 --w;
1066 goto exponent;
1068 default:
1069 goto bad_float;
1072 ++p;
1073 --w;
1076 /* No exponent has been seen, so we use the current scale factor. */
1077 exponent = - dtp->u.p.scale_factor;
1078 goto done;
1080 /* At this point the start of an exponent has been found. */
1081 exponent:
1082 p = eat_leading_spaces (&w, (char*) p);
1083 if (*p == '-' || *p == '+')
1085 if (*p == '-')
1086 exponent_sign = -1;
1087 ++p;
1088 --w;
1091 /* At this point a digit string is required. We calculate the value
1092 of the exponent in order to take account of the scale factor and
1093 the d parameter before explict conversion takes place. */
1095 if (w == 0)
1097 /* Extension: allow default exponent of 0 when omitted. */
1098 if (dtp->common.flags & IOPARM_DT_DEC_EXT)
1099 goto done;
1100 else
1101 goto bad_float;
1104 if (dtp->u.p.blank_status == BLANK_UNSPECIFIED)
1106 while (w > 0 && isdigit (*p))
1108 exponent *= 10;
1109 exponent += *p - '0';
1110 ++p;
1111 --w;
1114 /* Only allow trailing blanks. */
1115 while (w > 0)
1117 if (*p != ' ')
1118 goto bad_float;
1119 ++p;
1120 --w;
1123 else /* BZ or BN status is enabled. */
1125 while (w > 0)
1127 if (*p == ' ')
1129 if (dtp->u.p.blank_status == BLANK_ZERO)
1130 exponent *= 10;
1131 else
1132 assert (dtp->u.p.blank_status == BLANK_NULL);
1134 else if (!isdigit (*p))
1135 goto bad_float;
1136 else
1138 exponent *= 10;
1139 exponent += *p - '0';
1142 ++p;
1143 --w;
1147 exponent *= exponent_sign;
1149 done:
1150 /* Use the precision specified in the format if no decimal point has been
1151 seen. */
1152 if (!seen_dp)
1153 exponent -= f->u.real.d;
1155 /* Output a trailing '0' after decimal point if not yet found. */
1156 if (seen_dp && !seen_dec_digit)
1157 *(out++) = '0';
1158 /* Handle input of style "E+NN" by inserting a 0 for the
1159 significand. */
1160 else if (!seen_int_digit && !seen_dec_digit)
1162 notify_std (&dtp->common, GFC_STD_LEGACY,
1163 "REAL input of style 'E+NN'");
1164 *(out++) = '0';
1167 /* Print out the exponent to finish the reformatted number. Maximum 4
1168 digits for the exponent. */
1169 if (exponent != 0)
1171 int dig;
1173 *(out++) = 'e';
1174 if (exponent < 0)
1176 *(out++) = '-';
1177 exponent = - exponent;
1180 if (exponent >= 10000)
1181 goto bad_float;
1183 for (dig = 3; dig >= 0; --dig)
1185 out[dig] = (char) ('0' + exponent % 10);
1186 exponent /= 10;
1188 out += 4;
1190 *(out++) = '\0';
1192 /* Do the actual conversion. */
1193 convert_real (dtp, dest, buffer, length);
1194 if (buf_size > READF_TMP)
1195 free (buffer);
1196 return;
1198 /* The value read is zero. */
1199 zero:
1200 switch (length)
1202 case 4:
1203 *((GFC_REAL_4 *) dest) = 0.0;
1204 break;
1206 case 8:
1207 *((GFC_REAL_8 *) dest) = 0.0;
1208 break;
1210 #ifdef HAVE_GFC_REAL_10
1211 case 10:
1212 *((GFC_REAL_10 *) dest) = 0.0;
1213 break;
1214 #endif
1216 #ifdef HAVE_GFC_REAL_16
1217 case 16:
1218 *((GFC_REAL_16 *) dest) = 0.0;
1219 break;
1220 #endif
1222 default:
1223 internal_error (&dtp->common, "Unsupported real kind during IO");
1225 return;
1227 bad_float:
1228 if (buf_size > READF_TMP)
1229 free (buffer);
1230 generate_error (&dtp->common, LIBERROR_READ_VALUE,
1231 "Bad value during floating point read");
1232 next_record (dtp, 1);
1233 return;
1237 /* read_x()-- Deal with the X/TR descriptor. We just read some data
1238 and never look at it. */
1240 void
1241 read_x (st_parameter_dt *dtp, size_t n)
1243 size_t length;
1244 int q, q2;
1246 if ((dtp->u.p.current_unit->pad_status == PAD_NO || is_internal_unit (dtp))
1247 && dtp->u.p.current_unit->bytes_left < (gfc_offset) n)
1248 n = dtp->u.p.current_unit->bytes_left;
1250 if (n == 0)
1251 return;
1253 length = n;
1255 if (is_internal_unit (dtp))
1257 mem_alloc_r (dtp->u.p.current_unit->s, &length);
1258 if (unlikely (length < n))
1259 n = length;
1260 goto done;
1263 if (dtp->u.p.sf_seen_eor)
1264 return;
1266 n = 0;
1267 while (n < length)
1269 q = fbuf_getc (dtp->u.p.current_unit);
1270 if (q == EOF)
1271 break;
1272 else if (dtp->u.p.current_unit->flags.cc != CC_NONE
1273 && (q == '\n' || q == '\r'))
1275 /* Unexpected end of line. Set the position. */
1276 dtp->u.p.sf_seen_eor = 1;
1278 /* If we see an EOR during non-advancing I/O, we need to skip
1279 the rest of the I/O statement. Set the corresponding flag. */
1280 if (dtp->u.p.advance_status == ADVANCE_NO || dtp->u.p.seen_dollar)
1281 dtp->u.p.eor_condition = 1;
1283 /* If we encounter a CR, it might be a CRLF. */
1284 if (q == '\r') /* Probably a CRLF */
1286 /* See if there is an LF. */
1287 q2 = fbuf_getc (dtp->u.p.current_unit);
1288 if (q2 == '\n')
1289 dtp->u.p.sf_seen_eor = 2;
1290 else if (q2 != EOF) /* Oops, seek back. */
1291 fbuf_seek (dtp->u.p.current_unit, -1, SEEK_CUR);
1293 goto done;
1295 n++;
1298 done:
1299 if (((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0) ||
1300 dtp->u.p.current_unit->has_size)
1301 dtp->u.p.current_unit->size_used += (GFC_IO_INT) n;
1302 dtp->u.p.current_unit->bytes_left -= n;
1303 dtp->u.p.current_unit->strm_pos += (gfc_offset) n;