1 /* Copyright (C) 2002, 2003, 2004, 2005 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, 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
21 /* As a special exception, if you link this library with other files,
22 some of which are compiled with GCC, to produce an executable,
23 this library does not by itself cause the resulting executable
24 to be covered by the GNU General Public License.
25 This exception does not however invalidate any other reasons why
26 the executable file might be covered by the GNU General Public License. */
31 /* IO library include. */
34 #include "libgfortran.h"
35 #ifdef HAVE_PRAGMA_WEAK
37 #define SUPPORTS_WEAK 1
41 #define DEFAULT_TEMPDIR "/tmp"
43 /* Basic types used in data transfers. */
46 { BT_NULL, BT_INTEGER, BT_LOGICAL, BT_CHARACTER, BT_REAL,
53 { SUCCESS = 1, FAILURE }
56 struct st_parameter_dt;
60 char *(*alloc_w_at) (struct stream *, int *, gfc_offset);
61 char *(*alloc_r_at) (struct stream *, int *, gfc_offset);
62 try (*sfree) (struct stream *);
63 try (*close) (struct stream *);
64 try (*seek) (struct stream *, gfc_offset);
65 try (*truncate) (struct stream *);
66 int (*read) (struct stream *, void *, size_t *);
67 int (*write) (struct stream *, const void *, size_t *);
72 /* Macros for doing file I/O given a stream. */
74 #define sfree(s) ((s)->sfree)(s)
75 #define sclose(s) ((s)->close)(s)
77 #define salloc_r(s, len) ((s)->alloc_r_at)(s, len, -1)
78 #define salloc_w(s, len) ((s)->alloc_w_at)(s, len, -1)
80 #define salloc_r_at(s, len, where) ((s)->alloc_r_at)(s, len, where)
81 #define salloc_w_at(s, len, where) ((s)->alloc_w_at)(s, len, where)
83 #define sseek(s, pos) ((s)->seek)(s, pos)
84 #define struncate(s) ((s)->truncate)(s)
85 #define sread(s, buf, nbytes) ((s)->read)(s, buf, nbytes)
86 #define swrite(s, buf, nbytes) ((s)->write)(s, buf, nbytes)
88 /* The array_loop_spec contains the variables for the loops over index ranges
89 that are encountered. Since the variables can be negative, ssize_t
92 typedef struct array_loop_spec
94 /* Index counter for this dimension. */
97 /* Start for the index counter. */
100 /* End for the index counter. */
103 /* Step for the index counter. */
108 /* Representation of a namelist object in libgfortran
111 &GROUPNAME OBJECT=value[s] [,OBJECT=value[s]].../
113 &GROUPNAME OBJECT=value[s] [,OBJECT=value[s]]...&END
115 The object can be a fully qualified, compound name for an instrinsic
116 type, derived types or derived type components. So, a substring
117 a(:)%b(4)%ch(2:4)(1:7) has to be treated correctly in namelist
118 read. Hence full information about the structure of the object has
119 to be available to list_read.c and write.
121 These requirements are met by the following data structures.
123 namelist_info type contains all the scalar information about the
124 object and arrays of descriptor_dimension and array_loop_spec types for
127 typedef struct namelist_type
130 /* Object type, stored as GFC_DTYPE_xxxx. */
136 /* Address for the start of the object's data. */
139 /* Flag to show that a read is to be attempted for this node. */
142 /* Length of intrinsic type in bytes. */
145 /* Rank of the object. */
148 /* Overall size of the object in bytes. */
151 /* Length of character string. */
152 index_type string_length;
154 descriptor_dimension * dim;
155 array_loop_spec * ls;
156 struct namelist_type * next;
160 /* Options for the OPEN statement. */
163 { ACCESS_SEQUENTIAL, ACCESS_DIRECT, ACCESS_APPEND,
169 { ACTION_READ, ACTION_WRITE, ACTION_READWRITE,
175 { BLANK_NULL, BLANK_ZERO, BLANK_UNSPECIFIED }
179 { DELIM_NONE, DELIM_APOSTROPHE, DELIM_QUOTE,
185 { FORM_FORMATTED, FORM_UNFORMATTED, FORM_UNSPECIFIED }
189 { POSITION_ASIS, POSITION_REWIND, POSITION_APPEND,
195 { STATUS_UNKNOWN, STATUS_OLD, STATUS_NEW, STATUS_SCRATCH,
196 STATUS_REPLACE, STATUS_UNSPECIFIED
201 { PAD_YES, PAD_NO, PAD_UNSPECIFIED }
205 { ADVANCE_YES, ADVANCE_NO, ADVANCE_UNSPECIFIED }
212 #define CHARACTER1(name) \
214 gfc_charlen_type name ## _len
215 #define CHARACTER2(name) \
216 gfc_charlen_type name ## _len; \
219 #define IOPARM_LIBRETURN_MASK (3 << 0)
220 #define IOPARM_LIBRETURN_OK (0 << 0)
221 #define IOPARM_LIBRETURN_ERROR (1 << 0)
222 #define IOPARM_LIBRETURN_END (2 << 0)
223 #define IOPARM_LIBRETURN_EOR (3 << 0)
224 #define IOPARM_ERR (1 << 2)
225 #define IOPARM_END (1 << 3)
226 #define IOPARM_EOR (1 << 4)
227 #define IOPARM_HAS_IOSTAT (1 << 5)
228 #define IOPARM_HAS_IOMSG (1 << 6)
230 #define IOPARM_COMMON_MASK ((1 << 7) - 1)
232 typedef struct st_parameter_common
236 const char *filename;
239 GFC_INTEGER_4 *iostat;
243 #define IOPARM_OPEN_HAS_RECL_IN (1 << 7)
244 #define IOPARM_OPEN_HAS_FILE (1 << 8)
245 #define IOPARM_OPEN_HAS_STATUS (1 << 9)
246 #define IOPARM_OPEN_HAS_ACCESS (1 << 10)
247 #define IOPARM_OPEN_HAS_FORM (1 << 11)
248 #define IOPARM_OPEN_HAS_BLANK (1 << 12)
249 #define IOPARM_OPEN_HAS_POSITION (1 << 13)
250 #define IOPARM_OPEN_HAS_ACTION (1 << 14)
251 #define IOPARM_OPEN_HAS_DELIM (1 << 15)
252 #define IOPARM_OPEN_HAS_PAD (1 << 16)
256 st_parameter_common common;
257 GFC_INTEGER_4 recl_in;
263 CHARACTER1 (position);
270 #define IOPARM_CLOSE_HAS_STATUS (1 << 7)
274 st_parameter_common common;
281 st_parameter_common common;
283 st_parameter_filepos;
285 #define IOPARM_INQUIRE_HAS_EXIST (1 << 7)
286 #define IOPARM_INQUIRE_HAS_OPENED (1 << 8)
287 #define IOPARM_INQUIRE_HAS_NUMBER (1 << 9)
288 #define IOPARM_INQUIRE_HAS_NAMED (1 << 10)
289 #define IOPARM_INQUIRE_HAS_NEXTREC (1 << 11)
290 #define IOPARM_INQUIRE_HAS_RECL_OUT (1 << 12)
291 #define IOPARM_INQUIRE_HAS_FILE (1 << 13)
292 #define IOPARM_INQUIRE_HAS_ACCESS (1 << 14)
293 #define IOPARM_INQUIRE_HAS_FORM (1 << 15)
294 #define IOPARM_INQUIRE_HAS_BLANK (1 << 16)
295 #define IOPARM_INQUIRE_HAS_POSITION (1 << 17)
296 #define IOPARM_INQUIRE_HAS_ACTION (1 << 18)
297 #define IOPARM_INQUIRE_HAS_DELIM (1 << 19)
298 #define IOPARM_INQUIRE_HAS_PAD (1 << 20)
299 #define IOPARM_INQUIRE_HAS_NAME (1 << 21)
300 #define IOPARM_INQUIRE_HAS_SEQUENTIAL (1 << 22)
301 #define IOPARM_INQUIRE_HAS_DIRECT (1 << 23)
302 #define IOPARM_INQUIRE_HAS_FORMATTED (1 << 24)
303 #define IOPARM_INQUIRE_HAS_UNFORMATTED (1 << 25)
304 #define IOPARM_INQUIRE_HAS_READ (1 << 26)
305 #define IOPARM_INQUIRE_HAS_WRITE (1 << 27)
306 #define IOPARM_INQUIRE_HAS_READWRITE (1 << 28)
310 st_parameter_common common;
311 GFC_INTEGER_4 *exist, *opened, *number, *named;
312 GFC_INTEGER_4 *nextrec, *recl_out;
317 CHARACTER1 (position);
322 CHARACTER2 (sequential);
324 CHARACTER2 (formatted);
325 CHARACTER1 (unformatted);
328 CHARACTER2 (readwrite);
330 st_parameter_inquire;
335 #define IOPARM_DT_LIST_FORMAT (1 << 7)
336 #define IOPARM_DT_NAMELIST_READ_MODE (1 << 8)
337 #define IOPARM_DT_HAS_REC (1 << 9)
338 #define IOPARM_DT_HAS_SIZE (1 << 10)
339 #define IOPARM_DT_HAS_IOLENGTH (1 << 11)
340 #define IOPARM_DT_HAS_FORMAT (1 << 12)
341 #define IOPARM_DT_HAS_ADVANCE (1 << 13)
342 #define IOPARM_DT_HAS_INTERNAL_UNIT (1 << 14)
343 #define IOPARM_DT_HAS_NAMELIST_NAME (1 << 15)
344 /* Internal use bit. */
345 #define IOPARM_DT_IONML_SET (1 << 31)
347 typedef struct st_parameter_dt
349 st_parameter_common common;
351 GFC_INTEGER_4 *size, *iolength;
352 gfc_array_char *internal_unit_desc;
354 CHARACTER2 (advance);
355 CHARACTER1 (internal_unit);
356 CHARACTER2 (namelist_name);
357 /* Private part of the structure. The compiler just needs
358 to reserve enough space. */
363 void (*transfer) (struct st_parameter_dt *, bt, void *, int,
365 struct gfc_unit *current_unit;
366 int item_count; /* Item number in a formatted data transfer. */
368 unit_blank blank_status;
369 enum {SIGN_S, SIGN_SS, SIGN_SP} sign_status;
371 int max_pos; /* Maximum righthand column written to. */
372 /* Number of skips + spaces to be done for T and X-editing. */
374 /* Number of spaces to be done for T and X-editing. */
376 unit_advance advance_status;
377 char reversion_flag; /* Format reversion has occurred. */
382 char no_leading_blank;
389 /* A namelist specific flag used in the list directed library
390 to flag that calls are being made from namelist read (eg. to
391 ignore comments or to treat '/' as a terminator) */
393 /* A namelist specific flag used in the list directed library
394 to flag read errors and return, so that an attempt can be
395 made to read a new object name. */
397 /* Storage area for values except for strings. Must be large
398 enough to hold a complex value (two reals) of the largest
408 struct format_data *fmt;
410 namelist_info *ionml;
412 char pad[16 * sizeof (char *) + 32 * sizeof (int)];
428 unit_position position;
435 /* The default value of record length for preconnected units is defined
436 here. This value can be overriden by an environment variable.
437 Default value is 1 Gb. */
439 #define DEFAULT_RECL 1073741824
442 typedef struct gfc_unit
448 struct gfc_unit *left, *right;
451 int read_bad, current_record;
453 { NO_ENDFILE, AT_ENDFILE, AFTER_ENDFILE }
459 /* recl -- Record length of the file.
460 last_record -- Last record number read or written
461 maxrec -- Maximum record number in a direct access file
462 bytes_left -- Bytes left in current record. */
463 gfc_offset recl, last_record, maxrec, bytes_left;
465 __gthread_mutex_t lock;
466 /* Number of threads waiting to acquire this unit's lock.
467 When non-zero, close_unit doesn't only removes the unit
468 from the UNIT_ROOT tree, but doesn't free it and the
469 last of the waiting threads will do that.
470 This must be either atomically increased/decreased, or
471 always guarded by UNIT_LOCK. */
473 /* Flag set by close_unit if the unit as been closed.
474 Must be manipulated under unit's lock. */
477 /* For traversing arrays */
486 /* Format tokens. Only about half of these can be stored in the
491 FMT_NONE = 0, FMT_UNKNOWN, FMT_SIGNED_INT, FMT_ZERO, FMT_POSINT, FMT_PERIOD,
492 FMT_COMMA, FMT_COLON, FMT_SLASH, FMT_DOLLAR, FMT_T, FMT_TR, FMT_TL,
493 FMT_LPAREN, FMT_RPAREN, FMT_X, FMT_S, FMT_SS, FMT_SP, FMT_STRING,
494 FMT_BADSTRING, FMT_P, FMT_I, FMT_B, FMT_BN, FMT_BZ, FMT_O, FMT_Z, FMT_F,
495 FMT_E, FMT_EN, FMT_ES, FMT_G, FMT_L, FMT_A, FMT_D, FMT_H, FMT_END
500 /* Format nodes. A format string is converted into a tree of these
501 structures, which is traversed as part of a data transfer statement. */
540 /* Members for traversing the tree during data transfer. */
543 struct fnode *current;
551 extern int move_pos_offset (stream *, int);
552 internal_proto(move_pos_offset);
554 extern int compare_files (stream *, stream *);
555 internal_proto(compare_files);
557 extern stream *open_external (st_parameter_open *, unit_flags *);
558 internal_proto(open_external);
560 extern stream *open_internal (char *, int);
561 internal_proto(open_internal);
563 extern stream *input_stream (void);
564 internal_proto(input_stream);
566 extern stream *output_stream (void);
567 internal_proto(output_stream);
569 extern stream *error_stream (void);
570 internal_proto(error_stream);
572 extern int compare_file_filename (gfc_unit *, const char *, int);
573 internal_proto(compare_file_filename);
575 extern gfc_unit *find_file (const char *file, gfc_charlen_type file_len);
576 internal_proto(find_file);
578 extern void flush_all_units (void);
579 internal_proto(flush_all_units);
581 extern int stream_at_bof (stream *);
582 internal_proto(stream_at_bof);
584 extern int stream_at_eof (stream *);
585 internal_proto(stream_at_eof);
587 extern int delete_file (gfc_unit *);
588 internal_proto(delete_file);
590 extern int file_exists (const char *file, gfc_charlen_type file_len);
591 internal_proto(file_exists);
593 extern const char *inquire_sequential (const char *, int);
594 internal_proto(inquire_sequential);
596 extern const char *inquire_direct (const char *, int);
597 internal_proto(inquire_direct);
599 extern const char *inquire_formatted (const char *, int);
600 internal_proto(inquire_formatted);
602 extern const char *inquire_unformatted (const char *, int);
603 internal_proto(inquire_unformatted);
605 extern const char *inquire_read (const char *, int);
606 internal_proto(inquire_read);
608 extern const char *inquire_write (const char *, int);
609 internal_proto(inquire_write);
611 extern const char *inquire_readwrite (const char *, int);
612 internal_proto(inquire_readwrite);
614 extern gfc_offset file_length (stream *);
615 internal_proto(file_length);
617 extern gfc_offset file_position (stream *);
618 internal_proto(file_position);
620 extern int is_seekable (stream *);
621 internal_proto(is_seekable);
623 extern int is_preconnected (stream *);
624 internal_proto(is_preconnected);
626 extern void flush_if_preconnected (stream *);
627 internal_proto(flush_if_preconnected);
629 extern void empty_internal_buffer(stream *);
630 internal_proto(empty_internal_buffer);
632 extern try flush (stream *);
633 internal_proto(flush);
635 extern int stream_isatty (stream *);
636 internal_proto(stream_isatty);
638 extern char * stream_ttyname (stream *);
639 internal_proto(stream_ttyname);
641 extern gfc_offset stream_offset (stream *s);
642 internal_proto(stream_offset);
644 extern int unit_to_fd (int);
645 internal_proto(unit_to_fd);
647 extern int unpack_filename (char *, const char *, int);
648 internal_proto(unpack_filename);
652 /* Maximum file offset, computed at library initialization time. */
653 extern gfc_offset max_offset;
654 internal_proto(max_offset);
656 /* Unit tree root. */
657 extern gfc_unit *unit_root;
658 internal_proto(unit_root);
660 extern __gthread_mutex_t unit_lock;
661 internal_proto(unit_lock);
663 extern int close_unit (gfc_unit *);
664 internal_proto(close_unit);
666 extern int is_internal_unit (st_parameter_dt *);
667 internal_proto(is_internal_unit);
669 extern int is_array_io (st_parameter_dt *);
670 internal_proto(is_array_io);
672 extern gfc_unit *find_unit (int);
673 internal_proto(find_unit);
675 extern gfc_unit *find_or_create_unit (int);
676 internal_proto(find_unit);
678 extern gfc_unit *get_unit (st_parameter_dt *, int);
679 internal_proto(get_unit);
681 extern void unlock_unit (gfc_unit *);
682 internal_proto(unlock_unit);
686 extern void test_endfile (gfc_unit *);
687 internal_proto(test_endfile);
689 extern gfc_unit *new_unit (st_parameter_open *, gfc_unit *, unit_flags *);
690 internal_proto(new_unit);
694 extern void parse_format (st_parameter_dt *);
695 internal_proto(parse_format);
697 extern const fnode *next_format (st_parameter_dt *);
698 internal_proto(next_format);
700 extern void unget_format (st_parameter_dt *, const fnode *);
701 internal_proto(unget_format);
703 extern void format_error (st_parameter_dt *, const fnode *, const char *);
704 internal_proto(format_error);
706 extern void free_format_data (st_parameter_dt *);
707 internal_proto(free_format_data);
711 #define SCRATCH_SIZE 300
713 extern const char *type_name (bt);
714 internal_proto(type_name);
716 extern void *read_block (st_parameter_dt *, int *);
717 internal_proto(read_block);
719 extern void *write_block (st_parameter_dt *, int);
720 internal_proto(write_block);
722 extern gfc_offset next_array_record (st_parameter_dt *, array_loop_spec *);
723 internal_proto(next_array_record);
725 extern gfc_offset init_loop_spec (gfc_array_char *, array_loop_spec *);
726 internal_proto(init_loop_spec);
728 extern void next_record (st_parameter_dt *, int);
729 internal_proto(next_record);
733 extern void set_integer (void *, GFC_INTEGER_LARGEST, int);
734 internal_proto(set_integer);
736 extern GFC_UINTEGER_LARGEST max_value (int, int);
737 internal_proto(max_value);
739 extern int convert_real (st_parameter_dt *, void *, const char *, int);
740 internal_proto(convert_real);
742 extern void read_a (st_parameter_dt *, const fnode *, char *, int);
743 internal_proto(read_a);
745 extern void read_f (st_parameter_dt *, const fnode *, char *, int);
746 internal_proto(read_f);
748 extern void read_l (st_parameter_dt *, const fnode *, char *, int);
749 internal_proto(read_l);
751 extern void read_x (st_parameter_dt *, int);
752 internal_proto(read_x);
754 extern void read_radix (st_parameter_dt *, const fnode *, char *, int, int);
755 internal_proto(read_radix);
757 extern void read_decimal (st_parameter_dt *, const fnode *, char *, int);
758 internal_proto(read_decimal);
762 extern void list_formatted_read (st_parameter_dt *, bt, void *, int, size_t,
764 internal_proto(list_formatted_read);
766 extern void finish_list_read (st_parameter_dt *);
767 internal_proto(finish_list_read);
769 extern void namelist_read (st_parameter_dt *);
770 internal_proto(namelist_read);
772 extern void namelist_write (st_parameter_dt *);
773 internal_proto(namelist_write);
777 extern void write_a (st_parameter_dt *, const fnode *, const char *, int);
778 internal_proto(write_a);
780 extern void write_b (st_parameter_dt *, const fnode *, const char *, int);
781 internal_proto(write_b);
783 extern void write_d (st_parameter_dt *, const fnode *, const char *, int);
784 internal_proto(write_d);
786 extern void write_e (st_parameter_dt *, const fnode *, const char *, int);
787 internal_proto(write_e);
789 extern void write_en (st_parameter_dt *, const fnode *, const char *, int);
790 internal_proto(write_en);
792 extern void write_es (st_parameter_dt *, const fnode *, const char *, int);
793 internal_proto(write_es);
795 extern void write_f (st_parameter_dt *, const fnode *, const char *, int);
796 internal_proto(write_f);
798 extern void write_i (st_parameter_dt *, const fnode *, const char *, int);
799 internal_proto(write_i);
801 extern void write_l (st_parameter_dt *, const fnode *, char *, int);
802 internal_proto(write_l);
804 extern void write_o (st_parameter_dt *, const fnode *, const char *, int);
805 internal_proto(write_o);
807 extern void write_x (st_parameter_dt *, int, int);
808 internal_proto(write_x);
810 extern void write_z (st_parameter_dt *, const fnode *, const char *, int);
811 internal_proto(write_z);
813 extern void list_formatted_write (st_parameter_dt *, bt, void *, int, size_t,
815 internal_proto(list_formatted_write);
818 extern try notify_std (int, const char *);
819 internal_proto(notify_std);
821 /* size_from_kind.c */
822 extern size_t size_from_real_kind (int);
823 internal_proto(size_from_real_kind);
825 extern size_t size_from_complex_kind (int);
826 internal_proto(size_from_complex_kind);
829 extern void free_ionml (st_parameter_dt *);
830 internal_proto(free_ionml);
833 inc_waiting_locked (gfc_unit *u)
835 #ifdef HAVE_SYNC_FETCH_AND_ADD
836 (void) __sync_fetch_and_add (&u->waiting, 1);
843 predec_waiting_locked (gfc_unit *u)
845 #ifdef HAVE_SYNC_FETCH_AND_ADD
846 return __sync_add_and_fetch (&u->waiting, -1);
853 dec_waiting_unlocked (gfc_unit *u)
855 #ifdef HAVE_SYNC_FETCH_AND_ADD
856 (void) __sync_fetch_and_add (&u->waiting, -1);
858 __gthread_mutex_lock (&unit_lock);
860 __gthread_mutex_unlock (&unit_lock);