OSDN Git Service

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