1 /* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007
2 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, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 /* As a special exception, if you link this library with other files,
23 some of which are compiled with GCC, to produce an executable,
24 this library does not by itself cause the resulting executable
25 to be covered by the GNU General Public License.
26 This exception does not however invalidate any other reasons why
27 the executable file might be covered by the GNU General Public License. */
32 /* IO library include. */
34 #include "libgfortran.h"
39 /* Basic types used in data transfers. */
42 { BT_NULL
, BT_INTEGER
, BT_LOGICAL
, BT_CHARACTER
, BT_REAL
,
48 struct st_parameter_dt
;
52 char *(*alloc_w_at
) (struct stream
*, int *, gfc_offset
);
53 char *(*alloc_r_at
) (struct stream
*, int *, gfc_offset
);
54 try (*sfree
) (struct stream
*);
55 try (*close
) (struct stream
*);
56 try (*seek
) (struct stream
*, gfc_offset
);
57 try (*trunc
) (struct stream
*);
58 int (*read
) (struct stream
*, void *, size_t *);
59 int (*write
) (struct stream
*, const void *, size_t *);
60 try (*set
) (struct stream
*, int, size_t);
65 /* Macros for doing file I/O given a stream. */
67 #define sfree(s) ((s)->sfree)(s)
68 #define sclose(s) ((s)->close)(s)
70 #define salloc_r(s, len) ((s)->alloc_r_at)(s, len, -1)
71 #define salloc_w(s, len) ((s)->alloc_w_at)(s, len, -1)
73 #define salloc_r_at(s, len, where) ((s)->alloc_r_at)(s, len, where)
74 #define salloc_w_at(s, len, where) ((s)->alloc_w_at)(s, len, where)
76 #define sseek(s, pos) ((s)->seek)(s, pos)
77 #define struncate(s) ((s)->trunc)(s)
78 #define sread(s, buf, nbytes) ((s)->read)(s, buf, nbytes)
79 #define swrite(s, buf, nbytes) ((s)->write)(s, buf, nbytes)
81 #define sset(s, c, n) ((s)->set)(s, c, n)
83 /* Macros for testing what kinds of I/O we are doing. */
85 #define is_array_io(dtp) ((dtp)->internal_unit_desc)
87 #define is_internal_unit(dtp) ((dtp)->u.p.unit_is_internal)
89 #define is_stream_io(dtp) ((dtp)->u.p.current_unit->flags.access == ACCESS_STREAM)
91 /* The array_loop_spec contains the variables for the loops over index ranges
92 that are encountered. Since the variables can be negative, ssize_t
95 typedef struct array_loop_spec
97 /* Index counter for this dimension. */
100 /* Start for the index counter. */
103 /* End for the index counter. */
106 /* Step for the index counter. */
111 /* Representation of a namelist object in libgfortran
114 &GROUPNAME OBJECT=value[s] [,OBJECT=value[s]].../
116 &GROUPNAME OBJECT=value[s] [,OBJECT=value[s]]...&END
118 The object can be a fully qualified, compound name for an intrinsic
119 type, derived types or derived type components. So, a substring
120 a(:)%b(4)%ch(2:4)(1:7) has to be treated correctly in namelist
121 read. Hence full information about the structure of the object has
122 to be available to list_read.c and write.
124 These requirements are met by the following data structures.
126 namelist_info type contains all the scalar information about the
127 object and arrays of descriptor_dimension and array_loop_spec types for
130 typedef struct namelist_type
133 /* Object type, stored as GFC_DTYPE_xxxx. */
139 /* Address for the start of the object's data. */
142 /* Flag to show that a read is to be attempted for this node. */
145 /* Length of intrinsic type in bytes. */
148 /* Rank of the object. */
151 /* Overall size of the object in bytes. */
154 /* Length of character string. */
155 index_type string_length
;
157 descriptor_dimension
* dim
;
158 array_loop_spec
* ls
;
159 struct namelist_type
* next
;
163 /* Options for the OPEN statement. */
166 { ACCESS_SEQUENTIAL
, ACCESS_DIRECT
, ACCESS_APPEND
, ACCESS_STREAM
,
172 { ACTION_READ
, ACTION_WRITE
, ACTION_READWRITE
,
178 { BLANK_NULL
, BLANK_ZERO
, BLANK_UNSPECIFIED
}
182 { DELIM_NONE
, DELIM_APOSTROPHE
, DELIM_QUOTE
,
188 { FORM_FORMATTED
, FORM_UNFORMATTED
, FORM_UNSPECIFIED
}
192 { POSITION_ASIS
, POSITION_REWIND
, POSITION_APPEND
,
198 { STATUS_UNKNOWN
, STATUS_OLD
, STATUS_NEW
, STATUS_SCRATCH
,
199 STATUS_REPLACE
, STATUS_UNSPECIFIED
204 { PAD_YES
, PAD_NO
, PAD_UNSPECIFIED
}
208 { ADVANCE_YES
, ADVANCE_NO
, ADVANCE_UNSPECIFIED
}
215 #define CHARACTER1(name) \
217 gfc_charlen_type name ## _len
218 #define CHARACTER2(name) \
219 gfc_charlen_type name ## _len; \
224 st_parameter_common common
;
225 GFC_INTEGER_4 recl_in
;
231 CHARACTER1 (position
);
235 CHARACTER1 (convert
);
239 #define IOPARM_CLOSE_HAS_STATUS (1 << 7)
243 st_parameter_common common
;
250 st_parameter_common common
;
252 st_parameter_filepos
;
254 #define IOPARM_INQUIRE_HAS_EXIST (1 << 7)
255 #define IOPARM_INQUIRE_HAS_OPENED (1 << 8)
256 #define IOPARM_INQUIRE_HAS_NUMBER (1 << 9)
257 #define IOPARM_INQUIRE_HAS_NAMED (1 << 10)
258 #define IOPARM_INQUIRE_HAS_NEXTREC (1 << 11)
259 #define IOPARM_INQUIRE_HAS_RECL_OUT (1 << 12)
260 #define IOPARM_INQUIRE_HAS_STRM_POS_OUT (1 << 13)
261 #define IOPARM_INQUIRE_HAS_FILE (1 << 14)
262 #define IOPARM_INQUIRE_HAS_ACCESS (1 << 15)
263 #define IOPARM_INQUIRE_HAS_FORM (1 << 16)
264 #define IOPARM_INQUIRE_HAS_BLANK (1 << 17)
265 #define IOPARM_INQUIRE_HAS_POSITION (1 << 18)
266 #define IOPARM_INQUIRE_HAS_ACTION (1 << 19)
267 #define IOPARM_INQUIRE_HAS_DELIM (1 << 20)
268 #define IOPARM_INQUIRE_HAS_PAD (1 << 21)
269 #define IOPARM_INQUIRE_HAS_NAME (1 << 22)
270 #define IOPARM_INQUIRE_HAS_SEQUENTIAL (1 << 23)
271 #define IOPARM_INQUIRE_HAS_DIRECT (1 << 24)
272 #define IOPARM_INQUIRE_HAS_FORMATTED (1 << 25)
273 #define IOPARM_INQUIRE_HAS_UNFORMATTED (1 << 26)
274 #define IOPARM_INQUIRE_HAS_READ (1 << 27)
275 #define IOPARM_INQUIRE_HAS_WRITE (1 << 28)
276 #define IOPARM_INQUIRE_HAS_READWRITE (1 << 29)
277 #define IOPARM_INQUIRE_HAS_CONVERT (1 << 30)
281 st_parameter_common common
;
282 GFC_INTEGER_4
*exist
, *opened
, *number
, *named
;
283 GFC_INTEGER_4
*nextrec
, *recl_out
;
284 GFC_IO_INT
*strm_pos_out
;
289 CHARACTER1 (position
);
294 CHARACTER2 (sequential
);
296 CHARACTER2 (formatted
);
297 CHARACTER1 (unformatted
);
300 CHARACTER2 (readwrite
);
301 CHARACTER1 (convert
);
303 st_parameter_inquire
;
308 #define IOPARM_DT_LIST_FORMAT (1 << 7)
309 #define IOPARM_DT_NAMELIST_READ_MODE (1 << 8)
310 #define IOPARM_DT_HAS_REC (1 << 9)
311 #define IOPARM_DT_HAS_SIZE (1 << 10)
312 #define IOPARM_DT_HAS_IOLENGTH (1 << 11)
313 #define IOPARM_DT_HAS_FORMAT (1 << 12)
314 #define IOPARM_DT_HAS_ADVANCE (1 << 13)
315 #define IOPARM_DT_HAS_INTERNAL_UNIT (1 << 14)
316 #define IOPARM_DT_HAS_NAMELIST_NAME (1 << 15)
317 /* Internal use bit. */
318 #define IOPARM_DT_IONML_SET (1 << 31)
320 typedef struct st_parameter_dt
322 st_parameter_common common
;
324 GFC_IO_INT
*size
, *iolength
;
325 gfc_array_char
*internal_unit_desc
;
327 CHARACTER2 (advance
);
328 CHARACTER1 (internal_unit
);
329 CHARACTER2 (namelist_name
);
330 /* Private part of the structure. The compiler just needs
331 to reserve enough space. */
336 void (*transfer
) (struct st_parameter_dt
*, bt
, void *, int,
338 struct gfc_unit
*current_unit
;
339 /* Item number in a formatted data transfer. Also used in namelist
340 read_logical as an index into line_buffer. */
343 unit_blank blank_status
;
344 enum {SIGN_S
, SIGN_SS
, SIGN_SP
} sign_status
;
346 int max_pos
; /* Maximum righthand column written to. */
347 /* Number of skips + spaces to be done for T and X-editing. */
349 /* Number of spaces to be done for T and X-editing. */
351 /* Whether an EOR condition was encountered. Value is:
352 0 if no EOR was encountered
353 1 if an EOR was encountered due to a 1-byte marker (LF)
354 2 if an EOR was encountered due to a 2-bytes marker (CRLF) */
356 unit_advance advance_status
;
358 unsigned reversion_flag
: 1; /* Format reversion has occurred. */
359 unsigned first_item
: 1;
360 unsigned seen_dollar
: 1;
361 unsigned eor_condition
: 1;
362 unsigned no_leading_blank
: 1;
363 unsigned char_flag
: 1;
364 unsigned input_complete
: 1;
366 unsigned comma_flag
: 1;
367 /* A namelist specific flag used in the list directed library
368 to flag that calls are being made from namelist read (eg. to
369 ignore comments or to treat '/' as a terminator) */
370 unsigned namelist_mode
: 1;
371 /* A namelist specific flag used in the list directed library
372 to flag read errors and return, so that an attempt can be
373 made to read a new object name. */
374 unsigned nml_read_error
: 1;
375 /* A sequential formatted read specific flag used to signal that a
376 character string is being read so don't use commas to shorten a
377 formatted field width. */
378 unsigned sf_read_comma
: 1;
379 /* A namelist specific flag used to enable reading input from
380 line_buffer for logical reads. */
381 unsigned line_buffer_enabled
: 1;
382 /* An internal unit specific flag used to identify that the associated
384 unsigned unit_is_internal
: 1;
385 /* An internal unit specific flag to signify an EOF condition for list
388 /* 16 unused bits. */
400 struct format_data
*fmt
;
402 namelist_info
*ionml
;
403 /* A flag used to identify when a non-standard expanded namelist read
406 /* Storage area for values except for strings. Must be large
407 enough to hold a complex value (two reals) of the largest
410 gfc_offset size_used
;
412 /* This pad size must be equal to the pad_size declared in
413 trans-io.c (gfc_build_io_library_fndecls). The above structure
414 must be smaller or equal to this array. */
415 char pad
[16 * sizeof (char *) + 32 * sizeof (int)];
420 /* Ensure st_parameter_dt's u.pad is bigger or equal to u.p. */
421 extern char check_st_parameter_dt
[sizeof (((st_parameter_dt
*) 0)->u
.pad
)
422 >= sizeof (((st_parameter_dt
*) 0)->u
.p
)
436 unit_position position
;
439 unit_convert convert
;
445 typedef struct gfc_unit
451 struct gfc_unit
*left
, *right
;
454 int read_bad
, current_record
, saved_pos
, previous_nonadvancing_write
;
457 { NO_ENDFILE
, AT_ENDFILE
, AFTER_ENDFILE
}
463 /* recl -- Record length of the file.
464 last_record -- Last record number read or written
465 maxrec -- Maximum record number in a direct access file
466 bytes_left -- Bytes left in current record.
467 strm_pos -- Current position in file for STREAM I/O.
468 recl_subrecord -- Maximum length for subrecord.
469 bytes_left_subrecord -- Bytes left in current subrecord. */
470 gfc_offset recl
, last_record
, maxrec
, bytes_left
, strm_pos
,
471 recl_subrecord
, bytes_left_subrecord
;
473 /* Set to 1 if we have read a subrecord. */
477 __gthread_mutex_t lock
;
478 /* Number of threads waiting to acquire this unit's lock.
479 When non-zero, close_unit doesn't only removes the unit
480 from the UNIT_ROOT tree, but doesn't free it and the
481 last of the waiting threads will do that.
482 This must be either atomically increased/decreased, or
483 always guarded by UNIT_LOCK. */
485 /* Flag set by close_unit if the unit as been closed.
486 Must be manipulated under unit's lock. */
489 /* For traversing arrays */
498 /* Format tokens. Only about half of these can be stored in the
503 FMT_NONE
= 0, FMT_UNKNOWN
, FMT_SIGNED_INT
, FMT_ZERO
, FMT_POSINT
, FMT_PERIOD
,
504 FMT_COMMA
, FMT_COLON
, FMT_SLASH
, FMT_DOLLAR
, FMT_T
, FMT_TR
, FMT_TL
,
505 FMT_LPAREN
, FMT_RPAREN
, FMT_X
, FMT_S
, FMT_SS
, FMT_SP
, FMT_STRING
,
506 FMT_BADSTRING
, FMT_P
, FMT_I
, FMT_B
, FMT_BN
, FMT_BZ
, FMT_O
, FMT_Z
, FMT_F
,
507 FMT_E
, FMT_EN
, FMT_ES
, FMT_G
, FMT_L
, FMT_A
, FMT_D
, FMT_H
, FMT_END
512 /* Format nodes. A format string is converted into a tree of these
513 structures, which is traversed as part of a data transfer statement. */
552 /* Members for traversing the tree during data transfer. */
555 struct fnode
*current
;
563 extern int move_pos_offset (stream
*, int);
564 internal_proto(move_pos_offset
);
566 extern int compare_files (stream
*, stream
*);
567 internal_proto(compare_files
);
569 extern stream
*open_external (st_parameter_open
*, unit_flags
*);
570 internal_proto(open_external
);
572 extern stream
*open_internal (char *, int, gfc_offset
);
573 internal_proto(open_internal
);
575 extern stream
*input_stream (void);
576 internal_proto(input_stream
);
578 extern stream
*output_stream (void);
579 internal_proto(output_stream
);
581 extern stream
*error_stream (void);
582 internal_proto(error_stream
);
584 extern int compare_file_filename (gfc_unit
*, const char *, int);
585 internal_proto(compare_file_filename
);
587 extern gfc_unit
*find_file (const char *file
, gfc_charlen_type file_len
);
588 internal_proto(find_file
);
590 extern int stream_at_bof (stream
*);
591 internal_proto(stream_at_bof
);
593 extern int stream_at_eof (stream
*);
594 internal_proto(stream_at_eof
);
596 extern int delete_file (gfc_unit
*);
597 internal_proto(delete_file
);
599 extern int file_exists (const char *file
, gfc_charlen_type file_len
);
600 internal_proto(file_exists
);
602 extern const char *inquire_sequential (const char *, int);
603 internal_proto(inquire_sequential
);
605 extern const char *inquire_direct (const char *, int);
606 internal_proto(inquire_direct
);
608 extern const char *inquire_formatted (const char *, int);
609 internal_proto(inquire_formatted
);
611 extern const char *inquire_unformatted (const char *, int);
612 internal_proto(inquire_unformatted
);
614 extern const char *inquire_read (const char *, int);
615 internal_proto(inquire_read
);
617 extern const char *inquire_write (const char *, int);
618 internal_proto(inquire_write
);
620 extern const char *inquire_readwrite (const char *, int);
621 internal_proto(inquire_readwrite
);
623 extern gfc_offset
file_length (stream
*);
624 internal_proto(file_length
);
626 extern gfc_offset
file_position (stream
*);
627 internal_proto(file_position
);
629 extern int is_seekable (stream
*);
630 internal_proto(is_seekable
);
632 extern int is_special (stream
*);
633 internal_proto(is_special
);
635 extern int is_preconnected (stream
*);
636 internal_proto(is_preconnected
);
638 extern void flush_if_preconnected (stream
*);
639 internal_proto(flush_if_preconnected
);
641 extern void empty_internal_buffer(stream
*);
642 internal_proto(empty_internal_buffer
);
644 extern try flush (stream
*);
645 internal_proto(flush
);
647 extern int stream_isatty (stream
*);
648 internal_proto(stream_isatty
);
650 extern char * stream_ttyname (stream
*);
651 internal_proto(stream_ttyname
);
653 extern gfc_offset
stream_offset (stream
*s
);
654 internal_proto(stream_offset
);
656 extern int unpack_filename (char *, const char *, int);
657 internal_proto(unpack_filename
);
661 /* Maximum file offset, computed at library initialization time. */
662 extern gfc_offset max_offset
;
663 internal_proto(max_offset
);
665 /* Unit tree root. */
666 extern gfc_unit
*unit_root
;
667 internal_proto(unit_root
);
669 extern __gthread_mutex_t unit_lock
;
670 internal_proto(unit_lock
);
672 extern int close_unit (gfc_unit
*);
673 internal_proto(close_unit
);
675 extern gfc_unit
*get_internal_unit (st_parameter_dt
*);
676 internal_proto(get_internal_unit
);
678 extern void free_internal_unit (st_parameter_dt
*);
679 internal_proto(free_internal_unit
);
681 extern gfc_unit
*find_unit (int);
682 internal_proto(find_unit
);
684 extern gfc_unit
*find_or_create_unit (int);
685 internal_proto(find_or_create_unit
);
687 extern gfc_unit
*get_unit (st_parameter_dt
*, int);
688 internal_proto(get_unit
);
690 extern void unlock_unit (gfc_unit
*);
691 internal_proto(unlock_unit
);
693 extern void update_position (gfc_unit
*);
694 internal_proto(update_position
);
696 extern void finish_last_advance_record (gfc_unit
*u
);
697 internal_proto (finish_last_advance_record
);
701 extern gfc_unit
*new_unit (st_parameter_open
*, gfc_unit
*, unit_flags
*);
702 internal_proto(new_unit
);
706 extern void parse_format (st_parameter_dt
*);
707 internal_proto(parse_format
);
709 extern const fnode
*next_format (st_parameter_dt
*);
710 internal_proto(next_format
);
712 extern void unget_format (st_parameter_dt
*, const fnode
*);
713 internal_proto(unget_format
);
715 extern void format_error (st_parameter_dt
*, const fnode
*, const char *);
716 internal_proto(format_error
);
718 extern void free_format_data (st_parameter_dt
*);
719 internal_proto(free_format_data
);
723 #define SCRATCH_SIZE 300
725 extern const char *type_name (bt
);
726 internal_proto(type_name
);
728 extern void *read_block (st_parameter_dt
*, int *);
729 internal_proto(read_block
);
731 extern char *read_sf (st_parameter_dt
*, int *, int);
732 internal_proto(read_sf
);
734 extern void *write_block (st_parameter_dt
*, int);
735 internal_proto(write_block
);
737 extern gfc_offset
next_array_record (st_parameter_dt
*, array_loop_spec
*,
739 internal_proto(next_array_record
);
741 extern gfc_offset
init_loop_spec (gfc_array_char
*, array_loop_spec
*,
743 internal_proto(init_loop_spec
);
745 extern void next_record (st_parameter_dt
*, int);
746 internal_proto(next_record
);
748 extern void reverse_memcpy (void *, const void *, size_t);
749 internal_proto (reverse_memcpy
);
753 extern void set_integer (void *, GFC_INTEGER_LARGEST
, int);
754 internal_proto(set_integer
);
756 extern GFC_UINTEGER_LARGEST
max_value (int, int);
757 internal_proto(max_value
);
759 extern int convert_real (st_parameter_dt
*, void *, const char *, int);
760 internal_proto(convert_real
);
762 extern void read_a (st_parameter_dt
*, const fnode
*, char *, int);
763 internal_proto(read_a
);
765 extern void read_f (st_parameter_dt
*, const fnode
*, char *, int);
766 internal_proto(read_f
);
768 extern void read_l (st_parameter_dt
*, const fnode
*, char *, int);
769 internal_proto(read_l
);
771 extern void read_x (st_parameter_dt
*, int);
772 internal_proto(read_x
);
774 extern void read_radix (st_parameter_dt
*, const fnode
*, char *, int, int);
775 internal_proto(read_radix
);
777 extern void read_decimal (st_parameter_dt
*, const fnode
*, char *, int);
778 internal_proto(read_decimal
);
782 extern void list_formatted_read (st_parameter_dt
*, bt
, void *, int, size_t,
784 internal_proto(list_formatted_read
);
786 extern void finish_list_read (st_parameter_dt
*);
787 internal_proto(finish_list_read
);
789 extern void namelist_read (st_parameter_dt
*);
790 internal_proto(namelist_read
);
792 extern void namelist_write (st_parameter_dt
*);
793 internal_proto(namelist_write
);
797 extern void write_a (st_parameter_dt
*, const fnode
*, const char *, int);
798 internal_proto(write_a
);
800 extern void write_b (st_parameter_dt
*, const fnode
*, const char *, int);
801 internal_proto(write_b
);
803 extern void write_d (st_parameter_dt
*, const fnode
*, const char *, int);
804 internal_proto(write_d
);
806 extern void write_e (st_parameter_dt
*, const fnode
*, const char *, int);
807 internal_proto(write_e
);
809 extern void write_en (st_parameter_dt
*, const fnode
*, const char *, int);
810 internal_proto(write_en
);
812 extern void write_es (st_parameter_dt
*, const fnode
*, const char *, int);
813 internal_proto(write_es
);
815 extern void write_f (st_parameter_dt
*, const fnode
*, const char *, int);
816 internal_proto(write_f
);
818 extern void write_i (st_parameter_dt
*, const fnode
*, const char *, int);
819 internal_proto(write_i
);
821 extern void write_l (st_parameter_dt
*, const fnode
*, char *, int);
822 internal_proto(write_l
);
824 extern void write_o (st_parameter_dt
*, const fnode
*, const char *, int);
825 internal_proto(write_o
);
827 extern void write_x (st_parameter_dt
*, int, int);
828 internal_proto(write_x
);
830 extern void write_z (st_parameter_dt
*, const fnode
*, const char *, int);
831 internal_proto(write_z
);
833 extern void list_formatted_write (st_parameter_dt
*, bt
, void *, int, size_t,
835 internal_proto(list_formatted_write
);
837 /* size_from_kind.c */
838 extern size_t size_from_real_kind (int);
839 internal_proto(size_from_real_kind
);
841 extern size_t size_from_complex_kind (int);
842 internal_proto(size_from_complex_kind
);
845 extern void free_ionml (st_parameter_dt
*);
846 internal_proto(free_ionml
);
849 inc_waiting_locked (gfc_unit
*u
)
851 #ifdef HAVE_SYNC_FETCH_AND_ADD
852 (void) __sync_fetch_and_add (&u
->waiting
, 1);
859 predec_waiting_locked (gfc_unit
*u
)
861 #ifdef HAVE_SYNC_FETCH_AND_ADD
862 return __sync_add_and_fetch (&u
->waiting
, -1);
869 dec_waiting_unlocked (gfc_unit
*u
)
871 #ifdef HAVE_SYNC_FETCH_AND_ADD
872 (void) __sync_fetch_and_add (&u
->waiting
, -1);
874 __gthread_mutex_lock (&unit_lock
);
876 __gthread_mutex_unlock (&unit_lock
);