PR libfortran/62768 Handle filenames with embedded null characters.
[official-gcc.git] / libgfortran / io / transfer.c
blobdc1b6f4145ac257c746dc441e412c2a875d01865
1 /* Copyright (C) 2002-2014 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
3 Namelist transfer functions contributed by Paul Thomas
4 F2003 I/O support contributed by Jerry DeLisle
6 This file is part of the GNU Fortran runtime library (libgfortran).
8 Libgfortran is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 Libgfortran is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
28 /* transfer.c -- Top level handling of data transfer statements. */
30 #include "io.h"
31 #include "fbuf.h"
32 #include "format.h"
33 #include "unix.h"
34 #include <string.h>
35 #include <assert.h>
36 #include <stdlib.h>
37 #include <errno.h>
40 /* Calling conventions: Data transfer statements are unlike other
41 library calls in that they extend over several calls.
43 The first call is always a call to st_read() or st_write(). These
44 subroutines return no status unless a namelist read or write is
45 being done, in which case there is the usual status. No further
46 calls are necessary in this case.
48 For other sorts of data transfer, there are zero or more data
49 transfer statement that depend on the format of the data transfer
50 statement. For READ (and for backwards compatibily: for WRITE), one has
52 transfer_integer
53 transfer_logical
54 transfer_character
55 transfer_character_wide
56 transfer_real
57 transfer_complex
58 transfer_real128
59 transfer_complex128
61 and for WRITE
63 transfer_integer_write
64 transfer_logical_write
65 transfer_character_write
66 transfer_character_wide_write
67 transfer_real_write
68 transfer_complex_write
69 transfer_real128_write
70 transfer_complex128_write
72 These subroutines do not return status. The *128 functions
73 are in the file transfer128.c.
75 The last call is a call to st_[read|write]_done(). While
76 something can easily go wrong with the initial st_read() or
77 st_write(), an error inhibits any data from actually being
78 transferred. */
80 extern void transfer_integer (st_parameter_dt *, void *, int);
81 export_proto(transfer_integer);
83 extern void transfer_integer_write (st_parameter_dt *, void *, int);
84 export_proto(transfer_integer_write);
86 extern void transfer_real (st_parameter_dt *, void *, int);
87 export_proto(transfer_real);
89 extern void transfer_real_write (st_parameter_dt *, void *, int);
90 export_proto(transfer_real_write);
92 extern void transfer_logical (st_parameter_dt *, void *, int);
93 export_proto(transfer_logical);
95 extern void transfer_logical_write (st_parameter_dt *, void *, int);
96 export_proto(transfer_logical_write);
98 extern void transfer_character (st_parameter_dt *, void *, int);
99 export_proto(transfer_character);
101 extern void transfer_character_write (st_parameter_dt *, void *, int);
102 export_proto(transfer_character_write);
104 extern void transfer_character_wide (st_parameter_dt *, void *, int, int);
105 export_proto(transfer_character_wide);
107 extern void transfer_character_wide_write (st_parameter_dt *,
108 void *, int, int);
109 export_proto(transfer_character_wide_write);
111 extern void transfer_complex (st_parameter_dt *, void *, int);
112 export_proto(transfer_complex);
114 extern void transfer_complex_write (st_parameter_dt *, void *, int);
115 export_proto(transfer_complex_write);
117 extern void transfer_array (st_parameter_dt *, gfc_array_char *, int,
118 gfc_charlen_type);
119 export_proto(transfer_array);
121 extern void transfer_array_write (st_parameter_dt *, gfc_array_char *, int,
122 gfc_charlen_type);
123 export_proto(transfer_array_write);
125 static void us_read (st_parameter_dt *, int);
126 static void us_write (st_parameter_dt *, int);
127 static void next_record_r_unf (st_parameter_dt *, int);
128 static void next_record_w_unf (st_parameter_dt *, int);
130 static const st_option advance_opt[] = {
131 {"yes", ADVANCE_YES},
132 {"no", ADVANCE_NO},
133 {NULL, 0}
137 static const st_option decimal_opt[] = {
138 {"point", DECIMAL_POINT},
139 {"comma", DECIMAL_COMMA},
140 {NULL, 0}
143 static const st_option round_opt[] = {
144 {"up", ROUND_UP},
145 {"down", ROUND_DOWN},
146 {"zero", ROUND_ZERO},
147 {"nearest", ROUND_NEAREST},
148 {"compatible", ROUND_COMPATIBLE},
149 {"processor_defined", ROUND_PROCDEFINED},
150 {NULL, 0}
154 static const st_option sign_opt[] = {
155 {"plus", SIGN_SP},
156 {"suppress", SIGN_SS},
157 {"processor_defined", SIGN_S},
158 {NULL, 0}
161 static const st_option blank_opt[] = {
162 {"null", BLANK_NULL},
163 {"zero", BLANK_ZERO},
164 {NULL, 0}
167 static const st_option delim_opt[] = {
168 {"apostrophe", DELIM_APOSTROPHE},
169 {"quote", DELIM_QUOTE},
170 {"none", DELIM_NONE},
171 {NULL, 0}
174 static const st_option pad_opt[] = {
175 {"yes", PAD_YES},
176 {"no", PAD_NO},
177 {NULL, 0}
180 typedef enum
181 { FORMATTED_SEQUENTIAL, UNFORMATTED_SEQUENTIAL,
182 FORMATTED_DIRECT, UNFORMATTED_DIRECT, FORMATTED_STREAM, UNFORMATTED_STREAM
184 file_mode;
187 static file_mode
188 current_mode (st_parameter_dt *dtp)
190 file_mode m;
192 m = FORM_UNSPECIFIED;
194 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
196 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
197 FORMATTED_DIRECT : UNFORMATTED_DIRECT;
199 else if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
201 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
202 FORMATTED_SEQUENTIAL : UNFORMATTED_SEQUENTIAL;
204 else if (dtp->u.p.current_unit->flags.access == ACCESS_STREAM)
206 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
207 FORMATTED_STREAM : UNFORMATTED_STREAM;
210 return m;
214 /* Mid level data transfer statements. */
216 /* Read sequential file - internal unit */
218 static char *
219 read_sf_internal (st_parameter_dt *dtp, int * length)
221 static char *empty_string[0];
222 char *base;
223 int lorig;
225 /* Zero size array gives internal unit len of 0. Nothing to read. */
226 if (dtp->internal_unit_len == 0
227 && dtp->u.p.current_unit->pad_status == PAD_NO)
228 hit_eof (dtp);
230 /* If we have seen an eor previously, return a length of 0. The
231 caller is responsible for correctly padding the input field. */
232 if (dtp->u.p.sf_seen_eor)
234 *length = 0;
235 /* Just return something that isn't a NULL pointer, otherwise the
236 caller thinks an error occurred. */
237 return (char*) empty_string;
240 lorig = *length;
241 if (is_char4_unit(dtp))
243 int i;
244 gfc_char4_t *p = (gfc_char4_t *) mem_alloc_r4 (dtp->u.p.current_unit->s,
245 length);
246 base = fbuf_alloc (dtp->u.p.current_unit, lorig);
247 for (i = 0; i < *length; i++, p++)
248 base[i] = *p > 255 ? '?' : (unsigned char) *p;
250 else
251 base = mem_alloc_r (dtp->u.p.current_unit->s, length);
253 if (unlikely (lorig > *length))
255 hit_eof (dtp);
256 return NULL;
259 dtp->u.p.current_unit->bytes_left -= *length;
261 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
262 dtp->u.p.size_used += (GFC_IO_INT) *length;
264 return base;
268 /* When reading sequential formatted records we have a problem. We
269 don't know how long the line is until we read the trailing newline,
270 and we don't want to read too much. If we read too much, we might
271 have to do a physical seek backwards depending on how much data is
272 present, and devices like terminals aren't seekable and would cause
273 an I/O error.
275 Given this, the solution is to read a byte at a time, stopping if
276 we hit the newline. For small allocations, we use a static buffer.
277 For larger allocations, we are forced to allocate memory on the
278 heap. Hopefully this won't happen very often. */
280 /* Read sequential file - external unit */
282 static char *
283 read_sf (st_parameter_dt *dtp, int * length)
285 static char *empty_string[0];
286 int q, q2;
287 int n, lorig, seen_comma;
289 /* If we have seen an eor previously, return a length of 0. The
290 caller is responsible for correctly padding the input field. */
291 if (dtp->u.p.sf_seen_eor)
293 *length = 0;
294 /* Just return something that isn't a NULL pointer, otherwise the
295 caller thinks an error occurred. */
296 return (char*) empty_string;
299 n = seen_comma = 0;
301 /* Read data into format buffer and scan through it. */
302 lorig = *length;
304 while (n < *length)
306 q = fbuf_getc (dtp->u.p.current_unit);
307 if (q == EOF)
308 break;
309 else if (q == '\n' || q == '\r')
311 /* Unexpected end of line. Set the position. */
312 dtp->u.p.sf_seen_eor = 1;
314 /* If we see an EOR during non-advancing I/O, we need to skip
315 the rest of the I/O statement. Set the corresponding flag. */
316 if (dtp->u.p.advance_status == ADVANCE_NO || dtp->u.p.seen_dollar)
317 dtp->u.p.eor_condition = 1;
319 /* If we encounter a CR, it might be a CRLF. */
320 if (q == '\r') /* Probably a CRLF */
322 /* See if there is an LF. */
323 q2 = fbuf_getc (dtp->u.p.current_unit);
324 if (q2 == '\n')
325 dtp->u.p.sf_seen_eor = 2;
326 else if (q2 != EOF) /* Oops, seek back. */
327 fbuf_seek (dtp->u.p.current_unit, -1, SEEK_CUR);
330 /* Without padding, terminate the I/O statement without assigning
331 the value. With padding, the value still needs to be assigned,
332 so we can just continue with a short read. */
333 if (dtp->u.p.current_unit->pad_status == PAD_NO)
335 generate_error (&dtp->common, LIBERROR_EOR, NULL);
336 return NULL;
339 *length = n;
340 goto done;
342 /* Short circuit the read if a comma is found during numeric input.
343 The flag is set to zero during character reads so that commas in
344 strings are not ignored */
345 else if (q == ',')
346 if (dtp->u.p.sf_read_comma == 1)
348 seen_comma = 1;
349 notify_std (&dtp->common, GFC_STD_GNU,
350 "Comma in formatted numeric read.");
351 break;
353 n++;
356 *length = n;
358 /* A short read implies we hit EOF, unless we hit EOR, a comma, or
359 some other stuff. Set the relevant flags. */
360 if (lorig > *length && !dtp->u.p.sf_seen_eor && !seen_comma)
362 if (n > 0)
364 if (dtp->u.p.advance_status == ADVANCE_NO)
366 if (dtp->u.p.current_unit->pad_status == PAD_NO)
368 hit_eof (dtp);
369 return NULL;
371 else
372 dtp->u.p.eor_condition = 1;
374 else
375 dtp->u.p.at_eof = 1;
377 else if (dtp->u.p.advance_status == ADVANCE_NO
378 || dtp->u.p.current_unit->pad_status == PAD_NO
379 || dtp->u.p.current_unit->bytes_left
380 == dtp->u.p.current_unit->recl)
382 hit_eof (dtp);
383 return NULL;
387 done:
389 dtp->u.p.current_unit->bytes_left -= n;
391 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
392 dtp->u.p.size_used += (GFC_IO_INT) n;
394 /* We can't call fbuf_getptr before the loop doing fbuf_getc, because
395 fbuf_getc might reallocate the buffer. So return current pointer
396 minus all the advances, which is n plus up to two characters
397 of newline or comma. */
398 return fbuf_getptr (dtp->u.p.current_unit)
399 - n - dtp->u.p.sf_seen_eor - seen_comma;
403 /* Function for reading the next couple of bytes from the current
404 file, advancing the current position. We return NULL on end of record or
405 end of file. This function is only for formatted I/O, unformatted uses
406 read_block_direct.
408 If the read is short, then it is because the current record does not
409 have enough data to satisfy the read request and the file was
410 opened with PAD=YES. The caller must assume tailing spaces for
411 short reads. */
413 void *
414 read_block_form (st_parameter_dt *dtp, int * nbytes)
416 char *source;
417 int norig;
419 if (!is_stream_io (dtp))
421 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) *nbytes)
423 /* For preconnected units with default record length, set bytes left
424 to unit record length and proceed, otherwise error. */
425 if (dtp->u.p.current_unit->unit_number == options.stdin_unit
426 && dtp->u.p.current_unit->recl == DEFAULT_RECL)
427 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
428 else
430 if (unlikely (dtp->u.p.current_unit->pad_status == PAD_NO)
431 && !is_internal_unit (dtp))
433 /* Not enough data left. */
434 generate_error (&dtp->common, LIBERROR_EOR, NULL);
435 return NULL;
439 if (unlikely (dtp->u.p.current_unit->bytes_left == 0
440 && !is_internal_unit(dtp)))
442 hit_eof (dtp);
443 return NULL;
446 *nbytes = dtp->u.p.current_unit->bytes_left;
450 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
451 (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL ||
452 dtp->u.p.current_unit->flags.access == ACCESS_STREAM))
454 if (is_internal_unit (dtp))
455 source = read_sf_internal (dtp, nbytes);
456 else
457 source = read_sf (dtp, nbytes);
459 dtp->u.p.current_unit->strm_pos +=
460 (gfc_offset) (*nbytes + dtp->u.p.sf_seen_eor);
461 return source;
464 /* If we reach here, we can assume it's direct access. */
466 dtp->u.p.current_unit->bytes_left -= (gfc_offset) *nbytes;
468 norig = *nbytes;
469 source = fbuf_read (dtp->u.p.current_unit, nbytes);
470 fbuf_seek (dtp->u.p.current_unit, *nbytes, SEEK_CUR);
472 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
473 dtp->u.p.size_used += (GFC_IO_INT) *nbytes;
475 if (norig != *nbytes)
477 /* Short read, this shouldn't happen. */
478 if (dtp->u.p.current_unit->pad_status == PAD_NO)
480 generate_error (&dtp->common, LIBERROR_EOR, NULL);
481 source = NULL;
485 dtp->u.p.current_unit->strm_pos += (gfc_offset) *nbytes;
487 return source;
491 /* Read a block from a character(kind=4) internal unit, to be transferred into
492 a character(kind=4) variable. Note: Portions of this code borrowed from
493 read_sf_internal. */
494 void *
495 read_block_form4 (st_parameter_dt *dtp, int * nbytes)
497 static gfc_char4_t *empty_string[0];
498 gfc_char4_t *source;
499 int lorig;
501 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) *nbytes)
502 *nbytes = dtp->u.p.current_unit->bytes_left;
504 /* Zero size array gives internal unit len of 0. Nothing to read. */
505 if (dtp->internal_unit_len == 0
506 && dtp->u.p.current_unit->pad_status == PAD_NO)
507 hit_eof (dtp);
509 /* If we have seen an eor previously, return a length of 0. The
510 caller is responsible for correctly padding the input field. */
511 if (dtp->u.p.sf_seen_eor)
513 *nbytes = 0;
514 /* Just return something that isn't a NULL pointer, otherwise the
515 caller thinks an error occurred. */
516 return empty_string;
519 lorig = *nbytes;
520 source = (gfc_char4_t *) mem_alloc_r4 (dtp->u.p.current_unit->s, nbytes);
522 if (unlikely (lorig > *nbytes))
524 hit_eof (dtp);
525 return NULL;
528 dtp->u.p.current_unit->bytes_left -= *nbytes;
530 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
531 dtp->u.p.size_used += (GFC_IO_INT) *nbytes;
533 return source;
537 /* Reads a block directly into application data space. This is for
538 unformatted files. */
540 static void
541 read_block_direct (st_parameter_dt *dtp, void *buf, size_t nbytes)
543 ssize_t to_read_record;
544 ssize_t have_read_record;
545 ssize_t to_read_subrecord;
546 ssize_t have_read_subrecord;
547 int short_record;
549 if (is_stream_io (dtp))
551 have_read_record = sread (dtp->u.p.current_unit->s, buf,
552 nbytes);
553 if (unlikely (have_read_record < 0))
555 generate_error (&dtp->common, LIBERROR_OS, NULL);
556 return;
559 dtp->u.p.current_unit->strm_pos += (gfc_offset) have_read_record;
561 if (unlikely ((ssize_t) nbytes != have_read_record))
563 /* Short read, e.g. if we hit EOF. For stream files,
564 we have to set the end-of-file condition. */
565 hit_eof (dtp);
567 return;
570 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
572 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) nbytes)
574 short_record = 1;
575 to_read_record = dtp->u.p.current_unit->bytes_left;
576 nbytes = to_read_record;
578 else
580 short_record = 0;
581 to_read_record = nbytes;
584 dtp->u.p.current_unit->bytes_left -= to_read_record;
586 to_read_record = sread (dtp->u.p.current_unit->s, buf, to_read_record);
587 if (unlikely (to_read_record < 0))
589 generate_error (&dtp->common, LIBERROR_OS, NULL);
590 return;
593 if (to_read_record != (ssize_t) nbytes)
595 /* Short read, e.g. if we hit EOF. Apparently, we read
596 more than was written to the last record. */
597 return;
600 if (unlikely (short_record))
602 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
604 return;
607 /* Unformatted sequential. We loop over the subrecords, reading
608 until the request has been fulfilled or the record has run out
609 of continuation subrecords. */
611 /* Check whether we exceed the total record length. */
613 if (dtp->u.p.current_unit->flags.has_recl
614 && ((gfc_offset) nbytes > dtp->u.p.current_unit->bytes_left))
616 to_read_record = dtp->u.p.current_unit->bytes_left;
617 short_record = 1;
619 else
621 to_read_record = nbytes;
622 short_record = 0;
624 have_read_record = 0;
626 while(1)
628 if (dtp->u.p.current_unit->bytes_left_subrecord
629 < (gfc_offset) to_read_record)
631 to_read_subrecord = dtp->u.p.current_unit->bytes_left_subrecord;
632 to_read_record -= to_read_subrecord;
634 else
636 to_read_subrecord = to_read_record;
637 to_read_record = 0;
640 dtp->u.p.current_unit->bytes_left_subrecord -= to_read_subrecord;
642 have_read_subrecord = sread (dtp->u.p.current_unit->s,
643 buf + have_read_record, to_read_subrecord);
644 if (unlikely (have_read_subrecord < 0))
646 generate_error (&dtp->common, LIBERROR_OS, NULL);
647 return;
650 have_read_record += have_read_subrecord;
652 if (unlikely (to_read_subrecord != have_read_subrecord))
654 /* Short read, e.g. if we hit EOF. This means the record
655 structure has been corrupted, or the trailing record
656 marker would still be present. */
658 generate_error (&dtp->common, LIBERROR_CORRUPT_FILE, NULL);
659 return;
662 if (to_read_record > 0)
664 if (likely (dtp->u.p.current_unit->continued))
666 next_record_r_unf (dtp, 0);
667 us_read (dtp, 1);
669 else
671 /* Let's make sure the file position is correctly pre-positioned
672 for the next read statement. */
674 dtp->u.p.current_unit->current_record = 0;
675 next_record_r_unf (dtp, 0);
676 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
677 return;
680 else
682 /* Normal exit, the read request has been fulfilled. */
683 break;
687 dtp->u.p.current_unit->bytes_left -= have_read_record;
688 if (unlikely (short_record))
690 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
691 return;
693 return;
697 /* Function for writing a block of bytes to the current file at the
698 current position, advancing the file pointer. We are given a length
699 and return a pointer to a buffer that the caller must (completely)
700 fill in. Returns NULL on error. */
702 void *
703 write_block (st_parameter_dt *dtp, int length)
705 char *dest;
707 if (!is_stream_io (dtp))
709 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) length)
711 /* For preconnected units with default record length, set bytes left
712 to unit record length and proceed, otherwise error. */
713 if (likely ((dtp->u.p.current_unit->unit_number
714 == options.stdout_unit
715 || dtp->u.p.current_unit->unit_number
716 == options.stderr_unit)
717 && dtp->u.p.current_unit->recl == DEFAULT_RECL))
718 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
719 else
721 generate_error (&dtp->common, LIBERROR_EOR, NULL);
722 return NULL;
726 dtp->u.p.current_unit->bytes_left -= (gfc_offset) length;
729 if (is_internal_unit (dtp))
731 if (dtp->common.unit) /* char4 internel unit. */
733 gfc_char4_t *dest4;
734 dest4 = mem_alloc_w4 (dtp->u.p.current_unit->s, &length);
735 if (dest4 == NULL)
737 generate_error (&dtp->common, LIBERROR_END, NULL);
738 return NULL;
740 return dest4;
742 else
743 dest = mem_alloc_w (dtp->u.p.current_unit->s, &length);
745 if (dest == NULL)
747 generate_error (&dtp->common, LIBERROR_END, NULL);
748 return NULL;
751 if (unlikely (dtp->u.p.current_unit->endfile == AT_ENDFILE))
752 generate_error (&dtp->common, LIBERROR_END, NULL);
754 else
756 dest = fbuf_alloc (dtp->u.p.current_unit, length);
757 if (dest == NULL)
759 generate_error (&dtp->common, LIBERROR_OS, NULL);
760 return NULL;
764 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
765 dtp->u.p.size_used += (GFC_IO_INT) length;
767 dtp->u.p.current_unit->strm_pos += (gfc_offset) length;
769 return dest;
773 /* High level interface to swrite(), taking care of errors. This is only
774 called for unformatted files. There are three cases to consider:
775 Stream I/O, unformatted direct, unformatted sequential. */
777 static bool
778 write_buf (st_parameter_dt *dtp, void *buf, size_t nbytes)
781 ssize_t have_written;
782 ssize_t to_write_subrecord;
783 int short_record;
785 /* Stream I/O. */
787 if (is_stream_io (dtp))
789 have_written = swrite (dtp->u.p.current_unit->s, buf, nbytes);
790 if (unlikely (have_written < 0))
792 generate_error (&dtp->common, LIBERROR_OS, NULL);
793 return false;
796 dtp->u.p.current_unit->strm_pos += (gfc_offset) have_written;
798 return true;
801 /* Unformatted direct access. */
803 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
805 if (unlikely (dtp->u.p.current_unit->bytes_left < (gfc_offset) nbytes))
807 generate_error (&dtp->common, LIBERROR_DIRECT_EOR, NULL);
808 return false;
811 if (buf == NULL && nbytes == 0)
812 return true;
814 have_written = swrite (dtp->u.p.current_unit->s, buf, nbytes);
815 if (unlikely (have_written < 0))
817 generate_error (&dtp->common, LIBERROR_OS, NULL);
818 return false;
821 dtp->u.p.current_unit->strm_pos += (gfc_offset) have_written;
822 dtp->u.p.current_unit->bytes_left -= (gfc_offset) have_written;
824 return true;
827 /* Unformatted sequential. */
829 have_written = 0;
831 if (dtp->u.p.current_unit->flags.has_recl
832 && (gfc_offset) nbytes > dtp->u.p.current_unit->bytes_left)
834 nbytes = dtp->u.p.current_unit->bytes_left;
835 short_record = 1;
837 else
839 short_record = 0;
842 while (1)
845 to_write_subrecord =
846 (size_t) dtp->u.p.current_unit->bytes_left_subrecord < nbytes ?
847 (size_t) dtp->u.p.current_unit->bytes_left_subrecord : nbytes;
849 dtp->u.p.current_unit->bytes_left_subrecord -=
850 (gfc_offset) to_write_subrecord;
852 to_write_subrecord = swrite (dtp->u.p.current_unit->s,
853 buf + have_written, to_write_subrecord);
854 if (unlikely (to_write_subrecord < 0))
856 generate_error (&dtp->common, LIBERROR_OS, NULL);
857 return false;
860 dtp->u.p.current_unit->strm_pos += (gfc_offset) to_write_subrecord;
861 nbytes -= to_write_subrecord;
862 have_written += to_write_subrecord;
864 if (nbytes == 0)
865 break;
867 next_record_w_unf (dtp, 1);
868 us_write (dtp, 1);
870 dtp->u.p.current_unit->bytes_left -= have_written;
871 if (unlikely (short_record))
873 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
874 return false;
876 return true;
880 /* Reverse memcpy - used for byte swapping. */
882 static void
883 reverse_memcpy (void *dest, const void *src, size_t n)
885 char *d, *s;
886 size_t i;
888 d = (char *) dest;
889 s = (char *) src + n - 1;
891 /* Write with ascending order - this is likely faster
892 on modern architectures because of write combining. */
893 for (i=0; i<n; i++)
894 *(d++) = *(s--);
898 /* Utility function for byteswapping an array, using the bswap
899 builtins if possible. dest and src can overlap completely, or then
900 they must point to separate objects; partial overlaps are not
901 allowed. */
903 static void
904 bswap_array (void *dest, const void *src, size_t size, size_t nelems)
906 const char *ps;
907 char *pd;
909 switch (size)
911 case 1:
912 break;
913 case 2:
914 for (size_t i = 0; i < nelems; i++)
915 ((uint16_t*)dest)[i] = __builtin_bswap16 (((uint16_t*)src)[i]);
916 break;
917 case 4:
918 for (size_t i = 0; i < nelems; i++)
919 ((uint32_t*)dest)[i] = __builtin_bswap32 (((uint32_t*)src)[i]);
920 break;
921 case 8:
922 for (size_t i = 0; i < nelems; i++)
923 ((uint64_t*)dest)[i] = __builtin_bswap64 (((uint64_t*)src)[i]);
924 break;
925 case 12:
926 ps = src;
927 pd = dest;
928 for (size_t i = 0; i < nelems; i++)
930 uint32_t tmp;
931 memcpy (&tmp, ps, 4);
932 *(uint32_t*)pd = __builtin_bswap32 (*(uint32_t*)(ps + 8));
933 *(uint32_t*)(pd + 4) = __builtin_bswap32 (*(uint32_t*)(ps + 4));
934 *(uint32_t*)(pd + 8) = __builtin_bswap32 (tmp);
935 ps += size;
936 pd += size;
938 break;
939 case 16:
940 ps = src;
941 pd = dest;
942 for (size_t i = 0; i < nelems; i++)
944 uint64_t tmp;
945 memcpy (&tmp, ps, 8);
946 *(uint64_t*)pd = __builtin_bswap64 (*(uint64_t*)(ps + 8));
947 *(uint64_t*)(pd + 8) = __builtin_bswap64 (tmp);
948 ps += size;
949 pd += size;
951 break;
952 default:
953 pd = dest;
954 if (dest != src)
956 ps = src;
957 for (size_t i = 0; i < nelems; i++)
959 reverse_memcpy (pd, ps, size);
960 ps += size;
961 pd += size;
964 else
966 /* In-place byte swap. */
967 for (size_t i = 0; i < nelems; i++)
969 char tmp, *low = pd, *high = pd + size - 1;
970 for (size_t j = 0; j < size/2; j++)
972 tmp = *low;
973 *low = *high;
974 *high = tmp;
975 low++;
976 high--;
978 pd += size;
985 /* Master function for unformatted reads. */
987 static void
988 unformatted_read (st_parameter_dt *dtp, bt type,
989 void *dest, int kind, size_t size, size_t nelems)
991 if (type == BT_CHARACTER)
992 size *= GFC_SIZE_OF_CHAR_KIND(kind);
993 read_block_direct (dtp, dest, size * nelems);
995 if (unlikely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_SWAP)
996 && kind != 1)
998 /* Handle wide chracters. */
999 if (type == BT_CHARACTER)
1001 nelems *= size;
1002 size = kind;
1005 /* Break up complex into its constituent reals. */
1006 else if (type == BT_COMPLEX)
1008 nelems *= 2;
1009 size /= 2;
1011 bswap_array (dest, dest, size, nelems);
1016 /* Master function for unformatted writes. NOTE: For kind=10 the size is 16
1017 bytes on 64 bit machines. The unused bytes are not initialized and never
1018 used, which can show an error with memory checking analyzers like
1019 valgrind. */
1021 static void
1022 unformatted_write (st_parameter_dt *dtp, bt type,
1023 void *source, int kind, size_t size, size_t nelems)
1025 if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE)
1026 || kind == 1)
1028 size_t stride = type == BT_CHARACTER ?
1029 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
1031 write_buf (dtp, source, stride * nelems);
1033 else
1035 #define BSWAP_BUFSZ 512
1036 char buffer[BSWAP_BUFSZ];
1037 char *p;
1038 size_t nrem;
1040 p = source;
1042 /* Handle wide chracters. */
1043 if (type == BT_CHARACTER && kind != 1)
1045 nelems *= size;
1046 size = kind;
1049 /* Break up complex into its constituent reals. */
1050 if (type == BT_COMPLEX)
1052 nelems *= 2;
1053 size /= 2;
1056 /* By now, all complex variables have been split into their
1057 constituent reals. */
1059 nrem = nelems;
1062 size_t nc;
1063 if (size * nrem > BSWAP_BUFSZ)
1064 nc = BSWAP_BUFSZ / size;
1065 else
1066 nc = nrem;
1068 bswap_array (buffer, p, size, nc);
1069 write_buf (dtp, buffer, size * nc);
1070 p += size * nc;
1071 nrem -= nc;
1073 while (nrem > 0);
1078 /* Return a pointer to the name of a type. */
1080 const char *
1081 type_name (bt type)
1083 const char *p;
1085 switch (type)
1087 case BT_INTEGER:
1088 p = "INTEGER";
1089 break;
1090 case BT_LOGICAL:
1091 p = "LOGICAL";
1092 break;
1093 case BT_CHARACTER:
1094 p = "CHARACTER";
1095 break;
1096 case BT_REAL:
1097 p = "REAL";
1098 break;
1099 case BT_COMPLEX:
1100 p = "COMPLEX";
1101 break;
1102 default:
1103 internal_error (NULL, "type_name(): Bad type");
1106 return p;
1110 /* Write a constant string to the output.
1111 This is complicated because the string can have doubled delimiters
1112 in it. The length in the format node is the true length. */
1114 static void
1115 write_constant_string (st_parameter_dt *dtp, const fnode *f)
1117 char c, delimiter, *p, *q;
1118 int length;
1120 length = f->u.string.length;
1121 if (length == 0)
1122 return;
1124 p = write_block (dtp, length);
1125 if (p == NULL)
1126 return;
1128 q = f->u.string.p;
1129 delimiter = q[-1];
1131 for (; length > 0; length--)
1133 c = *p++ = *q++;
1134 if (c == delimiter && c != 'H' && c != 'h')
1135 q++; /* Skip the doubled delimiter. */
1140 /* Given actual and expected types in a formatted data transfer, make
1141 sure they agree. If not, an error message is generated. Returns
1142 nonzero if something went wrong. */
1144 static int
1145 require_type (st_parameter_dt *dtp, bt expected, bt actual, const fnode *f)
1147 #define BUFLEN 100
1148 char buffer[BUFLEN];
1150 if (actual == expected)
1151 return 0;
1153 /* Adjust item_count before emitting error message. */
1154 snprintf (buffer, BUFLEN,
1155 "Expected %s for item %d in formatted transfer, got %s",
1156 type_name (expected), dtp->u.p.item_count - 1, type_name (actual));
1158 format_error (dtp, f, buffer);
1159 return 1;
1163 static int
1164 require_numeric_type (st_parameter_dt *dtp, bt actual, const fnode *f)
1166 #define BUFLEN 100
1167 char buffer[BUFLEN];
1169 if (actual == BT_INTEGER || actual == BT_REAL || actual == BT_COMPLEX)
1170 return 0;
1172 /* Adjust item_count before emitting error message. */
1173 snprintf (buffer, BUFLEN,
1174 "Expected numeric type for item %d in formatted transfer, got %s",
1175 dtp->u.p.item_count - 1, type_name (actual));
1177 format_error (dtp, f, buffer);
1178 return 1;
1182 /* This function is in the main loop for a formatted data transfer
1183 statement. It would be natural to implement this as a coroutine
1184 with the user program, but C makes that awkward. We loop,
1185 processing format elements. When we actually have to transfer
1186 data instead of just setting flags, we return control to the user
1187 program which calls a function that supplies the address and type
1188 of the next element, then comes back here to process it. */
1190 static void
1191 formatted_transfer_scalar_read (st_parameter_dt *dtp, bt type, void *p, int kind,
1192 size_t size)
1194 int pos, bytes_used;
1195 const fnode *f;
1196 format_token t;
1197 int n;
1198 int consume_data_flag;
1200 /* Change a complex data item into a pair of reals. */
1202 n = (p == NULL) ? 0 : ((type != BT_COMPLEX) ? 1 : 2);
1203 if (type == BT_COMPLEX)
1205 type = BT_REAL;
1206 size /= 2;
1209 /* If there's an EOR condition, we simulate finalizing the transfer
1210 by doing nothing. */
1211 if (dtp->u.p.eor_condition)
1212 return;
1214 /* Set this flag so that commas in reads cause the read to complete before
1215 the entire field has been read. The next read field will start right after
1216 the comma in the stream. (Set to 0 for character reads). */
1217 dtp->u.p.sf_read_comma =
1218 dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA ? 0 : 1;
1220 for (;;)
1222 /* If reversion has occurred and there is another real data item,
1223 then we have to move to the next record. */
1224 if (dtp->u.p.reversion_flag && n > 0)
1226 dtp->u.p.reversion_flag = 0;
1227 next_record (dtp, 0);
1230 consume_data_flag = 1;
1231 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1232 break;
1234 f = next_format (dtp);
1235 if (f == NULL)
1237 /* No data descriptors left. */
1238 if (unlikely (n > 0))
1239 generate_error (&dtp->common, LIBERROR_FORMAT,
1240 "Insufficient data descriptors in format after reversion");
1241 return;
1244 t = f->format;
1246 bytes_used = (int)(dtp->u.p.current_unit->recl
1247 - dtp->u.p.current_unit->bytes_left);
1249 if (is_stream_io(dtp))
1250 bytes_used = 0;
1252 switch (t)
1254 case FMT_I:
1255 if (n == 0)
1256 goto need_read_data;
1257 if (require_type (dtp, BT_INTEGER, type, f))
1258 return;
1259 read_decimal (dtp, f, p, kind);
1260 break;
1262 case FMT_B:
1263 if (n == 0)
1264 goto need_read_data;
1265 if (!(compile_options.allow_std & GFC_STD_GNU)
1266 && require_numeric_type (dtp, type, f))
1267 return;
1268 if (!(compile_options.allow_std & GFC_STD_F2008)
1269 && require_type (dtp, BT_INTEGER, type, f))
1270 return;
1271 read_radix (dtp, f, p, kind, 2);
1272 break;
1274 case FMT_O:
1275 if (n == 0)
1276 goto need_read_data;
1277 if (!(compile_options.allow_std & GFC_STD_GNU)
1278 && require_numeric_type (dtp, type, f))
1279 return;
1280 if (!(compile_options.allow_std & GFC_STD_F2008)
1281 && require_type (dtp, BT_INTEGER, type, f))
1282 return;
1283 read_radix (dtp, f, p, kind, 8);
1284 break;
1286 case FMT_Z:
1287 if (n == 0)
1288 goto need_read_data;
1289 if (!(compile_options.allow_std & GFC_STD_GNU)
1290 && require_numeric_type (dtp, type, f))
1291 return;
1292 if (!(compile_options.allow_std & GFC_STD_F2008)
1293 && require_type (dtp, BT_INTEGER, type, f))
1294 return;
1295 read_radix (dtp, f, p, kind, 16);
1296 break;
1298 case FMT_A:
1299 if (n == 0)
1300 goto need_read_data;
1302 /* It is possible to have FMT_A with something not BT_CHARACTER such
1303 as when writing out hollerith strings, so check both type
1304 and kind before calling wide character routines. */
1305 if (type == BT_CHARACTER && kind == 4)
1306 read_a_char4 (dtp, f, p, size);
1307 else
1308 read_a (dtp, f, p, size);
1309 break;
1311 case FMT_L:
1312 if (n == 0)
1313 goto need_read_data;
1314 read_l (dtp, f, p, kind);
1315 break;
1317 case FMT_D:
1318 if (n == 0)
1319 goto need_read_data;
1320 if (require_type (dtp, BT_REAL, type, f))
1321 return;
1322 read_f (dtp, f, p, kind);
1323 break;
1325 case FMT_E:
1326 if (n == 0)
1327 goto need_read_data;
1328 if (require_type (dtp, BT_REAL, type, f))
1329 return;
1330 read_f (dtp, f, p, kind);
1331 break;
1333 case FMT_EN:
1334 if (n == 0)
1335 goto need_read_data;
1336 if (require_type (dtp, BT_REAL, type, f))
1337 return;
1338 read_f (dtp, f, p, kind);
1339 break;
1341 case FMT_ES:
1342 if (n == 0)
1343 goto need_read_data;
1344 if (require_type (dtp, BT_REAL, type, f))
1345 return;
1346 read_f (dtp, f, p, kind);
1347 break;
1349 case FMT_F:
1350 if (n == 0)
1351 goto need_read_data;
1352 if (require_type (dtp, BT_REAL, type, f))
1353 return;
1354 read_f (dtp, f, p, kind);
1355 break;
1357 case FMT_G:
1358 if (n == 0)
1359 goto need_read_data;
1360 switch (type)
1362 case BT_INTEGER:
1363 read_decimal (dtp, f, p, kind);
1364 break;
1365 case BT_LOGICAL:
1366 read_l (dtp, f, p, kind);
1367 break;
1368 case BT_CHARACTER:
1369 if (kind == 4)
1370 read_a_char4 (dtp, f, p, size);
1371 else
1372 read_a (dtp, f, p, size);
1373 break;
1374 case BT_REAL:
1375 read_f (dtp, f, p, kind);
1376 break;
1377 default:
1378 internal_error (&dtp->common, "formatted_transfer(): Bad type");
1380 break;
1382 case FMT_STRING:
1383 consume_data_flag = 0;
1384 format_error (dtp, f, "Constant string in input format");
1385 return;
1387 /* Format codes that don't transfer data. */
1388 case FMT_X:
1389 case FMT_TR:
1390 consume_data_flag = 0;
1391 dtp->u.p.skips += f->u.n;
1392 pos = bytes_used + dtp->u.p.skips - 1;
1393 dtp->u.p.pending_spaces = pos - dtp->u.p.max_pos + 1;
1394 read_x (dtp, f->u.n);
1395 break;
1397 case FMT_TL:
1398 case FMT_T:
1399 consume_data_flag = 0;
1401 if (f->format == FMT_TL)
1403 /* Handle the special case when no bytes have been used yet.
1404 Cannot go below zero. */
1405 if (bytes_used == 0)
1407 dtp->u.p.pending_spaces -= f->u.n;
1408 dtp->u.p.skips -= f->u.n;
1409 dtp->u.p.skips = dtp->u.p.skips < 0 ? 0 : dtp->u.p.skips;
1412 pos = bytes_used - f->u.n;
1414 else /* FMT_T */
1415 pos = f->u.n - 1;
1417 /* Standard 10.6.1.1: excessive left tabbing is reset to the
1418 left tab limit. We do not check if the position has gone
1419 beyond the end of record because a subsequent tab could
1420 bring us back again. */
1421 pos = pos < 0 ? 0 : pos;
1423 dtp->u.p.skips = dtp->u.p.skips + pos - bytes_used;
1424 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces
1425 + pos - dtp->u.p.max_pos;
1426 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces < 0
1427 ? 0 : dtp->u.p.pending_spaces;
1428 if (dtp->u.p.skips == 0)
1429 break;
1431 /* Adjust everything for end-of-record condition */
1432 if (dtp->u.p.sf_seen_eor && !is_internal_unit (dtp))
1434 dtp->u.p.current_unit->bytes_left -= dtp->u.p.sf_seen_eor;
1435 dtp->u.p.skips -= dtp->u.p.sf_seen_eor;
1436 bytes_used = pos;
1437 dtp->u.p.sf_seen_eor = 0;
1439 if (dtp->u.p.skips < 0)
1441 if (is_internal_unit (dtp))
1442 sseek (dtp->u.p.current_unit->s, dtp->u.p.skips, SEEK_CUR);
1443 else
1444 fbuf_seek (dtp->u.p.current_unit, dtp->u.p.skips, SEEK_CUR);
1445 dtp->u.p.current_unit->bytes_left -= (gfc_offset) dtp->u.p.skips;
1446 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1448 else
1449 read_x (dtp, dtp->u.p.skips);
1450 break;
1452 case FMT_S:
1453 consume_data_flag = 0;
1454 dtp->u.p.sign_status = SIGN_S;
1455 break;
1457 case FMT_SS:
1458 consume_data_flag = 0;
1459 dtp->u.p.sign_status = SIGN_SS;
1460 break;
1462 case FMT_SP:
1463 consume_data_flag = 0;
1464 dtp->u.p.sign_status = SIGN_SP;
1465 break;
1467 case FMT_BN:
1468 consume_data_flag = 0 ;
1469 dtp->u.p.blank_status = BLANK_NULL;
1470 break;
1472 case FMT_BZ:
1473 consume_data_flag = 0;
1474 dtp->u.p.blank_status = BLANK_ZERO;
1475 break;
1477 case FMT_DC:
1478 consume_data_flag = 0;
1479 dtp->u.p.current_unit->decimal_status = DECIMAL_COMMA;
1480 break;
1482 case FMT_DP:
1483 consume_data_flag = 0;
1484 dtp->u.p.current_unit->decimal_status = DECIMAL_POINT;
1485 break;
1487 case FMT_RC:
1488 consume_data_flag = 0;
1489 dtp->u.p.current_unit->round_status = ROUND_COMPATIBLE;
1490 break;
1492 case FMT_RD:
1493 consume_data_flag = 0;
1494 dtp->u.p.current_unit->round_status = ROUND_DOWN;
1495 break;
1497 case FMT_RN:
1498 consume_data_flag = 0;
1499 dtp->u.p.current_unit->round_status = ROUND_NEAREST;
1500 break;
1502 case FMT_RP:
1503 consume_data_flag = 0;
1504 dtp->u.p.current_unit->round_status = ROUND_PROCDEFINED;
1505 break;
1507 case FMT_RU:
1508 consume_data_flag = 0;
1509 dtp->u.p.current_unit->round_status = ROUND_UP;
1510 break;
1512 case FMT_RZ:
1513 consume_data_flag = 0;
1514 dtp->u.p.current_unit->round_status = ROUND_ZERO;
1515 break;
1517 case FMT_P:
1518 consume_data_flag = 0;
1519 dtp->u.p.scale_factor = f->u.k;
1520 break;
1522 case FMT_DOLLAR:
1523 consume_data_flag = 0;
1524 dtp->u.p.seen_dollar = 1;
1525 break;
1527 case FMT_SLASH:
1528 consume_data_flag = 0;
1529 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1530 next_record (dtp, 0);
1531 break;
1533 case FMT_COLON:
1534 /* A colon descriptor causes us to exit this loop (in
1535 particular preventing another / descriptor from being
1536 processed) unless there is another data item to be
1537 transferred. */
1538 consume_data_flag = 0;
1539 if (n == 0)
1540 return;
1541 break;
1543 default:
1544 internal_error (&dtp->common, "Bad format node");
1547 /* Adjust the item count and data pointer. */
1549 if ((consume_data_flag > 0) && (n > 0))
1551 n--;
1552 p = ((char *) p) + size;
1555 dtp->u.p.skips = 0;
1557 pos = (int)(dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left);
1558 dtp->u.p.max_pos = (dtp->u.p.max_pos > pos) ? dtp->u.p.max_pos : pos;
1561 return;
1563 /* Come here when we need a data descriptor but don't have one. We
1564 push the current format node back onto the input, then return and
1565 let the user program call us back with the data. */
1566 need_read_data:
1567 unget_format (dtp, f);
1571 static void
1572 formatted_transfer_scalar_write (st_parameter_dt *dtp, bt type, void *p, int kind,
1573 size_t size)
1575 int pos, bytes_used;
1576 const fnode *f;
1577 format_token t;
1578 int n;
1579 int consume_data_flag;
1581 /* Change a complex data item into a pair of reals. */
1583 n = (p == NULL) ? 0 : ((type != BT_COMPLEX) ? 1 : 2);
1584 if (type == BT_COMPLEX)
1586 type = BT_REAL;
1587 size /= 2;
1590 /* If there's an EOR condition, we simulate finalizing the transfer
1591 by doing nothing. */
1592 if (dtp->u.p.eor_condition)
1593 return;
1595 /* Set this flag so that commas in reads cause the read to complete before
1596 the entire field has been read. The next read field will start right after
1597 the comma in the stream. (Set to 0 for character reads). */
1598 dtp->u.p.sf_read_comma =
1599 dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA ? 0 : 1;
1601 for (;;)
1603 /* If reversion has occurred and there is another real data item,
1604 then we have to move to the next record. */
1605 if (dtp->u.p.reversion_flag && n > 0)
1607 dtp->u.p.reversion_flag = 0;
1608 next_record (dtp, 0);
1611 consume_data_flag = 1;
1612 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1613 break;
1615 f = next_format (dtp);
1616 if (f == NULL)
1618 /* No data descriptors left. */
1619 if (unlikely (n > 0))
1620 generate_error (&dtp->common, LIBERROR_FORMAT,
1621 "Insufficient data descriptors in format after reversion");
1622 return;
1625 /* Now discharge T, TR and X movements to the right. This is delayed
1626 until a data producing format to suppress trailing spaces. */
1628 t = f->format;
1629 if (dtp->u.p.mode == WRITING && dtp->u.p.skips != 0
1630 && ((n>0 && ( t == FMT_I || t == FMT_B || t == FMT_O
1631 || t == FMT_Z || t == FMT_F || t == FMT_E
1632 || t == FMT_EN || t == FMT_ES || t == FMT_G
1633 || t == FMT_L || t == FMT_A || t == FMT_D))
1634 || t == FMT_STRING))
1636 if (dtp->u.p.skips > 0)
1638 int tmp;
1639 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
1640 tmp = (int)(dtp->u.p.current_unit->recl
1641 - dtp->u.p.current_unit->bytes_left);
1642 dtp->u.p.max_pos =
1643 dtp->u.p.max_pos > tmp ? dtp->u.p.max_pos : tmp;
1645 if (dtp->u.p.skips < 0)
1647 if (is_internal_unit (dtp))
1648 sseek (dtp->u.p.current_unit->s, dtp->u.p.skips, SEEK_CUR);
1649 else
1650 fbuf_seek (dtp->u.p.current_unit, dtp->u.p.skips, SEEK_CUR);
1651 dtp->u.p.current_unit->bytes_left -= (gfc_offset) dtp->u.p.skips;
1653 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1656 bytes_used = (int)(dtp->u.p.current_unit->recl
1657 - dtp->u.p.current_unit->bytes_left);
1659 if (is_stream_io(dtp))
1660 bytes_used = 0;
1662 switch (t)
1664 case FMT_I:
1665 if (n == 0)
1666 goto need_data;
1667 if (require_type (dtp, BT_INTEGER, type, f))
1668 return;
1669 write_i (dtp, f, p, kind);
1670 break;
1672 case FMT_B:
1673 if (n == 0)
1674 goto need_data;
1675 if (!(compile_options.allow_std & GFC_STD_GNU)
1676 && require_numeric_type (dtp, type, f))
1677 return;
1678 if (!(compile_options.allow_std & GFC_STD_F2008)
1679 && require_type (dtp, BT_INTEGER, type, f))
1680 return;
1681 write_b (dtp, f, p, kind);
1682 break;
1684 case FMT_O:
1685 if (n == 0)
1686 goto need_data;
1687 if (!(compile_options.allow_std & GFC_STD_GNU)
1688 && require_numeric_type (dtp, type, f))
1689 return;
1690 if (!(compile_options.allow_std & GFC_STD_F2008)
1691 && require_type (dtp, BT_INTEGER, type, f))
1692 return;
1693 write_o (dtp, f, p, kind);
1694 break;
1696 case FMT_Z:
1697 if (n == 0)
1698 goto need_data;
1699 if (!(compile_options.allow_std & GFC_STD_GNU)
1700 && require_numeric_type (dtp, type, f))
1701 return;
1702 if (!(compile_options.allow_std & GFC_STD_F2008)
1703 && require_type (dtp, BT_INTEGER, type, f))
1704 return;
1705 write_z (dtp, f, p, kind);
1706 break;
1708 case FMT_A:
1709 if (n == 0)
1710 goto need_data;
1712 /* It is possible to have FMT_A with something not BT_CHARACTER such
1713 as when writing out hollerith strings, so check both type
1714 and kind before calling wide character routines. */
1715 if (type == BT_CHARACTER && kind == 4)
1716 write_a_char4 (dtp, f, p, size);
1717 else
1718 write_a (dtp, f, p, size);
1719 break;
1721 case FMT_L:
1722 if (n == 0)
1723 goto need_data;
1724 write_l (dtp, f, p, kind);
1725 break;
1727 case FMT_D:
1728 if (n == 0)
1729 goto need_data;
1730 if (require_type (dtp, BT_REAL, type, f))
1731 return;
1732 write_d (dtp, f, p, kind);
1733 break;
1735 case FMT_E:
1736 if (n == 0)
1737 goto need_data;
1738 if (require_type (dtp, BT_REAL, type, f))
1739 return;
1740 write_e (dtp, f, p, kind);
1741 break;
1743 case FMT_EN:
1744 if (n == 0)
1745 goto need_data;
1746 if (require_type (dtp, BT_REAL, type, f))
1747 return;
1748 write_en (dtp, f, p, kind);
1749 break;
1751 case FMT_ES:
1752 if (n == 0)
1753 goto need_data;
1754 if (require_type (dtp, BT_REAL, type, f))
1755 return;
1756 write_es (dtp, f, p, kind);
1757 break;
1759 case FMT_F:
1760 if (n == 0)
1761 goto need_data;
1762 if (require_type (dtp, BT_REAL, type, f))
1763 return;
1764 write_f (dtp, f, p, kind);
1765 break;
1767 case FMT_G:
1768 if (n == 0)
1769 goto need_data;
1770 switch (type)
1772 case BT_INTEGER:
1773 write_i (dtp, f, p, kind);
1774 break;
1775 case BT_LOGICAL:
1776 write_l (dtp, f, p, kind);
1777 break;
1778 case BT_CHARACTER:
1779 if (kind == 4)
1780 write_a_char4 (dtp, f, p, size);
1781 else
1782 write_a (dtp, f, p, size);
1783 break;
1784 case BT_REAL:
1785 if (f->u.real.w == 0)
1786 write_real_g0 (dtp, p, kind, f->u.real.d);
1787 else
1788 write_d (dtp, f, p, kind);
1789 break;
1790 default:
1791 internal_error (&dtp->common,
1792 "formatted_transfer(): Bad type");
1794 break;
1796 case FMT_STRING:
1797 consume_data_flag = 0;
1798 write_constant_string (dtp, f);
1799 break;
1801 /* Format codes that don't transfer data. */
1802 case FMT_X:
1803 case FMT_TR:
1804 consume_data_flag = 0;
1806 dtp->u.p.skips += f->u.n;
1807 pos = bytes_used + dtp->u.p.skips - 1;
1808 dtp->u.p.pending_spaces = pos - dtp->u.p.max_pos + 1;
1809 /* Writes occur just before the switch on f->format, above, so
1810 that trailing blanks are suppressed, unless we are doing a
1811 non-advancing write in which case we want to output the blanks
1812 now. */
1813 if (dtp->u.p.advance_status == ADVANCE_NO)
1815 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
1816 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1818 break;
1820 case FMT_TL:
1821 case FMT_T:
1822 consume_data_flag = 0;
1824 if (f->format == FMT_TL)
1827 /* Handle the special case when no bytes have been used yet.
1828 Cannot go below zero. */
1829 if (bytes_used == 0)
1831 dtp->u.p.pending_spaces -= f->u.n;
1832 dtp->u.p.skips -= f->u.n;
1833 dtp->u.p.skips = dtp->u.p.skips < 0 ? 0 : dtp->u.p.skips;
1836 pos = bytes_used - f->u.n;
1838 else /* FMT_T */
1839 pos = f->u.n - dtp->u.p.pending_spaces - 1;
1841 /* Standard 10.6.1.1: excessive left tabbing is reset to the
1842 left tab limit. We do not check if the position has gone
1843 beyond the end of record because a subsequent tab could
1844 bring us back again. */
1845 pos = pos < 0 ? 0 : pos;
1847 dtp->u.p.skips = dtp->u.p.skips + pos - bytes_used;
1848 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces
1849 + pos - dtp->u.p.max_pos;
1850 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces < 0
1851 ? 0 : dtp->u.p.pending_spaces;
1852 break;
1854 case FMT_S:
1855 consume_data_flag = 0;
1856 dtp->u.p.sign_status = SIGN_S;
1857 break;
1859 case FMT_SS:
1860 consume_data_flag = 0;
1861 dtp->u.p.sign_status = SIGN_SS;
1862 break;
1864 case FMT_SP:
1865 consume_data_flag = 0;
1866 dtp->u.p.sign_status = SIGN_SP;
1867 break;
1869 case FMT_BN:
1870 consume_data_flag = 0 ;
1871 dtp->u.p.blank_status = BLANK_NULL;
1872 break;
1874 case FMT_BZ:
1875 consume_data_flag = 0;
1876 dtp->u.p.blank_status = BLANK_ZERO;
1877 break;
1879 case FMT_DC:
1880 consume_data_flag = 0;
1881 dtp->u.p.current_unit->decimal_status = DECIMAL_COMMA;
1882 break;
1884 case FMT_DP:
1885 consume_data_flag = 0;
1886 dtp->u.p.current_unit->decimal_status = DECIMAL_POINT;
1887 break;
1889 case FMT_RC:
1890 consume_data_flag = 0;
1891 dtp->u.p.current_unit->round_status = ROUND_COMPATIBLE;
1892 break;
1894 case FMT_RD:
1895 consume_data_flag = 0;
1896 dtp->u.p.current_unit->round_status = ROUND_DOWN;
1897 break;
1899 case FMT_RN:
1900 consume_data_flag = 0;
1901 dtp->u.p.current_unit->round_status = ROUND_NEAREST;
1902 break;
1904 case FMT_RP:
1905 consume_data_flag = 0;
1906 dtp->u.p.current_unit->round_status = ROUND_PROCDEFINED;
1907 break;
1909 case FMT_RU:
1910 consume_data_flag = 0;
1911 dtp->u.p.current_unit->round_status = ROUND_UP;
1912 break;
1914 case FMT_RZ:
1915 consume_data_flag = 0;
1916 dtp->u.p.current_unit->round_status = ROUND_ZERO;
1917 break;
1919 case FMT_P:
1920 consume_data_flag = 0;
1921 dtp->u.p.scale_factor = f->u.k;
1922 break;
1924 case FMT_DOLLAR:
1925 consume_data_flag = 0;
1926 dtp->u.p.seen_dollar = 1;
1927 break;
1929 case FMT_SLASH:
1930 consume_data_flag = 0;
1931 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1932 next_record (dtp, 0);
1933 break;
1935 case FMT_COLON:
1936 /* A colon descriptor causes us to exit this loop (in
1937 particular preventing another / descriptor from being
1938 processed) unless there is another data item to be
1939 transferred. */
1940 consume_data_flag = 0;
1941 if (n == 0)
1942 return;
1943 break;
1945 default:
1946 internal_error (&dtp->common, "Bad format node");
1949 /* Adjust the item count and data pointer. */
1951 if ((consume_data_flag > 0) && (n > 0))
1953 n--;
1954 p = ((char *) p) + size;
1957 pos = (int)(dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left);
1958 dtp->u.p.max_pos = (dtp->u.p.max_pos > pos) ? dtp->u.p.max_pos : pos;
1961 return;
1963 /* Come here when we need a data descriptor but don't have one. We
1964 push the current format node back onto the input, then return and
1965 let the user program call us back with the data. */
1966 need_data:
1967 unget_format (dtp, f);
1970 /* This function is first called from data_init_transfer to initiate the loop
1971 over each item in the format, transferring data as required. Subsequent
1972 calls to this function occur for each data item foound in the READ/WRITE
1973 statement. The item_count is incremented for each call. Since the first
1974 call is from data_transfer_init, the item_count is always one greater than
1975 the actual count number of the item being transferred. */
1977 static void
1978 formatted_transfer (st_parameter_dt *dtp, bt type, void *p, int kind,
1979 size_t size, size_t nelems)
1981 size_t elem;
1982 char *tmp;
1984 tmp = (char *) p;
1985 size_t stride = type == BT_CHARACTER ?
1986 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
1987 if (dtp->u.p.mode == READING)
1989 /* Big loop over all the elements. */
1990 for (elem = 0; elem < nelems; elem++)
1992 dtp->u.p.item_count++;
1993 formatted_transfer_scalar_read (dtp, type, tmp + stride*elem, kind, size);
1996 else
1998 /* Big loop over all the elements. */
1999 for (elem = 0; elem < nelems; elem++)
2001 dtp->u.p.item_count++;
2002 formatted_transfer_scalar_write (dtp, type, tmp + stride*elem, kind, size);
2008 /* Data transfer entry points. The type of the data entity is
2009 implicit in the subroutine call. This prevents us from having to
2010 share a common enum with the compiler. */
2012 void
2013 transfer_integer (st_parameter_dt *dtp, void *p, int kind)
2015 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2016 return;
2017 dtp->u.p.transfer (dtp, BT_INTEGER, p, kind, kind, 1);
2020 void
2021 transfer_integer_write (st_parameter_dt *dtp, void *p, int kind)
2023 transfer_integer (dtp, p, kind);
2026 void
2027 transfer_real (st_parameter_dt *dtp, void *p, int kind)
2029 size_t size;
2030 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2031 return;
2032 size = size_from_real_kind (kind);
2033 dtp->u.p.transfer (dtp, BT_REAL, p, kind, size, 1);
2036 void
2037 transfer_real_write (st_parameter_dt *dtp, void *p, int kind)
2039 transfer_real (dtp, p, kind);
2042 void
2043 transfer_logical (st_parameter_dt *dtp, void *p, int kind)
2045 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2046 return;
2047 dtp->u.p.transfer (dtp, BT_LOGICAL, p, kind, kind, 1);
2050 void
2051 transfer_logical_write (st_parameter_dt *dtp, void *p, int kind)
2053 transfer_logical (dtp, p, kind);
2056 void
2057 transfer_character (st_parameter_dt *dtp, void *p, int len)
2059 static char *empty_string[0];
2061 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2062 return;
2064 /* Strings of zero length can have p == NULL, which confuses the
2065 transfer routines into thinking we need more data elements. To avoid
2066 this, we give them a nice pointer. */
2067 if (len == 0 && p == NULL)
2068 p = empty_string;
2070 /* Set kind here to 1. */
2071 dtp->u.p.transfer (dtp, BT_CHARACTER, p, 1, len, 1);
2074 void
2075 transfer_character_write (st_parameter_dt *dtp, void *p, int len)
2077 transfer_character (dtp, p, len);
2080 void
2081 transfer_character_wide (st_parameter_dt *dtp, void *p, int len, int kind)
2083 static char *empty_string[0];
2085 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2086 return;
2088 /* Strings of zero length can have p == NULL, which confuses the
2089 transfer routines into thinking we need more data elements. To avoid
2090 this, we give them a nice pointer. */
2091 if (len == 0 && p == NULL)
2092 p = empty_string;
2094 /* Here we pass the actual kind value. */
2095 dtp->u.p.transfer (dtp, BT_CHARACTER, p, kind, len, 1);
2098 void
2099 transfer_character_wide_write (st_parameter_dt *dtp, void *p, int len, int kind)
2101 transfer_character_wide (dtp, p, len, kind);
2104 void
2105 transfer_complex (st_parameter_dt *dtp, void *p, int kind)
2107 size_t size;
2108 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2109 return;
2110 size = size_from_complex_kind (kind);
2111 dtp->u.p.transfer (dtp, BT_COMPLEX, p, kind, size, 1);
2114 void
2115 transfer_complex_write (st_parameter_dt *dtp, void *p, int kind)
2117 transfer_complex (dtp, p, kind);
2120 void
2121 transfer_array (st_parameter_dt *dtp, gfc_array_char *desc, int kind,
2122 gfc_charlen_type charlen)
2124 index_type count[GFC_MAX_DIMENSIONS];
2125 index_type extent[GFC_MAX_DIMENSIONS];
2126 index_type stride[GFC_MAX_DIMENSIONS];
2127 index_type stride0, rank, size, n;
2128 size_t tsize;
2129 char *data;
2130 bt iotype;
2132 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2133 return;
2135 iotype = (bt) GFC_DESCRIPTOR_TYPE (desc);
2136 size = iotype == BT_CHARACTER ? charlen : GFC_DESCRIPTOR_SIZE (desc);
2138 rank = GFC_DESCRIPTOR_RANK (desc);
2139 for (n = 0; n < rank; n++)
2141 count[n] = 0;
2142 stride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(desc,n);
2143 extent[n] = GFC_DESCRIPTOR_EXTENT(desc,n);
2145 /* If the extent of even one dimension is zero, then the entire
2146 array section contains zero elements, so we return after writing
2147 a zero array record. */
2148 if (extent[n] <= 0)
2150 data = NULL;
2151 tsize = 0;
2152 dtp->u.p.transfer (dtp, iotype, data, kind, size, tsize);
2153 return;
2157 stride0 = stride[0];
2159 /* If the innermost dimension has a stride of 1, we can do the transfer
2160 in contiguous chunks. */
2161 if (stride0 == size)
2162 tsize = extent[0];
2163 else
2164 tsize = 1;
2166 data = GFC_DESCRIPTOR_DATA (desc);
2168 while (data)
2170 dtp->u.p.transfer (dtp, iotype, data, kind, size, tsize);
2171 data += stride0 * tsize;
2172 count[0] += tsize;
2173 n = 0;
2174 while (count[n] == extent[n])
2176 count[n] = 0;
2177 data -= stride[n] * extent[n];
2178 n++;
2179 if (n == rank)
2181 data = NULL;
2182 break;
2184 else
2186 count[n]++;
2187 data += stride[n];
2193 void
2194 transfer_array_write (st_parameter_dt *dtp, gfc_array_char *desc, int kind,
2195 gfc_charlen_type charlen)
2197 transfer_array (dtp, desc, kind, charlen);
2200 /* Preposition a sequential unformatted file while reading. */
2202 static void
2203 us_read (st_parameter_dt *dtp, int continued)
2205 ssize_t n, nr;
2206 GFC_INTEGER_4 i4;
2207 GFC_INTEGER_8 i8;
2208 gfc_offset i;
2210 if (compile_options.record_marker == 0)
2211 n = sizeof (GFC_INTEGER_4);
2212 else
2213 n = compile_options.record_marker;
2215 nr = sread (dtp->u.p.current_unit->s, &i, n);
2216 if (unlikely (nr < 0))
2218 generate_error (&dtp->common, LIBERROR_BAD_US, NULL);
2219 return;
2221 else if (nr == 0)
2223 hit_eof (dtp);
2224 return; /* end of file */
2226 else if (unlikely (n != nr))
2228 generate_error (&dtp->common, LIBERROR_BAD_US, NULL);
2229 return;
2232 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
2233 if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE))
2235 switch (nr)
2237 case sizeof(GFC_INTEGER_4):
2238 memcpy (&i4, &i, sizeof (i4));
2239 i = i4;
2240 break;
2242 case sizeof(GFC_INTEGER_8):
2243 memcpy (&i8, &i, sizeof (i8));
2244 i = i8;
2245 break;
2247 default:
2248 runtime_error ("Illegal value for record marker");
2249 break;
2252 else
2254 uint32_t u32;
2255 uint64_t u64;
2256 switch (nr)
2258 case sizeof(GFC_INTEGER_4):
2259 memcpy (&u32, &i, sizeof (u32));
2260 u32 = __builtin_bswap32 (u32);
2261 memcpy (&i4, &u32, sizeof (i4));
2262 i = i4;
2263 break;
2265 case sizeof(GFC_INTEGER_8):
2266 memcpy (&u64, &i, sizeof (u64));
2267 u64 = __builtin_bswap64 (u64);
2268 memcpy (&i8, &u64, sizeof (i8));
2269 i = i8;
2270 break;
2272 default:
2273 runtime_error ("Illegal value for record marker");
2274 break;
2278 if (i >= 0)
2280 dtp->u.p.current_unit->bytes_left_subrecord = i;
2281 dtp->u.p.current_unit->continued = 0;
2283 else
2285 dtp->u.p.current_unit->bytes_left_subrecord = -i;
2286 dtp->u.p.current_unit->continued = 1;
2289 if (! continued)
2290 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2294 /* Preposition a sequential unformatted file while writing. This
2295 amount to writing a bogus length that will be filled in later. */
2297 static void
2298 us_write (st_parameter_dt *dtp, int continued)
2300 ssize_t nbytes;
2301 gfc_offset dummy;
2303 dummy = 0;
2305 if (compile_options.record_marker == 0)
2306 nbytes = sizeof (GFC_INTEGER_4);
2307 else
2308 nbytes = compile_options.record_marker ;
2310 if (swrite (dtp->u.p.current_unit->s, &dummy, nbytes) != nbytes)
2311 generate_error (&dtp->common, LIBERROR_OS, NULL);
2313 /* For sequential unformatted, if RECL= was not specified in the OPEN
2314 we write until we have more bytes than can fit in the subrecord
2315 markers, then we write a new subrecord. */
2317 dtp->u.p.current_unit->bytes_left_subrecord =
2318 dtp->u.p.current_unit->recl_subrecord;
2319 dtp->u.p.current_unit->continued = continued;
2323 /* Position to the next record prior to transfer. We are assumed to
2324 be before the next record. We also calculate the bytes in the next
2325 record. */
2327 static void
2328 pre_position (st_parameter_dt *dtp)
2330 if (dtp->u.p.current_unit->current_record)
2331 return; /* Already positioned. */
2333 switch (current_mode (dtp))
2335 case FORMATTED_STREAM:
2336 case UNFORMATTED_STREAM:
2337 /* There are no records with stream I/O. If the position was specified
2338 data_transfer_init has already positioned the file. If no position
2339 was specified, we continue from where we last left off. I.e.
2340 there is nothing to do here. */
2341 break;
2343 case UNFORMATTED_SEQUENTIAL:
2344 if (dtp->u.p.mode == READING)
2345 us_read (dtp, 0);
2346 else
2347 us_write (dtp, 0);
2349 break;
2351 case FORMATTED_SEQUENTIAL:
2352 case FORMATTED_DIRECT:
2353 case UNFORMATTED_DIRECT:
2354 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2355 break;
2358 dtp->u.p.current_unit->current_record = 1;
2362 /* Initialize things for a data transfer. This code is common for
2363 both reading and writing. */
2365 static void
2366 data_transfer_init (st_parameter_dt *dtp, int read_flag)
2368 unit_flags u_flags; /* Used for creating a unit if needed. */
2369 GFC_INTEGER_4 cf = dtp->common.flags;
2370 namelist_info *ionml;
2372 ionml = ((cf & IOPARM_DT_IONML_SET) != 0) ? dtp->u.p.ionml : NULL;
2374 memset (&dtp->u.p, 0, sizeof (dtp->u.p));
2376 dtp->u.p.ionml = ionml;
2377 dtp->u.p.mode = read_flag ? READING : WRITING;
2379 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2380 return;
2382 if ((cf & IOPARM_DT_HAS_SIZE) != 0)
2383 dtp->u.p.size_used = 0; /* Initialize the count. */
2385 dtp->u.p.current_unit = get_unit (dtp, 1);
2386 if (dtp->u.p.current_unit->s == NULL)
2387 { /* Open the unit with some default flags. */
2388 st_parameter_open opp;
2389 unit_convert conv;
2391 if (dtp->common.unit < 0)
2393 close_unit (dtp->u.p.current_unit);
2394 dtp->u.p.current_unit = NULL;
2395 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2396 "Bad unit number in statement");
2397 return;
2399 memset (&u_flags, '\0', sizeof (u_flags));
2400 u_flags.access = ACCESS_SEQUENTIAL;
2401 u_flags.action = ACTION_READWRITE;
2403 /* Is it unformatted? */
2404 if (!(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT
2405 | IOPARM_DT_IONML_SET)))
2406 u_flags.form = FORM_UNFORMATTED;
2407 else
2408 u_flags.form = FORM_UNSPECIFIED;
2410 u_flags.delim = DELIM_UNSPECIFIED;
2411 u_flags.blank = BLANK_UNSPECIFIED;
2412 u_flags.pad = PAD_UNSPECIFIED;
2413 u_flags.decimal = DECIMAL_UNSPECIFIED;
2414 u_flags.encoding = ENCODING_UNSPECIFIED;
2415 u_flags.async = ASYNC_UNSPECIFIED;
2416 u_flags.round = ROUND_UNSPECIFIED;
2417 u_flags.sign = SIGN_UNSPECIFIED;
2419 u_flags.status = STATUS_UNKNOWN;
2421 conv = get_unformatted_convert (dtp->common.unit);
2423 if (conv == GFC_CONVERT_NONE)
2424 conv = compile_options.convert;
2426 /* We use big_endian, which is 0 on little-endian machines
2427 and 1 on big-endian machines. */
2428 switch (conv)
2430 case GFC_CONVERT_NATIVE:
2431 case GFC_CONVERT_SWAP:
2432 break;
2434 case GFC_CONVERT_BIG:
2435 conv = big_endian ? GFC_CONVERT_NATIVE : GFC_CONVERT_SWAP;
2436 break;
2438 case GFC_CONVERT_LITTLE:
2439 conv = big_endian ? GFC_CONVERT_SWAP : GFC_CONVERT_NATIVE;
2440 break;
2442 default:
2443 internal_error (&opp.common, "Illegal value for CONVERT");
2444 break;
2447 u_flags.convert = conv;
2449 opp.common = dtp->common;
2450 opp.common.flags &= IOPARM_COMMON_MASK;
2451 dtp->u.p.current_unit = new_unit (&opp, dtp->u.p.current_unit, &u_flags);
2452 dtp->common.flags &= ~IOPARM_COMMON_MASK;
2453 dtp->common.flags |= (opp.common.flags & IOPARM_COMMON_MASK);
2454 if (dtp->u.p.current_unit == NULL)
2455 return;
2458 /* Check the action. */
2460 if (read_flag && dtp->u.p.current_unit->flags.action == ACTION_WRITE)
2462 generate_error (&dtp->common, LIBERROR_BAD_ACTION,
2463 "Cannot read from file opened for WRITE");
2464 return;
2467 if (!read_flag && dtp->u.p.current_unit->flags.action == ACTION_READ)
2469 generate_error (&dtp->common, LIBERROR_BAD_ACTION,
2470 "Cannot write to file opened for READ");
2471 return;
2474 dtp->u.p.first_item = 1;
2476 /* Check the format. */
2478 if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
2479 parse_format (dtp);
2481 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED
2482 && (cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
2483 != 0)
2485 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2486 "Format present for UNFORMATTED data transfer");
2487 return;
2490 if ((cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0 && dtp->u.p.ionml != NULL)
2492 if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
2494 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2495 "A format cannot be specified with a namelist");
2496 return;
2499 else if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
2500 !(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT)))
2502 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2503 "Missing format for FORMATTED data transfer");
2504 return;
2507 if (is_internal_unit (dtp)
2508 && dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
2510 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2511 "Internal file cannot be accessed by UNFORMATTED "
2512 "data transfer");
2513 return;
2516 /* Check the record or position number. */
2518 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT
2519 && (cf & IOPARM_DT_HAS_REC) == 0)
2521 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
2522 "Direct access data transfer requires record number");
2523 return;
2526 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
2528 if ((cf & IOPARM_DT_HAS_REC) != 0)
2530 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2531 "Record number not allowed for sequential access "
2532 "data transfer");
2533 return;
2536 if (dtp->u.p.current_unit->endfile == AFTER_ENDFILE)
2538 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2539 "Sequential READ or WRITE not allowed after "
2540 "EOF marker, possibly use REWIND or BACKSPACE");
2541 return;
2545 /* Process the ADVANCE option. */
2547 dtp->u.p.advance_status
2548 = !(cf & IOPARM_DT_HAS_ADVANCE) ? ADVANCE_UNSPECIFIED :
2549 find_option (&dtp->common, dtp->advance, dtp->advance_len, advance_opt,
2550 "Bad ADVANCE parameter in data transfer statement");
2552 if (dtp->u.p.advance_status != ADVANCE_UNSPECIFIED)
2554 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
2556 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2557 "ADVANCE specification conflicts with sequential "
2558 "access");
2559 return;
2562 if (is_internal_unit (dtp))
2564 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2565 "ADVANCE specification conflicts with internal file");
2566 return;
2569 if ((cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
2570 != IOPARM_DT_HAS_FORMAT)
2572 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2573 "ADVANCE specification requires an explicit format");
2574 return;
2578 if (read_flag)
2580 dtp->u.p.current_unit->previous_nonadvancing_write = 0;
2582 if ((cf & IOPARM_EOR) != 0 && dtp->u.p.advance_status != ADVANCE_NO)
2584 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
2585 "EOR specification requires an ADVANCE specification "
2586 "of NO");
2587 return;
2590 if ((cf & IOPARM_DT_HAS_SIZE) != 0
2591 && dtp->u.p.advance_status != ADVANCE_NO)
2593 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
2594 "SIZE specification requires an ADVANCE "
2595 "specification of NO");
2596 return;
2599 else
2600 { /* Write constraints. */
2601 if ((cf & IOPARM_END) != 0)
2603 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2604 "END specification cannot appear in a write "
2605 "statement");
2606 return;
2609 if ((cf & IOPARM_EOR) != 0)
2611 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2612 "EOR specification cannot appear in a write "
2613 "statement");
2614 return;
2617 if ((cf & IOPARM_DT_HAS_SIZE) != 0)
2619 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2620 "SIZE specification cannot appear in a write "
2621 "statement");
2622 return;
2626 if (dtp->u.p.advance_status == ADVANCE_UNSPECIFIED)
2627 dtp->u.p.advance_status = ADVANCE_YES;
2629 /* Check the decimal mode. */
2630 dtp->u.p.current_unit->decimal_status
2631 = !(cf & IOPARM_DT_HAS_DECIMAL) ? DECIMAL_UNSPECIFIED :
2632 find_option (&dtp->common, dtp->decimal, dtp->decimal_len,
2633 decimal_opt, "Bad DECIMAL parameter in data transfer "
2634 "statement");
2636 if (dtp->u.p.current_unit->decimal_status == DECIMAL_UNSPECIFIED)
2637 dtp->u.p.current_unit->decimal_status = dtp->u.p.current_unit->flags.decimal;
2639 /* Check the round mode. */
2640 dtp->u.p.current_unit->round_status
2641 = !(cf & IOPARM_DT_HAS_ROUND) ? ROUND_UNSPECIFIED :
2642 find_option (&dtp->common, dtp->round, dtp->round_len,
2643 round_opt, "Bad ROUND parameter in data transfer "
2644 "statement");
2646 if (dtp->u.p.current_unit->round_status == ROUND_UNSPECIFIED)
2647 dtp->u.p.current_unit->round_status = dtp->u.p.current_unit->flags.round;
2649 /* Check the sign mode. */
2650 dtp->u.p.sign_status
2651 = !(cf & IOPARM_DT_HAS_SIGN) ? SIGN_UNSPECIFIED :
2652 find_option (&dtp->common, dtp->sign, dtp->sign_len, sign_opt,
2653 "Bad SIGN parameter in data transfer statement");
2655 if (dtp->u.p.sign_status == SIGN_UNSPECIFIED)
2656 dtp->u.p.sign_status = dtp->u.p.current_unit->flags.sign;
2658 /* Check the blank mode. */
2659 dtp->u.p.blank_status
2660 = !(cf & IOPARM_DT_HAS_BLANK) ? BLANK_UNSPECIFIED :
2661 find_option (&dtp->common, dtp->blank, dtp->blank_len,
2662 blank_opt,
2663 "Bad BLANK parameter in data transfer statement");
2665 if (dtp->u.p.blank_status == BLANK_UNSPECIFIED)
2666 dtp->u.p.blank_status = dtp->u.p.current_unit->flags.blank;
2668 /* Check the delim mode. */
2669 dtp->u.p.current_unit->delim_status
2670 = !(cf & IOPARM_DT_HAS_DELIM) ? DELIM_UNSPECIFIED :
2671 find_option (&dtp->common, dtp->delim, dtp->delim_len,
2672 delim_opt, "Bad DELIM parameter in data transfer statement");
2674 if (dtp->u.p.current_unit->delim_status == DELIM_UNSPECIFIED)
2676 if (ionml && dtp->u.p.current_unit->flags.delim == DELIM_UNSPECIFIED)
2677 dtp->u.p.current_unit->delim_status =
2678 compile_options.allow_std & GFC_STD_GNU ? DELIM_QUOTE : DELIM_NONE;
2679 else
2680 dtp->u.p.current_unit->delim_status = dtp->u.p.current_unit->flags.delim;
2683 /* Check the pad mode. */
2684 dtp->u.p.current_unit->pad_status
2685 = !(cf & IOPARM_DT_HAS_PAD) ? PAD_UNSPECIFIED :
2686 find_option (&dtp->common, dtp->pad, dtp->pad_len, pad_opt,
2687 "Bad PAD parameter in data transfer statement");
2689 if (dtp->u.p.current_unit->pad_status == PAD_UNSPECIFIED)
2690 dtp->u.p.current_unit->pad_status = dtp->u.p.current_unit->flags.pad;
2692 /* Check to see if we might be reading what we wrote before */
2694 if (dtp->u.p.mode != dtp->u.p.current_unit->mode
2695 && !is_internal_unit (dtp))
2697 int pos = fbuf_reset (dtp->u.p.current_unit);
2698 if (pos != 0)
2699 sseek (dtp->u.p.current_unit->s, pos, SEEK_CUR);
2700 sflush(dtp->u.p.current_unit->s);
2703 /* Check the POS= specifier: that it is in range and that it is used with a
2704 unit that has been connected for STREAM access. F2003 9.5.1.10. */
2706 if (((cf & IOPARM_DT_HAS_POS) != 0))
2708 if (is_stream_io (dtp))
2711 if (dtp->pos <= 0)
2713 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2714 "POS=specifier must be positive");
2715 return;
2718 if (dtp->pos >= dtp->u.p.current_unit->maxrec)
2720 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2721 "POS=specifier too large");
2722 return;
2725 dtp->rec = dtp->pos;
2727 if (dtp->u.p.mode == READING)
2729 /* Reset the endfile flag; if we hit EOF during reading
2730 we'll set the flag and generate an error at that point
2731 rather than worrying about it here. */
2732 dtp->u.p.current_unit->endfile = NO_ENDFILE;
2735 if (dtp->pos != dtp->u.p.current_unit->strm_pos)
2737 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
2738 if (sseek (dtp->u.p.current_unit->s, dtp->pos - 1, SEEK_SET) < 0)
2740 generate_error (&dtp->common, LIBERROR_OS, NULL);
2741 return;
2743 dtp->u.p.current_unit->strm_pos = dtp->pos;
2746 else
2748 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2749 "POS=specifier not allowed, "
2750 "Try OPEN with ACCESS='stream'");
2751 return;
2756 /* Sanity checks on the record number. */
2757 if ((cf & IOPARM_DT_HAS_REC) != 0)
2759 if (dtp->rec <= 0)
2761 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2762 "Record number must be positive");
2763 return;
2766 if (dtp->rec >= dtp->u.p.current_unit->maxrec)
2768 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2769 "Record number too large");
2770 return;
2773 /* Make sure format buffer is reset. */
2774 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED)
2775 fbuf_reset (dtp->u.p.current_unit);
2778 /* Check whether the record exists to be read. Only
2779 a partial record needs to exist. */
2781 if (dtp->u.p.mode == READING && (dtp->rec - 1)
2782 * dtp->u.p.current_unit->recl >= ssize (dtp->u.p.current_unit->s))
2784 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2785 "Non-existing record number");
2786 return;
2789 /* Position the file. */
2790 if (sseek (dtp->u.p.current_unit->s, (gfc_offset) (dtp->rec - 1)
2791 * dtp->u.p.current_unit->recl, SEEK_SET) < 0)
2793 generate_error (&dtp->common, LIBERROR_OS, NULL);
2794 return;
2797 /* TODO: This is required to maintain compatibility between
2798 4.3 and 4.4 runtime. Remove when ABI changes from 4.3 */
2800 if (is_stream_io (dtp))
2801 dtp->u.p.current_unit->strm_pos = dtp->rec;
2803 /* TODO: Un-comment this code when ABI changes from 4.3.
2804 if (dtp->u.p.current_unit->flags.access == ACCESS_STREAM)
2806 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2807 "Record number not allowed for stream access "
2808 "data transfer");
2809 return;
2810 } */
2813 /* Bugware for badly written mixed C-Fortran I/O. */
2814 if (!is_internal_unit (dtp))
2815 flush_if_preconnected(dtp->u.p.current_unit->s);
2817 dtp->u.p.current_unit->mode = dtp->u.p.mode;
2819 /* Set the maximum position reached from the previous I/O operation. This
2820 could be greater than zero from a previous non-advancing write. */
2821 dtp->u.p.max_pos = dtp->u.p.current_unit->saved_pos;
2823 pre_position (dtp);
2826 /* Set up the subroutine that will handle the transfers. */
2828 if (read_flag)
2830 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
2831 dtp->u.p.transfer = unformatted_read;
2832 else
2834 if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
2836 dtp->u.p.last_char = EOF - 1;
2837 dtp->u.p.transfer = list_formatted_read;
2839 else
2840 dtp->u.p.transfer = formatted_transfer;
2843 else
2845 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
2846 dtp->u.p.transfer = unformatted_write;
2847 else
2849 if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
2850 dtp->u.p.transfer = list_formatted_write;
2851 else
2852 dtp->u.p.transfer = formatted_transfer;
2856 /* Make sure that we don't do a read after a nonadvancing write. */
2858 if (read_flag)
2860 if (dtp->u.p.current_unit->read_bad && !is_stream_io (dtp))
2862 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2863 "Cannot READ after a nonadvancing WRITE");
2864 return;
2867 else
2869 if (dtp->u.p.advance_status == ADVANCE_YES && !dtp->u.p.seen_dollar)
2870 dtp->u.p.current_unit->read_bad = 1;
2873 /* Start the data transfer if we are doing a formatted transfer. */
2874 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
2875 && ((cf & (IOPARM_DT_LIST_FORMAT | IOPARM_DT_HAS_NAMELIST_NAME)) == 0)
2876 && dtp->u.p.ionml == NULL)
2877 formatted_transfer (dtp, 0, NULL, 0, 0, 1);
2880 /* Initialize an array_loop_spec given the array descriptor. The function
2881 returns the index of the last element of the array, and also returns
2882 starting record, where the first I/O goes to (necessary in case of
2883 negative strides). */
2885 gfc_offset
2886 init_loop_spec (gfc_array_char *desc, array_loop_spec *ls,
2887 gfc_offset *start_record)
2889 int rank = GFC_DESCRIPTOR_RANK(desc);
2890 int i;
2891 gfc_offset index;
2892 int empty;
2894 empty = 0;
2895 index = 1;
2896 *start_record = 0;
2898 for (i=0; i<rank; i++)
2900 ls[i].idx = GFC_DESCRIPTOR_LBOUND(desc,i);
2901 ls[i].start = GFC_DESCRIPTOR_LBOUND(desc,i);
2902 ls[i].end = GFC_DESCRIPTOR_UBOUND(desc,i);
2903 ls[i].step = GFC_DESCRIPTOR_STRIDE(desc,i);
2904 empty = empty || (GFC_DESCRIPTOR_UBOUND(desc,i)
2905 < GFC_DESCRIPTOR_LBOUND(desc,i));
2907 if (GFC_DESCRIPTOR_STRIDE(desc,i) > 0)
2909 index += (GFC_DESCRIPTOR_EXTENT(desc,i) - 1)
2910 * GFC_DESCRIPTOR_STRIDE(desc,i);
2912 else
2914 index -= (GFC_DESCRIPTOR_EXTENT(desc,i) - 1)
2915 * GFC_DESCRIPTOR_STRIDE(desc,i);
2916 *start_record -= (GFC_DESCRIPTOR_EXTENT(desc,i) - 1)
2917 * GFC_DESCRIPTOR_STRIDE(desc,i);
2921 if (empty)
2922 return 0;
2923 else
2924 return index;
2927 /* Determine the index to the next record in an internal unit array by
2928 by incrementing through the array_loop_spec. */
2930 gfc_offset
2931 next_array_record (st_parameter_dt *dtp, array_loop_spec *ls, int *finished)
2933 int i, carry;
2934 gfc_offset index;
2936 carry = 1;
2937 index = 0;
2939 for (i = 0; i < dtp->u.p.current_unit->rank; i++)
2941 if (carry)
2943 ls[i].idx++;
2944 if (ls[i].idx > ls[i].end)
2946 ls[i].idx = ls[i].start;
2947 carry = 1;
2949 else
2950 carry = 0;
2952 index = index + (ls[i].idx - ls[i].start) * ls[i].step;
2955 *finished = carry;
2957 return index;
2962 /* Skip to the end of the current record, taking care of an optional
2963 record marker of size bytes. If the file is not seekable, we
2964 read chunks of size MAX_READ until we get to the right
2965 position. */
2967 static void
2968 skip_record (st_parameter_dt *dtp, ssize_t bytes)
2970 ssize_t rlength, readb;
2971 static const ssize_t MAX_READ = 4096;
2972 char p[MAX_READ];
2974 dtp->u.p.current_unit->bytes_left_subrecord += bytes;
2975 if (dtp->u.p.current_unit->bytes_left_subrecord == 0)
2976 return;
2978 /* Direct access files do not generate END conditions,
2979 only I/O errors. */
2980 if (sseek (dtp->u.p.current_unit->s,
2981 dtp->u.p.current_unit->bytes_left_subrecord, SEEK_CUR) < 0)
2983 /* Seeking failed, fall back to seeking by reading data. */
2984 while (dtp->u.p.current_unit->bytes_left_subrecord > 0)
2986 rlength =
2987 (MAX_READ < dtp->u.p.current_unit->bytes_left_subrecord) ?
2988 MAX_READ : dtp->u.p.current_unit->bytes_left_subrecord;
2990 readb = sread (dtp->u.p.current_unit->s, p, rlength);
2991 if (readb < 0)
2993 generate_error (&dtp->common, LIBERROR_OS, NULL);
2994 return;
2997 dtp->u.p.current_unit->bytes_left_subrecord -= readb;
2999 return;
3001 dtp->u.p.current_unit->bytes_left_subrecord = 0;
3005 /* Advance to the next record reading unformatted files, taking
3006 care of subrecords. If complete_record is nonzero, we loop
3007 until all subrecords are cleared. */
3009 static void
3010 next_record_r_unf (st_parameter_dt *dtp, int complete_record)
3012 size_t bytes;
3014 bytes = compile_options.record_marker == 0 ?
3015 sizeof (GFC_INTEGER_4) : compile_options.record_marker;
3017 while(1)
3020 /* Skip over tail */
3022 skip_record (dtp, bytes);
3024 if ( ! (complete_record && dtp->u.p.current_unit->continued))
3025 return;
3027 us_read (dtp, 1);
3032 static gfc_offset
3033 min_off (gfc_offset a, gfc_offset b)
3035 return (a < b ? a : b);
3039 /* Space to the next record for read mode. */
3041 static void
3042 next_record_r (st_parameter_dt *dtp, int done)
3044 gfc_offset record;
3045 int bytes_left;
3046 char p;
3047 int cc;
3049 switch (current_mode (dtp))
3051 /* No records in unformatted STREAM I/O. */
3052 case UNFORMATTED_STREAM:
3053 return;
3055 case UNFORMATTED_SEQUENTIAL:
3056 next_record_r_unf (dtp, 1);
3057 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
3058 break;
3060 case FORMATTED_DIRECT:
3061 case UNFORMATTED_DIRECT:
3062 skip_record (dtp, dtp->u.p.current_unit->bytes_left);
3063 break;
3065 case FORMATTED_STREAM:
3066 case FORMATTED_SEQUENTIAL:
3067 /* read_sf has already terminated input because of an '\n', or
3068 we have hit EOF. */
3069 if (dtp->u.p.sf_seen_eor)
3071 dtp->u.p.sf_seen_eor = 0;
3072 break;
3075 if (is_internal_unit (dtp))
3077 if (is_array_io (dtp))
3079 int finished;
3081 record = next_array_record (dtp, dtp->u.p.current_unit->ls,
3082 &finished);
3083 if (!done && finished)
3084 hit_eof (dtp);
3086 /* Now seek to this record. */
3087 record = record * dtp->u.p.current_unit->recl;
3088 if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
3090 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3091 break;
3093 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
3095 else
3097 bytes_left = (int) dtp->u.p.current_unit->bytes_left;
3098 bytes_left = min_off (bytes_left,
3099 ssize (dtp->u.p.current_unit->s)
3100 - stell (dtp->u.p.current_unit->s));
3101 if (sseek (dtp->u.p.current_unit->s,
3102 bytes_left, SEEK_CUR) < 0)
3104 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3105 break;
3107 dtp->u.p.current_unit->bytes_left
3108 = dtp->u.p.current_unit->recl;
3110 break;
3112 else
3116 errno = 0;
3117 cc = fbuf_getc (dtp->u.p.current_unit);
3118 if (cc == EOF)
3120 if (errno != 0)
3121 generate_error (&dtp->common, LIBERROR_OS, NULL);
3122 else
3124 if (is_stream_io (dtp)
3125 || dtp->u.p.current_unit->pad_status == PAD_NO
3126 || dtp->u.p.current_unit->bytes_left
3127 == dtp->u.p.current_unit->recl)
3128 hit_eof (dtp);
3130 break;
3133 if (is_stream_io (dtp))
3134 dtp->u.p.current_unit->strm_pos++;
3136 p = (char) cc;
3138 while (p != '\n');
3140 break;
3145 /* Small utility function to write a record marker, taking care of
3146 byte swapping and of choosing the correct size. */
3148 static int
3149 write_us_marker (st_parameter_dt *dtp, const gfc_offset buf)
3151 size_t len;
3152 GFC_INTEGER_4 buf4;
3153 GFC_INTEGER_8 buf8;
3155 if (compile_options.record_marker == 0)
3156 len = sizeof (GFC_INTEGER_4);
3157 else
3158 len = compile_options.record_marker;
3160 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
3161 if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE))
3163 switch (len)
3165 case sizeof (GFC_INTEGER_4):
3166 buf4 = buf;
3167 return swrite (dtp->u.p.current_unit->s, &buf4, len);
3168 break;
3170 case sizeof (GFC_INTEGER_8):
3171 buf8 = buf;
3172 return swrite (dtp->u.p.current_unit->s, &buf8, len);
3173 break;
3175 default:
3176 runtime_error ("Illegal value for record marker");
3177 break;
3180 else
3182 uint32_t u32;
3183 uint64_t u64;
3184 switch (len)
3186 case sizeof (GFC_INTEGER_4):
3187 buf4 = buf;
3188 memcpy (&u32, &buf4, sizeof (u32));
3189 u32 = __builtin_bswap32 (u32);
3190 return swrite (dtp->u.p.current_unit->s, &u32, len);
3191 break;
3193 case sizeof (GFC_INTEGER_8):
3194 buf8 = buf;
3195 memcpy (&u64, &buf8, sizeof (u64));
3196 u64 = __builtin_bswap64 (u64);
3197 return swrite (dtp->u.p.current_unit->s, &u64, len);
3198 break;
3200 default:
3201 runtime_error ("Illegal value for record marker");
3202 break;
3208 /* Position to the next (sub)record in write mode for
3209 unformatted sequential files. */
3211 static void
3212 next_record_w_unf (st_parameter_dt *dtp, int next_subrecord)
3214 gfc_offset m, m_write, record_marker;
3216 /* Bytes written. */
3217 m = dtp->u.p.current_unit->recl_subrecord
3218 - dtp->u.p.current_unit->bytes_left_subrecord;
3220 if (compile_options.record_marker == 0)
3221 record_marker = sizeof (GFC_INTEGER_4);
3222 else
3223 record_marker = compile_options.record_marker;
3225 /* Seek to the head and overwrite the bogus length with the real
3226 length. */
3228 if (unlikely (sseek (dtp->u.p.current_unit->s, - m - record_marker,
3229 SEEK_CUR) < 0))
3230 goto io_error;
3232 if (next_subrecord)
3233 m_write = -m;
3234 else
3235 m_write = m;
3237 if (unlikely (write_us_marker (dtp, m_write) < 0))
3238 goto io_error;
3240 /* Seek past the end of the current record. */
3242 if (unlikely (sseek (dtp->u.p.current_unit->s, m, SEEK_CUR) < 0))
3243 goto io_error;
3245 /* Write the length tail. If we finish a record containing
3246 subrecords, we write out the negative length. */
3248 if (dtp->u.p.current_unit->continued)
3249 m_write = -m;
3250 else
3251 m_write = m;
3253 if (unlikely (write_us_marker (dtp, m_write) < 0))
3254 goto io_error;
3256 return;
3258 io_error:
3259 generate_error (&dtp->common, LIBERROR_OS, NULL);
3260 return;
3265 /* Utility function like memset() but operating on streams. Return
3266 value is same as for POSIX write(). */
3268 static ssize_t
3269 sset (stream * s, int c, ssize_t nbyte)
3271 static const int WRITE_CHUNK = 256;
3272 char p[WRITE_CHUNK];
3273 ssize_t bytes_left, trans;
3275 if (nbyte < WRITE_CHUNK)
3276 memset (p, c, nbyte);
3277 else
3278 memset (p, c, WRITE_CHUNK);
3280 bytes_left = nbyte;
3281 while (bytes_left > 0)
3283 trans = (bytes_left < WRITE_CHUNK) ? bytes_left : WRITE_CHUNK;
3284 trans = swrite (s, p, trans);
3285 if (trans <= 0)
3286 return trans;
3287 bytes_left -= trans;
3290 return nbyte - bytes_left;
3294 /* Position to the next record in write mode. */
3296 static void
3297 next_record_w (st_parameter_dt *dtp, int done)
3299 gfc_offset m, record, max_pos;
3300 int length;
3302 /* Zero counters for X- and T-editing. */
3303 max_pos = dtp->u.p.max_pos;
3304 dtp->u.p.max_pos = dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
3306 switch (current_mode (dtp))
3308 /* No records in unformatted STREAM I/O. */
3309 case UNFORMATTED_STREAM:
3310 return;
3312 case FORMATTED_DIRECT:
3313 if (dtp->u.p.current_unit->bytes_left == 0)
3314 break;
3316 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
3317 fbuf_flush (dtp->u.p.current_unit, WRITING);
3318 if (sset (dtp->u.p.current_unit->s, ' ',
3319 dtp->u.p.current_unit->bytes_left)
3320 != dtp->u.p.current_unit->bytes_left)
3321 goto io_error;
3323 break;
3325 case UNFORMATTED_DIRECT:
3326 if (dtp->u.p.current_unit->bytes_left > 0)
3328 length = (int) dtp->u.p.current_unit->bytes_left;
3329 if (sset (dtp->u.p.current_unit->s, 0, length) != length)
3330 goto io_error;
3332 break;
3334 case UNFORMATTED_SEQUENTIAL:
3335 next_record_w_unf (dtp, 0);
3336 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
3337 break;
3339 case FORMATTED_STREAM:
3340 case FORMATTED_SEQUENTIAL:
3342 if (is_internal_unit (dtp))
3344 char *p;
3345 if (is_array_io (dtp))
3347 int finished;
3349 length = (int) dtp->u.p.current_unit->bytes_left;
3351 /* If the farthest position reached is greater than current
3352 position, adjust the position and set length to pad out
3353 whats left. Otherwise just pad whats left.
3354 (for character array unit) */
3355 m = dtp->u.p.current_unit->recl
3356 - dtp->u.p.current_unit->bytes_left;
3357 if (max_pos > m)
3359 length = (int) (max_pos - m);
3360 if (sseek (dtp->u.p.current_unit->s,
3361 length, SEEK_CUR) < 0)
3363 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3364 return;
3366 length = (int) (dtp->u.p.current_unit->recl - max_pos);
3369 p = write_block (dtp, length);
3370 if (p == NULL)
3371 return;
3373 if (unlikely (is_char4_unit (dtp)))
3375 gfc_char4_t *p4 = (gfc_char4_t *) p;
3376 memset4 (p4, ' ', length);
3378 else
3379 memset (p, ' ', length);
3381 /* Now that the current record has been padded out,
3382 determine where the next record in the array is. */
3383 record = next_array_record (dtp, dtp->u.p.current_unit->ls,
3384 &finished);
3385 if (finished)
3386 dtp->u.p.current_unit->endfile = AT_ENDFILE;
3388 /* Now seek to this record */
3389 record = record * dtp->u.p.current_unit->recl;
3391 if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
3393 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3394 return;
3397 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
3399 else
3401 length = 1;
3403 /* If this is the last call to next_record move to the farthest
3404 position reached and set length to pad out the remainder
3405 of the record. (for character scaler unit) */
3406 if (done)
3408 m = dtp->u.p.current_unit->recl
3409 - dtp->u.p.current_unit->bytes_left;
3410 if (max_pos > m)
3412 length = (int) (max_pos - m);
3413 if (sseek (dtp->u.p.current_unit->s,
3414 length, SEEK_CUR) < 0)
3416 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3417 return;
3419 length = (int) (dtp->u.p.current_unit->recl - max_pos);
3421 else
3422 length = (int) dtp->u.p.current_unit->bytes_left;
3424 if (length > 0)
3426 p = write_block (dtp, length);
3427 if (p == NULL)
3428 return;
3430 if (unlikely (is_char4_unit (dtp)))
3432 gfc_char4_t *p4 = (gfc_char4_t *) p;
3433 memset4 (p4, (gfc_char4_t) ' ', length);
3435 else
3436 memset (p, ' ', length);
3440 else
3442 #ifdef HAVE_CRLF
3443 const int len = 2;
3444 #else
3445 const int len = 1;
3446 #endif
3447 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
3448 char * p = fbuf_alloc (dtp->u.p.current_unit, len);
3449 if (!p)
3450 goto io_error;
3451 #ifdef HAVE_CRLF
3452 *(p++) = '\r';
3453 #endif
3454 *p = '\n';
3455 if (is_stream_io (dtp))
3457 dtp->u.p.current_unit->strm_pos += len;
3458 if (dtp->u.p.current_unit->strm_pos
3459 < ssize (dtp->u.p.current_unit->s))
3460 unit_truncate (dtp->u.p.current_unit,
3461 dtp->u.p.current_unit->strm_pos - 1,
3462 &dtp->common);
3466 break;
3468 io_error:
3469 generate_error (&dtp->common, LIBERROR_OS, NULL);
3470 break;
3474 /* Position to the next record, which means moving to the end of the
3475 current record. This can happen under several different
3476 conditions. If the done flag is not set, we get ready to process
3477 the next record. */
3479 void
3480 next_record (st_parameter_dt *dtp, int done)
3482 gfc_offset fp; /* File position. */
3484 dtp->u.p.current_unit->read_bad = 0;
3486 if (dtp->u.p.mode == READING)
3487 next_record_r (dtp, done);
3488 else
3489 next_record_w (dtp, done);
3491 if (!is_stream_io (dtp))
3493 /* Since we have changed the position, set it to unspecified so
3494 that INQUIRE(POSITION=) knows it needs to look into it. */
3495 if (done)
3496 dtp->u.p.current_unit->flags.position = POSITION_UNSPECIFIED;
3498 dtp->u.p.current_unit->current_record = 0;
3499 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
3501 fp = stell (dtp->u.p.current_unit->s);
3502 /* Calculate next record, rounding up partial records. */
3503 dtp->u.p.current_unit->last_record =
3504 (fp + dtp->u.p.current_unit->recl - 1) /
3505 dtp->u.p.current_unit->recl;
3507 else
3508 dtp->u.p.current_unit->last_record++;
3511 if (!done)
3512 pre_position (dtp);
3514 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
3515 smarkeor (dtp->u.p.current_unit->s);
3519 /* Finalize the current data transfer. For a nonadvancing transfer,
3520 this means advancing to the next record. For internal units close the
3521 stream associated with the unit. */
3523 static void
3524 finalize_transfer (st_parameter_dt *dtp)
3526 GFC_INTEGER_4 cf = dtp->common.flags;
3528 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
3529 *dtp->size = dtp->u.p.size_used;
3531 if (dtp->u.p.eor_condition)
3533 generate_error (&dtp->common, LIBERROR_EOR, NULL);
3534 return;
3537 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
3539 if (dtp->u.p.current_unit && current_mode (dtp) == UNFORMATTED_SEQUENTIAL)
3540 dtp->u.p.current_unit->current_record = 0;
3541 return;
3544 if ((dtp->u.p.ionml != NULL)
3545 && (cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0)
3547 if ((cf & IOPARM_DT_NAMELIST_READ_MODE) != 0)
3548 namelist_read (dtp);
3549 else
3550 namelist_write (dtp);
3553 dtp->u.p.transfer = NULL;
3554 if (dtp->u.p.current_unit == NULL)
3555 return;
3557 if ((cf & IOPARM_DT_LIST_FORMAT) != 0 && dtp->u.p.mode == READING)
3559 finish_list_read (dtp);
3560 return;
3563 if (dtp->u.p.mode == WRITING)
3564 dtp->u.p.current_unit->previous_nonadvancing_write
3565 = dtp->u.p.advance_status == ADVANCE_NO;
3567 if (is_stream_io (dtp))
3569 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
3570 && dtp->u.p.advance_status != ADVANCE_NO)
3571 next_record (dtp, 1);
3573 return;
3576 dtp->u.p.current_unit->current_record = 0;
3578 if (!is_internal_unit (dtp) && dtp->u.p.seen_dollar)
3580 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
3581 dtp->u.p.seen_dollar = 0;
3582 return;
3585 /* For non-advancing I/O, save the current maximum position for use in the
3586 next I/O operation if needed. */
3587 if (dtp->u.p.advance_status == ADVANCE_NO)
3589 int bytes_written = (int) (dtp->u.p.current_unit->recl
3590 - dtp->u.p.current_unit->bytes_left);
3591 dtp->u.p.current_unit->saved_pos =
3592 dtp->u.p.max_pos > 0 ? dtp->u.p.max_pos - bytes_written : 0;
3593 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
3594 return;
3596 else if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
3597 && dtp->u.p.mode == WRITING && !is_internal_unit (dtp))
3598 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
3600 dtp->u.p.current_unit->saved_pos = 0;
3602 next_record (dtp, 1);
3605 /* Transfer function for IOLENGTH. It doesn't actually do any
3606 data transfer, it just updates the length counter. */
3608 static void
3609 iolength_transfer (st_parameter_dt *dtp, bt type __attribute__((unused)),
3610 void *dest __attribute__ ((unused)),
3611 int kind __attribute__((unused)),
3612 size_t size, size_t nelems)
3614 if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
3615 *dtp->iolength += (GFC_IO_INT) (size * nelems);
3619 /* Initialize the IOLENGTH data transfer. This function is in essence
3620 a very much simplified version of data_transfer_init(), because it
3621 doesn't have to deal with units at all. */
3623 static void
3624 iolength_transfer_init (st_parameter_dt *dtp)
3626 if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
3627 *dtp->iolength = 0;
3629 memset (&dtp->u.p, 0, sizeof (dtp->u.p));
3631 /* Set up the subroutine that will handle the transfers. */
3633 dtp->u.p.transfer = iolength_transfer;
3637 /* Library entry point for the IOLENGTH form of the INQUIRE
3638 statement. The IOLENGTH form requires no I/O to be performed, but
3639 it must still be a runtime library call so that we can determine
3640 the iolength for dynamic arrays and such. */
3642 extern void st_iolength (st_parameter_dt *);
3643 export_proto(st_iolength);
3645 void
3646 st_iolength (st_parameter_dt *dtp)
3648 library_start (&dtp->common);
3649 iolength_transfer_init (dtp);
3652 extern void st_iolength_done (st_parameter_dt *);
3653 export_proto(st_iolength_done);
3655 void
3656 st_iolength_done (st_parameter_dt *dtp __attribute__((unused)))
3658 free_ionml (dtp);
3659 library_end ();
3663 /* The READ statement. */
3665 extern void st_read (st_parameter_dt *);
3666 export_proto(st_read);
3668 void
3669 st_read (st_parameter_dt *dtp)
3671 library_start (&dtp->common);
3673 data_transfer_init (dtp, 1);
3676 extern void st_read_done (st_parameter_dt *);
3677 export_proto(st_read_done);
3679 void
3680 st_read_done (st_parameter_dt *dtp)
3682 finalize_transfer (dtp);
3683 if (is_internal_unit (dtp) || dtp->u.p.format_not_saved)
3684 free_format_data (dtp->u.p.fmt);
3685 free_ionml (dtp);
3686 if (dtp->u.p.current_unit != NULL)
3687 unlock_unit (dtp->u.p.current_unit);
3689 free_internal_unit (dtp);
3691 library_end ();
3694 extern void st_write (st_parameter_dt *);
3695 export_proto(st_write);
3697 void
3698 st_write (st_parameter_dt *dtp)
3700 library_start (&dtp->common);
3701 data_transfer_init (dtp, 0);
3704 extern void st_write_done (st_parameter_dt *);
3705 export_proto(st_write_done);
3707 void
3708 st_write_done (st_parameter_dt *dtp)
3710 finalize_transfer (dtp);
3712 /* Deal with endfile conditions associated with sequential files. */
3714 if (dtp->u.p.current_unit != NULL
3715 && dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
3716 switch (dtp->u.p.current_unit->endfile)
3718 case AT_ENDFILE: /* Remain at the endfile record. */
3719 break;
3721 case AFTER_ENDFILE:
3722 dtp->u.p.current_unit->endfile = AT_ENDFILE; /* Just at it now. */
3723 break;
3725 case NO_ENDFILE:
3726 /* Get rid of whatever is after this record. */
3727 if (!is_internal_unit (dtp))
3728 unit_truncate (dtp->u.p.current_unit,
3729 stell (dtp->u.p.current_unit->s),
3730 &dtp->common);
3731 dtp->u.p.current_unit->endfile = AT_ENDFILE;
3732 break;
3735 if (is_internal_unit (dtp) || dtp->u.p.format_not_saved)
3736 free_format_data (dtp->u.p.fmt);
3737 free_ionml (dtp);
3738 if (dtp->u.p.current_unit != NULL)
3739 unlock_unit (dtp->u.p.current_unit);
3741 free_internal_unit (dtp);
3743 library_end ();
3747 /* F2003: This is a stub for the runtime portion of the WAIT statement. */
3748 void
3749 st_wait (st_parameter_wait *wtp __attribute__((unused)))
3754 /* Receives the scalar information for namelist objects and stores it
3755 in a linked list of namelist_info types. */
3757 extern void st_set_nml_var (st_parameter_dt *dtp, void *, char *,
3758 GFC_INTEGER_4, gfc_charlen_type, GFC_INTEGER_4);
3759 export_proto(st_set_nml_var);
3762 void
3763 st_set_nml_var (st_parameter_dt *dtp, void * var_addr, char * var_name,
3764 GFC_INTEGER_4 len, gfc_charlen_type string_length,
3765 GFC_INTEGER_4 dtype)
3767 namelist_info *t1 = NULL;
3768 namelist_info *nml;
3769 size_t var_name_len = strlen (var_name);
3771 nml = (namelist_info*) xmalloc (sizeof (namelist_info));
3773 nml->mem_pos = var_addr;
3775 nml->var_name = (char*) xmalloc (var_name_len + 1);
3776 memcpy (nml->var_name, var_name, var_name_len);
3777 nml->var_name[var_name_len] = '\0';
3779 nml->len = (int) len;
3780 nml->string_length = (index_type) string_length;
3782 nml->var_rank = (int) (dtype & GFC_DTYPE_RANK_MASK);
3783 nml->size = (index_type) (dtype >> GFC_DTYPE_SIZE_SHIFT);
3784 nml->type = (bt) ((dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT);
3786 if (nml->var_rank > 0)
3788 nml->dim = (descriptor_dimension*)
3789 xmallocarray (nml->var_rank, sizeof (descriptor_dimension));
3790 nml->ls = (array_loop_spec*)
3791 xmallocarray (nml->var_rank, sizeof (array_loop_spec));
3793 else
3795 nml->dim = NULL;
3796 nml->ls = NULL;
3799 nml->next = NULL;
3801 if ((dtp->common.flags & IOPARM_DT_IONML_SET) == 0)
3803 dtp->common.flags |= IOPARM_DT_IONML_SET;
3804 dtp->u.p.ionml = nml;
3806 else
3808 for (t1 = dtp->u.p.ionml; t1->next; t1 = t1->next);
3809 t1->next = nml;
3813 /* Store the dimensional information for the namelist object. */
3814 extern void st_set_nml_var_dim (st_parameter_dt *, GFC_INTEGER_4,
3815 index_type, index_type,
3816 index_type);
3817 export_proto(st_set_nml_var_dim);
3819 void
3820 st_set_nml_var_dim (st_parameter_dt *dtp, GFC_INTEGER_4 n_dim,
3821 index_type stride, index_type lbound,
3822 index_type ubound)
3824 namelist_info * nml;
3825 int n;
3827 n = (int)n_dim;
3829 for (nml = dtp->u.p.ionml; nml->next; nml = nml->next);
3831 GFC_DIMENSION_SET(nml->dim[n],lbound,ubound,stride);
3835 /* Once upon a time, a poor innocent Fortran program was reading a
3836 file, when suddenly it hit the end-of-file (EOF). Unfortunately
3837 the OS doesn't tell whether we're at the EOF or whether we already
3838 went past it. Luckily our hero, libgfortran, keeps track of this.
3839 Call this function when you detect an EOF condition. See Section
3840 9.10.2 in F2003. */
3842 void
3843 hit_eof (st_parameter_dt * dtp)
3845 dtp->u.p.current_unit->flags.position = POSITION_APPEND;
3847 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
3848 switch (dtp->u.p.current_unit->endfile)
3850 case NO_ENDFILE:
3851 case AT_ENDFILE:
3852 generate_error (&dtp->common, LIBERROR_END, NULL);
3853 if (!is_internal_unit (dtp) && !dtp->u.p.namelist_mode)
3855 dtp->u.p.current_unit->endfile = AFTER_ENDFILE;
3856 dtp->u.p.current_unit->current_record = 0;
3858 else
3859 dtp->u.p.current_unit->endfile = AT_ENDFILE;
3860 break;
3862 case AFTER_ENDFILE:
3863 generate_error (&dtp->common, LIBERROR_ENDFILE, NULL);
3864 dtp->u.p.current_unit->current_record = 0;
3865 break;
3867 else
3869 /* Non-sequential files don't have an ENDFILE record, so we
3870 can't be at AFTER_ENDFILE. */
3871 dtp->u.p.current_unit->endfile = AT_ENDFILE;
3872 generate_error (&dtp->common, LIBERROR_END, NULL);
3873 dtp->u.p.current_unit->current_record = 0;