1 /* Copyright (C) 2002-2019 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/>. */
34 typedef unsigned char uchar
;
37 /* List directed input. Several parsing subroutines are practically
38 reimplemented from formatted input, the reason being that there are
39 all kinds of small differences between formatted and list directed
43 /* Subroutines for reading characters from the input. Because a
44 repeat count is ambiguous with an integer, we have to read the
45 whole digit string before seeing if there is a '*' which signals
46 the repeat count. Since we can have a lot of potential leading
47 zeros, we have to be able to back up by arbitrary amount. Because
48 the input might not be seekable, we have to buffer the data
51 #define CASE_DIGITS case '0': case '1': case '2': case '3': case '4': \
52 case '5': case '6': case '7': case '8': case '9'
54 #define CASE_SEPARATORS /* Fall through. */ \
55 case ' ': case ',': case '/': case '\n': \
56 case '\t': case '\r': case ';'
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 == ';' || \
62 (dtp->u.p.namelist_mode && c == '!'))
64 /* Maximum repeat count. Less than ten times the maximum signed int32. */
66 #define MAX_REPEAT 200000000
72 /* Wrappers for calling the current worker functions. */
74 #define next_char(dtp) ((dtp)->u.p.current_unit->next_char_fn_ptr (dtp))
75 #define push_char(dtp, c) ((dtp)->u.p.current_unit->push_char_fn_ptr (dtp, c))
77 /* Worker function to save a default KIND=1 character to a string
78 buffer, enlarging it as necessary. */
81 push_char_default (st_parameter_dt
*dtp
, int c
)
85 if (dtp
->u
.p
.saved_string
== NULL
)
87 /* Plain malloc should suffice here, zeroing not needed? */
88 dtp
->u
.p
.saved_string
= xcalloc (SCRATCH_SIZE
, 1);
89 dtp
->u
.p
.saved_length
= SCRATCH_SIZE
;
90 dtp
->u
.p
.saved_used
= 0;
93 if (dtp
->u
.p
.saved_used
>= dtp
->u
.p
.saved_length
)
95 dtp
->u
.p
.saved_length
= 2 * dtp
->u
.p
.saved_length
;
96 dtp
->u
.p
.saved_string
=
97 xrealloc (dtp
->u
.p
.saved_string
, dtp
->u
.p
.saved_length
);
100 dtp
->u
.p
.saved_string
[dtp
->u
.p
.saved_used
++] = (char) c
;
104 /* Worker function to save a KIND=4 character to a string buffer,
105 enlarging the buffer as necessary. */
107 push_char4 (st_parameter_dt
*dtp
, int c
)
109 gfc_char4_t
*p
= (gfc_char4_t
*) dtp
->u
.p
.saved_string
;
113 dtp
->u
.p
.saved_string
= xcalloc (SCRATCH_SIZE
, sizeof (gfc_char4_t
));
114 dtp
->u
.p
.saved_length
= SCRATCH_SIZE
;
115 dtp
->u
.p
.saved_used
= 0;
116 p
= (gfc_char4_t
*) dtp
->u
.p
.saved_string
;
119 if (dtp
->u
.p
.saved_used
>= dtp
->u
.p
.saved_length
)
121 dtp
->u
.p
.saved_length
= 2 * dtp
->u
.p
.saved_length
;
122 dtp
->u
.p
.saved_string
=
123 xrealloc (dtp
->u
.p
.saved_string
,
124 dtp
->u
.p
.saved_length
* sizeof (gfc_char4_t
));
125 p
= (gfc_char4_t
*) dtp
->u
.p
.saved_string
;
128 p
[dtp
->u
.p
.saved_used
++] = c
;
132 /* Free the input buffer if necessary. */
135 free_saved (st_parameter_dt
*dtp
)
137 if (dtp
->u
.p
.saved_string
== NULL
)
140 free (dtp
->u
.p
.saved_string
);
142 dtp
->u
.p
.saved_string
= NULL
;
143 dtp
->u
.p
.saved_used
= 0;
147 /* Free the line buffer if necessary. */
150 free_line (st_parameter_dt
*dtp
)
152 dtp
->u
.p
.line_buffer_pos
= 0;
153 dtp
->u
.p
.line_buffer_enabled
= 0;
155 if (dtp
->u
.p
.line_buffer
== NULL
)
158 free (dtp
->u
.p
.line_buffer
);
159 dtp
->u
.p
.line_buffer
= NULL
;
163 /* Unget saves the last character so when reading the next character,
164 we need to check to see if there is a character waiting. Similar,
165 if the line buffer is being used to read_logical, check it too. */
168 check_buffers (st_parameter_dt
*dtp
)
173 if (dtp
->u
.p
.current_unit
->last_char
!= EOF
- 1)
176 c
= dtp
->u
.p
.current_unit
->last_char
;
177 dtp
->u
.p
.current_unit
->last_char
= EOF
- 1;
181 /* Read from line_buffer if enabled. */
183 if (dtp
->u
.p
.line_buffer_enabled
)
187 c
= dtp
->u
.p
.line_buffer
[dtp
->u
.p
.line_buffer_pos
];
188 if (c
!= '\0' && dtp
->u
.p
.line_buffer_pos
< 64)
190 dtp
->u
.p
.line_buffer
[dtp
->u
.p
.line_buffer_pos
] = '\0';
191 dtp
->u
.p
.line_buffer_pos
++;
195 dtp
->u
.p
.line_buffer_pos
= 0;
196 dtp
->u
.p
.line_buffer_enabled
= 0;
200 dtp
->u
.p
.at_eol
= (c
== '\n' || c
== '\r' || c
== EOF
);
205 /* Worker function for default character encoded file. */
207 next_char_default (st_parameter_dt
*dtp
)
211 /* Always check the unget and line buffer first. */
212 if ((c
= check_buffers (dtp
)))
215 c
= fbuf_getc (dtp
->u
.p
.current_unit
);
216 if (c
!= EOF
&& is_stream_io (dtp
))
217 dtp
->u
.p
.current_unit
->strm_pos
++;
219 dtp
->u
.p
.at_eol
= (c
== '\n' || c
== EOF
);
224 /* Worker function for internal and array I/O units. */
226 next_char_internal (st_parameter_dt
*dtp
)
232 /* Always check the unget and line buffer first. */
233 if ((c
= check_buffers (dtp
)))
236 /* Handle the end-of-record and end-of-file conditions for
237 internal array unit. */
238 if (is_array_io (dtp
))
243 /* Check for "end-of-record" condition. */
244 if (dtp
->u
.p
.current_unit
->bytes_left
== 0)
249 record
= next_array_record (dtp
, dtp
->u
.p
.current_unit
->ls
,
252 /* Check for "end-of-file" condition. */
259 record
*= dtp
->u
.p
.current_unit
->recl
;
260 if (sseek (dtp
->u
.p
.current_unit
->s
, record
, SEEK_SET
) < 0)
263 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
268 /* Get the next character and handle end-of-record conditions. */
269 if (likely (dtp
->u
.p
.current_unit
->bytes_left
> 0))
271 if (unlikely (is_char4_unit(dtp
))) /* Check for kind=4 internal unit. */
272 length
= sread (dtp
->u
.p
.current_unit
->s
, &c
, 1);
276 length
= sread (dtp
->u
.p
.current_unit
->s
, &cc
, 1);
283 if (unlikely (length
< 0))
285 generate_error (&dtp
->common
, LIBERROR_OS
, NULL
);
289 if (is_array_io (dtp
))
291 /* Check whether we hit EOF. */
292 if (unlikely (length
== 0))
294 generate_error (&dtp
->common
, LIBERROR_INTERNAL_UNIT
, NULL
);
308 dtp
->u
.p
.current_unit
->bytes_left
--;
311 dtp
->u
.p
.at_eol
= (c
== '\n' || c
== EOF
);
316 /* Worker function for UTF encoded files. */
318 next_char_utf8 (st_parameter_dt
*dtp
)
320 static const uchar masks
[6] = { 0x7F, 0x1F, 0x0F, 0x07, 0x02, 0x01 };
321 static const uchar patns
[6] = { 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
325 /* Always check the unget and line buffer first. */
326 if (!(c
= check_buffers (dtp
)))
327 c
= fbuf_getc (dtp
->u
.p
.current_unit
);
332 /* The number of leading 1-bits in the first byte indicates how many
334 for (nb
= 2; nb
< 7; nb
++)
335 if ((c
& ~masks
[nb
-1]) == patns
[nb
-1])
340 c
= (c
& masks
[nb
-1]);
342 /* Decode the bytes read. */
343 for (i
= 1; i
< nb
; i
++)
345 gfc_char4_t n
= fbuf_getc (dtp
->u
.p
.current_unit
);
346 if ((n
& 0xC0) != 0x80)
348 c
= ((c
<< 6) + (n
& 0x3F));
351 /* Make sure the shortest possible encoding was used. */
352 if (c
<= 0x7F && nb
> 1) goto invalid
;
353 if (c
<= 0x7FF && nb
> 2) goto invalid
;
354 if (c
<= 0xFFFF && nb
> 3) goto invalid
;
355 if (c
<= 0x1FFFFF && nb
> 4) goto invalid
;
356 if (c
<= 0x3FFFFFF && nb
> 5) goto invalid
;
358 /* Make sure the character is valid. */
359 if (c
> 0x7FFFFFFF || (c
>= 0xD800 && c
<= 0xDFFF))
363 dtp
->u
.p
.at_eol
= (c
== '\n' || c
== (gfc_char4_t
) EOF
);
367 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
, "Invalid UTF-8 encoding");
368 return (gfc_char4_t
) '?';
371 /* Push a character back onto the input. */
374 unget_char (st_parameter_dt
*dtp
, int c
)
376 dtp
->u
.p
.current_unit
->last_char
= c
;
380 /* Skip over spaces in the input. Returns the nonspace character that
381 terminated the eating and also places it back on the input. */
384 eat_spaces (st_parameter_dt
*dtp
)
388 /* If internal character array IO, peak ahead and seek past spaces.
389 This is an optimization unique to character arrays with large
390 character lengths (PR38199). This code eliminates numerous calls
391 to next_character. */
392 if (is_array_io (dtp
) && (dtp
->u
.p
.current_unit
->last_char
== EOF
- 1))
394 gfc_offset offset
= stell (dtp
->u
.p
.current_unit
->s
);
397 if (is_char4_unit(dtp
)) /* kind=4 */
399 for (i
= 0; i
< dtp
->u
.p
.current_unit
->bytes_left
; i
++)
401 if (dtp
->internal_unit
[(offset
+ i
) * sizeof (gfc_char4_t
)]
408 for (i
= 0; i
< dtp
->u
.p
.current_unit
->bytes_left
; i
++)
410 if (dtp
->internal_unit
[offset
+ i
] != ' ')
417 sseek (dtp
->u
.p
.current_unit
->s
, offset
+ i
, SEEK_SET
);
418 dtp
->u
.p
.current_unit
->bytes_left
-= i
;
422 /* Now skip spaces, EOF and EOL are handled in next_char. */
425 while (c
!= EOF
&& (c
== ' ' || c
== '\r' || c
== '\t'));
432 /* This function reads characters through to the end of the current
433 line and just ignores them. Returns 0 for success and LIBERROR_END
437 eat_line (st_parameter_dt
*dtp
)
443 while (c
!= EOF
&& c
!= '\n');
450 /* Skip over a separator. Technically, we don't always eat the whole
451 separator. This is because if we've processed the last input item,
452 then a separator is unnecessary. Plus the fact that operating
453 systems usually deliver console input on a line basis.
455 The upshot is that if we see a newline as part of reading a
456 separator, we stop reading. If there are more input items, we
457 continue reading the separator with finish_separator() which takes
458 care of the fact that we may or may not have seen a comma as part
461 Returns 0 for success, and non-zero error code otherwise. */
464 eat_separator (st_parameter_dt
*dtp
)
470 dtp
->u
.p
.comma_flag
= 0;
472 if ((c
= next_char (dtp
)) == EOF
)
477 if (dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
)
484 dtp
->u
.p
.comma_flag
= 1;
489 dtp
->u
.p
.input_complete
= 1;
493 if ((n
= next_char(dtp
)) == EOF
)
503 if (dtp
->u
.p
.namelist_mode
)
507 if ((c
= next_char (dtp
)) == EOF
)
511 err
= eat_line (dtp
);
517 while (c
== '\n' || c
== '\r' || c
== ' ' || c
== '\t');
523 /* Eat a namelist comment. */
524 if (dtp
->u
.p
.namelist_mode
)
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
));
856 if (!dtp
->u
.p
.namelist_mode
)
863 return; /* Null value. */
866 /* Save the character in case it is the beginning
867 of the next object name. */
872 dtp
->u
.p
.saved_type
= BT_LOGICAL
;
873 dtp
->u
.p
.saved_length
= length
;
875 /* Eat trailing garbage. */
878 while (c
!= EOF
&& !is_separator (c
));
882 set_integer ((int *) dtp
->u
.p
.value
, v
, length
);
889 for(i
= 0; i
< 63; i
++)
894 /* All done if this is not a namelist read. */
895 if (!dtp
->u
.p
.namelist_mode
)
908 l_push_char (dtp
, c
);
911 dtp
->u
.p
.nml_read_error
= 1;
912 dtp
->u
.p
.line_buffer_enabled
= 1;
913 dtp
->u
.p
.line_buffer_pos
= 0;
921 if (nml_bad_return (dtp
, c
))
937 snprintf (message
, MSGLEN
, "Bad logical value while reading item %d",
938 dtp
->u
.p
.item_count
);
940 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
, message
);
945 dtp
->u
.p
.saved_type
= BT_LOGICAL
;
946 dtp
->u
.p
.saved_length
= length
;
947 set_integer ((int *) dtp
->u
.p
.value
, v
, length
);
953 /* Reading integers is tricky because we can actually be reading a
954 repeat count. We have to store the characters in a buffer because
955 we could be reading an integer that is larger than the default int
956 used for repeat counts. */
959 read_integer (st_parameter_dt
*dtp
, int length
)
961 char message
[MSGLEN
];
971 /* Fall through... */
974 if ((c
= next_char (dtp
)) == EOF
)
979 if (!dtp
->u
.p
.namelist_mode
)
982 CASE_SEPARATORS
: /* Single null. */
995 /* Take care of what may be a repeat count. */
1007 push_char (dtp
, '\0');
1011 if (!dtp
->u
.p
.namelist_mode
)
1014 CASE_SEPARATORS
: /* Not a repeat count. */
1024 if (convert_integer (dtp
, -1, 0))
1027 /* Get the real integer. */
1029 if ((c
= next_char (dtp
)) == EOF
)
1037 if (!dtp
->u
.p
.namelist_mode
)
1041 unget_char (dtp
, c
);
1042 eat_separator (dtp
);
1047 /* Fall through... */
1050 c
= next_char (dtp
);
1061 c
= next_char (dtp
);
1069 if (!dtp
->u
.p
.namelist_mode
)
1083 if (nml_bad_return (dtp
, c
))
1096 snprintf (message
, MSGLEN
, "Bad integer for item %d in list input",
1097 dtp
->u
.p
.item_count
);
1099 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
, message
);
1104 unget_char (dtp
, c
);
1105 eat_separator (dtp
);
1107 push_char (dtp
, '\0');
1108 if (convert_integer (dtp
, length
, negative
))
1115 dtp
->u
.p
.saved_type
= BT_INTEGER
;
1119 /* Read a character variable. */
1122 read_character (st_parameter_dt
*dtp
, int length
__attribute__ ((unused
)))
1124 char quote
, message
[MSGLEN
];
1127 quote
= ' '; /* Space means no quote character. */
1129 if ((c
= next_char (dtp
)) == EOF
)
1139 unget_char (dtp
, c
); /* NULL value. */
1140 eat_separator (dtp
);
1149 if (dtp
->u
.p
.namelist_mode
)
1151 unget_char (dtp
, c
);
1158 /* Deal with a possible repeat count. */
1162 c
= next_char (dtp
);
1171 unget_char (dtp
, c
);
1172 goto done
; /* String was only digits! */
1175 push_char (dtp
, '\0');
1180 goto get_string
; /* Not a repeat count after all. */
1185 if (convert_integer (dtp
, -1, 0))
1188 /* Now get the real string. */
1190 if ((c
= next_char (dtp
)) == EOF
)
1195 unget_char (dtp
, c
); /* Repeated NULL values. */
1196 eat_separator (dtp
);
1213 if ((c
= next_char (dtp
)) == EOF
)
1225 /* See if we have a doubled quote character or the end of
1228 if ((c
= next_char (dtp
)) == EOF
)
1232 push_char (dtp
, quote
);
1236 unget_char (dtp
, c
);
1242 unget_char (dtp
, c
);
1246 if (c
!= '\n' && c
!= '\r')
1256 /* At this point, we have to have a separator, or else the string is
1259 c
= next_char (dtp
);
1261 if (is_separator (c
) || c
== EOF
)
1263 unget_char (dtp
, c
);
1264 eat_separator (dtp
);
1265 dtp
->u
.p
.saved_type
= BT_CHARACTER
;
1270 snprintf (message
, MSGLEN
, "Invalid string input in item %d",
1271 dtp
->u
.p
.item_count
);
1272 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
, message
);
1284 /* Parse a component of a complex constant or a real number that we
1285 are sure is already there. This is a straight real number parser. */
1288 parse_real (st_parameter_dt
*dtp
, void *buffer
, int length
)
1290 char message
[MSGLEN
];
1293 if ((c
= next_char (dtp
)) == EOF
)
1296 if (c
== '-' || c
== '+')
1299 if ((c
= next_char (dtp
)) == EOF
)
1303 if (c
== ',' && dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
)
1306 if (!isdigit (c
) && c
!= '.')
1308 if (c
== 'i' || c
== 'I' || c
== 'n' || c
== 'N')
1316 seen_dp
= (c
== '.') ? 1 : 0;
1320 if ((c
= next_char (dtp
)) == EOF
)
1322 if (c
== ',' && dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
)
1344 push_char (dtp
, 'e');
1349 push_char (dtp
, 'e');
1351 if ((c
= next_char (dtp
)) == EOF
)
1356 if (!dtp
->u
.p
.namelist_mode
)
1369 if ((c
= next_char (dtp
)) == EOF
)
1371 if (c
!= '-' && c
!= '+')
1372 push_char (dtp
, '+');
1376 c
= next_char (dtp
);
1382 /* Extension: allow default exponent of 0 when omitted. */
1383 if (dtp
->common
.flags
& IOPARM_DT_DEC_EXT
)
1385 push_char (dtp
, '0');
1396 if ((c
= next_char (dtp
)) == EOF
)
1405 if (!dtp
->u
.p
.namelist_mode
)
1410 unget_char (dtp
, c
);
1419 unget_char (dtp
, c
);
1420 push_char (dtp
, '\0');
1422 m
= convert_real (dtp
, buffer
, dtp
->u
.p
.saved_string
, length
);
1428 unget_char (dtp
, c
);
1429 push_char (dtp
, '\0');
1431 m
= convert_infnan (dtp
, buffer
, dtp
->u
.p
.saved_string
, length
);
1437 /* Match INF and Infinity. */
1438 if ((c
== 'i' || c
== 'I')
1439 && ((c
= next_char (dtp
)) == 'n' || c
== 'N')
1440 && ((c
= next_char (dtp
)) == 'f' || c
== 'F'))
1442 c
= next_char (dtp
);
1443 if ((c
!= 'i' && c
!= 'I')
1444 || ((c
== 'i' || c
== 'I')
1445 && ((c
= next_char (dtp
)) == 'n' || c
== 'N')
1446 && ((c
= next_char (dtp
)) == 'i' || c
== 'I')
1447 && ((c
= next_char (dtp
)) == 't' || c
== 'T')
1448 && ((c
= next_char (dtp
)) == 'y' || c
== 'Y')
1449 && (c
= next_char (dtp
))))
1451 if (is_separator (c
) || (c
== EOF
))
1452 unget_char (dtp
, c
);
1453 push_char (dtp
, 'i');
1454 push_char (dtp
, 'n');
1455 push_char (dtp
, 'f');
1459 else if (((c
= next_char (dtp
)) == 'a' || c
== 'A')
1460 && ((c
= next_char (dtp
)) == 'n' || c
== 'N')
1461 && (c
= next_char (dtp
)))
1463 if (is_separator (c
) || (c
== EOF
))
1464 unget_char (dtp
, c
);
1465 push_char (dtp
, 'n');
1466 push_char (dtp
, 'a');
1467 push_char (dtp
, 'n');
1469 /* Match "NAN(alphanum)". */
1472 for ( ; c
!= ')'; c
= next_char (dtp
))
1473 if (is_separator (c
))
1476 c
= next_char (dtp
);
1477 if (is_separator (c
) || (c
== EOF
))
1478 unget_char (dtp
, c
);
1485 if (nml_bad_return (dtp
, c
))
1500 snprintf (message
, MSGLEN
, "Bad complex floating point "
1501 "number for item %d", dtp
->u
.p
.item_count
);
1503 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
, message
);
1509 /* Reading a complex number is straightforward because we can tell
1510 what it is right away. */
1513 read_complex (st_parameter_dt
*dtp
, void *dest
, int kind
, size_t size
)
1515 char message
[MSGLEN
];
1518 if (parse_repeat (dtp
))
1521 c
= next_char (dtp
);
1528 if (!dtp
->u
.p
.namelist_mode
)
1533 unget_char (dtp
, c
);
1534 eat_separator (dtp
);
1543 c
= next_char (dtp
);
1544 if (c
== '\n' || c
== '\r')
1547 unget_char (dtp
, c
);
1549 if (parse_real (dtp
, dest
, kind
))
1554 c
= next_char (dtp
);
1555 if (c
== '\n' || c
== '\r')
1558 unget_char (dtp
, c
);
1561 != (dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_POINT
? ',' : ';'))
1566 c
= next_char (dtp
);
1567 if (c
== '\n' || c
== '\r')
1570 unget_char (dtp
, c
);
1572 if (parse_real (dtp
, dest
+ size
/ 2, kind
))
1577 c
= next_char (dtp
);
1578 if (c
== '\n' || c
== '\r')
1581 unget_char (dtp
, c
);
1583 if (next_char (dtp
) != ')')
1586 c
= next_char (dtp
);
1587 if (!is_separator (c
) && (c
!= EOF
))
1590 unget_char (dtp
, c
);
1591 eat_separator (dtp
);
1594 dtp
->u
.p
.saved_type
= BT_COMPLEX
;
1599 if (nml_bad_return (dtp
, c
))
1612 snprintf (message
, MSGLEN
, "Bad complex value in item %d of list input",
1613 dtp
->u
.p
.item_count
);
1615 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
, message
);
1619 /* Parse a real number with a possible repeat count. */
1622 read_real (st_parameter_dt
*dtp
, void *dest
, int length
)
1624 char message
[MSGLEN
];
1631 c
= next_char (dtp
);
1632 if (c
== ',' && dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
)
1650 if (!dtp
->u
.p
.namelist_mode
)
1654 unget_char (dtp
, c
); /* Single null. */
1655 eat_separator (dtp
);
1668 /* Get the digit string that might be a repeat count. */
1672 c
= next_char (dtp
);
1673 if (c
== ',' && dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
)
1699 push_char (dtp
, 'e');
1701 c
= next_char (dtp
);
1705 push_char (dtp
, '\0');
1709 if (!dtp
->u
.p
.namelist_mode
)
1714 if (c
!= '\n' && c
!= ',' && c
!= '\r' && c
!= ';')
1715 unget_char (dtp
, c
);
1724 if (convert_integer (dtp
, -1, 0))
1727 /* Now get the number itself. */
1729 if ((c
= next_char (dtp
)) == EOF
)
1731 if (is_separator (c
))
1732 { /* Repeated null value. */
1733 unget_char (dtp
, c
);
1734 eat_separator (dtp
);
1738 if (c
!= '-' && c
!= '+')
1739 push_char (dtp
, '+');
1744 if ((c
= next_char (dtp
)) == EOF
)
1748 if (c
== ',' && dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
)
1751 if (!isdigit (c
) && c
!= '.')
1753 if (c
== 'i' || c
== 'I' || c
== 'n' || c
== 'N')
1772 c
= next_char (dtp
);
1773 if (c
== ',' && dtp
->u
.p
.current_unit
->decimal_status
== DECIMAL_COMMA
)
1782 if (!dtp
->u
.p
.namelist_mode
)
1807 push_char (dtp
, 'e');
1809 c
= next_char (dtp
);
1818 push_char (dtp
, 'e');
1820 if ((c
= next_char (dtp
)) == EOF
)
1822 if (c
!= '+' && c
!= '-')
1823 push_char (dtp
, '+');
1827 c
= next_char (dtp
);
1833 /* Extension: allow default exponent of 0 when omitted. */
1834 if (dtp
->common
.flags
& IOPARM_DT_DEC_EXT
)
1836 push_char (dtp
, '0');
1847 c
= next_char (dtp
);
1856 if (!dtp
->u
.p
.namelist_mode
)
1869 unget_char (dtp
, c
);
1870 eat_separator (dtp
);
1871 push_char (dtp
, '\0');
1872 if (convert_real (dtp
, dest
, dtp
->u
.p
.saved_string
, length
))
1879 dtp
->u
.p
.saved_type
= BT_REAL
;
1883 l_push_char (dtp
, c
);
1886 /* Match INF and Infinity. */
1887 if (c
== 'i' || c
== 'I')
1889 c
= next_char (dtp
);
1890 l_push_char (dtp
, c
);
1891 if (c
!= 'n' && c
!= 'N')
1893 c
= next_char (dtp
);
1894 l_push_char (dtp
, c
);
1895 if (c
!= 'f' && c
!= 'F')
1897 c
= next_char (dtp
);
1898 l_push_char (dtp
, c
);
1899 if (!is_separator (c
) && (c
!= EOF
))
1901 if (c
!= 'i' && c
!= 'I')
1903 c
= next_char (dtp
);
1904 l_push_char (dtp
, c
);
1905 if (c
!= 'n' && c
!= 'N')
1907 c
= next_char (dtp
);
1908 l_push_char (dtp
, c
);
1909 if (c
!= 'i' && c
!= 'I')
1911 c
= next_char (dtp
);
1912 l_push_char (dtp
, c
);
1913 if (c
!= 't' && c
!= 'T')
1915 c
= next_char (dtp
);
1916 l_push_char (dtp
, c
);
1917 if (c
!= 'y' && c
!= 'Y')
1919 c
= next_char (dtp
);
1920 l_push_char (dtp
, c
);
1926 c
= next_char (dtp
);
1927 l_push_char (dtp
, c
);
1928 if (c
!= 'a' && c
!= 'A')
1930 c
= next_char (dtp
);
1931 l_push_char (dtp
, c
);
1932 if (c
!= 'n' && c
!= 'N')
1934 c
= next_char (dtp
);
1935 l_push_char (dtp
, c
);
1937 /* Match NAN(alphanum). */
1940 for (c
= next_char (dtp
); c
!= ')'; c
= next_char (dtp
))
1941 if (is_separator (c
))
1944 l_push_char (dtp
, c
);
1946 l_push_char (dtp
, ')');
1947 c
= next_char (dtp
);
1948 l_push_char (dtp
, c
);
1952 if (!is_separator (c
) && (c
!= EOF
))
1955 if (dtp
->u
.p
.namelist_mode
)
1957 if (c
== ' ' || c
=='\n' || c
== '\r')
1961 if ((c
= next_char (dtp
)) == EOF
)
1964 while (c
== ' ' || c
=='\n' || c
== '\r');
1966 l_push_char (dtp
, c
);
1975 push_char (dtp
, 'i');
1976 push_char (dtp
, 'n');
1977 push_char (dtp
, 'f');
1981 push_char (dtp
, 'n');
1982 push_char (dtp
, 'a');
1983 push_char (dtp
, 'n');
1987 unget_char (dtp
, c
);
1988 eat_separator (dtp
);
1989 push_char (dtp
, '\0');
1990 if (convert_infnan (dtp
, dest
, dtp
->u
.p
.saved_string
, length
))
1994 dtp
->u
.p
.saved_type
= BT_REAL
;
1998 if (dtp
->u
.p
.namelist_mode
)
2000 dtp
->u
.p
.nml_read_error
= 1;
2001 dtp
->u
.p
.line_buffer_enabled
= 1;
2002 dtp
->u
.p
.line_buffer_pos
= 0;
2008 if (nml_bad_return (dtp
, c
))
2023 snprintf (message
, MSGLEN
, "Bad real number in item %d of list input",
2024 dtp
->u
.p
.item_count
);
2026 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
, message
);
2030 /* Check the current type against the saved type to make sure they are
2031 compatible. Returns nonzero if incompatible. */
2034 check_type (st_parameter_dt
*dtp
, bt type
, int kind
)
2036 char message
[MSGLEN
];
2038 if (dtp
->u
.p
.saved_type
!= BT_UNKNOWN
&& dtp
->u
.p
.saved_type
!= type
)
2040 snprintf (message
, MSGLEN
, "Read type %s where %s was expected for item %d",
2041 type_name (dtp
->u
.p
.saved_type
), type_name (type
),
2042 dtp
->u
.p
.item_count
);
2044 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
, message
);
2048 if (dtp
->u
.p
.saved_type
== BT_UNKNOWN
|| dtp
->u
.p
.saved_type
== BT_CHARACTER
)
2051 if ((type
!= BT_COMPLEX
&& dtp
->u
.p
.saved_length
!= kind
)
2052 || (type
== BT_COMPLEX
&& dtp
->u
.p
.saved_length
!= kind
*2))
2054 snprintf (message
, MSGLEN
,
2055 "Read kind %d %s where kind %d is required for item %d",
2056 type
== BT_COMPLEX
? dtp
->u
.p
.saved_length
/ 2
2057 : dtp
->u
.p
.saved_length
,
2058 type_name (dtp
->u
.p
.saved_type
), kind
,
2059 dtp
->u
.p
.item_count
);
2061 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
, message
);
2069 /* Initialize the function pointers to select the correct versions of
2070 next_char and push_char depending on what we are doing. */
2073 set_workers (st_parameter_dt
*dtp
)
2075 if (dtp
->u
.p
.current_unit
->flags
.encoding
== ENCODING_UTF8
)
2077 dtp
->u
.p
.current_unit
->next_char_fn_ptr
= &next_char_utf8
;
2078 dtp
->u
.p
.current_unit
->push_char_fn_ptr
= &push_char4
;
2080 else if (is_internal_unit (dtp
))
2082 dtp
->u
.p
.current_unit
->next_char_fn_ptr
= &next_char_internal
;
2083 dtp
->u
.p
.current_unit
->push_char_fn_ptr
= &push_char_default
;
2087 dtp
->u
.p
.current_unit
->next_char_fn_ptr
= &next_char_default
;
2088 dtp
->u
.p
.current_unit
->push_char_fn_ptr
= &push_char_default
;
2093 /* Top level data transfer subroutine for list reads. Because we have
2094 to deal with repeat counts, the data item is always saved after
2095 reading, usually in the dtp->u.p.value[] array. If a repeat count is
2096 greater than one, we copy the data item multiple times. */
2099 list_formatted_read_scalar (st_parameter_dt
*dtp
, bt type
, void *p
,
2100 int kind
, size_t size
)
2107 /* Set the next_char and push_char worker functions. */
2110 if (dtp
->u
.p
.first_item
)
2112 dtp
->u
.p
.first_item
= 0;
2113 dtp
->u
.p
.input_complete
= 0;
2114 dtp
->u
.p
.repeat_count
= 1;
2115 dtp
->u
.p
.at_eol
= 0;
2117 if ((c
= eat_spaces (dtp
)) == EOF
)
2122 if (is_separator (c
))
2124 /* Found a null value. */
2125 dtp
->u
.p
.repeat_count
= 0;
2126 eat_separator (dtp
);
2128 /* Set end-of-line flag. */
2129 if (c
== '\n' || c
== '\r')
2131 dtp
->u
.p
.at_eol
= 1;
2132 if (finish_separator (dtp
) == LIBERROR_END
)
2144 if (dtp
->u
.p
.repeat_count
> 0)
2146 if (check_type (dtp
, type
, kind
))
2151 if (dtp
->u
.p
.input_complete
)
2154 if (dtp
->u
.p
.at_eol
)
2155 finish_separator (dtp
);
2159 /* Trailing spaces prior to end of line. */
2160 if (dtp
->u
.p
.at_eol
)
2161 finish_separator (dtp
);
2164 dtp
->u
.p
.saved_type
= BT_UNKNOWN
;
2165 dtp
->u
.p
.repeat_count
= 1;
2171 read_integer (dtp
, kind
);
2174 read_logical (dtp
, kind
);
2177 read_character (dtp
, kind
);
2180 read_real (dtp
, p
, kind
);
2181 /* Copy value back to temporary if needed. */
2182 if (dtp
->u
.p
.repeat_count
> 0)
2183 memcpy (dtp
->u
.p
.value
, p
, size
);
2186 read_complex (dtp
, p
, kind
, size
);
2187 /* Copy value back to temporary if needed. */
2188 if (dtp
->u
.p
.repeat_count
> 0)
2189 memcpy (dtp
->u
.p
.value
, p
, size
);
2193 int unit
= dtp
->u
.p
.current_unit
->unit_number
;
2194 char iotype
[] = "LISTDIRECTED";
2195 gfc_charlen_type iotype_len
= 12;
2196 char tmp_iomsg
[IOMSG_LEN
] = "";
2198 gfc_charlen_type child_iomsg_len
;
2200 int *child_iostat
= NULL
;
2201 gfc_full_array_i4 vlist
;
2203 GFC_DESCRIPTOR_DATA(&vlist
) = NULL
;
2204 GFC_DIMENSION_SET(vlist
.dim
[0],1, 0, 0);
2206 /* Set iostat, intent(out). */
2208 child_iostat
= (dtp
->common
.flags
& IOPARM_HAS_IOSTAT
) ?
2209 dtp
->common
.iostat
: &noiostat
;
2211 /* Set iomsge, intent(inout). */
2212 if (dtp
->common
.flags
& IOPARM_HAS_IOMSG
)
2214 child_iomsg
= dtp
->common
.iomsg
;
2215 child_iomsg_len
= dtp
->common
.iomsg_len
;
2219 child_iomsg
= tmp_iomsg
;
2220 child_iomsg_len
= IOMSG_LEN
;
2223 /* Call the user defined formatted READ procedure. */
2224 dtp
->u
.p
.current_unit
->child_dtio
++;
2225 dtp
->u
.p
.fdtio_ptr (p
, &unit
, iotype
, &vlist
,
2226 child_iostat
, child_iomsg
,
2227 iotype_len
, child_iomsg_len
);
2228 dtp
->u
.p
.child_saved_iostat
= *child_iostat
;
2229 dtp
->u
.p
.current_unit
->child_dtio
--;
2233 internal_error (&dtp
->common
, "Bad type for list read");
2236 if (dtp
->u
.p
.saved_type
!= BT_CHARACTER
&& dtp
->u
.p
.saved_type
!= BT_UNKNOWN
)
2237 dtp
->u
.p
.saved_length
= size
;
2239 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
2243 switch (dtp
->u
.p
.saved_type
)
2247 if (dtp
->u
.p
.repeat_count
> 0)
2248 memcpy (p
, dtp
->u
.p
.value
, size
);
2253 memcpy (p
, dtp
->u
.p
.value
, size
);
2257 if (dtp
->u
.p
.saved_string
)
2259 m
= (size
< (size_t) dtp
->u
.p
.saved_used
)
2260 ? size
: (size_t) dtp
->u
.p
.saved_used
;
2262 q
= (gfc_char4_t
*) p
;
2263 r
= (gfc_char4_t
*) dtp
->u
.p
.saved_string
;
2264 if (dtp
->u
.p
.current_unit
->flags
.encoding
== ENCODING_UTF8
)
2265 for (size_t i
= 0; i
< m
; i
++)
2270 memcpy (p
, dtp
->u
.p
.saved_string
, m
);
2272 for (size_t i
= 0; i
< m
; i
++)
2277 /* Just delimiters encountered, nothing to copy but SPACE. */
2283 memset (((char *) p
) + m
, ' ', size
- m
);
2286 q
= (gfc_char4_t
*) p
;
2287 for (size_t i
= m
; i
< size
; i
++)
2288 q
[i
] = (unsigned char) ' ';
2297 internal_error (&dtp
->common
, "Bad type for list read");
2300 if (--dtp
->u
.p
.repeat_count
<= 0)
2304 /* err may have been set above from finish_separator, so if it is set
2305 trigger the hit_eof. The hit_eof will set bits in common.flags. */
2306 if (err
== LIBERROR_END
)
2311 /* Now we check common.flags for any errors that could have occurred in
2312 a READ elsewhere such as in read_integer. */
2313 err
= dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
;
2314 fbuf_flush_list (dtp
->u
.p
.current_unit
, LIST_READING
);
2320 list_formatted_read (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
2321 size_t size
, size_t nelems
)
2325 size_t stride
= type
== BT_CHARACTER
?
2326 size
* GFC_SIZE_OF_CHAR_KIND(kind
) : size
;
2331 /* Big loop over all the elements. */
2332 for (elem
= 0; elem
< nelems
; elem
++)
2334 dtp
->u
.p
.item_count
++;
2335 err
= list_formatted_read_scalar (dtp
, type
, tmp
+ stride
*elem
,
2343 /* Finish a list read. */
2346 finish_list_read (st_parameter_dt
*dtp
)
2350 fbuf_flush (dtp
->u
.p
.current_unit
, dtp
->u
.p
.mode
);
2352 if (dtp
->u
.p
.at_eol
)
2354 dtp
->u
.p
.at_eol
= 0;
2358 if (!is_internal_unit (dtp
))
2362 /* Set the next_char and push_char worker functions. */
2365 if (likely (dtp
->u
.p
.child_saved_iostat
== LIBERROR_OK
))
2367 c
= next_char (dtp
);
2385 void namelist_read (st_parameter_dt *dtp)
2387 static void nml_match_name (char *name, int len)
2388 static int nml_query (st_parameter_dt *dtp)
2389 static int nml_get_obj_data (st_parameter_dt *dtp,
2390 namelist_info **prev_nl, char *, size_t)
2392 static void nml_untouch_nodes (st_parameter_dt *dtp)
2393 static namelist_info *find_nml_node (st_parameter_dt *dtp,
2395 static int nml_parse_qualifier(descriptor_dimension *ad,
2396 array_loop_spec *ls, int rank, char *)
2397 static void nml_touch_nodes (namelist_info *nl)
2398 static int nml_read_obj (namelist_info *nl, index_type offset,
2399 namelist_info **prev_nl, char *, size_t,
2400 index_type clow, index_type chigh)
2404 /* Inputs a rank-dimensional qualifier, which can contain
2405 singlets, doublets, triplets or ':' with the standard meanings. */
2408 nml_parse_qualifier (st_parameter_dt
*dtp
, descriptor_dimension
*ad
,
2409 array_loop_spec
*ls
, int rank
, bt nml_elem_type
,
2410 char *parse_err_msg
, size_t parse_err_msg_size
,
2417 int is_array_section
, is_char
;
2421 is_array_section
= 0;
2422 dtp
->u
.p
.expanded_read
= 0;
2424 /* See if this is a character substring qualifier we are looking for. */
2431 /* The next character in the stream should be the '('. */
2433 if ((c
= next_char (dtp
)) == EOF
)
2436 /* Process the qualifier, by dimension and triplet. */
2438 for (dim
=0; dim
< rank
; dim
++ )
2440 for (indx
=0; indx
<3; indx
++)
2446 /* Process a potential sign. */
2447 if ((c
= next_char (dtp
)) == EOF
)
2459 unget_char (dtp
, c
);
2463 /* Process characters up to the next ':' , ',' or ')'. */
2466 c
= next_char (dtp
);
2473 is_array_section
= 1;
2477 if ((c
==',' && dim
== rank
-1)
2478 || (c
==')' && dim
< rank
-1))
2481 snprintf (parse_err_msg
, parse_err_msg_size
,
2482 "Bad substring qualifier");
2484 snprintf (parse_err_msg
, parse_err_msg_size
,
2485 "Bad number of index fields");
2494 case ' ': case '\t': case '\r': case '\n':
2500 snprintf (parse_err_msg
, parse_err_msg_size
,
2501 "Bad character in substring qualifier");
2503 snprintf (parse_err_msg
, parse_err_msg_size
,
2504 "Bad character in index");
2508 if ((c
== ',' || c
== ')') && indx
== 0
2509 && dtp
->u
.p
.saved_string
== 0)
2512 snprintf (parse_err_msg
, parse_err_msg_size
,
2513 "Null substring qualifier");
2515 snprintf (parse_err_msg
, parse_err_msg_size
,
2516 "Null index field");
2520 if ((c
== ':' && indx
== 1 && dtp
->u
.p
.saved_string
== 0)
2521 || (indx
== 2 && dtp
->u
.p
.saved_string
== 0))
2524 snprintf (parse_err_msg
, parse_err_msg_size
,
2525 "Bad substring qualifier");
2527 snprintf (parse_err_msg
, parse_err_msg_size
,
2528 "Bad index triplet");
2532 if (is_char
&& !is_array_section
)
2534 snprintf (parse_err_msg
, parse_err_msg_size
,
2535 "Missing colon in substring qualifier");
2539 /* If '( : ? )' or '( ? : )' break and flag read failure. */
2541 if ((c
== ':' && indx
== 0 && dtp
->u
.p
.saved_string
== 0)
2542 || (indx
==1 && dtp
->u
.p
.saved_string
== 0))
2548 /* Now read the index. */
2549 if (convert_integer (dtp
, sizeof(index_type
), neg
))
2552 snprintf (parse_err_msg
, parse_err_msg_size
,
2553 "Bad integer substring qualifier");
2555 snprintf (parse_err_msg
, parse_err_msg_size
,
2556 "Bad integer in index");
2562 /* Feed the index values to the triplet arrays. */
2566 memcpy (&ls
[dim
].start
, dtp
->u
.p
.value
, sizeof(index_type
));
2568 memcpy (&ls
[dim
].end
, dtp
->u
.p
.value
, sizeof(index_type
));
2570 memcpy (&ls
[dim
].step
, dtp
->u
.p
.value
, sizeof(index_type
));
2573 /* Singlet or doublet indices. */
2574 if (c
==',' || c
==')')
2578 memcpy (&ls
[dim
].start
, dtp
->u
.p
.value
, sizeof(index_type
));
2580 /* If -std=f95/2003 or an array section is specified,
2581 do not allow excess data to be processed. */
2582 if (is_array_section
== 1
2583 || !(compile_options
.allow_std
& GFC_STD_GNU
)
2584 || nml_elem_type
== BT_DERIVED
)
2585 ls
[dim
].end
= ls
[dim
].start
;
2587 dtp
->u
.p
.expanded_read
= 1;
2590 /* Check for non-zero rank. */
2591 if (is_array_section
== 1 && ls
[dim
].start
!= ls
[dim
].end
)
2598 if (is_array_section
== 1 && dtp
->u
.p
.expanded_read
== 1)
2601 dtp
->u
.p
.expanded_read
= 0;
2602 for (i
= 0; i
< dim
; i
++)
2603 ls
[i
].end
= ls
[i
].start
;
2606 /* Check the values of the triplet indices. */
2607 if ((ls
[dim
].start
> GFC_DIMENSION_UBOUND(ad
[dim
]))
2608 || (ls
[dim
].start
< GFC_DIMENSION_LBOUND(ad
[dim
]))
2609 || (ls
[dim
].end
> GFC_DIMENSION_UBOUND(ad
[dim
]))
2610 || (ls
[dim
].end
< GFC_DIMENSION_LBOUND(ad
[dim
])))
2613 snprintf (parse_err_msg
, parse_err_msg_size
,
2614 "Substring out of range");
2616 snprintf (parse_err_msg
, parse_err_msg_size
,
2617 "Index %d out of range", dim
+ 1);
2621 if (((ls
[dim
].end
- ls
[dim
].start
) * ls
[dim
].step
< 0)
2622 || (ls
[dim
].step
== 0))
2624 snprintf (parse_err_msg
, parse_err_msg_size
,
2625 "Bad range in index %d", dim
+ 1);
2629 /* Initialise the loop index counter. */
2630 ls
[dim
].idx
= ls
[dim
].start
;
2637 /* The EOF error message is issued by hit_eof. Return true so that the
2638 caller does not use parse_err_msg and parse_err_msg_size to generate
2639 an unrelated error message. */
2643 dtp
->u
.p
.input_complete
= 1;
2651 extended_look_ahead (char *p
, char *q
)
2655 /* Scan ahead to find a '%' in the p string. */
2656 for(r
= p
, s
= q
; *r
&& *s
; s
++)
2657 if ((*s
== '%' || *s
== '+') && strcmp (r
+ 1, s
+ 1) == 0)
2664 strcmp_extended_type (char *p
, char *q
)
2668 for (r
= p
, s
= q
; *r
&& *s
; r
++, s
++)
2672 if (*r
== '%' && *s
== '+' && extended_look_ahead (r
, s
))
2681 static namelist_info
*
2682 find_nml_node (st_parameter_dt
*dtp
, char *var_name
)
2684 namelist_info
*t
= dtp
->u
.p
.ionml
;
2687 if (strcmp (var_name
, t
->var_name
) == 0)
2692 if (strcmp_extended_type (var_name
, t
->var_name
))
2702 /* Visits all the components of a derived type that have
2703 not explicitly been identified in the namelist input.
2704 touched is set and the loop specification initialised
2705 to default values */
2708 nml_touch_nodes (namelist_info
*nl
)
2710 index_type len
= strlen (nl
->var_name
) + 1;
2712 char *ext_name
= xmalloc (len
+ 1);
2713 memcpy (ext_name
, nl
->var_name
, len
-1);
2714 memcpy (ext_name
+ len
- 1, "%", 2);
2715 for (nl
= nl
->next
; nl
; nl
= nl
->next
)
2717 if (strncmp (nl
->var_name
, ext_name
, len
) == 0)
2720 for (dim
=0; dim
< nl
->var_rank
; dim
++)
2722 nl
->ls
[dim
].step
= 1;
2723 nl
->ls
[dim
].end
= GFC_DESCRIPTOR_UBOUND(nl
,dim
);
2724 nl
->ls
[dim
].start
= GFC_DESCRIPTOR_LBOUND(nl
,dim
);
2725 nl
->ls
[dim
].idx
= nl
->ls
[dim
].start
;
2735 /* Resets touched for the entire list of nml_nodes, ready for a
2739 nml_untouch_nodes (st_parameter_dt
*dtp
)
2742 for (t
= dtp
->u
.p
.ionml
; t
; t
= t
->next
)
2747 /* Attempts to input name to namelist name. Returns
2748 dtp->u.p.nml_read_error = 1 on no match. */
2751 nml_match_name (st_parameter_dt
*dtp
, const char *name
, index_type len
)
2756 dtp
->u
.p
.nml_read_error
= 0;
2757 for (i
= 0; i
< len
; i
++)
2759 c
= next_char (dtp
);
2760 if (c
== EOF
|| (tolower (c
) != tolower (name
[i
])))
2762 dtp
->u
.p
.nml_read_error
= 1;
2768 /* If the namelist read is from stdin, output the current state of the
2769 namelist to stdout. This is used to implement the non-standard query
2770 features, ? and =?. If c == '=' the full namelist is printed. Otherwise
2771 the names alone are printed. */
2774 nml_query (st_parameter_dt
*dtp
, char c
)
2776 gfc_unit
*temp_unit
;
2781 static const index_type endlen
= 2;
2782 static const char endl
[] = "\r\n";
2783 static const char nmlend
[] = "&end\r\n";
2785 static const index_type endlen
= 1;
2786 static const char endl
[] = "\n";
2787 static const char nmlend
[] = "&end\n";
2790 if (dtp
->u
.p
.current_unit
->unit_number
!= options
.stdin_unit
)
2793 /* Store the current unit and transfer to stdout. */
2795 temp_unit
= dtp
->u
.p
.current_unit
;
2796 dtp
->u
.p
.current_unit
= find_unit (options
.stdout_unit
);
2798 if (dtp
->u
.p
.current_unit
)
2800 dtp
->u
.p
.mode
= WRITING
;
2801 next_record (dtp
, 0);
2803 /* Write the namelist in its entirety. */
2806 namelist_write (dtp
);
2808 /* Or write the list of names. */
2812 /* "&namelist_name\n" */
2814 len
= dtp
->namelist_name_len
;
2815 p
= write_block (dtp
, len
- 1 + endlen
);
2819 memcpy ((char*)(p
+ 1), dtp
->namelist_name
, len
);
2820 memcpy ((char*)(p
+ len
+ 1), &endl
, endlen
);
2821 for (nl
= dtp
->u
.p
.ionml
; nl
; nl
= nl
->next
)
2825 len
= strlen (nl
->var_name
);
2826 p
= write_block (dtp
, len
+ endlen
);
2830 memcpy ((char*)(p
+ 1), nl
->var_name
, len
);
2831 memcpy ((char*)(p
+ len
+ 1), &endl
, endlen
);
2836 p
= write_block (dtp
, endlen
+ 4);
2839 memcpy (p
, &nmlend
, endlen
+ 4);
2842 /* Flush the stream to force immediate output. */
2844 fbuf_flush (dtp
->u
.p
.current_unit
, WRITING
);
2845 sflush (dtp
->u
.p
.current_unit
->s
);
2846 unlock_unit (dtp
->u
.p
.current_unit
);
2851 /* Restore the current unit. */
2853 dtp
->u
.p
.current_unit
= temp_unit
;
2854 dtp
->u
.p
.mode
= READING
;
2858 /* Reads and stores the input for the namelist object nl. For an array,
2859 the function loops over the ranges defined by the loop specification.
2860 This default to all the data or to the specification from a qualifier.
2861 nml_read_obj recursively calls itself to read derived types. It visits
2862 all its own components but only reads data for those that were touched
2863 when the name was parsed. If a read error is encountered, an attempt is
2864 made to return to read a new object name because the standard allows too
2865 little data to be available. On the other hand, too much data is an
2869 nml_read_obj (st_parameter_dt
*dtp
, namelist_info
*nl
, index_type offset
,
2870 namelist_info
**pprev_nl
, char *nml_err_msg
,
2871 size_t nml_err_msg_size
, index_type clow
, index_type chigh
)
2880 size_t obj_name_len
;
2884 /* If we have encountered a previous read error or this object has not been
2885 touched in name parsing, just return. */
2886 if (dtp
->u
.p
.nml_read_error
|| !nl
->touched
)
2889 dtp
->u
.p
.item_count
++; /* Used in error messages. */
2890 dtp
->u
.p
.repeat_count
= 0;
2902 dlen
= size_from_real_kind (len
);
2906 dlen
= size_from_complex_kind (len
);
2910 dlen
= chigh
? (chigh
- clow
+ 1) : nl
->string_length
;
2919 /* Update the pointer to the data, using the current index vector */
2921 if ((nl
->type
== BT_DERIVED
|| nl
->type
== BT_CLASS
)
2922 && nl
->dtio_sub
!= NULL
)
2924 pdata
= NULL
; /* Not used under these conidtions. */
2925 if (nl
->type
== BT_CLASS
)
2926 list_obj
.data
= ((gfc_class
*)nl
->mem_pos
)->data
;
2928 list_obj
.data
= (void *)nl
->mem_pos
;
2930 for (dim
= 0; dim
< nl
->var_rank
; dim
++)
2931 list_obj
.data
= list_obj
.data
+ (nl
->ls
[dim
].idx
2932 - GFC_DESCRIPTOR_LBOUND(nl
,dim
))
2933 * GFC_DESCRIPTOR_STRIDE(nl
,dim
) * nl
->size
;
2937 pdata
= (void*)(nl
->mem_pos
+ offset
);
2938 for (dim
= 0; dim
< nl
->var_rank
; dim
++)
2939 pdata
= (void*)(pdata
+ (nl
->ls
[dim
].idx
2940 - GFC_DESCRIPTOR_LBOUND(nl
,dim
))
2941 * GFC_DESCRIPTOR_STRIDE(nl
,dim
) * nl
->size
);
2944 /* If we are finished with the repeat count, try to read next value. */
2947 if (--dtp
->u
.p
.repeat_count
<= 0)
2949 if (dtp
->u
.p
.input_complete
)
2951 if (dtp
->u
.p
.at_eol
)
2952 finish_separator (dtp
);
2953 if (dtp
->u
.p
.input_complete
)
2956 dtp
->u
.p
.saved_type
= BT_UNKNOWN
;
2962 read_integer (dtp
, len
);
2966 read_logical (dtp
, len
);
2970 read_character (dtp
, len
);
2974 /* Need to copy data back from the real location to the temp in
2975 order to handle nml reads into arrays. */
2976 read_real (dtp
, pdata
, len
);
2977 memcpy (dtp
->u
.p
.value
, pdata
, dlen
);
2981 /* Same as for REAL, copy back to temp. */
2982 read_complex (dtp
, pdata
, len
, dlen
);
2983 memcpy (dtp
->u
.p
.value
, pdata
, dlen
);
2988 /* If this object has a User Defined procedure, call it. */
2989 if (nl
->dtio_sub
!= NULL
)
2991 int unit
= dtp
->u
.p
.current_unit
->unit_number
;
2992 char iotype
[] = "NAMELIST";
2993 gfc_charlen_type iotype_len
= 8;
2994 char tmp_iomsg
[IOMSG_LEN
] = "";
2996 gfc_charlen_type child_iomsg_len
;
2998 int *child_iostat
= NULL
;
2999 gfc_full_array_i4 vlist
;
3000 formatted_dtio dtio_ptr
= (formatted_dtio
)nl
->dtio_sub
;
3002 GFC_DESCRIPTOR_DATA(&vlist
) = NULL
;
3003 GFC_DIMENSION_SET(vlist
.dim
[0],1, 0, 0);
3005 list_obj
.vptr
= nl
->vtable
;
3008 /* Set iostat, intent(out). */
3010 child_iostat
= (dtp
->common
.flags
& IOPARM_HAS_IOSTAT
) ?
3011 dtp
->common
.iostat
: &noiostat
;
3013 /* Set iomsg, intent(inout). */
3014 if (dtp
->common
.flags
& IOPARM_HAS_IOMSG
)
3016 child_iomsg
= dtp
->common
.iomsg
;
3017 child_iomsg_len
= dtp
->common
.iomsg_len
;
3021 child_iomsg
= tmp_iomsg
;
3022 child_iomsg_len
= IOMSG_LEN
;
3025 /* Call the user defined formatted READ procedure. */
3026 dtp
->u
.p
.current_unit
->child_dtio
++;
3027 dtio_ptr ((void *)&list_obj
, &unit
, iotype
, &vlist
,
3028 child_iostat
, child_iomsg
,
3029 iotype_len
, child_iomsg_len
);
3030 dtp
->u
.p
.child_saved_iostat
= *child_iostat
;
3031 dtp
->u
.p
.current_unit
->child_dtio
--;
3035 /* Must be default derived type namelist read. */
3036 obj_name_len
= strlen (nl
->var_name
) + 1;
3037 obj_name
= xmalloc (obj_name_len
+1);
3038 memcpy (obj_name
, nl
->var_name
, obj_name_len
-1);
3039 memcpy (obj_name
+ obj_name_len
- 1, "%", 2);
3041 /* If reading a derived type, disable the expanded read warning
3042 since a single object can have multiple reads. */
3043 dtp
->u
.p
.expanded_read
= 0;
3045 /* Now loop over the components. */
3047 for (cmp
= nl
->next
;
3049 !strncmp (cmp
->var_name
, obj_name
, obj_name_len
);
3052 /* Jump over nested derived type by testing if the potential
3053 component name contains '%'. */
3054 if (strchr (cmp
->var_name
+ obj_name_len
, '%'))
3057 if (!nml_read_obj (dtp
, cmp
, (index_type
)(pdata
- nl
->mem_pos
),
3058 pprev_nl
, nml_err_msg
, nml_err_msg_size
,
3065 if (dtp
->u
.p
.input_complete
)
3076 snprintf (nml_err_msg
, nml_err_msg_size
,
3077 "Bad type for namelist object %s", nl
->var_name
);
3078 internal_error (&dtp
->common
, nml_err_msg
);
3083 /* The standard permits array data to stop short of the number of
3084 elements specified in the loop specification. In this case, we
3085 should be here with dtp->u.p.nml_read_error != 0. Control returns to
3086 nml_get_obj_data and an attempt is made to read object name. */
3089 if (dtp
->u
.p
.nml_read_error
)
3091 dtp
->u
.p
.expanded_read
= 0;
3095 if (dtp
->u
.p
.saved_type
== BT_UNKNOWN
)
3097 dtp
->u
.p
.expanded_read
= 0;
3101 switch (dtp
->u
.p
.saved_type
)
3108 memcpy (pdata
, dtp
->u
.p
.value
, dlen
);
3112 if (dlen
< dtp
->u
.p
.saved_used
)
3114 if (compile_options
.bounds_check
)
3116 snprintf (nml_err_msg
, nml_err_msg_size
,
3117 "Namelist object '%s' truncated on read.",
3119 generate_warning (&dtp
->common
, nml_err_msg
);
3124 m
= dtp
->u
.p
.saved_used
;
3126 if (dtp
->u
.p
.current_unit
->flags
.encoding
== ENCODING_UTF8
)
3128 gfc_char4_t
*q4
, *p4
= pdata
;
3131 q4
= (gfc_char4_t
*) dtp
->u
.p
.saved_string
;
3133 for (i
= 0; i
< m
; i
++)
3136 for (i
= 0; i
< dlen
- m
; i
++)
3137 *p4
++ = (gfc_char4_t
) ' ';
3141 pdata
= (void*)( pdata
+ clow
- 1 );
3142 memcpy (pdata
, dtp
->u
.p
.saved_string
, m
);
3144 memset ((void*)( pdata
+ m
), ' ', dlen
- m
);
3152 /* Warn if a non-standard expanded read occurs. A single read of a
3153 single object is acceptable. If a second read occurs, issue a warning
3154 and set the flag to zero to prevent further warnings. */
3155 if (dtp
->u
.p
.expanded_read
== 2)
3157 notify_std (&dtp
->common
, GFC_STD_GNU
, "Non-standard expanded namelist read.");
3158 dtp
->u
.p
.expanded_read
= 0;
3161 /* If the expanded read warning flag is set, increment it,
3162 indicating that a single read has occurred. */
3163 if (dtp
->u
.p
.expanded_read
>= 1)
3164 dtp
->u
.p
.expanded_read
++;
3166 /* Break out of loop if scalar. */
3170 /* Now increment the index vector. */
3175 for (dim
= 0; dim
< nl
->var_rank
; dim
++)
3177 nl
->ls
[dim
].idx
+= nml_carry
* nl
->ls
[dim
].step
;
3179 if (((nl
->ls
[dim
].step
> 0) && (nl
->ls
[dim
].idx
> nl
->ls
[dim
].end
))
3181 ((nl
->ls
[dim
].step
< 0) && (nl
->ls
[dim
].idx
< nl
->ls
[dim
].end
)))
3183 nl
->ls
[dim
].idx
= nl
->ls
[dim
].start
;
3187 } while (!nml_carry
);
3189 if (dtp
->u
.p
.repeat_count
> 1)
3191 snprintf (nml_err_msg
, nml_err_msg_size
,
3192 "Repeat count too large for namelist object %s", nl
->var_name
);
3202 /* Parses the object name, including array and substring qualifiers. It
3203 iterates over derived type components, touching those components and
3204 setting their loop specifications, if there is a qualifier. If the
3205 object is itself a derived type, its components and subcomponents are
3206 touched. nml_read_obj is called at the end and this reads the data in
3207 the manner specified by the object name. */
3210 nml_get_obj_data (st_parameter_dt
*dtp
, namelist_info
**pprev_nl
,
3211 char *nml_err_msg
, size_t nml_err_msg_size
)
3215 namelist_info
*first_nl
= NULL
;
3216 namelist_info
*root_nl
= NULL
;
3217 int dim
, parsed_rank
;
3218 int component_flag
, qualifier_flag
;
3219 index_type clow
, chigh
;
3220 int non_zero_rank_count
;
3222 /* Look for end of input or object name. If '?' or '=?' are encountered
3223 in stdin, print the node names or the namelist to stdout. */
3225 eat_separator (dtp
);
3226 if (dtp
->u
.p
.input_complete
)
3229 if (dtp
->u
.p
.at_eol
)
3230 finish_separator (dtp
);
3231 if (dtp
->u
.p
.input_complete
)
3234 if ((c
= next_char (dtp
)) == EOF
)
3239 if ((c
= next_char (dtp
)) == EOF
)
3243 snprintf (nml_err_msg
, nml_err_msg_size
,
3244 "namelist read: misplaced = sign");
3247 nml_query (dtp
, '=');
3251 nml_query (dtp
, '?');
3256 nml_match_name (dtp
, "end", 3);
3257 if (dtp
->u
.p
.nml_read_error
)
3259 snprintf (nml_err_msg
, nml_err_msg_size
,
3260 "namelist not terminated with / or &end");
3265 dtp
->u
.p
.input_complete
= 1;
3272 /* Untouch all nodes of the namelist and reset the flags that are set for
3273 derived type components. */
3275 nml_untouch_nodes (dtp
);
3278 non_zero_rank_count
= 0;
3280 /* Get the object name - should '!' and '\n' be permitted separators? */
3288 if (!is_separator (c
))
3289 push_char_default (dtp
, tolower(c
));
3290 if ((c
= next_char (dtp
)) == EOF
)
3293 while (!( c
=='=' || c
==' ' || c
=='\t' || c
=='(' || c
=='%' ));
3295 unget_char (dtp
, c
);
3297 /* Check that the name is in the namelist and get pointer to object.
3298 Three error conditions exist: (i) An attempt is being made to
3299 identify a non-existent object, following a failed data read or
3300 (ii) The object name does not exist or (iii) Too many data items
3301 are present for an object. (iii) gives the same error message
3304 push_char_default (dtp
, '\0');
3308 #define EXT_STACK_SZ 100
3309 char ext_stack
[EXT_STACK_SZ
];
3311 size_t var_len
= strlen (root_nl
->var_name
);
3313 = dtp
->u
.p
.saved_string
? strlen (dtp
->u
.p
.saved_string
) : 0;
3314 size_t ext_size
= var_len
+ saved_len
+ 1;
3316 if (ext_size
> EXT_STACK_SZ
)
3317 ext_name
= xmalloc (ext_size
);
3319 ext_name
= ext_stack
;
3321 memcpy (ext_name
, root_nl
->var_name
, var_len
);
3322 if (dtp
->u
.p
.saved_string
)
3323 memcpy (ext_name
+ var_len
, dtp
->u
.p
.saved_string
, saved_len
);
3324 ext_name
[var_len
+ saved_len
] = '\0';
3325 nl
= find_nml_node (dtp
, ext_name
);
3327 if (ext_size
> EXT_STACK_SZ
)
3331 nl
= find_nml_node (dtp
, dtp
->u
.p
.saved_string
);
3335 if (dtp
->u
.p
.nml_read_error
&& *pprev_nl
)
3336 snprintf (nml_err_msg
, nml_err_msg_size
,
3337 "Bad data for namelist object %s", (*pprev_nl
)->var_name
);
3340 snprintf (nml_err_msg
, nml_err_msg_size
,
3341 "Cannot match namelist object name %s",
3342 dtp
->u
.p
.saved_string
);
3347 /* Get the length, data length, base pointer and rank of the variable.
3348 Set the default loop specification first. */
3350 for (dim
=0; dim
< nl
->var_rank
; dim
++)
3352 nl
->ls
[dim
].step
= 1;
3353 nl
->ls
[dim
].end
= GFC_DESCRIPTOR_UBOUND(nl
,dim
);
3354 nl
->ls
[dim
].start
= GFC_DESCRIPTOR_LBOUND(nl
,dim
);
3355 nl
->ls
[dim
].idx
= nl
->ls
[dim
].start
;
3358 /* Check to see if there is a qualifier: if so, parse it.*/
3360 if (c
== '(' && nl
->var_rank
)
3363 if (!nml_parse_qualifier (dtp
, nl
->dim
, nl
->ls
, nl
->var_rank
,
3364 nl
->type
, nml_err_msg
, nml_err_msg_size
,
3367 char *nml_err_msg_end
= strchr (nml_err_msg
, '\0');
3368 snprintf (nml_err_msg_end
,
3369 nml_err_msg_size
- (nml_err_msg_end
- nml_err_msg
),
3370 " for namelist variable %s", nl
->var_name
);
3373 if (parsed_rank
> 0)
3374 non_zero_rank_count
++;
3378 if ((c
= next_char (dtp
)) == EOF
)
3380 unget_char (dtp
, c
);
3382 else if (nl
->var_rank
> 0)
3383 non_zero_rank_count
++;
3385 /* Now parse a derived type component. The root namelist_info address
3386 is backed up, as is the previous component level. The component flag
3387 is set and the iteration is made by jumping back to get_name. */
3391 if (nl
->type
!= BT_DERIVED
)
3393 snprintf (nml_err_msg
, nml_err_msg_size
,
3394 "Attempt to get derived component for %s", nl
->var_name
);
3398 /* Don't move first_nl further in the list if a qualifier was found. */
3399 if ((*pprev_nl
== NULL
&& !qualifier_flag
) || !component_flag
)
3405 if ((c
= next_char (dtp
)) == EOF
)
3410 /* Parse a character qualifier, if present. chigh = 0 is a default
3411 that signals that the string length = string_length. */
3416 if (c
== '(' && nl
->type
== BT_CHARACTER
)
3418 descriptor_dimension chd
[1] = { {1, clow
, nl
->string_length
} };
3419 array_loop_spec ind
[1] = { {1, clow
, nl
->string_length
, 1} };
3421 if (!nml_parse_qualifier (dtp
, chd
, ind
, -1, nl
->type
,
3422 nml_err_msg
, nml_err_msg_size
, &parsed_rank
))
3424 char *nml_err_msg_end
= strchr (nml_err_msg
, '\0');
3425 snprintf (nml_err_msg_end
,
3426 nml_err_msg_size
- (nml_err_msg_end
- nml_err_msg
),
3427 " for namelist variable %s", nl
->var_name
);
3431 clow
= ind
[0].start
;
3434 if (ind
[0].step
!= 1)
3436 snprintf (nml_err_msg
, nml_err_msg_size
,
3437 "Step not allowed in substring qualifier"
3438 " for namelist object %s", nl
->var_name
);
3442 if ((c
= next_char (dtp
)) == EOF
)
3444 unget_char (dtp
, c
);
3447 /* Make sure no extraneous qualifiers are there. */
3451 snprintf (nml_err_msg
, nml_err_msg_size
,
3452 "Qualifier for a scalar or non-character namelist object %s",
3457 /* Make sure there is no more than one non-zero rank object. */
3458 if (non_zero_rank_count
> 1)
3460 snprintf (nml_err_msg
, nml_err_msg_size
,
3461 "Multiple sub-objects with non-zero rank in namelist object %s",
3463 non_zero_rank_count
= 0;
3467 /* According to the standard, an equal sign MUST follow an object name. The
3468 following is possibly lax - it allows comments, blank lines and so on to
3469 intervene. eat_spaces (dtp); c = next_char (dtp); would be compliant*/
3473 eat_separator (dtp
);
3474 if (dtp
->u
.p
.input_complete
)
3477 if (dtp
->u
.p
.at_eol
)
3478 finish_separator (dtp
);
3479 if (dtp
->u
.p
.input_complete
)
3482 if ((c
= next_char (dtp
)) == EOF
)
3487 snprintf (nml_err_msg
, nml_err_msg_size
,
3488 "Equal sign must follow namelist object name %s",
3493 /* If a derived type, touch its components and restore the root
3494 namelist_info if we have parsed a qualified derived type
3497 if (nl
->type
== BT_DERIVED
&& nl
->dtio_sub
== NULL
)
3498 nml_touch_nodes (nl
);
3502 if (first_nl
->var_rank
== 0)
3504 if (component_flag
&& qualifier_flag
)
3511 dtp
->u
.p
.nml_read_error
= 0;
3512 if (!nml_read_obj (dtp
, nl
, 0, pprev_nl
, nml_err_msg
, nml_err_msg_size
,
3520 /* The EOF error message is issued by hit_eof. Return true so that the
3521 caller does not use nml_err_msg and nml_err_msg_size to generate
3522 an unrelated error message. */
3525 dtp
->u
.p
.input_complete
= 1;
3526 unget_char (dtp
, c
);
3533 /* Entry point for namelist input. Goes through input until namelist name
3534 is matched. Then cycles through nml_get_obj_data until the input is
3535 completed or there is an error. */
3538 namelist_read (st_parameter_dt
*dtp
)
3541 char nml_err_msg
[200];
3543 /* Initialize the error string buffer just in case we get an unexpected fail
3544 somewhere and end up at nml_err_ret. */
3545 strcpy (nml_err_msg
, "Internal namelist read error");
3547 /* Pointer to the previously read object, in case attempt is made to read
3548 new object name. Should this fail, error message can give previous
3550 namelist_info
*prev_nl
= NULL
;
3552 dtp
->u
.p
.input_complete
= 0;
3553 dtp
->u
.p
.expanded_read
= 0;
3555 /* Set the next_char and push_char worker functions. */
3558 /* Look for &namelist_name . Skip all characters, testing for $nmlname.
3559 Exit on success or EOF. If '?' or '=?' encountered in stdin, print
3560 node names or namelist on stdout. */
3563 c
= next_char (dtp
);
3575 c
= next_char (dtp
);
3577 nml_query (dtp
, '=');
3579 unget_char (dtp
, c
);
3583 nml_query (dtp
, '?');
3593 /* Match the name of the namelist. */
3595 nml_match_name (dtp
, dtp
->namelist_name
, dtp
->namelist_name_len
);
3597 if (dtp
->u
.p
.nml_read_error
)
3600 /* A trailing space is required, we give a little latitude here, 10.9.1. */
3601 c
= next_char (dtp
);
3602 if (!is_separator(c
) && c
!= '!')
3604 unget_char (dtp
, c
);
3608 unget_char (dtp
, c
);
3609 eat_separator (dtp
);
3611 /* Ready to read namelist objects. If there is an error in input
3612 from stdin, output the error message and continue. */
3614 while (!dtp
->u
.p
.input_complete
)
3616 if (!nml_get_obj_data (dtp
, &prev_nl
, nml_err_msg
, sizeof nml_err_msg
))
3619 /* Reset the previous namelist pointer if we know we are not going
3620 to be doing multiple reads within a single namelist object. */
3621 if (prev_nl
&& prev_nl
->var_rank
== 0)
3632 /* All namelist error calls return from here */
3635 generate_error (&dtp
->common
, LIBERROR_READ_VALUE
, nml_err_msg
);