Home | History | Annotate | Line # | Download | only in io
io.h revision 1.1.1.1
      1 /* Copyright (C) 2002-2019 Free Software Foundation, Inc.
      2    Contributed by Andy Vaught
      3    F2003 I/O support contributed by Jerry DeLisle
      4 
      5 This file is part of the GNU Fortran runtime library (libgfortran).
      6 
      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 3, or (at your option)
     10 any later version.
     11 
     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.
     16 
     17 Under Section 7 of GPL version 3, you are granted additional
     18 permissions described in the GCC Runtime Library Exception, version
     19 3.1, as published by the Free Software Foundation.
     20 
     21 You should have received a copy of the GNU General Public License and
     22 a copy of the GCC Runtime Library Exception along with this program;
     23 see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     24 <http://www.gnu.org/licenses/>.  */
     25 
     26 #ifndef GFOR_IO_H
     27 #define GFOR_IO_H
     28 
     29 /* IO library include.  */
     30 
     31 #include "libgfortran.h"
     32 
     33 #include <gthr.h>
     34 
     35 
     36 /* POSIX 2008 specifies that the extended locale stuff is found in
     37    locale.h, but some systems have them in xlocale.h.  */
     38 
     39 #include <locale.h>
     40 
     41 #ifdef HAVE_XLOCALE_H
     42 #include <xlocale.h>
     43 #endif
     44 
     45 
     46 /* Forward declarations.  */
     47 struct st_parameter_dt;
     48 typedef struct stream stream;
     49 struct fbuf;
     50 struct format_data;
     51 typedef struct fnode fnode;
     52 struct gfc_unit;
     53 
     54 #ifdef HAVE_NEWLOCALE
     55 /* We have POSIX 2008 extended locale stuff.  */
     56 extern locale_t c_locale;
     57 internal_proto(c_locale);
     58 #else
     59 extern char* old_locale;
     60 internal_proto(old_locale);
     61 extern int old_locale_ctr;
     62 internal_proto(old_locale_ctr);
     63 extern __gthread_mutex_t old_locale_lock;
     64 internal_proto(old_locale_lock);
     65 #endif
     66 
     67 
     68 /* Macros for testing what kinds of I/O we are doing.  */
     69 
     70 #define is_array_io(dtp) ((dtp)->internal_unit_desc)
     71 
     72 #define is_internal_unit(dtp) ((dtp)->u.p.unit_is_internal)
     73 
     74 #define is_stream_io(dtp) ((dtp)->u.p.current_unit->flags.access == ACCESS_STREAM)
     75 
     76 #define is_char4_unit(dtp) ((dtp)->u.p.current_unit->internal_unit_kind == 4)
     77 
     78 /* The array_loop_spec contains the variables for the loops over index ranges
     79    that are encountered.  */
     80 
     81 typedef struct array_loop_spec
     82 {
     83   /* Index counter for this dimension.  */
     84   index_type idx;
     85 
     86   /* Start for the index counter.  */
     87   index_type start;
     88 
     89   /* End for the index counter.  */
     90   index_type end;
     91 
     92   /* Step for the index counter.  */
     93   index_type step;
     94 }
     95 array_loop_spec;
     96 
     97 /* User defined input/output iomsg length. */
     98 
     99 #define IOMSG_LEN 256
    100 
    101 /* Subroutine formatted_dtio (struct, unit, iotype, v_list, iostat,
    102 			      iomsg, (_iotype), (_iomsg))  */
    103 typedef void (*formatted_dtio)(void *, GFC_INTEGER_4 *, char *,
    104 			       gfc_full_array_i4 *,
    105 			       GFC_INTEGER_4 *, char *,
    106 			       gfc_charlen_type, gfc_charlen_type);
    107 
    108 /* Subroutine unformatted_dtio (struct, unit, iostat, iomsg, (_iomsg))  */
    109 typedef void (*unformatted_dtio)(void *, GFC_INTEGER_4 *, GFC_INTEGER_4 *,
    110 				 char *, gfc_charlen_type);
    111 
    112 /* The dtio calls for namelist require a CLASS object to be built.  */
    113 typedef struct gfc_class
    114 {
    115   void *data;
    116   void *vptr;
    117   index_type len;
    118 }
    119 gfc_class;
    120 
    121 
    122 /* A structure to build a hash table for format data.  */
    123 
    124 #define FORMAT_HASH_SIZE 16
    125 
    126 typedef struct format_hash_entry
    127 {
    128   char *key;
    129   gfc_charlen_type key_len;
    130   struct format_data *hashed_fmt;
    131 }
    132 format_hash_entry;
    133 
    134 /* Representation of a namelist object in libgfortran
    135 
    136    Namelist Records
    137       &GROUPNAME  OBJECT=value[s] [,OBJECT=value[s]].../
    138      or
    139       &GROUPNAME  OBJECT=value[s] [,OBJECT=value[s]]...&END
    140 
    141    The object can be a fully qualified, compound name for an intrinsic
    142    type, derived types or derived type components.  So, a substring
    143    a(:)%b(4)%ch(2:4)(1:7) has to be treated correctly in namelist
    144    read. Hence full information about the structure of the object has
    145    to be available to list_read.c and write.
    146 
    147    These requirements are met by the following data structures.
    148 
    149    namelist_info type contains all the scalar information about the
    150    object and arrays of descriptor_dimension and array_loop_spec types for
    151    arrays.  */
    152 
    153 typedef struct namelist_type
    154 {
    155   /* Object type.  */
    156   bt type;
    157 
    158   /* Object name.  */
    159   char * var_name;
    160 
    161   /* Address for the start of the object's data.  */
    162   void * mem_pos;
    163 
    164   /* Address of specific DTIO subroutine.  */
    165   void * dtio_sub;
    166 
    167   /* Address of vtable if dtio_sub non-null.  */
    168   void * vtable;
    169 
    170   /* Flag to show that a read is to be attempted for this node.  */
    171   int touched;
    172 
    173   /* Length of intrinsic type in bytes.  */
    174   int len;
    175 
    176   /* Rank of the object.  */
    177   int var_rank;
    178 
    179   /* Overall size of the object in bytes.  */
    180   index_type size;
    181 
    182   /* Length of character string.  */
    183   index_type string_length;
    184 
    185   descriptor_dimension * dim;
    186   array_loop_spec * ls;
    187   struct namelist_type * next;
    188 }
    189 namelist_info;
    190 
    191 /* Options for the OPEN statement.  */
    192 
    193 typedef enum
    194 { ACCESS_SEQUENTIAL, ACCESS_DIRECT, ACCESS_APPEND, ACCESS_STREAM,
    195   ACCESS_UNSPECIFIED
    196 }
    197 unit_access;
    198 
    199 typedef enum
    200 { ACTION_READ, ACTION_WRITE, ACTION_READWRITE,
    201   ACTION_UNSPECIFIED
    202 }
    203 unit_action;
    204 
    205 typedef enum
    206 { BLANK_NULL, BLANK_ZERO, BLANK_UNSPECIFIED }
    207 unit_blank;
    208 
    209 typedef enum
    210 { DELIM_NONE, DELIM_APOSTROPHE, DELIM_QUOTE,
    211   DELIM_UNSPECIFIED
    212 }
    213 unit_delim;
    214 
    215 typedef enum
    216 { FORM_FORMATTED, FORM_UNFORMATTED, FORM_UNSPECIFIED }
    217 unit_form;
    218 
    219 typedef enum
    220 { POSITION_ASIS, POSITION_REWIND, POSITION_APPEND,
    221   POSITION_UNSPECIFIED
    222 }
    223 unit_position;
    224 
    225 typedef enum
    226 { STATUS_UNKNOWN, STATUS_OLD, STATUS_NEW, STATUS_SCRATCH,
    227   STATUS_REPLACE, STATUS_UNSPECIFIED
    228 }
    229 unit_status;
    230 
    231 typedef enum
    232 { PAD_YES, PAD_NO, PAD_UNSPECIFIED }
    233 unit_pad;
    234 
    235 typedef enum
    236 { DECIMAL_POINT, DECIMAL_COMMA, DECIMAL_UNSPECIFIED }
    237 unit_decimal;
    238 
    239 typedef enum
    240 { ENCODING_UTF8, ENCODING_DEFAULT, ENCODING_UNSPECIFIED }
    241 unit_encoding;
    242 
    243 typedef enum
    244 { ROUND_UP = GFC_FPE_UPWARD,
    245   ROUND_DOWN = GFC_FPE_DOWNWARD,
    246   ROUND_ZERO = GFC_FPE_TOWARDZERO,
    247   ROUND_NEAREST = GFC_FPE_TONEAREST,
    248   ROUND_COMPATIBLE = 10, /* round away from zero.  */
    249   ROUND_PROCDEFINED, /* Here as ROUND_NEAREST. */
    250   ROUND_UNSPECIFIED /* Should never occur. */
    251 }
    252 unit_round;
    253 
    254 /* NOTE: unit_sign must correspond with the sign_status enumerator in
    255    st_parameter_dt to not break the ABI.  */
    256 typedef enum
    257 { SIGN_PROCDEFINED, SIGN_SUPPRESS, SIGN_PLUS, SIGN_UNSPECIFIED }
    258 unit_sign;
    259 
    260 typedef enum
    261 { ADVANCE_YES, ADVANCE_NO, ADVANCE_UNSPECIFIED }
    262 unit_advance;
    263 
    264 typedef enum
    265 {READING, WRITING, LIST_READING, LIST_WRITING}
    266 unit_mode;
    267 
    268 typedef enum
    269 { ASYNC_YES, ASYNC_NO, ASYNC_UNSPECIFIED }
    270 unit_async;
    271 
    272 typedef enum
    273 { SHARE_DENYRW, SHARE_DENYNONE,
    274   SHARE_UNSPECIFIED
    275 }
    276 unit_share;
    277 
    278 typedef enum
    279 { CC_LIST, CC_FORTRAN, CC_NONE,
    280   CC_UNSPECIFIED
    281 }
    282 unit_cc;
    283 
    284 /* End-of-record types for CC_FORTRAN.  */
    285 typedef enum
    286 { CCF_DEFAULT=0x0,
    287   CCF_OVERPRINT=0x1,
    288   CCF_ONE_LF=0x2,
    289   CCF_TWO_LF=0x4,
    290   CCF_PAGE_FEED=0x8,
    291   CCF_PROMPT=0x10,
    292   CCF_OVERPRINT_NOA=0x20,
    293 } /* 6 bits */
    294 cc_fortran;
    295 
    296 typedef enum
    297 { SIGN_S, SIGN_SS, SIGN_SP }
    298 unit_sign_s;
    299 
    300 /* Make sure to keep st_parameter_* in sync with gcc/fortran/ioparm.def.  */
    301 
    302 #define CHARACTER1(name) \
    303 	      char * name; \
    304 	      gfc_charlen_type name ## _len
    305 #define CHARACTER2(name) \
    306 	      gfc_charlen_type name ## _len; \
    307 	      char * name
    308 
    309 typedef struct
    310 {
    311   st_parameter_common common;
    312   GFC_IO_INT recl_in;
    313   CHARACTER2 (file);
    314   CHARACTER1 (status);
    315   CHARACTER2 (access);
    316   CHARACTER1 (form);
    317   CHARACTER2 (blank);
    318   CHARACTER1 (position);
    319   CHARACTER2 (action);
    320   CHARACTER1 (delim);
    321   CHARACTER2 (pad);
    322   CHARACTER1 (convert);
    323   CHARACTER2 (decimal);
    324   CHARACTER1 (encoding);
    325   CHARACTER2 (round);
    326   CHARACTER1 (sign);
    327   CHARACTER2 (asynchronous);
    328   GFC_INTEGER_4 *newunit;
    329   GFC_INTEGER_4 readonly;
    330   CHARACTER2 (cc);
    331   CHARACTER1 (share);
    332 }
    333 st_parameter_open;
    334 
    335 #define IOPARM_CLOSE_HAS_STATUS		(1 << 7)
    336 
    337 typedef struct
    338 {
    339   st_parameter_common common;
    340   CHARACTER1 (status);
    341 }
    342 st_parameter_close;
    343 
    344 typedef struct
    345 {
    346   st_parameter_common common;
    347 }
    348 st_parameter_filepos;
    349 
    350 #define IOPARM_INQUIRE_HAS_EXIST	(1 << 7)
    351 #define IOPARM_INQUIRE_HAS_OPENED	(1 << 8)
    352 #define IOPARM_INQUIRE_HAS_NUMBER	(1 << 9)
    353 #define IOPARM_INQUIRE_HAS_NAMED	(1 << 10)
    354 #define IOPARM_INQUIRE_HAS_NEXTREC	(1 << 11)
    355 #define IOPARM_INQUIRE_HAS_RECL_OUT	(1 << 12)
    356 #define IOPARM_INQUIRE_HAS_STRM_POS_OUT (1 << 13)
    357 #define IOPARM_INQUIRE_HAS_FILE		(1 << 14)
    358 #define IOPARM_INQUIRE_HAS_ACCESS	(1 << 15)
    359 #define IOPARM_INQUIRE_HAS_FORM		(1 << 16)
    360 #define IOPARM_INQUIRE_HAS_BLANK	(1 << 17)
    361 #define IOPARM_INQUIRE_HAS_POSITION	(1 << 18)
    362 #define IOPARM_INQUIRE_HAS_ACTION	(1 << 19)
    363 #define IOPARM_INQUIRE_HAS_DELIM	(1 << 20)
    364 #define IOPARM_INQUIRE_HAS_PAD		(1 << 21)
    365 #define IOPARM_INQUIRE_HAS_NAME		(1 << 22)
    366 #define IOPARM_INQUIRE_HAS_SEQUENTIAL	(1 << 23)
    367 #define IOPARM_INQUIRE_HAS_DIRECT	(1 << 24)
    368 #define IOPARM_INQUIRE_HAS_FORMATTED	(1 << 25)
    369 #define IOPARM_INQUIRE_HAS_UNFORMATTED	(1 << 26)
    370 #define IOPARM_INQUIRE_HAS_READ		(1 << 27)
    371 #define IOPARM_INQUIRE_HAS_WRITE	(1 << 28)
    372 #define IOPARM_INQUIRE_HAS_READWRITE	(1 << 29)
    373 #define IOPARM_INQUIRE_HAS_CONVERT	(1 << 30)
    374 #define IOPARM_INQUIRE_HAS_FLAGS2	(1u << 31)
    375 
    376 #define IOPARM_INQUIRE_HAS_ASYNCHRONOUS	(1 << 0)
    377 #define IOPARM_INQUIRE_HAS_DECIMAL	(1 << 1)
    378 #define IOPARM_INQUIRE_HAS_ENCODING	(1 << 2)
    379 #define IOPARM_INQUIRE_HAS_ROUND	(1 << 3)
    380 #define IOPARM_INQUIRE_HAS_SIGN		(1 << 4)
    381 #define IOPARM_INQUIRE_HAS_PENDING	(1 << 5)
    382 #define IOPARM_INQUIRE_HAS_SIZE		(1 << 6)
    383 #define IOPARM_INQUIRE_HAS_ID		(1 << 7)
    384 #define IOPARM_INQUIRE_HAS_IQSTREAM	(1 << 8)
    385 #define IOPARM_INQUIRE_HAS_SHARE	(1 << 9)
    386 #define IOPARM_INQUIRE_HAS_CC		(1 << 10)
    387 
    388 typedef struct
    389 {
    390   st_parameter_common common;
    391   GFC_INTEGER_4 *exist, *opened, *number, *named;
    392   GFC_IO_INT *nextrec, *recl_out, *strm_pos_out;
    393   CHARACTER1 (file);
    394   CHARACTER2 (access);
    395   CHARACTER1 (form);
    396   CHARACTER2 (blank);
    397   CHARACTER1 (position);
    398   CHARACTER2 (action);
    399   CHARACTER1 (delim);
    400   CHARACTER2 (pad);
    401   CHARACTER1 (name);
    402   CHARACTER2 (sequential);
    403   CHARACTER1 (direct);
    404   CHARACTER2 (formatted);
    405   CHARACTER1 (unformatted);
    406   CHARACTER2 (read);
    407   CHARACTER1 (write);
    408   CHARACTER2 (readwrite);
    409   CHARACTER1 (convert);
    410   GFC_INTEGER_4 flags2;
    411   CHARACTER1 (asynchronous);
    412   CHARACTER2 (decimal);
    413   CHARACTER1 (encoding);
    414   CHARACTER2 (round);
    415   CHARACTER1 (sign);
    416   GFC_INTEGER_4 *pending;
    417   GFC_IO_INT *size;
    418   GFC_INTEGER_4 *id;
    419   CHARACTER1 (iqstream);
    420   CHARACTER2 (share);
    421   CHARACTER1 (cc);
    422 }
    423 st_parameter_inquire;
    424 
    425 
    426 #define IOPARM_DT_LIST_FORMAT			(1 << 7)
    427 #define IOPARM_DT_NAMELIST_READ_MODE		(1 << 8)
    428 #define IOPARM_DT_HAS_REC			(1 << 9)
    429 #define IOPARM_DT_HAS_SIZE			(1 << 10)
    430 #define IOPARM_DT_HAS_IOLENGTH			(1 << 11)
    431 #define IOPARM_DT_HAS_FORMAT			(1 << 12)
    432 #define IOPARM_DT_HAS_ADVANCE			(1 << 13)
    433 #define IOPARM_DT_HAS_INTERNAL_UNIT		(1 << 14)
    434 #define IOPARM_DT_HAS_NAMELIST_NAME		(1 << 15)
    435 #define IOPARM_DT_HAS_ID			(1 << 16)
    436 #define IOPARM_DT_HAS_POS			(1 << 17)
    437 #define IOPARM_DT_HAS_ASYNCHRONOUS		(1 << 18)
    438 #define IOPARM_DT_HAS_BLANK			(1 << 19)
    439 #define IOPARM_DT_HAS_DECIMAL			(1 << 20)
    440 #define IOPARM_DT_HAS_DELIM			(1 << 21)
    441 #define IOPARM_DT_HAS_PAD			(1 << 22)
    442 #define IOPARM_DT_HAS_ROUND			(1 << 23)
    443 #define IOPARM_DT_HAS_SIGN			(1 << 24)
    444 #define IOPARM_DT_HAS_F2003                     (1 << 25)
    445 #define IOPARM_DT_HAS_UDTIO                     (1 << 26)
    446 #define IOPARM_DT_DEC_EXT			(1 << 27)
    447 /* Internal use bit.  */
    448 #define IOPARM_DT_IONML_SET			(1u << 31)
    449 
    450 
    451 typedef struct st_parameter_dt
    452 {
    453   st_parameter_common common;
    454   GFC_IO_INT rec;
    455   GFC_IO_INT *size, *iolength;
    456   gfc_array_char *internal_unit_desc;
    457   CHARACTER1 (format);
    458   CHARACTER2 (advance);
    459   CHARACTER1 (internal_unit);
    460   CHARACTER2 (namelist_name);
    461   GFC_INTEGER_4 *id;
    462   GFC_IO_INT pos;
    463   CHARACTER1 (asynchronous);
    464   CHARACTER2 (blank);
    465   CHARACTER1 (decimal);
    466   CHARACTER2 (delim);
    467   CHARACTER1 (pad);
    468   CHARACTER2 (round);
    469   CHARACTER1 (sign);
    470   /* Private part of the structure.  The compiler just needs
    471      to reserve enough space.  */
    472   union
    473     {
    474       struct
    475 	{
    476 	  void (*transfer) (struct st_parameter_dt *, bt, void *, int,
    477 			    size_t, size_t);
    478 	  struct gfc_unit *current_unit;
    479 	  /* Item number in a formatted data transfer.  Also used in namelist
    480 	     read_logical as an index into line_buffer.  */
    481 	  int item_count;
    482 	  unit_mode mode;
    483 	  unit_blank blank_status;
    484 	  unit_sign sign_status;
    485 	  int scale_factor;
    486 	  /* Maximum righthand column written to.  */
    487 	  int max_pos;
    488 	  /* Number of skips + spaces to be done for T and X-editing.  */
    489 	  int skips;
    490 	  /* Number of spaces to be done for T and X-editing.  */
    491 	  int pending_spaces;
    492 	  /* Whether an EOR condition was encountered. Value is:
    493 	       0 if no EOR was encountered
    494 	       1 if an EOR was encountered due to a 1-byte marker (LF)
    495 	       2 if an EOR was encountered due to a 2-bytes marker (CRLF) */
    496 	  int sf_seen_eor;
    497 	  unit_advance advance_status;
    498 	  unsigned reversion_flag : 1; /* Format reversion has occurred.  */
    499 	  unsigned first_item : 1;
    500 	  unsigned seen_dollar : 1;
    501 	  unsigned eor_condition : 1;
    502 	  unsigned no_leading_blank : 1;
    503 	  unsigned char_flag : 1;
    504 	  unsigned input_complete : 1;
    505 	  unsigned at_eol : 1;
    506 	  unsigned comma_flag : 1;
    507 	  /* A namelist specific flag used in the list directed library
    508 	     to flag that calls are being made from namelist read (e.g. to
    509 	     ignore comments or to treat '/' as a terminator)  */
    510 	  unsigned namelist_mode : 1;
    511 	  /* A namelist specific flag used in the list directed library
    512 	     to flag read errors and return, so that an attempt can be
    513 	     made to read a new object name.  */
    514 	  unsigned nml_read_error : 1;
    515 	  /* A sequential formatted read specific flag used to signal that a
    516 	     character string is being read so don't use commas to shorten a
    517 	     formatted field width.  */
    518 	  unsigned sf_read_comma : 1;
    519 	  /* A namelist specific flag used to enable reading input from
    520 	     line_buffer for logical reads.  */
    521 	  unsigned line_buffer_enabled : 1;
    522 	  /* An internal unit specific flag used to identify that the associated
    523 	     unit is internal.  */
    524 	  unsigned unit_is_internal : 1;
    525 	  /* An internal unit specific flag to signify an EOF condition for list
    526 	     directed read.  */
    527 	  unsigned at_eof : 1;
    528 	  /* Used for g0 floating point output.  */
    529 	  unsigned g0_no_blanks : 1;
    530 	  /* Used to signal use of free_format_data.  */
    531 	  unsigned format_not_saved : 1;
    532 	  /* A flag used to identify when a non-standard expanded namelist read
    533 	     has occurred.  */
    534 	  unsigned expanded_read : 1;
    535 	  /* Flag to indicate if the statement has async="YES". */
    536 	  unsigned async : 1;
    537 	  /* 12 unused bits.  */
    538 
    539 	  int child_saved_iostat;
    540 	  int nml_delim;
    541 	  int repeat_count;
    542 	  int saved_length;
    543 	  int saved_used;
    544 	  bt saved_type;
    545 	  char *saved_string;
    546 	  char *scratch;
    547 	  char *line_buffer;
    548 	  struct format_data *fmt;
    549 	  namelist_info *ionml;
    550 #ifdef HAVE_NEWLOCALE
    551 	  locale_t old_locale;
    552 #endif
    553 	  /* Current position within the look-ahead line buffer.  */
    554 	  int line_buffer_pos;
    555 	  /* Storage area for values except for strings.  Must be
    556 	     large enough to hold a complex value (two reals) of the
    557 	     largest kind.  */
    558 	  char value[32];
    559 	  GFC_IO_INT not_used; /* Needed for alignment. */
    560 	  formatted_dtio fdtio_ptr;
    561 	  unformatted_dtio ufdtio_ptr;
    562 	  /* With CC_FORTRAN, the first character of a record determines the
    563 	     style of record end (and start) to use. We must mark down the type
    564 	     when we write first in write_a so we remember the end type later in
    565 	     next_record_w.  */
    566 	  struct
    567 	    {
    568 	      unsigned type : 6; /* See enum cc_fortran.  */
    569 	      unsigned len  : 2; /* Always 0, 1, or 2.  */
    570 	      /* The union is updated after start-of-record is written.  */
    571 	      union
    572 		{
    573 		  char start; /* Output character for start of record.  */
    574 		  char end;   /* Output character for end of record.  */
    575 		} u;
    576 	    } cc;
    577 	} p;
    578       /* This pad size must be equal to the pad_size declared in
    579 	 trans-io.c (gfc_build_io_library_fndecls).  The above structure
    580 	 must be smaller or equal to this array.  */
    581       char pad[16 * sizeof (char *) + 32 * sizeof (int)];
    582     } u;
    583 }
    584 st_parameter_dt;
    585 
    586 /* Ensure st_parameter_dt's u.pad is bigger or equal to u.p.  */
    587 extern char check_st_parameter_dt[sizeof (((st_parameter_dt *) 0)->u.pad)
    588 				  >= sizeof (((st_parameter_dt *) 0)->u.p)
    589 				  ? 1 : -1];
    590 
    591 #define IOPARM_WAIT_HAS_ID		(1 << 7)
    592 
    593 typedef struct
    594 {
    595   st_parameter_common common;
    596   GFC_INTEGER_4 *id;
    597 }
    598 st_parameter_wait;
    599 
    600 
    601 #undef CHARACTER1
    602 #undef CHARACTER2
    603 
    604 typedef struct
    605 {
    606   unit_access access;
    607   unit_action action;
    608   unit_blank blank;
    609   unit_delim delim;
    610   unit_form form;
    611   int is_notpadded;
    612   unit_position position;
    613   unit_status status;
    614   unit_pad pad;
    615   unit_convert convert;
    616   int has_recl;
    617   unit_decimal decimal;
    618   unit_encoding encoding;
    619   unit_round round;
    620   unit_sign sign;
    621   unit_async async;
    622   unit_share share;
    623   unit_cc cc;
    624   int readonly;
    625 }
    626 unit_flags;
    627 
    628 
    629 typedef struct gfc_unit
    630 {
    631   int unit_number;
    632   stream *s;
    633 
    634   /* Treap links.  */
    635   struct gfc_unit *left, *right;
    636   int priority;
    637 
    638   int read_bad, current_record, saved_pos, previous_nonadvancing_write;
    639 
    640   enum
    641   { NO_ENDFILE, AT_ENDFILE, AFTER_ENDFILE }
    642   endfile;
    643 
    644   unit_mode mode;
    645   unit_flags flags;
    646   unit_pad pad_status;
    647   unit_decimal decimal_status;
    648   unit_delim delim_status;
    649   unit_round round_status;
    650 
    651   /* recl                 -- Record length of the file.
    652      last_record          -- Last record number read or written
    653      maxrec               -- Maximum record number in a direct access file
    654      bytes_left           -- Bytes left in current record.
    655      strm_pos             -- Current position in file for STREAM I/O.
    656      recl_subrecord       -- Maximum length for subrecord.
    657      bytes_left_subrecord -- Bytes left in current subrecord.  */
    658   gfc_offset recl, last_record, maxrec, bytes_left, strm_pos,
    659     recl_subrecord, bytes_left_subrecord;
    660 
    661   /* Set to 1 if we have read a subrecord.  */
    662 
    663   int continued;
    664 
    665   /* Contains the pointer to the async unit.  */
    666   struct async_unit *au;
    667 
    668   __gthread_mutex_t lock;
    669   /* Number of threads waiting to acquire this unit's lock.
    670      When non-zero, close_unit doesn't only removes the unit
    671      from the UNIT_ROOT tree, but doesn't free it and the
    672      last of the waiting threads will do that.
    673      This must be either atomically increased/decreased, or
    674      always guarded by UNIT_LOCK.  */
    675   int waiting;
    676   /* Flag set by close_unit if the unit as been closed.
    677      Must be manipulated under unit's lock.  */
    678   int closed;
    679 
    680   /* For traversing arrays */
    681   array_loop_spec *ls;
    682   int rank;
    683 
    684   /* Name of the file at the time OPEN was executed, as a
    685      null-terminated C string.  */
    686   char *filename;
    687 
    688   /* The format hash table.  */
    689   struct format_hash_entry format_hash_table[FORMAT_HASH_SIZE];
    690 
    691   /* Formatting buffer.  */
    692   struct fbuf *fbuf;
    693 
    694   /* Function pointer, points to list_read worker functions.  */
    695   int (*next_char_fn_ptr) (st_parameter_dt *);
    696   void (*push_char_fn_ptr) (st_parameter_dt *, int);
    697 
    698   /* Internal unit char string data.  */
    699   char * internal_unit;
    700   gfc_charlen_type internal_unit_len;
    701   gfc_array_char *string_unit_desc;
    702   int internal_unit_kind;
    703 
    704   /* DTIO Parent/Child procedure, 0 = parent, >0 = child level.  */
    705   int child_dtio;
    706 
    707   /* Used for ungetc() style functionality. Possible values
    708      are an unsigned char, EOF, or EOF - 1 used to mark the
    709      field as not valid.  */
    710   int last_char;
    711   bool has_size;
    712   GFC_IO_INT size_used;
    713 }
    714 gfc_unit;
    715 
    716 typedef struct gfc_saved_unit
    717 {
    718   GFC_INTEGER_4 unit_number;
    719   gfc_unit *unit;
    720 }
    721 gfc_saved_unit;
    722 
    723 /* TEMP_FAILURE_RETRY macro from glibc.  */
    724 
    725 #ifndef TEMP_FAILURE_RETRY
    726 /* Evaluate EXPRESSION, and repeat as long as it returns -1 with `errno'
    727    set to EINTR.  */
    728 
    729 # define TEMP_FAILURE_RETRY(expression) \
    730   (__extension__                                                              \
    731     ({ long int __result;                                                     \
    732        do __result = (long int) (expression);                                 \
    733        while (__result == -1L && errno == EINTR);                             \
    734        __result; }))
    735 #endif
    736 
    737 
    738 /* unit.c */
    739 
    740 /* Maximum file offset, computed at library initialization time.  */
    741 extern gfc_offset max_offset;
    742 internal_proto(max_offset);
    743 
    744 /* Default RECL for sequential access if not given in OPEN statement,
    745    computed at library initialization time.  */
    746 extern gfc_offset default_recl;
    747 internal_proto(default_recl);
    748 
    749 /* Unit tree root.  */
    750 extern gfc_unit *unit_root;
    751 internal_proto(unit_root);
    752 
    753 extern __gthread_mutex_t unit_lock;
    754 internal_proto(unit_lock);
    755 
    756 extern int close_unit (gfc_unit *);
    757 internal_proto(close_unit);
    758 
    759 extern gfc_unit *set_internal_unit (st_parameter_dt *, gfc_unit *, int);
    760 internal_proto(set_internal_unit);
    761 
    762 extern void stash_internal_unit (st_parameter_dt *);
    763 internal_proto(stash_internal_unit);
    764 
    765 extern gfc_unit *find_unit (int);
    766 internal_proto(find_unit);
    767 
    768 extern gfc_unit *find_or_create_unit (int);
    769 internal_proto(find_or_create_unit);
    770 
    771 extern gfc_unit *get_unit (st_parameter_dt *, int);
    772 internal_proto(get_unit);
    773 
    774 extern void unlock_unit(gfc_unit *);
    775 internal_proto(unlock_unit);
    776 
    777 extern void finish_last_advance_record (gfc_unit *u);
    778 internal_proto(finish_last_advance_record);
    779 
    780 extern int unit_truncate(gfc_unit *, gfc_offset, st_parameter_common *);
    781 internal_proto(unit_truncate);
    782 
    783 extern int newunit_alloc (void);
    784 internal_proto(newunit_alloc);
    785 
    786 extern void newunit_free (int);
    787 internal_proto(newunit_free);
    788 
    789 
    790 /* open.c */
    791 
    792 extern gfc_unit *new_unit (st_parameter_open *, gfc_unit *, unit_flags *);
    793 internal_proto(new_unit);
    794 
    795 
    796 /* transfer.c */
    797 
    798 #define SCRATCH_SIZE 300
    799 
    800 extern const char *type_name (bt);
    801 internal_proto(type_name);
    802 
    803 extern void * read_block_form (st_parameter_dt *, size_t *);
    804 internal_proto(read_block_form);
    805 
    806 extern void * read_block_form4 (st_parameter_dt *, size_t *);
    807 internal_proto(read_block_form4);
    808 
    809 extern void *write_block (st_parameter_dt *, size_t);
    810 internal_proto(write_block);
    811 
    812 extern gfc_offset next_array_record (st_parameter_dt *, array_loop_spec *,
    813 				     int*);
    814 internal_proto(next_array_record);
    815 
    816 extern gfc_offset init_loop_spec (gfc_array_char *, array_loop_spec *,
    817 				  gfc_offset *);
    818 internal_proto(init_loop_spec);
    819 
    820 extern void next_record (st_parameter_dt *, int);
    821 internal_proto(next_record);
    822 
    823 extern void st_wait (st_parameter_wait *);
    824 export_proto (st_wait);
    825 
    826 extern void st_wait_async (st_parameter_wait *);
    827 export_proto (st_wait_async);
    828 
    829 extern void hit_eof (st_parameter_dt *);
    830 internal_proto(hit_eof);
    831 
    832 extern void transfer_array_inner (st_parameter_dt *, gfc_array_char *, int,
    833 				  gfc_charlen_type);
    834 internal_proto (transfer_array_inner);
    835 
    836 /* read.c */
    837 
    838 extern void set_integer (void *, GFC_INTEGER_LARGEST, int);
    839 internal_proto(set_integer);
    840 
    841 extern GFC_UINTEGER_LARGEST si_max (int);
    842 internal_proto(si_max);
    843 
    844 extern int convert_real (st_parameter_dt *, void *, const char *, int);
    845 internal_proto(convert_real);
    846 
    847 extern int convert_infnan (st_parameter_dt *, void *, const char *, int);
    848 internal_proto(convert_infnan);
    849 
    850 extern void read_a (st_parameter_dt *, const fnode *, char *, size_t);
    851 internal_proto(read_a);
    852 
    853 extern void read_a_char4 (st_parameter_dt *, const fnode *, char *, size_t);
    854 internal_proto(read_a);
    855 
    856 extern void read_f (st_parameter_dt *, const fnode *, char *, int);
    857 internal_proto(read_f);
    858 
    859 extern void read_l (st_parameter_dt *, const fnode *, char *, int);
    860 internal_proto(read_l);
    861 
    862 extern void read_x (st_parameter_dt *, size_t);
    863 internal_proto(read_x);
    864 
    865 extern void read_radix (st_parameter_dt *, const fnode *, char *, int, int);
    866 internal_proto(read_radix);
    867 
    868 extern void read_decimal (st_parameter_dt *, const fnode *, char *, int);
    869 internal_proto(read_decimal);
    870 
    871 extern void read_user_defined (st_parameter_dt *, void *);
    872 internal_proto(read_user_defined);
    873 
    874 extern void read_user_defined (st_parameter_dt *, void *);
    875 internal_proto(read_user_defined);
    876 
    877 /* list_read.c */
    878 
    879 extern void list_formatted_read (st_parameter_dt *, bt, void *, int, size_t,
    880 				 size_t);
    881 internal_proto(list_formatted_read);
    882 
    883 extern void finish_list_read (st_parameter_dt *);
    884 internal_proto(finish_list_read);
    885 
    886 extern void namelist_read (st_parameter_dt *);
    887 internal_proto(namelist_read);
    888 
    889 extern void namelist_write (st_parameter_dt *);
    890 internal_proto(namelist_write);
    891 
    892 /* write.c */
    893 
    894 extern void write_a (st_parameter_dt *, const fnode *, const char *, size_t);
    895 internal_proto(write_a);
    896 
    897 extern void write_a_char4 (st_parameter_dt *, const fnode *, const char *, size_t);
    898 internal_proto(write_a_char4);
    899 
    900 extern void write_b (st_parameter_dt *, const fnode *, const char *, int);
    901 internal_proto(write_b);
    902 
    903 extern void write_d (st_parameter_dt *, const fnode *, const char *, int);
    904 internal_proto(write_d);
    905 
    906 extern void write_e (st_parameter_dt *, const fnode *, const char *, int);
    907 internal_proto(write_e);
    908 
    909 extern void write_en (st_parameter_dt *, const fnode *, const char *, int);
    910 internal_proto(write_en);
    911 
    912 extern void write_es (st_parameter_dt *, const fnode *, const char *, int);
    913 internal_proto(write_es);
    914 
    915 extern void write_f (st_parameter_dt *, const fnode *, const char *, int);
    916 internal_proto(write_f);
    917 
    918 extern void write_i (st_parameter_dt *, const fnode *, const char *, int);
    919 internal_proto(write_i);
    920 
    921 extern void write_l (st_parameter_dt *, const fnode *, char *, int);
    922 internal_proto(write_l);
    923 
    924 extern void write_o (st_parameter_dt *, const fnode *, const char *, int);
    925 internal_proto(write_o);
    926 
    927 extern void write_real (st_parameter_dt *, const char *, int);
    928 internal_proto(write_real);
    929 
    930 extern void write_real_g0 (st_parameter_dt *, const char *, int, int);
    931 internal_proto(write_real_g0);
    932 
    933 extern void write_x (st_parameter_dt *, int, int);
    934 internal_proto(write_x);
    935 
    936 extern void write_z (st_parameter_dt *, const fnode *, const char *, int);
    937 internal_proto(write_z);
    938 
    939 extern void write_user_defined (st_parameter_dt *, void *);
    940 internal_proto(write_user_defined);
    941 
    942 extern void write_user_defined (st_parameter_dt *, void *);
    943 internal_proto(write_user_defined);
    944 
    945 extern void list_formatted_write (st_parameter_dt *, bt, void *, int, size_t,
    946 				  size_t);
    947 internal_proto(list_formatted_write);
    948 
    949 /* size_from_kind.c */
    950 extern size_t size_from_real_kind (int);
    951 internal_proto(size_from_real_kind);
    952 
    953 extern size_t size_from_complex_kind (int);
    954 internal_proto(size_from_complex_kind);
    955 
    956 
    957 /* lock.c */
    958 extern void free_ionml (st_parameter_dt *);
    959 internal_proto(free_ionml);
    960 
    961 static inline void
    962 inc_waiting_locked (gfc_unit *u)
    963 {
    964 #ifdef HAVE_ATOMIC_FETCH_ADD
    965   (void) __atomic_fetch_add (&u->waiting, 1, __ATOMIC_RELAXED);
    966 #else
    967   u->waiting++;
    968 #endif
    969 }
    970 
    971 static inline int
    972 predec_waiting_locked (gfc_unit *u)
    973 {
    974 #ifdef HAVE_ATOMIC_FETCH_ADD
    975   /* Note that the pattern
    976 
    977      if (predec_waiting_locked (u) == 0)
    978          // destroy u
    979 
    980      could be further optimized by making this be an __ATOMIC_RELEASE,
    981      and then inserting a
    982 
    983      __atomic_thread_fence (__ATOMIC_ACQUIRE);
    984 
    985      inside the branch before destroying.  But for now, lets keep it
    986      simple.  */
    987   return __atomic_add_fetch (&u->waiting, -1, __ATOMIC_ACQ_REL);
    988 #else
    989   return --u->waiting;
    990 #endif
    991 }
    992 
    993 static inline void
    994 dec_waiting_unlocked (gfc_unit *u)
    995 {
    996 #ifdef HAVE_ATOMIC_FETCH_ADD
    997   (void) __atomic_fetch_add (&u->waiting, -1, __ATOMIC_RELAXED);
    998 #else
    999   __gthread_mutex_lock (&unit_lock);
   1000   u->waiting--;
   1001   __gthread_mutex_unlock (&unit_lock);
   1002 #endif
   1003 }
   1004 
   1005 
   1006 static inline void
   1007 memset4 (gfc_char4_t *p, gfc_char4_t c, int k)
   1008 {
   1009   int j;
   1010   for (j = 0; j < k; j++)
   1011     *p++ = c;
   1012 }
   1013 
   1014 #endif
   1015 
   1016 extern void
   1017 st_write_done_worker (st_parameter_dt *);
   1018 internal_proto (st_write_done_worker);
   1019 
   1020 extern void
   1021 st_read_done_worker (st_parameter_dt *);
   1022 internal_proto (st_read_done_worker);
   1023 
   1024 extern void
   1025 data_transfer_init_worker (st_parameter_dt *, int);
   1026 internal_proto (data_transfer_init_worker);
   1027