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_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 /* Read sequential file - internal unit */
183 read_sf_internal (st_parameter_dt
*dtp
, int * length
)
185 static char *empty_string
[0];
189 /* Zero size array gives internal unit len of 0. Nothing to read. */
190 if (dtp
->internal_unit_len
== 0
191 && dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
)
194 /* If we have seen an eor previously, return a length of 0. The
195 caller is responsible for correctly padding the input field. */
196 if (dtp
->u
.p
.sf_seen_eor
)
199 /* Just return something that isn't a NULL pointer, otherwise the
200 caller thinks an error occured. */
201 return (char*) empty_string
;
205 if (is_char4_unit(dtp
))
208 gfc_char4_t
*p
= (gfc_char4_t
*) mem_alloc_r4 (dtp
->u
.p
.current_unit
->s
,
210 base
= fbuf_alloc (dtp
->u
.p
.current_unit
, lorig
);
211 for (i
= 0; i
< *length
; i
++, p
++)
212 base
[i
] = *p
> 255 ? '?' : (unsigned char) *p
;
215 base
= mem_alloc_r (dtp
->u
.p
.current_unit
->s
, length
);
217 if (unlikely (lorig
> *length
))
223 dtp
->u
.p
.current_unit
->bytes_left
-= *length
;
225 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
226 dtp
->u
.p
.size_used
+= (GFC_IO_INT
) *length
;
232 /* When reading sequential formatted records we have a problem. We
233 don't know how long the line is until we read the trailing newline,
234 and we don't want to read too much. If we read too much, we might
235 have to do a physical seek backwards depending on how much data is
236 present, and devices like terminals aren't seekable and would cause
239 Given this, the solution is to read a byte at a time, stopping if
240 we hit the newline. For small allocations, we use a static buffer.
241 For larger allocations, we are forced to allocate memory on the
242 heap. Hopefully this won't happen very often. */
244 /* Read sequential file - external unit */
247 read_sf (st_parameter_dt
*dtp
, int * length
)
249 static char *empty_string
[0];
251 int n
, lorig
, seen_comma
;
253 /* If we have seen an eor previously, return a length of 0. The
254 caller is responsible for correctly padding the input field. */
255 if (dtp
->u
.p
.sf_seen_eor
)
258 /* Just return something that isn't a NULL pointer, otherwise the
259 caller thinks an error occured. */
260 return (char*) empty_string
;
265 /* Read data into format buffer and scan through it. */
267 base
= p
= fbuf_read (dtp
->u
.p
.current_unit
, length
);
275 if (q
== '\n' || q
== '\r')
277 /* Unexpected end of line. Set the position. */
278 fbuf_seek (dtp
->u
.p
.current_unit
, n
+ 1 ,SEEK_CUR
);
279 dtp
->u
.p
.sf_seen_eor
= 1;
281 /* If we see an EOR during non-advancing I/O, we need to skip
282 the rest of the I/O statement. Set the corresponding flag. */
283 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
|| dtp
->u
.p
.seen_dollar
)
284 dtp
->u
.p
.eor_condition
= 1;
286 /* If we encounter a CR, it might be a CRLF. */
287 if (q
== '\r') /* Probably a CRLF */
289 /* See if there is an LF. Use fbuf_read rather then fbuf_getc so
290 the position is not advanced unless it really is an LF. */
292 p
= fbuf_read (dtp
->u
.p
.current_unit
, &readlen
);
293 if (*p
== '\n' && readlen
== 1)
295 dtp
->u
.p
.sf_seen_eor
= 2;
296 fbuf_seek (dtp
->u
.p
.current_unit
, 1 ,SEEK_CUR
);
300 /* Without padding, terminate the I/O statement without assigning
301 the value. With padding, the value still needs to be assigned,
302 so we can just continue with a short read. */
303 if (dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
)
305 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
312 /* Short circuit the read if a comma is found during numeric input.
313 The flag is set to zero during character reads so that commas in
314 strings are not ignored */
316 if (dtp
->u
.p
.sf_read_comma
== 1)
319 notify_std (&dtp
->common
, GFC_STD_GNU
,
320 "Comma in formatted numeric read.");
328 fbuf_seek (dtp
->u
.p
.current_unit
, n
+ seen_comma
, SEEK_CUR
);
330 /* A short read implies we hit EOF, unless we hit EOR, a comma, or
331 some other stuff. Set the relevant flags. */
332 if (lorig
> *length
&& !dtp
->u
.p
.sf_seen_eor
&& !seen_comma
)
336 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
)
338 if (dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
)
344 dtp
->u
.p
.eor_condition
= 1;
349 else if (dtp
->u
.p
.advance_status
== ADVANCE_NO
350 || dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
351 || dtp
->u
.p
.current_unit
->bytes_left
352 == dtp
->u
.p
.current_unit
->recl
)
361 dtp
->u
.p
.current_unit
->bytes_left
-= n
;
363 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
364 dtp
->u
.p
.size_used
+= (GFC_IO_INT
) n
;
370 /* Function for reading the next couple of bytes from the current
371 file, advancing the current position. We return FAILURE on end of record or
372 end of file. This function is only for formatted I/O, unformatted uses
375 If the read is short, then it is because the current record does not
376 have enough data to satisfy the read request and the file was
377 opened with PAD=YES. The caller must assume tailing spaces for
381 read_block_form (st_parameter_dt
*dtp
, int * nbytes
)
386 if (!is_stream_io (dtp
))
388 if (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) *nbytes
)
390 /* For preconnected units with default record length, set bytes left
391 to unit record length and proceed, otherwise error. */
392 if (dtp
->u
.p
.current_unit
->unit_number
== options
.stdin_unit
393 && dtp
->u
.p
.current_unit
->recl
== DEFAULT_RECL
)
394 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
397 if (unlikely (dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
)
398 && !is_internal_unit (dtp
))
400 /* Not enough data left. */
401 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
406 if (unlikely (dtp
->u
.p
.current_unit
->bytes_left
== 0
407 && !is_internal_unit(dtp
)))
413 *nbytes
= dtp
->u
.p
.current_unit
->bytes_left
;
417 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
&&
418 (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
||
419 dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_STREAM
))
421 if (is_internal_unit (dtp
))
422 source
= read_sf_internal (dtp
, nbytes
);
424 source
= read_sf (dtp
, nbytes
);
426 dtp
->u
.p
.current_unit
->strm_pos
+=
427 (gfc_offset
) (*nbytes
+ dtp
->u
.p
.sf_seen_eor
);
431 /* If we reach here, we can assume it's direct access. */
433 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) *nbytes
;
436 source
= fbuf_read (dtp
->u
.p
.current_unit
, nbytes
);
437 fbuf_seek (dtp
->u
.p
.current_unit
, *nbytes
, SEEK_CUR
);
439 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
440 dtp
->u
.p
.size_used
+= (GFC_IO_INT
) *nbytes
;
442 if (norig
!= *nbytes
)
444 /* Short read, this shouldn't happen. */
445 if (!dtp
->u
.p
.current_unit
->pad_status
== PAD_YES
)
447 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
452 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) *nbytes
;
458 /* Read a block from a character(kind=4) internal unit, to be transferred into
459 a character(kind=4) variable. Note: Portions of this code borrowed from
462 read_block_form4 (st_parameter_dt
*dtp
, int * nbytes
)
464 static gfc_char4_t
*empty_string
[0];
468 if (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) *nbytes
)
469 *nbytes
= dtp
->u
.p
.current_unit
->bytes_left
;
471 /* Zero size array gives internal unit len of 0. Nothing to read. */
472 if (dtp
->internal_unit_len
== 0
473 && dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
)
476 /* If we have seen an eor previously, return a length of 0. The
477 caller is responsible for correctly padding the input field. */
478 if (dtp
->u
.p
.sf_seen_eor
)
481 /* Just return something that isn't a NULL pointer, otherwise the
482 caller thinks an error occured. */
487 source
= (gfc_char4_t
*) mem_alloc_r4 (dtp
->u
.p
.current_unit
->s
, nbytes
);
489 if (unlikely (lorig
> *nbytes
))
495 dtp
->u
.p
.current_unit
->bytes_left
-= *nbytes
;
497 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
498 dtp
->u
.p
.size_used
+= (GFC_IO_INT
) *nbytes
;
504 /* Reads a block directly into application data space. This is for
505 unformatted files. */
508 read_block_direct (st_parameter_dt
*dtp
, void *buf
, size_t nbytes
)
510 ssize_t to_read_record
;
511 ssize_t have_read_record
;
512 ssize_t to_read_subrecord
;
513 ssize_t have_read_subrecord
;
516 if (is_stream_io (dtp
))
518 have_read_record
= sread (dtp
->u
.p
.current_unit
->s
, buf
,
520 if (unlikely (have_read_record
< 0))
522 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
526 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) have_read_record
;
528 if (unlikely ((ssize_t
) nbytes
!= have_read_record
))
530 /* Short read, e.g. if we hit EOF. For stream files,
531 we have to set the end-of-file condition. */
537 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
539 if (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) nbytes
)
542 to_read_record
= dtp
->u
.p
.current_unit
->bytes_left
;
543 nbytes
= to_read_record
;
548 to_read_record
= nbytes
;
551 dtp
->u
.p
.current_unit
->bytes_left
-= to_read_record
;
553 to_read_record
= sread (dtp
->u
.p
.current_unit
->s
, buf
, to_read_record
);
554 if (unlikely (to_read_record
< 0))
556 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
560 if (to_read_record
!= (ssize_t
) nbytes
)
562 /* Short read, e.g. if we hit EOF. Apparently, we read
563 more than was written to the last record. */
567 if (unlikely (short_record
))
569 generate_error (&dtp
->common
, LIBERROR_SHORT_RECORD
, NULL
);
574 /* Unformatted sequential. We loop over the subrecords, reading
575 until the request has been fulfilled or the record has run out
576 of continuation subrecords. */
578 /* Check whether we exceed the total record length. */
580 if (dtp
->u
.p
.current_unit
->flags
.has_recl
581 && ((gfc_offset
) nbytes
> dtp
->u
.p
.current_unit
->bytes_left
))
583 to_read_record
= dtp
->u
.p
.current_unit
->bytes_left
;
588 to_read_record
= nbytes
;
591 have_read_record
= 0;
595 if (dtp
->u
.p
.current_unit
->bytes_left_subrecord
596 < (gfc_offset
) to_read_record
)
598 to_read_subrecord
= dtp
->u
.p
.current_unit
->bytes_left_subrecord
;
599 to_read_record
-= to_read_subrecord
;
603 to_read_subrecord
= to_read_record
;
607 dtp
->u
.p
.current_unit
->bytes_left_subrecord
-= to_read_subrecord
;
609 have_read_subrecord
= sread (dtp
->u
.p
.current_unit
->s
,
610 buf
+ have_read_record
, to_read_subrecord
);
611 if (unlikely (have_read_subrecord
) < 0)
613 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
617 have_read_record
+= have_read_subrecord
;
619 if (unlikely (to_read_subrecord
!= have_read_subrecord
))
621 /* Short read, e.g. if we hit EOF. This means the record
622 structure has been corrupted, or the trailing record
623 marker would still be present. */
625 generate_error (&dtp
->common
, LIBERROR_CORRUPT_FILE
, NULL
);
629 if (to_read_record
> 0)
631 if (likely (dtp
->u
.p
.current_unit
->continued
))
633 next_record_r_unf (dtp
, 0);
638 /* Let's make sure the file position is correctly pre-positioned
639 for the next read statement. */
641 dtp
->u
.p
.current_unit
->current_record
= 0;
642 next_record_r_unf (dtp
, 0);
643 generate_error (&dtp
->common
, LIBERROR_SHORT_RECORD
, NULL
);
649 /* Normal exit, the read request has been fulfilled. */
654 dtp
->u
.p
.current_unit
->bytes_left
-= have_read_record
;
655 if (unlikely (short_record
))
657 generate_error (&dtp
->common
, LIBERROR_SHORT_RECORD
, NULL
);
664 /* Function for writing a block of bytes to the current file at the
665 current position, advancing the file pointer. We are given a length
666 and return a pointer to a buffer that the caller must (completely)
667 fill in. Returns NULL on error. */
670 write_block (st_parameter_dt
*dtp
, int length
)
674 if (!is_stream_io (dtp
))
676 if (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) length
)
678 /* For preconnected units with default record length, set bytes left
679 to unit record length and proceed, otherwise error. */
680 if (likely ((dtp
->u
.p
.current_unit
->unit_number
681 == options
.stdout_unit
682 || dtp
->u
.p
.current_unit
->unit_number
683 == options
.stderr_unit
)
684 && dtp
->u
.p
.current_unit
->recl
== DEFAULT_RECL
))
685 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
688 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
693 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) length
;
696 if (is_internal_unit (dtp
))
698 if (dtp
->common
.unit
) /* char4 internel unit. */
701 dest4
= mem_alloc_w4 (dtp
->u
.p
.current_unit
->s
, &length
);
704 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
710 dest
= mem_alloc_w (dtp
->u
.p
.current_unit
->s
, &length
);
714 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
718 if (unlikely (dtp
->u
.p
.current_unit
->endfile
== AT_ENDFILE
))
719 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
723 dest
= fbuf_alloc (dtp
->u
.p
.current_unit
, length
);
726 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
731 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
732 dtp
->u
.p
.size_used
+= (GFC_IO_INT
) length
;
734 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) length
;
740 /* High level interface to swrite(), taking care of errors. This is only
741 called for unformatted files. There are three cases to consider:
742 Stream I/O, unformatted direct, unformatted sequential. */
745 write_buf (st_parameter_dt
*dtp
, void *buf
, size_t nbytes
)
748 ssize_t have_written
;
749 ssize_t to_write_subrecord
;
754 if (is_stream_io (dtp
))
756 have_written
= swrite (dtp
->u
.p
.current_unit
->s
, buf
, nbytes
);
757 if (unlikely (have_written
< 0))
759 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
763 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) have_written
;
768 /* Unformatted direct access. */
770 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
772 if (unlikely (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) nbytes
))
774 generate_error (&dtp
->common
, LIBERROR_DIRECT_EOR
, NULL
);
778 if (buf
== NULL
&& nbytes
== 0)
781 have_written
= swrite (dtp
->u
.p
.current_unit
->s
, buf
, nbytes
);
782 if (unlikely (have_written
< 0))
784 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
788 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) have_written
;
789 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) have_written
;
794 /* Unformatted sequential. */
798 if (dtp
->u
.p
.current_unit
->flags
.has_recl
799 && (gfc_offset
) nbytes
> dtp
->u
.p
.current_unit
->bytes_left
)
801 nbytes
= dtp
->u
.p
.current_unit
->bytes_left
;
813 (size_t) dtp
->u
.p
.current_unit
->bytes_left_subrecord
< nbytes
?
814 (size_t) dtp
->u
.p
.current_unit
->bytes_left_subrecord
: nbytes
;
816 dtp
->u
.p
.current_unit
->bytes_left_subrecord
-=
817 (gfc_offset
) to_write_subrecord
;
819 to_write_subrecord
= swrite (dtp
->u
.p
.current_unit
->s
,
820 buf
+ have_written
, to_write_subrecord
);
821 if (unlikely (to_write_subrecord
< 0))
823 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
827 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) to_write_subrecord
;
828 nbytes
-= to_write_subrecord
;
829 have_written
+= to_write_subrecord
;
834 next_record_w_unf (dtp
, 1);
837 dtp
->u
.p
.current_unit
->bytes_left
-= have_written
;
838 if (unlikely (short_record
))
840 generate_error (&dtp
->common
, LIBERROR_SHORT_RECORD
, NULL
);
847 /* Master function for unformatted reads. */
850 unformatted_read (st_parameter_dt
*dtp
, bt type
,
851 void *dest
, int kind
, size_t size
, size_t nelems
)
853 if (likely (dtp
->u
.p
.current_unit
->flags
.convert
== GFC_CONVERT_NATIVE
)
856 if (type
== BT_CHARACTER
)
857 size
*= GFC_SIZE_OF_CHAR_KIND(kind
);
858 read_block_direct (dtp
, dest
, size
* nelems
);
868 /* Handle wide chracters. */
869 if (type
== BT_CHARACTER
&& kind
!= 1)
875 /* Break up complex into its constituent reals. */
876 if (type
== BT_COMPLEX
)
882 /* By now, all complex variables have been split into their
883 constituent reals. */
885 for (i
= 0; i
< nelems
; i
++)
887 read_block_direct (dtp
, buffer
, size
);
888 reverse_memcpy (p
, buffer
, size
);
895 /* Master function for unformatted writes. NOTE: For kind=10 the size is 16
896 bytes on 64 bit machines. The unused bytes are not initialized and never
897 used, which can show an error with memory checking analyzers like
901 unformatted_write (st_parameter_dt
*dtp
, bt type
,
902 void *source
, int kind
, size_t size
, size_t nelems
)
904 if (likely (dtp
->u
.p
.current_unit
->flags
.convert
== GFC_CONVERT_NATIVE
)
907 size_t stride
= type
== BT_CHARACTER
?
908 size
* GFC_SIZE_OF_CHAR_KIND(kind
) : size
;
910 write_buf (dtp
, source
, stride
* nelems
);
920 /* Handle wide chracters. */
921 if (type
== BT_CHARACTER
&& kind
!= 1)
927 /* Break up complex into its constituent reals. */
928 if (type
== BT_COMPLEX
)
934 /* By now, all complex variables have been split into their
935 constituent reals. */
937 for (i
= 0; i
< nelems
; i
++)
939 reverse_memcpy(buffer
, p
, size
);
941 write_buf (dtp
, buffer
, size
);
947 /* Return a pointer to the name of a type. */
972 internal_error (NULL
, "type_name(): Bad type");
979 /* Write a constant string to the output.
980 This is complicated because the string can have doubled delimiters
981 in it. The length in the format node is the true length. */
984 write_constant_string (st_parameter_dt
*dtp
, const fnode
*f
)
986 char c
, delimiter
, *p
, *q
;
989 length
= f
->u
.string
.length
;
993 p
= write_block (dtp
, length
);
1000 for (; length
> 0; length
--)
1003 if (c
== delimiter
&& c
!= 'H' && c
!= 'h')
1004 q
++; /* Skip the doubled delimiter. */
1009 /* Given actual and expected types in a formatted data transfer, make
1010 sure they agree. If not, an error message is generated. Returns
1011 nonzero if something went wrong. */
1014 require_type (st_parameter_dt
*dtp
, bt expected
, bt actual
, const fnode
*f
)
1018 if (actual
== expected
)
1021 /* Adjust item_count before emitting error message. */
1022 sprintf (buffer
, "Expected %s for item %d in formatted transfer, got %s",
1023 type_name (expected
), dtp
->u
.p
.item_count
- 1, type_name (actual
));
1025 format_error (dtp
, f
, buffer
);
1030 /* This function is in the main loop for a formatted data transfer
1031 statement. It would be natural to implement this as a coroutine
1032 with the user program, but C makes that awkward. We loop,
1033 processing format elements. When we actually have to transfer
1034 data instead of just setting flags, we return control to the user
1035 program which calls a function that supplies the address and type
1036 of the next element, then comes back here to process it. */
1039 formatted_transfer_scalar_read (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
1042 int pos
, bytes_used
;
1046 int consume_data_flag
;
1048 /* Change a complex data item into a pair of reals. */
1050 n
= (p
== NULL
) ? 0 : ((type
!= BT_COMPLEX
) ? 1 : 2);
1051 if (type
== BT_COMPLEX
)
1057 /* If there's an EOR condition, we simulate finalizing the transfer
1058 by doing nothing. */
1059 if (dtp
->u
.p
.eor_condition
)
1062 /* Set this flag so that commas in reads cause the read to complete before
1063 the entire field has been read. The next read field will start right after
1064 the comma in the stream. (Set to 0 for character reads). */
1065 dtp
->u
.p
.sf_read_comma
=
1066 dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
? 0 : 1;
1070 /* If reversion has occurred and there is another real data item,
1071 then we have to move to the next record. */
1072 if (dtp
->u
.p
.reversion_flag
&& n
> 0)
1074 dtp
->u
.p
.reversion_flag
= 0;
1075 next_record (dtp
, 0);
1078 consume_data_flag
= 1;
1079 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1082 f
= next_format (dtp
);
1085 /* No data descriptors left. */
1086 if (unlikely (n
> 0))
1087 generate_error (&dtp
->common
, LIBERROR_FORMAT
,
1088 "Insufficient data descriptors in format after reversion");
1094 bytes_used
= (int)(dtp
->u
.p
.current_unit
->recl
1095 - dtp
->u
.p
.current_unit
->bytes_left
);
1097 if (is_stream_io(dtp
))
1104 goto need_read_data
;
1105 if (require_type (dtp
, BT_INTEGER
, type
, f
))
1107 read_decimal (dtp
, f
, p
, kind
);
1112 goto need_read_data
;
1113 if (!(compile_options
.allow_std
& GFC_STD_GNU
)
1114 && require_type (dtp
, BT_INTEGER
, type
, f
))
1116 read_radix (dtp
, f
, p
, kind
, 2);
1121 goto need_read_data
;
1122 if (!(compile_options
.allow_std
& GFC_STD_GNU
)
1123 && require_type (dtp
, BT_INTEGER
, type
, f
))
1125 read_radix (dtp
, f
, p
, kind
, 8);
1130 goto need_read_data
;
1131 if (!(compile_options
.allow_std
& GFC_STD_GNU
)
1132 && require_type (dtp
, BT_INTEGER
, type
, f
))
1134 read_radix (dtp
, f
, p
, kind
, 16);
1139 goto need_read_data
;
1141 /* It is possible to have FMT_A with something not BT_CHARACTER such
1142 as when writing out hollerith strings, so check both type
1143 and kind before calling wide character routines. */
1144 if (type
== BT_CHARACTER
&& kind
== 4)
1145 read_a_char4 (dtp
, f
, p
, size
);
1147 read_a (dtp
, f
, p
, size
);
1152 goto need_read_data
;
1153 read_l (dtp
, f
, p
, kind
);
1158 goto need_read_data
;
1159 if (require_type (dtp
, BT_REAL
, type
, f
))
1161 read_f (dtp
, f
, p
, kind
);
1166 goto need_read_data
;
1167 if (require_type (dtp
, BT_REAL
, type
, f
))
1169 read_f (dtp
, f
, p
, kind
);
1174 goto need_read_data
;
1175 if (require_type (dtp
, BT_REAL
, type
, f
))
1177 read_f (dtp
, f
, p
, kind
);
1182 goto need_read_data
;
1183 if (require_type (dtp
, BT_REAL
, type
, f
))
1185 read_f (dtp
, f
, p
, kind
);
1190 goto need_read_data
;
1191 if (require_type (dtp
, BT_REAL
, type
, f
))
1193 read_f (dtp
, f
, p
, kind
);
1198 goto need_read_data
;
1202 read_decimal (dtp
, f
, p
, kind
);
1205 read_l (dtp
, f
, p
, kind
);
1209 read_a_char4 (dtp
, f
, p
, size
);
1211 read_a (dtp
, f
, p
, size
);
1214 read_f (dtp
, f
, p
, kind
);
1217 internal_error (&dtp
->common
, "formatted_transfer(): Bad type");
1222 consume_data_flag
= 0;
1223 format_error (dtp
, f
, "Constant string in input format");
1226 /* Format codes that don't transfer data. */
1229 consume_data_flag
= 0;
1230 dtp
->u
.p
.skips
+= f
->u
.n
;
1231 pos
= bytes_used
+ dtp
->u
.p
.skips
- 1;
1232 dtp
->u
.p
.pending_spaces
= pos
- dtp
->u
.p
.max_pos
+ 1;
1233 read_x (dtp
, f
->u
.n
);
1238 consume_data_flag
= 0;
1240 if (f
->format
== FMT_TL
)
1242 /* Handle the special case when no bytes have been used yet.
1243 Cannot go below zero. */
1244 if (bytes_used
== 0)
1246 dtp
->u
.p
.pending_spaces
-= f
->u
.n
;
1247 dtp
->u
.p
.skips
-= f
->u
.n
;
1248 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
< 0 ? 0 : dtp
->u
.p
.skips
;
1251 pos
= bytes_used
- f
->u
.n
;
1256 /* Standard 10.6.1.1: excessive left tabbing is reset to the
1257 left tab limit. We do not check if the position has gone
1258 beyond the end of record because a subsequent tab could
1259 bring us back again. */
1260 pos
= pos
< 0 ? 0 : pos
;
1262 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
+ pos
- bytes_used
;
1263 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
1264 + pos
- dtp
->u
.p
.max_pos
;
1265 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
< 0
1266 ? 0 : dtp
->u
.p
.pending_spaces
;
1267 if (dtp
->u
.p
.skips
== 0)
1270 /* Adjust everything for end-of-record condition */
1271 if (dtp
->u
.p
.sf_seen_eor
&& !is_internal_unit (dtp
))
1273 dtp
->u
.p
.current_unit
->bytes_left
-= dtp
->u
.p
.sf_seen_eor
;
1274 dtp
->u
.p
.skips
-= dtp
->u
.p
.sf_seen_eor
;
1276 dtp
->u
.p
.sf_seen_eor
= 0;
1278 if (dtp
->u
.p
.skips
< 0)
1280 if (is_internal_unit (dtp
))
1281 sseek (dtp
->u
.p
.current_unit
->s
, dtp
->u
.p
.skips
, SEEK_CUR
);
1283 fbuf_seek (dtp
->u
.p
.current_unit
, dtp
->u
.p
.skips
, SEEK_CUR
);
1284 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) dtp
->u
.p
.skips
;
1285 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1288 read_x (dtp
, dtp
->u
.p
.skips
);
1292 consume_data_flag
= 0;
1293 dtp
->u
.p
.sign_status
= SIGN_S
;
1297 consume_data_flag
= 0;
1298 dtp
->u
.p
.sign_status
= SIGN_SS
;
1302 consume_data_flag
= 0;
1303 dtp
->u
.p
.sign_status
= SIGN_SP
;
1307 consume_data_flag
= 0 ;
1308 dtp
->u
.p
.blank_status
= BLANK_NULL
;
1312 consume_data_flag
= 0;
1313 dtp
->u
.p
.blank_status
= BLANK_ZERO
;
1317 consume_data_flag
= 0;
1318 dtp
->u
.p
.current_unit
->decimal_status
= DECIMAL_COMMA
;
1322 consume_data_flag
= 0;
1323 dtp
->u
.p
.current_unit
->decimal_status
= DECIMAL_POINT
;
1327 consume_data_flag
= 0;
1328 dtp
->u
.p
.current_unit
->round_status
= ROUND_COMPATIBLE
;
1332 consume_data_flag
= 0;
1333 dtp
->u
.p
.current_unit
->round_status
= ROUND_DOWN
;
1337 consume_data_flag
= 0;
1338 dtp
->u
.p
.current_unit
->round_status
= ROUND_NEAREST
;
1342 consume_data_flag
= 0;
1343 dtp
->u
.p
.current_unit
->round_status
= ROUND_PROCDEFINED
;
1347 consume_data_flag
= 0;
1348 dtp
->u
.p
.current_unit
->round_status
= ROUND_UP
;
1352 consume_data_flag
= 0;
1353 dtp
->u
.p
.current_unit
->round_status
= ROUND_ZERO
;
1357 consume_data_flag
= 0;
1358 dtp
->u
.p
.scale_factor
= f
->u
.k
;
1362 consume_data_flag
= 0;
1363 dtp
->u
.p
.seen_dollar
= 1;
1367 consume_data_flag
= 0;
1368 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1369 next_record (dtp
, 0);
1373 /* A colon descriptor causes us to exit this loop (in
1374 particular preventing another / descriptor from being
1375 processed) unless there is another data item to be
1377 consume_data_flag
= 0;
1383 internal_error (&dtp
->common
, "Bad format node");
1386 /* Adjust the item count and data pointer. */
1388 if ((consume_data_flag
> 0) && (n
> 0))
1391 p
= ((char *) p
) + size
;
1396 pos
= (int)(dtp
->u
.p
.current_unit
->recl
- dtp
->u
.p
.current_unit
->bytes_left
);
1397 dtp
->u
.p
.max_pos
= (dtp
->u
.p
.max_pos
> pos
) ? dtp
->u
.p
.max_pos
: pos
;
1402 /* Come here when we need a data descriptor but don't have one. We
1403 push the current format node back onto the input, then return and
1404 let the user program call us back with the data. */
1406 unget_format (dtp
, f
);
1411 formatted_transfer_scalar_write (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
1414 int pos
, bytes_used
;
1418 int consume_data_flag
;
1420 /* Change a complex data item into a pair of reals. */
1422 n
= (p
== NULL
) ? 0 : ((type
!= BT_COMPLEX
) ? 1 : 2);
1423 if (type
== BT_COMPLEX
)
1429 /* If there's an EOR condition, we simulate finalizing the transfer
1430 by doing nothing. */
1431 if (dtp
->u
.p
.eor_condition
)
1434 /* Set this flag so that commas in reads cause the read to complete before
1435 the entire field has been read. The next read field will start right after
1436 the comma in the stream. (Set to 0 for character reads). */
1437 dtp
->u
.p
.sf_read_comma
=
1438 dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
? 0 : 1;
1442 /* If reversion has occurred and there is another real data item,
1443 then we have to move to the next record. */
1444 if (dtp
->u
.p
.reversion_flag
&& n
> 0)
1446 dtp
->u
.p
.reversion_flag
= 0;
1447 next_record (dtp
, 0);
1450 consume_data_flag
= 1;
1451 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1454 f
= next_format (dtp
);
1457 /* No data descriptors left. */
1458 if (unlikely (n
> 0))
1459 generate_error (&dtp
->common
, LIBERROR_FORMAT
,
1460 "Insufficient data descriptors in format after reversion");
1464 /* Now discharge T, TR and X movements to the right. This is delayed
1465 until a data producing format to suppress trailing spaces. */
1468 if (dtp
->u
.p
.mode
== WRITING
&& dtp
->u
.p
.skips
!= 0
1469 && ((n
>0 && ( t
== FMT_I
|| t
== FMT_B
|| t
== FMT_O
1470 || t
== FMT_Z
|| t
== FMT_F
|| t
== FMT_E
1471 || t
== FMT_EN
|| t
== FMT_ES
|| t
== FMT_G
1472 || t
== FMT_L
|| t
== FMT_A
|| t
== FMT_D
))
1473 || t
== FMT_STRING
))
1475 if (dtp
->u
.p
.skips
> 0)
1478 write_x (dtp
, dtp
->u
.p
.skips
, dtp
->u
.p
.pending_spaces
);
1479 tmp
= (int)(dtp
->u
.p
.current_unit
->recl
1480 - dtp
->u
.p
.current_unit
->bytes_left
);
1482 dtp
->u
.p
.max_pos
> tmp
? dtp
->u
.p
.max_pos
: tmp
;
1484 if (dtp
->u
.p
.skips
< 0)
1486 if (is_internal_unit (dtp
))
1487 sseek (dtp
->u
.p
.current_unit
->s
, dtp
->u
.p
.skips
, SEEK_CUR
);
1489 fbuf_seek (dtp
->u
.p
.current_unit
, dtp
->u
.p
.skips
, SEEK_CUR
);
1490 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) dtp
->u
.p
.skips
;
1492 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1495 bytes_used
= (int)(dtp
->u
.p
.current_unit
->recl
1496 - dtp
->u
.p
.current_unit
->bytes_left
);
1498 if (is_stream_io(dtp
))
1506 if (require_type (dtp
, BT_INTEGER
, type
, f
))
1508 write_i (dtp
, f
, p
, kind
);
1514 if (!(compile_options
.allow_std
& GFC_STD_GNU
)
1515 && require_type (dtp
, BT_INTEGER
, type
, f
))
1517 write_b (dtp
, f
, p
, kind
);
1523 if (!(compile_options
.allow_std
& GFC_STD_GNU
)
1524 && require_type (dtp
, BT_INTEGER
, type
, f
))
1526 write_o (dtp
, f
, p
, kind
);
1532 if (!(compile_options
.allow_std
& GFC_STD_GNU
)
1533 && require_type (dtp
, BT_INTEGER
, type
, f
))
1535 write_z (dtp
, f
, p
, kind
);
1542 /* It is possible to have FMT_A with something not BT_CHARACTER such
1543 as when writing out hollerith strings, so check both type
1544 and kind before calling wide character routines. */
1545 if (type
== BT_CHARACTER
&& kind
== 4)
1546 write_a_char4 (dtp
, f
, p
, size
);
1548 write_a (dtp
, f
, p
, size
);
1554 write_l (dtp
, f
, p
, kind
);
1560 if (require_type (dtp
, BT_REAL
, type
, f
))
1562 write_d (dtp
, f
, p
, kind
);
1568 if (require_type (dtp
, BT_REAL
, type
, f
))
1570 write_e (dtp
, f
, p
, kind
);
1576 if (require_type (dtp
, BT_REAL
, type
, f
))
1578 write_en (dtp
, f
, p
, kind
);
1584 if (require_type (dtp
, BT_REAL
, type
, f
))
1586 write_es (dtp
, f
, p
, kind
);
1592 if (require_type (dtp
, BT_REAL
, type
, f
))
1594 write_f (dtp
, f
, p
, kind
);
1603 write_i (dtp
, f
, p
, kind
);
1606 write_l (dtp
, f
, p
, kind
);
1610 write_a_char4 (dtp
, f
, p
, size
);
1612 write_a (dtp
, f
, p
, size
);
1615 if (f
->u
.real
.w
== 0)
1616 write_real_g0 (dtp
, p
, kind
, f
->u
.real
.d
);
1618 write_d (dtp
, f
, p
, kind
);
1621 internal_error (&dtp
->common
,
1622 "formatted_transfer(): Bad type");
1627 consume_data_flag
= 0;
1628 write_constant_string (dtp
, f
);
1631 /* Format codes that don't transfer data. */
1634 consume_data_flag
= 0;
1636 dtp
->u
.p
.skips
+= f
->u
.n
;
1637 pos
= bytes_used
+ dtp
->u
.p
.skips
- 1;
1638 dtp
->u
.p
.pending_spaces
= pos
- dtp
->u
.p
.max_pos
+ 1;
1639 /* Writes occur just before the switch on f->format, above, so
1640 that trailing blanks are suppressed, unless we are doing a
1641 non-advancing write in which case we want to output the blanks
1643 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
)
1645 write_x (dtp
, dtp
->u
.p
.skips
, dtp
->u
.p
.pending_spaces
);
1646 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1652 consume_data_flag
= 0;
1654 if (f
->format
== FMT_TL
)
1657 /* Handle the special case when no bytes have been used yet.
1658 Cannot go below zero. */
1659 if (bytes_used
== 0)
1661 dtp
->u
.p
.pending_spaces
-= f
->u
.n
;
1662 dtp
->u
.p
.skips
-= f
->u
.n
;
1663 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
< 0 ? 0 : dtp
->u
.p
.skips
;
1666 pos
= bytes_used
- f
->u
.n
;
1669 pos
= f
->u
.n
- dtp
->u
.p
.pending_spaces
- 1;
1671 /* Standard 10.6.1.1: excessive left tabbing is reset to the
1672 left tab limit. We do not check if the position has gone
1673 beyond the end of record because a subsequent tab could
1674 bring us back again. */
1675 pos
= pos
< 0 ? 0 : pos
;
1677 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
+ pos
- bytes_used
;
1678 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
1679 + pos
- dtp
->u
.p
.max_pos
;
1680 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
< 0
1681 ? 0 : dtp
->u
.p
.pending_spaces
;
1685 consume_data_flag
= 0;
1686 dtp
->u
.p
.sign_status
= SIGN_S
;
1690 consume_data_flag
= 0;
1691 dtp
->u
.p
.sign_status
= SIGN_SS
;
1695 consume_data_flag
= 0;
1696 dtp
->u
.p
.sign_status
= SIGN_SP
;
1700 consume_data_flag
= 0 ;
1701 dtp
->u
.p
.blank_status
= BLANK_NULL
;
1705 consume_data_flag
= 0;
1706 dtp
->u
.p
.blank_status
= BLANK_ZERO
;
1710 consume_data_flag
= 0;
1711 dtp
->u
.p
.current_unit
->decimal_status
= DECIMAL_COMMA
;
1715 consume_data_flag
= 0;
1716 dtp
->u
.p
.current_unit
->decimal_status
= DECIMAL_POINT
;
1720 consume_data_flag
= 0;
1721 dtp
->u
.p
.current_unit
->round_status
= ROUND_COMPATIBLE
;
1725 consume_data_flag
= 0;
1726 dtp
->u
.p
.current_unit
->round_status
= ROUND_DOWN
;
1730 consume_data_flag
= 0;
1731 dtp
->u
.p
.current_unit
->round_status
= ROUND_NEAREST
;
1735 consume_data_flag
= 0;
1736 dtp
->u
.p
.current_unit
->round_status
= ROUND_PROCDEFINED
;
1740 consume_data_flag
= 0;
1741 dtp
->u
.p
.current_unit
->round_status
= ROUND_UP
;
1745 consume_data_flag
= 0;
1746 dtp
->u
.p
.current_unit
->round_status
= ROUND_ZERO
;
1750 consume_data_flag
= 0;
1751 dtp
->u
.p
.scale_factor
= f
->u
.k
;
1755 consume_data_flag
= 0;
1756 dtp
->u
.p
.seen_dollar
= 1;
1760 consume_data_flag
= 0;
1761 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1762 next_record (dtp
, 0);
1766 /* A colon descriptor causes us to exit this loop (in
1767 particular preventing another / descriptor from being
1768 processed) unless there is another data item to be
1770 consume_data_flag
= 0;
1776 internal_error (&dtp
->common
, "Bad format node");
1779 /* Adjust the item count and data pointer. */
1781 if ((consume_data_flag
> 0) && (n
> 0))
1784 p
= ((char *) p
) + size
;
1787 pos
= (int)(dtp
->u
.p
.current_unit
->recl
- dtp
->u
.p
.current_unit
->bytes_left
);
1788 dtp
->u
.p
.max_pos
= (dtp
->u
.p
.max_pos
> pos
) ? dtp
->u
.p
.max_pos
: pos
;
1793 /* Come here when we need a data descriptor but don't have one. We
1794 push the current format node back onto the input, then return and
1795 let the user program call us back with the data. */
1797 unget_format (dtp
, f
);
1800 /* This function is first called from data_init_transfer to initiate the loop
1801 over each item in the format, transferring data as required. Subsequent
1802 calls to this function occur for each data item foound in the READ/WRITE
1803 statement. The item_count is incremented for each call. Since the first
1804 call is from data_transfer_init, the item_count is always one greater than
1805 the actual count number of the item being transferred. */
1808 formatted_transfer (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
1809 size_t size
, size_t nelems
)
1815 size_t stride
= type
== BT_CHARACTER
?
1816 size
* GFC_SIZE_OF_CHAR_KIND(kind
) : size
;
1817 if (dtp
->u
.p
.mode
== READING
)
1819 /* Big loop over all the elements. */
1820 for (elem
= 0; elem
< nelems
; elem
++)
1822 dtp
->u
.p
.item_count
++;
1823 formatted_transfer_scalar_read (dtp
, type
, tmp
+ stride
*elem
, kind
, size
);
1828 /* Big loop over all the elements. */
1829 for (elem
= 0; elem
< nelems
; elem
++)
1831 dtp
->u
.p
.item_count
++;
1832 formatted_transfer_scalar_write (dtp
, type
, tmp
+ stride
*elem
, kind
, size
);
1838 /* Data transfer entry points. The type of the data entity is
1839 implicit in the subroutine call. This prevents us from having to
1840 share a common enum with the compiler. */
1843 transfer_integer (st_parameter_dt
*dtp
, void *p
, int kind
)
1845 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1847 dtp
->u
.p
.transfer (dtp
, BT_INTEGER
, p
, kind
, kind
, 1);
1852 transfer_real (st_parameter_dt
*dtp
, void *p
, int kind
)
1855 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1857 size
= size_from_real_kind (kind
);
1858 dtp
->u
.p
.transfer (dtp
, BT_REAL
, p
, kind
, size
, 1);
1863 transfer_logical (st_parameter_dt
*dtp
, void *p
, int kind
)
1865 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1867 dtp
->u
.p
.transfer (dtp
, BT_LOGICAL
, p
, kind
, kind
, 1);
1872 transfer_character (st_parameter_dt
*dtp
, void *p
, int len
)
1874 static char *empty_string
[0];
1876 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1879 /* Strings of zero length can have p == NULL, which confuses the
1880 transfer routines into thinking we need more data elements. To avoid
1881 this, we give them a nice pointer. */
1882 if (len
== 0 && p
== NULL
)
1885 /* Set kind here to 1. */
1886 dtp
->u
.p
.transfer (dtp
, BT_CHARACTER
, p
, 1, len
, 1);
1890 transfer_character_wide (st_parameter_dt
*dtp
, void *p
, int len
, int kind
)
1892 static char *empty_string
[0];
1894 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1897 /* Strings of zero length can have p == NULL, which confuses the
1898 transfer routines into thinking we need more data elements. To avoid
1899 this, we give them a nice pointer. */
1900 if (len
== 0 && p
== NULL
)
1903 /* Here we pass the actual kind value. */
1904 dtp
->u
.p
.transfer (dtp
, BT_CHARACTER
, p
, kind
, len
, 1);
1909 transfer_complex (st_parameter_dt
*dtp
, void *p
, int kind
)
1912 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1914 size
= size_from_complex_kind (kind
);
1915 dtp
->u
.p
.transfer (dtp
, BT_COMPLEX
, p
, kind
, size
, 1);
1920 transfer_array (st_parameter_dt
*dtp
, gfc_array_char
*desc
, int kind
,
1921 gfc_charlen_type charlen
)
1923 index_type count
[GFC_MAX_DIMENSIONS
];
1924 index_type extent
[GFC_MAX_DIMENSIONS
];
1925 index_type stride
[GFC_MAX_DIMENSIONS
];
1926 index_type stride0
, rank
, size
, type
, n
;
1931 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1934 type
= GFC_DESCRIPTOR_TYPE (desc
);
1935 size
= GFC_DESCRIPTOR_SIZE (desc
);
1937 /* FIXME: What a kludge: Array descriptors and the IO library use
1938 different enums for types. */
1941 case GFC_DTYPE_UNKNOWN
:
1942 iotype
= BT_NULL
; /* Is this correct? */
1944 case GFC_DTYPE_INTEGER
:
1945 iotype
= BT_INTEGER
;
1947 case GFC_DTYPE_LOGICAL
:
1948 iotype
= BT_LOGICAL
;
1950 case GFC_DTYPE_REAL
:
1953 case GFC_DTYPE_COMPLEX
:
1954 iotype
= BT_COMPLEX
;
1956 case GFC_DTYPE_CHARACTER
:
1957 iotype
= BT_CHARACTER
;
1960 case GFC_DTYPE_DERIVED
:
1961 internal_error (&dtp
->common
,
1962 "Derived type I/O should have been handled via the frontend.");
1965 internal_error (&dtp
->common
, "transfer_array(): Bad type");
1968 rank
= GFC_DESCRIPTOR_RANK (desc
);
1969 for (n
= 0; n
< rank
; n
++)
1972 stride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(desc
,n
);
1973 extent
[n
] = GFC_DESCRIPTOR_EXTENT(desc
,n
);
1975 /* If the extent of even one dimension is zero, then the entire
1976 array section contains zero elements, so we return after writing
1977 a zero array record. */
1982 dtp
->u
.p
.transfer (dtp
, iotype
, data
, kind
, size
, tsize
);
1987 stride0
= stride
[0];
1989 /* If the innermost dimension has a stride of 1, we can do the transfer
1990 in contiguous chunks. */
1991 if (stride0
== size
)
1996 data
= GFC_DESCRIPTOR_DATA (desc
);
2000 dtp
->u
.p
.transfer (dtp
, iotype
, data
, kind
, size
, tsize
);
2001 data
+= stride0
* tsize
;
2004 while (count
[n
] == extent
[n
])
2007 data
-= stride
[n
] * extent
[n
];
2024 /* Preposition a sequential unformatted file while reading. */
2027 us_read (st_parameter_dt
*dtp
, int continued
)
2034 if (compile_options
.record_marker
== 0)
2035 n
= sizeof (GFC_INTEGER_4
);
2037 n
= compile_options
.record_marker
;
2039 nr
= sread (dtp
->u
.p
.current_unit
->s
, &i
, n
);
2040 if (unlikely (nr
< 0))
2042 generate_error (&dtp
->common
, LIBERROR_BAD_US
, NULL
);
2048 return; /* end of file */
2050 else if (unlikely (n
!= nr
))
2052 generate_error (&dtp
->common
, LIBERROR_BAD_US
, NULL
);
2056 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
2057 if (likely (dtp
->u
.p
.current_unit
->flags
.convert
== GFC_CONVERT_NATIVE
))
2061 case sizeof(GFC_INTEGER_4
):
2062 memcpy (&i4
, &i
, sizeof (i4
));
2066 case sizeof(GFC_INTEGER_8
):
2067 memcpy (&i8
, &i
, sizeof (i8
));
2072 runtime_error ("Illegal value for record marker");
2079 case sizeof(GFC_INTEGER_4
):
2080 reverse_memcpy (&i4
, &i
, sizeof (i4
));
2084 case sizeof(GFC_INTEGER_8
):
2085 reverse_memcpy (&i8
, &i
, sizeof (i8
));
2090 runtime_error ("Illegal value for record marker");
2096 dtp
->u
.p
.current_unit
->bytes_left_subrecord
= i
;
2097 dtp
->u
.p
.current_unit
->continued
= 0;
2101 dtp
->u
.p
.current_unit
->bytes_left_subrecord
= -i
;
2102 dtp
->u
.p
.current_unit
->continued
= 1;
2106 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2110 /* Preposition a sequential unformatted file while writing. This
2111 amount to writing a bogus length that will be filled in later. */
2114 us_write (st_parameter_dt
*dtp
, int continued
)
2121 if (compile_options
.record_marker
== 0)
2122 nbytes
= sizeof (GFC_INTEGER_4
);
2124 nbytes
= compile_options
.record_marker
;
2126 if (swrite (dtp
->u
.p
.current_unit
->s
, &dummy
, nbytes
) != nbytes
)
2127 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2129 /* For sequential unformatted, if RECL= was not specified in the OPEN
2130 we write until we have more bytes than can fit in the subrecord
2131 markers, then we write a new subrecord. */
2133 dtp
->u
.p
.current_unit
->bytes_left_subrecord
=
2134 dtp
->u
.p
.current_unit
->recl_subrecord
;
2135 dtp
->u
.p
.current_unit
->continued
= continued
;
2139 /* Position to the next record prior to transfer. We are assumed to
2140 be before the next record. We also calculate the bytes in the next
2144 pre_position (st_parameter_dt
*dtp
)
2146 if (dtp
->u
.p
.current_unit
->current_record
)
2147 return; /* Already positioned. */
2149 switch (current_mode (dtp
))
2151 case FORMATTED_STREAM
:
2152 case UNFORMATTED_STREAM
:
2153 /* There are no records with stream I/O. If the position was specified
2154 data_transfer_init has already positioned the file. If no position
2155 was specified, we continue from where we last left off. I.e.
2156 there is nothing to do here. */
2159 case UNFORMATTED_SEQUENTIAL
:
2160 if (dtp
->u
.p
.mode
== READING
)
2167 case FORMATTED_SEQUENTIAL
:
2168 case FORMATTED_DIRECT
:
2169 case UNFORMATTED_DIRECT
:
2170 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2174 dtp
->u
.p
.current_unit
->current_record
= 1;
2178 /* Initialize things for a data transfer. This code is common for
2179 both reading and writing. */
2182 data_transfer_init (st_parameter_dt
*dtp
, int read_flag
)
2184 unit_flags u_flags
; /* Used for creating a unit if needed. */
2185 GFC_INTEGER_4 cf
= dtp
->common
.flags
;
2186 namelist_info
*ionml
;
2188 ionml
= ((cf
& IOPARM_DT_IONML_SET
) != 0) ? dtp
->u
.p
.ionml
: NULL
;
2190 memset (&dtp
->u
.p
, 0, sizeof (dtp
->u
.p
));
2192 dtp
->u
.p
.ionml
= ionml
;
2193 dtp
->u
.p
.mode
= read_flag
? READING
: WRITING
;
2195 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
2198 if ((cf
& IOPARM_DT_HAS_SIZE
) != 0)
2199 dtp
->u
.p
.size_used
= 0; /* Initialize the count. */
2201 dtp
->u
.p
.current_unit
= get_unit (dtp
, 1);
2202 if (dtp
->u
.p
.current_unit
->s
== NULL
)
2203 { /* Open the unit with some default flags. */
2204 st_parameter_open opp
;
2207 if (dtp
->common
.unit
< 0)
2209 close_unit (dtp
->u
.p
.current_unit
);
2210 dtp
->u
.p
.current_unit
= NULL
;
2211 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2212 "Bad unit number in statement");
2215 memset (&u_flags
, '\0', sizeof (u_flags
));
2216 u_flags
.access
= ACCESS_SEQUENTIAL
;
2217 u_flags
.action
= ACTION_READWRITE
;
2219 /* Is it unformatted? */
2220 if (!(cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
2221 | IOPARM_DT_IONML_SET
)))
2222 u_flags
.form
= FORM_UNFORMATTED
;
2224 u_flags
.form
= FORM_UNSPECIFIED
;
2226 u_flags
.delim
= DELIM_UNSPECIFIED
;
2227 u_flags
.blank
= BLANK_UNSPECIFIED
;
2228 u_flags
.pad
= PAD_UNSPECIFIED
;
2229 u_flags
.decimal
= DECIMAL_UNSPECIFIED
;
2230 u_flags
.encoding
= ENCODING_UNSPECIFIED
;
2231 u_flags
.async
= ASYNC_UNSPECIFIED
;
2232 u_flags
.round
= ROUND_UNSPECIFIED
;
2233 u_flags
.sign
= SIGN_UNSPECIFIED
;
2235 u_flags
.status
= STATUS_UNKNOWN
;
2237 conv
= get_unformatted_convert (dtp
->common
.unit
);
2239 if (conv
== GFC_CONVERT_NONE
)
2240 conv
= compile_options
.convert
;
2242 /* We use big_endian, which is 0 on little-endian machines
2243 and 1 on big-endian machines. */
2246 case GFC_CONVERT_NATIVE
:
2247 case GFC_CONVERT_SWAP
:
2250 case GFC_CONVERT_BIG
:
2251 conv
= big_endian
? GFC_CONVERT_NATIVE
: GFC_CONVERT_SWAP
;
2254 case GFC_CONVERT_LITTLE
:
2255 conv
= big_endian
? GFC_CONVERT_SWAP
: GFC_CONVERT_NATIVE
;
2259 internal_error (&opp
.common
, "Illegal value for CONVERT");
2263 u_flags
.convert
= conv
;
2265 opp
.common
= dtp
->common
;
2266 opp
.common
.flags
&= IOPARM_COMMON_MASK
;
2267 dtp
->u
.p
.current_unit
= new_unit (&opp
, dtp
->u
.p
.current_unit
, &u_flags
);
2268 dtp
->common
.flags
&= ~IOPARM_COMMON_MASK
;
2269 dtp
->common
.flags
|= (opp
.common
.flags
& IOPARM_COMMON_MASK
);
2270 if (dtp
->u
.p
.current_unit
== NULL
)
2274 /* Check the action. */
2276 if (read_flag
&& dtp
->u
.p
.current_unit
->flags
.action
== ACTION_WRITE
)
2278 generate_error (&dtp
->common
, LIBERROR_BAD_ACTION
,
2279 "Cannot read from file opened for WRITE");
2283 if (!read_flag
&& dtp
->u
.p
.current_unit
->flags
.action
== ACTION_READ
)
2285 generate_error (&dtp
->common
, LIBERROR_BAD_ACTION
,
2286 "Cannot write to file opened for READ");
2290 dtp
->u
.p
.first_item
= 1;
2292 /* Check the format. */
2294 if ((cf
& IOPARM_DT_HAS_FORMAT
) != 0)
2297 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
2298 && (cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
))
2301 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2302 "Format present for UNFORMATTED data transfer");
2306 if ((cf
& IOPARM_DT_HAS_NAMELIST_NAME
) != 0 && dtp
->u
.p
.ionml
!= NULL
)
2308 if ((cf
& IOPARM_DT_HAS_FORMAT
) != 0)
2309 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2310 "A format cannot be specified with a namelist");
2312 else if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
&&
2313 !(cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
)))
2315 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2316 "Missing format for FORMATTED data transfer");
2319 if (is_internal_unit (dtp
)
2320 && dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
)
2322 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2323 "Internal file cannot be accessed by UNFORMATTED "
2328 /* Check the record or position number. */
2330 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
2331 && (cf
& IOPARM_DT_HAS_REC
) == 0)
2333 generate_error (&dtp
->common
, LIBERROR_MISSING_OPTION
,
2334 "Direct access data transfer requires record number");
2338 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
2340 if ((cf
& IOPARM_DT_HAS_REC
) != 0)
2342 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2343 "Record number not allowed for sequential access "
2348 if (dtp
->u
.p
.current_unit
->endfile
== AFTER_ENDFILE
)
2350 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2351 "Sequential READ or WRITE not allowed after "
2352 "EOF marker, possibly use REWIND or BACKSPACE");
2357 /* Process the ADVANCE option. */
2359 dtp
->u
.p
.advance_status
2360 = !(cf
& IOPARM_DT_HAS_ADVANCE
) ? ADVANCE_UNSPECIFIED
:
2361 find_option (&dtp
->common
, dtp
->advance
, dtp
->advance_len
, advance_opt
,
2362 "Bad ADVANCE parameter in data transfer statement");
2364 if (dtp
->u
.p
.advance_status
!= ADVANCE_UNSPECIFIED
)
2366 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
2368 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2369 "ADVANCE specification conflicts with sequential "
2374 if (is_internal_unit (dtp
))
2376 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2377 "ADVANCE specification conflicts with internal file");
2381 if ((cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
))
2382 != IOPARM_DT_HAS_FORMAT
)
2384 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2385 "ADVANCE specification requires an explicit format");
2392 dtp
->u
.p
.current_unit
->previous_nonadvancing_write
= 0;
2394 if ((cf
& IOPARM_EOR
) != 0 && dtp
->u
.p
.advance_status
!= ADVANCE_NO
)
2396 generate_error (&dtp
->common
, LIBERROR_MISSING_OPTION
,
2397 "EOR specification requires an ADVANCE specification "
2402 if ((cf
& IOPARM_DT_HAS_SIZE
) != 0
2403 && dtp
->u
.p
.advance_status
!= ADVANCE_NO
)
2405 generate_error (&dtp
->common
, LIBERROR_MISSING_OPTION
,
2406 "SIZE specification requires an ADVANCE "
2407 "specification of NO");
2412 { /* Write constraints. */
2413 if ((cf
& IOPARM_END
) != 0)
2415 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2416 "END specification cannot appear in a write "
2421 if ((cf
& IOPARM_EOR
) != 0)
2423 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2424 "EOR specification cannot appear in a write "
2429 if ((cf
& IOPARM_DT_HAS_SIZE
) != 0)
2431 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2432 "SIZE specification cannot appear in a write "
2438 if (dtp
->u
.p
.advance_status
== ADVANCE_UNSPECIFIED
)
2439 dtp
->u
.p
.advance_status
= ADVANCE_YES
;
2441 /* Check the decimal mode. */
2442 dtp
->u
.p
.current_unit
->decimal_status
2443 = !(cf
& IOPARM_DT_HAS_DECIMAL
) ? DECIMAL_UNSPECIFIED
:
2444 find_option (&dtp
->common
, dtp
->decimal
, dtp
->decimal_len
,
2445 decimal_opt
, "Bad DECIMAL parameter in data transfer "
2448 if (dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_UNSPECIFIED
)
2449 dtp
->u
.p
.current_unit
->decimal_status
= dtp
->u
.p
.current_unit
->flags
.decimal
;
2451 /* Check the round mode. */
2452 dtp
->u
.p
.current_unit
->round_status
2453 = !(cf
& IOPARM_DT_HAS_ROUND
) ? ROUND_UNSPECIFIED
:
2454 find_option (&dtp
->common
, dtp
->round
, dtp
->round_len
,
2455 round_opt
, "Bad ROUND parameter in data transfer "
2458 if (dtp
->u
.p
.current_unit
->round_status
== ROUND_UNSPECIFIED
)
2459 dtp
->u
.p
.current_unit
->round_status
= dtp
->u
.p
.current_unit
->flags
.round
;
2461 /* Check the sign mode. */
2462 dtp
->u
.p
.sign_status
2463 = !(cf
& IOPARM_DT_HAS_SIGN
) ? SIGN_UNSPECIFIED
:
2464 find_option (&dtp
->common
, dtp
->sign
, dtp
->sign_len
, sign_opt
,
2465 "Bad SIGN parameter in data transfer statement");
2467 if (dtp
->u
.p
.sign_status
== SIGN_UNSPECIFIED
)
2468 dtp
->u
.p
.sign_status
= dtp
->u
.p
.current_unit
->flags
.sign
;
2470 /* Check the blank mode. */
2471 dtp
->u
.p
.blank_status
2472 = !(cf
& IOPARM_DT_HAS_BLANK
) ? BLANK_UNSPECIFIED
:
2473 find_option (&dtp
->common
, dtp
->blank
, dtp
->blank_len
,
2475 "Bad BLANK parameter in data transfer statement");
2477 if (dtp
->u
.p
.blank_status
== BLANK_UNSPECIFIED
)
2478 dtp
->u
.p
.blank_status
= dtp
->u
.p
.current_unit
->flags
.blank
;
2480 /* Check the delim mode. */
2481 dtp
->u
.p
.current_unit
->delim_status
2482 = !(cf
& IOPARM_DT_HAS_DELIM
) ? DELIM_UNSPECIFIED
:
2483 find_option (&dtp
->common
, dtp
->delim
, dtp
->delim_len
,
2484 delim_opt
, "Bad DELIM parameter in data transfer statement");
2486 if (dtp
->u
.p
.current_unit
->delim_status
== DELIM_UNSPECIFIED
)
2487 dtp
->u
.p
.current_unit
->delim_status
= dtp
->u
.p
.current_unit
->flags
.delim
;
2489 /* Check the pad mode. */
2490 dtp
->u
.p
.current_unit
->pad_status
2491 = !(cf
& IOPARM_DT_HAS_PAD
) ? PAD_UNSPECIFIED
:
2492 find_option (&dtp
->common
, dtp
->pad
, dtp
->pad_len
, pad_opt
,
2493 "Bad PAD parameter in data transfer statement");
2495 if (dtp
->u
.p
.current_unit
->pad_status
== PAD_UNSPECIFIED
)
2496 dtp
->u
.p
.current_unit
->pad_status
= dtp
->u
.p
.current_unit
->flags
.pad
;
2498 /* Check to see if we might be reading what we wrote before */
2500 if (dtp
->u
.p
.mode
!= dtp
->u
.p
.current_unit
->mode
2501 && !is_internal_unit (dtp
))
2503 int pos
= fbuf_reset (dtp
->u
.p
.current_unit
);
2505 sseek (dtp
->u
.p
.current_unit
->s
, pos
, SEEK_CUR
);
2506 sflush(dtp
->u
.p
.current_unit
->s
);
2509 /* Check the POS= specifier: that it is in range and that it is used with a
2510 unit that has been connected for STREAM access. F2003 9.5.1.10. */
2512 if (((cf
& IOPARM_DT_HAS_POS
) != 0))
2514 if (is_stream_io (dtp
))
2519 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2520 "POS=specifier must be positive");
2524 if (dtp
->pos
>= dtp
->u
.p
.current_unit
->maxrec
)
2526 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2527 "POS=specifier too large");
2531 dtp
->rec
= dtp
->pos
;
2533 if (dtp
->u
.p
.mode
== READING
)
2535 /* Reset the endfile flag; if we hit EOF during reading
2536 we'll set the flag and generate an error at that point
2537 rather than worrying about it here. */
2538 dtp
->u
.p
.current_unit
->endfile
= NO_ENDFILE
;
2541 if (dtp
->pos
!= dtp
->u
.p
.current_unit
->strm_pos
)
2543 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
2544 if (sseek (dtp
->u
.p
.current_unit
->s
, dtp
->pos
- 1, SEEK_SET
) < 0)
2546 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2549 dtp
->u
.p
.current_unit
->strm_pos
= dtp
->pos
;
2554 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2555 "POS=specifier not allowed, "
2556 "Try OPEN with ACCESS='stream'");
2562 /* Sanity checks on the record number. */
2563 if ((cf
& IOPARM_DT_HAS_REC
) != 0)
2567 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2568 "Record number must be positive");
2572 if (dtp
->rec
>= dtp
->u
.p
.current_unit
->maxrec
)
2574 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2575 "Record number too large");
2579 /* Make sure format buffer is reset. */
2580 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
)
2581 fbuf_reset (dtp
->u
.p
.current_unit
);
2584 /* Check whether the record exists to be read. Only
2585 a partial record needs to exist. */
2587 if (dtp
->u
.p
.mode
== READING
&& (dtp
->rec
- 1)
2588 * dtp
->u
.p
.current_unit
->recl
>= file_length (dtp
->u
.p
.current_unit
->s
))
2590 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2591 "Non-existing record number");
2595 /* Position the file. */
2596 if (sseek (dtp
->u
.p
.current_unit
->s
, (gfc_offset
) (dtp
->rec
- 1)
2597 * dtp
->u
.p
.current_unit
->recl
, SEEK_SET
) < 0)
2599 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2603 /* TODO: This is required to maintain compatibility between
2604 4.3 and 4.4 runtime. Remove when ABI changes from 4.3 */
2606 if (is_stream_io (dtp
))
2607 dtp
->u
.p
.current_unit
->strm_pos
= dtp
->rec
;
2609 /* TODO: Un-comment this code when ABI changes from 4.3.
2610 if (dtp->u.p.current_unit->flags.access == ACCESS_STREAM)
2612 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2613 "Record number not allowed for stream access "
2619 /* Bugware for badly written mixed C-Fortran I/O. */
2620 flush_if_preconnected(dtp
->u
.p
.current_unit
->s
);
2622 dtp
->u
.p
.current_unit
->mode
= dtp
->u
.p
.mode
;
2624 /* Set the maximum position reached from the previous I/O operation. This
2625 could be greater than zero from a previous non-advancing write. */
2626 dtp
->u
.p
.max_pos
= dtp
->u
.p
.current_unit
->saved_pos
;
2631 /* Set up the subroutine that will handle the transfers. */
2635 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
)
2636 dtp
->u
.p
.transfer
= unformatted_read
;
2639 if ((cf
& IOPARM_DT_LIST_FORMAT
) != 0)
2640 dtp
->u
.p
.transfer
= list_formatted_read
;
2642 dtp
->u
.p
.transfer
= formatted_transfer
;
2647 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
)
2648 dtp
->u
.p
.transfer
= unformatted_write
;
2651 if ((cf
& IOPARM_DT_LIST_FORMAT
) != 0)
2652 dtp
->u
.p
.transfer
= list_formatted_write
;
2654 dtp
->u
.p
.transfer
= formatted_transfer
;
2658 /* Make sure that we don't do a read after a nonadvancing write. */
2662 if (dtp
->u
.p
.current_unit
->read_bad
&& !is_stream_io (dtp
))
2664 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2665 "Cannot READ after a nonadvancing WRITE");
2671 if (dtp
->u
.p
.advance_status
== ADVANCE_YES
&& !dtp
->u
.p
.seen_dollar
)
2672 dtp
->u
.p
.current_unit
->read_bad
= 1;
2675 /* Start the data transfer if we are doing a formatted transfer. */
2676 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
2677 && ((cf
& (IOPARM_DT_LIST_FORMAT
| IOPARM_DT_HAS_NAMELIST_NAME
)) == 0)
2678 && dtp
->u
.p
.ionml
== NULL
)
2679 formatted_transfer (dtp
, 0, NULL
, 0, 0, 1);
2682 /* Initialize an array_loop_spec given the array descriptor. The function
2683 returns the index of the last element of the array, and also returns
2684 starting record, where the first I/O goes to (necessary in case of
2685 negative strides). */
2688 init_loop_spec (gfc_array_char
*desc
, array_loop_spec
*ls
,
2689 gfc_offset
*start_record
)
2691 int rank
= GFC_DESCRIPTOR_RANK(desc
);
2700 for (i
=0; i
<rank
; i
++)
2702 ls
[i
].idx
= GFC_DESCRIPTOR_LBOUND(desc
,i
);
2703 ls
[i
].start
= GFC_DESCRIPTOR_LBOUND(desc
,i
);
2704 ls
[i
].end
= GFC_DESCRIPTOR_UBOUND(desc
,i
);
2705 ls
[i
].step
= GFC_DESCRIPTOR_STRIDE(desc
,i
);
2706 empty
= empty
|| (GFC_DESCRIPTOR_UBOUND(desc
,i
)
2707 < GFC_DESCRIPTOR_LBOUND(desc
,i
));
2709 if (GFC_DESCRIPTOR_STRIDE(desc
,i
) > 0)
2711 index
+= (GFC_DESCRIPTOR_EXTENT(desc
,i
) - 1)
2712 * GFC_DESCRIPTOR_STRIDE(desc
,i
);
2716 index
-= (GFC_DESCRIPTOR_EXTENT(desc
,i
) - 1)
2717 * GFC_DESCRIPTOR_STRIDE(desc
,i
);
2718 *start_record
-= (GFC_DESCRIPTOR_EXTENT(desc
,i
) - 1)
2719 * GFC_DESCRIPTOR_STRIDE(desc
,i
);
2729 /* Determine the index to the next record in an internal unit array by
2730 by incrementing through the array_loop_spec. */
2733 next_array_record (st_parameter_dt
*dtp
, array_loop_spec
*ls
, int *finished
)
2741 for (i
= 0; i
< dtp
->u
.p
.current_unit
->rank
; i
++)
2746 if (ls
[i
].idx
> ls
[i
].end
)
2748 ls
[i
].idx
= ls
[i
].start
;
2754 index
= index
+ (ls
[i
].idx
- ls
[i
].start
) * ls
[i
].step
;
2764 /* Skip to the end of the current record, taking care of an optional
2765 record marker of size bytes. If the file is not seekable, we
2766 read chunks of size MAX_READ until we get to the right
2770 skip_record (st_parameter_dt
*dtp
, ssize_t bytes
)
2772 ssize_t rlength
, readb
;
2773 static const ssize_t MAX_READ
= 4096;
2776 dtp
->u
.p
.current_unit
->bytes_left_subrecord
+= bytes
;
2777 if (dtp
->u
.p
.current_unit
->bytes_left_subrecord
== 0)
2780 if (is_seekable (dtp
->u
.p
.current_unit
->s
))
2782 /* Direct access files do not generate END conditions,
2784 if (sseek (dtp
->u
.p
.current_unit
->s
,
2785 dtp
->u
.p
.current_unit
->bytes_left_subrecord
, SEEK_CUR
) < 0)
2786 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2788 dtp
->u
.p
.current_unit
->bytes_left_subrecord
= 0;
2791 { /* Seek by reading data. */
2792 while (dtp
->u
.p
.current_unit
->bytes_left_subrecord
> 0)
2795 (MAX_READ
< dtp
->u
.p
.current_unit
->bytes_left_subrecord
) ?
2796 MAX_READ
: dtp
->u
.p
.current_unit
->bytes_left_subrecord
;
2798 readb
= sread (dtp
->u
.p
.current_unit
->s
, p
, rlength
);
2801 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2805 dtp
->u
.p
.current_unit
->bytes_left_subrecord
-= readb
;
2812 /* Advance to the next record reading unformatted files, taking
2813 care of subrecords. If complete_record is nonzero, we loop
2814 until all subrecords are cleared. */
2817 next_record_r_unf (st_parameter_dt
*dtp
, int complete_record
)
2821 bytes
= compile_options
.record_marker
== 0 ?
2822 sizeof (GFC_INTEGER_4
) : compile_options
.record_marker
;
2827 /* Skip over tail */
2829 skip_record (dtp
, bytes
);
2831 if ( ! (complete_record
&& dtp
->u
.p
.current_unit
->continued
))
2839 static inline gfc_offset
2840 min_off (gfc_offset a
, gfc_offset b
)
2842 return (a
< b
? a
: b
);
2846 /* Space to the next record for read mode. */
2849 next_record_r (st_parameter_dt
*dtp
, int done
)
2856 switch (current_mode (dtp
))
2858 /* No records in unformatted STREAM I/O. */
2859 case UNFORMATTED_STREAM
:
2862 case UNFORMATTED_SEQUENTIAL
:
2863 next_record_r_unf (dtp
, 1);
2864 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2867 case FORMATTED_DIRECT
:
2868 case UNFORMATTED_DIRECT
:
2869 skip_record (dtp
, dtp
->u
.p
.current_unit
->bytes_left
);
2872 case FORMATTED_STREAM
:
2873 case FORMATTED_SEQUENTIAL
:
2874 /* read_sf has already terminated input because of an '\n', or
2876 if (dtp
->u
.p
.sf_seen_eor
)
2878 dtp
->u
.p
.sf_seen_eor
= 0;
2882 if (is_internal_unit (dtp
))
2884 if (is_array_io (dtp
))
2888 record
= next_array_record (dtp
, dtp
->u
.p
.current_unit
->ls
,
2890 if (!done
&& finished
)
2893 /* Now seek to this record. */
2894 record
= record
* dtp
->u
.p
.current_unit
->recl
;
2895 if (sseek (dtp
->u
.p
.current_unit
->s
, record
, SEEK_SET
) < 0)
2897 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
2900 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2904 bytes_left
= (int) dtp
->u
.p
.current_unit
->bytes_left
;
2905 bytes_left
= min_off (bytes_left
,
2906 file_length (dtp
->u
.p
.current_unit
->s
)
2907 - stell (dtp
->u
.p
.current_unit
->s
));
2908 if (sseek (dtp
->u
.p
.current_unit
->s
,
2909 bytes_left
, SEEK_CUR
) < 0)
2911 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
2914 dtp
->u
.p
.current_unit
->bytes_left
2915 = dtp
->u
.p
.current_unit
->recl
;
2924 cc
= fbuf_getc (dtp
->u
.p
.current_unit
);
2928 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2931 if (is_stream_io (dtp
)
2932 || dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
2933 || dtp
->u
.p
.current_unit
->bytes_left
2934 == dtp
->u
.p
.current_unit
->recl
)
2940 if (is_stream_io (dtp
))
2941 dtp
->u
.p
.current_unit
->strm_pos
++;
2952 /* Small utility function to write a record marker, taking care of
2953 byte swapping and of choosing the correct size. */
2956 write_us_marker (st_parameter_dt
*dtp
, const gfc_offset buf
)
2961 char p
[sizeof (GFC_INTEGER_8
)];
2963 if (compile_options
.record_marker
== 0)
2964 len
= sizeof (GFC_INTEGER_4
);
2966 len
= compile_options
.record_marker
;
2968 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
2969 if (likely (dtp
->u
.p
.current_unit
->flags
.convert
== GFC_CONVERT_NATIVE
))
2973 case sizeof (GFC_INTEGER_4
):
2975 return swrite (dtp
->u
.p
.current_unit
->s
, &buf4
, len
);
2978 case sizeof (GFC_INTEGER_8
):
2980 return swrite (dtp
->u
.p
.current_unit
->s
, &buf8
, len
);
2984 runtime_error ("Illegal value for record marker");
2992 case sizeof (GFC_INTEGER_4
):
2994 reverse_memcpy (p
, &buf4
, sizeof (GFC_INTEGER_4
));
2995 return swrite (dtp
->u
.p
.current_unit
->s
, p
, len
);
2998 case sizeof (GFC_INTEGER_8
):
3000 reverse_memcpy (p
, &buf8
, sizeof (GFC_INTEGER_8
));
3001 return swrite (dtp
->u
.p
.current_unit
->s
, p
, len
);
3005 runtime_error ("Illegal value for record marker");
3012 /* Position to the next (sub)record in write mode for
3013 unformatted sequential files. */
3016 next_record_w_unf (st_parameter_dt
*dtp
, int next_subrecord
)
3018 gfc_offset m
, m_write
, record_marker
;
3020 /* Bytes written. */
3021 m
= dtp
->u
.p
.current_unit
->recl_subrecord
3022 - dtp
->u
.p
.current_unit
->bytes_left_subrecord
;
3024 /* Write the length tail. If we finish a record containing
3025 subrecords, we write out the negative length. */
3027 if (dtp
->u
.p
.current_unit
->continued
)
3032 if (unlikely (write_us_marker (dtp
, m_write
) < 0))
3035 if (compile_options
.record_marker
== 0)
3036 record_marker
= sizeof (GFC_INTEGER_4
);
3038 record_marker
= compile_options
.record_marker
;
3040 /* Seek to the head and overwrite the bogus length with the real
3043 if (unlikely (sseek (dtp
->u
.p
.current_unit
->s
, - m
- 2 * record_marker
,
3052 if (unlikely (write_us_marker (dtp
, m_write
) < 0))
3055 /* Seek past the end of the current record. */
3057 if (unlikely (sseek (dtp
->u
.p
.current_unit
->s
, m
+ record_marker
,
3064 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
3070 /* Utility function like memset() but operating on streams. Return
3071 value is same as for POSIX write(). */
3074 sset (stream
* s
, int c
, ssize_t nbyte
)
3076 static const int WRITE_CHUNK
= 256;
3077 char p
[WRITE_CHUNK
];
3078 ssize_t bytes_left
, trans
;
3080 if (nbyte
< WRITE_CHUNK
)
3081 memset (p
, c
, nbyte
);
3083 memset (p
, c
, WRITE_CHUNK
);
3086 while (bytes_left
> 0)
3088 trans
= (bytes_left
< WRITE_CHUNK
) ? bytes_left
: WRITE_CHUNK
;
3089 trans
= swrite (s
, p
, trans
);
3092 bytes_left
-= trans
;
3095 return nbyte
- bytes_left
;
3099 memset4 (gfc_char4_t
*p
, gfc_char4_t c
, int k
)
3102 for (j
= 0; j
< k
; j
++)
3106 /* Position to the next record in write mode. */
3109 next_record_w (st_parameter_dt
*dtp
, int done
)
3111 gfc_offset m
, record
, max_pos
;
3114 /* Zero counters for X- and T-editing. */
3115 max_pos
= dtp
->u
.p
.max_pos
;
3116 dtp
->u
.p
.max_pos
= dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
3118 switch (current_mode (dtp
))
3120 /* No records in unformatted STREAM I/O. */
3121 case UNFORMATTED_STREAM
:
3124 case FORMATTED_DIRECT
:
3125 if (dtp
->u
.p
.current_unit
->bytes_left
== 0)
3128 fbuf_seek (dtp
->u
.p
.current_unit
, 0, SEEK_END
);
3129 fbuf_flush (dtp
->u
.p
.current_unit
, WRITING
);
3130 if (sset (dtp
->u
.p
.current_unit
->s
, ' ',
3131 dtp
->u
.p
.current_unit
->bytes_left
)
3132 != dtp
->u
.p
.current_unit
->bytes_left
)
3137 case UNFORMATTED_DIRECT
:
3138 if (dtp
->u
.p
.current_unit
->bytes_left
> 0)
3140 length
= (int) dtp
->u
.p
.current_unit
->bytes_left
;
3141 if (sset (dtp
->u
.p
.current_unit
->s
, 0, length
) != length
)
3146 case UNFORMATTED_SEQUENTIAL
:
3147 next_record_w_unf (dtp
, 0);
3148 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
3151 case FORMATTED_STREAM
:
3152 case FORMATTED_SEQUENTIAL
:
3154 if (is_internal_unit (dtp
))
3157 if (is_array_io (dtp
))
3161 length
= (int) dtp
->u
.p
.current_unit
->bytes_left
;
3163 /* If the farthest position reached is greater than current
3164 position, adjust the position and set length to pad out
3165 whats left. Otherwise just pad whats left.
3166 (for character array unit) */
3167 m
= dtp
->u
.p
.current_unit
->recl
3168 - dtp
->u
.p
.current_unit
->bytes_left
;
3171 length
= (int) (max_pos
- m
);
3172 if (sseek (dtp
->u
.p
.current_unit
->s
,
3173 length
, SEEK_CUR
) < 0)
3175 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
3178 length
= (int) (dtp
->u
.p
.current_unit
->recl
- max_pos
);
3181 p
= write_block (dtp
, length
);
3185 if (unlikely (is_char4_unit (dtp
)))
3187 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
3188 memset4 (p4
, ' ', length
);
3191 memset (p
, ' ', length
);
3193 /* Now that the current record has been padded out,
3194 determine where the next record in the array is. */
3195 record
= next_array_record (dtp
, dtp
->u
.p
.current_unit
->ls
,
3198 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
3200 /* Now seek to this record */
3201 record
= record
* dtp
->u
.p
.current_unit
->recl
;
3203 if (sseek (dtp
->u
.p
.current_unit
->s
, record
, SEEK_SET
) < 0)
3205 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
3209 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
3215 /* If this is the last call to next_record move to the farthest
3216 position reached and set length to pad out the remainder
3217 of the record. (for character scaler unit) */
3220 m
= dtp
->u
.p
.current_unit
->recl
3221 - dtp
->u
.p
.current_unit
->bytes_left
;
3224 length
= (int) (max_pos
- m
);
3225 if (sseek (dtp
->u
.p
.current_unit
->s
,
3226 length
, SEEK_CUR
) < 0)
3228 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
3231 length
= (int) (dtp
->u
.p
.current_unit
->recl
- max_pos
);
3234 length
= (int) dtp
->u
.p
.current_unit
->bytes_left
;
3238 p
= write_block (dtp
, length
);
3242 if (unlikely (is_char4_unit (dtp
)))
3244 gfc_char4_t
*p4
= (gfc_char4_t
*) p
;
3245 memset4 (p4
, (gfc_char4_t
) ' ', length
);
3248 memset (p
, ' ', length
);
3259 fbuf_seek (dtp
->u
.p
.current_unit
, 0, SEEK_END
);
3260 char * p
= fbuf_alloc (dtp
->u
.p
.current_unit
, len
);
3267 if (is_stream_io (dtp
))
3269 dtp
->u
.p
.current_unit
->strm_pos
+= len
;
3270 if (dtp
->u
.p
.current_unit
->strm_pos
3271 < file_length (dtp
->u
.p
.current_unit
->s
))
3272 unit_truncate (dtp
->u
.p
.current_unit
,
3273 dtp
->u
.p
.current_unit
->strm_pos
- 1,
3281 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
3286 /* Position to the next record, which means moving to the end of the
3287 current record. This can happen under several different
3288 conditions. If the done flag is not set, we get ready to process
3292 next_record (st_parameter_dt
*dtp
, int done
)
3294 gfc_offset fp
; /* File position. */
3296 dtp
->u
.p
.current_unit
->read_bad
= 0;
3298 if (dtp
->u
.p
.mode
== READING
)
3299 next_record_r (dtp
, done
);
3301 next_record_w (dtp
, done
);
3303 if (!is_stream_io (dtp
))
3305 /* Keep position up to date for INQUIRE */
3307 update_position (dtp
->u
.p
.current_unit
);
3309 dtp
->u
.p
.current_unit
->current_record
= 0;
3310 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
3312 fp
= stell (dtp
->u
.p
.current_unit
->s
);
3313 /* Calculate next record, rounding up partial records. */
3314 dtp
->u
.p
.current_unit
->last_record
=
3315 (fp
+ dtp
->u
.p
.current_unit
->recl
- 1) /
3316 dtp
->u
.p
.current_unit
->recl
;
3319 dtp
->u
.p
.current_unit
->last_record
++;
3325 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
3329 /* Finalize the current data transfer. For a nonadvancing transfer,
3330 this means advancing to the next record. For internal units close the
3331 stream associated with the unit. */
3334 finalize_transfer (st_parameter_dt
*dtp
)
3337 GFC_INTEGER_4 cf
= dtp
->common
.flags
;
3339 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
3340 *dtp
->size
= dtp
->u
.p
.size_used
;
3342 if (dtp
->u
.p
.eor_condition
)
3344 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
3348 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
3350 if (dtp
->u
.p
.current_unit
&& current_mode (dtp
) == UNFORMATTED_SEQUENTIAL
)
3351 dtp
->u
.p
.current_unit
->current_record
= 0;
3355 if ((dtp
->u
.p
.ionml
!= NULL
)
3356 && (cf
& IOPARM_DT_HAS_NAMELIST_NAME
) != 0)
3358 if ((cf
& IOPARM_DT_NAMELIST_READ_MODE
) != 0)
3359 namelist_read (dtp
);
3361 namelist_write (dtp
);
3364 dtp
->u
.p
.transfer
= NULL
;
3365 if (dtp
->u
.p
.current_unit
== NULL
)
3368 dtp
->u
.p
.eof_jump
= &eof_jump
;
3369 if (setjmp (eof_jump
))
3371 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
3375 if ((cf
& IOPARM_DT_LIST_FORMAT
) != 0 && dtp
->u
.p
.mode
== READING
)
3377 finish_list_read (dtp
);
3381 if (dtp
->u
.p
.mode
== WRITING
)
3382 dtp
->u
.p
.current_unit
->previous_nonadvancing_write
3383 = dtp
->u
.p
.advance_status
== ADVANCE_NO
;
3385 if (is_stream_io (dtp
))
3387 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
3388 && dtp
->u
.p
.advance_status
!= ADVANCE_NO
)
3389 next_record (dtp
, 1);
3394 dtp
->u
.p
.current_unit
->current_record
= 0;
3396 if (!is_internal_unit (dtp
) && dtp
->u
.p
.seen_dollar
)
3398 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
3399 dtp
->u
.p
.seen_dollar
= 0;
3403 /* For non-advancing I/O, save the current maximum position for use in the
3404 next I/O operation if needed. */
3405 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
)
3407 int bytes_written
= (int) (dtp
->u
.p
.current_unit
->recl
3408 - dtp
->u
.p
.current_unit
->bytes_left
);
3409 dtp
->u
.p
.current_unit
->saved_pos
=
3410 dtp
->u
.p
.max_pos
> 0 ? dtp
->u
.p
.max_pos
- bytes_written
: 0;
3411 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
3414 else if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
3415 && dtp
->u
.p
.mode
== WRITING
&& !is_internal_unit (dtp
))
3416 fbuf_seek (dtp
->u
.p
.current_unit
, 0, SEEK_END
);
3418 dtp
->u
.p
.current_unit
->saved_pos
= 0;
3420 next_record (dtp
, 1);
3423 /* Transfer function for IOLENGTH. It doesn't actually do any
3424 data transfer, it just updates the length counter. */
3427 iolength_transfer (st_parameter_dt
*dtp
, bt type
__attribute__((unused
)),
3428 void *dest
__attribute__ ((unused
)),
3429 int kind
__attribute__((unused
)),
3430 size_t size
, size_t nelems
)
3432 if ((dtp
->common
.flags
& IOPARM_DT_HAS_IOLENGTH
) != 0)
3433 *dtp
->iolength
+= (GFC_IO_INT
) (size
* nelems
);
3437 /* Initialize the IOLENGTH data transfer. This function is in essence
3438 a very much simplified version of data_transfer_init(), because it
3439 doesn't have to deal with units at all. */
3442 iolength_transfer_init (st_parameter_dt
*dtp
)
3444 if ((dtp
->common
.flags
& IOPARM_DT_HAS_IOLENGTH
) != 0)
3447 memset (&dtp
->u
.p
, 0, sizeof (dtp
->u
.p
));
3449 /* Set up the subroutine that will handle the transfers. */
3451 dtp
->u
.p
.transfer
= iolength_transfer
;
3455 /* Library entry point for the IOLENGTH form of the INQUIRE
3456 statement. The IOLENGTH form requires no I/O to be performed, but
3457 it must still be a runtime library call so that we can determine
3458 the iolength for dynamic arrays and such. */
3460 extern void st_iolength (st_parameter_dt
*);
3461 export_proto(st_iolength
);
3464 st_iolength (st_parameter_dt
*dtp
)
3466 library_start (&dtp
->common
);
3467 iolength_transfer_init (dtp
);
3470 extern void st_iolength_done (st_parameter_dt
*);
3471 export_proto(st_iolength_done
);
3474 st_iolength_done (st_parameter_dt
*dtp
__attribute__((unused
)))
3481 /* The READ statement. */
3483 extern void st_read (st_parameter_dt
*);
3484 export_proto(st_read
);
3487 st_read (st_parameter_dt
*dtp
)
3489 library_start (&dtp
->common
);
3491 data_transfer_init (dtp
, 1);
3494 extern void st_read_done (st_parameter_dt
*);
3495 export_proto(st_read_done
);
3498 st_read_done (st_parameter_dt
*dtp
)
3500 finalize_transfer (dtp
);
3501 if (is_internal_unit (dtp
) || dtp
->u
.p
.format_not_saved
)
3502 free_format_data (dtp
->u
.p
.fmt
);
3504 if (dtp
->u
.p
.current_unit
!= NULL
)
3505 unlock_unit (dtp
->u
.p
.current_unit
);
3507 free_internal_unit (dtp
);
3512 extern void st_write (st_parameter_dt
*);
3513 export_proto(st_write
);
3516 st_write (st_parameter_dt
*dtp
)
3518 library_start (&dtp
->common
);
3519 data_transfer_init (dtp
, 0);
3522 extern void st_write_done (st_parameter_dt
*);
3523 export_proto(st_write_done
);
3526 st_write_done (st_parameter_dt
*dtp
)
3528 finalize_transfer (dtp
);
3530 /* Deal with endfile conditions associated with sequential files. */
3532 if (dtp
->u
.p
.current_unit
!= NULL
3533 && dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
3534 switch (dtp
->u
.p
.current_unit
->endfile
)
3536 case AT_ENDFILE
: /* Remain at the endfile record. */
3540 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
; /* Just at it now. */
3544 /* Get rid of whatever is after this record. */
3545 if (!is_internal_unit (dtp
))
3546 unit_truncate (dtp
->u
.p
.current_unit
,
3547 stell (dtp
->u
.p
.current_unit
->s
),
3549 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
3553 if (is_internal_unit (dtp
) || dtp
->u
.p
.format_not_saved
)
3554 free_format_data (dtp
->u
.p
.fmt
);
3556 if (dtp
->u
.p
.current_unit
!= NULL
)
3557 unlock_unit (dtp
->u
.p
.current_unit
);
3559 free_internal_unit (dtp
);
3565 /* F2003: This is a stub for the runtime portion of the WAIT statement. */
3567 st_wait (st_parameter_wait
*wtp
__attribute__((unused
)))
3572 /* Receives the scalar information for namelist objects and stores it
3573 in a linked list of namelist_info types. */
3575 extern void st_set_nml_var (st_parameter_dt
*dtp
, void *, char *,
3576 GFC_INTEGER_4
, gfc_charlen_type
, GFC_INTEGER_4
);
3577 export_proto(st_set_nml_var
);
3581 st_set_nml_var (st_parameter_dt
*dtp
, void * var_addr
, char * var_name
,
3582 GFC_INTEGER_4 len
, gfc_charlen_type string_length
,
3583 GFC_INTEGER_4 dtype
)
3585 namelist_info
*t1
= NULL
;
3587 size_t var_name_len
= strlen (var_name
);
3589 nml
= (namelist_info
*) get_mem (sizeof (namelist_info
));
3591 nml
->mem_pos
= var_addr
;
3593 nml
->var_name
= (char*) get_mem (var_name_len
+ 1);
3594 memcpy (nml
->var_name
, var_name
, var_name_len
);
3595 nml
->var_name
[var_name_len
] = '\0';
3597 nml
->len
= (int) len
;
3598 nml
->string_length
= (index_type
) string_length
;
3600 nml
->var_rank
= (int) (dtype
& GFC_DTYPE_RANK_MASK
);
3601 nml
->size
= (index_type
) (dtype
>> GFC_DTYPE_SIZE_SHIFT
);
3602 nml
->type
= (bt
) ((dtype
& GFC_DTYPE_TYPE_MASK
) >> GFC_DTYPE_TYPE_SHIFT
);
3604 if (nml
->var_rank
> 0)
3606 nml
->dim
= (descriptor_dimension
*)
3607 get_mem (nml
->var_rank
* sizeof (descriptor_dimension
));
3608 nml
->ls
= (array_loop_spec
*)
3609 get_mem (nml
->var_rank
* sizeof (array_loop_spec
));
3619 if ((dtp
->common
.flags
& IOPARM_DT_IONML_SET
) == 0)
3621 dtp
->common
.flags
|= IOPARM_DT_IONML_SET
;
3622 dtp
->u
.p
.ionml
= nml
;
3626 for (t1
= dtp
->u
.p
.ionml
; t1
->next
; t1
= t1
->next
);
3631 /* Store the dimensional information for the namelist object. */
3632 extern void st_set_nml_var_dim (st_parameter_dt
*, GFC_INTEGER_4
,
3633 index_type
, index_type
,
3635 export_proto(st_set_nml_var_dim
);
3638 st_set_nml_var_dim (st_parameter_dt
*dtp
, GFC_INTEGER_4 n_dim
,
3639 index_type stride
, index_type lbound
,
3642 namelist_info
* nml
;
3647 for (nml
= dtp
->u
.p
.ionml
; nml
->next
; nml
= nml
->next
);
3649 GFC_DIMENSION_SET(nml
->dim
[n
],lbound
,ubound
,stride
);
3652 /* Reverse memcpy - used for byte swapping. */
3654 void reverse_memcpy (void *dest
, const void *src
, size_t n
)
3660 s
= (char *) src
+ n
- 1;
3662 /* Write with ascending order - this is likely faster
3663 on modern architectures because of write combining. */
3669 /* Once upon a time, a poor innocent Fortran program was reading a
3670 file, when suddenly it hit the end-of-file (EOF). Unfortunately
3671 the OS doesn't tell whether we're at the EOF or whether we already
3672 went past it. Luckily our hero, libgfortran, keeps track of this.
3673 Call this function when you detect an EOF condition. See Section
3677 hit_eof (st_parameter_dt
* dtp
)
3679 dtp
->u
.p
.current_unit
->flags
.position
= POSITION_APPEND
;
3681 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
3682 switch (dtp
->u
.p
.current_unit
->endfile
)
3686 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
3687 if (!is_internal_unit (dtp
))
3689 dtp
->u
.p
.current_unit
->endfile
= AFTER_ENDFILE
;
3690 dtp
->u
.p
.current_unit
->current_record
= 0;
3693 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
3697 generate_error (&dtp
->common
, LIBERROR_ENDFILE
, NULL
);
3698 dtp
->u
.p
.current_unit
->current_record
= 0;
3703 /* Non-sequential files don't have an ENDFILE record, so we
3704 can't be at AFTER_ENDFILE. */
3705 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
3706 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
3707 dtp
->u
.p
.current_unit
->current_record
= 0;