1 /* Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008
2 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
4 Namelist input contributed by Paul Thomas
5 F2003 I/O support contributed by Jerry DeLisle
7 This file is part of the GNU Fortran 95 runtime library (libgfortran).
9 Libgfortran is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 In addition to the permissions in the GNU General Public License, the
15 Free Software Foundation gives you unlimited permission to link the
16 compiled version of this file into combinations with other programs,
17 and to distribute those combinations without any restriction coming
18 from the use of this file. (The General Public License restrictions
19 do apply in other respects; for example, they cover modification of
20 the file, and distribution when not linked into a combine
23 Libgfortran is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with Libgfortran; see the file COPYING. If not, write to
30 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
31 Boston, MA 02110-1301, USA. */
39 /* List directed input. Several parsing subroutines are practically
40 reimplemented from formatted input, the reason being that there are
41 all kinds of small differences between formatted and list directed
45 /* Subroutines for reading characters from the input. Because a
46 repeat count is ambiguous with an integer, we have to read the
47 whole digit string before seeing if there is a '*' which signals
48 the repeat count. Since we can have a lot of potential leading
49 zeros, we have to be able to back up by arbitrary amount. Because
50 the input might not be seekable, we have to buffer the data
53 #define CASE_DIGITS case '0': case '1': case '2': case '3': case '4': \
54 case '5': case '6': case '7': case '8': case '9'
56 #define CASE_SEPARATORS case ' ': case ',': case '/': case '\n': case '\t': \
59 /* This macro assumes that we're operating on a variable. */
61 #define is_separator(c) (c == '/' || c == ',' || c == '\n' || c == ' ' \
62 || c == '\t' || c == '\r' || c == ';')
64 /* Maximum repeat count. Less than ten times the maximum signed int32. */
66 #define MAX_REPEAT 200000000
70 # define snprintf(str, size, ...) sprintf (str, __VA_ARGS__)
73 /* Save a character to a string buffer, enlarging it as necessary. */
76 push_char (st_parameter_dt *dtp, char c)
80 if (dtp->u.p.saved_string == NULL)
82 if (dtp->u.p.scratch == NULL)
83 dtp->u.p.scratch = get_mem (SCRATCH_SIZE);
84 dtp->u.p.saved_string = dtp->u.p.scratch;
85 memset (dtp->u.p.saved_string, 0, SCRATCH_SIZE);
86 dtp->u.p.saved_length = SCRATCH_SIZE;
87 dtp->u.p.saved_used = 0;
90 if (dtp->u.p.saved_used >= dtp->u.p.saved_length)
92 dtp->u.p.saved_length = 2 * dtp->u.p.saved_length;
93 new = get_mem (2 * dtp->u.p.saved_length);
95 memset (new, 0, 2 * dtp->u.p.saved_length);
97 memcpy (new, dtp->u.p.saved_string, dtp->u.p.saved_used);
98 if (dtp->u.p.saved_string != dtp->u.p.scratch)
99 free_mem (dtp->u.p.saved_string);
101 dtp->u.p.saved_string = new;
104 dtp->u.p.saved_string[dtp->u.p.saved_used++] = c;
108 /* Free the input buffer if necessary. */
111 free_saved (st_parameter_dt *dtp)
113 if (dtp->u.p.saved_string == NULL)
116 if (dtp->u.p.saved_string != dtp->u.p.scratch)
117 free_mem (dtp->u.p.saved_string);
119 dtp->u.p.saved_string = NULL;
120 dtp->u.p.saved_used = 0;
124 /* Free the line buffer if necessary. */
127 free_line (st_parameter_dt *dtp)
129 dtp->u.p.item_count = 0;
130 dtp->u.p.line_buffer_enabled = 0;
132 if (dtp->u.p.line_buffer == NULL)
135 free_mem (dtp->u.p.line_buffer);
136 dtp->u.p.line_buffer = NULL;
141 next_char (st_parameter_dt *dtp)
147 if (dtp->u.p.last_char != '\0')
150 c = dtp->u.p.last_char;
151 dtp->u.p.last_char = '\0';
155 /* Read from line_buffer if enabled. */
157 if (dtp->u.p.line_buffer_enabled)
161 c = dtp->u.p.line_buffer[dtp->u.p.item_count];
162 if (c != '\0' && dtp->u.p.item_count < 64)
164 dtp->u.p.line_buffer[dtp->u.p.item_count] = '\0';
165 dtp->u.p.item_count++;
169 dtp->u.p.item_count = 0;
170 dtp->u.p.line_buffer_enabled = 0;
173 /* Handle the end-of-record and end-of-file conditions for
174 internal array unit. */
175 if (is_array_io (dtp))
178 longjmp (*dtp->u.p.eof_jump, 1);
180 /* Check for "end-of-record" condition. */
181 if (dtp->u.p.current_unit->bytes_left == 0)
186 record = next_array_record (dtp, dtp->u.p.current_unit->ls,
189 /* Check for "end-of-file" condition. */
196 record *= dtp->u.p.current_unit->recl;
197 if (sseek (dtp->u.p.current_unit->s, record) == FAILURE)
198 longjmp (*dtp->u.p.eof_jump, 1);
200 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
205 /* Get the next character and handle end-of-record conditions. */
209 if (sread (dtp->u.p.current_unit->s, &c, &length) != 0)
211 generate_error (&dtp->common, LIBERROR_OS, NULL);
215 if (is_stream_io (dtp) && length == 1)
216 dtp->u.p.current_unit->strm_pos++;
218 if (is_internal_unit (dtp))
220 if (is_array_io (dtp))
222 /* Check whether we hit EOF. */
225 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
228 dtp->u.p.current_unit->bytes_left--;
233 longjmp (*dtp->u.p.eof_jump, 1);
245 if (dtp->u.p.advance_status == ADVANCE_NO)
247 if (dtp->u.p.current_unit->endfile == AT_ENDFILE)
248 longjmp (*dtp->u.p.eof_jump, 1);
249 dtp->u.p.current_unit->endfile = AT_ENDFILE;
253 longjmp (*dtp->u.p.eof_jump, 1);
257 dtp->u.p.at_eol = (c == '\n' || c == '\r');
262 /* Push a character back onto the input. */
265 unget_char (st_parameter_dt *dtp, char c)
267 dtp->u.p.last_char = c;
271 /* Skip over spaces in the input. Returns the nonspace character that
272 terminated the eating and also places it back on the input. */
275 eat_spaces (st_parameter_dt *dtp)
283 while (c == ' ' || c == '\t');
290 /* This function reads characters through to the end of the current line and
291 just ignores them. */
294 eat_line (st_parameter_dt *dtp)
297 if (!is_internal_unit (dtp))
304 /* Skip over a separator. Technically, we don't always eat the whole
305 separator. This is because if we've processed the last input item,
306 then a separator is unnecessary. Plus the fact that operating
307 systems usually deliver console input on a line basis.
309 The upshot is that if we see a newline as part of reading a
310 separator, we stop reading. If there are more input items, we
311 continue reading the separator with finish_separator() which takes
312 care of the fact that we may or may not have seen a comma as part
316 eat_separator (st_parameter_dt *dtp)
321 dtp->u.p.comma_flag = 0;
327 if ((dtp->common.flags & IOPARM_DT_HAS_F2003)
328 && dtp->u.p.decimal_status == DECIMAL_COMMA)
335 dtp->u.p.comma_flag = 1;
340 dtp->u.p.input_complete = 1;
354 if (dtp->u.p.namelist_mode)
370 while (c == '\n' || c == '\r' || c == ' ' || c == '\t');
376 if (dtp->u.p.namelist_mode)
377 { /* Eat a namelist comment. */
385 /* Fall Through... */
394 /* Finish processing a separator that was interrupted by a newline.
395 If we're here, then another data item is present, so we finish what
396 we started on the previous line. */
399 finish_separator (st_parameter_dt *dtp)
410 if (dtp->u.p.comma_flag)
414 c = eat_spaces (dtp);
415 if (c == '\n' || c == '\r')
422 dtp->u.p.input_complete = 1;
423 if (!dtp->u.p.namelist_mode)
432 if (dtp->u.p.namelist_mode)
448 /* This function is needed to catch bad conversions so that namelist can
449 attempt to see if dtp->u.p.saved_string contains a new object name rather
453 nml_bad_return (st_parameter_dt *dtp, char c)
455 if (dtp->u.p.namelist_mode)
457 dtp->u.p.nml_read_error = 1;
464 /* Convert an unsigned string to an integer. The length value is -1
465 if we are working on a repeat count. Returns nonzero if we have a
466 range problem. As a side effect, frees the dtp->u.p.saved_string. */
469 convert_integer (st_parameter_dt *dtp, int length, int negative)
471 char c, *buffer, message[100];
473 GFC_INTEGER_LARGEST v, max, max10;
475 buffer = dtp->u.p.saved_string;
478 max = (length == -1) ? MAX_REPEAT : max_value (length, 1);
503 set_integer (dtp->u.p.value, v, length);
507 dtp->u.p.repeat_count = v;
509 if (dtp->u.p.repeat_count == 0)
511 sprintf (message, "Zero repeat count in item %d of list input",
512 dtp->u.p.item_count);
514 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
524 sprintf (message, "Repeat count overflow in item %d of list input",
525 dtp->u.p.item_count);
527 sprintf (message, "Integer overflow while reading item %d",
528 dtp->u.p.item_count);
531 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
537 /* Parse a repeat count for logical and complex values which cannot
538 begin with a digit. Returns nonzero if we are done, zero if we
539 should continue on. */
542 parse_repeat (st_parameter_dt *dtp)
544 char c, message[100];
570 repeat = 10 * repeat + c - '0';
572 if (repeat > MAX_REPEAT)
575 "Repeat count overflow in item %d of list input",
576 dtp->u.p.item_count);
578 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
588 "Zero repeat count in item %d of list input",
589 dtp->u.p.item_count);
591 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
603 dtp->u.p.repeat_count = repeat;
610 sprintf (message, "Bad repeat count in item %d of list input",
611 dtp->u.p.item_count);
612 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
617 /* To read a logical we have to look ahead in the input stream to make sure
618 there is not an equal sign indicating a variable name. To do this we use
619 line_buffer to point to a temporary buffer, pushing characters there for
620 possible later reading. */
623 l_push_char (st_parameter_dt *dtp, char c)
625 if (dtp->u.p.line_buffer == NULL)
627 dtp->u.p.line_buffer = get_mem (SCRATCH_SIZE);
628 memset (dtp->u.p.line_buffer, 0, SCRATCH_SIZE);
631 dtp->u.p.line_buffer[dtp->u.p.item_count++] = c;
635 /* Read a logical character on the input. */
638 read_logical (st_parameter_dt *dtp, int length)
640 char c, message[100];
643 if (parse_repeat (dtp))
646 c = tolower (next_char (dtp));
647 l_push_char (dtp, c);
653 l_push_char (dtp, c);
655 if (!is_separator(c))
663 l_push_char (dtp, c);
665 if (!is_separator(c))
672 c = tolower (next_char (dtp));
690 return; /* Null value. */
693 /* Save the character in case it is the beginning
694 of the next object name. */
699 dtp->u.p.saved_type = BT_LOGICAL;
700 dtp->u.p.saved_length = length;
702 /* Eat trailing garbage. */
707 while (!is_separator (c));
711 set_integer ((int *) dtp->u.p.value, v, length);
718 for(i = 0; i < 63; i++)
723 /* All done if this is not a namelist read. */
724 if (!dtp->u.p.namelist_mode)
737 l_push_char (dtp, c);
740 dtp->u.p.nml_read_error = 1;
741 dtp->u.p.line_buffer_enabled = 1;
742 dtp->u.p.item_count = 0;
752 if (nml_bad_return (dtp, c))
757 sprintf (message, "Bad logical value while reading item %d",
758 dtp->u.p.item_count);
759 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
764 dtp->u.p.saved_type = BT_LOGICAL;
765 dtp->u.p.saved_length = length;
766 set_integer ((int *) dtp->u.p.value, v, length);
772 /* Reading integers is tricky because we can actually be reading a
773 repeat count. We have to store the characters in a buffer because
774 we could be reading an integer that is larger than the default int
775 used for repeat counts. */
778 read_integer (st_parameter_dt *dtp, int length)
780 char c, message[100];
790 /* Fall through... */
796 CASE_SEPARATORS: /* Single null. */
809 /* Take care of what may be a repeat count. */
821 push_char (dtp, '\0');
824 CASE_SEPARATORS: /* Not a repeat count. */
833 if (convert_integer (dtp, -1, 0))
836 /* Get the real integer. */
851 /* Fall through... */
882 if (nml_bad_return (dtp, c))
887 sprintf (message, "Bad integer for item %d in list input",
888 dtp->u.p.item_count);
889 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
897 push_char (dtp, '\0');
898 if (convert_integer (dtp, length, negative))
905 dtp->u.p.saved_type = BT_INTEGER;
909 /* Read a character variable. */
912 read_character (st_parameter_dt *dtp, int length __attribute__ ((unused)))
914 char c, quote, message[100];
916 quote = ' '; /* Space means no quote character. */
926 unget_char (dtp, c); /* NULL value. */
936 if (dtp->u.p.namelist_mode)
938 if (dtp->u.p.delim_status == DELIM_APOSTROPHE
939 || dtp->u.p.delim_status == DELIM_QUOTE
940 || c == '&' || c == '$' || c == '/')
946 /* Check to see if we are seeing a namelist object name by using the
947 line buffer and looking ahead for an '=' or '('. */
948 l_push_char (dtp, c);
951 for(i = 0; i < 63; i++)
961 l_push_char (dtp, c);
962 dtp->u.p.item_count = 0;
963 dtp->u.p.line_buffer_enabled = 1;
968 l_push_char (dtp, c);
970 if (c == '=' || c == '(')
972 dtp->u.p.item_count = 0;
973 dtp->u.p.nml_read_error = 1;
974 dtp->u.p.line_buffer_enabled = 1;
979 /* The string is too long to be a valid object name so assume that it
980 is a string to be read in as a value. */
981 dtp->u.p.item_count = 0;
982 dtp->u.p.line_buffer_enabled = 1;
990 /* Deal with a possible repeat count. */
1002 unget_char (dtp, c);
1003 goto done; /* String was only digits! */
1006 push_char (dtp, '\0');
1011 goto get_string; /* Not a repeat count after all. */
1016 if (convert_integer (dtp, -1, 0))
1019 /* Now get the real string. */
1021 c = next_char (dtp);
1025 unget_char (dtp, c); /* Repeated NULL values. */
1026 eat_separator (dtp);
1042 c = next_char (dtp);
1053 /* See if we have a doubled quote character or the end of
1056 c = next_char (dtp);
1059 push_char (dtp, quote);
1063 unget_char (dtp, c);
1069 unget_char (dtp, c);
1073 if (c != '\n' && c != '\r')
1083 /* At this point, we have to have a separator, or else the string is
1086 c = next_char (dtp);
1087 if (is_separator (c) || c == '!')
1089 unget_char (dtp, c);
1090 eat_separator (dtp);
1091 dtp->u.p.saved_type = BT_CHARACTER;
1097 sprintf (message, "Invalid string input in item %d",
1098 dtp->u.p.item_count);
1099 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1104 /* Parse a component of a complex constant or a real number that we
1105 are sure is already there. This is a straight real number parser. */
1108 parse_real (st_parameter_dt *dtp, void *buffer, int length)
1110 char c, message[100];
1113 c = next_char (dtp);
1114 if (c == '-' || c == '+')
1117 c = next_char (dtp);
1120 if ((dtp->common.flags & IOPARM_DT_HAS_F2003)
1121 && c == ',' && dtp->u.p.decimal_status == DECIMAL_COMMA)
1124 if (!isdigit (c) && c != '.')
1126 if (c == 'i' || c == 'I' || c == 'n' || c == 'N')
1134 seen_dp = (c == '.') ? 1 : 0;
1138 c = next_char (dtp);
1139 if ((dtp->common.flags & IOPARM_DT_HAS_F2003)
1140 && c == ',' && dtp->u.p.decimal_status == DECIMAL_COMMA)
1160 push_char (dtp, 'e');
1165 push_char (dtp, 'e');
1167 c = next_char (dtp);
1171 unget_char (dtp, c);
1180 c = next_char (dtp);
1181 if (c != '-' && c != '+')
1182 push_char (dtp, '+');
1186 c = next_char (dtp);
1197 c = next_char (dtp);
1205 unget_char (dtp, c);
1214 unget_char (dtp, c);
1215 push_char (dtp, '\0');
1217 m = convert_real (dtp, buffer, dtp->u.p.saved_string, length);
1223 /* Match INF and Infinity. */
1224 if ((c == 'i' || c == 'I')
1225 && ((c = next_char (dtp)) == 'n' || c == 'N')
1226 && ((c = next_char (dtp)) == 'f' || c == 'F'))
1228 c = next_char (dtp);
1229 if ((c != 'i' && c != 'I')
1230 || ((c == 'i' || c == 'I')
1231 && ((c = next_char (dtp)) == 'n' || c == 'N')
1232 && ((c = next_char (dtp)) == 'i' || c == 'I')
1233 && ((c = next_char (dtp)) == 't' || c == 'T')
1234 && ((c = next_char (dtp)) == 'y' || c == 'Y')
1235 && (c = next_char (dtp))))
1237 if (is_separator (c))
1238 unget_char (dtp, c);
1239 push_char (dtp, 'i');
1240 push_char (dtp, 'n');
1241 push_char (dtp, 'f');
1245 else if (((c = next_char (dtp)) == 'a' || c == 'A')
1246 && ((c = next_char (dtp)) == 'n' || c == 'N')
1247 && (c = next_char (dtp)))
1249 if (is_separator (c))
1250 unget_char (dtp, c);
1251 push_char (dtp, 'n');
1252 push_char (dtp, 'a');
1253 push_char (dtp, 'n');
1259 if (nml_bad_return (dtp, c))
1264 sprintf (message, "Bad floating point number for item %d",
1265 dtp->u.p.item_count);
1266 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1272 /* Reading a complex number is straightforward because we can tell
1273 what it is right away. */
1276 read_complex (st_parameter_dt *dtp, int kind, size_t size)
1281 if (parse_repeat (dtp))
1284 c = next_char (dtp);
1291 unget_char (dtp, c);
1292 eat_separator (dtp);
1300 if (parse_real (dtp, dtp->u.p.value, kind))
1305 c = next_char (dtp);
1306 if (c == '\n' || c== '\r')
1309 unget_char (dtp, c);
1311 if (dtp->common.flags & IOPARM_DT_HAS_F2003)
1314 != (dtp->u.p.decimal_status == DECIMAL_POINT ? ',' : ';'))
1319 if (next_char (dtp) != ',')
1325 c = next_char (dtp);
1326 if (c == '\n' || c== '\r')
1329 unget_char (dtp, c);
1331 if (parse_real (dtp, dtp->u.p.value + size / 2, kind))
1335 if (next_char (dtp) != ')')
1338 c = next_char (dtp);
1339 if (!is_separator (c))
1342 unget_char (dtp, c);
1343 eat_separator (dtp);
1346 dtp->u.p.saved_type = BT_COMPLEX;
1351 if (nml_bad_return (dtp, c))
1356 sprintf (message, "Bad complex value in item %d of list input",
1357 dtp->u.p.item_count);
1358 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1362 /* Parse a real number with a possible repeat count. */
1365 read_real (st_parameter_dt *dtp, int length)
1367 char c, message[100];
1373 c = next_char (dtp);
1374 if ((dtp->common.flags & IOPARM_DT_HAS_F2003)
1375 && c == ',' && dtp->u.p.decimal_status == DECIMAL_COMMA)
1393 unget_char (dtp, c); /* Single null. */
1394 eat_separator (dtp);
1407 /* Get the digit string that might be a repeat count. */
1411 c = next_char (dtp);
1412 if ((dtp->common.flags & IOPARM_DT_HAS_F2003)
1413 && c == ',' && dtp->u.p.decimal_status == DECIMAL_COMMA)
1437 push_char (dtp, 'e');
1439 c = next_char (dtp);
1443 push_char (dtp, '\0');
1447 if (c != '\n' && c != ',' && c != '\r' && c != ';')
1448 unget_char (dtp, c);
1457 if (convert_integer (dtp, -1, 0))
1460 /* Now get the number itself. */
1462 c = next_char (dtp);
1463 if (is_separator (c))
1464 { /* Repeated null value. */
1465 unget_char (dtp, c);
1466 eat_separator (dtp);
1470 if (c != '-' && c != '+')
1471 push_char (dtp, '+');
1476 c = next_char (dtp);
1479 if ((dtp->common.flags & IOPARM_DT_HAS_F2003)
1480 && c == ',' && dtp->u.p.decimal_status == DECIMAL_COMMA)
1483 if (!isdigit (c) && c != '.')
1485 if (c == 'i' || c == 'I' || c == 'n' || c == 'N')
1504 c = next_char (dtp);
1505 if ((dtp->common.flags & IOPARM_DT_HAS_F2003)
1506 && c == ',' && dtp->u.p.decimal_status == DECIMAL_COMMA)
1533 push_char (dtp, 'e');
1535 c = next_char (dtp);
1544 push_char (dtp, 'e');
1546 c = next_char (dtp);
1547 if (c != '+' && c != '-')
1548 push_char (dtp, '+');
1552 c = next_char (dtp);
1562 c = next_char (dtp);
1579 unget_char (dtp, c);
1580 eat_separator (dtp);
1581 push_char (dtp, '\0');
1582 if (convert_real (dtp, dtp->u.p.value, dtp->u.p.saved_string, length))
1586 dtp->u.p.saved_type = BT_REAL;
1590 l_push_char (dtp, c);
1593 /* Match INF and Infinity. */
1594 if (c == 'i' || c == 'I')
1596 c = next_char (dtp);
1597 l_push_char (dtp, c);
1598 if (c != 'n' && c != 'N')
1600 c = next_char (dtp);
1601 l_push_char (dtp, c);
1602 if (c != 'f' && c != 'F')
1604 c = next_char (dtp);
1605 l_push_char (dtp, c);
1606 if (!is_separator (c))
1608 if (c != 'i' && c != 'I')
1610 c = next_char (dtp);
1611 l_push_char (dtp, c);
1612 if (c != 'n' && c != 'N')
1614 c = next_char (dtp);
1615 l_push_char (dtp, c);
1616 if (c != 'i' && c != 'I')
1618 c = next_char (dtp);
1619 l_push_char (dtp, c);
1620 if (c != 't' && c != 'T')
1622 c = next_char (dtp);
1623 l_push_char (dtp, c);
1624 if (c != 'y' && c != 'Y')
1626 c = next_char (dtp);
1627 l_push_char (dtp, c);
1633 c = next_char (dtp);
1634 l_push_char (dtp, c);
1635 if (c != 'a' && c != 'A')
1637 c = next_char (dtp);
1638 l_push_char (dtp, c);
1639 if (c != 'n' && c != 'N')
1641 c = next_char (dtp);
1642 l_push_char (dtp, c);
1645 if (!is_separator (c))
1648 if (dtp->u.p.namelist_mode)
1650 if (c == ' ' || c =='\n' || c == '\r')
1653 c = next_char (dtp);
1654 while (c == ' ' || c =='\n' || c == '\r');
1656 l_push_char (dtp, c);
1665 push_char (dtp, 'i');
1666 push_char (dtp, 'n');
1667 push_char (dtp, 'f');
1671 push_char (dtp, 'n');
1672 push_char (dtp, 'a');
1673 push_char (dtp, 'n');
1680 if (dtp->u.p.namelist_mode)
1682 dtp->u.p.nml_read_error = 1;
1683 dtp->u.p.line_buffer_enabled = 1;
1684 dtp->u.p.item_count = 0;
1690 if (nml_bad_return (dtp, c))
1695 sprintf (message, "Bad real number in item %d of list input",
1696 dtp->u.p.item_count);
1697 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1701 /* Check the current type against the saved type to make sure they are
1702 compatible. Returns nonzero if incompatible. */
1705 check_type (st_parameter_dt *dtp, bt type, int len)
1709 if (dtp->u.p.saved_type != BT_NULL && dtp->u.p.saved_type != type)
1711 sprintf (message, "Read type %s where %s was expected for item %d",
1712 type_name (dtp->u.p.saved_type), type_name (type),
1713 dtp->u.p.item_count);
1715 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1719 if (dtp->u.p.saved_type == BT_NULL || dtp->u.p.saved_type == BT_CHARACTER)
1722 if (dtp->u.p.saved_length != len)
1725 "Read kind %d %s where kind %d is required for item %d",
1726 dtp->u.p.saved_length, type_name (dtp->u.p.saved_type), len,
1727 dtp->u.p.item_count);
1728 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1736 /* Top level data transfer subroutine for list reads. Because we have
1737 to deal with repeat counts, the data item is always saved after
1738 reading, usually in the dtp->u.p.value[] array. If a repeat count is
1739 greater than one, we copy the data item multiple times. */
1742 list_formatted_read_scalar (st_parameter_dt *dtp, volatile bt type, void *p,
1743 int kind, size_t size)
1750 dtp->u.p.namelist_mode = 0;
1752 dtp->u.p.eof_jump = &eof_jump;
1753 if (setjmp (eof_jump))
1755 generate_error (&dtp->common, LIBERROR_END, NULL);
1759 if (dtp->u.p.first_item)
1761 dtp->u.p.first_item = 0;
1762 dtp->u.p.input_complete = 0;
1763 dtp->u.p.repeat_count = 1;
1764 dtp->u.p.at_eol = 0;
1766 c = eat_spaces (dtp);
1767 if (is_separator (c))
1769 /* Found a null value. */
1770 eat_separator (dtp);
1771 dtp->u.p.repeat_count = 0;
1773 /* eat_separator sets this flag if the separator was a comma. */
1774 if (dtp->u.p.comma_flag)
1777 /* eat_separator sets this flag if the separator was a \n or \r. */
1778 if (dtp->u.p.at_eol)
1779 finish_separator (dtp);
1787 if (dtp->u.p.input_complete)
1790 if (dtp->u.p.repeat_count > 0)
1792 if (check_type (dtp, type, kind))
1797 if (dtp->u.p.at_eol)
1798 finish_separator (dtp);
1802 /* Trailing spaces prior to end of line. */
1803 if (dtp->u.p.at_eol)
1804 finish_separator (dtp);
1807 dtp->u.p.saved_type = BT_NULL;
1808 dtp->u.p.repeat_count = 1;
1814 read_integer (dtp, kind);
1817 read_logical (dtp, kind);
1820 read_character (dtp, kind);
1823 read_real (dtp, kind);
1826 read_complex (dtp, kind, size);
1829 internal_error (&dtp->common, "Bad type for list read");
1832 if (dtp->u.p.saved_type != BT_CHARACTER && dtp->u.p.saved_type != BT_NULL)
1833 dtp->u.p.saved_length = size;
1835 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1839 switch (dtp->u.p.saved_type)
1845 memcpy (p, dtp->u.p.value, size);
1849 if (dtp->u.p.saved_string)
1851 m = ((int) size < dtp->u.p.saved_used)
1852 ? (int) size : dtp->u.p.saved_used;
1854 memcpy (p, dtp->u.p.saved_string, m);
1857 q = (gfc_char4_t *) p;
1858 for (i = 0; i < m; i++)
1859 q[i] = (unsigned char) dtp->u.p.saved_string[i];
1863 /* Just delimiters encountered, nothing to copy but SPACE. */
1869 memset (((char *) p) + m, ' ', size - m);
1872 q = (gfc_char4_t *) p;
1873 for (i = m; i < (int) size; i++)
1874 q[i] = (unsigned char) ' ';
1883 if (--dtp->u.p.repeat_count <= 0)
1887 dtp->u.p.eof_jump = NULL;
1892 list_formatted_read (st_parameter_dt *dtp, bt type, void *p, int kind,
1893 size_t size, size_t nelems)
1897 size_t stride = type == BT_CHARACTER ?
1898 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
1902 /* Big loop over all the elements. */
1903 for (elem = 0; elem < nelems; elem++)
1905 dtp->u.p.item_count++;
1906 list_formatted_read_scalar (dtp, type, tmp + stride*elem, kind, size);
1911 /* Finish a list read. */
1914 finish_list_read (st_parameter_dt *dtp)
1920 if (dtp->u.p.at_eol)
1922 dtp->u.p.at_eol = 0;
1928 c = next_char (dtp);
1935 void namelist_read (st_parameter_dt *dtp)
1937 static void nml_match_name (char *name, int len)
1938 static int nml_query (st_parameter_dt *dtp)
1939 static int nml_get_obj_data (st_parameter_dt *dtp,
1940 namelist_info **prev_nl, char *, size_t)
1942 static void nml_untouch_nodes (st_parameter_dt *dtp)
1943 static namelist_info * find_nml_node (st_parameter_dt *dtp,
1945 static int nml_parse_qualifier(descriptor_dimension * ad,
1946 array_loop_spec * ls, int rank, char *)
1947 static void nml_touch_nodes (namelist_info * nl)
1948 static int nml_read_obj (namelist_info *nl, index_type offset,
1949 namelist_info **prev_nl, char *, size_t,
1950 index_type clow, index_type chigh)
1954 /* Inputs a rank-dimensional qualifier, which can contain
1955 singlets, doublets, triplets or ':' with the standard meanings. */
1958 nml_parse_qualifier (st_parameter_dt *dtp, descriptor_dimension *ad,
1959 array_loop_spec *ls, int rank, char *parse_err_msg,
1966 int is_array_section, is_char;
1970 is_array_section = 0;
1971 dtp->u.p.expanded_read = 0;
1973 /* See if this is a character substring qualifier we are looking for. */
1980 /* The next character in the stream should be the '('. */
1982 c = next_char (dtp);
1984 /* Process the qualifier, by dimension and triplet. */
1986 for (dim=0; dim < rank; dim++ )
1988 for (indx=0; indx<3; indx++)
1994 /* Process a potential sign. */
1995 c = next_char (dtp);
2006 unget_char (dtp, c);
2010 /* Process characters up to the next ':' , ',' or ')'. */
2013 c = next_char (dtp);
2018 is_array_section = 1;
2022 if ((c==',' && dim == rank -1)
2023 || (c==')' && dim < rank -1))
2026 sprintf (parse_err_msg, "Bad substring qualifier");
2028 sprintf (parse_err_msg, "Bad number of index fields");
2037 case ' ': case '\t':
2039 c = next_char (dtp);
2044 sprintf (parse_err_msg,
2045 "Bad character in substring qualifier");
2047 sprintf (parse_err_msg, "Bad character in index");
2051 if ((c == ',' || c == ')') && indx == 0
2052 && dtp->u.p.saved_string == 0)
2055 sprintf (parse_err_msg, "Null substring qualifier");
2057 sprintf (parse_err_msg, "Null index field");
2061 if ((c == ':' && indx == 1 && dtp->u.p.saved_string == 0)
2062 || (indx == 2 && dtp->u.p.saved_string == 0))
2065 sprintf (parse_err_msg, "Bad substring qualifier");
2067 sprintf (parse_err_msg, "Bad index triplet");
2071 if (is_char && !is_array_section)
2073 sprintf (parse_err_msg,
2074 "Missing colon in substring qualifier");
2078 /* If '( : ? )' or '( ? : )' break and flag read failure. */
2080 if ((c == ':' && indx == 0 && dtp->u.p.saved_string == 0)
2081 || (indx==1 && dtp->u.p.saved_string == 0))
2087 /* Now read the index. */
2088 if (convert_integer (dtp, sizeof(ssize_t), neg))
2091 sprintf (parse_err_msg, "Bad integer substring qualifier");
2093 sprintf (parse_err_msg, "Bad integer in index");
2099 /* Feed the index values to the triplet arrays. */
2103 memcpy (&ls[dim].start, dtp->u.p.value, sizeof(ssize_t));
2105 memcpy (&ls[dim].end, dtp->u.p.value, sizeof(ssize_t));
2107 memcpy (&ls[dim].step, dtp->u.p.value, sizeof(ssize_t));
2110 /* Singlet or doublet indices. */
2111 if (c==',' || c==')')
2115 memcpy (&ls[dim].start, dtp->u.p.value, sizeof(ssize_t));
2117 /* If -std=f95/2003 or an array section is specified,
2118 do not allow excess data to be processed. */
2119 if (is_array_section == 1
2120 || compile_options.allow_std < GFC_STD_GNU)
2121 ls[dim].end = ls[dim].start;
2123 dtp->u.p.expanded_read = 1;
2126 /* Check for non-zero rank. */
2127 if (is_array_section == 1 && ls[dim].start != ls[dim].end)
2134 /* Check the values of the triplet indices. */
2135 if ((ls[dim].start > (ssize_t)ad[dim].ubound)
2136 || (ls[dim].start < (ssize_t)ad[dim].lbound)
2137 || (ls[dim].end > (ssize_t)ad[dim].ubound)
2138 || (ls[dim].end < (ssize_t)ad[dim].lbound))
2141 sprintf (parse_err_msg, "Substring out of range");
2143 sprintf (parse_err_msg, "Index %d out of range", dim + 1);
2147 if (((ls[dim].end - ls[dim].start ) * ls[dim].step < 0)
2148 || (ls[dim].step == 0))
2150 sprintf (parse_err_msg, "Bad range in index %d", dim + 1);
2154 /* Initialise the loop index counter. */
2155 ls[dim].idx = ls[dim].start;
2165 static namelist_info *
2166 find_nml_node (st_parameter_dt *dtp, char * var_name)
2168 namelist_info * t = dtp->u.p.ionml;
2171 if (strcmp (var_name, t->var_name) == 0)
2181 /* Visits all the components of a derived type that have
2182 not explicitly been identified in the namelist input.
2183 touched is set and the loop specification initialised
2184 to default values */
2187 nml_touch_nodes (namelist_info * nl)
2189 index_type len = strlen (nl->var_name) + 1;
2191 char * ext_name = (char*)get_mem (len + 1);
2192 memcpy (ext_name, nl->var_name, len-1);
2193 memcpy (ext_name + len - 1, "%", 2);
2194 for (nl = nl->next; nl; nl = nl->next)
2196 if (strncmp (nl->var_name, ext_name, len) == 0)
2199 for (dim=0; dim < nl->var_rank; dim++)
2201 nl->ls[dim].step = 1;
2202 nl->ls[dim].end = nl->dim[dim].ubound;
2203 nl->ls[dim].start = nl->dim[dim].lbound;
2204 nl->ls[dim].idx = nl->ls[dim].start;
2210 free_mem (ext_name);
2214 /* Resets touched for the entire list of nml_nodes, ready for a
2218 nml_untouch_nodes (st_parameter_dt *dtp)
2221 for (t = dtp->u.p.ionml; t; t = t->next)
2226 /* Attempts to input name to namelist name. Returns
2227 dtp->u.p.nml_read_error = 1 on no match. */
2230 nml_match_name (st_parameter_dt *dtp, const char *name, index_type len)
2234 dtp->u.p.nml_read_error = 0;
2235 for (i = 0; i < len; i++)
2237 c = next_char (dtp);
2238 if (tolower (c) != tolower (name[i]))
2240 dtp->u.p.nml_read_error = 1;
2246 /* If the namelist read is from stdin, output the current state of the
2247 namelist to stdout. This is used to implement the non-standard query
2248 features, ? and =?. If c == '=' the full namelist is printed. Otherwise
2249 the names alone are printed. */
2252 nml_query (st_parameter_dt *dtp, char c)
2254 gfc_unit * temp_unit;
2259 static const index_type endlen = 3;
2260 static const char endl[] = "\r\n";
2261 static const char nmlend[] = "&end\r\n";
2263 static const index_type endlen = 2;
2264 static const char endl[] = "\n";
2265 static const char nmlend[] = "&end\n";
2268 if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
2271 /* Store the current unit and transfer to stdout. */
2273 temp_unit = dtp->u.p.current_unit;
2274 dtp->u.p.current_unit = find_unit (options.stdout_unit);
2276 if (dtp->u.p.current_unit)
2278 dtp->u.p.mode = WRITING;
2279 next_record (dtp, 0);
2281 /* Write the namelist in its entirety. */
2284 namelist_write (dtp);
2286 /* Or write the list of names. */
2290 /* "&namelist_name\n" */
2292 len = dtp->namelist_name_len;
2293 p = write_block (dtp, len + endlen);
2297 memcpy ((char*)(p + 1), dtp->namelist_name, len);
2298 memcpy ((char*)(p + len + 1), &endl, endlen - 1);
2299 for (nl = dtp->u.p.ionml; nl; nl = nl->next)
2303 len = strlen (nl->var_name);
2304 p = write_block (dtp, len + endlen);
2308 memcpy ((char*)(p + 1), nl->var_name, len);
2309 memcpy ((char*)(p + len + 1), &endl, endlen - 1);
2314 p = write_block (dtp, endlen + 3);
2316 memcpy (p, &nmlend, endlen + 3);
2319 /* Flush the stream to force immediate output. */
2321 fbuf_flush (dtp->u.p.current_unit, 1);
2322 flush (dtp->u.p.current_unit->s);
2323 unlock_unit (dtp->u.p.current_unit);
2328 /* Restore the current unit. */
2330 dtp->u.p.current_unit = temp_unit;
2331 dtp->u.p.mode = READING;
2335 /* Reads and stores the input for the namelist object nl. For an array,
2336 the function loops over the ranges defined by the loop specification.
2337 This default to all the data or to the specification from a qualifier.
2338 nml_read_obj recursively calls itself to read derived types. It visits
2339 all its own components but only reads data for those that were touched
2340 when the name was parsed. If a read error is encountered, an attempt is
2341 made to return to read a new object name because the standard allows too
2342 little data to be available. On the other hand, too much data is an
2346 nml_read_obj (st_parameter_dt *dtp, namelist_info * nl, index_type offset,
2347 namelist_info **pprev_nl, char *nml_err_msg,
2348 size_t nml_err_msg_size, index_type clow, index_type chigh)
2350 namelist_info * cmp;
2357 index_type obj_name_len;
2360 /* This object not touched in name parsing. */
2365 dtp->u.p.repeat_count = 0;
2371 case GFC_DTYPE_INTEGER:
2372 case GFC_DTYPE_LOGICAL:
2376 case GFC_DTYPE_REAL:
2377 dlen = size_from_real_kind (len);
2380 case GFC_DTYPE_COMPLEX:
2381 dlen = size_from_complex_kind (len);
2384 case GFC_DTYPE_CHARACTER:
2385 dlen = chigh ? (chigh - clow + 1) : nl->string_length;
2394 /* Update the pointer to the data, using the current index vector */
2396 pdata = (void*)(nl->mem_pos + offset);
2397 for (dim = 0; dim < nl->var_rank; dim++)
2398 pdata = (void*)(pdata + (nl->ls[dim].idx - nl->dim[dim].lbound) *
2399 nl->dim[dim].stride * nl->size);
2401 /* Reset the error flag and try to read next value, if
2402 dtp->u.p.repeat_count=0 */
2404 dtp->u.p.nml_read_error = 0;
2406 if (--dtp->u.p.repeat_count <= 0)
2408 if (dtp->u.p.input_complete)
2410 if (dtp->u.p.at_eol)
2411 finish_separator (dtp);
2412 if (dtp->u.p.input_complete)
2415 /* GFC_TYPE_UNKNOWN through for nulls and is detected
2416 after the switch block. */
2418 dtp->u.p.saved_type = GFC_DTYPE_UNKNOWN;
2423 case GFC_DTYPE_INTEGER:
2424 read_integer (dtp, len);
2427 case GFC_DTYPE_LOGICAL:
2428 read_logical (dtp, len);
2431 case GFC_DTYPE_CHARACTER:
2432 read_character (dtp, len);
2435 case GFC_DTYPE_REAL:
2436 read_real (dtp, len);
2439 case GFC_DTYPE_COMPLEX:
2440 read_complex (dtp, len, dlen);
2443 case GFC_DTYPE_DERIVED:
2444 obj_name_len = strlen (nl->var_name) + 1;
2445 obj_name = get_mem (obj_name_len+1);
2446 memcpy (obj_name, nl->var_name, obj_name_len-1);
2447 memcpy (obj_name + obj_name_len - 1, "%", 2);
2449 /* If reading a derived type, disable the expanded read warning
2450 since a single object can have multiple reads. */
2451 dtp->u.p.expanded_read = 0;
2453 /* Now loop over the components. Update the component pointer
2454 with the return value from nml_write_obj. This loop jumps
2455 past nested derived types by testing if the potential
2456 component name contains '%'. */
2458 for (cmp = nl->next;
2460 !strncmp (cmp->var_name, obj_name, obj_name_len) &&
2461 !strchr (cmp->var_name + obj_name_len, '%');
2465 if (nml_read_obj (dtp, cmp, (index_type)(pdata - nl->mem_pos),
2466 pprev_nl, nml_err_msg, nml_err_msg_size,
2467 clow, chigh) == FAILURE)
2469 free_mem (obj_name);
2473 if (dtp->u.p.input_complete)
2475 free_mem (obj_name);
2480 free_mem (obj_name);
2484 snprintf (nml_err_msg, nml_err_msg_size,
2485 "Bad type for namelist object %s", nl->var_name);
2486 internal_error (&dtp->common, nml_err_msg);
2491 /* The standard permits array data to stop short of the number of
2492 elements specified in the loop specification. In this case, we
2493 should be here with dtp->u.p.nml_read_error != 0. Control returns to
2494 nml_get_obj_data and an attempt is made to read object name. */
2497 if (dtp->u.p.nml_read_error)
2499 dtp->u.p.expanded_read = 0;
2503 if (dtp->u.p.saved_type == GFC_DTYPE_UNKNOWN)
2505 dtp->u.p.expanded_read = 0;
2509 /* Note the switch from GFC_DTYPE_type to BT_type at this point.
2510 This comes about because the read functions return BT_types. */
2512 switch (dtp->u.p.saved_type)
2519 memcpy (pdata, dtp->u.p.value, dlen);
2523 m = (dlen < dtp->u.p.saved_used) ? dlen : dtp->u.p.saved_used;
2524 pdata = (void*)( pdata + clow - 1 );
2525 memcpy (pdata, dtp->u.p.saved_string, m);
2527 memset ((void*)( pdata + m ), ' ', dlen - m);
2534 /* Warn if a non-standard expanded read occurs. A single read of a
2535 single object is acceptable. If a second read occurs, issue a warning
2536 and set the flag to zero to prevent further warnings. */
2537 if (dtp->u.p.expanded_read == 2)
2539 notify_std (&dtp->common, GFC_STD_GNU, "Non-standard expanded namelist read.");
2540 dtp->u.p.expanded_read = 0;
2543 /* If the expanded read warning flag is set, increment it,
2544 indicating that a single read has occurred. */
2545 if (dtp->u.p.expanded_read >= 1)
2546 dtp->u.p.expanded_read++;
2548 /* Break out of loop if scalar. */
2552 /* Now increment the index vector. */
2557 for (dim = 0; dim < nl->var_rank; dim++)
2559 nl->ls[dim].idx += nml_carry * nl->ls[dim].step;
2561 if (((nl->ls[dim].step > 0) && (nl->ls[dim].idx > nl->ls[dim].end))
2563 ((nl->ls[dim].step < 0) && (nl->ls[dim].idx < nl->ls[dim].end)))
2565 nl->ls[dim].idx = nl->ls[dim].start;
2569 } while (!nml_carry);
2571 if (dtp->u.p.repeat_count > 1)
2573 snprintf (nml_err_msg, nml_err_msg_size,
2574 "Repeat count too large for namelist object %s", nl->var_name);
2584 /* Parses the object name, including array and substring qualifiers. It
2585 iterates over derived type components, touching those components and
2586 setting their loop specifications, if there is a qualifier. If the
2587 object is itself a derived type, its components and subcomponents are
2588 touched. nml_read_obj is called at the end and this reads the data in
2589 the manner specified by the object name. */
2592 nml_get_obj_data (st_parameter_dt *dtp, namelist_info **pprev_nl,
2593 char *nml_err_msg, size_t nml_err_msg_size)
2597 namelist_info * first_nl = NULL;
2598 namelist_info * root_nl = NULL;
2599 int dim, parsed_rank;
2601 index_type clow, chigh;
2602 int non_zero_rank_count;
2604 /* Look for end of input or object name. If '?' or '=?' are encountered
2605 in stdin, print the node names or the namelist to stdout. */
2607 eat_separator (dtp);
2608 if (dtp->u.p.input_complete)
2611 if (dtp->u.p.at_eol)
2612 finish_separator (dtp);
2613 if (dtp->u.p.input_complete)
2616 c = next_char (dtp);
2620 c = next_char (dtp);
2623 sprintf (nml_err_msg, "namelist read: misplaced = sign");
2626 nml_query (dtp, '=');
2630 nml_query (dtp, '?');
2635 nml_match_name (dtp, "end", 3);
2636 if (dtp->u.p.nml_read_error)
2638 sprintf (nml_err_msg, "namelist not terminated with / or &end");
2642 dtp->u.p.input_complete = 1;
2649 /* Untouch all nodes of the namelist and reset the flag that is set for
2650 derived type components. */
2652 nml_untouch_nodes (dtp);
2654 non_zero_rank_count = 0;
2656 /* Get the object name - should '!' and '\n' be permitted separators? */
2664 if (!is_separator (c))
2665 push_char (dtp, tolower(c));
2666 c = next_char (dtp);
2667 } while (!( c=='=' || c==' ' || c=='\t' || c =='(' || c =='%' ));
2669 unget_char (dtp, c);
2671 /* Check that the name is in the namelist and get pointer to object.
2672 Three error conditions exist: (i) An attempt is being made to
2673 identify a non-existent object, following a failed data read or
2674 (ii) The object name does not exist or (iii) Too many data items
2675 are present for an object. (iii) gives the same error message
2678 push_char (dtp, '\0');
2682 size_t var_len = strlen (root_nl->var_name);
2684 = dtp->u.p.saved_string ? strlen (dtp->u.p.saved_string) : 0;
2685 char ext_name[var_len + saved_len + 1];
2687 memcpy (ext_name, root_nl->var_name, var_len);
2688 if (dtp->u.p.saved_string)
2689 memcpy (ext_name + var_len, dtp->u.p.saved_string, saved_len);
2690 ext_name[var_len + saved_len] = '\0';
2691 nl = find_nml_node (dtp, ext_name);
2694 nl = find_nml_node (dtp, dtp->u.p.saved_string);
2698 if (dtp->u.p.nml_read_error && *pprev_nl)
2699 snprintf (nml_err_msg, nml_err_msg_size,
2700 "Bad data for namelist object %s", (*pprev_nl)->var_name);
2703 snprintf (nml_err_msg, nml_err_msg_size,
2704 "Cannot match namelist object name %s",
2705 dtp->u.p.saved_string);
2710 /* Get the length, data length, base pointer and rank of the variable.
2711 Set the default loop specification first. */
2713 for (dim=0; dim < nl->var_rank; dim++)
2715 nl->ls[dim].step = 1;
2716 nl->ls[dim].end = nl->dim[dim].ubound;
2717 nl->ls[dim].start = nl->dim[dim].lbound;
2718 nl->ls[dim].idx = nl->ls[dim].start;
2721 /* Check to see if there is a qualifier: if so, parse it.*/
2723 if (c == '(' && nl->var_rank)
2726 if (nml_parse_qualifier (dtp, nl->dim, nl->ls, nl->var_rank,
2727 nml_err_msg, &parsed_rank) == FAILURE)
2729 char *nml_err_msg_end = strchr (nml_err_msg, '\0');
2730 snprintf (nml_err_msg_end,
2731 nml_err_msg_size - (nml_err_msg_end - nml_err_msg),
2732 " for namelist variable %s", nl->var_name);
2736 if (parsed_rank > 0)
2737 non_zero_rank_count++;
2739 c = next_char (dtp);
2740 unget_char (dtp, c);
2742 else if (nl->var_rank > 0)
2743 non_zero_rank_count++;
2745 /* Now parse a derived type component. The root namelist_info address
2746 is backed up, as is the previous component level. The component flag
2747 is set and the iteration is made by jumping back to get_name. */
2751 if (nl->type != GFC_DTYPE_DERIVED)
2753 snprintf (nml_err_msg, nml_err_msg_size,
2754 "Attempt to get derived component for %s", nl->var_name);
2758 if (!component_flag)
2763 c = next_char (dtp);
2767 /* Parse a character qualifier, if present. chigh = 0 is a default
2768 that signals that the string length = string_length. */
2773 if (c == '(' && nl->type == GFC_DTYPE_CHARACTER)
2775 descriptor_dimension chd[1] = { {1, clow, nl->string_length} };
2776 array_loop_spec ind[1] = { {1, clow, nl->string_length, 1} };
2778 if (nml_parse_qualifier (dtp, chd, ind, -1, nml_err_msg, &parsed_rank)
2781 char *nml_err_msg_end = strchr (nml_err_msg, '\0');
2782 snprintf (nml_err_msg_end,
2783 nml_err_msg_size - (nml_err_msg_end - nml_err_msg),
2784 " for namelist variable %s", nl->var_name);
2788 clow = ind[0].start;
2791 if (ind[0].step != 1)
2793 snprintf (nml_err_msg, nml_err_msg_size,
2794 "Step not allowed in substring qualifier"
2795 " for namelist object %s", nl->var_name);
2799 c = next_char (dtp);
2800 unget_char (dtp, c);
2803 /* If a derived type touch its components and restore the root
2804 namelist_info if we have parsed a qualified derived type
2807 if (nl->type == GFC_DTYPE_DERIVED)
2808 nml_touch_nodes (nl);
2809 if (component_flag && nl->var_rank > 0)
2812 /* Make sure no extraneous qualifiers are there. */
2816 snprintf (nml_err_msg, nml_err_msg_size,
2817 "Qualifier for a scalar or non-character namelist object %s",
2822 /* Make sure there is no more than one non-zero rank object. */
2823 if (non_zero_rank_count > 1)
2825 snprintf (nml_err_msg, nml_err_msg_size,
2826 "Multiple sub-objects with non-zero rank in namelist object %s",
2828 non_zero_rank_count = 0;
2832 /* According to the standard, an equal sign MUST follow an object name. The
2833 following is possibly lax - it allows comments, blank lines and so on to
2834 intervene. eat_spaces (dtp); c = next_char (dtp); would be compliant*/
2838 eat_separator (dtp);
2839 if (dtp->u.p.input_complete)
2842 if (dtp->u.p.at_eol)
2843 finish_separator (dtp);
2844 if (dtp->u.p.input_complete)
2847 c = next_char (dtp);
2851 snprintf (nml_err_msg, nml_err_msg_size,
2852 "Equal sign must follow namelist object name %s",
2857 if (nml_read_obj (dtp, nl, 0, pprev_nl, nml_err_msg, nml_err_msg_size,
2858 clow, chigh) == FAILURE)
2868 /* Entry point for namelist input. Goes through input until namelist name
2869 is matched. Then cycles through nml_get_obj_data until the input is
2870 completed or there is an error. */
2873 namelist_read (st_parameter_dt *dtp)
2877 char nml_err_msg[200];
2878 /* Pointer to the previously read object, in case attempt is made to read
2879 new object name. Should this fail, error message can give previous
2881 namelist_info *prev_nl = NULL;
2883 dtp->u.p.namelist_mode = 1;
2884 dtp->u.p.input_complete = 0;
2885 dtp->u.p.expanded_read = 0;
2887 dtp->u.p.eof_jump = &eof_jump;
2888 if (setjmp (eof_jump))
2890 dtp->u.p.eof_jump = NULL;
2891 generate_error (&dtp->common, LIBERROR_END, NULL);
2895 /* Look for &namelist_name . Skip all characters, testing for $nmlname.
2896 Exit on success or EOF. If '?' or '=?' encountered in stdin, print
2897 node names or namelist on stdout. */
2900 switch (c = next_char (dtp))
2911 c = next_char (dtp);
2913 nml_query (dtp, '=');
2915 unget_char (dtp, c);
2919 nml_query (dtp, '?');
2925 /* Match the name of the namelist. */
2927 nml_match_name (dtp, dtp->namelist_name, dtp->namelist_name_len);
2929 if (dtp->u.p.nml_read_error)
2932 /* A trailing space is required, we give a little lattitude here, 10.9.1. */
2933 c = next_char (dtp);
2934 if (!is_separator(c) && c != '!')
2936 unget_char (dtp, c);
2940 unget_char (dtp, c);
2941 eat_separator (dtp);
2943 /* Ready to read namelist objects. If there is an error in input
2944 from stdin, output the error message and continue. */
2946 while (!dtp->u.p.input_complete)
2948 if (nml_get_obj_data (dtp, &prev_nl, nml_err_msg, sizeof nml_err_msg)
2953 if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
2956 u = find_unit (options.stderr_unit);
2957 st_printf ("%s\n", nml_err_msg);
2967 dtp->u.p.eof_jump = NULL;
2972 /* All namelist error calls return from here */
2976 dtp->u.p.eof_jump = NULL;
2979 generate_error (&dtp->common, LIBERROR_READ_VALUE, nml_err_msg);