re PR target/91498 (STV change in r274481 causes 300.twolf regression on Haswell)
[official-gcc.git] / libgfortran / io / transfer.c
blobc43360f6332b3510c2e790986ac0b6a7ed96f15d
1 /* Copyright (C) 2002-2019 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
3 Namelist transfer functions contributed by Paul Thomas
4 F2003 I/O support contributed by Jerry DeLisle
6 This file is part of the GNU Fortran runtime library (libgfortran).
8 Libgfortran is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 Libgfortran is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
28 /* transfer.c -- Top level handling of data transfer statements. */
30 #include "io.h"
31 #include "fbuf.h"
32 #include "format.h"
33 #include "unix.h"
34 #include "async.h"
35 #include <string.h>
36 #include <errno.h>
39 /* Calling conventions: Data transfer statements are unlike other
40 library calls in that they extend over several calls.
42 The first call is always a call to st_read() or st_write(). These
43 subroutines return no status unless a namelist read or write is
44 being done, in which case there is the usual status. No further
45 calls are necessary in this case.
47 For other sorts of data transfer, there are zero or more data
48 transfer statement that depend on the format of the data transfer
49 statement. For READ (and for backwards compatibily: for WRITE), one has
51 transfer_integer
52 transfer_logical
53 transfer_character
54 transfer_character_wide
55 transfer_real
56 transfer_complex
57 transfer_real128
58 transfer_complex128
60 and for WRITE
62 transfer_integer_write
63 transfer_logical_write
64 transfer_character_write
65 transfer_character_wide_write
66 transfer_real_write
67 transfer_complex_write
68 transfer_real128_write
69 transfer_complex128_write
71 These subroutines do not return status. The *128 functions
72 are in the file transfer128.c.
74 The last call is a call to st_[read|write]_done(). While
75 something can easily go wrong with the initial st_read() or
76 st_write(), an error inhibits any data from actually being
77 transferred. */
79 extern void transfer_integer (st_parameter_dt *, void *, int);
80 export_proto(transfer_integer);
82 extern void transfer_integer_write (st_parameter_dt *, void *, int);
83 export_proto(transfer_integer_write);
85 extern void transfer_real (st_parameter_dt *, void *, int);
86 export_proto(transfer_real);
88 extern void transfer_real_write (st_parameter_dt *, void *, int);
89 export_proto(transfer_real_write);
91 extern void transfer_logical (st_parameter_dt *, void *, int);
92 export_proto(transfer_logical);
94 extern void transfer_logical_write (st_parameter_dt *, void *, int);
95 export_proto(transfer_logical_write);
97 extern void transfer_character (st_parameter_dt *, void *, gfc_charlen_type);
98 export_proto(transfer_character);
100 extern void transfer_character_write (st_parameter_dt *, void *, gfc_charlen_type);
101 export_proto(transfer_character_write);
103 extern void transfer_character_wide (st_parameter_dt *, void *, gfc_charlen_type, int);
104 export_proto(transfer_character_wide);
106 extern void transfer_character_wide_write (st_parameter_dt *,
107 void *, gfc_charlen_type, int);
108 export_proto(transfer_character_wide_write);
110 extern void transfer_complex (st_parameter_dt *, void *, int);
111 export_proto(transfer_complex);
113 extern void transfer_complex_write (st_parameter_dt *, void *, int);
114 export_proto(transfer_complex_write);
116 extern void transfer_array (st_parameter_dt *, gfc_array_char *, int,
117 gfc_charlen_type);
118 export_proto(transfer_array);
120 extern void transfer_array_write (st_parameter_dt *, gfc_array_char *, int,
121 gfc_charlen_type);
122 export_proto(transfer_array_write);
124 /* User defined derived type input/output. */
125 extern void
126 transfer_derived (st_parameter_dt *dtp, void *dtio_source, void *dtio_proc);
127 export_proto(transfer_derived);
129 extern void
130 transfer_derived_write (st_parameter_dt *dtp, void *dtio_source, void *dtio_proc);
131 export_proto(transfer_derived_write);
133 static void us_read (st_parameter_dt *, int);
134 static void us_write (st_parameter_dt *, int);
135 static void next_record_r_unf (st_parameter_dt *, int);
136 static void next_record_w_unf (st_parameter_dt *, int);
138 static const st_option advance_opt[] = {
139 {"yes", ADVANCE_YES},
140 {"no", ADVANCE_NO},
141 {NULL, 0}
145 static const st_option decimal_opt[] = {
146 {"point", DECIMAL_POINT},
147 {"comma", DECIMAL_COMMA},
148 {NULL, 0}
151 static const st_option round_opt[] = {
152 {"up", ROUND_UP},
153 {"down", ROUND_DOWN},
154 {"zero", ROUND_ZERO},
155 {"nearest", ROUND_NEAREST},
156 {"compatible", ROUND_COMPATIBLE},
157 {"processor_defined", ROUND_PROCDEFINED},
158 {NULL, 0}
162 static const st_option sign_opt[] = {
163 {"plus", SIGN_SP},
164 {"suppress", SIGN_SS},
165 {"processor_defined", SIGN_S},
166 {NULL, 0}
169 static const st_option blank_opt[] = {
170 {"null", BLANK_NULL},
171 {"zero", BLANK_ZERO},
172 {NULL, 0}
175 static const st_option delim_opt[] = {
176 {"apostrophe", DELIM_APOSTROPHE},
177 {"quote", DELIM_QUOTE},
178 {"none", DELIM_NONE},
179 {NULL, 0}
182 static const st_option pad_opt[] = {
183 {"yes", PAD_YES},
184 {"no", PAD_NO},
185 {NULL, 0}
188 static const st_option async_opt[] = {
189 {"yes", ASYNC_YES},
190 {"no", ASYNC_NO},
191 {NULL, 0}
194 typedef enum
195 { FORMATTED_SEQUENTIAL, UNFORMATTED_SEQUENTIAL,
196 FORMATTED_DIRECT, UNFORMATTED_DIRECT, FORMATTED_STREAM, UNFORMATTED_STREAM
198 file_mode;
201 static file_mode
202 current_mode (st_parameter_dt *dtp)
204 file_mode m;
206 m = FORM_UNSPECIFIED;
208 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
210 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
211 FORMATTED_DIRECT : UNFORMATTED_DIRECT;
213 else if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
215 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
216 FORMATTED_SEQUENTIAL : UNFORMATTED_SEQUENTIAL;
218 else if (dtp->u.p.current_unit->flags.access == ACCESS_STREAM)
220 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
221 FORMATTED_STREAM : UNFORMATTED_STREAM;
224 return m;
228 /* Mid level data transfer statements. */
230 /* Read sequential file - internal unit */
232 static char *
233 read_sf_internal (st_parameter_dt *dtp, size_t *length)
235 static char *empty_string[0];
236 char *base = NULL;
237 size_t lorig;
239 /* Zero size array gives internal unit len of 0. Nothing to read. */
240 if (dtp->internal_unit_len == 0
241 && dtp->u.p.current_unit->pad_status == PAD_NO)
242 hit_eof (dtp);
244 /* There are some cases with mixed DTIO where we have read a character
245 and saved it in the last character buffer, so we need to backup. */
246 if (unlikely (dtp->u.p.current_unit->child_dtio > 0 &&
247 dtp->u.p.current_unit->last_char != EOF - 1))
249 dtp->u.p.current_unit->last_char = EOF - 1;
250 sseek (dtp->u.p.current_unit->s, -1, SEEK_CUR);
253 /* To support legacy code we have to scan the input string one byte
254 at a time because we don't know where an early comma may be and the
255 requested length could go past the end of a comma shortened
256 string. We only do this if -std=legacy was given at compile
257 time. We also do not support this on kind=4 strings. */
258 if (unlikely(compile_options.warn_std == 0)) // the slow legacy way.
260 size_t n;
261 size_t tmp = 1;
262 char *q;
264 /* If we have seen an eor previously, return a length of 0. The
265 caller is responsible for correctly padding the input field. */
266 if (dtp->u.p.sf_seen_eor)
268 *length = 0;
269 /* Just return something that isn't a NULL pointer, otherwise the
270 caller thinks an error occurred. */
271 return (char*) empty_string;
274 /* Get the first character of the string to establish the base
275 address and check for comma or end-of-record condition. */
276 base = mem_alloc_r (dtp->u.p.current_unit->s, &tmp);
277 if (tmp == 0)
279 dtp->u.p.sf_seen_eor = 1;
280 *length = 0;
281 return (char*) empty_string;
283 if (*base == ',')
285 dtp->u.p.current_unit->bytes_left--;
286 *length = 0;
287 return (char*) empty_string;
290 /* Now we scan the rest and deal with either an end-of-file
291 condition or a comma, as needed. */
292 for (n = 1; n < *length; n++)
294 q = mem_alloc_r (dtp->u.p.current_unit->s, &tmp);
295 if (tmp == 0)
297 hit_eof (dtp);
298 return NULL;
300 if (*q == ',')
302 dtp->u.p.current_unit->bytes_left -= n;
303 *length = n;
304 break;
308 else // the fast way
310 lorig = *length;
311 if (is_char4_unit(dtp))
313 gfc_char4_t *p = (gfc_char4_t *) mem_alloc_r4 (dtp->u.p.current_unit->s,
314 length);
315 base = fbuf_alloc (dtp->u.p.current_unit, lorig);
316 for (size_t i = 0; i < *length; i++, p++)
317 base[i] = *p > 255 ? '?' : (unsigned char) *p;
319 else
320 base = mem_alloc_r (dtp->u.p.current_unit->s, length);
322 if (unlikely (lorig > *length))
324 hit_eof (dtp);
325 return NULL;
329 dtp->u.p.current_unit->bytes_left -= *length;
331 if (((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0) ||
332 dtp->u.p.current_unit->has_size)
333 dtp->u.p.current_unit->size_used += (GFC_IO_INT) *length;
335 return base;
339 /* When reading sequential formatted records we have a problem. We
340 don't know how long the line is until we read the trailing newline,
341 and we don't want to read too much. If we read too much, we might
342 have to do a physical seek backwards depending on how much data is
343 present, and devices like terminals aren't seekable and would cause
344 an I/O error.
346 Given this, the solution is to read a byte at a time, stopping if
347 we hit the newline. For small allocations, we use a static buffer.
348 For larger allocations, we are forced to allocate memory on the
349 heap. Hopefully this won't happen very often. */
351 /* Read sequential file - external unit */
353 static char *
354 read_sf (st_parameter_dt *dtp, size_t *length)
356 static char *empty_string[0];
357 size_t lorig, n;
358 int q, q2;
359 int seen_comma;
361 /* If we have seen an eor previously, return a length of 0. The
362 caller is responsible for correctly padding the input field. */
363 if (dtp->u.p.sf_seen_eor)
365 *length = 0;
366 /* Just return something that isn't a NULL pointer, otherwise the
367 caller thinks an error occurred. */
368 return (char*) empty_string;
371 /* There are some cases with mixed DTIO where we have read a character
372 and saved it in the last character buffer, so we need to backup. */
373 if (unlikely (dtp->u.p.current_unit->child_dtio > 0 &&
374 dtp->u.p.current_unit->last_char != EOF - 1))
376 dtp->u.p.current_unit->last_char = EOF - 1;
377 fbuf_seek (dtp->u.p.current_unit, -1, SEEK_CUR);
380 n = seen_comma = 0;
382 /* Read data into format buffer and scan through it. */
383 lorig = *length;
385 while (n < *length)
387 q = fbuf_getc (dtp->u.p.current_unit);
388 if (q == EOF)
389 break;
390 else if (dtp->u.p.current_unit->flags.cc != CC_NONE
391 && (q == '\n' || q == '\r'))
393 /* Unexpected end of line. Set the position. */
394 dtp->u.p.sf_seen_eor = 1;
396 /* If we see an EOR during non-advancing I/O, we need to skip
397 the rest of the I/O statement. Set the corresponding flag. */
398 if (dtp->u.p.advance_status == ADVANCE_NO || dtp->u.p.seen_dollar)
399 dtp->u.p.eor_condition = 1;
401 /* If we encounter a CR, it might be a CRLF. */
402 if (q == '\r') /* Probably a CRLF */
404 /* See if there is an LF. */
405 q2 = fbuf_getc (dtp->u.p.current_unit);
406 if (q2 == '\n')
407 dtp->u.p.sf_seen_eor = 2;
408 else if (q2 != EOF) /* Oops, seek back. */
409 fbuf_seek (dtp->u.p.current_unit, -1, SEEK_CUR);
412 /* Without padding, terminate the I/O statement without assigning
413 the value. With padding, the value still needs to be assigned,
414 so we can just continue with a short read. */
415 if (dtp->u.p.current_unit->pad_status == PAD_NO)
417 generate_error (&dtp->common, LIBERROR_EOR, NULL);
418 return NULL;
421 *length = n;
422 goto done;
424 /* Short circuit the read if a comma is found during numeric input.
425 The flag is set to zero during character reads so that commas in
426 strings are not ignored */
427 else if (q == ',')
428 if (dtp->u.p.sf_read_comma == 1)
430 seen_comma = 1;
431 notify_std (&dtp->common, GFC_STD_GNU,
432 "Comma in formatted numeric read.");
433 break;
435 n++;
438 *length = n;
440 /* A short read implies we hit EOF, unless we hit EOR, a comma, or
441 some other stuff. Set the relevant flags. */
442 if (lorig > *length && !dtp->u.p.sf_seen_eor && !seen_comma)
444 if (n > 0)
446 if (dtp->u.p.advance_status == ADVANCE_NO)
448 if (dtp->u.p.current_unit->pad_status == PAD_NO)
450 hit_eof (dtp);
451 return NULL;
453 else
454 dtp->u.p.eor_condition = 1;
456 else
457 dtp->u.p.at_eof = 1;
459 else if (dtp->u.p.advance_status == ADVANCE_NO
460 || dtp->u.p.current_unit->pad_status == PAD_NO
461 || dtp->u.p.current_unit->bytes_left
462 == dtp->u.p.current_unit->recl)
464 hit_eof (dtp);
465 return NULL;
469 done:
471 dtp->u.p.current_unit->bytes_left -= n;
473 if (((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0) ||
474 dtp->u.p.current_unit->has_size)
475 dtp->u.p.current_unit->size_used += (GFC_IO_INT) n;
477 /* We can't call fbuf_getptr before the loop doing fbuf_getc, because
478 fbuf_getc might reallocate the buffer. So return current pointer
479 minus all the advances, which is n plus up to two characters
480 of newline or comma. */
481 return fbuf_getptr (dtp->u.p.current_unit)
482 - n - dtp->u.p.sf_seen_eor - seen_comma;
486 /* Function for reading the next couple of bytes from the current
487 file, advancing the current position. We return NULL on end of record or
488 end of file. This function is only for formatted I/O, unformatted uses
489 read_block_direct.
491 If the read is short, then it is because the current record does not
492 have enough data to satisfy the read request and the file was
493 opened with PAD=YES. The caller must assume tailing spaces for
494 short reads. */
496 void *
497 read_block_form (st_parameter_dt *dtp, size_t *nbytes)
499 char *source;
500 size_t norig;
502 if (!is_stream_io (dtp))
504 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) *nbytes)
506 /* For preconnected units with default record length, set bytes left
507 to unit record length and proceed, otherwise error. */
508 if (dtp->u.p.current_unit->unit_number == options.stdin_unit
509 && dtp->u.p.current_unit->recl == default_recl)
510 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
511 else
513 if (unlikely (dtp->u.p.current_unit->pad_status == PAD_NO)
514 && !is_internal_unit (dtp))
516 /* Not enough data left. */
517 generate_error (&dtp->common, LIBERROR_EOR, NULL);
518 return NULL;
522 if (is_internal_unit(dtp))
524 if (*nbytes > 0 && dtp->u.p.current_unit->bytes_left == 0)
526 if (dtp->u.p.advance_status == ADVANCE_NO)
528 generate_error (&dtp->common, LIBERROR_EOR, NULL);
529 return NULL;
533 else
535 if (unlikely (dtp->u.p.current_unit->bytes_left == 0))
537 hit_eof (dtp);
538 return NULL;
542 *nbytes = dtp->u.p.current_unit->bytes_left;
546 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
547 (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL ||
548 dtp->u.p.current_unit->flags.access == ACCESS_STREAM))
550 if (is_internal_unit (dtp))
551 source = read_sf_internal (dtp, nbytes);
552 else
553 source = read_sf (dtp, nbytes);
555 dtp->u.p.current_unit->strm_pos +=
556 (gfc_offset) (*nbytes + dtp->u.p.sf_seen_eor);
557 return source;
560 /* If we reach here, we can assume it's direct access. */
562 dtp->u.p.current_unit->bytes_left -= (gfc_offset) *nbytes;
564 norig = *nbytes;
565 source = fbuf_read (dtp->u.p.current_unit, nbytes);
566 fbuf_seek (dtp->u.p.current_unit, *nbytes, SEEK_CUR);
568 if (((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0) ||
569 dtp->u.p.current_unit->has_size)
570 dtp->u.p.current_unit->size_used += (GFC_IO_INT) *nbytes;
572 if (norig != *nbytes)
574 /* Short read, this shouldn't happen. */
575 if (dtp->u.p.current_unit->pad_status == PAD_NO)
577 generate_error (&dtp->common, LIBERROR_EOR, NULL);
578 source = NULL;
582 dtp->u.p.current_unit->strm_pos += (gfc_offset) *nbytes;
584 return source;
588 /* Read a block from a character(kind=4) internal unit, to be transferred into
589 a character(kind=4) variable. Note: Portions of this code borrowed from
590 read_sf_internal. */
591 void *
592 read_block_form4 (st_parameter_dt *dtp, size_t *nbytes)
594 static gfc_char4_t *empty_string[0];
595 gfc_char4_t *source;
596 size_t lorig;
598 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) *nbytes)
599 *nbytes = dtp->u.p.current_unit->bytes_left;
601 /* Zero size array gives internal unit len of 0. Nothing to read. */
602 if (dtp->internal_unit_len == 0
603 && dtp->u.p.current_unit->pad_status == PAD_NO)
604 hit_eof (dtp);
606 /* If we have seen an eor previously, return a length of 0. The
607 caller is responsible for correctly padding the input field. */
608 if (dtp->u.p.sf_seen_eor)
610 *nbytes = 0;
611 /* Just return something that isn't a NULL pointer, otherwise the
612 caller thinks an error occurred. */
613 return empty_string;
616 lorig = *nbytes;
617 source = (gfc_char4_t *) mem_alloc_r4 (dtp->u.p.current_unit->s, nbytes);
619 if (unlikely (lorig > *nbytes))
621 hit_eof (dtp);
622 return NULL;
625 dtp->u.p.current_unit->bytes_left -= *nbytes;
627 if (((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0) ||
628 dtp->u.p.current_unit->has_size)
629 dtp->u.p.current_unit->size_used += (GFC_IO_INT) *nbytes;
631 return source;
635 /* Reads a block directly into application data space. This is for
636 unformatted files. */
638 static void
639 read_block_direct (st_parameter_dt *dtp, void *buf, size_t nbytes)
641 ssize_t to_read_record;
642 ssize_t have_read_record;
643 ssize_t to_read_subrecord;
644 ssize_t have_read_subrecord;
645 int short_record;
647 if (is_stream_io (dtp))
649 have_read_record = sread (dtp->u.p.current_unit->s, buf,
650 nbytes);
651 if (unlikely (have_read_record < 0))
653 generate_error (&dtp->common, LIBERROR_OS, NULL);
654 return;
657 dtp->u.p.current_unit->strm_pos += (gfc_offset) have_read_record;
659 if (unlikely ((ssize_t) nbytes != have_read_record))
661 /* Short read, e.g. if we hit EOF. For stream files,
662 we have to set the end-of-file condition. */
663 hit_eof (dtp);
665 return;
668 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
670 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) nbytes)
672 short_record = 1;
673 to_read_record = dtp->u.p.current_unit->bytes_left;
674 nbytes = to_read_record;
676 else
678 short_record = 0;
679 to_read_record = nbytes;
682 dtp->u.p.current_unit->bytes_left -= to_read_record;
684 to_read_record = sread (dtp->u.p.current_unit->s, buf, to_read_record);
685 if (unlikely (to_read_record < 0))
687 generate_error (&dtp->common, LIBERROR_OS, NULL);
688 return;
691 if (to_read_record != (ssize_t) nbytes)
693 /* Short read, e.g. if we hit EOF. Apparently, we read
694 more than was written to the last record. */
695 return;
698 if (unlikely (short_record))
700 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
702 return;
705 /* Unformatted sequential. We loop over the subrecords, reading
706 until the request has been fulfilled or the record has run out
707 of continuation subrecords. */
709 /* Check whether we exceed the total record length. */
711 if (dtp->u.p.current_unit->flags.has_recl
712 && ((gfc_offset) nbytes > dtp->u.p.current_unit->bytes_left))
714 to_read_record = dtp->u.p.current_unit->bytes_left;
715 short_record = 1;
717 else
719 to_read_record = nbytes;
720 short_record = 0;
722 have_read_record = 0;
724 while(1)
726 if (dtp->u.p.current_unit->bytes_left_subrecord
727 < (gfc_offset) to_read_record)
729 to_read_subrecord = dtp->u.p.current_unit->bytes_left_subrecord;
730 to_read_record -= to_read_subrecord;
732 else
734 to_read_subrecord = to_read_record;
735 to_read_record = 0;
738 dtp->u.p.current_unit->bytes_left_subrecord -= to_read_subrecord;
740 have_read_subrecord = sread (dtp->u.p.current_unit->s,
741 buf + have_read_record, to_read_subrecord);
742 if (unlikely (have_read_subrecord < 0))
744 generate_error (&dtp->common, LIBERROR_OS, NULL);
745 return;
748 have_read_record += have_read_subrecord;
750 if (unlikely (to_read_subrecord != have_read_subrecord))
752 /* Short read, e.g. if we hit EOF. This means the record
753 structure has been corrupted, or the trailing record
754 marker would still be present. */
756 generate_error (&dtp->common, LIBERROR_CORRUPT_FILE, NULL);
757 return;
760 if (to_read_record > 0)
762 if (likely (dtp->u.p.current_unit->continued))
764 next_record_r_unf (dtp, 0);
765 us_read (dtp, 1);
767 else
769 /* Let's make sure the file position is correctly pre-positioned
770 for the next read statement. */
772 dtp->u.p.current_unit->current_record = 0;
773 next_record_r_unf (dtp, 0);
774 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
775 return;
778 else
780 /* Normal exit, the read request has been fulfilled. */
781 break;
785 dtp->u.p.current_unit->bytes_left -= have_read_record;
786 if (unlikely (short_record))
788 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
789 return;
791 return;
795 /* Function for writing a block of bytes to the current file at the
796 current position, advancing the file pointer. We are given a length
797 and return a pointer to a buffer that the caller must (completely)
798 fill in. Returns NULL on error. */
800 void *
801 write_block (st_parameter_dt *dtp, size_t length)
803 char *dest;
805 if (!is_stream_io (dtp))
807 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) length)
809 /* For preconnected units with default record length, set bytes left
810 to unit record length and proceed, otherwise error. */
811 if (likely ((dtp->u.p.current_unit->unit_number
812 == options.stdout_unit
813 || dtp->u.p.current_unit->unit_number
814 == options.stderr_unit)
815 && dtp->u.p.current_unit->recl == default_recl))
816 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
817 else
819 generate_error (&dtp->common, LIBERROR_EOR, NULL);
820 return NULL;
824 dtp->u.p.current_unit->bytes_left -= (gfc_offset) length;
827 if (is_internal_unit (dtp))
829 if (is_char4_unit(dtp)) /* char4 internel unit. */
831 gfc_char4_t *dest4;
832 dest4 = mem_alloc_w4 (dtp->u.p.current_unit->s, &length);
833 if (dest4 == NULL)
835 generate_error (&dtp->common, LIBERROR_END, NULL);
836 return NULL;
838 return dest4;
840 else
841 dest = mem_alloc_w (dtp->u.p.current_unit->s, &length);
843 if (dest == NULL)
845 generate_error (&dtp->common, LIBERROR_END, NULL);
846 return NULL;
849 if (unlikely (dtp->u.p.current_unit->endfile == AT_ENDFILE))
850 generate_error (&dtp->common, LIBERROR_END, NULL);
852 else
854 dest = fbuf_alloc (dtp->u.p.current_unit, length);
855 if (dest == NULL)
857 generate_error (&dtp->common, LIBERROR_OS, NULL);
858 return NULL;
862 if (((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0) ||
863 dtp->u.p.current_unit->has_size)
864 dtp->u.p.current_unit->size_used += (GFC_IO_INT) length;
866 dtp->u.p.current_unit->strm_pos += (gfc_offset) length;
868 return dest;
872 /* High level interface to swrite(), taking care of errors. This is only
873 called for unformatted files. There are three cases to consider:
874 Stream I/O, unformatted direct, unformatted sequential. */
876 static bool
877 write_buf (st_parameter_dt *dtp, void *buf, size_t nbytes)
880 ssize_t have_written;
881 ssize_t to_write_subrecord;
882 int short_record;
884 /* Stream I/O. */
886 if (is_stream_io (dtp))
888 have_written = swrite (dtp->u.p.current_unit->s, buf, nbytes);
889 if (unlikely (have_written < 0))
891 generate_error (&dtp->common, LIBERROR_OS, NULL);
892 return false;
895 dtp->u.p.current_unit->strm_pos += (gfc_offset) have_written;
897 return true;
900 /* Unformatted direct access. */
902 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
904 if (unlikely (dtp->u.p.current_unit->bytes_left < (gfc_offset) nbytes))
906 generate_error (&dtp->common, LIBERROR_DIRECT_EOR, NULL);
907 return false;
910 if (buf == NULL && nbytes == 0)
911 return true;
913 have_written = swrite (dtp->u.p.current_unit->s, buf, nbytes);
914 if (unlikely (have_written < 0))
916 generate_error (&dtp->common, LIBERROR_OS, NULL);
917 return false;
920 dtp->u.p.current_unit->strm_pos += (gfc_offset) have_written;
921 dtp->u.p.current_unit->bytes_left -= (gfc_offset) have_written;
923 return true;
926 /* Unformatted sequential. */
928 have_written = 0;
930 if (dtp->u.p.current_unit->flags.has_recl
931 && (gfc_offset) nbytes > dtp->u.p.current_unit->bytes_left)
933 nbytes = dtp->u.p.current_unit->bytes_left;
934 short_record = 1;
936 else
938 short_record = 0;
941 while (1)
944 to_write_subrecord =
945 (size_t) dtp->u.p.current_unit->bytes_left_subrecord < nbytes ?
946 (size_t) dtp->u.p.current_unit->bytes_left_subrecord : nbytes;
948 dtp->u.p.current_unit->bytes_left_subrecord -=
949 (gfc_offset) to_write_subrecord;
951 to_write_subrecord = swrite (dtp->u.p.current_unit->s,
952 buf + have_written, to_write_subrecord);
953 if (unlikely (to_write_subrecord < 0))
955 generate_error (&dtp->common, LIBERROR_OS, NULL);
956 return false;
959 dtp->u.p.current_unit->strm_pos += (gfc_offset) to_write_subrecord;
960 nbytes -= to_write_subrecord;
961 have_written += to_write_subrecord;
963 if (nbytes == 0)
964 break;
966 next_record_w_unf (dtp, 1);
967 us_write (dtp, 1);
969 dtp->u.p.current_unit->bytes_left -= have_written;
970 if (unlikely (short_record))
972 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
973 return false;
975 return true;
979 /* Reverse memcpy - used for byte swapping. */
981 static void
982 reverse_memcpy (void *dest, const void *src, size_t n)
984 char *d, *s;
985 size_t i;
987 d = (char *) dest;
988 s = (char *) src + n - 1;
990 /* Write with ascending order - this is likely faster
991 on modern architectures because of write combining. */
992 for (i=0; i<n; i++)
993 *(d++) = *(s--);
997 /* Utility function for byteswapping an array, using the bswap
998 builtins if possible. dest and src can overlap completely, or then
999 they must point to separate objects; partial overlaps are not
1000 allowed. */
1002 static void
1003 bswap_array (void *dest, const void *src, size_t size, size_t nelems)
1005 const char *ps;
1006 char *pd;
1008 switch (size)
1010 case 1:
1011 break;
1012 case 2:
1013 for (size_t i = 0; i < nelems; i++)
1014 ((uint16_t*)dest)[i] = __builtin_bswap16 (((uint16_t*)src)[i]);
1015 break;
1016 case 4:
1017 for (size_t i = 0; i < nelems; i++)
1018 ((uint32_t*)dest)[i] = __builtin_bswap32 (((uint32_t*)src)[i]);
1019 break;
1020 case 8:
1021 for (size_t i = 0; i < nelems; i++)
1022 ((uint64_t*)dest)[i] = __builtin_bswap64 (((uint64_t*)src)[i]);
1023 break;
1024 case 12:
1025 ps = src;
1026 pd = dest;
1027 for (size_t i = 0; i < nelems; i++)
1029 uint32_t tmp;
1030 memcpy (&tmp, ps, 4);
1031 *(uint32_t*)pd = __builtin_bswap32 (*(uint32_t*)(ps + 8));
1032 *(uint32_t*)(pd + 4) = __builtin_bswap32 (*(uint32_t*)(ps + 4));
1033 *(uint32_t*)(pd + 8) = __builtin_bswap32 (tmp);
1034 ps += size;
1035 pd += size;
1037 break;
1038 case 16:
1039 ps = src;
1040 pd = dest;
1041 for (size_t i = 0; i < nelems; i++)
1043 uint64_t tmp;
1044 memcpy (&tmp, ps, 8);
1045 *(uint64_t*)pd = __builtin_bswap64 (*(uint64_t*)(ps + 8));
1046 *(uint64_t*)(pd + 8) = __builtin_bswap64 (tmp);
1047 ps += size;
1048 pd += size;
1050 break;
1051 default:
1052 pd = dest;
1053 if (dest != src)
1055 ps = src;
1056 for (size_t i = 0; i < nelems; i++)
1058 reverse_memcpy (pd, ps, size);
1059 ps += size;
1060 pd += size;
1063 else
1065 /* In-place byte swap. */
1066 for (size_t i = 0; i < nelems; i++)
1068 char tmp, *low = pd, *high = pd + size - 1;
1069 for (size_t j = 0; j < size/2; j++)
1071 tmp = *low;
1072 *low = *high;
1073 *high = tmp;
1074 low++;
1075 high--;
1077 pd += size;
1084 /* Master function for unformatted reads. */
1086 static void
1087 unformatted_read (st_parameter_dt *dtp, bt type,
1088 void *dest, int kind, size_t size, size_t nelems)
1090 if (type == BT_CLASS)
1092 int unit = dtp->u.p.current_unit->unit_number;
1093 char tmp_iomsg[IOMSG_LEN] = "";
1094 char *child_iomsg;
1095 gfc_charlen_type child_iomsg_len;
1096 int noiostat;
1097 int *child_iostat = NULL;
1099 /* Set iostat, intent(out). */
1100 noiostat = 0;
1101 child_iostat = (dtp->common.flags & IOPARM_HAS_IOSTAT) ?
1102 dtp->common.iostat : &noiostat;
1104 /* Set iomsg, intent(inout). */
1105 if (dtp->common.flags & IOPARM_HAS_IOMSG)
1107 child_iomsg = dtp->common.iomsg;
1108 child_iomsg_len = dtp->common.iomsg_len;
1110 else
1112 child_iomsg = tmp_iomsg;
1113 child_iomsg_len = IOMSG_LEN;
1116 /* Call the user defined unformatted READ procedure. */
1117 dtp->u.p.current_unit->child_dtio++;
1118 dtp->u.p.ufdtio_ptr (dest, &unit, child_iostat, child_iomsg,
1119 child_iomsg_len);
1120 dtp->u.p.current_unit->child_dtio--;
1121 return;
1124 if (type == BT_CHARACTER)
1125 size *= GFC_SIZE_OF_CHAR_KIND(kind);
1126 read_block_direct (dtp, dest, size * nelems);
1128 if (unlikely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_SWAP)
1129 && kind != 1)
1131 /* Handle wide chracters. */
1132 if (type == BT_CHARACTER)
1134 nelems *= size;
1135 size = kind;
1138 /* Break up complex into its constituent reals. */
1139 else if (type == BT_COMPLEX)
1141 nelems *= 2;
1142 size /= 2;
1144 bswap_array (dest, dest, size, nelems);
1149 /* Master function for unformatted writes. NOTE: For kind=10 the size is 16
1150 bytes on 64 bit machines. The unused bytes are not initialized and never
1151 used, which can show an error with memory checking analyzers like
1152 valgrind. We us BT_CLASS to denote a User Defined I/O call. */
1154 static void
1155 unformatted_write (st_parameter_dt *dtp, bt type,
1156 void *source, int kind, size_t size, size_t nelems)
1158 if (type == BT_CLASS)
1160 int unit = dtp->u.p.current_unit->unit_number;
1161 char tmp_iomsg[IOMSG_LEN] = "";
1162 char *child_iomsg;
1163 gfc_charlen_type child_iomsg_len;
1164 int noiostat;
1165 int *child_iostat = NULL;
1167 /* Set iostat, intent(out). */
1168 noiostat = 0;
1169 child_iostat = (dtp->common.flags & IOPARM_HAS_IOSTAT) ?
1170 dtp->common.iostat : &noiostat;
1172 /* Set iomsg, intent(inout). */
1173 if (dtp->common.flags & IOPARM_HAS_IOMSG)
1175 child_iomsg = dtp->common.iomsg;
1176 child_iomsg_len = dtp->common.iomsg_len;
1178 else
1180 child_iomsg = tmp_iomsg;
1181 child_iomsg_len = IOMSG_LEN;
1184 /* Call the user defined unformatted WRITE procedure. */
1185 dtp->u.p.current_unit->child_dtio++;
1186 dtp->u.p.ufdtio_ptr (source, &unit, child_iostat, child_iomsg,
1187 child_iomsg_len);
1188 dtp->u.p.current_unit->child_dtio--;
1189 return;
1192 if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE)
1193 || kind == 1)
1195 size_t stride = type == BT_CHARACTER ?
1196 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
1198 write_buf (dtp, source, stride * nelems);
1200 else
1202 #define BSWAP_BUFSZ 512
1203 char buffer[BSWAP_BUFSZ];
1204 char *p;
1205 size_t nrem;
1207 p = source;
1209 /* Handle wide chracters. */
1210 if (type == BT_CHARACTER && kind != 1)
1212 nelems *= size;
1213 size = kind;
1216 /* Break up complex into its constituent reals. */
1217 if (type == BT_COMPLEX)
1219 nelems *= 2;
1220 size /= 2;
1223 /* By now, all complex variables have been split into their
1224 constituent reals. */
1226 nrem = nelems;
1229 size_t nc;
1230 if (size * nrem > BSWAP_BUFSZ)
1231 nc = BSWAP_BUFSZ / size;
1232 else
1233 nc = nrem;
1235 bswap_array (buffer, p, size, nc);
1236 write_buf (dtp, buffer, size * nc);
1237 p += size * nc;
1238 nrem -= nc;
1240 while (nrem > 0);
1245 /* Return a pointer to the name of a type. */
1247 const char *
1248 type_name (bt type)
1250 const char *p;
1252 switch (type)
1254 case BT_INTEGER:
1255 p = "INTEGER";
1256 break;
1257 case BT_LOGICAL:
1258 p = "LOGICAL";
1259 break;
1260 case BT_CHARACTER:
1261 p = "CHARACTER";
1262 break;
1263 case BT_REAL:
1264 p = "REAL";
1265 break;
1266 case BT_COMPLEX:
1267 p = "COMPLEX";
1268 break;
1269 case BT_CLASS:
1270 p = "CLASS or DERIVED";
1271 break;
1272 default:
1273 internal_error (NULL, "type_name(): Bad type");
1276 return p;
1280 /* Write a constant string to the output.
1281 This is complicated because the string can have doubled delimiters
1282 in it. The length in the format node is the true length. */
1284 static void
1285 write_constant_string (st_parameter_dt *dtp, const fnode *f)
1287 char c, delimiter, *p, *q;
1288 int length;
1290 length = f->u.string.length;
1291 if (length == 0)
1292 return;
1294 p = write_block (dtp, length);
1295 if (p == NULL)
1296 return;
1298 q = f->u.string.p;
1299 delimiter = q[-1];
1301 for (; length > 0; length--)
1303 c = *p++ = *q++;
1304 if (c == delimiter && c != 'H' && c != 'h')
1305 q++; /* Skip the doubled delimiter. */
1310 /* Given actual and expected types in a formatted data transfer, make
1311 sure they agree. If not, an error message is generated. Returns
1312 nonzero if something went wrong. */
1314 static int
1315 require_type (st_parameter_dt *dtp, bt expected, bt actual, const fnode *f)
1317 #define BUFLEN 100
1318 char buffer[BUFLEN];
1320 if (actual == expected)
1321 return 0;
1323 /* Adjust item_count before emitting error message. */
1324 snprintf (buffer, BUFLEN,
1325 "Expected %s for item %d in formatted transfer, got %s",
1326 type_name (expected), dtp->u.p.item_count - 1, type_name (actual));
1328 format_error (dtp, f, buffer);
1329 return 1;
1333 /* Check that the dtio procedure required for formatted IO is present. */
1335 static int
1336 check_dtio_proc (st_parameter_dt *dtp, const fnode *f)
1338 char buffer[BUFLEN];
1340 if (dtp->u.p.fdtio_ptr != NULL)
1341 return 0;
1343 snprintf (buffer, BUFLEN,
1344 "Missing DTIO procedure or intrinsic type passed for item %d "
1345 "in formatted transfer",
1346 dtp->u.p.item_count - 1);
1348 format_error (dtp, f, buffer);
1349 return 1;
1353 static int
1354 require_numeric_type (st_parameter_dt *dtp, bt actual, const fnode *f)
1356 #define BUFLEN 100
1357 char buffer[BUFLEN];
1359 if (actual == BT_INTEGER || actual == BT_REAL || actual == BT_COMPLEX)
1360 return 0;
1362 /* Adjust item_count before emitting error message. */
1363 snprintf (buffer, BUFLEN,
1364 "Expected numeric type for item %d in formatted transfer, got %s",
1365 dtp->u.p.item_count - 1, type_name (actual));
1367 format_error (dtp, f, buffer);
1368 return 1;
1371 static char *
1372 get_dt_format (char *p, gfc_charlen_type *length)
1374 char delim = p[-1]; /* The delimiter is always the first character back. */
1375 char c, *q, *res;
1376 gfc_charlen_type len = *length; /* This length already correct, less 'DT'. */
1378 res = q = xmalloc (len + 2);
1380 /* Set the beginning of the string to 'DT', length adjusted below. */
1381 *q++ = 'D';
1382 *q++ = 'T';
1384 /* The string may contain doubled quotes so scan and skip as needed. */
1385 for (; len > 0; len--)
1387 c = *q++ = *p++;
1388 if (c == delim)
1389 p++; /* Skip the doubled delimiter. */
1392 /* Adjust the string length by two now that we are done. */
1393 *length += 2;
1395 return res;
1399 /* This function is in the main loop for a formatted data transfer
1400 statement. It would be natural to implement this as a coroutine
1401 with the user program, but C makes that awkward. We loop,
1402 processing format elements. When we actually have to transfer
1403 data instead of just setting flags, we return control to the user
1404 program which calls a function that supplies the address and type
1405 of the next element, then comes back here to process it. */
1407 static void
1408 formatted_transfer_scalar_read (st_parameter_dt *dtp, bt type, void *p, int kind,
1409 size_t size)
1411 int pos, bytes_used;
1412 const fnode *f;
1413 format_token t;
1414 int n;
1415 int consume_data_flag;
1417 /* Change a complex data item into a pair of reals. */
1419 n = (p == NULL) ? 0 : ((type != BT_COMPLEX) ? 1 : 2);
1420 if (type == BT_COMPLEX)
1422 type = BT_REAL;
1423 size /= 2;
1426 /* If there's an EOR condition, we simulate finalizing the transfer
1427 by doing nothing. */
1428 if (dtp->u.p.eor_condition)
1429 return;
1431 /* Set this flag so that commas in reads cause the read to complete before
1432 the entire field has been read. The next read field will start right after
1433 the comma in the stream. (Set to 0 for character reads). */
1434 dtp->u.p.sf_read_comma =
1435 dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA ? 0 : 1;
1437 for (;;)
1439 /* If reversion has occurred and there is another real data item,
1440 then we have to move to the next record. */
1441 if (dtp->u.p.reversion_flag && n > 0)
1443 dtp->u.p.reversion_flag = 0;
1444 next_record (dtp, 0);
1447 consume_data_flag = 1;
1448 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1449 break;
1451 f = next_format (dtp);
1452 if (f == NULL)
1454 /* No data descriptors left. */
1455 if (unlikely (n > 0))
1456 generate_error (&dtp->common, LIBERROR_FORMAT,
1457 "Insufficient data descriptors in format after reversion");
1458 return;
1461 t = f->format;
1463 bytes_used = (int)(dtp->u.p.current_unit->recl
1464 - dtp->u.p.current_unit->bytes_left);
1466 if (is_stream_io(dtp))
1467 bytes_used = 0;
1469 switch (t)
1471 case FMT_I:
1472 if (n == 0)
1473 goto need_read_data;
1474 if (require_type (dtp, BT_INTEGER, type, f))
1475 return;
1476 read_decimal (dtp, f, p, kind);
1477 break;
1479 case FMT_B:
1480 if (n == 0)
1481 goto need_read_data;
1482 if (!(compile_options.allow_std & GFC_STD_GNU)
1483 && require_numeric_type (dtp, type, f))
1484 return;
1485 if (!(compile_options.allow_std & GFC_STD_F2008)
1486 && require_type (dtp, BT_INTEGER, type, f))
1487 return;
1488 read_radix (dtp, f, p, kind, 2);
1489 break;
1491 case FMT_O:
1492 if (n == 0)
1493 goto need_read_data;
1494 if (!(compile_options.allow_std & GFC_STD_GNU)
1495 && require_numeric_type (dtp, type, f))
1496 return;
1497 if (!(compile_options.allow_std & GFC_STD_F2008)
1498 && require_type (dtp, BT_INTEGER, type, f))
1499 return;
1500 read_radix (dtp, f, p, kind, 8);
1501 break;
1503 case FMT_Z:
1504 if (n == 0)
1505 goto need_read_data;
1506 if (!(compile_options.allow_std & GFC_STD_GNU)
1507 && require_numeric_type (dtp, type, f))
1508 return;
1509 if (!(compile_options.allow_std & GFC_STD_F2008)
1510 && require_type (dtp, BT_INTEGER, type, f))
1511 return;
1512 read_radix (dtp, f, p, kind, 16);
1513 break;
1515 case FMT_A:
1516 if (n == 0)
1517 goto need_read_data;
1519 /* It is possible to have FMT_A with something not BT_CHARACTER such
1520 as when writing out hollerith strings, so check both type
1521 and kind before calling wide character routines. */
1522 if (type == BT_CHARACTER && kind == 4)
1523 read_a_char4 (dtp, f, p, size);
1524 else
1525 read_a (dtp, f, p, size);
1526 break;
1528 case FMT_L:
1529 if (n == 0)
1530 goto need_read_data;
1531 read_l (dtp, f, p, kind);
1532 break;
1534 case FMT_D:
1535 if (n == 0)
1536 goto need_read_data;
1537 if (require_type (dtp, BT_REAL, type, f))
1538 return;
1539 read_f (dtp, f, p, kind);
1540 break;
1542 case FMT_DT:
1543 if (n == 0)
1544 goto need_read_data;
1546 if (check_dtio_proc (dtp, f))
1547 return;
1548 if (require_type (dtp, BT_CLASS, type, f))
1549 return;
1550 int unit = dtp->u.p.current_unit->unit_number;
1551 char dt[] = "DT";
1552 char tmp_iomsg[IOMSG_LEN] = "";
1553 char *child_iomsg;
1554 gfc_charlen_type child_iomsg_len;
1555 int noiostat;
1556 int *child_iostat = NULL;
1557 char *iotype;
1558 gfc_charlen_type iotype_len = f->u.udf.string_len;
1560 /* Build the iotype string. */
1561 if (iotype_len == 0)
1563 iotype_len = 2;
1564 iotype = dt;
1566 else
1567 iotype = get_dt_format (f->u.udf.string, &iotype_len);
1569 /* Set iostat, intent(out). */
1570 noiostat = 0;
1571 child_iostat = (dtp->common.flags & IOPARM_HAS_IOSTAT) ?
1572 dtp->common.iostat : &noiostat;
1574 /* Set iomsg, intent(inout). */
1575 if (dtp->common.flags & IOPARM_HAS_IOMSG)
1577 child_iomsg = dtp->common.iomsg;
1578 child_iomsg_len = dtp->common.iomsg_len;
1580 else
1582 child_iomsg = tmp_iomsg;
1583 child_iomsg_len = IOMSG_LEN;
1586 /* Call the user defined formatted READ procedure. */
1587 dtp->u.p.current_unit->child_dtio++;
1588 dtp->u.p.current_unit->last_char = EOF - 1;
1589 dtp->u.p.fdtio_ptr (p, &unit, iotype, f->u.udf.vlist,
1590 child_iostat, child_iomsg,
1591 iotype_len, child_iomsg_len);
1592 dtp->u.p.current_unit->child_dtio--;
1594 if (f->u.udf.string_len != 0)
1595 free (iotype);
1596 /* Note: vlist is freed in free_format_data. */
1597 break;
1599 case FMT_E:
1600 if (n == 0)
1601 goto need_read_data;
1602 if (require_type (dtp, BT_REAL, type, f))
1603 return;
1604 read_f (dtp, f, p, kind);
1605 break;
1607 case FMT_EN:
1608 if (n == 0)
1609 goto need_read_data;
1610 if (require_type (dtp, BT_REAL, type, f))
1611 return;
1612 read_f (dtp, f, p, kind);
1613 break;
1615 case FMT_ES:
1616 if (n == 0)
1617 goto need_read_data;
1618 if (require_type (dtp, BT_REAL, type, f))
1619 return;
1620 read_f (dtp, f, p, kind);
1621 break;
1623 case FMT_F:
1624 if (n == 0)
1625 goto need_read_data;
1626 if (require_type (dtp, BT_REAL, type, f))
1627 return;
1628 read_f (dtp, f, p, kind);
1629 break;
1631 case FMT_G:
1632 if (n == 0)
1633 goto need_read_data;
1634 switch (type)
1636 case BT_INTEGER:
1637 read_decimal (dtp, f, p, kind);
1638 break;
1639 case BT_LOGICAL:
1640 read_l (dtp, f, p, kind);
1641 break;
1642 case BT_CHARACTER:
1643 if (kind == 4)
1644 read_a_char4 (dtp, f, p, size);
1645 else
1646 read_a (dtp, f, p, size);
1647 break;
1648 case BT_REAL:
1649 read_f (dtp, f, p, kind);
1650 break;
1651 default:
1652 internal_error (&dtp->common,
1653 "formatted_transfer (): Bad type");
1655 break;
1657 case FMT_STRING:
1658 consume_data_flag = 0;
1659 format_error (dtp, f, "Constant string in input format");
1660 return;
1662 /* Format codes that don't transfer data. */
1663 case FMT_X:
1664 case FMT_TR:
1665 consume_data_flag = 0;
1666 dtp->u.p.skips += f->u.n;
1667 pos = bytes_used + dtp->u.p.skips - 1;
1668 dtp->u.p.pending_spaces = pos - dtp->u.p.max_pos + 1;
1669 read_x (dtp, f->u.n);
1670 break;
1672 case FMT_TL:
1673 case FMT_T:
1674 consume_data_flag = 0;
1676 if (f->format == FMT_TL)
1678 /* Handle the special case when no bytes have been used yet.
1679 Cannot go below zero. */
1680 if (bytes_used == 0)
1682 dtp->u.p.pending_spaces -= f->u.n;
1683 dtp->u.p.skips -= f->u.n;
1684 dtp->u.p.skips = dtp->u.p.skips < 0 ? 0 : dtp->u.p.skips;
1687 pos = bytes_used - f->u.n;
1689 else /* FMT_T */
1690 pos = f->u.n - 1;
1692 /* Standard 10.6.1.1: excessive left tabbing is reset to the
1693 left tab limit. We do not check if the position has gone
1694 beyond the end of record because a subsequent tab could
1695 bring us back again. */
1696 pos = pos < 0 ? 0 : pos;
1698 dtp->u.p.skips = dtp->u.p.skips + pos - bytes_used;
1699 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces
1700 + pos - dtp->u.p.max_pos;
1701 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces < 0
1702 ? 0 : dtp->u.p.pending_spaces;
1703 if (dtp->u.p.skips == 0)
1704 break;
1706 /* Adjust everything for end-of-record condition */
1707 if (dtp->u.p.sf_seen_eor && !is_internal_unit (dtp))
1709 dtp->u.p.current_unit->bytes_left -= dtp->u.p.sf_seen_eor;
1710 dtp->u.p.skips -= dtp->u.p.sf_seen_eor;
1711 bytes_used = pos;
1712 if (dtp->u.p.pending_spaces == 0)
1713 dtp->u.p.sf_seen_eor = 0;
1715 if (dtp->u.p.skips < 0)
1717 if (is_internal_unit (dtp))
1718 sseek (dtp->u.p.current_unit->s, dtp->u.p.skips, SEEK_CUR);
1719 else
1720 fbuf_seek (dtp->u.p.current_unit, dtp->u.p.skips, SEEK_CUR);
1721 dtp->u.p.current_unit->bytes_left -= (gfc_offset) dtp->u.p.skips;
1722 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1724 else
1725 read_x (dtp, dtp->u.p.skips);
1726 break;
1728 case FMT_S:
1729 consume_data_flag = 0;
1730 dtp->u.p.sign_status = SIGN_S;
1731 break;
1733 case FMT_SS:
1734 consume_data_flag = 0;
1735 dtp->u.p.sign_status = SIGN_SS;
1736 break;
1738 case FMT_SP:
1739 consume_data_flag = 0;
1740 dtp->u.p.sign_status = SIGN_SP;
1741 break;
1743 case FMT_BN:
1744 consume_data_flag = 0 ;
1745 dtp->u.p.blank_status = BLANK_NULL;
1746 break;
1748 case FMT_BZ:
1749 consume_data_flag = 0;
1750 dtp->u.p.blank_status = BLANK_ZERO;
1751 break;
1753 case FMT_DC:
1754 consume_data_flag = 0;
1755 dtp->u.p.current_unit->decimal_status = DECIMAL_COMMA;
1756 break;
1758 case FMT_DP:
1759 consume_data_flag = 0;
1760 dtp->u.p.current_unit->decimal_status = DECIMAL_POINT;
1761 break;
1763 case FMT_RC:
1764 consume_data_flag = 0;
1765 dtp->u.p.current_unit->round_status = ROUND_COMPATIBLE;
1766 break;
1768 case FMT_RD:
1769 consume_data_flag = 0;
1770 dtp->u.p.current_unit->round_status = ROUND_DOWN;
1771 break;
1773 case FMT_RN:
1774 consume_data_flag = 0;
1775 dtp->u.p.current_unit->round_status = ROUND_NEAREST;
1776 break;
1778 case FMT_RP:
1779 consume_data_flag = 0;
1780 dtp->u.p.current_unit->round_status = ROUND_PROCDEFINED;
1781 break;
1783 case FMT_RU:
1784 consume_data_flag = 0;
1785 dtp->u.p.current_unit->round_status = ROUND_UP;
1786 break;
1788 case FMT_RZ:
1789 consume_data_flag = 0;
1790 dtp->u.p.current_unit->round_status = ROUND_ZERO;
1791 break;
1793 case FMT_P:
1794 consume_data_flag = 0;
1795 dtp->u.p.scale_factor = f->u.k;
1796 break;
1798 case FMT_DOLLAR:
1799 consume_data_flag = 0;
1800 dtp->u.p.seen_dollar = 1;
1801 break;
1803 case FMT_SLASH:
1804 consume_data_flag = 0;
1805 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1806 next_record (dtp, 0);
1807 break;
1809 case FMT_COLON:
1810 /* A colon descriptor causes us to exit this loop (in
1811 particular preventing another / descriptor from being
1812 processed) unless there is another data item to be
1813 transferred. */
1814 consume_data_flag = 0;
1815 if (n == 0)
1816 return;
1817 break;
1819 default:
1820 internal_error (&dtp->common, "Bad format node");
1823 /* Adjust the item count and data pointer. */
1825 if ((consume_data_flag > 0) && (n > 0))
1827 n--;
1828 p = ((char *) p) + size;
1831 dtp->u.p.skips = 0;
1833 pos = (int)(dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left);
1834 dtp->u.p.max_pos = (dtp->u.p.max_pos > pos) ? dtp->u.p.max_pos : pos;
1837 return;
1839 /* Come here when we need a data descriptor but don't have one. We
1840 push the current format node back onto the input, then return and
1841 let the user program call us back with the data. */
1842 need_read_data:
1843 unget_format (dtp, f);
1847 static void
1848 formatted_transfer_scalar_write (st_parameter_dt *dtp, bt type, void *p, int kind,
1849 size_t size)
1851 gfc_offset pos, bytes_used;
1852 const fnode *f;
1853 format_token t;
1854 int n;
1855 int consume_data_flag;
1857 /* Change a complex data item into a pair of reals. */
1859 n = (p == NULL) ? 0 : ((type != BT_COMPLEX) ? 1 : 2);
1860 if (type == BT_COMPLEX)
1862 type = BT_REAL;
1863 size /= 2;
1866 /* If there's an EOR condition, we simulate finalizing the transfer
1867 by doing nothing. */
1868 if (dtp->u.p.eor_condition)
1869 return;
1871 /* Set this flag so that commas in reads cause the read to complete before
1872 the entire field has been read. The next read field will start right after
1873 the comma in the stream. (Set to 0 for character reads). */
1874 dtp->u.p.sf_read_comma =
1875 dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA ? 0 : 1;
1877 for (;;)
1879 /* If reversion has occurred and there is another real data item,
1880 then we have to move to the next record. */
1881 if (dtp->u.p.reversion_flag && n > 0)
1883 dtp->u.p.reversion_flag = 0;
1884 next_record (dtp, 0);
1887 consume_data_flag = 1;
1888 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1889 break;
1891 f = next_format (dtp);
1892 if (f == NULL)
1894 /* No data descriptors left. */
1895 if (unlikely (n > 0))
1896 generate_error (&dtp->common, LIBERROR_FORMAT,
1897 "Insufficient data descriptors in format after reversion");
1898 return;
1901 /* Now discharge T, TR and X movements to the right. This is delayed
1902 until a data producing format to suppress trailing spaces. */
1904 t = f->format;
1905 if (dtp->u.p.mode == WRITING && dtp->u.p.skips != 0
1906 && ((n>0 && ( t == FMT_I || t == FMT_B || t == FMT_O
1907 || t == FMT_Z || t == FMT_F || t == FMT_E
1908 || t == FMT_EN || t == FMT_ES || t == FMT_G
1909 || t == FMT_L || t == FMT_A || t == FMT_D
1910 || t == FMT_DT))
1911 || t == FMT_STRING))
1913 if (dtp->u.p.skips > 0)
1915 gfc_offset tmp;
1916 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
1917 tmp = dtp->u.p.current_unit->recl
1918 - dtp->u.p.current_unit->bytes_left;
1919 dtp->u.p.max_pos =
1920 dtp->u.p.max_pos > tmp ? dtp->u.p.max_pos : tmp;
1921 dtp->u.p.skips = 0;
1923 if (dtp->u.p.skips < 0)
1925 if (is_internal_unit (dtp))
1926 sseek (dtp->u.p.current_unit->s, dtp->u.p.skips, SEEK_CUR);
1927 else
1928 fbuf_seek (dtp->u.p.current_unit, dtp->u.p.skips, SEEK_CUR);
1929 dtp->u.p.current_unit->bytes_left -= (gfc_offset) dtp->u.p.skips;
1931 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1934 bytes_used = dtp->u.p.current_unit->recl
1935 - dtp->u.p.current_unit->bytes_left;
1937 if (is_stream_io(dtp))
1938 bytes_used = 0;
1940 switch (t)
1942 case FMT_I:
1943 if (n == 0)
1944 goto need_data;
1945 if (require_type (dtp, BT_INTEGER, type, f))
1946 return;
1947 write_i (dtp, f, p, kind);
1948 break;
1950 case FMT_B:
1951 if (n == 0)
1952 goto need_data;
1953 if (!(compile_options.allow_std & GFC_STD_GNU)
1954 && require_numeric_type (dtp, type, f))
1955 return;
1956 if (!(compile_options.allow_std & GFC_STD_F2008)
1957 && require_type (dtp, BT_INTEGER, type, f))
1958 return;
1959 write_b (dtp, f, p, kind);
1960 break;
1962 case FMT_O:
1963 if (n == 0)
1964 goto need_data;
1965 if (!(compile_options.allow_std & GFC_STD_GNU)
1966 && require_numeric_type (dtp, type, f))
1967 return;
1968 if (!(compile_options.allow_std & GFC_STD_F2008)
1969 && require_type (dtp, BT_INTEGER, type, f))
1970 return;
1971 write_o (dtp, f, p, kind);
1972 break;
1974 case FMT_Z:
1975 if (n == 0)
1976 goto need_data;
1977 if (!(compile_options.allow_std & GFC_STD_GNU)
1978 && require_numeric_type (dtp, type, f))
1979 return;
1980 if (!(compile_options.allow_std & GFC_STD_F2008)
1981 && require_type (dtp, BT_INTEGER, type, f))
1982 return;
1983 write_z (dtp, f, p, kind);
1984 break;
1986 case FMT_A:
1987 if (n == 0)
1988 goto need_data;
1990 /* It is possible to have FMT_A with something not BT_CHARACTER such
1991 as when writing out hollerith strings, so check both type
1992 and kind before calling wide character routines. */
1993 if (type == BT_CHARACTER && kind == 4)
1994 write_a_char4 (dtp, f, p, size);
1995 else
1996 write_a (dtp, f, p, size);
1997 break;
1999 case FMT_L:
2000 if (n == 0)
2001 goto need_data;
2002 write_l (dtp, f, p, kind);
2003 break;
2005 case FMT_D:
2006 if (n == 0)
2007 goto need_data;
2008 if (require_type (dtp, BT_REAL, type, f))
2009 return;
2010 write_d (dtp, f, p, kind);
2011 break;
2013 case FMT_DT:
2014 if (n == 0)
2015 goto need_data;
2016 int unit = dtp->u.p.current_unit->unit_number;
2017 char dt[] = "DT";
2018 char tmp_iomsg[IOMSG_LEN] = "";
2019 char *child_iomsg;
2020 gfc_charlen_type child_iomsg_len;
2021 int noiostat;
2022 int *child_iostat = NULL;
2023 char *iotype;
2024 gfc_charlen_type iotype_len = f->u.udf.string_len;
2026 /* Build the iotype string. */
2027 if (iotype_len == 0)
2029 iotype_len = 2;
2030 iotype = dt;
2032 else
2033 iotype = get_dt_format (f->u.udf.string, &iotype_len);
2035 /* Set iostat, intent(out). */
2036 noiostat = 0;
2037 child_iostat = (dtp->common.flags & IOPARM_HAS_IOSTAT) ?
2038 dtp->common.iostat : &noiostat;
2040 /* Set iomsg, intent(inout). */
2041 if (dtp->common.flags & IOPARM_HAS_IOMSG)
2043 child_iomsg = dtp->common.iomsg;
2044 child_iomsg_len = dtp->common.iomsg_len;
2046 else
2048 child_iomsg = tmp_iomsg;
2049 child_iomsg_len = IOMSG_LEN;
2052 if (check_dtio_proc (dtp, f))
2053 return;
2055 /* Call the user defined formatted WRITE procedure. */
2056 dtp->u.p.current_unit->child_dtio++;
2058 dtp->u.p.fdtio_ptr (p, &unit, iotype, f->u.udf.vlist,
2059 child_iostat, child_iomsg,
2060 iotype_len, child_iomsg_len);
2061 dtp->u.p.current_unit->child_dtio--;
2063 if (f->u.udf.string_len != 0)
2064 free (iotype);
2065 /* Note: vlist is freed in free_format_data. */
2066 break;
2068 case FMT_E:
2069 if (n == 0)
2070 goto need_data;
2071 if (require_type (dtp, BT_REAL, type, f))
2072 return;
2073 write_e (dtp, f, p, kind);
2074 break;
2076 case FMT_EN:
2077 if (n == 0)
2078 goto need_data;
2079 if (require_type (dtp, BT_REAL, type, f))
2080 return;
2081 write_en (dtp, f, p, kind);
2082 break;
2084 case FMT_ES:
2085 if (n == 0)
2086 goto need_data;
2087 if (require_type (dtp, BT_REAL, type, f))
2088 return;
2089 write_es (dtp, f, p, kind);
2090 break;
2092 case FMT_F:
2093 if (n == 0)
2094 goto need_data;
2095 if (require_type (dtp, BT_REAL, type, f))
2096 return;
2097 write_f (dtp, f, p, kind);
2098 break;
2100 case FMT_G:
2101 if (n == 0)
2102 goto need_data;
2103 switch (type)
2105 case BT_INTEGER:
2106 write_i (dtp, f, p, kind);
2107 break;
2108 case BT_LOGICAL:
2109 write_l (dtp, f, p, kind);
2110 break;
2111 case BT_CHARACTER:
2112 if (kind == 4)
2113 write_a_char4 (dtp, f, p, size);
2114 else
2115 write_a (dtp, f, p, size);
2116 break;
2117 case BT_REAL:
2118 if (f->u.real.w == 0)
2119 write_real_g0 (dtp, p, kind, f->u.real.d);
2120 else
2121 write_d (dtp, f, p, kind);
2122 break;
2123 default:
2124 internal_error (&dtp->common,
2125 "formatted_transfer (): Bad type");
2127 break;
2129 case FMT_STRING:
2130 consume_data_flag = 0;
2131 write_constant_string (dtp, f);
2132 break;
2134 /* Format codes that don't transfer data. */
2135 case FMT_X:
2136 case FMT_TR:
2137 consume_data_flag = 0;
2139 dtp->u.p.skips += f->u.n;
2140 pos = bytes_used + dtp->u.p.skips - 1;
2141 dtp->u.p.pending_spaces = pos - dtp->u.p.max_pos + 1;
2142 /* Writes occur just before the switch on f->format, above, so
2143 that trailing blanks are suppressed, unless we are doing a
2144 non-advancing write in which case we want to output the blanks
2145 now. */
2146 if (dtp->u.p.advance_status == ADVANCE_NO)
2148 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
2149 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
2151 break;
2153 case FMT_TL:
2154 case FMT_T:
2155 consume_data_flag = 0;
2157 if (f->format == FMT_TL)
2160 /* Handle the special case when no bytes have been used yet.
2161 Cannot go below zero. */
2162 if (bytes_used == 0)
2164 dtp->u.p.pending_spaces -= f->u.n;
2165 dtp->u.p.skips -= f->u.n;
2166 dtp->u.p.skips = dtp->u.p.skips < 0 ? 0 : dtp->u.p.skips;
2169 pos = bytes_used - f->u.n;
2171 else /* FMT_T */
2172 pos = f->u.n - dtp->u.p.pending_spaces - 1;
2174 /* Standard 10.6.1.1: excessive left tabbing is reset to the
2175 left tab limit. We do not check if the position has gone
2176 beyond the end of record because a subsequent tab could
2177 bring us back again. */
2178 pos = pos < 0 ? 0 : pos;
2180 dtp->u.p.skips = dtp->u.p.skips + pos - bytes_used;
2181 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces
2182 + pos - dtp->u.p.max_pos;
2183 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces < 0
2184 ? 0 : dtp->u.p.pending_spaces;
2185 break;
2187 case FMT_S:
2188 consume_data_flag = 0;
2189 dtp->u.p.sign_status = SIGN_S;
2190 break;
2192 case FMT_SS:
2193 consume_data_flag = 0;
2194 dtp->u.p.sign_status = SIGN_SS;
2195 break;
2197 case FMT_SP:
2198 consume_data_flag = 0;
2199 dtp->u.p.sign_status = SIGN_SP;
2200 break;
2202 case FMT_BN:
2203 consume_data_flag = 0 ;
2204 dtp->u.p.blank_status = BLANK_NULL;
2205 break;
2207 case FMT_BZ:
2208 consume_data_flag = 0;
2209 dtp->u.p.blank_status = BLANK_ZERO;
2210 break;
2212 case FMT_DC:
2213 consume_data_flag = 0;
2214 dtp->u.p.current_unit->decimal_status = DECIMAL_COMMA;
2215 break;
2217 case FMT_DP:
2218 consume_data_flag = 0;
2219 dtp->u.p.current_unit->decimal_status = DECIMAL_POINT;
2220 break;
2222 case FMT_RC:
2223 consume_data_flag = 0;
2224 dtp->u.p.current_unit->round_status = ROUND_COMPATIBLE;
2225 break;
2227 case FMT_RD:
2228 consume_data_flag = 0;
2229 dtp->u.p.current_unit->round_status = ROUND_DOWN;
2230 break;
2232 case FMT_RN:
2233 consume_data_flag = 0;
2234 dtp->u.p.current_unit->round_status = ROUND_NEAREST;
2235 break;
2237 case FMT_RP:
2238 consume_data_flag = 0;
2239 dtp->u.p.current_unit->round_status = ROUND_PROCDEFINED;
2240 break;
2242 case FMT_RU:
2243 consume_data_flag = 0;
2244 dtp->u.p.current_unit->round_status = ROUND_UP;
2245 break;
2247 case FMT_RZ:
2248 consume_data_flag = 0;
2249 dtp->u.p.current_unit->round_status = ROUND_ZERO;
2250 break;
2252 case FMT_P:
2253 consume_data_flag = 0;
2254 dtp->u.p.scale_factor = f->u.k;
2255 break;
2257 case FMT_DOLLAR:
2258 consume_data_flag = 0;
2259 dtp->u.p.seen_dollar = 1;
2260 break;
2262 case FMT_SLASH:
2263 consume_data_flag = 0;
2264 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
2265 next_record (dtp, 0);
2266 break;
2268 case FMT_COLON:
2269 /* A colon descriptor causes us to exit this loop (in
2270 particular preventing another / descriptor from being
2271 processed) unless there is another data item to be
2272 transferred. */
2273 consume_data_flag = 0;
2274 if (n == 0)
2275 return;
2276 break;
2278 default:
2279 internal_error (&dtp->common, "Bad format node");
2282 /* Adjust the item count and data pointer. */
2284 if ((consume_data_flag > 0) && (n > 0))
2286 n--;
2287 p = ((char *) p) + size;
2290 pos = dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left;
2291 dtp->u.p.max_pos = (dtp->u.p.max_pos > pos) ? dtp->u.p.max_pos : pos;
2294 return;
2296 /* Come here when we need a data descriptor but don't have one. We
2297 push the current format node back onto the input, then return and
2298 let the user program call us back with the data. */
2299 need_data:
2300 unget_format (dtp, f);
2303 /* This function is first called from data_init_transfer to initiate the loop
2304 over each item in the format, transferring data as required. Subsequent
2305 calls to this function occur for each data item foound in the READ/WRITE
2306 statement. The item_count is incremented for each call. Since the first
2307 call is from data_transfer_init, the item_count is always one greater than
2308 the actual count number of the item being transferred. */
2310 static void
2311 formatted_transfer (st_parameter_dt *dtp, bt type, void *p, int kind,
2312 size_t size, size_t nelems)
2314 size_t elem;
2315 char *tmp;
2317 tmp = (char *) p;
2318 size_t stride = type == BT_CHARACTER ?
2319 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
2320 if (dtp->u.p.mode == READING)
2322 /* Big loop over all the elements. */
2323 for (elem = 0; elem < nelems; elem++)
2325 dtp->u.p.item_count++;
2326 formatted_transfer_scalar_read (dtp, type, tmp + stride*elem, kind, size);
2329 else
2331 /* Big loop over all the elements. */
2332 for (elem = 0; elem < nelems; elem++)
2334 dtp->u.p.item_count++;
2335 formatted_transfer_scalar_write (dtp, type, tmp + stride*elem, kind, size);
2340 /* Wrapper function for I/O of scalar types. If this should be an async I/O
2341 request, queue it. For a synchronous write on an async unit, perform the
2342 wait operation and return an error. For all synchronous writes, call the
2343 right transfer function. */
2345 static void
2346 wrap_scalar_transfer (st_parameter_dt *dtp, bt type, void *p, int kind,
2347 size_t size, size_t n_elem)
2349 if (dtp->u.p.current_unit && dtp->u.p.current_unit->au)
2351 if (dtp->u.p.async)
2353 transfer_args args;
2354 args.scalar.transfer = dtp->u.p.transfer;
2355 args.scalar.arg_bt = type;
2356 args.scalar.data = p;
2357 args.scalar.i = kind;
2358 args.scalar.s1 = size;
2359 args.scalar.s2 = n_elem;
2360 enqueue_transfer (dtp->u.p.current_unit->au, &args,
2361 AIO_TRANSFER_SCALAR);
2362 return;
2365 /* Come here if there was no asynchronous I/O to be scheduled. */
2366 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2367 return;
2369 dtp->u.p.transfer (dtp, type, p, kind, size, 1);
2373 /* Data transfer entry points. The type of the data entity is
2374 implicit in the subroutine call. This prevents us from having to
2375 share a common enum with the compiler. */
2377 void
2378 transfer_integer (st_parameter_dt *dtp, void *p, int kind)
2380 wrap_scalar_transfer (dtp, BT_INTEGER, p, kind, kind, 1);
2383 void
2384 transfer_integer_write (st_parameter_dt *dtp, void *p, int kind)
2386 transfer_integer (dtp, p, kind);
2389 void
2390 transfer_real (st_parameter_dt *dtp, void *p, int kind)
2392 size_t size;
2393 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2394 return;
2395 size = size_from_real_kind (kind);
2396 wrap_scalar_transfer (dtp, BT_REAL, p, kind, size, 1);
2399 void
2400 transfer_real_write (st_parameter_dt *dtp, void *p, int kind)
2402 transfer_real (dtp, p, kind);
2405 void
2406 transfer_logical (st_parameter_dt *dtp, void *p, int kind)
2408 wrap_scalar_transfer (dtp, BT_LOGICAL, p, kind, kind, 1);
2411 void
2412 transfer_logical_write (st_parameter_dt *dtp, void *p, int kind)
2414 transfer_logical (dtp, p, kind);
2417 void
2418 transfer_character (st_parameter_dt *dtp, void *p, gfc_charlen_type len)
2420 static char *empty_string[0];
2422 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2423 return;
2425 /* Strings of zero length can have p == NULL, which confuses the
2426 transfer routines into thinking we need more data elements. To avoid
2427 this, we give them a nice pointer. */
2428 if (len == 0 && p == NULL)
2429 p = empty_string;
2431 /* Set kind here to 1. */
2432 wrap_scalar_transfer (dtp, BT_CHARACTER, p, 1, len, 1);
2435 void
2436 transfer_character_write (st_parameter_dt *dtp, void *p, gfc_charlen_type len)
2438 transfer_character (dtp, p, len);
2441 void
2442 transfer_character_wide (st_parameter_dt *dtp, void *p, gfc_charlen_type len, int kind)
2444 static char *empty_string[0];
2446 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2447 return;
2449 /* Strings of zero length can have p == NULL, which confuses the
2450 transfer routines into thinking we need more data elements. To avoid
2451 this, we give them a nice pointer. */
2452 if (len == 0 && p == NULL)
2453 p = empty_string;
2455 /* Here we pass the actual kind value. */
2456 wrap_scalar_transfer (dtp, BT_CHARACTER, p, kind, len, 1);
2459 void
2460 transfer_character_wide_write (st_parameter_dt *dtp, void *p, gfc_charlen_type len, int kind)
2462 transfer_character_wide (dtp, p, len, kind);
2465 void
2466 transfer_complex (st_parameter_dt *dtp, void *p, int kind)
2468 size_t size;
2469 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2470 return;
2471 size = size_from_complex_kind (kind);
2472 wrap_scalar_transfer (dtp, BT_COMPLEX, p, kind, size, 1);
2475 void
2476 transfer_complex_write (st_parameter_dt *dtp, void *p, int kind)
2478 transfer_complex (dtp, p, kind);
2481 void
2482 transfer_array_inner (st_parameter_dt *dtp, gfc_array_char *desc, int kind,
2483 gfc_charlen_type charlen)
2485 index_type count[GFC_MAX_DIMENSIONS];
2486 index_type extent[GFC_MAX_DIMENSIONS];
2487 index_type stride[GFC_MAX_DIMENSIONS];
2488 index_type stride0, rank, size, n;
2489 size_t tsize;
2490 char *data;
2491 bt iotype;
2493 /* Adjust item_count before emitting error message. */
2495 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2496 return;
2498 iotype = (bt) GFC_DESCRIPTOR_TYPE (desc);
2499 size = iotype == BT_CHARACTER ? charlen : GFC_DESCRIPTOR_SIZE (desc);
2501 rank = GFC_DESCRIPTOR_RANK (desc);
2503 for (n = 0; n < rank; n++)
2505 count[n] = 0;
2506 stride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(desc,n);
2507 extent[n] = GFC_DESCRIPTOR_EXTENT(desc,n);
2509 /* If the extent of even one dimension is zero, then the entire
2510 array section contains zero elements, so we return after writing
2511 a zero array record. */
2512 if (extent[n] <= 0)
2514 data = NULL;
2515 tsize = 0;
2516 dtp->u.p.transfer (dtp, iotype, data, kind, size, tsize);
2517 return;
2521 stride0 = stride[0];
2523 /* If the innermost dimension has a stride of 1, we can do the transfer
2524 in contiguous chunks. */
2525 if (stride0 == size)
2526 tsize = extent[0];
2527 else
2528 tsize = 1;
2530 data = GFC_DESCRIPTOR_DATA (desc);
2532 while (data)
2534 dtp->u.p.transfer (dtp, iotype, data, kind, size, tsize);
2535 data += stride0 * tsize;
2536 count[0] += tsize;
2537 n = 0;
2538 while (count[n] == extent[n])
2540 count[n] = 0;
2541 data -= stride[n] * extent[n];
2542 n++;
2543 if (n == rank)
2545 data = NULL;
2546 break;
2548 else
2550 count[n]++;
2551 data += stride[n];
2557 void
2558 transfer_array (st_parameter_dt *dtp, gfc_array_char *desc, int kind,
2559 gfc_charlen_type charlen)
2561 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2562 return;
2564 if (dtp->u.p.current_unit && dtp->u.p.current_unit->au)
2566 if (dtp->u.p.async)
2568 transfer_args args;
2569 size_t sz = sizeof (gfc_array_char)
2570 + sizeof (descriptor_dimension)
2571 * GFC_DESCRIPTOR_RANK (desc);
2572 args.array.desc = xmalloc (sz);
2573 NOTE ("desc = %p", (void *) args.array.desc);
2574 memcpy (args.array.desc, desc, sz);
2575 args.array.kind = kind;
2576 args.array.charlen = charlen;
2577 enqueue_transfer (dtp->u.p.current_unit->au, &args,
2578 AIO_TRANSFER_ARRAY);
2579 return;
2582 /* Come here if there was no asynchronous I/O to be scheduled. */
2583 transfer_array_inner (dtp, desc, kind, charlen);
2587 void
2588 transfer_array_write (st_parameter_dt *dtp, gfc_array_char *desc, int kind,
2589 gfc_charlen_type charlen)
2591 transfer_array (dtp, desc, kind, charlen);
2595 /* User defined input/output iomsg. */
2597 #define IOMSG_LEN 256
2599 void
2600 transfer_derived (st_parameter_dt *parent, void *dtio_source, void *dtio_proc)
2602 if (parent->u.p.current_unit)
2604 if (parent->u.p.current_unit->flags.form == FORM_UNFORMATTED)
2605 parent->u.p.ufdtio_ptr = (unformatted_dtio) dtio_proc;
2606 else
2607 parent->u.p.fdtio_ptr = (formatted_dtio) dtio_proc;
2609 wrap_scalar_transfer (parent, BT_CLASS, dtio_source, 0, 0, 1);
2613 /* Preposition a sequential unformatted file while reading. */
2615 static void
2616 us_read (st_parameter_dt *dtp, int continued)
2618 ssize_t n, nr;
2619 GFC_INTEGER_4 i4;
2620 GFC_INTEGER_8 i8;
2621 gfc_offset i;
2623 if (compile_options.record_marker == 0)
2624 n = sizeof (GFC_INTEGER_4);
2625 else
2626 n = compile_options.record_marker;
2628 nr = sread (dtp->u.p.current_unit->s, &i, n);
2629 if (unlikely (nr < 0))
2631 generate_error (&dtp->common, LIBERROR_BAD_US, NULL);
2632 return;
2634 else if (nr == 0)
2636 hit_eof (dtp);
2637 return; /* end of file */
2639 else if (unlikely (n != nr))
2641 generate_error (&dtp->common, LIBERROR_BAD_US, NULL);
2642 return;
2645 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
2646 if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE))
2648 switch (nr)
2650 case sizeof(GFC_INTEGER_4):
2651 memcpy (&i4, &i, sizeof (i4));
2652 i = i4;
2653 break;
2655 case sizeof(GFC_INTEGER_8):
2656 memcpy (&i8, &i, sizeof (i8));
2657 i = i8;
2658 break;
2660 default:
2661 runtime_error ("Illegal value for record marker");
2662 break;
2665 else
2667 uint32_t u32;
2668 uint64_t u64;
2669 switch (nr)
2671 case sizeof(GFC_INTEGER_4):
2672 memcpy (&u32, &i, sizeof (u32));
2673 u32 = __builtin_bswap32 (u32);
2674 memcpy (&i4, &u32, sizeof (i4));
2675 i = i4;
2676 break;
2678 case sizeof(GFC_INTEGER_8):
2679 memcpy (&u64, &i, sizeof (u64));
2680 u64 = __builtin_bswap64 (u64);
2681 memcpy (&i8, &u64, sizeof (i8));
2682 i = i8;
2683 break;
2685 default:
2686 runtime_error ("Illegal value for record marker");
2687 break;
2691 if (i >= 0)
2693 dtp->u.p.current_unit->bytes_left_subrecord = i;
2694 dtp->u.p.current_unit->continued = 0;
2696 else
2698 dtp->u.p.current_unit->bytes_left_subrecord = -i;
2699 dtp->u.p.current_unit->continued = 1;
2702 if (! continued)
2703 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2707 /* Preposition a sequential unformatted file while writing. This
2708 amount to writing a bogus length that will be filled in later. */
2710 static void
2711 us_write (st_parameter_dt *dtp, int continued)
2713 ssize_t nbytes;
2714 gfc_offset dummy;
2716 dummy = 0;
2718 if (compile_options.record_marker == 0)
2719 nbytes = sizeof (GFC_INTEGER_4);
2720 else
2721 nbytes = compile_options.record_marker ;
2723 if (swrite (dtp->u.p.current_unit->s, &dummy, nbytes) != nbytes)
2724 generate_error (&dtp->common, LIBERROR_OS, NULL);
2726 /* For sequential unformatted, if RECL= was not specified in the OPEN
2727 we write until we have more bytes than can fit in the subrecord
2728 markers, then we write a new subrecord. */
2730 dtp->u.p.current_unit->bytes_left_subrecord =
2731 dtp->u.p.current_unit->recl_subrecord;
2732 dtp->u.p.current_unit->continued = continued;
2736 /* Position to the next record prior to transfer. We are assumed to
2737 be before the next record. We also calculate the bytes in the next
2738 record. */
2740 static void
2741 pre_position (st_parameter_dt *dtp)
2743 if (dtp->u.p.current_unit->current_record)
2744 return; /* Already positioned. */
2746 switch (current_mode (dtp))
2748 case FORMATTED_STREAM:
2749 case UNFORMATTED_STREAM:
2750 /* There are no records with stream I/O. If the position was specified
2751 data_transfer_init has already positioned the file. If no position
2752 was specified, we continue from where we last left off. I.e.
2753 there is nothing to do here. */
2754 break;
2756 case UNFORMATTED_SEQUENTIAL:
2757 if (dtp->u.p.mode == READING)
2758 us_read (dtp, 0);
2759 else
2760 us_write (dtp, 0);
2762 break;
2764 case FORMATTED_SEQUENTIAL:
2765 case FORMATTED_DIRECT:
2766 case UNFORMATTED_DIRECT:
2767 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2768 break;
2771 dtp->u.p.current_unit->current_record = 1;
2775 /* Initialize things for a data transfer. This code is common for
2776 both reading and writing. */
2778 static void
2779 data_transfer_init (st_parameter_dt *dtp, int read_flag)
2781 unit_flags u_flags; /* Used for creating a unit if needed. */
2782 GFC_INTEGER_4 cf = dtp->common.flags;
2783 namelist_info *ionml;
2784 async_unit *au;
2786 NOTE ("data_transfer_init");
2788 ionml = ((cf & IOPARM_DT_IONML_SET) != 0) ? dtp->u.p.ionml : NULL;
2790 memset (&dtp->u.p, 0, sizeof (dtp->u.p));
2792 dtp->u.p.ionml = ionml;
2793 dtp->u.p.mode = read_flag ? READING : WRITING;
2794 dtp->u.p.namelist_mode = 0;
2795 dtp->u.p.cc.len = 0;
2797 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2798 return;
2800 dtp->u.p.current_unit = get_unit (dtp, 1);
2802 if (dtp->u.p.current_unit == NULL)
2804 /* This means we tried to access an external unit < 0 without
2805 having opened it first with NEWUNIT=. */
2806 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2807 "Unit number is negative and unit was not already "
2808 "opened with OPEN(NEWUNIT=...)");
2809 return;
2811 else if (dtp->u.p.current_unit->s == NULL)
2812 { /* Open the unit with some default flags. */
2813 st_parameter_open opp;
2814 unit_convert conv;
2815 NOTE ("Open the unit with some default flags.");
2816 memset (&u_flags, '\0', sizeof (u_flags));
2817 u_flags.access = ACCESS_SEQUENTIAL;
2818 u_flags.action = ACTION_READWRITE;
2820 /* Is it unformatted? */
2821 if (!(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT
2822 | IOPARM_DT_IONML_SET)))
2823 u_flags.form = FORM_UNFORMATTED;
2824 else
2825 u_flags.form = FORM_UNSPECIFIED;
2827 u_flags.delim = DELIM_UNSPECIFIED;
2828 u_flags.blank = BLANK_UNSPECIFIED;
2829 u_flags.pad = PAD_UNSPECIFIED;
2830 u_flags.decimal = DECIMAL_UNSPECIFIED;
2831 u_flags.encoding = ENCODING_UNSPECIFIED;
2832 u_flags.async = ASYNC_UNSPECIFIED;
2833 u_flags.round = ROUND_UNSPECIFIED;
2834 u_flags.sign = SIGN_UNSPECIFIED;
2835 u_flags.share = SHARE_UNSPECIFIED;
2836 u_flags.cc = CC_UNSPECIFIED;
2837 u_flags.readonly = 0;
2839 u_flags.status = STATUS_UNKNOWN;
2841 conv = get_unformatted_convert (dtp->common.unit);
2843 if (conv == GFC_CONVERT_NONE)
2844 conv = compile_options.convert;
2846 switch (conv)
2848 case GFC_CONVERT_NATIVE:
2849 case GFC_CONVERT_SWAP:
2850 break;
2852 case GFC_CONVERT_BIG:
2853 conv = __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ ? GFC_CONVERT_NATIVE : GFC_CONVERT_SWAP;
2854 break;
2856 case GFC_CONVERT_LITTLE:
2857 conv = __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ ? GFC_CONVERT_SWAP : GFC_CONVERT_NATIVE;
2858 break;
2860 default:
2861 internal_error (&opp.common, "Illegal value for CONVERT");
2862 break;
2865 u_flags.convert = conv;
2867 opp.common = dtp->common;
2868 opp.common.flags &= IOPARM_COMMON_MASK;
2869 dtp->u.p.current_unit = new_unit (&opp, dtp->u.p.current_unit, &u_flags);
2870 dtp->common.flags &= ~IOPARM_COMMON_MASK;
2871 dtp->common.flags |= (opp.common.flags & IOPARM_COMMON_MASK);
2872 if (dtp->u.p.current_unit == NULL)
2873 return;
2876 if (dtp->u.p.current_unit->child_dtio == 0)
2878 if ((cf & IOPARM_DT_HAS_SIZE) != 0)
2880 dtp->u.p.current_unit->has_size = true;
2881 /* Initialize the count. */
2882 dtp->u.p.current_unit->size_used = 0;
2884 else
2885 dtp->u.p.current_unit->has_size = false;
2887 else if (dtp->u.p.current_unit->internal_unit_kind > 0)
2888 dtp->u.p.unit_is_internal = 1;
2890 if ((cf & IOPARM_DT_HAS_ASYNCHRONOUS) != 0)
2892 int f;
2893 f = find_option (&dtp->common, dtp->asynchronous, dtp->asynchronous_len,
2894 async_opt, "Bad ASYNCHRONOUS in data transfer "
2895 "statement");
2896 if (f == ASYNC_YES && dtp->u.p.current_unit->flags.async != ASYNC_YES)
2898 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2899 "ASYNCHRONOUS transfer without "
2900 "ASYHCRONOUS='YES' in OPEN");
2901 return;
2903 dtp->u.p.async = f == ASYNC_YES;
2906 au = dtp->u.p.current_unit->au;
2907 if (au)
2909 if (dtp->u.p.async)
2911 /* If this is an asynchronous I/O statement, collect errors and
2912 return if there are any. */
2913 if (collect_async_errors (&dtp->common, au))
2914 return;
2916 else
2918 /* Synchronous statement: Perform a wait operation for any pending
2919 asynchronous I/O. This needs to be done before all other error
2920 checks. See F2008, 9.6.4.1. */
2921 if (async_wait (&(dtp->common), au))
2922 return;
2926 /* Check the action. */
2928 if (read_flag && dtp->u.p.current_unit->flags.action == ACTION_WRITE)
2930 generate_error (&dtp->common, LIBERROR_BAD_ACTION,
2931 "Cannot read from file opened for WRITE");
2932 return;
2935 if (!read_flag && dtp->u.p.current_unit->flags.action == ACTION_READ)
2937 generate_error (&dtp->common, LIBERROR_BAD_ACTION,
2938 "Cannot write to file opened for READ");
2939 return;
2942 dtp->u.p.first_item = 1;
2944 /* Check the format. */
2946 if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
2947 parse_format (dtp);
2949 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED
2950 && (cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
2951 != 0)
2953 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2954 "Format present for UNFORMATTED data transfer");
2955 return;
2958 if ((cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0 && dtp->u.p.ionml != NULL)
2960 if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
2962 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2963 "A format cannot be specified with a namelist");
2964 return;
2967 else if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
2968 !(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT)))
2970 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2971 "Missing format for FORMATTED data transfer");
2972 return;
2975 if (is_internal_unit (dtp)
2976 && dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
2978 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2979 "Internal file cannot be accessed by UNFORMATTED "
2980 "data transfer");
2981 return;
2984 /* Check the record or position number. */
2986 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT
2987 && (cf & IOPARM_DT_HAS_REC) == 0)
2989 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
2990 "Direct access data transfer requires record number");
2991 return;
2994 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
2996 if ((cf & IOPARM_DT_HAS_REC) != 0)
2998 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2999 "Record number not allowed for sequential access "
3000 "data transfer");
3001 return;
3004 if (compile_options.warn_std &&
3005 dtp->u.p.current_unit->endfile == AFTER_ENDFILE)
3007 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
3008 "Sequential READ or WRITE not allowed after "
3009 "EOF marker, possibly use REWIND or BACKSPACE");
3010 return;
3014 /* Process the ADVANCE option. */
3016 dtp->u.p.advance_status
3017 = !(cf & IOPARM_DT_HAS_ADVANCE) ? ADVANCE_UNSPECIFIED :
3018 find_option (&dtp->common, dtp->advance, dtp->advance_len, advance_opt,
3019 "Bad ADVANCE parameter in data transfer statement");
3021 if (dtp->u.p.advance_status != ADVANCE_UNSPECIFIED)
3023 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
3025 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
3026 "ADVANCE specification conflicts with sequential "
3027 "access");
3028 return;
3031 if (is_internal_unit (dtp))
3033 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
3034 "ADVANCE specification conflicts with internal file");
3035 return;
3038 if ((cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
3039 != IOPARM_DT_HAS_FORMAT)
3041 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
3042 "ADVANCE specification requires an explicit format");
3043 return;
3047 /* Child IO is non-advancing and any ADVANCE= specifier is ignored.
3048 F2008 9.6.2.4 */
3049 if (dtp->u.p.current_unit->child_dtio > 0)
3050 dtp->u.p.advance_status = ADVANCE_NO;
3052 if (read_flag)
3054 dtp->u.p.current_unit->previous_nonadvancing_write = 0;
3056 if ((cf & IOPARM_EOR) != 0 && dtp->u.p.advance_status != ADVANCE_NO)
3058 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
3059 "EOR specification requires an ADVANCE specification "
3060 "of NO");
3061 return;
3064 if ((cf & IOPARM_DT_HAS_SIZE) != 0
3065 && dtp->u.p.advance_status != ADVANCE_NO)
3067 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
3068 "SIZE specification requires an ADVANCE "
3069 "specification of NO");
3070 return;
3073 else
3074 { /* Write constraints. */
3075 if ((cf & IOPARM_END) != 0)
3077 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
3078 "END specification cannot appear in a write "
3079 "statement");
3080 return;
3083 if ((cf & IOPARM_EOR) != 0)
3085 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
3086 "EOR specification cannot appear in a write "
3087 "statement");
3088 return;
3091 if ((cf & IOPARM_DT_HAS_SIZE) != 0)
3093 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
3094 "SIZE specification cannot appear in a write "
3095 "statement");
3096 return;
3100 if (dtp->u.p.advance_status == ADVANCE_UNSPECIFIED)
3101 dtp->u.p.advance_status = ADVANCE_YES;
3103 /* Check the decimal mode. */
3104 dtp->u.p.current_unit->decimal_status
3105 = !(cf & IOPARM_DT_HAS_DECIMAL) ? DECIMAL_UNSPECIFIED :
3106 find_option (&dtp->common, dtp->decimal, dtp->decimal_len,
3107 decimal_opt, "Bad DECIMAL parameter in data transfer "
3108 "statement");
3110 if (dtp->u.p.current_unit->decimal_status == DECIMAL_UNSPECIFIED)
3111 dtp->u.p.current_unit->decimal_status = dtp->u.p.current_unit->flags.decimal;
3113 /* Check the round mode. */
3114 dtp->u.p.current_unit->round_status
3115 = !(cf & IOPARM_DT_HAS_ROUND) ? ROUND_UNSPECIFIED :
3116 find_option (&dtp->common, dtp->round, dtp->round_len,
3117 round_opt, "Bad ROUND parameter in data transfer "
3118 "statement");
3120 if (dtp->u.p.current_unit->round_status == ROUND_UNSPECIFIED)
3121 dtp->u.p.current_unit->round_status = dtp->u.p.current_unit->flags.round;
3123 /* Check the sign mode. */
3124 dtp->u.p.sign_status
3125 = !(cf & IOPARM_DT_HAS_SIGN) ? SIGN_UNSPECIFIED :
3126 find_option (&dtp->common, dtp->sign, dtp->sign_len, sign_opt,
3127 "Bad SIGN parameter in data transfer statement");
3129 if (dtp->u.p.sign_status == SIGN_UNSPECIFIED)
3130 dtp->u.p.sign_status = dtp->u.p.current_unit->flags.sign;
3132 /* Check the blank mode. */
3133 dtp->u.p.blank_status
3134 = !(cf & IOPARM_DT_HAS_BLANK) ? BLANK_UNSPECIFIED :
3135 find_option (&dtp->common, dtp->blank, dtp->blank_len,
3136 blank_opt,
3137 "Bad BLANK parameter in data transfer statement");
3139 if (dtp->u.p.blank_status == BLANK_UNSPECIFIED)
3140 dtp->u.p.blank_status = dtp->u.p.current_unit->flags.blank;
3142 /* Check the delim mode. */
3143 dtp->u.p.current_unit->delim_status
3144 = !(cf & IOPARM_DT_HAS_DELIM) ? DELIM_UNSPECIFIED :
3145 find_option (&dtp->common, dtp->delim, dtp->delim_len,
3146 delim_opt, "Bad DELIM parameter in data transfer statement");
3148 if (dtp->u.p.current_unit->delim_status == DELIM_UNSPECIFIED)
3150 if (ionml && dtp->u.p.current_unit->flags.delim == DELIM_UNSPECIFIED)
3151 dtp->u.p.current_unit->delim_status = DELIM_QUOTE;
3152 else
3153 dtp->u.p.current_unit->delim_status = dtp->u.p.current_unit->flags.delim;
3156 /* Check the pad mode. */
3157 dtp->u.p.current_unit->pad_status
3158 = !(cf & IOPARM_DT_HAS_PAD) ? PAD_UNSPECIFIED :
3159 find_option (&dtp->common, dtp->pad, dtp->pad_len, pad_opt,
3160 "Bad PAD parameter in data transfer statement");
3162 if (dtp->u.p.current_unit->pad_status == PAD_UNSPECIFIED)
3163 dtp->u.p.current_unit->pad_status = dtp->u.p.current_unit->flags.pad;
3165 /* Set up the subroutine that will handle the transfers. */
3167 if (read_flag)
3169 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
3170 dtp->u.p.transfer = unformatted_read;
3171 else
3173 if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
3174 dtp->u.p.transfer = list_formatted_read;
3175 else
3176 dtp->u.p.transfer = formatted_transfer;
3179 else
3181 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
3182 dtp->u.p.transfer = unformatted_write;
3183 else
3185 if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
3186 dtp->u.p.transfer = list_formatted_write;
3187 else
3188 dtp->u.p.transfer = formatted_transfer;
3192 if (au && dtp->u.p.async)
3194 NOTE ("enqueue_data_transfer");
3195 enqueue_data_transfer_init (au, dtp, read_flag);
3197 else
3199 NOTE ("invoking data_transfer_init_worker");
3200 data_transfer_init_worker (dtp, read_flag);
3204 void
3205 data_transfer_init_worker (st_parameter_dt *dtp, int read_flag)
3207 GFC_INTEGER_4 cf = dtp->common.flags;
3209 NOTE ("starting worker...");
3211 if (read_flag && dtp->u.p.current_unit->flags.form != FORM_UNFORMATTED
3212 && ((cf & IOPARM_DT_LIST_FORMAT) != 0)
3213 && dtp->u.p.current_unit->child_dtio == 0)
3214 dtp->u.p.current_unit->last_char = EOF - 1;
3216 /* Check to see if we might be reading what we wrote before */
3218 if (dtp->u.p.mode != dtp->u.p.current_unit->mode
3219 && !is_internal_unit (dtp))
3221 int pos = fbuf_reset (dtp->u.p.current_unit);
3222 if (pos != 0)
3223 sseek (dtp->u.p.current_unit->s, pos, SEEK_CUR);
3224 sflush(dtp->u.p.current_unit->s);
3227 /* Check the POS= specifier: that it is in range and that it is used with a
3228 unit that has been connected for STREAM access. F2003 9.5.1.10. */
3230 if (((cf & IOPARM_DT_HAS_POS) != 0))
3232 if (is_stream_io (dtp))
3235 if (dtp->pos <= 0)
3237 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
3238 "POS=specifier must be positive");
3239 return;
3242 if (dtp->pos >= dtp->u.p.current_unit->maxrec)
3244 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
3245 "POS=specifier too large");
3246 return;
3249 dtp->rec = dtp->pos;
3251 if (dtp->u.p.mode == READING)
3253 /* Reset the endfile flag; if we hit EOF during reading
3254 we'll set the flag and generate an error at that point
3255 rather than worrying about it here. */
3256 dtp->u.p.current_unit->endfile = NO_ENDFILE;
3259 if (dtp->pos != dtp->u.p.current_unit->strm_pos)
3261 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
3262 if (sseek (dtp->u.p.current_unit->s, dtp->pos - 1, SEEK_SET) < 0)
3264 generate_error (&dtp->common, LIBERROR_OS, NULL);
3265 return;
3267 dtp->u.p.current_unit->strm_pos = dtp->pos;
3270 else
3272 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
3273 "POS=specifier not allowed, "
3274 "Try OPEN with ACCESS='stream'");
3275 return;
3280 /* Sanity checks on the record number. */
3281 if ((cf & IOPARM_DT_HAS_REC) != 0)
3283 if (dtp->rec <= 0)
3285 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
3286 "Record number must be positive");
3287 return;
3290 if (dtp->rec >= dtp->u.p.current_unit->maxrec)
3292 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
3293 "Record number too large");
3294 return;
3297 /* Make sure format buffer is reset. */
3298 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED)
3299 fbuf_reset (dtp->u.p.current_unit);
3302 /* Check whether the record exists to be read. Only
3303 a partial record needs to exist. */
3305 if (dtp->u.p.mode == READING && (dtp->rec - 1)
3306 * dtp->u.p.current_unit->recl >= ssize (dtp->u.p.current_unit->s))
3308 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
3309 "Non-existing record number");
3310 return;
3313 /* Position the file. */
3314 if (sseek (dtp->u.p.current_unit->s, (gfc_offset) (dtp->rec - 1)
3315 * dtp->u.p.current_unit->recl, SEEK_SET) < 0)
3317 generate_error (&dtp->common, LIBERROR_OS, NULL);
3318 return;
3321 if (dtp->u.p.current_unit->flags.access == ACCESS_STREAM)
3323 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
3324 "Record number not allowed for stream access "
3325 "data transfer");
3326 return;
3330 /* Bugware for badly written mixed C-Fortran I/O. */
3331 if (!is_internal_unit (dtp))
3332 flush_if_preconnected(dtp->u.p.current_unit->s);
3334 dtp->u.p.current_unit->mode = dtp->u.p.mode;
3336 /* Set the maximum position reached from the previous I/O operation. This
3337 could be greater than zero from a previous non-advancing write. */
3338 dtp->u.p.max_pos = dtp->u.p.current_unit->saved_pos;
3340 pre_position (dtp);
3342 /* Make sure that we don't do a read after a nonadvancing write. */
3344 if (read_flag)
3346 if (dtp->u.p.current_unit->read_bad && !is_stream_io (dtp))
3348 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
3349 "Cannot READ after a nonadvancing WRITE");
3350 return;
3353 else
3355 if (dtp->u.p.advance_status == ADVANCE_YES && !dtp->u.p.seen_dollar)
3356 dtp->u.p.current_unit->read_bad = 1;
3359 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED)
3361 #ifdef HAVE_USELOCALE
3362 dtp->u.p.old_locale = uselocale (c_locale);
3363 #else
3364 __gthread_mutex_lock (&old_locale_lock);
3365 if (!old_locale_ctr++)
3367 old_locale = setlocale (LC_NUMERIC, NULL);
3368 setlocale (LC_NUMERIC, "C");
3370 __gthread_mutex_unlock (&old_locale_lock);
3371 #endif
3372 /* Start the data transfer if we are doing a formatted transfer. */
3373 if ((cf & (IOPARM_DT_LIST_FORMAT | IOPARM_DT_HAS_NAMELIST_NAME)) == 0
3374 && dtp->u.p.ionml == NULL)
3375 formatted_transfer (dtp, 0, NULL, 0, 0, 1);
3380 /* Initialize an array_loop_spec given the array descriptor. The function
3381 returns the index of the last element of the array, and also returns
3382 starting record, where the first I/O goes to (necessary in case of
3383 negative strides). */
3385 gfc_offset
3386 init_loop_spec (gfc_array_char *desc, array_loop_spec *ls,
3387 gfc_offset *start_record)
3389 int rank = GFC_DESCRIPTOR_RANK(desc);
3390 int i;
3391 gfc_offset index;
3392 int empty;
3394 empty = 0;
3395 index = 1;
3396 *start_record = 0;
3398 for (i=0; i<rank; i++)
3400 ls[i].idx = GFC_DESCRIPTOR_LBOUND(desc,i);
3401 ls[i].start = GFC_DESCRIPTOR_LBOUND(desc,i);
3402 ls[i].end = GFC_DESCRIPTOR_UBOUND(desc,i);
3403 ls[i].step = GFC_DESCRIPTOR_STRIDE(desc,i);
3404 empty = empty || (GFC_DESCRIPTOR_UBOUND(desc,i)
3405 < GFC_DESCRIPTOR_LBOUND(desc,i));
3407 if (GFC_DESCRIPTOR_STRIDE(desc,i) > 0)
3409 index += (GFC_DESCRIPTOR_EXTENT(desc,i) - 1)
3410 * GFC_DESCRIPTOR_STRIDE(desc,i);
3412 else
3414 index -= (GFC_DESCRIPTOR_EXTENT(desc,i) - 1)
3415 * GFC_DESCRIPTOR_STRIDE(desc,i);
3416 *start_record -= (GFC_DESCRIPTOR_EXTENT(desc,i) - 1)
3417 * GFC_DESCRIPTOR_STRIDE(desc,i);
3421 if (empty)
3422 return 0;
3423 else
3424 return index;
3427 /* Determine the index to the next record in an internal unit array by
3428 by incrementing through the array_loop_spec. */
3430 gfc_offset
3431 next_array_record (st_parameter_dt *dtp, array_loop_spec *ls, int *finished)
3433 int i, carry;
3434 gfc_offset index;
3436 carry = 1;
3437 index = 0;
3439 for (i = 0; i < dtp->u.p.current_unit->rank; i++)
3441 if (carry)
3443 ls[i].idx++;
3444 if (ls[i].idx > ls[i].end)
3446 ls[i].idx = ls[i].start;
3447 carry = 1;
3449 else
3450 carry = 0;
3452 index = index + (ls[i].idx - ls[i].start) * ls[i].step;
3455 *finished = carry;
3457 return index;
3462 /* Skip to the end of the current record, taking care of an optional
3463 record marker of size bytes. If the file is not seekable, we
3464 read chunks of size MAX_READ until we get to the right
3465 position. */
3467 static void
3468 skip_record (st_parameter_dt *dtp, gfc_offset bytes)
3470 ssize_t rlength, readb;
3471 #define MAX_READ 4096
3472 char p[MAX_READ];
3474 dtp->u.p.current_unit->bytes_left_subrecord += bytes;
3475 if (dtp->u.p.current_unit->bytes_left_subrecord == 0)
3476 return;
3478 /* Direct access files do not generate END conditions,
3479 only I/O errors. */
3480 if (sseek (dtp->u.p.current_unit->s,
3481 dtp->u.p.current_unit->bytes_left_subrecord, SEEK_CUR) < 0)
3483 /* Seeking failed, fall back to seeking by reading data. */
3484 while (dtp->u.p.current_unit->bytes_left_subrecord > 0)
3486 rlength =
3487 (MAX_READ < dtp->u.p.current_unit->bytes_left_subrecord) ?
3488 MAX_READ : dtp->u.p.current_unit->bytes_left_subrecord;
3490 readb = sread (dtp->u.p.current_unit->s, p, rlength);
3491 if (readb < 0)
3493 generate_error (&dtp->common, LIBERROR_OS, NULL);
3494 return;
3497 dtp->u.p.current_unit->bytes_left_subrecord -= readb;
3499 return;
3501 dtp->u.p.current_unit->bytes_left_subrecord = 0;
3505 /* Advance to the next record reading unformatted files, taking
3506 care of subrecords. If complete_record is nonzero, we loop
3507 until all subrecords are cleared. */
3509 static void
3510 next_record_r_unf (st_parameter_dt *dtp, int complete_record)
3512 size_t bytes;
3514 bytes = compile_options.record_marker == 0 ?
3515 sizeof (GFC_INTEGER_4) : compile_options.record_marker;
3517 while(1)
3520 /* Skip over tail */
3522 skip_record (dtp, bytes);
3524 if ( ! (complete_record && dtp->u.p.current_unit->continued))
3525 return;
3527 us_read (dtp, 1);
3532 static gfc_offset
3533 min_off (gfc_offset a, gfc_offset b)
3535 return (a < b ? a : b);
3539 /* Space to the next record for read mode. */
3541 static void
3542 next_record_r (st_parameter_dt *dtp, int done)
3544 gfc_offset record;
3545 char p;
3546 int cc;
3548 switch (current_mode (dtp))
3550 /* No records in unformatted STREAM I/O. */
3551 case UNFORMATTED_STREAM:
3552 return;
3554 case UNFORMATTED_SEQUENTIAL:
3555 next_record_r_unf (dtp, 1);
3556 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
3557 break;
3559 case FORMATTED_DIRECT:
3560 case UNFORMATTED_DIRECT:
3561 skip_record (dtp, dtp->u.p.current_unit->bytes_left);
3562 break;
3564 case FORMATTED_STREAM:
3565 case FORMATTED_SEQUENTIAL:
3566 /* read_sf has already terminated input because of an '\n', or
3567 we have hit EOF. */
3568 if (dtp->u.p.sf_seen_eor)
3570 dtp->u.p.sf_seen_eor = 0;
3571 break;
3574 if (is_internal_unit (dtp))
3576 if (is_array_io (dtp))
3578 int finished;
3580 record = next_array_record (dtp, dtp->u.p.current_unit->ls,
3581 &finished);
3582 if (!done && finished)
3583 hit_eof (dtp);
3585 /* Now seek to this record. */
3586 record = record * dtp->u.p.current_unit->recl;
3587 if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
3589 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3590 break;
3592 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
3594 else
3596 gfc_offset bytes_left = dtp->u.p.current_unit->bytes_left;
3597 bytes_left = min_off (bytes_left,
3598 ssize (dtp->u.p.current_unit->s)
3599 - stell (dtp->u.p.current_unit->s));
3600 if (sseek (dtp->u.p.current_unit->s,
3601 bytes_left, SEEK_CUR) < 0)
3603 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3604 break;
3606 dtp->u.p.current_unit->bytes_left
3607 = dtp->u.p.current_unit->recl;
3609 break;
3611 else if (dtp->u.p.current_unit->flags.cc != CC_NONE)
3615 errno = 0;
3616 cc = fbuf_getc (dtp->u.p.current_unit);
3617 if (cc == EOF)
3619 if (errno != 0)
3620 generate_error (&dtp->common, LIBERROR_OS, NULL);
3621 else
3623 if (is_stream_io (dtp)
3624 || dtp->u.p.current_unit->pad_status == PAD_NO
3625 || dtp->u.p.current_unit->bytes_left
3626 == dtp->u.p.current_unit->recl)
3627 hit_eof (dtp);
3629 break;
3632 if (is_stream_io (dtp))
3633 dtp->u.p.current_unit->strm_pos++;
3635 p = (char) cc;
3637 while (p != '\n');
3639 break;
3644 /* Small utility function to write a record marker, taking care of
3645 byte swapping and of choosing the correct size. */
3647 static int
3648 write_us_marker (st_parameter_dt *dtp, const gfc_offset buf)
3650 size_t len;
3651 GFC_INTEGER_4 buf4;
3652 GFC_INTEGER_8 buf8;
3654 if (compile_options.record_marker == 0)
3655 len = sizeof (GFC_INTEGER_4);
3656 else
3657 len = compile_options.record_marker;
3659 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
3660 if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE))
3662 switch (len)
3664 case sizeof (GFC_INTEGER_4):
3665 buf4 = buf;
3666 return swrite (dtp->u.p.current_unit->s, &buf4, len);
3667 break;
3669 case sizeof (GFC_INTEGER_8):
3670 buf8 = buf;
3671 return swrite (dtp->u.p.current_unit->s, &buf8, len);
3672 break;
3674 default:
3675 runtime_error ("Illegal value for record marker");
3676 break;
3679 else
3681 uint32_t u32;
3682 uint64_t u64;
3683 switch (len)
3685 case sizeof (GFC_INTEGER_4):
3686 buf4 = buf;
3687 memcpy (&u32, &buf4, sizeof (u32));
3688 u32 = __builtin_bswap32 (u32);
3689 return swrite (dtp->u.p.current_unit->s, &u32, len);
3690 break;
3692 case sizeof (GFC_INTEGER_8):
3693 buf8 = buf;
3694 memcpy (&u64, &buf8, sizeof (u64));
3695 u64 = __builtin_bswap64 (u64);
3696 return swrite (dtp->u.p.current_unit->s, &u64, len);
3697 break;
3699 default:
3700 runtime_error ("Illegal value for record marker");
3701 break;
3707 /* Position to the next (sub)record in write mode for
3708 unformatted sequential files. */
3710 static void
3711 next_record_w_unf (st_parameter_dt *dtp, int next_subrecord)
3713 gfc_offset m, m_write, record_marker;
3715 /* Bytes written. */
3716 m = dtp->u.p.current_unit->recl_subrecord
3717 - dtp->u.p.current_unit->bytes_left_subrecord;
3719 if (compile_options.record_marker == 0)
3720 record_marker = sizeof (GFC_INTEGER_4);
3721 else
3722 record_marker = compile_options.record_marker;
3724 /* Seek to the head and overwrite the bogus length with the real
3725 length. */
3727 if (unlikely (sseek (dtp->u.p.current_unit->s, - m - record_marker,
3728 SEEK_CUR) < 0))
3729 goto io_error;
3731 if (next_subrecord)
3732 m_write = -m;
3733 else
3734 m_write = m;
3736 if (unlikely (write_us_marker (dtp, m_write) < 0))
3737 goto io_error;
3739 /* Seek past the end of the current record. */
3741 if (unlikely (sseek (dtp->u.p.current_unit->s, m, SEEK_CUR) < 0))
3742 goto io_error;
3744 /* Write the length tail. If we finish a record containing
3745 subrecords, we write out the negative length. */
3747 if (dtp->u.p.current_unit->continued)
3748 m_write = -m;
3749 else
3750 m_write = m;
3752 if (unlikely (write_us_marker (dtp, m_write) < 0))
3753 goto io_error;
3755 return;
3757 io_error:
3758 generate_error (&dtp->common, LIBERROR_OS, NULL);
3759 return;
3764 /* Utility function like memset() but operating on streams. Return
3765 value is same as for POSIX write(). */
3767 static gfc_offset
3768 sset (stream *s, int c, gfc_offset nbyte)
3770 #define WRITE_CHUNK 256
3771 char p[WRITE_CHUNK];
3772 gfc_offset bytes_left;
3773 ssize_t trans;
3775 if (nbyte < WRITE_CHUNK)
3776 memset (p, c, nbyte);
3777 else
3778 memset (p, c, WRITE_CHUNK);
3780 bytes_left = nbyte;
3781 while (bytes_left > 0)
3783 trans = (bytes_left < WRITE_CHUNK) ? bytes_left : WRITE_CHUNK;
3784 trans = swrite (s, p, trans);
3785 if (trans <= 0)
3786 return trans;
3787 bytes_left -= trans;
3790 return nbyte - bytes_left;
3794 /* Finish up a record according to the legacy carriagecontrol type, based
3795 on the first character in the record. */
3797 static void
3798 next_record_cc (st_parameter_dt *dtp)
3800 /* Only valid with CARRIAGECONTROL=FORTRAN. */
3801 if (dtp->u.p.current_unit->flags.cc != CC_FORTRAN)
3802 return;
3804 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
3805 if (dtp->u.p.cc.len > 0)
3807 char *p = fbuf_alloc (dtp->u.p.current_unit, dtp->u.p.cc.len);
3808 if (!p)
3809 generate_error (&dtp->common, LIBERROR_OS, NULL);
3811 /* Output CR for the first character with default CC setting. */
3812 *(p++) = dtp->u.p.cc.u.end;
3813 if (dtp->u.p.cc.len > 1)
3814 *p = dtp->u.p.cc.u.end;
3818 /* Position to the next record in write mode. */
3820 static void
3821 next_record_w (st_parameter_dt *dtp, int done)
3823 gfc_offset max_pos_off;
3825 /* Zero counters for X- and T-editing. */
3826 max_pos_off = dtp->u.p.max_pos;
3827 dtp->u.p.max_pos = dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
3829 switch (current_mode (dtp))
3831 /* No records in unformatted STREAM I/O. */
3832 case UNFORMATTED_STREAM:
3833 return;
3835 case FORMATTED_DIRECT:
3836 if (dtp->u.p.current_unit->bytes_left == 0)
3837 break;
3839 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
3840 fbuf_flush (dtp->u.p.current_unit, WRITING);
3841 if (sset (dtp->u.p.current_unit->s, ' ',
3842 dtp->u.p.current_unit->bytes_left)
3843 != dtp->u.p.current_unit->bytes_left)
3844 goto io_error;
3846 break;
3848 case UNFORMATTED_DIRECT:
3849 if (dtp->u.p.current_unit->bytes_left > 0)
3851 gfc_offset length = dtp->u.p.current_unit->bytes_left;
3852 if (sset (dtp->u.p.current_unit->s, 0, length) != length)
3853 goto io_error;
3855 break;
3857 case UNFORMATTED_SEQUENTIAL:
3858 next_record_w_unf (dtp, 0);
3859 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
3860 break;
3862 case FORMATTED_STREAM:
3863 case FORMATTED_SEQUENTIAL:
3865 if (is_internal_unit (dtp))
3867 char *p;
3868 /* Internal unit, so must fit in memory. */
3869 size_t length, m;
3870 size_t max_pos = max_pos_off;
3871 if (is_array_io (dtp))
3873 int finished;
3875 length = dtp->u.p.current_unit->bytes_left;
3877 /* If the farthest position reached is greater than current
3878 position, adjust the position and set length to pad out
3879 whats left. Otherwise just pad whats left.
3880 (for character array unit) */
3881 m = dtp->u.p.current_unit->recl
3882 - dtp->u.p.current_unit->bytes_left;
3883 if (max_pos > m)
3885 length = (max_pos - m);
3886 if (sseek (dtp->u.p.current_unit->s,
3887 length, SEEK_CUR) < 0)
3889 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3890 return;
3892 length = ((size_t) dtp->u.p.current_unit->recl - max_pos);
3895 p = write_block (dtp, length);
3896 if (p == NULL)
3897 return;
3899 if (unlikely (is_char4_unit (dtp)))
3901 gfc_char4_t *p4 = (gfc_char4_t *) p;
3902 memset4 (p4, ' ', length);
3904 else
3905 memset (p, ' ', length);
3907 /* Now that the current record has been padded out,
3908 determine where the next record in the array is.
3909 Note that this can return a negative value, so it
3910 needs to be assigned to a signed value. */
3911 gfc_offset record = next_array_record
3912 (dtp, dtp->u.p.current_unit->ls, &finished);
3913 if (finished)
3914 dtp->u.p.current_unit->endfile = AT_ENDFILE;
3916 /* Now seek to this record */
3917 record = record * dtp->u.p.current_unit->recl;
3919 if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
3921 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3922 return;
3925 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
3927 else
3929 length = 1;
3931 /* If this is the last call to next_record move to the farthest
3932 position reached and set length to pad out the remainder
3933 of the record. (for character scaler unit) */
3934 if (done)
3936 m = dtp->u.p.current_unit->recl
3937 - dtp->u.p.current_unit->bytes_left;
3938 if (max_pos > m)
3940 length = max_pos - m;
3941 if (sseek (dtp->u.p.current_unit->s,
3942 length, SEEK_CUR) < 0)
3944 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3945 return;
3947 length = (size_t) dtp->u.p.current_unit->recl
3948 - max_pos;
3950 else
3951 length = dtp->u.p.current_unit->bytes_left;
3953 if (length > 0)
3955 p = write_block (dtp, length);
3956 if (p == NULL)
3957 return;
3959 if (unlikely (is_char4_unit (dtp)))
3961 gfc_char4_t *p4 = (gfc_char4_t *) p;
3962 memset4 (p4, (gfc_char4_t) ' ', length);
3964 else
3965 memset (p, ' ', length);
3969 /* Handle legacy CARRIAGECONTROL line endings. */
3970 else if (dtp->u.p.current_unit->flags.cc == CC_FORTRAN)
3971 next_record_cc (dtp);
3972 else
3974 /* Skip newlines for CC=CC_NONE. */
3975 const int len = (dtp->u.p.current_unit->flags.cc == CC_NONE)
3977 #ifdef HAVE_CRLF
3978 : 2;
3979 #else
3980 : 1;
3981 #endif
3982 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
3983 if (dtp->u.p.current_unit->flags.cc != CC_NONE)
3985 char *p = fbuf_alloc (dtp->u.p.current_unit, len);
3986 if (!p)
3987 goto io_error;
3988 #ifdef HAVE_CRLF
3989 *(p++) = '\r';
3990 #endif
3991 *p = '\n';
3993 if (is_stream_io (dtp))
3995 dtp->u.p.current_unit->strm_pos += len;
3996 if (dtp->u.p.current_unit->strm_pos
3997 < ssize (dtp->u.p.current_unit->s))
3998 unit_truncate (dtp->u.p.current_unit,
3999 dtp->u.p.current_unit->strm_pos - 1,
4000 &dtp->common);
4004 break;
4006 io_error:
4007 generate_error (&dtp->common, LIBERROR_OS, NULL);
4008 break;
4012 /* Position to the next record, which means moving to the end of the
4013 current record. This can happen under several different
4014 conditions. If the done flag is not set, we get ready to process
4015 the next record. */
4017 void
4018 next_record (st_parameter_dt *dtp, int done)
4020 gfc_offset fp; /* File position. */
4022 dtp->u.p.current_unit->read_bad = 0;
4024 if (dtp->u.p.mode == READING)
4025 next_record_r (dtp, done);
4026 else
4027 next_record_w (dtp, done);
4029 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
4031 if (!is_stream_io (dtp))
4033 /* Since we have changed the position, set it to unspecified so
4034 that INQUIRE(POSITION=) knows it needs to look into it. */
4035 if (done)
4036 dtp->u.p.current_unit->flags.position = POSITION_UNSPECIFIED;
4038 dtp->u.p.current_unit->current_record = 0;
4039 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
4041 fp = stell (dtp->u.p.current_unit->s);
4042 /* Calculate next record, rounding up partial records. */
4043 dtp->u.p.current_unit->last_record =
4044 (fp + dtp->u.p.current_unit->recl) /
4045 dtp->u.p.current_unit->recl - 1;
4047 else
4048 dtp->u.p.current_unit->last_record++;
4051 if (!done)
4052 pre_position (dtp);
4054 smarkeor (dtp->u.p.current_unit->s);
4058 /* Finalize the current data transfer. For a nonadvancing transfer,
4059 this means advancing to the next record. For internal units close the
4060 stream associated with the unit. */
4062 static void
4063 finalize_transfer (st_parameter_dt *dtp)
4065 GFC_INTEGER_4 cf = dtp->common.flags;
4067 if ((dtp->u.p.ionml != NULL)
4068 && (cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0)
4070 dtp->u.p.namelist_mode = 1;
4071 if ((cf & IOPARM_DT_NAMELIST_READ_MODE) != 0)
4072 namelist_read (dtp);
4073 else
4074 namelist_write (dtp);
4077 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
4078 *dtp->size = dtp->u.p.current_unit->size_used;
4080 if (dtp->u.p.eor_condition)
4082 generate_error (&dtp->common, LIBERROR_EOR, NULL);
4083 goto done;
4086 if (dtp->u.p.current_unit && (dtp->u.p.current_unit->child_dtio > 0))
4088 if (cf & IOPARM_DT_HAS_FORMAT)
4090 free (dtp->u.p.fmt);
4091 free (dtp->format);
4093 return;
4096 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
4098 if (dtp->u.p.current_unit && current_mode (dtp) == UNFORMATTED_SEQUENTIAL)
4099 dtp->u.p.current_unit->current_record = 0;
4100 goto done;
4103 dtp->u.p.transfer = NULL;
4104 if (dtp->u.p.current_unit == NULL)
4105 goto done;
4107 if ((cf & IOPARM_DT_LIST_FORMAT) != 0 && dtp->u.p.mode == READING)
4109 finish_list_read (dtp);
4110 goto done;
4113 if (dtp->u.p.mode == WRITING)
4114 dtp->u.p.current_unit->previous_nonadvancing_write
4115 = dtp->u.p.advance_status == ADVANCE_NO;
4117 if (is_stream_io (dtp))
4119 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
4120 && dtp->u.p.advance_status != ADVANCE_NO)
4121 next_record (dtp, 1);
4123 goto done;
4126 dtp->u.p.current_unit->current_record = 0;
4128 if (!is_internal_unit (dtp) && dtp->u.p.seen_dollar)
4130 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
4131 dtp->u.p.seen_dollar = 0;
4132 goto done;
4135 /* For non-advancing I/O, save the current maximum position for use in the
4136 next I/O operation if needed. */
4137 if (dtp->u.p.advance_status == ADVANCE_NO)
4139 if (dtp->u.p.skips > 0)
4141 int tmp;
4142 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
4143 tmp = (int)(dtp->u.p.current_unit->recl
4144 - dtp->u.p.current_unit->bytes_left);
4145 dtp->u.p.max_pos =
4146 dtp->u.p.max_pos > tmp ? dtp->u.p.max_pos : tmp;
4147 dtp->u.p.skips = 0;
4149 int bytes_written = (int) (dtp->u.p.current_unit->recl
4150 - dtp->u.p.current_unit->bytes_left);
4151 dtp->u.p.current_unit->saved_pos =
4152 dtp->u.p.max_pos > 0 ? dtp->u.p.max_pos - bytes_written : 0;
4153 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
4154 goto done;
4156 else if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
4157 && dtp->u.p.mode == WRITING && !is_internal_unit (dtp))
4158 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
4160 dtp->u.p.current_unit->saved_pos = 0;
4161 dtp->u.p.current_unit->last_char = EOF - 1;
4162 next_record (dtp, 1);
4164 done:
4166 if (dtp->u.p.unit_is_internal)
4168 /* The unit structure may be reused later so clear the
4169 internal unit kind. */
4170 dtp->u.p.current_unit->internal_unit_kind = 0;
4172 fbuf_destroy (dtp->u.p.current_unit);
4173 if (dtp->u.p.current_unit
4174 && (dtp->u.p.current_unit->child_dtio == 0)
4175 && dtp->u.p.current_unit->s)
4177 sclose (dtp->u.p.current_unit->s);
4178 dtp->u.p.current_unit->s = NULL;
4182 #ifdef HAVE_USELOCALE
4183 if (dtp->u.p.old_locale != (locale_t) 0)
4185 uselocale (dtp->u.p.old_locale);
4186 dtp->u.p.old_locale = (locale_t) 0;
4188 #else
4189 __gthread_mutex_lock (&old_locale_lock);
4190 if (!--old_locale_ctr)
4192 setlocale (LC_NUMERIC, old_locale);
4193 old_locale = NULL;
4195 __gthread_mutex_unlock (&old_locale_lock);
4196 #endif
4199 /* Transfer function for IOLENGTH. It doesn't actually do any
4200 data transfer, it just updates the length counter. */
4202 static void
4203 iolength_transfer (st_parameter_dt *dtp, bt type __attribute__((unused)),
4204 void *dest __attribute__ ((unused)),
4205 int kind __attribute__((unused)),
4206 size_t size, size_t nelems)
4208 if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
4209 *dtp->iolength += (GFC_IO_INT) (size * nelems);
4213 /* Initialize the IOLENGTH data transfer. This function is in essence
4214 a very much simplified version of data_transfer_init(), because it
4215 doesn't have to deal with units at all. */
4217 static void
4218 iolength_transfer_init (st_parameter_dt *dtp)
4220 if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
4221 *dtp->iolength = 0;
4223 memset (&dtp->u.p, 0, sizeof (dtp->u.p));
4225 /* Set up the subroutine that will handle the transfers. */
4227 dtp->u.p.transfer = iolength_transfer;
4231 /* Library entry point for the IOLENGTH form of the INQUIRE
4232 statement. The IOLENGTH form requires no I/O to be performed, but
4233 it must still be a runtime library call so that we can determine
4234 the iolength for dynamic arrays and such. */
4236 extern void st_iolength (st_parameter_dt *);
4237 export_proto(st_iolength);
4239 void
4240 st_iolength (st_parameter_dt *dtp)
4242 library_start (&dtp->common);
4243 iolength_transfer_init (dtp);
4246 extern void st_iolength_done (st_parameter_dt *);
4247 export_proto(st_iolength_done);
4249 void
4250 st_iolength_done (st_parameter_dt *dtp __attribute__((unused)))
4252 free_ionml (dtp);
4253 library_end ();
4257 /* The READ statement. */
4259 extern void st_read (st_parameter_dt *);
4260 export_proto(st_read);
4262 void
4263 st_read (st_parameter_dt *dtp)
4265 library_start (&dtp->common);
4267 data_transfer_init (dtp, 1);
4270 extern void st_read_done (st_parameter_dt *);
4271 export_proto(st_read_done);
4273 void
4274 st_read_done_worker (st_parameter_dt *dtp)
4276 finalize_transfer (dtp);
4278 free_ionml (dtp);
4280 /* If this is a parent READ statement we do not need to retain the
4281 internal unit structure for child use. */
4282 if (dtp->u.p.current_unit != NULL
4283 && dtp->u.p.current_unit->child_dtio == 0)
4285 if (dtp->u.p.unit_is_internal)
4287 if ((dtp->common.flags & IOPARM_DT_HAS_UDTIO) == 0)
4289 free (dtp->u.p.current_unit->filename);
4290 dtp->u.p.current_unit->filename = NULL;
4291 if (dtp->u.p.current_unit->ls)
4292 free (dtp->u.p.current_unit->ls);
4293 dtp->u.p.current_unit->ls = NULL;
4295 newunit_free (dtp->common.unit);
4297 if (dtp->u.p.unit_is_internal || dtp->u.p.format_not_saved)
4299 free_format_data (dtp->u.p.fmt);
4300 free_format (dtp);
4305 void
4306 st_read_done (st_parameter_dt *dtp)
4308 if (dtp->u.p.current_unit)
4310 if (dtp->u.p.current_unit->au)
4312 if (dtp->common.flags & IOPARM_DT_HAS_ID)
4313 *dtp->id = enqueue_done_id (dtp->u.p.current_unit->au, AIO_READ_DONE);
4314 else
4316 if (dtp->u.p.async)
4317 enqueue_done (dtp->u.p.current_unit->au, AIO_READ_DONE);
4320 else
4321 st_read_done_worker (dtp);
4323 unlock_unit (dtp->u.p.current_unit);
4326 library_end ();
4329 extern void st_write (st_parameter_dt *);
4330 export_proto (st_write);
4332 void
4333 st_write (st_parameter_dt *dtp)
4335 library_start (&dtp->common);
4336 data_transfer_init (dtp, 0);
4340 void
4341 st_write_done_worker (st_parameter_dt *dtp)
4343 finalize_transfer (dtp);
4345 if (dtp->u.p.current_unit != NULL
4346 && dtp->u.p.current_unit->child_dtio == 0)
4348 /* Deal with endfile conditions associated with sequential files. */
4349 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
4350 switch (dtp->u.p.current_unit->endfile)
4352 case AT_ENDFILE: /* Remain at the endfile record. */
4353 break;
4355 case AFTER_ENDFILE:
4356 dtp->u.p.current_unit->endfile = AT_ENDFILE; /* Just at it now. */
4357 break;
4359 case NO_ENDFILE:
4360 /* Get rid of whatever is after this record. */
4361 if (!is_internal_unit (dtp))
4362 unit_truncate (dtp->u.p.current_unit,
4363 stell (dtp->u.p.current_unit->s),
4364 &dtp->common);
4365 dtp->u.p.current_unit->endfile = AT_ENDFILE;
4366 break;
4369 free_ionml (dtp);
4371 /* If this is a parent WRITE statement we do not need to retain the
4372 internal unit structure for child use. */
4373 if (dtp->u.p.unit_is_internal)
4375 if ((dtp->common.flags & IOPARM_DT_HAS_UDTIO) == 0)
4377 free (dtp->u.p.current_unit->filename);
4378 dtp->u.p.current_unit->filename = NULL;
4379 if (dtp->u.p.current_unit->ls)
4380 free (dtp->u.p.current_unit->ls);
4381 dtp->u.p.current_unit->ls = NULL;
4383 newunit_free (dtp->common.unit);
4385 if (dtp->u.p.unit_is_internal || dtp->u.p.format_not_saved)
4387 free_format_data (dtp->u.p.fmt);
4388 free_format (dtp);
4393 extern void st_write_done (st_parameter_dt *);
4394 export_proto(st_write_done);
4396 void
4397 st_write_done (st_parameter_dt *dtp)
4399 if (dtp->u.p.current_unit)
4401 if (dtp->u.p.current_unit->au && dtp->u.p.async)
4403 if (dtp->common.flags & IOPARM_DT_HAS_ID)
4404 *dtp->id = enqueue_done_id (dtp->u.p.current_unit->au,
4405 AIO_WRITE_DONE);
4406 else
4408 /* We perform synchronous I/O on an asynchronous unit, so no need
4409 to enqueue AIO_READ_DONE. */
4410 if (dtp->u.p.async)
4411 enqueue_done (dtp->u.p.current_unit->au, AIO_WRITE_DONE);
4414 else
4415 st_write_done_worker (dtp);
4417 unlock_unit (dtp->u.p.current_unit);
4420 library_end ();
4423 /* Wait operation. We need to keep around the do-nothing version
4424 of st_wait for compatibility with previous versions, which had marked
4425 the argument as unused (and thus liable to be removed).
4427 TODO: remove at next bump in version number. */
4429 void
4430 st_wait (st_parameter_wait *wtp __attribute__((unused)))
4432 return;
4435 void
4436 st_wait_async (st_parameter_wait *wtp)
4438 gfc_unit *u = find_unit (wtp->common.unit);
4439 if (ASYNC_IO && u->au)
4441 if (wtp->common.flags & IOPARM_WAIT_HAS_ID)
4442 async_wait_id (&(wtp->common), u->au, *wtp->id);
4443 else
4444 async_wait (&(wtp->common), u->au);
4447 unlock_unit (u);
4451 /* Receives the scalar information for namelist objects and stores it
4452 in a linked list of namelist_info types. */
4454 static void
4455 set_nml_var (st_parameter_dt *dtp, void *var_addr, char *var_name,
4456 GFC_INTEGER_4 len, gfc_charlen_type string_length,
4457 dtype_type dtype, void *dtio_sub, void *vtable)
4459 namelist_info *t1 = NULL;
4460 namelist_info *nml;
4461 size_t var_name_len = strlen (var_name);
4463 nml = (namelist_info*) xmalloc (sizeof (namelist_info));
4465 nml->mem_pos = var_addr;
4466 nml->dtio_sub = dtio_sub;
4467 nml->vtable = vtable;
4469 nml->var_name = (char*) xmalloc (var_name_len + 1);
4470 memcpy (nml->var_name, var_name, var_name_len);
4471 nml->var_name[var_name_len] = '\0';
4473 nml->len = (int) len;
4474 nml->string_length = (index_type) string_length;
4476 nml->var_rank = (int) (dtype.rank);
4477 nml->size = (index_type) (dtype.elem_len);
4478 nml->type = (bt) (dtype.type);
4480 if (nml->var_rank > 0)
4482 nml->dim = (descriptor_dimension*)
4483 xmallocarray (nml->var_rank, sizeof (descriptor_dimension));
4484 nml->ls = (array_loop_spec*)
4485 xmallocarray (nml->var_rank, sizeof (array_loop_spec));
4487 else
4489 nml->dim = NULL;
4490 nml->ls = NULL;
4493 nml->next = NULL;
4495 if ((dtp->common.flags & IOPARM_DT_IONML_SET) == 0)
4497 dtp->common.flags |= IOPARM_DT_IONML_SET;
4498 dtp->u.p.ionml = nml;
4500 else
4502 for (t1 = dtp->u.p.ionml; t1->next; t1 = t1->next);
4503 t1->next = nml;
4507 extern void st_set_nml_var (st_parameter_dt *dtp, void *, char *,
4508 GFC_INTEGER_4, gfc_charlen_type, dtype_type);
4509 export_proto(st_set_nml_var);
4511 void
4512 st_set_nml_var (st_parameter_dt *dtp, void *var_addr, char *var_name,
4513 GFC_INTEGER_4 len, gfc_charlen_type string_length,
4514 dtype_type dtype)
4516 set_nml_var (dtp, var_addr, var_name, len, string_length,
4517 dtype, NULL, NULL);
4521 /* Essentially the same as previous but carrying the dtio procedure
4522 and the vtable as additional arguments. */
4523 extern void st_set_nml_dtio_var (st_parameter_dt *dtp, void *, char *,
4524 GFC_INTEGER_4, gfc_charlen_type, dtype_type,
4525 void *, void *);
4526 export_proto(st_set_nml_dtio_var);
4529 void
4530 st_set_nml_dtio_var (st_parameter_dt *dtp, void *var_addr, char *var_name,
4531 GFC_INTEGER_4 len, gfc_charlen_type string_length,
4532 dtype_type dtype, void *dtio_sub, void *vtable)
4534 set_nml_var (dtp, var_addr, var_name, len, string_length,
4535 dtype, dtio_sub, vtable);
4538 /* Store the dimensional information for the namelist object. */
4539 extern void st_set_nml_var_dim (st_parameter_dt *, GFC_INTEGER_4,
4540 index_type, index_type,
4541 index_type);
4542 export_proto(st_set_nml_var_dim);
4544 void
4545 st_set_nml_var_dim (st_parameter_dt *dtp, GFC_INTEGER_4 n_dim,
4546 index_type stride, index_type lbound,
4547 index_type ubound)
4549 namelist_info *nml;
4550 int n;
4552 n = (int)n_dim;
4554 for (nml = dtp->u.p.ionml; nml->next; nml = nml->next);
4556 GFC_DIMENSION_SET(nml->dim[n],lbound,ubound,stride);
4560 /* Once upon a time, a poor innocent Fortran program was reading a
4561 file, when suddenly it hit the end-of-file (EOF). Unfortunately
4562 the OS doesn't tell whether we're at the EOF or whether we already
4563 went past it. Luckily our hero, libgfortran, keeps track of this.
4564 Call this function when you detect an EOF condition. See Section
4565 9.10.2 in F2003. */
4567 void
4568 hit_eof (st_parameter_dt *dtp)
4570 dtp->u.p.current_unit->flags.position = POSITION_APPEND;
4572 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
4573 switch (dtp->u.p.current_unit->endfile)
4575 case NO_ENDFILE:
4576 case AT_ENDFILE:
4577 generate_error (&dtp->common, LIBERROR_END, NULL);
4578 if (!is_internal_unit (dtp) && !dtp->u.p.namelist_mode)
4580 dtp->u.p.current_unit->endfile = AFTER_ENDFILE;
4581 dtp->u.p.current_unit->current_record = 0;
4583 else
4584 dtp->u.p.current_unit->endfile = AT_ENDFILE;
4585 break;
4587 case AFTER_ENDFILE:
4588 generate_error (&dtp->common, LIBERROR_ENDFILE, NULL);
4589 dtp->u.p.current_unit->current_record = 0;
4590 break;
4592 else
4594 /* Non-sequential files don't have an ENDFILE record, so we
4595 can't be at AFTER_ENDFILE. */
4596 dtp->u.p.current_unit->endfile = AT_ENDFILE;
4597 generate_error (&dtp->common, LIBERROR_END, NULL);
4598 dtp->u.p.current_unit->current_record = 0;