recog.c (split_all_insns): Remove dead code.
[official-gcc.git] / libgfortran / io / transfer.c
blobe327eea8f8041c0620686c697dae9decd0356d2d
1 /* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
2 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
4 Namelist transfer functions contributed by Paul Thomas
5 F2003 I/O support contributed by Jerry DeLisle
7 This file is part of the GNU Fortran runtime library (libgfortran).
9 Libgfortran is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
14 Libgfortran is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 Under Section 7 of GPL version 3, you are granted additional
20 permissions described in the GCC Runtime Library Exception, version
21 3.1, as published by the Free Software Foundation.
23 You should have received a copy of the GNU General Public License and
24 a copy of the GCC Runtime Library Exception along with this program;
25 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
26 <http://www.gnu.org/licenses/>. */
29 /* transfer.c -- Top level handling of data transfer statements. */
31 #include "io.h"
32 #include "fbuf.h"
33 #include "format.h"
34 #include "unix.h"
35 #include <string.h>
36 #include <assert.h>
37 #include <stdlib.h>
38 #include <errno.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_character_wide
57 transfer_real
58 transfer_complex
60 These subroutines do not return status.
62 The last call is a call to st_[read|write]_done(). While
63 something can easily go wrong with the initial st_read() or
64 st_write(), an error inhibits any data from actually being
65 transferred. */
67 extern void transfer_integer (st_parameter_dt *, void *, int);
68 export_proto(transfer_integer);
70 extern void transfer_integer_write (st_parameter_dt *, void *, int);
71 export_proto(transfer_integer_write);
73 extern void transfer_real (st_parameter_dt *, void *, int);
74 export_proto(transfer_real);
76 extern void transfer_real_write (st_parameter_dt *, void *, int);
77 export_proto(transfer_real_write);
79 extern void transfer_logical (st_parameter_dt *, void *, int);
80 export_proto(transfer_logical);
82 extern void transfer_logical_write (st_parameter_dt *, void *, int);
83 export_proto(transfer_logical_write);
85 extern void transfer_character (st_parameter_dt *, void *, int);
86 export_proto(transfer_character);
88 extern void transfer_character_write (st_parameter_dt *, void *, int);
89 export_proto(transfer_character_write);
91 extern void transfer_character_wide (st_parameter_dt *, void *, int, int);
92 export_proto(transfer_character_wide);
94 extern void transfer_character_wide_write (st_parameter_dt *,
95 void *, int, int);
96 export_proto(transfer_character_wide_write);
98 extern void transfer_complex (st_parameter_dt *, void *, int);
99 export_proto(transfer_complex);
101 extern void transfer_complex_write (st_parameter_dt *, void *, int);
102 export_proto(transfer_complex_write);
104 extern void transfer_array (st_parameter_dt *, gfc_array_char *, int,
105 gfc_charlen_type);
106 export_proto(transfer_array);
108 extern void transfer_array_write (st_parameter_dt *, gfc_array_char *, int,
109 gfc_charlen_type);
110 export_proto(transfer_array_write);
112 static void us_read (st_parameter_dt *, int);
113 static void us_write (st_parameter_dt *, int);
114 static void next_record_r_unf (st_parameter_dt *, int);
115 static void next_record_w_unf (st_parameter_dt *, int);
117 static const st_option advance_opt[] = {
118 {"yes", ADVANCE_YES},
119 {"no", ADVANCE_NO},
120 {NULL, 0}
124 static const st_option decimal_opt[] = {
125 {"point", DECIMAL_POINT},
126 {"comma", DECIMAL_COMMA},
127 {NULL, 0}
130 static const st_option round_opt[] = {
131 {"up", ROUND_UP},
132 {"down", ROUND_DOWN},
133 {"zero", ROUND_ZERO},
134 {"nearest", ROUND_NEAREST},
135 {"compatible", ROUND_COMPATIBLE},
136 {"processor_defined", ROUND_PROCDEFINED},
137 {NULL, 0}
141 static const st_option sign_opt[] = {
142 {"plus", SIGN_SP},
143 {"suppress", SIGN_SS},
144 {"processor_defined", SIGN_S},
145 {NULL, 0}
148 static const st_option blank_opt[] = {
149 {"null", BLANK_NULL},
150 {"zero", BLANK_ZERO},
151 {NULL, 0}
154 static const st_option delim_opt[] = {
155 {"apostrophe", DELIM_APOSTROPHE},
156 {"quote", DELIM_QUOTE},
157 {"none", DELIM_NONE},
158 {NULL, 0}
161 static const st_option pad_opt[] = {
162 {"yes", PAD_YES},
163 {"no", PAD_NO},
164 {NULL, 0}
167 typedef enum
168 { FORMATTED_SEQUENTIAL, UNFORMATTED_SEQUENTIAL,
169 FORMATTED_DIRECT, UNFORMATTED_DIRECT, FORMATTED_STREAM, UNFORMATTED_STREAM
171 file_mode;
174 static file_mode
175 current_mode (st_parameter_dt *dtp)
177 file_mode m;
179 m = FORM_UNSPECIFIED;
181 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
183 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
184 FORMATTED_DIRECT : UNFORMATTED_DIRECT;
186 else if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
188 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
189 FORMATTED_SEQUENTIAL : UNFORMATTED_SEQUENTIAL;
191 else if (dtp->u.p.current_unit->flags.access == ACCESS_STREAM)
193 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
194 FORMATTED_STREAM : UNFORMATTED_STREAM;
197 return m;
201 /* Mid level data transfer statements. */
203 /* Read sequential file - internal unit */
205 static char *
206 read_sf_internal (st_parameter_dt *dtp, int * length)
208 static char *empty_string[0];
209 char *base;
210 int lorig;
212 /* Zero size array gives internal unit len of 0. Nothing to read. */
213 if (dtp->internal_unit_len == 0
214 && dtp->u.p.current_unit->pad_status == PAD_NO)
215 hit_eof (dtp);
217 /* If we have seen an eor previously, return a length of 0. The
218 caller is responsible for correctly padding the input field. */
219 if (dtp->u.p.sf_seen_eor)
221 *length = 0;
222 /* Just return something that isn't a NULL pointer, otherwise the
223 caller thinks an error occured. */
224 return (char*) empty_string;
227 lorig = *length;
228 if (is_char4_unit(dtp))
230 int i;
231 gfc_char4_t *p = (gfc_char4_t *) mem_alloc_r4 (dtp->u.p.current_unit->s,
232 length);
233 base = fbuf_alloc (dtp->u.p.current_unit, lorig);
234 for (i = 0; i < *length; i++, p++)
235 base[i] = *p > 255 ? '?' : (unsigned char) *p;
237 else
238 base = mem_alloc_r (dtp->u.p.current_unit->s, length);
240 if (unlikely (lorig > *length))
242 hit_eof (dtp);
243 return NULL;
246 dtp->u.p.current_unit->bytes_left -= *length;
248 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
249 dtp->u.p.size_used += (GFC_IO_INT) *length;
251 return base;
255 /* When reading sequential formatted records we have a problem. We
256 don't know how long the line is until we read the trailing newline,
257 and we don't want to read too much. If we read too much, we might
258 have to do a physical seek backwards depending on how much data is
259 present, and devices like terminals aren't seekable and would cause
260 an I/O error.
262 Given this, the solution is to read a byte at a time, stopping if
263 we hit the newline. For small allocations, we use a static buffer.
264 For larger allocations, we are forced to allocate memory on the
265 heap. Hopefully this won't happen very often. */
267 /* Read sequential file - external unit */
269 static char *
270 read_sf (st_parameter_dt *dtp, int * length)
272 static char *empty_string[0];
273 char *base, *p, q;
274 int n, lorig, seen_comma;
276 /* If we have seen an eor previously, return a length of 0. The
277 caller is responsible for correctly padding the input field. */
278 if (dtp->u.p.sf_seen_eor)
280 *length = 0;
281 /* Just return something that isn't a NULL pointer, otherwise the
282 caller thinks an error occured. */
283 return (char*) empty_string;
286 n = seen_comma = 0;
288 /* Read data into format buffer and scan through it. */
289 lorig = *length;
290 base = p = fbuf_read (dtp->u.p.current_unit, length);
291 if (base == NULL)
292 return NULL;
294 while (n < *length)
296 q = *p;
298 if (q == '\n' || q == '\r')
300 /* Unexpected end of line. Set the position. */
301 fbuf_seek (dtp->u.p.current_unit, n + 1 ,SEEK_CUR);
302 dtp->u.p.sf_seen_eor = 1;
304 /* If we see an EOR during non-advancing I/O, we need to skip
305 the rest of the I/O statement. Set the corresponding flag. */
306 if (dtp->u.p.advance_status == ADVANCE_NO || dtp->u.p.seen_dollar)
307 dtp->u.p.eor_condition = 1;
309 /* If we encounter a CR, it might be a CRLF. */
310 if (q == '\r') /* Probably a CRLF */
312 /* See if there is an LF. Use fbuf_read rather then fbuf_getc so
313 the position is not advanced unless it really is an LF. */
314 int readlen = 1;
315 p = fbuf_read (dtp->u.p.current_unit, &readlen);
316 if (*p == '\n' && readlen == 1)
318 dtp->u.p.sf_seen_eor = 2;
319 fbuf_seek (dtp->u.p.current_unit, 1 ,SEEK_CUR);
323 /* Without padding, terminate the I/O statement without assigning
324 the value. With padding, the value still needs to be assigned,
325 so we can just continue with a short read. */
326 if (dtp->u.p.current_unit->pad_status == PAD_NO)
328 generate_error (&dtp->common, LIBERROR_EOR, NULL);
329 return NULL;
332 *length = n;
333 goto done;
335 /* Short circuit the read if a comma is found during numeric input.
336 The flag is set to zero during character reads so that commas in
337 strings are not ignored */
338 if (q == ',')
339 if (dtp->u.p.sf_read_comma == 1)
341 seen_comma = 1;
342 notify_std (&dtp->common, GFC_STD_GNU,
343 "Comma in formatted numeric read.");
344 *length = n;
345 break;
347 n++;
348 p++;
351 fbuf_seek (dtp->u.p.current_unit, n + seen_comma, SEEK_CUR);
353 /* A short read implies we hit EOF, unless we hit EOR, a comma, or
354 some other stuff. Set the relevant flags. */
355 if (lorig > *length && !dtp->u.p.sf_seen_eor && !seen_comma)
357 if (n > 0)
359 if (dtp->u.p.advance_status == ADVANCE_NO)
361 if (dtp->u.p.current_unit->pad_status == PAD_NO)
363 hit_eof (dtp);
364 return NULL;
366 else
367 dtp->u.p.eor_condition = 1;
369 else
370 dtp->u.p.at_eof = 1;
372 else if (dtp->u.p.advance_status == ADVANCE_NO
373 || dtp->u.p.current_unit->pad_status == PAD_NO
374 || dtp->u.p.current_unit->bytes_left
375 == dtp->u.p.current_unit->recl)
377 hit_eof (dtp);
378 return NULL;
382 done:
384 dtp->u.p.current_unit->bytes_left -= n;
386 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
387 dtp->u.p.size_used += (GFC_IO_INT) n;
389 return base;
393 /* Function for reading the next couple of bytes from the current
394 file, advancing the current position. We return FAILURE on end of record or
395 end of file. This function is only for formatted I/O, unformatted uses
396 read_block_direct.
398 If the read is short, then it is because the current record does not
399 have enough data to satisfy the read request and the file was
400 opened with PAD=YES. The caller must assume tailing spaces for
401 short reads. */
403 void *
404 read_block_form (st_parameter_dt *dtp, int * nbytes)
406 char *source;
407 int norig;
409 if (!is_stream_io (dtp))
411 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) *nbytes)
413 /* For preconnected units with default record length, set bytes left
414 to unit record length and proceed, otherwise error. */
415 if (dtp->u.p.current_unit->unit_number == options.stdin_unit
416 && dtp->u.p.current_unit->recl == DEFAULT_RECL)
417 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
418 else
420 if (unlikely (dtp->u.p.current_unit->pad_status == PAD_NO)
421 && !is_internal_unit (dtp))
423 /* Not enough data left. */
424 generate_error (&dtp->common, LIBERROR_EOR, NULL);
425 return NULL;
429 if (unlikely (dtp->u.p.current_unit->bytes_left == 0
430 && !is_internal_unit(dtp)))
432 hit_eof (dtp);
433 return NULL;
436 *nbytes = dtp->u.p.current_unit->bytes_left;
440 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
441 (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL ||
442 dtp->u.p.current_unit->flags.access == ACCESS_STREAM))
444 if (is_internal_unit (dtp))
445 source = read_sf_internal (dtp, nbytes);
446 else
447 source = read_sf (dtp, nbytes);
449 dtp->u.p.current_unit->strm_pos +=
450 (gfc_offset) (*nbytes + dtp->u.p.sf_seen_eor);
451 return source;
454 /* If we reach here, we can assume it's direct access. */
456 dtp->u.p.current_unit->bytes_left -= (gfc_offset) *nbytes;
458 norig = *nbytes;
459 source = fbuf_read (dtp->u.p.current_unit, nbytes);
460 fbuf_seek (dtp->u.p.current_unit, *nbytes, SEEK_CUR);
462 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
463 dtp->u.p.size_used += (GFC_IO_INT) *nbytes;
465 if (norig != *nbytes)
467 /* Short read, this shouldn't happen. */
468 if (!dtp->u.p.current_unit->pad_status == PAD_YES)
470 generate_error (&dtp->common, LIBERROR_EOR, NULL);
471 source = NULL;
475 dtp->u.p.current_unit->strm_pos += (gfc_offset) *nbytes;
477 return source;
481 /* Read a block from a character(kind=4) internal unit, to be transferred into
482 a character(kind=4) variable. Note: Portions of this code borrowed from
483 read_sf_internal. */
484 void *
485 read_block_form4 (st_parameter_dt *dtp, int * nbytes)
487 static gfc_char4_t *empty_string[0];
488 gfc_char4_t *source;
489 int lorig;
491 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) *nbytes)
492 *nbytes = dtp->u.p.current_unit->bytes_left;
494 /* Zero size array gives internal unit len of 0. Nothing to read. */
495 if (dtp->internal_unit_len == 0
496 && dtp->u.p.current_unit->pad_status == PAD_NO)
497 hit_eof (dtp);
499 /* If we have seen an eor previously, return a length of 0. The
500 caller is responsible for correctly padding the input field. */
501 if (dtp->u.p.sf_seen_eor)
503 *nbytes = 0;
504 /* Just return something that isn't a NULL pointer, otherwise the
505 caller thinks an error occured. */
506 return empty_string;
509 lorig = *nbytes;
510 source = (gfc_char4_t *) mem_alloc_r4 (dtp->u.p.current_unit->s, nbytes);
512 if (unlikely (lorig > *nbytes))
514 hit_eof (dtp);
515 return NULL;
518 dtp->u.p.current_unit->bytes_left -= *nbytes;
520 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
521 dtp->u.p.size_used += (GFC_IO_INT) *nbytes;
523 return source;
527 /* Reads a block directly into application data space. This is for
528 unformatted files. */
530 static void
531 read_block_direct (st_parameter_dt *dtp, void *buf, size_t nbytes)
533 ssize_t to_read_record;
534 ssize_t have_read_record;
535 ssize_t to_read_subrecord;
536 ssize_t have_read_subrecord;
537 int short_record;
539 if (is_stream_io (dtp))
541 have_read_record = sread (dtp->u.p.current_unit->s, buf,
542 nbytes);
543 if (unlikely (have_read_record < 0))
545 generate_error (&dtp->common, LIBERROR_OS, NULL);
546 return;
549 dtp->u.p.current_unit->strm_pos += (gfc_offset) have_read_record;
551 if (unlikely ((ssize_t) nbytes != have_read_record))
553 /* Short read, e.g. if we hit EOF. For stream files,
554 we have to set the end-of-file condition. */
555 hit_eof (dtp);
557 return;
560 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
562 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) nbytes)
564 short_record = 1;
565 to_read_record = dtp->u.p.current_unit->bytes_left;
566 nbytes = to_read_record;
568 else
570 short_record = 0;
571 to_read_record = nbytes;
574 dtp->u.p.current_unit->bytes_left -= to_read_record;
576 to_read_record = sread (dtp->u.p.current_unit->s, buf, to_read_record);
577 if (unlikely (to_read_record < 0))
579 generate_error (&dtp->common, LIBERROR_OS, NULL);
580 return;
583 if (to_read_record != (ssize_t) nbytes)
585 /* Short read, e.g. if we hit EOF. Apparently, we read
586 more than was written to the last record. */
587 return;
590 if (unlikely (short_record))
592 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
594 return;
597 /* Unformatted sequential. We loop over the subrecords, reading
598 until the request has been fulfilled or the record has run out
599 of continuation subrecords. */
601 /* Check whether we exceed the total record length. */
603 if (dtp->u.p.current_unit->flags.has_recl
604 && ((gfc_offset) nbytes > dtp->u.p.current_unit->bytes_left))
606 to_read_record = dtp->u.p.current_unit->bytes_left;
607 short_record = 1;
609 else
611 to_read_record = nbytes;
612 short_record = 0;
614 have_read_record = 0;
616 while(1)
618 if (dtp->u.p.current_unit->bytes_left_subrecord
619 < (gfc_offset) to_read_record)
621 to_read_subrecord = dtp->u.p.current_unit->bytes_left_subrecord;
622 to_read_record -= to_read_subrecord;
624 else
626 to_read_subrecord = to_read_record;
627 to_read_record = 0;
630 dtp->u.p.current_unit->bytes_left_subrecord -= to_read_subrecord;
632 have_read_subrecord = sread (dtp->u.p.current_unit->s,
633 buf + have_read_record, to_read_subrecord);
634 if (unlikely (have_read_subrecord) < 0)
636 generate_error (&dtp->common, LIBERROR_OS, NULL);
637 return;
640 have_read_record += have_read_subrecord;
642 if (unlikely (to_read_subrecord != have_read_subrecord))
644 /* Short read, e.g. if we hit EOF. This means the record
645 structure has been corrupted, or the trailing record
646 marker would still be present. */
648 generate_error (&dtp->common, LIBERROR_CORRUPT_FILE, NULL);
649 return;
652 if (to_read_record > 0)
654 if (likely (dtp->u.p.current_unit->continued))
656 next_record_r_unf (dtp, 0);
657 us_read (dtp, 1);
659 else
661 /* Let's make sure the file position is correctly pre-positioned
662 for the next read statement. */
664 dtp->u.p.current_unit->current_record = 0;
665 next_record_r_unf (dtp, 0);
666 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
667 return;
670 else
672 /* Normal exit, the read request has been fulfilled. */
673 break;
677 dtp->u.p.current_unit->bytes_left -= have_read_record;
678 if (unlikely (short_record))
680 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
681 return;
683 return;
687 /* Function for writing a block of bytes to the current file at the
688 current position, advancing the file pointer. We are given a length
689 and return a pointer to a buffer that the caller must (completely)
690 fill in. Returns NULL on error. */
692 void *
693 write_block (st_parameter_dt *dtp, int length)
695 char *dest;
697 if (!is_stream_io (dtp))
699 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) length)
701 /* For preconnected units with default record length, set bytes left
702 to unit record length and proceed, otherwise error. */
703 if (likely ((dtp->u.p.current_unit->unit_number
704 == options.stdout_unit
705 || dtp->u.p.current_unit->unit_number
706 == options.stderr_unit)
707 && dtp->u.p.current_unit->recl == DEFAULT_RECL))
708 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
709 else
711 generate_error (&dtp->common, LIBERROR_EOR, NULL);
712 return NULL;
716 dtp->u.p.current_unit->bytes_left -= (gfc_offset) length;
719 if (is_internal_unit (dtp))
721 if (dtp->common.unit) /* char4 internel unit. */
723 gfc_char4_t *dest4;
724 dest4 = mem_alloc_w4 (dtp->u.p.current_unit->s, &length);
725 if (dest4 == NULL)
727 generate_error (&dtp->common, LIBERROR_END, NULL);
728 return NULL;
730 return dest4;
732 else
733 dest = mem_alloc_w (dtp->u.p.current_unit->s, &length);
735 if (dest == NULL)
737 generate_error (&dtp->common, LIBERROR_END, NULL);
738 return NULL;
741 if (unlikely (dtp->u.p.current_unit->endfile == AT_ENDFILE))
742 generate_error (&dtp->common, LIBERROR_END, NULL);
744 else
746 dest = fbuf_alloc (dtp->u.p.current_unit, length);
747 if (dest == NULL)
749 generate_error (&dtp->common, LIBERROR_OS, NULL);
750 return NULL;
754 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
755 dtp->u.p.size_used += (GFC_IO_INT) length;
757 dtp->u.p.current_unit->strm_pos += (gfc_offset) length;
759 return dest;
763 /* High level interface to swrite(), taking care of errors. This is only
764 called for unformatted files. There are three cases to consider:
765 Stream I/O, unformatted direct, unformatted sequential. */
767 static try
768 write_buf (st_parameter_dt *dtp, void *buf, size_t nbytes)
771 ssize_t have_written;
772 ssize_t to_write_subrecord;
773 int short_record;
775 /* Stream I/O. */
777 if (is_stream_io (dtp))
779 have_written = swrite (dtp->u.p.current_unit->s, buf, nbytes);
780 if (unlikely (have_written < 0))
782 generate_error (&dtp->common, LIBERROR_OS, NULL);
783 return FAILURE;
786 dtp->u.p.current_unit->strm_pos += (gfc_offset) have_written;
788 return SUCCESS;
791 /* Unformatted direct access. */
793 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
795 if (unlikely (dtp->u.p.current_unit->bytes_left < (gfc_offset) nbytes))
797 generate_error (&dtp->common, LIBERROR_DIRECT_EOR, NULL);
798 return FAILURE;
801 if (buf == NULL && nbytes == 0)
802 return SUCCESS;
804 have_written = swrite (dtp->u.p.current_unit->s, buf, nbytes);
805 if (unlikely (have_written < 0))
807 generate_error (&dtp->common, LIBERROR_OS, NULL);
808 return FAILURE;
811 dtp->u.p.current_unit->strm_pos += (gfc_offset) have_written;
812 dtp->u.p.current_unit->bytes_left -= (gfc_offset) have_written;
814 return SUCCESS;
817 /* Unformatted sequential. */
819 have_written = 0;
821 if (dtp->u.p.current_unit->flags.has_recl
822 && (gfc_offset) nbytes > dtp->u.p.current_unit->bytes_left)
824 nbytes = dtp->u.p.current_unit->bytes_left;
825 short_record = 1;
827 else
829 short_record = 0;
832 while (1)
835 to_write_subrecord =
836 (size_t) dtp->u.p.current_unit->bytes_left_subrecord < nbytes ?
837 (size_t) dtp->u.p.current_unit->bytes_left_subrecord : nbytes;
839 dtp->u.p.current_unit->bytes_left_subrecord -=
840 (gfc_offset) to_write_subrecord;
842 to_write_subrecord = swrite (dtp->u.p.current_unit->s,
843 buf + have_written, to_write_subrecord);
844 if (unlikely (to_write_subrecord < 0))
846 generate_error (&dtp->common, LIBERROR_OS, NULL);
847 return FAILURE;
850 dtp->u.p.current_unit->strm_pos += (gfc_offset) to_write_subrecord;
851 nbytes -= to_write_subrecord;
852 have_written += to_write_subrecord;
854 if (nbytes == 0)
855 break;
857 next_record_w_unf (dtp, 1);
858 us_write (dtp, 1);
860 dtp->u.p.current_unit->bytes_left -= have_written;
861 if (unlikely (short_record))
863 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
864 return FAILURE;
866 return SUCCESS;
870 /* Master function for unformatted reads. */
872 static void
873 unformatted_read (st_parameter_dt *dtp, bt type,
874 void *dest, int kind, size_t size, size_t nelems)
876 if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE)
877 || kind == 1)
879 if (type == BT_CHARACTER)
880 size *= GFC_SIZE_OF_CHAR_KIND(kind);
881 read_block_direct (dtp, dest, size * nelems);
883 else
885 char buffer[16];
886 char *p;
887 size_t i;
889 p = dest;
891 /* Handle wide chracters. */
892 if (type == BT_CHARACTER && kind != 1)
894 nelems *= size;
895 size = kind;
898 /* Break up complex into its constituent reals. */
899 if (type == BT_COMPLEX)
901 nelems *= 2;
902 size /= 2;
905 /* By now, all complex variables have been split into their
906 constituent reals. */
908 for (i = 0; i < nelems; i++)
910 read_block_direct (dtp, buffer, size);
911 reverse_memcpy (p, buffer, size);
912 p += size;
918 /* Master function for unformatted writes. NOTE: For kind=10 the size is 16
919 bytes on 64 bit machines. The unused bytes are not initialized and never
920 used, which can show an error with memory checking analyzers like
921 valgrind. */
923 static void
924 unformatted_write (st_parameter_dt *dtp, bt type,
925 void *source, int kind, size_t size, size_t nelems)
927 if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE)
928 || kind == 1)
930 size_t stride = type == BT_CHARACTER ?
931 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
933 write_buf (dtp, source, stride * nelems);
935 else
937 char buffer[16];
938 char *p;
939 size_t i;
941 p = source;
943 /* Handle wide chracters. */
944 if (type == BT_CHARACTER && kind != 1)
946 nelems *= size;
947 size = kind;
950 /* Break up complex into its constituent reals. */
951 if (type == BT_COMPLEX)
953 nelems *= 2;
954 size /= 2;
957 /* By now, all complex variables have been split into their
958 constituent reals. */
960 for (i = 0; i < nelems; i++)
962 reverse_memcpy(buffer, p, size);
963 p += size;
964 write_buf (dtp, buffer, size);
970 /* Return a pointer to the name of a type. */
972 const char *
973 type_name (bt type)
975 const char *p;
977 switch (type)
979 case BT_INTEGER:
980 p = "INTEGER";
981 break;
982 case BT_LOGICAL:
983 p = "LOGICAL";
984 break;
985 case BT_CHARACTER:
986 p = "CHARACTER";
987 break;
988 case BT_REAL:
989 p = "REAL";
990 break;
991 case BT_COMPLEX:
992 p = "COMPLEX";
993 break;
994 default:
995 internal_error (NULL, "type_name(): Bad type");
998 return p;
1002 /* Write a constant string to the output.
1003 This is complicated because the string can have doubled delimiters
1004 in it. The length in the format node is the true length. */
1006 static void
1007 write_constant_string (st_parameter_dt *dtp, const fnode *f)
1009 char c, delimiter, *p, *q;
1010 int length;
1012 length = f->u.string.length;
1013 if (length == 0)
1014 return;
1016 p = write_block (dtp, length);
1017 if (p == NULL)
1018 return;
1020 q = f->u.string.p;
1021 delimiter = q[-1];
1023 for (; length > 0; length--)
1025 c = *p++ = *q++;
1026 if (c == delimiter && c != 'H' && c != 'h')
1027 q++; /* Skip the doubled delimiter. */
1032 /* Given actual and expected types in a formatted data transfer, make
1033 sure they agree. If not, an error message is generated. Returns
1034 nonzero if something went wrong. */
1036 static int
1037 require_type (st_parameter_dt *dtp, bt expected, bt actual, const fnode *f)
1039 char buffer[100];
1041 if (actual == expected)
1042 return 0;
1044 /* Adjust item_count before emitting error message. */
1045 sprintf (buffer, "Expected %s for item %d in formatted transfer, got %s",
1046 type_name (expected), dtp->u.p.item_count - 1, type_name (actual));
1048 format_error (dtp, f, buffer);
1049 return 1;
1053 /* This function is in the main loop for a formatted data transfer
1054 statement. It would be natural to implement this as a coroutine
1055 with the user program, but C makes that awkward. We loop,
1056 processing format elements. When we actually have to transfer
1057 data instead of just setting flags, we return control to the user
1058 program which calls a function that supplies the address and type
1059 of the next element, then comes back here to process it. */
1061 static void
1062 formatted_transfer_scalar_read (st_parameter_dt *dtp, bt type, void *p, int kind,
1063 size_t size)
1065 int pos, bytes_used;
1066 const fnode *f;
1067 format_token t;
1068 int n;
1069 int consume_data_flag;
1071 /* Change a complex data item into a pair of reals. */
1073 n = (p == NULL) ? 0 : ((type != BT_COMPLEX) ? 1 : 2);
1074 if (type == BT_COMPLEX)
1076 type = BT_REAL;
1077 size /= 2;
1080 /* If there's an EOR condition, we simulate finalizing the transfer
1081 by doing nothing. */
1082 if (dtp->u.p.eor_condition)
1083 return;
1085 /* Set this flag so that commas in reads cause the read to complete before
1086 the entire field has been read. The next read field will start right after
1087 the comma in the stream. (Set to 0 for character reads). */
1088 dtp->u.p.sf_read_comma =
1089 dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA ? 0 : 1;
1091 for (;;)
1093 /* If reversion has occurred and there is another real data item,
1094 then we have to move to the next record. */
1095 if (dtp->u.p.reversion_flag && n > 0)
1097 dtp->u.p.reversion_flag = 0;
1098 next_record (dtp, 0);
1101 consume_data_flag = 1;
1102 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1103 break;
1105 f = next_format (dtp);
1106 if (f == NULL)
1108 /* No data descriptors left. */
1109 if (unlikely (n > 0))
1110 generate_error (&dtp->common, LIBERROR_FORMAT,
1111 "Insufficient data descriptors in format after reversion");
1112 return;
1115 t = f->format;
1117 bytes_used = (int)(dtp->u.p.current_unit->recl
1118 - dtp->u.p.current_unit->bytes_left);
1120 if (is_stream_io(dtp))
1121 bytes_used = 0;
1123 switch (t)
1125 case FMT_I:
1126 if (n == 0)
1127 goto need_read_data;
1128 if (require_type (dtp, BT_INTEGER, type, f))
1129 return;
1130 read_decimal (dtp, f, p, kind);
1131 break;
1133 case FMT_B:
1134 if (n == 0)
1135 goto need_read_data;
1136 if (!(compile_options.allow_std & GFC_STD_GNU)
1137 && require_type (dtp, BT_INTEGER, type, f))
1138 return;
1139 read_radix (dtp, f, p, kind, 2);
1140 break;
1142 case FMT_O:
1143 if (n == 0)
1144 goto need_read_data;
1145 if (!(compile_options.allow_std & GFC_STD_GNU)
1146 && require_type (dtp, BT_INTEGER, type, f))
1147 return;
1148 read_radix (dtp, f, p, kind, 8);
1149 break;
1151 case FMT_Z:
1152 if (n == 0)
1153 goto need_read_data;
1154 if (!(compile_options.allow_std & GFC_STD_GNU)
1155 && require_type (dtp, BT_INTEGER, type, f))
1156 return;
1157 read_radix (dtp, f, p, kind, 16);
1158 break;
1160 case FMT_A:
1161 if (n == 0)
1162 goto need_read_data;
1164 /* It is possible to have FMT_A with something not BT_CHARACTER such
1165 as when writing out hollerith strings, so check both type
1166 and kind before calling wide character routines. */
1167 if (type == BT_CHARACTER && kind == 4)
1168 read_a_char4 (dtp, f, p, size);
1169 else
1170 read_a (dtp, f, p, size);
1171 break;
1173 case FMT_L:
1174 if (n == 0)
1175 goto need_read_data;
1176 read_l (dtp, f, p, kind);
1177 break;
1179 case FMT_D:
1180 if (n == 0)
1181 goto need_read_data;
1182 if (require_type (dtp, BT_REAL, type, f))
1183 return;
1184 read_f (dtp, f, p, kind);
1185 break;
1187 case FMT_E:
1188 if (n == 0)
1189 goto need_read_data;
1190 if (require_type (dtp, BT_REAL, type, f))
1191 return;
1192 read_f (dtp, f, p, kind);
1193 break;
1195 case FMT_EN:
1196 if (n == 0)
1197 goto need_read_data;
1198 if (require_type (dtp, BT_REAL, type, f))
1199 return;
1200 read_f (dtp, f, p, kind);
1201 break;
1203 case FMT_ES:
1204 if (n == 0)
1205 goto need_read_data;
1206 if (require_type (dtp, BT_REAL, type, f))
1207 return;
1208 read_f (dtp, f, p, kind);
1209 break;
1211 case FMT_F:
1212 if (n == 0)
1213 goto need_read_data;
1214 if (require_type (dtp, BT_REAL, type, f))
1215 return;
1216 read_f (dtp, f, p, kind);
1217 break;
1219 case FMT_G:
1220 if (n == 0)
1221 goto need_read_data;
1222 switch (type)
1224 case BT_INTEGER:
1225 read_decimal (dtp, f, p, kind);
1226 break;
1227 case BT_LOGICAL:
1228 read_l (dtp, f, p, kind);
1229 break;
1230 case BT_CHARACTER:
1231 if (kind == 4)
1232 read_a_char4 (dtp, f, p, size);
1233 else
1234 read_a (dtp, f, p, size);
1235 break;
1236 case BT_REAL:
1237 read_f (dtp, f, p, kind);
1238 break;
1239 default:
1240 internal_error (&dtp->common, "formatted_transfer(): Bad type");
1242 break;
1244 case FMT_STRING:
1245 consume_data_flag = 0;
1246 format_error (dtp, f, "Constant string in input format");
1247 return;
1249 /* Format codes that don't transfer data. */
1250 case FMT_X:
1251 case FMT_TR:
1252 consume_data_flag = 0;
1253 dtp->u.p.skips += f->u.n;
1254 pos = bytes_used + dtp->u.p.skips - 1;
1255 dtp->u.p.pending_spaces = pos - dtp->u.p.max_pos + 1;
1256 read_x (dtp, f->u.n);
1257 break;
1259 case FMT_TL:
1260 case FMT_T:
1261 consume_data_flag = 0;
1263 if (f->format == FMT_TL)
1265 /* Handle the special case when no bytes have been used yet.
1266 Cannot go below zero. */
1267 if (bytes_used == 0)
1269 dtp->u.p.pending_spaces -= f->u.n;
1270 dtp->u.p.skips -= f->u.n;
1271 dtp->u.p.skips = dtp->u.p.skips < 0 ? 0 : dtp->u.p.skips;
1274 pos = bytes_used - f->u.n;
1276 else /* FMT_T */
1277 pos = f->u.n - 1;
1279 /* Standard 10.6.1.1: excessive left tabbing is reset to the
1280 left tab limit. We do not check if the position has gone
1281 beyond the end of record because a subsequent tab could
1282 bring us back again. */
1283 pos = pos < 0 ? 0 : pos;
1285 dtp->u.p.skips = dtp->u.p.skips + pos - bytes_used;
1286 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces
1287 + pos - dtp->u.p.max_pos;
1288 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces < 0
1289 ? 0 : dtp->u.p.pending_spaces;
1290 if (dtp->u.p.skips == 0)
1291 break;
1293 /* Adjust everything for end-of-record condition */
1294 if (dtp->u.p.sf_seen_eor && !is_internal_unit (dtp))
1296 dtp->u.p.current_unit->bytes_left -= dtp->u.p.sf_seen_eor;
1297 dtp->u.p.skips -= dtp->u.p.sf_seen_eor;
1298 bytes_used = pos;
1299 dtp->u.p.sf_seen_eor = 0;
1301 if (dtp->u.p.skips < 0)
1303 if (is_internal_unit (dtp))
1304 sseek (dtp->u.p.current_unit->s, dtp->u.p.skips, SEEK_CUR);
1305 else
1306 fbuf_seek (dtp->u.p.current_unit, dtp->u.p.skips, SEEK_CUR);
1307 dtp->u.p.current_unit->bytes_left -= (gfc_offset) dtp->u.p.skips;
1308 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1310 else
1311 read_x (dtp, dtp->u.p.skips);
1312 break;
1314 case FMT_S:
1315 consume_data_flag = 0;
1316 dtp->u.p.sign_status = SIGN_S;
1317 break;
1319 case FMT_SS:
1320 consume_data_flag = 0;
1321 dtp->u.p.sign_status = SIGN_SS;
1322 break;
1324 case FMT_SP:
1325 consume_data_flag = 0;
1326 dtp->u.p.sign_status = SIGN_SP;
1327 break;
1329 case FMT_BN:
1330 consume_data_flag = 0 ;
1331 dtp->u.p.blank_status = BLANK_NULL;
1332 break;
1334 case FMT_BZ:
1335 consume_data_flag = 0;
1336 dtp->u.p.blank_status = BLANK_ZERO;
1337 break;
1339 case FMT_DC:
1340 consume_data_flag = 0;
1341 dtp->u.p.current_unit->decimal_status = DECIMAL_COMMA;
1342 break;
1344 case FMT_DP:
1345 consume_data_flag = 0;
1346 dtp->u.p.current_unit->decimal_status = DECIMAL_POINT;
1347 break;
1349 case FMT_RC:
1350 consume_data_flag = 0;
1351 dtp->u.p.current_unit->round_status = ROUND_COMPATIBLE;
1352 break;
1354 case FMT_RD:
1355 consume_data_flag = 0;
1356 dtp->u.p.current_unit->round_status = ROUND_DOWN;
1357 break;
1359 case FMT_RN:
1360 consume_data_flag = 0;
1361 dtp->u.p.current_unit->round_status = ROUND_NEAREST;
1362 break;
1364 case FMT_RP:
1365 consume_data_flag = 0;
1366 dtp->u.p.current_unit->round_status = ROUND_PROCDEFINED;
1367 break;
1369 case FMT_RU:
1370 consume_data_flag = 0;
1371 dtp->u.p.current_unit->round_status = ROUND_UP;
1372 break;
1374 case FMT_RZ:
1375 consume_data_flag = 0;
1376 dtp->u.p.current_unit->round_status = ROUND_ZERO;
1377 break;
1379 case FMT_P:
1380 consume_data_flag = 0;
1381 dtp->u.p.scale_factor = f->u.k;
1382 break;
1384 case FMT_DOLLAR:
1385 consume_data_flag = 0;
1386 dtp->u.p.seen_dollar = 1;
1387 break;
1389 case FMT_SLASH:
1390 consume_data_flag = 0;
1391 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1392 next_record (dtp, 0);
1393 break;
1395 case FMT_COLON:
1396 /* A colon descriptor causes us to exit this loop (in
1397 particular preventing another / descriptor from being
1398 processed) unless there is another data item to be
1399 transferred. */
1400 consume_data_flag = 0;
1401 if (n == 0)
1402 return;
1403 break;
1405 default:
1406 internal_error (&dtp->common, "Bad format node");
1409 /* Adjust the item count and data pointer. */
1411 if ((consume_data_flag > 0) && (n > 0))
1413 n--;
1414 p = ((char *) p) + size;
1417 dtp->u.p.skips = 0;
1419 pos = (int)(dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left);
1420 dtp->u.p.max_pos = (dtp->u.p.max_pos > pos) ? dtp->u.p.max_pos : pos;
1423 return;
1425 /* Come here when we need a data descriptor but don't have one. We
1426 push the current format node back onto the input, then return and
1427 let the user program call us back with the data. */
1428 need_read_data:
1429 unget_format (dtp, f);
1433 static void
1434 formatted_transfer_scalar_write (st_parameter_dt *dtp, bt type, void *p, int kind,
1435 size_t size)
1437 int pos, bytes_used;
1438 const fnode *f;
1439 format_token t;
1440 int n;
1441 int consume_data_flag;
1443 /* Change a complex data item into a pair of reals. */
1445 n = (p == NULL) ? 0 : ((type != BT_COMPLEX) ? 1 : 2);
1446 if (type == BT_COMPLEX)
1448 type = BT_REAL;
1449 size /= 2;
1452 /* If there's an EOR condition, we simulate finalizing the transfer
1453 by doing nothing. */
1454 if (dtp->u.p.eor_condition)
1455 return;
1457 /* Set this flag so that commas in reads cause the read to complete before
1458 the entire field has been read. The next read field will start right after
1459 the comma in the stream. (Set to 0 for character reads). */
1460 dtp->u.p.sf_read_comma =
1461 dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA ? 0 : 1;
1463 for (;;)
1465 /* If reversion has occurred and there is another real data item,
1466 then we have to move to the next record. */
1467 if (dtp->u.p.reversion_flag && n > 0)
1469 dtp->u.p.reversion_flag = 0;
1470 next_record (dtp, 0);
1473 consume_data_flag = 1;
1474 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1475 break;
1477 f = next_format (dtp);
1478 if (f == NULL)
1480 /* No data descriptors left. */
1481 if (unlikely (n > 0))
1482 generate_error (&dtp->common, LIBERROR_FORMAT,
1483 "Insufficient data descriptors in format after reversion");
1484 return;
1487 /* Now discharge T, TR and X movements to the right. This is delayed
1488 until a data producing format to suppress trailing spaces. */
1490 t = f->format;
1491 if (dtp->u.p.mode == WRITING && dtp->u.p.skips != 0
1492 && ((n>0 && ( t == FMT_I || t == FMT_B || t == FMT_O
1493 || t == FMT_Z || t == FMT_F || t == FMT_E
1494 || t == FMT_EN || t == FMT_ES || t == FMT_G
1495 || t == FMT_L || t == FMT_A || t == FMT_D))
1496 || t == FMT_STRING))
1498 if (dtp->u.p.skips > 0)
1500 int tmp;
1501 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
1502 tmp = (int)(dtp->u.p.current_unit->recl
1503 - dtp->u.p.current_unit->bytes_left);
1504 dtp->u.p.max_pos =
1505 dtp->u.p.max_pos > tmp ? dtp->u.p.max_pos : tmp;
1507 if (dtp->u.p.skips < 0)
1509 if (is_internal_unit (dtp))
1510 sseek (dtp->u.p.current_unit->s, dtp->u.p.skips, SEEK_CUR);
1511 else
1512 fbuf_seek (dtp->u.p.current_unit, dtp->u.p.skips, SEEK_CUR);
1513 dtp->u.p.current_unit->bytes_left -= (gfc_offset) dtp->u.p.skips;
1515 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1518 bytes_used = (int)(dtp->u.p.current_unit->recl
1519 - dtp->u.p.current_unit->bytes_left);
1521 if (is_stream_io(dtp))
1522 bytes_used = 0;
1524 switch (t)
1526 case FMT_I:
1527 if (n == 0)
1528 goto need_data;
1529 if (require_type (dtp, BT_INTEGER, type, f))
1530 return;
1531 write_i (dtp, f, p, kind);
1532 break;
1534 case FMT_B:
1535 if (n == 0)
1536 goto need_data;
1537 if (!(compile_options.allow_std & GFC_STD_GNU)
1538 && require_type (dtp, BT_INTEGER, type, f))
1539 return;
1540 write_b (dtp, f, p, kind);
1541 break;
1543 case FMT_O:
1544 if (n == 0)
1545 goto need_data;
1546 if (!(compile_options.allow_std & GFC_STD_GNU)
1547 && require_type (dtp, BT_INTEGER, type, f))
1548 return;
1549 write_o (dtp, f, p, kind);
1550 break;
1552 case FMT_Z:
1553 if (n == 0)
1554 goto need_data;
1555 if (!(compile_options.allow_std & GFC_STD_GNU)
1556 && require_type (dtp, BT_INTEGER, type, f))
1557 return;
1558 write_z (dtp, f, p, kind);
1559 break;
1561 case FMT_A:
1562 if (n == 0)
1563 goto need_data;
1565 /* It is possible to have FMT_A with something not BT_CHARACTER such
1566 as when writing out hollerith strings, so check both type
1567 and kind before calling wide character routines. */
1568 if (type == BT_CHARACTER && kind == 4)
1569 write_a_char4 (dtp, f, p, size);
1570 else
1571 write_a (dtp, f, p, size);
1572 break;
1574 case FMT_L:
1575 if (n == 0)
1576 goto need_data;
1577 write_l (dtp, f, p, kind);
1578 break;
1580 case FMT_D:
1581 if (n == 0)
1582 goto need_data;
1583 if (require_type (dtp, BT_REAL, type, f))
1584 return;
1585 write_d (dtp, f, p, kind);
1586 break;
1588 case FMT_E:
1589 if (n == 0)
1590 goto need_data;
1591 if (require_type (dtp, BT_REAL, type, f))
1592 return;
1593 write_e (dtp, f, p, kind);
1594 break;
1596 case FMT_EN:
1597 if (n == 0)
1598 goto need_data;
1599 if (require_type (dtp, BT_REAL, type, f))
1600 return;
1601 write_en (dtp, f, p, kind);
1602 break;
1604 case FMT_ES:
1605 if (n == 0)
1606 goto need_data;
1607 if (require_type (dtp, BT_REAL, type, f))
1608 return;
1609 write_es (dtp, f, p, kind);
1610 break;
1612 case FMT_F:
1613 if (n == 0)
1614 goto need_data;
1615 if (require_type (dtp, BT_REAL, type, f))
1616 return;
1617 write_f (dtp, f, p, kind);
1618 break;
1620 case FMT_G:
1621 if (n == 0)
1622 goto need_data;
1623 switch (type)
1625 case BT_INTEGER:
1626 write_i (dtp, f, p, kind);
1627 break;
1628 case BT_LOGICAL:
1629 write_l (dtp, f, p, kind);
1630 break;
1631 case BT_CHARACTER:
1632 if (kind == 4)
1633 write_a_char4 (dtp, f, p, size);
1634 else
1635 write_a (dtp, f, p, size);
1636 break;
1637 case BT_REAL:
1638 if (f->u.real.w == 0)
1639 write_real_g0 (dtp, p, kind, f->u.real.d);
1640 else
1641 write_d (dtp, f, p, kind);
1642 break;
1643 default:
1644 internal_error (&dtp->common,
1645 "formatted_transfer(): Bad type");
1647 break;
1649 case FMT_STRING:
1650 consume_data_flag = 0;
1651 write_constant_string (dtp, f);
1652 break;
1654 /* Format codes that don't transfer data. */
1655 case FMT_X:
1656 case FMT_TR:
1657 consume_data_flag = 0;
1659 dtp->u.p.skips += f->u.n;
1660 pos = bytes_used + dtp->u.p.skips - 1;
1661 dtp->u.p.pending_spaces = pos - dtp->u.p.max_pos + 1;
1662 /* Writes occur just before the switch on f->format, above, so
1663 that trailing blanks are suppressed, unless we are doing a
1664 non-advancing write in which case we want to output the blanks
1665 now. */
1666 if (dtp->u.p.advance_status == ADVANCE_NO)
1668 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
1669 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1671 break;
1673 case FMT_TL:
1674 case FMT_T:
1675 consume_data_flag = 0;
1677 if (f->format == FMT_TL)
1680 /* Handle the special case when no bytes have been used yet.
1681 Cannot go below zero. */
1682 if (bytes_used == 0)
1684 dtp->u.p.pending_spaces -= f->u.n;
1685 dtp->u.p.skips -= f->u.n;
1686 dtp->u.p.skips = dtp->u.p.skips < 0 ? 0 : dtp->u.p.skips;
1689 pos = bytes_used - f->u.n;
1691 else /* FMT_T */
1692 pos = f->u.n - dtp->u.p.pending_spaces - 1;
1694 /* Standard 10.6.1.1: excessive left tabbing is reset to the
1695 left tab limit. We do not check if the position has gone
1696 beyond the end of record because a subsequent tab could
1697 bring us back again. */
1698 pos = pos < 0 ? 0 : pos;
1700 dtp->u.p.skips = dtp->u.p.skips + pos - bytes_used;
1701 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces
1702 + pos - dtp->u.p.max_pos;
1703 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces < 0
1704 ? 0 : dtp->u.p.pending_spaces;
1705 break;
1707 case FMT_S:
1708 consume_data_flag = 0;
1709 dtp->u.p.sign_status = SIGN_S;
1710 break;
1712 case FMT_SS:
1713 consume_data_flag = 0;
1714 dtp->u.p.sign_status = SIGN_SS;
1715 break;
1717 case FMT_SP:
1718 consume_data_flag = 0;
1719 dtp->u.p.sign_status = SIGN_SP;
1720 break;
1722 case FMT_BN:
1723 consume_data_flag = 0 ;
1724 dtp->u.p.blank_status = BLANK_NULL;
1725 break;
1727 case FMT_BZ:
1728 consume_data_flag = 0;
1729 dtp->u.p.blank_status = BLANK_ZERO;
1730 break;
1732 case FMT_DC:
1733 consume_data_flag = 0;
1734 dtp->u.p.current_unit->decimal_status = DECIMAL_COMMA;
1735 break;
1737 case FMT_DP:
1738 consume_data_flag = 0;
1739 dtp->u.p.current_unit->decimal_status = DECIMAL_POINT;
1740 break;
1742 case FMT_RC:
1743 consume_data_flag = 0;
1744 dtp->u.p.current_unit->round_status = ROUND_COMPATIBLE;
1745 break;
1747 case FMT_RD:
1748 consume_data_flag = 0;
1749 dtp->u.p.current_unit->round_status = ROUND_DOWN;
1750 break;
1752 case FMT_RN:
1753 consume_data_flag = 0;
1754 dtp->u.p.current_unit->round_status = ROUND_NEAREST;
1755 break;
1757 case FMT_RP:
1758 consume_data_flag = 0;
1759 dtp->u.p.current_unit->round_status = ROUND_PROCDEFINED;
1760 break;
1762 case FMT_RU:
1763 consume_data_flag = 0;
1764 dtp->u.p.current_unit->round_status = ROUND_UP;
1765 break;
1767 case FMT_RZ:
1768 consume_data_flag = 0;
1769 dtp->u.p.current_unit->round_status = ROUND_ZERO;
1770 break;
1772 case FMT_P:
1773 consume_data_flag = 0;
1774 dtp->u.p.scale_factor = f->u.k;
1775 break;
1777 case FMT_DOLLAR:
1778 consume_data_flag = 0;
1779 dtp->u.p.seen_dollar = 1;
1780 break;
1782 case FMT_SLASH:
1783 consume_data_flag = 0;
1784 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1785 next_record (dtp, 0);
1786 break;
1788 case FMT_COLON:
1789 /* A colon descriptor causes us to exit this loop (in
1790 particular preventing another / descriptor from being
1791 processed) unless there is another data item to be
1792 transferred. */
1793 consume_data_flag = 0;
1794 if (n == 0)
1795 return;
1796 break;
1798 default:
1799 internal_error (&dtp->common, "Bad format node");
1802 /* Adjust the item count and data pointer. */
1804 if ((consume_data_flag > 0) && (n > 0))
1806 n--;
1807 p = ((char *) p) + size;
1810 pos = (int)(dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left);
1811 dtp->u.p.max_pos = (dtp->u.p.max_pos > pos) ? dtp->u.p.max_pos : pos;
1814 return;
1816 /* Come here when we need a data descriptor but don't have one. We
1817 push the current format node back onto the input, then return and
1818 let the user program call us back with the data. */
1819 need_data:
1820 unget_format (dtp, f);
1823 /* This function is first called from data_init_transfer to initiate the loop
1824 over each item in the format, transferring data as required. Subsequent
1825 calls to this function occur for each data item foound in the READ/WRITE
1826 statement. The item_count is incremented for each call. Since the first
1827 call is from data_transfer_init, the item_count is always one greater than
1828 the actual count number of the item being transferred. */
1830 static void
1831 formatted_transfer (st_parameter_dt *dtp, bt type, void *p, int kind,
1832 size_t size, size_t nelems)
1834 size_t elem;
1835 char *tmp;
1837 tmp = (char *) p;
1838 size_t stride = type == BT_CHARACTER ?
1839 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
1840 if (dtp->u.p.mode == READING)
1842 /* Big loop over all the elements. */
1843 for (elem = 0; elem < nelems; elem++)
1845 dtp->u.p.item_count++;
1846 formatted_transfer_scalar_read (dtp, type, tmp + stride*elem, kind, size);
1849 else
1851 /* Big loop over all the elements. */
1852 for (elem = 0; elem < nelems; elem++)
1854 dtp->u.p.item_count++;
1855 formatted_transfer_scalar_write (dtp, type, tmp + stride*elem, kind, size);
1861 /* Data transfer entry points. The type of the data entity is
1862 implicit in the subroutine call. This prevents us from having to
1863 share a common enum with the compiler. */
1865 void
1866 transfer_integer (st_parameter_dt *dtp, void *p, int kind)
1868 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1869 return;
1870 dtp->u.p.transfer (dtp, BT_INTEGER, p, kind, kind, 1);
1873 void
1874 transfer_integer_write (st_parameter_dt *dtp, void *p, int kind)
1876 transfer_integer (dtp, p, kind);
1879 void
1880 transfer_real (st_parameter_dt *dtp, void *p, int kind)
1882 size_t size;
1883 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1884 return;
1885 size = size_from_real_kind (kind);
1886 dtp->u.p.transfer (dtp, BT_REAL, p, kind, size, 1);
1889 void
1890 transfer_real_write (st_parameter_dt *dtp, void *p, int kind)
1892 transfer_real (dtp, p, kind);
1895 void
1896 transfer_logical (st_parameter_dt *dtp, void *p, int kind)
1898 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1899 return;
1900 dtp->u.p.transfer (dtp, BT_LOGICAL, p, kind, kind, 1);
1903 void
1904 transfer_logical_write (st_parameter_dt *dtp, void *p, int kind)
1906 transfer_logical (dtp, p, kind);
1909 void
1910 transfer_character (st_parameter_dt *dtp, void *p, int len)
1912 static char *empty_string[0];
1914 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1915 return;
1917 /* Strings of zero length can have p == NULL, which confuses the
1918 transfer routines into thinking we need more data elements. To avoid
1919 this, we give them a nice pointer. */
1920 if (len == 0 && p == NULL)
1921 p = empty_string;
1923 /* Set kind here to 1. */
1924 dtp->u.p.transfer (dtp, BT_CHARACTER, p, 1, len, 1);
1927 void
1928 transfer_character_write (st_parameter_dt *dtp, void *p, int len)
1930 transfer_character (dtp, p, len);
1933 void
1934 transfer_character_wide (st_parameter_dt *dtp, void *p, int len, int kind)
1936 static char *empty_string[0];
1938 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1939 return;
1941 /* Strings of zero length can have p == NULL, which confuses the
1942 transfer routines into thinking we need more data elements. To avoid
1943 this, we give them a nice pointer. */
1944 if (len == 0 && p == NULL)
1945 p = empty_string;
1947 /* Here we pass the actual kind value. */
1948 dtp->u.p.transfer (dtp, BT_CHARACTER, p, kind, len, 1);
1951 void
1952 transfer_character_wide_write (st_parameter_dt *dtp, void *p, int len, int kind)
1954 transfer_character_wide (dtp, p, len, kind);
1957 void
1958 transfer_complex (st_parameter_dt *dtp, void *p, int kind)
1960 size_t size;
1961 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1962 return;
1963 size = size_from_complex_kind (kind);
1964 dtp->u.p.transfer (dtp, BT_COMPLEX, p, kind, size, 1);
1967 void
1968 transfer_complex_write (st_parameter_dt *dtp, void *p, int kind)
1970 transfer_complex (dtp, p, kind);
1973 void
1974 transfer_array (st_parameter_dt *dtp, gfc_array_char *desc, int kind,
1975 gfc_charlen_type charlen)
1977 index_type count[GFC_MAX_DIMENSIONS];
1978 index_type extent[GFC_MAX_DIMENSIONS];
1979 index_type stride[GFC_MAX_DIMENSIONS];
1980 index_type stride0, rank, size, n;
1981 size_t tsize;
1982 char *data;
1983 bt iotype;
1985 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1986 return;
1988 iotype = (bt) GFC_DESCRIPTOR_TYPE (desc);
1989 size = iotype == BT_CHARACTER ? charlen : GFC_DESCRIPTOR_SIZE (desc);
1991 rank = GFC_DESCRIPTOR_RANK (desc);
1992 for (n = 0; n < rank; n++)
1994 count[n] = 0;
1995 stride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(desc,n);
1996 extent[n] = GFC_DESCRIPTOR_EXTENT(desc,n);
1998 /* If the extent of even one dimension is zero, then the entire
1999 array section contains zero elements, so we return after writing
2000 a zero array record. */
2001 if (extent[n] <= 0)
2003 data = NULL;
2004 tsize = 0;
2005 dtp->u.p.transfer (dtp, iotype, data, kind, size, tsize);
2006 return;
2010 stride0 = stride[0];
2012 /* If the innermost dimension has a stride of 1, we can do the transfer
2013 in contiguous chunks. */
2014 if (stride0 == size)
2015 tsize = extent[0];
2016 else
2017 tsize = 1;
2019 data = GFC_DESCRIPTOR_DATA (desc);
2021 while (data)
2023 dtp->u.p.transfer (dtp, iotype, data, kind, size, tsize);
2024 data += stride0 * tsize;
2025 count[0] += tsize;
2026 n = 0;
2027 while (count[n] == extent[n])
2029 count[n] = 0;
2030 data -= stride[n] * extent[n];
2031 n++;
2032 if (n == rank)
2034 data = NULL;
2035 break;
2037 else
2039 count[n]++;
2040 data += stride[n];
2046 void
2047 transfer_array_write (st_parameter_dt *dtp, gfc_array_char *desc, int kind,
2048 gfc_charlen_type charlen)
2050 transfer_array (dtp, desc, kind, charlen);
2053 /* Preposition a sequential unformatted file while reading. */
2055 static void
2056 us_read (st_parameter_dt *dtp, int continued)
2058 ssize_t n, nr;
2059 GFC_INTEGER_4 i4;
2060 GFC_INTEGER_8 i8;
2061 gfc_offset i;
2063 if (compile_options.record_marker == 0)
2064 n = sizeof (GFC_INTEGER_4);
2065 else
2066 n = compile_options.record_marker;
2068 nr = sread (dtp->u.p.current_unit->s, &i, n);
2069 if (unlikely (nr < 0))
2071 generate_error (&dtp->common, LIBERROR_BAD_US, NULL);
2072 return;
2074 else if (nr == 0)
2076 hit_eof (dtp);
2077 return; /* end of file */
2079 else if (unlikely (n != nr))
2081 generate_error (&dtp->common, LIBERROR_BAD_US, NULL);
2082 return;
2085 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
2086 if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE))
2088 switch (nr)
2090 case sizeof(GFC_INTEGER_4):
2091 memcpy (&i4, &i, sizeof (i4));
2092 i = i4;
2093 break;
2095 case sizeof(GFC_INTEGER_8):
2096 memcpy (&i8, &i, sizeof (i8));
2097 i = i8;
2098 break;
2100 default:
2101 runtime_error ("Illegal value for record marker");
2102 break;
2105 else
2106 switch (nr)
2108 case sizeof(GFC_INTEGER_4):
2109 reverse_memcpy (&i4, &i, sizeof (i4));
2110 i = i4;
2111 break;
2113 case sizeof(GFC_INTEGER_8):
2114 reverse_memcpy (&i8, &i, sizeof (i8));
2115 i = i8;
2116 break;
2118 default:
2119 runtime_error ("Illegal value for record marker");
2120 break;
2123 if (i >= 0)
2125 dtp->u.p.current_unit->bytes_left_subrecord = i;
2126 dtp->u.p.current_unit->continued = 0;
2128 else
2130 dtp->u.p.current_unit->bytes_left_subrecord = -i;
2131 dtp->u.p.current_unit->continued = 1;
2134 if (! continued)
2135 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2139 /* Preposition a sequential unformatted file while writing. This
2140 amount to writing a bogus length that will be filled in later. */
2142 static void
2143 us_write (st_parameter_dt *dtp, int continued)
2145 ssize_t nbytes;
2146 gfc_offset dummy;
2148 dummy = 0;
2150 if (compile_options.record_marker == 0)
2151 nbytes = sizeof (GFC_INTEGER_4);
2152 else
2153 nbytes = compile_options.record_marker ;
2155 if (swrite (dtp->u.p.current_unit->s, &dummy, nbytes) != nbytes)
2156 generate_error (&dtp->common, LIBERROR_OS, NULL);
2158 /* For sequential unformatted, if RECL= was not specified in the OPEN
2159 we write until we have more bytes than can fit in the subrecord
2160 markers, then we write a new subrecord. */
2162 dtp->u.p.current_unit->bytes_left_subrecord =
2163 dtp->u.p.current_unit->recl_subrecord;
2164 dtp->u.p.current_unit->continued = continued;
2168 /* Position to the next record prior to transfer. We are assumed to
2169 be before the next record. We also calculate the bytes in the next
2170 record. */
2172 static void
2173 pre_position (st_parameter_dt *dtp)
2175 if (dtp->u.p.current_unit->current_record)
2176 return; /* Already positioned. */
2178 switch (current_mode (dtp))
2180 case FORMATTED_STREAM:
2181 case UNFORMATTED_STREAM:
2182 /* There are no records with stream I/O. If the position was specified
2183 data_transfer_init has already positioned the file. If no position
2184 was specified, we continue from where we last left off. I.e.
2185 there is nothing to do here. */
2186 break;
2188 case UNFORMATTED_SEQUENTIAL:
2189 if (dtp->u.p.mode == READING)
2190 us_read (dtp, 0);
2191 else
2192 us_write (dtp, 0);
2194 break;
2196 case FORMATTED_SEQUENTIAL:
2197 case FORMATTED_DIRECT:
2198 case UNFORMATTED_DIRECT:
2199 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2200 break;
2203 dtp->u.p.current_unit->current_record = 1;
2207 /* Initialize things for a data transfer. This code is common for
2208 both reading and writing. */
2210 static void
2211 data_transfer_init (st_parameter_dt *dtp, int read_flag)
2213 unit_flags u_flags; /* Used for creating a unit if needed. */
2214 GFC_INTEGER_4 cf = dtp->common.flags;
2215 namelist_info *ionml;
2217 ionml = ((cf & IOPARM_DT_IONML_SET) != 0) ? dtp->u.p.ionml : NULL;
2219 memset (&dtp->u.p, 0, sizeof (dtp->u.p));
2221 dtp->u.p.ionml = ionml;
2222 dtp->u.p.mode = read_flag ? READING : WRITING;
2224 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2225 return;
2227 if ((cf & IOPARM_DT_HAS_SIZE) != 0)
2228 dtp->u.p.size_used = 0; /* Initialize the count. */
2230 dtp->u.p.current_unit = get_unit (dtp, 1);
2231 if (dtp->u.p.current_unit->s == NULL)
2232 { /* Open the unit with some default flags. */
2233 st_parameter_open opp;
2234 unit_convert conv;
2236 if (dtp->common.unit < 0)
2238 close_unit (dtp->u.p.current_unit);
2239 dtp->u.p.current_unit = NULL;
2240 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2241 "Bad unit number in statement");
2242 return;
2244 memset (&u_flags, '\0', sizeof (u_flags));
2245 u_flags.access = ACCESS_SEQUENTIAL;
2246 u_flags.action = ACTION_READWRITE;
2248 /* Is it unformatted? */
2249 if (!(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT
2250 | IOPARM_DT_IONML_SET)))
2251 u_flags.form = FORM_UNFORMATTED;
2252 else
2253 u_flags.form = FORM_UNSPECIFIED;
2255 u_flags.delim = DELIM_UNSPECIFIED;
2256 u_flags.blank = BLANK_UNSPECIFIED;
2257 u_flags.pad = PAD_UNSPECIFIED;
2258 u_flags.decimal = DECIMAL_UNSPECIFIED;
2259 u_flags.encoding = ENCODING_UNSPECIFIED;
2260 u_flags.async = ASYNC_UNSPECIFIED;
2261 u_flags.round = ROUND_UNSPECIFIED;
2262 u_flags.sign = SIGN_UNSPECIFIED;
2264 u_flags.status = STATUS_UNKNOWN;
2266 conv = get_unformatted_convert (dtp->common.unit);
2268 if (conv == GFC_CONVERT_NONE)
2269 conv = compile_options.convert;
2271 /* We use big_endian, which is 0 on little-endian machines
2272 and 1 on big-endian machines. */
2273 switch (conv)
2275 case GFC_CONVERT_NATIVE:
2276 case GFC_CONVERT_SWAP:
2277 break;
2279 case GFC_CONVERT_BIG:
2280 conv = big_endian ? GFC_CONVERT_NATIVE : GFC_CONVERT_SWAP;
2281 break;
2283 case GFC_CONVERT_LITTLE:
2284 conv = big_endian ? GFC_CONVERT_SWAP : GFC_CONVERT_NATIVE;
2285 break;
2287 default:
2288 internal_error (&opp.common, "Illegal value for CONVERT");
2289 break;
2292 u_flags.convert = conv;
2294 opp.common = dtp->common;
2295 opp.common.flags &= IOPARM_COMMON_MASK;
2296 dtp->u.p.current_unit = new_unit (&opp, dtp->u.p.current_unit, &u_flags);
2297 dtp->common.flags &= ~IOPARM_COMMON_MASK;
2298 dtp->common.flags |= (opp.common.flags & IOPARM_COMMON_MASK);
2299 if (dtp->u.p.current_unit == NULL)
2300 return;
2303 /* Check the action. */
2305 if (read_flag && dtp->u.p.current_unit->flags.action == ACTION_WRITE)
2307 generate_error (&dtp->common, LIBERROR_BAD_ACTION,
2308 "Cannot read from file opened for WRITE");
2309 return;
2312 if (!read_flag && dtp->u.p.current_unit->flags.action == ACTION_READ)
2314 generate_error (&dtp->common, LIBERROR_BAD_ACTION,
2315 "Cannot write to file opened for READ");
2316 return;
2319 dtp->u.p.first_item = 1;
2321 /* Check the format. */
2323 if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
2324 parse_format (dtp);
2326 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED
2327 && (cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
2328 != 0)
2330 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2331 "Format present for UNFORMATTED data transfer");
2332 return;
2335 if ((cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0 && dtp->u.p.ionml != NULL)
2337 if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
2338 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2339 "A format cannot be specified with a namelist");
2341 else if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
2342 !(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT)))
2344 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2345 "Missing format for FORMATTED data transfer");
2348 if (is_internal_unit (dtp)
2349 && dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
2351 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2352 "Internal file cannot be accessed by UNFORMATTED "
2353 "data transfer");
2354 return;
2357 /* Check the record or position number. */
2359 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT
2360 && (cf & IOPARM_DT_HAS_REC) == 0)
2362 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
2363 "Direct access data transfer requires record number");
2364 return;
2367 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
2369 if ((cf & IOPARM_DT_HAS_REC) != 0)
2371 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2372 "Record number not allowed for sequential access "
2373 "data transfer");
2374 return;
2377 if (dtp->u.p.current_unit->endfile == AFTER_ENDFILE)
2379 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2380 "Sequential READ or WRITE not allowed after "
2381 "EOF marker, possibly use REWIND or BACKSPACE");
2382 return;
2386 /* Process the ADVANCE option. */
2388 dtp->u.p.advance_status
2389 = !(cf & IOPARM_DT_HAS_ADVANCE) ? ADVANCE_UNSPECIFIED :
2390 find_option (&dtp->common, dtp->advance, dtp->advance_len, advance_opt,
2391 "Bad ADVANCE parameter in data transfer statement");
2393 if (dtp->u.p.advance_status != ADVANCE_UNSPECIFIED)
2395 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
2397 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2398 "ADVANCE specification conflicts with sequential "
2399 "access");
2400 return;
2403 if (is_internal_unit (dtp))
2405 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2406 "ADVANCE specification conflicts with internal file");
2407 return;
2410 if ((cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
2411 != IOPARM_DT_HAS_FORMAT)
2413 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2414 "ADVANCE specification requires an explicit format");
2415 return;
2419 if (read_flag)
2421 dtp->u.p.current_unit->previous_nonadvancing_write = 0;
2423 if ((cf & IOPARM_EOR) != 0 && dtp->u.p.advance_status != ADVANCE_NO)
2425 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
2426 "EOR specification requires an ADVANCE specification "
2427 "of NO");
2428 return;
2431 if ((cf & IOPARM_DT_HAS_SIZE) != 0
2432 && dtp->u.p.advance_status != ADVANCE_NO)
2434 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
2435 "SIZE specification requires an ADVANCE "
2436 "specification of NO");
2437 return;
2440 else
2441 { /* Write constraints. */
2442 if ((cf & IOPARM_END) != 0)
2444 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2445 "END specification cannot appear in a write "
2446 "statement");
2447 return;
2450 if ((cf & IOPARM_EOR) != 0)
2452 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2453 "EOR specification cannot appear in a write "
2454 "statement");
2455 return;
2458 if ((cf & IOPARM_DT_HAS_SIZE) != 0)
2460 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2461 "SIZE specification cannot appear in a write "
2462 "statement");
2463 return;
2467 if (dtp->u.p.advance_status == ADVANCE_UNSPECIFIED)
2468 dtp->u.p.advance_status = ADVANCE_YES;
2470 /* Check the decimal mode. */
2471 dtp->u.p.current_unit->decimal_status
2472 = !(cf & IOPARM_DT_HAS_DECIMAL) ? DECIMAL_UNSPECIFIED :
2473 find_option (&dtp->common, dtp->decimal, dtp->decimal_len,
2474 decimal_opt, "Bad DECIMAL parameter in data transfer "
2475 "statement");
2477 if (dtp->u.p.current_unit->decimal_status == DECIMAL_UNSPECIFIED)
2478 dtp->u.p.current_unit->decimal_status = dtp->u.p.current_unit->flags.decimal;
2480 /* Check the round mode. */
2481 dtp->u.p.current_unit->round_status
2482 = !(cf & IOPARM_DT_HAS_ROUND) ? ROUND_UNSPECIFIED :
2483 find_option (&dtp->common, dtp->round, dtp->round_len,
2484 round_opt, "Bad ROUND parameter in data transfer "
2485 "statement");
2487 if (dtp->u.p.current_unit->round_status == ROUND_UNSPECIFIED)
2488 dtp->u.p.current_unit->round_status = dtp->u.p.current_unit->flags.round;
2490 /* Check the sign mode. */
2491 dtp->u.p.sign_status
2492 = !(cf & IOPARM_DT_HAS_SIGN) ? SIGN_UNSPECIFIED :
2493 find_option (&dtp->common, dtp->sign, dtp->sign_len, sign_opt,
2494 "Bad SIGN parameter in data transfer statement");
2496 if (dtp->u.p.sign_status == SIGN_UNSPECIFIED)
2497 dtp->u.p.sign_status = dtp->u.p.current_unit->flags.sign;
2499 /* Check the blank mode. */
2500 dtp->u.p.blank_status
2501 = !(cf & IOPARM_DT_HAS_BLANK) ? BLANK_UNSPECIFIED :
2502 find_option (&dtp->common, dtp->blank, dtp->blank_len,
2503 blank_opt,
2504 "Bad BLANK parameter in data transfer statement");
2506 if (dtp->u.p.blank_status == BLANK_UNSPECIFIED)
2507 dtp->u.p.blank_status = dtp->u.p.current_unit->flags.blank;
2509 /* Check the delim mode. */
2510 dtp->u.p.current_unit->delim_status
2511 = !(cf & IOPARM_DT_HAS_DELIM) ? DELIM_UNSPECIFIED :
2512 find_option (&dtp->common, dtp->delim, dtp->delim_len,
2513 delim_opt, "Bad DELIM parameter in data transfer statement");
2515 if (dtp->u.p.current_unit->delim_status == DELIM_UNSPECIFIED)
2516 dtp->u.p.current_unit->delim_status = dtp->u.p.current_unit->flags.delim;
2518 /* Check the pad mode. */
2519 dtp->u.p.current_unit->pad_status
2520 = !(cf & IOPARM_DT_HAS_PAD) ? PAD_UNSPECIFIED :
2521 find_option (&dtp->common, dtp->pad, dtp->pad_len, pad_opt,
2522 "Bad PAD parameter in data transfer statement");
2524 if (dtp->u.p.current_unit->pad_status == PAD_UNSPECIFIED)
2525 dtp->u.p.current_unit->pad_status = dtp->u.p.current_unit->flags.pad;
2527 /* Check to see if we might be reading what we wrote before */
2529 if (dtp->u.p.mode != dtp->u.p.current_unit->mode
2530 && !is_internal_unit (dtp))
2532 int pos = fbuf_reset (dtp->u.p.current_unit);
2533 if (pos != 0)
2534 sseek (dtp->u.p.current_unit->s, pos, SEEK_CUR);
2535 sflush(dtp->u.p.current_unit->s);
2538 /* Check the POS= specifier: that it is in range and that it is used with a
2539 unit that has been connected for STREAM access. F2003 9.5.1.10. */
2541 if (((cf & IOPARM_DT_HAS_POS) != 0))
2543 if (is_stream_io (dtp))
2546 if (dtp->pos <= 0)
2548 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2549 "POS=specifier must be positive");
2550 return;
2553 if (dtp->pos >= dtp->u.p.current_unit->maxrec)
2555 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2556 "POS=specifier too large");
2557 return;
2560 dtp->rec = dtp->pos;
2562 if (dtp->u.p.mode == READING)
2564 /* Reset the endfile flag; if we hit EOF during reading
2565 we'll set the flag and generate an error at that point
2566 rather than worrying about it here. */
2567 dtp->u.p.current_unit->endfile = NO_ENDFILE;
2570 if (dtp->pos != dtp->u.p.current_unit->strm_pos)
2572 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
2573 if (sseek (dtp->u.p.current_unit->s, dtp->pos - 1, SEEK_SET) < 0)
2575 generate_error (&dtp->common, LIBERROR_OS, NULL);
2576 return;
2578 dtp->u.p.current_unit->strm_pos = dtp->pos;
2581 else
2583 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2584 "POS=specifier not allowed, "
2585 "Try OPEN with ACCESS='stream'");
2586 return;
2591 /* Sanity checks on the record number. */
2592 if ((cf & IOPARM_DT_HAS_REC) != 0)
2594 if (dtp->rec <= 0)
2596 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2597 "Record number must be positive");
2598 return;
2601 if (dtp->rec >= dtp->u.p.current_unit->maxrec)
2603 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2604 "Record number too large");
2605 return;
2608 /* Make sure format buffer is reset. */
2609 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED)
2610 fbuf_reset (dtp->u.p.current_unit);
2613 /* Check whether the record exists to be read. Only
2614 a partial record needs to exist. */
2616 if (dtp->u.p.mode == READING && (dtp->rec - 1)
2617 * dtp->u.p.current_unit->recl >= file_length (dtp->u.p.current_unit->s))
2619 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2620 "Non-existing record number");
2621 return;
2624 /* Position the file. */
2625 if (sseek (dtp->u.p.current_unit->s, (gfc_offset) (dtp->rec - 1)
2626 * dtp->u.p.current_unit->recl, SEEK_SET) < 0)
2628 generate_error (&dtp->common, LIBERROR_OS, NULL);
2629 return;
2632 /* TODO: This is required to maintain compatibility between
2633 4.3 and 4.4 runtime. Remove when ABI changes from 4.3 */
2635 if (is_stream_io (dtp))
2636 dtp->u.p.current_unit->strm_pos = dtp->rec;
2638 /* TODO: Un-comment this code when ABI changes from 4.3.
2639 if (dtp->u.p.current_unit->flags.access == ACCESS_STREAM)
2641 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2642 "Record number not allowed for stream access "
2643 "data transfer");
2644 return;
2645 } */
2648 /* Bugware for badly written mixed C-Fortran I/O. */
2649 flush_if_preconnected(dtp->u.p.current_unit->s);
2651 dtp->u.p.current_unit->mode = dtp->u.p.mode;
2653 /* Set the maximum position reached from the previous I/O operation. This
2654 could be greater than zero from a previous non-advancing write. */
2655 dtp->u.p.max_pos = dtp->u.p.current_unit->saved_pos;
2657 pre_position (dtp);
2660 /* Set up the subroutine that will handle the transfers. */
2662 if (read_flag)
2664 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
2665 dtp->u.p.transfer = unformatted_read;
2666 else
2668 if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
2669 dtp->u.p.transfer = list_formatted_read;
2670 else
2671 dtp->u.p.transfer = formatted_transfer;
2674 else
2676 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
2677 dtp->u.p.transfer = unformatted_write;
2678 else
2680 if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
2681 dtp->u.p.transfer = list_formatted_write;
2682 else
2683 dtp->u.p.transfer = formatted_transfer;
2687 /* Make sure that we don't do a read after a nonadvancing write. */
2689 if (read_flag)
2691 if (dtp->u.p.current_unit->read_bad && !is_stream_io (dtp))
2693 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2694 "Cannot READ after a nonadvancing WRITE");
2695 return;
2698 else
2700 if (dtp->u.p.advance_status == ADVANCE_YES && !dtp->u.p.seen_dollar)
2701 dtp->u.p.current_unit->read_bad = 1;
2704 /* Start the data transfer if we are doing a formatted transfer. */
2705 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
2706 && ((cf & (IOPARM_DT_LIST_FORMAT | IOPARM_DT_HAS_NAMELIST_NAME)) == 0)
2707 && dtp->u.p.ionml == NULL)
2708 formatted_transfer (dtp, 0, NULL, 0, 0, 1);
2711 /* Initialize an array_loop_spec given the array descriptor. The function
2712 returns the index of the last element of the array, and also returns
2713 starting record, where the first I/O goes to (necessary in case of
2714 negative strides). */
2716 gfc_offset
2717 init_loop_spec (gfc_array_char *desc, array_loop_spec *ls,
2718 gfc_offset *start_record)
2720 int rank = GFC_DESCRIPTOR_RANK(desc);
2721 int i;
2722 gfc_offset index;
2723 int empty;
2725 empty = 0;
2726 index = 1;
2727 *start_record = 0;
2729 for (i=0; i<rank; i++)
2731 ls[i].idx = GFC_DESCRIPTOR_LBOUND(desc,i);
2732 ls[i].start = GFC_DESCRIPTOR_LBOUND(desc,i);
2733 ls[i].end = GFC_DESCRIPTOR_UBOUND(desc,i);
2734 ls[i].step = GFC_DESCRIPTOR_STRIDE(desc,i);
2735 empty = empty || (GFC_DESCRIPTOR_UBOUND(desc,i)
2736 < GFC_DESCRIPTOR_LBOUND(desc,i));
2738 if (GFC_DESCRIPTOR_STRIDE(desc,i) > 0)
2740 index += (GFC_DESCRIPTOR_EXTENT(desc,i) - 1)
2741 * GFC_DESCRIPTOR_STRIDE(desc,i);
2743 else
2745 index -= (GFC_DESCRIPTOR_EXTENT(desc,i) - 1)
2746 * GFC_DESCRIPTOR_STRIDE(desc,i);
2747 *start_record -= (GFC_DESCRIPTOR_EXTENT(desc,i) - 1)
2748 * GFC_DESCRIPTOR_STRIDE(desc,i);
2752 if (empty)
2753 return 0;
2754 else
2755 return index;
2758 /* Determine the index to the next record in an internal unit array by
2759 by incrementing through the array_loop_spec. */
2761 gfc_offset
2762 next_array_record (st_parameter_dt *dtp, array_loop_spec *ls, int *finished)
2764 int i, carry;
2765 gfc_offset index;
2767 carry = 1;
2768 index = 0;
2770 for (i = 0; i < dtp->u.p.current_unit->rank; i++)
2772 if (carry)
2774 ls[i].idx++;
2775 if (ls[i].idx > ls[i].end)
2777 ls[i].idx = ls[i].start;
2778 carry = 1;
2780 else
2781 carry = 0;
2783 index = index + (ls[i].idx - ls[i].start) * ls[i].step;
2786 *finished = carry;
2788 return index;
2793 /* Skip to the end of the current record, taking care of an optional
2794 record marker of size bytes. If the file is not seekable, we
2795 read chunks of size MAX_READ until we get to the right
2796 position. */
2798 static void
2799 skip_record (st_parameter_dt *dtp, ssize_t bytes)
2801 ssize_t rlength, readb;
2802 static const ssize_t MAX_READ = 4096;
2803 char p[MAX_READ];
2805 dtp->u.p.current_unit->bytes_left_subrecord += bytes;
2806 if (dtp->u.p.current_unit->bytes_left_subrecord == 0)
2807 return;
2809 if (is_seekable (dtp->u.p.current_unit->s))
2811 /* Direct access files do not generate END conditions,
2812 only I/O errors. */
2813 if (sseek (dtp->u.p.current_unit->s,
2814 dtp->u.p.current_unit->bytes_left_subrecord, SEEK_CUR) < 0)
2815 generate_error (&dtp->common, LIBERROR_OS, NULL);
2817 dtp->u.p.current_unit->bytes_left_subrecord = 0;
2819 else
2820 { /* Seek by reading data. */
2821 while (dtp->u.p.current_unit->bytes_left_subrecord > 0)
2823 rlength =
2824 (MAX_READ < dtp->u.p.current_unit->bytes_left_subrecord) ?
2825 MAX_READ : dtp->u.p.current_unit->bytes_left_subrecord;
2827 readb = sread (dtp->u.p.current_unit->s, p, rlength);
2828 if (readb < 0)
2830 generate_error (&dtp->common, LIBERROR_OS, NULL);
2831 return;
2834 dtp->u.p.current_unit->bytes_left_subrecord -= readb;
2841 /* Advance to the next record reading unformatted files, taking
2842 care of subrecords. If complete_record is nonzero, we loop
2843 until all subrecords are cleared. */
2845 static void
2846 next_record_r_unf (st_parameter_dt *dtp, int complete_record)
2848 size_t bytes;
2850 bytes = compile_options.record_marker == 0 ?
2851 sizeof (GFC_INTEGER_4) : compile_options.record_marker;
2853 while(1)
2856 /* Skip over tail */
2858 skip_record (dtp, bytes);
2860 if ( ! (complete_record && dtp->u.p.current_unit->continued))
2861 return;
2863 us_read (dtp, 1);
2868 static inline gfc_offset
2869 min_off (gfc_offset a, gfc_offset b)
2871 return (a < b ? a : b);
2875 /* Space to the next record for read mode. */
2877 static void
2878 next_record_r (st_parameter_dt *dtp, int done)
2880 gfc_offset record;
2881 int bytes_left;
2882 char p;
2883 int cc;
2885 switch (current_mode (dtp))
2887 /* No records in unformatted STREAM I/O. */
2888 case UNFORMATTED_STREAM:
2889 return;
2891 case UNFORMATTED_SEQUENTIAL:
2892 next_record_r_unf (dtp, 1);
2893 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2894 break;
2896 case FORMATTED_DIRECT:
2897 case UNFORMATTED_DIRECT:
2898 skip_record (dtp, dtp->u.p.current_unit->bytes_left);
2899 break;
2901 case FORMATTED_STREAM:
2902 case FORMATTED_SEQUENTIAL:
2903 /* read_sf has already terminated input because of an '\n', or
2904 we have hit EOF. */
2905 if (dtp->u.p.sf_seen_eor)
2907 dtp->u.p.sf_seen_eor = 0;
2908 break;
2911 if (is_internal_unit (dtp))
2913 if (is_array_io (dtp))
2915 int finished;
2917 record = next_array_record (dtp, dtp->u.p.current_unit->ls,
2918 &finished);
2919 if (!done && finished)
2920 hit_eof (dtp);
2922 /* Now seek to this record. */
2923 record = record * dtp->u.p.current_unit->recl;
2924 if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
2926 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
2927 break;
2929 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2931 else
2933 bytes_left = (int) dtp->u.p.current_unit->bytes_left;
2934 bytes_left = min_off (bytes_left,
2935 file_length (dtp->u.p.current_unit->s)
2936 - stell (dtp->u.p.current_unit->s));
2937 if (sseek (dtp->u.p.current_unit->s,
2938 bytes_left, SEEK_CUR) < 0)
2940 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
2941 break;
2943 dtp->u.p.current_unit->bytes_left
2944 = dtp->u.p.current_unit->recl;
2946 break;
2948 else
2952 errno = 0;
2953 cc = fbuf_getc (dtp->u.p.current_unit);
2954 if (cc == EOF)
2956 if (errno != 0)
2957 generate_error (&dtp->common, LIBERROR_OS, NULL);
2958 else
2960 if (is_stream_io (dtp)
2961 || dtp->u.p.current_unit->pad_status == PAD_NO
2962 || dtp->u.p.current_unit->bytes_left
2963 == dtp->u.p.current_unit->recl)
2964 hit_eof (dtp);
2966 break;
2969 if (is_stream_io (dtp))
2970 dtp->u.p.current_unit->strm_pos++;
2972 p = (char) cc;
2974 while (p != '\n');
2976 break;
2981 /* Small utility function to write a record marker, taking care of
2982 byte swapping and of choosing the correct size. */
2984 static int
2985 write_us_marker (st_parameter_dt *dtp, const gfc_offset buf)
2987 size_t len;
2988 GFC_INTEGER_4 buf4;
2989 GFC_INTEGER_8 buf8;
2990 char p[sizeof (GFC_INTEGER_8)];
2992 if (compile_options.record_marker == 0)
2993 len = sizeof (GFC_INTEGER_4);
2994 else
2995 len = compile_options.record_marker;
2997 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
2998 if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE))
3000 switch (len)
3002 case sizeof (GFC_INTEGER_4):
3003 buf4 = buf;
3004 return swrite (dtp->u.p.current_unit->s, &buf4, len);
3005 break;
3007 case sizeof (GFC_INTEGER_8):
3008 buf8 = buf;
3009 return swrite (dtp->u.p.current_unit->s, &buf8, len);
3010 break;
3012 default:
3013 runtime_error ("Illegal value for record marker");
3014 break;
3017 else
3019 switch (len)
3021 case sizeof (GFC_INTEGER_4):
3022 buf4 = buf;
3023 reverse_memcpy (p, &buf4, sizeof (GFC_INTEGER_4));
3024 return swrite (dtp->u.p.current_unit->s, p, len);
3025 break;
3027 case sizeof (GFC_INTEGER_8):
3028 buf8 = buf;
3029 reverse_memcpy (p, &buf8, sizeof (GFC_INTEGER_8));
3030 return swrite (dtp->u.p.current_unit->s, p, len);
3031 break;
3033 default:
3034 runtime_error ("Illegal value for record marker");
3035 break;
3041 /* Position to the next (sub)record in write mode for
3042 unformatted sequential files. */
3044 static void
3045 next_record_w_unf (st_parameter_dt *dtp, int next_subrecord)
3047 gfc_offset m, m_write, record_marker;
3049 /* Bytes written. */
3050 m = dtp->u.p.current_unit->recl_subrecord
3051 - dtp->u.p.current_unit->bytes_left_subrecord;
3053 /* Write the length tail. If we finish a record containing
3054 subrecords, we write out the negative length. */
3056 if (dtp->u.p.current_unit->continued)
3057 m_write = -m;
3058 else
3059 m_write = m;
3061 if (unlikely (write_us_marker (dtp, m_write) < 0))
3062 goto io_error;
3064 if (compile_options.record_marker == 0)
3065 record_marker = sizeof (GFC_INTEGER_4);
3066 else
3067 record_marker = compile_options.record_marker;
3069 /* Seek to the head and overwrite the bogus length with the real
3070 length. */
3072 if (unlikely (sseek (dtp->u.p.current_unit->s, - m - 2 * record_marker,
3073 SEEK_CUR) < 0))
3074 goto io_error;
3076 if (next_subrecord)
3077 m_write = -m;
3078 else
3079 m_write = m;
3081 if (unlikely (write_us_marker (dtp, m_write) < 0))
3082 goto io_error;
3084 /* Seek past the end of the current record. */
3086 if (unlikely (sseek (dtp->u.p.current_unit->s, m + record_marker,
3087 SEEK_CUR) < 0))
3088 goto io_error;
3090 return;
3092 io_error:
3093 generate_error (&dtp->common, LIBERROR_OS, NULL);
3094 return;
3099 /* Utility function like memset() but operating on streams. Return
3100 value is same as for POSIX write(). */
3102 static ssize_t
3103 sset (stream * s, int c, ssize_t nbyte)
3105 static const int WRITE_CHUNK = 256;
3106 char p[WRITE_CHUNK];
3107 ssize_t bytes_left, trans;
3109 if (nbyte < WRITE_CHUNK)
3110 memset (p, c, nbyte);
3111 else
3112 memset (p, c, WRITE_CHUNK);
3114 bytes_left = nbyte;
3115 while (bytes_left > 0)
3117 trans = (bytes_left < WRITE_CHUNK) ? bytes_left : WRITE_CHUNK;
3118 trans = swrite (s, p, trans);
3119 if (trans <= 0)
3120 return trans;
3121 bytes_left -= trans;
3124 return nbyte - bytes_left;
3127 static inline void
3128 memset4 (gfc_char4_t *p, gfc_char4_t c, int k)
3130 int j;
3131 for (j = 0; j < k; j++)
3132 *p++ = c;
3135 /* Position to the next record in write mode. */
3137 static void
3138 next_record_w (st_parameter_dt *dtp, int done)
3140 gfc_offset m, record, max_pos;
3141 int length;
3143 /* Zero counters for X- and T-editing. */
3144 max_pos = dtp->u.p.max_pos;
3145 dtp->u.p.max_pos = dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
3147 switch (current_mode (dtp))
3149 /* No records in unformatted STREAM I/O. */
3150 case UNFORMATTED_STREAM:
3151 return;
3153 case FORMATTED_DIRECT:
3154 if (dtp->u.p.current_unit->bytes_left == 0)
3155 break;
3157 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
3158 fbuf_flush (dtp->u.p.current_unit, WRITING);
3159 if (sset (dtp->u.p.current_unit->s, ' ',
3160 dtp->u.p.current_unit->bytes_left)
3161 != dtp->u.p.current_unit->bytes_left)
3162 goto io_error;
3164 break;
3166 case UNFORMATTED_DIRECT:
3167 if (dtp->u.p.current_unit->bytes_left > 0)
3169 length = (int) dtp->u.p.current_unit->bytes_left;
3170 if (sset (dtp->u.p.current_unit->s, 0, length) != length)
3171 goto io_error;
3173 break;
3175 case UNFORMATTED_SEQUENTIAL:
3176 next_record_w_unf (dtp, 0);
3177 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
3178 break;
3180 case FORMATTED_STREAM:
3181 case FORMATTED_SEQUENTIAL:
3183 if (is_internal_unit (dtp))
3185 char *p;
3186 if (is_array_io (dtp))
3188 int finished;
3190 length = (int) dtp->u.p.current_unit->bytes_left;
3192 /* If the farthest position reached is greater than current
3193 position, adjust the position and set length to pad out
3194 whats left. Otherwise just pad whats left.
3195 (for character array unit) */
3196 m = dtp->u.p.current_unit->recl
3197 - dtp->u.p.current_unit->bytes_left;
3198 if (max_pos > m)
3200 length = (int) (max_pos - m);
3201 if (sseek (dtp->u.p.current_unit->s,
3202 length, SEEK_CUR) < 0)
3204 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3205 return;
3207 length = (int) (dtp->u.p.current_unit->recl - max_pos);
3210 p = write_block (dtp, length);
3211 if (p == NULL)
3212 return;
3214 if (unlikely (is_char4_unit (dtp)))
3216 gfc_char4_t *p4 = (gfc_char4_t *) p;
3217 memset4 (p4, ' ', length);
3219 else
3220 memset (p, ' ', length);
3222 /* Now that the current record has been padded out,
3223 determine where the next record in the array is. */
3224 record = next_array_record (dtp, dtp->u.p.current_unit->ls,
3225 &finished);
3226 if (finished)
3227 dtp->u.p.current_unit->endfile = AT_ENDFILE;
3229 /* Now seek to this record */
3230 record = record * dtp->u.p.current_unit->recl;
3232 if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
3234 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3235 return;
3238 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
3240 else
3242 length = 1;
3244 /* If this is the last call to next_record move to the farthest
3245 position reached and set length to pad out the remainder
3246 of the record. (for character scaler unit) */
3247 if (done)
3249 m = dtp->u.p.current_unit->recl
3250 - dtp->u.p.current_unit->bytes_left;
3251 if (max_pos > m)
3253 length = (int) (max_pos - m);
3254 if (sseek (dtp->u.p.current_unit->s,
3255 length, SEEK_CUR) < 0)
3257 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3258 return;
3260 length = (int) (dtp->u.p.current_unit->recl - max_pos);
3262 else
3263 length = (int) dtp->u.p.current_unit->bytes_left;
3265 if (length > 0)
3267 p = write_block (dtp, length);
3268 if (p == NULL)
3269 return;
3271 if (unlikely (is_char4_unit (dtp)))
3273 gfc_char4_t *p4 = (gfc_char4_t *) p;
3274 memset4 (p4, (gfc_char4_t) ' ', length);
3276 else
3277 memset (p, ' ', length);
3281 else
3283 #ifdef HAVE_CRLF
3284 const int len = 2;
3285 #else
3286 const int len = 1;
3287 #endif
3288 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
3289 char * p = fbuf_alloc (dtp->u.p.current_unit, len);
3290 if (!p)
3291 goto io_error;
3292 #ifdef HAVE_CRLF
3293 *(p++) = '\r';
3294 #endif
3295 *p = '\n';
3296 if (is_stream_io (dtp))
3298 dtp->u.p.current_unit->strm_pos += len;
3299 if (dtp->u.p.current_unit->strm_pos
3300 < file_length (dtp->u.p.current_unit->s))
3301 unit_truncate (dtp->u.p.current_unit,
3302 dtp->u.p.current_unit->strm_pos - 1,
3303 &dtp->common);
3307 break;
3309 io_error:
3310 generate_error (&dtp->common, LIBERROR_OS, NULL);
3311 break;
3315 /* Position to the next record, which means moving to the end of the
3316 current record. This can happen under several different
3317 conditions. If the done flag is not set, we get ready to process
3318 the next record. */
3320 void
3321 next_record (st_parameter_dt *dtp, int done)
3323 gfc_offset fp; /* File position. */
3325 dtp->u.p.current_unit->read_bad = 0;
3327 if (dtp->u.p.mode == READING)
3328 next_record_r (dtp, done);
3329 else
3330 next_record_w (dtp, done);
3332 if (!is_stream_io (dtp))
3334 /* Keep position up to date for INQUIRE */
3335 if (done)
3336 update_position (dtp->u.p.current_unit);
3338 dtp->u.p.current_unit->current_record = 0;
3339 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
3341 fp = stell (dtp->u.p.current_unit->s);
3342 /* Calculate next record, rounding up partial records. */
3343 dtp->u.p.current_unit->last_record =
3344 (fp + dtp->u.p.current_unit->recl - 1) /
3345 dtp->u.p.current_unit->recl;
3347 else
3348 dtp->u.p.current_unit->last_record++;
3351 if (!done)
3352 pre_position (dtp);
3354 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
3358 /* Finalize the current data transfer. For a nonadvancing transfer,
3359 this means advancing to the next record. For internal units close the
3360 stream associated with the unit. */
3362 static void
3363 finalize_transfer (st_parameter_dt *dtp)
3365 jmp_buf eof_jump;
3366 GFC_INTEGER_4 cf = dtp->common.flags;
3368 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
3369 *dtp->size = dtp->u.p.size_used;
3371 if (dtp->u.p.eor_condition)
3373 generate_error (&dtp->common, LIBERROR_EOR, NULL);
3374 return;
3377 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
3379 if (dtp->u.p.current_unit && current_mode (dtp) == UNFORMATTED_SEQUENTIAL)
3380 dtp->u.p.current_unit->current_record = 0;
3381 return;
3384 if ((dtp->u.p.ionml != NULL)
3385 && (cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0)
3387 if ((cf & IOPARM_DT_NAMELIST_READ_MODE) != 0)
3388 namelist_read (dtp);
3389 else
3390 namelist_write (dtp);
3393 dtp->u.p.transfer = NULL;
3394 if (dtp->u.p.current_unit == NULL)
3395 return;
3397 dtp->u.p.eof_jump = &eof_jump;
3398 if (setjmp (eof_jump))
3400 generate_error (&dtp->common, LIBERROR_END, NULL);
3401 return;
3404 if ((cf & IOPARM_DT_LIST_FORMAT) != 0 && dtp->u.p.mode == READING)
3406 finish_list_read (dtp);
3407 return;
3410 if (dtp->u.p.mode == WRITING)
3411 dtp->u.p.current_unit->previous_nonadvancing_write
3412 = dtp->u.p.advance_status == ADVANCE_NO;
3414 if (is_stream_io (dtp))
3416 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
3417 && dtp->u.p.advance_status != ADVANCE_NO)
3418 next_record (dtp, 1);
3420 return;
3423 dtp->u.p.current_unit->current_record = 0;
3425 if (!is_internal_unit (dtp) && dtp->u.p.seen_dollar)
3427 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
3428 dtp->u.p.seen_dollar = 0;
3429 return;
3432 /* For non-advancing I/O, save the current maximum position for use in the
3433 next I/O operation if needed. */
3434 if (dtp->u.p.advance_status == ADVANCE_NO)
3436 int bytes_written = (int) (dtp->u.p.current_unit->recl
3437 - dtp->u.p.current_unit->bytes_left);
3438 dtp->u.p.current_unit->saved_pos =
3439 dtp->u.p.max_pos > 0 ? dtp->u.p.max_pos - bytes_written : 0;
3440 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
3441 return;
3443 else if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
3444 && dtp->u.p.mode == WRITING && !is_internal_unit (dtp))
3445 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
3447 dtp->u.p.current_unit->saved_pos = 0;
3449 next_record (dtp, 1);
3452 /* Transfer function for IOLENGTH. It doesn't actually do any
3453 data transfer, it just updates the length counter. */
3455 static void
3456 iolength_transfer (st_parameter_dt *dtp, bt type __attribute__((unused)),
3457 void *dest __attribute__ ((unused)),
3458 int kind __attribute__((unused)),
3459 size_t size, size_t nelems)
3461 if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
3462 *dtp->iolength += (GFC_IO_INT) (size * nelems);
3466 /* Initialize the IOLENGTH data transfer. This function is in essence
3467 a very much simplified version of data_transfer_init(), because it
3468 doesn't have to deal with units at all. */
3470 static void
3471 iolength_transfer_init (st_parameter_dt *dtp)
3473 if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
3474 *dtp->iolength = 0;
3476 memset (&dtp->u.p, 0, sizeof (dtp->u.p));
3478 /* Set up the subroutine that will handle the transfers. */
3480 dtp->u.p.transfer = iolength_transfer;
3484 /* Library entry point for the IOLENGTH form of the INQUIRE
3485 statement. The IOLENGTH form requires no I/O to be performed, but
3486 it must still be a runtime library call so that we can determine
3487 the iolength for dynamic arrays and such. */
3489 extern void st_iolength (st_parameter_dt *);
3490 export_proto(st_iolength);
3492 void
3493 st_iolength (st_parameter_dt *dtp)
3495 library_start (&dtp->common);
3496 iolength_transfer_init (dtp);
3499 extern void st_iolength_done (st_parameter_dt *);
3500 export_proto(st_iolength_done);
3502 void
3503 st_iolength_done (st_parameter_dt *dtp __attribute__((unused)))
3505 free_ionml (dtp);
3506 library_end ();
3510 /* The READ statement. */
3512 extern void st_read (st_parameter_dt *);
3513 export_proto(st_read);
3515 void
3516 st_read (st_parameter_dt *dtp)
3518 library_start (&dtp->common);
3520 data_transfer_init (dtp, 1);
3523 extern void st_read_done (st_parameter_dt *);
3524 export_proto(st_read_done);
3526 void
3527 st_read_done (st_parameter_dt *dtp)
3529 finalize_transfer (dtp);
3530 if (is_internal_unit (dtp) || dtp->u.p.format_not_saved)
3531 free_format_data (dtp->u.p.fmt);
3532 free_ionml (dtp);
3533 if (dtp->u.p.current_unit != NULL)
3534 unlock_unit (dtp->u.p.current_unit);
3536 free_internal_unit (dtp);
3538 library_end ();
3541 extern void st_write (st_parameter_dt *);
3542 export_proto(st_write);
3544 void
3545 st_write (st_parameter_dt *dtp)
3547 library_start (&dtp->common);
3548 data_transfer_init (dtp, 0);
3551 extern void st_write_done (st_parameter_dt *);
3552 export_proto(st_write_done);
3554 void
3555 st_write_done (st_parameter_dt *dtp)
3557 finalize_transfer (dtp);
3559 /* Deal with endfile conditions associated with sequential files. */
3561 if (dtp->u.p.current_unit != NULL
3562 && dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
3563 switch (dtp->u.p.current_unit->endfile)
3565 case AT_ENDFILE: /* Remain at the endfile record. */
3566 break;
3568 case AFTER_ENDFILE:
3569 dtp->u.p.current_unit->endfile = AT_ENDFILE; /* Just at it now. */
3570 break;
3572 case NO_ENDFILE:
3573 /* Get rid of whatever is after this record. */
3574 if (!is_internal_unit (dtp))
3575 unit_truncate (dtp->u.p.current_unit,
3576 stell (dtp->u.p.current_unit->s),
3577 &dtp->common);
3578 dtp->u.p.current_unit->endfile = AT_ENDFILE;
3579 break;
3582 if (is_internal_unit (dtp) || dtp->u.p.format_not_saved)
3583 free_format_data (dtp->u.p.fmt);
3584 free_ionml (dtp);
3585 if (dtp->u.p.current_unit != NULL)
3586 unlock_unit (dtp->u.p.current_unit);
3588 free_internal_unit (dtp);
3590 library_end ();
3594 /* F2003: This is a stub for the runtime portion of the WAIT statement. */
3595 void
3596 st_wait (st_parameter_wait *wtp __attribute__((unused)))
3601 /* Receives the scalar information for namelist objects and stores it
3602 in a linked list of namelist_info types. */
3604 extern void st_set_nml_var (st_parameter_dt *dtp, void *, char *,
3605 GFC_INTEGER_4, gfc_charlen_type, GFC_INTEGER_4);
3606 export_proto(st_set_nml_var);
3609 void
3610 st_set_nml_var (st_parameter_dt *dtp, void * var_addr, char * var_name,
3611 GFC_INTEGER_4 len, gfc_charlen_type string_length,
3612 GFC_INTEGER_4 dtype)
3614 namelist_info *t1 = NULL;
3615 namelist_info *nml;
3616 size_t var_name_len = strlen (var_name);
3618 nml = (namelist_info*) get_mem (sizeof (namelist_info));
3620 nml->mem_pos = var_addr;
3622 nml->var_name = (char*) get_mem (var_name_len + 1);
3623 memcpy (nml->var_name, var_name, var_name_len);
3624 nml->var_name[var_name_len] = '\0';
3626 nml->len = (int) len;
3627 nml->string_length = (index_type) string_length;
3629 nml->var_rank = (int) (dtype & GFC_DTYPE_RANK_MASK);
3630 nml->size = (index_type) (dtype >> GFC_DTYPE_SIZE_SHIFT);
3631 nml->type = (bt) ((dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT);
3633 if (nml->var_rank > 0)
3635 nml->dim = (descriptor_dimension*)
3636 get_mem (nml->var_rank * sizeof (descriptor_dimension));
3637 nml->ls = (array_loop_spec*)
3638 get_mem (nml->var_rank * sizeof (array_loop_spec));
3640 else
3642 nml->dim = NULL;
3643 nml->ls = NULL;
3646 nml->next = NULL;
3648 if ((dtp->common.flags & IOPARM_DT_IONML_SET) == 0)
3650 dtp->common.flags |= IOPARM_DT_IONML_SET;
3651 dtp->u.p.ionml = nml;
3653 else
3655 for (t1 = dtp->u.p.ionml; t1->next; t1 = t1->next);
3656 t1->next = nml;
3660 /* Store the dimensional information for the namelist object. */
3661 extern void st_set_nml_var_dim (st_parameter_dt *, GFC_INTEGER_4,
3662 index_type, index_type,
3663 index_type);
3664 export_proto(st_set_nml_var_dim);
3666 void
3667 st_set_nml_var_dim (st_parameter_dt *dtp, GFC_INTEGER_4 n_dim,
3668 index_type stride, index_type lbound,
3669 index_type ubound)
3671 namelist_info * nml;
3672 int n;
3674 n = (int)n_dim;
3676 for (nml = dtp->u.p.ionml; nml->next; nml = nml->next);
3678 GFC_DIMENSION_SET(nml->dim[n],lbound,ubound,stride);
3681 /* Reverse memcpy - used for byte swapping. */
3683 void reverse_memcpy (void *dest, const void *src, size_t n)
3685 char *d, *s;
3686 size_t i;
3688 d = (char *) dest;
3689 s = (char *) src + n - 1;
3691 /* Write with ascending order - this is likely faster
3692 on modern architectures because of write combining. */
3693 for (i=0; i<n; i++)
3694 *(d++) = *(s--);
3698 /* Once upon a time, a poor innocent Fortran program was reading a
3699 file, when suddenly it hit the end-of-file (EOF). Unfortunately
3700 the OS doesn't tell whether we're at the EOF or whether we already
3701 went past it. Luckily our hero, libgfortran, keeps track of this.
3702 Call this function when you detect an EOF condition. See Section
3703 9.10.2 in F2003. */
3705 void
3706 hit_eof (st_parameter_dt * dtp)
3708 dtp->u.p.current_unit->flags.position = POSITION_APPEND;
3710 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
3711 switch (dtp->u.p.current_unit->endfile)
3713 case NO_ENDFILE:
3714 case AT_ENDFILE:
3715 generate_error (&dtp->common, LIBERROR_END, NULL);
3716 if (!is_internal_unit (dtp))
3718 dtp->u.p.current_unit->endfile = AFTER_ENDFILE;
3719 dtp->u.p.current_unit->current_record = 0;
3721 else
3722 dtp->u.p.current_unit->endfile = AT_ENDFILE;
3723 break;
3725 case AFTER_ENDFILE:
3726 generate_error (&dtp->common, LIBERROR_ENDFILE, NULL);
3727 dtp->u.p.current_unit->current_record = 0;
3728 break;
3730 else
3732 /* Non-sequential files don't have an ENDFILE record, so we
3733 can't be at AFTER_ENDFILE. */
3734 dtp->u.p.current_unit->endfile = AT_ENDFILE;
3735 generate_error (&dtp->common, LIBERROR_END, NULL);
3736 dtp->u.p.current_unit->current_record = 0;