Revert patch accidentily created on the wrong sandbox
[official-gcc.git] / libgfortran / io / read.c
blob70da220704238675cad73d5c51dd6515dc971566
1 /* Copyright (C) 2002-2020 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 /* This is a legacy extension, and the frontend will only allow such cases
639 * through when -fdec-format-defaults is passed.
641 if (w == (size_t) DEFAULT_WIDTH)
642 w = default_width_for_integer (length);
644 p = read_block_form (dtp, &w);
646 if (p == NULL)
647 return;
649 p = eat_leading_spaces (&w, p);
650 if (w == 0)
652 set_integer (dest, (GFC_INTEGER_LARGEST) 0, length);
653 return;
656 negative = 0;
658 switch (*p)
660 case '-':
661 negative = 1;
662 /* Fall through */
664 case '+':
665 p++;
666 if (--w == 0)
667 goto bad;
668 /* Fall through */
670 default:
671 break;
674 maxv = si_max (length);
675 if (negative)
676 maxv++;
677 maxv_10 = maxv / 10;
679 /* At this point we have a digit-string */
680 value = 0;
682 for (;;)
684 c = next_char (dtp, &p, &w);
685 if (c == '\0')
686 break;
688 if (c == ' ')
690 if (dtp->u.p.blank_status == BLANK_NULL)
692 /* Skip spaces. */
693 for ( ; w > 0; p++, w--)
694 if (*p != ' ') break;
695 continue;
697 if (dtp->u.p.blank_status == BLANK_ZERO) c = '0';
700 if (c < '0' || c > '9')
701 goto bad;
703 if (value > maxv_10)
704 goto overflow;
706 c -= '0';
707 value = 10 * value;
709 if (value > maxv - c)
710 goto overflow;
711 value += c;
714 if (negative)
715 v = -value;
716 else
717 v = value;
719 set_integer (dest, v, length);
720 return;
722 bad:
723 generate_error (&dtp->common, LIBERROR_READ_VALUE,
724 "Bad value during integer read");
725 next_record (dtp, 1);
726 return;
728 overflow:
729 generate_error (&dtp->common, LIBERROR_READ_OVERFLOW,
730 "Value overflowed during integer read");
731 next_record (dtp, 1);
736 /* read_radix()-- This function reads values for non-decimal radixes.
737 The difference here is that we treat the values here as unsigned
738 values for the purposes of overflow. If minus sign is present and
739 the top bit is set, the value will be incorrect. */
741 void
742 read_radix (st_parameter_dt *dtp, const fnode *f, char *dest, int length,
743 int radix)
745 GFC_UINTEGER_LARGEST value, maxv, maxv_r;
746 GFC_INTEGER_LARGEST v;
747 size_t w;
748 int negative;
749 char c, *p;
751 w = f->u.w;
753 p = read_block_form (dtp, &w);
755 if (p == NULL)
756 return;
758 p = eat_leading_spaces (&w, p);
759 if (w == 0)
761 set_integer (dest, (GFC_INTEGER_LARGEST) 0, length);
762 return;
765 /* Maximum unsigned value, assuming two's complement. */
766 maxv = 2 * si_max (length) + 1;
767 maxv_r = maxv / radix;
769 negative = 0;
770 value = 0;
772 switch (*p)
774 case '-':
775 negative = 1;
776 /* Fall through */
778 case '+':
779 p++;
780 if (--w == 0)
781 goto bad;
782 /* Fall through */
784 default:
785 break;
788 /* At this point we have a digit-string */
789 value = 0;
791 for (;;)
793 c = next_char (dtp, &p, &w);
794 if (c == '\0')
795 break;
796 if (c == ' ')
798 if (dtp->u.p.blank_status == BLANK_NULL) continue;
799 if (dtp->u.p.blank_status == BLANK_ZERO) c = '0';
802 switch (radix)
804 case 2:
805 if (c < '0' || c > '1')
806 goto bad;
807 break;
809 case 8:
810 if (c < '0' || c > '7')
811 goto bad;
812 break;
814 case 16:
815 switch (c)
817 case '0':
818 case '1':
819 case '2':
820 case '3':
821 case '4':
822 case '5':
823 case '6':
824 case '7':
825 case '8':
826 case '9':
827 break;
829 case 'a':
830 case 'b':
831 case 'c':
832 case 'd':
833 case 'e':
834 case 'f':
835 c = c - 'a' + '9' + 1;
836 break;
838 case 'A':
839 case 'B':
840 case 'C':
841 case 'D':
842 case 'E':
843 case 'F':
844 c = c - 'A' + '9' + 1;
845 break;
847 default:
848 goto bad;
851 break;
854 if (value > maxv_r)
855 goto overflow;
857 c -= '0';
858 value = radix * value;
860 if (maxv - c < value)
861 goto overflow;
862 value += c;
865 v = value;
866 if (negative)
867 v = -v;
869 set_integer (dest, v, length);
870 return;
872 bad:
873 generate_error (&dtp->common, LIBERROR_READ_VALUE,
874 "Bad value during integer read");
875 next_record (dtp, 1);
876 return;
878 overflow:
879 generate_error (&dtp->common, LIBERROR_READ_OVERFLOW,
880 "Value overflowed during integer read");
881 next_record (dtp, 1);
886 /* read_f()-- Read a floating point number with F-style editing, which
887 is what all of the other floating point descriptors behave as. The
888 tricky part is that optional spaces are allowed after an E or D,
889 and the implicit decimal point if a decimal point is not present in
890 the input. */
892 void
893 read_f (st_parameter_dt *dtp, const fnode *f, char *dest, int length)
895 #define READF_TMP 50
896 char tmp[READF_TMP];
897 size_t buf_size = 0;
898 size_t w;
899 int seen_dp, exponent;
900 int exponent_sign;
901 const char *p;
902 char *buffer;
903 char *out;
904 int seen_int_digit; /* Seen a digit before the decimal point? */
905 int seen_dec_digit; /* Seen a digit after the decimal point? */
907 seen_dp = 0;
908 seen_int_digit = 0;
909 seen_dec_digit = 0;
910 exponent_sign = 1;
911 exponent = 0;
912 w = f->u.w;
913 buffer = tmp;
915 /* Read in the next block. */
916 p = read_block_form (dtp, &w);
917 if (p == NULL)
918 return;
919 p = eat_leading_spaces (&w, (char*) p);
920 if (w == 0)
921 goto zero;
923 /* In this buffer we're going to re-format the number cleanly to be parsed
924 by convert_real in the end; this assures we're using strtod from the
925 C library for parsing and thus probably get the best accuracy possible.
926 This process may add a '+0.0' in front of the number as well as change the
927 exponent because of an implicit decimal point or the like. Thus allocating
928 strlen ("+0.0e-1000") == 10 characters plus one for NUL more than the
929 original buffer had should be enough. */
930 buf_size = w + 11;
931 if (buf_size > READF_TMP)
932 buffer = xmalloc (buf_size);
934 out = buffer;
936 /* Optional sign */
937 if (*p == '-' || *p == '+')
939 if (*p == '-')
940 *(out++) = '-';
941 ++p;
942 --w;
945 p = eat_leading_spaces (&w, (char*) p);
946 if (w == 0)
947 goto zero;
949 /* Check for Infinity or NaN. */
950 if (unlikely ((w >= 3 && (*p == 'i' || *p == 'I' || *p == 'n' || *p == 'N'))))
952 int seen_paren = 0;
953 char *save = out;
955 /* Scan through the buffer keeping track of spaces and parenthesis. We
956 null terminate the string as soon as we see a left paren or if we are
957 BLANK_NULL mode. Leading spaces have already been skipped above,
958 trailing spaces are ignored by converting to '\0'. A space
959 between "NaN" and the optional perenthesis is not permitted. */
960 while (w > 0)
962 *out = tolower (*p);
963 switch (*p)
965 case ' ':
966 if (dtp->u.p.blank_status == BLANK_ZERO)
968 *out = '0';
969 break;
971 *out = '\0';
972 if (seen_paren == 1)
973 goto bad_float;
974 break;
975 case '(':
976 seen_paren++;
977 *out = '\0';
978 break;
979 case ')':
980 if (seen_paren++ != 1)
981 goto bad_float;
982 break;
983 default:
984 if (!isalnum (*out))
985 goto bad_float;
987 --w;
988 ++p;
989 ++out;
992 *out = '\0';
994 if (seen_paren != 0 && seen_paren != 2)
995 goto bad_float;
997 if ((strcmp (save, "inf") == 0) || (strcmp (save, "infinity") == 0))
999 if (seen_paren)
1000 goto bad_float;
1002 else if (strcmp (save, "nan") != 0)
1003 goto bad_float;
1005 convert_infnan (dtp, dest, buffer, length);
1006 if (buf_size > READF_TMP)
1007 free (buffer);
1008 return;
1011 /* Process the mantissa string. */
1012 while (w > 0)
1014 switch (*p)
1016 case ',':
1017 if (dtp->u.p.current_unit->decimal_status != DECIMAL_COMMA)
1018 goto bad_float;
1019 /* Fall through. */
1020 case '.':
1021 if (seen_dp)
1022 goto bad_float;
1023 if (!seen_int_digit)
1024 *(out++) = '0';
1025 *(out++) = '.';
1026 seen_dp = 1;
1027 break;
1029 case ' ':
1030 if (dtp->u.p.blank_status == BLANK_ZERO)
1032 *(out++) = '0';
1033 goto found_digit;
1035 else if (dtp->u.p.blank_status == BLANK_NULL)
1036 break;
1037 else
1038 /* TODO: Should we check instead that there are only trailing
1039 blanks here, as is done below for exponents? */
1040 goto done;
1041 /* Fall through. */
1042 case '0':
1043 case '1':
1044 case '2':
1045 case '3':
1046 case '4':
1047 case '5':
1048 case '6':
1049 case '7':
1050 case '8':
1051 case '9':
1052 *(out++) = *p;
1053 found_digit:
1054 if (!seen_dp)
1055 seen_int_digit = 1;
1056 else
1057 seen_dec_digit = 1;
1058 break;
1060 case '-':
1061 case '+':
1062 goto exponent;
1064 case 'e':
1065 case 'E':
1066 case 'd':
1067 case 'D':
1068 case 'q':
1069 case 'Q':
1070 ++p;
1071 --w;
1072 goto exponent;
1074 default:
1075 goto bad_float;
1078 ++p;
1079 --w;
1082 /* No exponent has been seen, so we use the current scale factor. */
1083 exponent = - dtp->u.p.scale_factor;
1084 goto done;
1086 /* At this point the start of an exponent has been found. */
1087 exponent:
1088 p = eat_leading_spaces (&w, (char*) p);
1089 if (*p == '-' || *p == '+')
1091 if (*p == '-')
1092 exponent_sign = -1;
1093 ++p;
1094 --w;
1097 /* At this point a digit string is required. We calculate the value
1098 of the exponent in order to take account of the scale factor and
1099 the d parameter before explict conversion takes place. */
1101 if (w == 0)
1103 /* Extension: allow default exponent of 0 when omitted. */
1104 if (dtp->common.flags & IOPARM_DT_DEC_EXT)
1105 goto done;
1106 else
1107 goto bad_float;
1110 if (dtp->u.p.blank_status == BLANK_UNSPECIFIED)
1112 while (w > 0 && isdigit (*p))
1114 exponent *= 10;
1115 exponent += *p - '0';
1116 ++p;
1117 --w;
1120 /* Only allow trailing blanks. */
1121 while (w > 0)
1123 if (*p != ' ')
1124 goto bad_float;
1125 ++p;
1126 --w;
1129 else /* BZ or BN status is enabled. */
1131 while (w > 0)
1133 if (*p == ' ')
1135 if (dtp->u.p.blank_status == BLANK_ZERO)
1136 exponent *= 10;
1137 else
1138 assert (dtp->u.p.blank_status == BLANK_NULL);
1140 else if (!isdigit (*p))
1141 goto bad_float;
1142 else
1144 exponent *= 10;
1145 exponent += *p - '0';
1148 ++p;
1149 --w;
1153 exponent *= exponent_sign;
1155 done:
1156 /* Use the precision specified in the format if no decimal point has been
1157 seen. */
1158 if (!seen_dp)
1159 exponent -= f->u.real.d;
1161 /* Output a trailing '0' after decimal point if not yet found. */
1162 if (seen_dp && !seen_dec_digit)
1163 *(out++) = '0';
1164 /* Handle input of style "E+NN" by inserting a 0 for the
1165 significand. */
1166 else if (!seen_int_digit && !seen_dec_digit)
1168 notify_std (&dtp->common, GFC_STD_LEGACY,
1169 "REAL input of style 'E+NN'");
1170 *(out++) = '0';
1173 /* Print out the exponent to finish the reformatted number. Maximum 4
1174 digits for the exponent. */
1175 if (exponent != 0)
1177 int dig;
1179 *(out++) = 'e';
1180 if (exponent < 0)
1182 *(out++) = '-';
1183 exponent = - exponent;
1186 if (exponent >= 10000)
1187 goto bad_float;
1189 for (dig = 3; dig >= 0; --dig)
1191 out[dig] = (char) ('0' + exponent % 10);
1192 exponent /= 10;
1194 out += 4;
1196 *(out++) = '\0';
1198 /* Do the actual conversion. */
1199 convert_real (dtp, dest, buffer, length);
1200 if (buf_size > READF_TMP)
1201 free (buffer);
1202 return;
1204 /* The value read is zero. */
1205 zero:
1206 switch (length)
1208 case 4:
1209 *((GFC_REAL_4 *) dest) = 0.0;
1210 break;
1212 case 8:
1213 *((GFC_REAL_8 *) dest) = 0.0;
1214 break;
1216 #ifdef HAVE_GFC_REAL_10
1217 case 10:
1218 *((GFC_REAL_10 *) dest) = 0.0;
1219 break;
1220 #endif
1222 #ifdef HAVE_GFC_REAL_16
1223 case 16:
1224 *((GFC_REAL_16 *) dest) = 0.0;
1225 break;
1226 #endif
1228 default:
1229 internal_error (&dtp->common, "Unsupported real kind during IO");
1231 return;
1233 bad_float:
1234 if (buf_size > READF_TMP)
1235 free (buffer);
1236 generate_error (&dtp->common, LIBERROR_READ_VALUE,
1237 "Bad value during floating point read");
1238 next_record (dtp, 1);
1239 return;
1243 /* read_x()-- Deal with the X/TR descriptor. We just read some data
1244 and never look at it. */
1246 void
1247 read_x (st_parameter_dt *dtp, size_t n)
1249 size_t length;
1250 int q, q2;
1252 if ((dtp->u.p.current_unit->pad_status == PAD_NO || is_internal_unit (dtp))
1253 && dtp->u.p.current_unit->bytes_left < (gfc_offset) n)
1254 n = dtp->u.p.current_unit->bytes_left;
1256 if (n == 0)
1257 return;
1259 length = n;
1261 if (is_internal_unit (dtp))
1263 mem_alloc_r (dtp->u.p.current_unit->s, &length);
1264 if (unlikely (length < n))
1265 n = length;
1266 goto done;
1269 if (dtp->u.p.sf_seen_eor)
1270 return;
1272 n = 0;
1273 while (n < length)
1275 q = fbuf_getc (dtp->u.p.current_unit);
1276 if (q == EOF)
1277 break;
1278 else if (dtp->u.p.current_unit->flags.cc != CC_NONE
1279 && (q == '\n' || q == '\r'))
1281 /* Unexpected end of line. Set the position. */
1282 dtp->u.p.sf_seen_eor = 1;
1284 /* If we see an EOR during non-advancing I/O, we need to skip
1285 the rest of the I/O statement. Set the corresponding flag. */
1286 if (dtp->u.p.advance_status == ADVANCE_NO || dtp->u.p.seen_dollar)
1287 dtp->u.p.eor_condition = 1;
1289 /* If we encounter a CR, it might be a CRLF. */
1290 if (q == '\r') /* Probably a CRLF */
1292 /* See if there is an LF. */
1293 q2 = fbuf_getc (dtp->u.p.current_unit);
1294 if (q2 == '\n')
1295 dtp->u.p.sf_seen_eor = 2;
1296 else if (q2 != EOF) /* Oops, seek back. */
1297 fbuf_seek (dtp->u.p.current_unit, -1, SEEK_CUR);
1299 goto done;
1301 n++;
1304 done:
1305 if (((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0) ||
1306 dtp->u.p.current_unit->has_size)
1307 dtp->u.p.current_unit->size_used += (GFC_IO_INT) n;
1308 dtp->u.p.current_unit->bytes_left -= n;
1309 dtp->u.p.current_unit->strm_pos += (gfc_offset) n;