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