* c-decl.c (locate_old_decl): If a previous conflicting decl is
[official-gcc.git] / libgfortran / io / list_read.c
blobd38a4a8d0b40bc17b3e29f4ec4d4946abe4fe9d5
1 /* Copyright (C) 2002-2013 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
3 Namelist input contributed by Paul Thomas
4 F2003 I/O support contributed by Jerry DeLisle
6 This file is part of the GNU Fortran runtime library (libgfortran).
8 Libgfortran is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 Libgfortran is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
28 #include "io.h"
29 #include "fbuf.h"
30 #include "unix.h"
31 #include <string.h>
32 #include <stdlib.h>
33 #include <ctype.h>
36 /* List directed input. Several parsing subroutines are practically
37 reimplemented from formatted input, the reason being that there are
38 all kinds of small differences between formatted and list directed
39 parsing. */
42 /* Subroutines for reading characters from the input. Because a
43 repeat count is ambiguous with an integer, we have to read the
44 whole digit string before seeing if there is a '*' which signals
45 the repeat count. Since we can have a lot of potential leading
46 zeros, we have to be able to back up by arbitrary amount. Because
47 the input might not be seekable, we have to buffer the data
48 ourselves. */
50 #define CASE_DIGITS case '0': case '1': case '2': case '3': case '4': \
51 case '5': case '6': case '7': case '8': case '9'
53 #define CASE_SEPARATORS case ' ': case ',': case '/': case '\n': case '\t': \
54 case '\r': case ';'
56 /* This macro assumes that we're operating on a variable. */
58 #define is_separator(c) (c == '/' || c == ',' || c == '\n' || c == ' ' \
59 || c == '\t' || c == '\r' || c == ';')
61 /* Maximum repeat count. Less than ten times the maximum signed int32. */
63 #define MAX_REPEAT 200000000
66 #define MSGLEN 100
68 /* Save a character to a string buffer, enlarging it as necessary. */
70 static void
71 push_char (st_parameter_dt *dtp, char c)
73 char *new;
75 if (dtp->u.p.saved_string == NULL)
77 // Plain malloc should suffice here, zeroing not needed?
78 dtp->u.p.saved_string = xcalloc (SCRATCH_SIZE, 1);
79 dtp->u.p.saved_length = SCRATCH_SIZE;
80 dtp->u.p.saved_used = 0;
83 if (dtp->u.p.saved_used >= dtp->u.p.saved_length)
85 dtp->u.p.saved_length = 2 * dtp->u.p.saved_length;
86 new = realloc (dtp->u.p.saved_string, dtp->u.p.saved_length);
87 if (new == NULL)
88 generate_error (&dtp->common, LIBERROR_OS, NULL);
89 dtp->u.p.saved_string = new;
91 // Also this should not be necessary.
92 memset (new + dtp->u.p.saved_used, 0,
93 dtp->u.p.saved_length - dtp->u.p.saved_used);
97 dtp->u.p.saved_string[dtp->u.p.saved_used++] = c;
101 /* Free the input buffer if necessary. */
103 static void
104 free_saved (st_parameter_dt *dtp)
106 if (dtp->u.p.saved_string == NULL)
107 return;
109 free (dtp->u.p.saved_string);
111 dtp->u.p.saved_string = NULL;
112 dtp->u.p.saved_used = 0;
116 /* Free the line buffer if necessary. */
118 static void
119 free_line (st_parameter_dt *dtp)
121 dtp->u.p.item_count = 0;
122 dtp->u.p.line_buffer_enabled = 0;
124 if (dtp->u.p.line_buffer == NULL)
125 return;
127 free (dtp->u.p.line_buffer);
128 dtp->u.p.line_buffer = NULL;
132 static int
133 next_char (st_parameter_dt *dtp)
135 ssize_t length;
136 gfc_offset record;
137 int c;
139 if (dtp->u.p.last_char != EOF - 1)
141 dtp->u.p.at_eol = 0;
142 c = dtp->u.p.last_char;
143 dtp->u.p.last_char = EOF - 1;
144 goto done;
147 /* Read from line_buffer if enabled. */
149 if (dtp->u.p.line_buffer_enabled)
151 dtp->u.p.at_eol = 0;
153 c = dtp->u.p.line_buffer[dtp->u.p.item_count];
154 if (c != '\0' && dtp->u.p.item_count < 64)
156 dtp->u.p.line_buffer[dtp->u.p.item_count] = '\0';
157 dtp->u.p.item_count++;
158 goto done;
161 dtp->u.p.item_count = 0;
162 dtp->u.p.line_buffer_enabled = 0;
165 /* Handle the end-of-record and end-of-file conditions for
166 internal array unit. */
167 if (is_array_io (dtp))
169 if (dtp->u.p.at_eof)
170 return EOF;
172 /* Check for "end-of-record" condition. */
173 if (dtp->u.p.current_unit->bytes_left == 0)
175 int finished;
177 c = '\n';
178 record = next_array_record (dtp, dtp->u.p.current_unit->ls,
179 &finished);
181 /* Check for "end-of-file" condition. */
182 if (finished)
184 dtp->u.p.at_eof = 1;
185 goto done;
188 record *= dtp->u.p.current_unit->recl;
189 if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
190 return EOF;
192 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
193 goto done;
197 /* Get the next character and handle end-of-record conditions. */
199 if (is_internal_unit (dtp))
201 /* Check for kind=4 internal unit. */
202 if (dtp->common.unit)
203 length = sread (dtp->u.p.current_unit->s, &c, sizeof (gfc_char4_t));
204 else
206 char cc;
207 length = sread (dtp->u.p.current_unit->s, &cc, 1);
208 c = cc;
211 if (length < 0)
213 generate_error (&dtp->common, LIBERROR_OS, NULL);
214 return '\0';
217 if (is_array_io (dtp))
219 /* Check whether we hit EOF. */
220 if (length == 0)
222 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
223 return '\0';
225 dtp->u.p.current_unit->bytes_left--;
227 else
229 if (dtp->u.p.at_eof)
230 return EOF;
231 if (length == 0)
233 c = '\n';
234 dtp->u.p.at_eof = 1;
238 else
240 c = fbuf_getc (dtp->u.p.current_unit);
241 if (c != EOF && is_stream_io (dtp))
242 dtp->u.p.current_unit->strm_pos++;
244 done:
245 dtp->u.p.at_eol = (c == '\n' || c == EOF);
246 return c;
250 /* Push a character back onto the input. */
252 static void
253 unget_char (st_parameter_dt *dtp, int c)
255 dtp->u.p.last_char = c;
259 /* Skip over spaces in the input. Returns the nonspace character that
260 terminated the eating and also places it back on the input. */
262 static int
263 eat_spaces (st_parameter_dt *dtp)
265 int c;
268 c = next_char (dtp);
269 while (c != EOF && (c == ' ' || c == '\t'));
271 unget_char (dtp, c);
272 return c;
276 /* This function reads characters through to the end of the current
277 line and just ignores them. Returns 0 for success and LIBERROR_END
278 if it hit EOF. */
280 static int
281 eat_line (st_parameter_dt *dtp)
283 int c;
286 c = next_char (dtp);
287 while (c != EOF && c != '\n');
288 if (c == EOF)
289 return LIBERROR_END;
290 return 0;
294 /* Skip over a separator. Technically, we don't always eat the whole
295 separator. This is because if we've processed the last input item,
296 then a separator is unnecessary. Plus the fact that operating
297 systems usually deliver console input on a line basis.
299 The upshot is that if we see a newline as part of reading a
300 separator, we stop reading. If there are more input items, we
301 continue reading the separator with finish_separator() which takes
302 care of the fact that we may or may not have seen a comma as part
303 of the separator.
305 Returns 0 for success, and non-zero error code otherwise. */
307 static int
308 eat_separator (st_parameter_dt *dtp)
310 int c, n;
311 int err = 0;
313 eat_spaces (dtp);
314 dtp->u.p.comma_flag = 0;
316 if ((c = next_char (dtp)) == EOF)
317 return LIBERROR_END;
318 switch (c)
320 case ',':
321 if (dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
323 unget_char (dtp, c);
324 break;
326 /* Fall through. */
327 case ';':
328 dtp->u.p.comma_flag = 1;
329 eat_spaces (dtp);
330 break;
332 case '/':
333 dtp->u.p.input_complete = 1;
334 break;
336 case '\r':
337 if ((n = next_char(dtp)) == EOF)
338 return LIBERROR_END;
339 if (n != '\n')
341 unget_char (dtp, n);
342 break;
344 /* Fall through. */
345 case '\n':
346 dtp->u.p.at_eol = 1;
347 if (dtp->u.p.namelist_mode)
351 if ((c = next_char (dtp)) == EOF)
352 return LIBERROR_END;
353 if (c == '!')
355 err = eat_line (dtp);
356 if (err)
357 return err;
358 c = '\n';
361 while (c == '\n' || c == '\r' || c == ' ' || c == '\t');
362 unget_char (dtp, c);
364 break;
366 case '!':
367 if (dtp->u.p.namelist_mode)
368 { /* Eat a namelist comment. */
369 err = eat_line (dtp);
370 if (err)
371 return err;
373 break;
376 /* Fall Through... */
378 default:
379 unget_char (dtp, c);
380 break;
382 return err;
386 /* Finish processing a separator that was interrupted by a newline.
387 If we're here, then another data item is present, so we finish what
388 we started on the previous line. Return 0 on success, error code
389 on failure. */
391 static int
392 finish_separator (st_parameter_dt *dtp)
394 int c;
395 int err = LIBERROR_OK;
397 restart:
398 eat_spaces (dtp);
400 if ((c = next_char (dtp)) == EOF)
401 return LIBERROR_END;
402 switch (c)
404 case ',':
405 if (dtp->u.p.comma_flag)
406 unget_char (dtp, c);
407 else
409 if ((c = eat_spaces (dtp)) == EOF)
410 return LIBERROR_END;
411 if (c == '\n' || c == '\r')
412 goto restart;
415 break;
417 case '/':
418 dtp->u.p.input_complete = 1;
419 if (!dtp->u.p.namelist_mode)
420 return err;
421 break;
423 case '\n':
424 case '\r':
425 goto restart;
427 case '!':
428 if (dtp->u.p.namelist_mode)
430 err = eat_line (dtp);
431 if (err)
432 return err;
433 goto restart;
435 /* Fall through. */
436 default:
437 unget_char (dtp, c);
438 break;
440 return err;
444 /* This function is needed to catch bad conversions so that namelist can
445 attempt to see if dtp->u.p.saved_string contains a new object name rather
446 than a bad value. */
448 static int
449 nml_bad_return (st_parameter_dt *dtp, char c)
451 if (dtp->u.p.namelist_mode)
453 dtp->u.p.nml_read_error = 1;
454 unget_char (dtp, c);
455 return 1;
457 return 0;
460 /* Convert an unsigned string to an integer. The length value is -1
461 if we are working on a repeat count. Returns nonzero if we have a
462 range problem. As a side effect, frees the dtp->u.p.saved_string. */
464 static int
465 convert_integer (st_parameter_dt *dtp, int length, int negative)
467 char c, *buffer, message[MSGLEN];
468 int m;
469 GFC_UINTEGER_LARGEST v, max, max10;
470 GFC_INTEGER_LARGEST value;
472 buffer = dtp->u.p.saved_string;
473 v = 0;
475 if (length == -1)
476 max = MAX_REPEAT;
477 else
479 max = si_max (length);
480 if (negative)
481 max++;
483 max10 = max / 10;
485 for (;;)
487 c = *buffer++;
488 if (c == '\0')
489 break;
490 c -= '0';
492 if (v > max10)
493 goto overflow;
494 v = 10 * v;
496 if (v > max - c)
497 goto overflow;
498 v += c;
501 m = 0;
503 if (length != -1)
505 if (negative)
506 value = -v;
507 else
508 value = v;
509 set_integer (dtp->u.p.value, value, length);
511 else
513 dtp->u.p.repeat_count = v;
515 if (dtp->u.p.repeat_count == 0)
517 snprintf (message, MSGLEN, "Zero repeat count in item %d of list input",
518 dtp->u.p.item_count);
520 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
521 m = 1;
525 free_saved (dtp);
526 return m;
528 overflow:
529 if (length == -1)
530 snprintf (message, MSGLEN, "Repeat count overflow in item %d of list input",
531 dtp->u.p.item_count);
532 else
533 snprintf (message, MSGLEN, "Integer overflow while reading item %d",
534 dtp->u.p.item_count);
536 free_saved (dtp);
537 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
539 return 1;
543 /* Parse a repeat count for logical and complex values which cannot
544 begin with a digit. Returns nonzero if we are done, zero if we
545 should continue on. */
547 static int
548 parse_repeat (st_parameter_dt *dtp)
550 char message[MSGLEN];
551 int c, repeat;
553 if ((c = next_char (dtp)) == EOF)
554 goto bad_repeat;
555 switch (c)
557 CASE_DIGITS:
558 repeat = c - '0';
559 break;
561 CASE_SEPARATORS:
562 unget_char (dtp, c);
563 eat_separator (dtp);
564 return 1;
566 default:
567 unget_char (dtp, c);
568 return 0;
571 for (;;)
573 c = next_char (dtp);
574 switch (c)
576 CASE_DIGITS:
577 repeat = 10 * repeat + c - '0';
579 if (repeat > MAX_REPEAT)
581 snprintf (message, MSGLEN,
582 "Repeat count overflow 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 break;
591 case '*':
592 if (repeat == 0)
594 snprintf (message, MSGLEN,
595 "Zero repeat count in item %d of list input",
596 dtp->u.p.item_count);
598 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
599 return 1;
602 goto done;
604 default:
605 goto bad_repeat;
609 done:
610 dtp->u.p.repeat_count = repeat;
611 return 0;
613 bad_repeat:
615 free_saved (dtp);
616 if (c == EOF)
618 free_line (dtp);
619 hit_eof (dtp);
620 return 1;
622 else
623 eat_line (dtp);
624 snprintf (message, MSGLEN, "Bad repeat count in item %d of list input",
625 dtp->u.p.item_count);
626 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
627 return 1;
631 /* To read a logical we have to look ahead in the input stream to make sure
632 there is not an equal sign indicating a variable name. To do this we use
633 line_buffer to point to a temporary buffer, pushing characters there for
634 possible later reading. */
636 static void
637 l_push_char (st_parameter_dt *dtp, char c)
639 if (dtp->u.p.line_buffer == NULL)
640 dtp->u.p.line_buffer = xcalloc (SCRATCH_SIZE, 1);
642 dtp->u.p.line_buffer[dtp->u.p.item_count++] = c;
646 /* Read a logical character on the input. */
648 static void
649 read_logical (st_parameter_dt *dtp, int length)
651 char message[MSGLEN];
652 int c, i, v;
654 if (parse_repeat (dtp))
655 return;
657 c = tolower (next_char (dtp));
658 l_push_char (dtp, c);
659 switch (c)
661 case 't':
662 v = 1;
663 c = next_char (dtp);
664 l_push_char (dtp, c);
666 if (!is_separator(c) && c != EOF)
667 goto possible_name;
669 unget_char (dtp, c);
670 break;
671 case 'f':
672 v = 0;
673 c = next_char (dtp);
674 l_push_char (dtp, c);
676 if (!is_separator(c) && c != EOF)
677 goto possible_name;
679 unget_char (dtp, c);
680 break;
682 case '.':
683 c = tolower (next_char (dtp));
684 switch (c)
686 case 't':
687 v = 1;
688 break;
689 case 'f':
690 v = 0;
691 break;
692 default:
693 goto bad_logical;
696 break;
698 CASE_SEPARATORS:
699 case EOF:
700 unget_char (dtp, c);
701 eat_separator (dtp);
702 return; /* Null value. */
704 default:
705 /* Save the character in case it is the beginning
706 of the next object name. */
707 unget_char (dtp, c);
708 goto bad_logical;
711 dtp->u.p.saved_type = BT_LOGICAL;
712 dtp->u.p.saved_length = length;
714 /* Eat trailing garbage. */
716 c = next_char (dtp);
717 while (c != EOF && !is_separator (c));
719 unget_char (dtp, c);
720 eat_separator (dtp);
721 set_integer ((int *) dtp->u.p.value, v, length);
722 free_line (dtp);
724 return;
726 possible_name:
728 for(i = 0; i < 63; i++)
730 c = next_char (dtp);
731 if (is_separator(c))
733 /* All done if this is not a namelist read. */
734 if (!dtp->u.p.namelist_mode)
735 goto logical_done;
737 unget_char (dtp, c);
738 eat_separator (dtp);
739 c = next_char (dtp);
740 if (c != '=')
742 unget_char (dtp, c);
743 goto logical_done;
747 l_push_char (dtp, c);
748 if (c == '=')
750 dtp->u.p.nml_read_error = 1;
751 dtp->u.p.line_buffer_enabled = 1;
752 dtp->u.p.item_count = 0;
753 return;
758 bad_logical:
760 free_line (dtp);
762 if (nml_bad_return (dtp, c))
763 return;
765 free_saved (dtp);
766 if (c == EOF)
768 hit_eof (dtp);
769 return;
771 else if (c != '\n')
772 eat_line (dtp);
773 snprintf (message, MSGLEN, "Bad logical value while reading item %d",
774 dtp->u.p.item_count);
775 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
776 return;
778 logical_done:
780 dtp->u.p.saved_type = BT_LOGICAL;
781 dtp->u.p.saved_length = length;
782 set_integer ((int *) dtp->u.p.value, v, length);
783 free_saved (dtp);
784 free_line (dtp);
788 /* Reading integers is tricky because we can actually be reading a
789 repeat count. We have to store the characters in a buffer because
790 we could be reading an integer that is larger than the default int
791 used for repeat counts. */
793 static void
794 read_integer (st_parameter_dt *dtp, int length)
796 char message[MSGLEN];
797 int c, negative;
799 negative = 0;
801 c = next_char (dtp);
802 switch (c)
804 case '-':
805 negative = 1;
806 /* Fall through... */
808 case '+':
809 if ((c = next_char (dtp)) == EOF)
810 goto bad_integer;
811 goto get_integer;
813 CASE_SEPARATORS: /* Single null. */
814 unget_char (dtp, c);
815 eat_separator (dtp);
816 return;
818 CASE_DIGITS:
819 push_char (dtp, c);
820 break;
822 default:
823 goto bad_integer;
826 /* Take care of what may be a repeat count. */
828 for (;;)
830 c = next_char (dtp);
831 switch (c)
833 CASE_DIGITS:
834 push_char (dtp, c);
835 break;
837 case '*':
838 push_char (dtp, '\0');
839 goto repeat;
841 CASE_SEPARATORS: /* Not a repeat count. */
842 case EOF:
843 goto done;
845 default:
846 goto bad_integer;
850 repeat:
851 if (convert_integer (dtp, -1, 0))
852 return;
854 /* Get the real integer. */
856 if ((c = next_char (dtp)) == EOF)
857 goto bad_integer;
858 switch (c)
860 CASE_DIGITS:
861 break;
863 CASE_SEPARATORS:
864 unget_char (dtp, c);
865 eat_separator (dtp);
866 return;
868 case '-':
869 negative = 1;
870 /* Fall through... */
872 case '+':
873 c = next_char (dtp);
874 break;
877 get_integer:
878 if (!isdigit (c))
879 goto bad_integer;
880 push_char (dtp, c);
882 for (;;)
884 c = next_char (dtp);
885 switch (c)
887 CASE_DIGITS:
888 push_char (dtp, c);
889 break;
891 CASE_SEPARATORS:
892 case EOF:
893 goto done;
895 default:
896 goto bad_integer;
900 bad_integer:
902 if (nml_bad_return (dtp, c))
903 return;
905 free_saved (dtp);
906 if (c == EOF)
908 free_line (dtp);
909 hit_eof (dtp);
910 return;
912 else if (c != '\n')
913 eat_line (dtp);
915 free_line (dtp);
916 snprintf (message, MSGLEN, "Bad integer for item %d in list input",
917 dtp->u.p.item_count);
918 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
920 return;
922 done:
923 unget_char (dtp, c);
924 eat_separator (dtp);
926 push_char (dtp, '\0');
927 if (convert_integer (dtp, length, negative))
929 free_saved (dtp);
930 return;
933 free_saved (dtp);
934 dtp->u.p.saved_type = BT_INTEGER;
938 /* Read a character variable. */
940 static void
941 read_character (st_parameter_dt *dtp, int length __attribute__ ((unused)))
943 char quote, message[MSGLEN];
944 int c;
946 quote = ' '; /* Space means no quote character. */
948 if ((c = next_char (dtp)) == EOF)
949 goto eof;
950 switch (c)
952 CASE_DIGITS:
953 push_char (dtp, c);
954 break;
956 CASE_SEPARATORS:
957 case EOF:
958 unget_char (dtp, c); /* NULL value. */
959 eat_separator (dtp);
960 return;
962 case '"':
963 case '\'':
964 quote = c;
965 goto get_string;
967 default:
968 if (dtp->u.p.namelist_mode)
970 unget_char (dtp, c);
971 return;
974 push_char (dtp, c);
975 goto get_string;
978 /* Deal with a possible repeat count. */
980 for (;;)
982 c = next_char (dtp);
983 switch (c)
985 CASE_DIGITS:
986 push_char (dtp, c);
987 break;
989 CASE_SEPARATORS:
990 case EOF:
991 unget_char (dtp, c);
992 goto done; /* String was only digits! */
994 case '*':
995 push_char (dtp, '\0');
996 goto got_repeat;
998 default:
999 push_char (dtp, c);
1000 goto get_string; /* Not a repeat count after all. */
1004 got_repeat:
1005 if (convert_integer (dtp, -1, 0))
1006 return;
1008 /* Now get the real string. */
1010 if ((c = next_char (dtp)) == EOF)
1011 goto eof;
1012 switch (c)
1014 CASE_SEPARATORS:
1015 unget_char (dtp, c); /* Repeated NULL values. */
1016 eat_separator (dtp);
1017 return;
1019 case '"':
1020 case '\'':
1021 quote = c;
1022 break;
1024 default:
1025 push_char (dtp, c);
1026 break;
1029 get_string:
1030 for (;;)
1032 if ((c = next_char (dtp)) == EOF)
1033 goto done_eof;
1034 switch (c)
1036 case '"':
1037 case '\'':
1038 if (c != quote)
1040 push_char (dtp, c);
1041 break;
1044 /* See if we have a doubled quote character or the end of
1045 the string. */
1047 if ((c = next_char (dtp)) == EOF)
1048 goto done_eof;
1049 if (c == quote)
1051 push_char (dtp, quote);
1052 break;
1055 unget_char (dtp, c);
1056 goto done;
1058 CASE_SEPARATORS:
1059 if (quote == ' ')
1061 unget_char (dtp, c);
1062 goto done;
1065 if (c != '\n' && c != '\r')
1066 push_char (dtp, c);
1067 break;
1069 default:
1070 push_char (dtp, c);
1071 break;
1075 /* At this point, we have to have a separator, or else the string is
1076 invalid. */
1077 done:
1078 c = next_char (dtp);
1079 done_eof:
1080 if (is_separator (c) || c == '!' || c == EOF)
1082 unget_char (dtp, c);
1083 eat_separator (dtp);
1084 dtp->u.p.saved_type = BT_CHARACTER;
1086 else
1088 free_saved (dtp);
1089 snprintf (message, MSGLEN, "Invalid string input in item %d",
1090 dtp->u.p.item_count);
1091 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1093 free_line (dtp);
1094 return;
1096 eof:
1097 free_saved (dtp);
1098 free_line (dtp);
1099 hit_eof (dtp);
1103 /* Parse a component of a complex constant or a real number that we
1104 are sure is already there. This is a straight real number parser. */
1106 static int
1107 parse_real (st_parameter_dt *dtp, void *buffer, int length)
1109 char message[MSGLEN];
1110 int c, m, seen_dp;
1112 if ((c = next_char (dtp)) == EOF)
1113 goto bad;
1115 if (c == '-' || c == '+')
1117 push_char (dtp, c);
1118 if ((c = next_char (dtp)) == EOF)
1119 goto bad;
1122 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1123 c = '.';
1125 if (!isdigit (c) && c != '.')
1127 if (c == 'i' || c == 'I' || c == 'n' || c == 'N')
1128 goto inf_nan;
1129 else
1130 goto bad;
1133 push_char (dtp, c);
1135 seen_dp = (c == '.') ? 1 : 0;
1137 for (;;)
1139 if ((c = next_char (dtp)) == EOF)
1140 goto bad;
1141 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1142 c = '.';
1143 switch (c)
1145 CASE_DIGITS:
1146 push_char (dtp, c);
1147 break;
1149 case '.':
1150 if (seen_dp)
1151 goto bad;
1153 seen_dp = 1;
1154 push_char (dtp, c);
1155 break;
1157 case 'e':
1158 case 'E':
1159 case 'd':
1160 case 'D':
1161 case 'q':
1162 case 'Q':
1163 push_char (dtp, 'e');
1164 goto exp1;
1166 case '-':
1167 case '+':
1168 push_char (dtp, 'e');
1169 push_char (dtp, c);
1170 if ((c = next_char (dtp)) == EOF)
1171 goto bad;
1172 goto exp2;
1174 CASE_SEPARATORS:
1175 case EOF:
1176 goto done;
1178 default:
1179 goto done;
1183 exp1:
1184 if ((c = next_char (dtp)) == EOF)
1185 goto bad;
1186 if (c != '-' && c != '+')
1187 push_char (dtp, '+');
1188 else
1190 push_char (dtp, c);
1191 c = next_char (dtp);
1194 exp2:
1195 if (!isdigit (c))
1196 goto bad;
1198 push_char (dtp, c);
1200 for (;;)
1202 if ((c = next_char (dtp)) == EOF)
1203 goto bad;
1204 switch (c)
1206 CASE_DIGITS:
1207 push_char (dtp, c);
1208 break;
1210 CASE_SEPARATORS:
1211 case EOF:
1212 unget_char (dtp, c);
1213 goto done;
1215 default:
1216 goto done;
1220 done:
1221 unget_char (dtp, c);
1222 push_char (dtp, '\0');
1224 m = convert_real (dtp, buffer, dtp->u.p.saved_string, length);
1225 free_saved (dtp);
1227 return m;
1229 done_infnan:
1230 unget_char (dtp, c);
1231 push_char (dtp, '\0');
1233 m = convert_infnan (dtp, buffer, dtp->u.p.saved_string, length);
1234 free_saved (dtp);
1236 return m;
1238 inf_nan:
1239 /* Match INF and Infinity. */
1240 if ((c == 'i' || c == 'I')
1241 && ((c = next_char (dtp)) == 'n' || c == 'N')
1242 && ((c = next_char (dtp)) == 'f' || c == 'F'))
1244 c = next_char (dtp);
1245 if ((c != 'i' && c != 'I')
1246 || ((c == 'i' || c == 'I')
1247 && ((c = next_char (dtp)) == 'n' || c == 'N')
1248 && ((c = next_char (dtp)) == 'i' || c == 'I')
1249 && ((c = next_char (dtp)) == 't' || c == 'T')
1250 && ((c = next_char (dtp)) == 'y' || c == 'Y')
1251 && (c = next_char (dtp))))
1253 if (is_separator (c) || (c == EOF))
1254 unget_char (dtp, c);
1255 push_char (dtp, 'i');
1256 push_char (dtp, 'n');
1257 push_char (dtp, 'f');
1258 goto done_infnan;
1260 } /* Match NaN. */
1261 else if (((c = next_char (dtp)) == 'a' || c == 'A')
1262 && ((c = next_char (dtp)) == 'n' || c == 'N')
1263 && (c = next_char (dtp)))
1265 if (is_separator (c) || (c == EOF))
1266 unget_char (dtp, c);
1267 push_char (dtp, 'n');
1268 push_char (dtp, 'a');
1269 push_char (dtp, 'n');
1271 /* Match "NAN(alphanum)". */
1272 if (c == '(')
1274 for ( ; c != ')'; c = next_char (dtp))
1275 if (is_separator (c))
1276 goto bad;
1278 c = next_char (dtp);
1279 if (is_separator (c) || (c == EOF))
1280 unget_char (dtp, c);
1282 goto done_infnan;
1285 bad:
1287 if (nml_bad_return (dtp, c))
1288 return 0;
1290 free_saved (dtp);
1291 if (c == EOF)
1293 free_line (dtp);
1294 hit_eof (dtp);
1295 return 1;
1297 else if (c != '\n')
1298 eat_line (dtp);
1300 free_line (dtp);
1301 snprintf (message, MSGLEN, "Bad floating point number for item %d",
1302 dtp->u.p.item_count);
1303 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1305 return 1;
1309 /* Reading a complex number is straightforward because we can tell
1310 what it is right away. */
1312 static void
1313 read_complex (st_parameter_dt *dtp, void * dest, int kind, size_t size)
1315 char message[MSGLEN];
1316 int c;
1318 if (parse_repeat (dtp))
1319 return;
1321 c = next_char (dtp);
1322 switch (c)
1324 case '(':
1325 break;
1327 CASE_SEPARATORS:
1328 case EOF:
1329 unget_char (dtp, c);
1330 eat_separator (dtp);
1331 return;
1333 default:
1334 goto bad_complex;
1337 eol_1:
1338 eat_spaces (dtp);
1339 c = next_char (dtp);
1340 if (c == '\n' || c== '\r')
1341 goto eol_1;
1342 else
1343 unget_char (dtp, c);
1345 if (parse_real (dtp, dest, kind))
1346 return;
1348 eol_2:
1349 eat_spaces (dtp);
1350 c = next_char (dtp);
1351 if (c == '\n' || c== '\r')
1352 goto eol_2;
1353 else
1354 unget_char (dtp, c);
1356 if (next_char (dtp)
1357 != (dtp->u.p.current_unit->decimal_status == DECIMAL_POINT ? ',' : ';'))
1358 goto bad_complex;
1360 eol_3:
1361 eat_spaces (dtp);
1362 c = next_char (dtp);
1363 if (c == '\n' || c== '\r')
1364 goto eol_3;
1365 else
1366 unget_char (dtp, c);
1368 if (parse_real (dtp, dest + size / 2, kind))
1369 return;
1371 eol_4:
1372 eat_spaces (dtp);
1373 c = next_char (dtp);
1374 if (c == '\n' || c== '\r')
1375 goto eol_4;
1376 else
1377 unget_char (dtp, c);
1379 if (next_char (dtp) != ')')
1380 goto bad_complex;
1382 c = next_char (dtp);
1383 if (!is_separator (c) && (c != EOF))
1384 goto bad_complex;
1386 unget_char (dtp, c);
1387 eat_separator (dtp);
1389 free_saved (dtp);
1390 dtp->u.p.saved_type = BT_COMPLEX;
1391 return;
1393 bad_complex:
1395 if (nml_bad_return (dtp, c))
1396 return;
1398 free_saved (dtp);
1399 if (c == EOF)
1401 free_line (dtp);
1402 hit_eof (dtp);
1403 return;
1405 else if (c != '\n')
1406 eat_line (dtp);
1408 free_line (dtp);
1409 snprintf (message, MSGLEN, "Bad complex value in item %d of list input",
1410 dtp->u.p.item_count);
1411 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1415 /* Parse a real number with a possible repeat count. */
1417 static void
1418 read_real (st_parameter_dt *dtp, void * dest, int length)
1420 char message[MSGLEN];
1421 int c;
1422 int seen_dp;
1423 int is_inf;
1425 seen_dp = 0;
1427 c = next_char (dtp);
1428 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1429 c = '.';
1430 switch (c)
1432 CASE_DIGITS:
1433 push_char (dtp, c);
1434 break;
1436 case '.':
1437 push_char (dtp, c);
1438 seen_dp = 1;
1439 break;
1441 case '+':
1442 case '-':
1443 goto got_sign;
1445 CASE_SEPARATORS:
1446 unget_char (dtp, c); /* Single null. */
1447 eat_separator (dtp);
1448 return;
1450 case 'i':
1451 case 'I':
1452 case 'n':
1453 case 'N':
1454 goto inf_nan;
1456 default:
1457 goto bad_real;
1460 /* Get the digit string that might be a repeat count. */
1462 for (;;)
1464 c = next_char (dtp);
1465 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1466 c = '.';
1467 switch (c)
1469 CASE_DIGITS:
1470 push_char (dtp, c);
1471 break;
1473 case '.':
1474 if (seen_dp)
1475 goto bad_real;
1477 seen_dp = 1;
1478 push_char (dtp, c);
1479 goto real_loop;
1481 case 'E':
1482 case 'e':
1483 case 'D':
1484 case 'd':
1485 case 'Q':
1486 case 'q':
1487 goto exp1;
1489 case '+':
1490 case '-':
1491 push_char (dtp, 'e');
1492 push_char (dtp, c);
1493 c = next_char (dtp);
1494 goto exp2;
1496 case '*':
1497 push_char (dtp, '\0');
1498 goto got_repeat;
1500 CASE_SEPARATORS:
1501 case EOF:
1502 if (c != '\n' && c != ',' && c != '\r' && c != ';')
1503 unget_char (dtp, c);
1504 goto done;
1506 default:
1507 goto bad_real;
1511 got_repeat:
1512 if (convert_integer (dtp, -1, 0))
1513 return;
1515 /* Now get the number itself. */
1517 if ((c = next_char (dtp)) == EOF)
1518 goto bad_real;
1519 if (is_separator (c))
1520 { /* Repeated null value. */
1521 unget_char (dtp, c);
1522 eat_separator (dtp);
1523 return;
1526 if (c != '-' && c != '+')
1527 push_char (dtp, '+');
1528 else
1530 got_sign:
1531 push_char (dtp, c);
1532 if ((c = next_char (dtp)) == EOF)
1533 goto bad_real;
1536 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1537 c = '.';
1539 if (!isdigit (c) && c != '.')
1541 if (c == 'i' || c == 'I' || c == 'n' || c == 'N')
1542 goto inf_nan;
1543 else
1544 goto bad_real;
1547 if (c == '.')
1549 if (seen_dp)
1550 goto bad_real;
1551 else
1552 seen_dp = 1;
1555 push_char (dtp, c);
1557 real_loop:
1558 for (;;)
1560 c = next_char (dtp);
1561 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1562 c = '.';
1563 switch (c)
1565 CASE_DIGITS:
1566 push_char (dtp, c);
1567 break;
1569 CASE_SEPARATORS:
1570 case EOF:
1571 goto done;
1573 case '.':
1574 if (seen_dp)
1575 goto bad_real;
1577 seen_dp = 1;
1578 push_char (dtp, c);
1579 break;
1581 case 'E':
1582 case 'e':
1583 case 'D':
1584 case 'd':
1585 case 'Q':
1586 case 'q':
1587 goto exp1;
1589 case '+':
1590 case '-':
1591 push_char (dtp, 'e');
1592 push_char (dtp, c);
1593 c = next_char (dtp);
1594 goto exp2;
1596 default:
1597 goto bad_real;
1601 exp1:
1602 push_char (dtp, 'e');
1604 if ((c = next_char (dtp)) == EOF)
1605 goto bad_real;
1606 if (c != '+' && c != '-')
1607 push_char (dtp, '+');
1608 else
1610 push_char (dtp, c);
1611 c = next_char (dtp);
1614 exp2:
1615 if (!isdigit (c))
1616 goto bad_real;
1617 push_char (dtp, c);
1619 for (;;)
1621 c = next_char (dtp);
1623 switch (c)
1625 CASE_DIGITS:
1626 push_char (dtp, c);
1627 break;
1629 CASE_SEPARATORS:
1630 case EOF:
1631 goto done;
1633 default:
1634 goto bad_real;
1638 done:
1639 unget_char (dtp, c);
1640 eat_separator (dtp);
1641 push_char (dtp, '\0');
1642 if (convert_real (dtp, dest, dtp->u.p.saved_string, length))
1644 free_saved (dtp);
1645 return;
1648 free_saved (dtp);
1649 dtp->u.p.saved_type = BT_REAL;
1650 return;
1652 inf_nan:
1653 l_push_char (dtp, c);
1654 is_inf = 0;
1656 /* Match INF and Infinity. */
1657 if (c == 'i' || c == 'I')
1659 c = next_char (dtp);
1660 l_push_char (dtp, c);
1661 if (c != 'n' && c != 'N')
1662 goto unwind;
1663 c = next_char (dtp);
1664 l_push_char (dtp, c);
1665 if (c != 'f' && c != 'F')
1666 goto unwind;
1667 c = next_char (dtp);
1668 l_push_char (dtp, c);
1669 if (!is_separator (c) && (c != EOF))
1671 if (c != 'i' && c != 'I')
1672 goto unwind;
1673 c = next_char (dtp);
1674 l_push_char (dtp, c);
1675 if (c != 'n' && c != 'N')
1676 goto unwind;
1677 c = next_char (dtp);
1678 l_push_char (dtp, c);
1679 if (c != 'i' && c != 'I')
1680 goto unwind;
1681 c = next_char (dtp);
1682 l_push_char (dtp, c);
1683 if (c != 't' && c != 'T')
1684 goto unwind;
1685 c = next_char (dtp);
1686 l_push_char (dtp, c);
1687 if (c != 'y' && c != 'Y')
1688 goto unwind;
1689 c = next_char (dtp);
1690 l_push_char (dtp, c);
1692 is_inf = 1;
1693 } /* Match NaN. */
1694 else
1696 c = next_char (dtp);
1697 l_push_char (dtp, c);
1698 if (c != 'a' && c != 'A')
1699 goto unwind;
1700 c = next_char (dtp);
1701 l_push_char (dtp, c);
1702 if (c != 'n' && c != 'N')
1703 goto unwind;
1704 c = next_char (dtp);
1705 l_push_char (dtp, c);
1707 /* Match NAN(alphanum). */
1708 if (c == '(')
1710 for (c = next_char (dtp); c != ')'; c = next_char (dtp))
1711 if (is_separator (c))
1712 goto unwind;
1713 else
1714 l_push_char (dtp, c);
1716 l_push_char (dtp, ')');
1717 c = next_char (dtp);
1718 l_push_char (dtp, c);
1722 if (!is_separator (c) && (c != EOF))
1723 goto unwind;
1725 if (dtp->u.p.namelist_mode)
1727 if (c == ' ' || c =='\n' || c == '\r')
1731 if ((c = next_char (dtp)) == EOF)
1732 goto bad_real;
1734 while (c == ' ' || c =='\n' || c == '\r');
1736 l_push_char (dtp, c);
1738 if (c == '=')
1739 goto unwind;
1743 if (is_inf)
1745 push_char (dtp, 'i');
1746 push_char (dtp, 'n');
1747 push_char (dtp, 'f');
1749 else
1751 push_char (dtp, 'n');
1752 push_char (dtp, 'a');
1753 push_char (dtp, 'n');
1756 free_line (dtp);
1757 unget_char (dtp, c);
1758 eat_separator (dtp);
1759 push_char (dtp, '\0');
1760 if (convert_infnan (dtp, dest, dtp->u.p.saved_string, length))
1761 return;
1763 free_saved (dtp);
1764 dtp->u.p.saved_type = BT_REAL;
1765 return;
1767 unwind:
1768 if (dtp->u.p.namelist_mode)
1770 dtp->u.p.nml_read_error = 1;
1771 dtp->u.p.line_buffer_enabled = 1;
1772 dtp->u.p.item_count = 0;
1773 return;
1776 bad_real:
1778 if (nml_bad_return (dtp, c))
1779 return;
1781 free_saved (dtp);
1782 if (c == EOF)
1784 free_line (dtp);
1785 hit_eof (dtp);
1786 return;
1788 else if (c != '\n')
1789 eat_line (dtp);
1791 free_line (dtp);
1792 snprintf (message, MSGLEN, "Bad real number in item %d of list input",
1793 dtp->u.p.item_count);
1794 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1798 /* Check the current type against the saved type to make sure they are
1799 compatible. Returns nonzero if incompatible. */
1801 static int
1802 check_type (st_parameter_dt *dtp, bt type, int kind)
1804 char message[MSGLEN];
1806 if (dtp->u.p.saved_type != BT_UNKNOWN && dtp->u.p.saved_type != type)
1808 free_line (dtp);
1809 snprintf (message, MSGLEN, "Read type %s where %s was expected for item %d",
1810 type_name (dtp->u.p.saved_type), type_name (type),
1811 dtp->u.p.item_count);
1813 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1814 return 1;
1817 if (dtp->u.p.saved_type == BT_UNKNOWN || dtp->u.p.saved_type == BT_CHARACTER)
1818 return 0;
1820 if ((type != BT_COMPLEX && dtp->u.p.saved_length != kind)
1821 || (type == BT_COMPLEX && dtp->u.p.saved_length != kind*2))
1823 free_line (dtp);
1824 snprintf (message, MSGLEN,
1825 "Read kind %d %s where kind %d is required for item %d",
1826 type == BT_COMPLEX ? dtp->u.p.saved_length / 2
1827 : dtp->u.p.saved_length,
1828 type_name (dtp->u.p.saved_type), kind,
1829 dtp->u.p.item_count);
1830 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1831 return 1;
1834 return 0;
1838 /* Top level data transfer subroutine for list reads. Because we have
1839 to deal with repeat counts, the data item is always saved after
1840 reading, usually in the dtp->u.p.value[] array. If a repeat count is
1841 greater than one, we copy the data item multiple times. */
1843 static int
1844 list_formatted_read_scalar (st_parameter_dt *dtp, bt type, void *p,
1845 int kind, size_t size)
1847 gfc_char4_t *q;
1848 int c, i, m;
1849 int err = 0;
1851 dtp->u.p.namelist_mode = 0;
1853 if (dtp->u.p.first_item)
1855 dtp->u.p.first_item = 0;
1856 dtp->u.p.input_complete = 0;
1857 dtp->u.p.repeat_count = 1;
1858 dtp->u.p.at_eol = 0;
1860 if ((c = eat_spaces (dtp)) == EOF)
1862 err = LIBERROR_END;
1863 goto cleanup;
1865 if (is_separator (c))
1867 /* Found a null value. */
1868 eat_separator (dtp);
1869 dtp->u.p.repeat_count = 0;
1871 /* eat_separator sets this flag if the separator was a comma. */
1872 if (dtp->u.p.comma_flag)
1873 goto cleanup;
1875 /* eat_separator sets this flag if the separator was a \n or \r. */
1876 if (dtp->u.p.at_eol)
1877 finish_separator (dtp);
1878 else
1879 goto cleanup;
1883 else
1885 if (dtp->u.p.repeat_count > 0)
1887 if (check_type (dtp, type, kind))
1888 return err;
1889 goto set_value;
1892 if (dtp->u.p.input_complete)
1893 goto cleanup;
1895 if (dtp->u.p.at_eol)
1896 finish_separator (dtp);
1897 else
1899 eat_spaces (dtp);
1900 /* Trailing spaces prior to end of line. */
1901 if (dtp->u.p.at_eol)
1902 finish_separator (dtp);
1905 dtp->u.p.saved_type = BT_UNKNOWN;
1906 dtp->u.p.repeat_count = 1;
1909 switch (type)
1911 case BT_INTEGER:
1912 read_integer (dtp, kind);
1913 break;
1914 case BT_LOGICAL:
1915 read_logical (dtp, kind);
1916 break;
1917 case BT_CHARACTER:
1918 read_character (dtp, kind);
1919 break;
1920 case BT_REAL:
1921 read_real (dtp, p, kind);
1922 /* Copy value back to temporary if needed. */
1923 if (dtp->u.p.repeat_count > 0)
1924 memcpy (dtp->u.p.value, p, size);
1925 break;
1926 case BT_COMPLEX:
1927 read_complex (dtp, p, kind, size);
1928 /* Copy value back to temporary if needed. */
1929 if (dtp->u.p.repeat_count > 0)
1930 memcpy (dtp->u.p.value, p, size);
1931 break;
1932 default:
1933 internal_error (&dtp->common, "Bad type for list read");
1936 if (dtp->u.p.saved_type != BT_CHARACTER && dtp->u.p.saved_type != BT_UNKNOWN)
1937 dtp->u.p.saved_length = size;
1939 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1940 goto cleanup;
1942 set_value:
1943 switch (dtp->u.p.saved_type)
1945 case BT_COMPLEX:
1946 case BT_REAL:
1947 if (dtp->u.p.repeat_count > 0)
1948 memcpy (p, dtp->u.p.value, size);
1949 break;
1951 case BT_INTEGER:
1952 case BT_LOGICAL:
1953 memcpy (p, dtp->u.p.value, size);
1954 break;
1956 case BT_CHARACTER:
1957 if (dtp->u.p.saved_string)
1959 m = ((int) size < dtp->u.p.saved_used)
1960 ? (int) size : dtp->u.p.saved_used;
1961 if (kind == 1)
1962 memcpy (p, dtp->u.p.saved_string, m);
1963 else
1965 q = (gfc_char4_t *) p;
1966 for (i = 0; i < m; i++)
1967 q[i] = (unsigned char) dtp->u.p.saved_string[i];
1970 else
1971 /* Just delimiters encountered, nothing to copy but SPACE. */
1972 m = 0;
1974 if (m < (int) size)
1976 if (kind == 1)
1977 memset (((char *) p) + m, ' ', size - m);
1978 else
1980 q = (gfc_char4_t *) p;
1981 for (i = m; i < (int) size; i++)
1982 q[i] = (unsigned char) ' ';
1985 break;
1987 case BT_UNKNOWN:
1988 break;
1990 default:
1991 internal_error (&dtp->common, "Bad type for list read");
1994 if (--dtp->u.p.repeat_count <= 0)
1995 free_saved (dtp);
1997 cleanup:
1998 if (err == LIBERROR_END)
2000 free_line (dtp);
2001 hit_eof (dtp);
2003 return err;
2007 void
2008 list_formatted_read (st_parameter_dt *dtp, bt type, void *p, int kind,
2009 size_t size, size_t nelems)
2011 size_t elem;
2012 char *tmp;
2013 size_t stride = type == BT_CHARACTER ?
2014 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
2015 int err;
2017 tmp = (char *) p;
2019 /* Big loop over all the elements. */
2020 for (elem = 0; elem < nelems; elem++)
2022 dtp->u.p.item_count++;
2023 err = list_formatted_read_scalar (dtp, type, tmp + stride*elem,
2024 kind, size);
2025 if (err)
2026 break;
2031 /* Finish a list read. */
2033 void
2034 finish_list_read (st_parameter_dt *dtp)
2036 int err;
2038 free_saved (dtp);
2040 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
2042 if (dtp->u.p.at_eol)
2044 dtp->u.p.at_eol = 0;
2045 return;
2048 err = eat_line (dtp);
2049 if (err == LIBERROR_END)
2051 free_line (dtp);
2052 hit_eof (dtp);
2056 /* NAMELIST INPUT
2058 void namelist_read (st_parameter_dt *dtp)
2059 calls:
2060 static void nml_match_name (char *name, int len)
2061 static int nml_query (st_parameter_dt *dtp)
2062 static int nml_get_obj_data (st_parameter_dt *dtp,
2063 namelist_info **prev_nl, char *, size_t)
2064 calls:
2065 static void nml_untouch_nodes (st_parameter_dt *dtp)
2066 static namelist_info * find_nml_node (st_parameter_dt *dtp,
2067 char * var_name)
2068 static int nml_parse_qualifier(descriptor_dimension * ad,
2069 array_loop_spec * ls, int rank, char *)
2070 static void nml_touch_nodes (namelist_info * nl)
2071 static int nml_read_obj (namelist_info *nl, index_type offset,
2072 namelist_info **prev_nl, char *, size_t,
2073 index_type clow, index_type chigh)
2074 calls:
2075 -itself- */
2077 /* Inputs a rank-dimensional qualifier, which can contain
2078 singlets, doublets, triplets or ':' with the standard meanings. */
2080 static bool
2081 nml_parse_qualifier (st_parameter_dt *dtp, descriptor_dimension *ad,
2082 array_loop_spec *ls, int rank, bt nml_elem_type,
2083 char *parse_err_msg, size_t parse_err_msg_size,
2084 int *parsed_rank)
2086 int dim;
2087 int indx;
2088 int neg;
2089 int null_flag;
2090 int is_array_section, is_char;
2091 int c;
2093 is_char = 0;
2094 is_array_section = 0;
2095 dtp->u.p.expanded_read = 0;
2097 /* See if this is a character substring qualifier we are looking for. */
2098 if (rank == -1)
2100 rank = 1;
2101 is_char = 1;
2104 /* The next character in the stream should be the '('. */
2106 if ((c = next_char (dtp)) == EOF)
2107 goto err_ret;
2109 /* Process the qualifier, by dimension and triplet. */
2111 for (dim=0; dim < rank; dim++ )
2113 for (indx=0; indx<3; indx++)
2115 free_saved (dtp);
2116 eat_spaces (dtp);
2117 neg = 0;
2119 /* Process a potential sign. */
2120 if ((c = next_char (dtp)) == EOF)
2121 goto err_ret;
2122 switch (c)
2124 case '-':
2125 neg = 1;
2126 break;
2128 case '+':
2129 break;
2131 default:
2132 unget_char (dtp, c);
2133 break;
2136 /* Process characters up to the next ':' , ',' or ')'. */
2137 for (;;)
2139 c = next_char (dtp);
2140 switch (c)
2142 case EOF:
2143 goto err_ret;
2145 case ':':
2146 is_array_section = 1;
2147 break;
2149 case ',': case ')':
2150 if ((c==',' && dim == rank -1)
2151 || (c==')' && dim < rank -1))
2153 if (is_char)
2154 snprintf (parse_err_msg, parse_err_msg_size,
2155 "Bad substring qualifier");
2156 else
2157 snprintf (parse_err_msg, parse_err_msg_size,
2158 "Bad number of index fields");
2159 goto err_ret;
2161 break;
2163 CASE_DIGITS:
2164 push_char (dtp, c);
2165 continue;
2167 case ' ': case '\t': case '\r': case '\n':
2168 eat_spaces (dtp);
2169 break;
2171 default:
2172 if (is_char)
2173 snprintf (parse_err_msg, parse_err_msg_size,
2174 "Bad character in substring qualifier");
2175 else
2176 snprintf (parse_err_msg, parse_err_msg_size,
2177 "Bad character in index");
2178 goto err_ret;
2181 if ((c == ',' || c == ')') && indx == 0
2182 && dtp->u.p.saved_string == 0)
2184 if (is_char)
2185 snprintf (parse_err_msg, parse_err_msg_size,
2186 "Null substring qualifier");
2187 else
2188 snprintf (parse_err_msg, parse_err_msg_size,
2189 "Null index field");
2190 goto err_ret;
2193 if ((c == ':' && indx == 1 && dtp->u.p.saved_string == 0)
2194 || (indx == 2 && dtp->u.p.saved_string == 0))
2196 if (is_char)
2197 snprintf (parse_err_msg, parse_err_msg_size,
2198 "Bad substring qualifier");
2199 else
2200 snprintf (parse_err_msg, parse_err_msg_size,
2201 "Bad index triplet");
2202 goto err_ret;
2205 if (is_char && !is_array_section)
2207 snprintf (parse_err_msg, parse_err_msg_size,
2208 "Missing colon in substring qualifier");
2209 goto err_ret;
2212 /* If '( : ? )' or '( ? : )' break and flag read failure. */
2213 null_flag = 0;
2214 if ((c == ':' && indx == 0 && dtp->u.p.saved_string == 0)
2215 || (indx==1 && dtp->u.p.saved_string == 0))
2217 null_flag = 1;
2218 break;
2221 /* Now read the index. */
2222 if (convert_integer (dtp, sizeof(index_type), neg))
2224 if (is_char)
2225 snprintf (parse_err_msg, parse_err_msg_size,
2226 "Bad integer substring qualifier");
2227 else
2228 snprintf (parse_err_msg, parse_err_msg_size,
2229 "Bad integer in index");
2230 goto err_ret;
2232 break;
2235 /* Feed the index values to the triplet arrays. */
2236 if (!null_flag)
2238 if (indx == 0)
2239 memcpy (&ls[dim].start, dtp->u.p.value, sizeof(index_type));
2240 if (indx == 1)
2241 memcpy (&ls[dim].end, dtp->u.p.value, sizeof(index_type));
2242 if (indx == 2)
2243 memcpy (&ls[dim].step, dtp->u.p.value, sizeof(index_type));
2246 /* Singlet or doublet indices. */
2247 if (c==',' || c==')')
2249 if (indx == 0)
2251 memcpy (&ls[dim].start, dtp->u.p.value, sizeof(index_type));
2253 /* If -std=f95/2003 or an array section is specified,
2254 do not allow excess data to be processed. */
2255 if (is_array_section == 1
2256 || !(compile_options.allow_std & GFC_STD_GNU)
2257 || nml_elem_type == BT_DERIVED)
2258 ls[dim].end = ls[dim].start;
2259 else
2260 dtp->u.p.expanded_read = 1;
2263 /* Check for non-zero rank. */
2264 if (is_array_section == 1 && ls[dim].start != ls[dim].end)
2265 *parsed_rank = 1;
2267 break;
2271 if (is_array_section == 1 && dtp->u.p.expanded_read == 1)
2273 int i;
2274 dtp->u.p.expanded_read = 0;
2275 for (i = 0; i < dim; i++)
2276 ls[i].end = ls[i].start;
2279 /* Check the values of the triplet indices. */
2280 if ((ls[dim].start > GFC_DIMENSION_UBOUND(ad[dim]))
2281 || (ls[dim].start < GFC_DIMENSION_LBOUND(ad[dim]))
2282 || (ls[dim].end > GFC_DIMENSION_UBOUND(ad[dim]))
2283 || (ls[dim].end < GFC_DIMENSION_LBOUND(ad[dim])))
2285 if (is_char)
2286 snprintf (parse_err_msg, parse_err_msg_size,
2287 "Substring out of range");
2288 else
2289 snprintf (parse_err_msg, parse_err_msg_size,
2290 "Index %d out of range", dim + 1);
2291 goto err_ret;
2294 if (((ls[dim].end - ls[dim].start ) * ls[dim].step < 0)
2295 || (ls[dim].step == 0))
2297 snprintf (parse_err_msg, parse_err_msg_size,
2298 "Bad range in index %d", dim + 1);
2299 goto err_ret;
2302 /* Initialise the loop index counter. */
2303 ls[dim].idx = ls[dim].start;
2305 eat_spaces (dtp);
2306 return true;
2308 err_ret:
2310 /* The EOF error message is issued by hit_eof. Return true so that the
2311 caller does not use parse_err_msg and parse_err_msg_size to generate
2312 an unrelated error message. */
2313 if (c == EOF)
2315 hit_eof (dtp);
2316 dtp->u.p.input_complete = 1;
2317 return true;
2319 return false;
2322 static namelist_info *
2323 find_nml_node (st_parameter_dt *dtp, char * var_name)
2325 namelist_info * t = dtp->u.p.ionml;
2326 while (t != NULL)
2328 if (strcmp (var_name, t->var_name) == 0)
2330 t->touched = 1;
2331 return t;
2333 t = t->next;
2335 return NULL;
2338 /* Visits all the components of a derived type that have
2339 not explicitly been identified in the namelist input.
2340 touched is set and the loop specification initialised
2341 to default values */
2343 static void
2344 nml_touch_nodes (namelist_info * nl)
2346 index_type len = strlen (nl->var_name) + 1;
2347 int dim;
2348 char * ext_name = (char*)xmalloc (len + 1);
2349 memcpy (ext_name, nl->var_name, len-1);
2350 memcpy (ext_name + len - 1, "%", 2);
2351 for (nl = nl->next; nl; nl = nl->next)
2353 if (strncmp (nl->var_name, ext_name, len) == 0)
2355 nl->touched = 1;
2356 for (dim=0; dim < nl->var_rank; dim++)
2358 nl->ls[dim].step = 1;
2359 nl->ls[dim].end = GFC_DESCRIPTOR_UBOUND(nl,dim);
2360 nl->ls[dim].start = GFC_DESCRIPTOR_LBOUND(nl,dim);
2361 nl->ls[dim].idx = nl->ls[dim].start;
2364 else
2365 break;
2367 free (ext_name);
2368 return;
2371 /* Resets touched for the entire list of nml_nodes, ready for a
2372 new object. */
2374 static void
2375 nml_untouch_nodes (st_parameter_dt *dtp)
2377 namelist_info * t;
2378 for (t = dtp->u.p.ionml; t; t = t->next)
2379 t->touched = 0;
2380 return;
2383 /* Attempts to input name to namelist name. Returns
2384 dtp->u.p.nml_read_error = 1 on no match. */
2386 static void
2387 nml_match_name (st_parameter_dt *dtp, const char *name, index_type len)
2389 index_type i;
2390 int c;
2392 dtp->u.p.nml_read_error = 0;
2393 for (i = 0; i < len; i++)
2395 c = next_char (dtp);
2396 if (c == EOF || (tolower (c) != tolower (name[i])))
2398 dtp->u.p.nml_read_error = 1;
2399 break;
2404 /* If the namelist read is from stdin, output the current state of the
2405 namelist to stdout. This is used to implement the non-standard query
2406 features, ? and =?. If c == '=' the full namelist is printed. Otherwise
2407 the names alone are printed. */
2409 static void
2410 nml_query (st_parameter_dt *dtp, char c)
2412 gfc_unit * temp_unit;
2413 namelist_info * nl;
2414 index_type len;
2415 char * p;
2416 #ifdef HAVE_CRLF
2417 static const index_type endlen = 2;
2418 static const char endl[] = "\r\n";
2419 static const char nmlend[] = "&end\r\n";
2420 #else
2421 static const index_type endlen = 1;
2422 static const char endl[] = "\n";
2423 static const char nmlend[] = "&end\n";
2424 #endif
2426 if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
2427 return;
2429 /* Store the current unit and transfer to stdout. */
2431 temp_unit = dtp->u.p.current_unit;
2432 dtp->u.p.current_unit = find_unit (options.stdout_unit);
2434 if (dtp->u.p.current_unit)
2436 dtp->u.p.mode = WRITING;
2437 next_record (dtp, 0);
2439 /* Write the namelist in its entirety. */
2441 if (c == '=')
2442 namelist_write (dtp);
2444 /* Or write the list of names. */
2446 else
2448 /* "&namelist_name\n" */
2450 len = dtp->namelist_name_len;
2451 p = write_block (dtp, len - 1 + endlen);
2452 if (!p)
2453 goto query_return;
2454 memcpy (p, "&", 1);
2455 memcpy ((char*)(p + 1), dtp->namelist_name, len);
2456 memcpy ((char*)(p + len + 1), &endl, endlen);
2457 for (nl = dtp->u.p.ionml; nl; nl = nl->next)
2459 /* " var_name\n" */
2461 len = strlen (nl->var_name);
2462 p = write_block (dtp, len + endlen);
2463 if (!p)
2464 goto query_return;
2465 memcpy (p, " ", 1);
2466 memcpy ((char*)(p + 1), nl->var_name, len);
2467 memcpy ((char*)(p + len + 1), &endl, endlen);
2470 /* "&end\n" */
2472 p = write_block (dtp, endlen + 4);
2473 if (!p)
2474 goto query_return;
2475 memcpy (p, &nmlend, endlen + 4);
2478 /* Flush the stream to force immediate output. */
2480 fbuf_flush (dtp->u.p.current_unit, WRITING);
2481 sflush (dtp->u.p.current_unit->s);
2482 unlock_unit (dtp->u.p.current_unit);
2485 query_return:
2487 /* Restore the current unit. */
2489 dtp->u.p.current_unit = temp_unit;
2490 dtp->u.p.mode = READING;
2491 return;
2494 /* Reads and stores the input for the namelist object nl. For an array,
2495 the function loops over the ranges defined by the loop specification.
2496 This default to all the data or to the specification from a qualifier.
2497 nml_read_obj recursively calls itself to read derived types. It visits
2498 all its own components but only reads data for those that were touched
2499 when the name was parsed. If a read error is encountered, an attempt is
2500 made to return to read a new object name because the standard allows too
2501 little data to be available. On the other hand, too much data is an
2502 error. */
2504 static bool
2505 nml_read_obj (st_parameter_dt *dtp, namelist_info * nl, index_type offset,
2506 namelist_info **pprev_nl, char *nml_err_msg,
2507 size_t nml_err_msg_size, index_type clow, index_type chigh)
2509 namelist_info * cmp;
2510 char * obj_name;
2511 int nml_carry;
2512 int len;
2513 int dim;
2514 index_type dlen;
2515 index_type m;
2516 size_t obj_name_len;
2517 void * pdata;
2519 /* If we have encountered a previous read error or this object has not been
2520 touched in name parsing, just return. */
2521 if (dtp->u.p.nml_read_error || !nl->touched)
2522 return true;
2524 dtp->u.p.repeat_count = 0;
2525 eat_spaces (dtp);
2527 len = nl->len;
2528 switch (nl->type)
2530 case BT_INTEGER:
2531 case BT_LOGICAL:
2532 dlen = len;
2533 break;
2535 case BT_REAL:
2536 dlen = size_from_real_kind (len);
2537 break;
2539 case BT_COMPLEX:
2540 dlen = size_from_complex_kind (len);
2541 break;
2543 case BT_CHARACTER:
2544 dlen = chigh ? (chigh - clow + 1) : nl->string_length;
2545 break;
2547 default:
2548 dlen = 0;
2553 /* Update the pointer to the data, using the current index vector */
2555 pdata = (void*)(nl->mem_pos + offset);
2556 for (dim = 0; dim < nl->var_rank; dim++)
2557 pdata = (void*)(pdata + (nl->ls[dim].idx
2558 - GFC_DESCRIPTOR_LBOUND(nl,dim))
2559 * GFC_DESCRIPTOR_STRIDE(nl,dim) * nl->size);
2561 /* If we are finished with the repeat count, try to read next value. */
2563 nml_carry = 0;
2564 if (--dtp->u.p.repeat_count <= 0)
2566 if (dtp->u.p.input_complete)
2567 return true;
2568 if (dtp->u.p.at_eol)
2569 finish_separator (dtp);
2570 if (dtp->u.p.input_complete)
2571 return true;
2573 dtp->u.p.saved_type = BT_UNKNOWN;
2574 free_saved (dtp);
2576 switch (nl->type)
2578 case BT_INTEGER:
2579 read_integer (dtp, len);
2580 break;
2582 case BT_LOGICAL:
2583 read_logical (dtp, len);
2584 break;
2586 case BT_CHARACTER:
2587 read_character (dtp, len);
2588 break;
2590 case BT_REAL:
2591 /* Need to copy data back from the real location to the temp in
2592 order to handle nml reads into arrays. */
2593 read_real (dtp, pdata, len);
2594 memcpy (dtp->u.p.value, pdata, dlen);
2595 break;
2597 case BT_COMPLEX:
2598 /* Same as for REAL, copy back to temp. */
2599 read_complex (dtp, pdata, len, dlen);
2600 memcpy (dtp->u.p.value, pdata, dlen);
2601 break;
2603 case BT_DERIVED:
2604 obj_name_len = strlen (nl->var_name) + 1;
2605 obj_name = xmalloc (obj_name_len+1);
2606 memcpy (obj_name, nl->var_name, obj_name_len-1);
2607 memcpy (obj_name + obj_name_len - 1, "%", 2);
2609 /* If reading a derived type, disable the expanded read warning
2610 since a single object can have multiple reads. */
2611 dtp->u.p.expanded_read = 0;
2613 /* Now loop over the components. */
2615 for (cmp = nl->next;
2616 cmp &&
2617 !strncmp (cmp->var_name, obj_name, obj_name_len);
2618 cmp = cmp->next)
2620 /* Jump over nested derived type by testing if the potential
2621 component name contains '%'. */
2622 if (strchr (cmp->var_name + obj_name_len, '%'))
2623 continue;
2625 if (!nml_read_obj (dtp, cmp, (index_type)(pdata - nl->mem_pos),
2626 pprev_nl, nml_err_msg, nml_err_msg_size,
2627 clow, chigh))
2629 free (obj_name);
2630 return false;
2633 if (dtp->u.p.input_complete)
2635 free (obj_name);
2636 return true;
2640 free (obj_name);
2641 goto incr_idx;
2643 default:
2644 snprintf (nml_err_msg, nml_err_msg_size,
2645 "Bad type for namelist object %s", nl->var_name);
2646 internal_error (&dtp->common, nml_err_msg);
2647 goto nml_err_ret;
2651 /* The standard permits array data to stop short of the number of
2652 elements specified in the loop specification. In this case, we
2653 should be here with dtp->u.p.nml_read_error != 0. Control returns to
2654 nml_get_obj_data and an attempt is made to read object name. */
2656 *pprev_nl = nl;
2657 if (dtp->u.p.nml_read_error)
2659 dtp->u.p.expanded_read = 0;
2660 return true;
2663 if (dtp->u.p.saved_type == BT_UNKNOWN)
2665 dtp->u.p.expanded_read = 0;
2666 goto incr_idx;
2669 switch (dtp->u.p.saved_type)
2672 case BT_COMPLEX:
2673 case BT_REAL:
2674 case BT_INTEGER:
2675 case BT_LOGICAL:
2676 memcpy (pdata, dtp->u.p.value, dlen);
2677 break;
2679 case BT_CHARACTER:
2680 if (dlen < dtp->u.p.saved_used)
2682 if (compile_options.bounds_check)
2684 snprintf (nml_err_msg, nml_err_msg_size,
2685 "Namelist object '%s' truncated on read.",
2686 nl->var_name);
2687 generate_warning (&dtp->common, nml_err_msg);
2689 m = dlen;
2691 else
2692 m = dtp->u.p.saved_used;
2693 pdata = (void*)( pdata + clow - 1 );
2694 memcpy (pdata, dtp->u.p.saved_string, m);
2695 if (m < dlen)
2696 memset ((void*)( pdata + m ), ' ', dlen - m);
2697 break;
2699 default:
2700 break;
2703 /* Warn if a non-standard expanded read occurs. A single read of a
2704 single object is acceptable. If a second read occurs, issue a warning
2705 and set the flag to zero to prevent further warnings. */
2706 if (dtp->u.p.expanded_read == 2)
2708 notify_std (&dtp->common, GFC_STD_GNU, "Non-standard expanded namelist read.");
2709 dtp->u.p.expanded_read = 0;
2712 /* If the expanded read warning flag is set, increment it,
2713 indicating that a single read has occurred. */
2714 if (dtp->u.p.expanded_read >= 1)
2715 dtp->u.p.expanded_read++;
2717 /* Break out of loop if scalar. */
2718 if (!nl->var_rank)
2719 break;
2721 /* Now increment the index vector. */
2723 incr_idx:
2725 nml_carry = 1;
2726 for (dim = 0; dim < nl->var_rank; dim++)
2728 nl->ls[dim].idx += nml_carry * nl->ls[dim].step;
2729 nml_carry = 0;
2730 if (((nl->ls[dim].step > 0) && (nl->ls[dim].idx > nl->ls[dim].end))
2732 ((nl->ls[dim].step < 0) && (nl->ls[dim].idx < nl->ls[dim].end)))
2734 nl->ls[dim].idx = nl->ls[dim].start;
2735 nml_carry = 1;
2738 } while (!nml_carry);
2740 if (dtp->u.p.repeat_count > 1)
2742 snprintf (nml_err_msg, nml_err_msg_size,
2743 "Repeat count too large for namelist object %s", nl->var_name);
2744 goto nml_err_ret;
2746 return true;
2748 nml_err_ret:
2750 return false;
2753 /* Parses the object name, including array and substring qualifiers. It
2754 iterates over derived type components, touching those components and
2755 setting their loop specifications, if there is a qualifier. If the
2756 object is itself a derived type, its components and subcomponents are
2757 touched. nml_read_obj is called at the end and this reads the data in
2758 the manner specified by the object name. */
2760 static bool
2761 nml_get_obj_data (st_parameter_dt *dtp, namelist_info **pprev_nl,
2762 char *nml_err_msg, size_t nml_err_msg_size)
2764 int c;
2765 namelist_info * nl;
2766 namelist_info * first_nl = NULL;
2767 namelist_info * root_nl = NULL;
2768 int dim, parsed_rank;
2769 int component_flag, qualifier_flag;
2770 index_type clow, chigh;
2771 int non_zero_rank_count;
2773 /* Look for end of input or object name. If '?' or '=?' are encountered
2774 in stdin, print the node names or the namelist to stdout. */
2776 eat_separator (dtp);
2777 if (dtp->u.p.input_complete)
2778 return true;
2780 if (dtp->u.p.at_eol)
2781 finish_separator (dtp);
2782 if (dtp->u.p.input_complete)
2783 return true;
2785 if ((c = next_char (dtp)) == EOF)
2786 goto nml_err_ret;
2787 switch (c)
2789 case '=':
2790 if ((c = next_char (dtp)) == EOF)
2791 goto nml_err_ret;
2792 if (c != '?')
2794 snprintf (nml_err_msg, nml_err_msg_size,
2795 "namelist read: misplaced = sign");
2796 goto nml_err_ret;
2798 nml_query (dtp, '=');
2799 return true;
2801 case '?':
2802 nml_query (dtp, '?');
2803 return true;
2805 case '$':
2806 case '&':
2807 nml_match_name (dtp, "end", 3);
2808 if (dtp->u.p.nml_read_error)
2810 snprintf (nml_err_msg, nml_err_msg_size,
2811 "namelist not terminated with / or &end");
2812 goto nml_err_ret;
2814 /* Fall through. */
2815 case '/':
2816 dtp->u.p.input_complete = 1;
2817 return true;
2819 default :
2820 break;
2823 /* Untouch all nodes of the namelist and reset the flags that are set for
2824 derived type components. */
2826 nml_untouch_nodes (dtp);
2827 component_flag = 0;
2828 qualifier_flag = 0;
2829 non_zero_rank_count = 0;
2831 /* Get the object name - should '!' and '\n' be permitted separators? */
2833 get_name:
2835 free_saved (dtp);
2839 if (!is_separator (c))
2840 push_char (dtp, tolower(c));
2841 if ((c = next_char (dtp)) == EOF)
2842 goto nml_err_ret;
2844 while (!( c=='=' || c==' ' || c=='\t' || c =='(' || c =='%' ));
2846 unget_char (dtp, c);
2848 /* Check that the name is in the namelist and get pointer to object.
2849 Three error conditions exist: (i) An attempt is being made to
2850 identify a non-existent object, following a failed data read or
2851 (ii) The object name does not exist or (iii) Too many data items
2852 are present for an object. (iii) gives the same error message
2853 as (i) */
2855 push_char (dtp, '\0');
2857 if (component_flag)
2859 size_t var_len = strlen (root_nl->var_name);
2860 size_t saved_len
2861 = dtp->u.p.saved_string ? strlen (dtp->u.p.saved_string) : 0;
2862 char ext_name[var_len + saved_len + 1];
2864 memcpy (ext_name, root_nl->var_name, var_len);
2865 if (dtp->u.p.saved_string)
2866 memcpy (ext_name + var_len, dtp->u.p.saved_string, saved_len);
2867 ext_name[var_len + saved_len] = '\0';
2868 nl = find_nml_node (dtp, ext_name);
2870 else
2871 nl = find_nml_node (dtp, dtp->u.p.saved_string);
2873 if (nl == NULL)
2875 if (dtp->u.p.nml_read_error && *pprev_nl)
2876 snprintf (nml_err_msg, nml_err_msg_size,
2877 "Bad data for namelist object %s", (*pprev_nl)->var_name);
2879 else
2880 snprintf (nml_err_msg, nml_err_msg_size,
2881 "Cannot match namelist object name %s",
2882 dtp->u.p.saved_string);
2884 goto nml_err_ret;
2887 /* Get the length, data length, base pointer and rank of the variable.
2888 Set the default loop specification first. */
2890 for (dim=0; dim < nl->var_rank; dim++)
2892 nl->ls[dim].step = 1;
2893 nl->ls[dim].end = GFC_DESCRIPTOR_UBOUND(nl,dim);
2894 nl->ls[dim].start = GFC_DESCRIPTOR_LBOUND(nl,dim);
2895 nl->ls[dim].idx = nl->ls[dim].start;
2898 /* Check to see if there is a qualifier: if so, parse it.*/
2900 if (c == '(' && nl->var_rank)
2902 parsed_rank = 0;
2903 if (!nml_parse_qualifier (dtp, nl->dim, nl->ls, nl->var_rank,
2904 nl->type, nml_err_msg, nml_err_msg_size,
2905 &parsed_rank))
2907 char *nml_err_msg_end = strchr (nml_err_msg, '\0');
2908 snprintf (nml_err_msg_end,
2909 nml_err_msg_size - (nml_err_msg_end - nml_err_msg),
2910 " for namelist variable %s", nl->var_name);
2911 goto nml_err_ret;
2913 if (parsed_rank > 0)
2914 non_zero_rank_count++;
2916 qualifier_flag = 1;
2918 if ((c = next_char (dtp)) == EOF)
2919 goto nml_err_ret;
2920 unget_char (dtp, c);
2922 else if (nl->var_rank > 0)
2923 non_zero_rank_count++;
2925 /* Now parse a derived type component. The root namelist_info address
2926 is backed up, as is the previous component level. The component flag
2927 is set and the iteration is made by jumping back to get_name. */
2929 if (c == '%')
2931 if (nl->type != BT_DERIVED)
2933 snprintf (nml_err_msg, nml_err_msg_size,
2934 "Attempt to get derived component for %s", nl->var_name);
2935 goto nml_err_ret;
2938 /* Don't move first_nl further in the list if a qualifier was found. */
2939 if ((*pprev_nl == NULL && !qualifier_flag) || !component_flag)
2940 first_nl = nl;
2942 root_nl = nl;
2944 component_flag = 1;
2945 if ((c = next_char (dtp)) == EOF)
2946 goto nml_err_ret;
2947 goto get_name;
2950 /* Parse a character qualifier, if present. chigh = 0 is a default
2951 that signals that the string length = string_length. */
2953 clow = 1;
2954 chigh = 0;
2956 if (c == '(' && nl->type == BT_CHARACTER)
2958 descriptor_dimension chd[1] = { {1, clow, nl->string_length} };
2959 array_loop_spec ind[1] = { {1, clow, nl->string_length, 1} };
2961 if (!nml_parse_qualifier (dtp, chd, ind, -1, nl->type,
2962 nml_err_msg, nml_err_msg_size, &parsed_rank))
2964 char *nml_err_msg_end = strchr (nml_err_msg, '\0');
2965 snprintf (nml_err_msg_end,
2966 nml_err_msg_size - (nml_err_msg_end - nml_err_msg),
2967 " for namelist variable %s", nl->var_name);
2968 goto nml_err_ret;
2971 clow = ind[0].start;
2972 chigh = ind[0].end;
2974 if (ind[0].step != 1)
2976 snprintf (nml_err_msg, nml_err_msg_size,
2977 "Step not allowed in substring qualifier"
2978 " for namelist object %s", nl->var_name);
2979 goto nml_err_ret;
2982 if ((c = next_char (dtp)) == EOF)
2983 goto nml_err_ret;
2984 unget_char (dtp, c);
2987 /* Make sure no extraneous qualifiers are there. */
2989 if (c == '(')
2991 snprintf (nml_err_msg, nml_err_msg_size,
2992 "Qualifier for a scalar or non-character namelist object %s",
2993 nl->var_name);
2994 goto nml_err_ret;
2997 /* Make sure there is no more than one non-zero rank object. */
2998 if (non_zero_rank_count > 1)
3000 snprintf (nml_err_msg, nml_err_msg_size,
3001 "Multiple sub-objects with non-zero rank in namelist object %s",
3002 nl->var_name);
3003 non_zero_rank_count = 0;
3004 goto nml_err_ret;
3007 /* According to the standard, an equal sign MUST follow an object name. The
3008 following is possibly lax - it allows comments, blank lines and so on to
3009 intervene. eat_spaces (dtp); c = next_char (dtp); would be compliant*/
3011 free_saved (dtp);
3013 eat_separator (dtp);
3014 if (dtp->u.p.input_complete)
3015 return true;
3017 if (dtp->u.p.at_eol)
3018 finish_separator (dtp);
3019 if (dtp->u.p.input_complete)
3020 return true;
3022 if ((c = next_char (dtp)) == EOF)
3023 goto nml_err_ret;
3025 if (c != '=')
3027 snprintf (nml_err_msg, nml_err_msg_size,
3028 "Equal sign must follow namelist object name %s",
3029 nl->var_name);
3030 goto nml_err_ret;
3032 /* If a derived type, touch its components and restore the root
3033 namelist_info if we have parsed a qualified derived type
3034 component. */
3036 if (nl->type == BT_DERIVED)
3037 nml_touch_nodes (nl);
3039 if (first_nl)
3041 if (first_nl->var_rank == 0)
3043 if (component_flag && qualifier_flag)
3044 nl = first_nl;
3046 else
3047 nl = first_nl;
3050 dtp->u.p.nml_read_error = 0;
3051 if (!nml_read_obj (dtp, nl, 0, pprev_nl, nml_err_msg, nml_err_msg_size,
3052 clow, chigh))
3053 goto nml_err_ret;
3055 return true;
3057 nml_err_ret:
3059 /* The EOF error message is issued by hit_eof. Return true so that the
3060 caller does not use nml_err_msg and nml_err_msg_size to generate
3061 an unrelated error message. */
3062 if (c == EOF)
3064 dtp->u.p.input_complete = 1;
3065 unget_char (dtp, c);
3066 hit_eof (dtp);
3067 return true;
3069 return false;
3072 /* Entry point for namelist input. Goes through input until namelist name
3073 is matched. Then cycles through nml_get_obj_data until the input is
3074 completed or there is an error. */
3076 void
3077 namelist_read (st_parameter_dt *dtp)
3079 int c;
3080 char nml_err_msg[200];
3082 /* Initialize the error string buffer just in case we get an unexpected fail
3083 somewhere and end up at nml_err_ret. */
3084 strcpy (nml_err_msg, "Internal namelist read error");
3086 /* Pointer to the previously read object, in case attempt is made to read
3087 new object name. Should this fail, error message can give previous
3088 name. */
3089 namelist_info *prev_nl = NULL;
3091 dtp->u.p.namelist_mode = 1;
3092 dtp->u.p.input_complete = 0;
3093 dtp->u.p.expanded_read = 0;
3095 /* Look for &namelist_name . Skip all characters, testing for $nmlname.
3096 Exit on success or EOF. If '?' or '=?' encountered in stdin, print
3097 node names or namelist on stdout. */
3099 find_nml_name:
3100 c = next_char (dtp);
3101 switch (c)
3103 case '$':
3104 case '&':
3105 break;
3107 case '!':
3108 eat_line (dtp);
3109 goto find_nml_name;
3111 case '=':
3112 c = next_char (dtp);
3113 if (c == '?')
3114 nml_query (dtp, '=');
3115 else
3116 unget_char (dtp, c);
3117 goto find_nml_name;
3119 case '?':
3120 nml_query (dtp, '?');
3121 goto find_nml_name;
3123 case EOF:
3124 return;
3126 default:
3127 goto find_nml_name;
3130 /* Match the name of the namelist. */
3132 nml_match_name (dtp, dtp->namelist_name, dtp->namelist_name_len);
3134 if (dtp->u.p.nml_read_error)
3135 goto find_nml_name;
3137 /* A trailing space is required, we give a little latitude here, 10.9.1. */
3138 c = next_char (dtp);
3139 if (!is_separator(c) && c != '!')
3141 unget_char (dtp, c);
3142 goto find_nml_name;
3145 unget_char (dtp, c);
3146 eat_separator (dtp);
3148 /* Ready to read namelist objects. If there is an error in input
3149 from stdin, output the error message and continue. */
3151 while (!dtp->u.p.input_complete)
3153 if (!nml_get_obj_data (dtp, &prev_nl, nml_err_msg, sizeof nml_err_msg))
3155 if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
3156 goto nml_err_ret;
3157 generate_error (&dtp->common, LIBERROR_READ_VALUE, nml_err_msg);
3160 /* Reset the previous namelist pointer if we know we are not going
3161 to be doing multiple reads within a single namelist object. */
3162 if (prev_nl && prev_nl->var_rank == 0)
3163 prev_nl = NULL;
3166 free_saved (dtp);
3167 free_line (dtp);
3168 return;
3171 nml_err_ret:
3173 /* All namelist error calls return from here */
3174 free_saved (dtp);
3175 free_line (dtp);
3176 generate_error (&dtp->common, LIBERROR_READ_VALUE, nml_err_msg);
3177 return;