missing Changelog
[official-gcc.git] / libgfortran / io / list_read.c
blob403e7190a1234ed5f010c534f0258821cd0a9e55
1 /* Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011, 2012
2 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
4 Namelist input contributed by Paul Thomas
5 F2003 I/O support contributed by Jerry DeLisle
7 This file is part of the GNU Fortran runtime library (libgfortran).
9 Libgfortran is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
14 Libgfortran is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 Under Section 7 of GPL version 3, you are granted additional
20 permissions described in the GCC Runtime Library Exception, version
21 3.1, as published by the Free Software Foundation.
23 You should have received a copy of the GNU General Public License and
24 a copy of the GCC Runtime Library Exception along with this program;
25 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
26 <http://www.gnu.org/licenses/>. */
29 #include "io.h"
30 #include "fbuf.h"
31 #include "unix.h"
32 #include <string.h>
33 #include <stdlib.h>
34 #include <ctype.h>
37 /* List directed input. Several parsing subroutines are practically
38 reimplemented from formatted input, the reason being that there are
39 all kinds of small differences between formatted and list directed
40 parsing. */
43 /* Subroutines for reading characters from the input. Because a
44 repeat count is ambiguous with an integer, we have to read the
45 whole digit string before seeing if there is a '*' which signals
46 the repeat count. Since we can have a lot of potential leading
47 zeros, we have to be able to back up by arbitrary amount. Because
48 the input might not be seekable, we have to buffer the data
49 ourselves. */
51 #define CASE_DIGITS case '0': case '1': case '2': case '3': case '4': \
52 case '5': case '6': case '7': case '8': case '9'
54 #define CASE_SEPARATORS case ' ': case ',': case '/': case '\n': case '\t': \
55 case '\r': case ';'
57 /* This macro assumes that we're operating on a variable. */
59 #define is_separator(c) (c == '/' || c == ',' || c == '\n' || c == ' ' \
60 || c == '\t' || c == '\r' || c == ';')
62 /* Maximum repeat count. Less than ten times the maximum signed int32. */
64 #define MAX_REPEAT 200000000
67 #define MSGLEN 100
69 /* Save a character to a string buffer, enlarging it as necessary. */
71 static void
72 push_char (st_parameter_dt *dtp, char c)
74 char *new;
76 if (dtp->u.p.saved_string == NULL)
78 // Plain malloc should suffice here, zeroing not needed?
79 dtp->u.p.saved_string = xcalloc (SCRATCH_SIZE, 1);
80 dtp->u.p.saved_length = SCRATCH_SIZE;
81 dtp->u.p.saved_used = 0;
84 if (dtp->u.p.saved_used >= dtp->u.p.saved_length)
86 dtp->u.p.saved_length = 2 * dtp->u.p.saved_length;
87 new = realloc (dtp->u.p.saved_string, dtp->u.p.saved_length);
88 if (new == NULL)
89 generate_error (&dtp->common, LIBERROR_OS, NULL);
90 dtp->u.p.saved_string = new;
92 // Also this should not be necessary.
93 memset (new + dtp->u.p.saved_used, 0,
94 dtp->u.p.saved_length - dtp->u.p.saved_used);
98 dtp->u.p.saved_string[dtp->u.p.saved_used++] = c;
102 /* Free the input buffer if necessary. */
104 static void
105 free_saved (st_parameter_dt *dtp)
107 if (dtp->u.p.saved_string == NULL)
108 return;
110 free (dtp->u.p.saved_string);
112 dtp->u.p.saved_string = NULL;
113 dtp->u.p.saved_used = 0;
117 /* Free the line buffer if necessary. */
119 static void
120 free_line (st_parameter_dt *dtp)
122 dtp->u.p.item_count = 0;
123 dtp->u.p.line_buffer_enabled = 0;
125 if (dtp->u.p.line_buffer == NULL)
126 return;
128 free (dtp->u.p.line_buffer);
129 dtp->u.p.line_buffer = NULL;
133 static int
134 next_char (st_parameter_dt *dtp)
136 ssize_t length;
137 gfc_offset record;
138 int c;
140 if (dtp->u.p.last_char != EOF - 1)
142 dtp->u.p.at_eol = 0;
143 c = dtp->u.p.last_char;
144 dtp->u.p.last_char = EOF - 1;
145 goto done;
148 /* Read from line_buffer if enabled. */
150 if (dtp->u.p.line_buffer_enabled)
152 dtp->u.p.at_eol = 0;
154 c = dtp->u.p.line_buffer[dtp->u.p.item_count];
155 if (c != '\0' && dtp->u.p.item_count < 64)
157 dtp->u.p.line_buffer[dtp->u.p.item_count] = '\0';
158 dtp->u.p.item_count++;
159 goto done;
162 dtp->u.p.item_count = 0;
163 dtp->u.p.line_buffer_enabled = 0;
166 /* Handle the end-of-record and end-of-file conditions for
167 internal array unit. */
168 if (is_array_io (dtp))
170 if (dtp->u.p.at_eof)
171 return EOF;
173 /* Check for "end-of-record" condition. */
174 if (dtp->u.p.current_unit->bytes_left == 0)
176 int finished;
178 c = '\n';
179 record = next_array_record (dtp, dtp->u.p.current_unit->ls,
180 &finished);
182 /* Check for "end-of-file" condition. */
183 if (finished)
185 dtp->u.p.at_eof = 1;
186 goto done;
189 record *= dtp->u.p.current_unit->recl;
190 if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
191 return EOF;
193 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
194 goto done;
198 /* Get the next character and handle end-of-record conditions. */
200 if (is_internal_unit (dtp))
202 /* Check for kind=4 internal unit. */
203 if (dtp->common.unit)
204 length = sread (dtp->u.p.current_unit->s, &c, sizeof (gfc_char4_t));
205 else
207 char cc;
208 length = sread (dtp->u.p.current_unit->s, &cc, 1);
209 c = cc;
212 if (length < 0)
214 generate_error (&dtp->common, LIBERROR_OS, NULL);
215 return '\0';
218 if (is_array_io (dtp))
220 /* Check whether we hit EOF. */
221 if (length == 0)
223 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
224 return '\0';
226 dtp->u.p.current_unit->bytes_left--;
228 else
230 if (dtp->u.p.at_eof)
231 return EOF;
232 if (length == 0)
234 c = '\n';
235 dtp->u.p.at_eof = 1;
239 else
241 c = fbuf_getc (dtp->u.p.current_unit);
242 if (c != EOF && is_stream_io (dtp))
243 dtp->u.p.current_unit->strm_pos++;
245 done:
246 dtp->u.p.at_eol = (c == '\n' || c == '\r' || c == EOF);
247 return c;
251 /* Push a character back onto the input. */
253 static void
254 unget_char (st_parameter_dt *dtp, int c)
256 dtp->u.p.last_char = c;
260 /* Skip over spaces in the input. Returns the nonspace character that
261 terminated the eating and also places it back on the input. */
263 static int
264 eat_spaces (st_parameter_dt *dtp)
266 int c;
269 c = next_char (dtp);
270 while (c != EOF && (c == ' ' || c == '\t'));
272 unget_char (dtp, c);
273 return c;
277 /* This function reads characters through to the end of the current
278 line and just ignores them. Returns 0 for success and LIBERROR_END
279 if it hit EOF. */
281 static int
282 eat_line (st_parameter_dt *dtp)
284 int c;
287 c = next_char (dtp);
288 while (c != EOF && c != '\n');
289 if (c == EOF)
290 return LIBERROR_END;
291 return 0;
295 /* Skip over a separator. Technically, we don't always eat the whole
296 separator. This is because if we've processed the last input item,
297 then a separator is unnecessary. Plus the fact that operating
298 systems usually deliver console input on a line basis.
300 The upshot is that if we see a newline as part of reading a
301 separator, we stop reading. If there are more input items, we
302 continue reading the separator with finish_separator() which takes
303 care of the fact that we may or may not have seen a comma as part
304 of the separator.
306 Returns 0 for success, and non-zero error code otherwise. */
308 static int
309 eat_separator (st_parameter_dt *dtp)
311 int c, n;
312 int err = 0;
314 eat_spaces (dtp);
315 dtp->u.p.comma_flag = 0;
317 if ((c = next_char (dtp)) == EOF)
318 return LIBERROR_END;
319 switch (c)
321 case ',':
322 if (dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
324 unget_char (dtp, c);
325 break;
327 /* Fall through. */
328 case ';':
329 dtp->u.p.comma_flag = 1;
330 eat_spaces (dtp);
331 break;
333 case '/':
334 dtp->u.p.input_complete = 1;
335 break;
337 case '\r':
338 dtp->u.p.at_eol = 1;
339 if ((n = next_char(dtp)) == EOF)
340 return LIBERROR_END;
341 if (n != '\n')
343 unget_char (dtp, n);
344 break;
346 /* Fall through. */
347 case '\n':
348 dtp->u.p.at_eol = 1;
349 if (dtp->u.p.namelist_mode)
353 if ((c = next_char (dtp)) == EOF)
354 return LIBERROR_END;
355 if (c == '!')
357 err = eat_line (dtp);
358 if (err)
359 return err;
360 c = '\n';
363 while (c == '\n' || c == '\r' || c == ' ' || c == '\t');
364 unget_char (dtp, c);
366 break;
368 case '!':
369 if (dtp->u.p.namelist_mode)
370 { /* Eat a namelist comment. */
371 err = eat_line (dtp);
372 if (err)
373 return err;
375 break;
378 /* Fall Through... */
380 default:
381 unget_char (dtp, c);
382 break;
384 return err;
388 /* Finish processing a separator that was interrupted by a newline.
389 If we're here, then another data item is present, so we finish what
390 we started on the previous line. Return 0 on success, error code
391 on failure. */
393 static int
394 finish_separator (st_parameter_dt *dtp)
396 int c;
397 int err;
399 restart:
400 eat_spaces (dtp);
402 if ((c = next_char (dtp)) == EOF)
403 return LIBERROR_END;
404 switch (c)
406 case ',':
407 if (dtp->u.p.comma_flag)
408 unget_char (dtp, c);
409 else
411 if ((c = eat_spaces (dtp)) == EOF)
412 return LIBERROR_END;
413 if (c == '\n' || c == '\r')
414 goto restart;
417 break;
419 case '/':
420 dtp->u.p.input_complete = 1;
421 if (!dtp->u.p.namelist_mode)
422 return err;
423 break;
425 case '\n':
426 case '\r':
427 goto restart;
429 case '!':
430 if (dtp->u.p.namelist_mode)
432 err = eat_line (dtp);
433 if (err)
434 return err;
435 goto restart;
438 default:
439 unget_char (dtp, c);
440 break;
442 return err;
446 /* This function is needed to catch bad conversions so that namelist can
447 attempt to see if dtp->u.p.saved_string contains a new object name rather
448 than a bad value. */
450 static int
451 nml_bad_return (st_parameter_dt *dtp, char c)
453 if (dtp->u.p.namelist_mode)
455 dtp->u.p.nml_read_error = 1;
456 unget_char (dtp, c);
457 return 1;
459 return 0;
462 /* Convert an unsigned string to an integer. The length value is -1
463 if we are working on a repeat count. Returns nonzero if we have a
464 range problem. As a side effect, frees the dtp->u.p.saved_string. */
466 static int
467 convert_integer (st_parameter_dt *dtp, int length, int negative)
469 char c, *buffer, message[MSGLEN];
470 int m;
471 GFC_UINTEGER_LARGEST v, max, max10;
472 GFC_INTEGER_LARGEST value;
474 buffer = dtp->u.p.saved_string;
475 v = 0;
477 if (length == -1)
478 max = MAX_REPEAT;
479 else
481 max = si_max (length);
482 if (negative)
483 max++;
485 max10 = max / 10;
487 for (;;)
489 c = *buffer++;
490 if (c == '\0')
491 break;
492 c -= '0';
494 if (v > max10)
495 goto overflow;
496 v = 10 * v;
498 if (v > max - c)
499 goto overflow;
500 v += c;
503 m = 0;
505 if (length != -1)
507 if (negative)
508 value = -v;
509 else
510 value = v;
511 set_integer (dtp->u.p.value, value, length);
513 else
515 dtp->u.p.repeat_count = v;
517 if (dtp->u.p.repeat_count == 0)
519 snprintf (message, MSGLEN, "Zero repeat count in item %d of list input",
520 dtp->u.p.item_count);
522 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
523 m = 1;
527 free_saved (dtp);
528 return m;
530 overflow:
531 if (length == -1)
532 snprintf (message, MSGLEN, "Repeat count overflow in item %d of list input",
533 dtp->u.p.item_count);
534 else
535 snprintf (message, MSGLEN, "Integer overflow while reading item %d",
536 dtp->u.p.item_count);
538 free_saved (dtp);
539 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
541 return 1;
545 /* Parse a repeat count for logical and complex values which cannot
546 begin with a digit. Returns nonzero if we are done, zero if we
547 should continue on. */
549 static int
550 parse_repeat (st_parameter_dt *dtp)
552 char message[MSGLEN];
553 int c, repeat;
555 if ((c = next_char (dtp)) == EOF)
556 goto bad_repeat;
557 switch (c)
559 CASE_DIGITS:
560 repeat = c - '0';
561 break;
563 CASE_SEPARATORS:
564 unget_char (dtp, c);
565 eat_separator (dtp);
566 return 1;
568 default:
569 unget_char (dtp, c);
570 return 0;
573 for (;;)
575 c = next_char (dtp);
576 switch (c)
578 CASE_DIGITS:
579 repeat = 10 * repeat + c - '0';
581 if (repeat > MAX_REPEAT)
583 snprintf (message, MSGLEN,
584 "Repeat count overflow in item %d of list input",
585 dtp->u.p.item_count);
587 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
588 return 1;
591 break;
593 case '*':
594 if (repeat == 0)
596 snprintf (message, MSGLEN,
597 "Zero repeat count in item %d of list input",
598 dtp->u.p.item_count);
600 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
601 return 1;
604 goto done;
606 default:
607 goto bad_repeat;
611 done:
612 dtp->u.p.repeat_count = repeat;
613 return 0;
615 bad_repeat:
617 free_saved (dtp);
618 if (c == EOF)
620 hit_eof (dtp);
621 return 1;
623 else
624 eat_line (dtp);
625 snprintf (message, MSGLEN, "Bad repeat count in item %d of list input",
626 dtp->u.p.item_count);
627 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
628 return 1;
632 /* To read a logical we have to look ahead in the input stream to make sure
633 there is not an equal sign indicating a variable name. To do this we use
634 line_buffer to point to a temporary buffer, pushing characters there for
635 possible later reading. */
637 static void
638 l_push_char (st_parameter_dt *dtp, char c)
640 if (dtp->u.p.line_buffer == NULL)
641 dtp->u.p.line_buffer = xcalloc (SCRATCH_SIZE, 1);
643 dtp->u.p.line_buffer[dtp->u.p.item_count++] = c;
647 /* Read a logical character on the input. */
649 static void
650 read_logical (st_parameter_dt *dtp, int length)
652 char message[MSGLEN];
653 int c, i, v;
655 if (parse_repeat (dtp))
656 return;
658 c = tolower (next_char (dtp));
659 l_push_char (dtp, c);
660 switch (c)
662 case 't':
663 v = 1;
664 c = next_char (dtp);
665 l_push_char (dtp, c);
667 if (!is_separator(c) && c != EOF)
668 goto possible_name;
670 unget_char (dtp, c);
671 break;
672 case 'f':
673 v = 0;
674 c = next_char (dtp);
675 l_push_char (dtp, c);
677 if (!is_separator(c) && c != EOF)
678 goto possible_name;
680 unget_char (dtp, c);
681 break;
683 case '.':
684 c = tolower (next_char (dtp));
685 switch (c)
687 case 't':
688 v = 1;
689 break;
690 case 'f':
691 v = 0;
692 break;
693 default:
694 goto bad_logical;
697 break;
699 CASE_SEPARATORS:
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 hit_eof (dtp);
909 return;
911 else if (c != '\n')
912 eat_line (dtp);
913 snprintf (message, MSGLEN, "Bad integer for item %d in list input",
914 dtp->u.p.item_count);
915 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
917 return;
919 done:
920 unget_char (dtp, c);
921 eat_separator (dtp);
923 push_char (dtp, '\0');
924 if (convert_integer (dtp, length, negative))
926 free_saved (dtp);
927 return;
930 free_saved (dtp);
931 dtp->u.p.saved_type = BT_INTEGER;
935 /* Read a character variable. */
937 static void
938 read_character (st_parameter_dt *dtp, int length __attribute__ ((unused)))
940 char quote, message[MSGLEN];
941 int c;
943 quote = ' '; /* Space means no quote character. */
945 if ((c = next_char (dtp)) == EOF)
946 goto eof;
947 switch (c)
949 CASE_DIGITS:
950 push_char (dtp, c);
951 break;
953 CASE_SEPARATORS:
954 unget_char (dtp, c); /* NULL value. */
955 eat_separator (dtp);
956 return;
958 case '"':
959 case '\'':
960 quote = c;
961 goto get_string;
963 default:
964 if (dtp->u.p.namelist_mode)
966 unget_char (dtp, c);
967 return;
970 push_char (dtp, c);
971 goto get_string;
974 /* Deal with a possible repeat count. */
976 for (;;)
978 if ((c = next_char (dtp)) == EOF)
979 goto eof;
980 switch (c)
982 CASE_DIGITS:
983 push_char (dtp, c);
984 break;
986 CASE_SEPARATORS:
987 unget_char (dtp, c);
988 goto done; /* String was only digits! */
990 case '*':
991 push_char (dtp, '\0');
992 goto got_repeat;
994 default:
995 push_char (dtp, c);
996 goto get_string; /* Not a repeat count after all. */
1000 got_repeat:
1001 if (convert_integer (dtp, -1, 0))
1002 return;
1004 /* Now get the real string. */
1006 if ((c = next_char (dtp)) == EOF)
1007 goto eof;
1008 switch (c)
1010 CASE_SEPARATORS:
1011 unget_char (dtp, c); /* Repeated NULL values. */
1012 eat_separator (dtp);
1013 return;
1015 case '"':
1016 case '\'':
1017 quote = c;
1018 break;
1020 default:
1021 push_char (dtp, c);
1022 break;
1025 get_string:
1026 for (;;)
1028 if ((c = next_char (dtp)) == EOF)
1029 goto done_eof;
1030 switch (c)
1032 case '"':
1033 case '\'':
1034 if (c != quote)
1036 push_char (dtp, c);
1037 break;
1040 /* See if we have a doubled quote character or the end of
1041 the string. */
1043 if ((c = next_char (dtp)) == EOF)
1044 goto eof;
1045 if (c == quote)
1047 push_char (dtp, quote);
1048 break;
1051 unget_char (dtp, c);
1052 goto done;
1054 CASE_SEPARATORS:
1055 if (quote == ' ')
1057 unget_char (dtp, c);
1058 goto done;
1061 if (c != '\n' && c != '\r')
1062 push_char (dtp, c);
1063 break;
1065 default:
1066 push_char (dtp, c);
1067 break;
1071 /* At this point, we have to have a separator, or else the string is
1072 invalid. */
1073 done:
1074 c = next_char (dtp);
1075 done_eof:
1076 if (is_separator (c) || c == '!' || c == EOF)
1078 unget_char (dtp, c);
1079 eat_separator (dtp);
1080 dtp->u.p.saved_type = BT_CHARACTER;
1081 free_line (dtp);
1083 else
1085 free_saved (dtp);
1086 snprintf (message, MSGLEN, "Invalid string input in item %d",
1087 dtp->u.p.item_count);
1088 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1090 return;
1092 eof:
1093 free_saved (dtp);
1094 hit_eof (dtp);
1098 /* Parse a component of a complex constant or a real number that we
1099 are sure is already there. This is a straight real number parser. */
1101 static int
1102 parse_real (st_parameter_dt *dtp, void *buffer, int length)
1104 char message[MSGLEN];
1105 int c, m, seen_dp;
1107 if ((c = next_char (dtp)) == EOF)
1108 goto bad;
1110 if (c == '-' || c == '+')
1112 push_char (dtp, c);
1113 if ((c = next_char (dtp)) == EOF)
1114 goto bad;
1117 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1118 c = '.';
1120 if (!isdigit (c) && c != '.')
1122 if (c == 'i' || c == 'I' || c == 'n' || c == 'N')
1123 goto inf_nan;
1124 else
1125 goto bad;
1128 push_char (dtp, c);
1130 seen_dp = (c == '.') ? 1 : 0;
1132 for (;;)
1134 if ((c = next_char (dtp)) == EOF)
1135 goto bad;
1136 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1137 c = '.';
1138 switch (c)
1140 CASE_DIGITS:
1141 push_char (dtp, c);
1142 break;
1144 case '.':
1145 if (seen_dp)
1146 goto bad;
1148 seen_dp = 1;
1149 push_char (dtp, c);
1150 break;
1152 case 'e':
1153 case 'E':
1154 case 'd':
1155 case 'D':
1156 case 'q':
1157 case 'Q':
1158 push_char (dtp, 'e');
1159 goto exp1;
1161 case '-':
1162 case '+':
1163 push_char (dtp, 'e');
1164 push_char (dtp, c);
1165 if ((c = next_char (dtp)) == EOF)
1166 goto bad;
1167 goto exp2;
1169 CASE_SEPARATORS:
1170 goto done;
1172 default:
1173 goto done;
1177 exp1:
1178 if ((c = next_char (dtp)) == EOF)
1179 goto bad;
1180 if (c != '-' && c != '+')
1181 push_char (dtp, '+');
1182 else
1184 push_char (dtp, c);
1185 c = next_char (dtp);
1188 exp2:
1189 if (!isdigit (c))
1190 goto bad;
1192 push_char (dtp, c);
1194 for (;;)
1196 if ((c = next_char (dtp)) == EOF)
1197 goto bad;
1198 switch (c)
1200 CASE_DIGITS:
1201 push_char (dtp, c);
1202 break;
1204 CASE_SEPARATORS:
1205 unget_char (dtp, c);
1206 goto done;
1208 default:
1209 goto done;
1213 done:
1214 unget_char (dtp, c);
1215 push_char (dtp, '\0');
1217 m = convert_real (dtp, buffer, dtp->u.p.saved_string, length);
1218 free_saved (dtp);
1220 return m;
1222 done_infnan:
1223 unget_char (dtp, c);
1224 push_char (dtp, '\0');
1226 m = convert_infnan (dtp, buffer, dtp->u.p.saved_string, length);
1227 free_saved (dtp);
1229 return m;
1231 inf_nan:
1232 /* Match INF and Infinity. */
1233 if ((c == 'i' || c == 'I')
1234 && ((c = next_char (dtp)) == 'n' || c == 'N')
1235 && ((c = next_char (dtp)) == 'f' || c == 'F'))
1237 c = next_char (dtp);
1238 if ((c != 'i' && c != 'I')
1239 || ((c == 'i' || c == 'I')
1240 && ((c = next_char (dtp)) == 'n' || c == 'N')
1241 && ((c = next_char (dtp)) == 'i' || c == 'I')
1242 && ((c = next_char (dtp)) == 't' || c == 'T')
1243 && ((c = next_char (dtp)) == 'y' || c == 'Y')
1244 && (c = next_char (dtp))))
1246 if (is_separator (c))
1247 unget_char (dtp, c);
1248 push_char (dtp, 'i');
1249 push_char (dtp, 'n');
1250 push_char (dtp, 'f');
1251 goto done_infnan;
1253 } /* Match NaN. */
1254 else if (((c = next_char (dtp)) == 'a' || c == 'A')
1255 && ((c = next_char (dtp)) == 'n' || c == 'N')
1256 && (c = next_char (dtp)))
1258 if (is_separator (c))
1259 unget_char (dtp, c);
1260 push_char (dtp, 'n');
1261 push_char (dtp, 'a');
1262 push_char (dtp, 'n');
1264 /* Match "NAN(alphanum)". */
1265 if (c == '(')
1267 for ( ; c != ')'; c = next_char (dtp))
1268 if (is_separator (c))
1269 goto bad;
1271 c = next_char (dtp);
1272 if (is_separator (c))
1273 unget_char (dtp, c);
1275 goto done_infnan;
1278 bad:
1280 if (nml_bad_return (dtp, c))
1281 return 0;
1283 free_saved (dtp);
1284 if (c == EOF)
1286 hit_eof (dtp);
1287 return 1;
1289 else if (c != '\n')
1290 eat_line (dtp);
1291 snprintf (message, MSGLEN, "Bad floating point number for item %d",
1292 dtp->u.p.item_count);
1293 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1295 return 1;
1299 /* Reading a complex number is straightforward because we can tell
1300 what it is right away. */
1302 static void
1303 read_complex (st_parameter_dt *dtp, void * dest, int kind, size_t size)
1305 char message[MSGLEN];
1306 int c;
1308 if (parse_repeat (dtp))
1309 return;
1311 c = next_char (dtp);
1312 switch (c)
1314 case '(':
1315 break;
1317 CASE_SEPARATORS:
1318 unget_char (dtp, c);
1319 eat_separator (dtp);
1320 return;
1322 default:
1323 goto bad_complex;
1326 eol_1:
1327 eat_spaces (dtp);
1328 c = next_char (dtp);
1329 if (c == '\n' || c== '\r')
1330 goto eol_1;
1331 else
1332 unget_char (dtp, c);
1334 if (parse_real (dtp, dest, kind))
1335 return;
1337 eol_2:
1338 eat_spaces (dtp);
1339 c = next_char (dtp);
1340 if (c == '\n' || c== '\r')
1341 goto eol_2;
1342 else
1343 unget_char (dtp, c);
1345 if (next_char (dtp)
1346 != (dtp->u.p.current_unit->decimal_status == DECIMAL_POINT ? ',' : ';'))
1347 goto bad_complex;
1349 eol_3:
1350 eat_spaces (dtp);
1351 c = next_char (dtp);
1352 if (c == '\n' || c== '\r')
1353 goto eol_3;
1354 else
1355 unget_char (dtp, c);
1357 if (parse_real (dtp, dest + size / 2, kind))
1358 return;
1360 eol_4:
1361 eat_spaces (dtp);
1362 c = next_char (dtp);
1363 if (c == '\n' || c== '\r')
1364 goto eol_4;
1365 else
1366 unget_char (dtp, c);
1368 if (next_char (dtp) != ')')
1369 goto bad_complex;
1371 c = next_char (dtp);
1372 if (!is_separator (c))
1373 goto bad_complex;
1375 unget_char (dtp, c);
1376 eat_separator (dtp);
1378 free_saved (dtp);
1379 dtp->u.p.saved_type = BT_COMPLEX;
1380 return;
1382 bad_complex:
1384 if (nml_bad_return (dtp, c))
1385 return;
1387 free_saved (dtp);
1388 if (c == EOF)
1390 hit_eof (dtp);
1391 return;
1393 else if (c != '\n')
1394 eat_line (dtp);
1395 snprintf (message, MSGLEN, "Bad complex value in item %d of list input",
1396 dtp->u.p.item_count);
1397 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1401 /* Parse a real number with a possible repeat count. */
1403 static void
1404 read_real (st_parameter_dt *dtp, void * dest, int length)
1406 char message[MSGLEN];
1407 int c;
1408 int seen_dp;
1409 int is_inf;
1411 seen_dp = 0;
1413 c = next_char (dtp);
1414 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1415 c = '.';
1416 switch (c)
1418 CASE_DIGITS:
1419 push_char (dtp, c);
1420 break;
1422 case '.':
1423 push_char (dtp, c);
1424 seen_dp = 1;
1425 break;
1427 case '+':
1428 case '-':
1429 goto got_sign;
1431 CASE_SEPARATORS:
1432 unget_char (dtp, c); /* Single null. */
1433 eat_separator (dtp);
1434 return;
1436 case 'i':
1437 case 'I':
1438 case 'n':
1439 case 'N':
1440 goto inf_nan;
1442 default:
1443 goto bad_real;
1446 /* Get the digit string that might be a repeat count. */
1448 for (;;)
1450 c = next_char (dtp);
1451 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1452 c = '.';
1453 switch (c)
1455 CASE_DIGITS:
1456 push_char (dtp, c);
1457 break;
1459 case '.':
1460 if (seen_dp)
1461 goto bad_real;
1463 seen_dp = 1;
1464 push_char (dtp, c);
1465 goto real_loop;
1467 case 'E':
1468 case 'e':
1469 case 'D':
1470 case 'd':
1471 case 'Q':
1472 case 'q':
1473 goto exp1;
1475 case '+':
1476 case '-':
1477 push_char (dtp, 'e');
1478 push_char (dtp, c);
1479 c = next_char (dtp);
1480 goto exp2;
1482 case '*':
1483 push_char (dtp, '\0');
1484 goto got_repeat;
1486 CASE_SEPARATORS:
1487 if (c != '\n' && c != ',' && c != '\r' && c != ';')
1488 unget_char (dtp, c);
1489 goto done;
1491 default:
1492 goto bad_real;
1496 got_repeat:
1497 if (convert_integer (dtp, -1, 0))
1498 return;
1500 /* Now get the number itself. */
1502 if ((c = next_char (dtp)) == EOF)
1503 goto bad_real;
1504 if (is_separator (c))
1505 { /* Repeated null value. */
1506 unget_char (dtp, c);
1507 eat_separator (dtp);
1508 return;
1511 if (c != '-' && c != '+')
1512 push_char (dtp, '+');
1513 else
1515 got_sign:
1516 push_char (dtp, c);
1517 if ((c = next_char (dtp)) == EOF)
1518 goto bad_real;
1521 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1522 c = '.';
1524 if (!isdigit (c) && c != '.')
1526 if (c == 'i' || c == 'I' || c == 'n' || c == 'N')
1527 goto inf_nan;
1528 else
1529 goto bad_real;
1532 if (c == '.')
1534 if (seen_dp)
1535 goto bad_real;
1536 else
1537 seen_dp = 1;
1540 push_char (dtp, c);
1542 real_loop:
1543 for (;;)
1545 c = next_char (dtp);
1546 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1547 c = '.';
1548 switch (c)
1550 CASE_DIGITS:
1551 push_char (dtp, c);
1552 break;
1554 CASE_SEPARATORS:
1555 case EOF:
1556 goto done;
1558 case '.':
1559 if (seen_dp)
1560 goto bad_real;
1562 seen_dp = 1;
1563 push_char (dtp, c);
1564 break;
1566 case 'E':
1567 case 'e':
1568 case 'D':
1569 case 'd':
1570 case 'Q':
1571 case 'q':
1572 goto exp1;
1574 case '+':
1575 case '-':
1576 push_char (dtp, 'e');
1577 push_char (dtp, c);
1578 c = next_char (dtp);
1579 goto exp2;
1581 default:
1582 goto bad_real;
1586 exp1:
1587 push_char (dtp, 'e');
1589 if ((c = next_char (dtp)) == EOF)
1590 goto bad_real;
1591 if (c != '+' && c != '-')
1592 push_char (dtp, '+');
1593 else
1595 push_char (dtp, c);
1596 c = next_char (dtp);
1599 exp2:
1600 if (!isdigit (c))
1601 goto bad_real;
1602 push_char (dtp, c);
1604 for (;;)
1606 c = next_char (dtp);
1608 switch (c)
1610 CASE_DIGITS:
1611 push_char (dtp, c);
1612 break;
1614 CASE_SEPARATORS:
1615 goto done;
1617 default:
1618 goto bad_real;
1622 done:
1623 unget_char (dtp, c);
1624 eat_separator (dtp);
1625 push_char (dtp, '\0');
1626 if (convert_real (dtp, dest, dtp->u.p.saved_string, length))
1627 return;
1629 free_saved (dtp);
1630 dtp->u.p.saved_type = BT_REAL;
1631 return;
1633 inf_nan:
1634 l_push_char (dtp, c);
1635 is_inf = 0;
1637 /* Match INF and Infinity. */
1638 if (c == 'i' || c == 'I')
1640 c = next_char (dtp);
1641 l_push_char (dtp, c);
1642 if (c != 'n' && c != 'N')
1643 goto unwind;
1644 c = next_char (dtp);
1645 l_push_char (dtp, c);
1646 if (c != 'f' && c != 'F')
1647 goto unwind;
1648 c = next_char (dtp);
1649 l_push_char (dtp, c);
1650 if (!is_separator (c))
1652 if (c != 'i' && c != 'I')
1653 goto unwind;
1654 c = next_char (dtp);
1655 l_push_char (dtp, c);
1656 if (c != 'n' && c != 'N')
1657 goto unwind;
1658 c = next_char (dtp);
1659 l_push_char (dtp, c);
1660 if (c != 'i' && c != 'I')
1661 goto unwind;
1662 c = next_char (dtp);
1663 l_push_char (dtp, c);
1664 if (c != 't' && c != 'T')
1665 goto unwind;
1666 c = next_char (dtp);
1667 l_push_char (dtp, c);
1668 if (c != 'y' && c != 'Y')
1669 goto unwind;
1670 c = next_char (dtp);
1671 l_push_char (dtp, c);
1673 is_inf = 1;
1674 } /* Match NaN. */
1675 else
1677 c = next_char (dtp);
1678 l_push_char (dtp, c);
1679 if (c != 'a' && c != 'A')
1680 goto unwind;
1681 c = next_char (dtp);
1682 l_push_char (dtp, c);
1683 if (c != 'n' && c != 'N')
1684 goto unwind;
1685 c = next_char (dtp);
1686 l_push_char (dtp, c);
1688 /* Match NAN(alphanum). */
1689 if (c == '(')
1691 for (c = next_char (dtp); c != ')'; c = next_char (dtp))
1692 if (is_separator (c))
1693 goto unwind;
1694 else
1695 l_push_char (dtp, c);
1697 l_push_char (dtp, ')');
1698 c = next_char (dtp);
1699 l_push_char (dtp, c);
1703 if (!is_separator (c))
1704 goto unwind;
1706 if (dtp->u.p.namelist_mode)
1708 if (c == ' ' || c =='\n' || c == '\r')
1712 if ((c = next_char (dtp)) == EOF)
1713 goto bad_real;
1715 while (c == ' ' || c =='\n' || c == '\r');
1717 l_push_char (dtp, c);
1719 if (c == '=')
1720 goto unwind;
1724 if (is_inf)
1726 push_char (dtp, 'i');
1727 push_char (dtp, 'n');
1728 push_char (dtp, 'f');
1730 else
1732 push_char (dtp, 'n');
1733 push_char (dtp, 'a');
1734 push_char (dtp, 'n');
1737 free_line (dtp);
1738 unget_char (dtp, c);
1739 eat_separator (dtp);
1740 push_char (dtp, '\0');
1741 if (convert_infnan (dtp, dest, dtp->u.p.saved_string, length))
1742 return;
1744 free_saved (dtp);
1745 dtp->u.p.saved_type = BT_REAL;
1746 return;
1748 unwind:
1749 if (dtp->u.p.namelist_mode)
1751 dtp->u.p.nml_read_error = 1;
1752 dtp->u.p.line_buffer_enabled = 1;
1753 dtp->u.p.item_count = 0;
1754 return;
1757 bad_real:
1759 if (nml_bad_return (dtp, c))
1760 return;
1762 free_saved (dtp);
1763 if (c == EOF)
1765 hit_eof (dtp);
1766 return;
1768 else if (c != '\n')
1769 eat_line (dtp);
1771 snprintf (message, MSGLEN, "Bad real number in item %d of list input",
1772 dtp->u.p.item_count);
1773 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1777 /* Check the current type against the saved type to make sure they are
1778 compatible. Returns nonzero if incompatible. */
1780 static int
1781 check_type (st_parameter_dt *dtp, bt type, int len)
1783 char message[MSGLEN];
1785 if (dtp->u.p.saved_type != BT_UNKNOWN && dtp->u.p.saved_type != type)
1787 snprintf (message, MSGLEN, "Read type %s where %s was expected for item %d",
1788 type_name (dtp->u.p.saved_type), type_name (type),
1789 dtp->u.p.item_count);
1791 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1792 return 1;
1795 if (dtp->u.p.saved_type == BT_UNKNOWN || dtp->u.p.saved_type == BT_CHARACTER)
1796 return 0;
1798 if (dtp->u.p.saved_length != len)
1800 snprintf (message, MSGLEN,
1801 "Read kind %d %s where kind %d is required for item %d",
1802 dtp->u.p.saved_length, type_name (dtp->u.p.saved_type), len,
1803 dtp->u.p.item_count);
1804 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1805 return 1;
1808 return 0;
1812 /* Top level data transfer subroutine for list reads. Because we have
1813 to deal with repeat counts, the data item is always saved after
1814 reading, usually in the dtp->u.p.value[] array. If a repeat count is
1815 greater than one, we copy the data item multiple times. */
1817 static int
1818 list_formatted_read_scalar (st_parameter_dt *dtp, bt type, void *p,
1819 int kind, size_t size)
1821 gfc_char4_t *q;
1822 int c, i, m;
1823 int err = 0;
1825 dtp->u.p.namelist_mode = 0;
1827 if (dtp->u.p.first_item)
1829 dtp->u.p.first_item = 0;
1830 dtp->u.p.input_complete = 0;
1831 dtp->u.p.repeat_count = 1;
1832 dtp->u.p.at_eol = 0;
1834 if ((c = eat_spaces (dtp)) == EOF)
1836 err = LIBERROR_END;
1837 goto cleanup;
1839 if (is_separator (c))
1841 /* Found a null value. */
1842 eat_separator (dtp);
1843 dtp->u.p.repeat_count = 0;
1845 /* eat_separator sets this flag if the separator was a comma. */
1846 if (dtp->u.p.comma_flag)
1847 goto cleanup;
1849 /* eat_separator sets this flag if the separator was a \n or \r. */
1850 if (dtp->u.p.at_eol)
1851 finish_separator (dtp);
1852 else
1853 goto cleanup;
1857 else
1859 if (dtp->u.p.repeat_count > 0)
1861 if (check_type (dtp, type, kind))
1862 return err;
1863 goto set_value;
1866 if (dtp->u.p.input_complete)
1867 goto cleanup;
1869 if (dtp->u.p.at_eol)
1870 finish_separator (dtp);
1871 else
1873 eat_spaces (dtp);
1874 /* Trailing spaces prior to end of line. */
1875 if (dtp->u.p.at_eol)
1876 finish_separator (dtp);
1879 dtp->u.p.saved_type = BT_UNKNOWN;
1880 dtp->u.p.repeat_count = 1;
1883 switch (type)
1885 case BT_INTEGER:
1886 read_integer (dtp, kind);
1887 break;
1888 case BT_LOGICAL:
1889 read_logical (dtp, kind);
1890 break;
1891 case BT_CHARACTER:
1892 read_character (dtp, kind);
1893 break;
1894 case BT_REAL:
1895 read_real (dtp, p, kind);
1896 /* Copy value back to temporary if needed. */
1897 if (dtp->u.p.repeat_count > 0)
1898 memcpy (dtp->u.p.value, p, size);
1899 break;
1900 case BT_COMPLEX:
1901 read_complex (dtp, p, kind, size);
1902 /* Copy value back to temporary if needed. */
1903 if (dtp->u.p.repeat_count > 0)
1904 memcpy (dtp->u.p.value, p, size);
1905 break;
1906 default:
1907 internal_error (&dtp->common, "Bad type for list read");
1910 if (dtp->u.p.saved_type != BT_CHARACTER && dtp->u.p.saved_type != BT_UNKNOWN)
1911 dtp->u.p.saved_length = size;
1913 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1914 goto cleanup;
1916 set_value:
1917 switch (dtp->u.p.saved_type)
1919 case BT_COMPLEX:
1920 case BT_REAL:
1921 if (dtp->u.p.repeat_count > 0)
1922 memcpy (p, dtp->u.p.value, size);
1923 break;
1925 case BT_INTEGER:
1926 case BT_LOGICAL:
1927 memcpy (p, dtp->u.p.value, size);
1928 break;
1930 case BT_CHARACTER:
1931 if (dtp->u.p.saved_string)
1933 m = ((int) size < dtp->u.p.saved_used)
1934 ? (int) size : dtp->u.p.saved_used;
1935 if (kind == 1)
1936 memcpy (p, dtp->u.p.saved_string, m);
1937 else
1939 q = (gfc_char4_t *) p;
1940 for (i = 0; i < m; i++)
1941 q[i] = (unsigned char) dtp->u.p.saved_string[i];
1944 else
1945 /* Just delimiters encountered, nothing to copy but SPACE. */
1946 m = 0;
1948 if (m < (int) size)
1950 if (kind == 1)
1951 memset (((char *) p) + m, ' ', size - m);
1952 else
1954 q = (gfc_char4_t *) p;
1955 for (i = m; i < (int) size; i++)
1956 q[i] = (unsigned char) ' ';
1959 break;
1961 case BT_UNKNOWN:
1962 break;
1964 default:
1965 internal_error (&dtp->common, "Bad type for list read");
1968 if (--dtp->u.p.repeat_count <= 0)
1969 free_saved (dtp);
1971 cleanup:
1972 if (err == LIBERROR_END)
1973 hit_eof (dtp);
1974 return err;
1978 void
1979 list_formatted_read (st_parameter_dt *dtp, bt type, void *p, int kind,
1980 size_t size, size_t nelems)
1982 size_t elem;
1983 char *tmp;
1984 size_t stride = type == BT_CHARACTER ?
1985 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
1986 int err;
1988 tmp = (char *) p;
1990 /* Big loop over all the elements. */
1991 for (elem = 0; elem < nelems; elem++)
1993 dtp->u.p.item_count++;
1994 err = list_formatted_read_scalar (dtp, type, tmp + stride*elem,
1995 kind, size);
1996 if (err)
1997 break;
2002 /* Finish a list read. */
2004 void
2005 finish_list_read (st_parameter_dt *dtp)
2007 int err;
2009 free_saved (dtp);
2011 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
2013 if (dtp->u.p.at_eol)
2015 dtp->u.p.at_eol = 0;
2016 return;
2019 err = eat_line (dtp);
2020 if (err == LIBERROR_END)
2021 hit_eof (dtp);
2024 /* NAMELIST INPUT
2026 void namelist_read (st_parameter_dt *dtp)
2027 calls:
2028 static void nml_match_name (char *name, int len)
2029 static int nml_query (st_parameter_dt *dtp)
2030 static int nml_get_obj_data (st_parameter_dt *dtp,
2031 namelist_info **prev_nl, char *, size_t)
2032 calls:
2033 static void nml_untouch_nodes (st_parameter_dt *dtp)
2034 static namelist_info * find_nml_node (st_parameter_dt *dtp,
2035 char * var_name)
2036 static int nml_parse_qualifier(descriptor_dimension * ad,
2037 array_loop_spec * ls, int rank, char *)
2038 static void nml_touch_nodes (namelist_info * nl)
2039 static int nml_read_obj (namelist_info *nl, index_type offset,
2040 namelist_info **prev_nl, char *, size_t,
2041 index_type clow, index_type chigh)
2042 calls:
2043 -itself- */
2045 /* Inputs a rank-dimensional qualifier, which can contain
2046 singlets, doublets, triplets or ':' with the standard meanings. */
2048 static try
2049 nml_parse_qualifier (st_parameter_dt *dtp, descriptor_dimension *ad,
2050 array_loop_spec *ls, int rank, char *parse_err_msg,
2051 size_t parse_err_msg_size,
2052 int *parsed_rank)
2054 int dim;
2055 int indx;
2056 int neg;
2057 int null_flag;
2058 int is_array_section, is_char;
2059 int c;
2061 is_char = 0;
2062 is_array_section = 0;
2063 dtp->u.p.expanded_read = 0;
2065 /* See if this is a character substring qualifier we are looking for. */
2066 if (rank == -1)
2068 rank = 1;
2069 is_char = 1;
2072 /* The next character in the stream should be the '('. */
2074 if ((c = next_char (dtp)) == EOF)
2075 return FAILURE;
2077 /* Process the qualifier, by dimension and triplet. */
2079 for (dim=0; dim < rank; dim++ )
2081 for (indx=0; indx<3; indx++)
2083 free_saved (dtp);
2084 eat_spaces (dtp);
2085 neg = 0;
2087 /* Process a potential sign. */
2088 if ((c = next_char (dtp)) == EOF)
2089 return FAILURE;
2090 switch (c)
2092 case '-':
2093 neg = 1;
2094 break;
2096 case '+':
2097 break;
2099 default:
2100 unget_char (dtp, c);
2101 break;
2104 /* Process characters up to the next ':' , ',' or ')'. */
2105 for (;;)
2107 if ((c = next_char (dtp)) == EOF)
2108 return FAILURE;
2110 switch (c)
2112 case ':':
2113 is_array_section = 1;
2114 break;
2116 case ',': case ')':
2117 if ((c==',' && dim == rank -1)
2118 || (c==')' && dim < rank -1))
2120 if (is_char)
2121 snprintf (parse_err_msg, parse_err_msg_size,
2122 "Bad substring qualifier");
2123 else
2124 snprintf (parse_err_msg, parse_err_msg_size,
2125 "Bad number of index fields");
2126 goto err_ret;
2128 break;
2130 CASE_DIGITS:
2131 push_char (dtp, c);
2132 continue;
2134 case ' ': case '\t':
2135 eat_spaces (dtp);
2136 if ((c = next_char (dtp) == EOF))
2137 return FAILURE;
2138 break;
2140 default:
2141 if (is_char)
2142 snprintf (parse_err_msg, parse_err_msg_size,
2143 "Bad character in substring qualifier");
2144 else
2145 snprintf (parse_err_msg, parse_err_msg_size,
2146 "Bad character in index");
2147 goto err_ret;
2150 if ((c == ',' || c == ')') && indx == 0
2151 && dtp->u.p.saved_string == 0)
2153 if (is_char)
2154 snprintf (parse_err_msg, parse_err_msg_size,
2155 "Null substring qualifier");
2156 else
2157 snprintf (parse_err_msg, parse_err_msg_size,
2158 "Null index field");
2159 goto err_ret;
2162 if ((c == ':' && indx == 1 && dtp->u.p.saved_string == 0)
2163 || (indx == 2 && dtp->u.p.saved_string == 0))
2165 if (is_char)
2166 snprintf (parse_err_msg, parse_err_msg_size,
2167 "Bad substring qualifier");
2168 else
2169 snprintf (parse_err_msg, parse_err_msg_size,
2170 "Bad index triplet");
2171 goto err_ret;
2174 if (is_char && !is_array_section)
2176 snprintf (parse_err_msg, parse_err_msg_size,
2177 "Missing colon in substring qualifier");
2178 goto err_ret;
2181 /* If '( : ? )' or '( ? : )' break and flag read failure. */
2182 null_flag = 0;
2183 if ((c == ':' && indx == 0 && dtp->u.p.saved_string == 0)
2184 || (indx==1 && dtp->u.p.saved_string == 0))
2186 null_flag = 1;
2187 break;
2190 /* Now read the index. */
2191 if (convert_integer (dtp, sizeof(index_type), neg))
2193 if (is_char)
2194 snprintf (parse_err_msg, parse_err_msg_size,
2195 "Bad integer substring qualifier");
2196 else
2197 snprintf (parse_err_msg, parse_err_msg_size,
2198 "Bad integer in index");
2199 goto err_ret;
2201 break;
2204 /* Feed the index values to the triplet arrays. */
2205 if (!null_flag)
2207 if (indx == 0)
2208 memcpy (&ls[dim].start, dtp->u.p.value, sizeof(index_type));
2209 if (indx == 1)
2210 memcpy (&ls[dim].end, dtp->u.p.value, sizeof(index_type));
2211 if (indx == 2)
2212 memcpy (&ls[dim].step, dtp->u.p.value, sizeof(index_type));
2215 /* Singlet or doublet indices. */
2216 if (c==',' || c==')')
2218 if (indx == 0)
2220 memcpy (&ls[dim].start, dtp->u.p.value, sizeof(index_type));
2222 /* If -std=f95/2003 or an array section is specified,
2223 do not allow excess data to be processed. */
2224 if (is_array_section == 1
2225 || !(compile_options.allow_std & GFC_STD_GNU)
2226 || dtp->u.p.ionml->type == BT_DERIVED)
2227 ls[dim].end = ls[dim].start;
2228 else
2229 dtp->u.p.expanded_read = 1;
2232 /* Check for non-zero rank. */
2233 if (is_array_section == 1 && ls[dim].start != ls[dim].end)
2234 *parsed_rank = 1;
2236 break;
2240 if (is_array_section == 1 && dtp->u.p.expanded_read == 1)
2242 int i;
2243 dtp->u.p.expanded_read = 0;
2244 for (i = 0; i < dim; i++)
2245 ls[i].end = ls[i].start;
2248 /* Check the values of the triplet indices. */
2249 if ((ls[dim].start > GFC_DIMENSION_UBOUND(ad[dim]))
2250 || (ls[dim].start < GFC_DIMENSION_LBOUND(ad[dim]))
2251 || (ls[dim].end > GFC_DIMENSION_UBOUND(ad[dim]))
2252 || (ls[dim].end < GFC_DIMENSION_LBOUND(ad[dim])))
2254 if (is_char)
2255 snprintf (parse_err_msg, parse_err_msg_size,
2256 "Substring out of range");
2257 else
2258 snprintf (parse_err_msg, parse_err_msg_size,
2259 "Index %d out of range", dim + 1);
2260 goto err_ret;
2263 if (((ls[dim].end - ls[dim].start ) * ls[dim].step < 0)
2264 || (ls[dim].step == 0))
2266 snprintf (parse_err_msg, parse_err_msg_size,
2267 "Bad range in index %d", dim + 1);
2268 goto err_ret;
2271 /* Initialise the loop index counter. */
2272 ls[dim].idx = ls[dim].start;
2274 eat_spaces (dtp);
2275 return SUCCESS;
2277 err_ret:
2279 return FAILURE;
2282 static namelist_info *
2283 find_nml_node (st_parameter_dt *dtp, char * var_name)
2285 namelist_info * t = dtp->u.p.ionml;
2286 while (t != NULL)
2288 if (strcmp (var_name, t->var_name) == 0)
2290 t->touched = 1;
2291 return t;
2293 t = t->next;
2295 return NULL;
2298 /* Visits all the components of a derived type that have
2299 not explicitly been identified in the namelist input.
2300 touched is set and the loop specification initialised
2301 to default values */
2303 static void
2304 nml_touch_nodes (namelist_info * nl)
2306 index_type len = strlen (nl->var_name) + 1;
2307 int dim;
2308 char * ext_name = (char*)xmalloc (len + 1);
2309 memcpy (ext_name, nl->var_name, len-1);
2310 memcpy (ext_name + len - 1, "%", 2);
2311 for (nl = nl->next; nl; nl = nl->next)
2313 if (strncmp (nl->var_name, ext_name, len) == 0)
2315 nl->touched = 1;
2316 for (dim=0; dim < nl->var_rank; dim++)
2318 nl->ls[dim].step = 1;
2319 nl->ls[dim].end = GFC_DESCRIPTOR_UBOUND(nl,dim);
2320 nl->ls[dim].start = GFC_DESCRIPTOR_LBOUND(nl,dim);
2321 nl->ls[dim].idx = nl->ls[dim].start;
2324 else
2325 break;
2327 free (ext_name);
2328 return;
2331 /* Resets touched for the entire list of nml_nodes, ready for a
2332 new object. */
2334 static void
2335 nml_untouch_nodes (st_parameter_dt *dtp)
2337 namelist_info * t;
2338 for (t = dtp->u.p.ionml; t; t = t->next)
2339 t->touched = 0;
2340 return;
2343 /* Attempts to input name to namelist name. Returns
2344 dtp->u.p.nml_read_error = 1 on no match. */
2346 static void
2347 nml_match_name (st_parameter_dt *dtp, const char *name, index_type len)
2349 index_type i;
2350 int c;
2352 dtp->u.p.nml_read_error = 0;
2353 for (i = 0; i < len; i++)
2355 c = next_char (dtp);
2356 if (c == EOF || (tolower (c) != tolower (name[i])))
2358 dtp->u.p.nml_read_error = 1;
2359 break;
2364 /* If the namelist read is from stdin, output the current state of the
2365 namelist to stdout. This is used to implement the non-standard query
2366 features, ? and =?. If c == '=' the full namelist is printed. Otherwise
2367 the names alone are printed. */
2369 static void
2370 nml_query (st_parameter_dt *dtp, char c)
2372 gfc_unit * temp_unit;
2373 namelist_info * nl;
2374 index_type len;
2375 char * p;
2376 #ifdef HAVE_CRLF
2377 static const index_type endlen = 3;
2378 static const char endl[] = "\r\n";
2379 static const char nmlend[] = "&end\r\n";
2380 #else
2381 static const index_type endlen = 2;
2382 static const char endl[] = "\n";
2383 static const char nmlend[] = "&end\n";
2384 #endif
2386 if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
2387 return;
2389 /* Store the current unit and transfer to stdout. */
2391 temp_unit = dtp->u.p.current_unit;
2392 dtp->u.p.current_unit = find_unit (options.stdout_unit);
2394 if (dtp->u.p.current_unit)
2396 dtp->u.p.mode = WRITING;
2397 next_record (dtp, 0);
2399 /* Write the namelist in its entirety. */
2401 if (c == '=')
2402 namelist_write (dtp);
2404 /* Or write the list of names. */
2406 else
2408 /* "&namelist_name\n" */
2410 len = dtp->namelist_name_len;
2411 p = write_block (dtp, len + endlen);
2412 if (!p)
2413 goto query_return;
2414 memcpy (p, "&", 1);
2415 memcpy ((char*)(p + 1), dtp->namelist_name, len);
2416 memcpy ((char*)(p + len + 1), &endl, endlen - 1);
2417 for (nl = dtp->u.p.ionml; nl; nl = nl->next)
2419 /* " var_name\n" */
2421 len = strlen (nl->var_name);
2422 p = write_block (dtp, len + endlen);
2423 if (!p)
2424 goto query_return;
2425 memcpy (p, " ", 1);
2426 memcpy ((char*)(p + 1), nl->var_name, len);
2427 memcpy ((char*)(p + len + 1), &endl, endlen - 1);
2430 /* "&end\n" */
2432 p = write_block (dtp, endlen + 3);
2433 goto query_return;
2434 memcpy (p, &nmlend, endlen + 3);
2437 /* Flush the stream to force immediate output. */
2439 fbuf_flush (dtp->u.p.current_unit, WRITING);
2440 sflush (dtp->u.p.current_unit->s);
2441 unlock_unit (dtp->u.p.current_unit);
2444 query_return:
2446 /* Restore the current unit. */
2448 dtp->u.p.current_unit = temp_unit;
2449 dtp->u.p.mode = READING;
2450 return;
2453 /* Reads and stores the input for the namelist object nl. For an array,
2454 the function loops over the ranges defined by the loop specification.
2455 This default to all the data or to the specification from a qualifier.
2456 nml_read_obj recursively calls itself to read derived types. It visits
2457 all its own components but only reads data for those that were touched
2458 when the name was parsed. If a read error is encountered, an attempt is
2459 made to return to read a new object name because the standard allows too
2460 little data to be available. On the other hand, too much data is an
2461 error. */
2463 static try
2464 nml_read_obj (st_parameter_dt *dtp, namelist_info * nl, index_type offset,
2465 namelist_info **pprev_nl, char *nml_err_msg,
2466 size_t nml_err_msg_size, index_type clow, index_type chigh)
2468 namelist_info * cmp;
2469 char * obj_name;
2470 int nml_carry;
2471 int len;
2472 int dim;
2473 index_type dlen;
2474 index_type m;
2475 size_t obj_name_len;
2476 void * pdata;
2478 /* This object not touched in name parsing. */
2480 if (!nl->touched)
2481 return SUCCESS;
2483 dtp->u.p.repeat_count = 0;
2484 eat_spaces (dtp);
2486 len = nl->len;
2487 switch (nl->type)
2489 case BT_INTEGER:
2490 case BT_LOGICAL:
2491 dlen = len;
2492 break;
2494 case BT_REAL:
2495 dlen = size_from_real_kind (len);
2496 break;
2498 case BT_COMPLEX:
2499 dlen = size_from_complex_kind (len);
2500 break;
2502 case BT_CHARACTER:
2503 dlen = chigh ? (chigh - clow + 1) : nl->string_length;
2504 break;
2506 default:
2507 dlen = 0;
2512 /* Update the pointer to the data, using the current index vector */
2514 pdata = (void*)(nl->mem_pos + offset);
2515 for (dim = 0; dim < nl->var_rank; dim++)
2516 pdata = (void*)(pdata + (nl->ls[dim].idx
2517 - GFC_DESCRIPTOR_LBOUND(nl,dim))
2518 * GFC_DESCRIPTOR_STRIDE(nl,dim) * nl->size);
2520 /* Reset the error flag and try to read next value, if
2521 dtp->u.p.repeat_count=0 */
2523 dtp->u.p.nml_read_error = 0;
2524 nml_carry = 0;
2525 if (--dtp->u.p.repeat_count <= 0)
2527 if (dtp->u.p.input_complete)
2528 return SUCCESS;
2529 if (dtp->u.p.at_eol)
2530 finish_separator (dtp);
2531 if (dtp->u.p.input_complete)
2532 return SUCCESS;
2534 dtp->u.p.saved_type = BT_UNKNOWN;
2535 free_saved (dtp);
2537 switch (nl->type)
2539 case BT_INTEGER:
2540 read_integer (dtp, len);
2541 break;
2543 case BT_LOGICAL:
2544 read_logical (dtp, len);
2545 break;
2547 case BT_CHARACTER:
2548 read_character (dtp, len);
2549 break;
2551 case BT_REAL:
2552 /* Need to copy data back from the real location to the temp in order
2553 to handle nml reads into arrays. */
2554 read_real (dtp, pdata, len);
2555 memcpy (dtp->u.p.value, pdata, dlen);
2556 break;
2558 case BT_COMPLEX:
2559 /* Same as for REAL, copy back to temp. */
2560 read_complex (dtp, pdata, len, dlen);
2561 memcpy (dtp->u.p.value, pdata, dlen);
2562 break;
2564 case BT_DERIVED:
2565 obj_name_len = strlen (nl->var_name) + 1;
2566 obj_name = xmalloc (obj_name_len+1);
2567 memcpy (obj_name, nl->var_name, obj_name_len-1);
2568 memcpy (obj_name + obj_name_len - 1, "%", 2);
2570 /* If reading a derived type, disable the expanded read warning
2571 since a single object can have multiple reads. */
2572 dtp->u.p.expanded_read = 0;
2574 /* Now loop over the components. Update the component pointer
2575 with the return value from nml_write_obj. This loop jumps
2576 past nested derived types by testing if the potential
2577 component name contains '%'. */
2579 for (cmp = nl->next;
2580 cmp &&
2581 !strncmp (cmp->var_name, obj_name, obj_name_len) &&
2582 !strchr (cmp->var_name + obj_name_len, '%');
2583 cmp = cmp->next)
2586 if (nml_read_obj (dtp, cmp, (index_type)(pdata - nl->mem_pos),
2587 pprev_nl, nml_err_msg, nml_err_msg_size,
2588 clow, chigh) == FAILURE)
2590 free (obj_name);
2591 return FAILURE;
2594 if (dtp->u.p.input_complete)
2596 free (obj_name);
2597 return SUCCESS;
2601 free (obj_name);
2602 goto incr_idx;
2604 default:
2605 snprintf (nml_err_msg, nml_err_msg_size,
2606 "Bad type for namelist object %s", nl->var_name);
2607 internal_error (&dtp->common, nml_err_msg);
2608 goto nml_err_ret;
2612 /* The standard permits array data to stop short of the number of
2613 elements specified in the loop specification. In this case, we
2614 should be here with dtp->u.p.nml_read_error != 0. Control returns to
2615 nml_get_obj_data and an attempt is made to read object name. */
2617 *pprev_nl = nl;
2618 if (dtp->u.p.nml_read_error)
2620 dtp->u.p.expanded_read = 0;
2621 return SUCCESS;
2624 if (dtp->u.p.saved_type == BT_UNKNOWN)
2626 dtp->u.p.expanded_read = 0;
2627 goto incr_idx;
2630 switch (dtp->u.p.saved_type)
2633 case BT_COMPLEX:
2634 case BT_REAL:
2635 case BT_INTEGER:
2636 case BT_LOGICAL:
2637 memcpy (pdata, dtp->u.p.value, dlen);
2638 break;
2640 case BT_CHARACTER:
2641 if (dlen < dtp->u.p.saved_used)
2643 if (compile_options.bounds_check)
2645 snprintf (nml_err_msg, nml_err_msg_size,
2646 "Namelist object '%s' truncated on read.",
2647 nl->var_name);
2648 generate_warning (&dtp->common, nml_err_msg);
2650 m = dlen;
2652 else
2653 m = dtp->u.p.saved_used;
2654 pdata = (void*)( pdata + clow - 1 );
2655 memcpy (pdata, dtp->u.p.saved_string, m);
2656 if (m < dlen)
2657 memset ((void*)( pdata + m ), ' ', dlen - m);
2658 break;
2660 default:
2661 break;
2664 /* Warn if a non-standard expanded read occurs. A single read of a
2665 single object is acceptable. If a second read occurs, issue a warning
2666 and set the flag to zero to prevent further warnings. */
2667 if (dtp->u.p.expanded_read == 2)
2669 notify_std (&dtp->common, GFC_STD_GNU, "Non-standard expanded namelist read.");
2670 dtp->u.p.expanded_read = 0;
2673 /* If the expanded read warning flag is set, increment it,
2674 indicating that a single read has occurred. */
2675 if (dtp->u.p.expanded_read >= 1)
2676 dtp->u.p.expanded_read++;
2678 /* Break out of loop if scalar. */
2679 if (!nl->var_rank)
2680 break;
2682 /* Now increment the index vector. */
2684 incr_idx:
2686 nml_carry = 1;
2687 for (dim = 0; dim < nl->var_rank; dim++)
2689 nl->ls[dim].idx += nml_carry * nl->ls[dim].step;
2690 nml_carry = 0;
2691 if (((nl->ls[dim].step > 0) && (nl->ls[dim].idx > nl->ls[dim].end))
2693 ((nl->ls[dim].step < 0) && (nl->ls[dim].idx < nl->ls[dim].end)))
2695 nl->ls[dim].idx = nl->ls[dim].start;
2696 nml_carry = 1;
2699 } while (!nml_carry);
2701 if (dtp->u.p.repeat_count > 1)
2703 snprintf (nml_err_msg, nml_err_msg_size,
2704 "Repeat count too large for namelist object %s", nl->var_name);
2705 goto nml_err_ret;
2707 return SUCCESS;
2709 nml_err_ret:
2711 return FAILURE;
2714 /* Parses the object name, including array and substring qualifiers. It
2715 iterates over derived type components, touching those components and
2716 setting their loop specifications, if there is a qualifier. If the
2717 object is itself a derived type, its components and subcomponents are
2718 touched. nml_read_obj is called at the end and this reads the data in
2719 the manner specified by the object name. */
2721 static try
2722 nml_get_obj_data (st_parameter_dt *dtp, namelist_info **pprev_nl,
2723 char *nml_err_msg, size_t nml_err_msg_size)
2725 int c;
2726 namelist_info * nl;
2727 namelist_info * first_nl = NULL;
2728 namelist_info * root_nl = NULL;
2729 int dim, parsed_rank;
2730 int component_flag, qualifier_flag;
2731 index_type clow, chigh;
2732 int non_zero_rank_count;
2734 /* Look for end of input or object name. If '?' or '=?' are encountered
2735 in stdin, print the node names or the namelist to stdout. */
2737 eat_separator (dtp);
2738 if (dtp->u.p.input_complete)
2739 return SUCCESS;
2741 if (dtp->u.p.at_eol)
2742 finish_separator (dtp);
2743 if (dtp->u.p.input_complete)
2744 return SUCCESS;
2746 if ((c = next_char (dtp)) == EOF)
2747 return FAILURE;
2748 switch (c)
2750 case '=':
2751 if ((c = next_char (dtp)) == EOF)
2752 return FAILURE;
2753 if (c != '?')
2755 snprintf (nml_err_msg, nml_err_msg_size,
2756 "namelist read: misplaced = sign");
2757 goto nml_err_ret;
2759 nml_query (dtp, '=');
2760 return SUCCESS;
2762 case '?':
2763 nml_query (dtp, '?');
2764 return SUCCESS;
2766 case '$':
2767 case '&':
2768 nml_match_name (dtp, "end", 3);
2769 if (dtp->u.p.nml_read_error)
2771 snprintf (nml_err_msg, nml_err_msg_size,
2772 "namelist not terminated with / or &end");
2773 goto nml_err_ret;
2775 case '/':
2776 dtp->u.p.input_complete = 1;
2777 return SUCCESS;
2779 default :
2780 break;
2783 /* Untouch all nodes of the namelist and reset the flags that are set for
2784 derived type components. */
2786 nml_untouch_nodes (dtp);
2787 component_flag = 0;
2788 qualifier_flag = 0;
2789 non_zero_rank_count = 0;
2791 /* Get the object name - should '!' and '\n' be permitted separators? */
2793 get_name:
2795 free_saved (dtp);
2799 if (!is_separator (c))
2800 push_char (dtp, tolower(c));
2801 if ((c = next_char (dtp)) == EOF)
2802 return FAILURE;
2803 } while (!( c=='=' || c==' ' || c=='\t' || c =='(' || c =='%' ));
2805 unget_char (dtp, c);
2807 /* Check that the name is in the namelist and get pointer to object.
2808 Three error conditions exist: (i) An attempt is being made to
2809 identify a non-existent object, following a failed data read or
2810 (ii) The object name does not exist or (iii) Too many data items
2811 are present for an object. (iii) gives the same error message
2812 as (i) */
2814 push_char (dtp, '\0');
2816 if (component_flag)
2818 size_t var_len = strlen (root_nl->var_name);
2819 size_t saved_len
2820 = dtp->u.p.saved_string ? strlen (dtp->u.p.saved_string) : 0;
2821 char ext_name[var_len + saved_len + 1];
2823 memcpy (ext_name, root_nl->var_name, var_len);
2824 if (dtp->u.p.saved_string)
2825 memcpy (ext_name + var_len, dtp->u.p.saved_string, saved_len);
2826 ext_name[var_len + saved_len] = '\0';
2827 nl = find_nml_node (dtp, ext_name);
2829 else
2830 nl = find_nml_node (dtp, dtp->u.p.saved_string);
2832 if (nl == NULL)
2834 if (dtp->u.p.nml_read_error && *pprev_nl)
2835 snprintf (nml_err_msg, nml_err_msg_size,
2836 "Bad data for namelist object %s", (*pprev_nl)->var_name);
2838 else
2839 snprintf (nml_err_msg, nml_err_msg_size,
2840 "Cannot match namelist object name %s",
2841 dtp->u.p.saved_string);
2843 goto nml_err_ret;
2846 /* Get the length, data length, base pointer and rank of the variable.
2847 Set the default loop specification first. */
2849 for (dim=0; dim < nl->var_rank; dim++)
2851 nl->ls[dim].step = 1;
2852 nl->ls[dim].end = GFC_DESCRIPTOR_UBOUND(nl,dim);
2853 nl->ls[dim].start = GFC_DESCRIPTOR_LBOUND(nl,dim);
2854 nl->ls[dim].idx = nl->ls[dim].start;
2857 /* Check to see if there is a qualifier: if so, parse it.*/
2859 if (c == '(' && nl->var_rank)
2861 parsed_rank = 0;
2862 if (nml_parse_qualifier (dtp, nl->dim, nl->ls, nl->var_rank,
2863 nml_err_msg, nml_err_msg_size,
2864 &parsed_rank) == FAILURE)
2866 char *nml_err_msg_end = strchr (nml_err_msg, '\0');
2867 snprintf (nml_err_msg_end,
2868 nml_err_msg_size - (nml_err_msg_end - nml_err_msg),
2869 " for namelist variable %s", nl->var_name);
2870 goto nml_err_ret;
2872 if (parsed_rank > 0)
2873 non_zero_rank_count++;
2875 qualifier_flag = 1;
2877 if ((c = next_char (dtp)) == EOF)
2878 return FAILURE;
2879 unget_char (dtp, c);
2881 else if (nl->var_rank > 0)
2882 non_zero_rank_count++;
2884 /* Now parse a derived type component. The root namelist_info address
2885 is backed up, as is the previous component level. The component flag
2886 is set and the iteration is made by jumping back to get_name. */
2888 if (c == '%')
2890 if (nl->type != BT_DERIVED)
2892 snprintf (nml_err_msg, nml_err_msg_size,
2893 "Attempt to get derived component for %s", nl->var_name);
2894 goto nml_err_ret;
2897 if (*pprev_nl == NULL || !component_flag)
2898 first_nl = nl;
2900 root_nl = nl;
2902 component_flag = 1;
2903 if ((c = next_char (dtp)) == EOF)
2904 return FAILURE;
2905 goto get_name;
2908 /* Parse a character qualifier, if present. chigh = 0 is a default
2909 that signals that the string length = string_length. */
2911 clow = 1;
2912 chigh = 0;
2914 if (c == '(' && nl->type == BT_CHARACTER)
2916 descriptor_dimension chd[1] = { {1, clow, nl->string_length} };
2917 array_loop_spec ind[1] = { {1, clow, nl->string_length, 1} };
2919 if (nml_parse_qualifier (dtp, chd, ind, -1, nml_err_msg,
2920 nml_err_msg_size, &parsed_rank)
2921 == FAILURE)
2923 char *nml_err_msg_end = strchr (nml_err_msg, '\0');
2924 snprintf (nml_err_msg_end,
2925 nml_err_msg_size - (nml_err_msg_end - nml_err_msg),
2926 " for namelist variable %s", nl->var_name);
2927 goto nml_err_ret;
2930 clow = ind[0].start;
2931 chigh = ind[0].end;
2933 if (ind[0].step != 1)
2935 snprintf (nml_err_msg, nml_err_msg_size,
2936 "Step not allowed in substring qualifier"
2937 " for namelist object %s", nl->var_name);
2938 goto nml_err_ret;
2941 if ((c = next_char (dtp)) == EOF)
2942 return FAILURE;
2943 unget_char (dtp, c);
2946 /* Make sure no extraneous qualifiers are there. */
2948 if (c == '(')
2950 snprintf (nml_err_msg, nml_err_msg_size,
2951 "Qualifier for a scalar or non-character namelist object %s",
2952 nl->var_name);
2953 goto nml_err_ret;
2956 /* Make sure there is no more than one non-zero rank object. */
2957 if (non_zero_rank_count > 1)
2959 snprintf (nml_err_msg, nml_err_msg_size,
2960 "Multiple sub-objects with non-zero rank in namelist object %s",
2961 nl->var_name);
2962 non_zero_rank_count = 0;
2963 goto nml_err_ret;
2966 /* According to the standard, an equal sign MUST follow an object name. The
2967 following is possibly lax - it allows comments, blank lines and so on to
2968 intervene. eat_spaces (dtp); c = next_char (dtp); would be compliant*/
2970 free_saved (dtp);
2972 eat_separator (dtp);
2973 if (dtp->u.p.input_complete)
2974 return SUCCESS;
2976 if (dtp->u.p.at_eol)
2977 finish_separator (dtp);
2978 if (dtp->u.p.input_complete)
2979 return SUCCESS;
2981 if ((c = next_char (dtp)) == EOF)
2982 return FAILURE;
2984 if (c != '=')
2986 snprintf (nml_err_msg, nml_err_msg_size,
2987 "Equal sign must follow namelist object name %s",
2988 nl->var_name);
2989 goto nml_err_ret;
2991 /* If a derived type, touch its components and restore the root
2992 namelist_info if we have parsed a qualified derived type
2993 component. */
2995 if (nl->type == BT_DERIVED)
2996 nml_touch_nodes (nl);
2998 if (first_nl)
3000 if (first_nl->var_rank == 0)
3002 if (component_flag && qualifier_flag)
3003 nl = first_nl;
3005 else
3006 nl = first_nl;
3009 if (nml_read_obj (dtp, nl, 0, pprev_nl, nml_err_msg, nml_err_msg_size,
3010 clow, chigh) == FAILURE)
3011 goto nml_err_ret;
3013 return SUCCESS;
3015 nml_err_ret:
3017 return FAILURE;
3020 /* Entry point for namelist input. Goes through input until namelist name
3021 is matched. Then cycles through nml_get_obj_data until the input is
3022 completed or there is an error. */
3024 void
3025 namelist_read (st_parameter_dt *dtp)
3027 int c;
3028 char nml_err_msg[200];
3030 /* Initialize the error string buffer just in case we get an unexpected fail
3031 somewhere and end up at nml_err_ret. */
3032 strcpy (nml_err_msg, "Internal namelist read error");
3034 /* Pointer to the previously read object, in case attempt is made to read
3035 new object name. Should this fail, error message can give previous
3036 name. */
3037 namelist_info *prev_nl = NULL;
3039 dtp->u.p.namelist_mode = 1;
3040 dtp->u.p.input_complete = 0;
3041 dtp->u.p.expanded_read = 0;
3043 /* Look for &namelist_name . Skip all characters, testing for $nmlname.
3044 Exit on success or EOF. If '?' or '=?' encountered in stdin, print
3045 node names or namelist on stdout. */
3047 find_nml_name:
3048 c = next_char (dtp);
3049 switch (c)
3051 case '$':
3052 case '&':
3053 break;
3055 case '!':
3056 eat_line (dtp);
3057 goto find_nml_name;
3059 case '=':
3060 c = next_char (dtp);
3061 if (c == '?')
3062 nml_query (dtp, '=');
3063 else
3064 unget_char (dtp, c);
3065 goto find_nml_name;
3067 case '?':
3068 nml_query (dtp, '?');
3070 case EOF:
3071 return;
3073 default:
3074 goto find_nml_name;
3077 /* Match the name of the namelist. */
3079 nml_match_name (dtp, dtp->namelist_name, dtp->namelist_name_len);
3081 if (dtp->u.p.nml_read_error)
3082 goto find_nml_name;
3084 /* A trailing space is required, we give a little latitude here, 10.9.1. */
3085 c = next_char (dtp);
3086 if (!is_separator(c) && c != '!')
3088 unget_char (dtp, c);
3089 goto find_nml_name;
3092 unget_char (dtp, c);
3093 eat_separator (dtp);
3095 /* Ready to read namelist objects. If there is an error in input
3096 from stdin, output the error message and continue. */
3098 while (!dtp->u.p.input_complete)
3100 if (nml_get_obj_data (dtp, &prev_nl, nml_err_msg, sizeof nml_err_msg)
3101 == FAILURE)
3103 if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
3104 goto nml_err_ret;
3105 generate_error (&dtp->common, LIBERROR_READ_VALUE, nml_err_msg);
3108 /* Reset the previous namelist pointer if we know we are not going
3109 to be doing multiple reads within a single namelist object. */
3110 if (prev_nl && prev_nl->var_rank == 0)
3111 prev_nl = NULL;
3114 free_saved (dtp);
3115 free_line (dtp);
3116 return;
3119 nml_err_ret:
3121 /* All namelist error calls return from here */
3122 free_saved (dtp);
3123 free_line (dtp);
3124 generate_error (&dtp->common, LIBERROR_READ_VALUE, nml_err_msg);
3125 return;