re PR fortran/77707 (formatted direct access: nextrec off by one)
[gcc.git] / libgfortran / io / transfer.c
1 /* Copyright (C) 2002-2016 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
3 Namelist transfer functions contributed by Paul Thomas
4 F2003 I/O support contributed by Jerry DeLisle
5
6 This file is part of the GNU Fortran runtime library (libgfortran).
7
8 Libgfortran is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 Libgfortran is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
21
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
26
27
28 /* transfer.c -- Top level handling of data transfer statements. */
29
30 #include "io.h"
31 #include "fbuf.h"
32 #include "format.h"
33 #include "unix.h"
34 #include <string.h>
35 #include <assert.h>
36 #include <stdlib.h>
37 #include <errno.h>
38
39
40 /* Calling conventions: Data transfer statements are unlike other
41 library calls in that they extend over several calls.
42
43 The first call is always a call to st_read() or st_write(). These
44 subroutines return no status unless a namelist read or write is
45 being done, in which case there is the usual status. No further
46 calls are necessary in this case.
47
48 For other sorts of data transfer, there are zero or more data
49 transfer statement that depend on the format of the data transfer
50 statement. For READ (and for backwards compatibily: for WRITE), one has
51
52 transfer_integer
53 transfer_logical
54 transfer_character
55 transfer_character_wide
56 transfer_real
57 transfer_complex
58 transfer_real128
59 transfer_complex128
60
61 and for WRITE
62
63 transfer_integer_write
64 transfer_logical_write
65 transfer_character_write
66 transfer_character_wide_write
67 transfer_real_write
68 transfer_complex_write
69 transfer_real128_write
70 transfer_complex128_write
71
72 These subroutines do not return status. The *128 functions
73 are in the file transfer128.c.
74
75 The last call is a call to st_[read|write]_done(). While
76 something can easily go wrong with the initial st_read() or
77 st_write(), an error inhibits any data from actually being
78 transferred. */
79
80 extern void transfer_integer (st_parameter_dt *, void *, int);
81 export_proto(transfer_integer);
82
83 extern void transfer_integer_write (st_parameter_dt *, void *, int);
84 export_proto(transfer_integer_write);
85
86 extern void transfer_real (st_parameter_dt *, void *, int);
87 export_proto(transfer_real);
88
89 extern void transfer_real_write (st_parameter_dt *, void *, int);
90 export_proto(transfer_real_write);
91
92 extern void transfer_logical (st_parameter_dt *, void *, int);
93 export_proto(transfer_logical);
94
95 extern void transfer_logical_write (st_parameter_dt *, void *, int);
96 export_proto(transfer_logical_write);
97
98 extern void transfer_character (st_parameter_dt *, void *, int);
99 export_proto(transfer_character);
100
101 extern void transfer_character_write (st_parameter_dt *, void *, int);
102 export_proto(transfer_character_write);
103
104 extern void transfer_character_wide (st_parameter_dt *, void *, int, int);
105 export_proto(transfer_character_wide);
106
107 extern void transfer_character_wide_write (st_parameter_dt *,
108 void *, int, int);
109 export_proto(transfer_character_wide_write);
110
111 extern void transfer_complex (st_parameter_dt *, void *, int);
112 export_proto(transfer_complex);
113
114 extern void transfer_complex_write (st_parameter_dt *, void *, int);
115 export_proto(transfer_complex_write);
116
117 extern void transfer_array (st_parameter_dt *, gfc_array_char *, int,
118 gfc_charlen_type);
119 export_proto(transfer_array);
120
121 extern void transfer_array_write (st_parameter_dt *, gfc_array_char *, int,
122 gfc_charlen_type);
123 export_proto(transfer_array_write);
124
125 /* User defined derived type input/output. */
126 extern void
127 transfer_derived (st_parameter_dt *dtp, void *dtio_source, void *dtio_proc);
128 export_proto(transfer_derived);
129
130 extern void
131 transfer_derived_write (st_parameter_dt *dtp, void *dtio_source, void *dtio_proc);
132 export_proto(transfer_derived_write);
133
134 static void us_read (st_parameter_dt *, int);
135 static void us_write (st_parameter_dt *, int);
136 static void next_record_r_unf (st_parameter_dt *, int);
137 static void next_record_w_unf (st_parameter_dt *, int);
138
139 static const st_option advance_opt[] = {
140 {"yes", ADVANCE_YES},
141 {"no", ADVANCE_NO},
142 {NULL, 0}
143 };
144
145
146 static const st_option decimal_opt[] = {
147 {"point", DECIMAL_POINT},
148 {"comma", DECIMAL_COMMA},
149 {NULL, 0}
150 };
151
152 static const st_option round_opt[] = {
153 {"up", ROUND_UP},
154 {"down", ROUND_DOWN},
155 {"zero", ROUND_ZERO},
156 {"nearest", ROUND_NEAREST},
157 {"compatible", ROUND_COMPATIBLE},
158 {"processor_defined", ROUND_PROCDEFINED},
159 {NULL, 0}
160 };
161
162
163 static const st_option sign_opt[] = {
164 {"plus", SIGN_SP},
165 {"suppress", SIGN_SS},
166 {"processor_defined", SIGN_S},
167 {NULL, 0}
168 };
169
170 static const st_option blank_opt[] = {
171 {"null", BLANK_NULL},
172 {"zero", BLANK_ZERO},
173 {NULL, 0}
174 };
175
176 static const st_option delim_opt[] = {
177 {"apostrophe", DELIM_APOSTROPHE},
178 {"quote", DELIM_QUOTE},
179 {"none", DELIM_NONE},
180 {NULL, 0}
181 };
182
183 static const st_option pad_opt[] = {
184 {"yes", PAD_YES},
185 {"no", PAD_NO},
186 {NULL, 0}
187 };
188
189 typedef enum
190 { FORMATTED_SEQUENTIAL, UNFORMATTED_SEQUENTIAL,
191 FORMATTED_DIRECT, UNFORMATTED_DIRECT, FORMATTED_STREAM, UNFORMATTED_STREAM
192 }
193 file_mode;
194
195
196 static file_mode
197 current_mode (st_parameter_dt *dtp)
198 {
199 file_mode m;
200
201 m = FORM_UNSPECIFIED;
202
203 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
204 {
205 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
206 FORMATTED_DIRECT : UNFORMATTED_DIRECT;
207 }
208 else if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
209 {
210 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
211 FORMATTED_SEQUENTIAL : UNFORMATTED_SEQUENTIAL;
212 }
213 else if (dtp->u.p.current_unit->flags.access == ACCESS_STREAM)
214 {
215 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
216 FORMATTED_STREAM : UNFORMATTED_STREAM;
217 }
218
219 return m;
220 }
221
222
223 /* Mid level data transfer statements. */
224
225 /* Read sequential file - internal unit */
226
227 static char *
228 read_sf_internal (st_parameter_dt *dtp, int * length)
229 {
230 static char *empty_string[0];
231 char *base;
232 int lorig;
233
234 /* Zero size array gives internal unit len of 0. Nothing to read. */
235 if (dtp->internal_unit_len == 0
236 && dtp->u.p.current_unit->pad_status == PAD_NO)
237 hit_eof (dtp);
238
239 /* If we have seen an eor previously, return a length of 0. The
240 caller is responsible for correctly padding the input field. */
241 if (dtp->u.p.sf_seen_eor)
242 {
243 *length = 0;
244 /* Just return something that isn't a NULL pointer, otherwise the
245 caller thinks an error occurred. */
246 return (char*) empty_string;
247 }
248
249 lorig = *length;
250 if (is_char4_unit(dtp))
251 {
252 int i;
253 gfc_char4_t *p = (gfc_char4_t *) mem_alloc_r4 (dtp->u.p.current_unit->s,
254 length);
255 base = fbuf_alloc (dtp->u.p.current_unit, lorig);
256 for (i = 0; i < *length; i++, p++)
257 base[i] = *p > 255 ? '?' : (unsigned char) *p;
258 }
259 else
260 base = mem_alloc_r (dtp->u.p.current_unit->s, length);
261
262 if (unlikely (lorig > *length))
263 {
264 hit_eof (dtp);
265 return NULL;
266 }
267
268 dtp->u.p.current_unit->bytes_left -= *length;
269
270 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
271 dtp->u.p.size_used += (GFC_IO_INT) *length;
272
273 return base;
274
275 }
276
277 /* When reading sequential formatted records we have a problem. We
278 don't know how long the line is until we read the trailing newline,
279 and we don't want to read too much. If we read too much, we might
280 have to do a physical seek backwards depending on how much data is
281 present, and devices like terminals aren't seekable and would cause
282 an I/O error.
283
284 Given this, the solution is to read a byte at a time, stopping if
285 we hit the newline. For small allocations, we use a static buffer.
286 For larger allocations, we are forced to allocate memory on the
287 heap. Hopefully this won't happen very often. */
288
289 /* Read sequential file - external unit */
290
291 static char *
292 read_sf (st_parameter_dt *dtp, int * length)
293 {
294 static char *empty_string[0];
295 int q, q2;
296 int n, lorig, seen_comma;
297
298 /* If we have seen an eor previously, return a length of 0. The
299 caller is responsible for correctly padding the input field. */
300 if (dtp->u.p.sf_seen_eor)
301 {
302 *length = 0;
303 /* Just return something that isn't a NULL pointer, otherwise the
304 caller thinks an error occurred. */
305 return (char*) empty_string;
306 }
307
308 n = seen_comma = 0;
309
310 /* Read data into format buffer and scan through it. */
311 lorig = *length;
312
313 while (n < *length)
314 {
315 q = fbuf_getc (dtp->u.p.current_unit);
316 if (q == EOF)
317 break;
318 else if (q == '\n' || q == '\r')
319 {
320 /* Unexpected end of line. Set the position. */
321 dtp->u.p.sf_seen_eor = 1;
322
323 /* If we see an EOR during non-advancing I/O, we need to skip
324 the rest of the I/O statement. Set the corresponding flag. */
325 if (dtp->u.p.advance_status == ADVANCE_NO || dtp->u.p.seen_dollar)
326 dtp->u.p.eor_condition = 1;
327
328 /* If we encounter a CR, it might be a CRLF. */
329 if (q == '\r') /* Probably a CRLF */
330 {
331 /* See if there is an LF. */
332 q2 = fbuf_getc (dtp->u.p.current_unit);
333 if (q2 == '\n')
334 dtp->u.p.sf_seen_eor = 2;
335 else if (q2 != EOF) /* Oops, seek back. */
336 fbuf_seek (dtp->u.p.current_unit, -1, SEEK_CUR);
337 }
338
339 /* Without padding, terminate the I/O statement without assigning
340 the value. With padding, the value still needs to be assigned,
341 so we can just continue with a short read. */
342 if (dtp->u.p.current_unit->pad_status == PAD_NO)
343 {
344 generate_error (&dtp->common, LIBERROR_EOR, NULL);
345 return NULL;
346 }
347
348 *length = n;
349 goto done;
350 }
351 /* Short circuit the read if a comma is found during numeric input.
352 The flag is set to zero during character reads so that commas in
353 strings are not ignored */
354 else if (q == ',')
355 if (dtp->u.p.sf_read_comma == 1)
356 {
357 seen_comma = 1;
358 notify_std (&dtp->common, GFC_STD_GNU,
359 "Comma in formatted numeric read.");
360 break;
361 }
362 n++;
363 }
364
365 *length = n;
366
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)
370 {
371 if (n > 0)
372 {
373 if (dtp->u.p.advance_status == ADVANCE_NO)
374 {
375 if (dtp->u.p.current_unit->pad_status == PAD_NO)
376 {
377 hit_eof (dtp);
378 return NULL;
379 }
380 else
381 dtp->u.p.eor_condition = 1;
382 }
383 else
384 dtp->u.p.at_eof = 1;
385 }
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)
390 {
391 hit_eof (dtp);
392 return NULL;
393 }
394 }
395
396 done:
397
398 dtp->u.p.current_unit->bytes_left -= n;
399
400 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
401 dtp->u.p.size_used += (GFC_IO_INT) n;
402
403 /* We can't call fbuf_getptr before the loop doing fbuf_getc, because
404 fbuf_getc might reallocate the buffer. So return current pointer
405 minus all the advances, which is n plus up to two characters
406 of newline or comma. */
407 return fbuf_getptr (dtp->u.p.current_unit)
408 - n - dtp->u.p.sf_seen_eor - seen_comma;
409 }
410
411
412 /* Function for reading the next couple of bytes from the current
413 file, advancing the current position. We return NULL on end of record or
414 end of file. This function is only for formatted I/O, unformatted uses
415 read_block_direct.
416
417 If the read is short, then it is because the current record does not
418 have enough data to satisfy the read request and the file was
419 opened with PAD=YES. The caller must assume tailing spaces for
420 short reads. */
421
422 void *
423 read_block_form (st_parameter_dt *dtp, int * nbytes)
424 {
425 char *source;
426 int norig;
427
428 if (!is_stream_io (dtp))
429 {
430 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) *nbytes)
431 {
432 /* For preconnected units with default record length, set bytes left
433 to unit record length and proceed, otherwise error. */
434 if (dtp->u.p.current_unit->unit_number == options.stdin_unit
435 && dtp->u.p.current_unit->recl == DEFAULT_RECL)
436 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
437 else
438 {
439 if (unlikely (dtp->u.p.current_unit->pad_status == PAD_NO)
440 && !is_internal_unit (dtp))
441 {
442 /* Not enough data left. */
443 generate_error (&dtp->common, LIBERROR_EOR, NULL);
444 return NULL;
445 }
446 }
447
448 if (unlikely (dtp->u.p.current_unit->bytes_left == 0
449 && !is_internal_unit(dtp)))
450 {
451 hit_eof (dtp);
452 return NULL;
453 }
454
455 *nbytes = dtp->u.p.current_unit->bytes_left;
456 }
457 }
458
459 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
460 (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL ||
461 dtp->u.p.current_unit->flags.access == ACCESS_STREAM))
462 {
463 if (is_internal_unit (dtp))
464 source = read_sf_internal (dtp, nbytes);
465 else
466 source = read_sf (dtp, nbytes);
467
468 dtp->u.p.current_unit->strm_pos +=
469 (gfc_offset) (*nbytes + dtp->u.p.sf_seen_eor);
470 return source;
471 }
472
473 /* If we reach here, we can assume it's direct access. */
474
475 dtp->u.p.current_unit->bytes_left -= (gfc_offset) *nbytes;
476
477 norig = *nbytes;
478 source = fbuf_read (dtp->u.p.current_unit, nbytes);
479 fbuf_seek (dtp->u.p.current_unit, *nbytes, SEEK_CUR);
480
481 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
482 dtp->u.p.size_used += (GFC_IO_INT) *nbytes;
483
484 if (norig != *nbytes)
485 {
486 /* Short read, this shouldn't happen. */
487 if (dtp->u.p.current_unit->pad_status == PAD_NO)
488 {
489 generate_error (&dtp->common, LIBERROR_EOR, NULL);
490 source = NULL;
491 }
492 }
493
494 dtp->u.p.current_unit->strm_pos += (gfc_offset) *nbytes;
495
496 return source;
497 }
498
499
500 /* Read a block from a character(kind=4) internal unit, to be transferred into
501 a character(kind=4) variable. Note: Portions of this code borrowed from
502 read_sf_internal. */
503 void *
504 read_block_form4 (st_parameter_dt *dtp, int * nbytes)
505 {
506 static gfc_char4_t *empty_string[0];
507 gfc_char4_t *source;
508 int lorig;
509
510 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) *nbytes)
511 *nbytes = dtp->u.p.current_unit->bytes_left;
512
513 /* Zero size array gives internal unit len of 0. Nothing to read. */
514 if (dtp->internal_unit_len == 0
515 && dtp->u.p.current_unit->pad_status == PAD_NO)
516 hit_eof (dtp);
517
518 /* If we have seen an eor previously, return a length of 0. The
519 caller is responsible for correctly padding the input field. */
520 if (dtp->u.p.sf_seen_eor)
521 {
522 *nbytes = 0;
523 /* Just return something that isn't a NULL pointer, otherwise the
524 caller thinks an error occurred. */
525 return empty_string;
526 }
527
528 lorig = *nbytes;
529 source = (gfc_char4_t *) mem_alloc_r4 (dtp->u.p.current_unit->s, nbytes);
530
531 if (unlikely (lorig > *nbytes))
532 {
533 hit_eof (dtp);
534 return NULL;
535 }
536
537 dtp->u.p.current_unit->bytes_left -= *nbytes;
538
539 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
540 dtp->u.p.size_used += (GFC_IO_INT) *nbytes;
541
542 return source;
543 }
544
545
546 /* Reads a block directly into application data space. This is for
547 unformatted files. */
548
549 static void
550 read_block_direct (st_parameter_dt *dtp, void *buf, size_t nbytes)
551 {
552 ssize_t to_read_record;
553 ssize_t have_read_record;
554 ssize_t to_read_subrecord;
555 ssize_t have_read_subrecord;
556 int short_record;
557
558 if (is_stream_io (dtp))
559 {
560 have_read_record = sread (dtp->u.p.current_unit->s, buf,
561 nbytes);
562 if (unlikely (have_read_record < 0))
563 {
564 generate_error (&dtp->common, LIBERROR_OS, NULL);
565 return;
566 }
567
568 dtp->u.p.current_unit->strm_pos += (gfc_offset) have_read_record;
569
570 if (unlikely ((ssize_t) nbytes != have_read_record))
571 {
572 /* Short read, e.g. if we hit EOF. For stream files,
573 we have to set the end-of-file condition. */
574 hit_eof (dtp);
575 }
576 return;
577 }
578
579 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
580 {
581 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) nbytes)
582 {
583 short_record = 1;
584 to_read_record = dtp->u.p.current_unit->bytes_left;
585 nbytes = to_read_record;
586 }
587 else
588 {
589 short_record = 0;
590 to_read_record = nbytes;
591 }
592
593 dtp->u.p.current_unit->bytes_left -= to_read_record;
594
595 to_read_record = sread (dtp->u.p.current_unit->s, buf, to_read_record);
596 if (unlikely (to_read_record < 0))
597 {
598 generate_error (&dtp->common, LIBERROR_OS, NULL);
599 return;
600 }
601
602 if (to_read_record != (ssize_t) nbytes)
603 {
604 /* Short read, e.g. if we hit EOF. Apparently, we read
605 more than was written to the last record. */
606 return;
607 }
608
609 if (unlikely (short_record))
610 {
611 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
612 }
613 return;
614 }
615
616 /* Unformatted sequential. We loop over the subrecords, reading
617 until the request has been fulfilled or the record has run out
618 of continuation subrecords. */
619
620 /* Check whether we exceed the total record length. */
621
622 if (dtp->u.p.current_unit->flags.has_recl
623 && ((gfc_offset) nbytes > dtp->u.p.current_unit->bytes_left))
624 {
625 to_read_record = dtp->u.p.current_unit->bytes_left;
626 short_record = 1;
627 }
628 else
629 {
630 to_read_record = nbytes;
631 short_record = 0;
632 }
633 have_read_record = 0;
634
635 while(1)
636 {
637 if (dtp->u.p.current_unit->bytes_left_subrecord
638 < (gfc_offset) to_read_record)
639 {
640 to_read_subrecord = dtp->u.p.current_unit->bytes_left_subrecord;
641 to_read_record -= to_read_subrecord;
642 }
643 else
644 {
645 to_read_subrecord = to_read_record;
646 to_read_record = 0;
647 }
648
649 dtp->u.p.current_unit->bytes_left_subrecord -= to_read_subrecord;
650
651 have_read_subrecord = sread (dtp->u.p.current_unit->s,
652 buf + have_read_record, to_read_subrecord);
653 if (unlikely (have_read_subrecord < 0))
654 {
655 generate_error (&dtp->common, LIBERROR_OS, NULL);
656 return;
657 }
658
659 have_read_record += have_read_subrecord;
660
661 if (unlikely (to_read_subrecord != have_read_subrecord))
662 {
663 /* Short read, e.g. if we hit EOF. This means the record
664 structure has been corrupted, or the trailing record
665 marker would still be present. */
666
667 generate_error (&dtp->common, LIBERROR_CORRUPT_FILE, NULL);
668 return;
669 }
670
671 if (to_read_record > 0)
672 {
673 if (likely (dtp->u.p.current_unit->continued))
674 {
675 next_record_r_unf (dtp, 0);
676 us_read (dtp, 1);
677 }
678 else
679 {
680 /* Let's make sure the file position is correctly pre-positioned
681 for the next read statement. */
682
683 dtp->u.p.current_unit->current_record = 0;
684 next_record_r_unf (dtp, 0);
685 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
686 return;
687 }
688 }
689 else
690 {
691 /* Normal exit, the read request has been fulfilled. */
692 break;
693 }
694 }
695
696 dtp->u.p.current_unit->bytes_left -= have_read_record;
697 if (unlikely (short_record))
698 {
699 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
700 return;
701 }
702 return;
703 }
704
705
706 /* Function for writing a block of bytes to the current file at the
707 current position, advancing the file pointer. We are given a length
708 and return a pointer to a buffer that the caller must (completely)
709 fill in. Returns NULL on error. */
710
711 void *
712 write_block (st_parameter_dt *dtp, int length)
713 {
714 char *dest;
715
716 if (!is_stream_io (dtp))
717 {
718 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) length)
719 {
720 /* For preconnected units with default record length, set bytes left
721 to unit record length and proceed, otherwise error. */
722 if (likely ((dtp->u.p.current_unit->unit_number
723 == options.stdout_unit
724 || dtp->u.p.current_unit->unit_number
725 == options.stderr_unit)
726 && dtp->u.p.current_unit->recl == DEFAULT_RECL))
727 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
728 else
729 {
730 generate_error (&dtp->common, LIBERROR_EOR, NULL);
731 return NULL;
732 }
733 }
734
735 dtp->u.p.current_unit->bytes_left -= (gfc_offset) length;
736 }
737
738 if (is_internal_unit (dtp))
739 {
740 if (is_char4_unit(dtp)) /* char4 internel unit. */
741 {
742 gfc_char4_t *dest4;
743 dest4 = mem_alloc_w4 (dtp->u.p.current_unit->s, &length);
744 if (dest4 == NULL)
745 {
746 generate_error (&dtp->common, LIBERROR_END, NULL);
747 return NULL;
748 }
749 return dest4;
750 }
751 else
752 dest = mem_alloc_w (dtp->u.p.current_unit->s, &length);
753
754 if (dest == NULL)
755 {
756 generate_error (&dtp->common, LIBERROR_END, NULL);
757 return NULL;
758 }
759
760 if (unlikely (dtp->u.p.current_unit->endfile == AT_ENDFILE))
761 generate_error (&dtp->common, LIBERROR_END, NULL);
762 }
763 else
764 {
765 dest = fbuf_alloc (dtp->u.p.current_unit, length);
766 if (dest == NULL)
767 {
768 generate_error (&dtp->common, LIBERROR_OS, NULL);
769 return NULL;
770 }
771 }
772
773 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
774 dtp->u.p.size_used += (GFC_IO_INT) length;
775
776 dtp->u.p.current_unit->strm_pos += (gfc_offset) length;
777
778 return dest;
779 }
780
781
782 /* High level interface to swrite(), taking care of errors. This is only
783 called for unformatted files. There are three cases to consider:
784 Stream I/O, unformatted direct, unformatted sequential. */
785
786 static bool
787 write_buf (st_parameter_dt *dtp, void *buf, size_t nbytes)
788 {
789
790 ssize_t have_written;
791 ssize_t to_write_subrecord;
792 int short_record;
793
794 /* Stream I/O. */
795
796 if (is_stream_io (dtp))
797 {
798 have_written = swrite (dtp->u.p.current_unit->s, buf, nbytes);
799 if (unlikely (have_written < 0))
800 {
801 generate_error (&dtp->common, LIBERROR_OS, NULL);
802 return false;
803 }
804
805 dtp->u.p.current_unit->strm_pos += (gfc_offset) have_written;
806
807 return true;
808 }
809
810 /* Unformatted direct access. */
811
812 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
813 {
814 if (unlikely (dtp->u.p.current_unit->bytes_left < (gfc_offset) nbytes))
815 {
816 generate_error (&dtp->common, LIBERROR_DIRECT_EOR, NULL);
817 return false;
818 }
819
820 if (buf == NULL && nbytes == 0)
821 return true;
822
823 have_written = swrite (dtp->u.p.current_unit->s, buf, nbytes);
824 if (unlikely (have_written < 0))
825 {
826 generate_error (&dtp->common, LIBERROR_OS, NULL);
827 return false;
828 }
829
830 dtp->u.p.current_unit->strm_pos += (gfc_offset) have_written;
831 dtp->u.p.current_unit->bytes_left -= (gfc_offset) have_written;
832
833 return true;
834 }
835
836 /* Unformatted sequential. */
837
838 have_written = 0;
839
840 if (dtp->u.p.current_unit->flags.has_recl
841 && (gfc_offset) nbytes > dtp->u.p.current_unit->bytes_left)
842 {
843 nbytes = dtp->u.p.current_unit->bytes_left;
844 short_record = 1;
845 }
846 else
847 {
848 short_record = 0;
849 }
850
851 while (1)
852 {
853
854 to_write_subrecord =
855 (size_t) dtp->u.p.current_unit->bytes_left_subrecord < nbytes ?
856 (size_t) dtp->u.p.current_unit->bytes_left_subrecord : nbytes;
857
858 dtp->u.p.current_unit->bytes_left_subrecord -=
859 (gfc_offset) to_write_subrecord;
860
861 to_write_subrecord = swrite (dtp->u.p.current_unit->s,
862 buf + have_written, to_write_subrecord);
863 if (unlikely (to_write_subrecord < 0))
864 {
865 generate_error (&dtp->common, LIBERROR_OS, NULL);
866 return false;
867 }
868
869 dtp->u.p.current_unit->strm_pos += (gfc_offset) to_write_subrecord;
870 nbytes -= to_write_subrecord;
871 have_written += to_write_subrecord;
872
873 if (nbytes == 0)
874 break;
875
876 next_record_w_unf (dtp, 1);
877 us_write (dtp, 1);
878 }
879 dtp->u.p.current_unit->bytes_left -= have_written;
880 if (unlikely (short_record))
881 {
882 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
883 return false;
884 }
885 return true;
886 }
887
888
889 /* Reverse memcpy - used for byte swapping. */
890
891 static void
892 reverse_memcpy (void *dest, const void *src, size_t n)
893 {
894 char *d, *s;
895 size_t i;
896
897 d = (char *) dest;
898 s = (char *) src + n - 1;
899
900 /* Write with ascending order - this is likely faster
901 on modern architectures because of write combining. */
902 for (i=0; i<n; i++)
903 *(d++) = *(s--);
904 }
905
906
907 /* Utility function for byteswapping an array, using the bswap
908 builtins if possible. dest and src can overlap completely, or then
909 they must point to separate objects; partial overlaps are not
910 allowed. */
911
912 static void
913 bswap_array (void *dest, const void *src, size_t size, size_t nelems)
914 {
915 const char *ps;
916 char *pd;
917
918 switch (size)
919 {
920 case 1:
921 break;
922 case 2:
923 for (size_t i = 0; i < nelems; i++)
924 ((uint16_t*)dest)[i] = __builtin_bswap16 (((uint16_t*)src)[i]);
925 break;
926 case 4:
927 for (size_t i = 0; i < nelems; i++)
928 ((uint32_t*)dest)[i] = __builtin_bswap32 (((uint32_t*)src)[i]);
929 break;
930 case 8:
931 for (size_t i = 0; i < nelems; i++)
932 ((uint64_t*)dest)[i] = __builtin_bswap64 (((uint64_t*)src)[i]);
933 break;
934 case 12:
935 ps = src;
936 pd = dest;
937 for (size_t i = 0; i < nelems; i++)
938 {
939 uint32_t tmp;
940 memcpy (&tmp, ps, 4);
941 *(uint32_t*)pd = __builtin_bswap32 (*(uint32_t*)(ps + 8));
942 *(uint32_t*)(pd + 4) = __builtin_bswap32 (*(uint32_t*)(ps + 4));
943 *(uint32_t*)(pd + 8) = __builtin_bswap32 (tmp);
944 ps += size;
945 pd += size;
946 }
947 break;
948 case 16:
949 ps = src;
950 pd = dest;
951 for (size_t i = 0; i < nelems; i++)
952 {
953 uint64_t tmp;
954 memcpy (&tmp, ps, 8);
955 *(uint64_t*)pd = __builtin_bswap64 (*(uint64_t*)(ps + 8));
956 *(uint64_t*)(pd + 8) = __builtin_bswap64 (tmp);
957 ps += size;
958 pd += size;
959 }
960 break;
961 default:
962 pd = dest;
963 if (dest != src)
964 {
965 ps = src;
966 for (size_t i = 0; i < nelems; i++)
967 {
968 reverse_memcpy (pd, ps, size);
969 ps += size;
970 pd += size;
971 }
972 }
973 else
974 {
975 /* In-place byte swap. */
976 for (size_t i = 0; i < nelems; i++)
977 {
978 char tmp, *low = pd, *high = pd + size - 1;
979 for (size_t j = 0; j < size/2; j++)
980 {
981 tmp = *low;
982 *low = *high;
983 *high = tmp;
984 low++;
985 high--;
986 }
987 pd += size;
988 }
989 }
990 }
991 }
992
993
994 /* Master function for unformatted reads. */
995
996 static void
997 unformatted_read (st_parameter_dt *dtp, bt type,
998 void *dest, int kind, size_t size, size_t nelems)
999 {
1000 if (type == BT_CLASS)
1001 {
1002 int unit = dtp->u.p.current_unit->unit_number;
1003 char tmp_iomsg[IOMSG_LEN] = "";
1004 char *child_iomsg;
1005 gfc_charlen_type child_iomsg_len;
1006 int noiostat;
1007 int *child_iostat = NULL;
1008
1009 /* Set iostat, intent(out). */
1010 noiostat = 0;
1011 child_iostat = (dtp->common.flags & IOPARM_HAS_IOSTAT) ?
1012 dtp->common.iostat : &noiostat;
1013
1014 /* Set iomsg, intent(inout). */
1015 if (dtp->common.flags & IOPARM_HAS_IOMSG)
1016 {
1017 child_iomsg = dtp->common.iomsg;
1018 child_iomsg_len = dtp->common.iomsg_len;
1019 }
1020 else
1021 {
1022 child_iomsg = tmp_iomsg;
1023 child_iomsg_len = IOMSG_LEN;
1024 }
1025
1026 /* Call the user defined unformatted READ procedure. */
1027 dtp->u.p.current_unit->child_dtio++;
1028 dtp->u.p.ufdtio_ptr (dest, &unit, child_iostat, child_iomsg,
1029 child_iomsg_len);
1030 dtp->u.p.current_unit->child_dtio--;
1031 return;
1032 }
1033
1034 if (type == BT_CHARACTER)
1035 size *= GFC_SIZE_OF_CHAR_KIND(kind);
1036 read_block_direct (dtp, dest, size * nelems);
1037
1038 if (unlikely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_SWAP)
1039 && kind != 1)
1040 {
1041 /* Handle wide chracters. */
1042 if (type == BT_CHARACTER)
1043 {
1044 nelems *= size;
1045 size = kind;
1046 }
1047
1048 /* Break up complex into its constituent reals. */
1049 else if (type == BT_COMPLEX)
1050 {
1051 nelems *= 2;
1052 size /= 2;
1053 }
1054 bswap_array (dest, dest, size, nelems);
1055 }
1056 }
1057
1058
1059 /* Master function for unformatted writes. NOTE: For kind=10 the size is 16
1060 bytes on 64 bit machines. The unused bytes are not initialized and never
1061 used, which can show an error with memory checking analyzers like
1062 valgrind. We us BT_CLASS to denote a User Defined I/O call. */
1063
1064 static void
1065 unformatted_write (st_parameter_dt *dtp, bt type,
1066 void *source, int kind, size_t size, size_t nelems)
1067 {
1068 if (type == BT_CLASS)
1069 {
1070 int unit = dtp->u.p.current_unit->unit_number;
1071 char tmp_iomsg[IOMSG_LEN] = "";
1072 char *child_iomsg;
1073 gfc_charlen_type child_iomsg_len;
1074 int noiostat;
1075 int *child_iostat = NULL;
1076
1077 /* Set iostat, intent(out). */
1078 noiostat = 0;
1079 child_iostat = (dtp->common.flags & IOPARM_HAS_IOSTAT) ?
1080 dtp->common.iostat : &noiostat;
1081
1082 /* Set iomsg, intent(inout). */
1083 if (dtp->common.flags & IOPARM_HAS_IOMSG)
1084 {
1085 child_iomsg = dtp->common.iomsg;
1086 child_iomsg_len = dtp->common.iomsg_len;
1087 }
1088 else
1089 {
1090 child_iomsg = tmp_iomsg;
1091 child_iomsg_len = IOMSG_LEN;
1092 }
1093
1094 /* Call the user defined unformatted WRITE procedure. */
1095 dtp->u.p.current_unit->child_dtio++;
1096 dtp->u.p.ufdtio_ptr (source, &unit, child_iostat, child_iomsg,
1097 child_iomsg_len);
1098 dtp->u.p.current_unit->child_dtio--;
1099 return;
1100 }
1101
1102 if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE)
1103 || kind == 1)
1104 {
1105 size_t stride = type == BT_CHARACTER ?
1106 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
1107
1108 write_buf (dtp, source, stride * nelems);
1109 }
1110 else
1111 {
1112 #define BSWAP_BUFSZ 512
1113 char buffer[BSWAP_BUFSZ];
1114 char *p;
1115 size_t nrem;
1116
1117 p = source;
1118
1119 /* Handle wide chracters. */
1120 if (type == BT_CHARACTER && kind != 1)
1121 {
1122 nelems *= size;
1123 size = kind;
1124 }
1125
1126 /* Break up complex into its constituent reals. */
1127 if (type == BT_COMPLEX)
1128 {
1129 nelems *= 2;
1130 size /= 2;
1131 }
1132
1133 /* By now, all complex variables have been split into their
1134 constituent reals. */
1135
1136 nrem = nelems;
1137 do
1138 {
1139 size_t nc;
1140 if (size * nrem > BSWAP_BUFSZ)
1141 nc = BSWAP_BUFSZ / size;
1142 else
1143 nc = nrem;
1144
1145 bswap_array (buffer, p, size, nc);
1146 write_buf (dtp, buffer, size * nc);
1147 p += size * nc;
1148 nrem -= nc;
1149 }
1150 while (nrem > 0);
1151 }
1152 }
1153
1154
1155 /* Return a pointer to the name of a type. */
1156
1157 const char *
1158 type_name (bt type)
1159 {
1160 const char *p;
1161
1162 switch (type)
1163 {
1164 case BT_INTEGER:
1165 p = "INTEGER";
1166 break;
1167 case BT_LOGICAL:
1168 p = "LOGICAL";
1169 break;
1170 case BT_CHARACTER:
1171 p = "CHARACTER";
1172 break;
1173 case BT_REAL:
1174 p = "REAL";
1175 break;
1176 case BT_COMPLEX:
1177 p = "COMPLEX";
1178 break;
1179 case BT_CLASS:
1180 p = "CLASS or DERIVED";
1181 break;
1182 default:
1183 internal_error (NULL, "type_name(): Bad type");
1184 }
1185
1186 return p;
1187 }
1188
1189
1190 /* Write a constant string to the output.
1191 This is complicated because the string can have doubled delimiters
1192 in it. The length in the format node is the true length. */
1193
1194 static void
1195 write_constant_string (st_parameter_dt *dtp, const fnode *f)
1196 {
1197 char c, delimiter, *p, *q;
1198 int length;
1199
1200 length = f->u.string.length;
1201 if (length == 0)
1202 return;
1203
1204 p = write_block (dtp, length);
1205 if (p == NULL)
1206 return;
1207
1208 q = f->u.string.p;
1209 delimiter = q[-1];
1210
1211 for (; length > 0; length--)
1212 {
1213 c = *p++ = *q++;
1214 if (c == delimiter && c != 'H' && c != 'h')
1215 q++; /* Skip the doubled delimiter. */
1216 }
1217 }
1218
1219
1220 /* Given actual and expected types in a formatted data transfer, make
1221 sure they agree. If not, an error message is generated. Returns
1222 nonzero if something went wrong. */
1223
1224 static int
1225 require_type (st_parameter_dt *dtp, bt expected, bt actual, const fnode *f)
1226 {
1227 #define BUFLEN 100
1228 char buffer[BUFLEN];
1229
1230 if (actual == expected)
1231 return 0;
1232
1233 /* Adjust item_count before emitting error message. */
1234 snprintf (buffer, BUFLEN,
1235 "Expected %s for item %d in formatted transfer, got %s",
1236 type_name (expected), dtp->u.p.item_count - 1, type_name (actual));
1237
1238 format_error (dtp, f, buffer);
1239 return 1;
1240 }
1241
1242
1243 static int
1244 require_numeric_type (st_parameter_dt *dtp, bt actual, const fnode *f)
1245 {
1246 #define BUFLEN 100
1247 char buffer[BUFLEN];
1248
1249 if (actual == BT_INTEGER || actual == BT_REAL || actual == BT_COMPLEX)
1250 return 0;
1251
1252 /* Adjust item_count before emitting error message. */
1253 snprintf (buffer, BUFLEN,
1254 "Expected numeric type for item %d in formatted transfer, got %s",
1255 dtp->u.p.item_count - 1, type_name (actual));
1256
1257 format_error (dtp, f, buffer);
1258 return 1;
1259 }
1260
1261
1262 /* This function is in the main loop for a formatted data transfer
1263 statement. It would be natural to implement this as a coroutine
1264 with the user program, but C makes that awkward. We loop,
1265 processing format elements. When we actually have to transfer
1266 data instead of just setting flags, we return control to the user
1267 program which calls a function that supplies the address and type
1268 of the next element, then comes back here to process it. */
1269
1270 static void
1271 formatted_transfer_scalar_read (st_parameter_dt *dtp, bt type, void *p, int kind,
1272 size_t size)
1273 {
1274 int pos, bytes_used;
1275 const fnode *f;
1276 format_token t;
1277 int n;
1278 int consume_data_flag;
1279
1280 /* Change a complex data item into a pair of reals. */
1281
1282 n = (p == NULL) ? 0 : ((type != BT_COMPLEX) ? 1 : 2);
1283 if (type == BT_COMPLEX)
1284 {
1285 type = BT_REAL;
1286 size /= 2;
1287 }
1288
1289 /* If there's an EOR condition, we simulate finalizing the transfer
1290 by doing nothing. */
1291 if (dtp->u.p.eor_condition)
1292 return;
1293
1294 /* Set this flag so that commas in reads cause the read to complete before
1295 the entire field has been read. The next read field will start right after
1296 the comma in the stream. (Set to 0 for character reads). */
1297 dtp->u.p.sf_read_comma =
1298 dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA ? 0 : 1;
1299
1300 for (;;)
1301 {
1302 /* If reversion has occurred and there is another real data item,
1303 then we have to move to the next record. */
1304 if (dtp->u.p.reversion_flag && n > 0)
1305 {
1306 dtp->u.p.reversion_flag = 0;
1307 next_record (dtp, 0);
1308 }
1309
1310 consume_data_flag = 1;
1311 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1312 break;
1313
1314 f = next_format (dtp);
1315 if (f == NULL)
1316 {
1317 /* No data descriptors left. */
1318 if (unlikely (n > 0))
1319 generate_error (&dtp->common, LIBERROR_FORMAT,
1320 "Insufficient data descriptors in format after reversion");
1321 return;
1322 }
1323
1324 t = f->format;
1325
1326 bytes_used = (int)(dtp->u.p.current_unit->recl
1327 - dtp->u.p.current_unit->bytes_left);
1328
1329 if (is_stream_io(dtp))
1330 bytes_used = 0;
1331
1332 switch (t)
1333 {
1334 case FMT_I:
1335 if (n == 0)
1336 goto need_read_data;
1337 if (require_type (dtp, BT_INTEGER, type, f))
1338 return;
1339 read_decimal (dtp, f, p, kind);
1340 break;
1341
1342 case FMT_B:
1343 if (n == 0)
1344 goto need_read_data;
1345 if (!(compile_options.allow_std & GFC_STD_GNU)
1346 && require_numeric_type (dtp, type, f))
1347 return;
1348 if (!(compile_options.allow_std & GFC_STD_F2008)
1349 && require_type (dtp, BT_INTEGER, type, f))
1350 return;
1351 read_radix (dtp, f, p, kind, 2);
1352 break;
1353
1354 case FMT_O:
1355 if (n == 0)
1356 goto need_read_data;
1357 if (!(compile_options.allow_std & GFC_STD_GNU)
1358 && require_numeric_type (dtp, type, f))
1359 return;
1360 if (!(compile_options.allow_std & GFC_STD_F2008)
1361 && require_type (dtp, BT_INTEGER, type, f))
1362 return;
1363 read_radix (dtp, f, p, kind, 8);
1364 break;
1365
1366 case FMT_Z:
1367 if (n == 0)
1368 goto need_read_data;
1369 if (!(compile_options.allow_std & GFC_STD_GNU)
1370 && require_numeric_type (dtp, type, f))
1371 return;
1372 if (!(compile_options.allow_std & GFC_STD_F2008)
1373 && require_type (dtp, BT_INTEGER, type, f))
1374 return;
1375 read_radix (dtp, f, p, kind, 16);
1376 break;
1377
1378 case FMT_A:
1379 if (n == 0)
1380 goto need_read_data;
1381
1382 /* It is possible to have FMT_A with something not BT_CHARACTER such
1383 as when writing out hollerith strings, so check both type
1384 and kind before calling wide character routines. */
1385 if (type == BT_CHARACTER && kind == 4)
1386 read_a_char4 (dtp, f, p, size);
1387 else
1388 read_a (dtp, f, p, size);
1389 break;
1390
1391 case FMT_L:
1392 if (n == 0)
1393 goto need_read_data;
1394 read_l (dtp, f, p, kind);
1395 break;
1396
1397 case FMT_D:
1398 if (n == 0)
1399 goto need_read_data;
1400 if (require_type (dtp, BT_REAL, type, f))
1401 return;
1402 read_f (dtp, f, p, kind);
1403 break;
1404
1405 case FMT_DT:
1406 if (n == 0)
1407 goto need_read_data;
1408 if (require_type (dtp, BT_CLASS, type, f))
1409 return;
1410 int unit = dtp->u.p.current_unit->unit_number;
1411 char dt[] = "DT";
1412 char tmp_iomsg[IOMSG_LEN] = "";
1413 char *child_iomsg;
1414 gfc_charlen_type child_iomsg_len;
1415 int noiostat;
1416 int *child_iostat = NULL;
1417 char *iotype = f->u.udf.string;
1418 gfc_charlen_type iotype_len = f->u.udf.string_len;
1419
1420 /* Build the iotype string. */
1421 if (iotype_len == 0)
1422 {
1423 iotype_len = 2;
1424 iotype = dt;
1425 }
1426 else
1427 {
1428 iotype_len += 2;
1429 iotype = xmalloc (iotype_len);
1430 iotype[0] = dt[0];
1431 iotype[1] = dt[1];
1432 memcpy (iotype + 2, f->u.udf.string, f->u.udf.string_len);
1433 }
1434
1435 /* Set iostat, intent(out). */
1436 noiostat = 0;
1437 child_iostat = (dtp->common.flags & IOPARM_HAS_IOSTAT) ?
1438 dtp->common.iostat : &noiostat;
1439
1440 /* Set iomsg, intent(inout). */
1441 if (dtp->common.flags & IOPARM_HAS_IOMSG)
1442 {
1443 child_iomsg = dtp->common.iomsg;
1444 child_iomsg_len = dtp->common.iomsg_len;
1445 }
1446 else
1447 {
1448 child_iomsg = tmp_iomsg;
1449 child_iomsg_len = IOMSG_LEN;
1450 }
1451
1452 /* Call the user defined formatted READ procedure. */
1453 dtp->u.p.current_unit->child_dtio++;
1454 dtp->u.p.fdtio_ptr (p, &unit, iotype, f->u.udf.vlist,
1455 child_iostat, child_iomsg,
1456 iotype_len, child_iomsg_len);
1457 dtp->u.p.current_unit->child_dtio--;
1458
1459 if (f->u.udf.string_len != 0)
1460 free (iotype);
1461 /* Note: vlist is freed in free_format_data. */
1462 break;
1463
1464 case FMT_E:
1465 if (n == 0)
1466 goto need_read_data;
1467 if (require_type (dtp, BT_REAL, type, f))
1468 return;
1469 read_f (dtp, f, p, kind);
1470 break;
1471
1472 case FMT_EN:
1473 if (n == 0)
1474 goto need_read_data;
1475 if (require_type (dtp, BT_REAL, type, f))
1476 return;
1477 read_f (dtp, f, p, kind);
1478 break;
1479
1480 case FMT_ES:
1481 if (n == 0)
1482 goto need_read_data;
1483 if (require_type (dtp, BT_REAL, type, f))
1484 return;
1485 read_f (dtp, f, p, kind);
1486 break;
1487
1488 case FMT_F:
1489 if (n == 0)
1490 goto need_read_data;
1491 if (require_type (dtp, BT_REAL, type, f))
1492 return;
1493 read_f (dtp, f, p, kind);
1494 break;
1495
1496 case FMT_G:
1497 if (n == 0)
1498 goto need_read_data;
1499 switch (type)
1500 {
1501 case BT_INTEGER:
1502 read_decimal (dtp, f, p, kind);
1503 break;
1504 case BT_LOGICAL:
1505 read_l (dtp, f, p, kind);
1506 break;
1507 case BT_CHARACTER:
1508 if (kind == 4)
1509 read_a_char4 (dtp, f, p, size);
1510 else
1511 read_a (dtp, f, p, size);
1512 break;
1513 case BT_REAL:
1514 read_f (dtp, f, p, kind);
1515 break;
1516 default:
1517 internal_error (&dtp->common, "formatted_transfer(): Bad type");
1518 }
1519 break;
1520
1521 case FMT_STRING:
1522 consume_data_flag = 0;
1523 format_error (dtp, f, "Constant string in input format");
1524 return;
1525
1526 /* Format codes that don't transfer data. */
1527 case FMT_X:
1528 case FMT_TR:
1529 consume_data_flag = 0;
1530 dtp->u.p.skips += f->u.n;
1531 pos = bytes_used + dtp->u.p.skips - 1;
1532 dtp->u.p.pending_spaces = pos - dtp->u.p.max_pos + 1;
1533 read_x (dtp, f->u.n);
1534 break;
1535
1536 case FMT_TL:
1537 case FMT_T:
1538 consume_data_flag = 0;
1539
1540 if (f->format == FMT_TL)
1541 {
1542 /* Handle the special case when no bytes have been used yet.
1543 Cannot go below zero. */
1544 if (bytes_used == 0)
1545 {
1546 dtp->u.p.pending_spaces -= f->u.n;
1547 dtp->u.p.skips -= f->u.n;
1548 dtp->u.p.skips = dtp->u.p.skips < 0 ? 0 : dtp->u.p.skips;
1549 }
1550
1551 pos = bytes_used - f->u.n;
1552 }
1553 else /* FMT_T */
1554 pos = f->u.n - 1;
1555
1556 /* Standard 10.6.1.1: excessive left tabbing is reset to the
1557 left tab limit. We do not check if the position has gone
1558 beyond the end of record because a subsequent tab could
1559 bring us back again. */
1560 pos = pos < 0 ? 0 : pos;
1561
1562 dtp->u.p.skips = dtp->u.p.skips + pos - bytes_used;
1563 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces
1564 + pos - dtp->u.p.max_pos;
1565 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces < 0
1566 ? 0 : dtp->u.p.pending_spaces;
1567 if (dtp->u.p.skips == 0)
1568 break;
1569
1570 /* Adjust everything for end-of-record condition */
1571 if (dtp->u.p.sf_seen_eor && !is_internal_unit (dtp))
1572 {
1573 dtp->u.p.current_unit->bytes_left -= dtp->u.p.sf_seen_eor;
1574 dtp->u.p.skips -= dtp->u.p.sf_seen_eor;
1575 bytes_used = pos;
1576 dtp->u.p.sf_seen_eor = 0;
1577 }
1578 if (dtp->u.p.skips < 0)
1579 {
1580 if (is_internal_unit (dtp))
1581 sseek (dtp->u.p.current_unit->s, dtp->u.p.skips, SEEK_CUR);
1582 else
1583 fbuf_seek (dtp->u.p.current_unit, dtp->u.p.skips, SEEK_CUR);
1584 dtp->u.p.current_unit->bytes_left -= (gfc_offset) dtp->u.p.skips;
1585 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1586 }
1587 else
1588 read_x (dtp, dtp->u.p.skips);
1589 break;
1590
1591 case FMT_S:
1592 consume_data_flag = 0;
1593 dtp->u.p.sign_status = SIGN_S;
1594 break;
1595
1596 case FMT_SS:
1597 consume_data_flag = 0;
1598 dtp->u.p.sign_status = SIGN_SS;
1599 break;
1600
1601 case FMT_SP:
1602 consume_data_flag = 0;
1603 dtp->u.p.sign_status = SIGN_SP;
1604 break;
1605
1606 case FMT_BN:
1607 consume_data_flag = 0 ;
1608 dtp->u.p.blank_status = BLANK_NULL;
1609 break;
1610
1611 case FMT_BZ:
1612 consume_data_flag = 0;
1613 dtp->u.p.blank_status = BLANK_ZERO;
1614 break;
1615
1616 case FMT_DC:
1617 consume_data_flag = 0;
1618 dtp->u.p.current_unit->decimal_status = DECIMAL_COMMA;
1619 break;
1620
1621 case FMT_DP:
1622 consume_data_flag = 0;
1623 dtp->u.p.current_unit->decimal_status = DECIMAL_POINT;
1624 break;
1625
1626 case FMT_RC:
1627 consume_data_flag = 0;
1628 dtp->u.p.current_unit->round_status = ROUND_COMPATIBLE;
1629 break;
1630
1631 case FMT_RD:
1632 consume_data_flag = 0;
1633 dtp->u.p.current_unit->round_status = ROUND_DOWN;
1634 break;
1635
1636 case FMT_RN:
1637 consume_data_flag = 0;
1638 dtp->u.p.current_unit->round_status = ROUND_NEAREST;
1639 break;
1640
1641 case FMT_RP:
1642 consume_data_flag = 0;
1643 dtp->u.p.current_unit->round_status = ROUND_PROCDEFINED;
1644 break;
1645
1646 case FMT_RU:
1647 consume_data_flag = 0;
1648 dtp->u.p.current_unit->round_status = ROUND_UP;
1649 break;
1650
1651 case FMT_RZ:
1652 consume_data_flag = 0;
1653 dtp->u.p.current_unit->round_status = ROUND_ZERO;
1654 break;
1655
1656 case FMT_P:
1657 consume_data_flag = 0;
1658 dtp->u.p.scale_factor = f->u.k;
1659 break;
1660
1661 case FMT_DOLLAR:
1662 consume_data_flag = 0;
1663 dtp->u.p.seen_dollar = 1;
1664 break;
1665
1666 case FMT_SLASH:
1667 consume_data_flag = 0;
1668 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1669 next_record (dtp, 0);
1670 break;
1671
1672 case FMT_COLON:
1673 /* A colon descriptor causes us to exit this loop (in
1674 particular preventing another / descriptor from being
1675 processed) unless there is another data item to be
1676 transferred. */
1677 consume_data_flag = 0;
1678 if (n == 0)
1679 return;
1680 break;
1681
1682 default:
1683 internal_error (&dtp->common, "Bad format node");
1684 }
1685
1686 /* Adjust the item count and data pointer. */
1687
1688 if ((consume_data_flag > 0) && (n > 0))
1689 {
1690 n--;
1691 p = ((char *) p) + size;
1692 }
1693
1694 dtp->u.p.skips = 0;
1695
1696 pos = (int)(dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left);
1697 dtp->u.p.max_pos = (dtp->u.p.max_pos > pos) ? dtp->u.p.max_pos : pos;
1698 }
1699
1700 return;
1701
1702 /* Come here when we need a data descriptor but don't have one. We
1703 push the current format node back onto the input, then return and
1704 let the user program call us back with the data. */
1705 need_read_data:
1706 unget_format (dtp, f);
1707 }
1708
1709
1710 static void
1711 formatted_transfer_scalar_write (st_parameter_dt *dtp, bt type, void *p, int kind,
1712 size_t size)
1713 {
1714 int pos, bytes_used;
1715 const fnode *f;
1716 format_token t;
1717 int n;
1718 int consume_data_flag;
1719
1720 /* Change a complex data item into a pair of reals. */
1721
1722 n = (p == NULL) ? 0 : ((type != BT_COMPLEX) ? 1 : 2);
1723 if (type == BT_COMPLEX)
1724 {
1725 type = BT_REAL;
1726 size /= 2;
1727 }
1728
1729 /* If there's an EOR condition, we simulate finalizing the transfer
1730 by doing nothing. */
1731 if (dtp->u.p.eor_condition)
1732 return;
1733
1734 /* Set this flag so that commas in reads cause the read to complete before
1735 the entire field has been read. The next read field will start right after
1736 the comma in the stream. (Set to 0 for character reads). */
1737 dtp->u.p.sf_read_comma =
1738 dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA ? 0 : 1;
1739
1740 for (;;)
1741 {
1742 /* If reversion has occurred and there is another real data item,
1743 then we have to move to the next record. */
1744 if (dtp->u.p.reversion_flag && n > 0)
1745 {
1746 dtp->u.p.reversion_flag = 0;
1747 next_record (dtp, 0);
1748 }
1749
1750 consume_data_flag = 1;
1751 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1752 break;
1753
1754 f = next_format (dtp);
1755 if (f == NULL)
1756 {
1757 /* No data descriptors left. */
1758 if (unlikely (n > 0))
1759 generate_error (&dtp->common, LIBERROR_FORMAT,
1760 "Insufficient data descriptors in format after reversion");
1761 return;
1762 }
1763
1764 /* Now discharge T, TR and X movements to the right. This is delayed
1765 until a data producing format to suppress trailing spaces. */
1766
1767 t = f->format;
1768 if (dtp->u.p.mode == WRITING && dtp->u.p.skips != 0
1769 && ((n>0 && ( t == FMT_I || t == FMT_B || t == FMT_O
1770 || t == FMT_Z || t == FMT_F || t == FMT_E
1771 || t == FMT_EN || t == FMT_ES || t == FMT_G
1772 || t == FMT_L || t == FMT_A || t == FMT_D
1773 || t == FMT_DT))
1774 || t == FMT_STRING))
1775 {
1776 if (dtp->u.p.skips > 0)
1777 {
1778 int tmp;
1779 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
1780 tmp = (int)(dtp->u.p.current_unit->recl
1781 - dtp->u.p.current_unit->bytes_left);
1782 dtp->u.p.max_pos =
1783 dtp->u.p.max_pos > tmp ? dtp->u.p.max_pos : tmp;
1784 dtp->u.p.skips = 0;
1785 }
1786 if (dtp->u.p.skips < 0)
1787 {
1788 if (is_internal_unit (dtp))
1789 sseek (dtp->u.p.current_unit->s, dtp->u.p.skips, SEEK_CUR);
1790 else
1791 fbuf_seek (dtp->u.p.current_unit, dtp->u.p.skips, SEEK_CUR);
1792 dtp->u.p.current_unit->bytes_left -= (gfc_offset) dtp->u.p.skips;
1793 }
1794 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1795 }
1796
1797 bytes_used = (int)(dtp->u.p.current_unit->recl
1798 - dtp->u.p.current_unit->bytes_left);
1799
1800 if (is_stream_io(dtp))
1801 bytes_used = 0;
1802
1803 switch (t)
1804 {
1805 case FMT_I:
1806 if (n == 0)
1807 goto need_data;
1808 if (require_type (dtp, BT_INTEGER, type, f))
1809 return;
1810 write_i (dtp, f, p, kind);
1811 break;
1812
1813 case FMT_B:
1814 if (n == 0)
1815 goto need_data;
1816 if (!(compile_options.allow_std & GFC_STD_GNU)
1817 && require_numeric_type (dtp, type, f))
1818 return;
1819 if (!(compile_options.allow_std & GFC_STD_F2008)
1820 && require_type (dtp, BT_INTEGER, type, f))
1821 return;
1822 write_b (dtp, f, p, kind);
1823 break;
1824
1825 case FMT_O:
1826 if (n == 0)
1827 goto need_data;
1828 if (!(compile_options.allow_std & GFC_STD_GNU)
1829 && require_numeric_type (dtp, type, f))
1830 return;
1831 if (!(compile_options.allow_std & GFC_STD_F2008)
1832 && require_type (dtp, BT_INTEGER, type, f))
1833 return;
1834 write_o (dtp, f, p, kind);
1835 break;
1836
1837 case FMT_Z:
1838 if (n == 0)
1839 goto need_data;
1840 if (!(compile_options.allow_std & GFC_STD_GNU)
1841 && require_numeric_type (dtp, type, f))
1842 return;
1843 if (!(compile_options.allow_std & GFC_STD_F2008)
1844 && require_type (dtp, BT_INTEGER, type, f))
1845 return;
1846 write_z (dtp, f, p, kind);
1847 break;
1848
1849 case FMT_A:
1850 if (n == 0)
1851 goto need_data;
1852
1853 /* It is possible to have FMT_A with something not BT_CHARACTER such
1854 as when writing out hollerith strings, so check both type
1855 and kind before calling wide character routines. */
1856 if (type == BT_CHARACTER && kind == 4)
1857 write_a_char4 (dtp, f, p, size);
1858 else
1859 write_a (dtp, f, p, size);
1860 break;
1861
1862 case FMT_L:
1863 if (n == 0)
1864 goto need_data;
1865 write_l (dtp, f, p, kind);
1866 break;
1867
1868 case FMT_D:
1869 if (n == 0)
1870 goto need_data;
1871 if (require_type (dtp, BT_REAL, type, f))
1872 return;
1873 write_d (dtp, f, p, kind);
1874 break;
1875
1876 case FMT_DT:
1877 if (n == 0)
1878 goto need_data;
1879 int unit = dtp->u.p.current_unit->unit_number;
1880 char dt[] = "DT";
1881 char tmp_iomsg[IOMSG_LEN] = "";
1882 char *child_iomsg;
1883 gfc_charlen_type child_iomsg_len;
1884 int noiostat;
1885 int *child_iostat = NULL;
1886 char *iotype = f->u.udf.string;
1887 gfc_charlen_type iotype_len = f->u.udf.string_len;
1888
1889 /* Build the iotype string. */
1890 if (iotype_len == 0)
1891 {
1892 iotype_len = 2;
1893 iotype = dt;
1894 }
1895 else
1896 {
1897 iotype_len += 2;
1898 iotype = xmalloc (iotype_len);
1899 iotype[0] = dt[0];
1900 iotype[1] = dt[1];
1901 memcpy (iotype + 2, f->u.udf.string, f->u.udf.string_len);
1902 }
1903
1904 /* Set iostat, intent(out). */
1905 noiostat = 0;
1906 child_iostat = (dtp->common.flags & IOPARM_HAS_IOSTAT) ?
1907 dtp->common.iostat : &noiostat;
1908
1909 /* Set iomsg, intent(inout). */
1910 if (dtp->common.flags & IOPARM_HAS_IOMSG)
1911 {
1912 child_iomsg = dtp->common.iomsg;
1913 child_iomsg_len = dtp->common.iomsg_len;
1914 }
1915 else
1916 {
1917 child_iomsg = tmp_iomsg;
1918 child_iomsg_len = IOMSG_LEN;
1919 }
1920
1921 /* Call the user defined formatted WRITE procedure. */
1922 dtp->u.p.current_unit->child_dtio++;
1923 dtp->u.p.fdtio_ptr (p, &unit, iotype, f->u.udf.vlist,
1924 child_iostat, child_iomsg,
1925 iotype_len, child_iomsg_len);
1926 dtp->u.p.current_unit->child_dtio--;
1927
1928 if (f->u.udf.string_len != 0)
1929 free (iotype);
1930 /* Note: vlist is freed in free_format_data. */
1931 break;
1932
1933 case FMT_E:
1934 if (n == 0)
1935 goto need_data;
1936 if (require_type (dtp, BT_REAL, type, f))
1937 return;
1938 write_e (dtp, f, p, kind);
1939 break;
1940
1941 case FMT_EN:
1942 if (n == 0)
1943 goto need_data;
1944 if (require_type (dtp, BT_REAL, type, f))
1945 return;
1946 write_en (dtp, f, p, kind);
1947 break;
1948
1949 case FMT_ES:
1950 if (n == 0)
1951 goto need_data;
1952 if (require_type (dtp, BT_REAL, type, f))
1953 return;
1954 write_es (dtp, f, p, kind);
1955 break;
1956
1957 case FMT_F:
1958 if (n == 0)
1959 goto need_data;
1960 if (require_type (dtp, BT_REAL, type, f))
1961 return;
1962 write_f (dtp, f, p, kind);
1963 break;
1964
1965 case FMT_G:
1966 if (n == 0)
1967 goto need_data;
1968 switch (type)
1969 {
1970 case BT_INTEGER:
1971 write_i (dtp, f, p, kind);
1972 break;
1973 case BT_LOGICAL:
1974 write_l (dtp, f, p, kind);
1975 break;
1976 case BT_CHARACTER:
1977 if (kind == 4)
1978 write_a_char4 (dtp, f, p, size);
1979 else
1980 write_a (dtp, f, p, size);
1981 break;
1982 case BT_REAL:
1983 if (f->u.real.w == 0)
1984 write_real_g0 (dtp, p, kind, f->u.real.d);
1985 else
1986 write_d (dtp, f, p, kind);
1987 break;
1988 default:
1989 internal_error (&dtp->common,
1990 "formatted_transfer(): Bad type");
1991 }
1992 break;
1993
1994 case FMT_STRING:
1995 consume_data_flag = 0;
1996 write_constant_string (dtp, f);
1997 break;
1998
1999 /* Format codes that don't transfer data. */
2000 case FMT_X:
2001 case FMT_TR:
2002 consume_data_flag = 0;
2003
2004 dtp->u.p.skips += f->u.n;
2005 pos = bytes_used + dtp->u.p.skips - 1;
2006 dtp->u.p.pending_spaces = pos - dtp->u.p.max_pos + 1;
2007 /* Writes occur just before the switch on f->format, above, so
2008 that trailing blanks are suppressed, unless we are doing a
2009 non-advancing write in which case we want to output the blanks
2010 now. */
2011 if (dtp->u.p.advance_status == ADVANCE_NO)
2012 {
2013 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
2014 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
2015 }
2016 break;
2017
2018 case FMT_TL:
2019 case FMT_T:
2020 consume_data_flag = 0;
2021
2022 if (f->format == FMT_TL)
2023 {
2024
2025 /* Handle the special case when no bytes have been used yet.
2026 Cannot go below zero. */
2027 if (bytes_used == 0)
2028 {
2029 dtp->u.p.pending_spaces -= f->u.n;
2030 dtp->u.p.skips -= f->u.n;
2031 dtp->u.p.skips = dtp->u.p.skips < 0 ? 0 : dtp->u.p.skips;
2032 }
2033
2034 pos = bytes_used - f->u.n;
2035 }
2036 else /* FMT_T */
2037 pos = f->u.n - dtp->u.p.pending_spaces - 1;
2038
2039 /* Standard 10.6.1.1: excessive left tabbing is reset to the
2040 left tab limit. We do not check if the position has gone
2041 beyond the end of record because a subsequent tab could
2042 bring us back again. */
2043 pos = pos < 0 ? 0 : pos;
2044
2045 dtp->u.p.skips = dtp->u.p.skips + pos - bytes_used;
2046 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces
2047 + pos - dtp->u.p.max_pos;
2048 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces < 0
2049 ? 0 : dtp->u.p.pending_spaces;
2050 break;
2051
2052 case FMT_S:
2053 consume_data_flag = 0;
2054 dtp->u.p.sign_status = SIGN_S;
2055 break;
2056
2057 case FMT_SS:
2058 consume_data_flag = 0;
2059 dtp->u.p.sign_status = SIGN_SS;
2060 break;
2061
2062 case FMT_SP:
2063 consume_data_flag = 0;
2064 dtp->u.p.sign_status = SIGN_SP;
2065 break;
2066
2067 case FMT_BN:
2068 consume_data_flag = 0 ;
2069 dtp->u.p.blank_status = BLANK_NULL;
2070 break;
2071
2072 case FMT_BZ:
2073 consume_data_flag = 0;
2074 dtp->u.p.blank_status = BLANK_ZERO;
2075 break;
2076
2077 case FMT_DC:
2078 consume_data_flag = 0;
2079 dtp->u.p.current_unit->decimal_status = DECIMAL_COMMA;
2080 break;
2081
2082 case FMT_DP:
2083 consume_data_flag = 0;
2084 dtp->u.p.current_unit->decimal_status = DECIMAL_POINT;
2085 break;
2086
2087 case FMT_RC:
2088 consume_data_flag = 0;
2089 dtp->u.p.current_unit->round_status = ROUND_COMPATIBLE;
2090 break;
2091
2092 case FMT_RD:
2093 consume_data_flag = 0;
2094 dtp->u.p.current_unit->round_status = ROUND_DOWN;
2095 break;
2096
2097 case FMT_RN:
2098 consume_data_flag = 0;
2099 dtp->u.p.current_unit->round_status = ROUND_NEAREST;
2100 break;
2101
2102 case FMT_RP:
2103 consume_data_flag = 0;
2104 dtp->u.p.current_unit->round_status = ROUND_PROCDEFINED;
2105 break;
2106
2107 case FMT_RU:
2108 consume_data_flag = 0;
2109 dtp->u.p.current_unit->round_status = ROUND_UP;
2110 break;
2111
2112 case FMT_RZ:
2113 consume_data_flag = 0;
2114 dtp->u.p.current_unit->round_status = ROUND_ZERO;
2115 break;
2116
2117 case FMT_P:
2118 consume_data_flag = 0;
2119 dtp->u.p.scale_factor = f->u.k;
2120 break;
2121
2122 case FMT_DOLLAR:
2123 consume_data_flag = 0;
2124 dtp->u.p.seen_dollar = 1;
2125 break;
2126
2127 case FMT_SLASH:
2128 consume_data_flag = 0;
2129 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
2130 next_record (dtp, 0);
2131 break;
2132
2133 case FMT_COLON:
2134 /* A colon descriptor causes us to exit this loop (in
2135 particular preventing another / descriptor from being
2136 processed) unless there is another data item to be
2137 transferred. */
2138 consume_data_flag = 0;
2139 if (n == 0)
2140 return;
2141 break;
2142
2143 default:
2144 internal_error (&dtp->common, "Bad format node");
2145 }
2146
2147 /* Adjust the item count and data pointer. */
2148
2149 if ((consume_data_flag > 0) && (n > 0))
2150 {
2151 n--;
2152 p = ((char *) p) + size;
2153 }
2154
2155 pos = (int)(dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left);
2156 dtp->u.p.max_pos = (dtp->u.p.max_pos > pos) ? dtp->u.p.max_pos : pos;
2157 }
2158
2159 return;
2160
2161 /* Come here when we need a data descriptor but don't have one. We
2162 push the current format node back onto the input, then return and
2163 let the user program call us back with the data. */
2164 need_data:
2165 unget_format (dtp, f);
2166 }
2167
2168 /* This function is first called from data_init_transfer to initiate the loop
2169 over each item in the format, transferring data as required. Subsequent
2170 calls to this function occur for each data item foound in the READ/WRITE
2171 statement. The item_count is incremented for each call. Since the first
2172 call is from data_transfer_init, the item_count is always one greater than
2173 the actual count number of the item being transferred. */
2174
2175 static void
2176 formatted_transfer (st_parameter_dt *dtp, bt type, void *p, int kind,
2177 size_t size, size_t nelems)
2178 {
2179 size_t elem;
2180 char *tmp;
2181
2182 tmp = (char *) p;
2183 size_t stride = type == BT_CHARACTER ?
2184 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
2185 if (dtp->u.p.mode == READING)
2186 {
2187 /* Big loop over all the elements. */
2188 for (elem = 0; elem < nelems; elem++)
2189 {
2190 dtp->u.p.item_count++;
2191 formatted_transfer_scalar_read (dtp, type, tmp + stride*elem, kind, size);
2192 }
2193 }
2194 else
2195 {
2196 /* Big loop over all the elements. */
2197 for (elem = 0; elem < nelems; elem++)
2198 {
2199 dtp->u.p.item_count++;
2200 formatted_transfer_scalar_write (dtp, type, tmp + stride*elem, kind, size);
2201 }
2202 }
2203 }
2204
2205
2206 /* Data transfer entry points. The type of the data entity is
2207 implicit in the subroutine call. This prevents us from having to
2208 share a common enum with the compiler. */
2209
2210 void
2211 transfer_integer (st_parameter_dt *dtp, void *p, int kind)
2212 {
2213 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2214 return;
2215 dtp->u.p.transfer (dtp, BT_INTEGER, p, kind, kind, 1);
2216 }
2217
2218 void
2219 transfer_integer_write (st_parameter_dt *dtp, void *p, int kind)
2220 {
2221 transfer_integer (dtp, p, kind);
2222 }
2223
2224 void
2225 transfer_real (st_parameter_dt *dtp, void *p, int kind)
2226 {
2227 size_t size;
2228 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2229 return;
2230 size = size_from_real_kind (kind);
2231 dtp->u.p.transfer (dtp, BT_REAL, p, kind, size, 1);
2232 }
2233
2234 void
2235 transfer_real_write (st_parameter_dt *dtp, void *p, int kind)
2236 {
2237 transfer_real (dtp, p, kind);
2238 }
2239
2240 void
2241 transfer_logical (st_parameter_dt *dtp, void *p, int kind)
2242 {
2243 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2244 return;
2245 dtp->u.p.transfer (dtp, BT_LOGICAL, p, kind, kind, 1);
2246 }
2247
2248 void
2249 transfer_logical_write (st_parameter_dt *dtp, void *p, int kind)
2250 {
2251 transfer_logical (dtp, p, kind);
2252 }
2253
2254 void
2255 transfer_character (st_parameter_dt *dtp, void *p, int len)
2256 {
2257 static char *empty_string[0];
2258
2259 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2260 return;
2261
2262 /* Strings of zero length can have p == NULL, which confuses the
2263 transfer routines into thinking we need more data elements. To avoid
2264 this, we give them a nice pointer. */
2265 if (len == 0 && p == NULL)
2266 p = empty_string;
2267
2268 /* Set kind here to 1. */
2269 dtp->u.p.transfer (dtp, BT_CHARACTER, p, 1, len, 1);
2270 }
2271
2272 void
2273 transfer_character_write (st_parameter_dt *dtp, void *p, int len)
2274 {
2275 transfer_character (dtp, p, len);
2276 }
2277
2278 void
2279 transfer_character_wide (st_parameter_dt *dtp, void *p, int len, int kind)
2280 {
2281 static char *empty_string[0];
2282
2283 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2284 return;
2285
2286 /* Strings of zero length can have p == NULL, which confuses the
2287 transfer routines into thinking we need more data elements. To avoid
2288 this, we give them a nice pointer. */
2289 if (len == 0 && p == NULL)
2290 p = empty_string;
2291
2292 /* Here we pass the actual kind value. */
2293 dtp->u.p.transfer (dtp, BT_CHARACTER, p, kind, len, 1);
2294 }
2295
2296 void
2297 transfer_character_wide_write (st_parameter_dt *dtp, void *p, int len, int kind)
2298 {
2299 transfer_character_wide (dtp, p, len, kind);
2300 }
2301
2302 void
2303 transfer_complex (st_parameter_dt *dtp, void *p, int kind)
2304 {
2305 size_t size;
2306 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2307 return;
2308 size = size_from_complex_kind (kind);
2309 dtp->u.p.transfer (dtp, BT_COMPLEX, p, kind, size, 1);
2310 }
2311
2312 void
2313 transfer_complex_write (st_parameter_dt *dtp, void *p, int kind)
2314 {
2315 transfer_complex (dtp, p, kind);
2316 }
2317
2318 void
2319 transfer_array (st_parameter_dt *dtp, gfc_array_char *desc, int kind,
2320 gfc_charlen_type charlen)
2321 {
2322 index_type count[GFC_MAX_DIMENSIONS];
2323 index_type extent[GFC_MAX_DIMENSIONS];
2324 index_type stride[GFC_MAX_DIMENSIONS];
2325 index_type stride0, rank, size, n;
2326 size_t tsize;
2327 char *data;
2328 bt iotype;
2329
2330 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2331 return;
2332
2333 iotype = (bt) GFC_DESCRIPTOR_TYPE (desc);
2334 size = iotype == BT_CHARACTER ? charlen : GFC_DESCRIPTOR_SIZE (desc);
2335
2336 rank = GFC_DESCRIPTOR_RANK (desc);
2337 for (n = 0; n < rank; n++)
2338 {
2339 count[n] = 0;
2340 stride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(desc,n);
2341 extent[n] = GFC_DESCRIPTOR_EXTENT(desc,n);
2342
2343 /* If the extent of even one dimension is zero, then the entire
2344 array section contains zero elements, so we return after writing
2345 a zero array record. */
2346 if (extent[n] <= 0)
2347 {
2348 data = NULL;
2349 tsize = 0;
2350 dtp->u.p.transfer (dtp, iotype, data, kind, size, tsize);
2351 return;
2352 }
2353 }
2354
2355 stride0 = stride[0];
2356
2357 /* If the innermost dimension has a stride of 1, we can do the transfer
2358 in contiguous chunks. */
2359 if (stride0 == size)
2360 tsize = extent[0];
2361 else
2362 tsize = 1;
2363
2364 data = GFC_DESCRIPTOR_DATA (desc);
2365
2366 while (data)
2367 {
2368 dtp->u.p.transfer (dtp, iotype, data, kind, size, tsize);
2369 data += stride0 * tsize;
2370 count[0] += tsize;
2371 n = 0;
2372 while (count[n] == extent[n])
2373 {
2374 count[n] = 0;
2375 data -= stride[n] * extent[n];
2376 n++;
2377 if (n == rank)
2378 {
2379 data = NULL;
2380 break;
2381 }
2382 else
2383 {
2384 count[n]++;
2385 data += stride[n];
2386 }
2387 }
2388 }
2389 }
2390
2391 void
2392 transfer_array_write (st_parameter_dt *dtp, gfc_array_char *desc, int kind,
2393 gfc_charlen_type charlen)
2394 {
2395 transfer_array (dtp, desc, kind, charlen);
2396 }
2397
2398
2399 /* User defined input/output iomsg. */
2400
2401 #define IOMSG_LEN 256
2402
2403 void
2404 transfer_derived (st_parameter_dt *parent, void *dtio_source, void *dtio_proc)
2405 {
2406 if (parent->u.p.current_unit)
2407 {
2408 if (parent->u.p.current_unit->flags.form == FORM_UNFORMATTED)
2409 parent->u.p.ufdtio_ptr = (unformatted_dtio) dtio_proc;
2410 else
2411 parent->u.p.fdtio_ptr = (formatted_dtio) dtio_proc;
2412 }
2413 parent->u.p.transfer (parent, BT_CLASS, dtio_source, 0, 0, 1);
2414 }
2415
2416
2417 /* Preposition a sequential unformatted file while reading. */
2418
2419 static void
2420 us_read (st_parameter_dt *dtp, int continued)
2421 {
2422 ssize_t n, nr;
2423 GFC_INTEGER_4 i4;
2424 GFC_INTEGER_8 i8;
2425 gfc_offset i;
2426
2427 if (compile_options.record_marker == 0)
2428 n = sizeof (GFC_INTEGER_4);
2429 else
2430 n = compile_options.record_marker;
2431
2432 nr = sread (dtp->u.p.current_unit->s, &i, n);
2433 if (unlikely (nr < 0))
2434 {
2435 generate_error (&dtp->common, LIBERROR_BAD_US, NULL);
2436 return;
2437 }
2438 else if (nr == 0)
2439 {
2440 hit_eof (dtp);
2441 return; /* end of file */
2442 }
2443 else if (unlikely (n != nr))
2444 {
2445 generate_error (&dtp->common, LIBERROR_BAD_US, NULL);
2446 return;
2447 }
2448
2449 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
2450 if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE))
2451 {
2452 switch (nr)
2453 {
2454 case sizeof(GFC_INTEGER_4):
2455 memcpy (&i4, &i, sizeof (i4));
2456 i = i4;
2457 break;
2458
2459 case sizeof(GFC_INTEGER_8):
2460 memcpy (&i8, &i, sizeof (i8));
2461 i = i8;
2462 break;
2463
2464 default:
2465 runtime_error ("Illegal value for record marker");
2466 break;
2467 }
2468 }
2469 else
2470 {
2471 uint32_t u32;
2472 uint64_t u64;
2473 switch (nr)
2474 {
2475 case sizeof(GFC_INTEGER_4):
2476 memcpy (&u32, &i, sizeof (u32));
2477 u32 = __builtin_bswap32 (u32);
2478 memcpy (&i4, &u32, sizeof (i4));
2479 i = i4;
2480 break;
2481
2482 case sizeof(GFC_INTEGER_8):
2483 memcpy (&u64, &i, sizeof (u64));
2484 u64 = __builtin_bswap64 (u64);
2485 memcpy (&i8, &u64, sizeof (i8));
2486 i = i8;
2487 break;
2488
2489 default:
2490 runtime_error ("Illegal value for record marker");
2491 break;
2492 }
2493 }
2494
2495 if (i >= 0)
2496 {
2497 dtp->u.p.current_unit->bytes_left_subrecord = i;
2498 dtp->u.p.current_unit->continued = 0;
2499 }
2500 else
2501 {
2502 dtp->u.p.current_unit->bytes_left_subrecord = -i;
2503 dtp->u.p.current_unit->continued = 1;
2504 }
2505
2506 if (! continued)
2507 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2508 }
2509
2510
2511 /* Preposition a sequential unformatted file while writing. This
2512 amount to writing a bogus length that will be filled in later. */
2513
2514 static void
2515 us_write (st_parameter_dt *dtp, int continued)
2516 {
2517 ssize_t nbytes;
2518 gfc_offset dummy;
2519
2520 dummy = 0;
2521
2522 if (compile_options.record_marker == 0)
2523 nbytes = sizeof (GFC_INTEGER_4);
2524 else
2525 nbytes = compile_options.record_marker ;
2526
2527 if (swrite (dtp->u.p.current_unit->s, &dummy, nbytes) != nbytes)
2528 generate_error (&dtp->common, LIBERROR_OS, NULL);
2529
2530 /* For sequential unformatted, if RECL= was not specified in the OPEN
2531 we write until we have more bytes than can fit in the subrecord
2532 markers, then we write a new subrecord. */
2533
2534 dtp->u.p.current_unit->bytes_left_subrecord =
2535 dtp->u.p.current_unit->recl_subrecord;
2536 dtp->u.p.current_unit->continued = continued;
2537 }
2538
2539
2540 /* Position to the next record prior to transfer. We are assumed to
2541 be before the next record. We also calculate the bytes in the next
2542 record. */
2543
2544 static void
2545 pre_position (st_parameter_dt *dtp)
2546 {
2547 if (dtp->u.p.current_unit->current_record)
2548 return; /* Already positioned. */
2549
2550 switch (current_mode (dtp))
2551 {
2552 case FORMATTED_STREAM:
2553 case UNFORMATTED_STREAM:
2554 /* There are no records with stream I/O. If the position was specified
2555 data_transfer_init has already positioned the file. If no position
2556 was specified, we continue from where we last left off. I.e.
2557 there is nothing to do here. */
2558 break;
2559
2560 case UNFORMATTED_SEQUENTIAL:
2561 if (dtp->u.p.mode == READING)
2562 us_read (dtp, 0);
2563 else
2564 us_write (dtp, 0);
2565
2566 break;
2567
2568 case FORMATTED_SEQUENTIAL:
2569 case FORMATTED_DIRECT:
2570 case UNFORMATTED_DIRECT:
2571 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2572 break;
2573 }
2574
2575 dtp->u.p.current_unit->current_record = 1;
2576 }
2577
2578
2579 /* Initialize things for a data transfer. This code is common for
2580 both reading and writing. */
2581
2582 static void
2583 data_transfer_init (st_parameter_dt *dtp, int read_flag)
2584 {
2585 unit_flags u_flags; /* Used for creating a unit if needed. */
2586 GFC_INTEGER_4 cf = dtp->common.flags;
2587 namelist_info *ionml;
2588
2589 ionml = ((cf & IOPARM_DT_IONML_SET) != 0) ? dtp->u.p.ionml : NULL;
2590
2591 memset (&dtp->u.p, 0, sizeof (dtp->u.p));
2592
2593 dtp->u.p.ionml = ionml;
2594 dtp->u.p.mode = read_flag ? READING : WRITING;
2595
2596 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2597 return;
2598
2599 if ((cf & IOPARM_DT_HAS_SIZE) != 0)
2600 dtp->u.p.size_used = 0; /* Initialize the count. */
2601
2602 dtp->u.p.current_unit = get_unit (dtp, 1);
2603
2604 if (dtp->u.p.current_unit->s == NULL)
2605 { /* Open the unit with some default flags. */
2606 st_parameter_open opp;
2607 unit_convert conv;
2608
2609 if (dtp->common.unit < 0 && !is_internal_unit (dtp))
2610 {
2611 close_unit (dtp->u.p.current_unit);
2612 dtp->u.p.current_unit = NULL;
2613 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2614 "Bad unit number in statement");
2615 return;
2616 }
2617 memset (&u_flags, '\0', sizeof (u_flags));
2618 u_flags.access = ACCESS_SEQUENTIAL;
2619 u_flags.action = ACTION_READWRITE;
2620
2621 /* Is it unformatted? */
2622 if (!(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT
2623 | IOPARM_DT_IONML_SET)))
2624 u_flags.form = FORM_UNFORMATTED;
2625 else
2626 u_flags.form = FORM_UNSPECIFIED;
2627
2628 u_flags.delim = DELIM_UNSPECIFIED;
2629 u_flags.blank = BLANK_UNSPECIFIED;
2630 u_flags.pad = PAD_UNSPECIFIED;
2631 u_flags.decimal = DECIMAL_UNSPECIFIED;
2632 u_flags.encoding = ENCODING_UNSPECIFIED;
2633 u_flags.async = ASYNC_UNSPECIFIED;
2634 u_flags.round = ROUND_UNSPECIFIED;
2635 u_flags.sign = SIGN_UNSPECIFIED;
2636
2637 u_flags.status = STATUS_UNKNOWN;
2638
2639 conv = get_unformatted_convert (dtp->common.unit);
2640
2641 if (conv == GFC_CONVERT_NONE)
2642 conv = compile_options.convert;
2643
2644 /* We use big_endian, which is 0 on little-endian machines
2645 and 1 on big-endian machines. */
2646 switch (conv)
2647 {
2648 case GFC_CONVERT_NATIVE:
2649 case GFC_CONVERT_SWAP:
2650 break;
2651
2652 case GFC_CONVERT_BIG:
2653 conv = big_endian ? GFC_CONVERT_NATIVE : GFC_CONVERT_SWAP;
2654 break;
2655
2656 case GFC_CONVERT_LITTLE:
2657 conv = big_endian ? GFC_CONVERT_SWAP : GFC_CONVERT_NATIVE;
2658 break;
2659
2660 default:
2661 internal_error (&opp.common, "Illegal value for CONVERT");
2662 break;
2663 }
2664
2665 u_flags.convert = conv;
2666
2667 opp.common = dtp->common;
2668 opp.common.flags &= IOPARM_COMMON_MASK;
2669 dtp->u.p.current_unit = new_unit (&opp, dtp->u.p.current_unit, &u_flags);
2670 dtp->common.flags &= ~IOPARM_COMMON_MASK;
2671 dtp->common.flags |= (opp.common.flags & IOPARM_COMMON_MASK);
2672 if (dtp->u.p.current_unit == NULL)
2673 return;
2674 }
2675
2676 /* Check the action. */
2677
2678 if (read_flag && dtp->u.p.current_unit->flags.action == ACTION_WRITE)
2679 {
2680 generate_error (&dtp->common, LIBERROR_BAD_ACTION,
2681 "Cannot read from file opened for WRITE");
2682 return;
2683 }
2684
2685 if (!read_flag && dtp->u.p.current_unit->flags.action == ACTION_READ)
2686 {
2687 generate_error (&dtp->common, LIBERROR_BAD_ACTION,
2688 "Cannot write to file opened for READ");
2689 return;
2690 }
2691
2692 dtp->u.p.first_item = 1;
2693
2694 /* Check the format. */
2695
2696 if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
2697 parse_format (dtp);
2698
2699 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED
2700 && (cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
2701 != 0)
2702 {
2703 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2704 "Format present for UNFORMATTED data transfer");
2705 return;
2706 }
2707
2708 if ((cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0 && dtp->u.p.ionml != NULL)
2709 {
2710 if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
2711 {
2712 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2713 "A format cannot be specified with a namelist");
2714 return;
2715 }
2716 }
2717 else if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
2718 !(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT)))
2719 {
2720 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2721 "Missing format for FORMATTED data transfer");
2722 return;
2723 }
2724
2725 if (is_internal_unit (dtp)
2726 && dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
2727 {
2728 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2729 "Internal file cannot be accessed by UNFORMATTED "
2730 "data transfer");
2731 return;
2732 }
2733
2734 /* Check the record or position number. */
2735
2736 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT
2737 && (cf & IOPARM_DT_HAS_REC) == 0)
2738 {
2739 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
2740 "Direct access data transfer requires record number");
2741 return;
2742 }
2743
2744 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
2745 {
2746 if ((cf & IOPARM_DT_HAS_REC) != 0)
2747 {
2748 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2749 "Record number not allowed for sequential access "
2750 "data transfer");
2751 return;
2752 }
2753
2754 if (compile_options.warn_std &&
2755 dtp->u.p.current_unit->endfile == AFTER_ENDFILE)
2756 {
2757 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2758 "Sequential READ or WRITE not allowed after "
2759 "EOF marker, possibly use REWIND or BACKSPACE");
2760 return;
2761 }
2762 }
2763 /* Process the ADVANCE option. */
2764
2765 dtp->u.p.advance_status
2766 = !(cf & IOPARM_DT_HAS_ADVANCE) ? ADVANCE_UNSPECIFIED :
2767 find_option (&dtp->common, dtp->advance, dtp->advance_len, advance_opt,
2768 "Bad ADVANCE parameter in data transfer statement");
2769
2770 if (dtp->u.p.advance_status != ADVANCE_UNSPECIFIED)
2771 {
2772 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
2773 {
2774 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2775 "ADVANCE specification conflicts with sequential "
2776 "access");
2777 return;
2778 }
2779
2780 if (is_internal_unit (dtp))
2781 {
2782 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2783 "ADVANCE specification conflicts with internal file");
2784 return;
2785 }
2786
2787 if ((cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
2788 != IOPARM_DT_HAS_FORMAT)
2789 {
2790 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2791 "ADVANCE specification requires an explicit format");
2792 return;
2793 }
2794 }
2795
2796 if (read_flag)
2797 {
2798 dtp->u.p.current_unit->previous_nonadvancing_write = 0;
2799
2800 if ((cf & IOPARM_EOR) != 0 && dtp->u.p.advance_status != ADVANCE_NO)
2801 {
2802 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
2803 "EOR specification requires an ADVANCE specification "
2804 "of NO");
2805 return;
2806 }
2807
2808 if ((cf & IOPARM_DT_HAS_SIZE) != 0
2809 && dtp->u.p.advance_status != ADVANCE_NO)
2810 {
2811 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
2812 "SIZE specification requires an ADVANCE "
2813 "specification of NO");
2814 return;
2815 }
2816 }
2817 else
2818 { /* Write constraints. */
2819 if ((cf & IOPARM_END) != 0)
2820 {
2821 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2822 "END specification cannot appear in a write "
2823 "statement");
2824 return;
2825 }
2826
2827 if ((cf & IOPARM_EOR) != 0)
2828 {
2829 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2830 "EOR specification cannot appear in a write "
2831 "statement");
2832 return;
2833 }
2834
2835 if ((cf & IOPARM_DT_HAS_SIZE) != 0)
2836 {
2837 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2838 "SIZE specification cannot appear in a write "
2839 "statement");
2840 return;
2841 }
2842 }
2843
2844 if (dtp->u.p.advance_status == ADVANCE_UNSPECIFIED)
2845 dtp->u.p.advance_status = ADVANCE_YES;
2846
2847 /* Check the decimal mode. */
2848 dtp->u.p.current_unit->decimal_status
2849 = !(cf & IOPARM_DT_HAS_DECIMAL) ? DECIMAL_UNSPECIFIED :
2850 find_option (&dtp->common, dtp->decimal, dtp->decimal_len,
2851 decimal_opt, "Bad DECIMAL parameter in data transfer "
2852 "statement");
2853
2854 if (dtp->u.p.current_unit->decimal_status == DECIMAL_UNSPECIFIED)
2855 dtp->u.p.current_unit->decimal_status = dtp->u.p.current_unit->flags.decimal;
2856
2857 /* Check the round mode. */
2858 dtp->u.p.current_unit->round_status
2859 = !(cf & IOPARM_DT_HAS_ROUND) ? ROUND_UNSPECIFIED :
2860 find_option (&dtp->common, dtp->round, dtp->round_len,
2861 round_opt, "Bad ROUND parameter in data transfer "
2862 "statement");
2863
2864 if (dtp->u.p.current_unit->round_status == ROUND_UNSPECIFIED)
2865 dtp->u.p.current_unit->round_status = dtp->u.p.current_unit->flags.round;
2866
2867 /* Check the sign mode. */
2868 dtp->u.p.sign_status
2869 = !(cf & IOPARM_DT_HAS_SIGN) ? SIGN_UNSPECIFIED :
2870 find_option (&dtp->common, dtp->sign, dtp->sign_len, sign_opt,
2871 "Bad SIGN parameter in data transfer statement");
2872
2873 if (dtp->u.p.sign_status == SIGN_UNSPECIFIED)
2874 dtp->u.p.sign_status = dtp->u.p.current_unit->flags.sign;
2875
2876 /* Check the blank mode. */
2877 dtp->u.p.blank_status
2878 = !(cf & IOPARM_DT_HAS_BLANK) ? BLANK_UNSPECIFIED :
2879 find_option (&dtp->common, dtp->blank, dtp->blank_len,
2880 blank_opt,
2881 "Bad BLANK parameter in data transfer statement");
2882
2883 if (dtp->u.p.blank_status == BLANK_UNSPECIFIED)
2884 dtp->u.p.blank_status = dtp->u.p.current_unit->flags.blank;
2885
2886 /* Check the delim mode. */
2887 dtp->u.p.current_unit->delim_status
2888 = !(cf & IOPARM_DT_HAS_DELIM) ? DELIM_UNSPECIFIED :
2889 find_option (&dtp->common, dtp->delim, dtp->delim_len,
2890 delim_opt, "Bad DELIM parameter in data transfer statement");
2891
2892 if (dtp->u.p.current_unit->delim_status == DELIM_UNSPECIFIED)
2893 {
2894 if (ionml && dtp->u.p.current_unit->flags.delim == DELIM_UNSPECIFIED)
2895 dtp->u.p.current_unit->delim_status = DELIM_QUOTE;
2896 else
2897 dtp->u.p.current_unit->delim_status = dtp->u.p.current_unit->flags.delim;
2898 }
2899
2900 /* Check the pad mode. */
2901 dtp->u.p.current_unit->pad_status
2902 = !(cf & IOPARM_DT_HAS_PAD) ? PAD_UNSPECIFIED :
2903 find_option (&dtp->common, dtp->pad, dtp->pad_len, pad_opt,
2904 "Bad PAD parameter in data transfer statement");
2905
2906 if (dtp->u.p.current_unit->pad_status == PAD_UNSPECIFIED)
2907 dtp->u.p.current_unit->pad_status = dtp->u.p.current_unit->flags.pad;
2908
2909 /* Check to see if we might be reading what we wrote before */
2910
2911 if (dtp->u.p.mode != dtp->u.p.current_unit->mode
2912 && !is_internal_unit (dtp))
2913 {
2914 int pos = fbuf_reset (dtp->u.p.current_unit);
2915 if (pos != 0)
2916 sseek (dtp->u.p.current_unit->s, pos, SEEK_CUR);
2917 sflush(dtp->u.p.current_unit->s);
2918 }
2919
2920 /* Check the POS= specifier: that it is in range and that it is used with a
2921 unit that has been connected for STREAM access. F2003 9.5.1.10. */
2922
2923 if (((cf & IOPARM_DT_HAS_POS) != 0))
2924 {
2925 if (is_stream_io (dtp))
2926 {
2927
2928 if (dtp->pos <= 0)
2929 {
2930 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2931 "POS=specifier must be positive");
2932 return;
2933 }
2934
2935 if (dtp->pos >= dtp->u.p.current_unit->maxrec)
2936 {
2937 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2938 "POS=specifier too large");
2939 return;
2940 }
2941
2942 dtp->rec = dtp->pos;
2943
2944 if (dtp->u.p.mode == READING)
2945 {
2946 /* Reset the endfile flag; if we hit EOF during reading
2947 we'll set the flag and generate an error at that point
2948 rather than worrying about it here. */
2949 dtp->u.p.current_unit->endfile = NO_ENDFILE;
2950 }
2951
2952 if (dtp->pos != dtp->u.p.current_unit->strm_pos)
2953 {
2954 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
2955 if (sseek (dtp->u.p.current_unit->s, dtp->pos - 1, SEEK_SET) < 0)
2956 {
2957 generate_error (&dtp->common, LIBERROR_OS, NULL);
2958 return;
2959 }
2960 dtp->u.p.current_unit->strm_pos = dtp->pos;
2961 }
2962 }
2963 else
2964 {
2965 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2966 "POS=specifier not allowed, "
2967 "Try OPEN with ACCESS='stream'");
2968 return;
2969 }
2970 }
2971
2972
2973 /* Sanity checks on the record number. */
2974 if ((cf & IOPARM_DT_HAS_REC) != 0)
2975 {
2976 if (dtp->rec <= 0)
2977 {
2978 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2979 "Record number must be positive");
2980 return;
2981 }
2982
2983 if (dtp->rec >= dtp->u.p.current_unit->maxrec)
2984 {
2985 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2986 "Record number too large");
2987 return;
2988 }
2989
2990 /* Make sure format buffer is reset. */
2991 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED)
2992 fbuf_reset (dtp->u.p.current_unit);
2993
2994
2995 /* Check whether the record exists to be read. Only
2996 a partial record needs to exist. */
2997
2998 if (dtp->u.p.mode == READING && (dtp->rec - 1)
2999 * dtp->u.p.current_unit->recl >= ssize (dtp->u.p.current_unit->s))
3000 {
3001 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
3002 "Non-existing record number");
3003 return;
3004 }
3005
3006 /* Position the file. */
3007 if (sseek (dtp->u.p.current_unit->s, (gfc_offset) (dtp->rec - 1)
3008 * dtp->u.p.current_unit->recl, SEEK_SET) < 0)
3009 {
3010 generate_error (&dtp->common, LIBERROR_OS, NULL);
3011 return;
3012 }
3013
3014 /* TODO: This is required to maintain compatibility between
3015 4.3 and 4.4 runtime. Remove when ABI changes from 4.3 */
3016
3017 if (is_stream_io (dtp))
3018 dtp->u.p.current_unit->strm_pos = dtp->rec;
3019
3020 /* TODO: Un-comment this code when ABI changes from 4.3.
3021 if (dtp->u.p.current_unit->flags.access == ACCESS_STREAM)
3022 {
3023 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
3024 "Record number not allowed for stream access "
3025 "data transfer");
3026 return;
3027 } */
3028 }
3029
3030 /* Bugware for badly written mixed C-Fortran I/O. */
3031 if (!is_internal_unit (dtp))
3032 flush_if_preconnected(dtp->u.p.current_unit->s);
3033
3034 dtp->u.p.current_unit->mode = dtp->u.p.mode;
3035
3036 /* Set the maximum position reached from the previous I/O operation. This
3037 could be greater than zero from a previous non-advancing write. */
3038 dtp->u.p.max_pos = dtp->u.p.current_unit->saved_pos;
3039
3040 pre_position (dtp);
3041
3042
3043 /* Set up the subroutine that will handle the transfers. */
3044
3045 if (read_flag)
3046 {
3047 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
3048 dtp->u.p.transfer = unformatted_read;
3049 else
3050 {
3051 if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
3052 {
3053 if (dtp->u.p.current_unit->child_dtio == 0)
3054 dtp->u.p.current_unit->last_char = EOF - 1;
3055 dtp->u.p.transfer = list_formatted_read;
3056 }
3057 else
3058 dtp->u.p.transfer = formatted_transfer;
3059 }
3060 }
3061 else
3062 {
3063 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
3064 dtp->u.p.transfer = unformatted_write;
3065 else
3066 {
3067 if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
3068 dtp->u.p.transfer = list_formatted_write;
3069 else
3070 dtp->u.p.transfer = formatted_transfer;
3071 }
3072 }
3073
3074 /* Make sure that we don't do a read after a nonadvancing write. */
3075
3076 if (read_flag)
3077 {
3078 if (dtp->u.p.current_unit->read_bad && !is_stream_io (dtp))
3079 {
3080 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
3081 "Cannot READ after a nonadvancing WRITE");
3082 return;
3083 }
3084 }
3085 else
3086 {
3087 if (dtp->u.p.advance_status == ADVANCE_YES && !dtp->u.p.seen_dollar)
3088 dtp->u.p.current_unit->read_bad = 1;
3089 }
3090
3091 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED)
3092 {
3093 #ifdef HAVE_USELOCALE
3094 dtp->u.p.old_locale = uselocale (c_locale);
3095 #else
3096 __gthread_mutex_lock (&old_locale_lock);
3097 if (!old_locale_ctr++)
3098 {
3099 old_locale = setlocale (LC_NUMERIC, NULL);
3100 setlocale (LC_NUMERIC, "C");
3101 }
3102 __gthread_mutex_unlock (&old_locale_lock);
3103 #endif
3104 /* Start the data transfer if we are doing a formatted transfer. */
3105 if ((cf & (IOPARM_DT_LIST_FORMAT | IOPARM_DT_HAS_NAMELIST_NAME)) == 0
3106 && dtp->u.p.ionml == NULL)
3107 formatted_transfer (dtp, 0, NULL, 0, 0, 1);
3108 }
3109 }
3110
3111
3112 /* Initialize an array_loop_spec given the array descriptor. The function
3113 returns the index of the last element of the array, and also returns
3114 starting record, where the first I/O goes to (necessary in case of
3115 negative strides). */
3116
3117 gfc_offset
3118 init_loop_spec (gfc_array_char *desc, array_loop_spec *ls,
3119 gfc_offset *start_record)
3120 {
3121 int rank = GFC_DESCRIPTOR_RANK(desc);
3122 int i;
3123 gfc_offset index;
3124 int empty;
3125
3126 empty = 0;
3127 index = 1;
3128 *start_record = 0;
3129
3130 for (i=0; i<rank; i++)
3131 {
3132 ls[i].idx = GFC_DESCRIPTOR_LBOUND(desc,i);
3133 ls[i].start = GFC_DESCRIPTOR_LBOUND(desc,i);
3134 ls[i].end = GFC_DESCRIPTOR_UBOUND(desc,i);
3135 ls[i].step = GFC_DESCRIPTOR_STRIDE(desc,i);
3136 empty = empty || (GFC_DESCRIPTOR_UBOUND(desc,i)
3137 < GFC_DESCRIPTOR_LBOUND(desc,i));
3138
3139 if (GFC_DESCRIPTOR_STRIDE(desc,i) > 0)
3140 {
3141 index += (GFC_DESCRIPTOR_EXTENT(desc,i) - 1)
3142 * GFC_DESCRIPTOR_STRIDE(desc,i);
3143 }
3144 else
3145 {
3146 index -= (GFC_DESCRIPTOR_EXTENT(desc,i) - 1)
3147 * GFC_DESCRIPTOR_STRIDE(desc,i);
3148 *start_record -= (GFC_DESCRIPTOR_EXTENT(desc,i) - 1)
3149 * GFC_DESCRIPTOR_STRIDE(desc,i);
3150 }
3151 }
3152
3153 if (empty)
3154 return 0;
3155 else
3156 return index;
3157 }
3158
3159 /* Determine the index to the next record in an internal unit array by
3160 by incrementing through the array_loop_spec. */
3161
3162 gfc_offset
3163 next_array_record (st_parameter_dt *dtp, array_loop_spec *ls, int *finished)
3164 {
3165 int i, carry;
3166 gfc_offset index;
3167
3168 carry = 1;
3169 index = 0;
3170
3171 for (i = 0; i < dtp->u.p.current_unit->rank; i++)
3172 {
3173 if (carry)
3174 {
3175 ls[i].idx++;
3176 if (ls[i].idx > ls[i].end)
3177 {
3178 ls[i].idx = ls[i].start;
3179 carry = 1;
3180 }
3181 else
3182 carry = 0;
3183 }
3184 index = index + (ls[i].idx - ls[i].start) * ls[i].step;
3185 }
3186
3187 *finished = carry;
3188
3189 return index;
3190 }
3191
3192
3193
3194 /* Skip to the end of the current record, taking care of an optional
3195 record marker of size bytes. If the file is not seekable, we
3196 read chunks of size MAX_READ until we get to the right
3197 position. */
3198
3199 static void
3200 skip_record (st_parameter_dt *dtp, ssize_t bytes)
3201 {
3202 ssize_t rlength, readb;
3203 #define MAX_READ 4096
3204 char p[MAX_READ];
3205
3206 dtp->u.p.current_unit->bytes_left_subrecord += bytes;
3207 if (dtp->u.p.current_unit->bytes_left_subrecord == 0)
3208 return;
3209
3210 /* Direct access files do not generate END conditions,
3211 only I/O errors. */
3212 if (sseek (dtp->u.p.current_unit->s,
3213 dtp->u.p.current_unit->bytes_left_subrecord, SEEK_CUR) < 0)
3214 {
3215 /* Seeking failed, fall back to seeking by reading data. */
3216 while (dtp->u.p.current_unit->bytes_left_subrecord > 0)
3217 {
3218 rlength =
3219 (MAX_READ < dtp->u.p.current_unit->bytes_left_subrecord) ?
3220 MAX_READ : dtp->u.p.current_unit->bytes_left_subrecord;
3221
3222 readb = sread (dtp->u.p.current_unit->s, p, rlength);
3223 if (readb < 0)
3224 {
3225 generate_error (&dtp->common, LIBERROR_OS, NULL);
3226 return;
3227 }
3228
3229 dtp->u.p.current_unit->bytes_left_subrecord -= readb;
3230 }
3231 return;
3232 }
3233 dtp->u.p.current_unit->bytes_left_subrecord = 0;
3234 }
3235
3236
3237 /* Advance to the next record reading unformatted files, taking
3238 care of subrecords. If complete_record is nonzero, we loop
3239 until all subrecords are cleared. */
3240
3241 static void
3242 next_record_r_unf (st_parameter_dt *dtp, int complete_record)
3243 {
3244 size_t bytes;
3245
3246 bytes = compile_options.record_marker == 0 ?
3247 sizeof (GFC_INTEGER_4) : compile_options.record_marker;
3248
3249 while(1)
3250 {
3251
3252 /* Skip over tail */
3253
3254 skip_record (dtp, bytes);
3255
3256 if ( ! (complete_record && dtp->u.p.current_unit->continued))
3257 return;
3258
3259 us_read (dtp, 1);
3260 }
3261 }
3262
3263
3264 static gfc_offset
3265 min_off (gfc_offset a, gfc_offset b)
3266 {
3267 return (a < b ? a : b);
3268 }
3269
3270
3271 /* Space to the next record for read mode. */
3272
3273 static void
3274 next_record_r (st_parameter_dt *dtp, int done)
3275 {
3276 gfc_offset record;
3277 int bytes_left;
3278 char p;
3279 int cc;
3280
3281 switch (current_mode (dtp))
3282 {
3283 /* No records in unformatted STREAM I/O. */
3284 case UNFORMATTED_STREAM:
3285 return;
3286
3287 case UNFORMATTED_SEQUENTIAL:
3288 next_record_r_unf (dtp, 1);
3289 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
3290 break;
3291
3292 case FORMATTED_DIRECT:
3293 case UNFORMATTED_DIRECT:
3294 skip_record (dtp, dtp->u.p.current_unit->bytes_left);
3295 break;
3296
3297 case FORMATTED_STREAM:
3298 case FORMATTED_SEQUENTIAL:
3299 /* read_sf has already terminated input because of an '\n', or
3300 we have hit EOF. */
3301 if (dtp->u.p.sf_seen_eor)
3302 {
3303 dtp->u.p.sf_seen_eor = 0;
3304 break;
3305 }
3306
3307 if (is_internal_unit (dtp))
3308 {
3309 if (is_array_io (dtp))
3310 {
3311 int finished;
3312
3313 record = next_array_record (dtp, dtp->u.p.current_unit->ls,
3314 &finished);
3315 if (!done && finished)
3316 hit_eof (dtp);
3317
3318 /* Now seek to this record. */
3319 record = record * dtp->u.p.current_unit->recl;
3320 if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
3321 {
3322 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3323 break;
3324 }
3325 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
3326 }
3327 else
3328 {
3329 bytes_left = (int) dtp->u.p.current_unit->bytes_left;
3330 bytes_left = min_off (bytes_left,
3331 ssize (dtp->u.p.current_unit->s)
3332 - stell (dtp->u.p.current_unit->s));
3333 if (sseek (dtp->u.p.current_unit->s,
3334 bytes_left, SEEK_CUR) < 0)
3335 {
3336 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3337 break;
3338 }
3339 dtp->u.p.current_unit->bytes_left
3340 = dtp->u.p.current_unit->recl;
3341 }
3342 break;
3343 }
3344 else
3345 {
3346 do
3347 {
3348 errno = 0;
3349 cc = fbuf_getc (dtp->u.p.current_unit);
3350 if (cc == EOF)
3351 {
3352 if (errno != 0)
3353 generate_error (&dtp->common, LIBERROR_OS, NULL);
3354 else
3355 {
3356 if (is_stream_io (dtp)
3357 || dtp->u.p.current_unit->pad_status == PAD_NO
3358 || dtp->u.p.current_unit->bytes_left
3359 == dtp->u.p.current_unit->recl)
3360 hit_eof (dtp);
3361 }
3362 break;
3363 }
3364
3365 if (is_stream_io (dtp))
3366 dtp->u.p.current_unit->strm_pos++;
3367
3368 p = (char) cc;
3369 }
3370 while (p != '\n');
3371 }
3372 break;
3373 }
3374 }
3375
3376
3377 /* Small utility function to write a record marker, taking care of
3378 byte swapping and of choosing the correct size. */
3379
3380 static int
3381 write_us_marker (st_parameter_dt *dtp, const gfc_offset buf)
3382 {
3383 size_t len;
3384 GFC_INTEGER_4 buf4;
3385 GFC_INTEGER_8 buf8;
3386
3387 if (compile_options.record_marker == 0)
3388 len = sizeof (GFC_INTEGER_4);
3389 else
3390 len = compile_options.record_marker;
3391
3392 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
3393 if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE))
3394 {
3395 switch (len)
3396 {
3397 case sizeof (GFC_INTEGER_4):
3398 buf4 = buf;
3399 return swrite (dtp->u.p.current_unit->s, &buf4, len);
3400 break;
3401
3402 case sizeof (GFC_INTEGER_8):
3403 buf8 = buf;
3404 return swrite (dtp->u.p.current_unit->s, &buf8, len);
3405 break;
3406
3407 default:
3408 runtime_error ("Illegal value for record marker");
3409 break;
3410 }
3411 }
3412 else
3413 {
3414 uint32_t u32;
3415 uint64_t u64;
3416 switch (len)
3417 {
3418 case sizeof (GFC_INTEGER_4):
3419 buf4 = buf;
3420 memcpy (&u32, &buf4, sizeof (u32));
3421 u32 = __builtin_bswap32 (u32);
3422 return swrite (dtp->u.p.current_unit->s, &u32, len);
3423 break;
3424
3425 case sizeof (GFC_INTEGER_8):
3426 buf8 = buf;
3427 memcpy (&u64, &buf8, sizeof (u64));
3428 u64 = __builtin_bswap64 (u64);
3429 return swrite (dtp->u.p.current_unit->s, &u64, len);
3430 break;
3431
3432 default:
3433 runtime_error ("Illegal value for record marker");
3434 break;
3435 }
3436 }
3437
3438 }
3439
3440 /* Position to the next (sub)record in write mode for
3441 unformatted sequential files. */
3442
3443 static void
3444 next_record_w_unf (st_parameter_dt *dtp, int next_subrecord)
3445 {
3446 gfc_offset m, m_write, record_marker;
3447
3448 /* Bytes written. */
3449 m = dtp->u.p.current_unit->recl_subrecord
3450 - dtp->u.p.current_unit->bytes_left_subrecord;
3451
3452 if (compile_options.record_marker == 0)
3453 record_marker = sizeof (GFC_INTEGER_4);
3454 else
3455 record_marker = compile_options.record_marker;
3456
3457 /* Seek to the head and overwrite the bogus length with the real
3458 length. */
3459
3460 if (unlikely (sseek (dtp->u.p.current_unit->s, - m - record_marker,
3461 SEEK_CUR) < 0))
3462 goto io_error;
3463
3464 if (next_subrecord)
3465 m_write = -m;
3466 else
3467 m_write = m;
3468
3469 if (unlikely (write_us_marker (dtp, m_write) < 0))
3470 goto io_error;
3471
3472 /* Seek past the end of the current record. */
3473
3474 if (unlikely (sseek (dtp->u.p.current_unit->s, m, SEEK_CUR) < 0))
3475 goto io_error;
3476
3477 /* Write the length tail. If we finish a record containing
3478 subrecords, we write out the negative length. */
3479
3480 if (dtp->u.p.current_unit->continued)
3481 m_write = -m;
3482 else
3483 m_write = m;
3484
3485 if (unlikely (write_us_marker (dtp, m_write) < 0))
3486 goto io_error;
3487
3488 return;
3489
3490 io_error:
3491 generate_error (&dtp->common, LIBERROR_OS, NULL);
3492 return;
3493
3494 }
3495
3496
3497 /* Utility function like memset() but operating on streams. Return
3498 value is same as for POSIX write(). */
3499
3500 static ssize_t
3501 sset (stream * s, int c, ssize_t nbyte)
3502 {
3503 #define WRITE_CHUNK 256
3504 char p[WRITE_CHUNK];
3505 ssize_t bytes_left, trans;
3506
3507 if (nbyte < WRITE_CHUNK)
3508 memset (p, c, nbyte);
3509 else
3510 memset (p, c, WRITE_CHUNK);
3511
3512 bytes_left = nbyte;
3513 while (bytes_left > 0)
3514 {
3515 trans = (bytes_left < WRITE_CHUNK) ? bytes_left : WRITE_CHUNK;
3516 trans = swrite (s, p, trans);
3517 if (trans <= 0)
3518 return trans;
3519 bytes_left -= trans;
3520 }
3521
3522 return nbyte - bytes_left;
3523 }
3524
3525
3526 /* Position to the next record in write mode. */
3527
3528 static void
3529 next_record_w (st_parameter_dt *dtp, int done)
3530 {
3531 gfc_offset m, record, max_pos;
3532 int length;
3533
3534 /* Zero counters for X- and T-editing. */
3535 max_pos = dtp->u.p.max_pos;
3536 dtp->u.p.max_pos = dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
3537
3538 switch (current_mode (dtp))
3539 {
3540 /* No records in unformatted STREAM I/O. */
3541 case UNFORMATTED_STREAM:
3542 return;
3543
3544 case FORMATTED_DIRECT:
3545 if (dtp->u.p.current_unit->bytes_left == 0)
3546 break;
3547
3548 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
3549 fbuf_flush (dtp->u.p.current_unit, WRITING);
3550 if (sset (dtp->u.p.current_unit->s, ' ',
3551 dtp->u.p.current_unit->bytes_left)
3552 != dtp->u.p.current_unit->bytes_left)
3553 goto io_error;
3554
3555 break;
3556
3557 case UNFORMATTED_DIRECT:
3558 if (dtp->u.p.current_unit->bytes_left > 0)
3559 {
3560 length = (int) dtp->u.p.current_unit->bytes_left;
3561 if (sset (dtp->u.p.current_unit->s, 0, length) != length)
3562 goto io_error;
3563 }
3564 break;
3565
3566 case UNFORMATTED_SEQUENTIAL:
3567 next_record_w_unf (dtp, 0);
3568 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
3569 break;
3570
3571 case FORMATTED_STREAM:
3572 case FORMATTED_SEQUENTIAL:
3573
3574 if (is_internal_unit (dtp))
3575 {
3576 char *p;
3577 if (is_array_io (dtp))
3578 {
3579 int finished;
3580
3581 length = (int) dtp->u.p.current_unit->bytes_left;
3582
3583 /* If the farthest position reached is greater than current
3584 position, adjust the position and set length to pad out
3585 whats left. Otherwise just pad whats left.
3586 (for character array unit) */
3587 m = dtp->u.p.current_unit->recl
3588 - dtp->u.p.current_unit->bytes_left;
3589 if (max_pos > m)
3590 {
3591 length = (int) (max_pos - m);
3592 if (sseek (dtp->u.p.current_unit->s,
3593 length, SEEK_CUR) < 0)
3594 {
3595 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3596 return;
3597 }
3598 length = (int) (dtp->u.p.current_unit->recl - max_pos);
3599 }
3600
3601 p = write_block (dtp, length);
3602 if (p == NULL)
3603 return;
3604
3605 if (unlikely (is_char4_unit (dtp)))
3606 {
3607 gfc_char4_t *p4 = (gfc_char4_t *) p;
3608 memset4 (p4, ' ', length);
3609 }
3610 else
3611 memset (p, ' ', length);
3612
3613 /* Now that the current record has been padded out,
3614 determine where the next record in the array is. */
3615 record = next_array_record (dtp, dtp->u.p.current_unit->ls,
3616 &finished);
3617 if (finished)
3618 dtp->u.p.current_unit->endfile = AT_ENDFILE;
3619
3620 /* Now seek to this record */
3621 record = record * dtp->u.p.current_unit->recl;
3622
3623 if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
3624 {
3625 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3626 return;
3627 }
3628
3629 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
3630 }
3631 else
3632 {
3633 length = 1;
3634
3635 /* If this is the last call to next_record move to the farthest
3636 position reached and set length to pad out the remainder
3637 of the record. (for character scaler unit) */
3638 if (done)
3639 {
3640 m = dtp->u.p.current_unit->recl
3641 - dtp->u.p.current_unit->bytes_left;
3642 if (max_pos > m)
3643 {
3644 length = (int) (max_pos - m);
3645 if (sseek (dtp->u.p.current_unit->s,
3646 length, SEEK_CUR) < 0)
3647 {
3648 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3649 return;
3650 }
3651 length = (int) (dtp->u.p.current_unit->recl - max_pos);
3652 }
3653 else
3654 length = (int) dtp->u.p.current_unit->bytes_left;
3655 }
3656 if (length > 0)
3657 {
3658 p = write_block (dtp, length);
3659 if (p == NULL)
3660 return;
3661
3662 if (unlikely (is_char4_unit (dtp)))
3663 {
3664 gfc_char4_t *p4 = (gfc_char4_t *) p;
3665 memset4 (p4, (gfc_char4_t) ' ', length);
3666 }
3667 else
3668 memset (p, ' ', length);
3669 }
3670 }
3671 }
3672 else
3673 {
3674 #ifdef HAVE_CRLF
3675 const int len = 2;
3676 #else
3677 const int len = 1;
3678 #endif
3679 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
3680 char * p = fbuf_alloc (dtp->u.p.current_unit, len);
3681 if (!p)
3682 goto io_error;
3683 #ifdef HAVE_CRLF
3684 *(p++) = '\r';
3685 #endif
3686 *p = '\n';
3687 if (is_stream_io (dtp))
3688 {
3689 dtp->u.p.current_unit->strm_pos += len;
3690 if (dtp->u.p.current_unit->strm_pos
3691 < ssize (dtp->u.p.current_unit->s))
3692 unit_truncate (dtp->u.p.current_unit,
3693 dtp->u.p.current_unit->strm_pos - 1,
3694 &dtp->common);
3695 }
3696 }
3697
3698 break;
3699
3700 io_error:
3701 generate_error (&dtp->common, LIBERROR_OS, NULL);
3702 break;
3703 }
3704 }
3705
3706 /* Position to the next record, which means moving to the end of the
3707 current record. This can happen under several different
3708 conditions. If the done flag is not set, we get ready to process
3709 the next record. */
3710
3711 void
3712 next_record (st_parameter_dt *dtp, int done)
3713 {
3714 gfc_offset fp; /* File position. */
3715
3716 dtp->u.p.current_unit->read_bad = 0;
3717
3718 if (dtp->u.p.mode == READING)
3719 next_record_r (dtp, done);
3720 else
3721 next_record_w (dtp, done);
3722
3723 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
3724
3725 if (!is_stream_io (dtp))
3726 {
3727 /* Since we have changed the position, set it to unspecified so
3728 that INQUIRE(POSITION=) knows it needs to look into it. */
3729 if (done)
3730 dtp->u.p.current_unit->flags.position = POSITION_UNSPECIFIED;
3731
3732 dtp->u.p.current_unit->current_record = 0;
3733 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
3734 {
3735 fp = stell (dtp->u.p.current_unit->s);
3736 /* Calculate next record, rounding up partial records. */
3737 dtp->u.p.current_unit->last_record =
3738 (fp + dtp->u.p.current_unit->recl) /
3739 dtp->u.p.current_unit->recl - 1;
3740 }
3741 else
3742 dtp->u.p.current_unit->last_record++;
3743 }
3744
3745 if (!done)
3746 pre_position (dtp);
3747
3748 smarkeor (dtp->u.p.current_unit->s);
3749 }
3750
3751
3752 /* Finalize the current data transfer. For a nonadvancing transfer,
3753 this means advancing to the next record. For internal units close the
3754 stream associated with the unit. */
3755
3756 static void
3757 finalize_transfer (st_parameter_dt *dtp)
3758 {
3759 GFC_INTEGER_4 cf = dtp->common.flags;
3760
3761 if ((dtp->u.p.ionml != NULL)
3762 && (cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0)
3763 {
3764 if ((cf & IOPARM_DT_NAMELIST_READ_MODE) != 0)
3765 namelist_read (dtp);
3766 else
3767 namelist_write (dtp);
3768 }
3769
3770 if (dtp->u.p.current_unit && (dtp->u.p.current_unit->child_dtio > 0))
3771 return;
3772
3773 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
3774 *dtp->size = dtp->u.p.size_used;
3775
3776 if (dtp->u.p.eor_condition)
3777 {
3778 generate_error (&dtp->common, LIBERROR_EOR, NULL);
3779 goto done;
3780 }
3781
3782 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
3783 {
3784 if (dtp->u.p.current_unit && current_mode (dtp) == UNFORMATTED_SEQUENTIAL)
3785 dtp->u.p.current_unit->current_record = 0;
3786 goto done;
3787 }
3788
3789 dtp->u.p.transfer = NULL;
3790 if (dtp->u.p.current_unit == NULL)
3791 goto done;
3792
3793 if ((cf & IOPARM_DT_LIST_FORMAT) != 0 && dtp->u.p.mode == READING)
3794 {
3795 finish_list_read (dtp);
3796 goto done;
3797 }
3798
3799 if (dtp->u.p.mode == WRITING)
3800 dtp->u.p.current_unit->previous_nonadvancing_write
3801 = dtp->u.p.advance_status == ADVANCE_NO;
3802
3803 if (is_stream_io (dtp))
3804 {
3805 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
3806 && dtp->u.p.advance_status != ADVANCE_NO)
3807 next_record (dtp, 1);
3808
3809 goto done;
3810 }
3811
3812 dtp->u.p.current_unit->current_record = 0;
3813
3814 if (!is_internal_unit (dtp) && dtp->u.p.seen_dollar)
3815 {
3816 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
3817 dtp->u.p.seen_dollar = 0;
3818 goto done;
3819 }
3820
3821 /* For non-advancing I/O, save the current maximum position for use in the
3822 next I/O operation if needed. */
3823 if (dtp->u.p.advance_status == ADVANCE_NO)
3824 {
3825 if (dtp->u.p.skips > 0)
3826 {
3827 int tmp;
3828 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
3829 tmp = (int)(dtp->u.p.current_unit->recl
3830 - dtp->u.p.current_unit->bytes_left);
3831 dtp->u.p.max_pos =
3832 dtp->u.p.max_pos > tmp ? dtp->u.p.max_pos : tmp;
3833 dtp->u.p.skips = 0;
3834 }
3835 int bytes_written = (int) (dtp->u.p.current_unit->recl
3836 - dtp->u.p.current_unit->bytes_left);
3837 dtp->u.p.current_unit->saved_pos =
3838 dtp->u.p.max_pos > 0 ? dtp->u.p.max_pos - bytes_written : 0;
3839 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
3840 goto done;
3841 }
3842 else if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
3843 && dtp->u.p.mode == WRITING && !is_internal_unit (dtp))
3844 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
3845
3846 dtp->u.p.current_unit->saved_pos = 0;
3847
3848 next_record (dtp, 1);
3849
3850 done:
3851 #ifdef HAVE_USELOCALE
3852 if (dtp->u.p.old_locale != (locale_t) 0)
3853 {
3854 uselocale (dtp->u.p.old_locale);
3855 dtp->u.p.old_locale = (locale_t) 0;
3856 }
3857 #else
3858 __gthread_mutex_lock (&old_locale_lock);
3859 if (!--old_locale_ctr)
3860 {
3861 setlocale (LC_NUMERIC, old_locale);
3862 old_locale = NULL;
3863 }
3864 __gthread_mutex_unlock (&old_locale_lock);
3865 #endif
3866 }
3867
3868 /* Transfer function for IOLENGTH. It doesn't actually do any
3869 data transfer, it just updates the length counter. */
3870
3871 static void
3872 iolength_transfer (st_parameter_dt *dtp, bt type __attribute__((unused)),
3873 void *dest __attribute__ ((unused)),
3874 int kind __attribute__((unused)),
3875 size_t size, size_t nelems)
3876 {
3877 if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
3878 *dtp->iolength += (GFC_IO_INT) (size * nelems);
3879 }
3880
3881
3882 /* Initialize the IOLENGTH data transfer. This function is in essence
3883 a very much simplified version of data_transfer_init(), because it
3884 doesn't have to deal with units at all. */
3885
3886 static void
3887 iolength_transfer_init (st_parameter_dt *dtp)
3888 {
3889 if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
3890 *dtp->iolength = 0;
3891
3892 memset (&dtp->u.p, 0, sizeof (dtp->u.p));
3893
3894 /* Set up the subroutine that will handle the transfers. */
3895
3896 dtp->u.p.transfer = iolength_transfer;
3897 }
3898
3899
3900 /* Library entry point for the IOLENGTH form of the INQUIRE
3901 statement. The IOLENGTH form requires no I/O to be performed, but
3902 it must still be a runtime library call so that we can determine
3903 the iolength for dynamic arrays and such. */
3904
3905 extern void st_iolength (st_parameter_dt *);
3906 export_proto(st_iolength);
3907
3908 void
3909 st_iolength (st_parameter_dt *dtp)
3910 {
3911 library_start (&dtp->common);
3912 iolength_transfer_init (dtp);
3913 }
3914
3915 extern void st_iolength_done (st_parameter_dt *);
3916 export_proto(st_iolength_done);
3917
3918 void
3919 st_iolength_done (st_parameter_dt *dtp __attribute__((unused)))
3920 {
3921 free_ionml (dtp);
3922 library_end ();
3923 }
3924
3925
3926 /* The READ statement. */
3927
3928 extern void st_read (st_parameter_dt *);
3929 export_proto(st_read);
3930
3931 void
3932 st_read (st_parameter_dt *dtp)
3933 {
3934 library_start (&dtp->common);
3935
3936 data_transfer_init (dtp, 1);
3937 }
3938
3939 extern void st_read_done (st_parameter_dt *);
3940 export_proto(st_read_done);
3941
3942 void
3943 st_read_done (st_parameter_dt *dtp)
3944 {
3945 finalize_transfer (dtp);
3946
3947 free_ionml (dtp);
3948
3949 /* If this is a parent READ statement we do not need to retain the
3950 internal unit structure for child use. Free it and stash the unit
3951 number for reuse. */
3952 if (dtp->u.p.current_unit != NULL
3953 && dtp->u.p.current_unit->child_dtio == 0)
3954 {
3955 if (is_internal_unit (dtp) &&
3956 (dtp->common.flags & IOPARM_DT_HAS_UDTIO) == 0)
3957 {
3958 free (dtp->u.p.current_unit->filename);
3959 dtp->u.p.current_unit->filename = NULL;
3960 free_format_hash_table (dtp->u.p.current_unit);
3961 free (dtp->u.p.current_unit->s);
3962 dtp->u.p.current_unit->s = NULL;
3963 if (dtp->u.p.current_unit->ls)
3964 free (dtp->u.p.current_unit->ls);
3965 dtp->u.p.current_unit->ls = NULL;
3966 stash_internal_unit (dtp);
3967 }
3968 if (is_internal_unit (dtp) || dtp->u.p.format_not_saved)
3969 {
3970 free_format_data (dtp->u.p.fmt);
3971 free_format (dtp);
3972 }
3973 unlock_unit (dtp->u.p.current_unit);
3974 }
3975
3976 library_end ();
3977 }
3978
3979 extern void st_write (st_parameter_dt *);
3980 export_proto(st_write);
3981
3982 void
3983 st_write (st_parameter_dt *dtp)
3984 {
3985 library_start (&dtp->common);
3986 data_transfer_init (dtp, 0);
3987 }
3988
3989 extern void st_write_done (st_parameter_dt *);
3990 export_proto(st_write_done);
3991
3992 void
3993 st_write_done (st_parameter_dt *dtp)
3994 {
3995 finalize_transfer (dtp);
3996
3997 if (dtp->u.p.current_unit != NULL
3998 && dtp->u.p.current_unit->child_dtio == 0)
3999 {
4000 /* Deal with endfile conditions associated with sequential files. */
4001 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
4002 switch (dtp->u.p.current_unit->endfile)
4003 {
4004 case AT_ENDFILE: /* Remain at the endfile record. */
4005 break;
4006
4007 case AFTER_ENDFILE:
4008 dtp->u.p.current_unit->endfile = AT_ENDFILE; /* Just at it now. */
4009 break;
4010
4011 case NO_ENDFILE:
4012 /* Get rid of whatever is after this record. */
4013 if (!is_internal_unit (dtp))
4014 unit_truncate (dtp->u.p.current_unit,
4015 stell (dtp->u.p.current_unit->s),
4016 &dtp->common);
4017 dtp->u.p.current_unit->endfile = AT_ENDFILE;
4018 break;
4019 }
4020
4021 free_ionml (dtp);
4022
4023 /* If this is a parent WRITE statement we do not need to retain the
4024 internal unit structure for child use. Free it and stash the
4025 unit number for reuse. */
4026 if (is_internal_unit (dtp) &&
4027 (dtp->common.flags & IOPARM_DT_HAS_UDTIO) == 0)
4028 {
4029 free (dtp->u.p.current_unit->filename);
4030 dtp->u.p.current_unit->filename = NULL;
4031 free_format_hash_table (dtp->u.p.current_unit);
4032 free (dtp->u.p.current_unit->s);
4033 dtp->u.p.current_unit->s = NULL;
4034 if (dtp->u.p.current_unit->ls)
4035 free (dtp->u.p.current_unit->ls);
4036 dtp->u.p.current_unit->ls = NULL;
4037 stash_internal_unit (dtp);
4038 }
4039 if (is_internal_unit (dtp) || dtp->u.p.format_not_saved)
4040 {
4041 free_format_data (dtp->u.p.fmt);
4042 free_format (dtp);
4043 }
4044 unlock_unit (dtp->u.p.current_unit);
4045 }
4046 library_end ();
4047 }
4048
4049
4050 /* F2003: This is a stub for the runtime portion of the WAIT statement. */
4051 void
4052 st_wait (st_parameter_wait *wtp __attribute__((unused)))
4053 {
4054 }
4055
4056
4057 /* Receives the scalar information for namelist objects and stores it
4058 in a linked list of namelist_info types. */
4059
4060 static void
4061 set_nml_var (st_parameter_dt *dtp, void * var_addr, char * var_name,
4062 GFC_INTEGER_4 len, gfc_charlen_type string_length,
4063 GFC_INTEGER_4 dtype, void *dtio_sub, void *vtable)
4064 {
4065 namelist_info *t1 = NULL;
4066 namelist_info *nml;
4067 size_t var_name_len = strlen (var_name);
4068
4069 nml = (namelist_info*) xmalloc (sizeof (namelist_info));
4070
4071 nml->mem_pos = var_addr;
4072 nml->dtio_sub = dtio_sub;
4073 nml->vtable = vtable;
4074
4075 nml->var_name = (char*) xmalloc (var_name_len + 1);
4076 memcpy (nml->var_name, var_name, var_name_len);
4077 nml->var_name[var_name_len] = '\0';
4078
4079 nml->len = (int) len;
4080 nml->string_length = (index_type) string_length;
4081
4082 nml->var_rank = (int) (dtype & GFC_DTYPE_RANK_MASK);
4083 nml->size = (index_type) (dtype >> GFC_DTYPE_SIZE_SHIFT);
4084 nml->type = (bt) ((dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT);
4085
4086 if (nml->var_rank > 0)
4087 {
4088 nml->dim = (descriptor_dimension*)
4089 xmallocarray (nml->var_rank, sizeof (descriptor_dimension));
4090 nml->ls = (array_loop_spec*)
4091 xmallocarray (nml->var_rank, sizeof (array_loop_spec));
4092 }
4093 else
4094 {
4095 nml->dim = NULL;
4096 nml->ls = NULL;
4097 }
4098
4099 nml->next = NULL;
4100
4101 if ((dtp->common.flags & IOPARM_DT_IONML_SET) == 0)
4102 {
4103 dtp->common.flags |= IOPARM_DT_IONML_SET;
4104 dtp->u.p.ionml = nml;
4105 }
4106 else
4107 {
4108 for (t1 = dtp->u.p.ionml; t1->next; t1 = t1->next);
4109 t1->next = nml;
4110 }
4111 }
4112
4113 extern void st_set_nml_var (st_parameter_dt *dtp, void *, char *,
4114 GFC_INTEGER_4, gfc_charlen_type, GFC_INTEGER_4);
4115 export_proto(st_set_nml_var);
4116
4117 void
4118 st_set_nml_var (st_parameter_dt *dtp, void * var_addr, char * var_name,
4119 GFC_INTEGER_4 len, gfc_charlen_type string_length,
4120 GFC_INTEGER_4 dtype)
4121 {
4122 set_nml_var (dtp, var_addr, var_name, len, string_length,
4123 dtype, NULL, NULL);
4124 }
4125
4126
4127 /* Essentially the same as previous but carrying the dtio procedure
4128 and the vtable as additional arguments. */
4129 extern void st_set_nml_dtio_var (st_parameter_dt *dtp, void *, char *,
4130 GFC_INTEGER_4, gfc_charlen_type, GFC_INTEGER_4,
4131 void *, void *);
4132 export_proto(st_set_nml_dtio_var);
4133
4134
4135 void
4136 st_set_nml_dtio_var (st_parameter_dt *dtp, void * var_addr, char * var_name,
4137 GFC_INTEGER_4 len, gfc_charlen_type string_length,
4138 GFC_INTEGER_4 dtype, void *dtio_sub, void *vtable)
4139 {
4140 set_nml_var (dtp, var_addr, var_name, len, string_length,
4141 dtype, dtio_sub, vtable);
4142 }
4143
4144 /* Store the dimensional information for the namelist object. */
4145 extern void st_set_nml_var_dim (st_parameter_dt *, GFC_INTEGER_4,
4146 index_type, index_type,
4147 index_type);
4148 export_proto(st_set_nml_var_dim);
4149
4150 void
4151 st_set_nml_var_dim (st_parameter_dt *dtp, GFC_INTEGER_4 n_dim,
4152 index_type stride, index_type lbound,
4153 index_type ubound)
4154 {
4155 namelist_info * nml;
4156 int n;
4157
4158 n = (int)n_dim;
4159
4160 for (nml = dtp->u.p.ionml; nml->next; nml = nml->next);
4161
4162 GFC_DIMENSION_SET(nml->dim[n],lbound,ubound,stride);
4163 }
4164
4165
4166 /* Once upon a time, a poor innocent Fortran program was reading a
4167 file, when suddenly it hit the end-of-file (EOF). Unfortunately
4168 the OS doesn't tell whether we're at the EOF or whether we already
4169 went past it. Luckily our hero, libgfortran, keeps track of this.
4170 Call this function when you detect an EOF condition. See Section
4171 9.10.2 in F2003. */
4172
4173 void
4174 hit_eof (st_parameter_dt * dtp)
4175 {
4176 dtp->u.p.current_unit->flags.position = POSITION_APPEND;
4177
4178 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
4179 switch (dtp->u.p.current_unit->endfile)
4180 {
4181 case NO_ENDFILE:
4182 case AT_ENDFILE:
4183 generate_error (&dtp->common, LIBERROR_END, NULL);
4184 if (!is_internal_unit (dtp) && !dtp->u.p.namelist_mode)
4185 {
4186 dtp->u.p.current_unit->endfile = AFTER_ENDFILE;
4187 dtp->u.p.current_unit->current_record = 0;
4188 }
4189 else
4190 dtp->u.p.current_unit->endfile = AT_ENDFILE;
4191 break;
4192
4193 case AFTER_ENDFILE:
4194 generate_error (&dtp->common, LIBERROR_ENDFILE, NULL);
4195 dtp->u.p.current_unit->current_record = 0;
4196 break;
4197 }
4198 else
4199 {
4200 /* Non-sequential files don't have an ENDFILE record, so we
4201 can't be at AFTER_ENDFILE. */
4202 dtp->u.p.current_unit->endfile = AT_ENDFILE;
4203 generate_error (&dtp->common, LIBERROR_END, NULL);
4204 dtp->u.p.current_unit->current_record = 0;
4205 }
4206 }