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