1 /* Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
3 Namelist input contributed by Paul Thomas
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with Libgfortran; see the file COPYING. If not, write to
28 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA. */
35 #include "libgfortran.h"
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')
64 /* Maximum repeat count. Less than ten times the maximum signed int32. */
66 #define MAX_REPEAT 200000000
69 /* Save a character to a string buffer, enlarging it as necessary. */
72 push_char (st_parameter_dt
*dtp
, char c
)
76 if (dtp
->u
.p
.saved_string
== NULL
)
78 if (dtp
->u
.p
.scratch
== NULL
)
79 dtp
->u
.p
.scratch
= get_mem (SCRATCH_SIZE
);
80 dtp
->u
.p
.saved_string
= dtp
->u
.p
.scratch
;
81 memset (dtp
->u
.p
.saved_string
, 0, SCRATCH_SIZE
);
82 dtp
->u
.p
.saved_length
= SCRATCH_SIZE
;
83 dtp
->u
.p
.saved_used
= 0;
86 if (dtp
->u
.p
.saved_used
>= dtp
->u
.p
.saved_length
)
88 dtp
->u
.p
.saved_length
= 2 * dtp
->u
.p
.saved_length
;
89 new = get_mem (2 * dtp
->u
.p
.saved_length
);
91 memset (new, 0, 2 * dtp
->u
.p
.saved_length
);
93 memcpy (new, dtp
->u
.p
.saved_string
, dtp
->u
.p
.saved_used
);
94 if (dtp
->u
.p
.saved_string
!= dtp
->u
.p
.scratch
)
95 free_mem (dtp
->u
.p
.saved_string
);
97 dtp
->u
.p
.saved_string
= new;
100 dtp
->u
.p
.saved_string
[dtp
->u
.p
.saved_used
++] = c
;
104 /* Free the input buffer if necessary. */
107 free_saved (st_parameter_dt
*dtp
)
109 if (dtp
->u
.p
.saved_string
== NULL
)
112 if (dtp
->u
.p
.saved_string
!= dtp
->u
.p
.scratch
)
113 free_mem (dtp
->u
.p
.saved_string
);
115 dtp
->u
.p
.saved_string
= NULL
;
116 dtp
->u
.p
.saved_used
= 0;
121 next_char (st_parameter_dt
*dtp
)
127 if (dtp
->u
.p
.last_char
!= '\0')
130 c
= dtp
->u
.p
.last_char
;
131 dtp
->u
.p
.last_char
= '\0';
137 /* Handle the end-of-record condition for internal array unit */
138 if (is_array_io(dtp
) && dtp
->u
.p
.current_unit
->bytes_left
== 0)
141 record
= next_array_record (dtp
, dtp
->u
.p
.current_unit
->ls
);
143 /* Check for "end-of-file" condition */
145 longjmp (*dtp
->u
.p
.eof_jump
, 1);
147 record
*= dtp
->u
.p
.current_unit
->recl
;
149 if (sseek (dtp
->u
.p
.current_unit
->s
, record
) == FAILURE
)
150 longjmp (*dtp
->u
.p
.eof_jump
, 1);
152 dtp
->u
.p
.current_unit
->bytes_left
= dtp
->u
.p
.current_unit
->recl
;
156 /* Get the next character and handle end-of-record conditions */
157 p
= salloc_r (dtp
->u
.p
.current_unit
->s
, &length
);
159 if (is_internal_unit(dtp
))
161 if (is_array_io(dtp
))
163 /* End of record is handled in the next pass through, above. The
164 check for NULL here is cautionary. */
167 generate_error (&dtp
->common
, ERROR_OS
, NULL
);
171 dtp
->u
.p
.current_unit
->bytes_left
--;
177 longjmp (*dtp
->u
.p
.eof_jump
, 1);
188 generate_error (&dtp
->common
, ERROR_OS
, NULL
);
192 longjmp (*dtp
->u
.p
.eof_jump
, 1);
196 dtp
->u
.p
.at_eol
= (c
== '\n' || c
== '\r');
201 /* Push a character back onto the input. */
204 unget_char (st_parameter_dt
*dtp
, char c
)
206 dtp
->u
.p
.last_char
= c
;
210 /* Skip over spaces in the input. Returns the nonspace character that
211 terminated the eating and also places it back on the input. */
214 eat_spaces (st_parameter_dt
*dtp
)
222 while (c
== ' ' || c
== '\t');
229 /* Skip over a separator. Technically, we don't always eat the whole
230 separator. This is because if we've processed the last input item,
231 then a separator is unnecessary. Plus the fact that operating
232 systems usually deliver console input on a line basis.
234 The upshot is that if we see a newline as part of reading a
235 separator, we stop reading. If there are more input items, we
236 continue reading the separator with finish_separator() which takes
237 care of the fact that we may or may not have seen a comma as part
241 eat_separator (st_parameter_dt
*dtp
)
246 dtp
->u
.p
.comma_flag
= 0;
252 dtp
->u
.p
.comma_flag
= 1;
257 dtp
->u
.p
.input_complete
= 1;
276 if (dtp
->u
.p
.namelist_mode
)
277 { /* Eat a namelist comment. */
285 /* Fall Through... */
294 /* Finish processing a separator that was interrupted by a newline.
295 If we're here, then another data item is present, so we finish what
296 we started on the previous line. */
299 finish_separator (st_parameter_dt
*dtp
)
310 if (dtp
->u
.p
.comma_flag
)
314 c
= eat_spaces (dtp
);
315 if (c
== '\n' || c
== '\r')
322 dtp
->u
.p
.input_complete
= 1;
323 if (!dtp
->u
.p
.namelist_mode
) next_record (dtp
, 0);
331 if (dtp
->u
.p
.namelist_mode
)
346 /* This function is needed to catch bad conversions so that namelist can
347 attempt to see if dtp->u.p.saved_string contains a new object name rather
351 nml_bad_return (st_parameter_dt
*dtp
, char c
)
353 if (dtp
->u
.p
.namelist_mode
)
355 dtp
->u
.p
.nml_read_error
= 1;
362 /* Convert an unsigned string to an integer. The length value is -1
363 if we are working on a repeat count. Returns nonzero if we have a
364 range problem. As a side effect, frees the dtp->u.p.saved_string. */
367 convert_integer (st_parameter_dt
*dtp
, int length
, int negative
)
369 char c
, *buffer
, message
[100];
371 GFC_INTEGER_LARGEST v
, max
, max10
;
373 buffer
= dtp
->u
.p
.saved_string
;
376 max
= (length
== -1) ? MAX_REPEAT
: max_value (length
, 1);
401 set_integer (dtp
->u
.p
.value
, v
, length
);
405 dtp
->u
.p
.repeat_count
= v
;
407 if (dtp
->u
.p
.repeat_count
== 0)
409 st_sprintf (message
, "Zero repeat count in item %d of list input",
410 dtp
->u
.p
.item_count
);
412 generate_error (&dtp
->common
, ERROR_READ_VALUE
, message
);
422 st_sprintf (message
, "Repeat count overflow in item %d of list input",
423 dtp
->u
.p
.item_count
);
425 st_sprintf (message
, "Integer overflow while reading item %d",
426 dtp
->u
.p
.item_count
);
429 generate_error (&dtp
->common
, ERROR_READ_VALUE
, message
);
435 /* Parse a repeat count for logical and complex values which cannot
436 begin with a digit. Returns nonzero if we are done, zero if we
437 should continue on. */
440 parse_repeat (st_parameter_dt
*dtp
)
442 char c
, message
[100];
468 repeat
= 10 * repeat
+ c
- '0';
470 if (repeat
> MAX_REPEAT
)
473 "Repeat count overflow in item %d of list input",
474 dtp
->u
.p
.item_count
);
476 generate_error (&dtp
->common
, ERROR_READ_VALUE
, message
);
486 "Zero repeat count in item %d of list input",
487 dtp
->u
.p
.item_count
);
489 generate_error (&dtp
->common
, ERROR_READ_VALUE
, message
);
501 dtp
->u
.p
.repeat_count
= repeat
;
505 st_sprintf (message
, "Bad repeat count in item %d of list input",
506 dtp
->u
.p
.item_count
);
508 generate_error (&dtp
->common
, ERROR_READ_VALUE
, message
);
513 /* Read a logical character on the input. */
516 read_logical (st_parameter_dt
*dtp
, int length
)
518 char c
, message
[100];
521 if (parse_repeat (dtp
))
557 return; /* Null value. */
563 dtp
->u
.p
.saved_type
= BT_LOGICAL
;
564 dtp
->u
.p
.saved_length
= length
;
566 /* Eat trailing garbage. */
571 while (!is_separator (c
));
576 set_integer ((int *) dtp
->u
.p
.value
, v
, length
);
582 if (nml_bad_return (dtp
, c
))
585 st_sprintf (message
, "Bad logical value while reading item %d",
586 dtp
->u
.p
.item_count
);
588 generate_error (&dtp
->common
, ERROR_READ_VALUE
, message
);
592 /* Reading integers is tricky because we can actually be reading a
593 repeat count. We have to store the characters in a buffer because
594 we could be reading an integer that is larger than the default int
595 used for repeat counts. */
598 read_integer (st_parameter_dt
*dtp
, int length
)
600 char c
, message
[100];
610 /* Fall through... */
616 CASE_SEPARATORS
: /* Single null. */
629 /* Take care of what may be a repeat count. */
641 push_char (dtp
, '\0');
644 CASE_SEPARATORS
: /* Not a repeat count. */
653 if (convert_integer (dtp
, -1, 0))
656 /* Get the real integer. */
671 /* Fall through... */
702 if (nml_bad_return (dtp
, c
))
707 st_sprintf (message
, "Bad integer for item %d in list input",
708 dtp
->u
.p
.item_count
);
709 generate_error (&dtp
->common
, ERROR_READ_VALUE
, message
);
717 push_char (dtp
, '\0');
718 if (convert_integer (dtp
, length
, negative
))
725 dtp
->u
.p
.saved_type
= BT_INTEGER
;
729 /* Read a character variable. */
732 read_character (st_parameter_dt
*dtp
, int length
__attribute__ ((unused
)))
734 char c
, quote
, message
[100];
736 quote
= ' '; /* Space means no quote character. */
746 unget_char (dtp
, c
); /* NULL value. */
756 if (dtp
->u
.p
.namelist_mode
)
765 /* Deal with a possible repeat count. */
778 goto done
; /* String was only digits! */
781 push_char (dtp
, '\0');
786 goto get_string
; /* Not a repeat count after all. */
791 if (convert_integer (dtp
, -1, 0))
794 /* Now get the real string. */
800 unget_char (dtp
, c
); /* Repeated NULL values. */
828 /* See if we have a doubled quote character or the end of
834 push_char (dtp
, quote
);
848 if (c
!= '\n' && c
!= '\r')
858 /* At this point, we have to have a separator, or else the string is
862 if (is_separator (c
))
866 dtp
->u
.p
.saved_type
= BT_CHARACTER
;
871 st_sprintf (message
, "Invalid string input in item %d",
872 dtp
->u
.p
.item_count
);
873 generate_error (&dtp
->common
, ERROR_READ_VALUE
, message
);
878 /* Parse a component of a complex constant or a real number that we
879 are sure is already there. This is a straight real number parser. */
882 parse_real (st_parameter_dt
*dtp
, void *buffer
, int length
)
884 char c
, message
[100];
888 if (c
== '-' || c
== '+')
894 if (!isdigit (c
) && c
!= '.')
899 seen_dp
= (c
== '.') ? 1 : 0;
922 push_char (dtp
, 'e');
927 push_char (dtp
, 'e');
943 if (c
!= '-' && c
!= '+')
944 push_char (dtp
, '+');
976 push_char (dtp
, '\0');
978 m
= convert_real (dtp
, buffer
, dtp
->u
.p
.saved_string
, length
);
985 st_sprintf (message
, "Bad floating point number for item %d",
986 dtp
->u
.p
.item_count
);
987 generate_error (&dtp
->common
, ERROR_READ_VALUE
, message
);
993 /* Reading a complex number is straightforward because we can tell
994 what it is right away. */
997 read_complex (st_parameter_dt
*dtp
, int kind
, size_t size
)
1002 if (parse_repeat (dtp
))
1005 c
= next_char (dtp
);
1012 unget_char (dtp
, c
);
1013 eat_separator (dtp
);
1021 if (parse_real (dtp
, dtp
->u
.p
.value
, kind
))
1026 c
= next_char (dtp
);
1027 if (c
== '\n' || c
== '\r')
1030 unget_char (dtp
, c
);
1032 if (next_char (dtp
) != ',')
1037 c
= next_char (dtp
);
1038 if (c
== '\n' || c
== '\r')
1041 unget_char (dtp
, c
);
1043 if (parse_real (dtp
, dtp
->u
.p
.value
+ size
/ 2, kind
))
1047 if (next_char (dtp
) != ')')
1050 c
= next_char (dtp
);
1051 if (!is_separator (c
))
1054 unget_char (dtp
, c
);
1055 eat_separator (dtp
);
1058 dtp
->u
.p
.saved_type
= BT_COMPLEX
;
1063 if (nml_bad_return (dtp
, c
))
1066 st_sprintf (message
, "Bad complex value in item %d of list input",
1067 dtp
->u
.p
.item_count
);
1069 generate_error (&dtp
->common
, ERROR_READ_VALUE
, message
);
1073 /* Parse a real number with a possible repeat count. */
1076 read_real (st_parameter_dt
*dtp
, int length
)
1078 char c
, message
[100];
1083 c
= next_char (dtp
);
1100 unget_char (dtp
, c
); /* Single null. */
1101 eat_separator (dtp
);
1108 /* Get the digit string that might be a repeat count. */
1112 c
= next_char (dtp
);
1135 push_char (dtp
, 'e');
1137 c
= next_char (dtp
);
1141 push_char (dtp
, '\0');
1145 if (c
!= '\n' && c
!= ',' && c
!= '\r')
1146 unget_char (dtp
, c
);
1155 if (convert_integer (dtp
, -1, 0))
1158 /* Now get the number itself. */
1160 c
= next_char (dtp
);
1161 if (is_separator (c
))
1162 { /* Repeated null value. */
1163 unget_char (dtp
, c
);
1164 eat_separator (dtp
);
1168 if (c
!= '-' && c
!= '+')
1169 push_char (dtp
, '+');
1174 c
= next_char (dtp
);
1177 if (!isdigit (c
) && c
!= '.')
1193 c
= next_char (dtp
);
1219 push_char (dtp
, 'e');
1221 c
= next_char (dtp
);
1230 push_char (dtp
, 'e');
1232 c
= next_char (dtp
);
1233 if (c
!= '+' && c
!= '-')
1234 push_char (dtp
, '+');
1238 c
= next_char (dtp
);
1248 c
= next_char (dtp
);
1265 unget_char (dtp
, c
);
1266 eat_separator (dtp
);
1267 push_char (dtp
, '\0');
1268 if (convert_real (dtp
, dtp
->u
.p
.value
, dtp
->u
.p
.saved_string
, length
))
1272 dtp
->u
.p
.saved_type
= BT_REAL
;
1277 if (nml_bad_return (dtp
, c
))
1280 st_sprintf (message
, "Bad real number in item %d of list input",
1281 dtp
->u
.p
.item_count
);
1283 generate_error (&dtp
->common
, ERROR_READ_VALUE
, message
);
1287 /* Check the current type against the saved type to make sure they are
1288 compatible. Returns nonzero if incompatible. */
1291 check_type (st_parameter_dt
*dtp
, bt type
, int len
)
1295 if (dtp
->u
.p
.saved_type
!= BT_NULL
&& dtp
->u
.p
.saved_type
!= type
)
1297 st_sprintf (message
, "Read type %s where %s was expected for item %d",
1298 type_name (dtp
->u
.p
.saved_type
), type_name (type
),
1299 dtp
->u
.p
.item_count
);
1301 generate_error (&dtp
->common
, ERROR_READ_VALUE
, message
);
1305 if (dtp
->u
.p
.saved_type
== BT_NULL
|| dtp
->u
.p
.saved_type
== BT_CHARACTER
)
1308 if (dtp
->u
.p
.saved_length
!= len
)
1310 st_sprintf (message
,
1311 "Read kind %d %s where kind %d is required for item %d",
1312 dtp
->u
.p
.saved_length
, type_name (dtp
->u
.p
.saved_type
), len
,
1313 dtp
->u
.p
.item_count
);
1314 generate_error (&dtp
->common
, ERROR_READ_VALUE
, message
);
1322 /* Top level data transfer subroutine for list reads. Because we have
1323 to deal with repeat counts, the data item is always saved after
1324 reading, usually in the dtp->u.p.value[] array. If a repeat count is
1325 greater than one, we copy the data item multiple times. */
1328 list_formatted_read_scalar (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
1335 dtp
->u
.p
.namelist_mode
= 0;
1337 dtp
->u
.p
.eof_jump
= &eof_jump
;
1338 if (setjmp (eof_jump
))
1340 generate_error (&dtp
->common
, ERROR_END
, NULL
);
1344 if (dtp
->u
.p
.first_item
)
1346 dtp
->u
.p
.first_item
= 0;
1347 dtp
->u
.p
.input_complete
= 0;
1348 dtp
->u
.p
.repeat_count
= 1;
1349 dtp
->u
.p
.at_eol
= 0;
1351 c
= eat_spaces (dtp
);
1352 if (is_separator (c
))
1353 { /* Found a null value. */
1354 eat_separator (dtp
);
1355 dtp
->u
.p
.repeat_count
= 0;
1362 if (dtp
->u
.p
.input_complete
)
1365 if (dtp
->u
.p
.repeat_count
> 0)
1367 if (check_type (dtp
, type
, kind
))
1372 if (dtp
->u
.p
.at_eol
)
1373 finish_separator (dtp
);
1377 /* trailing spaces prior to end of line */
1378 if (dtp
->u
.p
.at_eol
)
1379 finish_separator (dtp
);
1382 dtp
->u
.p
.saved_type
= BT_NULL
;
1383 dtp
->u
.p
.repeat_count
= 1;
1389 read_integer (dtp
, kind
);
1392 read_logical (dtp
, kind
);
1395 read_character (dtp
, kind
);
1398 read_real (dtp
, kind
);
1401 read_complex (dtp
, kind
, size
);
1404 internal_error (&dtp
->common
, "Bad type for list read");
1407 if (dtp
->u
.p
.saved_type
!= BT_CHARACTER
&& dtp
->u
.p
.saved_type
!= BT_NULL
)
1408 dtp
->u
.p
.saved_length
= size
;
1410 if ((dtp
->common
.flags
& IOPARM_LIBRETURN_MASK
) != IOPARM_LIBRETURN_OK
)
1414 switch (dtp
->u
.p
.saved_type
)
1420 memcpy (p
, dtp
->u
.p
.value
, size
);
1424 if (dtp
->u
.p
.saved_string
)
1426 m
= ((int) size
< dtp
->u
.p
.saved_used
)
1427 ? (int) size
: dtp
->u
.p
.saved_used
;
1428 memcpy (p
, dtp
->u
.p
.saved_string
, m
);
1431 /* Just delimiters encountered, nothing to copy but SPACE. */
1435 memset (((char *) p
) + m
, ' ', size
- m
);
1442 if (--dtp
->u
.p
.repeat_count
<= 0)
1446 dtp
->u
.p
.eof_jump
= NULL
;
1451 list_formatted_read (st_parameter_dt
*dtp
, bt type
, void *p
, int kind
,
1452 size_t size
, size_t nelems
)
1459 /* Big loop over all the elements. */
1460 for (elem
= 0; elem
< nelems
; elem
++)
1462 dtp
->u
.p
.item_count
++;
1463 list_formatted_read_scalar (dtp
, type
, tmp
+ size
*elem
, kind
, size
);
1468 /* Finish a list read. */
1471 finish_list_read (st_parameter_dt
*dtp
)
1477 if (dtp
->u
.p
.at_eol
)
1479 dtp
->u
.p
.at_eol
= 0;
1485 c
= next_char (dtp
);
1492 void namelist_read (st_parameter_dt *dtp)
1494 static void nml_match_name (char *name, int len)
1495 static int nml_query (st_parameter_dt *dtp)
1496 static int nml_get_obj_data (st_parameter_dt *dtp,
1497 namelist_info **prev_nl, char *)
1499 static void nml_untouch_nodes (st_parameter_dt *dtp)
1500 static namelist_info * find_nml_node (st_parameter_dt *dtp,
1502 static int nml_parse_qualifier(descriptor_dimension * ad,
1503 array_loop_spec * ls, int rank, char *)
1504 static void nml_touch_nodes (namelist_info * nl)
1505 static int nml_read_obj (namelist_info *nl, index_type offset,
1506 namelist_info **prev_nl, char *,
1507 index_type clow, index_type chigh)
1511 /* Inputs a rank-dimensional qualifier, which can contain
1512 singlets, doublets, triplets or ':' with the standard meanings. */
1515 nml_parse_qualifier (st_parameter_dt
*dtp
, descriptor_dimension
*ad
,
1516 array_loop_spec
*ls
, int rank
, char *parse_err_msg
)
1524 /* The next character in the stream should be the '('. */
1526 c
= next_char (dtp
);
1528 /* Process the qualifier, by dimension and triplet. */
1530 for (dim
=0; dim
< rank
; dim
++ )
1532 for (indx
=0; indx
<3; indx
++)
1538 /* Process a potential sign. */
1539 c
= next_char (dtp
);
1550 unget_char (dtp
, c
);
1554 /* Process characters up to the next ':' , ',' or ')'. */
1557 c
= next_char (dtp
);
1565 if ((c
==',' && dim
== rank
-1)
1566 || (c
==')' && dim
< rank
-1))
1568 st_sprintf (parse_err_msg
,
1569 "Bad number of index fields");
1578 case ' ': case '\t':
1580 c
= next_char (dtp
);
1584 st_sprintf (parse_err_msg
, "Bad character in index");
1588 if ((c
== ',' || c
== ')') && indx
== 0
1589 && dtp
->u
.p
.saved_string
== 0)
1591 st_sprintf (parse_err_msg
, "Null index field");
1595 if ((c
== ':' && indx
== 1 && dtp
->u
.p
.saved_string
== 0)
1596 || (indx
== 2 && dtp
->u
.p
.saved_string
== 0))
1598 st_sprintf(parse_err_msg
, "Bad index triplet");
1602 /* If '( : ? )' or '( ? : )' break and flag read failure. */
1604 if ((c
== ':' && indx
== 0 && dtp
->u
.p
.saved_string
== 0)
1605 || (indx
==1 && dtp
->u
.p
.saved_string
== 0))
1611 /* Now read the index. */
1612 if (convert_integer (dtp
, sizeof(ssize_t
), neg
))
1614 st_sprintf (parse_err_msg
, "Bad integer in index");
1620 /* Feed the index values to the triplet arrays. */
1624 memcpy (&ls
[dim
].start
, dtp
->u
.p
.value
, sizeof(ssize_t
));
1626 memcpy (&ls
[dim
].end
, dtp
->u
.p
.value
, sizeof(ssize_t
));
1628 memcpy (&ls
[dim
].step
, dtp
->u
.p
.value
, sizeof(ssize_t
));
1631 /* Singlet or doublet indices. */
1632 if (c
==',' || c
==')')
1636 memcpy (&ls
[dim
].start
, dtp
->u
.p
.value
, sizeof(ssize_t
));
1637 ls
[dim
].end
= ls
[dim
].start
;
1643 /* Check the values of the triplet indices. */
1644 if ((ls
[dim
].start
> (ssize_t
)ad
[dim
].ubound
)
1645 || (ls
[dim
].start
< (ssize_t
)ad
[dim
].lbound
)
1646 || (ls
[dim
].end
> (ssize_t
)ad
[dim
].ubound
)
1647 || (ls
[dim
].end
< (ssize_t
)ad
[dim
].lbound
))
1649 st_sprintf (parse_err_msg
, "Index %d out of range", dim
+ 1);
1652 if (((ls
[dim
].end
- ls
[dim
].start
) * ls
[dim
].step
< 0)
1653 || (ls
[dim
].step
== 0))
1655 st_sprintf (parse_err_msg
, "Bad range in index %d", dim
+ 1);
1659 /* Initialise the loop index counter. */
1660 ls
[dim
].idx
= ls
[dim
].start
;
1670 static namelist_info
*
1671 find_nml_node (st_parameter_dt
*dtp
, char * var_name
)
1673 namelist_info
* t
= dtp
->u
.p
.ionml
;
1676 if (strcmp (var_name
, t
->var_name
) == 0)
1686 /* Visits all the components of a derived type that have
1687 not explicitly been identified in the namelist input.
1688 touched is set and the loop specification initialised
1689 to default values */
1692 nml_touch_nodes (namelist_info
* nl
)
1694 index_type len
= strlen (nl
->var_name
) + 1;
1696 char * ext_name
= (char*)get_mem (len
+ 1);
1697 strcpy (ext_name
, nl
->var_name
);
1698 strcat (ext_name
, "%");
1699 for (nl
= nl
->next
; nl
; nl
= nl
->next
)
1701 if (strncmp (nl
->var_name
, ext_name
, len
) == 0)
1704 for (dim
=0; dim
< nl
->var_rank
; dim
++)
1706 nl
->ls
[dim
].step
= 1;
1707 nl
->ls
[dim
].end
= nl
->dim
[dim
].ubound
;
1708 nl
->ls
[dim
].start
= nl
->dim
[dim
].lbound
;
1709 nl
->ls
[dim
].idx
= nl
->ls
[dim
].start
;
1715 free_mem (ext_name
);
1719 /* Resets touched for the entire list of nml_nodes, ready for a
1723 nml_untouch_nodes (st_parameter_dt
*dtp
)
1726 for (t
= dtp
->u
.p
.ionml
; t
; t
= t
->next
)
1731 /* Attempts to input name to namelist name. Returns
1732 dtp->u.p.nml_read_error = 1 on no match. */
1735 nml_match_name (st_parameter_dt
*dtp
, const char *name
, index_type len
)
1739 dtp
->u
.p
.nml_read_error
= 0;
1740 for (i
= 0; i
< len
; i
++)
1742 c
= next_char (dtp
);
1743 if (tolower (c
) != tolower (name
[i
]))
1745 dtp
->u
.p
.nml_read_error
= 1;
1751 /* If the namelist read is from stdin, output the current state of the
1752 namelist to stdout. This is used to implement the non-standard query
1753 features, ? and =?. If c == '=' the full namelist is printed. Otherwise
1754 the names alone are printed. */
1757 nml_query (st_parameter_dt
*dtp
, char c
)
1759 gfc_unit
* temp_unit
;
1764 if (dtp
->u
.p
.current_unit
->unit_number
!= options
.stdin_unit
)
1767 /* Store the current unit and transfer to stdout. */
1769 temp_unit
= dtp
->u
.p
.current_unit
;
1770 dtp
->u
.p
.current_unit
= find_unit (options
.stdout_unit
);
1772 if (dtp
->u
.p
.current_unit
)
1774 dtp
->u
.p
.mode
= WRITING
;
1775 next_record (dtp
, 0);
1777 /* Write the namelist in its entirety. */
1780 namelist_write (dtp
);
1782 /* Or write the list of names. */
1787 /* "&namelist_name\n" */
1789 len
= dtp
->namelist_name_len
;
1791 p
= write_block (dtp
, len
+ 3);
1793 p
= write_block (dtp
, len
+ 2);
1798 memcpy ((char*)(p
+ 1), dtp
->namelist_name
, len
);
1800 memcpy ((char*)(p
+ len
+ 1), "\r\n", 2);
1802 memcpy ((char*)(p
+ len
+ 1), "\n", 1);
1804 for (nl
= dtp
->u
.p
.ionml
; nl
; nl
= nl
->next
)
1809 len
= strlen (nl
->var_name
);
1811 p
= write_block (dtp
, len
+ 3);
1813 p
= write_block (dtp
, len
+ 2);
1818 memcpy ((char*)(p
+ 1), nl
->var_name
, len
);
1820 memcpy ((char*)(p
+ len
+ 1), "\r\n", 2);
1822 memcpy ((char*)(p
+ len
+ 1), "\n", 1);
1829 p
= write_block (dtp
, 6);
1831 p
= write_block (dtp
, 5);
1836 memcpy (p
, "&end\r\n", 6);
1838 memcpy (p
, "&end\n", 5);
1842 /* Flush the stream to force immediate output. */
1844 flush (dtp
->u
.p
.current_unit
->s
);
1845 unlock_unit (dtp
->u
.p
.current_unit
);
1850 /* Restore the current unit. */
1852 dtp
->u
.p
.current_unit
= temp_unit
;
1853 dtp
->u
.p
.mode
= READING
;
1857 /* Reads and stores the input for the namelist object nl. For an array,
1858 the function loops over the ranges defined by the loop specification.
1859 This default to all the data or to the specification from a qualifier.
1860 nml_read_obj recursively calls itself to read derived types. It visits
1861 all its own components but only reads data for those that were touched
1862 when the name was parsed. If a read error is encountered, an attempt is
1863 made to return to read a new object name because the standard allows too
1864 little data to be available. On the other hand, too much data is an
1868 nml_read_obj (st_parameter_dt
*dtp
, namelist_info
* nl
, index_type offset
,
1869 namelist_info
**pprev_nl
, char *nml_err_msg
,
1870 index_type clow
, index_type chigh
)
1873 namelist_info
* cmp
;
1880 index_type obj_name_len
;
1883 /* This object not touched in name parsing. */
1888 dtp
->u
.p
.repeat_count
= 0;
1895 case GFC_DTYPE_INTEGER
:
1896 case GFC_DTYPE_LOGICAL
:
1900 case GFC_DTYPE_REAL
:
1901 dlen
= size_from_real_kind (len
);
1904 case GFC_DTYPE_COMPLEX
:
1905 dlen
= size_from_complex_kind (len
);
1908 case GFC_DTYPE_CHARACTER
:
1909 dlen
= chigh
? (chigh
- clow
+ 1) : nl
->string_length
;
1919 /* Update the pointer to the data, using the current index vector */
1921 pdata
= (void*)(nl
->mem_pos
+ offset
);
1922 for (dim
= 0; dim
< nl
->var_rank
; dim
++)
1923 pdata
= (void*)(pdata
+ (nl
->ls
[dim
].idx
- nl
->dim
[dim
].lbound
) *
1924 nl
->dim
[dim
].stride
* nl
->size
);
1926 /* Reset the error flag and try to read next value, if
1927 dtp->u.p.repeat_count=0 */
1929 dtp
->u
.p
.nml_read_error
= 0;
1931 if (--dtp
->u
.p
.repeat_count
<= 0)
1933 if (dtp
->u
.p
.input_complete
)
1935 if (dtp
->u
.p
.at_eol
)
1936 finish_separator (dtp
);
1937 if (dtp
->u
.p
.input_complete
)
1940 /* GFC_TYPE_UNKNOWN through for nulls and is detected
1941 after the switch block. */
1943 dtp
->u
.p
.saved_type
= GFC_DTYPE_UNKNOWN
;
1948 case GFC_DTYPE_INTEGER
:
1949 read_integer (dtp
, len
);
1952 case GFC_DTYPE_LOGICAL
:
1953 read_logical (dtp
, len
);
1956 case GFC_DTYPE_CHARACTER
:
1957 read_character (dtp
, len
);
1960 case GFC_DTYPE_REAL
:
1961 read_real (dtp
, len
);
1964 case GFC_DTYPE_COMPLEX
:
1965 read_complex (dtp
, len
, dlen
);
1968 case GFC_DTYPE_DERIVED
:
1969 obj_name_len
= strlen (nl
->var_name
) + 1;
1970 obj_name
= get_mem (obj_name_len
+1);
1971 strcpy (obj_name
, nl
->var_name
);
1972 strcat (obj_name
, "%");
1974 /* Now loop over the components. Update the component pointer
1975 with the return value from nml_write_obj. This loop jumps
1976 past nested derived types by testing if the potential
1977 component name contains '%'. */
1979 for (cmp
= nl
->next
;
1981 !strncmp (cmp
->var_name
, obj_name
, obj_name_len
) &&
1982 !strchr (cmp
->var_name
+ obj_name_len
, '%');
1986 if (nml_read_obj (dtp
, cmp
, (index_type
)(pdata
- nl
->mem_pos
),
1987 pprev_nl
, nml_err_msg
, clow
, chigh
)
1990 free_mem (obj_name
);
1994 if (dtp
->u
.p
.input_complete
)
1996 free_mem (obj_name
);
2001 free_mem (obj_name
);
2005 st_sprintf (nml_err_msg
, "Bad type for namelist object %s",
2007 internal_error (&dtp
->common
, nml_err_msg
);
2012 /* The standard permits array data to stop short of the number of
2013 elements specified in the loop specification. In this case, we
2014 should be here with dtp->u.p.nml_read_error != 0. Control returns to
2015 nml_get_obj_data and an attempt is made to read object name. */
2018 if (dtp
->u
.p
.nml_read_error
)
2021 if (dtp
->u
.p
.saved_type
== GFC_DTYPE_UNKNOWN
)
2025 /* Note the switch from GFC_DTYPE_type to BT_type at this point.
2026 This comes about because the read functions return BT_types. */
2028 switch (dtp
->u
.p
.saved_type
)
2035 memcpy (pdata
, dtp
->u
.p
.value
, dlen
);
2039 m
= (dlen
< dtp
->u
.p
.saved_used
) ? dlen
: dtp
->u
.p
.saved_used
;
2040 pdata
= (void*)( pdata
+ clow
- 1 );
2041 memcpy (pdata
, dtp
->u
.p
.saved_string
, m
);
2043 memset ((void*)( pdata
+ m
), ' ', dlen
- m
);
2050 /* Break out of loop if scalar. */
2055 /* Now increment the index vector. */
2060 for (dim
= 0; dim
< nl
->var_rank
; dim
++)
2062 nl
->ls
[dim
].idx
+= nml_carry
* nl
->ls
[dim
].step
;
2064 if (((nl
->ls
[dim
].step
> 0) && (nl
->ls
[dim
].idx
> nl
->ls
[dim
].end
))
2066 ((nl
->ls
[dim
].step
< 0) && (nl
->ls
[dim
].idx
< nl
->ls
[dim
].end
)))
2068 nl
->ls
[dim
].idx
= nl
->ls
[dim
].start
;
2072 } while (!nml_carry
);
2074 if (dtp
->u
.p
.repeat_count
> 1)
2076 st_sprintf (nml_err_msg
, "Repeat count too large for namelist object %s" ,
2087 /* Parses the object name, including array and substring qualifiers. It
2088 iterates over derived type components, touching those components and
2089 setting their loop specifications, if there is a qualifier. If the
2090 object is itself a derived type, its components and subcomponents are
2091 touched. nml_read_obj is called at the end and this reads the data in
2092 the manner specified by the object name. */
2095 nml_get_obj_data (st_parameter_dt
*dtp
, namelist_info
**pprev_nl
,
2100 namelist_info
* first_nl
= NULL
;
2101 namelist_info
* root_nl
= NULL
;
2104 char parse_err_msg
[30];
2105 index_type clow
, chigh
;
2107 /* Look for end of input or object name. If '?' or '=?' are encountered
2108 in stdin, print the node names or the namelist to stdout. */
2110 eat_separator (dtp
);
2111 if (dtp
->u
.p
.input_complete
)
2114 if (dtp
->u
.p
.at_eol
)
2115 finish_separator (dtp
);
2116 if (dtp
->u
.p
.input_complete
)
2119 c
= next_char (dtp
);
2123 c
= next_char (dtp
);
2126 st_sprintf (nml_err_msg
, "namelist read: missplaced = sign");
2129 nml_query (dtp
, '=');
2133 nml_query (dtp
, '?');
2138 nml_match_name (dtp
, "end", 3);
2139 if (dtp
->u
.p
.nml_read_error
)
2141 st_sprintf (nml_err_msg
, "namelist not terminated with / or &end");
2145 dtp
->u
.p
.input_complete
= 1;
2152 /* Untouch all nodes of the namelist and reset the flag that is set for
2153 derived type components. */
2155 nml_untouch_nodes (dtp
);
2158 /* Get the object name - should '!' and '\n' be permitted separators? */
2166 push_char (dtp
, tolower(c
));
2167 c
= next_char (dtp
);
2168 } while (!( c
=='=' || c
==' ' || c
=='\t' || c
=='(' || c
=='%' ));
2170 unget_char (dtp
, c
);
2172 /* Check that the name is in the namelist and get pointer to object.
2173 Three error conditions exist: (i) An attempt is being made to
2174 identify a non-existent object, following a failed data read or
2175 (ii) The object name does not exist or (iii) Too many data items
2176 are present for an object. (iii) gives the same error message
2179 push_char (dtp
, '\0');
2183 size_t var_len
= strlen (root_nl
->var_name
);
2185 = dtp
->u
.p
.saved_string
? strlen (dtp
->u
.p
.saved_string
) : 0;
2186 char ext_name
[var_len
+ saved_len
+ 1];
2188 memcpy (ext_name
, root_nl
->var_name
, var_len
);
2189 if (dtp
->u
.p
.saved_string
)
2190 memcpy (ext_name
+ var_len
, dtp
->u
.p
.saved_string
, saved_len
);
2191 ext_name
[var_len
+ saved_len
] = '\0';
2192 nl
= find_nml_node (dtp
, ext_name
);
2195 nl
= find_nml_node (dtp
, dtp
->u
.p
.saved_string
);
2199 if (dtp
->u
.p
.nml_read_error
&& *pprev_nl
)
2200 st_sprintf (nml_err_msg
, "Bad data for namelist object %s",
2201 (*pprev_nl
)->var_name
);
2204 st_sprintf (nml_err_msg
, "Cannot match namelist object name %s",
2205 dtp
->u
.p
.saved_string
);
2210 /* Get the length, data length, base pointer and rank of the variable.
2211 Set the default loop specification first. */
2213 for (dim
=0; dim
< nl
->var_rank
; dim
++)
2215 nl
->ls
[dim
].step
= 1;
2216 nl
->ls
[dim
].end
= nl
->dim
[dim
].ubound
;
2217 nl
->ls
[dim
].start
= nl
->dim
[dim
].lbound
;
2218 nl
->ls
[dim
].idx
= nl
->ls
[dim
].start
;
2221 /* Check to see if there is a qualifier: if so, parse it.*/
2223 if (c
== '(' && nl
->var_rank
)
2225 if (nml_parse_qualifier (dtp
, nl
->dim
, nl
->ls
, nl
->var_rank
,
2226 parse_err_msg
) == FAILURE
)
2228 st_sprintf (nml_err_msg
, "%s for namelist variable %s",
2229 parse_err_msg
, nl
->var_name
);
2232 c
= next_char (dtp
);
2233 unget_char (dtp
, c
);
2236 /* Now parse a derived type component. The root namelist_info address
2237 is backed up, as is the previous component level. The component flag
2238 is set and the iteration is made by jumping back to get_name. */
2243 if (nl
->type
!= GFC_DTYPE_DERIVED
)
2245 st_sprintf (nml_err_msg
, "Attempt to get derived component for %s",
2250 if (!component_flag
)
2255 c
= next_char (dtp
);
2260 /* Parse a character qualifier, if present. chigh = 0 is a default
2261 that signals that the string length = string_length. */
2266 if (c
== '(' && nl
->type
== GFC_DTYPE_CHARACTER
)
2268 descriptor_dimension chd
[1] = { {1, clow
, nl
->string_length
} };
2269 array_loop_spec ind
[1] = { {1, clow
, nl
->string_length
, 1} };
2271 if (nml_parse_qualifier (dtp
, chd
, ind
, 1, parse_err_msg
) == FAILURE
)
2273 st_sprintf (nml_err_msg
, "%s for namelist variable %s",
2274 parse_err_msg
, nl
->var_name
);
2278 clow
= ind
[0].start
;
2281 if (ind
[0].step
!= 1)
2283 st_sprintf (nml_err_msg
,
2284 "Bad step in substring for namelist object %s",
2289 c
= next_char (dtp
);
2290 unget_char (dtp
, c
);
2293 /* If a derived type touch its components and restore the root
2294 namelist_info if we have parsed a qualified derived type
2297 if (nl
->type
== GFC_DTYPE_DERIVED
)
2298 nml_touch_nodes (nl
);
2302 /*make sure no extraneous qualifiers are there.*/
2306 st_sprintf (nml_err_msg
, "Qualifier for a scalar or non-character"
2307 " namelist object %s", nl
->var_name
);
2311 /* According to the standard, an equal sign MUST follow an object name. The
2312 following is possibly lax - it allows comments, blank lines and so on to
2313 intervene. eat_spaces (dtp); c = next_char (dtp); would be compliant*/
2317 eat_separator (dtp
);
2318 if (dtp
->u
.p
.input_complete
)
2321 if (dtp
->u
.p
.at_eol
)
2322 finish_separator (dtp
);
2323 if (dtp
->u
.p
.input_complete
)
2326 c
= next_char (dtp
);
2330 st_sprintf (nml_err_msg
, "Equal sign must follow namelist object name %s",
2335 if (nml_read_obj (dtp
, nl
, 0, pprev_nl
, nml_err_msg
, clow
, chigh
) == FAILURE
)
2345 /* Entry point for namelist input. Goes through input until namelist name
2346 is matched. Then cycles through nml_get_obj_data until the input is
2347 completed or there is an error. */
2350 namelist_read (st_parameter_dt
*dtp
)
2354 char nml_err_msg
[100];
2355 /* Pointer to the previously read object, in case attempt is made to read
2356 new object name. Should this fail, error message can give previous
2358 namelist_info
*prev_nl
= NULL
;
2360 dtp
->u
.p
.namelist_mode
= 1;
2361 dtp
->u
.p
.input_complete
= 0;
2363 dtp
->u
.p
.eof_jump
= &eof_jump
;
2364 if (setjmp (eof_jump
))
2366 dtp
->u
.p
.eof_jump
= NULL
;
2367 generate_error (&dtp
->common
, ERROR_END
, NULL
);
2371 /* Look for &namelist_name . Skip all characters, testing for $nmlname.
2372 Exit on success or EOF. If '?' or '=?' encountered in stdin, print
2373 node names or namelist on stdout. */
2376 switch (c
= next_char (dtp
))
2383 c
= next_char (dtp
);
2385 nml_query (dtp
, '=');
2387 unget_char (dtp
, c
);
2391 nml_query (dtp
, '?');
2397 /* Match the name of the namelist. */
2399 nml_match_name (dtp
, dtp
->namelist_name
, dtp
->namelist_name_len
);
2401 if (dtp
->u
.p
.nml_read_error
)
2404 /* Ready to read namelist objects. If there is an error in input
2405 from stdin, output the error message and continue. */
2407 while (!dtp
->u
.p
.input_complete
)
2409 if (nml_get_obj_data (dtp
, &prev_nl
, nml_err_msg
) == FAILURE
)
2413 if (dtp
->u
.p
.current_unit
->unit_number
!= options
.stdin_unit
)
2416 u
= find_unit (options
.stderr_unit
);
2417 st_printf ("%s\n", nml_err_msg
);
2427 dtp
->u
.p
.eof_jump
= NULL
;
2431 /* All namelist error calls return from here */
2435 dtp
->u
.p
.eof_jump
= NULL
;
2437 generate_error (&dtp
->common
, ERROR_READ_VALUE
, nml_err_msg
);