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