PR libfortran/25561 libfortran/37754
[gcc.git] / libgfortran / io / transfer.c
1 /* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008
2 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
4 Namelist transfer functions contributed by Paul Thomas
5 F2003 I/O support contributed by Jerry DeLisle
6
7 This file is part of the GNU Fortran 95 runtime library (libgfortran).
8
9 Libgfortran is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13
14 In addition to the permissions in the GNU General Public License, the
15 Free Software Foundation gives you unlimited permission to link the
16 compiled version of this file into combinations with other programs,
17 and to distribute those combinations without any restriction coming
18 from the use of this file. (The General Public License restrictions
19 do apply in other respects; for example, they cover modification of
20 the file, and distribution when not linked into a combine
21 executable.)
22
23 Libgfortran is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
27
28 You should have received a copy of the GNU General Public License
29 along with Libgfortran; see the file COPYING. If not, write to
30 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
31 Boston, MA 02110-1301, USA. */
32
33
34 /* transfer.c -- Top level handling of data transfer statements. */
35
36 #include "io.h"
37 #include <string.h>
38 #include <assert.h>
39 #include <stdlib.h>
40 #include <errno.h>
41
42
43 /* Calling conventions: Data transfer statements are unlike other
44 library calls in that they extend over several calls.
45
46 The first call is always a call to st_read() or st_write(). These
47 subroutines return no status unless a namelist read or write is
48 being done, in which case there is the usual status. No further
49 calls are necessary in this case.
50
51 For other sorts of data transfer, there are zero or more data
52 transfer statement that depend on the format of the data transfer
53 statement.
54
55 transfer_integer
56 transfer_logical
57 transfer_character
58 transfer_character_wide
59 transfer_real
60 transfer_complex
61
62 These subroutines do not return status.
63
64 The last call is a call to st_[read|write]_done(). While
65 something can easily go wrong with the initial st_read() or
66 st_write(), an error inhibits any data from actually being
67 transferred. */
68
69 extern void transfer_integer (st_parameter_dt *, void *, int);
70 export_proto(transfer_integer);
71
72 extern void transfer_real (st_parameter_dt *, void *, int);
73 export_proto(transfer_real);
74
75 extern void transfer_logical (st_parameter_dt *, void *, int);
76 export_proto(transfer_logical);
77
78 extern void transfer_character (st_parameter_dt *, void *, int);
79 export_proto(transfer_character);
80
81 extern void transfer_character_wide (st_parameter_dt *, void *, int, int);
82 export_proto(transfer_character_wide);
83
84 extern void transfer_complex (st_parameter_dt *, void *, int);
85 export_proto(transfer_complex);
86
87 extern void transfer_array (st_parameter_dt *, gfc_array_char *, int,
88 gfc_charlen_type);
89 export_proto(transfer_array);
90
91 static void us_read (st_parameter_dt *, int);
92 static void us_write (st_parameter_dt *, int);
93 static void next_record_r_unf (st_parameter_dt *, int);
94 static void next_record_w_unf (st_parameter_dt *, int);
95
96 static const st_option advance_opt[] = {
97 {"yes", ADVANCE_YES},
98 {"no", ADVANCE_NO},
99 {NULL, 0}
100 };
101
102
103 static const st_option decimal_opt[] = {
104 {"point", DECIMAL_POINT},
105 {"comma", DECIMAL_COMMA},
106 {NULL, 0}
107 };
108
109
110 static const st_option sign_opt[] = {
111 {"plus", SIGN_SP},
112 {"suppress", SIGN_SS},
113 {"processor_defined", SIGN_S},
114 {NULL, 0}
115 };
116
117 static const st_option blank_opt[] = {
118 {"null", BLANK_NULL},
119 {"zero", BLANK_ZERO},
120 {NULL, 0}
121 };
122
123 static const st_option delim_opt[] = {
124 {"apostrophe", DELIM_APOSTROPHE},
125 {"quote", DELIM_QUOTE},
126 {"none", DELIM_NONE},
127 {NULL, 0}
128 };
129
130 static const st_option pad_opt[] = {
131 {"yes", PAD_YES},
132 {"no", PAD_NO},
133 {NULL, 0}
134 };
135
136 typedef enum
137 { FORMATTED_SEQUENTIAL, UNFORMATTED_SEQUENTIAL,
138 FORMATTED_DIRECT, UNFORMATTED_DIRECT, FORMATTED_STREAM, UNFORMATTED_STREAM
139 }
140 file_mode;
141
142
143 static file_mode
144 current_mode (st_parameter_dt *dtp)
145 {
146 file_mode m;
147
148 m = FORM_UNSPECIFIED;
149
150 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
151 {
152 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
153 FORMATTED_DIRECT : UNFORMATTED_DIRECT;
154 }
155 else if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
156 {
157 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
158 FORMATTED_SEQUENTIAL : UNFORMATTED_SEQUENTIAL;
159 }
160 else if (dtp->u.p.current_unit->flags.access == ACCESS_STREAM)
161 {
162 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
163 FORMATTED_STREAM : UNFORMATTED_STREAM;
164 }
165
166 return m;
167 }
168
169
170 /* Mid level data transfer statements. These subroutines do reading
171 and writing in the style of salloc_r()/salloc_w() within the
172 current record. */
173
174 /* When reading sequential formatted records we have a problem. We
175 don't know how long the line is until we read the trailing newline,
176 and we don't want to read too much. If we read too much, we might
177 have to do a physical seek backwards depending on how much data is
178 present, and devices like terminals aren't seekable and would cause
179 an I/O error.
180
181 Given this, the solution is to read a byte at a time, stopping if
182 we hit the newline. For small allocations, we use a static buffer.
183 For larger allocations, we are forced to allocate memory on the
184 heap. Hopefully this won't happen very often. */
185
186 char *
187 read_sf (st_parameter_dt *dtp, int * length, int no_error)
188 {
189 static char *empty_string[0];
190 char *base, *p, q;
191 int n, lorig, memread, seen_comma;
192
193 /* If we hit EOF previously with the no_error flag set (i.e. X, T,
194 TR edit descriptors), and we now try to read again, this time
195 without setting no_error. */
196 if (!no_error && dtp->u.p.at_eof)
197 {
198 *length = 0;
199 hit_eof (dtp);
200 return NULL;
201 }
202
203 /* If we have seen an eor previously, return a length of 0. The
204 caller is responsible for correctly padding the input field. */
205 if (dtp->u.p.sf_seen_eor)
206 {
207 *length = 0;
208 /* Just return something that isn't a NULL pointer, otherwise the
209 caller thinks an error occured. */
210 return (char*) empty_string;
211 }
212
213 if (is_internal_unit (dtp))
214 {
215 memread = *length;
216 base = mem_alloc_r (dtp->u.p.current_unit->s, length);
217 if (unlikely (memread > *length))
218 {
219 hit_eof (dtp);
220 return NULL;
221 }
222 n = *length;
223 goto done;
224 }
225
226 n = seen_comma = 0;
227
228 /* Read data into format buffer and scan through it. */
229 lorig = *length;
230 base = p = fbuf_read (dtp->u.p.current_unit, length);
231 if (base == NULL)
232 return NULL;
233
234 while (n < *length)
235 {
236 q = *p;
237
238 if (q == '\n' || q == '\r')
239 {
240 /* Unexpected end of line. */
241
242 /* If we see an EOR during non-advancing I/O, we need to skip
243 the rest of the I/O statement. Set the corresponding flag. */
244 if (dtp->u.p.advance_status == ADVANCE_NO || dtp->u.p.seen_dollar)
245 dtp->u.p.eor_condition = 1;
246
247 /* If we encounter a CR, it might be a CRLF. */
248 if (q == '\r') /* Probably a CRLF */
249 {
250 if (n < *length && *(p + 1) == '\n')
251 dtp->u.p.sf_seen_eor = 2;
252 }
253 else
254 dtp->u.p.sf_seen_eor = 1;
255
256 /* Without padding, terminate the I/O statement without assigning
257 the value. With padding, the value still needs to be assigned,
258 so we can just continue with a short read. */
259 if (dtp->u.p.current_unit->pad_status == PAD_NO)
260 {
261 if (likely (no_error))
262 break;
263 generate_error (&dtp->common, LIBERROR_EOR, NULL);
264 return NULL;
265 }
266
267 *length = n;
268 break;
269 }
270 /* Short circuit the read if a comma is found during numeric input.
271 The flag is set to zero during character reads so that commas in
272 strings are not ignored */
273 if (q == ',')
274 if (dtp->u.p.sf_read_comma == 1)
275 {
276 seen_comma = 1;
277 notify_std (&dtp->common, GFC_STD_GNU,
278 "Comma in formatted numeric read.");
279 *length = n;
280 break;
281 }
282
283 n++;
284 p++;
285 }
286
287 fbuf_seek (dtp->u.p.current_unit, n + dtp->u.p.sf_seen_eor + seen_comma,
288 SEEK_CUR);
289
290 /* A short read implies we hit EOF, unless we hit EOR, a comma, or
291 some other stuff. Set the relevant flags. */
292 if (lorig > *length && !dtp->u.p.sf_seen_eor && !seen_comma)
293 {
294 if (no_error)
295 dtp->u.p.at_eof = 1;
296 else
297 {
298 hit_eof (dtp);
299 return NULL;
300 }
301 }
302
303 done:
304
305 dtp->u.p.current_unit->bytes_left -= n;
306
307 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
308 dtp->u.p.size_used += (GFC_IO_INT) n;
309
310 return base;
311 }
312
313
314 /* Function for reading the next couple of bytes from the current
315 file, advancing the current position. We return FAILURE on end of record or
316 end of file. This function is only for formatted I/O, unformatted uses
317 read_block_direct.
318
319 If the read is short, then it is because the current record does not
320 have enough data to satisfy the read request and the file was
321 opened with PAD=YES. The caller must assume tailing spaces for
322 short reads. */
323
324 void *
325 read_block_form (st_parameter_dt *dtp, int * nbytes)
326 {
327 char *source;
328 int norig;
329
330 if (!is_stream_io (dtp))
331 {
332 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) *nbytes)
333 {
334 /* For preconnected units with default record length, set bytes left
335 to unit record length and proceed, otherwise error. */
336 if (dtp->u.p.current_unit->unit_number == options.stdin_unit
337 && dtp->u.p.current_unit->recl == DEFAULT_RECL)
338 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
339 else
340 {
341 if (unlikely (dtp->u.p.current_unit->pad_status == PAD_NO))
342 {
343 /* Not enough data left. */
344 generate_error (&dtp->common, LIBERROR_EOR, NULL);
345 return NULL;
346 }
347 }
348
349 if (unlikely (dtp->u.p.current_unit->bytes_left == 0))
350 {
351 hit_eof (dtp);
352 return NULL;
353 }
354
355 *nbytes = dtp->u.p.current_unit->bytes_left;
356 }
357 }
358
359 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
360 (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL ||
361 dtp->u.p.current_unit->flags.access == ACCESS_STREAM))
362 {
363 source = read_sf (dtp, nbytes, 0);
364 dtp->u.p.current_unit->strm_pos +=
365 (gfc_offset) (*nbytes + dtp->u.p.sf_seen_eor);
366 return source;
367 }
368
369 /* If we reach here, we can assume it's direct access. */
370
371 dtp->u.p.current_unit->bytes_left -= (gfc_offset) *nbytes;
372
373 norig = *nbytes;
374 source = fbuf_read (dtp->u.p.current_unit, nbytes);
375 fbuf_seek (dtp->u.p.current_unit, *nbytes, SEEK_CUR);
376
377 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
378 dtp->u.p.size_used += (GFC_IO_INT) *nbytes;
379
380 if (norig != *nbytes)
381 {
382 /* Short read, this shouldn't happen. */
383 if (!dtp->u.p.current_unit->pad_status == PAD_YES)
384 {
385 generate_error (&dtp->common, LIBERROR_EOR, NULL);
386 source = NULL;
387 }
388 }
389
390 dtp->u.p.current_unit->strm_pos += (gfc_offset) *nbytes;
391
392 return source;
393 }
394
395
396 /* Reads a block directly into application data space. This is for
397 unformatted files. */
398
399 static void
400 read_block_direct (st_parameter_dt *dtp, void *buf, size_t *nbytes)
401 {
402 ssize_t to_read_record;
403 ssize_t have_read_record;
404 ssize_t to_read_subrecord;
405 ssize_t have_read_subrecord;
406 int short_record;
407
408 if (is_stream_io (dtp))
409 {
410 to_read_record = *nbytes;
411 have_read_record = sread (dtp->u.p.current_unit->s, buf,
412 to_read_record);
413 if (unlikely (have_read_record < 0))
414 {
415 generate_error (&dtp->common, LIBERROR_OS, NULL);
416 return;
417 }
418
419 dtp->u.p.current_unit->strm_pos += (gfc_offset) have_read_record;
420
421 if (unlikely (to_read_record != have_read_record))
422 {
423 /* Short read, e.g. if we hit EOF. For stream files,
424 we have to set the end-of-file condition. */
425 hit_eof (dtp);
426 return;
427 }
428 return;
429 }
430
431 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
432 {
433 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) *nbytes)
434 {
435 short_record = 1;
436 to_read_record = (size_t) dtp->u.p.current_unit->bytes_left;
437 *nbytes = to_read_record;
438 }
439
440 else
441 {
442 short_record = 0;
443 to_read_record = *nbytes;
444 }
445
446 dtp->u.p.current_unit->bytes_left -= to_read_record;
447
448 to_read_record = sread (dtp->u.p.current_unit->s, buf, to_read_record);
449 if (unlikely (to_read_record < 0))
450 {
451 generate_error (&dtp->common, LIBERROR_OS, NULL);
452 return;
453 }
454
455 if (to_read_record != (ssize_t) *nbytes)
456 {
457 /* Short read, e.g. if we hit EOF. Apparently, we read
458 more than was written to the last record. */
459 *nbytes = to_read_record;
460 return;
461 }
462
463 if (unlikely (short_record))
464 {
465 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
466 return;
467 }
468 return;
469 }
470
471 /* Unformatted sequential. We loop over the subrecords, reading
472 until the request has been fulfilled or the record has run out
473 of continuation subrecords. */
474
475 /* Check whether we exceed the total record length. */
476
477 if (dtp->u.p.current_unit->flags.has_recl
478 && (*nbytes > (size_t) dtp->u.p.current_unit->bytes_left))
479 {
480 to_read_record = (ssize_t) dtp->u.p.current_unit->bytes_left;
481 short_record = 1;
482 }
483 else
484 {
485 to_read_record = *nbytes;
486 short_record = 0;
487 }
488 have_read_record = 0;
489
490 while(1)
491 {
492 if (dtp->u.p.current_unit->bytes_left_subrecord
493 < (gfc_offset) to_read_record)
494 {
495 to_read_subrecord = (ssize_t) dtp->u.p.current_unit->bytes_left_subrecord;
496 to_read_record -= to_read_subrecord;
497 }
498 else
499 {
500 to_read_subrecord = to_read_record;
501 to_read_record = 0;
502 }
503
504 dtp->u.p.current_unit->bytes_left_subrecord -= to_read_subrecord;
505
506 have_read_subrecord = sread (dtp->u.p.current_unit->s,
507 buf + have_read_record, to_read_subrecord);
508 if (unlikely (have_read_subrecord) < 0)
509 {
510 generate_error (&dtp->common, LIBERROR_OS, NULL);
511 return;
512 }
513
514 have_read_record += have_read_subrecord;
515
516 if (unlikely (to_read_subrecord != have_read_subrecord))
517
518 {
519 /* Short read, e.g. if we hit EOF. This means the record
520 structure has been corrupted, or the trailing record
521 marker would still be present. */
522
523 *nbytes = have_read_record;
524 generate_error (&dtp->common, LIBERROR_CORRUPT_FILE, NULL);
525 return;
526 }
527
528 if (to_read_record > 0)
529 {
530 if (likely (dtp->u.p.current_unit->continued))
531 {
532 next_record_r_unf (dtp, 0);
533 us_read (dtp, 1);
534 }
535 else
536 {
537 /* Let's make sure the file position is correctly pre-positioned
538 for the next read statement. */
539
540 dtp->u.p.current_unit->current_record = 0;
541 next_record_r_unf (dtp, 0);
542 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
543 return;
544 }
545 }
546 else
547 {
548 /* Normal exit, the read request has been fulfilled. */
549 break;
550 }
551 }
552
553 dtp->u.p.current_unit->bytes_left -= have_read_record;
554 if (unlikely (short_record))
555 {
556 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
557 return;
558 }
559 return;
560 }
561
562
563 /* Function for writing a block of bytes to the current file at the
564 current position, advancing the file pointer. We are given a length
565 and return a pointer to a buffer that the caller must (completely)
566 fill in. Returns NULL on error. */
567
568 void *
569 write_block (st_parameter_dt *dtp, int length)
570 {
571 char *dest;
572
573 if (!is_stream_io (dtp))
574 {
575 if (dtp->u.p.current_unit->bytes_left < (gfc_offset) length)
576 {
577 /* For preconnected units with default record length, set bytes left
578 to unit record length and proceed, otherwise error. */
579 if (likely ((dtp->u.p.current_unit->unit_number
580 == options.stdout_unit
581 || dtp->u.p.current_unit->unit_number
582 == options.stderr_unit)
583 && dtp->u.p.current_unit->recl == DEFAULT_RECL))
584 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
585 else
586 {
587 generate_error (&dtp->common, LIBERROR_EOR, NULL);
588 return NULL;
589 }
590 }
591
592 dtp->u.p.current_unit->bytes_left -= (gfc_offset) length;
593 }
594
595 if (is_internal_unit (dtp))
596 {
597 dest = mem_alloc_w (dtp->u.p.current_unit->s, &length);
598
599 if (dest == NULL)
600 {
601 generate_error (&dtp->common, LIBERROR_END, NULL);
602 return NULL;
603 }
604
605 if (unlikely (dtp->u.p.current_unit->endfile == AT_ENDFILE))
606 generate_error (&dtp->common, LIBERROR_END, NULL);
607 }
608 else
609 {
610 dest = fbuf_alloc (dtp->u.p.current_unit, length);
611 if (dest == NULL)
612 {
613 generate_error (&dtp->common, LIBERROR_OS, NULL);
614 return NULL;
615 }
616 }
617
618 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
619 dtp->u.p.size_used += (GFC_IO_INT) length;
620
621 dtp->u.p.current_unit->strm_pos += (gfc_offset) length;
622
623 return dest;
624 }
625
626
627 /* High level interface to swrite(), taking care of errors. This is only
628 called for unformatted files. There are three cases to consider:
629 Stream I/O, unformatted direct, unformatted sequential. */
630
631 static try
632 write_buf (st_parameter_dt *dtp, void *buf, size_t nbytes)
633 {
634
635 ssize_t have_written;
636 ssize_t to_write_subrecord;
637 int short_record;
638
639 /* Stream I/O. */
640
641 if (is_stream_io (dtp))
642 {
643 have_written = swrite (dtp->u.p.current_unit->s, buf, nbytes);
644 if (unlikely (have_written < 0))
645 {
646 generate_error (&dtp->common, LIBERROR_OS, NULL);
647 return FAILURE;
648 }
649
650 dtp->u.p.current_unit->strm_pos += (gfc_offset) have_written;
651
652 return SUCCESS;
653 }
654
655 /* Unformatted direct access. */
656
657 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
658 {
659 if (unlikely (dtp->u.p.current_unit->bytes_left < (gfc_offset) nbytes))
660 {
661 generate_error (&dtp->common, LIBERROR_DIRECT_EOR, NULL);
662 return FAILURE;
663 }
664
665 if (buf == NULL && nbytes == 0)
666 return SUCCESS;
667
668 have_written = swrite (dtp->u.p.current_unit->s, buf, nbytes);
669 if (unlikely (have_written < 0))
670 {
671 generate_error (&dtp->common, LIBERROR_OS, NULL);
672 return FAILURE;
673 }
674
675 dtp->u.p.current_unit->strm_pos += (gfc_offset) have_written;
676 dtp->u.p.current_unit->bytes_left -= (gfc_offset) have_written;
677
678 return SUCCESS;
679 }
680
681 /* Unformatted sequential. */
682
683 have_written = 0;
684
685 if (dtp->u.p.current_unit->flags.has_recl
686 && (gfc_offset) nbytes > dtp->u.p.current_unit->bytes_left)
687 {
688 nbytes = dtp->u.p.current_unit->bytes_left;
689 short_record = 1;
690 }
691 else
692 {
693 short_record = 0;
694 }
695
696 while (1)
697 {
698
699 to_write_subrecord =
700 (size_t) dtp->u.p.current_unit->bytes_left_subrecord < nbytes ?
701 (size_t) dtp->u.p.current_unit->bytes_left_subrecord : nbytes;
702
703 dtp->u.p.current_unit->bytes_left_subrecord -=
704 (gfc_offset) to_write_subrecord;
705
706 to_write_subrecord = swrite (dtp->u.p.current_unit->s,
707 buf + have_written, to_write_subrecord);
708 if (unlikely (to_write_subrecord < 0))
709 {
710 generate_error (&dtp->common, LIBERROR_OS, NULL);
711 return FAILURE;
712 }
713
714 dtp->u.p.current_unit->strm_pos += (gfc_offset) to_write_subrecord;
715 nbytes -= to_write_subrecord;
716 have_written += to_write_subrecord;
717
718 if (nbytes == 0)
719 break;
720
721 next_record_w_unf (dtp, 1);
722 us_write (dtp, 1);
723 }
724 dtp->u.p.current_unit->bytes_left -= have_written;
725 if (unlikely (short_record))
726 {
727 generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL);
728 return FAILURE;
729 }
730 return SUCCESS;
731 }
732
733
734 /* Master function for unformatted reads. */
735
736 static void
737 unformatted_read (st_parameter_dt *dtp, bt type,
738 void *dest, int kind, size_t size, size_t nelems)
739 {
740 size_t i, sz;
741
742 if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE)
743 || kind == 1)
744 {
745 sz = size * nelems;
746 if (type == BT_CHARACTER)
747 sz *= GFC_SIZE_OF_CHAR_KIND(kind);
748 read_block_direct (dtp, dest, &sz);
749 }
750 else
751 {
752 char buffer[16];
753 char *p;
754
755 p = dest;
756
757 /* Handle wide chracters. */
758 if (type == BT_CHARACTER && kind != 1)
759 {
760 nelems *= size;
761 size = kind;
762 }
763
764 /* Break up complex into its constituent reals. */
765 if (type == BT_COMPLEX)
766 {
767 nelems *= 2;
768 size /= 2;
769 }
770
771 /* By now, all complex variables have been split into their
772 constituent reals. */
773
774 for (i = 0; i < nelems; i++)
775 {
776 read_block_direct (dtp, buffer, &size);
777 reverse_memcpy (p, buffer, size);
778 p += size;
779 }
780 }
781 }
782
783
784 /* Master function for unformatted writes. NOTE: For kind=10 the size is 16
785 bytes on 64 bit machines. The unused bytes are not initialized and never
786 used, which can show an error with memory checking analyzers like
787 valgrind. */
788
789 static void
790 unformatted_write (st_parameter_dt *dtp, bt type,
791 void *source, int kind, size_t size, size_t nelems)
792 {
793 if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE)
794 || kind == 1)
795 {
796 size_t stride = type == BT_CHARACTER ?
797 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
798
799 write_buf (dtp, source, stride * nelems);
800 }
801 else
802 {
803 char buffer[16];
804 char *p;
805 size_t i;
806
807 p = source;
808
809 /* Handle wide chracters. */
810 if (type == BT_CHARACTER && kind != 1)
811 {
812 nelems *= size;
813 size = kind;
814 }
815
816 /* Break up complex into its constituent reals. */
817 if (type == BT_COMPLEX)
818 {
819 nelems *= 2;
820 size /= 2;
821 }
822
823 /* By now, all complex variables have been split into their
824 constituent reals. */
825
826 for (i = 0; i < nelems; i++)
827 {
828 reverse_memcpy(buffer, p, size);
829 p += size;
830 write_buf (dtp, buffer, size);
831 }
832 }
833 }
834
835
836 /* Return a pointer to the name of a type. */
837
838 const char *
839 type_name (bt type)
840 {
841 const char *p;
842
843 switch (type)
844 {
845 case BT_INTEGER:
846 p = "INTEGER";
847 break;
848 case BT_LOGICAL:
849 p = "LOGICAL";
850 break;
851 case BT_CHARACTER:
852 p = "CHARACTER";
853 break;
854 case BT_REAL:
855 p = "REAL";
856 break;
857 case BT_COMPLEX:
858 p = "COMPLEX";
859 break;
860 default:
861 internal_error (NULL, "type_name(): Bad type");
862 }
863
864 return p;
865 }
866
867
868 /* Write a constant string to the output.
869 This is complicated because the string can have doubled delimiters
870 in it. The length in the format node is the true length. */
871
872 static void
873 write_constant_string (st_parameter_dt *dtp, const fnode *f)
874 {
875 char c, delimiter, *p, *q;
876 int length;
877
878 length = f->u.string.length;
879 if (length == 0)
880 return;
881
882 p = write_block (dtp, length);
883 if (p == NULL)
884 return;
885
886 q = f->u.string.p;
887 delimiter = q[-1];
888
889 for (; length > 0; length--)
890 {
891 c = *p++ = *q++;
892 if (c == delimiter && c != 'H' && c != 'h')
893 q++; /* Skip the doubled delimiter. */
894 }
895 }
896
897
898 /* Given actual and expected types in a formatted data transfer, make
899 sure they agree. If not, an error message is generated. Returns
900 nonzero if something went wrong. */
901
902 static int
903 require_type (st_parameter_dt *dtp, bt expected, bt actual, const fnode *f)
904 {
905 char buffer[100];
906
907 if (actual == expected)
908 return 0;
909
910 sprintf (buffer, "Expected %s for item %d in formatted transfer, got %s",
911 type_name (expected), dtp->u.p.item_count, type_name (actual));
912
913 format_error (dtp, f, buffer);
914 return 1;
915 }
916
917
918 /* This subroutine is the main loop for a formatted data transfer
919 statement. It would be natural to implement this as a coroutine
920 with the user program, but C makes that awkward. We loop,
921 processing format elements. When we actually have to transfer
922 data instead of just setting flags, we return control to the user
923 program which calls a subroutine that supplies the address and type
924 of the next element, then comes back here to process it. */
925
926 static void
927 formatted_transfer_scalar (st_parameter_dt *dtp, bt type, void *p, int kind,
928 size_t size)
929 {
930 int pos, bytes_used;
931 const fnode *f;
932 format_token t;
933 int n;
934 int consume_data_flag;
935
936 /* Change a complex data item into a pair of reals. */
937
938 n = (p == NULL) ? 0 : ((type != BT_COMPLEX) ? 1 : 2);
939 if (type == BT_COMPLEX)
940 {
941 type = BT_REAL;
942 size /= 2;
943 }
944
945 /* If there's an EOR condition, we simulate finalizing the transfer
946 by doing nothing. */
947 if (dtp->u.p.eor_condition)
948 return;
949
950 /* Set this flag so that commas in reads cause the read to complete before
951 the entire field has been read. The next read field will start right after
952 the comma in the stream. (Set to 0 for character reads). */
953 dtp->u.p.sf_read_comma =
954 dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA ? 0 : 1;
955
956 for (;;)
957 {
958 /* If reversion has occurred and there is another real data item,
959 then we have to move to the next record. */
960 if (dtp->u.p.reversion_flag && n > 0)
961 {
962 dtp->u.p.reversion_flag = 0;
963 next_record (dtp, 0);
964 }
965
966 consume_data_flag = 1;
967 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
968 break;
969
970 f = next_format (dtp);
971 if (f == NULL)
972 {
973 /* No data descriptors left. */
974 if (unlikely (n > 0))
975 generate_error (&dtp->common, LIBERROR_FORMAT,
976 "Insufficient data descriptors in format after reversion");
977 return;
978 }
979
980 /* Now discharge T, TR and X movements to the right. This is delayed
981 until a data producing format to suppress trailing spaces. */
982
983 t = f->format;
984 if (dtp->u.p.mode == WRITING && dtp->u.p.skips != 0
985 && ((n>0 && ( t == FMT_I || t == FMT_B || t == FMT_O
986 || t == FMT_Z || t == FMT_F || t == FMT_E
987 || t == FMT_EN || t == FMT_ES || t == FMT_G
988 || t == FMT_L || t == FMT_A || t == FMT_D))
989 || t == FMT_STRING))
990 {
991 if (dtp->u.p.skips > 0)
992 {
993 int tmp;
994 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
995 tmp = (int)(dtp->u.p.current_unit->recl
996 - dtp->u.p.current_unit->bytes_left);
997 dtp->u.p.max_pos =
998 dtp->u.p.max_pos > tmp ? dtp->u.p.max_pos : tmp;
999 }
1000 if (dtp->u.p.skips < 0)
1001 {
1002 if (is_internal_unit (dtp))
1003 move_pos_offset (dtp->u.p.current_unit->s, dtp->u.p.skips);
1004 else
1005 fbuf_seek (dtp->u.p.current_unit, dtp->u.p.skips, SEEK_CUR);
1006 dtp->u.p.current_unit->bytes_left -= (gfc_offset) dtp->u.p.skips;
1007 }
1008 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1009 }
1010
1011 bytes_used = (int)(dtp->u.p.current_unit->recl
1012 - dtp->u.p.current_unit->bytes_left);
1013
1014 if (is_stream_io(dtp))
1015 bytes_used = 0;
1016
1017 switch (t)
1018 {
1019 case FMT_I:
1020 if (n == 0)
1021 goto need_data;
1022 if (require_type (dtp, BT_INTEGER, type, f))
1023 return;
1024
1025 if (dtp->u.p.mode == READING)
1026 read_decimal (dtp, f, p, kind);
1027 else
1028 write_i (dtp, f, p, kind);
1029
1030 break;
1031
1032 case FMT_B:
1033 if (n == 0)
1034 goto need_data;
1035
1036 if (compile_options.allow_std < GFC_STD_GNU
1037 && require_type (dtp, BT_INTEGER, type, f))
1038 return;
1039
1040 if (dtp->u.p.mode == READING)
1041 read_radix (dtp, f, p, kind, 2);
1042 else
1043 write_b (dtp, f, p, kind);
1044
1045 break;
1046
1047 case FMT_O:
1048 if (n == 0)
1049 goto need_data;
1050
1051 if (compile_options.allow_std < GFC_STD_GNU
1052 && require_type (dtp, BT_INTEGER, type, f))
1053 return;
1054
1055 if (dtp->u.p.mode == READING)
1056 read_radix (dtp, f, p, kind, 8);
1057 else
1058 write_o (dtp, f, p, kind);
1059
1060 break;
1061
1062 case FMT_Z:
1063 if (n == 0)
1064 goto need_data;
1065
1066 if (compile_options.allow_std < GFC_STD_GNU
1067 && require_type (dtp, BT_INTEGER, type, f))
1068 return;
1069
1070 if (dtp->u.p.mode == READING)
1071 read_radix (dtp, f, p, kind, 16);
1072 else
1073 write_z (dtp, f, p, kind);
1074
1075 break;
1076
1077 case FMT_A:
1078 if (n == 0)
1079 goto need_data;
1080
1081 /* It is possible to have FMT_A with something not BT_CHARACTER such
1082 as when writing out hollerith strings, so check both type
1083 and kind before calling wide character routines. */
1084 if (dtp->u.p.mode == READING)
1085 {
1086 if (type == BT_CHARACTER && kind == 4)
1087 read_a_char4 (dtp, f, p, size);
1088 else
1089 read_a (dtp, f, p, size);
1090 }
1091 else
1092 {
1093 if (type == BT_CHARACTER && kind == 4)
1094 write_a_char4 (dtp, f, p, size);
1095 else
1096 write_a (dtp, f, p, size);
1097 }
1098 break;
1099
1100 case FMT_L:
1101 if (n == 0)
1102 goto need_data;
1103
1104 if (dtp->u.p.mode == READING)
1105 read_l (dtp, f, p, kind);
1106 else
1107 write_l (dtp, f, p, kind);
1108
1109 break;
1110
1111 case FMT_D:
1112 if (n == 0)
1113 goto need_data;
1114 if (require_type (dtp, BT_REAL, type, f))
1115 return;
1116
1117 if (dtp->u.p.mode == READING)
1118 read_f (dtp, f, p, kind);
1119 else
1120 write_d (dtp, f, p, kind);
1121
1122 break;
1123
1124 case FMT_E:
1125 if (n == 0)
1126 goto need_data;
1127 if (require_type (dtp, BT_REAL, type, f))
1128 return;
1129
1130 if (dtp->u.p.mode == READING)
1131 read_f (dtp, f, p, kind);
1132 else
1133 write_e (dtp, f, p, kind);
1134 break;
1135
1136 case FMT_EN:
1137 if (n == 0)
1138 goto need_data;
1139 if (require_type (dtp, BT_REAL, type, f))
1140 return;
1141
1142 if (dtp->u.p.mode == READING)
1143 read_f (dtp, f, p, kind);
1144 else
1145 write_en (dtp, f, p, kind);
1146
1147 break;
1148
1149 case FMT_ES:
1150 if (n == 0)
1151 goto need_data;
1152 if (require_type (dtp, BT_REAL, type, f))
1153 return;
1154
1155 if (dtp->u.p.mode == READING)
1156 read_f (dtp, f, p, kind);
1157 else
1158 write_es (dtp, f, p, kind);
1159
1160 break;
1161
1162 case FMT_F:
1163 if (n == 0)
1164 goto need_data;
1165 if (require_type (dtp, BT_REAL, type, f))
1166 return;
1167
1168 if (dtp->u.p.mode == READING)
1169 read_f (dtp, f, p, kind);
1170 else
1171 write_f (dtp, f, p, kind);
1172
1173 break;
1174
1175 case FMT_G:
1176 if (n == 0)
1177 goto need_data;
1178 if (dtp->u.p.mode == READING)
1179 switch (type)
1180 {
1181 case BT_INTEGER:
1182 read_decimal (dtp, f, p, kind);
1183 break;
1184 case BT_LOGICAL:
1185 read_l (dtp, f, p, kind);
1186 break;
1187 case BT_CHARACTER:
1188 if (kind == 4)
1189 read_a_char4 (dtp, f, p, size);
1190 else
1191 read_a (dtp, f, p, size);
1192 break;
1193 case BT_REAL:
1194 read_f (dtp, f, p, kind);
1195 break;
1196 default:
1197 goto bad_type;
1198 }
1199 else
1200 switch (type)
1201 {
1202 case BT_INTEGER:
1203 write_i (dtp, f, p, kind);
1204 break;
1205 case BT_LOGICAL:
1206 write_l (dtp, f, p, kind);
1207 break;
1208 case BT_CHARACTER:
1209 if (kind == 4)
1210 write_a_char4 (dtp, f, p, size);
1211 else
1212 write_a (dtp, f, p, size);
1213 break;
1214 case BT_REAL:
1215 if (f->u.real.w == 0)
1216 write_real_g0 (dtp, p, kind, f->u.real.d);
1217 else
1218 write_d (dtp, f, p, kind);
1219 break;
1220 default:
1221 bad_type:
1222 internal_error (&dtp->common,
1223 "formatted_transfer(): Bad type");
1224 }
1225
1226 break;
1227
1228 case FMT_STRING:
1229 consume_data_flag = 0;
1230 if (dtp->u.p.mode == READING)
1231 {
1232 format_error (dtp, f, "Constant string in input format");
1233 return;
1234 }
1235 write_constant_string (dtp, f);
1236 break;
1237
1238 /* Format codes that don't transfer data. */
1239 case FMT_X:
1240 case FMT_TR:
1241 consume_data_flag = 0;
1242
1243 dtp->u.p.skips += f->u.n;
1244 pos = bytes_used + dtp->u.p.skips - 1;
1245 dtp->u.p.pending_spaces = pos - dtp->u.p.max_pos + 1;
1246 /* Writes occur just before the switch on f->format, above, so
1247 that trailing blanks are suppressed, unless we are doing a
1248 non-advancing write in which case we want to output the blanks
1249 now. */
1250 if (dtp->u.p.mode == WRITING
1251 && dtp->u.p.advance_status == ADVANCE_NO)
1252 {
1253 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
1254 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1255 }
1256
1257 if (dtp->u.p.mode == READING)
1258 read_x (dtp, f->u.n);
1259
1260 break;
1261
1262 case FMT_TL:
1263 case FMT_T:
1264 consume_data_flag = 0;
1265
1266 if (f->format == FMT_TL)
1267 {
1268
1269 /* Handle the special case when no bytes have been used yet.
1270 Cannot go below zero. */
1271 if (bytes_used == 0)
1272 {
1273 dtp->u.p.pending_spaces -= f->u.n;
1274 dtp->u.p.skips -= f->u.n;
1275 dtp->u.p.skips = dtp->u.p.skips < 0 ? 0 : dtp->u.p.skips;
1276 }
1277
1278 pos = bytes_used - f->u.n;
1279 }
1280 else /* FMT_T */
1281 {
1282 if (dtp->u.p.mode == READING)
1283 pos = f->u.n - 1;
1284 else
1285 pos = f->u.n - dtp->u.p.pending_spaces - 1;
1286 }
1287
1288 /* Standard 10.6.1.1: excessive left tabbing is reset to the
1289 left tab limit. We do not check if the position has gone
1290 beyond the end of record because a subsequent tab could
1291 bring us back again. */
1292 pos = pos < 0 ? 0 : pos;
1293
1294 dtp->u.p.skips = dtp->u.p.skips + pos - bytes_used;
1295 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces
1296 + pos - dtp->u.p.max_pos;
1297 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces < 0
1298 ? 0 : dtp->u.p.pending_spaces;
1299
1300 if (dtp->u.p.skips == 0)
1301 break;
1302
1303 /* Writes occur just before the switch on f->format, above, so that
1304 trailing blanks are suppressed. */
1305 if (dtp->u.p.mode == READING)
1306 {
1307 /* Adjust everything for end-of-record condition */
1308 if (dtp->u.p.sf_seen_eor && !is_internal_unit (dtp))
1309 {
1310 dtp->u.p.current_unit->bytes_left -= dtp->u.p.sf_seen_eor;
1311 dtp->u.p.skips -= dtp->u.p.sf_seen_eor;
1312 bytes_used = pos;
1313 dtp->u.p.sf_seen_eor = 0;
1314 }
1315 if (dtp->u.p.skips < 0)
1316 {
1317 if (is_internal_unit (dtp))
1318 move_pos_offset (dtp->u.p.current_unit->s, dtp->u.p.skips);
1319 else
1320 fbuf_seek (dtp->u.p.current_unit, dtp->u.p.skips, SEEK_CUR);
1321 dtp->u.p.current_unit->bytes_left
1322 -= (gfc_offset) dtp->u.p.skips;
1323 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1324 }
1325 else
1326 read_x (dtp, dtp->u.p.skips);
1327 }
1328
1329 break;
1330
1331 case FMT_S:
1332 consume_data_flag = 0;
1333 dtp->u.p.sign_status = SIGN_S;
1334 break;
1335
1336 case FMT_SS:
1337 consume_data_flag = 0;
1338 dtp->u.p.sign_status = SIGN_SS;
1339 break;
1340
1341 case FMT_SP:
1342 consume_data_flag = 0;
1343 dtp->u.p.sign_status = SIGN_SP;
1344 break;
1345
1346 case FMT_BN:
1347 consume_data_flag = 0 ;
1348 dtp->u.p.blank_status = BLANK_NULL;
1349 break;
1350
1351 case FMT_BZ:
1352 consume_data_flag = 0;
1353 dtp->u.p.blank_status = BLANK_ZERO;
1354 break;
1355
1356 case FMT_DC:
1357 consume_data_flag = 0;
1358 dtp->u.p.current_unit->decimal_status = DECIMAL_COMMA;
1359 break;
1360
1361 case FMT_DP:
1362 consume_data_flag = 0;
1363 dtp->u.p.current_unit->decimal_status = DECIMAL_POINT;
1364 break;
1365
1366 case FMT_P:
1367 consume_data_flag = 0;
1368 dtp->u.p.scale_factor = f->u.k;
1369 break;
1370
1371 case FMT_DOLLAR:
1372 consume_data_flag = 0;
1373 dtp->u.p.seen_dollar = 1;
1374 break;
1375
1376 case FMT_SLASH:
1377 consume_data_flag = 0;
1378 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1379 next_record (dtp, 0);
1380 break;
1381
1382 case FMT_COLON:
1383 /* A colon descriptor causes us to exit this loop (in
1384 particular preventing another / descriptor from being
1385 processed) unless there is another data item to be
1386 transferred. */
1387 consume_data_flag = 0;
1388 if (n == 0)
1389 return;
1390 break;
1391
1392 default:
1393 internal_error (&dtp->common, "Bad format node");
1394 }
1395
1396 /* Adjust the item count and data pointer. */
1397
1398 if ((consume_data_flag > 0) && (n > 0))
1399 {
1400 n--;
1401 p = ((char *) p) + size;
1402 }
1403
1404 if (dtp->u.p.mode == READING)
1405 dtp->u.p.skips = 0;
1406
1407 pos = (int)(dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left);
1408 dtp->u.p.max_pos = (dtp->u.p.max_pos > pos) ? dtp->u.p.max_pos : pos;
1409
1410 }
1411
1412 return;
1413
1414 /* Come here when we need a data descriptor but don't have one. We
1415 push the current format node back onto the input, then return and
1416 let the user program call us back with the data. */
1417 need_data:
1418 unget_format (dtp, f);
1419 }
1420
1421 static void
1422 formatted_transfer (st_parameter_dt *dtp, bt type, void *p, int kind,
1423 size_t size, size_t nelems)
1424 {
1425 size_t elem;
1426 char *tmp;
1427
1428 tmp = (char *) p;
1429 size_t stride = type == BT_CHARACTER ?
1430 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
1431 /* Big loop over all the elements. */
1432 for (elem = 0; elem < nelems; elem++)
1433 {
1434 dtp->u.p.item_count++;
1435 formatted_transfer_scalar (dtp, type, tmp + stride*elem, kind, size);
1436 }
1437 }
1438
1439
1440
1441 /* Data transfer entry points. The type of the data entity is
1442 implicit in the subroutine call. This prevents us from having to
1443 share a common enum with the compiler. */
1444
1445 void
1446 transfer_integer (st_parameter_dt *dtp, void *p, int kind)
1447 {
1448 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1449 return;
1450 dtp->u.p.transfer (dtp, BT_INTEGER, p, kind, kind, 1);
1451 }
1452
1453
1454 void
1455 transfer_real (st_parameter_dt *dtp, void *p, int kind)
1456 {
1457 size_t size;
1458 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1459 return;
1460 size = size_from_real_kind (kind);
1461 dtp->u.p.transfer (dtp, BT_REAL, p, kind, size, 1);
1462 }
1463
1464
1465 void
1466 transfer_logical (st_parameter_dt *dtp, void *p, int kind)
1467 {
1468 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1469 return;
1470 dtp->u.p.transfer (dtp, BT_LOGICAL, p, kind, kind, 1);
1471 }
1472
1473
1474 void
1475 transfer_character (st_parameter_dt *dtp, void *p, int len)
1476 {
1477 static char *empty_string[0];
1478
1479 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1480 return;
1481
1482 /* Strings of zero length can have p == NULL, which confuses the
1483 transfer routines into thinking we need more data elements. To avoid
1484 this, we give them a nice pointer. */
1485 if (len == 0 && p == NULL)
1486 p = empty_string;
1487
1488 /* Set kind here to 1. */
1489 dtp->u.p.transfer (dtp, BT_CHARACTER, p, 1, len, 1);
1490 }
1491
1492 void
1493 transfer_character_wide (st_parameter_dt *dtp, void *p, int len, int kind)
1494 {
1495 static char *empty_string[0];
1496
1497 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1498 return;
1499
1500 /* Strings of zero length can have p == NULL, which confuses the
1501 transfer routines into thinking we need more data elements. To avoid
1502 this, we give them a nice pointer. */
1503 if (len == 0 && p == NULL)
1504 p = empty_string;
1505
1506 /* Here we pass the actual kind value. */
1507 dtp->u.p.transfer (dtp, BT_CHARACTER, p, kind, len, 1);
1508 }
1509
1510
1511 void
1512 transfer_complex (st_parameter_dt *dtp, void *p, int kind)
1513 {
1514 size_t size;
1515 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1516 return;
1517 size = size_from_complex_kind (kind);
1518 dtp->u.p.transfer (dtp, BT_COMPLEX, p, kind, size, 1);
1519 }
1520
1521
1522 void
1523 transfer_array (st_parameter_dt *dtp, gfc_array_char *desc, int kind,
1524 gfc_charlen_type charlen)
1525 {
1526 index_type count[GFC_MAX_DIMENSIONS];
1527 index_type extent[GFC_MAX_DIMENSIONS];
1528 index_type stride[GFC_MAX_DIMENSIONS];
1529 index_type stride0, rank, size, type, n;
1530 size_t tsize;
1531 char *data;
1532 bt iotype;
1533
1534 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1535 return;
1536
1537 type = GFC_DESCRIPTOR_TYPE (desc);
1538 size = GFC_DESCRIPTOR_SIZE (desc);
1539
1540 /* FIXME: What a kludge: Array descriptors and the IO library use
1541 different enums for types. */
1542 switch (type)
1543 {
1544 case GFC_DTYPE_UNKNOWN:
1545 iotype = BT_NULL; /* Is this correct? */
1546 break;
1547 case GFC_DTYPE_INTEGER:
1548 iotype = BT_INTEGER;
1549 break;
1550 case GFC_DTYPE_LOGICAL:
1551 iotype = BT_LOGICAL;
1552 break;
1553 case GFC_DTYPE_REAL:
1554 iotype = BT_REAL;
1555 break;
1556 case GFC_DTYPE_COMPLEX:
1557 iotype = BT_COMPLEX;
1558 break;
1559 case GFC_DTYPE_CHARACTER:
1560 iotype = BT_CHARACTER;
1561 size = charlen;
1562 break;
1563 case GFC_DTYPE_DERIVED:
1564 internal_error (&dtp->common,
1565 "Derived type I/O should have been handled via the frontend.");
1566 break;
1567 default:
1568 internal_error (&dtp->common, "transfer_array(): Bad type");
1569 }
1570
1571 rank = GFC_DESCRIPTOR_RANK (desc);
1572 for (n = 0; n < rank; n++)
1573 {
1574 count[n] = 0;
1575 stride[n] = iotype == BT_CHARACTER ?
1576 desc->dim[n].stride * GFC_SIZE_OF_CHAR_KIND(kind) :
1577 desc->dim[n].stride;
1578 extent[n] = desc->dim[n].ubound + 1 - desc->dim[n].lbound;
1579
1580 /* If the extent of even one dimension is zero, then the entire
1581 array section contains zero elements, so we return after writing
1582 a zero array record. */
1583 if (extent[n] <= 0)
1584 {
1585 data = NULL;
1586 tsize = 0;
1587 dtp->u.p.transfer (dtp, iotype, data, kind, size, tsize);
1588 return;
1589 }
1590 }
1591
1592 stride0 = stride[0];
1593
1594 /* If the innermost dimension has stride 1, we can do the transfer
1595 in contiguous chunks. */
1596 if (stride0 == 1)
1597 tsize = extent[0];
1598 else
1599 tsize = 1;
1600
1601 data = GFC_DESCRIPTOR_DATA (desc);
1602
1603 while (data)
1604 {
1605 dtp->u.p.transfer (dtp, iotype, data, kind, size, tsize);
1606 data += stride0 * size * tsize;
1607 count[0] += tsize;
1608 n = 0;
1609 while (count[n] == extent[n])
1610 {
1611 count[n] = 0;
1612 data -= stride[n] * extent[n] * size;
1613 n++;
1614 if (n == rank)
1615 {
1616 data = NULL;
1617 break;
1618 }
1619 else
1620 {
1621 count[n]++;
1622 data += stride[n] * size;
1623 }
1624 }
1625 }
1626 }
1627
1628
1629 /* Preposition a sequential unformatted file while reading. */
1630
1631 static void
1632 us_read (st_parameter_dt *dtp, int continued)
1633 {
1634 ssize_t n, nr;
1635 GFC_INTEGER_4 i4;
1636 GFC_INTEGER_8 i8;
1637 gfc_offset i;
1638
1639 if (compile_options.record_marker == 0)
1640 n = sizeof (GFC_INTEGER_4);
1641 else
1642 n = compile_options.record_marker;
1643
1644 nr = sread (dtp->u.p.current_unit->s, &i, n);
1645 if (unlikely (nr < 0))
1646 {
1647 generate_error (&dtp->common, LIBERROR_BAD_US, NULL);
1648 return;
1649 }
1650 else if (nr == 0)
1651 {
1652 hit_eof (dtp);
1653 return; /* end of file */
1654 }
1655 else if (unlikely (n != nr))
1656 {
1657 generate_error (&dtp->common, LIBERROR_BAD_US, NULL);
1658 return;
1659 }
1660
1661 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
1662 if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE))
1663 {
1664 switch (nr)
1665 {
1666 case sizeof(GFC_INTEGER_4):
1667 memcpy (&i4, &i, sizeof (i4));
1668 i = i4;
1669 break;
1670
1671 case sizeof(GFC_INTEGER_8):
1672 memcpy (&i8, &i, sizeof (i8));
1673 i = i8;
1674 break;
1675
1676 default:
1677 runtime_error ("Illegal value for record marker");
1678 break;
1679 }
1680 }
1681 else
1682 switch (nr)
1683 {
1684 case sizeof(GFC_INTEGER_4):
1685 reverse_memcpy (&i4, &i, sizeof (i4));
1686 i = i4;
1687 break;
1688
1689 case sizeof(GFC_INTEGER_8):
1690 reverse_memcpy (&i8, &i, sizeof (i8));
1691 i = i8;
1692 break;
1693
1694 default:
1695 runtime_error ("Illegal value for record marker");
1696 break;
1697 }
1698
1699 if (i >= 0)
1700 {
1701 dtp->u.p.current_unit->bytes_left_subrecord = i;
1702 dtp->u.p.current_unit->continued = 0;
1703 }
1704 else
1705 {
1706 dtp->u.p.current_unit->bytes_left_subrecord = -i;
1707 dtp->u.p.current_unit->continued = 1;
1708 }
1709
1710 if (! continued)
1711 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
1712 }
1713
1714
1715 /* Preposition a sequential unformatted file while writing. This
1716 amount to writing a bogus length that will be filled in later. */
1717
1718 static void
1719 us_write (st_parameter_dt *dtp, int continued)
1720 {
1721 ssize_t nbytes;
1722 gfc_offset dummy;
1723
1724 dummy = 0;
1725
1726 if (compile_options.record_marker == 0)
1727 nbytes = sizeof (GFC_INTEGER_4);
1728 else
1729 nbytes = compile_options.record_marker ;
1730
1731 if (swrite (dtp->u.p.current_unit->s, &dummy, nbytes) != nbytes)
1732 generate_error (&dtp->common, LIBERROR_OS, NULL);
1733
1734 /* For sequential unformatted, if RECL= was not specified in the OPEN
1735 we write until we have more bytes than can fit in the subrecord
1736 markers, then we write a new subrecord. */
1737
1738 dtp->u.p.current_unit->bytes_left_subrecord =
1739 dtp->u.p.current_unit->recl_subrecord;
1740 dtp->u.p.current_unit->continued = continued;
1741 }
1742
1743
1744 /* Position to the next record prior to transfer. We are assumed to
1745 be before the next record. We also calculate the bytes in the next
1746 record. */
1747
1748 static void
1749 pre_position (st_parameter_dt *dtp)
1750 {
1751 if (dtp->u.p.current_unit->current_record)
1752 return; /* Already positioned. */
1753
1754 switch (current_mode (dtp))
1755 {
1756 case FORMATTED_STREAM:
1757 case UNFORMATTED_STREAM:
1758 /* There are no records with stream I/O. If the position was specified
1759 data_transfer_init has already positioned the file. If no position
1760 was specified, we continue from where we last left off. I.e.
1761 there is nothing to do here. */
1762 break;
1763
1764 case UNFORMATTED_SEQUENTIAL:
1765 if (dtp->u.p.mode == READING)
1766 us_read (dtp, 0);
1767 else
1768 us_write (dtp, 0);
1769
1770 break;
1771
1772 case FORMATTED_SEQUENTIAL:
1773 case FORMATTED_DIRECT:
1774 case UNFORMATTED_DIRECT:
1775 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
1776 break;
1777 }
1778
1779 dtp->u.p.current_unit->current_record = 1;
1780 }
1781
1782
1783 /* Initialize things for a data transfer. This code is common for
1784 both reading and writing. */
1785
1786 static void
1787 data_transfer_init (st_parameter_dt *dtp, int read_flag)
1788 {
1789 unit_flags u_flags; /* Used for creating a unit if needed. */
1790 GFC_INTEGER_4 cf = dtp->common.flags;
1791 namelist_info *ionml;
1792
1793 ionml = ((cf & IOPARM_DT_IONML_SET) != 0) ? dtp->u.p.ionml : NULL;
1794
1795 memset (&dtp->u.p, 0, sizeof (dtp->u.p));
1796
1797 dtp->u.p.ionml = ionml;
1798 dtp->u.p.mode = read_flag ? READING : WRITING;
1799
1800 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1801 return;
1802
1803 if ((cf & IOPARM_DT_HAS_SIZE) != 0)
1804 dtp->u.p.size_used = 0; /* Initialize the count. */
1805
1806 dtp->u.p.current_unit = get_unit (dtp, 1);
1807 if (dtp->u.p.current_unit->s == NULL)
1808 { /* Open the unit with some default flags. */
1809 st_parameter_open opp;
1810 unit_convert conv;
1811
1812 if (dtp->common.unit < 0)
1813 {
1814 close_unit (dtp->u.p.current_unit);
1815 dtp->u.p.current_unit = NULL;
1816 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
1817 "Bad unit number in OPEN statement");
1818 return;
1819 }
1820 memset (&u_flags, '\0', sizeof (u_flags));
1821 u_flags.access = ACCESS_SEQUENTIAL;
1822 u_flags.action = ACTION_READWRITE;
1823
1824 /* Is it unformatted? */
1825 if (!(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT
1826 | IOPARM_DT_IONML_SET)))
1827 u_flags.form = FORM_UNFORMATTED;
1828 else
1829 u_flags.form = FORM_UNSPECIFIED;
1830
1831 u_flags.delim = DELIM_UNSPECIFIED;
1832 u_flags.blank = BLANK_UNSPECIFIED;
1833 u_flags.pad = PAD_UNSPECIFIED;
1834 u_flags.decimal = DECIMAL_UNSPECIFIED;
1835 u_flags.encoding = ENCODING_UNSPECIFIED;
1836 u_flags.async = ASYNC_UNSPECIFIED;
1837 u_flags.round = ROUND_UNSPECIFIED;
1838 u_flags.sign = SIGN_UNSPECIFIED;
1839
1840 u_flags.status = STATUS_UNKNOWN;
1841
1842 conv = get_unformatted_convert (dtp->common.unit);
1843
1844 if (conv == GFC_CONVERT_NONE)
1845 conv = compile_options.convert;
1846
1847 /* We use big_endian, which is 0 on little-endian machines
1848 and 1 on big-endian machines. */
1849 switch (conv)
1850 {
1851 case GFC_CONVERT_NATIVE:
1852 case GFC_CONVERT_SWAP:
1853 break;
1854
1855 case GFC_CONVERT_BIG:
1856 conv = big_endian ? GFC_CONVERT_NATIVE : GFC_CONVERT_SWAP;
1857 break;
1858
1859 case GFC_CONVERT_LITTLE:
1860 conv = big_endian ? GFC_CONVERT_SWAP : GFC_CONVERT_NATIVE;
1861 break;
1862
1863 default:
1864 internal_error (&opp.common, "Illegal value for CONVERT");
1865 break;
1866 }
1867
1868 u_flags.convert = conv;
1869
1870 opp.common = dtp->common;
1871 opp.common.flags &= IOPARM_COMMON_MASK;
1872 dtp->u.p.current_unit = new_unit (&opp, dtp->u.p.current_unit, &u_flags);
1873 dtp->common.flags &= ~IOPARM_COMMON_MASK;
1874 dtp->common.flags |= (opp.common.flags & IOPARM_COMMON_MASK);
1875 if (dtp->u.p.current_unit == NULL)
1876 return;
1877 }
1878
1879 /* Check the action. */
1880
1881 if (read_flag && dtp->u.p.current_unit->flags.action == ACTION_WRITE)
1882 {
1883 generate_error (&dtp->common, LIBERROR_BAD_ACTION,
1884 "Cannot read from file opened for WRITE");
1885 return;
1886 }
1887
1888 if (!read_flag && dtp->u.p.current_unit->flags.action == ACTION_READ)
1889 {
1890 generate_error (&dtp->common, LIBERROR_BAD_ACTION,
1891 "Cannot write to file opened for READ");
1892 return;
1893 }
1894
1895 dtp->u.p.first_item = 1;
1896
1897 /* Check the format. */
1898
1899 if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
1900 parse_format (dtp);
1901
1902 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED
1903 && (cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
1904 != 0)
1905 {
1906 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
1907 "Format present for UNFORMATTED data transfer");
1908 return;
1909 }
1910
1911 if ((cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0 && dtp->u.p.ionml != NULL)
1912 {
1913 if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
1914 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
1915 "A format cannot be specified with a namelist");
1916 }
1917 else if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
1918 !(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT)))
1919 {
1920 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
1921 "Missing format for FORMATTED data transfer");
1922 }
1923
1924 if (is_internal_unit (dtp)
1925 && dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
1926 {
1927 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
1928 "Internal file cannot be accessed by UNFORMATTED "
1929 "data transfer");
1930 return;
1931 }
1932
1933 /* Check the record or position number. */
1934
1935 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT
1936 && (cf & IOPARM_DT_HAS_REC) == 0)
1937 {
1938 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
1939 "Direct access data transfer requires record number");
1940 return;
1941 }
1942
1943 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL
1944 && (cf & IOPARM_DT_HAS_REC) != 0)
1945 {
1946 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
1947 "Record number not allowed for sequential access "
1948 "data transfer");
1949 return;
1950 }
1951
1952 /* Process the ADVANCE option. */
1953
1954 dtp->u.p.advance_status
1955 = !(cf & IOPARM_DT_HAS_ADVANCE) ? ADVANCE_UNSPECIFIED :
1956 find_option (&dtp->common, dtp->advance, dtp->advance_len, advance_opt,
1957 "Bad ADVANCE parameter in data transfer statement");
1958
1959 if (dtp->u.p.advance_status != ADVANCE_UNSPECIFIED)
1960 {
1961 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
1962 {
1963 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
1964 "ADVANCE specification conflicts with sequential "
1965 "access");
1966 return;
1967 }
1968
1969 if (is_internal_unit (dtp))
1970 {
1971 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
1972 "ADVANCE specification conflicts with internal file");
1973 return;
1974 }
1975
1976 if ((cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
1977 != IOPARM_DT_HAS_FORMAT)
1978 {
1979 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
1980 "ADVANCE specification requires an explicit format");
1981 return;
1982 }
1983 }
1984
1985 if (read_flag)
1986 {
1987 dtp->u.p.current_unit->previous_nonadvancing_write = 0;
1988
1989 if ((cf & IOPARM_EOR) != 0 && dtp->u.p.advance_status != ADVANCE_NO)
1990 {
1991 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
1992 "EOR specification requires an ADVANCE specification "
1993 "of NO");
1994 return;
1995 }
1996
1997 if ((cf & IOPARM_DT_HAS_SIZE) != 0
1998 && dtp->u.p.advance_status != ADVANCE_NO)
1999 {
2000 generate_error (&dtp->common, LIBERROR_MISSING_OPTION,
2001 "SIZE specification requires an ADVANCE "
2002 "specification of NO");
2003 return;
2004 }
2005 }
2006 else
2007 { /* Write constraints. */
2008 if ((cf & IOPARM_END) != 0)
2009 {
2010 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2011 "END specification cannot appear in a write "
2012 "statement");
2013 return;
2014 }
2015
2016 if ((cf & IOPARM_EOR) != 0)
2017 {
2018 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2019 "EOR specification cannot appear in a write "
2020 "statement");
2021 return;
2022 }
2023
2024 if ((cf & IOPARM_DT_HAS_SIZE) != 0)
2025 {
2026 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2027 "SIZE specification cannot appear in a write "
2028 "statement");
2029 return;
2030 }
2031 }
2032
2033 if (dtp->u.p.advance_status == ADVANCE_UNSPECIFIED)
2034 dtp->u.p.advance_status = ADVANCE_YES;
2035
2036 /* Check the decimal mode. */
2037 dtp->u.p.current_unit->decimal_status
2038 = !(cf & IOPARM_DT_HAS_DECIMAL) ? DECIMAL_UNSPECIFIED :
2039 find_option (&dtp->common, dtp->decimal, dtp->decimal_len,
2040 decimal_opt, "Bad DECIMAL parameter in data transfer "
2041 "statement");
2042
2043 if (dtp->u.p.current_unit->decimal_status == DECIMAL_UNSPECIFIED)
2044 dtp->u.p.current_unit->decimal_status = dtp->u.p.current_unit->flags.decimal;
2045
2046 /* Check the sign mode. */
2047 dtp->u.p.sign_status
2048 = !(cf & IOPARM_DT_HAS_SIGN) ? SIGN_UNSPECIFIED :
2049 find_option (&dtp->common, dtp->sign, dtp->sign_len, sign_opt,
2050 "Bad SIGN parameter in data transfer statement");
2051
2052 if (dtp->u.p.sign_status == SIGN_UNSPECIFIED)
2053 dtp->u.p.sign_status = dtp->u.p.current_unit->flags.sign;
2054
2055 /* Check the blank mode. */
2056 dtp->u.p.blank_status
2057 = !(cf & IOPARM_DT_HAS_BLANK) ? BLANK_UNSPECIFIED :
2058 find_option (&dtp->common, dtp->blank, dtp->blank_len,
2059 blank_opt,
2060 "Bad BLANK parameter in data transfer statement");
2061
2062 if (dtp->u.p.blank_status == BLANK_UNSPECIFIED)
2063 dtp->u.p.blank_status = dtp->u.p.current_unit->flags.blank;
2064
2065 /* Check the delim mode. */
2066 dtp->u.p.current_unit->delim_status
2067 = !(cf & IOPARM_DT_HAS_DELIM) ? DELIM_UNSPECIFIED :
2068 find_option (&dtp->common, dtp->delim, dtp->delim_len,
2069 delim_opt, "Bad DELIM parameter in data transfer statement");
2070
2071 if (dtp->u.p.current_unit->delim_status == DELIM_UNSPECIFIED)
2072 dtp->u.p.current_unit->delim_status = dtp->u.p.current_unit->flags.delim;
2073
2074 /* Check the pad mode. */
2075 dtp->u.p.current_unit->pad_status
2076 = !(cf & IOPARM_DT_HAS_PAD) ? PAD_UNSPECIFIED :
2077 find_option (&dtp->common, dtp->pad, dtp->pad_len, pad_opt,
2078 "Bad PAD parameter in data transfer statement");
2079
2080 if (dtp->u.p.current_unit->pad_status == PAD_UNSPECIFIED)
2081 dtp->u.p.current_unit->pad_status = dtp->u.p.current_unit->flags.pad;
2082
2083 /* Check to see if we might be reading what we wrote before */
2084
2085 if (dtp->u.p.mode != dtp->u.p.current_unit->mode
2086 && !is_internal_unit (dtp))
2087 {
2088 int pos = fbuf_reset (dtp->u.p.current_unit);
2089 if (pos != 0)
2090 sseek (dtp->u.p.current_unit->s, pos, SEEK_CUR);
2091 sflush(dtp->u.p.current_unit->s);
2092 }
2093
2094 /* Check the POS= specifier: that it is in range and that it is used with a
2095 unit that has been connected for STREAM access. F2003 9.5.1.10. */
2096
2097 if (((cf & IOPARM_DT_HAS_POS) != 0))
2098 {
2099 if (is_stream_io (dtp))
2100 {
2101
2102 if (dtp->pos <= 0)
2103 {
2104 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2105 "POS=specifier must be positive");
2106 return;
2107 }
2108
2109 if (dtp->pos >= dtp->u.p.current_unit->maxrec)
2110 {
2111 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2112 "POS=specifier too large");
2113 return;
2114 }
2115
2116 dtp->rec = dtp->pos;
2117
2118 if (dtp->u.p.mode == READING)
2119 {
2120 /* Reset the endfile flag; if we hit EOF during reading
2121 we'll set the flag and generate an error at that point
2122 rather than worrying about it here. */
2123 dtp->u.p.current_unit->endfile = NO_ENDFILE;
2124 }
2125
2126 if (dtp->pos != dtp->u.p.current_unit->strm_pos)
2127 {
2128 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
2129 sflush (dtp->u.p.current_unit->s);
2130 if (sseek (dtp->u.p.current_unit->s, dtp->pos - 1, SEEK_SET) < 0)
2131 {
2132 generate_error (&dtp->common, LIBERROR_OS, NULL);
2133 return;
2134 }
2135 dtp->u.p.current_unit->strm_pos = dtp->pos;
2136 }
2137 }
2138 else
2139 {
2140 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2141 "POS=specifier not allowed, "
2142 "Try OPEN with ACCESS='stream'");
2143 return;
2144 }
2145 }
2146
2147
2148 /* Sanity checks on the record number. */
2149 if ((cf & IOPARM_DT_HAS_REC) != 0)
2150 {
2151 if (dtp->rec <= 0)
2152 {
2153 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2154 "Record number must be positive");
2155 return;
2156 }
2157
2158 if (dtp->rec >= dtp->u.p.current_unit->maxrec)
2159 {
2160 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2161 "Record number too large");
2162 return;
2163 }
2164
2165 /* Make sure format buffer is reset. */
2166 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED)
2167 fbuf_reset (dtp->u.p.current_unit);
2168
2169
2170 /* Check whether the record exists to be read. Only
2171 a partial record needs to exist. */
2172
2173 if (dtp->u.p.mode == READING && (dtp->rec - 1)
2174 * dtp->u.p.current_unit->recl >= file_length (dtp->u.p.current_unit->s))
2175 {
2176 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2177 "Non-existing record number");
2178 return;
2179 }
2180
2181 /* Position the file. */
2182 if (sseek (dtp->u.p.current_unit->s, (gfc_offset) (dtp->rec - 1)
2183 * dtp->u.p.current_unit->recl, SEEK_SET) < 0)
2184 {
2185 generate_error (&dtp->common, LIBERROR_OS, NULL);
2186 return;
2187 }
2188
2189 /* TODO: This is required to maintain compatibility between
2190 4.3 and 4.4 runtime. Remove when ABI changes from 4.3 */
2191
2192 if (is_stream_io (dtp))
2193 dtp->u.p.current_unit->strm_pos = dtp->rec;
2194
2195 /* TODO: Un-comment this code when ABI changes from 4.3.
2196 if (dtp->u.p.current_unit->flags.access == ACCESS_STREAM)
2197 {
2198 generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT,
2199 "Record number not allowed for stream access "
2200 "data transfer");
2201 return;
2202 } */
2203 }
2204
2205 /* Bugware for badly written mixed C-Fortran I/O. */
2206 flush_if_preconnected(dtp->u.p.current_unit->s);
2207
2208 dtp->u.p.current_unit->mode = dtp->u.p.mode;
2209
2210 /* Set the maximum position reached from the previous I/O operation. This
2211 could be greater than zero from a previous non-advancing write. */
2212 dtp->u.p.max_pos = dtp->u.p.current_unit->saved_pos;
2213
2214 pre_position (dtp);
2215
2216
2217 /* Set up the subroutine that will handle the transfers. */
2218
2219 if (read_flag)
2220 {
2221 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
2222 dtp->u.p.transfer = unformatted_read;
2223 else
2224 {
2225 if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
2226 dtp->u.p.transfer = list_formatted_read;
2227 else
2228 dtp->u.p.transfer = formatted_transfer;
2229 }
2230 }
2231 else
2232 {
2233 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
2234 dtp->u.p.transfer = unformatted_write;
2235 else
2236 {
2237 if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
2238 dtp->u.p.transfer = list_formatted_write;
2239 else
2240 dtp->u.p.transfer = formatted_transfer;
2241 }
2242 }
2243
2244 /* Make sure that we don't do a read after a nonadvancing write. */
2245
2246 if (read_flag)
2247 {
2248 if (dtp->u.p.current_unit->read_bad && !is_stream_io (dtp))
2249 {
2250 generate_error (&dtp->common, LIBERROR_BAD_OPTION,
2251 "Cannot READ after a nonadvancing WRITE");
2252 return;
2253 }
2254 }
2255 else
2256 {
2257 if (dtp->u.p.advance_status == ADVANCE_YES && !dtp->u.p.seen_dollar)
2258 dtp->u.p.current_unit->read_bad = 1;
2259 }
2260
2261 /* Start the data transfer if we are doing a formatted transfer. */
2262 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
2263 && ((cf & (IOPARM_DT_LIST_FORMAT | IOPARM_DT_HAS_NAMELIST_NAME)) == 0)
2264 && dtp->u.p.ionml == NULL)
2265 formatted_transfer (dtp, 0, NULL, 0, 0, 1);
2266 }
2267
2268 /* Initialize an array_loop_spec given the array descriptor. The function
2269 returns the index of the last element of the array, and also returns
2270 starting record, where the first I/O goes to (necessary in case of
2271 negative strides). */
2272
2273 gfc_offset
2274 init_loop_spec (gfc_array_char *desc, array_loop_spec *ls,
2275 gfc_offset *start_record)
2276 {
2277 int rank = GFC_DESCRIPTOR_RANK(desc);
2278 int i;
2279 gfc_offset index;
2280 int empty;
2281
2282 empty = 0;
2283 index = 1;
2284 *start_record = 0;
2285
2286 for (i=0; i<rank; i++)
2287 {
2288 ls[i].idx = desc->dim[i].lbound;
2289 ls[i].start = desc->dim[i].lbound;
2290 ls[i].end = desc->dim[i].ubound;
2291 ls[i].step = desc->dim[i].stride;
2292 empty = empty || (desc->dim[i].ubound < desc->dim[i].lbound);
2293
2294 if (desc->dim[i].stride > 0)
2295 {
2296 index += (desc->dim[i].ubound - desc->dim[i].lbound)
2297 * desc->dim[i].stride;
2298 }
2299 else
2300 {
2301 index -= (desc->dim[i].ubound - desc->dim[i].lbound)
2302 * desc->dim[i].stride;
2303 *start_record -= (desc->dim[i].ubound - desc->dim[i].lbound)
2304 * desc->dim[i].stride;
2305 }
2306 }
2307
2308 if (empty)
2309 return 0;
2310 else
2311 return index;
2312 }
2313
2314 /* Determine the index to the next record in an internal unit array by
2315 by incrementing through the array_loop_spec. */
2316
2317 gfc_offset
2318 next_array_record (st_parameter_dt *dtp, array_loop_spec *ls, int *finished)
2319 {
2320 int i, carry;
2321 gfc_offset index;
2322
2323 carry = 1;
2324 index = 0;
2325
2326 for (i = 0; i < dtp->u.p.current_unit->rank; i++)
2327 {
2328 if (carry)
2329 {
2330 ls[i].idx++;
2331 if (ls[i].idx > ls[i].end)
2332 {
2333 ls[i].idx = ls[i].start;
2334 carry = 1;
2335 }
2336 else
2337 carry = 0;
2338 }
2339 index = index + (ls[i].idx - ls[i].start) * ls[i].step;
2340 }
2341
2342 *finished = carry;
2343
2344 return index;
2345 }
2346
2347
2348
2349 /* Skip to the end of the current record, taking care of an optional
2350 record marker of size bytes. If the file is not seekable, we
2351 read chunks of size MAX_READ until we get to the right
2352 position. */
2353
2354 static void
2355 skip_record (st_parameter_dt *dtp, size_t bytes)
2356 {
2357 size_t rlength;
2358 ssize_t readb;
2359 static const size_t MAX_READ = 4096;
2360 char p[MAX_READ];
2361
2362 dtp->u.p.current_unit->bytes_left_subrecord += bytes;
2363 if (dtp->u.p.current_unit->bytes_left_subrecord == 0)
2364 return;
2365
2366 if (is_seekable (dtp->u.p.current_unit->s))
2367 {
2368 /* Direct access files do not generate END conditions,
2369 only I/O errors. */
2370 if (sseek (dtp->u.p.current_unit->s,
2371 dtp->u.p.current_unit->bytes_left_subrecord, SEEK_CUR) < 0)
2372 generate_error (&dtp->common, LIBERROR_OS, NULL);
2373 }
2374 else
2375 { /* Seek by reading data. */
2376 while (dtp->u.p.current_unit->bytes_left_subrecord > 0)
2377 {
2378 rlength =
2379 (MAX_READ < (size_t) dtp->u.p.current_unit->bytes_left_subrecord) ?
2380 MAX_READ : (size_t) dtp->u.p.current_unit->bytes_left_subrecord;
2381
2382 readb = sread (dtp->u.p.current_unit->s, p, rlength);
2383 if (readb < 0)
2384 {
2385 generate_error (&dtp->common, LIBERROR_OS, NULL);
2386 return;
2387 }
2388
2389 dtp->u.p.current_unit->bytes_left_subrecord -= readb;
2390 }
2391 }
2392
2393 }
2394
2395
2396 /* Advance to the next record reading unformatted files, taking
2397 care of subrecords. If complete_record is nonzero, we loop
2398 until all subrecords are cleared. */
2399
2400 static void
2401 next_record_r_unf (st_parameter_dt *dtp, int complete_record)
2402 {
2403 size_t bytes;
2404
2405 bytes = compile_options.record_marker == 0 ?
2406 sizeof (GFC_INTEGER_4) : compile_options.record_marker;
2407
2408 while(1)
2409 {
2410
2411 /* Skip over tail */
2412
2413 skip_record (dtp, bytes);
2414
2415 if ( ! (complete_record && dtp->u.p.current_unit->continued))
2416 return;
2417
2418 us_read (dtp, 1);
2419 }
2420 }
2421
2422
2423 static inline gfc_offset
2424 min_off (gfc_offset a, gfc_offset b)
2425 {
2426 return (a < b ? a : b);
2427 }
2428
2429
2430 /* Space to the next record for read mode. */
2431
2432 static void
2433 next_record_r (st_parameter_dt *dtp)
2434 {
2435 gfc_offset record;
2436 int bytes_left;
2437 char p;
2438 int cc;
2439
2440 switch (current_mode (dtp))
2441 {
2442 /* No records in unformatted STREAM I/O. */
2443 case UNFORMATTED_STREAM:
2444 return;
2445
2446 case UNFORMATTED_SEQUENTIAL:
2447 next_record_r_unf (dtp, 1);
2448 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2449 break;
2450
2451 case FORMATTED_DIRECT:
2452 case UNFORMATTED_DIRECT:
2453 skip_record (dtp, 0);
2454 break;
2455
2456 case FORMATTED_STREAM:
2457 case FORMATTED_SEQUENTIAL:
2458 /* read_sf has already terminated input because of an '\n', or
2459 we have hit EOF. */
2460 if (dtp->u.p.sf_seen_eor || dtp->u.p.at_eof)
2461 {
2462 dtp->u.p.sf_seen_eor = 0;
2463 dtp->u.p.at_eof = 0;
2464 break;
2465 }
2466
2467 if (is_internal_unit (dtp))
2468 {
2469 if (is_array_io (dtp))
2470 {
2471 int finished;
2472
2473 record = next_array_record (dtp, dtp->u.p.current_unit->ls,
2474 &finished);
2475
2476 /* Now seek to this record. */
2477 record = record * dtp->u.p.current_unit->recl;
2478 if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
2479 {
2480 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
2481 break;
2482 }
2483 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2484 }
2485 else
2486 {
2487 bytes_left = (int) dtp->u.p.current_unit->bytes_left;
2488 bytes_left = min_off (bytes_left,
2489 file_length (dtp->u.p.current_unit->s)
2490 - stell (dtp->u.p.current_unit->s));
2491 if (sseek (dtp->u.p.current_unit->s,
2492 bytes_left, SEEK_CUR) < 0)
2493 {
2494 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
2495 break;
2496 }
2497 dtp->u.p.current_unit->bytes_left
2498 = dtp->u.p.current_unit->recl;
2499 }
2500 break;
2501 }
2502 else
2503 {
2504 do
2505 {
2506 errno = 0;
2507 cc = fbuf_getc (dtp->u.p.current_unit);
2508 if (cc == EOF)
2509 {
2510 if (errno != 0)
2511 generate_error (&dtp->common, LIBERROR_OS, NULL);
2512 else
2513 hit_eof (dtp);
2514 break;
2515 }
2516
2517 if (is_stream_io (dtp))
2518 dtp->u.p.current_unit->strm_pos++;
2519
2520 p = (char) cc;
2521 }
2522 while (p != '\n');
2523 }
2524 break;
2525 }
2526 }
2527
2528
2529 /* Small utility function to write a record marker, taking care of
2530 byte swapping and of choosing the correct size. */
2531
2532 static int
2533 write_us_marker (st_parameter_dt *dtp, const gfc_offset buf)
2534 {
2535 size_t len;
2536 GFC_INTEGER_4 buf4;
2537 GFC_INTEGER_8 buf8;
2538 char p[sizeof (GFC_INTEGER_8)];
2539
2540 if (compile_options.record_marker == 0)
2541 len = sizeof (GFC_INTEGER_4);
2542 else
2543 len = compile_options.record_marker;
2544
2545 /* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */
2546 if (likely (dtp->u.p.current_unit->flags.convert == GFC_CONVERT_NATIVE))
2547 {
2548 switch (len)
2549 {
2550 case sizeof (GFC_INTEGER_4):
2551 buf4 = buf;
2552 return swrite (dtp->u.p.current_unit->s, &buf4, len);
2553 break;
2554
2555 case sizeof (GFC_INTEGER_8):
2556 buf8 = buf;
2557 return swrite (dtp->u.p.current_unit->s, &buf8, len);
2558 break;
2559
2560 default:
2561 runtime_error ("Illegal value for record marker");
2562 break;
2563 }
2564 }
2565 else
2566 {
2567 switch (len)
2568 {
2569 case sizeof (GFC_INTEGER_4):
2570 buf4 = buf;
2571 reverse_memcpy (p, &buf4, sizeof (GFC_INTEGER_4));
2572 return swrite (dtp->u.p.current_unit->s, p, len);
2573 break;
2574
2575 case sizeof (GFC_INTEGER_8):
2576 buf8 = buf;
2577 reverse_memcpy (p, &buf8, sizeof (GFC_INTEGER_8));
2578 return swrite (dtp->u.p.current_unit->s, p, len);
2579 break;
2580
2581 default:
2582 runtime_error ("Illegal value for record marker");
2583 break;
2584 }
2585 }
2586
2587 }
2588
2589 /* Position to the next (sub)record in write mode for
2590 unformatted sequential files. */
2591
2592 static void
2593 next_record_w_unf (st_parameter_dt *dtp, int next_subrecord)
2594 {
2595 gfc_offset c, m, m_write;
2596 size_t record_marker;
2597
2598 /* Bytes written. */
2599 m = dtp->u.p.current_unit->recl_subrecord
2600 - dtp->u.p.current_unit->bytes_left_subrecord;
2601 c = stell (dtp->u.p.current_unit->s);
2602
2603 /* Write the length tail. If we finish a record containing
2604 subrecords, we write out the negative length. */
2605
2606 if (dtp->u.p.current_unit->continued)
2607 m_write = -m;
2608 else
2609 m_write = m;
2610
2611 if (unlikely (write_us_marker (dtp, m_write) < 0))
2612 goto io_error;
2613
2614 if (compile_options.record_marker == 0)
2615 record_marker = sizeof (GFC_INTEGER_4);
2616 else
2617 record_marker = compile_options.record_marker;
2618
2619 /* Seek to the head and overwrite the bogus length with the real
2620 length. */
2621
2622 if (unlikely (sseek (dtp->u.p.current_unit->s, c - m - record_marker,
2623 SEEK_SET) < 0))
2624 goto io_error;
2625
2626 if (next_subrecord)
2627 m_write = -m;
2628 else
2629 m_write = m;
2630
2631 if (unlikely (write_us_marker (dtp, m_write) < 0))
2632 goto io_error;
2633
2634 /* Seek past the end of the current record. */
2635
2636 if (unlikely (sseek (dtp->u.p.current_unit->s, c + record_marker,
2637 SEEK_SET) < 0))
2638 goto io_error;
2639
2640 return;
2641
2642 io_error:
2643 generate_error (&dtp->common, LIBERROR_OS, NULL);
2644 return;
2645
2646 }
2647
2648
2649 /* Utility function like memset() but operating on streams. Return
2650 value is same as for POSIX write(). */
2651
2652 static ssize_t
2653 sset (stream * s, int c, ssize_t nbyte)
2654 {
2655 static const int WRITE_CHUNK = 256;
2656 char p[WRITE_CHUNK];
2657 ssize_t bytes_left, trans;
2658
2659 if (nbyte < WRITE_CHUNK)
2660 memset (p, c, nbyte);
2661 else
2662 memset (p, c, WRITE_CHUNK);
2663
2664 bytes_left = nbyte;
2665 while (bytes_left > 0)
2666 {
2667 trans = (bytes_left < WRITE_CHUNK) ? bytes_left : WRITE_CHUNK;
2668 trans = swrite (s, p, trans);
2669 if (trans < 0)
2670 return trans;
2671 bytes_left -= trans;
2672 }
2673
2674 return nbyte - bytes_left;
2675 }
2676
2677 /* Position to the next record in write mode. */
2678
2679 static void
2680 next_record_w (st_parameter_dt *dtp, int done)
2681 {
2682 gfc_offset m, record, max_pos;
2683 int length;
2684
2685 /* Zero counters for X- and T-editing. */
2686 max_pos = dtp->u.p.max_pos;
2687 dtp->u.p.max_pos = dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
2688
2689 switch (current_mode (dtp))
2690 {
2691 /* No records in unformatted STREAM I/O. */
2692 case UNFORMATTED_STREAM:
2693 return;
2694
2695 case FORMATTED_DIRECT:
2696 if (dtp->u.p.current_unit->bytes_left == 0)
2697 break;
2698
2699 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
2700 fbuf_flush (dtp->u.p.current_unit, WRITING);
2701 if (sset (dtp->u.p.current_unit->s, ' ',
2702 dtp->u.p.current_unit->bytes_left)
2703 != dtp->u.p.current_unit->bytes_left)
2704 goto io_error;
2705
2706 break;
2707
2708 case UNFORMATTED_DIRECT:
2709 if (dtp->u.p.current_unit->bytes_left > 0)
2710 {
2711 length = (int) dtp->u.p.current_unit->bytes_left;
2712 if (sset (dtp->u.p.current_unit->s, 0, length) != length)
2713 goto io_error;
2714 }
2715 break;
2716
2717 case UNFORMATTED_SEQUENTIAL:
2718 next_record_w_unf (dtp, 0);
2719 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2720 break;
2721
2722 case FORMATTED_STREAM:
2723 case FORMATTED_SEQUENTIAL:
2724
2725 if (is_internal_unit (dtp))
2726 {
2727 if (is_array_io (dtp))
2728 {
2729 int finished;
2730
2731 length = (int) dtp->u.p.current_unit->bytes_left;
2732
2733 /* If the farthest position reached is greater than current
2734 position, adjust the position and set length to pad out
2735 whats left. Otherwise just pad whats left.
2736 (for character array unit) */
2737 m = dtp->u.p.current_unit->recl
2738 - dtp->u.p.current_unit->bytes_left;
2739 if (max_pos > m)
2740 {
2741 length = (int) (max_pos - m);
2742 if (sseek (dtp->u.p.current_unit->s,
2743 length, SEEK_CUR) < 0)
2744 {
2745 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
2746 return;
2747 }
2748 length = (int) (dtp->u.p.current_unit->recl - max_pos);
2749 }
2750
2751 if (sset (dtp->u.p.current_unit->s, ' ', length) != length)
2752 {
2753 generate_error (&dtp->common, LIBERROR_END, NULL);
2754 return;
2755 }
2756
2757 /* Now that the current record has been padded out,
2758 determine where the next record in the array is. */
2759 record = next_array_record (dtp, dtp->u.p.current_unit->ls,
2760 &finished);
2761 if (finished)
2762 dtp->u.p.current_unit->endfile = AT_ENDFILE;
2763
2764 /* Now seek to this record */
2765 record = record * dtp->u.p.current_unit->recl;
2766
2767 if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
2768 {
2769 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
2770 return;
2771 }
2772
2773 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
2774 }
2775 else
2776 {
2777 length = 1;
2778
2779 /* If this is the last call to next_record move to the farthest
2780 position reached and set length to pad out the remainder
2781 of the record. (for character scaler unit) */
2782 if (done)
2783 {
2784 m = dtp->u.p.current_unit->recl
2785 - dtp->u.p.current_unit->bytes_left;
2786 if (max_pos > m)
2787 {
2788 length = (int) (max_pos - m);
2789 if (sseek (dtp->u.p.current_unit->s,
2790 length, SEEK_CUR) < 0)
2791 {
2792 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
2793 return;
2794 }
2795 length = (int) (dtp->u.p.current_unit->recl - max_pos);
2796 }
2797 else
2798 length = (int) dtp->u.p.current_unit->bytes_left;
2799 }
2800
2801 if (sset (dtp->u.p.current_unit->s, ' ', length) != length)
2802 {
2803 generate_error (&dtp->common, LIBERROR_END, NULL);
2804 return;
2805 }
2806 }
2807 }
2808 else
2809 {
2810 #ifdef HAVE_CRLF
2811 const int len = 2;
2812 #else
2813 const int len = 1;
2814 #endif
2815 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
2816 char * p = fbuf_alloc (dtp->u.p.current_unit, len);
2817 if (!p)
2818 goto io_error;
2819 #ifdef HAVE_CRLF
2820 *(p++) = '\r';
2821 #endif
2822 *p = '\n';
2823 if (is_stream_io (dtp))
2824 {
2825 dtp->u.p.current_unit->strm_pos += len;
2826 if (dtp->u.p.current_unit->strm_pos
2827 < file_length (dtp->u.p.current_unit->s))
2828 unit_truncate (dtp->u.p.current_unit,
2829 dtp->u.p.current_unit->strm_pos - 1,
2830 &dtp->common);
2831 }
2832 }
2833
2834 break;
2835
2836 io_error:
2837 generate_error (&dtp->common, LIBERROR_OS, NULL);
2838 break;
2839 }
2840 }
2841
2842 /* Position to the next record, which means moving to the end of the
2843 current record. This can happen under several different
2844 conditions. If the done flag is not set, we get ready to process
2845 the next record. */
2846
2847 void
2848 next_record (st_parameter_dt *dtp, int done)
2849 {
2850 gfc_offset fp; /* File position. */
2851
2852 dtp->u.p.current_unit->read_bad = 0;
2853
2854 if (dtp->u.p.mode == READING)
2855 next_record_r (dtp);
2856 else
2857 next_record_w (dtp, done);
2858
2859 if (!is_stream_io (dtp))
2860 {
2861 /* Keep position up to date for INQUIRE */
2862 if (done)
2863 update_position (dtp->u.p.current_unit);
2864
2865 dtp->u.p.current_unit->current_record = 0;
2866 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
2867 {
2868 fp = stell (dtp->u.p.current_unit->s);
2869 /* Calculate next record, rounding up partial records. */
2870 dtp->u.p.current_unit->last_record =
2871 (fp + dtp->u.p.current_unit->recl - 1) /
2872 dtp->u.p.current_unit->recl;
2873 }
2874 else
2875 dtp->u.p.current_unit->last_record++;
2876 }
2877
2878 if (!done)
2879 pre_position (dtp);
2880
2881 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
2882 }
2883
2884
2885 /* Finalize the current data transfer. For a nonadvancing transfer,
2886 this means advancing to the next record. For internal units close the
2887 stream associated with the unit. */
2888
2889 static void
2890 finalize_transfer (st_parameter_dt *dtp)
2891 {
2892 jmp_buf eof_jump;
2893 GFC_INTEGER_4 cf = dtp->common.flags;
2894
2895 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
2896 *dtp->size = dtp->u.p.size_used;
2897
2898 if (dtp->u.p.eor_condition)
2899 {
2900 generate_error (&dtp->common, LIBERROR_EOR, NULL);
2901 return;
2902 }
2903
2904 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2905 return;
2906
2907 if ((dtp->u.p.ionml != NULL)
2908 && (cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0)
2909 {
2910 if ((cf & IOPARM_DT_NAMELIST_READ_MODE) != 0)
2911 namelist_read (dtp);
2912 else
2913 namelist_write (dtp);
2914 }
2915
2916 dtp->u.p.transfer = NULL;
2917 if (dtp->u.p.current_unit == NULL)
2918 return;
2919
2920 dtp->u.p.eof_jump = &eof_jump;
2921 if (setjmp (eof_jump))
2922 {
2923 generate_error (&dtp->common, LIBERROR_END, NULL);
2924 return;
2925 }
2926
2927 if ((cf & IOPARM_DT_LIST_FORMAT) != 0 && dtp->u.p.mode == READING)
2928 {
2929 finish_list_read (dtp);
2930 return;
2931 }
2932
2933 if (dtp->u.p.mode == WRITING)
2934 dtp->u.p.current_unit->previous_nonadvancing_write
2935 = dtp->u.p.advance_status == ADVANCE_NO;
2936
2937 if (is_stream_io (dtp))
2938 {
2939 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
2940 && dtp->u.p.advance_status != ADVANCE_NO)
2941 next_record (dtp, 1);
2942
2943 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED
2944 && stell (dtp->u.p.current_unit->s) >= dtp->rec)
2945 {
2946 sflush (dtp->u.p.current_unit->s);
2947 }
2948 return;
2949 }
2950
2951 dtp->u.p.current_unit->current_record = 0;
2952
2953 if (!is_internal_unit (dtp) && dtp->u.p.seen_dollar)
2954 {
2955 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
2956 dtp->u.p.seen_dollar = 0;
2957 return;
2958 }
2959
2960 /* For non-advancing I/O, save the current maximum position for use in the
2961 next I/O operation if needed. */
2962 if (dtp->u.p.advance_status == ADVANCE_NO)
2963 {
2964 int bytes_written = (int) (dtp->u.p.current_unit->recl
2965 - dtp->u.p.current_unit->bytes_left);
2966 dtp->u.p.current_unit->saved_pos =
2967 dtp->u.p.max_pos > 0 ? dtp->u.p.max_pos - bytes_written : 0;
2968 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
2969 sflush (dtp->u.p.current_unit->s);
2970 return;
2971 }
2972 else if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
2973 && dtp->u.p.mode == WRITING && !is_internal_unit (dtp))
2974 fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END);
2975
2976 dtp->u.p.current_unit->saved_pos = 0;
2977
2978 next_record (dtp, 1);
2979 }
2980
2981 /* Transfer function for IOLENGTH. It doesn't actually do any
2982 data transfer, it just updates the length counter. */
2983
2984 static void
2985 iolength_transfer (st_parameter_dt *dtp, bt type __attribute__((unused)),
2986 void *dest __attribute__ ((unused)),
2987 int kind __attribute__((unused)),
2988 size_t size, size_t nelems)
2989 {
2990 if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
2991 *dtp->iolength += (GFC_IO_INT) size * nelems;
2992 }
2993
2994
2995 /* Initialize the IOLENGTH data transfer. This function is in essence
2996 a very much simplified version of data_transfer_init(), because it
2997 doesn't have to deal with units at all. */
2998
2999 static void
3000 iolength_transfer_init (st_parameter_dt *dtp)
3001 {
3002 if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
3003 *dtp->iolength = 0;
3004
3005 memset (&dtp->u.p, 0, sizeof (dtp->u.p));
3006
3007 /* Set up the subroutine that will handle the transfers. */
3008
3009 dtp->u.p.transfer = iolength_transfer;
3010 }
3011
3012
3013 /* Library entry point for the IOLENGTH form of the INQUIRE
3014 statement. The IOLENGTH form requires no I/O to be performed, but
3015 it must still be a runtime library call so that we can determine
3016 the iolength for dynamic arrays and such. */
3017
3018 extern void st_iolength (st_parameter_dt *);
3019 export_proto(st_iolength);
3020
3021 void
3022 st_iolength (st_parameter_dt *dtp)
3023 {
3024 library_start (&dtp->common);
3025 iolength_transfer_init (dtp);
3026 }
3027
3028 extern void st_iolength_done (st_parameter_dt *);
3029 export_proto(st_iolength_done);
3030
3031 void
3032 st_iolength_done (st_parameter_dt *dtp __attribute__((unused)))
3033 {
3034 free_ionml (dtp);
3035 library_end ();
3036 }
3037
3038
3039 /* The READ statement. */
3040
3041 extern void st_read (st_parameter_dt *);
3042 export_proto(st_read);
3043
3044 void
3045 st_read (st_parameter_dt *dtp)
3046 {
3047 library_start (&dtp->common);
3048
3049 data_transfer_init (dtp, 1);
3050 }
3051
3052 extern void st_read_done (st_parameter_dt *);
3053 export_proto(st_read_done);
3054
3055 void
3056 st_read_done (st_parameter_dt *dtp)
3057 {
3058 finalize_transfer (dtp);
3059 free_format_data (dtp);
3060 free_ionml (dtp);
3061 if (dtp->u.p.current_unit != NULL)
3062 unlock_unit (dtp->u.p.current_unit);
3063
3064 free_internal_unit (dtp);
3065
3066 library_end ();
3067 }
3068
3069 extern void st_write (st_parameter_dt *);
3070 export_proto(st_write);
3071
3072 void
3073 st_write (st_parameter_dt *dtp)
3074 {
3075 library_start (&dtp->common);
3076 data_transfer_init (dtp, 0);
3077 }
3078
3079 extern void st_write_done (st_parameter_dt *);
3080 export_proto(st_write_done);
3081
3082 void
3083 st_write_done (st_parameter_dt *dtp)
3084 {
3085 finalize_transfer (dtp);
3086
3087 /* Deal with endfile conditions associated with sequential files. */
3088
3089 if (dtp->u.p.current_unit != NULL
3090 && dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
3091 switch (dtp->u.p.current_unit->endfile)
3092 {
3093 case AT_ENDFILE: /* Remain at the endfile record. */
3094 break;
3095
3096 case AFTER_ENDFILE:
3097 dtp->u.p.current_unit->endfile = AT_ENDFILE; /* Just at it now. */
3098 break;
3099
3100 case NO_ENDFILE:
3101 /* Get rid of whatever is after this record. */
3102 if (!is_internal_unit (dtp))
3103 unit_truncate (dtp->u.p.current_unit,
3104 stell (dtp->u.p.current_unit->s),
3105 &dtp->common);
3106 dtp->u.p.current_unit->endfile = AT_ENDFILE;
3107 break;
3108 }
3109
3110 free_format_data (dtp);
3111 free_ionml (dtp);
3112 if (dtp->u.p.current_unit != NULL)
3113 unlock_unit (dtp->u.p.current_unit);
3114
3115 free_internal_unit (dtp);
3116
3117 library_end ();
3118 }
3119
3120
3121 /* F2003: This is a stub for the runtime portion of the WAIT statement. */
3122 void
3123 st_wait (st_parameter_wait *wtp __attribute__((unused)))
3124 {
3125 }
3126
3127
3128 /* Receives the scalar information for namelist objects and stores it
3129 in a linked list of namelist_info types. */
3130
3131 extern void st_set_nml_var (st_parameter_dt *dtp, void *, char *,
3132 GFC_INTEGER_4, gfc_charlen_type, GFC_INTEGER_4);
3133 export_proto(st_set_nml_var);
3134
3135
3136 void
3137 st_set_nml_var (st_parameter_dt *dtp, void * var_addr, char * var_name,
3138 GFC_INTEGER_4 len, gfc_charlen_type string_length,
3139 GFC_INTEGER_4 dtype)
3140 {
3141 namelist_info *t1 = NULL;
3142 namelist_info *nml;
3143 size_t var_name_len = strlen (var_name);
3144
3145 nml = (namelist_info*) get_mem (sizeof (namelist_info));
3146
3147 nml->mem_pos = var_addr;
3148
3149 nml->var_name = (char*) get_mem (var_name_len + 1);
3150 memcpy (nml->var_name, var_name, var_name_len);
3151 nml->var_name[var_name_len] = '\0';
3152
3153 nml->len = (int) len;
3154 nml->string_length = (index_type) string_length;
3155
3156 nml->var_rank = (int) (dtype & GFC_DTYPE_RANK_MASK);
3157 nml->size = (index_type) (dtype >> GFC_DTYPE_SIZE_SHIFT);
3158 nml->type = (bt) ((dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT);
3159
3160 if (nml->var_rank > 0)
3161 {
3162 nml->dim = (descriptor_dimension*)
3163 get_mem (nml->var_rank * sizeof (descriptor_dimension));
3164 nml->ls = (array_loop_spec*)
3165 get_mem (nml->var_rank * sizeof (array_loop_spec));
3166 }
3167 else
3168 {
3169 nml->dim = NULL;
3170 nml->ls = NULL;
3171 }
3172
3173 nml->next = NULL;
3174
3175 if ((dtp->common.flags & IOPARM_DT_IONML_SET) == 0)
3176 {
3177 dtp->common.flags |= IOPARM_DT_IONML_SET;
3178 dtp->u.p.ionml = nml;
3179 }
3180 else
3181 {
3182 for (t1 = dtp->u.p.ionml; t1->next; t1 = t1->next);
3183 t1->next = nml;
3184 }
3185 }
3186
3187 /* Store the dimensional information for the namelist object. */
3188 extern void st_set_nml_var_dim (st_parameter_dt *, GFC_INTEGER_4,
3189 index_type, index_type,
3190 index_type);
3191 export_proto(st_set_nml_var_dim);
3192
3193 void
3194 st_set_nml_var_dim (st_parameter_dt *dtp, GFC_INTEGER_4 n_dim,
3195 index_type stride, index_type lbound,
3196 index_type ubound)
3197 {
3198 namelist_info * nml;
3199 int n;
3200
3201 n = (int)n_dim;
3202
3203 for (nml = dtp->u.p.ionml; nml->next; nml = nml->next);
3204
3205 nml->dim[n].stride = stride;
3206 nml->dim[n].lbound = lbound;
3207 nml->dim[n].ubound = ubound;
3208 }
3209
3210 /* Reverse memcpy - used for byte swapping. */
3211
3212 void reverse_memcpy (void *dest, const void *src, size_t n)
3213 {
3214 char *d, *s;
3215 size_t i;
3216
3217 d = (char *) dest;
3218 s = (char *) src + n - 1;
3219
3220 /* Write with ascending order - this is likely faster
3221 on modern architectures because of write combining. */
3222 for (i=0; i<n; i++)
3223 *(d++) = *(s--);
3224 }
3225
3226
3227 /* Once upon a time, a poor innocent Fortran program was reading a
3228 file, when suddenly it hit the end-of-file (EOF). Unfortunately
3229 the OS doesn't tell whether we're at the EOF or whether we already
3230 went past it. Luckily our hero, libgfortran, keeps track of this.
3231 Call this function when you detect an EOF condition. See Section
3232 9.10.2 in F2003. */
3233
3234 void
3235 hit_eof (st_parameter_dt * dtp)
3236 {
3237 dtp->u.p.current_unit->flags.position = POSITION_APPEND;
3238
3239 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
3240 switch (dtp->u.p.current_unit->endfile)
3241 {
3242 case NO_ENDFILE:
3243 case AT_ENDFILE:
3244 generate_error (&dtp->common, LIBERROR_END, NULL);
3245 if (!is_internal_unit (dtp))
3246 {
3247 dtp->u.p.current_unit->endfile = AFTER_ENDFILE;
3248 dtp->u.p.current_unit->current_record = 0;
3249 }
3250 else
3251 dtp->u.p.current_unit->endfile = AT_ENDFILE;
3252 break;
3253
3254 case AFTER_ENDFILE:
3255 generate_error (&dtp->common, LIBERROR_ENDFILE, NULL);
3256 dtp->u.p.current_unit->current_record = 0;
3257 break;
3258 }
3259 else
3260 {
3261 /* Non-sequential files don't have an ENDFILE record, so we
3262 can't be at AFTER_ENDFILE. */
3263 dtp->u.p.current_unit->endfile = AT_ENDFILE;
3264 generate_error (&dtp->common, LIBERROR_END, NULL);
3265 dtp->u.p.current_unit->current_record = 0;
3266 }
3267 }