1 /* Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
4 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6 Libgfortran is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 In addition to the permissions in the GNU General Public License, the
12 Free Software Foundation gives you unlimited permission to link the
13 compiled version of this file into combinations with other programs,
14 and to distribute those combinations without any restriction coming
15 from the use of this file. (The General Public License restrictions
16 do apply in other respects; for example, they cover modification of
17 the file, and distribution when not linked into a combine
20 Libgfortran is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with Libgfortran; see the file COPYING. If not, write to
27 the Free Software Foundation, 59 Temple Place - Suite 330,
28 Boston, MA 02111-1307, USA. */
34 #include "libgfortran.h"
38 /* List directed input. Several parsing subroutines are practically
39 reimplemented from formatted input, the reason being that there are
40 all kinds of small differences between formatted and list directed
44 /* Subroutines for reading characters from the input. Because a
45 repeat count is ambiguous with an integer, we have to read the
46 whole digit string before seeing if there is a '*' which signals
47 the repeat count. Since we can have a lot of potential leading
48 zeros, we have to be able to back up by arbitrary amount. Because
49 the input might not be seekable, we have to buffer the data
50 ourselves. Data is buffered in scratch[] until it becomes too
51 large, after which we start allocating memory on the heap. */
53 static int repeat_count, saved_length, saved_used, input_complete, at_eol;
54 static int comma_flag, namelist_mode;
56 static char last_char, *saved_string;
61 /* Storage area for values except for strings. Must be large enough
62 to hold a complex value (two reals) of the largest kind. */
64 static char value[20];
66 #define CASE_DIGITS case '0': case '1': case '2': case '3': case '4': \
67 case '5': case '6': case '7': case '8': case '9'
69 #define CASE_SEPARATORS case ' ': case ',': case '/': case '\n': case '\t': \
72 /* This macro assumes that we're operating on a variable. */
74 #define is_separator(c) (c == '/' || c == ',' || c == '\n' || c == ' ' \
75 || c == '\t' || c == '\r')
77 /* Maximum repeat count. Less than ten times the maximum signed int32. */
79 #define MAX_REPEAT 200000000
82 /* Save a character to a string buffer, enlarging it as necessary. */
89 if (saved_string == NULL)
91 saved_string = scratch;
92 memset (saved_string,0,SCRATCH_SIZE);
93 saved_length = SCRATCH_SIZE;
97 if (saved_used >= saved_length)
99 saved_length = 2 * saved_length;
100 new = get_mem (2 * saved_length);
102 memset (new,0,2 * saved_length);
104 memcpy (new, saved_string, saved_used);
105 if (saved_string != scratch)
106 free_mem (saved_string);
111 saved_string[saved_used++] = c;
115 /* Free the input buffer if necessary. */
120 if (saved_string == NULL)
123 if (saved_string != scratch)
124 free_mem (saved_string);
136 if (last_char != '\0')
146 p = salloc_r (current_unit->s, &length);
149 generate_error (ERROR_OS, NULL);
155 /* For internal files return a newline instead of signalling EOF. */
156 /* ??? This isn't quite right, but we don't handle internal files
157 with multiple records. */
158 if (is_internal_unit ())
161 longjmp (g.eof_jump, 1);
167 at_eol = (c == '\n' || c == '\r');
172 /* Push a character back onto the input. */
181 /* Skip over spaces in the input. Returns the nonspace character that
182 terminated the eating and also places it back on the input. */
193 while (c == ' ' || c == '\t');
200 /* Skip over a separator. Technically, we don't always eat the whole
201 separator. This is because if we've processed the last input item,
202 then a separator is unnecessary. Plus the fact that operating
203 systems usually deliver console input on a line basis.
205 The upshot is that if we see a newline as part of reading a
206 separator, we stop reading. If there are more input items, we
207 continue reading the separator with finish_separator() which takes
208 care of the fact that we may or may not have seen a comma as part
239 { /* Eat a namelist comment. */
247 /* Fall Through... */
256 /* Finish processing a separator that was interrupted by a newline.
257 If we're here, then another data item is present, so we finish what
258 we started on the previous line. */
261 finish_separator (void)
309 /* Convert an unsigned string to an integer. The length value is -1
310 if we are working on a repeat count. Returns nonzero if we have a
311 range problem. As a side effect, frees the saved_string. */
314 convert_integer (int length, int negative)
316 char c, *buffer, message[100];
318 int64_t v, max, max10;
320 buffer = saved_string;
323 max = (length == -1) ? MAX_REPEAT : max_value (length, 1);
348 set_integer (value, v, length);
354 if (repeat_count == 0)
356 st_sprintf (message, "Zero repeat count in item %d of list input",
359 generate_error (ERROR_READ_VALUE, message);
369 st_sprintf (message, "Repeat count overflow in item %d of list input",
372 st_sprintf (message, "Integer overflow while reading item %d",
376 generate_error (ERROR_READ_VALUE, message);
382 /* Parse a repeat count for logical and complex values which cannot
383 begin with a digit. Returns nonzero if we are done, zero if we
384 should continue on. */
389 char c, message[100];
415 repeat = 10 * repeat + c - '0';
417 if (repeat > MAX_REPEAT)
420 "Repeat count overflow in item %d of list input",
423 generate_error (ERROR_READ_VALUE, message);
433 "Zero repeat count in item %d of list input",
436 generate_error (ERROR_READ_VALUE, message);
448 repeat_count = repeat;
452 st_sprintf (message, "Bad repeat count in item %d of list input",
455 generate_error (ERROR_READ_VALUE, message);
460 /* Read a logical character on the input. */
463 read_logical (int length)
465 char c, message[100];
504 return; /* Null value. */
510 saved_type = BT_LOGICAL;
511 saved_length = length;
513 /* Eat trailing garbage. */
518 while (!is_separator (c));
523 set_integer ((int *) value, v, length);
528 st_sprintf (message, "Bad logical value while reading item %d",
531 generate_error (ERROR_READ_VALUE, message);
535 /* Reading integers is tricky because we can actually be reading a
536 repeat count. We have to store the characters in a buffer because
537 we could be reading an integer that is larger than the default int
538 used for repeat counts. */
541 read_integer (int length)
543 char c, message[100];
553 /* Fall through... */
559 CASE_SEPARATORS: /* Single null. */
572 /* Take care of what may be a repeat count. */
587 CASE_SEPARATORS: /* Not a repeat count. */
596 if (convert_integer (-1, 0))
599 /* Get the real integer. */
614 /* Fall through... */
646 st_sprintf (message, "Bad integer for item %d in list input", g.item_count);
647 generate_error (ERROR_READ_VALUE, message);
656 if (convert_integer (length, negative))
663 saved_type = BT_INTEGER;
667 /* Read a character variable. */
670 read_character (int length)
672 char c, quote, message[100];
674 quote = ' '; /* Space means no quote character. */
684 unget_char (c); /* NULL value. */
698 /* Deal with a possible repeat count. */
711 goto done; /* String was only digits! */
719 goto get_string; /* Not a repeat count after all. */
724 if (convert_integer (-1, 0))
727 /* Now get the real string. */
733 unget_char (c); /* Repeated NULL values. */
761 /* See if we have a doubled quote character or the end of
791 /* At this point, we have to have a separator, or else the string is
795 if (is_separator (c))
799 saved_type = BT_CHARACTER;
804 st_sprintf (message, "Invalid string input in item %d", g.item_count);
805 generate_error (ERROR_READ_VALUE, message);
810 /* Parse a component of a complex constant or a real number that we
811 are sure is already there. This is a straight real number parser. */
814 parse_real (void *buffer, int length)
816 char c, message[100];
820 if (c == '-' || c == '+')
826 if (!isdigit (c) && c != '.')
831 seen_dp = (c == '.') ? 1 : 0;
875 if (c != '-' && c != '+')
910 m = convert_real (buffer, saved_string, length);
917 st_sprintf (message, "Bad floating point number for item %d", g.item_count);
918 generate_error (ERROR_READ_VALUE, message);
924 /* Reading a complex number is straightforward because we can tell
925 what it is right away. */
928 read_complex (int length)
952 if (parse_real (value, length))
956 if (next_char () != ',')
960 if (parse_real (value + length, length))
964 if (next_char () != ')')
968 if (!is_separator (c))
975 saved_type = BT_COMPLEX;
979 st_sprintf (message, "Bad complex value in item %d of list input",
982 generate_error (ERROR_READ_VALUE, message);
986 /* Parse a real number with a possible repeat count. */
989 read_real (int length)
991 char c, message[100];
1013 unget_char (c); /* Single null. */
1021 /* Get the digit string that might be a repeat count. */
1058 if (c != '\n' && c != ',' && c != '\r')
1068 if (convert_integer (-1, 0))
1071 /* Now get the number itself. */
1074 if (is_separator (c))
1075 { /* Repeated null value. */
1081 if (c != '-' && c != '+')
1090 if (!isdigit (c) && c != '.')
1146 if (c != '+' && c != '-')
1181 if (convert_real (value, saved_string, length))
1185 saved_type = BT_REAL;
1189 st_sprintf (message, "Bad real number in item %d of list input",
1192 generate_error (ERROR_READ_VALUE, message);
1196 /* Check the current type against the saved type to make sure they are
1197 compatible. Returns nonzero if incompatible. */
1200 check_type (bt type, int len)
1204 if (saved_type != BT_NULL && saved_type != type)
1206 st_sprintf (message, "Read type %s where %s was expected for item %d",
1207 type_name (saved_type), type_name (type), g.item_count);
1209 generate_error (ERROR_READ_VALUE, message);
1213 if (saved_type == BT_NULL || saved_type == BT_CHARACTER)
1216 if (saved_length != len)
1218 st_sprintf (message,
1219 "Read kind %d %s where kind %d is required for item %d",
1220 saved_length, type_name (saved_type), len, g.item_count);
1221 generate_error (ERROR_READ_VALUE, message);
1229 /* Top level data transfer subroutine for list reads. Because we have
1230 to deal with repeat counts, the data item is always saved after
1231 reading, usually in the value[] array. If a repeat count is
1232 greater than one, we copy the data item multiple times. */
1235 list_formatted_read (bt type, void *p, int len)
1242 if (setjmp (g.eof_jump))
1244 generate_error (ERROR_END, NULL);
1256 if (is_separator (c))
1257 { /* Found a null value. */
1261 finish_separator ();
1272 if (repeat_count > 0)
1274 if (check_type (type, len))
1280 finish_separator ();
1284 /* trailing spaces prior to end of line */
1286 finish_separator ();
1289 saved_type = BT_NULL;
1302 read_character (len);
1311 internal_error ("Bad type for list read");
1314 if (saved_type != BT_CHARACTER && saved_type != BT_NULL)
1317 if (ioparm.library_return != LIBRARY_OK)
1330 memcpy (p, value, len);
1336 m = (len < saved_used) ? len : saved_used;
1337 memcpy (p, saved_string, m);
1340 /* Just delimiters encountered, nothing to copy but SPACE. */
1344 memset (((char *) p) + m, ' ', len - m);
1351 if (--repeat_count <= 0)
1361 /* Finish a list read. */
1364 finish_list_read (void)
1383 static namelist_info *
1384 find_nml_node (char * var_name)
1386 namelist_info * t = ionml;
1389 if (strcmp (var_name,t->var_name) == 0)
1391 t->value_acquired = 1;
1400 match_namelist_name (char *name, int len)
1404 char * namelist_name = name;
1407 /* Match the name of the namelist. */
1409 if (tolower (next_char ()) != tolower (namelist_name[name_len++]))
1412 generate_error (ERROR_READ_VALUE, "Wrong namelist name found");
1416 while (name_len < len)
1419 if (tolower (c) != tolower (namelist_name[name_len++]))
1425 /********************************************************************
1427 ********************************************************************/
1429 /* Process a namelist read. This subroutine initializes things,
1430 positions to the first element and
1431 FIXME: was this comment ever complete? */
1434 namelist_read (void)
1437 int name_matched, next_name ;
1444 if (setjmp (g.eof_jump))
1446 generate_error (ERROR_END, NULL);
1467 generate_error (ERROR_READ_VALUE, "Invalid character in namelist");
1471 /* Match the name of the namelist. */
1472 match_namelist_name(ioparm.namelist_name, ioparm.namelist_name_len);
1474 /* Ready to read namelist elements. */
1475 while (!input_complete)
1485 match_namelist_name("end",3);
1500 nl = find_nml_node (saved_string);
1502 internal_error ("Can not match a namelist variable");
1508 /* skip any blanks or tabs after the = */
1520 read_character (len);
1529 internal_error ("Bad type for namelist read");
1536 /* Fall through... */
1541 memcpy (p, value, len);
1545 m = (len < saved_used) ? len : saved_used;
1546 memcpy (p, saved_string, m);
1549 memset (((char *) p) + m, ' ', len - m);
1559 push_char(tolower(c));