io.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. /* Copyright (C) 2002-2015 Free Software Foundation, Inc.
  2. Contributed by Andy Vaught
  3. F2003 I/O support contributed by Jerry DeLisle
  4. This file is part of the GNU Fortran runtime library (libgfortran).
  5. Libgfortran is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. Libgfortran is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #ifndef GFOR_IO_H
  21. #define GFOR_IO_H
  22. /* IO library include. */
  23. #include "libgfortran.h"
  24. #include <gthr.h>
  25. /* POSIX 2008 specifies that the extended locale stuff is found in
  26. locale.h, but some systems have them in xlocale.h. */
  27. #include <locale.h>
  28. #ifdef HAVE_XLOCALE_H
  29. #include <xlocale.h>
  30. #endif
  31. /* Forward declarations. */
  32. struct st_parameter_dt;
  33. typedef struct stream stream;
  34. struct fbuf;
  35. struct format_data;
  36. typedef struct fnode fnode;
  37. struct gfc_unit;
  38. #ifdef HAVE_NEWLOCALE
  39. /* We have POSIX 2008 extended locale stuff. */
  40. extern locale_t c_locale;
  41. internal_proto(c_locale);
  42. #else
  43. extern char* old_locale;
  44. internal_proto(old_locale);
  45. extern int old_locale_ctr;
  46. internal_proto(old_locale_ctr);
  47. extern __gthread_mutex_t old_locale_lock;
  48. internal_proto(old_locale_lock);
  49. #endif
  50. /* Macros for testing what kinds of I/O we are doing. */
  51. #define is_array_io(dtp) ((dtp)->internal_unit_desc)
  52. #define is_internal_unit(dtp) ((dtp)->u.p.unit_is_internal)
  53. #define is_stream_io(dtp) ((dtp)->u.p.current_unit->flags.access == ACCESS_STREAM)
  54. #define is_char4_unit(dtp) ((dtp)->u.p.unit_is_internal && (dtp)->common.unit)
  55. /* The array_loop_spec contains the variables for the loops over index ranges
  56. that are encountered. */
  57. typedef struct array_loop_spec
  58. {
  59. /* Index counter for this dimension. */
  60. index_type idx;
  61. /* Start for the index counter. */
  62. index_type start;
  63. /* End for the index counter. */
  64. index_type end;
  65. /* Step for the index counter. */
  66. index_type step;
  67. }
  68. array_loop_spec;
  69. /* A structure to build a hash table for format data. */
  70. #define FORMAT_HASH_SIZE 16
  71. typedef struct format_hash_entry
  72. {
  73. char *key;
  74. gfc_charlen_type key_len;
  75. struct format_data *hashed_fmt;
  76. }
  77. format_hash_entry;
  78. /* Representation of a namelist object in libgfortran
  79. Namelist Records
  80. &GROUPNAME OBJECT=value[s] [,OBJECT=value[s]].../
  81. or
  82. &GROUPNAME OBJECT=value[s] [,OBJECT=value[s]]...&END
  83. The object can be a fully qualified, compound name for an intrinsic
  84. type, derived types or derived type components. So, a substring
  85. a(:)%b(4)%ch(2:4)(1:7) has to be treated correctly in namelist
  86. read. Hence full information about the structure of the object has
  87. to be available to list_read.c and write.
  88. These requirements are met by the following data structures.
  89. namelist_info type contains all the scalar information about the
  90. object and arrays of descriptor_dimension and array_loop_spec types for
  91. arrays. */
  92. typedef struct namelist_type
  93. {
  94. /* Object type. */
  95. bt type;
  96. /* Object name. */
  97. char * var_name;
  98. /* Address for the start of the object's data. */
  99. void * mem_pos;
  100. /* Flag to show that a read is to be attempted for this node. */
  101. int touched;
  102. /* Length of intrinsic type in bytes. */
  103. int len;
  104. /* Rank of the object. */
  105. int var_rank;
  106. /* Overall size of the object in bytes. */
  107. index_type size;
  108. /* Length of character string. */
  109. index_type string_length;
  110. descriptor_dimension * dim;
  111. array_loop_spec * ls;
  112. struct namelist_type * next;
  113. }
  114. namelist_info;
  115. /* Options for the OPEN statement. */
  116. typedef enum
  117. { ACCESS_SEQUENTIAL, ACCESS_DIRECT, ACCESS_APPEND, ACCESS_STREAM,
  118. ACCESS_UNSPECIFIED
  119. }
  120. unit_access;
  121. typedef enum
  122. { ACTION_READ, ACTION_WRITE, ACTION_READWRITE,
  123. ACTION_UNSPECIFIED
  124. }
  125. unit_action;
  126. typedef enum
  127. { BLANK_NULL, BLANK_ZERO, BLANK_UNSPECIFIED }
  128. unit_blank;
  129. typedef enum
  130. { DELIM_NONE, DELIM_APOSTROPHE, DELIM_QUOTE,
  131. DELIM_UNSPECIFIED
  132. }
  133. unit_delim;
  134. typedef enum
  135. { FORM_FORMATTED, FORM_UNFORMATTED, FORM_UNSPECIFIED }
  136. unit_form;
  137. typedef enum
  138. { POSITION_ASIS, POSITION_REWIND, POSITION_APPEND,
  139. POSITION_UNSPECIFIED
  140. }
  141. unit_position;
  142. typedef enum
  143. { STATUS_UNKNOWN, STATUS_OLD, STATUS_NEW, STATUS_SCRATCH,
  144. STATUS_REPLACE, STATUS_UNSPECIFIED
  145. }
  146. unit_status;
  147. typedef enum
  148. { PAD_YES, PAD_NO, PAD_UNSPECIFIED }
  149. unit_pad;
  150. typedef enum
  151. { DECIMAL_POINT, DECIMAL_COMMA, DECIMAL_UNSPECIFIED }
  152. unit_decimal;
  153. typedef enum
  154. { ENCODING_UTF8, ENCODING_DEFAULT, ENCODING_UNSPECIFIED }
  155. unit_encoding;
  156. typedef enum
  157. { ROUND_UP = GFC_FPE_UPWARD,
  158. ROUND_DOWN = GFC_FPE_DOWNWARD,
  159. ROUND_ZERO = GFC_FPE_TOWARDZERO,
  160. ROUND_NEAREST = GFC_FPE_TONEAREST,
  161. ROUND_COMPATIBLE = 10, /* round away from zero. */
  162. ROUND_PROCDEFINED, /* Here as ROUND_NEAREST. */
  163. ROUND_UNSPECIFIED /* Should never occur. */
  164. }
  165. unit_round;
  166. /* NOTE: unit_sign must correspond with the sign_status enumerator in
  167. st_parameter_dt to not break the ABI. */
  168. typedef enum
  169. { SIGN_PROCDEFINED, SIGN_SUPPRESS, SIGN_PLUS, SIGN_UNSPECIFIED }
  170. unit_sign;
  171. typedef enum
  172. { ADVANCE_YES, ADVANCE_NO, ADVANCE_UNSPECIFIED }
  173. unit_advance;
  174. typedef enum
  175. {READING, WRITING, LIST_READING, LIST_WRITING}
  176. unit_mode;
  177. typedef enum
  178. { ASYNC_YES, ASYNC_NO, ASYNC_UNSPECIFIED }
  179. unit_async;
  180. typedef enum
  181. { SIGN_S, SIGN_SS, SIGN_SP }
  182. unit_sign_s;
  183. #define CHARACTER1(name) \
  184. char * name; \
  185. gfc_charlen_type name ## _len
  186. #define CHARACTER2(name) \
  187. gfc_charlen_type name ## _len; \
  188. char * name
  189. typedef struct
  190. {
  191. st_parameter_common common;
  192. GFC_INTEGER_4 recl_in;
  193. CHARACTER2 (file);
  194. CHARACTER1 (status);
  195. CHARACTER2 (access);
  196. CHARACTER1 (form);
  197. CHARACTER2 (blank);
  198. CHARACTER1 (position);
  199. CHARACTER2 (action);
  200. CHARACTER1 (delim);
  201. CHARACTER2 (pad);
  202. CHARACTER1 (convert);
  203. CHARACTER2 (decimal);
  204. CHARACTER1 (encoding);
  205. CHARACTER2 (round);
  206. CHARACTER1 (sign);
  207. CHARACTER2 (asynchronous);
  208. GFC_INTEGER_4 *newunit;
  209. }
  210. st_parameter_open;
  211. #define IOPARM_CLOSE_HAS_STATUS (1 << 7)
  212. typedef struct
  213. {
  214. st_parameter_common common;
  215. CHARACTER1 (status);
  216. }
  217. st_parameter_close;
  218. typedef struct
  219. {
  220. st_parameter_common common;
  221. }
  222. st_parameter_filepos;
  223. #define IOPARM_INQUIRE_HAS_EXIST (1 << 7)
  224. #define IOPARM_INQUIRE_HAS_OPENED (1 << 8)
  225. #define IOPARM_INQUIRE_HAS_NUMBER (1 << 9)
  226. #define IOPARM_INQUIRE_HAS_NAMED (1 << 10)
  227. #define IOPARM_INQUIRE_HAS_NEXTREC (1 << 11)
  228. #define IOPARM_INQUIRE_HAS_RECL_OUT (1 << 12)
  229. #define IOPARM_INQUIRE_HAS_STRM_POS_OUT (1 << 13)
  230. #define IOPARM_INQUIRE_HAS_FILE (1 << 14)
  231. #define IOPARM_INQUIRE_HAS_ACCESS (1 << 15)
  232. #define IOPARM_INQUIRE_HAS_FORM (1 << 16)
  233. #define IOPARM_INQUIRE_HAS_BLANK (1 << 17)
  234. #define IOPARM_INQUIRE_HAS_POSITION (1 << 18)
  235. #define IOPARM_INQUIRE_HAS_ACTION (1 << 19)
  236. #define IOPARM_INQUIRE_HAS_DELIM (1 << 20)
  237. #define IOPARM_INQUIRE_HAS_PAD (1 << 21)
  238. #define IOPARM_INQUIRE_HAS_NAME (1 << 22)
  239. #define IOPARM_INQUIRE_HAS_SEQUENTIAL (1 << 23)
  240. #define IOPARM_INQUIRE_HAS_DIRECT (1 << 24)
  241. #define IOPARM_INQUIRE_HAS_FORMATTED (1 << 25)
  242. #define IOPARM_INQUIRE_HAS_UNFORMATTED (1 << 26)
  243. #define IOPARM_INQUIRE_HAS_READ (1 << 27)
  244. #define IOPARM_INQUIRE_HAS_WRITE (1 << 28)
  245. #define IOPARM_INQUIRE_HAS_READWRITE (1 << 29)
  246. #define IOPARM_INQUIRE_HAS_CONVERT (1 << 30)
  247. #define IOPARM_INQUIRE_HAS_FLAGS2 (1 << 31)
  248. #define IOPARM_INQUIRE_HAS_ASYNCHRONOUS (1 << 0)
  249. #define IOPARM_INQUIRE_HAS_DECIMAL (1 << 1)
  250. #define IOPARM_INQUIRE_HAS_ENCODING (1 << 2)
  251. #define IOPARM_INQUIRE_HAS_ROUND (1 << 3)
  252. #define IOPARM_INQUIRE_HAS_SIGN (1 << 4)
  253. #define IOPARM_INQUIRE_HAS_PENDING (1 << 5)
  254. #define IOPARM_INQUIRE_HAS_SIZE (1 << 6)
  255. #define IOPARM_INQUIRE_HAS_ID (1 << 7)
  256. #define IOPARM_INQUIRE_HAS_IQSTREAM (1 << 8)
  257. typedef struct
  258. {
  259. st_parameter_common common;
  260. GFC_INTEGER_4 *exist, *opened, *number, *named;
  261. GFC_INTEGER_4 *nextrec, *recl_out;
  262. GFC_IO_INT *strm_pos_out;
  263. CHARACTER1 (file);
  264. CHARACTER2 (access);
  265. CHARACTER1 (form);
  266. CHARACTER2 (blank);
  267. CHARACTER1 (position);
  268. CHARACTER2 (action);
  269. CHARACTER1 (delim);
  270. CHARACTER2 (pad);
  271. CHARACTER1 (name);
  272. CHARACTER2 (sequential);
  273. CHARACTER1 (direct);
  274. CHARACTER2 (formatted);
  275. CHARACTER1 (unformatted);
  276. CHARACTER2 (read);
  277. CHARACTER1 (write);
  278. CHARACTER2 (readwrite);
  279. CHARACTER1 (convert);
  280. GFC_INTEGER_4 flags2;
  281. CHARACTER1 (asynchronous);
  282. CHARACTER2 (decimal);
  283. CHARACTER1 (encoding);
  284. CHARACTER2 (round);
  285. CHARACTER1 (sign);
  286. GFC_INTEGER_4 *pending;
  287. GFC_IO_INT *size;
  288. GFC_INTEGER_4 *id;
  289. CHARACTER1 (iqstream);
  290. }
  291. st_parameter_inquire;
  292. #define IOPARM_DT_LIST_FORMAT (1 << 7)
  293. #define IOPARM_DT_NAMELIST_READ_MODE (1 << 8)
  294. #define IOPARM_DT_HAS_REC (1 << 9)
  295. #define IOPARM_DT_HAS_SIZE (1 << 10)
  296. #define IOPARM_DT_HAS_IOLENGTH (1 << 11)
  297. #define IOPARM_DT_HAS_FORMAT (1 << 12)
  298. #define IOPARM_DT_HAS_ADVANCE (1 << 13)
  299. #define IOPARM_DT_HAS_INTERNAL_UNIT (1 << 14)
  300. #define IOPARM_DT_HAS_NAMELIST_NAME (1 << 15)
  301. #define IOPARM_DT_HAS_ID (1 << 16)
  302. #define IOPARM_DT_HAS_POS (1 << 17)
  303. #define IOPARM_DT_HAS_ASYNCHRONOUS (1 << 18)
  304. #define IOPARM_DT_HAS_BLANK (1 << 19)
  305. #define IOPARM_DT_HAS_DECIMAL (1 << 20)
  306. #define IOPARM_DT_HAS_DELIM (1 << 21)
  307. #define IOPARM_DT_HAS_PAD (1 << 22)
  308. #define IOPARM_DT_HAS_ROUND (1 << 23)
  309. #define IOPARM_DT_HAS_SIGN (1 << 24)
  310. #define IOPARM_DT_HAS_F2003 (1 << 25)
  311. /* Internal use bit. */
  312. #define IOPARM_DT_IONML_SET (1 << 31)
  313. typedef struct st_parameter_dt
  314. {
  315. st_parameter_common common;
  316. GFC_IO_INT rec;
  317. GFC_IO_INT *size, *iolength;
  318. gfc_array_char *internal_unit_desc;
  319. CHARACTER1 (format);
  320. CHARACTER2 (advance);
  321. CHARACTER1 (internal_unit);
  322. CHARACTER2 (namelist_name);
  323. /* Private part of the structure. The compiler just needs
  324. to reserve enough space. */
  325. union
  326. {
  327. struct
  328. {
  329. void (*transfer) (struct st_parameter_dt *, bt, void *, int,
  330. size_t, size_t);
  331. struct gfc_unit *current_unit;
  332. /* Item number in a formatted data transfer. Also used in namelist
  333. read_logical as an index into line_buffer. */
  334. int item_count;
  335. unit_mode mode;
  336. unit_blank blank_status;
  337. unit_sign sign_status;
  338. int scale_factor;
  339. int max_pos; /* Maximum righthand column written to. */
  340. /* Number of skips + spaces to be done for T and X-editing. */
  341. int skips;
  342. /* Number of spaces to be done for T and X-editing. */
  343. int pending_spaces;
  344. /* Whether an EOR condition was encountered. Value is:
  345. 0 if no EOR was encountered
  346. 1 if an EOR was encountered due to a 1-byte marker (LF)
  347. 2 if an EOR was encountered due to a 2-bytes marker (CRLF) */
  348. int sf_seen_eor;
  349. unit_advance advance_status;
  350. unsigned reversion_flag : 1; /* Format reversion has occurred. */
  351. unsigned first_item : 1;
  352. unsigned seen_dollar : 1;
  353. unsigned eor_condition : 1;
  354. unsigned no_leading_blank : 1;
  355. unsigned char_flag : 1;
  356. unsigned input_complete : 1;
  357. unsigned at_eol : 1;
  358. unsigned comma_flag : 1;
  359. /* A namelist specific flag used in the list directed library
  360. to flag that calls are being made from namelist read (e.g. to
  361. ignore comments or to treat '/' as a terminator) */
  362. unsigned namelist_mode : 1;
  363. /* A namelist specific flag used in the list directed library
  364. to flag read errors and return, so that an attempt can be
  365. made to read a new object name. */
  366. unsigned nml_read_error : 1;
  367. /* A sequential formatted read specific flag used to signal that a
  368. character string is being read so don't use commas to shorten a
  369. formatted field width. */
  370. unsigned sf_read_comma : 1;
  371. /* A namelist specific flag used to enable reading input from
  372. line_buffer for logical reads. */
  373. unsigned line_buffer_enabled : 1;
  374. /* An internal unit specific flag used to identify that the associated
  375. unit is internal. */
  376. unsigned unit_is_internal : 1;
  377. /* An internal unit specific flag to signify an EOF condition for list
  378. directed read. */
  379. unsigned at_eof : 1;
  380. /* Used for g0 floating point output. */
  381. unsigned g0_no_blanks : 1;
  382. /* Used to signal use of free_format_data. */
  383. unsigned format_not_saved : 1;
  384. /* A flag used to identify when a non-standard expanded namelist read
  385. has occurred. */
  386. unsigned expanded_read : 1;
  387. /* 13 unused bits. */
  388. /* Used for ungetc() style functionality. Possible values
  389. are an unsigned char, EOF, or EOF - 1 used to mark the
  390. field as not valid. */
  391. int last_char;
  392. char nml_delim;
  393. int repeat_count;
  394. int saved_length;
  395. int saved_used;
  396. bt saved_type;
  397. char *saved_string;
  398. char *scratch;
  399. char *line_buffer;
  400. struct format_data *fmt;
  401. namelist_info *ionml;
  402. #ifdef HAVE_NEWLOCALE
  403. locale_t old_locale;
  404. #endif
  405. /* Current position within the look-ahead line buffer. */
  406. int line_buffer_pos;
  407. /* Storage area for values except for strings. Must be
  408. large enough to hold a complex value (two reals) of the
  409. largest kind. */
  410. char value[32];
  411. GFC_IO_INT size_used;
  412. } p;
  413. /* This pad size must be equal to the pad_size declared in
  414. trans-io.c (gfc_build_io_library_fndecls). The above structure
  415. must be smaller or equal to this array. */
  416. char pad[16 * sizeof (char *) + 32 * sizeof (int)];
  417. } u;
  418. GFC_INTEGER_4 *id;
  419. GFC_IO_INT pos;
  420. CHARACTER1 (asynchronous);
  421. CHARACTER2 (blank);
  422. CHARACTER1 (decimal);
  423. CHARACTER2 (delim);
  424. CHARACTER1 (pad);
  425. CHARACTER2 (round);
  426. CHARACTER1 (sign);
  427. }
  428. st_parameter_dt;
  429. /* Ensure st_parameter_dt's u.pad is bigger or equal to u.p. */
  430. extern char check_st_parameter_dt[sizeof (((st_parameter_dt *) 0)->u.pad)
  431. >= sizeof (((st_parameter_dt *) 0)->u.p)
  432. ? 1 : -1];
  433. #define IOPARM_WAIT_HAS_ID (1 << 7)
  434. typedef struct
  435. {
  436. st_parameter_common common;
  437. CHARACTER1 (id);
  438. }
  439. st_parameter_wait;
  440. #undef CHARACTER1
  441. #undef CHARACTER2
  442. typedef struct
  443. {
  444. unit_access access;
  445. unit_action action;
  446. unit_blank blank;
  447. unit_delim delim;
  448. unit_form form;
  449. int is_notpadded;
  450. unit_position position;
  451. unit_status status;
  452. unit_pad pad;
  453. unit_convert convert;
  454. int has_recl;
  455. unit_decimal decimal;
  456. unit_encoding encoding;
  457. unit_round round;
  458. unit_sign sign;
  459. unit_async async;
  460. }
  461. unit_flags;
  462. typedef struct gfc_unit
  463. {
  464. int unit_number;
  465. stream *s;
  466. /* Treap links. */
  467. struct gfc_unit *left, *right;
  468. int priority;
  469. int read_bad, current_record, saved_pos, previous_nonadvancing_write;
  470. enum
  471. { NO_ENDFILE, AT_ENDFILE, AFTER_ENDFILE }
  472. endfile;
  473. unit_mode mode;
  474. unit_flags flags;
  475. unit_pad pad_status;
  476. unit_decimal decimal_status;
  477. unit_delim delim_status;
  478. unit_round round_status;
  479. /* recl -- Record length of the file.
  480. last_record -- Last record number read or written
  481. maxrec -- Maximum record number in a direct access file
  482. bytes_left -- Bytes left in current record.
  483. strm_pos -- Current position in file for STREAM I/O.
  484. recl_subrecord -- Maximum length for subrecord.
  485. bytes_left_subrecord -- Bytes left in current subrecord. */
  486. gfc_offset recl, last_record, maxrec, bytes_left, strm_pos,
  487. recl_subrecord, bytes_left_subrecord;
  488. /* Set to 1 if we have read a subrecord. */
  489. int continued;
  490. __gthread_mutex_t lock;
  491. /* Number of threads waiting to acquire this unit's lock.
  492. When non-zero, close_unit doesn't only removes the unit
  493. from the UNIT_ROOT tree, but doesn't free it and the
  494. last of the waiting threads will do that.
  495. This must be either atomically increased/decreased, or
  496. always guarded by UNIT_LOCK. */
  497. int waiting;
  498. /* Flag set by close_unit if the unit as been closed.
  499. Must be manipulated under unit's lock. */
  500. int closed;
  501. /* For traversing arrays */
  502. array_loop_spec *ls;
  503. int rank;
  504. /* Name of the file at the time OPEN was executed, as a
  505. null-terminated C string. */
  506. char *filename;
  507. /* The format hash table. */
  508. struct format_hash_entry format_hash_table[FORMAT_HASH_SIZE];
  509. /* Formatting buffer. */
  510. struct fbuf *fbuf;
  511. /* Function pointer, points to list_read worker functions. */
  512. int (*next_char_fn_ptr) (st_parameter_dt *);
  513. void (*push_char_fn_ptr) (st_parameter_dt *, int);
  514. }
  515. gfc_unit;
  516. /* unit.c */
  517. /* Maximum file offset, computed at library initialization time. */
  518. extern gfc_offset max_offset;
  519. internal_proto(max_offset);
  520. /* Unit tree root. */
  521. extern gfc_unit *unit_root;
  522. internal_proto(unit_root);
  523. extern __gthread_mutex_t unit_lock;
  524. internal_proto(unit_lock);
  525. extern int close_unit (gfc_unit *);
  526. internal_proto(close_unit);
  527. extern gfc_unit *get_internal_unit (st_parameter_dt *);
  528. internal_proto(get_internal_unit);
  529. extern void free_internal_unit (st_parameter_dt *);
  530. internal_proto(free_internal_unit);
  531. extern gfc_unit *find_unit (int);
  532. internal_proto(find_unit);
  533. extern gfc_unit *find_or_create_unit (int);
  534. internal_proto(find_or_create_unit);
  535. extern gfc_unit *get_unit (st_parameter_dt *, int);
  536. internal_proto(get_unit);
  537. extern void unlock_unit (gfc_unit *);
  538. internal_proto(unlock_unit);
  539. extern void finish_last_advance_record (gfc_unit *u);
  540. internal_proto (finish_last_advance_record);
  541. extern int unit_truncate (gfc_unit *, gfc_offset, st_parameter_common *);
  542. internal_proto (unit_truncate);
  543. extern GFC_INTEGER_4 get_unique_unit_number (st_parameter_open *);
  544. internal_proto(get_unique_unit_number);
  545. /* open.c */
  546. extern gfc_unit *new_unit (st_parameter_open *, gfc_unit *, unit_flags *);
  547. internal_proto(new_unit);
  548. /* transfer.c */
  549. #define SCRATCH_SIZE 300
  550. extern const char *type_name (bt);
  551. internal_proto(type_name);
  552. extern void * read_block_form (st_parameter_dt *, int *);
  553. internal_proto(read_block_form);
  554. extern void * read_block_form4 (st_parameter_dt *, int *);
  555. internal_proto(read_block_form4);
  556. extern void *write_block (st_parameter_dt *, int);
  557. internal_proto(write_block);
  558. extern gfc_offset next_array_record (st_parameter_dt *, array_loop_spec *,
  559. int*);
  560. internal_proto(next_array_record);
  561. extern gfc_offset init_loop_spec (gfc_array_char *, array_loop_spec *,
  562. gfc_offset *);
  563. internal_proto(init_loop_spec);
  564. extern void next_record (st_parameter_dt *, int);
  565. internal_proto(next_record);
  566. extern void st_wait (st_parameter_wait *);
  567. export_proto(st_wait);
  568. extern void hit_eof (st_parameter_dt *);
  569. internal_proto(hit_eof);
  570. /* read.c */
  571. extern void set_integer (void *, GFC_INTEGER_LARGEST, int);
  572. internal_proto(set_integer);
  573. extern GFC_UINTEGER_LARGEST si_max (int);
  574. internal_proto(si_max);
  575. extern int convert_real (st_parameter_dt *, void *, const char *, int);
  576. internal_proto(convert_real);
  577. extern int convert_infnan (st_parameter_dt *, void *, const char *, int);
  578. internal_proto(convert_infnan);
  579. extern void read_a (st_parameter_dt *, const fnode *, char *, int);
  580. internal_proto(read_a);
  581. extern void read_a_char4 (st_parameter_dt *, const fnode *, char *, int);
  582. internal_proto(read_a);
  583. extern void read_f (st_parameter_dt *, const fnode *, char *, int);
  584. internal_proto(read_f);
  585. extern void read_l (st_parameter_dt *, const fnode *, char *, int);
  586. internal_proto(read_l);
  587. extern void read_x (st_parameter_dt *, int);
  588. internal_proto(read_x);
  589. extern void read_radix (st_parameter_dt *, const fnode *, char *, int, int);
  590. internal_proto(read_radix);
  591. extern void read_decimal (st_parameter_dt *, const fnode *, char *, int);
  592. internal_proto(read_decimal);
  593. /* list_read.c */
  594. extern void list_formatted_read (st_parameter_dt *, bt, void *, int, size_t,
  595. size_t);
  596. internal_proto(list_formatted_read);
  597. extern void finish_list_read (st_parameter_dt *);
  598. internal_proto(finish_list_read);
  599. extern void namelist_read (st_parameter_dt *);
  600. internal_proto(namelist_read);
  601. extern void namelist_write (st_parameter_dt *);
  602. internal_proto(namelist_write);
  603. /* write.c */
  604. extern void write_a (st_parameter_dt *, const fnode *, const char *, int);
  605. internal_proto(write_a);
  606. extern void write_a_char4 (st_parameter_dt *, const fnode *, const char *, int);
  607. internal_proto(write_a_char4);
  608. extern void write_b (st_parameter_dt *, const fnode *, const char *, int);
  609. internal_proto(write_b);
  610. extern void write_d (st_parameter_dt *, const fnode *, const char *, int);
  611. internal_proto(write_d);
  612. extern void write_e (st_parameter_dt *, const fnode *, const char *, int);
  613. internal_proto(write_e);
  614. extern void write_en (st_parameter_dt *, const fnode *, const char *, int);
  615. internal_proto(write_en);
  616. extern void write_es (st_parameter_dt *, const fnode *, const char *, int);
  617. internal_proto(write_es);
  618. extern void write_f (st_parameter_dt *, const fnode *, const char *, int);
  619. internal_proto(write_f);
  620. extern void write_i (st_parameter_dt *, const fnode *, const char *, int);
  621. internal_proto(write_i);
  622. extern void write_l (st_parameter_dt *, const fnode *, char *, int);
  623. internal_proto(write_l);
  624. extern void write_o (st_parameter_dt *, const fnode *, const char *, int);
  625. internal_proto(write_o);
  626. extern void write_real (st_parameter_dt *, const char *, int);
  627. internal_proto(write_real);
  628. extern void write_real_g0 (st_parameter_dt *, const char *, int, int);
  629. internal_proto(write_real_g0);
  630. extern void write_x (st_parameter_dt *, int, int);
  631. internal_proto(write_x);
  632. extern void write_z (st_parameter_dt *, const fnode *, const char *, int);
  633. internal_proto(write_z);
  634. extern void list_formatted_write (st_parameter_dt *, bt, void *, int, size_t,
  635. size_t);
  636. internal_proto(list_formatted_write);
  637. /* size_from_kind.c */
  638. extern size_t size_from_real_kind (int);
  639. internal_proto(size_from_real_kind);
  640. extern size_t size_from_complex_kind (int);
  641. internal_proto(size_from_complex_kind);
  642. /* lock.c */
  643. extern void free_ionml (st_parameter_dt *);
  644. internal_proto(free_ionml);
  645. static inline void
  646. inc_waiting_locked (gfc_unit *u)
  647. {
  648. #ifdef HAVE_SYNC_FETCH_AND_ADD
  649. (void) __sync_fetch_and_add (&u->waiting, 1);
  650. #else
  651. u->waiting++;
  652. #endif
  653. }
  654. static inline int
  655. predec_waiting_locked (gfc_unit *u)
  656. {
  657. #ifdef HAVE_SYNC_FETCH_AND_ADD
  658. return __sync_add_and_fetch (&u->waiting, -1);
  659. #else
  660. return --u->waiting;
  661. #endif
  662. }
  663. static inline void
  664. dec_waiting_unlocked (gfc_unit *u)
  665. {
  666. #ifdef HAVE_SYNC_FETCH_AND_ADD
  667. (void) __sync_fetch_and_add (&u->waiting, -1);
  668. #else
  669. __gthread_mutex_lock (&unit_lock);
  670. u->waiting--;
  671. __gthread_mutex_unlock (&unit_lock);
  672. #endif
  673. }
  674. static inline void
  675. memset4 (gfc_char4_t *p, gfc_char4_t c, int k)
  676. {
  677. int j;
  678. for (j = 0; j < k; j++)
  679. *p++ = c;
  680. }
  681. #endif