OSDN Git Service

e8e8390d1c54bf732c9f167bf93c7744a1ac1fc5
[pf3gnuchains/gcc-fork.git] / libgfortran / io / io.h
1 /* Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
2    Contributed by Andy Vaught
3
4 This file is part of the GNU Fortran 95 runtime library (libgfortran).
5
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)
9 any later version.
10
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.
15
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.  */
20
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.  */
27
28 #ifndef GFOR_IO_H
29 #define GFOR_IO_H
30
31 /* IO library include.  */
32
33 #include <setjmp.h>
34 #include "libgfortran.h"
35
36 #include <gthr.h>
37
38 #define DEFAULT_TEMPDIR "/tmp"
39
40 /* Basic types used in data transfers.  */
41
42 typedef enum
43 { BT_NULL, BT_INTEGER, BT_LOGICAL, BT_CHARACTER, BT_REAL,
44   BT_COMPLEX
45 }
46 bt;
47
48
49 struct st_parameter_dt;
50
51 typedef struct stream
52 {
53   char *(*alloc_w_at) (struct stream *, int *, gfc_offset);
54   char *(*alloc_r_at) (struct stream *, int *, gfc_offset);
55   try (*sfree) (struct stream *);
56   try (*close) (struct stream *);
57   try (*seek) (struct stream *, gfc_offset);
58   try (*truncate) (struct stream *);
59   int (*read) (struct stream *, void *, size_t *);
60   int (*write) (struct stream *, const void *, size_t *);
61   try (*set) (struct stream *, int, size_t);
62 }
63 stream;
64
65
66 /* Macros for doing file I/O given a stream.  */
67
68 #define sfree(s) ((s)->sfree)(s)
69 #define sclose(s) ((s)->close)(s)
70
71 #define salloc_r(s, len) ((s)->alloc_r_at)(s, len, -1)
72 #define salloc_w(s, len) ((s)->alloc_w_at)(s, len, -1)
73
74 #define salloc_r_at(s, len, where) ((s)->alloc_r_at)(s, len, where)
75 #define salloc_w_at(s, len, where) ((s)->alloc_w_at)(s, len, where)
76
77 #define sseek(s, pos) ((s)->seek)(s, pos)
78 #define struncate(s) ((s)->truncate)(s)
79 #define sread(s, buf, nbytes) ((s)->read)(s, buf, nbytes)
80 #define swrite(s, buf, nbytes) ((s)->write)(s, buf, nbytes)
81
82 #define sset(s, c, n) ((s)->set)(s, c, n)
83
84 /* The array_loop_spec contains the variables for the loops over index ranges
85    that are encountered.  Since the variables can be negative, ssize_t
86    is used.  */
87
88 typedef struct array_loop_spec
89 {
90   /* Index counter for this dimension.  */
91   ssize_t idx;
92
93   /* Start for the index counter.  */
94   ssize_t start;
95
96   /* End for the index counter.  */
97   ssize_t end;
98
99   /* Step for the index counter.  */
100   ssize_t step;
101 }
102 array_loop_spec;
103
104 /* Representation of a namelist object in libgfortran
105
106    Namelist Records
107       &GROUPNAME  OBJECT=value[s] [,OBJECT=value[s]].../
108      or
109       &GROUPNAME  OBJECT=value[s] [,OBJECT=value[s]]...&END
110
111    The object can be a fully qualified, compound name for an intrinsic
112    type, derived types or derived type components.  So, a substring
113    a(:)%b(4)%ch(2:4)(1:7) has to be treated correctly in namelist
114    read. Hence full information about the structure of the object has
115    to be available to list_read.c and write.
116
117    These requirements are met by the following data structures.
118
119    namelist_info type contains all the scalar information about the
120    object and arrays of descriptor_dimension and array_loop_spec types for
121    arrays.  */
122
123 typedef struct namelist_type
124 {
125
126   /* Object type, stored as GFC_DTYPE_xxxx.  */
127   bt type;
128
129   /* Object name.  */
130   char * var_name;
131
132   /* Address for the start of the object's data.  */
133   void * mem_pos;
134
135   /* Flag to show that a read is to be attempted for this node.  */
136   int touched;
137
138   /* Length of intrinsic type in bytes.  */
139   int len;
140
141   /* Rank of the object.  */
142   int var_rank;
143
144   /* Overall size of the object in bytes.  */
145   index_type size;
146
147   /* Length of character string.  */
148   index_type string_length;
149
150   descriptor_dimension * dim;
151   array_loop_spec * ls;
152   struct namelist_type * next;
153 }
154 namelist_info;
155
156 /* Options for the OPEN statement.  */
157
158 typedef enum
159 { ACCESS_SEQUENTIAL, ACCESS_DIRECT, ACCESS_APPEND, ACCESS_STREAM,
160   ACCESS_UNSPECIFIED
161 }
162 unit_access;
163
164 typedef enum
165 { ACTION_READ, ACTION_WRITE, ACTION_READWRITE,
166   ACTION_UNSPECIFIED
167 }
168 unit_action;
169
170 typedef enum
171 { BLANK_NULL, BLANK_ZERO, BLANK_UNSPECIFIED }
172 unit_blank;
173
174 typedef enum
175 { DELIM_NONE, DELIM_APOSTROPHE, DELIM_QUOTE,
176   DELIM_UNSPECIFIED
177 }
178 unit_delim;
179
180 typedef enum
181 { FORM_FORMATTED, FORM_UNFORMATTED, FORM_UNSPECIFIED }
182 unit_form;
183
184 typedef enum
185 { POSITION_ASIS, POSITION_REWIND, POSITION_APPEND,
186   POSITION_UNSPECIFIED
187 }
188 unit_position;
189
190 typedef enum
191 { STATUS_UNKNOWN, STATUS_OLD, STATUS_NEW, STATUS_SCRATCH,
192   STATUS_REPLACE, STATUS_UNSPECIFIED
193 }
194 unit_status;
195
196 typedef enum
197 { PAD_YES, PAD_NO, PAD_UNSPECIFIED }
198 unit_pad;
199
200 typedef enum
201 { ADVANCE_YES, ADVANCE_NO, ADVANCE_UNSPECIFIED }
202 unit_advance;
203
204 typedef enum
205 {READING, WRITING}
206 unit_mode;
207
208 typedef enum
209 { CONVERT_NONE=-1, CONVERT_NATIVE, CONVERT_SWAP, CONVERT_BIG, CONVERT_LITTLE }
210 unit_convert;
211
212 #define CHARACTER1(name) \
213               char * name; \
214               gfc_charlen_type name ## _len
215 #define CHARACTER2(name) \
216               gfc_charlen_type name ## _len; \
217               char * name
218
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)
229
230 #define IOPARM_COMMON_MASK              ((1 << 7) - 1)
231
232 typedef struct st_parameter_common
233 {
234   GFC_INTEGER_4 flags;
235   GFC_INTEGER_4 unit;
236   const char *filename;
237   GFC_INTEGER_4 line;
238   CHARACTER2 (iomsg);
239   GFC_INTEGER_4 *iostat;
240 }
241 st_parameter_common;
242
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)
253 #define IOPARM_OPEN_HAS_CONVERT         (1 << 17)
254
255 typedef struct
256 {
257   st_parameter_common common;
258   GFC_INTEGER_4 recl_in;
259   CHARACTER2 (file);
260   CHARACTER1 (status);
261   CHARACTER2 (access);
262   CHARACTER1 (form);
263   CHARACTER2 (blank);
264   CHARACTER1 (position);
265   CHARACTER2 (action);
266   CHARACTER1 (delim);
267   CHARACTER2 (pad);
268   CHARACTER1 (convert);
269 }
270 st_parameter_open;
271
272 #define IOPARM_CLOSE_HAS_STATUS         (1 << 7)
273
274 typedef struct
275 {
276   st_parameter_common common;
277   CHARACTER1 (status);
278 }
279 st_parameter_close;
280
281 typedef struct
282 {
283   st_parameter_common common;
284 }
285 st_parameter_filepos;
286
287 #define IOPARM_INQUIRE_HAS_EXIST        (1 << 7)
288 #define IOPARM_INQUIRE_HAS_OPENED       (1 << 8)
289 #define IOPARM_INQUIRE_HAS_NUMBER       (1 << 9)
290 #define IOPARM_INQUIRE_HAS_NAMED        (1 << 10)
291 #define IOPARM_INQUIRE_HAS_NEXTREC      (1 << 11)
292 #define IOPARM_INQUIRE_HAS_RECL_OUT     (1 << 12)
293 #define IOPARM_INQUIRE_HAS_STRM_POS_OUT (1 << 13)
294 #define IOPARM_INQUIRE_HAS_FILE         (1 << 14)
295 #define IOPARM_INQUIRE_HAS_ACCESS       (1 << 15)
296 #define IOPARM_INQUIRE_HAS_FORM         (1 << 16)
297 #define IOPARM_INQUIRE_HAS_BLANK        (1 << 17)
298 #define IOPARM_INQUIRE_HAS_POSITION     (1 << 18)
299 #define IOPARM_INQUIRE_HAS_ACTION       (1 << 19)
300 #define IOPARM_INQUIRE_HAS_DELIM        (1 << 20)
301 #define IOPARM_INQUIRE_HAS_PAD          (1 << 21)
302 #define IOPARM_INQUIRE_HAS_NAME         (1 << 22)
303 #define IOPARM_INQUIRE_HAS_SEQUENTIAL   (1 << 23)
304 #define IOPARM_INQUIRE_HAS_DIRECT       (1 << 24)
305 #define IOPARM_INQUIRE_HAS_FORMATTED    (1 << 25)
306 #define IOPARM_INQUIRE_HAS_UNFORMATTED  (1 << 26)
307 #define IOPARM_INQUIRE_HAS_READ         (1 << 27)
308 #define IOPARM_INQUIRE_HAS_WRITE        (1 << 28)
309 #define IOPARM_INQUIRE_HAS_READWRITE    (1 << 29)
310 #define IOPARM_INQUIRE_HAS_CONVERT      (1 << 30)
311
312 typedef struct
313 {
314   st_parameter_common common;
315   GFC_INTEGER_4 *exist, *opened, *number, *named;
316   GFC_INTEGER_4 *nextrec, *recl_out;
317   GFC_IO_INT *strm_pos_out;
318   CHARACTER1 (file);
319   CHARACTER2 (access);
320   CHARACTER1 (form);
321   CHARACTER2 (blank);
322   CHARACTER1 (position);
323   CHARACTER2 (action);
324   CHARACTER1 (delim);
325   CHARACTER2 (pad);
326   CHARACTER1 (name);
327   CHARACTER2 (sequential);
328   CHARACTER1 (direct);
329   CHARACTER2 (formatted);
330   CHARACTER1 (unformatted);
331   CHARACTER2 (read);
332   CHARACTER1 (write);
333   CHARACTER2 (readwrite);
334   CHARACTER1 (convert);
335 }
336 st_parameter_inquire;
337
338 struct gfc_unit;
339 struct format_data;
340
341 #define IOPARM_DT_LIST_FORMAT                   (1 << 7)
342 #define IOPARM_DT_NAMELIST_READ_MODE            (1 << 8)
343 #define IOPARM_DT_HAS_REC                       (1 << 9)
344 #define IOPARM_DT_HAS_SIZE                      (1 << 10)
345 #define IOPARM_DT_HAS_IOLENGTH                  (1 << 11)
346 #define IOPARM_DT_HAS_FORMAT                    (1 << 12)
347 #define IOPARM_DT_HAS_ADVANCE                   (1 << 13)
348 #define IOPARM_DT_HAS_INTERNAL_UNIT             (1 << 14)
349 #define IOPARM_DT_HAS_NAMELIST_NAME             (1 << 15)
350 /* Internal use bit.  */
351 #define IOPARM_DT_IONML_SET                     (1 << 31)
352
353 typedef struct st_parameter_dt
354 {
355   st_parameter_common common;
356   GFC_IO_INT rec;
357   GFC_INTEGER_4 *size, *iolength;
358   gfc_array_char *internal_unit_desc;
359   CHARACTER1 (format);
360   CHARACTER2 (advance);
361   CHARACTER1 (internal_unit);
362   CHARACTER2 (namelist_name);
363   /* Private part of the structure.  The compiler just needs
364      to reserve enough space.  */
365   union
366     {
367       struct
368         {
369           void (*transfer) (struct st_parameter_dt *, bt, void *, int,
370                             size_t, size_t);
371           struct gfc_unit *current_unit;
372           /* Item number in a formatted data transfer.  Also used in namelist
373                read_logical as an index into line_buffer.  */
374           int item_count;
375           unit_mode mode;
376           unit_blank blank_status;
377           enum {SIGN_S, SIGN_SS, SIGN_SP} sign_status;
378           int scale_factor;
379           int max_pos; /* Maximum righthand column written to.  */
380           /* Number of skips + spaces to be done for T and X-editing.  */
381           int skips;
382           /* Number of spaces to be done for T and X-editing.  */
383           int pending_spaces;
384           /* Whether an EOR condition was encountered. Value is:
385                0 if no EOR was encountered
386                1 if an EOR was encountered due to a 1-byte marker (LF)
387                2 if an EOR was encountered due to a 2-bytes marker (CRLF) */
388           int sf_seen_eor;
389           unit_advance advance_status;
390
391           unsigned reversion_flag : 1; /* Format reversion has occurred.  */
392           unsigned first_item : 1;
393           unsigned seen_dollar : 1;
394           unsigned eor_condition : 1;
395           unsigned no_leading_blank : 1;
396           unsigned char_flag : 1;
397           unsigned input_complete : 1;
398           unsigned at_eol : 1;
399           unsigned comma_flag : 1;
400           /* A namelist specific flag used in the list directed library
401              to flag that calls are being made from namelist read (eg. to
402              ignore comments or to treat '/' as a terminator)  */
403           unsigned namelist_mode : 1;
404           /* A namelist specific flag used in the list directed library
405              to flag read errors and return, so that an attempt can be
406              made to read a new object name.  */
407           unsigned nml_read_error : 1;
408           /* A sequential formatted read specific flag used to signal that a
409              character string is being read so don't use commas to shorten a
410              formatted field width.  */
411           unsigned sf_read_comma : 1;
412           /* A namelist specific flag used to enable reading input from 
413              line_buffer for logical reads.  */
414           unsigned line_buffer_enabled : 1;
415           /* An internal unit specific flag used to identify that the associated
416              unit is internal.  */
417           unsigned unit_is_internal : 1;
418           /* An internal unit specific flag to signify an EOF condition for list
419              directed read.  */
420           unsigned at_eof : 1;
421           /* 16 unused bits.  */
422
423           char last_char;
424           char nml_delim;
425
426           int repeat_count;
427           int saved_length;
428           int saved_used;
429           bt saved_type;
430           char *saved_string;
431           char *scratch;
432           char *line_buffer;
433           struct format_data *fmt;
434           jmp_buf *eof_jump;
435           namelist_info *ionml;
436           /* A flag used to identify when a non-standard expanded namelist read
437              has occurred.  */
438           int expanded_read;
439           /* Storage area for values except for strings.  Must be large
440              enough to hold a complex value (two reals) of the largest
441              kind.  */
442           char value[32];
443           gfc_offset size_used;
444         } p;
445       /* This pad size must be equal to the pad_size declared in
446          trans-io.c (gfc_build_io_library_fndecls).  The above structure
447          must be smaller or equal to this array.  */
448       char pad[16 * sizeof (char *) + 32 * sizeof (int)];
449     } u;
450 }
451 st_parameter_dt;
452
453 /* Ensure st_parameter_dt's u.pad is bigger or equal to u.p.  */
454 extern char check_st_parameter_dt[sizeof (((st_parameter_dt *) 0)->u.pad)
455                                   >= sizeof (((st_parameter_dt *) 0)->u.p)
456                                   ? 1 : -1];
457
458 #undef CHARACTER1
459 #undef CHARACTER2
460
461 typedef struct
462 {
463   unit_access access;
464   unit_action action;
465   unit_blank blank;
466   unit_delim delim;
467   unit_form form;
468   int is_notpadded;
469   unit_position position;
470   unit_status status;
471   unit_pad pad;
472   unit_convert convert;
473   int has_recl;
474 }
475 unit_flags;
476
477
478 /* The default value of record length for preconnected units is defined
479    here. This value can be overriden by an environment variable.
480    Default value is 1 Gb.  */
481
482 #define DEFAULT_RECL 1073741824
483
484
485 typedef struct gfc_unit
486 {
487   int unit_number;
488   stream *s;
489   
490   /* Treap links.  */
491   struct gfc_unit *left, *right;
492   int priority;
493
494   int read_bad, current_record;
495   enum
496   { NO_ENDFILE, AT_ENDFILE, AFTER_ENDFILE }
497   endfile;
498
499   unit_mode mode;
500   unit_flags flags;
501
502   /* recl           -- Record length of the file.
503      last_record    -- Last record number read or written
504      maxrec         -- Maximum record number in a direct access file
505      bytes_left     -- Bytes left in current record.
506      strm_pos       -- Current position in file for STREAM I/O.  */
507   gfc_offset recl, last_record, maxrec, bytes_left, strm_pos;
508
509   __gthread_mutex_t lock;
510   /* Number of threads waiting to acquire this unit's lock.
511      When non-zero, close_unit doesn't only removes the unit
512      from the UNIT_ROOT tree, but doesn't free it and the
513      last of the waiting threads will do that.
514      This must be either atomically increased/decreased, or
515      always guarded by UNIT_LOCK.  */
516   int waiting;
517   /* Flag set by close_unit if the unit as been closed.
518      Must be manipulated under unit's lock.  */
519   int closed;
520
521   /* For traversing arrays */
522   array_loop_spec *ls;
523   int rank;
524
525   int file_len;
526   char *file;
527 }
528 gfc_unit;
529
530 /* Format tokens.  Only about half of these can be stored in the
531    format nodes.  */
532
533 typedef enum
534 {
535   FMT_NONE = 0, FMT_UNKNOWN, FMT_SIGNED_INT, FMT_ZERO, FMT_POSINT, FMT_PERIOD,
536   FMT_COMMA, FMT_COLON, FMT_SLASH, FMT_DOLLAR, FMT_T, FMT_TR, FMT_TL,
537   FMT_LPAREN, FMT_RPAREN, FMT_X, FMT_S, FMT_SS, FMT_SP, FMT_STRING,
538   FMT_BADSTRING, FMT_P, FMT_I, FMT_B, FMT_BN, FMT_BZ, FMT_O, FMT_Z, FMT_F,
539   FMT_E, FMT_EN, FMT_ES, FMT_G, FMT_L, FMT_A, FMT_D, FMT_H, FMT_END
540 }
541 format_token;
542
543
544 /* Format nodes.  A format string is converted into a tree of these
545    structures, which is traversed as part of a data transfer statement.  */
546
547 typedef struct fnode
548 {
549   format_token format;
550   int repeat;
551   struct fnode *next;
552   char *source;
553
554   union
555   {
556     struct
557     {
558       int w, d, e;
559     }
560     real;
561
562     struct
563     {
564       int length;
565       char *p;
566     }
567     string;
568
569     struct
570     {
571       int w, m;
572     }
573     integer;
574
575     int w;
576     int k;
577     int r;
578     int n;
579
580     struct fnode *child;
581   }
582   u;
583
584   /* Members for traversing the tree during data transfer.  */
585
586   int count;
587   struct fnode *current;
588
589 }
590 fnode;
591
592
593 /* unix.c */
594
595 extern int move_pos_offset (stream *, int);
596 internal_proto(move_pos_offset);
597
598 extern int compare_files (stream *, stream *);
599 internal_proto(compare_files);
600
601 extern stream *open_external (st_parameter_open *, unit_flags *);
602 internal_proto(open_external);
603
604 extern stream *open_internal (char *, int);
605 internal_proto(open_internal);
606
607 extern stream *input_stream (void);
608 internal_proto(input_stream);
609
610 extern stream *output_stream (void);
611 internal_proto(output_stream);
612
613 extern stream *error_stream (void);
614 internal_proto(error_stream);
615
616 extern int compare_file_filename (gfc_unit *, const char *, int);
617 internal_proto(compare_file_filename);
618
619 extern gfc_unit *find_file (const char *file, gfc_charlen_type file_len);
620 internal_proto(find_file);
621
622 extern void flush_all_units (void);
623 internal_proto(flush_all_units);
624
625 extern int stream_at_bof (stream *);
626 internal_proto(stream_at_bof);
627
628 extern int stream_at_eof (stream *);
629 internal_proto(stream_at_eof);
630
631 extern int delete_file (gfc_unit *);
632 internal_proto(delete_file);
633
634 extern int file_exists (const char *file, gfc_charlen_type file_len);
635 internal_proto(file_exists);
636
637 extern const char *inquire_sequential (const char *, int);
638 internal_proto(inquire_sequential);
639
640 extern const char *inquire_direct (const char *, int);
641 internal_proto(inquire_direct);
642
643 extern const char *inquire_formatted (const char *, int);
644 internal_proto(inquire_formatted);
645
646 extern const char *inquire_unformatted (const char *, int);
647 internal_proto(inquire_unformatted);
648
649 extern const char *inquire_read (const char *, int);
650 internal_proto(inquire_read);
651
652 extern const char *inquire_write (const char *, int);
653 internal_proto(inquire_write);
654
655 extern const char *inquire_readwrite (const char *, int);
656 internal_proto(inquire_readwrite);
657
658 extern gfc_offset file_length (stream *);
659 internal_proto(file_length);
660
661 extern gfc_offset file_position (stream *);
662 internal_proto(file_position);
663
664 extern int is_seekable (stream *);
665 internal_proto(is_seekable);
666
667 extern int is_preconnected (stream *);
668 internal_proto(is_preconnected);
669
670 extern void flush_if_preconnected (stream *);
671 internal_proto(flush_if_preconnected);
672
673 extern void empty_internal_buffer(stream *);
674 internal_proto(empty_internal_buffer);
675
676 extern try flush (stream *);
677 internal_proto(flush);
678
679 extern int stream_isatty (stream *);
680 internal_proto(stream_isatty);
681
682 extern char * stream_ttyname (stream *);
683 internal_proto(stream_ttyname);
684
685 extern gfc_offset stream_offset (stream *s);
686 internal_proto(stream_offset);
687
688 extern int unpack_filename (char *, const char *, int);
689 internal_proto(unpack_filename);
690
691 /* unit.c */
692
693 /* Maximum file offset, computed at library initialization time.  */
694 extern gfc_offset max_offset;
695 internal_proto(max_offset);
696
697 /* Unit tree root.  */
698 extern gfc_unit *unit_root;
699 internal_proto(unit_root);
700
701 extern __gthread_mutex_t unit_lock;
702 internal_proto(unit_lock);
703
704 extern int close_unit (gfc_unit *);
705 internal_proto(close_unit);
706
707 extern gfc_unit *get_internal_unit (st_parameter_dt *);
708 internal_proto(get_internal_unit);
709
710 extern void free_internal_unit (st_parameter_dt *);
711 internal_proto(free_internal_unit);
712
713 extern int is_internal_unit (st_parameter_dt *);
714 internal_proto(is_internal_unit);
715
716 extern int is_array_io (st_parameter_dt *);
717 internal_proto(is_array_io);
718
719 extern int is_stream_io (st_parameter_dt *);
720 internal_proto(is_stream_io);
721
722 extern gfc_unit *find_unit (int);
723 internal_proto(find_unit);
724
725 extern gfc_unit *find_or_create_unit (int);
726 internal_proto(find_or_create_unit);
727
728 extern gfc_unit *get_unit (st_parameter_dt *, int);
729 internal_proto(get_unit);
730
731 extern void unlock_unit (gfc_unit *);
732 internal_proto(unlock_unit);
733
734 /* open.c */
735
736 extern void test_endfile (gfc_unit *);
737 internal_proto(test_endfile);
738
739 extern gfc_unit *new_unit (st_parameter_open *, gfc_unit *, unit_flags *);
740 internal_proto(new_unit);
741
742 /* format.c */
743
744 extern void parse_format (st_parameter_dt *);
745 internal_proto(parse_format);
746
747 extern const fnode *next_format (st_parameter_dt *);
748 internal_proto(next_format);
749
750 extern void unget_format (st_parameter_dt *, const fnode *);
751 internal_proto(unget_format);
752
753 extern void format_error (st_parameter_dt *, const fnode *, const char *);
754 internal_proto(format_error);
755
756 extern void free_format_data (st_parameter_dt *);
757 internal_proto(free_format_data);
758
759 /* transfer.c */
760
761 #define SCRATCH_SIZE 300
762
763 extern const char *type_name (bt);
764 internal_proto(type_name);
765
766 extern void *read_block (st_parameter_dt *, int *);
767 internal_proto(read_block);
768
769 extern char *read_sf (st_parameter_dt *, int *, int);
770 internal_proto(read_sf);
771
772 extern void *write_block (st_parameter_dt *, int);
773 internal_proto(write_block);
774
775 extern gfc_offset next_array_record (st_parameter_dt *, array_loop_spec *);
776 internal_proto(next_array_record);
777
778 extern gfc_offset init_loop_spec (gfc_array_char *, array_loop_spec *);
779 internal_proto(init_loop_spec);
780
781 extern void next_record (st_parameter_dt *, int);
782 internal_proto(next_record);
783
784 extern void reverse_memcpy (void *, const void *, size_t);
785 internal_proto (reverse_memcpy);
786
787 /* read.c */
788
789 extern void set_integer (void *, GFC_INTEGER_LARGEST, int);
790 internal_proto(set_integer);
791
792 extern GFC_UINTEGER_LARGEST max_value (int, int);
793 internal_proto(max_value);
794
795 extern int convert_real (st_parameter_dt *, void *, const char *, int);
796 internal_proto(convert_real);
797
798 extern void read_a (st_parameter_dt *, const fnode *, char *, int);
799 internal_proto(read_a);
800
801 extern void read_f (st_parameter_dt *, const fnode *, char *, int);
802 internal_proto(read_f);
803
804 extern void read_l (st_parameter_dt *, const fnode *, char *, int);
805 internal_proto(read_l);
806
807 extern void read_x (st_parameter_dt *, int);
808 internal_proto(read_x);
809
810 extern void read_radix (st_parameter_dt *, const fnode *, char *, int, int);
811 internal_proto(read_radix);
812
813 extern void read_decimal (st_parameter_dt *, const fnode *, char *, int);
814 internal_proto(read_decimal);
815
816 /* list_read.c */
817
818 extern void list_formatted_read (st_parameter_dt *, bt, void *, int, size_t,
819                                  size_t);
820 internal_proto(list_formatted_read);
821
822 extern void finish_list_read (st_parameter_dt *);
823 internal_proto(finish_list_read);
824
825 extern void namelist_read (st_parameter_dt *);
826 internal_proto(namelist_read);
827
828 extern void namelist_write (st_parameter_dt *);
829 internal_proto(namelist_write);
830
831 /* write.c */
832
833 extern void write_a (st_parameter_dt *, const fnode *, const char *, int);
834 internal_proto(write_a);
835
836 extern void write_b (st_parameter_dt *, const fnode *, const char *, int);
837 internal_proto(write_b);
838
839 extern void write_d (st_parameter_dt *, const fnode *, const char *, int);
840 internal_proto(write_d);
841
842 extern void write_e (st_parameter_dt *, const fnode *, const char *, int);
843 internal_proto(write_e);
844
845 extern void write_en (st_parameter_dt *, const fnode *, const char *, int);
846 internal_proto(write_en);
847
848 extern void write_es (st_parameter_dt *, const fnode *, const char *, int);
849 internal_proto(write_es);
850
851 extern void write_f (st_parameter_dt *, const fnode *, const char *, int);
852 internal_proto(write_f);
853
854 extern void write_i (st_parameter_dt *, const fnode *, const char *, int);
855 internal_proto(write_i);
856
857 extern void write_l (st_parameter_dt *, const fnode *, char *, int);
858 internal_proto(write_l);
859
860 extern void write_o (st_parameter_dt *, const fnode *, const char *, int);
861 internal_proto(write_o);
862
863 extern void write_x (st_parameter_dt *, int, int);
864 internal_proto(write_x);
865
866 extern void write_z (st_parameter_dt *, const fnode *, const char *, int);
867 internal_proto(write_z);
868
869 extern void list_formatted_write (st_parameter_dt *, bt, void *, int, size_t,
870                                   size_t);
871 internal_proto(list_formatted_write);
872
873 /* error.c */
874 extern notification notification_std(int);
875 internal_proto(notification_std);
876
877 /* size_from_kind.c */
878 extern size_t size_from_real_kind (int);
879 internal_proto(size_from_real_kind);
880
881 extern size_t size_from_complex_kind (int);
882 internal_proto(size_from_complex_kind);
883
884 /* lock.c */
885 extern void free_ionml (st_parameter_dt *);
886 internal_proto(free_ionml);
887
888 static inline void
889 inc_waiting_locked (gfc_unit *u)
890 {
891 #ifdef HAVE_SYNC_FETCH_AND_ADD
892   (void) __sync_fetch_and_add (&u->waiting, 1);
893 #else
894   u->waiting++;
895 #endif
896 }
897
898 static inline int
899 predec_waiting_locked (gfc_unit *u)
900 {
901 #ifdef HAVE_SYNC_FETCH_AND_ADD
902   return __sync_add_and_fetch (&u->waiting, -1);
903 #else
904   return --u->waiting;
905 #endif
906 }
907
908 static inline void
909 dec_waiting_unlocked (gfc_unit *u)
910 {
911 #ifdef HAVE_SYNC_FETCH_AND_ADD
912   (void) __sync_fetch_and_add (&u->waiting, -1);
913 #else
914   __gthread_mutex_lock (&unit_lock);
915   u->waiting--;
916   __gthread_mutex_unlock (&unit_lock);
917 #endif
918 }
919
920 #endif
921
922 /* ../runtime/environ.c  This is here because we return unit_convert.  */
923
924 unit_convert get_unformatted_convert (int);
925 internal_proto(get_unformatted_convert);