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. */
699 dest
= mem_alloc_w4 (dtp
->u
.p
.current_unit
->s
, &length
);
701 dest
= mem_alloc_w (dtp
->u
.p
.current_unit
->s
, &length
);
705 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
709 if (unlikely (dtp
->u
.p
.current_unit
->endfile
== AT_ENDFILE
))
710 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
714 dest
= fbuf_alloc (dtp
->u
.p
.current_unit
, length
);
717 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
722 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
723 dtp
->u
.p
.size_used
+= (GFC_IO_INT
) length
;
725 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) length
;
731 /* High level interface to swrite(), taking care of errors. This is only
732 called for unformatted files. There are three cases to consider:
733 Stream I/O, unformatted direct, unformatted sequential. */
736 write_buf (st_parameter_dt
*dtp
, void *buf
, size_t nbytes
)
739 ssize_t have_written
;
740 ssize_t to_write_subrecord
;
745 if (is_stream_io (dtp
))
747 have_written
= swrite (dtp
->u
.p
.current_unit
->s
, buf
, nbytes
);
748 if (unlikely (have_written
< 0))
750 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
754 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) have_written
;
759 /* Unformatted direct access. */
761 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
763 if (unlikely (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) nbytes
))
765 generate_error (&dtp
->common
, LIBERROR_DIRECT_EOR
, NULL
);
769 if (buf
== NULL
&& nbytes
== 0)
772 have_written
= swrite (dtp
->u
.p
.current_unit
->s
, buf
, nbytes
);
773 if (unlikely (have_written
< 0))
775 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
779 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) have_written
;
780 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) have_written
;
785 /* Unformatted sequential. */
789 if (dtp
->u
.p
.current_unit
->flags
.has_recl
790 && (gfc_offset
) nbytes
> dtp
->u
.p
.current_unit
->bytes_left
)
792 nbytes
= dtp
->u
.p
.current_unit
->bytes_left
;
804 (size_t) dtp
->u
.p
.current_unit
->bytes_left_subrecord
< nbytes
?
805 (size_t) dtp
->u
.p
.current_unit
->bytes_left_subrecord
: nbytes
;
807 dtp
->u
.p
.current_unit
->bytes_left_subrecord
-=
808 (gfc_offset
) to_write_subrecord
;
810 to_write_subrecord
= swrite (dtp
->u
.p
.current_unit
->s
,
811 buf
+ have_written
, to_write_subrecord
);
812 if (unlikely (to_write_subrecord
< 0))
814 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
818 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) to_write_subrecord
;
819 nbytes
-= to_write_subrecord
;
820 have_written
+= to_write_subrecord
;
825 next_record_w_unf (dtp
, 1);
828 dtp
->u
.p
.current_unit
->bytes_left
-= have_written
;
829 if (unlikely (short_record
))
831 generate_error (&dtp
->common
, LIBERROR_SHORT_RECORD
, NULL
);
838 /* Master function for unformatted reads. */
841 unformatted_read (st_parameter_dt
*dtp
, bt type
,
842 void *dest
, int kind
, size_t size
, size_t nelems
)
844 if (likely (dtp
->u
.p
.current_unit
->flags
.convert
== GFC_CONVERT_NATIVE
)
847 if (type
== BT_CHARACTER
)
848 size
*= GFC_SIZE_OF_CHAR_KIND(kind
);
849 read_block_direct (dtp
, dest
, size
* nelems
);
859 /* Handle wide chracters. */
860 if (type
== BT_CHARACTER
&& kind
!= 1)
866 /* Break up complex into its constituent reals. */
867 if (type
== BT_COMPLEX
)
873 /* By now, all complex variables have been split into their
874 constituent reals. */
876 for (i
= 0; i
< nelems
; i
++)
878 read_block_direct (dtp
, buffer
, size
);
879 reverse_memcpy (p
, buffer
, size
);
886 /* Master function for unformatted writes. NOTE: For kind=10 the size is 16
887 bytes on 64 bit machines. The unused bytes are not initialized and never
888 used, which can show an error with memory checking analyzers like
892 unformatted_write (st_parameter_dt
*dtp
, bt type
,
893 void *source
, int kind
, size_t size
, size_t nelems
)
895 if (likely (dtp
->u
.p
.current_unit
->flags
.convert
== GFC_CONVERT_NATIVE
)
898 size_t stride
= type
== BT_CHARACTER
?
899 size
* GFC_SIZE_OF_CHAR_KIND(kind
) : size
;
901 write_buf (dtp
, source
, stride
* nelems
);
911 /* Handle wide chracters. */
912 if (type
== BT_CHARACTER
&& kind
!= 1)
918 /* Break up complex into its constituent reals. */
919 if (type
== BT_COMPLEX
)
925 /* By now, all complex variables have been split into their
926 constituent reals. */
928 for (i
= 0; i
< nelems
; i
++)
930 reverse_memcpy(buffer
, p
, size
);
932 write_buf (dtp
, buffer
, size
);
938 /* Return a pointer to the name of a type. */
963 internal_error (NULL
, "type_name(): Bad type");
970 /* Write a constant string to the output.
971 This is complicated because the string can have doubled delimiters
972 in it. The length in the format node is the true length. */
975 write_constant_string (st_parameter_dt
*dtp
, const fnode
*f
)
977 char c
, delimiter
, *p
, *q
;
980 length
= f
->u
.string
.length
;
984 p
= write_block (dtp
, length
);
991 for (; length
> 0; length
--)
994 if (c
== delimiter
&& c
!= 'H' && c
!= 'h')
995 q
++; /* Skip the doubled delimiter. */
1000 /* Given actual and expected types in a formatted data transfer, make
1001 sure they agree. If not, an error message is generated. Returns
1002 nonzero if something went wrong. */
1005 require_type (st_parameter_dt
*dtp
, bt expected
, bt actual
, const fnode
*f
)
1009 if (actual
== expected
)
1012 /* Adjust item_count before emitting error message. */
1013 sprintf (buffer
, "Expected %s for item %d in formatted transfer, got %s",
1014 type_name (expected
), dtp
->u
.p
.item_count
- 1, type_name (actual
));
1016 format_error (dtp
, f
, buffer
);
1021 /* This function is in the main loop for a formatted data transfer
1022 statement. It would be natural to implement this as a coroutine
1023 with the user program, but C makes that awkward. We loop,
1024 processing format elements. When we actually have to transfer
1025 data instead of just setting flags, we return control to the user
1026 program which calls a function that supplies the address and type
1027 of the next element, then comes back here to process it. */
1030 formatted_transfer_scalar_read (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
1033 int pos
, bytes_used
;
1037 int consume_data_flag
;
1039 /* Change a complex data item into a pair of reals. */
1041 n
= (p
== NULL
) ? 0 : ((type
!= BT_COMPLEX
) ? 1 : 2);
1042 if (type
== BT_COMPLEX
)
1048 /* If there's an EOR condition, we simulate finalizing the transfer
1049 by doing nothing. */
1050 if (dtp
->u
.p
.eor_condition
)
1053 /* Set this flag so that commas in reads cause the read to complete before
1054 the entire field has been read. The next read field will start right after
1055 the comma in the stream. (Set to 0 for character reads). */
1056 dtp
->u
.p
.sf_read_comma
=
1057 dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
? 0 : 1;
1061 /* If reversion has occurred and there is another real data item,
1062 then we have to move to the next record. */
1063 if (dtp
->u
.p
.reversion_flag
&& n
> 0)
1065 dtp
->u
.p
.reversion_flag
= 0;
1066 next_record (dtp
, 0);
1069 consume_data_flag
= 1;
1070 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1073 f
= next_format (dtp
);
1076 /* No data descriptors left. */
1077 if (unlikely (n
> 0))
1078 generate_error (&dtp
->common
, LIBERROR_FORMAT
,
1079 "Insufficient data descriptors in format after reversion");
1085 bytes_used
= (int)(dtp
->u
.p
.current_unit
->recl
1086 - dtp
->u
.p
.current_unit
->bytes_left
);
1088 if (is_stream_io(dtp
))
1095 goto need_read_data
;
1096 if (require_type (dtp
, BT_INTEGER
, type
, f
))
1098 read_decimal (dtp
, f
, p
, kind
);
1103 goto need_read_data
;
1104 if (!(compile_options
.allow_std
& GFC_STD_GNU
)
1105 && require_type (dtp
, BT_INTEGER
, type
, f
))
1107 read_radix (dtp
, f
, p
, kind
, 2);
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
, 8);
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
, 16);
1130 goto need_read_data
;
1132 /* It is possible to have FMT_A with something not BT_CHARACTER such
1133 as when writing out hollerith strings, so check both type
1134 and kind before calling wide character routines. */
1135 if (type
== BT_CHARACTER
&& kind
== 4)
1136 read_a_char4 (dtp
, f
, p
, size
);
1138 read_a (dtp
, f
, p
, size
);
1143 goto need_read_data
;
1144 read_l (dtp
, f
, p
, kind
);
1149 goto need_read_data
;
1150 if (require_type (dtp
, BT_REAL
, type
, f
))
1152 read_f (dtp
, f
, p
, kind
);
1157 goto need_read_data
;
1158 if (require_type (dtp
, BT_REAL
, type
, f
))
1160 read_f (dtp
, f
, p
, kind
);
1165 goto need_read_data
;
1166 if (require_type (dtp
, BT_REAL
, type
, f
))
1168 read_f (dtp
, f
, p
, kind
);
1173 goto need_read_data
;
1174 if (require_type (dtp
, BT_REAL
, type
, f
))
1176 read_f (dtp
, f
, p
, kind
);
1181 goto need_read_data
;
1182 if (require_type (dtp
, BT_REAL
, type
, f
))
1184 read_f (dtp
, f
, p
, kind
);
1189 goto need_read_data
;
1193 read_decimal (dtp
, f
, p
, kind
);
1196 read_l (dtp
, f
, p
, kind
);
1200 read_a_char4 (dtp
, f
, p
, size
);
1202 read_a (dtp
, f
, p
, size
);
1205 read_f (dtp
, f
, p
, kind
);
1208 internal_error (&dtp
->common
, "formatted_transfer(): Bad type");
1213 consume_data_flag
= 0;
1214 format_error (dtp
, f
, "Constant string in input format");
1217 /* Format codes that don't transfer data. */
1220 consume_data_flag
= 0;
1221 dtp
->u
.p
.skips
+= f
->u
.n
;
1222 pos
= bytes_used
+ dtp
->u
.p
.skips
- 1;
1223 dtp
->u
.p
.pending_spaces
= pos
- dtp
->u
.p
.max_pos
+ 1;
1224 read_x (dtp
, f
->u
.n
);
1229 consume_data_flag
= 0;
1231 if (f
->format
== FMT_TL
)
1233 /* Handle the special case when no bytes have been used yet.
1234 Cannot go below zero. */
1235 if (bytes_used
== 0)
1237 dtp
->u
.p
.pending_spaces
-= f
->u
.n
;
1238 dtp
->u
.p
.skips
-= f
->u
.n
;
1239 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
< 0 ? 0 : dtp
->u
.p
.skips
;
1242 pos
= bytes_used
- f
->u
.n
;
1247 /* Standard 10.6.1.1: excessive left tabbing is reset to the
1248 left tab limit. We do not check if the position has gone
1249 beyond the end of record because a subsequent tab could
1250 bring us back again. */
1251 pos
= pos
< 0 ? 0 : pos
;
1253 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
+ pos
- bytes_used
;
1254 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
1255 + pos
- dtp
->u
.p
.max_pos
;
1256 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
< 0
1257 ? 0 : dtp
->u
.p
.pending_spaces
;
1258 if (dtp
->u
.p
.skips
== 0)
1261 /* Adjust everything for end-of-record condition */
1262 if (dtp
->u
.p
.sf_seen_eor
&& !is_internal_unit (dtp
))
1264 dtp
->u
.p
.current_unit
->bytes_left
-= dtp
->u
.p
.sf_seen_eor
;
1265 dtp
->u
.p
.skips
-= dtp
->u
.p
.sf_seen_eor
;
1267 dtp
->u
.p
.sf_seen_eor
= 0;
1269 if (dtp
->u
.p
.skips
< 0)
1271 if (is_internal_unit (dtp
))
1272 sseek (dtp
->u
.p
.current_unit
->s
, dtp
->u
.p
.skips
, SEEK_CUR
);
1274 fbuf_seek (dtp
->u
.p
.current_unit
, dtp
->u
.p
.skips
, SEEK_CUR
);
1275 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) dtp
->u
.p
.skips
;
1276 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1279 read_x (dtp
, dtp
->u
.p
.skips
);
1283 consume_data_flag
= 0;
1284 dtp
->u
.p
.sign_status
= SIGN_S
;
1288 consume_data_flag
= 0;
1289 dtp
->u
.p
.sign_status
= SIGN_SS
;
1293 consume_data_flag
= 0;
1294 dtp
->u
.p
.sign_status
= SIGN_SP
;
1298 consume_data_flag
= 0 ;
1299 dtp
->u
.p
.blank_status
= BLANK_NULL
;
1303 consume_data_flag
= 0;
1304 dtp
->u
.p
.blank_status
= BLANK_ZERO
;
1308 consume_data_flag
= 0;
1309 dtp
->u
.p
.current_unit
->decimal_status
= DECIMAL_COMMA
;
1313 consume_data_flag
= 0;
1314 dtp
->u
.p
.current_unit
->decimal_status
= DECIMAL_POINT
;
1318 consume_data_flag
= 0;
1319 dtp
->u
.p
.current_unit
->round_status
= ROUND_COMPATIBLE
;
1323 consume_data_flag
= 0;
1324 dtp
->u
.p
.current_unit
->round_status
= ROUND_DOWN
;
1328 consume_data_flag
= 0;
1329 dtp
->u
.p
.current_unit
->round_status
= ROUND_NEAREST
;
1333 consume_data_flag
= 0;
1334 dtp
->u
.p
.current_unit
->round_status
= ROUND_PROCDEFINED
;
1338 consume_data_flag
= 0;
1339 dtp
->u
.p
.current_unit
->round_status
= ROUND_UP
;
1343 consume_data_flag
= 0;
1344 dtp
->u
.p
.current_unit
->round_status
= ROUND_ZERO
;
1348 consume_data_flag
= 0;
1349 dtp
->u
.p
.scale_factor
= f
->u
.k
;
1353 consume_data_flag
= 0;
1354 dtp
->u
.p
.seen_dollar
= 1;
1358 consume_data_flag
= 0;
1359 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1360 next_record (dtp
, 0);
1364 /* A colon descriptor causes us to exit this loop (in
1365 particular preventing another / descriptor from being
1366 processed) unless there is another data item to be
1368 consume_data_flag
= 0;
1374 internal_error (&dtp
->common
, "Bad format node");
1377 /* Adjust the item count and data pointer. */
1379 if ((consume_data_flag
> 0) && (n
> 0))
1382 p
= ((char *) p
) + size
;
1387 pos
= (int)(dtp
->u
.p
.current_unit
->recl
- dtp
->u
.p
.current_unit
->bytes_left
);
1388 dtp
->u
.p
.max_pos
= (dtp
->u
.p
.max_pos
> pos
) ? dtp
->u
.p
.max_pos
: pos
;
1393 /* Come here when we need a data descriptor but don't have one. We
1394 push the current format node back onto the input, then return and
1395 let the user program call us back with the data. */
1397 unget_format (dtp
, f
);
1402 formatted_transfer_scalar_write (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
1405 int pos
, bytes_used
;
1409 int consume_data_flag
;
1411 /* Change a complex data item into a pair of reals. */
1413 n
= (p
== NULL
) ? 0 : ((type
!= BT_COMPLEX
) ? 1 : 2);
1414 if (type
== BT_COMPLEX
)
1420 /* If there's an EOR condition, we simulate finalizing the transfer
1421 by doing nothing. */
1422 if (dtp
->u
.p
.eor_condition
)
1425 /* Set this flag so that commas in reads cause the read to complete before
1426 the entire field has been read. The next read field will start right after
1427 the comma in the stream. (Set to 0 for character reads). */
1428 dtp
->u
.p
.sf_read_comma
=
1429 dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
? 0 : 1;
1433 /* If reversion has occurred and there is another real data item,
1434 then we have to move to the next record. */
1435 if (dtp
->u
.p
.reversion_flag
&& n
> 0)
1437 dtp
->u
.p
.reversion_flag
= 0;
1438 next_record (dtp
, 0);
1441 consume_data_flag
= 1;
1442 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1445 f
= next_format (dtp
);
1448 /* No data descriptors left. */
1449 if (unlikely (n
> 0))
1450 generate_error (&dtp
->common
, LIBERROR_FORMAT
,
1451 "Insufficient data descriptors in format after reversion");
1455 /* Now discharge T, TR and X movements to the right. This is delayed
1456 until a data producing format to suppress trailing spaces. */
1459 if (dtp
->u
.p
.mode
== WRITING
&& dtp
->u
.p
.skips
!= 0
1460 && ((n
>0 && ( t
== FMT_I
|| t
== FMT_B
|| t
== FMT_O
1461 || t
== FMT_Z
|| t
== FMT_F
|| t
== FMT_E
1462 || t
== FMT_EN
|| t
== FMT_ES
|| t
== FMT_G
1463 || t
== FMT_L
|| t
== FMT_A
|| t
== FMT_D
))
1464 || t
== FMT_STRING
))
1466 if (dtp
->u
.p
.skips
> 0)
1469 write_x (dtp
, dtp
->u
.p
.skips
, dtp
->u
.p
.pending_spaces
);
1470 tmp
= (int)(dtp
->u
.p
.current_unit
->recl
1471 - dtp
->u
.p
.current_unit
->bytes_left
);
1473 dtp
->u
.p
.max_pos
> tmp
? dtp
->u
.p
.max_pos
: tmp
;
1475 if (dtp
->u
.p
.skips
< 0)
1477 if (is_internal_unit (dtp
))
1478 sseek (dtp
->u
.p
.current_unit
->s
, dtp
->u
.p
.skips
, SEEK_CUR
);
1480 fbuf_seek (dtp
->u
.p
.current_unit
, dtp
->u
.p
.skips
, SEEK_CUR
);
1481 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) dtp
->u
.p
.skips
;
1483 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1486 bytes_used
= (int)(dtp
->u
.p
.current_unit
->recl
1487 - dtp
->u
.p
.current_unit
->bytes_left
);
1489 if (is_stream_io(dtp
))
1497 if (require_type (dtp
, BT_INTEGER
, type
, f
))
1499 write_i (dtp
, f
, p
, kind
);
1505 if (!(compile_options
.allow_std
& GFC_STD_GNU
)
1506 && require_type (dtp
, BT_INTEGER
, type
, f
))
1508 write_b (dtp
, f
, p
, kind
);
1514 if (!(compile_options
.allow_std
& GFC_STD_GNU
)
1515 && require_type (dtp
, BT_INTEGER
, type
, f
))
1517 write_o (dtp
, f
, p
, kind
);
1523 if (!(compile_options
.allow_std
& GFC_STD_GNU
)
1524 && require_type (dtp
, BT_INTEGER
, type
, f
))
1526 write_z (dtp
, f
, p
, kind
);
1533 /* It is possible to have FMT_A with something not BT_CHARACTER such
1534 as when writing out hollerith strings, so check both type
1535 and kind before calling wide character routines. */
1536 if (type
== BT_CHARACTER
&& kind
== 4)
1537 write_a_char4 (dtp
, f
, p
, size
);
1539 write_a (dtp
, f
, p
, size
);
1545 write_l (dtp
, f
, p
, kind
);
1551 if (require_type (dtp
, BT_REAL
, type
, f
))
1553 write_d (dtp
, f
, p
, kind
);
1559 if (require_type (dtp
, BT_REAL
, type
, f
))
1561 write_e (dtp
, f
, p
, kind
);
1567 if (require_type (dtp
, BT_REAL
, type
, f
))
1569 write_en (dtp
, f
, p
, kind
);
1575 if (require_type (dtp
, BT_REAL
, type
, f
))
1577 write_es (dtp
, f
, p
, kind
);
1583 if (require_type (dtp
, BT_REAL
, type
, f
))
1585 write_f (dtp
, f
, p
, kind
);
1594 write_i (dtp
, f
, p
, kind
);
1597 write_l (dtp
, f
, p
, kind
);
1601 write_a_char4 (dtp
, f
, p
, size
);
1603 write_a (dtp
, f
, p
, size
);
1606 if (f
->u
.real
.w
== 0)
1607 write_real_g0 (dtp
, p
, kind
, f
->u
.real
.d
);
1609 write_d (dtp
, f
, p
, kind
);
1612 internal_error (&dtp
->common
,
1613 "formatted_transfer(): Bad type");
1618 consume_data_flag
= 0;
1619 write_constant_string (dtp
, f
);
1622 /* Format codes that don't transfer data. */
1625 consume_data_flag
= 0;
1627 dtp
->u
.p
.skips
+= f
->u
.n
;
1628 pos
= bytes_used
+ dtp
->u
.p
.skips
- 1;
1629 dtp
->u
.p
.pending_spaces
= pos
- dtp
->u
.p
.max_pos
+ 1;
1630 /* Writes occur just before the switch on f->format, above, so
1631 that trailing blanks are suppressed, unless we are doing a
1632 non-advancing write in which case we want to output the blanks
1634 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
)
1636 write_x (dtp
, dtp
->u
.p
.skips
, dtp
->u
.p
.pending_spaces
);
1637 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1643 consume_data_flag
= 0;
1645 if (f
->format
== FMT_TL
)
1648 /* Handle the special case when no bytes have been used yet.
1649 Cannot go below zero. */
1650 if (bytes_used
== 0)
1652 dtp
->u
.p
.pending_spaces
-= f
->u
.n
;
1653 dtp
->u
.p
.skips
-= f
->u
.n
;
1654 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
< 0 ? 0 : dtp
->u
.p
.skips
;
1657 pos
= bytes_used
- f
->u
.n
;
1660 pos
= f
->u
.n
- dtp
->u
.p
.pending_spaces
- 1;
1662 /* Standard 10.6.1.1: excessive left tabbing is reset to the
1663 left tab limit. We do not check if the position has gone
1664 beyond the end of record because a subsequent tab could
1665 bring us back again. */
1666 pos
= pos
< 0 ? 0 : pos
;
1668 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
+ pos
- bytes_used
;
1669 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
1670 + pos
- dtp
->u
.p
.max_pos
;
1671 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
< 0
1672 ? 0 : dtp
->u
.p
.pending_spaces
;
1676 consume_data_flag
= 0;
1677 dtp
->u
.p
.sign_status
= SIGN_S
;
1681 consume_data_flag
= 0;
1682 dtp
->u
.p
.sign_status
= SIGN_SS
;
1686 consume_data_flag
= 0;
1687 dtp
->u
.p
.sign_status
= SIGN_SP
;
1691 consume_data_flag
= 0 ;
1692 dtp
->u
.p
.blank_status
= BLANK_NULL
;
1696 consume_data_flag
= 0;
1697 dtp
->u
.p
.blank_status
= BLANK_ZERO
;
1701 consume_data_flag
= 0;
1702 dtp
->u
.p
.current_unit
->decimal_status
= DECIMAL_COMMA
;
1706 consume_data_flag
= 0;
1707 dtp
->u
.p
.current_unit
->decimal_status
= DECIMAL_POINT
;
1711 consume_data_flag
= 0;
1712 dtp
->u
.p
.current_unit
->round_status
= ROUND_COMPATIBLE
;
1716 consume_data_flag
= 0;
1717 dtp
->u
.p
.current_unit
->round_status
= ROUND_DOWN
;
1721 consume_data_flag
= 0;
1722 dtp
->u
.p
.current_unit
->round_status
= ROUND_NEAREST
;
1726 consume_data_flag
= 0;
1727 dtp
->u
.p
.current_unit
->round_status
= ROUND_PROCDEFINED
;
1731 consume_data_flag
= 0;
1732 dtp
->u
.p
.current_unit
->round_status
= ROUND_UP
;
1736 consume_data_flag
= 0;
1737 dtp
->u
.p
.current_unit
->round_status
= ROUND_ZERO
;
1741 consume_data_flag
= 0;
1742 dtp
->u
.p
.scale_factor
= f
->u
.k
;
1746 consume_data_flag
= 0;
1747 dtp
->u
.p
.seen_dollar
= 1;
1751 consume_data_flag
= 0;
1752 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1753 next_record (dtp
, 0);
1757 /* A colon descriptor causes us to exit this loop (in
1758 particular preventing another / descriptor from being
1759 processed) unless there is another data item to be
1761 consume_data_flag
= 0;
1767 internal_error (&dtp
->common
, "Bad format node");
1770 /* Adjust the item count and data pointer. */
1772 if ((consume_data_flag
> 0) && (n
> 0))
1775 p
= ((char *) p
) + size
;
1778 pos
= (int)(dtp
->u
.p
.current_unit
->recl
- dtp
->u
.p
.current_unit
->bytes_left
);
1779 dtp
->u
.p
.max_pos
= (dtp
->u
.p
.max_pos
> pos
) ? dtp
->u
.p
.max_pos
: pos
;
1784 /* Come here when we need a data descriptor but don't have one. We
1785 push the current format node back onto the input, then return and
1786 let the user program call us back with the data. */
1788 unget_format (dtp
, f
);
1791 /* This function is first called from data_init_transfer to initiate the loop
1792 over each item in the format, transferring data as required. Subsequent
1793 calls to this function occur for each data item foound in the READ/WRITE
1794 statement. The item_count is incremented for each call. Since the first
1795 call is from data_transfer_init, the item_count is always one greater than
1796 the actual count number of the item being transferred. */
1799 formatted_transfer (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
1800 size_t size
, size_t nelems
)
1806 size_t stride
= type
== BT_CHARACTER
?
1807 size
* GFC_SIZE_OF_CHAR_KIND(kind
) : size
;
1808 if (dtp
->u
.p
.mode
== READING
)
1810 /* Big loop over all the elements. */
1811 for (elem
= 0; elem
< nelems
; elem
++)
1813 dtp
->u
.p
.item_count
++;
1814 formatted_transfer_scalar_read (dtp
, type
, tmp
+ stride
*elem
, kind
, size
);
1819 /* Big loop over all the elements. */
1820 for (elem
= 0; elem
< nelems
; elem
++)
1822 dtp
->u
.p
.item_count
++;
1823 formatted_transfer_scalar_write (dtp
, type
, tmp
+ stride
*elem
, kind
, size
);
1829 /* Data transfer entry points. The type of the data entity is
1830 implicit in the subroutine call. This prevents us from having to
1831 share a common enum with the compiler. */
1834 transfer_integer (st_parameter_dt
*dtp
, void *p
, int kind
)
1836 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1838 dtp
->u
.p
.transfer (dtp
, BT_INTEGER
, p
, kind
, kind
, 1);
1843 transfer_real (st_parameter_dt
*dtp
, void *p
, int kind
)
1846 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1848 size
= size_from_real_kind (kind
);
1849 dtp
->u
.p
.transfer (dtp
, BT_REAL
, p
, kind
, size
, 1);
1854 transfer_logical (st_parameter_dt
*dtp
, void *p
, int kind
)
1856 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1858 dtp
->u
.p
.transfer (dtp
, BT_LOGICAL
, p
, kind
, kind
, 1);
1863 transfer_character (st_parameter_dt
*dtp
, void *p
, int len
)
1865 static char *empty_string
[0];
1867 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1870 /* Strings of zero length can have p == NULL, which confuses the
1871 transfer routines into thinking we need more data elements. To avoid
1872 this, we give them a nice pointer. */
1873 if (len
== 0 && p
== NULL
)
1876 /* Set kind here to 1. */
1877 dtp
->u
.p
.transfer (dtp
, BT_CHARACTER
, p
, 1, len
, 1);
1881 transfer_character_wide (st_parameter_dt
*dtp
, void *p
, int len
, int kind
)
1883 static char *empty_string
[0];
1885 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1888 /* Strings of zero length can have p == NULL, which confuses the
1889 transfer routines into thinking we need more data elements. To avoid
1890 this, we give them a nice pointer. */
1891 if (len
== 0 && p
== NULL
)
1894 /* Here we pass the actual kind value. */
1895 dtp
->u
.p
.transfer (dtp
, BT_CHARACTER
, p
, kind
, len
, 1);
1900 transfer_complex (st_parameter_dt
*dtp
, void *p
, int kind
)
1903 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1905 size
= size_from_complex_kind (kind
);
1906 dtp
->u
.p
.transfer (dtp
, BT_COMPLEX
, p
, kind
, size
, 1);
1911 transfer_array (st_parameter_dt
*dtp
, gfc_array_char
*desc
, int kind
,
1912 gfc_charlen_type charlen
)
1914 index_type count
[GFC_MAX_DIMENSIONS
];
1915 index_type extent
[GFC_MAX_DIMENSIONS
];
1916 index_type stride
[GFC_MAX_DIMENSIONS
];
1917 index_type stride0
, rank
, size
, type
, n
;
1922 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1925 type
= GFC_DESCRIPTOR_TYPE (desc
);
1926 size
= GFC_DESCRIPTOR_SIZE (desc
);
1928 /* FIXME: What a kludge: Array descriptors and the IO library use
1929 different enums for types. */
1932 case GFC_DTYPE_UNKNOWN
:
1933 iotype
= BT_NULL
; /* Is this correct? */
1935 case GFC_DTYPE_INTEGER
:
1936 iotype
= BT_INTEGER
;
1938 case GFC_DTYPE_LOGICAL
:
1939 iotype
= BT_LOGICAL
;
1941 case GFC_DTYPE_REAL
:
1944 case GFC_DTYPE_COMPLEX
:
1945 iotype
= BT_COMPLEX
;
1947 case GFC_DTYPE_CHARACTER
:
1948 iotype
= BT_CHARACTER
;
1951 case GFC_DTYPE_DERIVED
:
1952 internal_error (&dtp
->common
,
1953 "Derived type I/O should have been handled via the frontend.");
1956 internal_error (&dtp
->common
, "transfer_array(): Bad type");
1959 rank
= GFC_DESCRIPTOR_RANK (desc
);
1960 for (n
= 0; n
< rank
; n
++)
1963 stride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(desc
,n
);
1964 extent
[n
] = GFC_DESCRIPTOR_EXTENT(desc
,n
);
1966 /* If the extent of even one dimension is zero, then the entire
1967 array section contains zero elements, so we return after writing
1968 a zero array record. */
1973 dtp
->u
.p
.transfer (dtp
, iotype
, data
, kind
, size
, tsize
);
1978 stride0
= stride
[0];
1980 /* If the innermost dimension has a stride of 1, we can do the transfer
1981 in contiguous chunks. */
1982 if (stride0
== size
)
1987 data
= GFC_DESCRIPTOR_DATA (desc
);
1991 dtp
->u
.p
.transfer (dtp
, iotype
, data
, kind
, size
, tsize
);
1992 data
+= stride0
* tsize
;
1995 while (count
[n
] == extent
[n
])
1998 data
-= stride
[n
] * extent
[n
];
2015 /* Preposition a sequential unformatted file while reading. */
2018 us_read (st_parameter_dt
*dtp
, int continued
)
2025 if (compile_options
.record_marker
== 0)
2026 n
= sizeof (GFC_INTEGER_4
);
2028 n
= compile_options
.record_marker
;
2030 nr
= sread (dtp
->u
.p
.current_unit
->s
, &i
, n
);
2031 if (unlikely (nr
< 0))
2033 generate_error (&dtp
->common
, LIBERROR_BAD_US
, NULL
);
2039 return; /* end of file */
2041 else if (unlikely (n
!= nr
))
2043 generate_error (&dtp
->common
, LIBERROR_BAD_US
, NULL
);
2047 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
2048 if (likely (dtp
->u
.p
.current_unit
->flags
.convert
== GFC_CONVERT_NATIVE
))
2052 case sizeof(GFC_INTEGER_4
):
2053 memcpy (&i4
, &i
, sizeof (i4
));
2057 case sizeof(GFC_INTEGER_8
):
2058 memcpy (&i8
, &i
, sizeof (i8
));
2063 runtime_error ("Illegal value for record marker");
2070 case sizeof(GFC_INTEGER_4
):
2071 reverse_memcpy (&i4
, &i
, sizeof (i4
));
2075 case sizeof(GFC_INTEGER_8
):
2076 reverse_memcpy (&i8
, &i
, sizeof (i8
));
2081 runtime_error ("Illegal value for record marker");
2087 dtp
->u
.p
.current_unit
->bytes_left_subrecord
= i
;
2088 dtp
->u
.p
.current_unit
->continued
= 0;
2092 dtp
->u
.p
.current_unit
->bytes_left_subrecord
= -i
;
2093 dtp
->u
.p
.current_unit
->continued
= 1;
2097 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2101 /* Preposition a sequential unformatted file while writing. This
2102 amount to writing a bogus length that will be filled in later. */
2105 us_write (st_parameter_dt
*dtp
, int continued
)
2112 if (compile_options
.record_marker
== 0)
2113 nbytes
= sizeof (GFC_INTEGER_4
);
2115 nbytes
= compile_options
.record_marker
;
2117 if (swrite (dtp
->u
.p
.current_unit
->s
, &dummy
, nbytes
) != nbytes
)
2118 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2120 /* For sequential unformatted, if RECL= was not specified in the OPEN
2121 we write until we have more bytes than can fit in the subrecord
2122 markers, then we write a new subrecord. */
2124 dtp
->u
.p
.current_unit
->bytes_left_subrecord
=
2125 dtp
->u
.p
.current_unit
->recl_subrecord
;
2126 dtp
->u
.p
.current_unit
->continued
= continued
;
2130 /* Position to the next record prior to transfer. We are assumed to
2131 be before the next record. We also calculate the bytes in the next
2135 pre_position (st_parameter_dt
*dtp
)
2137 if (dtp
->u
.p
.current_unit
->current_record
)
2138 return; /* Already positioned. */
2140 switch (current_mode (dtp
))
2142 case FORMATTED_STREAM
:
2143 case UNFORMATTED_STREAM
:
2144 /* There are no records with stream I/O. If the position was specified
2145 data_transfer_init has already positioned the file. If no position
2146 was specified, we continue from where we last left off. I.e.
2147 there is nothing to do here. */
2150 case UNFORMATTED_SEQUENTIAL
:
2151 if (dtp
->u
.p
.mode
== READING
)
2158 case FORMATTED_SEQUENTIAL
:
2159 case FORMATTED_DIRECT
:
2160 case UNFORMATTED_DIRECT
:
2161 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2165 dtp
->u
.p
.current_unit
->current_record
= 1;
2169 /* Initialize things for a data transfer. This code is common for
2170 both reading and writing. */
2173 data_transfer_init (st_parameter_dt
*dtp
, int read_flag
)
2175 unit_flags u_flags
; /* Used for creating a unit if needed. */
2176 GFC_INTEGER_4 cf
= dtp
->common
.flags
;
2177 namelist_info
*ionml
;
2179 ionml
= ((cf
& IOPARM_DT_IONML_SET
) != 0) ? dtp
->u
.p
.ionml
: NULL
;
2181 memset (&dtp
->u
.p
, 0, sizeof (dtp
->u
.p
));
2183 dtp
->u
.p
.ionml
= ionml
;
2184 dtp
->u
.p
.mode
= read_flag
? READING
: WRITING
;
2186 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
2189 if ((cf
& IOPARM_DT_HAS_SIZE
) != 0)
2190 dtp
->u
.p
.size_used
= 0; /* Initialize the count. */
2192 dtp
->u
.p
.current_unit
= get_unit (dtp
, 1);
2193 if (dtp
->u
.p
.current_unit
->s
== NULL
)
2194 { /* Open the unit with some default flags. */
2195 st_parameter_open opp
;
2198 if (dtp
->common
.unit
< 0)
2200 close_unit (dtp
->u
.p
.current_unit
);
2201 dtp
->u
.p
.current_unit
= NULL
;
2202 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2203 "Bad unit number in statement");
2206 memset (&u_flags
, '\0', sizeof (u_flags
));
2207 u_flags
.access
= ACCESS_SEQUENTIAL
;
2208 u_flags
.action
= ACTION_READWRITE
;
2210 /* Is it unformatted? */
2211 if (!(cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
2212 | IOPARM_DT_IONML_SET
)))
2213 u_flags
.form
= FORM_UNFORMATTED
;
2215 u_flags
.form
= FORM_UNSPECIFIED
;
2217 u_flags
.delim
= DELIM_UNSPECIFIED
;
2218 u_flags
.blank
= BLANK_UNSPECIFIED
;
2219 u_flags
.pad
= PAD_UNSPECIFIED
;
2220 u_flags
.decimal
= DECIMAL_UNSPECIFIED
;
2221 u_flags
.encoding
= ENCODING_UNSPECIFIED
;
2222 u_flags
.async
= ASYNC_UNSPECIFIED
;
2223 u_flags
.round
= ROUND_UNSPECIFIED
;
2224 u_flags
.sign
= SIGN_UNSPECIFIED
;
2226 u_flags
.status
= STATUS_UNKNOWN
;
2228 conv
= get_unformatted_convert (dtp
->common
.unit
);
2230 if (conv
== GFC_CONVERT_NONE
)
2231 conv
= compile_options
.convert
;
2233 /* We use big_endian, which is 0 on little-endian machines
2234 and 1 on big-endian machines. */
2237 case GFC_CONVERT_NATIVE
:
2238 case GFC_CONVERT_SWAP
:
2241 case GFC_CONVERT_BIG
:
2242 conv
= big_endian
? GFC_CONVERT_NATIVE
: GFC_CONVERT_SWAP
;
2245 case GFC_CONVERT_LITTLE
:
2246 conv
= big_endian
? GFC_CONVERT_SWAP
: GFC_CONVERT_NATIVE
;
2250 internal_error (&opp
.common
, "Illegal value for CONVERT");
2254 u_flags
.convert
= conv
;
2256 opp
.common
= dtp
->common
;
2257 opp
.common
.flags
&= IOPARM_COMMON_MASK
;
2258 dtp
->u
.p
.current_unit
= new_unit (&opp
, dtp
->u
.p
.current_unit
, &u_flags
);
2259 dtp
->common
.flags
&= ~IOPARM_COMMON_MASK
;
2260 dtp
->common
.flags
|= (opp
.common
.flags
& IOPARM_COMMON_MASK
);
2261 if (dtp
->u
.p
.current_unit
== NULL
)
2265 /* Check the action. */
2267 if (read_flag
&& dtp
->u
.p
.current_unit
->flags
.action
== ACTION_WRITE
)
2269 generate_error (&dtp
->common
, LIBERROR_BAD_ACTION
,
2270 "Cannot read from file opened for WRITE");
2274 if (!read_flag
&& dtp
->u
.p
.current_unit
->flags
.action
== ACTION_READ
)
2276 generate_error (&dtp
->common
, LIBERROR_BAD_ACTION
,
2277 "Cannot write to file opened for READ");
2281 dtp
->u
.p
.first_item
= 1;
2283 /* Check the format. */
2285 if ((cf
& IOPARM_DT_HAS_FORMAT
) != 0)
2288 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
2289 && (cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
))
2292 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2293 "Format present for UNFORMATTED data transfer");
2297 if ((cf
& IOPARM_DT_HAS_NAMELIST_NAME
) != 0 && dtp
->u
.p
.ionml
!= NULL
)
2299 if ((cf
& IOPARM_DT_HAS_FORMAT
) != 0)
2300 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2301 "A format cannot be specified with a namelist");
2303 else if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
&&
2304 !(cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
)))
2306 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2307 "Missing format for FORMATTED data transfer");
2310 if (is_internal_unit (dtp
)
2311 && dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
)
2313 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2314 "Internal file cannot be accessed by UNFORMATTED "
2319 /* Check the record or position number. */
2321 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
2322 && (cf
& IOPARM_DT_HAS_REC
) == 0)
2324 generate_error (&dtp
->common
, LIBERROR_MISSING_OPTION
,
2325 "Direct access data transfer requires record number");
2329 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
2331 if ((cf
& IOPARM_DT_HAS_REC
) != 0)
2333 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2334 "Record number not allowed for sequential access "
2339 if (dtp
->u
.p
.current_unit
->endfile
== AFTER_ENDFILE
)
2341 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2342 "Sequential READ or WRITE not allowed after "
2343 "EOF marker, possibly use REWIND or BACKSPACE");
2348 /* Process the ADVANCE option. */
2350 dtp
->u
.p
.advance_status
2351 = !(cf
& IOPARM_DT_HAS_ADVANCE
) ? ADVANCE_UNSPECIFIED
:
2352 find_option (&dtp
->common
, dtp
->advance
, dtp
->advance_len
, advance_opt
,
2353 "Bad ADVANCE parameter in data transfer statement");
2355 if (dtp
->u
.p
.advance_status
!= ADVANCE_UNSPECIFIED
)
2357 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
2359 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2360 "ADVANCE specification conflicts with sequential "
2365 if (is_internal_unit (dtp
))
2367 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2368 "ADVANCE specification conflicts with internal file");
2372 if ((cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
))
2373 != IOPARM_DT_HAS_FORMAT
)
2375 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2376 "ADVANCE specification requires an explicit format");
2383 dtp
->u
.p
.current_unit
->previous_nonadvancing_write
= 0;
2385 if ((cf
& IOPARM_EOR
) != 0 && dtp
->u
.p
.advance_status
!= ADVANCE_NO
)
2387 generate_error (&dtp
->common
, LIBERROR_MISSING_OPTION
,
2388 "EOR specification requires an ADVANCE specification "
2393 if ((cf
& IOPARM_DT_HAS_SIZE
) != 0
2394 && dtp
->u
.p
.advance_status
!= ADVANCE_NO
)
2396 generate_error (&dtp
->common
, LIBERROR_MISSING_OPTION
,
2397 "SIZE specification requires an ADVANCE "
2398 "specification of NO");
2403 { /* Write constraints. */
2404 if ((cf
& IOPARM_END
) != 0)
2406 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2407 "END specification cannot appear in a write "
2412 if ((cf
& IOPARM_EOR
) != 0)
2414 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2415 "EOR specification cannot appear in a write "
2420 if ((cf
& IOPARM_DT_HAS_SIZE
) != 0)
2422 generate_error (&dtp
->common
, LIBERROR_OPTION_CONFLICT
,
2423 "SIZE specification cannot appear in a write "
2429 if (dtp
->u
.p
.advance_status
== ADVANCE_UNSPECIFIED
)
2430 dtp
->u
.p
.advance_status
= ADVANCE_YES
;
2432 /* Check the decimal mode. */
2433 dtp
->u
.p
.current_unit
->decimal_status
2434 = !(cf
& IOPARM_DT_HAS_DECIMAL
) ? DECIMAL_UNSPECIFIED
:
2435 find_option (&dtp
->common
, dtp
->decimal
, dtp
->decimal_len
,
2436 decimal_opt
, "Bad DECIMAL parameter in data transfer "
2439 if (dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_UNSPECIFIED
)
2440 dtp
->u
.p
.current_unit
->decimal_status
= dtp
->u
.p
.current_unit
->flags
.decimal
;
2442 /* Check the round mode. */
2443 dtp
->u
.p
.current_unit
->round_status
2444 = !(cf
& IOPARM_DT_HAS_ROUND
) ? ROUND_UNSPECIFIED
:
2445 find_option (&dtp
->common
, dtp
->round
, dtp
->round_len
,
2446 round_opt
, "Bad ROUND parameter in data transfer "
2449 if (dtp
->u
.p
.current_unit
->round_status
== ROUND_UNSPECIFIED
)
2450 dtp
->u
.p
.current_unit
->round_status
= dtp
->u
.p
.current_unit
->flags
.round
;
2452 /* Check the sign mode. */
2453 dtp
->u
.p
.sign_status
2454 = !(cf
& IOPARM_DT_HAS_SIGN
) ? SIGN_UNSPECIFIED
:
2455 find_option (&dtp
->common
, dtp
->sign
, dtp
->sign_len
, sign_opt
,
2456 "Bad SIGN parameter in data transfer statement");
2458 if (dtp
->u
.p
.sign_status
== SIGN_UNSPECIFIED
)
2459 dtp
->u
.p
.sign_status
= dtp
->u
.p
.current_unit
->flags
.sign
;
2461 /* Check the blank mode. */
2462 dtp
->u
.p
.blank_status
2463 = !(cf
& IOPARM_DT_HAS_BLANK
) ? BLANK_UNSPECIFIED
:
2464 find_option (&dtp
->common
, dtp
->blank
, dtp
->blank_len
,
2466 "Bad BLANK parameter in data transfer statement");
2468 if (dtp
->u
.p
.blank_status
== BLANK_UNSPECIFIED
)
2469 dtp
->u
.p
.blank_status
= dtp
->u
.p
.current_unit
->flags
.blank
;
2471 /* Check the delim mode. */
2472 dtp
->u
.p
.current_unit
->delim_status
2473 = !(cf
& IOPARM_DT_HAS_DELIM
) ? DELIM_UNSPECIFIED
:
2474 find_option (&dtp
->common
, dtp
->delim
, dtp
->delim_len
,
2475 delim_opt
, "Bad DELIM parameter in data transfer statement");
2477 if (dtp
->u
.p
.current_unit
->delim_status
== DELIM_UNSPECIFIED
)
2478 dtp
->u
.p
.current_unit
->delim_status
= dtp
->u
.p
.current_unit
->flags
.delim
;
2480 /* Check the pad mode. */
2481 dtp
->u
.p
.current_unit
->pad_status
2482 = !(cf
& IOPARM_DT_HAS_PAD
) ? PAD_UNSPECIFIED
:
2483 find_option (&dtp
->common
, dtp
->pad
, dtp
->pad_len
, pad_opt
,
2484 "Bad PAD parameter in data transfer statement");
2486 if (dtp
->u
.p
.current_unit
->pad_status
== PAD_UNSPECIFIED
)
2487 dtp
->u
.p
.current_unit
->pad_status
= dtp
->u
.p
.current_unit
->flags
.pad
;
2489 /* Check to see if we might be reading what we wrote before */
2491 if (dtp
->u
.p
.mode
!= dtp
->u
.p
.current_unit
->mode
2492 && !is_internal_unit (dtp
))
2494 int pos
= fbuf_reset (dtp
->u
.p
.current_unit
);
2496 sseek (dtp
->u
.p
.current_unit
->s
, pos
, SEEK_CUR
);
2497 sflush(dtp
->u
.p
.current_unit
->s
);
2500 /* Check the POS= specifier: that it is in range and that it is used with a
2501 unit that has been connected for STREAM access. F2003 9.5.1.10. */
2503 if (((cf
& IOPARM_DT_HAS_POS
) != 0))
2505 if (is_stream_io (dtp
))
2510 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2511 "POS=specifier must be positive");
2515 if (dtp
->pos
>= dtp
->u
.p
.current_unit
->maxrec
)
2517 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2518 "POS=specifier too large");
2522 dtp
->rec
= dtp
->pos
;
2524 if (dtp
->u
.p
.mode
== READING
)
2526 /* Reset the endfile flag; if we hit EOF during reading
2527 we'll set the flag and generate an error at that point
2528 rather than worrying about it here. */
2529 dtp
->u
.p
.current_unit
->endfile
= NO_ENDFILE
;
2532 if (dtp
->pos
!= dtp
->u
.p
.current_unit
->strm_pos
)
2534 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
2535 if (sseek (dtp
->u
.p
.current_unit
->s
, dtp
->pos
- 1, SEEK_SET
) < 0)
2537 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2540 dtp
->u
.p
.current_unit
->strm_pos
= dtp
->pos
;
2545 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2546 "POS=specifier not allowed, "
2547 "Try OPEN with ACCESS='stream'");
2553 /* Sanity checks on the record number. */
2554 if ((cf
& IOPARM_DT_HAS_REC
) != 0)
2558 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2559 "Record number must be positive");
2563 if (dtp
->rec
>= dtp
->u
.p
.current_unit
->maxrec
)
2565 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2566 "Record number too large");
2570 /* Make sure format buffer is reset. */
2571 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
)
2572 fbuf_reset (dtp
->u
.p
.current_unit
);
2575 /* Check whether the record exists to be read. Only
2576 a partial record needs to exist. */
2578 if (dtp
->u
.p
.mode
== READING
&& (dtp
->rec
- 1)
2579 * dtp
->u
.p
.current_unit
->recl
>= file_length (dtp
->u
.p
.current_unit
->s
))
2581 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2582 "Non-existing record number");
2586 /* Position the file. */
2587 if (sseek (dtp
->u
.p
.current_unit
->s
, (gfc_offset
) (dtp
->rec
- 1)
2588 * dtp
->u
.p
.current_unit
->recl
, SEEK_SET
) < 0)
2590 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2594 /* TODO: This is required to maintain compatibility between
2595 4.3 and 4.4 runtime. Remove when ABI changes from 4.3 */
2597 if (is_stream_io (dtp
))
2598 dtp
->u
.p
.current_unit
->strm_pos
= dtp
->rec
;
2600 /* TODO: Un-comment this code when ABI changes from 4.3.
2601 if (dtp->u.p.current_unit->flags.access == ACCESS_STREAM)
2603 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2604 "Record number not allowed for stream access "
2610 /* Bugware for badly written mixed C-Fortran I/O. */
2611 flush_if_preconnected(dtp
->u
.p
.current_unit
->s
);
2613 dtp
->u
.p
.current_unit
->mode
= dtp
->u
.p
.mode
;
2615 /* Set the maximum position reached from the previous I/O operation. This
2616 could be greater than zero from a previous non-advancing write. */
2617 dtp
->u
.p
.max_pos
= dtp
->u
.p
.current_unit
->saved_pos
;
2622 /* Set up the subroutine that will handle the transfers. */
2626 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
)
2627 dtp
->u
.p
.transfer
= unformatted_read
;
2630 if ((cf
& IOPARM_DT_LIST_FORMAT
) != 0)
2631 dtp
->u
.p
.transfer
= list_formatted_read
;
2633 dtp
->u
.p
.transfer
= formatted_transfer
;
2638 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
)
2639 dtp
->u
.p
.transfer
= unformatted_write
;
2642 if ((cf
& IOPARM_DT_LIST_FORMAT
) != 0)
2643 dtp
->u
.p
.transfer
= list_formatted_write
;
2645 dtp
->u
.p
.transfer
= formatted_transfer
;
2649 /* Make sure that we don't do a read after a nonadvancing write. */
2653 if (dtp
->u
.p
.current_unit
->read_bad
&& !is_stream_io (dtp
))
2655 generate_error (&dtp
->common
, LIBERROR_BAD_OPTION
,
2656 "Cannot READ after a nonadvancing WRITE");
2662 if (dtp
->u
.p
.advance_status
== ADVANCE_YES
&& !dtp
->u
.p
.seen_dollar
)
2663 dtp
->u
.p
.current_unit
->read_bad
= 1;
2666 /* Start the data transfer if we are doing a formatted transfer. */
2667 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
2668 && ((cf
& (IOPARM_DT_LIST_FORMAT
| IOPARM_DT_HAS_NAMELIST_NAME
)) == 0)
2669 && dtp
->u
.p
.ionml
== NULL
)
2670 formatted_transfer (dtp
, 0, NULL
, 0, 0, 1);
2673 /* Initialize an array_loop_spec given the array descriptor. The function
2674 returns the index of the last element of the array, and also returns
2675 starting record, where the first I/O goes to (necessary in case of
2676 negative strides). */
2679 init_loop_spec (gfc_array_char
*desc
, array_loop_spec
*ls
,
2680 gfc_offset
*start_record
)
2682 int rank
= GFC_DESCRIPTOR_RANK(desc
);
2691 for (i
=0; i
<rank
; i
++)
2693 ls
[i
].idx
= GFC_DESCRIPTOR_LBOUND(desc
,i
);
2694 ls
[i
].start
= GFC_DESCRIPTOR_LBOUND(desc
,i
);
2695 ls
[i
].end
= GFC_DESCRIPTOR_UBOUND(desc
,i
);
2696 ls
[i
].step
= GFC_DESCRIPTOR_STRIDE(desc
,i
);
2697 empty
= empty
|| (GFC_DESCRIPTOR_UBOUND(desc
,i
)
2698 < GFC_DESCRIPTOR_LBOUND(desc
,i
));
2700 if (GFC_DESCRIPTOR_STRIDE(desc
,i
) > 0)
2702 index
+= (GFC_DESCRIPTOR_EXTENT(desc
,i
) - 1)
2703 * GFC_DESCRIPTOR_STRIDE(desc
,i
);
2707 index
-= (GFC_DESCRIPTOR_EXTENT(desc
,i
) - 1)
2708 * GFC_DESCRIPTOR_STRIDE(desc
,i
);
2709 *start_record
-= (GFC_DESCRIPTOR_EXTENT(desc
,i
) - 1)
2710 * GFC_DESCRIPTOR_STRIDE(desc
,i
);
2720 /* Determine the index to the next record in an internal unit array by
2721 by incrementing through the array_loop_spec. */
2724 next_array_record (st_parameter_dt
*dtp
, array_loop_spec
*ls
, int *finished
)
2732 for (i
= 0; i
< dtp
->u
.p
.current_unit
->rank
; i
++)
2737 if (ls
[i
].idx
> ls
[i
].end
)
2739 ls
[i
].idx
= ls
[i
].start
;
2745 index
= index
+ (ls
[i
].idx
- ls
[i
].start
) * ls
[i
].step
;
2755 /* Skip to the end of the current record, taking care of an optional
2756 record marker of size bytes. If the file is not seekable, we
2757 read chunks of size MAX_READ until we get to the right
2761 skip_record (st_parameter_dt
*dtp
, ssize_t bytes
)
2763 ssize_t rlength
, readb
;
2764 static const ssize_t MAX_READ
= 4096;
2767 dtp
->u
.p
.current_unit
->bytes_left_subrecord
+= bytes
;
2768 if (dtp
->u
.p
.current_unit
->bytes_left_subrecord
== 0)
2771 if (is_seekable (dtp
->u
.p
.current_unit
->s
))
2773 /* Direct access files do not generate END conditions,
2775 if (sseek (dtp
->u
.p
.current_unit
->s
,
2776 dtp
->u
.p
.current_unit
->bytes_left_subrecord
, SEEK_CUR
) < 0)
2777 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2779 dtp
->u
.p
.current_unit
->bytes_left_subrecord
= 0;
2782 { /* Seek by reading data. */
2783 while (dtp
->u
.p
.current_unit
->bytes_left_subrecord
> 0)
2786 (MAX_READ
< dtp
->u
.p
.current_unit
->bytes_left_subrecord
) ?
2787 MAX_READ
: dtp
->u
.p
.current_unit
->bytes_left_subrecord
;
2789 readb
= sread (dtp
->u
.p
.current_unit
->s
, p
, rlength
);
2792 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2796 dtp
->u
.p
.current_unit
->bytes_left_subrecord
-= readb
;
2803 /* Advance to the next record reading unformatted files, taking
2804 care of subrecords. If complete_record is nonzero, we loop
2805 until all subrecords are cleared. */
2808 next_record_r_unf (st_parameter_dt
*dtp
, int complete_record
)
2812 bytes
= compile_options
.record_marker
== 0 ?
2813 sizeof (GFC_INTEGER_4
) : compile_options
.record_marker
;
2818 /* Skip over tail */
2820 skip_record (dtp
, bytes
);
2822 if ( ! (complete_record
&& dtp
->u
.p
.current_unit
->continued
))
2830 static inline gfc_offset
2831 min_off (gfc_offset a
, gfc_offset b
)
2833 return (a
< b
? a
: b
);
2837 /* Space to the next record for read mode. */
2840 next_record_r (st_parameter_dt
*dtp
, int done
)
2847 switch (current_mode (dtp
))
2849 /* No records in unformatted STREAM I/O. */
2850 case UNFORMATTED_STREAM
:
2853 case UNFORMATTED_SEQUENTIAL
:
2854 next_record_r_unf (dtp
, 1);
2855 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2858 case FORMATTED_DIRECT
:
2859 case UNFORMATTED_DIRECT
:
2860 skip_record (dtp
, dtp
->u
.p
.current_unit
->bytes_left
);
2863 case FORMATTED_STREAM
:
2864 case FORMATTED_SEQUENTIAL
:
2865 /* read_sf has already terminated input because of an '\n', or
2867 if (dtp
->u
.p
.sf_seen_eor
)
2869 dtp
->u
.p
.sf_seen_eor
= 0;
2873 if (is_internal_unit (dtp
))
2875 if (is_array_io (dtp
))
2879 record
= next_array_record (dtp
, dtp
->u
.p
.current_unit
->ls
,
2881 if (!done
&& finished
)
2884 /* Now seek to this record. */
2885 record
= record
* dtp
->u
.p
.current_unit
->recl
;
2886 if (sseek (dtp
->u
.p
.current_unit
->s
, record
, SEEK_SET
) < 0)
2888 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
2891 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2895 bytes_left
= (int) dtp
->u
.p
.current_unit
->bytes_left
;
2896 bytes_left
= min_off (bytes_left
,
2897 file_length (dtp
->u
.p
.current_unit
->s
)
2898 - stell (dtp
->u
.p
.current_unit
->s
));
2899 if (sseek (dtp
->u
.p
.current_unit
->s
,
2900 bytes_left
, SEEK_CUR
) < 0)
2902 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
2905 dtp
->u
.p
.current_unit
->bytes_left
2906 = dtp
->u
.p
.current_unit
->recl
;
2915 cc
= fbuf_getc (dtp
->u
.p
.current_unit
);
2919 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
2922 if (is_stream_io (dtp
)
2923 || dtp
->u
.p
.current_unit
->pad_status
== PAD_NO
2924 || dtp
->u
.p
.current_unit
->bytes_left
2925 == dtp
->u
.p
.current_unit
->recl
)
2931 if (is_stream_io (dtp
))
2932 dtp
->u
.p
.current_unit
->strm_pos
++;
2943 /* Small utility function to write a record marker, taking care of
2944 byte swapping and of choosing the correct size. */
2947 write_us_marker (st_parameter_dt
*dtp
, const gfc_offset buf
)
2952 char p
[sizeof (GFC_INTEGER_8
)];
2954 if (compile_options
.record_marker
== 0)
2955 len
= sizeof (GFC_INTEGER_4
);
2957 len
= compile_options
.record_marker
;
2959 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
2960 if (likely (dtp
->u
.p
.current_unit
->flags
.convert
== GFC_CONVERT_NATIVE
))
2964 case sizeof (GFC_INTEGER_4
):
2966 return swrite (dtp
->u
.p
.current_unit
->s
, &buf4
, len
);
2969 case sizeof (GFC_INTEGER_8
):
2971 return swrite (dtp
->u
.p
.current_unit
->s
, &buf8
, len
);
2975 runtime_error ("Illegal value for record marker");
2983 case sizeof (GFC_INTEGER_4
):
2985 reverse_memcpy (p
, &buf4
, sizeof (GFC_INTEGER_4
));
2986 return swrite (dtp
->u
.p
.current_unit
->s
, p
, len
);
2989 case sizeof (GFC_INTEGER_8
):
2991 reverse_memcpy (p
, &buf8
, sizeof (GFC_INTEGER_8
));
2992 return swrite (dtp
->u
.p
.current_unit
->s
, p
, len
);
2996 runtime_error ("Illegal value for record marker");
3003 /* Position to the next (sub)record in write mode for
3004 unformatted sequential files. */
3007 next_record_w_unf (st_parameter_dt
*dtp
, int next_subrecord
)
3009 gfc_offset m
, m_write
, record_marker
;
3011 /* Bytes written. */
3012 m
= dtp
->u
.p
.current_unit
->recl_subrecord
3013 - dtp
->u
.p
.current_unit
->bytes_left_subrecord
;
3015 /* Write the length tail. If we finish a record containing
3016 subrecords, we write out the negative length. */
3018 if (dtp
->u
.p
.current_unit
->continued
)
3023 if (unlikely (write_us_marker (dtp
, m_write
) < 0))
3026 if (compile_options
.record_marker
== 0)
3027 record_marker
= sizeof (GFC_INTEGER_4
);
3029 record_marker
= compile_options
.record_marker
;
3031 /* Seek to the head and overwrite the bogus length with the real
3034 if (unlikely (sseek (dtp
->u
.p
.current_unit
->s
, - m
- 2 * record_marker
,
3043 if (unlikely (write_us_marker (dtp
, m_write
) < 0))
3046 /* Seek past the end of the current record. */
3048 if (unlikely (sseek (dtp
->u
.p
.current_unit
->s
, m
+ record_marker
,
3055 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
3061 /* Utility function like memset() but operating on streams. Return
3062 value is same as for POSIX write(). */
3065 sset (stream
* s
, int c
, ssize_t nbyte
)
3067 static const int WRITE_CHUNK
= 256;
3068 char p
[WRITE_CHUNK
];
3069 ssize_t bytes_left
, trans
;
3071 if (nbyte
< WRITE_CHUNK
)
3072 memset (p
, c
, nbyte
);
3074 memset (p
, c
, WRITE_CHUNK
);
3077 while (bytes_left
> 0)
3079 trans
= (bytes_left
< WRITE_CHUNK
) ? bytes_left
: WRITE_CHUNK
;
3080 trans
= swrite (s
, p
, trans
);
3083 bytes_left
-= trans
;
3086 return nbyte
- bytes_left
;
3089 /* Position to the next record in write mode. */
3092 next_record_w (st_parameter_dt
*dtp
, int done
)
3094 gfc_offset m
, record
, max_pos
;
3097 /* Zero counters for X- and T-editing. */
3098 max_pos
= dtp
->u
.p
.max_pos
;
3099 dtp
->u
.p
.max_pos
= dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
3101 switch (current_mode (dtp
))
3103 /* No records in unformatted STREAM I/O. */
3104 case UNFORMATTED_STREAM
:
3107 case FORMATTED_DIRECT
:
3108 if (dtp
->u
.p
.current_unit
->bytes_left
== 0)
3111 fbuf_seek (dtp
->u
.p
.current_unit
, 0, SEEK_END
);
3112 fbuf_flush (dtp
->u
.p
.current_unit
, WRITING
);
3113 if (sset (dtp
->u
.p
.current_unit
->s
, ' ',
3114 dtp
->u
.p
.current_unit
->bytes_left
)
3115 != dtp
->u
.p
.current_unit
->bytes_left
)
3120 case UNFORMATTED_DIRECT
:
3121 if (dtp
->u
.p
.current_unit
->bytes_left
> 0)
3123 length
= (int) dtp
->u
.p
.current_unit
->bytes_left
;
3124 if (sset (dtp
->u
.p
.current_unit
->s
, 0, length
) != length
)
3129 case UNFORMATTED_SEQUENTIAL
:
3130 next_record_w_unf (dtp
, 0);
3131 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
3134 case FORMATTED_STREAM
:
3135 case FORMATTED_SEQUENTIAL
:
3137 if (is_internal_unit (dtp
))
3139 if (is_array_io (dtp
))
3143 length
= (int) dtp
->u
.p
.current_unit
->bytes_left
;
3145 /* If the farthest position reached is greater than current
3146 position, adjust the position and set length to pad out
3147 whats left. Otherwise just pad whats left.
3148 (for character array unit) */
3149 m
= dtp
->u
.p
.current_unit
->recl
3150 - dtp
->u
.p
.current_unit
->bytes_left
;
3153 length
= (int) (max_pos
- m
);
3154 if (sseek (dtp
->u
.p
.current_unit
->s
,
3155 length
, SEEK_CUR
) < 0)
3157 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
3160 length
= (int) (dtp
->u
.p
.current_unit
->recl
- max_pos
);
3163 if (sset (dtp
->u
.p
.current_unit
->s
, ' ', length
) != length
)
3165 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
3169 /* Now that the current record has been padded out,
3170 determine where the next record in the array is. */
3171 record
= next_array_record (dtp
, dtp
->u
.p
.current_unit
->ls
,
3174 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
3176 /* Now seek to this record */
3177 record
= record
* dtp
->u
.p
.current_unit
->recl
;
3179 if (sseek (dtp
->u
.p
.current_unit
->s
, record
, SEEK_SET
) < 0)
3181 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
3185 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
3191 /* If this is the last call to next_record move to the farthest
3192 position reached and set length to pad out the remainder
3193 of the record. (for character scaler unit) */
3196 m
= dtp
->u
.p
.current_unit
->recl
3197 - dtp
->u
.p
.current_unit
->bytes_left
;
3200 length
= (int) (max_pos
- m
);
3201 if (sseek (dtp
->u
.p
.current_unit
->s
,
3202 length
, SEEK_CUR
) < 0)
3204 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
3207 length
= (int) (dtp
->u
.p
.current_unit
->recl
- max_pos
);
3210 length
= (int) dtp
->u
.p
.current_unit
->bytes_left
;
3213 if (sset (dtp
->u
.p
.current_unit
->s
, ' ', length
) != length
)
3215 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
3227 fbuf_seek (dtp
->u
.p
.current_unit
, 0, SEEK_END
);
3228 char * p
= fbuf_alloc (dtp
->u
.p
.current_unit
, len
);
3235 if (is_stream_io (dtp
))
3237 dtp
->u
.p
.current_unit
->strm_pos
+= len
;
3238 if (dtp
->u
.p
.current_unit
->strm_pos
3239 < file_length (dtp
->u
.p
.current_unit
->s
))
3240 unit_truncate (dtp
->u
.p
.current_unit
,
3241 dtp
->u
.p
.current_unit
->strm_pos
- 1,
3249 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
3254 /* Position to the next record, which means moving to the end of the
3255 current record. This can happen under several different
3256 conditions. If the done flag is not set, we get ready to process
3260 next_record (st_parameter_dt
*dtp
, int done
)
3262 gfc_offset fp
; /* File position. */
3264 dtp
->u
.p
.current_unit
->read_bad
= 0;
3266 if (dtp
->u
.p
.mode
== READING
)
3267 next_record_r (dtp
, done
);
3269 next_record_w (dtp
, done
);
3271 if (!is_stream_io (dtp
))
3273 /* Keep position up to date for INQUIRE */
3275 update_position (dtp
->u
.p
.current_unit
);
3277 dtp
->u
.p
.current_unit
->current_record
= 0;
3278 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
3280 fp
= stell (dtp
->u
.p
.current_unit
->s
);
3281 /* Calculate next record, rounding up partial records. */
3282 dtp
->u
.p
.current_unit
->last_record
=
3283 (fp
+ dtp
->u
.p
.current_unit
->recl
- 1) /
3284 dtp
->u
.p
.current_unit
->recl
;
3287 dtp
->u
.p
.current_unit
->last_record
++;
3293 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
3297 /* Finalize the current data transfer. For a nonadvancing transfer,
3298 this means advancing to the next record. For internal units close the
3299 stream associated with the unit. */
3302 finalize_transfer (st_parameter_dt
*dtp
)
3305 GFC_INTEGER_4 cf
= dtp
->common
.flags
;
3307 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
3308 *dtp
->size
= dtp
->u
.p
.size_used
;
3310 if (dtp
->u
.p
.eor_condition
)
3312 generate_error (&dtp
->common
, LIBERROR_EOR
, NULL
);
3316 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
3318 if (dtp
->u
.p
.current_unit
&& current_mode (dtp
) == UNFORMATTED_SEQUENTIAL
)
3319 dtp
->u
.p
.current_unit
->current_record
= 0;
3323 if ((dtp
->u
.p
.ionml
!= NULL
)
3324 && (cf
& IOPARM_DT_HAS_NAMELIST_NAME
) != 0)
3326 if ((cf
& IOPARM_DT_NAMELIST_READ_MODE
) != 0)
3327 namelist_read (dtp
);
3329 namelist_write (dtp
);
3332 dtp
->u
.p
.transfer
= NULL
;
3333 if (dtp
->u
.p
.current_unit
== NULL
)
3336 dtp
->u
.p
.eof_jump
= &eof_jump
;
3337 if (setjmp (eof_jump
))
3339 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
3343 if ((cf
& IOPARM_DT_LIST_FORMAT
) != 0 && dtp
->u
.p
.mode
== READING
)
3345 finish_list_read (dtp
);
3349 if (dtp
->u
.p
.mode
== WRITING
)
3350 dtp
->u
.p
.current_unit
->previous_nonadvancing_write
3351 = dtp
->u
.p
.advance_status
== ADVANCE_NO
;
3353 if (is_stream_io (dtp
))
3355 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
3356 && dtp
->u
.p
.advance_status
!= ADVANCE_NO
)
3357 next_record (dtp
, 1);
3362 dtp
->u
.p
.current_unit
->current_record
= 0;
3364 if (!is_internal_unit (dtp
) && dtp
->u
.p
.seen_dollar
)
3366 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
3367 dtp
->u
.p
.seen_dollar
= 0;
3371 /* For non-advancing I/O, save the current maximum position for use in the
3372 next I/O operation if needed. */
3373 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
)
3375 int bytes_written
= (int) (dtp
->u
.p
.current_unit
->recl
3376 - dtp
->u
.p
.current_unit
->bytes_left
);
3377 dtp
->u
.p
.current_unit
->saved_pos
=
3378 dtp
->u
.p
.max_pos
> 0 ? dtp
->u
.p
.max_pos
- bytes_written
: 0;
3379 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
3382 else if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
3383 && dtp
->u
.p
.mode
== WRITING
&& !is_internal_unit (dtp
))
3384 fbuf_seek (dtp
->u
.p
.current_unit
, 0, SEEK_END
);
3386 dtp
->u
.p
.current_unit
->saved_pos
= 0;
3388 next_record (dtp
, 1);
3391 /* Transfer function for IOLENGTH. It doesn't actually do any
3392 data transfer, it just updates the length counter. */
3395 iolength_transfer (st_parameter_dt
*dtp
, bt type
__attribute__((unused
)),
3396 void *dest
__attribute__ ((unused
)),
3397 int kind
__attribute__((unused
)),
3398 size_t size
, size_t nelems
)
3400 if ((dtp
->common
.flags
& IOPARM_DT_HAS_IOLENGTH
) != 0)
3401 *dtp
->iolength
+= (GFC_IO_INT
) (size
* nelems
);
3405 /* Initialize the IOLENGTH data transfer. This function is in essence
3406 a very much simplified version of data_transfer_init(), because it
3407 doesn't have to deal with units at all. */
3410 iolength_transfer_init (st_parameter_dt
*dtp
)
3412 if ((dtp
->common
.flags
& IOPARM_DT_HAS_IOLENGTH
) != 0)
3415 memset (&dtp
->u
.p
, 0, sizeof (dtp
->u
.p
));
3417 /* Set up the subroutine that will handle the transfers. */
3419 dtp
->u
.p
.transfer
= iolength_transfer
;
3423 /* Library entry point for the IOLENGTH form of the INQUIRE
3424 statement. The IOLENGTH form requires no I/O to be performed, but
3425 it must still be a runtime library call so that we can determine
3426 the iolength for dynamic arrays and such. */
3428 extern void st_iolength (st_parameter_dt
*);
3429 export_proto(st_iolength
);
3432 st_iolength (st_parameter_dt
*dtp
)
3434 library_start (&dtp
->common
);
3435 iolength_transfer_init (dtp
);
3438 extern void st_iolength_done (st_parameter_dt
*);
3439 export_proto(st_iolength_done
);
3442 st_iolength_done (st_parameter_dt
*dtp
__attribute__((unused
)))
3449 /* The READ statement. */
3451 extern void st_read (st_parameter_dt
*);
3452 export_proto(st_read
);
3455 st_read (st_parameter_dt
*dtp
)
3457 library_start (&dtp
->common
);
3459 data_transfer_init (dtp
, 1);
3462 extern void st_read_done (st_parameter_dt
*);
3463 export_proto(st_read_done
);
3466 st_read_done (st_parameter_dt
*dtp
)
3468 finalize_transfer (dtp
);
3469 if (is_internal_unit (dtp
) || dtp
->u
.p
.format_not_saved
)
3470 free_format_data (dtp
->u
.p
.fmt
);
3472 if (dtp
->u
.p
.current_unit
!= NULL
)
3473 unlock_unit (dtp
->u
.p
.current_unit
);
3475 free_internal_unit (dtp
);
3480 extern void st_write (st_parameter_dt
*);
3481 export_proto(st_write
);
3484 st_write (st_parameter_dt
*dtp
)
3486 library_start (&dtp
->common
);
3487 data_transfer_init (dtp
, 0);
3490 extern void st_write_done (st_parameter_dt
*);
3491 export_proto(st_write_done
);
3494 st_write_done (st_parameter_dt
*dtp
)
3496 finalize_transfer (dtp
);
3498 /* Deal with endfile conditions associated with sequential files. */
3500 if (dtp
->u
.p
.current_unit
!= NULL
3501 && dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
3502 switch (dtp
->u
.p
.current_unit
->endfile
)
3504 case AT_ENDFILE
: /* Remain at the endfile record. */
3508 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
; /* Just at it now. */
3512 /* Get rid of whatever is after this record. */
3513 if (!is_internal_unit (dtp
))
3514 unit_truncate (dtp
->u
.p
.current_unit
,
3515 stell (dtp
->u
.p
.current_unit
->s
),
3517 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
3521 if (is_internal_unit (dtp
) || dtp
->u
.p
.format_not_saved
)
3522 free_format_data (dtp
->u
.p
.fmt
);
3524 if (dtp
->u
.p
.current_unit
!= NULL
)
3525 unlock_unit (dtp
->u
.p
.current_unit
);
3527 free_internal_unit (dtp
);
3533 /* F2003: This is a stub for the runtime portion of the WAIT statement. */
3535 st_wait (st_parameter_wait
*wtp
__attribute__((unused
)))
3540 /* Receives the scalar information for namelist objects and stores it
3541 in a linked list of namelist_info types. */
3543 extern void st_set_nml_var (st_parameter_dt
*dtp
, void *, char *,
3544 GFC_INTEGER_4
, gfc_charlen_type
, GFC_INTEGER_4
);
3545 export_proto(st_set_nml_var
);
3549 st_set_nml_var (st_parameter_dt
*dtp
, void * var_addr
, char * var_name
,
3550 GFC_INTEGER_4 len
, gfc_charlen_type string_length
,
3551 GFC_INTEGER_4 dtype
)
3553 namelist_info
*t1
= NULL
;
3555 size_t var_name_len
= strlen (var_name
);
3557 nml
= (namelist_info
*) get_mem (sizeof (namelist_info
));
3559 nml
->mem_pos
= var_addr
;
3561 nml
->var_name
= (char*) get_mem (var_name_len
+ 1);
3562 memcpy (nml
->var_name
, var_name
, var_name_len
);
3563 nml
->var_name
[var_name_len
] = '\0';
3565 nml
->len
= (int) len
;
3566 nml
->string_length
= (index_type
) string_length
;
3568 nml
->var_rank
= (int) (dtype
& GFC_DTYPE_RANK_MASK
);
3569 nml
->size
= (index_type
) (dtype
>> GFC_DTYPE_SIZE_SHIFT
);
3570 nml
->type
= (bt
) ((dtype
& GFC_DTYPE_TYPE_MASK
) >> GFC_DTYPE_TYPE_SHIFT
);
3572 if (nml
->var_rank
> 0)
3574 nml
->dim
= (descriptor_dimension
*)
3575 get_mem (nml
->var_rank
* sizeof (descriptor_dimension
));
3576 nml
->ls
= (array_loop_spec
*)
3577 get_mem (nml
->var_rank
* sizeof (array_loop_spec
));
3587 if ((dtp
->common
.flags
& IOPARM_DT_IONML_SET
) == 0)
3589 dtp
->common
.flags
|= IOPARM_DT_IONML_SET
;
3590 dtp
->u
.p
.ionml
= nml
;
3594 for (t1
= dtp
->u
.p
.ionml
; t1
->next
; t1
= t1
->next
);
3599 /* Store the dimensional information for the namelist object. */
3600 extern void st_set_nml_var_dim (st_parameter_dt
*, GFC_INTEGER_4
,
3601 index_type
, index_type
,
3603 export_proto(st_set_nml_var_dim
);
3606 st_set_nml_var_dim (st_parameter_dt
*dtp
, GFC_INTEGER_4 n_dim
,
3607 index_type stride
, index_type lbound
,
3610 namelist_info
* nml
;
3615 for (nml
= dtp
->u
.p
.ionml
; nml
->next
; nml
= nml
->next
);
3617 GFC_DIMENSION_SET(nml
->dim
[n
],lbound
,ubound
,stride
);
3620 /* Reverse memcpy - used for byte swapping. */
3622 void reverse_memcpy (void *dest
, const void *src
, size_t n
)
3628 s
= (char *) src
+ n
- 1;
3630 /* Write with ascending order - this is likely faster
3631 on modern architectures because of write combining. */
3637 /* Once upon a time, a poor innocent Fortran program was reading a
3638 file, when suddenly it hit the end-of-file (EOF). Unfortunately
3639 the OS doesn't tell whether we're at the EOF or whether we already
3640 went past it. Luckily our hero, libgfortran, keeps track of this.
3641 Call this function when you detect an EOF condition. See Section
3645 hit_eof (st_parameter_dt
* dtp
)
3647 dtp
->u
.p
.current_unit
->flags
.position
= POSITION_APPEND
;
3649 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
3650 switch (dtp
->u
.p
.current_unit
->endfile
)
3654 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
3655 if (!is_internal_unit (dtp
))
3657 dtp
->u
.p
.current_unit
->endfile
= AFTER_ENDFILE
;
3658 dtp
->u
.p
.current_unit
->current_record
= 0;
3661 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
3665 generate_error (&dtp
->common
, LIBERROR_ENDFILE
, NULL
);
3666 dtp
->u
.p
.current_unit
->current_record
= 0;
3671 /* Non-sequential files don't have an ENDFILE record, so we
3672 can't be at AFTER_ENDFILE. */
3673 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
3674 generate_error (&dtp
->common
, LIBERROR_END
, NULL
);
3675 dtp
->u
.p
.current_unit
->current_record
= 0;