1 /* Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
3 Namelist transfer functions contributed by Paul Thomas
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with Libgfortran; see the file COPYING. If not, write to
28 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA. */
32 /* transfer.c -- Top level handling of data transfer statements. */
37 #include "libgfortran.h"
41 /* Calling conventions: Data transfer statements are unlike other
42 library calls in that they extend over several calls.
44 The first call is always a call to st_read() or st_write(). These
45 subroutines return no status unless a namelist read or write is
46 being done, in which case there is the usual status. No further
47 calls are necessary in this case.
49 For other sorts of data transfer, there are zero or more data
50 transfer statement that depend on the format of the data transfer
59 These subroutines do not return status.
61 The last call is a call to st_[read|write]_done(). While
62 something can easily go wrong with the initial st_read() or
63 st_write(), an error inhibits any data from actually being
66 extern void transfer_integer (st_parameter_dt *, void *, int);
67 export_proto(transfer_integer);
69 extern void transfer_real (st_parameter_dt *, void *, int);
70 export_proto(transfer_real);
72 extern void transfer_logical (st_parameter_dt *, void *, int);
73 export_proto(transfer_logical);
75 extern void transfer_character (st_parameter_dt *, void *, int);
76 export_proto(transfer_character);
78 extern void transfer_complex (st_parameter_dt *, void *, int);
79 export_proto(transfer_complex);
81 extern void transfer_array (st_parameter_dt *, gfc_array_char *, int,
83 export_proto(transfer_array);
85 static const st_option advance_opt[] = {
93 { FORMATTED_SEQUENTIAL, UNFORMATTED_SEQUENTIAL,
94 FORMATTED_DIRECT, UNFORMATTED_DIRECT, FORMATTED_STREAM, UNFORMATTED_STREAM
100 current_mode (st_parameter_dt *dtp)
104 m = FORM_UNSPECIFIED;
106 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
108 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
109 FORMATTED_DIRECT : UNFORMATTED_DIRECT;
111 else if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
113 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
114 FORMATTED_SEQUENTIAL : UNFORMATTED_SEQUENTIAL;
116 else if (dtp->u.p.current_unit->flags.access == ACCESS_STREAM)
118 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
119 FORMATTED_STREAM : UNFORMATTED_STREAM;
126 /* Mid level data transfer statements. These subroutines do reading
127 and writing in the style of salloc_r()/salloc_w() within the
130 /* When reading sequential formatted records we have a problem. We
131 don't know how long the line is until we read the trailing newline,
132 and we don't want to read too much. If we read too much, we might
133 have to do a physical seek backwards depending on how much data is
134 present, and devices like terminals aren't seekable and would cause
137 Given this, the solution is to read a byte at a time, stopping if
138 we hit the newline. For small allocations, we use a static buffer.
139 For larger allocations, we are forced to allocate memory on the
140 heap. Hopefully this won't happen very often. */
143 read_sf (st_parameter_dt *dtp, int *length, int no_error)
146 int n, readlen, crlf;
149 if (*length > SCRATCH_SIZE)
150 dtp->u.p.line_buffer = get_mem (*length);
151 p = base = dtp->u.p.line_buffer;
153 /* If we have seen an eor previously, return a length of 0. The
154 caller is responsible for correctly padding the input field. */
155 if (dtp->u.p.sf_seen_eor)
166 if (is_internal_unit (dtp))
168 /* readlen may be modified inside salloc_r if
169 is_internal_unit (dtp) is true. */
173 q = salloc_r (dtp->u.p.current_unit->s, &readlen);
177 /* If we have a line without a terminating \n, drop through to
179 if (readlen < 1 && n == 0)
183 generate_error (&dtp->common, ERROR_END, NULL);
187 if (readlen < 1 || *q == '\n' || *q == '\r')
189 /* Unexpected end of line. */
191 /* If we see an EOR during non-advancing I/O, we need to skip
192 the rest of the I/O statement. Set the corresponding flag. */
193 if (dtp->u.p.advance_status == ADVANCE_NO || dtp->u.p.seen_dollar)
194 dtp->u.p.eor_condition = 1;
197 /* If we encounter a CR, it might be a CRLF. */
198 if (*q == '\r') /* Probably a CRLF */
201 pos = stream_offset (dtp->u.p.current_unit->s);
202 q = salloc_r (dtp->u.p.current_unit->s, &readlen);
203 if (*q != '\n' && readlen == 1) /* Not a CRLF after all. */
204 sseek (dtp->u.p.current_unit->s, pos);
209 /* Without padding, terminate the I/O statement without assigning
210 the value. With padding, the value still needs to be assigned,
211 so we can just continue with a short read. */
212 if (dtp->u.p.current_unit->flags.pad == PAD_NO)
216 generate_error (&dtp->common, ERROR_EOR, NULL);
221 dtp->u.p.sf_seen_eor = (crlf ? 2 : 1);
224 /* Short circuit the read if a comma is found during numeric input.
225 The flag is set to zero during character reads so that commas in
226 strings are not ignored */
228 if (dtp->u.p.sf_read_comma == 1)
230 notify_std (&dtp->common, GFC_STD_GNU,
231 "Comma in formatted numeric read.");
238 dtp->u.p.sf_seen_eor = 0;
241 dtp->u.p.current_unit->bytes_left -= *length;
243 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
244 dtp->u.p.size_used += (gfc_offset) *length;
250 /* Function for reading the next couple of bytes from the current
251 file, advancing the current position. We return a pointer to a
252 buffer containing the bytes. We return NULL on end of record or
255 If the read is short, then it is because the current record does not
256 have enough data to satisfy the read request and the file was
257 opened with PAD=YES. The caller must assume tailing spaces for
261 read_block (st_parameter_dt *dtp, int *length)
266 if (!is_stream_io (dtp))
268 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) *length)
270 /* For preconnected units with default record length, set bytes left
271 to unit record length and proceed, otherwise error. */
272 if (dtp->u.p.current_unit->unit_number == options.stdin_unit
273 && dtp->u.p.current_unit->recl == DEFAULT_RECL)
274 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
277 if (dtp->u.p.current_unit->flags.pad == PAD_NO)
279 /* Not enough data left. */
280 generate_error (&dtp->common, ERROR_EOR, NULL);
285 if (dtp->u.p.current_unit->bytes_left == 0)
287 dtp->u.p.current_unit->endfile = AT_ENDFILE;
288 generate_error (&dtp->common, ERROR_END, NULL);
292 *length = dtp->u.p.current_unit->bytes_left;
295 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
296 dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
297 return read_sf (dtp, length, 0); /* Special case. */
299 dtp->u.p.current_unit->bytes_left -= (gfc_offset) *length;
302 source = salloc_r (dtp->u.p.current_unit->s, &nread);
304 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
305 dtp->u.p.size_used += (gfc_offset) nread;
307 if (nread != *length)
308 { /* Short read, this shouldn't happen. */
309 if (dtp->u.p.current_unit->flags.pad == PAD_YES)
313 generate_error (&dtp->common, ERROR_EOR, NULL);
320 if (sseek (dtp->u.p.current_unit->s,
321 (gfc_offset) (dtp->rec - 1)) == FAILURE)
323 generate_error (&dtp->common, ERROR_END, NULL);
328 source = salloc_r (dtp->u.p.current_unit->s, &nread);
330 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
331 dtp->u.p.size_used += (gfc_offset) nread;
333 if (nread != *length)
334 { /* Short read, this shouldn't happen. */
335 if (dtp->u.p.current_unit->flags.pad == PAD_YES)
339 generate_error (&dtp->common, ERROR_END, NULL);
344 dtp->rec += (GFC_IO_INT) nread;
350 /* Reads a block directly into application data space. */
353 read_block_direct (st_parameter_dt *dtp, void *buf, size_t *nbytes)
359 if (!is_stream_io (dtp))
361 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) *nbytes)
363 /* For preconnected units with default record length, set
364 bytes left to unit record length and proceed, otherwise
366 if (dtp->u.p.current_unit->unit_number == options.stdin_unit
367 && dtp->u.p.current_unit->recl == DEFAULT_RECL)
368 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
371 if (dtp->u.p.current_unit->flags.pad == PAD_NO)
373 /* Not enough data left. */
374 generate_error (&dtp->common, ERROR_EOR, NULL);
379 if (dtp->u.p.current_unit->bytes_left == 0)
381 dtp->u.p.current_unit->endfile = AT_ENDFILE;
382 generate_error (&dtp->common, ERROR_END, NULL);
386 *nbytes = (size_t) dtp->u.p.current_unit->bytes_left;
389 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
390 dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
392 length = (int *) nbytes;
393 data = read_sf (dtp, length, 0); /* Special case. */
394 memcpy (buf, data, (size_t) *length);
398 dtp->u.p.current_unit->bytes_left -= (gfc_offset) *nbytes;
402 if (sseek (dtp->u.p.current_unit->s,
403 (gfc_offset) (dtp->rec - 1)) == FAILURE)
405 generate_error (&dtp->common, ERROR_END, NULL);
411 if (sread (dtp->u.p.current_unit->s, buf, &nread) != 0)
413 generate_error (&dtp->common, ERROR_OS, NULL);
417 if (!is_stream_io (dtp))
419 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
420 dtp->u.p.size_used += (gfc_offset) nread;
423 dtp->rec += (GFC_IO_INT) nread;
425 if (nread != *nbytes) /* Short read, e.g. if we hit EOF. */
427 if (!is_stream_io (dtp))
428 generate_error (&dtp->common, ERROR_EOR, NULL);
430 generate_error (&dtp->common, ERROR_END, NULL);
435 /* Function for writing a block of bytes to the current file at the
436 current position, advancing the file pointer. We are given a length
437 and return a pointer to a buffer that the caller must (completely)
438 fill in. Returns NULL on error. */
441 write_block (st_parameter_dt *dtp, int length)
445 if (!is_stream_io (dtp))
447 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) length)
449 /* For preconnected units with default record length, set bytes left
450 to unit record length and proceed, otherwise error. */
451 if ((dtp->u.p.current_unit->unit_number == options.stdout_unit
452 || dtp->u.p.current_unit->unit_number == options.stderr_unit)
453 && dtp->u.p.current_unit->recl == DEFAULT_RECL)
454 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
457 generate_error (&dtp->common, ERROR_EOR, NULL);
462 dtp->u.p.current_unit->bytes_left -= (gfc_offset) length;
465 dest = salloc_w (dtp->u.p.current_unit->s, &length);
469 generate_error (&dtp->common, ERROR_END, NULL);
473 if (is_internal_unit (dtp) && dtp->u.p.current_unit->endfile == AT_ENDFILE)
474 generate_error (&dtp->common, ERROR_END, NULL);
476 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
477 dtp->u.p.size_used += (gfc_offset) length;
481 if (sseek (dtp->u.p.current_unit->s,
482 (gfc_offset) (dtp->rec - 1)) == FAILURE)
484 generate_error (&dtp->common, ERROR_END, NULL);
488 dest = salloc_w (dtp->u.p.current_unit->s, &length);
492 generate_error (&dtp->common, ERROR_END, NULL);
496 dtp->rec += (GFC_IO_INT) length;
503 /* High level interface to swrite(), taking care of errors. */
506 write_buf (st_parameter_dt *dtp, void *buf, size_t nbytes)
508 if (!is_stream_io (dtp))
510 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) nbytes)
512 /* For preconnected units with default record length, set
513 bytes left to unit record length and proceed, otherwise
515 if ((dtp->u.p.current_unit->unit_number == options.stdout_unit
516 || dtp->u.p.current_unit->unit_number == options.stderr_unit)
517 && dtp->u.p.current_unit->recl == DEFAULT_RECL)
518 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
521 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
522 generate_error (&dtp->common, ERROR_DIRECT_EOR, NULL);
524 generate_error (&dtp->common, ERROR_EOR, NULL);
529 dtp->u.p.current_unit->bytes_left -= (gfc_offset) nbytes;
533 if (sseek (dtp->u.p.current_unit->s,
534 (gfc_offset) (dtp->rec - 1)) == FAILURE)
536 generate_error (&dtp->common, ERROR_OS, NULL);
541 if (swrite (dtp->u.p.current_unit->s, buf, &nbytes) != 0)
543 generate_error (&dtp->common, ERROR_OS, NULL);
547 if (!is_stream_io (dtp))
549 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
550 dtp->u.p.size_used += (gfc_offset) nbytes;
553 dtp->rec += (GFC_IO_INT) nbytes;
559 /* Master function for unformatted reads. */
562 unformatted_read (st_parameter_dt *dtp, bt type,
563 void *dest, int kind,
564 size_t size, size_t nelems)
568 /* Currently, character implies size=1. */
569 if (dtp->u.p.current_unit->flags.convert == CONVERT_NATIVE
570 || size == 1 || type == BT_CHARACTER)
573 read_block_direct (dtp, dest, &sz);
580 /* Break up complex into its constituent reals. */
581 if (type == BT_COMPLEX)
588 /* By now, all complex variables have been split into their
589 constituent reals. For types with padding, we only need to
590 read kind bytes. We don't care about the contents
594 for (i=0; i<nelems; i++)
596 read_block_direct (dtp, buffer, &sz);
597 reverse_memcpy (p, buffer, sz);
604 /* Master function for unformatted writes. */
607 unformatted_write (st_parameter_dt *dtp, bt type,
608 void *source, int kind,
609 size_t size, size_t nelems)
611 if (dtp->u.p.current_unit->flags.convert == CONVERT_NATIVE ||
612 size == 1 || type == BT_CHARACTER)
616 write_buf (dtp, source, size);
624 /* Break up complex into its constituent reals. */
625 if (type == BT_COMPLEX)
633 /* By now, all complex variables have been split into their
634 constituent reals. For types with padding, we only need to
635 read kind bytes. We don't care about the contents
639 for (i=0; i<nelems; i++)
641 reverse_memcpy(buffer, p, size);
643 write_buf (dtp, buffer, sz);
649 /* Return a pointer to the name of a type. */
674 internal_error (NULL, "type_name(): Bad type");
681 /* Write a constant string to the output.
682 This is complicated because the string can have doubled delimiters
683 in it. The length in the format node is the true length. */
686 write_constant_string (st_parameter_dt *dtp, const fnode *f)
688 char c, delimiter, *p, *q;
691 length = f->u.string.length;
695 p = write_block (dtp, length);
702 for (; length > 0; length--)
705 if (c == delimiter && c != 'H' && c != 'h')
706 q++; /* Skip the doubled delimiter. */
711 /* Given actual and expected types in a formatted data transfer, make
712 sure they agree. If not, an error message is generated. Returns
713 nonzero if something went wrong. */
716 require_type (st_parameter_dt *dtp, bt expected, bt actual, const fnode *f)
720 if (actual == expected)
723 st_sprintf (buffer, "Expected %s for item %d in formatted transfer, got %s",
724 type_name (expected), dtp->u.p.item_count, type_name (actual));
726 format_error (dtp, f, buffer);
731 /* This subroutine is the main loop for a formatted data transfer
732 statement. It would be natural to implement this as a coroutine
733 with the user program, but C makes that awkward. We loop,
734 processesing format elements. When we actually have to transfer
735 data instead of just setting flags, we return control to the user
736 program which calls a subroutine that supplies the address and type
737 of the next element, then comes back here to process it. */
740 formatted_transfer_scalar (st_parameter_dt *dtp, bt type, void *p, int len,
743 char scratch[SCRATCH_SIZE];
748 int consume_data_flag;
750 /* Change a complex data item into a pair of reals. */
752 n = (p == NULL) ? 0 : ((type != BT_COMPLEX) ? 1 : 2);
753 if (type == BT_COMPLEX)
759 /* If there's an EOR condition, we simulate finalizing the transfer
761 if (dtp->u.p.eor_condition)
764 /* Set this flag so that commas in reads cause the read to complete before
765 the entire field has been read. The next read field will start right after
766 the comma in the stream. (Set to 0 for character reads). */
767 dtp->u.p.sf_read_comma = 1;
769 dtp->u.p.line_buffer = scratch;
772 /* If reversion has occurred and there is another real data item,
773 then we have to move to the next record. */
774 if (dtp->u.p.reversion_flag && n > 0)
776 dtp->u.p.reversion_flag = 0;
777 next_record (dtp, 0);
780 consume_data_flag = 1 ;
781 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
784 f = next_format (dtp);
787 /* No data descriptors left. */
789 generate_error (&dtp->common, ERROR_FORMAT,
790 "Insufficient data descriptors in format after reversion");
794 /* Now discharge T, TR and X movements to the right. This is delayed
795 until a data producing format to suppress trailing spaces. */
798 if (dtp->u.p.mode == WRITING && dtp->u.p.skips != 0
799 && ((n>0 && ( t == FMT_I || t == FMT_B || t == FMT_O
800 || t == FMT_Z || t == FMT_F || t == FMT_E
801 || t == FMT_EN || t == FMT_ES || t == FMT_G
802 || t == FMT_L || t == FMT_A || t == FMT_D))
805 if (dtp->u.p.skips > 0)
807 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
808 dtp->u.p.max_pos = (int)(dtp->u.p.current_unit->recl
809 - dtp->u.p.current_unit->bytes_left);
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 -= (gfc_offset) dtp->u.p.skips;
816 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
819 bytes_used = (int)(dtp->u.p.current_unit->recl
820 - dtp->u.p.current_unit->bytes_left);
827 if (require_type (dtp, BT_INTEGER, type, f))
830 if (dtp->u.p.mode == READING)
831 read_decimal (dtp, f, p, len);
833 write_i (dtp, f, p, len);
840 if (require_type (dtp, BT_INTEGER, type, f))
843 if (dtp->u.p.mode == READING)
844 read_radix (dtp, f, p, len, 2);
846 write_b (dtp, f, p, len);
854 if (dtp->u.p.mode == READING)
855 read_radix (dtp, f, p, len, 8);
857 write_o (dtp, f, p, len);
865 if (dtp->u.p.mode == READING)
866 read_radix (dtp, f, p, len, 16);
868 write_z (dtp, f, p, len);
876 if (dtp->u.p.mode == READING)
877 read_a (dtp, f, p, len);
879 write_a (dtp, f, p, len);
887 if (dtp->u.p.mode == READING)
888 read_l (dtp, f, p, len);
890 write_l (dtp, f, p, len);
897 if (require_type (dtp, BT_REAL, type, f))
900 if (dtp->u.p.mode == READING)
901 read_f (dtp, f, p, len);
903 write_d (dtp, f, p, len);
910 if (require_type (dtp, BT_REAL, type, f))
913 if (dtp->u.p.mode == READING)
914 read_f (dtp, f, p, len);
916 write_e (dtp, f, p, len);
922 if (require_type (dtp, BT_REAL, type, f))
925 if (dtp->u.p.mode == READING)
926 read_f (dtp, f, p, len);
928 write_en (dtp, f, p, len);
935 if (require_type (dtp, BT_REAL, type, f))
938 if (dtp->u.p.mode == READING)
939 read_f (dtp, f, p, len);
941 write_es (dtp, f, p, len);
948 if (require_type (dtp, BT_REAL, type, f))
951 if (dtp->u.p.mode == READING)
952 read_f (dtp, f, p, len);
954 write_f (dtp, f, p, len);
961 if (dtp->u.p.mode == READING)
965 read_decimal (dtp, f, p, len);
968 read_l (dtp, f, p, len);
971 read_a (dtp, f, p, len);
974 read_f (dtp, f, p, len);
983 write_i (dtp, f, p, len);
986 write_l (dtp, f, p, len);
989 write_a (dtp, f, p, len);
992 write_d (dtp, f, p, len);
996 internal_error (&dtp->common,
997 "formatted_transfer(): Bad type");
1003 consume_data_flag = 0 ;
1004 if (dtp->u.p.mode == READING)
1006 format_error (dtp, f, "Constant string in input format");
1009 write_constant_string (dtp, f);
1012 /* Format codes that don't transfer data. */
1015 consume_data_flag = 0 ;
1017 pos = bytes_used + f->u.n + dtp->u.p.skips;
1018 dtp->u.p.skips = f->u.n + dtp->u.p.skips;
1019 dtp->u.p.pending_spaces = pos - dtp->u.p.max_pos;
1021 /* Writes occur just before the switch on f->format, above, so
1022 that trailing blanks are suppressed, unless we are doing a
1023 non-advancing write in which case we want to output the blanks
1025 if (dtp->u.p.mode == WRITING
1026 && dtp->u.p.advance_status == ADVANCE_NO)
1028 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
1029 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1031 if (dtp->u.p.mode == READING)
1032 read_x (dtp, f->u.n);
1038 if (f->format == FMT_TL)
1041 /* Handle the special case when no bytes have been used yet.
1042 Cannot go below zero. */
1043 if (bytes_used == 0)
1045 dtp->u.p.pending_spaces -= f->u.n;
1046 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces < 0 ? 0
1047 : dtp->u.p.pending_spaces;
1048 dtp->u.p.skips -= f->u.n;
1049 dtp->u.p.skips = dtp->u.p.skips < 0 ? 0 : dtp->u.p.skips;
1052 pos = bytes_used - f->u.n;
1056 consume_data_flag = 0;
1060 /* Standard 10.6.1.1: excessive left tabbing is reset to the
1061 left tab limit. We do not check if the position has gone
1062 beyond the end of record because a subsequent tab could
1063 bring us back again. */
1064 pos = pos < 0 ? 0 : pos;
1066 dtp->u.p.skips = dtp->u.p.skips + pos - bytes_used;
1067 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces
1068 + pos - dtp->u.p.max_pos;
1070 if (dtp->u.p.skips == 0)
1073 /* Writes occur just before the switch on f->format, above, so that
1074 trailing blanks are suppressed. */
1075 if (dtp->u.p.mode == READING)
1077 /* Adjust everything for end-of-record condition */
1078 if (dtp->u.p.sf_seen_eor && !is_internal_unit (dtp))
1080 if (dtp->u.p.sf_seen_eor == 2)
1082 /* The EOR was a CRLF (two bytes wide). */
1083 dtp->u.p.current_unit->bytes_left -= 2;
1084 dtp->u.p.skips -= 2;
1088 /* The EOR marker was only one byte wide. */
1089 dtp->u.p.current_unit->bytes_left--;
1093 dtp->u.p.sf_seen_eor = 0;
1095 if (dtp->u.p.skips < 0)
1097 move_pos_offset (dtp->u.p.current_unit->s, dtp->u.p.skips);
1098 dtp->u.p.current_unit->bytes_left
1099 -= (gfc_offset) dtp->u.p.skips;
1100 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1103 read_x (dtp, dtp->u.p.skips);
1109 consume_data_flag = 0 ;
1110 dtp->u.p.sign_status = SIGN_S;
1114 consume_data_flag = 0 ;
1115 dtp->u.p.sign_status = SIGN_SS;
1119 consume_data_flag = 0 ;
1120 dtp->u.p.sign_status = SIGN_SP;
1124 consume_data_flag = 0 ;
1125 dtp->u.p.blank_status = BLANK_NULL;
1129 consume_data_flag = 0 ;
1130 dtp->u.p.blank_status = BLANK_ZERO;
1134 consume_data_flag = 0 ;
1135 dtp->u.p.scale_factor = f->u.k;
1139 consume_data_flag = 0 ;
1140 dtp->u.p.seen_dollar = 1;
1144 consume_data_flag = 0 ;
1145 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1146 next_record (dtp, 0);
1150 /* A colon descriptor causes us to exit this loop (in
1151 particular preventing another / descriptor from being
1152 processed) unless there is another data item to be
1154 consume_data_flag = 0 ;
1160 internal_error (&dtp->common, "Bad format node");
1163 /* Free a buffer that we had to allocate during a sequential
1164 formatted read of a block that was larger than the static
1167 if (dtp->u.p.line_buffer != scratch)
1169 free_mem (dtp->u.p.line_buffer);
1170 dtp->u.p.line_buffer = scratch;
1173 /* Adjust the item count and data pointer. */
1175 if ((consume_data_flag > 0) && (n > 0))
1178 p = ((char *) p) + size;
1181 if (dtp->u.p.mode == READING)
1184 pos = (int)(dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left);
1185 dtp->u.p.max_pos = (dtp->u.p.max_pos > pos) ? dtp->u.p.max_pos : pos;
1191 /* Come here when we need a data descriptor but don't have one. We
1192 push the current format node back onto the input, then return and
1193 let the user program call us back with the data. */
1195 unget_format (dtp, f);
1199 formatted_transfer (st_parameter_dt *dtp, bt type, void *p, int kind,
1200 size_t size, size_t nelems)
1207 /* Big loop over all the elements. */
1208 for (elem = 0; elem < nelems; elem++)
1210 dtp->u.p.item_count++;
1211 formatted_transfer_scalar (dtp, type, tmp + size*elem, kind, size);
1217 /* Data transfer entry points. The type of the data entity is
1218 implicit in the subroutine call. This prevents us from having to
1219 share a common enum with the compiler. */
1222 transfer_integer (st_parameter_dt *dtp, void *p, int kind)
1224 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1226 dtp->u.p.transfer (dtp, BT_INTEGER, p, kind, kind, 1);
1231 transfer_real (st_parameter_dt *dtp, void *p, int kind)
1234 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1236 size = size_from_real_kind (kind);
1237 dtp->u.p.transfer (dtp, BT_REAL, p, kind, size, 1);
1242 transfer_logical (st_parameter_dt *dtp, void *p, int kind)
1244 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1246 dtp->u.p.transfer (dtp, BT_LOGICAL, p, kind, kind, 1);
1251 transfer_character (st_parameter_dt *dtp, void *p, int len)
1253 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1255 /* Currently we support only 1 byte chars, and the library is a bit
1256 confused of character kind vs. length, so we kludge it by setting
1258 dtp->u.p.transfer (dtp, BT_CHARACTER, p, len, len, 1);
1263 transfer_complex (st_parameter_dt *dtp, void *p, int kind)
1266 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1268 size = size_from_complex_kind (kind);
1269 dtp->u.p.transfer (dtp, BT_COMPLEX, p, kind, size, 1);
1274 transfer_array (st_parameter_dt *dtp, gfc_array_char *desc, int kind,
1275 gfc_charlen_type charlen)
1277 index_type count[GFC_MAX_DIMENSIONS];
1278 index_type extent[GFC_MAX_DIMENSIONS];
1279 index_type stride[GFC_MAX_DIMENSIONS];
1280 index_type stride0, rank, size, type, n;
1285 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1288 type = GFC_DESCRIPTOR_TYPE (desc);
1289 size = GFC_DESCRIPTOR_SIZE (desc);
1291 /* FIXME: What a kludge: Array descriptors and the IO library use
1292 different enums for types. */
1295 case GFC_DTYPE_UNKNOWN:
1296 iotype = BT_NULL; /* Is this correct? */
1298 case GFC_DTYPE_INTEGER:
1299 iotype = BT_INTEGER;
1301 case GFC_DTYPE_LOGICAL:
1302 iotype = BT_LOGICAL;
1304 case GFC_DTYPE_REAL:
1307 case GFC_DTYPE_COMPLEX:
1308 iotype = BT_COMPLEX;
1310 case GFC_DTYPE_CHARACTER:
1311 iotype = BT_CHARACTER;
1312 /* FIXME: Currently dtype contains the charlen, which is
1313 clobbered if charlen > 2**24. That's why we use a separate
1314 argument for the charlen. However, if we want to support
1315 non-8-bit charsets we need to fix dtype to contain
1316 sizeof(chartype) and fix the code below. */
1320 case GFC_DTYPE_DERIVED:
1321 internal_error (&dtp->common,
1322 "Derived type I/O should have been handled via the frontend.");
1325 internal_error (&dtp->common, "transfer_array(): Bad type");
1328 rank = GFC_DESCRIPTOR_RANK (desc);
1329 for (n = 0; n < rank; n++)
1332 stride[n] = desc->dim[n].stride;
1333 extent[n] = desc->dim[n].ubound + 1 - desc->dim[n].lbound;
1335 /* If the extent of even one dimension is zero, then the entire
1336 array section contains zero elements, so we return. */
1341 stride0 = stride[0];
1343 /* If the innermost dimension has stride 1, we can do the transfer
1344 in contiguous chunks. */
1350 data = GFC_DESCRIPTOR_DATA (desc);
1354 dtp->u.p.transfer (dtp, iotype, data, kind, size, tsize);
1355 data += stride0 * size * tsize;
1358 while (count[n] == extent[n])
1361 data -= stride[n] * extent[n] * size;
1371 data += stride[n] * size;
1378 /* Preposition a sequential unformatted file while reading. */
1381 us_read (st_parameter_dt *dtp)
1390 if (dtp->u.p.current_unit->endfile == AT_ENDFILE)
1393 if (compile_options.record_marker == 0)
1394 n = sizeof (gfc_offset);
1396 n = compile_options.record_marker;
1400 p = salloc_r (dtp->u.p.current_unit->s, &n);
1404 dtp->u.p.current_unit->endfile = AT_ENDFILE;
1405 return; /* end of file */
1408 if (p == NULL || n != nr)
1410 generate_error (&dtp->common, ERROR_BAD_US, NULL);
1414 /* Only CONVERT_NATIVE and CONVERT_SWAP are valid here. */
1415 if (dtp->u.p.current_unit->flags.convert == CONVERT_NATIVE)
1417 switch (compile_options.record_marker)
1420 memcpy (&i, p, sizeof(gfc_offset));
1423 case sizeof(GFC_INTEGER_4):
1424 memcpy (&i4, p, sizeof (i4));
1428 case sizeof(GFC_INTEGER_8):
1429 memcpy (&i8, p, sizeof (i8));
1434 runtime_error ("Illegal value for record marker");
1439 switch (compile_options.record_marker)
1442 reverse_memcpy (&i, p, sizeof(gfc_offset));
1445 case sizeof(GFC_INTEGER_4):
1446 reverse_memcpy (&i4, p, sizeof (i4));
1450 case sizeof(GFC_INTEGER_8):
1451 reverse_memcpy (&i8, p, sizeof (i8));
1456 runtime_error ("Illegal value for record marker");
1460 dtp->u.p.current_unit->bytes_left = i;
1464 /* Preposition a sequential unformatted file while writing. This
1465 amount to writing a bogus length that will be filled in later. */
1468 us_write (st_parameter_dt *dtp)
1475 if (compile_options.record_marker == 0)
1476 nbytes = sizeof (gfc_offset);
1478 nbytes = compile_options.record_marker ;
1480 if (swrite (dtp->u.p.current_unit->s, &dummy, &nbytes) != 0)
1481 generate_error (&dtp->common, ERROR_OS, NULL);
1483 /* For sequential unformatted, we write until we have more bytes
1484 than can fit in the record markers. If disk space runs out first,
1485 it will error on the write. */
1486 dtp->u.p.current_unit->recl = max_offset;
1488 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
1492 /* Position to the next record prior to transfer. We are assumed to
1493 be before the next record. We also calculate the bytes in the next
1497 pre_position (st_parameter_dt *dtp)
1499 if (dtp->u.p.current_unit->current_record)
1500 return; /* Already positioned. */
1502 switch (current_mode (dtp))
1504 case FORMATTED_STREAM:
1505 case UNFORMATTED_STREAM:
1506 /* There are no records with stream I/O. Set the default position
1507 to the beginning of the file if no position was specified. */
1508 if ((dtp->common.flags & IOPARM_DT_HAS_REC) == 0)
1512 case UNFORMATTED_SEQUENTIAL:
1513 if (dtp->u.p.mode == READING)
1520 case FORMATTED_SEQUENTIAL:
1521 case FORMATTED_DIRECT:
1522 case UNFORMATTED_DIRECT:
1523 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
1527 dtp->u.p.current_unit->current_record = 1;
1531 /* Initialize things for a data transfer. This code is common for
1532 both reading and writing. */
1535 data_transfer_init (st_parameter_dt *dtp, int read_flag)
1537 unit_flags u_flags; /* Used for creating a unit if needed. */
1538 GFC_INTEGER_4 cf = dtp->common.flags;
1539 namelist_info *ionml;
1541 ionml = ((cf & IOPARM_DT_IONML_SET) != 0) ? dtp->u.p.ionml : NULL;
1542 memset (&dtp->u.p, 0, sizeof (dtp->u.p));
1543 dtp->u.p.ionml = ionml;
1544 dtp->u.p.mode = read_flag ? READING : WRITING;
1546 if ((cf & IOPARM_DT_HAS_SIZE) != 0)
1547 dtp->u.p.size_used = 0; /* Initialize the count. */
1549 dtp->u.p.current_unit = get_unit (dtp, 1);
1550 if (dtp->u.p.current_unit->s == NULL)
1551 { /* Open the unit with some default flags. */
1552 st_parameter_open opp;
1555 if (dtp->common.unit < 0)
1557 close_unit (dtp->u.p.current_unit);
1558 dtp->u.p.current_unit = NULL;
1559 generate_error (&dtp->common, ERROR_BAD_OPTION,
1560 "Bad unit number in OPEN statement");
1563 memset (&u_flags, '\0', sizeof (u_flags));
1564 u_flags.access = ACCESS_SEQUENTIAL;
1565 u_flags.action = ACTION_READWRITE;
1567 /* Is it unformatted? */
1568 if (!(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT
1569 | IOPARM_DT_IONML_SET)))
1570 u_flags.form = FORM_UNFORMATTED;
1572 u_flags.form = FORM_UNSPECIFIED;
1574 u_flags.delim = DELIM_UNSPECIFIED;
1575 u_flags.blank = BLANK_UNSPECIFIED;
1576 u_flags.pad = PAD_UNSPECIFIED;
1577 u_flags.status = STATUS_UNKNOWN;
1579 conv = get_unformatted_convert (dtp->common.unit);
1581 if (conv == CONVERT_NONE)
1582 conv = compile_options.convert;
1584 /* We use l8_to_l4_offset, which is 0 on little-endian machines
1585 and 1 on big-endian machines. */
1588 case CONVERT_NATIVE:
1593 conv = l8_to_l4_offset ? CONVERT_NATIVE : CONVERT_SWAP;
1596 case CONVERT_LITTLE:
1597 conv = l8_to_l4_offset ? CONVERT_SWAP : CONVERT_NATIVE;
1601 internal_error (&opp.common, "Illegal value for CONVERT");
1605 u_flags.convert = conv;
1607 opp.common = dtp->common;
1608 opp.common.flags &= IOPARM_COMMON_MASK;
1609 dtp->u.p.current_unit = new_unit (&opp, dtp->u.p.current_unit, &u_flags);
1610 dtp->common.flags &= ~IOPARM_COMMON_MASK;
1611 dtp->common.flags |= (opp.common.flags & IOPARM_COMMON_MASK);
1612 if (dtp->u.p.current_unit == NULL)
1616 /* Check the action. */
1618 if (read_flag && dtp->u.p.current_unit->flags.action == ACTION_WRITE)
1619 generate_error (&dtp->common, ERROR_BAD_ACTION,
1620 "Cannot read from file opened for WRITE");
1622 if (!read_flag && dtp->u.p.current_unit->flags.action == ACTION_READ)
1623 generate_error (&dtp->common, ERROR_BAD_ACTION,
1624 "Cannot write to file opened for READ");
1626 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1629 dtp->u.p.first_item = 1;
1631 /* Check the format. */
1633 if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
1636 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1639 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED
1640 && (cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
1642 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1643 "Format present for UNFORMATTED data transfer");
1645 if ((cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0 && dtp->u.p.ionml != NULL)
1647 if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
1648 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1649 "A format cannot be specified with a namelist");
1651 else if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
1652 !(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT)))
1653 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1654 "Missing format for FORMATTED data transfer");
1656 if (is_internal_unit (dtp)
1657 && dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
1658 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1659 "Internal file cannot be accessed by UNFORMATTED data transfer");
1661 /* Check the record or position number. */
1663 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT
1664 && (cf & IOPARM_DT_HAS_REC) == 0)
1666 generate_error (&dtp->common, ERROR_MISSING_OPTION,
1667 "Direct access data transfer requires record number");
1671 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL
1672 && (cf & IOPARM_DT_HAS_REC) != 0)
1674 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1675 "Record number not allowed for sequential access data transfer");
1679 /* Process the ADVANCE option. */
1681 dtp->u.p.advance_status
1682 = !(cf & IOPARM_DT_HAS_ADVANCE) ? ADVANCE_UNSPECIFIED :
1683 find_option (&dtp->common, dtp->advance, dtp->advance_len, advance_opt,
1684 "Bad ADVANCE parameter in data transfer statement");
1686 if (dtp->u.p.advance_status != ADVANCE_UNSPECIFIED)
1688 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
1689 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1690 "ADVANCE specification conflicts with sequential access");
1692 if (is_internal_unit (dtp))
1693 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1694 "ADVANCE specification conflicts with internal file");
1696 if ((cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
1697 != IOPARM_DT_HAS_FORMAT)
1698 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1699 "ADVANCE specification requires an explicit format");
1704 if ((cf & IOPARM_EOR) != 0 && dtp->u.p.advance_status != ADVANCE_NO)
1705 generate_error (&dtp->common, ERROR_MISSING_OPTION,
1706 "EOR specification requires an ADVANCE specification of NO");
1708 if ((cf & IOPARM_DT_HAS_SIZE) != 0 && dtp->u.p.advance_status != ADVANCE_NO)
1709 generate_error (&dtp->common, ERROR_MISSING_OPTION,
1710 "SIZE specification requires an ADVANCE specification of NO");
1714 { /* Write constraints. */
1715 if ((cf & IOPARM_END) != 0)
1716 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1717 "END specification cannot appear in a write statement");
1719 if ((cf & IOPARM_EOR) != 0)
1720 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1721 "EOR specification cannot appear in a write statement");
1723 if ((cf & IOPARM_DT_HAS_SIZE) != 0)
1724 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1725 "SIZE specification cannot appear in a write statement");
1728 if (dtp->u.p.advance_status == ADVANCE_UNSPECIFIED)
1729 dtp->u.p.advance_status = ADVANCE_YES;
1730 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1733 /* Sanity checks on the record number. */
1734 if ((cf & IOPARM_DT_HAS_REC) != 0)
1738 generate_error (&dtp->common, ERROR_BAD_OPTION,
1739 "Record number must be positive");
1743 if (dtp->rec >= dtp->u.p.current_unit->maxrec)
1745 generate_error (&dtp->common, ERROR_BAD_OPTION,
1746 "Record number too large");
1750 /* Check to see if we might be reading what we wrote before */
1752 if (dtp->u.p.mode == READING
1753 && dtp->u.p.current_unit->mode == WRITING
1754 && !is_internal_unit (dtp))
1755 flush(dtp->u.p.current_unit->s);
1757 /* Check whether the record exists to be read. Only
1758 a partial record needs to exist. */
1760 if (dtp->u.p.mode == READING && (dtp->rec -1)
1761 * dtp->u.p.current_unit->recl >= file_length (dtp->u.p.current_unit->s))
1763 generate_error (&dtp->common, ERROR_BAD_OPTION,
1764 "Non-existing record number");
1768 /* Position the file. */
1769 if (sseek (dtp->u.p.current_unit->s, (gfc_offset) (dtp->rec - 1)
1770 * dtp->u.p.current_unit->recl) == FAILURE)
1772 generate_error (&dtp->common, ERROR_OS, NULL);
1777 /* Overwriting an existing sequential file ?
1778 it is always safe to truncate the file on the first write */
1779 if (dtp->u.p.mode == WRITING
1780 && dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL
1781 && dtp->u.p.current_unit->last_record == 0
1782 && !is_preconnected(dtp->u.p.current_unit->s))
1783 struncate(dtp->u.p.current_unit->s);
1785 /* Bugware for badly written mixed C-Fortran I/O. */
1786 flush_if_preconnected(dtp->u.p.current_unit->s);
1788 dtp->u.p.current_unit->mode = dtp->u.p.mode;
1790 /* Set the initial value of flags. */
1792 dtp->u.p.blank_status = dtp->u.p.current_unit->flags.blank;
1793 dtp->u.p.sign_status = SIGN_S;
1797 /* Set up the subroutine that will handle the transfers. */
1801 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
1802 dtp->u.p.transfer = unformatted_read;
1805 if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
1806 dtp->u.p.transfer = list_formatted_read;
1808 dtp->u.p.transfer = formatted_transfer;
1813 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
1814 dtp->u.p.transfer = unformatted_write;
1817 if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
1818 dtp->u.p.transfer = list_formatted_write;
1820 dtp->u.p.transfer = formatted_transfer;
1824 /* Make sure that we don't do a read after a nonadvancing write. */
1828 if (dtp->u.p.current_unit->read_bad && !is_stream_io (dtp))
1830 generate_error (&dtp->common, ERROR_BAD_OPTION,
1831 "Cannot READ after a nonadvancing WRITE");
1837 if (dtp->u.p.advance_status == ADVANCE_YES && !dtp->u.p.seen_dollar)
1838 dtp->u.p.current_unit->read_bad = 1;
1841 /* Start the data transfer if we are doing a formatted transfer. */
1842 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
1843 && ((cf & (IOPARM_DT_LIST_FORMAT | IOPARM_DT_HAS_NAMELIST_NAME)) == 0)
1844 && dtp->u.p.ionml == NULL)
1845 formatted_transfer (dtp, 0, NULL, 0, 0, 1);
1848 /* Initialize an array_loop_spec given the array descriptor. The function
1849 returns the index of the last element of the array. */
1852 init_loop_spec (gfc_array_char *desc, array_loop_spec *ls)
1854 int rank = GFC_DESCRIPTOR_RANK(desc);
1859 for (i=0; i<rank; i++)
1862 ls[i].start = desc->dim[i].lbound;
1863 ls[i].end = desc->dim[i].ubound;
1864 ls[i].step = desc->dim[i].stride;
1866 index += (desc->dim[i].ubound - desc->dim[i].lbound)
1867 * desc->dim[i].stride;
1872 /* Determine the index to the next record in an internal unit array by
1873 by incrementing through the array_loop_spec. TODO: Implement handling
1874 negative strides. */
1877 next_array_record (st_parameter_dt *dtp, array_loop_spec *ls)
1885 for (i = 0; i < dtp->u.p.current_unit->rank; i++)
1890 if (ls[i].idx > ls[i].end)
1892 ls[i].idx = ls[i].start;
1898 index = index + (ls[i].idx - 1) * ls[i].step;
1903 /* Space to the next record for read mode. If the file is not
1904 seekable, we read MAX_READ chunks until we get to the right
1907 #define MAX_READ 4096
1910 next_record_r (st_parameter_dt *dtp)
1912 gfc_offset new, record;
1913 int bytes_left, rlength, length;
1916 switch (current_mode (dtp))
1918 /* No records in STREAM I/O. */
1919 case FORMATTED_STREAM:
1920 case UNFORMATTED_STREAM:
1923 case UNFORMATTED_SEQUENTIAL:
1925 /* Skip over tail */
1926 dtp->u.p.current_unit->bytes_left +=
1927 compile_options.record_marker == 0 ?
1928 sizeof (gfc_offset) : compile_options.record_marker;
1930 /* Fall through... */
1932 case FORMATTED_DIRECT:
1933 case UNFORMATTED_DIRECT:
1934 if (dtp->u.p.current_unit->bytes_left == 0)
1937 if (is_seekable (dtp->u.p.current_unit->s))
1939 new = file_position (dtp->u.p.current_unit->s)
1940 + dtp->u.p.current_unit->bytes_left;
1942 /* Direct access files do not generate END conditions,
1944 if (sseek (dtp->u.p.current_unit->s, new) == FAILURE)
1945 generate_error (&dtp->common, ERROR_OS, NULL);
1949 { /* Seek by reading data. */
1950 while (dtp->u.p.current_unit->bytes_left > 0)
1952 rlength = length = (MAX_READ > dtp->u.p.current_unit->bytes_left) ?
1953 MAX_READ : dtp->u.p.current_unit->bytes_left;
1955 p = salloc_r (dtp->u.p.current_unit->s, &rlength);
1958 generate_error (&dtp->common, ERROR_OS, NULL);
1962 dtp->u.p.current_unit->bytes_left -= length;
1967 case FORMATTED_SEQUENTIAL:
1969 /* sf_read has already terminated input because of an '\n' */
1970 if (dtp->u.p.sf_seen_eor)
1972 dtp->u.p.sf_seen_eor = 0;
1976 if (is_internal_unit (dtp))
1978 if (is_array_io (dtp))
1980 record = next_array_record (dtp, dtp->u.p.current_unit->ls);
1982 /* Now seek to this record. */
1983 record = record * dtp->u.p.current_unit->recl;
1984 if (sseek (dtp->u.p.current_unit->s, record) == FAILURE)
1986 generate_error (&dtp->common, ERROR_INTERNAL_UNIT, NULL);
1989 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
1993 bytes_left = (int) dtp->u.p.current_unit->bytes_left;
1994 p = salloc_r (dtp->u.p.current_unit->s, &bytes_left);
1996 dtp->u.p.current_unit->bytes_left
1997 = dtp->u.p.current_unit->recl;
2003 p = salloc_r (dtp->u.p.current_unit->s, &length);
2007 generate_error (&dtp->common, ERROR_OS, NULL);
2013 dtp->u.p.current_unit->endfile = AT_ENDFILE;
2022 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
2023 test_endfile (dtp->u.p.current_unit);
2027 /* Small utility function to write a record marker, taking care of
2028 byte swapping and of choosing the correct size. */
2031 write_us_marker (st_parameter_dt *dtp, const gfc_offset buf)
2036 char p[sizeof (GFC_INTEGER_8)];
2038 if (compile_options.record_marker == 0)
2039 len = sizeof (gfc_offset);
2041 len = compile_options.record_marker;
2043 /* Only CONVERT_NATIVE and CONVERT_SWAP are valid here. */
2044 if (dtp->u.p.current_unit->flags.convert == CONVERT_NATIVE)
2046 switch (compile_options.record_marker)
2049 return swrite (dtp->u.p.current_unit->s, &buf, &len);
2052 case sizeof (GFC_INTEGER_4):
2054 return swrite (dtp->u.p.current_unit->s, &buf4, &len);
2057 case sizeof (GFC_INTEGER_8):
2059 return swrite (dtp->u.p.current_unit->s, &buf8, &len);
2063 runtime_error ("Illegal value for record marker");
2069 switch (compile_options.record_marker)
2072 reverse_memcpy (p, &buf, sizeof (gfc_offset));
2073 return swrite (dtp->u.p.current_unit->s, p, &len);
2076 case sizeof (GFC_INTEGER_4):
2078 reverse_memcpy (p, &buf4, sizeof (GFC_INTEGER_4));
2079 return swrite (dtp->u.p.current_unit->s, p, &len);
2082 case sizeof (GFC_INTEGER_8):
2084 reverse_memcpy (p, &buf8, sizeof (GFC_INTEGER_4));
2085 return swrite (dtp->u.p.current_unit->s, p, &len);
2089 runtime_error ("Illegal value for record marker");
2097 /* Position to the next record in write mode. */
2100 next_record_w (st_parameter_dt *dtp, int done)
2102 gfc_offset c, m, record, max_pos;
2105 size_t record_marker;
2107 /* Zero counters for X- and T-editing. */
2108 max_pos = dtp->u.p.max_pos;
2109 dtp->u.p.max_pos = dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
2111 switch (current_mode (dtp))
2113 /* No records in STREAM I/O. */
2114 case FORMATTED_STREAM:
2115 case UNFORMATTED_STREAM:
2118 case FORMATTED_DIRECT:
2119 if (dtp->u.p.current_unit->bytes_left == 0)
2122 if (sset (dtp->u.p.current_unit->s, ' ',
2123 dtp->u.p.current_unit->bytes_left) == FAILURE)
2128 case UNFORMATTED_DIRECT:
2129 if (sfree (dtp->u.p.current_unit->s) == FAILURE)
2133 case UNFORMATTED_SEQUENTIAL:
2134 /* Bytes written. */
2135 m = dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left;
2136 c = file_position (dtp->u.p.current_unit->s);
2138 /* Write the length tail. */
2140 if (write_us_marker (dtp, m) != 0)
2143 if (compile_options.record_marker == 4)
2144 record_marker = sizeof(GFC_INTEGER_4);
2146 record_marker = sizeof (gfc_offset);
2148 /* Seek to the head and overwrite the bogus length with the real
2151 if (sseek (dtp->u.p.current_unit->s, c - m - record_marker)
2155 if (write_us_marker (dtp, m) != 0)
2158 /* Seek past the end of the current record. */
2160 if (sseek (dtp->u.p.current_unit->s, c + record_marker) == FAILURE)
2165 case FORMATTED_SEQUENTIAL:
2167 if (is_internal_unit (dtp))
2169 if (is_array_io (dtp))
2171 length = (int) dtp->u.p.current_unit->bytes_left;
2173 /* If the farthest position reached is greater than current
2174 position, adjust the position and set length to pad out
2175 whats left. Otherwise just pad whats left.
2176 (for character array unit) */
2177 m = dtp->u.p.current_unit->recl
2178 - dtp->u.p.current_unit->bytes_left;
2181 length = (int) (max_pos - m);
2182 p = salloc_w (dtp->u.p.current_unit->s, &length);
2183 length = (int) (dtp->u.p.current_unit->recl - max_pos);
2186 if (sset (dtp->u.p.current_unit->s, ' ', length) == FAILURE)
2188 generate_error (&dtp->common, ERROR_END, NULL);
2192 /* Now that the current record has been padded out,
2193 determine where the next record in the array is. */
2194 record = next_array_record (dtp, dtp->u.p.current_unit->ls);
2196 dtp->u.p.current_unit->endfile = AT_ENDFILE;
2198 /* Now seek to this record */
2199 record = record * dtp->u.p.current_unit->recl;
2201 if (sseek (dtp->u.p.current_unit->s, record) == FAILURE)
2203 generate_error (&dtp->common, ERROR_INTERNAL_UNIT, NULL);
2207 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2213 /* If this is the last call to next_record move to the farthest
2214 position reached and set length to pad out the remainder
2215 of the record. (for character scaler unit) */
2218 m = dtp->u.p.current_unit->recl
2219 - dtp->u.p.current_unit->bytes_left;
2222 length = (int) (max_pos - m);
2223 p = salloc_w (dtp->u.p.current_unit->s, &length);
2224 length = (int) (dtp->u.p.current_unit->recl - max_pos);
2227 length = (int) dtp->u.p.current_unit->bytes_left;
2229 if (sset (dtp->u.p.current_unit->s, ' ', length) == FAILURE)
2231 generate_error (&dtp->common, ERROR_END, NULL);
2238 if (dtp->u.p.current_unit->bytes_left == 0)
2241 /* If this is the last call to next_record move to the farthest
2242 position reached in preparation for completing the record.
2246 m = dtp->u.p.current_unit->recl -
2247 dtp->u.p.current_unit->bytes_left;
2250 length = (int) (max_pos - m);
2251 p = salloc_w (dtp->u.p.current_unit->s, &length);
2255 const char crlf[] = "\r\n";
2261 if (swrite (dtp->u.p.current_unit->s, &crlf[2-len], &len) != 0)
2268 generate_error (&dtp->common, ERROR_OS, NULL);
2273 /* Position to the next record, which means moving to the end of the
2274 current record. This can happen under several different
2275 conditions. If the done flag is not set, we get ready to process
2279 next_record (st_parameter_dt *dtp, int done)
2281 if (is_stream_io (dtp))
2284 gfc_offset fp; /* File position. */
2286 dtp->u.p.current_unit->read_bad = 0;
2288 if (dtp->u.p.mode == READING)
2289 next_record_r (dtp);
2291 next_record_w (dtp, done);
2293 /* keep position up to date for INQUIRE */
2294 dtp->u.p.current_unit->flags.position = POSITION_ASIS;
2295 dtp->u.p.current_unit->current_record = 0;
2296 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
2298 fp = file_position (dtp->u.p.current_unit->s);
2299 /* Calculate next record, rounding up partial records. */
2300 dtp->u.p.current_unit->last_record = (fp + dtp->u.p.current_unit->recl - 1)
2301 / dtp->u.p.current_unit->recl;
2304 dtp->u.p.current_unit->last_record++;
2311 /* Finalize the current data transfer. For a nonadvancing transfer,
2312 this means advancing to the next record. For internal units close the
2313 stream associated with the unit. */
2316 finalize_transfer (st_parameter_dt *dtp)
2319 GFC_INTEGER_4 cf = dtp->common.flags;
2321 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
2322 *dtp->size = (GFC_INTEGER_4) dtp->u.p.size_used;
2324 if (dtp->u.p.eor_condition)
2326 generate_error (&dtp->common, ERROR_EOR, NULL);
2330 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2333 if ((dtp->u.p.ionml != NULL)
2334 && (cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0)
2336 if ((cf & IOPARM_DT_NAMELIST_READ_MODE) != 0)
2337 namelist_read (dtp);
2339 namelist_write (dtp);
2342 dtp->u.p.transfer = NULL;
2343 if (dtp->u.p.current_unit == NULL)
2346 dtp->u.p.eof_jump = &eof_jump;
2347 if (setjmp (eof_jump))
2349 generate_error (&dtp->common, ERROR_END, NULL);
2353 if ((cf & IOPARM_DT_LIST_FORMAT) != 0 && dtp->u.p.mode == READING)
2354 finish_list_read (dtp);
2355 else if (!is_stream_io (dtp))
2357 dtp->u.p.current_unit->current_record = 0;
2358 if (dtp->u.p.advance_status == ADVANCE_NO || dtp->u.p.seen_dollar)
2360 /* Most systems buffer lines, so force the partial record
2361 to be written out. */
2362 if (!is_internal_unit (dtp))
2363 flush (dtp->u.p.current_unit->s);
2364 dtp->u.p.seen_dollar = 0;
2367 next_record (dtp, 1);
2371 flush (dtp->u.p.current_unit->s);
2372 dtp->u.p.current_unit->last_record = dtp->rec;
2375 sfree (dtp->u.p.current_unit->s);
2378 /* Transfer function for IOLENGTH. It doesn't actually do any
2379 data transfer, it just updates the length counter. */
2382 iolength_transfer (st_parameter_dt *dtp, bt type __attribute__((unused)),
2383 void *dest __attribute__ ((unused)),
2384 int kind __attribute__((unused)),
2385 size_t size, size_t nelems)
2387 if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
2388 *dtp->iolength += (GFC_INTEGER_4) size * nelems;
2392 /* Initialize the IOLENGTH data transfer. This function is in essence
2393 a very much simplified version of data_transfer_init(), because it
2394 doesn't have to deal with units at all. */
2397 iolength_transfer_init (st_parameter_dt *dtp)
2399 if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
2402 memset (&dtp->u.p, 0, sizeof (dtp->u.p));
2404 /* Set up the subroutine that will handle the transfers. */
2406 dtp->u.p.transfer = iolength_transfer;
2410 /* Library entry point for the IOLENGTH form of the INQUIRE
2411 statement. The IOLENGTH form requires no I/O to be performed, but
2412 it must still be a runtime library call so that we can determine
2413 the iolength for dynamic arrays and such. */
2415 extern void st_iolength (st_parameter_dt *);
2416 export_proto(st_iolength);
2419 st_iolength (st_parameter_dt *dtp)
2421 library_start (&dtp->common);
2422 iolength_transfer_init (dtp);
2425 extern void st_iolength_done (st_parameter_dt *);
2426 export_proto(st_iolength_done);
2429 st_iolength_done (st_parameter_dt *dtp __attribute__((unused)))
2432 if (dtp->u.p.scratch != NULL)
2433 free_mem (dtp->u.p.scratch);
2438 /* The READ statement. */
2440 extern void st_read (st_parameter_dt *);
2441 export_proto(st_read);
2444 st_read (st_parameter_dt *dtp)
2446 library_start (&dtp->common);
2448 data_transfer_init (dtp, 1);
2450 /* Handle complications dealing with the endfile record. It is
2451 significant that this is the only place where ERROR_END is
2452 generated. Reading an end of file elsewhere is either end of
2453 record or an I/O error. */
2455 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
2456 switch (dtp->u.p.current_unit->endfile)
2462 if (!is_internal_unit (dtp))
2464 generate_error (&dtp->common, ERROR_END, NULL);
2465 dtp->u.p.current_unit->endfile = AFTER_ENDFILE;
2466 dtp->u.p.current_unit->current_record = 0;
2471 generate_error (&dtp->common, ERROR_ENDFILE, NULL);
2472 dtp->u.p.current_unit->current_record = 0;
2477 extern void st_read_done (st_parameter_dt *);
2478 export_proto(st_read_done);
2481 st_read_done (st_parameter_dt *dtp)
2483 finalize_transfer (dtp);
2484 free_format_data (dtp);
2486 if (dtp->u.p.scratch != NULL)
2487 free_mem (dtp->u.p.scratch);
2488 if (dtp->u.p.current_unit != NULL)
2489 unlock_unit (dtp->u.p.current_unit);
2491 free_internal_unit (dtp);
2496 extern void st_write (st_parameter_dt *);
2497 export_proto(st_write);
2500 st_write (st_parameter_dt *dtp)
2502 library_start (&dtp->common);
2503 data_transfer_init (dtp, 0);
2506 extern void st_write_done (st_parameter_dt *);
2507 export_proto(st_write_done);
2510 st_write_done (st_parameter_dt *dtp)
2512 finalize_transfer (dtp);
2514 /* Deal with endfile conditions associated with sequential files. */
2516 if (dtp->u.p.current_unit != NULL
2517 && dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
2518 switch (dtp->u.p.current_unit->endfile)
2520 case AT_ENDFILE: /* Remain at the endfile record. */
2524 dtp->u.p.current_unit->endfile = AT_ENDFILE; /* Just at it now. */
2528 /* Get rid of whatever is after this record. */
2529 if (!is_internal_unit (dtp))
2531 flush (dtp->u.p.current_unit->s);
2532 if (struncate (dtp->u.p.current_unit->s) == FAILURE)
2533 generate_error (&dtp->common, ERROR_OS, NULL);
2535 dtp->u.p.current_unit->endfile = AT_ENDFILE;
2539 free_format_data (dtp);
2541 if (dtp->u.p.scratch != NULL)
2542 free_mem (dtp->u.p.scratch);
2543 if (dtp->u.p.current_unit != NULL)
2544 unlock_unit (dtp->u.p.current_unit);
2546 free_internal_unit (dtp);
2551 /* Receives the scalar information for namelist objects and stores it
2552 in a linked list of namelist_info types. */
2554 extern void st_set_nml_var (st_parameter_dt *dtp, void *, char *,
2555 GFC_INTEGER_4, gfc_charlen_type, GFC_INTEGER_4);
2556 export_proto(st_set_nml_var);
2560 st_set_nml_var (st_parameter_dt *dtp, void * var_addr, char * var_name,
2561 GFC_INTEGER_4 len, gfc_charlen_type string_length,
2562 GFC_INTEGER_4 dtype)
2564 namelist_info *t1 = NULL;
2567 nml = (namelist_info*) get_mem (sizeof (namelist_info));
2569 nml->mem_pos = var_addr;
2571 nml->var_name = (char*) get_mem (strlen (var_name) + 1);
2572 strcpy (nml->var_name, var_name);
2574 nml->len = (int) len;
2575 nml->string_length = (index_type) string_length;
2577 nml->var_rank = (int) (dtype & GFC_DTYPE_RANK_MASK);
2578 nml->size = (index_type) (dtype >> GFC_DTYPE_SIZE_SHIFT);
2579 nml->type = (bt) ((dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT);
2581 if (nml->var_rank > 0)
2583 nml->dim = (descriptor_dimension*)
2584 get_mem (nml->var_rank * sizeof (descriptor_dimension));
2585 nml->ls = (array_loop_spec*)
2586 get_mem (nml->var_rank * sizeof (array_loop_spec));
2596 if ((dtp->common.flags & IOPARM_DT_IONML_SET) == 0)
2598 dtp->common.flags |= IOPARM_DT_IONML_SET;
2599 dtp->u.p.ionml = nml;
2603 for (t1 = dtp->u.p.ionml; t1->next; t1 = t1->next);
2608 /* Store the dimensional information for the namelist object. */
2609 extern void st_set_nml_var_dim (st_parameter_dt *, GFC_INTEGER_4,
2610 GFC_INTEGER_4, GFC_INTEGER_4,
2612 export_proto(st_set_nml_var_dim);
2615 st_set_nml_var_dim (st_parameter_dt *dtp, GFC_INTEGER_4 n_dim,
2616 GFC_INTEGER_4 stride, GFC_INTEGER_4 lbound,
2617 GFC_INTEGER_4 ubound)
2619 namelist_info * nml;
2624 for (nml = dtp->u.p.ionml; nml->next; nml = nml->next);
2626 nml->dim[n].stride = (ssize_t)stride;
2627 nml->dim[n].lbound = (ssize_t)lbound;
2628 nml->dim[n].ubound = (ssize_t)ubound;
2631 /* Reverse memcpy - used for byte swapping. */
2633 void reverse_memcpy (void *dest, const void *src, size_t n)
2639 s = (char *) src + n - 1;
2641 /* Write with ascending order - this is likely faster
2642 on modern architectures because of write combining. */