2006-10-31 Thomas Koenig <Thomas.Koenig@online.de>
[official-gcc.git] / libgfortran / io / transfer.c
blobb4c2bb65b0c1be0616efe47eb6df80d5a533e9af
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)
10 any later version.
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
19 executable.)
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. */
34 #include "config.h"
35 #include <string.h>
36 #include <assert.h>
37 #include "libgfortran.h"
38 #include "io.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
51 statement.
53 transfer_integer
54 transfer_logical
55 transfer_character
56 transfer_real
57 transfer_complex
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
64 transferred. */
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,
82 gfc_charlen_type);
83 export_proto(transfer_array);
85 static const st_option advance_opt[] = {
86 {"yes", ADVANCE_YES},
87 {"no", ADVANCE_NO},
88 {NULL, 0}
92 typedef enum
93 { FORMATTED_SEQUENTIAL, UNFORMATTED_SEQUENTIAL,
94 FORMATTED_DIRECT, UNFORMATTED_DIRECT, FORMATTED_STREAM, UNFORMATTED_STREAM
96 file_mode;
99 static file_mode
100 current_mode (st_parameter_dt *dtp)
102 file_mode m;
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;
122 return m;
126 /* Mid level data transfer statements. These subroutines do reading
127 and writing in the style of salloc_r()/salloc_w() within the
128 current record. */
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
135 an I/O error.
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. */
142 char *
143 read_sf (st_parameter_dt *dtp, int *length, int no_error)
145 char *base, *p, *q;
146 int n, readlen, crlf;
147 gfc_offset pos;
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)
157 *length = 0;
158 return base;
161 readlen = 1;
162 n = 0;
166 if (is_internal_unit (dtp))
168 /* readlen may be modified inside salloc_r if
169 is_internal_unit (dtp) is true. */
170 readlen = 1;
173 q = salloc_r (dtp->u.p.current_unit->s, &readlen);
174 if (q == NULL)
175 break;
177 /* If we have a line without a terminating \n, drop through to
178 EOR below. */
179 if (readlen < 1 && n == 0)
181 if (no_error)
182 break;
183 generate_error (&dtp->common, ERROR_END, NULL);
184 return 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;
196 crlf = 0;
197 /* If we encounter a CR, it might be a CRLF. */
198 if (*q == '\r') /* Probably a CRLF */
200 readlen = 1;
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);
205 else
206 crlf = 1;
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)
214 if (no_error)
215 break;
216 generate_error (&dtp->common, ERROR_EOR, NULL);
217 return NULL;
220 *length = n;
221 dtp->u.p.sf_seen_eor = (crlf ? 2 : 1);
222 break;
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 */
227 if (*q == ',')
228 if (dtp->u.p.sf_read_comma == 1)
230 notify_std (&dtp->common, GFC_STD_GNU,
231 "Comma in formatted numeric read.");
232 *length = n;
233 break;
236 n++;
237 *p++ = *q;
238 dtp->u.p.sf_seen_eor = 0;
240 while (n < *length);
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;
246 return base;
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
253 end of file.
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
258 short reads. */
260 void *
261 read_block (st_parameter_dt *dtp, int *length)
263 char *source;
264 int nread;
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;
275 else
277 if (dtp->u.p.current_unit->flags.pad == PAD_NO)
279 /* Not enough data left. */
280 generate_error (&dtp->common, ERROR_EOR, NULL);
281 return 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);
289 return 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;
301 nread = *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)
310 *length = nread;
311 else
313 generate_error (&dtp->common, ERROR_EOR, NULL);
314 source = NULL;
318 else
320 if (sseek (dtp->u.p.current_unit->s,
321 dtp->u.p.current_unit->strm_pos - 1) == FAILURE)
323 generate_error (&dtp->common, ERROR_END, NULL);
324 return NULL;
327 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED)
329 source = read_sf (dtp, length, 0);
330 dtp->u.p.current_unit->strm_pos +=
331 (gfc_offset) (*length + dtp->u.p.sf_seen_eor);
332 return source;
334 nread = *length;
335 source = salloc_r (dtp->u.p.current_unit->s, &nread);
337 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
338 dtp->u.p.size_used += (gfc_offset) nread;
340 if (nread != *length)
341 { /* Short read, this shouldn't happen. */
342 if (dtp->u.p.current_unit->flags.pad == PAD_YES)
343 *length = nread;
344 else
346 generate_error (&dtp->common, ERROR_END, NULL);
347 source = NULL;
351 dtp->u.p.current_unit->strm_pos += (gfc_offset) nread;
353 return source;
357 /* Reads a block directly into application data space. */
359 static void
360 read_block_direct (st_parameter_dt *dtp, void *buf, size_t *nbytes)
362 size_t nread;
363 int short_record;
365 if (is_stream_io (dtp))
367 if (sseek (dtp->u.p.current_unit->s,
368 dtp->u.p.current_unit->strm_pos - 1) == FAILURE)
370 generate_error (&dtp->common, ERROR_END, NULL);
371 return;
374 nread = *nbytes;
375 if (sread (dtp->u.p.current_unit->s, buf, &nread) != 0)
377 generate_error (&dtp->common, ERROR_OS, NULL);
378 return;
381 dtp->u.p.current_unit->strm_pos += (gfc_offset) nread;
383 if (nread != *nbytes) /* Short read, e.g. if we hit EOF. */
384 generate_error (&dtp->common, ERROR_END, NULL);
386 return;
389 /* Unformatted file with records */
390 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) *nbytes)
392 short_record = 1;
393 nread = (size_t) dtp->u.p.current_unit->bytes_left;
394 *nbytes = nread;
396 if (dtp->u.p.current_unit->bytes_left == 0)
398 dtp->u.p.current_unit->endfile = AT_ENDFILE;
399 generate_error (&dtp->common, ERROR_END, NULL);
400 return;
404 else
406 short_record = 0;
407 nread = *nbytes;
410 dtp->u.p.current_unit->bytes_left -= nread;
412 if (sread (dtp->u.p.current_unit->s, buf, &nread) != 0)
414 generate_error (&dtp->common, ERROR_OS, NULL);
415 return;
418 if (nread != *nbytes) /* Short read, e.g. if we hit EOF. */
420 *nbytes = nread;
421 generate_error (&dtp->common, ERROR_END, NULL);
422 return;
425 if (short_record)
427 generate_error (&dtp->common, ERROR_SHORT_RECORD, NULL);
428 return;
433 /* Function for writing a block of bytes to the current file at the
434 current position, advancing the file pointer. We are given a length
435 and return a pointer to a buffer that the caller must (completely)
436 fill in. Returns NULL on error. */
438 void *
439 write_block (st_parameter_dt *dtp, int length)
441 char *dest;
443 if (!is_stream_io (dtp))
445 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) length)
447 /* For preconnected units with default record length, set bytes left
448 to unit record length and proceed, otherwise error. */
449 if ((dtp->u.p.current_unit->unit_number == options.stdout_unit
450 || dtp->u.p.current_unit->unit_number == options.stderr_unit)
451 && dtp->u.p.current_unit->recl == DEFAULT_RECL)
452 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
453 else
455 generate_error (&dtp->common, ERROR_EOR, NULL);
456 return NULL;
460 dtp->u.p.current_unit->bytes_left -= (gfc_offset) length;
463 dest = salloc_w (dtp->u.p.current_unit->s, &length);
465 if (dest == NULL)
467 generate_error (&dtp->common, ERROR_END, NULL);
468 return NULL;
471 if (is_internal_unit (dtp) && dtp->u.p.current_unit->endfile == AT_ENDFILE)
472 generate_error (&dtp->common, ERROR_END, NULL);
474 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
475 dtp->u.p.size_used += (gfc_offset) length;
477 else
479 if (sseek (dtp->u.p.current_unit->s,
480 dtp->u.p.current_unit->strm_pos - 1) == FAILURE)
482 generate_error (&dtp->common, ERROR_OS, NULL);
483 return NULL;
486 dest = salloc_w (dtp->u.p.current_unit->s, &length);
488 if (dest == NULL)
490 generate_error (&dtp->common, ERROR_END, NULL);
491 return NULL;
494 dtp->u.p.current_unit->strm_pos += (gfc_offset) length;
497 return dest;
501 /* High level interface to swrite(), taking care of errors. */
503 static try
504 write_buf (st_parameter_dt *dtp, void *buf, size_t nbytes)
506 if (!is_stream_io (dtp))
508 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) nbytes)
510 /* For preconnected units with default record length, set
511 bytes left to unit record length and proceed, otherwise
512 error. */
513 if ((dtp->u.p.current_unit->unit_number == options.stdout_unit
514 || dtp->u.p.current_unit->unit_number == options.stderr_unit)
515 && dtp->u.p.current_unit->recl == DEFAULT_RECL)
516 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
517 else
519 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
520 generate_error (&dtp->common, ERROR_DIRECT_EOR, NULL);
521 else
522 generate_error (&dtp->common, ERROR_EOR, NULL);
523 return FAILURE;
527 dtp->u.p.current_unit->bytes_left -= (gfc_offset) nbytes;
529 else
531 if (sseek (dtp->u.p.current_unit->s,
532 dtp->u.p.current_unit->strm_pos - 1) == FAILURE)
534 generate_error (&dtp->common, ERROR_OS, NULL);
535 return FAILURE;
539 if (swrite (dtp->u.p.current_unit->s, buf, &nbytes) != 0)
541 generate_error (&dtp->common, ERROR_OS, NULL);
542 return FAILURE;
545 if (!is_stream_io (dtp))
547 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
548 dtp->u.p.size_used += (gfc_offset) nbytes;
550 else
551 dtp->u.p.current_unit->strm_pos += (gfc_offset) nbytes;
553 return SUCCESS;
557 /* Master function for unformatted reads. */
559 static void
560 unformatted_read (st_parameter_dt *dtp, bt type,
561 void *dest, int kind,
562 size_t size, size_t nelems)
564 size_t i, sz;
566 /* Currently, character implies size=1. */
567 if (dtp->u.p.current_unit->flags.convert == CONVERT_NATIVE
568 || size == 1 || type == BT_CHARACTER)
570 sz = size * nelems;
571 read_block_direct (dtp, dest, &sz);
573 else
575 char buffer[16];
576 char *p;
578 /* Break up complex into its constituent reals. */
579 if (type == BT_COMPLEX)
581 nelems *= 2;
582 size /= 2;
584 p = dest;
586 /* By now, all complex variables have been split into their
587 constituent reals. For types with padding, we only need to
588 read kind bytes. We don't care about the contents
589 of the padding. If we hit a short record, then sz is
590 adjusted accordingly, making later reads no-ops. */
592 sz = kind;
593 for (i=0; i<nelems; i++)
595 read_block_direct (dtp, buffer, &sz);
596 reverse_memcpy (p, buffer, sz);
597 p += size;
603 /* Master function for unformatted writes. */
605 static void
606 unformatted_write (st_parameter_dt *dtp, bt type,
607 void *source, int kind,
608 size_t size, size_t nelems)
610 if (dtp->u.p.current_unit->flags.convert == CONVERT_NATIVE ||
611 size == 1 || type == BT_CHARACTER)
613 size *= nelems;
615 write_buf (dtp, source, size);
617 else
619 char buffer[16];
620 char *p;
621 size_t i, sz;
623 /* Break up complex into its constituent reals. */
624 if (type == BT_COMPLEX)
626 nelems *= 2;
627 size /= 2;
630 p = source;
632 /* By now, all complex variables have been split into their
633 constituent reals. For types with padding, we only need to
634 read kind bytes. We don't care about the contents
635 of the padding. */
637 sz = kind;
638 for (i=0; i<nelems; i++)
640 reverse_memcpy(buffer, p, size);
641 p+= size;
642 write_buf (dtp, buffer, sz);
648 /* Return a pointer to the name of a type. */
650 const char *
651 type_name (bt type)
653 const char *p;
655 switch (type)
657 case BT_INTEGER:
658 p = "INTEGER";
659 break;
660 case BT_LOGICAL:
661 p = "LOGICAL";
662 break;
663 case BT_CHARACTER:
664 p = "CHARACTER";
665 break;
666 case BT_REAL:
667 p = "REAL";
668 break;
669 case BT_COMPLEX:
670 p = "COMPLEX";
671 break;
672 default:
673 internal_error (NULL, "type_name(): Bad type");
676 return p;
680 /* Write a constant string to the output.
681 This is complicated because the string can have doubled delimiters
682 in it. The length in the format node is the true length. */
684 static void
685 write_constant_string (st_parameter_dt *dtp, const fnode *f)
687 char c, delimiter, *p, *q;
688 int length;
690 length = f->u.string.length;
691 if (length == 0)
692 return;
694 p = write_block (dtp, length);
695 if (p == NULL)
696 return;
698 q = f->u.string.p;
699 delimiter = q[-1];
701 for (; length > 0; length--)
703 c = *p++ = *q++;
704 if (c == delimiter && c != 'H' && c != 'h')
705 q++; /* Skip the doubled delimiter. */
710 /* Given actual and expected types in a formatted data transfer, make
711 sure they agree. If not, an error message is generated. Returns
712 nonzero if something went wrong. */
714 static int
715 require_type (st_parameter_dt *dtp, bt expected, bt actual, const fnode *f)
717 char buffer[100];
719 if (actual == expected)
720 return 0;
722 st_sprintf (buffer, "Expected %s for item %d in formatted transfer, got %s",
723 type_name (expected), dtp->u.p.item_count, type_name (actual));
725 format_error (dtp, f, buffer);
726 return 1;
730 /* This subroutine is the main loop for a formatted data transfer
731 statement. It would be natural to implement this as a coroutine
732 with the user program, but C makes that awkward. We loop,
733 processing format elements. When we actually have to transfer
734 data instead of just setting flags, we return control to the user
735 program which calls a subroutine that supplies the address and type
736 of the next element, then comes back here to process it. */
738 static void
739 formatted_transfer_scalar (st_parameter_dt *dtp, bt type, void *p, int len,
740 size_t size)
742 char scratch[SCRATCH_SIZE];
743 int pos, bytes_used;
744 const fnode *f;
745 format_token t;
746 int n;
747 int consume_data_flag;
749 /* Change a complex data item into a pair of reals. */
751 n = (p == NULL) ? 0 : ((type != BT_COMPLEX) ? 1 : 2);
752 if (type == BT_COMPLEX)
754 type = BT_REAL;
755 size /= 2;
758 /* If there's an EOR condition, we simulate finalizing the transfer
759 by doing nothing. */
760 if (dtp->u.p.eor_condition)
761 return;
763 /* Set this flag so that commas in reads cause the read to complete before
764 the entire field has been read. The next read field will start right after
765 the comma in the stream. (Set to 0 for character reads). */
766 dtp->u.p.sf_read_comma = 1;
768 dtp->u.p.line_buffer = scratch;
769 for (;;)
771 /* If reversion has occurred and there is another real data item,
772 then we have to move to the next record. */
773 if (dtp->u.p.reversion_flag && n > 0)
775 dtp->u.p.reversion_flag = 0;
776 next_record (dtp, 0);
779 consume_data_flag = 1 ;
780 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
781 break;
783 f = next_format (dtp);
784 if (f == NULL)
786 /* No data descriptors left. */
787 if (n > 0)
788 generate_error (&dtp->common, ERROR_FORMAT,
789 "Insufficient data descriptors in format after reversion");
790 return;
793 /* Now discharge T, TR and X movements to the right. This is delayed
794 until a data producing format to suppress trailing spaces. */
796 t = f->format;
797 if (dtp->u.p.mode == WRITING && dtp->u.p.skips != 0
798 && ((n>0 && ( t == FMT_I || t == FMT_B || t == FMT_O
799 || t == FMT_Z || t == FMT_F || t == FMT_E
800 || t == FMT_EN || t == FMT_ES || t == FMT_G
801 || t == FMT_L || t == FMT_A || t == FMT_D))
802 || t == FMT_STRING))
804 if (dtp->u.p.skips > 0)
806 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
807 dtp->u.p.max_pos = (int)(dtp->u.p.current_unit->recl
808 - dtp->u.p.current_unit->bytes_left);
810 if (dtp->u.p.skips < 0)
812 move_pos_offset (dtp->u.p.current_unit->s, dtp->u.p.skips);
813 dtp->u.p.current_unit->bytes_left -= (gfc_offset) dtp->u.p.skips;
815 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
818 bytes_used = (int)(dtp->u.p.current_unit->recl
819 - dtp->u.p.current_unit->bytes_left);
821 switch (t)
823 case FMT_I:
824 if (n == 0)
825 goto need_data;
826 if (require_type (dtp, BT_INTEGER, type, f))
827 return;
829 if (dtp->u.p.mode == READING)
830 read_decimal (dtp, f, p, len);
831 else
832 write_i (dtp, f, p, len);
834 break;
836 case FMT_B:
837 if (n == 0)
838 goto need_data;
840 if (compile_options.allow_std < GFC_STD_GNU
841 && require_type (dtp, BT_INTEGER, type, f))
842 return;
844 if (dtp->u.p.mode == READING)
845 read_radix (dtp, f, p, len, 2);
846 else
847 write_b (dtp, f, p, len);
849 break;
851 case FMT_O:
852 if (n == 0)
853 goto need_data;
855 if (compile_options.allow_std < GFC_STD_GNU
856 && require_type (dtp, BT_INTEGER, type, f))
857 return;
859 if (dtp->u.p.mode == READING)
860 read_radix (dtp, f, p, len, 8);
861 else
862 write_o (dtp, f, p, len);
864 break;
866 case FMT_Z:
867 if (n == 0)
868 goto need_data;
870 if (compile_options.allow_std < GFC_STD_GNU
871 && require_type (dtp, BT_INTEGER, type, f))
872 return;
874 if (dtp->u.p.mode == READING)
875 read_radix (dtp, f, p, len, 16);
876 else
877 write_z (dtp, f, p, len);
879 break;
881 case FMT_A:
882 if (n == 0)
883 goto need_data;
885 if (dtp->u.p.mode == READING)
886 read_a (dtp, f, p, len);
887 else
888 write_a (dtp, f, p, len);
890 break;
892 case FMT_L:
893 if (n == 0)
894 goto need_data;
896 if (dtp->u.p.mode == READING)
897 read_l (dtp, f, p, len);
898 else
899 write_l (dtp, f, p, len);
901 break;
903 case FMT_D:
904 if (n == 0)
905 goto need_data;
906 if (require_type (dtp, BT_REAL, type, f))
907 return;
909 if (dtp->u.p.mode == READING)
910 read_f (dtp, f, p, len);
911 else
912 write_d (dtp, f, p, len);
914 break;
916 case FMT_E:
917 if (n == 0)
918 goto need_data;
919 if (require_type (dtp, BT_REAL, type, f))
920 return;
922 if (dtp->u.p.mode == READING)
923 read_f (dtp, f, p, len);
924 else
925 write_e (dtp, f, p, len);
926 break;
928 case FMT_EN:
929 if (n == 0)
930 goto need_data;
931 if (require_type (dtp, BT_REAL, type, f))
932 return;
934 if (dtp->u.p.mode == READING)
935 read_f (dtp, f, p, len);
936 else
937 write_en (dtp, f, p, len);
939 break;
941 case FMT_ES:
942 if (n == 0)
943 goto need_data;
944 if (require_type (dtp, BT_REAL, type, f))
945 return;
947 if (dtp->u.p.mode == READING)
948 read_f (dtp, f, p, len);
949 else
950 write_es (dtp, f, p, len);
952 break;
954 case FMT_F:
955 if (n == 0)
956 goto need_data;
957 if (require_type (dtp, BT_REAL, type, f))
958 return;
960 if (dtp->u.p.mode == READING)
961 read_f (dtp, f, p, len);
962 else
963 write_f (dtp, f, p, len);
965 break;
967 case FMT_G:
968 if (n == 0)
969 goto need_data;
970 if (dtp->u.p.mode == READING)
971 switch (type)
973 case BT_INTEGER:
974 read_decimal (dtp, f, p, len);
975 break;
976 case BT_LOGICAL:
977 read_l (dtp, f, p, len);
978 break;
979 case BT_CHARACTER:
980 read_a (dtp, f, p, len);
981 break;
982 case BT_REAL:
983 read_f (dtp, f, p, len);
984 break;
985 default:
986 goto bad_type;
988 else
989 switch (type)
991 case BT_INTEGER:
992 write_i (dtp, f, p, len);
993 break;
994 case BT_LOGICAL:
995 write_l (dtp, f, p, len);
996 break;
997 case BT_CHARACTER:
998 write_a (dtp, f, p, len);
999 break;
1000 case BT_REAL:
1001 write_d (dtp, f, p, len);
1002 break;
1003 default:
1004 bad_type:
1005 internal_error (&dtp->common,
1006 "formatted_transfer(): Bad type");
1009 break;
1011 case FMT_STRING:
1012 consume_data_flag = 0 ;
1013 if (dtp->u.p.mode == READING)
1015 format_error (dtp, f, "Constant string in input format");
1016 return;
1018 write_constant_string (dtp, f);
1019 break;
1021 /* Format codes that don't transfer data. */
1022 case FMT_X:
1023 case FMT_TR:
1024 consume_data_flag = 0 ;
1026 pos = bytes_used + f->u.n + dtp->u.p.skips;
1027 dtp->u.p.skips = f->u.n + dtp->u.p.skips;
1028 dtp->u.p.pending_spaces = pos - dtp->u.p.max_pos;
1030 /* Writes occur just before the switch on f->format, above, so
1031 that trailing blanks are suppressed, unless we are doing a
1032 non-advancing write in which case we want to output the blanks
1033 now. */
1034 if (dtp->u.p.mode == WRITING
1035 && dtp->u.p.advance_status == ADVANCE_NO)
1037 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
1038 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1040 if (dtp->u.p.mode == READING)
1041 read_x (dtp, f->u.n);
1043 break;
1045 case FMT_TL:
1046 case FMT_T:
1047 if (f->format == FMT_TL)
1050 /* Handle the special case when no bytes have been used yet.
1051 Cannot go below zero. */
1052 if (bytes_used == 0)
1054 dtp->u.p.pending_spaces -= f->u.n;
1055 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces < 0 ? 0
1056 : dtp->u.p.pending_spaces;
1057 dtp->u.p.skips -= f->u.n;
1058 dtp->u.p.skips = dtp->u.p.skips < 0 ? 0 : dtp->u.p.skips;
1061 pos = bytes_used - f->u.n;
1063 else /* FMT_T */
1065 consume_data_flag = 0;
1066 pos = f->u.n - 1;
1069 /* Standard 10.6.1.1: excessive left tabbing is reset to the
1070 left tab limit. We do not check if the position has gone
1071 beyond the end of record because a subsequent tab could
1072 bring us back again. */
1073 pos = pos < 0 ? 0 : pos;
1075 dtp->u.p.skips = dtp->u.p.skips + pos - bytes_used;
1076 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces
1077 + pos - dtp->u.p.max_pos;
1079 if (dtp->u.p.skips == 0)
1080 break;
1082 /* Writes occur just before the switch on f->format, above, so that
1083 trailing blanks are suppressed. */
1084 if (dtp->u.p.mode == READING)
1086 /* Adjust everything for end-of-record condition */
1087 if (dtp->u.p.sf_seen_eor && !is_internal_unit (dtp))
1089 if (dtp->u.p.sf_seen_eor == 2)
1091 /* The EOR was a CRLF (two bytes wide). */
1092 dtp->u.p.current_unit->bytes_left -= 2;
1093 dtp->u.p.skips -= 2;
1095 else
1097 /* The EOR marker was only one byte wide. */
1098 dtp->u.p.current_unit->bytes_left--;
1099 dtp->u.p.skips--;
1101 bytes_used = pos;
1102 dtp->u.p.sf_seen_eor = 0;
1104 if (dtp->u.p.skips < 0)
1106 move_pos_offset (dtp->u.p.current_unit->s, dtp->u.p.skips);
1107 dtp->u.p.current_unit->bytes_left
1108 -= (gfc_offset) dtp->u.p.skips;
1109 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1111 else
1112 read_x (dtp, dtp->u.p.skips);
1115 break;
1117 case FMT_S:
1118 consume_data_flag = 0 ;
1119 dtp->u.p.sign_status = SIGN_S;
1120 break;
1122 case FMT_SS:
1123 consume_data_flag = 0 ;
1124 dtp->u.p.sign_status = SIGN_SS;
1125 break;
1127 case FMT_SP:
1128 consume_data_flag = 0 ;
1129 dtp->u.p.sign_status = SIGN_SP;
1130 break;
1132 case FMT_BN:
1133 consume_data_flag = 0 ;
1134 dtp->u.p.blank_status = BLANK_NULL;
1135 break;
1137 case FMT_BZ:
1138 consume_data_flag = 0 ;
1139 dtp->u.p.blank_status = BLANK_ZERO;
1140 break;
1142 case FMT_P:
1143 consume_data_flag = 0 ;
1144 dtp->u.p.scale_factor = f->u.k;
1145 break;
1147 case FMT_DOLLAR:
1148 consume_data_flag = 0 ;
1149 dtp->u.p.seen_dollar = 1;
1150 break;
1152 case FMT_SLASH:
1153 consume_data_flag = 0 ;
1154 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1155 next_record (dtp, 0);
1156 break;
1158 case FMT_COLON:
1159 /* A colon descriptor causes us to exit this loop (in
1160 particular preventing another / descriptor from being
1161 processed) unless there is another data item to be
1162 transferred. */
1163 consume_data_flag = 0 ;
1164 if (n == 0)
1165 return;
1166 break;
1168 default:
1169 internal_error (&dtp->common, "Bad format node");
1172 /* Free a buffer that we had to allocate during a sequential
1173 formatted read of a block that was larger than the static
1174 buffer. */
1176 if (dtp->u.p.line_buffer != scratch)
1178 free_mem (dtp->u.p.line_buffer);
1179 dtp->u.p.line_buffer = scratch;
1182 /* Adjust the item count and data pointer. */
1184 if ((consume_data_flag > 0) && (n > 0))
1186 n--;
1187 p = ((char *) p) + size;
1190 if (dtp->u.p.mode == READING)
1191 dtp->u.p.skips = 0;
1193 pos = (int)(dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left);
1194 dtp->u.p.max_pos = (dtp->u.p.max_pos > pos) ? dtp->u.p.max_pos : pos;
1198 return;
1200 /* Come here when we need a data descriptor but don't have one. We
1201 push the current format node back onto the input, then return and
1202 let the user program call us back with the data. */
1203 need_data:
1204 unget_format (dtp, f);
1207 static void
1208 formatted_transfer (st_parameter_dt *dtp, bt type, void *p, int kind,
1209 size_t size, size_t nelems)
1211 size_t elem;
1212 char *tmp;
1214 tmp = (char *) p;
1216 /* Big loop over all the elements. */
1217 for (elem = 0; elem < nelems; elem++)
1219 dtp->u.p.item_count++;
1220 formatted_transfer_scalar (dtp, type, tmp + size*elem, kind, size);
1226 /* Data transfer entry points. The type of the data entity is
1227 implicit in the subroutine call. This prevents us from having to
1228 share a common enum with the compiler. */
1230 void
1231 transfer_integer (st_parameter_dt *dtp, void *p, int kind)
1233 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1234 return;
1235 dtp->u.p.transfer (dtp, BT_INTEGER, p, kind, kind, 1);
1239 void
1240 transfer_real (st_parameter_dt *dtp, void *p, int kind)
1242 size_t size;
1243 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1244 return;
1245 size = size_from_real_kind (kind);
1246 dtp->u.p.transfer (dtp, BT_REAL, p, kind, size, 1);
1250 void
1251 transfer_logical (st_parameter_dt *dtp, void *p, int kind)
1253 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1254 return;
1255 dtp->u.p.transfer (dtp, BT_LOGICAL, p, kind, kind, 1);
1259 void
1260 transfer_character (st_parameter_dt *dtp, void *p, int len)
1262 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1263 return;
1264 /* Currently we support only 1 byte chars, and the library is a bit
1265 confused of character kind vs. length, so we kludge it by setting
1266 kind = length. */
1267 dtp->u.p.transfer (dtp, BT_CHARACTER, p, len, len, 1);
1271 void
1272 transfer_complex (st_parameter_dt *dtp, void *p, int kind)
1274 size_t size;
1275 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1276 return;
1277 size = size_from_complex_kind (kind);
1278 dtp->u.p.transfer (dtp, BT_COMPLEX, p, kind, size, 1);
1282 void
1283 transfer_array (st_parameter_dt *dtp, gfc_array_char *desc, int kind,
1284 gfc_charlen_type charlen)
1286 index_type count[GFC_MAX_DIMENSIONS];
1287 index_type extent[GFC_MAX_DIMENSIONS];
1288 index_type stride[GFC_MAX_DIMENSIONS];
1289 index_type stride0, rank, size, type, n;
1290 size_t tsize;
1291 char *data;
1292 bt iotype;
1294 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1295 return;
1297 type = GFC_DESCRIPTOR_TYPE (desc);
1298 size = GFC_DESCRIPTOR_SIZE (desc);
1300 /* FIXME: What a kludge: Array descriptors and the IO library use
1301 different enums for types. */
1302 switch (type)
1304 case GFC_DTYPE_UNKNOWN:
1305 iotype = BT_NULL; /* Is this correct? */
1306 break;
1307 case GFC_DTYPE_INTEGER:
1308 iotype = BT_INTEGER;
1309 break;
1310 case GFC_DTYPE_LOGICAL:
1311 iotype = BT_LOGICAL;
1312 break;
1313 case GFC_DTYPE_REAL:
1314 iotype = BT_REAL;
1315 break;
1316 case GFC_DTYPE_COMPLEX:
1317 iotype = BT_COMPLEX;
1318 break;
1319 case GFC_DTYPE_CHARACTER:
1320 iotype = BT_CHARACTER;
1321 /* FIXME: Currently dtype contains the charlen, which is
1322 clobbered if charlen > 2**24. That's why we use a separate
1323 argument for the charlen. However, if we want to support
1324 non-8-bit charsets we need to fix dtype to contain
1325 sizeof(chartype) and fix the code below. */
1326 size = charlen;
1327 kind = charlen;
1328 break;
1329 case GFC_DTYPE_DERIVED:
1330 internal_error (&dtp->common,
1331 "Derived type I/O should have been handled via the frontend.");
1332 break;
1333 default:
1334 internal_error (&dtp->common, "transfer_array(): Bad type");
1337 rank = GFC_DESCRIPTOR_RANK (desc);
1338 for (n = 0; n < rank; n++)
1340 count[n] = 0;
1341 stride[n] = desc->dim[n].stride;
1342 extent[n] = desc->dim[n].ubound + 1 - desc->dim[n].lbound;
1344 /* If the extent of even one dimension is zero, then the entire
1345 array section contains zero elements, so we return. */
1346 if (extent[n] == 0)
1347 return;
1350 stride0 = stride[0];
1352 /* If the innermost dimension has stride 1, we can do the transfer
1353 in contiguous chunks. */
1354 if (stride0 == 1)
1355 tsize = extent[0];
1356 else
1357 tsize = 1;
1359 data = GFC_DESCRIPTOR_DATA (desc);
1361 while (data)
1363 dtp->u.p.transfer (dtp, iotype, data, kind, size, tsize);
1364 data += stride0 * size * tsize;
1365 count[0] += tsize;
1366 n = 0;
1367 while (count[n] == extent[n])
1369 count[n] = 0;
1370 data -= stride[n] * extent[n] * size;
1371 n++;
1372 if (n == rank)
1374 data = NULL;
1375 break;
1377 else
1379 count[n]++;
1380 data += stride[n] * size;
1387 /* Preposition a sequential unformatted file while reading. */
1389 static void
1390 us_read (st_parameter_dt *dtp)
1392 char *p;
1393 int n;
1394 int nr;
1395 GFC_INTEGER_4 i4;
1396 GFC_INTEGER_8 i8;
1397 gfc_offset i;
1399 if (dtp->u.p.current_unit->endfile == AT_ENDFILE)
1400 return;
1402 if (compile_options.record_marker == 0)
1403 n = sizeof (gfc_offset);
1404 else
1405 n = compile_options.record_marker;
1407 nr = n;
1409 p = salloc_r (dtp->u.p.current_unit->s, &n);
1411 if (n == 0)
1413 dtp->u.p.current_unit->endfile = AT_ENDFILE;
1414 return; /* end of file */
1417 if (p == NULL || n != nr)
1419 generate_error (&dtp->common, ERROR_BAD_US, NULL);
1420 return;
1423 /* Only CONVERT_NATIVE and CONVERT_SWAP are valid here. */
1424 if (dtp->u.p.current_unit->flags.convert == CONVERT_NATIVE)
1426 switch (compile_options.record_marker)
1428 case 0:
1429 memcpy (&i, p, sizeof(gfc_offset));
1430 break;
1432 case sizeof(GFC_INTEGER_4):
1433 memcpy (&i4, p, sizeof (i4));
1434 i = i4;
1435 break;
1437 case sizeof(GFC_INTEGER_8):
1438 memcpy (&i8, p, sizeof (i8));
1439 i = i8;
1440 break;
1442 default:
1443 runtime_error ("Illegal value for record marker");
1444 break;
1447 else
1448 switch (compile_options.record_marker)
1450 case 0:
1451 reverse_memcpy (&i, p, sizeof(gfc_offset));
1452 break;
1454 case sizeof(GFC_INTEGER_4):
1455 reverse_memcpy (&i4, p, sizeof (i4));
1456 i = i4;
1457 break;
1459 case sizeof(GFC_INTEGER_8):
1460 reverse_memcpy (&i8, p, sizeof (i8));
1461 i = i8;
1462 break;
1464 default:
1465 runtime_error ("Illegal value for record marker");
1466 break;
1469 dtp->u.p.current_unit->bytes_left = i;
1473 /* Preposition a sequential unformatted file while writing. This
1474 amount to writing a bogus length that will be filled in later. */
1476 static void
1477 us_write (st_parameter_dt *dtp)
1479 size_t nbytes;
1480 gfc_offset dummy;
1482 dummy = 0;
1484 if (compile_options.record_marker == 0)
1485 nbytes = sizeof (gfc_offset);
1486 else
1487 nbytes = compile_options.record_marker ;
1489 if (swrite (dtp->u.p.current_unit->s, &dummy, &nbytes) != 0)
1490 generate_error (&dtp->common, ERROR_OS, NULL);
1492 /* For sequential unformatted, we write until we have more bytes
1493 than can fit in the record markers. If disk space runs out first,
1494 it will error on the write. */
1495 dtp->u.p.current_unit->recl = max_offset;
1497 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
1501 /* Position to the next record prior to transfer. We are assumed to
1502 be before the next record. We also calculate the bytes in the next
1503 record. */
1505 static void
1506 pre_position (st_parameter_dt *dtp)
1508 if (dtp->u.p.current_unit->current_record)
1509 return; /* Already positioned. */
1511 switch (current_mode (dtp))
1513 case FORMATTED_STREAM:
1514 case UNFORMATTED_STREAM:
1515 /* There are no records with stream I/O. Set the default position
1516 to the beginning of the file if no position was specified. */
1517 if ((dtp->common.flags & IOPARM_DT_HAS_REC) == 0)
1518 dtp->u.p.current_unit->strm_pos = 1;
1519 break;
1521 case UNFORMATTED_SEQUENTIAL:
1522 if (dtp->u.p.mode == READING)
1523 us_read (dtp);
1524 else
1525 us_write (dtp);
1527 break;
1529 case FORMATTED_SEQUENTIAL:
1530 case FORMATTED_DIRECT:
1531 case UNFORMATTED_DIRECT:
1532 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
1533 break;
1536 dtp->u.p.current_unit->current_record = 1;
1540 /* Initialize things for a data transfer. This code is common for
1541 both reading and writing. */
1543 static void
1544 data_transfer_init (st_parameter_dt *dtp, int read_flag)
1546 unit_flags u_flags; /* Used for creating a unit if needed. */
1547 GFC_INTEGER_4 cf = dtp->common.flags;
1548 namelist_info *ionml;
1550 ionml = ((cf & IOPARM_DT_IONML_SET) != 0) ? dtp->u.p.ionml : NULL;
1551 memset (&dtp->u.p, 0, sizeof (dtp->u.p));
1552 dtp->u.p.ionml = ionml;
1553 dtp->u.p.mode = read_flag ? READING : WRITING;
1555 if ((cf & IOPARM_DT_HAS_SIZE) != 0)
1556 dtp->u.p.size_used = 0; /* Initialize the count. */
1558 dtp->u.p.current_unit = get_unit (dtp, 1);
1559 if (dtp->u.p.current_unit->s == NULL)
1560 { /* Open the unit with some default flags. */
1561 st_parameter_open opp;
1562 unit_convert conv;
1564 if (dtp->common.unit < 0)
1566 close_unit (dtp->u.p.current_unit);
1567 dtp->u.p.current_unit = NULL;
1568 generate_error (&dtp->common, ERROR_BAD_OPTION,
1569 "Bad unit number in OPEN statement");
1570 return;
1572 memset (&u_flags, '\0', sizeof (u_flags));
1573 u_flags.access = ACCESS_SEQUENTIAL;
1574 u_flags.action = ACTION_READWRITE;
1576 /* Is it unformatted? */
1577 if (!(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT
1578 | IOPARM_DT_IONML_SET)))
1579 u_flags.form = FORM_UNFORMATTED;
1580 else
1581 u_flags.form = FORM_UNSPECIFIED;
1583 u_flags.delim = DELIM_UNSPECIFIED;
1584 u_flags.blank = BLANK_UNSPECIFIED;
1585 u_flags.pad = PAD_UNSPECIFIED;
1586 u_flags.status = STATUS_UNKNOWN;
1588 conv = get_unformatted_convert (dtp->common.unit);
1590 if (conv == CONVERT_NONE)
1591 conv = compile_options.convert;
1593 /* We use l8_to_l4_offset, which is 0 on little-endian machines
1594 and 1 on big-endian machines. */
1595 switch (conv)
1597 case CONVERT_NATIVE:
1598 case CONVERT_SWAP:
1599 break;
1601 case CONVERT_BIG:
1602 conv = l8_to_l4_offset ? CONVERT_NATIVE : CONVERT_SWAP;
1603 break;
1605 case CONVERT_LITTLE:
1606 conv = l8_to_l4_offset ? CONVERT_SWAP : CONVERT_NATIVE;
1607 break;
1609 default:
1610 internal_error (&opp.common, "Illegal value for CONVERT");
1611 break;
1614 u_flags.convert = conv;
1616 opp.common = dtp->common;
1617 opp.common.flags &= IOPARM_COMMON_MASK;
1618 dtp->u.p.current_unit = new_unit (&opp, dtp->u.p.current_unit, &u_flags);
1619 dtp->common.flags &= ~IOPARM_COMMON_MASK;
1620 dtp->common.flags |= (opp.common.flags & IOPARM_COMMON_MASK);
1621 if (dtp->u.p.current_unit == NULL)
1622 return;
1625 /* Check the action. */
1627 if (read_flag && dtp->u.p.current_unit->flags.action == ACTION_WRITE)
1628 generate_error (&dtp->common, ERROR_BAD_ACTION,
1629 "Cannot read from file opened for WRITE");
1631 if (!read_flag && dtp->u.p.current_unit->flags.action == ACTION_READ)
1632 generate_error (&dtp->common, ERROR_BAD_ACTION,
1633 "Cannot write to file opened for READ");
1635 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1636 return;
1638 dtp->u.p.first_item = 1;
1640 /* Check the format. */
1642 if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
1643 parse_format (dtp);
1645 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1646 return;
1648 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED
1649 && (cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
1650 != 0)
1651 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1652 "Format present for UNFORMATTED data transfer");
1654 if ((cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0 && dtp->u.p.ionml != NULL)
1656 if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
1657 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1658 "A format cannot be specified with a namelist");
1660 else if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
1661 !(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT)))
1662 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1663 "Missing format for FORMATTED data transfer");
1665 if (is_internal_unit (dtp)
1666 && dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
1667 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1668 "Internal file cannot be accessed by UNFORMATTED data transfer");
1670 /* Check the record or position number. */
1672 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT
1673 && (cf & IOPARM_DT_HAS_REC) == 0)
1675 generate_error (&dtp->common, ERROR_MISSING_OPTION,
1676 "Direct access data transfer requires record number");
1677 return;
1680 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL
1681 && (cf & IOPARM_DT_HAS_REC) != 0)
1683 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1684 "Record number not allowed for sequential access data transfer");
1685 return;
1688 /* Process the ADVANCE option. */
1690 dtp->u.p.advance_status
1691 = !(cf & IOPARM_DT_HAS_ADVANCE) ? ADVANCE_UNSPECIFIED :
1692 find_option (&dtp->common, dtp->advance, dtp->advance_len, advance_opt,
1693 "Bad ADVANCE parameter in data transfer statement");
1695 if (dtp->u.p.advance_status != ADVANCE_UNSPECIFIED)
1697 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
1698 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1699 "ADVANCE specification conflicts with sequential access");
1701 if (is_internal_unit (dtp))
1702 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1703 "ADVANCE specification conflicts with internal file");
1705 if ((cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
1706 != IOPARM_DT_HAS_FORMAT)
1707 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1708 "ADVANCE specification requires an explicit format");
1711 if (read_flag)
1713 if ((cf & IOPARM_EOR) != 0 && dtp->u.p.advance_status != ADVANCE_NO)
1714 generate_error (&dtp->common, ERROR_MISSING_OPTION,
1715 "EOR specification requires an ADVANCE specification of NO");
1717 if ((cf & IOPARM_DT_HAS_SIZE) != 0 && dtp->u.p.advance_status != ADVANCE_NO)
1718 generate_error (&dtp->common, ERROR_MISSING_OPTION,
1719 "SIZE specification requires an ADVANCE specification of NO");
1722 else
1723 { /* Write constraints. */
1724 if ((cf & IOPARM_END) != 0)
1725 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1726 "END specification cannot appear in a write statement");
1728 if ((cf & IOPARM_EOR) != 0)
1729 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1730 "EOR specification cannot appear in a write statement");
1732 if ((cf & IOPARM_DT_HAS_SIZE) != 0)
1733 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1734 "SIZE specification cannot appear in a write statement");
1737 if (dtp->u.p.advance_status == ADVANCE_UNSPECIFIED)
1738 dtp->u.p.advance_status = ADVANCE_YES;
1739 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1740 return;
1742 /* Sanity checks on the record number. */
1743 if ((cf & IOPARM_DT_HAS_REC) != 0)
1745 if (dtp->rec <= 0)
1747 generate_error (&dtp->common, ERROR_BAD_OPTION,
1748 "Record number must be positive");
1749 return;
1752 if (dtp->rec >= dtp->u.p.current_unit->maxrec)
1754 generate_error (&dtp->common, ERROR_BAD_OPTION,
1755 "Record number too large");
1756 return;
1759 /* Check to see if we might be reading what we wrote before */
1761 if (dtp->u.p.mode == READING
1762 && dtp->u.p.current_unit->mode == WRITING
1763 && !is_internal_unit (dtp))
1764 flush(dtp->u.p.current_unit->s);
1766 /* Check whether the record exists to be read. Only
1767 a partial record needs to exist. */
1769 if (dtp->u.p.mode == READING && (dtp->rec -1)
1770 * dtp->u.p.current_unit->recl >= file_length (dtp->u.p.current_unit->s))
1772 generate_error (&dtp->common, ERROR_BAD_OPTION,
1773 "Non-existing record number");
1774 return;
1777 /* Position the file. */
1778 if (!is_stream_io (dtp))
1780 if (sseek (dtp->u.p.current_unit->s, (gfc_offset) (dtp->rec - 1)
1781 * dtp->u.p.current_unit->recl) == FAILURE)
1783 generate_error (&dtp->common, ERROR_OS, NULL);
1784 return;
1787 else
1788 dtp->u.p.current_unit->strm_pos = dtp->rec;
1792 /* Overwriting an existing sequential file ?
1793 it is always safe to truncate the file on the first write */
1794 if (dtp->u.p.mode == WRITING
1795 && dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL
1796 && dtp->u.p.current_unit->last_record == 0
1797 && !is_preconnected(dtp->u.p.current_unit->s))
1798 struncate(dtp->u.p.current_unit->s);
1800 /* Bugware for badly written mixed C-Fortran I/O. */
1801 flush_if_preconnected(dtp->u.p.current_unit->s);
1803 dtp->u.p.current_unit->mode = dtp->u.p.mode;
1805 /* Set the initial value of flags. */
1807 dtp->u.p.blank_status = dtp->u.p.current_unit->flags.blank;
1808 dtp->u.p.sign_status = SIGN_S;
1810 pre_position (dtp);
1812 /* Set up the subroutine that will handle the transfers. */
1814 if (read_flag)
1816 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
1817 dtp->u.p.transfer = unformatted_read;
1818 else
1820 if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
1821 dtp->u.p.transfer = list_formatted_read;
1822 else
1823 dtp->u.p.transfer = formatted_transfer;
1826 else
1828 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
1829 dtp->u.p.transfer = unformatted_write;
1830 else
1832 if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
1833 dtp->u.p.transfer = list_formatted_write;
1834 else
1835 dtp->u.p.transfer = formatted_transfer;
1839 /* Make sure that we don't do a read after a nonadvancing write. */
1841 if (read_flag)
1843 if (dtp->u.p.current_unit->read_bad && !is_stream_io (dtp))
1845 generate_error (&dtp->common, ERROR_BAD_OPTION,
1846 "Cannot READ after a nonadvancing WRITE");
1847 return;
1850 else
1852 if (dtp->u.p.advance_status == ADVANCE_YES && !dtp->u.p.seen_dollar)
1853 dtp->u.p.current_unit->read_bad = 1;
1856 /* Start the data transfer if we are doing a formatted transfer. */
1857 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
1858 && ((cf & (IOPARM_DT_LIST_FORMAT | IOPARM_DT_HAS_NAMELIST_NAME)) == 0)
1859 && dtp->u.p.ionml == NULL)
1860 formatted_transfer (dtp, 0, NULL, 0, 0, 1);
1863 /* Initialize an array_loop_spec given the array descriptor. The function
1864 returns the index of the last element of the array. */
1866 gfc_offset
1867 init_loop_spec (gfc_array_char *desc, array_loop_spec *ls)
1869 int rank = GFC_DESCRIPTOR_RANK(desc);
1870 int i;
1871 gfc_offset index;
1873 index = 1;
1874 for (i=0; i<rank; i++)
1876 ls[i].idx = 1;
1877 ls[i].start = desc->dim[i].lbound;
1878 ls[i].end = desc->dim[i].ubound;
1879 ls[i].step = desc->dim[i].stride;
1881 index += (desc->dim[i].ubound - desc->dim[i].lbound)
1882 * desc->dim[i].stride;
1884 return index;
1887 /* Determine the index to the next record in an internal unit array by
1888 by incrementing through the array_loop_spec. TODO: Implement handling
1889 negative strides. */
1891 gfc_offset
1892 next_array_record (st_parameter_dt *dtp, array_loop_spec *ls)
1894 int i, carry;
1895 gfc_offset index;
1897 carry = 1;
1898 index = 0;
1900 for (i = 0; i < dtp->u.p.current_unit->rank; i++)
1902 if (carry)
1904 ls[i].idx++;
1905 if (ls[i].idx > ls[i].end)
1907 ls[i].idx = ls[i].start;
1908 carry = 1;
1910 else
1911 carry = 0;
1913 index = index + (ls[i].idx - 1) * ls[i].step;
1915 return index;
1918 /* Space to the next record for read mode. If the file is not
1919 seekable, we read MAX_READ chunks until we get to the right
1920 position. */
1922 #define MAX_READ 4096
1924 static void
1925 next_record_r (st_parameter_dt *dtp)
1927 gfc_offset new, record;
1928 int bytes_left, rlength, length;
1929 char *p;
1931 switch (current_mode (dtp))
1933 /* No records in unformatted STREAM I/O. */
1934 case UNFORMATTED_STREAM:
1935 return;
1937 case UNFORMATTED_SEQUENTIAL:
1939 /* Skip over tail */
1940 dtp->u.p.current_unit->bytes_left +=
1941 compile_options.record_marker == 0 ?
1942 sizeof (gfc_offset) : compile_options.record_marker;
1944 /* Fall through... */
1946 case FORMATTED_DIRECT:
1947 case UNFORMATTED_DIRECT:
1948 if (dtp->u.p.current_unit->bytes_left == 0)
1949 break;
1951 if (is_seekable (dtp->u.p.current_unit->s))
1953 new = file_position (dtp->u.p.current_unit->s)
1954 + dtp->u.p.current_unit->bytes_left;
1956 /* Direct access files do not generate END conditions,
1957 only I/O errors. */
1958 if (sseek (dtp->u.p.current_unit->s, new) == FAILURE)
1959 generate_error (&dtp->common, ERROR_OS, NULL);
1962 else
1963 { /* Seek by reading data. */
1964 while (dtp->u.p.current_unit->bytes_left > 0)
1966 rlength = length = (MAX_READ > dtp->u.p.current_unit->bytes_left) ?
1967 MAX_READ : dtp->u.p.current_unit->bytes_left;
1969 p = salloc_r (dtp->u.p.current_unit->s, &rlength);
1970 if (p == NULL)
1972 generate_error (&dtp->common, ERROR_OS, NULL);
1973 break;
1976 dtp->u.p.current_unit->bytes_left -= length;
1979 break;
1981 case FORMATTED_STREAM:
1982 case FORMATTED_SEQUENTIAL:
1983 length = 1;
1984 /* sf_read has already terminated input because of an '\n' */
1985 if (dtp->u.p.sf_seen_eor)
1987 dtp->u.p.sf_seen_eor = 0;
1988 break;
1991 if (is_internal_unit (dtp))
1993 if (is_array_io (dtp))
1995 record = next_array_record (dtp, dtp->u.p.current_unit->ls);
1997 /* Now seek to this record. */
1998 record = record * dtp->u.p.current_unit->recl;
1999 if (sseek (dtp->u.p.current_unit->s, record) == FAILURE)
2001 generate_error (&dtp->common, ERROR_INTERNAL_UNIT, NULL);
2002 break;
2004 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2006 else
2008 bytes_left = (int) dtp->u.p.current_unit->bytes_left;
2009 p = salloc_r (dtp->u.p.current_unit->s, &bytes_left);
2010 if (p != NULL)
2011 dtp->u.p.current_unit->bytes_left
2012 = dtp->u.p.current_unit->recl;
2014 break;
2016 else do
2018 p = salloc_r (dtp->u.p.current_unit->s, &length);
2020 if (p == NULL)
2022 generate_error (&dtp->common, ERROR_OS, NULL);
2023 break;
2026 if (length == 0)
2028 dtp->u.p.current_unit->endfile = AT_ENDFILE;
2029 break;
2032 if (is_stream_io (dtp))
2033 dtp->u.p.current_unit->strm_pos++;
2035 while (*p != '\n');
2037 break;
2040 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
2041 test_endfile (dtp->u.p.current_unit);
2045 /* Small utility function to write a record marker, taking care of
2046 byte swapping and of choosing the correct size. */
2048 inline static int
2049 write_us_marker (st_parameter_dt *dtp, const gfc_offset buf)
2051 size_t len;
2052 GFC_INTEGER_4 buf4;
2053 GFC_INTEGER_8 buf8;
2054 char p[sizeof (GFC_INTEGER_8)];
2056 if (compile_options.record_marker == 0)
2057 len = sizeof (gfc_offset);
2058 else
2059 len = compile_options.record_marker;
2061 /* Only CONVERT_NATIVE and CONVERT_SWAP are valid here. */
2062 if (dtp->u.p.current_unit->flags.convert == CONVERT_NATIVE)
2064 switch (compile_options.record_marker)
2066 case 0:
2067 return swrite (dtp->u.p.current_unit->s, &buf, &len);
2068 break;
2070 case sizeof (GFC_INTEGER_4):
2071 buf4 = buf;
2072 return swrite (dtp->u.p.current_unit->s, &buf4, &len);
2073 break;
2075 case sizeof (GFC_INTEGER_8):
2076 buf8 = buf;
2077 return swrite (dtp->u.p.current_unit->s, &buf8, &len);
2078 break;
2080 default:
2081 runtime_error ("Illegal value for record marker");
2082 break;
2085 else
2087 switch (compile_options.record_marker)
2089 case 0:
2090 reverse_memcpy (p, &buf, sizeof (gfc_offset));
2091 return swrite (dtp->u.p.current_unit->s, p, &len);
2092 break;
2094 case sizeof (GFC_INTEGER_4):
2095 buf4 = buf;
2096 reverse_memcpy (p, &buf4, sizeof (GFC_INTEGER_4));
2097 return swrite (dtp->u.p.current_unit->s, p, &len);
2098 break;
2100 case sizeof (GFC_INTEGER_8):
2101 buf8 = buf;
2102 reverse_memcpy (p, &buf8, sizeof (GFC_INTEGER_4));
2103 return swrite (dtp->u.p.current_unit->s, p, &len);
2104 break;
2106 default:
2107 runtime_error ("Illegal value for record marker");
2108 break;
2115 /* Position to the next record in write mode. */
2117 static void
2118 next_record_w (st_parameter_dt *dtp, int done)
2120 gfc_offset c, m, record, max_pos;
2121 int length;
2122 char *p;
2123 size_t record_marker;
2125 /* Zero counters for X- and T-editing. */
2126 max_pos = dtp->u.p.max_pos;
2127 dtp->u.p.max_pos = dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
2129 switch (current_mode (dtp))
2131 /* No records in unformatted STREAM I/O. */
2132 case UNFORMATTED_STREAM:
2133 return;
2135 case FORMATTED_DIRECT:
2136 if (dtp->u.p.current_unit->bytes_left == 0)
2137 break;
2139 if (sset (dtp->u.p.current_unit->s, ' ',
2140 dtp->u.p.current_unit->bytes_left) == FAILURE)
2141 goto io_error;
2143 break;
2145 case UNFORMATTED_DIRECT:
2146 if (sfree (dtp->u.p.current_unit->s) == FAILURE)
2147 goto io_error;
2148 break;
2150 case UNFORMATTED_SEQUENTIAL:
2151 /* Bytes written. */
2152 m = dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left;
2153 c = file_position (dtp->u.p.current_unit->s);
2155 /* Write the length tail. */
2157 if (write_us_marker (dtp, m) != 0)
2158 goto io_error;
2160 if (compile_options.record_marker == 4)
2161 record_marker = sizeof(GFC_INTEGER_4);
2162 else
2163 record_marker = sizeof (gfc_offset);
2165 /* Seek to the head and overwrite the bogus length with the real
2166 length. */
2168 if (sseek (dtp->u.p.current_unit->s, c - m - record_marker)
2169 == FAILURE)
2170 goto io_error;
2172 if (write_us_marker (dtp, m) != 0)
2173 goto io_error;
2175 /* Seek past the end of the current record. */
2177 if (sseek (dtp->u.p.current_unit->s, c + record_marker) == FAILURE)
2178 goto io_error;
2180 break;
2182 case FORMATTED_STREAM:
2183 case FORMATTED_SEQUENTIAL:
2185 if (is_internal_unit (dtp))
2187 if (is_array_io (dtp))
2189 length = (int) dtp->u.p.current_unit->bytes_left;
2191 /* If the farthest position reached is greater than current
2192 position, adjust the position and set length to pad out
2193 whats left. Otherwise just pad whats left.
2194 (for character array unit) */
2195 m = dtp->u.p.current_unit->recl
2196 - dtp->u.p.current_unit->bytes_left;
2197 if (max_pos > m)
2199 length = (int) (max_pos - m);
2200 p = salloc_w (dtp->u.p.current_unit->s, &length);
2201 length = (int) (dtp->u.p.current_unit->recl - max_pos);
2204 if (sset (dtp->u.p.current_unit->s, ' ', length) == FAILURE)
2206 generate_error (&dtp->common, ERROR_END, NULL);
2207 return;
2210 /* Now that the current record has been padded out,
2211 determine where the next record in the array is. */
2212 record = next_array_record (dtp, dtp->u.p.current_unit->ls);
2213 if (record == 0)
2214 dtp->u.p.current_unit->endfile = AT_ENDFILE;
2216 /* Now seek to this record */
2217 record = record * dtp->u.p.current_unit->recl;
2219 if (sseek (dtp->u.p.current_unit->s, record) == FAILURE)
2221 generate_error (&dtp->common, ERROR_INTERNAL_UNIT, NULL);
2222 return;
2225 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2227 else
2229 length = 1;
2231 /* If this is the last call to next_record move to the farthest
2232 position reached and set length to pad out the remainder
2233 of the record. (for character scaler unit) */
2234 if (done)
2236 m = dtp->u.p.current_unit->recl
2237 - dtp->u.p.current_unit->bytes_left;
2238 if (max_pos > m)
2240 length = (int) (max_pos - m);
2241 p = salloc_w (dtp->u.p.current_unit->s, &length);
2242 length = (int) (dtp->u.p.current_unit->recl - max_pos);
2244 else
2245 length = (int) dtp->u.p.current_unit->bytes_left;
2247 if (sset (dtp->u.p.current_unit->s, ' ', length) == FAILURE)
2249 generate_error (&dtp->common, ERROR_END, NULL);
2250 return;
2254 else
2257 /* If this is the last call to next_record move to the farthest
2258 position reached in preparation for completing the record.
2259 (for file unit) */
2260 if (done)
2262 m = dtp->u.p.current_unit->recl -
2263 dtp->u.p.current_unit->bytes_left;
2264 if (max_pos > m)
2266 length = (int) (max_pos - m);
2267 p = salloc_w (dtp->u.p.current_unit->s, &length);
2270 size_t len;
2271 const char crlf[] = "\r\n";
2272 #ifdef HAVE_CRLF
2273 len = 2;
2274 #else
2275 len = 1;
2276 #endif
2277 if (swrite (dtp->u.p.current_unit->s, &crlf[2-len], &len) != 0)
2278 goto io_error;
2280 if (is_stream_io (dtp))
2281 dtp->u.p.current_unit->strm_pos += len;
2284 break;
2286 io_error:
2287 generate_error (&dtp->common, ERROR_OS, NULL);
2288 break;
2292 /* Position to the next record, which means moving to the end of the
2293 current record. This can happen under several different
2294 conditions. If the done flag is not set, we get ready to process
2295 the next record. */
2297 void
2298 next_record (st_parameter_dt *dtp, int done)
2300 gfc_offset fp; /* File position. */
2302 dtp->u.p.current_unit->read_bad = 0;
2304 if (dtp->u.p.mode == READING)
2305 next_record_r (dtp);
2306 else
2307 next_record_w (dtp, done);
2309 if (!is_stream_io (dtp))
2311 /* keep position up to date for INQUIRE */
2312 dtp->u.p.current_unit->flags.position = POSITION_ASIS;
2313 dtp->u.p.current_unit->current_record = 0;
2314 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
2316 fp = file_position (dtp->u.p.current_unit->s);
2317 /* Calculate next record, rounding up partial records. */
2318 dtp->u.p.current_unit->last_record =
2319 (fp + dtp->u.p.current_unit->recl - 1) /
2320 dtp->u.p.current_unit->recl;
2322 else
2323 dtp->u.p.current_unit->last_record++;
2326 if (!done)
2327 pre_position (dtp);
2331 /* Finalize the current data transfer. For a nonadvancing transfer,
2332 this means advancing to the next record. For internal units close the
2333 stream associated with the unit. */
2335 static void
2336 finalize_transfer (st_parameter_dt *dtp)
2338 jmp_buf eof_jump;
2339 GFC_INTEGER_4 cf = dtp->common.flags;
2341 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
2342 *dtp->size = (GFC_INTEGER_4) dtp->u.p.size_used;
2344 if (dtp->u.p.eor_condition)
2346 generate_error (&dtp->common, ERROR_EOR, NULL);
2347 return;
2350 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2351 return;
2353 if ((dtp->u.p.ionml != NULL)
2354 && (cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0)
2356 if ((cf & IOPARM_DT_NAMELIST_READ_MODE) != 0)
2357 namelist_read (dtp);
2358 else
2359 namelist_write (dtp);
2362 dtp->u.p.transfer = NULL;
2363 if (dtp->u.p.current_unit == NULL)
2364 return;
2366 dtp->u.p.eof_jump = &eof_jump;
2367 if (setjmp (eof_jump))
2369 generate_error (&dtp->common, ERROR_END, NULL);
2370 return;
2373 if ((cf & IOPARM_DT_LIST_FORMAT) != 0 && dtp->u.p.mode == READING)
2374 finish_list_read (dtp);
2375 else if (!is_stream_io (dtp))
2377 dtp->u.p.current_unit->current_record = 0;
2378 if (dtp->u.p.advance_status == ADVANCE_NO || dtp->u.p.seen_dollar)
2380 /* Most systems buffer lines, so force the partial record
2381 to be written out. */
2382 if (!is_internal_unit (dtp))
2383 flush (dtp->u.p.current_unit->s);
2384 dtp->u.p.seen_dollar = 0;
2385 return;
2387 next_record (dtp, 1);
2389 else
2391 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED)
2392 next_record (dtp, 1);
2393 flush (dtp->u.p.current_unit->s);
2396 sfree (dtp->u.p.current_unit->s);
2399 /* Transfer function for IOLENGTH. It doesn't actually do any
2400 data transfer, it just updates the length counter. */
2402 static void
2403 iolength_transfer (st_parameter_dt *dtp, bt type __attribute__((unused)),
2404 void *dest __attribute__ ((unused)),
2405 int kind __attribute__((unused)),
2406 size_t size, size_t nelems)
2408 if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
2409 *dtp->iolength += (GFC_INTEGER_4) size * nelems;
2413 /* Initialize the IOLENGTH data transfer. This function is in essence
2414 a very much simplified version of data_transfer_init(), because it
2415 doesn't have to deal with units at all. */
2417 static void
2418 iolength_transfer_init (st_parameter_dt *dtp)
2420 if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
2421 *dtp->iolength = 0;
2423 memset (&dtp->u.p, 0, sizeof (dtp->u.p));
2425 /* Set up the subroutine that will handle the transfers. */
2427 dtp->u.p.transfer = iolength_transfer;
2431 /* Library entry point for the IOLENGTH form of the INQUIRE
2432 statement. The IOLENGTH form requires no I/O to be performed, but
2433 it must still be a runtime library call so that we can determine
2434 the iolength for dynamic arrays and such. */
2436 extern void st_iolength (st_parameter_dt *);
2437 export_proto(st_iolength);
2439 void
2440 st_iolength (st_parameter_dt *dtp)
2442 library_start (&dtp->common);
2443 iolength_transfer_init (dtp);
2446 extern void st_iolength_done (st_parameter_dt *);
2447 export_proto(st_iolength_done);
2449 void
2450 st_iolength_done (st_parameter_dt *dtp __attribute__((unused)))
2452 free_ionml (dtp);
2453 if (dtp->u.p.scratch != NULL)
2454 free_mem (dtp->u.p.scratch);
2455 library_end ();
2459 /* The READ statement. */
2461 extern void st_read (st_parameter_dt *);
2462 export_proto(st_read);
2464 void
2465 st_read (st_parameter_dt *dtp)
2467 library_start (&dtp->common);
2469 data_transfer_init (dtp, 1);
2471 /* Handle complications dealing with the endfile record. It is
2472 significant that this is the only place where ERROR_END is
2473 generated. Reading an end of file elsewhere is either end of
2474 record or an I/O error. */
2476 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
2477 switch (dtp->u.p.current_unit->endfile)
2479 case NO_ENDFILE:
2480 break;
2482 case AT_ENDFILE:
2483 if (!is_internal_unit (dtp))
2485 generate_error (&dtp->common, ERROR_END, NULL);
2486 dtp->u.p.current_unit->endfile = AFTER_ENDFILE;
2487 dtp->u.p.current_unit->current_record = 0;
2489 break;
2491 case AFTER_ENDFILE:
2492 generate_error (&dtp->common, ERROR_ENDFILE, NULL);
2493 dtp->u.p.current_unit->current_record = 0;
2494 break;
2498 extern void st_read_done (st_parameter_dt *);
2499 export_proto(st_read_done);
2501 void
2502 st_read_done (st_parameter_dt *dtp)
2504 finalize_transfer (dtp);
2505 free_format_data (dtp);
2506 free_ionml (dtp);
2507 if (dtp->u.p.scratch != NULL)
2508 free_mem (dtp->u.p.scratch);
2509 if (dtp->u.p.current_unit != NULL)
2510 unlock_unit (dtp->u.p.current_unit);
2512 free_internal_unit (dtp);
2514 library_end ();
2517 extern void st_write (st_parameter_dt *);
2518 export_proto(st_write);
2520 void
2521 st_write (st_parameter_dt *dtp)
2523 library_start (&dtp->common);
2524 data_transfer_init (dtp, 0);
2527 extern void st_write_done (st_parameter_dt *);
2528 export_proto(st_write_done);
2530 void
2531 st_write_done (st_parameter_dt *dtp)
2533 finalize_transfer (dtp);
2535 /* Deal with endfile conditions associated with sequential files. */
2537 if (dtp->u.p.current_unit != NULL
2538 && dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
2539 switch (dtp->u.p.current_unit->endfile)
2541 case AT_ENDFILE: /* Remain at the endfile record. */
2542 break;
2544 case AFTER_ENDFILE:
2545 dtp->u.p.current_unit->endfile = AT_ENDFILE; /* Just at it now. */
2546 break;
2548 case NO_ENDFILE:
2549 /* Get rid of whatever is after this record. */
2550 if (!is_internal_unit (dtp))
2552 flush (dtp->u.p.current_unit->s);
2553 if (struncate (dtp->u.p.current_unit->s) == FAILURE)
2554 generate_error (&dtp->common, ERROR_OS, NULL);
2556 dtp->u.p.current_unit->endfile = AT_ENDFILE;
2557 break;
2560 free_format_data (dtp);
2561 free_ionml (dtp);
2562 if (dtp->u.p.scratch != NULL)
2563 free_mem (dtp->u.p.scratch);
2564 if (dtp->u.p.current_unit != NULL)
2565 unlock_unit (dtp->u.p.current_unit);
2567 free_internal_unit (dtp);
2569 library_end ();
2572 /* Receives the scalar information for namelist objects and stores it
2573 in a linked list of namelist_info types. */
2575 extern void st_set_nml_var (st_parameter_dt *dtp, void *, char *,
2576 GFC_INTEGER_4, gfc_charlen_type, GFC_INTEGER_4);
2577 export_proto(st_set_nml_var);
2580 void
2581 st_set_nml_var (st_parameter_dt *dtp, void * var_addr, char * var_name,
2582 GFC_INTEGER_4 len, gfc_charlen_type string_length,
2583 GFC_INTEGER_4 dtype)
2585 namelist_info *t1 = NULL;
2586 namelist_info *nml;
2588 nml = (namelist_info*) get_mem (sizeof (namelist_info));
2590 nml->mem_pos = var_addr;
2592 nml->var_name = (char*) get_mem (strlen (var_name) + 1);
2593 strcpy (nml->var_name, var_name);
2595 nml->len = (int) len;
2596 nml->string_length = (index_type) string_length;
2598 nml->var_rank = (int) (dtype & GFC_DTYPE_RANK_MASK);
2599 nml->size = (index_type) (dtype >> GFC_DTYPE_SIZE_SHIFT);
2600 nml->type = (bt) ((dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT);
2602 if (nml->var_rank > 0)
2604 nml->dim = (descriptor_dimension*)
2605 get_mem (nml->var_rank * sizeof (descriptor_dimension));
2606 nml->ls = (array_loop_spec*)
2607 get_mem (nml->var_rank * sizeof (array_loop_spec));
2609 else
2611 nml->dim = NULL;
2612 nml->ls = NULL;
2615 nml->next = NULL;
2617 if ((dtp->common.flags & IOPARM_DT_IONML_SET) == 0)
2619 dtp->common.flags |= IOPARM_DT_IONML_SET;
2620 dtp->u.p.ionml = nml;
2622 else
2624 for (t1 = dtp->u.p.ionml; t1->next; t1 = t1->next);
2625 t1->next = nml;
2629 /* Store the dimensional information for the namelist object. */
2630 extern void st_set_nml_var_dim (st_parameter_dt *, GFC_INTEGER_4,
2631 GFC_INTEGER_4, GFC_INTEGER_4,
2632 GFC_INTEGER_4);
2633 export_proto(st_set_nml_var_dim);
2635 void
2636 st_set_nml_var_dim (st_parameter_dt *dtp, GFC_INTEGER_4 n_dim,
2637 GFC_INTEGER_4 stride, GFC_INTEGER_4 lbound,
2638 GFC_INTEGER_4 ubound)
2640 namelist_info * nml;
2641 int n;
2643 n = (int)n_dim;
2645 for (nml = dtp->u.p.ionml; nml->next; nml = nml->next);
2647 nml->dim[n].stride = (ssize_t)stride;
2648 nml->dim[n].lbound = (ssize_t)lbound;
2649 nml->dim[n].ubound = (ssize_t)ubound;
2652 /* Reverse memcpy - used for byte swapping. */
2654 void reverse_memcpy (void *dest, const void *src, size_t n)
2656 char *d, *s;
2657 size_t i;
2659 d = (char *) dest;
2660 s = (char *) src + n - 1;
2662 /* Write with ascending order - this is likely faster
2663 on modern architectures because of write combining. */
2664 for (i=0; i<n; i++)
2665 *(d++) = *(s--);