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 95 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_real (st_parameter_dt
*, void *, int);
71 export_proto(transfer_real
);
73 extern void transfer_logical (st_parameter_dt
*, void *, int);
74 export_proto(transfer_logical
);
76 extern void transfer_character (st_parameter_dt
*, void *, int);
77 export_proto(transfer_character
);
79 extern void transfer_character_wide (st_parameter_dt
*, void *, int, int);
80 export_proto(transfer_character_wide
);
82 extern void transfer_complex (st_parameter_dt
*, void *, int);
83 export_proto(transfer_complex
);
85 extern void transfer_array (st_parameter_dt
*, gfc_array_char
*, int,
87 export_proto(transfer_array
);
89 static void us_read (st_parameter_dt
*, int);
90 static void us_write (st_parameter_dt
*, int);
91 static void next_record_r_unf (st_parameter_dt
*, int);
92 static void next_record_w_unf (st_parameter_dt
*, int);
94 static const st_option advance_opt
[] = {
101 static const st_option decimal_opt
[] = {
102 {"point", DECIMAL_POINT
},
103 {"comma", DECIMAL_COMMA
},
107 static const st_option round_opt
[] = {
109 {"down", ROUND_DOWN
},
110 {"zero", ROUND_ZERO
},
111 {"nearest", ROUND_NEAREST
},
112 {"compatible", ROUND_COMPATIBLE
},
113 {"processor_defined", ROUND_PROCDEFINED
},
118 static const st_option sign_opt
[] = {
120 {"suppress", SIGN_SS
},
121 {"processor_defined", SIGN_S
},
125 static const st_option blank_opt
[] = {
126 {"null", BLANK_NULL
},
127 {"zero", BLANK_ZERO
},
131 static const st_option delim_opt
[] = {
132 {"apostrophe", DELIM_APOSTROPHE
},
133 {"quote", DELIM_QUOTE
},
134 {"none", DELIM_NONE
},
138 static const st_option pad_opt
[] = {
145 { FORMATTED_SEQUENTIAL
, UNFORMATTED_SEQUENTIAL
,
146 FORMATTED_DIRECT
, UNFORMATTED_DIRECT
, FORMATTED_STREAM
, UNFORMATTED_STREAM
152 current_mode (st_parameter_dt
*dtp
)
156 m
= FORM_UNSPECIFIED
;
158 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
160 m
= dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
?
161 FORMATTED_DIRECT
: UNFORMATTED_DIRECT
;
163 else if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
165 m
= dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
?
166 FORMATTED_SEQUENTIAL
: UNFORMATTED_SEQUENTIAL
;
168 else if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_STREAM
)
170 m
= dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
?
171 FORMATTED_STREAM
: UNFORMATTED_STREAM
;
178 /* Mid level data transfer statements. */
180 /* When reading sequential formatted records we have a problem. We
181 don't know how long the line is until we read the trailing newline,
182 and we don't want to read too much. If we read too much, we might
183 have to do a physical seek backwards depending on how much data is
184 present, and devices like terminals aren't seekable and would cause
187 Given this, the solution is to read a byte at a time, stopping if
188 we hit the newline. For small allocations, we use a static buffer.
189 For larger allocations, we are forced to allocate memory on the
190 heap. Hopefully this won't happen very often. */
192 /* Read sequential file - internal unit */
195 read_sf_internal (st_parameter_dt
*dtp
, int * length
)
197 static char *empty_string
[0];
201 /* Zero size array gives internal unit len of 0. Nothing to read. */
202 if (dtp
->internal_unit_len
== 0
203 && dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
)
206 /* If we have seen an eor previously, return a length of 0. The
207 caller is responsible for correctly padding the input field. */
208 if (dtp
->u
.p
.sf_seen_eor
)
211 /* Just return something that isn't a NULL pointer, otherwise the
212 caller thinks an error occured. */
213 return (char*) empty_string
;
217 base
= mem_alloc_r (dtp
->u
.p
.current_unit
->s
, length
);
218 if (unlikely (lorig
> *length
))
224 dtp
->u
.p
.current_unit
->bytes_left
-= *length
;
226 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
227 dtp
->u
.p
.size_used
+= (GFC_IO_INT
) *length
;
233 /* Read sequential file - external unit */
236 read_sf (st_parameter_dt
*dtp
, int * length
)
238 static char *empty_string
[0];
240 int n
, lorig
, seen_comma
;
242 /* If we have seen an eor previously, return a length of 0. The
243 caller is responsible for correctly padding the input field. */
244 if (dtp
->u
.p
.sf_seen_eor
)
247 /* Just return something that isn't a NULL pointer, otherwise the
248 caller thinks an error occured. */
249 return (char*) empty_string
;
254 /* Read data into format buffer and scan through it. */
256 base
= p
= fbuf_read (dtp
->u
.p
.current_unit
, length
);
264 if (q
== '\n' || q
== '\r')
266 /* Unexpected end of line. Set the position. */
267 fbuf_seek (dtp
->u
.p
.current_unit
, n
+ 1 ,SEEK_CUR
);
268 dtp
->u
.p
.sf_seen_eor
= 1;
270 /* If we see an EOR during non-advancing I/O, we need to skip
271 the rest of the I/O statement. Set the corresponding flag. */
272 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
|| dtp
->u
.p
.seen_dollar
)
273 dtp
->u
.p
.eor_condition
= 1;
275 /* If we encounter a CR, it might be a CRLF. */
276 if (q
== '\r') /* Probably a CRLF */
278 /* See if there is an LF. Use fbuf_read rather then fbuf_getc so
279 the position is not advanced unless it really is an LF. */
281 p
= fbuf_read (dtp
->u
.p
.current_unit
, &readlen
);
282 if (*p
== '\n' && readlen
== 1)
284 dtp
->u
.p
.sf_seen_eor
= 2;
285 fbuf_seek (dtp
->u
.p
.current_unit
, 1 ,SEEK_CUR
);
289 /* Without padding, terminate the I/O statement without assigning
290 the value. With padding, the value still needs to be assigned,
291 so we can just continue with a short read. */
292 if (dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
)
294 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
301 /* Short circuit the read if a comma is found during numeric input.
302 The flag is set to zero during character reads so that commas in
303 strings are not ignored */
305 if (dtp
->u
.p
.sf_read_comma
== 1)
308 notify_std (&dtp
->common
, GFC_STD_GNU
,
309 "Comma in formatted numeric read.");
317 fbuf_seek (dtp
->u
.p
.current_unit
, n
+ seen_comma
, SEEK_CUR
);
319 /* A short read implies we hit EOF, unless we hit EOR, a comma, or
320 some other stuff. Set the relevant flags. */
321 if (lorig
> *length
&& !dtp
->u
.p
.sf_seen_eor
&& !seen_comma
)
325 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
)
327 if (dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
)
333 dtp
->u
.p
.eor_condition
= 1;
338 else if (dtp
->u
.p
.advance_status
== ADVANCE_NO
339 || dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
340 || dtp
->u
.p
.current_unit
->bytes_left
341 == dtp
->u
.p
.current_unit
->recl
)
350 dtp
->u
.p
.current_unit
->bytes_left
-= n
;
352 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
353 dtp
->u
.p
.size_used
+= (GFC_IO_INT
) n
;
359 /* Function for reading the next couple of bytes from the current
360 file, advancing the current position. We return FAILURE on end of record or
361 end of file. This function is only for formatted I/O, unformatted uses
364 If the read is short, then it is because the current record does not
365 have enough data to satisfy the read request and the file was
366 opened with PAD=YES. The caller must assume tailing spaces for
370 read_block_form (st_parameter_dt
*dtp
, int * nbytes
)
375 if (!is_stream_io (dtp
))
377 if (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) *nbytes
)
379 /* For preconnected units with default record length, set bytes left
380 to unit record length and proceed, otherwise error. */
381 if (dtp
->u
.p
.current_unit
->unit_number
== options
.stdin_unit
382 && dtp
->u
.p
.current_unit
->recl
== DEFAULT_RECL
)
383 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
386 if (unlikely (dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
)
387 && !is_internal_unit (dtp
))
389 /* Not enough data left. */
390 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
395 if (unlikely (dtp
->u
.p
.current_unit
->bytes_left
== 0
396 && !is_internal_unit(dtp
)))
402 *nbytes
= dtp
->u
.p
.current_unit
->bytes_left
;
406 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
&&
407 (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
||
408 dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_STREAM
))
410 if (is_internal_unit (dtp
))
411 source
= read_sf_internal (dtp
, nbytes
);
413 source
= read_sf (dtp
, nbytes
);
415 dtp
->u
.p
.current_unit
->strm_pos
+=
416 (gfc_offset
) (*nbytes
+ dtp
->u
.p
.sf_seen_eor
);
420 /* If we reach here, we can assume it's direct access. */
422 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) *nbytes
;
425 source
= fbuf_read (dtp
->u
.p
.current_unit
, nbytes
);
426 fbuf_seek (dtp
->u
.p
.current_unit
, *nbytes
, SEEK_CUR
);
428 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
429 dtp
->u
.p
.size_used
+= (GFC_IO_INT
) *nbytes
;
431 if (norig
!= *nbytes
)
433 /* Short read, this shouldn't happen. */
434 if (!dtp
->u
.p
.current_unit
->pad_status
== PAD_YES
)
436 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
441 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) *nbytes
;
447 /* Reads a block directly into application data space. This is for
448 unformatted files. */
451 read_block_direct (st_parameter_dt
*dtp
, void *buf
, size_t nbytes
)
453 ssize_t to_read_record
;
454 ssize_t have_read_record
;
455 ssize_t to_read_subrecord
;
456 ssize_t have_read_subrecord
;
459 if (is_stream_io (dtp
))
461 have_read_record
= sread (dtp
->u
.p
.current_unit
->s
, buf
,
463 if (unlikely (have_read_record
< 0))
465 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
469 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) have_read_record
;
471 if (unlikely ((ssize_t
) nbytes
!= have_read_record
))
473 /* Short read, e.g. if we hit EOF. For stream files,
474 we have to set the end-of-file condition. */
480 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
482 if (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) nbytes
)
485 to_read_record
= dtp
->u
.p
.current_unit
->bytes_left
;
486 nbytes
= to_read_record
;
491 to_read_record
= nbytes
;
494 dtp
->u
.p
.current_unit
->bytes_left
-= to_read_record
;
496 to_read_record
= sread (dtp
->u
.p
.current_unit
->s
, buf
, to_read_record
);
497 if (unlikely (to_read_record
< 0))
499 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
503 if (to_read_record
!= (ssize_t
) nbytes
)
505 /* Short read, e.g. if we hit EOF. Apparently, we read
506 more than was written to the last record. */
510 if (unlikely (short_record
))
512 generate_error (&dtp
->common
, LIBERROR_SHORT_RECORD
, NULL
);
517 /* Unformatted sequential. We loop over the subrecords, reading
518 until the request has been fulfilled or the record has run out
519 of continuation subrecords. */
521 /* Check whether we exceed the total record length. */
523 if (dtp
->u
.p
.current_unit
->flags
.has_recl
524 && ((gfc_offset
) nbytes
> dtp
->u
.p
.current_unit
->bytes_left
))
526 to_read_record
= dtp
->u
.p
.current_unit
->bytes_left
;
531 to_read_record
= nbytes
;
534 have_read_record
= 0;
538 if (dtp
->u
.p
.current_unit
->bytes_left_subrecord
539 < (gfc_offset
) to_read_record
)
541 to_read_subrecord
= dtp
->u
.p
.current_unit
->bytes_left_subrecord
;
542 to_read_record
-= to_read_subrecord
;
546 to_read_subrecord
= to_read_record
;
550 dtp
->u
.p
.current_unit
->bytes_left_subrecord
-= to_read_subrecord
;
552 have_read_subrecord
= sread (dtp
->u
.p
.current_unit
->s
,
553 buf
+ have_read_record
, to_read_subrecord
);
554 if (unlikely (have_read_subrecord
) < 0)
556 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
560 have_read_record
+= have_read_subrecord
;
562 if (unlikely (to_read_subrecord
!= have_read_subrecord
))
565 /* Short read, e.g. if we hit EOF. This means the record
566 structure has been corrupted, or the trailing record
567 marker would still be present. */
569 generate_error (&dtp
->common
, LIBERROR_CORRUPT_FILE
, NULL
);
573 if (to_read_record
> 0)
575 if (likely (dtp
->u
.p
.current_unit
->continued
))
577 next_record_r_unf (dtp
, 0);
582 /* Let's make sure the file position is correctly pre-positioned
583 for the next read statement. */
585 dtp
->u
.p
.current_unit
->current_record
= 0;
586 next_record_r_unf (dtp
, 0);
587 generate_error (&dtp
->common
, LIBERROR_SHORT_RECORD
, NULL
);
593 /* Normal exit, the read request has been fulfilled. */
598 dtp
->u
.p
.current_unit
->bytes_left
-= have_read_record
;
599 if (unlikely (short_record
))
601 generate_error (&dtp
->common
, LIBERROR_SHORT_RECORD
, NULL
);
608 /* Function for writing a block of bytes to the current file at the
609 current position, advancing the file pointer. We are given a length
610 and return a pointer to a buffer that the caller must (completely)
611 fill in. Returns NULL on error. */
614 write_block (st_parameter_dt
*dtp
, int length
)
618 if (!is_stream_io (dtp
))
620 if (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) length
)
622 /* For preconnected units with default record length, set bytes left
623 to unit record length and proceed, otherwise error. */
624 if (likely ((dtp
->u
.p
.current_unit
->unit_number
625 == options
.stdout_unit
626 || dtp
->u
.p
.current_unit
->unit_number
627 == options
.stderr_unit
)
628 && dtp
->u
.p
.current_unit
->recl
== DEFAULT_RECL
))
629 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
632 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
637 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) length
;
640 if (is_internal_unit (dtp
))
642 dest
= mem_alloc_w (dtp
->u
.p
.current_unit
->s
, &length
);
646 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
650 if (unlikely (dtp
->u
.p
.current_unit
->endfile
== AT_ENDFILE
))
651 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
655 dest
= fbuf_alloc (dtp
->u
.p
.current_unit
, length
);
658 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
663 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
664 dtp
->u
.p
.size_used
+= (GFC_IO_INT
) length
;
666 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) length
;
672 /* High level interface to swrite(), taking care of errors. This is only
673 called for unformatted files. There are three cases to consider:
674 Stream I/O, unformatted direct, unformatted sequential. */
677 write_buf (st_parameter_dt
*dtp
, void *buf
, size_t nbytes
)
680 ssize_t have_written
;
681 ssize_t to_write_subrecord
;
686 if (is_stream_io (dtp
))
688 have_written
= swrite (dtp
->u
.p
.current_unit
->s
, buf
, nbytes
);
689 if (unlikely (have_written
< 0))
691 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
695 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) have_written
;
700 /* Unformatted direct access. */
702 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
704 if (unlikely (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) nbytes
))
706 generate_error (&dtp
->common
, LIBERROR_DIRECT_EOR
, NULL
);
710 if (buf
== NULL
&& nbytes
== 0)
713 have_written
= swrite (dtp
->u
.p
.current_unit
->s
, buf
, nbytes
);
714 if (unlikely (have_written
< 0))
716 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
720 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) have_written
;
721 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) have_written
;
726 /* Unformatted sequential. */
730 if (dtp
->u
.p
.current_unit
->flags
.has_recl
731 && (gfc_offset
) nbytes
> dtp
->u
.p
.current_unit
->bytes_left
)
733 nbytes
= dtp
->u
.p
.current_unit
->bytes_left
;
745 (size_t) dtp
->u
.p
.current_unit
->bytes_left_subrecord
< nbytes
?
746 (size_t) dtp
->u
.p
.current_unit
->bytes_left_subrecord
: nbytes
;
748 dtp
->u
.p
.current_unit
->bytes_left_subrecord
-=
749 (gfc_offset
) to_write_subrecord
;
751 to_write_subrecord
= swrite (dtp
->u
.p
.current_unit
->s
,
752 buf
+ have_written
, to_write_subrecord
);
753 if (unlikely (to_write_subrecord
< 0))
755 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
759 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) to_write_subrecord
;
760 nbytes
-= to_write_subrecord
;
761 have_written
+= to_write_subrecord
;
766 next_record_w_unf (dtp
, 1);
769 dtp
->u
.p
.current_unit
->bytes_left
-= have_written
;
770 if (unlikely (short_record
))
772 generate_error (&dtp
->common
, LIBERROR_SHORT_RECORD
, NULL
);
779 /* Master function for unformatted reads. */
782 unformatted_read (st_parameter_dt
*dtp
, bt type
,
783 void *dest
, int kind
, size_t size
, size_t nelems
)
785 if (likely (dtp
->u
.p
.current_unit
->flags
.convert
== GFC_CONVERT_NATIVE
)
788 if (type
== BT_CHARACTER
)
789 size
*= GFC_SIZE_OF_CHAR_KIND(kind
);
790 read_block_direct (dtp
, dest
, size
* nelems
);
800 /* Handle wide chracters. */
801 if (type
== BT_CHARACTER
&& kind
!= 1)
807 /* Break up complex into its constituent reals. */
808 if (type
== BT_COMPLEX
)
814 /* By now, all complex variables have been split into their
815 constituent reals. */
817 for (i
= 0; i
< nelems
; i
++)
819 read_block_direct (dtp
, buffer
, size
);
820 reverse_memcpy (p
, buffer
, size
);
827 /* Master function for unformatted writes. NOTE: For kind=10 the size is 16
828 bytes on 64 bit machines. The unused bytes are not initialized and never
829 used, which can show an error with memory checking analyzers like
833 unformatted_write (st_parameter_dt
*dtp
, bt type
,
834 void *source
, int kind
, size_t size
, size_t nelems
)
836 if (likely (dtp
->u
.p
.current_unit
->flags
.convert
== GFC_CONVERT_NATIVE
)
839 size_t stride
= type
== BT_CHARACTER
?
840 size
* GFC_SIZE_OF_CHAR_KIND(kind
) : size
;
842 write_buf (dtp
, source
, stride
* nelems
);
852 /* Handle wide chracters. */
853 if (type
== BT_CHARACTER
&& kind
!= 1)
859 /* Break up complex into its constituent reals. */
860 if (type
== BT_COMPLEX
)
866 /* By now, all complex variables have been split into their
867 constituent reals. */
869 for (i
= 0; i
< nelems
; i
++)
871 reverse_memcpy(buffer
, p
, size
);
873 write_buf (dtp
, buffer
, size
);
879 /* Return a pointer to the name of a type. */
904 internal_error (NULL
, "type_name(): Bad type");
911 /* Write a constant string to the output.
912 This is complicated because the string can have doubled delimiters
913 in it. The length in the format node is the true length. */
916 write_constant_string (st_parameter_dt
*dtp
, const fnode
*f
)
918 char c
, delimiter
, *p
, *q
;
921 length
= f
->u
.string
.length
;
925 p
= write_block (dtp
, length
);
932 for (; length
> 0; length
--)
935 if (c
== delimiter
&& c
!= 'H' && c
!= 'h')
936 q
++; /* Skip the doubled delimiter. */
941 /* Given actual and expected types in a formatted data transfer, make
942 sure they agree. If not, an error message is generated. Returns
943 nonzero if something went wrong. */
946 require_type (st_parameter_dt
*dtp
, bt expected
, bt actual
, const fnode
*f
)
950 if (actual
== expected
)
953 /* Adjust item_count before emitting error message. */
954 sprintf (buffer
, "Expected %s for item %d in formatted transfer, got %s",
955 type_name (expected
), dtp
->u
.p
.item_count
- 1, type_name (actual
));
957 format_error (dtp
, f
, buffer
);
962 /* This function is in the main loop for a formatted data transfer
963 statement. It would be natural to implement this as a coroutine
964 with the user program, but C makes that awkward. We loop,
965 processing format elements. When we actually have to transfer
966 data instead of just setting flags, we return control to the user
967 program which calls a function that supplies the address and type
968 of the next element, then comes back here to process it. */
971 formatted_transfer_scalar_read (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
978 int consume_data_flag
;
980 /* Change a complex data item into a pair of reals. */
982 n
= (p
== NULL
) ? 0 : ((type
!= BT_COMPLEX
) ? 1 : 2);
983 if (type
== BT_COMPLEX
)
989 /* If there's an EOR condition, we simulate finalizing the transfer
991 if (dtp
->u
.p
.eor_condition
)
994 /* Set this flag so that commas in reads cause the read to complete before
995 the entire field has been read. The next read field will start right after
996 the comma in the stream. (Set to 0 for character reads). */
997 dtp
->u
.p
.sf_read_comma
=
998 dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
? 0 : 1;
1002 /* If reversion has occurred and there is another real data item,
1003 then we have to move to the next record. */
1004 if (dtp
->u
.p
.reversion_flag
&& n
> 0)
1006 dtp
->u
.p
.reversion_flag
= 0;
1007 next_record (dtp
, 0);
1010 consume_data_flag
= 1;
1011 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1014 f
= next_format (dtp
);
1017 /* No data descriptors left. */
1018 if (unlikely (n
> 0))
1019 generate_error (&dtp
->common
, LIBERROR_FORMAT
,
1020 "Insufficient data descriptors in format after reversion");
1026 bytes_used
= (int)(dtp
->u
.p
.current_unit
->recl
1027 - dtp
->u
.p
.current_unit
->bytes_left
);
1029 if (is_stream_io(dtp
))
1036 goto need_read_data
;
1037 if (require_type (dtp
, BT_INTEGER
, type
, f
))
1039 read_decimal (dtp
, f
, p
, kind
);
1044 goto need_read_data
;
1045 if (compile_options
.allow_std
< GFC_STD_GNU
1046 && require_type (dtp
, BT_INTEGER
, type
, f
))
1048 read_radix (dtp
, f
, p
, kind
, 2);
1053 goto need_read_data
;
1054 if (compile_options
.allow_std
< GFC_STD_GNU
1055 && require_type (dtp
, BT_INTEGER
, type
, f
))
1057 read_radix (dtp
, f
, p
, kind
, 8);
1062 goto need_read_data
;
1063 if (compile_options
.allow_std
< GFC_STD_GNU
1064 && require_type (dtp
, BT_INTEGER
, type
, f
))
1066 read_radix (dtp
, f
, p
, kind
, 16);
1071 goto need_read_data
;
1073 /* It is possible to have FMT_A with something not BT_CHARACTER such
1074 as when writing out hollerith strings, so check both type
1075 and kind before calling wide character routines. */
1076 if (type
== BT_CHARACTER
&& kind
== 4)
1077 read_a_char4 (dtp
, f
, p
, size
);
1079 read_a (dtp
, f
, p
, size
);
1084 goto need_read_data
;
1085 read_l (dtp
, f
, p
, kind
);
1090 goto need_read_data
;
1091 if (require_type (dtp
, BT_REAL
, type
, f
))
1093 read_f (dtp
, f
, p
, kind
);
1098 goto need_read_data
;
1099 if (require_type (dtp
, BT_REAL
, type
, f
))
1101 read_f (dtp
, f
, p
, kind
);
1106 goto need_read_data
;
1107 if (require_type (dtp
, BT_REAL
, type
, f
))
1109 read_f (dtp
, f
, p
, kind
);
1114 goto need_read_data
;
1115 if (require_type (dtp
, BT_REAL
, type
, f
))
1117 read_f (dtp
, f
, p
, kind
);
1122 goto need_read_data
;
1123 if (require_type (dtp
, BT_REAL
, type
, f
))
1125 read_f (dtp
, f
, p
, kind
);
1130 goto need_read_data
;
1134 read_decimal (dtp
, f
, p
, kind
);
1137 read_l (dtp
, f
, p
, kind
);
1141 read_a_char4 (dtp
, f
, p
, size
);
1143 read_a (dtp
, f
, p
, size
);
1146 read_f (dtp
, f
, p
, kind
);
1149 internal_error (&dtp
->common
, "formatted_transfer(): Bad type");
1154 consume_data_flag
= 0;
1155 format_error (dtp
, f
, "Constant string in input format");
1158 /* Format codes that don't transfer data. */
1161 consume_data_flag
= 0;
1162 dtp
->u
.p
.skips
+= f
->u
.n
;
1163 pos
= bytes_used
+ dtp
->u
.p
.skips
- 1;
1164 dtp
->u
.p
.pending_spaces
= pos
- dtp
->u
.p
.max_pos
+ 1;
1165 read_x (dtp
, f
->u
.n
);
1170 consume_data_flag
= 0;
1172 if (f
->format
== FMT_TL
)
1174 /* Handle the special case when no bytes have been used yet.
1175 Cannot go below zero. */
1176 if (bytes_used
== 0)
1178 dtp
->u
.p
.pending_spaces
-= f
->u
.n
;
1179 dtp
->u
.p
.skips
-= f
->u
.n
;
1180 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
< 0 ? 0 : dtp
->u
.p
.skips
;
1183 pos
= bytes_used
- f
->u
.n
;
1188 /* Standard 10.6.1.1: excessive left tabbing is reset to the
1189 left tab limit. We do not check if the position has gone
1190 beyond the end of record because a subsequent tab could
1191 bring us back again. */
1192 pos
= pos
< 0 ? 0 : pos
;
1194 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
+ pos
- bytes_used
;
1195 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
1196 + pos
- dtp
->u
.p
.max_pos
;
1197 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
< 0
1198 ? 0 : dtp
->u
.p
.pending_spaces
;
1199 if (dtp
->u
.p
.skips
== 0)
1202 /* Adjust everything for end-of-record condition */
1203 if (dtp
->u
.p
.sf_seen_eor
&& !is_internal_unit (dtp
))
1205 dtp
->u
.p
.current_unit
->bytes_left
-= dtp
->u
.p
.sf_seen_eor
;
1206 dtp
->u
.p
.skips
-= dtp
->u
.p
.sf_seen_eor
;
1208 dtp
->u
.p
.sf_seen_eor
= 0;
1210 if (dtp
->u
.p
.skips
< 0)
1212 if (is_internal_unit (dtp
))
1213 sseek (dtp
->u
.p
.current_unit
->s
, dtp
->u
.p
.skips
, SEEK_CUR
);
1215 fbuf_seek (dtp
->u
.p
.current_unit
, dtp
->u
.p
.skips
, SEEK_CUR
);
1216 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) dtp
->u
.p
.skips
;
1217 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1220 read_x (dtp
, dtp
->u
.p
.skips
);
1224 consume_data_flag
= 0;
1225 dtp
->u
.p
.sign_status
= SIGN_S
;
1229 consume_data_flag
= 0;
1230 dtp
->u
.p
.sign_status
= SIGN_SS
;
1234 consume_data_flag
= 0;
1235 dtp
->u
.p
.sign_status
= SIGN_SP
;
1239 consume_data_flag
= 0 ;
1240 dtp
->u
.p
.blank_status
= BLANK_NULL
;
1244 consume_data_flag
= 0;
1245 dtp
->u
.p
.blank_status
= BLANK_ZERO
;
1249 consume_data_flag
= 0;
1250 dtp
->u
.p
.current_unit
->decimal_status
= DECIMAL_COMMA
;
1254 consume_data_flag
= 0;
1255 dtp
->u
.p
.current_unit
->decimal_status
= DECIMAL_POINT
;
1259 consume_data_flag
= 0;
1260 dtp
->u
.p
.current_unit
->round_status
= ROUND_COMPATIBLE
;
1264 consume_data_flag
= 0;
1265 dtp
->u
.p
.current_unit
->round_status
= ROUND_DOWN
;
1269 consume_data_flag
= 0;
1270 dtp
->u
.p
.current_unit
->round_status
= ROUND_NEAREST
;
1274 consume_data_flag
= 0;
1275 dtp
->u
.p
.current_unit
->round_status
= ROUND_PROCDEFINED
;
1279 consume_data_flag
= 0;
1280 dtp
->u
.p
.current_unit
->round_status
= ROUND_UP
;
1284 consume_data_flag
= 0;
1285 dtp
->u
.p
.current_unit
->round_status
= ROUND_ZERO
;
1289 consume_data_flag
= 0;
1290 dtp
->u
.p
.scale_factor
= f
->u
.k
;
1294 consume_data_flag
= 0;
1295 dtp
->u
.p
.seen_dollar
= 1;
1299 consume_data_flag
= 0;
1300 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1301 next_record (dtp
, 0);
1305 /* A colon descriptor causes us to exit this loop (in
1306 particular preventing another / descriptor from being
1307 processed) unless there is another data item to be
1309 consume_data_flag
= 0;
1315 internal_error (&dtp
->common
, "Bad format node");
1318 /* Adjust the item count and data pointer. */
1320 if ((consume_data_flag
> 0) && (n
> 0))
1323 p
= ((char *) p
) + size
;
1328 pos
= (int)(dtp
->u
.p
.current_unit
->recl
- dtp
->u
.p
.current_unit
->bytes_left
);
1329 dtp
->u
.p
.max_pos
= (dtp
->u
.p
.max_pos
> pos
) ? dtp
->u
.p
.max_pos
: pos
;
1334 /* Come here when we need a data descriptor but don't have one. We
1335 push the current format node back onto the input, then return and
1336 let the user program call us back with the data. */
1338 unget_format (dtp
, f
);
1343 formatted_transfer_scalar_write (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
1346 int pos
, bytes_used
;
1350 int consume_data_flag
;
1352 /* Change a complex data item into a pair of reals. */
1354 n
= (p
== NULL
) ? 0 : ((type
!= BT_COMPLEX
) ? 1 : 2);
1355 if (type
== BT_COMPLEX
)
1361 /* If there's an EOR condition, we simulate finalizing the transfer
1362 by doing nothing. */
1363 if (dtp
->u
.p
.eor_condition
)
1366 /* Set this flag so that commas in reads cause the read to complete before
1367 the entire field has been read. The next read field will start right after
1368 the comma in the stream. (Set to 0 for character reads). */
1369 dtp
->u
.p
.sf_read_comma
=
1370 dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
? 0 : 1;
1374 /* If reversion has occurred and there is another real data item,
1375 then we have to move to the next record. */
1376 if (dtp
->u
.p
.reversion_flag
&& n
> 0)
1378 dtp
->u
.p
.reversion_flag
= 0;
1379 next_record (dtp
, 0);
1382 consume_data_flag
= 1;
1383 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1386 f
= next_format (dtp
);
1389 /* No data descriptors left. */
1390 if (unlikely (n
> 0))
1391 generate_error (&dtp
->common
, LIBERROR_FORMAT
,
1392 "Insufficient data descriptors in format after reversion");
1396 /* Now discharge T, TR and X movements to the right. This is delayed
1397 until a data producing format to suppress trailing spaces. */
1400 if (dtp
->u
.p
.mode
== WRITING
&& dtp
->u
.p
.skips
!= 0
1401 && ((n
>0 && ( t
== FMT_I
|| t
== FMT_B
|| t
== FMT_O
1402 || t
== FMT_Z
|| t
== FMT_F
|| t
== FMT_E
1403 || t
== FMT_EN
|| t
== FMT_ES
|| t
== FMT_G
1404 || t
== FMT_L
|| t
== FMT_A
|| t
== FMT_D
))
1405 || t
== FMT_STRING
))
1407 if (dtp
->u
.p
.skips
> 0)
1410 write_x (dtp
, dtp
->u
.p
.skips
, dtp
->u
.p
.pending_spaces
);
1411 tmp
= (int)(dtp
->u
.p
.current_unit
->recl
1412 - dtp
->u
.p
.current_unit
->bytes_left
);
1414 dtp
->u
.p
.max_pos
> tmp
? dtp
->u
.p
.max_pos
: tmp
;
1416 if (dtp
->u
.p
.skips
< 0)
1418 if (is_internal_unit (dtp
))
1419 sseek (dtp
->u
.p
.current_unit
->s
, dtp
->u
.p
.skips
, SEEK_CUR
);
1421 fbuf_seek (dtp
->u
.p
.current_unit
, dtp
->u
.p
.skips
, SEEK_CUR
);
1422 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) dtp
->u
.p
.skips
;
1424 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1427 bytes_used
= (int)(dtp
->u
.p
.current_unit
->recl
1428 - dtp
->u
.p
.current_unit
->bytes_left
);
1430 if (is_stream_io(dtp
))
1438 if (require_type (dtp
, BT_INTEGER
, type
, f
))
1440 write_i (dtp
, f
, p
, kind
);
1446 if (compile_options
.allow_std
< GFC_STD_GNU
1447 && require_type (dtp
, BT_INTEGER
, type
, f
))
1449 write_b (dtp
, f
, p
, kind
);
1455 if (compile_options
.allow_std
< GFC_STD_GNU
1456 && require_type (dtp
, BT_INTEGER
, type
, f
))
1458 write_o (dtp
, f
, p
, kind
);
1464 if (compile_options
.allow_std
< GFC_STD_GNU
1465 && require_type (dtp
, BT_INTEGER
, type
, f
))
1467 write_z (dtp
, f
, p
, kind
);
1474 /* It is possible to have FMT_A with something not BT_CHARACTER such
1475 as when writing out hollerith strings, so check both type
1476 and kind before calling wide character routines. */
1477 if (type
== BT_CHARACTER
&& kind
== 4)
1478 write_a_char4 (dtp
, f
, p
, size
);
1480 write_a (dtp
, f
, p
, size
);
1486 write_l (dtp
, f
, p
, kind
);
1492 if (require_type (dtp
, BT_REAL
, type
, f
))
1494 write_d (dtp
, f
, p
, kind
);
1500 if (require_type (dtp
, BT_REAL
, type
, f
))
1502 write_e (dtp
, f
, p
, kind
);
1508 if (require_type (dtp
, BT_REAL
, type
, f
))
1510 write_en (dtp
, f
, p
, kind
);
1516 if (require_type (dtp
, BT_REAL
, type
, f
))
1518 write_es (dtp
, f
, p
, kind
);
1524 if (require_type (dtp
, BT_REAL
, type
, f
))
1526 write_f (dtp
, f
, p
, kind
);
1535 write_i (dtp
, f
, p
, kind
);
1538 write_l (dtp
, f
, p
, kind
);
1542 write_a_char4 (dtp
, f
, p
, size
);
1544 write_a (dtp
, f
, p
, size
);
1547 if (f
->u
.real
.w
== 0)
1548 write_real_g0 (dtp
, p
, kind
, f
->u
.real
.d
);
1550 write_d (dtp
, f
, p
, kind
);
1553 internal_error (&dtp
->common
,
1554 "formatted_transfer(): Bad type");
1559 consume_data_flag
= 0;
1560 write_constant_string (dtp
, f
);
1563 /* Format codes that don't transfer data. */
1566 consume_data_flag
= 0;
1568 dtp
->u
.p
.skips
+= f
->u
.n
;
1569 pos
= bytes_used
+ dtp
->u
.p
.skips
- 1;
1570 dtp
->u
.p
.pending_spaces
= pos
- dtp
->u
.p
.max_pos
+ 1;
1571 /* Writes occur just before the switch on f->format, above, so
1572 that trailing blanks are suppressed, unless we are doing a
1573 non-advancing write in which case we want to output the blanks
1575 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
)
1577 write_x (dtp
, dtp
->u
.p
.skips
, dtp
->u
.p
.pending_spaces
);
1578 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1584 consume_data_flag
= 0;
1586 if (f
->format
== FMT_TL
)
1589 /* Handle the special case when no bytes have been used yet.
1590 Cannot go below zero. */
1591 if (bytes_used
== 0)
1593 dtp
->u
.p
.pending_spaces
-= f
->u
.n
;
1594 dtp
->u
.p
.skips
-= f
->u
.n
;
1595 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
< 0 ? 0 : dtp
->u
.p
.skips
;
1598 pos
= bytes_used
- f
->u
.n
;
1601 pos
= f
->u
.n
- dtp
->u
.p
.pending_spaces
- 1;
1603 /* Standard 10.6.1.1: excessive left tabbing is reset to the
1604 left tab limit. We do not check if the position has gone
1605 beyond the end of record because a subsequent tab could
1606 bring us back again. */
1607 pos
= pos
< 0 ? 0 : pos
;
1609 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
+ pos
- bytes_used
;
1610 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
1611 + pos
- dtp
->u
.p
.max_pos
;
1612 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
< 0
1613 ? 0 : dtp
->u
.p
.pending_spaces
;
1617 consume_data_flag
= 0;
1618 dtp
->u
.p
.sign_status
= SIGN_S
;
1622 consume_data_flag
= 0;
1623 dtp
->u
.p
.sign_status
= SIGN_SS
;
1627 consume_data_flag
= 0;
1628 dtp
->u
.p
.sign_status
= SIGN_SP
;
1632 consume_data_flag
= 0 ;
1633 dtp
->u
.p
.blank_status
= BLANK_NULL
;
1637 consume_data_flag
= 0;
1638 dtp
->u
.p
.blank_status
= BLANK_ZERO
;
1642 consume_data_flag
= 0;
1643 dtp
->u
.p
.current_unit
->decimal_status
= DECIMAL_COMMA
;
1647 consume_data_flag
= 0;
1648 dtp
->u
.p
.current_unit
->decimal_status
= DECIMAL_POINT
;
1652 consume_data_flag
= 0;
1653 dtp
->u
.p
.current_unit
->round_status
= ROUND_COMPATIBLE
;
1657 consume_data_flag
= 0;
1658 dtp
->u
.p
.current_unit
->round_status
= ROUND_DOWN
;
1662 consume_data_flag
= 0;
1663 dtp
->u
.p
.current_unit
->round_status
= ROUND_NEAREST
;
1667 consume_data_flag
= 0;
1668 dtp
->u
.p
.current_unit
->round_status
= ROUND_PROCDEFINED
;
1672 consume_data_flag
= 0;
1673 dtp
->u
.p
.current_unit
->round_status
= ROUND_UP
;
1677 consume_data_flag
= 0;
1678 dtp
->u
.p
.current_unit
->round_status
= ROUND_ZERO
;
1682 consume_data_flag
= 0;
1683 dtp
->u
.p
.scale_factor
= f
->u
.k
;
1687 consume_data_flag
= 0;
1688 dtp
->u
.p
.seen_dollar
= 1;
1692 consume_data_flag
= 0;
1693 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1694 next_record (dtp
, 0);
1698 /* A colon descriptor causes us to exit this loop (in
1699 particular preventing another / descriptor from being
1700 processed) unless there is another data item to be
1702 consume_data_flag
= 0;
1708 internal_error (&dtp
->common
, "Bad format node");
1711 /* Adjust the item count and data pointer. */
1713 if ((consume_data_flag
> 0) && (n
> 0))
1716 p
= ((char *) p
) + size
;
1719 pos
= (int)(dtp
->u
.p
.current_unit
->recl
- dtp
->u
.p
.current_unit
->bytes_left
);
1720 dtp
->u
.p
.max_pos
= (dtp
->u
.p
.max_pos
> pos
) ? dtp
->u
.p
.max_pos
: pos
;
1725 /* Come here when we need a data descriptor but don't have one. We
1726 push the current format node back onto the input, then return and
1727 let the user program call us back with the data. */
1729 unget_format (dtp
, f
);
1732 /* This function is first called from data_init_transfer to initiate the loop
1733 over each item in the format, transferring data as required. Subsequent
1734 calls to this function occur for each data item foound in the READ/WRITE
1735 statement. The item_count is incremented for each call. Since the first
1736 call is from data_transfer_init, the item_count is always one greater than
1737 the actual count number of the item being transferred. */
1740 formatted_transfer (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
1741 size_t size
, size_t nelems
)
1747 size_t stride
= type
== BT_CHARACTER
?
1748 size
* GFC_SIZE_OF_CHAR_KIND(kind
) : size
;
1749 if (dtp
->u
.p
.mode
== READING
)
1751 /* Big loop over all the elements. */
1752 for (elem
= 0; elem
< nelems
; elem
++)
1754 dtp
->u
.p
.item_count
++;
1755 formatted_transfer_scalar_read (dtp
, type
, tmp
+ stride
*elem
, kind
, size
);
1760 /* Big loop over all the elements. */
1761 for (elem
= 0; elem
< nelems
; elem
++)
1763 dtp
->u
.p
.item_count
++;
1764 formatted_transfer_scalar_write (dtp
, type
, tmp
+ stride
*elem
, kind
, size
);
1770 /* Data transfer entry points. The type of the data entity is
1771 implicit in the subroutine call. This prevents us from having to
1772 share a common enum with the compiler. */
1775 transfer_integer (st_parameter_dt
*dtp
, void *p
, int kind
)
1777 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1779 dtp
->u
.p
.transfer (dtp
, BT_INTEGER
, p
, kind
, kind
, 1);
1784 transfer_real (st_parameter_dt
*dtp
, void *p
, int kind
)
1787 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1789 size
= size_from_real_kind (kind
);
1790 dtp
->u
.p
.transfer (dtp
, BT_REAL
, p
, kind
, size
, 1);
1795 transfer_logical (st_parameter_dt
*dtp
, void *p
, int kind
)
1797 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1799 dtp
->u
.p
.transfer (dtp
, BT_LOGICAL
, p
, kind
, kind
, 1);
1804 transfer_character (st_parameter_dt
*dtp
, void *p
, int len
)
1806 static char *empty_string
[0];
1808 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1811 /* Strings of zero length can have p == NULL, which confuses the
1812 transfer routines into thinking we need more data elements. To avoid
1813 this, we give them a nice pointer. */
1814 if (len
== 0 && p
== NULL
)
1817 /* Set kind here to 1. */
1818 dtp
->u
.p
.transfer (dtp
, BT_CHARACTER
, p
, 1, len
, 1);
1822 transfer_character_wide (st_parameter_dt
*dtp
, void *p
, int len
, int kind
)
1824 static char *empty_string
[0];
1826 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1829 /* Strings of zero length can have p == NULL, which confuses the
1830 transfer routines into thinking we need more data elements. To avoid
1831 this, we give them a nice pointer. */
1832 if (len
== 0 && p
== NULL
)
1835 /* Here we pass the actual kind value. */
1836 dtp
->u
.p
.transfer (dtp
, BT_CHARACTER
, p
, kind
, len
, 1);
1841 transfer_complex (st_parameter_dt
*dtp
, void *p
, int kind
)
1844 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1846 size
= size_from_complex_kind (kind
);
1847 dtp
->u
.p
.transfer (dtp
, BT_COMPLEX
, p
, kind
, size
, 1);
1852 transfer_array (st_parameter_dt
*dtp
, gfc_array_char
*desc
, int kind
,
1853 gfc_charlen_type charlen
)
1855 index_type count
[GFC_MAX_DIMENSIONS
];
1856 index_type extent
[GFC_MAX_DIMENSIONS
];
1857 index_type stride
[GFC_MAX_DIMENSIONS
];
1858 index_type stride0
, rank
, size
, type
, n
;
1863 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1866 type
= GFC_DESCRIPTOR_TYPE (desc
);
1867 size
= GFC_DESCRIPTOR_SIZE (desc
);
1869 /* FIXME: What a kludge: Array descriptors and the IO library use
1870 different enums for types. */
1873 case GFC_DTYPE_UNKNOWN
:
1874 iotype
= BT_NULL
; /* Is this correct? */
1876 case GFC_DTYPE_INTEGER
:
1877 iotype
= BT_INTEGER
;
1879 case GFC_DTYPE_LOGICAL
:
1880 iotype
= BT_LOGICAL
;
1882 case GFC_DTYPE_REAL
:
1885 case GFC_DTYPE_COMPLEX
:
1886 iotype
= BT_COMPLEX
;
1888 case GFC_DTYPE_CHARACTER
:
1889 iotype
= BT_CHARACTER
;
1892 case GFC_DTYPE_DERIVED
:
1893 internal_error (&dtp
->common
,
1894 "Derived type I/O should have been handled via the frontend.");
1897 internal_error (&dtp
->common
, "transfer_array(): Bad type");
1900 rank
= GFC_DESCRIPTOR_RANK (desc
);
1901 for (n
= 0; n
< rank
; n
++)
1904 stride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(desc
,n
);
1905 extent
[n
] = GFC_DESCRIPTOR_EXTENT(desc
,n
);
1907 /* If the extent of even one dimension is zero, then the entire
1908 array section contains zero elements, so we return after writing
1909 a zero array record. */
1914 dtp
->u
.p
.transfer (dtp
, iotype
, data
, kind
, size
, tsize
);
1919 stride0
= stride
[0];
1921 /* If the innermost dimension has a stride of 1, we can do the transfer
1922 in contiguous chunks. */
1923 if (stride0
== size
)
1928 data
= GFC_DESCRIPTOR_DATA (desc
);
1932 dtp
->u
.p
.transfer (dtp
, iotype
, data
, kind
, size
, tsize
);
1933 data
+= stride0
* tsize
;
1936 while (count
[n
] == extent
[n
])
1939 data
-= stride
[n
] * extent
[n
];
1956 /* Preposition a sequential unformatted file while reading. */
1959 us_read (st_parameter_dt
*dtp
, int continued
)
1966 if (compile_options
.record_marker
== 0)
1967 n
= sizeof (GFC_INTEGER_4
);
1969 n
= compile_options
.record_marker
;
1971 nr
= sread (dtp
->u
.p
.current_unit
->s
, &i
, n
);
1972 if (unlikely (nr
< 0))
1974 generate_error (&dtp
->common
, LIBERROR_BAD_US
, NULL
);
1980 return; /* end of file */
1982 else if (unlikely (n
!= nr
))
1984 generate_error (&dtp
->common
, LIBERROR_BAD_US
, NULL
);
1988 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
1989 if (likely (dtp
->u
.p
.current_unit
->flags
.convert
== GFC_CONVERT_NATIVE
))
1993 case sizeof(GFC_INTEGER_4
):
1994 memcpy (&i4
, &i
, sizeof (i4
));
1998 case sizeof(GFC_INTEGER_8
):
1999 memcpy (&i8
, &i
, sizeof (i8
));
2004 runtime_error ("Illegal value for record marker");
2011 case sizeof(GFC_INTEGER_4
):
2012 reverse_memcpy (&i4
, &i
, sizeof (i4
));
2016 case sizeof(GFC_INTEGER_8
):
2017 reverse_memcpy (&i8
, &i
, sizeof (i8
));
2022 runtime_error ("Illegal value for record marker");
2028 dtp
->u
.p
.current_unit
->bytes_left_subrecord
= i
;
2029 dtp
->u
.p
.current_unit
->continued
= 0;
2033 dtp
->u
.p
.current_unit
->bytes_left_subrecord
= -i
;
2034 dtp
->u
.p
.current_unit
->continued
= 1;
2038 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2042 /* Preposition a sequential unformatted file while writing. This
2043 amount to writing a bogus length that will be filled in later. */
2046 us_write (st_parameter_dt
*dtp
, int continued
)
2053 if (compile_options
.record_marker
== 0)
2054 nbytes
= sizeof (GFC_INTEGER_4
);
2056 nbytes
= compile_options
.record_marker
;
2058 if (swrite (dtp
->u
.p
.current_unit
->s
, &dummy
, nbytes
) != nbytes
)
2059 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2061 /* For sequential unformatted, if RECL= was not specified in the OPEN
2062 we write until we have more bytes than can fit in the subrecord
2063 markers, then we write a new subrecord. */
2065 dtp
->u
.p
.current_unit
->bytes_left_subrecord
=
2066 dtp
->u
.p
.current_unit
->recl_subrecord
;
2067 dtp
->u
.p
.current_unit
->continued
= continued
;
2071 /* Position to the next record prior to transfer. We are assumed to
2072 be before the next record. We also calculate the bytes in the next
2076 pre_position (st_parameter_dt
*dtp
)
2078 if (dtp
->u
.p
.current_unit
->current_record
)
2079 return; /* Already positioned. */
2081 switch (current_mode (dtp
))
2083 case FORMATTED_STREAM
:
2084 case UNFORMATTED_STREAM
:
2085 /* There are no records with stream I/O. If the position was specified
2086 data_transfer_init has already positioned the file. If no position
2087 was specified, we continue from where we last left off. I.e.
2088 there is nothing to do here. */
2091 case UNFORMATTED_SEQUENTIAL
:
2092 if (dtp
->u
.p
.mode
== READING
)
2099 case FORMATTED_SEQUENTIAL
:
2100 case FORMATTED_DIRECT
:
2101 case UNFORMATTED_DIRECT
:
2102 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2106 dtp
->u
.p
.current_unit
->current_record
= 1;
2110 /* Initialize things for a data transfer. This code is common for
2111 both reading and writing. */
2114 data_transfer_init (st_parameter_dt
*dtp
, int read_flag
)
2116 unit_flags u_flags
; /* Used for creating a unit if needed. */
2117 GFC_INTEGER_4 cf
= dtp
->common
.flags
;
2118 namelist_info
*ionml
;
2120 ionml
= ((cf
& IOPARM_DT_IONML_SET
) != 0) ? dtp
->u
.p
.ionml
: NULL
;
2122 memset (&dtp
->u
.p
, 0, sizeof (dtp
->u
.p
));
2124 dtp
->u
.p
.ionml
= ionml
;
2125 dtp
->u
.p
.mode
= read_flag
? READING
: WRITING
;
2127 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
2130 if ((cf
& IOPARM_DT_HAS_SIZE
) != 0)
2131 dtp
->u
.p
.size_used
= 0; /* Initialize the count. */
2133 dtp
->u
.p
.current_unit
= get_unit (dtp
, 1);
2134 if (dtp
->u
.p
.current_unit
->s
== NULL
)
2135 { /* Open the unit with some default flags. */
2136 st_parameter_open opp
;
2139 if (dtp
->common
.unit
< 0)
2141 close_unit (dtp
->u
.p
.current_unit
);
2142 dtp
->u
.p
.current_unit
= NULL
;
2143 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2144 "Bad unit number in statement");
2147 memset (&u_flags
, '\0', sizeof (u_flags
));
2148 u_flags
.access
= ACCESS_SEQUENTIAL
;
2149 u_flags
.action
= ACTION_READWRITE
;
2151 /* Is it unformatted? */
2152 if (!(cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
2153 | IOPARM_DT_IONML_SET
)))
2154 u_flags
.form
= FORM_UNFORMATTED
;
2156 u_flags
.form
= FORM_UNSPECIFIED
;
2158 u_flags
.delim
= DELIM_UNSPECIFIED
;
2159 u_flags
.blank
= BLANK_UNSPECIFIED
;
2160 u_flags
.pad
= PAD_UNSPECIFIED
;
2161 u_flags
.decimal
= DECIMAL_UNSPECIFIED
;
2162 u_flags
.encoding
= ENCODING_UNSPECIFIED
;
2163 u_flags
.async
= ASYNC_UNSPECIFIED
;
2164 u_flags
.round
= ROUND_UNSPECIFIED
;
2165 u_flags
.sign
= SIGN_UNSPECIFIED
;
2167 u_flags
.status
= STATUS_UNKNOWN
;
2169 conv
= get_unformatted_convert (dtp
->common
.unit
);
2171 if (conv
== GFC_CONVERT_NONE
)
2172 conv
= compile_options
.convert
;
2174 /* We use big_endian, which is 0 on little-endian machines
2175 and 1 on big-endian machines. */
2178 case GFC_CONVERT_NATIVE
:
2179 case GFC_CONVERT_SWAP
:
2182 case GFC_CONVERT_BIG
:
2183 conv
= big_endian
? GFC_CONVERT_NATIVE
: GFC_CONVERT_SWAP
;
2186 case GFC_CONVERT_LITTLE
:
2187 conv
= big_endian
? GFC_CONVERT_SWAP
: GFC_CONVERT_NATIVE
;
2191 internal_error (&opp
.common
, "Illegal value for CONVERT");
2195 u_flags
.convert
= conv
;
2197 opp
.common
= dtp
->common
;
2198 opp
.common
.flags
&= IOPARM_COMMON_MASK
;
2199 dtp
->u
.p
.current_unit
= new_unit (&opp
, dtp
->u
.p
.current_unit
, &u_flags
);
2200 dtp
->common
.flags
&= ~IOPARM_COMMON_MASK
;
2201 dtp
->common
.flags
|= (opp
.common
.flags
& IOPARM_COMMON_MASK
);
2202 if (dtp
->u
.p
.current_unit
== NULL
)
2206 /* Check the action. */
2208 if (read_flag
&& dtp
->u
.p
.current_unit
->flags
.action
== ACTION_WRITE
)
2210 generate_error (&dtp
->common
, LIBERROR_BAD_ACTION
,
2211 "Cannot read from file opened for WRITE");
2215 if (!read_flag
&& dtp
->u
.p
.current_unit
->flags
.action
== ACTION_READ
)
2217 generate_error (&dtp
->common
, LIBERROR_BAD_ACTION
,
2218 "Cannot write to file opened for READ");
2222 dtp
->u
.p
.first_item
= 1;
2224 /* Check the format. */
2226 if ((cf
& IOPARM_DT_HAS_FORMAT
) != 0)
2229 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
2230 && (cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
))
2233 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2234 "Format present for UNFORMATTED data transfer");
2238 if ((cf
& IOPARM_DT_HAS_NAMELIST_NAME
) != 0 && dtp
->u
.p
.ionml
!= NULL
)
2240 if ((cf
& IOPARM_DT_HAS_FORMAT
) != 0)
2241 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2242 "A format cannot be specified with a namelist");
2244 else if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
&&
2245 !(cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
)))
2247 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2248 "Missing format for FORMATTED data transfer");
2251 if (is_internal_unit (dtp
)
2252 && dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
)
2254 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2255 "Internal file cannot be accessed by UNFORMATTED "
2260 /* Check the record or position number. */
2262 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
2263 && (cf
& IOPARM_DT_HAS_REC
) == 0)
2265 generate_error (&dtp
->common
, LIBERROR_MISSING_OPTION
,
2266 "Direct access data transfer requires record number");
2270 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
2272 if ((cf
& IOPARM_DT_HAS_REC
) != 0)
2274 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2275 "Record number not allowed for sequential access "
2280 if (dtp
->u
.p
.current_unit
->endfile
== AFTER_ENDFILE
)
2282 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2283 "Sequential READ or WRITE not allowed after "
2284 "EOF marker, possibly use REWIND or BACKSPACE");
2289 /* Process the ADVANCE option. */
2291 dtp
->u
.p
.advance_status
2292 = !(cf
& IOPARM_DT_HAS_ADVANCE
) ? ADVANCE_UNSPECIFIED
:
2293 find_option (&dtp
->common
, dtp
->advance
, dtp
->advance_len
, advance_opt
,
2294 "Bad ADVANCE parameter in data transfer statement");
2296 if (dtp
->u
.p
.advance_status
!= ADVANCE_UNSPECIFIED
)
2298 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
2300 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2301 "ADVANCE specification conflicts with sequential "
2306 if (is_internal_unit (dtp
))
2308 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2309 "ADVANCE specification conflicts with internal file");
2313 if ((cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
))
2314 != IOPARM_DT_HAS_FORMAT
)
2316 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2317 "ADVANCE specification requires an explicit format");
2324 dtp
->u
.p
.current_unit
->previous_nonadvancing_write
= 0;
2326 if ((cf
& IOPARM_EOR
) != 0 && dtp
->u
.p
.advance_status
!= ADVANCE_NO
)
2328 generate_error (&dtp
->common
, LIBERROR_MISSING_OPTION
,
2329 "EOR specification requires an ADVANCE specification "
2334 if ((cf
& IOPARM_DT_HAS_SIZE
) != 0
2335 && dtp
->u
.p
.advance_status
!= ADVANCE_NO
)
2337 generate_error (&dtp
->common
, LIBERROR_MISSING_OPTION
,
2338 "SIZE specification requires an ADVANCE "
2339 "specification of NO");
2344 { /* Write constraints. */
2345 if ((cf
& IOPARM_END
) != 0)
2347 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2348 "END specification cannot appear in a write "
2353 if ((cf
& IOPARM_EOR
) != 0)
2355 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2356 "EOR specification cannot appear in a write "
2361 if ((cf
& IOPARM_DT_HAS_SIZE
) != 0)
2363 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2364 "SIZE specification cannot appear in a write "
2370 if (dtp
->u
.p
.advance_status
== ADVANCE_UNSPECIFIED
)
2371 dtp
->u
.p
.advance_status
= ADVANCE_YES
;
2373 /* Check the decimal mode. */
2374 dtp
->u
.p
.current_unit
->decimal_status
2375 = !(cf
& IOPARM_DT_HAS_DECIMAL
) ? DECIMAL_UNSPECIFIED
:
2376 find_option (&dtp
->common
, dtp
->decimal
, dtp
->decimal_len
,
2377 decimal_opt
, "Bad DECIMAL parameter in data transfer "
2380 if (dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_UNSPECIFIED
)
2381 dtp
->u
.p
.current_unit
->decimal_status
= dtp
->u
.p
.current_unit
->flags
.decimal
;
2383 /* Check the round mode. */
2384 dtp
->u
.p
.current_unit
->round_status
2385 = !(cf
& IOPARM_DT_HAS_ROUND
) ? ROUND_UNSPECIFIED
:
2386 find_option (&dtp
->common
, dtp
->round
, dtp
->round_len
,
2387 round_opt
, "Bad ROUND parameter in data transfer "
2390 if (dtp
->u
.p
.current_unit
->round_status
== ROUND_UNSPECIFIED
)
2391 dtp
->u
.p
.current_unit
->round_status
= dtp
->u
.p
.current_unit
->flags
.round
;
2393 /* Check the sign mode. */
2394 dtp
->u
.p
.sign_status
2395 = !(cf
& IOPARM_DT_HAS_SIGN
) ? SIGN_UNSPECIFIED
:
2396 find_option (&dtp
->common
, dtp
->sign
, dtp
->sign_len
, sign_opt
,
2397 "Bad SIGN parameter in data transfer statement");
2399 if (dtp
->u
.p
.sign_status
== SIGN_UNSPECIFIED
)
2400 dtp
->u
.p
.sign_status
= dtp
->u
.p
.current_unit
->flags
.sign
;
2402 /* Check the blank mode. */
2403 dtp
->u
.p
.blank_status
2404 = !(cf
& IOPARM_DT_HAS_BLANK
) ? BLANK_UNSPECIFIED
:
2405 find_option (&dtp
->common
, dtp
->blank
, dtp
->blank_len
,
2407 "Bad BLANK parameter in data transfer statement");
2409 if (dtp
->u
.p
.blank_status
== BLANK_UNSPECIFIED
)
2410 dtp
->u
.p
.blank_status
= dtp
->u
.p
.current_unit
->flags
.blank
;
2412 /* Check the delim mode. */
2413 dtp
->u
.p
.current_unit
->delim_status
2414 = !(cf
& IOPARM_DT_HAS_DELIM
) ? DELIM_UNSPECIFIED
:
2415 find_option (&dtp
->common
, dtp
->delim
, dtp
->delim_len
,
2416 delim_opt
, "Bad DELIM parameter in data transfer statement");
2418 if (dtp
->u
.p
.current_unit
->delim_status
== DELIM_UNSPECIFIED
)
2419 dtp
->u
.p
.current_unit
->delim_status
= dtp
->u
.p
.current_unit
->flags
.delim
;
2421 /* Check the pad mode. */
2422 dtp
->u
.p
.current_unit
->pad_status
2423 = !(cf
& IOPARM_DT_HAS_PAD
) ? PAD_UNSPECIFIED
:
2424 find_option (&dtp
->common
, dtp
->pad
, dtp
->pad_len
, pad_opt
,
2425 "Bad PAD parameter in data transfer statement");
2427 if (dtp
->u
.p
.current_unit
->pad_status
== PAD_UNSPECIFIED
)
2428 dtp
->u
.p
.current_unit
->pad_status
= dtp
->u
.p
.current_unit
->flags
.pad
;
2430 /* Check to see if we might be reading what we wrote before */
2432 if (dtp
->u
.p
.mode
!= dtp
->u
.p
.current_unit
->mode
2433 && !is_internal_unit (dtp
))
2435 int pos
= fbuf_reset (dtp
->u
.p
.current_unit
);
2437 sseek (dtp
->u
.p
.current_unit
->s
, pos
, SEEK_CUR
);
2438 sflush(dtp
->u
.p
.current_unit
->s
);
2441 /* Check the POS= specifier: that it is in range and that it is used with a
2442 unit that has been connected for STREAM access. F2003 9.5.1.10. */
2444 if (((cf
& IOPARM_DT_HAS_POS
) != 0))
2446 if (is_stream_io (dtp
))
2451 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2452 "POS=specifier must be positive");
2456 if (dtp
->pos
>= dtp
->u
.p
.current_unit
->maxrec
)
2458 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2459 "POS=specifier too large");
2463 dtp
->rec
= dtp
->pos
;
2465 if (dtp
->u
.p
.mode
== READING
)
2467 /* Reset the endfile flag; if we hit EOF during reading
2468 we'll set the flag and generate an error at that point
2469 rather than worrying about it here. */
2470 dtp
->u
.p
.current_unit
->endfile
= NO_ENDFILE
;
2473 if (dtp
->pos
!= dtp
->u
.p
.current_unit
->strm_pos
)
2475 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
2476 if (sseek (dtp
->u
.p
.current_unit
->s
, dtp
->pos
- 1, SEEK_SET
) < 0)
2478 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2481 dtp
->u
.p
.current_unit
->strm_pos
= dtp
->pos
;
2486 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2487 "POS=specifier not allowed, "
2488 "Try OPEN with ACCESS='stream'");
2494 /* Sanity checks on the record number. */
2495 if ((cf
& IOPARM_DT_HAS_REC
) != 0)
2499 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2500 "Record number must be positive");
2504 if (dtp
->rec
>= dtp
->u
.p
.current_unit
->maxrec
)
2506 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2507 "Record number too large");
2511 /* Make sure format buffer is reset. */
2512 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
)
2513 fbuf_reset (dtp
->u
.p
.current_unit
);
2516 /* Check whether the record exists to be read. Only
2517 a partial record needs to exist. */
2519 if (dtp
->u
.p
.mode
== READING
&& (dtp
->rec
- 1)
2520 * dtp
->u
.p
.current_unit
->recl
>= file_length (dtp
->u
.p
.current_unit
->s
))
2522 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2523 "Non-existing record number");
2527 /* Position the file. */
2528 if (sseek (dtp
->u
.p
.current_unit
->s
, (gfc_offset
) (dtp
->rec
- 1)
2529 * dtp
->u
.p
.current_unit
->recl
, SEEK_SET
) < 0)
2531 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2535 /* TODO: This is required to maintain compatibility between
2536 4.3 and 4.4 runtime. Remove when ABI changes from 4.3 */
2538 if (is_stream_io (dtp
))
2539 dtp
->u
.p
.current_unit
->strm_pos
= dtp
->rec
;
2541 /* TODO: Un-comment this code when ABI changes from 4.3.
2542 if (dtp->u.p.current_unit->flags.access == ACCESS_STREAM)
2544 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2545 "Record number not allowed for stream access "
2551 /* Bugware for badly written mixed C-Fortran I/O. */
2552 flush_if_preconnected(dtp
->u
.p
.current_unit
->s
);
2554 dtp
->u
.p
.current_unit
->mode
= dtp
->u
.p
.mode
;
2556 /* Set the maximum position reached from the previous I/O operation. This
2557 could be greater than zero from a previous non-advancing write. */
2558 dtp
->u
.p
.max_pos
= dtp
->u
.p
.current_unit
->saved_pos
;
2563 /* Set up the subroutine that will handle the transfers. */
2567 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
)
2568 dtp
->u
.p
.transfer
= unformatted_read
;
2571 if ((cf
& IOPARM_DT_LIST_FORMAT
) != 0)
2572 dtp
->u
.p
.transfer
= list_formatted_read
;
2574 dtp
->u
.p
.transfer
= formatted_transfer
;
2579 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
)
2580 dtp
->u
.p
.transfer
= unformatted_write
;
2583 if ((cf
& IOPARM_DT_LIST_FORMAT
) != 0)
2584 dtp
->u
.p
.transfer
= list_formatted_write
;
2586 dtp
->u
.p
.transfer
= formatted_transfer
;
2590 /* Make sure that we don't do a read after a nonadvancing write. */
2594 if (dtp
->u
.p
.current_unit
->read_bad
&& !is_stream_io (dtp
))
2596 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2597 "Cannot READ after a nonadvancing WRITE");
2603 if (dtp
->u
.p
.advance_status
== ADVANCE_YES
&& !dtp
->u
.p
.seen_dollar
)
2604 dtp
->u
.p
.current_unit
->read_bad
= 1;
2607 /* Start the data transfer if we are doing a formatted transfer. */
2608 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
2609 && ((cf
& (IOPARM_DT_LIST_FORMAT
| IOPARM_DT_HAS_NAMELIST_NAME
)) == 0)
2610 && dtp
->u
.p
.ionml
== NULL
)
2611 formatted_transfer (dtp
, 0, NULL
, 0, 0, 1);
2614 /* Initialize an array_loop_spec given the array descriptor. The function
2615 returns the index of the last element of the array, and also returns
2616 starting record, where the first I/O goes to (necessary in case of
2617 negative strides). */
2620 init_loop_spec (gfc_array_char
*desc
, array_loop_spec
*ls
,
2621 gfc_offset
*start_record
)
2623 int rank
= GFC_DESCRIPTOR_RANK(desc
);
2632 for (i
=0; i
<rank
; i
++)
2634 ls
[i
].idx
= GFC_DESCRIPTOR_LBOUND(desc
,i
);
2635 ls
[i
].start
= GFC_DESCRIPTOR_LBOUND(desc
,i
);
2636 ls
[i
].end
= GFC_DESCRIPTOR_UBOUND(desc
,i
);
2637 ls
[i
].step
= GFC_DESCRIPTOR_STRIDE(desc
,i
);
2638 empty
= empty
|| (GFC_DESCRIPTOR_UBOUND(desc
,i
)
2639 < GFC_DESCRIPTOR_LBOUND(desc
,i
));
2641 if (GFC_DESCRIPTOR_STRIDE(desc
,i
) > 0)
2643 index
+= (GFC_DESCRIPTOR_EXTENT(desc
,i
) - 1)
2644 * GFC_DESCRIPTOR_STRIDE(desc
,i
);
2648 index
-= (GFC_DESCRIPTOR_EXTENT(desc
,i
) - 1)
2649 * GFC_DESCRIPTOR_STRIDE(desc
,i
);
2650 *start_record
-= (GFC_DESCRIPTOR_EXTENT(desc
,i
) - 1)
2651 * GFC_DESCRIPTOR_STRIDE(desc
,i
);
2661 /* Determine the index to the next record in an internal unit array by
2662 by incrementing through the array_loop_spec. */
2665 next_array_record (st_parameter_dt
*dtp
, array_loop_spec
*ls
, int *finished
)
2673 for (i
= 0; i
< dtp
->u
.p
.current_unit
->rank
; i
++)
2678 if (ls
[i
].idx
> ls
[i
].end
)
2680 ls
[i
].idx
= ls
[i
].start
;
2686 index
= index
+ (ls
[i
].idx
- ls
[i
].start
) * ls
[i
].step
;
2696 /* Skip to the end of the current record, taking care of an optional
2697 record marker of size bytes. If the file is not seekable, we
2698 read chunks of size MAX_READ until we get to the right
2702 skip_record (st_parameter_dt
*dtp
, ssize_t bytes
)
2704 ssize_t rlength
, readb
;
2705 static const ssize_t MAX_READ
= 4096;
2708 dtp
->u
.p
.current_unit
->bytes_left_subrecord
+= bytes
;
2709 if (dtp
->u
.p
.current_unit
->bytes_left_subrecord
== 0)
2712 if (is_seekable (dtp
->u
.p
.current_unit
->s
))
2714 /* Direct access files do not generate END conditions,
2716 if (sseek (dtp
->u
.p
.current_unit
->s
,
2717 dtp
->u
.p
.current_unit
->bytes_left_subrecord
, SEEK_CUR
) < 0)
2718 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2720 dtp
->u
.p
.current_unit
->bytes_left_subrecord
= 0;
2723 { /* Seek by reading data. */
2724 while (dtp
->u
.p
.current_unit
->bytes_left_subrecord
> 0)
2727 (MAX_READ
< dtp
->u
.p
.current_unit
->bytes_left_subrecord
) ?
2728 MAX_READ
: dtp
->u
.p
.current_unit
->bytes_left_subrecord
;
2730 readb
= sread (dtp
->u
.p
.current_unit
->s
, p
, rlength
);
2733 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2737 dtp
->u
.p
.current_unit
->bytes_left_subrecord
-= readb
;
2744 /* Advance to the next record reading unformatted files, taking
2745 care of subrecords. If complete_record is nonzero, we loop
2746 until all subrecords are cleared. */
2749 next_record_r_unf (st_parameter_dt
*dtp
, int complete_record
)
2753 bytes
= compile_options
.record_marker
== 0 ?
2754 sizeof (GFC_INTEGER_4
) : compile_options
.record_marker
;
2759 /* Skip over tail */
2761 skip_record (dtp
, bytes
);
2763 if ( ! (complete_record
&& dtp
->u
.p
.current_unit
->continued
))
2771 static inline gfc_offset
2772 min_off (gfc_offset a
, gfc_offset b
)
2774 return (a
< b
? a
: b
);
2778 /* Space to the next record for read mode. */
2781 next_record_r (st_parameter_dt
*dtp
, int done
)
2788 switch (current_mode (dtp
))
2790 /* No records in unformatted STREAM I/O. */
2791 case UNFORMATTED_STREAM
:
2794 case UNFORMATTED_SEQUENTIAL
:
2795 next_record_r_unf (dtp
, 1);
2796 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2799 case FORMATTED_DIRECT
:
2800 case UNFORMATTED_DIRECT
:
2801 skip_record (dtp
, dtp
->u
.p
.current_unit
->bytes_left
);
2804 case FORMATTED_STREAM
:
2805 case FORMATTED_SEQUENTIAL
:
2806 /* read_sf has already terminated input because of an '\n', or
2808 if (dtp
->u
.p
.sf_seen_eor
)
2810 dtp
->u
.p
.sf_seen_eor
= 0;
2814 if (is_internal_unit (dtp
))
2816 if (is_array_io (dtp
))
2820 record
= next_array_record (dtp
, dtp
->u
.p
.current_unit
->ls
,
2822 if (!done
&& finished
)
2825 /* Now seek to this record. */
2826 record
= record
* dtp
->u
.p
.current_unit
->recl
;
2827 if (sseek (dtp
->u
.p
.current_unit
->s
, record
, SEEK_SET
) < 0)
2829 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
2832 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2836 bytes_left
= (int) dtp
->u
.p
.current_unit
->bytes_left
;
2837 bytes_left
= min_off (bytes_left
,
2838 file_length (dtp
->u
.p
.current_unit
->s
)
2839 - stell (dtp
->u
.p
.current_unit
->s
));
2840 if (sseek (dtp
->u
.p
.current_unit
->s
,
2841 bytes_left
, SEEK_CUR
) < 0)
2843 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
2846 dtp
->u
.p
.current_unit
->bytes_left
2847 = dtp
->u
.p
.current_unit
->recl
;
2856 cc
= fbuf_getc (dtp
->u
.p
.current_unit
);
2860 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2863 if (is_stream_io (dtp
)
2864 || dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
2865 || dtp
->u
.p
.current_unit
->bytes_left
2866 == dtp
->u
.p
.current_unit
->recl
)
2872 if (is_stream_io (dtp
))
2873 dtp
->u
.p
.current_unit
->strm_pos
++;
2884 /* Small utility function to write a record marker, taking care of
2885 byte swapping and of choosing the correct size. */
2888 write_us_marker (st_parameter_dt
*dtp
, const gfc_offset buf
)
2893 char p
[sizeof (GFC_INTEGER_8
)];
2895 if (compile_options
.record_marker
== 0)
2896 len
= sizeof (GFC_INTEGER_4
);
2898 len
= compile_options
.record_marker
;
2900 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
2901 if (likely (dtp
->u
.p
.current_unit
->flags
.convert
== GFC_CONVERT_NATIVE
))
2905 case sizeof (GFC_INTEGER_4
):
2907 return swrite (dtp
->u
.p
.current_unit
->s
, &buf4
, len
);
2910 case sizeof (GFC_INTEGER_8
):
2912 return swrite (dtp
->u
.p
.current_unit
->s
, &buf8
, len
);
2916 runtime_error ("Illegal value for record marker");
2924 case sizeof (GFC_INTEGER_4
):
2926 reverse_memcpy (p
, &buf4
, sizeof (GFC_INTEGER_4
));
2927 return swrite (dtp
->u
.p
.current_unit
->s
, p
, len
);
2930 case sizeof (GFC_INTEGER_8
):
2932 reverse_memcpy (p
, &buf8
, sizeof (GFC_INTEGER_8
));
2933 return swrite (dtp
->u
.p
.current_unit
->s
, p
, len
);
2937 runtime_error ("Illegal value for record marker");
2944 /* Position to the next (sub)record in write mode for
2945 unformatted sequential files. */
2948 next_record_w_unf (st_parameter_dt
*dtp
, int next_subrecord
)
2950 gfc_offset m
, m_write
, record_marker
;
2952 /* Bytes written. */
2953 m
= dtp
->u
.p
.current_unit
->recl_subrecord
2954 - dtp
->u
.p
.current_unit
->bytes_left_subrecord
;
2956 /* Write the length tail. If we finish a record containing
2957 subrecords, we write out the negative length. */
2959 if (dtp
->u
.p
.current_unit
->continued
)
2964 if (unlikely (write_us_marker (dtp
, m_write
) < 0))
2967 if (compile_options
.record_marker
== 0)
2968 record_marker
= sizeof (GFC_INTEGER_4
);
2970 record_marker
= compile_options
.record_marker
;
2972 /* Seek to the head and overwrite the bogus length with the real
2975 if (unlikely (sseek (dtp
->u
.p
.current_unit
->s
, - m
- 2 * record_marker
,
2984 if (unlikely (write_us_marker (dtp
, m_write
) < 0))
2987 /* Seek past the end of the current record. */
2989 if (unlikely (sseek (dtp
->u
.p
.current_unit
->s
, m
+ record_marker
,
2996 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
3002 /* Utility function like memset() but operating on streams. Return
3003 value is same as for POSIX write(). */
3006 sset (stream
* s
, int c
, ssize_t nbyte
)
3008 static const int WRITE_CHUNK
= 256;
3009 char p
[WRITE_CHUNK
];
3010 ssize_t bytes_left
, trans
;
3012 if (nbyte
< WRITE_CHUNK
)
3013 memset (p
, c
, nbyte
);
3015 memset (p
, c
, WRITE_CHUNK
);
3018 while (bytes_left
> 0)
3020 trans
= (bytes_left
< WRITE_CHUNK
) ? bytes_left
: WRITE_CHUNK
;
3021 trans
= swrite (s
, p
, trans
);
3024 bytes_left
-= trans
;
3027 return nbyte
- bytes_left
;
3030 /* Position to the next record in write mode. */
3033 next_record_w (st_parameter_dt
*dtp
, int done
)
3035 gfc_offset m
, record
, max_pos
;
3038 /* Zero counters for X- and T-editing. */
3039 max_pos
= dtp
->u
.p
.max_pos
;
3040 dtp
->u
.p
.max_pos
= dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
3042 switch (current_mode (dtp
))
3044 /* No records in unformatted STREAM I/O. */
3045 case UNFORMATTED_STREAM
:
3048 case FORMATTED_DIRECT
:
3049 if (dtp
->u
.p
.current_unit
->bytes_left
== 0)
3052 fbuf_seek (dtp
->u
.p
.current_unit
, 0, SEEK_END
);
3053 fbuf_flush (dtp
->u
.p
.current_unit
, WRITING
);
3054 if (sset (dtp
->u
.p
.current_unit
->s
, ' ',
3055 dtp
->u
.p
.current_unit
->bytes_left
)
3056 != dtp
->u
.p
.current_unit
->bytes_left
)
3061 case UNFORMATTED_DIRECT
:
3062 if (dtp
->u
.p
.current_unit
->bytes_left
> 0)
3064 length
= (int) dtp
->u
.p
.current_unit
->bytes_left
;
3065 if (sset (dtp
->u
.p
.current_unit
->s
, 0, length
) != length
)
3070 case UNFORMATTED_SEQUENTIAL
:
3071 next_record_w_unf (dtp
, 0);
3072 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
3075 case FORMATTED_STREAM
:
3076 case FORMATTED_SEQUENTIAL
:
3078 if (is_internal_unit (dtp
))
3080 if (is_array_io (dtp
))
3084 length
= (int) dtp
->u
.p
.current_unit
->bytes_left
;
3086 /* If the farthest position reached is greater than current
3087 position, adjust the position and set length to pad out
3088 whats left. Otherwise just pad whats left.
3089 (for character array unit) */
3090 m
= dtp
->u
.p
.current_unit
->recl
3091 - dtp
->u
.p
.current_unit
->bytes_left
;
3094 length
= (int) (max_pos
- m
);
3095 if (sseek (dtp
->u
.p
.current_unit
->s
,
3096 length
, SEEK_CUR
) < 0)
3098 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
3101 length
= (int) (dtp
->u
.p
.current_unit
->recl
- max_pos
);
3104 if (sset (dtp
->u
.p
.current_unit
->s
, ' ', length
) != length
)
3106 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
3110 /* Now that the current record has been padded out,
3111 determine where the next record in the array is. */
3112 record
= next_array_record (dtp
, dtp
->u
.p
.current_unit
->ls
,
3115 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
3117 /* Now seek to this record */
3118 record
= record
* dtp
->u
.p
.current_unit
->recl
;
3120 if (sseek (dtp
->u
.p
.current_unit
->s
, record
, SEEK_SET
) < 0)
3122 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
3126 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
3132 /* If this is the last call to next_record move to the farthest
3133 position reached and set length to pad out the remainder
3134 of the record. (for character scaler unit) */
3137 m
= dtp
->u
.p
.current_unit
->recl
3138 - dtp
->u
.p
.current_unit
->bytes_left
;
3141 length
= (int) (max_pos
- m
);
3142 if (sseek (dtp
->u
.p
.current_unit
->s
,
3143 length
, SEEK_CUR
) < 0)
3145 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
3148 length
= (int) (dtp
->u
.p
.current_unit
->recl
- max_pos
);
3151 length
= (int) dtp
->u
.p
.current_unit
->bytes_left
;
3154 if (sset (dtp
->u
.p
.current_unit
->s
, ' ', length
) != length
)
3156 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
3168 fbuf_seek (dtp
->u
.p
.current_unit
, 0, SEEK_END
);
3169 char * p
= fbuf_alloc (dtp
->u
.p
.current_unit
, len
);
3176 if (is_stream_io (dtp
))
3178 dtp
->u
.p
.current_unit
->strm_pos
+= len
;
3179 if (dtp
->u
.p
.current_unit
->strm_pos
3180 < file_length (dtp
->u
.p
.current_unit
->s
))
3181 unit_truncate (dtp
->u
.p
.current_unit
,
3182 dtp
->u
.p
.current_unit
->strm_pos
- 1,
3190 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
3195 /* Position to the next record, which means moving to the end of the
3196 current record. This can happen under several different
3197 conditions. If the done flag is not set, we get ready to process
3201 next_record (st_parameter_dt
*dtp
, int done
)
3203 gfc_offset fp
; /* File position. */
3205 dtp
->u
.p
.current_unit
->read_bad
= 0;
3207 if (dtp
->u
.p
.mode
== READING
)
3208 next_record_r (dtp
, done
);
3210 next_record_w (dtp
, done
);
3212 if (!is_stream_io (dtp
))
3214 /* Keep position up to date for INQUIRE */
3216 update_position (dtp
->u
.p
.current_unit
);
3218 dtp
->u
.p
.current_unit
->current_record
= 0;
3219 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
3221 fp
= stell (dtp
->u
.p
.current_unit
->s
);
3222 /* Calculate next record, rounding up partial records. */
3223 dtp
->u
.p
.current_unit
->last_record
=
3224 (fp
+ dtp
->u
.p
.current_unit
->recl
- 1) /
3225 dtp
->u
.p
.current_unit
->recl
;
3228 dtp
->u
.p
.current_unit
->last_record
++;
3234 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
3238 /* Finalize the current data transfer. For a nonadvancing transfer,
3239 this means advancing to the next record. For internal units close the
3240 stream associated with the unit. */
3243 finalize_transfer (st_parameter_dt
*dtp
)
3246 GFC_INTEGER_4 cf
= dtp
->common
.flags
;
3248 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
3249 *dtp
->size
= dtp
->u
.p
.size_used
;
3251 if (dtp
->u
.p
.eor_condition
)
3253 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
3257 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
3259 if (dtp
->u
.p
.current_unit
&& current_mode (dtp
) == UNFORMATTED_SEQUENTIAL
)
3260 dtp
->u
.p
.current_unit
->current_record
= 0;
3264 if ((dtp
->u
.p
.ionml
!= NULL
)
3265 && (cf
& IOPARM_DT_HAS_NAMELIST_NAME
) != 0)
3267 if ((cf
& IOPARM_DT_NAMELIST_READ_MODE
) != 0)
3268 namelist_read (dtp
);
3270 namelist_write (dtp
);
3273 dtp
->u
.p
.transfer
= NULL
;
3274 if (dtp
->u
.p
.current_unit
== NULL
)
3277 dtp
->u
.p
.eof_jump
= &eof_jump
;
3278 if (setjmp (eof_jump
))
3280 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
3284 if ((cf
& IOPARM_DT_LIST_FORMAT
) != 0 && dtp
->u
.p
.mode
== READING
)
3286 finish_list_read (dtp
);
3290 if (dtp
->u
.p
.mode
== WRITING
)
3291 dtp
->u
.p
.current_unit
->previous_nonadvancing_write
3292 = dtp
->u
.p
.advance_status
== ADVANCE_NO
;
3294 if (is_stream_io (dtp
))
3296 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
3297 && dtp
->u
.p
.advance_status
!= ADVANCE_NO
)
3298 next_record (dtp
, 1);
3303 dtp
->u
.p
.current_unit
->current_record
= 0;
3305 if (!is_internal_unit (dtp
) && dtp
->u
.p
.seen_dollar
)
3307 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
3308 dtp
->u
.p
.seen_dollar
= 0;
3312 /* For non-advancing I/O, save the current maximum position for use in the
3313 next I/O operation if needed. */
3314 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
)
3316 int bytes_written
= (int) (dtp
->u
.p
.current_unit
->recl
3317 - dtp
->u
.p
.current_unit
->bytes_left
);
3318 dtp
->u
.p
.current_unit
->saved_pos
=
3319 dtp
->u
.p
.max_pos
> 0 ? dtp
->u
.p
.max_pos
- bytes_written
: 0;
3320 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
3323 else if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
3324 && dtp
->u
.p
.mode
== WRITING
&& !is_internal_unit (dtp
))
3325 fbuf_seek (dtp
->u
.p
.current_unit
, 0, SEEK_END
);
3327 dtp
->u
.p
.current_unit
->saved_pos
= 0;
3329 next_record (dtp
, 1);
3332 /* Transfer function for IOLENGTH. It doesn't actually do any
3333 data transfer, it just updates the length counter. */
3336 iolength_transfer (st_parameter_dt
*dtp
, bt type
__attribute__((unused
)),
3337 void *dest
__attribute__ ((unused
)),
3338 int kind
__attribute__((unused
)),
3339 size_t size
, size_t nelems
)
3341 if ((dtp
->common
.flags
& IOPARM_DT_HAS_IOLENGTH
) != 0)
3342 *dtp
->iolength
+= (GFC_IO_INT
) (size
* nelems
);
3346 /* Initialize the IOLENGTH data transfer. This function is in essence
3347 a very much simplified version of data_transfer_init(), because it
3348 doesn't have to deal with units at all. */
3351 iolength_transfer_init (st_parameter_dt
*dtp
)
3353 if ((dtp
->common
.flags
& IOPARM_DT_HAS_IOLENGTH
) != 0)
3356 memset (&dtp
->u
.p
, 0, sizeof (dtp
->u
.p
));
3358 /* Set up the subroutine that will handle the transfers. */
3360 dtp
->u
.p
.transfer
= iolength_transfer
;
3364 /* Library entry point for the IOLENGTH form of the INQUIRE
3365 statement. The IOLENGTH form requires no I/O to be performed, but
3366 it must still be a runtime library call so that we can determine
3367 the iolength for dynamic arrays and such. */
3369 extern void st_iolength (st_parameter_dt
*);
3370 export_proto(st_iolength
);
3373 st_iolength (st_parameter_dt
*dtp
)
3375 library_start (&dtp
->common
);
3376 iolength_transfer_init (dtp
);
3379 extern void st_iolength_done (st_parameter_dt
*);
3380 export_proto(st_iolength_done
);
3383 st_iolength_done (st_parameter_dt
*dtp
__attribute__((unused
)))
3390 /* The READ statement. */
3392 extern void st_read (st_parameter_dt
*);
3393 export_proto(st_read
);
3396 st_read (st_parameter_dt
*dtp
)
3398 library_start (&dtp
->common
);
3400 data_transfer_init (dtp
, 1);
3403 extern void st_read_done (st_parameter_dt
*);
3404 export_proto(st_read_done
);
3407 st_read_done (st_parameter_dt
*dtp
)
3409 finalize_transfer (dtp
);
3410 if (is_internal_unit (dtp
) || dtp
->u
.p
.format_not_saved
)
3411 free_format_data (dtp
->u
.p
.fmt
);
3413 if (dtp
->u
.p
.current_unit
!= NULL
)
3414 unlock_unit (dtp
->u
.p
.current_unit
);
3416 free_internal_unit (dtp
);
3421 extern void st_write (st_parameter_dt
*);
3422 export_proto(st_write
);
3425 st_write (st_parameter_dt
*dtp
)
3427 library_start (&dtp
->common
);
3428 data_transfer_init (dtp
, 0);
3431 extern void st_write_done (st_parameter_dt
*);
3432 export_proto(st_write_done
);
3435 st_write_done (st_parameter_dt
*dtp
)
3437 finalize_transfer (dtp
);
3439 /* Deal with endfile conditions associated with sequential files. */
3441 if (dtp
->u
.p
.current_unit
!= NULL
3442 && dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
3443 switch (dtp
->u
.p
.current_unit
->endfile
)
3445 case AT_ENDFILE
: /* Remain at the endfile record. */
3449 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
; /* Just at it now. */
3453 /* Get rid of whatever is after this record. */
3454 if (!is_internal_unit (dtp
))
3455 unit_truncate (dtp
->u
.p
.current_unit
,
3456 stell (dtp
->u
.p
.current_unit
->s
),
3458 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
3462 if (is_internal_unit (dtp
) || dtp
->u
.p
.format_not_saved
)
3463 free_format_data (dtp
->u
.p
.fmt
);
3465 if (dtp
->u
.p
.current_unit
!= NULL
)
3466 unlock_unit (dtp
->u
.p
.current_unit
);
3468 free_internal_unit (dtp
);
3474 /* F2003: This is a stub for the runtime portion of the WAIT statement. */
3476 st_wait (st_parameter_wait
*wtp
__attribute__((unused
)))
3481 /* Receives the scalar information for namelist objects and stores it
3482 in a linked list of namelist_info types. */
3484 extern void st_set_nml_var (st_parameter_dt
*dtp
, void *, char *,
3485 GFC_INTEGER_4
, gfc_charlen_type
, GFC_INTEGER_4
);
3486 export_proto(st_set_nml_var
);
3490 st_set_nml_var (st_parameter_dt
*dtp
, void * var_addr
, char * var_name
,
3491 GFC_INTEGER_4 len
, gfc_charlen_type string_length
,
3492 GFC_INTEGER_4 dtype
)
3494 namelist_info
*t1
= NULL
;
3496 size_t var_name_len
= strlen (var_name
);
3498 nml
= (namelist_info
*) get_mem (sizeof (namelist_info
));
3500 nml
->mem_pos
= var_addr
;
3502 nml
->var_name
= (char*) get_mem (var_name_len
+ 1);
3503 memcpy (nml
->var_name
, var_name
, var_name_len
);
3504 nml
->var_name
[var_name_len
] = '\0';
3506 nml
->len
= (int) len
;
3507 nml
->string_length
= (index_type
) string_length
;
3509 nml
->var_rank
= (int) (dtype
& GFC_DTYPE_RANK_MASK
);
3510 nml
->size
= (index_type
) (dtype
>> GFC_DTYPE_SIZE_SHIFT
);
3511 nml
->type
= (bt
) ((dtype
& GFC_DTYPE_TYPE_MASK
) >> GFC_DTYPE_TYPE_SHIFT
);
3513 if (nml
->var_rank
> 0)
3515 nml
->dim
= (descriptor_dimension
*)
3516 get_mem (nml
->var_rank
* sizeof (descriptor_dimension
));
3517 nml
->ls
= (array_loop_spec
*)
3518 get_mem (nml
->var_rank
* sizeof (array_loop_spec
));
3528 if ((dtp
->common
.flags
& IOPARM_DT_IONML_SET
) == 0)
3530 dtp
->common
.flags
|= IOPARM_DT_IONML_SET
;
3531 dtp
->u
.p
.ionml
= nml
;
3535 for (t1
= dtp
->u
.p
.ionml
; t1
->next
; t1
= t1
->next
);
3540 /* Store the dimensional information for the namelist object. */
3541 extern void st_set_nml_var_dim (st_parameter_dt
*, GFC_INTEGER_4
,
3542 index_type
, index_type
,
3544 export_proto(st_set_nml_var_dim
);
3547 st_set_nml_var_dim (st_parameter_dt
*dtp
, GFC_INTEGER_4 n_dim
,
3548 index_type stride
, index_type lbound
,
3551 namelist_info
* nml
;
3556 for (nml
= dtp
->u
.p
.ionml
; nml
->next
; nml
= nml
->next
);
3558 GFC_DIMENSION_SET(nml
->dim
[n
],lbound
,ubound
,stride
);
3561 /* Reverse memcpy - used for byte swapping. */
3563 void reverse_memcpy (void *dest
, const void *src
, size_t n
)
3569 s
= (char *) src
+ n
- 1;
3571 /* Write with ascending order - this is likely faster
3572 on modern architectures because of write combining. */
3578 /* Once upon a time, a poor innocent Fortran program was reading a
3579 file, when suddenly it hit the end-of-file (EOF). Unfortunately
3580 the OS doesn't tell whether we're at the EOF or whether we already
3581 went past it. Luckily our hero, libgfortran, keeps track of this.
3582 Call this function when you detect an EOF condition. See Section
3586 hit_eof (st_parameter_dt
* dtp
)
3588 dtp
->u
.p
.current_unit
->flags
.position
= POSITION_APPEND
;
3590 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
3591 switch (dtp
->u
.p
.current_unit
->endfile
)
3595 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
3596 if (!is_internal_unit (dtp
))
3598 dtp
->u
.p
.current_unit
->endfile
= AFTER_ENDFILE
;
3599 dtp
->u
.p
.current_unit
->current_record
= 0;
3602 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
3606 generate_error (&dtp
->common
, LIBERROR_ENDFILE
, NULL
);
3607 dtp
->u
.p
.current_unit
->current_record
= 0;
3612 /* Non-sequential files don't have an ENDFILE record, so we
3613 can't be at AFTER_ENDFILE. */
3614 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
3615 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
3616 dtp
->u
.p
.current_unit
->current_record
= 0;