1 /* Copyright (C) 2002-2017 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)
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/>. */
34 typedef unsigned char uchar
;
36 /* read.c -- Deal with formatted reads */
39 /* set_integer()-- All of the integer assignments come here to
40 actually place the value into memory. */
43 set_integer (void *dest
, GFC_INTEGER_LARGEST value
, int length
)
47 #ifdef HAVE_GFC_INTEGER_16
48 /* length=10 comes about for kind=10 real/complex BOZ, cf. PR41711. */
52 GFC_INTEGER_16 tmp
= value
;
53 memcpy (dest
, (void *) &tmp
, length
);
59 GFC_INTEGER_8 tmp
= value
;
60 memcpy (dest
, (void *) &tmp
, length
);
65 GFC_INTEGER_4 tmp
= value
;
66 memcpy (dest
, (void *) &tmp
, length
);
71 GFC_INTEGER_2 tmp
= value
;
72 memcpy (dest
, (void *) &tmp
, length
);
77 GFC_INTEGER_1 tmp
= value
;
78 memcpy (dest
, (void *) &tmp
, length
);
82 internal_error (NULL
, "Bad integer kind");
87 /* Max signed value of size give by length argument. */
92 #if defined HAVE_GFC_REAL_16 || defined HAVE_GFC_REAL_10
93 GFC_UINTEGER_LARGEST value
;
98 #if defined HAVE_GFC_REAL_16 || defined HAVE_GFC_REAL_10
102 for (int n
= 1; n
< 4 * length
; n
++)
103 value
= (value
<< 2) + 3;
107 return GFC_INTEGER_8_HUGE
;
109 return GFC_INTEGER_4_HUGE
;
111 return GFC_INTEGER_2_HUGE
;
113 return GFC_INTEGER_1_HUGE
;
115 internal_error (NULL
, "Bad integer kind");
120 /* convert_real()-- Convert a character representation of a floating
121 point number to the machine number. Returns nonzero if there is an
122 invalid input. Note: many architectures (e.g. IA-64, HP-PA)
123 require that the storage pointed to by the dest argument is
124 properly aligned for the type in question. */
127 convert_real (st_parameter_dt
*dtp
, void *dest
, const char *buffer
, int length
)
130 int round_mode
, old_round_mode
;
132 switch (dtp
->u
.p
.current_unit
->round_status
)
134 case ROUND_COMPATIBLE
:
135 /* FIXME: As NEAREST but round away from zero for a tie. */
136 case ROUND_UNSPECIFIED
:
137 /* Should not occur. */
138 case ROUND_PROCDEFINED
:
139 round_mode
= ROUND_NEAREST
;
142 round_mode
= dtp
->u
.p
.current_unit
->round_status
;
146 old_round_mode
= get_fpu_rounding_mode();
147 set_fpu_rounding_mode (round_mode
);
152 *((GFC_REAL_4
*) dest
) =
153 #if defined(HAVE_STRTOF)
154 gfc_strtof (buffer
, &endptr
);
156 (GFC_REAL_4
) gfc_strtod (buffer
, &endptr
);
161 *((GFC_REAL_8
*) dest
) = gfc_strtod (buffer
, &endptr
);
164 #if defined(HAVE_GFC_REAL_10) && defined (HAVE_STRTOLD)
166 *((GFC_REAL_10
*) dest
) = gfc_strtold (buffer
, &endptr
);
170 #if defined(HAVE_GFC_REAL_16)
171 # if defined(GFC_REAL_16_IS_FLOAT128)
173 *((GFC_REAL_16
*) dest
) = __qmath_(strtoflt128
) (buffer
, &endptr
);
175 # elif defined(HAVE_STRTOLD)
177 *((GFC_REAL_16
*) dest
) = gfc_strtold (buffer
, &endptr
);
183 internal_error (&dtp
->common
, "Unsupported real kind during IO");
186 set_fpu_rounding_mode (old_round_mode
);
188 if (buffer
== endptr
)
190 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
,
191 "Error during floating point read");
192 next_record (dtp
, 1);
199 /* convert_infnan()-- Convert character INF/NAN representation to the
200 machine number. Note: many architectures (e.g. IA-64, HP-PA) require
201 that the storage pointed to by the dest argument is properly aligned
202 for the type in question. */
205 convert_infnan (st_parameter_dt
*dtp
, void *dest
, const char *buffer
,
208 const char *s
= buffer
;
209 int is_inf
, plus
= 1;
225 *((GFC_REAL_4
*) dest
) = plus
? __builtin_inff () : -__builtin_inff ();
227 *((GFC_REAL_4
*) dest
) = plus
? __builtin_nanf ("") : -__builtin_nanf ("");
232 *((GFC_REAL_8
*) dest
) = plus
? __builtin_inf () : -__builtin_inf ();
234 *((GFC_REAL_8
*) dest
) = plus
? __builtin_nan ("") : -__builtin_nan ("");
237 #if defined(HAVE_GFC_REAL_10)
240 *((GFC_REAL_10
*) dest
) = plus
? __builtin_infl () : -__builtin_infl ();
242 *((GFC_REAL_10
*) dest
) = plus
? __builtin_nanl ("") : -__builtin_nanl ("");
246 #if defined(HAVE_GFC_REAL_16)
247 # if defined(GFC_REAL_16_IS_FLOAT128)
249 *((GFC_REAL_16
*) dest
) = __qmath_(strtoflt128
) (buffer
, NULL
);
254 *((GFC_REAL_16
*) dest
) = plus
? __builtin_infl () : -__builtin_infl ();
256 *((GFC_REAL_16
*) dest
) = plus
? __builtin_nanl ("") : -__builtin_nanl ("");
262 internal_error (&dtp
->common
, "Unsupported real kind during IO");
269 /* read_l()-- Read a logical value */
272 read_l (st_parameter_dt
*dtp
, const fnode
*f
, char *dest
, int length
)
279 p
= read_block_form (dtp
, &w
);
302 set_integer (dest
, (GFC_INTEGER_LARGEST
) 1, length
);
306 set_integer (dest
, (GFC_INTEGER_LARGEST
) 0, length
);
310 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
,
311 "Bad value on logical read");
312 next_record (dtp
, 1);
319 read_utf8 (st_parameter_dt
*dtp
, int *nbytes
)
321 static const uchar masks
[6] = { 0x7F, 0x1F, 0x0F, 0x07, 0x02, 0x01 };
322 static const uchar patns
[6] = { 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
329 s
= read_block_form (dtp
, nbytes
);
333 /* If this is a short read, just return. */
341 /* The number of leading 1-bits in the first byte indicates how many
343 for (nb
= 2; nb
< 7; nb
++)
344 if ((c
& ~masks
[nb
-1]) == patns
[nb
-1])
349 c
= (c
& masks
[nb
-1]);
352 s
= read_block_form (dtp
, &nread
);
355 /* Decode the bytes read. */
356 for (i
= 1; i
< nb
; i
++)
358 gfc_char4_t n
= *s
++;
360 if ((n
& 0xC0) != 0x80)
363 c
= ((c
<< 6) + (n
& 0x3F));
366 /* Make sure the shortest possible encoding was used. */
367 if (c
<= 0x7F && nb
> 1) goto invalid
;
368 if (c
<= 0x7FF && nb
> 2) goto invalid
;
369 if (c
<= 0xFFFF && nb
> 3) goto invalid
;
370 if (c
<= 0x1FFFFF && nb
> 4) goto invalid
;
371 if (c
<= 0x3FFFFFF && nb
> 5) goto invalid
;
373 /* Make sure the character is valid. */
374 if (c
> 0x7FFFFFFF || (c
>= 0xD800 && c
<= 0xDFFF))
380 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
, "Invalid UTF-8 encoding");
381 return (gfc_char4_t
) '?';
386 read_utf8_char1 (st_parameter_dt
*dtp
, char *p
, int len
, int width
)
393 len
= (width
< len
) ? len
: width
;
397 /* Proceed with decoding one character at a time. */
398 for (j
= 0; j
< len
; j
++, dest
++)
400 c
= read_utf8 (dtp
, &nbytes
);
402 /* Check for a short read and if so, break out. */
406 *dest
= c
> 255 ? '?' : (uchar
) c
;
409 /* If there was a short read, pad the remaining characters. */
410 for (i
= j
; i
< len
; i
++)
416 read_default_char1 (st_parameter_dt
*dtp
, char *p
, int len
, int width
)
421 s
= read_block_form (dtp
, &width
);
428 m
= (width
> len
) ? len
: width
;
433 memset (p
+ m
, ' ', n
);
438 read_utf8_char4 (st_parameter_dt
*dtp
, void *p
, int len
, int width
)
444 len
= (width
< len
) ? len
: width
;
446 dest
= (gfc_char4_t
*) p
;
448 /* Proceed with decoding one character at a time. */
449 for (j
= 0; j
< len
; j
++, dest
++)
451 *dest
= read_utf8 (dtp
, &nbytes
);
453 /* Check for a short read and if so, break out. */
458 /* If there was a short read, pad the remaining characters. */
459 for (i
= j
; i
< len
; i
++)
460 *dest
++ = (gfc_char4_t
) ' ';
466 read_default_char4 (st_parameter_dt
*dtp
, char *p
, int len
, int width
)
471 if (is_char4_unit(dtp
))
475 s4
= (gfc_char4_t
*) read_block_form4 (dtp
, &width
);
482 m
= ((int) width
> len
) ? len
: (int) width
;
484 dest
= (gfc_char4_t
*) p
;
486 for (n
= 0; n
< m
; n
++)
489 for (n
= 0; n
< len
- (int) width
; n
++)
490 *dest
++ = (gfc_char4_t
) ' ';
496 s
= read_block_form (dtp
, &width
);
503 m
= ((int) width
> len
) ? len
: (int) width
;
505 dest
= (gfc_char4_t
*) p
;
507 for (n
= 0; n
< m
; n
++, dest
++, s
++)
508 *dest
= (unsigned char ) *s
;
510 for (n
= 0; n
< len
- (int) width
; n
++, dest
++)
511 *dest
= (unsigned char) ' ';
516 /* read_a()-- Read a character record into a KIND=1 character destination,
517 processing UTF-8 encoding if necessary. */
520 read_a (st_parameter_dt
*dtp
, const fnode
*f
, char *p
, int length
)
526 if (wi
== -1) /* '(A)' edit descriptor */
530 /* Read in w characters, treating comma as not a separator. */
531 dtp
->u
.p
.sf_read_comma
= 0;
533 if (dtp
->u
.p
.current_unit
->flags
.encoding
== ENCODING_UTF8
)
534 read_utf8_char1 (dtp
, p
, length
, w
);
536 read_default_char1 (dtp
, p
, length
, w
);
538 dtp
->u
.p
.sf_read_comma
=
539 dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
? 0 : 1;
543 /* read_a_char4()-- Read a character record into a KIND=4 character destination,
544 processing UTF-8 encoding if necessary. */
547 read_a_char4 (st_parameter_dt
*dtp
, const fnode
*f
, char *p
, int length
)
552 if (w
== -1) /* '(A)' edit descriptor */
555 /* Read in w characters, treating comma as not a separator. */
556 dtp
->u
.p
.sf_read_comma
= 0;
558 if (dtp
->u
.p
.current_unit
->flags
.encoding
== ENCODING_UTF8
)
559 read_utf8_char4 (dtp
, p
, length
, w
);
561 read_default_char4 (dtp
, p
, length
, w
);
563 dtp
->u
.p
.sf_read_comma
=
564 dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
? 0 : 1;
567 /* eat_leading_spaces()-- Given a character pointer and a width,
568 ignore the leading spaces. */
571 eat_leading_spaces (int *width
, char *p
)
575 if (*width
== 0 || *p
!= ' ')
587 next_char (st_parameter_dt
*dtp
, char **p
, int *w
)
602 if (dtp
->u
.p
.blank_status
!= BLANK_UNSPECIFIED
)
603 return ' '; /* return a blank to signal a null */
605 /* At this point, the rest of the field has to be trailing blanks */
619 /* read_decimal()-- Read a decimal integer value. The values here are
623 read_decimal (st_parameter_dt
*dtp
, const fnode
*f
, char *dest
, int length
)
625 GFC_UINTEGER_LARGEST value
, maxv
, maxv_10
;
626 GFC_INTEGER_LARGEST v
;
632 p
= read_block_form (dtp
, &w
);
637 p
= eat_leading_spaces (&w
, p
);
640 set_integer (dest
, (GFC_INTEGER_LARGEST
) 0, length
);
662 maxv
= si_max (length
);
667 /* At this point we have a digit-string */
672 c
= next_char (dtp
, &p
, &w
);
678 if (dtp
->u
.p
.blank_status
== BLANK_NULL
)
681 for ( ; w
> 0; p
++, w
--)
682 if (*p
!= ' ') break;
685 if (dtp
->u
.p
.blank_status
== BLANK_ZERO
) c
= '0';
688 if (c
< '0' || c
> '9')
697 if (value
> maxv
- c
)
707 set_integer (dest
, v
, length
);
711 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
,
712 "Bad value during integer read");
713 next_record (dtp
, 1);
717 generate_error (&dtp
->common
, LIBERROR_READ_OVERFLOW
,
718 "Value overflowed during integer read");
719 next_record (dtp
, 1);
724 /* read_radix()-- This function reads values for non-decimal radixes.
725 The difference here is that we treat the values here as unsigned
726 values for the purposes of overflow. If minus sign is present and
727 the top bit is set, the value will be incorrect. */
730 read_radix (st_parameter_dt
*dtp
, const fnode
*f
, char *dest
, int length
,
733 GFC_UINTEGER_LARGEST value
, maxv
, maxv_r
;
734 GFC_INTEGER_LARGEST v
;
740 p
= read_block_form (dtp
, &w
);
745 p
= eat_leading_spaces (&w
, p
);
748 set_integer (dest
, (GFC_INTEGER_LARGEST
) 0, length
);
752 /* Maximum unsigned value, assuming two's complement. */
753 maxv
= 2 * si_max (length
) + 1;
754 maxv_r
= maxv
/ radix
;
775 /* At this point we have a digit-string */
780 c
= next_char (dtp
, &p
, &w
);
785 if (dtp
->u
.p
.blank_status
== BLANK_NULL
) continue;
786 if (dtp
->u
.p
.blank_status
== BLANK_ZERO
) c
= '0';
792 if (c
< '0' || c
> '1')
797 if (c
< '0' || c
> '7')
822 c
= c
- 'a' + '9' + 1;
831 c
= c
- 'A' + '9' + 1;
845 value
= radix
* value
;
847 if (maxv
- c
< value
)
856 set_integer (dest
, v
, length
);
860 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
,
861 "Bad value during integer read");
862 next_record (dtp
, 1);
866 generate_error (&dtp
->common
, LIBERROR_READ_OVERFLOW
,
867 "Value overflowed during integer read");
868 next_record (dtp
, 1);
873 /* read_f()-- Read a floating point number with F-style editing, which
874 is what all of the other floating point descriptors behave as. The
875 tricky part is that optional spaces are allowed after an E or D,
876 and the implicit decimal point if a decimal point is not present in
880 read_f (st_parameter_dt
*dtp
, const fnode
*f
, char *dest
, int length
)
885 int w
, seen_dp
, exponent
;
890 int seen_int_digit
; /* Seen a digit before the decimal point? */
891 int seen_dec_digit
; /* Seen a digit after the decimal point? */
901 /* Read in the next block. */
902 p
= read_block_form (dtp
, &w
);
905 p
= eat_leading_spaces (&w
, (char*) p
);
909 /* In this buffer we're going to re-format the number cleanly to be parsed
910 by convert_real in the end; this assures we're using strtod from the
911 C library for parsing and thus probably get the best accuracy possible.
912 This process may add a '+0.0' in front of the number as well as change the
913 exponent because of an implicit decimal point or the like. Thus allocating
914 strlen ("+0.0e-1000") == 10 characters plus one for NUL more than the
915 original buffer had should be enough. */
917 if (buf_size
> READF_TMP
)
918 buffer
= xmalloc (buf_size
);
923 if (*p
== '-' || *p
== '+')
931 p
= eat_leading_spaces (&w
, (char*) p
);
935 /* Check for Infinity or NaN. */
936 if (unlikely ((w
>= 3 && (*p
== 'i' || *p
== 'I' || *p
== 'n' || *p
== 'N'))))
941 /* Scan through the buffer keeping track of spaces and parenthesis. We
942 null terminate the string as soon as we see a left paren or if we are
943 BLANK_NULL mode. Leading spaces have already been skipped above,
944 trailing spaces are ignored by converting to '\0'. A space
945 between "NaN" and the optional perenthesis is not permitted. */
952 if (dtp
->u
.p
.blank_status
== BLANK_ZERO
)
966 if (seen_paren
++ != 1)
980 if (seen_paren
!= 0 && seen_paren
!= 2)
983 if ((strcmp (save
, "inf") == 0) || (strcmp (save
, "infinity") == 0))
988 else if (strcmp (save
, "nan") != 0)
991 convert_infnan (dtp
, dest
, buffer
, length
);
992 if (buf_size
> READF_TMP
)
997 /* Process the mantissa string. */
1003 if (dtp
->u
.p
.current_unit
->decimal_status
!= DECIMAL_COMMA
)
1009 if (!seen_int_digit
)
1016 if (dtp
->u
.p
.blank_status
== BLANK_ZERO
)
1021 else if (dtp
->u
.p
.blank_status
== BLANK_NULL
)
1024 /* TODO: Should we check instead that there are only trailing
1025 blanks here, as is done below for exponents? */
1068 /* No exponent has been seen, so we use the current scale factor. */
1069 exponent
= - dtp
->u
.p
.scale_factor
;
1072 /* At this point the start of an exponent has been found. */
1074 p
= eat_leading_spaces (&w
, (char*) p
);
1075 if (*p
== '-' || *p
== '+')
1083 /* At this point a digit string is required. We calculate the value
1084 of the exponent in order to take account of the scale factor and
1085 the d parameter before explict conversion takes place. */
1089 /* Extension: allow default exponent of 0 when omitted. */
1090 if (dtp
->common
.flags
& IOPARM_DT_DEFAULT_EXP
)
1096 if (dtp
->u
.p
.blank_status
== BLANK_UNSPECIFIED
)
1098 while (w
> 0 && isdigit (*p
))
1101 exponent
+= *p
- '0';
1106 /* Only allow trailing blanks. */
1115 else /* BZ or BN status is enabled. */
1121 if (dtp
->u
.p
.blank_status
== BLANK_ZERO
)
1124 assert (dtp
->u
.p
.blank_status
== BLANK_NULL
);
1126 else if (!isdigit (*p
))
1131 exponent
+= *p
- '0';
1139 exponent
*= exponent_sign
;
1142 /* Use the precision specified in the format if no decimal point has been
1145 exponent
-= f
->u
.real
.d
;
1147 /* Output a trailing '0' after decimal point if not yet found. */
1148 if (seen_dp
&& !seen_dec_digit
)
1150 /* Handle input of style "E+NN" by inserting a 0 for the
1152 else if (!seen_int_digit
&& !seen_dec_digit
)
1154 notify_std (&dtp
->common
, GFC_STD_LEGACY
,
1155 "REAL input of style 'E+NN'");
1159 /* Print out the exponent to finish the reformatted number. Maximum 4
1160 digits for the exponent. */
1169 exponent
= - exponent
;
1172 if (exponent
>= 10000)
1175 for (dig
= 3; dig
>= 0; --dig
)
1177 out
[dig
] = (char) ('0' + exponent
% 10);
1184 /* Do the actual conversion. */
1185 convert_real (dtp
, dest
, buffer
, length
);
1186 if (buf_size
> READF_TMP
)
1190 /* The value read is zero. */
1195 *((GFC_REAL_4
*) dest
) = 0.0;
1199 *((GFC_REAL_8
*) dest
) = 0.0;
1202 #ifdef HAVE_GFC_REAL_10
1204 *((GFC_REAL_10
*) dest
) = 0.0;
1208 #ifdef HAVE_GFC_REAL_16
1210 *((GFC_REAL_16
*) dest
) = 0.0;
1215 internal_error (&dtp
->common
, "Unsupported real kind during IO");
1220 if (buf_size
> READF_TMP
)
1222 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
,
1223 "Bad value during floating point read");
1224 next_record (dtp
, 1);
1229 /* read_x()-- Deal with the X/TR descriptor. We just read some data
1230 and never look at it. */
1233 read_x (st_parameter_dt
*dtp
, int n
)
1237 if ((dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
|| is_internal_unit (dtp
))
1238 && dtp
->u
.p
.current_unit
->bytes_left
< n
)
1239 n
= dtp
->u
.p
.current_unit
->bytes_left
;
1246 if (is_internal_unit (dtp
))
1248 mem_alloc_r (dtp
->u
.p
.current_unit
->s
, &length
);
1249 if (unlikely (length
< n
))
1254 if (dtp
->u
.p
.sf_seen_eor
)
1260 q
= fbuf_getc (dtp
->u
.p
.current_unit
);
1263 else if (dtp
->u
.p
.current_unit
->flags
.cc
!= CC_NONE
1264 && (q
== '\n' || q
== '\r'))
1266 /* Unexpected end of line. Set the position. */
1267 dtp
->u
.p
.sf_seen_eor
= 1;
1269 /* If we see an EOR during non-advancing I/O, we need to skip
1270 the rest of the I/O statement. Set the corresponding flag. */
1271 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
|| dtp
->u
.p
.seen_dollar
)
1272 dtp
->u
.p
.eor_condition
= 1;
1274 /* If we encounter a CR, it might be a CRLF. */
1275 if (q
== '\r') /* Probably a CRLF */
1277 /* See if there is an LF. */
1278 q2
= fbuf_getc (dtp
->u
.p
.current_unit
);
1280 dtp
->u
.p
.sf_seen_eor
= 2;
1281 else if (q2
!= EOF
) /* Oops, seek back. */
1282 fbuf_seek (dtp
->u
.p
.current_unit
, -1, SEEK_CUR
);
1290 if (((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0) ||
1291 dtp
->u
.p
.current_unit
->has_size
)
1292 dtp
->u
.p
.current_unit
->size_used
+= (GFC_IO_INT
) n
;
1293 dtp
->u
.p
.current_unit
->bytes_left
-= n
;
1294 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) n
;