1 /* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
2 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
4 Namelist transfer functions contributed by Paul Thomas
5 F2003 I/O support contributed by Jerry DeLisle
7 This file is part of the GNU Fortran runtime library (libgfortran).
9 Libgfortran is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 Libgfortran is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 Under Section 7 of GPL version 3, you are granted additional
20 permissions described in the GCC Runtime Library Exception, version
21 3.1, as published by the Free Software Foundation.
23 You should have received a copy of the GNU General Public License and
24 a copy of the GCC Runtime Library Exception along with this program;
25 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
26 <http://www.gnu.org/licenses/>. */
29 /* transfer.c -- Top level handling of data transfer statements. */
41 /* Calling conventions: Data transfer statements are unlike other
42 library calls in that they extend over several calls.
44 The first call is always a call to st_read() or st_write(). These
45 subroutines return no status unless a namelist read or write is
46 being done, in which case there is the usual status. No further
47 calls are necessary in this case.
49 For other sorts of data transfer, there are zero or more data
50 transfer statement that depend on the format of the data transfer
51 statement. For READ (and for backwards compatibily: for WRITE), one has
56 transfer_character_wide
64 transfer_integer_write
65 transfer_logical_write
66 transfer_character_write
67 transfer_character_wide_write
69 transfer_complex_write
70 transfer_real128_write
71 transfer_complex128_write
73 These subroutines do not return status. The *128 functions
74 are in the file transfer128.c.
76 The last call is a call to st_[read|write]_done(). While
77 something can easily go wrong with the initial st_read() or
78 st_write(), an error inhibits any data from actually being
81 extern void transfer_integer (st_parameter_dt
*, void *, int);
82 export_proto(transfer_integer
);
84 extern void transfer_integer_write (st_parameter_dt
*, void *, int);
85 export_proto(transfer_integer_write
);
87 extern void transfer_real (st_parameter_dt
*, void *, int);
88 export_proto(transfer_real
);
90 extern void transfer_real_write (st_parameter_dt
*, void *, int);
91 export_proto(transfer_real_write
);
93 extern void transfer_logical (st_parameter_dt
*, void *, int);
94 export_proto(transfer_logical
);
96 extern void transfer_logical_write (st_parameter_dt
*, void *, int);
97 export_proto(transfer_logical_write
);
99 extern void transfer_character (st_parameter_dt
*, void *, int);
100 export_proto(transfer_character
);
102 extern void transfer_character_write (st_parameter_dt
*, void *, int);
103 export_proto(transfer_character_write
);
105 extern void transfer_character_wide (st_parameter_dt
*, void *, int, int);
106 export_proto(transfer_character_wide
);
108 extern void transfer_character_wide_write (st_parameter_dt
*,
110 export_proto(transfer_character_wide_write
);
112 extern void transfer_complex (st_parameter_dt
*, void *, int);
113 export_proto(transfer_complex
);
115 extern void transfer_complex_write (st_parameter_dt
*, void *, int);
116 export_proto(transfer_complex_write
);
118 extern void transfer_array (st_parameter_dt
*, gfc_array_char
*, int,
120 export_proto(transfer_array
);
122 extern void transfer_array_write (st_parameter_dt
*, gfc_array_char
*, int,
124 export_proto(transfer_array_write
);
126 static void us_read (st_parameter_dt
*, int);
127 static void us_write (st_parameter_dt
*, int);
128 static void next_record_r_unf (st_parameter_dt
*, int);
129 static void next_record_w_unf (st_parameter_dt
*, int);
131 static const st_option advance_opt
[] = {
132 {"yes", ADVANCE_YES
},
138 static const st_option decimal_opt
[] = {
139 {"point", DECIMAL_POINT
},
140 {"comma", DECIMAL_COMMA
},
144 static const st_option round_opt
[] = {
146 {"down", ROUND_DOWN
},
147 {"zero", ROUND_ZERO
},
148 {"nearest", ROUND_NEAREST
},
149 {"compatible", ROUND_COMPATIBLE
},
150 {"processor_defined", ROUND_PROCDEFINED
},
155 static const st_option sign_opt
[] = {
157 {"suppress", SIGN_SS
},
158 {"processor_defined", SIGN_S
},
162 static const st_option blank_opt
[] = {
163 {"null", BLANK_NULL
},
164 {"zero", BLANK_ZERO
},
168 static const st_option delim_opt
[] = {
169 {"apostrophe", DELIM_APOSTROPHE
},
170 {"quote", DELIM_QUOTE
},
171 {"none", DELIM_NONE
},
175 static const st_option pad_opt
[] = {
182 { FORMATTED_SEQUENTIAL
, UNFORMATTED_SEQUENTIAL
,
183 FORMATTED_DIRECT
, UNFORMATTED_DIRECT
, FORMATTED_STREAM
, UNFORMATTED_STREAM
189 current_mode (st_parameter_dt
*dtp
)
193 m
= FORM_UNSPECIFIED
;
195 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
197 m
= dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
?
198 FORMATTED_DIRECT
: UNFORMATTED_DIRECT
;
200 else if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
202 m
= dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
?
203 FORMATTED_SEQUENTIAL
: UNFORMATTED_SEQUENTIAL
;
205 else if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_STREAM
)
207 m
= dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
?
208 FORMATTED_STREAM
: UNFORMATTED_STREAM
;
215 /* Mid level data transfer statements. */
217 /* Read sequential file - internal unit */
220 read_sf_internal (st_parameter_dt
*dtp
, int * length
)
222 static char *empty_string
[0];
226 /* Zero size array gives internal unit len of 0. Nothing to read. */
227 if (dtp
->internal_unit_len
== 0
228 && dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
)
231 /* If we have seen an eor previously, return a length of 0. The
232 caller is responsible for correctly padding the input field. */
233 if (dtp
->u
.p
.sf_seen_eor
)
236 /* Just return something that isn't a NULL pointer, otherwise the
237 caller thinks an error occured. */
238 return (char*) empty_string
;
242 if (is_char4_unit(dtp
))
245 gfc_char4_t
*p
= (gfc_char4_t
*) mem_alloc_r4 (dtp
->u
.p
.current_unit
->s
,
247 base
= fbuf_alloc (dtp
->u
.p
.current_unit
, lorig
);
248 for (i
= 0; i
< *length
; i
++, p
++)
249 base
[i
] = *p
> 255 ? '?' : (unsigned char) *p
;
252 base
= mem_alloc_r (dtp
->u
.p
.current_unit
->s
, length
);
254 if (unlikely (lorig
> *length
))
260 dtp
->u
.p
.current_unit
->bytes_left
-= *length
;
262 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
263 dtp
->u
.p
.size_used
+= (GFC_IO_INT
) *length
;
269 /* When reading sequential formatted records we have a problem. We
270 don't know how long the line is until we read the trailing newline,
271 and we don't want to read too much. If we read too much, we might
272 have to do a physical seek backwards depending on how much data is
273 present, and devices like terminals aren't seekable and would cause
276 Given this, the solution is to read a byte at a time, stopping if
277 we hit the newline. For small allocations, we use a static buffer.
278 For larger allocations, we are forced to allocate memory on the
279 heap. Hopefully this won't happen very often. */
281 /* Read sequential file - external unit */
284 read_sf (st_parameter_dt
*dtp
, int * length
)
286 static char *empty_string
[0];
288 int n
, lorig
, seen_comma
;
290 /* If we have seen an eor previously, return a length of 0. The
291 caller is responsible for correctly padding the input field. */
292 if (dtp
->u
.p
.sf_seen_eor
)
295 /* Just return something that isn't a NULL pointer, otherwise the
296 caller thinks an error occured. */
297 return (char*) empty_string
;
302 /* Read data into format buffer and scan through it. */
304 base
= p
= fbuf_read (dtp
->u
.p
.current_unit
, length
);
312 if (q
== '\n' || q
== '\r')
314 /* Unexpected end of line. Set the position. */
315 fbuf_seek (dtp
->u
.p
.current_unit
, n
+ 1 ,SEEK_CUR
);
316 dtp
->u
.p
.sf_seen_eor
= 1;
318 /* If we see an EOR during non-advancing I/O, we need to skip
319 the rest of the I/O statement. Set the corresponding flag. */
320 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
|| dtp
->u
.p
.seen_dollar
)
321 dtp
->u
.p
.eor_condition
= 1;
323 /* If we encounter a CR, it might be a CRLF. */
324 if (q
== '\r') /* Probably a CRLF */
326 /* See if there is an LF. Use fbuf_read rather then fbuf_getc so
327 the position is not advanced unless it really is an LF. */
329 p
= fbuf_read (dtp
->u
.p
.current_unit
, &readlen
);
330 if (*p
== '\n' && readlen
== 1)
332 dtp
->u
.p
.sf_seen_eor
= 2;
333 fbuf_seek (dtp
->u
.p
.current_unit
, 1 ,SEEK_CUR
);
337 /* Without padding, terminate the I/O statement without assigning
338 the value. With padding, the value still needs to be assigned,
339 so we can just continue with a short read. */
340 if (dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
)
342 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
349 /* Short circuit the read if a comma is found during numeric input.
350 The flag is set to zero during character reads so that commas in
351 strings are not ignored */
353 if (dtp
->u
.p
.sf_read_comma
== 1)
356 notify_std (&dtp
->common
, GFC_STD_GNU
,
357 "Comma in formatted numeric read.");
365 fbuf_seek (dtp
->u
.p
.current_unit
, n
+ seen_comma
, SEEK_CUR
);
367 /* A short read implies we hit EOF, unless we hit EOR, a comma, or
368 some other stuff. Set the relevant flags. */
369 if (lorig
> *length
&& !dtp
->u
.p
.sf_seen_eor
&& !seen_comma
)
373 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
)
375 if (dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
)
381 dtp
->u
.p
.eor_condition
= 1;
386 else if (dtp
->u
.p
.advance_status
== ADVANCE_NO
387 || dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
388 || dtp
->u
.p
.current_unit
->bytes_left
389 == dtp
->u
.p
.current_unit
->recl
)
398 dtp
->u
.p
.current_unit
->bytes_left
-= n
;
400 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
401 dtp
->u
.p
.size_used
+= (GFC_IO_INT
) n
;
407 /* Function for reading the next couple of bytes from the current
408 file, advancing the current position. We return FAILURE on end of record or
409 end of file. This function is only for formatted I/O, unformatted uses
412 If the read is short, then it is because the current record does not
413 have enough data to satisfy the read request and the file was
414 opened with PAD=YES. The caller must assume tailing spaces for
418 read_block_form (st_parameter_dt
*dtp
, int * nbytes
)
423 if (!is_stream_io (dtp
))
425 if (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) *nbytes
)
427 /* For preconnected units with default record length, set bytes left
428 to unit record length and proceed, otherwise error. */
429 if (dtp
->u
.p
.current_unit
->unit_number
== options
.stdin_unit
430 && dtp
->u
.p
.current_unit
->recl
== DEFAULT_RECL
)
431 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
434 if (unlikely (dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
)
435 && !is_internal_unit (dtp
))
437 /* Not enough data left. */
438 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
443 if (unlikely (dtp
->u
.p
.current_unit
->bytes_left
== 0
444 && !is_internal_unit(dtp
)))
450 *nbytes
= dtp
->u
.p
.current_unit
->bytes_left
;
454 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
&&
455 (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
||
456 dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_STREAM
))
458 if (is_internal_unit (dtp
))
459 source
= read_sf_internal (dtp
, nbytes
);
461 source
= read_sf (dtp
, nbytes
);
463 dtp
->u
.p
.current_unit
->strm_pos
+=
464 (gfc_offset
) (*nbytes
+ dtp
->u
.p
.sf_seen_eor
);
468 /* If we reach here, we can assume it's direct access. */
470 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) *nbytes
;
473 source
= fbuf_read (dtp
->u
.p
.current_unit
, nbytes
);
474 fbuf_seek (dtp
->u
.p
.current_unit
, *nbytes
, SEEK_CUR
);
476 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
477 dtp
->u
.p
.size_used
+= (GFC_IO_INT
) *nbytes
;
479 if (norig
!= *nbytes
)
481 /* Short read, this shouldn't happen. */
482 if (!dtp
->u
.p
.current_unit
->pad_status
== PAD_YES
)
484 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
489 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) *nbytes
;
495 /* Read a block from a character(kind=4) internal unit, to be transferred into
496 a character(kind=4) variable. Note: Portions of this code borrowed from
499 read_block_form4 (st_parameter_dt
*dtp
, int * nbytes
)
501 static gfc_char4_t
*empty_string
[0];
505 if (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) *nbytes
)
506 *nbytes
= dtp
->u
.p
.current_unit
->bytes_left
;
508 /* Zero size array gives internal unit len of 0. Nothing to read. */
509 if (dtp
->internal_unit_len
== 0
510 && dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
)
513 /* If we have seen an eor previously, return a length of 0. The
514 caller is responsible for correctly padding the input field. */
515 if (dtp
->u
.p
.sf_seen_eor
)
518 /* Just return something that isn't a NULL pointer, otherwise the
519 caller thinks an error occured. */
524 source
= (gfc_char4_t
*) mem_alloc_r4 (dtp
->u
.p
.current_unit
->s
, nbytes
);
526 if (unlikely (lorig
> *nbytes
))
532 dtp
->u
.p
.current_unit
->bytes_left
-= *nbytes
;
534 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
535 dtp
->u
.p
.size_used
+= (GFC_IO_INT
) *nbytes
;
541 /* Reads a block directly into application data space. This is for
542 unformatted files. */
545 read_block_direct (st_parameter_dt
*dtp
, void *buf
, size_t nbytes
)
547 ssize_t to_read_record
;
548 ssize_t have_read_record
;
549 ssize_t to_read_subrecord
;
550 ssize_t have_read_subrecord
;
553 if (is_stream_io (dtp
))
555 have_read_record
= sread (dtp
->u
.p
.current_unit
->s
, buf
,
557 if (unlikely (have_read_record
< 0))
559 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
563 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) have_read_record
;
565 if (unlikely ((ssize_t
) nbytes
!= have_read_record
))
567 /* Short read, e.g. if we hit EOF. For stream files,
568 we have to set the end-of-file condition. */
574 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
576 if (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) nbytes
)
579 to_read_record
= dtp
->u
.p
.current_unit
->bytes_left
;
580 nbytes
= to_read_record
;
585 to_read_record
= nbytes
;
588 dtp
->u
.p
.current_unit
->bytes_left
-= to_read_record
;
590 to_read_record
= sread (dtp
->u
.p
.current_unit
->s
, buf
, to_read_record
);
591 if (unlikely (to_read_record
< 0))
593 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
597 if (to_read_record
!= (ssize_t
) nbytes
)
599 /* Short read, e.g. if we hit EOF. Apparently, we read
600 more than was written to the last record. */
604 if (unlikely (short_record
))
606 generate_error (&dtp
->common
, LIBERROR_SHORT_RECORD
, NULL
);
611 /* Unformatted sequential. We loop over the subrecords, reading
612 until the request has been fulfilled or the record has run out
613 of continuation subrecords. */
615 /* Check whether we exceed the total record length. */
617 if (dtp
->u
.p
.current_unit
->flags
.has_recl
618 && ((gfc_offset
) nbytes
> dtp
->u
.p
.current_unit
->bytes_left
))
620 to_read_record
= dtp
->u
.p
.current_unit
->bytes_left
;
625 to_read_record
= nbytes
;
628 have_read_record
= 0;
632 if (dtp
->u
.p
.current_unit
->bytes_left_subrecord
633 < (gfc_offset
) to_read_record
)
635 to_read_subrecord
= dtp
->u
.p
.current_unit
->bytes_left_subrecord
;
636 to_read_record
-= to_read_subrecord
;
640 to_read_subrecord
= to_read_record
;
644 dtp
->u
.p
.current_unit
->bytes_left_subrecord
-= to_read_subrecord
;
646 have_read_subrecord
= sread (dtp
->u
.p
.current_unit
->s
,
647 buf
+ have_read_record
, to_read_subrecord
);
648 if (unlikely (have_read_subrecord
) < 0)
650 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
654 have_read_record
+= have_read_subrecord
;
656 if (unlikely (to_read_subrecord
!= have_read_subrecord
))
658 /* Short read, e.g. if we hit EOF. This means the record
659 structure has been corrupted, or the trailing record
660 marker would still be present. */
662 generate_error (&dtp
->common
, LIBERROR_CORRUPT_FILE
, NULL
);
666 if (to_read_record
> 0)
668 if (likely (dtp
->u
.p
.current_unit
->continued
))
670 next_record_r_unf (dtp
, 0);
675 /* Let's make sure the file position is correctly pre-positioned
676 for the next read statement. */
678 dtp
->u
.p
.current_unit
->current_record
= 0;
679 next_record_r_unf (dtp
, 0);
680 generate_error (&dtp
->common
, LIBERROR_SHORT_RECORD
, NULL
);
686 /* Normal exit, the read request has been fulfilled. */
691 dtp
->u
.p
.current_unit
->bytes_left
-= have_read_record
;
692 if (unlikely (short_record
))
694 generate_error (&dtp
->common
, LIBERROR_SHORT_RECORD
, NULL
);
701 /* Function for writing a block of bytes to the current file at the
702 current position, advancing the file pointer. We are given a length
703 and return a pointer to a buffer that the caller must (completely)
704 fill in. Returns NULL on error. */
707 write_block (st_parameter_dt
*dtp
, int length
)
711 if (!is_stream_io (dtp
))
713 if (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) length
)
715 /* For preconnected units with default record length, set bytes left
716 to unit record length and proceed, otherwise error. */
717 if (likely ((dtp
->u
.p
.current_unit
->unit_number
718 == options
.stdout_unit
719 || dtp
->u
.p
.current_unit
->unit_number
720 == options
.stderr_unit
)
721 && dtp
->u
.p
.current_unit
->recl
== DEFAULT_RECL
))
722 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
725 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
730 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) length
;
733 if (is_internal_unit (dtp
))
735 if (dtp
->common
.unit
) /* char4 internel unit. */
738 dest4
= mem_alloc_w4 (dtp
->u
.p
.current_unit
->s
, &length
);
741 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
747 dest
= mem_alloc_w (dtp
->u
.p
.current_unit
->s
, &length
);
751 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
755 if (unlikely (dtp
->u
.p
.current_unit
->endfile
== AT_ENDFILE
))
756 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
760 dest
= fbuf_alloc (dtp
->u
.p
.current_unit
, length
);
763 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
768 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
769 dtp
->u
.p
.size_used
+= (GFC_IO_INT
) length
;
771 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) length
;
777 /* High level interface to swrite(), taking care of errors. This is only
778 called for unformatted files. There are three cases to consider:
779 Stream I/O, unformatted direct, unformatted sequential. */
782 write_buf (st_parameter_dt
*dtp
, void *buf
, size_t nbytes
)
785 ssize_t have_written
;
786 ssize_t to_write_subrecord
;
791 if (is_stream_io (dtp
))
793 have_written
= swrite (dtp
->u
.p
.current_unit
->s
, buf
, nbytes
);
794 if (unlikely (have_written
< 0))
796 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
800 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) have_written
;
805 /* Unformatted direct access. */
807 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
809 if (unlikely (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) nbytes
))
811 generate_error (&dtp
->common
, LIBERROR_DIRECT_EOR
, NULL
);
815 if (buf
== NULL
&& nbytes
== 0)
818 have_written
= swrite (dtp
->u
.p
.current_unit
->s
, buf
, nbytes
);
819 if (unlikely (have_written
< 0))
821 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
825 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) have_written
;
826 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) have_written
;
831 /* Unformatted sequential. */
835 if (dtp
->u
.p
.current_unit
->flags
.has_recl
836 && (gfc_offset
) nbytes
> dtp
->u
.p
.current_unit
->bytes_left
)
838 nbytes
= dtp
->u
.p
.current_unit
->bytes_left
;
850 (size_t) dtp
->u
.p
.current_unit
->bytes_left_subrecord
< nbytes
?
851 (size_t) dtp
->u
.p
.current_unit
->bytes_left_subrecord
: nbytes
;
853 dtp
->u
.p
.current_unit
->bytes_left_subrecord
-=
854 (gfc_offset
) to_write_subrecord
;
856 to_write_subrecord
= swrite (dtp
->u
.p
.current_unit
->s
,
857 buf
+ have_written
, to_write_subrecord
);
858 if (unlikely (to_write_subrecord
< 0))
860 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
864 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) to_write_subrecord
;
865 nbytes
-= to_write_subrecord
;
866 have_written
+= to_write_subrecord
;
871 next_record_w_unf (dtp
, 1);
874 dtp
->u
.p
.current_unit
->bytes_left
-= have_written
;
875 if (unlikely (short_record
))
877 generate_error (&dtp
->common
, LIBERROR_SHORT_RECORD
, NULL
);
884 /* Master function for unformatted reads. */
887 unformatted_read (st_parameter_dt
*dtp
, bt type
,
888 void *dest
, int kind
, size_t size
, size_t nelems
)
890 if (likely (dtp
->u
.p
.current_unit
->flags
.convert
== GFC_CONVERT_NATIVE
)
893 if (type
== BT_CHARACTER
)
894 size
*= GFC_SIZE_OF_CHAR_KIND(kind
);
895 read_block_direct (dtp
, dest
, size
* nelems
);
905 /* Handle wide chracters. */
906 if (type
== BT_CHARACTER
&& kind
!= 1)
912 /* Break up complex into its constituent reals. */
913 if (type
== BT_COMPLEX
)
919 /* By now, all complex variables have been split into their
920 constituent reals. */
922 for (i
= 0; i
< nelems
; i
++)
924 read_block_direct (dtp
, buffer
, size
);
925 reverse_memcpy (p
, buffer
, size
);
932 /* Master function for unformatted writes. NOTE: For kind=10 the size is 16
933 bytes on 64 bit machines. The unused bytes are not initialized and never
934 used, which can show an error with memory checking analyzers like
938 unformatted_write (st_parameter_dt
*dtp
, bt type
,
939 void *source
, int kind
, size_t size
, size_t nelems
)
941 if (likely (dtp
->u
.p
.current_unit
->flags
.convert
== GFC_CONVERT_NATIVE
)
944 size_t stride
= type
== BT_CHARACTER
?
945 size
* GFC_SIZE_OF_CHAR_KIND(kind
) : size
;
947 write_buf (dtp
, source
, stride
* nelems
);
957 /* Handle wide chracters. */
958 if (type
== BT_CHARACTER
&& kind
!= 1)
964 /* Break up complex into its constituent reals. */
965 if (type
== BT_COMPLEX
)
971 /* By now, all complex variables have been split into their
972 constituent reals. */
974 for (i
= 0; i
< nelems
; i
++)
976 reverse_memcpy(buffer
, p
, size
);
978 write_buf (dtp
, buffer
, size
);
984 /* Return a pointer to the name of a type. */
1009 internal_error (NULL
, "type_name(): Bad type");
1016 /* Write a constant string to the output.
1017 This is complicated because the string can have doubled delimiters
1018 in it. The length in the format node is the true length. */
1021 write_constant_string (st_parameter_dt
*dtp
, const fnode
*f
)
1023 char c
, delimiter
, *p
, *q
;
1026 length
= f
->u
.string
.length
;
1030 p
= write_block (dtp
, length
);
1037 for (; length
> 0; length
--)
1040 if (c
== delimiter
&& c
!= 'H' && c
!= 'h')
1041 q
++; /* Skip the doubled delimiter. */
1046 /* Given actual and expected types in a formatted data transfer, make
1047 sure they agree. If not, an error message is generated. Returns
1048 nonzero if something went wrong. */
1051 require_type (st_parameter_dt
*dtp
, bt expected
, bt actual
, const fnode
*f
)
1055 if (actual
== expected
)
1058 /* Adjust item_count before emitting error message. */
1059 sprintf (buffer
, "Expected %s for item %d in formatted transfer, got %s",
1060 type_name (expected
), dtp
->u
.p
.item_count
- 1, type_name (actual
));
1062 format_error (dtp
, f
, buffer
);
1067 /* This function is in the main loop for a formatted data transfer
1068 statement. It would be natural to implement this as a coroutine
1069 with the user program, but C makes that awkward. We loop,
1070 processing format elements. When we actually have to transfer
1071 data instead of just setting flags, we return control to the user
1072 program which calls a function that supplies the address and type
1073 of the next element, then comes back here to process it. */
1076 formatted_transfer_scalar_read (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
1079 int pos
, bytes_used
;
1083 int consume_data_flag
;
1085 /* Change a complex data item into a pair of reals. */
1087 n
= (p
== NULL
) ? 0 : ((type
!= BT_COMPLEX
) ? 1 : 2);
1088 if (type
== BT_COMPLEX
)
1094 /* If there's an EOR condition, we simulate finalizing the transfer
1095 by doing nothing. */
1096 if (dtp
->u
.p
.eor_condition
)
1099 /* Set this flag so that commas in reads cause the read to complete before
1100 the entire field has been read. The next read field will start right after
1101 the comma in the stream. (Set to 0 for character reads). */
1102 dtp
->u
.p
.sf_read_comma
=
1103 dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
? 0 : 1;
1107 /* If reversion has occurred and there is another real data item,
1108 then we have to move to the next record. */
1109 if (dtp
->u
.p
.reversion_flag
&& n
> 0)
1111 dtp
->u
.p
.reversion_flag
= 0;
1112 next_record (dtp
, 0);
1115 consume_data_flag
= 1;
1116 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1119 f
= next_format (dtp
);
1122 /* No data descriptors left. */
1123 if (unlikely (n
> 0))
1124 generate_error (&dtp
->common
, LIBERROR_FORMAT
,
1125 "Insufficient data descriptors in format after reversion");
1131 bytes_used
= (int)(dtp
->u
.p
.current_unit
->recl
1132 - dtp
->u
.p
.current_unit
->bytes_left
);
1134 if (is_stream_io(dtp
))
1141 goto need_read_data
;
1142 if (require_type (dtp
, BT_INTEGER
, type
, f
))
1144 read_decimal (dtp
, f
, p
, kind
);
1149 goto need_read_data
;
1150 if (!(compile_options
.allow_std
& GFC_STD_GNU
)
1151 && require_type (dtp
, BT_INTEGER
, type
, f
))
1153 read_radix (dtp
, f
, p
, kind
, 2);
1158 goto need_read_data
;
1159 if (!(compile_options
.allow_std
& GFC_STD_GNU
)
1160 && require_type (dtp
, BT_INTEGER
, type
, f
))
1162 read_radix (dtp
, f
, p
, kind
, 8);
1167 goto need_read_data
;
1168 if (!(compile_options
.allow_std
& GFC_STD_GNU
)
1169 && require_type (dtp
, BT_INTEGER
, type
, f
))
1171 read_radix (dtp
, f
, p
, kind
, 16);
1176 goto need_read_data
;
1178 /* It is possible to have FMT_A with something not BT_CHARACTER such
1179 as when writing out hollerith strings, so check both type
1180 and kind before calling wide character routines. */
1181 if (type
== BT_CHARACTER
&& kind
== 4)
1182 read_a_char4 (dtp
, f
, p
, size
);
1184 read_a (dtp
, f
, p
, size
);
1189 goto need_read_data
;
1190 read_l (dtp
, f
, p
, kind
);
1195 goto need_read_data
;
1196 if (require_type (dtp
, BT_REAL
, type
, f
))
1198 read_f (dtp
, f
, p
, kind
);
1203 goto need_read_data
;
1204 if (require_type (dtp
, BT_REAL
, type
, f
))
1206 read_f (dtp
, f
, p
, kind
);
1211 goto need_read_data
;
1212 if (require_type (dtp
, BT_REAL
, type
, f
))
1214 read_f (dtp
, f
, p
, kind
);
1219 goto need_read_data
;
1220 if (require_type (dtp
, BT_REAL
, type
, f
))
1222 read_f (dtp
, f
, p
, kind
);
1227 goto need_read_data
;
1228 if (require_type (dtp
, BT_REAL
, type
, f
))
1230 read_f (dtp
, f
, p
, kind
);
1235 goto need_read_data
;
1239 read_decimal (dtp
, f
, p
, kind
);
1242 read_l (dtp
, f
, p
, kind
);
1246 read_a_char4 (dtp
, f
, p
, size
);
1248 read_a (dtp
, f
, p
, size
);
1251 read_f (dtp
, f
, p
, kind
);
1254 internal_error (&dtp
->common
, "formatted_transfer(): Bad type");
1259 consume_data_flag
= 0;
1260 format_error (dtp
, f
, "Constant string in input format");
1263 /* Format codes that don't transfer data. */
1266 consume_data_flag
= 0;
1267 dtp
->u
.p
.skips
+= f
->u
.n
;
1268 pos
= bytes_used
+ dtp
->u
.p
.skips
- 1;
1269 dtp
->u
.p
.pending_spaces
= pos
- dtp
->u
.p
.max_pos
+ 1;
1270 read_x (dtp
, f
->u
.n
);
1275 consume_data_flag
= 0;
1277 if (f
->format
== FMT_TL
)
1279 /* Handle the special case when no bytes have been used yet.
1280 Cannot go below zero. */
1281 if (bytes_used
== 0)
1283 dtp
->u
.p
.pending_spaces
-= f
->u
.n
;
1284 dtp
->u
.p
.skips
-= f
->u
.n
;
1285 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
< 0 ? 0 : dtp
->u
.p
.skips
;
1288 pos
= bytes_used
- f
->u
.n
;
1293 /* Standard 10.6.1.1: excessive left tabbing is reset to the
1294 left tab limit. We do not check if the position has gone
1295 beyond the end of record because a subsequent tab could
1296 bring us back again. */
1297 pos
= pos
< 0 ? 0 : pos
;
1299 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
+ pos
- bytes_used
;
1300 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
1301 + pos
- dtp
->u
.p
.max_pos
;
1302 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
< 0
1303 ? 0 : dtp
->u
.p
.pending_spaces
;
1304 if (dtp
->u
.p
.skips
== 0)
1307 /* Adjust everything for end-of-record condition */
1308 if (dtp
->u
.p
.sf_seen_eor
&& !is_internal_unit (dtp
))
1310 dtp
->u
.p
.current_unit
->bytes_left
-= dtp
->u
.p
.sf_seen_eor
;
1311 dtp
->u
.p
.skips
-= dtp
->u
.p
.sf_seen_eor
;
1313 dtp
->u
.p
.sf_seen_eor
= 0;
1315 if (dtp
->u
.p
.skips
< 0)
1317 if (is_internal_unit (dtp
))
1318 sseek (dtp
->u
.p
.current_unit
->s
, dtp
->u
.p
.skips
, SEEK_CUR
);
1320 fbuf_seek (dtp
->u
.p
.current_unit
, dtp
->u
.p
.skips
, SEEK_CUR
);
1321 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) dtp
->u
.p
.skips
;
1322 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1325 read_x (dtp
, dtp
->u
.p
.skips
);
1329 consume_data_flag
= 0;
1330 dtp
->u
.p
.sign_status
= SIGN_S
;
1334 consume_data_flag
= 0;
1335 dtp
->u
.p
.sign_status
= SIGN_SS
;
1339 consume_data_flag
= 0;
1340 dtp
->u
.p
.sign_status
= SIGN_SP
;
1344 consume_data_flag
= 0 ;
1345 dtp
->u
.p
.blank_status
= BLANK_NULL
;
1349 consume_data_flag
= 0;
1350 dtp
->u
.p
.blank_status
= BLANK_ZERO
;
1354 consume_data_flag
= 0;
1355 dtp
->u
.p
.current_unit
->decimal_status
= DECIMAL_COMMA
;
1359 consume_data_flag
= 0;
1360 dtp
->u
.p
.current_unit
->decimal_status
= DECIMAL_POINT
;
1364 consume_data_flag
= 0;
1365 dtp
->u
.p
.current_unit
->round_status
= ROUND_COMPATIBLE
;
1369 consume_data_flag
= 0;
1370 dtp
->u
.p
.current_unit
->round_status
= ROUND_DOWN
;
1374 consume_data_flag
= 0;
1375 dtp
->u
.p
.current_unit
->round_status
= ROUND_NEAREST
;
1379 consume_data_flag
= 0;
1380 dtp
->u
.p
.current_unit
->round_status
= ROUND_PROCDEFINED
;
1384 consume_data_flag
= 0;
1385 dtp
->u
.p
.current_unit
->round_status
= ROUND_UP
;
1389 consume_data_flag
= 0;
1390 dtp
->u
.p
.current_unit
->round_status
= ROUND_ZERO
;
1394 consume_data_flag
= 0;
1395 dtp
->u
.p
.scale_factor
= f
->u
.k
;
1399 consume_data_flag
= 0;
1400 dtp
->u
.p
.seen_dollar
= 1;
1404 consume_data_flag
= 0;
1405 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1406 next_record (dtp
, 0);
1410 /* A colon descriptor causes us to exit this loop (in
1411 particular preventing another / descriptor from being
1412 processed) unless there is another data item to be
1414 consume_data_flag
= 0;
1420 internal_error (&dtp
->common
, "Bad format node");
1423 /* Adjust the item count and data pointer. */
1425 if ((consume_data_flag
> 0) && (n
> 0))
1428 p
= ((char *) p
) + size
;
1433 pos
= (int)(dtp
->u
.p
.current_unit
->recl
- dtp
->u
.p
.current_unit
->bytes_left
);
1434 dtp
->u
.p
.max_pos
= (dtp
->u
.p
.max_pos
> pos
) ? dtp
->u
.p
.max_pos
: pos
;
1439 /* Come here when we need a data descriptor but don't have one. We
1440 push the current format node back onto the input, then return and
1441 let the user program call us back with the data. */
1443 unget_format (dtp
, f
);
1448 formatted_transfer_scalar_write (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
1451 int pos
, bytes_used
;
1455 int consume_data_flag
;
1457 /* Change a complex data item into a pair of reals. */
1459 n
= (p
== NULL
) ? 0 : ((type
!= BT_COMPLEX
) ? 1 : 2);
1460 if (type
== BT_COMPLEX
)
1466 /* If there's an EOR condition, we simulate finalizing the transfer
1467 by doing nothing. */
1468 if (dtp
->u
.p
.eor_condition
)
1471 /* Set this flag so that commas in reads cause the read to complete before
1472 the entire field has been read. The next read field will start right after
1473 the comma in the stream. (Set to 0 for character reads). */
1474 dtp
->u
.p
.sf_read_comma
=
1475 dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
? 0 : 1;
1479 /* If reversion has occurred and there is another real data item,
1480 then we have to move to the next record. */
1481 if (dtp
->u
.p
.reversion_flag
&& n
> 0)
1483 dtp
->u
.p
.reversion_flag
= 0;
1484 next_record (dtp
, 0);
1487 consume_data_flag
= 1;
1488 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1491 f
= next_format (dtp
);
1494 /* No data descriptors left. */
1495 if (unlikely (n
> 0))
1496 generate_error (&dtp
->common
, LIBERROR_FORMAT
,
1497 "Insufficient data descriptors in format after reversion");
1501 /* Now discharge T, TR and X movements to the right. This is delayed
1502 until a data producing format to suppress trailing spaces. */
1505 if (dtp
->u
.p
.mode
== WRITING
&& dtp
->u
.p
.skips
!= 0
1506 && ((n
>0 && ( t
== FMT_I
|| t
== FMT_B
|| t
== FMT_O
1507 || t
== FMT_Z
|| t
== FMT_F
|| t
== FMT_E
1508 || t
== FMT_EN
|| t
== FMT_ES
|| t
== FMT_G
1509 || t
== FMT_L
|| t
== FMT_A
|| t
== FMT_D
))
1510 || t
== FMT_STRING
))
1512 if (dtp
->u
.p
.skips
> 0)
1515 write_x (dtp
, dtp
->u
.p
.skips
, dtp
->u
.p
.pending_spaces
);
1516 tmp
= (int)(dtp
->u
.p
.current_unit
->recl
1517 - dtp
->u
.p
.current_unit
->bytes_left
);
1519 dtp
->u
.p
.max_pos
> tmp
? dtp
->u
.p
.max_pos
: tmp
;
1521 if (dtp
->u
.p
.skips
< 0)
1523 if (is_internal_unit (dtp
))
1524 sseek (dtp
->u
.p
.current_unit
->s
, dtp
->u
.p
.skips
, SEEK_CUR
);
1526 fbuf_seek (dtp
->u
.p
.current_unit
, dtp
->u
.p
.skips
, SEEK_CUR
);
1527 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) dtp
->u
.p
.skips
;
1529 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1532 bytes_used
= (int)(dtp
->u
.p
.current_unit
->recl
1533 - dtp
->u
.p
.current_unit
->bytes_left
);
1535 if (is_stream_io(dtp
))
1543 if (require_type (dtp
, BT_INTEGER
, type
, f
))
1545 write_i (dtp
, f
, p
, kind
);
1551 if (!(compile_options
.allow_std
& GFC_STD_GNU
)
1552 && require_type (dtp
, BT_INTEGER
, type
, f
))
1554 write_b (dtp
, f
, p
, kind
);
1560 if (!(compile_options
.allow_std
& GFC_STD_GNU
)
1561 && require_type (dtp
, BT_INTEGER
, type
, f
))
1563 write_o (dtp
, f
, p
, kind
);
1569 if (!(compile_options
.allow_std
& GFC_STD_GNU
)
1570 && require_type (dtp
, BT_INTEGER
, type
, f
))
1572 write_z (dtp
, f
, p
, kind
);
1579 /* It is possible to have FMT_A with something not BT_CHARACTER such
1580 as when writing out hollerith strings, so check both type
1581 and kind before calling wide character routines. */
1582 if (type
== BT_CHARACTER
&& kind
== 4)
1583 write_a_char4 (dtp
, f
, p
, size
);
1585 write_a (dtp
, f
, p
, size
);
1591 write_l (dtp
, f
, p
, kind
);
1597 if (require_type (dtp
, BT_REAL
, type
, f
))
1599 write_d (dtp
, f
, p
, kind
);
1605 if (require_type (dtp
, BT_REAL
, type
, f
))
1607 write_e (dtp
, f
, p
, kind
);
1613 if (require_type (dtp
, BT_REAL
, type
, f
))
1615 write_en (dtp
, f
, p
, kind
);
1621 if (require_type (dtp
, BT_REAL
, type
, f
))
1623 write_es (dtp
, f
, p
, kind
);
1629 if (require_type (dtp
, BT_REAL
, type
, f
))
1631 write_f (dtp
, f
, p
, kind
);
1640 write_i (dtp
, f
, p
, kind
);
1643 write_l (dtp
, f
, p
, kind
);
1647 write_a_char4 (dtp
, f
, p
, size
);
1649 write_a (dtp
, f
, p
, size
);
1652 if (f
->u
.real
.w
== 0)
1653 write_real_g0 (dtp
, p
, kind
, f
->u
.real
.d
);
1655 write_d (dtp
, f
, p
, kind
);
1658 internal_error (&dtp
->common
,
1659 "formatted_transfer(): Bad type");
1664 consume_data_flag
= 0;
1665 write_constant_string (dtp
, f
);
1668 /* Format codes that don't transfer data. */
1671 consume_data_flag
= 0;
1673 dtp
->u
.p
.skips
+= f
->u
.n
;
1674 pos
= bytes_used
+ dtp
->u
.p
.skips
- 1;
1675 dtp
->u
.p
.pending_spaces
= pos
- dtp
->u
.p
.max_pos
+ 1;
1676 /* Writes occur just before the switch on f->format, above, so
1677 that trailing blanks are suppressed, unless we are doing a
1678 non-advancing write in which case we want to output the blanks
1680 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
)
1682 write_x (dtp
, dtp
->u
.p
.skips
, dtp
->u
.p
.pending_spaces
);
1683 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1689 consume_data_flag
= 0;
1691 if (f
->format
== FMT_TL
)
1694 /* Handle the special case when no bytes have been used yet.
1695 Cannot go below zero. */
1696 if (bytes_used
== 0)
1698 dtp
->u
.p
.pending_spaces
-= f
->u
.n
;
1699 dtp
->u
.p
.skips
-= f
->u
.n
;
1700 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
< 0 ? 0 : dtp
->u
.p
.skips
;
1703 pos
= bytes_used
- f
->u
.n
;
1706 pos
= f
->u
.n
- dtp
->u
.p
.pending_spaces
- 1;
1708 /* Standard 10.6.1.1: excessive left tabbing is reset to the
1709 left tab limit. We do not check if the position has gone
1710 beyond the end of record because a subsequent tab could
1711 bring us back again. */
1712 pos
= pos
< 0 ? 0 : pos
;
1714 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
+ pos
- bytes_used
;
1715 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
1716 + pos
- dtp
->u
.p
.max_pos
;
1717 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
< 0
1718 ? 0 : dtp
->u
.p
.pending_spaces
;
1722 consume_data_flag
= 0;
1723 dtp
->u
.p
.sign_status
= SIGN_S
;
1727 consume_data_flag
= 0;
1728 dtp
->u
.p
.sign_status
= SIGN_SS
;
1732 consume_data_flag
= 0;
1733 dtp
->u
.p
.sign_status
= SIGN_SP
;
1737 consume_data_flag
= 0 ;
1738 dtp
->u
.p
.blank_status
= BLANK_NULL
;
1742 consume_data_flag
= 0;
1743 dtp
->u
.p
.blank_status
= BLANK_ZERO
;
1747 consume_data_flag
= 0;
1748 dtp
->u
.p
.current_unit
->decimal_status
= DECIMAL_COMMA
;
1752 consume_data_flag
= 0;
1753 dtp
->u
.p
.current_unit
->decimal_status
= DECIMAL_POINT
;
1757 consume_data_flag
= 0;
1758 dtp
->u
.p
.current_unit
->round_status
= ROUND_COMPATIBLE
;
1762 consume_data_flag
= 0;
1763 dtp
->u
.p
.current_unit
->round_status
= ROUND_DOWN
;
1767 consume_data_flag
= 0;
1768 dtp
->u
.p
.current_unit
->round_status
= ROUND_NEAREST
;
1772 consume_data_flag
= 0;
1773 dtp
->u
.p
.current_unit
->round_status
= ROUND_PROCDEFINED
;
1777 consume_data_flag
= 0;
1778 dtp
->u
.p
.current_unit
->round_status
= ROUND_UP
;
1782 consume_data_flag
= 0;
1783 dtp
->u
.p
.current_unit
->round_status
= ROUND_ZERO
;
1787 consume_data_flag
= 0;
1788 dtp
->u
.p
.scale_factor
= f
->u
.k
;
1792 consume_data_flag
= 0;
1793 dtp
->u
.p
.seen_dollar
= 1;
1797 consume_data_flag
= 0;
1798 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1799 next_record (dtp
, 0);
1803 /* A colon descriptor causes us to exit this loop (in
1804 particular preventing another / descriptor from being
1805 processed) unless there is another data item to be
1807 consume_data_flag
= 0;
1813 internal_error (&dtp
->common
, "Bad format node");
1816 /* Adjust the item count and data pointer. */
1818 if ((consume_data_flag
> 0) && (n
> 0))
1821 p
= ((char *) p
) + size
;
1824 pos
= (int)(dtp
->u
.p
.current_unit
->recl
- dtp
->u
.p
.current_unit
->bytes_left
);
1825 dtp
->u
.p
.max_pos
= (dtp
->u
.p
.max_pos
> pos
) ? dtp
->u
.p
.max_pos
: pos
;
1830 /* Come here when we need a data descriptor but don't have one. We
1831 push the current format node back onto the input, then return and
1832 let the user program call us back with the data. */
1834 unget_format (dtp
, f
);
1837 /* This function is first called from data_init_transfer to initiate the loop
1838 over each item in the format, transferring data as required. Subsequent
1839 calls to this function occur for each data item foound in the READ/WRITE
1840 statement. The item_count is incremented for each call. Since the first
1841 call is from data_transfer_init, the item_count is always one greater than
1842 the actual count number of the item being transferred. */
1845 formatted_transfer (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
1846 size_t size
, size_t nelems
)
1852 size_t stride
= type
== BT_CHARACTER
?
1853 size
* GFC_SIZE_OF_CHAR_KIND(kind
) : size
;
1854 if (dtp
->u
.p
.mode
== READING
)
1856 /* Big loop over all the elements. */
1857 for (elem
= 0; elem
< nelems
; elem
++)
1859 dtp
->u
.p
.item_count
++;
1860 formatted_transfer_scalar_read (dtp
, type
, tmp
+ stride
*elem
, kind
, size
);
1865 /* Big loop over all the elements. */
1866 for (elem
= 0; elem
< nelems
; elem
++)
1868 dtp
->u
.p
.item_count
++;
1869 formatted_transfer_scalar_write (dtp
, type
, tmp
+ stride
*elem
, kind
, size
);
1875 /* Data transfer entry points. The type of the data entity is
1876 implicit in the subroutine call. This prevents us from having to
1877 share a common enum with the compiler. */
1880 transfer_integer (st_parameter_dt
*dtp
, void *p
, int kind
)
1882 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1884 dtp
->u
.p
.transfer (dtp
, BT_INTEGER
, p
, kind
, kind
, 1);
1888 transfer_integer_write (st_parameter_dt
*dtp
, void *p
, int kind
)
1890 transfer_integer (dtp
, p
, kind
);
1894 transfer_real (st_parameter_dt
*dtp
, void *p
, int kind
)
1897 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1899 size
= size_from_real_kind (kind
);
1900 dtp
->u
.p
.transfer (dtp
, BT_REAL
, p
, kind
, size
, 1);
1904 transfer_real_write (st_parameter_dt
*dtp
, void *p
, int kind
)
1906 transfer_real (dtp
, p
, kind
);
1910 transfer_logical (st_parameter_dt
*dtp
, void *p
, int kind
)
1912 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1914 dtp
->u
.p
.transfer (dtp
, BT_LOGICAL
, p
, kind
, kind
, 1);
1918 transfer_logical_write (st_parameter_dt
*dtp
, void *p
, int kind
)
1920 transfer_logical (dtp
, p
, kind
);
1924 transfer_character (st_parameter_dt
*dtp
, void *p
, int len
)
1926 static char *empty_string
[0];
1928 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1931 /* Strings of zero length can have p == NULL, which confuses the
1932 transfer routines into thinking we need more data elements. To avoid
1933 this, we give them a nice pointer. */
1934 if (len
== 0 && p
== NULL
)
1937 /* Set kind here to 1. */
1938 dtp
->u
.p
.transfer (dtp
, BT_CHARACTER
, p
, 1, len
, 1);
1942 transfer_character_write (st_parameter_dt
*dtp
, void *p
, int len
)
1944 transfer_character (dtp
, p
, len
);
1948 transfer_character_wide (st_parameter_dt
*dtp
, void *p
, int len
, int kind
)
1950 static char *empty_string
[0];
1952 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1955 /* Strings of zero length can have p == NULL, which confuses the
1956 transfer routines into thinking we need more data elements. To avoid
1957 this, we give them a nice pointer. */
1958 if (len
== 0 && p
== NULL
)
1961 /* Here we pass the actual kind value. */
1962 dtp
->u
.p
.transfer (dtp
, BT_CHARACTER
, p
, kind
, len
, 1);
1966 transfer_character_wide_write (st_parameter_dt
*dtp
, void *p
, int len
, int kind
)
1968 transfer_character_wide (dtp
, p
, len
, kind
);
1972 transfer_complex (st_parameter_dt
*dtp
, void *p
, int kind
)
1975 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1977 size
= size_from_complex_kind (kind
);
1978 dtp
->u
.p
.transfer (dtp
, BT_COMPLEX
, p
, kind
, size
, 1);
1982 transfer_complex_write (st_parameter_dt
*dtp
, void *p
, int kind
)
1984 transfer_complex (dtp
, p
, kind
);
1988 transfer_array (st_parameter_dt
*dtp
, gfc_array_char
*desc
, int kind
,
1989 gfc_charlen_type charlen
)
1991 index_type count
[GFC_MAX_DIMENSIONS
];
1992 index_type extent
[GFC_MAX_DIMENSIONS
];
1993 index_type stride
[GFC_MAX_DIMENSIONS
];
1994 index_type stride0
, rank
, size
, n
;
1999 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
2002 iotype
= (bt
) GFC_DESCRIPTOR_TYPE (desc
);
2003 size
= iotype
== BT_CHARACTER
? charlen
: GFC_DESCRIPTOR_SIZE (desc
);
2005 rank
= GFC_DESCRIPTOR_RANK (desc
);
2006 for (n
= 0; n
< rank
; n
++)
2009 stride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(desc
,n
);
2010 extent
[n
] = GFC_DESCRIPTOR_EXTENT(desc
,n
);
2012 /* If the extent of even one dimension is zero, then the entire
2013 array section contains zero elements, so we return after writing
2014 a zero array record. */
2019 dtp
->u
.p
.transfer (dtp
, iotype
, data
, kind
, size
, tsize
);
2024 stride0
= stride
[0];
2026 /* If the innermost dimension has a stride of 1, we can do the transfer
2027 in contiguous chunks. */
2028 if (stride0
== size
)
2033 data
= GFC_DESCRIPTOR_DATA (desc
);
2037 dtp
->u
.p
.transfer (dtp
, iotype
, data
, kind
, size
, tsize
);
2038 data
+= stride0
* tsize
;
2041 while (count
[n
] == extent
[n
])
2044 data
-= stride
[n
] * extent
[n
];
2061 transfer_array_write (st_parameter_dt
*dtp
, gfc_array_char
*desc
, int kind
,
2062 gfc_charlen_type charlen
)
2064 transfer_array (dtp
, desc
, kind
, charlen
);
2067 /* Preposition a sequential unformatted file while reading. */
2070 us_read (st_parameter_dt
*dtp
, int continued
)
2077 if (compile_options
.record_marker
== 0)
2078 n
= sizeof (GFC_INTEGER_4
);
2080 n
= compile_options
.record_marker
;
2082 nr
= sread (dtp
->u
.p
.current_unit
->s
, &i
, n
);
2083 if (unlikely (nr
< 0))
2085 generate_error (&dtp
->common
, LIBERROR_BAD_US
, NULL
);
2091 return; /* end of file */
2093 else if (unlikely (n
!= nr
))
2095 generate_error (&dtp
->common
, LIBERROR_BAD_US
, NULL
);
2099 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
2100 if (likely (dtp
->u
.p
.current_unit
->flags
.convert
== GFC_CONVERT_NATIVE
))
2104 case sizeof(GFC_INTEGER_4
):
2105 memcpy (&i4
, &i
, sizeof (i4
));
2109 case sizeof(GFC_INTEGER_8
):
2110 memcpy (&i8
, &i
, sizeof (i8
));
2115 runtime_error ("Illegal value for record marker");
2122 case sizeof(GFC_INTEGER_4
):
2123 reverse_memcpy (&i4
, &i
, sizeof (i4
));
2127 case sizeof(GFC_INTEGER_8
):
2128 reverse_memcpy (&i8
, &i
, sizeof (i8
));
2133 runtime_error ("Illegal value for record marker");
2139 dtp
->u
.p
.current_unit
->bytes_left_subrecord
= i
;
2140 dtp
->u
.p
.current_unit
->continued
= 0;
2144 dtp
->u
.p
.current_unit
->bytes_left_subrecord
= -i
;
2145 dtp
->u
.p
.current_unit
->continued
= 1;
2149 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2153 /* Preposition a sequential unformatted file while writing. This
2154 amount to writing a bogus length that will be filled in later. */
2157 us_write (st_parameter_dt
*dtp
, int continued
)
2164 if (compile_options
.record_marker
== 0)
2165 nbytes
= sizeof (GFC_INTEGER_4
);
2167 nbytes
= compile_options
.record_marker
;
2169 if (swrite (dtp
->u
.p
.current_unit
->s
, &dummy
, nbytes
) != nbytes
)
2170 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2172 /* For sequential unformatted, if RECL= was not specified in the OPEN
2173 we write until we have more bytes than can fit in the subrecord
2174 markers, then we write a new subrecord. */
2176 dtp
->u
.p
.current_unit
->bytes_left_subrecord
=
2177 dtp
->u
.p
.current_unit
->recl_subrecord
;
2178 dtp
->u
.p
.current_unit
->continued
= continued
;
2182 /* Position to the next record prior to transfer. We are assumed to
2183 be before the next record. We also calculate the bytes in the next
2187 pre_position (st_parameter_dt
*dtp
)
2189 if (dtp
->u
.p
.current_unit
->current_record
)
2190 return; /* Already positioned. */
2192 switch (current_mode (dtp
))
2194 case FORMATTED_STREAM
:
2195 case UNFORMATTED_STREAM
:
2196 /* There are no records with stream I/O. If the position was specified
2197 data_transfer_init has already positioned the file. If no position
2198 was specified, we continue from where we last left off. I.e.
2199 there is nothing to do here. */
2202 case UNFORMATTED_SEQUENTIAL
:
2203 if (dtp
->u
.p
.mode
== READING
)
2210 case FORMATTED_SEQUENTIAL
:
2211 case FORMATTED_DIRECT
:
2212 case UNFORMATTED_DIRECT
:
2213 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2217 dtp
->u
.p
.current_unit
->current_record
= 1;
2221 /* Initialize things for a data transfer. This code is common for
2222 both reading and writing. */
2225 data_transfer_init (st_parameter_dt
*dtp
, int read_flag
)
2227 unit_flags u_flags
; /* Used for creating a unit if needed. */
2228 GFC_INTEGER_4 cf
= dtp
->common
.flags
;
2229 namelist_info
*ionml
;
2231 ionml
= ((cf
& IOPARM_DT_IONML_SET
) != 0) ? dtp
->u
.p
.ionml
: NULL
;
2233 memset (&dtp
->u
.p
, 0, sizeof (dtp
->u
.p
));
2235 dtp
->u
.p
.ionml
= ionml
;
2236 dtp
->u
.p
.mode
= read_flag
? READING
: WRITING
;
2238 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
2241 if ((cf
& IOPARM_DT_HAS_SIZE
) != 0)
2242 dtp
->u
.p
.size_used
= 0; /* Initialize the count. */
2244 dtp
->u
.p
.current_unit
= get_unit (dtp
, 1);
2245 if (dtp
->u
.p
.current_unit
->s
== NULL
)
2246 { /* Open the unit with some default flags. */
2247 st_parameter_open opp
;
2250 if (dtp
->common
.unit
< 0)
2252 close_unit (dtp
->u
.p
.current_unit
);
2253 dtp
->u
.p
.current_unit
= NULL
;
2254 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2255 "Bad unit number in statement");
2258 memset (&u_flags
, '\0', sizeof (u_flags
));
2259 u_flags
.access
= ACCESS_SEQUENTIAL
;
2260 u_flags
.action
= ACTION_READWRITE
;
2262 /* Is it unformatted? */
2263 if (!(cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
2264 | IOPARM_DT_IONML_SET
)))
2265 u_flags
.form
= FORM_UNFORMATTED
;
2267 u_flags
.form
= FORM_UNSPECIFIED
;
2269 u_flags
.delim
= DELIM_UNSPECIFIED
;
2270 u_flags
.blank
= BLANK_UNSPECIFIED
;
2271 u_flags
.pad
= PAD_UNSPECIFIED
;
2272 u_flags
.decimal
= DECIMAL_UNSPECIFIED
;
2273 u_flags
.encoding
= ENCODING_UNSPECIFIED
;
2274 u_flags
.async
= ASYNC_UNSPECIFIED
;
2275 u_flags
.round
= ROUND_UNSPECIFIED
;
2276 u_flags
.sign
= SIGN_UNSPECIFIED
;
2278 u_flags
.status
= STATUS_UNKNOWN
;
2280 conv
= get_unformatted_convert (dtp
->common
.unit
);
2282 if (conv
== GFC_CONVERT_NONE
)
2283 conv
= compile_options
.convert
;
2285 /* We use big_endian, which is 0 on little-endian machines
2286 and 1 on big-endian machines. */
2289 case GFC_CONVERT_NATIVE
:
2290 case GFC_CONVERT_SWAP
:
2293 case GFC_CONVERT_BIG
:
2294 conv
= big_endian
? GFC_CONVERT_NATIVE
: GFC_CONVERT_SWAP
;
2297 case GFC_CONVERT_LITTLE
:
2298 conv
= big_endian
? GFC_CONVERT_SWAP
: GFC_CONVERT_NATIVE
;
2302 internal_error (&opp
.common
, "Illegal value for CONVERT");
2306 u_flags
.convert
= conv
;
2308 opp
.common
= dtp
->common
;
2309 opp
.common
.flags
&= IOPARM_COMMON_MASK
;
2310 dtp
->u
.p
.current_unit
= new_unit (&opp
, dtp
->u
.p
.current_unit
, &u_flags
);
2311 dtp
->common
.flags
&= ~IOPARM_COMMON_MASK
;
2312 dtp
->common
.flags
|= (opp
.common
.flags
& IOPARM_COMMON_MASK
);
2313 if (dtp
->u
.p
.current_unit
== NULL
)
2317 /* Check the action. */
2319 if (read_flag
&& dtp
->u
.p
.current_unit
->flags
.action
== ACTION_WRITE
)
2321 generate_error (&dtp
->common
, LIBERROR_BAD_ACTION
,
2322 "Cannot read from file opened for WRITE");
2326 if (!read_flag
&& dtp
->u
.p
.current_unit
->flags
.action
== ACTION_READ
)
2328 generate_error (&dtp
->common
, LIBERROR_BAD_ACTION
,
2329 "Cannot write to file opened for READ");
2333 dtp
->u
.p
.first_item
= 1;
2335 /* Check the format. */
2337 if ((cf
& IOPARM_DT_HAS_FORMAT
) != 0)
2340 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
2341 && (cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
))
2344 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2345 "Format present for UNFORMATTED data transfer");
2349 if ((cf
& IOPARM_DT_HAS_NAMELIST_NAME
) != 0 && dtp
->u
.p
.ionml
!= NULL
)
2351 if ((cf
& IOPARM_DT_HAS_FORMAT
) != 0)
2352 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2353 "A format cannot be specified with a namelist");
2355 else if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
&&
2356 !(cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
)))
2358 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2359 "Missing format for FORMATTED data transfer");
2362 if (is_internal_unit (dtp
)
2363 && dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
)
2365 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2366 "Internal file cannot be accessed by UNFORMATTED "
2371 /* Check the record or position number. */
2373 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
2374 && (cf
& IOPARM_DT_HAS_REC
) == 0)
2376 generate_error (&dtp
->common
, LIBERROR_MISSING_OPTION
,
2377 "Direct access data transfer requires record number");
2381 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
2383 if ((cf
& IOPARM_DT_HAS_REC
) != 0)
2385 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2386 "Record number not allowed for sequential access "
2391 if (dtp
->u
.p
.current_unit
->endfile
== AFTER_ENDFILE
)
2393 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2394 "Sequential READ or WRITE not allowed after "
2395 "EOF marker, possibly use REWIND or BACKSPACE");
2400 /* Process the ADVANCE option. */
2402 dtp
->u
.p
.advance_status
2403 = !(cf
& IOPARM_DT_HAS_ADVANCE
) ? ADVANCE_UNSPECIFIED
:
2404 find_option (&dtp
->common
, dtp
->advance
, dtp
->advance_len
, advance_opt
,
2405 "Bad ADVANCE parameter in data transfer statement");
2407 if (dtp
->u
.p
.advance_status
!= ADVANCE_UNSPECIFIED
)
2409 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
2411 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2412 "ADVANCE specification conflicts with sequential "
2417 if (is_internal_unit (dtp
))
2419 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2420 "ADVANCE specification conflicts with internal file");
2424 if ((cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
))
2425 != IOPARM_DT_HAS_FORMAT
)
2427 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2428 "ADVANCE specification requires an explicit format");
2435 dtp
->u
.p
.current_unit
->previous_nonadvancing_write
= 0;
2437 if ((cf
& IOPARM_EOR
) != 0 && dtp
->u
.p
.advance_status
!= ADVANCE_NO
)
2439 generate_error (&dtp
->common
, LIBERROR_MISSING_OPTION
,
2440 "EOR specification requires an ADVANCE specification "
2445 if ((cf
& IOPARM_DT_HAS_SIZE
) != 0
2446 && dtp
->u
.p
.advance_status
!= ADVANCE_NO
)
2448 generate_error (&dtp
->common
, LIBERROR_MISSING_OPTION
,
2449 "SIZE specification requires an ADVANCE "
2450 "specification of NO");
2455 { /* Write constraints. */
2456 if ((cf
& IOPARM_END
) != 0)
2458 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2459 "END specification cannot appear in a write "
2464 if ((cf
& IOPARM_EOR
) != 0)
2466 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2467 "EOR specification cannot appear in a write "
2472 if ((cf
& IOPARM_DT_HAS_SIZE
) != 0)
2474 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2475 "SIZE specification cannot appear in a write "
2481 if (dtp
->u
.p
.advance_status
== ADVANCE_UNSPECIFIED
)
2482 dtp
->u
.p
.advance_status
= ADVANCE_YES
;
2484 /* Check the decimal mode. */
2485 dtp
->u
.p
.current_unit
->decimal_status
2486 = !(cf
& IOPARM_DT_HAS_DECIMAL
) ? DECIMAL_UNSPECIFIED
:
2487 find_option (&dtp
->common
, dtp
->decimal
, dtp
->decimal_len
,
2488 decimal_opt
, "Bad DECIMAL parameter in data transfer "
2491 if (dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_UNSPECIFIED
)
2492 dtp
->u
.p
.current_unit
->decimal_status
= dtp
->u
.p
.current_unit
->flags
.decimal
;
2494 /* Check the round mode. */
2495 dtp
->u
.p
.current_unit
->round_status
2496 = !(cf
& IOPARM_DT_HAS_ROUND
) ? ROUND_UNSPECIFIED
:
2497 find_option (&dtp
->common
, dtp
->round
, dtp
->round_len
,
2498 round_opt
, "Bad ROUND parameter in data transfer "
2501 if (dtp
->u
.p
.current_unit
->round_status
== ROUND_UNSPECIFIED
)
2502 dtp
->u
.p
.current_unit
->round_status
= dtp
->u
.p
.current_unit
->flags
.round
;
2504 /* Check the sign mode. */
2505 dtp
->u
.p
.sign_status
2506 = !(cf
& IOPARM_DT_HAS_SIGN
) ? SIGN_UNSPECIFIED
:
2507 find_option (&dtp
->common
, dtp
->sign
, dtp
->sign_len
, sign_opt
,
2508 "Bad SIGN parameter in data transfer statement");
2510 if (dtp
->u
.p
.sign_status
== SIGN_UNSPECIFIED
)
2511 dtp
->u
.p
.sign_status
= dtp
->u
.p
.current_unit
->flags
.sign
;
2513 /* Check the blank mode. */
2514 dtp
->u
.p
.blank_status
2515 = !(cf
& IOPARM_DT_HAS_BLANK
) ? BLANK_UNSPECIFIED
:
2516 find_option (&dtp
->common
, dtp
->blank
, dtp
->blank_len
,
2518 "Bad BLANK parameter in data transfer statement");
2520 if (dtp
->u
.p
.blank_status
== BLANK_UNSPECIFIED
)
2521 dtp
->u
.p
.blank_status
= dtp
->u
.p
.current_unit
->flags
.blank
;
2523 /* Check the delim mode. */
2524 dtp
->u
.p
.current_unit
->delim_status
2525 = !(cf
& IOPARM_DT_HAS_DELIM
) ? DELIM_UNSPECIFIED
:
2526 find_option (&dtp
->common
, dtp
->delim
, dtp
->delim_len
,
2527 delim_opt
, "Bad DELIM parameter in data transfer statement");
2529 if (dtp
->u
.p
.current_unit
->delim_status
== DELIM_UNSPECIFIED
)
2530 dtp
->u
.p
.current_unit
->delim_status
= dtp
->u
.p
.current_unit
->flags
.delim
;
2532 /* Check the pad mode. */
2533 dtp
->u
.p
.current_unit
->pad_status
2534 = !(cf
& IOPARM_DT_HAS_PAD
) ? PAD_UNSPECIFIED
:
2535 find_option (&dtp
->common
, dtp
->pad
, dtp
->pad_len
, pad_opt
,
2536 "Bad PAD parameter in data transfer statement");
2538 if (dtp
->u
.p
.current_unit
->pad_status
== PAD_UNSPECIFIED
)
2539 dtp
->u
.p
.current_unit
->pad_status
= dtp
->u
.p
.current_unit
->flags
.pad
;
2541 /* Check to see if we might be reading what we wrote before */
2543 if (dtp
->u
.p
.mode
!= dtp
->u
.p
.current_unit
->mode
2544 && !is_internal_unit (dtp
))
2546 int pos
= fbuf_reset (dtp
->u
.p
.current_unit
);
2548 sseek (dtp
->u
.p
.current_unit
->s
, pos
, SEEK_CUR
);
2549 sflush(dtp
->u
.p
.current_unit
->s
);
2552 /* Check the POS= specifier: that it is in range and that it is used with a
2553 unit that has been connected for STREAM access. F2003 9.5.1.10. */
2555 if (((cf
& IOPARM_DT_HAS_POS
) != 0))
2557 if (is_stream_io (dtp
))
2562 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2563 "POS=specifier must be positive");
2567 if (dtp
->pos
>= dtp
->u
.p
.current_unit
->maxrec
)
2569 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2570 "POS=specifier too large");
2574 dtp
->rec
= dtp
->pos
;
2576 if (dtp
->u
.p
.mode
== READING
)
2578 /* Reset the endfile flag; if we hit EOF during reading
2579 we'll set the flag and generate an error at that point
2580 rather than worrying about it here. */
2581 dtp
->u
.p
.current_unit
->endfile
= NO_ENDFILE
;
2584 if (dtp
->pos
!= dtp
->u
.p
.current_unit
->strm_pos
)
2586 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
2587 if (sseek (dtp
->u
.p
.current_unit
->s
, dtp
->pos
- 1, SEEK_SET
) < 0)
2589 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2592 dtp
->u
.p
.current_unit
->strm_pos
= dtp
->pos
;
2597 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2598 "POS=specifier not allowed, "
2599 "Try OPEN with ACCESS='stream'");
2605 /* Sanity checks on the record number. */
2606 if ((cf
& IOPARM_DT_HAS_REC
) != 0)
2610 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2611 "Record number must be positive");
2615 if (dtp
->rec
>= dtp
->u
.p
.current_unit
->maxrec
)
2617 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2618 "Record number too large");
2622 /* Make sure format buffer is reset. */
2623 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
)
2624 fbuf_reset (dtp
->u
.p
.current_unit
);
2627 /* Check whether the record exists to be read. Only
2628 a partial record needs to exist. */
2630 if (dtp
->u
.p
.mode
== READING
&& (dtp
->rec
- 1)
2631 * dtp
->u
.p
.current_unit
->recl
>= file_length (dtp
->u
.p
.current_unit
->s
))
2633 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2634 "Non-existing record number");
2638 /* Position the file. */
2639 if (sseek (dtp
->u
.p
.current_unit
->s
, (gfc_offset
) (dtp
->rec
- 1)
2640 * dtp
->u
.p
.current_unit
->recl
, SEEK_SET
) < 0)
2642 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2646 /* TODO: This is required to maintain compatibility between
2647 4.3 and 4.4 runtime. Remove when ABI changes from 4.3 */
2649 if (is_stream_io (dtp
))
2650 dtp
->u
.p
.current_unit
->strm_pos
= dtp
->rec
;
2652 /* TODO: Un-comment this code when ABI changes from 4.3.
2653 if (dtp->u.p.current_unit->flags.access == ACCESS_STREAM)
2655 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2656 "Record number not allowed for stream access "
2662 /* Bugware for badly written mixed C-Fortran I/O. */
2663 if (!is_internal_unit (dtp
))
2664 flush_if_preconnected(dtp
->u
.p
.current_unit
->s
);
2666 dtp
->u
.p
.current_unit
->mode
= dtp
->u
.p
.mode
;
2668 /* Set the maximum position reached from the previous I/O operation. This
2669 could be greater than zero from a previous non-advancing write. */
2670 dtp
->u
.p
.max_pos
= dtp
->u
.p
.current_unit
->saved_pos
;
2675 /* Set up the subroutine that will handle the transfers. */
2679 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
)
2680 dtp
->u
.p
.transfer
= unformatted_read
;
2683 if ((cf
& IOPARM_DT_LIST_FORMAT
) != 0)
2685 dtp
->u
.p
.last_char
= EOF
- 1;
2686 dtp
->u
.p
.transfer
= list_formatted_read
;
2689 dtp
->u
.p
.transfer
= formatted_transfer
;
2694 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
)
2695 dtp
->u
.p
.transfer
= unformatted_write
;
2698 if ((cf
& IOPARM_DT_LIST_FORMAT
) != 0)
2699 dtp
->u
.p
.transfer
= list_formatted_write
;
2701 dtp
->u
.p
.transfer
= formatted_transfer
;
2705 /* Make sure that we don't do a read after a nonadvancing write. */
2709 if (dtp
->u
.p
.current_unit
->read_bad
&& !is_stream_io (dtp
))
2711 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2712 "Cannot READ after a nonadvancing WRITE");
2718 if (dtp
->u
.p
.advance_status
== ADVANCE_YES
&& !dtp
->u
.p
.seen_dollar
)
2719 dtp
->u
.p
.current_unit
->read_bad
= 1;
2722 /* Start the data transfer if we are doing a formatted transfer. */
2723 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
2724 && ((cf
& (IOPARM_DT_LIST_FORMAT
| IOPARM_DT_HAS_NAMELIST_NAME
)) == 0)
2725 && dtp
->u
.p
.ionml
== NULL
)
2726 formatted_transfer (dtp
, 0, NULL
, 0, 0, 1);
2729 /* Initialize an array_loop_spec given the array descriptor. The function
2730 returns the index of the last element of the array, and also returns
2731 starting record, where the first I/O goes to (necessary in case of
2732 negative strides). */
2735 init_loop_spec (gfc_array_char
*desc
, array_loop_spec
*ls
,
2736 gfc_offset
*start_record
)
2738 int rank
= GFC_DESCRIPTOR_RANK(desc
);
2747 for (i
=0; i
<rank
; i
++)
2749 ls
[i
].idx
= GFC_DESCRIPTOR_LBOUND(desc
,i
);
2750 ls
[i
].start
= GFC_DESCRIPTOR_LBOUND(desc
,i
);
2751 ls
[i
].end
= GFC_DESCRIPTOR_UBOUND(desc
,i
);
2752 ls
[i
].step
= GFC_DESCRIPTOR_STRIDE(desc
,i
);
2753 empty
= empty
|| (GFC_DESCRIPTOR_UBOUND(desc
,i
)
2754 < GFC_DESCRIPTOR_LBOUND(desc
,i
));
2756 if (GFC_DESCRIPTOR_STRIDE(desc
,i
) > 0)
2758 index
+= (GFC_DESCRIPTOR_EXTENT(desc
,i
) - 1)
2759 * GFC_DESCRIPTOR_STRIDE(desc
,i
);
2763 index
-= (GFC_DESCRIPTOR_EXTENT(desc
,i
) - 1)
2764 * GFC_DESCRIPTOR_STRIDE(desc
,i
);
2765 *start_record
-= (GFC_DESCRIPTOR_EXTENT(desc
,i
) - 1)
2766 * GFC_DESCRIPTOR_STRIDE(desc
,i
);
2776 /* Determine the index to the next record in an internal unit array by
2777 by incrementing through the array_loop_spec. */
2780 next_array_record (st_parameter_dt
*dtp
, array_loop_spec
*ls
, int *finished
)
2788 for (i
= 0; i
< dtp
->u
.p
.current_unit
->rank
; i
++)
2793 if (ls
[i
].idx
> ls
[i
].end
)
2795 ls
[i
].idx
= ls
[i
].start
;
2801 index
= index
+ (ls
[i
].idx
- ls
[i
].start
) * ls
[i
].step
;
2811 /* Skip to the end of the current record, taking care of an optional
2812 record marker of size bytes. If the file is not seekable, we
2813 read chunks of size MAX_READ until we get to the right
2817 skip_record (st_parameter_dt
*dtp
, ssize_t bytes
)
2819 ssize_t rlength
, readb
;
2820 static const ssize_t MAX_READ
= 4096;
2823 dtp
->u
.p
.current_unit
->bytes_left_subrecord
+= bytes
;
2824 if (dtp
->u
.p
.current_unit
->bytes_left_subrecord
== 0)
2827 if (is_seekable (dtp
->u
.p
.current_unit
->s
))
2829 /* Direct access files do not generate END conditions,
2831 if (sseek (dtp
->u
.p
.current_unit
->s
,
2832 dtp
->u
.p
.current_unit
->bytes_left_subrecord
, SEEK_CUR
) < 0)
2833 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2835 dtp
->u
.p
.current_unit
->bytes_left_subrecord
= 0;
2838 { /* Seek by reading data. */
2839 while (dtp
->u
.p
.current_unit
->bytes_left_subrecord
> 0)
2842 (MAX_READ
< dtp
->u
.p
.current_unit
->bytes_left_subrecord
) ?
2843 MAX_READ
: dtp
->u
.p
.current_unit
->bytes_left_subrecord
;
2845 readb
= sread (dtp
->u
.p
.current_unit
->s
, p
, rlength
);
2848 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2852 dtp
->u
.p
.current_unit
->bytes_left_subrecord
-= readb
;
2859 /* Advance to the next record reading unformatted files, taking
2860 care of subrecords. If complete_record is nonzero, we loop
2861 until all subrecords are cleared. */
2864 next_record_r_unf (st_parameter_dt
*dtp
, int complete_record
)
2868 bytes
= compile_options
.record_marker
== 0 ?
2869 sizeof (GFC_INTEGER_4
) : compile_options
.record_marker
;
2874 /* Skip over tail */
2876 skip_record (dtp
, bytes
);
2878 if ( ! (complete_record
&& dtp
->u
.p
.current_unit
->continued
))
2886 static inline gfc_offset
2887 min_off (gfc_offset a
, gfc_offset b
)
2889 return (a
< b
? a
: b
);
2893 /* Space to the next record for read mode. */
2896 next_record_r (st_parameter_dt
*dtp
, int done
)
2903 switch (current_mode (dtp
))
2905 /* No records in unformatted STREAM I/O. */
2906 case UNFORMATTED_STREAM
:
2909 case UNFORMATTED_SEQUENTIAL
:
2910 next_record_r_unf (dtp
, 1);
2911 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2914 case FORMATTED_DIRECT
:
2915 case UNFORMATTED_DIRECT
:
2916 skip_record (dtp
, dtp
->u
.p
.current_unit
->bytes_left
);
2919 case FORMATTED_STREAM
:
2920 case FORMATTED_SEQUENTIAL
:
2921 /* read_sf has already terminated input because of an '\n', or
2923 if (dtp
->u
.p
.sf_seen_eor
)
2925 dtp
->u
.p
.sf_seen_eor
= 0;
2929 if (is_internal_unit (dtp
))
2931 if (is_array_io (dtp
))
2935 record
= next_array_record (dtp
, dtp
->u
.p
.current_unit
->ls
,
2937 if (!done
&& finished
)
2940 /* Now seek to this record. */
2941 record
= record
* dtp
->u
.p
.current_unit
->recl
;
2942 if (sseek (dtp
->u
.p
.current_unit
->s
, record
, SEEK_SET
) < 0)
2944 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
2947 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2951 bytes_left
= (int) dtp
->u
.p
.current_unit
->bytes_left
;
2952 bytes_left
= min_off (bytes_left
,
2953 file_length (dtp
->u
.p
.current_unit
->s
)
2954 - stell (dtp
->u
.p
.current_unit
->s
));
2955 if (sseek (dtp
->u
.p
.current_unit
->s
,
2956 bytes_left
, SEEK_CUR
) < 0)
2958 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
2961 dtp
->u
.p
.current_unit
->bytes_left
2962 = dtp
->u
.p
.current_unit
->recl
;
2971 cc
= fbuf_getc (dtp
->u
.p
.current_unit
);
2975 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2978 if (is_stream_io (dtp
)
2979 || dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
2980 || dtp
->u
.p
.current_unit
->bytes_left
2981 == dtp
->u
.p
.current_unit
->recl
)
2987 if (is_stream_io (dtp
))
2988 dtp
->u
.p
.current_unit
->strm_pos
++;
2999 /* Small utility function to write a record marker, taking care of
3000 byte swapping and of choosing the correct size. */
3003 write_us_marker (st_parameter_dt
*dtp
, const gfc_offset buf
)
3008 char p
[sizeof (GFC_INTEGER_8
)];
3010 if (compile_options
.record_marker
== 0)
3011 len
= sizeof (GFC_INTEGER_4
);
3013 len
= compile_options
.record_marker
;
3015 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
3016 if (likely (dtp
->u
.p
.current_unit
->flags
.convert
== GFC_CONVERT_NATIVE
))
3020 case sizeof (GFC_INTEGER_4
):
3022 return swrite (dtp
->u
.p
.current_unit
->s
, &buf4
, len
);
3025 case sizeof (GFC_INTEGER_8
):
3027 return swrite (dtp
->u
.p
.current_unit
->s
, &buf8
, len
);
3031 runtime_error ("Illegal value for record marker");
3039 case sizeof (GFC_INTEGER_4
):
3041 reverse_memcpy (p
, &buf4
, sizeof (GFC_INTEGER_4
));
3042 return swrite (dtp
->u
.p
.current_unit
->s
, p
, len
);
3045 case sizeof (GFC_INTEGER_8
):
3047 reverse_memcpy (p
, &buf8
, sizeof (GFC_INTEGER_8
));
3048 return swrite (dtp
->u
.p
.current_unit
->s
, p
, len
);
3052 runtime_error ("Illegal value for record marker");
3059 /* Position to the next (sub)record in write mode for
3060 unformatted sequential files. */
3063 next_record_w_unf (st_parameter_dt
*dtp
, int next_subrecord
)
3065 gfc_offset m
, m_write
, record_marker
;
3067 /* Bytes written. */
3068 m
= dtp
->u
.p
.current_unit
->recl_subrecord
3069 - dtp
->u
.p
.current_unit
->bytes_left_subrecord
;
3071 /* Write the length tail. If we finish a record containing
3072 subrecords, we write out the negative length. */
3074 if (dtp
->u
.p
.current_unit
->continued
)
3079 if (unlikely (write_us_marker (dtp
, m_write
) < 0))
3082 if (compile_options
.record_marker
== 0)
3083 record_marker
= sizeof (GFC_INTEGER_4
);
3085 record_marker
= compile_options
.record_marker
;
3087 /* Seek to the head and overwrite the bogus length with the real
3090 if (unlikely (sseek (dtp
->u
.p
.current_unit
->s
, - m
- 2 * record_marker
,
3099 if (unlikely (write_us_marker (dtp
, m_write
) < 0))
3102 /* Seek past the end of the current record. */
3104 if (unlikely (sseek (dtp
->u
.p
.current_unit
->s
, m
+ record_marker
,
3111 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
3117 /* Utility function like memset() but operating on streams. Return
3118 value is same as for POSIX write(). */
3121 sset (stream
* s
, int c
, ssize_t nbyte
)
3123 static const int WRITE_CHUNK
= 256;
3124 char p
[WRITE_CHUNK
];
3125 ssize_t bytes_left
, trans
;
3127 if (nbyte
< WRITE_CHUNK
)
3128 memset (p
, c
, nbyte
);
3130 memset (p
, c
, WRITE_CHUNK
);
3133 while (bytes_left
> 0)
3135 trans
= (bytes_left
< WRITE_CHUNK
) ? bytes_left
: WRITE_CHUNK
;
3136 trans
= swrite (s
, p
, trans
);
3139 bytes_left
-= trans
;
3142 return nbyte
- bytes_left
;
3146 memset4 (gfc_char4_t
*p
, gfc_char4_t c
, int k
)
3149 for (j
= 0; j
< k
; j
++)
3153 /* Position to the next record in write mode. */
3156 next_record_w (st_parameter_dt
*dtp
, int done
)
3158 gfc_offset m
, record
, max_pos
;
3161 /* Zero counters for X- and T-editing. */
3162 max_pos
= dtp
->u
.p
.max_pos
;
3163 dtp
->u
.p
.max_pos
= dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
3165 switch (current_mode (dtp
))
3167 /* No records in unformatted STREAM I/O. */
3168 case UNFORMATTED_STREAM
:
3171 case FORMATTED_DIRECT
:
3172 if (dtp
->u
.p
.current_unit
->bytes_left
== 0)
3175 fbuf_seek (dtp
->u
.p
.current_unit
, 0, SEEK_END
);
3176 fbuf_flush (dtp
->u
.p
.current_unit
, WRITING
);
3177 if (sset (dtp
->u
.p
.current_unit
->s
, ' ',
3178 dtp
->u
.p
.current_unit
->bytes_left
)
3179 != dtp
->u
.p
.current_unit
->bytes_left
)
3184 case UNFORMATTED_DIRECT
:
3185 if (dtp
->u
.p
.current_unit
->bytes_left
> 0)
3187 length
= (int) dtp
->u
.p
.current_unit
->bytes_left
;
3188 if (sset (dtp
->u
.p
.current_unit
->s
, 0, length
) != length
)
3193 case UNFORMATTED_SEQUENTIAL
:
3194 next_record_w_unf (dtp
, 0);
3195 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
3198 case FORMATTED_STREAM
:
3199 case FORMATTED_SEQUENTIAL
:
3201 if (is_internal_unit (dtp
))
3204 if (is_array_io (dtp
))
3208 length
= (int) dtp
->u
.p
.current_unit
->bytes_left
;
3210 /* If the farthest position reached is greater than current
3211 position, adjust the position and set length to pad out
3212 whats left. Otherwise just pad whats left.
3213 (for character array unit) */
3214 m
= dtp
->u
.p
.current_unit
->recl
3215 - dtp
->u
.p
.current_unit
->bytes_left
;
3218 length
= (int) (max_pos
- m
);
3219 if (sseek (dtp
->u
.p
.current_unit
->s
,
3220 length
, SEEK_CUR
) < 0)
3222 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
3225 length
= (int) (dtp
->u
.p
.current_unit
->recl
- max_pos
);
3228 p
= write_block (dtp
, length
);
3232 if (unlikely (is_char4_unit (dtp
)))
3234 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
3235 memset4 (p4
, ' ', length
);
3238 memset (p
, ' ', length
);
3240 /* Now that the current record has been padded out,
3241 determine where the next record in the array is. */
3242 record
= next_array_record (dtp
, dtp
->u
.p
.current_unit
->ls
,
3245 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
3247 /* Now seek to this record */
3248 record
= record
* dtp
->u
.p
.current_unit
->recl
;
3250 if (sseek (dtp
->u
.p
.current_unit
->s
, record
, SEEK_SET
) < 0)
3252 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
3256 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
3262 /* If this is the last call to next_record move to the farthest
3263 position reached and set length to pad out the remainder
3264 of the record. (for character scaler unit) */
3267 m
= dtp
->u
.p
.current_unit
->recl
3268 - dtp
->u
.p
.current_unit
->bytes_left
;
3271 length
= (int) (max_pos
- m
);
3272 if (sseek (dtp
->u
.p
.current_unit
->s
,
3273 length
, SEEK_CUR
) < 0)
3275 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
3278 length
= (int) (dtp
->u
.p
.current_unit
->recl
- max_pos
);
3281 length
= (int) dtp
->u
.p
.current_unit
->bytes_left
;
3285 p
= write_block (dtp
, length
);
3289 if (unlikely (is_char4_unit (dtp
)))
3291 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
3292 memset4 (p4
, (gfc_char4_t
) ' ', length
);
3295 memset (p
, ' ', length
);
3306 fbuf_seek (dtp
->u
.p
.current_unit
, 0, SEEK_END
);
3307 char * p
= fbuf_alloc (dtp
->u
.p
.current_unit
, len
);
3314 if (is_stream_io (dtp
))
3316 dtp
->u
.p
.current_unit
->strm_pos
+= len
;
3317 if (dtp
->u
.p
.current_unit
->strm_pos
3318 < file_length (dtp
->u
.p
.current_unit
->s
))
3319 unit_truncate (dtp
->u
.p
.current_unit
,
3320 dtp
->u
.p
.current_unit
->strm_pos
- 1,
3328 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
3333 /* Position to the next record, which means moving to the end of the
3334 current record. This can happen under several different
3335 conditions. If the done flag is not set, we get ready to process
3339 next_record (st_parameter_dt
*dtp
, int done
)
3341 gfc_offset fp
; /* File position. */
3343 dtp
->u
.p
.current_unit
->read_bad
= 0;
3345 if (dtp
->u
.p
.mode
== READING
)
3346 next_record_r (dtp
, done
);
3348 next_record_w (dtp
, done
);
3350 if (!is_stream_io (dtp
))
3352 /* Keep position up to date for INQUIRE */
3354 update_position (dtp
->u
.p
.current_unit
);
3356 dtp
->u
.p
.current_unit
->current_record
= 0;
3357 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
3359 fp
= stell (dtp
->u
.p
.current_unit
->s
);
3360 /* Calculate next record, rounding up partial records. */
3361 dtp
->u
.p
.current_unit
->last_record
=
3362 (fp
+ dtp
->u
.p
.current_unit
->recl
- 1) /
3363 dtp
->u
.p
.current_unit
->recl
;
3366 dtp
->u
.p
.current_unit
->last_record
++;
3372 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
3376 /* Finalize the current data transfer. For a nonadvancing transfer,
3377 this means advancing to the next record. For internal units close the
3378 stream associated with the unit. */
3381 finalize_transfer (st_parameter_dt
*dtp
)
3383 GFC_INTEGER_4 cf
= dtp
->common
.flags
;
3385 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
3386 *dtp
->size
= dtp
->u
.p
.size_used
;
3388 if (dtp
->u
.p
.eor_condition
)
3390 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
3394 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
3396 if (dtp
->u
.p
.current_unit
&& current_mode (dtp
) == UNFORMATTED_SEQUENTIAL
)
3397 dtp
->u
.p
.current_unit
->current_record
= 0;
3401 if ((dtp
->u
.p
.ionml
!= NULL
)
3402 && (cf
& IOPARM_DT_HAS_NAMELIST_NAME
) != 0)
3404 if ((cf
& IOPARM_DT_NAMELIST_READ_MODE
) != 0)
3405 namelist_read (dtp
);
3407 namelist_write (dtp
);
3410 dtp
->u
.p
.transfer
= NULL
;
3411 if (dtp
->u
.p
.current_unit
== NULL
)
3414 if ((cf
& IOPARM_DT_LIST_FORMAT
) != 0 && dtp
->u
.p
.mode
== READING
)
3416 finish_list_read (dtp
);
3420 if (dtp
->u
.p
.mode
== WRITING
)
3421 dtp
->u
.p
.current_unit
->previous_nonadvancing_write
3422 = dtp
->u
.p
.advance_status
== ADVANCE_NO
;
3424 if (is_stream_io (dtp
))
3426 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
3427 && dtp
->u
.p
.advance_status
!= ADVANCE_NO
)
3428 next_record (dtp
, 1);
3433 dtp
->u
.p
.current_unit
->current_record
= 0;
3435 if (!is_internal_unit (dtp
) && dtp
->u
.p
.seen_dollar
)
3437 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
3438 dtp
->u
.p
.seen_dollar
= 0;
3442 /* For non-advancing I/O, save the current maximum position for use in the
3443 next I/O operation if needed. */
3444 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
)
3446 int bytes_written
= (int) (dtp
->u
.p
.current_unit
->recl
3447 - dtp
->u
.p
.current_unit
->bytes_left
);
3448 dtp
->u
.p
.current_unit
->saved_pos
=
3449 dtp
->u
.p
.max_pos
> 0 ? dtp
->u
.p
.max_pos
- bytes_written
: 0;
3450 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
3453 else if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
3454 && dtp
->u
.p
.mode
== WRITING
&& !is_internal_unit (dtp
))
3455 fbuf_seek (dtp
->u
.p
.current_unit
, 0, SEEK_END
);
3457 dtp
->u
.p
.current_unit
->saved_pos
= 0;
3459 next_record (dtp
, 1);
3462 /* Transfer function for IOLENGTH. It doesn't actually do any
3463 data transfer, it just updates the length counter. */
3466 iolength_transfer (st_parameter_dt
*dtp
, bt type
__attribute__((unused
)),
3467 void *dest
__attribute__ ((unused
)),
3468 int kind
__attribute__((unused
)),
3469 size_t size
, size_t nelems
)
3471 if ((dtp
->common
.flags
& IOPARM_DT_HAS_IOLENGTH
) != 0)
3472 *dtp
->iolength
+= (GFC_IO_INT
) (size
* nelems
);
3476 /* Initialize the IOLENGTH data transfer. This function is in essence
3477 a very much simplified version of data_transfer_init(), because it
3478 doesn't have to deal with units at all. */
3481 iolength_transfer_init (st_parameter_dt
*dtp
)
3483 if ((dtp
->common
.flags
& IOPARM_DT_HAS_IOLENGTH
) != 0)
3486 memset (&dtp
->u
.p
, 0, sizeof (dtp
->u
.p
));
3488 /* Set up the subroutine that will handle the transfers. */
3490 dtp
->u
.p
.transfer
= iolength_transfer
;
3494 /* Library entry point for the IOLENGTH form of the INQUIRE
3495 statement. The IOLENGTH form requires no I/O to be performed, but
3496 it must still be a runtime library call so that we can determine
3497 the iolength for dynamic arrays and such. */
3499 extern void st_iolength (st_parameter_dt
*);
3500 export_proto(st_iolength
);
3503 st_iolength (st_parameter_dt
*dtp
)
3505 library_start (&dtp
->common
);
3506 iolength_transfer_init (dtp
);
3509 extern void st_iolength_done (st_parameter_dt
*);
3510 export_proto(st_iolength_done
);
3513 st_iolength_done (st_parameter_dt
*dtp
__attribute__((unused
)))
3520 /* The READ statement. */
3522 extern void st_read (st_parameter_dt
*);
3523 export_proto(st_read
);
3526 st_read (st_parameter_dt
*dtp
)
3528 library_start (&dtp
->common
);
3530 data_transfer_init (dtp
, 1);
3533 extern void st_read_done (st_parameter_dt
*);
3534 export_proto(st_read_done
);
3537 st_read_done (st_parameter_dt
*dtp
)
3539 finalize_transfer (dtp
);
3540 if (is_internal_unit (dtp
) || dtp
->u
.p
.format_not_saved
)
3541 free_format_data (dtp
->u
.p
.fmt
);
3543 if (dtp
->u
.p
.current_unit
!= NULL
)
3544 unlock_unit (dtp
->u
.p
.current_unit
);
3546 free_internal_unit (dtp
);
3551 extern void st_write (st_parameter_dt
*);
3552 export_proto(st_write
);
3555 st_write (st_parameter_dt
*dtp
)
3557 library_start (&dtp
->common
);
3558 data_transfer_init (dtp
, 0);
3561 extern void st_write_done (st_parameter_dt
*);
3562 export_proto(st_write_done
);
3565 st_write_done (st_parameter_dt
*dtp
)
3567 finalize_transfer (dtp
);
3569 /* Deal with endfile conditions associated with sequential files. */
3571 if (dtp
->u
.p
.current_unit
!= NULL
3572 && dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
3573 switch (dtp
->u
.p
.current_unit
->endfile
)
3575 case AT_ENDFILE
: /* Remain at the endfile record. */
3579 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
; /* Just at it now. */
3583 /* Get rid of whatever is after this record. */
3584 if (!is_internal_unit (dtp
))
3585 unit_truncate (dtp
->u
.p
.current_unit
,
3586 stell (dtp
->u
.p
.current_unit
->s
),
3588 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
3592 if (is_internal_unit (dtp
) || dtp
->u
.p
.format_not_saved
)
3593 free_format_data (dtp
->u
.p
.fmt
);
3595 if (dtp
->u
.p
.current_unit
!= NULL
)
3596 unlock_unit (dtp
->u
.p
.current_unit
);
3598 free_internal_unit (dtp
);
3604 /* F2003: This is a stub for the runtime portion of the WAIT statement. */
3606 st_wait (st_parameter_wait
*wtp
__attribute__((unused
)))
3611 /* Receives the scalar information for namelist objects and stores it
3612 in a linked list of namelist_info types. */
3614 extern void st_set_nml_var (st_parameter_dt
*dtp
, void *, char *,
3615 GFC_INTEGER_4
, gfc_charlen_type
, GFC_INTEGER_4
);
3616 export_proto(st_set_nml_var
);
3620 st_set_nml_var (st_parameter_dt
*dtp
, void * var_addr
, char * var_name
,
3621 GFC_INTEGER_4 len
, gfc_charlen_type string_length
,
3622 GFC_INTEGER_4 dtype
)
3624 namelist_info
*t1
= NULL
;
3626 size_t var_name_len
= strlen (var_name
);
3628 nml
= (namelist_info
*) get_mem (sizeof (namelist_info
));
3630 nml
->mem_pos
= var_addr
;
3632 nml
->var_name
= (char*) get_mem (var_name_len
+ 1);
3633 memcpy (nml
->var_name
, var_name
, var_name_len
);
3634 nml
->var_name
[var_name_len
] = '\0';
3636 nml
->len
= (int) len
;
3637 nml
->string_length
= (index_type
) string_length
;
3639 nml
->var_rank
= (int) (dtype
& GFC_DTYPE_RANK_MASK
);
3640 nml
->size
= (index_type
) (dtype
>> GFC_DTYPE_SIZE_SHIFT
);
3641 nml
->type
= (bt
) ((dtype
& GFC_DTYPE_TYPE_MASK
) >> GFC_DTYPE_TYPE_SHIFT
);
3643 if (nml
->var_rank
> 0)
3645 nml
->dim
= (descriptor_dimension
*)
3646 get_mem (nml
->var_rank
* sizeof (descriptor_dimension
));
3647 nml
->ls
= (array_loop_spec
*)
3648 get_mem (nml
->var_rank
* sizeof (array_loop_spec
));
3658 if ((dtp
->common
.flags
& IOPARM_DT_IONML_SET
) == 0)
3660 dtp
->common
.flags
|= IOPARM_DT_IONML_SET
;
3661 dtp
->u
.p
.ionml
= nml
;
3665 for (t1
= dtp
->u
.p
.ionml
; t1
->next
; t1
= t1
->next
);
3670 /* Store the dimensional information for the namelist object. */
3671 extern void st_set_nml_var_dim (st_parameter_dt
*, GFC_INTEGER_4
,
3672 index_type
, index_type
,
3674 export_proto(st_set_nml_var_dim
);
3677 st_set_nml_var_dim (st_parameter_dt
*dtp
, GFC_INTEGER_4 n_dim
,
3678 index_type stride
, index_type lbound
,
3681 namelist_info
* nml
;
3686 for (nml
= dtp
->u
.p
.ionml
; nml
->next
; nml
= nml
->next
);
3688 GFC_DIMENSION_SET(nml
->dim
[n
],lbound
,ubound
,stride
);
3691 /* Reverse memcpy - used for byte swapping. */
3693 void reverse_memcpy (void *dest
, const void *src
, size_t n
)
3699 s
= (char *) src
+ n
- 1;
3701 /* Write with ascending order - this is likely faster
3702 on modern architectures because of write combining. */
3708 /* Once upon a time, a poor innocent Fortran program was reading a
3709 file, when suddenly it hit the end-of-file (EOF). Unfortunately
3710 the OS doesn't tell whether we're at the EOF or whether we already
3711 went past it. Luckily our hero, libgfortran, keeps track of this.
3712 Call this function when you detect an EOF condition. See Section
3716 hit_eof (st_parameter_dt
* dtp
)
3718 dtp
->u
.p
.current_unit
->flags
.position
= POSITION_APPEND
;
3720 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
3721 switch (dtp
->u
.p
.current_unit
->endfile
)
3725 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
3726 if (!is_internal_unit (dtp
))
3728 dtp
->u
.p
.current_unit
->endfile
= AFTER_ENDFILE
;
3729 dtp
->u
.p
.current_unit
->current_record
= 0;
3732 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
3736 generate_error (&dtp
->common
, LIBERROR_ENDFILE
, NULL
);
3737 dtp
->u
.p
.current_unit
->current_record
= 0;
3742 /* Non-sequential files don't have an ENDFILE record, so we
3743 can't be at AFTER_ENDFILE. */
3744 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
3745 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
3746 dtp
->u
.p
.current_unit
->current_record
= 0;