gcc/
[official-gcc.git] / libgfortran / io / list_read.c
blob942f311410fcfb43a4f54701021c6703b22f8d51
1 /* Copyright (C) 2002-2014 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.line_buffer_pos = 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.line_buffer_pos];
154 if (c != '\0' && dtp->u.p.line_buffer_pos < 64)
156 dtp->u.p.line_buffer[dtp->u.p.line_buffer_pos] = '\0';
157 dtp->u.p.line_buffer_pos++;
158 goto done;
161 dtp->u.p.line_buffer_pos = 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.line_buffer_pos++] = 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.line_buffer_pos = 0;
753 return;
758 bad_logical:
760 if (nml_bad_return (dtp, c))
762 free_line (dtp);
763 return;
767 free_saved (dtp);
768 if (c == EOF)
770 free_line (dtp);
771 hit_eof (dtp);
772 return;
774 else if (c != '\n')
775 eat_line (dtp);
776 snprintf (message, MSGLEN, "Bad logical value while reading item %d",
777 dtp->u.p.item_count);
778 free_line (dtp);
779 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
780 return;
782 logical_done:
784 dtp->u.p.saved_type = BT_LOGICAL;
785 dtp->u.p.saved_length = length;
786 set_integer ((int *) dtp->u.p.value, v, length);
787 free_saved (dtp);
788 free_line (dtp);
792 /* Reading integers is tricky because we can actually be reading a
793 repeat count. We have to store the characters in a buffer because
794 we could be reading an integer that is larger than the default int
795 used for repeat counts. */
797 static void
798 read_integer (st_parameter_dt *dtp, int length)
800 char message[MSGLEN];
801 int c, negative;
803 negative = 0;
805 c = next_char (dtp);
806 switch (c)
808 case '-':
809 negative = 1;
810 /* Fall through... */
812 case '+':
813 if ((c = next_char (dtp)) == EOF)
814 goto bad_integer;
815 goto get_integer;
817 CASE_SEPARATORS: /* Single null. */
818 unget_char (dtp, c);
819 eat_separator (dtp);
820 return;
822 CASE_DIGITS:
823 push_char (dtp, c);
824 break;
826 default:
827 goto bad_integer;
830 /* Take care of what may be a repeat count. */
832 for (;;)
834 c = next_char (dtp);
835 switch (c)
837 CASE_DIGITS:
838 push_char (dtp, c);
839 break;
841 case '*':
842 push_char (dtp, '\0');
843 goto repeat;
845 CASE_SEPARATORS: /* Not a repeat count. */
846 case EOF:
847 goto done;
849 default:
850 goto bad_integer;
854 repeat:
855 if (convert_integer (dtp, -1, 0))
856 return;
858 /* Get the real integer. */
860 if ((c = next_char (dtp)) == EOF)
861 goto bad_integer;
862 switch (c)
864 CASE_DIGITS:
865 break;
867 CASE_SEPARATORS:
868 unget_char (dtp, c);
869 eat_separator (dtp);
870 return;
872 case '-':
873 negative = 1;
874 /* Fall through... */
876 case '+':
877 c = next_char (dtp);
878 break;
881 get_integer:
882 if (!isdigit (c))
883 goto bad_integer;
884 push_char (dtp, c);
886 for (;;)
888 c = next_char (dtp);
889 switch (c)
891 CASE_DIGITS:
892 push_char (dtp, c);
893 break;
895 CASE_SEPARATORS:
896 case EOF:
897 goto done;
899 default:
900 goto bad_integer;
904 bad_integer:
906 if (nml_bad_return (dtp, c))
907 return;
909 free_saved (dtp);
910 if (c == EOF)
912 free_line (dtp);
913 hit_eof (dtp);
914 return;
916 else if (c != '\n')
917 eat_line (dtp);
919 snprintf (message, MSGLEN, "Bad integer for item %d in list input",
920 dtp->u.p.item_count);
921 free_line (dtp);
922 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
924 return;
926 done:
927 unget_char (dtp, c);
928 eat_separator (dtp);
930 push_char (dtp, '\0');
931 if (convert_integer (dtp, length, negative))
933 free_saved (dtp);
934 return;
937 free_saved (dtp);
938 dtp->u.p.saved_type = BT_INTEGER;
942 /* Read a character variable. */
944 static void
945 read_character (st_parameter_dt *dtp, int length __attribute__ ((unused)))
947 char quote, message[MSGLEN];
948 int c;
950 quote = ' '; /* Space means no quote character. */
952 if ((c = next_char (dtp)) == EOF)
953 goto eof;
954 switch (c)
956 CASE_DIGITS:
957 push_char (dtp, c);
958 break;
960 CASE_SEPARATORS:
961 case EOF:
962 unget_char (dtp, c); /* NULL value. */
963 eat_separator (dtp);
964 return;
966 case '"':
967 case '\'':
968 quote = c;
969 goto get_string;
971 default:
972 if (dtp->u.p.namelist_mode)
974 unget_char (dtp, c);
975 return;
978 push_char (dtp, c);
979 goto get_string;
982 /* Deal with a possible repeat count. */
984 for (;;)
986 c = next_char (dtp);
987 switch (c)
989 CASE_DIGITS:
990 push_char (dtp, c);
991 break;
993 CASE_SEPARATORS:
994 case EOF:
995 unget_char (dtp, c);
996 goto done; /* String was only digits! */
998 case '*':
999 push_char (dtp, '\0');
1000 goto got_repeat;
1002 default:
1003 push_char (dtp, c);
1004 goto get_string; /* Not a repeat count after all. */
1008 got_repeat:
1009 if (convert_integer (dtp, -1, 0))
1010 return;
1012 /* Now get the real string. */
1014 if ((c = next_char (dtp)) == EOF)
1015 goto eof;
1016 switch (c)
1018 CASE_SEPARATORS:
1019 unget_char (dtp, c); /* Repeated NULL values. */
1020 eat_separator (dtp);
1021 return;
1023 case '"':
1024 case '\'':
1025 quote = c;
1026 break;
1028 default:
1029 push_char (dtp, c);
1030 break;
1033 get_string:
1034 for (;;)
1036 if ((c = next_char (dtp)) == EOF)
1037 goto done_eof;
1038 switch (c)
1040 case '"':
1041 case '\'':
1042 if (c != quote)
1044 push_char (dtp, c);
1045 break;
1048 /* See if we have a doubled quote character or the end of
1049 the string. */
1051 if ((c = next_char (dtp)) == EOF)
1052 goto done_eof;
1053 if (c == quote)
1055 push_char (dtp, quote);
1056 break;
1059 unget_char (dtp, c);
1060 goto done;
1062 CASE_SEPARATORS:
1063 if (quote == ' ')
1065 unget_char (dtp, c);
1066 goto done;
1069 if (c != '\n' && c != '\r')
1070 push_char (dtp, c);
1071 break;
1073 default:
1074 push_char (dtp, c);
1075 break;
1079 /* At this point, we have to have a separator, or else the string is
1080 invalid. */
1081 done:
1082 c = next_char (dtp);
1083 done_eof:
1084 if (is_separator (c) || c == '!' || c == EOF)
1086 unget_char (dtp, c);
1087 eat_separator (dtp);
1088 dtp->u.p.saved_type = BT_CHARACTER;
1090 else
1092 free_saved (dtp);
1093 snprintf (message, MSGLEN, "Invalid string input in item %d",
1094 dtp->u.p.item_count);
1095 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1097 free_line (dtp);
1098 return;
1100 eof:
1101 free_saved (dtp);
1102 free_line (dtp);
1103 hit_eof (dtp);
1107 /* Parse a component of a complex constant or a real number that we
1108 are sure is already there. This is a straight real number parser. */
1110 static int
1111 parse_real (st_parameter_dt *dtp, void *buffer, int length)
1113 char message[MSGLEN];
1114 int c, m, seen_dp;
1116 if ((c = next_char (dtp)) == EOF)
1117 goto bad;
1119 if (c == '-' || c == '+')
1121 push_char (dtp, c);
1122 if ((c = next_char (dtp)) == EOF)
1123 goto bad;
1126 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1127 c = '.';
1129 if (!isdigit (c) && c != '.')
1131 if (c == 'i' || c == 'I' || c == 'n' || c == 'N')
1132 goto inf_nan;
1133 else
1134 goto bad;
1137 push_char (dtp, c);
1139 seen_dp = (c == '.') ? 1 : 0;
1141 for (;;)
1143 if ((c = next_char (dtp)) == EOF)
1144 goto bad;
1145 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1146 c = '.';
1147 switch (c)
1149 CASE_DIGITS:
1150 push_char (dtp, c);
1151 break;
1153 case '.':
1154 if (seen_dp)
1155 goto bad;
1157 seen_dp = 1;
1158 push_char (dtp, c);
1159 break;
1161 case 'e':
1162 case 'E':
1163 case 'd':
1164 case 'D':
1165 case 'q':
1166 case 'Q':
1167 push_char (dtp, 'e');
1168 goto exp1;
1170 case '-':
1171 case '+':
1172 push_char (dtp, 'e');
1173 push_char (dtp, c);
1174 if ((c = next_char (dtp)) == EOF)
1175 goto bad;
1176 goto exp2;
1178 CASE_SEPARATORS:
1179 case EOF:
1180 goto done;
1182 default:
1183 goto done;
1187 exp1:
1188 if ((c = next_char (dtp)) == EOF)
1189 goto bad;
1190 if (c != '-' && c != '+')
1191 push_char (dtp, '+');
1192 else
1194 push_char (dtp, c);
1195 c = next_char (dtp);
1198 exp2:
1199 if (!isdigit (c))
1200 goto bad;
1202 push_char (dtp, c);
1204 for (;;)
1206 if ((c = next_char (dtp)) == EOF)
1207 goto bad;
1208 switch (c)
1210 CASE_DIGITS:
1211 push_char (dtp, c);
1212 break;
1214 CASE_SEPARATORS:
1215 case EOF:
1216 unget_char (dtp, c);
1217 goto done;
1219 default:
1220 goto done;
1224 done:
1225 unget_char (dtp, c);
1226 push_char (dtp, '\0');
1228 m = convert_real (dtp, buffer, dtp->u.p.saved_string, length);
1229 free_saved (dtp);
1231 return m;
1233 done_infnan:
1234 unget_char (dtp, c);
1235 push_char (dtp, '\0');
1237 m = convert_infnan (dtp, buffer, dtp->u.p.saved_string, length);
1238 free_saved (dtp);
1240 return m;
1242 inf_nan:
1243 /* Match INF and Infinity. */
1244 if ((c == 'i' || c == 'I')
1245 && ((c = next_char (dtp)) == 'n' || c == 'N')
1246 && ((c = next_char (dtp)) == 'f' || c == 'F'))
1248 c = next_char (dtp);
1249 if ((c != 'i' && c != 'I')
1250 || ((c == 'i' || c == 'I')
1251 && ((c = next_char (dtp)) == 'n' || c == 'N')
1252 && ((c = next_char (dtp)) == 'i' || c == 'I')
1253 && ((c = next_char (dtp)) == 't' || c == 'T')
1254 && ((c = next_char (dtp)) == 'y' || c == 'Y')
1255 && (c = next_char (dtp))))
1257 if (is_separator (c) || (c == EOF))
1258 unget_char (dtp, c);
1259 push_char (dtp, 'i');
1260 push_char (dtp, 'n');
1261 push_char (dtp, 'f');
1262 goto done_infnan;
1264 } /* Match NaN. */
1265 else if (((c = next_char (dtp)) == 'a' || c == 'A')
1266 && ((c = next_char (dtp)) == 'n' || c == 'N')
1267 && (c = next_char (dtp)))
1269 if (is_separator (c) || (c == EOF))
1270 unget_char (dtp, c);
1271 push_char (dtp, 'n');
1272 push_char (dtp, 'a');
1273 push_char (dtp, 'n');
1275 /* Match "NAN(alphanum)". */
1276 if (c == '(')
1278 for ( ; c != ')'; c = next_char (dtp))
1279 if (is_separator (c))
1280 goto bad;
1282 c = next_char (dtp);
1283 if (is_separator (c) || (c == EOF))
1284 unget_char (dtp, c);
1286 goto done_infnan;
1289 bad:
1291 if (nml_bad_return (dtp, c))
1292 return 0;
1294 free_saved (dtp);
1295 if (c == EOF)
1297 free_line (dtp);
1298 hit_eof (dtp);
1299 return 1;
1301 else if (c != '\n')
1302 eat_line (dtp);
1304 snprintf (message, MSGLEN, "Bad floating point number for item %d",
1305 dtp->u.p.item_count);
1306 free_line (dtp);
1307 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1309 return 1;
1313 /* Reading a complex number is straightforward because we can tell
1314 what it is right away. */
1316 static void
1317 read_complex (st_parameter_dt *dtp, void * dest, int kind, size_t size)
1319 char message[MSGLEN];
1320 int c;
1322 if (parse_repeat (dtp))
1323 return;
1325 c = next_char (dtp);
1326 switch (c)
1328 case '(':
1329 break;
1331 CASE_SEPARATORS:
1332 case EOF:
1333 unget_char (dtp, c);
1334 eat_separator (dtp);
1335 return;
1337 default:
1338 goto bad_complex;
1341 eol_1:
1342 eat_spaces (dtp);
1343 c = next_char (dtp);
1344 if (c == '\n' || c== '\r')
1345 goto eol_1;
1346 else
1347 unget_char (dtp, c);
1349 if (parse_real (dtp, dest, kind))
1350 return;
1352 eol_2:
1353 eat_spaces (dtp);
1354 c = next_char (dtp);
1355 if (c == '\n' || c== '\r')
1356 goto eol_2;
1357 else
1358 unget_char (dtp, c);
1360 if (next_char (dtp)
1361 != (dtp->u.p.current_unit->decimal_status == DECIMAL_POINT ? ',' : ';'))
1362 goto bad_complex;
1364 eol_3:
1365 eat_spaces (dtp);
1366 c = next_char (dtp);
1367 if (c == '\n' || c== '\r')
1368 goto eol_3;
1369 else
1370 unget_char (dtp, c);
1372 if (parse_real (dtp, dest + size / 2, kind))
1373 return;
1375 eol_4:
1376 eat_spaces (dtp);
1377 c = next_char (dtp);
1378 if (c == '\n' || c== '\r')
1379 goto eol_4;
1380 else
1381 unget_char (dtp, c);
1383 if (next_char (dtp) != ')')
1384 goto bad_complex;
1386 c = next_char (dtp);
1387 if (!is_separator (c) && (c != EOF))
1388 goto bad_complex;
1390 unget_char (dtp, c);
1391 eat_separator (dtp);
1393 free_saved (dtp);
1394 dtp->u.p.saved_type = BT_COMPLEX;
1395 return;
1397 bad_complex:
1399 if (nml_bad_return (dtp, c))
1400 return;
1402 free_saved (dtp);
1403 if (c == EOF)
1405 free_line (dtp);
1406 hit_eof (dtp);
1407 return;
1409 else if (c != '\n')
1410 eat_line (dtp);
1412 snprintf (message, MSGLEN, "Bad complex value in item %d of list input",
1413 dtp->u.p.item_count);
1414 free_line (dtp);
1415 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1419 /* Parse a real number with a possible repeat count. */
1421 static void
1422 read_real (st_parameter_dt *dtp, void * dest, int length)
1424 char message[MSGLEN];
1425 int c;
1426 int seen_dp;
1427 int is_inf;
1429 seen_dp = 0;
1431 c = next_char (dtp);
1432 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1433 c = '.';
1434 switch (c)
1436 CASE_DIGITS:
1437 push_char (dtp, c);
1438 break;
1440 case '.':
1441 push_char (dtp, c);
1442 seen_dp = 1;
1443 break;
1445 case '+':
1446 case '-':
1447 goto got_sign;
1449 CASE_SEPARATORS:
1450 unget_char (dtp, c); /* Single null. */
1451 eat_separator (dtp);
1452 return;
1454 case 'i':
1455 case 'I':
1456 case 'n':
1457 case 'N':
1458 goto inf_nan;
1460 default:
1461 goto bad_real;
1464 /* Get the digit string that might be a repeat count. */
1466 for (;;)
1468 c = next_char (dtp);
1469 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1470 c = '.';
1471 switch (c)
1473 CASE_DIGITS:
1474 push_char (dtp, c);
1475 break;
1477 case '.':
1478 if (seen_dp)
1479 goto bad_real;
1481 seen_dp = 1;
1482 push_char (dtp, c);
1483 goto real_loop;
1485 case 'E':
1486 case 'e':
1487 case 'D':
1488 case 'd':
1489 case 'Q':
1490 case 'q':
1491 goto exp1;
1493 case '+':
1494 case '-':
1495 push_char (dtp, 'e');
1496 push_char (dtp, c);
1497 c = next_char (dtp);
1498 goto exp2;
1500 case '*':
1501 push_char (dtp, '\0');
1502 goto got_repeat;
1504 CASE_SEPARATORS:
1505 case EOF:
1506 if (c != '\n' && c != ',' && c != '\r' && c != ';')
1507 unget_char (dtp, c);
1508 goto done;
1510 default:
1511 goto bad_real;
1515 got_repeat:
1516 if (convert_integer (dtp, -1, 0))
1517 return;
1519 /* Now get the number itself. */
1521 if ((c = next_char (dtp)) == EOF)
1522 goto bad_real;
1523 if (is_separator (c))
1524 { /* Repeated null value. */
1525 unget_char (dtp, c);
1526 eat_separator (dtp);
1527 return;
1530 if (c != '-' && c != '+')
1531 push_char (dtp, '+');
1532 else
1534 got_sign:
1535 push_char (dtp, c);
1536 if ((c = next_char (dtp)) == EOF)
1537 goto bad_real;
1540 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1541 c = '.';
1543 if (!isdigit (c) && c != '.')
1545 if (c == 'i' || c == 'I' || c == 'n' || c == 'N')
1546 goto inf_nan;
1547 else
1548 goto bad_real;
1551 if (c == '.')
1553 if (seen_dp)
1554 goto bad_real;
1555 else
1556 seen_dp = 1;
1559 push_char (dtp, c);
1561 real_loop:
1562 for (;;)
1564 c = next_char (dtp);
1565 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1566 c = '.';
1567 switch (c)
1569 CASE_DIGITS:
1570 push_char (dtp, c);
1571 break;
1573 CASE_SEPARATORS:
1574 case EOF:
1575 goto done;
1577 case '.':
1578 if (seen_dp)
1579 goto bad_real;
1581 seen_dp = 1;
1582 push_char (dtp, c);
1583 break;
1585 case 'E':
1586 case 'e':
1587 case 'D':
1588 case 'd':
1589 case 'Q':
1590 case 'q':
1591 goto exp1;
1593 case '+':
1594 case '-':
1595 push_char (dtp, 'e');
1596 push_char (dtp, c);
1597 c = next_char (dtp);
1598 goto exp2;
1600 default:
1601 goto bad_real;
1605 exp1:
1606 push_char (dtp, 'e');
1608 if ((c = next_char (dtp)) == EOF)
1609 goto bad_real;
1610 if (c != '+' && c != '-')
1611 push_char (dtp, '+');
1612 else
1614 push_char (dtp, c);
1615 c = next_char (dtp);
1618 exp2:
1619 if (!isdigit (c))
1620 goto bad_real;
1621 push_char (dtp, c);
1623 for (;;)
1625 c = next_char (dtp);
1627 switch (c)
1629 CASE_DIGITS:
1630 push_char (dtp, c);
1631 break;
1633 CASE_SEPARATORS:
1634 case EOF:
1635 goto done;
1637 default:
1638 goto bad_real;
1642 done:
1643 unget_char (dtp, c);
1644 eat_separator (dtp);
1645 push_char (dtp, '\0');
1646 if (convert_real (dtp, dest, dtp->u.p.saved_string, length))
1648 free_saved (dtp);
1649 return;
1652 free_saved (dtp);
1653 dtp->u.p.saved_type = BT_REAL;
1654 return;
1656 inf_nan:
1657 l_push_char (dtp, c);
1658 is_inf = 0;
1660 /* Match INF and Infinity. */
1661 if (c == 'i' || c == 'I')
1663 c = next_char (dtp);
1664 l_push_char (dtp, c);
1665 if (c != 'n' && c != 'N')
1666 goto unwind;
1667 c = next_char (dtp);
1668 l_push_char (dtp, c);
1669 if (c != 'f' && c != 'F')
1670 goto unwind;
1671 c = next_char (dtp);
1672 l_push_char (dtp, c);
1673 if (!is_separator (c) && (c != EOF))
1675 if (c != 'i' && c != 'I')
1676 goto unwind;
1677 c = next_char (dtp);
1678 l_push_char (dtp, c);
1679 if (c != 'n' && c != 'N')
1680 goto unwind;
1681 c = next_char (dtp);
1682 l_push_char (dtp, c);
1683 if (c != 'i' && c != 'I')
1684 goto unwind;
1685 c = next_char (dtp);
1686 l_push_char (dtp, c);
1687 if (c != 't' && c != 'T')
1688 goto unwind;
1689 c = next_char (dtp);
1690 l_push_char (dtp, c);
1691 if (c != 'y' && c != 'Y')
1692 goto unwind;
1693 c = next_char (dtp);
1694 l_push_char (dtp, c);
1696 is_inf = 1;
1697 } /* Match NaN. */
1698 else
1700 c = next_char (dtp);
1701 l_push_char (dtp, c);
1702 if (c != 'a' && c != 'A')
1703 goto unwind;
1704 c = next_char (dtp);
1705 l_push_char (dtp, c);
1706 if (c != 'n' && c != 'N')
1707 goto unwind;
1708 c = next_char (dtp);
1709 l_push_char (dtp, c);
1711 /* Match NAN(alphanum). */
1712 if (c == '(')
1714 for (c = next_char (dtp); c != ')'; c = next_char (dtp))
1715 if (is_separator (c))
1716 goto unwind;
1717 else
1718 l_push_char (dtp, c);
1720 l_push_char (dtp, ')');
1721 c = next_char (dtp);
1722 l_push_char (dtp, c);
1726 if (!is_separator (c) && (c != EOF))
1727 goto unwind;
1729 if (dtp->u.p.namelist_mode)
1731 if (c == ' ' || c =='\n' || c == '\r')
1735 if ((c = next_char (dtp)) == EOF)
1736 goto bad_real;
1738 while (c == ' ' || c =='\n' || c == '\r');
1740 l_push_char (dtp, c);
1742 if (c == '=')
1743 goto unwind;
1747 if (is_inf)
1749 push_char (dtp, 'i');
1750 push_char (dtp, 'n');
1751 push_char (dtp, 'f');
1753 else
1755 push_char (dtp, 'n');
1756 push_char (dtp, 'a');
1757 push_char (dtp, 'n');
1760 free_line (dtp);
1761 unget_char (dtp, c);
1762 eat_separator (dtp);
1763 push_char (dtp, '\0');
1764 if (convert_infnan (dtp, dest, dtp->u.p.saved_string, length))
1765 return;
1767 free_saved (dtp);
1768 dtp->u.p.saved_type = BT_REAL;
1769 return;
1771 unwind:
1772 if (dtp->u.p.namelist_mode)
1774 dtp->u.p.nml_read_error = 1;
1775 dtp->u.p.line_buffer_enabled = 1;
1776 dtp->u.p.line_buffer_pos = 0;
1777 return;
1780 bad_real:
1782 if (nml_bad_return (dtp, c))
1783 return;
1785 free_saved (dtp);
1786 if (c == EOF)
1788 free_line (dtp);
1789 hit_eof (dtp);
1790 return;
1792 else if (c != '\n')
1793 eat_line (dtp);
1795 snprintf (message, MSGLEN, "Bad real number in item %d of list input",
1796 dtp->u.p.item_count);
1797 free_line (dtp);
1798 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1802 /* Check the current type against the saved type to make sure they are
1803 compatible. Returns nonzero if incompatible. */
1805 static int
1806 check_type (st_parameter_dt *dtp, bt type, int kind)
1808 char message[MSGLEN];
1810 if (dtp->u.p.saved_type != BT_UNKNOWN && dtp->u.p.saved_type != type)
1812 snprintf (message, MSGLEN, "Read type %s where %s was expected for item %d",
1813 type_name (dtp->u.p.saved_type), type_name (type),
1814 dtp->u.p.item_count);
1815 free_line (dtp);
1816 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1817 return 1;
1820 if (dtp->u.p.saved_type == BT_UNKNOWN || dtp->u.p.saved_type == BT_CHARACTER)
1821 return 0;
1823 if ((type != BT_COMPLEX && dtp->u.p.saved_length != kind)
1824 || (type == BT_COMPLEX && dtp->u.p.saved_length != kind*2))
1826 snprintf (message, MSGLEN,
1827 "Read kind %d %s where kind %d is required for item %d",
1828 type == BT_COMPLEX ? dtp->u.p.saved_length / 2
1829 : dtp->u.p.saved_length,
1830 type_name (dtp->u.p.saved_type), kind,
1831 dtp->u.p.item_count);
1832 free_line (dtp);
1833 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1834 return 1;
1837 return 0;
1841 /* Top level data transfer subroutine for list reads. Because we have
1842 to deal with repeat counts, the data item is always saved after
1843 reading, usually in the dtp->u.p.value[] array. If a repeat count is
1844 greater than one, we copy the data item multiple times. */
1846 static int
1847 list_formatted_read_scalar (st_parameter_dt *dtp, bt type, void *p,
1848 int kind, size_t size)
1850 gfc_char4_t *q;
1851 int c, i, m;
1852 int err = 0;
1854 dtp->u.p.namelist_mode = 0;
1856 if (dtp->u.p.first_item)
1858 dtp->u.p.first_item = 0;
1859 dtp->u.p.input_complete = 0;
1860 dtp->u.p.repeat_count = 1;
1861 dtp->u.p.at_eol = 0;
1863 if ((c = eat_spaces (dtp)) == EOF)
1865 err = LIBERROR_END;
1866 goto cleanup;
1868 if (is_separator (c))
1870 /* Found a null value. */
1871 eat_separator (dtp);
1872 dtp->u.p.repeat_count = 0;
1874 /* eat_separator sets this flag if the separator was a comma. */
1875 if (dtp->u.p.comma_flag)
1876 goto cleanup;
1878 /* eat_separator sets this flag if the separator was a \n or \r. */
1879 if (dtp->u.p.at_eol)
1880 finish_separator (dtp);
1881 else
1882 goto cleanup;
1886 else
1888 if (dtp->u.p.repeat_count > 0)
1890 if (check_type (dtp, type, kind))
1891 return err;
1892 goto set_value;
1895 if (dtp->u.p.input_complete)
1896 goto cleanup;
1898 if (dtp->u.p.at_eol)
1899 finish_separator (dtp);
1900 else
1902 eat_spaces (dtp);
1903 /* Trailing spaces prior to end of line. */
1904 if (dtp->u.p.at_eol)
1905 finish_separator (dtp);
1908 dtp->u.p.saved_type = BT_UNKNOWN;
1909 dtp->u.p.repeat_count = 1;
1912 switch (type)
1914 case BT_INTEGER:
1915 read_integer (dtp, kind);
1916 break;
1917 case BT_LOGICAL:
1918 read_logical (dtp, kind);
1919 break;
1920 case BT_CHARACTER:
1921 read_character (dtp, kind);
1922 break;
1923 case BT_REAL:
1924 read_real (dtp, p, kind);
1925 /* Copy value back to temporary if needed. */
1926 if (dtp->u.p.repeat_count > 0)
1927 memcpy (dtp->u.p.value, p, size);
1928 break;
1929 case BT_COMPLEX:
1930 read_complex (dtp, p, kind, size);
1931 /* Copy value back to temporary if needed. */
1932 if (dtp->u.p.repeat_count > 0)
1933 memcpy (dtp->u.p.value, p, size);
1934 break;
1935 default:
1936 internal_error (&dtp->common, "Bad type for list read");
1939 if (dtp->u.p.saved_type != BT_CHARACTER && dtp->u.p.saved_type != BT_UNKNOWN)
1940 dtp->u.p.saved_length = size;
1942 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1943 goto cleanup;
1945 set_value:
1946 switch (dtp->u.p.saved_type)
1948 case BT_COMPLEX:
1949 case BT_REAL:
1950 if (dtp->u.p.repeat_count > 0)
1951 memcpy (p, dtp->u.p.value, size);
1952 break;
1954 case BT_INTEGER:
1955 case BT_LOGICAL:
1956 memcpy (p, dtp->u.p.value, size);
1957 break;
1959 case BT_CHARACTER:
1960 if (dtp->u.p.saved_string)
1962 m = ((int) size < dtp->u.p.saved_used)
1963 ? (int) size : dtp->u.p.saved_used;
1964 if (kind == 1)
1965 memcpy (p, dtp->u.p.saved_string, m);
1966 else
1968 q = (gfc_char4_t *) p;
1969 for (i = 0; i < m; i++)
1970 q[i] = (unsigned char) dtp->u.p.saved_string[i];
1973 else
1974 /* Just delimiters encountered, nothing to copy but SPACE. */
1975 m = 0;
1977 if (m < (int) size)
1979 if (kind == 1)
1980 memset (((char *) p) + m, ' ', size - m);
1981 else
1983 q = (gfc_char4_t *) p;
1984 for (i = m; i < (int) size; i++)
1985 q[i] = (unsigned char) ' ';
1988 break;
1990 case BT_UNKNOWN:
1991 break;
1993 default:
1994 internal_error (&dtp->common, "Bad type for list read");
1997 if (--dtp->u.p.repeat_count <= 0)
1998 free_saved (dtp);
2000 cleanup:
2001 if (err == LIBERROR_END)
2003 free_line (dtp);
2004 hit_eof (dtp);
2006 return err;
2010 void
2011 list_formatted_read (st_parameter_dt *dtp, bt type, void *p, int kind,
2012 size_t size, size_t nelems)
2014 size_t elem;
2015 char *tmp;
2016 size_t stride = type == BT_CHARACTER ?
2017 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
2018 int err;
2020 tmp = (char *) p;
2022 /* Big loop over all the elements. */
2023 for (elem = 0; elem < nelems; elem++)
2025 dtp->u.p.item_count++;
2026 err = list_formatted_read_scalar (dtp, type, tmp + stride*elem,
2027 kind, size);
2028 if (err)
2029 break;
2034 /* Finish a list read. */
2036 void
2037 finish_list_read (st_parameter_dt *dtp)
2039 int err;
2041 free_saved (dtp);
2043 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
2045 if (dtp->u.p.at_eol)
2047 dtp->u.p.at_eol = 0;
2048 return;
2051 err = eat_line (dtp);
2052 if (err == LIBERROR_END)
2054 free_line (dtp);
2055 hit_eof (dtp);
2059 /* NAMELIST INPUT
2061 void namelist_read (st_parameter_dt *dtp)
2062 calls:
2063 static void nml_match_name (char *name, int len)
2064 static int nml_query (st_parameter_dt *dtp)
2065 static int nml_get_obj_data (st_parameter_dt *dtp,
2066 namelist_info **prev_nl, char *, size_t)
2067 calls:
2068 static void nml_untouch_nodes (st_parameter_dt *dtp)
2069 static namelist_info * find_nml_node (st_parameter_dt *dtp,
2070 char * var_name)
2071 static int nml_parse_qualifier(descriptor_dimension * ad,
2072 array_loop_spec * ls, int rank, char *)
2073 static void nml_touch_nodes (namelist_info * nl)
2074 static int nml_read_obj (namelist_info *nl, index_type offset,
2075 namelist_info **prev_nl, char *, size_t,
2076 index_type clow, index_type chigh)
2077 calls:
2078 -itself- */
2080 /* Inputs a rank-dimensional qualifier, which can contain
2081 singlets, doublets, triplets or ':' with the standard meanings. */
2083 static bool
2084 nml_parse_qualifier (st_parameter_dt *dtp, descriptor_dimension *ad,
2085 array_loop_spec *ls, int rank, bt nml_elem_type,
2086 char *parse_err_msg, size_t parse_err_msg_size,
2087 int *parsed_rank)
2089 int dim;
2090 int indx;
2091 int neg;
2092 int null_flag;
2093 int is_array_section, is_char;
2094 int c;
2096 is_char = 0;
2097 is_array_section = 0;
2098 dtp->u.p.expanded_read = 0;
2100 /* See if this is a character substring qualifier we are looking for. */
2101 if (rank == -1)
2103 rank = 1;
2104 is_char = 1;
2107 /* The next character in the stream should be the '('. */
2109 if ((c = next_char (dtp)) == EOF)
2110 goto err_ret;
2112 /* Process the qualifier, by dimension and triplet. */
2114 for (dim=0; dim < rank; dim++ )
2116 for (indx=0; indx<3; indx++)
2118 free_saved (dtp);
2119 eat_spaces (dtp);
2120 neg = 0;
2122 /* Process a potential sign. */
2123 if ((c = next_char (dtp)) == EOF)
2124 goto err_ret;
2125 switch (c)
2127 case '-':
2128 neg = 1;
2129 break;
2131 case '+':
2132 break;
2134 default:
2135 unget_char (dtp, c);
2136 break;
2139 /* Process characters up to the next ':' , ',' or ')'. */
2140 for (;;)
2142 c = next_char (dtp);
2143 switch (c)
2145 case EOF:
2146 goto err_ret;
2148 case ':':
2149 is_array_section = 1;
2150 break;
2152 case ',': case ')':
2153 if ((c==',' && dim == rank -1)
2154 || (c==')' && dim < rank -1))
2156 if (is_char)
2157 snprintf (parse_err_msg, parse_err_msg_size,
2158 "Bad substring qualifier");
2159 else
2160 snprintf (parse_err_msg, parse_err_msg_size,
2161 "Bad number of index fields");
2162 goto err_ret;
2164 break;
2166 CASE_DIGITS:
2167 push_char (dtp, c);
2168 continue;
2170 case ' ': case '\t': case '\r': case '\n':
2171 eat_spaces (dtp);
2172 break;
2174 default:
2175 if (is_char)
2176 snprintf (parse_err_msg, parse_err_msg_size,
2177 "Bad character in substring qualifier");
2178 else
2179 snprintf (parse_err_msg, parse_err_msg_size,
2180 "Bad character in index");
2181 goto err_ret;
2184 if ((c == ',' || c == ')') && indx == 0
2185 && dtp->u.p.saved_string == 0)
2187 if (is_char)
2188 snprintf (parse_err_msg, parse_err_msg_size,
2189 "Null substring qualifier");
2190 else
2191 snprintf (parse_err_msg, parse_err_msg_size,
2192 "Null index field");
2193 goto err_ret;
2196 if ((c == ':' && indx == 1 && dtp->u.p.saved_string == 0)
2197 || (indx == 2 && dtp->u.p.saved_string == 0))
2199 if (is_char)
2200 snprintf (parse_err_msg, parse_err_msg_size,
2201 "Bad substring qualifier");
2202 else
2203 snprintf (parse_err_msg, parse_err_msg_size,
2204 "Bad index triplet");
2205 goto err_ret;
2208 if (is_char && !is_array_section)
2210 snprintf (parse_err_msg, parse_err_msg_size,
2211 "Missing colon in substring qualifier");
2212 goto err_ret;
2215 /* If '( : ? )' or '( ? : )' break and flag read failure. */
2216 null_flag = 0;
2217 if ((c == ':' && indx == 0 && dtp->u.p.saved_string == 0)
2218 || (indx==1 && dtp->u.p.saved_string == 0))
2220 null_flag = 1;
2221 break;
2224 /* Now read the index. */
2225 if (convert_integer (dtp, sizeof(index_type), neg))
2227 if (is_char)
2228 snprintf (parse_err_msg, parse_err_msg_size,
2229 "Bad integer substring qualifier");
2230 else
2231 snprintf (parse_err_msg, parse_err_msg_size,
2232 "Bad integer in index");
2233 goto err_ret;
2235 break;
2238 /* Feed the index values to the triplet arrays. */
2239 if (!null_flag)
2241 if (indx == 0)
2242 memcpy (&ls[dim].start, dtp->u.p.value, sizeof(index_type));
2243 if (indx == 1)
2244 memcpy (&ls[dim].end, dtp->u.p.value, sizeof(index_type));
2245 if (indx == 2)
2246 memcpy (&ls[dim].step, dtp->u.p.value, sizeof(index_type));
2249 /* Singlet or doublet indices. */
2250 if (c==',' || c==')')
2252 if (indx == 0)
2254 memcpy (&ls[dim].start, dtp->u.p.value, sizeof(index_type));
2256 /* If -std=f95/2003 or an array section is specified,
2257 do not allow excess data to be processed. */
2258 if (is_array_section == 1
2259 || !(compile_options.allow_std & GFC_STD_GNU)
2260 || nml_elem_type == BT_DERIVED)
2261 ls[dim].end = ls[dim].start;
2262 else
2263 dtp->u.p.expanded_read = 1;
2266 /* Check for non-zero rank. */
2267 if (is_array_section == 1 && ls[dim].start != ls[dim].end)
2268 *parsed_rank = 1;
2270 break;
2274 if (is_array_section == 1 && dtp->u.p.expanded_read == 1)
2276 int i;
2277 dtp->u.p.expanded_read = 0;
2278 for (i = 0; i < dim; i++)
2279 ls[i].end = ls[i].start;
2282 /* Check the values of the triplet indices. */
2283 if ((ls[dim].start > GFC_DIMENSION_UBOUND(ad[dim]))
2284 || (ls[dim].start < GFC_DIMENSION_LBOUND(ad[dim]))
2285 || (ls[dim].end > GFC_DIMENSION_UBOUND(ad[dim]))
2286 || (ls[dim].end < GFC_DIMENSION_LBOUND(ad[dim])))
2288 if (is_char)
2289 snprintf (parse_err_msg, parse_err_msg_size,
2290 "Substring out of range");
2291 else
2292 snprintf (parse_err_msg, parse_err_msg_size,
2293 "Index %d out of range", dim + 1);
2294 goto err_ret;
2297 if (((ls[dim].end - ls[dim].start ) * ls[dim].step < 0)
2298 || (ls[dim].step == 0))
2300 snprintf (parse_err_msg, parse_err_msg_size,
2301 "Bad range in index %d", dim + 1);
2302 goto err_ret;
2305 /* Initialise the loop index counter. */
2306 ls[dim].idx = ls[dim].start;
2308 eat_spaces (dtp);
2309 return true;
2311 err_ret:
2313 /* The EOF error message is issued by hit_eof. Return true so that the
2314 caller does not use parse_err_msg and parse_err_msg_size to generate
2315 an unrelated error message. */
2316 if (c == EOF)
2318 hit_eof (dtp);
2319 dtp->u.p.input_complete = 1;
2320 return true;
2322 return false;
2325 static namelist_info *
2326 find_nml_node (st_parameter_dt *dtp, char * var_name)
2328 namelist_info * t = dtp->u.p.ionml;
2329 while (t != NULL)
2331 if (strcmp (var_name, t->var_name) == 0)
2333 t->touched = 1;
2334 return t;
2336 t = t->next;
2338 return NULL;
2341 /* Visits all the components of a derived type that have
2342 not explicitly been identified in the namelist input.
2343 touched is set and the loop specification initialised
2344 to default values */
2346 static void
2347 nml_touch_nodes (namelist_info * nl)
2349 index_type len = strlen (nl->var_name) + 1;
2350 int dim;
2351 char * ext_name = (char*)xmalloc (len + 1);
2352 memcpy (ext_name, nl->var_name, len-1);
2353 memcpy (ext_name + len - 1, "%", 2);
2354 for (nl = nl->next; nl; nl = nl->next)
2356 if (strncmp (nl->var_name, ext_name, len) == 0)
2358 nl->touched = 1;
2359 for (dim=0; dim < nl->var_rank; dim++)
2361 nl->ls[dim].step = 1;
2362 nl->ls[dim].end = GFC_DESCRIPTOR_UBOUND(nl,dim);
2363 nl->ls[dim].start = GFC_DESCRIPTOR_LBOUND(nl,dim);
2364 nl->ls[dim].idx = nl->ls[dim].start;
2367 else
2368 break;
2370 free (ext_name);
2371 return;
2374 /* Resets touched for the entire list of nml_nodes, ready for a
2375 new object. */
2377 static void
2378 nml_untouch_nodes (st_parameter_dt *dtp)
2380 namelist_info * t;
2381 for (t = dtp->u.p.ionml; t; t = t->next)
2382 t->touched = 0;
2383 return;
2386 /* Attempts to input name to namelist name. Returns
2387 dtp->u.p.nml_read_error = 1 on no match. */
2389 static void
2390 nml_match_name (st_parameter_dt *dtp, const char *name, index_type len)
2392 index_type i;
2393 int c;
2395 dtp->u.p.nml_read_error = 0;
2396 for (i = 0; i < len; i++)
2398 c = next_char (dtp);
2399 if (c == EOF || (tolower (c) != tolower (name[i])))
2401 dtp->u.p.nml_read_error = 1;
2402 break;
2407 /* If the namelist read is from stdin, output the current state of the
2408 namelist to stdout. This is used to implement the non-standard query
2409 features, ? and =?. If c == '=' the full namelist is printed. Otherwise
2410 the names alone are printed. */
2412 static void
2413 nml_query (st_parameter_dt *dtp, char c)
2415 gfc_unit * temp_unit;
2416 namelist_info * nl;
2417 index_type len;
2418 char * p;
2419 #ifdef HAVE_CRLF
2420 static const index_type endlen = 2;
2421 static const char endl[] = "\r\n";
2422 static const char nmlend[] = "&end\r\n";
2423 #else
2424 static const index_type endlen = 1;
2425 static const char endl[] = "\n";
2426 static const char nmlend[] = "&end\n";
2427 #endif
2429 if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
2430 return;
2432 /* Store the current unit and transfer to stdout. */
2434 temp_unit = dtp->u.p.current_unit;
2435 dtp->u.p.current_unit = find_unit (options.stdout_unit);
2437 if (dtp->u.p.current_unit)
2439 dtp->u.p.mode = WRITING;
2440 next_record (dtp, 0);
2442 /* Write the namelist in its entirety. */
2444 if (c == '=')
2445 namelist_write (dtp);
2447 /* Or write the list of names. */
2449 else
2451 /* "&namelist_name\n" */
2453 len = dtp->namelist_name_len;
2454 p = write_block (dtp, len - 1 + endlen);
2455 if (!p)
2456 goto query_return;
2457 memcpy (p, "&", 1);
2458 memcpy ((char*)(p + 1), dtp->namelist_name, len);
2459 memcpy ((char*)(p + len + 1), &endl, endlen);
2460 for (nl = dtp->u.p.ionml; nl; nl = nl->next)
2462 /* " var_name\n" */
2464 len = strlen (nl->var_name);
2465 p = write_block (dtp, len + endlen);
2466 if (!p)
2467 goto query_return;
2468 memcpy (p, " ", 1);
2469 memcpy ((char*)(p + 1), nl->var_name, len);
2470 memcpy ((char*)(p + len + 1), &endl, endlen);
2473 /* "&end\n" */
2475 p = write_block (dtp, endlen + 4);
2476 if (!p)
2477 goto query_return;
2478 memcpy (p, &nmlend, endlen + 4);
2481 /* Flush the stream to force immediate output. */
2483 fbuf_flush (dtp->u.p.current_unit, WRITING);
2484 sflush (dtp->u.p.current_unit->s);
2485 unlock_unit (dtp->u.p.current_unit);
2488 query_return:
2490 /* Restore the current unit. */
2492 dtp->u.p.current_unit = temp_unit;
2493 dtp->u.p.mode = READING;
2494 return;
2497 /* Reads and stores the input for the namelist object nl. For an array,
2498 the function loops over the ranges defined by the loop specification.
2499 This default to all the data or to the specification from a qualifier.
2500 nml_read_obj recursively calls itself to read derived types. It visits
2501 all its own components but only reads data for those that were touched
2502 when the name was parsed. If a read error is encountered, an attempt is
2503 made to return to read a new object name because the standard allows too
2504 little data to be available. On the other hand, too much data is an
2505 error. */
2507 static bool
2508 nml_read_obj (st_parameter_dt *dtp, namelist_info * nl, index_type offset,
2509 namelist_info **pprev_nl, char *nml_err_msg,
2510 size_t nml_err_msg_size, index_type clow, index_type chigh)
2512 namelist_info * cmp;
2513 char * obj_name;
2514 int nml_carry;
2515 int len;
2516 int dim;
2517 index_type dlen;
2518 index_type m;
2519 size_t obj_name_len;
2520 void * pdata;
2522 /* If we have encountered a previous read error or this object has not been
2523 touched in name parsing, just return. */
2524 if (dtp->u.p.nml_read_error || !nl->touched)
2525 return true;
2527 dtp->u.p.repeat_count = 0;
2528 eat_spaces (dtp);
2530 len = nl->len;
2531 switch (nl->type)
2533 case BT_INTEGER:
2534 case BT_LOGICAL:
2535 dlen = len;
2536 break;
2538 case BT_REAL:
2539 dlen = size_from_real_kind (len);
2540 break;
2542 case BT_COMPLEX:
2543 dlen = size_from_complex_kind (len);
2544 break;
2546 case BT_CHARACTER:
2547 dlen = chigh ? (chigh - clow + 1) : nl->string_length;
2548 break;
2550 default:
2551 dlen = 0;
2556 /* Update the pointer to the data, using the current index vector */
2558 pdata = (void*)(nl->mem_pos + offset);
2559 for (dim = 0; dim < nl->var_rank; dim++)
2560 pdata = (void*)(pdata + (nl->ls[dim].idx
2561 - GFC_DESCRIPTOR_LBOUND(nl,dim))
2562 * GFC_DESCRIPTOR_STRIDE(nl,dim) * nl->size);
2564 /* If we are finished with the repeat count, try to read next value. */
2566 nml_carry = 0;
2567 if (--dtp->u.p.repeat_count <= 0)
2569 if (dtp->u.p.input_complete)
2570 return true;
2571 if (dtp->u.p.at_eol)
2572 finish_separator (dtp);
2573 if (dtp->u.p.input_complete)
2574 return true;
2576 dtp->u.p.saved_type = BT_UNKNOWN;
2577 free_saved (dtp);
2579 switch (nl->type)
2581 case BT_INTEGER:
2582 read_integer (dtp, len);
2583 break;
2585 case BT_LOGICAL:
2586 read_logical (dtp, len);
2587 break;
2589 case BT_CHARACTER:
2590 read_character (dtp, len);
2591 break;
2593 case BT_REAL:
2594 /* Need to copy data back from the real location to the temp in
2595 order to handle nml reads into arrays. */
2596 read_real (dtp, pdata, len);
2597 memcpy (dtp->u.p.value, pdata, dlen);
2598 break;
2600 case BT_COMPLEX:
2601 /* Same as for REAL, copy back to temp. */
2602 read_complex (dtp, pdata, len, dlen);
2603 memcpy (dtp->u.p.value, pdata, dlen);
2604 break;
2606 case BT_DERIVED:
2607 obj_name_len = strlen (nl->var_name) + 1;
2608 obj_name = xmalloc (obj_name_len+1);
2609 memcpy (obj_name, nl->var_name, obj_name_len-1);
2610 memcpy (obj_name + obj_name_len - 1, "%", 2);
2612 /* If reading a derived type, disable the expanded read warning
2613 since a single object can have multiple reads. */
2614 dtp->u.p.expanded_read = 0;
2616 /* Now loop over the components. */
2618 for (cmp = nl->next;
2619 cmp &&
2620 !strncmp (cmp->var_name, obj_name, obj_name_len);
2621 cmp = cmp->next)
2623 /* Jump over nested derived type by testing if the potential
2624 component name contains '%'. */
2625 if (strchr (cmp->var_name + obj_name_len, '%'))
2626 continue;
2628 if (!nml_read_obj (dtp, cmp, (index_type)(pdata - nl->mem_pos),
2629 pprev_nl, nml_err_msg, nml_err_msg_size,
2630 clow, chigh))
2632 free (obj_name);
2633 return false;
2636 if (dtp->u.p.input_complete)
2638 free (obj_name);
2639 return true;
2643 free (obj_name);
2644 goto incr_idx;
2646 default:
2647 snprintf (nml_err_msg, nml_err_msg_size,
2648 "Bad type for namelist object %s", nl->var_name);
2649 internal_error (&dtp->common, nml_err_msg);
2650 goto nml_err_ret;
2654 /* The standard permits array data to stop short of the number of
2655 elements specified in the loop specification. In this case, we
2656 should be here with dtp->u.p.nml_read_error != 0. Control returns to
2657 nml_get_obj_data and an attempt is made to read object name. */
2659 *pprev_nl = nl;
2660 if (dtp->u.p.nml_read_error)
2662 dtp->u.p.expanded_read = 0;
2663 return true;
2666 if (dtp->u.p.saved_type == BT_UNKNOWN)
2668 dtp->u.p.expanded_read = 0;
2669 goto incr_idx;
2672 switch (dtp->u.p.saved_type)
2675 case BT_COMPLEX:
2676 case BT_REAL:
2677 case BT_INTEGER:
2678 case BT_LOGICAL:
2679 memcpy (pdata, dtp->u.p.value, dlen);
2680 break;
2682 case BT_CHARACTER:
2683 if (dlen < dtp->u.p.saved_used)
2685 if (compile_options.bounds_check)
2687 snprintf (nml_err_msg, nml_err_msg_size,
2688 "Namelist object '%s' truncated on read.",
2689 nl->var_name);
2690 generate_warning (&dtp->common, nml_err_msg);
2692 m = dlen;
2694 else
2695 m = dtp->u.p.saved_used;
2696 pdata = (void*)( pdata + clow - 1 );
2697 memcpy (pdata, dtp->u.p.saved_string, m);
2698 if (m < dlen)
2699 memset ((void*)( pdata + m ), ' ', dlen - m);
2700 break;
2702 default:
2703 break;
2706 /* Warn if a non-standard expanded read occurs. A single read of a
2707 single object is acceptable. If a second read occurs, issue a warning
2708 and set the flag to zero to prevent further warnings. */
2709 if (dtp->u.p.expanded_read == 2)
2711 notify_std (&dtp->common, GFC_STD_GNU, "Non-standard expanded namelist read.");
2712 dtp->u.p.expanded_read = 0;
2715 /* If the expanded read warning flag is set, increment it,
2716 indicating that a single read has occurred. */
2717 if (dtp->u.p.expanded_read >= 1)
2718 dtp->u.p.expanded_read++;
2720 /* Break out of loop if scalar. */
2721 if (!nl->var_rank)
2722 break;
2724 /* Now increment the index vector. */
2726 incr_idx:
2728 nml_carry = 1;
2729 for (dim = 0; dim < nl->var_rank; dim++)
2731 nl->ls[dim].idx += nml_carry * nl->ls[dim].step;
2732 nml_carry = 0;
2733 if (((nl->ls[dim].step > 0) && (nl->ls[dim].idx > nl->ls[dim].end))
2735 ((nl->ls[dim].step < 0) && (nl->ls[dim].idx < nl->ls[dim].end)))
2737 nl->ls[dim].idx = nl->ls[dim].start;
2738 nml_carry = 1;
2741 } while (!nml_carry);
2743 if (dtp->u.p.repeat_count > 1)
2745 snprintf (nml_err_msg, nml_err_msg_size,
2746 "Repeat count too large for namelist object %s", nl->var_name);
2747 goto nml_err_ret;
2749 return true;
2751 nml_err_ret:
2753 return false;
2756 /* Parses the object name, including array and substring qualifiers. It
2757 iterates over derived type components, touching those components and
2758 setting their loop specifications, if there is a qualifier. If the
2759 object is itself a derived type, its components and subcomponents are
2760 touched. nml_read_obj is called at the end and this reads the data in
2761 the manner specified by the object name. */
2763 static bool
2764 nml_get_obj_data (st_parameter_dt *dtp, namelist_info **pprev_nl,
2765 char *nml_err_msg, size_t nml_err_msg_size)
2767 int c;
2768 namelist_info * nl;
2769 namelist_info * first_nl = NULL;
2770 namelist_info * root_nl = NULL;
2771 int dim, parsed_rank;
2772 int component_flag, qualifier_flag;
2773 index_type clow, chigh;
2774 int non_zero_rank_count;
2776 /* Look for end of input or object name. If '?' or '=?' are encountered
2777 in stdin, print the node names or the namelist to stdout. */
2779 eat_separator (dtp);
2780 if (dtp->u.p.input_complete)
2781 return true;
2783 if (dtp->u.p.at_eol)
2784 finish_separator (dtp);
2785 if (dtp->u.p.input_complete)
2786 return true;
2788 if ((c = next_char (dtp)) == EOF)
2789 goto nml_err_ret;
2790 switch (c)
2792 case '=':
2793 if ((c = next_char (dtp)) == EOF)
2794 goto nml_err_ret;
2795 if (c != '?')
2797 snprintf (nml_err_msg, nml_err_msg_size,
2798 "namelist read: misplaced = sign");
2799 goto nml_err_ret;
2801 nml_query (dtp, '=');
2802 return true;
2804 case '?':
2805 nml_query (dtp, '?');
2806 return true;
2808 case '$':
2809 case '&':
2810 nml_match_name (dtp, "end", 3);
2811 if (dtp->u.p.nml_read_error)
2813 snprintf (nml_err_msg, nml_err_msg_size,
2814 "namelist not terminated with / or &end");
2815 goto nml_err_ret;
2817 /* Fall through. */
2818 case '/':
2819 dtp->u.p.input_complete = 1;
2820 return true;
2822 default :
2823 break;
2826 /* Untouch all nodes of the namelist and reset the flags that are set for
2827 derived type components. */
2829 nml_untouch_nodes (dtp);
2830 component_flag = 0;
2831 qualifier_flag = 0;
2832 non_zero_rank_count = 0;
2834 /* Get the object name - should '!' and '\n' be permitted separators? */
2836 get_name:
2838 free_saved (dtp);
2842 if (!is_separator (c))
2843 push_char (dtp, tolower(c));
2844 if ((c = next_char (dtp)) == EOF)
2845 goto nml_err_ret;
2847 while (!( c=='=' || c==' ' || c=='\t' || c =='(' || c =='%' ));
2849 unget_char (dtp, c);
2851 /* Check that the name is in the namelist and get pointer to object.
2852 Three error conditions exist: (i) An attempt is being made to
2853 identify a non-existent object, following a failed data read or
2854 (ii) The object name does not exist or (iii) Too many data items
2855 are present for an object. (iii) gives the same error message
2856 as (i) */
2858 push_char (dtp, '\0');
2860 if (component_flag)
2862 size_t var_len = strlen (root_nl->var_name);
2863 size_t saved_len
2864 = dtp->u.p.saved_string ? strlen (dtp->u.p.saved_string) : 0;
2865 char ext_name[var_len + saved_len + 1];
2867 memcpy (ext_name, root_nl->var_name, var_len);
2868 if (dtp->u.p.saved_string)
2869 memcpy (ext_name + var_len, dtp->u.p.saved_string, saved_len);
2870 ext_name[var_len + saved_len] = '\0';
2871 nl = find_nml_node (dtp, ext_name);
2873 else
2874 nl = find_nml_node (dtp, dtp->u.p.saved_string);
2876 if (nl == NULL)
2878 if (dtp->u.p.nml_read_error && *pprev_nl)
2879 snprintf (nml_err_msg, nml_err_msg_size,
2880 "Bad data for namelist object %s", (*pprev_nl)->var_name);
2882 else
2883 snprintf (nml_err_msg, nml_err_msg_size,
2884 "Cannot match namelist object name %s",
2885 dtp->u.p.saved_string);
2887 goto nml_err_ret;
2890 /* Get the length, data length, base pointer and rank of the variable.
2891 Set the default loop specification first. */
2893 for (dim=0; dim < nl->var_rank; dim++)
2895 nl->ls[dim].step = 1;
2896 nl->ls[dim].end = GFC_DESCRIPTOR_UBOUND(nl,dim);
2897 nl->ls[dim].start = GFC_DESCRIPTOR_LBOUND(nl,dim);
2898 nl->ls[dim].idx = nl->ls[dim].start;
2901 /* Check to see if there is a qualifier: if so, parse it.*/
2903 if (c == '(' && nl->var_rank)
2905 parsed_rank = 0;
2906 if (!nml_parse_qualifier (dtp, nl->dim, nl->ls, nl->var_rank,
2907 nl->type, nml_err_msg, nml_err_msg_size,
2908 &parsed_rank))
2910 char *nml_err_msg_end = strchr (nml_err_msg, '\0');
2911 snprintf (nml_err_msg_end,
2912 nml_err_msg_size - (nml_err_msg_end - nml_err_msg),
2913 " for namelist variable %s", nl->var_name);
2914 goto nml_err_ret;
2916 if (parsed_rank > 0)
2917 non_zero_rank_count++;
2919 qualifier_flag = 1;
2921 if ((c = next_char (dtp)) == EOF)
2922 goto nml_err_ret;
2923 unget_char (dtp, c);
2925 else if (nl->var_rank > 0)
2926 non_zero_rank_count++;
2928 /* Now parse a derived type component. The root namelist_info address
2929 is backed up, as is the previous component level. The component flag
2930 is set and the iteration is made by jumping back to get_name. */
2932 if (c == '%')
2934 if (nl->type != BT_DERIVED)
2936 snprintf (nml_err_msg, nml_err_msg_size,
2937 "Attempt to get derived component for %s", nl->var_name);
2938 goto nml_err_ret;
2941 /* Don't move first_nl further in the list if a qualifier was found. */
2942 if ((*pprev_nl == NULL && !qualifier_flag) || !component_flag)
2943 first_nl = nl;
2945 root_nl = nl;
2947 component_flag = 1;
2948 if ((c = next_char (dtp)) == EOF)
2949 goto nml_err_ret;
2950 goto get_name;
2953 /* Parse a character qualifier, if present. chigh = 0 is a default
2954 that signals that the string length = string_length. */
2956 clow = 1;
2957 chigh = 0;
2959 if (c == '(' && nl->type == BT_CHARACTER)
2961 descriptor_dimension chd[1] = { {1, clow, nl->string_length} };
2962 array_loop_spec ind[1] = { {1, clow, nl->string_length, 1} };
2964 if (!nml_parse_qualifier (dtp, chd, ind, -1, nl->type,
2965 nml_err_msg, nml_err_msg_size, &parsed_rank))
2967 char *nml_err_msg_end = strchr (nml_err_msg, '\0');
2968 snprintf (nml_err_msg_end,
2969 nml_err_msg_size - (nml_err_msg_end - nml_err_msg),
2970 " for namelist variable %s", nl->var_name);
2971 goto nml_err_ret;
2974 clow = ind[0].start;
2975 chigh = ind[0].end;
2977 if (ind[0].step != 1)
2979 snprintf (nml_err_msg, nml_err_msg_size,
2980 "Step not allowed in substring qualifier"
2981 " for namelist object %s", nl->var_name);
2982 goto nml_err_ret;
2985 if ((c = next_char (dtp)) == EOF)
2986 goto nml_err_ret;
2987 unget_char (dtp, c);
2990 /* Make sure no extraneous qualifiers are there. */
2992 if (c == '(')
2994 snprintf (nml_err_msg, nml_err_msg_size,
2995 "Qualifier for a scalar or non-character namelist object %s",
2996 nl->var_name);
2997 goto nml_err_ret;
3000 /* Make sure there is no more than one non-zero rank object. */
3001 if (non_zero_rank_count > 1)
3003 snprintf (nml_err_msg, nml_err_msg_size,
3004 "Multiple sub-objects with non-zero rank in namelist object %s",
3005 nl->var_name);
3006 non_zero_rank_count = 0;
3007 goto nml_err_ret;
3010 /* According to the standard, an equal sign MUST follow an object name. The
3011 following is possibly lax - it allows comments, blank lines and so on to
3012 intervene. eat_spaces (dtp); c = next_char (dtp); would be compliant*/
3014 free_saved (dtp);
3016 eat_separator (dtp);
3017 if (dtp->u.p.input_complete)
3018 return true;
3020 if (dtp->u.p.at_eol)
3021 finish_separator (dtp);
3022 if (dtp->u.p.input_complete)
3023 return true;
3025 if ((c = next_char (dtp)) == EOF)
3026 goto nml_err_ret;
3028 if (c != '=')
3030 snprintf (nml_err_msg, nml_err_msg_size,
3031 "Equal sign must follow namelist object name %s",
3032 nl->var_name);
3033 goto nml_err_ret;
3035 /* If a derived type, touch its components and restore the root
3036 namelist_info if we have parsed a qualified derived type
3037 component. */
3039 if (nl->type == BT_DERIVED)
3040 nml_touch_nodes (nl);
3042 if (first_nl)
3044 if (first_nl->var_rank == 0)
3046 if (component_flag && qualifier_flag)
3047 nl = first_nl;
3049 else
3050 nl = first_nl;
3053 dtp->u.p.nml_read_error = 0;
3054 if (!nml_read_obj (dtp, nl, 0, pprev_nl, nml_err_msg, nml_err_msg_size,
3055 clow, chigh))
3056 goto nml_err_ret;
3058 return true;
3060 nml_err_ret:
3062 /* The EOF error message is issued by hit_eof. Return true so that the
3063 caller does not use nml_err_msg and nml_err_msg_size to generate
3064 an unrelated error message. */
3065 if (c == EOF)
3067 dtp->u.p.input_complete = 1;
3068 unget_char (dtp, c);
3069 hit_eof (dtp);
3070 return true;
3072 return false;
3075 /* Entry point for namelist input. Goes through input until namelist name
3076 is matched. Then cycles through nml_get_obj_data until the input is
3077 completed or there is an error. */
3079 void
3080 namelist_read (st_parameter_dt *dtp)
3082 int c;
3083 char nml_err_msg[200];
3085 /* Initialize the error string buffer just in case we get an unexpected fail
3086 somewhere and end up at nml_err_ret. */
3087 strcpy (nml_err_msg, "Internal namelist read error");
3089 /* Pointer to the previously read object, in case attempt is made to read
3090 new object name. Should this fail, error message can give previous
3091 name. */
3092 namelist_info *prev_nl = NULL;
3094 dtp->u.p.namelist_mode = 1;
3095 dtp->u.p.input_complete = 0;
3096 dtp->u.p.expanded_read = 0;
3098 /* Look for &namelist_name . Skip all characters, testing for $nmlname.
3099 Exit on success or EOF. If '?' or '=?' encountered in stdin, print
3100 node names or namelist on stdout. */
3102 find_nml_name:
3103 c = next_char (dtp);
3104 switch (c)
3106 case '$':
3107 case '&':
3108 break;
3110 case '!':
3111 eat_line (dtp);
3112 goto find_nml_name;
3114 case '=':
3115 c = next_char (dtp);
3116 if (c == '?')
3117 nml_query (dtp, '=');
3118 else
3119 unget_char (dtp, c);
3120 goto find_nml_name;
3122 case '?':
3123 nml_query (dtp, '?');
3124 goto find_nml_name;
3126 case EOF:
3127 return;
3129 default:
3130 goto find_nml_name;
3133 /* Match the name of the namelist. */
3135 nml_match_name (dtp, dtp->namelist_name, dtp->namelist_name_len);
3137 if (dtp->u.p.nml_read_error)
3138 goto find_nml_name;
3140 /* A trailing space is required, we give a little latitude here, 10.9.1. */
3141 c = next_char (dtp);
3142 if (!is_separator(c) && c != '!')
3144 unget_char (dtp, c);
3145 goto find_nml_name;
3148 unget_char (dtp, c);
3149 eat_separator (dtp);
3151 /* Ready to read namelist objects. If there is an error in input
3152 from stdin, output the error message and continue. */
3154 while (!dtp->u.p.input_complete)
3156 if (!nml_get_obj_data (dtp, &prev_nl, nml_err_msg, sizeof nml_err_msg))
3158 if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
3159 goto nml_err_ret;
3160 generate_error (&dtp->common, LIBERROR_READ_VALUE, nml_err_msg);
3163 /* Reset the previous namelist pointer if we know we are not going
3164 to be doing multiple reads within a single namelist object. */
3165 if (prev_nl && prev_nl->var_rank == 0)
3166 prev_nl = NULL;
3169 free_saved (dtp);
3170 free_line (dtp);
3171 return;
3174 nml_err_ret:
3176 /* All namelist error calls return from here */
3177 free_saved (dtp);
3178 free_line (dtp);
3179 generate_error (&dtp->common, LIBERROR_READ_VALUE, nml_err_msg);
3180 return;