Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libgfortran / io / transfer.c
blob7696643deb904c852b856ce3735c194965d879ce
1 /* Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
3 Namelist transfer functions contributed by Paul Thomas
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
19 executable.)
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with Libgfortran; see the file COPYING. If not, write to
28 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA. */
32 /* transfer.c -- Top level handling of data transfer statements. */
34 #include "config.h"
35 #include <string.h>
36 #include <assert.h>
37 #include "libgfortran.h"
38 #include "io.h"
41 /* Calling conventions: Data transfer statements are unlike other
42 library calls in that they extend over several calls.
44 The first call is always a call to st_read() or st_write(). These
45 subroutines return no status unless a namelist read or write is
46 being done, in which case there is the usual status. No further
47 calls are necessary in this case.
49 For other sorts of data transfer, there are zero or more data
50 transfer statement that depend on the format of the data transfer
51 statement.
53 transfer_integer
54 transfer_logical
55 transfer_character
56 transfer_real
57 transfer_complex
59 These subroutines do not return status.
61 The last call is a call to st_[read|write]_done(). While
62 something can easily go wrong with the initial st_read() or
63 st_write(), an error inhibits any data from actually being
64 transferred. */
66 extern void transfer_integer (st_parameter_dt *, void *, int);
67 export_proto(transfer_integer);
69 extern void transfer_real (st_parameter_dt *, void *, int);
70 export_proto(transfer_real);
72 extern void transfer_logical (st_parameter_dt *, void *, int);
73 export_proto(transfer_logical);
75 extern void transfer_character (st_parameter_dt *, void *, int);
76 export_proto(transfer_character);
78 extern void transfer_complex (st_parameter_dt *, void *, int);
79 export_proto(transfer_complex);
81 extern void transfer_array (st_parameter_dt *, gfc_array_char *, int,
82 gfc_charlen_type);
83 export_proto(transfer_array);
85 static const st_option advance_opt[] = {
86 {"yes", ADVANCE_YES},
87 {"no", ADVANCE_NO},
88 {NULL, 0}
92 typedef enum
93 { FORMATTED_SEQUENTIAL, UNFORMATTED_SEQUENTIAL,
94 FORMATTED_DIRECT, UNFORMATTED_DIRECT
96 file_mode;
99 static file_mode
100 current_mode (st_parameter_dt *dtp)
102 file_mode m;
104 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
106 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
107 FORMATTED_DIRECT : UNFORMATTED_DIRECT;
109 else
111 m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ?
112 FORMATTED_SEQUENTIAL : UNFORMATTED_SEQUENTIAL;
115 return m;
119 /* Mid level data transfer statements. These subroutines do reading
120 and writing in the style of salloc_r()/salloc_w() within the
121 current record. */
123 /* When reading sequential formatted records we have a problem. We
124 don't know how long the line is until we read the trailing newline,
125 and we don't want to read too much. If we read too much, we might
126 have to do a physical seek backwards depending on how much data is
127 present, and devices like terminals aren't seekable and would cause
128 an I/O error.
130 Given this, the solution is to read a byte at a time, stopping if
131 we hit the newline. For small locations, we use a static buffer.
132 For larger allocations, we are forced to allocate memory on the
133 heap. Hopefully this won't happen very often. */
135 static char *
136 read_sf (st_parameter_dt *dtp, int *length)
138 char *base, *p, *q;
139 int n, readlen, crlf;
140 gfc_offset pos;
142 if (*length > SCRATCH_SIZE)
143 dtp->u.p.line_buffer = get_mem (*length);
144 p = base = dtp->u.p.line_buffer;
146 /* If we have seen an eor previously, return a length of 0. The
147 caller is responsible for correctly padding the input field. */
148 if (dtp->u.p.sf_seen_eor)
150 *length = 0;
151 return base;
154 readlen = 1;
155 n = 0;
159 if (is_internal_unit (dtp))
161 /* readlen may be modified inside salloc_r if
162 is_internal_unit (dtp) is true. */
163 readlen = 1;
166 q = salloc_r (dtp->u.p.current_unit->s, &readlen);
167 if (q == NULL)
168 break;
170 /* If we have a line without a terminating \n, drop through to
171 EOR below. */
172 if (readlen < 1 && n == 0)
174 generate_error (&dtp->common, ERROR_END, NULL);
175 return NULL;
178 if (readlen < 1 || *q == '\n' || *q == '\r')
180 /* Unexpected end of line. */
182 /* If we see an EOR during non-advancing I/O, we need to skip
183 the rest of the I/O statement. Set the corresponding flag. */
184 if (dtp->u.p.advance_status == ADVANCE_NO || dtp->u.p.seen_dollar)
185 dtp->u.p.eor_condition = 1;
187 crlf = 0;
188 /* If we encounter a CR, it might be a CRLF. */
189 if (*q == '\r') /* Probably a CRLF */
191 readlen = 1;
192 pos = stream_offset (dtp->u.p.current_unit->s);
193 q = salloc_r (dtp->u.p.current_unit->s, &readlen);
194 if (*q != '\n' && readlen == 1) /* Not a CRLF after all. */
195 sseek (dtp->u.p.current_unit->s, pos);
196 else
197 crlf = 1;
200 /* Without padding, terminate the I/O statement without assigning
201 the value. With padding, the value still needs to be assigned,
202 so we can just continue with a short read. */
203 if (dtp->u.p.current_unit->flags.pad == PAD_NO)
205 generate_error (&dtp->common, ERROR_EOR, NULL);
206 return NULL;
209 *length = n;
210 dtp->u.p.sf_seen_eor = (crlf ? 2 : 1);
211 break;
213 /* Short circuit the read if a comma is found during numeric input.
214 The flag is set to zero during character reads so that commas in
215 strings are not ignored */
216 if (*q == ',')
217 if (dtp->u.p.sf_read_comma == 1)
219 notify_std (GFC_STD_GNU, "Comma in formatted numeric read.");
220 *length = n;
221 break;
224 n++;
225 *p++ = *q;
226 dtp->u.p.sf_seen_eor = 0;
228 while (n < *length);
229 dtp->u.p.current_unit->bytes_left -= *length;
231 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
232 *dtp->size += *length;
234 return base;
238 /* Function for reading the next couple of bytes from the current
239 file, advancing the current position. We return a pointer to a
240 buffer containing the bytes. We return NULL on end of record or
241 end of file.
243 If the read is short, then it is because the current record does not
244 have enough data to satisfy the read request and the file was
245 opened with PAD=YES. The caller must assume tailing spaces for
246 short reads. */
248 void *
249 read_block (st_parameter_dt *dtp, int *length)
251 char *source;
252 int nread;
254 if (dtp->u.p.current_unit->bytes_left < *length)
256 if (dtp->u.p.current_unit->flags.pad == PAD_NO)
258 generate_error (&dtp->common, ERROR_EOR, NULL);
259 /* Not enough data left. */
260 return NULL;
263 *length = dtp->u.p.current_unit->bytes_left;
266 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
267 dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
268 return read_sf (dtp, length); /* Special case. */
270 dtp->u.p.current_unit->bytes_left -= *length;
272 nread = *length;
273 source = salloc_r (dtp->u.p.current_unit->s, &nread);
275 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
276 *dtp->size += nread;
278 if (nread != *length)
279 { /* Short read, this shouldn't happen. */
280 if (dtp->u.p.current_unit->flags.pad == PAD_YES)
281 *length = nread;
282 else
284 generate_error (&dtp->common, ERROR_EOR, NULL);
285 source = NULL;
289 return source;
293 /* Reads a block directly into application data space. */
295 static void
296 read_block_direct (st_parameter_dt *dtp, void *buf, size_t *nbytes)
298 int *length;
299 void *data;
300 size_t nread;
302 if (dtp->u.p.current_unit->bytes_left < *nbytes)
304 if (dtp->u.p.current_unit->flags.pad == PAD_NO)
306 /* Not enough data left. */
307 generate_error (&dtp->common, ERROR_EOR, NULL);
308 return;
311 *nbytes = dtp->u.p.current_unit->bytes_left;
314 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
315 dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
317 length = (int *) nbytes;
318 data = read_sf (dtp, length); /* Special case. */
319 memcpy (buf, data, (size_t) *length);
320 return;
323 dtp->u.p.current_unit->bytes_left -= *nbytes;
325 nread = *nbytes;
326 if (sread (dtp->u.p.current_unit->s, buf, &nread) != 0)
328 generate_error (&dtp->common, ERROR_OS, NULL);
329 return;
332 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
333 *dtp->size += (GFC_INTEGER_4) nread;
335 if (nread != *nbytes)
336 { /* Short read, e.g. if we hit EOF. */
337 if (dtp->u.p.current_unit->flags.pad == PAD_YES)
339 memset (((char *) buf) + nread, ' ', *nbytes - nread);
340 *nbytes = nread;
342 else
343 generate_error (&dtp->common, ERROR_EOR, NULL);
348 /* Function for writing a block of bytes to the current file at the
349 current position, advancing the file pointer. We are given a length
350 and return a pointer to a buffer that the caller must (completely)
351 fill in. Returns NULL on error. */
353 void *
354 write_block (st_parameter_dt *dtp, int length)
356 char *dest;
358 if (dtp->u.p.current_unit->bytes_left < length)
360 generate_error (&dtp->common, ERROR_EOR, NULL);
361 return NULL;
364 dtp->u.p.current_unit->bytes_left -= (gfc_offset) length;
365 dest = salloc_w (dtp->u.p.current_unit->s, &length);
367 if (dest == NULL)
369 generate_error (&dtp->common, ERROR_END, NULL);
370 return NULL;
373 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
374 *dtp->size += length;
376 return dest;
380 /* Writes a block directly without necessarily allocating space in a
381 buffer. */
383 static void
384 write_block_direct (st_parameter_dt *dtp, void *buf, size_t *nbytes)
386 if (dtp->u.p.current_unit->bytes_left < *nbytes)
387 generate_error (&dtp->common, ERROR_EOR, NULL);
389 dtp->u.p.current_unit->bytes_left -= (gfc_offset) *nbytes;
391 if (swrite (dtp->u.p.current_unit->s, buf, nbytes) != 0)
392 generate_error (&dtp->common, ERROR_OS, NULL);
394 if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0)
395 *dtp->size += (GFC_INTEGER_4) *nbytes;
399 /* Master function for unformatted reads. */
401 static void
402 unformatted_read (st_parameter_dt *dtp, bt type,
403 void *dest, int kind,
404 size_t size, size_t nelems)
406 /* Currently, character implies size=1. */
407 if (dtp->u.p.current_unit->flags.convert == CONVERT_NATIVE
408 || size == 1 || type == BT_CHARACTER)
410 size *= nelems;
411 read_block_direct (dtp, dest, &size);
413 else
415 char buffer[16];
416 char *p;
417 size_t i, sz;
419 /* Break up complex into its constituent reals. */
420 if (type == BT_COMPLEX)
422 nelems *= 2;
423 size /= 2;
425 p = dest;
427 /* By now, all complex variables have been split into their
428 constituent reals. For types with padding, we only need to
429 read kind bytes. We don't care about the contents
430 of the padding. */
432 sz = kind;
433 for (i=0; i<nelems; i++)
435 read_block_direct (dtp, buffer, &sz);
436 reverse_memcpy (p, buffer, sz);
437 p += size;
443 /* Master function for unformatted writes. */
445 static void
446 unformatted_write (st_parameter_dt *dtp, bt type,
447 void *source, int kind,
448 size_t size, size_t nelems)
450 if (dtp->u.p.current_unit->flags.convert == CONVERT_NATIVE ||
451 size == 1 || type == BT_CHARACTER)
453 size *= nelems;
455 write_block_direct (dtp, source, &size);
457 else
459 char buffer[16];
460 char *p;
461 size_t i, sz;
463 /* Break up complex into its constituent reals. */
464 if (type == BT_COMPLEX)
466 nelems *= 2;
467 size /= 2;
470 p = source;
472 /* By now, all complex variables have been split into their
473 constituent reals. For types with padding, we only need to
474 read kind bytes. We don't care about the contents
475 of the padding. */
477 sz = kind;
478 for (i=0; i<nelems; i++)
480 reverse_memcpy(buffer, p, size);
481 p+= size;
482 write_block_direct (dtp, buffer, &sz);
488 /* Return a pointer to the name of a type. */
490 const char *
491 type_name (bt type)
493 const char *p;
495 switch (type)
497 case BT_INTEGER:
498 p = "INTEGER";
499 break;
500 case BT_LOGICAL:
501 p = "LOGICAL";
502 break;
503 case BT_CHARACTER:
504 p = "CHARACTER";
505 break;
506 case BT_REAL:
507 p = "REAL";
508 break;
509 case BT_COMPLEX:
510 p = "COMPLEX";
511 break;
512 default:
513 internal_error (NULL, "type_name(): Bad type");
516 return p;
520 /* Write a constant string to the output.
521 This is complicated because the string can have doubled delimiters
522 in it. The length in the format node is the true length. */
524 static void
525 write_constant_string (st_parameter_dt *dtp, const fnode *f)
527 char c, delimiter, *p, *q;
528 int length;
530 length = f->u.string.length;
531 if (length == 0)
532 return;
534 p = write_block (dtp, length);
535 if (p == NULL)
536 return;
538 q = f->u.string.p;
539 delimiter = q[-1];
541 for (; length > 0; length--)
543 c = *p++ = *q++;
544 if (c == delimiter && c != 'H' && c != 'h')
545 q++; /* Skip the doubled delimiter. */
550 /* Given actual and expected types in a formatted data transfer, make
551 sure they agree. If not, an error message is generated. Returns
552 nonzero if something went wrong. */
554 static int
555 require_type (st_parameter_dt *dtp, bt expected, bt actual, const fnode *f)
557 char buffer[100];
559 if (actual == expected)
560 return 0;
562 st_sprintf (buffer, "Expected %s for item %d in formatted transfer, got %s",
563 type_name (expected), dtp->u.p.item_count, type_name (actual));
565 format_error (dtp, f, buffer);
566 return 1;
570 /* This subroutine is the main loop for a formatted data transfer
571 statement. It would be natural to implement this as a coroutine
572 with the user program, but C makes that awkward. We loop,
573 processesing format elements. When we actually have to transfer
574 data instead of just setting flags, we return control to the user
575 program which calls a subroutine that supplies the address and type
576 of the next element, then comes back here to process it. */
578 static void
579 formatted_transfer_scalar (st_parameter_dt *dtp, bt type, void *p, int len,
580 size_t size)
582 char scratch[SCRATCH_SIZE];
583 int pos, bytes_used;
584 const fnode *f;
585 format_token t;
586 int n;
587 int consume_data_flag;
589 /* Change a complex data item into a pair of reals. */
591 n = (p == NULL) ? 0 : ((type != BT_COMPLEX) ? 1 : 2);
592 if (type == BT_COMPLEX)
594 type = BT_REAL;
595 size /= 2;
598 /* If there's an EOR condition, we simulate finalizing the transfer
599 by doing nothing. */
600 if (dtp->u.p.eor_condition)
601 return;
603 /* Set this flag so that commas in reads cause the read to complete before
604 the entire field has been read. The next read field will start right after
605 the comma in the stream. (Set to 0 for character reads). */
606 dtp->u.p.sf_read_comma = 1;
608 dtp->u.p.line_buffer = scratch;
609 for (;;)
611 /* If reversion has occurred and there is another real data item,
612 then we have to move to the next record. */
613 if (dtp->u.p.reversion_flag && n > 0)
615 dtp->u.p.reversion_flag = 0;
616 next_record (dtp, 0);
619 consume_data_flag = 1 ;
620 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
621 break;
623 f = next_format (dtp);
624 if (f == NULL)
625 return; /* No data descriptors left (already raised). */
627 /* Now discharge T, TR and X movements to the right. This is delayed
628 until a data producing format to suppress trailing spaces. */
630 t = f->format;
631 if (dtp->u.p.mode == WRITING && dtp->u.p.skips != 0
632 && ((n>0 && ( t == FMT_I || t == FMT_B || t == FMT_O
633 || t == FMT_Z || t == FMT_F || t == FMT_E
634 || t == FMT_EN || t == FMT_ES || t == FMT_G
635 || t == FMT_L || t == FMT_A || t == FMT_D))
636 || t == FMT_STRING))
638 if (dtp->u.p.skips > 0)
640 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
641 dtp->u.p.max_pos = (int)(dtp->u.p.current_unit->recl
642 - dtp->u.p.current_unit->bytes_left);
644 if (dtp->u.p.skips < 0)
646 move_pos_offset (dtp->u.p.current_unit->s, dtp->u.p.skips);
647 dtp->u.p.current_unit->bytes_left -= (gfc_offset) dtp->u.p.skips;
649 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
652 bytes_used = (int)(dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left);
654 switch (t)
656 case FMT_I:
657 if (n == 0)
658 goto need_data;
659 if (require_type (dtp, BT_INTEGER, type, f))
660 return;
662 if (dtp->u.p.mode == READING)
663 read_decimal (dtp, f, p, len);
664 else
665 write_i (dtp, f, p, len);
667 break;
669 case FMT_B:
670 if (n == 0)
671 goto need_data;
672 if (require_type (dtp, BT_INTEGER, type, f))
673 return;
675 if (dtp->u.p.mode == READING)
676 read_radix (dtp, f, p, len, 2);
677 else
678 write_b (dtp, f, p, len);
680 break;
682 case FMT_O:
683 if (n == 0)
684 goto need_data;
686 if (dtp->u.p.mode == READING)
687 read_radix (dtp, f, p, len, 8);
688 else
689 write_o (dtp, f, p, len);
691 break;
693 case FMT_Z:
694 if (n == 0)
695 goto need_data;
697 if (dtp->u.p.mode == READING)
698 read_radix (dtp, f, p, len, 16);
699 else
700 write_z (dtp, f, p, len);
702 break;
704 case FMT_A:
705 if (n == 0)
706 goto need_data;
708 if (dtp->u.p.mode == READING)
709 read_a (dtp, f, p, len);
710 else
711 write_a (dtp, f, p, len);
713 break;
715 case FMT_L:
716 if (n == 0)
717 goto need_data;
719 if (dtp->u.p.mode == READING)
720 read_l (dtp, f, p, len);
721 else
722 write_l (dtp, f, p, len);
724 break;
726 case FMT_D:
727 if (n == 0)
728 goto need_data;
729 if (require_type (dtp, BT_REAL, type, f))
730 return;
732 if (dtp->u.p.mode == READING)
733 read_f (dtp, f, p, len);
734 else
735 write_d (dtp, f, p, len);
737 break;
739 case FMT_E:
740 if (n == 0)
741 goto need_data;
742 if (require_type (dtp, BT_REAL, type, f))
743 return;
745 if (dtp->u.p.mode == READING)
746 read_f (dtp, f, p, len);
747 else
748 write_e (dtp, f, p, len);
749 break;
751 case FMT_EN:
752 if (n == 0)
753 goto need_data;
754 if (require_type (dtp, BT_REAL, type, f))
755 return;
757 if (dtp->u.p.mode == READING)
758 read_f (dtp, f, p, len);
759 else
760 write_en (dtp, f, p, len);
762 break;
764 case FMT_ES:
765 if (n == 0)
766 goto need_data;
767 if (require_type (dtp, BT_REAL, type, f))
768 return;
770 if (dtp->u.p.mode == READING)
771 read_f (dtp, f, p, len);
772 else
773 write_es (dtp, f, p, len);
775 break;
777 case FMT_F:
778 if (n == 0)
779 goto need_data;
780 if (require_type (dtp, BT_REAL, type, f))
781 return;
783 if (dtp->u.p.mode == READING)
784 read_f (dtp, f, p, len);
785 else
786 write_f (dtp, f, p, len);
788 break;
790 case FMT_G:
791 if (n == 0)
792 goto need_data;
793 if (dtp->u.p.mode == READING)
794 switch (type)
796 case BT_INTEGER:
797 read_decimal (dtp, f, p, len);
798 break;
799 case BT_LOGICAL:
800 read_l (dtp, f, p, len);
801 break;
802 case BT_CHARACTER:
803 read_a (dtp, f, p, len);
804 break;
805 case BT_REAL:
806 read_f (dtp, f, p, len);
807 break;
808 default:
809 goto bad_type;
811 else
812 switch (type)
814 case BT_INTEGER:
815 write_i (dtp, f, p, len);
816 break;
817 case BT_LOGICAL:
818 write_l (dtp, f, p, len);
819 break;
820 case BT_CHARACTER:
821 write_a (dtp, f, p, len);
822 break;
823 case BT_REAL:
824 write_d (dtp, f, p, len);
825 break;
826 default:
827 bad_type:
828 internal_error (&dtp->common,
829 "formatted_transfer(): Bad type");
832 break;
834 case FMT_STRING:
835 consume_data_flag = 0 ;
836 if (dtp->u.p.mode == READING)
838 format_error (dtp, f, "Constant string in input format");
839 return;
841 write_constant_string (dtp, f);
842 break;
844 /* Format codes that don't transfer data. */
845 case FMT_X:
846 case FMT_TR:
847 consume_data_flag = 0 ;
849 pos = bytes_used + f->u.n + dtp->u.p.skips;
850 dtp->u.p.skips = f->u.n + dtp->u.p.skips;
851 dtp->u.p.pending_spaces = pos - dtp->u.p.max_pos;
853 /* Writes occur just before the switch on f->format, above, so
854 that trailing blanks are suppressed, unless we are doing a
855 non-advancing write in which case we want to output the blanks
856 now. */
857 if (dtp->u.p.mode == WRITING
858 && dtp->u.p.advance_status == ADVANCE_NO)
860 write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces);
861 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
863 if (dtp->u.p.mode == READING)
864 read_x (dtp, f->u.n);
866 break;
868 case FMT_TL:
869 case FMT_T:
870 if (f->format == FMT_TL)
871 pos = bytes_used - f->u.n;
872 else /* FMT_T */
874 consume_data_flag = 0;
875 pos = f->u.n - 1;
878 /* Standard 10.6.1.1: excessive left tabbing is reset to the
879 left tab limit. We do not check if the position has gone
880 beyond the end of record because a subsequent tab could
881 bring us back again. */
882 pos = pos < 0 ? 0 : pos;
884 dtp->u.p.skips = dtp->u.p.skips + pos - bytes_used;
885 dtp->u.p.pending_spaces = dtp->u.p.pending_spaces
886 + pos - dtp->u.p.max_pos;
888 if (dtp->u.p.skips == 0)
889 break;
891 /* Writes occur just before the switch on f->format, above, so that
892 trailing blanks are suppressed. */
893 if (dtp->u.p.mode == READING)
895 /* Adjust everything for end-of-record condition */
896 if (dtp->u.p.sf_seen_eor && !is_internal_unit (dtp))
898 if (dtp->u.p.sf_seen_eor == 2)
900 /* The EOR was a CRLF (two bytes wide). */
901 dtp->u.p.current_unit->bytes_left -= 2;
902 dtp->u.p.skips -= 2;
904 else
906 /* The EOR marker was only one byte wide. */
907 dtp->u.p.current_unit->bytes_left--;
908 dtp->u.p.skips--;
910 bytes_used = pos;
911 dtp->u.p.sf_seen_eor = 0;
913 if (dtp->u.p.skips < 0)
915 move_pos_offset (dtp->u.p.current_unit->s, dtp->u.p.skips);
916 dtp->u.p.current_unit->bytes_left
917 -= (gfc_offset) dtp->u.p.skips;
918 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
920 else
921 read_x (dtp, dtp->u.p.skips);
924 break;
926 case FMT_S:
927 consume_data_flag = 0 ;
928 dtp->u.p.sign_status = SIGN_S;
929 break;
931 case FMT_SS:
932 consume_data_flag = 0 ;
933 dtp->u.p.sign_status = SIGN_SS;
934 break;
936 case FMT_SP:
937 consume_data_flag = 0 ;
938 dtp->u.p.sign_status = SIGN_SP;
939 break;
941 case FMT_BN:
942 consume_data_flag = 0 ;
943 dtp->u.p.blank_status = BLANK_NULL;
944 break;
946 case FMT_BZ:
947 consume_data_flag = 0 ;
948 dtp->u.p.blank_status = BLANK_ZERO;
949 break;
951 case FMT_P:
952 consume_data_flag = 0 ;
953 dtp->u.p.scale_factor = f->u.k;
954 break;
956 case FMT_DOLLAR:
957 consume_data_flag = 0 ;
958 dtp->u.p.seen_dollar = 1;
959 break;
961 case FMT_SLASH:
962 consume_data_flag = 0 ;
963 dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
964 next_record (dtp, 0);
965 break;
967 case FMT_COLON:
968 /* A colon descriptor causes us to exit this loop (in
969 particular preventing another / descriptor from being
970 processed) unless there is another data item to be
971 transferred. */
972 consume_data_flag = 0 ;
973 if (n == 0)
974 return;
975 break;
977 default:
978 internal_error (&dtp->common, "Bad format node");
981 /* Free a buffer that we had to allocate during a sequential
982 formatted read of a block that was larger than the static
983 buffer. */
985 if (dtp->u.p.line_buffer != scratch)
987 free_mem (dtp->u.p.line_buffer);
988 dtp->u.p.line_buffer = scratch;
991 /* Adjust the item count and data pointer. */
993 if ((consume_data_flag > 0) && (n > 0))
995 n--;
996 p = ((char *) p) + size;
999 if (dtp->u.p.mode == READING)
1000 dtp->u.p.skips = 0;
1002 pos = (int)(dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left);
1003 dtp->u.p.max_pos = (dtp->u.p.max_pos > pos) ? dtp->u.p.max_pos : pos;
1007 return;
1009 /* Come here when we need a data descriptor but don't have one. We
1010 push the current format node back onto the input, then return and
1011 let the user program call us back with the data. */
1012 need_data:
1013 unget_format (dtp, f);
1016 static void
1017 formatted_transfer (st_parameter_dt *dtp, bt type, void *p, int kind,
1018 size_t size, size_t nelems)
1020 size_t elem;
1021 char *tmp;
1023 tmp = (char *) p;
1025 /* Big loop over all the elements. */
1026 for (elem = 0; elem < nelems; elem++)
1028 dtp->u.p.item_count++;
1029 formatted_transfer_scalar (dtp, type, tmp + size*elem, kind, size);
1035 /* Data transfer entry points. The type of the data entity is
1036 implicit in the subroutine call. This prevents us from having to
1037 share a common enum with the compiler. */
1039 void
1040 transfer_integer (st_parameter_dt *dtp, void *p, int kind)
1042 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1043 return;
1044 dtp->u.p.transfer (dtp, BT_INTEGER, p, kind, kind, 1);
1048 void
1049 transfer_real (st_parameter_dt *dtp, void *p, int kind)
1051 size_t size;
1052 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1053 return;
1054 size = size_from_real_kind (kind);
1055 dtp->u.p.transfer (dtp, BT_REAL, p, kind, size, 1);
1059 void
1060 transfer_logical (st_parameter_dt *dtp, void *p, int kind)
1062 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1063 return;
1064 dtp->u.p.transfer (dtp, BT_LOGICAL, p, kind, kind, 1);
1068 void
1069 transfer_character (st_parameter_dt *dtp, void *p, int len)
1071 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1072 return;
1073 /* Currently we support only 1 byte chars, and the library is a bit
1074 confused of character kind vs. length, so we kludge it by setting
1075 kind = length. */
1076 dtp->u.p.transfer (dtp, BT_CHARACTER, p, len, len, 1);
1080 void
1081 transfer_complex (st_parameter_dt *dtp, void *p, int kind)
1083 size_t size;
1084 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1085 return;
1086 size = size_from_complex_kind (kind);
1087 dtp->u.p.transfer (dtp, BT_COMPLEX, p, kind, size, 1);
1091 void
1092 transfer_array (st_parameter_dt *dtp, gfc_array_char *desc, int kind,
1093 gfc_charlen_type charlen)
1095 index_type count[GFC_MAX_DIMENSIONS];
1096 index_type extent[GFC_MAX_DIMENSIONS];
1097 index_type stride[GFC_MAX_DIMENSIONS];
1098 index_type stride0, rank, size, type, n;
1099 size_t tsize;
1100 char *data;
1101 bt iotype;
1103 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1104 return;
1106 type = GFC_DESCRIPTOR_TYPE (desc);
1107 size = GFC_DESCRIPTOR_SIZE (desc);
1109 /* FIXME: What a kludge: Array descriptors and the IO library use
1110 different enums for types. */
1111 switch (type)
1113 case GFC_DTYPE_UNKNOWN:
1114 iotype = BT_NULL; /* Is this correct? */
1115 break;
1116 case GFC_DTYPE_INTEGER:
1117 iotype = BT_INTEGER;
1118 break;
1119 case GFC_DTYPE_LOGICAL:
1120 iotype = BT_LOGICAL;
1121 break;
1122 case GFC_DTYPE_REAL:
1123 iotype = BT_REAL;
1124 break;
1125 case GFC_DTYPE_COMPLEX:
1126 iotype = BT_COMPLEX;
1127 break;
1128 case GFC_DTYPE_CHARACTER:
1129 iotype = BT_CHARACTER;
1130 /* FIXME: Currently dtype contains the charlen, which is
1131 clobbered if charlen > 2**24. That's why we use a separate
1132 argument for the charlen. However, if we want to support
1133 non-8-bit charsets we need to fix dtype to contain
1134 sizeof(chartype) and fix the code below. */
1135 size = charlen;
1136 kind = charlen;
1137 break;
1138 case GFC_DTYPE_DERIVED:
1139 internal_error (&dtp->common,
1140 "Derived type I/O should have been handled via the frontend.");
1141 break;
1142 default:
1143 internal_error (&dtp->common, "transfer_array(): Bad type");
1146 if (desc->dim[0].stride == 0)
1147 desc->dim[0].stride = 1;
1149 rank = GFC_DESCRIPTOR_RANK (desc);
1150 for (n = 0; n < rank; n++)
1152 count[n] = 0;
1153 stride[n] = desc->dim[n].stride;
1154 extent[n] = desc->dim[n].ubound + 1 - desc->dim[n].lbound;
1156 /* If the extent of even one dimension is zero, then the entire
1157 array section contains zero elements, so we return. */
1158 if (extent[n] == 0)
1159 return;
1162 stride0 = stride[0];
1164 /* If the innermost dimension has stride 1, we can do the transfer
1165 in contiguous chunks. */
1166 if (stride0 == 1)
1167 tsize = extent[0];
1168 else
1169 tsize = 1;
1171 data = GFC_DESCRIPTOR_DATA (desc);
1173 while (data)
1175 dtp->u.p.transfer (dtp, iotype, data, kind, size, tsize);
1176 data += stride0 * size * tsize;
1177 count[0] += tsize;
1178 n = 0;
1179 while (count[n] == extent[n])
1181 count[n] = 0;
1182 data -= stride[n] * extent[n] * size;
1183 n++;
1184 if (n == rank)
1186 data = NULL;
1187 break;
1189 else
1191 count[n]++;
1192 data += stride[n] * size;
1199 /* Preposition a sequential unformatted file while reading. */
1201 static void
1202 us_read (st_parameter_dt *dtp)
1204 char *p;
1205 int n;
1206 gfc_offset i;
1208 n = sizeof (gfc_offset);
1209 p = salloc_r (dtp->u.p.current_unit->s, &n);
1211 if (n == 0)
1212 return; /* end of file */
1214 if (p == NULL || n != sizeof (gfc_offset))
1216 generate_error (&dtp->common, ERROR_BAD_US, NULL);
1217 return;
1220 /* Only CONVERT_NATIVE and CONVERT_SWAP are valid here. */
1221 if (dtp->u.p.current_unit->flags.convert == CONVERT_NATIVE)
1222 memcpy (&i, p, sizeof (gfc_offset));
1223 else
1224 reverse_memcpy (&i, p, sizeof (gfc_offset));
1226 dtp->u.p.current_unit->bytes_left = i;
1230 /* Preposition a sequential unformatted file while writing. This
1231 amount to writing a bogus length that will be filled in later. */
1233 static void
1234 us_write (st_parameter_dt *dtp)
1236 char *p;
1237 int length;
1239 length = sizeof (gfc_offset);
1240 p = salloc_w (dtp->u.p.current_unit->s, &length);
1242 if (p == NULL)
1244 generate_error (&dtp->common, ERROR_OS, NULL);
1245 return;
1248 memset (p, '\0', sizeof (gfc_offset)); /* Bogus value for now. */
1249 if (sfree (dtp->u.p.current_unit->s) == FAILURE)
1250 generate_error (&dtp->common, ERROR_OS, NULL);
1252 /* For sequential unformatted, we write until we have more bytes than
1253 can fit in the record markers. If disk space runs out first, it will
1254 error on the write. */
1255 dtp->u.p.current_unit->recl = max_offset;
1257 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
1261 /* Position to the next record prior to transfer. We are assumed to
1262 be before the next record. We also calculate the bytes in the next
1263 record. */
1265 static void
1266 pre_position (st_parameter_dt *dtp)
1268 if (dtp->u.p.current_unit->current_record)
1269 return; /* Already positioned. */
1271 switch (current_mode (dtp))
1273 case UNFORMATTED_SEQUENTIAL:
1274 if (dtp->u.p.mode == READING)
1275 us_read (dtp);
1276 else
1277 us_write (dtp);
1279 break;
1281 case FORMATTED_SEQUENTIAL:
1282 case FORMATTED_DIRECT:
1283 case UNFORMATTED_DIRECT:
1284 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
1285 break;
1288 dtp->u.p.current_unit->current_record = 1;
1292 /* Initialize things for a data transfer. This code is common for
1293 both reading and writing. */
1295 static void
1296 data_transfer_init (st_parameter_dt *dtp, int read_flag)
1298 unit_flags u_flags; /* Used for creating a unit if needed. */
1299 GFC_INTEGER_4 cf = dtp->common.flags;
1300 namelist_info *ionml;
1302 ionml = ((cf & IOPARM_DT_IONML_SET) != 0) ? dtp->u.p.ionml : NULL;
1303 memset (&dtp->u.p, 0, sizeof (dtp->u.p));
1304 dtp->u.p.ionml = ionml;
1305 dtp->u.p.mode = read_flag ? READING : WRITING;
1307 if ((cf & IOPARM_DT_HAS_SIZE) != 0)
1308 *dtp->size = 0; /* Initialize the count. */
1310 dtp->u.p.current_unit = get_unit (dtp, 1);
1311 if (dtp->u.p.current_unit->s == NULL)
1312 { /* Open the unit with some default flags. */
1313 st_parameter_open opp;
1314 if (dtp->common.unit < 0)
1316 close_unit (dtp->u.p.current_unit);
1317 dtp->u.p.current_unit = NULL;
1318 generate_error (&dtp->common, ERROR_BAD_OPTION,
1319 "Bad unit number in OPEN statement");
1320 return;
1322 memset (&u_flags, '\0', sizeof (u_flags));
1323 u_flags.access = ACCESS_SEQUENTIAL;
1324 u_flags.action = ACTION_READWRITE;
1326 /* Is it unformatted? */
1327 if (!(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT
1328 | IOPARM_DT_IONML_SET)))
1329 u_flags.form = FORM_UNFORMATTED;
1330 else
1331 u_flags.form = FORM_UNSPECIFIED;
1333 u_flags.delim = DELIM_UNSPECIFIED;
1334 u_flags.blank = BLANK_UNSPECIFIED;
1335 u_flags.pad = PAD_UNSPECIFIED;
1336 u_flags.status = STATUS_UNKNOWN;
1337 opp.common = dtp->common;
1338 opp.common.flags &= IOPARM_COMMON_MASK;
1339 dtp->u.p.current_unit = new_unit (&opp, dtp->u.p.current_unit, &u_flags);
1340 dtp->common.flags &= ~IOPARM_COMMON_MASK;
1341 dtp->common.flags |= (opp.common.flags & IOPARM_COMMON_MASK);
1342 if (dtp->u.p.current_unit == NULL)
1343 return;
1346 /* Check the action. */
1348 if (read_flag && dtp->u.p.current_unit->flags.action == ACTION_WRITE)
1349 generate_error (&dtp->common, ERROR_BAD_ACTION,
1350 "Cannot read from file opened for WRITE");
1352 if (!read_flag && dtp->u.p.current_unit->flags.action == ACTION_READ)
1353 generate_error (&dtp->common, ERROR_BAD_ACTION,
1354 "Cannot write to file opened for READ");
1356 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1357 return;
1359 dtp->u.p.first_item = 1;
1361 /* Check the format. */
1363 if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
1364 parse_format (dtp);
1366 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1367 return;
1369 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED
1370 && (cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
1371 != 0)
1372 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1373 "Format present for UNFORMATTED data transfer");
1375 if ((cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0 && dtp->u.p.ionml != NULL)
1377 if ((cf & IOPARM_DT_HAS_FORMAT) != 0)
1378 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1379 "A format cannot be specified with a namelist");
1381 else if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED &&
1382 !(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT)))
1383 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1384 "Missing format for FORMATTED data transfer");
1387 if (is_internal_unit (dtp)
1388 && dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
1389 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1390 "Internal file cannot be accessed by UNFORMATTED data transfer");
1392 /* Check the record number. */
1394 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT
1395 && (cf & IOPARM_DT_HAS_REC) == 0)
1397 generate_error (&dtp->common, ERROR_MISSING_OPTION,
1398 "Direct access data transfer requires record number");
1399 return;
1402 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL
1403 && (cf & IOPARM_DT_HAS_REC) != 0)
1405 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1406 "Record number not allowed for sequential access data transfer");
1407 return;
1410 /* Process the ADVANCE option. */
1412 dtp->u.p.advance_status
1413 = !(cf & IOPARM_DT_HAS_ADVANCE) ? ADVANCE_UNSPECIFIED :
1414 find_option (&dtp->common, dtp->advance, dtp->advance_len, advance_opt,
1415 "Bad ADVANCE parameter in data transfer statement");
1417 if (dtp->u.p.advance_status != ADVANCE_UNSPECIFIED)
1419 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
1420 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1421 "ADVANCE specification conflicts with sequential access");
1423 if (is_internal_unit (dtp))
1424 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1425 "ADVANCE specification conflicts with internal file");
1427 if ((cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))
1428 != IOPARM_DT_HAS_FORMAT)
1429 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1430 "ADVANCE specification requires an explicit format");
1433 if (read_flag)
1435 if ((cf & IOPARM_EOR) != 0 && dtp->u.p.advance_status != ADVANCE_NO)
1436 generate_error (&dtp->common, ERROR_MISSING_OPTION,
1437 "EOR specification requires an ADVANCE specification of NO");
1439 if ((cf & IOPARM_DT_HAS_SIZE) != 0 && dtp->u.p.advance_status != ADVANCE_NO)
1440 generate_error (&dtp->common, ERROR_MISSING_OPTION,
1441 "SIZE specification requires an ADVANCE specification of NO");
1444 else
1445 { /* Write constraints. */
1446 if ((cf & IOPARM_END) != 0)
1447 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1448 "END specification cannot appear in a write statement");
1450 if ((cf & IOPARM_EOR) != 0)
1451 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1452 "EOR specification cannot appear in a write statement");
1454 if ((cf & IOPARM_DT_HAS_SIZE) != 0)
1455 generate_error (&dtp->common, ERROR_OPTION_CONFLICT,
1456 "SIZE specification cannot appear in a write statement");
1459 if (dtp->u.p.advance_status == ADVANCE_UNSPECIFIED)
1460 dtp->u.p.advance_status = ADVANCE_YES;
1461 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1462 return;
1464 /* Sanity checks on the record number. */
1466 if ((cf & IOPARM_DT_HAS_REC) != 0)
1468 if (dtp->rec <= 0)
1470 generate_error (&dtp->common, ERROR_BAD_OPTION,
1471 "Record number must be positive");
1472 return;
1475 if (dtp->rec >= dtp->u.p.current_unit->maxrec)
1477 generate_error (&dtp->common, ERROR_BAD_OPTION,
1478 "Record number too large");
1479 return;
1482 /* Check to see if we might be reading what we wrote before */
1484 if (dtp->u.p.mode == READING && dtp->u.p.current_unit->mode == WRITING)
1485 flush(dtp->u.p.current_unit->s);
1487 /* Check whether the record exists to be read. Only
1488 a partial record needs to exist. */
1490 if (dtp->u.p.mode == READING && (dtp->rec -1)
1491 * dtp->u.p.current_unit->recl >= file_length (dtp->u.p.current_unit->s))
1493 generate_error (&dtp->common, ERROR_BAD_OPTION,
1494 "Non-existing record number");
1495 return;
1498 /* Position the file. */
1499 if (sseek (dtp->u.p.current_unit->s,
1500 (dtp->rec - 1) * dtp->u.p.current_unit->recl) == FAILURE)
1502 generate_error (&dtp->common, ERROR_OS, NULL);
1503 return;
1507 /* Overwriting an existing sequential file ?
1508 it is always safe to truncate the file on the first write */
1509 if (dtp->u.p.mode == WRITING
1510 && dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL
1511 && dtp->u.p.current_unit->last_record == 0 && !is_preconnected(dtp->u.p.current_unit->s))
1512 struncate(dtp->u.p.current_unit->s);
1514 /* Bugware for badly written mixed C-Fortran I/O. */
1515 flush_if_preconnected(dtp->u.p.current_unit->s);
1517 dtp->u.p.current_unit->mode = dtp->u.p.mode;
1519 /* Set the initial value of flags. */
1521 dtp->u.p.blank_status = dtp->u.p.current_unit->flags.blank;
1522 dtp->u.p.sign_status = SIGN_S;
1524 pre_position (dtp);
1526 /* Set up the subroutine that will handle the transfers. */
1528 if (read_flag)
1530 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
1531 dtp->u.p.transfer = unformatted_read;
1532 else
1534 if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
1535 dtp->u.p.transfer = list_formatted_read;
1536 else
1537 dtp->u.p.transfer = formatted_transfer;
1540 else
1542 if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED)
1543 dtp->u.p.transfer = unformatted_write;
1544 else
1546 if ((cf & IOPARM_DT_LIST_FORMAT) != 0)
1547 dtp->u.p.transfer = list_formatted_write;
1548 else
1549 dtp->u.p.transfer = formatted_transfer;
1553 /* Make sure that we don't do a read after a nonadvancing write. */
1555 if (read_flag)
1557 if (dtp->u.p.current_unit->read_bad)
1559 generate_error (&dtp->common, ERROR_BAD_OPTION,
1560 "Cannot READ after a nonadvancing WRITE");
1561 return;
1564 else
1566 if (dtp->u.p.advance_status == ADVANCE_YES && !dtp->u.p.seen_dollar)
1567 dtp->u.p.current_unit->read_bad = 1;
1570 /* Start the data transfer if we are doing a formatted transfer. */
1571 if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED
1572 && ((cf & (IOPARM_DT_LIST_FORMAT | IOPARM_DT_HAS_NAMELIST_NAME)) == 0)
1573 && dtp->u.p.ionml == NULL)
1574 formatted_transfer (dtp, 0, NULL, 0, 0, 1);
1577 /* Initialize an array_loop_spec given the array descriptor. The function
1578 returns the index of the last element of the array. */
1580 gfc_offset
1581 init_loop_spec (gfc_array_char *desc, array_loop_spec *ls)
1583 int rank = GFC_DESCRIPTOR_RANK(desc);
1584 int i;
1585 gfc_offset index;
1587 index = 1;
1588 for (i=0; i<rank; i++)
1590 ls[i].idx = 1;
1591 ls[i].start = desc->dim[i].lbound;
1592 ls[i].end = desc->dim[i].ubound;
1593 ls[i].step = desc->dim[i].stride;
1595 index += (desc->dim[i].ubound - desc->dim[i].lbound)
1596 * desc->dim[i].stride;
1598 return index;
1601 /* Determine the index to the next record in an internal unit array by
1602 by incrementing through the array_loop_spec. TODO: Implement handling
1603 negative strides. */
1605 gfc_offset
1606 next_array_record (st_parameter_dt *dtp, array_loop_spec *ls)
1608 int i, carry;
1609 gfc_offset index;
1611 carry = 1;
1612 index = 0;
1614 for (i = 0; i < dtp->u.p.current_unit->rank; i++)
1616 if (carry)
1618 ls[i].idx++;
1619 if (ls[i].idx > ls[i].end)
1621 ls[i].idx = ls[i].start;
1622 carry = 1;
1624 else
1625 carry = 0;
1627 index = index + (ls[i].idx - 1) * ls[i].step;
1629 return index;
1632 /* Space to the next record for read mode. If the file is not
1633 seekable, we read MAX_READ chunks until we get to the right
1634 position. */
1636 #define MAX_READ 4096
1638 static void
1639 next_record_r (st_parameter_dt *dtp)
1641 gfc_offset new, record;
1642 int bytes_left, rlength, length;
1643 char *p;
1645 switch (current_mode (dtp))
1647 case UNFORMATTED_SEQUENTIAL:
1648 dtp->u.p.current_unit->bytes_left += sizeof (gfc_offset); /* Skip over tail */
1650 /* Fall through... */
1652 case FORMATTED_DIRECT:
1653 case UNFORMATTED_DIRECT:
1654 if (dtp->u.p.current_unit->bytes_left == 0)
1655 break;
1657 if (is_seekable (dtp->u.p.current_unit->s))
1659 new = file_position (dtp->u.p.current_unit->s) + dtp->u.p.current_unit->bytes_left;
1661 /* Direct access files do not generate END conditions,
1662 only I/O errors. */
1663 if (sseek (dtp->u.p.current_unit->s, new) == FAILURE)
1664 generate_error (&dtp->common, ERROR_OS, NULL);
1667 else
1668 { /* Seek by reading data. */
1669 while (dtp->u.p.current_unit->bytes_left > 0)
1671 rlength = length = (MAX_READ > dtp->u.p.current_unit->bytes_left) ?
1672 MAX_READ : dtp->u.p.current_unit->bytes_left;
1674 p = salloc_r (dtp->u.p.current_unit->s, &rlength);
1675 if (p == NULL)
1677 generate_error (&dtp->common, ERROR_OS, NULL);
1678 break;
1681 dtp->u.p.current_unit->bytes_left -= length;
1684 break;
1686 case FORMATTED_SEQUENTIAL:
1687 length = 1;
1688 /* sf_read has already terminated input because of an '\n' */
1689 if (dtp->u.p.sf_seen_eor)
1691 dtp->u.p.sf_seen_eor = 0;
1692 break;
1695 if (is_internal_unit (dtp))
1697 if (is_array_io (dtp))
1699 record = next_array_record (dtp, dtp->u.p.current_unit->ls);
1701 /* Now seek to this record. */
1702 record = record * dtp->u.p.current_unit->recl;
1703 if (sseek (dtp->u.p.current_unit->s, record) == FAILURE)
1705 generate_error (&dtp->common, ERROR_OS, NULL);
1706 break;
1708 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
1710 else
1712 bytes_left = (int) dtp->u.p.current_unit->bytes_left;
1713 p = salloc_r (dtp->u.p.current_unit->s, &bytes_left);
1714 if (p != NULL)
1715 dtp->u.p.current_unit->bytes_left
1716 = dtp->u.p.current_unit->recl;
1718 break;
1720 else do
1722 p = salloc_r (dtp->u.p.current_unit->s, &length);
1724 if (p == NULL)
1726 generate_error (&dtp->common, ERROR_OS, NULL);
1727 break;
1730 if (length == 0)
1732 dtp->u.p.current_unit->endfile = AT_ENDFILE;
1733 break;
1736 while (*p != '\n');
1738 break;
1741 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
1742 test_endfile (dtp->u.p.current_unit);
1746 /* Position to the next record in write mode. */
1748 static void
1749 next_record_w (st_parameter_dt *dtp, int done)
1751 gfc_offset c, m, record, max_pos;
1752 int length;
1753 char *p;
1755 /* Zero counters for X- and T-editing. */
1756 max_pos = dtp->u.p.max_pos;
1757 dtp->u.p.max_pos = dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
1759 switch (current_mode (dtp))
1761 case FORMATTED_DIRECT:
1762 if (dtp->u.p.current_unit->bytes_left == 0)
1763 break;
1765 length = dtp->u.p.current_unit->bytes_left;
1766 p = salloc_w (dtp->u.p.current_unit->s, &length);
1768 if (p == NULL)
1769 goto io_error;
1771 memset (p, ' ', dtp->u.p.current_unit->bytes_left);
1772 if (sfree (dtp->u.p.current_unit->s) == FAILURE)
1773 goto io_error;
1774 break;
1776 case UNFORMATTED_DIRECT:
1777 if (sfree (dtp->u.p.current_unit->s) == FAILURE)
1778 goto io_error;
1779 break;
1781 case UNFORMATTED_SEQUENTIAL:
1782 /* Bytes written. */
1783 m = dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left;
1784 c = file_position (dtp->u.p.current_unit->s);
1786 length = sizeof (gfc_offset);
1788 /* Write the length tail. */
1790 p = salloc_w (dtp->u.p.current_unit->s, &length);
1791 if (p == NULL)
1792 goto io_error;
1794 /* Only CONVERT_NATIVE and CONVERT_SWAP are valid here. */
1795 if (dtp->u.p.current_unit->flags.convert == CONVERT_NATIVE)
1796 memcpy (p, &m, sizeof (gfc_offset));
1797 else
1798 reverse_memcpy (p, &m, sizeof (gfc_offset));
1800 if (sfree (dtp->u.p.current_unit->s) == FAILURE)
1801 goto io_error;
1803 /* Seek to the head and overwrite the bogus length with the real
1804 length. */
1806 p = salloc_w_at (dtp->u.p.current_unit->s, &length, c - m - length);
1807 if (p == NULL)
1808 generate_error (&dtp->common, ERROR_OS, NULL);
1810 /* Only CONVERT_NATIVE and CONVERT_SWAP are valid here. */
1811 if (dtp->u.p.current_unit->flags.convert == CONVERT_NATIVE)
1812 memcpy (p, &m, sizeof (gfc_offset));
1813 else
1814 reverse_memcpy (p, &m, sizeof (gfc_offset));
1816 if (sfree (dtp->u.p.current_unit->s) == FAILURE)
1817 goto io_error;
1819 /* Seek past the end of the current record. */
1821 if (sseek (dtp->u.p.current_unit->s, c + sizeof (gfc_offset)) == FAILURE)
1822 goto io_error;
1824 break;
1826 case FORMATTED_SEQUENTIAL:
1828 if (dtp->u.p.current_unit->bytes_left == 0)
1829 break;
1831 if (is_internal_unit (dtp))
1833 if (is_array_io (dtp))
1835 length = (int) dtp->u.p.current_unit->bytes_left;
1837 /* If the farthest position reached is greater than current
1838 position, adjust the position and set length to pad out
1839 whats left. Otherwise just pad whats left.
1840 (for character array unit) */
1841 m = dtp->u.p.current_unit->recl
1842 - dtp->u.p.current_unit->bytes_left;
1843 if (max_pos > m)
1845 length = (int) (max_pos - m);
1846 p = salloc_w (dtp->u.p.current_unit->s, &length);
1847 length = (int) (dtp->u.p.current_unit->recl - max_pos);
1850 p = salloc_w (dtp->u.p.current_unit->s, &length);
1851 if (p == NULL)
1853 generate_error (&dtp->common, ERROR_END, NULL);
1854 return;
1856 memset(p, ' ', length);
1858 /* Now that the current record has been padded out,
1859 determine where the next record in the array is. */
1860 record = next_array_record (dtp, dtp->u.p.current_unit->ls);
1862 /* Now seek to this record */
1863 record = record * dtp->u.p.current_unit->recl;
1865 if (sseek (dtp->u.p.current_unit->s, record) == FAILURE)
1866 goto io_error;
1868 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
1870 else
1872 length = 1;
1874 /* If this is the last call to next_record move to the farthest
1875 position reached and set length to pad out the remainder
1876 of the record. (for character scaler unit) */
1877 if (done)
1879 m = dtp->u.p.current_unit->recl
1880 - dtp->u.p.current_unit->bytes_left;
1881 if (max_pos > m)
1883 length = (int) (max_pos - m);
1884 p = salloc_w (dtp->u.p.current_unit->s, &length);
1885 length = (int) (dtp->u.p.current_unit->recl - max_pos);
1887 else
1888 length = (int) dtp->u.p.current_unit->bytes_left;
1890 p = salloc_w (dtp->u.p.current_unit->s, &length);
1891 if (p == NULL)
1893 generate_error (&dtp->common, ERROR_END, NULL);
1894 return;
1896 memset (p, ' ', length);
1899 else
1901 /* If this is the last call to next_record move to the farthest
1902 position reached in preparation for completing the record.
1903 (for file unit) */
1904 if (done)
1906 m = dtp->u.p.current_unit->recl -
1907 dtp->u.p.current_unit->bytes_left;
1908 if (max_pos > m)
1910 length = (int) (max_pos - m);
1911 p = salloc_w (dtp->u.p.current_unit->s, &length);
1914 #ifdef HAVE_CRLF
1915 length = 2;
1916 #else
1917 length = 1;
1918 #endif
1919 p = salloc_w (dtp->u.p.current_unit->s, &length);
1920 if (p)
1921 { /* No new line for internal writes. */
1922 #ifdef HAVE_CRLF
1923 p[0] = '\r';
1924 p[1] = '\n';
1925 #else
1926 *p = '\n';
1927 #endif
1929 else
1930 goto io_error;
1933 break;
1935 io_error:
1936 generate_error (&dtp->common, ERROR_OS, NULL);
1937 break;
1941 /* Position to the next record, which means moving to the end of the
1942 current record. This can happen under several different
1943 conditions. If the done flag is not set, we get ready to process
1944 the next record. */
1946 void
1947 next_record (st_parameter_dt *dtp, int done)
1949 gfc_offset fp; /* File position. */
1951 dtp->u.p.current_unit->read_bad = 0;
1953 if (dtp->u.p.mode == READING)
1954 next_record_r (dtp);
1955 else
1956 next_record_w (dtp, done);
1958 /* keep position up to date for INQUIRE */
1959 dtp->u.p.current_unit->flags.position = POSITION_ASIS;
1961 dtp->u.p.current_unit->current_record = 0;
1962 if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT)
1964 fp = file_position (dtp->u.p.current_unit->s);
1965 /* Calculate next record, rounding up partial records. */
1966 dtp->u.p.current_unit->last_record = (fp + dtp->u.p.current_unit->recl - 1)
1967 / dtp->u.p.current_unit->recl;
1969 else
1970 dtp->u.p.current_unit->last_record++;
1972 if (!done)
1973 pre_position (dtp);
1977 /* Finalize the current data transfer. For a nonadvancing transfer,
1978 this means advancing to the next record. For internal units close the
1979 stream associated with the unit. */
1981 static void
1982 finalize_transfer (st_parameter_dt *dtp)
1984 jmp_buf eof_jump;
1985 GFC_INTEGER_4 cf = dtp->common.flags;
1987 if (dtp->u.p.eor_condition)
1989 generate_error (&dtp->common, ERROR_EOR, NULL);
1990 return;
1993 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1994 return;
1996 if ((dtp->u.p.ionml != NULL)
1997 && (cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0)
1999 if ((cf & IOPARM_DT_NAMELIST_READ_MODE) != 0)
2000 namelist_read (dtp);
2001 else
2002 namelist_write (dtp);
2005 dtp->u.p.transfer = NULL;
2006 if (dtp->u.p.current_unit == NULL)
2007 return;
2009 dtp->u.p.eof_jump = &eof_jump;
2010 if (setjmp (eof_jump))
2012 generate_error (&dtp->common, ERROR_END, NULL);
2013 return;
2016 if ((cf & IOPARM_DT_LIST_FORMAT) != 0 && dtp->u.p.mode == READING)
2017 finish_list_read (dtp);
2018 else
2020 if (dtp->u.p.advance_status == ADVANCE_NO || dtp->u.p.seen_dollar)
2022 /* Most systems buffer lines, so force the partial record
2023 to be written out. */
2024 flush (dtp->u.p.current_unit->s);
2025 dtp->u.p.seen_dollar = 0;
2026 return;
2029 next_record (dtp, 1);
2030 dtp->u.p.current_unit->current_record = 0;
2033 sfree (dtp->u.p.current_unit->s);
2035 if (is_internal_unit (dtp))
2037 if (is_array_io (dtp) && dtp->u.p.current_unit->ls != NULL)
2038 free_mem (dtp->u.p.current_unit->ls);
2039 sclose (dtp->u.p.current_unit->s);
2044 /* Transfer function for IOLENGTH. It doesn't actually do any
2045 data transfer, it just updates the length counter. */
2047 static void
2048 iolength_transfer (st_parameter_dt *dtp, bt type __attribute__((unused)),
2049 void *dest __attribute__ ((unused)),
2050 int kind __attribute__((unused)),
2051 size_t size, size_t nelems)
2053 if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
2054 *dtp->iolength += (GFC_INTEGER_4) size * nelems;
2058 /* Initialize the IOLENGTH data transfer. This function is in essence
2059 a very much simplified version of data_transfer_init(), because it
2060 doesn't have to deal with units at all. */
2062 static void
2063 iolength_transfer_init (st_parameter_dt *dtp)
2065 if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0)
2066 *dtp->iolength = 0;
2068 memset (&dtp->u.p, 0, sizeof (dtp->u.p));
2070 /* Set up the subroutine that will handle the transfers. */
2072 dtp->u.p.transfer = iolength_transfer;
2076 /* Library entry point for the IOLENGTH form of the INQUIRE
2077 statement. The IOLENGTH form requires no I/O to be performed, but
2078 it must still be a runtime library call so that we can determine
2079 the iolength for dynamic arrays and such. */
2081 extern void st_iolength (st_parameter_dt *);
2082 export_proto(st_iolength);
2084 void
2085 st_iolength (st_parameter_dt *dtp)
2087 library_start (&dtp->common);
2088 iolength_transfer_init (dtp);
2091 extern void st_iolength_done (st_parameter_dt *);
2092 export_proto(st_iolength_done);
2094 void
2095 st_iolength_done (st_parameter_dt *dtp __attribute__((unused)))
2097 free_ionml (dtp);
2098 if (dtp->u.p.scratch != NULL)
2099 free_mem (dtp->u.p.scratch);
2100 library_end ();
2104 /* The READ statement. */
2106 extern void st_read (st_parameter_dt *);
2107 export_proto(st_read);
2109 void
2110 st_read (st_parameter_dt *dtp)
2113 library_start (&dtp->common);
2115 data_transfer_init (dtp, 1);
2117 /* Handle complications dealing with the endfile record. It is
2118 significant that this is the only place where ERROR_END is
2119 generated. Reading an end of file elsewhere is either end of
2120 record or an I/O error. */
2122 if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
2123 switch (dtp->u.p.current_unit->endfile)
2125 case NO_ENDFILE:
2126 break;
2128 case AT_ENDFILE:
2129 if (!is_internal_unit (dtp))
2131 generate_error (&dtp->common, ERROR_END, NULL);
2132 dtp->u.p.current_unit->endfile = AFTER_ENDFILE;
2133 dtp->u.p.current_unit->current_record = 0;
2135 break;
2137 case AFTER_ENDFILE:
2138 generate_error (&dtp->common, ERROR_ENDFILE, NULL);
2139 dtp->u.p.current_unit->current_record = 0;
2140 break;
2144 extern void st_read_done (st_parameter_dt *);
2145 export_proto(st_read_done);
2147 void
2148 st_read_done (st_parameter_dt *dtp)
2150 finalize_transfer (dtp);
2151 free_format_data (dtp);
2152 free_ionml (dtp);
2153 if (dtp->u.p.scratch != NULL)
2154 free_mem (dtp->u.p.scratch);
2155 if (dtp->u.p.current_unit != NULL)
2156 unlock_unit (dtp->u.p.current_unit);
2157 library_end ();
2160 extern void st_write (st_parameter_dt *);
2161 export_proto(st_write);
2163 void
2164 st_write (st_parameter_dt *dtp)
2166 library_start (&dtp->common);
2167 data_transfer_init (dtp, 0);
2170 extern void st_write_done (st_parameter_dt *);
2171 export_proto(st_write_done);
2173 void
2174 st_write_done (st_parameter_dt *dtp)
2176 finalize_transfer (dtp);
2178 /* Deal with endfile conditions associated with sequential files. */
2180 if (dtp->u.p.current_unit != NULL && dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL)
2181 switch (dtp->u.p.current_unit->endfile)
2183 case AT_ENDFILE: /* Remain at the endfile record. */
2184 break;
2186 case AFTER_ENDFILE:
2187 dtp->u.p.current_unit->endfile = AT_ENDFILE; /* Just at it now. */
2188 break;
2190 case NO_ENDFILE:
2191 if (dtp->u.p.current_unit->current_record > dtp->u.p.current_unit->last_record)
2193 /* Get rid of whatever is after this record. */
2194 if (struncate (dtp->u.p.current_unit->s) == FAILURE)
2195 generate_error (&dtp->common, ERROR_OS, NULL);
2198 dtp->u.p.current_unit->endfile = AT_ENDFILE;
2199 break;
2202 free_format_data (dtp);
2203 free_ionml (dtp);
2204 if (dtp->u.p.scratch != NULL)
2205 free_mem (dtp->u.p.scratch);
2206 if (dtp->u.p.current_unit != NULL)
2207 unlock_unit (dtp->u.p.current_unit);
2208 library_end ();
2211 /* Receives the scalar information for namelist objects and stores it
2212 in a linked list of namelist_info types. */
2214 extern void st_set_nml_var (st_parameter_dt *dtp, void *, char *,
2215 GFC_INTEGER_4, gfc_charlen_type, GFC_INTEGER_4);
2216 export_proto(st_set_nml_var);
2219 void
2220 st_set_nml_var (st_parameter_dt *dtp, void * var_addr, char * var_name,
2221 GFC_INTEGER_4 len, gfc_charlen_type string_length,
2222 GFC_INTEGER_4 dtype)
2224 namelist_info *t1 = NULL;
2225 namelist_info *nml;
2227 nml = (namelist_info*) get_mem (sizeof (namelist_info));
2229 nml->mem_pos = var_addr;
2231 nml->var_name = (char*) get_mem (strlen (var_name) + 1);
2232 strcpy (nml->var_name, var_name);
2234 nml->len = (int) len;
2235 nml->string_length = (index_type) string_length;
2237 nml->var_rank = (int) (dtype & GFC_DTYPE_RANK_MASK);
2238 nml->size = (index_type) (dtype >> GFC_DTYPE_SIZE_SHIFT);
2239 nml->type = (bt) ((dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT);
2241 if (nml->var_rank > 0)
2243 nml->dim = (descriptor_dimension*)
2244 get_mem (nml->var_rank * sizeof (descriptor_dimension));
2245 nml->ls = (array_loop_spec*)
2246 get_mem (nml->var_rank * sizeof (array_loop_spec));
2248 else
2250 nml->dim = NULL;
2251 nml->ls = NULL;
2254 nml->next = NULL;
2256 if ((dtp->common.flags & IOPARM_DT_IONML_SET) == 0)
2258 dtp->common.flags |= IOPARM_DT_IONML_SET;
2259 dtp->u.p.ionml = nml;
2261 else
2263 for (t1 = dtp->u.p.ionml; t1->next; t1 = t1->next);
2264 t1->next = nml;
2268 /* Store the dimensional information for the namelist object. */
2269 extern void st_set_nml_var_dim (st_parameter_dt *, GFC_INTEGER_4,
2270 GFC_INTEGER_4, GFC_INTEGER_4,
2271 GFC_INTEGER_4);
2272 export_proto(st_set_nml_var_dim);
2274 void
2275 st_set_nml_var_dim (st_parameter_dt *dtp, GFC_INTEGER_4 n_dim,
2276 GFC_INTEGER_4 stride, GFC_INTEGER_4 lbound,
2277 GFC_INTEGER_4 ubound)
2279 namelist_info * nml;
2280 int n;
2282 n = (int)n_dim;
2284 for (nml = dtp->u.p.ionml; nml->next; nml = nml->next);
2286 nml->dim[n].stride = (ssize_t)stride;
2287 nml->dim[n].lbound = (ssize_t)lbound;
2288 nml->dim[n].ubound = (ssize_t)ubound;
2291 /* Reverse memcpy - used for byte swapping. */
2293 void reverse_memcpy (void *dest, const void *src, size_t n)
2295 char *d, *s;
2296 size_t i;
2298 d = (char *) dest;
2299 s = (char *) src + n - 1;
2301 /* Write with ascending order - this is likely faster
2302 on modern architectures because of write combining. */
2303 for (i=0; i<n; i++)
2304 *(d++) = *(s--);