2 /* Copyright (C) 2002-2003 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
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 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Libgfortran; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* transfer.c -- Top level handling of data transfer statements. */
28 #include "libgfortran.h"
32 /* Calling conventions: Data transfer statements are unlike other
33 * library calls in that they extend over several calls.
35 * The first call is always a call to st_read() or st_write(). These
36 * subroutines return no status unless a namelist read or write is
37 * being done, in which case there is the usual status. No further
38 * calls are necessary in this case.
40 * For other sorts of data transfer, there are zero or more data
41 * transfer statement that depend on the format of the data transfer
50 * These subroutines do not return status.
52 * The last call is a call to st_[read|write]_done(). While
53 * something can easily go wrong with the initial st_read() or
54 * st_write(), an error inhibits any data from actually being
58 gfc_unit
*current_unit
;
59 static int sf_seen_eor
= 0;
61 char scratch
[SCRATCH_SIZE
];
62 static char *line_buffer
= NULL
;
64 static unit_advance advance_status
;
66 static st_option advance_opt
[] = {
73 static void (*transfer
) (bt
, void *, int);
77 { FORMATTED_SEQUENTIAL
, UNFORMATTED_SEQUENTIAL
,
78 FORMATTED_DIRECT
, UNFORMATTED_DIRECT
88 if (current_unit
->flags
.access
== ACCESS_DIRECT
)
90 m
= current_unit
->flags
.form
== FORM_FORMATTED
?
91 FORMATTED_DIRECT
: UNFORMATTED_DIRECT
;
95 m
= current_unit
->flags
.form
== FORM_FORMATTED
?
96 FORMATTED_SEQUENTIAL
: UNFORMATTED_SEQUENTIAL
;
103 /* Mid level data transfer statements. These subroutines do reading
104 * and writing in the style of salloc_r()/salloc_w() within the
107 /* read_sf()-- When reading sequential formatted records we have a
108 * problem. We don't know how long the line is until we read the
109 * trailing newline, and we don't want to read too much. If we read
110 * too much, we might have to do a physical seek backwards depending
111 * on how much data is present, and devices like terminals aren't
112 * seekable and would cause an I/O error.
114 * Given this, the solution is to read a byte at a time, stopping if
115 * we hit the newline. For small locations, we use a static buffer.
116 * For larger allocations, we are forced to allocate memory on the
117 * heap. Hopefully this won't happen very often. */
120 read_sf (int *length
)
122 static char data
[SCRATCH_SIZE
];
126 if (*length
> SCRATCH_SIZE
)
127 p
= base
= line_buffer
= get_mem (*length
);
131 memset(base
,'\0',*length
);
133 current_unit
->bytes_left
= options
.default_recl
;
139 if (is_internal_unit())
141 /* unity may be modified inside salloc_r if is_internal_unit() is true */
145 q
= salloc_r (current_unit
->s
, &unity
);
151 if (current_unit
->unit_number
== options
.stdin_unit
)
156 /* Unexpected end of line */
157 if (current_unit
->flags
.pad
== PAD_NO
)
159 generate_error (ERROR_EOR
, NULL
);
163 current_unit
->bytes_left
= 0;
179 /* read_block()-- Function for reading the next couple of bytes from
180 * the current file, advancing the current position. We return a
181 * pointer to a buffer containing the bytes. We return NULL on end of
182 * record or end of file.
184 * If the read is short, then it is because the current record does not
185 * have enough data to satisfy the read request and the file was
186 * opened with PAD=YES. The caller must assume tailing spaces for
190 read_block (int *length
)
195 if (current_unit
->flags
.form
== FORM_FORMATTED
&&
196 current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
197 return read_sf (length
); /* Special case */
199 if (current_unit
->bytes_left
< *length
)
201 if (current_unit
->flags
.pad
== PAD_NO
)
203 generate_error (ERROR_EOR
, NULL
); /* Not enough data left */
207 *length
= current_unit
->bytes_left
;
210 current_unit
->bytes_left
-= *length
;
213 source
= salloc_r (current_unit
->s
, &nread
);
215 if (ioparm
.size
!= NULL
)
216 *ioparm
.size
+= nread
;
218 if (nread
!= *length
)
219 { /* Short read, this shouldn't happen */
220 if (current_unit
->flags
.pad
== PAD_YES
)
224 generate_error (ERROR_EOR
, NULL
);
233 /* write_block()-- Function for writing a block of bytes to the
234 * current file at the current position, advancing the file pointer.
235 * We are given a length and return a pointer to a buffer that the
236 * caller must (completely) fill in. Returns NULL on error. */
239 write_block (int length
)
243 if (!is_internal_unit() && current_unit
->bytes_left
< length
)
245 generate_error (ERROR_EOR
, NULL
);
249 current_unit
->bytes_left
-= length
;
250 dest
= salloc_w (current_unit
->s
, &length
);
252 if (ioparm
.size
!= NULL
)
253 *ioparm
.size
+= length
;
259 /* unformatted_read()-- Master function for unformatted reads. */
262 unformatted_read (bt type
, void *dest
, int length
)
267 source
= read_block (&w
);
271 memcpy (dest
, source
, w
);
273 memset (((char *) dest
) + w
, ' ', length
- w
);
278 unformatted_write (bt type
, void *source
, int length
)
281 dest
= write_block (length
);
283 memcpy (dest
, source
, length
);
287 /* type_name()-- Return a pointer to the name of a type. */
312 internal_error ("type_name(): Bad type");
319 /* write_constant_string()-- write a constant string to the output.
320 * This is complicated because the string can have doubled delimiters
321 * in it. The length in the format node is the true length. */
324 write_constant_string (fnode
* f
)
326 char c
, delimiter
, *p
, *q
;
329 length
= f
->u
.string
.length
;
333 p
= write_block (length
);
340 for (; length
> 0; length
--)
343 if (c
== delimiter
&& c
!= 'H')
344 q
++; /* Skip the doubled delimiter */
349 /* require_type()-- Given actual and expected types in a formatted
350 * data transfer, make sure they agree. If not, an error message is
351 * generated. Returns nonzero if something went wrong. */
354 require_type (bt expected
, bt actual
, fnode
* f
)
358 if (actual
== expected
)
361 st_sprintf (buffer
, "Expected %s for item %d in formatted transfer, got %s",
362 type_name (expected
), g
.item_count
, type_name (actual
));
364 format_error (f
, buffer
);
369 /* formatted_transfer()-- This subroutine is the main loop for a
370 * formatted data transfer statement. It would be natural to
371 * implement this as a coroutine with the user program, but C makes
372 * that awkward. We loop, processesing format elements. When we
373 * actually have to transfer data instead of just setting flags, we
374 * return control to the user program which calls a subroutine that
375 * supplies the address and type of the next element, then comes back
376 * 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 particular
694 * preventing another / descriptor from being processed) unless there
695 * is another data item to be transferred. */
696 consume_data_flag
= 0 ;
702 internal_error ("Bad format node");
705 /* Free a buffer that we had to allocate during a sequential
706 * formatted read of a block that was larger than the static
709 if (line_buffer
!= NULL
)
711 free_mem (line_buffer
);
715 /* Adjust the item count and data pointer */
717 if ((consume_data_flag
> 0) && (n
> 0))
720 p
= ((char *) p
) + len
;
726 /* Come here when we need a data descriptor but don't have one. We
727 * push the current format node back onto the input, then return and
728 * let the user program call us back with the data. */
736 /* Data transfer entry points. The type of the data entity is
737 * implicit in the subroutine call. This prevents us from having to
738 * share a common enum with the compiler. */
741 transfer_integer (void *p
, int kind
)
745 if (ioparm
.library_return
!= LIBRARY_OK
)
747 transfer (BT_INTEGER
, p
, kind
);
752 transfer_real (void *p
, int kind
)
756 if (ioparm
.library_return
!= LIBRARY_OK
)
758 transfer (BT_REAL
, p
, kind
);
763 transfer_logical (void *p
, int kind
)
767 if (ioparm
.library_return
!= LIBRARY_OK
)
769 transfer (BT_LOGICAL
, p
, kind
);
774 transfer_character (void *p
, int len
)
778 if (ioparm
.library_return
!= LIBRARY_OK
)
780 transfer (BT_CHARACTER
, p
, len
);
785 transfer_complex (void *p
, int kind
)
789 if (ioparm
.library_return
!= LIBRARY_OK
)
791 transfer (BT_COMPLEX
, p
, kind
);
795 /* us_read()-- Preposition a sequential unformatted file while reading. */
803 n
= sizeof (gfc_offset
);
804 p
= (gfc_offset
*) salloc_r (current_unit
->s
, &n
);
806 if (p
== NULL
|| n
!= sizeof (gfc_offset
))
808 generate_error (ERROR_BAD_US
, NULL
);
812 current_unit
->bytes_left
= *p
;
816 /* us_write()-- Preposition a sequential unformatted file while
817 * writing. This amount to writing a bogus length that will be filled
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 /* pre_position()-- position to the next record prior to transfer. We
849 * are assumed to be before the next record. We also calculate the
850 * bytes in the next record. */
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 /* data_transfer_init()-- Initialize things for a data transfer. This
881 * code is common for 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 */
1014 if (ioparm
.end
!= 0)
1015 generate_error (ERROR_OPTION_CONFLICT
,
1016 "END specification cannot appear in a write statement");
1018 if (ioparm
.eor
!= 0)
1019 generate_error (ERROR_OPTION_CONFLICT
,
1020 "EOR specification cannot appear in a write statement");
1022 if (ioparm
.size
!= 0)
1023 generate_error (ERROR_OPTION_CONFLICT
,
1024 "SIZE specification cannot appear in a write statement");
1027 if (advance_status
== ADVANCE_UNSPECIFIED
)
1028 advance_status
= ADVANCE_YES
;
1029 if (ioparm
.library_return
!= LIBRARY_OK
)
1032 /* Sanity checks on the record number */
1036 if (ioparm
.rec
<= 0)
1038 generate_error (ERROR_BAD_OPTION
, "Record number must be positive");
1042 if (ioparm
.rec
>= current_unit
->maxrec
)
1044 generate_error (ERROR_BAD_OPTION
, "Record number too large");
1048 /* Position the file */
1050 if (sseek (current_unit
->s
,
1051 (ioparm
.rec
- 1) * current_unit
->recl
) == FAILURE
)
1052 generate_error (ERROR_OS
, NULL
);
1055 /* Set the initial value of flags */
1057 g
.blank_status
= current_unit
->flags
.blank
;
1058 g
.sign_status
= SIGN_S
;
1066 /* Set up the subroutine that will handle the transfers */
1070 if (current_unit
->flags
.form
== FORM_UNFORMATTED
)
1071 transfer
= unformatted_read
;
1074 if (ioparm
.list_format
)
1076 transfer
= list_formatted_read
;
1080 transfer
= formatted_transfer
;
1085 if (current_unit
->flags
.form
== FORM_UNFORMATTED
)
1086 transfer
= unformatted_write
;
1089 if (ioparm
.list_format
)
1090 transfer
= list_formatted_write
;
1092 transfer
= formatted_transfer
;
1096 /* Make sure that we don't do a read after a nonadvancing write */
1100 if (current_unit
->read_bad
)
1102 generate_error (ERROR_BAD_OPTION
,
1103 "Cannot READ after a nonadvancing WRITE");
1109 if (advance_status
== ADVANCE_YES
)
1110 current_unit
->read_bad
= 1;
1113 /* Start the data transfer if we are doing a formatted transfer */
1114 if (current_unit
->flags
.form
== FORM_FORMATTED
&& !ioparm
.list_format
1115 && ioparm
.namelist_name
== NULL
&& ionml
== NULL
)
1117 formatted_transfer (0, NULL
, 0);
1122 /* next_record_r()-- Space to the next record for read mode. If the
1123 * file is not seekable, we read MAX_READ chunks until we get to the
1124 * right position. */
1126 #define MAX_READ 4096
1129 next_record_r (int done
)
1131 int rlength
, length
;
1135 switch (current_mode ())
1137 case UNFORMATTED_SEQUENTIAL
:
1138 current_unit
->bytes_left
+= sizeof (gfc_offset
); /* Skip over tail */
1142 case FORMATTED_DIRECT
:
1143 case UNFORMATTED_DIRECT
:
1144 if (current_unit
->bytes_left
== 0)
1147 if (is_seekable (current_unit
->s
))
1149 new = file_position (current_unit
->s
) + current_unit
->bytes_left
;
1151 /* Direct access files do not generate END conditions, only I/O errors */
1153 if (sseek (current_unit
->s
, new) == FAILURE
)
1154 generate_error (ERROR_OS
, NULL
);
1158 { /* Seek by reading data */
1159 while (current_unit
->bytes_left
> 0)
1161 rlength
= length
= (MAX_READ
> current_unit
->bytes_left
) ?
1162 MAX_READ
: current_unit
->bytes_left
;
1164 p
= salloc_r (current_unit
->s
, &rlength
);
1167 generate_error (ERROR_OS
, NULL
);
1171 current_unit
->bytes_left
-= length
;
1177 case FORMATTED_SEQUENTIAL
:
1179 if (sf_seen_eor
&& done
)
1184 p
= salloc_r (current_unit
->s
, &length
);
1186 /*In case of internal file, there may not be any '\n'.*/
1187 if (is_internal_unit() && p
== NULL
)
1194 generate_error (ERROR_OS
, NULL
);
1200 current_unit
->endfile
= AT_ENDFILE
;
1209 if (current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
1210 test_endfile (current_unit
);
1214 /* next_record_w()-- Position to the next record in write mode */
1217 next_record_w (int done
)
1223 switch (current_mode ())
1225 case FORMATTED_DIRECT
:
1226 if (current_unit
->bytes_left
== 0)
1229 length
= current_unit
->bytes_left
;
1230 p
= salloc_w (current_unit
->s
, &length
);
1235 memset (p
, ' ', current_unit
->bytes_left
);
1236 if (sfree (current_unit
->s
) == FAILURE
)
1240 case UNFORMATTED_DIRECT
:
1241 if (sfree (current_unit
->s
) == FAILURE
)
1245 case UNFORMATTED_SEQUENTIAL
:
1246 m
= current_unit
->recl
- current_unit
->bytes_left
; /* Bytes written */
1247 c
= file_position (current_unit
->s
);
1249 length
= sizeof (gfc_offset
);
1251 /* Write the length tail */
1253 p
= salloc_w (current_unit
->s
, &length
);
1257 *((gfc_offset
*) p
) = m
;
1258 if (sfree (current_unit
->s
) == FAILURE
)
1261 /* Seek to the head and overwrite the bogus length with the real length */
1263 p
= salloc_w_at (current_unit
->s
, &length
, c
- m
- length
);
1265 generate_error (ERROR_OS
, NULL
);
1267 *((gfc_offset
*) p
) = m
;
1268 if (sfree (current_unit
->s
) == FAILURE
)
1271 /* Seek past the end of the current record */
1273 if (sseek (current_unit
->s
, c
+ sizeof (gfc_offset
)) == FAILURE
)
1278 case FORMATTED_SEQUENTIAL
:
1280 p
= salloc_w (current_unit
->s
, &length
);
1282 if (!is_internal_unit())
1285 *p
= '\n'; /* no CR for internal writes */
1290 if (sfree (current_unit
->s
) == FAILURE
)
1296 generate_error (ERROR_OS
, NULL
);
1302 /* next_record()-- Position to the next record, which means moving to
1303 * the end of the current record. This can happen under several
1304 * different conditions. If the done flag is not set, we get ready to
1305 * process the next record. */
1308 next_record (int done
)
1310 gfc_offset fp
; /* file position */
1312 current_unit
->read_bad
= 0;
1314 if (g
.mode
== READING
)
1315 next_record_r (done
);
1317 next_record_w (done
);
1319 current_unit
->current_record
= 0;
1320 if (current_unit
->flags
.access
== ACCESS_DIRECT
)
1322 fp
= file_position (current_unit
->s
);
1323 /* Calculate next record, rounding up partial records. */
1324 current_unit
->last_record
= (fp
+ current_unit
->recl
- 1)
1325 / current_unit
->recl
;
1328 current_unit
->last_record
++;
1335 /* Finalize the current data transfer. For a nonadvancing transfer,
1336 * this means advancing to the next record. */
1339 finalize_transfer (void)
1342 if (setjmp (g
.eof_jump
))
1344 generate_error (ERROR_END
, NULL
);
1348 if ((ionml
!= NULL
) && (ioparm
.namelist_name
!= NULL
))
1350 if (ioparm
.namelist_read_mode
)
1357 if (current_unit
== NULL
)
1360 if (ioparm
.list_format
&& g
.mode
== READING
)
1361 finish_list_read ();
1366 if (advance_status
== ADVANCE_NO
)
1368 /* Most systems buffer lines, so force the partial record
1369 to be written out. */
1370 flush (current_unit
->s
);
1375 current_unit
->current_record
= 0;
1378 sfree (current_unit
->s
);
1382 /* Transfer function for IOLENGTH. It doesn't actually do any
1383 data transfer, it just updates the length counter. */
1386 iolength_transfer (bt type
, void *dest
, int len
)
1388 if (ioparm
.iolength
!= NULL
)
1389 *ioparm
.iolength
+= len
;
1393 /* Initialize the IOLENGTH data transfer. This function is in essence
1394 a very much simplified version of data_transfer_init(), because it
1395 doesn't have to deal with units at all. */
1398 iolength_transfer_init (void)
1401 if (ioparm
.iolength
!= NULL
)
1402 *ioparm
.iolength
= 0;
1406 /* Set up the subroutine that will handle the transfers. */
1408 transfer
= iolength_transfer
;
1413 /* Library entry point for the IOLENGTH form of the INQUIRE
1414 statement. The IOLENGTH form requires no I/O to be performed, but
1415 it must still be a runtime library call so that we can determine
1416 the iolength for dynamic arrays and such. */
1423 iolength_transfer_init ();
1427 st_iolength_done (void)
1433 /* The READ statement */
1441 data_transfer_init (1);
1443 /* Handle complications dealing with the endfile record. It is
1444 * significant that this is the only place where ERROR_END is
1445 * generated. Reading an end of file elsewhere is either end of
1446 * record or an I/O error. */
1448 if (current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
1449 switch (current_unit
->endfile
)
1455 if (!is_internal_unit())
1457 generate_error (ERROR_END
, NULL
);
1458 current_unit
->endfile
= AFTER_ENDFILE
;
1463 generate_error (ERROR_ENDFILE
, NULL
);
1472 finalize_transfer ();
1483 data_transfer_init (0);
1488 st_write_done (void)
1491 finalize_transfer ();
1493 /* Deal with endfile conditions associated with sequential files */
1495 if (current_unit
!= NULL
&& current_unit
->flags
.access
== ACCESS_SEQUENTIAL
)
1496 switch (current_unit
->endfile
)
1498 case AT_ENDFILE
: /* Remain at the endfile record */
1502 current_unit
->endfile
= AT_ENDFILE
; /* Just at it now */
1505 case NO_ENDFILE
: /* Get rid of whatever is after this record */
1506 if (struncate (current_unit
->s
) == FAILURE
)
1507 generate_error (ERROR_OS
, NULL
);
1509 current_unit
->endfile
= AT_ENDFILE
;
1518 st_set_nml_var (void * var_addr
, char * var_name
, int var_name_len
,
1519 int kind
, bt type
, int string_length
)
1521 namelist_info
*t1
= NULL
, *t2
= NULL
;
1522 namelist_info
*nml
= (namelist_info
*) get_mem (sizeof(
1524 nml
->mem_pos
= var_addr
;
1527 assert (var_name_len
> 0);
1528 nml
->var_name
= (char*) get_mem (var_name_len
+1);
1529 strncpy (nml
->var_name
, var_name
, var_name_len
);
1530 nml
->var_name
[var_name_len
] = 0;
1534 assert (var_name_len
== 0);
1535 nml
->var_name
= NULL
;
1540 nml
->string_length
= string_length
;
1559 st_set_nml_var_int (void * var_addr
, char * var_name
, int var_name_len
,
1562 st_set_nml_var (var_addr
, var_name
, var_name_len
, kind
, BT_INTEGER
, 0);
1566 st_set_nml_var_float (void * var_addr
, char * var_name
, int var_name_len
,
1569 st_set_nml_var (var_addr
, var_name
, var_name_len
, kind
, BT_REAL
, 0);
1573 st_set_nml_var_char (void * var_addr
, char * var_name
, int var_name_len
,
1574 int kind
, gfc_strlen_type string_length
)
1576 st_set_nml_var (var_addr
, var_name
, var_name_len
, kind
, BT_CHARACTER
,
1581 st_set_nml_var_complex (void * var_addr
, char * var_name
, int var_name_len
,
1584 st_set_nml_var (var_addr
, var_name
, var_name_len
, kind
, BT_COMPLEX
, 0);
1588 st_set_nml_var_log (void * var_addr
, char * var_name
, int var_name_len
,
1591 st_set_nml_var (var_addr
, var_name
, var_name_len
, kind
, BT_LOGICAL
, 0);