PR lto/42531
[official-gcc.git] / libgfortran / io / list_read.c
blobc281e34eacfbe6510561ba914651dd80c14937f6
1 /* Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 2009
2 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
4 Namelist input contributed by Paul Thomas
5 F2003 I/O support contributed by Jerry DeLisle
7 This file is part of the GNU Fortran 95 runtime library (libgfortran).
9 Libgfortran is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
14 Libgfortran is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 Under Section 7 of GPL version 3, you are granted additional
20 permissions described in the GCC Runtime Library Exception, version
21 3.1, as published by the Free Software Foundation.
23 You should have received a copy of the GNU General Public License and
24 a copy of the GCC Runtime Library Exception along with this program;
25 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
26 <http://www.gnu.org/licenses/>. */
29 #include "io.h"
30 #include "fbuf.h"
31 #include "unix.h"
32 #include <string.h>
33 #include <stdlib.h>
34 #include <ctype.h>
37 /* List directed input. Several parsing subroutines are practically
38 reimplemented from formatted input, the reason being that there are
39 all kinds of small differences between formatted and list directed
40 parsing. */
43 /* Subroutines for reading characters from the input. Because a
44 repeat count is ambiguous with an integer, we have to read the
45 whole digit string before seeing if there is a '*' which signals
46 the repeat count. Since we can have a lot of potential leading
47 zeros, we have to be able to back up by arbitrary amount. Because
48 the input might not be seekable, we have to buffer the data
49 ourselves. */
51 #define CASE_DIGITS case '0': case '1': case '2': case '3': case '4': \
52 case '5': case '6': case '7': case '8': case '9'
54 #define CASE_SEPARATORS case ' ': case ',': case '/': case '\n': case '\t': \
55 case '\r': case ';'
57 /* This macro assumes that we're operating on a variable. */
59 #define is_separator(c) (c == '/' || c == ',' || c == '\n' || c == ' ' \
60 || c == '\t' || c == '\r' || c == ';')
62 /* Maximum repeat count. Less than ten times the maximum signed int32. */
64 #define MAX_REPEAT 200000000
66 #ifndef HAVE_SNPRINTF
67 # undef snprintf
68 # define snprintf(str, size, ...) sprintf (str, __VA_ARGS__)
69 #endif
71 /* Save a character to a string buffer, enlarging it as necessary. */
73 static void
74 push_char (st_parameter_dt *dtp, char c)
76 char *new;
78 if (dtp->u.p.saved_string == NULL)
80 dtp->u.p.saved_string = get_mem (SCRATCH_SIZE);
81 // memset below should be commented out.
82 memset (dtp->u.p.saved_string, 0, SCRATCH_SIZE);
83 dtp->u.p.saved_length = SCRATCH_SIZE;
84 dtp->u.p.saved_used = 0;
87 if (dtp->u.p.saved_used >= dtp->u.p.saved_length)
89 dtp->u.p.saved_length = 2 * dtp->u.p.saved_length;
90 new = realloc (dtp->u.p.saved_string, dtp->u.p.saved_length);
91 if (new == NULL)
92 generate_error (&dtp->common, LIBERROR_OS, NULL);
93 dtp->u.p.saved_string = new;
95 // Also this should not be necessary.
96 memset (new + dtp->u.p.saved_used, 0,
97 dtp->u.p.saved_length - dtp->u.p.saved_used);
101 dtp->u.p.saved_string[dtp->u.p.saved_used++] = c;
105 /* Free the input buffer if necessary. */
107 static void
108 free_saved (st_parameter_dt *dtp)
110 if (dtp->u.p.saved_string == NULL)
111 return;
113 free_mem (dtp->u.p.saved_string);
115 dtp->u.p.saved_string = NULL;
116 dtp->u.p.saved_used = 0;
120 /* Free the line buffer if necessary. */
122 static void
123 free_line (st_parameter_dt *dtp)
125 dtp->u.p.item_count = 0;
126 dtp->u.p.line_buffer_enabled = 0;
128 if (dtp->u.p.line_buffer == NULL)
129 return;
131 free_mem (dtp->u.p.line_buffer);
132 dtp->u.p.line_buffer = NULL;
136 static char
137 next_char (st_parameter_dt *dtp)
139 ssize_t length;
140 gfc_offset record;
141 char c;
142 int cc;
144 if (dtp->u.p.last_char != '\0')
146 dtp->u.p.at_eol = 0;
147 c = dtp->u.p.last_char;
148 dtp->u.p.last_char = '\0';
149 goto done;
152 /* Read from line_buffer if enabled. */
154 if (dtp->u.p.line_buffer_enabled)
156 dtp->u.p.at_eol = 0;
158 c = dtp->u.p.line_buffer[dtp->u.p.item_count];
159 if (c != '\0' && dtp->u.p.item_count < 64)
161 dtp->u.p.line_buffer[dtp->u.p.item_count] = '\0';
162 dtp->u.p.item_count++;
163 goto done;
166 dtp->u.p.item_count = 0;
167 dtp->u.p.line_buffer_enabled = 0;
170 /* Handle the end-of-record and end-of-file conditions for
171 internal array unit. */
172 if (is_array_io (dtp))
174 if (dtp->u.p.at_eof)
175 longjmp (*dtp->u.p.eof_jump, 1);
177 /* Check for "end-of-record" condition. */
178 if (dtp->u.p.current_unit->bytes_left == 0)
180 int finished;
182 c = '\n';
183 record = next_array_record (dtp, dtp->u.p.current_unit->ls,
184 &finished);
186 /* Check for "end-of-file" condition. */
187 if (finished)
189 dtp->u.p.at_eof = 1;
190 goto done;
193 record *= dtp->u.p.current_unit->recl;
194 if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
195 longjmp (*dtp->u.p.eof_jump, 1);
197 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
198 goto done;
202 /* Get the next character and handle end-of-record conditions. */
204 if (is_internal_unit (dtp))
206 length = sread (dtp->u.p.current_unit->s, &c, 1);
207 if (length < 0)
209 generate_error (&dtp->common, LIBERROR_OS, NULL);
210 return '\0';
213 if (is_array_io (dtp))
215 /* Check whether we hit EOF. */
216 if (length == 0)
218 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
219 return '\0';
221 dtp->u.p.current_unit->bytes_left--;
223 else
225 if (dtp->u.p.at_eof)
226 longjmp (*dtp->u.p.eof_jump, 1);
227 if (length == 0)
229 c = '\n';
230 dtp->u.p.at_eof = 1;
234 else
236 cc = fbuf_getc (dtp->u.p.current_unit);
238 if (cc == EOF)
240 if (dtp->u.p.current_unit->endfile == AT_ENDFILE)
241 longjmp (*dtp->u.p.eof_jump, 1);
242 dtp->u.p.current_unit->endfile = AT_ENDFILE;
243 c = '\n';
245 else
246 c = (char) cc;
247 if (is_stream_io (dtp) && cc != EOF)
248 dtp->u.p.current_unit->strm_pos++;
251 done:
252 dtp->u.p.at_eol = (c == '\n' || c == '\r');
253 return c;
257 /* Push a character back onto the input. */
259 static void
260 unget_char (st_parameter_dt *dtp, char c)
262 dtp->u.p.last_char = c;
266 /* Skip over spaces in the input. Returns the nonspace character that
267 terminated the eating and also places it back on the input. */
269 static char
270 eat_spaces (st_parameter_dt *dtp)
272 char c;
276 c = next_char (dtp);
278 while (c == ' ' || c == '\t');
280 unget_char (dtp, c);
281 return c;
285 /* This function reads characters through to the end of the current line and
286 just ignores them. */
288 static void
289 eat_line (st_parameter_dt *dtp)
291 char c;
294 c = next_char (dtp);
295 while (c != '\n');
299 /* Skip over a separator. Technically, we don't always eat the whole
300 separator. This is because if we've processed the last input item,
301 then a separator is unnecessary. Plus the fact that operating
302 systems usually deliver console input on a line basis.
304 The upshot is that if we see a newline as part of reading a
305 separator, we stop reading. If there are more input items, we
306 continue reading the separator with finish_separator() which takes
307 care of the fact that we may or may not have seen a comma as part
308 of the separator. */
310 static void
311 eat_separator (st_parameter_dt *dtp)
313 char c, n;
315 eat_spaces (dtp);
316 dtp->u.p.comma_flag = 0;
318 c = next_char (dtp);
319 switch (c)
321 case ',':
322 if (dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
324 unget_char (dtp, c);
325 break;
327 /* Fall through. */
328 case ';':
329 dtp->u.p.comma_flag = 1;
330 eat_spaces (dtp);
331 break;
333 case '/':
334 dtp->u.p.input_complete = 1;
335 break;
337 case '\r':
338 dtp->u.p.at_eol = 1;
339 n = next_char(dtp);
340 if (n != '\n')
342 unget_char (dtp, n);
343 break;
345 /* Fall through. */
346 case '\n':
347 dtp->u.p.at_eol = 1;
348 if (dtp->u.p.namelist_mode)
352 c = next_char (dtp);
353 if (c == '!')
355 eat_line (dtp);
356 c = next_char (dtp);
357 if (c == '!')
359 eat_line (dtp);
360 c = next_char (dtp);
364 while (c == '\n' || c == '\r' || c == ' ' || c == '\t');
365 unget_char (dtp, c);
367 break;
369 case '!':
370 if (dtp->u.p.namelist_mode)
371 { /* Eat a namelist comment. */
373 c = next_char (dtp);
374 while (c != '\n');
376 break;
379 /* Fall Through... */
381 default:
382 unget_char (dtp, c);
383 break;
388 /* Finish processing a separator that was interrupted by a newline.
389 If we're here, then another data item is present, so we finish what
390 we started on the previous line. */
392 static void
393 finish_separator (st_parameter_dt *dtp)
395 char c;
397 restart:
398 eat_spaces (dtp);
400 c = next_char (dtp);
401 switch (c)
403 case ',':
404 if (dtp->u.p.comma_flag)
405 unget_char (dtp, c);
406 else
408 c = eat_spaces (dtp);
409 if (c == '\n' || c == '\r')
410 goto restart;
413 break;
415 case '/':
416 dtp->u.p.input_complete = 1;
417 if (!dtp->u.p.namelist_mode)
418 return;
419 break;
421 case '\n':
422 case '\r':
423 goto restart;
425 case '!':
426 if (dtp->u.p.namelist_mode)
429 c = next_char (dtp);
430 while (c != '\n');
432 goto restart;
435 default:
436 unget_char (dtp, c);
437 break;
442 /* This function is needed to catch bad conversions so that namelist can
443 attempt to see if dtp->u.p.saved_string contains a new object name rather
444 than a bad value. */
446 static int
447 nml_bad_return (st_parameter_dt *dtp, char c)
449 if (dtp->u.p.namelist_mode)
451 dtp->u.p.nml_read_error = 1;
452 unget_char (dtp, c);
453 return 1;
455 return 0;
458 /* Convert an unsigned string to an integer. The length value is -1
459 if we are working on a repeat count. Returns nonzero if we have a
460 range problem. As a side effect, frees the dtp->u.p.saved_string. */
462 static int
463 convert_integer (st_parameter_dt *dtp, int length, int negative)
465 char c, *buffer, message[100];
466 int m;
467 GFC_INTEGER_LARGEST v, max, max10;
469 buffer = dtp->u.p.saved_string;
470 v = 0;
472 max = (length == -1) ? MAX_REPEAT : max_value (length, 1);
473 max10 = max / 10;
475 for (;;)
477 c = *buffer++;
478 if (c == '\0')
479 break;
480 c -= '0';
482 if (v > max10)
483 goto overflow;
484 v = 10 * v;
486 if (v > max - c)
487 goto overflow;
488 v += c;
491 m = 0;
493 if (length != -1)
495 if (negative)
496 v = -v;
497 set_integer (dtp->u.p.value, v, length);
499 else
501 dtp->u.p.repeat_count = v;
503 if (dtp->u.p.repeat_count == 0)
505 sprintf (message, "Zero repeat count in item %d of list input",
506 dtp->u.p.item_count);
508 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
509 m = 1;
513 free_saved (dtp);
514 return m;
516 overflow:
517 if (length == -1)
518 sprintf (message, "Repeat count overflow in item %d of list input",
519 dtp->u.p.item_count);
520 else
521 sprintf (message, "Integer overflow while reading item %d",
522 dtp->u.p.item_count);
524 free_saved (dtp);
525 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
527 return 1;
531 /* Parse a repeat count for logical and complex values which cannot
532 begin with a digit. Returns nonzero if we are done, zero if we
533 should continue on. */
535 static int
536 parse_repeat (st_parameter_dt *dtp)
538 char c, message[100];
539 int repeat;
541 c = next_char (dtp);
542 switch (c)
544 CASE_DIGITS:
545 repeat = c - '0';
546 break;
548 CASE_SEPARATORS:
549 unget_char (dtp, c);
550 eat_separator (dtp);
551 return 1;
553 default:
554 unget_char (dtp, c);
555 return 0;
558 for (;;)
560 c = next_char (dtp);
561 switch (c)
563 CASE_DIGITS:
564 repeat = 10 * repeat + c - '0';
566 if (repeat > MAX_REPEAT)
568 sprintf (message,
569 "Repeat count overflow in item %d of list input",
570 dtp->u.p.item_count);
572 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
573 return 1;
576 break;
578 case '*':
579 if (repeat == 0)
581 sprintf (message,
582 "Zero repeat count in item %d of list input",
583 dtp->u.p.item_count);
585 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
586 return 1;
589 goto done;
591 default:
592 goto bad_repeat;
596 done:
597 dtp->u.p.repeat_count = repeat;
598 return 0;
600 bad_repeat:
602 eat_line (dtp);
603 free_saved (dtp);
604 sprintf (message, "Bad repeat count in item %d of list input",
605 dtp->u.p.item_count);
606 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
607 return 1;
611 /* To read a logical we have to look ahead in the input stream to make sure
612 there is not an equal sign indicating a variable name. To do this we use
613 line_buffer to point to a temporary buffer, pushing characters there for
614 possible later reading. */
616 static void
617 l_push_char (st_parameter_dt *dtp, char c)
619 if (dtp->u.p.line_buffer == NULL)
621 dtp->u.p.line_buffer = get_mem (SCRATCH_SIZE);
622 memset (dtp->u.p.line_buffer, 0, SCRATCH_SIZE);
625 dtp->u.p.line_buffer[dtp->u.p.item_count++] = c;
629 /* Read a logical character on the input. */
631 static void
632 read_logical (st_parameter_dt *dtp, int length)
634 char c, message[100];
635 int i, v;
637 if (parse_repeat (dtp))
638 return;
640 c = tolower (next_char (dtp));
641 l_push_char (dtp, c);
642 switch (c)
644 case 't':
645 v = 1;
646 c = next_char (dtp);
647 l_push_char (dtp, c);
649 if (!is_separator(c))
650 goto possible_name;
652 unget_char (dtp, c);
653 break;
654 case 'f':
655 v = 0;
656 c = next_char (dtp);
657 l_push_char (dtp, c);
659 if (!is_separator(c))
660 goto possible_name;
662 unget_char (dtp, c);
663 break;
665 case '.':
666 c = tolower (next_char (dtp));
667 switch (c)
669 case 't':
670 v = 1;
671 break;
672 case 'f':
673 v = 0;
674 break;
675 default:
676 goto bad_logical;
679 break;
681 CASE_SEPARATORS:
682 unget_char (dtp, c);
683 eat_separator (dtp);
684 return; /* Null value. */
686 default:
687 /* Save the character in case it is the beginning
688 of the next object name. */
689 unget_char (dtp, c);
690 goto bad_logical;
693 dtp->u.p.saved_type = BT_LOGICAL;
694 dtp->u.p.saved_length = length;
696 /* Eat trailing garbage. */
699 c = next_char (dtp);
701 while (!is_separator (c));
703 unget_char (dtp, c);
704 eat_separator (dtp);
705 set_integer ((int *) dtp->u.p.value, v, length);
706 free_line (dtp);
708 return;
710 possible_name:
712 for(i = 0; i < 63; i++)
714 c = next_char (dtp);
715 if (is_separator(c))
717 /* All done if this is not a namelist read. */
718 if (!dtp->u.p.namelist_mode)
719 goto logical_done;
721 unget_char (dtp, c);
722 eat_separator (dtp);
723 c = next_char (dtp);
724 if (c != '=')
726 unget_char (dtp, c);
727 goto logical_done;
731 l_push_char (dtp, c);
732 if (c == '=')
734 dtp->u.p.nml_read_error = 1;
735 dtp->u.p.line_buffer_enabled = 1;
736 dtp->u.p.item_count = 0;
737 return;
742 bad_logical:
744 free_line (dtp);
746 if (nml_bad_return (dtp, c))
747 return;
749 eat_line (dtp);
750 free_saved (dtp);
751 sprintf (message, "Bad logical value while reading item %d",
752 dtp->u.p.item_count);
753 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
754 return;
756 logical_done:
758 dtp->u.p.saved_type = BT_LOGICAL;
759 dtp->u.p.saved_length = length;
760 set_integer ((int *) dtp->u.p.value, v, length);
761 free_saved (dtp);
762 free_line (dtp);
766 /* Reading integers is tricky because we can actually be reading a
767 repeat count. We have to store the characters in a buffer because
768 we could be reading an integer that is larger than the default int
769 used for repeat counts. */
771 static void
772 read_integer (st_parameter_dt *dtp, int length)
774 char c, message[100];
775 int negative;
777 negative = 0;
779 c = next_char (dtp);
780 switch (c)
782 case '-':
783 negative = 1;
784 /* Fall through... */
786 case '+':
787 c = next_char (dtp);
788 goto get_integer;
790 CASE_SEPARATORS: /* Single null. */
791 unget_char (dtp, c);
792 eat_separator (dtp);
793 return;
795 CASE_DIGITS:
796 push_char (dtp, c);
797 break;
799 default:
800 goto bad_integer;
803 /* Take care of what may be a repeat count. */
805 for (;;)
807 c = next_char (dtp);
808 switch (c)
810 CASE_DIGITS:
811 push_char (dtp, c);
812 break;
814 case '*':
815 push_char (dtp, '\0');
816 goto repeat;
818 CASE_SEPARATORS: /* Not a repeat count. */
819 goto done;
821 default:
822 goto bad_integer;
826 repeat:
827 if (convert_integer (dtp, -1, 0))
828 return;
830 /* Get the real integer. */
832 c = next_char (dtp);
833 switch (c)
835 CASE_DIGITS:
836 break;
838 CASE_SEPARATORS:
839 unget_char (dtp, c);
840 eat_separator (dtp);
841 return;
843 case '-':
844 negative = 1;
845 /* Fall through... */
847 case '+':
848 c = next_char (dtp);
849 break;
852 get_integer:
853 if (!isdigit (c))
854 goto bad_integer;
855 push_char (dtp, c);
857 for (;;)
859 c = next_char (dtp);
860 switch (c)
862 CASE_DIGITS:
863 push_char (dtp, c);
864 break;
866 CASE_SEPARATORS:
867 goto done;
869 default:
870 goto bad_integer;
874 bad_integer:
876 if (nml_bad_return (dtp, c))
877 return;
879 eat_line (dtp);
880 free_saved (dtp);
881 sprintf (message, "Bad integer for item %d in list input",
882 dtp->u.p.item_count);
883 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
885 return;
887 done:
888 unget_char (dtp, c);
889 eat_separator (dtp);
891 push_char (dtp, '\0');
892 if (convert_integer (dtp, length, negative))
894 free_saved (dtp);
895 return;
898 free_saved (dtp);
899 dtp->u.p.saved_type = BT_INTEGER;
903 /* Read a character variable. */
905 static void
906 read_character (st_parameter_dt *dtp, int length __attribute__ ((unused)))
908 char c, quote, message[100];
910 quote = ' '; /* Space means no quote character. */
912 c = next_char (dtp);
913 switch (c)
915 CASE_DIGITS:
916 push_char (dtp, c);
917 break;
919 CASE_SEPARATORS:
920 unget_char (dtp, c); /* NULL value. */
921 eat_separator (dtp);
922 return;
924 case '"':
925 case '\'':
926 quote = c;
927 goto get_string;
929 default:
930 if (dtp->u.p.namelist_mode)
932 unget_char (dtp, c);
933 return;
936 push_char (dtp, c);
937 goto get_string;
940 /* Deal with a possible repeat count. */
942 for (;;)
944 c = next_char (dtp);
945 switch (c)
947 CASE_DIGITS:
948 push_char (dtp, c);
949 break;
951 CASE_SEPARATORS:
952 unget_char (dtp, c);
953 goto done; /* String was only digits! */
955 case '*':
956 push_char (dtp, '\0');
957 goto got_repeat;
959 default:
960 push_char (dtp, c);
961 goto get_string; /* Not a repeat count after all. */
965 got_repeat:
966 if (convert_integer (dtp, -1, 0))
967 return;
969 /* Now get the real string. */
971 c = next_char (dtp);
972 switch (c)
974 CASE_SEPARATORS:
975 unget_char (dtp, c); /* Repeated NULL values. */
976 eat_separator (dtp);
977 return;
979 case '"':
980 case '\'':
981 quote = c;
982 break;
984 default:
985 push_char (dtp, c);
986 break;
989 get_string:
990 for (;;)
992 c = next_char (dtp);
993 switch (c)
995 case '"':
996 case '\'':
997 if (c != quote)
999 push_char (dtp, c);
1000 break;
1003 /* See if we have a doubled quote character or the end of
1004 the string. */
1006 c = next_char (dtp);
1007 if (c == quote)
1009 push_char (dtp, quote);
1010 break;
1013 unget_char (dtp, c);
1014 goto done;
1016 CASE_SEPARATORS:
1017 if (quote == ' ')
1019 unget_char (dtp, c);
1020 goto done;
1023 if (c != '\n' && c != '\r')
1024 push_char (dtp, c);
1025 break;
1027 default:
1028 push_char (dtp, c);
1029 break;
1033 /* At this point, we have to have a separator, or else the string is
1034 invalid. */
1035 done:
1036 c = next_char (dtp);
1037 if (is_separator (c) || c == '!')
1039 unget_char (dtp, c);
1040 eat_separator (dtp);
1041 dtp->u.p.saved_type = BT_CHARACTER;
1042 free_line (dtp);
1044 else
1046 free_saved (dtp);
1047 sprintf (message, "Invalid string input in item %d",
1048 dtp->u.p.item_count);
1049 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1054 /* Parse a component of a complex constant or a real number that we
1055 are sure is already there. This is a straight real number parser. */
1057 static int
1058 parse_real (st_parameter_dt *dtp, void *buffer, int length)
1060 char c, message[100];
1061 int m, seen_dp;
1063 c = next_char (dtp);
1064 if (c == '-' || c == '+')
1066 push_char (dtp, c);
1067 c = next_char (dtp);
1070 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1071 c = '.';
1073 if (!isdigit (c) && c != '.')
1075 if (c == 'i' || c == 'I' || c == 'n' || c == 'N')
1076 goto inf_nan;
1077 else
1078 goto bad;
1081 push_char (dtp, c);
1083 seen_dp = (c == '.') ? 1 : 0;
1085 for (;;)
1087 c = next_char (dtp);
1088 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1089 c = '.';
1090 switch (c)
1092 CASE_DIGITS:
1093 push_char (dtp, c);
1094 break;
1096 case '.':
1097 if (seen_dp)
1098 goto bad;
1100 seen_dp = 1;
1101 push_char (dtp, c);
1102 break;
1104 case 'e':
1105 case 'E':
1106 case 'd':
1107 case 'D':
1108 push_char (dtp, 'e');
1109 goto exp1;
1111 case '-':
1112 case '+':
1113 push_char (dtp, 'e');
1114 push_char (dtp, c);
1115 c = next_char (dtp);
1116 goto exp2;
1118 CASE_SEPARATORS:
1119 unget_char (dtp, c);
1120 goto done;
1122 default:
1123 goto done;
1127 exp1:
1128 c = next_char (dtp);
1129 if (c != '-' && c != '+')
1130 push_char (dtp, '+');
1131 else
1133 push_char (dtp, c);
1134 c = next_char (dtp);
1137 exp2:
1138 if (!isdigit (c))
1139 goto bad;
1141 push_char (dtp, c);
1143 for (;;)
1145 c = next_char (dtp);
1146 switch (c)
1148 CASE_DIGITS:
1149 push_char (dtp, c);
1150 break;
1152 CASE_SEPARATORS:
1153 unget_char (dtp, c);
1154 goto done;
1156 default:
1157 goto done;
1161 done:
1162 unget_char (dtp, c);
1163 push_char (dtp, '\0');
1165 m = convert_real (dtp, buffer, dtp->u.p.saved_string, length);
1166 free_saved (dtp);
1168 return m;
1170 inf_nan:
1171 /* Match INF and Infinity. */
1172 if ((c == 'i' || c == 'I')
1173 && ((c = next_char (dtp)) == 'n' || c == 'N')
1174 && ((c = next_char (dtp)) == 'f' || c == 'F'))
1176 c = next_char (dtp);
1177 if ((c != 'i' && c != 'I')
1178 || ((c == 'i' || c == 'I')
1179 && ((c = next_char (dtp)) == 'n' || c == 'N')
1180 && ((c = next_char (dtp)) == 'i' || c == 'I')
1181 && ((c = next_char (dtp)) == 't' || c == 'T')
1182 && ((c = next_char (dtp)) == 'y' || c == 'Y')
1183 && (c = next_char (dtp))))
1185 if (is_separator (c))
1186 unget_char (dtp, c);
1187 push_char (dtp, 'i');
1188 push_char (dtp, 'n');
1189 push_char (dtp, 'f');
1190 goto done;
1192 } /* Match NaN. */
1193 else if (((c = next_char (dtp)) == 'a' || c == 'A')
1194 && ((c = next_char (dtp)) == 'n' || c == 'N')
1195 && (c = next_char (dtp)))
1197 if (is_separator (c))
1198 unget_char (dtp, c);
1199 push_char (dtp, 'n');
1200 push_char (dtp, 'a');
1201 push_char (dtp, 'n');
1202 goto done;
1205 bad:
1207 if (nml_bad_return (dtp, c))
1208 return 0;
1210 eat_line (dtp);
1211 free_saved (dtp);
1212 sprintf (message, "Bad floating point number for item %d",
1213 dtp->u.p.item_count);
1214 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1216 return 1;
1220 /* Reading a complex number is straightforward because we can tell
1221 what it is right away. */
1223 static void
1224 read_complex (st_parameter_dt *dtp, void * dest, int kind, size_t size)
1226 char message[100];
1227 char c;
1229 if (parse_repeat (dtp))
1230 return;
1232 c = next_char (dtp);
1233 switch (c)
1235 case '(':
1236 break;
1238 CASE_SEPARATORS:
1239 unget_char (dtp, c);
1240 eat_separator (dtp);
1241 return;
1243 default:
1244 goto bad_complex;
1247 eat_spaces (dtp);
1248 if (parse_real (dtp, dest, kind))
1249 return;
1251 eol_1:
1252 eat_spaces (dtp);
1253 c = next_char (dtp);
1254 if (c == '\n' || c== '\r')
1255 goto eol_1;
1256 else
1257 unget_char (dtp, c);
1259 if (next_char (dtp)
1260 != (dtp->u.p.current_unit->decimal_status == DECIMAL_POINT ? ',' : ';'))
1261 goto bad_complex;
1263 eol_2:
1264 eat_spaces (dtp);
1265 c = next_char (dtp);
1266 if (c == '\n' || c== '\r')
1267 goto eol_2;
1268 else
1269 unget_char (dtp, c);
1271 if (parse_real (dtp, dest + size / 2, kind))
1272 return;
1274 eat_spaces (dtp);
1275 if (next_char (dtp) != ')')
1276 goto bad_complex;
1278 c = next_char (dtp);
1279 if (!is_separator (c))
1280 goto bad_complex;
1282 unget_char (dtp, c);
1283 eat_separator (dtp);
1285 free_saved (dtp);
1286 dtp->u.p.saved_type = BT_COMPLEX;
1287 return;
1289 bad_complex:
1291 if (nml_bad_return (dtp, c))
1292 return;
1294 eat_line (dtp);
1295 free_saved (dtp);
1296 sprintf (message, "Bad complex value in item %d of list input",
1297 dtp->u.p.item_count);
1298 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1302 /* Parse a real number with a possible repeat count. */
1304 static void
1305 read_real (st_parameter_dt *dtp, void * dest, int length)
1307 char c, message[100];
1308 int seen_dp;
1309 int is_inf;
1311 seen_dp = 0;
1313 c = next_char (dtp);
1314 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1315 c = '.';
1316 switch (c)
1318 CASE_DIGITS:
1319 push_char (dtp, c);
1320 break;
1322 case '.':
1323 push_char (dtp, c);
1324 seen_dp = 1;
1325 break;
1327 case '+':
1328 case '-':
1329 goto got_sign;
1331 CASE_SEPARATORS:
1332 unget_char (dtp, c); /* Single null. */
1333 eat_separator (dtp);
1334 return;
1336 case 'i':
1337 case 'I':
1338 case 'n':
1339 case 'N':
1340 goto inf_nan;
1342 default:
1343 goto bad_real;
1346 /* Get the digit string that might be a repeat count. */
1348 for (;;)
1350 c = next_char (dtp);
1351 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1352 c = '.';
1353 switch (c)
1355 CASE_DIGITS:
1356 push_char (dtp, c);
1357 break;
1359 case '.':
1360 if (seen_dp)
1361 goto bad_real;
1363 seen_dp = 1;
1364 push_char (dtp, c);
1365 goto real_loop;
1367 case 'E':
1368 case 'e':
1369 case 'D':
1370 case 'd':
1371 goto exp1;
1373 case '+':
1374 case '-':
1375 push_char (dtp, 'e');
1376 push_char (dtp, c);
1377 c = next_char (dtp);
1378 goto exp2;
1380 case '*':
1381 push_char (dtp, '\0');
1382 goto got_repeat;
1384 CASE_SEPARATORS:
1385 if (c != '\n' && c != ',' && c != '\r' && c != ';')
1386 unget_char (dtp, c);
1387 goto done;
1389 default:
1390 goto bad_real;
1394 got_repeat:
1395 if (convert_integer (dtp, -1, 0))
1396 return;
1398 /* Now get the number itself. */
1400 c = next_char (dtp);
1401 if (is_separator (c))
1402 { /* Repeated null value. */
1403 unget_char (dtp, c);
1404 eat_separator (dtp);
1405 return;
1408 if (c != '-' && c != '+')
1409 push_char (dtp, '+');
1410 else
1412 got_sign:
1413 push_char (dtp, c);
1414 c = next_char (dtp);
1417 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1418 c = '.';
1420 if (!isdigit (c) && c != '.')
1422 if (c == 'i' || c == 'I' || c == 'n' || c == 'N')
1423 goto inf_nan;
1424 else
1425 goto bad_real;
1428 if (c == '.')
1430 if (seen_dp)
1431 goto bad_real;
1432 else
1433 seen_dp = 1;
1436 push_char (dtp, c);
1438 real_loop:
1439 for (;;)
1441 c = next_char (dtp);
1442 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1443 c = '.';
1444 switch (c)
1446 CASE_DIGITS:
1447 push_char (dtp, c);
1448 break;
1450 CASE_SEPARATORS:
1451 goto done;
1453 case '.':
1454 if (seen_dp)
1455 goto bad_real;
1457 seen_dp = 1;
1458 push_char (dtp, c);
1459 break;
1461 case 'E':
1462 case 'e':
1463 case 'D':
1464 case 'd':
1465 goto exp1;
1467 case '+':
1468 case '-':
1469 push_char (dtp, 'e');
1470 push_char (dtp, c);
1471 c = next_char (dtp);
1472 goto exp2;
1474 default:
1475 goto bad_real;
1479 exp1:
1480 push_char (dtp, 'e');
1482 c = next_char (dtp);
1483 if (c != '+' && c != '-')
1484 push_char (dtp, '+');
1485 else
1487 push_char (dtp, c);
1488 c = next_char (dtp);
1491 exp2:
1492 if (!isdigit (c))
1493 goto bad_real;
1494 push_char (dtp, c);
1496 for (;;)
1498 c = next_char (dtp);
1500 switch (c)
1502 CASE_DIGITS:
1503 push_char (dtp, c);
1504 break;
1506 CASE_SEPARATORS:
1507 goto done;
1509 default:
1510 goto bad_real;
1514 done:
1515 unget_char (dtp, c);
1516 eat_separator (dtp);
1517 push_char (dtp, '\0');
1518 if (convert_real (dtp, dest, dtp->u.p.saved_string, length))
1519 return;
1521 free_saved (dtp);
1522 dtp->u.p.saved_type = BT_REAL;
1523 return;
1525 inf_nan:
1526 l_push_char (dtp, c);
1527 is_inf = 0;
1529 /* Match INF and Infinity. */
1530 if (c == 'i' || c == 'I')
1532 c = next_char (dtp);
1533 l_push_char (dtp, c);
1534 if (c != 'n' && c != 'N')
1535 goto unwind;
1536 c = next_char (dtp);
1537 l_push_char (dtp, c);
1538 if (c != 'f' && c != 'F')
1539 goto unwind;
1540 c = next_char (dtp);
1541 l_push_char (dtp, c);
1542 if (!is_separator (c))
1544 if (c != 'i' && c != 'I')
1545 goto unwind;
1546 c = next_char (dtp);
1547 l_push_char (dtp, c);
1548 if (c != 'n' && c != 'N')
1549 goto unwind;
1550 c = next_char (dtp);
1551 l_push_char (dtp, c);
1552 if (c != 'i' && c != 'I')
1553 goto unwind;
1554 c = next_char (dtp);
1555 l_push_char (dtp, c);
1556 if (c != 't' && c != 'T')
1557 goto unwind;
1558 c = next_char (dtp);
1559 l_push_char (dtp, c);
1560 if (c != 'y' && c != 'Y')
1561 goto unwind;
1562 c = next_char (dtp);
1563 l_push_char (dtp, c);
1565 is_inf = 1;
1566 } /* Match NaN. */
1567 else
1569 c = next_char (dtp);
1570 l_push_char (dtp, c);
1571 if (c != 'a' && c != 'A')
1572 goto unwind;
1573 c = next_char (dtp);
1574 l_push_char (dtp, c);
1575 if (c != 'n' && c != 'N')
1576 goto unwind;
1577 c = next_char (dtp);
1578 l_push_char (dtp, c);
1581 if (!is_separator (c))
1582 goto unwind;
1584 if (dtp->u.p.namelist_mode)
1586 if (c == ' ' || c =='\n' || c == '\r')
1589 c = next_char (dtp);
1590 while (c == ' ' || c =='\n' || c == '\r');
1592 l_push_char (dtp, c);
1594 if (c == '=')
1595 goto unwind;
1599 if (is_inf)
1601 push_char (dtp, 'i');
1602 push_char (dtp, 'n');
1603 push_char (dtp, 'f');
1605 else
1607 push_char (dtp, 'n');
1608 push_char (dtp, 'a');
1609 push_char (dtp, 'n');
1612 free_line (dtp);
1613 goto done;
1615 unwind:
1616 if (dtp->u.p.namelist_mode)
1618 dtp->u.p.nml_read_error = 1;
1619 dtp->u.p.line_buffer_enabled = 1;
1620 dtp->u.p.item_count = 0;
1621 return;
1624 bad_real:
1626 if (nml_bad_return (dtp, c))
1627 return;
1629 eat_line (dtp);
1630 free_saved (dtp);
1631 sprintf (message, "Bad real number in item %d of list input",
1632 dtp->u.p.item_count);
1633 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1637 /* Check the current type against the saved type to make sure they are
1638 compatible. Returns nonzero if incompatible. */
1640 static int
1641 check_type (st_parameter_dt *dtp, bt type, int len)
1643 char message[100];
1645 if (dtp->u.p.saved_type != BT_NULL && dtp->u.p.saved_type != type)
1647 sprintf (message, "Read type %s where %s was expected for item %d",
1648 type_name (dtp->u.p.saved_type), type_name (type),
1649 dtp->u.p.item_count);
1651 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1652 return 1;
1655 if (dtp->u.p.saved_type == BT_NULL || dtp->u.p.saved_type == BT_CHARACTER)
1656 return 0;
1658 if (dtp->u.p.saved_length != len)
1660 sprintf (message,
1661 "Read kind %d %s where kind %d is required for item %d",
1662 dtp->u.p.saved_length, type_name (dtp->u.p.saved_type), len,
1663 dtp->u.p.item_count);
1664 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1665 return 1;
1668 return 0;
1672 /* Top level data transfer subroutine for list reads. Because we have
1673 to deal with repeat counts, the data item is always saved after
1674 reading, usually in the dtp->u.p.value[] array. If a repeat count is
1675 greater than one, we copy the data item multiple times. */
1677 static void
1678 list_formatted_read_scalar (st_parameter_dt *dtp, volatile bt type, void *p,
1679 int kind, size_t size)
1681 char c;
1682 gfc_char4_t *q;
1683 int i, m;
1684 jmp_buf eof_jump;
1686 dtp->u.p.namelist_mode = 0;
1688 dtp->u.p.eof_jump = &eof_jump;
1689 if (setjmp (eof_jump))
1691 generate_error (&dtp->common, LIBERROR_END, NULL);
1692 if (!is_internal_unit (dtp))
1694 dtp->u.p.current_unit->endfile = AFTER_ENDFILE;
1695 dtp->u.p.current_unit->current_record = 0;
1697 goto cleanup;
1700 if (dtp->u.p.first_item)
1702 dtp->u.p.first_item = 0;
1703 dtp->u.p.input_complete = 0;
1704 dtp->u.p.repeat_count = 1;
1705 dtp->u.p.at_eol = 0;
1707 c = eat_spaces (dtp);
1708 if (is_separator (c))
1710 /* Found a null value. */
1711 eat_separator (dtp);
1712 dtp->u.p.repeat_count = 0;
1714 /* eat_separator sets this flag if the separator was a comma. */
1715 if (dtp->u.p.comma_flag)
1716 goto cleanup;
1718 /* eat_separator sets this flag if the separator was a \n or \r. */
1719 if (dtp->u.p.at_eol)
1720 finish_separator (dtp);
1721 else
1722 goto cleanup;
1726 else
1728 if (dtp->u.p.repeat_count > 0)
1730 if (check_type (dtp, type, kind))
1731 return;
1732 goto set_value;
1735 if (dtp->u.p.input_complete)
1736 goto cleanup;
1738 if (dtp->u.p.input_complete)
1739 goto cleanup;
1741 if (dtp->u.p.at_eol)
1742 finish_separator (dtp);
1743 else
1745 eat_spaces (dtp);
1746 /* Trailing spaces prior to end of line. */
1747 if (dtp->u.p.at_eol)
1748 finish_separator (dtp);
1751 dtp->u.p.saved_type = BT_NULL;
1752 dtp->u.p.repeat_count = 1;
1755 switch (type)
1757 case BT_INTEGER:
1758 read_integer (dtp, kind);
1759 break;
1760 case BT_LOGICAL:
1761 read_logical (dtp, kind);
1762 break;
1763 case BT_CHARACTER:
1764 read_character (dtp, kind);
1765 break;
1766 case BT_REAL:
1767 read_real (dtp, p, kind);
1768 /* Copy value back to temporary if needed. */
1769 if (dtp->u.p.repeat_count > 0)
1770 memcpy (dtp->u.p.value, p, kind);
1771 break;
1772 case BT_COMPLEX:
1773 read_complex (dtp, p, kind, size);
1774 /* Copy value back to temporary if needed. */
1775 if (dtp->u.p.repeat_count > 0)
1776 memcpy (dtp->u.p.value, p, size);
1777 break;
1778 default:
1779 internal_error (&dtp->common, "Bad type for list read");
1782 if (dtp->u.p.saved_type != BT_CHARACTER && dtp->u.p.saved_type != BT_NULL)
1783 dtp->u.p.saved_length = size;
1785 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1786 goto cleanup;
1788 set_value:
1789 switch (dtp->u.p.saved_type)
1791 case BT_COMPLEX:
1792 case BT_REAL:
1793 if (dtp->u.p.repeat_count > 0)
1794 memcpy (p, dtp->u.p.value, size);
1795 break;
1797 case BT_INTEGER:
1798 case BT_LOGICAL:
1799 memcpy (p, dtp->u.p.value, size);
1800 break;
1802 case BT_CHARACTER:
1803 if (dtp->u.p.saved_string)
1805 m = ((int) size < dtp->u.p.saved_used)
1806 ? (int) size : dtp->u.p.saved_used;
1807 if (kind == 1)
1808 memcpy (p, dtp->u.p.saved_string, m);
1809 else
1811 q = (gfc_char4_t *) p;
1812 for (i = 0; i < m; i++)
1813 q[i] = (unsigned char) dtp->u.p.saved_string[i];
1816 else
1817 /* Just delimiters encountered, nothing to copy but SPACE. */
1818 m = 0;
1820 if (m < (int) size)
1822 if (kind == 1)
1823 memset (((char *) p) + m, ' ', size - m);
1824 else
1826 q = (gfc_char4_t *) p;
1827 for (i = m; i < (int) size; i++)
1828 q[i] = (unsigned char) ' ';
1831 break;
1833 case BT_NULL:
1834 break;
1837 if (--dtp->u.p.repeat_count <= 0)
1838 free_saved (dtp);
1840 cleanup:
1841 dtp->u.p.eof_jump = NULL;
1845 void
1846 list_formatted_read (st_parameter_dt *dtp, bt type, void *p, int kind,
1847 size_t size, size_t nelems)
1849 size_t elem;
1850 char *tmp;
1851 size_t stride = type == BT_CHARACTER ?
1852 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
1854 tmp = (char *) p;
1856 /* Big loop over all the elements. */
1857 for (elem = 0; elem < nelems; elem++)
1859 dtp->u.p.item_count++;
1860 list_formatted_read_scalar (dtp, type, tmp + stride*elem, kind, size);
1865 /* Finish a list read. */
1867 void
1868 finish_list_read (st_parameter_dt *dtp)
1870 char c;
1872 free_saved (dtp);
1874 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
1876 if (dtp->u.p.at_eol)
1878 dtp->u.p.at_eol = 0;
1879 return;
1884 c = next_char (dtp);
1886 while (c != '\n');
1888 if (dtp->u.p.current_unit->endfile != NO_ENDFILE)
1890 generate_error (&dtp->common, LIBERROR_END, NULL);
1891 dtp->u.p.current_unit->endfile = AFTER_ENDFILE;
1892 dtp->u.p.current_unit->current_record = 0;
1896 /* NAMELIST INPUT
1898 void namelist_read (st_parameter_dt *dtp)
1899 calls:
1900 static void nml_match_name (char *name, int len)
1901 static int nml_query (st_parameter_dt *dtp)
1902 static int nml_get_obj_data (st_parameter_dt *dtp,
1903 namelist_info **prev_nl, char *, size_t)
1904 calls:
1905 static void nml_untouch_nodes (st_parameter_dt *dtp)
1906 static namelist_info * find_nml_node (st_parameter_dt *dtp,
1907 char * var_name)
1908 static int nml_parse_qualifier(descriptor_dimension * ad,
1909 array_loop_spec * ls, int rank, char *)
1910 static void nml_touch_nodes (namelist_info * nl)
1911 static int nml_read_obj (namelist_info *nl, index_type offset,
1912 namelist_info **prev_nl, char *, size_t,
1913 index_type clow, index_type chigh)
1914 calls:
1915 -itself- */
1917 /* Inputs a rank-dimensional qualifier, which can contain
1918 singlets, doublets, triplets or ':' with the standard meanings. */
1920 static try
1921 nml_parse_qualifier (st_parameter_dt *dtp, descriptor_dimension *ad,
1922 array_loop_spec *ls, int rank, char *parse_err_msg,
1923 int *parsed_rank)
1925 int dim;
1926 int indx;
1927 int neg;
1928 int null_flag;
1929 int is_array_section, is_char;
1930 char c;
1932 is_char = 0;
1933 is_array_section = 0;
1934 dtp->u.p.expanded_read = 0;
1936 /* See if this is a character substring qualifier we are looking for. */
1937 if (rank == -1)
1939 rank = 1;
1940 is_char = 1;
1943 /* The next character in the stream should be the '('. */
1945 c = next_char (dtp);
1947 /* Process the qualifier, by dimension and triplet. */
1949 for (dim=0; dim < rank; dim++ )
1951 for (indx=0; indx<3; indx++)
1953 free_saved (dtp);
1954 eat_spaces (dtp);
1955 neg = 0;
1957 /* Process a potential sign. */
1958 c = next_char (dtp);
1959 switch (c)
1961 case '-':
1962 neg = 1;
1963 break;
1965 case '+':
1966 break;
1968 default:
1969 unget_char (dtp, c);
1970 break;
1973 /* Process characters up to the next ':' , ',' or ')'. */
1974 for (;;)
1976 c = next_char (dtp);
1978 switch (c)
1980 case ':':
1981 is_array_section = 1;
1982 break;
1984 case ',': case ')':
1985 if ((c==',' && dim == rank -1)
1986 || (c==')' && dim < rank -1))
1988 if (is_char)
1989 sprintf (parse_err_msg, "Bad substring qualifier");
1990 else
1991 sprintf (parse_err_msg, "Bad number of index fields");
1992 goto err_ret;
1994 break;
1996 CASE_DIGITS:
1997 push_char (dtp, c);
1998 continue;
2000 case ' ': case '\t':
2001 eat_spaces (dtp);
2002 c = next_char (dtp);
2003 break;
2005 default:
2006 if (is_char)
2007 sprintf (parse_err_msg,
2008 "Bad character in substring qualifier");
2009 else
2010 sprintf (parse_err_msg, "Bad character in index");
2011 goto err_ret;
2014 if ((c == ',' || c == ')') && indx == 0
2015 && dtp->u.p.saved_string == 0)
2017 if (is_char)
2018 sprintf (parse_err_msg, "Null substring qualifier");
2019 else
2020 sprintf (parse_err_msg, "Null index field");
2021 goto err_ret;
2024 if ((c == ':' && indx == 1 && dtp->u.p.saved_string == 0)
2025 || (indx == 2 && dtp->u.p.saved_string == 0))
2027 if (is_char)
2028 sprintf (parse_err_msg, "Bad substring qualifier");
2029 else
2030 sprintf (parse_err_msg, "Bad index triplet");
2031 goto err_ret;
2034 if (is_char && !is_array_section)
2036 sprintf (parse_err_msg,
2037 "Missing colon in substring qualifier");
2038 goto err_ret;
2041 /* If '( : ? )' or '( ? : )' break and flag read failure. */
2042 null_flag = 0;
2043 if ((c == ':' && indx == 0 && dtp->u.p.saved_string == 0)
2044 || (indx==1 && dtp->u.p.saved_string == 0))
2046 null_flag = 1;
2047 break;
2050 /* Now read the index. */
2051 if (convert_integer (dtp, sizeof(ssize_t), neg))
2053 if (is_char)
2054 sprintf (parse_err_msg, "Bad integer substring qualifier");
2055 else
2056 sprintf (parse_err_msg, "Bad integer in index");
2057 goto err_ret;
2059 break;
2062 /* Feed the index values to the triplet arrays. */
2063 if (!null_flag)
2065 if (indx == 0)
2066 memcpy (&ls[dim].start, dtp->u.p.value, sizeof(ssize_t));
2067 if (indx == 1)
2068 memcpy (&ls[dim].end, dtp->u.p.value, sizeof(ssize_t));
2069 if (indx == 2)
2070 memcpy (&ls[dim].step, dtp->u.p.value, sizeof(ssize_t));
2073 /* Singlet or doublet indices. */
2074 if (c==',' || c==')')
2076 if (indx == 0)
2078 memcpy (&ls[dim].start, dtp->u.p.value, sizeof(ssize_t));
2080 /* If -std=f95/2003 or an array section is specified,
2081 do not allow excess data to be processed. */
2082 if (is_array_section == 1
2083 || compile_options.allow_std < GFC_STD_GNU)
2084 ls[dim].end = ls[dim].start;
2085 else
2086 dtp->u.p.expanded_read = 1;
2089 /* Check for non-zero rank. */
2090 if (is_array_section == 1 && ls[dim].start != ls[dim].end)
2091 *parsed_rank = 1;
2093 break;
2097 /* Check the values of the triplet indices. */
2098 if ((ls[dim].start > (ssize_t) GFC_DIMENSION_UBOUND(ad[dim]))
2099 || (ls[dim].start < (ssize_t) GFC_DIMENSION_LBOUND(ad[dim]))
2100 || (ls[dim].end > (ssize_t) GFC_DIMENSION_UBOUND(ad[dim]))
2101 || (ls[dim].end < (ssize_t) GFC_DIMENSION_LBOUND(ad[dim])))
2103 if (is_char)
2104 sprintf (parse_err_msg, "Substring out of range");
2105 else
2106 sprintf (parse_err_msg, "Index %d out of range", dim + 1);
2107 goto err_ret;
2110 if (((ls[dim].end - ls[dim].start ) * ls[dim].step < 0)
2111 || (ls[dim].step == 0))
2113 sprintf (parse_err_msg, "Bad range in index %d", dim + 1);
2114 goto err_ret;
2117 /* Initialise the loop index counter. */
2118 ls[dim].idx = ls[dim].start;
2120 eat_spaces (dtp);
2121 return SUCCESS;
2123 err_ret:
2125 return FAILURE;
2128 static namelist_info *
2129 find_nml_node (st_parameter_dt *dtp, char * var_name)
2131 namelist_info * t = dtp->u.p.ionml;
2132 while (t != NULL)
2134 if (strcmp (var_name, t->var_name) == 0)
2136 t->touched = 1;
2137 return t;
2139 t = t->next;
2141 return NULL;
2144 /* Visits all the components of a derived type that have
2145 not explicitly been identified in the namelist input.
2146 touched is set and the loop specification initialised
2147 to default values */
2149 static void
2150 nml_touch_nodes (namelist_info * nl)
2152 index_type len = strlen (nl->var_name) + 1;
2153 int dim;
2154 char * ext_name = (char*)get_mem (len + 1);
2155 memcpy (ext_name, nl->var_name, len-1);
2156 memcpy (ext_name + len - 1, "%", 2);
2157 for (nl = nl->next; nl; nl = nl->next)
2159 if (strncmp (nl->var_name, ext_name, len) == 0)
2161 nl->touched = 1;
2162 for (dim=0; dim < nl->var_rank; dim++)
2164 nl->ls[dim].step = 1;
2165 nl->ls[dim].end = GFC_DESCRIPTOR_UBOUND(nl,dim);
2166 nl->ls[dim].start = GFC_DESCRIPTOR_LBOUND(nl,dim);
2167 nl->ls[dim].idx = nl->ls[dim].start;
2170 else
2171 break;
2173 free_mem (ext_name);
2174 return;
2177 /* Resets touched for the entire list of nml_nodes, ready for a
2178 new object. */
2180 static void
2181 nml_untouch_nodes (st_parameter_dt *dtp)
2183 namelist_info * t;
2184 for (t = dtp->u.p.ionml; t; t = t->next)
2185 t->touched = 0;
2186 return;
2189 /* Attempts to input name to namelist name. Returns
2190 dtp->u.p.nml_read_error = 1 on no match. */
2192 static void
2193 nml_match_name (st_parameter_dt *dtp, const char *name, index_type len)
2195 index_type i;
2196 char c;
2197 dtp->u.p.nml_read_error = 0;
2198 for (i = 0; i < len; i++)
2200 c = next_char (dtp);
2201 if (tolower (c) != tolower (name[i]))
2203 dtp->u.p.nml_read_error = 1;
2204 break;
2209 /* If the namelist read is from stdin, output the current state of the
2210 namelist to stdout. This is used to implement the non-standard query
2211 features, ? and =?. If c == '=' the full namelist is printed. Otherwise
2212 the names alone are printed. */
2214 static void
2215 nml_query (st_parameter_dt *dtp, char c)
2217 gfc_unit * temp_unit;
2218 namelist_info * nl;
2219 index_type len;
2220 char * p;
2221 #ifdef HAVE_CRLF
2222 static const index_type endlen = 3;
2223 static const char endl[] = "\r\n";
2224 static const char nmlend[] = "&end\r\n";
2225 #else
2226 static const index_type endlen = 2;
2227 static const char endl[] = "\n";
2228 static const char nmlend[] = "&end\n";
2229 #endif
2231 if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
2232 return;
2234 /* Store the current unit and transfer to stdout. */
2236 temp_unit = dtp->u.p.current_unit;
2237 dtp->u.p.current_unit = find_unit (options.stdout_unit);
2239 if (dtp->u.p.current_unit)
2241 dtp->u.p.mode = WRITING;
2242 next_record (dtp, 0);
2244 /* Write the namelist in its entirety. */
2246 if (c == '=')
2247 namelist_write (dtp);
2249 /* Or write the list of names. */
2251 else
2253 /* "&namelist_name\n" */
2255 len = dtp->namelist_name_len;
2256 p = write_block (dtp, len + endlen);
2257 if (!p)
2258 goto query_return;
2259 memcpy (p, "&", 1);
2260 memcpy ((char*)(p + 1), dtp->namelist_name, len);
2261 memcpy ((char*)(p + len + 1), &endl, endlen - 1);
2262 for (nl = dtp->u.p.ionml; nl; nl = nl->next)
2264 /* " var_name\n" */
2266 len = strlen (nl->var_name);
2267 p = write_block (dtp, len + endlen);
2268 if (!p)
2269 goto query_return;
2270 memcpy (p, " ", 1);
2271 memcpy ((char*)(p + 1), nl->var_name, len);
2272 memcpy ((char*)(p + len + 1), &endl, endlen - 1);
2275 /* "&end\n" */
2277 p = write_block (dtp, endlen + 3);
2278 goto query_return;
2279 memcpy (p, &nmlend, endlen + 3);
2282 /* Flush the stream to force immediate output. */
2284 fbuf_flush (dtp->u.p.current_unit, WRITING);
2285 sflush (dtp->u.p.current_unit->s);
2286 unlock_unit (dtp->u.p.current_unit);
2289 query_return:
2291 /* Restore the current unit. */
2293 dtp->u.p.current_unit = temp_unit;
2294 dtp->u.p.mode = READING;
2295 return;
2298 /* Reads and stores the input for the namelist object nl. For an array,
2299 the function loops over the ranges defined by the loop specification.
2300 This default to all the data or to the specification from a qualifier.
2301 nml_read_obj recursively calls itself to read derived types. It visits
2302 all its own components but only reads data for those that were touched
2303 when the name was parsed. If a read error is encountered, an attempt is
2304 made to return to read a new object name because the standard allows too
2305 little data to be available. On the other hand, too much data is an
2306 error. */
2308 static try
2309 nml_read_obj (st_parameter_dt *dtp, namelist_info * nl, index_type offset,
2310 namelist_info **pprev_nl, char *nml_err_msg,
2311 size_t nml_err_msg_size, index_type clow, index_type chigh)
2313 namelist_info * cmp;
2314 char * obj_name;
2315 int nml_carry;
2316 int len;
2317 int dim;
2318 index_type dlen;
2319 index_type m;
2320 size_t obj_name_len;
2321 void * pdata;
2323 /* This object not touched in name parsing. */
2325 if (!nl->touched)
2326 return SUCCESS;
2328 dtp->u.p.repeat_count = 0;
2329 eat_spaces (dtp);
2331 len = nl->len;
2332 switch (nl->type)
2334 case GFC_DTYPE_INTEGER:
2335 case GFC_DTYPE_LOGICAL:
2336 dlen = len;
2337 break;
2339 case GFC_DTYPE_REAL:
2340 dlen = size_from_real_kind (len);
2341 break;
2343 case GFC_DTYPE_COMPLEX:
2344 dlen = size_from_complex_kind (len);
2345 break;
2347 case GFC_DTYPE_CHARACTER:
2348 dlen = chigh ? (chigh - clow + 1) : nl->string_length;
2349 break;
2351 default:
2352 dlen = 0;
2357 /* Update the pointer to the data, using the current index vector */
2359 pdata = (void*)(nl->mem_pos + offset);
2360 for (dim = 0; dim < nl->var_rank; dim++)
2361 pdata = (void*)(pdata + (nl->ls[dim].idx
2362 - GFC_DESCRIPTOR_LBOUND(nl,dim))
2363 * GFC_DESCRIPTOR_STRIDE(nl,dim) * nl->size);
2365 /* Reset the error flag and try to read next value, if
2366 dtp->u.p.repeat_count=0 */
2368 dtp->u.p.nml_read_error = 0;
2369 nml_carry = 0;
2370 if (--dtp->u.p.repeat_count <= 0)
2372 if (dtp->u.p.input_complete)
2373 return SUCCESS;
2374 if (dtp->u.p.at_eol)
2375 finish_separator (dtp);
2376 if (dtp->u.p.input_complete)
2377 return SUCCESS;
2379 /* BT_NULL (equivalent to GFC_DTYPE_UNKNOWN) falls through
2380 for nulls and is detected at default: of switch block. */
2382 dtp->u.p.saved_type = BT_NULL;
2383 free_saved (dtp);
2385 switch (nl->type)
2387 case GFC_DTYPE_INTEGER:
2388 read_integer (dtp, len);
2389 break;
2391 case GFC_DTYPE_LOGICAL:
2392 read_logical (dtp, len);
2393 break;
2395 case GFC_DTYPE_CHARACTER:
2396 read_character (dtp, len);
2397 break;
2399 case GFC_DTYPE_REAL:
2400 /* Need to copy data back from the real location to the temp in order
2401 to handle nml reads into arrays. */
2402 read_real (dtp, pdata, len);
2403 memcpy (dtp->u.p.value, pdata, dlen);
2404 break;
2406 case GFC_DTYPE_COMPLEX:
2407 /* Same as for REAL, copy back to temp. */
2408 read_complex (dtp, pdata, len, dlen);
2409 memcpy (dtp->u.p.value, pdata, dlen);
2410 break;
2412 case GFC_DTYPE_DERIVED:
2413 obj_name_len = strlen (nl->var_name) + 1;
2414 obj_name = get_mem (obj_name_len+1);
2415 memcpy (obj_name, nl->var_name, obj_name_len-1);
2416 memcpy (obj_name + obj_name_len - 1, "%", 2);
2418 /* If reading a derived type, disable the expanded read warning
2419 since a single object can have multiple reads. */
2420 dtp->u.p.expanded_read = 0;
2422 /* Now loop over the components. Update the component pointer
2423 with the return value from nml_write_obj. This loop jumps
2424 past nested derived types by testing if the potential
2425 component name contains '%'. */
2427 for (cmp = nl->next;
2428 cmp &&
2429 !strncmp (cmp->var_name, obj_name, obj_name_len) &&
2430 !strchr (cmp->var_name + obj_name_len, '%');
2431 cmp = cmp->next)
2434 if (nml_read_obj (dtp, cmp, (index_type)(pdata - nl->mem_pos),
2435 pprev_nl, nml_err_msg, nml_err_msg_size,
2436 clow, chigh) == FAILURE)
2438 free_mem (obj_name);
2439 return FAILURE;
2442 if (dtp->u.p.input_complete)
2444 free_mem (obj_name);
2445 return SUCCESS;
2449 free_mem (obj_name);
2450 goto incr_idx;
2452 default:
2453 snprintf (nml_err_msg, nml_err_msg_size,
2454 "Bad type for namelist object %s", nl->var_name);
2455 internal_error (&dtp->common, nml_err_msg);
2456 goto nml_err_ret;
2460 /* The standard permits array data to stop short of the number of
2461 elements specified in the loop specification. In this case, we
2462 should be here with dtp->u.p.nml_read_error != 0. Control returns to
2463 nml_get_obj_data and an attempt is made to read object name. */
2465 *pprev_nl = nl;
2466 if (dtp->u.p.nml_read_error)
2468 dtp->u.p.expanded_read = 0;
2469 return SUCCESS;
2472 if (dtp->u.p.saved_type == BT_NULL)
2474 dtp->u.p.expanded_read = 0;
2475 goto incr_idx;
2478 /* Note the switch from GFC_DTYPE_type to BT_type at this point.
2479 This comes about because the read functions return BT_types. */
2481 switch (dtp->u.p.saved_type)
2484 case BT_COMPLEX:
2485 case BT_REAL:
2486 case BT_INTEGER:
2487 case BT_LOGICAL:
2488 memcpy (pdata, dtp->u.p.value, dlen);
2489 break;
2491 case BT_CHARACTER:
2492 m = (dlen < dtp->u.p.saved_used) ? dlen : dtp->u.p.saved_used;
2493 pdata = (void*)( pdata + clow - 1 );
2494 memcpy (pdata, dtp->u.p.saved_string, m);
2495 if (m < dlen)
2496 memset ((void*)( pdata + m ), ' ', dlen - m);
2497 break;
2499 default:
2500 break;
2503 /* Warn if a non-standard expanded read occurs. A single read of a
2504 single object is acceptable. If a second read occurs, issue a warning
2505 and set the flag to zero to prevent further warnings. */
2506 if (dtp->u.p.expanded_read == 2)
2508 notify_std (&dtp->common, GFC_STD_GNU, "Non-standard expanded namelist read.");
2509 dtp->u.p.expanded_read = 0;
2512 /* If the expanded read warning flag is set, increment it,
2513 indicating that a single read has occurred. */
2514 if (dtp->u.p.expanded_read >= 1)
2515 dtp->u.p.expanded_read++;
2517 /* Break out of loop if scalar. */
2518 if (!nl->var_rank)
2519 break;
2521 /* Now increment the index vector. */
2523 incr_idx:
2525 nml_carry = 1;
2526 for (dim = 0; dim < nl->var_rank; dim++)
2528 nl->ls[dim].idx += nml_carry * nl->ls[dim].step;
2529 nml_carry = 0;
2530 if (((nl->ls[dim].step > 0) && (nl->ls[dim].idx > nl->ls[dim].end))
2532 ((nl->ls[dim].step < 0) && (nl->ls[dim].idx < nl->ls[dim].end)))
2534 nl->ls[dim].idx = nl->ls[dim].start;
2535 nml_carry = 1;
2538 } while (!nml_carry);
2540 if (dtp->u.p.repeat_count > 1)
2542 snprintf (nml_err_msg, nml_err_msg_size,
2543 "Repeat count too large for namelist object %s", nl->var_name);
2544 goto nml_err_ret;
2546 return SUCCESS;
2548 nml_err_ret:
2550 return FAILURE;
2553 /* Parses the object name, including array and substring qualifiers. It
2554 iterates over derived type components, touching those components and
2555 setting their loop specifications, if there is a qualifier. If the
2556 object is itself a derived type, its components and subcomponents are
2557 touched. nml_read_obj is called at the end and this reads the data in
2558 the manner specified by the object name. */
2560 static try
2561 nml_get_obj_data (st_parameter_dt *dtp, namelist_info **pprev_nl,
2562 char *nml_err_msg, size_t nml_err_msg_size)
2564 char c;
2565 namelist_info * nl;
2566 namelist_info * first_nl = NULL;
2567 namelist_info * root_nl = NULL;
2568 int dim, parsed_rank;
2569 int component_flag;
2570 index_type clow, chigh;
2571 int non_zero_rank_count;
2573 /* Look for end of input or object name. If '?' or '=?' are encountered
2574 in stdin, print the node names or the namelist to stdout. */
2576 eat_separator (dtp);
2577 if (dtp->u.p.input_complete)
2578 return SUCCESS;
2580 if (dtp->u.p.at_eol)
2581 finish_separator (dtp);
2582 if (dtp->u.p.input_complete)
2583 return SUCCESS;
2585 c = next_char (dtp);
2586 switch (c)
2588 case '=':
2589 c = next_char (dtp);
2590 if (c != '?')
2592 sprintf (nml_err_msg, "namelist read: misplaced = sign");
2593 goto nml_err_ret;
2595 nml_query (dtp, '=');
2596 return SUCCESS;
2598 case '?':
2599 nml_query (dtp, '?');
2600 return SUCCESS;
2602 case '$':
2603 case '&':
2604 nml_match_name (dtp, "end", 3);
2605 if (dtp->u.p.nml_read_error)
2607 sprintf (nml_err_msg, "namelist not terminated with / or &end");
2608 goto nml_err_ret;
2610 case '/':
2611 dtp->u.p.input_complete = 1;
2612 return SUCCESS;
2614 default :
2615 break;
2618 /* Untouch all nodes of the namelist and reset the flag that is set for
2619 derived type components. */
2621 nml_untouch_nodes (dtp);
2622 component_flag = 0;
2623 non_zero_rank_count = 0;
2625 /* Get the object name - should '!' and '\n' be permitted separators? */
2627 get_name:
2629 free_saved (dtp);
2633 if (!is_separator (c))
2634 push_char (dtp, tolower(c));
2635 c = next_char (dtp);
2636 } while (!( c=='=' || c==' ' || c=='\t' || c =='(' || c =='%' ));
2638 unget_char (dtp, c);
2640 /* Check that the name is in the namelist and get pointer to object.
2641 Three error conditions exist: (i) An attempt is being made to
2642 identify a non-existent object, following a failed data read or
2643 (ii) The object name does not exist or (iii) Too many data items
2644 are present for an object. (iii) gives the same error message
2645 as (i) */
2647 push_char (dtp, '\0');
2649 if (component_flag)
2651 size_t var_len = strlen (root_nl->var_name);
2652 size_t saved_len
2653 = dtp->u.p.saved_string ? strlen (dtp->u.p.saved_string) : 0;
2654 char ext_name[var_len + saved_len + 1];
2656 memcpy (ext_name, root_nl->var_name, var_len);
2657 if (dtp->u.p.saved_string)
2658 memcpy (ext_name + var_len, dtp->u.p.saved_string, saved_len);
2659 ext_name[var_len + saved_len] = '\0';
2660 nl = find_nml_node (dtp, ext_name);
2662 else
2663 nl = find_nml_node (dtp, dtp->u.p.saved_string);
2665 if (nl == NULL)
2667 if (dtp->u.p.nml_read_error && *pprev_nl)
2668 snprintf (nml_err_msg, nml_err_msg_size,
2669 "Bad data for namelist object %s", (*pprev_nl)->var_name);
2671 else
2672 snprintf (nml_err_msg, nml_err_msg_size,
2673 "Cannot match namelist object name %s",
2674 dtp->u.p.saved_string);
2676 goto nml_err_ret;
2679 /* Get the length, data length, base pointer and rank of the variable.
2680 Set the default loop specification first. */
2682 for (dim=0; dim < nl->var_rank; dim++)
2684 nl->ls[dim].step = 1;
2685 nl->ls[dim].end = GFC_DESCRIPTOR_UBOUND(nl,dim);
2686 nl->ls[dim].start = GFC_DESCRIPTOR_LBOUND(nl,dim);
2687 nl->ls[dim].idx = nl->ls[dim].start;
2690 /* Check to see if there is a qualifier: if so, parse it.*/
2692 if (c == '(' && nl->var_rank)
2694 parsed_rank = 0;
2695 if (nml_parse_qualifier (dtp, nl->dim, nl->ls, nl->var_rank,
2696 nml_err_msg, &parsed_rank) == FAILURE)
2698 char *nml_err_msg_end = strchr (nml_err_msg, '\0');
2699 snprintf (nml_err_msg_end,
2700 nml_err_msg_size - (nml_err_msg_end - nml_err_msg),
2701 " for namelist variable %s", nl->var_name);
2702 goto nml_err_ret;
2705 if (parsed_rank > 0)
2706 non_zero_rank_count++;
2708 c = next_char (dtp);
2709 unget_char (dtp, c);
2711 else if (nl->var_rank > 0)
2712 non_zero_rank_count++;
2714 /* Now parse a derived type component. The root namelist_info address
2715 is backed up, as is the previous component level. The component flag
2716 is set and the iteration is made by jumping back to get_name. */
2718 if (c == '%')
2720 if (nl->type != GFC_DTYPE_DERIVED)
2722 snprintf (nml_err_msg, nml_err_msg_size,
2723 "Attempt to get derived component for %s", nl->var_name);
2724 goto nml_err_ret;
2727 if (!component_flag)
2728 first_nl = nl;
2730 root_nl = nl;
2731 component_flag = 1;
2732 c = next_char (dtp);
2733 goto get_name;
2736 /* Parse a character qualifier, if present. chigh = 0 is a default
2737 that signals that the string length = string_length. */
2739 clow = 1;
2740 chigh = 0;
2742 if (c == '(' && nl->type == GFC_DTYPE_CHARACTER)
2744 descriptor_dimension chd[1] = { {1, clow, nl->string_length} };
2745 array_loop_spec ind[1] = { {1, clow, nl->string_length, 1} };
2747 if (nml_parse_qualifier (dtp, chd, ind, -1, nml_err_msg, &parsed_rank)
2748 == FAILURE)
2750 char *nml_err_msg_end = strchr (nml_err_msg, '\0');
2751 snprintf (nml_err_msg_end,
2752 nml_err_msg_size - (nml_err_msg_end - nml_err_msg),
2753 " for namelist variable %s", nl->var_name);
2754 goto nml_err_ret;
2757 clow = ind[0].start;
2758 chigh = ind[0].end;
2760 if (ind[0].step != 1)
2762 snprintf (nml_err_msg, nml_err_msg_size,
2763 "Step not allowed in substring qualifier"
2764 " for namelist object %s", nl->var_name);
2765 goto nml_err_ret;
2768 c = next_char (dtp);
2769 unget_char (dtp, c);
2772 /* If a derived type touch its components and restore the root
2773 namelist_info if we have parsed a qualified derived type
2774 component. */
2776 if (nl->type == GFC_DTYPE_DERIVED)
2777 nml_touch_nodes (nl);
2778 if (component_flag && nl->var_rank > 0 && nl->next)
2779 nl = first_nl;
2781 /* Make sure no extraneous qualifiers are there. */
2783 if (c == '(')
2785 snprintf (nml_err_msg, nml_err_msg_size,
2786 "Qualifier for a scalar or non-character namelist object %s",
2787 nl->var_name);
2788 goto nml_err_ret;
2791 /* Make sure there is no more than one non-zero rank object. */
2792 if (non_zero_rank_count > 1)
2794 snprintf (nml_err_msg, nml_err_msg_size,
2795 "Multiple sub-objects with non-zero rank in namelist object %s",
2796 nl->var_name);
2797 non_zero_rank_count = 0;
2798 goto nml_err_ret;
2801 /* According to the standard, an equal sign MUST follow an object name. The
2802 following is possibly lax - it allows comments, blank lines and so on to
2803 intervene. eat_spaces (dtp); c = next_char (dtp); would be compliant*/
2805 free_saved (dtp);
2807 eat_separator (dtp);
2808 if (dtp->u.p.input_complete)
2809 return SUCCESS;
2811 if (dtp->u.p.at_eol)
2812 finish_separator (dtp);
2813 if (dtp->u.p.input_complete)
2814 return SUCCESS;
2816 c = next_char (dtp);
2818 if (c != '=')
2820 snprintf (nml_err_msg, nml_err_msg_size,
2821 "Equal sign must follow namelist object name %s",
2822 nl->var_name);
2823 goto nml_err_ret;
2826 if (first_nl != NULL && first_nl->var_rank > 0)
2827 nl = first_nl;
2829 if (nml_read_obj (dtp, nl, 0, pprev_nl, nml_err_msg, nml_err_msg_size,
2830 clow, chigh) == FAILURE)
2831 goto nml_err_ret;
2833 return SUCCESS;
2835 nml_err_ret:
2837 return FAILURE;
2840 /* Entry point for namelist input. Goes through input until namelist name
2841 is matched. Then cycles through nml_get_obj_data until the input is
2842 completed or there is an error. */
2844 void
2845 namelist_read (st_parameter_dt *dtp)
2847 char c;
2848 jmp_buf eof_jump;
2849 char nml_err_msg[200];
2850 /* Pointer to the previously read object, in case attempt is made to read
2851 new object name. Should this fail, error message can give previous
2852 name. */
2853 namelist_info *prev_nl = NULL;
2855 dtp->u.p.namelist_mode = 1;
2856 dtp->u.p.input_complete = 0;
2857 dtp->u.p.expanded_read = 0;
2859 dtp->u.p.eof_jump = &eof_jump;
2860 if (setjmp (eof_jump))
2862 dtp->u.p.eof_jump = NULL;
2863 generate_error (&dtp->common, LIBERROR_END, NULL);
2864 return;
2867 /* Look for &namelist_name . Skip all characters, testing for $nmlname.
2868 Exit on success or EOF. If '?' or '=?' encountered in stdin, print
2869 node names or namelist on stdout. */
2871 find_nml_name:
2872 switch (c = next_char (dtp))
2874 case '$':
2875 case '&':
2876 break;
2878 case '!':
2879 eat_line (dtp);
2880 goto find_nml_name;
2882 case '=':
2883 c = next_char (dtp);
2884 if (c == '?')
2885 nml_query (dtp, '=');
2886 else
2887 unget_char (dtp, c);
2888 goto find_nml_name;
2890 case '?':
2891 nml_query (dtp, '?');
2893 default:
2894 goto find_nml_name;
2897 /* Match the name of the namelist. */
2899 nml_match_name (dtp, dtp->namelist_name, dtp->namelist_name_len);
2901 if (dtp->u.p.nml_read_error)
2902 goto find_nml_name;
2904 /* A trailing space is required, we give a little lattitude here, 10.9.1. */
2905 c = next_char (dtp);
2906 if (!is_separator(c) && c != '!')
2908 unget_char (dtp, c);
2909 goto find_nml_name;
2912 unget_char (dtp, c);
2913 eat_separator (dtp);
2915 /* Ready to read namelist objects. If there is an error in input
2916 from stdin, output the error message and continue. */
2918 while (!dtp->u.p.input_complete)
2920 if (nml_get_obj_data (dtp, &prev_nl, nml_err_msg, sizeof nml_err_msg)
2921 == FAILURE)
2923 gfc_unit *u;
2925 if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
2926 goto nml_err_ret;
2928 u = find_unit (options.stderr_unit);
2929 st_printf ("%s\n", nml_err_msg);
2930 if (u != NULL)
2932 sflush (u->s);
2933 unlock_unit (u);
2939 dtp->u.p.eof_jump = NULL;
2940 free_saved (dtp);
2941 free_line (dtp);
2942 return;
2944 /* All namelist error calls return from here */
2946 nml_err_ret:
2948 dtp->u.p.eof_jump = NULL;
2949 free_saved (dtp);
2950 free_line (dtp);
2951 generate_error (&dtp->common, LIBERROR_READ_VALUE, nml_err_msg);
2952 return;