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 /* Function for writing a block of bytes to the current file at the
290 current position, advancing the file pointer. We are given a length
291 and return a pointer to a buffer that the caller must (completely)
292 fill in. Returns NULL on error. */
295 write_block (int length)
299 if (current_unit->bytes_left < length)
301 generate_error (ERROR_EOR, NULL);
305 current_unit->bytes_left -= (gfc_offset)length;
306 dest = salloc_w (current_unit->s, &length);
308 if (ioparm.size != NULL)
309 *ioparm.size += length;
315 /* Master function for unformatted reads. */
318 unformatted_read (bt type, void *dest, int length, size_t nelems)
325 /* Transfer functions get passed the kind of the entity, so we have
326 to fix this for COMPLEX data which are twice the size of their
328 if (type == BT_COMPLEX)
332 source = read_block (&w);
336 memcpy (dest, source, w);
338 memset (((char *) dest) + w, ' ', length - w);
342 /* Master function for unformatted writes. */
345 unformatted_write (bt type, void *source, int length, size_t nelems)
350 len = length * nelems;
352 /* Correction for kind vs. length as in unformatted_read. */
353 if (type == BT_COMPLEX)
356 dest = write_block (len);
358 memcpy (dest, source, len);
362 /* Return a pointer to the name of a type. */
387 internal_error ("type_name(): Bad type");
394 /* Write a constant string to the output.
395 This is complicated because the string can have doubled delimiters
396 in it. The length in the format node is the true length. */
399 write_constant_string (fnode * f)
401 char c, delimiter, *p, *q;
404 length = f->u.string.length;
408 p = write_block (length);
415 for (; length > 0; length--)
418 if (c == delimiter && c != 'H' && c != 'h')
419 q++; /* Skip the doubled delimiter. */
424 /* Given actual and expected types in a formatted data transfer, make
425 sure they agree. If not, an error message is generated. Returns
426 nonzero if something went wrong. */
429 require_type (bt expected, bt actual, fnode * f)
433 if (actual == expected)
436 st_sprintf (buffer, "Expected %s for item %d in formatted transfer, got %s",
437 type_name (expected), g.item_count, type_name (actual));
439 format_error (f, buffer);
444 /* This subroutine is the main loop for a formatted data transfer
445 statement. It would be natural to implement this as a coroutine
446 with the user program, but C makes that awkward. We loop,
447 processesing format elements. When we actually have to transfer
448 data instead of just setting flags, we return control to the user
449 program which calls a subroutine that supplies the address and type
450 of the next element, then comes back here to process it. */
453 formatted_transfer_scalar (bt type, void *p, int len)
459 int consume_data_flag;
461 /* Change a complex data item into a pair of reals. */
463 n = (p == NULL) ? 0 : ((type != BT_COMPLEX) ? 1 : 2);
464 if (type == BT_COMPLEX)
467 /* If there's an EOR condition, we simulate finalizing the transfer
474 /* If reversion has occurred and there is another real data item,
475 then we have to move to the next record. */
476 if (g.reversion_flag && n > 0)
478 g.reversion_flag = 0;
482 consume_data_flag = 1 ;
483 if (ioparm.library_return != LIBRARY_OK)
488 return; /* No data descriptors left (already raised). */
490 /* Now discharge T, TR and X movements to the right. This is delayed
491 until a data producing format to suppress trailing spaces. */
493 if (g.mode == WRITING && skips != 0
494 && ((n>0 && ( t == FMT_I || t == FMT_B || t == FMT_O
495 || t == FMT_Z || t == FMT_F || t == FMT_E
496 || t == FMT_EN || t == FMT_ES || t == FMT_G
497 || t == FMT_L || t == FMT_A || t == FMT_D))
502 write_x (skips, pending_spaces);
503 max_pos = (int)(current_unit->recl - current_unit->bytes_left);
507 move_pos_offset (current_unit->s, skips);
508 current_unit->bytes_left -= (gfc_offset)skips;
510 skips = pending_spaces = 0;
513 bytes_used = (int)(current_unit->recl - current_unit->bytes_left);
520 if (require_type (BT_INTEGER, type, f))
523 if (g.mode == READING)
524 read_decimal (f, p, len);
533 if (require_type (BT_INTEGER, type, f))
536 if (g.mode == READING)
537 read_radix (f, p, len, 2);
547 if (g.mode == READING)
548 read_radix (f, p, len, 8);
558 if (g.mode == READING)
559 read_radix (f, p, len, 16);
569 if (g.mode == READING)
580 if (g.mode == READING)
590 if (require_type (BT_REAL, type, f))
593 if (g.mode == READING)
603 if (require_type (BT_REAL, type, f))
606 if (g.mode == READING)
615 if (require_type (BT_REAL, type, f))
618 if (g.mode == READING)
621 write_en (f, p, len);
628 if (require_type (BT_REAL, type, f))
631 if (g.mode == READING)
634 write_es (f, p, len);
641 if (require_type (BT_REAL, type, f))
644 if (g.mode == READING)
654 if (g.mode == READING)
658 read_decimal (f, p, len);
689 internal_error ("formatted_transfer(): Bad type");
695 consume_data_flag = 0 ;
696 if (g.mode == READING)
698 format_error (f, "Constant string in input format");
701 write_constant_string (f);
704 /* Format codes that don't transfer data. */
707 consume_data_flag = 0 ;
709 pos = bytes_used + f->u.n + skips;
710 skips = f->u.n + skips;
711 pending_spaces = pos - max_pos;
713 /* Writes occur just before the switch on f->format, above, so that
714 trailing blanks are suppressed. */
715 if (g.mode == READING)
722 if (f->format == FMT_TL)
723 pos = bytes_used - f->u.n;
726 consume_data_flag = 0;
730 /* Standard 10.6.1.1: excessive left tabbing is reset to the
731 left tab limit. We do not check if the position has gone
732 beyond the end of record because a subsequent tab could
733 bring us back again. */
734 pos = pos < 0 ? 0 : pos;
736 skips = skips + pos - bytes_used;
737 pending_spaces = pending_spaces + pos - max_pos;
742 /* Writes occur just before the switch on f->format, above, so that
743 trailing blanks are suppressed. */
744 if (g.mode == READING)
750 move_pos_offset (current_unit->s, skips);
751 current_unit->bytes_left -= (gfc_offset)skips;
752 skips = pending_spaces = 0;
759 consume_data_flag = 0 ;
760 g.sign_status = SIGN_S;
764 consume_data_flag = 0 ;
765 g.sign_status = SIGN_SS;
769 consume_data_flag = 0 ;
770 g.sign_status = SIGN_SP;
774 consume_data_flag = 0 ;
775 g.blank_status = BLANK_NULL;
779 consume_data_flag = 0 ;
780 g.blank_status = BLANK_ZERO;
784 consume_data_flag = 0 ;
785 g.scale_factor = f->u.k;
789 consume_data_flag = 0 ;
794 consume_data_flag = 0 ;
795 skips = pending_spaces = 0;
800 /* A colon descriptor causes us to exit this loop (in
801 particular preventing another / descriptor from being
802 processed) unless there is another data item to be
804 consume_data_flag = 0 ;
810 internal_error ("Bad format node");
813 /* Free a buffer that we had to allocate during a sequential
814 formatted read of a block that was larger than the static
817 if (line_buffer != NULL)
819 free_mem (line_buffer);
823 /* Adjust the item count and data pointer. */
825 if ((consume_data_flag > 0) && (n > 0))
828 p = ((char *) p) + len;
831 if (g.mode == READING)
834 pos = (int)(current_unit->recl - current_unit->bytes_left);
835 max_pos = (max_pos > pos) ? max_pos : pos;
841 /* Come here when we need a data descriptor but don't have one. We
842 push the current format node back onto the input, then return and
843 let the user program call us back with the data. */
849 formatted_transfer (bt type, void *p, int len, size_t nelems)
857 if (type == BT_COMPLEX)
862 /* Big loop over all the elements. */
863 for (elem = 0; elem < nelems; elem++)
866 formatted_transfer_scalar (type, tmp + size*elem, len);
872 /* Data transfer entry points. The type of the data entity is
873 implicit in the subroutine call. This prevents us from having to
874 share a common enum with the compiler. */
877 transfer_integer (void *p, int kind)
879 if (ioparm.library_return != LIBRARY_OK)
881 transfer (BT_INTEGER, p, kind, 1);
886 transfer_real (void *p, int kind)
888 if (ioparm.library_return != LIBRARY_OK)
890 transfer (BT_REAL, p, kind, 1);
895 transfer_logical (void *p, int kind)
897 if (ioparm.library_return != LIBRARY_OK)
899 transfer (BT_LOGICAL, p, kind, 1);
904 transfer_character (void *p, int len)
906 if (ioparm.library_return != LIBRARY_OK)
908 transfer (BT_CHARACTER, p, len, 1);
913 transfer_complex (void *p, int kind)
915 if (ioparm.library_return != LIBRARY_OK)
917 transfer (BT_COMPLEX, p, kind, 1);
922 transfer_array (gfc_array_char *desc, gfc_charlen_type charlen)
924 index_type count[GFC_MAX_DIMENSIONS];
925 index_type extent[GFC_MAX_DIMENSIONS];
926 index_type stride[GFC_MAX_DIMENSIONS];
927 index_type stride0, rank, size, type, n, kind;
932 if (ioparm.library_return != LIBRARY_OK)
935 type = GFC_DESCRIPTOR_TYPE (desc);
936 size = GFC_DESCRIPTOR_SIZE (desc);
939 /* FIXME: What a kludge: Array descriptors and the IO library use
940 different enums for types. */
943 case GFC_DTYPE_UNKNOWN:
944 iotype = BT_NULL; /* Is this correct? */
946 case GFC_DTYPE_INTEGER:
949 case GFC_DTYPE_LOGICAL:
955 case GFC_DTYPE_COMPLEX:
959 case GFC_DTYPE_CHARACTER:
960 iotype = BT_CHARACTER;
961 /* FIXME: Currently dtype contains the charlen, which is
962 clobbered if charlen > 2**24. That's why we use a separate
963 argument for the charlen. However, if we want to support
964 non-8-bit charsets we need to fix dtype to contain
965 sizeof(chartype) and fix the code below. */
969 case GFC_DTYPE_DERIVED:
970 internal_error ("Derived type I/O should have been handled via the frontend.");
973 internal_error ("transfer_array(): Bad type");
976 if (desc->dim[0].stride == 0)
977 desc->dim[0].stride = 1;
979 rank = GFC_DESCRIPTOR_RANK (desc);
980 for (n = 0; n < rank; n++)
983 stride[n] = desc->dim[n].stride;
984 extent[n] = desc->dim[n].ubound + 1 - desc->dim[n].lbound;
986 /* If the extent of even one dimension is zero, then the entire
987 array section contains zero elements, so we return. */
994 /* If the innermost dimension has stride 1, we can do the transfer
995 in contiguous chunks. */
1001 data = GFC_DESCRIPTOR_DATA (desc);
1005 transfer (iotype, data, kind, tsize);
1006 data += stride0 * size * tsize;
1009 while (count[n] == extent[n])
1012 data -= stride[n] * extent[n] * size;
1022 data += stride[n] * size;
1029 /* Preposition a sequential unformatted file while reading. */
1038 n = sizeof (gfc_offset);
1039 p = salloc_r (current_unit->s, &n);
1042 return; /* end of file */
1044 if (p == NULL || n != sizeof (gfc_offset))
1046 generate_error (ERROR_BAD_US, NULL);
1050 memcpy (&i, p, sizeof (gfc_offset));
1051 current_unit->bytes_left = i;
1055 /* Preposition a sequential unformatted file while writing. This
1056 amount to writing a bogus length that will be filled in later. */
1064 length = sizeof (gfc_offset);
1065 p = salloc_w (current_unit->s, &length);
1069 generate_error (ERROR_OS, NULL);
1073 memset (p, '\0', sizeof (gfc_offset)); /* Bogus value for now. */
1074 if (sfree (current_unit->s) == FAILURE)
1075 generate_error (ERROR_OS, NULL);
1077 /* For sequential unformatted, we write until we have more bytes than
1078 can fit in the record markers. If disk space runs out first, it will
1079 error on the write. */
1080 current_unit->recl = g.max_offset;
1082 current_unit->bytes_left = current_unit->recl;
1086 /* Position to the next record prior to transfer. We are assumed to
1087 be before the next record. We also calculate the bytes in the next
1093 if (current_unit->current_record)
1094 return; /* Already positioned. */
1096 switch (current_mode ())
1098 case UNFORMATTED_SEQUENTIAL:
1099 if (g.mode == READING)
1106 case FORMATTED_SEQUENTIAL:
1107 case FORMATTED_DIRECT:
1108 case UNFORMATTED_DIRECT:
1109 current_unit->bytes_left = current_unit->recl;
1113 current_unit->current_record = 1;
1117 /* Initialize things for a data transfer. This code is common for
1118 both reading and writing. */
1121 data_transfer_init (int read_flag)
1123 unit_flags u_flags; /* Used for creating a unit if needed. */
1125 g.mode = read_flag ? READING : WRITING;
1127 if (ioparm.size != NULL)
1128 *ioparm.size = 0; /* Initialize the count. */
1130 current_unit = get_unit (read_flag);
1131 if (current_unit == NULL)
1132 { /* Open the unit with some default flags. */
1133 if (ioparm.unit < 0)
1135 generate_error (ERROR_BAD_OPTION, "Bad unit number in OPEN statement");
1139 memset (&u_flags, '\0', sizeof (u_flags));
1140 u_flags.access = ACCESS_SEQUENTIAL;
1141 u_flags.action = ACTION_READWRITE;
1142 /* Is it unformatted? */
1143 if (ioparm.format == NULL && !ioparm.list_format)
1144 u_flags.form = FORM_UNFORMATTED;
1146 u_flags.form = FORM_UNSPECIFIED;
1147 u_flags.delim = DELIM_UNSPECIFIED;
1148 u_flags.blank = BLANK_UNSPECIFIED;
1149 u_flags.pad = PAD_UNSPECIFIED;
1150 u_flags.status = STATUS_UNKNOWN;
1152 current_unit = get_unit (read_flag);
1155 if (current_unit == NULL)
1158 /* Check the action. */
1160 if (read_flag && current_unit->flags.action == ACTION_WRITE)
1161 generate_error (ERROR_BAD_ACTION,
1162 "Cannot read from file opened for WRITE");
1164 if (!read_flag && current_unit->flags.action == ACTION_READ)
1165 generate_error (ERROR_BAD_ACTION, "Cannot write to file opened for READ");
1167 if (ioparm.library_return != LIBRARY_OK)
1170 /* Check the format. */
1175 if (ioparm.library_return != LIBRARY_OK)
1178 if (current_unit->flags.form == FORM_UNFORMATTED
1179 && (ioparm.format != NULL || ioparm.list_format))
1180 generate_error (ERROR_OPTION_CONFLICT,
1181 "Format present for UNFORMATTED data transfer");
1183 if (ioparm.namelist_name != NULL && ionml != NULL)
1185 if(ioparm.format != NULL)
1186 generate_error (ERROR_OPTION_CONFLICT,
1187 "A format cannot be specified with a namelist");
1189 else if (current_unit->flags.form == FORM_FORMATTED &&
1190 ioparm.format == NULL && !ioparm.list_format)
1191 generate_error (ERROR_OPTION_CONFLICT,
1192 "Missing format for FORMATTED data transfer");
1195 if (is_internal_unit () && current_unit->flags.form == FORM_UNFORMATTED)
1196 generate_error (ERROR_OPTION_CONFLICT,
1197 "Internal file cannot be accessed by UNFORMATTED data transfer");
1199 /* Check the record number. */
1201 if (current_unit->flags.access == ACCESS_DIRECT && ioparm.rec == 0)
1203 generate_error (ERROR_MISSING_OPTION,
1204 "Direct access data transfer requires record number");
1208 if (current_unit->flags.access == ACCESS_SEQUENTIAL && ioparm.rec != 0)
1210 generate_error (ERROR_OPTION_CONFLICT,
1211 "Record number not allowed for sequential access data transfer");
1215 /* Process the ADVANCE option. */
1217 advance_status = (ioparm.advance == NULL) ? ADVANCE_UNSPECIFIED :
1218 find_option (ioparm.advance, ioparm.advance_len, advance_opt,
1219 "Bad ADVANCE parameter in data transfer statement");
1221 if (advance_status != ADVANCE_UNSPECIFIED)
1223 if (current_unit->flags.access == ACCESS_DIRECT)
1224 generate_error (ERROR_OPTION_CONFLICT,
1225 "ADVANCE specification conflicts with sequential access");
1227 if (is_internal_unit ())
1228 generate_error (ERROR_OPTION_CONFLICT,
1229 "ADVANCE specification conflicts with internal file");
1231 if (ioparm.format == NULL || ioparm.list_format)
1232 generate_error (ERROR_OPTION_CONFLICT,
1233 "ADVANCE specification requires an explicit format");
1238 if (ioparm.eor != 0 && advance_status != ADVANCE_NO)
1239 generate_error (ERROR_MISSING_OPTION,
1240 "EOR specification requires an ADVANCE specification of NO");
1242 if (ioparm.size != NULL && advance_status != ADVANCE_NO)
1243 generate_error (ERROR_MISSING_OPTION,
1244 "SIZE specification requires an ADVANCE specification of NO");
1248 { /* Write constraints. */
1249 if (ioparm.end != 0)
1250 generate_error (ERROR_OPTION_CONFLICT,
1251 "END specification cannot appear in a write statement");
1253 if (ioparm.eor != 0)
1254 generate_error (ERROR_OPTION_CONFLICT,
1255 "EOR specification cannot appear in a write statement");
1257 if (ioparm.size != 0)
1258 generate_error (ERROR_OPTION_CONFLICT,
1259 "SIZE specification cannot appear in a write statement");
1262 if (advance_status == ADVANCE_UNSPECIFIED)
1263 advance_status = ADVANCE_YES;
1264 if (ioparm.library_return != LIBRARY_OK)
1267 /* Sanity checks on the record number. */
1271 if (ioparm.rec <= 0)
1273 generate_error (ERROR_BAD_OPTION, "Record number must be positive");
1277 if (ioparm.rec >= current_unit->maxrec)
1279 generate_error (ERROR_BAD_OPTION, "Record number too large");
1283 /* Check to see if we might be reading what we wrote before */
1285 if (g.mode == READING && current_unit->mode == WRITING)
1286 flush(current_unit->s);
1288 /* Check whether the record exists to be read. Only
1289 a partial record needs to exist. */
1291 if (g.mode == READING && (ioparm.rec -1)
1292 * current_unit->recl >= file_length (current_unit->s))
1294 generate_error (ERROR_BAD_OPTION, "Non-existing record number");
1298 /* Position the file. */
1299 if (sseek (current_unit->s,
1300 (ioparm.rec - 1) * current_unit->recl) == FAILURE)
1302 generate_error (ERROR_OS, NULL);
1307 /* Overwriting an existing sequential file ?
1308 it is always safe to truncate the file on the first write */
1309 if (g.mode == WRITING
1310 && current_unit->flags.access == ACCESS_SEQUENTIAL
1311 && current_unit->last_record == 0 && !is_preconnected(current_unit->s))
1312 struncate(current_unit->s);
1314 current_unit->mode = g.mode;
1316 /* Set the initial value of flags. */
1318 g.blank_status = current_unit->flags.blank;
1319 g.sign_status = SIGN_S;
1329 /* Set up the subroutine that will handle the transfers. */
1333 if (current_unit->flags.form == FORM_UNFORMATTED)
1334 transfer = unformatted_read;
1337 if (ioparm.list_format)
1339 transfer = list_formatted_read;
1343 transfer = formatted_transfer;
1348 if (current_unit->flags.form == FORM_UNFORMATTED)
1349 transfer = unformatted_write;
1352 if (ioparm.list_format)
1353 transfer = list_formatted_write;
1355 transfer = formatted_transfer;
1359 /* Make sure that we don't do a read after a nonadvancing write. */
1363 if (current_unit->read_bad)
1365 generate_error (ERROR_BAD_OPTION,
1366 "Cannot READ after a nonadvancing WRITE");
1372 if (advance_status == ADVANCE_YES && !g.seen_dollar)
1373 current_unit->read_bad = 1;
1376 /* Reset counters for T and X-editing. */
1377 max_pos = skips = pending_spaces = 0;
1379 /* Start the data transfer if we are doing a formatted transfer. */
1380 if (current_unit->flags.form == FORM_FORMATTED && !ioparm.list_format
1381 && ioparm.namelist_name == NULL && ionml == NULL)
1382 formatted_transfer (0, NULL, 0, 1);
1386 /* Space to the next record for read mode. If the file is not
1387 seekable, we read MAX_READ chunks until we get to the right
1390 #define MAX_READ 4096
1393 next_record_r (void)
1395 int rlength, length, bytes_left;
1399 switch (current_mode ())
1401 case UNFORMATTED_SEQUENTIAL:
1402 current_unit->bytes_left += sizeof (gfc_offset); /* Skip over tail */
1404 /* Fall through... */
1406 case FORMATTED_DIRECT:
1407 case UNFORMATTED_DIRECT:
1408 if (current_unit->bytes_left == 0)
1411 if (is_seekable (current_unit->s))
1413 new = file_position (current_unit->s) + current_unit->bytes_left;
1415 /* Direct access files do not generate END conditions,
1417 if (sseek (current_unit->s, new) == FAILURE)
1418 generate_error (ERROR_OS, NULL);
1422 { /* Seek by reading data. */
1423 while (current_unit->bytes_left > 0)
1425 rlength = length = (MAX_READ > current_unit->bytes_left) ?
1426 MAX_READ : current_unit->bytes_left;
1428 p = salloc_r (current_unit->s, &rlength);
1431 generate_error (ERROR_OS, NULL);
1435 current_unit->bytes_left -= length;
1440 case FORMATTED_SEQUENTIAL:
1442 /* sf_read has already terminated input because of an '\n' */
1449 if (is_internal_unit())
1451 bytes_left = (int) current_unit->bytes_left;
1452 p = salloc_r (current_unit->s, &bytes_left);
1454 current_unit->bytes_left = current_unit->recl;
1459 p = salloc_r (current_unit->s, &length);
1463 generate_error (ERROR_OS, NULL);
1469 current_unit->endfile = AT_ENDFILE;
1478 if (current_unit->flags.access == ACCESS_SEQUENTIAL)
1479 test_endfile (current_unit);
1483 /* Position to the next record in write mode. */
1486 next_record_w (void)
1489 int length, bytes_left;
1492 /* Zero counters for X- and T-editing. */
1493 max_pos = skips = pending_spaces = 0;
1495 switch (current_mode ())
1497 case FORMATTED_DIRECT:
1498 if (current_unit->bytes_left == 0)
1501 length = current_unit->bytes_left;
1502 p = salloc_w (current_unit->s, &length);
1507 memset (p, ' ', current_unit->bytes_left);
1508 if (sfree (current_unit->s) == FAILURE)
1512 case UNFORMATTED_DIRECT:
1513 if (sfree (current_unit->s) == FAILURE)
1517 case UNFORMATTED_SEQUENTIAL:
1518 m = current_unit->recl - current_unit->bytes_left; /* Bytes written. */
1519 c = file_position (current_unit->s);
1521 length = sizeof (gfc_offset);
1523 /* Write the length tail. */
1525 p = salloc_w (current_unit->s, &length);
1529 memcpy (p, &m, sizeof (gfc_offset));
1530 if (sfree (current_unit->s) == FAILURE)
1533 /* Seek to the head and overwrite the bogus length with the real
1536 p = salloc_w_at (current_unit->s, &length, c - m - length);
1538 generate_error (ERROR_OS, NULL);
1540 memcpy (p, &m, sizeof (gfc_offset));
1541 if (sfree (current_unit->s) == FAILURE)
1544 /* Seek past the end of the current record. */
1546 if (sseek (current_unit->s, c + sizeof (gfc_offset)) == FAILURE)
1551 case FORMATTED_SEQUENTIAL:
1553 if (current_unit->bytes_left == 0)
1556 if (is_internal_unit())
1560 bytes_left = (int) current_unit->bytes_left;
1561 p = salloc_w (current_unit->s, &bytes_left);
1564 memset(p, ' ', bytes_left);
1565 current_unit->bytes_left = current_unit->recl;
1571 p = salloc_w (current_unit->s, &length);
1581 p = salloc_w (current_unit->s, &length);
1583 { /* No new line for internal writes. */
1598 generate_error (ERROR_OS, NULL);
1604 /* Position to the next record, which means moving to the end of the
1605 current record. This can happen under several different
1606 conditions. If the done flag is not set, we get ready to process
1610 next_record (int done)
1612 gfc_offset fp; /* File position. */
1614 current_unit->read_bad = 0;
1616 if (g.mode == READING)
1621 /* keep position up to date for INQUIRE */
1622 current_unit->flags.position = POSITION_ASIS;
1624 current_unit->current_record = 0;
1625 if (current_unit->flags.access == ACCESS_DIRECT)
1627 fp = file_position (current_unit->s);
1628 /* Calculate next record, rounding up partial records. */
1629 current_unit->last_record = (fp + current_unit->recl - 1)
1630 / current_unit->recl;
1633 current_unit->last_record++;
1640 /* Finalize the current data transfer. For a nonadvancing transfer,
1641 this means advancing to the next record. For internal units close the
1642 steam associated with the unit. */
1645 finalize_transfer (void)
1650 generate_error (ERROR_EOR, NULL);
1654 if (ioparm.library_return != LIBRARY_OK)
1657 if ((ionml != NULL) && (ioparm.namelist_name != NULL))
1659 if (ioparm.namelist_read_mode)
1666 if (current_unit == NULL)
1669 if (setjmp (g.eof_jump))
1671 generate_error (ERROR_END, NULL);
1675 if (ioparm.list_format && g.mode == READING)
1676 finish_list_read ();
1681 if (advance_status == ADVANCE_NO || g.seen_dollar)
1683 /* Most systems buffer lines, so force the partial record
1684 to be written out. */
1685 flush (current_unit->s);
1691 current_unit->current_record = 0;
1694 sfree (current_unit->s);
1696 if (is_internal_unit ())
1697 sclose (current_unit->s);
1701 /* Transfer function for IOLENGTH. It doesn't actually do any
1702 data transfer, it just updates the length counter. */
1705 iolength_transfer (bt type, void *dest __attribute__ ((unused)),
1706 int len, size_t nelems)
1708 if (ioparm.iolength != NULL)
1710 if (type == BT_COMPLEX)
1711 *ioparm.iolength += 2 * len * nelems;
1713 *ioparm.iolength += len * nelems;
1718 /* Initialize the IOLENGTH data transfer. This function is in essence
1719 a very much simplified version of data_transfer_init(), because it
1720 doesn't have to deal with units at all. */
1723 iolength_transfer_init (void)
1725 if (ioparm.iolength != NULL)
1726 *ioparm.iolength = 0;
1730 /* Set up the subroutine that will handle the transfers. */
1732 transfer = iolength_transfer;
1736 /* Library entry point for the IOLENGTH form of the INQUIRE
1737 statement. The IOLENGTH form requires no I/O to be performed, but
1738 it must still be a runtime library call so that we can determine
1739 the iolength for dynamic arrays and such. */
1741 extern void st_iolength (void);
1742 export_proto(st_iolength);
1748 iolength_transfer_init ();
1751 extern void st_iolength_done (void);
1752 export_proto(st_iolength_done);
1755 st_iolength_done (void)
1761 /* The READ statement. */
1763 extern void st_read (void);
1764 export_proto(st_read);
1772 data_transfer_init (1);
1774 /* Handle complications dealing with the endfile record. It is
1775 significant that this is the only place where ERROR_END is
1776 generated. Reading an end of file elsewhere is either end of
1777 record or an I/O error. */
1779 if (current_unit->flags.access == ACCESS_SEQUENTIAL)
1780 switch (current_unit->endfile)
1786 if (!is_internal_unit())
1788 generate_error (ERROR_END, NULL);
1789 current_unit->endfile = AFTER_ENDFILE;
1794 generate_error (ERROR_ENDFILE, NULL);
1799 extern void st_read_done (void);
1800 export_proto(st_read_done);
1805 finalize_transfer ();
1809 extern void st_write (void);
1810 export_proto(st_write);
1817 data_transfer_init (0);
1820 extern void st_write_done (void);
1821 export_proto(st_write_done);
1824 st_write_done (void)
1826 finalize_transfer ();
1828 /* Deal with endfile conditions associated with sequential files. */
1830 if (current_unit != NULL && current_unit->flags.access == ACCESS_SEQUENTIAL)
1831 switch (current_unit->endfile)
1833 case AT_ENDFILE: /* Remain at the endfile record. */
1837 current_unit->endfile = AT_ENDFILE; /* Just at it now. */
1841 if (current_unit->current_record > current_unit->last_record)
1843 /* Get rid of whatever is after this record. */
1844 if (struncate (current_unit->s) == FAILURE)
1845 generate_error (ERROR_OS, NULL);
1848 current_unit->endfile = AT_ENDFILE;
1855 /* Receives the scalar information for namelist objects and stores it
1856 in a linked list of namelist_info types. */
1858 extern void st_set_nml_var (void * ,char * ,
1859 GFC_INTEGER_4 ,gfc_charlen_type ,GFC_INTEGER_4);
1860 export_proto(st_set_nml_var);
1864 st_set_nml_var (void * var_addr, char * var_name, GFC_INTEGER_4 len,
1865 gfc_charlen_type string_length, GFC_INTEGER_4 dtype)
1867 namelist_info *t1 = NULL;
1870 nml = (namelist_info*) get_mem (sizeof (namelist_info));
1872 nml->mem_pos = var_addr;
1874 nml->var_name = (char*) get_mem (strlen (var_name) + 1);
1875 strcpy (nml->var_name, var_name);
1877 nml->len = (int) len;
1878 nml->string_length = (index_type) string_length;
1880 nml->var_rank = (int) (dtype & GFC_DTYPE_RANK_MASK);
1881 nml->size = (index_type) (dtype >> GFC_DTYPE_SIZE_SHIFT);
1882 nml->type = (bt) ((dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT);
1884 if (nml->var_rank > 0)
1886 nml->dim = (descriptor_dimension*)
1887 get_mem (nml->var_rank * sizeof (descriptor_dimension));
1888 nml->ls = (nml_loop_spec*)
1889 get_mem (nml->var_rank * sizeof (nml_loop_spec));
1903 for (t1 = ionml; t1->next; t1 = t1->next);
1909 /* Store the dimensional information for the namelist object. */
1910 extern void st_set_nml_var_dim (GFC_INTEGER_4, GFC_INTEGER_4,
1911 GFC_INTEGER_4 ,GFC_INTEGER_4);
1912 export_proto(st_set_nml_var_dim);
1915 st_set_nml_var_dim (GFC_INTEGER_4 n_dim, GFC_INTEGER_4 stride,
1916 GFC_INTEGER_4 lbound, GFC_INTEGER_4 ubound)
1918 namelist_info * nml;
1923 for (nml = ionml; nml->next; nml = nml->next);
1925 nml->dim[n].stride = (ssize_t)stride;
1926 nml->dim[n].lbound = (ssize_t)lbound;
1927 nml->dim[n].ubound = (ssize_t)ubound;