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)
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
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. */
37 #include "libgfortran.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
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
66 extern void transfer_integer (void *, int);
67 export_proto(transfer_integer);
69 extern void transfer_real (void *, int);
70 export_proto(transfer_real);
72 extern void transfer_logical (void *, int);
73 export_proto(transfer_logical);
75 extern void transfer_character (void *, int);
76 export_proto(transfer_character);
78 extern void transfer_complex (void *, int);
79 export_proto(transfer_complex);
81 extern void transfer_array (gfc_array_char *, gfc_charlen_type);
82 export_proto(transfer_array);
84 gfc_unit *current_unit = NULL;
85 static int sf_seen_eor = 0;
86 static int eor_condition = 0;
88 /* Maximum righthand column written to. */
90 /* Number of skips + spaces to be done for T and X-editing. */
92 /* Number of spaces to be done for T and X-editing. */
93 static int pending_spaces;
95 char scratch[SCRATCH_SIZE];
96 static char *line_buffer = NULL;
98 static unit_advance advance_status;
100 static const st_option advance_opt[] = {
101 {"yes", ADVANCE_YES},
107 static void (*transfer) (bt, void *, int, size_t);
111 { FORMATTED_SEQUENTIAL, UNFORMATTED_SEQUENTIAL,
112 FORMATTED_DIRECT, UNFORMATTED_DIRECT
122 if (current_unit->flags.access == ACCESS_DIRECT)
124 m = current_unit->flags.form == FORM_FORMATTED ?
125 FORMATTED_DIRECT : UNFORMATTED_DIRECT;
129 m = current_unit->flags.form == FORM_FORMATTED ?
130 FORMATTED_SEQUENTIAL : UNFORMATTED_SEQUENTIAL;
137 /* Mid level data transfer statements. These subroutines do reading
138 and writing in the style of salloc_r()/salloc_w() within the
141 /* When reading sequential formatted records we have a problem. We
142 don't know how long the line is until we read the trailing newline,
143 and we don't want to read too much. If we read too much, we might
144 have to do a physical seek backwards depending on how much data is
145 present, and devices like terminals aren't seekable and would cause
148 Given this, the solution is to read a byte at a time, stopping if
149 we hit the newline. For small locations, we use a static buffer.
150 For larger allocations, we are forced to allocate memory on the
151 heap. Hopefully this won't happen very often. */
154 read_sf (int *length)
156 static char data[SCRATCH_SIZE];
160 if (*length > SCRATCH_SIZE)
161 p = base = line_buffer = get_mem (*length);
165 /* If we have seen an eor previously, return a length of 0. The
166 caller is responsible for correctly padding the input field. */
178 if (is_internal_unit())
180 /* readlen may be modified inside salloc_r if
181 is_internal_unit() is true. */
185 q = salloc_r (current_unit->s, &readlen);
189 /* If we have a line without a terminating \n, drop through to
191 if (readlen < 1 && n == 0)
193 generate_error (ERROR_END, NULL);
197 if (readlen < 1 || *q == '\n' || *q == '\r')
199 /* Unexpected end of line. */
201 /* If we see an EOR during non-advancing I/O, we need to skip
202 the rest of the I/O statement. Set the corresponding flag. */
203 if (advance_status == ADVANCE_NO || g.seen_dollar)
206 /* Without padding, terminate the I/O statement without assigning
207 the value. With padding, the value still needs to be assigned,
208 so we can just continue with a short read. */
209 if (current_unit->flags.pad == PAD_NO)
211 generate_error (ERROR_EOR, NULL);
215 current_unit->bytes_left = 0;
226 current_unit->bytes_left -= *length;
228 if (ioparm.size != NULL)
229 *ioparm.size += *length;
235 /* Function for reading the next couple of bytes from the current
236 file, advancing the current position. We return a pointer to a
237 buffer containing the bytes. We return NULL on end of record or
240 If the read is short, then it is because the current record does not
241 have enough data to satisfy the read request and the file was
242 opened with PAD=YES. The caller must assume tailing spaces for
246 read_block (int *length)
251 if (current_unit->flags.form == FORM_FORMATTED &&
252 current_unit->flags.access == ACCESS_SEQUENTIAL)
253 return read_sf (length); /* Special case. */
255 if (current_unit->bytes_left < *length)
257 if (current_unit->flags.pad == PAD_NO)
259 generate_error (ERROR_EOR, NULL); /* Not enough data left. */
263 *length = current_unit->bytes_left;
266 current_unit->bytes_left -= *length;
269 source = salloc_r (current_unit->s, &nread);
271 if (ioparm.size != NULL)
272 *ioparm.size += nread;
274 if (nread != *length)
275 { /* Short read, this shouldn't happen. */
276 if (current_unit->flags.pad == PAD_YES)
280 generate_error (ERROR_EOR, NULL);
289 /* Reads a block directly into application data space. */
292 read_block_direct (void * buf, size_t * nbytes)
298 if (current_unit->flags.form == FORM_FORMATTED &&
299 current_unit->flags.access == ACCESS_SEQUENTIAL)
301 length = (int*) nbytes;
302 data = read_sf (length); /* Special case. */
303 memcpy (buf, data, (size_t) *length);
307 if (current_unit->bytes_left < *nbytes)
309 if (current_unit->flags.pad == PAD_NO)
311 generate_error (ERROR_EOR, NULL); /* Not enough data left. */
315 *nbytes = current_unit->bytes_left;
318 current_unit->bytes_left -= *nbytes;
321 if (sread (current_unit->s, buf, &nread) != 0)
323 generate_error (ERROR_OS, NULL);
327 if (ioparm.size != NULL)
328 *ioparm.size += (GFC_INTEGER_4) nread;
330 if (nread != *nbytes)
331 { /* Short read, e.g. if we hit EOF. */
332 if (current_unit->flags.pad == PAD_YES)
334 memset (((char *) buf) + nread, ' ', *nbytes - nread);
338 generate_error (ERROR_EOR, NULL);
343 /* Function for writing a block of bytes to the current file at the
344 current position, advancing the file pointer. We are given a length
345 and return a pointer to a buffer that the caller must (completely)
346 fill in. Returns NULL on error. */
349 write_block (int length)
353 if (current_unit->bytes_left < length)
355 generate_error (ERROR_EOR, NULL);
359 current_unit->bytes_left -= (gfc_offset)length;
360 dest = salloc_w (current_unit->s, &length);
364 generate_error (ERROR_END, NULL);
368 if (ioparm.size != NULL)
369 *ioparm.size += length;
375 /* Writes a block directly without necessarily allocating space in a
379 write_block_direct (void * buf, size_t * nbytes)
381 if (current_unit->bytes_left < *nbytes)
382 generate_error (ERROR_EOR, NULL);
384 current_unit->bytes_left -= (gfc_offset) *nbytes;
386 if (swrite (current_unit->s, buf, nbytes) != 0)
387 generate_error (ERROR_OS, NULL);
389 if (ioparm.size != NULL)
390 *ioparm.size += (GFC_INTEGER_4) *nbytes;
394 /* Master function for unformatted reads. */
397 unformatted_read (bt type, void *dest, int length, size_t nelems)
401 len = length * nelems;
403 /* Transfer functions get passed the kind of the entity, so we have
404 to fix this for COMPLEX data which are twice the size of their
406 if (type == BT_COMPLEX)
409 read_block_direct (dest, &len);
413 /* Master function for unformatted writes. */
416 unformatted_write (bt type, void *source, int length, size_t nelems)
420 len = length * nelems;
422 /* Correction for kind vs. length as in unformatted_read. */
423 if (type == BT_COMPLEX)
426 write_block_direct (source, &len);
430 /* Return a pointer to the name of a type. */
455 internal_error ("type_name(): Bad type");
462 /* Write a constant string to the output.
463 This is complicated because the string can have doubled delimiters
464 in it. The length in the format node is the true length. */
467 write_constant_string (fnode * f)
469 char c, delimiter, *p, *q;
472 length = f->u.string.length;
476 p = write_block (length);
483 for (; length > 0; length--)
486 if (c == delimiter && c != 'H' && c != 'h')
487 q++; /* Skip the doubled delimiter. */
492 /* Given actual and expected types in a formatted data transfer, make
493 sure they agree. If not, an error message is generated. Returns
494 nonzero if something went wrong. */
497 require_type (bt expected, bt actual, fnode * f)
501 if (actual == expected)
504 st_sprintf (buffer, "Expected %s for item %d in formatted transfer, got %s",
505 type_name (expected), g.item_count, type_name (actual));
507 format_error (f, buffer);
512 /* This subroutine is the main loop for a formatted data transfer
513 statement. It would be natural to implement this as a coroutine
514 with the user program, but C makes that awkward. We loop,
515 processesing format elements. When we actually have to transfer
516 data instead of just setting flags, we return control to the user
517 program which calls a subroutine that supplies the address and type
518 of the next element, then comes back here to process it. */
521 formatted_transfer_scalar (bt type, void *p, int len)
527 int consume_data_flag;
529 /* Change a complex data item into a pair of reals. */
531 n = (p == NULL) ? 0 : ((type != BT_COMPLEX) ? 1 : 2);
532 if (type == BT_COMPLEX)
535 /* If there's an EOR condition, we simulate finalizing the transfer
542 /* If reversion has occurred and there is another real data item,
543 then we have to move to the next record. */
544 if (g.reversion_flag && n > 0)
546 g.reversion_flag = 0;
550 consume_data_flag = 1 ;
551 if (ioparm.library_return != LIBRARY_OK)
556 return; /* No data descriptors left (already raised). */
558 /* Now discharge T, TR and X movements to the right. This is delayed
559 until a data producing format to suppress trailing spaces. */
561 if (g.mode == WRITING && skips != 0
562 && ((n>0 && ( t == FMT_I || t == FMT_B || t == FMT_O
563 || t == FMT_Z || t == FMT_F || t == FMT_E
564 || t == FMT_EN || t == FMT_ES || t == FMT_G
565 || t == FMT_L || t == FMT_A || t == FMT_D))
570 write_x (skips, pending_spaces);
571 max_pos = (int)(current_unit->recl - current_unit->bytes_left);
575 move_pos_offset (current_unit->s, skips);
576 current_unit->bytes_left -= (gfc_offset)skips;
578 skips = pending_spaces = 0;
581 bytes_used = (int)(current_unit->recl - current_unit->bytes_left);
588 if (require_type (BT_INTEGER, type, f))
591 if (g.mode == READING)
592 read_decimal (f, p, len);
601 if (require_type (BT_INTEGER, type, f))
604 if (g.mode == READING)
605 read_radix (f, p, len, 2);
615 if (g.mode == READING)
616 read_radix (f, p, len, 8);
626 if (g.mode == READING)
627 read_radix (f, p, len, 16);
637 if (g.mode == READING)
648 if (g.mode == READING)
658 if (require_type (BT_REAL, type, f))
661 if (g.mode == READING)
671 if (require_type (BT_REAL, type, f))
674 if (g.mode == READING)
683 if (require_type (BT_REAL, type, f))
686 if (g.mode == READING)
689 write_en (f, p, len);
696 if (require_type (BT_REAL, type, f))
699 if (g.mode == READING)
702 write_es (f, p, len);
709 if (require_type (BT_REAL, type, f))
712 if (g.mode == READING)
722 if (g.mode == READING)
726 read_decimal (f, p, len);
757 internal_error ("formatted_transfer(): Bad type");
763 consume_data_flag = 0 ;
764 if (g.mode == READING)
766 format_error (f, "Constant string in input format");
769 write_constant_string (f);
772 /* Format codes that don't transfer data. */
775 consume_data_flag = 0 ;
777 pos = bytes_used + f->u.n + skips;
778 skips = f->u.n + skips;
779 pending_spaces = pos - max_pos;
781 /* Writes occur just before the switch on f->format, above, so that
782 trailing blanks are suppressed. */
783 if (g.mode == READING)
790 if (f->format == FMT_TL)
791 pos = bytes_used - f->u.n;
794 consume_data_flag = 0;
798 /* Standard 10.6.1.1: excessive left tabbing is reset to the
799 left tab limit. We do not check if the position has gone
800 beyond the end of record because a subsequent tab could
801 bring us back again. */
802 pos = pos < 0 ? 0 : pos;
804 skips = skips + pos - bytes_used;
805 pending_spaces = pending_spaces + pos - max_pos;
810 /* Writes occur just before the switch on f->format, above, so that
811 trailing blanks are suppressed. */
812 if (g.mode == READING)
818 move_pos_offset (current_unit->s, skips);
819 current_unit->bytes_left -= (gfc_offset)skips;
820 skips = pending_spaces = 0;
827 consume_data_flag = 0 ;
828 g.sign_status = SIGN_S;
832 consume_data_flag = 0 ;
833 g.sign_status = SIGN_SS;
837 consume_data_flag = 0 ;
838 g.sign_status = SIGN_SP;
842 consume_data_flag = 0 ;
843 g.blank_status = BLANK_NULL;
847 consume_data_flag = 0 ;
848 g.blank_status = BLANK_ZERO;
852 consume_data_flag = 0 ;
853 g.scale_factor = f->u.k;
857 consume_data_flag = 0 ;
862 consume_data_flag = 0 ;
863 skips = pending_spaces = 0;
868 /* A colon descriptor causes us to exit this loop (in
869 particular preventing another / descriptor from being
870 processed) unless there is another data item to be
872 consume_data_flag = 0 ;
878 internal_error ("Bad format node");
881 /* Free a buffer that we had to allocate during a sequential
882 formatted read of a block that was larger than the static
885 if (line_buffer != NULL)
887 free_mem (line_buffer);
891 /* Adjust the item count and data pointer. */
893 if ((consume_data_flag > 0) && (n > 0))
896 p = ((char *) p) + len;
899 if (g.mode == READING)
902 pos = (int)(current_unit->recl - current_unit->bytes_left);
903 max_pos = (max_pos > pos) ? max_pos : pos;
909 /* Come here when we need a data descriptor but don't have one. We
910 push the current format node back onto the input, then return and
911 let the user program call us back with the data. */
917 formatted_transfer (bt type, void *p, int len, size_t nelems)
925 if (type == BT_COMPLEX)
930 /* Big loop over all the elements. */
931 for (elem = 0; elem < nelems; elem++)
934 formatted_transfer_scalar (type, tmp + size*elem, len);
940 /* Data transfer entry points. The type of the data entity is
941 implicit in the subroutine call. This prevents us from having to
942 share a common enum with the compiler. */
945 transfer_integer (void *p, int kind)
947 if (ioparm.library_return != LIBRARY_OK)
949 transfer (BT_INTEGER, p, kind, 1);
954 transfer_real (void *p, int kind)
956 if (ioparm.library_return != LIBRARY_OK)
958 transfer (BT_REAL, p, kind, 1);
963 transfer_logical (void *p, int kind)
965 if (ioparm.library_return != LIBRARY_OK)
967 transfer (BT_LOGICAL, p, kind, 1);
972 transfer_character (void *p, int len)
974 if (ioparm.library_return != LIBRARY_OK)
976 transfer (BT_CHARACTER, p, len, 1);
981 transfer_complex (void *p, int kind)
983 if (ioparm.library_return != LIBRARY_OK)
985 transfer (BT_COMPLEX, p, kind, 1);
990 transfer_array (gfc_array_char *desc, gfc_charlen_type charlen)
992 index_type count[GFC_MAX_DIMENSIONS];
993 index_type extent[GFC_MAX_DIMENSIONS];
994 index_type stride[GFC_MAX_DIMENSIONS];
995 index_type stride0, rank, size, type, n, kind;
1000 if (ioparm.library_return != LIBRARY_OK)
1003 type = GFC_DESCRIPTOR_TYPE (desc);
1004 size = GFC_DESCRIPTOR_SIZE (desc);
1007 /* FIXME: What a kludge: Array descriptors and the IO library use
1008 different enums for types. */
1011 case GFC_DTYPE_UNKNOWN:
1012 iotype = BT_NULL; /* Is this correct? */
1014 case GFC_DTYPE_INTEGER:
1015 iotype = BT_INTEGER;
1017 case GFC_DTYPE_LOGICAL:
1018 iotype = BT_LOGICAL;
1020 case GFC_DTYPE_REAL:
1023 case GFC_DTYPE_COMPLEX:
1024 iotype = BT_COMPLEX;
1027 case GFC_DTYPE_CHARACTER:
1028 iotype = BT_CHARACTER;
1029 /* FIXME: Currently dtype contains the charlen, which is
1030 clobbered if charlen > 2**24. That's why we use a separate
1031 argument for the charlen. However, if we want to support
1032 non-8-bit charsets we need to fix dtype to contain
1033 sizeof(chartype) and fix the code below. */
1037 case GFC_DTYPE_DERIVED:
1038 internal_error ("Derived type I/O should have been handled via the frontend.");
1041 internal_error ("transfer_array(): Bad type");
1044 if (desc->dim[0].stride == 0)
1045 desc->dim[0].stride = 1;
1047 rank = GFC_DESCRIPTOR_RANK (desc);
1048 for (n = 0; n < rank; n++)
1051 stride[n] = desc->dim[n].stride;
1052 extent[n] = desc->dim[n].ubound + 1 - desc->dim[n].lbound;
1054 /* If the extent of even one dimension is zero, then the entire
1055 array section contains zero elements, so we return. */
1060 stride0 = stride[0];
1062 /* If the innermost dimension has stride 1, we can do the transfer
1063 in contiguous chunks. */
1069 data = GFC_DESCRIPTOR_DATA (desc);
1073 transfer (iotype, data, kind, tsize);
1074 data += stride0 * size * tsize;
1077 while (count[n] == extent[n])
1080 data -= stride[n] * extent[n] * size;
1090 data += stride[n] * size;
1097 /* Preposition a sequential unformatted file while reading. */
1106 n = sizeof (gfc_offset);
1107 p = salloc_r (current_unit->s, &n);
1110 return; /* end of file */
1112 if (p == NULL || n != sizeof (gfc_offset))
1114 generate_error (ERROR_BAD_US, NULL);
1118 memcpy (&i, p, sizeof (gfc_offset));
1119 current_unit->bytes_left = i;
1123 /* Preposition a sequential unformatted file while writing. This
1124 amount to writing a bogus length that will be filled in later. */
1132 length = sizeof (gfc_offset);
1133 p = salloc_w (current_unit->s, &length);
1137 generate_error (ERROR_OS, NULL);
1141 memset (p, '\0', sizeof (gfc_offset)); /* Bogus value for now. */
1142 if (sfree (current_unit->s) == FAILURE)
1143 generate_error (ERROR_OS, NULL);
1145 /* For sequential unformatted, we write until we have more bytes than
1146 can fit in the record markers. If disk space runs out first, it will
1147 error on the write. */
1148 current_unit->recl = g.max_offset;
1150 current_unit->bytes_left = current_unit->recl;
1154 /* Position to the next record prior to transfer. We are assumed to
1155 be before the next record. We also calculate the bytes in the next
1161 if (current_unit->current_record)
1162 return; /* Already positioned. */
1164 switch (current_mode ())
1166 case UNFORMATTED_SEQUENTIAL:
1167 if (g.mode == READING)
1174 case FORMATTED_SEQUENTIAL:
1175 case FORMATTED_DIRECT:
1176 case UNFORMATTED_DIRECT:
1177 current_unit->bytes_left = current_unit->recl;
1181 current_unit->current_record = 1;
1185 /* Initialize things for a data transfer. This code is common for
1186 both reading and writing. */
1189 data_transfer_init (int read_flag)
1191 unit_flags u_flags; /* Used for creating a unit if needed. */
1193 g.mode = read_flag ? READING : WRITING;
1195 if (ioparm.size != NULL)
1196 *ioparm.size = 0; /* Initialize the count. */
1198 current_unit = get_unit (read_flag);
1199 if (current_unit == NULL)
1200 { /* Open the unit with some default flags. */
1201 if (ioparm.unit < 0)
1203 generate_error (ERROR_BAD_OPTION, "Bad unit number in OPEN statement");
1207 memset (&u_flags, '\0', sizeof (u_flags));
1208 u_flags.access = ACCESS_SEQUENTIAL;
1209 u_flags.action = ACTION_READWRITE;
1210 /* Is it unformatted? */
1211 if (ioparm.format == NULL && !ioparm.list_format)
1212 u_flags.form = FORM_UNFORMATTED;
1214 u_flags.form = FORM_UNSPECIFIED;
1215 u_flags.delim = DELIM_UNSPECIFIED;
1216 u_flags.blank = BLANK_UNSPECIFIED;
1217 u_flags.pad = PAD_UNSPECIFIED;
1218 u_flags.status = STATUS_UNKNOWN;
1220 current_unit = get_unit (read_flag);
1223 if (current_unit == NULL)
1226 /* Check the action. */
1228 if (read_flag && current_unit->flags.action == ACTION_WRITE)
1229 generate_error (ERROR_BAD_ACTION,
1230 "Cannot read from file opened for WRITE");
1232 if (!read_flag && current_unit->flags.action == ACTION_READ)
1233 generate_error (ERROR_BAD_ACTION, "Cannot write to file opened for READ");
1235 if (ioparm.library_return != LIBRARY_OK)
1238 /* Check the format. */
1243 if (ioparm.library_return != LIBRARY_OK)
1246 if (current_unit->flags.form == FORM_UNFORMATTED
1247 && (ioparm.format != NULL || ioparm.list_format))
1248 generate_error (ERROR_OPTION_CONFLICT,
1249 "Format present for UNFORMATTED data transfer");
1251 if (ioparm.namelist_name != NULL && ionml != NULL)
1253 if(ioparm.format != NULL)
1254 generate_error (ERROR_OPTION_CONFLICT,
1255 "A format cannot be specified with a namelist");
1257 else if (current_unit->flags.form == FORM_FORMATTED &&
1258 ioparm.format == NULL && !ioparm.list_format)
1259 generate_error (ERROR_OPTION_CONFLICT,
1260 "Missing format for FORMATTED data transfer");
1263 if (is_internal_unit () && current_unit->flags.form == FORM_UNFORMATTED)
1264 generate_error (ERROR_OPTION_CONFLICT,
1265 "Internal file cannot be accessed by UNFORMATTED data transfer");
1267 /* Check the record number. */
1269 if (current_unit->flags.access == ACCESS_DIRECT && ioparm.rec == 0)
1271 generate_error (ERROR_MISSING_OPTION,
1272 "Direct access data transfer requires record number");
1276 if (current_unit->flags.access == ACCESS_SEQUENTIAL && ioparm.rec != 0)
1278 generate_error (ERROR_OPTION_CONFLICT,
1279 "Record number not allowed for sequential access data transfer");
1283 /* Process the ADVANCE option. */
1285 advance_status = (ioparm.advance == NULL) ? ADVANCE_UNSPECIFIED :
1286 find_option (ioparm.advance, ioparm.advance_len, advance_opt,
1287 "Bad ADVANCE parameter in data transfer statement");
1289 if (advance_status != ADVANCE_UNSPECIFIED)
1291 if (current_unit->flags.access == ACCESS_DIRECT)
1292 generate_error (ERROR_OPTION_CONFLICT,
1293 "ADVANCE specification conflicts with sequential access");
1295 if (is_internal_unit ())
1296 generate_error (ERROR_OPTION_CONFLICT,
1297 "ADVANCE specification conflicts with internal file");
1299 if (ioparm.format == NULL || ioparm.list_format)
1300 generate_error (ERROR_OPTION_CONFLICT,
1301 "ADVANCE specification requires an explicit format");
1306 if (ioparm.eor != 0 && advance_status != ADVANCE_NO)
1307 generate_error (ERROR_MISSING_OPTION,
1308 "EOR specification requires an ADVANCE specification of NO");
1310 if (ioparm.size != NULL && advance_status != ADVANCE_NO)
1311 generate_error (ERROR_MISSING_OPTION,
1312 "SIZE specification requires an ADVANCE specification of NO");
1316 { /* Write constraints. */
1317 if (ioparm.end != 0)
1318 generate_error (ERROR_OPTION_CONFLICT,
1319 "END specification cannot appear in a write statement");
1321 if (ioparm.eor != 0)
1322 generate_error (ERROR_OPTION_CONFLICT,
1323 "EOR specification cannot appear in a write statement");
1325 if (ioparm.size != 0)
1326 generate_error (ERROR_OPTION_CONFLICT,
1327 "SIZE specification cannot appear in a write statement");
1330 if (advance_status == ADVANCE_UNSPECIFIED)
1331 advance_status = ADVANCE_YES;
1332 if (ioparm.library_return != LIBRARY_OK)
1335 /* Sanity checks on the record number. */
1339 if (ioparm.rec <= 0)
1341 generate_error (ERROR_BAD_OPTION, "Record number must be positive");
1345 if (ioparm.rec >= current_unit->maxrec)
1347 generate_error (ERROR_BAD_OPTION, "Record number too large");
1351 /* Check to see if we might be reading what we wrote before */
1353 if (g.mode == READING && current_unit->mode == WRITING)
1354 flush(current_unit->s);
1356 /* Check whether the record exists to be read. Only
1357 a partial record needs to exist. */
1359 if (g.mode == READING && (ioparm.rec -1)
1360 * current_unit->recl >= file_length (current_unit->s))
1362 generate_error (ERROR_BAD_OPTION, "Non-existing record number");
1366 /* Position the file. */
1367 if (sseek (current_unit->s,
1368 (ioparm.rec - 1) * current_unit->recl) == FAILURE)
1370 generate_error (ERROR_OS, NULL);
1375 /* Overwriting an existing sequential file ?
1376 it is always safe to truncate the file on the first write */
1377 if (g.mode == WRITING
1378 && current_unit->flags.access == ACCESS_SEQUENTIAL
1379 && current_unit->last_record == 0 && !is_preconnected(current_unit->s))
1380 struncate(current_unit->s);
1382 current_unit->mode = g.mode;
1384 /* Set the initial value of flags. */
1386 g.blank_status = current_unit->flags.blank;
1387 g.sign_status = SIGN_S;
1397 /* Set up the subroutine that will handle the transfers. */
1401 if (current_unit->flags.form == FORM_UNFORMATTED)
1402 transfer = unformatted_read;
1405 if (ioparm.list_format)
1407 transfer = list_formatted_read;
1411 transfer = formatted_transfer;
1416 if (current_unit->flags.form == FORM_UNFORMATTED)
1417 transfer = unformatted_write;
1420 if (ioparm.list_format)
1421 transfer = list_formatted_write;
1423 transfer = formatted_transfer;
1427 /* Make sure that we don't do a read after a nonadvancing write. */
1431 if (current_unit->read_bad)
1433 generate_error (ERROR_BAD_OPTION,
1434 "Cannot READ after a nonadvancing WRITE");
1440 if (advance_status == ADVANCE_YES && !g.seen_dollar)
1441 current_unit->read_bad = 1;
1444 /* Reset counters for T and X-editing. */
1445 max_pos = skips = pending_spaces = 0;
1447 /* Start the data transfer if we are doing a formatted transfer. */
1448 if (current_unit->flags.form == FORM_FORMATTED && !ioparm.list_format
1449 && ioparm.namelist_name == NULL && ionml == NULL)
1450 formatted_transfer (0, NULL, 0, 1);
1454 /* Space to the next record for read mode. If the file is not
1455 seekable, we read MAX_READ chunks until we get to the right
1458 #define MAX_READ 4096
1461 next_record_r (void)
1463 int rlength, length, bytes_left;
1467 switch (current_mode ())
1469 case UNFORMATTED_SEQUENTIAL:
1470 current_unit->bytes_left += sizeof (gfc_offset); /* Skip over tail */
1472 /* Fall through... */
1474 case FORMATTED_DIRECT:
1475 case UNFORMATTED_DIRECT:
1476 if (current_unit->bytes_left == 0)
1479 if (is_seekable (current_unit->s))
1481 new = file_position (current_unit->s) + current_unit->bytes_left;
1483 /* Direct access files do not generate END conditions,
1485 if (sseek (current_unit->s, new) == FAILURE)
1486 generate_error (ERROR_OS, NULL);
1490 { /* Seek by reading data. */
1491 while (current_unit->bytes_left > 0)
1493 rlength = length = (MAX_READ > current_unit->bytes_left) ?
1494 MAX_READ : current_unit->bytes_left;
1496 p = salloc_r (current_unit->s, &rlength);
1499 generate_error (ERROR_OS, NULL);
1503 current_unit->bytes_left -= length;
1508 case FORMATTED_SEQUENTIAL:
1510 /* sf_read has already terminated input because of an '\n' */
1517 if (is_internal_unit())
1519 bytes_left = (int) current_unit->bytes_left;
1520 p = salloc_r (current_unit->s, &bytes_left);
1522 current_unit->bytes_left = current_unit->recl;
1527 p = salloc_r (current_unit->s, &length);
1531 generate_error (ERROR_OS, NULL);
1537 current_unit->endfile = AT_ENDFILE;
1546 if (current_unit->flags.access == ACCESS_SEQUENTIAL)
1547 test_endfile (current_unit);
1551 /* Position to the next record in write mode. */
1554 next_record_w (void)
1557 int length, bytes_left;
1560 /* Zero counters for X- and T-editing. */
1561 max_pos = skips = pending_spaces = 0;
1563 switch (current_mode ())
1565 case FORMATTED_DIRECT:
1566 if (current_unit->bytes_left == 0)
1569 length = current_unit->bytes_left;
1570 p = salloc_w (current_unit->s, &length);
1575 memset (p, ' ', current_unit->bytes_left);
1576 if (sfree (current_unit->s) == FAILURE)
1580 case UNFORMATTED_DIRECT:
1581 if (sfree (current_unit->s) == FAILURE)
1585 case UNFORMATTED_SEQUENTIAL:
1586 m = current_unit->recl - current_unit->bytes_left; /* Bytes written. */
1587 c = file_position (current_unit->s);
1589 length = sizeof (gfc_offset);
1591 /* Write the length tail. */
1593 p = salloc_w (current_unit->s, &length);
1597 memcpy (p, &m, sizeof (gfc_offset));
1598 if (sfree (current_unit->s) == FAILURE)
1601 /* Seek to the head and overwrite the bogus length with the real
1604 p = salloc_w_at (current_unit->s, &length, c - m - length);
1606 generate_error (ERROR_OS, NULL);
1608 memcpy (p, &m, sizeof (gfc_offset));
1609 if (sfree (current_unit->s) == FAILURE)
1612 /* Seek past the end of the current record. */
1614 if (sseek (current_unit->s, c + sizeof (gfc_offset)) == FAILURE)
1619 case FORMATTED_SEQUENTIAL:
1621 if (current_unit->bytes_left == 0)
1624 if (is_internal_unit())
1628 bytes_left = (int) current_unit->bytes_left;
1629 p = salloc_w (current_unit->s, &bytes_left);
1632 generate_error (ERROR_END, NULL);
1635 memset(p, ' ', bytes_left);
1636 current_unit->bytes_left = current_unit->recl;
1641 p = salloc_w (current_unit->s, &length);
1653 p = salloc_w (current_unit->s, &length);
1655 { /* No new line for internal writes. */
1670 generate_error (ERROR_OS, NULL);
1676 /* Position to the next record, which means moving to the end of the
1677 current record. This can happen under several different
1678 conditions. If the done flag is not set, we get ready to process
1682 next_record (int done)
1684 gfc_offset fp; /* File position. */
1686 current_unit->read_bad = 0;
1688 if (g.mode == READING)
1693 /* keep position up to date for INQUIRE */
1694 current_unit->flags.position = POSITION_ASIS;
1696 current_unit->current_record = 0;
1697 if (current_unit->flags.access == ACCESS_DIRECT)
1699 fp = file_position (current_unit->s);
1700 /* Calculate next record, rounding up partial records. */
1701 current_unit->last_record = (fp + current_unit->recl - 1)
1702 / current_unit->recl;
1705 current_unit->last_record++;
1712 /* Finalize the current data transfer. For a nonadvancing transfer,
1713 this means advancing to the next record. For internal units close the
1714 steam associated with the unit. */
1717 finalize_transfer (void)
1722 generate_error (ERROR_EOR, NULL);
1726 if (ioparm.library_return != LIBRARY_OK)
1729 if ((ionml != NULL) && (ioparm.namelist_name != NULL))
1731 if (ioparm.namelist_read_mode)
1738 if (current_unit == NULL)
1741 if (setjmp (g.eof_jump))
1743 generate_error (ERROR_END, NULL);
1747 if (ioparm.list_format && g.mode == READING)
1748 finish_list_read ();
1753 if (advance_status == ADVANCE_NO || g.seen_dollar)
1755 /* Most systems buffer lines, so force the partial record
1756 to be written out. */
1757 flush (current_unit->s);
1763 current_unit->current_record = 0;
1766 sfree (current_unit->s);
1768 if (is_internal_unit ())
1769 sclose (current_unit->s);
1773 /* Transfer function for IOLENGTH. It doesn't actually do any
1774 data transfer, it just updates the length counter. */
1777 iolength_transfer (bt type, void *dest __attribute__ ((unused)),
1778 int len, size_t nelems)
1780 if (ioparm.iolength != NULL)
1782 if (type == BT_COMPLEX)
1783 *ioparm.iolength += 2 * len * nelems;
1785 *ioparm.iolength += len * nelems;
1790 /* Initialize the IOLENGTH data transfer. This function is in essence
1791 a very much simplified version of data_transfer_init(), because it
1792 doesn't have to deal with units at all. */
1795 iolength_transfer_init (void)
1797 if (ioparm.iolength != NULL)
1798 *ioparm.iolength = 0;
1802 /* Set up the subroutine that will handle the transfers. */
1804 transfer = iolength_transfer;
1808 /* Library entry point for the IOLENGTH form of the INQUIRE
1809 statement. The IOLENGTH form requires no I/O to be performed, but
1810 it must still be a runtime library call so that we can determine
1811 the iolength for dynamic arrays and such. */
1813 extern void st_iolength (void);
1814 export_proto(st_iolength);
1820 iolength_transfer_init ();
1823 extern void st_iolength_done (void);
1824 export_proto(st_iolength_done);
1827 st_iolength_done (void)
1833 /* The READ statement. */
1835 extern void st_read (void);
1836 export_proto(st_read);
1844 data_transfer_init (1);
1846 /* Handle complications dealing with the endfile record. It is
1847 significant that this is the only place where ERROR_END is
1848 generated. Reading an end of file elsewhere is either end of
1849 record or an I/O error. */
1851 if (current_unit->flags.access == ACCESS_SEQUENTIAL)
1852 switch (current_unit->endfile)
1858 if (!is_internal_unit())
1860 generate_error (ERROR_END, NULL);
1861 current_unit->endfile = AFTER_ENDFILE;
1866 generate_error (ERROR_ENDFILE, NULL);
1871 extern void st_read_done (void);
1872 export_proto(st_read_done);
1877 finalize_transfer ();
1881 extern void st_write (void);
1882 export_proto(st_write);
1889 data_transfer_init (0);
1892 extern void st_write_done (void);
1893 export_proto(st_write_done);
1896 st_write_done (void)
1898 finalize_transfer ();
1900 /* Deal with endfile conditions associated with sequential files. */
1902 if (current_unit != NULL && current_unit->flags.access == ACCESS_SEQUENTIAL)
1903 switch (current_unit->endfile)
1905 case AT_ENDFILE: /* Remain at the endfile record. */
1909 current_unit->endfile = AT_ENDFILE; /* Just at it now. */
1913 if (current_unit->current_record > current_unit->last_record)
1915 /* Get rid of whatever is after this record. */
1916 if (struncate (current_unit->s) == FAILURE)
1917 generate_error (ERROR_OS, NULL);
1920 current_unit->endfile = AT_ENDFILE;
1927 /* Receives the scalar information for namelist objects and stores it
1928 in a linked list of namelist_info types. */
1930 extern void st_set_nml_var (void * ,char * ,
1931 GFC_INTEGER_4 ,gfc_charlen_type ,GFC_INTEGER_4);
1932 export_proto(st_set_nml_var);
1936 st_set_nml_var (void * var_addr, char * var_name, GFC_INTEGER_4 len,
1937 gfc_charlen_type string_length, GFC_INTEGER_4 dtype)
1939 namelist_info *t1 = NULL;
1942 nml = (namelist_info*) get_mem (sizeof (namelist_info));
1944 nml->mem_pos = var_addr;
1946 nml->var_name = (char*) get_mem (strlen (var_name) + 1);
1947 strcpy (nml->var_name, var_name);
1949 nml->len = (int) len;
1950 nml->string_length = (index_type) string_length;
1952 nml->var_rank = (int) (dtype & GFC_DTYPE_RANK_MASK);
1953 nml->size = (index_type) (dtype >> GFC_DTYPE_SIZE_SHIFT);
1954 nml->type = (bt) ((dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT);
1956 if (nml->var_rank > 0)
1958 nml->dim = (descriptor_dimension*)
1959 get_mem (nml->var_rank * sizeof (descriptor_dimension));
1960 nml->ls = (nml_loop_spec*)
1961 get_mem (nml->var_rank * sizeof (nml_loop_spec));
1975 for (t1 = ionml; t1->next; t1 = t1->next);
1981 /* Store the dimensional information for the namelist object. */
1982 extern void st_set_nml_var_dim (GFC_INTEGER_4, GFC_INTEGER_4,
1983 GFC_INTEGER_4 ,GFC_INTEGER_4);
1984 export_proto(st_set_nml_var_dim);
1987 st_set_nml_var_dim (GFC_INTEGER_4 n_dim, GFC_INTEGER_4 stride,
1988 GFC_INTEGER_4 lbound, GFC_INTEGER_4 ubound)
1990 namelist_info * nml;
1995 for (nml = ionml; nml->next; nml = nml->next);
1997 nml->dim[n].stride = (ssize_t)stride;
1998 nml->dim[n].lbound = (ssize_t)lbound;
1999 nml->dim[n].ubound = (ssize_t)ubound;