OSDN Git Service

gcc/fortran/
[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, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, 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 "/var/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 }
60 stream;
61
62
63 /* Macros for doing file I/O given a stream.  */
64
65 #define sfree(s) ((s)->sfree)(s)
66 #define sclose(s) ((s)->close)(s)
67
68 #define salloc_r(s, len) ((s)->alloc_r_at)(s, len, -1)
69 #define salloc_w(s, len) ((s)->alloc_w_at)(s, len, -1)
70
71 #define salloc_r_at(s, len, where) ((s)->alloc_r_at)(s, len, where)
72 #define salloc_w_at(s, len, where) ((s)->alloc_w_at)(s, len, where)
73
74 #define sseek(s, pos) ((s)->seek)(s, pos)
75 #define struncate(s) ((s)->truncate)(s)
76
77 /* Namelist represent object */
78 /*
79    Namelist Records
80        &groupname  object=value [,object=value].../
81      or
82        &groupname  object=value [,object=value]...&groupname
83
84   Even more complex, during the execution of a program containing a
85   namelist READ statement, you can specify a question mark character(?)
86   or a question mark character preceded by an equal sign(=?) to get
87   the information of the namelist group. By '?', the name of variables
88   in the namelist will be displayed, by '=?', the name and value of
89   variables will be displayed.
90
91   All these requirements need a new data structure to record all info
92   about the namelist.
93 */
94
95 typedef struct namelist_type
96 {
97   char * var_name;
98   void * mem_pos;
99   int  value_acquired;
100   int len;
101   int string_length;
102   bt type;
103   struct namelist_type * next;
104 }
105 namelist_info;
106
107 /* Options for the OPEN statement.  */
108
109 typedef enum
110 { ACCESS_SEQUENTIAL, ACCESS_DIRECT,
111   ACCESS_UNSPECIFIED
112 }
113 unit_access;
114
115 typedef enum
116 { ACTION_READ, ACTION_WRITE, ACTION_READWRITE,
117   ACTION_UNSPECIFIED
118 }
119 unit_action;
120
121 typedef enum
122 { BLANK_NULL, BLANK_ZERO, BLANK_UNSPECIFIED }
123 unit_blank;
124
125 typedef enum
126 { DELIM_NONE, DELIM_APOSTROPHE, DELIM_QUOTE,
127   DELIM_UNSPECIFIED
128 }
129 unit_delim;
130
131 typedef enum
132 { FORM_FORMATTED, FORM_UNFORMATTED, FORM_UNSPECIFIED }
133 unit_form;
134
135 typedef enum
136 { POSITION_ASIS, POSITION_REWIND, POSITION_APPEND,
137   POSITION_UNSPECIFIED
138 }
139 unit_position;
140
141 typedef enum
142 { STATUS_UNKNOWN, STATUS_OLD, STATUS_NEW, STATUS_SCRATCH,
143   STATUS_REPLACE, STATUS_UNSPECIFIED
144 }
145 unit_status;
146
147 typedef enum
148 { PAD_YES, PAD_NO, PAD_UNSPECIFIED }
149 unit_pad;
150
151 typedef enum
152 { ADVANCE_YES, ADVANCE_NO, ADVANCE_UNSPECIFIED }
153 unit_advance;
154
155 typedef enum
156 {READING, WRITING}
157 unit_mode;
158
159 /* Statement parameters.  These are all the things that can appear in
160    an I/O statement.  Some are inputs and some are outputs, but none
161    are both.  All of these values are initially zeroed and are zeroed
162    at the end of a library statement.  The relevant values need to be
163    set before entry to an I/O statement.  This structure needs to be
164    duplicated by the back end.  */
165
166 typedef struct
167 {
168   GFC_INTEGER_4 unit;
169   GFC_INTEGER_4 err, end, eor, list_format; /* These are flags, not values.  */
170
171 /* Return values from library statements.  These are returned only if
172    the labels are specified in the statement itself and the condition
173    occurs.  In most cases, none of the labels are specified and the
174    return value does not have to be checked.  Must be consistent with
175    the front end.  */
176
177   enum
178   {
179     LIBRARY_OK = 0,
180     LIBRARY_ERROR,
181     LIBRARY_END,
182     LIBRARY_EOR
183   }
184   library_return;
185
186   GFC_INTEGER_4 *iostat, *exist, *opened, *number, *named;
187   GFC_INTEGER_4 rec;
188   GFC_INTEGER_4 *nextrec, *size;
189
190   GFC_INTEGER_4 recl_in; 
191   GFC_INTEGER_4 *recl_out;
192
193   GFC_INTEGER_4 *iolength;
194
195 #define CHARACTER(name) \
196               char * name; \
197               gfc_charlen_type name ## _len
198   CHARACTER (file);
199   CHARACTER (status);
200   CHARACTER (access);
201   CHARACTER (form);
202   CHARACTER (blank);
203   CHARACTER (position);
204   CHARACTER (action);
205   CHARACTER (delim);
206   CHARACTER (pad);
207   CHARACTER (format);
208   CHARACTER (advance);
209   CHARACTER (name);
210   CHARACTER (internal_unit);
211   CHARACTER (sequential);
212   CHARACTER (direct);
213   CHARACTER (formatted);
214   CHARACTER (unformatted);
215   CHARACTER (read);
216   CHARACTER (write);
217   CHARACTER (readwrite);
218
219 /* namelist related data */
220   CHARACTER (namelist_name);
221   GFC_INTEGER_4 namelist_read_mode;
222
223 #undef CHARACTER
224 }
225 st_parameter;
226
227 extern st_parameter ioparm;
228 iexport_data_proto(ioparm);
229
230 extern namelist_info * ionml;
231 internal_proto(ionml);
232
233 typedef struct
234 {
235   unit_access access;
236   unit_action action;
237   unit_blank blank;
238   unit_delim delim;
239   unit_form form;
240   int is_notpadded;
241   unit_position position;
242   unit_status status;
243   unit_pad pad;
244 }
245 unit_flags;
246
247
248 /* The default value of record length is defined here.  This value can
249    be overriden by the OPEN statement or by an environment variable.  */
250
251 #define DEFAULT_RECL 10000
252
253
254 typedef struct gfc_unit
255 {
256   int unit_number;
257
258   stream *s;
259
260   struct gfc_unit *left, *right;        /* Treap links.  */
261   int priority;
262
263   int read_bad, current_record;
264   enum
265   { NO_ENDFILE, AT_ENDFILE, AFTER_ENDFILE }
266   endfile;
267
268   unit_mode  mode;
269   unit_flags flags;
270   gfc_offset recl, last_record, maxrec, bytes_left;
271
272   /* recl           -- Record length of the file.
273      last_record    -- Last record number read or written
274      maxrec         -- Maximum record number in a direct access file
275      bytes_left     -- Bytes left in current record.  */
276
277   int file_len;
278   char file[1];       /* Filename is allocated at the end of the structure.  */
279 }
280 gfc_unit;
281
282 /* Global variables.  Putting these in a structure makes it easier to
283    maintain, particularly with the constraint of a prefix.  */
284
285 typedef struct
286 {
287   int in_library;       /* Nonzero if a library call is being processed.  */
288   int size;     /* Bytes processed by the current data-transfer statement.  */
289   gfc_offset max_offset;        /* Maximum file offset.  */
290   int item_count;       /* Item number in a formatted data transfer.  */
291   int reversion_flag;   /* Format reversion has occurred.  */
292   int first_item;
293
294   gfc_unit *unit_root;
295   int seen_dollar;
296
297   unit_mode  mode;
298
299   unit_blank blank_status;
300   enum {SIGN_S, SIGN_SS, SIGN_SP} sign_status;
301   int scale_factor;
302   jmp_buf eof_jump;  
303 }
304 global_t;
305
306 extern global_t g;
307 internal_proto(g);
308
309 extern gfc_unit *current_unit;
310 internal_proto(current_unit);
311
312 /* Format tokens.  Only about half of these can be stored in the
313    format nodes.  */
314
315 typedef enum
316 {
317   FMT_NONE = 0, FMT_UNKNOWN, FMT_SIGNED_INT, FMT_ZERO, FMT_POSINT, FMT_PERIOD,
318   FMT_COMMA, FMT_COLON, FMT_SLASH, FMT_DOLLAR, FMT_T, FMT_TR, FMT_TL,
319   FMT_LPAREN, FMT_RPAREN, FMT_X, FMT_S, FMT_SS, FMT_SP, FMT_STRING,
320   FMT_BADSTRING, FMT_P, FMT_I, FMT_B, FMT_BN, FMT_BZ, FMT_O, FMT_Z, FMT_F,
321   FMT_E, FMT_EN, FMT_ES, FMT_G, FMT_L, FMT_A, FMT_D, FMT_H, FMT_END
322 }
323 format_token;
324
325
326 /* Format nodes.  A format string is converted into a tree of these
327    structures, which is traversed as part of a data transfer statement.  */
328
329 typedef struct fnode
330 {
331   format_token format;
332   int repeat;
333   struct fnode *next;
334   char *source;
335
336   union
337   {
338     struct
339     {
340       int w, d, e;
341     }
342     real;
343
344     struct
345     {
346       int length;
347       char *p;
348     }
349     string;
350
351     struct
352     {
353       int w, m;
354     }
355     integer;
356
357     int w;
358     int k;
359     int r;
360     int n;
361
362     struct fnode *child;
363   }
364   u;
365
366   /* Members for traversing the tree during data transfer.  */
367
368   int count;
369   struct fnode *current;
370
371 }
372 fnode;
373
374
375 /* unix.c */
376
377 extern int move_pos_offset (stream *, int);
378 internal_proto(move_pos_offset);
379
380 extern int compare_files (stream *, stream *);
381 internal_proto(compare_files);
382
383 extern stream *init_error_stream (void);
384 internal_proto(init_error_stream);
385
386 extern stream *open_external (unit_flags *);
387 internal_proto(open_external);
388
389 extern stream *open_internal (char *, int);
390 internal_proto(open_internal);
391
392 extern stream *input_stream (void);
393 internal_proto(input_stream);
394
395 extern stream *output_stream (void);
396 internal_proto(output_stream);
397
398 extern int compare_file_filename (stream *, const char *, int);
399 internal_proto(compare_file_filename);
400
401 extern gfc_unit *find_file (void);
402 internal_proto(find_file);
403
404 extern int stream_at_bof (stream *);
405 internal_proto(stream_at_bof);
406
407 extern int stream_at_eof (stream *);
408 internal_proto(stream_at_eof);
409
410 extern int delete_file (gfc_unit *);
411 internal_proto(delete_file);
412
413 extern int file_exists (void);
414 internal_proto(file_exists);
415
416 extern const char *inquire_sequential (const char *, int);
417 internal_proto(inquire_sequential);
418
419 extern const char *inquire_direct (const char *, int);
420 internal_proto(inquire_direct);
421
422 extern const char *inquire_formatted (const char *, int);
423 internal_proto(inquire_formatted);
424
425 extern const char *inquire_unformatted (const char *, int);
426 internal_proto(inquire_unformatted);
427
428 extern const char *inquire_read (const char *, int);
429 internal_proto(inquire_read);
430
431 extern const char *inquire_write (const char *, int);
432 internal_proto(inquire_write);
433
434 extern const char *inquire_readwrite (const char *, int);
435 internal_proto(inquire_readwrite);
436
437 extern gfc_offset file_length (stream *);
438 internal_proto(file_length);
439
440 extern gfc_offset file_position (stream *);
441 internal_proto(file_position);
442
443 extern int is_seekable (stream *);
444 internal_proto(is_seekable);
445
446 extern void empty_internal_buffer(stream *);
447 internal_proto(empty_internal_buffer);
448
449 extern try flush (stream *);
450 internal_proto(flush);
451
452 extern int unit_to_fd (int);
453 internal_proto(unit_to_fd);
454
455 /* unit.c */
456
457 extern void insert_unit (gfc_unit *);
458 internal_proto(insert_unit);
459
460 extern int close_unit (gfc_unit *);
461 internal_proto(close_unit);
462
463 extern int is_internal_unit (void);
464 internal_proto(is_internal_unit);
465
466 extern gfc_unit *find_unit (int);
467 internal_proto(find_unit);
468
469 extern gfc_unit *get_unit (int);
470 internal_proto(get_unit);
471
472 /* open.c */
473
474 extern void test_endfile (gfc_unit *);
475 internal_proto(test_endfile);
476
477 extern void new_unit (unit_flags *);
478 internal_proto(new_unit);
479
480 /* format.c */
481
482 extern void parse_format (void);
483 internal_proto(parse_format);
484
485 extern fnode *next_format (void);
486 internal_proto(next_format);
487
488 extern void unget_format (fnode *);
489 internal_proto(unget_format);
490
491 extern void format_error (fnode *, const char *);
492 internal_proto(format_error);
493
494 extern void free_fnodes (void);
495 internal_proto(free_fnodes);
496
497 /* transfer.c */
498
499 #define SCRATCH_SIZE 300
500
501 extern char scratch[];
502 internal_proto(scratch);
503
504 extern const char *type_name (bt);
505 internal_proto(type_name);
506
507 extern void *read_block (int *);
508 internal_proto(read_block);
509
510 extern void *write_block (int);
511 internal_proto(write_block);
512
513 extern void next_record (int);
514 internal_proto(next_record);
515
516 /* read.c */
517
518 extern void set_integer (void *, int64_t, int);
519 internal_proto(set_integer);
520
521 extern uint64_t max_value (int, int);
522 internal_proto(max_value);
523
524 extern int convert_real (void *, const char *, int);
525 internal_proto(convert_real);
526
527 extern void read_a (fnode *, char *, int);
528 internal_proto(read_a);
529
530 extern void read_f (fnode *, char *, int);
531 internal_proto(read_f);
532
533 extern void read_l (fnode *, char *, int);
534 internal_proto(read_l);
535
536 extern void read_x (fnode *);
537 internal_proto(read_x);
538
539 extern void read_radix (fnode *, char *, int, int);
540 internal_proto(read_radix);
541
542 extern void read_decimal (fnode *, char *, int);
543 internal_proto(read_decimal);
544
545 /* list_read.c */
546
547 extern void list_formatted_read (bt, void *, int);
548 internal_proto(list_formatted_read);
549
550 extern void finish_list_read (void);
551 internal_proto(finish_list_read);
552
553 extern void init_at_eol();
554 internal_proto(init_at_eol);
555
556 extern void namelist_read();
557 internal_proto(namelist_read);
558
559 extern void namelist_write();
560 internal_proto(namelist_write);
561
562 /* write.c */
563
564 extern void write_a (fnode *, const char *, int);
565 internal_proto(write_a);
566
567 extern void write_b (fnode *, const char *, int);
568 internal_proto(write_b);
569
570 extern void write_d (fnode *, const char *, int);
571 internal_proto(write_d);
572
573 extern void write_e (fnode *, const char *, int);
574 internal_proto(write_e);
575
576 extern void write_en (fnode *, const char *, int);
577 internal_proto(write_en);
578
579 extern void write_es (fnode *, const char *, int);
580 internal_proto(write_es);
581
582 extern void write_f (fnode *, const char *, int);
583 internal_proto(write_f);
584
585 extern void write_i (fnode *, const char *, int);
586 internal_proto(write_i);
587
588 extern void write_l (fnode *, char *, int);
589 internal_proto(write_l);
590
591 extern void write_o (fnode *, const char *, int);
592 internal_proto(write_o);
593
594 extern void write_x (fnode *);
595 internal_proto(write_x);
596
597 extern void write_z (fnode *, const char *, int);
598 internal_proto(write_z);
599
600 extern void list_formatted_write (bt, void *, int);
601 internal_proto(list_formatted_write);
602
603 #endif