PR tree-optimization/17549
[official-gcc.git] / libgfortran / io / list_read.c
blobeecc11491e365c36d59427e9442edf4c7bc7ff70
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)
9 any later version.
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
18 executable.)
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. */
31 #include "config.h"
32 #include <string.h>
33 #include <ctype.h>
34 #include "libgfortran.h"
35 #include "io.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
41 parsing. */
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;
57 static bt saved_type;
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'
71 /* This macro assumes that we're operating on a variable. */
73 #define is_separator(c) (c == '/' || c == ',' || c == '\n' || c == ' ' \
74 || c == '\t')
76 /* Maximum repeat count. Less than ten times the maximum signed int32. */
78 #define MAX_REPEAT 200000000
81 /* Save a character to a string buffer, enlarging it as necessary. */
83 static void
84 push_char (char c)
86 char *new;
88 if (saved_string == NULL)
90 saved_string = scratch;
91 memset (saved_string,0,SCRATCH_SIZE);
92 saved_length = SCRATCH_SIZE;
93 saved_used = 0;
96 if (saved_used >= saved_length)
98 saved_length = 2 * saved_length;
99 new = get_mem (2 * saved_length);
101 memset (new,0,2 * saved_length);
103 memcpy (new, saved_string, saved_used);
104 if (saved_string != scratch)
105 free_mem (saved_string);
107 saved_string = new;
110 saved_string[saved_used++] = c;
114 /* Free the input buffer if necessary. */
116 static void
117 free_saved (void)
119 if (saved_string == NULL)
120 return;
122 if (saved_string != scratch)
123 free_mem (saved_string);
125 saved_string = NULL;
129 static char
130 next_char (void)
132 int length;
133 char c, *p;
135 if (last_char != '\0')
137 at_eol = 0;
138 c = last_char;
139 last_char = '\0';
140 goto done;
143 length = 1;
145 p = salloc_r (current_unit->s, &length);
146 if (p == NULL)
148 generate_error (ERROR_OS, NULL);
149 return '\0';
152 if (length == 0)
154 /* For internal files return a newline instead of signalling EOF. */
155 /* ??? This isn't quite right, but we don't handle internal files
156 with multiple records. */
157 if (is_internal_unit ())
158 c = '\n';
159 else
160 longjmp (g.eof_jump, 1);
162 else
163 c = *p;
165 done:
166 at_eol = (c == '\n');
167 return c;
171 /* Push a character back onto the input. */
173 static void
174 unget_char (char c)
176 last_char = c;
180 /* Skip over spaces in the input. Returns the nonspace character that
181 terminated the eating and also places it back on the input. */
183 static char
184 eat_spaces (void)
186 char c;
190 c = next_char ();
192 while (c == ' ' || c == '\t');
194 unget_char (c);
195 return c;
199 /* Skip over a separator. Technically, we don't always eat the whole
200 separator. This is because if we've processed the last input item,
201 then a separator is unnecessary. Plus the fact that operating
202 systems usually deliver console input on a line basis.
204 The upshot is that if we see a newline as part of reading a
205 separator, we stop reading. If there are more input items, we
206 continue reading the separator with finish_separator() which takes
207 care of the fact that we may or may not have seen a comma as part
208 of the separator. */
210 static void
211 eat_separator (void)
213 char c;
215 eat_spaces ();
216 comma_flag = 0;
218 c = next_char ();
219 switch (c)
221 case ',':
222 comma_flag = 1;
223 eat_spaces ();
224 break;
226 case '/':
227 input_complete = 1;
228 next_record (0);
229 at_eol = 1;
230 break;
232 case '\n':
233 break;
235 case '!':
236 if (namelist_mode)
237 { /* Eat a namelist comment. */
239 c = next_char ();
240 while (c != '\n');
242 break;
245 /* Fall Through... */
247 default:
248 unget_char (c);
249 break;
254 /* Finish processing a separator that was interrupted by a newline.
255 If we're here, then another data item is present, so we finish what
256 we started on the previous line. */
258 static void
259 finish_separator (void)
261 char c;
263 restart:
264 eat_spaces ();
266 c = next_char ();
267 switch (c)
269 case ',':
270 if (comma_flag)
271 unget_char (c);
272 else
274 c = eat_spaces ();
275 if (c == '\n')
276 goto restart;
279 break;
281 case '/':
282 input_complete = 1;
283 next_record (0);
284 break;
286 case '\n':
287 goto restart;
289 case '!':
290 if (namelist_mode)
293 c = next_char ();
294 while (c != '\n');
296 goto restart;
299 default:
300 unget_char (c);
301 break;
306 /* Convert an unsigned string to an integer. The length value is -1
307 if we are working on a repeat count. Returns nonzero if we have a
308 range problem. As a side effect, frees the saved_string. */
310 static int
311 convert_integer (int length, int negative)
313 char c, *buffer, message[100];
314 int m;
315 int64_t v, max, max10;
317 buffer = saved_string;
318 v = 0;
320 max = (length == -1) ? MAX_REPEAT : max_value (length, 1);
321 max10 = max / 10;
323 for (;;)
325 c = *buffer++;
326 if (c == '\0')
327 break;
328 c -= '0';
330 if (v > max10)
331 goto overflow;
332 v = 10 * v;
334 if (v > max - c)
335 goto overflow;
336 v += c;
339 m = 0;
341 if (length != -1)
343 if (negative)
344 v = -v;
345 set_integer (value, v, length);
347 else
349 repeat_count = v;
351 if (repeat_count == 0)
353 st_sprintf (message, "Zero repeat count in item %d of list input",
354 g.item_count);
356 generate_error (ERROR_READ_VALUE, message);
357 m = 1;
361 free_saved ();
362 return m;
364 overflow:
365 if (length == -1)
366 st_sprintf (message, "Repeat count overflow in item %d of list input",
367 g.item_count);
368 else
369 st_sprintf (message, "Integer overflow while reading item %d",
370 g.item_count);
372 free_saved ();
373 generate_error (ERROR_READ_VALUE, message);
375 return 1;
379 /* Parse a repeat count for logical and complex values which cannot
380 begin with a digit. Returns nonzero if we are done, zero if we
381 should continue on. */
383 static int
384 parse_repeat (void)
386 char c, message[100];
387 int repeat;
389 c = next_char ();
390 switch (c)
392 CASE_DIGITS:
393 repeat = c - '0';
394 break;
396 CASE_SEPARATORS:
397 unget_char (c);
398 eat_separator ();
399 return 1;
401 default:
402 unget_char (c);
403 return 0;
406 for (;;)
408 c = next_char ();
409 switch (c)
411 CASE_DIGITS:
412 repeat = 10 * repeat + c - '0';
414 if (repeat > MAX_REPEAT)
416 st_sprintf (message,
417 "Repeat count overflow in item %d of list input",
418 g.item_count);
420 generate_error (ERROR_READ_VALUE, message);
421 return 1;
424 break;
426 case '*':
427 if (repeat == 0)
429 st_sprintf (message,
430 "Zero repeat count in item %d of list input",
431 g.item_count);
433 generate_error (ERROR_READ_VALUE, message);
434 return 1;
437 goto done;
439 default:
440 goto bad_repeat;
444 done:
445 repeat_count = repeat;
446 return 0;
448 bad_repeat:
449 st_sprintf (message, "Bad repeat count in item %d of list input",
450 g.item_count);
452 generate_error (ERROR_READ_VALUE, message);
453 return 1;
457 /* Read a logical character on the input. */
459 static void
460 read_logical (int length)
462 char c, message[100];
463 int v;
465 if (parse_repeat ())
466 return;
468 c = next_char ();
469 switch (c)
471 case 't':
472 case 'T':
473 v = 1;
474 break;
475 case 'f':
476 case 'F':
477 v = 0;
478 break;
480 case '.':
481 c = next_char ();
482 switch (c)
484 case 't':
485 case 'T':
486 v = 1;
487 break;
488 case 'f':
489 case 'F':
490 v = 0;
491 break;
492 default:
493 goto bad_logical;
496 break;
498 CASE_SEPARATORS:
499 unget_char (c);
500 eat_separator ();
501 return; /* Null value. */
503 default:
504 goto bad_logical;
507 saved_type = BT_LOGICAL;
508 saved_length = length;
510 /* Eat trailing garbage. */
513 c = next_char ();
515 while (!is_separator (c));
517 unget_char (c);
518 eat_separator ();
519 free_saved ();
520 set_integer ((int *) value, v, length);
522 return;
524 bad_logical:
525 st_sprintf (message, "Bad logical value while reading item %d",
526 g.item_count);
528 generate_error (ERROR_READ_VALUE, message);
532 /* Reading integers is tricky because we can actually be reading a
533 repeat count. We have to store the characters in a buffer because
534 we could be reading an integer that is larger than the default int
535 used for repeat counts. */
537 static void
538 read_integer (int length)
540 char c, message[100];
541 int negative;
543 negative = 0;
545 c = next_char ();
546 switch (c)
548 case '-':
549 negative = 1;
550 /* Fall through... */
552 case '+':
553 c = next_char ();
554 goto get_integer;
556 CASE_SEPARATORS: /* Single null. */
557 unget_char (c);
558 eat_separator ();
559 return;
561 CASE_DIGITS:
562 push_char (c);
563 break;
565 default:
566 goto bad_integer;
569 /* Take care of what may be a repeat count. */
571 for (;;)
573 c = next_char ();
574 switch (c)
576 CASE_DIGITS:
577 push_char (c);
578 break;
580 case '*':
581 push_char ('\0');
582 goto repeat;
584 CASE_SEPARATORS: /* Not a repeat count. */
585 goto done;
587 default:
588 goto bad_integer;
592 repeat:
593 if (convert_integer (-1, 0))
594 return;
596 /* Get the real integer. */
598 c = next_char ();
599 switch (c)
601 CASE_DIGITS:
602 break;
604 CASE_SEPARATORS:
605 unget_char (c);
606 eat_separator ();
607 return;
609 case '-':
610 negative = 1;
611 /* Fall through... */
613 case '+':
614 c = next_char ();
615 break;
618 get_integer:
619 if (!isdigit (c))
620 goto bad_integer;
621 push_char (c);
623 for (;;)
625 c = next_char ();
626 switch (c)
628 CASE_DIGITS:
629 push_char (c);
630 break;
632 CASE_SEPARATORS:
633 goto done;
635 default:
636 goto bad_integer;
640 bad_integer:
641 free_saved ();
643 st_sprintf (message, "Bad integer for item %d in list input", g.item_count);
644 generate_error (ERROR_READ_VALUE, message);
646 return;
648 done:
649 unget_char (c);
650 eat_separator ();
652 push_char ('\0');
653 if (convert_integer (length, negative))
655 free_saved ();
656 return;
659 free_saved ();
660 saved_type = BT_INTEGER;
664 /* Read a character variable. */
666 static void
667 read_character (int length)
669 char c, quote, message[100];
671 quote = ' '; /* Space means no quote character. */
673 c = next_char ();
674 switch (c)
676 CASE_DIGITS:
677 push_char (c);
678 break;
680 CASE_SEPARATORS:
681 unget_char (c); /* NULL value. */
682 eat_separator ();
683 return;
685 case '"':
686 case '\'':
687 quote = c;
688 goto get_string;
690 default:
691 push_char (c);
692 goto get_string;
695 /* Deal with a possible repeat count. */
697 for (;;)
699 c = next_char ();
700 switch (c)
702 CASE_DIGITS:
703 push_char (c);
704 break;
706 CASE_SEPARATORS:
707 unget_char (c);
708 goto done; /* String was only digits! */
710 case '*':
711 push_char ('\0');
712 goto got_repeat;
714 default:
715 push_char (c);
716 goto get_string; /* Not a repeat count after all. */
720 got_repeat:
721 if (convert_integer (-1, 0))
722 return;
724 /* Now get the real string. */
726 c = next_char ();
727 switch (c)
729 CASE_SEPARATORS:
730 unget_char (c); /* Repeated NULL values. */
731 eat_separator ();
732 return;
734 case '"':
735 case '\'':
736 quote = c;
737 break;
739 default:
740 push_char (c);
741 break;
744 get_string:
745 for (;;)
747 c = next_char ();
748 switch (c)
750 case '"':
751 case '\'':
752 if (c != quote)
754 push_char (c);
755 break;
758 /* See if we have a doubled quote character or the end of
759 the string. */
761 c = next_char ();
762 if (c == quote)
764 push_char (quote);
765 break;
768 unget_char (c);
769 goto done;
771 CASE_SEPARATORS:
772 if (quote == ' ')
774 unget_char (c);
775 goto done;
778 if (c != '\n')
779 push_char (c);
780 break;
782 default:
783 push_char (c);
784 break;
788 /* At this point, we have to have a separator, or else the string is
789 invalid. */
790 done:
791 c = next_char ();
792 if (is_separator (c))
794 unget_char (c);
795 eat_separator ();
796 saved_type = BT_CHARACTER;
798 else
800 free_saved ();
801 st_sprintf (message, "Invalid string input in item %d", g.item_count);
802 generate_error (ERROR_READ_VALUE, message);
807 /* Parse a component of a complex constant or a real number that we
808 are sure is already there. This is a straight real number parser. */
810 static int
811 parse_real (void *buffer, int length)
813 char c, message[100];
814 int m, seen_dp;
816 c = next_char ();
817 if (c == '-' || c == '+')
819 push_char (c);
820 c = next_char ();
823 if (!isdigit (c) && c != '.')
824 goto bad;
826 push_char (c);
828 seen_dp = (c == '.') ? 1 : 0;
830 for (;;)
832 c = next_char ();
833 switch (c)
835 CASE_DIGITS:
836 push_char (c);
837 break;
839 case '.':
840 if (seen_dp)
841 goto bad;
843 seen_dp = 1;
844 push_char (c);
845 break;
847 case 'e':
848 case 'E':
849 case 'd':
850 case 'D':
851 push_char ('e');
852 goto exp1;
854 case '-':
855 case '+':
856 push_char ('e');
857 push_char (c);
858 c = next_char ();
859 goto exp2;
861 CASE_SEPARATORS:
862 unget_char (c);
863 goto done;
865 default:
866 goto done;
870 exp1:
871 c = next_char ();
872 if (c != '-' && c != '+')
873 push_char ('+');
874 else
876 push_char (c);
877 c = next_char ();
880 exp2:
881 if (!isdigit (c))
882 goto bad;
883 push_char (c);
885 for (;;)
887 c = next_char ();
888 switch (c)
890 CASE_DIGITS:
891 push_char (c);
892 break;
894 CASE_SEPARATORS:
895 unget_char (c);
896 goto done;
898 default:
899 goto done;
903 done:
904 unget_char (c);
905 push_char ('\0');
907 m = convert_real (buffer, saved_string, length);
908 free_saved ();
910 return m;
912 bad:
913 free_saved ();
914 st_sprintf (message, "Bad floating point number for item %d", g.item_count);
915 generate_error (ERROR_READ_VALUE, message);
917 return 1;
921 /* Reading a complex number is straightforward because we can tell
922 what it is right away. */
924 static void
925 read_complex (int length)
927 char message[100];
928 char c;
930 if (parse_repeat ())
931 return;
933 c = next_char ();
934 switch (c)
936 case '(':
937 break;
939 CASE_SEPARATORS:
940 unget_char (c);
941 eat_separator ();
942 return;
944 default:
945 goto bad_complex;
948 eat_spaces ();
949 if (parse_real (value, length))
950 return;
952 eat_spaces ();
953 if (next_char () != ',')
954 goto bad_complex;
956 eat_spaces ();
957 if (parse_real (value + length, length))
958 return;
960 eat_spaces ();
961 if (next_char () != ')')
962 goto bad_complex;
964 c = next_char ();
965 if (!is_separator (c))
966 goto bad_complex;
968 unget_char (c);
969 eat_separator ();
971 free_saved ();
972 saved_type = BT_COMPLEX;
973 return;
975 bad_complex:
976 st_sprintf (message, "Bad complex value in item %d of list input",
977 g.item_count);
979 generate_error (ERROR_READ_VALUE, message);
983 /* Parse a real number with a possible repeat count. */
985 static void
986 read_real (int length)
988 char c, message[100];
989 int seen_dp;
991 seen_dp = 0;
993 c = next_char ();
994 switch (c)
996 CASE_DIGITS:
997 push_char (c);
998 break;
1000 case '.':
1001 push_char (c);
1002 seen_dp = 1;
1003 break;
1005 case '+':
1006 case '-':
1007 goto got_sign;
1009 CASE_SEPARATORS:
1010 unget_char (c); /* Single null. */
1011 eat_separator ();
1012 return;
1014 default:
1015 goto bad_real;
1018 /* Get the digit string that might be a repeat count. */
1020 for (;;)
1022 c = next_char ();
1023 switch (c)
1025 CASE_DIGITS:
1026 push_char (c);
1027 break;
1029 case '.':
1030 if (seen_dp)
1031 goto bad_real;
1033 seen_dp = 1;
1034 push_char (c);
1035 goto real_loop;
1037 case 'E':
1038 case 'e':
1039 case 'D':
1040 case 'd':
1041 goto exp1;
1043 case '+':
1044 case '-':
1045 push_char ('e');
1046 push_char (c);
1047 c = next_char ();
1048 goto exp2;
1050 case '*':
1051 push_char ('\0');
1052 goto got_repeat;
1054 CASE_SEPARATORS:
1055 if (c != '\n' && c != ',')
1056 unget_char (c); /* Real number that is just a digit-string. */
1057 goto done;
1059 default:
1060 goto bad_real;
1064 got_repeat:
1065 if (convert_integer (-1, 0))
1066 return;
1068 /* Now get the number itself. */
1070 c = next_char ();
1071 if (is_separator (c))
1072 { /* Repeated null value. */
1073 unget_char (c);
1074 eat_separator ();
1075 return;
1078 if (c != '-' && c != '+')
1079 push_char ('+');
1080 else
1082 got_sign:
1083 push_char (c);
1084 c = next_char ();
1087 if (!isdigit (c) && c != '.')
1088 goto bad_real;
1090 if (c == '.')
1092 if (seen_dp)
1093 goto bad_real;
1094 else
1095 seen_dp = 1;
1098 push_char (c);
1100 real_loop:
1101 for (;;)
1103 c = next_char ();
1104 switch (c)
1106 CASE_DIGITS:
1107 push_char (c);
1108 break;
1110 CASE_SEPARATORS:
1111 goto done;
1113 case '.':
1114 if (seen_dp)
1115 goto bad_real;
1117 seen_dp = 1;
1118 push_char (c);
1119 break;
1121 case 'E':
1122 case 'e':
1123 case 'D':
1124 case 'd':
1125 goto exp1;
1127 case '+':
1128 case '-':
1129 push_char ('e');
1130 push_char (c);
1131 c = next_char ();
1132 goto exp2;
1134 default:
1135 goto bad_real;
1139 exp1:
1140 push_char ('e');
1142 c = next_char ();
1143 if (c != '+' && c != '-')
1144 push_char ('+');
1145 else
1147 push_char (c);
1148 c = next_char ();
1151 exp2:
1152 if (!isdigit (c))
1153 goto bad_real;
1154 push_char (c);
1156 for (;;)
1158 c = next_char ();
1160 switch (c)
1162 CASE_DIGITS:
1163 push_char (c);
1164 break;
1166 CASE_SEPARATORS:
1167 unget_char (c);
1168 eat_separator ();
1169 goto done;
1171 default:
1172 goto bad_real;
1176 done:
1177 push_char ('\0');
1178 if (convert_real (value, saved_string, length))
1179 return;
1181 free_saved ();
1182 saved_type = BT_REAL;
1183 return;
1185 bad_real:
1186 st_sprintf (message, "Bad real number in item %d of list input",
1187 g.item_count);
1189 generate_error (ERROR_READ_VALUE, message);
1193 /* Check the current type against the saved type to make sure they are
1194 compatible. Returns nonzero if incompatible. */
1196 static int
1197 check_type (bt type, int len)
1199 char message[100];
1201 if (saved_type != BT_NULL && saved_type != type)
1203 st_sprintf (message, "Read type %s where %s was expected for item %d",
1204 type_name (saved_type), type_name (type), g.item_count);
1206 generate_error (ERROR_READ_VALUE, message);
1207 return 1;
1210 if (saved_type == BT_NULL || saved_type == BT_CHARACTER)
1211 return 0;
1213 if (saved_length != len)
1215 st_sprintf (message,
1216 "Read kind %d %s where kind %d is required for item %d",
1217 saved_length, type_name (saved_type), len, g.item_count);
1218 generate_error (ERROR_READ_VALUE, message);
1219 return 1;
1222 return 0;
1226 /* Top level data transfer subroutine for list reads. Because we have
1227 to deal with repeat counts, the data item is always saved after
1228 reading, usually in the value[] array. If a repeat count is
1229 greater than one, we copy the data item multiple times. */
1231 void
1232 list_formatted_read (bt type, void *p, int len)
1234 char c;
1235 int m;
1237 namelist_mode = 0;
1239 if (setjmp (g.eof_jump))
1241 generate_error (ERROR_END, NULL);
1242 return;
1245 if (g.first_item)
1247 g.first_item = 0;
1248 input_complete = 0;
1249 repeat_count = 1;
1250 at_eol = 0;
1252 c = eat_spaces ();
1253 if (is_separator (c))
1254 { /* Found a null value. */
1255 eat_separator ();
1256 repeat_count = 0;
1257 if (at_eol)
1258 finish_separator ();
1259 else
1260 return;
1264 else
1266 if (input_complete)
1267 return;
1269 if (repeat_count > 0)
1271 if (check_type (type, len))
1272 return;
1273 goto set_value;
1276 if (at_eol)
1277 finish_separator ();
1278 else
1280 eat_spaces ();
1281 /* trailing spaces prior to end of line */
1282 if (at_eol)
1283 finish_separator ();
1286 saved_type = BT_NULL;
1287 repeat_count = 1;
1290 switch (type)
1292 case BT_INTEGER:
1293 read_integer (len);
1294 break;
1295 case BT_LOGICAL:
1296 read_logical (len);
1297 break;
1298 case BT_CHARACTER:
1299 read_character (len);
1300 break;
1301 case BT_REAL:
1302 read_real (len);
1303 break;
1304 case BT_COMPLEX:
1305 read_complex (len);
1306 break;
1307 default:
1308 internal_error ("Bad type for list read");
1311 if (saved_type != BT_CHARACTER && saved_type != BT_NULL)
1312 saved_length = len;
1314 if (ioparm.library_return != LIBRARY_OK)
1315 return;
1317 set_value:
1318 switch (saved_type)
1320 case BT_COMPLEX:
1321 len = 2 * len;
1322 /* Fall through. */
1324 case BT_INTEGER:
1325 case BT_REAL:
1326 case BT_LOGICAL:
1327 memcpy (p, value, len);
1328 break;
1330 case BT_CHARACTER:
1331 if (saved_string)
1333 m = (len < saved_used) ? len : saved_used;
1334 memcpy (p, saved_string, m);
1336 else
1337 /* Just delimiters encountered, nothing to copy but SPACE. */
1338 m = 0;
1340 if (m < len)
1341 memset (((char *) p) + m, ' ', len - m);
1342 break;
1344 case BT_NULL:
1345 break;
1348 if (--repeat_count <= 0)
1349 free_saved ();
1352 void
1353 init_at_eol(void)
1355 at_eol = 0;
1358 /* Finish a list read. */
1360 void
1361 finish_list_read (void)
1363 char c;
1365 free_saved ();
1367 if (at_eol)
1369 at_eol = 0;
1370 return;
1375 c = next_char ();
1377 while (c != '\n');
1380 static namelist_info *
1381 find_nml_node (char * var_name)
1383 namelist_info * t = ionml;
1384 while (t != NULL)
1386 if (strcmp (var_name,t->var_name) == 0)
1388 t->value_acquired = 1;
1389 return t;
1391 t = t->next;
1393 return NULL;
1396 static void
1397 match_namelist_name (char *name, int len)
1399 int name_len;
1400 char c;
1401 char * namelist_name = name;
1403 name_len = 0;
1404 /* Match the name of the namelist. */
1406 if (tolower (next_char ()) != tolower (namelist_name[name_len++]))
1408 wrong_name:
1409 generate_error (ERROR_READ_VALUE, "Wrong namelist name found");
1410 return;
1413 while (name_len < len)
1415 c = next_char ();
1416 if (tolower (c) != tolower (namelist_name[name_len++]))
1417 goto wrong_name;
1422 /********************************************************************
1423 Namelist reads
1424 ********************************************************************/
1426 /* Process a namelist read. This subroutine initializes things,
1427 positions to the first element and
1428 FIXME: was this comment ever complete? */
1430 void
1431 namelist_read (void)
1433 char c;
1434 int name_matched, next_name ;
1435 namelist_info * nl;
1436 int len, m;
1437 void * p;
1439 namelist_mode = 1;
1441 if (setjmp (g.eof_jump))
1443 generate_error (ERROR_END, NULL);
1444 return;
1447 restart:
1448 c = next_char ();
1449 switch (c)
1451 case ' ':
1452 goto restart;
1453 case '!':
1455 c = next_char ();
1456 while (c != '\n');
1458 goto restart;
1460 case '&':
1461 break;
1463 default:
1464 generate_error (ERROR_READ_VALUE, "Invalid character in namelist");
1465 return;
1468 /* Match the name of the namelist. */
1469 match_namelist_name(ioparm.namelist_name, ioparm.namelist_name_len);
1471 /* Ready to read namelist elements. */
1472 while (!input_complete)
1474 c = next_char ();
1475 switch (c)
1477 case '/':
1478 input_complete = 1;
1479 next_record (0);
1480 break;
1481 case '&':
1482 match_namelist_name("end",3);
1483 return;
1484 case '\\':
1485 return;
1486 case ' ':
1487 case '\n':
1488 case '\t':
1489 break;
1490 case ',':
1491 next_name = 1;
1492 break;
1494 case '=':
1495 name_matched = 1;
1496 nl = find_nml_node (saved_string);
1497 if (nl == NULL)
1498 internal_error ("Can not match a namelist variable");
1499 free_saved();
1501 len = nl->len;
1502 p = nl->mem_pos;
1504 /* skip any blanks or tabs after the = */
1505 eat_spaces ();
1507 switch (nl->type)
1509 case BT_INTEGER:
1510 read_integer (len);
1511 break;
1512 case BT_LOGICAL:
1513 read_logical (len);
1514 break;
1515 case BT_CHARACTER:
1516 read_character (len);
1517 break;
1518 case BT_REAL:
1519 read_real (len);
1520 break;
1521 case BT_COMPLEX:
1522 read_complex (len);
1523 break;
1524 default:
1525 internal_error ("Bad type for namelist read");
1528 switch (saved_type)
1530 case BT_COMPLEX:
1531 len = 2 * len;
1532 /* Fall through... */
1534 case BT_INTEGER:
1535 case BT_REAL:
1536 case BT_LOGICAL:
1537 memcpy (p, value, len);
1538 break;
1540 case BT_CHARACTER:
1541 m = (len < saved_used) ? len : saved_used;
1542 memcpy (p, saved_string, m);
1544 if (m < len)
1545 memset (((char *) p) + m, ' ', len - m);
1546 break;
1548 case BT_NULL:
1549 break;
1552 break;
1554 default :
1555 push_char(tolower(c));
1556 break;