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
56 transfer_character_wide
60 These subroutines do not return status.
62 The last call is a call to st_[read|write]_done(). While
63 something can easily go wrong with the initial st_read() or
64 st_write(), an error inhibits any data from actually being
67 extern void transfer_integer (st_parameter_dt
*, void *, int);
68 export_proto(transfer_integer
);
70 extern void transfer_integer_write (st_parameter_dt
*, void *, int);
71 export_proto(transfer_integer_write
);
73 extern void transfer_real (st_parameter_dt
*, void *, int);
74 export_proto(transfer_real
);
76 extern void transfer_real_write (st_parameter_dt
*, void *, int);
77 export_proto(transfer_real_write
);
79 extern void transfer_logical (st_parameter_dt
*, void *, int);
80 export_proto(transfer_logical
);
82 extern void transfer_logical_write (st_parameter_dt
*, void *, int);
83 export_proto(transfer_logical_write
);
85 extern void transfer_character (st_parameter_dt
*, void *, int);
86 export_proto(transfer_character
);
88 extern void transfer_character_write (st_parameter_dt
*, void *, int);
89 export_proto(transfer_character_write
);
91 extern void transfer_character_wide (st_parameter_dt
*, void *, int, int);
92 export_proto(transfer_character_wide
);
94 extern void transfer_character_wide_write (st_parameter_dt
*,
96 export_proto(transfer_character_wide_write
);
98 extern void transfer_complex (st_parameter_dt
*, void *, int);
99 export_proto(transfer_complex
);
101 extern void transfer_complex_write (st_parameter_dt
*, void *, int);
102 export_proto(transfer_complex_write
);
104 extern void transfer_array (st_parameter_dt
*, gfc_array_char
*, int,
106 export_proto(transfer_array
);
108 extern void transfer_array_write (st_parameter_dt
*, gfc_array_char
*, int,
110 export_proto(transfer_array_write
);
112 static void us_read (st_parameter_dt
*, int);
113 static void us_write (st_parameter_dt
*, int);
114 static void next_record_r_unf (st_parameter_dt
*, int);
115 static void next_record_w_unf (st_parameter_dt
*, int);
117 static const st_option advance_opt
[] = {
118 {"yes", ADVANCE_YES
},
124 static const st_option decimal_opt
[] = {
125 {"point", DECIMAL_POINT
},
126 {"comma", DECIMAL_COMMA
},
130 static const st_option round_opt
[] = {
132 {"down", ROUND_DOWN
},
133 {"zero", ROUND_ZERO
},
134 {"nearest", ROUND_NEAREST
},
135 {"compatible", ROUND_COMPATIBLE
},
136 {"processor_defined", ROUND_PROCDEFINED
},
141 static const st_option sign_opt
[] = {
143 {"suppress", SIGN_SS
},
144 {"processor_defined", SIGN_S
},
148 static const st_option blank_opt
[] = {
149 {"null", BLANK_NULL
},
150 {"zero", BLANK_ZERO
},
154 static const st_option delim_opt
[] = {
155 {"apostrophe", DELIM_APOSTROPHE
},
156 {"quote", DELIM_QUOTE
},
157 {"none", DELIM_NONE
},
161 static const st_option pad_opt
[] = {
168 { FORMATTED_SEQUENTIAL
, UNFORMATTED_SEQUENTIAL
,
169 FORMATTED_DIRECT
, UNFORMATTED_DIRECT
, FORMATTED_STREAM
, UNFORMATTED_STREAM
175 current_mode (st_parameter_dt
*dtp
)
179 m
= FORM_UNSPECIFIED
;
181 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
183 m
= dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
?
184 FORMATTED_DIRECT
: UNFORMATTED_DIRECT
;
186 else if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
188 m
= dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
?
189 FORMATTED_SEQUENTIAL
: UNFORMATTED_SEQUENTIAL
;
191 else if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_STREAM
)
193 m
= dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
?
194 FORMATTED_STREAM
: UNFORMATTED_STREAM
;
201 /* Mid level data transfer statements. */
203 /* Read sequential file - internal unit */
206 read_sf_internal (st_parameter_dt
*dtp
, int * length
)
208 static char *empty_string
[0];
212 /* Zero size array gives internal unit len of 0. Nothing to read. */
213 if (dtp
->internal_unit_len
== 0
214 && dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
)
217 /* If we have seen an eor previously, return a length of 0. The
218 caller is responsible for correctly padding the input field. */
219 if (dtp
->u
.p
.sf_seen_eor
)
222 /* Just return something that isn't a NULL pointer, otherwise the
223 caller thinks an error occured. */
224 return (char*) empty_string
;
228 if (is_char4_unit(dtp
))
231 gfc_char4_t
*p
= (gfc_char4_t
*) mem_alloc_r4 (dtp
->u
.p
.current_unit
->s
,
233 base
= fbuf_alloc (dtp
->u
.p
.current_unit
, lorig
);
234 for (i
= 0; i
< *length
; i
++, p
++)
235 base
[i
] = *p
> 255 ? '?' : (unsigned char) *p
;
238 base
= mem_alloc_r (dtp
->u
.p
.current_unit
->s
, length
);
240 if (unlikely (lorig
> *length
))
246 dtp
->u
.p
.current_unit
->bytes_left
-= *length
;
248 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
249 dtp
->u
.p
.size_used
+= (GFC_IO_INT
) *length
;
255 /* When reading sequential formatted records we have a problem. We
256 don't know how long the line is until we read the trailing newline,
257 and we don't want to read too much. If we read too much, we might
258 have to do a physical seek backwards depending on how much data is
259 present, and devices like terminals aren't seekable and would cause
262 Given this, the solution is to read a byte at a time, stopping if
263 we hit the newline. For small allocations, we use a static buffer.
264 For larger allocations, we are forced to allocate memory on the
265 heap. Hopefully this won't happen very often. */
267 /* Read sequential file - external unit */
270 read_sf (st_parameter_dt
*dtp
, int * length
)
272 static char *empty_string
[0];
274 int n
, lorig
, seen_comma
;
276 /* If we have seen an eor previously, return a length of 0. The
277 caller is responsible for correctly padding the input field. */
278 if (dtp
->u
.p
.sf_seen_eor
)
281 /* Just return something that isn't a NULL pointer, otherwise the
282 caller thinks an error occured. */
283 return (char*) empty_string
;
288 /* Read data into format buffer and scan through it. */
290 base
= p
= fbuf_read (dtp
->u
.p
.current_unit
, length
);
298 if (q
== '\n' || q
== '\r')
300 /* Unexpected end of line. Set the position. */
301 fbuf_seek (dtp
->u
.p
.current_unit
, n
+ 1 ,SEEK_CUR
);
302 dtp
->u
.p
.sf_seen_eor
= 1;
304 /* If we see an EOR during non-advancing I/O, we need to skip
305 the rest of the I/O statement. Set the corresponding flag. */
306 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
|| dtp
->u
.p
.seen_dollar
)
307 dtp
->u
.p
.eor_condition
= 1;
309 /* If we encounter a CR, it might be a CRLF. */
310 if (q
== '\r') /* Probably a CRLF */
312 /* See if there is an LF. Use fbuf_read rather then fbuf_getc so
313 the position is not advanced unless it really is an LF. */
315 p
= fbuf_read (dtp
->u
.p
.current_unit
, &readlen
);
316 if (*p
== '\n' && readlen
== 1)
318 dtp
->u
.p
.sf_seen_eor
= 2;
319 fbuf_seek (dtp
->u
.p
.current_unit
, 1 ,SEEK_CUR
);
323 /* Without padding, terminate the I/O statement without assigning
324 the value. With padding, the value still needs to be assigned,
325 so we can just continue with a short read. */
326 if (dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
)
328 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
335 /* Short circuit the read if a comma is found during numeric input.
336 The flag is set to zero during character reads so that commas in
337 strings are not ignored */
339 if (dtp
->u
.p
.sf_read_comma
== 1)
342 notify_std (&dtp
->common
, GFC_STD_GNU
,
343 "Comma in formatted numeric read.");
351 fbuf_seek (dtp
->u
.p
.current_unit
, n
+ seen_comma
, SEEK_CUR
);
353 /* A short read implies we hit EOF, unless we hit EOR, a comma, or
354 some other stuff. Set the relevant flags. */
355 if (lorig
> *length
&& !dtp
->u
.p
.sf_seen_eor
&& !seen_comma
)
359 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
)
361 if (dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
)
367 dtp
->u
.p
.eor_condition
= 1;
372 else if (dtp
->u
.p
.advance_status
== ADVANCE_NO
373 || dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
374 || dtp
->u
.p
.current_unit
->bytes_left
375 == dtp
->u
.p
.current_unit
->recl
)
384 dtp
->u
.p
.current_unit
->bytes_left
-= n
;
386 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
387 dtp
->u
.p
.size_used
+= (GFC_IO_INT
) n
;
393 /* Function for reading the next couple of bytes from the current
394 file, advancing the current position. We return FAILURE on end of record or
395 end of file. This function is only for formatted I/O, unformatted uses
398 If the read is short, then it is because the current record does not
399 have enough data to satisfy the read request and the file was
400 opened with PAD=YES. The caller must assume tailing spaces for
404 read_block_form (st_parameter_dt
*dtp
, int * nbytes
)
409 if (!is_stream_io (dtp
))
411 if (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) *nbytes
)
413 /* For preconnected units with default record length, set bytes left
414 to unit record length and proceed, otherwise error. */
415 if (dtp
->u
.p
.current_unit
->unit_number
== options
.stdin_unit
416 && dtp
->u
.p
.current_unit
->recl
== DEFAULT_RECL
)
417 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
420 if (unlikely (dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
)
421 && !is_internal_unit (dtp
))
423 /* Not enough data left. */
424 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
429 if (unlikely (dtp
->u
.p
.current_unit
->bytes_left
== 0
430 && !is_internal_unit(dtp
)))
436 *nbytes
= dtp
->u
.p
.current_unit
->bytes_left
;
440 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
&&
441 (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
||
442 dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_STREAM
))
444 if (is_internal_unit (dtp
))
445 source
= read_sf_internal (dtp
, nbytes
);
447 source
= read_sf (dtp
, nbytes
);
449 dtp
->u
.p
.current_unit
->strm_pos
+=
450 (gfc_offset
) (*nbytes
+ dtp
->u
.p
.sf_seen_eor
);
454 /* If we reach here, we can assume it's direct access. */
456 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) *nbytes
;
459 source
= fbuf_read (dtp
->u
.p
.current_unit
, nbytes
);
460 fbuf_seek (dtp
->u
.p
.current_unit
, *nbytes
, SEEK_CUR
);
462 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
463 dtp
->u
.p
.size_used
+= (GFC_IO_INT
) *nbytes
;
465 if (norig
!= *nbytes
)
467 /* Short read, this shouldn't happen. */
468 if (!dtp
->u
.p
.current_unit
->pad_status
== PAD_YES
)
470 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
475 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) *nbytes
;
481 /* Read a block from a character(kind=4) internal unit, to be transferred into
482 a character(kind=4) variable. Note: Portions of this code borrowed from
485 read_block_form4 (st_parameter_dt
*dtp
, int * nbytes
)
487 static gfc_char4_t
*empty_string
[0];
491 if (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) *nbytes
)
492 *nbytes
= dtp
->u
.p
.current_unit
->bytes_left
;
494 /* Zero size array gives internal unit len of 0. Nothing to read. */
495 if (dtp
->internal_unit_len
== 0
496 && dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
)
499 /* If we have seen an eor previously, return a length of 0. The
500 caller is responsible for correctly padding the input field. */
501 if (dtp
->u
.p
.sf_seen_eor
)
504 /* Just return something that isn't a NULL pointer, otherwise the
505 caller thinks an error occured. */
510 source
= (gfc_char4_t
*) mem_alloc_r4 (dtp
->u
.p
.current_unit
->s
, nbytes
);
512 if (unlikely (lorig
> *nbytes
))
518 dtp
->u
.p
.current_unit
->bytes_left
-= *nbytes
;
520 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
521 dtp
->u
.p
.size_used
+= (GFC_IO_INT
) *nbytes
;
527 /* Reads a block directly into application data space. This is for
528 unformatted files. */
531 read_block_direct (st_parameter_dt
*dtp
, void *buf
, size_t nbytes
)
533 ssize_t to_read_record
;
534 ssize_t have_read_record
;
535 ssize_t to_read_subrecord
;
536 ssize_t have_read_subrecord
;
539 if (is_stream_io (dtp
))
541 have_read_record
= sread (dtp
->u
.p
.current_unit
->s
, buf
,
543 if (unlikely (have_read_record
< 0))
545 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
549 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) have_read_record
;
551 if (unlikely ((ssize_t
) nbytes
!= have_read_record
))
553 /* Short read, e.g. if we hit EOF. For stream files,
554 we have to set the end-of-file condition. */
560 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
562 if (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) nbytes
)
565 to_read_record
= dtp
->u
.p
.current_unit
->bytes_left
;
566 nbytes
= to_read_record
;
571 to_read_record
= nbytes
;
574 dtp
->u
.p
.current_unit
->bytes_left
-= to_read_record
;
576 to_read_record
= sread (dtp
->u
.p
.current_unit
->s
, buf
, to_read_record
);
577 if (unlikely (to_read_record
< 0))
579 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
583 if (to_read_record
!= (ssize_t
) nbytes
)
585 /* Short read, e.g. if we hit EOF. Apparently, we read
586 more than was written to the last record. */
590 if (unlikely (short_record
))
592 generate_error (&dtp
->common
, LIBERROR_SHORT_RECORD
, NULL
);
597 /* Unformatted sequential. We loop over the subrecords, reading
598 until the request has been fulfilled or the record has run out
599 of continuation subrecords. */
601 /* Check whether we exceed the total record length. */
603 if (dtp
->u
.p
.current_unit
->flags
.has_recl
604 && ((gfc_offset
) nbytes
> dtp
->u
.p
.current_unit
->bytes_left
))
606 to_read_record
= dtp
->u
.p
.current_unit
->bytes_left
;
611 to_read_record
= nbytes
;
614 have_read_record
= 0;
618 if (dtp
->u
.p
.current_unit
->bytes_left_subrecord
619 < (gfc_offset
) to_read_record
)
621 to_read_subrecord
= dtp
->u
.p
.current_unit
->bytes_left_subrecord
;
622 to_read_record
-= to_read_subrecord
;
626 to_read_subrecord
= to_read_record
;
630 dtp
->u
.p
.current_unit
->bytes_left_subrecord
-= to_read_subrecord
;
632 have_read_subrecord
= sread (dtp
->u
.p
.current_unit
->s
,
633 buf
+ have_read_record
, to_read_subrecord
);
634 if (unlikely (have_read_subrecord
) < 0)
636 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
640 have_read_record
+= have_read_subrecord
;
642 if (unlikely (to_read_subrecord
!= have_read_subrecord
))
644 /* Short read, e.g. if we hit EOF. This means the record
645 structure has been corrupted, or the trailing record
646 marker would still be present. */
648 generate_error (&dtp
->common
, LIBERROR_CORRUPT_FILE
, NULL
);
652 if (to_read_record
> 0)
654 if (likely (dtp
->u
.p
.current_unit
->continued
))
656 next_record_r_unf (dtp
, 0);
661 /* Let's make sure the file position is correctly pre-positioned
662 for the next read statement. */
664 dtp
->u
.p
.current_unit
->current_record
= 0;
665 next_record_r_unf (dtp
, 0);
666 generate_error (&dtp
->common
, LIBERROR_SHORT_RECORD
, NULL
);
672 /* Normal exit, the read request has been fulfilled. */
677 dtp
->u
.p
.current_unit
->bytes_left
-= have_read_record
;
678 if (unlikely (short_record
))
680 generate_error (&dtp
->common
, LIBERROR_SHORT_RECORD
, NULL
);
687 /* Function for writing a block of bytes to the current file at the
688 current position, advancing the file pointer. We are given a length
689 and return a pointer to a buffer that the caller must (completely)
690 fill in. Returns NULL on error. */
693 write_block (st_parameter_dt
*dtp
, int length
)
697 if (!is_stream_io (dtp
))
699 if (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) length
)
701 /* For preconnected units with default record length, set bytes left
702 to unit record length and proceed, otherwise error. */
703 if (likely ((dtp
->u
.p
.current_unit
->unit_number
704 == options
.stdout_unit
705 || dtp
->u
.p
.current_unit
->unit_number
706 == options
.stderr_unit
)
707 && dtp
->u
.p
.current_unit
->recl
== DEFAULT_RECL
))
708 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
711 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
716 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) length
;
719 if (is_internal_unit (dtp
))
721 if (dtp
->common
.unit
) /* char4 internel unit. */
724 dest4
= mem_alloc_w4 (dtp
->u
.p
.current_unit
->s
, &length
);
727 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
733 dest
= mem_alloc_w (dtp
->u
.p
.current_unit
->s
, &length
);
737 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
741 if (unlikely (dtp
->u
.p
.current_unit
->endfile
== AT_ENDFILE
))
742 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
746 dest
= fbuf_alloc (dtp
->u
.p
.current_unit
, length
);
749 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
754 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
755 dtp
->u
.p
.size_used
+= (GFC_IO_INT
) length
;
757 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) length
;
763 /* High level interface to swrite(), taking care of errors. This is only
764 called for unformatted files. There are three cases to consider:
765 Stream I/O, unformatted direct, unformatted sequential. */
768 write_buf (st_parameter_dt
*dtp
, void *buf
, size_t nbytes
)
771 ssize_t have_written
;
772 ssize_t to_write_subrecord
;
777 if (is_stream_io (dtp
))
779 have_written
= swrite (dtp
->u
.p
.current_unit
->s
, buf
, nbytes
);
780 if (unlikely (have_written
< 0))
782 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
786 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) have_written
;
791 /* Unformatted direct access. */
793 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
795 if (unlikely (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) nbytes
))
797 generate_error (&dtp
->common
, LIBERROR_DIRECT_EOR
, NULL
);
801 if (buf
== NULL
&& nbytes
== 0)
804 have_written
= swrite (dtp
->u
.p
.current_unit
->s
, buf
, nbytes
);
805 if (unlikely (have_written
< 0))
807 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
811 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) have_written
;
812 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) have_written
;
817 /* Unformatted sequential. */
821 if (dtp
->u
.p
.current_unit
->flags
.has_recl
822 && (gfc_offset
) nbytes
> dtp
->u
.p
.current_unit
->bytes_left
)
824 nbytes
= dtp
->u
.p
.current_unit
->bytes_left
;
836 (size_t) dtp
->u
.p
.current_unit
->bytes_left_subrecord
< nbytes
?
837 (size_t) dtp
->u
.p
.current_unit
->bytes_left_subrecord
: nbytes
;
839 dtp
->u
.p
.current_unit
->bytes_left_subrecord
-=
840 (gfc_offset
) to_write_subrecord
;
842 to_write_subrecord
= swrite (dtp
->u
.p
.current_unit
->s
,
843 buf
+ have_written
, to_write_subrecord
);
844 if (unlikely (to_write_subrecord
< 0))
846 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
850 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) to_write_subrecord
;
851 nbytes
-= to_write_subrecord
;
852 have_written
+= to_write_subrecord
;
857 next_record_w_unf (dtp
, 1);
860 dtp
->u
.p
.current_unit
->bytes_left
-= have_written
;
861 if (unlikely (short_record
))
863 generate_error (&dtp
->common
, LIBERROR_SHORT_RECORD
, NULL
);
870 /* Master function for unformatted reads. */
873 unformatted_read (st_parameter_dt
*dtp
, bt type
,
874 void *dest
, int kind
, size_t size
, size_t nelems
)
876 if (likely (dtp
->u
.p
.current_unit
->flags
.convert
== GFC_CONVERT_NATIVE
)
879 if (type
== BT_CHARACTER
)
880 size
*= GFC_SIZE_OF_CHAR_KIND(kind
);
881 read_block_direct (dtp
, dest
, size
* nelems
);
891 /* Handle wide chracters. */
892 if (type
== BT_CHARACTER
&& kind
!= 1)
898 /* Break up complex into its constituent reals. */
899 if (type
== BT_COMPLEX
)
905 /* By now, all complex variables have been split into their
906 constituent reals. */
908 for (i
= 0; i
< nelems
; i
++)
910 read_block_direct (dtp
, buffer
, size
);
911 reverse_memcpy (p
, buffer
, size
);
918 /* Master function for unformatted writes. NOTE: For kind=10 the size is 16
919 bytes on 64 bit machines. The unused bytes are not initialized and never
920 used, which can show an error with memory checking analyzers like
924 unformatted_write (st_parameter_dt
*dtp
, bt type
,
925 void *source
, int kind
, size_t size
, size_t nelems
)
927 if (likely (dtp
->u
.p
.current_unit
->flags
.convert
== GFC_CONVERT_NATIVE
)
930 size_t stride
= type
== BT_CHARACTER
?
931 size
* GFC_SIZE_OF_CHAR_KIND(kind
) : size
;
933 write_buf (dtp
, source
, stride
* nelems
);
943 /* Handle wide chracters. */
944 if (type
== BT_CHARACTER
&& kind
!= 1)
950 /* Break up complex into its constituent reals. */
951 if (type
== BT_COMPLEX
)
957 /* By now, all complex variables have been split into their
958 constituent reals. */
960 for (i
= 0; i
< nelems
; i
++)
962 reverse_memcpy(buffer
, p
, size
);
964 write_buf (dtp
, buffer
, size
);
970 /* Return a pointer to the name of a type. */
995 internal_error (NULL
, "type_name(): Bad type");
1002 /* Write a constant string to the output.
1003 This is complicated because the string can have doubled delimiters
1004 in it. The length in the format node is the true length. */
1007 write_constant_string (st_parameter_dt
*dtp
, const fnode
*f
)
1009 char c
, delimiter
, *p
, *q
;
1012 length
= f
->u
.string
.length
;
1016 p
= write_block (dtp
, length
);
1023 for (; length
> 0; length
--)
1026 if (c
== delimiter
&& c
!= 'H' && c
!= 'h')
1027 q
++; /* Skip the doubled delimiter. */
1032 /* Given actual and expected types in a formatted data transfer, make
1033 sure they agree. If not, an error message is generated. Returns
1034 nonzero if something went wrong. */
1037 require_type (st_parameter_dt
*dtp
, bt expected
, bt actual
, const fnode
*f
)
1041 if (actual
== expected
)
1044 /* Adjust item_count before emitting error message. */
1045 sprintf (buffer
, "Expected %s for item %d in formatted transfer, got %s",
1046 type_name (expected
), dtp
->u
.p
.item_count
- 1, type_name (actual
));
1048 format_error (dtp
, f
, buffer
);
1053 /* This function is in the main loop for a formatted data transfer
1054 statement. It would be natural to implement this as a coroutine
1055 with the user program, but C makes that awkward. We loop,
1056 processing format elements. When we actually have to transfer
1057 data instead of just setting flags, we return control to the user
1058 program which calls a function that supplies the address and type
1059 of the next element, then comes back here to process it. */
1062 formatted_transfer_scalar_read (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
1065 int pos
, bytes_used
;
1069 int consume_data_flag
;
1071 /* Change a complex data item into a pair of reals. */
1073 n
= (p
== NULL
) ? 0 : ((type
!= BT_COMPLEX
) ? 1 : 2);
1074 if (type
== BT_COMPLEX
)
1080 /* If there's an EOR condition, we simulate finalizing the transfer
1081 by doing nothing. */
1082 if (dtp
->u
.p
.eor_condition
)
1085 /* Set this flag so that commas in reads cause the read to complete before
1086 the entire field has been read. The next read field will start right after
1087 the comma in the stream. (Set to 0 for character reads). */
1088 dtp
->u
.p
.sf_read_comma
=
1089 dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
? 0 : 1;
1093 /* If reversion has occurred and there is another real data item,
1094 then we have to move to the next record. */
1095 if (dtp
->u
.p
.reversion_flag
&& n
> 0)
1097 dtp
->u
.p
.reversion_flag
= 0;
1098 next_record (dtp
, 0);
1101 consume_data_flag
= 1;
1102 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1105 f
= next_format (dtp
);
1108 /* No data descriptors left. */
1109 if (unlikely (n
> 0))
1110 generate_error (&dtp
->common
, LIBERROR_FORMAT
,
1111 "Insufficient data descriptors in format after reversion");
1117 bytes_used
= (int)(dtp
->u
.p
.current_unit
->recl
1118 - dtp
->u
.p
.current_unit
->bytes_left
);
1120 if (is_stream_io(dtp
))
1127 goto need_read_data
;
1128 if (require_type (dtp
, BT_INTEGER
, type
, f
))
1130 read_decimal (dtp
, f
, p
, kind
);
1135 goto need_read_data
;
1136 if (!(compile_options
.allow_std
& GFC_STD_GNU
)
1137 && require_type (dtp
, BT_INTEGER
, type
, f
))
1139 read_radix (dtp
, f
, p
, kind
, 2);
1144 goto need_read_data
;
1145 if (!(compile_options
.allow_std
& GFC_STD_GNU
)
1146 && require_type (dtp
, BT_INTEGER
, type
, f
))
1148 read_radix (dtp
, f
, p
, kind
, 8);
1153 goto need_read_data
;
1154 if (!(compile_options
.allow_std
& GFC_STD_GNU
)
1155 && require_type (dtp
, BT_INTEGER
, type
, f
))
1157 read_radix (dtp
, f
, p
, kind
, 16);
1162 goto need_read_data
;
1164 /* It is possible to have FMT_A with something not BT_CHARACTER such
1165 as when writing out hollerith strings, so check both type
1166 and kind before calling wide character routines. */
1167 if (type
== BT_CHARACTER
&& kind
== 4)
1168 read_a_char4 (dtp
, f
, p
, size
);
1170 read_a (dtp
, f
, p
, size
);
1175 goto need_read_data
;
1176 read_l (dtp
, f
, p
, kind
);
1181 goto need_read_data
;
1182 if (require_type (dtp
, BT_REAL
, type
, f
))
1184 read_f (dtp
, f
, p
, kind
);
1189 goto need_read_data
;
1190 if (require_type (dtp
, BT_REAL
, type
, f
))
1192 read_f (dtp
, f
, p
, kind
);
1197 goto need_read_data
;
1198 if (require_type (dtp
, BT_REAL
, type
, f
))
1200 read_f (dtp
, f
, p
, kind
);
1205 goto need_read_data
;
1206 if (require_type (dtp
, BT_REAL
, type
, f
))
1208 read_f (dtp
, f
, p
, kind
);
1213 goto need_read_data
;
1214 if (require_type (dtp
, BT_REAL
, type
, f
))
1216 read_f (dtp
, f
, p
, kind
);
1221 goto need_read_data
;
1225 read_decimal (dtp
, f
, p
, kind
);
1228 read_l (dtp
, f
, p
, kind
);
1232 read_a_char4 (dtp
, f
, p
, size
);
1234 read_a (dtp
, f
, p
, size
);
1237 read_f (dtp
, f
, p
, kind
);
1240 internal_error (&dtp
->common
, "formatted_transfer(): Bad type");
1245 consume_data_flag
= 0;
1246 format_error (dtp
, f
, "Constant string in input format");
1249 /* Format codes that don't transfer data. */
1252 consume_data_flag
= 0;
1253 dtp
->u
.p
.skips
+= f
->u
.n
;
1254 pos
= bytes_used
+ dtp
->u
.p
.skips
- 1;
1255 dtp
->u
.p
.pending_spaces
= pos
- dtp
->u
.p
.max_pos
+ 1;
1256 read_x (dtp
, f
->u
.n
);
1261 consume_data_flag
= 0;
1263 if (f
->format
== FMT_TL
)
1265 /* Handle the special case when no bytes have been used yet.
1266 Cannot go below zero. */
1267 if (bytes_used
== 0)
1269 dtp
->u
.p
.pending_spaces
-= f
->u
.n
;
1270 dtp
->u
.p
.skips
-= f
->u
.n
;
1271 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
< 0 ? 0 : dtp
->u
.p
.skips
;
1274 pos
= bytes_used
- f
->u
.n
;
1279 /* Standard 10.6.1.1: excessive left tabbing is reset to the
1280 left tab limit. We do not check if the position has gone
1281 beyond the end of record because a subsequent tab could
1282 bring us back again. */
1283 pos
= pos
< 0 ? 0 : pos
;
1285 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
+ pos
- bytes_used
;
1286 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
1287 + pos
- dtp
->u
.p
.max_pos
;
1288 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
< 0
1289 ? 0 : dtp
->u
.p
.pending_spaces
;
1290 if (dtp
->u
.p
.skips
== 0)
1293 /* Adjust everything for end-of-record condition */
1294 if (dtp
->u
.p
.sf_seen_eor
&& !is_internal_unit (dtp
))
1296 dtp
->u
.p
.current_unit
->bytes_left
-= dtp
->u
.p
.sf_seen_eor
;
1297 dtp
->u
.p
.skips
-= dtp
->u
.p
.sf_seen_eor
;
1299 dtp
->u
.p
.sf_seen_eor
= 0;
1301 if (dtp
->u
.p
.skips
< 0)
1303 if (is_internal_unit (dtp
))
1304 sseek (dtp
->u
.p
.current_unit
->s
, dtp
->u
.p
.skips
, SEEK_CUR
);
1306 fbuf_seek (dtp
->u
.p
.current_unit
, dtp
->u
.p
.skips
, SEEK_CUR
);
1307 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) dtp
->u
.p
.skips
;
1308 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1311 read_x (dtp
, dtp
->u
.p
.skips
);
1315 consume_data_flag
= 0;
1316 dtp
->u
.p
.sign_status
= SIGN_S
;
1320 consume_data_flag
= 0;
1321 dtp
->u
.p
.sign_status
= SIGN_SS
;
1325 consume_data_flag
= 0;
1326 dtp
->u
.p
.sign_status
= SIGN_SP
;
1330 consume_data_flag
= 0 ;
1331 dtp
->u
.p
.blank_status
= BLANK_NULL
;
1335 consume_data_flag
= 0;
1336 dtp
->u
.p
.blank_status
= BLANK_ZERO
;
1340 consume_data_flag
= 0;
1341 dtp
->u
.p
.current_unit
->decimal_status
= DECIMAL_COMMA
;
1345 consume_data_flag
= 0;
1346 dtp
->u
.p
.current_unit
->decimal_status
= DECIMAL_POINT
;
1350 consume_data_flag
= 0;
1351 dtp
->u
.p
.current_unit
->round_status
= ROUND_COMPATIBLE
;
1355 consume_data_flag
= 0;
1356 dtp
->u
.p
.current_unit
->round_status
= ROUND_DOWN
;
1360 consume_data_flag
= 0;
1361 dtp
->u
.p
.current_unit
->round_status
= ROUND_NEAREST
;
1365 consume_data_flag
= 0;
1366 dtp
->u
.p
.current_unit
->round_status
= ROUND_PROCDEFINED
;
1370 consume_data_flag
= 0;
1371 dtp
->u
.p
.current_unit
->round_status
= ROUND_UP
;
1375 consume_data_flag
= 0;
1376 dtp
->u
.p
.current_unit
->round_status
= ROUND_ZERO
;
1380 consume_data_flag
= 0;
1381 dtp
->u
.p
.scale_factor
= f
->u
.k
;
1385 consume_data_flag
= 0;
1386 dtp
->u
.p
.seen_dollar
= 1;
1390 consume_data_flag
= 0;
1391 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1392 next_record (dtp
, 0);
1396 /* A colon descriptor causes us to exit this loop (in
1397 particular preventing another / descriptor from being
1398 processed) unless there is another data item to be
1400 consume_data_flag
= 0;
1406 internal_error (&dtp
->common
, "Bad format node");
1409 /* Adjust the item count and data pointer. */
1411 if ((consume_data_flag
> 0) && (n
> 0))
1414 p
= ((char *) p
) + size
;
1419 pos
= (int)(dtp
->u
.p
.current_unit
->recl
- dtp
->u
.p
.current_unit
->bytes_left
);
1420 dtp
->u
.p
.max_pos
= (dtp
->u
.p
.max_pos
> pos
) ? dtp
->u
.p
.max_pos
: pos
;
1425 /* Come here when we need a data descriptor but don't have one. We
1426 push the current format node back onto the input, then return and
1427 let the user program call us back with the data. */
1429 unget_format (dtp
, f
);
1434 formatted_transfer_scalar_write (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
1437 int pos
, bytes_used
;
1441 int consume_data_flag
;
1443 /* Change a complex data item into a pair of reals. */
1445 n
= (p
== NULL
) ? 0 : ((type
!= BT_COMPLEX
) ? 1 : 2);
1446 if (type
== BT_COMPLEX
)
1452 /* If there's an EOR condition, we simulate finalizing the transfer
1453 by doing nothing. */
1454 if (dtp
->u
.p
.eor_condition
)
1457 /* Set this flag so that commas in reads cause the read to complete before
1458 the entire field has been read. The next read field will start right after
1459 the comma in the stream. (Set to 0 for character reads). */
1460 dtp
->u
.p
.sf_read_comma
=
1461 dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
? 0 : 1;
1465 /* If reversion has occurred and there is another real data item,
1466 then we have to move to the next record. */
1467 if (dtp
->u
.p
.reversion_flag
&& n
> 0)
1469 dtp
->u
.p
.reversion_flag
= 0;
1470 next_record (dtp
, 0);
1473 consume_data_flag
= 1;
1474 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1477 f
= next_format (dtp
);
1480 /* No data descriptors left. */
1481 if (unlikely (n
> 0))
1482 generate_error (&dtp
->common
, LIBERROR_FORMAT
,
1483 "Insufficient data descriptors in format after reversion");
1487 /* Now discharge T, TR and X movements to the right. This is delayed
1488 until a data producing format to suppress trailing spaces. */
1491 if (dtp
->u
.p
.mode
== WRITING
&& dtp
->u
.p
.skips
!= 0
1492 && ((n
>0 && ( t
== FMT_I
|| t
== FMT_B
|| t
== FMT_O
1493 || t
== FMT_Z
|| t
== FMT_F
|| t
== FMT_E
1494 || t
== FMT_EN
|| t
== FMT_ES
|| t
== FMT_G
1495 || t
== FMT_L
|| t
== FMT_A
|| t
== FMT_D
))
1496 || t
== FMT_STRING
))
1498 if (dtp
->u
.p
.skips
> 0)
1501 write_x (dtp
, dtp
->u
.p
.skips
, dtp
->u
.p
.pending_spaces
);
1502 tmp
= (int)(dtp
->u
.p
.current_unit
->recl
1503 - dtp
->u
.p
.current_unit
->bytes_left
);
1505 dtp
->u
.p
.max_pos
> tmp
? dtp
->u
.p
.max_pos
: tmp
;
1507 if (dtp
->u
.p
.skips
< 0)
1509 if (is_internal_unit (dtp
))
1510 sseek (dtp
->u
.p
.current_unit
->s
, dtp
->u
.p
.skips
, SEEK_CUR
);
1512 fbuf_seek (dtp
->u
.p
.current_unit
, dtp
->u
.p
.skips
, SEEK_CUR
);
1513 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) dtp
->u
.p
.skips
;
1515 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1518 bytes_used
= (int)(dtp
->u
.p
.current_unit
->recl
1519 - dtp
->u
.p
.current_unit
->bytes_left
);
1521 if (is_stream_io(dtp
))
1529 if (require_type (dtp
, BT_INTEGER
, type
, f
))
1531 write_i (dtp
, f
, p
, kind
);
1537 if (!(compile_options
.allow_std
& GFC_STD_GNU
)
1538 && require_type (dtp
, BT_INTEGER
, type
, f
))
1540 write_b (dtp
, f
, p
, kind
);
1546 if (!(compile_options
.allow_std
& GFC_STD_GNU
)
1547 && require_type (dtp
, BT_INTEGER
, type
, f
))
1549 write_o (dtp
, f
, p
, kind
);
1555 if (!(compile_options
.allow_std
& GFC_STD_GNU
)
1556 && require_type (dtp
, BT_INTEGER
, type
, f
))
1558 write_z (dtp
, f
, p
, kind
);
1565 /* It is possible to have FMT_A with something not BT_CHARACTER such
1566 as when writing out hollerith strings, so check both type
1567 and kind before calling wide character routines. */
1568 if (type
== BT_CHARACTER
&& kind
== 4)
1569 write_a_char4 (dtp
, f
, p
, size
);
1571 write_a (dtp
, f
, p
, size
);
1577 write_l (dtp
, f
, p
, kind
);
1583 if (require_type (dtp
, BT_REAL
, type
, f
))
1585 write_d (dtp
, f
, p
, kind
);
1591 if (require_type (dtp
, BT_REAL
, type
, f
))
1593 write_e (dtp
, f
, p
, kind
);
1599 if (require_type (dtp
, BT_REAL
, type
, f
))
1601 write_en (dtp
, f
, p
, kind
);
1607 if (require_type (dtp
, BT_REAL
, type
, f
))
1609 write_es (dtp
, f
, p
, kind
);
1615 if (require_type (dtp
, BT_REAL
, type
, f
))
1617 write_f (dtp
, f
, p
, kind
);
1626 write_i (dtp
, f
, p
, kind
);
1629 write_l (dtp
, f
, p
, kind
);
1633 write_a_char4 (dtp
, f
, p
, size
);
1635 write_a (dtp
, f
, p
, size
);
1638 if (f
->u
.real
.w
== 0)
1639 write_real_g0 (dtp
, p
, kind
, f
->u
.real
.d
);
1641 write_d (dtp
, f
, p
, kind
);
1644 internal_error (&dtp
->common
,
1645 "formatted_transfer(): Bad type");
1650 consume_data_flag
= 0;
1651 write_constant_string (dtp
, f
);
1654 /* Format codes that don't transfer data. */
1657 consume_data_flag
= 0;
1659 dtp
->u
.p
.skips
+= f
->u
.n
;
1660 pos
= bytes_used
+ dtp
->u
.p
.skips
- 1;
1661 dtp
->u
.p
.pending_spaces
= pos
- dtp
->u
.p
.max_pos
+ 1;
1662 /* Writes occur just before the switch on f->format, above, so
1663 that trailing blanks are suppressed, unless we are doing a
1664 non-advancing write in which case we want to output the blanks
1666 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
)
1668 write_x (dtp
, dtp
->u
.p
.skips
, dtp
->u
.p
.pending_spaces
);
1669 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1675 consume_data_flag
= 0;
1677 if (f
->format
== FMT_TL
)
1680 /* Handle the special case when no bytes have been used yet.
1681 Cannot go below zero. */
1682 if (bytes_used
== 0)
1684 dtp
->u
.p
.pending_spaces
-= f
->u
.n
;
1685 dtp
->u
.p
.skips
-= f
->u
.n
;
1686 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
< 0 ? 0 : dtp
->u
.p
.skips
;
1689 pos
= bytes_used
- f
->u
.n
;
1692 pos
= f
->u
.n
- dtp
->u
.p
.pending_spaces
- 1;
1694 /* Standard 10.6.1.1: excessive left tabbing is reset to the
1695 left tab limit. We do not check if the position has gone
1696 beyond the end of record because a subsequent tab could
1697 bring us back again. */
1698 pos
= pos
< 0 ? 0 : pos
;
1700 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
+ pos
- bytes_used
;
1701 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
1702 + pos
- dtp
->u
.p
.max_pos
;
1703 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
< 0
1704 ? 0 : dtp
->u
.p
.pending_spaces
;
1708 consume_data_flag
= 0;
1709 dtp
->u
.p
.sign_status
= SIGN_S
;
1713 consume_data_flag
= 0;
1714 dtp
->u
.p
.sign_status
= SIGN_SS
;
1718 consume_data_flag
= 0;
1719 dtp
->u
.p
.sign_status
= SIGN_SP
;
1723 consume_data_flag
= 0 ;
1724 dtp
->u
.p
.blank_status
= BLANK_NULL
;
1728 consume_data_flag
= 0;
1729 dtp
->u
.p
.blank_status
= BLANK_ZERO
;
1733 consume_data_flag
= 0;
1734 dtp
->u
.p
.current_unit
->decimal_status
= DECIMAL_COMMA
;
1738 consume_data_flag
= 0;
1739 dtp
->u
.p
.current_unit
->decimal_status
= DECIMAL_POINT
;
1743 consume_data_flag
= 0;
1744 dtp
->u
.p
.current_unit
->round_status
= ROUND_COMPATIBLE
;
1748 consume_data_flag
= 0;
1749 dtp
->u
.p
.current_unit
->round_status
= ROUND_DOWN
;
1753 consume_data_flag
= 0;
1754 dtp
->u
.p
.current_unit
->round_status
= ROUND_NEAREST
;
1758 consume_data_flag
= 0;
1759 dtp
->u
.p
.current_unit
->round_status
= ROUND_PROCDEFINED
;
1763 consume_data_flag
= 0;
1764 dtp
->u
.p
.current_unit
->round_status
= ROUND_UP
;
1768 consume_data_flag
= 0;
1769 dtp
->u
.p
.current_unit
->round_status
= ROUND_ZERO
;
1773 consume_data_flag
= 0;
1774 dtp
->u
.p
.scale_factor
= f
->u
.k
;
1778 consume_data_flag
= 0;
1779 dtp
->u
.p
.seen_dollar
= 1;
1783 consume_data_flag
= 0;
1784 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1785 next_record (dtp
, 0);
1789 /* A colon descriptor causes us to exit this loop (in
1790 particular preventing another / descriptor from being
1791 processed) unless there is another data item to be
1793 consume_data_flag
= 0;
1799 internal_error (&dtp
->common
, "Bad format node");
1802 /* Adjust the item count and data pointer. */
1804 if ((consume_data_flag
> 0) && (n
> 0))
1807 p
= ((char *) p
) + size
;
1810 pos
= (int)(dtp
->u
.p
.current_unit
->recl
- dtp
->u
.p
.current_unit
->bytes_left
);
1811 dtp
->u
.p
.max_pos
= (dtp
->u
.p
.max_pos
> pos
) ? dtp
->u
.p
.max_pos
: pos
;
1816 /* Come here when we need a data descriptor but don't have one. We
1817 push the current format node back onto the input, then return and
1818 let the user program call us back with the data. */
1820 unget_format (dtp
, f
);
1823 /* This function is first called from data_init_transfer to initiate the loop
1824 over each item in the format, transferring data as required. Subsequent
1825 calls to this function occur for each data item foound in the READ/WRITE
1826 statement. The item_count is incremented for each call. Since the first
1827 call is from data_transfer_init, the item_count is always one greater than
1828 the actual count number of the item being transferred. */
1831 formatted_transfer (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
1832 size_t size
, size_t nelems
)
1838 size_t stride
= type
== BT_CHARACTER
?
1839 size
* GFC_SIZE_OF_CHAR_KIND(kind
) : size
;
1840 if (dtp
->u
.p
.mode
== READING
)
1842 /* Big loop over all the elements. */
1843 for (elem
= 0; elem
< nelems
; elem
++)
1845 dtp
->u
.p
.item_count
++;
1846 formatted_transfer_scalar_read (dtp
, type
, tmp
+ stride
*elem
, kind
, size
);
1851 /* Big loop over all the elements. */
1852 for (elem
= 0; elem
< nelems
; elem
++)
1854 dtp
->u
.p
.item_count
++;
1855 formatted_transfer_scalar_write (dtp
, type
, tmp
+ stride
*elem
, kind
, size
);
1861 /* Data transfer entry points. The type of the data entity is
1862 implicit in the subroutine call. This prevents us from having to
1863 share a common enum with the compiler. */
1866 transfer_integer (st_parameter_dt
*dtp
, void *p
, int kind
)
1868 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1870 dtp
->u
.p
.transfer (dtp
, BT_INTEGER
, p
, kind
, kind
, 1);
1874 transfer_integer_write (st_parameter_dt
*dtp
, void *p
, int kind
)
1876 transfer_integer (dtp
, p
, kind
);
1880 transfer_real (st_parameter_dt
*dtp
, void *p
, int kind
)
1883 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1885 size
= size_from_real_kind (kind
);
1886 dtp
->u
.p
.transfer (dtp
, BT_REAL
, p
, kind
, size
, 1);
1890 transfer_real_write (st_parameter_dt
*dtp
, void *p
, int kind
)
1892 transfer_real (dtp
, p
, kind
);
1896 transfer_logical (st_parameter_dt
*dtp
, void *p
, int kind
)
1898 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1900 dtp
->u
.p
.transfer (dtp
, BT_LOGICAL
, p
, kind
, kind
, 1);
1904 transfer_logical_write (st_parameter_dt
*dtp
, void *p
, int kind
)
1906 transfer_logical (dtp
, p
, kind
);
1910 transfer_character (st_parameter_dt
*dtp
, void *p
, int len
)
1912 static char *empty_string
[0];
1914 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1917 /* Strings of zero length can have p == NULL, which confuses the
1918 transfer routines into thinking we need more data elements. To avoid
1919 this, we give them a nice pointer. */
1920 if (len
== 0 && p
== NULL
)
1923 /* Set kind here to 1. */
1924 dtp
->u
.p
.transfer (dtp
, BT_CHARACTER
, p
, 1, len
, 1);
1928 transfer_character_write (st_parameter_dt
*dtp
, void *p
, int len
)
1930 transfer_character (dtp
, p
, len
);
1934 transfer_character_wide (st_parameter_dt
*dtp
, void *p
, int len
, int kind
)
1936 static char *empty_string
[0];
1938 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1941 /* Strings of zero length can have p == NULL, which confuses the
1942 transfer routines into thinking we need more data elements. To avoid
1943 this, we give them a nice pointer. */
1944 if (len
== 0 && p
== NULL
)
1947 /* Here we pass the actual kind value. */
1948 dtp
->u
.p
.transfer (dtp
, BT_CHARACTER
, p
, kind
, len
, 1);
1952 transfer_character_wide_write (st_parameter_dt
*dtp
, void *p
, int len
, int kind
)
1954 transfer_character_wide (dtp
, p
, len
, kind
);
1958 transfer_complex (st_parameter_dt
*dtp
, void *p
, int kind
)
1961 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1963 size
= size_from_complex_kind (kind
);
1964 dtp
->u
.p
.transfer (dtp
, BT_COMPLEX
, p
, kind
, size
, 1);
1968 transfer_complex_write (st_parameter_dt
*dtp
, void *p
, int kind
)
1970 transfer_complex (dtp
, p
, kind
);
1974 transfer_array (st_parameter_dt
*dtp
, gfc_array_char
*desc
, int kind
,
1975 gfc_charlen_type charlen
)
1977 index_type count
[GFC_MAX_DIMENSIONS
];
1978 index_type extent
[GFC_MAX_DIMENSIONS
];
1979 index_type stride
[GFC_MAX_DIMENSIONS
];
1980 index_type stride0
, rank
, size
, n
;
1985 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1988 iotype
= (bt
) GFC_DESCRIPTOR_TYPE (desc
);
1989 size
= iotype
== BT_CHARACTER
? charlen
: GFC_DESCRIPTOR_SIZE (desc
);
1991 rank
= GFC_DESCRIPTOR_RANK (desc
);
1992 for (n
= 0; n
< rank
; n
++)
1995 stride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(desc
,n
);
1996 extent
[n
] = GFC_DESCRIPTOR_EXTENT(desc
,n
);
1998 /* If the extent of even one dimension is zero, then the entire
1999 array section contains zero elements, so we return after writing
2000 a zero array record. */
2005 dtp
->u
.p
.transfer (dtp
, iotype
, data
, kind
, size
, tsize
);
2010 stride0
= stride
[0];
2012 /* If the innermost dimension has a stride of 1, we can do the transfer
2013 in contiguous chunks. */
2014 if (stride0
== size
)
2019 data
= GFC_DESCRIPTOR_DATA (desc
);
2023 dtp
->u
.p
.transfer (dtp
, iotype
, data
, kind
, size
, tsize
);
2024 data
+= stride0
* tsize
;
2027 while (count
[n
] == extent
[n
])
2030 data
-= stride
[n
] * extent
[n
];
2047 transfer_array_write (st_parameter_dt
*dtp
, gfc_array_char
*desc
, int kind
,
2048 gfc_charlen_type charlen
)
2050 transfer_array (dtp
, desc
, kind
, charlen
);
2053 /* Preposition a sequential unformatted file while reading. */
2056 us_read (st_parameter_dt
*dtp
, int continued
)
2063 if (compile_options
.record_marker
== 0)
2064 n
= sizeof (GFC_INTEGER_4
);
2066 n
= compile_options
.record_marker
;
2068 nr
= sread (dtp
->u
.p
.current_unit
->s
, &i
, n
);
2069 if (unlikely (nr
< 0))
2071 generate_error (&dtp
->common
, LIBERROR_BAD_US
, NULL
);
2077 return; /* end of file */
2079 else if (unlikely (n
!= nr
))
2081 generate_error (&dtp
->common
, LIBERROR_BAD_US
, NULL
);
2085 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
2086 if (likely (dtp
->u
.p
.current_unit
->flags
.convert
== GFC_CONVERT_NATIVE
))
2090 case sizeof(GFC_INTEGER_4
):
2091 memcpy (&i4
, &i
, sizeof (i4
));
2095 case sizeof(GFC_INTEGER_8
):
2096 memcpy (&i8
, &i
, sizeof (i8
));
2101 runtime_error ("Illegal value for record marker");
2108 case sizeof(GFC_INTEGER_4
):
2109 reverse_memcpy (&i4
, &i
, sizeof (i4
));
2113 case sizeof(GFC_INTEGER_8
):
2114 reverse_memcpy (&i8
, &i
, sizeof (i8
));
2119 runtime_error ("Illegal value for record marker");
2125 dtp
->u
.p
.current_unit
->bytes_left_subrecord
= i
;
2126 dtp
->u
.p
.current_unit
->continued
= 0;
2130 dtp
->u
.p
.current_unit
->bytes_left_subrecord
= -i
;
2131 dtp
->u
.p
.current_unit
->continued
= 1;
2135 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2139 /* Preposition a sequential unformatted file while writing. This
2140 amount to writing a bogus length that will be filled in later. */
2143 us_write (st_parameter_dt
*dtp
, int continued
)
2150 if (compile_options
.record_marker
== 0)
2151 nbytes
= sizeof (GFC_INTEGER_4
);
2153 nbytes
= compile_options
.record_marker
;
2155 if (swrite (dtp
->u
.p
.current_unit
->s
, &dummy
, nbytes
) != nbytes
)
2156 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2158 /* For sequential unformatted, if RECL= was not specified in the OPEN
2159 we write until we have more bytes than can fit in the subrecord
2160 markers, then we write a new subrecord. */
2162 dtp
->u
.p
.current_unit
->bytes_left_subrecord
=
2163 dtp
->u
.p
.current_unit
->recl_subrecord
;
2164 dtp
->u
.p
.current_unit
->continued
= continued
;
2168 /* Position to the next record prior to transfer. We are assumed to
2169 be before the next record. We also calculate the bytes in the next
2173 pre_position (st_parameter_dt
*dtp
)
2175 if (dtp
->u
.p
.current_unit
->current_record
)
2176 return; /* Already positioned. */
2178 switch (current_mode (dtp
))
2180 case FORMATTED_STREAM
:
2181 case UNFORMATTED_STREAM
:
2182 /* There are no records with stream I/O. If the position was specified
2183 data_transfer_init has already positioned the file. If no position
2184 was specified, we continue from where we last left off. I.e.
2185 there is nothing to do here. */
2188 case UNFORMATTED_SEQUENTIAL
:
2189 if (dtp
->u
.p
.mode
== READING
)
2196 case FORMATTED_SEQUENTIAL
:
2197 case FORMATTED_DIRECT
:
2198 case UNFORMATTED_DIRECT
:
2199 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2203 dtp
->u
.p
.current_unit
->current_record
= 1;
2207 /* Initialize things for a data transfer. This code is common for
2208 both reading and writing. */
2211 data_transfer_init (st_parameter_dt
*dtp
, int read_flag
)
2213 unit_flags u_flags
; /* Used for creating a unit if needed. */
2214 GFC_INTEGER_4 cf
= dtp
->common
.flags
;
2215 namelist_info
*ionml
;
2217 ionml
= ((cf
& IOPARM_DT_IONML_SET
) != 0) ? dtp
->u
.p
.ionml
: NULL
;
2219 memset (&dtp
->u
.p
, 0, sizeof (dtp
->u
.p
));
2221 dtp
->u
.p
.ionml
= ionml
;
2222 dtp
->u
.p
.mode
= read_flag
? READING
: WRITING
;
2224 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
2227 if ((cf
& IOPARM_DT_HAS_SIZE
) != 0)
2228 dtp
->u
.p
.size_used
= 0; /* Initialize the count. */
2230 dtp
->u
.p
.current_unit
= get_unit (dtp
, 1);
2231 if (dtp
->u
.p
.current_unit
->s
== NULL
)
2232 { /* Open the unit with some default flags. */
2233 st_parameter_open opp
;
2236 if (dtp
->common
.unit
< 0)
2238 close_unit (dtp
->u
.p
.current_unit
);
2239 dtp
->u
.p
.current_unit
= NULL
;
2240 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2241 "Bad unit number in statement");
2244 memset (&u_flags
, '\0', sizeof (u_flags
));
2245 u_flags
.access
= ACCESS_SEQUENTIAL
;
2246 u_flags
.action
= ACTION_READWRITE
;
2248 /* Is it unformatted? */
2249 if (!(cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
2250 | IOPARM_DT_IONML_SET
)))
2251 u_flags
.form
= FORM_UNFORMATTED
;
2253 u_flags
.form
= FORM_UNSPECIFIED
;
2255 u_flags
.delim
= DELIM_UNSPECIFIED
;
2256 u_flags
.blank
= BLANK_UNSPECIFIED
;
2257 u_flags
.pad
= PAD_UNSPECIFIED
;
2258 u_flags
.decimal
= DECIMAL_UNSPECIFIED
;
2259 u_flags
.encoding
= ENCODING_UNSPECIFIED
;
2260 u_flags
.async
= ASYNC_UNSPECIFIED
;
2261 u_flags
.round
= ROUND_UNSPECIFIED
;
2262 u_flags
.sign
= SIGN_UNSPECIFIED
;
2264 u_flags
.status
= STATUS_UNKNOWN
;
2266 conv
= get_unformatted_convert (dtp
->common
.unit
);
2268 if (conv
== GFC_CONVERT_NONE
)
2269 conv
= compile_options
.convert
;
2271 /* We use big_endian, which is 0 on little-endian machines
2272 and 1 on big-endian machines. */
2275 case GFC_CONVERT_NATIVE
:
2276 case GFC_CONVERT_SWAP
:
2279 case GFC_CONVERT_BIG
:
2280 conv
= big_endian
? GFC_CONVERT_NATIVE
: GFC_CONVERT_SWAP
;
2283 case GFC_CONVERT_LITTLE
:
2284 conv
= big_endian
? GFC_CONVERT_SWAP
: GFC_CONVERT_NATIVE
;
2288 internal_error (&opp
.common
, "Illegal value for CONVERT");
2292 u_flags
.convert
= conv
;
2294 opp
.common
= dtp
->common
;
2295 opp
.common
.flags
&= IOPARM_COMMON_MASK
;
2296 dtp
->u
.p
.current_unit
= new_unit (&opp
, dtp
->u
.p
.current_unit
, &u_flags
);
2297 dtp
->common
.flags
&= ~IOPARM_COMMON_MASK
;
2298 dtp
->common
.flags
|= (opp
.common
.flags
& IOPARM_COMMON_MASK
);
2299 if (dtp
->u
.p
.current_unit
== NULL
)
2303 /* Check the action. */
2305 if (read_flag
&& dtp
->u
.p
.current_unit
->flags
.action
== ACTION_WRITE
)
2307 generate_error (&dtp
->common
, LIBERROR_BAD_ACTION
,
2308 "Cannot read from file opened for WRITE");
2312 if (!read_flag
&& dtp
->u
.p
.current_unit
->flags
.action
== ACTION_READ
)
2314 generate_error (&dtp
->common
, LIBERROR_BAD_ACTION
,
2315 "Cannot write to file opened for READ");
2319 dtp
->u
.p
.first_item
= 1;
2321 /* Check the format. */
2323 if ((cf
& IOPARM_DT_HAS_FORMAT
) != 0)
2326 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
2327 && (cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
))
2330 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2331 "Format present for UNFORMATTED data transfer");
2335 if ((cf
& IOPARM_DT_HAS_NAMELIST_NAME
) != 0 && dtp
->u
.p
.ionml
!= NULL
)
2337 if ((cf
& IOPARM_DT_HAS_FORMAT
) != 0)
2338 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2339 "A format cannot be specified with a namelist");
2341 else if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
&&
2342 !(cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
)))
2344 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2345 "Missing format for FORMATTED data transfer");
2348 if (is_internal_unit (dtp
)
2349 && dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
)
2351 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2352 "Internal file cannot be accessed by UNFORMATTED "
2357 /* Check the record or position number. */
2359 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
2360 && (cf
& IOPARM_DT_HAS_REC
) == 0)
2362 generate_error (&dtp
->common
, LIBERROR_MISSING_OPTION
,
2363 "Direct access data transfer requires record number");
2367 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
2369 if ((cf
& IOPARM_DT_HAS_REC
) != 0)
2371 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2372 "Record number not allowed for sequential access "
2377 if (dtp
->u
.p
.current_unit
->endfile
== AFTER_ENDFILE
)
2379 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2380 "Sequential READ or WRITE not allowed after "
2381 "EOF marker, possibly use REWIND or BACKSPACE");
2386 /* Process the ADVANCE option. */
2388 dtp
->u
.p
.advance_status
2389 = !(cf
& IOPARM_DT_HAS_ADVANCE
) ? ADVANCE_UNSPECIFIED
:
2390 find_option (&dtp
->common
, dtp
->advance
, dtp
->advance_len
, advance_opt
,
2391 "Bad ADVANCE parameter in data transfer statement");
2393 if (dtp
->u
.p
.advance_status
!= ADVANCE_UNSPECIFIED
)
2395 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
2397 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2398 "ADVANCE specification conflicts with sequential "
2403 if (is_internal_unit (dtp
))
2405 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2406 "ADVANCE specification conflicts with internal file");
2410 if ((cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
))
2411 != IOPARM_DT_HAS_FORMAT
)
2413 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2414 "ADVANCE specification requires an explicit format");
2421 dtp
->u
.p
.current_unit
->previous_nonadvancing_write
= 0;
2423 if ((cf
& IOPARM_EOR
) != 0 && dtp
->u
.p
.advance_status
!= ADVANCE_NO
)
2425 generate_error (&dtp
->common
, LIBERROR_MISSING_OPTION
,
2426 "EOR specification requires an ADVANCE specification "
2431 if ((cf
& IOPARM_DT_HAS_SIZE
) != 0
2432 && dtp
->u
.p
.advance_status
!= ADVANCE_NO
)
2434 generate_error (&dtp
->common
, LIBERROR_MISSING_OPTION
,
2435 "SIZE specification requires an ADVANCE "
2436 "specification of NO");
2441 { /* Write constraints. */
2442 if ((cf
& IOPARM_END
) != 0)
2444 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2445 "END specification cannot appear in a write "
2450 if ((cf
& IOPARM_EOR
) != 0)
2452 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2453 "EOR specification cannot appear in a write "
2458 if ((cf
& IOPARM_DT_HAS_SIZE
) != 0)
2460 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2461 "SIZE specification cannot appear in a write "
2467 if (dtp
->u
.p
.advance_status
== ADVANCE_UNSPECIFIED
)
2468 dtp
->u
.p
.advance_status
= ADVANCE_YES
;
2470 /* Check the decimal mode. */
2471 dtp
->u
.p
.current_unit
->decimal_status
2472 = !(cf
& IOPARM_DT_HAS_DECIMAL
) ? DECIMAL_UNSPECIFIED
:
2473 find_option (&dtp
->common
, dtp
->decimal
, dtp
->decimal_len
,
2474 decimal_opt
, "Bad DECIMAL parameter in data transfer "
2477 if (dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_UNSPECIFIED
)
2478 dtp
->u
.p
.current_unit
->decimal_status
= dtp
->u
.p
.current_unit
->flags
.decimal
;
2480 /* Check the round mode. */
2481 dtp
->u
.p
.current_unit
->round_status
2482 = !(cf
& IOPARM_DT_HAS_ROUND
) ? ROUND_UNSPECIFIED
:
2483 find_option (&dtp
->common
, dtp
->round
, dtp
->round_len
,
2484 round_opt
, "Bad ROUND parameter in data transfer "
2487 if (dtp
->u
.p
.current_unit
->round_status
== ROUND_UNSPECIFIED
)
2488 dtp
->u
.p
.current_unit
->round_status
= dtp
->u
.p
.current_unit
->flags
.round
;
2490 /* Check the sign mode. */
2491 dtp
->u
.p
.sign_status
2492 = !(cf
& IOPARM_DT_HAS_SIGN
) ? SIGN_UNSPECIFIED
:
2493 find_option (&dtp
->common
, dtp
->sign
, dtp
->sign_len
, sign_opt
,
2494 "Bad SIGN parameter in data transfer statement");
2496 if (dtp
->u
.p
.sign_status
== SIGN_UNSPECIFIED
)
2497 dtp
->u
.p
.sign_status
= dtp
->u
.p
.current_unit
->flags
.sign
;
2499 /* Check the blank mode. */
2500 dtp
->u
.p
.blank_status
2501 = !(cf
& IOPARM_DT_HAS_BLANK
) ? BLANK_UNSPECIFIED
:
2502 find_option (&dtp
->common
, dtp
->blank
, dtp
->blank_len
,
2504 "Bad BLANK parameter in data transfer statement");
2506 if (dtp
->u
.p
.blank_status
== BLANK_UNSPECIFIED
)
2507 dtp
->u
.p
.blank_status
= dtp
->u
.p
.current_unit
->flags
.blank
;
2509 /* Check the delim mode. */
2510 dtp
->u
.p
.current_unit
->delim_status
2511 = !(cf
& IOPARM_DT_HAS_DELIM
) ? DELIM_UNSPECIFIED
:
2512 find_option (&dtp
->common
, dtp
->delim
, dtp
->delim_len
,
2513 delim_opt
, "Bad DELIM parameter in data transfer statement");
2515 if (dtp
->u
.p
.current_unit
->delim_status
== DELIM_UNSPECIFIED
)
2516 dtp
->u
.p
.current_unit
->delim_status
= dtp
->u
.p
.current_unit
->flags
.delim
;
2518 /* Check the pad mode. */
2519 dtp
->u
.p
.current_unit
->pad_status
2520 = !(cf
& IOPARM_DT_HAS_PAD
) ? PAD_UNSPECIFIED
:
2521 find_option (&dtp
->common
, dtp
->pad
, dtp
->pad_len
, pad_opt
,
2522 "Bad PAD parameter in data transfer statement");
2524 if (dtp
->u
.p
.current_unit
->pad_status
== PAD_UNSPECIFIED
)
2525 dtp
->u
.p
.current_unit
->pad_status
= dtp
->u
.p
.current_unit
->flags
.pad
;
2527 /* Check to see if we might be reading what we wrote before */
2529 if (dtp
->u
.p
.mode
!= dtp
->u
.p
.current_unit
->mode
2530 && !is_internal_unit (dtp
))
2532 int pos
= fbuf_reset (dtp
->u
.p
.current_unit
);
2534 sseek (dtp
->u
.p
.current_unit
->s
, pos
, SEEK_CUR
);
2535 sflush(dtp
->u
.p
.current_unit
->s
);
2538 /* Check the POS= specifier: that it is in range and that it is used with a
2539 unit that has been connected for STREAM access. F2003 9.5.1.10. */
2541 if (((cf
& IOPARM_DT_HAS_POS
) != 0))
2543 if (is_stream_io (dtp
))
2548 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2549 "POS=specifier must be positive");
2553 if (dtp
->pos
>= dtp
->u
.p
.current_unit
->maxrec
)
2555 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2556 "POS=specifier too large");
2560 dtp
->rec
= dtp
->pos
;
2562 if (dtp
->u
.p
.mode
== READING
)
2564 /* Reset the endfile flag; if we hit EOF during reading
2565 we'll set the flag and generate an error at that point
2566 rather than worrying about it here. */
2567 dtp
->u
.p
.current_unit
->endfile
= NO_ENDFILE
;
2570 if (dtp
->pos
!= dtp
->u
.p
.current_unit
->strm_pos
)
2572 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
2573 if (sseek (dtp
->u
.p
.current_unit
->s
, dtp
->pos
- 1, SEEK_SET
) < 0)
2575 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2578 dtp
->u
.p
.current_unit
->strm_pos
= dtp
->pos
;
2583 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2584 "POS=specifier not allowed, "
2585 "Try OPEN with ACCESS='stream'");
2591 /* Sanity checks on the record number. */
2592 if ((cf
& IOPARM_DT_HAS_REC
) != 0)
2596 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2597 "Record number must be positive");
2601 if (dtp
->rec
>= dtp
->u
.p
.current_unit
->maxrec
)
2603 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2604 "Record number too large");
2608 /* Make sure format buffer is reset. */
2609 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
)
2610 fbuf_reset (dtp
->u
.p
.current_unit
);
2613 /* Check whether the record exists to be read. Only
2614 a partial record needs to exist. */
2616 if (dtp
->u
.p
.mode
== READING
&& (dtp
->rec
- 1)
2617 * dtp
->u
.p
.current_unit
->recl
>= file_length (dtp
->u
.p
.current_unit
->s
))
2619 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2620 "Non-existing record number");
2624 /* Position the file. */
2625 if (sseek (dtp
->u
.p
.current_unit
->s
, (gfc_offset
) (dtp
->rec
- 1)
2626 * dtp
->u
.p
.current_unit
->recl
, SEEK_SET
) < 0)
2628 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2632 /* TODO: This is required to maintain compatibility between
2633 4.3 and 4.4 runtime. Remove when ABI changes from 4.3 */
2635 if (is_stream_io (dtp
))
2636 dtp
->u
.p
.current_unit
->strm_pos
= dtp
->rec
;
2638 /* TODO: Un-comment this code when ABI changes from 4.3.
2639 if (dtp->u.p.current_unit->flags.access == ACCESS_STREAM)
2641 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2642 "Record number not allowed for stream access "
2648 /* Bugware for badly written mixed C-Fortran I/O. */
2649 flush_if_preconnected(dtp
->u
.p
.current_unit
->s
);
2651 dtp
->u
.p
.current_unit
->mode
= dtp
->u
.p
.mode
;
2653 /* Set the maximum position reached from the previous I/O operation. This
2654 could be greater than zero from a previous non-advancing write. */
2655 dtp
->u
.p
.max_pos
= dtp
->u
.p
.current_unit
->saved_pos
;
2660 /* Set up the subroutine that will handle the transfers. */
2664 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
)
2665 dtp
->u
.p
.transfer
= unformatted_read
;
2668 if ((cf
& IOPARM_DT_LIST_FORMAT
) != 0)
2669 dtp
->u
.p
.transfer
= list_formatted_read
;
2671 dtp
->u
.p
.transfer
= formatted_transfer
;
2676 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
)
2677 dtp
->u
.p
.transfer
= unformatted_write
;
2680 if ((cf
& IOPARM_DT_LIST_FORMAT
) != 0)
2681 dtp
->u
.p
.transfer
= list_formatted_write
;
2683 dtp
->u
.p
.transfer
= formatted_transfer
;
2687 /* Make sure that we don't do a read after a nonadvancing write. */
2691 if (dtp
->u
.p
.current_unit
->read_bad
&& !is_stream_io (dtp
))
2693 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2694 "Cannot READ after a nonadvancing WRITE");
2700 if (dtp
->u
.p
.advance_status
== ADVANCE_YES
&& !dtp
->u
.p
.seen_dollar
)
2701 dtp
->u
.p
.current_unit
->read_bad
= 1;
2704 /* Start the data transfer if we are doing a formatted transfer. */
2705 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
2706 && ((cf
& (IOPARM_DT_LIST_FORMAT
| IOPARM_DT_HAS_NAMELIST_NAME
)) == 0)
2707 && dtp
->u
.p
.ionml
== NULL
)
2708 formatted_transfer (dtp
, 0, NULL
, 0, 0, 1);
2711 /* Initialize an array_loop_spec given the array descriptor. The function
2712 returns the index of the last element of the array, and also returns
2713 starting record, where the first I/O goes to (necessary in case of
2714 negative strides). */
2717 init_loop_spec (gfc_array_char
*desc
, array_loop_spec
*ls
,
2718 gfc_offset
*start_record
)
2720 int rank
= GFC_DESCRIPTOR_RANK(desc
);
2729 for (i
=0; i
<rank
; i
++)
2731 ls
[i
].idx
= GFC_DESCRIPTOR_LBOUND(desc
,i
);
2732 ls
[i
].start
= GFC_DESCRIPTOR_LBOUND(desc
,i
);
2733 ls
[i
].end
= GFC_DESCRIPTOR_UBOUND(desc
,i
);
2734 ls
[i
].step
= GFC_DESCRIPTOR_STRIDE(desc
,i
);
2735 empty
= empty
|| (GFC_DESCRIPTOR_UBOUND(desc
,i
)
2736 < GFC_DESCRIPTOR_LBOUND(desc
,i
));
2738 if (GFC_DESCRIPTOR_STRIDE(desc
,i
) > 0)
2740 index
+= (GFC_DESCRIPTOR_EXTENT(desc
,i
) - 1)
2741 * GFC_DESCRIPTOR_STRIDE(desc
,i
);
2745 index
-= (GFC_DESCRIPTOR_EXTENT(desc
,i
) - 1)
2746 * GFC_DESCRIPTOR_STRIDE(desc
,i
);
2747 *start_record
-= (GFC_DESCRIPTOR_EXTENT(desc
,i
) - 1)
2748 * GFC_DESCRIPTOR_STRIDE(desc
,i
);
2758 /* Determine the index to the next record in an internal unit array by
2759 by incrementing through the array_loop_spec. */
2762 next_array_record (st_parameter_dt
*dtp
, array_loop_spec
*ls
, int *finished
)
2770 for (i
= 0; i
< dtp
->u
.p
.current_unit
->rank
; i
++)
2775 if (ls
[i
].idx
> ls
[i
].end
)
2777 ls
[i
].idx
= ls
[i
].start
;
2783 index
= index
+ (ls
[i
].idx
- ls
[i
].start
) * ls
[i
].step
;
2793 /* Skip to the end of the current record, taking care of an optional
2794 record marker of size bytes. If the file is not seekable, we
2795 read chunks of size MAX_READ until we get to the right
2799 skip_record (st_parameter_dt
*dtp
, ssize_t bytes
)
2801 ssize_t rlength
, readb
;
2802 static const ssize_t MAX_READ
= 4096;
2805 dtp
->u
.p
.current_unit
->bytes_left_subrecord
+= bytes
;
2806 if (dtp
->u
.p
.current_unit
->bytes_left_subrecord
== 0)
2809 if (is_seekable (dtp
->u
.p
.current_unit
->s
))
2811 /* Direct access files do not generate END conditions,
2813 if (sseek (dtp
->u
.p
.current_unit
->s
,
2814 dtp
->u
.p
.current_unit
->bytes_left_subrecord
, SEEK_CUR
) < 0)
2815 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2817 dtp
->u
.p
.current_unit
->bytes_left_subrecord
= 0;
2820 { /* Seek by reading data. */
2821 while (dtp
->u
.p
.current_unit
->bytes_left_subrecord
> 0)
2824 (MAX_READ
< dtp
->u
.p
.current_unit
->bytes_left_subrecord
) ?
2825 MAX_READ
: dtp
->u
.p
.current_unit
->bytes_left_subrecord
;
2827 readb
= sread (dtp
->u
.p
.current_unit
->s
, p
, rlength
);
2830 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2834 dtp
->u
.p
.current_unit
->bytes_left_subrecord
-= readb
;
2841 /* Advance to the next record reading unformatted files, taking
2842 care of subrecords. If complete_record is nonzero, we loop
2843 until all subrecords are cleared. */
2846 next_record_r_unf (st_parameter_dt
*dtp
, int complete_record
)
2850 bytes
= compile_options
.record_marker
== 0 ?
2851 sizeof (GFC_INTEGER_4
) : compile_options
.record_marker
;
2856 /* Skip over tail */
2858 skip_record (dtp
, bytes
);
2860 if ( ! (complete_record
&& dtp
->u
.p
.current_unit
->continued
))
2868 static inline gfc_offset
2869 min_off (gfc_offset a
, gfc_offset b
)
2871 return (a
< b
? a
: b
);
2875 /* Space to the next record for read mode. */
2878 next_record_r (st_parameter_dt
*dtp
, int done
)
2885 switch (current_mode (dtp
))
2887 /* No records in unformatted STREAM I/O. */
2888 case UNFORMATTED_STREAM
:
2891 case UNFORMATTED_SEQUENTIAL
:
2892 next_record_r_unf (dtp
, 1);
2893 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2896 case FORMATTED_DIRECT
:
2897 case UNFORMATTED_DIRECT
:
2898 skip_record (dtp
, dtp
->u
.p
.current_unit
->bytes_left
);
2901 case FORMATTED_STREAM
:
2902 case FORMATTED_SEQUENTIAL
:
2903 /* read_sf has already terminated input because of an '\n', or
2905 if (dtp
->u
.p
.sf_seen_eor
)
2907 dtp
->u
.p
.sf_seen_eor
= 0;
2911 if (is_internal_unit (dtp
))
2913 if (is_array_io (dtp
))
2917 record
= next_array_record (dtp
, dtp
->u
.p
.current_unit
->ls
,
2919 if (!done
&& finished
)
2922 /* Now seek to this record. */
2923 record
= record
* dtp
->u
.p
.current_unit
->recl
;
2924 if (sseek (dtp
->u
.p
.current_unit
->s
, record
, SEEK_SET
) < 0)
2926 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
2929 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2933 bytes_left
= (int) dtp
->u
.p
.current_unit
->bytes_left
;
2934 bytes_left
= min_off (bytes_left
,
2935 file_length (dtp
->u
.p
.current_unit
->s
)
2936 - stell (dtp
->u
.p
.current_unit
->s
));
2937 if (sseek (dtp
->u
.p
.current_unit
->s
,
2938 bytes_left
, SEEK_CUR
) < 0)
2940 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
2943 dtp
->u
.p
.current_unit
->bytes_left
2944 = dtp
->u
.p
.current_unit
->recl
;
2953 cc
= fbuf_getc (dtp
->u
.p
.current_unit
);
2957 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2960 if (is_stream_io (dtp
)
2961 || dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
2962 || dtp
->u
.p
.current_unit
->bytes_left
2963 == dtp
->u
.p
.current_unit
->recl
)
2969 if (is_stream_io (dtp
))
2970 dtp
->u
.p
.current_unit
->strm_pos
++;
2981 /* Small utility function to write a record marker, taking care of
2982 byte swapping and of choosing the correct size. */
2985 write_us_marker (st_parameter_dt
*dtp
, const gfc_offset buf
)
2990 char p
[sizeof (GFC_INTEGER_8
)];
2992 if (compile_options
.record_marker
== 0)
2993 len
= sizeof (GFC_INTEGER_4
);
2995 len
= compile_options
.record_marker
;
2997 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
2998 if (likely (dtp
->u
.p
.current_unit
->flags
.convert
== GFC_CONVERT_NATIVE
))
3002 case sizeof (GFC_INTEGER_4
):
3004 return swrite (dtp
->u
.p
.current_unit
->s
, &buf4
, len
);
3007 case sizeof (GFC_INTEGER_8
):
3009 return swrite (dtp
->u
.p
.current_unit
->s
, &buf8
, len
);
3013 runtime_error ("Illegal value for record marker");
3021 case sizeof (GFC_INTEGER_4
):
3023 reverse_memcpy (p
, &buf4
, sizeof (GFC_INTEGER_4
));
3024 return swrite (dtp
->u
.p
.current_unit
->s
, p
, len
);
3027 case sizeof (GFC_INTEGER_8
):
3029 reverse_memcpy (p
, &buf8
, sizeof (GFC_INTEGER_8
));
3030 return swrite (dtp
->u
.p
.current_unit
->s
, p
, len
);
3034 runtime_error ("Illegal value for record marker");
3041 /* Position to the next (sub)record in write mode for
3042 unformatted sequential files. */
3045 next_record_w_unf (st_parameter_dt
*dtp
, int next_subrecord
)
3047 gfc_offset m
, m_write
, record_marker
;
3049 /* Bytes written. */
3050 m
= dtp
->u
.p
.current_unit
->recl_subrecord
3051 - dtp
->u
.p
.current_unit
->bytes_left_subrecord
;
3053 /* Write the length tail. If we finish a record containing
3054 subrecords, we write out the negative length. */
3056 if (dtp
->u
.p
.current_unit
->continued
)
3061 if (unlikely (write_us_marker (dtp
, m_write
) < 0))
3064 if (compile_options
.record_marker
== 0)
3065 record_marker
= sizeof (GFC_INTEGER_4
);
3067 record_marker
= compile_options
.record_marker
;
3069 /* Seek to the head and overwrite the bogus length with the real
3072 if (unlikely (sseek (dtp
->u
.p
.current_unit
->s
, - m
- 2 * record_marker
,
3081 if (unlikely (write_us_marker (dtp
, m_write
) < 0))
3084 /* Seek past the end of the current record. */
3086 if (unlikely (sseek (dtp
->u
.p
.current_unit
->s
, m
+ record_marker
,
3093 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
3099 /* Utility function like memset() but operating on streams. Return
3100 value is same as for POSIX write(). */
3103 sset (stream
* s
, int c
, ssize_t nbyte
)
3105 static const int WRITE_CHUNK
= 256;
3106 char p
[WRITE_CHUNK
];
3107 ssize_t bytes_left
, trans
;
3109 if (nbyte
< WRITE_CHUNK
)
3110 memset (p
, c
, nbyte
);
3112 memset (p
, c
, WRITE_CHUNK
);
3115 while (bytes_left
> 0)
3117 trans
= (bytes_left
< WRITE_CHUNK
) ? bytes_left
: WRITE_CHUNK
;
3118 trans
= swrite (s
, p
, trans
);
3121 bytes_left
-= trans
;
3124 return nbyte
- bytes_left
;
3128 memset4 (gfc_char4_t
*p
, gfc_char4_t c
, int k
)
3131 for (j
= 0; j
< k
; j
++)
3135 /* Position to the next record in write mode. */
3138 next_record_w (st_parameter_dt
*dtp
, int done
)
3140 gfc_offset m
, record
, max_pos
;
3143 /* Zero counters for X- and T-editing. */
3144 max_pos
= dtp
->u
.p
.max_pos
;
3145 dtp
->u
.p
.max_pos
= dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
3147 switch (current_mode (dtp
))
3149 /* No records in unformatted STREAM I/O. */
3150 case UNFORMATTED_STREAM
:
3153 case FORMATTED_DIRECT
:
3154 if (dtp
->u
.p
.current_unit
->bytes_left
== 0)
3157 fbuf_seek (dtp
->u
.p
.current_unit
, 0, SEEK_END
);
3158 fbuf_flush (dtp
->u
.p
.current_unit
, WRITING
);
3159 if (sset (dtp
->u
.p
.current_unit
->s
, ' ',
3160 dtp
->u
.p
.current_unit
->bytes_left
)
3161 != dtp
->u
.p
.current_unit
->bytes_left
)
3166 case UNFORMATTED_DIRECT
:
3167 if (dtp
->u
.p
.current_unit
->bytes_left
> 0)
3169 length
= (int) dtp
->u
.p
.current_unit
->bytes_left
;
3170 if (sset (dtp
->u
.p
.current_unit
->s
, 0, length
) != length
)
3175 case UNFORMATTED_SEQUENTIAL
:
3176 next_record_w_unf (dtp
, 0);
3177 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
3180 case FORMATTED_STREAM
:
3181 case FORMATTED_SEQUENTIAL
:
3183 if (is_internal_unit (dtp
))
3186 if (is_array_io (dtp
))
3190 length
= (int) dtp
->u
.p
.current_unit
->bytes_left
;
3192 /* If the farthest position reached is greater than current
3193 position, adjust the position and set length to pad out
3194 whats left. Otherwise just pad whats left.
3195 (for character array unit) */
3196 m
= dtp
->u
.p
.current_unit
->recl
3197 - dtp
->u
.p
.current_unit
->bytes_left
;
3200 length
= (int) (max_pos
- m
);
3201 if (sseek (dtp
->u
.p
.current_unit
->s
,
3202 length
, SEEK_CUR
) < 0)
3204 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
3207 length
= (int) (dtp
->u
.p
.current_unit
->recl
- max_pos
);
3210 p
= write_block (dtp
, length
);
3214 if (unlikely (is_char4_unit (dtp
)))
3216 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
3217 memset4 (p4
, ' ', length
);
3220 memset (p
, ' ', length
);
3222 /* Now that the current record has been padded out,
3223 determine where the next record in the array is. */
3224 record
= next_array_record (dtp
, dtp
->u
.p
.current_unit
->ls
,
3227 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
3229 /* Now seek to this record */
3230 record
= record
* dtp
->u
.p
.current_unit
->recl
;
3232 if (sseek (dtp
->u
.p
.current_unit
->s
, record
, SEEK_SET
) < 0)
3234 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
3238 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
3244 /* If this is the last call to next_record move to the farthest
3245 position reached and set length to pad out the remainder
3246 of the record. (for character scaler unit) */
3249 m
= dtp
->u
.p
.current_unit
->recl
3250 - dtp
->u
.p
.current_unit
->bytes_left
;
3253 length
= (int) (max_pos
- m
);
3254 if (sseek (dtp
->u
.p
.current_unit
->s
,
3255 length
, SEEK_CUR
) < 0)
3257 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
3260 length
= (int) (dtp
->u
.p
.current_unit
->recl
- max_pos
);
3263 length
= (int) dtp
->u
.p
.current_unit
->bytes_left
;
3267 p
= write_block (dtp
, length
);
3271 if (unlikely (is_char4_unit (dtp
)))
3273 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
3274 memset4 (p4
, (gfc_char4_t
) ' ', length
);
3277 memset (p
, ' ', length
);
3288 fbuf_seek (dtp
->u
.p
.current_unit
, 0, SEEK_END
);
3289 char * p
= fbuf_alloc (dtp
->u
.p
.current_unit
, len
);
3296 if (is_stream_io (dtp
))
3298 dtp
->u
.p
.current_unit
->strm_pos
+= len
;
3299 if (dtp
->u
.p
.current_unit
->strm_pos
3300 < file_length (dtp
->u
.p
.current_unit
->s
))
3301 unit_truncate (dtp
->u
.p
.current_unit
,
3302 dtp
->u
.p
.current_unit
->strm_pos
- 1,
3310 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
3315 /* Position to the next record, which means moving to the end of the
3316 current record. This can happen under several different
3317 conditions. If the done flag is not set, we get ready to process
3321 next_record (st_parameter_dt
*dtp
, int done
)
3323 gfc_offset fp
; /* File position. */
3325 dtp
->u
.p
.current_unit
->read_bad
= 0;
3327 if (dtp
->u
.p
.mode
== READING
)
3328 next_record_r (dtp
, done
);
3330 next_record_w (dtp
, done
);
3332 if (!is_stream_io (dtp
))
3334 /* Keep position up to date for INQUIRE */
3336 update_position (dtp
->u
.p
.current_unit
);
3338 dtp
->u
.p
.current_unit
->current_record
= 0;
3339 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
3341 fp
= stell (dtp
->u
.p
.current_unit
->s
);
3342 /* Calculate next record, rounding up partial records. */
3343 dtp
->u
.p
.current_unit
->last_record
=
3344 (fp
+ dtp
->u
.p
.current_unit
->recl
- 1) /
3345 dtp
->u
.p
.current_unit
->recl
;
3348 dtp
->u
.p
.current_unit
->last_record
++;
3354 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
3358 /* Finalize the current data transfer. For a nonadvancing transfer,
3359 this means advancing to the next record. For internal units close the
3360 stream associated with the unit. */
3363 finalize_transfer (st_parameter_dt
*dtp
)
3366 GFC_INTEGER_4 cf
= dtp
->common
.flags
;
3368 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
3369 *dtp
->size
= dtp
->u
.p
.size_used
;
3371 if (dtp
->u
.p
.eor_condition
)
3373 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
3377 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
3379 if (dtp
->u
.p
.current_unit
&& current_mode (dtp
) == UNFORMATTED_SEQUENTIAL
)
3380 dtp
->u
.p
.current_unit
->current_record
= 0;
3384 if ((dtp
->u
.p
.ionml
!= NULL
)
3385 && (cf
& IOPARM_DT_HAS_NAMELIST_NAME
) != 0)
3387 if ((cf
& IOPARM_DT_NAMELIST_READ_MODE
) != 0)
3388 namelist_read (dtp
);
3390 namelist_write (dtp
);
3393 dtp
->u
.p
.transfer
= NULL
;
3394 if (dtp
->u
.p
.current_unit
== NULL
)
3397 dtp
->u
.p
.eof_jump
= &eof_jump
;
3398 if (setjmp (eof_jump
))
3400 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
3404 if ((cf
& IOPARM_DT_LIST_FORMAT
) != 0 && dtp
->u
.p
.mode
== READING
)
3406 finish_list_read (dtp
);
3410 if (dtp
->u
.p
.mode
== WRITING
)
3411 dtp
->u
.p
.current_unit
->previous_nonadvancing_write
3412 = dtp
->u
.p
.advance_status
== ADVANCE_NO
;
3414 if (is_stream_io (dtp
))
3416 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
3417 && dtp
->u
.p
.advance_status
!= ADVANCE_NO
)
3418 next_record (dtp
, 1);
3423 dtp
->u
.p
.current_unit
->current_record
= 0;
3425 if (!is_internal_unit (dtp
) && dtp
->u
.p
.seen_dollar
)
3427 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
3428 dtp
->u
.p
.seen_dollar
= 0;
3432 /* For non-advancing I/O, save the current maximum position for use in the
3433 next I/O operation if needed. */
3434 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
)
3436 int bytes_written
= (int) (dtp
->u
.p
.current_unit
->recl
3437 - dtp
->u
.p
.current_unit
->bytes_left
);
3438 dtp
->u
.p
.current_unit
->saved_pos
=
3439 dtp
->u
.p
.max_pos
> 0 ? dtp
->u
.p
.max_pos
- bytes_written
: 0;
3440 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
3443 else if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
3444 && dtp
->u
.p
.mode
== WRITING
&& !is_internal_unit (dtp
))
3445 fbuf_seek (dtp
->u
.p
.current_unit
, 0, SEEK_END
);
3447 dtp
->u
.p
.current_unit
->saved_pos
= 0;
3449 next_record (dtp
, 1);
3452 /* Transfer function for IOLENGTH. It doesn't actually do any
3453 data transfer, it just updates the length counter. */
3456 iolength_transfer (st_parameter_dt
*dtp
, bt type
__attribute__((unused
)),
3457 void *dest
__attribute__ ((unused
)),
3458 int kind
__attribute__((unused
)),
3459 size_t size
, size_t nelems
)
3461 if ((dtp
->common
.flags
& IOPARM_DT_HAS_IOLENGTH
) != 0)
3462 *dtp
->iolength
+= (GFC_IO_INT
) (size
* nelems
);
3466 /* Initialize the IOLENGTH data transfer. This function is in essence
3467 a very much simplified version of data_transfer_init(), because it
3468 doesn't have to deal with units at all. */
3471 iolength_transfer_init (st_parameter_dt
*dtp
)
3473 if ((dtp
->common
.flags
& IOPARM_DT_HAS_IOLENGTH
) != 0)
3476 memset (&dtp
->u
.p
, 0, sizeof (dtp
->u
.p
));
3478 /* Set up the subroutine that will handle the transfers. */
3480 dtp
->u
.p
.transfer
= iolength_transfer
;
3484 /* Library entry point for the IOLENGTH form of the INQUIRE
3485 statement. The IOLENGTH form requires no I/O to be performed, but
3486 it must still be a runtime library call so that we can determine
3487 the iolength for dynamic arrays and such. */
3489 extern void st_iolength (st_parameter_dt
*);
3490 export_proto(st_iolength
);
3493 st_iolength (st_parameter_dt
*dtp
)
3495 library_start (&dtp
->common
);
3496 iolength_transfer_init (dtp
);
3499 extern void st_iolength_done (st_parameter_dt
*);
3500 export_proto(st_iolength_done
);
3503 st_iolength_done (st_parameter_dt
*dtp
__attribute__((unused
)))
3510 /* The READ statement. */
3512 extern void st_read (st_parameter_dt
*);
3513 export_proto(st_read
);
3516 st_read (st_parameter_dt
*dtp
)
3518 library_start (&dtp
->common
);
3520 data_transfer_init (dtp
, 1);
3523 extern void st_read_done (st_parameter_dt
*);
3524 export_proto(st_read_done
);
3527 st_read_done (st_parameter_dt
*dtp
)
3529 finalize_transfer (dtp
);
3530 if (is_internal_unit (dtp
) || dtp
->u
.p
.format_not_saved
)
3531 free_format_data (dtp
->u
.p
.fmt
);
3533 if (dtp
->u
.p
.current_unit
!= NULL
)
3534 unlock_unit (dtp
->u
.p
.current_unit
);
3536 free_internal_unit (dtp
);
3541 extern void st_write (st_parameter_dt
*);
3542 export_proto(st_write
);
3545 st_write (st_parameter_dt
*dtp
)
3547 library_start (&dtp
->common
);
3548 data_transfer_init (dtp
, 0);
3551 extern void st_write_done (st_parameter_dt
*);
3552 export_proto(st_write_done
);
3555 st_write_done (st_parameter_dt
*dtp
)
3557 finalize_transfer (dtp
);
3559 /* Deal with endfile conditions associated with sequential files. */
3561 if (dtp
->u
.p
.current_unit
!= NULL
3562 && dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
3563 switch (dtp
->u
.p
.current_unit
->endfile
)
3565 case AT_ENDFILE
: /* Remain at the endfile record. */
3569 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
; /* Just at it now. */
3573 /* Get rid of whatever is after this record. */
3574 if (!is_internal_unit (dtp
))
3575 unit_truncate (dtp
->u
.p
.current_unit
,
3576 stell (dtp
->u
.p
.current_unit
->s
),
3578 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
3582 if (is_internal_unit (dtp
) || dtp
->u
.p
.format_not_saved
)
3583 free_format_data (dtp
->u
.p
.fmt
);
3585 if (dtp
->u
.p
.current_unit
!= NULL
)
3586 unlock_unit (dtp
->u
.p
.current_unit
);
3588 free_internal_unit (dtp
);
3594 /* F2003: This is a stub for the runtime portion of the WAIT statement. */
3596 st_wait (st_parameter_wait
*wtp
__attribute__((unused
)))
3601 /* Receives the scalar information for namelist objects and stores it
3602 in a linked list of namelist_info types. */
3604 extern void st_set_nml_var (st_parameter_dt
*dtp
, void *, char *,
3605 GFC_INTEGER_4
, gfc_charlen_type
, GFC_INTEGER_4
);
3606 export_proto(st_set_nml_var
);
3610 st_set_nml_var (st_parameter_dt
*dtp
, void * var_addr
, char * var_name
,
3611 GFC_INTEGER_4 len
, gfc_charlen_type string_length
,
3612 GFC_INTEGER_4 dtype
)
3614 namelist_info
*t1
= NULL
;
3616 size_t var_name_len
= strlen (var_name
);
3618 nml
= (namelist_info
*) get_mem (sizeof (namelist_info
));
3620 nml
->mem_pos
= var_addr
;
3622 nml
->var_name
= (char*) get_mem (var_name_len
+ 1);
3623 memcpy (nml
->var_name
, var_name
, var_name_len
);
3624 nml
->var_name
[var_name_len
] = '\0';
3626 nml
->len
= (int) len
;
3627 nml
->string_length
= (index_type
) string_length
;
3629 nml
->var_rank
= (int) (dtype
& GFC_DTYPE_RANK_MASK
);
3630 nml
->size
= (index_type
) (dtype
>> GFC_DTYPE_SIZE_SHIFT
);
3631 nml
->type
= (bt
) ((dtype
& GFC_DTYPE_TYPE_MASK
) >> GFC_DTYPE_TYPE_SHIFT
);
3633 if (nml
->var_rank
> 0)
3635 nml
->dim
= (descriptor_dimension
*)
3636 get_mem (nml
->var_rank
* sizeof (descriptor_dimension
));
3637 nml
->ls
= (array_loop_spec
*)
3638 get_mem (nml
->var_rank
* sizeof (array_loop_spec
));
3648 if ((dtp
->common
.flags
& IOPARM_DT_IONML_SET
) == 0)
3650 dtp
->common
.flags
|= IOPARM_DT_IONML_SET
;
3651 dtp
->u
.p
.ionml
= nml
;
3655 for (t1
= dtp
->u
.p
.ionml
; t1
->next
; t1
= t1
->next
);
3660 /* Store the dimensional information for the namelist object. */
3661 extern void st_set_nml_var_dim (st_parameter_dt
*, GFC_INTEGER_4
,
3662 index_type
, index_type
,
3664 export_proto(st_set_nml_var_dim
);
3667 st_set_nml_var_dim (st_parameter_dt
*dtp
, GFC_INTEGER_4 n_dim
,
3668 index_type stride
, index_type lbound
,
3671 namelist_info
* nml
;
3676 for (nml
= dtp
->u
.p
.ionml
; nml
->next
; nml
= nml
->next
);
3678 GFC_DIMENSION_SET(nml
->dim
[n
],lbound
,ubound
,stride
);
3681 /* Reverse memcpy - used for byte swapping. */
3683 void reverse_memcpy (void *dest
, const void *src
, size_t n
)
3689 s
= (char *) src
+ n
- 1;
3691 /* Write with ascending order - this is likely faster
3692 on modern architectures because of write combining. */
3698 /* Once upon a time, a poor innocent Fortran program was reading a
3699 file, when suddenly it hit the end-of-file (EOF). Unfortunately
3700 the OS doesn't tell whether we're at the EOF or whether we already
3701 went past it. Luckily our hero, libgfortran, keeps track of this.
3702 Call this function when you detect an EOF condition. See Section
3706 hit_eof (st_parameter_dt
* dtp
)
3708 dtp
->u
.p
.current_unit
->flags
.position
= POSITION_APPEND
;
3710 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
3711 switch (dtp
->u
.p
.current_unit
->endfile
)
3715 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
3716 if (!is_internal_unit (dtp
))
3718 dtp
->u
.p
.current_unit
->endfile
= AFTER_ENDFILE
;
3719 dtp
->u
.p
.current_unit
->current_record
= 0;
3722 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
3726 generate_error (&dtp
->common
, LIBERROR_ENDFILE
, NULL
);
3727 dtp
->u
.p
.current_unit
->current_record
= 0;
3732 /* Non-sequential files don't have an ENDFILE record, so we
3733 can't be at AFTER_ENDFILE. */
3734 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
3735 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
3736 dtp
->u
.p
.current_unit
->current_record
= 0;