Merge from mainline
[official-gcc.git] / libgfortran / io / list_read.c
blob793f0e25d4134e24e2b81cda442598719c736fa9
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)
10 any later version.
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
19 executable.)
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. */
32 #include "config.h"
33 #include <string.h>
34 #include <ctype.h>
35 #include "libgfortran.h"
36 #include "io.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
42 parsing. */
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
51 ourselves. */
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': \
57 case '\r'
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. */
71 static void
72 push_char (st_parameter_dt *dtp, char c)
74 char *new;
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. */
106 static void
107 free_saved (st_parameter_dt *dtp)
109 if (dtp->u.p.saved_string == NULL)
110 return;
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;
120 static char
121 next_char (st_parameter_dt *dtp)
123 int length;
124 gfc_offset record;
125 char c, *p;
127 if (dtp->u.p.last_char != '\0')
129 dtp->u.p.at_eol = 0;
130 c = dtp->u.p.last_char;
131 dtp->u.p.last_char = '\0';
132 goto done;
135 length = 1;
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)
140 c = '\n';
141 record = next_array_record (dtp, dtp->u.p.current_unit->ls);
143 /* Check for "end-of-file" condition */
144 if (record == 0)
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;
153 goto done;
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. */
165 if (p == NULL)
167 generate_error (&dtp->common, ERROR_INTERNAL_UNIT, NULL);
168 return '\0';
171 dtp->u.p.current_unit->bytes_left--;
172 c = *p;
174 else
176 if (p == NULL)
177 longjmp (*dtp->u.p.eof_jump, 1);
178 if (length == 0)
179 c = '\n';
180 else
181 c = *p;
184 else
186 if (p == NULL)
188 generate_error (&dtp->common, ERROR_OS, NULL);
189 return '\0';
191 if (length == 0)
192 longjmp (*dtp->u.p.eof_jump, 1);
193 c = *p;
195 done:
196 dtp->u.p.at_eol = (c == '\n' || c == '\r');
197 return c;
201 /* Push a character back onto the input. */
203 static void
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. */
213 static char
214 eat_spaces (st_parameter_dt *dtp)
216 char c;
220 c = next_char (dtp);
222 while (c == ' ' || c == '\t');
224 unget_char (dtp, c);
225 return c;
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
238 of the separator. */
240 static void
241 eat_separator (st_parameter_dt *dtp)
243 char c, n;
245 eat_spaces (dtp);
246 dtp->u.p.comma_flag = 0;
248 c = next_char (dtp);
249 switch (c)
251 case ',':
252 dtp->u.p.comma_flag = 1;
253 eat_spaces (dtp);
254 break;
256 case '/':
257 dtp->u.p.input_complete = 1;
258 break;
260 case '\r':
261 n = next_char(dtp);
262 if (n == '\n')
263 dtp->u.p.at_eol = 1;
264 else
266 unget_char (dtp, n);
267 unget_char (dtp, c);
269 break;
271 case '\n':
272 dtp->u.p.at_eol = 1;
273 break;
275 case '!':
276 if (dtp->u.p.namelist_mode)
277 { /* Eat a namelist comment. */
279 c = next_char (dtp);
280 while (c != '\n');
282 break;
285 /* Fall Through... */
287 default:
288 unget_char (dtp, c);
289 break;
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. */
298 static void
299 finish_separator (st_parameter_dt *dtp)
301 char c;
303 restart:
304 eat_spaces (dtp);
306 c = next_char (dtp);
307 switch (c)
309 case ',':
310 if (dtp->u.p.comma_flag)
311 unget_char (dtp, c);
312 else
314 c = eat_spaces (dtp);
315 if (c == '\n' || c == '\r')
316 goto restart;
319 break;
321 case '/':
322 dtp->u.p.input_complete = 1;
323 if (!dtp->u.p.namelist_mode) next_record (dtp, 0);
324 break;
326 case '\n':
327 case '\r':
328 goto restart;
330 case '!':
331 if (dtp->u.p.namelist_mode)
334 c = next_char (dtp);
335 while (c != '\n');
337 goto restart;
340 default:
341 unget_char (dtp, c);
342 break;
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
348 than a bad value. */
350 static int
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;
356 unget_char (dtp, c);
357 return 1;
359 return 0;
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. */
366 static int
367 convert_integer (st_parameter_dt *dtp, int length, int negative)
369 char c, *buffer, message[100];
370 int m;
371 GFC_INTEGER_LARGEST v, max, max10;
373 buffer = dtp->u.p.saved_string;
374 v = 0;
376 max = (length == -1) ? MAX_REPEAT : max_value (length, 1);
377 max10 = max / 10;
379 for (;;)
381 c = *buffer++;
382 if (c == '\0')
383 break;
384 c -= '0';
386 if (v > max10)
387 goto overflow;
388 v = 10 * v;
390 if (v > max - c)
391 goto overflow;
392 v += c;
395 m = 0;
397 if (length != -1)
399 if (negative)
400 v = -v;
401 set_integer (dtp->u.p.value, v, length);
403 else
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);
413 m = 1;
417 free_saved (dtp);
418 return m;
420 overflow:
421 if (length == -1)
422 st_sprintf (message, "Repeat count overflow in item %d of list input",
423 dtp->u.p.item_count);
424 else
425 st_sprintf (message, "Integer overflow while reading item %d",
426 dtp->u.p.item_count);
428 free_saved (dtp);
429 generate_error (&dtp->common, ERROR_READ_VALUE, message);
431 return 1;
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. */
439 static int
440 parse_repeat (st_parameter_dt *dtp)
442 char c, message[100];
443 int repeat;
445 c = next_char (dtp);
446 switch (c)
448 CASE_DIGITS:
449 repeat = c - '0';
450 break;
452 CASE_SEPARATORS:
453 unget_char (dtp, c);
454 eat_separator (dtp);
455 return 1;
457 default:
458 unget_char (dtp, c);
459 return 0;
462 for (;;)
464 c = next_char (dtp);
465 switch (c)
467 CASE_DIGITS:
468 repeat = 10 * repeat + c - '0';
470 if (repeat > MAX_REPEAT)
472 st_sprintf (message,
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);
477 return 1;
480 break;
482 case '*':
483 if (repeat == 0)
485 st_sprintf (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);
490 return 1;
493 goto done;
495 default:
496 goto bad_repeat;
500 done:
501 dtp->u.p.repeat_count = repeat;
502 return 0;
504 bad_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);
509 return 1;
513 /* Read a logical character on the input. */
515 static void
516 read_logical (st_parameter_dt *dtp, int length)
518 char c, message[100];
519 int v;
521 if (parse_repeat (dtp))
522 return;
524 c = next_char (dtp);
525 switch (c)
527 case 't':
528 case 'T':
529 v = 1;
530 break;
531 case 'f':
532 case 'F':
533 v = 0;
534 break;
536 case '.':
537 c = next_char (dtp);
538 switch (c)
540 case 't':
541 case 'T':
542 v = 1;
543 break;
544 case 'f':
545 case 'F':
546 v = 0;
547 break;
548 default:
549 goto bad_logical;
552 break;
554 CASE_SEPARATORS:
555 unget_char (dtp, c);
556 eat_separator (dtp);
557 return; /* Null value. */
559 default:
560 goto bad_logical;
563 dtp->u.p.saved_type = BT_LOGICAL;
564 dtp->u.p.saved_length = length;
566 /* Eat trailing garbage. */
569 c = next_char (dtp);
571 while (!is_separator (c));
573 unget_char (dtp, c);
574 eat_separator (dtp);
575 free_saved (dtp);
576 set_integer ((int *) dtp->u.p.value, v, length);
578 return;
580 bad_logical:
582 if (nml_bad_return (dtp, c))
583 return;
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. */
597 static void
598 read_integer (st_parameter_dt *dtp, int length)
600 char c, message[100];
601 int negative;
603 negative = 0;
605 c = next_char (dtp);
606 switch (c)
608 case '-':
609 negative = 1;
610 /* Fall through... */
612 case '+':
613 c = next_char (dtp);
614 goto get_integer;
616 CASE_SEPARATORS: /* Single null. */
617 unget_char (dtp, c);
618 eat_separator (dtp);
619 return;
621 CASE_DIGITS:
622 push_char (dtp, c);
623 break;
625 default:
626 goto bad_integer;
629 /* Take care of what may be a repeat count. */
631 for (;;)
633 c = next_char (dtp);
634 switch (c)
636 CASE_DIGITS:
637 push_char (dtp, c);
638 break;
640 case '*':
641 push_char (dtp, '\0');
642 goto repeat;
644 CASE_SEPARATORS: /* Not a repeat count. */
645 goto done;
647 default:
648 goto bad_integer;
652 repeat:
653 if (convert_integer (dtp, -1, 0))
654 return;
656 /* Get the real integer. */
658 c = next_char (dtp);
659 switch (c)
661 CASE_DIGITS:
662 break;
664 CASE_SEPARATORS:
665 unget_char (dtp, c);
666 eat_separator (dtp);
667 return;
669 case '-':
670 negative = 1;
671 /* Fall through... */
673 case '+':
674 c = next_char (dtp);
675 break;
678 get_integer:
679 if (!isdigit (c))
680 goto bad_integer;
681 push_char (dtp, c);
683 for (;;)
685 c = next_char (dtp);
686 switch (c)
688 CASE_DIGITS:
689 push_char (dtp, c);
690 break;
692 CASE_SEPARATORS:
693 goto done;
695 default:
696 goto bad_integer;
700 bad_integer:
702 if (nml_bad_return (dtp, c))
703 return;
705 free_saved (dtp);
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);
711 return;
713 done:
714 unget_char (dtp, c);
715 eat_separator (dtp);
717 push_char (dtp, '\0');
718 if (convert_integer (dtp, length, negative))
720 free_saved (dtp);
721 return;
724 free_saved (dtp);
725 dtp->u.p.saved_type = BT_INTEGER;
729 /* Read a character variable. */
731 static void
732 read_character (st_parameter_dt *dtp, int length __attribute__ ((unused)))
734 char c, quote, message[100];
736 quote = ' '; /* Space means no quote character. */
738 c = next_char (dtp);
739 switch (c)
741 CASE_DIGITS:
742 push_char (dtp, c);
743 break;
745 CASE_SEPARATORS:
746 unget_char (dtp, c); /* NULL value. */
747 eat_separator (dtp);
748 return;
750 case '"':
751 case '\'':
752 quote = c;
753 goto get_string;
755 default:
756 if (dtp->u.p.namelist_mode)
758 unget_char (dtp,c);
759 return;
761 push_char (dtp, c);
762 goto get_string;
765 /* Deal with a possible repeat count. */
767 for (;;)
769 c = next_char (dtp);
770 switch (c)
772 CASE_DIGITS:
773 push_char (dtp, c);
774 break;
776 CASE_SEPARATORS:
777 unget_char (dtp, c);
778 goto done; /* String was only digits! */
780 case '*':
781 push_char (dtp, '\0');
782 goto got_repeat;
784 default:
785 push_char (dtp, c);
786 goto get_string; /* Not a repeat count after all. */
790 got_repeat:
791 if (convert_integer (dtp, -1, 0))
792 return;
794 /* Now get the real string. */
796 c = next_char (dtp);
797 switch (c)
799 CASE_SEPARATORS:
800 unget_char (dtp, c); /* Repeated NULL values. */
801 eat_separator (dtp);
802 return;
804 case '"':
805 case '\'':
806 quote = c;
807 break;
809 default:
810 push_char (dtp, c);
811 break;
814 get_string:
815 for (;;)
817 c = next_char (dtp);
818 switch (c)
820 case '"':
821 case '\'':
822 if (c != quote)
824 push_char (dtp, c);
825 break;
828 /* See if we have a doubled quote character or the end of
829 the string. */
831 c = next_char (dtp);
832 if (c == quote)
834 push_char (dtp, quote);
835 break;
838 unget_char (dtp, c);
839 goto done;
841 CASE_SEPARATORS:
842 if (quote == ' ')
844 unget_char (dtp, c);
845 goto done;
848 if (c != '\n' && c != '\r')
849 push_char (dtp, c);
850 break;
852 default:
853 push_char (dtp, c);
854 break;
858 /* At this point, we have to have a separator, or else the string is
859 invalid. */
860 done:
861 c = next_char (dtp);
862 if (is_separator (c))
864 unget_char (dtp, c);
865 eat_separator (dtp);
866 dtp->u.p.saved_type = BT_CHARACTER;
868 else
870 free_saved (dtp);
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. */
881 static int
882 parse_real (st_parameter_dt *dtp, void *buffer, int length)
884 char c, message[100];
885 int m, seen_dp;
887 c = next_char (dtp);
888 if (c == '-' || c == '+')
890 push_char (dtp, c);
891 c = next_char (dtp);
894 if (!isdigit (c) && c != '.')
895 goto bad;
897 push_char (dtp, c);
899 seen_dp = (c == '.') ? 1 : 0;
901 for (;;)
903 c = next_char (dtp);
904 switch (c)
906 CASE_DIGITS:
907 push_char (dtp, c);
908 break;
910 case '.':
911 if (seen_dp)
912 goto bad;
914 seen_dp = 1;
915 push_char (dtp, c);
916 break;
918 case 'e':
919 case 'E':
920 case 'd':
921 case 'D':
922 push_char (dtp, 'e');
923 goto exp1;
925 case '-':
926 case '+':
927 push_char (dtp, 'e');
928 push_char (dtp, c);
929 c = next_char (dtp);
930 goto exp2;
932 CASE_SEPARATORS:
933 unget_char (dtp, c);
934 goto done;
936 default:
937 goto done;
941 exp1:
942 c = next_char (dtp);
943 if (c != '-' && c != '+')
944 push_char (dtp, '+');
945 else
947 push_char (dtp, c);
948 c = next_char (dtp);
951 exp2:
952 if (!isdigit (c))
953 goto bad;
954 push_char (dtp, c);
956 for (;;)
958 c = next_char (dtp);
959 switch (c)
961 CASE_DIGITS:
962 push_char (dtp, c);
963 break;
965 CASE_SEPARATORS:
966 unget_char (dtp, c);
967 goto done;
969 default:
970 goto done;
974 done:
975 unget_char (dtp, c);
976 push_char (dtp, '\0');
978 m = convert_real (dtp, buffer, dtp->u.p.saved_string, length);
979 free_saved (dtp);
981 return m;
983 bad:
984 free_saved (dtp);
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);
989 return 1;
993 /* Reading a complex number is straightforward because we can tell
994 what it is right away. */
996 static void
997 read_complex (st_parameter_dt *dtp, int kind, size_t size)
999 char message[100];
1000 char c;
1002 if (parse_repeat (dtp))
1003 return;
1005 c = next_char (dtp);
1006 switch (c)
1008 case '(':
1009 break;
1011 CASE_SEPARATORS:
1012 unget_char (dtp, c);
1013 eat_separator (dtp);
1014 return;
1016 default:
1017 goto bad_complex;
1020 eat_spaces (dtp);
1021 if (parse_real (dtp, dtp->u.p.value, kind))
1022 return;
1024 eol_1:
1025 eat_spaces (dtp);
1026 c = next_char (dtp);
1027 if (c == '\n' || c== '\r')
1028 goto eol_1;
1029 else
1030 unget_char (dtp, c);
1032 if (next_char (dtp) != ',')
1033 goto bad_complex;
1035 eol_2:
1036 eat_spaces (dtp);
1037 c = next_char (dtp);
1038 if (c == '\n' || c== '\r')
1039 goto eol_2;
1040 else
1041 unget_char (dtp, c);
1043 if (parse_real (dtp, dtp->u.p.value + size / 2, kind))
1044 return;
1046 eat_spaces (dtp);
1047 if (next_char (dtp) != ')')
1048 goto bad_complex;
1050 c = next_char (dtp);
1051 if (!is_separator (c))
1052 goto bad_complex;
1054 unget_char (dtp, c);
1055 eat_separator (dtp);
1057 free_saved (dtp);
1058 dtp->u.p.saved_type = BT_COMPLEX;
1059 return;
1061 bad_complex:
1063 if (nml_bad_return (dtp, c))
1064 return;
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. */
1075 static void
1076 read_real (st_parameter_dt *dtp, int length)
1078 char c, message[100];
1079 int seen_dp;
1081 seen_dp = 0;
1083 c = next_char (dtp);
1084 switch (c)
1086 CASE_DIGITS:
1087 push_char (dtp, c);
1088 break;
1090 case '.':
1091 push_char (dtp, c);
1092 seen_dp = 1;
1093 break;
1095 case '+':
1096 case '-':
1097 goto got_sign;
1099 CASE_SEPARATORS:
1100 unget_char (dtp, c); /* Single null. */
1101 eat_separator (dtp);
1102 return;
1104 default:
1105 goto bad_real;
1108 /* Get the digit string that might be a repeat count. */
1110 for (;;)
1112 c = next_char (dtp);
1113 switch (c)
1115 CASE_DIGITS:
1116 push_char (dtp, c);
1117 break;
1119 case '.':
1120 if (seen_dp)
1121 goto bad_real;
1123 seen_dp = 1;
1124 push_char (dtp, c);
1125 goto real_loop;
1127 case 'E':
1128 case 'e':
1129 case 'D':
1130 case 'd':
1131 goto exp1;
1133 case '+':
1134 case '-':
1135 push_char (dtp, 'e');
1136 push_char (dtp, c);
1137 c = next_char (dtp);
1138 goto exp2;
1140 case '*':
1141 push_char (dtp, '\0');
1142 goto got_repeat;
1144 CASE_SEPARATORS:
1145 if (c != '\n' && c != ',' && c != '\r')
1146 unget_char (dtp, c);
1147 goto done;
1149 default:
1150 goto bad_real;
1154 got_repeat:
1155 if (convert_integer (dtp, -1, 0))
1156 return;
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);
1165 return;
1168 if (c != '-' && c != '+')
1169 push_char (dtp, '+');
1170 else
1172 got_sign:
1173 push_char (dtp, c);
1174 c = next_char (dtp);
1177 if (!isdigit (c) && c != '.')
1178 goto bad_real;
1180 if (c == '.')
1182 if (seen_dp)
1183 goto bad_real;
1184 else
1185 seen_dp = 1;
1188 push_char (dtp, c);
1190 real_loop:
1191 for (;;)
1193 c = next_char (dtp);
1194 switch (c)
1196 CASE_DIGITS:
1197 push_char (dtp, c);
1198 break;
1200 CASE_SEPARATORS:
1201 goto done;
1203 case '.':
1204 if (seen_dp)
1205 goto bad_real;
1207 seen_dp = 1;
1208 push_char (dtp, c);
1209 break;
1211 case 'E':
1212 case 'e':
1213 case 'D':
1214 case 'd':
1215 goto exp1;
1217 case '+':
1218 case '-':
1219 push_char (dtp, 'e');
1220 push_char (dtp, c);
1221 c = next_char (dtp);
1222 goto exp2;
1224 default:
1225 goto bad_real;
1229 exp1:
1230 push_char (dtp, 'e');
1232 c = next_char (dtp);
1233 if (c != '+' && c != '-')
1234 push_char (dtp, '+');
1235 else
1237 push_char (dtp, c);
1238 c = next_char (dtp);
1241 exp2:
1242 if (!isdigit (c))
1243 goto bad_real;
1244 push_char (dtp, c);
1246 for (;;)
1248 c = next_char (dtp);
1250 switch (c)
1252 CASE_DIGITS:
1253 push_char (dtp, c);
1254 break;
1256 CASE_SEPARATORS:
1257 goto done;
1259 default:
1260 goto bad_real;
1264 done:
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))
1269 return;
1271 free_saved (dtp);
1272 dtp->u.p.saved_type = BT_REAL;
1273 return;
1275 bad_real:
1277 if (nml_bad_return (dtp, c))
1278 return;
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. */
1290 static int
1291 check_type (st_parameter_dt *dtp, bt type, int len)
1293 char message[100];
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);
1302 return 1;
1305 if (dtp->u.p.saved_type == BT_NULL || dtp->u.p.saved_type == BT_CHARACTER)
1306 return 0;
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);
1315 return 1;
1318 return 0;
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. */
1327 static void
1328 list_formatted_read_scalar (st_parameter_dt *dtp, bt type, void *p, int kind,
1329 size_t size)
1331 char c;
1332 int m;
1333 jmp_buf eof_jump;
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);
1341 goto cleanup;
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;
1357 /* eat_separator sets this flag if the separator was a comma */
1358 if (dtp->u.p.comma_flag)
1359 goto cleanup;
1361 /* eat_separator sets this flag if the separator was a \n or \r */
1362 if (dtp->u.p.at_eol)
1363 finish_separator (dtp);
1364 else
1365 goto cleanup;
1369 else
1371 if (dtp->u.p.input_complete)
1372 goto cleanup;
1374 if (dtp->u.p.repeat_count > 0)
1376 if (check_type (dtp, type, kind))
1377 return;
1378 goto set_value;
1381 if (dtp->u.p.at_eol)
1382 finish_separator (dtp);
1383 else
1385 eat_spaces (dtp);
1386 /* trailing spaces prior to end of line */
1387 if (dtp->u.p.at_eol)
1388 finish_separator (dtp);
1391 dtp->u.p.saved_type = BT_NULL;
1392 dtp->u.p.repeat_count = 1;
1395 switch (type)
1397 case BT_INTEGER:
1398 read_integer (dtp, kind);
1399 break;
1400 case BT_LOGICAL:
1401 read_logical (dtp, kind);
1402 break;
1403 case BT_CHARACTER:
1404 read_character (dtp, kind);
1405 break;
1406 case BT_REAL:
1407 read_real (dtp, kind);
1408 break;
1409 case BT_COMPLEX:
1410 read_complex (dtp, kind, size);
1411 break;
1412 default:
1413 internal_error (&dtp->common, "Bad type for list read");
1416 if (dtp->u.p.saved_type != BT_CHARACTER && dtp->u.p.saved_type != BT_NULL)
1417 dtp->u.p.saved_length = size;
1419 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1420 goto cleanup;
1422 set_value:
1423 switch (dtp->u.p.saved_type)
1425 case BT_COMPLEX:
1426 case BT_INTEGER:
1427 case BT_REAL:
1428 case BT_LOGICAL:
1429 memcpy (p, dtp->u.p.value, size);
1430 break;
1432 case BT_CHARACTER:
1433 if (dtp->u.p.saved_string)
1435 m = ((int) size < dtp->u.p.saved_used)
1436 ? (int) size : dtp->u.p.saved_used;
1437 memcpy (p, dtp->u.p.saved_string, m);
1439 else
1440 /* Just delimiters encountered, nothing to copy but SPACE. */
1441 m = 0;
1443 if (m < (int) size)
1444 memset (((char *) p) + m, ' ', size - m);
1445 break;
1447 case BT_NULL:
1448 break;
1451 if (--dtp->u.p.repeat_count <= 0)
1452 free_saved (dtp);
1454 cleanup:
1455 dtp->u.p.eof_jump = NULL;
1459 void
1460 list_formatted_read (st_parameter_dt *dtp, bt type, void *p, int kind,
1461 size_t size, size_t nelems)
1463 size_t elem;
1464 char *tmp;
1466 tmp = (char *) p;
1468 /* Big loop over all the elements. */
1469 for (elem = 0; elem < nelems; elem++)
1471 dtp->u.p.item_count++;
1472 list_formatted_read_scalar (dtp, type, tmp + size*elem, kind, size);
1477 /* Finish a list read. */
1479 void
1480 finish_list_read (st_parameter_dt *dtp)
1482 char c;
1484 free_saved (dtp);
1486 if (dtp->u.p.at_eol)
1488 dtp->u.p.at_eol = 0;
1489 return;
1494 c = next_char (dtp);
1496 while (c != '\n');
1499 /* NAMELIST INPUT
1501 void namelist_read (st_parameter_dt *dtp)
1502 calls:
1503 static void nml_match_name (char *name, int len)
1504 static int nml_query (st_parameter_dt *dtp)
1505 static int nml_get_obj_data (st_parameter_dt *dtp,
1506 namelist_info **prev_nl, char *)
1507 calls:
1508 static void nml_untouch_nodes (st_parameter_dt *dtp)
1509 static namelist_info * find_nml_node (st_parameter_dt *dtp,
1510 char * var_name)
1511 static int nml_parse_qualifier(descriptor_dimension * ad,
1512 array_loop_spec * ls, int rank, char *)
1513 static void nml_touch_nodes (namelist_info * nl)
1514 static int nml_read_obj (namelist_info *nl, index_type offset,
1515 namelist_info **prev_nl, char *,
1516 index_type clow, index_type chigh)
1517 calls:
1518 -itself- */
1520 /* Inputs a rank-dimensional qualifier, which can contain
1521 singlets, doublets, triplets or ':' with the standard meanings. */
1523 static try
1524 nml_parse_qualifier (st_parameter_dt *dtp, descriptor_dimension *ad,
1525 array_loop_spec *ls, int rank, char *parse_err_msg)
1527 int dim;
1528 int indx;
1529 int neg;
1530 int null_flag;
1531 char c;
1533 /* The next character in the stream should be the '('. */
1535 c = next_char (dtp);
1537 /* Process the qualifier, by dimension and triplet. */
1539 for (dim=0; dim < rank; dim++ )
1541 for (indx=0; indx<3; indx++)
1543 free_saved (dtp);
1544 eat_spaces (dtp);
1545 neg = 0;
1547 /* Process a potential sign. */
1548 c = next_char (dtp);
1549 switch (c)
1551 case '-':
1552 neg = 1;
1553 break;
1555 case '+':
1556 break;
1558 default:
1559 unget_char (dtp, c);
1560 break;
1563 /* Process characters up to the next ':' , ',' or ')'. */
1564 for (;;)
1566 c = next_char (dtp);
1568 switch (c)
1570 case ':':
1571 break;
1573 case ',': case ')':
1574 if ((c==',' && dim == rank -1)
1575 || (c==')' && dim < rank -1))
1577 st_sprintf (parse_err_msg,
1578 "Bad number of index fields");
1579 goto err_ret;
1581 break;
1583 CASE_DIGITS:
1584 push_char (dtp, c);
1585 continue;
1587 case ' ': case '\t':
1588 eat_spaces (dtp);
1589 c = next_char (dtp);
1590 break;
1592 default:
1593 st_sprintf (parse_err_msg, "Bad character in index");
1594 goto err_ret;
1597 if ((c == ',' || c == ')') && indx == 0
1598 && dtp->u.p.saved_string == 0)
1600 st_sprintf (parse_err_msg, "Null index field");
1601 goto err_ret;
1604 if ((c == ':' && indx == 1 && dtp->u.p.saved_string == 0)
1605 || (indx == 2 && dtp->u.p.saved_string == 0))
1607 st_sprintf(parse_err_msg, "Bad index triplet");
1608 goto err_ret;
1611 /* If '( : ? )' or '( ? : )' break and flag read failure. */
1612 null_flag = 0;
1613 if ((c == ':' && indx == 0 && dtp->u.p.saved_string == 0)
1614 || (indx==1 && dtp->u.p.saved_string == 0))
1616 null_flag = 1;
1617 break;
1620 /* Now read the index. */
1621 if (convert_integer (dtp, sizeof(ssize_t), neg))
1623 st_sprintf (parse_err_msg, "Bad integer in index");
1624 goto err_ret;
1626 break;
1629 /* Feed the index values to the triplet arrays. */
1630 if (!null_flag)
1632 if (indx == 0)
1633 memcpy (&ls[dim].start, dtp->u.p.value, sizeof(ssize_t));
1634 if (indx == 1)
1635 memcpy (&ls[dim].end, dtp->u.p.value, sizeof(ssize_t));
1636 if (indx == 2)
1637 memcpy (&ls[dim].step, dtp->u.p.value, sizeof(ssize_t));
1640 /* Singlet or doublet indices. */
1641 if (c==',' || c==')')
1643 if (indx == 0)
1645 memcpy (&ls[dim].start, dtp->u.p.value, sizeof(ssize_t));
1646 ls[dim].end = ls[dim].start;
1648 break;
1652 /* Check the values of the triplet indices. */
1653 if ((ls[dim].start > (ssize_t)ad[dim].ubound)
1654 || (ls[dim].start < (ssize_t)ad[dim].lbound)
1655 || (ls[dim].end > (ssize_t)ad[dim].ubound)
1656 || (ls[dim].end < (ssize_t)ad[dim].lbound))
1658 st_sprintf (parse_err_msg, "Index %d out of range", dim + 1);
1659 goto err_ret;
1661 if (((ls[dim].end - ls[dim].start ) * ls[dim].step < 0)
1662 || (ls[dim].step == 0))
1664 st_sprintf (parse_err_msg, "Bad range in index %d", dim + 1);
1665 goto err_ret;
1668 /* Initialise the loop index counter. */
1669 ls[dim].idx = ls[dim].start;
1671 eat_spaces (dtp);
1672 return SUCCESS;
1674 err_ret:
1676 return FAILURE;
1679 static namelist_info *
1680 find_nml_node (st_parameter_dt *dtp, char * var_name)
1682 namelist_info * t = dtp->u.p.ionml;
1683 while (t != NULL)
1685 if (strcmp (var_name, t->var_name) == 0)
1687 t->touched = 1;
1688 return t;
1690 t = t->next;
1692 return NULL;
1695 /* Visits all the components of a derived type that have
1696 not explicitly been identified in the namelist input.
1697 touched is set and the loop specification initialised
1698 to default values */
1700 static void
1701 nml_touch_nodes (namelist_info * nl)
1703 index_type len = strlen (nl->var_name) + 1;
1704 int dim;
1705 char * ext_name = (char*)get_mem (len + 1);
1706 strcpy (ext_name, nl->var_name);
1707 strcat (ext_name, "%");
1708 for (nl = nl->next; nl; nl = nl->next)
1710 if (strncmp (nl->var_name, ext_name, len) == 0)
1712 nl->touched = 1;
1713 for (dim=0; dim < nl->var_rank; dim++)
1715 nl->ls[dim].step = 1;
1716 nl->ls[dim].end = nl->dim[dim].ubound;
1717 nl->ls[dim].start = nl->dim[dim].lbound;
1718 nl->ls[dim].idx = nl->ls[dim].start;
1721 else
1722 break;
1724 free_mem (ext_name);
1725 return;
1728 /* Resets touched for the entire list of nml_nodes, ready for a
1729 new object. */
1731 static void
1732 nml_untouch_nodes (st_parameter_dt *dtp)
1734 namelist_info * t;
1735 for (t = dtp->u.p.ionml; t; t = t->next)
1736 t->touched = 0;
1737 return;
1740 /* Attempts to input name to namelist name. Returns
1741 dtp->u.p.nml_read_error = 1 on no match. */
1743 static void
1744 nml_match_name (st_parameter_dt *dtp, const char *name, index_type len)
1746 index_type i;
1747 char c;
1748 dtp->u.p.nml_read_error = 0;
1749 for (i = 0; i < len; i++)
1751 c = next_char (dtp);
1752 if (tolower (c) != tolower (name[i]))
1754 dtp->u.p.nml_read_error = 1;
1755 break;
1760 /* If the namelist read is from stdin, output the current state of the
1761 namelist to stdout. This is used to implement the non-standard query
1762 features, ? and =?. If c == '=' the full namelist is printed. Otherwise
1763 the names alone are printed. */
1765 static void
1766 nml_query (st_parameter_dt *dtp, char c)
1768 gfc_unit * temp_unit;
1769 namelist_info * nl;
1770 index_type len;
1771 char * p;
1773 if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
1774 return;
1776 /* Store the current unit and transfer to stdout. */
1778 temp_unit = dtp->u.p.current_unit;
1779 dtp->u.p.current_unit = find_unit (options.stdout_unit);
1781 if (dtp->u.p.current_unit)
1783 dtp->u.p.mode = WRITING;
1784 next_record (dtp, 0);
1786 /* Write the namelist in its entirety. */
1788 if (c == '=')
1789 namelist_write (dtp);
1791 /* Or write the list of names. */
1793 else
1796 /* "&namelist_name\n" */
1798 len = dtp->namelist_name_len;
1799 #ifdef HAVE_CRLF
1800 p = write_block (dtp, len + 3);
1801 #else
1802 p = write_block (dtp, len + 2);
1803 #endif
1804 if (!p)
1805 goto query_return;
1806 memcpy (p, "&", 1);
1807 memcpy ((char*)(p + 1), dtp->namelist_name, len);
1808 #ifdef HAVE_CRLF
1809 memcpy ((char*)(p + len + 1), "\r\n", 2);
1810 #else
1811 memcpy ((char*)(p + len + 1), "\n", 1);
1812 #endif
1813 for (nl = dtp->u.p.ionml; nl; nl = nl->next)
1816 /* " var_name\n" */
1818 len = strlen (nl->var_name);
1819 #ifdef HAVE_CRLF
1820 p = write_block (dtp, len + 3);
1821 #else
1822 p = write_block (dtp, len + 2);
1823 #endif
1824 if (!p)
1825 goto query_return;
1826 memcpy (p, " ", 1);
1827 memcpy ((char*)(p + 1), nl->var_name, len);
1828 #ifdef HAVE_CRLF
1829 memcpy ((char*)(p + len + 1), "\r\n", 2);
1830 #else
1831 memcpy ((char*)(p + len + 1), "\n", 1);
1832 #endif
1835 /* "&end\n" */
1837 #ifdef HAVE_CRLF
1838 p = write_block (dtp, 6);
1839 #else
1840 p = write_block (dtp, 5);
1841 #endif
1842 if (!p)
1843 goto query_return;
1844 #ifdef HAVE_CRLF
1845 memcpy (p, "&end\r\n", 6);
1846 #else
1847 memcpy (p, "&end\n", 5);
1848 #endif
1851 /* Flush the stream to force immediate output. */
1853 flush (dtp->u.p.current_unit->s);
1854 unlock_unit (dtp->u.p.current_unit);
1857 query_return:
1859 /* Restore the current unit. */
1861 dtp->u.p.current_unit = temp_unit;
1862 dtp->u.p.mode = READING;
1863 return;
1866 /* Reads and stores the input for the namelist object nl. For an array,
1867 the function loops over the ranges defined by the loop specification.
1868 This default to all the data or to the specification from a qualifier.
1869 nml_read_obj recursively calls itself to read derived types. It visits
1870 all its own components but only reads data for those that were touched
1871 when the name was parsed. If a read error is encountered, an attempt is
1872 made to return to read a new object name because the standard allows too
1873 little data to be available. On the other hand, too much data is an
1874 error. */
1876 static try
1877 nml_read_obj (st_parameter_dt *dtp, namelist_info * nl, index_type offset,
1878 namelist_info **pprev_nl, char *nml_err_msg,
1879 index_type clow, index_type chigh)
1882 namelist_info * cmp;
1883 char * obj_name;
1884 int nml_carry;
1885 int len;
1886 int dim;
1887 index_type dlen;
1888 index_type m;
1889 index_type obj_name_len;
1890 void * pdata ;
1892 /* This object not touched in name parsing. */
1894 if (!nl->touched)
1895 return SUCCESS;
1897 dtp->u.p.repeat_count = 0;
1898 eat_spaces (dtp);
1900 len = nl->len;
1901 switch (nl->type)
1904 case GFC_DTYPE_INTEGER:
1905 case GFC_DTYPE_LOGICAL:
1906 dlen = len;
1907 break;
1909 case GFC_DTYPE_REAL:
1910 dlen = size_from_real_kind (len);
1911 break;
1913 case GFC_DTYPE_COMPLEX:
1914 dlen = size_from_complex_kind (len);
1915 break;
1917 case GFC_DTYPE_CHARACTER:
1918 dlen = chigh ? (chigh - clow + 1) : nl->string_length;
1919 break;
1921 default:
1922 dlen = 0;
1928 /* Update the pointer to the data, using the current index vector */
1930 pdata = (void*)(nl->mem_pos + offset);
1931 for (dim = 0; dim < nl->var_rank; dim++)
1932 pdata = (void*)(pdata + (nl->ls[dim].idx - nl->dim[dim].lbound) *
1933 nl->dim[dim].stride * nl->size);
1935 /* Reset the error flag and try to read next value, if
1936 dtp->u.p.repeat_count=0 */
1938 dtp->u.p.nml_read_error = 0;
1939 nml_carry = 0;
1940 if (--dtp->u.p.repeat_count <= 0)
1942 if (dtp->u.p.input_complete)
1943 return SUCCESS;
1944 if (dtp->u.p.at_eol)
1945 finish_separator (dtp);
1946 if (dtp->u.p.input_complete)
1947 return SUCCESS;
1949 /* GFC_TYPE_UNKNOWN through for nulls and is detected
1950 after the switch block. */
1952 dtp->u.p.saved_type = GFC_DTYPE_UNKNOWN;
1953 free_saved (dtp);
1955 switch (nl->type)
1957 case GFC_DTYPE_INTEGER:
1958 read_integer (dtp, len);
1959 break;
1961 case GFC_DTYPE_LOGICAL:
1962 read_logical (dtp, len);
1963 break;
1965 case GFC_DTYPE_CHARACTER:
1966 read_character (dtp, len);
1967 break;
1969 case GFC_DTYPE_REAL:
1970 read_real (dtp, len);
1971 break;
1973 case GFC_DTYPE_COMPLEX:
1974 read_complex (dtp, len, dlen);
1975 break;
1977 case GFC_DTYPE_DERIVED:
1978 obj_name_len = strlen (nl->var_name) + 1;
1979 obj_name = get_mem (obj_name_len+1);
1980 strcpy (obj_name, nl->var_name);
1981 strcat (obj_name, "%");
1983 /* Now loop over the components. Update the component pointer
1984 with the return value from nml_write_obj. This loop jumps
1985 past nested derived types by testing if the potential
1986 component name contains '%'. */
1988 for (cmp = nl->next;
1989 cmp &&
1990 !strncmp (cmp->var_name, obj_name, obj_name_len) &&
1991 !strchr (cmp->var_name + obj_name_len, '%');
1992 cmp = cmp->next)
1995 if (nml_read_obj (dtp, cmp, (index_type)(pdata - nl->mem_pos),
1996 pprev_nl, nml_err_msg, clow, chigh)
1997 == FAILURE)
1999 free_mem (obj_name);
2000 return FAILURE;
2003 if (dtp->u.p.input_complete)
2005 free_mem (obj_name);
2006 return SUCCESS;
2010 free_mem (obj_name);
2011 goto incr_idx;
2013 default:
2014 st_sprintf (nml_err_msg, "Bad type for namelist object %s",
2015 nl->var_name);
2016 internal_error (&dtp->common, nml_err_msg);
2017 goto nml_err_ret;
2021 /* The standard permits array data to stop short of the number of
2022 elements specified in the loop specification. In this case, we
2023 should be here with dtp->u.p.nml_read_error != 0. Control returns to
2024 nml_get_obj_data and an attempt is made to read object name. */
2026 *pprev_nl = nl;
2027 if (dtp->u.p.nml_read_error)
2028 return SUCCESS;
2030 if (dtp->u.p.saved_type == GFC_DTYPE_UNKNOWN)
2031 goto incr_idx;
2034 /* Note the switch from GFC_DTYPE_type to BT_type at this point.
2035 This comes about because the read functions return BT_types. */
2037 switch (dtp->u.p.saved_type)
2040 case BT_COMPLEX:
2041 case BT_REAL:
2042 case BT_INTEGER:
2043 case BT_LOGICAL:
2044 memcpy (pdata, dtp->u.p.value, dlen);
2045 break;
2047 case BT_CHARACTER:
2048 m = (dlen < dtp->u.p.saved_used) ? dlen : dtp->u.p.saved_used;
2049 pdata = (void*)( pdata + clow - 1 );
2050 memcpy (pdata, dtp->u.p.saved_string, m);
2051 if (m < dlen)
2052 memset ((void*)( pdata + m ), ' ', dlen - m);
2053 break;
2055 default:
2056 break;
2059 /* Break out of loop if scalar. */
2061 if (!nl->var_rank)
2062 break;
2064 /* Now increment the index vector. */
2066 incr_idx:
2068 nml_carry = 1;
2069 for (dim = 0; dim < nl->var_rank; dim++)
2071 nl->ls[dim].idx += nml_carry * nl->ls[dim].step;
2072 nml_carry = 0;
2073 if (((nl->ls[dim].step > 0) && (nl->ls[dim].idx > nl->ls[dim].end))
2075 ((nl->ls[dim].step < 0) && (nl->ls[dim].idx < nl->ls[dim].end)))
2077 nl->ls[dim].idx = nl->ls[dim].start;
2078 nml_carry = 1;
2081 } while (!nml_carry);
2083 if (dtp->u.p.repeat_count > 1)
2085 st_sprintf (nml_err_msg, "Repeat count too large for namelist object %s" ,
2086 nl->var_name );
2087 goto nml_err_ret;
2089 return SUCCESS;
2091 nml_err_ret:
2093 return FAILURE;
2096 /* Parses the object name, including array and substring qualifiers. It
2097 iterates over derived type components, touching those components and
2098 setting their loop specifications, if there is a qualifier. If the
2099 object is itself a derived type, its components and subcomponents are
2100 touched. nml_read_obj is called at the end and this reads the data in
2101 the manner specified by the object name. */
2103 static try
2104 nml_get_obj_data (st_parameter_dt *dtp, namelist_info **pprev_nl,
2105 char *nml_err_msg)
2107 char c;
2108 namelist_info * nl;
2109 namelist_info * first_nl = NULL;
2110 namelist_info * root_nl = NULL;
2111 int dim;
2112 int component_flag;
2113 char parse_err_msg[30];
2114 index_type clow, chigh;
2116 /* Look for end of input or object name. If '?' or '=?' are encountered
2117 in stdin, print the node names or the namelist to stdout. */
2119 eat_separator (dtp);
2120 if (dtp->u.p.input_complete)
2121 return SUCCESS;
2123 if (dtp->u.p.at_eol)
2124 finish_separator (dtp);
2125 if (dtp->u.p.input_complete)
2126 return SUCCESS;
2128 c = next_char (dtp);
2129 switch (c)
2131 case '=':
2132 c = next_char (dtp);
2133 if (c != '?')
2135 st_sprintf (nml_err_msg, "namelist read: missplaced = sign");
2136 goto nml_err_ret;
2138 nml_query (dtp, '=');
2139 return SUCCESS;
2141 case '?':
2142 nml_query (dtp, '?');
2143 return SUCCESS;
2145 case '$':
2146 case '&':
2147 nml_match_name (dtp, "end", 3);
2148 if (dtp->u.p.nml_read_error)
2150 st_sprintf (nml_err_msg, "namelist not terminated with / or &end");
2151 goto nml_err_ret;
2153 case '/':
2154 dtp->u.p.input_complete = 1;
2155 return SUCCESS;
2157 default :
2158 break;
2161 /* Untouch all nodes of the namelist and reset the flag that is set for
2162 derived type components. */
2164 nml_untouch_nodes (dtp);
2165 component_flag = 0;
2167 /* Get the object name - should '!' and '\n' be permitted separators? */
2169 get_name:
2171 free_saved (dtp);
2175 push_char (dtp, tolower(c));
2176 c = next_char (dtp);
2177 } while (!( c=='=' || c==' ' || c=='\t' || c =='(' || c =='%' ));
2179 unget_char (dtp, c);
2181 /* Check that the name is in the namelist and get pointer to object.
2182 Three error conditions exist: (i) An attempt is being made to
2183 identify a non-existent object, following a failed data read or
2184 (ii) The object name does not exist or (iii) Too many data items
2185 are present for an object. (iii) gives the same error message
2186 as (i) */
2188 push_char (dtp, '\0');
2190 if (component_flag)
2192 size_t var_len = strlen (root_nl->var_name);
2193 size_t saved_len
2194 = dtp->u.p.saved_string ? strlen (dtp->u.p.saved_string) : 0;
2195 char ext_name[var_len + saved_len + 1];
2197 memcpy (ext_name, root_nl->var_name, var_len);
2198 if (dtp->u.p.saved_string)
2199 memcpy (ext_name + var_len, dtp->u.p.saved_string, saved_len);
2200 ext_name[var_len + saved_len] = '\0';
2201 nl = find_nml_node (dtp, ext_name);
2203 else
2204 nl = find_nml_node (dtp, dtp->u.p.saved_string);
2206 if (nl == NULL)
2208 if (dtp->u.p.nml_read_error && *pprev_nl)
2209 st_sprintf (nml_err_msg, "Bad data for namelist object %s",
2210 (*pprev_nl)->var_name);
2212 else
2213 st_sprintf (nml_err_msg, "Cannot match namelist object name %s",
2214 dtp->u.p.saved_string);
2216 goto nml_err_ret;
2219 /* Get the length, data length, base pointer and rank of the variable.
2220 Set the default loop specification first. */
2222 for (dim=0; dim < nl->var_rank; dim++)
2224 nl->ls[dim].step = 1;
2225 nl->ls[dim].end = nl->dim[dim].ubound;
2226 nl->ls[dim].start = nl->dim[dim].lbound;
2227 nl->ls[dim].idx = nl->ls[dim].start;
2230 /* Check to see if there is a qualifier: if so, parse it.*/
2232 if (c == '(' && nl->var_rank)
2234 if (nml_parse_qualifier (dtp, nl->dim, nl->ls, nl->var_rank,
2235 parse_err_msg) == FAILURE)
2237 st_sprintf (nml_err_msg, "%s for namelist variable %s",
2238 parse_err_msg, nl->var_name);
2239 goto nml_err_ret;
2241 c = next_char (dtp);
2242 unget_char (dtp, c);
2245 /* Now parse a derived type component. The root namelist_info address
2246 is backed up, as is the previous component level. The component flag
2247 is set and the iteration is made by jumping back to get_name. */
2249 if (c == '%')
2252 if (nl->type != GFC_DTYPE_DERIVED)
2254 st_sprintf (nml_err_msg, "Attempt to get derived component for %s",
2255 nl->var_name);
2256 goto nml_err_ret;
2259 if (!component_flag)
2260 first_nl = nl;
2262 root_nl = nl;
2263 component_flag = 1;
2264 c = next_char (dtp);
2265 goto get_name;
2269 /* Parse a character qualifier, if present. chigh = 0 is a default
2270 that signals that the string length = string_length. */
2272 clow = 1;
2273 chigh = 0;
2275 if (c == '(' && nl->type == GFC_DTYPE_CHARACTER)
2277 descriptor_dimension chd[1] = { {1, clow, nl->string_length} };
2278 array_loop_spec ind[1] = { {1, clow, nl->string_length, 1} };
2280 if (nml_parse_qualifier (dtp, chd, ind, 1, parse_err_msg) == FAILURE)
2282 st_sprintf (nml_err_msg, "%s for namelist variable %s",
2283 parse_err_msg, nl->var_name);
2284 goto nml_err_ret;
2287 clow = ind[0].start;
2288 chigh = ind[0].end;
2290 if (ind[0].step != 1)
2292 st_sprintf (nml_err_msg,
2293 "Bad step in substring for namelist object %s",
2294 nl->var_name);
2295 goto nml_err_ret;
2298 c = next_char (dtp);
2299 unget_char (dtp, c);
2302 /* If a derived type touch its components and restore the root
2303 namelist_info if we have parsed a qualified derived type
2304 component. */
2306 if (nl->type == GFC_DTYPE_DERIVED)
2307 nml_touch_nodes (nl);
2308 if (component_flag)
2309 nl = first_nl;
2311 /*make sure no extraneous qualifiers are there.*/
2313 if (c == '(')
2315 st_sprintf (nml_err_msg, "Qualifier for a scalar or non-character"
2316 " namelist object %s", nl->var_name);
2317 goto nml_err_ret;
2320 /* According to the standard, an equal sign MUST follow an object name. The
2321 following is possibly lax - it allows comments, blank lines and so on to
2322 intervene. eat_spaces (dtp); c = next_char (dtp); would be compliant*/
2324 free_saved (dtp);
2326 eat_separator (dtp);
2327 if (dtp->u.p.input_complete)
2328 return SUCCESS;
2330 if (dtp->u.p.at_eol)
2331 finish_separator (dtp);
2332 if (dtp->u.p.input_complete)
2333 return SUCCESS;
2335 c = next_char (dtp);
2337 if (c != '=')
2339 st_sprintf (nml_err_msg, "Equal sign must follow namelist object name %s",
2340 nl->var_name);
2341 goto nml_err_ret;
2344 if (nml_read_obj (dtp, nl, 0, pprev_nl, nml_err_msg, clow, chigh) == FAILURE)
2345 goto nml_err_ret;
2347 return SUCCESS;
2349 nml_err_ret:
2351 return FAILURE;
2354 /* Entry point for namelist input. Goes through input until namelist name
2355 is matched. Then cycles through nml_get_obj_data until the input is
2356 completed or there is an error. */
2358 void
2359 namelist_read (st_parameter_dt *dtp)
2361 char c;
2362 jmp_buf eof_jump;
2363 char nml_err_msg[100];
2364 /* Pointer to the previously read object, in case attempt is made to read
2365 new object name. Should this fail, error message can give previous
2366 name. */
2367 namelist_info *prev_nl = NULL;
2369 dtp->u.p.namelist_mode = 1;
2370 dtp->u.p.input_complete = 0;
2372 dtp->u.p.eof_jump = &eof_jump;
2373 if (setjmp (eof_jump))
2375 dtp->u.p.eof_jump = NULL;
2376 generate_error (&dtp->common, ERROR_END, NULL);
2377 return;
2380 /* Look for &namelist_name . Skip all characters, testing for $nmlname.
2381 Exit on success or EOF. If '?' or '=?' encountered in stdin, print
2382 node names or namelist on stdout. */
2384 find_nml_name:
2385 switch (c = next_char (dtp))
2387 case '$':
2388 case '&':
2389 break;
2391 case '=':
2392 c = next_char (dtp);
2393 if (c == '?')
2394 nml_query (dtp, '=');
2395 else
2396 unget_char (dtp, c);
2397 goto find_nml_name;
2399 case '?':
2400 nml_query (dtp, '?');
2402 default:
2403 goto find_nml_name;
2406 /* Match the name of the namelist. */
2408 nml_match_name (dtp, dtp->namelist_name, dtp->namelist_name_len);
2410 if (dtp->u.p.nml_read_error)
2411 goto find_nml_name;
2413 /* Ready to read namelist objects. If there is an error in input
2414 from stdin, output the error message and continue. */
2416 while (!dtp->u.p.input_complete)
2418 if (nml_get_obj_data (dtp, &prev_nl, nml_err_msg) == FAILURE)
2420 gfc_unit *u;
2422 if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
2423 goto nml_err_ret;
2425 u = find_unit (options.stderr_unit);
2426 st_printf ("%s\n", nml_err_msg);
2427 if (u != NULL)
2429 flush (u->s);
2430 unlock_unit (u);
2436 dtp->u.p.eof_jump = NULL;
2437 free_saved (dtp);
2438 return;
2440 /* All namelist error calls return from here */
2442 nml_err_ret:
2444 dtp->u.p.eof_jump = NULL;
2445 free_saved (dtp);
2446 generate_error (&dtp->common, ERROR_READ_VALUE, nml_err_msg);
2447 return;