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 gfc_unit *current_unit = NULL;
82 static int sf_seen_eor = 0;
83 static int eor_condition = 0;
85 /* Maximum righthand column written to. */
87 /* Number of skips + spaces to be done for T and X-editing. */
89 /* Number of spaces to be done for T and X-editing. */
90 static int pending_spaces;
92 char scratch[SCRATCH_SIZE];
93 static char *line_buffer = NULL;
95 static unit_advance advance_status;
97 static st_option advance_opt[] = {
104 static void (*transfer) (bt, void *, int);
108 { FORMATTED_SEQUENTIAL, UNFORMATTED_SEQUENTIAL,
109 FORMATTED_DIRECT, UNFORMATTED_DIRECT
119 if (current_unit->flags.access == ACCESS_DIRECT)
121 m = current_unit->flags.form == FORM_FORMATTED ?
122 FORMATTED_DIRECT : UNFORMATTED_DIRECT;
126 m = current_unit->flags.form == FORM_FORMATTED ?
127 FORMATTED_SEQUENTIAL : UNFORMATTED_SEQUENTIAL;
134 /* Mid level data transfer statements. These subroutines do reading
135 and writing in the style of salloc_r()/salloc_w() within the
138 /* When reading sequential formatted records we have a problem. We
139 don't know how long the line is until we read the trailing newline,
140 and we don't want to read too much. If we read too much, we might
141 have to do a physical seek backwards depending on how much data is
142 present, and devices like terminals aren't seekable and would cause
145 Given this, the solution is to read a byte at a time, stopping if
146 we hit the newline. For small locations, we use a static buffer.
147 For larger allocations, we are forced to allocate memory on the
148 heap. Hopefully this won't happen very often. */
151 read_sf (int *length)
153 static char data[SCRATCH_SIZE];
157 if (*length > SCRATCH_SIZE)
158 p = base = line_buffer = get_mem (*length);
162 /* If we have seen an eor previously, return a length of 0. The
163 caller is responsible for correctly padding the input field. */
175 if (is_internal_unit())
177 /* readlen may be modified inside salloc_r if
178 is_internal_unit() is true. */
182 q = salloc_r (current_unit->s, &readlen);
186 /* If we have a line without a terminating \n, drop through to
188 if (readlen < 1 && n == 0)
190 generate_error (ERROR_END, NULL);
194 if (readlen < 1 || *q == '\n' || *q == '\r')
196 /* Unexpected end of line. */
198 /* If we see an EOR during non-advancing I/O, we need to skip
199 the rest of the I/O statement. Set the corresponding flag. */
200 if (advance_status == ADVANCE_NO || g.seen_dollar)
203 /* Without padding, terminate the I/O statement without assigning
204 the value. With padding, the value still needs to be assigned,
205 so we can just continue with a short read. */
206 if (current_unit->flags.pad == PAD_NO)
208 generate_error (ERROR_EOR, NULL);
212 current_unit->bytes_left = 0;
223 current_unit->bytes_left -= *length;
225 if (ioparm.size != NULL)
226 *ioparm.size += *length;
232 /* Function for reading the next couple of bytes from the current
233 file, advancing the current position. We return a pointer to a
234 buffer containing the bytes. We return NULL on end of record or
237 If the read is short, then it is because the current record does not
238 have enough data to satisfy the read request and the file was
239 opened with PAD=YES. The caller must assume tailing spaces for
243 read_block (int *length)
248 if (current_unit->flags.form == FORM_FORMATTED &&
249 current_unit->flags.access == ACCESS_SEQUENTIAL)
250 return read_sf (length); /* Special case. */
252 if (current_unit->bytes_left < *length)
254 if (current_unit->flags.pad == PAD_NO)
256 generate_error (ERROR_EOR, NULL); /* Not enough data left. */
260 *length = current_unit->bytes_left;
263 current_unit->bytes_left -= *length;
266 source = salloc_r (current_unit->s, &nread);
268 if (ioparm.size != NULL)
269 *ioparm.size += nread;
271 if (nread != *length)
272 { /* Short read, this shouldn't happen. */
273 if (current_unit->flags.pad == PAD_YES)
277 generate_error (ERROR_EOR, NULL);
286 /* Function for writing a block of bytes to the current file at the
287 current position, advancing the file pointer. We are given a length
288 and return a pointer to a buffer that the caller must (completely)
289 fill in. Returns NULL on error. */
292 write_block (int length)
296 if (!is_internal_unit() && current_unit->bytes_left < length)
298 generate_error (ERROR_EOR, NULL);
302 current_unit->bytes_left -= length;
303 dest = salloc_w (current_unit->s, &length);
305 if (ioparm.size != NULL)
306 *ioparm.size += length;
312 /* Master function for unformatted reads. */
315 unformatted_read (bt type, void *dest, int length)
320 /* Transfer functions get passed the kind of the entity, so we have
321 to fix this for COMPLEX data which are twice the size of their
323 if (type == BT_COMPLEX)
327 source = read_block (&w);
331 memcpy (dest, source, w);
333 memset (((char *) dest) + w, ' ', length - w);
337 /* Master function for unformatted writes. */
340 unformatted_write (bt type, void *source, int length)
344 /* Correction for kind vs. length as in unformatted_read. */
345 if (type == BT_COMPLEX)
348 dest = write_block (length);
350 memcpy (dest, source, length);
354 /* Return a pointer to the name of a type. */
379 internal_error ("type_name(): Bad type");
386 /* Write a constant string to the output.
387 This is complicated because the string can have doubled delimiters
388 in it. The length in the format node is the true length. */
391 write_constant_string (fnode * f)
393 char c, delimiter, *p, *q;
396 length = f->u.string.length;
400 p = write_block (length);
407 for (; length > 0; length--)
410 if (c == delimiter && c != 'H' && c != 'h')
411 q++; /* Skip the doubled delimiter. */
416 /* Given actual and expected types in a formatted data transfer, make
417 sure they agree. If not, an error message is generated. Returns
418 nonzero if something went wrong. */
421 require_type (bt expected, bt actual, fnode * f)
425 if (actual == expected)
428 st_sprintf (buffer, "Expected %s for item %d in formatted transfer, got %s",
429 type_name (expected), g.item_count, type_name (actual));
431 format_error (f, buffer);
436 /* This subroutine is the main loop for a formatted data transfer
437 statement. It would be natural to implement this as a coroutine
438 with the user program, but C makes that awkward. We loop,
439 processesing format elements. When we actually have to transfer
440 data instead of just setting flags, we return control to the user
441 program which calls a subroutine that supplies the address and type
442 of the next element, then comes back here to process it. */
445 formatted_transfer (bt type, void *p, int len)
451 int consume_data_flag;
453 /* Change a complex data item into a pair of reals. */
455 n = (p == NULL) ? 0 : ((type != BT_COMPLEX) ? 1 : 2);
456 if (type == BT_COMPLEX)
459 /* If there's an EOR condition, we simulate finalizing the transfer
466 /* If reversion has occurred and there is another real data item,
467 then we have to move to the next record. */
468 if (g.reversion_flag && n > 0)
470 g.reversion_flag = 0;
474 consume_data_flag = 1 ;
475 if (ioparm.library_return != LIBRARY_OK)
480 return; /* No data descriptors left (already raised). */
482 /* Now discharge T, TR and X movements to the right. This is delayed
483 until a data producing format to suppress trailing spaces. */
485 if (g.mode == WRITING && skips != 0
486 && ((n>0 && ( t == FMT_I || t == FMT_B || t == FMT_O
487 || t == FMT_Z || t == FMT_F || t == FMT_E
488 || t == FMT_EN || t == FMT_ES || t == FMT_G
489 || t == FMT_L || t == FMT_A || t == FMT_D))
494 write_x (skips, pending_spaces);
495 max_pos = (int)(current_unit->recl - current_unit->bytes_left);
499 move_pos_offset (current_unit->s, skips);
500 current_unit->bytes_left -= (gfc_offset)skips;
502 skips = pending_spaces = 0;
505 bytes_used = (int)(current_unit->recl - current_unit->bytes_left);
512 if (require_type (BT_INTEGER, type, f))
515 if (g.mode == READING)
516 read_decimal (f, p, len);
525 if (require_type (BT_INTEGER, type, f))
528 if (g.mode == READING)
529 read_radix (f, p, len, 2);
539 if (g.mode == READING)
540 read_radix (f, p, len, 8);
550 if (g.mode == READING)
551 read_radix (f, p, len, 16);
561 if (g.mode == READING)
572 if (g.mode == READING)
582 if (require_type (BT_REAL, type, f))
585 if (g.mode == READING)
595 if (require_type (BT_REAL, type, f))
598 if (g.mode == READING)
607 if (require_type (BT_REAL, type, f))
610 if (g.mode == READING)
613 write_en (f, p, len);
620 if (require_type (BT_REAL, type, f))
623 if (g.mode == READING)
626 write_es (f, p, len);
633 if (require_type (BT_REAL, type, f))
636 if (g.mode == READING)
646 if (g.mode == READING)
650 read_decimal (f, p, len);
681 internal_error ("formatted_transfer(): Bad type");
687 consume_data_flag = 0 ;
688 if (g.mode == READING)
690 format_error (f, "Constant string in input format");
693 write_constant_string (f);
696 /* Format codes that don't transfer data. */
699 consume_data_flag = 0 ;
701 pos = bytes_used + f->u.n + skips;
702 skips = f->u.n + skips;
703 pending_spaces = pos - max_pos;
705 /* Writes occur just before the switch on f->format, above, so that
706 trailing blanks are suppressed. */
707 if (g.mode == READING)
714 if (f->format == FMT_TL)
715 pos = bytes_used - f->u.n;
718 consume_data_flag = 0;
722 /* Standard 10.6.1.1: excessive left tabbing is reset to the
723 left tab limit. We do not check if the position has gone
724 beyond the end of record because a subsequent tab could
725 bring us back again. */
726 pos = pos < 0 ? 0 : pos;
728 skips = skips + pos - bytes_used;
729 pending_spaces = pending_spaces + pos - max_pos;
734 /* Writes occur just before the switch on f->format, above, so that
735 trailing blanks are suppressed. */
736 if (g.mode == READING)
742 move_pos_offset (current_unit->s, skips);
743 current_unit->bytes_left -= (gfc_offset)skips;
744 skips = pending_spaces = 0;
751 consume_data_flag = 0 ;
752 g.sign_status = SIGN_S;
756 consume_data_flag = 0 ;
757 g.sign_status = SIGN_SS;
761 consume_data_flag = 0 ;
762 g.sign_status = SIGN_SP;
766 consume_data_flag = 0 ;
767 g.blank_status = BLANK_NULL;
771 consume_data_flag = 0 ;
772 g.blank_status = BLANK_ZERO;
776 consume_data_flag = 0 ;
777 g.scale_factor = f->u.k;
781 consume_data_flag = 0 ;
786 consume_data_flag = 0 ;
787 skips = pending_spaces = 0;
792 /* A colon descriptor causes us to exit this loop (in
793 particular preventing another / descriptor from being
794 processed) unless there is another data item to be
796 consume_data_flag = 0 ;
802 internal_error ("Bad format node");
805 /* Free a buffer that we had to allocate during a sequential
806 formatted read of a block that was larger than the static
809 if (line_buffer != NULL)
811 free_mem (line_buffer);
815 /* Adjust the item count and data pointer. */
817 if ((consume_data_flag > 0) && (n > 0))
820 p = ((char *) p) + len;
823 if (g.mode == READING)
826 pos = (int)(current_unit->recl - current_unit->bytes_left);
827 max_pos = (max_pos > pos) ? max_pos : pos;
833 /* Come here when we need a data descriptor but don't have one. We
834 push the current format node back onto the input, then return and
835 let the user program call us back with the data. */
841 /* Data transfer entry points. The type of the data entity is
842 implicit in the subroutine call. This prevents us from having to
843 share a common enum with the compiler. */
846 transfer_integer (void *p, int kind)
849 if (ioparm.library_return != LIBRARY_OK)
851 transfer (BT_INTEGER, p, kind);
856 transfer_real (void *p, int kind)
859 if (ioparm.library_return != LIBRARY_OK)
861 transfer (BT_REAL, p, kind);
866 transfer_logical (void *p, int kind)
869 if (ioparm.library_return != LIBRARY_OK)
871 transfer (BT_LOGICAL, p, kind);
876 transfer_character (void *p, int len)
879 if (ioparm.library_return != LIBRARY_OK)
881 transfer (BT_CHARACTER, p, len);
886 transfer_complex (void *p, int kind)
889 if (ioparm.library_return != LIBRARY_OK)
891 transfer (BT_COMPLEX, p, kind);
895 /* Preposition a sequential unformatted file while reading. */
904 n = sizeof (gfc_offset);
905 p = salloc_r (current_unit->s, &n);
908 return; /* end of file */
910 if (p == NULL || n != sizeof (gfc_offset))
912 generate_error (ERROR_BAD_US, NULL);
916 memcpy (&i, p, sizeof (gfc_offset));
917 current_unit->bytes_left = i;
921 /* Preposition a sequential unformatted file while writing. This
922 amount to writing a bogus length that will be filled in later. */
930 length = sizeof (gfc_offset);
931 p = salloc_w (current_unit->s, &length);
935 generate_error (ERROR_OS, NULL);
939 memset (p, '\0', sizeof (gfc_offset)); /* Bogus value for now. */
940 if (sfree (current_unit->s) == FAILURE)
941 generate_error (ERROR_OS, NULL);
943 /* For sequential unformatted, we write until we have more bytes than
944 can fit in the record markers. If disk space runs out first, it will
945 error on the write. */
946 current_unit->recl = g.max_offset;
948 current_unit->bytes_left = current_unit->recl;
952 /* Position to the next record prior to transfer. We are assumed to
953 be before the next record. We also calculate the bytes in the next
959 if (current_unit->current_record)
960 return; /* Already positioned. */
962 switch (current_mode ())
964 case UNFORMATTED_SEQUENTIAL:
965 if (g.mode == READING)
972 case FORMATTED_SEQUENTIAL:
973 case FORMATTED_DIRECT:
974 case UNFORMATTED_DIRECT:
975 current_unit->bytes_left = current_unit->recl;
979 current_unit->current_record = 1;
983 /* Initialize things for a data transfer. This code is common for
984 both reading and writing. */
987 data_transfer_init (int read_flag)
989 unit_flags u_flags; /* Used for creating a unit if needed. */
991 g.mode = read_flag ? READING : WRITING;
993 if (ioparm.size != NULL)
994 *ioparm.size = 0; /* Initialize the count. */
996 current_unit = get_unit (read_flag);
997 if (current_unit == NULL)
998 { /* Open the unit with some default flags. */
1001 generate_error (ERROR_BAD_OPTION, "Bad unit number in OPEN statement");
1005 memset (&u_flags, '\0', sizeof (u_flags));
1006 u_flags.access = ACCESS_SEQUENTIAL;
1007 u_flags.action = ACTION_READWRITE;
1008 /* Is it unformatted? */
1009 if (ioparm.format == NULL && !ioparm.list_format)
1010 u_flags.form = FORM_UNFORMATTED;
1012 u_flags.form = FORM_UNSPECIFIED;
1013 u_flags.delim = DELIM_UNSPECIFIED;
1014 u_flags.blank = BLANK_UNSPECIFIED;
1015 u_flags.pad = PAD_UNSPECIFIED;
1016 u_flags.status = STATUS_UNKNOWN;
1018 current_unit = get_unit (read_flag);
1021 if (current_unit == NULL)
1024 if (is_internal_unit())
1026 current_unit->recl = file_length(current_unit->s);
1027 if (g.mode==WRITING)
1028 empty_internal_buffer (current_unit->s);
1030 current_unit->bytes_left = current_unit->recl;
1033 /* Check the action. */
1035 if (read_flag && current_unit->flags.action == ACTION_WRITE)
1036 generate_error (ERROR_BAD_ACTION,
1037 "Cannot read from file opened for WRITE");
1039 if (!read_flag && current_unit->flags.action == ACTION_READ)
1040 generate_error (ERROR_BAD_ACTION, "Cannot write to file opened for READ");
1042 if (ioparm.library_return != LIBRARY_OK)
1045 /* Check the format. */
1050 if (ioparm.library_return != LIBRARY_OK)
1053 if (current_unit->flags.form == FORM_UNFORMATTED
1054 && (ioparm.format != NULL || ioparm.list_format))
1055 generate_error (ERROR_OPTION_CONFLICT,
1056 "Format present for UNFORMATTED data transfer");
1058 if (ioparm.namelist_name != NULL && ionml != NULL)
1060 if(ioparm.format != NULL)
1061 generate_error (ERROR_OPTION_CONFLICT,
1062 "A format cannot be specified with a namelist");
1064 else if (current_unit->flags.form == FORM_FORMATTED &&
1065 ioparm.format == NULL && !ioparm.list_format)
1066 generate_error (ERROR_OPTION_CONFLICT,
1067 "Missing format for FORMATTED data transfer");
1070 if (is_internal_unit () && current_unit->flags.form == FORM_UNFORMATTED)
1071 generate_error (ERROR_OPTION_CONFLICT,
1072 "Internal file cannot be accessed by UNFORMATTED data transfer");
1074 /* Check the record number. */
1076 if (current_unit->flags.access == ACCESS_DIRECT && ioparm.rec == 0)
1078 generate_error (ERROR_MISSING_OPTION,
1079 "Direct access data transfer requires record number");
1083 if (current_unit->flags.access == ACCESS_SEQUENTIAL && ioparm.rec != 0)
1085 generate_error (ERROR_OPTION_CONFLICT,
1086 "Record number not allowed for sequential access data transfer");
1090 /* Process the ADVANCE option. */
1092 advance_status = (ioparm.advance == NULL) ? ADVANCE_UNSPECIFIED :
1093 find_option (ioparm.advance, ioparm.advance_len, advance_opt,
1094 "Bad ADVANCE parameter in data transfer statement");
1096 if (advance_status != ADVANCE_UNSPECIFIED)
1098 if (current_unit->flags.access == ACCESS_DIRECT)
1099 generate_error (ERROR_OPTION_CONFLICT,
1100 "ADVANCE specification conflicts with sequential access");
1102 if (is_internal_unit ())
1103 generate_error (ERROR_OPTION_CONFLICT,
1104 "ADVANCE specification conflicts with internal file");
1106 if (ioparm.format == NULL || ioparm.list_format)
1107 generate_error (ERROR_OPTION_CONFLICT,
1108 "ADVANCE specification requires an explicit format");
1113 if (ioparm.eor != 0 && advance_status != ADVANCE_NO)
1114 generate_error (ERROR_MISSING_OPTION,
1115 "EOR specification requires an ADVANCE specification of NO");
1117 if (ioparm.size != NULL && advance_status != ADVANCE_NO)
1118 generate_error (ERROR_MISSING_OPTION,
1119 "SIZE specification requires an ADVANCE specification of NO");
1123 { /* Write constraints. */
1124 if (ioparm.end != 0)
1125 generate_error (ERROR_OPTION_CONFLICT,
1126 "END specification cannot appear in a write statement");
1128 if (ioparm.eor != 0)
1129 generate_error (ERROR_OPTION_CONFLICT,
1130 "EOR specification cannot appear in a write statement");
1132 if (ioparm.size != 0)
1133 generate_error (ERROR_OPTION_CONFLICT,
1134 "SIZE specification cannot appear in a write statement");
1137 if (advance_status == ADVANCE_UNSPECIFIED)
1138 advance_status = ADVANCE_YES;
1139 if (ioparm.library_return != LIBRARY_OK)
1142 /* Sanity checks on the record number. */
1146 if (ioparm.rec <= 0)
1148 generate_error (ERROR_BAD_OPTION, "Record number must be positive");
1152 if (ioparm.rec >= current_unit->maxrec)
1154 generate_error (ERROR_BAD_OPTION, "Record number too large");
1158 /* Check to see if we might be reading what we wrote before */
1160 if (g.mode == READING && current_unit->mode == WRITING)
1161 flush(current_unit->s);
1163 /* Check whether the record exists to be read. Only
1164 a partial record needs to exist. */
1166 if (g.mode == READING && (ioparm.rec -1)
1167 * current_unit->recl >= file_length (current_unit->s))
1169 generate_error (ERROR_BAD_OPTION, "Non-existing record number");
1173 /* Position the file. */
1174 if (sseek (current_unit->s,
1175 (ioparm.rec - 1) * current_unit->recl) == FAILURE)
1177 generate_error (ERROR_OS, NULL);
1182 /* Overwriting an existing sequential file ?
1183 it is always safe to truncate the file on the first write */
1184 if (g.mode == WRITING
1185 && current_unit->flags.access == ACCESS_SEQUENTIAL
1186 && current_unit->last_record == 0)
1187 struncate(current_unit->s);
1189 current_unit->mode = g.mode;
1191 /* Set the initial value of flags. */
1193 g.blank_status = current_unit->flags.blank;
1194 g.sign_status = SIGN_S;
1204 /* Set up the subroutine that will handle the transfers. */
1208 if (current_unit->flags.form == FORM_UNFORMATTED)
1209 transfer = unformatted_read;
1212 if (ioparm.list_format)
1214 transfer = list_formatted_read;
1218 transfer = formatted_transfer;
1223 if (current_unit->flags.form == FORM_UNFORMATTED)
1224 transfer = unformatted_write;
1227 if (ioparm.list_format)
1228 transfer = list_formatted_write;
1230 transfer = formatted_transfer;
1234 /* Make sure that we don't do a read after a nonadvancing write. */
1238 if (current_unit->read_bad)
1240 generate_error (ERROR_BAD_OPTION,
1241 "Cannot READ after a nonadvancing WRITE");
1247 if (advance_status == ADVANCE_YES && !g.seen_dollar)
1248 current_unit->read_bad = 1;
1251 /* Reset counters for T and X-editing. */
1252 max_pos = skips = pending_spaces = 0;
1254 /* Start the data transfer if we are doing a formatted transfer. */
1255 if (current_unit->flags.form == FORM_FORMATTED && !ioparm.list_format
1256 && ioparm.namelist_name == NULL && ionml == NULL)
1257 formatted_transfer (0, NULL, 0);
1261 /* Space to the next record for read mode. If the file is not
1262 seekable, we read MAX_READ chunks until we get to the right
1265 #define MAX_READ 4096
1268 next_record_r (void)
1270 int rlength, length;
1274 switch (current_mode ())
1276 case UNFORMATTED_SEQUENTIAL:
1277 current_unit->bytes_left += sizeof (gfc_offset); /* Skip over tail */
1279 /* Fall through... */
1281 case FORMATTED_DIRECT:
1282 case UNFORMATTED_DIRECT:
1283 if (current_unit->bytes_left == 0)
1286 if (is_seekable (current_unit->s))
1288 new = file_position (current_unit->s) + current_unit->bytes_left;
1290 /* Direct access files do not generate END conditions,
1292 if (sseek (current_unit->s, new) == FAILURE)
1293 generate_error (ERROR_OS, NULL);
1297 { /* Seek by reading data. */
1298 while (current_unit->bytes_left > 0)
1300 rlength = length = (MAX_READ > current_unit->bytes_left) ?
1301 MAX_READ : current_unit->bytes_left;
1303 p = salloc_r (current_unit->s, &rlength);
1306 generate_error (ERROR_OS, NULL);
1310 current_unit->bytes_left -= length;
1315 case FORMATTED_SEQUENTIAL:
1317 /* sf_read has already terminated input because of an '\n' */
1326 p = salloc_r (current_unit->s, &length);
1328 /* In case of internal file, there may not be any '\n'. */
1329 if (is_internal_unit() && p == NULL)
1336 generate_error (ERROR_OS, NULL);
1342 current_unit->endfile = AT_ENDFILE;
1351 if (current_unit->flags.access == ACCESS_SEQUENTIAL)
1352 test_endfile (current_unit);
1356 /* Position to the next record in write mode. */
1359 next_record_w (void)
1365 /* Zero counters for X- and T-editing. */
1366 max_pos = skips = pending_spaces = 0;
1368 switch (current_mode ())
1370 case FORMATTED_DIRECT:
1371 if (current_unit->bytes_left == 0)
1374 length = current_unit->bytes_left;
1375 p = salloc_w (current_unit->s, &length);
1380 memset (p, ' ', current_unit->bytes_left);
1381 if (sfree (current_unit->s) == FAILURE)
1385 case UNFORMATTED_DIRECT:
1386 if (sfree (current_unit->s) == FAILURE)
1390 case UNFORMATTED_SEQUENTIAL:
1391 m = current_unit->recl - current_unit->bytes_left; /* Bytes written. */
1392 c = file_position (current_unit->s);
1394 length = sizeof (gfc_offset);
1396 /* Write the length tail. */
1398 p = salloc_w (current_unit->s, &length);
1402 memcpy (p, &m, sizeof (gfc_offset));
1403 if (sfree (current_unit->s) == FAILURE)
1406 /* Seek to the head and overwrite the bogus length with the real
1409 p = salloc_w_at (current_unit->s, &length, c - m - length);
1411 generate_error (ERROR_OS, NULL);
1413 memcpy (p, &m, sizeof (gfc_offset));
1414 if (sfree (current_unit->s) == FAILURE)
1417 /* Seek past the end of the current record. */
1419 if (sseek (current_unit->s, c + sizeof (gfc_offset)) == FAILURE)
1424 case FORMATTED_SEQUENTIAL:
1430 p = salloc_w (current_unit->s, &length);
1432 if (!is_internal_unit())
1435 { /* No new line for internal writes. */
1447 if (sfree (current_unit->s) == FAILURE)
1453 generate_error (ERROR_OS, NULL);
1459 /* Position to the next record, which means moving to the end of the
1460 current record. This can happen under several different
1461 conditions. If the done flag is not set, we get ready to process
1465 next_record (int done)
1467 gfc_offset fp; /* File position. */
1469 current_unit->read_bad = 0;
1471 if (g.mode == READING)
1476 /* keep position up to date for INQUIRE */
1477 current_unit->flags.position = POSITION_ASIS;
1479 current_unit->current_record = 0;
1480 if (current_unit->flags.access == ACCESS_DIRECT)
1482 fp = file_position (current_unit->s);
1483 /* Calculate next record, rounding up partial records. */
1484 current_unit->last_record = (fp + current_unit->recl - 1)
1485 / current_unit->recl;
1488 current_unit->last_record++;
1495 /* Finalize the current data transfer. For a nonadvancing transfer,
1496 this means advancing to the next record. For internal units close the
1497 steam associated with the unit. */
1500 finalize_transfer (void)
1505 generate_error (ERROR_EOR, NULL);
1509 if (ioparm.library_return != LIBRARY_OK)
1512 if ((ionml != NULL) && (ioparm.namelist_name != NULL))
1514 if (ioparm.namelist_read_mode)
1521 if (current_unit == NULL)
1524 if (setjmp (g.eof_jump))
1526 generate_error (ERROR_END, NULL);
1530 if (ioparm.list_format && g.mode == READING)
1531 finish_list_read ();
1536 if (advance_status == ADVANCE_NO || g.seen_dollar)
1538 /* Most systems buffer lines, so force the partial record
1539 to be written out. */
1540 flush (current_unit->s);
1546 current_unit->current_record = 0;
1549 sfree (current_unit->s);
1551 if (is_internal_unit ())
1552 sclose (current_unit->s);
1556 /* Transfer function for IOLENGTH. It doesn't actually do any
1557 data transfer, it just updates the length counter. */
1560 iolength_transfer (bt type , void *dest __attribute__ ((unused)),
1563 if (ioparm.iolength != NULL)
1565 if (type == BT_COMPLEX)
1566 *ioparm.iolength += 2*len;
1568 *ioparm.iolength += len;
1573 /* Initialize the IOLENGTH data transfer. This function is in essence
1574 a very much simplified version of data_transfer_init(), because it
1575 doesn't have to deal with units at all. */
1578 iolength_transfer_init (void)
1580 if (ioparm.iolength != NULL)
1581 *ioparm.iolength = 0;
1585 /* Set up the subroutine that will handle the transfers. */
1587 transfer = iolength_transfer;
1591 /* Library entry point for the IOLENGTH form of the INQUIRE
1592 statement. The IOLENGTH form requires no I/O to be performed, but
1593 it must still be a runtime library call so that we can determine
1594 the iolength for dynamic arrays and such. */
1596 extern void st_iolength (void);
1597 export_proto(st_iolength);
1603 iolength_transfer_init ();
1606 extern void st_iolength_done (void);
1607 export_proto(st_iolength_done);
1610 st_iolength_done (void)
1616 /* The READ statement. */
1618 extern void st_read (void);
1619 export_proto(st_read);
1627 data_transfer_init (1);
1629 /* Handle complications dealing with the endfile record. It is
1630 significant that this is the only place where ERROR_END is
1631 generated. Reading an end of file elsewhere is either end of
1632 record or an I/O error. */
1634 if (current_unit->flags.access == ACCESS_SEQUENTIAL)
1635 switch (current_unit->endfile)
1641 if (!is_internal_unit())
1643 generate_error (ERROR_END, NULL);
1644 current_unit->endfile = AFTER_ENDFILE;
1649 generate_error (ERROR_ENDFILE, NULL);
1654 extern void st_read_done (void);
1655 export_proto(st_read_done);
1660 finalize_transfer ();
1664 extern void st_write (void);
1665 export_proto(st_write);
1672 data_transfer_init (0);
1675 extern void st_write_done (void);
1676 export_proto(st_write_done);
1679 st_write_done (void)
1681 finalize_transfer ();
1683 /* Deal with endfile conditions associated with sequential files. */
1685 if (current_unit != NULL && current_unit->flags.access == ACCESS_SEQUENTIAL)
1686 switch (current_unit->endfile)
1688 case AT_ENDFILE: /* Remain at the endfile record. */
1692 current_unit->endfile = AT_ENDFILE; /* Just at it now. */
1696 if (current_unit->current_record > current_unit->last_record)
1698 /* Get rid of whatever is after this record. */
1699 if (struncate (current_unit->s) == FAILURE)
1700 generate_error (ERROR_OS, NULL);
1703 current_unit->endfile = AT_ENDFILE;
1710 /* Receives the scalar information for namelist objects and stores it
1711 in a linked list of namelist_info types. */
1713 extern void st_set_nml_var (void * ,char * ,
1714 GFC_INTEGER_4 ,gfc_charlen_type ,GFC_INTEGER_4);
1715 export_proto(st_set_nml_var);
1719 st_set_nml_var (void * var_addr, char * var_name, GFC_INTEGER_4 len,
1720 gfc_charlen_type string_length, GFC_INTEGER_4 dtype)
1722 namelist_info *t1 = NULL;
1725 nml = (namelist_info*) get_mem (sizeof (namelist_info));
1727 nml->mem_pos = var_addr;
1729 nml->var_name = (char*) get_mem (strlen (var_name) + 1);
1730 strcpy (nml->var_name, var_name);
1732 nml->len = (int) len;
1733 nml->string_length = (index_type) string_length;
1735 nml->var_rank = (int) (dtype & GFC_DTYPE_RANK_MASK);
1736 nml->size = (index_type) (dtype >> GFC_DTYPE_SIZE_SHIFT);
1737 nml->type = (bt) ((dtype & GFC_DTYPE_TYPE_MASK) >> GFC_DTYPE_TYPE_SHIFT);
1739 if (nml->var_rank > 0)
1741 nml->dim = (descriptor_dimension*)
1742 get_mem (nml->var_rank * sizeof (descriptor_dimension));
1743 nml->ls = (nml_loop_spec*)
1744 get_mem (nml->var_rank * sizeof (nml_loop_spec));
1758 for (t1 = ionml; t1->next; t1 = t1->next);
1764 /* Store the dimensional information for the namelist object. */
1765 extern void st_set_nml_var_dim (GFC_INTEGER_4, GFC_INTEGER_4,
1766 GFC_INTEGER_4 ,GFC_INTEGER_4);
1767 export_proto(st_set_nml_var_dim);
1770 st_set_nml_var_dim (GFC_INTEGER_4 n_dim, GFC_INTEGER_4 stride,
1771 GFC_INTEGER_4 lbound, GFC_INTEGER_4 ubound)
1773 namelist_info * nml;
1778 for (nml = ionml; nml->next; nml = nml->next);
1780 nml->dim[n].stride = (ssize_t)stride;
1781 nml->dim[n].lbound = (ssize_t)lbound;
1782 nml->dim[n].ubound = (ssize_t)ubound;