* io/list_read.c (list_formatted_read_scalar): Fix copying real
[official-gcc.git] / libgfortran / io / list_read.c
blob9d301d6241894104381e036e525aa5e02f5b289c
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 char cc;
203 length = sread (dtp->u.p.current_unit->s, &cc, 1);
204 c = cc;
205 if (length < 0)
207 generate_error (&dtp->common, LIBERROR_OS, NULL);
208 return '\0';
211 if (is_array_io (dtp))
213 /* Check whether we hit EOF. */
214 if (length == 0)
216 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
217 return '\0';
219 dtp->u.p.current_unit->bytes_left--;
221 else
223 if (dtp->u.p.at_eof)
224 return EOF;
225 if (length == 0)
227 c = '\n';
228 dtp->u.p.at_eof = 1;
232 else
234 c = fbuf_getc (dtp->u.p.current_unit);
235 if (c != EOF && is_stream_io (dtp))
236 dtp->u.p.current_unit->strm_pos++;
238 done:
239 dtp->u.p.at_eol = (c == '\n' || c == '\r' || c == EOF);
240 return c;
244 /* Push a character back onto the input. */
246 static void
247 unget_char (st_parameter_dt *dtp, int c)
249 dtp->u.p.last_char = c;
253 /* Skip over spaces in the input. Returns the nonspace character that
254 terminated the eating and also places it back on the input. */
256 static int
257 eat_spaces (st_parameter_dt *dtp)
259 int c;
262 c = next_char (dtp);
263 while (c != EOF && (c == ' ' || c == '\t'));
265 unget_char (dtp, c);
266 return c;
270 /* This function reads characters through to the end of the current
271 line and just ignores them. Returns 0 for success and LIBERROR_END
272 if it hit EOF. */
274 static int
275 eat_line (st_parameter_dt *dtp)
277 int c;
280 c = next_char (dtp);
281 while (c != EOF && c != '\n');
282 if (c == EOF)
283 return LIBERROR_END;
284 return 0;
288 /* Skip over a separator. Technically, we don't always eat the whole
289 separator. This is because if we've processed the last input item,
290 then a separator is unnecessary. Plus the fact that operating
291 systems usually deliver console input on a line basis.
293 The upshot is that if we see a newline as part of reading a
294 separator, we stop reading. If there are more input items, we
295 continue reading the separator with finish_separator() which takes
296 care of the fact that we may or may not have seen a comma as part
297 of the separator.
299 Returns 0 for success, and non-zero error code otherwise. */
301 static int
302 eat_separator (st_parameter_dt *dtp)
304 int c, n;
305 int err = 0;
307 eat_spaces (dtp);
308 dtp->u.p.comma_flag = 0;
310 if ((c = next_char (dtp)) == EOF)
311 return LIBERROR_END;
312 switch (c)
314 case ',':
315 if (dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
317 unget_char (dtp, c);
318 break;
320 /* Fall through. */
321 case ';':
322 dtp->u.p.comma_flag = 1;
323 eat_spaces (dtp);
324 break;
326 case '/':
327 dtp->u.p.input_complete = 1;
328 break;
330 case '\r':
331 dtp->u.p.at_eol = 1;
332 if ((n = next_char(dtp)) == EOF)
333 return LIBERROR_END;
334 if (n != '\n')
336 unget_char (dtp, n);
337 break;
339 /* Fall through. */
340 case '\n':
341 dtp->u.p.at_eol = 1;
342 if (dtp->u.p.namelist_mode)
346 if ((c = next_char (dtp)) == EOF)
347 return LIBERROR_END;
348 if (c == '!')
350 err = eat_line (dtp);
351 if (err)
352 return err;
353 c = '\n';
356 while (c == '\n' || c == '\r' || c == ' ' || c == '\t');
357 unget_char (dtp, c);
359 break;
361 case '!':
362 if (dtp->u.p.namelist_mode)
363 { /* Eat a namelist comment. */
364 err = eat_line (dtp);
365 if (err)
366 return err;
368 break;
371 /* Fall Through... */
373 default:
374 unget_char (dtp, c);
375 break;
377 return err;
381 /* Finish processing a separator that was interrupted by a newline.
382 If we're here, then another data item is present, so we finish what
383 we started on the previous line. Return 0 on success, error code
384 on failure. */
386 static int
387 finish_separator (st_parameter_dt *dtp)
389 int c;
390 int err;
392 restart:
393 eat_spaces (dtp);
395 if ((c = next_char (dtp)) == EOF)
396 return LIBERROR_END;
397 switch (c)
399 case ',':
400 if (dtp->u.p.comma_flag)
401 unget_char (dtp, c);
402 else
404 if ((c = eat_spaces (dtp)) == EOF)
405 return LIBERROR_END;
406 if (c == '\n' || c == '\r')
407 goto restart;
410 break;
412 case '/':
413 dtp->u.p.input_complete = 1;
414 if (!dtp->u.p.namelist_mode)
415 return err;
416 break;
418 case '\n':
419 case '\r':
420 goto restart;
422 case '!':
423 if (dtp->u.p.namelist_mode)
425 err = eat_line (dtp);
426 if (err)
427 return err;
428 goto restart;
431 default:
432 unget_char (dtp, c);
433 break;
435 return err;
439 /* This function is needed to catch bad conversions so that namelist can
440 attempt to see if dtp->u.p.saved_string contains a new object name rather
441 than a bad value. */
443 static int
444 nml_bad_return (st_parameter_dt *dtp, char c)
446 if (dtp->u.p.namelist_mode)
448 dtp->u.p.nml_read_error = 1;
449 unget_char (dtp, c);
450 return 1;
452 return 0;
455 /* Convert an unsigned string to an integer. The length value is -1
456 if we are working on a repeat count. Returns nonzero if we have a
457 range problem. As a side effect, frees the dtp->u.p.saved_string. */
459 static int
460 convert_integer (st_parameter_dt *dtp, int length, int negative)
462 char c, *buffer, message[MSGLEN];
463 int m;
464 GFC_UINTEGER_LARGEST v, max, max10;
465 GFC_INTEGER_LARGEST value;
467 buffer = dtp->u.p.saved_string;
468 v = 0;
470 if (length == -1)
471 max = MAX_REPEAT;
472 else
474 max = si_max (length);
475 if (negative)
476 max++;
478 max10 = max / 10;
480 for (;;)
482 c = *buffer++;
483 if (c == '\0')
484 break;
485 c -= '0';
487 if (v > max10)
488 goto overflow;
489 v = 10 * v;
491 if (v > max - c)
492 goto overflow;
493 v += c;
496 m = 0;
498 if (length != -1)
500 if (negative)
501 value = -v;
502 else
503 value = v;
504 set_integer (dtp->u.p.value, value, length);
506 else
508 dtp->u.p.repeat_count = v;
510 if (dtp->u.p.repeat_count == 0)
512 snprintf (message, MSGLEN, "Zero repeat count in item %d of list input",
513 dtp->u.p.item_count);
515 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
516 m = 1;
520 free_saved (dtp);
521 return m;
523 overflow:
524 if (length == -1)
525 snprintf (message, MSGLEN, "Repeat count overflow in item %d of list input",
526 dtp->u.p.item_count);
527 else
528 snprintf (message, MSGLEN, "Integer overflow while reading item %d",
529 dtp->u.p.item_count);
531 free_saved (dtp);
532 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
534 return 1;
538 /* Parse a repeat count for logical and complex values which cannot
539 begin with a digit. Returns nonzero if we are done, zero if we
540 should continue on. */
542 static int
543 parse_repeat (st_parameter_dt *dtp)
545 char message[MSGLEN];
546 int c, repeat;
548 if ((c = next_char (dtp)) == EOF)
549 goto bad_repeat;
550 switch (c)
552 CASE_DIGITS:
553 repeat = c - '0';
554 break;
556 CASE_SEPARATORS:
557 unget_char (dtp, c);
558 eat_separator (dtp);
559 return 1;
561 default:
562 unget_char (dtp, c);
563 return 0;
566 for (;;)
568 c = next_char (dtp);
569 switch (c)
571 CASE_DIGITS:
572 repeat = 10 * repeat + c - '0';
574 if (repeat > MAX_REPEAT)
576 snprintf (message, MSGLEN,
577 "Repeat count overflow in item %d of list input",
578 dtp->u.p.item_count);
580 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
581 return 1;
584 break;
586 case '*':
587 if (repeat == 0)
589 snprintf (message, MSGLEN,
590 "Zero repeat count in item %d of list input",
591 dtp->u.p.item_count);
593 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
594 return 1;
597 goto done;
599 default:
600 goto bad_repeat;
604 done:
605 dtp->u.p.repeat_count = repeat;
606 return 0;
608 bad_repeat:
610 free_saved (dtp);
611 if (c == EOF)
613 hit_eof (dtp);
614 return 1;
616 else
617 eat_line (dtp);
618 snprintf (message, MSGLEN, "Bad repeat count in item %d of list input",
619 dtp->u.p.item_count);
620 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
621 return 1;
625 /* To read a logical we have to look ahead in the input stream to make sure
626 there is not an equal sign indicating a variable name. To do this we use
627 line_buffer to point to a temporary buffer, pushing characters there for
628 possible later reading. */
630 static void
631 l_push_char (st_parameter_dt *dtp, char c)
633 if (dtp->u.p.line_buffer == NULL)
634 dtp->u.p.line_buffer = xcalloc (SCRATCH_SIZE, 1);
636 dtp->u.p.line_buffer[dtp->u.p.item_count++] = c;
640 /* Read a logical character on the input. */
642 static void
643 read_logical (st_parameter_dt *dtp, int length)
645 char message[MSGLEN];
646 int c, i, v;
648 if (parse_repeat (dtp))
649 return;
651 c = tolower (next_char (dtp));
652 l_push_char (dtp, c);
653 switch (c)
655 case 't':
656 v = 1;
657 c = next_char (dtp);
658 l_push_char (dtp, c);
660 if (!is_separator(c) && c != EOF)
661 goto possible_name;
663 unget_char (dtp, c);
664 break;
665 case 'f':
666 v = 0;
667 c = next_char (dtp);
668 l_push_char (dtp, c);
670 if (!is_separator(c) && c != EOF)
671 goto possible_name;
673 unget_char (dtp, c);
674 break;
676 case '.':
677 c = tolower (next_char (dtp));
678 switch (c)
680 case 't':
681 v = 1;
682 break;
683 case 'f':
684 v = 0;
685 break;
686 default:
687 goto bad_logical;
690 break;
692 CASE_SEPARATORS:
693 unget_char (dtp, c);
694 eat_separator (dtp);
695 return; /* Null value. */
697 default:
698 /* Save the character in case it is the beginning
699 of the next object name. */
700 unget_char (dtp, c);
701 goto bad_logical;
704 dtp->u.p.saved_type = BT_LOGICAL;
705 dtp->u.p.saved_length = length;
707 /* Eat trailing garbage. */
709 c = next_char (dtp);
710 while (c != EOF && !is_separator (c));
712 unget_char (dtp, c);
713 eat_separator (dtp);
714 set_integer ((int *) dtp->u.p.value, v, length);
715 free_line (dtp);
717 return;
719 possible_name:
721 for(i = 0; i < 63; i++)
723 c = next_char (dtp);
724 if (is_separator(c))
726 /* All done if this is not a namelist read. */
727 if (!dtp->u.p.namelist_mode)
728 goto logical_done;
730 unget_char (dtp, c);
731 eat_separator (dtp);
732 c = next_char (dtp);
733 if (c != '=')
735 unget_char (dtp, c);
736 goto logical_done;
740 l_push_char (dtp, c);
741 if (c == '=')
743 dtp->u.p.nml_read_error = 1;
744 dtp->u.p.line_buffer_enabled = 1;
745 dtp->u.p.item_count = 0;
746 return;
751 bad_logical:
753 free_line (dtp);
755 if (nml_bad_return (dtp, c))
756 return;
758 free_saved (dtp);
759 if (c == EOF)
761 hit_eof (dtp);
762 return;
764 else if (c != '\n')
765 eat_line (dtp);
766 snprintf (message, MSGLEN, "Bad logical value while reading item %d",
767 dtp->u.p.item_count);
768 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
769 return;
771 logical_done:
773 dtp->u.p.saved_type = BT_LOGICAL;
774 dtp->u.p.saved_length = length;
775 set_integer ((int *) dtp->u.p.value, v, length);
776 free_saved (dtp);
777 free_line (dtp);
781 /* Reading integers is tricky because we can actually be reading a
782 repeat count. We have to store the characters in a buffer because
783 we could be reading an integer that is larger than the default int
784 used for repeat counts. */
786 static void
787 read_integer (st_parameter_dt *dtp, int length)
789 char message[MSGLEN];
790 int c, negative;
792 negative = 0;
794 c = next_char (dtp);
795 switch (c)
797 case '-':
798 negative = 1;
799 /* Fall through... */
801 case '+':
802 if ((c = next_char (dtp)) == EOF)
803 goto bad_integer;
804 goto get_integer;
806 CASE_SEPARATORS: /* Single null. */
807 unget_char (dtp, c);
808 eat_separator (dtp);
809 return;
811 CASE_DIGITS:
812 push_char (dtp, c);
813 break;
815 default:
816 goto bad_integer;
819 /* Take care of what may be a repeat count. */
821 for (;;)
823 c = next_char (dtp);
824 switch (c)
826 CASE_DIGITS:
827 push_char (dtp, c);
828 break;
830 case '*':
831 push_char (dtp, '\0');
832 goto repeat;
834 CASE_SEPARATORS: /* Not a repeat count. */
835 case EOF:
836 goto done;
838 default:
839 goto bad_integer;
843 repeat:
844 if (convert_integer (dtp, -1, 0))
845 return;
847 /* Get the real integer. */
849 if ((c = next_char (dtp)) == EOF)
850 goto bad_integer;
851 switch (c)
853 CASE_DIGITS:
854 break;
856 CASE_SEPARATORS:
857 unget_char (dtp, c);
858 eat_separator (dtp);
859 return;
861 case '-':
862 negative = 1;
863 /* Fall through... */
865 case '+':
866 c = next_char (dtp);
867 break;
870 get_integer:
871 if (!isdigit (c))
872 goto bad_integer;
873 push_char (dtp, c);
875 for (;;)
877 c = next_char (dtp);
878 switch (c)
880 CASE_DIGITS:
881 push_char (dtp, c);
882 break;
884 CASE_SEPARATORS:
885 case EOF:
886 goto done;
888 default:
889 goto bad_integer;
893 bad_integer:
895 if (nml_bad_return (dtp, c))
896 return;
898 free_saved (dtp);
899 if (c == EOF)
901 hit_eof (dtp);
902 return;
904 else if (c != '\n')
905 eat_line (dtp);
906 snprintf (message, MSGLEN, "Bad integer for item %d in list input",
907 dtp->u.p.item_count);
908 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
910 return;
912 done:
913 unget_char (dtp, c);
914 eat_separator (dtp);
916 push_char (dtp, '\0');
917 if (convert_integer (dtp, length, negative))
919 free_saved (dtp);
920 return;
923 free_saved (dtp);
924 dtp->u.p.saved_type = BT_INTEGER;
928 /* Read a character variable. */
930 static void
931 read_character (st_parameter_dt *dtp, int length __attribute__ ((unused)))
933 char quote, message[MSGLEN];
934 int c;
936 quote = ' '; /* Space means no quote character. */
938 if ((c = next_char (dtp)) == EOF)
939 goto eof;
940 switch (c)
942 CASE_DIGITS:
943 push_char (dtp, c);
944 break;
946 CASE_SEPARATORS:
947 unget_char (dtp, c); /* NULL value. */
948 eat_separator (dtp);
949 return;
951 case '"':
952 case '\'':
953 quote = c;
954 goto get_string;
956 default:
957 if (dtp->u.p.namelist_mode)
959 unget_char (dtp, c);
960 return;
963 push_char (dtp, c);
964 goto get_string;
967 /* Deal with a possible repeat count. */
969 for (;;)
971 if ((c = next_char (dtp)) == EOF)
972 goto eof;
973 switch (c)
975 CASE_DIGITS:
976 push_char (dtp, c);
977 break;
979 CASE_SEPARATORS:
980 unget_char (dtp, c);
981 goto done; /* String was only digits! */
983 case '*':
984 push_char (dtp, '\0');
985 goto got_repeat;
987 default:
988 push_char (dtp, c);
989 goto get_string; /* Not a repeat count after all. */
993 got_repeat:
994 if (convert_integer (dtp, -1, 0))
995 return;
997 /* Now get the real string. */
999 if ((c = next_char (dtp)) == EOF)
1000 goto eof;
1001 switch (c)
1003 CASE_SEPARATORS:
1004 unget_char (dtp, c); /* Repeated NULL values. */
1005 eat_separator (dtp);
1006 return;
1008 case '"':
1009 case '\'':
1010 quote = c;
1011 break;
1013 default:
1014 push_char (dtp, c);
1015 break;
1018 get_string:
1019 for (;;)
1021 if ((c = next_char (dtp)) == EOF)
1022 goto done_eof;
1023 switch (c)
1025 case '"':
1026 case '\'':
1027 if (c != quote)
1029 push_char (dtp, c);
1030 break;
1033 /* See if we have a doubled quote character or the end of
1034 the string. */
1036 if ((c = next_char (dtp)) == EOF)
1037 goto eof;
1038 if (c == quote)
1040 push_char (dtp, quote);
1041 break;
1044 unget_char (dtp, c);
1045 goto done;
1047 CASE_SEPARATORS:
1048 if (quote == ' ')
1050 unget_char (dtp, c);
1051 goto done;
1054 if (c != '\n' && c != '\r')
1055 push_char (dtp, c);
1056 break;
1058 default:
1059 push_char (dtp, c);
1060 break;
1064 /* At this point, we have to have a separator, or else the string is
1065 invalid. */
1066 done:
1067 c = next_char (dtp);
1068 done_eof:
1069 if (is_separator (c) || c == '!' || c == EOF)
1071 unget_char (dtp, c);
1072 eat_separator (dtp);
1073 dtp->u.p.saved_type = BT_CHARACTER;
1074 free_line (dtp);
1076 else
1078 free_saved (dtp);
1079 snprintf (message, MSGLEN, "Invalid string input in item %d",
1080 dtp->u.p.item_count);
1081 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1083 return;
1085 eof:
1086 free_saved (dtp);
1087 hit_eof (dtp);
1091 /* Parse a component of a complex constant or a real number that we
1092 are sure is already there. This is a straight real number parser. */
1094 static int
1095 parse_real (st_parameter_dt *dtp, void *buffer, int length)
1097 char message[MSGLEN];
1098 int c, m, seen_dp;
1100 if ((c = next_char (dtp)) == EOF)
1101 goto bad;
1103 if (c == '-' || c == '+')
1105 push_char (dtp, c);
1106 if ((c = next_char (dtp)) == EOF)
1107 goto bad;
1110 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1111 c = '.';
1113 if (!isdigit (c) && c != '.')
1115 if (c == 'i' || c == 'I' || c == 'n' || c == 'N')
1116 goto inf_nan;
1117 else
1118 goto bad;
1121 push_char (dtp, c);
1123 seen_dp = (c == '.') ? 1 : 0;
1125 for (;;)
1127 if ((c = next_char (dtp)) == EOF)
1128 goto bad;
1129 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1130 c = '.';
1131 switch (c)
1133 CASE_DIGITS:
1134 push_char (dtp, c);
1135 break;
1137 case '.':
1138 if (seen_dp)
1139 goto bad;
1141 seen_dp = 1;
1142 push_char (dtp, c);
1143 break;
1145 case 'e':
1146 case 'E':
1147 case 'd':
1148 case 'D':
1149 case 'q':
1150 case 'Q':
1151 push_char (dtp, 'e');
1152 goto exp1;
1154 case '-':
1155 case '+':
1156 push_char (dtp, 'e');
1157 push_char (dtp, c);
1158 if ((c = next_char (dtp)) == EOF)
1159 goto bad;
1160 goto exp2;
1162 CASE_SEPARATORS:
1163 goto done;
1165 default:
1166 goto done;
1170 exp1:
1171 if ((c = next_char (dtp)) == EOF)
1172 goto bad;
1173 if (c != '-' && c != '+')
1174 push_char (dtp, '+');
1175 else
1177 push_char (dtp, c);
1178 c = next_char (dtp);
1181 exp2:
1182 if (!isdigit (c))
1183 goto bad;
1185 push_char (dtp, c);
1187 for (;;)
1189 if ((c = next_char (dtp)) == EOF)
1190 goto bad;
1191 switch (c)
1193 CASE_DIGITS:
1194 push_char (dtp, c);
1195 break;
1197 CASE_SEPARATORS:
1198 unget_char (dtp, c);
1199 goto done;
1201 default:
1202 goto done;
1206 done:
1207 unget_char (dtp, c);
1208 push_char (dtp, '\0');
1210 m = convert_real (dtp, buffer, dtp->u.p.saved_string, length);
1211 free_saved (dtp);
1213 return m;
1215 done_infnan:
1216 unget_char (dtp, c);
1217 push_char (dtp, '\0');
1219 m = convert_infnan (dtp, buffer, dtp->u.p.saved_string, length);
1220 free_saved (dtp);
1222 return m;
1224 inf_nan:
1225 /* Match INF and Infinity. */
1226 if ((c == 'i' || c == 'I')
1227 && ((c = next_char (dtp)) == 'n' || c == 'N')
1228 && ((c = next_char (dtp)) == 'f' || c == 'F'))
1230 c = next_char (dtp);
1231 if ((c != 'i' && c != 'I')
1232 || ((c == 'i' || c == 'I')
1233 && ((c = next_char (dtp)) == 'n' || c == 'N')
1234 && ((c = next_char (dtp)) == 'i' || c == 'I')
1235 && ((c = next_char (dtp)) == 't' || c == 'T')
1236 && ((c = next_char (dtp)) == 'y' || c == 'Y')
1237 && (c = next_char (dtp))))
1239 if (is_separator (c))
1240 unget_char (dtp, c);
1241 push_char (dtp, 'i');
1242 push_char (dtp, 'n');
1243 push_char (dtp, 'f');
1244 goto done_infnan;
1246 } /* Match NaN. */
1247 else if (((c = next_char (dtp)) == 'a' || c == 'A')
1248 && ((c = next_char (dtp)) == 'n' || c == 'N')
1249 && (c = next_char (dtp)))
1251 if (is_separator (c))
1252 unget_char (dtp, c);
1253 push_char (dtp, 'n');
1254 push_char (dtp, 'a');
1255 push_char (dtp, 'n');
1257 /* Match "NAN(alphanum)". */
1258 if (c == '(')
1260 for ( ; c != ')'; c = next_char (dtp))
1261 if (is_separator (c))
1262 goto bad;
1264 c = next_char (dtp);
1265 if (is_separator (c))
1266 unget_char (dtp, c);
1268 goto done_infnan;
1271 bad:
1273 if (nml_bad_return (dtp, c))
1274 return 0;
1276 free_saved (dtp);
1277 if (c == EOF)
1279 hit_eof (dtp);
1280 return 1;
1282 else if (c != '\n')
1283 eat_line (dtp);
1284 snprintf (message, MSGLEN, "Bad floating point number for item %d",
1285 dtp->u.p.item_count);
1286 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1288 return 1;
1292 /* Reading a complex number is straightforward because we can tell
1293 what it is right away. */
1295 static void
1296 read_complex (st_parameter_dt *dtp, void * dest, int kind, size_t size)
1298 char message[MSGLEN];
1299 int c;
1301 if (parse_repeat (dtp))
1302 return;
1304 c = next_char (dtp);
1305 switch (c)
1307 case '(':
1308 break;
1310 CASE_SEPARATORS:
1311 unget_char (dtp, c);
1312 eat_separator (dtp);
1313 return;
1315 default:
1316 goto bad_complex;
1319 eol_1:
1320 eat_spaces (dtp);
1321 c = next_char (dtp);
1322 if (c == '\n' || c== '\r')
1323 goto eol_1;
1324 else
1325 unget_char (dtp, c);
1327 if (parse_real (dtp, dest, kind))
1328 return;
1330 eol_2:
1331 eat_spaces (dtp);
1332 c = next_char (dtp);
1333 if (c == '\n' || c== '\r')
1334 goto eol_2;
1335 else
1336 unget_char (dtp, c);
1338 if (next_char (dtp)
1339 != (dtp->u.p.current_unit->decimal_status == DECIMAL_POINT ? ',' : ';'))
1340 goto bad_complex;
1342 eol_3:
1343 eat_spaces (dtp);
1344 c = next_char (dtp);
1345 if (c == '\n' || c== '\r')
1346 goto eol_3;
1347 else
1348 unget_char (dtp, c);
1350 if (parse_real (dtp, dest + size / 2, kind))
1351 return;
1353 eol_4:
1354 eat_spaces (dtp);
1355 c = next_char (dtp);
1356 if (c == '\n' || c== '\r')
1357 goto eol_4;
1358 else
1359 unget_char (dtp, c);
1361 if (next_char (dtp) != ')')
1362 goto bad_complex;
1364 c = next_char (dtp);
1365 if (!is_separator (c))
1366 goto bad_complex;
1368 unget_char (dtp, c);
1369 eat_separator (dtp);
1371 free_saved (dtp);
1372 dtp->u.p.saved_type = BT_COMPLEX;
1373 return;
1375 bad_complex:
1377 if (nml_bad_return (dtp, c))
1378 return;
1380 free_saved (dtp);
1381 if (c == EOF)
1383 hit_eof (dtp);
1384 return;
1386 else if (c != '\n')
1387 eat_line (dtp);
1388 snprintf (message, MSGLEN, "Bad complex value in item %d of list input",
1389 dtp->u.p.item_count);
1390 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1394 /* Parse a real number with a possible repeat count. */
1396 static void
1397 read_real (st_parameter_dt *dtp, void * dest, int length)
1399 char message[MSGLEN];
1400 int c;
1401 int seen_dp;
1402 int is_inf;
1404 seen_dp = 0;
1406 c = next_char (dtp);
1407 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1408 c = '.';
1409 switch (c)
1411 CASE_DIGITS:
1412 push_char (dtp, c);
1413 break;
1415 case '.':
1416 push_char (dtp, c);
1417 seen_dp = 1;
1418 break;
1420 case '+':
1421 case '-':
1422 goto got_sign;
1424 CASE_SEPARATORS:
1425 unget_char (dtp, c); /* Single null. */
1426 eat_separator (dtp);
1427 return;
1429 case 'i':
1430 case 'I':
1431 case 'n':
1432 case 'N':
1433 goto inf_nan;
1435 default:
1436 goto bad_real;
1439 /* Get the digit string that might be a repeat count. */
1441 for (;;)
1443 c = next_char (dtp);
1444 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1445 c = '.';
1446 switch (c)
1448 CASE_DIGITS:
1449 push_char (dtp, c);
1450 break;
1452 case '.':
1453 if (seen_dp)
1454 goto bad_real;
1456 seen_dp = 1;
1457 push_char (dtp, c);
1458 goto real_loop;
1460 case 'E':
1461 case 'e':
1462 case 'D':
1463 case 'd':
1464 case 'Q':
1465 case 'q':
1466 goto exp1;
1468 case '+':
1469 case '-':
1470 push_char (dtp, 'e');
1471 push_char (dtp, c);
1472 c = next_char (dtp);
1473 goto exp2;
1475 case '*':
1476 push_char (dtp, '\0');
1477 goto got_repeat;
1479 CASE_SEPARATORS:
1480 if (c != '\n' && c != ',' && c != '\r' && c != ';')
1481 unget_char (dtp, c);
1482 goto done;
1484 default:
1485 goto bad_real;
1489 got_repeat:
1490 if (convert_integer (dtp, -1, 0))
1491 return;
1493 /* Now get the number itself. */
1495 if ((c = next_char (dtp)) == EOF)
1496 goto bad_real;
1497 if (is_separator (c))
1498 { /* Repeated null value. */
1499 unget_char (dtp, c);
1500 eat_separator (dtp);
1501 return;
1504 if (c != '-' && c != '+')
1505 push_char (dtp, '+');
1506 else
1508 got_sign:
1509 push_char (dtp, c);
1510 if ((c = next_char (dtp)) == EOF)
1511 goto bad_real;
1514 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1515 c = '.';
1517 if (!isdigit (c) && c != '.')
1519 if (c == 'i' || c == 'I' || c == 'n' || c == 'N')
1520 goto inf_nan;
1521 else
1522 goto bad_real;
1525 if (c == '.')
1527 if (seen_dp)
1528 goto bad_real;
1529 else
1530 seen_dp = 1;
1533 push_char (dtp, c);
1535 real_loop:
1536 for (;;)
1538 c = next_char (dtp);
1539 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1540 c = '.';
1541 switch (c)
1543 CASE_DIGITS:
1544 push_char (dtp, c);
1545 break;
1547 CASE_SEPARATORS:
1548 case EOF:
1549 goto done;
1551 case '.':
1552 if (seen_dp)
1553 goto bad_real;
1555 seen_dp = 1;
1556 push_char (dtp, c);
1557 break;
1559 case 'E':
1560 case 'e':
1561 case 'D':
1562 case 'd':
1563 case 'Q':
1564 case 'q':
1565 goto exp1;
1567 case '+':
1568 case '-':
1569 push_char (dtp, 'e');
1570 push_char (dtp, c);
1571 c = next_char (dtp);
1572 goto exp2;
1574 default:
1575 goto bad_real;
1579 exp1:
1580 push_char (dtp, 'e');
1582 if ((c = next_char (dtp)) == EOF)
1583 goto bad_real;
1584 if (c != '+' && c != '-')
1585 push_char (dtp, '+');
1586 else
1588 push_char (dtp, c);
1589 c = next_char (dtp);
1592 exp2:
1593 if (!isdigit (c))
1594 goto bad_real;
1595 push_char (dtp, c);
1597 for (;;)
1599 c = next_char (dtp);
1601 switch (c)
1603 CASE_DIGITS:
1604 push_char (dtp, c);
1605 break;
1607 CASE_SEPARATORS:
1608 goto done;
1610 default:
1611 goto bad_real;
1615 done:
1616 unget_char (dtp, c);
1617 eat_separator (dtp);
1618 push_char (dtp, '\0');
1619 if (convert_real (dtp, dest, dtp->u.p.saved_string, length))
1620 return;
1622 free_saved (dtp);
1623 dtp->u.p.saved_type = BT_REAL;
1624 return;
1626 inf_nan:
1627 l_push_char (dtp, c);
1628 is_inf = 0;
1630 /* Match INF and Infinity. */
1631 if (c == 'i' || c == 'I')
1633 c = next_char (dtp);
1634 l_push_char (dtp, c);
1635 if (c != 'n' && c != 'N')
1636 goto unwind;
1637 c = next_char (dtp);
1638 l_push_char (dtp, c);
1639 if (c != 'f' && c != 'F')
1640 goto unwind;
1641 c = next_char (dtp);
1642 l_push_char (dtp, c);
1643 if (!is_separator (c))
1645 if (c != 'i' && c != 'I')
1646 goto unwind;
1647 c = next_char (dtp);
1648 l_push_char (dtp, c);
1649 if (c != 'n' && c != 'N')
1650 goto unwind;
1651 c = next_char (dtp);
1652 l_push_char (dtp, c);
1653 if (c != 'i' && c != 'I')
1654 goto unwind;
1655 c = next_char (dtp);
1656 l_push_char (dtp, c);
1657 if (c != 't' && c != 'T')
1658 goto unwind;
1659 c = next_char (dtp);
1660 l_push_char (dtp, c);
1661 if (c != 'y' && c != 'Y')
1662 goto unwind;
1663 c = next_char (dtp);
1664 l_push_char (dtp, c);
1666 is_inf = 1;
1667 } /* Match NaN. */
1668 else
1670 c = next_char (dtp);
1671 l_push_char (dtp, c);
1672 if (c != 'a' && c != 'A')
1673 goto unwind;
1674 c = next_char (dtp);
1675 l_push_char (dtp, c);
1676 if (c != 'n' && c != 'N')
1677 goto unwind;
1678 c = next_char (dtp);
1679 l_push_char (dtp, c);
1681 /* Match NAN(alphanum). */
1682 if (c == '(')
1684 for (c = next_char (dtp); c != ')'; c = next_char (dtp))
1685 if (is_separator (c))
1686 goto unwind;
1687 else
1688 l_push_char (dtp, c);
1690 l_push_char (dtp, ')');
1691 c = next_char (dtp);
1692 l_push_char (dtp, c);
1696 if (!is_separator (c))
1697 goto unwind;
1699 if (dtp->u.p.namelist_mode)
1701 if (c == ' ' || c =='\n' || c == '\r')
1705 if ((c = next_char (dtp)) == EOF)
1706 goto bad_real;
1708 while (c == ' ' || c =='\n' || c == '\r');
1710 l_push_char (dtp, c);
1712 if (c == '=')
1713 goto unwind;
1717 if (is_inf)
1719 push_char (dtp, 'i');
1720 push_char (dtp, 'n');
1721 push_char (dtp, 'f');
1723 else
1725 push_char (dtp, 'n');
1726 push_char (dtp, 'a');
1727 push_char (dtp, 'n');
1730 free_line (dtp);
1731 unget_char (dtp, c);
1732 eat_separator (dtp);
1733 push_char (dtp, '\0');
1734 if (convert_infnan (dtp, dest, dtp->u.p.saved_string, length))
1735 return;
1737 free_saved (dtp);
1738 dtp->u.p.saved_type = BT_REAL;
1739 return;
1741 unwind:
1742 if (dtp->u.p.namelist_mode)
1744 dtp->u.p.nml_read_error = 1;
1745 dtp->u.p.line_buffer_enabled = 1;
1746 dtp->u.p.item_count = 0;
1747 return;
1750 bad_real:
1752 if (nml_bad_return (dtp, c))
1753 return;
1755 free_saved (dtp);
1756 if (c == EOF)
1758 hit_eof (dtp);
1759 return;
1761 else if (c != '\n')
1762 eat_line (dtp);
1764 snprintf (message, MSGLEN, "Bad real number in item %d of list input",
1765 dtp->u.p.item_count);
1766 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1770 /* Check the current type against the saved type to make sure they are
1771 compatible. Returns nonzero if incompatible. */
1773 static int
1774 check_type (st_parameter_dt *dtp, bt type, int len)
1776 char message[MSGLEN];
1778 if (dtp->u.p.saved_type != BT_UNKNOWN && dtp->u.p.saved_type != type)
1780 snprintf (message, MSGLEN, "Read type %s where %s was expected for item %d",
1781 type_name (dtp->u.p.saved_type), type_name (type),
1782 dtp->u.p.item_count);
1784 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1785 return 1;
1788 if (dtp->u.p.saved_type == BT_UNKNOWN || dtp->u.p.saved_type == BT_CHARACTER)
1789 return 0;
1791 if (dtp->u.p.saved_length != len)
1793 snprintf (message, MSGLEN,
1794 "Read kind %d %s where kind %d is required for item %d",
1795 dtp->u.p.saved_length, type_name (dtp->u.p.saved_type), len,
1796 dtp->u.p.item_count);
1797 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1798 return 1;
1801 return 0;
1805 /* Top level data transfer subroutine for list reads. Because we have
1806 to deal with repeat counts, the data item is always saved after
1807 reading, usually in the dtp->u.p.value[] array. If a repeat count is
1808 greater than one, we copy the data item multiple times. */
1810 static int
1811 list_formatted_read_scalar (st_parameter_dt *dtp, bt type, void *p,
1812 int kind, size_t size)
1814 gfc_char4_t *q;
1815 int c, i, m;
1816 int err = 0;
1818 dtp->u.p.namelist_mode = 0;
1820 if (dtp->u.p.first_item)
1822 dtp->u.p.first_item = 0;
1823 dtp->u.p.input_complete = 0;
1824 dtp->u.p.repeat_count = 1;
1825 dtp->u.p.at_eol = 0;
1827 if ((c = eat_spaces (dtp)) == EOF)
1829 err = LIBERROR_END;
1830 goto cleanup;
1832 if (is_separator (c))
1834 /* Found a null value. */
1835 eat_separator (dtp);
1836 dtp->u.p.repeat_count = 0;
1838 /* eat_separator sets this flag if the separator was a comma. */
1839 if (dtp->u.p.comma_flag)
1840 goto cleanup;
1842 /* eat_separator sets this flag if the separator was a \n or \r. */
1843 if (dtp->u.p.at_eol)
1844 finish_separator (dtp);
1845 else
1846 goto cleanup;
1850 else
1852 if (dtp->u.p.repeat_count > 0)
1854 if (check_type (dtp, type, kind))
1855 return err;
1856 goto set_value;
1859 if (dtp->u.p.input_complete)
1860 goto cleanup;
1862 if (dtp->u.p.at_eol)
1863 finish_separator (dtp);
1864 else
1866 eat_spaces (dtp);
1867 /* Trailing spaces prior to end of line. */
1868 if (dtp->u.p.at_eol)
1869 finish_separator (dtp);
1872 dtp->u.p.saved_type = BT_UNKNOWN;
1873 dtp->u.p.repeat_count = 1;
1876 switch (type)
1878 case BT_INTEGER:
1879 read_integer (dtp, kind);
1880 break;
1881 case BT_LOGICAL:
1882 read_logical (dtp, kind);
1883 break;
1884 case BT_CHARACTER:
1885 read_character (dtp, kind);
1886 break;
1887 case BT_REAL:
1888 read_real (dtp, p, kind);
1889 /* Copy value back to temporary if needed. */
1890 if (dtp->u.p.repeat_count > 0)
1891 memcpy (dtp->u.p.value, p, size);
1892 break;
1893 case BT_COMPLEX:
1894 read_complex (dtp, p, kind, size);
1895 /* Copy value back to temporary if needed. */
1896 if (dtp->u.p.repeat_count > 0)
1897 memcpy (dtp->u.p.value, p, size);
1898 break;
1899 default:
1900 internal_error (&dtp->common, "Bad type for list read");
1903 if (dtp->u.p.saved_type != BT_CHARACTER && dtp->u.p.saved_type != BT_UNKNOWN)
1904 dtp->u.p.saved_length = size;
1906 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1907 goto cleanup;
1909 set_value:
1910 switch (dtp->u.p.saved_type)
1912 case BT_COMPLEX:
1913 case BT_REAL:
1914 if (dtp->u.p.repeat_count > 0)
1915 memcpy (p, dtp->u.p.value, size);
1916 break;
1918 case BT_INTEGER:
1919 case BT_LOGICAL:
1920 memcpy (p, dtp->u.p.value, size);
1921 break;
1923 case BT_CHARACTER:
1924 if (dtp->u.p.saved_string)
1926 m = ((int) size < dtp->u.p.saved_used)
1927 ? (int) size : dtp->u.p.saved_used;
1928 if (kind == 1)
1929 memcpy (p, dtp->u.p.saved_string, m);
1930 else
1932 q = (gfc_char4_t *) p;
1933 for (i = 0; i < m; i++)
1934 q[i] = (unsigned char) dtp->u.p.saved_string[i];
1937 else
1938 /* Just delimiters encountered, nothing to copy but SPACE. */
1939 m = 0;
1941 if (m < (int) size)
1943 if (kind == 1)
1944 memset (((char *) p) + m, ' ', size - m);
1945 else
1947 q = (gfc_char4_t *) p;
1948 for (i = m; i < (int) size; i++)
1949 q[i] = (unsigned char) ' ';
1952 break;
1954 case BT_UNKNOWN:
1955 break;
1957 default:
1958 internal_error (&dtp->common, "Bad type for list read");
1961 if (--dtp->u.p.repeat_count <= 0)
1962 free_saved (dtp);
1964 cleanup:
1965 if (err == LIBERROR_END)
1966 hit_eof (dtp);
1967 return err;
1971 void
1972 list_formatted_read (st_parameter_dt *dtp, bt type, void *p, int kind,
1973 size_t size, size_t nelems)
1975 size_t elem;
1976 char *tmp;
1977 size_t stride = type == BT_CHARACTER ?
1978 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
1979 int err;
1981 tmp = (char *) p;
1983 /* Big loop over all the elements. */
1984 for (elem = 0; elem < nelems; elem++)
1986 dtp->u.p.item_count++;
1987 err = list_formatted_read_scalar (dtp, type, tmp + stride*elem,
1988 kind, size);
1989 if (err)
1990 break;
1995 /* Finish a list read. */
1997 void
1998 finish_list_read (st_parameter_dt *dtp)
2000 int err;
2002 free_saved (dtp);
2004 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
2006 if (dtp->u.p.at_eol)
2008 dtp->u.p.at_eol = 0;
2009 return;
2012 err = eat_line (dtp);
2013 if (err == LIBERROR_END)
2014 hit_eof (dtp);
2017 /* NAMELIST INPUT
2019 void namelist_read (st_parameter_dt *dtp)
2020 calls:
2021 static void nml_match_name (char *name, int len)
2022 static int nml_query (st_parameter_dt *dtp)
2023 static int nml_get_obj_data (st_parameter_dt *dtp,
2024 namelist_info **prev_nl, char *, size_t)
2025 calls:
2026 static void nml_untouch_nodes (st_parameter_dt *dtp)
2027 static namelist_info * find_nml_node (st_parameter_dt *dtp,
2028 char * var_name)
2029 static int nml_parse_qualifier(descriptor_dimension * ad,
2030 array_loop_spec * ls, int rank, char *)
2031 static void nml_touch_nodes (namelist_info * nl)
2032 static int nml_read_obj (namelist_info *nl, index_type offset,
2033 namelist_info **prev_nl, char *, size_t,
2034 index_type clow, index_type chigh)
2035 calls:
2036 -itself- */
2038 /* Inputs a rank-dimensional qualifier, which can contain
2039 singlets, doublets, triplets or ':' with the standard meanings. */
2041 static try
2042 nml_parse_qualifier (st_parameter_dt *dtp, descriptor_dimension *ad,
2043 array_loop_spec *ls, int rank, char *parse_err_msg,
2044 size_t parse_err_msg_size,
2045 int *parsed_rank)
2047 int dim;
2048 int indx;
2049 int neg;
2050 int null_flag;
2051 int is_array_section, is_char;
2052 int c;
2054 is_char = 0;
2055 is_array_section = 0;
2056 dtp->u.p.expanded_read = 0;
2058 /* See if this is a character substring qualifier we are looking for. */
2059 if (rank == -1)
2061 rank = 1;
2062 is_char = 1;
2065 /* The next character in the stream should be the '('. */
2067 if ((c = next_char (dtp)) == EOF)
2068 return FAILURE;
2070 /* Process the qualifier, by dimension and triplet. */
2072 for (dim=0; dim < rank; dim++ )
2074 for (indx=0; indx<3; indx++)
2076 free_saved (dtp);
2077 eat_spaces (dtp);
2078 neg = 0;
2080 /* Process a potential sign. */
2081 if ((c = next_char (dtp)) == EOF)
2082 return FAILURE;
2083 switch (c)
2085 case '-':
2086 neg = 1;
2087 break;
2089 case '+':
2090 break;
2092 default:
2093 unget_char (dtp, c);
2094 break;
2097 /* Process characters up to the next ':' , ',' or ')'. */
2098 for (;;)
2100 if ((c = next_char (dtp)) == EOF)
2101 return FAILURE;
2103 switch (c)
2105 case ':':
2106 is_array_section = 1;
2107 break;
2109 case ',': case ')':
2110 if ((c==',' && dim == rank -1)
2111 || (c==')' && dim < rank -1))
2113 if (is_char)
2114 snprintf (parse_err_msg, parse_err_msg_size,
2115 "Bad substring qualifier");
2116 else
2117 snprintf (parse_err_msg, parse_err_msg_size,
2118 "Bad number of index fields");
2119 goto err_ret;
2121 break;
2123 CASE_DIGITS:
2124 push_char (dtp, c);
2125 continue;
2127 case ' ': case '\t':
2128 eat_spaces (dtp);
2129 if ((c = next_char (dtp) == EOF))
2130 return FAILURE;
2131 break;
2133 default:
2134 if (is_char)
2135 snprintf (parse_err_msg, parse_err_msg_size,
2136 "Bad character in substring qualifier");
2137 else
2138 snprintf (parse_err_msg, parse_err_msg_size,
2139 "Bad character in index");
2140 goto err_ret;
2143 if ((c == ',' || c == ')') && indx == 0
2144 && dtp->u.p.saved_string == 0)
2146 if (is_char)
2147 snprintf (parse_err_msg, parse_err_msg_size,
2148 "Null substring qualifier");
2149 else
2150 snprintf (parse_err_msg, parse_err_msg_size,
2151 "Null index field");
2152 goto err_ret;
2155 if ((c == ':' && indx == 1 && dtp->u.p.saved_string == 0)
2156 || (indx == 2 && dtp->u.p.saved_string == 0))
2158 if (is_char)
2159 snprintf (parse_err_msg, parse_err_msg_size,
2160 "Bad substring qualifier");
2161 else
2162 snprintf (parse_err_msg, parse_err_msg_size,
2163 "Bad index triplet");
2164 goto err_ret;
2167 if (is_char && !is_array_section)
2169 snprintf (parse_err_msg, parse_err_msg_size,
2170 "Missing colon in substring qualifier");
2171 goto err_ret;
2174 /* If '( : ? )' or '( ? : )' break and flag read failure. */
2175 null_flag = 0;
2176 if ((c == ':' && indx == 0 && dtp->u.p.saved_string == 0)
2177 || (indx==1 && dtp->u.p.saved_string == 0))
2179 null_flag = 1;
2180 break;
2183 /* Now read the index. */
2184 if (convert_integer (dtp, sizeof(index_type), neg))
2186 if (is_char)
2187 snprintf (parse_err_msg, parse_err_msg_size,
2188 "Bad integer substring qualifier");
2189 else
2190 snprintf (parse_err_msg, parse_err_msg_size,
2191 "Bad integer in index");
2192 goto err_ret;
2194 break;
2197 /* Feed the index values to the triplet arrays. */
2198 if (!null_flag)
2200 if (indx == 0)
2201 memcpy (&ls[dim].start, dtp->u.p.value, sizeof(index_type));
2202 if (indx == 1)
2203 memcpy (&ls[dim].end, dtp->u.p.value, sizeof(index_type));
2204 if (indx == 2)
2205 memcpy (&ls[dim].step, dtp->u.p.value, sizeof(index_type));
2208 /* Singlet or doublet indices. */
2209 if (c==',' || c==')')
2211 if (indx == 0)
2213 memcpy (&ls[dim].start, dtp->u.p.value, sizeof(index_type));
2215 /* If -std=f95/2003 or an array section is specified,
2216 do not allow excess data to be processed. */
2217 if (is_array_section == 1
2218 || !(compile_options.allow_std & GFC_STD_GNU)
2219 || dtp->u.p.ionml->type == BT_DERIVED)
2220 ls[dim].end = ls[dim].start;
2221 else
2222 dtp->u.p.expanded_read = 1;
2225 /* Check for non-zero rank. */
2226 if (is_array_section == 1 && ls[dim].start != ls[dim].end)
2227 *parsed_rank = 1;
2229 break;
2233 if (is_array_section == 1 && dtp->u.p.expanded_read == 1)
2235 int i;
2236 dtp->u.p.expanded_read = 0;
2237 for (i = 0; i < dim; i++)
2238 ls[i].end = ls[i].start;
2241 /* Check the values of the triplet indices. */
2242 if ((ls[dim].start > GFC_DIMENSION_UBOUND(ad[dim]))
2243 || (ls[dim].start < GFC_DIMENSION_LBOUND(ad[dim]))
2244 || (ls[dim].end > GFC_DIMENSION_UBOUND(ad[dim]))
2245 || (ls[dim].end < GFC_DIMENSION_LBOUND(ad[dim])))
2247 if (is_char)
2248 snprintf (parse_err_msg, parse_err_msg_size,
2249 "Substring out of range");
2250 else
2251 snprintf (parse_err_msg, parse_err_msg_size,
2252 "Index %d out of range", dim + 1);
2253 goto err_ret;
2256 if (((ls[dim].end - ls[dim].start ) * ls[dim].step < 0)
2257 || (ls[dim].step == 0))
2259 snprintf (parse_err_msg, parse_err_msg_size,
2260 "Bad range in index %d", dim + 1);
2261 goto err_ret;
2264 /* Initialise the loop index counter. */
2265 ls[dim].idx = ls[dim].start;
2267 eat_spaces (dtp);
2268 return SUCCESS;
2270 err_ret:
2272 return FAILURE;
2275 static namelist_info *
2276 find_nml_node (st_parameter_dt *dtp, char * var_name)
2278 namelist_info * t = dtp->u.p.ionml;
2279 while (t != NULL)
2281 if (strcmp (var_name, t->var_name) == 0)
2283 t->touched = 1;
2284 return t;
2286 t = t->next;
2288 return NULL;
2291 /* Visits all the components of a derived type that have
2292 not explicitly been identified in the namelist input.
2293 touched is set and the loop specification initialised
2294 to default values */
2296 static void
2297 nml_touch_nodes (namelist_info * nl)
2299 index_type len = strlen (nl->var_name) + 1;
2300 int dim;
2301 char * ext_name = (char*)xmalloc (len + 1);
2302 memcpy (ext_name, nl->var_name, len-1);
2303 memcpy (ext_name + len - 1, "%", 2);
2304 for (nl = nl->next; nl; nl = nl->next)
2306 if (strncmp (nl->var_name, ext_name, len) == 0)
2308 nl->touched = 1;
2309 for (dim=0; dim < nl->var_rank; dim++)
2311 nl->ls[dim].step = 1;
2312 nl->ls[dim].end = GFC_DESCRIPTOR_UBOUND(nl,dim);
2313 nl->ls[dim].start = GFC_DESCRIPTOR_LBOUND(nl,dim);
2314 nl->ls[dim].idx = nl->ls[dim].start;
2317 else
2318 break;
2320 free (ext_name);
2321 return;
2324 /* Resets touched for the entire list of nml_nodes, ready for a
2325 new object. */
2327 static void
2328 nml_untouch_nodes (st_parameter_dt *dtp)
2330 namelist_info * t;
2331 for (t = dtp->u.p.ionml; t; t = t->next)
2332 t->touched = 0;
2333 return;
2336 /* Attempts to input name to namelist name. Returns
2337 dtp->u.p.nml_read_error = 1 on no match. */
2339 static void
2340 nml_match_name (st_parameter_dt *dtp, const char *name, index_type len)
2342 index_type i;
2343 int c;
2345 dtp->u.p.nml_read_error = 0;
2346 for (i = 0; i < len; i++)
2348 c = next_char (dtp);
2349 if (c == EOF || (tolower (c) != tolower (name[i])))
2351 dtp->u.p.nml_read_error = 1;
2352 break;
2357 /* If the namelist read is from stdin, output the current state of the
2358 namelist to stdout. This is used to implement the non-standard query
2359 features, ? and =?. If c == '=' the full namelist is printed. Otherwise
2360 the names alone are printed. */
2362 static void
2363 nml_query (st_parameter_dt *dtp, char c)
2365 gfc_unit * temp_unit;
2366 namelist_info * nl;
2367 index_type len;
2368 char * p;
2369 #ifdef HAVE_CRLF
2370 static const index_type endlen = 3;
2371 static const char endl[] = "\r\n";
2372 static const char nmlend[] = "&end\r\n";
2373 #else
2374 static const index_type endlen = 2;
2375 static const char endl[] = "\n";
2376 static const char nmlend[] = "&end\n";
2377 #endif
2379 if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
2380 return;
2382 /* Store the current unit and transfer to stdout. */
2384 temp_unit = dtp->u.p.current_unit;
2385 dtp->u.p.current_unit = find_unit (options.stdout_unit);
2387 if (dtp->u.p.current_unit)
2389 dtp->u.p.mode = WRITING;
2390 next_record (dtp, 0);
2392 /* Write the namelist in its entirety. */
2394 if (c == '=')
2395 namelist_write (dtp);
2397 /* Or write the list of names. */
2399 else
2401 /* "&namelist_name\n" */
2403 len = dtp->namelist_name_len;
2404 p = write_block (dtp, len + endlen);
2405 if (!p)
2406 goto query_return;
2407 memcpy (p, "&", 1);
2408 memcpy ((char*)(p + 1), dtp->namelist_name, len);
2409 memcpy ((char*)(p + len + 1), &endl, endlen - 1);
2410 for (nl = dtp->u.p.ionml; nl; nl = nl->next)
2412 /* " var_name\n" */
2414 len = strlen (nl->var_name);
2415 p = write_block (dtp, len + endlen);
2416 if (!p)
2417 goto query_return;
2418 memcpy (p, " ", 1);
2419 memcpy ((char*)(p + 1), nl->var_name, len);
2420 memcpy ((char*)(p + len + 1), &endl, endlen - 1);
2423 /* "&end\n" */
2425 p = write_block (dtp, endlen + 3);
2426 goto query_return;
2427 memcpy (p, &nmlend, endlen + 3);
2430 /* Flush the stream to force immediate output. */
2432 fbuf_flush (dtp->u.p.current_unit, WRITING);
2433 sflush (dtp->u.p.current_unit->s);
2434 unlock_unit (dtp->u.p.current_unit);
2437 query_return:
2439 /* Restore the current unit. */
2441 dtp->u.p.current_unit = temp_unit;
2442 dtp->u.p.mode = READING;
2443 return;
2446 /* Reads and stores the input for the namelist object nl. For an array,
2447 the function loops over the ranges defined by the loop specification.
2448 This default to all the data or to the specification from a qualifier.
2449 nml_read_obj recursively calls itself to read derived types. It visits
2450 all its own components but only reads data for those that were touched
2451 when the name was parsed. If a read error is encountered, an attempt is
2452 made to return to read a new object name because the standard allows too
2453 little data to be available. On the other hand, too much data is an
2454 error. */
2456 static try
2457 nml_read_obj (st_parameter_dt *dtp, namelist_info * nl, index_type offset,
2458 namelist_info **pprev_nl, char *nml_err_msg,
2459 size_t nml_err_msg_size, index_type clow, index_type chigh)
2461 namelist_info * cmp;
2462 char * obj_name;
2463 int nml_carry;
2464 int len;
2465 int dim;
2466 index_type dlen;
2467 index_type m;
2468 size_t obj_name_len;
2469 void * pdata;
2471 /* This object not touched in name parsing. */
2473 if (!nl->touched)
2474 return SUCCESS;
2476 dtp->u.p.repeat_count = 0;
2477 eat_spaces (dtp);
2479 len = nl->len;
2480 switch (nl->type)
2482 case BT_INTEGER:
2483 case BT_LOGICAL:
2484 dlen = len;
2485 break;
2487 case BT_REAL:
2488 dlen = size_from_real_kind (len);
2489 break;
2491 case BT_COMPLEX:
2492 dlen = size_from_complex_kind (len);
2493 break;
2495 case BT_CHARACTER:
2496 dlen = chigh ? (chigh - clow + 1) : nl->string_length;
2497 break;
2499 default:
2500 dlen = 0;
2505 /* Update the pointer to the data, using the current index vector */
2507 pdata = (void*)(nl->mem_pos + offset);
2508 for (dim = 0; dim < nl->var_rank; dim++)
2509 pdata = (void*)(pdata + (nl->ls[dim].idx
2510 - GFC_DESCRIPTOR_LBOUND(nl,dim))
2511 * GFC_DESCRIPTOR_STRIDE(nl,dim) * nl->size);
2513 /* Reset the error flag and try to read next value, if
2514 dtp->u.p.repeat_count=0 */
2516 dtp->u.p.nml_read_error = 0;
2517 nml_carry = 0;
2518 if (--dtp->u.p.repeat_count <= 0)
2520 if (dtp->u.p.input_complete)
2521 return SUCCESS;
2522 if (dtp->u.p.at_eol)
2523 finish_separator (dtp);
2524 if (dtp->u.p.input_complete)
2525 return SUCCESS;
2527 dtp->u.p.saved_type = BT_UNKNOWN;
2528 free_saved (dtp);
2530 switch (nl->type)
2532 case BT_INTEGER:
2533 read_integer (dtp, len);
2534 break;
2536 case BT_LOGICAL:
2537 read_logical (dtp, len);
2538 break;
2540 case BT_CHARACTER:
2541 read_character (dtp, len);
2542 break;
2544 case BT_REAL:
2545 /* Need to copy data back from the real location to the temp in order
2546 to handle nml reads into arrays. */
2547 read_real (dtp, pdata, len);
2548 memcpy (dtp->u.p.value, pdata, dlen);
2549 break;
2551 case BT_COMPLEX:
2552 /* Same as for REAL, copy back to temp. */
2553 read_complex (dtp, pdata, len, dlen);
2554 memcpy (dtp->u.p.value, pdata, dlen);
2555 break;
2557 case BT_DERIVED:
2558 obj_name_len = strlen (nl->var_name) + 1;
2559 obj_name = xmalloc (obj_name_len+1);
2560 memcpy (obj_name, nl->var_name, obj_name_len-1);
2561 memcpy (obj_name + obj_name_len - 1, "%", 2);
2563 /* If reading a derived type, disable the expanded read warning
2564 since a single object can have multiple reads. */
2565 dtp->u.p.expanded_read = 0;
2567 /* Now loop over the components. Update the component pointer
2568 with the return value from nml_write_obj. This loop jumps
2569 past nested derived types by testing if the potential
2570 component name contains '%'. */
2572 for (cmp = nl->next;
2573 cmp &&
2574 !strncmp (cmp->var_name, obj_name, obj_name_len) &&
2575 !strchr (cmp->var_name + obj_name_len, '%');
2576 cmp = cmp->next)
2579 if (nml_read_obj (dtp, cmp, (index_type)(pdata - nl->mem_pos),
2580 pprev_nl, nml_err_msg, nml_err_msg_size,
2581 clow, chigh) == FAILURE)
2583 free (obj_name);
2584 return FAILURE;
2587 if (dtp->u.p.input_complete)
2589 free (obj_name);
2590 return SUCCESS;
2594 free (obj_name);
2595 goto incr_idx;
2597 default:
2598 snprintf (nml_err_msg, nml_err_msg_size,
2599 "Bad type for namelist object %s", nl->var_name);
2600 internal_error (&dtp->common, nml_err_msg);
2601 goto nml_err_ret;
2605 /* The standard permits array data to stop short of the number of
2606 elements specified in the loop specification. In this case, we
2607 should be here with dtp->u.p.nml_read_error != 0. Control returns to
2608 nml_get_obj_data and an attempt is made to read object name. */
2610 *pprev_nl = nl;
2611 if (dtp->u.p.nml_read_error)
2613 dtp->u.p.expanded_read = 0;
2614 return SUCCESS;
2617 if (dtp->u.p.saved_type == BT_UNKNOWN)
2619 dtp->u.p.expanded_read = 0;
2620 goto incr_idx;
2623 switch (dtp->u.p.saved_type)
2626 case BT_COMPLEX:
2627 case BT_REAL:
2628 case BT_INTEGER:
2629 case BT_LOGICAL:
2630 memcpy (pdata, dtp->u.p.value, dlen);
2631 break;
2633 case BT_CHARACTER:
2634 if (dlen < dtp->u.p.saved_used)
2636 if (compile_options.bounds_check)
2638 snprintf (nml_err_msg, nml_err_msg_size,
2639 "Namelist object '%s' truncated on read.",
2640 nl->var_name);
2641 generate_warning (&dtp->common, nml_err_msg);
2643 m = dlen;
2645 else
2646 m = dtp->u.p.saved_used;
2647 pdata = (void*)( pdata + clow - 1 );
2648 memcpy (pdata, dtp->u.p.saved_string, m);
2649 if (m < dlen)
2650 memset ((void*)( pdata + m ), ' ', dlen - m);
2651 break;
2653 default:
2654 break;
2657 /* Warn if a non-standard expanded read occurs. A single read of a
2658 single object is acceptable. If a second read occurs, issue a warning
2659 and set the flag to zero to prevent further warnings. */
2660 if (dtp->u.p.expanded_read == 2)
2662 notify_std (&dtp->common, GFC_STD_GNU, "Non-standard expanded namelist read.");
2663 dtp->u.p.expanded_read = 0;
2666 /* If the expanded read warning flag is set, increment it,
2667 indicating that a single read has occurred. */
2668 if (dtp->u.p.expanded_read >= 1)
2669 dtp->u.p.expanded_read++;
2671 /* Break out of loop if scalar. */
2672 if (!nl->var_rank)
2673 break;
2675 /* Now increment the index vector. */
2677 incr_idx:
2679 nml_carry = 1;
2680 for (dim = 0; dim < nl->var_rank; dim++)
2682 nl->ls[dim].idx += nml_carry * nl->ls[dim].step;
2683 nml_carry = 0;
2684 if (((nl->ls[dim].step > 0) && (nl->ls[dim].idx > nl->ls[dim].end))
2686 ((nl->ls[dim].step < 0) && (nl->ls[dim].idx < nl->ls[dim].end)))
2688 nl->ls[dim].idx = nl->ls[dim].start;
2689 nml_carry = 1;
2692 } while (!nml_carry);
2694 if (dtp->u.p.repeat_count > 1)
2696 snprintf (nml_err_msg, nml_err_msg_size,
2697 "Repeat count too large for namelist object %s", nl->var_name);
2698 goto nml_err_ret;
2700 return SUCCESS;
2702 nml_err_ret:
2704 return FAILURE;
2707 /* Parses the object name, including array and substring qualifiers. It
2708 iterates over derived type components, touching those components and
2709 setting their loop specifications, if there is a qualifier. If the
2710 object is itself a derived type, its components and subcomponents are
2711 touched. nml_read_obj is called at the end and this reads the data in
2712 the manner specified by the object name. */
2714 static try
2715 nml_get_obj_data (st_parameter_dt *dtp, namelist_info **pprev_nl,
2716 char *nml_err_msg, size_t nml_err_msg_size)
2718 int c;
2719 namelist_info * nl;
2720 namelist_info * first_nl = NULL;
2721 namelist_info * root_nl = NULL;
2722 int dim, parsed_rank;
2723 int component_flag, qualifier_flag;
2724 index_type clow, chigh;
2725 int non_zero_rank_count;
2727 /* Look for end of input or object name. If '?' or '=?' are encountered
2728 in stdin, print the node names or the namelist to stdout. */
2730 eat_separator (dtp);
2731 if (dtp->u.p.input_complete)
2732 return SUCCESS;
2734 if (dtp->u.p.at_eol)
2735 finish_separator (dtp);
2736 if (dtp->u.p.input_complete)
2737 return SUCCESS;
2739 if ((c = next_char (dtp)) == EOF)
2740 return FAILURE;
2741 switch (c)
2743 case '=':
2744 if ((c = next_char (dtp)) == EOF)
2745 return FAILURE;
2746 if (c != '?')
2748 snprintf (nml_err_msg, nml_err_msg_size,
2749 "namelist read: misplaced = sign");
2750 goto nml_err_ret;
2752 nml_query (dtp, '=');
2753 return SUCCESS;
2755 case '?':
2756 nml_query (dtp, '?');
2757 return SUCCESS;
2759 case '$':
2760 case '&':
2761 nml_match_name (dtp, "end", 3);
2762 if (dtp->u.p.nml_read_error)
2764 snprintf (nml_err_msg, nml_err_msg_size,
2765 "namelist not terminated with / or &end");
2766 goto nml_err_ret;
2768 case '/':
2769 dtp->u.p.input_complete = 1;
2770 return SUCCESS;
2772 default :
2773 break;
2776 /* Untouch all nodes of the namelist and reset the flags that are set for
2777 derived type components. */
2779 nml_untouch_nodes (dtp);
2780 component_flag = 0;
2781 qualifier_flag = 0;
2782 non_zero_rank_count = 0;
2784 /* Get the object name - should '!' and '\n' be permitted separators? */
2786 get_name:
2788 free_saved (dtp);
2792 if (!is_separator (c))
2793 push_char (dtp, tolower(c));
2794 if ((c = next_char (dtp)) == EOF)
2795 return FAILURE;
2796 } while (!( c=='=' || c==' ' || c=='\t' || c =='(' || c =='%' ));
2798 unget_char (dtp, c);
2800 /* Check that the name is in the namelist and get pointer to object.
2801 Three error conditions exist: (i) An attempt is being made to
2802 identify a non-existent object, following a failed data read or
2803 (ii) The object name does not exist or (iii) Too many data items
2804 are present for an object. (iii) gives the same error message
2805 as (i) */
2807 push_char (dtp, '\0');
2809 if (component_flag)
2811 size_t var_len = strlen (root_nl->var_name);
2812 size_t saved_len
2813 = dtp->u.p.saved_string ? strlen (dtp->u.p.saved_string) : 0;
2814 char ext_name[var_len + saved_len + 1];
2816 memcpy (ext_name, root_nl->var_name, var_len);
2817 if (dtp->u.p.saved_string)
2818 memcpy (ext_name + var_len, dtp->u.p.saved_string, saved_len);
2819 ext_name[var_len + saved_len] = '\0';
2820 nl = find_nml_node (dtp, ext_name);
2822 else
2823 nl = find_nml_node (dtp, dtp->u.p.saved_string);
2825 if (nl == NULL)
2827 if (dtp->u.p.nml_read_error && *pprev_nl)
2828 snprintf (nml_err_msg, nml_err_msg_size,
2829 "Bad data for namelist object %s", (*pprev_nl)->var_name);
2831 else
2832 snprintf (nml_err_msg, nml_err_msg_size,
2833 "Cannot match namelist object name %s",
2834 dtp->u.p.saved_string);
2836 goto nml_err_ret;
2839 /* Get the length, data length, base pointer and rank of the variable.
2840 Set the default loop specification first. */
2842 for (dim=0; dim < nl->var_rank; dim++)
2844 nl->ls[dim].step = 1;
2845 nl->ls[dim].end = GFC_DESCRIPTOR_UBOUND(nl,dim);
2846 nl->ls[dim].start = GFC_DESCRIPTOR_LBOUND(nl,dim);
2847 nl->ls[dim].idx = nl->ls[dim].start;
2850 /* Check to see if there is a qualifier: if so, parse it.*/
2852 if (c == '(' && nl->var_rank)
2854 parsed_rank = 0;
2855 if (nml_parse_qualifier (dtp, nl->dim, nl->ls, nl->var_rank,
2856 nml_err_msg, nml_err_msg_size,
2857 &parsed_rank) == FAILURE)
2859 char *nml_err_msg_end = strchr (nml_err_msg, '\0');
2860 snprintf (nml_err_msg_end,
2861 nml_err_msg_size - (nml_err_msg_end - nml_err_msg),
2862 " for namelist variable %s", nl->var_name);
2863 goto nml_err_ret;
2865 if (parsed_rank > 0)
2866 non_zero_rank_count++;
2868 qualifier_flag = 1;
2870 if ((c = next_char (dtp)) == EOF)
2871 return FAILURE;
2872 unget_char (dtp, c);
2874 else if (nl->var_rank > 0)
2875 non_zero_rank_count++;
2877 /* Now parse a derived type component. The root namelist_info address
2878 is backed up, as is the previous component level. The component flag
2879 is set and the iteration is made by jumping back to get_name. */
2881 if (c == '%')
2883 if (nl->type != BT_DERIVED)
2885 snprintf (nml_err_msg, nml_err_msg_size,
2886 "Attempt to get derived component for %s", nl->var_name);
2887 goto nml_err_ret;
2890 if (*pprev_nl == NULL || !component_flag)
2891 first_nl = nl;
2893 root_nl = nl;
2895 component_flag = 1;
2896 if ((c = next_char (dtp)) == EOF)
2897 return FAILURE;
2898 goto get_name;
2901 /* Parse a character qualifier, if present. chigh = 0 is a default
2902 that signals that the string length = string_length. */
2904 clow = 1;
2905 chigh = 0;
2907 if (c == '(' && nl->type == BT_CHARACTER)
2909 descriptor_dimension chd[1] = { {1, clow, nl->string_length} };
2910 array_loop_spec ind[1] = { {1, clow, nl->string_length, 1} };
2912 if (nml_parse_qualifier (dtp, chd, ind, -1, nml_err_msg,
2913 nml_err_msg_size, &parsed_rank)
2914 == FAILURE)
2916 char *nml_err_msg_end = strchr (nml_err_msg, '\0');
2917 snprintf (nml_err_msg_end,
2918 nml_err_msg_size - (nml_err_msg_end - nml_err_msg),
2919 " for namelist variable %s", nl->var_name);
2920 goto nml_err_ret;
2923 clow = ind[0].start;
2924 chigh = ind[0].end;
2926 if (ind[0].step != 1)
2928 snprintf (nml_err_msg, nml_err_msg_size,
2929 "Step not allowed in substring qualifier"
2930 " for namelist object %s", nl->var_name);
2931 goto nml_err_ret;
2934 if ((c = next_char (dtp)) == EOF)
2935 return FAILURE;
2936 unget_char (dtp, c);
2939 /* Make sure no extraneous qualifiers are there. */
2941 if (c == '(')
2943 snprintf (nml_err_msg, nml_err_msg_size,
2944 "Qualifier for a scalar or non-character namelist object %s",
2945 nl->var_name);
2946 goto nml_err_ret;
2949 /* Make sure there is no more than one non-zero rank object. */
2950 if (non_zero_rank_count > 1)
2952 snprintf (nml_err_msg, nml_err_msg_size,
2953 "Multiple sub-objects with non-zero rank in namelist object %s",
2954 nl->var_name);
2955 non_zero_rank_count = 0;
2956 goto nml_err_ret;
2959 /* According to the standard, an equal sign MUST follow an object name. The
2960 following is possibly lax - it allows comments, blank lines and so on to
2961 intervene. eat_spaces (dtp); c = next_char (dtp); would be compliant*/
2963 free_saved (dtp);
2965 eat_separator (dtp);
2966 if (dtp->u.p.input_complete)
2967 return SUCCESS;
2969 if (dtp->u.p.at_eol)
2970 finish_separator (dtp);
2971 if (dtp->u.p.input_complete)
2972 return SUCCESS;
2974 if ((c = next_char (dtp)) == EOF)
2975 return FAILURE;
2977 if (c != '=')
2979 snprintf (nml_err_msg, nml_err_msg_size,
2980 "Equal sign must follow namelist object name %s",
2981 nl->var_name);
2982 goto nml_err_ret;
2984 /* If a derived type, touch its components and restore the root
2985 namelist_info if we have parsed a qualified derived type
2986 component. */
2988 if (nl->type == BT_DERIVED)
2989 nml_touch_nodes (nl);
2991 if (first_nl)
2993 if (first_nl->var_rank == 0)
2995 if (component_flag && qualifier_flag)
2996 nl = first_nl;
2998 else
2999 nl = first_nl;
3002 if (nml_read_obj (dtp, nl, 0, pprev_nl, nml_err_msg, nml_err_msg_size,
3003 clow, chigh) == FAILURE)
3004 goto nml_err_ret;
3006 return SUCCESS;
3008 nml_err_ret:
3010 return FAILURE;
3013 /* Entry point for namelist input. Goes through input until namelist name
3014 is matched. Then cycles through nml_get_obj_data until the input is
3015 completed or there is an error. */
3017 void
3018 namelist_read (st_parameter_dt *dtp)
3020 int c;
3021 char nml_err_msg[200];
3023 /* Initialize the error string buffer just in case we get an unexpected fail
3024 somewhere and end up at nml_err_ret. */
3025 strcpy (nml_err_msg, "Internal namelist read error");
3027 /* Pointer to the previously read object, in case attempt is made to read
3028 new object name. Should this fail, error message can give previous
3029 name. */
3030 namelist_info *prev_nl = NULL;
3032 dtp->u.p.namelist_mode = 1;
3033 dtp->u.p.input_complete = 0;
3034 dtp->u.p.expanded_read = 0;
3036 /* Look for &namelist_name . Skip all characters, testing for $nmlname.
3037 Exit on success or EOF. If '?' or '=?' encountered in stdin, print
3038 node names or namelist on stdout. */
3040 find_nml_name:
3041 c = next_char (dtp);
3042 switch (c)
3044 case '$':
3045 case '&':
3046 break;
3048 case '!':
3049 eat_line (dtp);
3050 goto find_nml_name;
3052 case '=':
3053 c = next_char (dtp);
3054 if (c == '?')
3055 nml_query (dtp, '=');
3056 else
3057 unget_char (dtp, c);
3058 goto find_nml_name;
3060 case '?':
3061 nml_query (dtp, '?');
3063 case EOF:
3064 return;
3066 default:
3067 goto find_nml_name;
3070 /* Match the name of the namelist. */
3072 nml_match_name (dtp, dtp->namelist_name, dtp->namelist_name_len);
3074 if (dtp->u.p.nml_read_error)
3075 goto find_nml_name;
3077 /* A trailing space is required, we give a little latitude here, 10.9.1. */
3078 c = next_char (dtp);
3079 if (!is_separator(c) && c != '!')
3081 unget_char (dtp, c);
3082 goto find_nml_name;
3085 unget_char (dtp, c);
3086 eat_separator (dtp);
3088 /* Ready to read namelist objects. If there is an error in input
3089 from stdin, output the error message and continue. */
3091 while (!dtp->u.p.input_complete)
3093 if (nml_get_obj_data (dtp, &prev_nl, nml_err_msg, sizeof nml_err_msg)
3094 == FAILURE)
3096 if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
3097 goto nml_err_ret;
3098 generate_error (&dtp->common, LIBERROR_READ_VALUE, nml_err_msg);
3101 /* Reset the previous namelist pointer if we know we are not going
3102 to be doing multiple reads within a single namelist object. */
3103 if (prev_nl && prev_nl->var_rank == 0)
3104 prev_nl = NULL;
3107 free_saved (dtp);
3108 free_line (dtp);
3109 return;
3112 nml_err_ret:
3114 /* All namelist error calls return from here */
3115 free_saved (dtp);
3116 free_line (dtp);
3117 generate_error (&dtp->common, LIBERROR_READ_VALUE, nml_err_msg);
3118 return;