1 /* Copyright (C) 2002-2019 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
3 Namelist transfer functions contributed by Paul Thomas
4 F2003 I/O support contributed by Jerry DeLisle
6 This file is part of the GNU Fortran runtime library (libgfortran).
8 Libgfortran is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 Libgfortran is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
28 /* transfer.c -- Top level handling of data transfer statements. */
39 /* Calling conventions: Data transfer statements are unlike other
40 library calls in that they extend over several calls.
42 The first call is always a call to st_read() or st_write(). These
43 subroutines return no status unless a namelist read or write is
44 being done, in which case there is the usual status. No further
45 calls are necessary in this case.
47 For other sorts of data transfer, there are zero or more data
48 transfer statement that depend on the format of the data transfer
49 statement. For READ (and for backwards compatibily: for WRITE), one has
54 transfer_character_wide
62 transfer_integer_write
63 transfer_logical_write
64 transfer_character_write
65 transfer_character_wide_write
67 transfer_complex_write
68 transfer_real128_write
69 transfer_complex128_write
71 These subroutines do not return status. The *128 functions
72 are in the file transfer128.c.
74 The last call is a call to st_[read|write]_done(). While
75 something can easily go wrong with the initial st_read() or
76 st_write(), an error inhibits any data from actually being
79 extern void transfer_integer (st_parameter_dt
*, void *, int);
80 export_proto(transfer_integer
);
82 extern void transfer_integer_write (st_parameter_dt
*, void *, int);
83 export_proto(transfer_integer_write
);
85 extern void transfer_real (st_parameter_dt
*, void *, int);
86 export_proto(transfer_real
);
88 extern void transfer_real_write (st_parameter_dt
*, void *, int);
89 export_proto(transfer_real_write
);
91 extern void transfer_logical (st_parameter_dt
*, void *, int);
92 export_proto(transfer_logical
);
94 extern void transfer_logical_write (st_parameter_dt
*, void *, int);
95 export_proto(transfer_logical_write
);
97 extern void transfer_character (st_parameter_dt
*, void *, gfc_charlen_type
);
98 export_proto(transfer_character
);
100 extern void transfer_character_write (st_parameter_dt
*, void *, gfc_charlen_type
);
101 export_proto(transfer_character_write
);
103 extern void transfer_character_wide (st_parameter_dt
*, void *, gfc_charlen_type
, int);
104 export_proto(transfer_character_wide
);
106 extern void transfer_character_wide_write (st_parameter_dt
*,
107 void *, gfc_charlen_type
, int);
108 export_proto(transfer_character_wide_write
);
110 extern void transfer_complex (st_parameter_dt
*, void *, int);
111 export_proto(transfer_complex
);
113 extern void transfer_complex_write (st_parameter_dt
*, void *, int);
114 export_proto(transfer_complex_write
);
116 extern void transfer_array (st_parameter_dt
*, gfc_array_char
*, int,
118 export_proto(transfer_array
);
120 extern void transfer_array_write (st_parameter_dt
*, gfc_array_char
*, int,
122 export_proto(transfer_array_write
);
124 /* User defined derived type input/output. */
126 transfer_derived (st_parameter_dt
*dtp
, void *dtio_source
, void *dtio_proc
);
127 export_proto(transfer_derived
);
130 transfer_derived_write (st_parameter_dt
*dtp
, void *dtio_source
, void *dtio_proc
);
131 export_proto(transfer_derived_write
);
133 static void us_read (st_parameter_dt
*, int);
134 static void us_write (st_parameter_dt
*, int);
135 static void next_record_r_unf (st_parameter_dt
*, int);
136 static void next_record_w_unf (st_parameter_dt
*, int);
138 static const st_option advance_opt
[] = {
139 {"yes", ADVANCE_YES
},
145 static const st_option decimal_opt
[] = {
146 {"point", DECIMAL_POINT
},
147 {"comma", DECIMAL_COMMA
},
151 static const st_option round_opt
[] = {
153 {"down", ROUND_DOWN
},
154 {"zero", ROUND_ZERO
},
155 {"nearest", ROUND_NEAREST
},
156 {"compatible", ROUND_COMPATIBLE
},
157 {"processor_defined", ROUND_PROCDEFINED
},
162 static const st_option sign_opt
[] = {
164 {"suppress", SIGN_SS
},
165 {"processor_defined", SIGN_S
},
169 static const st_option blank_opt
[] = {
170 {"null", BLANK_NULL
},
171 {"zero", BLANK_ZERO
},
175 static const st_option delim_opt
[] = {
176 {"apostrophe", DELIM_APOSTROPHE
},
177 {"quote", DELIM_QUOTE
},
178 {"none", DELIM_NONE
},
182 static const st_option pad_opt
[] = {
188 static const st_option async_opt
[] = {
195 { FORMATTED_SEQUENTIAL
, UNFORMATTED_SEQUENTIAL
,
196 FORMATTED_DIRECT
, UNFORMATTED_DIRECT
, FORMATTED_STREAM
, UNFORMATTED_STREAM
202 current_mode (st_parameter_dt
*dtp
)
206 m
= FORM_UNSPECIFIED
;
208 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
210 m
= dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
?
211 FORMATTED_DIRECT
: UNFORMATTED_DIRECT
;
213 else if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
215 m
= dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
?
216 FORMATTED_SEQUENTIAL
: UNFORMATTED_SEQUENTIAL
;
218 else if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_STREAM
)
220 m
= dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
?
221 FORMATTED_STREAM
: UNFORMATTED_STREAM
;
228 /* Mid level data transfer statements. */
230 /* Read sequential file - internal unit */
233 read_sf_internal (st_parameter_dt
*dtp
, size_t *length
)
235 static char *empty_string
[0];
239 /* Zero size array gives internal unit len of 0. Nothing to read. */
240 if (dtp
->internal_unit_len
== 0
241 && dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
)
244 /* There are some cases with mixed DTIO where we have read a character
245 and saved it in the last character buffer, so we need to backup. */
246 if (unlikely (dtp
->u
.p
.current_unit
->child_dtio
> 0 &&
247 dtp
->u
.p
.current_unit
->last_char
!= EOF
- 1))
249 dtp
->u
.p
.current_unit
->last_char
= EOF
- 1;
250 sseek (dtp
->u
.p
.current_unit
->s
, -1, SEEK_CUR
);
253 /* To support legacy code we have to scan the input string one byte
254 at a time because we don't know where an early comma may be and the
255 requested length could go past the end of a comma shortened
256 string. We only do this if -std=legacy was given at compile
257 time. We also do not support this on kind=4 strings. */
258 if (unlikely(compile_options
.warn_std
== 0)) // the slow legacy way.
264 /* If we have seen an eor previously, return a length of 0. The
265 caller is responsible for correctly padding the input field. */
266 if (dtp
->u
.p
.sf_seen_eor
)
269 /* Just return something that isn't a NULL pointer, otherwise the
270 caller thinks an error occurred. */
271 return (char*) empty_string
;
274 /* Get the first character of the string to establish the base
275 address and check for comma or end-of-record condition. */
276 base
= mem_alloc_r (dtp
->u
.p
.current_unit
->s
, &tmp
);
279 dtp
->u
.p
.sf_seen_eor
= 1;
281 return (char*) empty_string
;
285 dtp
->u
.p
.current_unit
->bytes_left
--;
287 return (char*) empty_string
;
290 /* Now we scan the rest and deal with either an end-of-file
291 condition or a comma, as needed. */
292 for (n
= 1; n
< *length
; n
++)
294 q
= mem_alloc_r (dtp
->u
.p
.current_unit
->s
, &tmp
);
302 dtp
->u
.p
.current_unit
->bytes_left
-= n
;
311 if (is_char4_unit(dtp
))
313 gfc_char4_t
*p
= (gfc_char4_t
*) mem_alloc_r4 (dtp
->u
.p
.current_unit
->s
,
315 base
= fbuf_alloc (dtp
->u
.p
.current_unit
, lorig
);
316 for (size_t i
= 0; i
< *length
; i
++, p
++)
317 base
[i
] = *p
> 255 ? '?' : (unsigned char) *p
;
320 base
= mem_alloc_r (dtp
->u
.p
.current_unit
->s
, length
);
322 if (unlikely (lorig
> *length
))
329 dtp
->u
.p
.current_unit
->bytes_left
-= *length
;
331 if (((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0) ||
332 dtp
->u
.p
.current_unit
->has_size
)
333 dtp
->u
.p
.current_unit
->size_used
+= (GFC_IO_INT
) *length
;
339 /* When reading sequential formatted records we have a problem. We
340 don't know how long the line is until we read the trailing newline,
341 and we don't want to read too much. If we read too much, we might
342 have to do a physical seek backwards depending on how much data is
343 present, and devices like terminals aren't seekable and would cause
346 Given this, the solution is to read a byte at a time, stopping if
347 we hit the newline. For small allocations, we use a static buffer.
348 For larger allocations, we are forced to allocate memory on the
349 heap. Hopefully this won't happen very often. */
351 /* Read sequential file - external unit */
354 read_sf (st_parameter_dt
*dtp
, size_t *length
)
356 static char *empty_string
[0];
361 /* If we have seen an eor previously, return a length of 0. The
362 caller is responsible for correctly padding the input field. */
363 if (dtp
->u
.p
.sf_seen_eor
)
366 /* Just return something that isn't a NULL pointer, otherwise the
367 caller thinks an error occurred. */
368 return (char*) empty_string
;
371 /* There are some cases with mixed DTIO where we have read a character
372 and saved it in the last character buffer, so we need to backup. */
373 if (unlikely (dtp
->u
.p
.current_unit
->child_dtio
> 0 &&
374 dtp
->u
.p
.current_unit
->last_char
!= EOF
- 1))
376 dtp
->u
.p
.current_unit
->last_char
= EOF
- 1;
377 fbuf_seek (dtp
->u
.p
.current_unit
, -1, SEEK_CUR
);
382 /* Read data into format buffer and scan through it. */
387 q
= fbuf_getc (dtp
->u
.p
.current_unit
);
390 else if (dtp
->u
.p
.current_unit
->flags
.cc
!= CC_NONE
391 && (q
== '\n' || q
== '\r'))
393 /* Unexpected end of line. Set the position. */
394 dtp
->u
.p
.sf_seen_eor
= 1;
396 /* If we see an EOR during non-advancing I/O, we need to skip
397 the rest of the I/O statement. Set the corresponding flag. */
398 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
|| dtp
->u
.p
.seen_dollar
)
399 dtp
->u
.p
.eor_condition
= 1;
401 /* If we encounter a CR, it might be a CRLF. */
402 if (q
== '\r') /* Probably a CRLF */
404 /* See if there is an LF. */
405 q2
= fbuf_getc (dtp
->u
.p
.current_unit
);
407 dtp
->u
.p
.sf_seen_eor
= 2;
408 else if (q2
!= EOF
) /* Oops, seek back. */
409 fbuf_seek (dtp
->u
.p
.current_unit
, -1, SEEK_CUR
);
412 /* Without padding, terminate the I/O statement without assigning
413 the value. With padding, the value still needs to be assigned,
414 so we can just continue with a short read. */
415 if (dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
)
417 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
424 /* Short circuit the read if a comma is found during numeric input.
425 The flag is set to zero during character reads so that commas in
426 strings are not ignored */
428 if (dtp
->u
.p
.sf_read_comma
== 1)
431 notify_std (&dtp
->common
, GFC_STD_GNU
,
432 "Comma in formatted numeric read.");
440 /* A short read implies we hit EOF, unless we hit EOR, a comma, or
441 some other stuff. Set the relevant flags. */
442 if (lorig
> *length
&& !dtp
->u
.p
.sf_seen_eor
&& !seen_comma
)
446 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
)
448 if (dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
)
454 dtp
->u
.p
.eor_condition
= 1;
459 else if (dtp
->u
.p
.advance_status
== ADVANCE_NO
460 || dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
461 || dtp
->u
.p
.current_unit
->bytes_left
462 == dtp
->u
.p
.current_unit
->recl
)
471 dtp
->u
.p
.current_unit
->bytes_left
-= n
;
473 if (((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0) ||
474 dtp
->u
.p
.current_unit
->has_size
)
475 dtp
->u
.p
.current_unit
->size_used
+= (GFC_IO_INT
) n
;
477 /* We can't call fbuf_getptr before the loop doing fbuf_getc, because
478 fbuf_getc might reallocate the buffer. So return current pointer
479 minus all the advances, which is n plus up to two characters
480 of newline or comma. */
481 return fbuf_getptr (dtp
->u
.p
.current_unit
)
482 - n
- dtp
->u
.p
.sf_seen_eor
- seen_comma
;
486 /* Function for reading the next couple of bytes from the current
487 file, advancing the current position. We return NULL on end of record or
488 end of file. This function is only for formatted I/O, unformatted uses
491 If the read is short, then it is because the current record does not
492 have enough data to satisfy the read request and the file was
493 opened with PAD=YES. The caller must assume tailing spaces for
497 read_block_form (st_parameter_dt
*dtp
, size_t *nbytes
)
502 if (!is_stream_io (dtp
))
504 if (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) *nbytes
)
506 /* For preconnected units with default record length, set bytes left
507 to unit record length and proceed, otherwise error. */
508 if (dtp
->u
.p
.current_unit
->unit_number
== options
.stdin_unit
509 && dtp
->u
.p
.current_unit
->recl
== default_recl
)
510 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
513 if (unlikely (dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
)
514 && !is_internal_unit (dtp
))
516 /* Not enough data left. */
517 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
522 if (is_internal_unit(dtp
))
524 if (*nbytes
> 0 && dtp
->u
.p
.current_unit
->bytes_left
== 0)
526 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
)
528 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
535 if (unlikely (dtp
->u
.p
.current_unit
->bytes_left
== 0))
542 *nbytes
= dtp
->u
.p
.current_unit
->bytes_left
;
546 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
&&
547 (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
||
548 dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_STREAM
))
550 if (is_internal_unit (dtp
))
551 source
= read_sf_internal (dtp
, nbytes
);
553 source
= read_sf (dtp
, nbytes
);
555 dtp
->u
.p
.current_unit
->strm_pos
+=
556 (gfc_offset
) (*nbytes
+ dtp
->u
.p
.sf_seen_eor
);
560 /* If we reach here, we can assume it's direct access. */
562 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) *nbytes
;
565 source
= fbuf_read (dtp
->u
.p
.current_unit
, nbytes
);
566 fbuf_seek (dtp
->u
.p
.current_unit
, *nbytes
, SEEK_CUR
);
568 if (((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0) ||
569 dtp
->u
.p
.current_unit
->has_size
)
570 dtp
->u
.p
.current_unit
->size_used
+= (GFC_IO_INT
) *nbytes
;
572 if (norig
!= *nbytes
)
574 /* Short read, this shouldn't happen. */
575 if (dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
)
577 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
582 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) *nbytes
;
588 /* Read a block from a character(kind=4) internal unit, to be transferred into
589 a character(kind=4) variable. Note: Portions of this code borrowed from
592 read_block_form4 (st_parameter_dt
*dtp
, size_t *nbytes
)
594 static gfc_char4_t
*empty_string
[0];
598 if (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) *nbytes
)
599 *nbytes
= dtp
->u
.p
.current_unit
->bytes_left
;
601 /* Zero size array gives internal unit len of 0. Nothing to read. */
602 if (dtp
->internal_unit_len
== 0
603 && dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
)
606 /* If we have seen an eor previously, return a length of 0. The
607 caller is responsible for correctly padding the input field. */
608 if (dtp
->u
.p
.sf_seen_eor
)
611 /* Just return something that isn't a NULL pointer, otherwise the
612 caller thinks an error occurred. */
617 source
= (gfc_char4_t
*) mem_alloc_r4 (dtp
->u
.p
.current_unit
->s
, nbytes
);
619 if (unlikely (lorig
> *nbytes
))
625 dtp
->u
.p
.current_unit
->bytes_left
-= *nbytes
;
627 if (((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0) ||
628 dtp
->u
.p
.current_unit
->has_size
)
629 dtp
->u
.p
.current_unit
->size_used
+= (GFC_IO_INT
) *nbytes
;
635 /* Reads a block directly into application data space. This is for
636 unformatted files. */
639 read_block_direct (st_parameter_dt
*dtp
, void *buf
, size_t nbytes
)
641 ssize_t to_read_record
;
642 ssize_t have_read_record
;
643 ssize_t to_read_subrecord
;
644 ssize_t have_read_subrecord
;
647 if (is_stream_io (dtp
))
649 have_read_record
= sread (dtp
->u
.p
.current_unit
->s
, buf
,
651 if (unlikely (have_read_record
< 0))
653 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
657 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) have_read_record
;
659 if (unlikely ((ssize_t
) nbytes
!= have_read_record
))
661 /* Short read, e.g. if we hit EOF. For stream files,
662 we have to set the end-of-file condition. */
668 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
670 if (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) nbytes
)
673 to_read_record
= dtp
->u
.p
.current_unit
->bytes_left
;
674 nbytes
= to_read_record
;
679 to_read_record
= nbytes
;
682 dtp
->u
.p
.current_unit
->bytes_left
-= to_read_record
;
684 to_read_record
= sread (dtp
->u
.p
.current_unit
->s
, buf
, to_read_record
);
685 if (unlikely (to_read_record
< 0))
687 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
691 if (to_read_record
!= (ssize_t
) nbytes
)
693 /* Short read, e.g. if we hit EOF. Apparently, we read
694 more than was written to the last record. */
698 if (unlikely (short_record
))
700 generate_error (&dtp
->common
, LIBERROR_SHORT_RECORD
, NULL
);
705 /* Unformatted sequential. We loop over the subrecords, reading
706 until the request has been fulfilled or the record has run out
707 of continuation subrecords. */
709 /* Check whether we exceed the total record length. */
711 if (dtp
->u
.p
.current_unit
->flags
.has_recl
712 && ((gfc_offset
) nbytes
> dtp
->u
.p
.current_unit
->bytes_left
))
714 to_read_record
= dtp
->u
.p
.current_unit
->bytes_left
;
719 to_read_record
= nbytes
;
722 have_read_record
= 0;
726 if (dtp
->u
.p
.current_unit
->bytes_left_subrecord
727 < (gfc_offset
) to_read_record
)
729 to_read_subrecord
= dtp
->u
.p
.current_unit
->bytes_left_subrecord
;
730 to_read_record
-= to_read_subrecord
;
734 to_read_subrecord
= to_read_record
;
738 dtp
->u
.p
.current_unit
->bytes_left_subrecord
-= to_read_subrecord
;
740 have_read_subrecord
= sread (dtp
->u
.p
.current_unit
->s
,
741 buf
+ have_read_record
, to_read_subrecord
);
742 if (unlikely (have_read_subrecord
< 0))
744 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
748 have_read_record
+= have_read_subrecord
;
750 if (unlikely (to_read_subrecord
!= have_read_subrecord
))
752 /* Short read, e.g. if we hit EOF. This means the record
753 structure has been corrupted, or the trailing record
754 marker would still be present. */
756 generate_error (&dtp
->common
, LIBERROR_CORRUPT_FILE
, NULL
);
760 if (to_read_record
> 0)
762 if (likely (dtp
->u
.p
.current_unit
->continued
))
764 next_record_r_unf (dtp
, 0);
769 /* Let's make sure the file position is correctly pre-positioned
770 for the next read statement. */
772 dtp
->u
.p
.current_unit
->current_record
= 0;
773 next_record_r_unf (dtp
, 0);
774 generate_error (&dtp
->common
, LIBERROR_SHORT_RECORD
, NULL
);
780 /* Normal exit, the read request has been fulfilled. */
785 dtp
->u
.p
.current_unit
->bytes_left
-= have_read_record
;
786 if (unlikely (short_record
))
788 generate_error (&dtp
->common
, LIBERROR_SHORT_RECORD
, NULL
);
795 /* Function for writing a block of bytes to the current file at the
796 current position, advancing the file pointer. We are given a length
797 and return a pointer to a buffer that the caller must (completely)
798 fill in. Returns NULL on error. */
801 write_block (st_parameter_dt
*dtp
, size_t length
)
805 if (!is_stream_io (dtp
))
807 if (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) length
)
809 /* For preconnected units with default record length, set bytes left
810 to unit record length and proceed, otherwise error. */
811 if (likely ((dtp
->u
.p
.current_unit
->unit_number
812 == options
.stdout_unit
813 || dtp
->u
.p
.current_unit
->unit_number
814 == options
.stderr_unit
)
815 && dtp
->u
.p
.current_unit
->recl
== default_recl
))
816 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
819 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
824 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) length
;
827 if (is_internal_unit (dtp
))
829 if (is_char4_unit(dtp
)) /* char4 internel unit. */
832 dest4
= mem_alloc_w4 (dtp
->u
.p
.current_unit
->s
, &length
);
835 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
841 dest
= mem_alloc_w (dtp
->u
.p
.current_unit
->s
, &length
);
845 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
849 if (unlikely (dtp
->u
.p
.current_unit
->endfile
== AT_ENDFILE
))
850 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
854 dest
= fbuf_alloc (dtp
->u
.p
.current_unit
, length
);
857 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
862 if (((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0) ||
863 dtp
->u
.p
.current_unit
->has_size
)
864 dtp
->u
.p
.current_unit
->size_used
+= (GFC_IO_INT
) length
;
866 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) length
;
872 /* High level interface to swrite(), taking care of errors. This is only
873 called for unformatted files. There are three cases to consider:
874 Stream I/O, unformatted direct, unformatted sequential. */
877 write_buf (st_parameter_dt
*dtp
, void *buf
, size_t nbytes
)
880 ssize_t have_written
;
881 ssize_t to_write_subrecord
;
886 if (is_stream_io (dtp
))
888 have_written
= swrite (dtp
->u
.p
.current_unit
->s
, buf
, nbytes
);
889 if (unlikely (have_written
< 0))
891 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
895 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) have_written
;
900 /* Unformatted direct access. */
902 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
904 if (unlikely (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) nbytes
))
906 generate_error (&dtp
->common
, LIBERROR_DIRECT_EOR
, NULL
);
910 if (buf
== NULL
&& nbytes
== 0)
913 have_written
= swrite (dtp
->u
.p
.current_unit
->s
, buf
, nbytes
);
914 if (unlikely (have_written
< 0))
916 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
920 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) have_written
;
921 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) have_written
;
926 /* Unformatted sequential. */
930 if (dtp
->u
.p
.current_unit
->flags
.has_recl
931 && (gfc_offset
) nbytes
> dtp
->u
.p
.current_unit
->bytes_left
)
933 nbytes
= dtp
->u
.p
.current_unit
->bytes_left
;
945 (size_t) dtp
->u
.p
.current_unit
->bytes_left_subrecord
< nbytes
?
946 (size_t) dtp
->u
.p
.current_unit
->bytes_left_subrecord
: nbytes
;
948 dtp
->u
.p
.current_unit
->bytes_left_subrecord
-=
949 (gfc_offset
) to_write_subrecord
;
951 to_write_subrecord
= swrite (dtp
->u
.p
.current_unit
->s
,
952 buf
+ have_written
, to_write_subrecord
);
953 if (unlikely (to_write_subrecord
< 0))
955 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
959 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) to_write_subrecord
;
960 nbytes
-= to_write_subrecord
;
961 have_written
+= to_write_subrecord
;
966 next_record_w_unf (dtp
, 1);
969 dtp
->u
.p
.current_unit
->bytes_left
-= have_written
;
970 if (unlikely (short_record
))
972 generate_error (&dtp
->common
, LIBERROR_SHORT_RECORD
, NULL
);
979 /* Reverse memcpy - used for byte swapping. */
982 reverse_memcpy (void *dest
, const void *src
, size_t n
)
988 s
= (char *) src
+ n
- 1;
990 /* Write with ascending order - this is likely faster
991 on modern architectures because of write combining. */
997 /* Utility function for byteswapping an array, using the bswap
998 builtins if possible. dest and src can overlap completely, or then
999 they must point to separate objects; partial overlaps are not
1003 bswap_array (void *dest
, const void *src
, size_t size
, size_t nelems
)
1013 for (size_t i
= 0; i
< nelems
; i
++)
1014 ((uint16_t*)dest
)[i
] = __builtin_bswap16 (((uint16_t*)src
)[i
]);
1017 for (size_t i
= 0; i
< nelems
; i
++)
1018 ((uint32_t*)dest
)[i
] = __builtin_bswap32 (((uint32_t*)src
)[i
]);
1021 for (size_t i
= 0; i
< nelems
; i
++)
1022 ((uint64_t*)dest
)[i
] = __builtin_bswap64 (((uint64_t*)src
)[i
]);
1027 for (size_t i
= 0; i
< nelems
; i
++)
1030 memcpy (&tmp
, ps
, 4);
1031 *(uint32_t*)pd
= __builtin_bswap32 (*(uint32_t*)(ps
+ 8));
1032 *(uint32_t*)(pd
+ 4) = __builtin_bswap32 (*(uint32_t*)(ps
+ 4));
1033 *(uint32_t*)(pd
+ 8) = __builtin_bswap32 (tmp
);
1041 for (size_t i
= 0; i
< nelems
; i
++)
1044 memcpy (&tmp
, ps
, 8);
1045 *(uint64_t*)pd
= __builtin_bswap64 (*(uint64_t*)(ps
+ 8));
1046 *(uint64_t*)(pd
+ 8) = __builtin_bswap64 (tmp
);
1056 for (size_t i
= 0; i
< nelems
; i
++)
1058 reverse_memcpy (pd
, ps
, size
);
1065 /* In-place byte swap. */
1066 for (size_t i
= 0; i
< nelems
; i
++)
1068 char tmp
, *low
= pd
, *high
= pd
+ size
- 1;
1069 for (size_t j
= 0; j
< size
/2; j
++)
1084 /* Master function for unformatted reads. */
1087 unformatted_read (st_parameter_dt
*dtp
, bt type
,
1088 void *dest
, int kind
, size_t size
, size_t nelems
)
1090 if (type
== BT_CLASS
)
1092 int unit
= dtp
->u
.p
.current_unit
->unit_number
;
1093 char tmp_iomsg
[IOMSG_LEN
] = "";
1095 gfc_charlen_type child_iomsg_len
;
1097 int *child_iostat
= NULL
;
1099 /* Set iostat, intent(out). */
1101 child_iostat
= (dtp
->common
.flags
& IOPARM_HAS_IOSTAT
) ?
1102 dtp
->common
.iostat
: &noiostat
;
1104 /* Set iomsg, intent(inout). */
1105 if (dtp
->common
.flags
& IOPARM_HAS_IOMSG
)
1107 child_iomsg
= dtp
->common
.iomsg
;
1108 child_iomsg_len
= dtp
->common
.iomsg_len
;
1112 child_iomsg
= tmp_iomsg
;
1113 child_iomsg_len
= IOMSG_LEN
;
1116 /* Call the user defined unformatted READ procedure. */
1117 dtp
->u
.p
.current_unit
->child_dtio
++;
1118 dtp
->u
.p
.ufdtio_ptr (dest
, &unit
, child_iostat
, child_iomsg
,
1120 dtp
->u
.p
.current_unit
->child_dtio
--;
1124 if (type
== BT_CHARACTER
)
1125 size
*= GFC_SIZE_OF_CHAR_KIND(kind
);
1126 read_block_direct (dtp
, dest
, size
* nelems
);
1128 if (unlikely (dtp
->u
.p
.current_unit
->flags
.convert
== GFC_CONVERT_SWAP
)
1131 /* Handle wide chracters. */
1132 if (type
== BT_CHARACTER
)
1138 /* Break up complex into its constituent reals. */
1139 else if (type
== BT_COMPLEX
)
1144 bswap_array (dest
, dest
, size
, nelems
);
1149 /* Master function for unformatted writes. NOTE: For kind=10 the size is 16
1150 bytes on 64 bit machines. The unused bytes are not initialized and never
1151 used, which can show an error with memory checking analyzers like
1152 valgrind. We us BT_CLASS to denote a User Defined I/O call. */
1155 unformatted_write (st_parameter_dt
*dtp
, bt type
,
1156 void *source
, int kind
, size_t size
, size_t nelems
)
1158 if (type
== BT_CLASS
)
1160 int unit
= dtp
->u
.p
.current_unit
->unit_number
;
1161 char tmp_iomsg
[IOMSG_LEN
] = "";
1163 gfc_charlen_type child_iomsg_len
;
1165 int *child_iostat
= NULL
;
1167 /* Set iostat, intent(out). */
1169 child_iostat
= (dtp
->common
.flags
& IOPARM_HAS_IOSTAT
) ?
1170 dtp
->common
.iostat
: &noiostat
;
1172 /* Set iomsg, intent(inout). */
1173 if (dtp
->common
.flags
& IOPARM_HAS_IOMSG
)
1175 child_iomsg
= dtp
->common
.iomsg
;
1176 child_iomsg_len
= dtp
->common
.iomsg_len
;
1180 child_iomsg
= tmp_iomsg
;
1181 child_iomsg_len
= IOMSG_LEN
;
1184 /* Call the user defined unformatted WRITE procedure. */
1185 dtp
->u
.p
.current_unit
->child_dtio
++;
1186 dtp
->u
.p
.ufdtio_ptr (source
, &unit
, child_iostat
, child_iomsg
,
1188 dtp
->u
.p
.current_unit
->child_dtio
--;
1192 if (likely (dtp
->u
.p
.current_unit
->flags
.convert
== GFC_CONVERT_NATIVE
)
1195 size_t stride
= type
== BT_CHARACTER
?
1196 size
* GFC_SIZE_OF_CHAR_KIND(kind
) : size
;
1198 write_buf (dtp
, source
, stride
* nelems
);
1202 #define BSWAP_BUFSZ 512
1203 char buffer
[BSWAP_BUFSZ
];
1209 /* Handle wide chracters. */
1210 if (type
== BT_CHARACTER
&& kind
!= 1)
1216 /* Break up complex into its constituent reals. */
1217 if (type
== BT_COMPLEX
)
1223 /* By now, all complex variables have been split into their
1224 constituent reals. */
1230 if (size
* nrem
> BSWAP_BUFSZ
)
1231 nc
= BSWAP_BUFSZ
/ size
;
1235 bswap_array (buffer
, p
, size
, nc
);
1236 write_buf (dtp
, buffer
, size
* nc
);
1245 /* Return a pointer to the name of a type. */
1270 p
= "CLASS or DERIVED";
1273 internal_error (NULL
, "type_name(): Bad type");
1280 /* Write a constant string to the output.
1281 This is complicated because the string can have doubled delimiters
1282 in it. The length in the format node is the true length. */
1285 write_constant_string (st_parameter_dt
*dtp
, const fnode
*f
)
1287 char c
, delimiter
, *p
, *q
;
1290 length
= f
->u
.string
.length
;
1294 p
= write_block (dtp
, length
);
1301 for (; length
> 0; length
--)
1304 if (c
== delimiter
&& c
!= 'H' && c
!= 'h')
1305 q
++; /* Skip the doubled delimiter. */
1310 /* Given actual and expected types in a formatted data transfer, make
1311 sure they agree. If not, an error message is generated. Returns
1312 nonzero if something went wrong. */
1315 require_type (st_parameter_dt
*dtp
, bt expected
, bt actual
, const fnode
*f
)
1318 char buffer
[BUFLEN
];
1320 if (actual
== expected
)
1323 /* Adjust item_count before emitting error message. */
1324 snprintf (buffer
, BUFLEN
,
1325 "Expected %s for item %d in formatted transfer, got %s",
1326 type_name (expected
), dtp
->u
.p
.item_count
- 1, type_name (actual
));
1328 format_error (dtp
, f
, buffer
);
1333 /* Check that the dtio procedure required for formatted IO is present. */
1336 check_dtio_proc (st_parameter_dt
*dtp
, const fnode
*f
)
1338 char buffer
[BUFLEN
];
1340 if (dtp
->u
.p
.fdtio_ptr
!= NULL
)
1343 snprintf (buffer
, BUFLEN
,
1344 "Missing DTIO procedure or intrinsic type passed for item %d "
1345 "in formatted transfer",
1346 dtp
->u
.p
.item_count
- 1);
1348 format_error (dtp
, f
, buffer
);
1354 require_numeric_type (st_parameter_dt
*dtp
, bt actual
, const fnode
*f
)
1357 char buffer
[BUFLEN
];
1359 if (actual
== BT_INTEGER
|| actual
== BT_REAL
|| actual
== BT_COMPLEX
)
1362 /* Adjust item_count before emitting error message. */
1363 snprintf (buffer
, BUFLEN
,
1364 "Expected numeric type for item %d in formatted transfer, got %s",
1365 dtp
->u
.p
.item_count
- 1, type_name (actual
));
1367 format_error (dtp
, f
, buffer
);
1372 get_dt_format (char *p
, gfc_charlen_type
*length
)
1374 char delim
= p
[-1]; /* The delimiter is always the first character back. */
1376 gfc_charlen_type len
= *length
; /* This length already correct, less 'DT'. */
1378 res
= q
= xmalloc (len
+ 2);
1380 /* Set the beginning of the string to 'DT', length adjusted below. */
1384 /* The string may contain doubled quotes so scan and skip as needed. */
1385 for (; len
> 0; len
--)
1389 p
++; /* Skip the doubled delimiter. */
1392 /* Adjust the string length by two now that we are done. */
1399 /* This function is in the main loop for a formatted data transfer
1400 statement. It would be natural to implement this as a coroutine
1401 with the user program, but C makes that awkward. We loop,
1402 processing format elements. When we actually have to transfer
1403 data instead of just setting flags, we return control to the user
1404 program which calls a function that supplies the address and type
1405 of the next element, then comes back here to process it. */
1408 formatted_transfer_scalar_read (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
1411 int pos
, bytes_used
;
1415 int consume_data_flag
;
1417 /* Change a complex data item into a pair of reals. */
1419 n
= (p
== NULL
) ? 0 : ((type
!= BT_COMPLEX
) ? 1 : 2);
1420 if (type
== BT_COMPLEX
)
1426 /* If there's an EOR condition, we simulate finalizing the transfer
1427 by doing nothing. */
1428 if (dtp
->u
.p
.eor_condition
)
1431 /* Set this flag so that commas in reads cause the read to complete before
1432 the entire field has been read. The next read field will start right after
1433 the comma in the stream. (Set to 0 for character reads). */
1434 dtp
->u
.p
.sf_read_comma
=
1435 dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
? 0 : 1;
1439 /* If reversion has occurred and there is another real data item,
1440 then we have to move to the next record. */
1441 if (dtp
->u
.p
.reversion_flag
&& n
> 0)
1443 dtp
->u
.p
.reversion_flag
= 0;
1444 next_record (dtp
, 0);
1447 consume_data_flag
= 1;
1448 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1451 f
= next_format (dtp
);
1454 /* No data descriptors left. */
1455 if (unlikely (n
> 0))
1456 generate_error (&dtp
->common
, LIBERROR_FORMAT
,
1457 "Insufficient data descriptors in format after reversion");
1463 bytes_used
= (int)(dtp
->u
.p
.current_unit
->recl
1464 - dtp
->u
.p
.current_unit
->bytes_left
);
1466 if (is_stream_io(dtp
))
1473 goto need_read_data
;
1474 if (require_type (dtp
, BT_INTEGER
, type
, f
))
1476 read_decimal (dtp
, f
, p
, kind
);
1481 goto need_read_data
;
1482 if (!(compile_options
.allow_std
& GFC_STD_GNU
)
1483 && require_numeric_type (dtp
, type
, f
))
1485 if (!(compile_options
.allow_std
& GFC_STD_F2008
)
1486 && require_type (dtp
, BT_INTEGER
, type
, f
))
1488 read_radix (dtp
, f
, p
, kind
, 2);
1493 goto need_read_data
;
1494 if (!(compile_options
.allow_std
& GFC_STD_GNU
)
1495 && require_numeric_type (dtp
, type
, f
))
1497 if (!(compile_options
.allow_std
& GFC_STD_F2008
)
1498 && require_type (dtp
, BT_INTEGER
, type
, f
))
1500 read_radix (dtp
, f
, p
, kind
, 8);
1505 goto need_read_data
;
1506 if (!(compile_options
.allow_std
& GFC_STD_GNU
)
1507 && require_numeric_type (dtp
, type
, f
))
1509 if (!(compile_options
.allow_std
& GFC_STD_F2008
)
1510 && require_type (dtp
, BT_INTEGER
, type
, f
))
1512 read_radix (dtp
, f
, p
, kind
, 16);
1517 goto need_read_data
;
1519 /* It is possible to have FMT_A with something not BT_CHARACTER such
1520 as when writing out hollerith strings, so check both type
1521 and kind before calling wide character routines. */
1522 if (type
== BT_CHARACTER
&& kind
== 4)
1523 read_a_char4 (dtp
, f
, p
, size
);
1525 read_a (dtp
, f
, p
, size
);
1530 goto need_read_data
;
1531 read_l (dtp
, f
, p
, kind
);
1536 goto need_read_data
;
1537 if (require_type (dtp
, BT_REAL
, type
, f
))
1539 read_f (dtp
, f
, p
, kind
);
1544 goto need_read_data
;
1546 if (check_dtio_proc (dtp
, f
))
1548 if (require_type (dtp
, BT_CLASS
, type
, f
))
1550 int unit
= dtp
->u
.p
.current_unit
->unit_number
;
1552 char tmp_iomsg
[IOMSG_LEN
] = "";
1554 gfc_charlen_type child_iomsg_len
;
1556 int *child_iostat
= NULL
;
1558 gfc_charlen_type iotype_len
= f
->u
.udf
.string_len
;
1560 /* Build the iotype string. */
1561 if (iotype_len
== 0)
1567 iotype
= get_dt_format (f
->u
.udf
.string
, &iotype_len
);
1569 /* Set iostat, intent(out). */
1571 child_iostat
= (dtp
->common
.flags
& IOPARM_HAS_IOSTAT
) ?
1572 dtp
->common
.iostat
: &noiostat
;
1574 /* Set iomsg, intent(inout). */
1575 if (dtp
->common
.flags
& IOPARM_HAS_IOMSG
)
1577 child_iomsg
= dtp
->common
.iomsg
;
1578 child_iomsg_len
= dtp
->common
.iomsg_len
;
1582 child_iomsg
= tmp_iomsg
;
1583 child_iomsg_len
= IOMSG_LEN
;
1586 /* Call the user defined formatted READ procedure. */
1587 dtp
->u
.p
.current_unit
->child_dtio
++;
1588 dtp
->u
.p
.current_unit
->last_char
= EOF
- 1;
1589 dtp
->u
.p
.fdtio_ptr (p
, &unit
, iotype
, f
->u
.udf
.vlist
,
1590 child_iostat
, child_iomsg
,
1591 iotype_len
, child_iomsg_len
);
1592 dtp
->u
.p
.current_unit
->child_dtio
--;
1594 if (f
->u
.udf
.string_len
!= 0)
1596 /* Note: vlist is freed in free_format_data. */
1601 goto need_read_data
;
1602 if (require_type (dtp
, BT_REAL
, type
, f
))
1604 read_f (dtp
, f
, p
, kind
);
1609 goto need_read_data
;
1610 if (require_type (dtp
, BT_REAL
, type
, f
))
1612 read_f (dtp
, f
, p
, kind
);
1617 goto need_read_data
;
1618 if (require_type (dtp
, BT_REAL
, type
, f
))
1620 read_f (dtp
, f
, p
, kind
);
1625 goto need_read_data
;
1626 if (require_type (dtp
, BT_REAL
, type
, f
))
1628 read_f (dtp
, f
, p
, kind
);
1633 goto need_read_data
;
1637 read_decimal (dtp
, f
, p
, kind
);
1640 read_l (dtp
, f
, p
, kind
);
1644 read_a_char4 (dtp
, f
, p
, size
);
1646 read_a (dtp
, f
, p
, size
);
1649 read_f (dtp
, f
, p
, kind
);
1652 internal_error (&dtp
->common
,
1653 "formatted_transfer (): Bad type");
1658 consume_data_flag
= 0;
1659 format_error (dtp
, f
, "Constant string in input format");
1662 /* Format codes that don't transfer data. */
1665 consume_data_flag
= 0;
1666 dtp
->u
.p
.skips
+= f
->u
.n
;
1667 pos
= bytes_used
+ dtp
->u
.p
.skips
- 1;
1668 dtp
->u
.p
.pending_spaces
= pos
- dtp
->u
.p
.max_pos
+ 1;
1669 read_x (dtp
, f
->u
.n
);
1674 consume_data_flag
= 0;
1676 if (f
->format
== FMT_TL
)
1678 /* Handle the special case when no bytes have been used yet.
1679 Cannot go below zero. */
1680 if (bytes_used
== 0)
1682 dtp
->u
.p
.pending_spaces
-= f
->u
.n
;
1683 dtp
->u
.p
.skips
-= f
->u
.n
;
1684 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
< 0 ? 0 : dtp
->u
.p
.skips
;
1687 pos
= bytes_used
- f
->u
.n
;
1692 /* Standard 10.6.1.1: excessive left tabbing is reset to the
1693 left tab limit. We do not check if the position has gone
1694 beyond the end of record because a subsequent tab could
1695 bring us back again. */
1696 pos
= pos
< 0 ? 0 : pos
;
1698 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
+ pos
- bytes_used
;
1699 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
1700 + pos
- dtp
->u
.p
.max_pos
;
1701 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
< 0
1702 ? 0 : dtp
->u
.p
.pending_spaces
;
1703 if (dtp
->u
.p
.skips
== 0)
1706 /* Adjust everything for end-of-record condition */
1707 if (dtp
->u
.p
.sf_seen_eor
&& !is_internal_unit (dtp
))
1709 dtp
->u
.p
.current_unit
->bytes_left
-= dtp
->u
.p
.sf_seen_eor
;
1710 dtp
->u
.p
.skips
-= dtp
->u
.p
.sf_seen_eor
;
1712 if (dtp
->u
.p
.pending_spaces
== 0)
1713 dtp
->u
.p
.sf_seen_eor
= 0;
1715 if (dtp
->u
.p
.skips
< 0)
1717 if (is_internal_unit (dtp
))
1718 sseek (dtp
->u
.p
.current_unit
->s
, dtp
->u
.p
.skips
, SEEK_CUR
);
1720 fbuf_seek (dtp
->u
.p
.current_unit
, dtp
->u
.p
.skips
, SEEK_CUR
);
1721 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) dtp
->u
.p
.skips
;
1722 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1725 read_x (dtp
, dtp
->u
.p
.skips
);
1729 consume_data_flag
= 0;
1730 dtp
->u
.p
.sign_status
= SIGN_S
;
1734 consume_data_flag
= 0;
1735 dtp
->u
.p
.sign_status
= SIGN_SS
;
1739 consume_data_flag
= 0;
1740 dtp
->u
.p
.sign_status
= SIGN_SP
;
1744 consume_data_flag
= 0 ;
1745 dtp
->u
.p
.blank_status
= BLANK_NULL
;
1749 consume_data_flag
= 0;
1750 dtp
->u
.p
.blank_status
= BLANK_ZERO
;
1754 consume_data_flag
= 0;
1755 dtp
->u
.p
.current_unit
->decimal_status
= DECIMAL_COMMA
;
1759 consume_data_flag
= 0;
1760 dtp
->u
.p
.current_unit
->decimal_status
= DECIMAL_POINT
;
1764 consume_data_flag
= 0;
1765 dtp
->u
.p
.current_unit
->round_status
= ROUND_COMPATIBLE
;
1769 consume_data_flag
= 0;
1770 dtp
->u
.p
.current_unit
->round_status
= ROUND_DOWN
;
1774 consume_data_flag
= 0;
1775 dtp
->u
.p
.current_unit
->round_status
= ROUND_NEAREST
;
1779 consume_data_flag
= 0;
1780 dtp
->u
.p
.current_unit
->round_status
= ROUND_PROCDEFINED
;
1784 consume_data_flag
= 0;
1785 dtp
->u
.p
.current_unit
->round_status
= ROUND_UP
;
1789 consume_data_flag
= 0;
1790 dtp
->u
.p
.current_unit
->round_status
= ROUND_ZERO
;
1794 consume_data_flag
= 0;
1795 dtp
->u
.p
.scale_factor
= f
->u
.k
;
1799 consume_data_flag
= 0;
1800 dtp
->u
.p
.seen_dollar
= 1;
1804 consume_data_flag
= 0;
1805 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1806 next_record (dtp
, 0);
1810 /* A colon descriptor causes us to exit this loop (in
1811 particular preventing another / descriptor from being
1812 processed) unless there is another data item to be
1814 consume_data_flag
= 0;
1820 internal_error (&dtp
->common
, "Bad format node");
1823 /* Adjust the item count and data pointer. */
1825 if ((consume_data_flag
> 0) && (n
> 0))
1828 p
= ((char *) p
) + size
;
1833 pos
= (int)(dtp
->u
.p
.current_unit
->recl
- dtp
->u
.p
.current_unit
->bytes_left
);
1834 dtp
->u
.p
.max_pos
= (dtp
->u
.p
.max_pos
> pos
) ? dtp
->u
.p
.max_pos
: pos
;
1839 /* Come here when we need a data descriptor but don't have one. We
1840 push the current format node back onto the input, then return and
1841 let the user program call us back with the data. */
1843 unget_format (dtp
, f
);
1848 formatted_transfer_scalar_write (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
1851 gfc_offset pos
, bytes_used
;
1855 int consume_data_flag
;
1857 /* Change a complex data item into a pair of reals. */
1859 n
= (p
== NULL
) ? 0 : ((type
!= BT_COMPLEX
) ? 1 : 2);
1860 if (type
== BT_COMPLEX
)
1866 /* If there's an EOR condition, we simulate finalizing the transfer
1867 by doing nothing. */
1868 if (dtp
->u
.p
.eor_condition
)
1871 /* Set this flag so that commas in reads cause the read to complete before
1872 the entire field has been read. The next read field will start right after
1873 the comma in the stream. (Set to 0 for character reads). */
1874 dtp
->u
.p
.sf_read_comma
=
1875 dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
? 0 : 1;
1879 /* If reversion has occurred and there is another real data item,
1880 then we have to move to the next record. */
1881 if (dtp
->u
.p
.reversion_flag
&& n
> 0)
1883 dtp
->u
.p
.reversion_flag
= 0;
1884 next_record (dtp
, 0);
1887 consume_data_flag
= 1;
1888 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1891 f
= next_format (dtp
);
1894 /* No data descriptors left. */
1895 if (unlikely (n
> 0))
1896 generate_error (&dtp
->common
, LIBERROR_FORMAT
,
1897 "Insufficient data descriptors in format after reversion");
1901 /* Now discharge T, TR and X movements to the right. This is delayed
1902 until a data producing format to suppress trailing spaces. */
1905 if (dtp
->u
.p
.mode
== WRITING
&& dtp
->u
.p
.skips
!= 0
1906 && ((n
>0 && ( t
== FMT_I
|| t
== FMT_B
|| t
== FMT_O
1907 || t
== FMT_Z
|| t
== FMT_F
|| t
== FMT_E
1908 || t
== FMT_EN
|| t
== FMT_ES
|| t
== FMT_G
1909 || t
== FMT_L
|| t
== FMT_A
|| t
== FMT_D
1911 || t
== FMT_STRING
))
1913 if (dtp
->u
.p
.skips
> 0)
1916 write_x (dtp
, dtp
->u
.p
.skips
, dtp
->u
.p
.pending_spaces
);
1917 tmp
= dtp
->u
.p
.current_unit
->recl
1918 - dtp
->u
.p
.current_unit
->bytes_left
;
1920 dtp
->u
.p
.max_pos
> tmp
? dtp
->u
.p
.max_pos
: tmp
;
1923 if (dtp
->u
.p
.skips
< 0)
1925 if (is_internal_unit (dtp
))
1926 sseek (dtp
->u
.p
.current_unit
->s
, dtp
->u
.p
.skips
, SEEK_CUR
);
1928 fbuf_seek (dtp
->u
.p
.current_unit
, dtp
->u
.p
.skips
, SEEK_CUR
);
1929 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) dtp
->u
.p
.skips
;
1931 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1934 bytes_used
= dtp
->u
.p
.current_unit
->recl
1935 - dtp
->u
.p
.current_unit
->bytes_left
;
1937 if (is_stream_io(dtp
))
1945 if (require_type (dtp
, BT_INTEGER
, type
, f
))
1947 write_i (dtp
, f
, p
, kind
);
1953 if (!(compile_options
.allow_std
& GFC_STD_GNU
)
1954 && require_numeric_type (dtp
, type
, f
))
1956 if (!(compile_options
.allow_std
& GFC_STD_F2008
)
1957 && require_type (dtp
, BT_INTEGER
, type
, f
))
1959 write_b (dtp
, f
, p
, kind
);
1965 if (!(compile_options
.allow_std
& GFC_STD_GNU
)
1966 && require_numeric_type (dtp
, type
, f
))
1968 if (!(compile_options
.allow_std
& GFC_STD_F2008
)
1969 && require_type (dtp
, BT_INTEGER
, type
, f
))
1971 write_o (dtp
, f
, p
, kind
);
1977 if (!(compile_options
.allow_std
& GFC_STD_GNU
)
1978 && require_numeric_type (dtp
, type
, f
))
1980 if (!(compile_options
.allow_std
& GFC_STD_F2008
)
1981 && require_type (dtp
, BT_INTEGER
, type
, f
))
1983 write_z (dtp
, f
, p
, kind
);
1990 /* It is possible to have FMT_A with something not BT_CHARACTER such
1991 as when writing out hollerith strings, so check both type
1992 and kind before calling wide character routines. */
1993 if (type
== BT_CHARACTER
&& kind
== 4)
1994 write_a_char4 (dtp
, f
, p
, size
);
1996 write_a (dtp
, f
, p
, size
);
2002 write_l (dtp
, f
, p
, kind
);
2008 if (require_type (dtp
, BT_REAL
, type
, f
))
2010 write_d (dtp
, f
, p
, kind
);
2016 int unit
= dtp
->u
.p
.current_unit
->unit_number
;
2018 char tmp_iomsg
[IOMSG_LEN
] = "";
2020 gfc_charlen_type child_iomsg_len
;
2022 int *child_iostat
= NULL
;
2024 gfc_charlen_type iotype_len
= f
->u
.udf
.string_len
;
2026 /* Build the iotype string. */
2027 if (iotype_len
== 0)
2033 iotype
= get_dt_format (f
->u
.udf
.string
, &iotype_len
);
2035 /* Set iostat, intent(out). */
2037 child_iostat
= (dtp
->common
.flags
& IOPARM_HAS_IOSTAT
) ?
2038 dtp
->common
.iostat
: &noiostat
;
2040 /* Set iomsg, intent(inout). */
2041 if (dtp
->common
.flags
& IOPARM_HAS_IOMSG
)
2043 child_iomsg
= dtp
->common
.iomsg
;
2044 child_iomsg_len
= dtp
->common
.iomsg_len
;
2048 child_iomsg
= tmp_iomsg
;
2049 child_iomsg_len
= IOMSG_LEN
;
2052 if (check_dtio_proc (dtp
, f
))
2055 /* Call the user defined formatted WRITE procedure. */
2056 dtp
->u
.p
.current_unit
->child_dtio
++;
2058 dtp
->u
.p
.fdtio_ptr (p
, &unit
, iotype
, f
->u
.udf
.vlist
,
2059 child_iostat
, child_iomsg
,
2060 iotype_len
, child_iomsg_len
);
2061 dtp
->u
.p
.current_unit
->child_dtio
--;
2063 if (f
->u
.udf
.string_len
!= 0)
2065 /* Note: vlist is freed in free_format_data. */
2071 if (require_type (dtp
, BT_REAL
, type
, f
))
2073 write_e (dtp
, f
, p
, kind
);
2079 if (require_type (dtp
, BT_REAL
, type
, f
))
2081 write_en (dtp
, f
, p
, kind
);
2087 if (require_type (dtp
, BT_REAL
, type
, f
))
2089 write_es (dtp
, f
, p
, kind
);
2095 if (require_type (dtp
, BT_REAL
, type
, f
))
2097 write_f (dtp
, f
, p
, kind
);
2106 write_i (dtp
, f
, p
, kind
);
2109 write_l (dtp
, f
, p
, kind
);
2113 write_a_char4 (dtp
, f
, p
, size
);
2115 write_a (dtp
, f
, p
, size
);
2118 if (f
->u
.real
.w
== 0)
2119 write_real_g0 (dtp
, p
, kind
, f
->u
.real
.d
);
2121 write_d (dtp
, f
, p
, kind
);
2124 internal_error (&dtp
->common
,
2125 "formatted_transfer (): Bad type");
2130 consume_data_flag
= 0;
2131 write_constant_string (dtp
, f
);
2134 /* Format codes that don't transfer data. */
2137 consume_data_flag
= 0;
2139 dtp
->u
.p
.skips
+= f
->u
.n
;
2140 pos
= bytes_used
+ dtp
->u
.p
.skips
- 1;
2141 dtp
->u
.p
.pending_spaces
= pos
- dtp
->u
.p
.max_pos
+ 1;
2142 /* Writes occur just before the switch on f->format, above, so
2143 that trailing blanks are suppressed, unless we are doing a
2144 non-advancing write in which case we want to output the blanks
2146 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
)
2148 write_x (dtp
, dtp
->u
.p
.skips
, dtp
->u
.p
.pending_spaces
);
2149 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
2155 consume_data_flag
= 0;
2157 if (f
->format
== FMT_TL
)
2160 /* Handle the special case when no bytes have been used yet.
2161 Cannot go below zero. */
2162 if (bytes_used
== 0)
2164 dtp
->u
.p
.pending_spaces
-= f
->u
.n
;
2165 dtp
->u
.p
.skips
-= f
->u
.n
;
2166 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
< 0 ? 0 : dtp
->u
.p
.skips
;
2169 pos
= bytes_used
- f
->u
.n
;
2172 pos
= f
->u
.n
- dtp
->u
.p
.pending_spaces
- 1;
2174 /* Standard 10.6.1.1: excessive left tabbing is reset to the
2175 left tab limit. We do not check if the position has gone
2176 beyond the end of record because a subsequent tab could
2177 bring us back again. */
2178 pos
= pos
< 0 ? 0 : pos
;
2180 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
+ pos
- bytes_used
;
2181 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
2182 + pos
- dtp
->u
.p
.max_pos
;
2183 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
< 0
2184 ? 0 : dtp
->u
.p
.pending_spaces
;
2188 consume_data_flag
= 0;
2189 dtp
->u
.p
.sign_status
= SIGN_S
;
2193 consume_data_flag
= 0;
2194 dtp
->u
.p
.sign_status
= SIGN_SS
;
2198 consume_data_flag
= 0;
2199 dtp
->u
.p
.sign_status
= SIGN_SP
;
2203 consume_data_flag
= 0 ;
2204 dtp
->u
.p
.blank_status
= BLANK_NULL
;
2208 consume_data_flag
= 0;
2209 dtp
->u
.p
.blank_status
= BLANK_ZERO
;
2213 consume_data_flag
= 0;
2214 dtp
->u
.p
.current_unit
->decimal_status
= DECIMAL_COMMA
;
2218 consume_data_flag
= 0;
2219 dtp
->u
.p
.current_unit
->decimal_status
= DECIMAL_POINT
;
2223 consume_data_flag
= 0;
2224 dtp
->u
.p
.current_unit
->round_status
= ROUND_COMPATIBLE
;
2228 consume_data_flag
= 0;
2229 dtp
->u
.p
.current_unit
->round_status
= ROUND_DOWN
;
2233 consume_data_flag
= 0;
2234 dtp
->u
.p
.current_unit
->round_status
= ROUND_NEAREST
;
2238 consume_data_flag
= 0;
2239 dtp
->u
.p
.current_unit
->round_status
= ROUND_PROCDEFINED
;
2243 consume_data_flag
= 0;
2244 dtp
->u
.p
.current_unit
->round_status
= ROUND_UP
;
2248 consume_data_flag
= 0;
2249 dtp
->u
.p
.current_unit
->round_status
= ROUND_ZERO
;
2253 consume_data_flag
= 0;
2254 dtp
->u
.p
.scale_factor
= f
->u
.k
;
2258 consume_data_flag
= 0;
2259 dtp
->u
.p
.seen_dollar
= 1;
2263 consume_data_flag
= 0;
2264 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
2265 next_record (dtp
, 0);
2269 /* A colon descriptor causes us to exit this loop (in
2270 particular preventing another / descriptor from being
2271 processed) unless there is another data item to be
2273 consume_data_flag
= 0;
2279 internal_error (&dtp
->common
, "Bad format node");
2282 /* Adjust the item count and data pointer. */
2284 if ((consume_data_flag
> 0) && (n
> 0))
2287 p
= ((char *) p
) + size
;
2290 pos
= dtp
->u
.p
.current_unit
->recl
- dtp
->u
.p
.current_unit
->bytes_left
;
2291 dtp
->u
.p
.max_pos
= (dtp
->u
.p
.max_pos
> pos
) ? dtp
->u
.p
.max_pos
: pos
;
2296 /* Come here when we need a data descriptor but don't have one. We
2297 push the current format node back onto the input, then return and
2298 let the user program call us back with the data. */
2300 unget_format (dtp
, f
);
2303 /* This function is first called from data_init_transfer to initiate the loop
2304 over each item in the format, transferring data as required. Subsequent
2305 calls to this function occur for each data item foound in the READ/WRITE
2306 statement. The item_count is incremented for each call. Since the first
2307 call is from data_transfer_init, the item_count is always one greater than
2308 the actual count number of the item being transferred. */
2311 formatted_transfer (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
2312 size_t size
, size_t nelems
)
2318 size_t stride
= type
== BT_CHARACTER
?
2319 size
* GFC_SIZE_OF_CHAR_KIND(kind
) : size
;
2320 if (dtp
->u
.p
.mode
== READING
)
2322 /* Big loop over all the elements. */
2323 for (elem
= 0; elem
< nelems
; elem
++)
2325 dtp
->u
.p
.item_count
++;
2326 formatted_transfer_scalar_read (dtp
, type
, tmp
+ stride
*elem
, kind
, size
);
2331 /* Big loop over all the elements. */
2332 for (elem
= 0; elem
< nelems
; elem
++)
2334 dtp
->u
.p
.item_count
++;
2335 formatted_transfer_scalar_write (dtp
, type
, tmp
+ stride
*elem
, kind
, size
);
2340 /* Wrapper function for I/O of scalar types. If this should be an async I/O
2341 request, queue it. For a synchronous write on an async unit, perform the
2342 wait operation and return an error. For all synchronous writes, call the
2343 right transfer function. */
2346 wrap_scalar_transfer (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
2347 size_t size
, size_t n_elem
)
2349 if (dtp
->u
.p
.current_unit
&& dtp
->u
.p
.current_unit
->au
)
2354 args
.scalar
.transfer
= dtp
->u
.p
.transfer
;
2355 args
.scalar
.arg_bt
= type
;
2356 args
.scalar
.data
= p
;
2357 args
.scalar
.i
= kind
;
2358 args
.scalar
.s1
= size
;
2359 args
.scalar
.s2
= n_elem
;
2360 enqueue_transfer (dtp
->u
.p
.current_unit
->au
, &args
,
2361 AIO_TRANSFER_SCALAR
);
2365 /* Come here if there was no asynchronous I/O to be scheduled. */
2366 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
2369 dtp
->u
.p
.transfer (dtp
, type
, p
, kind
, size
, 1);
2373 /* Data transfer entry points. The type of the data entity is
2374 implicit in the subroutine call. This prevents us from having to
2375 share a common enum with the compiler. */
2378 transfer_integer (st_parameter_dt
*dtp
, void *p
, int kind
)
2380 wrap_scalar_transfer (dtp
, BT_INTEGER
, p
, kind
, kind
, 1);
2384 transfer_integer_write (st_parameter_dt
*dtp
, void *p
, int kind
)
2386 transfer_integer (dtp
, p
, kind
);
2390 transfer_real (st_parameter_dt
*dtp
, void *p
, int kind
)
2393 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
2395 size
= size_from_real_kind (kind
);
2396 wrap_scalar_transfer (dtp
, BT_REAL
, p
, kind
, size
, 1);
2400 transfer_real_write (st_parameter_dt
*dtp
, void *p
, int kind
)
2402 transfer_real (dtp
, p
, kind
);
2406 transfer_logical (st_parameter_dt
*dtp
, void *p
, int kind
)
2408 wrap_scalar_transfer (dtp
, BT_LOGICAL
, p
, kind
, kind
, 1);
2412 transfer_logical_write (st_parameter_dt
*dtp
, void *p
, int kind
)
2414 transfer_logical (dtp
, p
, kind
);
2418 transfer_character (st_parameter_dt
*dtp
, void *p
, gfc_charlen_type len
)
2420 static char *empty_string
[0];
2422 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
2425 /* Strings of zero length can have p == NULL, which confuses the
2426 transfer routines into thinking we need more data elements. To avoid
2427 this, we give them a nice pointer. */
2428 if (len
== 0 && p
== NULL
)
2431 /* Set kind here to 1. */
2432 wrap_scalar_transfer (dtp
, BT_CHARACTER
, p
, 1, len
, 1);
2436 transfer_character_write (st_parameter_dt
*dtp
, void *p
, gfc_charlen_type len
)
2438 transfer_character (dtp
, p
, len
);
2442 transfer_character_wide (st_parameter_dt
*dtp
, void *p
, gfc_charlen_type len
, int kind
)
2444 static char *empty_string
[0];
2446 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
2449 /* Strings of zero length can have p == NULL, which confuses the
2450 transfer routines into thinking we need more data elements. To avoid
2451 this, we give them a nice pointer. */
2452 if (len
== 0 && p
== NULL
)
2455 /* Here we pass the actual kind value. */
2456 wrap_scalar_transfer (dtp
, BT_CHARACTER
, p
, kind
, len
, 1);
2460 transfer_character_wide_write (st_parameter_dt
*dtp
, void *p
, gfc_charlen_type len
, int kind
)
2462 transfer_character_wide (dtp
, p
, len
, kind
);
2466 transfer_complex (st_parameter_dt
*dtp
, void *p
, int kind
)
2469 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
2471 size
= size_from_complex_kind (kind
);
2472 wrap_scalar_transfer (dtp
, BT_COMPLEX
, p
, kind
, size
, 1);
2476 transfer_complex_write (st_parameter_dt
*dtp
, void *p
, int kind
)
2478 transfer_complex (dtp
, p
, kind
);
2482 transfer_array_inner (st_parameter_dt
*dtp
, gfc_array_char
*desc
, int kind
,
2483 gfc_charlen_type charlen
)
2485 index_type count
[GFC_MAX_DIMENSIONS
];
2486 index_type extent
[GFC_MAX_DIMENSIONS
];
2487 index_type stride
[GFC_MAX_DIMENSIONS
];
2488 index_type stride0
, rank
, size
, n
;
2493 /* Adjust item_count before emitting error message. */
2495 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
2498 iotype
= (bt
) GFC_DESCRIPTOR_TYPE (desc
);
2499 size
= iotype
== BT_CHARACTER
? charlen
: GFC_DESCRIPTOR_SIZE (desc
);
2501 rank
= GFC_DESCRIPTOR_RANK (desc
);
2503 for (n
= 0; n
< rank
; n
++)
2506 stride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(desc
,n
);
2507 extent
[n
] = GFC_DESCRIPTOR_EXTENT(desc
,n
);
2509 /* If the extent of even one dimension is zero, then the entire
2510 array section contains zero elements, so we return after writing
2511 a zero array record. */
2516 dtp
->u
.p
.transfer (dtp
, iotype
, data
, kind
, size
, tsize
);
2521 stride0
= stride
[0];
2523 /* If the innermost dimension has a stride of 1, we can do the transfer
2524 in contiguous chunks. */
2525 if (stride0
== size
)
2530 data
= GFC_DESCRIPTOR_DATA (desc
);
2534 dtp
->u
.p
.transfer (dtp
, iotype
, data
, kind
, size
, tsize
);
2535 data
+= stride0
* tsize
;
2538 while (count
[n
] == extent
[n
])
2541 data
-= stride
[n
] * extent
[n
];
2558 transfer_array (st_parameter_dt
*dtp
, gfc_array_char
*desc
, int kind
,
2559 gfc_charlen_type charlen
)
2561 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
2564 if (dtp
->u
.p
.current_unit
&& dtp
->u
.p
.current_unit
->au
)
2569 size_t sz
= sizeof (gfc_array_char
)
2570 + sizeof (descriptor_dimension
)
2571 * GFC_DESCRIPTOR_RANK (desc
);
2572 args
.array
.desc
= xmalloc (sz
);
2573 NOTE ("desc = %p", (void *) args
.array
.desc
);
2574 memcpy (args
.array
.desc
, desc
, sz
);
2575 args
.array
.kind
= kind
;
2576 args
.array
.charlen
= charlen
;
2577 enqueue_transfer (dtp
->u
.p
.current_unit
->au
, &args
,
2578 AIO_TRANSFER_ARRAY
);
2582 /* Come here if there was no asynchronous I/O to be scheduled. */
2583 transfer_array_inner (dtp
, desc
, kind
, charlen
);
2588 transfer_array_write (st_parameter_dt
*dtp
, gfc_array_char
*desc
, int kind
,
2589 gfc_charlen_type charlen
)
2591 transfer_array (dtp
, desc
, kind
, charlen
);
2595 /* User defined input/output iomsg. */
2597 #define IOMSG_LEN 256
2600 transfer_derived (st_parameter_dt
*parent
, void *dtio_source
, void *dtio_proc
)
2602 if (parent
->u
.p
.current_unit
)
2604 if (parent
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
)
2605 parent
->u
.p
.ufdtio_ptr
= (unformatted_dtio
) dtio_proc
;
2607 parent
->u
.p
.fdtio_ptr
= (formatted_dtio
) dtio_proc
;
2609 wrap_scalar_transfer (parent
, BT_CLASS
, dtio_source
, 0, 0, 1);
2613 /* Preposition a sequential unformatted file while reading. */
2616 us_read (st_parameter_dt
*dtp
, int continued
)
2623 if (compile_options
.record_marker
== 0)
2624 n
= sizeof (GFC_INTEGER_4
);
2626 n
= compile_options
.record_marker
;
2628 nr
= sread (dtp
->u
.p
.current_unit
->s
, &i
, n
);
2629 if (unlikely (nr
< 0))
2631 generate_error (&dtp
->common
, LIBERROR_BAD_US
, NULL
);
2637 return; /* end of file */
2639 else if (unlikely (n
!= nr
))
2641 generate_error (&dtp
->common
, LIBERROR_BAD_US
, NULL
);
2645 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
2646 if (likely (dtp
->u
.p
.current_unit
->flags
.convert
== GFC_CONVERT_NATIVE
))
2650 case sizeof(GFC_INTEGER_4
):
2651 memcpy (&i4
, &i
, sizeof (i4
));
2655 case sizeof(GFC_INTEGER_8
):
2656 memcpy (&i8
, &i
, sizeof (i8
));
2661 runtime_error ("Illegal value for record marker");
2671 case sizeof(GFC_INTEGER_4
):
2672 memcpy (&u32
, &i
, sizeof (u32
));
2673 u32
= __builtin_bswap32 (u32
);
2674 memcpy (&i4
, &u32
, sizeof (i4
));
2678 case sizeof(GFC_INTEGER_8
):
2679 memcpy (&u64
, &i
, sizeof (u64
));
2680 u64
= __builtin_bswap64 (u64
);
2681 memcpy (&i8
, &u64
, sizeof (i8
));
2686 runtime_error ("Illegal value for record marker");
2693 dtp
->u
.p
.current_unit
->bytes_left_subrecord
= i
;
2694 dtp
->u
.p
.current_unit
->continued
= 0;
2698 dtp
->u
.p
.current_unit
->bytes_left_subrecord
= -i
;
2699 dtp
->u
.p
.current_unit
->continued
= 1;
2703 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2707 /* Preposition a sequential unformatted file while writing. This
2708 amount to writing a bogus length that will be filled in later. */
2711 us_write (st_parameter_dt
*dtp
, int continued
)
2718 if (compile_options
.record_marker
== 0)
2719 nbytes
= sizeof (GFC_INTEGER_4
);
2721 nbytes
= compile_options
.record_marker
;
2723 if (swrite (dtp
->u
.p
.current_unit
->s
, &dummy
, nbytes
) != nbytes
)
2724 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2726 /* For sequential unformatted, if RECL= was not specified in the OPEN
2727 we write until we have more bytes than can fit in the subrecord
2728 markers, then we write a new subrecord. */
2730 dtp
->u
.p
.current_unit
->bytes_left_subrecord
=
2731 dtp
->u
.p
.current_unit
->recl_subrecord
;
2732 dtp
->u
.p
.current_unit
->continued
= continued
;
2736 /* Position to the next record prior to transfer. We are assumed to
2737 be before the next record. We also calculate the bytes in the next
2741 pre_position (st_parameter_dt
*dtp
)
2743 if (dtp
->u
.p
.current_unit
->current_record
)
2744 return; /* Already positioned. */
2746 switch (current_mode (dtp
))
2748 case FORMATTED_STREAM
:
2749 case UNFORMATTED_STREAM
:
2750 /* There are no records with stream I/O. If the position was specified
2751 data_transfer_init has already positioned the file. If no position
2752 was specified, we continue from where we last left off. I.e.
2753 there is nothing to do here. */
2756 case UNFORMATTED_SEQUENTIAL
:
2757 if (dtp
->u
.p
.mode
== READING
)
2764 case FORMATTED_SEQUENTIAL
:
2765 case FORMATTED_DIRECT
:
2766 case UNFORMATTED_DIRECT
:
2767 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2771 dtp
->u
.p
.current_unit
->current_record
= 1;
2775 /* Initialize things for a data transfer. This code is common for
2776 both reading and writing. */
2779 data_transfer_init (st_parameter_dt
*dtp
, int read_flag
)
2781 unit_flags u_flags
; /* Used for creating a unit if needed. */
2782 GFC_INTEGER_4 cf
= dtp
->common
.flags
;
2783 namelist_info
*ionml
;
2786 NOTE ("data_transfer_init");
2788 ionml
= ((cf
& IOPARM_DT_IONML_SET
) != 0) ? dtp
->u
.p
.ionml
: NULL
;
2790 memset (&dtp
->u
.p
, 0, sizeof (dtp
->u
.p
));
2792 dtp
->u
.p
.ionml
= ionml
;
2793 dtp
->u
.p
.mode
= read_flag
? READING
: WRITING
;
2794 dtp
->u
.p
.namelist_mode
= 0;
2795 dtp
->u
.p
.cc
.len
= 0;
2797 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
2800 dtp
->u
.p
.current_unit
= get_unit (dtp
, 1);
2802 if (dtp
->u
.p
.current_unit
== NULL
)
2804 /* This means we tried to access an external unit < 0 without
2805 having opened it first with NEWUNIT=. */
2806 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2807 "Unit number is negative and unit was not already "
2808 "opened with OPEN(NEWUNIT=...)");
2811 else if (dtp
->u
.p
.current_unit
->s
== NULL
)
2812 { /* Open the unit with some default flags. */
2813 st_parameter_open opp
;
2815 NOTE ("Open the unit with some default flags.");
2816 memset (&u_flags
, '\0', sizeof (u_flags
));
2817 u_flags
.access
= ACCESS_SEQUENTIAL
;
2818 u_flags
.action
= ACTION_READWRITE
;
2820 /* Is it unformatted? */
2821 if (!(cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
2822 | IOPARM_DT_IONML_SET
)))
2823 u_flags
.form
= FORM_UNFORMATTED
;
2825 u_flags
.form
= FORM_UNSPECIFIED
;
2827 u_flags
.delim
= DELIM_UNSPECIFIED
;
2828 u_flags
.blank
= BLANK_UNSPECIFIED
;
2829 u_flags
.pad
= PAD_UNSPECIFIED
;
2830 u_flags
.decimal
= DECIMAL_UNSPECIFIED
;
2831 u_flags
.encoding
= ENCODING_UNSPECIFIED
;
2832 u_flags
.async
= ASYNC_UNSPECIFIED
;
2833 u_flags
.round
= ROUND_UNSPECIFIED
;
2834 u_flags
.sign
= SIGN_UNSPECIFIED
;
2835 u_flags
.share
= SHARE_UNSPECIFIED
;
2836 u_flags
.cc
= CC_UNSPECIFIED
;
2837 u_flags
.readonly
= 0;
2839 u_flags
.status
= STATUS_UNKNOWN
;
2841 conv
= get_unformatted_convert (dtp
->common
.unit
);
2843 if (conv
== GFC_CONVERT_NONE
)
2844 conv
= compile_options
.convert
;
2848 case GFC_CONVERT_NATIVE
:
2849 case GFC_CONVERT_SWAP
:
2852 case GFC_CONVERT_BIG
:
2853 conv
= __BYTE_ORDER__
== __ORDER_BIG_ENDIAN__
? GFC_CONVERT_NATIVE
: GFC_CONVERT_SWAP
;
2856 case GFC_CONVERT_LITTLE
:
2857 conv
= __BYTE_ORDER__
== __ORDER_BIG_ENDIAN__
? GFC_CONVERT_SWAP
: GFC_CONVERT_NATIVE
;
2861 internal_error (&opp
.common
, "Illegal value for CONVERT");
2865 u_flags
.convert
= conv
;
2867 opp
.common
= dtp
->common
;
2868 opp
.common
.flags
&= IOPARM_COMMON_MASK
;
2869 dtp
->u
.p
.current_unit
= new_unit (&opp
, dtp
->u
.p
.current_unit
, &u_flags
);
2870 dtp
->common
.flags
&= ~IOPARM_COMMON_MASK
;
2871 dtp
->common
.flags
|= (opp
.common
.flags
& IOPARM_COMMON_MASK
);
2872 if (dtp
->u
.p
.current_unit
== NULL
)
2876 if (dtp
->u
.p
.current_unit
->child_dtio
== 0)
2878 if ((cf
& IOPARM_DT_HAS_SIZE
) != 0)
2880 dtp
->u
.p
.current_unit
->has_size
= true;
2881 /* Initialize the count. */
2882 dtp
->u
.p
.current_unit
->size_used
= 0;
2885 dtp
->u
.p
.current_unit
->has_size
= false;
2887 else if (dtp
->u
.p
.current_unit
->internal_unit_kind
> 0)
2888 dtp
->u
.p
.unit_is_internal
= 1;
2890 if ((cf
& IOPARM_DT_HAS_ASYNCHRONOUS
) != 0)
2893 f
= find_option (&dtp
->common
, dtp
->asynchronous
, dtp
->asynchronous_len
,
2894 async_opt
, "Bad ASYNCHRONOUS in data transfer "
2896 if (f
== ASYNC_YES
&& dtp
->u
.p
.current_unit
->flags
.async
!= ASYNC_YES
)
2898 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2899 "ASYNCHRONOUS transfer without "
2900 "ASYHCRONOUS='YES' in OPEN");
2903 dtp
->u
.p
.async
= f
== ASYNC_YES
;
2906 au
= dtp
->u
.p
.current_unit
->au
;
2911 /* If this is an asynchronous I/O statement, collect errors and
2912 return if there are any. */
2913 if (collect_async_errors (&dtp
->common
, au
))
2918 /* Synchronous statement: Perform a wait operation for any pending
2919 asynchronous I/O. This needs to be done before all other error
2920 checks. See F2008, 9.6.4.1. */
2921 if (async_wait (&(dtp
->common
), au
))
2926 /* Check the action. */
2928 if (read_flag
&& dtp
->u
.p
.current_unit
->flags
.action
== ACTION_WRITE
)
2930 generate_error (&dtp
->common
, LIBERROR_BAD_ACTION
,
2931 "Cannot read from file opened for WRITE");
2935 if (!read_flag
&& dtp
->u
.p
.current_unit
->flags
.action
== ACTION_READ
)
2937 generate_error (&dtp
->common
, LIBERROR_BAD_ACTION
,
2938 "Cannot write to file opened for READ");
2942 dtp
->u
.p
.first_item
= 1;
2944 /* Check the format. */
2946 if ((cf
& IOPARM_DT_HAS_FORMAT
) != 0)
2949 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
2950 && (cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
))
2953 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2954 "Format present for UNFORMATTED data transfer");
2958 if ((cf
& IOPARM_DT_HAS_NAMELIST_NAME
) != 0 && dtp
->u
.p
.ionml
!= NULL
)
2960 if ((cf
& IOPARM_DT_HAS_FORMAT
) != 0)
2962 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2963 "A format cannot be specified with a namelist");
2967 else if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
&&
2968 !(cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
)))
2970 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2971 "Missing format for FORMATTED data transfer");
2975 if (is_internal_unit (dtp
)
2976 && dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
)
2978 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2979 "Internal file cannot be accessed by UNFORMATTED "
2984 /* Check the record or position number. */
2986 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
2987 && (cf
& IOPARM_DT_HAS_REC
) == 0)
2989 generate_error (&dtp
->common
, LIBERROR_MISSING_OPTION
,
2990 "Direct access data transfer requires record number");
2994 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
2996 if ((cf
& IOPARM_DT_HAS_REC
) != 0)
2998 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2999 "Record number not allowed for sequential access "
3004 if (compile_options
.warn_std
&&
3005 dtp
->u
.p
.current_unit
->endfile
== AFTER_ENDFILE
)
3007 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
3008 "Sequential READ or WRITE not allowed after "
3009 "EOF marker, possibly use REWIND or BACKSPACE");
3014 /* Process the ADVANCE option. */
3016 dtp
->u
.p
.advance_status
3017 = !(cf
& IOPARM_DT_HAS_ADVANCE
) ? ADVANCE_UNSPECIFIED
:
3018 find_option (&dtp
->common
, dtp
->advance
, dtp
->advance_len
, advance_opt
,
3019 "Bad ADVANCE parameter in data transfer statement");
3021 if (dtp
->u
.p
.advance_status
!= ADVANCE_UNSPECIFIED
)
3023 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
3025 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
3026 "ADVANCE specification conflicts with sequential "
3031 if (is_internal_unit (dtp
))
3033 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
3034 "ADVANCE specification conflicts with internal file");
3038 if ((cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
))
3039 != IOPARM_DT_HAS_FORMAT
)
3041 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
3042 "ADVANCE specification requires an explicit format");
3047 /* Child IO is non-advancing and any ADVANCE= specifier is ignored.
3049 if (dtp
->u
.p
.current_unit
->child_dtio
> 0)
3050 dtp
->u
.p
.advance_status
= ADVANCE_NO
;
3054 dtp
->u
.p
.current_unit
->previous_nonadvancing_write
= 0;
3056 if ((cf
& IOPARM_EOR
) != 0 && dtp
->u
.p
.advance_status
!= ADVANCE_NO
)
3058 generate_error (&dtp
->common
, LIBERROR_MISSING_OPTION
,
3059 "EOR specification requires an ADVANCE specification "
3064 if ((cf
& IOPARM_DT_HAS_SIZE
) != 0
3065 && dtp
->u
.p
.advance_status
!= ADVANCE_NO
)
3067 generate_error (&dtp
->common
, LIBERROR_MISSING_OPTION
,
3068 "SIZE specification requires an ADVANCE "
3069 "specification of NO");
3074 { /* Write constraints. */
3075 if ((cf
& IOPARM_END
) != 0)
3077 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
3078 "END specification cannot appear in a write "
3083 if ((cf
& IOPARM_EOR
) != 0)
3085 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
3086 "EOR specification cannot appear in a write "
3091 if ((cf
& IOPARM_DT_HAS_SIZE
) != 0)
3093 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
3094 "SIZE specification cannot appear in a write "
3100 if (dtp
->u
.p
.advance_status
== ADVANCE_UNSPECIFIED
)
3101 dtp
->u
.p
.advance_status
= ADVANCE_YES
;
3103 /* Check the decimal mode. */
3104 dtp
->u
.p
.current_unit
->decimal_status
3105 = !(cf
& IOPARM_DT_HAS_DECIMAL
) ? DECIMAL_UNSPECIFIED
:
3106 find_option (&dtp
->common
, dtp
->decimal
, dtp
->decimal_len
,
3107 decimal_opt
, "Bad DECIMAL parameter in data transfer "
3110 if (dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_UNSPECIFIED
)
3111 dtp
->u
.p
.current_unit
->decimal_status
= dtp
->u
.p
.current_unit
->flags
.decimal
;
3113 /* Check the round mode. */
3114 dtp
->u
.p
.current_unit
->round_status
3115 = !(cf
& IOPARM_DT_HAS_ROUND
) ? ROUND_UNSPECIFIED
:
3116 find_option (&dtp
->common
, dtp
->round
, dtp
->round_len
,
3117 round_opt
, "Bad ROUND parameter in data transfer "
3120 if (dtp
->u
.p
.current_unit
->round_status
== ROUND_UNSPECIFIED
)
3121 dtp
->u
.p
.current_unit
->round_status
= dtp
->u
.p
.current_unit
->flags
.round
;
3123 /* Check the sign mode. */
3124 dtp
->u
.p
.sign_status
3125 = !(cf
& IOPARM_DT_HAS_SIGN
) ? SIGN_UNSPECIFIED
:
3126 find_option (&dtp
->common
, dtp
->sign
, dtp
->sign_len
, sign_opt
,
3127 "Bad SIGN parameter in data transfer statement");
3129 if (dtp
->u
.p
.sign_status
== SIGN_UNSPECIFIED
)
3130 dtp
->u
.p
.sign_status
= dtp
->u
.p
.current_unit
->flags
.sign
;
3132 /* Check the blank mode. */
3133 dtp
->u
.p
.blank_status
3134 = !(cf
& IOPARM_DT_HAS_BLANK
) ? BLANK_UNSPECIFIED
:
3135 find_option (&dtp
->common
, dtp
->blank
, dtp
->blank_len
,
3137 "Bad BLANK parameter in data transfer statement");
3139 if (dtp
->u
.p
.blank_status
== BLANK_UNSPECIFIED
)
3140 dtp
->u
.p
.blank_status
= dtp
->u
.p
.current_unit
->flags
.blank
;
3142 /* Check the delim mode. */
3143 dtp
->u
.p
.current_unit
->delim_status
3144 = !(cf
& IOPARM_DT_HAS_DELIM
) ? DELIM_UNSPECIFIED
:
3145 find_option (&dtp
->common
, dtp
->delim
, dtp
->delim_len
,
3146 delim_opt
, "Bad DELIM parameter in data transfer statement");
3148 if (dtp
->u
.p
.current_unit
->delim_status
== DELIM_UNSPECIFIED
)
3150 if (ionml
&& dtp
->u
.p
.current_unit
->flags
.delim
== DELIM_UNSPECIFIED
)
3151 dtp
->u
.p
.current_unit
->delim_status
= DELIM_QUOTE
;
3153 dtp
->u
.p
.current_unit
->delim_status
= dtp
->u
.p
.current_unit
->flags
.delim
;
3156 /* Check the pad mode. */
3157 dtp
->u
.p
.current_unit
->pad_status
3158 = !(cf
& IOPARM_DT_HAS_PAD
) ? PAD_UNSPECIFIED
:
3159 find_option (&dtp
->common
, dtp
->pad
, dtp
->pad_len
, pad_opt
,
3160 "Bad PAD parameter in data transfer statement");
3162 if (dtp
->u
.p
.current_unit
->pad_status
== PAD_UNSPECIFIED
)
3163 dtp
->u
.p
.current_unit
->pad_status
= dtp
->u
.p
.current_unit
->flags
.pad
;
3165 /* Set up the subroutine that will handle the transfers. */
3169 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
)
3170 dtp
->u
.p
.transfer
= unformatted_read
;
3173 if ((cf
& IOPARM_DT_LIST_FORMAT
) != 0)
3174 dtp
->u
.p
.transfer
= list_formatted_read
;
3176 dtp
->u
.p
.transfer
= formatted_transfer
;
3181 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
)
3182 dtp
->u
.p
.transfer
= unformatted_write
;
3185 if ((cf
& IOPARM_DT_LIST_FORMAT
) != 0)
3186 dtp
->u
.p
.transfer
= list_formatted_write
;
3188 dtp
->u
.p
.transfer
= formatted_transfer
;
3192 if (au
&& dtp
->u
.p
.async
)
3194 NOTE ("enqueue_data_transfer");
3195 enqueue_data_transfer_init (au
, dtp
, read_flag
);
3199 NOTE ("invoking data_transfer_init_worker");
3200 data_transfer_init_worker (dtp
, read_flag
);
3205 data_transfer_init_worker (st_parameter_dt
*dtp
, int read_flag
)
3207 GFC_INTEGER_4 cf
= dtp
->common
.flags
;
3209 NOTE ("starting worker...");
3211 if (read_flag
&& dtp
->u
.p
.current_unit
->flags
.form
!= FORM_UNFORMATTED
3212 && ((cf
& IOPARM_DT_LIST_FORMAT
) != 0)
3213 && dtp
->u
.p
.current_unit
->child_dtio
== 0)
3214 dtp
->u
.p
.current_unit
->last_char
= EOF
- 1;
3216 /* Check to see if we might be reading what we wrote before */
3218 if (dtp
->u
.p
.mode
!= dtp
->u
.p
.current_unit
->mode
3219 && !is_internal_unit (dtp
))
3221 int pos
= fbuf_reset (dtp
->u
.p
.current_unit
);
3223 sseek (dtp
->u
.p
.current_unit
->s
, pos
, SEEK_CUR
);
3224 sflush(dtp
->u
.p
.current_unit
->s
);
3227 /* Check the POS= specifier: that it is in range and that it is used with a
3228 unit that has been connected for STREAM access. F2003 9.5.1.10. */
3230 if (((cf
& IOPARM_DT_HAS_POS
) != 0))
3232 if (is_stream_io (dtp
))
3237 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
3238 "POS=specifier must be positive");
3242 if (dtp
->pos
>= dtp
->u
.p
.current_unit
->maxrec
)
3244 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
3245 "POS=specifier too large");
3249 dtp
->rec
= dtp
->pos
;
3251 if (dtp
->u
.p
.mode
== READING
)
3253 /* Reset the endfile flag; if we hit EOF during reading
3254 we'll set the flag and generate an error at that point
3255 rather than worrying about it here. */
3256 dtp
->u
.p
.current_unit
->endfile
= NO_ENDFILE
;
3259 if (dtp
->pos
!= dtp
->u
.p
.current_unit
->strm_pos
)
3261 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
3262 if (sseek (dtp
->u
.p
.current_unit
->s
, dtp
->pos
- 1, SEEK_SET
) < 0)
3264 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
3267 dtp
->u
.p
.current_unit
->strm_pos
= dtp
->pos
;
3272 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
3273 "POS=specifier not allowed, "
3274 "Try OPEN with ACCESS='stream'");
3280 /* Sanity checks on the record number. */
3281 if ((cf
& IOPARM_DT_HAS_REC
) != 0)
3285 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
3286 "Record number must be positive");
3290 if (dtp
->rec
>= dtp
->u
.p
.current_unit
->maxrec
)
3292 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
3293 "Record number too large");
3297 /* Make sure format buffer is reset. */
3298 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
)
3299 fbuf_reset (dtp
->u
.p
.current_unit
);
3302 /* Check whether the record exists to be read. Only
3303 a partial record needs to exist. */
3305 if (dtp
->u
.p
.mode
== READING
&& (dtp
->rec
- 1)
3306 * dtp
->u
.p
.current_unit
->recl
>= ssize (dtp
->u
.p
.current_unit
->s
))
3308 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
3309 "Non-existing record number");
3313 /* Position the file. */
3314 if (sseek (dtp
->u
.p
.current_unit
->s
, (gfc_offset
) (dtp
->rec
- 1)
3315 * dtp
->u
.p
.current_unit
->recl
, SEEK_SET
) < 0)
3317 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
3321 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_STREAM
)
3323 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
3324 "Record number not allowed for stream access "
3330 /* Bugware for badly written mixed C-Fortran I/O. */
3331 if (!is_internal_unit (dtp
))
3332 flush_if_preconnected(dtp
->u
.p
.current_unit
->s
);
3334 dtp
->u
.p
.current_unit
->mode
= dtp
->u
.p
.mode
;
3336 /* Set the maximum position reached from the previous I/O operation. This
3337 could be greater than zero from a previous non-advancing write. */
3338 dtp
->u
.p
.max_pos
= dtp
->u
.p
.current_unit
->saved_pos
;
3342 /* Make sure that we don't do a read after a nonadvancing write. */
3346 if (dtp
->u
.p
.current_unit
->read_bad
&& !is_stream_io (dtp
))
3348 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
3349 "Cannot READ after a nonadvancing WRITE");
3355 if (dtp
->u
.p
.advance_status
== ADVANCE_YES
&& !dtp
->u
.p
.seen_dollar
)
3356 dtp
->u
.p
.current_unit
->read_bad
= 1;
3359 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
)
3361 #ifdef HAVE_USELOCALE
3362 dtp
->u
.p
.old_locale
= uselocale (c_locale
);
3364 __gthread_mutex_lock (&old_locale_lock
);
3365 if (!old_locale_ctr
++)
3367 old_locale
= setlocale (LC_NUMERIC
, NULL
);
3368 setlocale (LC_NUMERIC
, "C");
3370 __gthread_mutex_unlock (&old_locale_lock
);
3372 /* Start the data transfer if we are doing a formatted transfer. */
3373 if ((cf
& (IOPARM_DT_LIST_FORMAT
| IOPARM_DT_HAS_NAMELIST_NAME
)) == 0
3374 && dtp
->u
.p
.ionml
== NULL
)
3375 formatted_transfer (dtp
, 0, NULL
, 0, 0, 1);
3380 /* Initialize an array_loop_spec given the array descriptor. The function
3381 returns the index of the last element of the array, and also returns
3382 starting record, where the first I/O goes to (necessary in case of
3383 negative strides). */
3386 init_loop_spec (gfc_array_char
*desc
, array_loop_spec
*ls
,
3387 gfc_offset
*start_record
)
3389 int rank
= GFC_DESCRIPTOR_RANK(desc
);
3398 for (i
=0; i
<rank
; i
++)
3400 ls
[i
].idx
= GFC_DESCRIPTOR_LBOUND(desc
,i
);
3401 ls
[i
].start
= GFC_DESCRIPTOR_LBOUND(desc
,i
);
3402 ls
[i
].end
= GFC_DESCRIPTOR_UBOUND(desc
,i
);
3403 ls
[i
].step
= GFC_DESCRIPTOR_STRIDE(desc
,i
);
3404 empty
= empty
|| (GFC_DESCRIPTOR_UBOUND(desc
,i
)
3405 < GFC_DESCRIPTOR_LBOUND(desc
,i
));
3407 if (GFC_DESCRIPTOR_STRIDE(desc
,i
) > 0)
3409 index
+= (GFC_DESCRIPTOR_EXTENT(desc
,i
) - 1)
3410 * GFC_DESCRIPTOR_STRIDE(desc
,i
);
3414 index
-= (GFC_DESCRIPTOR_EXTENT(desc
,i
) - 1)
3415 * GFC_DESCRIPTOR_STRIDE(desc
,i
);
3416 *start_record
-= (GFC_DESCRIPTOR_EXTENT(desc
,i
) - 1)
3417 * GFC_DESCRIPTOR_STRIDE(desc
,i
);
3427 /* Determine the index to the next record in an internal unit array by
3428 by incrementing through the array_loop_spec. */
3431 next_array_record (st_parameter_dt
*dtp
, array_loop_spec
*ls
, int *finished
)
3439 for (i
= 0; i
< dtp
->u
.p
.current_unit
->rank
; i
++)
3444 if (ls
[i
].idx
> ls
[i
].end
)
3446 ls
[i
].idx
= ls
[i
].start
;
3452 index
= index
+ (ls
[i
].idx
- ls
[i
].start
) * ls
[i
].step
;
3462 /* Skip to the end of the current record, taking care of an optional
3463 record marker of size bytes. If the file is not seekable, we
3464 read chunks of size MAX_READ until we get to the right
3468 skip_record (st_parameter_dt
*dtp
, gfc_offset bytes
)
3470 ssize_t rlength
, readb
;
3471 #define MAX_READ 4096
3474 dtp
->u
.p
.current_unit
->bytes_left_subrecord
+= bytes
;
3475 if (dtp
->u
.p
.current_unit
->bytes_left_subrecord
== 0)
3478 /* Direct access files do not generate END conditions,
3480 if (sseek (dtp
->u
.p
.current_unit
->s
,
3481 dtp
->u
.p
.current_unit
->bytes_left_subrecord
, SEEK_CUR
) < 0)
3483 /* Seeking failed, fall back to seeking by reading data. */
3484 while (dtp
->u
.p
.current_unit
->bytes_left_subrecord
> 0)
3487 (MAX_READ
< dtp
->u
.p
.current_unit
->bytes_left_subrecord
) ?
3488 MAX_READ
: dtp
->u
.p
.current_unit
->bytes_left_subrecord
;
3490 readb
= sread (dtp
->u
.p
.current_unit
->s
, p
, rlength
);
3493 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
3497 dtp
->u
.p
.current_unit
->bytes_left_subrecord
-= readb
;
3501 dtp
->u
.p
.current_unit
->bytes_left_subrecord
= 0;
3505 /* Advance to the next record reading unformatted files, taking
3506 care of subrecords. If complete_record is nonzero, we loop
3507 until all subrecords are cleared. */
3510 next_record_r_unf (st_parameter_dt
*dtp
, int complete_record
)
3514 bytes
= compile_options
.record_marker
== 0 ?
3515 sizeof (GFC_INTEGER_4
) : compile_options
.record_marker
;
3520 /* Skip over tail */
3522 skip_record (dtp
, bytes
);
3524 if ( ! (complete_record
&& dtp
->u
.p
.current_unit
->continued
))
3533 min_off (gfc_offset a
, gfc_offset b
)
3535 return (a
< b
? a
: b
);
3539 /* Space to the next record for read mode. */
3542 next_record_r (st_parameter_dt
*dtp
, int done
)
3548 switch (current_mode (dtp
))
3550 /* No records in unformatted STREAM I/O. */
3551 case UNFORMATTED_STREAM
:
3554 case UNFORMATTED_SEQUENTIAL
:
3555 next_record_r_unf (dtp
, 1);
3556 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
3559 case FORMATTED_DIRECT
:
3560 case UNFORMATTED_DIRECT
:
3561 skip_record (dtp
, dtp
->u
.p
.current_unit
->bytes_left
);
3564 case FORMATTED_STREAM
:
3565 case FORMATTED_SEQUENTIAL
:
3566 /* read_sf has already terminated input because of an '\n', or
3568 if (dtp
->u
.p
.sf_seen_eor
)
3570 dtp
->u
.p
.sf_seen_eor
= 0;
3574 if (is_internal_unit (dtp
))
3576 if (is_array_io (dtp
))
3580 record
= next_array_record (dtp
, dtp
->u
.p
.current_unit
->ls
,
3582 if (!done
&& finished
)
3585 /* Now seek to this record. */
3586 record
= record
* dtp
->u
.p
.current_unit
->recl
;
3587 if (sseek (dtp
->u
.p
.current_unit
->s
, record
, SEEK_SET
) < 0)
3589 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
3592 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
3596 gfc_offset bytes_left
= dtp
->u
.p
.current_unit
->bytes_left
;
3597 bytes_left
= min_off (bytes_left
,
3598 ssize (dtp
->u
.p
.current_unit
->s
)
3599 - stell (dtp
->u
.p
.current_unit
->s
));
3600 if (sseek (dtp
->u
.p
.current_unit
->s
,
3601 bytes_left
, SEEK_CUR
) < 0)
3603 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
3606 dtp
->u
.p
.current_unit
->bytes_left
3607 = dtp
->u
.p
.current_unit
->recl
;
3611 else if (dtp
->u
.p
.current_unit
->flags
.cc
!= CC_NONE
)
3616 cc
= fbuf_getc (dtp
->u
.p
.current_unit
);
3620 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
3623 if (is_stream_io (dtp
)
3624 || dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
3625 || dtp
->u
.p
.current_unit
->bytes_left
3626 == dtp
->u
.p
.current_unit
->recl
)
3632 if (is_stream_io (dtp
))
3633 dtp
->u
.p
.current_unit
->strm_pos
++;
3644 /* Small utility function to write a record marker, taking care of
3645 byte swapping and of choosing the correct size. */
3648 write_us_marker (st_parameter_dt
*dtp
, const gfc_offset buf
)
3654 if (compile_options
.record_marker
== 0)
3655 len
= sizeof (GFC_INTEGER_4
);
3657 len
= compile_options
.record_marker
;
3659 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
3660 if (likely (dtp
->u
.p
.current_unit
->flags
.convert
== GFC_CONVERT_NATIVE
))
3664 case sizeof (GFC_INTEGER_4
):
3666 return swrite (dtp
->u
.p
.current_unit
->s
, &buf4
, len
);
3669 case sizeof (GFC_INTEGER_8
):
3671 return swrite (dtp
->u
.p
.current_unit
->s
, &buf8
, len
);
3675 runtime_error ("Illegal value for record marker");
3685 case sizeof (GFC_INTEGER_4
):
3687 memcpy (&u32
, &buf4
, sizeof (u32
));
3688 u32
= __builtin_bswap32 (u32
);
3689 return swrite (dtp
->u
.p
.current_unit
->s
, &u32
, len
);
3692 case sizeof (GFC_INTEGER_8
):
3694 memcpy (&u64
, &buf8
, sizeof (u64
));
3695 u64
= __builtin_bswap64 (u64
);
3696 return swrite (dtp
->u
.p
.current_unit
->s
, &u64
, len
);
3700 runtime_error ("Illegal value for record marker");
3707 /* Position to the next (sub)record in write mode for
3708 unformatted sequential files. */
3711 next_record_w_unf (st_parameter_dt
*dtp
, int next_subrecord
)
3713 gfc_offset m
, m_write
, record_marker
;
3715 /* Bytes written. */
3716 m
= dtp
->u
.p
.current_unit
->recl_subrecord
3717 - dtp
->u
.p
.current_unit
->bytes_left_subrecord
;
3719 if (compile_options
.record_marker
== 0)
3720 record_marker
= sizeof (GFC_INTEGER_4
);
3722 record_marker
= compile_options
.record_marker
;
3724 /* Seek to the head and overwrite the bogus length with the real
3727 if (unlikely (sseek (dtp
->u
.p
.current_unit
->s
, - m
- record_marker
,
3736 if (unlikely (write_us_marker (dtp
, m_write
) < 0))
3739 /* Seek past the end of the current record. */
3741 if (unlikely (sseek (dtp
->u
.p
.current_unit
->s
, m
, SEEK_CUR
) < 0))
3744 /* Write the length tail. If we finish a record containing
3745 subrecords, we write out the negative length. */
3747 if (dtp
->u
.p
.current_unit
->continued
)
3752 if (unlikely (write_us_marker (dtp
, m_write
) < 0))
3758 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
3764 /* Utility function like memset() but operating on streams. Return
3765 value is same as for POSIX write(). */
3768 sset (stream
*s
, int c
, gfc_offset nbyte
)
3770 #define WRITE_CHUNK 256
3771 char p
[WRITE_CHUNK
];
3772 gfc_offset bytes_left
;
3775 if (nbyte
< WRITE_CHUNK
)
3776 memset (p
, c
, nbyte
);
3778 memset (p
, c
, WRITE_CHUNK
);
3781 while (bytes_left
> 0)
3783 trans
= (bytes_left
< WRITE_CHUNK
) ? bytes_left
: WRITE_CHUNK
;
3784 trans
= swrite (s
, p
, trans
);
3787 bytes_left
-= trans
;
3790 return nbyte
- bytes_left
;
3794 /* Finish up a record according to the legacy carriagecontrol type, based
3795 on the first character in the record. */
3798 next_record_cc (st_parameter_dt
*dtp
)
3800 /* Only valid with CARRIAGECONTROL=FORTRAN. */
3801 if (dtp
->u
.p
.current_unit
->flags
.cc
!= CC_FORTRAN
)
3804 fbuf_seek (dtp
->u
.p
.current_unit
, 0, SEEK_END
);
3805 if (dtp
->u
.p
.cc
.len
> 0)
3807 char *p
= fbuf_alloc (dtp
->u
.p
.current_unit
, dtp
->u
.p
.cc
.len
);
3809 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
3811 /* Output CR for the first character with default CC setting. */
3812 *(p
++) = dtp
->u
.p
.cc
.u
.end
;
3813 if (dtp
->u
.p
.cc
.len
> 1)
3814 *p
= dtp
->u
.p
.cc
.u
.end
;
3818 /* Position to the next record in write mode. */
3821 next_record_w (st_parameter_dt
*dtp
, int done
)
3823 gfc_offset max_pos_off
;
3825 /* Zero counters for X- and T-editing. */
3826 max_pos_off
= dtp
->u
.p
.max_pos
;
3827 dtp
->u
.p
.max_pos
= dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
3829 switch (current_mode (dtp
))
3831 /* No records in unformatted STREAM I/O. */
3832 case UNFORMATTED_STREAM
:
3835 case FORMATTED_DIRECT
:
3836 if (dtp
->u
.p
.current_unit
->bytes_left
== 0)
3839 fbuf_seek (dtp
->u
.p
.current_unit
, 0, SEEK_END
);
3840 fbuf_flush (dtp
->u
.p
.current_unit
, WRITING
);
3841 if (sset (dtp
->u
.p
.current_unit
->s
, ' ',
3842 dtp
->u
.p
.current_unit
->bytes_left
)
3843 != dtp
->u
.p
.current_unit
->bytes_left
)
3848 case UNFORMATTED_DIRECT
:
3849 if (dtp
->u
.p
.current_unit
->bytes_left
> 0)
3851 gfc_offset length
= dtp
->u
.p
.current_unit
->bytes_left
;
3852 if (sset (dtp
->u
.p
.current_unit
->s
, 0, length
) != length
)
3857 case UNFORMATTED_SEQUENTIAL
:
3858 next_record_w_unf (dtp
, 0);
3859 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
3862 case FORMATTED_STREAM
:
3863 case FORMATTED_SEQUENTIAL
:
3865 if (is_internal_unit (dtp
))
3868 /* Internal unit, so must fit in memory. */
3870 size_t max_pos
= max_pos_off
;
3871 if (is_array_io (dtp
))
3875 length
= dtp
->u
.p
.current_unit
->bytes_left
;
3877 /* If the farthest position reached is greater than current
3878 position, adjust the position and set length to pad out
3879 whats left. Otherwise just pad whats left.
3880 (for character array unit) */
3881 m
= dtp
->u
.p
.current_unit
->recl
3882 - dtp
->u
.p
.current_unit
->bytes_left
;
3885 length
= (max_pos
- m
);
3886 if (sseek (dtp
->u
.p
.current_unit
->s
,
3887 length
, SEEK_CUR
) < 0)
3889 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
3892 length
= ((size_t) dtp
->u
.p
.current_unit
->recl
- max_pos
);
3895 p
= write_block (dtp
, length
);
3899 if (unlikely (is_char4_unit (dtp
)))
3901 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
3902 memset4 (p4
, ' ', length
);
3905 memset (p
, ' ', length
);
3907 /* Now that the current record has been padded out,
3908 determine where the next record in the array is.
3909 Note that this can return a negative value, so it
3910 needs to be assigned to a signed value. */
3911 gfc_offset record
= next_array_record
3912 (dtp
, dtp
->u
.p
.current_unit
->ls
, &finished
);
3914 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
3916 /* Now seek to this record */
3917 record
= record
* dtp
->u
.p
.current_unit
->recl
;
3919 if (sseek (dtp
->u
.p
.current_unit
->s
, record
, SEEK_SET
) < 0)
3921 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
3925 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
3931 /* If this is the last call to next_record move to the farthest
3932 position reached and set length to pad out the remainder
3933 of the record. (for character scaler unit) */
3936 m
= dtp
->u
.p
.current_unit
->recl
3937 - dtp
->u
.p
.current_unit
->bytes_left
;
3940 length
= max_pos
- m
;
3941 if (sseek (dtp
->u
.p
.current_unit
->s
,
3942 length
, SEEK_CUR
) < 0)
3944 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
3947 length
= (size_t) dtp
->u
.p
.current_unit
->recl
3951 length
= dtp
->u
.p
.current_unit
->bytes_left
;
3955 p
= write_block (dtp
, length
);
3959 if (unlikely (is_char4_unit (dtp
)))
3961 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
3962 memset4 (p4
, (gfc_char4_t
) ' ', length
);
3965 memset (p
, ' ', length
);
3969 /* Handle legacy CARRIAGECONTROL line endings. */
3970 else if (dtp
->u
.p
.current_unit
->flags
.cc
== CC_FORTRAN
)
3971 next_record_cc (dtp
);
3974 /* Skip newlines for CC=CC_NONE. */
3975 const int len
= (dtp
->u
.p
.current_unit
->flags
.cc
== CC_NONE
)
3982 fbuf_seek (dtp
->u
.p
.current_unit
, 0, SEEK_END
);
3983 if (dtp
->u
.p
.current_unit
->flags
.cc
!= CC_NONE
)
3985 char *p
= fbuf_alloc (dtp
->u
.p
.current_unit
, len
);
3993 if (is_stream_io (dtp
))
3995 dtp
->u
.p
.current_unit
->strm_pos
+= len
;
3996 if (dtp
->u
.p
.current_unit
->strm_pos
3997 < ssize (dtp
->u
.p
.current_unit
->s
))
3998 unit_truncate (dtp
->u
.p
.current_unit
,
3999 dtp
->u
.p
.current_unit
->strm_pos
- 1,
4007 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
4012 /* Position to the next record, which means moving to the end of the
4013 current record. This can happen under several different
4014 conditions. If the done flag is not set, we get ready to process
4018 next_record (st_parameter_dt
*dtp
, int done
)
4020 gfc_offset fp
; /* File position. */
4022 dtp
->u
.p
.current_unit
->read_bad
= 0;
4024 if (dtp
->u
.p
.mode
== READING
)
4025 next_record_r (dtp
, done
);
4027 next_record_w (dtp
, done
);
4029 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
4031 if (!is_stream_io (dtp
))
4033 /* Since we have changed the position, set it to unspecified so
4034 that INQUIRE(POSITION=) knows it needs to look into it. */
4036 dtp
->u
.p
.current_unit
->flags
.position
= POSITION_UNSPECIFIED
;
4038 dtp
->u
.p
.current_unit
->current_record
= 0;
4039 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
4041 fp
= stell (dtp
->u
.p
.current_unit
->s
);
4042 /* Calculate next record, rounding up partial records. */
4043 dtp
->u
.p
.current_unit
->last_record
=
4044 (fp
+ dtp
->u
.p
.current_unit
->recl
) /
4045 dtp
->u
.p
.current_unit
->recl
- 1;
4048 dtp
->u
.p
.current_unit
->last_record
++;
4054 smarkeor (dtp
->u
.p
.current_unit
->s
);
4058 /* Finalize the current data transfer. For a nonadvancing transfer,
4059 this means advancing to the next record. For internal units close the
4060 stream associated with the unit. */
4063 finalize_transfer (st_parameter_dt
*dtp
)
4065 GFC_INTEGER_4 cf
= dtp
->common
.flags
;
4067 if ((dtp
->u
.p
.ionml
!= NULL
)
4068 && (cf
& IOPARM_DT_HAS_NAMELIST_NAME
) != 0)
4070 dtp
->u
.p
.namelist_mode
= 1;
4071 if ((cf
& IOPARM_DT_NAMELIST_READ_MODE
) != 0)
4072 namelist_read (dtp
);
4074 namelist_write (dtp
);
4077 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
4078 *dtp
->size
= dtp
->u
.p
.current_unit
->size_used
;
4080 if (dtp
->u
.p
.eor_condition
)
4082 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
4086 if (dtp
->u
.p
.current_unit
&& (dtp
->u
.p
.current_unit
->child_dtio
> 0))
4088 if (cf
& IOPARM_DT_HAS_FORMAT
)
4090 free (dtp
->u
.p
.fmt
);
4096 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
4098 if (dtp
->u
.p
.current_unit
&& current_mode (dtp
) == UNFORMATTED_SEQUENTIAL
)
4099 dtp
->u
.p
.current_unit
->current_record
= 0;
4103 dtp
->u
.p
.transfer
= NULL
;
4104 if (dtp
->u
.p
.current_unit
== NULL
)
4107 if ((cf
& IOPARM_DT_LIST_FORMAT
) != 0 && dtp
->u
.p
.mode
== READING
)
4109 finish_list_read (dtp
);
4113 if (dtp
->u
.p
.mode
== WRITING
)
4114 dtp
->u
.p
.current_unit
->previous_nonadvancing_write
4115 = dtp
->u
.p
.advance_status
== ADVANCE_NO
;
4117 if (is_stream_io (dtp
))
4119 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
4120 && dtp
->u
.p
.advance_status
!= ADVANCE_NO
)
4121 next_record (dtp
, 1);
4126 dtp
->u
.p
.current_unit
->current_record
= 0;
4128 if (!is_internal_unit (dtp
) && dtp
->u
.p
.seen_dollar
)
4130 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
4131 dtp
->u
.p
.seen_dollar
= 0;
4135 /* For non-advancing I/O, save the current maximum position for use in the
4136 next I/O operation if needed. */
4137 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
)
4139 if (dtp
->u
.p
.skips
> 0)
4142 write_x (dtp
, dtp
->u
.p
.skips
, dtp
->u
.p
.pending_spaces
);
4143 tmp
= (int)(dtp
->u
.p
.current_unit
->recl
4144 - dtp
->u
.p
.current_unit
->bytes_left
);
4146 dtp
->u
.p
.max_pos
> tmp
? dtp
->u
.p
.max_pos
: tmp
;
4149 int bytes_written
= (int) (dtp
->u
.p
.current_unit
->recl
4150 - dtp
->u
.p
.current_unit
->bytes_left
);
4151 dtp
->u
.p
.current_unit
->saved_pos
=
4152 dtp
->u
.p
.max_pos
> 0 ? dtp
->u
.p
.max_pos
- bytes_written
: 0;
4153 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
4156 else if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
4157 && dtp
->u
.p
.mode
== WRITING
&& !is_internal_unit (dtp
))
4158 fbuf_seek (dtp
->u
.p
.current_unit
, 0, SEEK_END
);
4160 dtp
->u
.p
.current_unit
->saved_pos
= 0;
4161 dtp
->u
.p
.current_unit
->last_char
= EOF
- 1;
4162 next_record (dtp
, 1);
4166 if (dtp
->u
.p
.unit_is_internal
)
4168 /* The unit structure may be reused later so clear the
4169 internal unit kind. */
4170 dtp
->u
.p
.current_unit
->internal_unit_kind
= 0;
4172 fbuf_destroy (dtp
->u
.p
.current_unit
);
4173 if (dtp
->u
.p
.current_unit
4174 && (dtp
->u
.p
.current_unit
->child_dtio
== 0)
4175 && dtp
->u
.p
.current_unit
->s
)
4177 sclose (dtp
->u
.p
.current_unit
->s
);
4178 dtp
->u
.p
.current_unit
->s
= NULL
;
4182 #ifdef HAVE_USELOCALE
4183 if (dtp
->u
.p
.old_locale
!= (locale_t
) 0)
4185 uselocale (dtp
->u
.p
.old_locale
);
4186 dtp
->u
.p
.old_locale
= (locale_t
) 0;
4189 __gthread_mutex_lock (&old_locale_lock
);
4190 if (!--old_locale_ctr
)
4192 setlocale (LC_NUMERIC
, old_locale
);
4195 __gthread_mutex_unlock (&old_locale_lock
);
4199 /* Transfer function for IOLENGTH. It doesn't actually do any
4200 data transfer, it just updates the length counter. */
4203 iolength_transfer (st_parameter_dt
*dtp
, bt type
__attribute__((unused
)),
4204 void *dest
__attribute__ ((unused
)),
4205 int kind
__attribute__((unused
)),
4206 size_t size
, size_t nelems
)
4208 if ((dtp
->common
.flags
& IOPARM_DT_HAS_IOLENGTH
) != 0)
4209 *dtp
->iolength
+= (GFC_IO_INT
) (size
* nelems
);
4213 /* Initialize the IOLENGTH data transfer. This function is in essence
4214 a very much simplified version of data_transfer_init(), because it
4215 doesn't have to deal with units at all. */
4218 iolength_transfer_init (st_parameter_dt
*dtp
)
4220 if ((dtp
->common
.flags
& IOPARM_DT_HAS_IOLENGTH
) != 0)
4223 memset (&dtp
->u
.p
, 0, sizeof (dtp
->u
.p
));
4225 /* Set up the subroutine that will handle the transfers. */
4227 dtp
->u
.p
.transfer
= iolength_transfer
;
4231 /* Library entry point for the IOLENGTH form of the INQUIRE
4232 statement. The IOLENGTH form requires no I/O to be performed, but
4233 it must still be a runtime library call so that we can determine
4234 the iolength for dynamic arrays and such. */
4236 extern void st_iolength (st_parameter_dt
*);
4237 export_proto(st_iolength
);
4240 st_iolength (st_parameter_dt
*dtp
)
4242 library_start (&dtp
->common
);
4243 iolength_transfer_init (dtp
);
4246 extern void st_iolength_done (st_parameter_dt
*);
4247 export_proto(st_iolength_done
);
4250 st_iolength_done (st_parameter_dt
*dtp
__attribute__((unused
)))
4257 /* The READ statement. */
4259 extern void st_read (st_parameter_dt
*);
4260 export_proto(st_read
);
4263 st_read (st_parameter_dt
*dtp
)
4265 library_start (&dtp
->common
);
4267 data_transfer_init (dtp
, 1);
4270 extern void st_read_done (st_parameter_dt
*);
4271 export_proto(st_read_done
);
4274 st_read_done_worker (st_parameter_dt
*dtp
)
4276 finalize_transfer (dtp
);
4280 /* If this is a parent READ statement we do not need to retain the
4281 internal unit structure for child use. */
4282 if (dtp
->u
.p
.current_unit
!= NULL
4283 && dtp
->u
.p
.current_unit
->child_dtio
== 0)
4285 if (dtp
->u
.p
.unit_is_internal
)
4287 if ((dtp
->common
.flags
& IOPARM_DT_HAS_UDTIO
) == 0)
4289 free (dtp
->u
.p
.current_unit
->filename
);
4290 dtp
->u
.p
.current_unit
->filename
= NULL
;
4291 if (dtp
->u
.p
.current_unit
->ls
)
4292 free (dtp
->u
.p
.current_unit
->ls
);
4293 dtp
->u
.p
.current_unit
->ls
= NULL
;
4295 newunit_free (dtp
->common
.unit
);
4297 if (dtp
->u
.p
.unit_is_internal
|| dtp
->u
.p
.format_not_saved
)
4299 free_format_data (dtp
->u
.p
.fmt
);
4306 st_read_done (st_parameter_dt
*dtp
)
4308 if (dtp
->u
.p
.current_unit
)
4310 if (dtp
->u
.p
.current_unit
->au
)
4312 if (dtp
->common
.flags
& IOPARM_DT_HAS_ID
)
4313 *dtp
->id
= enqueue_done_id (dtp
->u
.p
.current_unit
->au
, AIO_READ_DONE
);
4317 enqueue_done (dtp
->u
.p
.current_unit
->au
, AIO_READ_DONE
);
4321 st_read_done_worker (dtp
);
4323 unlock_unit (dtp
->u
.p
.current_unit
);
4329 extern void st_write (st_parameter_dt
*);
4330 export_proto (st_write
);
4333 st_write (st_parameter_dt
*dtp
)
4335 library_start (&dtp
->common
);
4336 data_transfer_init (dtp
, 0);
4341 st_write_done_worker (st_parameter_dt
*dtp
)
4343 finalize_transfer (dtp
);
4345 if (dtp
->u
.p
.current_unit
!= NULL
4346 && dtp
->u
.p
.current_unit
->child_dtio
== 0)
4348 /* Deal with endfile conditions associated with sequential files. */
4349 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
4350 switch (dtp
->u
.p
.current_unit
->endfile
)
4352 case AT_ENDFILE
: /* Remain at the endfile record. */
4356 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
; /* Just at it now. */
4360 /* Get rid of whatever is after this record. */
4361 if (!is_internal_unit (dtp
))
4362 unit_truncate (dtp
->u
.p
.current_unit
,
4363 stell (dtp
->u
.p
.current_unit
->s
),
4365 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
4371 /* If this is a parent WRITE statement we do not need to retain the
4372 internal unit structure for child use. */
4373 if (dtp
->u
.p
.unit_is_internal
)
4375 if ((dtp
->common
.flags
& IOPARM_DT_HAS_UDTIO
) == 0)
4377 free (dtp
->u
.p
.current_unit
->filename
);
4378 dtp
->u
.p
.current_unit
->filename
= NULL
;
4379 if (dtp
->u
.p
.current_unit
->ls
)
4380 free (dtp
->u
.p
.current_unit
->ls
);
4381 dtp
->u
.p
.current_unit
->ls
= NULL
;
4383 newunit_free (dtp
->common
.unit
);
4385 if (dtp
->u
.p
.unit_is_internal
|| dtp
->u
.p
.format_not_saved
)
4387 free_format_data (dtp
->u
.p
.fmt
);
4393 extern void st_write_done (st_parameter_dt
*);
4394 export_proto(st_write_done
);
4397 st_write_done (st_parameter_dt
*dtp
)
4399 if (dtp
->u
.p
.current_unit
)
4401 if (dtp
->u
.p
.current_unit
->au
&& dtp
->u
.p
.async
)
4403 if (dtp
->common
.flags
& IOPARM_DT_HAS_ID
)
4404 *dtp
->id
= enqueue_done_id (dtp
->u
.p
.current_unit
->au
,
4408 /* We perform synchronous I/O on an asynchronous unit, so no need
4409 to enqueue AIO_READ_DONE. */
4411 enqueue_done (dtp
->u
.p
.current_unit
->au
, AIO_WRITE_DONE
);
4415 st_write_done_worker (dtp
);
4417 unlock_unit (dtp
->u
.p
.current_unit
);
4423 /* Wait operation. We need to keep around the do-nothing version
4424 of st_wait for compatibility with previous versions, which had marked
4425 the argument as unused (and thus liable to be removed).
4427 TODO: remove at next bump in version number. */
4430 st_wait (st_parameter_wait
*wtp
__attribute__((unused
)))
4436 st_wait_async (st_parameter_wait
*wtp
)
4438 gfc_unit
*u
= find_unit (wtp
->common
.unit
);
4439 if (ASYNC_IO
&& u
->au
)
4441 if (wtp
->common
.flags
& IOPARM_WAIT_HAS_ID
)
4442 async_wait_id (&(wtp
->common
), u
->au
, *wtp
->id
);
4444 async_wait (&(wtp
->common
), u
->au
);
4451 /* Receives the scalar information for namelist objects and stores it
4452 in a linked list of namelist_info types. */
4455 set_nml_var (st_parameter_dt
*dtp
, void *var_addr
, char *var_name
,
4456 GFC_INTEGER_4 len
, gfc_charlen_type string_length
,
4457 dtype_type dtype
, void *dtio_sub
, void *vtable
)
4459 namelist_info
*t1
= NULL
;
4461 size_t var_name_len
= strlen (var_name
);
4463 nml
= (namelist_info
*) xmalloc (sizeof (namelist_info
));
4465 nml
->mem_pos
= var_addr
;
4466 nml
->dtio_sub
= dtio_sub
;
4467 nml
->vtable
= vtable
;
4469 nml
->var_name
= (char*) xmalloc (var_name_len
+ 1);
4470 memcpy (nml
->var_name
, var_name
, var_name_len
);
4471 nml
->var_name
[var_name_len
] = '\0';
4473 nml
->len
= (int) len
;
4474 nml
->string_length
= (index_type
) string_length
;
4476 nml
->var_rank
= (int) (dtype
.rank
);
4477 nml
->size
= (index_type
) (dtype
.elem_len
);
4478 nml
->type
= (bt
) (dtype
.type
);
4480 if (nml
->var_rank
> 0)
4482 nml
->dim
= (descriptor_dimension
*)
4483 xmallocarray (nml
->var_rank
, sizeof (descriptor_dimension
));
4484 nml
->ls
= (array_loop_spec
*)
4485 xmallocarray (nml
->var_rank
, sizeof (array_loop_spec
));
4495 if ((dtp
->common
.flags
& IOPARM_DT_IONML_SET
) == 0)
4497 dtp
->common
.flags
|= IOPARM_DT_IONML_SET
;
4498 dtp
->u
.p
.ionml
= nml
;
4502 for (t1
= dtp
->u
.p
.ionml
; t1
->next
; t1
= t1
->next
);
4507 extern void st_set_nml_var (st_parameter_dt
*dtp
, void *, char *,
4508 GFC_INTEGER_4
, gfc_charlen_type
, dtype_type
);
4509 export_proto(st_set_nml_var
);
4512 st_set_nml_var (st_parameter_dt
*dtp
, void *var_addr
, char *var_name
,
4513 GFC_INTEGER_4 len
, gfc_charlen_type string_length
,
4516 set_nml_var (dtp
, var_addr
, var_name
, len
, string_length
,
4521 /* Essentially the same as previous but carrying the dtio procedure
4522 and the vtable as additional arguments. */
4523 extern void st_set_nml_dtio_var (st_parameter_dt
*dtp
, void *, char *,
4524 GFC_INTEGER_4
, gfc_charlen_type
, dtype_type
,
4526 export_proto(st_set_nml_dtio_var
);
4530 st_set_nml_dtio_var (st_parameter_dt
*dtp
, void *var_addr
, char *var_name
,
4531 GFC_INTEGER_4 len
, gfc_charlen_type string_length
,
4532 dtype_type dtype
, void *dtio_sub
, void *vtable
)
4534 set_nml_var (dtp
, var_addr
, var_name
, len
, string_length
,
4535 dtype
, dtio_sub
, vtable
);
4538 /* Store the dimensional information for the namelist object. */
4539 extern void st_set_nml_var_dim (st_parameter_dt
*, GFC_INTEGER_4
,
4540 index_type
, index_type
,
4542 export_proto(st_set_nml_var_dim
);
4545 st_set_nml_var_dim (st_parameter_dt
*dtp
, GFC_INTEGER_4 n_dim
,
4546 index_type stride
, index_type lbound
,
4554 for (nml
= dtp
->u
.p
.ionml
; nml
->next
; nml
= nml
->next
);
4556 GFC_DIMENSION_SET(nml
->dim
[n
],lbound
,ubound
,stride
);
4560 /* Once upon a time, a poor innocent Fortran program was reading a
4561 file, when suddenly it hit the end-of-file (EOF). Unfortunately
4562 the OS doesn't tell whether we're at the EOF or whether we already
4563 went past it. Luckily our hero, libgfortran, keeps track of this.
4564 Call this function when you detect an EOF condition. See Section
4568 hit_eof (st_parameter_dt
*dtp
)
4570 dtp
->u
.p
.current_unit
->flags
.position
= POSITION_APPEND
;
4572 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
4573 switch (dtp
->u
.p
.current_unit
->endfile
)
4577 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
4578 if (!is_internal_unit (dtp
) && !dtp
->u
.p
.namelist_mode
)
4580 dtp
->u
.p
.current_unit
->endfile
= AFTER_ENDFILE
;
4581 dtp
->u
.p
.current_unit
->current_record
= 0;
4584 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
4588 generate_error (&dtp
->common
, LIBERROR_ENDFILE
, NULL
);
4589 dtp
->u
.p
.current_unit
->current_record
= 0;
4594 /* Non-sequential files don't have an ENDFILE record, so we
4595 can't be at AFTER_ENDFILE. */
4596 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
4597 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
4598 dtp
->u
.p
.current_unit
->current_record
= 0;