OSDN Git Service

PR libfortran/23272
[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 #define DEFAULT_TEMPDIR "/tmp"
37
38 /* Basic types used in data transfers.  */
39
40 typedef enum
41 { BT_NULL, BT_INTEGER, BT_LOGICAL, BT_CHARACTER, BT_REAL,
42   BT_COMPLEX
43 }
44 bt;
45
46
47 typedef enum
48 { SUCCESS = 1, FAILURE }
49 try;
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 }
62 stream;
63
64
65 /* Macros for doing file I/O given a stream.  */
66
67 #define sfree(s) ((s)->sfree)(s)
68 #define sclose(s) ((s)->close)(s)
69
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)
72
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)
75
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)
80
81 /* Representation of a namelist object in libgfortran
82
83    Namelist Records
84       &GROUPNAME  OBJECT=value[s] [,OBJECT=value[s]].../
85      or
86       &GROUPNAME  OBJECT=value[s] [,OBJECT=value[s]]...&END
87
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.
93
94    These requirements are met by the following data structures.
95
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.  */
99
100 typedef struct nml_loop_spec
101 {
102
103   /* Index counter for this dimension.  */
104   ssize_t idx;
105
106   /* Start for the index counter.  */
107   ssize_t start;
108
109   /* End for the index counter.  */
110   ssize_t end;
111
112   /* Step for the index counter.  */
113   ssize_t step;
114 }
115 nml_loop_spec;
116
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.  */
120
121 typedef struct namelist_type
122 {
123
124   /* Object type, stored as GFC_DTYPE_xxxx.  */
125   bt type;
126
127   /* Object name.  */
128   char * var_name;
129
130   /* Address for the start of the object's data.  */
131   void * mem_pos;
132
133   /* Flag to show that a read is to be attempted for this node.  */
134   int touched;
135
136   /* Length of intrinsic type in bytes.  */
137   int len;
138
139   /* Rank of the object.  */
140   int var_rank;
141
142   /* Overall size of the object in bytes.  */
143   index_type size;
144
145   /* Length of character string.  */
146   index_type string_length;
147
148   descriptor_dimension * dim;
149   nml_loop_spec * ls;
150   struct namelist_type * next;
151 }
152 namelist_info;
153
154 /* Options for the OPEN statement.  */
155
156 typedef enum
157 { ACCESS_SEQUENTIAL, ACCESS_DIRECT,
158   ACCESS_UNSPECIFIED
159 }
160 unit_access;
161
162 typedef enum
163 { ACTION_READ, ACTION_WRITE, ACTION_READWRITE,
164   ACTION_UNSPECIFIED
165 }
166 unit_action;
167
168 typedef enum
169 { BLANK_NULL, BLANK_ZERO, BLANK_UNSPECIFIED }
170 unit_blank;
171
172 typedef enum
173 { DELIM_NONE, DELIM_APOSTROPHE, DELIM_QUOTE,
174   DELIM_UNSPECIFIED
175 }
176 unit_delim;
177
178 typedef enum
179 { FORM_FORMATTED, FORM_UNFORMATTED, FORM_UNSPECIFIED }
180 unit_form;
181
182 typedef enum
183 { POSITION_ASIS, POSITION_REWIND, POSITION_APPEND,
184   POSITION_UNSPECIFIED
185 }
186 unit_position;
187
188 typedef enum
189 { STATUS_UNKNOWN, STATUS_OLD, STATUS_NEW, STATUS_SCRATCH,
190   STATUS_REPLACE, STATUS_UNSPECIFIED
191 }
192 unit_status;
193
194 typedef enum
195 { PAD_YES, PAD_NO, PAD_UNSPECIFIED }
196 unit_pad;
197
198 typedef enum
199 { ADVANCE_YES, ADVANCE_NO, ADVANCE_UNSPECIFIED }
200 unit_advance;
201
202 typedef enum
203 {READING, WRITING}
204 unit_mode;
205
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.  */
212
213 typedef struct
214 {
215   GFC_INTEGER_4 unit;
216   GFC_INTEGER_4 err, end, eor, list_format; /* These are flags, not values.  */
217
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.  */
223
224   enum
225   {
226     LIBRARY_OK = 0,
227     LIBRARY_ERROR,
228     LIBRARY_END,
229     LIBRARY_EOR
230   }
231   library_return;
232
233   GFC_INTEGER_4 *iostat, *exist, *opened, *number, *named;
234   GFC_INTEGER_4 rec;
235   GFC_INTEGER_4 *nextrec, *size;
236
237   GFC_INTEGER_4 recl_in;
238   GFC_INTEGER_4 *recl_out;
239
240   GFC_INTEGER_4 *iolength;
241
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);
266
267 /* namelist related data */
268   CHARACTER (namelist_name);
269   GFC_INTEGER_4 namelist_read_mode;
270
271   /* iomsg */
272   CHARACTER (iomsg);
273
274 #undef CHARACTER
275 }
276 st_parameter;
277
278 extern st_parameter ioparm;
279 iexport_data_proto(ioparm);
280
281 extern namelist_info * ionml;
282 internal_proto(ionml);
283
284 typedef struct
285 {
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;
295 }
296 unit_flags;
297
298
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.  */
302
303 #define DEFAULT_RECL 1073741824
304
305
306 typedef struct gfc_unit
307 {
308   int unit_number;
309
310   stream *s;
311
312   struct gfc_unit *left, *right;        /* Treap links.  */
313   int priority;
314
315   int read_bad, current_record;
316   enum
317   { NO_ENDFILE, AT_ENDFILE, AFTER_ENDFILE }
318   endfile;
319
320   unit_mode  mode;
321   unit_flags flags;
322   gfc_offset recl, last_record, maxrec, bytes_left;
323
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.  */
328
329   int file_len;
330   char file[1];       /* Filename is allocated at the end of the structure.  */
331 }
332 gfc_unit;
333
334 /* Global variables.  Putting these in a structure makes it easier to
335    maintain, particularly with the constraint of a prefix.  */
336
337 typedef struct
338 {
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;
345
346   gfc_unit *unit_root;
347   int seen_dollar;
348
349   unit_mode  mode;
350
351   unit_blank blank_status;
352   enum {SIGN_S, SIGN_SS, SIGN_SP} sign_status;
353   int scale_factor;
354   jmp_buf eof_jump;
355 }
356 global_t;
357
358 extern global_t g;
359 internal_proto(g);
360
361 extern gfc_unit *current_unit;
362 internal_proto(current_unit);
363
364 /* Format tokens.  Only about half of these can be stored in the
365    format nodes.  */
366
367 typedef enum
368 {
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
374 }
375 format_token;
376
377
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.  */
380
381 typedef struct fnode
382 {
383   format_token format;
384   int repeat;
385   struct fnode *next;
386   char *source;
387
388   union
389   {
390     struct
391     {
392       int w, d, e;
393     }
394     real;
395
396     struct
397     {
398       int length;
399       char *p;
400     }
401     string;
402
403     struct
404     {
405       int w, m;
406     }
407     integer;
408
409     int w;
410     int k;
411     int r;
412     int n;
413
414     struct fnode *child;
415   }
416   u;
417
418   /* Members for traversing the tree during data transfer.  */
419
420   int count;
421   struct fnode *current;
422
423 }
424 fnode;
425
426
427 /* unix.c */
428
429 extern int move_pos_offset (stream *, int);
430 internal_proto(move_pos_offset);
431
432 extern int compare_files (stream *, stream *);
433 internal_proto(compare_files);
434
435 extern stream *init_error_stream (void);
436 internal_proto(init_error_stream);
437
438 extern stream *open_external (unit_flags *);
439 internal_proto(open_external);
440
441 extern stream *open_internal (char *, int);
442 internal_proto(open_internal);
443
444 extern stream *input_stream (void);
445 internal_proto(input_stream);
446
447 extern stream *output_stream (void);
448 internal_proto(output_stream);
449
450 extern stream *error_stream (void);
451 internal_proto(error_stream);
452
453 extern int compare_file_filename (gfc_unit *, const char *, int);
454 internal_proto(compare_file_filename);
455
456 extern gfc_unit *find_file (void);
457 internal_proto(find_file);
458
459 extern int stream_at_bof (stream *);
460 internal_proto(stream_at_bof);
461
462 extern int stream_at_eof (stream *);
463 internal_proto(stream_at_eof);
464
465 extern int delete_file (gfc_unit *);
466 internal_proto(delete_file);
467
468 extern int file_exists (void);
469 internal_proto(file_exists);
470
471 extern const char *inquire_sequential (const char *, int);
472 internal_proto(inquire_sequential);
473
474 extern const char *inquire_direct (const char *, int);
475 internal_proto(inquire_direct);
476
477 extern const char *inquire_formatted (const char *, int);
478 internal_proto(inquire_formatted);
479
480 extern const char *inquire_unformatted (const char *, int);
481 internal_proto(inquire_unformatted);
482
483 extern const char *inquire_read (const char *, int);
484 internal_proto(inquire_read);
485
486 extern const char *inquire_write (const char *, int);
487 internal_proto(inquire_write);
488
489 extern const char *inquire_readwrite (const char *, int);
490 internal_proto(inquire_readwrite);
491
492 extern gfc_offset file_length (stream *);
493 internal_proto(file_length);
494
495 extern gfc_offset file_position (stream *);
496 internal_proto(file_position);
497
498 extern int is_seekable (stream *);
499 internal_proto(is_seekable);
500
501 extern int is_preconnected (stream *);
502 internal_proto(is_preconnected);
503
504 extern void empty_internal_buffer(stream *);
505 internal_proto(empty_internal_buffer);
506
507 extern try flush (stream *);
508 internal_proto(flush);
509
510 extern int stream_isatty (stream *);
511 internal_proto(stream_isatty);
512
513 extern char * stream_ttyname (stream *);
514 internal_proto(stream_ttyname);
515
516 extern int unit_to_fd (int);
517 internal_proto(unit_to_fd);
518
519 extern int unpack_filename (char *, const char *, int);
520 internal_proto(unpack_filename);
521
522 /* unit.c */
523
524 extern void insert_unit (gfc_unit *);
525 internal_proto(insert_unit);
526
527 extern int close_unit (gfc_unit *);
528 internal_proto(close_unit);
529
530 extern int is_internal_unit (void);
531 internal_proto(is_internal_unit);
532
533 extern int is_array_io (void);
534 internal_proto(is_array_io);
535
536 extern gfc_offset get_array_unit_len (gfc_array_char *);
537 internal_proto(get_array_unit_len);
538
539 extern gfc_unit *find_unit (int);
540 internal_proto(find_unit);
541
542 extern gfc_unit *get_unit (int);
543 internal_proto(get_unit);
544
545 /* open.c */
546
547 extern void test_endfile (gfc_unit *);
548 internal_proto(test_endfile);
549
550 extern void new_unit (unit_flags *);
551 internal_proto(new_unit);
552
553 /* format.c */
554
555 extern void parse_format (void);
556 internal_proto(parse_format);
557
558 extern fnode *next_format (void);
559 internal_proto(next_format);
560
561 extern void unget_format (fnode *);
562 internal_proto(unget_format);
563
564 extern void format_error (fnode *, const char *);
565 internal_proto(format_error);
566
567 extern void free_fnodes (void);
568 internal_proto(free_fnodes);
569
570 /* transfer.c */
571
572 #define SCRATCH_SIZE 300
573
574 extern char scratch[];
575 internal_proto(scratch);
576
577 extern const char *type_name (bt);
578 internal_proto(type_name);
579
580 extern void *read_block (int *);
581 internal_proto(read_block);
582
583 extern void *write_block (int);
584 internal_proto(write_block);
585
586 extern void next_record (int);
587 internal_proto(next_record);
588
589 /* read.c */
590
591 extern void set_integer (void *, GFC_INTEGER_LARGEST, int);
592 internal_proto(set_integer);
593
594 extern GFC_UINTEGER_LARGEST max_value (int, int);
595 internal_proto(max_value);
596
597 extern int convert_real (void *, const char *, int);
598 internal_proto(convert_real);
599
600 extern void read_a (fnode *, char *, int);
601 internal_proto(read_a);
602
603 extern void read_f (fnode *, char *, int);
604 internal_proto(read_f);
605
606 extern void read_l (fnode *, char *, int);
607 internal_proto(read_l);
608
609 extern void read_x (int);
610 internal_proto(read_x);
611
612 extern void read_radix (fnode *, char *, int, int);
613 internal_proto(read_radix);
614
615 extern void read_decimal (fnode *, char *, int);
616 internal_proto(read_decimal);
617
618 /* list_read.c */
619
620 extern void list_formatted_read (bt, void *, int, size_t);
621 internal_proto(list_formatted_read);
622
623 extern void finish_list_read (void);
624 internal_proto(finish_list_read);
625
626 extern void init_at_eol (void);
627 internal_proto(init_at_eol);
628
629 extern void namelist_read (void);
630 internal_proto(namelist_read);
631
632 extern void namelist_write (void);
633 internal_proto(namelist_write);
634
635 /* write.c */
636
637 extern void write_a (fnode *, const char *, int);
638 internal_proto(write_a);
639
640 extern void write_b (fnode *, const char *, int);
641 internal_proto(write_b);
642
643 extern void write_d (fnode *, const char *, int);
644 internal_proto(write_d);
645
646 extern void write_e (fnode *, const char *, int);
647 internal_proto(write_e);
648
649 extern void write_en (fnode *, const char *, int);
650 internal_proto(write_en);
651
652 extern void write_es (fnode *, const char *, int);
653 internal_proto(write_es);
654
655 extern void write_f (fnode *, const char *, int);
656 internal_proto(write_f);
657
658 extern void write_i (fnode *, const char *, int);
659 internal_proto(write_i);
660
661 extern void write_l (fnode *, char *, int);
662 internal_proto(write_l);
663
664 extern void write_o (fnode *, const char *, int);
665 internal_proto(write_o);
666
667 extern void write_x (int, int);
668 internal_proto(write_x);
669
670 extern void write_z (fnode *, const char *, int);
671 internal_proto(write_z);
672
673 extern void list_formatted_write (bt, void *, int, size_t);
674 internal_proto(list_formatted_write);
675
676 /* error.c */
677 extern try notify_std (int, const char *);
678 internal_proto(notify_std);
679
680 #endif