/
[official-gcc.git] / libgfortran / io / transfer.c
blob8fffe0e54c8bd059041edc1a309a6d642743aa10
1 /* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
2 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
4 Namelist transfer functions contributed by Paul Thomas
5 F2003 I/O support contributed by Jerry DeLisle
7 This file is part of the GNU Fortran runtime library (libgfortran).
9 Libgfortran is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
14 Libgfortran is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 Under Section 7 of GPL version 3, you are granted additional
20 permissions described in the GCC Runtime Library Exception, version
21 3.1, as published by the Free Software Foundation.
23 You should have received a copy of the GNU General Public License and
24 a copy of the GCC Runtime Library Exception along with this program;
25 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
26 <http://www.gnu.org/licenses/>. */
29 /* transfer.c -- Top level handling of data transfer statements. */
31 #include "io.h"
32 #include "fbuf.h"
33 #include "format.h"
34 #include "unix.h"
35 #include <string.h>
36 #include <assert.h>
37 #include <stdlib.h>
38 #include <errno.h>
41 /* Calling conventions: Data transfer statements are unlike other
42 library calls in that they extend over several calls.
44 The first call is always a call to st_read() or st_write(). These
45 subroutines return no status unless a namelist read or write is
46 being done, in which case there is the usual status. No further
47 calls are necessary in this case.
49 For other sorts of data transfer, there are zero or more data
50 transfer statement that depend on the format of the data transfer
51 statement. For READ (and for backwards compatibily: for WRITE), one has
53 transfer_integer
54 transfer_logical
55 transfer_character
56 transfer_character_wide
57 transfer_real
58 transfer_complex
59 transfer_real128
60 transfer_complex128
62 and for WRITE
64 transfer_integer_write
65 transfer_logical_write
66 transfer_character_write
67 transfer_character_wide_write
68 transfer_real_write
69 transfer_complex_write
70 transfer_real128_write
71 transfer_complex128_write
73 These subroutines do not return status. The *128 functions
74 are in the file transfer128.c.
76 The last call is a call to st_[read|write]_done(). While
77 something can easily go wrong with the initial st_read() or
78 st_write(), an error inhibits any data from actually being
79 transferred. */
81 extern void transfer_integer (st_parameter_dt *, void *, int);
82 export_proto(transfer_integer);
84 extern void transfer_integer_write (st_parameter_dt *, void *, int);
85 export_proto(transfer_integer_write);
87 extern void transfer_real (st_parameter_dt *, void *, int);
88 export_proto(transfer_real);
90 extern void transfer_real_write (st_parameter_dt *, void *, int);
91 export_proto(transfer_real_write);
93 extern void transfer_logical (st_parameter_dt *, void *, int);
94 export_proto(transfer_logical);
96 extern void transfer_logical_write (st_parameter_dt *, void *, int);
97 export_proto(transfer_logical_write);
99 extern void transfer_character (st_parameter_dt *, void *, int);
100 export_proto(transfer_character);
102 extern void transfer_character_write (st_parameter_dt *, void *, int);
103 export_proto(transfer_character_write);
105 extern void transfer_character_wide (st_parameter_dt *, void *, int, int);
106 export_proto(transfer_character_wide);
108 extern void transfer_character_wide_write (st_parameter_dt *,
109 void *, int, int);
110 export_proto(transfer_character_wide_write);
112 extern void transfer_complex (st_parameter_dt *, void *, int);
113 export_proto(transfer_complex);
115 extern void transfer_complex_write (st_parameter_dt *, void *, int);
116 export_proto(transfer_complex_write);
118 extern void transfer_array (st_parameter_dt *, gfc_array_char *, int,
119 gfc_charlen_type);
120 export_proto(transfer_array);
122 extern void transfer_array_write (st_parameter_dt *, gfc_array_char *, int,
123 gfc_charlen_type);
124 export_proto(transfer_array_write);
126 static void us_read (st_parameter_dt *, int);
127 static void us_write (st_parameter_dt *, int);
128 static void next_record_r_unf (st_parameter_dt *, int);
129 static void next_record_w_unf (st_parameter_dt *, int);
131 static const st_option advance_opt[] = {
132 {"yes", ADVANCE_YES},
133 {"no", ADVANCE_NO},
134 {NULL, 0}
138 static const st_option decimal_opt[] = {
139 {"point", DECIMAL_POINT},
140 {"comma", DECIMAL_COMMA},
141 {NULL, 0}
144 static const st_option round_opt[] = {
145 {"up", ROUND_UP},
146 {"down", ROUND_DOWN},
147 {"zero", ROUND_ZERO},
148 {"nearest", ROUND_NEAREST},
149 {"compatible", ROUND_COMPATIBLE},
150 {"processor_defined", ROUND_PROCDEFINED},
151 {NULL, 0}
155 static const st_option sign_opt[] = {
156 {"plus", SIGN_SP},
157 {"suppress", SIGN_SS},
158 {"processor_defined", SIGN_S},
159 {NULL, 0}
162 static const st_option blank_opt[] = {
163 {"null", BLANK_NULL},
164 {"zero", BLANK_ZERO},
165 {NULL, 0}
168 static const st_option delim_opt[] = {
169 {"apostrophe", DELIM_APOSTROPHE},
170 {"quote", DELIM_QUOTE},
171 {"none", DELIM_NONE},
172 {NULL, 0}
175 static const st_option pad_opt[] = {
176 {"yes", PAD_YES},
177 {"no", PAD_NO},
178 {NULL, 0}
181 typedef enum
182 { FORMATTED_SEQUENTIAL, UNFORMATTED_SEQUENTIAL,
183 FORMATTED_DIRECT, UNFORMATTED_DIRECT, FORMATTED_STREAM, UNFORMATTED_STREAM
185 file_mode;
188 static file_mode
189 current_mode (st_parameter_dt *dtp)
191 file_mode m;
193 m = FORM_UNSPECIFIED;
195 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
197 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
198 FORMATTED_DIRECT : UNFORMATTED_DIRECT;
200 else if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
202 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
203 FORMATTED_SEQUENTIAL : UNFORMATTED_SEQUENTIAL;
205 else if (dtp->u.p.current_unit->flags.access == ACCESS_STREAM)
207 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
208 FORMATTED_STREAM : UNFORMATTED_STREAM;
211 return m;
215 /* Mid level data transfer statements. */
217 /* Read sequential file - internal unit */
219 static char *
220 read_sf_internal (st_parameter_dt *dtp, int * length)
222 static char *empty_string[0];
223 char *base;
224 int lorig;
226 /* Zero size array gives internal unit len of 0. Nothing to read. */
227 if (dtp->internal_unit_len == 0
228 && dtp->u.p.current_unit->pad_status == PAD_NO)
229 hit_eof (dtp);
231 /* If we have seen an eor previously, return a length of 0. The
232 caller is responsible for correctly padding the input field. */
233 if (dtp->u.p.sf_seen_eor)
235 *length = 0;
236 /* Just return something that isn't a NULL pointer, otherwise the
237 caller thinks an error occured. */
238 return (char*) empty_string;
241 lorig = *length;
242 if (is_char4_unit(dtp))
244 int i;
245 gfc_char4_t *p = (gfc_char4_t *) mem_alloc_r4 (dtp->u.p.current_unit->s,
246 length);
247 base = fbuf_alloc (dtp->u.p.current_unit, lorig);
248 for (i = 0; i < *length; i++, p++)
249 base[i] = *p > 255 ? '?' : (unsigned char) *p;
251 else
252 base = mem_alloc_r (dtp->u.p.current_unit->s, length);
254 if (unlikely (lorig > *length))
256 hit_eof (dtp);
257 return NULL;
260 dtp->u.p.current_unit->bytes_left -= *length;
262 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
263 dtp->u.p.size_used += (GFC_IO_INT) *length;
265 return base;
269 /* When reading sequential formatted records we have a problem. We
270 don't know how long the line is until we read the trailing newline,
271 and we don't want to read too much. If we read too much, we might
272 have to do a physical seek backwards depending on how much data is
273 present, and devices like terminals aren't seekable and would cause
274 an I/O error.
276 Given this, the solution is to read a byte at a time, stopping if
277 we hit the newline. For small allocations, we use a static buffer.
278 For larger allocations, we are forced to allocate memory on the
279 heap. Hopefully this won't happen very often. */
281 /* Read sequential file - external unit */
283 static char *
284 read_sf (st_parameter_dt *dtp, int * length)
286 static char *empty_string[0];
287 char *base, *p, q;
288 int n, lorig, seen_comma;
290 /* If we have seen an eor previously, return a length of 0. The
291 caller is responsible for correctly padding the input field. */
292 if (dtp->u.p.sf_seen_eor)
294 *length = 0;
295 /* Just return something that isn't a NULL pointer, otherwise the
296 caller thinks an error occured. */
297 return (char*) empty_string;
300 n = seen_comma = 0;
302 /* Read data into format buffer and scan through it. */
303 lorig = *length;
304 base = p = fbuf_read (dtp->u.p.current_unit, length);
305 if (base == NULL)
306 return NULL;
308 while (n < *length)
310 q = *p;
312 if (q == '\n' || q == '\r')
314 /* Unexpected end of line. Set the position. */
315 fbuf_seek (dtp->u.p.current_unit, n + 1 ,SEEK_CUR);
316 dtp->u.p.sf_seen_eor = 1;
318 /* If we see an EOR during non-advancing I/O, we need to skip
319 the rest of the I/O statement. Set the corresponding flag. */
320 if (dtp->u.p.advance_status == ADVANCE_NO || dtp->u.p.seen_dollar)
321 dtp->u.p.eor_condition = 1;
323 /* If we encounter a CR, it might be a CRLF. */
324 if (q == '\r') /* Probably a CRLF */
326 /* See if there is an LF. Use fbuf_read rather then fbuf_getc so
327 the position is not advanced unless it really is an LF. */
328 int readlen = 1;
329 p = fbuf_read (dtp->u.p.current_unit, &readlen);
330 if (*p == '\n' && readlen == 1)
332 dtp->u.p.sf_seen_eor = 2;
333 fbuf_seek (dtp->u.p.current_unit, 1 ,SEEK_CUR);
337 /* Without padding, terminate the I/O statement without assigning
338 the value. With padding, the value still needs to be assigned,
339 so we can just continue with a short read. */
340 if (dtp->u.p.current_unit->pad_status == PAD_NO)
342 generate_error (&dtp->common, LIBERROR_EOR, NULL);
343 return NULL;
346 *length = n;
347 goto done;
349 /* Short circuit the read if a comma is found during numeric input.
350 The flag is set to zero during character reads so that commas in
351 strings are not ignored */
352 if (q == ',')
353 if (dtp->u.p.sf_read_comma == 1)
355 seen_comma = 1;
356 notify_std (&dtp->common, GFC_STD_GNU,
357 "Comma in formatted numeric read.");
358 *length = n;
359 break;
361 n++;
362 p++;
365 fbuf_seek (dtp->u.p.current_unit, n + seen_comma, SEEK_CUR);
367 /* A short read implies we hit EOF, unless we hit EOR, a comma, or
368 some other stuff. Set the relevant flags. */
369 if (lorig > *length && !dtp->u.p.sf_seen_eor && !seen_comma)
371 if (n > 0)
373 if (dtp->u.p.advance_status == ADVANCE_NO)
375 if (dtp->u.p.current_unit->pad_status == PAD_NO)
377 hit_eof (dtp);
378 return NULL;
380 else
381 dtp->u.p.eor_condition = 1;
383 else
384 dtp->u.p.at_eof = 1;
386 else if (dtp->u.p.advance_status == ADVANCE_NO
387 || dtp->u.p.current_unit->pad_status == PAD_NO
388 || dtp->u.p.current_unit->bytes_left
389 == dtp->u.p.current_unit->recl)
391 hit_eof (dtp);
392 return NULL;
396 done:
398 dtp->u.p.current_unit->bytes_left -= n;
400 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
401 dtp->u.p.size_used += (GFC_IO_INT) n;
403 return base;
407 /* Function for reading the next couple of bytes from the current
408 file, advancing the current position. We return FAILURE on end of record or
409 end of file. This function is only for formatted I/O, unformatted uses
410 read_block_direct.
412 If the read is short, then it is because the current record does not
413 have enough data to satisfy the read request and the file was
414 opened with PAD=YES. The caller must assume tailing spaces for
415 short reads. */
417 void *
418 read_block_form (st_parameter_dt *dtp, int * nbytes)
420 char *source;
421 int norig;
423 if (!is_stream_io (dtp))
425 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) *nbytes)
427 /* For preconnected units with default record length, set bytes left
428 to unit record length and proceed, otherwise error. */
429 if (dtp->u.p.current_unit->unit_number == options.stdin_unit
430 && dtp->u.p.current_unit->recl == DEFAULT_RECL)
431 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
432 else
434 if (unlikely (dtp->u.p.current_unit->pad_status == PAD_NO)
435 && !is_internal_unit (dtp))
437 /* Not enough data left. */
438 generate_error (&dtp->common, LIBERROR_EOR, NULL);
439 return NULL;
443 if (unlikely (dtp->u.p.current_unit->bytes_left == 0
444 && !is_internal_unit(dtp)))
446 hit_eof (dtp);
447 return NULL;
450 *nbytes = dtp->u.p.current_unit->bytes_left;
454 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
455 (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL ||
456 dtp->u.p.current_unit->flags.access == ACCESS_STREAM))
458 if (is_internal_unit (dtp))
459 source = read_sf_internal (dtp, nbytes);
460 else
461 source = read_sf (dtp, nbytes);
463 dtp->u.p.current_unit->strm_pos +=
464 (gfc_offset) (*nbytes + dtp->u.p.sf_seen_eor);
465 return source;
468 /* If we reach here, we can assume it's direct access. */
470 dtp->u.p.current_unit->bytes_left -= (gfc_offset) *nbytes;
472 norig = *nbytes;
473 source = fbuf_read (dtp->u.p.current_unit, nbytes);
474 fbuf_seek (dtp->u.p.current_unit, *nbytes, SEEK_CUR);
476 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
477 dtp->u.p.size_used += (GFC_IO_INT) *nbytes;
479 if (norig != *nbytes)
481 /* Short read, this shouldn't happen. */
482 if (!dtp->u.p.current_unit->pad_status == PAD_YES)
484 generate_error (&dtp->common, LIBERROR_EOR, NULL);
485 source = NULL;
489 dtp->u.p.current_unit->strm_pos += (gfc_offset) *nbytes;
491 return source;
495 /* Read a block from a character(kind=4) internal unit, to be transferred into
496 a character(kind=4) variable. Note: Portions of this code borrowed from
497 read_sf_internal. */
498 void *
499 read_block_form4 (st_parameter_dt *dtp, int * nbytes)
501 static gfc_char4_t *empty_string[0];
502 gfc_char4_t *source;
503 int lorig;
505 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) *nbytes)
506 *nbytes = dtp->u.p.current_unit->bytes_left;
508 /* Zero size array gives internal unit len of 0. Nothing to read. */
509 if (dtp->internal_unit_len == 0
510 && dtp->u.p.current_unit->pad_status == PAD_NO)
511 hit_eof (dtp);
513 /* If we have seen an eor previously, return a length of 0. The
514 caller is responsible for correctly padding the input field. */
515 if (dtp->u.p.sf_seen_eor)
517 *nbytes = 0;
518 /* Just return something that isn't a NULL pointer, otherwise the
519 caller thinks an error occured. */
520 return empty_string;
523 lorig = *nbytes;
524 source = (gfc_char4_t *) mem_alloc_r4 (dtp->u.p.current_unit->s, nbytes);
526 if (unlikely (lorig > *nbytes))
528 hit_eof (dtp);
529 return NULL;
532 dtp->u.p.current_unit->bytes_left -= *nbytes;
534 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
535 dtp->u.p.size_used += (GFC_IO_INT) *nbytes;
537 return source;
541 /* Reads a block directly into application data space. This is for
542 unformatted files. */
544 static void
545 read_block_direct (st_parameter_dt *dtp, void *buf, size_t nbytes)
547 ssize_t to_read_record;
548 ssize_t have_read_record;
549 ssize_t to_read_subrecord;
550 ssize_t have_read_subrecord;
551 int short_record;
553 if (is_stream_io (dtp))
555 have_read_record = sread (dtp->u.p.current_unit->s, buf,
556 nbytes);
557 if (unlikely (have_read_record < 0))
559 generate_error (&dtp->common, LIBERROR_OS, NULL);
560 return;
563 dtp->u.p.current_unit->strm_pos += (gfc_offset) have_read_record;
565 if (unlikely ((ssize_t) nbytes != have_read_record))
567 /* Short read, e.g. if we hit EOF. For stream files,
568 we have to set the end-of-file condition. */
569 hit_eof (dtp);
571 return;
574 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
576 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) nbytes)
578 short_record = 1;
579 to_read_record = dtp->u.p.current_unit->bytes_left;
580 nbytes = to_read_record;
582 else
584 short_record = 0;
585 to_read_record = nbytes;
588 dtp->u.p.current_unit->bytes_left -= to_read_record;
590 to_read_record = sread (dtp->u.p.current_unit->s, buf, to_read_record);
591 if (unlikely (to_read_record < 0))
593 generate_error (&dtp->common, LIBERROR_OS, NULL);
594 return;
597 if (to_read_record != (ssize_t) nbytes)
599 /* Short read, e.g. if we hit EOF. Apparently, we read
600 more than was written to the last record. */
601 return;
604 if (unlikely (short_record))
606 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
608 return;
611 /* Unformatted sequential. We loop over the subrecords, reading
612 until the request has been fulfilled or the record has run out
613 of continuation subrecords. */
615 /* Check whether we exceed the total record length. */
617 if (dtp->u.p.current_unit->flags.has_recl
618 && ((gfc_offset) nbytes > dtp->u.p.current_unit->bytes_left))
620 to_read_record = dtp->u.p.current_unit->bytes_left;
621 short_record = 1;
623 else
625 to_read_record = nbytes;
626 short_record = 0;
628 have_read_record = 0;
630 while(1)
632 if (dtp->u.p.current_unit->bytes_left_subrecord
633 < (gfc_offset) to_read_record)
635 to_read_subrecord = dtp->u.p.current_unit->bytes_left_subrecord;
636 to_read_record -= to_read_subrecord;
638 else
640 to_read_subrecord = to_read_record;
641 to_read_record = 0;
644 dtp->u.p.current_unit->bytes_left_subrecord -= to_read_subrecord;
646 have_read_subrecord = sread (dtp->u.p.current_unit->s,
647 buf + have_read_record, to_read_subrecord);
648 if (unlikely (have_read_subrecord) < 0)
650 generate_error (&dtp->common, LIBERROR_OS, NULL);
651 return;
654 have_read_record += have_read_subrecord;
656 if (unlikely (to_read_subrecord != have_read_subrecord))
658 /* Short read, e.g. if we hit EOF. This means the record
659 structure has been corrupted, or the trailing record
660 marker would still be present. */
662 generate_error (&dtp->common, LIBERROR_CORRUPT_FILE, NULL);
663 return;
666 if (to_read_record > 0)
668 if (likely (dtp->u.p.current_unit->continued))
670 next_record_r_unf (dtp, 0);
671 us_read (dtp, 1);
673 else
675 /* Let's make sure the file position is correctly pre-positioned
676 for the next read statement. */
678 dtp->u.p.current_unit->current_record = 0;
679 next_record_r_unf (dtp, 0);
680 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
681 return;
684 else
686 /* Normal exit, the read request has been fulfilled. */
687 break;
691 dtp->u.p.current_unit->bytes_left -= have_read_record;
692 if (unlikely (short_record))
694 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
695 return;
697 return;
701 /* Function for writing a block of bytes to the current file at the
702 current position, advancing the file pointer. We are given a length
703 and return a pointer to a buffer that the caller must (completely)
704 fill in. Returns NULL on error. */
706 void *
707 write_block (st_parameter_dt *dtp, int length)
709 char *dest;
711 if (!is_stream_io (dtp))
713 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) length)
715 /* For preconnected units with default record length, set bytes left
716 to unit record length and proceed, otherwise error. */
717 if (likely ((dtp->u.p.current_unit->unit_number
718 == options.stdout_unit
719 || dtp->u.p.current_unit->unit_number
720 == options.stderr_unit)
721 && dtp->u.p.current_unit->recl == DEFAULT_RECL))
722 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
723 else
725 generate_error (&dtp->common, LIBERROR_EOR, NULL);
726 return NULL;
730 dtp->u.p.current_unit->bytes_left -= (gfc_offset) length;
733 if (is_internal_unit (dtp))
735 if (dtp->common.unit) /* char4 internel unit. */
737 gfc_char4_t *dest4;
738 dest4 = mem_alloc_w4 (dtp->u.p.current_unit->s, &length);
739 if (dest4 == NULL)
741 generate_error (&dtp->common, LIBERROR_END, NULL);
742 return NULL;
744 return dest4;
746 else
747 dest = mem_alloc_w (dtp->u.p.current_unit->s, &length);
749 if (dest == NULL)
751 generate_error (&dtp->common, LIBERROR_END, NULL);
752 return NULL;
755 if (unlikely (dtp->u.p.current_unit->endfile == AT_ENDFILE))
756 generate_error (&dtp->common, LIBERROR_END, NULL);
758 else
760 dest = fbuf_alloc (dtp->u.p.current_unit, length);
761 if (dest == NULL)
763 generate_error (&dtp->common, LIBERROR_OS, NULL);
764 return NULL;
768 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
769 dtp->u.p.size_used += (GFC_IO_INT) length;
771 dtp->u.p.current_unit->strm_pos += (gfc_offset) length;
773 return dest;
777 /* High level interface to swrite(), taking care of errors. This is only
778 called for unformatted files. There are three cases to consider:
779 Stream I/O, unformatted direct, unformatted sequential. */
781 static try
782 write_buf (st_parameter_dt *dtp, void *buf, size_t nbytes)
785 ssize_t have_written;
786 ssize_t to_write_subrecord;
787 int short_record;
789 /* Stream I/O. */
791 if (is_stream_io (dtp))
793 have_written = swrite (dtp->u.p.current_unit->s, buf, nbytes);
794 if (unlikely (have_written < 0))
796 generate_error (&dtp->common, LIBERROR_OS, NULL);
797 return FAILURE;
800 dtp->u.p.current_unit->strm_pos += (gfc_offset) have_written;
802 return SUCCESS;
805 /* Unformatted direct access. */
807 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
809 if (unlikely (dtp->u.p.current_unit->bytes_left < (gfc_offset) nbytes))
811 generate_error (&dtp->common, LIBERROR_DIRECT_EOR, NULL);
812 return FAILURE;
815 if (buf == NULL && nbytes == 0)
816 return SUCCESS;
818 have_written = swrite (dtp->u.p.current_unit->s, buf, nbytes);
819 if (unlikely (have_written < 0))
821 generate_error (&dtp->common, LIBERROR_OS, NULL);
822 return FAILURE;
825 dtp->u.p.current_unit->strm_pos += (gfc_offset) have_written;
826 dtp->u.p.current_unit->bytes_left -= (gfc_offset) have_written;
828 return SUCCESS;
831 /* Unformatted sequential. */
833 have_written = 0;
835 if (dtp->u.p.current_unit->flags.has_recl
836 && (gfc_offset) nbytes > dtp->u.p.current_unit->bytes_left)
838 nbytes = dtp->u.p.current_unit->bytes_left;
839 short_record = 1;
841 else
843 short_record = 0;
846 while (1)
849 to_write_subrecord =
850 (size_t) dtp->u.p.current_unit->bytes_left_subrecord < nbytes ?
851 (size_t) dtp->u.p.current_unit->bytes_left_subrecord : nbytes;
853 dtp->u.p.current_unit->bytes_left_subrecord -=
854 (gfc_offset) to_write_subrecord;
856 to_write_subrecord = swrite (dtp->u.p.current_unit->s,
857 buf + have_written, to_write_subrecord);
858 if (unlikely (to_write_subrecord < 0))
860 generate_error (&dtp->common, LIBERROR_OS, NULL);
861 return FAILURE;
864 dtp->u.p.current_unit->strm_pos += (gfc_offset) to_write_subrecord;
865 nbytes -= to_write_subrecord;
866 have_written += to_write_subrecord;
868 if (nbytes == 0)
869 break;
871 next_record_w_unf (dtp, 1);
872 us_write (dtp, 1);
874 dtp->u.p.current_unit->bytes_left -= have_written;
875 if (unlikely (short_record))
877 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
878 return FAILURE;
880 return SUCCESS;
884 /* Master function for unformatted reads. */
886 static void
887 unformatted_read (st_parameter_dt *dtp, bt type,
888 void *dest, int kind, size_t size, size_t nelems)
890 if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE)
891 || kind == 1)
893 if (type == BT_CHARACTER)
894 size *= GFC_SIZE_OF_CHAR_KIND(kind);
895 read_block_direct (dtp, dest, size * nelems);
897 else
899 char buffer[16];
900 char *p;
901 size_t i;
903 p = dest;
905 /* Handle wide chracters. */
906 if (type == BT_CHARACTER && kind != 1)
908 nelems *= size;
909 size = kind;
912 /* Break up complex into its constituent reals. */
913 if (type == BT_COMPLEX)
915 nelems *= 2;
916 size /= 2;
919 /* By now, all complex variables have been split into their
920 constituent reals. */
922 for (i = 0; i < nelems; i++)
924 read_block_direct (dtp, buffer, size);
925 reverse_memcpy (p, buffer, size);
926 p += size;
932 /* Master function for unformatted writes. NOTE: For kind=10 the size is 16
933 bytes on 64 bit machines. The unused bytes are not initialized and never
934 used, which can show an error with memory checking analyzers like
935 valgrind. */
937 static void
938 unformatted_write (st_parameter_dt *dtp, bt type,
939 void *source, int kind, size_t size, size_t nelems)
941 if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE)
942 || kind == 1)
944 size_t stride = type == BT_CHARACTER ?
945 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
947 write_buf (dtp, source, stride * nelems);
949 else
951 char buffer[16];
952 char *p;
953 size_t i;
955 p = source;
957 /* Handle wide chracters. */
958 if (type == BT_CHARACTER && kind != 1)
960 nelems *= size;
961 size = kind;
964 /* Break up complex into its constituent reals. */
965 if (type == BT_COMPLEX)
967 nelems *= 2;
968 size /= 2;
971 /* By now, all complex variables have been split into their
972 constituent reals. */
974 for (i = 0; i < nelems; i++)
976 reverse_memcpy(buffer, p, size);
977 p += size;
978 write_buf (dtp, buffer, size);
984 /* Return a pointer to the name of a type. */
986 const char *
987 type_name (bt type)
989 const char *p;
991 switch (type)
993 case BT_INTEGER:
994 p = "INTEGER";
995 break;
996 case BT_LOGICAL:
997 p = "LOGICAL";
998 break;
999 case BT_CHARACTER:
1000 p = "CHARACTER";
1001 break;
1002 case BT_REAL:
1003 p = "REAL";
1004 break;
1005 case BT_COMPLEX:
1006 p = "COMPLEX";
1007 break;
1008 default:
1009 internal_error (NULL, "type_name(): Bad type");
1012 return p;
1016 /* Write a constant string to the output.
1017 This is complicated because the string can have doubled delimiters
1018 in it. The length in the format node is the true length. */
1020 static void
1021 write_constant_string (st_parameter_dt *dtp, const fnode *f)
1023 char c, delimiter, *p, *q;
1024 int length;
1026 length = f->u.string.length;
1027 if (length == 0)
1028 return;
1030 p = write_block (dtp, length);
1031 if (p == NULL)
1032 return;
1034 q = f->u.string.p;
1035 delimiter = q[-1];
1037 for (; length > 0; length--)
1039 c = *p++ = *q++;
1040 if (c == delimiter && c != 'H' && c != 'h')
1041 q++; /* Skip the doubled delimiter. */
1046 /* Given actual and expected types in a formatted data transfer, make
1047 sure they agree. If not, an error message is generated. Returns
1048 nonzero if something went wrong. */
1050 static int
1051 require_type (st_parameter_dt *dtp, bt expected, bt actual, const fnode *f)
1053 char buffer[100];
1055 if (actual == expected)
1056 return 0;
1058 /* Adjust item_count before emitting error message. */
1059 sprintf (buffer, "Expected %s for item %d in formatted transfer, got %s",
1060 type_name (expected), dtp->u.p.item_count - 1, type_name (actual));
1062 format_error (dtp, f, buffer);
1063 return 1;
1067 /* This function is in the main loop for a formatted data transfer
1068 statement. It would be natural to implement this as a coroutine
1069 with the user program, but C makes that awkward. We loop,
1070 processing format elements. When we actually have to transfer
1071 data instead of just setting flags, we return control to the user
1072 program which calls a function that supplies the address and type
1073 of the next element, then comes back here to process it. */
1075 static void
1076 formatted_transfer_scalar_read (st_parameter_dt *dtp, bt type, void *p, int kind,
1077 size_t size)
1079 int pos, bytes_used;
1080 const fnode *f;
1081 format_token t;
1082 int n;
1083 int consume_data_flag;
1085 /* Change a complex data item into a pair of reals. */
1087 n = (p == NULL) ? 0 : ((type != BT_COMPLEX) ? 1 : 2);
1088 if (type == BT_COMPLEX)
1090 type = BT_REAL;
1091 size /= 2;
1094 /* If there's an EOR condition, we simulate finalizing the transfer
1095 by doing nothing. */
1096 if (dtp->u.p.eor_condition)
1097 return;
1099 /* Set this flag so that commas in reads cause the read to complete before
1100 the entire field has been read. The next read field will start right after
1101 the comma in the stream. (Set to 0 for character reads). */
1102 dtp->u.p.sf_read_comma =
1103 dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA ? 0 : 1;
1105 for (;;)
1107 /* If reversion has occurred and there is another real data item,
1108 then we have to move to the next record. */
1109 if (dtp->u.p.reversion_flag && n > 0)
1111 dtp->u.p.reversion_flag = 0;
1112 next_record (dtp, 0);
1115 consume_data_flag = 1;
1116 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1117 break;
1119 f = next_format (dtp);
1120 if (f == NULL)
1122 /* No data descriptors left. */
1123 if (unlikely (n > 0))
1124 generate_error (&dtp->common, LIBERROR_FORMAT,
1125 "Insufficient data descriptors in format after reversion");
1126 return;
1129 t = f->format;
1131 bytes_used = (int)(dtp->u.p.current_unit->recl
1132 - dtp->u.p.current_unit->bytes_left);
1134 if (is_stream_io(dtp))
1135 bytes_used = 0;
1137 switch (t)
1139 case FMT_I:
1140 if (n == 0)
1141 goto need_read_data;
1142 if (require_type (dtp, BT_INTEGER, type, f))
1143 return;
1144 read_decimal (dtp, f, p, kind);
1145 break;
1147 case FMT_B:
1148 if (n == 0)
1149 goto need_read_data;
1150 if (!(compile_options.allow_std & GFC_STD_GNU)
1151 && require_type (dtp, BT_INTEGER, type, f))
1152 return;
1153 read_radix (dtp, f, p, kind, 2);
1154 break;
1156 case FMT_O:
1157 if (n == 0)
1158 goto need_read_data;
1159 if (!(compile_options.allow_std & GFC_STD_GNU)
1160 && require_type (dtp, BT_INTEGER, type, f))
1161 return;
1162 read_radix (dtp, f, p, kind, 8);
1163 break;
1165 case FMT_Z:
1166 if (n == 0)
1167 goto need_read_data;
1168 if (!(compile_options.allow_std & GFC_STD_GNU)
1169 && require_type (dtp, BT_INTEGER, type, f))
1170 return;
1171 read_radix (dtp, f, p, kind, 16);
1172 break;
1174 case FMT_A:
1175 if (n == 0)
1176 goto need_read_data;
1178 /* It is possible to have FMT_A with something not BT_CHARACTER such
1179 as when writing out hollerith strings, so check both type
1180 and kind before calling wide character routines. */
1181 if (type == BT_CHARACTER && kind == 4)
1182 read_a_char4 (dtp, f, p, size);
1183 else
1184 read_a (dtp, f, p, size);
1185 break;
1187 case FMT_L:
1188 if (n == 0)
1189 goto need_read_data;
1190 read_l (dtp, f, p, kind);
1191 break;
1193 case FMT_D:
1194 if (n == 0)
1195 goto need_read_data;
1196 if (require_type (dtp, BT_REAL, type, f))
1197 return;
1198 read_f (dtp, f, p, kind);
1199 break;
1201 case FMT_E:
1202 if (n == 0)
1203 goto need_read_data;
1204 if (require_type (dtp, BT_REAL, type, f))
1205 return;
1206 read_f (dtp, f, p, kind);
1207 break;
1209 case FMT_EN:
1210 if (n == 0)
1211 goto need_read_data;
1212 if (require_type (dtp, BT_REAL, type, f))
1213 return;
1214 read_f (dtp, f, p, kind);
1215 break;
1217 case FMT_ES:
1218 if (n == 0)
1219 goto need_read_data;
1220 if (require_type (dtp, BT_REAL, type, f))
1221 return;
1222 read_f (dtp, f, p, kind);
1223 break;
1225 case FMT_F:
1226 if (n == 0)
1227 goto need_read_data;
1228 if (require_type (dtp, BT_REAL, type, f))
1229 return;
1230 read_f (dtp, f, p, kind);
1231 break;
1233 case FMT_G:
1234 if (n == 0)
1235 goto need_read_data;
1236 switch (type)
1238 case BT_INTEGER:
1239 read_decimal (dtp, f, p, kind);
1240 break;
1241 case BT_LOGICAL:
1242 read_l (dtp, f, p, kind);
1243 break;
1244 case BT_CHARACTER:
1245 if (kind == 4)
1246 read_a_char4 (dtp, f, p, size);
1247 else
1248 read_a (dtp, f, p, size);
1249 break;
1250 case BT_REAL:
1251 read_f (dtp, f, p, kind);
1252 break;
1253 default:
1254 internal_error (&dtp->common, "formatted_transfer(): Bad type");
1256 break;
1258 case FMT_STRING:
1259 consume_data_flag = 0;
1260 format_error (dtp, f, "Constant string in input format");
1261 return;
1263 /* Format codes that don't transfer data. */
1264 case FMT_X:
1265 case FMT_TR:
1266 consume_data_flag = 0;
1267 dtp->u.p.skips += f->u.n;
1268 pos = bytes_used + dtp->u.p.skips - 1;
1269 dtp->u.p.pending_spaces = pos - dtp->u.p.max_pos + 1;
1270 read_x (dtp, f->u.n);
1271 break;
1273 case FMT_TL:
1274 case FMT_T:
1275 consume_data_flag = 0;
1277 if (f->format == FMT_TL)
1279 /* Handle the special case when no bytes have been used yet.
1280 Cannot go below zero. */
1281 if (bytes_used == 0)
1283 dtp->u.p.pending_spaces -= f->u.n;
1284 dtp->u.p.skips -= f->u.n;
1285 dtp->u.p.skips = dtp->u.p.skips < 0 ? 0 : dtp->u.p.skips;
1288 pos = bytes_used - f->u.n;
1290 else /* FMT_T */
1291 pos = f->u.n - 1;
1293 /* Standard 10.6.1.1: excessive left tabbing is reset to the
1294 left tab limit. We do not check if the position has gone
1295 beyond the end of record because a subsequent tab could
1296 bring us back again. */
1297 pos = pos < 0 ? 0 : pos;
1299 dtp->u.p.skips = dtp->u.p.skips + pos - bytes_used;
1300 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces
1301 + pos - dtp->u.p.max_pos;
1302 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces < 0
1303 ? 0 : dtp->u.p.pending_spaces;
1304 if (dtp->u.p.skips == 0)
1305 break;
1307 /* Adjust everything for end-of-record condition */
1308 if (dtp->u.p.sf_seen_eor && !is_internal_unit (dtp))
1310 dtp->u.p.current_unit->bytes_left -= dtp->u.p.sf_seen_eor;
1311 dtp->u.p.skips -= dtp->u.p.sf_seen_eor;
1312 bytes_used = pos;
1313 dtp->u.p.sf_seen_eor = 0;
1315 if (dtp->u.p.skips < 0)
1317 if (is_internal_unit (dtp))
1318 sseek (dtp->u.p.current_unit->s, dtp->u.p.skips, SEEK_CUR);
1319 else
1320 fbuf_seek (dtp->u.p.current_unit, dtp->u.p.skips, SEEK_CUR);
1321 dtp->u.p.current_unit->bytes_left -= (gfc_offset) dtp->u.p.skips;
1322 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1324 else
1325 read_x (dtp, dtp->u.p.skips);
1326 break;
1328 case FMT_S:
1329 consume_data_flag = 0;
1330 dtp->u.p.sign_status = SIGN_S;
1331 break;
1333 case FMT_SS:
1334 consume_data_flag = 0;
1335 dtp->u.p.sign_status = SIGN_SS;
1336 break;
1338 case FMT_SP:
1339 consume_data_flag = 0;
1340 dtp->u.p.sign_status = SIGN_SP;
1341 break;
1343 case FMT_BN:
1344 consume_data_flag = 0 ;
1345 dtp->u.p.blank_status = BLANK_NULL;
1346 break;
1348 case FMT_BZ:
1349 consume_data_flag = 0;
1350 dtp->u.p.blank_status = BLANK_ZERO;
1351 break;
1353 case FMT_DC:
1354 consume_data_flag = 0;
1355 dtp->u.p.current_unit->decimal_status = DECIMAL_COMMA;
1356 break;
1358 case FMT_DP:
1359 consume_data_flag = 0;
1360 dtp->u.p.current_unit->decimal_status = DECIMAL_POINT;
1361 break;
1363 case FMT_RC:
1364 consume_data_flag = 0;
1365 dtp->u.p.current_unit->round_status = ROUND_COMPATIBLE;
1366 break;
1368 case FMT_RD:
1369 consume_data_flag = 0;
1370 dtp->u.p.current_unit->round_status = ROUND_DOWN;
1371 break;
1373 case FMT_RN:
1374 consume_data_flag = 0;
1375 dtp->u.p.current_unit->round_status = ROUND_NEAREST;
1376 break;
1378 case FMT_RP:
1379 consume_data_flag = 0;
1380 dtp->u.p.current_unit->round_status = ROUND_PROCDEFINED;
1381 break;
1383 case FMT_RU:
1384 consume_data_flag = 0;
1385 dtp->u.p.current_unit->round_status = ROUND_UP;
1386 break;
1388 case FMT_RZ:
1389 consume_data_flag = 0;
1390 dtp->u.p.current_unit->round_status = ROUND_ZERO;
1391 break;
1393 case FMT_P:
1394 consume_data_flag = 0;
1395 dtp->u.p.scale_factor = f->u.k;
1396 break;
1398 case FMT_DOLLAR:
1399 consume_data_flag = 0;
1400 dtp->u.p.seen_dollar = 1;
1401 break;
1403 case FMT_SLASH:
1404 consume_data_flag = 0;
1405 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1406 next_record (dtp, 0);
1407 break;
1409 case FMT_COLON:
1410 /* A colon descriptor causes us to exit this loop (in
1411 particular preventing another / descriptor from being
1412 processed) unless there is another data item to be
1413 transferred. */
1414 consume_data_flag = 0;
1415 if (n == 0)
1416 return;
1417 break;
1419 default:
1420 internal_error (&dtp->common, "Bad format node");
1423 /* Adjust the item count and data pointer. */
1425 if ((consume_data_flag > 0) && (n > 0))
1427 n--;
1428 p = ((char *) p) + size;
1431 dtp->u.p.skips = 0;
1433 pos = (int)(dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left);
1434 dtp->u.p.max_pos = (dtp->u.p.max_pos > pos) ? dtp->u.p.max_pos : pos;
1437 return;
1439 /* Come here when we need a data descriptor but don't have one. We
1440 push the current format node back onto the input, then return and
1441 let the user program call us back with the data. */
1442 need_read_data:
1443 unget_format (dtp, f);
1447 static void
1448 formatted_transfer_scalar_write (st_parameter_dt *dtp, bt type, void *p, int kind,
1449 size_t size)
1451 int pos, bytes_used;
1452 const fnode *f;
1453 format_token t;
1454 int n;
1455 int consume_data_flag;
1457 /* Change a complex data item into a pair of reals. */
1459 n = (p == NULL) ? 0 : ((type != BT_COMPLEX) ? 1 : 2);
1460 if (type == BT_COMPLEX)
1462 type = BT_REAL;
1463 size /= 2;
1466 /* If there's an EOR condition, we simulate finalizing the transfer
1467 by doing nothing. */
1468 if (dtp->u.p.eor_condition)
1469 return;
1471 /* Set this flag so that commas in reads cause the read to complete before
1472 the entire field has been read. The next read field will start right after
1473 the comma in the stream. (Set to 0 for character reads). */
1474 dtp->u.p.sf_read_comma =
1475 dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA ? 0 : 1;
1477 for (;;)
1479 /* If reversion has occurred and there is another real data item,
1480 then we have to move to the next record. */
1481 if (dtp->u.p.reversion_flag && n > 0)
1483 dtp->u.p.reversion_flag = 0;
1484 next_record (dtp, 0);
1487 consume_data_flag = 1;
1488 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1489 break;
1491 f = next_format (dtp);
1492 if (f == NULL)
1494 /* No data descriptors left. */
1495 if (unlikely (n > 0))
1496 generate_error (&dtp->common, LIBERROR_FORMAT,
1497 "Insufficient data descriptors in format after reversion");
1498 return;
1501 /* Now discharge T, TR and X movements to the right. This is delayed
1502 until a data producing format to suppress trailing spaces. */
1504 t = f->format;
1505 if (dtp->u.p.mode == WRITING && dtp->u.p.skips != 0
1506 && ((n>0 && ( t == FMT_I || t == FMT_B || t == FMT_O
1507 || t == FMT_Z || t == FMT_F || t == FMT_E
1508 || t == FMT_EN || t == FMT_ES || t == FMT_G
1509 || t == FMT_L || t == FMT_A || t == FMT_D))
1510 || t == FMT_STRING))
1512 if (dtp->u.p.skips > 0)
1514 int tmp;
1515 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
1516 tmp = (int)(dtp->u.p.current_unit->recl
1517 - dtp->u.p.current_unit->bytes_left);
1518 dtp->u.p.max_pos =
1519 dtp->u.p.max_pos > tmp ? dtp->u.p.max_pos : tmp;
1521 if (dtp->u.p.skips < 0)
1523 if (is_internal_unit (dtp))
1524 sseek (dtp->u.p.current_unit->s, dtp->u.p.skips, SEEK_CUR);
1525 else
1526 fbuf_seek (dtp->u.p.current_unit, dtp->u.p.skips, SEEK_CUR);
1527 dtp->u.p.current_unit->bytes_left -= (gfc_offset) dtp->u.p.skips;
1529 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1532 bytes_used = (int)(dtp->u.p.current_unit->recl
1533 - dtp->u.p.current_unit->bytes_left);
1535 if (is_stream_io(dtp))
1536 bytes_used = 0;
1538 switch (t)
1540 case FMT_I:
1541 if (n == 0)
1542 goto need_data;
1543 if (require_type (dtp, BT_INTEGER, type, f))
1544 return;
1545 write_i (dtp, f, p, kind);
1546 break;
1548 case FMT_B:
1549 if (n == 0)
1550 goto need_data;
1551 if (!(compile_options.allow_std & GFC_STD_GNU)
1552 && require_type (dtp, BT_INTEGER, type, f))
1553 return;
1554 write_b (dtp, f, p, kind);
1555 break;
1557 case FMT_O:
1558 if (n == 0)
1559 goto need_data;
1560 if (!(compile_options.allow_std & GFC_STD_GNU)
1561 && require_type (dtp, BT_INTEGER, type, f))
1562 return;
1563 write_o (dtp, f, p, kind);
1564 break;
1566 case FMT_Z:
1567 if (n == 0)
1568 goto need_data;
1569 if (!(compile_options.allow_std & GFC_STD_GNU)
1570 && require_type (dtp, BT_INTEGER, type, f))
1571 return;
1572 write_z (dtp, f, p, kind);
1573 break;
1575 case FMT_A:
1576 if (n == 0)
1577 goto need_data;
1579 /* It is possible to have FMT_A with something not BT_CHARACTER such
1580 as when writing out hollerith strings, so check both type
1581 and kind before calling wide character routines. */
1582 if (type == BT_CHARACTER && kind == 4)
1583 write_a_char4 (dtp, f, p, size);
1584 else
1585 write_a (dtp, f, p, size);
1586 break;
1588 case FMT_L:
1589 if (n == 0)
1590 goto need_data;
1591 write_l (dtp, f, p, kind);
1592 break;
1594 case FMT_D:
1595 if (n == 0)
1596 goto need_data;
1597 if (require_type (dtp, BT_REAL, type, f))
1598 return;
1599 write_d (dtp, f, p, kind);
1600 break;
1602 case FMT_E:
1603 if (n == 0)
1604 goto need_data;
1605 if (require_type (dtp, BT_REAL, type, f))
1606 return;
1607 write_e (dtp, f, p, kind);
1608 break;
1610 case FMT_EN:
1611 if (n == 0)
1612 goto need_data;
1613 if (require_type (dtp, BT_REAL, type, f))
1614 return;
1615 write_en (dtp, f, p, kind);
1616 break;
1618 case FMT_ES:
1619 if (n == 0)
1620 goto need_data;
1621 if (require_type (dtp, BT_REAL, type, f))
1622 return;
1623 write_es (dtp, f, p, kind);
1624 break;
1626 case FMT_F:
1627 if (n == 0)
1628 goto need_data;
1629 if (require_type (dtp, BT_REAL, type, f))
1630 return;
1631 write_f (dtp, f, p, kind);
1632 break;
1634 case FMT_G:
1635 if (n == 0)
1636 goto need_data;
1637 switch (type)
1639 case BT_INTEGER:
1640 write_i (dtp, f, p, kind);
1641 break;
1642 case BT_LOGICAL:
1643 write_l (dtp, f, p, kind);
1644 break;
1645 case BT_CHARACTER:
1646 if (kind == 4)
1647 write_a_char4 (dtp, f, p, size);
1648 else
1649 write_a (dtp, f, p, size);
1650 break;
1651 case BT_REAL:
1652 if (f->u.real.w == 0)
1653 write_real_g0 (dtp, p, kind, f->u.real.d);
1654 else
1655 write_d (dtp, f, p, kind);
1656 break;
1657 default:
1658 internal_error (&dtp->common,
1659 "formatted_transfer(): Bad type");
1661 break;
1663 case FMT_STRING:
1664 consume_data_flag = 0;
1665 write_constant_string (dtp, f);
1666 break;
1668 /* Format codes that don't transfer data. */
1669 case FMT_X:
1670 case FMT_TR:
1671 consume_data_flag = 0;
1673 dtp->u.p.skips += f->u.n;
1674 pos = bytes_used + dtp->u.p.skips - 1;
1675 dtp->u.p.pending_spaces = pos - dtp->u.p.max_pos + 1;
1676 /* Writes occur just before the switch on f->format, above, so
1677 that trailing blanks are suppressed, unless we are doing a
1678 non-advancing write in which case we want to output the blanks
1679 now. */
1680 if (dtp->u.p.advance_status == ADVANCE_NO)
1682 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
1683 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1685 break;
1687 case FMT_TL:
1688 case FMT_T:
1689 consume_data_flag = 0;
1691 if (f->format == FMT_TL)
1694 /* Handle the special case when no bytes have been used yet.
1695 Cannot go below zero. */
1696 if (bytes_used == 0)
1698 dtp->u.p.pending_spaces -= f->u.n;
1699 dtp->u.p.skips -= f->u.n;
1700 dtp->u.p.skips = dtp->u.p.skips < 0 ? 0 : dtp->u.p.skips;
1703 pos = bytes_used - f->u.n;
1705 else /* FMT_T */
1706 pos = f->u.n - dtp->u.p.pending_spaces - 1;
1708 /* Standard 10.6.1.1: excessive left tabbing is reset to the
1709 left tab limit. We do not check if the position has gone
1710 beyond the end of record because a subsequent tab could
1711 bring us back again. */
1712 pos = pos < 0 ? 0 : pos;
1714 dtp->u.p.skips = dtp->u.p.skips + pos - bytes_used;
1715 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces
1716 + pos - dtp->u.p.max_pos;
1717 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces < 0
1718 ? 0 : dtp->u.p.pending_spaces;
1719 break;
1721 case FMT_S:
1722 consume_data_flag = 0;
1723 dtp->u.p.sign_status = SIGN_S;
1724 break;
1726 case FMT_SS:
1727 consume_data_flag = 0;
1728 dtp->u.p.sign_status = SIGN_SS;
1729 break;
1731 case FMT_SP:
1732 consume_data_flag = 0;
1733 dtp->u.p.sign_status = SIGN_SP;
1734 break;
1736 case FMT_BN:
1737 consume_data_flag = 0 ;
1738 dtp->u.p.blank_status = BLANK_NULL;
1739 break;
1741 case FMT_BZ:
1742 consume_data_flag = 0;
1743 dtp->u.p.blank_status = BLANK_ZERO;
1744 break;
1746 case FMT_DC:
1747 consume_data_flag = 0;
1748 dtp->u.p.current_unit->decimal_status = DECIMAL_COMMA;
1749 break;
1751 case FMT_DP:
1752 consume_data_flag = 0;
1753 dtp->u.p.current_unit->decimal_status = DECIMAL_POINT;
1754 break;
1756 case FMT_RC:
1757 consume_data_flag = 0;
1758 dtp->u.p.current_unit->round_status = ROUND_COMPATIBLE;
1759 break;
1761 case FMT_RD:
1762 consume_data_flag = 0;
1763 dtp->u.p.current_unit->round_status = ROUND_DOWN;
1764 break;
1766 case FMT_RN:
1767 consume_data_flag = 0;
1768 dtp->u.p.current_unit->round_status = ROUND_NEAREST;
1769 break;
1771 case FMT_RP:
1772 consume_data_flag = 0;
1773 dtp->u.p.current_unit->round_status = ROUND_PROCDEFINED;
1774 break;
1776 case FMT_RU:
1777 consume_data_flag = 0;
1778 dtp->u.p.current_unit->round_status = ROUND_UP;
1779 break;
1781 case FMT_RZ:
1782 consume_data_flag = 0;
1783 dtp->u.p.current_unit->round_status = ROUND_ZERO;
1784 break;
1786 case FMT_P:
1787 consume_data_flag = 0;
1788 dtp->u.p.scale_factor = f->u.k;
1789 break;
1791 case FMT_DOLLAR:
1792 consume_data_flag = 0;
1793 dtp->u.p.seen_dollar = 1;
1794 break;
1796 case FMT_SLASH:
1797 consume_data_flag = 0;
1798 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1799 next_record (dtp, 0);
1800 break;
1802 case FMT_COLON:
1803 /* A colon descriptor causes us to exit this loop (in
1804 particular preventing another / descriptor from being
1805 processed) unless there is another data item to be
1806 transferred. */
1807 consume_data_flag = 0;
1808 if (n == 0)
1809 return;
1810 break;
1812 default:
1813 internal_error (&dtp->common, "Bad format node");
1816 /* Adjust the item count and data pointer. */
1818 if ((consume_data_flag > 0) && (n > 0))
1820 n--;
1821 p = ((char *) p) + size;
1824 pos = (int)(dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left);
1825 dtp->u.p.max_pos = (dtp->u.p.max_pos > pos) ? dtp->u.p.max_pos : pos;
1828 return;
1830 /* Come here when we need a data descriptor but don't have one. We
1831 push the current format node back onto the input, then return and
1832 let the user program call us back with the data. */
1833 need_data:
1834 unget_format (dtp, f);
1837 /* This function is first called from data_init_transfer to initiate the loop
1838 over each item in the format, transferring data as required. Subsequent
1839 calls to this function occur for each data item foound in the READ/WRITE
1840 statement. The item_count is incremented for each call. Since the first
1841 call is from data_transfer_init, the item_count is always one greater than
1842 the actual count number of the item being transferred. */
1844 static void
1845 formatted_transfer (st_parameter_dt *dtp, bt type, void *p, int kind,
1846 size_t size, size_t nelems)
1848 size_t elem;
1849 char *tmp;
1851 tmp = (char *) p;
1852 size_t stride = type == BT_CHARACTER ?
1853 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
1854 if (dtp->u.p.mode == READING)
1856 /* Big loop over all the elements. */
1857 for (elem = 0; elem < nelems; elem++)
1859 dtp->u.p.item_count++;
1860 formatted_transfer_scalar_read (dtp, type, tmp + stride*elem, kind, size);
1863 else
1865 /* Big loop over all the elements. */
1866 for (elem = 0; elem < nelems; elem++)
1868 dtp->u.p.item_count++;
1869 formatted_transfer_scalar_write (dtp, type, tmp + stride*elem, kind, size);
1875 /* Data transfer entry points. The type of the data entity is
1876 implicit in the subroutine call. This prevents us from having to
1877 share a common enum with the compiler. */
1879 void
1880 transfer_integer (st_parameter_dt *dtp, void *p, int kind)
1882 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1883 return;
1884 dtp->u.p.transfer (dtp, BT_INTEGER, p, kind, kind, 1);
1887 void
1888 transfer_integer_write (st_parameter_dt *dtp, void *p, int kind)
1890 transfer_integer (dtp, p, kind);
1893 void
1894 transfer_real (st_parameter_dt *dtp, void *p, int kind)
1896 size_t size;
1897 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1898 return;
1899 size = size_from_real_kind (kind);
1900 dtp->u.p.transfer (dtp, BT_REAL, p, kind, size, 1);
1903 void
1904 transfer_real_write (st_parameter_dt *dtp, void *p, int kind)
1906 transfer_real (dtp, p, kind);
1909 void
1910 transfer_logical (st_parameter_dt *dtp, void *p, int kind)
1912 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1913 return;
1914 dtp->u.p.transfer (dtp, BT_LOGICAL, p, kind, kind, 1);
1917 void
1918 transfer_logical_write (st_parameter_dt *dtp, void *p, int kind)
1920 transfer_logical (dtp, p, kind);
1923 void
1924 transfer_character (st_parameter_dt *dtp, void *p, int len)
1926 static char *empty_string[0];
1928 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1929 return;
1931 /* Strings of zero length can have p == NULL, which confuses the
1932 transfer routines into thinking we need more data elements. To avoid
1933 this, we give them a nice pointer. */
1934 if (len == 0 && p == NULL)
1935 p = empty_string;
1937 /* Set kind here to 1. */
1938 dtp->u.p.transfer (dtp, BT_CHARACTER, p, 1, len, 1);
1941 void
1942 transfer_character_write (st_parameter_dt *dtp, void *p, int len)
1944 transfer_character (dtp, p, len);
1947 void
1948 transfer_character_wide (st_parameter_dt *dtp, void *p, int len, int kind)
1950 static char *empty_string[0];
1952 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1953 return;
1955 /* Strings of zero length can have p == NULL, which confuses the
1956 transfer routines into thinking we need more data elements. To avoid
1957 this, we give them a nice pointer. */
1958 if (len == 0 && p == NULL)
1959 p = empty_string;
1961 /* Here we pass the actual kind value. */
1962 dtp->u.p.transfer (dtp, BT_CHARACTER, p, kind, len, 1);
1965 void
1966 transfer_character_wide_write (st_parameter_dt *dtp, void *p, int len, int kind)
1968 transfer_character_wide (dtp, p, len, kind);
1971 void
1972 transfer_complex (st_parameter_dt *dtp, void *p, int kind)
1974 size_t size;
1975 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1976 return;
1977 size = size_from_complex_kind (kind);
1978 dtp->u.p.transfer (dtp, BT_COMPLEX, p, kind, size, 1);
1981 void
1982 transfer_complex_write (st_parameter_dt *dtp, void *p, int kind)
1984 transfer_complex (dtp, p, kind);
1987 void
1988 transfer_array (st_parameter_dt *dtp, gfc_array_char *desc, int kind,
1989 gfc_charlen_type charlen)
1991 index_type count[GFC_MAX_DIMENSIONS];
1992 index_type extent[GFC_MAX_DIMENSIONS];
1993 index_type stride[GFC_MAX_DIMENSIONS];
1994 index_type stride0, rank, size, n;
1995 size_t tsize;
1996 char *data;
1997 bt iotype;
1999 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2000 return;
2002 iotype = (bt) GFC_DESCRIPTOR_TYPE (desc);
2003 size = iotype == BT_CHARACTER ? charlen : GFC_DESCRIPTOR_SIZE (desc);
2005 rank = GFC_DESCRIPTOR_RANK (desc);
2006 for (n = 0; n < rank; n++)
2008 count[n] = 0;
2009 stride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(desc,n);
2010 extent[n] = GFC_DESCRIPTOR_EXTENT(desc,n);
2012 /* If the extent of even one dimension is zero, then the entire
2013 array section contains zero elements, so we return after writing
2014 a zero array record. */
2015 if (extent[n] <= 0)
2017 data = NULL;
2018 tsize = 0;
2019 dtp->u.p.transfer (dtp, iotype, data, kind, size, tsize);
2020 return;
2024 stride0 = stride[0];
2026 /* If the innermost dimension has a stride of 1, we can do the transfer
2027 in contiguous chunks. */
2028 if (stride0 == size)
2029 tsize = extent[0];
2030 else
2031 tsize = 1;
2033 data = GFC_DESCRIPTOR_DATA (desc);
2035 while (data)
2037 dtp->u.p.transfer (dtp, iotype, data, kind, size, tsize);
2038 data += stride0 * tsize;
2039 count[0] += tsize;
2040 n = 0;
2041 while (count[n] == extent[n])
2043 count[n] = 0;
2044 data -= stride[n] * extent[n];
2045 n++;
2046 if (n == rank)
2048 data = NULL;
2049 break;
2051 else
2053 count[n]++;
2054 data += stride[n];
2060 void
2061 transfer_array_write (st_parameter_dt *dtp, gfc_array_char *desc, int kind,
2062 gfc_charlen_type charlen)
2064 transfer_array (dtp, desc, kind, charlen);
2067 /* Preposition a sequential unformatted file while reading. */
2069 static void
2070 us_read (st_parameter_dt *dtp, int continued)
2072 ssize_t n, nr;
2073 GFC_INTEGER_4 i4;
2074 GFC_INTEGER_8 i8;
2075 gfc_offset i;
2077 if (compile_options.record_marker == 0)
2078 n = sizeof (GFC_INTEGER_4);
2079 else
2080 n = compile_options.record_marker;
2082 nr = sread (dtp->u.p.current_unit->s, &i, n);
2083 if (unlikely (nr < 0))
2085 generate_error (&dtp->common, LIBERROR_BAD_US, NULL);
2086 return;
2088 else if (nr == 0)
2090 hit_eof (dtp);
2091 return; /* end of file */
2093 else if (unlikely (n != nr))
2095 generate_error (&dtp->common, LIBERROR_BAD_US, NULL);
2096 return;
2099 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
2100 if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE))
2102 switch (nr)
2104 case sizeof(GFC_INTEGER_4):
2105 memcpy (&i4, &i, sizeof (i4));
2106 i = i4;
2107 break;
2109 case sizeof(GFC_INTEGER_8):
2110 memcpy (&i8, &i, sizeof (i8));
2111 i = i8;
2112 break;
2114 default:
2115 runtime_error ("Illegal value for record marker");
2116 break;
2119 else
2120 switch (nr)
2122 case sizeof(GFC_INTEGER_4):
2123 reverse_memcpy (&i4, &i, sizeof (i4));
2124 i = i4;
2125 break;
2127 case sizeof(GFC_INTEGER_8):
2128 reverse_memcpy (&i8, &i, sizeof (i8));
2129 i = i8;
2130 break;
2132 default:
2133 runtime_error ("Illegal value for record marker");
2134 break;
2137 if (i >= 0)
2139 dtp->u.p.current_unit->bytes_left_subrecord = i;
2140 dtp->u.p.current_unit->continued = 0;
2142 else
2144 dtp->u.p.current_unit->bytes_left_subrecord = -i;
2145 dtp->u.p.current_unit->continued = 1;
2148 if (! continued)
2149 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2153 /* Preposition a sequential unformatted file while writing. This
2154 amount to writing a bogus length that will be filled in later. */
2156 static void
2157 us_write (st_parameter_dt *dtp, int continued)
2159 ssize_t nbytes;
2160 gfc_offset dummy;
2162 dummy = 0;
2164 if (compile_options.record_marker == 0)
2165 nbytes = sizeof (GFC_INTEGER_4);
2166 else
2167 nbytes = compile_options.record_marker ;
2169 if (swrite (dtp->u.p.current_unit->s, &dummy, nbytes) != nbytes)
2170 generate_error (&dtp->common, LIBERROR_OS, NULL);
2172 /* For sequential unformatted, if RECL= was not specified in the OPEN
2173 we write until we have more bytes than can fit in the subrecord
2174 markers, then we write a new subrecord. */
2176 dtp->u.p.current_unit->bytes_left_subrecord =
2177 dtp->u.p.current_unit->recl_subrecord;
2178 dtp->u.p.current_unit->continued = continued;
2182 /* Position to the next record prior to transfer. We are assumed to
2183 be before the next record. We also calculate the bytes in the next
2184 record. */
2186 static void
2187 pre_position (st_parameter_dt *dtp)
2189 if (dtp->u.p.current_unit->current_record)
2190 return; /* Already positioned. */
2192 switch (current_mode (dtp))
2194 case FORMATTED_STREAM:
2195 case UNFORMATTED_STREAM:
2196 /* There are no records with stream I/O. If the position was specified
2197 data_transfer_init has already positioned the file. If no position
2198 was specified, we continue from where we last left off. I.e.
2199 there is nothing to do here. */
2200 break;
2202 case UNFORMATTED_SEQUENTIAL:
2203 if (dtp->u.p.mode == READING)
2204 us_read (dtp, 0);
2205 else
2206 us_write (dtp, 0);
2208 break;
2210 case FORMATTED_SEQUENTIAL:
2211 case FORMATTED_DIRECT:
2212 case UNFORMATTED_DIRECT:
2213 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2214 break;
2217 dtp->u.p.current_unit->current_record = 1;
2221 /* Initialize things for a data transfer. This code is common for
2222 both reading and writing. */
2224 static void
2225 data_transfer_init (st_parameter_dt *dtp, int read_flag)
2227 unit_flags u_flags; /* Used for creating a unit if needed. */
2228 GFC_INTEGER_4 cf = dtp->common.flags;
2229 namelist_info *ionml;
2231 ionml = ((cf & IOPARM_DT_IONML_SET) != 0) ? dtp->u.p.ionml : NULL;
2233 memset (&dtp->u.p, 0, sizeof (dtp->u.p));
2235 dtp->u.p.ionml = ionml;
2236 dtp->u.p.mode = read_flag ? READING : WRITING;
2238 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2239 return;
2241 if ((cf & IOPARM_DT_HAS_SIZE) != 0)
2242 dtp->u.p.size_used = 0; /* Initialize the count. */
2244 dtp->u.p.current_unit = get_unit (dtp, 1);
2245 if (dtp->u.p.current_unit->s == NULL)
2246 { /* Open the unit with some default flags. */
2247 st_parameter_open opp;
2248 unit_convert conv;
2250 if (dtp->common.unit < 0)
2252 close_unit (dtp->u.p.current_unit);
2253 dtp->u.p.current_unit = NULL;
2254 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2255 "Bad unit number in statement");
2256 return;
2258 memset (&u_flags, '\0', sizeof (u_flags));
2259 u_flags.access = ACCESS_SEQUENTIAL;
2260 u_flags.action = ACTION_READWRITE;
2262 /* Is it unformatted? */
2263 if (!(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT
2264 | IOPARM_DT_IONML_SET)))
2265 u_flags.form = FORM_UNFORMATTED;
2266 else
2267 u_flags.form = FORM_UNSPECIFIED;
2269 u_flags.delim = DELIM_UNSPECIFIED;
2270 u_flags.blank = BLANK_UNSPECIFIED;
2271 u_flags.pad = PAD_UNSPECIFIED;
2272 u_flags.decimal = DECIMAL_UNSPECIFIED;
2273 u_flags.encoding = ENCODING_UNSPECIFIED;
2274 u_flags.async = ASYNC_UNSPECIFIED;
2275 u_flags.round = ROUND_UNSPECIFIED;
2276 u_flags.sign = SIGN_UNSPECIFIED;
2278 u_flags.status = STATUS_UNKNOWN;
2280 conv = get_unformatted_convert (dtp->common.unit);
2282 if (conv == GFC_CONVERT_NONE)
2283 conv = compile_options.convert;
2285 /* We use big_endian, which is 0 on little-endian machines
2286 and 1 on big-endian machines. */
2287 switch (conv)
2289 case GFC_CONVERT_NATIVE:
2290 case GFC_CONVERT_SWAP:
2291 break;
2293 case GFC_CONVERT_BIG:
2294 conv = big_endian ? GFC_CONVERT_NATIVE : GFC_CONVERT_SWAP;
2295 break;
2297 case GFC_CONVERT_LITTLE:
2298 conv = big_endian ? GFC_CONVERT_SWAP : GFC_CONVERT_NATIVE;
2299 break;
2301 default:
2302 internal_error (&opp.common, "Illegal value for CONVERT");
2303 break;
2306 u_flags.convert = conv;
2308 opp.common = dtp->common;
2309 opp.common.flags &= IOPARM_COMMON_MASK;
2310 dtp->u.p.current_unit = new_unit (&opp, dtp->u.p.current_unit, &u_flags);
2311 dtp->common.flags &= ~IOPARM_COMMON_MASK;
2312 dtp->common.flags |= (opp.common.flags & IOPARM_COMMON_MASK);
2313 if (dtp->u.p.current_unit == NULL)
2314 return;
2317 /* Check the action. */
2319 if (read_flag && dtp->u.p.current_unit->flags.action == ACTION_WRITE)
2321 generate_error (&dtp->common, LIBERROR_BAD_ACTION,
2322 "Cannot read from file opened for WRITE");
2323 return;
2326 if (!read_flag && dtp->u.p.current_unit->flags.action == ACTION_READ)
2328 generate_error (&dtp->common, LIBERROR_BAD_ACTION,
2329 "Cannot write to file opened for READ");
2330 return;
2333 dtp->u.p.first_item = 1;
2335 /* Check the format. */
2337 if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
2338 parse_format (dtp);
2340 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED
2341 && (cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
2342 != 0)
2344 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2345 "Format present for UNFORMATTED data transfer");
2346 return;
2349 if ((cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0 && dtp->u.p.ionml != NULL)
2351 if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
2352 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2353 "A format cannot be specified with a namelist");
2355 else if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
2356 !(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT)))
2358 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2359 "Missing format for FORMATTED data transfer");
2362 if (is_internal_unit (dtp)
2363 && dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
2365 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2366 "Internal file cannot be accessed by UNFORMATTED "
2367 "data transfer");
2368 return;
2371 /* Check the record or position number. */
2373 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT
2374 && (cf & IOPARM_DT_HAS_REC) == 0)
2376 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
2377 "Direct access data transfer requires record number");
2378 return;
2381 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
2383 if ((cf & IOPARM_DT_HAS_REC) != 0)
2385 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2386 "Record number not allowed for sequential access "
2387 "data transfer");
2388 return;
2391 if (dtp->u.p.current_unit->endfile == AFTER_ENDFILE)
2393 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2394 "Sequential READ or WRITE not allowed after "
2395 "EOF marker, possibly use REWIND or BACKSPACE");
2396 return;
2400 /* Process the ADVANCE option. */
2402 dtp->u.p.advance_status
2403 = !(cf & IOPARM_DT_HAS_ADVANCE) ? ADVANCE_UNSPECIFIED :
2404 find_option (&dtp->common, dtp->advance, dtp->advance_len, advance_opt,
2405 "Bad ADVANCE parameter in data transfer statement");
2407 if (dtp->u.p.advance_status != ADVANCE_UNSPECIFIED)
2409 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
2411 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2412 "ADVANCE specification conflicts with sequential "
2413 "access");
2414 return;
2417 if (is_internal_unit (dtp))
2419 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2420 "ADVANCE specification conflicts with internal file");
2421 return;
2424 if ((cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
2425 != IOPARM_DT_HAS_FORMAT)
2427 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2428 "ADVANCE specification requires an explicit format");
2429 return;
2433 if (read_flag)
2435 dtp->u.p.current_unit->previous_nonadvancing_write = 0;
2437 if ((cf & IOPARM_EOR) != 0 && dtp->u.p.advance_status != ADVANCE_NO)
2439 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
2440 "EOR specification requires an ADVANCE specification "
2441 "of NO");
2442 return;
2445 if ((cf & IOPARM_DT_HAS_SIZE) != 0
2446 && dtp->u.p.advance_status != ADVANCE_NO)
2448 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
2449 "SIZE specification requires an ADVANCE "
2450 "specification of NO");
2451 return;
2454 else
2455 { /* Write constraints. */
2456 if ((cf & IOPARM_END) != 0)
2458 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2459 "END specification cannot appear in a write "
2460 "statement");
2461 return;
2464 if ((cf & IOPARM_EOR) != 0)
2466 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2467 "EOR specification cannot appear in a write "
2468 "statement");
2469 return;
2472 if ((cf & IOPARM_DT_HAS_SIZE) != 0)
2474 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2475 "SIZE specification cannot appear in a write "
2476 "statement");
2477 return;
2481 if (dtp->u.p.advance_status == ADVANCE_UNSPECIFIED)
2482 dtp->u.p.advance_status = ADVANCE_YES;
2484 /* Check the decimal mode. */
2485 dtp->u.p.current_unit->decimal_status
2486 = !(cf & IOPARM_DT_HAS_DECIMAL) ? DECIMAL_UNSPECIFIED :
2487 find_option (&dtp->common, dtp->decimal, dtp->decimal_len,
2488 decimal_opt, "Bad DECIMAL parameter in data transfer "
2489 "statement");
2491 if (dtp->u.p.current_unit->decimal_status == DECIMAL_UNSPECIFIED)
2492 dtp->u.p.current_unit->decimal_status = dtp->u.p.current_unit->flags.decimal;
2494 /* Check the round mode. */
2495 dtp->u.p.current_unit->round_status
2496 = !(cf & IOPARM_DT_HAS_ROUND) ? ROUND_UNSPECIFIED :
2497 find_option (&dtp->common, dtp->round, dtp->round_len,
2498 round_opt, "Bad ROUND parameter in data transfer "
2499 "statement");
2501 if (dtp->u.p.current_unit->round_status == ROUND_UNSPECIFIED)
2502 dtp->u.p.current_unit->round_status = dtp->u.p.current_unit->flags.round;
2504 /* Check the sign mode. */
2505 dtp->u.p.sign_status
2506 = !(cf & IOPARM_DT_HAS_SIGN) ? SIGN_UNSPECIFIED :
2507 find_option (&dtp->common, dtp->sign, dtp->sign_len, sign_opt,
2508 "Bad SIGN parameter in data transfer statement");
2510 if (dtp->u.p.sign_status == SIGN_UNSPECIFIED)
2511 dtp->u.p.sign_status = dtp->u.p.current_unit->flags.sign;
2513 /* Check the blank mode. */
2514 dtp->u.p.blank_status
2515 = !(cf & IOPARM_DT_HAS_BLANK) ? BLANK_UNSPECIFIED :
2516 find_option (&dtp->common, dtp->blank, dtp->blank_len,
2517 blank_opt,
2518 "Bad BLANK parameter in data transfer statement");
2520 if (dtp->u.p.blank_status == BLANK_UNSPECIFIED)
2521 dtp->u.p.blank_status = dtp->u.p.current_unit->flags.blank;
2523 /* Check the delim mode. */
2524 dtp->u.p.current_unit->delim_status
2525 = !(cf & IOPARM_DT_HAS_DELIM) ? DELIM_UNSPECIFIED :
2526 find_option (&dtp->common, dtp->delim, dtp->delim_len,
2527 delim_opt, "Bad DELIM parameter in data transfer statement");
2529 if (dtp->u.p.current_unit->delim_status == DELIM_UNSPECIFIED)
2530 dtp->u.p.current_unit->delim_status = dtp->u.p.current_unit->flags.delim;
2532 /* Check the pad mode. */
2533 dtp->u.p.current_unit->pad_status
2534 = !(cf & IOPARM_DT_HAS_PAD) ? PAD_UNSPECIFIED :
2535 find_option (&dtp->common, dtp->pad, dtp->pad_len, pad_opt,
2536 "Bad PAD parameter in data transfer statement");
2538 if (dtp->u.p.current_unit->pad_status == PAD_UNSPECIFIED)
2539 dtp->u.p.current_unit->pad_status = dtp->u.p.current_unit->flags.pad;
2541 /* Check to see if we might be reading what we wrote before */
2543 if (dtp->u.p.mode != dtp->u.p.current_unit->mode
2544 && !is_internal_unit (dtp))
2546 int pos = fbuf_reset (dtp->u.p.current_unit);
2547 if (pos != 0)
2548 sseek (dtp->u.p.current_unit->s, pos, SEEK_CUR);
2549 sflush(dtp->u.p.current_unit->s);
2552 /* Check the POS= specifier: that it is in range and that it is used with a
2553 unit that has been connected for STREAM access. F2003 9.5.1.10. */
2555 if (((cf & IOPARM_DT_HAS_POS) != 0))
2557 if (is_stream_io (dtp))
2560 if (dtp->pos <= 0)
2562 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2563 "POS=specifier must be positive");
2564 return;
2567 if (dtp->pos >= dtp->u.p.current_unit->maxrec)
2569 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2570 "POS=specifier too large");
2571 return;
2574 dtp->rec = dtp->pos;
2576 if (dtp->u.p.mode == READING)
2578 /* Reset the endfile flag; if we hit EOF during reading
2579 we'll set the flag and generate an error at that point
2580 rather than worrying about it here. */
2581 dtp->u.p.current_unit->endfile = NO_ENDFILE;
2584 if (dtp->pos != dtp->u.p.current_unit->strm_pos)
2586 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
2587 if (sseek (dtp->u.p.current_unit->s, dtp->pos - 1, SEEK_SET) < 0)
2589 generate_error (&dtp->common, LIBERROR_OS, NULL);
2590 return;
2592 dtp->u.p.current_unit->strm_pos = dtp->pos;
2595 else
2597 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2598 "POS=specifier not allowed, "
2599 "Try OPEN with ACCESS='stream'");
2600 return;
2605 /* Sanity checks on the record number. */
2606 if ((cf & IOPARM_DT_HAS_REC) != 0)
2608 if (dtp->rec <= 0)
2610 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2611 "Record number must be positive");
2612 return;
2615 if (dtp->rec >= dtp->u.p.current_unit->maxrec)
2617 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2618 "Record number too large");
2619 return;
2622 /* Make sure format buffer is reset. */
2623 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED)
2624 fbuf_reset (dtp->u.p.current_unit);
2627 /* Check whether the record exists to be read. Only
2628 a partial record needs to exist. */
2630 if (dtp->u.p.mode == READING && (dtp->rec - 1)
2631 * dtp->u.p.current_unit->recl >= file_length (dtp->u.p.current_unit->s))
2633 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2634 "Non-existing record number");
2635 return;
2638 /* Position the file. */
2639 if (sseek (dtp->u.p.current_unit->s, (gfc_offset) (dtp->rec - 1)
2640 * dtp->u.p.current_unit->recl, SEEK_SET) < 0)
2642 generate_error (&dtp->common, LIBERROR_OS, NULL);
2643 return;
2646 /* TODO: This is required to maintain compatibility between
2647 4.3 and 4.4 runtime. Remove when ABI changes from 4.3 */
2649 if (is_stream_io (dtp))
2650 dtp->u.p.current_unit->strm_pos = dtp->rec;
2652 /* TODO: Un-comment this code when ABI changes from 4.3.
2653 if (dtp->u.p.current_unit->flags.access == ACCESS_STREAM)
2655 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2656 "Record number not allowed for stream access "
2657 "data transfer");
2658 return;
2659 } */
2662 /* Bugware for badly written mixed C-Fortran I/O. */
2663 if (!is_internal_unit (dtp))
2664 flush_if_preconnected(dtp->u.p.current_unit->s);
2666 dtp->u.p.current_unit->mode = dtp->u.p.mode;
2668 /* Set the maximum position reached from the previous I/O operation. This
2669 could be greater than zero from a previous non-advancing write. */
2670 dtp->u.p.max_pos = dtp->u.p.current_unit->saved_pos;
2672 pre_position (dtp);
2675 /* Set up the subroutine that will handle the transfers. */
2677 if (read_flag)
2679 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
2680 dtp->u.p.transfer = unformatted_read;
2681 else
2683 if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
2685 dtp->u.p.last_char = EOF - 1;
2686 dtp->u.p.transfer = list_formatted_read;
2688 else
2689 dtp->u.p.transfer = formatted_transfer;
2692 else
2694 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
2695 dtp->u.p.transfer = unformatted_write;
2696 else
2698 if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
2699 dtp->u.p.transfer = list_formatted_write;
2700 else
2701 dtp->u.p.transfer = formatted_transfer;
2705 /* Make sure that we don't do a read after a nonadvancing write. */
2707 if (read_flag)
2709 if (dtp->u.p.current_unit->read_bad && !is_stream_io (dtp))
2711 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2712 "Cannot READ after a nonadvancing WRITE");
2713 return;
2716 else
2718 if (dtp->u.p.advance_status == ADVANCE_YES && !dtp->u.p.seen_dollar)
2719 dtp->u.p.current_unit->read_bad = 1;
2722 /* Start the data transfer if we are doing a formatted transfer. */
2723 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
2724 && ((cf & (IOPARM_DT_LIST_FORMAT | IOPARM_DT_HAS_NAMELIST_NAME)) == 0)
2725 && dtp->u.p.ionml == NULL)
2726 formatted_transfer (dtp, 0, NULL, 0, 0, 1);
2729 /* Initialize an array_loop_spec given the array descriptor. The function
2730 returns the index of the last element of the array, and also returns
2731 starting record, where the first I/O goes to (necessary in case of
2732 negative strides). */
2734 gfc_offset
2735 init_loop_spec (gfc_array_char *desc, array_loop_spec *ls,
2736 gfc_offset *start_record)
2738 int rank = GFC_DESCRIPTOR_RANK(desc);
2739 int i;
2740 gfc_offset index;
2741 int empty;
2743 empty = 0;
2744 index = 1;
2745 *start_record = 0;
2747 for (i=0; i<rank; i++)
2749 ls[i].idx = GFC_DESCRIPTOR_LBOUND(desc,i);
2750 ls[i].start = GFC_DESCRIPTOR_LBOUND(desc,i);
2751 ls[i].end = GFC_DESCRIPTOR_UBOUND(desc,i);
2752 ls[i].step = GFC_DESCRIPTOR_STRIDE(desc,i);
2753 empty = empty || (GFC_DESCRIPTOR_UBOUND(desc,i)
2754 < GFC_DESCRIPTOR_LBOUND(desc,i));
2756 if (GFC_DESCRIPTOR_STRIDE(desc,i) > 0)
2758 index += (GFC_DESCRIPTOR_EXTENT(desc,i) - 1)
2759 * GFC_DESCRIPTOR_STRIDE(desc,i);
2761 else
2763 index -= (GFC_DESCRIPTOR_EXTENT(desc,i) - 1)
2764 * GFC_DESCRIPTOR_STRIDE(desc,i);
2765 *start_record -= (GFC_DESCRIPTOR_EXTENT(desc,i) - 1)
2766 * GFC_DESCRIPTOR_STRIDE(desc,i);
2770 if (empty)
2771 return 0;
2772 else
2773 return index;
2776 /* Determine the index to the next record in an internal unit array by
2777 by incrementing through the array_loop_spec. */
2779 gfc_offset
2780 next_array_record (st_parameter_dt *dtp, array_loop_spec *ls, int *finished)
2782 int i, carry;
2783 gfc_offset index;
2785 carry = 1;
2786 index = 0;
2788 for (i = 0; i < dtp->u.p.current_unit->rank; i++)
2790 if (carry)
2792 ls[i].idx++;
2793 if (ls[i].idx > ls[i].end)
2795 ls[i].idx = ls[i].start;
2796 carry = 1;
2798 else
2799 carry = 0;
2801 index = index + (ls[i].idx - ls[i].start) * ls[i].step;
2804 *finished = carry;
2806 return index;
2811 /* Skip to the end of the current record, taking care of an optional
2812 record marker of size bytes. If the file is not seekable, we
2813 read chunks of size MAX_READ until we get to the right
2814 position. */
2816 static void
2817 skip_record (st_parameter_dt *dtp, ssize_t bytes)
2819 ssize_t rlength, readb;
2820 static const ssize_t MAX_READ = 4096;
2821 char p[MAX_READ];
2823 dtp->u.p.current_unit->bytes_left_subrecord += bytes;
2824 if (dtp->u.p.current_unit->bytes_left_subrecord == 0)
2825 return;
2827 if (is_seekable (dtp->u.p.current_unit->s))
2829 /* Direct access files do not generate END conditions,
2830 only I/O errors. */
2831 if (sseek (dtp->u.p.current_unit->s,
2832 dtp->u.p.current_unit->bytes_left_subrecord, SEEK_CUR) < 0)
2833 generate_error (&dtp->common, LIBERROR_OS, NULL);
2835 dtp->u.p.current_unit->bytes_left_subrecord = 0;
2837 else
2838 { /* Seek by reading data. */
2839 while (dtp->u.p.current_unit->bytes_left_subrecord > 0)
2841 rlength =
2842 (MAX_READ < dtp->u.p.current_unit->bytes_left_subrecord) ?
2843 MAX_READ : dtp->u.p.current_unit->bytes_left_subrecord;
2845 readb = sread (dtp->u.p.current_unit->s, p, rlength);
2846 if (readb < 0)
2848 generate_error (&dtp->common, LIBERROR_OS, NULL);
2849 return;
2852 dtp->u.p.current_unit->bytes_left_subrecord -= readb;
2859 /* Advance to the next record reading unformatted files, taking
2860 care of subrecords. If complete_record is nonzero, we loop
2861 until all subrecords are cleared. */
2863 static void
2864 next_record_r_unf (st_parameter_dt *dtp, int complete_record)
2866 size_t bytes;
2868 bytes = compile_options.record_marker == 0 ?
2869 sizeof (GFC_INTEGER_4) : compile_options.record_marker;
2871 while(1)
2874 /* Skip over tail */
2876 skip_record (dtp, bytes);
2878 if ( ! (complete_record && dtp->u.p.current_unit->continued))
2879 return;
2881 us_read (dtp, 1);
2886 static inline gfc_offset
2887 min_off (gfc_offset a, gfc_offset b)
2889 return (a < b ? a : b);
2893 /* Space to the next record for read mode. */
2895 static void
2896 next_record_r (st_parameter_dt *dtp, int done)
2898 gfc_offset record;
2899 int bytes_left;
2900 char p;
2901 int cc;
2903 switch (current_mode (dtp))
2905 /* No records in unformatted STREAM I/O. */
2906 case UNFORMATTED_STREAM:
2907 return;
2909 case UNFORMATTED_SEQUENTIAL:
2910 next_record_r_unf (dtp, 1);
2911 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2912 break;
2914 case FORMATTED_DIRECT:
2915 case UNFORMATTED_DIRECT:
2916 skip_record (dtp, dtp->u.p.current_unit->bytes_left);
2917 break;
2919 case FORMATTED_STREAM:
2920 case FORMATTED_SEQUENTIAL:
2921 /* read_sf has already terminated input because of an '\n', or
2922 we have hit EOF. */
2923 if (dtp->u.p.sf_seen_eor)
2925 dtp->u.p.sf_seen_eor = 0;
2926 break;
2929 if (is_internal_unit (dtp))
2931 if (is_array_io (dtp))
2933 int finished;
2935 record = next_array_record (dtp, dtp->u.p.current_unit->ls,
2936 &finished);
2937 if (!done && finished)
2938 hit_eof (dtp);
2940 /* Now seek to this record. */
2941 record = record * dtp->u.p.current_unit->recl;
2942 if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
2944 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
2945 break;
2947 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2949 else
2951 bytes_left = (int) dtp->u.p.current_unit->bytes_left;
2952 bytes_left = min_off (bytes_left,
2953 file_length (dtp->u.p.current_unit->s)
2954 - stell (dtp->u.p.current_unit->s));
2955 if (sseek (dtp->u.p.current_unit->s,
2956 bytes_left, SEEK_CUR) < 0)
2958 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
2959 break;
2961 dtp->u.p.current_unit->bytes_left
2962 = dtp->u.p.current_unit->recl;
2964 break;
2966 else
2970 errno = 0;
2971 cc = fbuf_getc (dtp->u.p.current_unit);
2972 if (cc == EOF)
2974 if (errno != 0)
2975 generate_error (&dtp->common, LIBERROR_OS, NULL);
2976 else
2978 if (is_stream_io (dtp)
2979 || dtp->u.p.current_unit->pad_status == PAD_NO
2980 || dtp->u.p.current_unit->bytes_left
2981 == dtp->u.p.current_unit->recl)
2982 hit_eof (dtp);
2984 break;
2987 if (is_stream_io (dtp))
2988 dtp->u.p.current_unit->strm_pos++;
2990 p = (char) cc;
2992 while (p != '\n');
2994 break;
2999 /* Small utility function to write a record marker, taking care of
3000 byte swapping and of choosing the correct size. */
3002 static int
3003 write_us_marker (st_parameter_dt *dtp, const gfc_offset buf)
3005 size_t len;
3006 GFC_INTEGER_4 buf4;
3007 GFC_INTEGER_8 buf8;
3008 char p[sizeof (GFC_INTEGER_8)];
3010 if (compile_options.record_marker == 0)
3011 len = sizeof (GFC_INTEGER_4);
3012 else
3013 len = compile_options.record_marker;
3015 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
3016 if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE))
3018 switch (len)
3020 case sizeof (GFC_INTEGER_4):
3021 buf4 = buf;
3022 return swrite (dtp->u.p.current_unit->s, &buf4, len);
3023 break;
3025 case sizeof (GFC_INTEGER_8):
3026 buf8 = buf;
3027 return swrite (dtp->u.p.current_unit->s, &buf8, len);
3028 break;
3030 default:
3031 runtime_error ("Illegal value for record marker");
3032 break;
3035 else
3037 switch (len)
3039 case sizeof (GFC_INTEGER_4):
3040 buf4 = buf;
3041 reverse_memcpy (p, &buf4, sizeof (GFC_INTEGER_4));
3042 return swrite (dtp->u.p.current_unit->s, p, len);
3043 break;
3045 case sizeof (GFC_INTEGER_8):
3046 buf8 = buf;
3047 reverse_memcpy (p, &buf8, sizeof (GFC_INTEGER_8));
3048 return swrite (dtp->u.p.current_unit->s, p, len);
3049 break;
3051 default:
3052 runtime_error ("Illegal value for record marker");
3053 break;
3059 /* Position to the next (sub)record in write mode for
3060 unformatted sequential files. */
3062 static void
3063 next_record_w_unf (st_parameter_dt *dtp, int next_subrecord)
3065 gfc_offset m, m_write, record_marker;
3067 /* Bytes written. */
3068 m = dtp->u.p.current_unit->recl_subrecord
3069 - dtp->u.p.current_unit->bytes_left_subrecord;
3071 /* Write the length tail. If we finish a record containing
3072 subrecords, we write out the negative length. */
3074 if (dtp->u.p.current_unit->continued)
3075 m_write = -m;
3076 else
3077 m_write = m;
3079 if (unlikely (write_us_marker (dtp, m_write) < 0))
3080 goto io_error;
3082 if (compile_options.record_marker == 0)
3083 record_marker = sizeof (GFC_INTEGER_4);
3084 else
3085 record_marker = compile_options.record_marker;
3087 /* Seek to the head and overwrite the bogus length with the real
3088 length. */
3090 if (unlikely (sseek (dtp->u.p.current_unit->s, - m - 2 * record_marker,
3091 SEEK_CUR) < 0))
3092 goto io_error;
3094 if (next_subrecord)
3095 m_write = -m;
3096 else
3097 m_write = m;
3099 if (unlikely (write_us_marker (dtp, m_write) < 0))
3100 goto io_error;
3102 /* Seek past the end of the current record. */
3104 if (unlikely (sseek (dtp->u.p.current_unit->s, m + record_marker,
3105 SEEK_CUR) < 0))
3106 goto io_error;
3108 return;
3110 io_error:
3111 generate_error (&dtp->common, LIBERROR_OS, NULL);
3112 return;
3117 /* Utility function like memset() but operating on streams. Return
3118 value is same as for POSIX write(). */
3120 static ssize_t
3121 sset (stream * s, int c, ssize_t nbyte)
3123 static const int WRITE_CHUNK = 256;
3124 char p[WRITE_CHUNK];
3125 ssize_t bytes_left, trans;
3127 if (nbyte < WRITE_CHUNK)
3128 memset (p, c, nbyte);
3129 else
3130 memset (p, c, WRITE_CHUNK);
3132 bytes_left = nbyte;
3133 while (bytes_left > 0)
3135 trans = (bytes_left < WRITE_CHUNK) ? bytes_left : WRITE_CHUNK;
3136 trans = swrite (s, p, trans);
3137 if (trans <= 0)
3138 return trans;
3139 bytes_left -= trans;
3142 return nbyte - bytes_left;
3145 static inline void
3146 memset4 (gfc_char4_t *p, gfc_char4_t c, int k)
3148 int j;
3149 for (j = 0; j < k; j++)
3150 *p++ = c;
3153 /* Position to the next record in write mode. */
3155 static void
3156 next_record_w (st_parameter_dt *dtp, int done)
3158 gfc_offset m, record, max_pos;
3159 int length;
3161 /* Zero counters for X- and T-editing. */
3162 max_pos = dtp->u.p.max_pos;
3163 dtp->u.p.max_pos = dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
3165 switch (current_mode (dtp))
3167 /* No records in unformatted STREAM I/O. */
3168 case UNFORMATTED_STREAM:
3169 return;
3171 case FORMATTED_DIRECT:
3172 if (dtp->u.p.current_unit->bytes_left == 0)
3173 break;
3175 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
3176 fbuf_flush (dtp->u.p.current_unit, WRITING);
3177 if (sset (dtp->u.p.current_unit->s, ' ',
3178 dtp->u.p.current_unit->bytes_left)
3179 != dtp->u.p.current_unit->bytes_left)
3180 goto io_error;
3182 break;
3184 case UNFORMATTED_DIRECT:
3185 if (dtp->u.p.current_unit->bytes_left > 0)
3187 length = (int) dtp->u.p.current_unit->bytes_left;
3188 if (sset (dtp->u.p.current_unit->s, 0, length) != length)
3189 goto io_error;
3191 break;
3193 case UNFORMATTED_SEQUENTIAL:
3194 next_record_w_unf (dtp, 0);
3195 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
3196 break;
3198 case FORMATTED_STREAM:
3199 case FORMATTED_SEQUENTIAL:
3201 if (is_internal_unit (dtp))
3203 char *p;
3204 if (is_array_io (dtp))
3206 int finished;
3208 length = (int) dtp->u.p.current_unit->bytes_left;
3210 /* If the farthest position reached is greater than current
3211 position, adjust the position and set length to pad out
3212 whats left. Otherwise just pad whats left.
3213 (for character array unit) */
3214 m = dtp->u.p.current_unit->recl
3215 - dtp->u.p.current_unit->bytes_left;
3216 if (max_pos > m)
3218 length = (int) (max_pos - m);
3219 if (sseek (dtp->u.p.current_unit->s,
3220 length, SEEK_CUR) < 0)
3222 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3223 return;
3225 length = (int) (dtp->u.p.current_unit->recl - max_pos);
3228 p = write_block (dtp, length);
3229 if (p == NULL)
3230 return;
3232 if (unlikely (is_char4_unit (dtp)))
3234 gfc_char4_t *p4 = (gfc_char4_t *) p;
3235 memset4 (p4, ' ', length);
3237 else
3238 memset (p, ' ', length);
3240 /* Now that the current record has been padded out,
3241 determine where the next record in the array is. */
3242 record = next_array_record (dtp, dtp->u.p.current_unit->ls,
3243 &finished);
3244 if (finished)
3245 dtp->u.p.current_unit->endfile = AT_ENDFILE;
3247 /* Now seek to this record */
3248 record = record * dtp->u.p.current_unit->recl;
3250 if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
3252 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3253 return;
3256 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
3258 else
3260 length = 1;
3262 /* If this is the last call to next_record move to the farthest
3263 position reached and set length to pad out the remainder
3264 of the record. (for character scaler unit) */
3265 if (done)
3267 m = dtp->u.p.current_unit->recl
3268 - dtp->u.p.current_unit->bytes_left;
3269 if (max_pos > m)
3271 length = (int) (max_pos - m);
3272 if (sseek (dtp->u.p.current_unit->s,
3273 length, SEEK_CUR) < 0)
3275 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3276 return;
3278 length = (int) (dtp->u.p.current_unit->recl - max_pos);
3280 else
3281 length = (int) dtp->u.p.current_unit->bytes_left;
3283 if (length > 0)
3285 p = write_block (dtp, length);
3286 if (p == NULL)
3287 return;
3289 if (unlikely (is_char4_unit (dtp)))
3291 gfc_char4_t *p4 = (gfc_char4_t *) p;
3292 memset4 (p4, (gfc_char4_t) ' ', length);
3294 else
3295 memset (p, ' ', length);
3299 else
3301 #ifdef HAVE_CRLF
3302 const int len = 2;
3303 #else
3304 const int len = 1;
3305 #endif
3306 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
3307 char * p = fbuf_alloc (dtp->u.p.current_unit, len);
3308 if (!p)
3309 goto io_error;
3310 #ifdef HAVE_CRLF
3311 *(p++) = '\r';
3312 #endif
3313 *p = '\n';
3314 if (is_stream_io (dtp))
3316 dtp->u.p.current_unit->strm_pos += len;
3317 if (dtp->u.p.current_unit->strm_pos
3318 < file_length (dtp->u.p.current_unit->s))
3319 unit_truncate (dtp->u.p.current_unit,
3320 dtp->u.p.current_unit->strm_pos - 1,
3321 &dtp->common);
3325 break;
3327 io_error:
3328 generate_error (&dtp->common, LIBERROR_OS, NULL);
3329 break;
3333 /* Position to the next record, which means moving to the end of the
3334 current record. This can happen under several different
3335 conditions. If the done flag is not set, we get ready to process
3336 the next record. */
3338 void
3339 next_record (st_parameter_dt *dtp, int done)
3341 gfc_offset fp; /* File position. */
3343 dtp->u.p.current_unit->read_bad = 0;
3345 if (dtp->u.p.mode == READING)
3346 next_record_r (dtp, done);
3347 else
3348 next_record_w (dtp, done);
3350 if (!is_stream_io (dtp))
3352 /* Keep position up to date for INQUIRE */
3353 if (done)
3354 update_position (dtp->u.p.current_unit);
3356 dtp->u.p.current_unit->current_record = 0;
3357 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
3359 fp = stell (dtp->u.p.current_unit->s);
3360 /* Calculate next record, rounding up partial records. */
3361 dtp->u.p.current_unit->last_record =
3362 (fp + dtp->u.p.current_unit->recl - 1) /
3363 dtp->u.p.current_unit->recl;
3365 else
3366 dtp->u.p.current_unit->last_record++;
3369 if (!done)
3370 pre_position (dtp);
3372 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
3376 /* Finalize the current data transfer. For a nonadvancing transfer,
3377 this means advancing to the next record. For internal units close the
3378 stream associated with the unit. */
3380 static void
3381 finalize_transfer (st_parameter_dt *dtp)
3383 GFC_INTEGER_4 cf = dtp->common.flags;
3385 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
3386 *dtp->size = dtp->u.p.size_used;
3388 if (dtp->u.p.eor_condition)
3390 generate_error (&dtp->common, LIBERROR_EOR, NULL);
3391 return;
3394 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
3396 if (dtp->u.p.current_unit && current_mode (dtp) == UNFORMATTED_SEQUENTIAL)
3397 dtp->u.p.current_unit->current_record = 0;
3398 return;
3401 if ((dtp->u.p.ionml != NULL)
3402 && (cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0)
3404 if ((cf & IOPARM_DT_NAMELIST_READ_MODE) != 0)
3405 namelist_read (dtp);
3406 else
3407 namelist_write (dtp);
3410 dtp->u.p.transfer = NULL;
3411 if (dtp->u.p.current_unit == NULL)
3412 return;
3414 if ((cf & IOPARM_DT_LIST_FORMAT) != 0 && dtp->u.p.mode == READING)
3416 finish_list_read (dtp);
3417 return;
3420 if (dtp->u.p.mode == WRITING)
3421 dtp->u.p.current_unit->previous_nonadvancing_write
3422 = dtp->u.p.advance_status == ADVANCE_NO;
3424 if (is_stream_io (dtp))
3426 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
3427 && dtp->u.p.advance_status != ADVANCE_NO)
3428 next_record (dtp, 1);
3430 return;
3433 dtp->u.p.current_unit->current_record = 0;
3435 if (!is_internal_unit (dtp) && dtp->u.p.seen_dollar)
3437 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
3438 dtp->u.p.seen_dollar = 0;
3439 return;
3442 /* For non-advancing I/O, save the current maximum position for use in the
3443 next I/O operation if needed. */
3444 if (dtp->u.p.advance_status == ADVANCE_NO)
3446 int bytes_written = (int) (dtp->u.p.current_unit->recl
3447 - dtp->u.p.current_unit->bytes_left);
3448 dtp->u.p.current_unit->saved_pos =
3449 dtp->u.p.max_pos > 0 ? dtp->u.p.max_pos - bytes_written : 0;
3450 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
3451 return;
3453 else if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
3454 && dtp->u.p.mode == WRITING && !is_internal_unit (dtp))
3455 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
3457 dtp->u.p.current_unit->saved_pos = 0;
3459 next_record (dtp, 1);
3462 /* Transfer function for IOLENGTH. It doesn't actually do any
3463 data transfer, it just updates the length counter. */
3465 static void
3466 iolength_transfer (st_parameter_dt *dtp, bt type __attribute__((unused)),
3467 void *dest __attribute__ ((unused)),
3468 int kind __attribute__((unused)),
3469 size_t size, size_t nelems)
3471 if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
3472 *dtp->iolength += (GFC_IO_INT) (size * nelems);
3476 /* Initialize the IOLENGTH data transfer. This function is in essence
3477 a very much simplified version of data_transfer_init(), because it
3478 doesn't have to deal with units at all. */
3480 static void
3481 iolength_transfer_init (st_parameter_dt *dtp)
3483 if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
3484 *dtp->iolength = 0;
3486 memset (&dtp->u.p, 0, sizeof (dtp->u.p));
3488 /* Set up the subroutine that will handle the transfers. */
3490 dtp->u.p.transfer = iolength_transfer;
3494 /* Library entry point for the IOLENGTH form of the INQUIRE
3495 statement. The IOLENGTH form requires no I/O to be performed, but
3496 it must still be a runtime library call so that we can determine
3497 the iolength for dynamic arrays and such. */
3499 extern void st_iolength (st_parameter_dt *);
3500 export_proto(st_iolength);
3502 void
3503 st_iolength (st_parameter_dt *dtp)
3505 library_start (&dtp->common);
3506 iolength_transfer_init (dtp);
3509 extern void st_iolength_done (st_parameter_dt *);
3510 export_proto(st_iolength_done);
3512 void
3513 st_iolength_done (st_parameter_dt *dtp __attribute__((unused)))
3515 free_ionml (dtp);
3516 library_end ();
3520 /* The READ statement. */
3522 extern void st_read (st_parameter_dt *);
3523 export_proto(st_read);
3525 void
3526 st_read (st_parameter_dt *dtp)
3528 library_start (&dtp->common);
3530 data_transfer_init (dtp, 1);
3533 extern void st_read_done (st_parameter_dt *);
3534 export_proto(st_read_done);
3536 void
3537 st_read_done (st_parameter_dt *dtp)
3539 finalize_transfer (dtp);
3540 if (is_internal_unit (dtp) || dtp->u.p.format_not_saved)
3541 free_format_data (dtp->u.p.fmt);
3542 free_ionml (dtp);
3543 if (dtp->u.p.current_unit != NULL)
3544 unlock_unit (dtp->u.p.current_unit);
3546 free_internal_unit (dtp);
3548 library_end ();
3551 extern void st_write (st_parameter_dt *);
3552 export_proto(st_write);
3554 void
3555 st_write (st_parameter_dt *dtp)
3557 library_start (&dtp->common);
3558 data_transfer_init (dtp, 0);
3561 extern void st_write_done (st_parameter_dt *);
3562 export_proto(st_write_done);
3564 void
3565 st_write_done (st_parameter_dt *dtp)
3567 finalize_transfer (dtp);
3569 /* Deal with endfile conditions associated with sequential files. */
3571 if (dtp->u.p.current_unit != NULL
3572 && dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
3573 switch (dtp->u.p.current_unit->endfile)
3575 case AT_ENDFILE: /* Remain at the endfile record. */
3576 break;
3578 case AFTER_ENDFILE:
3579 dtp->u.p.current_unit->endfile = AT_ENDFILE; /* Just at it now. */
3580 break;
3582 case NO_ENDFILE:
3583 /* Get rid of whatever is after this record. */
3584 if (!is_internal_unit (dtp))
3585 unit_truncate (dtp->u.p.current_unit,
3586 stell (dtp->u.p.current_unit->s),
3587 &dtp->common);
3588 dtp->u.p.current_unit->endfile = AT_ENDFILE;
3589 break;
3592 if (is_internal_unit (dtp) || dtp->u.p.format_not_saved)
3593 free_format_data (dtp->u.p.fmt);
3594 free_ionml (dtp);
3595 if (dtp->u.p.current_unit != NULL)
3596 unlock_unit (dtp->u.p.current_unit);
3598 free_internal_unit (dtp);
3600 library_end ();
3604 /* F2003: This is a stub for the runtime portion of the WAIT statement. */
3605 void
3606 st_wait (st_parameter_wait *wtp __attribute__((unused)))
3611 /* Receives the scalar information for namelist objects and stores it
3612 in a linked list of namelist_info types. */
3614 extern void st_set_nml_var (st_parameter_dt *dtp, void *, char *,
3615 GFC_INTEGER_4, gfc_charlen_type, GFC_INTEGER_4);
3616 export_proto(st_set_nml_var);
3619 void
3620 st_set_nml_var (st_parameter_dt *dtp, void * var_addr, char * var_name,
3621 GFC_INTEGER_4 len, gfc_charlen_type string_length,
3622 GFC_INTEGER_4 dtype)
3624 namelist_info *t1 = NULL;
3625 namelist_info *nml;
3626 size_t var_name_len = strlen (var_name);
3628 nml = (namelist_info*) get_mem (sizeof (namelist_info));
3630 nml->mem_pos = var_addr;
3632 nml->var_name = (char*) get_mem (var_name_len + 1);
3633 memcpy (nml->var_name, var_name, var_name_len);
3634 nml->var_name[var_name_len] = '\0';
3636 nml->len = (int) len;
3637 nml->string_length = (index_type) string_length;
3639 nml->var_rank = (int) (dtype & GFC_DTYPE_RANK_MASK);
3640 nml->size = (index_type) (dtype >> GFC_DTYPE_SIZE_SHIFT);
3641 nml->type = (bt) ((dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT);
3643 if (nml->var_rank > 0)
3645 nml->dim = (descriptor_dimension*)
3646 get_mem (nml->var_rank * sizeof (descriptor_dimension));
3647 nml->ls = (array_loop_spec*)
3648 get_mem (nml->var_rank * sizeof (array_loop_spec));
3650 else
3652 nml->dim = NULL;
3653 nml->ls = NULL;
3656 nml->next = NULL;
3658 if ((dtp->common.flags & IOPARM_DT_IONML_SET) == 0)
3660 dtp->common.flags |= IOPARM_DT_IONML_SET;
3661 dtp->u.p.ionml = nml;
3663 else
3665 for (t1 = dtp->u.p.ionml; t1->next; t1 = t1->next);
3666 t1->next = nml;
3670 /* Store the dimensional information for the namelist object. */
3671 extern void st_set_nml_var_dim (st_parameter_dt *, GFC_INTEGER_4,
3672 index_type, index_type,
3673 index_type);
3674 export_proto(st_set_nml_var_dim);
3676 void
3677 st_set_nml_var_dim (st_parameter_dt *dtp, GFC_INTEGER_4 n_dim,
3678 index_type stride, index_type lbound,
3679 index_type ubound)
3681 namelist_info * nml;
3682 int n;
3684 n = (int)n_dim;
3686 for (nml = dtp->u.p.ionml; nml->next; nml = nml->next);
3688 GFC_DIMENSION_SET(nml->dim[n],lbound,ubound,stride);
3691 /* Reverse memcpy - used for byte swapping. */
3693 void reverse_memcpy (void *dest, const void *src, size_t n)
3695 char *d, *s;
3696 size_t i;
3698 d = (char *) dest;
3699 s = (char *) src + n - 1;
3701 /* Write with ascending order - this is likely faster
3702 on modern architectures because of write combining. */
3703 for (i=0; i<n; i++)
3704 *(d++) = *(s--);
3708 /* Once upon a time, a poor innocent Fortran program was reading a
3709 file, when suddenly it hit the end-of-file (EOF). Unfortunately
3710 the OS doesn't tell whether we're at the EOF or whether we already
3711 went past it. Luckily our hero, libgfortran, keeps track of this.
3712 Call this function when you detect an EOF condition. See Section
3713 9.10.2 in F2003. */
3715 void
3716 hit_eof (st_parameter_dt * dtp)
3718 dtp->u.p.current_unit->flags.position = POSITION_APPEND;
3720 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
3721 switch (dtp->u.p.current_unit->endfile)
3723 case NO_ENDFILE:
3724 case AT_ENDFILE:
3725 generate_error (&dtp->common, LIBERROR_END, NULL);
3726 if (!is_internal_unit (dtp))
3728 dtp->u.p.current_unit->endfile = AFTER_ENDFILE;
3729 dtp->u.p.current_unit->current_record = 0;
3731 else
3732 dtp->u.p.current_unit->endfile = AT_ENDFILE;
3733 break;
3735 case AFTER_ENDFILE:
3736 generate_error (&dtp->common, LIBERROR_ENDFILE, NULL);
3737 dtp->u.p.current_unit->current_record = 0;
3738 break;
3740 else
3742 /* Non-sequential files don't have an ENDFILE record, so we
3743 can't be at AFTER_ENDFILE. */
3744 dtp->u.p.current_unit->endfile = AT_ENDFILE;
3745 generate_error (&dtp->common, LIBERROR_END, NULL);
3746 dtp->u.p.current_unit->current_record = 0;