1 /* Copyright (C) 2002-2014 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
3 Namelist input contributed by Paul Thomas
4 F2003 I/O support contributed by Jerry DeLisle
6 This file is part of the GNU Fortran runtime library (libgfortran).
8 Libgfortran is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 Libgfortran is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
35 typedef unsigned char uchar
;
38 /* List directed input. Several parsing subroutines are practically
39 reimplemented from formatted input, the reason being that there are
40 all kinds of small differences between formatted and list directed
44 /* Subroutines for reading characters from the input. Because a
45 repeat count is ambiguous with an integer, we have to read the
46 whole digit string before seeing if there is a '*' which signals
47 the repeat count. Since we can have a lot of potential leading
48 zeros, we have to be able to back up by arbitrary amount. Because
49 the input might not be seekable, we have to buffer the data
52 #define CASE_DIGITS case '0': case '1': case '2': case '3': case '4': \
53 case '5': case '6': case '7': case '8': case '9'
55 #define CASE_SEPARATORS case ' ': case ',': case '/': case '\n': case '\t': \
58 /* This macro assumes that we're operating on a variable. */
60 #define is_separator(c) (c == '/' || c == ',' || c == '\n' || c == ' ' \
61 || c == '\t' || c == '\r' || c == ';')
63 /* Maximum repeat count. Less than ten times the maximum signed int32. */
65 #define MAX_REPEAT 200000000
71 /* Wrappers for calling the current worker functions. */
73 #define next_char(dtp) ((dtp)->u.p.current_unit->next_char_fn_ptr (dtp))
74 #define push_char(dtp, c) ((dtp)->u.p.current_unit->push_char_fn_ptr (dtp, c))
76 /* Worker function to save a default KIND=1 character to a string
77 buffer, enlarging it as necessary. */
80 push_char_default (st_parameter_dt
*dtp
, int c
)
84 if (dtp
->u
.p
.saved_string
== NULL
)
86 // Plain malloc should suffice here, zeroing not needed?
87 dtp
->u
.p
.saved_string
= xcalloc (SCRATCH_SIZE
, 1);
88 dtp
->u
.p
.saved_length
= SCRATCH_SIZE
;
89 dtp
->u
.p
.saved_used
= 0;
92 if (dtp
->u
.p
.saved_used
>= dtp
->u
.p
.saved_length
)
94 dtp
->u
.p
.saved_length
= 2 * dtp
->u
.p
.saved_length
;
95 dtp
->u
.p
.saved_string
=
96 xrealloc (dtp
->u
.p
.saved_string
, dtp
->u
.p
.saved_length
);
98 // Also this should not be necessary.
99 memset (dtp
->u
.p
.saved_string
+ dtp
->u
.p
.saved_used
, 0,
100 dtp
->u
.p
.saved_length
- dtp
->u
.p
.saved_used
);
104 dtp
->u
.p
.saved_string
[dtp
->u
.p
.saved_used
++] = (char) c
;
108 /* Worker function to save a KIND=4 character to a string buffer,
109 enlarging the buffer as necessary. */
112 push_char4 (st_parameter_dt
*dtp
, int c
)
114 gfc_char4_t
*new, *p
= (gfc_char4_t
*) dtp
->u
.p
.saved_string
;
118 dtp
->u
.p
.saved_string
= xcalloc (SCRATCH_SIZE
, sizeof (gfc_char4_t
));
119 dtp
->u
.p
.saved_length
= SCRATCH_SIZE
;
120 dtp
->u
.p
.saved_used
= 0;
121 p
= (gfc_char4_t
*) dtp
->u
.p
.saved_string
;
124 if (dtp
->u
.p
.saved_used
>= dtp
->u
.p
.saved_length
)
126 dtp
->u
.p
.saved_length
= 2 * dtp
->u
.p
.saved_length
;
127 p
= xrealloc (p
, dtp
->u
.p
.saved_length
* sizeof (gfc_char4_t
));
129 memset4 (new + dtp
->u
.p
.saved_used
, 0,
130 dtp
->u
.p
.saved_length
- dtp
->u
.p
.saved_used
);
133 p
[dtp
->u
.p
.saved_used
++] = c
;
137 /* Free the input buffer if necessary. */
140 free_saved (st_parameter_dt
*dtp
)
142 if (dtp
->u
.p
.saved_string
== NULL
)
145 free (dtp
->u
.p
.saved_string
);
147 dtp
->u
.p
.saved_string
= NULL
;
148 dtp
->u
.p
.saved_used
= 0;
152 /* Free the line buffer if necessary. */
155 free_line (st_parameter_dt
*dtp
)
157 dtp
->u
.p
.line_buffer_pos
= 0;
158 dtp
->u
.p
.line_buffer_enabled
= 0;
160 if (dtp
->u
.p
.line_buffer
== NULL
)
163 free (dtp
->u
.p
.line_buffer
);
164 dtp
->u
.p
.line_buffer
= NULL
;
168 /* Unget saves the last character so when reading the next character,
169 we need to check to see if there is a character waiting. Similar,
170 if the line buffer is being used to read_logical, check it too. */
173 check_buffers (st_parameter_dt
*dtp
)
178 if (dtp
->u
.p
.last_char
!= EOF
- 1)
181 c
= dtp
->u
.p
.last_char
;
182 dtp
->u
.p
.last_char
= EOF
- 1;
186 /* Read from line_buffer if enabled. */
188 if (dtp
->u
.p
.line_buffer_enabled
)
192 c
= dtp
->u
.p
.line_buffer
[dtp
->u
.p
.line_buffer_pos
];
193 if (c
!= '\0' && dtp
->u
.p
.line_buffer_pos
< 64)
195 dtp
->u
.p
.line_buffer
[dtp
->u
.p
.line_buffer_pos
] = '\0';
196 dtp
->u
.p
.line_buffer_pos
++;
200 dtp
->u
.p
.line_buffer_pos
= 0;
201 dtp
->u
.p
.line_buffer_enabled
= 0;
205 dtp
->u
.p
.at_eol
= (c
== '\n' || c
== EOF
);
210 /* Worker function for default character encoded file. */
212 next_char_default (st_parameter_dt
*dtp
)
216 /* Always check the unget and line buffer first. */
217 if ((c
= check_buffers (dtp
)))
220 c
= fbuf_getc (dtp
->u
.p
.current_unit
);
221 if (c
!= EOF
&& is_stream_io (dtp
))
222 dtp
->u
.p
.current_unit
->strm_pos
++;
224 dtp
->u
.p
.at_eol
= (c
== '\n' || c
== EOF
);
229 /* Worker function for internal and array I/O units. */
231 next_char_internal (st_parameter_dt
*dtp
)
237 /* Always check the unget and line buffer first. */
238 if ((c
= check_buffers (dtp
)))
241 /* Handle the end-of-record and end-of-file conditions for
242 internal array unit. */
243 if (is_array_io (dtp
))
248 /* Check for "end-of-record" condition. */
249 if (dtp
->u
.p
.current_unit
->bytes_left
== 0)
254 record
= next_array_record (dtp
, dtp
->u
.p
.current_unit
->ls
,
257 /* Check for "end-of-file" condition. */
264 record
*= dtp
->u
.p
.current_unit
->recl
;
265 if (sseek (dtp
->u
.p
.current_unit
->s
, record
, SEEK_SET
) < 0)
268 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
273 /* Get the next character and handle end-of-record conditions. */
275 if (dtp
->common
.unit
) /* Check for kind=4 internal unit. */
276 length
= sread (dtp
->u
.p
.current_unit
->s
, &c
, 1);
280 length
= sread (dtp
->u
.p
.current_unit
->s
, &cc
, 1);
284 if (unlikely (length
< 0))
286 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
290 if (is_array_io (dtp
))
292 /* Check whether we hit EOF. */
293 if (unlikely (length
== 0))
295 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
298 dtp
->u
.p
.current_unit
->bytes_left
--;
312 dtp
->u
.p
.at_eol
= (c
== '\n' || c
== EOF
);
317 /* Worker function for UTF encoded files. */
319 next_char_utf8 (st_parameter_dt
*dtp
)
321 static const uchar masks
[6] = { 0x7F, 0x1F, 0x0F, 0x07, 0x02, 0x01 };
322 static const uchar patns
[6] = { 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
326 /* Always check the unget and line buffer first. */
327 if (!(c
= check_buffers (dtp
)))
328 c
= fbuf_getc (dtp
->u
.p
.current_unit
);
333 /* The number of leading 1-bits in the first byte indicates how many
335 for (nb
= 2; nb
< 7; nb
++)
336 if ((c
& ~masks
[nb
-1]) == patns
[nb
-1])
341 c
= (c
& masks
[nb
-1]);
343 /* Decode the bytes read. */
344 for (i
= 1; i
< nb
; i
++)
346 gfc_char4_t n
= fbuf_getc (dtp
->u
.p
.current_unit
);
347 if ((n
& 0xC0) != 0x80)
349 c
= ((c
<< 6) + (n
& 0x3F));
352 /* Make sure the shortest possible encoding was used. */
353 if (c
<= 0x7F && nb
> 1) goto invalid
;
354 if (c
<= 0x7FF && nb
> 2) goto invalid
;
355 if (c
<= 0xFFFF && nb
> 3) goto invalid
;
356 if (c
<= 0x1FFFFF && nb
> 4) goto invalid
;
357 if (c
<= 0x3FFFFFF && nb
> 5) goto invalid
;
359 /* Make sure the character is valid. */
360 if (c
> 0x7FFFFFFF || (c
>= 0xD800 && c
<= 0xDFFF))
364 dtp
->u
.p
.at_eol
= (c
== '\n' || c
== (gfc_char4_t
) EOF
);
368 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
, "Invalid UTF-8 encoding");
369 return (gfc_char4_t
) '?';
372 /* Push a character back onto the input. */
375 unget_char (st_parameter_dt
*dtp
, int c
)
377 dtp
->u
.p
.last_char
= c
;
381 /* Skip over spaces in the input. Returns the nonspace character that
382 terminated the eating and also places it back on the input. */
385 eat_spaces (st_parameter_dt
*dtp
)
389 /* If internal character array IO, peak ahead and seek past spaces.
390 This is an optimization unique to character arrays with large
391 character lengths (PR38199). This code eliminates numerous calls
392 to next_character. */
393 if (is_array_io (dtp
) && (dtp
->u
.p
.last_char
== EOF
- 1))
395 gfc_offset offset
= stell (dtp
->u
.p
.current_unit
->s
);
398 if (dtp
->common
.unit
) /* kind=4 */
400 for (i
= 0; i
< dtp
->u
.p
.current_unit
->bytes_left
; i
++)
402 if (dtp
->internal_unit
[(offset
+ i
) * sizeof (gfc_char4_t
)]
409 for (i
= 0; i
< dtp
->u
.p
.current_unit
->bytes_left
; i
++)
411 if (dtp
->internal_unit
[offset
+ i
] != ' ')
418 sseek (dtp
->u
.p
.current_unit
->s
, offset
+ i
, SEEK_SET
);
419 dtp
->u
.p
.current_unit
->bytes_left
-= i
;
423 /* Now skip spaces, EOF and EOL are handled in next_char. */
426 while (c
!= EOF
&& (c
== ' ' || c
== '\t'));
433 /* This function reads characters through to the end of the current
434 line and just ignores them. Returns 0 for success and LIBERROR_END
438 eat_line (st_parameter_dt
*dtp
)
444 while (c
!= EOF
&& c
!= '\n');
451 /* Skip over a separator. Technically, we don't always eat the whole
452 separator. This is because if we've processed the last input item,
453 then a separator is unnecessary. Plus the fact that operating
454 systems usually deliver console input on a line basis.
456 The upshot is that if we see a newline as part of reading a
457 separator, we stop reading. If there are more input items, we
458 continue reading the separator with finish_separator() which takes
459 care of the fact that we may or may not have seen a comma as part
462 Returns 0 for success, and non-zero error code otherwise. */
465 eat_separator (st_parameter_dt
*dtp
)
471 dtp
->u
.p
.comma_flag
= 0;
473 if ((c
= next_char (dtp
)) == EOF
)
478 if (dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
)
485 dtp
->u
.p
.comma_flag
= 1;
490 dtp
->u
.p
.input_complete
= 1;
494 if ((n
= next_char(dtp
)) == EOF
)
504 if (dtp
->u
.p
.namelist_mode
)
508 if ((c
= next_char (dtp
)) == EOF
)
512 err
= eat_line (dtp
);
518 while (c
== '\n' || c
== '\r' || c
== ' ' || c
== '\t');
524 if (dtp
->u
.p
.namelist_mode
)
525 { /* Eat a namelist comment. */
526 err
= eat_line (dtp
);
533 /* Fall Through... */
543 /* Finish processing a separator that was interrupted by a newline.
544 If we're here, then another data item is present, so we finish what
545 we started on the previous line. Return 0 on success, error code
549 finish_separator (st_parameter_dt
*dtp
)
552 int err
= LIBERROR_OK
;
557 if ((c
= next_char (dtp
)) == EOF
)
562 if (dtp
->u
.p
.comma_flag
)
566 if ((c
= eat_spaces (dtp
)) == EOF
)
568 if (c
== '\n' || c
== '\r')
575 dtp
->u
.p
.input_complete
= 1;
576 if (!dtp
->u
.p
.namelist_mode
)
585 if (dtp
->u
.p
.namelist_mode
)
587 err
= eat_line (dtp
);
601 /* This function is needed to catch bad conversions so that namelist can
602 attempt to see if dtp->u.p.saved_string contains a new object name rather
606 nml_bad_return (st_parameter_dt
*dtp
, char c
)
608 if (dtp
->u
.p
.namelist_mode
)
610 dtp
->u
.p
.nml_read_error
= 1;
617 /* Convert an unsigned string to an integer. The length value is -1
618 if we are working on a repeat count. Returns nonzero if we have a
619 range problem. As a side effect, frees the dtp->u.p.saved_string. */
622 convert_integer (st_parameter_dt
*dtp
, int length
, int negative
)
624 char c
, *buffer
, message
[MSGLEN
];
626 GFC_UINTEGER_LARGEST v
, max
, max10
;
627 GFC_INTEGER_LARGEST value
;
629 buffer
= dtp
->u
.p
.saved_string
;
636 max
= si_max (length
);
666 set_integer (dtp
->u
.p
.value
, value
, length
);
670 dtp
->u
.p
.repeat_count
= v
;
672 if (dtp
->u
.p
.repeat_count
== 0)
674 snprintf (message
, MSGLEN
, "Zero repeat count in item %d of list input",
675 dtp
->u
.p
.item_count
);
677 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
, message
);
687 snprintf (message
, MSGLEN
, "Repeat count overflow in item %d of list input",
688 dtp
->u
.p
.item_count
);
690 snprintf (message
, MSGLEN
, "Integer overflow while reading item %d",
691 dtp
->u
.p
.item_count
);
694 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
, message
);
700 /* Parse a repeat count for logical and complex values which cannot
701 begin with a digit. Returns nonzero if we are done, zero if we
702 should continue on. */
705 parse_repeat (st_parameter_dt
*dtp
)
707 char message
[MSGLEN
];
710 if ((c
= next_char (dtp
)) == EOF
)
734 repeat
= 10 * repeat
+ c
- '0';
736 if (repeat
> MAX_REPEAT
)
738 snprintf (message
, MSGLEN
,
739 "Repeat count overflow in item %d of list input",
740 dtp
->u
.p
.item_count
);
742 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
, message
);
751 snprintf (message
, MSGLEN
,
752 "Zero repeat count in item %d of list input",
753 dtp
->u
.p
.item_count
);
755 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
, message
);
767 dtp
->u
.p
.repeat_count
= repeat
;
781 snprintf (message
, MSGLEN
, "Bad repeat count in item %d of list input",
782 dtp
->u
.p
.item_count
);
783 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
, message
);
788 /* To read a logical we have to look ahead in the input stream to make sure
789 there is not an equal sign indicating a variable name. To do this we use
790 line_buffer to point to a temporary buffer, pushing characters there for
791 possible later reading. */
794 l_push_char (st_parameter_dt
*dtp
, char c
)
796 if (dtp
->u
.p
.line_buffer
== NULL
)
797 dtp
->u
.p
.line_buffer
= xcalloc (SCRATCH_SIZE
, 1);
799 dtp
->u
.p
.line_buffer
[dtp
->u
.p
.line_buffer_pos
++] = c
;
803 /* Read a logical character on the input. */
806 read_logical (st_parameter_dt
*dtp
, int length
)
808 char message
[MSGLEN
];
811 if (parse_repeat (dtp
))
814 c
= tolower (next_char (dtp
));
815 l_push_char (dtp
, c
);
821 l_push_char (dtp
, c
);
823 if (!is_separator(c
) && c
!= EOF
)
831 l_push_char (dtp
, c
);
833 if (!is_separator(c
) && c
!= EOF
)
840 c
= tolower (next_char (dtp
));
859 return; /* Null value. */
862 /* Save the character in case it is the beginning
863 of the next object name. */
868 dtp
->u
.p
.saved_type
= BT_LOGICAL
;
869 dtp
->u
.p
.saved_length
= length
;
871 /* Eat trailing garbage. */
874 while (c
!= EOF
&& !is_separator (c
));
878 set_integer ((int *) dtp
->u
.p
.value
, v
, length
);
885 for(i
= 0; i
< 63; i
++)
890 /* All done if this is not a namelist read. */
891 if (!dtp
->u
.p
.namelist_mode
)
904 l_push_char (dtp
, c
);
907 dtp
->u
.p
.nml_read_error
= 1;
908 dtp
->u
.p
.line_buffer_enabled
= 1;
909 dtp
->u
.p
.line_buffer_pos
= 0;
917 if (nml_bad_return (dtp
, c
))
933 snprintf (message
, MSGLEN
, "Bad logical value while reading item %d",
934 dtp
->u
.p
.item_count
);
936 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
, message
);
941 dtp
->u
.p
.saved_type
= BT_LOGICAL
;
942 dtp
->u
.p
.saved_length
= length
;
943 set_integer ((int *) dtp
->u
.p
.value
, v
, length
);
949 /* Reading integers is tricky because we can actually be reading a
950 repeat count. We have to store the characters in a buffer because
951 we could be reading an integer that is larger than the default int
952 used for repeat counts. */
955 read_integer (st_parameter_dt
*dtp
, int length
)
957 char message
[MSGLEN
];
967 /* Fall through... */
970 if ((c
= next_char (dtp
)) == EOF
)
974 CASE_SEPARATORS
: /* Single null. */
987 /* Take care of what may be a repeat count. */
999 push_char (dtp
, '\0');
1002 CASE_SEPARATORS
: /* Not a repeat count. */
1012 if (convert_integer (dtp
, -1, 0))
1015 /* Get the real integer. */
1017 if ((c
= next_char (dtp
)) == EOF
)
1025 unget_char (dtp
, c
);
1026 eat_separator (dtp
);
1031 /* Fall through... */
1034 c
= next_char (dtp
);
1045 c
= next_char (dtp
);
1063 if (nml_bad_return (dtp
, c
))
1076 snprintf (message
, MSGLEN
, "Bad integer for item %d in list input",
1077 dtp
->u
.p
.item_count
);
1079 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
, message
);
1084 unget_char (dtp
, c
);
1085 eat_separator (dtp
);
1087 push_char (dtp
, '\0');
1088 if (convert_integer (dtp
, length
, negative
))
1095 dtp
->u
.p
.saved_type
= BT_INTEGER
;
1099 /* Read a character variable. */
1102 read_character (st_parameter_dt
*dtp
, int length
__attribute__ ((unused
)))
1104 char quote
, message
[MSGLEN
];
1107 quote
= ' '; /* Space means no quote character. */
1109 if ((c
= next_char (dtp
)) == EOF
)
1119 unget_char (dtp
, c
); /* NULL value. */
1120 eat_separator (dtp
);
1129 if (dtp
->u
.p
.namelist_mode
)
1131 if (dtp
->u
.p
.current_unit
->delim_status
== DELIM_NONE
)
1133 /* No delimiters so finish reading the string now. */
1136 for (i
= dtp
->u
.p
.ionml
->string_length
; i
> 1; i
--)
1138 if ((c
= next_char (dtp
)) == EOF
)
1142 dtp
->u
.p
.saved_type
= BT_CHARACTER
;
1146 unget_char (dtp
, c
);
1153 /* Deal with a possible repeat count. */
1157 c
= next_char (dtp
);
1166 unget_char (dtp
, c
);
1167 goto done
; /* String was only digits! */
1170 push_char (dtp
, '\0');
1175 goto get_string
; /* Not a repeat count after all. */
1180 if (convert_integer (dtp
, -1, 0))
1183 /* Now get the real string. */
1185 if ((c
= next_char (dtp
)) == EOF
)
1190 unget_char (dtp
, c
); /* Repeated NULL values. */
1191 eat_separator (dtp
);
1208 if ((c
= next_char (dtp
)) == EOF
)
1220 /* See if we have a doubled quote character or the end of
1223 if ((c
= next_char (dtp
)) == EOF
)
1227 push_char (dtp
, quote
);
1231 unget_char (dtp
, c
);
1237 unget_char (dtp
, c
);
1241 if (c
!= '\n' && c
!= '\r')
1251 /* At this point, we have to have a separator, or else the string is
1254 c
= next_char (dtp
);
1256 if (is_separator (c
) || c
== '!' || c
== EOF
)
1258 unget_char (dtp
, c
);
1259 eat_separator (dtp
);
1260 dtp
->u
.p
.saved_type
= BT_CHARACTER
;
1265 snprintf (message
, MSGLEN
, "Invalid string input in item %d",
1266 dtp
->u
.p
.item_count
);
1267 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
, message
);
1279 /* Parse a component of a complex constant or a real number that we
1280 are sure is already there. This is a straight real number parser. */
1283 parse_real (st_parameter_dt
*dtp
, void *buffer
, int length
)
1285 char message
[MSGLEN
];
1288 if ((c
= next_char (dtp
)) == EOF
)
1291 if (c
== '-' || c
== '+')
1294 if ((c
= next_char (dtp
)) == EOF
)
1298 if (c
== ',' && dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
)
1301 if (!isdigit (c
) && c
!= '.')
1303 if (c
== 'i' || c
== 'I' || c
== 'n' || c
== 'N')
1311 seen_dp
= (c
== '.') ? 1 : 0;
1315 if ((c
= next_char (dtp
)) == EOF
)
1317 if (c
== ',' && dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
)
1339 push_char (dtp
, 'e');
1344 push_char (dtp
, 'e');
1346 if ((c
= next_char (dtp
)) == EOF
)
1360 if ((c
= next_char (dtp
)) == EOF
)
1362 if (c
!= '-' && c
!= '+')
1363 push_char (dtp
, '+');
1367 c
= next_char (dtp
);
1378 if ((c
= next_char (dtp
)) == EOF
)
1388 unget_char (dtp
, c
);
1397 unget_char (dtp
, c
);
1398 push_char (dtp
, '\0');
1400 m
= convert_real (dtp
, buffer
, dtp
->u
.p
.saved_string
, length
);
1406 unget_char (dtp
, c
);
1407 push_char (dtp
, '\0');
1409 m
= convert_infnan (dtp
, buffer
, dtp
->u
.p
.saved_string
, length
);
1415 /* Match INF and Infinity. */
1416 if ((c
== 'i' || c
== 'I')
1417 && ((c
= next_char (dtp
)) == 'n' || c
== 'N')
1418 && ((c
= next_char (dtp
)) == 'f' || c
== 'F'))
1420 c
= next_char (dtp
);
1421 if ((c
!= 'i' && c
!= 'I')
1422 || ((c
== 'i' || c
== 'I')
1423 && ((c
= next_char (dtp
)) == 'n' || c
== 'N')
1424 && ((c
= next_char (dtp
)) == 'i' || c
== 'I')
1425 && ((c
= next_char (dtp
)) == 't' || c
== 'T')
1426 && ((c
= next_char (dtp
)) == 'y' || c
== 'Y')
1427 && (c
= next_char (dtp
))))
1429 if (is_separator (c
) || (c
== EOF
))
1430 unget_char (dtp
, c
);
1431 push_char (dtp
, 'i');
1432 push_char (dtp
, 'n');
1433 push_char (dtp
, 'f');
1437 else if (((c
= next_char (dtp
)) == 'a' || c
== 'A')
1438 && ((c
= next_char (dtp
)) == 'n' || c
== 'N')
1439 && (c
= next_char (dtp
)))
1441 if (is_separator (c
) || (c
== EOF
))
1442 unget_char (dtp
, c
);
1443 push_char (dtp
, 'n');
1444 push_char (dtp
, 'a');
1445 push_char (dtp
, 'n');
1447 /* Match "NAN(alphanum)". */
1450 for ( ; c
!= ')'; c
= next_char (dtp
))
1451 if (is_separator (c
))
1454 c
= next_char (dtp
);
1455 if (is_separator (c
) || (c
== EOF
))
1456 unget_char (dtp
, c
);
1463 if (nml_bad_return (dtp
, c
))
1476 snprintf (message
, MSGLEN
, "Bad floating point number for item %d",
1477 dtp
->u
.p
.item_count
);
1479 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
, message
);
1485 /* Reading a complex number is straightforward because we can tell
1486 what it is right away. */
1489 read_complex (st_parameter_dt
*dtp
, void * dest
, int kind
, size_t size
)
1491 char message
[MSGLEN
];
1494 if (parse_repeat (dtp
))
1497 c
= next_char (dtp
);
1505 unget_char (dtp
, c
);
1506 eat_separator (dtp
);
1515 c
= next_char (dtp
);
1516 if (c
== '\n' || c
== '\r')
1519 unget_char (dtp
, c
);
1521 if (parse_real (dtp
, dest
, kind
))
1526 c
= next_char (dtp
);
1527 if (c
== '\n' || c
== '\r')
1530 unget_char (dtp
, c
);
1533 != (dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_POINT
? ',' : ';'))
1538 c
= next_char (dtp
);
1539 if (c
== '\n' || c
== '\r')
1542 unget_char (dtp
, c
);
1544 if (parse_real (dtp
, dest
+ size
/ 2, kind
))
1549 c
= next_char (dtp
);
1550 if (c
== '\n' || c
== '\r')
1553 unget_char (dtp
, c
);
1555 if (next_char (dtp
) != ')')
1558 c
= next_char (dtp
);
1559 if (!is_separator (c
) && (c
!= EOF
))
1562 unget_char (dtp
, c
);
1563 eat_separator (dtp
);
1566 dtp
->u
.p
.saved_type
= BT_COMPLEX
;
1571 if (nml_bad_return (dtp
, c
))
1584 snprintf (message
, MSGLEN
, "Bad complex value in item %d of list input",
1585 dtp
->u
.p
.item_count
);
1587 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
, message
);
1591 /* Parse a real number with a possible repeat count. */
1594 read_real (st_parameter_dt
*dtp
, void * dest
, int length
)
1596 char message
[MSGLEN
];
1603 c
= next_char (dtp
);
1604 if (c
== ',' && dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
)
1622 unget_char (dtp
, c
); /* Single null. */
1623 eat_separator (dtp
);
1636 /* Get the digit string that might be a repeat count. */
1640 c
= next_char (dtp
);
1641 if (c
== ',' && dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
)
1667 push_char (dtp
, 'e');
1669 c
= next_char (dtp
);
1673 push_char (dtp
, '\0');
1678 if (c
!= '\n' && c
!= ',' && c
!= '\r' && c
!= ';')
1679 unget_char (dtp
, c
);
1688 if (convert_integer (dtp
, -1, 0))
1691 /* Now get the number itself. */
1693 if ((c
= next_char (dtp
)) == EOF
)
1695 if (is_separator (c
))
1696 { /* Repeated null value. */
1697 unget_char (dtp
, c
);
1698 eat_separator (dtp
);
1702 if (c
!= '-' && c
!= '+')
1703 push_char (dtp
, '+');
1708 if ((c
= next_char (dtp
)) == EOF
)
1712 if (c
== ',' && dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
)
1715 if (!isdigit (c
) && c
!= '.')
1717 if (c
== 'i' || c
== 'I' || c
== 'n' || c
== 'N')
1736 c
= next_char (dtp
);
1737 if (c
== ',' && dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
)
1767 push_char (dtp
, 'e');
1769 c
= next_char (dtp
);
1778 push_char (dtp
, 'e');
1780 if ((c
= next_char (dtp
)) == EOF
)
1782 if (c
!= '+' && c
!= '-')
1783 push_char (dtp
, '+');
1787 c
= next_char (dtp
);
1797 c
= next_char (dtp
);
1815 unget_char (dtp
, c
);
1816 eat_separator (dtp
);
1817 push_char (dtp
, '\0');
1818 if (convert_real (dtp
, dest
, dtp
->u
.p
.saved_string
, length
))
1825 dtp
->u
.p
.saved_type
= BT_REAL
;
1829 l_push_char (dtp
, c
);
1832 /* Match INF and Infinity. */
1833 if (c
== 'i' || c
== 'I')
1835 c
= next_char (dtp
);
1836 l_push_char (dtp
, c
);
1837 if (c
!= 'n' && c
!= 'N')
1839 c
= next_char (dtp
);
1840 l_push_char (dtp
, c
);
1841 if (c
!= 'f' && c
!= 'F')
1843 c
= next_char (dtp
);
1844 l_push_char (dtp
, c
);
1845 if (!is_separator (c
) && (c
!= EOF
))
1847 if (c
!= 'i' && c
!= 'I')
1849 c
= next_char (dtp
);
1850 l_push_char (dtp
, c
);
1851 if (c
!= 'n' && c
!= 'N')
1853 c
= next_char (dtp
);
1854 l_push_char (dtp
, c
);
1855 if (c
!= 'i' && c
!= 'I')
1857 c
= next_char (dtp
);
1858 l_push_char (dtp
, c
);
1859 if (c
!= 't' && c
!= 'T')
1861 c
= next_char (dtp
);
1862 l_push_char (dtp
, c
);
1863 if (c
!= 'y' && c
!= 'Y')
1865 c
= next_char (dtp
);
1866 l_push_char (dtp
, c
);
1872 c
= next_char (dtp
);
1873 l_push_char (dtp
, c
);
1874 if (c
!= 'a' && c
!= 'A')
1876 c
= next_char (dtp
);
1877 l_push_char (dtp
, c
);
1878 if (c
!= 'n' && c
!= 'N')
1880 c
= next_char (dtp
);
1881 l_push_char (dtp
, c
);
1883 /* Match NAN(alphanum). */
1886 for (c
= next_char (dtp
); c
!= ')'; c
= next_char (dtp
))
1887 if (is_separator (c
))
1890 l_push_char (dtp
, c
);
1892 l_push_char (dtp
, ')');
1893 c
= next_char (dtp
);
1894 l_push_char (dtp
, c
);
1898 if (!is_separator (c
) && (c
!= EOF
))
1901 if (dtp
->u
.p
.namelist_mode
)
1903 if (c
== ' ' || c
=='\n' || c
== '\r')
1907 if ((c
= next_char (dtp
)) == EOF
)
1910 while (c
== ' ' || c
=='\n' || c
== '\r');
1912 l_push_char (dtp
, c
);
1921 push_char (dtp
, 'i');
1922 push_char (dtp
, 'n');
1923 push_char (dtp
, 'f');
1927 push_char (dtp
, 'n');
1928 push_char (dtp
, 'a');
1929 push_char (dtp
, 'n');
1933 unget_char (dtp
, c
);
1934 eat_separator (dtp
);
1935 push_char (dtp
, '\0');
1936 if (convert_infnan (dtp
, dest
, dtp
->u
.p
.saved_string
, length
))
1940 dtp
->u
.p
.saved_type
= BT_REAL
;
1944 if (dtp
->u
.p
.namelist_mode
)
1946 dtp
->u
.p
.nml_read_error
= 1;
1947 dtp
->u
.p
.line_buffer_enabled
= 1;
1948 dtp
->u
.p
.line_buffer_pos
= 0;
1954 if (nml_bad_return (dtp
, c
))
1967 snprintf (message
, MSGLEN
, "Bad real number in item %d of list input",
1968 dtp
->u
.p
.item_count
);
1970 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
, message
);
1974 /* Check the current type against the saved type to make sure they are
1975 compatible. Returns nonzero if incompatible. */
1978 check_type (st_parameter_dt
*dtp
, bt type
, int kind
)
1980 char message
[MSGLEN
];
1982 if (dtp
->u
.p
.saved_type
!= BT_UNKNOWN
&& dtp
->u
.p
.saved_type
!= type
)
1984 snprintf (message
, MSGLEN
, "Read type %s where %s was expected for item %d",
1985 type_name (dtp
->u
.p
.saved_type
), type_name (type
),
1986 dtp
->u
.p
.item_count
);
1988 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
, message
);
1992 if (dtp
->u
.p
.saved_type
== BT_UNKNOWN
|| dtp
->u
.p
.saved_type
== BT_CHARACTER
)
1995 if ((type
!= BT_COMPLEX
&& dtp
->u
.p
.saved_length
!= kind
)
1996 || (type
== BT_COMPLEX
&& dtp
->u
.p
.saved_length
!= kind
*2))
1998 snprintf (message
, MSGLEN
,
1999 "Read kind %d %s where kind %d is required for item %d",
2000 type
== BT_COMPLEX
? dtp
->u
.p
.saved_length
/ 2
2001 : dtp
->u
.p
.saved_length
,
2002 type_name (dtp
->u
.p
.saved_type
), kind
,
2003 dtp
->u
.p
.item_count
);
2005 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
, message
);
2013 /* Initialize the function pointers to select the correct versions of
2014 next_char and push_char depending on what we are doing. */
2017 set_workers (st_parameter_dt
*dtp
)
2019 if (dtp
->u
.p
.current_unit
->flags
.encoding
== ENCODING_UTF8
)
2021 dtp
->u
.p
.current_unit
->next_char_fn_ptr
= &next_char_utf8
;
2022 dtp
->u
.p
.current_unit
->push_char_fn_ptr
= &push_char4
;
2024 else if (is_internal_unit (dtp
))
2026 dtp
->u
.p
.current_unit
->next_char_fn_ptr
= &next_char_internal
;
2027 dtp
->u
.p
.current_unit
->push_char_fn_ptr
= &push_char_default
;
2031 dtp
->u
.p
.current_unit
->next_char_fn_ptr
= &next_char_default
;
2032 dtp
->u
.p
.current_unit
->push_char_fn_ptr
= &push_char_default
;
2037 /* Top level data transfer subroutine for list reads. Because we have
2038 to deal with repeat counts, the data item is always saved after
2039 reading, usually in the dtp->u.p.value[] array. If a repeat count is
2040 greater than one, we copy the data item multiple times. */
2043 list_formatted_read_scalar (st_parameter_dt
*dtp
, bt type
, void *p
,
2044 int kind
, size_t size
)
2050 dtp
->u
.p
.namelist_mode
= 0;
2052 /* Set the next_char and push_char worker functions. */
2055 if (dtp
->u
.p
.first_item
)
2057 dtp
->u
.p
.first_item
= 0;
2058 dtp
->u
.p
.input_complete
= 0;
2059 dtp
->u
.p
.repeat_count
= 1;
2060 dtp
->u
.p
.at_eol
= 0;
2062 if ((c
= eat_spaces (dtp
)) == EOF
)
2067 if (is_separator (c
))
2069 /* Found a null value. */
2070 dtp
->u
.p
.repeat_count
= 0;
2071 eat_separator (dtp
);
2073 /* Set end-of-line flag. */
2074 if (c
== '\n' || c
== '\r')
2076 dtp
->u
.p
.at_eol
= 1;
2077 if (finish_separator (dtp
) == LIBERROR_END
)
2089 if (dtp
->u
.p
.repeat_count
> 0)
2091 if (check_type (dtp
, type
, kind
))
2096 if (dtp
->u
.p
.input_complete
)
2099 if (dtp
->u
.p
.at_eol
)
2100 finish_separator (dtp
);
2104 /* Trailing spaces prior to end of line. */
2105 if (dtp
->u
.p
.at_eol
)
2106 finish_separator (dtp
);
2109 dtp
->u
.p
.saved_type
= BT_UNKNOWN
;
2110 dtp
->u
.p
.repeat_count
= 1;
2116 read_integer (dtp
, kind
);
2119 read_logical (dtp
, kind
);
2122 read_character (dtp
, kind
);
2125 read_real (dtp
, p
, kind
);
2126 /* Copy value back to temporary if needed. */
2127 if (dtp
->u
.p
.repeat_count
> 0)
2128 memcpy (dtp
->u
.p
.value
, p
, size
);
2131 read_complex (dtp
, p
, kind
, size
);
2132 /* Copy value back to temporary if needed. */
2133 if (dtp
->u
.p
.repeat_count
> 0)
2134 memcpy (dtp
->u
.p
.value
, p
, size
);
2137 internal_error (&dtp
->common
, "Bad type for list read");
2140 if (dtp
->u
.p
.saved_type
!= BT_CHARACTER
&& dtp
->u
.p
.saved_type
!= BT_UNKNOWN
)
2141 dtp
->u
.p
.saved_length
= size
;
2143 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
2147 switch (dtp
->u
.p
.saved_type
)
2151 if (dtp
->u
.p
.repeat_count
> 0)
2152 memcpy (p
, dtp
->u
.p
.value
, size
);
2157 memcpy (p
, dtp
->u
.p
.value
, size
);
2161 if (dtp
->u
.p
.saved_string
)
2163 m
= ((int) size
< dtp
->u
.p
.saved_used
)
2164 ? (int) size
: dtp
->u
.p
.saved_used
;
2166 q
= (gfc_char4_t
*) p
;
2167 r
= (gfc_char4_t
*) dtp
->u
.p
.saved_string
;
2168 if (dtp
->u
.p
.current_unit
->flags
.encoding
== ENCODING_UTF8
)
2169 for (i
= 0; i
< m
; i
++)
2174 memcpy (p
, dtp
->u
.p
.saved_string
, m
);
2176 for (i
= 0; i
< m
; i
++)
2181 /* Just delimiters encountered, nothing to copy but SPACE. */
2187 memset (((char *) p
) + m
, ' ', size
- m
);
2190 q
= (gfc_char4_t
*) p
;
2191 for (i
= m
; i
< (int) size
; i
++)
2192 q
[i
] = (unsigned char) ' ';
2201 internal_error (&dtp
->common
, "Bad type for list read");
2204 if (--dtp
->u
.p
.repeat_count
<= 0)
2208 if (err
== LIBERROR_END
)
2218 list_formatted_read (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
2219 size_t size
, size_t nelems
)
2223 size_t stride
= type
== BT_CHARACTER
?
2224 size
* GFC_SIZE_OF_CHAR_KIND(kind
) : size
;
2229 /* Big loop over all the elements. */
2230 for (elem
= 0; elem
< nelems
; elem
++)
2232 dtp
->u
.p
.item_count
++;
2233 err
= list_formatted_read_scalar (dtp
, type
, tmp
+ stride
*elem
,
2241 /* Finish a list read. */
2244 finish_list_read (st_parameter_dt
*dtp
)
2248 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
2250 if (dtp
->u
.p
.at_eol
)
2252 dtp
->u
.p
.at_eol
= 0;
2256 if (!is_internal_unit (dtp
))
2260 /* Set the next_char and push_char worker functions. */
2263 c
= next_char (dtp
);
2280 void namelist_read (st_parameter_dt *dtp)
2282 static void nml_match_name (char *name, int len)
2283 static int nml_query (st_parameter_dt *dtp)
2284 static int nml_get_obj_data (st_parameter_dt *dtp,
2285 namelist_info **prev_nl, char *, size_t)
2287 static void nml_untouch_nodes (st_parameter_dt *dtp)
2288 static namelist_info * find_nml_node (st_parameter_dt *dtp,
2290 static int nml_parse_qualifier(descriptor_dimension * ad,
2291 array_loop_spec * ls, int rank, char *)
2292 static void nml_touch_nodes (namelist_info * nl)
2293 static int nml_read_obj (namelist_info *nl, index_type offset,
2294 namelist_info **prev_nl, char *, size_t,
2295 index_type clow, index_type chigh)
2299 /* Inputs a rank-dimensional qualifier, which can contain
2300 singlets, doublets, triplets or ':' with the standard meanings. */
2303 nml_parse_qualifier (st_parameter_dt
*dtp
, descriptor_dimension
*ad
,
2304 array_loop_spec
*ls
, int rank
, bt nml_elem_type
,
2305 char *parse_err_msg
, size_t parse_err_msg_size
,
2312 int is_array_section
, is_char
;
2316 is_array_section
= 0;
2317 dtp
->u
.p
.expanded_read
= 0;
2319 /* See if this is a character substring qualifier we are looking for. */
2326 /* The next character in the stream should be the '('. */
2328 if ((c
= next_char (dtp
)) == EOF
)
2331 /* Process the qualifier, by dimension and triplet. */
2333 for (dim
=0; dim
< rank
; dim
++ )
2335 for (indx
=0; indx
<3; indx
++)
2341 /* Process a potential sign. */
2342 if ((c
= next_char (dtp
)) == EOF
)
2354 unget_char (dtp
, c
);
2358 /* Process characters up to the next ':' , ',' or ')'. */
2361 c
= next_char (dtp
);
2368 is_array_section
= 1;
2372 if ((c
==',' && dim
== rank
-1)
2373 || (c
==')' && dim
< rank
-1))
2376 snprintf (parse_err_msg
, parse_err_msg_size
,
2377 "Bad substring qualifier");
2379 snprintf (parse_err_msg
, parse_err_msg_size
,
2380 "Bad number of index fields");
2389 case ' ': case '\t': case '\r': case '\n':
2395 snprintf (parse_err_msg
, parse_err_msg_size
,
2396 "Bad character in substring qualifier");
2398 snprintf (parse_err_msg
, parse_err_msg_size
,
2399 "Bad character in index");
2403 if ((c
== ',' || c
== ')') && indx
== 0
2404 && dtp
->u
.p
.saved_string
== 0)
2407 snprintf (parse_err_msg
, parse_err_msg_size
,
2408 "Null substring qualifier");
2410 snprintf (parse_err_msg
, parse_err_msg_size
,
2411 "Null index field");
2415 if ((c
== ':' && indx
== 1 && dtp
->u
.p
.saved_string
== 0)
2416 || (indx
== 2 && dtp
->u
.p
.saved_string
== 0))
2419 snprintf (parse_err_msg
, parse_err_msg_size
,
2420 "Bad substring qualifier");
2422 snprintf (parse_err_msg
, parse_err_msg_size
,
2423 "Bad index triplet");
2427 if (is_char
&& !is_array_section
)
2429 snprintf (parse_err_msg
, parse_err_msg_size
,
2430 "Missing colon in substring qualifier");
2434 /* If '( : ? )' or '( ? : )' break and flag read failure. */
2436 if ((c
== ':' && indx
== 0 && dtp
->u
.p
.saved_string
== 0)
2437 || (indx
==1 && dtp
->u
.p
.saved_string
== 0))
2443 /* Now read the index. */
2444 if (convert_integer (dtp
, sizeof(index_type
), neg
))
2447 snprintf (parse_err_msg
, parse_err_msg_size
,
2448 "Bad integer substring qualifier");
2450 snprintf (parse_err_msg
, parse_err_msg_size
,
2451 "Bad integer in index");
2457 /* Feed the index values to the triplet arrays. */
2461 memcpy (&ls
[dim
].start
, dtp
->u
.p
.value
, sizeof(index_type
));
2463 memcpy (&ls
[dim
].end
, dtp
->u
.p
.value
, sizeof(index_type
));
2465 memcpy (&ls
[dim
].step
, dtp
->u
.p
.value
, sizeof(index_type
));
2468 /* Singlet or doublet indices. */
2469 if (c
==',' || c
==')')
2473 memcpy (&ls
[dim
].start
, dtp
->u
.p
.value
, sizeof(index_type
));
2475 /* If -std=f95/2003 or an array section is specified,
2476 do not allow excess data to be processed. */
2477 if (is_array_section
== 1
2478 || !(compile_options
.allow_std
& GFC_STD_GNU
)
2479 || nml_elem_type
== BT_DERIVED
)
2480 ls
[dim
].end
= ls
[dim
].start
;
2482 dtp
->u
.p
.expanded_read
= 1;
2485 /* Check for non-zero rank. */
2486 if (is_array_section
== 1 && ls
[dim
].start
!= ls
[dim
].end
)
2493 if (is_array_section
== 1 && dtp
->u
.p
.expanded_read
== 1)
2496 dtp
->u
.p
.expanded_read
= 0;
2497 for (i
= 0; i
< dim
; i
++)
2498 ls
[i
].end
= ls
[i
].start
;
2501 /* Check the values of the triplet indices. */
2502 if ((ls
[dim
].start
> GFC_DIMENSION_UBOUND(ad
[dim
]))
2503 || (ls
[dim
].start
< GFC_DIMENSION_LBOUND(ad
[dim
]))
2504 || (ls
[dim
].end
> GFC_DIMENSION_UBOUND(ad
[dim
]))
2505 || (ls
[dim
].end
< GFC_DIMENSION_LBOUND(ad
[dim
])))
2508 snprintf (parse_err_msg
, parse_err_msg_size
,
2509 "Substring out of range");
2511 snprintf (parse_err_msg
, parse_err_msg_size
,
2512 "Index %d out of range", dim
+ 1);
2516 if (((ls
[dim
].end
- ls
[dim
].start
) * ls
[dim
].step
< 0)
2517 || (ls
[dim
].step
== 0))
2519 snprintf (parse_err_msg
, parse_err_msg_size
,
2520 "Bad range in index %d", dim
+ 1);
2524 /* Initialise the loop index counter. */
2525 ls
[dim
].idx
= ls
[dim
].start
;
2532 /* The EOF error message is issued by hit_eof. Return true so that the
2533 caller does not use parse_err_msg and parse_err_msg_size to generate
2534 an unrelated error message. */
2538 dtp
->u
.p
.input_complete
= 1;
2546 extended_look_ahead (char *p
, char *q
)
2550 /* Scan ahead to find a '%' in the p string. */
2551 for(r
= p
, s
= q
; *r
&& *s
; s
++)
2552 if ((*s
== '%' || *s
== '+') && strcmp (r
+ 1, s
+ 1) == 0)
2559 strcmp_extended_type (char *p
, char *q
)
2563 for (r
= p
, s
= q
; *r
&& *s
; r
++, s
++)
2567 if (*r
== '%' && *s
== '+' && extended_look_ahead (r
, s
))
2576 static namelist_info
*
2577 find_nml_node (st_parameter_dt
*dtp
, char * var_name
)
2579 namelist_info
* t
= dtp
->u
.p
.ionml
;
2582 if (strcmp (var_name
, t
->var_name
) == 0)
2587 if (strcmp_extended_type (var_name
, t
->var_name
))
2597 /* Visits all the components of a derived type that have
2598 not explicitly been identified in the namelist input.
2599 touched is set and the loop specification initialised
2600 to default values */
2603 nml_touch_nodes (namelist_info
* nl
)
2605 index_type len
= strlen (nl
->var_name
) + 1;
2607 char * ext_name
= xmalloc (len
+ 1);
2608 memcpy (ext_name
, nl
->var_name
, len
-1);
2609 memcpy (ext_name
+ len
- 1, "%", 2);
2610 for (nl
= nl
->next
; nl
; nl
= nl
->next
)
2612 if (strncmp (nl
->var_name
, ext_name
, len
) == 0)
2615 for (dim
=0; dim
< nl
->var_rank
; dim
++)
2617 nl
->ls
[dim
].step
= 1;
2618 nl
->ls
[dim
].end
= GFC_DESCRIPTOR_UBOUND(nl
,dim
);
2619 nl
->ls
[dim
].start
= GFC_DESCRIPTOR_LBOUND(nl
,dim
);
2620 nl
->ls
[dim
].idx
= nl
->ls
[dim
].start
;
2630 /* Resets touched for the entire list of nml_nodes, ready for a
2634 nml_untouch_nodes (st_parameter_dt
*dtp
)
2637 for (t
= dtp
->u
.p
.ionml
; t
; t
= t
->next
)
2642 /* Attempts to input name to namelist name. Returns
2643 dtp->u.p.nml_read_error = 1 on no match. */
2646 nml_match_name (st_parameter_dt
*dtp
, const char *name
, index_type len
)
2651 dtp
->u
.p
.nml_read_error
= 0;
2652 for (i
= 0; i
< len
; i
++)
2654 c
= next_char (dtp
);
2655 if (c
== EOF
|| (tolower (c
) != tolower (name
[i
])))
2657 dtp
->u
.p
.nml_read_error
= 1;
2663 /* If the namelist read is from stdin, output the current state of the
2664 namelist to stdout. This is used to implement the non-standard query
2665 features, ? and =?. If c == '=' the full namelist is printed. Otherwise
2666 the names alone are printed. */
2669 nml_query (st_parameter_dt
*dtp
, char c
)
2671 gfc_unit
* temp_unit
;
2676 static const index_type endlen
= 2;
2677 static const char endl
[] = "\r\n";
2678 static const char nmlend
[] = "&end\r\n";
2680 static const index_type endlen
= 1;
2681 static const char endl
[] = "\n";
2682 static const char nmlend
[] = "&end\n";
2685 if (dtp
->u
.p
.current_unit
->unit_number
!= options
.stdin_unit
)
2688 /* Store the current unit and transfer to stdout. */
2690 temp_unit
= dtp
->u
.p
.current_unit
;
2691 dtp
->u
.p
.current_unit
= find_unit (options
.stdout_unit
);
2693 if (dtp
->u
.p
.current_unit
)
2695 dtp
->u
.p
.mode
= WRITING
;
2696 next_record (dtp
, 0);
2698 /* Write the namelist in its entirety. */
2701 namelist_write (dtp
);
2703 /* Or write the list of names. */
2707 /* "&namelist_name\n" */
2709 len
= dtp
->namelist_name_len
;
2710 p
= write_block (dtp
, len
- 1 + endlen
);
2714 memcpy ((char*)(p
+ 1), dtp
->namelist_name
, len
);
2715 memcpy ((char*)(p
+ len
+ 1), &endl
, endlen
);
2716 for (nl
= dtp
->u
.p
.ionml
; nl
; nl
= nl
->next
)
2720 len
= strlen (nl
->var_name
);
2721 p
= write_block (dtp
, len
+ endlen
);
2725 memcpy ((char*)(p
+ 1), nl
->var_name
, len
);
2726 memcpy ((char*)(p
+ len
+ 1), &endl
, endlen
);
2731 p
= write_block (dtp
, endlen
+ 4);
2734 memcpy (p
, &nmlend
, endlen
+ 4);
2737 /* Flush the stream to force immediate output. */
2739 fbuf_flush (dtp
->u
.p
.current_unit
, WRITING
);
2740 sflush (dtp
->u
.p
.current_unit
->s
);
2741 unlock_unit (dtp
->u
.p
.current_unit
);
2746 /* Restore the current unit. */
2748 dtp
->u
.p
.current_unit
= temp_unit
;
2749 dtp
->u
.p
.mode
= READING
;
2753 /* Reads and stores the input for the namelist object nl. For an array,
2754 the function loops over the ranges defined by the loop specification.
2755 This default to all the data or to the specification from a qualifier.
2756 nml_read_obj recursively calls itself to read derived types. It visits
2757 all its own components but only reads data for those that were touched
2758 when the name was parsed. If a read error is encountered, an attempt is
2759 made to return to read a new object name because the standard allows too
2760 little data to be available. On the other hand, too much data is an
2764 nml_read_obj (st_parameter_dt
*dtp
, namelist_info
* nl
, index_type offset
,
2765 namelist_info
**pprev_nl
, char *nml_err_msg
,
2766 size_t nml_err_msg_size
, index_type clow
, index_type chigh
)
2768 namelist_info
* cmp
;
2775 size_t obj_name_len
;
2778 /* If we have encountered a previous read error or this object has not been
2779 touched in name parsing, just return. */
2780 if (dtp
->u
.p
.nml_read_error
|| !nl
->touched
)
2783 dtp
->u
.p
.repeat_count
= 0;
2795 dlen
= size_from_real_kind (len
);
2799 dlen
= size_from_complex_kind (len
);
2803 dlen
= chigh
? (chigh
- clow
+ 1) : nl
->string_length
;
2812 /* Update the pointer to the data, using the current index vector */
2814 pdata
= (void*)(nl
->mem_pos
+ offset
);
2815 for (dim
= 0; dim
< nl
->var_rank
; dim
++)
2816 pdata
= (void*)(pdata
+ (nl
->ls
[dim
].idx
2817 - GFC_DESCRIPTOR_LBOUND(nl
,dim
))
2818 * GFC_DESCRIPTOR_STRIDE(nl
,dim
) * nl
->size
);
2820 /* If we are finished with the repeat count, try to read next value. */
2823 if (--dtp
->u
.p
.repeat_count
<= 0)
2825 if (dtp
->u
.p
.input_complete
)
2827 if (dtp
->u
.p
.at_eol
)
2828 finish_separator (dtp
);
2829 if (dtp
->u
.p
.input_complete
)
2832 dtp
->u
.p
.saved_type
= BT_UNKNOWN
;
2838 read_integer (dtp
, len
);
2842 read_logical (dtp
, len
);
2846 read_character (dtp
, len
);
2850 /* Need to copy data back from the real location to the temp in
2851 order to handle nml reads into arrays. */
2852 read_real (dtp
, pdata
, len
);
2853 memcpy (dtp
->u
.p
.value
, pdata
, dlen
);
2857 /* Same as for REAL, copy back to temp. */
2858 read_complex (dtp
, pdata
, len
, dlen
);
2859 memcpy (dtp
->u
.p
.value
, pdata
, dlen
);
2863 obj_name_len
= strlen (nl
->var_name
) + 1;
2864 obj_name
= xmalloc (obj_name_len
+1);
2865 memcpy (obj_name
, nl
->var_name
, obj_name_len
-1);
2866 memcpy (obj_name
+ obj_name_len
- 1, "%", 2);
2868 /* If reading a derived type, disable the expanded read warning
2869 since a single object can have multiple reads. */
2870 dtp
->u
.p
.expanded_read
= 0;
2872 /* Now loop over the components. */
2874 for (cmp
= nl
->next
;
2876 !strncmp (cmp
->var_name
, obj_name
, obj_name_len
);
2879 /* Jump over nested derived type by testing if the potential
2880 component name contains '%'. */
2881 if (strchr (cmp
->var_name
+ obj_name_len
, '%'))
2884 if (!nml_read_obj (dtp
, cmp
, (index_type
)(pdata
- nl
->mem_pos
),
2885 pprev_nl
, nml_err_msg
, nml_err_msg_size
,
2892 if (dtp
->u
.p
.input_complete
)
2903 snprintf (nml_err_msg
, nml_err_msg_size
,
2904 "Bad type for namelist object %s", nl
->var_name
);
2905 internal_error (&dtp
->common
, nml_err_msg
);
2910 /* The standard permits array data to stop short of the number of
2911 elements specified in the loop specification. In this case, we
2912 should be here with dtp->u.p.nml_read_error != 0. Control returns to
2913 nml_get_obj_data and an attempt is made to read object name. */
2916 if (dtp
->u
.p
.nml_read_error
)
2918 dtp
->u
.p
.expanded_read
= 0;
2922 if (dtp
->u
.p
.saved_type
== BT_UNKNOWN
)
2924 dtp
->u
.p
.expanded_read
= 0;
2928 switch (dtp
->u
.p
.saved_type
)
2935 memcpy (pdata
, dtp
->u
.p
.value
, dlen
);
2939 if (dlen
< dtp
->u
.p
.saved_used
)
2941 if (compile_options
.bounds_check
)
2943 snprintf (nml_err_msg
, nml_err_msg_size
,
2944 "Namelist object '%s' truncated on read.",
2946 generate_warning (&dtp
->common
, nml_err_msg
);
2951 m
= dtp
->u
.p
.saved_used
;
2953 if (dtp
->u
.p
.current_unit
->flags
.encoding
== ENCODING_UTF8
)
2955 gfc_char4_t
*q4
, *p4
= pdata
;
2958 q4
= (gfc_char4_t
*) dtp
->u
.p
.saved_string
;
2960 for (i
= 0; i
< m
; i
++)
2963 for (i
= 0; i
< dlen
- m
; i
++)
2964 *p4
++ = (gfc_char4_t
) ' ';
2968 pdata
= (void*)( pdata
+ clow
- 1 );
2969 memcpy (pdata
, dtp
->u
.p
.saved_string
, m
);
2971 memset ((void*)( pdata
+ m
), ' ', dlen
- m
);
2979 /* Warn if a non-standard expanded read occurs. A single read of a
2980 single object is acceptable. If a second read occurs, issue a warning
2981 and set the flag to zero to prevent further warnings. */
2982 if (dtp
->u
.p
.expanded_read
== 2)
2984 notify_std (&dtp
->common
, GFC_STD_GNU
, "Non-standard expanded namelist read.");
2985 dtp
->u
.p
.expanded_read
= 0;
2988 /* If the expanded read warning flag is set, increment it,
2989 indicating that a single read has occurred. */
2990 if (dtp
->u
.p
.expanded_read
>= 1)
2991 dtp
->u
.p
.expanded_read
++;
2993 /* Break out of loop if scalar. */
2997 /* Now increment the index vector. */
3002 for (dim
= 0; dim
< nl
->var_rank
; dim
++)
3004 nl
->ls
[dim
].idx
+= nml_carry
* nl
->ls
[dim
].step
;
3006 if (((nl
->ls
[dim
].step
> 0) && (nl
->ls
[dim
].idx
> nl
->ls
[dim
].end
))
3008 ((nl
->ls
[dim
].step
< 0) && (nl
->ls
[dim
].idx
< nl
->ls
[dim
].end
)))
3010 nl
->ls
[dim
].idx
= nl
->ls
[dim
].start
;
3014 } while (!nml_carry
);
3016 if (dtp
->u
.p
.repeat_count
> 1)
3018 snprintf (nml_err_msg
, nml_err_msg_size
,
3019 "Repeat count too large for namelist object %s", nl
->var_name
);
3029 /* Parses the object name, including array and substring qualifiers. It
3030 iterates over derived type components, touching those components and
3031 setting their loop specifications, if there is a qualifier. If the
3032 object is itself a derived type, its components and subcomponents are
3033 touched. nml_read_obj is called at the end and this reads the data in
3034 the manner specified by the object name. */
3037 nml_get_obj_data (st_parameter_dt
*dtp
, namelist_info
**pprev_nl
,
3038 char *nml_err_msg
, size_t nml_err_msg_size
)
3042 namelist_info
* first_nl
= NULL
;
3043 namelist_info
* root_nl
= NULL
;
3044 int dim
, parsed_rank
;
3045 int component_flag
, qualifier_flag
;
3046 index_type clow
, chigh
;
3047 int non_zero_rank_count
;
3049 /* Look for end of input or object name. If '?' or '=?' are encountered
3050 in stdin, print the node names or the namelist to stdout. */
3052 eat_separator (dtp
);
3053 if (dtp
->u
.p
.input_complete
)
3056 if (dtp
->u
.p
.at_eol
)
3057 finish_separator (dtp
);
3058 if (dtp
->u
.p
.input_complete
)
3061 if ((c
= next_char (dtp
)) == EOF
)
3066 if ((c
= next_char (dtp
)) == EOF
)
3070 snprintf (nml_err_msg
, nml_err_msg_size
,
3071 "namelist read: misplaced = sign");
3074 nml_query (dtp
, '=');
3078 nml_query (dtp
, '?');
3083 nml_match_name (dtp
, "end", 3);
3084 if (dtp
->u
.p
.nml_read_error
)
3086 snprintf (nml_err_msg
, nml_err_msg_size
,
3087 "namelist not terminated with / or &end");
3092 dtp
->u
.p
.input_complete
= 1;
3099 /* Untouch all nodes of the namelist and reset the flags that are set for
3100 derived type components. */
3102 nml_untouch_nodes (dtp
);
3105 non_zero_rank_count
= 0;
3107 /* Get the object name - should '!' and '\n' be permitted separators? */
3115 if (!is_separator (c
))
3116 push_char_default (dtp
, tolower(c
));
3117 if ((c
= next_char (dtp
)) == EOF
)
3120 while (!( c
=='=' || c
==' ' || c
=='\t' || c
=='(' || c
=='%' ));
3122 unget_char (dtp
, c
);
3124 /* Check that the name is in the namelist and get pointer to object.
3125 Three error conditions exist: (i) An attempt is being made to
3126 identify a non-existent object, following a failed data read or
3127 (ii) The object name does not exist or (iii) Too many data items
3128 are present for an object. (iii) gives the same error message
3131 push_char_default (dtp
, '\0');
3135 size_t var_len
= strlen (root_nl
->var_name
);
3137 = dtp
->u
.p
.saved_string
? strlen (dtp
->u
.p
.saved_string
) : 0;
3138 char ext_name
[var_len
+ saved_len
+ 1];
3140 memcpy (ext_name
, root_nl
->var_name
, var_len
);
3141 if (dtp
->u
.p
.saved_string
)
3142 memcpy (ext_name
+ var_len
, dtp
->u
.p
.saved_string
, saved_len
);
3143 ext_name
[var_len
+ saved_len
] = '\0';
3144 nl
= find_nml_node (dtp
, ext_name
);
3147 nl
= find_nml_node (dtp
, dtp
->u
.p
.saved_string
);
3151 if (dtp
->u
.p
.nml_read_error
&& *pprev_nl
)
3152 snprintf (nml_err_msg
, nml_err_msg_size
,
3153 "Bad data for namelist object %s", (*pprev_nl
)->var_name
);
3156 snprintf (nml_err_msg
, nml_err_msg_size
,
3157 "Cannot match namelist object name %s",
3158 dtp
->u
.p
.saved_string
);
3163 /* Get the length, data length, base pointer and rank of the variable.
3164 Set the default loop specification first. */
3166 for (dim
=0; dim
< nl
->var_rank
; dim
++)
3168 nl
->ls
[dim
].step
= 1;
3169 nl
->ls
[dim
].end
= GFC_DESCRIPTOR_UBOUND(nl
,dim
);
3170 nl
->ls
[dim
].start
= GFC_DESCRIPTOR_LBOUND(nl
,dim
);
3171 nl
->ls
[dim
].idx
= nl
->ls
[dim
].start
;
3174 /* Check to see if there is a qualifier: if so, parse it.*/
3176 if (c
== '(' && nl
->var_rank
)
3179 if (!nml_parse_qualifier (dtp
, nl
->dim
, nl
->ls
, nl
->var_rank
,
3180 nl
->type
, nml_err_msg
, nml_err_msg_size
,
3183 char *nml_err_msg_end
= strchr (nml_err_msg
, '\0');
3184 snprintf (nml_err_msg_end
,
3185 nml_err_msg_size
- (nml_err_msg_end
- nml_err_msg
),
3186 " for namelist variable %s", nl
->var_name
);
3189 if (parsed_rank
> 0)
3190 non_zero_rank_count
++;
3194 if ((c
= next_char (dtp
)) == EOF
)
3196 unget_char (dtp
, c
);
3198 else if (nl
->var_rank
> 0)
3199 non_zero_rank_count
++;
3201 /* Now parse a derived type component. The root namelist_info address
3202 is backed up, as is the previous component level. The component flag
3203 is set and the iteration is made by jumping back to get_name. */
3207 if (nl
->type
!= BT_DERIVED
)
3209 snprintf (nml_err_msg
, nml_err_msg_size
,
3210 "Attempt to get derived component for %s", nl
->var_name
);
3214 /* Don't move first_nl further in the list if a qualifier was found. */
3215 if ((*pprev_nl
== NULL
&& !qualifier_flag
) || !component_flag
)
3221 if ((c
= next_char (dtp
)) == EOF
)
3226 /* Parse a character qualifier, if present. chigh = 0 is a default
3227 that signals that the string length = string_length. */
3232 if (c
== '(' && nl
->type
== BT_CHARACTER
)
3234 descriptor_dimension chd
[1] = { {1, clow
, nl
->string_length
} };
3235 array_loop_spec ind
[1] = { {1, clow
, nl
->string_length
, 1} };
3237 if (!nml_parse_qualifier (dtp
, chd
, ind
, -1, nl
->type
,
3238 nml_err_msg
, nml_err_msg_size
, &parsed_rank
))
3240 char *nml_err_msg_end
= strchr (nml_err_msg
, '\0');
3241 snprintf (nml_err_msg_end
,
3242 nml_err_msg_size
- (nml_err_msg_end
- nml_err_msg
),
3243 " for namelist variable %s", nl
->var_name
);
3247 clow
= ind
[0].start
;
3250 if (ind
[0].step
!= 1)
3252 snprintf (nml_err_msg
, nml_err_msg_size
,
3253 "Step not allowed in substring qualifier"
3254 " for namelist object %s", nl
->var_name
);
3258 if ((c
= next_char (dtp
)) == EOF
)
3260 unget_char (dtp
, c
);
3263 /* Make sure no extraneous qualifiers are there. */
3267 snprintf (nml_err_msg
, nml_err_msg_size
,
3268 "Qualifier for a scalar or non-character namelist object %s",
3273 /* Make sure there is no more than one non-zero rank object. */
3274 if (non_zero_rank_count
> 1)
3276 snprintf (nml_err_msg
, nml_err_msg_size
,
3277 "Multiple sub-objects with non-zero rank in namelist object %s",
3279 non_zero_rank_count
= 0;
3283 /* According to the standard, an equal sign MUST follow an object name. The
3284 following is possibly lax - it allows comments, blank lines and so on to
3285 intervene. eat_spaces (dtp); c = next_char (dtp); would be compliant*/
3289 eat_separator (dtp
);
3290 if (dtp
->u
.p
.input_complete
)
3293 if (dtp
->u
.p
.at_eol
)
3294 finish_separator (dtp
);
3295 if (dtp
->u
.p
.input_complete
)
3298 if ((c
= next_char (dtp
)) == EOF
)
3303 snprintf (nml_err_msg
, nml_err_msg_size
,
3304 "Equal sign must follow namelist object name %s",
3308 /* If a derived type, touch its components and restore the root
3309 namelist_info if we have parsed a qualified derived type
3312 if (nl
->type
== BT_DERIVED
)
3313 nml_touch_nodes (nl
);
3317 if (first_nl
->var_rank
== 0)
3319 if (component_flag
&& qualifier_flag
)
3326 dtp
->u
.p
.nml_read_error
= 0;
3327 if (!nml_read_obj (dtp
, nl
, 0, pprev_nl
, nml_err_msg
, nml_err_msg_size
,
3335 /* The EOF error message is issued by hit_eof. Return true so that the
3336 caller does not use nml_err_msg and nml_err_msg_size to generate
3337 an unrelated error message. */
3340 dtp
->u
.p
.input_complete
= 1;
3341 unget_char (dtp
, c
);
3348 /* Entry point for namelist input. Goes through input until namelist name
3349 is matched. Then cycles through nml_get_obj_data until the input is
3350 completed or there is an error. */
3353 namelist_read (st_parameter_dt
*dtp
)
3356 char nml_err_msg
[200];
3358 /* Initialize the error string buffer just in case we get an unexpected fail
3359 somewhere and end up at nml_err_ret. */
3360 strcpy (nml_err_msg
, "Internal namelist read error");
3362 /* Pointer to the previously read object, in case attempt is made to read
3363 new object name. Should this fail, error message can give previous
3365 namelist_info
*prev_nl
= NULL
;
3367 dtp
->u
.p
.namelist_mode
= 1;
3368 dtp
->u
.p
.input_complete
= 0;
3369 dtp
->u
.p
.expanded_read
= 0;
3371 /* Set the next_char and push_char worker functions. */
3374 /* Look for &namelist_name . Skip all characters, testing for $nmlname.
3375 Exit on success or EOF. If '?' or '=?' encountered in stdin, print
3376 node names or namelist on stdout. */
3379 c
= next_char (dtp
);
3391 c
= next_char (dtp
);
3393 nml_query (dtp
, '=');
3395 unget_char (dtp
, c
);
3399 nml_query (dtp
, '?');
3409 /* Match the name of the namelist. */
3411 nml_match_name (dtp
, dtp
->namelist_name
, dtp
->namelist_name_len
);
3413 if (dtp
->u
.p
.nml_read_error
)
3416 /* A trailing space is required, we give a little latitude here, 10.9.1. */
3417 c
= next_char (dtp
);
3418 if (!is_separator(c
) && c
!= '!')
3420 unget_char (dtp
, c
);
3424 unget_char (dtp
, c
);
3425 eat_separator (dtp
);
3427 /* Ready to read namelist objects. If there is an error in input
3428 from stdin, output the error message and continue. */
3430 while (!dtp
->u
.p
.input_complete
)
3432 if (!nml_get_obj_data (dtp
, &prev_nl
, nml_err_msg
, sizeof nml_err_msg
))
3434 if (dtp
->u
.p
.current_unit
->unit_number
!= options
.stdin_unit
)
3436 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
, nml_err_msg
);
3439 /* Reset the previous namelist pointer if we know we are not going
3440 to be doing multiple reads within a single namelist object. */
3441 if (prev_nl
&& prev_nl
->var_rank
== 0)
3452 /* All namelist error calls return from here */
3455 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
, nml_err_msg
);