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