Cleanup memsize types
[gcc.git] / libgfortran / io / list_read.c
1 /* Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
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
6
7 This file is part of the GNU Fortran runtime library (libgfortran).
8
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.
13
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.
18
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.
22
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/>. */
27
28
29 #include "io.h"
30 #include "fbuf.h"
31 #include "unix.h"
32 #include <string.h>
33 #include <stdlib.h>
34 #include <ctype.h>
35
36
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. */
41
42
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. */
50
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'
53
54 #define CASE_SEPARATORS case ' ': case ',': case '/': case '\n': case '\t': \
55 case '\r': case ';'
56
57 /* This macro assumes that we're operating on a variable. */
58
59 #define is_separator(c) (c == '/' || c == ',' || c == '\n' || c == ' ' \
60 || c == '\t' || c == '\r' || c == ';')
61
62 /* Maximum repeat count. Less than ten times the maximum signed int32. */
63
64 #define MAX_REPEAT 200000000
65
66 #ifndef HAVE_SNPRINTF
67 # undef snprintf
68 # define snprintf(str, size, ...) sprintf (str, __VA_ARGS__)
69 #endif
70
71 /* Save a character to a string buffer, enlarging it as necessary. */
72
73 static void
74 push_char (st_parameter_dt *dtp, char c)
75 {
76 char *new;
77
78 if (dtp->u.p.saved_string == NULL)
79 {
80 dtp->u.p.saved_string = get_mem (SCRATCH_SIZE);
81 // memset below should be commented out.
82 memset (dtp->u.p.saved_string, 0, SCRATCH_SIZE);
83 dtp->u.p.saved_length = SCRATCH_SIZE;
84 dtp->u.p.saved_used = 0;
85 }
86
87 if (dtp->u.p.saved_used >= dtp->u.p.saved_length)
88 {
89 dtp->u.p.saved_length = 2 * dtp->u.p.saved_length;
90 new = realloc (dtp->u.p.saved_string, dtp->u.p.saved_length);
91 if (new == NULL)
92 generate_error (&dtp->common, LIBERROR_OS, NULL);
93 dtp->u.p.saved_string = new;
94
95 // Also this should not be necessary.
96 memset (new + dtp->u.p.saved_used, 0,
97 dtp->u.p.saved_length - dtp->u.p.saved_used);
98
99 }
100
101 dtp->u.p.saved_string[dtp->u.p.saved_used++] = c;
102 }
103
104
105 /* Free the input buffer if necessary. */
106
107 static void
108 free_saved (st_parameter_dt *dtp)
109 {
110 if (dtp->u.p.saved_string == NULL)
111 return;
112
113 free (dtp->u.p.saved_string);
114
115 dtp->u.p.saved_string = NULL;
116 dtp->u.p.saved_used = 0;
117 }
118
119
120 /* Free the line buffer if necessary. */
121
122 static void
123 free_line (st_parameter_dt *dtp)
124 {
125 dtp->u.p.item_count = 0;
126 dtp->u.p.line_buffer_enabled = 0;
127
128 if (dtp->u.p.line_buffer == NULL)
129 return;
130
131 free (dtp->u.p.line_buffer);
132 dtp->u.p.line_buffer = NULL;
133 }
134
135
136 static int
137 next_char (st_parameter_dt *dtp)
138 {
139 ssize_t length;
140 gfc_offset record;
141 int c;
142
143 if (dtp->u.p.last_char != EOF - 1)
144 {
145 dtp->u.p.at_eol = 0;
146 c = dtp->u.p.last_char;
147 dtp->u.p.last_char = EOF - 1;
148 goto done;
149 }
150
151 /* Read from line_buffer if enabled. */
152
153 if (dtp->u.p.line_buffer_enabled)
154 {
155 dtp->u.p.at_eol = 0;
156
157 c = dtp->u.p.line_buffer[dtp->u.p.item_count];
158 if (c != '\0' && dtp->u.p.item_count < 64)
159 {
160 dtp->u.p.line_buffer[dtp->u.p.item_count] = '\0';
161 dtp->u.p.item_count++;
162 goto done;
163 }
164
165 dtp->u.p.item_count = 0;
166 dtp->u.p.line_buffer_enabled = 0;
167 }
168
169 /* Handle the end-of-record and end-of-file conditions for
170 internal array unit. */
171 if (is_array_io (dtp))
172 {
173 if (dtp->u.p.at_eof)
174 return EOF;
175
176 /* Check for "end-of-record" condition. */
177 if (dtp->u.p.current_unit->bytes_left == 0)
178 {
179 int finished;
180
181 c = '\n';
182 record = next_array_record (dtp, dtp->u.p.current_unit->ls,
183 &finished);
184
185 /* Check for "end-of-file" condition. */
186 if (finished)
187 {
188 dtp->u.p.at_eof = 1;
189 goto done;
190 }
191
192 record *= dtp->u.p.current_unit->recl;
193 if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
194 return EOF;
195
196 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
197 goto done;
198 }
199 }
200
201 /* Get the next character and handle end-of-record conditions. */
202
203 if (is_internal_unit (dtp))
204 {
205 char cc;
206 length = sread (dtp->u.p.current_unit->s, &cc, 1);
207 c = cc;
208 if (length < 0)
209 {
210 generate_error (&dtp->common, LIBERROR_OS, NULL);
211 return '\0';
212 }
213
214 if (is_array_io (dtp))
215 {
216 /* Check whether we hit EOF. */
217 if (length == 0)
218 {
219 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
220 return '\0';
221 }
222 dtp->u.p.current_unit->bytes_left--;
223 }
224 else
225 {
226 if (dtp->u.p.at_eof)
227 return EOF;
228 if (length == 0)
229 {
230 c = '\n';
231 dtp->u.p.at_eof = 1;
232 }
233 }
234 }
235 else
236 {
237 c = fbuf_getc (dtp->u.p.current_unit);
238 if (c != EOF && is_stream_io (dtp))
239 dtp->u.p.current_unit->strm_pos++;
240 }
241 done:
242 dtp->u.p.at_eol = (c == '\n' || c == '\r' || c == EOF);
243 return c;
244 }
245
246
247 /* Push a character back onto the input. */
248
249 static void
250 unget_char (st_parameter_dt *dtp, int c)
251 {
252 dtp->u.p.last_char = c;
253 }
254
255
256 /* Skip over spaces in the input. Returns the nonspace character that
257 terminated the eating and also places it back on the input. */
258
259 static int
260 eat_spaces (st_parameter_dt *dtp)
261 {
262 int c;
263
264 do
265 c = next_char (dtp);
266 while (c != EOF && (c == ' ' || c == '\t'));
267
268 unget_char (dtp, c);
269 return c;
270 }
271
272
273 /* This function reads characters through to the end of the current
274 line and just ignores them. Returns 0 for success and LIBERROR_END
275 if it hit EOF. */
276
277 static int
278 eat_line (st_parameter_dt *dtp)
279 {
280 int c;
281
282 do
283 c = next_char (dtp);
284 while (c != EOF && c != '\n');
285 if (c == EOF)
286 return LIBERROR_END;
287 return 0;
288 }
289
290
291 /* Skip over a separator. Technically, we don't always eat the whole
292 separator. This is because if we've processed the last input item,
293 then a separator is unnecessary. Plus the fact that operating
294 systems usually deliver console input on a line basis.
295
296 The upshot is that if we see a newline as part of reading a
297 separator, we stop reading. If there are more input items, we
298 continue reading the separator with finish_separator() which takes
299 care of the fact that we may or may not have seen a comma as part
300 of the separator.
301
302 Returns 0 for success, and non-zero error code otherwise. */
303
304 static int
305 eat_separator (st_parameter_dt *dtp)
306 {
307 int c, n;
308 int err = 0;
309
310 eat_spaces (dtp);
311 dtp->u.p.comma_flag = 0;
312
313 if ((c = next_char (dtp)) == EOF)
314 return LIBERROR_END;
315 switch (c)
316 {
317 case ',':
318 if (dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
319 {
320 unget_char (dtp, c);
321 break;
322 }
323 /* Fall through. */
324 case ';':
325 dtp->u.p.comma_flag = 1;
326 eat_spaces (dtp);
327 break;
328
329 case '/':
330 dtp->u.p.input_complete = 1;
331 break;
332
333 case '\r':
334 dtp->u.p.at_eol = 1;
335 if ((n = next_char(dtp)) == EOF)
336 return LIBERROR_END;
337 if (n != '\n')
338 {
339 unget_char (dtp, n);
340 break;
341 }
342 /* Fall through. */
343 case '\n':
344 dtp->u.p.at_eol = 1;
345 if (dtp->u.p.namelist_mode)
346 {
347 do
348 {
349 if ((c = next_char (dtp)) == EOF)
350 return LIBERROR_END;
351 if (c == '!')
352 {
353 err = eat_line (dtp);
354 if (err)
355 return err;
356 if ((c = next_char (dtp)) == EOF)
357 return LIBERROR_END;
358 if (c == '!')
359 {
360 err = eat_line (dtp);
361 if (err)
362 return err;
363 if ((c = next_char (dtp)) == EOF)
364 return LIBERROR_END;
365 }
366 }
367 }
368 while (c == '\n' || c == '\r' || c == ' ' || c == '\t');
369 unget_char (dtp, c);
370 }
371 break;
372
373 case '!':
374 if (dtp->u.p.namelist_mode)
375 { /* Eat a namelist comment. */
376 err = eat_line (dtp);
377 if (err)
378 return err;
379
380 break;
381 }
382
383 /* Fall Through... */
384
385 default:
386 unget_char (dtp, c);
387 break;
388 }
389 return err;
390 }
391
392
393 /* Finish processing a separator that was interrupted by a newline.
394 If we're here, then another data item is present, so we finish what
395 we started on the previous line. Return 0 on success, error code
396 on failure. */
397
398 static int
399 finish_separator (st_parameter_dt *dtp)
400 {
401 int c;
402 int err;
403
404 restart:
405 eat_spaces (dtp);
406
407 if ((c = next_char (dtp)) == EOF)
408 return LIBERROR_END;
409 switch (c)
410 {
411 case ',':
412 if (dtp->u.p.comma_flag)
413 unget_char (dtp, c);
414 else
415 {
416 if ((c = eat_spaces (dtp)) == EOF)
417 return LIBERROR_END;
418 if (c == '\n' || c == '\r')
419 goto restart;
420 }
421
422 break;
423
424 case '/':
425 dtp->u.p.input_complete = 1;
426 if (!dtp->u.p.namelist_mode)
427 return err;
428 break;
429
430 case '\n':
431 case '\r':
432 goto restart;
433
434 case '!':
435 if (dtp->u.p.namelist_mode)
436 {
437 err = eat_line (dtp);
438 if (err)
439 return err;
440 goto restart;
441 }
442
443 default:
444 unget_char (dtp, c);
445 break;
446 }
447 return err;
448 }
449
450
451 /* This function is needed to catch bad conversions so that namelist can
452 attempt to see if dtp->u.p.saved_string contains a new object name rather
453 than a bad value. */
454
455 static int
456 nml_bad_return (st_parameter_dt *dtp, char c)
457 {
458 if (dtp->u.p.namelist_mode)
459 {
460 dtp->u.p.nml_read_error = 1;
461 unget_char (dtp, c);
462 return 1;
463 }
464 return 0;
465 }
466
467 /* Convert an unsigned string to an integer. The length value is -1
468 if we are working on a repeat count. Returns nonzero if we have a
469 range problem. As a side effect, frees the dtp->u.p.saved_string. */
470
471 static int
472 convert_integer (st_parameter_dt *dtp, int length, int negative)
473 {
474 char c, *buffer, message[100];
475 int m;
476 GFC_INTEGER_LARGEST v, max, max10;
477
478 buffer = dtp->u.p.saved_string;
479 v = 0;
480
481 max = (length == -1) ? MAX_REPEAT : max_value (length, 1);
482 max10 = max / 10;
483
484 for (;;)
485 {
486 c = *buffer++;
487 if (c == '\0')
488 break;
489 c -= '0';
490
491 if (v > max10)
492 goto overflow;
493 v = 10 * v;
494
495 if (v > max - c)
496 goto overflow;
497 v += c;
498 }
499
500 m = 0;
501
502 if (length != -1)
503 {
504 if (negative)
505 v = -v;
506 set_integer (dtp->u.p.value, v, length);
507 }
508 else
509 {
510 dtp->u.p.repeat_count = v;
511
512 if (dtp->u.p.repeat_count == 0)
513 {
514 sprintf (message, "Zero repeat count in item %d of list input",
515 dtp->u.p.item_count);
516
517 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
518 m = 1;
519 }
520 }
521
522 free_saved (dtp);
523 return m;
524
525 overflow:
526 if (length == -1)
527 sprintf (message, "Repeat count overflow in item %d of list input",
528 dtp->u.p.item_count);
529 else
530 sprintf (message, "Integer overflow while reading item %d",
531 dtp->u.p.item_count);
532
533 free_saved (dtp);
534 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
535
536 return 1;
537 }
538
539
540 /* Parse a repeat count for logical and complex values which cannot
541 begin with a digit. Returns nonzero if we are done, zero if we
542 should continue on. */
543
544 static int
545 parse_repeat (st_parameter_dt *dtp)
546 {
547 char message[100];
548 int c, repeat;
549
550 if ((c = next_char (dtp)) == EOF)
551 goto bad_repeat;
552 switch (c)
553 {
554 CASE_DIGITS:
555 repeat = c - '0';
556 break;
557
558 CASE_SEPARATORS:
559 unget_char (dtp, c);
560 eat_separator (dtp);
561 return 1;
562
563 default:
564 unget_char (dtp, c);
565 return 0;
566 }
567
568 for (;;)
569 {
570 c = next_char (dtp);
571 switch (c)
572 {
573 CASE_DIGITS:
574 repeat = 10 * repeat + c - '0';
575
576 if (repeat > MAX_REPEAT)
577 {
578 sprintf (message,
579 "Repeat count overflow in item %d of list input",
580 dtp->u.p.item_count);
581
582 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
583 return 1;
584 }
585
586 break;
587
588 case '*':
589 if (repeat == 0)
590 {
591 sprintf (message,
592 "Zero repeat count in item %d of list input",
593 dtp->u.p.item_count);
594
595 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
596 return 1;
597 }
598
599 goto done;
600
601 default:
602 goto bad_repeat;
603 }
604 }
605
606 done:
607 dtp->u.p.repeat_count = repeat;
608 return 0;
609
610 bad_repeat:
611
612 free_saved (dtp);
613 if (c == EOF)
614 {
615 hit_eof (dtp);
616 return 1;
617 }
618 else
619 eat_line (dtp);
620 sprintf (message, "Bad repeat count in item %d of list input",
621 dtp->u.p.item_count);
622 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
623 return 1;
624 }
625
626
627 /* To read a logical we have to look ahead in the input stream to make sure
628 there is not an equal sign indicating a variable name. To do this we use
629 line_buffer to point to a temporary buffer, pushing characters there for
630 possible later reading. */
631
632 static void
633 l_push_char (st_parameter_dt *dtp, char c)
634 {
635 if (dtp->u.p.line_buffer == NULL)
636 {
637 dtp->u.p.line_buffer = get_mem (SCRATCH_SIZE);
638 memset (dtp->u.p.line_buffer, 0, SCRATCH_SIZE);
639 }
640
641 dtp->u.p.line_buffer[dtp->u.p.item_count++] = c;
642 }
643
644
645 /* Read a logical character on the input. */
646
647 static void
648 read_logical (st_parameter_dt *dtp, int length)
649 {
650 char message[100];
651 int c, i, v;
652
653 if (parse_repeat (dtp))
654 return;
655
656 c = tolower (next_char (dtp));
657 l_push_char (dtp, c);
658 switch (c)
659 {
660 case 't':
661 v = 1;
662 if ((c = next_char (dtp)) == EOF)
663 goto bad_logical;
664 l_push_char (dtp, c);
665
666 if (!is_separator(c))
667 goto possible_name;
668
669 unget_char (dtp, c);
670 break;
671 case 'f':
672 v = 0;
673 if ((c = next_char (dtp)) == EOF)
674 goto bad_logical;
675 l_push_char (dtp, c);
676
677 if (!is_separator(c))
678 goto possible_name;
679
680 unget_char (dtp, c);
681 break;
682
683 case '.':
684 c = tolower (next_char (dtp));
685 switch (c)
686 {
687 case 't':
688 v = 1;
689 break;
690 case 'f':
691 v = 0;
692 break;
693 default:
694 goto bad_logical;
695 }
696
697 break;
698
699 CASE_SEPARATORS:
700 unget_char (dtp, c);
701 eat_separator (dtp);
702 return; /* Null value. */
703
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;
709 }
710
711 dtp->u.p.saved_type = BT_LOGICAL;
712 dtp->u.p.saved_length = length;
713
714 /* Eat trailing garbage. */
715 do
716 c = next_char (dtp);
717 while (c != EOF && !is_separator (c));
718
719 unget_char (dtp, c);
720 eat_separator (dtp);
721 set_integer ((int *) dtp->u.p.value, v, length);
722 free_line (dtp);
723
724 return;
725
726 possible_name:
727
728 for(i = 0; i < 63; i++)
729 {
730 c = next_char (dtp);
731 if (is_separator(c))
732 {
733 /* All done if this is not a namelist read. */
734 if (!dtp->u.p.namelist_mode)
735 goto logical_done;
736
737 unget_char (dtp, c);
738 eat_separator (dtp);
739 c = next_char (dtp);
740 if (c != '=')
741 {
742 unget_char (dtp, c);
743 goto logical_done;
744 }
745 }
746
747 l_push_char (dtp, c);
748 if (c == '=')
749 {
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;
754 }
755
756 }
757
758 bad_logical:
759
760 free_line (dtp);
761
762 if (nml_bad_return (dtp, c))
763 return;
764
765 free_saved (dtp);
766 if (c == EOF)
767 {
768 hit_eof (dtp);
769 return;
770 }
771 else if (c != '\n')
772 eat_line (dtp);
773 sprintf (message, "Bad logical value while reading item %d",
774 dtp->u.p.item_count);
775 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
776 return;
777
778 logical_done:
779
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);
785 }
786
787
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. */
792
793 static void
794 read_integer (st_parameter_dt *dtp, int length)
795 {
796 char message[100];
797 int c, negative;
798
799 negative = 0;
800
801 c = next_char (dtp);
802 switch (c)
803 {
804 case '-':
805 negative = 1;
806 /* Fall through... */
807
808 case '+':
809 if ((c = next_char (dtp)) == EOF)
810 goto bad_integer;
811 goto get_integer;
812
813 CASE_SEPARATORS: /* Single null. */
814 unget_char (dtp, c);
815 eat_separator (dtp);
816 return;
817
818 CASE_DIGITS:
819 push_char (dtp, c);
820 break;
821
822 default:
823 goto bad_integer;
824 }
825
826 /* Take care of what may be a repeat count. */
827
828 for (;;)
829 {
830 c = next_char (dtp);
831 switch (c)
832 {
833 CASE_DIGITS:
834 push_char (dtp, c);
835 break;
836
837 case '*':
838 push_char (dtp, '\0');
839 goto repeat;
840
841 CASE_SEPARATORS: /* Not a repeat count. */
842 goto done;
843
844 default:
845 goto bad_integer;
846 }
847 }
848
849 repeat:
850 if (convert_integer (dtp, -1, 0))
851 return;
852
853 /* Get the real integer. */
854
855 if ((c = next_char (dtp)) == EOF)
856 goto bad_integer;
857 switch (c)
858 {
859 CASE_DIGITS:
860 break;
861
862 CASE_SEPARATORS:
863 unget_char (dtp, c);
864 eat_separator (dtp);
865 return;
866
867 case '-':
868 negative = 1;
869 /* Fall through... */
870
871 case '+':
872 c = next_char (dtp);
873 break;
874 }
875
876 get_integer:
877 if (!isdigit (c))
878 goto bad_integer;
879 push_char (dtp, c);
880
881 for (;;)
882 {
883 c = next_char (dtp);
884 switch (c)
885 {
886 CASE_DIGITS:
887 push_char (dtp, c);
888 break;
889
890 CASE_SEPARATORS:
891 goto done;
892
893 default:
894 goto bad_integer;
895 }
896 }
897
898 bad_integer:
899
900 if (nml_bad_return (dtp, c))
901 return;
902
903 free_saved (dtp);
904 if (c == EOF)
905 {
906 hit_eof (dtp);
907 return;
908 }
909 else if (c != '\n')
910 eat_line (dtp);
911 sprintf (message, "Bad integer for item %d in list input",
912 dtp->u.p.item_count);
913 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
914
915 return;
916
917 done:
918 unget_char (dtp, c);
919 eat_separator (dtp);
920
921 push_char (dtp, '\0');
922 if (convert_integer (dtp, length, negative))
923 {
924 free_saved (dtp);
925 return;
926 }
927
928 free_saved (dtp);
929 dtp->u.p.saved_type = BT_INTEGER;
930 }
931
932
933 /* Read a character variable. */
934
935 static void
936 read_character (st_parameter_dt *dtp, int length __attribute__ ((unused)))
937 {
938 char quote, message[100];
939 int c;
940
941 quote = ' '; /* Space means no quote character. */
942
943 if ((c = next_char (dtp)) == EOF)
944 goto eof;
945 switch (c)
946 {
947 CASE_DIGITS:
948 push_char (dtp, c);
949 break;
950
951 CASE_SEPARATORS:
952 unget_char (dtp, c); /* NULL value. */
953 eat_separator (dtp);
954 return;
955
956 case '"':
957 case '\'':
958 quote = c;
959 goto get_string;
960
961 default:
962 if (dtp->u.p.namelist_mode)
963 {
964 unget_char (dtp, c);
965 return;
966 }
967
968 push_char (dtp, c);
969 goto get_string;
970 }
971
972 /* Deal with a possible repeat count. */
973
974 for (;;)
975 {
976 if ((c = next_char (dtp)) == EOF)
977 goto eof;
978 switch (c)
979 {
980 CASE_DIGITS:
981 push_char (dtp, c);
982 break;
983
984 CASE_SEPARATORS:
985 unget_char (dtp, c);
986 goto done; /* String was only digits! */
987
988 case '*':
989 push_char (dtp, '\0');
990 goto got_repeat;
991
992 default:
993 push_char (dtp, c);
994 goto get_string; /* Not a repeat count after all. */
995 }
996 }
997
998 got_repeat:
999 if (convert_integer (dtp, -1, 0))
1000 return;
1001
1002 /* Now get the real string. */
1003
1004 if ((c = next_char (dtp)) == EOF)
1005 goto eof;
1006 switch (c)
1007 {
1008 CASE_SEPARATORS:
1009 unget_char (dtp, c); /* Repeated NULL values. */
1010 eat_separator (dtp);
1011 return;
1012
1013 case '"':
1014 case '\'':
1015 quote = c;
1016 break;
1017
1018 default:
1019 push_char (dtp, c);
1020 break;
1021 }
1022
1023 get_string:
1024 for (;;)
1025 {
1026 if ((c = next_char (dtp)) == EOF)
1027 goto eof;
1028 switch (c)
1029 {
1030 case '"':
1031 case '\'':
1032 if (c != quote)
1033 {
1034 push_char (dtp, c);
1035 break;
1036 }
1037
1038 /* See if we have a doubled quote character or the end of
1039 the string. */
1040
1041 if ((c = next_char (dtp)) == EOF)
1042 goto eof;
1043 if (c == quote)
1044 {
1045 push_char (dtp, quote);
1046 break;
1047 }
1048
1049 unget_char (dtp, c);
1050 goto done;
1051
1052 CASE_SEPARATORS:
1053 if (quote == ' ')
1054 {
1055 unget_char (dtp, c);
1056 goto done;
1057 }
1058
1059 if (c != '\n' && c != '\r')
1060 push_char (dtp, c);
1061 break;
1062
1063 default:
1064 push_char (dtp, c);
1065 break;
1066 }
1067 }
1068
1069 /* At this point, we have to have a separator, or else the string is
1070 invalid. */
1071 done:
1072 c = next_char (dtp);
1073 eof:
1074 if (is_separator (c) || c == '!')
1075 {
1076 unget_char (dtp, c);
1077 eat_separator (dtp);
1078 dtp->u.p.saved_type = BT_CHARACTER;
1079 free_line (dtp);
1080 }
1081 else
1082 {
1083 free_saved (dtp);
1084 if (c == EOF)
1085 {
1086 hit_eof (dtp);
1087 return;
1088 }
1089 sprintf (message, "Invalid string input in item %d",
1090 dtp->u.p.item_count);
1091 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1092 }
1093 }
1094
1095
1096 /* Parse a component of a complex constant or a real number that we
1097 are sure is already there. This is a straight real number parser. */
1098
1099 static int
1100 parse_real (st_parameter_dt *dtp, void *buffer, int length)
1101 {
1102 char message[100];
1103 int c, m, seen_dp;
1104
1105 if ((c = next_char (dtp)) == EOF)
1106 goto bad;
1107
1108 if (c == '-' || c == '+')
1109 {
1110 push_char (dtp, c);
1111 if ((c = next_char (dtp)) == EOF)
1112 goto bad;
1113 }
1114
1115 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1116 c = '.';
1117
1118 if (!isdigit (c) && c != '.')
1119 {
1120 if (c == 'i' || c == 'I' || c == 'n' || c == 'N')
1121 goto inf_nan;
1122 else
1123 goto bad;
1124 }
1125
1126 push_char (dtp, c);
1127
1128 seen_dp = (c == '.') ? 1 : 0;
1129
1130 for (;;)
1131 {
1132 if ((c = next_char (dtp)) == EOF)
1133 goto bad;
1134 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1135 c = '.';
1136 switch (c)
1137 {
1138 CASE_DIGITS:
1139 push_char (dtp, c);
1140 break;
1141
1142 case '.':
1143 if (seen_dp)
1144 goto bad;
1145
1146 seen_dp = 1;
1147 push_char (dtp, c);
1148 break;
1149
1150 case 'e':
1151 case 'E':
1152 case 'd':
1153 case 'D':
1154 push_char (dtp, 'e');
1155 goto exp1;
1156
1157 case '-':
1158 case '+':
1159 push_char (dtp, 'e');
1160 push_char (dtp, c);
1161 if ((c = next_char (dtp)) == EOF)
1162 goto bad;
1163 goto exp2;
1164
1165 CASE_SEPARATORS:
1166 goto done;
1167
1168 default:
1169 goto done;
1170 }
1171 }
1172
1173 exp1:
1174 if ((c = next_char (dtp)) == EOF)
1175 goto bad;
1176 if (c != '-' && c != '+')
1177 push_char (dtp, '+');
1178 else
1179 {
1180 push_char (dtp, c);
1181 c = next_char (dtp);
1182 }
1183
1184 exp2:
1185 if (!isdigit (c))
1186 goto bad;
1187
1188 push_char (dtp, c);
1189
1190 for (;;)
1191 {
1192 if ((c = next_char (dtp)) == EOF)
1193 goto bad;
1194 switch (c)
1195 {
1196 CASE_DIGITS:
1197 push_char (dtp, c);
1198 break;
1199
1200 CASE_SEPARATORS:
1201 unget_char (dtp, c);
1202 goto done;
1203
1204 default:
1205 goto done;
1206 }
1207 }
1208
1209 done:
1210 unget_char (dtp, c);
1211 push_char (dtp, '\0');
1212
1213 m = convert_real (dtp, buffer, dtp->u.p.saved_string, length);
1214 free_saved (dtp);
1215
1216 return m;
1217
1218 done_infnan:
1219 unget_char (dtp, c);
1220 push_char (dtp, '\0');
1221
1222 m = convert_infnan (dtp, buffer, dtp->u.p.saved_string, length);
1223 free_saved (dtp);
1224
1225 return m;
1226
1227 inf_nan:
1228 /* Match INF and Infinity. */
1229 if ((c == 'i' || c == 'I')
1230 && ((c = next_char (dtp)) == 'n' || c == 'N')
1231 && ((c = next_char (dtp)) == 'f' || c == 'F'))
1232 {
1233 c = next_char (dtp);
1234 if ((c != 'i' && c != 'I')
1235 || ((c == 'i' || c == 'I')
1236 && ((c = next_char (dtp)) == 'n' || c == 'N')
1237 && ((c = next_char (dtp)) == 'i' || c == 'I')
1238 && ((c = next_char (dtp)) == 't' || c == 'T')
1239 && ((c = next_char (dtp)) == 'y' || c == 'Y')
1240 && (c = next_char (dtp))))
1241 {
1242 if (is_separator (c))
1243 unget_char (dtp, c);
1244 push_char (dtp, 'i');
1245 push_char (dtp, 'n');
1246 push_char (dtp, 'f');
1247 goto done_infnan;
1248 }
1249 } /* Match NaN. */
1250 else if (((c = next_char (dtp)) == 'a' || c == 'A')
1251 && ((c = next_char (dtp)) == 'n' || c == 'N')
1252 && (c = next_char (dtp)))
1253 {
1254 if (is_separator (c))
1255 unget_char (dtp, c);
1256 push_char (dtp, 'n');
1257 push_char (dtp, 'a');
1258 push_char (dtp, 'n');
1259
1260 /* Match "NAN(alphanum)". */
1261 if (c == '(')
1262 {
1263 for ( ; c != ')'; c = next_char (dtp))
1264 if (is_separator (c))
1265 goto bad;
1266
1267 c = next_char (dtp);
1268 if (is_separator (c))
1269 unget_char (dtp, c);
1270 }
1271 goto done_infnan;
1272 }
1273
1274 bad:
1275
1276 if (nml_bad_return (dtp, c))
1277 return 0;
1278
1279 free_saved (dtp);
1280 if (c == EOF)
1281 {
1282 hit_eof (dtp);
1283 return 1;
1284 }
1285 else if (c != '\n')
1286 eat_line (dtp);
1287 sprintf (message, "Bad floating point number for item %d",
1288 dtp->u.p.item_count);
1289 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1290
1291 return 1;
1292 }
1293
1294
1295 /* Reading a complex number is straightforward because we can tell
1296 what it is right away. */
1297
1298 static void
1299 read_complex (st_parameter_dt *dtp, void * dest, int kind, size_t size)
1300 {
1301 char message[100];
1302 int c;
1303
1304 if (parse_repeat (dtp))
1305 return;
1306
1307 c = next_char (dtp);
1308 switch (c)
1309 {
1310 case '(':
1311 break;
1312
1313 CASE_SEPARATORS:
1314 unget_char (dtp, c);
1315 eat_separator (dtp);
1316 return;
1317
1318 default:
1319 goto bad_complex;
1320 }
1321
1322 eol_1:
1323 eat_spaces (dtp);
1324 c = next_char (dtp);
1325 if (c == '\n' || c== '\r')
1326 goto eol_1;
1327 else
1328 unget_char (dtp, c);
1329
1330 if (parse_real (dtp, dest, kind))
1331 return;
1332
1333 eol_2:
1334 eat_spaces (dtp);
1335 c = next_char (dtp);
1336 if (c == '\n' || c== '\r')
1337 goto eol_2;
1338 else
1339 unget_char (dtp, c);
1340
1341 if (next_char (dtp)
1342 != (dtp->u.p.current_unit->decimal_status == DECIMAL_POINT ? ',' : ';'))
1343 goto bad_complex;
1344
1345 eol_3:
1346 eat_spaces (dtp);
1347 c = next_char (dtp);
1348 if (c == '\n' || c== '\r')
1349 goto eol_3;
1350 else
1351 unget_char (dtp, c);
1352
1353 if (parse_real (dtp, dest + size / 2, kind))
1354 return;
1355
1356 eol_4:
1357 eat_spaces (dtp);
1358 c = next_char (dtp);
1359 if (c == '\n' || c== '\r')
1360 goto eol_4;
1361 else
1362 unget_char (dtp, c);
1363
1364 if (next_char (dtp) != ')')
1365 goto bad_complex;
1366
1367 c = next_char (dtp);
1368 if (!is_separator (c))
1369 goto bad_complex;
1370
1371 unget_char (dtp, c);
1372 eat_separator (dtp);
1373
1374 free_saved (dtp);
1375 dtp->u.p.saved_type = BT_COMPLEX;
1376 return;
1377
1378 bad_complex:
1379
1380 if (nml_bad_return (dtp, c))
1381 return;
1382
1383 free_saved (dtp);
1384 if (c == EOF)
1385 {
1386 hit_eof (dtp);
1387 return;
1388 }
1389 else if (c != '\n')
1390 eat_line (dtp);
1391 sprintf (message, "Bad complex value in item %d of list input",
1392 dtp->u.p.item_count);
1393 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1394 }
1395
1396
1397 /* Parse a real number with a possible repeat count. */
1398
1399 static void
1400 read_real (st_parameter_dt *dtp, void * dest, int length)
1401 {
1402 char message[100];
1403 int c;
1404 int seen_dp;
1405 int is_inf;
1406
1407 seen_dp = 0;
1408
1409 c = next_char (dtp);
1410 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1411 c = '.';
1412 switch (c)
1413 {
1414 CASE_DIGITS:
1415 push_char (dtp, c);
1416 break;
1417
1418 case '.':
1419 push_char (dtp, c);
1420 seen_dp = 1;
1421 break;
1422
1423 case '+':
1424 case '-':
1425 goto got_sign;
1426
1427 CASE_SEPARATORS:
1428 unget_char (dtp, c); /* Single null. */
1429 eat_separator (dtp);
1430 return;
1431
1432 case 'i':
1433 case 'I':
1434 case 'n':
1435 case 'N':
1436 goto inf_nan;
1437
1438 default:
1439 goto bad_real;
1440 }
1441
1442 /* Get the digit string that might be a repeat count. */
1443
1444 for (;;)
1445 {
1446 c = next_char (dtp);
1447 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1448 c = '.';
1449 switch (c)
1450 {
1451 CASE_DIGITS:
1452 push_char (dtp, c);
1453 break;
1454
1455 case '.':
1456 if (seen_dp)
1457 goto bad_real;
1458
1459 seen_dp = 1;
1460 push_char (dtp, c);
1461 goto real_loop;
1462
1463 case 'E':
1464 case 'e':
1465 case 'D':
1466 case 'd':
1467 goto exp1;
1468
1469 case '+':
1470 case '-':
1471 push_char (dtp, 'e');
1472 push_char (dtp, c);
1473 c = next_char (dtp);
1474 goto exp2;
1475
1476 case '*':
1477 push_char (dtp, '\0');
1478 goto got_repeat;
1479
1480 CASE_SEPARATORS:
1481 if (c != '\n' && c != ',' && c != '\r' && c != ';')
1482 unget_char (dtp, c);
1483 goto done;
1484
1485 default:
1486 goto bad_real;
1487 }
1488 }
1489
1490 got_repeat:
1491 if (convert_integer (dtp, -1, 0))
1492 return;
1493
1494 /* Now get the number itself. */
1495
1496 if ((c = next_char (dtp)) == EOF)
1497 goto bad_real;
1498 if (is_separator (c))
1499 { /* Repeated null value. */
1500 unget_char (dtp, c);
1501 eat_separator (dtp);
1502 return;
1503 }
1504
1505 if (c != '-' && c != '+')
1506 push_char (dtp, '+');
1507 else
1508 {
1509 got_sign:
1510 push_char (dtp, c);
1511 if ((c = next_char (dtp)) == EOF)
1512 goto bad_real;
1513 }
1514
1515 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1516 c = '.';
1517
1518 if (!isdigit (c) && c != '.')
1519 {
1520 if (c == 'i' || c == 'I' || c == 'n' || c == 'N')
1521 goto inf_nan;
1522 else
1523 goto bad_real;
1524 }
1525
1526 if (c == '.')
1527 {
1528 if (seen_dp)
1529 goto bad_real;
1530 else
1531 seen_dp = 1;
1532 }
1533
1534 push_char (dtp, c);
1535
1536 real_loop:
1537 for (;;)
1538 {
1539 c = next_char (dtp);
1540 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1541 c = '.';
1542 switch (c)
1543 {
1544 CASE_DIGITS:
1545 push_char (dtp, c);
1546 break;
1547
1548 CASE_SEPARATORS:
1549 case EOF:
1550 goto done;
1551
1552 case '.':
1553 if (seen_dp)
1554 goto bad_real;
1555
1556 seen_dp = 1;
1557 push_char (dtp, c);
1558 break;
1559
1560 case 'E':
1561 case 'e':
1562 case 'D':
1563 case 'd':
1564 goto exp1;
1565
1566 case '+':
1567 case '-':
1568 push_char (dtp, 'e');
1569 push_char (dtp, c);
1570 c = next_char (dtp);
1571 goto exp2;
1572
1573 default:
1574 goto bad_real;
1575 }
1576 }
1577
1578 exp1:
1579 push_char (dtp, 'e');
1580
1581 if ((c = next_char (dtp)) == EOF)
1582 goto bad_real;
1583 if (c != '+' && c != '-')
1584 push_char (dtp, '+');
1585 else
1586 {
1587 push_char (dtp, c);
1588 c = next_char (dtp);
1589 }
1590
1591 exp2:
1592 if (!isdigit (c))
1593 goto bad_real;
1594 push_char (dtp, c);
1595
1596 for (;;)
1597 {
1598 c = next_char (dtp);
1599
1600 switch (c)
1601 {
1602 CASE_DIGITS:
1603 push_char (dtp, c);
1604 break;
1605
1606 CASE_SEPARATORS:
1607 goto done;
1608
1609 default:
1610 goto bad_real;
1611 }
1612 }
1613
1614 done:
1615 unget_char (dtp, c);
1616 eat_separator (dtp);
1617 push_char (dtp, '\0');
1618 if (convert_real (dtp, dest, dtp->u.p.saved_string, length))
1619 return;
1620
1621 free_saved (dtp);
1622 dtp->u.p.saved_type = BT_REAL;
1623 return;
1624
1625 inf_nan:
1626 l_push_char (dtp, c);
1627 is_inf = 0;
1628
1629 /* Match INF and Infinity. */
1630 if (c == 'i' || c == 'I')
1631 {
1632 c = next_char (dtp);
1633 l_push_char (dtp, c);
1634 if (c != 'n' && c != 'N')
1635 goto unwind;
1636 c = next_char (dtp);
1637 l_push_char (dtp, c);
1638 if (c != 'f' && c != 'F')
1639 goto unwind;
1640 c = next_char (dtp);
1641 l_push_char (dtp, c);
1642 if (!is_separator (c))
1643 {
1644 if (c != 'i' && c != 'I')
1645 goto unwind;
1646 c = next_char (dtp);
1647 l_push_char (dtp, c);
1648 if (c != 'n' && c != 'N')
1649 goto unwind;
1650 c = next_char (dtp);
1651 l_push_char (dtp, c);
1652 if (c != 'i' && c != 'I')
1653 goto unwind;
1654 c = next_char (dtp);
1655 l_push_char (dtp, c);
1656 if (c != 't' && c != 'T')
1657 goto unwind;
1658 c = next_char (dtp);
1659 l_push_char (dtp, c);
1660 if (c != 'y' && c != 'Y')
1661 goto unwind;
1662 c = next_char (dtp);
1663 l_push_char (dtp, c);
1664 }
1665 is_inf = 1;
1666 } /* Match NaN. */
1667 else
1668 {
1669 c = next_char (dtp);
1670 l_push_char (dtp, c);
1671 if (c != 'a' && c != 'A')
1672 goto unwind;
1673 c = next_char (dtp);
1674 l_push_char (dtp, c);
1675 if (c != 'n' && c != 'N')
1676 goto unwind;
1677 c = next_char (dtp);
1678 l_push_char (dtp, c);
1679
1680 /* Match NAN(alphanum). */
1681 if (c == '(')
1682 {
1683 for (c = next_char (dtp); c != ')'; c = next_char (dtp))
1684 if (is_separator (c))
1685 goto unwind;
1686 else
1687 l_push_char (dtp, c);
1688
1689 l_push_char (dtp, ')');
1690 c = next_char (dtp);
1691 l_push_char (dtp, c);
1692 }
1693 }
1694
1695 if (!is_separator (c))
1696 goto unwind;
1697
1698 if (dtp->u.p.namelist_mode)
1699 {
1700 if (c == ' ' || c =='\n' || c == '\r')
1701 {
1702 do
1703 {
1704 if ((c = next_char (dtp)) == EOF)
1705 goto bad_real;
1706 }
1707 while (c == ' ' || c =='\n' || c == '\r');
1708
1709 l_push_char (dtp, c);
1710
1711 if (c == '=')
1712 goto unwind;
1713 }
1714 }
1715
1716 if (is_inf)
1717 {
1718 push_char (dtp, 'i');
1719 push_char (dtp, 'n');
1720 push_char (dtp, 'f');
1721 }
1722 else
1723 {
1724 push_char (dtp, 'n');
1725 push_char (dtp, 'a');
1726 push_char (dtp, 'n');
1727 }
1728
1729 free_line (dtp);
1730 unget_char (dtp, c);
1731 eat_separator (dtp);
1732 push_char (dtp, '\0');
1733 if (convert_infnan (dtp, dest, dtp->u.p.saved_string, length))
1734 return;
1735
1736 free_saved (dtp);
1737 dtp->u.p.saved_type = BT_REAL;
1738 return;
1739
1740 unwind:
1741 if (dtp->u.p.namelist_mode)
1742 {
1743 dtp->u.p.nml_read_error = 1;
1744 dtp->u.p.line_buffer_enabled = 1;
1745 dtp->u.p.item_count = 0;
1746 return;
1747 }
1748
1749 bad_real:
1750
1751 if (nml_bad_return (dtp, c))
1752 return;
1753
1754 free_saved (dtp);
1755 if (c == EOF)
1756 {
1757 hit_eof (dtp);
1758 return;
1759 }
1760 else if (c != '\n')
1761 eat_line (dtp);
1762
1763 sprintf (message, "Bad real number in item %d of list input",
1764 dtp->u.p.item_count);
1765 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1766 }
1767
1768
1769 /* Check the current type against the saved type to make sure they are
1770 compatible. Returns nonzero if incompatible. */
1771
1772 static int
1773 check_type (st_parameter_dt *dtp, bt type, int len)
1774 {
1775 char message[100];
1776
1777 if (dtp->u.p.saved_type != BT_UNKNOWN && dtp->u.p.saved_type != type)
1778 {
1779 sprintf (message, "Read type %s where %s was expected for item %d",
1780 type_name (dtp->u.p.saved_type), type_name (type),
1781 dtp->u.p.item_count);
1782
1783 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1784 return 1;
1785 }
1786
1787 if (dtp->u.p.saved_type == BT_UNKNOWN || dtp->u.p.saved_type == BT_CHARACTER)
1788 return 0;
1789
1790 if (dtp->u.p.saved_length != len)
1791 {
1792 sprintf (message,
1793 "Read kind %d %s where kind %d is required for item %d",
1794 dtp->u.p.saved_length, type_name (dtp->u.p.saved_type), len,
1795 dtp->u.p.item_count);
1796 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1797 return 1;
1798 }
1799
1800 return 0;
1801 }
1802
1803
1804 /* Top level data transfer subroutine for list reads. Because we have
1805 to deal with repeat counts, the data item is always saved after
1806 reading, usually in the dtp->u.p.value[] array. If a repeat count is
1807 greater than one, we copy the data item multiple times. */
1808
1809 static int
1810 list_formatted_read_scalar (st_parameter_dt *dtp, bt type, void *p,
1811 int kind, size_t size)
1812 {
1813 gfc_char4_t *q;
1814 int c, i, m;
1815 int err = 0;
1816
1817 dtp->u.p.namelist_mode = 0;
1818
1819 if (dtp->u.p.first_item)
1820 {
1821 dtp->u.p.first_item = 0;
1822 dtp->u.p.input_complete = 0;
1823 dtp->u.p.repeat_count = 1;
1824 dtp->u.p.at_eol = 0;
1825
1826 if ((c = eat_spaces (dtp)) == EOF)
1827 {
1828 err = LIBERROR_END;
1829 goto cleanup;
1830 }
1831 if (is_separator (c))
1832 {
1833 /* Found a null value. */
1834 eat_separator (dtp);
1835 dtp->u.p.repeat_count = 0;
1836
1837 /* eat_separator sets this flag if the separator was a comma. */
1838 if (dtp->u.p.comma_flag)
1839 goto cleanup;
1840
1841 /* eat_separator sets this flag if the separator was a \n or \r. */
1842 if (dtp->u.p.at_eol)
1843 finish_separator (dtp);
1844 else
1845 goto cleanup;
1846 }
1847
1848 }
1849 else
1850 {
1851 if (dtp->u.p.repeat_count > 0)
1852 {
1853 if (check_type (dtp, type, kind))
1854 return err;
1855 goto set_value;
1856 }
1857
1858 if (dtp->u.p.input_complete)
1859 goto cleanup;
1860
1861 if (dtp->u.p.at_eol)
1862 finish_separator (dtp);
1863 else
1864 {
1865 eat_spaces (dtp);
1866 /* Trailing spaces prior to end of line. */
1867 if (dtp->u.p.at_eol)
1868 finish_separator (dtp);
1869 }
1870
1871 dtp->u.p.saved_type = BT_UNKNOWN;
1872 dtp->u.p.repeat_count = 1;
1873 }
1874
1875 switch (type)
1876 {
1877 case BT_INTEGER:
1878 read_integer (dtp, kind);
1879 break;
1880 case BT_LOGICAL:
1881 read_logical (dtp, kind);
1882 break;
1883 case BT_CHARACTER:
1884 read_character (dtp, kind);
1885 break;
1886 case BT_REAL:
1887 read_real (dtp, p, kind);
1888 /* Copy value back to temporary if needed. */
1889 if (dtp->u.p.repeat_count > 0)
1890 memcpy (dtp->u.p.value, p, kind);
1891 break;
1892 case BT_COMPLEX:
1893 read_complex (dtp, p, kind, size);
1894 /* Copy value back to temporary if needed. */
1895 if (dtp->u.p.repeat_count > 0)
1896 memcpy (dtp->u.p.value, p, size);
1897 break;
1898 default:
1899 internal_error (&dtp->common, "Bad type for list read");
1900 }
1901
1902 if (dtp->u.p.saved_type != BT_CHARACTER && dtp->u.p.saved_type != BT_UNKNOWN)
1903 dtp->u.p.saved_length = size;
1904
1905 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1906 goto cleanup;
1907
1908 set_value:
1909 switch (dtp->u.p.saved_type)
1910 {
1911 case BT_COMPLEX:
1912 case BT_REAL:
1913 if (dtp->u.p.repeat_count > 0)
1914 memcpy (p, dtp->u.p.value, size);
1915 break;
1916
1917 case BT_INTEGER:
1918 case BT_LOGICAL:
1919 memcpy (p, dtp->u.p.value, size);
1920 break;
1921
1922 case BT_CHARACTER:
1923 if (dtp->u.p.saved_string)
1924 {
1925 m = ((int) size < dtp->u.p.saved_used)
1926 ? (int) size : dtp->u.p.saved_used;
1927 if (kind == 1)
1928 memcpy (p, dtp->u.p.saved_string, m);
1929 else
1930 {
1931 q = (gfc_char4_t *) p;
1932 for (i = 0; i < m; i++)
1933 q[i] = (unsigned char) dtp->u.p.saved_string[i];
1934 }
1935 }
1936 else
1937 /* Just delimiters encountered, nothing to copy but SPACE. */
1938 m = 0;
1939
1940 if (m < (int) size)
1941 {
1942 if (kind == 1)
1943 memset (((char *) p) + m, ' ', size - m);
1944 else
1945 {
1946 q = (gfc_char4_t *) p;
1947 for (i = m; i < (int) size; i++)
1948 q[i] = (unsigned char) ' ';
1949 }
1950 }
1951 break;
1952
1953 case BT_UNKNOWN:
1954 break;
1955
1956 default:
1957 internal_error (&dtp->common, "Bad type for list read");
1958 }
1959
1960 if (--dtp->u.p.repeat_count <= 0)
1961 free_saved (dtp);
1962
1963 cleanup:
1964 if (err == LIBERROR_END)
1965 hit_eof (dtp);
1966 return err;
1967 }
1968
1969
1970 void
1971 list_formatted_read (st_parameter_dt *dtp, bt type, void *p, int kind,
1972 size_t size, size_t nelems)
1973 {
1974 size_t elem;
1975 char *tmp;
1976 size_t stride = type == BT_CHARACTER ?
1977 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
1978 int err;
1979
1980 tmp = (char *) p;
1981
1982 /* Big loop over all the elements. */
1983 for (elem = 0; elem < nelems; elem++)
1984 {
1985 dtp->u.p.item_count++;
1986 err = list_formatted_read_scalar (dtp, type, tmp + stride*elem,
1987 kind, size);
1988 if (err)
1989 break;
1990 }
1991 }
1992
1993
1994 /* Finish a list read. */
1995
1996 void
1997 finish_list_read (st_parameter_dt *dtp)
1998 {
1999 int err;
2000
2001 free_saved (dtp);
2002
2003 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
2004
2005 if (dtp->u.p.at_eol)
2006 {
2007 dtp->u.p.at_eol = 0;
2008 return;
2009 }
2010
2011 err = eat_line (dtp);
2012 if (err == LIBERROR_END)
2013 hit_eof (dtp);
2014 }
2015
2016 /* NAMELIST INPUT
2017
2018 void namelist_read (st_parameter_dt *dtp)
2019 calls:
2020 static void nml_match_name (char *name, int len)
2021 static int nml_query (st_parameter_dt *dtp)
2022 static int nml_get_obj_data (st_parameter_dt *dtp,
2023 namelist_info **prev_nl, char *, size_t)
2024 calls:
2025 static void nml_untouch_nodes (st_parameter_dt *dtp)
2026 static namelist_info * find_nml_node (st_parameter_dt *dtp,
2027 char * var_name)
2028 static int nml_parse_qualifier(descriptor_dimension * ad,
2029 array_loop_spec * ls, int rank, char *)
2030 static void nml_touch_nodes (namelist_info * nl)
2031 static int nml_read_obj (namelist_info *nl, index_type offset,
2032 namelist_info **prev_nl, char *, size_t,
2033 index_type clow, index_type chigh)
2034 calls:
2035 -itself- */
2036
2037 /* Inputs a rank-dimensional qualifier, which can contain
2038 singlets, doublets, triplets or ':' with the standard meanings. */
2039
2040 static try
2041 nml_parse_qualifier (st_parameter_dt *dtp, descriptor_dimension *ad,
2042 array_loop_spec *ls, int rank, char *parse_err_msg,
2043 int *parsed_rank)
2044 {
2045 int dim;
2046 int indx;
2047 int neg;
2048 int null_flag;
2049 int is_array_section, is_char;
2050 int c;
2051
2052 is_char = 0;
2053 is_array_section = 0;
2054 dtp->u.p.expanded_read = 0;
2055
2056 /* See if this is a character substring qualifier we are looking for. */
2057 if (rank == -1)
2058 {
2059 rank = 1;
2060 is_char = 1;
2061 }
2062
2063 /* The next character in the stream should be the '('. */
2064
2065 if ((c = next_char (dtp)) == EOF)
2066 return FAILURE;
2067
2068 /* Process the qualifier, by dimension and triplet. */
2069
2070 for (dim=0; dim < rank; dim++ )
2071 {
2072 for (indx=0; indx<3; indx++)
2073 {
2074 free_saved (dtp);
2075 eat_spaces (dtp);
2076 neg = 0;
2077
2078 /* Process a potential sign. */
2079 if ((c = next_char (dtp)) == EOF)
2080 return FAILURE;
2081 switch (c)
2082 {
2083 case '-':
2084 neg = 1;
2085 break;
2086
2087 case '+':
2088 break;
2089
2090 default:
2091 unget_char (dtp, c);
2092 break;
2093 }
2094
2095 /* Process characters up to the next ':' , ',' or ')'. */
2096 for (;;)
2097 {
2098 if ((c = next_char (dtp)) == EOF)
2099 return FAILURE;
2100
2101 switch (c)
2102 {
2103 case ':':
2104 is_array_section = 1;
2105 break;
2106
2107 case ',': case ')':
2108 if ((c==',' && dim == rank -1)
2109 || (c==')' && dim < rank -1))
2110 {
2111 if (is_char)
2112 sprintf (parse_err_msg, "Bad substring qualifier");
2113 else
2114 sprintf (parse_err_msg, "Bad number of index fields");
2115 goto err_ret;
2116 }
2117 break;
2118
2119 CASE_DIGITS:
2120 push_char (dtp, c);
2121 continue;
2122
2123 case ' ': case '\t':
2124 eat_spaces (dtp);
2125 if ((c = next_char (dtp) == EOF))
2126 return FAILURE;
2127 break;
2128
2129 default:
2130 if (is_char)
2131 sprintf (parse_err_msg,
2132 "Bad character in substring qualifier");
2133 else
2134 sprintf (parse_err_msg, "Bad character in index");
2135 goto err_ret;
2136 }
2137
2138 if ((c == ',' || c == ')') && indx == 0
2139 && dtp->u.p.saved_string == 0)
2140 {
2141 if (is_char)
2142 sprintf (parse_err_msg, "Null substring qualifier");
2143 else
2144 sprintf (parse_err_msg, "Null index field");
2145 goto err_ret;
2146 }
2147
2148 if ((c == ':' && indx == 1 && dtp->u.p.saved_string == 0)
2149 || (indx == 2 && dtp->u.p.saved_string == 0))
2150 {
2151 if (is_char)
2152 sprintf (parse_err_msg, "Bad substring qualifier");
2153 else
2154 sprintf (parse_err_msg, "Bad index triplet");
2155 goto err_ret;
2156 }
2157
2158 if (is_char && !is_array_section)
2159 {
2160 sprintf (parse_err_msg,
2161 "Missing colon in substring qualifier");
2162 goto err_ret;
2163 }
2164
2165 /* If '( : ? )' or '( ? : )' break and flag read failure. */
2166 null_flag = 0;
2167 if ((c == ':' && indx == 0 && dtp->u.p.saved_string == 0)
2168 || (indx==1 && dtp->u.p.saved_string == 0))
2169 {
2170 null_flag = 1;
2171 break;
2172 }
2173
2174 /* Now read the index. */
2175 if (convert_integer (dtp, sizeof(index_type), neg))
2176 {
2177 if (is_char)
2178 sprintf (parse_err_msg, "Bad integer substring qualifier");
2179 else
2180 sprintf (parse_err_msg, "Bad integer in index");
2181 goto err_ret;
2182 }
2183 break;
2184 }
2185
2186 /* Feed the index values to the triplet arrays. */
2187 if (!null_flag)
2188 {
2189 if (indx == 0)
2190 memcpy (&ls[dim].start, dtp->u.p.value, sizeof(index_type));
2191 if (indx == 1)
2192 memcpy (&ls[dim].end, dtp->u.p.value, sizeof(index_type));
2193 if (indx == 2)
2194 memcpy (&ls[dim].step, dtp->u.p.value, sizeof(index_type));
2195 }
2196
2197 /* Singlet or doublet indices. */
2198 if (c==',' || c==')')
2199 {
2200 if (indx == 0)
2201 {
2202 memcpy (&ls[dim].start, dtp->u.p.value, sizeof(index_type));
2203
2204 /* If -std=f95/2003 or an array section is specified,
2205 do not allow excess data to be processed. */
2206 if (is_array_section == 1
2207 || !(compile_options.allow_std & GFC_STD_GNU)
2208 || !dtp->u.p.ionml->touched
2209 || dtp->u.p.ionml->type == BT_DERIVED)
2210 ls[dim].end = ls[dim].start;
2211 else
2212 dtp->u.p.expanded_read = 1;
2213 }
2214
2215 /* Check for non-zero rank. */
2216 if (is_array_section == 1 && ls[dim].start != ls[dim].end)
2217 *parsed_rank = 1;
2218
2219 break;
2220 }
2221 }
2222
2223 if (is_array_section == 1 && dtp->u.p.expanded_read == 1)
2224 {
2225 int i;
2226 dtp->u.p.expanded_read = 0;
2227 for (i = 0; i < dim; i++)
2228 ls[i].end = ls[i].start;
2229 }
2230
2231 /* Check the values of the triplet indices. */
2232 if ((ls[dim].start > GFC_DIMENSION_UBOUND(ad[dim]))
2233 || (ls[dim].start < GFC_DIMENSION_LBOUND(ad[dim]))
2234 || (ls[dim].end > GFC_DIMENSION_UBOUND(ad[dim]))
2235 || (ls[dim].end < GFC_DIMENSION_LBOUND(ad[dim])))
2236 {
2237 if (is_char)
2238 sprintf (parse_err_msg, "Substring out of range");
2239 else
2240 sprintf (parse_err_msg, "Index %d out of range", dim + 1);
2241 goto err_ret;
2242 }
2243
2244 if (((ls[dim].end - ls[dim].start ) * ls[dim].step < 0)
2245 || (ls[dim].step == 0))
2246 {
2247 sprintf (parse_err_msg, "Bad range in index %d", dim + 1);
2248 goto err_ret;
2249 }
2250
2251 /* Initialise the loop index counter. */
2252 ls[dim].idx = ls[dim].start;
2253 }
2254 eat_spaces (dtp);
2255 return SUCCESS;
2256
2257 err_ret:
2258
2259 return FAILURE;
2260 }
2261
2262 static namelist_info *
2263 find_nml_node (st_parameter_dt *dtp, char * var_name)
2264 {
2265 namelist_info * t = dtp->u.p.ionml;
2266 while (t != NULL)
2267 {
2268 if (strcmp (var_name, t->var_name) == 0)
2269 {
2270 t->touched = 1;
2271 return t;
2272 }
2273 t = t->next;
2274 }
2275 return NULL;
2276 }
2277
2278 /* Visits all the components of a derived type that have
2279 not explicitly been identified in the namelist input.
2280 touched is set and the loop specification initialised
2281 to default values */
2282
2283 static void
2284 nml_touch_nodes (namelist_info * nl)
2285 {
2286 index_type len = strlen (nl->var_name) + 1;
2287 int dim;
2288 char * ext_name = (char*)get_mem (len + 1);
2289 memcpy (ext_name, nl->var_name, len-1);
2290 memcpy (ext_name + len - 1, "%", 2);
2291 for (nl = nl->next; nl; nl = nl->next)
2292 {
2293 if (strncmp (nl->var_name, ext_name, len) == 0)
2294 {
2295 nl->touched = 1;
2296 for (dim=0; dim < nl->var_rank; dim++)
2297 {
2298 nl->ls[dim].step = 1;
2299 nl->ls[dim].end = GFC_DESCRIPTOR_UBOUND(nl,dim);
2300 nl->ls[dim].start = GFC_DESCRIPTOR_LBOUND(nl,dim);
2301 nl->ls[dim].idx = nl->ls[dim].start;
2302 }
2303 }
2304 else
2305 break;
2306 }
2307 free (ext_name);
2308 return;
2309 }
2310
2311 /* Resets touched for the entire list of nml_nodes, ready for a
2312 new object. */
2313
2314 static void
2315 nml_untouch_nodes (st_parameter_dt *dtp)
2316 {
2317 namelist_info * t;
2318 for (t = dtp->u.p.ionml; t; t = t->next)
2319 t->touched = 0;
2320 return;
2321 }
2322
2323 /* Attempts to input name to namelist name. Returns
2324 dtp->u.p.nml_read_error = 1 on no match. */
2325
2326 static void
2327 nml_match_name (st_parameter_dt *dtp, const char *name, index_type len)
2328 {
2329 index_type i;
2330 int c;
2331
2332 dtp->u.p.nml_read_error = 0;
2333 for (i = 0; i < len; i++)
2334 {
2335 c = next_char (dtp);
2336 if (c == EOF || (tolower (c) != tolower (name[i])))
2337 {
2338 dtp->u.p.nml_read_error = 1;
2339 break;
2340 }
2341 }
2342 }
2343
2344 /* If the namelist read is from stdin, output the current state of the
2345 namelist to stdout. This is used to implement the non-standard query
2346 features, ? and =?. If c == '=' the full namelist is printed. Otherwise
2347 the names alone are printed. */
2348
2349 static void
2350 nml_query (st_parameter_dt *dtp, char c)
2351 {
2352 gfc_unit * temp_unit;
2353 namelist_info * nl;
2354 index_type len;
2355 char * p;
2356 #ifdef HAVE_CRLF
2357 static const index_type endlen = 3;
2358 static const char endl[] = "\r\n";
2359 static const char nmlend[] = "&end\r\n";
2360 #else
2361 static const index_type endlen = 2;
2362 static const char endl[] = "\n";
2363 static const char nmlend[] = "&end\n";
2364 #endif
2365
2366 if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
2367 return;
2368
2369 /* Store the current unit and transfer to stdout. */
2370
2371 temp_unit = dtp->u.p.current_unit;
2372 dtp->u.p.current_unit = find_unit (options.stdout_unit);
2373
2374 if (dtp->u.p.current_unit)
2375 {
2376 dtp->u.p.mode = WRITING;
2377 next_record (dtp, 0);
2378
2379 /* Write the namelist in its entirety. */
2380
2381 if (c == '=')
2382 namelist_write (dtp);
2383
2384 /* Or write the list of names. */
2385
2386 else
2387 {
2388 /* "&namelist_name\n" */
2389
2390 len = dtp->namelist_name_len;
2391 p = write_block (dtp, len + endlen);
2392 if (!p)
2393 goto query_return;
2394 memcpy (p, "&", 1);
2395 memcpy ((char*)(p + 1), dtp->namelist_name, len);
2396 memcpy ((char*)(p + len + 1), &endl, endlen - 1);
2397 for (nl = dtp->u.p.ionml; nl; nl = nl->next)
2398 {
2399 /* " var_name\n" */
2400
2401 len = strlen (nl->var_name);
2402 p = write_block (dtp, len + endlen);
2403 if (!p)
2404 goto query_return;
2405 memcpy (p, " ", 1);
2406 memcpy ((char*)(p + 1), nl->var_name, len);
2407 memcpy ((char*)(p + len + 1), &endl, endlen - 1);
2408 }
2409
2410 /* "&end\n" */
2411
2412 p = write_block (dtp, endlen + 3);
2413 goto query_return;
2414 memcpy (p, &nmlend, endlen + 3);
2415 }
2416
2417 /* Flush the stream to force immediate output. */
2418
2419 fbuf_flush (dtp->u.p.current_unit, WRITING);
2420 sflush (dtp->u.p.current_unit->s);
2421 unlock_unit (dtp->u.p.current_unit);
2422 }
2423
2424 query_return:
2425
2426 /* Restore the current unit. */
2427
2428 dtp->u.p.current_unit = temp_unit;
2429 dtp->u.p.mode = READING;
2430 return;
2431 }
2432
2433 /* Reads and stores the input for the namelist object nl. For an array,
2434 the function loops over the ranges defined by the loop specification.
2435 This default to all the data or to the specification from a qualifier.
2436 nml_read_obj recursively calls itself to read derived types. It visits
2437 all its own components but only reads data for those that were touched
2438 when the name was parsed. If a read error is encountered, an attempt is
2439 made to return to read a new object name because the standard allows too
2440 little data to be available. On the other hand, too much data is an
2441 error. */
2442
2443 static try
2444 nml_read_obj (st_parameter_dt *dtp, namelist_info * nl, index_type offset,
2445 namelist_info **pprev_nl, char *nml_err_msg,
2446 size_t nml_err_msg_size, index_type clow, index_type chigh)
2447 {
2448 namelist_info * cmp;
2449 char * obj_name;
2450 int nml_carry;
2451 int len;
2452 int dim;
2453 index_type dlen;
2454 index_type m;
2455 size_t obj_name_len;
2456 void * pdata;
2457
2458 /* This object not touched in name parsing. */
2459
2460 if (!nl->touched)
2461 return SUCCESS;
2462
2463 dtp->u.p.repeat_count = 0;
2464 eat_spaces (dtp);
2465
2466 len = nl->len;
2467 switch (nl->type)
2468 {
2469 case BT_INTEGER:
2470 case BT_LOGICAL:
2471 dlen = len;
2472 break;
2473
2474 case BT_REAL:
2475 dlen = size_from_real_kind (len);
2476 break;
2477
2478 case BT_COMPLEX:
2479 dlen = size_from_complex_kind (len);
2480 break;
2481
2482 case BT_CHARACTER:
2483 dlen = chigh ? (chigh - clow + 1) : nl->string_length;
2484 break;
2485
2486 default:
2487 dlen = 0;
2488 }
2489
2490 do
2491 {
2492 /* Update the pointer to the data, using the current index vector */
2493
2494 pdata = (void*)(nl->mem_pos + offset);
2495 for (dim = 0; dim < nl->var_rank; dim++)
2496 pdata = (void*)(pdata + (nl->ls[dim].idx
2497 - GFC_DESCRIPTOR_LBOUND(nl,dim))
2498 * GFC_DESCRIPTOR_STRIDE(nl,dim) * nl->size);
2499
2500 /* Reset the error flag and try to read next value, if
2501 dtp->u.p.repeat_count=0 */
2502
2503 dtp->u.p.nml_read_error = 0;
2504 nml_carry = 0;
2505 if (--dtp->u.p.repeat_count <= 0)
2506 {
2507 if (dtp->u.p.input_complete)
2508 return SUCCESS;
2509 if (dtp->u.p.at_eol)
2510 finish_separator (dtp);
2511 if (dtp->u.p.input_complete)
2512 return SUCCESS;
2513
2514 dtp->u.p.saved_type = BT_UNKNOWN;
2515 free_saved (dtp);
2516
2517 switch (nl->type)
2518 {
2519 case BT_INTEGER:
2520 read_integer (dtp, len);
2521 break;
2522
2523 case BT_LOGICAL:
2524 read_logical (dtp, len);
2525 break;
2526
2527 case BT_CHARACTER:
2528 read_character (dtp, len);
2529 break;
2530
2531 case BT_REAL:
2532 /* Need to copy data back from the real location to the temp in order
2533 to handle nml reads into arrays. */
2534 read_real (dtp, pdata, len);
2535 memcpy (dtp->u.p.value, pdata, dlen);
2536 break;
2537
2538 case BT_COMPLEX:
2539 /* Same as for REAL, copy back to temp. */
2540 read_complex (dtp, pdata, len, dlen);
2541 memcpy (dtp->u.p.value, pdata, dlen);
2542 break;
2543
2544 case BT_DERIVED:
2545 obj_name_len = strlen (nl->var_name) + 1;
2546 obj_name = get_mem (obj_name_len+1);
2547 memcpy (obj_name, nl->var_name, obj_name_len-1);
2548 memcpy (obj_name + obj_name_len - 1, "%", 2);
2549
2550 /* If reading a derived type, disable the expanded read warning
2551 since a single object can have multiple reads. */
2552 dtp->u.p.expanded_read = 0;
2553
2554 /* Now loop over the components. Update the component pointer
2555 with the return value from nml_write_obj. This loop jumps
2556 past nested derived types by testing if the potential
2557 component name contains '%'. */
2558
2559 for (cmp = nl->next;
2560 cmp &&
2561 !strncmp (cmp->var_name, obj_name, obj_name_len) &&
2562 !strchr (cmp->var_name + obj_name_len, '%');
2563 cmp = cmp->next)
2564 {
2565
2566 if (nml_read_obj (dtp, cmp, (index_type)(pdata - nl->mem_pos),
2567 pprev_nl, nml_err_msg, nml_err_msg_size,
2568 clow, chigh) == FAILURE)
2569 {
2570 free (obj_name);
2571 return FAILURE;
2572 }
2573
2574 if (dtp->u.p.input_complete)
2575 {
2576 free (obj_name);
2577 return SUCCESS;
2578 }
2579 }
2580
2581 free (obj_name);
2582 goto incr_idx;
2583
2584 default:
2585 snprintf (nml_err_msg, nml_err_msg_size,
2586 "Bad type for namelist object %s", nl->var_name);
2587 internal_error (&dtp->common, nml_err_msg);
2588 goto nml_err_ret;
2589 }
2590 }
2591
2592 /* The standard permits array data to stop short of the number of
2593 elements specified in the loop specification. In this case, we
2594 should be here with dtp->u.p.nml_read_error != 0. Control returns to
2595 nml_get_obj_data and an attempt is made to read object name. */
2596
2597 *pprev_nl = nl;
2598 if (dtp->u.p.nml_read_error)
2599 {
2600 dtp->u.p.expanded_read = 0;
2601 return SUCCESS;
2602 }
2603
2604 if (dtp->u.p.saved_type == BT_UNKNOWN)
2605 {
2606 dtp->u.p.expanded_read = 0;
2607 goto incr_idx;
2608 }
2609
2610 switch (dtp->u.p.saved_type)
2611 {
2612
2613 case BT_COMPLEX:
2614 case BT_REAL:
2615 case BT_INTEGER:
2616 case BT_LOGICAL:
2617 memcpy (pdata, dtp->u.p.value, dlen);
2618 break;
2619
2620 case BT_CHARACTER:
2621 if (dlen < dtp->u.p.saved_used)
2622 {
2623 if (compile_options.bounds_check)
2624 {
2625 snprintf (nml_err_msg, nml_err_msg_size,
2626 "Namelist object '%s' truncated on read.",
2627 nl->var_name);
2628 generate_warning (&dtp->common, nml_err_msg);
2629 }
2630 m = dlen;
2631 }
2632 else
2633 m = dtp->u.p.saved_used;
2634 pdata = (void*)( pdata + clow - 1 );
2635 memcpy (pdata, dtp->u.p.saved_string, m);
2636 if (m < dlen)
2637 memset ((void*)( pdata + m ), ' ', dlen - m);
2638 break;
2639
2640 default:
2641 break;
2642 }
2643
2644 /* Warn if a non-standard expanded read occurs. A single read of a
2645 single object is acceptable. If a second read occurs, issue a warning
2646 and set the flag to zero to prevent further warnings. */
2647 if (dtp->u.p.expanded_read == 2)
2648 {
2649 notify_std (&dtp->common, GFC_STD_GNU, "Non-standard expanded namelist read.");
2650 dtp->u.p.expanded_read = 0;
2651 }
2652
2653 /* If the expanded read warning flag is set, increment it,
2654 indicating that a single read has occurred. */
2655 if (dtp->u.p.expanded_read >= 1)
2656 dtp->u.p.expanded_read++;
2657
2658 /* Break out of loop if scalar. */
2659 if (!nl->var_rank)
2660 break;
2661
2662 /* Now increment the index vector. */
2663
2664 incr_idx:
2665
2666 nml_carry = 1;
2667 for (dim = 0; dim < nl->var_rank; dim++)
2668 {
2669 nl->ls[dim].idx += nml_carry * nl->ls[dim].step;
2670 nml_carry = 0;
2671 if (((nl->ls[dim].step > 0) && (nl->ls[dim].idx > nl->ls[dim].end))
2672 ||
2673 ((nl->ls[dim].step < 0) && (nl->ls[dim].idx < nl->ls[dim].end)))
2674 {
2675 nl->ls[dim].idx = nl->ls[dim].start;
2676 nml_carry = 1;
2677 }
2678 }
2679 } while (!nml_carry);
2680
2681 if (dtp->u.p.repeat_count > 1)
2682 {
2683 snprintf (nml_err_msg, nml_err_msg_size,
2684 "Repeat count too large for namelist object %s", nl->var_name);
2685 goto nml_err_ret;
2686 }
2687 return SUCCESS;
2688
2689 nml_err_ret:
2690
2691 return FAILURE;
2692 }
2693
2694 /* Parses the object name, including array and substring qualifiers. It
2695 iterates over derived type components, touching those components and
2696 setting their loop specifications, if there is a qualifier. If the
2697 object is itself a derived type, its components and subcomponents are
2698 touched. nml_read_obj is called at the end and this reads the data in
2699 the manner specified by the object name. */
2700
2701 static try
2702 nml_get_obj_data (st_parameter_dt *dtp, namelist_info **pprev_nl,
2703 char *nml_err_msg, size_t nml_err_msg_size)
2704 {
2705 int c;
2706 namelist_info * nl;
2707 namelist_info * first_nl = NULL;
2708 namelist_info * root_nl = NULL;
2709 int dim, parsed_rank;
2710 int component_flag, qualifier_flag;
2711 index_type clow, chigh;
2712 int non_zero_rank_count;
2713
2714 /* Look for end of input or object name. If '?' or '=?' are encountered
2715 in stdin, print the node names or the namelist to stdout. */
2716
2717 eat_separator (dtp);
2718 if (dtp->u.p.input_complete)
2719 return SUCCESS;
2720
2721 if (dtp->u.p.at_eol)
2722 finish_separator (dtp);
2723 if (dtp->u.p.input_complete)
2724 return SUCCESS;
2725
2726 if ((c = next_char (dtp)) == EOF)
2727 return FAILURE;
2728 switch (c)
2729 {
2730 case '=':
2731 if ((c = next_char (dtp)) == EOF)
2732 return FAILURE;
2733 if (c != '?')
2734 {
2735 sprintf (nml_err_msg, "namelist read: misplaced = sign");
2736 goto nml_err_ret;
2737 }
2738 nml_query (dtp, '=');
2739 return SUCCESS;
2740
2741 case '?':
2742 nml_query (dtp, '?');
2743 return SUCCESS;
2744
2745 case '$':
2746 case '&':
2747 nml_match_name (dtp, "end", 3);
2748 if (dtp->u.p.nml_read_error)
2749 {
2750 sprintf (nml_err_msg, "namelist not terminated with / or &end");
2751 goto nml_err_ret;
2752 }
2753 case '/':
2754 dtp->u.p.input_complete = 1;
2755 return SUCCESS;
2756
2757 default :
2758 break;
2759 }
2760
2761 /* Untouch all nodes of the namelist and reset the flags that are set for
2762 derived type components. */
2763
2764 nml_untouch_nodes (dtp);
2765 component_flag = 0;
2766 qualifier_flag = 0;
2767 non_zero_rank_count = 0;
2768
2769 /* Get the object name - should '!' and '\n' be permitted separators? */
2770
2771 get_name:
2772
2773 free_saved (dtp);
2774
2775 do
2776 {
2777 if (!is_separator (c))
2778 push_char (dtp, tolower(c));
2779 if ((c = next_char (dtp)) == EOF)
2780 return FAILURE;
2781 } while (!( c=='=' || c==' ' || c=='\t' || c =='(' || c =='%' ));
2782
2783 unget_char (dtp, c);
2784
2785 /* Check that the name is in the namelist and get pointer to object.
2786 Three error conditions exist: (i) An attempt is being made to
2787 identify a non-existent object, following a failed data read or
2788 (ii) The object name does not exist or (iii) Too many data items
2789 are present for an object. (iii) gives the same error message
2790 as (i) */
2791
2792 push_char (dtp, '\0');
2793
2794 if (component_flag)
2795 {
2796 size_t var_len = strlen (root_nl->var_name);
2797 size_t saved_len
2798 = dtp->u.p.saved_string ? strlen (dtp->u.p.saved_string) : 0;
2799 char ext_name[var_len + saved_len + 1];
2800
2801 memcpy (ext_name, root_nl->var_name, var_len);
2802 if (dtp->u.p.saved_string)
2803 memcpy (ext_name + var_len, dtp->u.p.saved_string, saved_len);
2804 ext_name[var_len + saved_len] = '\0';
2805 nl = find_nml_node (dtp, ext_name);
2806 }
2807 else
2808 nl = find_nml_node (dtp, dtp->u.p.saved_string);
2809
2810 if (nl == NULL)
2811 {
2812 if (dtp->u.p.nml_read_error && *pprev_nl)
2813 snprintf (nml_err_msg, nml_err_msg_size,
2814 "Bad data for namelist object %s", (*pprev_nl)->var_name);
2815
2816 else
2817 snprintf (nml_err_msg, nml_err_msg_size,
2818 "Cannot match namelist object name %s",
2819 dtp->u.p.saved_string);
2820
2821 goto nml_err_ret;
2822 }
2823
2824 /* Get the length, data length, base pointer and rank of the variable.
2825 Set the default loop specification first. */
2826
2827 for (dim=0; dim < nl->var_rank; dim++)
2828 {
2829 nl->ls[dim].step = 1;
2830 nl->ls[dim].end = GFC_DESCRIPTOR_UBOUND(nl,dim);
2831 nl->ls[dim].start = GFC_DESCRIPTOR_LBOUND(nl,dim);
2832 nl->ls[dim].idx = nl->ls[dim].start;
2833 }
2834
2835 /* Check to see if there is a qualifier: if so, parse it.*/
2836
2837 if (c == '(' && nl->var_rank)
2838 {
2839 parsed_rank = 0;
2840 if (nml_parse_qualifier (dtp, nl->dim, nl->ls, nl->var_rank,
2841 nml_err_msg, &parsed_rank) == FAILURE)
2842 {
2843 char *nml_err_msg_end = strchr (nml_err_msg, '\0');
2844 snprintf (nml_err_msg_end,
2845 nml_err_msg_size - (nml_err_msg_end - nml_err_msg),
2846 " for namelist variable %s", nl->var_name);
2847 goto nml_err_ret;
2848 }
2849 if (parsed_rank > 0)
2850 non_zero_rank_count++;
2851
2852 qualifier_flag = 1;
2853
2854 if ((c = next_char (dtp)) == EOF)
2855 return FAILURE;
2856 unget_char (dtp, c);
2857 }
2858 else if (nl->var_rank > 0)
2859 non_zero_rank_count++;
2860
2861 /* Now parse a derived type component. The root namelist_info address
2862 is backed up, as is the previous component level. The component flag
2863 is set and the iteration is made by jumping back to get_name. */
2864
2865 if (c == '%')
2866 {
2867 if (nl->type != BT_DERIVED)
2868 {
2869 snprintf (nml_err_msg, nml_err_msg_size,
2870 "Attempt to get derived component for %s", nl->var_name);
2871 goto nml_err_ret;
2872 }
2873
2874 if (*pprev_nl == NULL || !component_flag)
2875 first_nl = nl;
2876
2877 root_nl = nl;
2878
2879 component_flag = 1;
2880 if ((c = next_char (dtp)) == EOF)
2881 return FAILURE;
2882 goto get_name;
2883 }
2884
2885 /* Parse a character qualifier, if present. chigh = 0 is a default
2886 that signals that the string length = string_length. */
2887
2888 clow = 1;
2889 chigh = 0;
2890
2891 if (c == '(' && nl->type == BT_CHARACTER)
2892 {
2893 descriptor_dimension chd[1] = { {1, clow, nl->string_length} };
2894 array_loop_spec ind[1] = { {1, clow, nl->string_length, 1} };
2895
2896 if (nml_parse_qualifier (dtp, chd, ind, -1, nml_err_msg, &parsed_rank)
2897 == FAILURE)
2898 {
2899 char *nml_err_msg_end = strchr (nml_err_msg, '\0');
2900 snprintf (nml_err_msg_end,
2901 nml_err_msg_size - (nml_err_msg_end - nml_err_msg),
2902 " for namelist variable %s", nl->var_name);
2903 goto nml_err_ret;
2904 }
2905
2906 clow = ind[0].start;
2907 chigh = ind[0].end;
2908
2909 if (ind[0].step != 1)
2910 {
2911 snprintf (nml_err_msg, nml_err_msg_size,
2912 "Step not allowed in substring qualifier"
2913 " for namelist object %s", nl->var_name);
2914 goto nml_err_ret;
2915 }
2916
2917 if ((c = next_char (dtp)) == EOF)
2918 return FAILURE;
2919 unget_char (dtp, c);
2920 }
2921
2922 /* Make sure no extraneous qualifiers are there. */
2923
2924 if (c == '(')
2925 {
2926 snprintf (nml_err_msg, nml_err_msg_size,
2927 "Qualifier for a scalar or non-character namelist object %s",
2928 nl->var_name);
2929 goto nml_err_ret;
2930 }
2931
2932 /* Make sure there is no more than one non-zero rank object. */
2933 if (non_zero_rank_count > 1)
2934 {
2935 snprintf (nml_err_msg, nml_err_msg_size,
2936 "Multiple sub-objects with non-zero rank in namelist object %s",
2937 nl->var_name);
2938 non_zero_rank_count = 0;
2939 goto nml_err_ret;
2940 }
2941
2942 /* According to the standard, an equal sign MUST follow an object name. The
2943 following is possibly lax - it allows comments, blank lines and so on to
2944 intervene. eat_spaces (dtp); c = next_char (dtp); would be compliant*/
2945
2946 free_saved (dtp);
2947
2948 eat_separator (dtp);
2949 if (dtp->u.p.input_complete)
2950 return SUCCESS;
2951
2952 if (dtp->u.p.at_eol)
2953 finish_separator (dtp);
2954 if (dtp->u.p.input_complete)
2955 return SUCCESS;
2956
2957 if ((c = next_char (dtp)) == EOF)
2958 return FAILURE;
2959
2960 if (c != '=')
2961 {
2962 snprintf (nml_err_msg, nml_err_msg_size,
2963 "Equal sign must follow namelist object name %s",
2964 nl->var_name);
2965 goto nml_err_ret;
2966 }
2967 /* If a derived type, touch its components and restore the root
2968 namelist_info if we have parsed a qualified derived type
2969 component. */
2970
2971 if (nl->type == BT_DERIVED)
2972 nml_touch_nodes (nl);
2973
2974 if (first_nl)
2975 {
2976 if (first_nl->var_rank == 0)
2977 {
2978 if (component_flag && qualifier_flag)
2979 nl = first_nl;
2980 }
2981 else
2982 nl = first_nl;
2983 }
2984
2985 if (nml_read_obj (dtp, nl, 0, pprev_nl, nml_err_msg, nml_err_msg_size,
2986 clow, chigh) == FAILURE)
2987 goto nml_err_ret;
2988
2989 return SUCCESS;
2990
2991 nml_err_ret:
2992
2993 return FAILURE;
2994 }
2995
2996 /* Entry point for namelist input. Goes through input until namelist name
2997 is matched. Then cycles through nml_get_obj_data until the input is
2998 completed or there is an error. */
2999
3000 void
3001 namelist_read (st_parameter_dt *dtp)
3002 {
3003 int c;
3004 char nml_err_msg[200];
3005
3006 /* Initialize the error string buffer just in case we get an unexpected fail
3007 somewhere and end up at nml_err_ret. */
3008 strcpy (nml_err_msg, "Internal namelist read error");
3009
3010 /* Pointer to the previously read object, in case attempt is made to read
3011 new object name. Should this fail, error message can give previous
3012 name. */
3013 namelist_info *prev_nl = NULL;
3014
3015 dtp->u.p.namelist_mode = 1;
3016 dtp->u.p.input_complete = 0;
3017 dtp->u.p.expanded_read = 0;
3018
3019 /* Look for &namelist_name . Skip all characters, testing for $nmlname.
3020 Exit on success or EOF. If '?' or '=?' encountered in stdin, print
3021 node names or namelist on stdout. */
3022
3023 find_nml_name:
3024 c = next_char (dtp);
3025 switch (c)
3026 {
3027 case '$':
3028 case '&':
3029 break;
3030
3031 case '!':
3032 eat_line (dtp);
3033 goto find_nml_name;
3034
3035 case '=':
3036 c = next_char (dtp);
3037 if (c == '?')
3038 nml_query (dtp, '=');
3039 else
3040 unget_char (dtp, c);
3041 goto find_nml_name;
3042
3043 case '?':
3044 nml_query (dtp, '?');
3045
3046 case EOF:
3047 return;
3048
3049 default:
3050 goto find_nml_name;
3051 }
3052
3053 /* Match the name of the namelist. */
3054
3055 nml_match_name (dtp, dtp->namelist_name, dtp->namelist_name_len);
3056
3057 if (dtp->u.p.nml_read_error)
3058 goto find_nml_name;
3059
3060 /* A trailing space is required, we give a little lattitude here, 10.9.1. */
3061 c = next_char (dtp);
3062 if (!is_separator(c) && c != '!')
3063 {
3064 unget_char (dtp, c);
3065 goto find_nml_name;
3066 }
3067
3068 unget_char (dtp, c);
3069 eat_separator (dtp);
3070
3071 /* Ready to read namelist objects. If there is an error in input
3072 from stdin, output the error message and continue. */
3073
3074 while (!dtp->u.p.input_complete)
3075 {
3076 if (nml_get_obj_data (dtp, &prev_nl, nml_err_msg, sizeof nml_err_msg)
3077 == FAILURE)
3078 {
3079 if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
3080 goto nml_err_ret;
3081 generate_error (&dtp->common, LIBERROR_READ_VALUE, nml_err_msg);
3082 }
3083
3084 /* Reset the previous namelist pointer if we know we are not going
3085 to be doing multiple reads within a single namelist object. */
3086 if (prev_nl && prev_nl->var_rank == 0)
3087 prev_nl = NULL;
3088 }
3089
3090 free_saved (dtp);
3091 free_line (dtp);
3092 return;
3093
3094
3095 nml_err_ret:
3096
3097 /* All namelist error calls return from here */
3098 free_saved (dtp);
3099 free_line (dtp);
3100 generate_error (&dtp->common, LIBERROR_READ_VALUE, nml_err_msg);
3101 return;
3102 }