1 /* Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
3 Namelist transfer functions contributed by Paul Thomas
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with Libgfortran; see the file COPYING. If not, write to
28 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA. */
32 /* transfer.c -- Top level handling of data transfer statements. */
37 #include "libgfortran.h"
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
59 These subroutines do not return status.
61 The last call is a call to st_[read|write]_done(). While
62 something can easily go wrong with the initial st_read() or
63 st_write(), an error inhibits any data from actually being
66 extern void transfer_integer (st_parameter_dt
*, void *, int);
67 export_proto(transfer_integer
);
69 extern void transfer_real (st_parameter_dt
*, void *, int);
70 export_proto(transfer_real
);
72 extern void transfer_logical (st_parameter_dt
*, void *, int);
73 export_proto(transfer_logical
);
75 extern void transfer_character (st_parameter_dt
*, void *, int);
76 export_proto(transfer_character
);
78 extern void transfer_complex (st_parameter_dt
*, void *, int);
79 export_proto(transfer_complex
);
81 extern void transfer_array (st_parameter_dt
*, gfc_array_char
*, int,
83 export_proto(transfer_array
);
85 static void us_read (st_parameter_dt
*, int);
86 static void us_write (st_parameter_dt
*, int);
87 static void next_record_r_unf (st_parameter_dt
*, int);
88 static void next_record_w_unf (st_parameter_dt
*, int);
90 static const st_option advance_opt
[] = {
98 { FORMATTED_SEQUENTIAL
, UNFORMATTED_SEQUENTIAL
,
99 FORMATTED_DIRECT
, UNFORMATTED_DIRECT
, FORMATTED_STREAM
, UNFORMATTED_STREAM
105 current_mode (st_parameter_dt
*dtp
)
109 m
= FORM_UNSPECIFIED
;
111 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
113 m
= dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
?
114 FORMATTED_DIRECT
: UNFORMATTED_DIRECT
;
116 else if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
118 m
= dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
?
119 FORMATTED_SEQUENTIAL
: UNFORMATTED_SEQUENTIAL
;
121 else if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_STREAM
)
123 m
= dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
?
124 FORMATTED_STREAM
: UNFORMATTED_STREAM
;
131 /* Mid level data transfer statements. These subroutines do reading
132 and writing in the style of salloc_r()/salloc_w() within the
135 /* When reading sequential formatted records we have a problem. We
136 don't know how long the line is until we read the trailing newline,
137 and we don't want to read too much. If we read too much, we might
138 have to do a physical seek backwards depending on how much data is
139 present, and devices like terminals aren't seekable and would cause
142 Given this, the solution is to read a byte at a time, stopping if
143 we hit the newline. For small allocations, we use a static buffer.
144 For larger allocations, we are forced to allocate memory on the
145 heap. Hopefully this won't happen very often. */
148 read_sf (st_parameter_dt
*dtp
, int *length
, int no_error
)
151 int n
, readlen
, crlf
;
154 if (*length
> SCRATCH_SIZE
)
155 dtp
->u
.p
.line_buffer
= get_mem (*length
);
156 p
= base
= dtp
->u
.p
.line_buffer
;
158 /* If we have seen an eor previously, return a length of 0. The
159 caller is responsible for correctly padding the input field. */
160 if (dtp
->u
.p
.sf_seen_eor
)
171 if (is_internal_unit (dtp
))
173 /* readlen may be modified inside salloc_r if
174 is_internal_unit (dtp) is true. */
178 q
= salloc_r (dtp
->u
.p
.current_unit
->s
, &readlen
);
182 /* If we have a line without a terminating \n, drop through to
184 if (readlen
< 1 && n
== 0)
188 generate_error (&dtp
->common
, ERROR_END
, NULL
);
192 if (readlen
< 1 || *q
== '\n' || *q
== '\r')
194 /* Unexpected end of line. */
196 /* If we see an EOR during non-advancing I/O, we need to skip
197 the rest of the I/O statement. Set the corresponding flag. */
198 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
|| dtp
->u
.p
.seen_dollar
)
199 dtp
->u
.p
.eor_condition
= 1;
202 /* If we encounter a CR, it might be a CRLF. */
203 if (*q
== '\r') /* Probably a CRLF */
206 pos
= stream_offset (dtp
->u
.p
.current_unit
->s
);
207 q
= salloc_r (dtp
->u
.p
.current_unit
->s
, &readlen
);
208 if (*q
!= '\n' && readlen
== 1) /* Not a CRLF after all. */
209 sseek (dtp
->u
.p
.current_unit
->s
, pos
);
214 /* Without padding, terminate the I/O statement without assigning
215 the value. With padding, the value still needs to be assigned,
216 so we can just continue with a short read. */
217 if (dtp
->u
.p
.current_unit
->flags
.pad
== PAD_NO
)
221 generate_error (&dtp
->common
, ERROR_EOR
, NULL
);
226 dtp
->u
.p
.sf_seen_eor
= (crlf
? 2 : 1);
229 /* Short circuit the read if a comma is found during numeric input.
230 The flag is set to zero during character reads so that commas in
231 strings are not ignored */
233 if (dtp
->u
.p
.sf_read_comma
== 1)
235 notify_std (&dtp
->common
, GFC_STD_GNU
,
236 "Comma in formatted numeric read.");
243 dtp
->u
.p
.sf_seen_eor
= 0;
246 dtp
->u
.p
.current_unit
->bytes_left
-= *length
;
248 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
249 dtp
->u
.p
.size_used
+= (gfc_offset
) *length
;
255 /* Function for reading the next couple of bytes from the current
256 file, advancing the current position. We return a pointer to a
257 buffer containing the bytes. We return NULL on end of record or
260 If the read is short, then it is because the current record does not
261 have enough data to satisfy the read request and the file was
262 opened with PAD=YES. The caller must assume tailing spaces for
266 read_block (st_parameter_dt
*dtp
, int *length
)
271 if (is_stream_io (dtp
))
273 if (sseek (dtp
->u
.p
.current_unit
->s
,
274 dtp
->u
.p
.current_unit
->strm_pos
- 1) == FAILURE
)
276 generate_error (&dtp
->common
, ERROR_END
, NULL
);
282 if (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) *length
)
284 /* For preconnected units with default record length, set bytes left
285 to unit record length and proceed, otherwise error. */
286 if (dtp
->u
.p
.current_unit
->unit_number
== options
.stdin_unit
287 && dtp
->u
.p
.current_unit
->recl
== DEFAULT_RECL
)
288 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
291 if (dtp
->u
.p
.current_unit
->flags
.pad
== PAD_NO
)
293 /* Not enough data left. */
294 generate_error (&dtp
->common
, ERROR_EOR
, NULL
);
299 if (dtp
->u
.p
.current_unit
->bytes_left
== 0)
301 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
302 generate_error (&dtp
->common
, ERROR_END
, NULL
);
306 *length
= dtp
->u
.p
.current_unit
->bytes_left
;
310 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
&&
311 (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
||
312 dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_STREAM
))
314 source
= read_sf (dtp
, length
, 0);
315 dtp
->u
.p
.current_unit
->strm_pos
+=
316 (gfc_offset
) (*length
+ dtp
->u
.p
.sf_seen_eor
);
319 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) *length
;
322 source
= salloc_r (dtp
->u
.p
.current_unit
->s
, &nread
);
324 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
325 dtp
->u
.p
.size_used
+= (gfc_offset
) nread
;
327 if (nread
!= *length
)
328 { /* Short read, this shouldn't happen. */
329 if (dtp
->u
.p
.current_unit
->flags
.pad
== PAD_YES
)
333 generate_error (&dtp
->common
, ERROR_EOR
, NULL
);
338 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) nread
;
344 /* Reads a block directly into application data space. This is for
345 unformatted files. */
348 read_block_direct (st_parameter_dt
*dtp
, void *buf
, size_t *nbytes
)
350 size_t to_read_record
;
351 size_t have_read_record
;
352 size_t to_read_subrecord
;
353 size_t have_read_subrecord
;
356 if (is_stream_io (dtp
))
358 if (sseek (dtp
->u
.p
.current_unit
->s
,
359 dtp
->u
.p
.current_unit
->strm_pos
- 1) == FAILURE
)
361 generate_error (&dtp
->common
, ERROR_END
, NULL
);
365 to_read_record
= *nbytes
;
366 have_read_record
= to_read_record
;
367 if (sread (dtp
->u
.p
.current_unit
->s
, buf
, &have_read_record
) != 0)
369 generate_error (&dtp
->common
, ERROR_OS
, NULL
);
373 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) have_read_record
;
375 if (to_read_record
!= have_read_record
)
377 /* Short read, e.g. if we hit EOF. For stream files,
378 we have to set the end-of-file condition. */
379 generate_error (&dtp
->common
, ERROR_END
, NULL
);
385 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
387 if (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) *nbytes
)
390 to_read_record
= (size_t) dtp
->u
.p
.current_unit
->bytes_left
;
391 *nbytes
= to_read_record
;
397 to_read_record
= *nbytes
;
400 dtp
->u
.p
.current_unit
->bytes_left
-= to_read_record
;
402 if (sread (dtp
->u
.p
.current_unit
->s
, buf
, &to_read_record
) != 0)
404 generate_error (&dtp
->common
, ERROR_OS
, NULL
);
408 if (to_read_record
!= *nbytes
)
410 /* Short read, e.g. if we hit EOF. Apparently, we read
411 more than was written to the last record. */
412 *nbytes
= to_read_record
;
418 generate_error (&dtp
->common
, ERROR_SHORT_RECORD
, NULL
);
424 /* Unformatted sequential. We loop over the subrecords, reading
425 until the request has been fulfilled or the record has run out
426 of continuation subrecords. */
428 if (dtp
->u
.p
.current_unit
->endfile
== AT_ENDFILE
)
430 generate_error (&dtp
->common
, ERROR_END
, NULL
);
434 /* Check whether we exceed the total record length. */
436 if (dtp
->u
.p
.current_unit
->flags
.has_recl
437 && (*nbytes
> (size_t) dtp
->u
.p
.current_unit
->bytes_left
))
439 to_read_record
= (size_t) dtp
->u
.p
.current_unit
->bytes_left
;
444 to_read_record
= *nbytes
;
447 have_read_record
= 0;
451 if (dtp
->u
.p
.current_unit
->bytes_left_subrecord
452 < (gfc_offset
) to_read_record
)
454 to_read_subrecord
= (size_t) dtp
->u
.p
.current_unit
->bytes_left_subrecord
;
455 to_read_record
-= to_read_subrecord
;
459 to_read_subrecord
= to_read_record
;
463 dtp
->u
.p
.current_unit
->bytes_left_subrecord
-= to_read_subrecord
;
465 have_read_subrecord
= to_read_subrecord
;
466 if (sread (dtp
->u
.p
.current_unit
->s
, buf
+ have_read_record
,
467 &have_read_subrecord
) != 0)
469 generate_error (&dtp
->common
, ERROR_OS
, NULL
);
473 have_read_record
+= have_read_subrecord
;
475 if (to_read_subrecord
!= have_read_subrecord
)
478 /* Short read, e.g. if we hit EOF. This means the record
479 structure has been corrupted, or the trailing record
480 marker would still be present. */
482 *nbytes
= have_read_record
;
483 generate_error (&dtp
->common
, ERROR_CORRUPT_FILE
, NULL
);
487 if (to_read_record
> 0)
489 if (dtp
->u
.p
.current_unit
->continued
)
491 next_record_r_unf (dtp
, 0);
496 /* Let's make sure the file position is correctly pre-positioned
497 for the next read statement. */
499 dtp
->u
.p
.current_unit
->current_record
= 0;
500 next_record_r_unf (dtp
, 0);
501 generate_error (&dtp
->common
, ERROR_SHORT_RECORD
, NULL
);
507 /* Normal exit, the read request has been fulfilled. */
512 dtp
->u
.p
.current_unit
->bytes_left
-= have_read_record
;
515 generate_error (&dtp
->common
, ERROR_SHORT_RECORD
, NULL
);
522 /* Function for writing a block of bytes to the current file at the
523 current position, advancing the file pointer. We are given a length
524 and return a pointer to a buffer that the caller must (completely)
525 fill in. Returns NULL on error. */
528 write_block (st_parameter_dt
*dtp
, int length
)
532 if (is_stream_io (dtp
))
534 if (sseek (dtp
->u
.p
.current_unit
->s
,
535 dtp
->u
.p
.current_unit
->strm_pos
- 1) == FAILURE
)
537 generate_error (&dtp
->common
, ERROR_OS
, NULL
);
543 if (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) length
)
545 /* For preconnected units with default record length, set bytes left
546 to unit record length and proceed, otherwise error. */
547 if ((dtp
->u
.p
.current_unit
->unit_number
== options
.stdout_unit
548 || dtp
->u
.p
.current_unit
->unit_number
== options
.stderr_unit
)
549 && dtp
->u
.p
.current_unit
->recl
== DEFAULT_RECL
)
550 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
553 generate_error (&dtp
->common
, ERROR_EOR
, NULL
);
558 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) length
;
561 dest
= salloc_w (dtp
->u
.p
.current_unit
->s
, &length
);
565 generate_error (&dtp
->common
, ERROR_END
, NULL
);
569 if (is_internal_unit (dtp
) && dtp
->u
.p
.current_unit
->endfile
== AT_ENDFILE
)
570 generate_error (&dtp
->common
, ERROR_END
, NULL
);
572 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
573 dtp
->u
.p
.size_used
+= (gfc_offset
) length
;
575 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) length
;
581 /* High level interface to swrite(), taking care of errors. This is only
582 called for unformatted files. There are three cases to consider:
583 Stream I/O, unformatted direct, unformatted sequential. */
586 write_buf (st_parameter_dt
*dtp
, void *buf
, size_t nbytes
)
589 size_t have_written
, to_write_subrecord
;
595 if (is_stream_io (dtp
))
597 if (sseek (dtp
->u
.p
.current_unit
->s
,
598 dtp
->u
.p
.current_unit
->strm_pos
- 1) == FAILURE
)
600 generate_error (&dtp
->common
, ERROR_OS
, NULL
);
604 if (swrite (dtp
->u
.p
.current_unit
->s
, buf
, &nbytes
) != 0)
606 generate_error (&dtp
->common
, ERROR_OS
, NULL
);
610 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) nbytes
;
615 /* Unformatted direct access. */
617 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
619 if (dtp
->u
.p
.current_unit
->bytes_left
< (gfc_offset
) nbytes
)
621 generate_error (&dtp
->common
, ERROR_DIRECT_EOR
, NULL
);
625 if (swrite (dtp
->u
.p
.current_unit
->s
, buf
, &nbytes
) != 0)
627 generate_error (&dtp
->common
, ERROR_OS
, NULL
);
631 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) nbytes
;
632 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) nbytes
;
638 /* Unformatted sequential. */
642 if (dtp
->u
.p
.current_unit
->flags
.has_recl
643 && (gfc_offset
) nbytes
> dtp
->u
.p
.current_unit
->bytes_left
)
645 nbytes
= dtp
->u
.p
.current_unit
->bytes_left
;
657 (size_t) dtp
->u
.p
.current_unit
->bytes_left_subrecord
< nbytes
?
658 (size_t) dtp
->u
.p
.current_unit
->bytes_left_subrecord
: nbytes
;
660 dtp
->u
.p
.current_unit
->bytes_left_subrecord
-=
661 (gfc_offset
) to_write_subrecord
;
663 if (swrite (dtp
->u
.p
.current_unit
->s
, buf
+ have_written
,
664 &to_write_subrecord
) != 0)
666 generate_error (&dtp
->common
, ERROR_OS
, NULL
);
670 dtp
->u
.p
.current_unit
->strm_pos
+= (gfc_offset
) to_write_subrecord
;
671 nbytes
-= to_write_subrecord
;
672 have_written
+= to_write_subrecord
;
677 next_record_w_unf (dtp
, 1);
680 dtp
->u
.p
.current_unit
->bytes_left
-= have_written
;
683 generate_error (&dtp
->common
, ERROR_SHORT_RECORD
, NULL
);
690 /* Master function for unformatted reads. */
693 unformatted_read (st_parameter_dt
*dtp
, bt type
,
694 void *dest
, int kind
,
695 size_t size
, size_t nelems
)
699 /* Currently, character implies size=1. */
700 if (dtp
->u
.p
.current_unit
->flags
.convert
== CONVERT_NATIVE
701 || size
== 1 || type
== BT_CHARACTER
)
704 read_block_direct (dtp
, dest
, &sz
);
711 /* Break up complex into its constituent reals. */
712 if (type
== BT_COMPLEX
)
719 /* By now, all complex variables have been split into their
720 constituent reals. For types with padding, we only need to
721 read kind bytes. We don't care about the contents
722 of the padding. If we hit a short record, then sz is
723 adjusted accordingly, making later reads no-ops. */
725 if (type
== BT_REAL
|| type
== BT_COMPLEX
)
726 sz
= size_from_real_kind (kind
);
730 for (i
=0; i
<nelems
; i
++)
732 read_block_direct (dtp
, buffer
, &sz
);
733 reverse_memcpy (p
, buffer
, sz
);
740 /* Master function for unformatted writes. */
743 unformatted_write (st_parameter_dt
*dtp
, bt type
,
744 void *source
, int kind
,
745 size_t size
, size_t nelems
)
747 if (dtp
->u
.p
.current_unit
->flags
.convert
== CONVERT_NATIVE
||
748 size
== 1 || type
== BT_CHARACTER
)
752 write_buf (dtp
, source
, size
);
760 /* Break up complex into its constituent reals. */
761 if (type
== BT_COMPLEX
)
769 /* By now, all complex variables have been split into their
770 constituent reals. For types with padding, we only need to
771 read kind bytes. We don't care about the contents
774 if (type
== BT_REAL
|| type
== BT_COMPLEX
)
775 sz
= size_from_real_kind (kind
);
779 for (i
=0; i
<nelems
; i
++)
781 reverse_memcpy(buffer
, p
, size
);
783 write_buf (dtp
, buffer
, sz
);
789 /* Return a pointer to the name of a type. */
814 internal_error (NULL
, "type_name(): Bad type");
821 /* Write a constant string to the output.
822 This is complicated because the string can have doubled delimiters
823 in it. The length in the format node is the true length. */
826 write_constant_string (st_parameter_dt
*dtp
, const fnode
*f
)
828 char c
, delimiter
, *p
, *q
;
831 length
= f
->u
.string
.length
;
835 p
= write_block (dtp
, length
);
842 for (; length
> 0; length
--)
845 if (c
== delimiter
&& c
!= 'H' && c
!= 'h')
846 q
++; /* Skip the doubled delimiter. */
851 /* Given actual and expected types in a formatted data transfer, make
852 sure they agree. If not, an error message is generated. Returns
853 nonzero if something went wrong. */
856 require_type (st_parameter_dt
*dtp
, bt expected
, bt actual
, const fnode
*f
)
860 if (actual
== expected
)
863 st_sprintf (buffer
, "Expected %s for item %d in formatted transfer, got %s",
864 type_name (expected
), dtp
->u
.p
.item_count
, type_name (actual
));
866 format_error (dtp
, f
, buffer
);
871 /* This subroutine is the main loop for a formatted data transfer
872 statement. It would be natural to implement this as a coroutine
873 with the user program, but C makes that awkward. We loop,
874 processing format elements. When we actually have to transfer
875 data instead of just setting flags, we return control to the user
876 program which calls a subroutine that supplies the address and type
877 of the next element, then comes back here to process it. */
880 formatted_transfer_scalar (st_parameter_dt
*dtp
, bt type
, void *p
, int len
,
883 char scratch
[SCRATCH_SIZE
];
888 int consume_data_flag
;
890 /* Change a complex data item into a pair of reals. */
892 n
= (p
== NULL
) ? 0 : ((type
!= BT_COMPLEX
) ? 1 : 2);
893 if (type
== BT_COMPLEX
)
899 /* If there's an EOR condition, we simulate finalizing the transfer
901 if (dtp
->u
.p
.eor_condition
)
904 /* Set this flag so that commas in reads cause the read to complete before
905 the entire field has been read. The next read field will start right after
906 the comma in the stream. (Set to 0 for character reads). */
907 dtp
->u
.p
.sf_read_comma
= 1;
909 dtp
->u
.p
.line_buffer
= scratch
;
912 /* If reversion has occurred and there is another real data item,
913 then we have to move to the next record. */
914 if (dtp
->u
.p
.reversion_flag
&& n
> 0)
916 dtp
->u
.p
.reversion_flag
= 0;
917 next_record (dtp
, 0);
920 consume_data_flag
= 1 ;
921 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
924 f
= next_format (dtp
);
927 /* No data descriptors left. */
929 generate_error (&dtp
->common
, ERROR_FORMAT
,
930 "Insufficient data descriptors in format after reversion");
934 /* Now discharge T, TR and X movements to the right. This is delayed
935 until a data producing format to suppress trailing spaces. */
938 if (dtp
->u
.p
.mode
== WRITING
&& dtp
->u
.p
.skips
!= 0
939 && ((n
>0 && ( t
== FMT_I
|| t
== FMT_B
|| t
== FMT_O
940 || t
== FMT_Z
|| t
== FMT_F
|| t
== FMT_E
941 || t
== FMT_EN
|| t
== FMT_ES
|| t
== FMT_G
942 || t
== FMT_L
|| t
== FMT_A
|| t
== FMT_D
))
945 if (dtp
->u
.p
.skips
> 0)
947 write_x (dtp
, dtp
->u
.p
.skips
, dtp
->u
.p
.pending_spaces
);
948 dtp
->u
.p
.max_pos
= (int)(dtp
->u
.p
.current_unit
->recl
949 - dtp
->u
.p
.current_unit
->bytes_left
);
951 if (dtp
->u
.p
.skips
< 0)
953 move_pos_offset (dtp
->u
.p
.current_unit
->s
, dtp
->u
.p
.skips
);
954 dtp
->u
.p
.current_unit
->bytes_left
-= (gfc_offset
) dtp
->u
.p
.skips
;
956 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
959 bytes_used
= (int)(dtp
->u
.p
.current_unit
->recl
960 - dtp
->u
.p
.current_unit
->bytes_left
);
962 if (is_stream_io(dtp
))
970 if (require_type (dtp
, BT_INTEGER
, type
, f
))
973 if (dtp
->u
.p
.mode
== READING
)
974 read_decimal (dtp
, f
, p
, len
);
976 write_i (dtp
, f
, p
, len
);
983 if (require_type (dtp
, BT_INTEGER
, type
, f
))
986 if (dtp
->u
.p
.mode
== READING
)
987 read_radix (dtp
, f
, p
, len
, 2);
989 write_b (dtp
, f
, p
, len
);
997 if (dtp
->u
.p
.mode
== READING
)
998 read_radix (dtp
, f
, p
, len
, 8);
1000 write_o (dtp
, f
, p
, len
);
1008 if (dtp
->u
.p
.mode
== READING
)
1009 read_radix (dtp
, f
, p
, len
, 16);
1011 write_z (dtp
, f
, p
, len
);
1019 if (dtp
->u
.p
.mode
== READING
)
1020 read_a (dtp
, f
, p
, len
);
1022 write_a (dtp
, f
, p
, len
);
1030 if (dtp
->u
.p
.mode
== READING
)
1031 read_l (dtp
, f
, p
, len
);
1033 write_l (dtp
, f
, p
, len
);
1040 if (require_type (dtp
, BT_REAL
, type
, f
))
1043 if (dtp
->u
.p
.mode
== READING
)
1044 read_f (dtp
, f
, p
, len
);
1046 write_d (dtp
, f
, p
, len
);
1053 if (require_type (dtp
, BT_REAL
, type
, f
))
1056 if (dtp
->u
.p
.mode
== READING
)
1057 read_f (dtp
, f
, p
, len
);
1059 write_e (dtp
, f
, p
, len
);
1065 if (require_type (dtp
, BT_REAL
, type
, f
))
1068 if (dtp
->u
.p
.mode
== READING
)
1069 read_f (dtp
, f
, p
, len
);
1071 write_en (dtp
, f
, p
, len
);
1078 if (require_type (dtp
, BT_REAL
, type
, f
))
1081 if (dtp
->u
.p
.mode
== READING
)
1082 read_f (dtp
, f
, p
, len
);
1084 write_es (dtp
, f
, p
, len
);
1091 if (require_type (dtp
, BT_REAL
, type
, f
))
1094 if (dtp
->u
.p
.mode
== READING
)
1095 read_f (dtp
, f
, p
, len
);
1097 write_f (dtp
, f
, p
, len
);
1104 if (dtp
->u
.p
.mode
== READING
)
1108 read_decimal (dtp
, f
, p
, len
);
1111 read_l (dtp
, f
, p
, len
);
1114 read_a (dtp
, f
, p
, len
);
1117 read_f (dtp
, f
, p
, len
);
1126 write_i (dtp
, f
, p
, len
);
1129 write_l (dtp
, f
, p
, len
);
1132 write_a (dtp
, f
, p
, len
);
1135 write_d (dtp
, f
, p
, len
);
1139 internal_error (&dtp
->common
,
1140 "formatted_transfer(): Bad type");
1146 consume_data_flag
= 0 ;
1147 if (dtp
->u
.p
.mode
== READING
)
1149 format_error (dtp
, f
, "Constant string in input format");
1152 write_constant_string (dtp
, f
);
1155 /* Format codes that don't transfer data. */
1158 consume_data_flag
= 0;
1160 dtp
->u
.p
.skips
+= f
->u
.n
;
1161 pos
= bytes_used
+ dtp
->u
.p
.skips
- 1;
1162 dtp
->u
.p
.pending_spaces
= pos
- dtp
->u
.p
.max_pos
+ 1;
1164 /* Writes occur just before the switch on f->format, above, so
1165 that trailing blanks are suppressed, unless we are doing a
1166 non-advancing write in which case we want to output the blanks
1168 if (dtp
->u
.p
.mode
== WRITING
1169 && dtp
->u
.p
.advance_status
== ADVANCE_NO
)
1171 write_x (dtp
, dtp
->u
.p
.skips
, dtp
->u
.p
.pending_spaces
);
1172 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1175 if (dtp
->u
.p
.mode
== READING
)
1176 read_x (dtp
, f
->u
.n
);
1182 consume_data_flag
= 0;
1184 if (f
->format
== FMT_TL
)
1187 /* Handle the special case when no bytes have been used yet.
1188 Cannot go below zero. */
1189 if (bytes_used
== 0)
1191 dtp
->u
.p
.pending_spaces
-= f
->u
.n
;
1192 dtp
->u
.p
.skips
-= f
->u
.n
;
1193 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
< 0 ? 0 : dtp
->u
.p
.skips
;
1196 pos
= bytes_used
- f
->u
.n
;
1200 if (dtp
->u
.p
.mode
== READING
)
1203 pos
= f
->u
.n
- dtp
->u
.p
.pending_spaces
- 1;
1206 /* Standard 10.6.1.1: excessive left tabbing is reset to the
1207 left tab limit. We do not check if the position has gone
1208 beyond the end of record because a subsequent tab could
1209 bring us back again. */
1210 pos
= pos
< 0 ? 0 : pos
;
1212 dtp
->u
.p
.skips
= dtp
->u
.p
.skips
+ pos
- bytes_used
;
1213 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
1214 + pos
- dtp
->u
.p
.max_pos
;
1215 dtp
->u
.p
.pending_spaces
= dtp
->u
.p
.pending_spaces
< 0
1216 ? 0 : dtp
->u
.p
.pending_spaces
;
1218 if (dtp
->u
.p
.skips
== 0)
1221 /* Writes occur just before the switch on f->format, above, so that
1222 trailing blanks are suppressed. */
1223 if (dtp
->u
.p
.mode
== READING
)
1225 /* Adjust everything for end-of-record condition */
1226 if (dtp
->u
.p
.sf_seen_eor
&& !is_internal_unit (dtp
))
1228 if (dtp
->u
.p
.sf_seen_eor
== 2)
1230 /* The EOR was a CRLF (two bytes wide). */
1231 dtp
->u
.p
.current_unit
->bytes_left
-= 2;
1232 dtp
->u
.p
.skips
-= 2;
1236 /* The EOR marker was only one byte wide. */
1237 dtp
->u
.p
.current_unit
->bytes_left
--;
1241 dtp
->u
.p
.sf_seen_eor
= 0;
1243 if (dtp
->u
.p
.skips
< 0)
1245 move_pos_offset (dtp
->u
.p
.current_unit
->s
, dtp
->u
.p
.skips
);
1246 dtp
->u
.p
.current_unit
->bytes_left
1247 -= (gfc_offset
) dtp
->u
.p
.skips
;
1248 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1251 read_x (dtp
, dtp
->u
.p
.skips
);
1257 consume_data_flag
= 0 ;
1258 dtp
->u
.p
.sign_status
= SIGN_S
;
1262 consume_data_flag
= 0 ;
1263 dtp
->u
.p
.sign_status
= SIGN_SS
;
1267 consume_data_flag
= 0 ;
1268 dtp
->u
.p
.sign_status
= SIGN_SP
;
1272 consume_data_flag
= 0 ;
1273 dtp
->u
.p
.blank_status
= BLANK_NULL
;
1277 consume_data_flag
= 0 ;
1278 dtp
->u
.p
.blank_status
= BLANK_ZERO
;
1282 consume_data_flag
= 0 ;
1283 dtp
->u
.p
.scale_factor
= f
->u
.k
;
1287 consume_data_flag
= 0 ;
1288 dtp
->u
.p
.seen_dollar
= 1;
1292 consume_data_flag
= 0 ;
1293 dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
1294 next_record (dtp
, 0);
1298 /* A colon descriptor causes us to exit this loop (in
1299 particular preventing another / descriptor from being
1300 processed) unless there is another data item to be
1302 consume_data_flag
= 0 ;
1308 internal_error (&dtp
->common
, "Bad format node");
1311 /* Free a buffer that we had to allocate during a sequential
1312 formatted read of a block that was larger than the static
1315 if (dtp
->u
.p
.line_buffer
!= scratch
)
1317 free_mem (dtp
->u
.p
.line_buffer
);
1318 dtp
->u
.p
.line_buffer
= scratch
;
1321 /* Adjust the item count and data pointer. */
1323 if ((consume_data_flag
> 0) && (n
> 0))
1326 p
= ((char *) p
) + size
;
1329 if (dtp
->u
.p
.mode
== READING
)
1332 pos
= (int)(dtp
->u
.p
.current_unit
->recl
- dtp
->u
.p
.current_unit
->bytes_left
);
1333 dtp
->u
.p
.max_pos
= (dtp
->u
.p
.max_pos
> pos
) ? dtp
->u
.p
.max_pos
: pos
;
1339 /* Come here when we need a data descriptor but don't have one. We
1340 push the current format node back onto the input, then return and
1341 let the user program call us back with the data. */
1343 unget_format (dtp
, f
);
1347 formatted_transfer (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
1348 size_t size
, size_t nelems
)
1355 /* Big loop over all the elements. */
1356 for (elem
= 0; elem
< nelems
; elem
++)
1358 dtp
->u
.p
.item_count
++;
1359 formatted_transfer_scalar (dtp
, type
, tmp
+ size
*elem
, kind
, size
);
1365 /* Data transfer entry points. The type of the data entity is
1366 implicit in the subroutine call. This prevents us from having to
1367 share a common enum with the compiler. */
1370 transfer_integer (st_parameter_dt
*dtp
, void *p
, int kind
)
1372 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1374 dtp
->u
.p
.transfer (dtp
, BT_INTEGER
, p
, kind
, kind
, 1);
1379 transfer_real (st_parameter_dt
*dtp
, void *p
, int kind
)
1382 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1384 size
= size_from_real_kind (kind
);
1385 dtp
->u
.p
.transfer (dtp
, BT_REAL
, p
, kind
, size
, 1);
1390 transfer_logical (st_parameter_dt
*dtp
, void *p
, int kind
)
1392 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1394 dtp
->u
.p
.transfer (dtp
, BT_LOGICAL
, p
, kind
, kind
, 1);
1399 transfer_character (st_parameter_dt
*dtp
, void *p
, int len
)
1401 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1403 /* Currently we support only 1 byte chars, and the library is a bit
1404 confused of character kind vs. length, so we kludge it by setting
1406 dtp
->u
.p
.transfer (dtp
, BT_CHARACTER
, p
, len
, len
, 1);
1411 transfer_complex (st_parameter_dt
*dtp
, void *p
, int kind
)
1414 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1416 size
= size_from_complex_kind (kind
);
1417 dtp
->u
.p
.transfer (dtp
, BT_COMPLEX
, p
, kind
, size
, 1);
1422 transfer_array (st_parameter_dt
*dtp
, gfc_array_char
*desc
, int kind
,
1423 gfc_charlen_type charlen
)
1425 index_type count
[GFC_MAX_DIMENSIONS
];
1426 index_type extent
[GFC_MAX_DIMENSIONS
];
1427 index_type stride
[GFC_MAX_DIMENSIONS
];
1428 index_type stride0
, rank
, size
, type
, n
;
1433 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1436 type
= GFC_DESCRIPTOR_TYPE (desc
);
1437 size
= GFC_DESCRIPTOR_SIZE (desc
);
1439 /* FIXME: What a kludge: Array descriptors and the IO library use
1440 different enums for types. */
1443 case GFC_DTYPE_UNKNOWN
:
1444 iotype
= BT_NULL
; /* Is this correct? */
1446 case GFC_DTYPE_INTEGER
:
1447 iotype
= BT_INTEGER
;
1449 case GFC_DTYPE_LOGICAL
:
1450 iotype
= BT_LOGICAL
;
1452 case GFC_DTYPE_REAL
:
1455 case GFC_DTYPE_COMPLEX
:
1456 iotype
= BT_COMPLEX
;
1458 case GFC_DTYPE_CHARACTER
:
1459 iotype
= BT_CHARACTER
;
1460 /* FIXME: Currently dtype contains the charlen, which is
1461 clobbered if charlen > 2**24. That's why we use a separate
1462 argument for the charlen. However, if we want to support
1463 non-8-bit charsets we need to fix dtype to contain
1464 sizeof(chartype) and fix the code below. */
1468 case GFC_DTYPE_DERIVED
:
1469 internal_error (&dtp
->common
,
1470 "Derived type I/O should have been handled via the frontend.");
1473 internal_error (&dtp
->common
, "transfer_array(): Bad type");
1476 rank
= GFC_DESCRIPTOR_RANK (desc
);
1477 for (n
= 0; n
< rank
; n
++)
1480 stride
[n
] = desc
->dim
[n
].stride
;
1481 extent
[n
] = desc
->dim
[n
].ubound
+ 1 - desc
->dim
[n
].lbound
;
1483 /* If the extent of even one dimension is zero, then the entire
1484 array section contains zero elements, so we return. */
1489 stride0
= stride
[0];
1491 /* If the innermost dimension has stride 1, we can do the transfer
1492 in contiguous chunks. */
1498 data
= GFC_DESCRIPTOR_DATA (desc
);
1502 dtp
->u
.p
.transfer (dtp
, iotype
, data
, kind
, size
, tsize
);
1503 data
+= stride0
* size
* tsize
;
1506 while (count
[n
] == extent
[n
])
1509 data
-= stride
[n
] * extent
[n
] * size
;
1519 data
+= stride
[n
] * size
;
1526 /* Preposition a sequential unformatted file while reading. */
1529 us_read (st_parameter_dt
*dtp
, int continued
)
1538 if (dtp
->u
.p
.current_unit
->endfile
== AT_ENDFILE
)
1541 if (compile_options
.record_marker
== 0)
1542 n
= sizeof (GFC_INTEGER_4
);
1544 n
= compile_options
.record_marker
;
1548 p
= salloc_r (dtp
->u
.p
.current_unit
->s
, &n
);
1552 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
1553 return; /* end of file */
1556 if (p
== NULL
|| n
!= nr
)
1558 generate_error (&dtp
->common
, ERROR_BAD_US
, NULL
);
1562 /* Only CONVERT_NATIVE and CONVERT_SWAP are valid here. */
1563 if (dtp
->u
.p
.current_unit
->flags
.convert
== CONVERT_NATIVE
)
1567 case sizeof(GFC_INTEGER_4
):
1568 memcpy (&i4
, p
, sizeof (i4
));
1572 case sizeof(GFC_INTEGER_8
):
1573 memcpy (&i8
, p
, sizeof (i8
));
1578 runtime_error ("Illegal value for record marker");
1585 case sizeof(GFC_INTEGER_4
):
1586 reverse_memcpy (&i4
, p
, sizeof (i4
));
1590 case sizeof(GFC_INTEGER_8
):
1591 reverse_memcpy (&i8
, p
, sizeof (i8
));
1596 runtime_error ("Illegal value for record marker");
1602 dtp
->u
.p
.current_unit
->bytes_left_subrecord
= i
;
1603 dtp
->u
.p
.current_unit
->continued
= 0;
1607 dtp
->u
.p
.current_unit
->bytes_left_subrecord
= -i
;
1608 dtp
->u
.p
.current_unit
->continued
= 1;
1612 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
1616 /* Preposition a sequential unformatted file while writing. This
1617 amount to writing a bogus length that will be filled in later. */
1620 us_write (st_parameter_dt
*dtp
, int continued
)
1627 if (compile_options
.record_marker
== 0)
1628 nbytes
= sizeof (GFC_INTEGER_4
);
1630 nbytes
= compile_options
.record_marker
;
1632 if (swrite (dtp
->u
.p
.current_unit
->s
, &dummy
, &nbytes
) != 0)
1633 generate_error (&dtp
->common
, ERROR_OS
, NULL
);
1635 /* For sequential unformatted, if RECL= was not specified in the OPEN
1636 we write until we have more bytes than can fit in the subrecord
1637 markers, then we write a new subrecord. */
1639 dtp
->u
.p
.current_unit
->bytes_left_subrecord
=
1640 dtp
->u
.p
.current_unit
->recl_subrecord
;
1641 dtp
->u
.p
.current_unit
->continued
= continued
;
1645 /* Position to the next record prior to transfer. We are assumed to
1646 be before the next record. We also calculate the bytes in the next
1650 pre_position (st_parameter_dt
*dtp
)
1652 if (dtp
->u
.p
.current_unit
->current_record
)
1653 return; /* Already positioned. */
1655 switch (current_mode (dtp
))
1657 case FORMATTED_STREAM
:
1658 case UNFORMATTED_STREAM
:
1659 /* There are no records with stream I/O. Set the default position
1660 to the beginning of the file if no position was specified. */
1661 if ((dtp
->common
.flags
& IOPARM_DT_HAS_REC
) == 0)
1662 dtp
->u
.p
.current_unit
->strm_pos
= 1;
1665 case UNFORMATTED_SEQUENTIAL
:
1666 if (dtp
->u
.p
.mode
== READING
)
1673 case FORMATTED_SEQUENTIAL
:
1674 case FORMATTED_DIRECT
:
1675 case UNFORMATTED_DIRECT
:
1676 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
1680 dtp
->u
.p
.current_unit
->current_record
= 1;
1684 /* Initialize things for a data transfer. This code is common for
1685 both reading and writing. */
1688 data_transfer_init (st_parameter_dt
*dtp
, int read_flag
)
1690 unit_flags u_flags
; /* Used for creating a unit if needed. */
1691 GFC_INTEGER_4 cf
= dtp
->common
.flags
;
1692 namelist_info
*ionml
;
1694 ionml
= ((cf
& IOPARM_DT_IONML_SET
) != 0) ? dtp
->u
.p
.ionml
: NULL
;
1695 memset (&dtp
->u
.p
, 0, sizeof (dtp
->u
.p
));
1696 dtp
->u
.p
.ionml
= ionml
;
1697 dtp
->u
.p
.mode
= read_flag
? READING
: WRITING
;
1699 if ((cf
& IOPARM_DT_HAS_SIZE
) != 0)
1700 dtp
->u
.p
.size_used
= 0; /* Initialize the count. */
1702 dtp
->u
.p
.current_unit
= get_unit (dtp
, 1);
1703 if (dtp
->u
.p
.current_unit
->s
== NULL
)
1704 { /* Open the unit with some default flags. */
1705 st_parameter_open opp
;
1708 if (dtp
->common
.unit
< 0)
1710 close_unit (dtp
->u
.p
.current_unit
);
1711 dtp
->u
.p
.current_unit
= NULL
;
1712 generate_error (&dtp
->common
, ERROR_BAD_OPTION
,
1713 "Bad unit number in OPEN statement");
1716 memset (&u_flags
, '\0', sizeof (u_flags
));
1717 u_flags
.access
= ACCESS_SEQUENTIAL
;
1718 u_flags
.action
= ACTION_READWRITE
;
1720 /* Is it unformatted? */
1721 if (!(cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
1722 | IOPARM_DT_IONML_SET
)))
1723 u_flags
.form
= FORM_UNFORMATTED
;
1725 u_flags
.form
= FORM_UNSPECIFIED
;
1727 u_flags
.delim
= DELIM_UNSPECIFIED
;
1728 u_flags
.blank
= BLANK_UNSPECIFIED
;
1729 u_flags
.pad
= PAD_UNSPECIFIED
;
1730 u_flags
.status
= STATUS_UNKNOWN
;
1732 conv
= get_unformatted_convert (dtp
->common
.unit
);
1734 if (conv
== CONVERT_NONE
)
1735 conv
= compile_options
.convert
;
1737 /* We use l8_to_l4_offset, which is 0 on little-endian machines
1738 and 1 on big-endian machines. */
1741 case CONVERT_NATIVE
:
1746 conv
= l8_to_l4_offset
? CONVERT_NATIVE
: CONVERT_SWAP
;
1749 case CONVERT_LITTLE
:
1750 conv
= l8_to_l4_offset
? CONVERT_SWAP
: CONVERT_NATIVE
;
1754 internal_error (&opp
.common
, "Illegal value for CONVERT");
1758 u_flags
.convert
= conv
;
1760 opp
.common
= dtp
->common
;
1761 opp
.common
.flags
&= IOPARM_COMMON_MASK
;
1762 dtp
->u
.p
.current_unit
= new_unit (&opp
, dtp
->u
.p
.current_unit
, &u_flags
);
1763 dtp
->common
.flags
&= ~IOPARM_COMMON_MASK
;
1764 dtp
->common
.flags
|= (opp
.common
.flags
& IOPARM_COMMON_MASK
);
1765 if (dtp
->u
.p
.current_unit
== NULL
)
1769 /* Check the action. */
1771 if (read_flag
&& dtp
->u
.p
.current_unit
->flags
.action
== ACTION_WRITE
)
1773 generate_error (&dtp
->common
, ERROR_BAD_ACTION
,
1774 "Cannot read from file opened for WRITE");
1778 if (!read_flag
&& dtp
->u
.p
.current_unit
->flags
.action
== ACTION_READ
)
1780 generate_error (&dtp
->common
, ERROR_BAD_ACTION
,
1781 "Cannot write to file opened for READ");
1785 dtp
->u
.p
.first_item
= 1;
1787 /* Check the format. */
1789 if ((cf
& IOPARM_DT_HAS_FORMAT
) != 0)
1792 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
1793 && (cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
))
1796 generate_error (&dtp
->common
, ERROR_OPTION_CONFLICT
,
1797 "Format present for UNFORMATTED data transfer");
1801 if ((cf
& IOPARM_DT_HAS_NAMELIST_NAME
) != 0 && dtp
->u
.p
.ionml
!= NULL
)
1803 if ((cf
& IOPARM_DT_HAS_FORMAT
) != 0)
1804 generate_error (&dtp
->common
, ERROR_OPTION_CONFLICT
,
1805 "A format cannot be specified with a namelist");
1807 else if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
&&
1808 !(cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
)))
1810 generate_error (&dtp
->common
, ERROR_OPTION_CONFLICT
,
1811 "Missing format for FORMATTED data transfer");
1814 if (is_internal_unit (dtp
)
1815 && dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
)
1817 generate_error (&dtp
->common
, ERROR_OPTION_CONFLICT
,
1818 "Internal file cannot be accessed by UNFORMATTED "
1823 /* Check the record or position number. */
1825 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
1826 && (cf
& IOPARM_DT_HAS_REC
) == 0)
1828 generate_error (&dtp
->common
, ERROR_MISSING_OPTION
,
1829 "Direct access data transfer requires record number");
1833 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
1834 && (cf
& IOPARM_DT_HAS_REC
) != 0)
1836 generate_error (&dtp
->common
, ERROR_OPTION_CONFLICT
,
1837 "Record number not allowed for sequential access data transfer");
1841 /* Process the ADVANCE option. */
1843 dtp
->u
.p
.advance_status
1844 = !(cf
& IOPARM_DT_HAS_ADVANCE
) ? ADVANCE_UNSPECIFIED
:
1845 find_option (&dtp
->common
, dtp
->advance
, dtp
->advance_len
, advance_opt
,
1846 "Bad ADVANCE parameter in data transfer statement");
1848 if (dtp
->u
.p
.advance_status
!= ADVANCE_UNSPECIFIED
)
1850 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
1852 generate_error (&dtp
->common
, ERROR_OPTION_CONFLICT
,
1853 "ADVANCE specification conflicts with sequential access");
1857 if (is_internal_unit (dtp
))
1859 generate_error (&dtp
->common
, ERROR_OPTION_CONFLICT
,
1860 "ADVANCE specification conflicts with internal file");
1864 if ((cf
& (IOPARM_DT_HAS_FORMAT
| IOPARM_DT_LIST_FORMAT
))
1865 != IOPARM_DT_HAS_FORMAT
)
1867 generate_error (&dtp
->common
, ERROR_OPTION_CONFLICT
,
1868 "ADVANCE specification requires an explicit format");
1875 if ((cf
& IOPARM_EOR
) != 0 && dtp
->u
.p
.advance_status
!= ADVANCE_NO
)
1877 generate_error (&dtp
->common
, ERROR_MISSING_OPTION
,
1878 "EOR specification requires an ADVANCE specification "
1883 if ((cf
& IOPARM_DT_HAS_SIZE
) != 0 && dtp
->u
.p
.advance_status
!= ADVANCE_NO
)
1885 generate_error (&dtp
->common
, ERROR_MISSING_OPTION
,
1886 "SIZE specification requires an ADVANCE specification of NO");
1891 { /* Write constraints. */
1892 if ((cf
& IOPARM_END
) != 0)
1894 generate_error (&dtp
->common
, ERROR_OPTION_CONFLICT
,
1895 "END specification cannot appear in a write statement");
1899 if ((cf
& IOPARM_EOR
) != 0)
1901 generate_error (&dtp
->common
, ERROR_OPTION_CONFLICT
,
1902 "EOR specification cannot appear in a write statement");
1906 if ((cf
& IOPARM_DT_HAS_SIZE
) != 0)
1908 generate_error (&dtp
->common
, ERROR_OPTION_CONFLICT
,
1909 "SIZE specification cannot appear in a write statement");
1914 if (dtp
->u
.p
.advance_status
== ADVANCE_UNSPECIFIED
)
1915 dtp
->u
.p
.advance_status
= ADVANCE_YES
;
1917 /* Sanity checks on the record number. */
1918 if ((cf
& IOPARM_DT_HAS_REC
) != 0)
1922 generate_error (&dtp
->common
, ERROR_BAD_OPTION
,
1923 "Record number must be positive");
1927 if (dtp
->rec
>= dtp
->u
.p
.current_unit
->maxrec
)
1929 generate_error (&dtp
->common
, ERROR_BAD_OPTION
,
1930 "Record number too large");
1934 /* Check to see if we might be reading what we wrote before */
1936 if (dtp
->u
.p
.mode
== READING
1937 && dtp
->u
.p
.current_unit
->mode
== WRITING
1938 && !is_internal_unit (dtp
))
1939 flush(dtp
->u
.p
.current_unit
->s
);
1941 /* Check whether the record exists to be read. Only
1942 a partial record needs to exist. */
1944 if (dtp
->u
.p
.mode
== READING
&& (dtp
->rec
-1)
1945 * dtp
->u
.p
.current_unit
->recl
>= file_length (dtp
->u
.p
.current_unit
->s
))
1947 generate_error (&dtp
->common
, ERROR_BAD_OPTION
,
1948 "Non-existing record number");
1952 /* Position the file. */
1953 if (!is_stream_io (dtp
))
1955 if (sseek (dtp
->u
.p
.current_unit
->s
, (gfc_offset
) (dtp
->rec
- 1)
1956 * dtp
->u
.p
.current_unit
->recl
) == FAILURE
)
1958 generate_error (&dtp
->common
, ERROR_OS
, NULL
);
1963 dtp
->u
.p
.current_unit
->strm_pos
= dtp
->rec
;
1967 /* Overwriting an existing sequential file ?
1968 it is always safe to truncate the file on the first write */
1969 if (dtp
->u
.p
.mode
== WRITING
1970 && dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
1971 && dtp
->u
.p
.current_unit
->last_record
== 0
1972 && !is_preconnected(dtp
->u
.p
.current_unit
->s
))
1973 struncate(dtp
->u
.p
.current_unit
->s
);
1975 /* Bugware for badly written mixed C-Fortran I/O. */
1976 flush_if_preconnected(dtp
->u
.p
.current_unit
->s
);
1978 dtp
->u
.p
.current_unit
->mode
= dtp
->u
.p
.mode
;
1980 /* Set the initial value of flags. */
1982 dtp
->u
.p
.blank_status
= dtp
->u
.p
.current_unit
->flags
.blank
;
1983 dtp
->u
.p
.sign_status
= SIGN_S
;
1987 /* Set up the subroutine that will handle the transfers. */
1991 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
)
1992 dtp
->u
.p
.transfer
= unformatted_read
;
1995 if ((cf
& IOPARM_DT_LIST_FORMAT
) != 0)
1996 dtp
->u
.p
.transfer
= list_formatted_read
;
1998 dtp
->u
.p
.transfer
= formatted_transfer
;
2003 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_UNFORMATTED
)
2004 dtp
->u
.p
.transfer
= unformatted_write
;
2007 if ((cf
& IOPARM_DT_LIST_FORMAT
) != 0)
2008 dtp
->u
.p
.transfer
= list_formatted_write
;
2010 dtp
->u
.p
.transfer
= formatted_transfer
;
2014 /* Make sure that we don't do a read after a nonadvancing write. */
2018 if (dtp
->u
.p
.current_unit
->read_bad
&& !is_stream_io (dtp
))
2020 generate_error (&dtp
->common
, ERROR_BAD_OPTION
,
2021 "Cannot READ after a nonadvancing WRITE");
2027 if (dtp
->u
.p
.advance_status
== ADVANCE_YES
&& !dtp
->u
.p
.seen_dollar
)
2028 dtp
->u
.p
.current_unit
->read_bad
= 1;
2031 /* Start the data transfer if we are doing a formatted transfer. */
2032 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
2033 && ((cf
& (IOPARM_DT_LIST_FORMAT
| IOPARM_DT_HAS_NAMELIST_NAME
)) == 0)
2034 && dtp
->u
.p
.ionml
== NULL
)
2035 formatted_transfer (dtp
, 0, NULL
, 0, 0, 1);
2038 /* Initialize an array_loop_spec given the array descriptor. The function
2039 returns the index of the last element of the array. */
2042 init_loop_spec (gfc_array_char
*desc
, array_loop_spec
*ls
)
2044 int rank
= GFC_DESCRIPTOR_RANK(desc
);
2049 for (i
=0; i
<rank
; i
++)
2051 ls
[i
].idx
= desc
->dim
[i
].lbound
;
2052 ls
[i
].start
= desc
->dim
[i
].lbound
;
2053 ls
[i
].end
= desc
->dim
[i
].ubound
;
2054 ls
[i
].step
= desc
->dim
[i
].stride
;
2056 index
+= (desc
->dim
[i
].ubound
- desc
->dim
[i
].lbound
)
2057 * desc
->dim
[i
].stride
;
2062 /* Determine the index to the next record in an internal unit array by
2063 by incrementing through the array_loop_spec. TODO: Implement handling
2064 negative strides. */
2067 next_array_record (st_parameter_dt
*dtp
, array_loop_spec
*ls
)
2075 for (i
= 0; i
< dtp
->u
.p
.current_unit
->rank
; i
++)
2080 if (ls
[i
].idx
> ls
[i
].end
)
2082 ls
[i
].idx
= ls
[i
].start
;
2088 index
= index
+ (ls
[i
].idx
- ls
[i
].start
) * ls
[i
].step
;
2096 /* Skip to the end of the current record, taking care of an optional
2097 record marker of size bytes. If the file is not seekable, we
2098 read chunks of size MAX_READ until we get to the right
2101 #define MAX_READ 4096
2104 skip_record (st_parameter_dt
*dtp
, size_t bytes
)
2107 int rlength
, length
;
2110 dtp
->u
.p
.current_unit
->bytes_left_subrecord
+= bytes
;
2111 if (dtp
->u
.p
.current_unit
->bytes_left_subrecord
== 0)
2114 if (is_seekable (dtp
->u
.p
.current_unit
->s
))
2116 new = file_position (dtp
->u
.p
.current_unit
->s
)
2117 + dtp
->u
.p
.current_unit
->bytes_left_subrecord
;
2119 /* Direct access files do not generate END conditions,
2121 if (sseek (dtp
->u
.p
.current_unit
->s
, new) == FAILURE
)
2122 generate_error (&dtp
->common
, ERROR_OS
, NULL
);
2125 { /* Seek by reading data. */
2126 while (dtp
->u
.p
.current_unit
->bytes_left_subrecord
> 0)
2129 (MAX_READ
> dtp
->u
.p
.current_unit
->bytes_left_subrecord
) ?
2130 MAX_READ
: dtp
->u
.p
.current_unit
->bytes_left_subrecord
;
2132 p
= salloc_r (dtp
->u
.p
.current_unit
->s
, &rlength
);
2135 generate_error (&dtp
->common
, ERROR_OS
, NULL
);
2139 dtp
->u
.p
.current_unit
->bytes_left_subrecord
-= length
;
2147 /* Advance to the next record reading unformatted files, taking
2148 care of subrecords. If complete_record is nonzero, we loop
2149 until all subrecords are cleared. */
2152 next_record_r_unf (st_parameter_dt
*dtp
, int complete_record
)
2156 bytes
= compile_options
.record_marker
== 0 ?
2157 sizeof (GFC_INTEGER_4
) : compile_options
.record_marker
;
2162 /* Skip over tail */
2164 skip_record (dtp
, bytes
);
2166 if ( ! (complete_record
&& dtp
->u
.p
.current_unit
->continued
))
2173 /* Space to the next record for read mode. */
2176 next_record_r (st_parameter_dt
*dtp
)
2179 int length
, bytes_left
;
2182 switch (current_mode (dtp
))
2184 /* No records in unformatted STREAM I/O. */
2185 case UNFORMATTED_STREAM
:
2188 case UNFORMATTED_SEQUENTIAL
:
2189 next_record_r_unf (dtp
, 1);
2190 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2193 case FORMATTED_DIRECT
:
2194 case UNFORMATTED_DIRECT
:
2195 skip_record (dtp
, 0);
2198 case FORMATTED_STREAM
:
2199 case FORMATTED_SEQUENTIAL
:
2201 /* sf_read has already terminated input because of an '\n' */
2202 if (dtp
->u
.p
.sf_seen_eor
)
2204 dtp
->u
.p
.sf_seen_eor
= 0;
2208 if (is_internal_unit (dtp
))
2210 if (is_array_io (dtp
))
2212 record
= next_array_record (dtp
, dtp
->u
.p
.current_unit
->ls
);
2214 /* Now seek to this record. */
2215 record
= record
* dtp
->u
.p
.current_unit
->recl
;
2216 if (sseek (dtp
->u
.p
.current_unit
->s
, record
) == FAILURE
)
2218 generate_error (&dtp
->common
, ERROR_INTERNAL_UNIT
, NULL
);
2221 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2225 bytes_left
= (int) dtp
->u
.p
.current_unit
->bytes_left
;
2226 p
= salloc_r (dtp
->u
.p
.current_unit
->s
, &bytes_left
);
2228 dtp
->u
.p
.current_unit
->bytes_left
2229 = dtp
->u
.p
.current_unit
->recl
;
2235 p
= salloc_r (dtp
->u
.p
.current_unit
->s
, &length
);
2239 generate_error (&dtp
->common
, ERROR_OS
, NULL
);
2245 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
2249 if (is_stream_io (dtp
))
2250 dtp
->u
.p
.current_unit
->strm_pos
++;
2257 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
2258 test_endfile (dtp
->u
.p
.current_unit
);
2262 /* Small utility function to write a record marker, taking care of
2263 byte swapping and of choosing the correct size. */
2266 write_us_marker (st_parameter_dt
*dtp
, const gfc_offset buf
)
2271 char p
[sizeof (GFC_INTEGER_8
)];
2273 if (compile_options
.record_marker
== 0)
2274 len
= sizeof (GFC_INTEGER_4
);
2276 len
= compile_options
.record_marker
;
2278 /* Only CONVERT_NATIVE and CONVERT_SWAP are valid here. */
2279 if (dtp
->u
.p
.current_unit
->flags
.convert
== CONVERT_NATIVE
)
2283 case sizeof (GFC_INTEGER_4
):
2285 return swrite (dtp
->u
.p
.current_unit
->s
, &buf4
, &len
);
2288 case sizeof (GFC_INTEGER_8
):
2290 return swrite (dtp
->u
.p
.current_unit
->s
, &buf8
, &len
);
2294 runtime_error ("Illegal value for record marker");
2302 case sizeof (GFC_INTEGER_4
):
2304 reverse_memcpy (p
, &buf4
, sizeof (GFC_INTEGER_4
));
2305 return swrite (dtp
->u
.p
.current_unit
->s
, p
, &len
);
2308 case sizeof (GFC_INTEGER_8
):
2310 reverse_memcpy (p
, &buf8
, sizeof (GFC_INTEGER_8
));
2311 return swrite (dtp
->u
.p
.current_unit
->s
, p
, &len
);
2315 runtime_error ("Illegal value for record marker");
2322 /* Position to the next (sub)record in write mode for
2323 unformatted sequential files. */
2326 next_record_w_unf (st_parameter_dt
*dtp
, int next_subrecord
)
2328 gfc_offset c
, m
, m_write
;
2329 size_t record_marker
;
2331 /* Bytes written. */
2332 m
= dtp
->u
.p
.current_unit
->recl_subrecord
2333 - dtp
->u
.p
.current_unit
->bytes_left_subrecord
;
2334 c
= file_position (dtp
->u
.p
.current_unit
->s
);
2336 /* Write the length tail. If we finish a record containing
2337 subrecords, we write out the negative length. */
2339 if (dtp
->u
.p
.current_unit
->continued
)
2344 if (write_us_marker (dtp
, m_write
) != 0)
2347 if (compile_options
.record_marker
== 0)
2348 record_marker
= sizeof (GFC_INTEGER_4
);
2350 record_marker
= compile_options
.record_marker
;
2352 /* Seek to the head and overwrite the bogus length with the real
2355 if (sseek (dtp
->u
.p
.current_unit
->s
, c
- m
- record_marker
)
2364 if (write_us_marker (dtp
, m_write
) != 0)
2367 /* Seek past the end of the current record. */
2369 if (sseek (dtp
->u
.p
.current_unit
->s
, c
+ record_marker
) == FAILURE
)
2375 generate_error (&dtp
->common
, ERROR_OS
, NULL
);
2380 /* Position to the next record in write mode. */
2383 next_record_w (st_parameter_dt
*dtp
, int done
)
2385 gfc_offset m
, record
, max_pos
;
2389 /* Zero counters for X- and T-editing. */
2390 max_pos
= dtp
->u
.p
.max_pos
;
2391 dtp
->u
.p
.max_pos
= dtp
->u
.p
.skips
= dtp
->u
.p
.pending_spaces
= 0;
2393 switch (current_mode (dtp
))
2395 /* No records in unformatted STREAM I/O. */
2396 case UNFORMATTED_STREAM
:
2399 case FORMATTED_DIRECT
:
2400 if (dtp
->u
.p
.current_unit
->bytes_left
== 0)
2403 if (sset (dtp
->u
.p
.current_unit
->s
, ' ',
2404 dtp
->u
.p
.current_unit
->bytes_left
) == FAILURE
)
2409 case UNFORMATTED_DIRECT
:
2410 if (sfree (dtp
->u
.p
.current_unit
->s
) == FAILURE
)
2414 case UNFORMATTED_SEQUENTIAL
:
2415 next_record_w_unf (dtp
, 0);
2416 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2419 case FORMATTED_STREAM
:
2420 case FORMATTED_SEQUENTIAL
:
2422 if (is_internal_unit (dtp
))
2424 if (is_array_io (dtp
))
2426 length
= (int) dtp
->u
.p
.current_unit
->bytes_left
;
2428 /* If the farthest position reached is greater than current
2429 position, adjust the position and set length to pad out
2430 whats left. Otherwise just pad whats left.
2431 (for character array unit) */
2432 m
= dtp
->u
.p
.current_unit
->recl
2433 - dtp
->u
.p
.current_unit
->bytes_left
;
2436 length
= (int) (max_pos
- m
);
2437 p
= salloc_w (dtp
->u
.p
.current_unit
->s
, &length
);
2438 length
= (int) (dtp
->u
.p
.current_unit
->recl
- max_pos
);
2441 if (sset (dtp
->u
.p
.current_unit
->s
, ' ', length
) == FAILURE
)
2443 generate_error (&dtp
->common
, ERROR_END
, NULL
);
2447 /* Now that the current record has been padded out,
2448 determine where the next record in the array is. */
2449 record
= next_array_record (dtp
, dtp
->u
.p
.current_unit
->ls
);
2451 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
2453 /* Now seek to this record */
2454 record
= record
* dtp
->u
.p
.current_unit
->recl
;
2456 if (sseek (dtp
->u
.p
.current_unit
->s
, record
) == FAILURE
)
2458 generate_error (&dtp
->common
, ERROR_INTERNAL_UNIT
, NULL
);
2462 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
2468 /* If this is the last call to next_record move to the farthest
2469 position reached and set length to pad out the remainder
2470 of the record. (for character scaler unit) */
2473 m
= dtp
->u
.p
.current_unit
->recl
2474 - dtp
->u
.p
.current_unit
->bytes_left
;
2477 length
= (int) (max_pos
- m
);
2478 p
= salloc_w (dtp
->u
.p
.current_unit
->s
, &length
);
2479 length
= (int) (dtp
->u
.p
.current_unit
->recl
- max_pos
);
2482 length
= (int) dtp
->u
.p
.current_unit
->bytes_left
;
2485 if (sset (dtp
->u
.p
.current_unit
->s
, ' ', length
) == FAILURE
)
2487 generate_error (&dtp
->common
, ERROR_END
, NULL
);
2495 /* If this is the last call to next_record move to the farthest
2496 position reached in preparation for completing the record.
2500 m
= dtp
->u
.p
.current_unit
->recl
-
2501 dtp
->u
.p
.current_unit
->bytes_left
;
2504 length
= (int) (max_pos
- m
);
2505 p
= salloc_w (dtp
->u
.p
.current_unit
->s
, &length
);
2509 const char crlf
[] = "\r\n";
2515 if (swrite (dtp
->u
.p
.current_unit
->s
, &crlf
[2-len
], &len
) != 0)
2518 if (is_stream_io (dtp
))
2519 dtp
->u
.p
.current_unit
->strm_pos
+= len
;
2525 generate_error (&dtp
->common
, ERROR_OS
, NULL
);
2530 /* Position to the next record, which means moving to the end of the
2531 current record. This can happen under several different
2532 conditions. If the done flag is not set, we get ready to process
2536 next_record (st_parameter_dt
*dtp
, int done
)
2538 gfc_offset fp
; /* File position. */
2540 dtp
->u
.p
.current_unit
->read_bad
= 0;
2542 if (dtp
->u
.p
.mode
== READING
)
2543 next_record_r (dtp
);
2545 next_record_w (dtp
, done
);
2547 if (!is_stream_io (dtp
))
2549 /* keep position up to date for INQUIRE */
2550 dtp
->u
.p
.current_unit
->flags
.position
= POSITION_ASIS
;
2551 dtp
->u
.p
.current_unit
->current_record
= 0;
2552 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_DIRECT
)
2554 fp
= file_position (dtp
->u
.p
.current_unit
->s
);
2555 /* Calculate next record, rounding up partial records. */
2556 dtp
->u
.p
.current_unit
->last_record
=
2557 (fp
+ dtp
->u
.p
.current_unit
->recl
- 1) /
2558 dtp
->u
.p
.current_unit
->recl
;
2561 dtp
->u
.p
.current_unit
->last_record
++;
2569 /* Finalize the current data transfer. For a nonadvancing transfer,
2570 this means advancing to the next record. For internal units close the
2571 stream associated with the unit. */
2574 finalize_transfer (st_parameter_dt
*dtp
)
2577 GFC_INTEGER_4 cf
= dtp
->common
.flags
;
2579 if ((dtp
->common
.flags
& IOPARM_DT_HAS_SIZE
) != 0)
2580 *dtp
->size
= (GFC_INTEGER_4
) dtp
->u
.p
.size_used
;
2582 if (dtp
->u
.p
.eor_condition
)
2584 generate_error (&dtp
->common
, ERROR_EOR
, NULL
);
2588 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
2591 if ((dtp
->u
.p
.ionml
!= NULL
)
2592 && (cf
& IOPARM_DT_HAS_NAMELIST_NAME
) != 0)
2594 if ((cf
& IOPARM_DT_NAMELIST_READ_MODE
) != 0)
2595 namelist_read (dtp
);
2597 namelist_write (dtp
);
2600 dtp
->u
.p
.transfer
= NULL
;
2601 if (dtp
->u
.p
.current_unit
== NULL
)
2604 dtp
->u
.p
.eof_jump
= &eof_jump
;
2605 if (setjmp (eof_jump
))
2607 generate_error (&dtp
->common
, ERROR_END
, NULL
);
2611 if ((cf
& IOPARM_DT_LIST_FORMAT
) != 0 && dtp
->u
.p
.mode
== READING
)
2613 finish_list_read (dtp
);
2614 sfree (dtp
->u
.p
.current_unit
->s
);
2618 if (is_stream_io (dtp
))
2620 if (dtp
->u
.p
.current_unit
->flags
.form
== FORM_FORMATTED
)
2621 next_record (dtp
, 1);
2622 flush (dtp
->u
.p
.current_unit
->s
);
2623 sfree (dtp
->u
.p
.current_unit
->s
);
2627 dtp
->u
.p
.current_unit
->current_record
= 0;
2629 if (!is_internal_unit (dtp
) && dtp
->u
.p
.seen_dollar
)
2631 dtp
->u
.p
.seen_dollar
= 0;
2632 sfree (dtp
->u
.p
.current_unit
->s
);
2636 if (dtp
->u
.p
.advance_status
== ADVANCE_NO
)
2638 flush (dtp
->u
.p
.current_unit
->s
);
2642 next_record (dtp
, 1);
2643 sfree (dtp
->u
.p
.current_unit
->s
);
2646 /* Transfer function for IOLENGTH. It doesn't actually do any
2647 data transfer, it just updates the length counter. */
2650 iolength_transfer (st_parameter_dt
*dtp
, bt type
__attribute__((unused
)),
2651 void *dest
__attribute__ ((unused
)),
2652 int kind
__attribute__((unused
)),
2653 size_t size
, size_t nelems
)
2655 if ((dtp
->common
.flags
& IOPARM_DT_HAS_IOLENGTH
) != 0)
2656 *dtp
->iolength
+= (GFC_INTEGER_4
) size
* nelems
;
2660 /* Initialize the IOLENGTH data transfer. This function is in essence
2661 a very much simplified version of data_transfer_init(), because it
2662 doesn't have to deal with units at all. */
2665 iolength_transfer_init (st_parameter_dt
*dtp
)
2667 if ((dtp
->common
.flags
& IOPARM_DT_HAS_IOLENGTH
) != 0)
2670 memset (&dtp
->u
.p
, 0, sizeof (dtp
->u
.p
));
2672 /* Set up the subroutine that will handle the transfers. */
2674 dtp
->u
.p
.transfer
= iolength_transfer
;
2678 /* Library entry point for the IOLENGTH form of the INQUIRE
2679 statement. The IOLENGTH form requires no I/O to be performed, but
2680 it must still be a runtime library call so that we can determine
2681 the iolength for dynamic arrays and such. */
2683 extern void st_iolength (st_parameter_dt
*);
2684 export_proto(st_iolength
);
2687 st_iolength (st_parameter_dt
*dtp
)
2689 library_start (&dtp
->common
);
2690 iolength_transfer_init (dtp
);
2693 extern void st_iolength_done (st_parameter_dt
*);
2694 export_proto(st_iolength_done
);
2697 st_iolength_done (st_parameter_dt
*dtp
__attribute__((unused
)))
2700 if (dtp
->u
.p
.scratch
!= NULL
)
2701 free_mem (dtp
->u
.p
.scratch
);
2706 /* The READ statement. */
2708 extern void st_read (st_parameter_dt
*);
2709 export_proto(st_read
);
2712 st_read (st_parameter_dt
*dtp
)
2714 library_start (&dtp
->common
);
2716 data_transfer_init (dtp
, 1);
2718 /* Handle complications dealing with the endfile record. It is
2719 significant that this is the only place where ERROR_END is
2720 generated. Reading an end of file elsewhere is either end of
2721 record or an I/O error. */
2723 if (dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
2724 switch (dtp
->u
.p
.current_unit
->endfile
)
2730 if (!is_internal_unit (dtp
))
2732 generate_error (&dtp
->common
, ERROR_END
, NULL
);
2733 dtp
->u
.p
.current_unit
->endfile
= AFTER_ENDFILE
;
2734 dtp
->u
.p
.current_unit
->current_record
= 0;
2739 generate_error (&dtp
->common
, ERROR_ENDFILE
, NULL
);
2740 dtp
->u
.p
.current_unit
->current_record
= 0;
2745 extern void st_read_done (st_parameter_dt
*);
2746 export_proto(st_read_done
);
2749 st_read_done (st_parameter_dt
*dtp
)
2751 finalize_transfer (dtp
);
2752 free_format_data (dtp
);
2754 if (dtp
->u
.p
.scratch
!= NULL
)
2755 free_mem (dtp
->u
.p
.scratch
);
2756 if (dtp
->u
.p
.current_unit
!= NULL
)
2757 unlock_unit (dtp
->u
.p
.current_unit
);
2759 free_internal_unit (dtp
);
2764 extern void st_write (st_parameter_dt
*);
2765 export_proto(st_write
);
2768 st_write (st_parameter_dt
*dtp
)
2770 library_start (&dtp
->common
);
2771 data_transfer_init (dtp
, 0);
2774 extern void st_write_done (st_parameter_dt
*);
2775 export_proto(st_write_done
);
2778 st_write_done (st_parameter_dt
*dtp
)
2780 finalize_transfer (dtp
);
2782 /* Deal with endfile conditions associated with sequential files. */
2784 if (dtp
->u
.p
.current_unit
!= NULL
2785 && dtp
->u
.p
.current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
2786 switch (dtp
->u
.p
.current_unit
->endfile
)
2788 case AT_ENDFILE
: /* Remain at the endfile record. */
2792 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
; /* Just at it now. */
2796 /* Get rid of whatever is after this record. */
2797 if (!is_internal_unit (dtp
))
2799 flush (dtp
->u
.p
.current_unit
->s
);
2800 if (struncate (dtp
->u
.p
.current_unit
->s
) == FAILURE
)
2801 generate_error (&dtp
->common
, ERROR_OS
, NULL
);
2803 dtp
->u
.p
.current_unit
->endfile
= AT_ENDFILE
;
2807 free_format_data (dtp
);
2809 if (dtp
->u
.p
.scratch
!= NULL
)
2810 free_mem (dtp
->u
.p
.scratch
);
2811 if (dtp
->u
.p
.current_unit
!= NULL
)
2812 unlock_unit (dtp
->u
.p
.current_unit
);
2814 free_internal_unit (dtp
);
2819 /* Receives the scalar information for namelist objects and stores it
2820 in a linked list of namelist_info types. */
2822 extern void st_set_nml_var (st_parameter_dt
*dtp
, void *, char *,
2823 GFC_INTEGER_4
, gfc_charlen_type
, GFC_INTEGER_4
);
2824 export_proto(st_set_nml_var
);
2828 st_set_nml_var (st_parameter_dt
*dtp
, void * var_addr
, char * var_name
,
2829 GFC_INTEGER_4 len
, gfc_charlen_type string_length
,
2830 GFC_INTEGER_4 dtype
)
2832 namelist_info
*t1
= NULL
;
2835 nml
= (namelist_info
*) get_mem (sizeof (namelist_info
));
2837 nml
->mem_pos
= var_addr
;
2839 nml
->var_name
= (char*) get_mem (strlen (var_name
) + 1);
2840 strcpy (nml
->var_name
, var_name
);
2842 nml
->len
= (int) len
;
2843 nml
->string_length
= (index_type
) string_length
;
2845 nml
->var_rank
= (int) (dtype
& GFC_DTYPE_RANK_MASK
);
2846 nml
->size
= (index_type
) (dtype
>> GFC_DTYPE_SIZE_SHIFT
);
2847 nml
->type
= (bt
) ((dtype
& GFC_DTYPE_TYPE_MASK
) >> GFC_DTYPE_TYPE_SHIFT
);
2849 if (nml
->var_rank
> 0)
2851 nml
->dim
= (descriptor_dimension
*)
2852 get_mem (nml
->var_rank
* sizeof (descriptor_dimension
));
2853 nml
->ls
= (array_loop_spec
*)
2854 get_mem (nml
->var_rank
* sizeof (array_loop_spec
));
2864 if ((dtp
->common
.flags
& IOPARM_DT_IONML_SET
) == 0)
2866 dtp
->common
.flags
|= IOPARM_DT_IONML_SET
;
2867 dtp
->u
.p
.ionml
= nml
;
2871 for (t1
= dtp
->u
.p
.ionml
; t1
->next
; t1
= t1
->next
);
2876 /* Store the dimensional information for the namelist object. */
2877 extern void st_set_nml_var_dim (st_parameter_dt
*, GFC_INTEGER_4
,
2878 GFC_INTEGER_4
, GFC_INTEGER_4
,
2880 export_proto(st_set_nml_var_dim
);
2883 st_set_nml_var_dim (st_parameter_dt
*dtp
, GFC_INTEGER_4 n_dim
,
2884 GFC_INTEGER_4 stride
, GFC_INTEGER_4 lbound
,
2885 GFC_INTEGER_4 ubound
)
2887 namelist_info
* nml
;
2892 for (nml
= dtp
->u
.p
.ionml
; nml
->next
; nml
= nml
->next
);
2894 nml
->dim
[n
].stride
= (ssize_t
)stride
;
2895 nml
->dim
[n
].lbound
= (ssize_t
)lbound
;
2896 nml
->dim
[n
].ubound
= (ssize_t
)ubound
;
2899 /* Reverse memcpy - used for byte swapping. */
2901 void reverse_memcpy (void *dest
, const void *src
, size_t n
)
2907 s
= (char *) src
+ n
- 1;
2909 /* Write with ascending order - this is likely faster
2910 on modern architectures because of write combining. */