PR 48587 Newunit allocator, cleanup
[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 == NULL)
2605 {
2606 /* This means we tried to access an external unit < 0 without
2607 having opened it first with NEWUNIT=. */
2608 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2609 "Unit number is negative and unit was not already "
2610 "opened with OPEN(NEWUNIT=...)");
2611 return;
2612 }
2613 else if (dtp->u.p.current_unit->s == NULL)
2614 { /* Open the unit with some default flags. */
2615 st_parameter_open opp;
2616 unit_convert conv;
2617
2618 memset (&u_flags, '\0', sizeof (u_flags));
2619 u_flags.access = ACCESS_SEQUENTIAL;
2620 u_flags.action = ACTION_READWRITE;
2621
2622 /* Is it unformatted? */
2623 if (!(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT
2624 | IOPARM_DT_IONML_SET)))
2625 u_flags.form = FORM_UNFORMATTED;
2626 else
2627 u_flags.form = FORM_UNSPECIFIED;
2628
2629 u_flags.delim = DELIM_UNSPECIFIED;
2630 u_flags.blank = BLANK_UNSPECIFIED;
2631 u_flags.pad = PAD_UNSPECIFIED;
2632 u_flags.decimal = DECIMAL_UNSPECIFIED;
2633 u_flags.encoding = ENCODING_UNSPECIFIED;
2634 u_flags.async = ASYNC_UNSPECIFIED;
2635 u_flags.round = ROUND_UNSPECIFIED;
2636 u_flags.sign = SIGN_UNSPECIFIED;
2637
2638 u_flags.status = STATUS_UNKNOWN;
2639
2640 conv = get_unformatted_convert (dtp->common.unit);
2641
2642 if (conv == GFC_CONVERT_NONE)
2643 conv = compile_options.convert;
2644
2645 /* We use big_endian, which is 0 on little-endian machines
2646 and 1 on big-endian machines. */
2647 switch (conv)
2648 {
2649 case GFC_CONVERT_NATIVE:
2650 case GFC_CONVERT_SWAP:
2651 break;
2652
2653 case GFC_CONVERT_BIG:
2654 conv = big_endian ? GFC_CONVERT_NATIVE : GFC_CONVERT_SWAP;
2655 break;
2656
2657 case GFC_CONVERT_LITTLE:
2658 conv = big_endian ? GFC_CONVERT_SWAP : GFC_CONVERT_NATIVE;
2659 break;
2660
2661 default:
2662 internal_error (&opp.common, "Illegal value for CONVERT");
2663 break;
2664 }
2665
2666 u_flags.convert = conv;
2667
2668 opp.common = dtp->common;
2669 opp.common.flags &= IOPARM_COMMON_MASK;
2670 dtp->u.p.current_unit = new_unit (&opp, dtp->u.p.current_unit, &u_flags);
2671 dtp->common.flags &= ~IOPARM_COMMON_MASK;
2672 dtp->common.flags |= (opp.common.flags & IOPARM_COMMON_MASK);
2673 if (dtp->u.p.current_unit == NULL)
2674 return;
2675 }
2676
2677 /* Check the action. */
2678
2679 if (read_flag && dtp->u.p.current_unit->flags.action == ACTION_WRITE)
2680 {
2681 generate_error (&dtp->common, LIBERROR_BAD_ACTION,
2682 "Cannot read from file opened for WRITE");
2683 return;
2684 }
2685
2686 if (!read_flag && dtp->u.p.current_unit->flags.action == ACTION_READ)
2687 {
2688 generate_error (&dtp->common, LIBERROR_BAD_ACTION,
2689 "Cannot write to file opened for READ");
2690 return;
2691 }
2692
2693 dtp->u.p.first_item = 1;
2694
2695 /* Check the format. */
2696
2697 if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
2698 parse_format (dtp);
2699
2700 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED
2701 && (cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
2702 != 0)
2703 {
2704 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2705 "Format present for UNFORMATTED data transfer");
2706 return;
2707 }
2708
2709 if ((cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0 && dtp->u.p.ionml != NULL)
2710 {
2711 if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
2712 {
2713 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2714 "A format cannot be specified with a namelist");
2715 return;
2716 }
2717 }
2718 else if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
2719 !(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT)))
2720 {
2721 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2722 "Missing format for FORMATTED data transfer");
2723 return;
2724 }
2725
2726 if (is_internal_unit (dtp)
2727 && dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
2728 {
2729 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2730 "Internal file cannot be accessed by UNFORMATTED "
2731 "data transfer");
2732 return;
2733 }
2734
2735 /* Check the record or position number. */
2736
2737 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT
2738 && (cf & IOPARM_DT_HAS_REC) == 0)
2739 {
2740 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
2741 "Direct access data transfer requires record number");
2742 return;
2743 }
2744
2745 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
2746 {
2747 if ((cf & IOPARM_DT_HAS_REC) != 0)
2748 {
2749 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2750 "Record number not allowed for sequential access "
2751 "data transfer");
2752 return;
2753 }
2754
2755 if (compile_options.warn_std &&
2756 dtp->u.p.current_unit->endfile == AFTER_ENDFILE)
2757 {
2758 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2759 "Sequential READ or WRITE not allowed after "
2760 "EOF marker, possibly use REWIND or BACKSPACE");
2761 return;
2762 }
2763 }
2764 /* Process the ADVANCE option. */
2765
2766 dtp->u.p.advance_status
2767 = !(cf & IOPARM_DT_HAS_ADVANCE) ? ADVANCE_UNSPECIFIED :
2768 find_option (&dtp->common, dtp->advance, dtp->advance_len, advance_opt,
2769 "Bad ADVANCE parameter in data transfer statement");
2770
2771 if (dtp->u.p.advance_status != ADVANCE_UNSPECIFIED)
2772 {
2773 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
2774 {
2775 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2776 "ADVANCE specification conflicts with sequential "
2777 "access");
2778 return;
2779 }
2780
2781 if (is_internal_unit (dtp))
2782 {
2783 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2784 "ADVANCE specification conflicts with internal file");
2785 return;
2786 }
2787
2788 if ((cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
2789 != IOPARM_DT_HAS_FORMAT)
2790 {
2791 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2792 "ADVANCE specification requires an explicit format");
2793 return;
2794 }
2795 }
2796
2797 if (read_flag)
2798 {
2799 dtp->u.p.current_unit->previous_nonadvancing_write = 0;
2800
2801 if ((cf & IOPARM_EOR) != 0 && dtp->u.p.advance_status != ADVANCE_NO)
2802 {
2803 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
2804 "EOR specification requires an ADVANCE specification "
2805 "of NO");
2806 return;
2807 }
2808
2809 if ((cf & IOPARM_DT_HAS_SIZE) != 0
2810 && dtp->u.p.advance_status != ADVANCE_NO)
2811 {
2812 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
2813 "SIZE specification requires an ADVANCE "
2814 "specification of NO");
2815 return;
2816 }
2817 }
2818 else
2819 { /* Write constraints. */
2820 if ((cf & IOPARM_END) != 0)
2821 {
2822 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2823 "END specification cannot appear in a write "
2824 "statement");
2825 return;
2826 }
2827
2828 if ((cf & IOPARM_EOR) != 0)
2829 {
2830 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2831 "EOR specification cannot appear in a write "
2832 "statement");
2833 return;
2834 }
2835
2836 if ((cf & IOPARM_DT_HAS_SIZE) != 0)
2837 {
2838 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2839 "SIZE specification cannot appear in a write "
2840 "statement");
2841 return;
2842 }
2843 }
2844
2845 if (dtp->u.p.advance_status == ADVANCE_UNSPECIFIED)
2846 dtp->u.p.advance_status = ADVANCE_YES;
2847
2848 /* Check the decimal mode. */
2849 dtp->u.p.current_unit->decimal_status
2850 = !(cf & IOPARM_DT_HAS_DECIMAL) ? DECIMAL_UNSPECIFIED :
2851 find_option (&dtp->common, dtp->decimal, dtp->decimal_len,
2852 decimal_opt, "Bad DECIMAL parameter in data transfer "
2853 "statement");
2854
2855 if (dtp->u.p.current_unit->decimal_status == DECIMAL_UNSPECIFIED)
2856 dtp->u.p.current_unit->decimal_status = dtp->u.p.current_unit->flags.decimal;
2857
2858 /* Check the round mode. */
2859 dtp->u.p.current_unit->round_status
2860 = !(cf & IOPARM_DT_HAS_ROUND) ? ROUND_UNSPECIFIED :
2861 find_option (&dtp->common, dtp->round, dtp->round_len,
2862 round_opt, "Bad ROUND parameter in data transfer "
2863 "statement");
2864
2865 if (dtp->u.p.current_unit->round_status == ROUND_UNSPECIFIED)
2866 dtp->u.p.current_unit->round_status = dtp->u.p.current_unit->flags.round;
2867
2868 /* Check the sign mode. */
2869 dtp->u.p.sign_status
2870 = !(cf & IOPARM_DT_HAS_SIGN) ? SIGN_UNSPECIFIED :
2871 find_option (&dtp->common, dtp->sign, dtp->sign_len, sign_opt,
2872 "Bad SIGN parameter in data transfer statement");
2873
2874 if (dtp->u.p.sign_status == SIGN_UNSPECIFIED)
2875 dtp->u.p.sign_status = dtp->u.p.current_unit->flags.sign;
2876
2877 /* Check the blank mode. */
2878 dtp->u.p.blank_status
2879 = !(cf & IOPARM_DT_HAS_BLANK) ? BLANK_UNSPECIFIED :
2880 find_option (&dtp->common, dtp->blank, dtp->blank_len,
2881 blank_opt,
2882 "Bad BLANK parameter in data transfer statement");
2883
2884 if (dtp->u.p.blank_status == BLANK_UNSPECIFIED)
2885 dtp->u.p.blank_status = dtp->u.p.current_unit->flags.blank;
2886
2887 /* Check the delim mode. */
2888 dtp->u.p.current_unit->delim_status
2889 = !(cf & IOPARM_DT_HAS_DELIM) ? DELIM_UNSPECIFIED :
2890 find_option (&dtp->common, dtp->delim, dtp->delim_len,
2891 delim_opt, "Bad DELIM parameter in data transfer statement");
2892
2893 if (dtp->u.p.current_unit->delim_status == DELIM_UNSPECIFIED)
2894 {
2895 if (ionml && dtp->u.p.current_unit->flags.delim == DELIM_UNSPECIFIED)
2896 dtp->u.p.current_unit->delim_status = DELIM_QUOTE;
2897 else
2898 dtp->u.p.current_unit->delim_status = dtp->u.p.current_unit->flags.delim;
2899 }
2900
2901 /* Check the pad mode. */
2902 dtp->u.p.current_unit->pad_status
2903 = !(cf & IOPARM_DT_HAS_PAD) ? PAD_UNSPECIFIED :
2904 find_option (&dtp->common, dtp->pad, dtp->pad_len, pad_opt,
2905 "Bad PAD parameter in data transfer statement");
2906
2907 if (dtp->u.p.current_unit->pad_status == PAD_UNSPECIFIED)
2908 dtp->u.p.current_unit->pad_status = dtp->u.p.current_unit->flags.pad;
2909
2910 /* Check to see if we might be reading what we wrote before */
2911
2912 if (dtp->u.p.mode != dtp->u.p.current_unit->mode
2913 && !is_internal_unit (dtp))
2914 {
2915 int pos = fbuf_reset (dtp->u.p.current_unit);
2916 if (pos != 0)
2917 sseek (dtp->u.p.current_unit->s, pos, SEEK_CUR);
2918 sflush(dtp->u.p.current_unit->s);
2919 }
2920
2921 /* Check the POS= specifier: that it is in range and that it is used with a
2922 unit that has been connected for STREAM access. F2003 9.5.1.10. */
2923
2924 if (((cf & IOPARM_DT_HAS_POS) != 0))
2925 {
2926 if (is_stream_io (dtp))
2927 {
2928
2929 if (dtp->pos <= 0)
2930 {
2931 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2932 "POS=specifier must be positive");
2933 return;
2934 }
2935
2936 if (dtp->pos >= dtp->u.p.current_unit->maxrec)
2937 {
2938 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2939 "POS=specifier too large");
2940 return;
2941 }
2942
2943 dtp->rec = dtp->pos;
2944
2945 if (dtp->u.p.mode == READING)
2946 {
2947 /* Reset the endfile flag; if we hit EOF during reading
2948 we'll set the flag and generate an error at that point
2949 rather than worrying about it here. */
2950 dtp->u.p.current_unit->endfile = NO_ENDFILE;
2951 }
2952
2953 if (dtp->pos != dtp->u.p.current_unit->strm_pos)
2954 {
2955 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
2956 if (sseek (dtp->u.p.current_unit->s, dtp->pos - 1, SEEK_SET) < 0)
2957 {
2958 generate_error (&dtp->common, LIBERROR_OS, NULL);
2959 return;
2960 }
2961 dtp->u.p.current_unit->strm_pos = dtp->pos;
2962 }
2963 }
2964 else
2965 {
2966 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2967 "POS=specifier not allowed, "
2968 "Try OPEN with ACCESS='stream'");
2969 return;
2970 }
2971 }
2972
2973
2974 /* Sanity checks on the record number. */
2975 if ((cf & IOPARM_DT_HAS_REC) != 0)
2976 {
2977 if (dtp->rec <= 0)
2978 {
2979 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2980 "Record number must be positive");
2981 return;
2982 }
2983
2984 if (dtp->rec >= dtp->u.p.current_unit->maxrec)
2985 {
2986 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2987 "Record number too large");
2988 return;
2989 }
2990
2991 /* Make sure format buffer is reset. */
2992 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED)
2993 fbuf_reset (dtp->u.p.current_unit);
2994
2995
2996 /* Check whether the record exists to be read. Only
2997 a partial record needs to exist. */
2998
2999 if (dtp->u.p.mode == READING && (dtp->rec - 1)
3000 * dtp->u.p.current_unit->recl >= ssize (dtp->u.p.current_unit->s))
3001 {
3002 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
3003 "Non-existing record number");
3004 return;
3005 }
3006
3007 /* Position the file. */
3008 if (sseek (dtp->u.p.current_unit->s, (gfc_offset) (dtp->rec - 1)
3009 * dtp->u.p.current_unit->recl, SEEK_SET) < 0)
3010 {
3011 generate_error (&dtp->common, LIBERROR_OS, NULL);
3012 return;
3013 }
3014
3015 /* TODO: This is required to maintain compatibility between
3016 4.3 and 4.4 runtime. Remove when ABI changes from 4.3 */
3017
3018 if (is_stream_io (dtp))
3019 dtp->u.p.current_unit->strm_pos = dtp->rec;
3020
3021 /* TODO: Un-comment this code when ABI changes from 4.3.
3022 if (dtp->u.p.current_unit->flags.access == ACCESS_STREAM)
3023 {
3024 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
3025 "Record number not allowed for stream access "
3026 "data transfer");
3027 return;
3028 } */
3029 }
3030
3031 /* Bugware for badly written mixed C-Fortran I/O. */
3032 if (!is_internal_unit (dtp))
3033 flush_if_preconnected(dtp->u.p.current_unit->s);
3034
3035 dtp->u.p.current_unit->mode = dtp->u.p.mode;
3036
3037 /* Set the maximum position reached from the previous I/O operation. This
3038 could be greater than zero from a previous non-advancing write. */
3039 dtp->u.p.max_pos = dtp->u.p.current_unit->saved_pos;
3040
3041 pre_position (dtp);
3042
3043
3044 /* Set up the subroutine that will handle the transfers. */
3045
3046 if (read_flag)
3047 {
3048 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
3049 dtp->u.p.transfer = unformatted_read;
3050 else
3051 {
3052 if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
3053 {
3054 if (dtp->u.p.current_unit->child_dtio == 0)
3055 dtp->u.p.current_unit->last_char = EOF - 1;
3056 dtp->u.p.transfer = list_formatted_read;
3057 }
3058 else
3059 dtp->u.p.transfer = formatted_transfer;
3060 }
3061 }
3062 else
3063 {
3064 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
3065 dtp->u.p.transfer = unformatted_write;
3066 else
3067 {
3068 if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
3069 dtp->u.p.transfer = list_formatted_write;
3070 else
3071 dtp->u.p.transfer = formatted_transfer;
3072 }
3073 }
3074
3075 /* Make sure that we don't do a read after a nonadvancing write. */
3076
3077 if (read_flag)
3078 {
3079 if (dtp->u.p.current_unit->read_bad && !is_stream_io (dtp))
3080 {
3081 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
3082 "Cannot READ after a nonadvancing WRITE");
3083 return;
3084 }
3085 }
3086 else
3087 {
3088 if (dtp->u.p.advance_status == ADVANCE_YES && !dtp->u.p.seen_dollar)
3089 dtp->u.p.current_unit->read_bad = 1;
3090 }
3091
3092 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED)
3093 {
3094 #ifdef HAVE_USELOCALE
3095 dtp->u.p.old_locale = uselocale (c_locale);
3096 #else
3097 __gthread_mutex_lock (&old_locale_lock);
3098 if (!old_locale_ctr++)
3099 {
3100 old_locale = setlocale (LC_NUMERIC, NULL);
3101 setlocale (LC_NUMERIC, "C");
3102 }
3103 __gthread_mutex_unlock (&old_locale_lock);
3104 #endif
3105 /* Start the data transfer if we are doing a formatted transfer. */
3106 if ((cf & (IOPARM_DT_LIST_FORMAT | IOPARM_DT_HAS_NAMELIST_NAME)) == 0
3107 && dtp->u.p.ionml == NULL)
3108 formatted_transfer (dtp, 0, NULL, 0, 0, 1);
3109 }
3110 }
3111
3112
3113 /* Initialize an array_loop_spec given the array descriptor. The function
3114 returns the index of the last element of the array, and also returns
3115 starting record, where the first I/O goes to (necessary in case of
3116 negative strides). */
3117
3118 gfc_offset
3119 init_loop_spec (gfc_array_char *desc, array_loop_spec *ls,
3120 gfc_offset *start_record)
3121 {
3122 int rank = GFC_DESCRIPTOR_RANK(desc);
3123 int i;
3124 gfc_offset index;
3125 int empty;
3126
3127 empty = 0;
3128 index = 1;
3129 *start_record = 0;
3130
3131 for (i=0; i<rank; i++)
3132 {
3133 ls[i].idx = GFC_DESCRIPTOR_LBOUND(desc,i);
3134 ls[i].start = GFC_DESCRIPTOR_LBOUND(desc,i);
3135 ls[i].end = GFC_DESCRIPTOR_UBOUND(desc,i);
3136 ls[i].step = GFC_DESCRIPTOR_STRIDE(desc,i);
3137 empty = empty || (GFC_DESCRIPTOR_UBOUND(desc,i)
3138 < GFC_DESCRIPTOR_LBOUND(desc,i));
3139
3140 if (GFC_DESCRIPTOR_STRIDE(desc,i) > 0)
3141 {
3142 index += (GFC_DESCRIPTOR_EXTENT(desc,i) - 1)
3143 * GFC_DESCRIPTOR_STRIDE(desc,i);
3144 }
3145 else
3146 {
3147 index -= (GFC_DESCRIPTOR_EXTENT(desc,i) - 1)
3148 * GFC_DESCRIPTOR_STRIDE(desc,i);
3149 *start_record -= (GFC_DESCRIPTOR_EXTENT(desc,i) - 1)
3150 * GFC_DESCRIPTOR_STRIDE(desc,i);
3151 }
3152 }
3153
3154 if (empty)
3155 return 0;
3156 else
3157 return index;
3158 }
3159
3160 /* Determine the index to the next record in an internal unit array by
3161 by incrementing through the array_loop_spec. */
3162
3163 gfc_offset
3164 next_array_record (st_parameter_dt *dtp, array_loop_spec *ls, int *finished)
3165 {
3166 int i, carry;
3167 gfc_offset index;
3168
3169 carry = 1;
3170 index = 0;
3171
3172 for (i = 0; i < dtp->u.p.current_unit->rank; i++)
3173 {
3174 if (carry)
3175 {
3176 ls[i].idx++;
3177 if (ls[i].idx > ls[i].end)
3178 {
3179 ls[i].idx = ls[i].start;
3180 carry = 1;
3181 }
3182 else
3183 carry = 0;
3184 }
3185 index = index + (ls[i].idx - ls[i].start) * ls[i].step;
3186 }
3187
3188 *finished = carry;
3189
3190 return index;
3191 }
3192
3193
3194
3195 /* Skip to the end of the current record, taking care of an optional
3196 record marker of size bytes. If the file is not seekable, we
3197 read chunks of size MAX_READ until we get to the right
3198 position. */
3199
3200 static void
3201 skip_record (st_parameter_dt *dtp, ssize_t bytes)
3202 {
3203 ssize_t rlength, readb;
3204 #define MAX_READ 4096
3205 char p[MAX_READ];
3206
3207 dtp->u.p.current_unit->bytes_left_subrecord += bytes;
3208 if (dtp->u.p.current_unit->bytes_left_subrecord == 0)
3209 return;
3210
3211 /* Direct access files do not generate END conditions,
3212 only I/O errors. */
3213 if (sseek (dtp->u.p.current_unit->s,
3214 dtp->u.p.current_unit->bytes_left_subrecord, SEEK_CUR) < 0)
3215 {
3216 /* Seeking failed, fall back to seeking by reading data. */
3217 while (dtp->u.p.current_unit->bytes_left_subrecord > 0)
3218 {
3219 rlength =
3220 (MAX_READ < dtp->u.p.current_unit->bytes_left_subrecord) ?
3221 MAX_READ : dtp->u.p.current_unit->bytes_left_subrecord;
3222
3223 readb = sread (dtp->u.p.current_unit->s, p, rlength);
3224 if (readb < 0)
3225 {
3226 generate_error (&dtp->common, LIBERROR_OS, NULL);
3227 return;
3228 }
3229
3230 dtp->u.p.current_unit->bytes_left_subrecord -= readb;
3231 }
3232 return;
3233 }
3234 dtp->u.p.current_unit->bytes_left_subrecord = 0;
3235 }
3236
3237
3238 /* Advance to the next record reading unformatted files, taking
3239 care of subrecords. If complete_record is nonzero, we loop
3240 until all subrecords are cleared. */
3241
3242 static void
3243 next_record_r_unf (st_parameter_dt *dtp, int complete_record)
3244 {
3245 size_t bytes;
3246
3247 bytes = compile_options.record_marker == 0 ?
3248 sizeof (GFC_INTEGER_4) : compile_options.record_marker;
3249
3250 while(1)
3251 {
3252
3253 /* Skip over tail */
3254
3255 skip_record (dtp, bytes);
3256
3257 if ( ! (complete_record && dtp->u.p.current_unit->continued))
3258 return;
3259
3260 us_read (dtp, 1);
3261 }
3262 }
3263
3264
3265 static gfc_offset
3266 min_off (gfc_offset a, gfc_offset b)
3267 {
3268 return (a < b ? a : b);
3269 }
3270
3271
3272 /* Space to the next record for read mode. */
3273
3274 static void
3275 next_record_r (st_parameter_dt *dtp, int done)
3276 {
3277 gfc_offset record;
3278 int bytes_left;
3279 char p;
3280 int cc;
3281
3282 switch (current_mode (dtp))
3283 {
3284 /* No records in unformatted STREAM I/O. */
3285 case UNFORMATTED_STREAM:
3286 return;
3287
3288 case UNFORMATTED_SEQUENTIAL:
3289 next_record_r_unf (dtp, 1);
3290 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
3291 break;
3292
3293 case FORMATTED_DIRECT:
3294 case UNFORMATTED_DIRECT:
3295 skip_record (dtp, dtp->u.p.current_unit->bytes_left);
3296 break;
3297
3298 case FORMATTED_STREAM:
3299 case FORMATTED_SEQUENTIAL:
3300 /* read_sf has already terminated input because of an '\n', or
3301 we have hit EOF. */
3302 if (dtp->u.p.sf_seen_eor)
3303 {
3304 dtp->u.p.sf_seen_eor = 0;
3305 break;
3306 }
3307
3308 if (is_internal_unit (dtp))
3309 {
3310 if (is_array_io (dtp))
3311 {
3312 int finished;
3313
3314 record = next_array_record (dtp, dtp->u.p.current_unit->ls,
3315 &finished);
3316 if (!done && finished)
3317 hit_eof (dtp);
3318
3319 /* Now seek to this record. */
3320 record = record * dtp->u.p.current_unit->recl;
3321 if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
3322 {
3323 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3324 break;
3325 }
3326 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
3327 }
3328 else
3329 {
3330 bytes_left = (int) dtp->u.p.current_unit->bytes_left;
3331 bytes_left = min_off (bytes_left,
3332 ssize (dtp->u.p.current_unit->s)
3333 - stell (dtp->u.p.current_unit->s));
3334 if (sseek (dtp->u.p.current_unit->s,
3335 bytes_left, SEEK_CUR) < 0)
3336 {
3337 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3338 break;
3339 }
3340 dtp->u.p.current_unit->bytes_left
3341 = dtp->u.p.current_unit->recl;
3342 }
3343 break;
3344 }
3345 else
3346 {
3347 do
3348 {
3349 errno = 0;
3350 cc = fbuf_getc (dtp->u.p.current_unit);
3351 if (cc == EOF)
3352 {
3353 if (errno != 0)
3354 generate_error (&dtp->common, LIBERROR_OS, NULL);
3355 else
3356 {
3357 if (is_stream_io (dtp)
3358 || dtp->u.p.current_unit->pad_status == PAD_NO
3359 || dtp->u.p.current_unit->bytes_left
3360 == dtp->u.p.current_unit->recl)
3361 hit_eof (dtp);
3362 }
3363 break;
3364 }
3365
3366 if (is_stream_io (dtp))
3367 dtp->u.p.current_unit->strm_pos++;
3368
3369 p = (char) cc;
3370 }
3371 while (p != '\n');
3372 }
3373 break;
3374 }
3375 }
3376
3377
3378 /* Small utility function to write a record marker, taking care of
3379 byte swapping and of choosing the correct size. */
3380
3381 static int
3382 write_us_marker (st_parameter_dt *dtp, const gfc_offset buf)
3383 {
3384 size_t len;
3385 GFC_INTEGER_4 buf4;
3386 GFC_INTEGER_8 buf8;
3387
3388 if (compile_options.record_marker == 0)
3389 len = sizeof (GFC_INTEGER_4);
3390 else
3391 len = compile_options.record_marker;
3392
3393 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
3394 if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE))
3395 {
3396 switch (len)
3397 {
3398 case sizeof (GFC_INTEGER_4):
3399 buf4 = buf;
3400 return swrite (dtp->u.p.current_unit->s, &buf4, len);
3401 break;
3402
3403 case sizeof (GFC_INTEGER_8):
3404 buf8 = buf;
3405 return swrite (dtp->u.p.current_unit->s, &buf8, len);
3406 break;
3407
3408 default:
3409 runtime_error ("Illegal value for record marker");
3410 break;
3411 }
3412 }
3413 else
3414 {
3415 uint32_t u32;
3416 uint64_t u64;
3417 switch (len)
3418 {
3419 case sizeof (GFC_INTEGER_4):
3420 buf4 = buf;
3421 memcpy (&u32, &buf4, sizeof (u32));
3422 u32 = __builtin_bswap32 (u32);
3423 return swrite (dtp->u.p.current_unit->s, &u32, len);
3424 break;
3425
3426 case sizeof (GFC_INTEGER_8):
3427 buf8 = buf;
3428 memcpy (&u64, &buf8, sizeof (u64));
3429 u64 = __builtin_bswap64 (u64);
3430 return swrite (dtp->u.p.current_unit->s, &u64, len);
3431 break;
3432
3433 default:
3434 runtime_error ("Illegal value for record marker");
3435 break;
3436 }
3437 }
3438
3439 }
3440
3441 /* Position to the next (sub)record in write mode for
3442 unformatted sequential files. */
3443
3444 static void
3445 next_record_w_unf (st_parameter_dt *dtp, int next_subrecord)
3446 {
3447 gfc_offset m, m_write, record_marker;
3448
3449 /* Bytes written. */
3450 m = dtp->u.p.current_unit->recl_subrecord
3451 - dtp->u.p.current_unit->bytes_left_subrecord;
3452
3453 if (compile_options.record_marker == 0)
3454 record_marker = sizeof (GFC_INTEGER_4);
3455 else
3456 record_marker = compile_options.record_marker;
3457
3458 /* Seek to the head and overwrite the bogus length with the real
3459 length. */
3460
3461 if (unlikely (sseek (dtp->u.p.current_unit->s, - m - record_marker,
3462 SEEK_CUR) < 0))
3463 goto io_error;
3464
3465 if (next_subrecord)
3466 m_write = -m;
3467 else
3468 m_write = m;
3469
3470 if (unlikely (write_us_marker (dtp, m_write) < 0))
3471 goto io_error;
3472
3473 /* Seek past the end of the current record. */
3474
3475 if (unlikely (sseek (dtp->u.p.current_unit->s, m, SEEK_CUR) < 0))
3476 goto io_error;
3477
3478 /* Write the length tail. If we finish a record containing
3479 subrecords, we write out the negative length. */
3480
3481 if (dtp->u.p.current_unit->continued)
3482 m_write = -m;
3483 else
3484 m_write = m;
3485
3486 if (unlikely (write_us_marker (dtp, m_write) < 0))
3487 goto io_error;
3488
3489 return;
3490
3491 io_error:
3492 generate_error (&dtp->common, LIBERROR_OS, NULL);
3493 return;
3494
3495 }
3496
3497
3498 /* Utility function like memset() but operating on streams. Return
3499 value is same as for POSIX write(). */
3500
3501 static ssize_t
3502 sset (stream * s, int c, ssize_t nbyte)
3503 {
3504 #define WRITE_CHUNK 256
3505 char p[WRITE_CHUNK];
3506 ssize_t bytes_left, trans;
3507
3508 if (nbyte < WRITE_CHUNK)
3509 memset (p, c, nbyte);
3510 else
3511 memset (p, c, WRITE_CHUNK);
3512
3513 bytes_left = nbyte;
3514 while (bytes_left > 0)
3515 {
3516 trans = (bytes_left < WRITE_CHUNK) ? bytes_left : WRITE_CHUNK;
3517 trans = swrite (s, p, trans);
3518 if (trans <= 0)
3519 return trans;
3520 bytes_left -= trans;
3521 }
3522
3523 return nbyte - bytes_left;
3524 }
3525
3526
3527 /* Position to the next record in write mode. */
3528
3529 static void
3530 next_record_w (st_parameter_dt *dtp, int done)
3531 {
3532 gfc_offset m, record, max_pos;
3533 int length;
3534
3535 /* Zero counters for X- and T-editing. */
3536 max_pos = dtp->u.p.max_pos;
3537 dtp->u.p.max_pos = dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
3538
3539 switch (current_mode (dtp))
3540 {
3541 /* No records in unformatted STREAM I/O. */
3542 case UNFORMATTED_STREAM:
3543 return;
3544
3545 case FORMATTED_DIRECT:
3546 if (dtp->u.p.current_unit->bytes_left == 0)
3547 break;
3548
3549 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
3550 fbuf_flush (dtp->u.p.current_unit, WRITING);
3551 if (sset (dtp->u.p.current_unit->s, ' ',
3552 dtp->u.p.current_unit->bytes_left)
3553 != dtp->u.p.current_unit->bytes_left)
3554 goto io_error;
3555
3556 break;
3557
3558 case UNFORMATTED_DIRECT:
3559 if (dtp->u.p.current_unit->bytes_left > 0)
3560 {
3561 length = (int) dtp->u.p.current_unit->bytes_left;
3562 if (sset (dtp->u.p.current_unit->s, 0, length) != length)
3563 goto io_error;
3564 }
3565 break;
3566
3567 case UNFORMATTED_SEQUENTIAL:
3568 next_record_w_unf (dtp, 0);
3569 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
3570 break;
3571
3572 case FORMATTED_STREAM:
3573 case FORMATTED_SEQUENTIAL:
3574
3575 if (is_internal_unit (dtp))
3576 {
3577 char *p;
3578 if (is_array_io (dtp))
3579 {
3580 int finished;
3581
3582 length = (int) dtp->u.p.current_unit->bytes_left;
3583
3584 /* If the farthest position reached is greater than current
3585 position, adjust the position and set length to pad out
3586 whats left. Otherwise just pad whats left.
3587 (for character array unit) */
3588 m = dtp->u.p.current_unit->recl
3589 - dtp->u.p.current_unit->bytes_left;
3590 if (max_pos > m)
3591 {
3592 length = (int) (max_pos - m);
3593 if (sseek (dtp->u.p.current_unit->s,
3594 length, SEEK_CUR) < 0)
3595 {
3596 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3597 return;
3598 }
3599 length = (int) (dtp->u.p.current_unit->recl - max_pos);
3600 }
3601
3602 p = write_block (dtp, length);
3603 if (p == NULL)
3604 return;
3605
3606 if (unlikely (is_char4_unit (dtp)))
3607 {
3608 gfc_char4_t *p4 = (gfc_char4_t *) p;
3609 memset4 (p4, ' ', length);
3610 }
3611 else
3612 memset (p, ' ', length);
3613
3614 /* Now that the current record has been padded out,
3615 determine where the next record in the array is. */
3616 record = next_array_record (dtp, dtp->u.p.current_unit->ls,
3617 &finished);
3618 if (finished)
3619 dtp->u.p.current_unit->endfile = AT_ENDFILE;
3620
3621 /* Now seek to this record */
3622 record = record * dtp->u.p.current_unit->recl;
3623
3624 if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
3625 {
3626 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3627 return;
3628 }
3629
3630 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
3631 }
3632 else
3633 {
3634 length = 1;
3635
3636 /* If this is the last call to next_record move to the farthest
3637 position reached and set length to pad out the remainder
3638 of the record. (for character scaler unit) */
3639 if (done)
3640 {
3641 m = dtp->u.p.current_unit->recl
3642 - dtp->u.p.current_unit->bytes_left;
3643 if (max_pos > m)
3644 {
3645 length = (int) (max_pos - m);
3646 if (sseek (dtp->u.p.current_unit->s,
3647 length, SEEK_CUR) < 0)
3648 {
3649 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
3650 return;
3651 }
3652 length = (int) (dtp->u.p.current_unit->recl - max_pos);
3653 }
3654 else
3655 length = (int) dtp->u.p.current_unit->bytes_left;
3656 }
3657 if (length > 0)
3658 {
3659 p = write_block (dtp, length);
3660 if (p == NULL)
3661 return;
3662
3663 if (unlikely (is_char4_unit (dtp)))
3664 {
3665 gfc_char4_t *p4 = (gfc_char4_t *) p;
3666 memset4 (p4, (gfc_char4_t) ' ', length);
3667 }
3668 else
3669 memset (p, ' ', length);
3670 }
3671 }
3672 }
3673 else
3674 {
3675 #ifdef HAVE_CRLF
3676 const int len = 2;
3677 #else
3678 const int len = 1;
3679 #endif
3680 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
3681 char * p = fbuf_alloc (dtp->u.p.current_unit, len);
3682 if (!p)
3683 goto io_error;
3684 #ifdef HAVE_CRLF
3685 *(p++) = '\r';
3686 #endif
3687 *p = '\n';
3688 if (is_stream_io (dtp))
3689 {
3690 dtp->u.p.current_unit->strm_pos += len;
3691 if (dtp->u.p.current_unit->strm_pos
3692 < ssize (dtp->u.p.current_unit->s))
3693 unit_truncate (dtp->u.p.current_unit,
3694 dtp->u.p.current_unit->strm_pos - 1,
3695 &dtp->common);
3696 }
3697 }
3698
3699 break;
3700
3701 io_error:
3702 generate_error (&dtp->common, LIBERROR_OS, NULL);
3703 break;
3704 }
3705 }
3706
3707 /* Position to the next record, which means moving to the end of the
3708 current record. This can happen under several different
3709 conditions. If the done flag is not set, we get ready to process
3710 the next record. */
3711
3712 void
3713 next_record (st_parameter_dt *dtp, int done)
3714 {
3715 gfc_offset fp; /* File position. */
3716
3717 dtp->u.p.current_unit->read_bad = 0;
3718
3719 if (dtp->u.p.mode == READING)
3720 next_record_r (dtp, done);
3721 else
3722 next_record_w (dtp, done);
3723
3724 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
3725
3726 if (!is_stream_io (dtp))
3727 {
3728 /* Since we have changed the position, set it to unspecified so
3729 that INQUIRE(POSITION=) knows it needs to look into it. */
3730 if (done)
3731 dtp->u.p.current_unit->flags.position = POSITION_UNSPECIFIED;
3732
3733 dtp->u.p.current_unit->current_record = 0;
3734 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
3735 {
3736 fp = stell (dtp->u.p.current_unit->s);
3737 /* Calculate next record, rounding up partial records. */
3738 dtp->u.p.current_unit->last_record =
3739 (fp + dtp->u.p.current_unit->recl) /
3740 dtp->u.p.current_unit->recl - 1;
3741 }
3742 else
3743 dtp->u.p.current_unit->last_record++;
3744 }
3745
3746 if (!done)
3747 pre_position (dtp);
3748
3749 smarkeor (dtp->u.p.current_unit->s);
3750 }
3751
3752
3753 /* Finalize the current data transfer. For a nonadvancing transfer,
3754 this means advancing to the next record. For internal units close the
3755 stream associated with the unit. */
3756
3757 static void
3758 finalize_transfer (st_parameter_dt *dtp)
3759 {
3760 GFC_INTEGER_4 cf = dtp->common.flags;
3761
3762 if ((dtp->u.p.ionml != NULL)
3763 && (cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0)
3764 {
3765 if ((cf & IOPARM_DT_NAMELIST_READ_MODE) != 0)
3766 namelist_read (dtp);
3767 else
3768 namelist_write (dtp);
3769 }
3770
3771 if (dtp->u.p.current_unit && (dtp->u.p.current_unit->child_dtio > 0))
3772 return;
3773
3774 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
3775 *dtp->size = dtp->u.p.size_used;
3776
3777 if (dtp->u.p.eor_condition)
3778 {
3779 generate_error (&dtp->common, LIBERROR_EOR, NULL);
3780 goto done;
3781 }
3782
3783 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
3784 {
3785 if (dtp->u.p.current_unit && current_mode (dtp) == UNFORMATTED_SEQUENTIAL)
3786 dtp->u.p.current_unit->current_record = 0;
3787 goto done;
3788 }
3789
3790 dtp->u.p.transfer = NULL;
3791 if (dtp->u.p.current_unit == NULL)
3792 goto done;
3793
3794 if ((cf & IOPARM_DT_LIST_FORMAT) != 0 && dtp->u.p.mode == READING)
3795 {
3796 finish_list_read (dtp);
3797 goto done;
3798 }
3799
3800 if (dtp->u.p.mode == WRITING)
3801 dtp->u.p.current_unit->previous_nonadvancing_write
3802 = dtp->u.p.advance_status == ADVANCE_NO;
3803
3804 if (is_stream_io (dtp))
3805 {
3806 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
3807 && dtp->u.p.advance_status != ADVANCE_NO)
3808 next_record (dtp, 1);
3809
3810 goto done;
3811 }
3812
3813 dtp->u.p.current_unit->current_record = 0;
3814
3815 if (!is_internal_unit (dtp) && dtp->u.p.seen_dollar)
3816 {
3817 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
3818 dtp->u.p.seen_dollar = 0;
3819 goto done;
3820 }
3821
3822 /* For non-advancing I/O, save the current maximum position for use in the
3823 next I/O operation if needed. */
3824 if (dtp->u.p.advance_status == ADVANCE_NO)
3825 {
3826 if (dtp->u.p.skips > 0)
3827 {
3828 int tmp;
3829 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
3830 tmp = (int)(dtp->u.p.current_unit->recl
3831 - dtp->u.p.current_unit->bytes_left);
3832 dtp->u.p.max_pos =
3833 dtp->u.p.max_pos > tmp ? dtp->u.p.max_pos : tmp;
3834 dtp->u.p.skips = 0;
3835 }
3836 int bytes_written = (int) (dtp->u.p.current_unit->recl
3837 - dtp->u.p.current_unit->bytes_left);
3838 dtp->u.p.current_unit->saved_pos =
3839 dtp->u.p.max_pos > 0 ? dtp->u.p.max_pos - bytes_written : 0;
3840 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
3841 goto done;
3842 }
3843 else if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
3844 && dtp->u.p.mode == WRITING && !is_internal_unit (dtp))
3845 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
3846
3847 dtp->u.p.current_unit->saved_pos = 0;
3848
3849 next_record (dtp, 1);
3850
3851 done:
3852 #ifdef HAVE_USELOCALE
3853 if (dtp->u.p.old_locale != (locale_t) 0)
3854 {
3855 uselocale (dtp->u.p.old_locale);
3856 dtp->u.p.old_locale = (locale_t) 0;
3857 }
3858 #else
3859 __gthread_mutex_lock (&old_locale_lock);
3860 if (!--old_locale_ctr)
3861 {
3862 setlocale (LC_NUMERIC, old_locale);
3863 old_locale = NULL;
3864 }
3865 __gthread_mutex_unlock (&old_locale_lock);
3866 #endif
3867 }
3868
3869 /* Transfer function for IOLENGTH. It doesn't actually do any
3870 data transfer, it just updates the length counter. */
3871
3872 static void
3873 iolength_transfer (st_parameter_dt *dtp, bt type __attribute__((unused)),
3874 void *dest __attribute__ ((unused)),
3875 int kind __attribute__((unused)),
3876 size_t size, size_t nelems)
3877 {
3878 if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
3879 *dtp->iolength += (GFC_IO_INT) (size * nelems);
3880 }
3881
3882
3883 /* Initialize the IOLENGTH data transfer. This function is in essence
3884 a very much simplified version of data_transfer_init(), because it
3885 doesn't have to deal with units at all. */
3886
3887 static void
3888 iolength_transfer_init (st_parameter_dt *dtp)
3889 {
3890 if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
3891 *dtp->iolength = 0;
3892
3893 memset (&dtp->u.p, 0, sizeof (dtp->u.p));
3894
3895 /* Set up the subroutine that will handle the transfers. */
3896
3897 dtp->u.p.transfer = iolength_transfer;
3898 }
3899
3900
3901 /* Library entry point for the IOLENGTH form of the INQUIRE
3902 statement. The IOLENGTH form requires no I/O to be performed, but
3903 it must still be a runtime library call so that we can determine
3904 the iolength for dynamic arrays and such. */
3905
3906 extern void st_iolength (st_parameter_dt *);
3907 export_proto(st_iolength);
3908
3909 void
3910 st_iolength (st_parameter_dt *dtp)
3911 {
3912 library_start (&dtp->common);
3913 iolength_transfer_init (dtp);
3914 }
3915
3916 extern void st_iolength_done (st_parameter_dt *);
3917 export_proto(st_iolength_done);
3918
3919 void
3920 st_iolength_done (st_parameter_dt *dtp __attribute__((unused)))
3921 {
3922 free_ionml (dtp);
3923 library_end ();
3924 }
3925
3926
3927 /* The READ statement. */
3928
3929 extern void st_read (st_parameter_dt *);
3930 export_proto(st_read);
3931
3932 void
3933 st_read (st_parameter_dt *dtp)
3934 {
3935 library_start (&dtp->common);
3936
3937 data_transfer_init (dtp, 1);
3938 }
3939
3940 extern void st_read_done (st_parameter_dt *);
3941 export_proto(st_read_done);
3942
3943 void
3944 st_read_done (st_parameter_dt *dtp)
3945 {
3946 finalize_transfer (dtp);
3947
3948 free_ionml (dtp);
3949
3950 /* If this is a parent READ statement we do not need to retain the
3951 internal unit structure for child use. Free it and stash the unit
3952 number for reuse. */
3953 if (dtp->u.p.current_unit != NULL
3954 && dtp->u.p.current_unit->child_dtio == 0)
3955 {
3956 if (is_internal_unit (dtp) &&
3957 (dtp->common.flags & IOPARM_DT_HAS_UDTIO) == 0)
3958 {
3959 free (dtp->u.p.current_unit->filename);
3960 dtp->u.p.current_unit->filename = NULL;
3961 free_format_hash_table (dtp->u.p.current_unit);
3962 free (dtp->u.p.current_unit->s);
3963 dtp->u.p.current_unit->s = NULL;
3964 if (dtp->u.p.current_unit->ls)
3965 free (dtp->u.p.current_unit->ls);
3966 dtp->u.p.current_unit->ls = NULL;
3967 stash_internal_unit (dtp);
3968 }
3969 if (is_internal_unit (dtp) || dtp->u.p.format_not_saved)
3970 {
3971 free_format_data (dtp->u.p.fmt);
3972 free_format (dtp);
3973 }
3974 unlock_unit (dtp->u.p.current_unit);
3975 }
3976
3977 library_end ();
3978 }
3979
3980 extern void st_write (st_parameter_dt *);
3981 export_proto(st_write);
3982
3983 void
3984 st_write (st_parameter_dt *dtp)
3985 {
3986 library_start (&dtp->common);
3987 data_transfer_init (dtp, 0);
3988 }
3989
3990 extern void st_write_done (st_parameter_dt *);
3991 export_proto(st_write_done);
3992
3993 void
3994 st_write_done (st_parameter_dt *dtp)
3995 {
3996 finalize_transfer (dtp);
3997
3998 if (dtp->u.p.current_unit != NULL
3999 && dtp->u.p.current_unit->child_dtio == 0)
4000 {
4001 /* Deal with endfile conditions associated with sequential files. */
4002 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
4003 switch (dtp->u.p.current_unit->endfile)
4004 {
4005 case AT_ENDFILE: /* Remain at the endfile record. */
4006 break;
4007
4008 case AFTER_ENDFILE:
4009 dtp->u.p.current_unit->endfile = AT_ENDFILE; /* Just at it now. */
4010 break;
4011
4012 case NO_ENDFILE:
4013 /* Get rid of whatever is after this record. */
4014 if (!is_internal_unit (dtp))
4015 unit_truncate (dtp->u.p.current_unit,
4016 stell (dtp->u.p.current_unit->s),
4017 &dtp->common);
4018 dtp->u.p.current_unit->endfile = AT_ENDFILE;
4019 break;
4020 }
4021
4022 free_ionml (dtp);
4023
4024 /* If this is a parent WRITE statement we do not need to retain the
4025 internal unit structure for child use. Free it and stash the
4026 unit number for reuse. */
4027 if (is_internal_unit (dtp) &&
4028 (dtp->common.flags & IOPARM_DT_HAS_UDTIO) == 0)
4029 {
4030 free (dtp->u.p.current_unit->filename);
4031 dtp->u.p.current_unit->filename = NULL;
4032 free_format_hash_table (dtp->u.p.current_unit);
4033 free (dtp->u.p.current_unit->s);
4034 dtp->u.p.current_unit->s = NULL;
4035 if (dtp->u.p.current_unit->ls)
4036 free (dtp->u.p.current_unit->ls);
4037 dtp->u.p.current_unit->ls = NULL;
4038 stash_internal_unit (dtp);
4039 }
4040 if (is_internal_unit (dtp) || dtp->u.p.format_not_saved)
4041 {
4042 free_format_data (dtp->u.p.fmt);
4043 free_format (dtp);
4044 }
4045 unlock_unit (dtp->u.p.current_unit);
4046 }
4047 library_end ();
4048 }
4049
4050
4051 /* F2003: This is a stub for the runtime portion of the WAIT statement. */
4052 void
4053 st_wait (st_parameter_wait *wtp __attribute__((unused)))
4054 {
4055 }
4056
4057
4058 /* Receives the scalar information for namelist objects and stores it
4059 in a linked list of namelist_info types. */
4060
4061 static void
4062 set_nml_var (st_parameter_dt *dtp, void * var_addr, char * var_name,
4063 GFC_INTEGER_4 len, gfc_charlen_type string_length,
4064 GFC_INTEGER_4 dtype, void *dtio_sub, void *vtable)
4065 {
4066 namelist_info *t1 = NULL;
4067 namelist_info *nml;
4068 size_t var_name_len = strlen (var_name);
4069
4070 nml = (namelist_info*) xmalloc (sizeof (namelist_info));
4071
4072 nml->mem_pos = var_addr;
4073 nml->dtio_sub = dtio_sub;
4074 nml->vtable = vtable;
4075
4076 nml->var_name = (char*) xmalloc (var_name_len + 1);
4077 memcpy (nml->var_name, var_name, var_name_len);
4078 nml->var_name[var_name_len] = '\0';
4079
4080 nml->len = (int) len;
4081 nml->string_length = (index_type) string_length;
4082
4083 nml->var_rank = (int) (dtype & GFC_DTYPE_RANK_MASK);
4084 nml->size = (index_type) (dtype >> GFC_DTYPE_SIZE_SHIFT);
4085 nml->type = (bt) ((dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT);
4086
4087 if (nml->var_rank > 0)
4088 {
4089 nml->dim = (descriptor_dimension*)
4090 xmallocarray (nml->var_rank, sizeof (descriptor_dimension));
4091 nml->ls = (array_loop_spec*)
4092 xmallocarray (nml->var_rank, sizeof (array_loop_spec));
4093 }
4094 else
4095 {
4096 nml->dim = NULL;
4097 nml->ls = NULL;
4098 }
4099
4100 nml->next = NULL;
4101
4102 if ((dtp->common.flags & IOPARM_DT_IONML_SET) == 0)
4103 {
4104 dtp->common.flags |= IOPARM_DT_IONML_SET;
4105 dtp->u.p.ionml = nml;
4106 }
4107 else
4108 {
4109 for (t1 = dtp->u.p.ionml; t1->next; t1 = t1->next);
4110 t1->next = nml;
4111 }
4112 }
4113
4114 extern void st_set_nml_var (st_parameter_dt *dtp, void *, char *,
4115 GFC_INTEGER_4, gfc_charlen_type, GFC_INTEGER_4);
4116 export_proto(st_set_nml_var);
4117
4118 void
4119 st_set_nml_var (st_parameter_dt *dtp, void * var_addr, char * var_name,
4120 GFC_INTEGER_4 len, gfc_charlen_type string_length,
4121 GFC_INTEGER_4 dtype)
4122 {
4123 set_nml_var (dtp, var_addr, var_name, len, string_length,
4124 dtype, NULL, NULL);
4125 }
4126
4127
4128 /* Essentially the same as previous but carrying the dtio procedure
4129 and the vtable as additional arguments. */
4130 extern void st_set_nml_dtio_var (st_parameter_dt *dtp, void *, char *,
4131 GFC_INTEGER_4, gfc_charlen_type, GFC_INTEGER_4,
4132 void *, void *);
4133 export_proto(st_set_nml_dtio_var);
4134
4135
4136 void
4137 st_set_nml_dtio_var (st_parameter_dt *dtp, void * var_addr, char * var_name,
4138 GFC_INTEGER_4 len, gfc_charlen_type string_length,
4139 GFC_INTEGER_4 dtype, void *dtio_sub, void *vtable)
4140 {
4141 set_nml_var (dtp, var_addr, var_name, len, string_length,
4142 dtype, dtio_sub, vtable);
4143 }
4144
4145 /* Store the dimensional information for the namelist object. */
4146 extern void st_set_nml_var_dim (st_parameter_dt *, GFC_INTEGER_4,
4147 index_type, index_type,
4148 index_type);
4149 export_proto(st_set_nml_var_dim);
4150
4151 void
4152 st_set_nml_var_dim (st_parameter_dt *dtp, GFC_INTEGER_4 n_dim,
4153 index_type stride, index_type lbound,
4154 index_type ubound)
4155 {
4156 namelist_info * nml;
4157 int n;
4158
4159 n = (int)n_dim;
4160
4161 for (nml = dtp->u.p.ionml; nml->next; nml = nml->next);
4162
4163 GFC_DIMENSION_SET(nml->dim[n],lbound,ubound,stride);
4164 }
4165
4166
4167 /* Once upon a time, a poor innocent Fortran program was reading a
4168 file, when suddenly it hit the end-of-file (EOF). Unfortunately
4169 the OS doesn't tell whether we're at the EOF or whether we already
4170 went past it. Luckily our hero, libgfortran, keeps track of this.
4171 Call this function when you detect an EOF condition. See Section
4172 9.10.2 in F2003. */
4173
4174 void
4175 hit_eof (st_parameter_dt * dtp)
4176 {
4177 dtp->u.p.current_unit->flags.position = POSITION_APPEND;
4178
4179 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
4180 switch (dtp->u.p.current_unit->endfile)
4181 {
4182 case NO_ENDFILE:
4183 case AT_ENDFILE:
4184 generate_error (&dtp->common, LIBERROR_END, NULL);
4185 if (!is_internal_unit (dtp) && !dtp->u.p.namelist_mode)
4186 {
4187 dtp->u.p.current_unit->endfile = AFTER_ENDFILE;
4188 dtp->u.p.current_unit->current_record = 0;
4189 }
4190 else
4191 dtp->u.p.current_unit->endfile = AT_ENDFILE;
4192 break;
4193
4194 case AFTER_ENDFILE:
4195 generate_error (&dtp->common, LIBERROR_ENDFILE, NULL);
4196 dtp->u.p.current_unit->current_record = 0;
4197 break;
4198 }
4199 else
4200 {
4201 /* Non-sequential files don't have an ENDFILE record, so we
4202 can't be at AFTER_ENDFILE. */
4203 dtp->u.p.current_unit->endfile = AT_ENDFILE;
4204 generate_error (&dtp->common, LIBERROR_END, NULL);
4205 dtp->u.p.current_unit->current_record = 0;
4206 }
4207 }