1 /* Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
4 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6 Libgfortran is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 Libgfortran is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Libgfortran; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* transfer.c -- Top level handling of data transfer statements. */
27 #include "libgfortran.h"
31 /* Calling conventions: Data transfer statements are unlike other
32 library calls in that they extend over several calls.
34 The first call is always a call to st_read() or st_write(). These
35 subroutines return no status unless a namelist read or write is
36 being done, in which case there is the usual status. No further
37 calls are necessary in this case.
39 For other sorts of data transfer, there are zero or more data
40 transfer statement that depend on the format of the data transfer
49 These subroutines do not return status.
51 The last call is a call to st_[read|write]_done(). While
52 something can easily go wrong with the initial st_read() or
53 st_write(), an error inhibits any data from actually being
56 gfc_unit *current_unit;
57 static int sf_seen_eor = 0;
59 char scratch[SCRATCH_SIZE];
60 static char *line_buffer = NULL;
62 static unit_advance advance_status;
64 static st_option advance_opt[] = {
71 static void (*transfer) (bt, void *, int);
75 { FORMATTED_SEQUENTIAL, UNFORMATTED_SEQUENTIAL,
76 FORMATTED_DIRECT, UNFORMATTED_DIRECT
86 if (current_unit->flags.access == ACCESS_DIRECT)
88 m = current_unit->flags.form == FORM_FORMATTED ?
89 FORMATTED_DIRECT : UNFORMATTED_DIRECT;
93 m = current_unit->flags.form == FORM_FORMATTED ?
94 FORMATTED_SEQUENTIAL : UNFORMATTED_SEQUENTIAL;
101 /* Mid level data transfer statements. These subroutines do reading
102 and writing in the style of salloc_r()/salloc_w() within the
105 /* When reading sequential formatted records we have a problem. We
106 don't know how long the line is until we read the trailing newline,
107 and we don't want to read too much. If we read too much, we might
108 have to do a physical seek backwards depending on how much data is
109 present, and devices like terminals aren't seekable and would cause
112 Given this, the solution is to read a byte at a time, stopping if
113 we hit the newline. For small locations, we use a static buffer.
114 For larger allocations, we are forced to allocate memory on the
115 heap. Hopefully this won't happen very often. */
118 read_sf (int *length)
120 static char data[SCRATCH_SIZE];
124 if (*length > SCRATCH_SIZE)
125 p = base = line_buffer = get_mem (*length);
129 memset(base,'\0',*length);
131 current_unit->bytes_left = options.default_recl;
137 if (is_internal_unit())
139 /* unity may be modified inside salloc_r if
140 is_internal_unit() is true. */
144 q = salloc_r (current_unit->s, &unity);
150 if (current_unit->unit_number == options.stdin_unit)
155 /* Unexpected end of line. */
156 if (current_unit->flags.pad == PAD_NO)
158 generate_error (ERROR_EOR, NULL);
162 current_unit->bytes_left = 0;
178 /* Function for reading the next couple of bytes from the current
179 file, advancing the current position. We return a pointer to a
180 buffer containing the bytes. We return NULL on end of record or
183 If the read is short, then it is because the current record does not
184 have enough data to satisfy the read request and the file was
185 opened with PAD=YES. The caller must assume tailing spaces for
189 read_block (int *length)
194 if (current_unit->flags.form == FORM_FORMATTED &&
195 current_unit->flags.access == ACCESS_SEQUENTIAL)
196 return read_sf (length); /* Special case. */
198 if (current_unit->bytes_left < *length)
200 if (current_unit->flags.pad == PAD_NO)
202 generate_error (ERROR_EOR, NULL); /* Not enough data left. */
206 *length = current_unit->bytes_left;
209 current_unit->bytes_left -= *length;
212 source = salloc_r (current_unit->s, &nread);
214 if (ioparm.size != NULL)
215 *ioparm.size += nread;
217 if (nread != *length)
218 { /* Short read, this shouldn't happen. */
219 if (current_unit->flags.pad == PAD_YES)
223 generate_error (ERROR_EOR, NULL);
232 /* Function for writing a block of bytes to the current file at the
233 current position, advancing the file pointer. We are given a length
234 and return a pointer to a buffer that the caller must (completely)
235 fill in. Returns NULL on error. */
238 write_block (int length)
242 if (!is_internal_unit() && current_unit->bytes_left < length)
244 generate_error (ERROR_EOR, NULL);
248 current_unit->bytes_left -= length;
249 dest = salloc_w (current_unit->s, &length);
251 if (ioparm.size != NULL)
252 *ioparm.size += length;
258 /* Master function for unformatted reads. */
261 unformatted_read (bt type, void *dest, int length)
266 source = read_block (&w);
270 memcpy (dest, source, w);
272 memset (((char *) dest) + w, ' ', length - w);
276 /* Master function for unformatted writes. */
279 unformatted_write (bt type, void *source, int length)
282 dest = write_block (length);
284 memcpy (dest, source, length);
288 /* Return a pointer to the name of a type. */
313 internal_error ("type_name(): Bad type");
320 /* Write a constant string to the output.
321 This is complicated because the string can have doubled delimiters
322 in it. The length in the format node is the true length. */
325 write_constant_string (fnode * f)
327 char c, delimiter, *p, *q;
330 length = f->u.string.length;
334 p = write_block (length);
341 for (; length > 0; length--)
344 if (c == delimiter && c != 'H')
345 q++; /* Skip the doubled delimiter. */
350 /* Given actual and expected types in a formatted data transfer, make
351 sure they agree. If not, an error message is generated. Returns
352 nonzero if something went wrong. */
355 require_type (bt expected, bt actual, fnode * f)
359 if (actual == expected)
362 st_sprintf (buffer, "Expected %s for item %d in formatted transfer, got %s",
363 type_name (expected), g.item_count, type_name (actual));
365 format_error (f, buffer);
370 /* This subroutine is the main loop for a formatted data transfer
371 statement. It would be natural to implement this as a coroutine
372 with the user program, but C makes that awkward. We loop,
373 processesing format elements. When we actually have to transfer
374 data instead of just setting flags, we return control to the user
375 program which calls a subroutine that supplies the address and type
376 of the next element, then comes back here to process it. */
379 formatted_transfer (bt type, void *p, int len)
384 int consume_data_flag;
386 /* Change a complex data item into a pair of reals. */
388 n = (p == NULL) ? 0 : ((type != BT_COMPLEX) ? 1 : 2);
389 if (type == BT_COMPLEX)
392 /* If reversion has occurred and there is another real data item,
393 then we have to move to the next record. */
395 if (g.reversion_flag && n > 0)
397 g.reversion_flag = 0;
402 consume_data_flag = 1 ;
403 if (ioparm.library_return != LIBRARY_OK)
408 return; /* No data descriptors left (already raised). */
415 if (require_type (BT_INTEGER, type, f))
418 if (g.mode == READING)
419 read_decimal (f, p, len);
428 if (require_type (BT_INTEGER, type, f))
431 if (g.mode == READING)
432 read_radix (f, p, len, 2);
442 if (g.mode == READING)
443 read_radix (f, p, len, 8);
453 if (g.mode == READING)
454 read_radix (f, p, len, 16);
463 if (require_type (BT_CHARACTER, type, f))
466 if (g.mode == READING)
477 if (g.mode == READING)
487 if (require_type (BT_REAL, type, f))
490 if (g.mode == READING)
500 if (require_type (BT_REAL, type, f))
503 if (g.mode == READING)
512 if (require_type (BT_REAL, type, f))
515 if (g.mode == READING)
518 write_en (f, p, len);
525 if (require_type (BT_REAL, type, f))
528 if (g.mode == READING)
531 write_es (f, p, len);
538 if (require_type (BT_REAL, type, f))
541 if (g.mode == READING)
551 if (g.mode == READING)
555 read_decimal (f, p, len);
586 internal_error ("formatted_transfer(): Bad type");
592 consume_data_flag = 0 ;
593 if (g.mode == READING)
595 format_error (f, "Constant string in input format");
598 write_constant_string (f);
601 /* Format codes that don't transfer data. */
604 consume_data_flag = 0 ;
605 if (g.mode == READING)
614 if (f->format==FMT_TL)
617 pos= current_unit->recl - current_unit->bytes_left - pos;
621 consume_data_flag = 0 ;
625 if (pos < 0 || pos >= current_unit->recl )
627 generate_error (ERROR_EOR, "T Or TL edit position error");
630 m = pos - (current_unit->recl - current_unit->bytes_left);
638 if (g.mode == READING)
645 move_pos_offset (current_unit->s,m);
651 consume_data_flag = 0 ;
652 g.sign_status = SIGN_S;
656 consume_data_flag = 0 ;
657 g.sign_status = SIGN_SS;
661 consume_data_flag = 0 ;
662 g.sign_status = SIGN_SP;
666 consume_data_flag = 0 ;
667 g.blank_status = BLANK_NULL;
671 consume_data_flag = 0 ;
672 g.blank_status = BLANK_ZERO;
676 consume_data_flag = 0 ;
677 g.scale_factor = f->u.k;
681 consume_data_flag = 0 ;
686 consume_data_flag = 0 ;
687 for (i = 0; i < f->repeat; i++)
693 /* A colon descriptor causes us to exit this loop (in
694 particular preventing another / descriptor from being
695 processed) unless there is another data item to be
697 consume_data_flag = 0 ;
703 internal_error ("Bad format node");
706 /* Free a buffer that we had to allocate during a sequential
707 formatted read of a block that was larger than the static
710 if (line_buffer != NULL)
712 free_mem (line_buffer);
716 /* Adjust the item count and data pointer. */
718 if ((consume_data_flag > 0) && (n > 0))
721 p = ((char *) p) + len;
727 /* Come here when we need a data descriptor but don't have one. We
728 push the current format node back onto the input, then return and
729 let the user program call us back with the data. */
737 /* Data transfer entry points. The type of the data entity is
738 implicit in the subroutine call. This prevents us from having to
739 share a common enum with the compiler. */
742 transfer_integer (void *p, int kind)
746 if (ioparm.library_return != LIBRARY_OK)
748 transfer (BT_INTEGER, p, kind);
753 transfer_real (void *p, int kind)
757 if (ioparm.library_return != LIBRARY_OK)
759 transfer (BT_REAL, p, kind);
764 transfer_logical (void *p, int kind)
768 if (ioparm.library_return != LIBRARY_OK)
770 transfer (BT_LOGICAL, p, kind);
775 transfer_character (void *p, int len)
779 if (ioparm.library_return != LIBRARY_OK)
781 transfer (BT_CHARACTER, p, len);
786 transfer_complex (void *p, int kind)
790 if (ioparm.library_return != LIBRARY_OK)
792 transfer (BT_COMPLEX, p, kind);
796 /* Preposition a sequential unformatted file while reading. */
804 n = sizeof (gfc_offset);
805 p = (gfc_offset *) salloc_r (current_unit->s, &n);
807 if (p == NULL || n != sizeof (gfc_offset))
809 generate_error (ERROR_BAD_US, NULL);
813 current_unit->bytes_left = *p;
817 /* Preposition a sequential unformatted file while writing. This
818 amount to writing a bogus length that will be filled in later. */
826 length = sizeof (gfc_offset);
827 p = (gfc_offset *) salloc_w (current_unit->s, &length);
831 generate_error (ERROR_OS, NULL);
835 *p = 0; /* Bogus value for now. */
836 if (sfree (current_unit->s) == FAILURE)
837 generate_error (ERROR_OS, NULL);
839 /* For sequential unformatted, we write until we have more bytes than
840 can fit in the record markers. If disk space runs out first, it will
841 error on the write. */
842 current_unit->recl = g.max_offset;
844 current_unit->bytes_left = current_unit->recl;
848 /* Position to the next record prior to transfer. We are assumed to
849 be before the next record. We also calculate the bytes in the next
856 if (current_unit->current_record)
857 return; /* Already positioned. */
859 switch (current_mode ())
861 case UNFORMATTED_SEQUENTIAL:
862 if (g.mode == READING)
869 case FORMATTED_SEQUENTIAL:
870 case FORMATTED_DIRECT:
871 case UNFORMATTED_DIRECT:
872 current_unit->bytes_left = current_unit->recl;
876 current_unit->current_record = 1;
880 /* Initialize things for a data transfer. This code is common for
881 both reading and writing. */
884 data_transfer_init (int read_flag)
886 unit_flags u_flags; /* Used for creating a unit if needed. */
888 g.mode = read_flag ? READING : WRITING;
890 if (ioparm.size != NULL)
891 *ioparm.size = 0; /* Initialize the count. */
893 current_unit = get_unit (read_flag);
894 if (current_unit == NULL)
895 { /* Open the unit with some default flags. */
896 memset (&u_flags, '\0', sizeof (u_flags));
897 u_flags.access = ACCESS_SEQUENTIAL;
898 u_flags.action = ACTION_READWRITE;
899 /* Is it unformatted? */
900 if (ioparm.format == NULL && !ioparm.list_format)
901 u_flags.form = FORM_UNFORMATTED;
903 u_flags.form = FORM_UNSPECIFIED;
904 u_flags.delim = DELIM_UNSPECIFIED;
905 u_flags.blank = BLANK_UNSPECIFIED;
906 u_flags.pad = PAD_UNSPECIFIED;
907 u_flags.status = STATUS_UNKNOWN;
909 current_unit = get_unit (read_flag);
912 if (current_unit == NULL)
915 if (is_internal_unit())
917 current_unit->recl = file_length(current_unit->s);
919 empty_internal_buffer (current_unit->s);
922 /* Check the action. */
924 if (read_flag && current_unit->flags.action == ACTION_WRITE)
925 generate_error (ERROR_BAD_ACTION,
926 "Cannot read from file opened for WRITE");
928 if (!read_flag && current_unit->flags.action == ACTION_READ)
929 generate_error (ERROR_BAD_ACTION, "Cannot write to file opened for READ");
931 if (ioparm.library_return != LIBRARY_OK)
934 /* Check the format. */
939 if (ioparm.library_return != LIBRARY_OK)
942 if (current_unit->flags.form == FORM_UNFORMATTED
943 && (ioparm.format != NULL || ioparm.list_format))
944 generate_error (ERROR_OPTION_CONFLICT,
945 "Format present for UNFORMATTED data transfer");
947 if (ioparm.namelist_name != NULL && ionml != NULL)
949 if(ioparm.format != NULL)
950 generate_error (ERROR_OPTION_CONFLICT,
951 "A format cannot be specified with a namelist");
953 else if (current_unit->flags.form == FORM_FORMATTED &&
954 ioparm.format == NULL && !ioparm.list_format)
955 generate_error (ERROR_OPTION_CONFLICT,
956 "Missing format for FORMATTED data transfer");
959 if (is_internal_unit () && current_unit->flags.form == FORM_UNFORMATTED)
960 generate_error (ERROR_OPTION_CONFLICT,
961 "Internal file cannot be accessed by UNFORMATTED data transfer");
963 /* Check the record number. */
965 if (current_unit->flags.access == ACCESS_DIRECT && ioparm.rec == 0)
967 generate_error (ERROR_MISSING_OPTION,
968 "Direct access data transfer requires record number");
972 if (current_unit->flags.access == ACCESS_SEQUENTIAL && ioparm.rec != 0)
974 generate_error (ERROR_OPTION_CONFLICT,
975 "Record number not allowed for sequential access data transfer");
979 /* Process the ADVANCE option. */
981 advance_status = (ioparm.advance == NULL) ? ADVANCE_UNSPECIFIED :
982 find_option (ioparm.advance, ioparm.advance_len, advance_opt,
983 "Bad ADVANCE parameter in data transfer statement");
985 if (advance_status != ADVANCE_UNSPECIFIED)
987 if (current_unit->flags.access == ACCESS_DIRECT)
988 generate_error (ERROR_OPTION_CONFLICT,
989 "ADVANCE specification conflicts with sequential access");
991 if (is_internal_unit ())
992 generate_error (ERROR_OPTION_CONFLICT,
993 "ADVANCE specification conflicts with internal file");
995 if (ioparm.format == NULL || ioparm.list_format)
996 generate_error (ERROR_OPTION_CONFLICT,
997 "ADVANCE specification requires an explicit format");
1002 if (ioparm.eor != 0 && advance_status == ADVANCE_NO)
1003 generate_error (ERROR_MISSING_OPTION,
1004 "EOR specification requires an ADVANCE specification of NO");
1006 if (ioparm.size != NULL && advance_status != ADVANCE_NO)
1007 generate_error (ERROR_MISSING_OPTION,
1008 "SIZE specification requires an ADVANCE specification of NO");
1012 { /* Write constraints. */
1013 if (ioparm.end != 0)
1014 generate_error (ERROR_OPTION_CONFLICT,
1015 "END specification cannot appear in a write statement");
1017 if (ioparm.eor != 0)
1018 generate_error (ERROR_OPTION_CONFLICT,
1019 "EOR specification cannot appear in a write statement");
1021 if (ioparm.size != 0)
1022 generate_error (ERROR_OPTION_CONFLICT,
1023 "SIZE specification cannot appear in a write statement");
1026 if (advance_status == ADVANCE_UNSPECIFIED)
1027 advance_status = ADVANCE_YES;
1028 if (ioparm.library_return != LIBRARY_OK)
1031 /* Sanity checks on the record number. */
1035 if (ioparm.rec <= 0)
1037 generate_error (ERROR_BAD_OPTION, "Record number must be positive");
1041 if (ioparm.rec >= current_unit->maxrec)
1043 generate_error (ERROR_BAD_OPTION, "Record number too large");
1047 /* Check to see if we might be reading what we wrote before */
1049 if (g.mode == READING && current_unit->mode == WRITING)
1050 flush(current_unit->s);
1052 /* Position the file. */
1053 if (sseek (current_unit->s,
1054 (ioparm.rec - 1) * current_unit->recl) == FAILURE)
1055 generate_error (ERROR_OS, NULL);
1058 current_unit->mode = g.mode;
1060 /* Set the initial value of flags. */
1062 g.blank_status = current_unit->flags.blank;
1063 g.sign_status = SIGN_S;
1071 /* Set up the subroutine that will handle the transfers. */
1075 if (current_unit->flags.form == FORM_UNFORMATTED)
1076 transfer = unformatted_read;
1079 if (ioparm.list_format)
1081 transfer = list_formatted_read;
1085 transfer = formatted_transfer;
1090 if (current_unit->flags.form == FORM_UNFORMATTED)
1091 transfer = unformatted_write;
1094 if (ioparm.list_format)
1095 transfer = list_formatted_write;
1097 transfer = formatted_transfer;
1101 /* Make sure that we don't do a read after a nonadvancing write. */
1105 if (current_unit->read_bad)
1107 generate_error (ERROR_BAD_OPTION,
1108 "Cannot READ after a nonadvancing WRITE");
1114 if (advance_status == ADVANCE_YES)
1115 current_unit->read_bad = 1;
1118 /* Start the data transfer if we are doing a formatted transfer. */
1119 if (current_unit->flags.form == FORM_FORMATTED && !ioparm.list_format
1120 && ioparm.namelist_name == NULL && ionml == NULL)
1122 formatted_transfer (0, NULL, 0);
1127 /* Space to the next record for read mode. If the file is not
1128 seekable, we read MAX_READ chunks until we get to the right
1131 #define MAX_READ 4096
1134 next_record_r (int done)
1136 int rlength, length;
1140 switch (current_mode ())
1142 case UNFORMATTED_SEQUENTIAL:
1143 current_unit->bytes_left += sizeof (gfc_offset); /* Skip over tail */
1145 /* Fall through... */
1147 case FORMATTED_DIRECT:
1148 case UNFORMATTED_DIRECT:
1149 if (current_unit->bytes_left == 0)
1152 if (is_seekable (current_unit->s))
1154 new = file_position (current_unit->s) + current_unit->bytes_left;
1156 /* Direct access files do not generate END conditions,
1158 if (sseek (current_unit->s, new) == FAILURE)
1159 generate_error (ERROR_OS, NULL);
1163 { /* Seek by reading data. */
1164 while (current_unit->bytes_left > 0)
1166 rlength = length = (MAX_READ > current_unit->bytes_left) ?
1167 MAX_READ : current_unit->bytes_left;
1169 p = salloc_r (current_unit->s, &rlength);
1172 generate_error (ERROR_OS, NULL);
1176 current_unit->bytes_left -= length;
1182 case FORMATTED_SEQUENTIAL:
1184 if (sf_seen_eor && done)
1189 p = salloc_r (current_unit->s, &length);
1191 /* In case of internal file, there may not be any '\n'. */
1192 if (is_internal_unit() && p == NULL)
1199 generate_error (ERROR_OS, NULL);
1205 current_unit->endfile = AT_ENDFILE;
1214 if (current_unit->flags.access == ACCESS_SEQUENTIAL)
1215 test_endfile (current_unit);
1219 /* Position to the next record in write mode. */
1222 next_record_w (int done)
1228 switch (current_mode ())
1230 case FORMATTED_DIRECT:
1231 if (current_unit->bytes_left == 0)
1234 length = current_unit->bytes_left;
1235 p = salloc_w (current_unit->s, &length);
1240 memset (p, ' ', current_unit->bytes_left);
1241 if (sfree (current_unit->s) == FAILURE)
1245 case UNFORMATTED_DIRECT:
1246 if (sfree (current_unit->s) == FAILURE)
1250 case UNFORMATTED_SEQUENTIAL:
1251 m = current_unit->recl - current_unit->bytes_left; /* Bytes written. */
1252 c = file_position (current_unit->s);
1254 length = sizeof (gfc_offset);
1256 /* Write the length tail. */
1258 p = salloc_w (current_unit->s, &length);
1262 *((gfc_offset *) p) = m;
1263 if (sfree (current_unit->s) == FAILURE)
1266 /* Seek to the head and overwrite the bogus length with the real
1269 p = salloc_w_at (current_unit->s, &length, c - m - length);
1271 generate_error (ERROR_OS, NULL);
1273 *((gfc_offset *) p) = m;
1274 if (sfree (current_unit->s) == FAILURE)
1277 /* Seek past the end of the current record. */
1279 if (sseek (current_unit->s, c + sizeof (gfc_offset)) == FAILURE)
1284 case FORMATTED_SEQUENTIAL:
1286 p = salloc_w (current_unit->s, &length);
1288 if (!is_internal_unit())
1291 *p = '\n'; /* No CR for internal writes. */
1296 if (sfree (current_unit->s) == FAILURE)
1302 generate_error (ERROR_OS, NULL);
1308 /* Position to the next record, which means moving to the end of the
1309 current record. This can happen under several different
1310 conditions. If the done flag is not set, we get ready to process
1314 next_record (int done)
1316 gfc_offset fp; /* File position. */
1318 current_unit->read_bad = 0;
1320 if (g.mode == READING)
1321 next_record_r (done);
1323 next_record_w (done);
1325 current_unit->current_record = 0;
1326 if (current_unit->flags.access == ACCESS_DIRECT)
1328 fp = file_position (current_unit->s);
1329 /* Calculate next record, rounding up partial records. */
1330 current_unit->last_record = (fp + current_unit->recl - 1)
1331 / current_unit->recl;
1334 current_unit->last_record++;
1341 /* Finalize the current data transfer. For a nonadvancing transfer,
1342 this means advancing to the next record. */
1345 finalize_transfer (void)
1348 if (setjmp (g.eof_jump))
1350 generate_error (ERROR_END, NULL);
1354 if ((ionml != NULL) && (ioparm.namelist_name != NULL))
1356 if (ioparm.namelist_read_mode)
1363 if (current_unit == NULL)
1366 if (ioparm.list_format && g.mode == READING)
1367 finish_list_read ();
1372 if (advance_status == ADVANCE_NO)
1374 /* Most systems buffer lines, so force the partial record
1375 to be written out. */
1376 flush (current_unit->s);
1381 current_unit->current_record = 0;
1384 sfree (current_unit->s);
1388 /* Transfer function for IOLENGTH. It doesn't actually do any
1389 data transfer, it just updates the length counter. */
1392 iolength_transfer (bt type, void *dest, int len)
1394 if (ioparm.iolength != NULL)
1395 *ioparm.iolength += len;
1399 /* Initialize the IOLENGTH data transfer. This function is in essence
1400 a very much simplified version of data_transfer_init(), because it
1401 doesn't have to deal with units at all. */
1404 iolength_transfer_init (void)
1407 if (ioparm.iolength != NULL)
1408 *ioparm.iolength = 0;
1412 /* Set up the subroutine that will handle the transfers. */
1414 transfer = iolength_transfer;
1419 /* Library entry point for the IOLENGTH form of the INQUIRE
1420 statement. The IOLENGTH form requires no I/O to be performed, but
1421 it must still be a runtime library call so that we can determine
1422 the iolength for dynamic arrays and such. */
1429 iolength_transfer_init ();
1433 st_iolength_done (void)
1439 /* The READ statement. */
1447 data_transfer_init (1);
1449 /* Handle complications dealing with the endfile record. It is
1450 significant that this is the only place where ERROR_END is
1451 generated. Reading an end of file elsewhere is either end of
1452 record or an I/O error. */
1454 if (current_unit->flags.access == ACCESS_SEQUENTIAL)
1455 switch (current_unit->endfile)
1461 if (!is_internal_unit())
1463 generate_error (ERROR_END, NULL);
1464 current_unit->endfile = AFTER_ENDFILE;
1469 generate_error (ERROR_ENDFILE, NULL);
1478 finalize_transfer ();
1489 data_transfer_init (0);
1494 st_write_done (void)
1497 finalize_transfer ();
1499 /* Deal with endfile conditions associated with sequential files. */
1501 if (current_unit != NULL && current_unit->flags.access == ACCESS_SEQUENTIAL)
1502 switch (current_unit->endfile)
1504 case AT_ENDFILE: /* Remain at the endfile record. */
1508 current_unit->endfile = AT_ENDFILE; /* Just at it now. */
1511 case NO_ENDFILE: /* Get rid of whatever is after this record. */
1512 if (struncate (current_unit->s) == FAILURE)
1513 generate_error (ERROR_OS, NULL);
1515 current_unit->endfile = AT_ENDFILE;
1524 st_set_nml_var (void * var_addr, char * var_name, int var_name_len,
1525 int kind, bt type, int string_length)
1527 namelist_info *t1 = NULL, *t2 = NULL;
1528 namelist_info *nml = (namelist_info *) get_mem (sizeof (namelist_info));
1529 nml->mem_pos = var_addr;
1532 assert (var_name_len > 0);
1533 nml->var_name = (char*) get_mem (var_name_len+1);
1534 strncpy (nml->var_name, var_name, var_name_len);
1535 nml->var_name[var_name_len] = 0;
1539 assert (var_name_len == 0);
1540 nml->var_name = NULL;
1545 nml->string_length = string_length;
1564 st_set_nml_var_int (void * var_addr, char * var_name, int var_name_len,
1568 st_set_nml_var (var_addr, var_name, var_name_len, kind, BT_INTEGER, 0);
1572 st_set_nml_var_float (void * var_addr, char * var_name, int var_name_len,
1576 st_set_nml_var (var_addr, var_name, var_name_len, kind, BT_REAL, 0);
1580 st_set_nml_var_char (void * var_addr, char * var_name, int var_name_len,
1581 int kind, gfc_strlen_type string_length)
1584 st_set_nml_var (var_addr, var_name, var_name_len, kind, BT_CHARACTER,
1589 st_set_nml_var_complex (void * var_addr, char * var_name, int var_name_len,
1593 st_set_nml_var (var_addr, var_name, var_name_len, kind, BT_COMPLEX, 0);
1597 st_set_nml_var_log (void * var_addr, char * var_name, int var_name_len,
1601 st_set_nml_var (var_addr, var_name, var_name_len, kind, BT_LOGICAL, 0);