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, 59 Temple Place - Suite 330,
29 Boston, MA 02111-1307, 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 gfc_unit *current_unit = NULL;
82 static int sf_seen_eor = 0;
83 static int eor_condition = 0;
85 char scratch[SCRATCH_SIZE];
86 static char *line_buffer = NULL;
88 static unit_advance advance_status;
90 static st_option advance_opt[] = {
97 static void (*transfer) (bt, void *, int);
101 { FORMATTED_SEQUENTIAL, UNFORMATTED_SEQUENTIAL,
102 FORMATTED_DIRECT, UNFORMATTED_DIRECT
112 if (current_unit->flags.access == ACCESS_DIRECT)
114 m = current_unit->flags.form == FORM_FORMATTED ?
115 FORMATTED_DIRECT : UNFORMATTED_DIRECT;
119 m = current_unit->flags.form == FORM_FORMATTED ?
120 FORMATTED_SEQUENTIAL : UNFORMATTED_SEQUENTIAL;
127 /* Mid level data transfer statements. These subroutines do reading
128 and writing in the style of salloc_r()/salloc_w() within the
131 /* When reading sequential formatted records we have a problem. We
132 don't know how long the line is until we read the trailing newline,
133 and we don't want to read too much. If we read too much, we might
134 have to do a physical seek backwards depending on how much data is
135 present, and devices like terminals aren't seekable and would cause
138 Given this, the solution is to read a byte at a time, stopping if
139 we hit the newline. For small locations, we use a static buffer.
140 For larger allocations, we are forced to allocate memory on the
141 heap. Hopefully this won't happen very often. */
144 read_sf (int *length)
146 static char data[SCRATCH_SIZE];
150 if (*length > SCRATCH_SIZE)
151 p = base = line_buffer = get_mem (*length);
155 /* If we have seen an eor previously, return a length of 0. The
156 caller is responsible for correctly padding the input field. */
168 if (is_internal_unit())
170 /* readlen may be modified inside salloc_r if
171 is_internal_unit() is true. */
175 q = salloc_r (current_unit->s, &readlen);
179 /* If we have a line without a terminating \n, drop through to
181 if (readlen < 1 && n == 0)
183 generate_error (ERROR_END, NULL);
187 if (readlen < 1 || *q == '\n' || *q == '\r')
189 /* Unexpected end of line. */
191 /* If we see an EOR during non-advancing I/O, we need to skip
192 the rest of the I/O statement. Set the corresponding flag. */
193 if (advance_status == ADVANCE_NO || g.seen_dollar)
196 /* Without padding, terminate the I/O statement without assigning
197 the value. With padding, the value still needs to be assigned,
198 so we can just continue with a short read. */
199 if (current_unit->flags.pad == PAD_NO)
201 generate_error (ERROR_EOR, NULL);
205 current_unit->bytes_left = 0;
216 current_unit->bytes_left -= *length;
218 if (ioparm.size != NULL)
219 *ioparm.size += *length;
225 /* Function for reading the next couple of bytes from the current
226 file, advancing the current position. We return a pointer to a
227 buffer containing the bytes. We return NULL on end of record or
230 If the read is short, then it is because the current record does not
231 have enough data to satisfy the read request and the file was
232 opened with PAD=YES. The caller must assume tailing spaces for
236 read_block (int *length)
241 if (current_unit->flags.form == FORM_FORMATTED &&
242 current_unit->flags.access == ACCESS_SEQUENTIAL)
243 return read_sf (length); /* Special case. */
245 if (current_unit->bytes_left < *length)
247 if (current_unit->flags.pad == PAD_NO)
249 generate_error (ERROR_EOR, NULL); /* Not enough data left. */
253 *length = current_unit->bytes_left;
256 current_unit->bytes_left -= *length;
259 source = salloc_r (current_unit->s, &nread);
261 if (ioparm.size != NULL)
262 *ioparm.size += nread;
264 if (nread != *length)
265 { /* Short read, this shouldn't happen. */
266 if (current_unit->flags.pad == PAD_YES)
270 generate_error (ERROR_EOR, NULL);
279 /* Function for writing a block of bytes to the current file at the
280 current position, advancing the file pointer. We are given a length
281 and return a pointer to a buffer that the caller must (completely)
282 fill in. Returns NULL on error. */
285 write_block (int length)
289 if (!is_internal_unit() && current_unit->bytes_left < length)
291 generate_error (ERROR_EOR, NULL);
295 current_unit->bytes_left -= length;
296 dest = salloc_w (current_unit->s, &length);
298 if (ioparm.size != NULL)
299 *ioparm.size += length;
305 /* Master function for unformatted reads. */
308 unformatted_read (bt type, void *dest, int length)
313 /* Transfer functions get passed the kind of the entity, so we have
314 to fix this for COMPLEX data which are twice the size of their
316 if (type == BT_COMPLEX)
320 source = read_block (&w);
324 memcpy (dest, source, w);
326 memset (((char *) dest) + w, ' ', length - w);
330 /* Master function for unformatted writes. */
333 unformatted_write (bt type, void *source, int length)
337 /* Correction for kind vs. length as in unformatted_read. */
338 if (type == BT_COMPLEX)
341 dest = write_block (length);
343 memcpy (dest, source, length);
347 /* Return a pointer to the name of a type. */
372 internal_error ("type_name(): Bad type");
379 /* Write a constant string to the output.
380 This is complicated because the string can have doubled delimiters
381 in it. The length in the format node is the true length. */
384 write_constant_string (fnode * f)
386 char c, delimiter, *p, *q;
389 length = f->u.string.length;
393 p = write_block (length);
400 for (; length > 0; length--)
403 if (c == delimiter && c != 'H' && c != 'h')
404 q++; /* Skip the doubled delimiter. */
409 /* Given actual and expected types in a formatted data transfer, make
410 sure they agree. If not, an error message is generated. Returns
411 nonzero if something went wrong. */
414 require_type (bt expected, bt actual, fnode * f)
418 if (actual == expected)
421 st_sprintf (buffer, "Expected %s for item %d in formatted transfer, got %s",
422 type_name (expected), g.item_count, type_name (actual));
424 format_error (f, buffer);
429 /* This subroutine is the main loop for a formatted data transfer
430 statement. It would be natural to implement this as a coroutine
431 with the user program, but C makes that awkward. We loop,
432 processesing format elements. When we actually have to transfer
433 data instead of just setting flags, we return control to the user
434 program which calls a subroutine that supplies the address and type
435 of the next element, then comes back here to process it. */
438 formatted_transfer (bt type, void *p, int len)
443 int consume_data_flag;
445 /* Change a complex data item into a pair of reals. */
447 n = (p == NULL) ? 0 : ((type != BT_COMPLEX) ? 1 : 2);
448 if (type == BT_COMPLEX)
451 /* If there's an EOR condition, we simulate finalizing the transfer
458 /* If reversion has occurred and there is another real data item,
459 then we have to move to the next record. */
460 if (g.reversion_flag && n > 0)
462 g.reversion_flag = 0;
466 consume_data_flag = 1 ;
467 if (ioparm.library_return != LIBRARY_OK)
472 return; /* No data descriptors left (already raised). */
479 if (require_type (BT_INTEGER, type, f))
482 if (g.mode == READING)
483 read_decimal (f, p, len);
492 if (require_type (BT_INTEGER, type, f))
495 if (g.mode == READING)
496 read_radix (f, p, len, 2);
506 if (g.mode == READING)
507 read_radix (f, p, len, 8);
517 if (g.mode == READING)
518 read_radix (f, p, len, 16);
527 if (require_type (BT_CHARACTER, type, f))
530 if (g.mode == READING)
541 if (g.mode == READING)
551 if (require_type (BT_REAL, type, f))
554 if (g.mode == READING)
564 if (require_type (BT_REAL, type, f))
567 if (g.mode == READING)
576 if (require_type (BT_REAL, type, f))
579 if (g.mode == READING)
582 write_en (f, p, len);
589 if (require_type (BT_REAL, type, f))
592 if (g.mode == READING)
595 write_es (f, p, len);
602 if (require_type (BT_REAL, type, f))
605 if (g.mode == READING)
615 if (g.mode == READING)
619 read_decimal (f, p, len);
650 internal_error ("formatted_transfer(): Bad type");
656 consume_data_flag = 0 ;
657 if (g.mode == READING)
659 format_error (f, "Constant string in input format");
662 write_constant_string (f);
665 /* Format codes that don't transfer data. */
668 consume_data_flag = 0 ;
669 if (g.mode == READING)
678 if (f->format == FMT_TL)
679 pos = current_unit->recl - current_unit->bytes_left - f->u.n;
682 consume_data_flag = 0;
686 if (pos < 0 || pos >= current_unit->recl )
688 generate_error (ERROR_EOR, "T Or TL edit position error");
691 m = pos - (current_unit->recl - current_unit->bytes_left);
699 if (g.mode == READING)
706 move_pos_offset (current_unit->s,m);
707 current_unit->bytes_left -= m;
713 consume_data_flag = 0 ;
714 g.sign_status = SIGN_S;
718 consume_data_flag = 0 ;
719 g.sign_status = SIGN_SS;
723 consume_data_flag = 0 ;
724 g.sign_status = SIGN_SP;
728 consume_data_flag = 0 ;
729 g.blank_status = BLANK_NULL;
733 consume_data_flag = 0 ;
734 g.blank_status = BLANK_ZERO;
738 consume_data_flag = 0 ;
739 g.scale_factor = f->u.k;
743 consume_data_flag = 0 ;
748 consume_data_flag = 0 ;
749 for (i = 0; i < f->repeat; i++)
755 /* A colon descriptor causes us to exit this loop (in
756 particular preventing another / descriptor from being
757 processed) unless there is another data item to be
759 consume_data_flag = 0 ;
765 internal_error ("Bad format node");
768 /* Free a buffer that we had to allocate during a sequential
769 formatted read of a block that was larger than the static
772 if (line_buffer != NULL)
774 free_mem (line_buffer);
778 /* Adjust the item count and data pointer. */
780 if ((consume_data_flag > 0) && (n > 0))
783 p = ((char *) p) + len;
789 /* Come here when we need a data descriptor but don't have one. We
790 push the current format node back onto the input, then return and
791 let the user program call us back with the data. */
797 /* Data transfer entry points. The type of the data entity is
798 implicit in the subroutine call. This prevents us from having to
799 share a common enum with the compiler. */
802 transfer_integer (void *p, int kind)
805 if (ioparm.library_return != LIBRARY_OK)
807 transfer (BT_INTEGER, p, kind);
812 transfer_real (void *p, int kind)
815 if (ioparm.library_return != LIBRARY_OK)
817 transfer (BT_REAL, p, kind);
822 transfer_logical (void *p, int kind)
825 if (ioparm.library_return != LIBRARY_OK)
827 transfer (BT_LOGICAL, p, kind);
832 transfer_character (void *p, int len)
835 if (ioparm.library_return != LIBRARY_OK)
837 transfer (BT_CHARACTER, p, len);
842 transfer_complex (void *p, int kind)
845 if (ioparm.library_return != LIBRARY_OK)
847 transfer (BT_COMPLEX, p, kind);
851 /* Preposition a sequential unformatted file while reading. */
860 n = sizeof (gfc_offset);
861 p = salloc_r (current_unit->s, &n);
864 return; /* end of file */
866 if (p == NULL || n != sizeof (gfc_offset))
868 generate_error (ERROR_BAD_US, NULL);
872 memcpy (&i, p, sizeof (gfc_offset));
873 current_unit->bytes_left = i;
877 /* Preposition a sequential unformatted file while writing. This
878 amount to writing a bogus length that will be filled in later. */
886 length = sizeof (gfc_offset);
887 p = salloc_w (current_unit->s, &length);
891 generate_error (ERROR_OS, NULL);
895 memset (p, '\0', sizeof (gfc_offset)); /* Bogus value for now. */
896 if (sfree (current_unit->s) == FAILURE)
897 generate_error (ERROR_OS, NULL);
899 /* For sequential unformatted, we write until we have more bytes than
900 can fit in the record markers. If disk space runs out first, it will
901 error on the write. */
902 current_unit->recl = g.max_offset;
904 current_unit->bytes_left = current_unit->recl;
908 /* Position to the next record prior to transfer. We are assumed to
909 be before the next record. We also calculate the bytes in the next
915 if (current_unit->current_record)
916 return; /* Already positioned. */
918 switch (current_mode ())
920 case UNFORMATTED_SEQUENTIAL:
921 if (g.mode == READING)
928 case FORMATTED_SEQUENTIAL:
929 case FORMATTED_DIRECT:
930 case UNFORMATTED_DIRECT:
931 current_unit->bytes_left = current_unit->recl;
935 current_unit->current_record = 1;
939 /* Initialize things for a data transfer. This code is common for
940 both reading and writing. */
943 data_transfer_init (int read_flag)
945 unit_flags u_flags; /* Used for creating a unit if needed. */
947 g.mode = read_flag ? READING : WRITING;
949 if (ioparm.size != NULL)
950 *ioparm.size = 0; /* Initialize the count. */
952 current_unit = get_unit (read_flag);
953 if (current_unit == NULL)
954 { /* Open the unit with some default flags. */
957 generate_error (ERROR_BAD_OPTION, "Bad unit number in OPEN statement");
961 memset (&u_flags, '\0', sizeof (u_flags));
962 u_flags.access = ACCESS_SEQUENTIAL;
963 u_flags.action = ACTION_READWRITE;
964 /* Is it unformatted? */
965 if (ioparm.format == NULL && !ioparm.list_format)
966 u_flags.form = FORM_UNFORMATTED;
968 u_flags.form = FORM_UNSPECIFIED;
969 u_flags.delim = DELIM_UNSPECIFIED;
970 u_flags.blank = BLANK_UNSPECIFIED;
971 u_flags.pad = PAD_UNSPECIFIED;
972 u_flags.status = STATUS_UNKNOWN;
974 current_unit = get_unit (read_flag);
977 if (current_unit == NULL)
980 if (is_internal_unit())
982 current_unit->recl = file_length(current_unit->s);
984 empty_internal_buffer (current_unit->s);
987 /* Check the action. */
989 if (read_flag && current_unit->flags.action == ACTION_WRITE)
990 generate_error (ERROR_BAD_ACTION,
991 "Cannot read from file opened for WRITE");
993 if (!read_flag && current_unit->flags.action == ACTION_READ)
994 generate_error (ERROR_BAD_ACTION, "Cannot write to file opened for READ");
996 if (ioparm.library_return != LIBRARY_OK)
999 /* Check the format. */
1004 if (ioparm.library_return != LIBRARY_OK)
1007 if (current_unit->flags.form == FORM_UNFORMATTED
1008 && (ioparm.format != NULL || ioparm.list_format))
1009 generate_error (ERROR_OPTION_CONFLICT,
1010 "Format present for UNFORMATTED data transfer");
1012 if (ioparm.namelist_name != NULL && ionml != NULL)
1014 if(ioparm.format != NULL)
1015 generate_error (ERROR_OPTION_CONFLICT,
1016 "A format cannot be specified with a namelist");
1018 else if (current_unit->flags.form == FORM_FORMATTED &&
1019 ioparm.format == NULL && !ioparm.list_format)
1020 generate_error (ERROR_OPTION_CONFLICT,
1021 "Missing format for FORMATTED data transfer");
1024 if (is_internal_unit () && current_unit->flags.form == FORM_UNFORMATTED)
1025 generate_error (ERROR_OPTION_CONFLICT,
1026 "Internal file cannot be accessed by UNFORMATTED data transfer");
1028 /* Check the record number. */
1030 if (current_unit->flags.access == ACCESS_DIRECT && ioparm.rec == 0)
1032 generate_error (ERROR_MISSING_OPTION,
1033 "Direct access data transfer requires record number");
1037 if (current_unit->flags.access == ACCESS_SEQUENTIAL && ioparm.rec != 0)
1039 generate_error (ERROR_OPTION_CONFLICT,
1040 "Record number not allowed for sequential access data transfer");
1044 /* Process the ADVANCE option. */
1046 advance_status = (ioparm.advance == NULL) ? ADVANCE_UNSPECIFIED :
1047 find_option (ioparm.advance, ioparm.advance_len, advance_opt,
1048 "Bad ADVANCE parameter in data transfer statement");
1050 if (advance_status != ADVANCE_UNSPECIFIED)
1052 if (current_unit->flags.access == ACCESS_DIRECT)
1053 generate_error (ERROR_OPTION_CONFLICT,
1054 "ADVANCE specification conflicts with sequential access");
1056 if (is_internal_unit ())
1057 generate_error (ERROR_OPTION_CONFLICT,
1058 "ADVANCE specification conflicts with internal file");
1060 if (ioparm.format == NULL || ioparm.list_format)
1061 generate_error (ERROR_OPTION_CONFLICT,
1062 "ADVANCE specification requires an explicit format");
1067 if (ioparm.eor != 0 && advance_status != ADVANCE_NO)
1068 generate_error (ERROR_MISSING_OPTION,
1069 "EOR specification requires an ADVANCE specification of NO");
1071 if (ioparm.size != NULL && advance_status != ADVANCE_NO)
1072 generate_error (ERROR_MISSING_OPTION,
1073 "SIZE specification requires an ADVANCE specification of NO");
1077 { /* Write constraints. */
1078 if (ioparm.end != 0)
1079 generate_error (ERROR_OPTION_CONFLICT,
1080 "END specification cannot appear in a write statement");
1082 if (ioparm.eor != 0)
1083 generate_error (ERROR_OPTION_CONFLICT,
1084 "EOR specification cannot appear in a write statement");
1086 if (ioparm.size != 0)
1087 generate_error (ERROR_OPTION_CONFLICT,
1088 "SIZE specification cannot appear in a write statement");
1091 if (advance_status == ADVANCE_UNSPECIFIED)
1092 advance_status = ADVANCE_YES;
1093 if (ioparm.library_return != LIBRARY_OK)
1096 /* Sanity checks on the record number. */
1100 if (ioparm.rec <= 0)
1102 generate_error (ERROR_BAD_OPTION, "Record number must be positive");
1106 if (ioparm.rec >= current_unit->maxrec)
1108 generate_error (ERROR_BAD_OPTION, "Record number too large");
1112 /* Check to see if we might be reading what we wrote before */
1114 if (g.mode == READING && current_unit->mode == WRITING)
1115 flush(current_unit->s);
1117 /* Position the file. */
1118 if (sseek (current_unit->s,
1119 (ioparm.rec - 1) * current_unit->recl) == FAILURE)
1120 generate_error (ERROR_OS, NULL);
1123 /* Overwriting an existing sequential file ?
1124 it is always safe to truncate the file on the first write */
1125 if (g.mode == WRITING
1126 && current_unit->flags.access == ACCESS_SEQUENTIAL
1127 && current_unit->current_record == 0)
1128 struncate(current_unit->s);
1130 current_unit->mode = g.mode;
1132 /* Set the initial value of flags. */
1134 g.blank_status = current_unit->flags.blank;
1135 g.sign_status = SIGN_S;
1145 /* Set up the subroutine that will handle the transfers. */
1149 if (current_unit->flags.form == FORM_UNFORMATTED)
1150 transfer = unformatted_read;
1153 if (ioparm.list_format)
1155 transfer = list_formatted_read;
1159 transfer = formatted_transfer;
1164 if (current_unit->flags.form == FORM_UNFORMATTED)
1165 transfer = unformatted_write;
1168 if (ioparm.list_format)
1169 transfer = list_formatted_write;
1171 transfer = formatted_transfer;
1175 /* Make sure that we don't do a read after a nonadvancing write. */
1179 if (current_unit->read_bad)
1181 generate_error (ERROR_BAD_OPTION,
1182 "Cannot READ after a nonadvancing WRITE");
1188 if (advance_status == ADVANCE_YES && !g.seen_dollar)
1189 current_unit->read_bad = 1;
1192 /* Start the data transfer if we are doing a formatted transfer. */
1193 if (current_unit->flags.form == FORM_FORMATTED && !ioparm.list_format
1194 && ioparm.namelist_name == NULL && ionml == NULL)
1195 formatted_transfer (0, NULL, 0);
1199 /* Space to the next record for read mode. If the file is not
1200 seekable, we read MAX_READ chunks until we get to the right
1203 #define MAX_READ 4096
1206 next_record_r (void)
1208 int rlength, length;
1212 switch (current_mode ())
1214 case UNFORMATTED_SEQUENTIAL:
1215 current_unit->bytes_left += sizeof (gfc_offset); /* Skip over tail */
1217 /* Fall through... */
1219 case FORMATTED_DIRECT:
1220 case UNFORMATTED_DIRECT:
1221 if (current_unit->bytes_left == 0)
1224 if (is_seekable (current_unit->s))
1226 new = file_position (current_unit->s) + current_unit->bytes_left;
1228 /* Direct access files do not generate END conditions,
1230 if (sseek (current_unit->s, new) == FAILURE)
1231 generate_error (ERROR_OS, NULL);
1235 { /* Seek by reading data. */
1236 while (current_unit->bytes_left > 0)
1238 rlength = length = (MAX_READ > current_unit->bytes_left) ?
1239 MAX_READ : current_unit->bytes_left;
1241 p = salloc_r (current_unit->s, &rlength);
1244 generate_error (ERROR_OS, NULL);
1248 current_unit->bytes_left -= length;
1253 case FORMATTED_SEQUENTIAL:
1255 /* sf_read has already terminated input because of an '\n' */
1264 p = salloc_r (current_unit->s, &length);
1266 /* In case of internal file, there may not be any '\n'. */
1267 if (is_internal_unit() && p == NULL)
1274 generate_error (ERROR_OS, NULL);
1280 current_unit->endfile = AT_ENDFILE;
1289 if (current_unit->flags.access == ACCESS_SEQUENTIAL)
1290 test_endfile (current_unit);
1294 /* Position to the next record in write mode. */
1297 next_record_w (void)
1303 switch (current_mode ())
1305 case FORMATTED_DIRECT:
1306 if (current_unit->bytes_left == 0)
1309 length = current_unit->bytes_left;
1310 p = salloc_w (current_unit->s, &length);
1315 memset (p, ' ', current_unit->bytes_left);
1316 if (sfree (current_unit->s) == FAILURE)
1320 case UNFORMATTED_DIRECT:
1321 if (sfree (current_unit->s) == FAILURE)
1325 case UNFORMATTED_SEQUENTIAL:
1326 m = current_unit->recl - current_unit->bytes_left; /* Bytes written. */
1327 c = file_position (current_unit->s);
1329 length = sizeof (gfc_offset);
1331 /* Write the length tail. */
1333 p = salloc_w (current_unit->s, &length);
1337 memcpy (p, &m, sizeof (gfc_offset));
1338 if (sfree (current_unit->s) == FAILURE)
1341 /* Seek to the head and overwrite the bogus length with the real
1344 p = salloc_w_at (current_unit->s, &length, c - m - length);
1346 generate_error (ERROR_OS, NULL);
1348 memcpy (p, &m, sizeof (gfc_offset));
1349 if (sfree (current_unit->s) == FAILURE)
1352 /* Seek past the end of the current record. */
1354 if (sseek (current_unit->s, c + sizeof (gfc_offset)) == FAILURE)
1359 case FORMATTED_SEQUENTIAL:
1361 p = salloc_w (current_unit->s, &length);
1363 if (!is_internal_unit())
1366 *p = '\n'; /* No CR for internal writes. */
1371 if (sfree (current_unit->s) == FAILURE)
1377 generate_error (ERROR_OS, NULL);
1383 /* Position to the next record, which means moving to the end of the
1384 current record. This can happen under several different
1385 conditions. If the done flag is not set, we get ready to process
1389 next_record (int done)
1391 gfc_offset fp; /* File position. */
1393 current_unit->read_bad = 0;
1395 if (g.mode == READING)
1400 /* keep position up to date for INQUIRE */
1401 current_unit->flags.position = POSITION_ASIS;
1403 current_unit->current_record = 0;
1404 if (current_unit->flags.access == ACCESS_DIRECT)
1406 fp = file_position (current_unit->s);
1407 /* Calculate next record, rounding up partial records. */
1408 current_unit->last_record = (fp + current_unit->recl - 1)
1409 / current_unit->recl;
1412 current_unit->last_record++;
1419 /* Finalize the current data transfer. For a nonadvancing transfer,
1420 this means advancing to the next record. For internal units close the
1421 steam associated with the unit. */
1424 finalize_transfer (void)
1429 generate_error (ERROR_EOR, NULL);
1433 if (ioparm.library_return != LIBRARY_OK)
1436 if ((ionml != NULL) && (ioparm.namelist_name != NULL))
1438 if (ioparm.namelist_read_mode)
1445 if (current_unit == NULL)
1448 if (setjmp (g.eof_jump))
1450 generate_error (ERROR_END, NULL);
1454 if (ioparm.list_format && g.mode == READING)
1455 finish_list_read ();
1460 if (advance_status == ADVANCE_NO || g.seen_dollar)
1462 /* Most systems buffer lines, so force the partial record
1463 to be written out. */
1464 flush (current_unit->s);
1470 current_unit->current_record = 0;
1473 sfree (current_unit->s);
1475 if (is_internal_unit ())
1476 sclose (current_unit->s);
1480 /* Transfer function for IOLENGTH. It doesn't actually do any
1481 data transfer, it just updates the length counter. */
1484 iolength_transfer (bt type __attribute__ ((unused)),
1485 void *dest __attribute__ ((unused)),
1488 if (ioparm.iolength != NULL)
1489 *ioparm.iolength += len;
1493 /* Initialize the IOLENGTH data transfer. This function is in essence
1494 a very much simplified version of data_transfer_init(), because it
1495 doesn't have to deal with units at all. */
1498 iolength_transfer_init (void)
1500 if (ioparm.iolength != NULL)
1501 *ioparm.iolength = 0;
1505 /* Set up the subroutine that will handle the transfers. */
1507 transfer = iolength_transfer;
1511 /* Library entry point for the IOLENGTH form of the INQUIRE
1512 statement. The IOLENGTH form requires no I/O to be performed, but
1513 it must still be a runtime library call so that we can determine
1514 the iolength for dynamic arrays and such. */
1516 extern void st_iolength (void);
1517 export_proto(st_iolength);
1523 iolength_transfer_init ();
1526 extern void st_iolength_done (void);
1527 export_proto(st_iolength_done);
1530 st_iolength_done (void)
1536 /* The READ statement. */
1538 extern void st_read (void);
1539 export_proto(st_read);
1546 data_transfer_init (1);
1548 /* Handle complications dealing with the endfile record. It is
1549 significant that this is the only place where ERROR_END is
1550 generated. Reading an end of file elsewhere is either end of
1551 record or an I/O error. */
1553 if (current_unit->flags.access == ACCESS_SEQUENTIAL)
1554 switch (current_unit->endfile)
1560 if (!is_internal_unit())
1562 generate_error (ERROR_END, NULL);
1563 current_unit->endfile = AFTER_ENDFILE;
1568 generate_error (ERROR_ENDFILE, NULL);
1573 extern void st_read_done (void);
1574 export_proto(st_read_done);
1579 finalize_transfer ();
1583 extern void st_write (void);
1584 export_proto(st_write);
1590 data_transfer_init (0);
1593 extern void st_write_done (void);
1594 export_proto(st_write_done);
1597 st_write_done (void)
1599 finalize_transfer ();
1601 /* Deal with endfile conditions associated with sequential files. */
1603 if (current_unit != NULL && current_unit->flags.access == ACCESS_SEQUENTIAL)
1604 switch (current_unit->endfile)
1606 case AT_ENDFILE: /* Remain at the endfile record. */
1610 current_unit->endfile = AT_ENDFILE; /* Just at it now. */
1614 if (current_unit->current_record > current_unit->last_record)
1616 /* Get rid of whatever is after this record. */
1617 if (struncate (current_unit->s) == FAILURE)
1618 generate_error (ERROR_OS, NULL);
1621 current_unit->endfile = AT_ENDFILE;
1628 /* Receives the scalar information for namelist objects and stores it
1629 in a linked list of namelist_info types. */
1631 extern void st_set_nml_var (void * ,char * ,
1632 GFC_INTEGER_4 ,gfc_charlen_type ,GFC_INTEGER_4);
1633 export_proto(st_set_nml_var);
1637 st_set_nml_var (void * var_addr, char * var_name, GFC_INTEGER_4 len,
1638 gfc_charlen_type string_length, GFC_INTEGER_4 dtype)
1640 namelist_info *t1 = NULL;
1643 nml = (namelist_info*) get_mem (sizeof (namelist_info));
1645 nml->mem_pos = var_addr;
1647 nml->var_name = (char*) get_mem (strlen (var_name) + 1);
1648 strcpy (nml->var_name, var_name);
1650 nml->len = (int) len;
1651 nml->string_length = (index_type) string_length;
1653 nml->var_rank = (int) (dtype & GFC_DTYPE_RANK_MASK);
1654 nml->size = (index_type) (dtype >> GFC_DTYPE_SIZE_SHIFT);
1655 nml->type = (bt) ((dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT);
1657 if (nml->var_rank > 0)
1659 nml->dim = (descriptor_dimension*)
1660 get_mem (nml->var_rank * sizeof (descriptor_dimension));
1661 nml->ls = (nml_loop_spec*)
1662 get_mem (nml->var_rank * sizeof (nml_loop_spec));
1676 for (t1 = ionml; t1->next; t1 = t1->next);
1682 /* Store the dimensional information for the namelist object. */
1683 extern void st_set_nml_var_dim (GFC_INTEGER_4, GFC_INTEGER_4,
1684 GFC_INTEGER_4 ,GFC_INTEGER_4);
1685 export_proto(st_set_nml_var_dim);
1688 st_set_nml_var_dim (GFC_INTEGER_4 n_dim, GFC_INTEGER_4 stride,
1689 GFC_INTEGER_4 lbound, GFC_INTEGER_4 ubound)
1691 namelist_info * nml;
1696 for (nml = ionml; nml->next; nml = nml->next);
1698 nml->dim[n].stride = (ssize_t)stride;
1699 nml->dim[n].lbound = (ssize_t)lbound;
1700 nml->dim[n].ubound = (ssize_t)ubound;