* config/fpu-387.h (set_fpu): Add "=m" for stmxcsr.
[official-gcc.git] / libgfortran / io / io.h
blob3cb98f42ede6428ba40a76788c441b3787ffebd1
1 /* Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
4 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6 Libgfortran is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 Libgfortran is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Libgfortran; see the file COPYING. If not, write to
18 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
21 /* As a special exception, if you link this library with other files,
22 some of which are compiled with GCC, to produce an executable,
23 this library does not by itself cause the resulting executable
24 to be covered by the GNU General Public License.
25 This exception does not however invalidate any other reasons why
26 the executable file might be covered by the GNU General Public License. */
28 #ifndef GFOR_IO_H
29 #define GFOR_IO_H
31 /* IO library include. */
33 #include <setjmp.h>
34 #include "libgfortran.h"
36 #define DEFAULT_TEMPDIR "/tmp"
38 /* Basic types used in data transfers. */
40 typedef enum
41 { BT_NULL, BT_INTEGER, BT_LOGICAL, BT_CHARACTER, BT_REAL,
42 BT_COMPLEX
44 bt;
47 typedef enum
48 { SUCCESS = 1, FAILURE }
49 try;
51 typedef struct stream
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 *);
62 stream;
65 /* Macros for doing file I/O given a stream. */
67 #define sfree(s) ((s)->sfree)(s)
68 #define sclose(s) ((s)->close)(s)
70 #define salloc_r(s, len) ((s)->alloc_r_at)(s, len, -1)
71 #define salloc_w(s, len) ((s)->alloc_w_at)(s, len, -1)
73 #define salloc_r_at(s, len, where) ((s)->alloc_r_at)(s, len, where)
74 #define salloc_w_at(s, len, where) ((s)->alloc_w_at)(s, len, where)
76 #define sseek(s, pos) ((s)->seek)(s, pos)
77 #define struncate(s) ((s)->truncate)(s)
78 #define sread(s, buf, nbytes) ((s)->read)(s, buf, nbytes)
79 #define swrite(s, buf, nbytes) ((s)->write)(s, buf, nbytes)
81 /* Representation of a namelist object in libgfortran
83 Namelist Records
84 &GROUPNAME OBJECT=value[s] [,OBJECT=value[s]].../
86 &GROUPNAME OBJECT=value[s] [,OBJECT=value[s]]...&END
88 The object can be a fully qualified, compound name for an instrinsic
89 type, derived types or derived type components. So, a substring
90 a(:)%b(4)%ch(2:4)(1:7) has to be treated correctly in namelist
91 read. Hence full information about the structure of the object has
92 to be available to list_read.c and write.
94 These requirements are met by the following data structures.
96 nml_loop_spec contains the variables for the loops over index ranges
97 that are encountered. Since the variables can be negative, ssize_t
98 is used. */
100 typedef struct nml_loop_spec
103 /* Index counter for this dimension. */
104 ssize_t idx;
106 /* Start for the index counter. */
107 ssize_t start;
109 /* End for the index counter. */
110 ssize_t end;
112 /* Step for the index counter. */
113 ssize_t step;
115 nml_loop_spec;
117 /* namelist_info type contains all the scalar information about the
118 object and arrays of descriptor_dimension and nml_loop_spec types for
119 arrays. */
121 typedef struct namelist_type
124 /* Object type, stored as GFC_DTYPE_xxxx. */
125 bt type;
127 /* Object name. */
128 char * var_name;
130 /* Address for the start of the object's data. */
131 void * mem_pos;
133 /* Flag to show that a read is to be attempted for this node. */
134 int touched;
136 /* Length of intrinsic type in bytes. */
137 int len;
139 /* Rank of the object. */
140 int var_rank;
142 /* Overall size of the object in bytes. */
143 index_type size;
145 /* Length of character string. */
146 index_type string_length;
148 descriptor_dimension * dim;
149 nml_loop_spec * ls;
150 struct namelist_type * next;
152 namelist_info;
154 /* Options for the OPEN statement. */
156 typedef enum
157 { ACCESS_SEQUENTIAL, ACCESS_DIRECT,
158 ACCESS_UNSPECIFIED
160 unit_access;
162 typedef enum
163 { ACTION_READ, ACTION_WRITE, ACTION_READWRITE,
164 ACTION_UNSPECIFIED
166 unit_action;
168 typedef enum
169 { BLANK_NULL, BLANK_ZERO, BLANK_UNSPECIFIED }
170 unit_blank;
172 typedef enum
173 { DELIM_NONE, DELIM_APOSTROPHE, DELIM_QUOTE,
174 DELIM_UNSPECIFIED
176 unit_delim;
178 typedef enum
179 { FORM_FORMATTED, FORM_UNFORMATTED, FORM_UNSPECIFIED }
180 unit_form;
182 typedef enum
183 { POSITION_ASIS, POSITION_REWIND, POSITION_APPEND,
184 POSITION_UNSPECIFIED
186 unit_position;
188 typedef enum
189 { STATUS_UNKNOWN, STATUS_OLD, STATUS_NEW, STATUS_SCRATCH,
190 STATUS_REPLACE, STATUS_UNSPECIFIED
192 unit_status;
194 typedef enum
195 { PAD_YES, PAD_NO, PAD_UNSPECIFIED }
196 unit_pad;
198 typedef enum
199 { ADVANCE_YES, ADVANCE_NO, ADVANCE_UNSPECIFIED }
200 unit_advance;
202 typedef enum
203 {READING, WRITING}
204 unit_mode;
206 /* Statement parameters. These are all the things that can appear in
207 an I/O statement. Some are inputs and some are outputs, but none
208 are both. All of these values are initially zeroed and are zeroed
209 at the end of a library statement. The relevant values need to be
210 set before entry to an I/O statement. This structure needs to be
211 duplicated by the back end. */
213 typedef struct
215 GFC_INTEGER_4 unit;
216 GFC_INTEGER_4 err, end, eor, list_format; /* These are flags, not values. */
218 /* Return values from library statements. These are returned only if
219 the labels are specified in the statement itself and the condition
220 occurs. In most cases, none of the labels are specified and the
221 return value does not have to be checked. Must be consistent with
222 the front end. */
224 enum
226 LIBRARY_OK = 0,
227 LIBRARY_ERROR,
228 LIBRARY_END,
229 LIBRARY_EOR
231 library_return;
233 GFC_INTEGER_4 *iostat, *exist, *opened, *number, *named;
234 GFC_INTEGER_4 rec;
235 GFC_INTEGER_4 *nextrec, *size;
237 GFC_INTEGER_4 recl_in;
238 GFC_INTEGER_4 *recl_out;
240 GFC_INTEGER_4 *iolength;
242 #define CHARACTER(name) \
243 char * name; \
244 gfc_charlen_type name ## _len
245 CHARACTER (file);
246 CHARACTER (status);
247 CHARACTER (access);
248 CHARACTER (form);
249 CHARACTER (blank);
250 CHARACTER (position);
251 CHARACTER (action);
252 CHARACTER (delim);
253 CHARACTER (pad);
254 CHARACTER (format);
255 CHARACTER (advance);
256 CHARACTER (name);
257 CHARACTER (internal_unit);
258 gfc_array_char *internal_unit_desc;
259 CHARACTER (sequential);
260 CHARACTER (direct);
261 CHARACTER (formatted);
262 CHARACTER (unformatted);
263 CHARACTER (read);
264 CHARACTER (write);
265 CHARACTER (readwrite);
267 /* namelist related data */
268 CHARACTER (namelist_name);
269 GFC_INTEGER_4 namelist_read_mode;
271 /* iomsg */
272 CHARACTER (iomsg);
274 #undef CHARACTER
276 st_parameter;
278 extern st_parameter ioparm;
279 iexport_data_proto(ioparm);
281 extern namelist_info * ionml;
282 internal_proto(ionml);
284 typedef struct
286 unit_access access;
287 unit_action action;
288 unit_blank blank;
289 unit_delim delim;
290 unit_form form;
291 int is_notpadded;
292 unit_position position;
293 unit_status status;
294 unit_pad pad;
296 unit_flags;
299 /* The default value of record length for preconnected units is defined
300 here. This value can be overriden by an environment variable.
301 Default value is 1 Gb. */
303 #define DEFAULT_RECL 1073741824
306 typedef struct gfc_unit
308 int unit_number;
310 stream *s;
312 struct gfc_unit *left, *right; /* Treap links. */
313 int priority;
315 int read_bad, current_record;
316 enum
317 { NO_ENDFILE, AT_ENDFILE, AFTER_ENDFILE }
318 endfile;
320 unit_mode mode;
321 unit_flags flags;
322 gfc_offset recl, last_record, maxrec, bytes_left;
324 /* recl -- Record length of the file.
325 last_record -- Last record number read or written
326 maxrec -- Maximum record number in a direct access file
327 bytes_left -- Bytes left in current record. */
329 int file_len;
330 char file[1]; /* Filename is allocated at the end of the structure. */
332 gfc_unit;
334 /* Global variables. Putting these in a structure makes it easier to
335 maintain, particularly with the constraint of a prefix. */
337 typedef struct
339 int in_library; /* Nonzero if a library call is being processed. */
340 int size; /* Bytes processed by the current data-transfer statement. */
341 gfc_offset max_offset; /* Maximum file offset. */
342 int item_count; /* Item number in a formatted data transfer. */
343 int reversion_flag; /* Format reversion has occurred. */
344 int first_item;
346 gfc_unit *unit_root;
347 int seen_dollar;
349 unit_mode mode;
351 unit_blank blank_status;
352 enum {SIGN_S, SIGN_SS, SIGN_SP} sign_status;
353 int scale_factor;
354 jmp_buf eof_jump;
356 global_t;
358 extern global_t g;
359 internal_proto(g);
361 extern gfc_unit *current_unit;
362 internal_proto(current_unit);
364 /* Format tokens. Only about half of these can be stored in the
365 format nodes. */
367 typedef enum
369 FMT_NONE = 0, FMT_UNKNOWN, FMT_SIGNED_INT, FMT_ZERO, FMT_POSINT, FMT_PERIOD,
370 FMT_COMMA, FMT_COLON, FMT_SLASH, FMT_DOLLAR, FMT_T, FMT_TR, FMT_TL,
371 FMT_LPAREN, FMT_RPAREN, FMT_X, FMT_S, FMT_SS, FMT_SP, FMT_STRING,
372 FMT_BADSTRING, FMT_P, FMT_I, FMT_B, FMT_BN, FMT_BZ, FMT_O, FMT_Z, FMT_F,
373 FMT_E, FMT_EN, FMT_ES, FMT_G, FMT_L, FMT_A, FMT_D, FMT_H, FMT_END
375 format_token;
378 /* Format nodes. A format string is converted into a tree of these
379 structures, which is traversed as part of a data transfer statement. */
381 typedef struct fnode
383 format_token format;
384 int repeat;
385 struct fnode *next;
386 char *source;
388 union
390 struct
392 int w, d, e;
394 real;
396 struct
398 int length;
399 char *p;
401 string;
403 struct
405 int w, m;
407 integer;
409 int w;
410 int k;
411 int r;
412 int n;
414 struct fnode *child;
418 /* Members for traversing the tree during data transfer. */
420 int count;
421 struct fnode *current;
424 fnode;
427 /* unix.c */
429 extern int move_pos_offset (stream *, int);
430 internal_proto(move_pos_offset);
432 extern int compare_files (stream *, stream *);
433 internal_proto(compare_files);
435 extern stream *init_error_stream (void);
436 internal_proto(init_error_stream);
438 extern stream *open_external (unit_flags *);
439 internal_proto(open_external);
441 extern stream *open_internal (char *, int);
442 internal_proto(open_internal);
444 extern stream *input_stream (void);
445 internal_proto(input_stream);
447 extern stream *output_stream (void);
448 internal_proto(output_stream);
450 extern stream *error_stream (void);
451 internal_proto(error_stream);
453 extern int compare_file_filename (stream *, const char *, int);
454 internal_proto(compare_file_filename);
456 extern gfc_unit *find_file (void);
457 internal_proto(find_file);
459 extern int stream_at_bof (stream *);
460 internal_proto(stream_at_bof);
462 extern int stream_at_eof (stream *);
463 internal_proto(stream_at_eof);
465 extern int delete_file (gfc_unit *);
466 internal_proto(delete_file);
468 extern int file_exists (void);
469 internal_proto(file_exists);
471 extern const char *inquire_sequential (const char *, int);
472 internal_proto(inquire_sequential);
474 extern const char *inquire_direct (const char *, int);
475 internal_proto(inquire_direct);
477 extern const char *inquire_formatted (const char *, int);
478 internal_proto(inquire_formatted);
480 extern const char *inquire_unformatted (const char *, int);
481 internal_proto(inquire_unformatted);
483 extern const char *inquire_read (const char *, int);
484 internal_proto(inquire_read);
486 extern const char *inquire_write (const char *, int);
487 internal_proto(inquire_write);
489 extern const char *inquire_readwrite (const char *, int);
490 internal_proto(inquire_readwrite);
492 extern gfc_offset file_length (stream *);
493 internal_proto(file_length);
495 extern gfc_offset file_position (stream *);
496 internal_proto(file_position);
498 extern int is_seekable (stream *);
499 internal_proto(is_seekable);
501 extern int is_preconnected (stream *);
502 internal_proto(is_preconnected);
504 extern void empty_internal_buffer(stream *);
505 internal_proto(empty_internal_buffer);
507 extern try flush (stream *);
508 internal_proto(flush);
510 extern int stream_isatty (stream *);
511 internal_proto(stream_isatty);
513 extern char * stream_ttyname (stream *);
514 internal_proto(stream_ttyname);
516 extern int unit_to_fd (int);
517 internal_proto(unit_to_fd);
519 extern int unpack_filename (char *, const char *, int);
520 internal_proto(unpack_filename);
522 /* unit.c */
524 extern void insert_unit (gfc_unit *);
525 internal_proto(insert_unit);
527 extern int close_unit (gfc_unit *);
528 internal_proto(close_unit);
530 extern int is_internal_unit (void);
531 internal_proto(is_internal_unit);
533 extern int is_array_io (void);
534 internal_proto(is_array_io);
536 extern gfc_offset get_array_unit_len (gfc_array_char *);
537 internal_proto(get_array_unit_len);
539 extern gfc_unit *find_unit (int);
540 internal_proto(find_unit);
542 extern gfc_unit *get_unit (int);
543 internal_proto(get_unit);
545 /* open.c */
547 extern void test_endfile (gfc_unit *);
548 internal_proto(test_endfile);
550 extern void new_unit (unit_flags *);
551 internal_proto(new_unit);
553 /* format.c */
555 extern void parse_format (void);
556 internal_proto(parse_format);
558 extern fnode *next_format (void);
559 internal_proto(next_format);
561 extern void unget_format (fnode *);
562 internal_proto(unget_format);
564 extern void format_error (fnode *, const char *);
565 internal_proto(format_error);
567 extern void free_fnodes (void);
568 internal_proto(free_fnodes);
570 /* transfer.c */
572 #define SCRATCH_SIZE 300
574 extern char scratch[];
575 internal_proto(scratch);
577 extern const char *type_name (bt);
578 internal_proto(type_name);
580 extern void *read_block (int *);
581 internal_proto(read_block);
583 extern void *write_block (int);
584 internal_proto(write_block);
586 extern void next_record (int);
587 internal_proto(next_record);
589 /* read.c */
591 extern void set_integer (void *, GFC_INTEGER_LARGEST, int);
592 internal_proto(set_integer);
594 extern GFC_UINTEGER_LARGEST max_value (int, int);
595 internal_proto(max_value);
597 extern int convert_real (void *, const char *, int);
598 internal_proto(convert_real);
600 extern void read_a (fnode *, char *, int);
601 internal_proto(read_a);
603 extern void read_f (fnode *, char *, int);
604 internal_proto(read_f);
606 extern void read_l (fnode *, char *, int);
607 internal_proto(read_l);
609 extern void read_x (int);
610 internal_proto(read_x);
612 extern void read_radix (fnode *, char *, int, int);
613 internal_proto(read_radix);
615 extern void read_decimal (fnode *, char *, int);
616 internal_proto(read_decimal);
618 /* list_read.c */
620 extern void list_formatted_read (bt, void *, int, size_t);
621 internal_proto(list_formatted_read);
623 extern void finish_list_read (void);
624 internal_proto(finish_list_read);
626 extern void init_at_eol (void);
627 internal_proto(init_at_eol);
629 extern void namelist_read (void);
630 internal_proto(namelist_read);
632 extern void namelist_write (void);
633 internal_proto(namelist_write);
635 /* write.c */
637 extern void write_a (fnode *, const char *, int);
638 internal_proto(write_a);
640 extern void write_b (fnode *, const char *, int);
641 internal_proto(write_b);
643 extern void write_d (fnode *, const char *, int);
644 internal_proto(write_d);
646 extern void write_e (fnode *, const char *, int);
647 internal_proto(write_e);
649 extern void write_en (fnode *, const char *, int);
650 internal_proto(write_en);
652 extern void write_es (fnode *, const char *, int);
653 internal_proto(write_es);
655 extern void write_f (fnode *, const char *, int);
656 internal_proto(write_f);
658 extern void write_i (fnode *, const char *, int);
659 internal_proto(write_i);
661 extern void write_l (fnode *, char *, int);
662 internal_proto(write_l);
664 extern void write_o (fnode *, const char *, int);
665 internal_proto(write_o);
667 extern void write_x (int, int);
668 internal_proto(write_x);
670 extern void write_z (fnode *, const char *, int);
671 internal_proto(write_z);
673 extern void list_formatted_write (bt, void *, int, size_t);
674 internal_proto(list_formatted_write);
676 /* error.c */
677 extern try notify_std (int, const char *);
678 internal_proto(notify_std);
680 #endif