1 /* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008
2 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
4 Namelist transfer functions contributed by Paul Thomas
5 F2003 I/O support contributed by Jerry DeLisle
7 This file is part of the GNU Fortran 95 runtime library (libgfortran).
9 Libgfortran is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 In addition to the permissions in the GNU General Public License, the
15 Free Software Foundation gives you unlimited permission to link the
16 compiled version of this file into combinations with other programs,
17 and to distribute those combinations without any restriction coming
18 from the use of this file. (The General Public License restrictions
19 do apply in other respects; for example, they cover modification of
20 the file, and distribution when not linked into a combine
23 Libgfortran is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with Libgfortran; see the file COPYING. If not, write to
30 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
31 Boston, MA 02110-1301, USA. */
34 /* transfer.c -- Top level handling of data transfer statements. */
42 /* Calling conventions: Data transfer statements are unlike other
43 library calls in that they extend over several calls.
45 The first call is always a call to st_read() or st_write(). These
46 subroutines return no status unless a namelist read or write is
47 being done, in which case there is the usual status. No further
48 calls are necessary in this case.
50 For other sorts of data transfer, there are zero or more data
51 transfer statement that depend on the format of the data transfer
57 transfer_character_wide
61 These subroutines do not return status.
63 The last call is a call to st_[read|write]_done(). While
64 something can easily go wrong with the initial st_read() or
65 st_write(), an error inhibits any data from actually being
68 extern void transfer_integer (st_parameter_dt *, void *, int);
69 export_proto(transfer_integer);
71 extern void transfer_real (st_parameter_dt *, void *, int);
72 export_proto(transfer_real);
74 extern void transfer_logical (st_parameter_dt *, void *, int);
75 export_proto(transfer_logical);
77 extern void transfer_character (st_parameter_dt *, void *, int);
78 export_proto(transfer_character);
80 extern void transfer_character_wide (st_parameter_dt *, void *, int, int);
81 export_proto(transfer_character_wide);
83 extern void transfer_complex (st_parameter_dt *, void *, int);
84 export_proto(transfer_complex);
86 extern void transfer_array (st_parameter_dt *, gfc_array_char *, int,
88 export_proto(transfer_array);
90 static void us_read (st_parameter_dt *, int);
91 static void us_write (st_parameter_dt *, int);
92 static void next_record_r_unf (st_parameter_dt *, int);
93 static void next_record_w_unf (st_parameter_dt *, int);
95 static const st_option advance_opt[] = {
102 static const st_option decimal_opt[] = {
103 {"point", DECIMAL_POINT},
104 {"comma", DECIMAL_COMMA},
109 static const st_option sign_opt[] = {
111 {"suppress", SIGN_SS},
112 {"processor_defined", SIGN_S},
116 static const st_option blank_opt[] = {
117 {"null", BLANK_NULL},
118 {"zero", BLANK_ZERO},
122 static const st_option delim_opt[] = {
123 {"apostrophe", DELIM_APOSTROPHE},
124 {"quote", DELIM_QUOTE},
125 {"none", DELIM_NONE},
129 static const st_option pad_opt[] = {
136 { FORMATTED_SEQUENTIAL, UNFORMATTED_SEQUENTIAL,
137 FORMATTED_DIRECT, UNFORMATTED_DIRECT, FORMATTED_STREAM, UNFORMATTED_STREAM
143 current_mode (st_parameter_dt *dtp)
147 m = FORM_UNSPECIFIED;
149 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
151 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
152 FORMATTED_DIRECT : UNFORMATTED_DIRECT;
154 else if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
156 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
157 FORMATTED_SEQUENTIAL : UNFORMATTED_SEQUENTIAL;
159 else if (dtp->u.p.current_unit->flags.access == ACCESS_STREAM)
161 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
162 FORMATTED_STREAM : UNFORMATTED_STREAM;
169 /* Mid level data transfer statements. These subroutines do reading
170 and writing in the style of salloc_r()/salloc_w() within the
173 /* When reading sequential formatted records we have a problem. We
174 don't know how long the line is until we read the trailing newline,
175 and we don't want to read too much. If we read too much, we might
176 have to do a physical seek backwards depending on how much data is
177 present, and devices like terminals aren't seekable and would cause
180 Given this, the solution is to read a byte at a time, stopping if
181 we hit the newline. For small allocations, we use a static buffer.
182 For larger allocations, we are forced to allocate memory on the
183 heap. Hopefully this won't happen very often. */
186 read_sf (st_parameter_dt *dtp, int *length, int no_error)
193 if (*length > SCRATCH_SIZE)
194 dtp->u.p.line_buffer = get_mem (*length);
195 p = base = dtp->u.p.line_buffer;
197 /* If we have seen an eor previously, return a length of 0. The
198 caller is responsible for correctly padding the input field. */
199 if (dtp->u.p.sf_seen_eor)
205 if (is_internal_unit (dtp))
208 if (sread (dtp->u.p.current_unit->s, p, &readlen) != 0 || readlen < (size_t) *length)
210 generate_error (&dtp->common, LIBERROR_END, NULL);
222 if (sread (dtp->u.p.current_unit->s, &q, &readlen) != 0)
224 generate_error (&dtp->common, LIBERROR_END, NULL);
228 /* If we have a line without a terminating \n, drop through to
230 if (readlen < 1 && n == 0)
234 generate_error (&dtp->common, LIBERROR_END, NULL);
238 if (readlen < 1 || q == '\n' || q == '\r')
240 /* Unexpected end of line. */
242 /* If we see an EOR during non-advancing I/O, we need to skip
243 the rest of the I/O statement. Set the corresponding flag. */
244 if (dtp->u.p.advance_status == ADVANCE_NO || dtp->u.p.seen_dollar)
245 dtp->u.p.eor_condition = 1;
248 /* If we encounter a CR, it might be a CRLF. */
249 if (q == '\r') /* Probably a CRLF */
252 pos = stream_offset (dtp->u.p.current_unit->s);
253 if (sread (dtp->u.p.current_unit->s, &q, &readlen) != 0)
255 generate_error (&dtp->common, LIBERROR_END, NULL);
258 if (q != '\n' && readlen == 1) /* Not a CRLF after all. */
259 sseek (dtp->u.p.current_unit->s, pos);
264 /* Without padding, terminate the I/O statement without assigning
265 the value. With padding, the value still needs to be assigned,
266 so we can just continue with a short read. */
267 if (dtp->u.p.pad_status == PAD_NO)
271 generate_error (&dtp->common, LIBERROR_EOR, NULL);
276 dtp->u.p.sf_seen_eor = (crlf ? 2 : 1);
279 /* Short circuit the read if a comma is found during numeric input.
280 The flag is set to zero during character reads so that commas in
281 strings are not ignored */
283 if (dtp->u.p.sf_read_comma == 1)
285 notify_std (&dtp->common, GFC_STD_GNU,
286 "Comma in formatted numeric read.");
293 dtp->u.p.sf_seen_eor = 0;
298 dtp->u.p.current_unit->bytes_left -= *length;
300 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
301 dtp->u.p.size_used += (gfc_offset) *length;
307 /* Function for reading the next couple of bytes from the current
308 file, advancing the current position. We return FAILURE on end of record or
309 end of file. This function is only for formatted I/O, unformatted uses
312 If the read is short, then it is because the current record does not
313 have enough data to satisfy the read request and the file was
314 opened with PAD=YES. The caller must assume tailing spaces for
318 read_block_form (st_parameter_dt *dtp, void *buf, size_t *nbytes)
324 if (!is_stream_io (dtp))
326 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) *nbytes)
328 /* For preconnected units with default record length, set bytes left
329 to unit record length and proceed, otherwise error. */
330 if (dtp->u.p.current_unit->unit_number == options.stdin_unit
331 && dtp->u.p.current_unit->recl == DEFAULT_RECL)
332 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
335 if (dtp->u.p.pad_status == PAD_NO)
337 /* Not enough data left. */
338 generate_error (&dtp->common, LIBERROR_EOR, NULL);
343 if (dtp->u.p.current_unit->bytes_left == 0)
345 dtp->u.p.current_unit->endfile = AT_ENDFILE;
346 generate_error (&dtp->common, LIBERROR_END, NULL);
350 *nbytes = dtp->u.p.current_unit->bytes_left;
354 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
355 (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL ||
356 dtp->u.p.current_unit->flags.access == ACCESS_STREAM))
359 source = read_sf (dtp, &nb, 0);
361 dtp->u.p.current_unit->strm_pos +=
362 (gfc_offset) (*nbytes + dtp->u.p.sf_seen_eor);
365 memcpy (buf, source, *nbytes);
368 dtp->u.p.current_unit->bytes_left -= (gfc_offset) *nbytes;
371 if (sread (dtp->u.p.current_unit->s, buf, &nread) != 0)
373 generate_error (&dtp->common, LIBERROR_OS, NULL);
377 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
378 dtp->u.p.size_used += (gfc_offset) nread;
380 if (nread != *nbytes)
381 { /* Short read, this shouldn't happen. */
382 if (dtp->u.p.pad_status == PAD_YES)
386 generate_error (&dtp->common, LIBERROR_EOR, NULL);
391 dtp->u.p.current_unit->strm_pos += (gfc_offset) nread;
397 /* Reads a block directly into application data space. This is for
398 unformatted files. */
401 read_block_direct (st_parameter_dt *dtp, void *buf, size_t *nbytes)
403 size_t to_read_record;
404 size_t have_read_record;
405 size_t to_read_subrecord;
406 size_t have_read_subrecord;
409 if (is_stream_io (dtp))
411 to_read_record = *nbytes;
412 have_read_record = to_read_record;
413 if (sread (dtp->u.p.current_unit->s, buf, &have_read_record) != 0)
415 generate_error (&dtp->common, LIBERROR_OS, NULL);
419 dtp->u.p.current_unit->strm_pos += (gfc_offset) have_read_record;
421 if (to_read_record != have_read_record)
423 /* Short read, e.g. if we hit EOF. For stream files,
424 we have to set the end-of-file condition. */
425 generate_error (&dtp->common, LIBERROR_END, NULL);
431 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
433 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) *nbytes)
436 to_read_record = (size_t) dtp->u.p.current_unit->bytes_left;
437 *nbytes = to_read_record;
443 to_read_record = *nbytes;
446 dtp->u.p.current_unit->bytes_left -= to_read_record;
448 if (sread (dtp->u.p.current_unit->s, buf, &to_read_record) != 0)
450 generate_error (&dtp->common, LIBERROR_OS, NULL);
454 if (to_read_record != *nbytes)
456 /* Short read, e.g. if we hit EOF. Apparently, we read
457 more than was written to the last record. */
458 *nbytes = to_read_record;
464 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
470 /* Unformatted sequential. We loop over the subrecords, reading
471 until the request has been fulfilled or the record has run out
472 of continuation subrecords. */
474 if (dtp->u.p.current_unit->endfile == AT_ENDFILE)
476 generate_error (&dtp->common, LIBERROR_END, NULL);
480 /* Check whether we exceed the total record length. */
482 if (dtp->u.p.current_unit->flags.has_recl
483 && (*nbytes > (size_t) dtp->u.p.current_unit->bytes_left))
485 to_read_record = (size_t) dtp->u.p.current_unit->bytes_left;
490 to_read_record = *nbytes;
493 have_read_record = 0;
497 if (dtp->u.p.current_unit->bytes_left_subrecord
498 < (gfc_offset) to_read_record)
500 to_read_subrecord = (size_t) dtp->u.p.current_unit->bytes_left_subrecord;
501 to_read_record -= to_read_subrecord;
505 to_read_subrecord = to_read_record;
509 dtp->u.p.current_unit->bytes_left_subrecord -= to_read_subrecord;
511 have_read_subrecord = to_read_subrecord;
512 if (sread (dtp->u.p.current_unit->s, buf + have_read_record,
513 &have_read_subrecord) != 0)
515 generate_error (&dtp->common, LIBERROR_OS, NULL);
519 have_read_record += have_read_subrecord;
521 if (to_read_subrecord != have_read_subrecord)
524 /* Short read, e.g. if we hit EOF. This means the record
525 structure has been corrupted, or the trailing record
526 marker would still be present. */
528 *nbytes = have_read_record;
529 generate_error (&dtp->common, LIBERROR_CORRUPT_FILE, NULL);
533 if (to_read_record > 0)
535 if (dtp->u.p.current_unit->continued)
537 next_record_r_unf (dtp, 0);
542 /* Let's make sure the file position is correctly pre-positioned
543 for the next read statement. */
545 dtp->u.p.current_unit->current_record = 0;
546 next_record_r_unf (dtp, 0);
547 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
553 /* Normal exit, the read request has been fulfilled. */
558 dtp->u.p.current_unit->bytes_left -= have_read_record;
561 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
568 /* Function for writing a block of bytes to the current file at the
569 current position, advancing the file pointer. We are given a length
570 and return a pointer to a buffer that the caller must (completely)
571 fill in. Returns NULL on error. */
574 write_block (st_parameter_dt *dtp, int length)
578 if (!is_stream_io (dtp))
580 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) length)
582 /* For preconnected units with default record length, set bytes left
583 to unit record length and proceed, otherwise error. */
584 if ((dtp->u.p.current_unit->unit_number == options.stdout_unit
585 || dtp->u.p.current_unit->unit_number == options.stderr_unit)
586 && dtp->u.p.current_unit->recl == DEFAULT_RECL)
587 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
590 generate_error (&dtp->common, LIBERROR_EOR, NULL);
595 dtp->u.p.current_unit->bytes_left -= (gfc_offset) length;
598 if (is_internal_unit (dtp))
600 dest = salloc_w (dtp->u.p.current_unit->s, &length);
604 generate_error (&dtp->common, LIBERROR_END, NULL);
608 if (dtp->u.p.current_unit->endfile == AT_ENDFILE)
609 generate_error (&dtp->common, LIBERROR_END, NULL);
613 dest = fbuf_alloc (dtp->u.p.current_unit, length);
616 generate_error (&dtp->common, LIBERROR_OS, NULL);
621 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
622 dtp->u.p.size_used += (gfc_offset) length;
624 dtp->u.p.current_unit->strm_pos += (gfc_offset) length;
630 /* High level interface to swrite(), taking care of errors. This is only
631 called for unformatted files. There are three cases to consider:
632 Stream I/O, unformatted direct, unformatted sequential. */
635 write_buf (st_parameter_dt *dtp, void *buf, size_t nbytes)
638 size_t have_written, to_write_subrecord;
643 if (is_stream_io (dtp))
645 if (swrite (dtp->u.p.current_unit->s, buf, &nbytes) != 0)
647 generate_error (&dtp->common, LIBERROR_OS, NULL);
651 dtp->u.p.current_unit->strm_pos += (gfc_offset) nbytes;
656 /* Unformatted direct access. */
658 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
660 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) nbytes)
662 generate_error (&dtp->common, LIBERROR_DIRECT_EOR, NULL);
666 if (buf == NULL && nbytes == 0)
669 if (swrite (dtp->u.p.current_unit->s, buf, &nbytes) != 0)
671 generate_error (&dtp->common, LIBERROR_OS, NULL);
675 dtp->u.p.current_unit->strm_pos += (gfc_offset) nbytes;
676 dtp->u.p.current_unit->bytes_left -= (gfc_offset) nbytes;
681 /* Unformatted sequential. */
685 if (dtp->u.p.current_unit->flags.has_recl
686 && (gfc_offset) nbytes > dtp->u.p.current_unit->bytes_left)
688 nbytes = dtp->u.p.current_unit->bytes_left;
700 (size_t) dtp->u.p.current_unit->bytes_left_subrecord < nbytes ?
701 (size_t) dtp->u.p.current_unit->bytes_left_subrecord : nbytes;
703 dtp->u.p.current_unit->bytes_left_subrecord -=
704 (gfc_offset) to_write_subrecord;
706 if (swrite (dtp->u.p.current_unit->s, buf + have_written,
707 &to_write_subrecord) != 0)
709 generate_error (&dtp->common, LIBERROR_OS, NULL);
713 dtp->u.p.current_unit->strm_pos += (gfc_offset) to_write_subrecord;
714 nbytes -= to_write_subrecord;
715 have_written += to_write_subrecord;
720 next_record_w_unf (dtp, 1);
723 dtp->u.p.current_unit->bytes_left -= have_written;
726 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
733 /* Master function for unformatted reads. */
736 unformatted_read (st_parameter_dt *dtp, bt type,
737 void *dest, int kind, size_t size, size_t nelems)
741 if (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE
745 if (type == BT_CHARACTER)
746 sz *= GFC_SIZE_OF_CHAR_KIND(kind);
747 read_block_direct (dtp, dest, &sz);
756 /* Handle wide chracters. */
757 if (type == BT_CHARACTER && kind != 1)
763 /* Break up complex into its constituent reals. */
764 if (type == BT_COMPLEX)
770 /* By now, all complex variables have been split into their
771 constituent reals. */
773 for (i = 0; i < nelems; i++)
775 read_block_direct (dtp, buffer, &size);
776 reverse_memcpy (p, buffer, size);
783 /* Master function for unformatted writes. NOTE: For kind=10 the size is 16
784 bytes on 64 bit machines. The unused bytes are not initialized and never
785 used, which can show an error with memory checking analyzers like
789 unformatted_write (st_parameter_dt *dtp, bt type,
790 void *source, int kind, size_t size, size_t nelems)
792 if (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE ||
795 size_t stride = type == BT_CHARACTER ?
796 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
798 write_buf (dtp, source, stride * nelems);
808 /* Handle wide chracters. */
809 if (type == BT_CHARACTER && kind != 1)
815 /* Break up complex into its constituent reals. */
816 if (type == BT_COMPLEX)
822 /* By now, all complex variables have been split into their
823 constituent reals. */
825 for (i = 0; i < nelems; i++)
827 reverse_memcpy(buffer, p, size);
829 write_buf (dtp, buffer, size);
835 /* Return a pointer to the name of a type. */
860 internal_error (NULL, "type_name(): Bad type");
867 /* Write a constant string to the output.
868 This is complicated because the string can have doubled delimiters
869 in it. The length in the format node is the true length. */
872 write_constant_string (st_parameter_dt *dtp, const fnode *f)
874 char c, delimiter, *p, *q;
877 length = f->u.string.length;
881 p = write_block (dtp, length);
888 for (; length > 0; length--)
891 if (c == delimiter && c != 'H' && c != 'h')
892 q++; /* Skip the doubled delimiter. */
897 /* Given actual and expected types in a formatted data transfer, make
898 sure they agree. If not, an error message is generated. Returns
899 nonzero if something went wrong. */
902 require_type (st_parameter_dt *dtp, bt expected, bt actual, const fnode *f)
906 if (actual == expected)
909 sprintf (buffer, "Expected %s for item %d in formatted transfer, got %s",
910 type_name (expected), dtp->u.p.item_count, type_name (actual));
912 format_error (dtp, f, buffer);
917 /* This subroutine is the main loop for a formatted data transfer
918 statement. It would be natural to implement this as a coroutine
919 with the user program, but C makes that awkward. We loop,
920 processing format elements. When we actually have to transfer
921 data instead of just setting flags, we return control to the user
922 program which calls a subroutine that supplies the address and type
923 of the next element, then comes back here to process it. */
926 formatted_transfer_scalar (st_parameter_dt *dtp, bt type, void *p, int kind,
929 char scratch[SCRATCH_SIZE];
934 int consume_data_flag;
936 /* Change a complex data item into a pair of reals. */
938 n = (p == NULL) ? 0 : ((type != BT_COMPLEX) ? 1 : 2);
939 if (type == BT_COMPLEX)
945 /* If there's an EOR condition, we simulate finalizing the transfer
947 if (dtp->u.p.eor_condition)
950 /* Set this flag so that commas in reads cause the read to complete before
951 the entire field has been read. The next read field will start right after
952 the comma in the stream. (Set to 0 for character reads). */
953 dtp->u.p.sf_read_comma = dtp->u.p.decimal_status == DECIMAL_COMMA ? 0 : 1;
954 dtp->u.p.line_buffer = scratch;
958 /* If reversion has occurred and there is another real data item,
959 then we have to move to the next record. */
960 if (dtp->u.p.reversion_flag && n > 0)
962 dtp->u.p.reversion_flag = 0;
963 next_record (dtp, 0);
966 consume_data_flag = 1;
967 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
970 f = next_format (dtp);
973 /* No data descriptors left. */
975 generate_error (&dtp->common, LIBERROR_FORMAT,
976 "Insufficient data descriptors in format after reversion");
980 /* Now discharge T, TR and X movements to the right. This is delayed
981 until a data producing format to suppress trailing spaces. */
984 if (dtp->u.p.mode == WRITING && dtp->u.p.skips != 0
985 && ((n>0 && ( t == FMT_I || t == FMT_B || t == FMT_O
986 || t == FMT_Z || t == FMT_F || t == FMT_E
987 || t == FMT_EN || t == FMT_ES || t == FMT_G
988 || t == FMT_L || t == FMT_A || t == FMT_D))
991 if (dtp->u.p.skips > 0)
994 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
995 tmp = (int)(dtp->u.p.current_unit->recl
996 - dtp->u.p.current_unit->bytes_left);
998 dtp->u.p.max_pos > tmp ? dtp->u.p.max_pos : tmp;
1000 if (dtp->u.p.skips < 0)
1002 if (is_internal_unit (dtp))
1003 move_pos_offset (dtp->u.p.current_unit->s, dtp->u.p.skips);
1005 fbuf_seek (dtp->u.p.current_unit, dtp->u.p.skips);
1006 dtp->u.p.current_unit->bytes_left -= (gfc_offset) dtp->u.p.skips;
1008 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1011 bytes_used = (int)(dtp->u.p.current_unit->recl
1012 - dtp->u.p.current_unit->bytes_left);
1014 if (is_stream_io(dtp))
1022 if (require_type (dtp, BT_INTEGER, type, f))
1025 if (dtp->u.p.mode == READING)
1026 read_decimal (dtp, f, p, kind);
1028 write_i (dtp, f, p, kind);
1036 if (compile_options.allow_std < GFC_STD_GNU
1037 && require_type (dtp, BT_INTEGER, type, f))
1040 if (dtp->u.p.mode == READING)
1041 read_radix (dtp, f, p, kind, 2);
1043 write_b (dtp, f, p, kind);
1051 if (compile_options.allow_std < GFC_STD_GNU
1052 && require_type (dtp, BT_INTEGER, type, f))
1055 if (dtp->u.p.mode == READING)
1056 read_radix (dtp, f, p, kind, 8);
1058 write_o (dtp, f, p, kind);
1066 if (compile_options.allow_std < GFC_STD_GNU
1067 && require_type (dtp, BT_INTEGER, type, f))
1070 if (dtp->u.p.mode == READING)
1071 read_radix (dtp, f, p, kind, 16);
1073 write_z (dtp, f, p, kind);
1081 /* It is possible to have FMT_A with something not BT_CHARACTER such
1082 as when writing out hollerith strings, so check both type
1083 and kind before calling wide character routines. */
1084 if (dtp->u.p.mode == READING)
1086 if (type == BT_CHARACTER && kind == 4)
1087 read_a_char4 (dtp, f, p, size);
1089 read_a (dtp, f, p, size);
1093 if (type == BT_CHARACTER && kind == 4)
1094 write_a_char4 (dtp, f, p, size);
1096 write_a (dtp, f, p, size);
1104 if (dtp->u.p.mode == READING)
1105 read_l (dtp, f, p, kind);
1107 write_l (dtp, f, p, kind);
1114 if (require_type (dtp, BT_REAL, type, f))
1117 if (dtp->u.p.mode == READING)
1118 read_f (dtp, f, p, kind);
1120 write_d (dtp, f, p, kind);
1127 if (require_type (dtp, BT_REAL, type, f))
1130 if (dtp->u.p.mode == READING)
1131 read_f (dtp, f, p, kind);
1133 write_e (dtp, f, p, kind);
1139 if (require_type (dtp, BT_REAL, type, f))
1142 if (dtp->u.p.mode == READING)
1143 read_f (dtp, f, p, kind);
1145 write_en (dtp, f, p, kind);
1152 if (require_type (dtp, BT_REAL, type, f))
1155 if (dtp->u.p.mode == READING)
1156 read_f (dtp, f, p, kind);
1158 write_es (dtp, f, p, kind);
1165 if (require_type (dtp, BT_REAL, type, f))
1168 if (dtp->u.p.mode == READING)
1169 read_f (dtp, f, p, kind);
1171 write_f (dtp, f, p, kind);
1178 if (dtp->u.p.mode == READING)
1182 read_decimal (dtp, f, p, kind);
1185 read_l (dtp, f, p, kind);
1189 read_a_char4 (dtp, f, p, size);
1191 read_a (dtp, f, p, size);
1194 read_f (dtp, f, p, kind);
1203 write_i (dtp, f, p, kind);
1206 write_l (dtp, f, p, kind);
1210 write_a_char4 (dtp, f, p, size);
1212 write_a (dtp, f, p, size);
1215 if (f->u.real.w == 0)
1216 write_real (dtp, p, kind);
1218 write_d (dtp, f, p, kind);
1222 internal_error (&dtp->common,
1223 "formatted_transfer(): Bad type");
1229 consume_data_flag = 0;
1230 if (dtp->u.p.mode == READING)
1232 format_error (dtp, f, "Constant string in input format");
1235 write_constant_string (dtp, f);
1238 /* Format codes that don't transfer data. */
1241 consume_data_flag = 0;
1243 dtp->u.p.skips += f->u.n;
1244 pos = bytes_used + dtp->u.p.skips - 1;
1245 dtp->u.p.pending_spaces = pos - dtp->u.p.max_pos + 1;
1247 /* Writes occur just before the switch on f->format, above, so
1248 that trailing blanks are suppressed, unless we are doing a
1249 non-advancing write in which case we want to output the blanks
1251 if (dtp->u.p.mode == WRITING
1252 && dtp->u.p.advance_status == ADVANCE_NO)
1254 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
1255 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1258 if (dtp->u.p.mode == READING)
1259 read_x (dtp, f->u.n);
1265 consume_data_flag = 0;
1267 if (f->format == FMT_TL)
1270 /* Handle the special case when no bytes have been used yet.
1271 Cannot go below zero. */
1272 if (bytes_used == 0)
1274 dtp->u.p.pending_spaces -= f->u.n;
1275 dtp->u.p.skips -= f->u.n;
1276 dtp->u.p.skips = dtp->u.p.skips < 0 ? 0 : dtp->u.p.skips;
1279 pos = bytes_used - f->u.n;
1283 if (dtp->u.p.mode == READING)
1286 pos = f->u.n - dtp->u.p.pending_spaces - 1;
1289 /* Standard 10.6.1.1: excessive left tabbing is reset to the
1290 left tab limit. We do not check if the position has gone
1291 beyond the end of record because a subsequent tab could
1292 bring us back again. */
1293 pos = pos < 0 ? 0 : pos;
1295 dtp->u.p.skips = dtp->u.p.skips + pos - bytes_used;
1296 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces
1297 + pos - dtp->u.p.max_pos;
1298 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces < 0
1299 ? 0 : dtp->u.p.pending_spaces;
1301 if (dtp->u.p.skips == 0)
1304 /* Writes occur just before the switch on f->format, above, so that
1305 trailing blanks are suppressed. */
1306 if (dtp->u.p.mode == READING)
1308 /* Adjust everything for end-of-record condition */
1309 if (dtp->u.p.sf_seen_eor && !is_internal_unit (dtp))
1311 if (dtp->u.p.sf_seen_eor == 2)
1313 /* The EOR was a CRLF (two bytes wide). */
1314 dtp->u.p.current_unit->bytes_left -= 2;
1315 dtp->u.p.skips -= 2;
1319 /* The EOR marker was only one byte wide. */
1320 dtp->u.p.current_unit->bytes_left--;
1324 dtp->u.p.sf_seen_eor = 0;
1326 if (dtp->u.p.skips < 0)
1328 move_pos_offset (dtp->u.p.current_unit->s, dtp->u.p.skips);
1329 dtp->u.p.current_unit->bytes_left
1330 -= (gfc_offset) dtp->u.p.skips;
1331 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1334 read_x (dtp, dtp->u.p.skips);
1340 consume_data_flag = 0;
1341 dtp->u.p.sign_status = SIGN_S;
1345 consume_data_flag = 0;
1346 dtp->u.p.sign_status = SIGN_SS;
1350 consume_data_flag = 0;
1351 dtp->u.p.sign_status = SIGN_SP;
1355 consume_data_flag = 0 ;
1356 dtp->u.p.blank_status = BLANK_NULL;
1360 consume_data_flag = 0;
1361 dtp->u.p.blank_status = BLANK_ZERO;
1365 consume_data_flag = 0;
1366 dtp->u.p.decimal_status = DECIMAL_COMMA;
1370 consume_data_flag = 0;
1371 dtp->u.p.decimal_status = DECIMAL_POINT;
1375 consume_data_flag = 0;
1376 dtp->u.p.scale_factor = f->u.k;
1380 consume_data_flag = 0;
1381 dtp->u.p.seen_dollar = 1;
1385 consume_data_flag = 0;
1386 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1387 next_record (dtp, 0);
1391 /* A colon descriptor causes us to exit this loop (in
1392 particular preventing another / descriptor from being
1393 processed) unless there is another data item to be
1395 consume_data_flag = 0;
1401 internal_error (&dtp->common, "Bad format node");
1404 /* Free a buffer that we had to allocate during a sequential
1405 formatted read of a block that was larger than the static
1408 if (dtp->u.p.line_buffer != scratch)
1410 free_mem (dtp->u.p.line_buffer);
1411 dtp->u.p.line_buffer = scratch;
1414 /* Adjust the item count and data pointer. */
1416 if ((consume_data_flag > 0) && (n > 0))
1419 p = ((char *) p) + size;
1422 if (dtp->u.p.mode == READING)
1425 pos = (int)(dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left);
1426 dtp->u.p.max_pos = (dtp->u.p.max_pos > pos) ? dtp->u.p.max_pos : pos;
1432 /* Come here when we need a data descriptor but don't have one. We
1433 push the current format node back onto the input, then return and
1434 let the user program call us back with the data. */
1436 unget_format (dtp, f);
1440 formatted_transfer (st_parameter_dt *dtp, bt type, void *p, int kind,
1441 size_t size, size_t nelems)
1447 size_t stride = type == BT_CHARACTER ?
1448 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
1449 /* Big loop over all the elements. */
1450 for (elem = 0; elem < nelems; elem++)
1452 dtp->u.p.item_count++;
1453 formatted_transfer_scalar (dtp, type, tmp + stride*elem, kind, size);
1459 /* Data transfer entry points. The type of the data entity is
1460 implicit in the subroutine call. This prevents us from having to
1461 share a common enum with the compiler. */
1464 transfer_integer (st_parameter_dt *dtp, void *p, int kind)
1466 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1468 dtp->u.p.transfer (dtp, BT_INTEGER, p, kind, kind, 1);
1473 transfer_real (st_parameter_dt *dtp, void *p, int kind)
1476 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1478 size = size_from_real_kind (kind);
1479 dtp->u.p.transfer (dtp, BT_REAL, p, kind, size, 1);
1484 transfer_logical (st_parameter_dt *dtp, void *p, int kind)
1486 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1488 dtp->u.p.transfer (dtp, BT_LOGICAL, p, kind, kind, 1);
1493 transfer_character (st_parameter_dt *dtp, void *p, int len)
1495 static char *empty_string[0];
1497 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1500 /* Strings of zero length can have p == NULL, which confuses the
1501 transfer routines into thinking we need more data elements. To avoid
1502 this, we give them a nice pointer. */
1503 if (len == 0 && p == NULL)
1506 /* Set kind here to 1. */
1507 dtp->u.p.transfer (dtp, BT_CHARACTER, p, 1, len, 1);
1511 transfer_character_wide (st_parameter_dt *dtp, void *p, int len, int kind)
1513 static char *empty_string[0];
1515 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1518 /* Strings of zero length can have p == NULL, which confuses the
1519 transfer routines into thinking we need more data elements. To avoid
1520 this, we give them a nice pointer. */
1521 if (len == 0 && p == NULL)
1524 /* Here we pass the actual kind value. */
1525 dtp->u.p.transfer (dtp, BT_CHARACTER, p, kind, len, 1);
1530 transfer_complex (st_parameter_dt *dtp, void *p, int kind)
1533 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1535 size = size_from_complex_kind (kind);
1536 dtp->u.p.transfer (dtp, BT_COMPLEX, p, kind, size, 1);
1541 transfer_array (st_parameter_dt *dtp, gfc_array_char *desc, int kind,
1542 gfc_charlen_type charlen)
1544 index_type count[GFC_MAX_DIMENSIONS];
1545 index_type extent[GFC_MAX_DIMENSIONS];
1546 index_type stride[GFC_MAX_DIMENSIONS];
1547 index_type stride0, rank, size, type, n;
1552 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1555 type = GFC_DESCRIPTOR_TYPE (desc);
1556 size = GFC_DESCRIPTOR_SIZE (desc);
1558 /* FIXME: What a kludge: Array descriptors and the IO library use
1559 different enums for types. */
1562 case GFC_DTYPE_UNKNOWN:
1563 iotype = BT_NULL; /* Is this correct? */
1565 case GFC_DTYPE_INTEGER:
1566 iotype = BT_INTEGER;
1568 case GFC_DTYPE_LOGICAL:
1569 iotype = BT_LOGICAL;
1571 case GFC_DTYPE_REAL:
1574 case GFC_DTYPE_COMPLEX:
1575 iotype = BT_COMPLEX;
1577 case GFC_DTYPE_CHARACTER:
1578 iotype = BT_CHARACTER;
1581 case GFC_DTYPE_DERIVED:
1582 internal_error (&dtp->common,
1583 "Derived type I/O should have been handled via the frontend.");
1586 internal_error (&dtp->common, "transfer_array(): Bad type");
1589 rank = GFC_DESCRIPTOR_RANK (desc);
1590 for (n = 0; n < rank; n++)
1593 stride[n] = iotype == BT_CHARACTER ?
1594 desc->dim[n].stride * GFC_SIZE_OF_CHAR_KIND(kind) :
1595 desc->dim[n].stride;
1596 extent[n] = desc->dim[n].ubound + 1 - desc->dim[n].lbound;
1598 /* If the extent of even one dimension is zero, then the entire
1599 array section contains zero elements, so we return after writing
1600 a zero array record. */
1605 dtp->u.p.transfer (dtp, iotype, data, kind, size, tsize);
1610 stride0 = stride[0];
1612 /* If the innermost dimension has stride 1, we can do the transfer
1613 in contiguous chunks. */
1619 data = GFC_DESCRIPTOR_DATA (desc);
1623 dtp->u.p.transfer (dtp, iotype, data, kind, size, tsize);
1624 data += stride0 * size * tsize;
1627 while (count[n] == extent[n])
1630 data -= stride[n] * extent[n] * size;
1640 data += stride[n] * size;
1647 /* Preposition a sequential unformatted file while reading. */
1650 us_read (st_parameter_dt *dtp, int continued)
1657 if (dtp->u.p.current_unit->endfile == AT_ENDFILE)
1660 if (compile_options.record_marker == 0)
1661 n = sizeof (GFC_INTEGER_4);
1663 n = compile_options.record_marker;
1667 if (sread (dtp->u.p.current_unit->s, &i, &n) != 0)
1669 generate_error (&dtp->common, LIBERROR_BAD_US, NULL);
1675 dtp->u.p.current_unit->endfile = AT_ENDFILE;
1676 return; /* end of file */
1681 generate_error (&dtp->common, LIBERROR_BAD_US, NULL);
1685 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
1686 if (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE)
1690 case sizeof(GFC_INTEGER_4):
1691 memcpy (&i4, &i, sizeof (i4));
1695 case sizeof(GFC_INTEGER_8):
1696 memcpy (&i8, &i, sizeof (i8));
1701 runtime_error ("Illegal value for record marker");
1708 case sizeof(GFC_INTEGER_4):
1709 reverse_memcpy (&i4, &i, sizeof (i4));
1713 case sizeof(GFC_INTEGER_8):
1714 reverse_memcpy (&i8, &i, sizeof (i8));
1719 runtime_error ("Illegal value for record marker");
1725 dtp->u.p.current_unit->bytes_left_subrecord = i;
1726 dtp->u.p.current_unit->continued = 0;
1730 dtp->u.p.current_unit->bytes_left_subrecord = -i;
1731 dtp->u.p.current_unit->continued = 1;
1735 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
1739 /* Preposition a sequential unformatted file while writing. This
1740 amount to writing a bogus length that will be filled in later. */
1743 us_write (st_parameter_dt *dtp, int continued)
1750 if (compile_options.record_marker == 0)
1751 nbytes = sizeof (GFC_INTEGER_4);
1753 nbytes = compile_options.record_marker ;
1755 if (swrite (dtp->u.p.current_unit->s, &dummy, &nbytes) != 0)
1756 generate_error (&dtp->common, LIBERROR_OS, NULL);
1758 /* For sequential unformatted, if RECL= was not specified in the OPEN
1759 we write until we have more bytes than can fit in the subrecord
1760 markers, then we write a new subrecord. */
1762 dtp->u.p.current_unit->bytes_left_subrecord =
1763 dtp->u.p.current_unit->recl_subrecord;
1764 dtp->u.p.current_unit->continued = continued;
1768 /* Position to the next record prior to transfer. We are assumed to
1769 be before the next record. We also calculate the bytes in the next
1773 pre_position (st_parameter_dt *dtp)
1775 if (dtp->u.p.current_unit->current_record)
1776 return; /* Already positioned. */
1778 switch (current_mode (dtp))
1780 case FORMATTED_STREAM:
1781 case UNFORMATTED_STREAM:
1782 /* There are no records with stream I/O. If the position was specified
1783 data_transfer_init has already positioned the file. If no position
1784 was specified, we continue from where we last left off. I.e.
1785 there is nothing to do here. */
1788 case UNFORMATTED_SEQUENTIAL:
1789 if (dtp->u.p.mode == READING)
1796 case FORMATTED_SEQUENTIAL:
1797 case FORMATTED_DIRECT:
1798 case UNFORMATTED_DIRECT:
1799 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
1803 dtp->u.p.current_unit->current_record = 1;
1807 /* Initialize things for a data transfer. This code is common for
1808 both reading and writing. */
1811 data_transfer_init (st_parameter_dt *dtp, int read_flag)
1813 unit_flags u_flags; /* Used for creating a unit if needed. */
1814 GFC_INTEGER_4 cf = dtp->common.flags;
1815 namelist_info *ionml;
1817 ionml = ((cf & IOPARM_DT_IONML_SET) != 0) ? dtp->u.p.ionml : NULL;
1818 memset (&dtp->u.p, 0, sizeof (dtp->u.p));
1819 dtp->u.p.ionml = ionml;
1820 dtp->u.p.mode = read_flag ? READING : WRITING;
1822 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1825 if ((cf & IOPARM_DT_HAS_SIZE) != 0)
1826 dtp->u.p.size_used = 0; /* Initialize the count. */
1828 dtp->u.p.current_unit = get_unit (dtp, 1);
1829 if (dtp->u.p.current_unit->s == NULL)
1830 { /* Open the unit with some default flags. */
1831 st_parameter_open opp;
1834 if (dtp->common.unit < 0)
1836 close_unit (dtp->u.p.current_unit);
1837 dtp->u.p.current_unit = NULL;
1838 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
1839 "Bad unit number in OPEN statement");
1842 memset (&u_flags, '\0', sizeof (u_flags));
1843 u_flags.access = ACCESS_SEQUENTIAL;
1844 u_flags.action = ACTION_READWRITE;
1846 /* Is it unformatted? */
1847 if (!(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT
1848 | IOPARM_DT_IONML_SET)))
1849 u_flags.form = FORM_UNFORMATTED;
1851 u_flags.form = FORM_UNSPECIFIED;
1853 u_flags.delim = DELIM_UNSPECIFIED;
1854 u_flags.blank = BLANK_UNSPECIFIED;
1855 u_flags.pad = PAD_UNSPECIFIED;
1856 u_flags.decimal = DECIMAL_UNSPECIFIED;
1857 u_flags.encoding = ENCODING_UNSPECIFIED;
1858 u_flags.async = ASYNC_UNSPECIFIED;
1859 u_flags.round = ROUND_UNSPECIFIED;
1860 u_flags.sign = SIGN_UNSPECIFIED;
1861 u_flags.status = STATUS_UNKNOWN;
1863 conv = get_unformatted_convert (dtp->common.unit);
1865 if (conv == GFC_CONVERT_NONE)
1866 conv = compile_options.convert;
1868 /* We use big_endian, which is 0 on little-endian machines
1869 and 1 on big-endian machines. */
1872 case GFC_CONVERT_NATIVE:
1873 case GFC_CONVERT_SWAP:
1876 case GFC_CONVERT_BIG:
1877 conv = big_endian ? GFC_CONVERT_NATIVE : GFC_CONVERT_SWAP;
1880 case GFC_CONVERT_LITTLE:
1881 conv = big_endian ? GFC_CONVERT_SWAP : GFC_CONVERT_NATIVE;
1885 internal_error (&opp.common, "Illegal value for CONVERT");
1889 u_flags.convert = conv;
1891 opp.common = dtp->common;
1892 opp.common.flags &= IOPARM_COMMON_MASK;
1893 dtp->u.p.current_unit = new_unit (&opp, dtp->u.p.current_unit, &u_flags);
1894 dtp->common.flags &= ~IOPARM_COMMON_MASK;
1895 dtp->common.flags |= (opp.common.flags & IOPARM_COMMON_MASK);
1896 if (dtp->u.p.current_unit == NULL)
1900 /* Check the action. */
1902 if (read_flag && dtp->u.p.current_unit->flags.action == ACTION_WRITE)
1904 generate_error (&dtp->common, LIBERROR_BAD_ACTION,
1905 "Cannot read from file opened for WRITE");
1909 if (!read_flag && dtp->u.p.current_unit->flags.action == ACTION_READ)
1911 generate_error (&dtp->common, LIBERROR_BAD_ACTION,
1912 "Cannot write to file opened for READ");
1916 dtp->u.p.first_item = 1;
1918 /* Check the format. */
1920 if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
1923 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED
1924 && (cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
1927 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
1928 "Format present for UNFORMATTED data transfer");
1932 if ((cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0 && dtp->u.p.ionml != NULL)
1934 if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
1935 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
1936 "A format cannot be specified with a namelist");
1938 else if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
1939 !(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT)))
1941 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
1942 "Missing format for FORMATTED data transfer");
1945 if (is_internal_unit (dtp)
1946 && dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
1948 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
1949 "Internal file cannot be accessed by UNFORMATTED "
1954 /* Check the record or position number. */
1956 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT
1957 && (cf & IOPARM_DT_HAS_REC) == 0)
1959 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
1960 "Direct access data transfer requires record number");
1964 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL
1965 && (cf & IOPARM_DT_HAS_REC) != 0)
1967 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
1968 "Record number not allowed for sequential access data transfer");
1972 /* Process the ADVANCE option. */
1974 dtp->u.p.advance_status
1975 = !(cf & IOPARM_DT_HAS_ADVANCE) ? ADVANCE_UNSPECIFIED :
1976 find_option (&dtp->common, dtp->advance, dtp->advance_len, advance_opt,
1977 "Bad ADVANCE parameter in data transfer statement");
1979 if (dtp->u.p.advance_status != ADVANCE_UNSPECIFIED)
1981 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
1983 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
1984 "ADVANCE specification conflicts with sequential access");
1988 if (is_internal_unit (dtp))
1990 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
1991 "ADVANCE specification conflicts with internal file");
1995 if ((cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
1996 != IOPARM_DT_HAS_FORMAT)
1998 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
1999 "ADVANCE specification requires an explicit format");
2006 dtp->u.p.current_unit->previous_nonadvancing_write = 0;
2008 if ((cf & IOPARM_EOR) != 0 && dtp->u.p.advance_status != ADVANCE_NO)
2010 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
2011 "EOR specification requires an ADVANCE specification "
2016 if ((cf & IOPARM_DT_HAS_SIZE) != 0 && dtp->u.p.advance_status != ADVANCE_NO)
2018 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
2019 "SIZE specification requires an ADVANCE specification of NO");
2024 { /* Write constraints. */
2025 if ((cf & IOPARM_END) != 0)
2027 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2028 "END specification cannot appear in a write statement");
2032 if ((cf & IOPARM_EOR) != 0)
2034 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2035 "EOR specification cannot appear in a write statement");
2039 if ((cf & IOPARM_DT_HAS_SIZE) != 0)
2041 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2042 "SIZE specification cannot appear in a write statement");
2047 if (dtp->u.p.advance_status == ADVANCE_UNSPECIFIED)
2048 dtp->u.p.advance_status = ADVANCE_YES;
2050 /* Check the decimal mode. */
2052 dtp->u.p.decimal_status
2053 = !(cf & IOPARM_DT_HAS_DECIMAL) ? DECIMAL_UNSPECIFIED :
2054 find_option (&dtp->common, dtp->decimal, dtp->decimal_len, decimal_opt,
2055 "Bad DECIMAL parameter in data transfer statement");
2057 if (dtp->u.p.decimal_status == DECIMAL_UNSPECIFIED)
2058 dtp->u.p.decimal_status = dtp->u.p.current_unit->flags.decimal;
2060 /* Check the sign mode. */
2061 dtp->u.p.sign_status
2062 = !(cf & IOPARM_DT_HAS_SIGN) ? SIGN_UNSPECIFIED :
2063 find_option (&dtp->common, dtp->sign, dtp->sign_len, sign_opt,
2064 "Bad SIGN parameter in data transfer statement");
2066 if (dtp->u.p.sign_status == SIGN_UNSPECIFIED)
2067 dtp->u.p.sign_status = dtp->u.p.current_unit->flags.sign;
2069 /* Check the blank mode. */
2070 dtp->u.p.blank_status
2071 = !(cf & IOPARM_DT_HAS_BLANK) ? BLANK_UNSPECIFIED :
2072 find_option (&dtp->common, dtp->blank, dtp->blank_len, blank_opt,
2073 "Bad BLANK parameter in data transfer statement");
2075 if (dtp->u.p.blank_status == BLANK_UNSPECIFIED)
2076 dtp->u.p.blank_status = dtp->u.p.current_unit->flags.blank;
2078 /* Check the delim mode. */
2079 dtp->u.p.delim_status
2080 = !(cf & IOPARM_DT_HAS_DELIM) ? DELIM_UNSPECIFIED :
2081 find_option (&dtp->common, dtp->delim, dtp->delim_len, delim_opt,
2082 "Bad DELIM parameter in data transfer statement");
2084 if (dtp->u.p.delim_status == DELIM_UNSPECIFIED)
2085 dtp->u.p.delim_status = dtp->u.p.current_unit->flags.delim;
2087 /* Check the pad mode. */
2089 = !(cf & IOPARM_DT_HAS_PAD) ? PAD_UNSPECIFIED :
2090 find_option (&dtp->common, dtp->pad, dtp->pad_len, pad_opt,
2091 "Bad PAD parameter in data transfer statement");
2093 if (dtp->u.p.pad_status == PAD_UNSPECIFIED)
2094 dtp->u.p.pad_status = dtp->u.p.current_unit->flags.pad;
2096 /* Sanity checks on the record number. */
2097 if ((cf & IOPARM_DT_HAS_REC) != 0)
2101 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2102 "Record number must be positive");
2106 if (dtp->rec >= dtp->u.p.current_unit->maxrec)
2108 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2109 "Record number too large");
2113 /* Check to see if we might be reading what we wrote before */
2115 if (dtp->u.p.mode == READING
2116 && dtp->u.p.current_unit->mode == WRITING
2117 && !is_internal_unit (dtp))
2119 fbuf_flush (dtp->u.p.current_unit, 1);
2120 flush(dtp->u.p.current_unit->s);
2123 /* Check whether the record exists to be read. Only
2124 a partial record needs to exist. */
2126 if (dtp->u.p.mode == READING && (dtp->rec - 1)
2127 * dtp->u.p.current_unit->recl >= file_length (dtp->u.p.current_unit->s))
2129 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2130 "Non-existing record number");
2134 /* Position the file. */
2135 if (!is_stream_io (dtp))
2137 if (sseek (dtp->u.p.current_unit->s, (gfc_offset) (dtp->rec - 1)
2138 * dtp->u.p.current_unit->recl) == FAILURE)
2140 generate_error (&dtp->common, LIBERROR_OS, NULL);
2146 if (dtp->u.p.current_unit->strm_pos != dtp->rec)
2148 fbuf_flush (dtp->u.p.current_unit, 1);
2149 flush (dtp->u.p.current_unit->s);
2150 if (sseek (dtp->u.p.current_unit->s, dtp->rec - 1) == FAILURE)
2152 generate_error (&dtp->common, LIBERROR_OS, NULL);
2155 dtp->u.p.current_unit->strm_pos = dtp->rec;
2161 /* Overwriting an existing sequential file ?
2162 it is always safe to truncate the file on the first write */
2163 if (dtp->u.p.mode == WRITING
2164 && dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL
2165 && dtp->u.p.current_unit->last_record == 0
2166 && !is_preconnected(dtp->u.p.current_unit->s))
2167 struncate(dtp->u.p.current_unit->s);
2169 /* Bugware for badly written mixed C-Fortran I/O. */
2170 flush_if_preconnected(dtp->u.p.current_unit->s);
2172 dtp->u.p.current_unit->mode = dtp->u.p.mode;
2174 /* Set the maximum position reached from the previous I/O operation. This
2175 could be greater than zero from a previous non-advancing write. */
2176 dtp->u.p.max_pos = dtp->u.p.current_unit->saved_pos;
2181 /* Set up the subroutine that will handle the transfers. */
2185 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
2186 dtp->u.p.transfer = unformatted_read;
2189 if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
2190 dtp->u.p.transfer = list_formatted_read;
2192 dtp->u.p.transfer = formatted_transfer;
2197 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
2198 dtp->u.p.transfer = unformatted_write;
2201 if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
2202 dtp->u.p.transfer = list_formatted_write;
2204 dtp->u.p.transfer = formatted_transfer;
2208 /* Make sure that we don't do a read after a nonadvancing write. */
2212 if (dtp->u.p.current_unit->read_bad && !is_stream_io (dtp))
2214 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2215 "Cannot READ after a nonadvancing WRITE");
2221 if (dtp->u.p.advance_status == ADVANCE_YES && !dtp->u.p.seen_dollar)
2222 dtp->u.p.current_unit->read_bad = 1;
2225 /* Start the data transfer if we are doing a formatted transfer. */
2226 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
2227 && ((cf & (IOPARM_DT_LIST_FORMAT | IOPARM_DT_HAS_NAMELIST_NAME)) == 0)
2228 && dtp->u.p.ionml == NULL)
2229 formatted_transfer (dtp, 0, NULL, 0, 0, 1);
2232 /* Initialize an array_loop_spec given the array descriptor. The function
2233 returns the index of the last element of the array, and also returns
2234 starting record, where the first I/O goes to (necessary in case of
2235 negative strides). */
2238 init_loop_spec (gfc_array_char *desc, array_loop_spec *ls,
2239 gfc_offset *start_record)
2241 int rank = GFC_DESCRIPTOR_RANK(desc);
2250 for (i=0; i<rank; i++)
2252 ls[i].idx = desc->dim[i].lbound;
2253 ls[i].start = desc->dim[i].lbound;
2254 ls[i].end = desc->dim[i].ubound;
2255 ls[i].step = desc->dim[i].stride;
2256 empty = empty || (desc->dim[i].ubound < desc->dim[i].lbound);
2258 if (desc->dim[i].stride > 0)
2260 index += (desc->dim[i].ubound - desc->dim[i].lbound)
2261 * desc->dim[i].stride;
2265 index -= (desc->dim[i].ubound - desc->dim[i].lbound)
2266 * desc->dim[i].stride;
2267 *start_record -= (desc->dim[i].ubound - desc->dim[i].lbound)
2268 * desc->dim[i].stride;
2278 /* Determine the index to the next record in an internal unit array by
2279 by incrementing through the array_loop_spec. */
2282 next_array_record (st_parameter_dt *dtp, array_loop_spec *ls, int *finished)
2290 for (i = 0; i < dtp->u.p.current_unit->rank; i++)
2295 if (ls[i].idx > ls[i].end)
2297 ls[i].idx = ls[i].start;
2303 index = index + (ls[i].idx - ls[i].start) * ls[i].step;
2313 /* Skip to the end of the current record, taking care of an optional
2314 record marker of size bytes. If the file is not seekable, we
2315 read chunks of size MAX_READ until we get to the right
2319 skip_record (st_parameter_dt *dtp, size_t bytes)
2323 static const size_t MAX_READ = 4096;
2326 dtp->u.p.current_unit->bytes_left_subrecord += bytes;
2327 if (dtp->u.p.current_unit->bytes_left_subrecord == 0)
2330 if (is_seekable (dtp->u.p.current_unit->s))
2332 new = file_position (dtp->u.p.current_unit->s)
2333 + dtp->u.p.current_unit->bytes_left_subrecord;
2335 /* Direct access files do not generate END conditions,
2337 if (sseek (dtp->u.p.current_unit->s, new) == FAILURE)
2338 generate_error (&dtp->common, LIBERROR_OS, NULL);
2341 { /* Seek by reading data. */
2342 while (dtp->u.p.current_unit->bytes_left_subrecord > 0)
2345 (MAX_READ > (size_t) dtp->u.p.current_unit->bytes_left_subrecord) ?
2346 MAX_READ : (size_t) dtp->u.p.current_unit->bytes_left_subrecord;
2348 if (sread (dtp->u.p.current_unit->s, p, &rlength) != 0)
2350 generate_error (&dtp->common, LIBERROR_OS, NULL);
2354 dtp->u.p.current_unit->bytes_left_subrecord -= rlength;
2361 /* Advance to the next record reading unformatted files, taking
2362 care of subrecords. If complete_record is nonzero, we loop
2363 until all subrecords are cleared. */
2366 next_record_r_unf (st_parameter_dt *dtp, int complete_record)
2370 bytes = compile_options.record_marker == 0 ?
2371 sizeof (GFC_INTEGER_4) : compile_options.record_marker;
2376 /* Skip over tail */
2378 skip_record (dtp, bytes);
2380 if ( ! (complete_record && dtp->u.p.current_unit->continued))
2388 static inline gfc_offset
2389 min_off (gfc_offset a, gfc_offset b)
2391 return (a < b ? a : b);
2395 /* Space to the next record for read mode. */
2398 next_record_r (st_parameter_dt *dtp)
2405 switch (current_mode (dtp))
2407 /* No records in unformatted STREAM I/O. */
2408 case UNFORMATTED_STREAM:
2411 case UNFORMATTED_SEQUENTIAL:
2412 next_record_r_unf (dtp, 1);
2413 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2416 case FORMATTED_DIRECT:
2417 case UNFORMATTED_DIRECT:
2418 skip_record (dtp, 0);
2421 case FORMATTED_STREAM:
2422 case FORMATTED_SEQUENTIAL:
2424 /* sf_read has already terminated input because of an '\n' */
2425 if (dtp->u.p.sf_seen_eor)
2427 dtp->u.p.sf_seen_eor = 0;
2431 if (is_internal_unit (dtp))
2433 if (is_array_io (dtp))
2437 record = next_array_record (dtp, dtp->u.p.current_unit->ls,
2440 /* Now seek to this record. */
2441 record = record * dtp->u.p.current_unit->recl;
2442 if (sseek (dtp->u.p.current_unit->s, record) == FAILURE)
2444 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
2447 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2451 bytes_left = (int) dtp->u.p.current_unit->bytes_left;
2452 bytes_left = min_off (bytes_left,
2453 file_length (dtp->u.p.current_unit->s)
2454 - file_position (dtp->u.p.current_unit->s));
2455 if (sseek (dtp->u.p.current_unit->s,
2456 file_position (dtp->u.p.current_unit->s)
2457 + bytes_left) == FAILURE)
2459 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
2462 dtp->u.p.current_unit->bytes_left
2463 = dtp->u.p.current_unit->recl;
2469 if (sread (dtp->u.p.current_unit->s, &p, &length) != 0)
2471 generate_error (&dtp->common, LIBERROR_OS, NULL);
2477 dtp->u.p.current_unit->endfile = AT_ENDFILE;
2481 if (is_stream_io (dtp))
2482 dtp->u.p.current_unit->strm_pos++;
2489 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL
2490 && !dtp->u.p.namelist_mode
2491 && dtp->u.p.current_unit->endfile == NO_ENDFILE
2492 && (file_length (dtp->u.p.current_unit->s) ==
2493 file_position (dtp->u.p.current_unit->s)))
2494 dtp->u.p.current_unit->endfile = AT_ENDFILE;
2499 /* Small utility function to write a record marker, taking care of
2500 byte swapping and of choosing the correct size. */
2503 write_us_marker (st_parameter_dt *dtp, const gfc_offset buf)
2508 char p[sizeof (GFC_INTEGER_8)];
2510 if (compile_options.record_marker == 0)
2511 len = sizeof (GFC_INTEGER_4);
2513 len = compile_options.record_marker;
2515 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
2516 if (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE)
2520 case sizeof (GFC_INTEGER_4):
2522 return swrite (dtp->u.p.current_unit->s, &buf4, &len);
2525 case sizeof (GFC_INTEGER_8):
2527 return swrite (dtp->u.p.current_unit->s, &buf8, &len);
2531 runtime_error ("Illegal value for record marker");
2539 case sizeof (GFC_INTEGER_4):
2541 reverse_memcpy (p, &buf4, sizeof (GFC_INTEGER_4));
2542 return swrite (dtp->u.p.current_unit->s, p, &len);
2545 case sizeof (GFC_INTEGER_8):
2547 reverse_memcpy (p, &buf8, sizeof (GFC_INTEGER_8));
2548 return swrite (dtp->u.p.current_unit->s, p, &len);
2552 runtime_error ("Illegal value for record marker");
2559 /* Position to the next (sub)record in write mode for
2560 unformatted sequential files. */
2563 next_record_w_unf (st_parameter_dt *dtp, int next_subrecord)
2565 gfc_offset c, m, m_write;
2566 size_t record_marker;
2568 /* Bytes written. */
2569 m = dtp->u.p.current_unit->recl_subrecord
2570 - dtp->u.p.current_unit->bytes_left_subrecord;
2571 c = file_position (dtp->u.p.current_unit->s);
2573 /* Write the length tail. If we finish a record containing
2574 subrecords, we write out the negative length. */
2576 if (dtp->u.p.current_unit->continued)
2581 if (write_us_marker (dtp, m_write) != 0)
2584 if (compile_options.record_marker == 0)
2585 record_marker = sizeof (GFC_INTEGER_4);
2587 record_marker = compile_options.record_marker;
2589 /* Seek to the head and overwrite the bogus length with the real
2592 if (sseek (dtp->u.p.current_unit->s, c - m - record_marker)
2601 if (write_us_marker (dtp, m_write) != 0)
2604 /* Seek past the end of the current record. */
2606 if (sseek (dtp->u.p.current_unit->s, c + record_marker) == FAILURE)
2612 generate_error (&dtp->common, LIBERROR_OS, NULL);
2617 /* Position to the next record in write mode. */
2620 next_record_w (st_parameter_dt *dtp, int done)
2622 gfc_offset m, record, max_pos;
2625 /* Flush and reset the format buffer. */
2626 fbuf_flush (dtp->u.p.current_unit, 1);
2628 /* Zero counters for X- and T-editing. */
2629 max_pos = dtp->u.p.max_pos;
2630 dtp->u.p.max_pos = dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
2632 switch (current_mode (dtp))
2634 /* No records in unformatted STREAM I/O. */
2635 case UNFORMATTED_STREAM:
2638 case FORMATTED_DIRECT:
2639 if (dtp->u.p.current_unit->bytes_left == 0)
2642 if (sset (dtp->u.p.current_unit->s, ' ',
2643 dtp->u.p.current_unit->bytes_left) == FAILURE)
2648 case UNFORMATTED_DIRECT:
2649 if (dtp->u.p.current_unit->bytes_left > 0)
2651 length = (int) dtp->u.p.current_unit->bytes_left;
2652 if (sset (dtp->u.p.current_unit->s, 0, length) == FAILURE)
2657 case UNFORMATTED_SEQUENTIAL:
2658 next_record_w_unf (dtp, 0);
2659 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2662 case FORMATTED_STREAM:
2663 case FORMATTED_SEQUENTIAL:
2665 if (is_internal_unit (dtp))
2667 if (is_array_io (dtp))
2671 length = (int) dtp->u.p.current_unit->bytes_left;
2673 /* If the farthest position reached is greater than current
2674 position, adjust the position and set length to pad out
2675 whats left. Otherwise just pad whats left.
2676 (for character array unit) */
2677 m = dtp->u.p.current_unit->recl
2678 - dtp->u.p.current_unit->bytes_left;
2681 length = (int) (max_pos - m);
2682 if (sseek (dtp->u.p.current_unit->s,
2683 file_position (dtp->u.p.current_unit->s)
2684 + length) == FAILURE)
2686 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
2689 length = (int) (dtp->u.p.current_unit->recl - max_pos);
2692 if (sset (dtp->u.p.current_unit->s, ' ', length) == FAILURE)
2694 generate_error (&dtp->common, LIBERROR_END, NULL);
2698 /* Now that the current record has been padded out,
2699 determine where the next record in the array is. */
2700 record = next_array_record (dtp, dtp->u.p.current_unit->ls,
2703 dtp->u.p.current_unit->endfile = AT_ENDFILE;
2705 /* Now seek to this record */
2706 record = record * dtp->u.p.current_unit->recl;
2708 if (sseek (dtp->u.p.current_unit->s, record) == FAILURE)
2710 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
2714 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2720 /* If this is the last call to next_record move to the farthest
2721 position reached and set length to pad out the remainder
2722 of the record. (for character scaler unit) */
2725 m = dtp->u.p.current_unit->recl
2726 - dtp->u.p.current_unit->bytes_left;
2729 length = (int) (max_pos - m);
2730 if (sseek (dtp->u.p.current_unit->s,
2731 file_position (dtp->u.p.current_unit->s)
2732 + length) == FAILURE)
2734 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
2737 length = (int) (dtp->u.p.current_unit->recl - max_pos);
2740 length = (int) dtp->u.p.current_unit->bytes_left;
2743 if (sset (dtp->u.p.current_unit->s, ' ', length) == FAILURE)
2745 generate_error (&dtp->common, LIBERROR_END, NULL);
2753 const char crlf[] = "\r\n";
2760 if (swrite (dtp->u.p.current_unit->s, &crlf[2-len], &len) != 0)
2763 if (is_stream_io (dtp))
2765 dtp->u.p.current_unit->strm_pos += len;
2766 if (dtp->u.p.current_unit->strm_pos
2767 < file_length (dtp->u.p.current_unit->s))
2768 struncate (dtp->u.p.current_unit->s);
2775 generate_error (&dtp->common, LIBERROR_OS, NULL);
2780 /* Position to the next record, which means moving to the end of the
2781 current record. This can happen under several different
2782 conditions. If the done flag is not set, we get ready to process
2786 next_record (st_parameter_dt *dtp, int done)
2788 gfc_offset fp; /* File position. */
2790 dtp->u.p.current_unit->read_bad = 0;
2792 if (dtp->u.p.mode == READING)
2793 next_record_r (dtp);
2795 next_record_w (dtp, done);
2797 if (!is_stream_io (dtp))
2799 /* Keep position up to date for INQUIRE */
2801 update_position (dtp->u.p.current_unit);
2803 dtp->u.p.current_unit->current_record = 0;
2804 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
2806 fp = file_position (dtp->u.p.current_unit->s);
2807 /* Calculate next record, rounding up partial records. */
2808 dtp->u.p.current_unit->last_record =
2809 (fp + dtp->u.p.current_unit->recl - 1) /
2810 dtp->u.p.current_unit->recl;
2813 dtp->u.p.current_unit->last_record++;
2821 /* Finalize the current data transfer. For a nonadvancing transfer,
2822 this means advancing to the next record. For internal units close the
2823 stream associated with the unit. */
2826 finalize_transfer (st_parameter_dt *dtp)
2829 GFC_INTEGER_4 cf = dtp->common.flags;
2831 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
2832 *dtp->size = (GFC_IO_INT) dtp->u.p.size_used;
2834 if (dtp->u.p.eor_condition)
2836 generate_error (&dtp->common, LIBERROR_EOR, NULL);
2840 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2843 if ((dtp->u.p.ionml != NULL)
2844 && (cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0)
2846 if ((cf & IOPARM_DT_NAMELIST_READ_MODE) != 0)
2847 namelist_read (dtp);
2849 namelist_write (dtp);
2852 dtp->u.p.transfer = NULL;
2853 if (dtp->u.p.current_unit == NULL)
2856 dtp->u.p.eof_jump = &eof_jump;
2857 if (setjmp (eof_jump))
2859 generate_error (&dtp->common, LIBERROR_END, NULL);
2863 if ((cf & IOPARM_DT_LIST_FORMAT) != 0 && dtp->u.p.mode == READING)
2865 finish_list_read (dtp);
2866 sfree (dtp->u.p.current_unit->s);
2870 if (dtp->u.p.mode == WRITING)
2871 dtp->u.p.current_unit->previous_nonadvancing_write
2872 = dtp->u.p.advance_status == ADVANCE_NO;
2874 if (is_stream_io (dtp))
2876 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
2877 && dtp->u.p.advance_status != ADVANCE_NO)
2878 next_record (dtp, 1);
2880 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED
2881 && file_position (dtp->u.p.current_unit->s) >= dtp->rec)
2883 flush (dtp->u.p.current_unit->s);
2884 sfree (dtp->u.p.current_unit->s);
2889 dtp->u.p.current_unit->current_record = 0;
2891 if (!is_internal_unit (dtp) && dtp->u.p.seen_dollar)
2893 dtp->u.p.seen_dollar = 0;
2894 fbuf_flush (dtp->u.p.current_unit, 1);
2895 sfree (dtp->u.p.current_unit->s);
2899 /* For non-advancing I/O, save the current maximum position for use in the
2900 next I/O operation if needed. */
2901 if (dtp->u.p.advance_status == ADVANCE_NO)
2903 int bytes_written = (int) (dtp->u.p.current_unit->recl
2904 - dtp->u.p.current_unit->bytes_left);
2905 dtp->u.p.current_unit->saved_pos =
2906 dtp->u.p.max_pos > 0 ? dtp->u.p.max_pos - bytes_written : 0;
2907 fbuf_flush (dtp->u.p.current_unit, 0);
2908 flush (dtp->u.p.current_unit->s);
2912 dtp->u.p.current_unit->saved_pos = 0;
2914 next_record (dtp, 1);
2915 sfree (dtp->u.p.current_unit->s);
2918 /* Transfer function for IOLENGTH. It doesn't actually do any
2919 data transfer, it just updates the length counter. */
2922 iolength_transfer (st_parameter_dt *dtp, bt type __attribute__((unused)),
2923 void *dest __attribute__ ((unused)),
2924 int kind __attribute__((unused)),
2925 size_t size, size_t nelems)
2927 if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
2928 *dtp->iolength += (GFC_IO_INT) size * nelems;
2932 /* Initialize the IOLENGTH data transfer. This function is in essence
2933 a very much simplified version of data_transfer_init(), because it
2934 doesn't have to deal with units at all. */
2937 iolength_transfer_init (st_parameter_dt *dtp)
2939 if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
2942 memset (&dtp->u.p, 0, sizeof (dtp->u.p));
2944 /* Set up the subroutine that will handle the transfers. */
2946 dtp->u.p.transfer = iolength_transfer;
2950 /* Library entry point for the IOLENGTH form of the INQUIRE
2951 statement. The IOLENGTH form requires no I/O to be performed, but
2952 it must still be a runtime library call so that we can determine
2953 the iolength for dynamic arrays and such. */
2955 extern void st_iolength (st_parameter_dt *);
2956 export_proto(st_iolength);
2959 st_iolength (st_parameter_dt *dtp)
2961 library_start (&dtp->common);
2962 iolength_transfer_init (dtp);
2965 extern void st_iolength_done (st_parameter_dt *);
2966 export_proto(st_iolength_done);
2969 st_iolength_done (st_parameter_dt *dtp __attribute__((unused)))
2972 if (dtp->u.p.scratch != NULL)
2973 free_mem (dtp->u.p.scratch);
2978 /* The READ statement. */
2980 extern void st_read (st_parameter_dt *);
2981 export_proto(st_read);
2984 st_read (st_parameter_dt *dtp)
2986 library_start (&dtp->common);
2988 data_transfer_init (dtp, 1);
2990 /* Handle complications dealing with the endfile record. */
2992 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
2993 switch (dtp->u.p.current_unit->endfile)
2999 if (!is_internal_unit (dtp))
3001 generate_error (&dtp->common, LIBERROR_END, NULL);
3002 dtp->u.p.current_unit->endfile = AFTER_ENDFILE;
3003 dtp->u.p.current_unit->current_record = 0;
3008 generate_error (&dtp->common, LIBERROR_ENDFILE, NULL);
3009 dtp->u.p.current_unit->current_record = 0;
3014 extern void st_read_done (st_parameter_dt *);
3015 export_proto(st_read_done);
3018 st_read_done (st_parameter_dt *dtp)
3020 finalize_transfer (dtp);
3021 free_format_data (dtp);
3023 if (dtp->u.p.scratch != NULL)
3024 free_mem (dtp->u.p.scratch);
3025 if (dtp->u.p.current_unit != NULL)
3026 unlock_unit (dtp->u.p.current_unit);
3028 free_internal_unit (dtp);
3033 extern void st_write (st_parameter_dt *);
3034 export_proto(st_write);
3037 st_write (st_parameter_dt *dtp)
3039 library_start (&dtp->common);
3040 data_transfer_init (dtp, 0);
3043 extern void st_write_done (st_parameter_dt *);
3044 export_proto(st_write_done);
3047 st_write_done (st_parameter_dt *dtp)
3049 finalize_transfer (dtp);
3051 /* Deal with endfile conditions associated with sequential files. */
3053 if (dtp->u.p.current_unit != NULL
3054 && dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
3055 switch (dtp->u.p.current_unit->endfile)
3057 case AT_ENDFILE: /* Remain at the endfile record. */
3061 dtp->u.p.current_unit->endfile = AT_ENDFILE; /* Just at it now. */
3065 /* Get rid of whatever is after this record. */
3066 if (!is_internal_unit (dtp))
3068 flush (dtp->u.p.current_unit->s);
3069 if (struncate (dtp->u.p.current_unit->s) == FAILURE)
3070 generate_error (&dtp->common, LIBERROR_OS, NULL);
3072 dtp->u.p.current_unit->endfile = AT_ENDFILE;
3076 free_format_data (dtp);
3078 if (dtp->u.p.scratch != NULL)
3079 free_mem (dtp->u.p.scratch);
3080 if (dtp->u.p.current_unit != NULL)
3081 unlock_unit (dtp->u.p.current_unit);
3083 free_internal_unit (dtp);
3089 /* F2003: This is a stub for the runtime portion of the WAIT statement. */
3091 st_wait (st_parameter_wait *wtp __attribute__((unused)))
3096 /* Receives the scalar information for namelist objects and stores it
3097 in a linked list of namelist_info types. */
3099 extern void st_set_nml_var (st_parameter_dt *dtp, void *, char *,
3100 GFC_INTEGER_4, gfc_charlen_type, GFC_INTEGER_4);
3101 export_proto(st_set_nml_var);
3105 st_set_nml_var (st_parameter_dt *dtp, void * var_addr, char * var_name,
3106 GFC_INTEGER_4 len, gfc_charlen_type string_length,
3107 GFC_INTEGER_4 dtype)
3109 namelist_info *t1 = NULL;
3111 size_t var_name_len = strlen (var_name);
3113 nml = (namelist_info*) get_mem (sizeof (namelist_info));
3115 nml->mem_pos = var_addr;
3117 nml->var_name = (char*) get_mem (var_name_len + 1);
3118 memcpy (nml->var_name, var_name, var_name_len);
3119 nml->var_name[var_name_len] = '\0';
3121 nml->len = (int) len;
3122 nml->string_length = (index_type) string_length;
3124 nml->var_rank = (int) (dtype & GFC_DTYPE_RANK_MASK);
3125 nml->size = (index_type) (dtype >> GFC_DTYPE_SIZE_SHIFT);
3126 nml->type = (bt) ((dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT);
3128 if (nml->var_rank > 0)
3130 nml->dim = (descriptor_dimension*)
3131 get_mem (nml->var_rank * sizeof (descriptor_dimension));
3132 nml->ls = (array_loop_spec*)
3133 get_mem (nml->var_rank * sizeof (array_loop_spec));
3143 if ((dtp->common.flags & IOPARM_DT_IONML_SET) == 0)
3145 dtp->common.flags |= IOPARM_DT_IONML_SET;
3146 dtp->u.p.ionml = nml;
3150 for (t1 = dtp->u.p.ionml; t1->next; t1 = t1->next);
3155 /* Store the dimensional information for the namelist object. */
3156 extern void st_set_nml_var_dim (st_parameter_dt *, GFC_INTEGER_4,
3157 index_type, index_type,
3159 export_proto(st_set_nml_var_dim);
3162 st_set_nml_var_dim (st_parameter_dt *dtp, GFC_INTEGER_4 n_dim,
3163 index_type stride, index_type lbound,
3166 namelist_info * nml;
3171 for (nml = dtp->u.p.ionml; nml->next; nml = nml->next);
3173 nml->dim[n].stride = stride;
3174 nml->dim[n].lbound = lbound;
3175 nml->dim[n].ubound = ubound;
3178 /* Reverse memcpy - used for byte swapping. */
3180 void reverse_memcpy (void *dest, const void *src, size_t n)
3186 s = (char *) src + n - 1;
3188 /* Write with ascending order - this is likely faster
3189 on modern architectures because of write combining. */