Fix typo.
[official-gcc.git] / libgfortran / io / transfer.c
blobdc12745bb133d43863712254c25e9fb43b699a89
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)
10 any later version.
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. */
25 #include "config.h"
26 #include <string.h>
27 #include <assert.h>
28 #include "libgfortran.h"
29 #include "io.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
42 * statement.
44 * transfer_integer
45 * transfer_logical
46 * transfer_character
47 * transfer_real
48 * transfer_complex
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
55 * transferred.
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[] = {
67 {"yes", ADVANCE_YES},
68 {"no", ADVANCE_NO},
69 {NULL}
73 static void (*transfer) (bt, void *, int);
76 typedef enum
77 { FORMATTED_SEQUENTIAL, UNFORMATTED_SEQUENTIAL,
78 FORMATTED_DIRECT, UNFORMATTED_DIRECT
80 file_mode;
83 static file_mode
84 current_mode (void)
86 file_mode m;
88 if (current_unit->flags.access == ACCESS_DIRECT)
90 m = current_unit->flags.form == FORM_FORMATTED ?
91 FORMATTED_DIRECT : UNFORMATTED_DIRECT;
93 else
95 m = current_unit->flags.form == FORM_FORMATTED ?
96 FORMATTED_SEQUENTIAL : UNFORMATTED_SEQUENTIAL;
99 return m;
103 /* Mid level data transfer statements. These subroutines do reading
104 * and writing in the style of salloc_r()/salloc_w() within the
105 * current record. */
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. */
119 static char *
120 read_sf (int *length)
122 static char data[SCRATCH_SIZE];
123 char *base, *p, *q;
124 int n, unity;
126 if (*length > SCRATCH_SIZE)
127 p = base = line_buffer = get_mem (*length);
128 else
129 p = base = data;
131 memset(base,'\0',*length);
133 current_unit->bytes_left = options.default_recl;
134 unity = 1;
135 n = 0;
139 if (is_internal_unit())
141 /* unity may be modified inside salloc_r if is_internal_unit() is true */
142 unity = 1;
145 q = salloc_r (current_unit->s, &unity);
146 if (q == NULL)
147 break;
149 if (*q == '\n')
151 if (current_unit->unit_number == options.stdin_unit)
153 if (n <= 0)
154 continue;
156 /* Unexpected end of line */
157 if (current_unit->flags.pad == PAD_NO)
159 generate_error (ERROR_EOR, NULL);
160 return NULL;
163 current_unit->bytes_left = 0;
164 *length = n;
165 sf_seen_eor = 1;
166 break;
169 n++;
170 *p++ = *q;
171 sf_seen_eor = 0;
173 while (n < *length);
175 return base;
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
187 * short reads. */
189 void *
190 read_block (int *length)
192 char *source;
193 int nread;
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 */
204 return NULL;
207 *length = current_unit->bytes_left;
210 current_unit->bytes_left -= *length;
212 nread = *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)
221 *length = nread;
222 else
224 generate_error (ERROR_EOR, NULL);
225 source = NULL;
229 return source;
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. */
238 void *
239 write_block (int length)
241 char *dest;
243 if (!is_internal_unit() && current_unit->bytes_left < length)
245 generate_error (ERROR_EOR, NULL);
246 return NULL;
249 current_unit->bytes_left -= length;
250 dest = salloc_w (current_unit->s, &length);
252 if (ioparm.size != NULL)
253 *ioparm.size += length;
255 return dest;
259 /* unformatted_read()-- Master function for unformatted reads. */
261 static void
262 unformatted_read (bt type, void *dest, int length)
264 void *source;
265 int w;
266 w = length;
267 source = read_block (&w);
269 if (source != NULL)
271 memcpy (dest, source, w);
272 if (length != w)
273 memset (((char *) dest) + w, ' ', length - w);
277 static void
278 unformatted_write (bt type, void *source, int length)
280 void *dest;
281 dest = write_block (length);
282 if (dest != NULL)
283 memcpy (dest, source, length);
287 /* type_name()-- Return a pointer to the name of a type. */
289 const char *
290 type_name (bt type)
292 const char *p;
294 switch (type)
296 case BT_INTEGER:
297 p = "INTEGER";
298 break;
299 case BT_LOGICAL:
300 p = "LOGICAL";
301 break;
302 case BT_CHARACTER:
303 p = "CHARACTER";
304 break;
305 case BT_REAL:
306 p = "REAL";
307 break;
308 case BT_COMPLEX:
309 p = "COMPLEX";
310 break;
311 default:
312 internal_error ("type_name(): Bad type");
315 return p;
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. */
323 static void
324 write_constant_string (fnode * f)
326 char c, delimiter, *p, *q;
327 int length;
329 length = f->u.string.length;
330 if (length == 0)
331 return;
333 p = write_block (length);
334 if (p == NULL)
335 return;
337 q = f->u.string.p;
338 delimiter = q[-1];
340 for (; length > 0; length--)
342 c = *p++ = *q++;
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. */
353 static int
354 require_type (bt expected, bt actual, fnode * f)
356 char buffer[100];
358 if (actual == expected)
359 return 0;
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);
365 return 1;
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. */
378 static void
379 formatted_transfer (bt type, void *p, int len)
381 int pos ,m ;
382 fnode *f;
383 int i, n;
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)
390 type = BT_REAL;
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;
398 next_record (0);
400 for (;;)
402 consume_data_flag = 1 ;
403 if (ioparm.library_return != LIBRARY_OK)
404 break;
406 f = next_format ();
407 if (f == NULL)
408 return; /* No data descriptors left (already raised) */
410 switch (f->format)
412 case FMT_I:
413 if (n == 0)
414 goto need_data;
415 if (require_type (BT_INTEGER, type, f))
416 return;
418 if (g.mode == READING)
419 read_decimal (f, p, len);
420 else
421 write_i (f, p, len);
423 break;
425 case FMT_B:
426 if (n == 0)
427 goto need_data;
428 if (require_type (BT_INTEGER, type, f))
429 return;
431 if (g.mode == READING)
432 read_radix (f, p, len, 2);
433 else
434 write_b (f, p, len);
436 break;
438 case FMT_O:
439 if (n == 0)
440 goto need_data;
442 if (g.mode == READING)
443 read_radix (f, p, len, 8);
444 else
445 write_o (f, p, len);
447 break;
449 case FMT_Z:
450 if (n == 0)
451 goto need_data;
453 if (g.mode == READING)
454 read_radix (f, p, len, 16);
455 else
456 write_z (f, p, len);
458 break;
460 case FMT_A:
461 if (n == 0)
462 goto need_data;
463 if (require_type (BT_CHARACTER, type, f))
464 return;
466 if (g.mode == READING)
467 read_a (f, p, len);
468 else
469 write_a (f, p, len);
471 break;
473 case FMT_L:
474 if (n == 0)
475 goto need_data;
477 if (g.mode == READING)
478 read_l (f, p, len);
479 else
480 write_l (f, p, len);
482 break;
484 case FMT_D:
485 if (n == 0)
486 goto need_data;
487 if (require_type (BT_REAL, type, f))
488 return;
490 if (g.mode == READING)
491 read_f (f, p, len);
492 else
493 write_d (f, p, len);
495 break;
497 case FMT_E:
498 if (n == 0)
499 goto need_data;
500 if (require_type (BT_REAL, type, f))
501 return;
503 if (g.mode == READING)
504 read_f (f, p, len);
505 else
506 write_e (f, p, len);
507 break;
509 case FMT_EN:
510 if (n == 0)
511 goto need_data;
512 if (require_type (BT_REAL, type, f))
513 return;
515 if (g.mode == READING)
516 read_f (f, p, len);
517 else
518 write_en (f, p, len);
520 break;
522 case FMT_ES:
523 if (n == 0)
524 goto need_data;
525 if (require_type (BT_REAL, type, f))
526 return;
528 if (g.mode == READING)
529 read_f (f, p, len);
530 else
531 write_es (f, p, len);
533 break;
535 case FMT_F:
536 if (n == 0)
537 goto need_data;
538 if (require_type (BT_REAL, type, f))
539 return;
541 if (g.mode == READING)
542 read_f (f, p, len);
543 else
544 write_f (f, p, len);
546 break;
548 case FMT_G:
549 if (n == 0)
550 goto need_data;
551 if (g.mode == READING)
552 switch (type)
554 case BT_INTEGER:
555 read_decimal (f, p, len);
556 break;
557 case BT_LOGICAL:
558 read_l (f, p, len);
559 break;
560 case BT_CHARACTER:
561 read_a (f, p, len);
562 break;
563 case BT_REAL:
564 read_f (f, p, len);
565 break;
566 default:
567 goto bad_type;
569 else
570 switch (type)
572 case BT_INTEGER:
573 write_i (f, p, len);
574 break;
575 case BT_LOGICAL:
576 write_l (f, p, len);
577 break;
578 case BT_CHARACTER:
579 write_a (f, p, len);
580 break;
581 case BT_REAL:
582 write_d (f, p, len);
583 break;
584 default:
585 bad_type:
586 internal_error ("formatted_transfer(): Bad type");
589 break;
591 case FMT_STRING:
592 consume_data_flag = 0 ;
593 if (g.mode == READING)
595 format_error (f, "Constant string in input format");
596 return;
598 write_constant_string (f);
599 break;
601 /* Format codes that don't transfer data */
602 case FMT_X:
603 case FMT_TR:
604 consume_data_flag = 0 ;
605 if (g.mode == READING)
606 read_x (f);
607 else
608 write_x (f);
610 break;
612 case FMT_TL:
613 case FMT_T:
614 if (f->format==FMT_TL)
616 pos = f->u.n ;
617 pos= current_unit->recl - current_unit->bytes_left - pos;
619 else // FMT==T
621 consume_data_flag = 0 ;
622 pos = f->u.n - 1;
625 if (pos < 0 || pos >= current_unit->recl )
627 generate_error (ERROR_EOR, "T Or TL edit position error");
628 break ;
630 m = pos - (current_unit->recl - current_unit->bytes_left);
632 if (m == 0)
633 break;
635 if (m > 0)
637 f->u.n = m;
638 if (g.mode == READING)
639 read_x (f);
640 else
641 write_x (f);
643 if (m < 0)
645 move_pos_offset (current_unit->s,m);
648 break;
650 case FMT_S:
651 consume_data_flag = 0 ;
652 g.sign_status = SIGN_S;
653 break;
655 case FMT_SS:
656 consume_data_flag = 0 ;
657 g.sign_status = SIGN_SS;
658 break;
660 case FMT_SP:
661 consume_data_flag = 0 ;
662 g.sign_status = SIGN_SP;
663 break;
665 case FMT_BN:
666 consume_data_flag = 0 ;
667 g.blank_status = BLANK_NULL;
668 break;
670 case FMT_BZ:
671 consume_data_flag = 0 ;
672 g.blank_status = BLANK_ZERO;
673 break;
675 case FMT_P:
676 consume_data_flag = 0 ;
677 g.scale_factor = f->u.k;
678 break;
680 case FMT_DOLLAR:
681 consume_data_flag = 0 ;
682 g.seen_dollar = 1;
683 break;
685 case FMT_SLASH:
686 consume_data_flag = 0 ;
687 for (i = 0; i < f->repeat; i++)
688 next_record (0);
690 break;
692 case FMT_COLON:
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 ;
697 if (n == 0)
698 return;
699 break;
701 default:
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
707 * buffer. */
709 if (line_buffer != NULL)
711 free_mem (line_buffer);
712 line_buffer = NULL;
715 /* Adjust the item count and data pointer */
717 if ((consume_data_flag > 0) && (n > 0))
719 n--;
720 p = ((char *) p) + len;
724 return;
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. */
730 need_data:
731 unget_format (f);
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. */
740 void
741 transfer_integer (void *p, int kind)
744 g.item_count++;
745 if (ioparm.library_return != LIBRARY_OK)
746 return;
747 transfer (BT_INTEGER, p, kind);
751 void
752 transfer_real (void *p, int kind)
755 g.item_count++;
756 if (ioparm.library_return != LIBRARY_OK)
757 return;
758 transfer (BT_REAL, p, kind);
762 void
763 transfer_logical (void *p, int kind)
766 g.item_count++;
767 if (ioparm.library_return != LIBRARY_OK)
768 return;
769 transfer (BT_LOGICAL, p, kind);
773 void
774 transfer_character (void *p, int len)
777 g.item_count++;
778 if (ioparm.library_return != LIBRARY_OK)
779 return;
780 transfer (BT_CHARACTER, p, len);
784 void
785 transfer_complex (void *p, int kind)
788 g.item_count++;
789 if (ioparm.library_return != LIBRARY_OK)
790 return;
791 transfer (BT_COMPLEX, p, kind);
795 /* us_read()-- Preposition a sequential unformatted file while reading. */
797 static void
798 us_read (void)
800 gfc_offset *p;
801 int n;
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);
809 return;
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
818 * in later. */
820 static void
821 us_write (void)
823 gfc_offset *p;
824 int length;
826 length = sizeof (gfc_offset);
827 p = (gfc_offset *) salloc_w (current_unit->s, &length);
829 if (p == NULL)
831 generate_error (ERROR_OS, NULL);
832 return;
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. */
852 static void
853 pre_position (void)
856 if (current_unit->current_record)
857 return; /* Already positioned */
859 switch (current_mode ())
861 case UNFORMATTED_SEQUENTIAL:
862 if (g.mode == READING)
863 us_read ();
864 else
865 us_write ();
867 break;
869 case FORMATTED_SEQUENTIAL:
870 case FORMATTED_DIRECT:
871 case UNFORMATTED_DIRECT:
872 current_unit->bytes_left = current_unit->recl;
873 break;
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. */
883 static void
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;
902 else
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;
908 new_unit(&u_flags);
909 current_unit = get_unit (read_flag);
912 if (current_unit == NULL)
913 return;
915 if (is_internal_unit())
917 current_unit->recl = file_length(current_unit->s);
918 if (g.mode==WRITING)
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)
932 return;
934 /* Check the format */
936 if (ioparm.format)
937 parse_format ();
939 if (ioparm.library_return != LIBRARY_OK)
940 return;
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");
969 return;
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");
976 return;
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");
1000 if (read_flag)
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");
1011 else
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)
1030 return;
1032 /* Sanity checks on the record number */
1034 if (ioparm.rec)
1036 if (ioparm.rec <= 0)
1038 generate_error (ERROR_BAD_OPTION, "Record number must be positive");
1039 return;
1042 if (ioparm.rec >= current_unit->maxrec)
1044 generate_error (ERROR_BAD_OPTION, "Record number too large");
1045 return;
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;
1059 g.scale_factor = 0;
1060 g.seen_dollar = 0;
1061 g.first_item = 1;
1062 g.item_count = 0;
1064 pre_position ();
1066 /* Set up the subroutine that will handle the transfers */
1068 if (read_flag)
1070 if (current_unit->flags.form == FORM_UNFORMATTED)
1071 transfer = unformatted_read;
1072 else
1074 if (ioparm.list_format)
1076 transfer = list_formatted_read;
1077 init_at_eol();
1079 else
1080 transfer = formatted_transfer;
1083 else
1085 if (current_unit->flags.form == FORM_UNFORMATTED)
1086 transfer = unformatted_write;
1087 else
1089 if (ioparm.list_format)
1090 transfer = list_formatted_write;
1091 else
1092 transfer = formatted_transfer;
1096 /* Make sure that we don't do a read after a nonadvancing write */
1098 if (read_flag)
1100 if (current_unit->read_bad)
1102 generate_error (ERROR_BAD_OPTION,
1103 "Cannot READ after a nonadvancing WRITE");
1104 return;
1107 else
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
1128 static void
1129 next_record_r (int done)
1131 int rlength, length;
1132 gfc_offset new;
1133 char *p;
1135 switch (current_mode ())
1137 case UNFORMATTED_SEQUENTIAL:
1138 current_unit->bytes_left += sizeof (gfc_offset); /* Skip over tail */
1140 /* Fall through */
1142 case FORMATTED_DIRECT:
1143 case UNFORMATTED_DIRECT:
1144 if (current_unit->bytes_left == 0)
1145 break;
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);
1157 else
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);
1165 if (p == NULL)
1167 generate_error (ERROR_OS, NULL);
1168 break;
1171 current_unit->bytes_left -= length;
1175 break;
1177 case FORMATTED_SEQUENTIAL:
1178 length = 1;
1179 if (sf_seen_eor && done)
1180 break;
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)
1189 break;
1192 if (p == NULL)
1194 generate_error (ERROR_OS, NULL);
1195 break;
1198 if (length == 0)
1200 current_unit->endfile = AT_ENDFILE;
1201 break;
1204 while (*p != '\n');
1206 break;
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 */
1216 static void
1217 next_record_w (int done)
1219 gfc_offset c, m;
1220 int length;
1221 char *p;
1223 switch (current_mode ())
1225 case FORMATTED_DIRECT:
1226 if (current_unit->bytes_left == 0)
1227 break;
1229 length = current_unit->bytes_left;
1230 p = salloc_w (current_unit->s, &length);
1232 if (p == NULL)
1233 goto io_error;
1235 memset (p, ' ', current_unit->bytes_left);
1236 if (sfree (current_unit->s) == FAILURE)
1237 goto io_error;
1238 break;
1240 case UNFORMATTED_DIRECT:
1241 if (sfree (current_unit->s) == FAILURE)
1242 goto io_error;
1243 break;
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);
1254 if (p == NULL)
1255 goto io_error;
1257 *((gfc_offset *) p) = m;
1258 if (sfree (current_unit->s) == FAILURE)
1259 goto io_error;
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);
1264 if (p == NULL)
1265 generate_error (ERROR_OS, NULL);
1267 *((gfc_offset *) p) = m;
1268 if (sfree (current_unit->s) == FAILURE)
1269 goto io_error;
1271 /* Seek past the end of the current record */
1273 if (sseek (current_unit->s, c + sizeof (gfc_offset)) == FAILURE)
1274 goto io_error;
1276 break;
1278 case FORMATTED_SEQUENTIAL:
1279 length = 1;
1280 p = salloc_w (current_unit->s, &length);
1282 if (!is_internal_unit())
1284 if (p)
1285 *p = '\n'; /* no CR for internal writes */
1286 else
1287 goto io_error;
1290 if (sfree (current_unit->s) == FAILURE)
1291 goto io_error;
1293 break;
1295 io_error:
1296 generate_error (ERROR_OS, NULL);
1297 break;
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. */
1307 void
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);
1316 else
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;
1327 else
1328 current_unit->last_record++;
1330 if (!done)
1331 pre_position ();
1335 /* Finalize the current data transfer. For a nonadvancing transfer,
1336 * this means advancing to the next record. */
1338 static void
1339 finalize_transfer (void)
1342 if (setjmp (g.eof_jump))
1344 generate_error (ERROR_END, NULL);
1345 return;
1348 if ((ionml != NULL) && (ioparm.namelist_name != NULL))
1350 if (ioparm.namelist_read_mode)
1351 namelist_read();
1352 else
1353 namelist_write();
1356 transfer = NULL;
1357 if (current_unit == NULL)
1358 return;
1360 if (ioparm.list_format && g.mode == READING)
1361 finish_list_read ();
1362 else
1364 free_fnodes ();
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);
1371 return;
1374 next_record (1);
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. */
1385 static void
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. */
1397 static void
1398 iolength_transfer_init (void)
1401 if (ioparm.iolength != NULL)
1402 *ioparm.iolength = 0;
1404 g.item_count = 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. */
1418 void
1419 st_iolength (void)
1421 library_start ();
1423 iolength_transfer_init ();
1426 void
1427 st_iolength_done (void)
1429 library_end ();
1433 /* The READ statement */
1435 void
1436 st_read (void)
1439 library_start ();
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)
1451 case NO_ENDFILE:
1452 break;
1454 case AT_ENDFILE:
1455 if (!is_internal_unit())
1457 generate_error (ERROR_END, NULL);
1458 current_unit->endfile = AFTER_ENDFILE;
1460 break;
1462 case AFTER_ENDFILE:
1463 generate_error (ERROR_ENDFILE, NULL);
1464 break;
1469 void
1470 st_read_done (void)
1472 finalize_transfer ();
1474 library_end ();
1478 void
1479 st_write (void)
1482 library_start ();
1483 data_transfer_init (0);
1487 void
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 */
1499 break;
1501 case AFTER_ENDFILE:
1502 current_unit->endfile = AT_ENDFILE; /* Just at it now */
1503 break;
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;
1510 break;
1513 library_end ();
1517 static void
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(
1523 namelist_info ));
1524 nml->mem_pos = var_addr;
1525 if (var_name)
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;
1532 else
1534 assert (var_name_len == 0);
1535 nml->var_name = NULL;
1538 nml->len = kind;
1539 nml->type = type;
1540 nml->string_length = string_length;
1542 nml->next = NULL;
1544 if (ionml == NULL)
1545 ionml = nml;
1546 else
1548 t1 = ionml;
1549 while (t1 != NULL)
1551 t2 = t1;
1552 t1 = t1->next;
1554 t2->next = nml;
1558 void
1559 st_set_nml_var_int (void * var_addr, char * var_name, int var_name_len,
1560 int kind)
1562 st_set_nml_var (var_addr, var_name, var_name_len, kind, BT_INTEGER, 0);
1565 void
1566 st_set_nml_var_float (void * var_addr, char * var_name, int var_name_len,
1567 int kind)
1569 st_set_nml_var (var_addr, var_name, var_name_len, kind, BT_REAL, 0);
1572 void
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,
1577 string_length);
1580 void
1581 st_set_nml_var_complex (void * var_addr, char * var_name, int var_name_len,
1582 int kind)
1584 st_set_nml_var (var_addr, var_name, var_name_len, kind, BT_COMPLEX, 0);
1587 void
1588 st_set_nml_var_log (void * var_addr, char * var_name, int var_name_len,
1589 int kind)
1591 st_set_nml_var (var_addr, var_name, var_name_len, kind, BT_LOGICAL, 0);