1 /* Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
3 Namelist transfer functions contributed by Paul Thomas
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with Libgfortran; see the file COPYING. If not, write to
28 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA. */
32 /* transfer.c -- Top level handling of data transfer statements. */
37 #include "libgfortran.h"
41 /* Calling conventions: Data transfer statements are unlike other
42 library calls in that they extend over several calls.
44 The first call is always a call to st_read() or st_write(). These
45 subroutines return no status unless a namelist read or write is
46 being done, in which case there is the usual status. No further
47 calls are necessary in this case.
49 For other sorts of data transfer, there are zero or more data
50 transfer statement that depend on the format of the data transfer
59 These subroutines do not return status.
61 The last call is a call to st_[read|write]_done(). While
62 something can easily go wrong with the initial st_read() or
63 st_write(), an error inhibits any data from actually being
66 extern void transfer_integer (st_parameter_dt *, void *, int);
67 export_proto(transfer_integer);
69 extern void transfer_real (st_parameter_dt *, void *, int);
70 export_proto(transfer_real);
72 extern void transfer_logical (st_parameter_dt *, void *, int);
73 export_proto(transfer_logical);
75 extern void transfer_character (st_parameter_dt *, void *, int);
76 export_proto(transfer_character);
78 extern void transfer_complex (st_parameter_dt *, void *, int);
79 export_proto(transfer_complex);
81 extern void transfer_array (st_parameter_dt *, gfc_array_char *, int,
83 export_proto(transfer_array);
85 static const st_option advance_opt[] = {
93 { FORMATTED_SEQUENTIAL, UNFORMATTED_SEQUENTIAL,
94 FORMATTED_DIRECT, UNFORMATTED_DIRECT
100 current_mode (st_parameter_dt *dtp)
104 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
106 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
107 FORMATTED_DIRECT : UNFORMATTED_DIRECT;
111 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
112 FORMATTED_SEQUENTIAL : UNFORMATTED_SEQUENTIAL;
119 /* Mid level data transfer statements. These subroutines do reading
120 and writing in the style of salloc_r()/salloc_w() within the
123 /* When reading sequential formatted records we have a problem. We
124 don't know how long the line is until we read the trailing newline,
125 and we don't want to read too much. If we read too much, we might
126 have to do a physical seek backwards depending on how much data is
127 present, and devices like terminals aren't seekable and would cause
130 Given this, the solution is to read a byte at a time, stopping if
131 we hit the newline. For small locations, we use a static buffer.
132 For larger allocations, we are forced to allocate memory on the
133 heap. Hopefully this won't happen very often. */
136 read_sf (st_parameter_dt *dtp, int *length)
141 if (*length > SCRATCH_SIZE)
142 dtp->u.p.line_buffer = get_mem (*length);
143 p = base = dtp->u.p.line_buffer;
145 /* If we have seen an eor previously, return a length of 0. The
146 caller is responsible for correctly padding the input field. */
147 if (dtp->u.p.sf_seen_eor)
158 if (is_internal_unit (dtp))
160 /* readlen may be modified inside salloc_r if
161 is_internal_unit (dtp) is true. */
165 q = salloc_r (dtp->u.p.current_unit->s, &readlen);
169 /* If we have a line without a terminating \n, drop through to
171 if (readlen < 1 && n == 0)
173 generate_error (&dtp->common, ERROR_END, NULL);
177 if (readlen < 1 || *q == '\n' || *q == '\r')
179 /* Unexpected end of line. */
181 /* If we see an EOR during non-advancing I/O, we need to skip
182 the rest of the I/O statement. Set the corresponding flag. */
183 if (dtp->u.p.advance_status == ADVANCE_NO || dtp->u.p.seen_dollar)
184 dtp->u.p.eor_condition = 1;
186 /* Without padding, terminate the I/O statement without assigning
187 the value. With padding, the value still needs to be assigned,
188 so we can just continue with a short read. */
189 if (dtp->u.p.current_unit->flags.pad == PAD_NO)
191 generate_error (&dtp->common, ERROR_EOR, NULL);
196 dtp->u.p.sf_seen_eor = 1;
202 dtp->u.p.sf_seen_eor = 0;
205 dtp->u.p.current_unit->bytes_left -= *length;
207 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
208 *dtp->size += *length;
214 /* Function for reading the next couple of bytes from the current
215 file, advancing the current position. We return a pointer to a
216 buffer containing the bytes. We return NULL on end of record or
219 If the read is short, then it is because the current record does not
220 have enough data to satisfy the read request and the file was
221 opened with PAD=YES. The caller must assume tailing spaces for
225 read_block (st_parameter_dt *dtp, int *length)
230 if (dtp->u.p.current_unit->bytes_left < *length)
232 if (dtp->u.p.current_unit->flags.pad == PAD_NO)
234 generate_error (&dtp->common, ERROR_EOR, NULL);
235 /* Not enough data left. */
239 *length = dtp->u.p.current_unit->bytes_left;
242 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
243 dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
244 return read_sf (dtp, length); /* Special case. */
246 dtp->u.p.current_unit->bytes_left -= *length;
249 source = salloc_r (dtp->u.p.current_unit->s, &nread);
251 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
254 if (nread != *length)
255 { /* Short read, this shouldn't happen. */
256 if (dtp->u.p.current_unit->flags.pad == PAD_YES)
260 generate_error (&dtp->common, ERROR_EOR, NULL);
269 /* Reads a block directly into application data space. */
272 read_block_direct (st_parameter_dt *dtp, void *buf, size_t *nbytes)
278 if (dtp->u.p.current_unit->bytes_left < *nbytes)
280 if (dtp->u.p.current_unit->flags.pad == PAD_NO)
282 /* Not enough data left. */
283 generate_error (&dtp->common, ERROR_EOR, NULL);
287 *nbytes = dtp->u.p.current_unit->bytes_left;
290 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
291 dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
293 length = (int *) nbytes;
294 data = read_sf (dtp, length); /* Special case. */
295 memcpy (buf, data, (size_t) *length);
299 dtp->u.p.current_unit->bytes_left -= *nbytes;
302 if (sread (dtp->u.p.current_unit->s, buf, &nread) != 0)
304 generate_error (&dtp->common, ERROR_OS, NULL);
308 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
309 *dtp->size += (GFC_INTEGER_4) nread;
311 if (nread != *nbytes)
312 { /* Short read, e.g. if we hit EOF. */
313 if (dtp->u.p.current_unit->flags.pad == PAD_YES)
315 memset (((char *) buf) + nread, ' ', *nbytes - nread);
319 generate_error (&dtp->common, ERROR_EOR, NULL);
324 /* Function for writing a block of bytes to the current file at the
325 current position, advancing the file pointer. We are given a length
326 and return a pointer to a buffer that the caller must (completely)
327 fill in. Returns NULL on error. */
330 write_block (st_parameter_dt *dtp, int length)
334 if (dtp->u.p.current_unit->bytes_left < length)
336 generate_error (&dtp->common, ERROR_EOR, NULL);
340 dtp->u.p.current_unit->bytes_left -= (gfc_offset) length;
341 dest = salloc_w (dtp->u.p.current_unit->s, &length);
345 generate_error (&dtp->common, ERROR_END, NULL);
349 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
350 *dtp->size += length;
356 /* Writes a block directly without necessarily allocating space in a
360 write_block_direct (st_parameter_dt *dtp, void *buf, size_t *nbytes)
362 if (dtp->u.p.current_unit->bytes_left < *nbytes)
363 generate_error (&dtp->common, ERROR_EOR, NULL);
365 dtp->u.p.current_unit->bytes_left -= (gfc_offset) *nbytes;
367 if (swrite (dtp->u.p.current_unit->s, buf, nbytes) != 0)
368 generate_error (&dtp->common, ERROR_OS, NULL);
370 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
371 *dtp->size += (GFC_INTEGER_4) *nbytes;
375 /* Master function for unformatted reads. */
378 unformatted_read (st_parameter_dt *dtp, bt type __attribute__((unused)),
379 void *dest, int kind __attribute__((unused)),
380 size_t size, size_t nelems)
384 read_block_direct (dtp, dest, &size);
388 /* Master function for unformatted writes. */
391 unformatted_write (st_parameter_dt *dtp, bt type __attribute__((unused)),
392 void *source, int kind __attribute__((unused)),
393 size_t size, size_t nelems)
397 write_block_direct (dtp, source, &size);
401 /* Return a pointer to the name of a type. */
426 internal_error (NULL, "type_name(): Bad type");
433 /* Write a constant string to the output.
434 This is complicated because the string can have doubled delimiters
435 in it. The length in the format node is the true length. */
438 write_constant_string (st_parameter_dt *dtp, const fnode *f)
440 char c, delimiter, *p, *q;
443 length = f->u.string.length;
447 p = write_block (dtp, length);
454 for (; length > 0; length--)
457 if (c == delimiter && c != 'H' && c != 'h')
458 q++; /* Skip the doubled delimiter. */
463 /* Given actual and expected types in a formatted data transfer, make
464 sure they agree. If not, an error message is generated. Returns
465 nonzero if something went wrong. */
468 require_type (st_parameter_dt *dtp, bt expected, bt actual, const fnode *f)
472 if (actual == expected)
475 st_sprintf (buffer, "Expected %s for item %d in formatted transfer, got %s",
476 type_name (expected), dtp->u.p.item_count, type_name (actual));
478 format_error (dtp, f, buffer);
483 /* This subroutine is the main loop for a formatted data transfer
484 statement. It would be natural to implement this as a coroutine
485 with the user program, but C makes that awkward. We loop,
486 processesing format elements. When we actually have to transfer
487 data instead of just setting flags, we return control to the user
488 program which calls a subroutine that supplies the address and type
489 of the next element, then comes back here to process it. */
492 formatted_transfer_scalar (st_parameter_dt *dtp, bt type, void *p, int len,
495 char scratch[SCRATCH_SIZE];
500 int consume_data_flag;
502 /* Change a complex data item into a pair of reals. */
504 n = (p == NULL) ? 0 : ((type != BT_COMPLEX) ? 1 : 2);
505 if (type == BT_COMPLEX)
511 /* If there's an EOR condition, we simulate finalizing the transfer
513 if (dtp->u.p.eor_condition)
516 dtp->u.p.line_buffer = scratch;
519 /* If reversion has occurred and there is another real data item,
520 then we have to move to the next record. */
521 if (dtp->u.p.reversion_flag && n > 0)
523 dtp->u.p.reversion_flag = 0;
524 next_record (dtp, 0);
527 consume_data_flag = 1 ;
528 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
531 f = next_format (dtp);
533 return; /* No data descriptors left (already raised). */
535 /* Now discharge T, TR and X movements to the right. This is delayed
536 until a data producing format to suppress trailing spaces. */
539 if (dtp->u.p.mode == WRITING && dtp->u.p.skips != 0
540 && ((n>0 && ( t == FMT_I || t == FMT_B || t == FMT_O
541 || t == FMT_Z || t == FMT_F || t == FMT_E
542 || t == FMT_EN || t == FMT_ES || t == FMT_G
543 || t == FMT_L || t == FMT_A || t == FMT_D))
546 if (dtp->u.p.skips > 0)
548 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
549 dtp->u.p.max_pos = (int)(dtp->u.p.current_unit->recl
550 - dtp->u.p.current_unit->bytes_left);
552 if (dtp->u.p.skips < 0)
554 move_pos_offset (dtp->u.p.current_unit->s, dtp->u.p.skips);
555 dtp->u.p.current_unit->bytes_left -= (gfc_offset) dtp->u.p.skips;
557 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
560 bytes_used = (int)(dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left);
567 if (require_type (dtp, BT_INTEGER, type, f))
570 if (dtp->u.p.mode == READING)
571 read_decimal (dtp, f, p, len);
573 write_i (dtp, f, p, len);
580 if (require_type (dtp, BT_INTEGER, type, f))
583 if (dtp->u.p.mode == READING)
584 read_radix (dtp, f, p, len, 2);
586 write_b (dtp, f, p, len);
594 if (dtp->u.p.mode == READING)
595 read_radix (dtp, f, p, len, 8);
597 write_o (dtp, f, p, len);
605 if (dtp->u.p.mode == READING)
606 read_radix (dtp, f, p, len, 16);
608 write_z (dtp, f, p, len);
616 if (dtp->u.p.mode == READING)
617 read_a (dtp, f, p, len);
619 write_a (dtp, f, p, len);
627 if (dtp->u.p.mode == READING)
628 read_l (dtp, f, p, len);
630 write_l (dtp, f, p, len);
637 if (require_type (dtp, BT_REAL, type, f))
640 if (dtp->u.p.mode == READING)
641 read_f (dtp, f, p, len);
643 write_d (dtp, f, p, len);
650 if (require_type (dtp, BT_REAL, type, f))
653 if (dtp->u.p.mode == READING)
654 read_f (dtp, f, p, len);
656 write_e (dtp, f, p, len);
662 if (require_type (dtp, BT_REAL, type, f))
665 if (dtp->u.p.mode == READING)
666 read_f (dtp, f, p, len);
668 write_en (dtp, f, p, len);
675 if (require_type (dtp, BT_REAL, type, f))
678 if (dtp->u.p.mode == READING)
679 read_f (dtp, f, p, len);
681 write_es (dtp, f, p, len);
688 if (require_type (dtp, BT_REAL, type, f))
691 if (dtp->u.p.mode == READING)
692 read_f (dtp, f, p, len);
694 write_f (dtp, f, p, len);
701 if (dtp->u.p.mode == READING)
705 read_decimal (dtp, f, p, len);
708 read_l (dtp, f, p, len);
711 read_a (dtp, f, p, len);
714 read_f (dtp, f, p, len);
723 write_i (dtp, f, p, len);
726 write_l (dtp, f, p, len);
729 write_a (dtp, f, p, len);
732 write_d (dtp, f, p, len);
736 internal_error (&dtp->common,
737 "formatted_transfer(): Bad type");
743 consume_data_flag = 0 ;
744 if (dtp->u.p.mode == READING)
746 format_error (dtp, f, "Constant string in input format");
749 write_constant_string (dtp, f);
752 /* Format codes that don't transfer data. */
755 consume_data_flag = 0 ;
757 pos = bytes_used + f->u.n + dtp->u.p.skips;
758 dtp->u.p.skips = f->u.n + dtp->u.p.skips;
759 dtp->u.p.pending_spaces = pos - dtp->u.p.max_pos;
761 /* Writes occur just before the switch on f->format, above, so
762 that trailing blanks are suppressed, unless we are doing a
763 non-advancing write in which case we want to output the blanks
765 if (dtp->u.p.mode == WRITING
766 && dtp->u.p.advance_status == ADVANCE_NO)
768 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
769 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
771 if (dtp->u.p.mode == READING)
772 read_x (dtp, f->u.n);
778 if (f->format == FMT_TL)
779 pos = bytes_used - f->u.n;
782 consume_data_flag = 0;
786 /* Standard 10.6.1.1: excessive left tabbing is reset to the
787 left tab limit. We do not check if the position has gone
788 beyond the end of record because a subsequent tab could
789 bring us back again. */
790 pos = pos < 0 ? 0 : pos;
792 dtp->u.p.skips = dtp->u.p.skips + pos - bytes_used;
793 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces
794 + pos - dtp->u.p.max_pos;
796 if (dtp->u.p.skips == 0)
799 /* Writes occur just before the switch on f->format, above, so that
800 trailing blanks are suppressed. */
801 if (dtp->u.p.mode == READING)
803 /* Adjust everything for end-of-record condition */
804 if (dtp->u.p.sf_seen_eor && !is_internal_unit (dtp))
806 dtp->u.p.current_unit->bytes_left--;
808 dtp->u.p.sf_seen_eor = 0;
811 if (dtp->u.p.skips < 0)
813 move_pos_offset (dtp->u.p.current_unit->s, dtp->u.p.skips);
814 dtp->u.p.current_unit->bytes_left
815 -= (gfc_offset) dtp->u.p.skips;
816 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
819 read_x (dtp, dtp->u.p.skips);
825 consume_data_flag = 0 ;
826 dtp->u.p.sign_status = SIGN_S;
830 consume_data_flag = 0 ;
831 dtp->u.p.sign_status = SIGN_SS;
835 consume_data_flag = 0 ;
836 dtp->u.p.sign_status = SIGN_SP;
840 consume_data_flag = 0 ;
841 dtp->u.p.blank_status = BLANK_NULL;
845 consume_data_flag = 0 ;
846 dtp->u.p.blank_status = BLANK_ZERO;
850 consume_data_flag = 0 ;
851 dtp->u.p.scale_factor = f->u.k;
855 consume_data_flag = 0 ;
856 dtp->u.p.seen_dollar = 1;
860 consume_data_flag = 0 ;
861 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
862 next_record (dtp, 0);
866 /* A colon descriptor causes us to exit this loop (in
867 particular preventing another / descriptor from being
868 processed) unless there is another data item to be
870 consume_data_flag = 0 ;
876 internal_error (&dtp->common, "Bad format node");
879 /* Free a buffer that we had to allocate during a sequential
880 formatted read of a block that was larger than the static
883 if (dtp->u.p.line_buffer != scratch)
885 free_mem (dtp->u.p.line_buffer);
886 dtp->u.p.line_buffer = scratch;
889 /* Adjust the item count and data pointer. */
891 if ((consume_data_flag > 0) && (n > 0))
894 p = ((char *) p) + size;
897 if (dtp->u.p.mode == READING)
900 pos = (int)(dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left);
901 dtp->u.p.max_pos = (dtp->u.p.max_pos > pos) ? dtp->u.p.max_pos : pos;
907 /* Come here when we need a data descriptor but don't have one. We
908 push the current format node back onto the input, then return and
909 let the user program call us back with the data. */
911 unget_format (dtp, f);
915 formatted_transfer (st_parameter_dt *dtp, bt type, void *p, int kind,
916 size_t size, size_t nelems)
923 /* Big loop over all the elements. */
924 for (elem = 0; elem < nelems; elem++)
926 dtp->u.p.item_count++;
927 formatted_transfer_scalar (dtp, type, tmp + size*elem, kind, size);
933 /* Data transfer entry points. The type of the data entity is
934 implicit in the subroutine call. This prevents us from having to
935 share a common enum with the compiler. */
938 transfer_integer (st_parameter_dt *dtp, void *p, int kind)
940 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
942 dtp->u.p.transfer (dtp, BT_INTEGER, p, kind, kind, 1);
947 transfer_real (st_parameter_dt *dtp, void *p, int kind)
950 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
952 size = size_from_real_kind (kind);
953 dtp->u.p.transfer (dtp, BT_REAL, p, kind, size, 1);
958 transfer_logical (st_parameter_dt *dtp, void *p, int kind)
960 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
962 dtp->u.p.transfer (dtp, BT_LOGICAL, p, kind, kind, 1);
967 transfer_character (st_parameter_dt *dtp, void *p, int len)
969 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
971 /* Currently we support only 1 byte chars, and the library is a bit
972 confused of character kind vs. length, so we kludge it by setting
974 dtp->u.p.transfer (dtp, BT_CHARACTER, p, len, len, 1);
979 transfer_complex (st_parameter_dt *dtp, void *p, int kind)
982 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
984 size = size_from_complex_kind (kind);
985 dtp->u.p.transfer (dtp, BT_COMPLEX, p, kind, size, 1);
990 transfer_array (st_parameter_dt *dtp, gfc_array_char *desc, int kind,
991 gfc_charlen_type charlen)
993 index_type count[GFC_MAX_DIMENSIONS];
994 index_type extent[GFC_MAX_DIMENSIONS];
995 index_type stride[GFC_MAX_DIMENSIONS];
996 index_type stride0, rank, size, type, n;
1001 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1004 type = GFC_DESCRIPTOR_TYPE (desc);
1005 size = GFC_DESCRIPTOR_SIZE (desc);
1007 /* FIXME: What a kludge: Array descriptors and the IO library use
1008 different enums for types. */
1011 case GFC_DTYPE_UNKNOWN:
1012 iotype = BT_NULL; /* Is this correct? */
1014 case GFC_DTYPE_INTEGER:
1015 iotype = BT_INTEGER;
1017 case GFC_DTYPE_LOGICAL:
1018 iotype = BT_LOGICAL;
1020 case GFC_DTYPE_REAL:
1023 case GFC_DTYPE_COMPLEX:
1024 iotype = BT_COMPLEX;
1026 case GFC_DTYPE_CHARACTER:
1027 iotype = BT_CHARACTER;
1028 /* FIXME: Currently dtype contains the charlen, which is
1029 clobbered if charlen > 2**24. That's why we use a separate
1030 argument for the charlen. However, if we want to support
1031 non-8-bit charsets we need to fix dtype to contain
1032 sizeof(chartype) and fix the code below. */
1036 case GFC_DTYPE_DERIVED:
1037 internal_error (&dtp->common,
1038 "Derived type I/O should have been handled via the frontend.");
1041 internal_error (&dtp->common, "transfer_array(): Bad type");
1044 if (desc->dim[0].stride == 0)
1045 desc->dim[0].stride = 1;
1047 rank = GFC_DESCRIPTOR_RANK (desc);
1048 for (n = 0; n < rank; n++)
1051 stride[n] = desc->dim[n].stride;
1052 extent[n] = desc->dim[n].ubound + 1 - desc->dim[n].lbound;
1054 /* If the extent of even one dimension is zero, then the entire
1055 array section contains zero elements, so we return. */
1060 stride0 = stride[0];
1062 /* If the innermost dimension has stride 1, we can do the transfer
1063 in contiguous chunks. */
1069 data = GFC_DESCRIPTOR_DATA (desc);
1073 dtp->u.p.transfer (dtp, iotype, data, kind, size, tsize);
1074 data += stride0 * size * tsize;
1077 while (count[n] == extent[n])
1080 data -= stride[n] * extent[n] * size;
1090 data += stride[n] * size;
1097 /* Preposition a sequential unformatted file while reading. */
1100 us_read (st_parameter_dt *dtp)
1106 n = sizeof (gfc_offset);
1107 p = salloc_r (dtp->u.p.current_unit->s, &n);
1110 return; /* end of file */
1112 if (p == NULL || n != sizeof (gfc_offset))
1114 generate_error (&dtp->common, ERROR_BAD_US, NULL);
1118 memcpy (&i, p, sizeof (gfc_offset));
1119 dtp->u.p.current_unit->bytes_left = i;
1123 /* Preposition a sequential unformatted file while writing. This
1124 amount to writing a bogus length that will be filled in later. */
1127 us_write (st_parameter_dt *dtp)
1132 length = sizeof (gfc_offset);
1133 p = salloc_w (dtp->u.p.current_unit->s, &length);
1137 generate_error (&dtp->common, ERROR_OS, NULL);
1141 memset (p, '\0', sizeof (gfc_offset)); /* Bogus value for now. */
1142 if (sfree (dtp->u.p.current_unit->s) == FAILURE)
1143 generate_error (&dtp->common, ERROR_OS, NULL);
1145 /* For sequential unformatted, we write until we have more bytes than
1146 can fit in the record markers. If disk space runs out first, it will
1147 error on the write. */
1148 dtp->u.p.current_unit->recl = max_offset;
1150 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
1154 /* Position to the next record prior to transfer. We are assumed to
1155 be before the next record. We also calculate the bytes in the next
1159 pre_position (st_parameter_dt *dtp)
1161 if (dtp->u.p.current_unit->current_record)
1162 return; /* Already positioned. */
1164 switch (current_mode (dtp))
1166 case UNFORMATTED_SEQUENTIAL:
1167 if (dtp->u.p.mode == READING)
1174 case FORMATTED_SEQUENTIAL:
1175 case FORMATTED_DIRECT:
1176 case UNFORMATTED_DIRECT:
1177 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
1181 dtp->u.p.current_unit->current_record = 1;
1185 /* Initialize things for a data transfer. This code is common for
1186 both reading and writing. */
1189 data_transfer_init (st_parameter_dt *dtp, int read_flag)
1191 unit_flags u_flags; /* Used for creating a unit if needed. */
1192 GFC_INTEGER_4 cf = dtp->common.flags;
1193 namelist_info *ionml;
1195 ionml = ((cf & IOPARM_DT_IONML_SET) != 0) ? dtp->u.p.ionml : NULL;
1196 memset (&dtp->u.p, 0, sizeof (dtp->u.p));
1197 dtp->u.p.ionml = ionml;
1198 dtp->u.p.mode = read_flag ? READING : WRITING;
1200 if ((cf & IOPARM_DT_HAS_SIZE) != 0)
1201 *dtp->size = 0; /* Initialize the count. */
1203 dtp->u.p.current_unit = get_unit (dtp, 1);
1204 if (dtp->u.p.current_unit->s == NULL)
1205 { /* Open the unit with some default flags. */
1206 st_parameter_open opp;
1207 if (dtp->common.unit < 0)
1209 close_unit (dtp->u.p.current_unit);
1210 dtp->u.p.current_unit = NULL;
1211 generate_error (&dtp->common, ERROR_BAD_OPTION,
1212 "Bad unit number in OPEN statement");
1215 memset (&u_flags, '\0', sizeof (u_flags));
1216 u_flags.access = ACCESS_SEQUENTIAL;
1217 u_flags.action = ACTION_READWRITE;
1218 /* Is it unformatted? */
1219 if (!(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT)))
1220 u_flags.form = FORM_UNFORMATTED;
1222 u_flags.form = FORM_UNSPECIFIED;
1223 u_flags.delim = DELIM_UNSPECIFIED;
1224 u_flags.blank = BLANK_UNSPECIFIED;
1225 u_flags.pad = PAD_UNSPECIFIED;
1226 u_flags.status = STATUS_UNKNOWN;
1227 opp.common = dtp->common;
1228 opp.common.flags &= IOPARM_COMMON_MASK;
1229 dtp->u.p.current_unit = new_unit (&opp, dtp->u.p.current_unit, &u_flags);
1230 dtp->common.flags &= ~IOPARM_COMMON_MASK;
1231 dtp->common.flags |= (opp.common.flags & IOPARM_COMMON_MASK);
1232 if (dtp->u.p.current_unit == NULL)
1236 /* Check the action. */
1238 if (read_flag && dtp->u.p.current_unit->flags.action == ACTION_WRITE)
1239 generate_error (&dtp->common, ERROR_BAD_ACTION,
1240 "Cannot read from file opened for WRITE");
1242 if (!read_flag && dtp->u.p.current_unit->flags.action == ACTION_READ)
1243 generate_error (&dtp->common, ERROR_BAD_ACTION,
1244 "Cannot write to file opened for READ");
1246 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1249 dtp->u.p.first_item = 1;
1251 /* Check the format. */
1253 if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
1256 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1259 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED
1260 && (cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
1262 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1263 "Format present for UNFORMATTED data transfer");
1265 if ((cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0 && dtp->u.p.ionml != NULL)
1267 if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
1268 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1269 "A format cannot be specified with a namelist");
1271 else if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
1272 !(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT)))
1273 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1274 "Missing format for FORMATTED data transfer");
1277 if (is_internal_unit (dtp)
1278 && dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
1279 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1280 "Internal file cannot be accessed by UNFORMATTED data transfer");
1282 /* Check the record number. */
1284 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT
1285 && (cf & IOPARM_DT_HAS_REC) == 0)
1287 generate_error (&dtp->common, ERROR_MISSING_OPTION,
1288 "Direct access data transfer requires record number");
1292 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL
1293 && (cf & IOPARM_DT_HAS_REC) != 0)
1295 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1296 "Record number not allowed for sequential access data transfer");
1300 /* Process the ADVANCE option. */
1302 dtp->u.p.advance_status
1303 = !(cf & IOPARM_DT_HAS_ADVANCE) ? ADVANCE_UNSPECIFIED :
1304 find_option (&dtp->common, dtp->advance, dtp->advance_len, advance_opt,
1305 "Bad ADVANCE parameter in data transfer statement");
1307 if (dtp->u.p.advance_status != ADVANCE_UNSPECIFIED)
1309 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
1310 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1311 "ADVANCE specification conflicts with sequential access");
1313 if (is_internal_unit (dtp))
1314 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1315 "ADVANCE specification conflicts with internal file");
1317 if ((cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
1318 != IOPARM_DT_HAS_FORMAT)
1319 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1320 "ADVANCE specification requires an explicit format");
1325 if ((cf & IOPARM_EOR) != 0 && dtp->u.p.advance_status != ADVANCE_NO)
1326 generate_error (&dtp->common, ERROR_MISSING_OPTION,
1327 "EOR specification requires an ADVANCE specification of NO");
1329 if ((cf & IOPARM_DT_HAS_SIZE) != 0 && dtp->u.p.advance_status != ADVANCE_NO)
1330 generate_error (&dtp->common, ERROR_MISSING_OPTION,
1331 "SIZE specification requires an ADVANCE specification of NO");
1335 { /* Write constraints. */
1336 if ((cf & IOPARM_END) != 0)
1337 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1338 "END specification cannot appear in a write statement");
1340 if ((cf & IOPARM_EOR) != 0)
1341 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1342 "EOR specification cannot appear in a write statement");
1344 if ((cf & IOPARM_DT_HAS_SIZE) != 0)
1345 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1346 "SIZE specification cannot appear in a write statement");
1349 if (dtp->u.p.advance_status == ADVANCE_UNSPECIFIED)
1350 dtp->u.p.advance_status = ADVANCE_YES;
1351 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1354 /* Sanity checks on the record number. */
1356 if ((cf & IOPARM_DT_HAS_REC) != 0)
1360 generate_error (&dtp->common, ERROR_BAD_OPTION,
1361 "Record number must be positive");
1365 if (dtp->rec >= dtp->u.p.current_unit->maxrec)
1367 generate_error (&dtp->common, ERROR_BAD_OPTION,
1368 "Record number too large");
1372 /* Check to see if we might be reading what we wrote before */
1374 if (dtp->u.p.mode == READING && dtp->u.p.current_unit->mode == WRITING)
1375 flush(dtp->u.p.current_unit->s);
1377 /* Check whether the record exists to be read. Only
1378 a partial record needs to exist. */
1380 if (dtp->u.p.mode == READING && (dtp->rec -1)
1381 * dtp->u.p.current_unit->recl >= file_length (dtp->u.p.current_unit->s))
1383 generate_error (&dtp->common, ERROR_BAD_OPTION,
1384 "Non-existing record number");
1388 /* Position the file. */
1389 if (sseek (dtp->u.p.current_unit->s,
1390 (dtp->rec - 1) * dtp->u.p.current_unit->recl) == FAILURE)
1392 generate_error (&dtp->common, ERROR_OS, NULL);
1397 /* Overwriting an existing sequential file ?
1398 it is always safe to truncate the file on the first write */
1399 if (dtp->u.p.mode == WRITING
1400 && dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL
1401 && dtp->u.p.current_unit->last_record == 0 && !is_preconnected(dtp->u.p.current_unit->s))
1402 struncate(dtp->u.p.current_unit->s);
1404 /* Bugware for badly written mixed C-Fortran I/O. */
1405 flush_if_preconnected(dtp->u.p.current_unit->s);
1407 dtp->u.p.current_unit->mode = dtp->u.p.mode;
1409 /* Set the initial value of flags. */
1411 dtp->u.p.blank_status = dtp->u.p.current_unit->flags.blank;
1412 dtp->u.p.sign_status = SIGN_S;
1416 /* Set up the subroutine that will handle the transfers. */
1420 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
1421 dtp->u.p.transfer = unformatted_read;
1424 if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
1425 dtp->u.p.transfer = list_formatted_read;
1427 dtp->u.p.transfer = formatted_transfer;
1432 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
1433 dtp->u.p.transfer = unformatted_write;
1436 if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
1437 dtp->u.p.transfer = list_formatted_write;
1439 dtp->u.p.transfer = formatted_transfer;
1443 /* Make sure that we don't do a read after a nonadvancing write. */
1447 if (dtp->u.p.current_unit->read_bad)
1449 generate_error (&dtp->common, ERROR_BAD_OPTION,
1450 "Cannot READ after a nonadvancing WRITE");
1456 if (dtp->u.p.advance_status == ADVANCE_YES && !dtp->u.p.seen_dollar)
1457 dtp->u.p.current_unit->read_bad = 1;
1460 /* Start the data transfer if we are doing a formatted transfer. */
1461 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
1462 && ((cf & (IOPARM_DT_LIST_FORMAT | IOPARM_DT_HAS_NAMELIST_NAME)) == 0)
1463 && dtp->u.p.ionml == NULL)
1464 formatted_transfer (dtp, 0, NULL, 0, 0, 1);
1467 /* Initialize an array_loop_spec given the array descriptor. The function
1468 returns the index of the last element of the array. */
1471 init_loop_spec (gfc_array_char *desc, array_loop_spec *ls)
1473 int rank = GFC_DESCRIPTOR_RANK(desc);
1478 for (i=0; i<rank; i++)
1481 ls[i].start = desc->dim[i].lbound;
1482 ls[i].end = desc->dim[i].ubound;
1483 ls[i].step = desc->dim[i].stride;
1485 index += (desc->dim[i].ubound - desc->dim[i].lbound)
1486 * desc->dim[i].stride;
1491 /* Determine the index to the next record in an internal unit array by
1492 by incrementing through the array_loop_spec. TODO: Implement handling
1493 negative strides. */
1496 next_array_record (st_parameter_dt *dtp, array_loop_spec *ls)
1504 for (i = 0; i < dtp->u.p.current_unit->rank; i++)
1509 if (ls[i].idx > ls[i].end)
1511 ls[i].idx = ls[i].start;
1517 index = index + (ls[i].idx - 1) * ls[i].step;
1522 /* Space to the next record for read mode. If the file is not
1523 seekable, we read MAX_READ chunks until we get to the right
1526 #define MAX_READ 4096
1529 next_record_r (st_parameter_dt *dtp)
1531 gfc_offset new, record;
1532 int bytes_left, rlength, length;
1535 switch (current_mode (dtp))
1537 case UNFORMATTED_SEQUENTIAL:
1538 dtp->u.p.current_unit->bytes_left += sizeof (gfc_offset); /* Skip over tail */
1540 /* Fall through... */
1542 case FORMATTED_DIRECT:
1543 case UNFORMATTED_DIRECT:
1544 if (dtp->u.p.current_unit->bytes_left == 0)
1547 if (is_seekable (dtp->u.p.current_unit->s))
1549 new = file_position (dtp->u.p.current_unit->s) + dtp->u.p.current_unit->bytes_left;
1551 /* Direct access files do not generate END conditions,
1553 if (sseek (dtp->u.p.current_unit->s, new) == FAILURE)
1554 generate_error (&dtp->common, ERROR_OS, NULL);
1558 { /* Seek by reading data. */
1559 while (dtp->u.p.current_unit->bytes_left > 0)
1561 rlength = length = (MAX_READ > dtp->u.p.current_unit->bytes_left) ?
1562 MAX_READ : dtp->u.p.current_unit->bytes_left;
1564 p = salloc_r (dtp->u.p.current_unit->s, &rlength);
1567 generate_error (&dtp->common, ERROR_OS, NULL);
1571 dtp->u.p.current_unit->bytes_left -= length;
1576 case FORMATTED_SEQUENTIAL:
1578 /* sf_read has already terminated input because of an '\n' */
1579 if (dtp->u.p.sf_seen_eor)
1581 dtp->u.p.sf_seen_eor = 0;
1585 if (is_internal_unit (dtp))
1587 if (is_array_io (dtp))
1589 record = next_array_record (dtp, dtp->u.p.current_unit->ls);
1591 /* Now seek to this record. */
1592 record = record * dtp->u.p.current_unit->recl;
1593 if (sseek (dtp->u.p.current_unit->s, record) == FAILURE)
1595 generate_error (&dtp->common, ERROR_OS, NULL);
1598 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
1602 bytes_left = (int) dtp->u.p.current_unit->bytes_left;
1603 p = salloc_r (dtp->u.p.current_unit->s, &bytes_left);
1605 dtp->u.p.current_unit->bytes_left
1606 = dtp->u.p.current_unit->recl;
1612 p = salloc_r (dtp->u.p.current_unit->s, &length);
1616 generate_error (&dtp->common, ERROR_OS, NULL);
1622 dtp->u.p.current_unit->endfile = AT_ENDFILE;
1631 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
1632 test_endfile (dtp->u.p.current_unit);
1636 /* Position to the next record in write mode. */
1639 next_record_w (st_parameter_dt *dtp)
1641 gfc_offset c, m, record;
1642 int bytes_left, length;
1645 /* Zero counters for X- and T-editing. */
1646 dtp->u.p.max_pos = dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1648 switch (current_mode (dtp))
1650 case FORMATTED_DIRECT:
1651 if (dtp->u.p.current_unit->bytes_left == 0)
1654 length = dtp->u.p.current_unit->bytes_left;
1655 p = salloc_w (dtp->u.p.current_unit->s, &length);
1660 memset (p, ' ', dtp->u.p.current_unit->bytes_left);
1661 if (sfree (dtp->u.p.current_unit->s) == FAILURE)
1665 case UNFORMATTED_DIRECT:
1666 if (sfree (dtp->u.p.current_unit->s) == FAILURE)
1670 case UNFORMATTED_SEQUENTIAL:
1671 /* Bytes written. */
1672 m = dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left;
1673 c = file_position (dtp->u.p.current_unit->s);
1675 length = sizeof (gfc_offset);
1677 /* Write the length tail. */
1679 p = salloc_w (dtp->u.p.current_unit->s, &length);
1683 memcpy (p, &m, sizeof (gfc_offset));
1684 if (sfree (dtp->u.p.current_unit->s) == FAILURE)
1687 /* Seek to the head and overwrite the bogus length with the real
1690 p = salloc_w_at (dtp->u.p.current_unit->s, &length, c - m - length);
1692 generate_error (&dtp->common, ERROR_OS, NULL);
1694 memcpy (p, &m, sizeof (gfc_offset));
1695 if (sfree (dtp->u.p.current_unit->s) == FAILURE)
1698 /* Seek past the end of the current record. */
1700 if (sseek (dtp->u.p.current_unit->s, c + sizeof (gfc_offset)) == FAILURE)
1705 case FORMATTED_SEQUENTIAL:
1707 if (dtp->u.p.current_unit->bytes_left == 0)
1710 if (is_internal_unit (dtp))
1712 if (is_array_io (dtp))
1714 bytes_left = (int) dtp->u.p.current_unit->bytes_left;
1715 p = salloc_w (dtp->u.p.current_unit->s, &bytes_left);
1718 generate_error (&dtp->common, ERROR_END, NULL);
1721 memset(p, ' ', bytes_left);
1723 /* Now that the current record has been padded out,
1724 determine where the next record in the array is. */
1726 record = next_array_record (dtp, dtp->u.p.current_unit->ls);
1728 /* Now seek to this record */
1729 record = record * dtp->u.p.current_unit->recl;
1731 if (sseek (dtp->u.p.current_unit->s, record) == FAILURE)
1734 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
1739 p = salloc_w (dtp->u.p.current_unit->s, &length);
1751 p = salloc_w (dtp->u.p.current_unit->s, &length);
1753 { /* No new line for internal writes. */
1768 generate_error (&dtp->common, ERROR_OS, NULL);
1773 /* Position to the next record, which means moving to the end of the
1774 current record. This can happen under several different
1775 conditions. If the done flag is not set, we get ready to process
1779 next_record (st_parameter_dt *dtp, int done)
1781 gfc_offset fp; /* File position. */
1783 dtp->u.p.current_unit->read_bad = 0;
1785 if (dtp->u.p.mode == READING)
1786 next_record_r (dtp);
1788 next_record_w (dtp);
1790 /* keep position up to date for INQUIRE */
1791 dtp->u.p.current_unit->flags.position = POSITION_ASIS;
1793 dtp->u.p.current_unit->current_record = 0;
1794 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
1796 fp = file_position (dtp->u.p.current_unit->s);
1797 /* Calculate next record, rounding up partial records. */
1798 dtp->u.p.current_unit->last_record = (fp + dtp->u.p.current_unit->recl - 1)
1799 / dtp->u.p.current_unit->recl;
1802 dtp->u.p.current_unit->last_record++;
1809 /* Finalize the current data transfer. For a nonadvancing transfer,
1810 this means advancing to the next record. For internal units close the
1811 stream associated with the unit. */
1814 finalize_transfer (st_parameter_dt *dtp)
1817 GFC_INTEGER_4 cf = dtp->common.flags;
1819 if (dtp->u.p.eor_condition)
1821 generate_error (&dtp->common, ERROR_EOR, NULL);
1825 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1828 if ((dtp->u.p.ionml != NULL)
1829 && (cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0)
1831 if ((cf & IOPARM_DT_NAMELIST_READ_MODE) != 0)
1832 namelist_read (dtp);
1834 namelist_write (dtp);
1837 dtp->u.p.transfer = NULL;
1838 if (dtp->u.p.current_unit == NULL)
1841 dtp->u.p.eof_jump = &eof_jump;
1842 if (setjmp (eof_jump))
1844 generate_error (&dtp->common, ERROR_END, NULL);
1848 if ((cf & IOPARM_DT_LIST_FORMAT) != 0 && dtp->u.p.mode == READING)
1849 finish_list_read (dtp);
1852 if (dtp->u.p.advance_status == ADVANCE_NO || dtp->u.p.seen_dollar)
1854 /* Most systems buffer lines, so force the partial record
1855 to be written out. */
1856 flush (dtp->u.p.current_unit->s);
1857 dtp->u.p.seen_dollar = 0;
1861 next_record (dtp, 1);
1862 dtp->u.p.current_unit->current_record = 0;
1865 sfree (dtp->u.p.current_unit->s);
1867 if (is_internal_unit (dtp))
1869 if (is_array_io (dtp) && dtp->u.p.current_unit->ls != NULL)
1870 free_mem (dtp->u.p.current_unit->ls);
1871 sclose (dtp->u.p.current_unit->s);
1876 /* Transfer function for IOLENGTH. It doesn't actually do any
1877 data transfer, it just updates the length counter. */
1880 iolength_transfer (st_parameter_dt *dtp, bt type __attribute__((unused)),
1881 void *dest __attribute__ ((unused)),
1882 int kind __attribute__((unused)),
1883 size_t size, size_t nelems)
1885 if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
1886 *dtp->iolength += (GFC_INTEGER_4) size * nelems;
1890 /* Initialize the IOLENGTH data transfer. This function is in essence
1891 a very much simplified version of data_transfer_init(), because it
1892 doesn't have to deal with units at all. */
1895 iolength_transfer_init (st_parameter_dt *dtp)
1897 if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
1900 memset (&dtp->u.p, 0, sizeof (dtp->u.p));
1902 /* Set up the subroutine that will handle the transfers. */
1904 dtp->u.p.transfer = iolength_transfer;
1908 /* Library entry point for the IOLENGTH form of the INQUIRE
1909 statement. The IOLENGTH form requires no I/O to be performed, but
1910 it must still be a runtime library call so that we can determine
1911 the iolength for dynamic arrays and such. */
1913 extern void st_iolength (st_parameter_dt *);
1914 export_proto(st_iolength);
1917 st_iolength (st_parameter_dt *dtp)
1919 library_start (&dtp->common);
1920 iolength_transfer_init (dtp);
1923 extern void st_iolength_done (st_parameter_dt *);
1924 export_proto(st_iolength_done);
1927 st_iolength_done (st_parameter_dt *dtp __attribute__((unused)))
1930 if (dtp->u.p.scratch != NULL)
1931 free_mem (dtp->u.p.scratch);
1936 /* The READ statement. */
1938 extern void st_read (st_parameter_dt *);
1939 export_proto(st_read);
1942 st_read (st_parameter_dt *dtp)
1945 library_start (&dtp->common);
1947 data_transfer_init (dtp, 1);
1949 /* Handle complications dealing with the endfile record. It is
1950 significant that this is the only place where ERROR_END is
1951 generated. Reading an end of file elsewhere is either end of
1952 record or an I/O error. */
1954 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
1955 switch (dtp->u.p.current_unit->endfile)
1961 if (!is_internal_unit (dtp))
1963 generate_error (&dtp->common, ERROR_END, NULL);
1964 dtp->u.p.current_unit->endfile = AFTER_ENDFILE;
1965 dtp->u.p.current_unit->current_record = 0;
1970 generate_error (&dtp->common, ERROR_ENDFILE, NULL);
1971 dtp->u.p.current_unit->current_record = 0;
1976 extern void st_read_done (st_parameter_dt *);
1977 export_proto(st_read_done);
1980 st_read_done (st_parameter_dt *dtp)
1982 finalize_transfer (dtp);
1983 free_format_data (dtp);
1985 if (dtp->u.p.scratch != NULL)
1986 free_mem (dtp->u.p.scratch);
1987 if (dtp->u.p.current_unit != NULL)
1988 unlock_unit (dtp->u.p.current_unit);
1992 extern void st_write (st_parameter_dt *);
1993 export_proto(st_write);
1996 st_write (st_parameter_dt *dtp)
1998 library_start (&dtp->common);
1999 data_transfer_init (dtp, 0);
2002 extern void st_write_done (st_parameter_dt *);
2003 export_proto(st_write_done);
2006 st_write_done (st_parameter_dt *dtp)
2008 finalize_transfer (dtp);
2010 /* Deal with endfile conditions associated with sequential files. */
2012 if (dtp->u.p.current_unit != NULL && dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
2013 switch (dtp->u.p.current_unit->endfile)
2015 case AT_ENDFILE: /* Remain at the endfile record. */
2019 dtp->u.p.current_unit->endfile = AT_ENDFILE; /* Just at it now. */
2023 if (dtp->u.p.current_unit->current_record > dtp->u.p.current_unit->last_record)
2025 /* Get rid of whatever is after this record. */
2026 if (struncate (dtp->u.p.current_unit->s) == FAILURE)
2027 generate_error (&dtp->common, ERROR_OS, NULL);
2030 dtp->u.p.current_unit->endfile = AT_ENDFILE;
2034 free_format_data (dtp);
2036 if (dtp->u.p.scratch != NULL)
2037 free_mem (dtp->u.p.scratch);
2038 if (dtp->u.p.current_unit != NULL)
2039 unlock_unit (dtp->u.p.current_unit);
2043 /* Receives the scalar information for namelist objects and stores it
2044 in a linked list of namelist_info types. */
2046 extern void st_set_nml_var (st_parameter_dt *dtp, void *, char *,
2047 GFC_INTEGER_4, gfc_charlen_type, GFC_INTEGER_4);
2048 export_proto(st_set_nml_var);
2052 st_set_nml_var (st_parameter_dt *dtp, void * var_addr, char * var_name,
2053 GFC_INTEGER_4 len, gfc_charlen_type string_length,
2054 GFC_INTEGER_4 dtype)
2056 namelist_info *t1 = NULL;
2059 nml = (namelist_info*) get_mem (sizeof (namelist_info));
2061 nml->mem_pos = var_addr;
2063 nml->var_name = (char*) get_mem (strlen (var_name) + 1);
2064 strcpy (nml->var_name, var_name);
2066 nml->len = (int) len;
2067 nml->string_length = (index_type) string_length;
2069 nml->var_rank = (int) (dtype & GFC_DTYPE_RANK_MASK);
2070 nml->size = (index_type) (dtype >> GFC_DTYPE_SIZE_SHIFT);
2071 nml->type = (bt) ((dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT);
2073 if (nml->var_rank > 0)
2075 nml->dim = (descriptor_dimension*)
2076 get_mem (nml->var_rank * sizeof (descriptor_dimension));
2077 nml->ls = (array_loop_spec*)
2078 get_mem (nml->var_rank * sizeof (array_loop_spec));
2088 if ((dtp->common.flags & IOPARM_DT_IONML_SET) == 0)
2090 dtp->common.flags |= IOPARM_DT_IONML_SET;
2091 dtp->u.p.ionml = nml;
2095 for (t1 = dtp->u.p.ionml; t1->next; t1 = t1->next);
2100 /* Store the dimensional information for the namelist object. */
2101 extern void st_set_nml_var_dim (st_parameter_dt *, GFC_INTEGER_4,
2102 GFC_INTEGER_4, GFC_INTEGER_4,
2104 export_proto(st_set_nml_var_dim);
2107 st_set_nml_var_dim (st_parameter_dt *dtp, GFC_INTEGER_4 n_dim,
2108 GFC_INTEGER_4 stride, GFC_INTEGER_4 lbound,
2109 GFC_INTEGER_4 ubound)
2111 namelist_info * nml;
2116 for (nml = dtp->u.p.ionml; nml->next; nml = nml->next);
2118 nml->dim[n].stride = (ssize_t)stride;
2119 nml->dim[n].lbound = (ssize_t)lbound;
2120 nml->dim[n].ubound = (ssize_t)ubound;