re PR fortran/29563 (Internal read loses data.)
[gcc.git] / libgfortran / io / list_read.c
1 /* Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
3 Namelist input contributed by Paul Thomas
4
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6
7 Libgfortran is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
19 executable.)
20
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with Libgfortran; see the file COPYING. If not, write to
28 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA. */
30
31
32 #include "config.h"
33 #include <string.h>
34 #include <ctype.h>
35 #include "libgfortran.h"
36 #include "io.h"
37
38
39 /* List directed input. Several parsing subroutines are practically
40 reimplemented from formatted input, the reason being that there are
41 all kinds of small differences between formatted and list directed
42 parsing. */
43
44
45 /* Subroutines for reading characters from the input. Because a
46 repeat count is ambiguous with an integer, we have to read the
47 whole digit string before seeing if there is a '*' which signals
48 the repeat count. Since we can have a lot of potential leading
49 zeros, we have to be able to back up by arbitrary amount. Because
50 the input might not be seekable, we have to buffer the data
51 ourselves. */
52
53 #define CASE_DIGITS case '0': case '1': case '2': case '3': case '4': \
54 case '5': case '6': case '7': case '8': case '9'
55
56 #define CASE_SEPARATORS case ' ': case ',': case '/': case '\n': case '\t': \
57 case '\r'
58
59 /* This macro assumes that we're operating on a variable. */
60
61 #define is_separator(c) (c == '/' || c == ',' || c == '\n' || c == ' ' \
62 || c == '\t' || c == '\r')
63
64 /* Maximum repeat count. Less than ten times the maximum signed int32. */
65
66 #define MAX_REPEAT 200000000
67
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 if (dtp->u.p.scratch == NULL)
79 dtp->u.p.scratch = get_mem (SCRATCH_SIZE);
80 dtp->u.p.saved_string = dtp->u.p.scratch;
81 memset (dtp->u.p.saved_string, 0, SCRATCH_SIZE);
82 dtp->u.p.saved_length = SCRATCH_SIZE;
83 dtp->u.p.saved_used = 0;
84 }
85
86 if (dtp->u.p.saved_used >= dtp->u.p.saved_length)
87 {
88 dtp->u.p.saved_length = 2 * dtp->u.p.saved_length;
89 new = get_mem (2 * dtp->u.p.saved_length);
90
91 memset (new, 0, 2 * dtp->u.p.saved_length);
92
93 memcpy (new, dtp->u.p.saved_string, dtp->u.p.saved_used);
94 if (dtp->u.p.saved_string != dtp->u.p.scratch)
95 free_mem (dtp->u.p.saved_string);
96
97 dtp->u.p.saved_string = new;
98 }
99
100 dtp->u.p.saved_string[dtp->u.p.saved_used++] = c;
101 }
102
103
104 /* Free the input buffer if necessary. */
105
106 static void
107 free_saved (st_parameter_dt *dtp)
108 {
109 if (dtp->u.p.saved_string == NULL)
110 return;
111
112 if (dtp->u.p.saved_string != dtp->u.p.scratch)
113 free_mem (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 if (dtp->u.p.line_buffer == NULL)
126 return;
127
128 free_mem (dtp->u.p.line_buffer);
129 dtp->u.p.line_buffer = NULL;
130 }
131
132
133 static char
134 next_char (st_parameter_dt *dtp)
135 {
136 int length;
137 gfc_offset record;
138 char c, *p;
139
140 if (dtp->u.p.last_char != '\0')
141 {
142 dtp->u.p.at_eol = 0;
143 c = dtp->u.p.last_char;
144 dtp->u.p.last_char = '\0';
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 longjmp (*dtp->u.p.eof_jump, 1);
172
173 /* Check for "end-of-record" condition. */
174 if (dtp->u.p.current_unit->bytes_left == 0)
175 {
176 record = next_array_record (dtp, dtp->u.p.current_unit->ls);
177
178 /* Check for "end-of-file" condition. */
179 if (record == 0)
180 {
181 dtp->u.p.at_eof = 1;
182 c = '\n';
183 goto done;
184 }
185
186 record *= dtp->u.p.current_unit->recl;
187 if (sseek (dtp->u.p.current_unit->s, record) == FAILURE)
188 longjmp (*dtp->u.p.eof_jump, 1);
189
190 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
191 }
192 }
193
194 /* Get the next character and handle end-of-record conditions. */
195
196 length = 1;
197
198 p = salloc_r (dtp->u.p.current_unit->s, &length);
199
200 if (is_stream_io (dtp))
201 dtp->u.p.current_unit->strm_pos++;
202
203 if (is_internal_unit(dtp))
204 {
205 if (is_array_io(dtp))
206 {
207 /* End of record is handled in the next pass through, above. The
208 check for NULL here is cautionary. */
209 if (p == NULL)
210 {
211 generate_error (&dtp->common, ERROR_INTERNAL_UNIT, NULL);
212 return '\0';
213 }
214
215 dtp->u.p.current_unit->bytes_left--;
216 c = *p;
217 }
218 else
219 {
220 if (p == NULL)
221 longjmp (*dtp->u.p.eof_jump, 1);
222 if (length == 0)
223 c = '\n';
224 else
225 c = *p;
226 }
227 }
228 else
229 {
230 if (p == NULL)
231 {
232 generate_error (&dtp->common, ERROR_OS, NULL);
233 return '\0';
234 }
235 if (length == 0)
236 longjmp (*dtp->u.p.eof_jump, 1);
237 c = *p;
238 }
239 done:
240 dtp->u.p.at_eol = (c == '\n' || c == '\r');
241 return c;
242 }
243
244
245 /* Push a character back onto the input. */
246
247 static void
248 unget_char (st_parameter_dt *dtp, char c)
249 {
250 dtp->u.p.last_char = c;
251 }
252
253
254 /* Skip over spaces in the input. Returns the nonspace character that
255 terminated the eating and also places it back on the input. */
256
257 static char
258 eat_spaces (st_parameter_dt *dtp)
259 {
260 char c;
261
262 do
263 {
264 c = next_char (dtp);
265 }
266 while (c == ' ' || c == '\t');
267
268 unget_char (dtp, c);
269 return c;
270 }
271
272
273 /* Skip over a separator. Technically, we don't always eat the whole
274 separator. This is because if we've processed the last input item,
275 then a separator is unnecessary. Plus the fact that operating
276 systems usually deliver console input on a line basis.
277
278 The upshot is that if we see a newline as part of reading a
279 separator, we stop reading. If there are more input items, we
280 continue reading the separator with finish_separator() which takes
281 care of the fact that we may or may not have seen a comma as part
282 of the separator. */
283
284 static void
285 eat_separator (st_parameter_dt *dtp)
286 {
287 char c, n;
288
289 eat_spaces (dtp);
290 dtp->u.p.comma_flag = 0;
291
292 c = next_char (dtp);
293 switch (c)
294 {
295 case ',':
296 dtp->u.p.comma_flag = 1;
297 eat_spaces (dtp);
298 break;
299
300 case '/':
301 dtp->u.p.input_complete = 1;
302 break;
303
304 case '\r':
305 n = next_char(dtp);
306 if (n == '\n')
307 dtp->u.p.at_eol = 1;
308 else
309 unget_char (dtp, n);
310 break;
311
312 case '\n':
313 dtp->u.p.at_eol = 1;
314 break;
315
316 case '!':
317 if (dtp->u.p.namelist_mode)
318 { /* Eat a namelist comment. */
319 do
320 c = next_char (dtp);
321 while (c != '\n');
322
323 break;
324 }
325
326 /* Fall Through... */
327
328 default:
329 unget_char (dtp, c);
330 break;
331 }
332 }
333
334
335 /* Finish processing a separator that was interrupted by a newline.
336 If we're here, then another data item is present, so we finish what
337 we started on the previous line. */
338
339 static void
340 finish_separator (st_parameter_dt *dtp)
341 {
342 char c;
343
344 restart:
345 eat_spaces (dtp);
346
347 c = next_char (dtp);
348 switch (c)
349 {
350 case ',':
351 if (dtp->u.p.comma_flag)
352 unget_char (dtp, c);
353 else
354 {
355 c = eat_spaces (dtp);
356 if (c == '\n' || c == '\r')
357 goto restart;
358 }
359
360 break;
361
362 case '/':
363 dtp->u.p.input_complete = 1;
364 if (!dtp->u.p.namelist_mode) next_record (dtp, 0);
365 break;
366
367 case '\n':
368 case '\r':
369 goto restart;
370
371 case '!':
372 if (dtp->u.p.namelist_mode)
373 {
374 do
375 c = next_char (dtp);
376 while (c != '\n');
377
378 goto restart;
379 }
380
381 default:
382 unget_char (dtp, c);
383 break;
384 }
385 }
386
387
388 /* This function reads characters through to the end of the current line and
389 just ignores them. */
390
391 static void
392 eat_line (st_parameter_dt *dtp)
393 {
394 char c;
395 if (!is_internal_unit (dtp))
396 do
397 c = next_char (dtp);
398 while (c != '\n');
399 }
400
401
402 /* This function is needed to catch bad conversions so that namelist can
403 attempt to see if dtp->u.p.saved_string contains a new object name rather
404 than a bad value. */
405
406 static int
407 nml_bad_return (st_parameter_dt *dtp, char c)
408 {
409 if (dtp->u.p.namelist_mode)
410 {
411 dtp->u.p.nml_read_error = 1;
412 unget_char (dtp, c);
413 return 1;
414 }
415 return 0;
416 }
417
418 /* Convert an unsigned string to an integer. The length value is -1
419 if we are working on a repeat count. Returns nonzero if we have a
420 range problem. As a side effect, frees the dtp->u.p.saved_string. */
421
422 static int
423 convert_integer (st_parameter_dt *dtp, int length, int negative)
424 {
425 char c, *buffer, message[100];
426 int m;
427 GFC_INTEGER_LARGEST v, max, max10;
428
429 buffer = dtp->u.p.saved_string;
430 v = 0;
431
432 max = (length == -1) ? MAX_REPEAT : max_value (length, 1);
433 max10 = max / 10;
434
435 for (;;)
436 {
437 c = *buffer++;
438 if (c == '\0')
439 break;
440 c -= '0';
441
442 if (v > max10)
443 goto overflow;
444 v = 10 * v;
445
446 if (v > max - c)
447 goto overflow;
448 v += c;
449 }
450
451 m = 0;
452
453 if (length != -1)
454 {
455 if (negative)
456 v = -v;
457 set_integer (dtp->u.p.value, v, length);
458 }
459 else
460 {
461 dtp->u.p.repeat_count = v;
462
463 if (dtp->u.p.repeat_count == 0)
464 {
465 st_sprintf (message, "Zero repeat count in item %d of list input",
466 dtp->u.p.item_count);
467
468 generate_error (&dtp->common, ERROR_READ_VALUE, message);
469 m = 1;
470 }
471 }
472
473 free_saved (dtp);
474 return m;
475
476 overflow:
477 if (length == -1)
478 st_sprintf (message, "Repeat count overflow in item %d of list input",
479 dtp->u.p.item_count);
480 else
481 st_sprintf (message, "Integer overflow while reading item %d",
482 dtp->u.p.item_count);
483
484 free_saved (dtp);
485 generate_error (&dtp->common, ERROR_READ_VALUE, message);
486
487 return 1;
488 }
489
490
491 /* Parse a repeat count for logical and complex values which cannot
492 begin with a digit. Returns nonzero if we are done, zero if we
493 should continue on. */
494
495 static int
496 parse_repeat (st_parameter_dt *dtp)
497 {
498 char c, message[100];
499 int repeat;
500
501 c = next_char (dtp);
502 switch (c)
503 {
504 CASE_DIGITS:
505 repeat = c - '0';
506 break;
507
508 CASE_SEPARATORS:
509 unget_char (dtp, c);
510 eat_separator (dtp);
511 return 1;
512
513 default:
514 unget_char (dtp, c);
515 return 0;
516 }
517
518 for (;;)
519 {
520 c = next_char (dtp);
521 switch (c)
522 {
523 CASE_DIGITS:
524 repeat = 10 * repeat + c - '0';
525
526 if (repeat > MAX_REPEAT)
527 {
528 st_sprintf (message,
529 "Repeat count overflow in item %d of list input",
530 dtp->u.p.item_count);
531
532 generate_error (&dtp->common, ERROR_READ_VALUE, message);
533 return 1;
534 }
535
536 break;
537
538 case '*':
539 if (repeat == 0)
540 {
541 st_sprintf (message,
542 "Zero repeat count in item %d of list input",
543 dtp->u.p.item_count);
544
545 generate_error (&dtp->common, ERROR_READ_VALUE, message);
546 return 1;
547 }
548
549 goto done;
550
551 default:
552 goto bad_repeat;
553 }
554 }
555
556 done:
557 dtp->u.p.repeat_count = repeat;
558 return 0;
559
560 bad_repeat:
561
562 eat_line (dtp);
563 free_saved (dtp);
564 st_sprintf (message, "Bad repeat count in item %d of list input",
565 dtp->u.p.item_count);
566 generate_error (&dtp->common, ERROR_READ_VALUE, message);
567 return 1;
568 }
569
570
571 /* To read a logical we have to look ahead in the input stream to make sure
572 there is not an equal sign indicating a variable name. To do this we use
573 line_buffer to point to a temporary buffer, pushing characters there for
574 possible later reading. */
575
576 static void
577 l_push_char (st_parameter_dt *dtp, char c)
578 {
579 if (dtp->u.p.line_buffer == NULL)
580 {
581 dtp->u.p.line_buffer = get_mem (SCRATCH_SIZE);
582 memset (dtp->u.p.line_buffer, 0, SCRATCH_SIZE);
583 }
584
585 dtp->u.p.line_buffer[dtp->u.p.item_count++] = c;
586 }
587
588
589 /* Read a logical character on the input. */
590
591 static void
592 read_logical (st_parameter_dt *dtp, int length)
593 {
594 char c, message[100];
595 int i, v;
596
597 if (parse_repeat (dtp))
598 return;
599
600 c = tolower (next_char (dtp));
601 l_push_char (dtp, c);
602 switch (c)
603 {
604 case 't':
605 v = 1;
606 c = next_char (dtp);
607 l_push_char (dtp, c);
608
609 if (!is_separator(c))
610 goto possible_name;
611
612 unget_char (dtp, c);
613 break;
614 case 'f':
615 v = 0;
616 c = next_char (dtp);
617 l_push_char (dtp, c);
618
619 if (!is_separator(c))
620 goto possible_name;
621
622 unget_char (dtp, c);
623 break;
624 case '.':
625 c = tolower (next_char (dtp));
626 switch (c)
627 {
628 case 't':
629 v = 1;
630 break;
631 case 'f':
632 v = 0;
633 break;
634 default:
635 goto bad_logical;
636 }
637
638 break;
639
640 CASE_SEPARATORS:
641 unget_char (dtp, c);
642 eat_separator (dtp);
643 return; /* Null value. */
644
645 default:
646 goto bad_logical;
647 }
648
649 dtp->u.p.saved_type = BT_LOGICAL;
650 dtp->u.p.saved_length = length;
651
652 /* Eat trailing garbage. */
653 do
654 {
655 c = next_char (dtp);
656 }
657 while (!is_separator (c));
658
659 unget_char (dtp, c);
660 eat_separator (dtp);
661 dtp->u.p.item_count = 0;
662 dtp->u.p.line_buffer_enabled = 0;
663 set_integer ((int *) dtp->u.p.value, v, length);
664 free_line (dtp);
665
666 return;
667
668 possible_name:
669
670 for(i = 0; i < 63; i++)
671 {
672 c = next_char (dtp);
673 if (is_separator(c))
674 {
675 /* All done if this is not a namelist read. */
676 if (!dtp->u.p.namelist_mode)
677 goto logical_done;
678
679 unget_char (dtp, c);
680 eat_separator (dtp);
681 c = next_char (dtp);
682 if (c != '=')
683 {
684 unget_char (dtp, c);
685 goto logical_done;
686 }
687 }
688
689 l_push_char (dtp, c);
690 if (c == '=')
691 {
692 dtp->u.p.nml_read_error = 1;
693 dtp->u.p.line_buffer_enabled = 1;
694 dtp->u.p.item_count = 0;
695 return;
696 }
697
698 }
699
700 bad_logical:
701
702 free_line (dtp);
703
704 if (nml_bad_return (dtp, c))
705 return;
706
707 eat_line (dtp);
708 free_saved (dtp);
709 st_sprintf (message, "Bad logical value while reading item %d",
710 dtp->u.p.item_count);
711 generate_error (&dtp->common, ERROR_READ_VALUE, message);
712 return;
713
714 logical_done:
715
716 dtp->u.p.item_count = 0;
717 dtp->u.p.line_buffer_enabled = 0;
718 dtp->u.p.saved_type = BT_LOGICAL;
719 dtp->u.p.saved_length = length;
720 set_integer ((int *) dtp->u.p.value, v, length);
721 free_saved (dtp);
722 free_line (dtp);
723 }
724
725
726 /* Reading integers is tricky because we can actually be reading a
727 repeat count. We have to store the characters in a buffer because
728 we could be reading an integer that is larger than the default int
729 used for repeat counts. */
730
731 static void
732 read_integer (st_parameter_dt *dtp, int length)
733 {
734 char c, message[100];
735 int negative;
736
737 negative = 0;
738
739 c = next_char (dtp);
740 switch (c)
741 {
742 case '-':
743 negative = 1;
744 /* Fall through... */
745
746 case '+':
747 c = next_char (dtp);
748 goto get_integer;
749
750 CASE_SEPARATORS: /* Single null. */
751 unget_char (dtp, c);
752 eat_separator (dtp);
753 return;
754
755 CASE_DIGITS:
756 push_char (dtp, c);
757 break;
758
759 default:
760 goto bad_integer;
761 }
762
763 /* Take care of what may be a repeat count. */
764
765 for (;;)
766 {
767 c = next_char (dtp);
768 switch (c)
769 {
770 CASE_DIGITS:
771 push_char (dtp, c);
772 break;
773
774 case '*':
775 push_char (dtp, '\0');
776 goto repeat;
777
778 CASE_SEPARATORS: /* Not a repeat count. */
779 goto done;
780
781 default:
782 goto bad_integer;
783 }
784 }
785
786 repeat:
787 if (convert_integer (dtp, -1, 0))
788 return;
789
790 /* Get the real integer. */
791
792 c = next_char (dtp);
793 switch (c)
794 {
795 CASE_DIGITS:
796 break;
797
798 CASE_SEPARATORS:
799 unget_char (dtp, c);
800 eat_separator (dtp);
801 return;
802
803 case '-':
804 negative = 1;
805 /* Fall through... */
806
807 case '+':
808 c = next_char (dtp);
809 break;
810 }
811
812 get_integer:
813 if (!isdigit (c))
814 goto bad_integer;
815 push_char (dtp, c);
816
817 for (;;)
818 {
819 c = next_char (dtp);
820 switch (c)
821 {
822 CASE_DIGITS:
823 push_char (dtp, c);
824 break;
825
826 CASE_SEPARATORS:
827 goto done;
828
829 default:
830 goto bad_integer;
831 }
832 }
833
834 bad_integer:
835
836 if (nml_bad_return (dtp, c))
837 return;
838
839 eat_line (dtp);
840 free_saved (dtp);
841 st_sprintf (message, "Bad integer for item %d in list input",
842 dtp->u.p.item_count);
843 generate_error (&dtp->common, ERROR_READ_VALUE, message);
844
845 return;
846
847 done:
848 unget_char (dtp, c);
849 eat_separator (dtp);
850
851 push_char (dtp, '\0');
852 if (convert_integer (dtp, length, negative))
853 {
854 free_saved (dtp);
855 return;
856 }
857
858 free_saved (dtp);
859 dtp->u.p.saved_type = BT_INTEGER;
860 }
861
862
863 /* Read a character variable. */
864
865 static void
866 read_character (st_parameter_dt *dtp, int length __attribute__ ((unused)))
867 {
868 char c, quote, message[100];
869
870 quote = ' '; /* Space means no quote character. */
871
872 c = next_char (dtp);
873 switch (c)
874 {
875 CASE_DIGITS:
876 push_char (dtp, c);
877 break;
878
879 CASE_SEPARATORS:
880 unget_char (dtp, c); /* NULL value. */
881 eat_separator (dtp);
882 return;
883
884 case '"':
885 case '\'':
886 quote = c;
887 goto get_string;
888
889 default:
890 if (dtp->u.p.namelist_mode)
891 {
892 unget_char (dtp,c);
893 return;
894 }
895 push_char (dtp, c);
896 goto get_string;
897 }
898
899 /* Deal with a possible repeat count. */
900
901 for (;;)
902 {
903 c = next_char (dtp);
904 switch (c)
905 {
906 CASE_DIGITS:
907 push_char (dtp, c);
908 break;
909
910 CASE_SEPARATORS:
911 unget_char (dtp, c);
912 goto done; /* String was only digits! */
913
914 case '*':
915 push_char (dtp, '\0');
916 goto got_repeat;
917
918 default:
919 push_char (dtp, c);
920 goto get_string; /* Not a repeat count after all. */
921 }
922 }
923
924 got_repeat:
925 if (convert_integer (dtp, -1, 0))
926 return;
927
928 /* Now get the real string. */
929
930 c = next_char (dtp);
931 switch (c)
932 {
933 CASE_SEPARATORS:
934 unget_char (dtp, c); /* Repeated NULL values. */
935 eat_separator (dtp);
936 return;
937
938 case '"':
939 case '\'':
940 quote = c;
941 break;
942
943 default:
944 push_char (dtp, c);
945 break;
946 }
947
948 get_string:
949 for (;;)
950 {
951 c = next_char (dtp);
952 switch (c)
953 {
954 case '"':
955 case '\'':
956 if (c != quote)
957 {
958 push_char (dtp, c);
959 break;
960 }
961
962 /* See if we have a doubled quote character or the end of
963 the string. */
964
965 c = next_char (dtp);
966 if (c == quote)
967 {
968 push_char (dtp, quote);
969 break;
970 }
971
972 unget_char (dtp, c);
973 goto done;
974
975 CASE_SEPARATORS:
976 if (quote == ' ')
977 {
978 unget_char (dtp, c);
979 goto done;
980 }
981
982 if (c != '\n' && c != '\r')
983 push_char (dtp, c);
984 break;
985
986 default:
987 push_char (dtp, c);
988 break;
989 }
990 }
991
992 /* At this point, we have to have a separator, or else the string is
993 invalid. */
994 done:
995 c = next_char (dtp);
996 if (is_separator (c))
997 {
998 unget_char (dtp, c);
999 eat_separator (dtp);
1000 dtp->u.p.saved_type = BT_CHARACTER;
1001 }
1002 else
1003 {
1004 free_saved (dtp);
1005 st_sprintf (message, "Invalid string input in item %d",
1006 dtp->u.p.item_count);
1007 generate_error (&dtp->common, ERROR_READ_VALUE, message);
1008 }
1009 }
1010
1011
1012 /* Parse a component of a complex constant or a real number that we
1013 are sure is already there. This is a straight real number parser. */
1014
1015 static int
1016 parse_real (st_parameter_dt *dtp, void *buffer, int length)
1017 {
1018 char c, message[100];
1019 int m, seen_dp;
1020
1021 c = next_char (dtp);
1022 if (c == '-' || c == '+')
1023 {
1024 push_char (dtp, c);
1025 c = next_char (dtp);
1026 }
1027
1028 if (!isdigit (c) && c != '.')
1029 goto bad;
1030
1031 push_char (dtp, c);
1032
1033 seen_dp = (c == '.') ? 1 : 0;
1034
1035 for (;;)
1036 {
1037 c = next_char (dtp);
1038 switch (c)
1039 {
1040 CASE_DIGITS:
1041 push_char (dtp, c);
1042 break;
1043
1044 case '.':
1045 if (seen_dp)
1046 goto bad;
1047
1048 seen_dp = 1;
1049 push_char (dtp, c);
1050 break;
1051
1052 case 'e':
1053 case 'E':
1054 case 'd':
1055 case 'D':
1056 push_char (dtp, 'e');
1057 goto exp1;
1058
1059 case '-':
1060 case '+':
1061 push_char (dtp, 'e');
1062 push_char (dtp, c);
1063 c = next_char (dtp);
1064 goto exp2;
1065
1066 CASE_SEPARATORS:
1067 unget_char (dtp, c);
1068 goto done;
1069
1070 default:
1071 goto done;
1072 }
1073 }
1074
1075 exp1:
1076 c = next_char (dtp);
1077 if (c != '-' && c != '+')
1078 push_char (dtp, '+');
1079 else
1080 {
1081 push_char (dtp, c);
1082 c = next_char (dtp);
1083 }
1084
1085 exp2:
1086 if (!isdigit (c))
1087 goto bad;
1088 push_char (dtp, c);
1089
1090 for (;;)
1091 {
1092 c = next_char (dtp);
1093 switch (c)
1094 {
1095 CASE_DIGITS:
1096 push_char (dtp, c);
1097 break;
1098
1099 CASE_SEPARATORS:
1100 unget_char (dtp, c);
1101 goto done;
1102
1103 default:
1104 goto done;
1105 }
1106 }
1107
1108 done:
1109 unget_char (dtp, c);
1110 push_char (dtp, '\0');
1111
1112 m = convert_real (dtp, buffer, dtp->u.p.saved_string, length);
1113 free_saved (dtp);
1114
1115 return m;
1116
1117 bad:
1118
1119 if (nml_bad_return (dtp, c))
1120 return 0;
1121
1122 eat_line (dtp);
1123 free_saved (dtp);
1124 st_sprintf (message, "Bad floating point number for item %d",
1125 dtp->u.p.item_count);
1126 generate_error (&dtp->common, ERROR_READ_VALUE, message);
1127
1128 return 1;
1129 }
1130
1131
1132 /* Reading a complex number is straightforward because we can tell
1133 what it is right away. */
1134
1135 static void
1136 read_complex (st_parameter_dt *dtp, int kind, size_t size)
1137 {
1138 char message[100];
1139 char c;
1140
1141 if (parse_repeat (dtp))
1142 return;
1143
1144 c = next_char (dtp);
1145 switch (c)
1146 {
1147 case '(':
1148 break;
1149
1150 CASE_SEPARATORS:
1151 unget_char (dtp, c);
1152 eat_separator (dtp);
1153 return;
1154
1155 default:
1156 goto bad_complex;
1157 }
1158
1159 eat_spaces (dtp);
1160 if (parse_real (dtp, dtp->u.p.value, kind))
1161 return;
1162
1163 eol_1:
1164 eat_spaces (dtp);
1165 c = next_char (dtp);
1166 if (c == '\n' || c== '\r')
1167 goto eol_1;
1168 else
1169 unget_char (dtp, c);
1170
1171 if (next_char (dtp) != ',')
1172 goto bad_complex;
1173
1174 eol_2:
1175 eat_spaces (dtp);
1176 c = next_char (dtp);
1177 if (c == '\n' || c== '\r')
1178 goto eol_2;
1179 else
1180 unget_char (dtp, c);
1181
1182 if (parse_real (dtp, dtp->u.p.value + size / 2, kind))
1183 return;
1184
1185 eat_spaces (dtp);
1186 if (next_char (dtp) != ')')
1187 goto bad_complex;
1188
1189 c = next_char (dtp);
1190 if (!is_separator (c))
1191 goto bad_complex;
1192
1193 unget_char (dtp, c);
1194 eat_separator (dtp);
1195
1196 free_saved (dtp);
1197 dtp->u.p.saved_type = BT_COMPLEX;
1198 return;
1199
1200 bad_complex:
1201
1202 if (nml_bad_return (dtp, c))
1203 return;
1204
1205 eat_line (dtp);
1206 free_saved (dtp);
1207 st_sprintf (message, "Bad complex value in item %d of list input",
1208 dtp->u.p.item_count);
1209 generate_error (&dtp->common, ERROR_READ_VALUE, message);
1210 }
1211
1212
1213 /* Parse a real number with a possible repeat count. */
1214
1215 static void
1216 read_real (st_parameter_dt *dtp, int length)
1217 {
1218 char c, message[100];
1219 int seen_dp;
1220
1221 seen_dp = 0;
1222
1223 c = next_char (dtp);
1224 switch (c)
1225 {
1226 CASE_DIGITS:
1227 push_char (dtp, c);
1228 break;
1229
1230 case '.':
1231 push_char (dtp, c);
1232 seen_dp = 1;
1233 break;
1234
1235 case '+':
1236 case '-':
1237 goto got_sign;
1238
1239 CASE_SEPARATORS:
1240 unget_char (dtp, c); /* Single null. */
1241 eat_separator (dtp);
1242 return;
1243
1244 default:
1245 goto bad_real;
1246 }
1247
1248 /* Get the digit string that might be a repeat count. */
1249
1250 for (;;)
1251 {
1252 c = next_char (dtp);
1253 switch (c)
1254 {
1255 CASE_DIGITS:
1256 push_char (dtp, c);
1257 break;
1258
1259 case '.':
1260 if (seen_dp)
1261 goto bad_real;
1262
1263 seen_dp = 1;
1264 push_char (dtp, c);
1265 goto real_loop;
1266
1267 case 'E':
1268 case 'e':
1269 case 'D':
1270 case 'd':
1271 goto exp1;
1272
1273 case '+':
1274 case '-':
1275 push_char (dtp, 'e');
1276 push_char (dtp, c);
1277 c = next_char (dtp);
1278 goto exp2;
1279
1280 case '*':
1281 push_char (dtp, '\0');
1282 goto got_repeat;
1283
1284 CASE_SEPARATORS:
1285 if (c != '\n' && c != ',' && c != '\r')
1286 unget_char (dtp, c);
1287 goto done;
1288
1289 default:
1290 goto bad_real;
1291 }
1292 }
1293
1294 got_repeat:
1295 if (convert_integer (dtp, -1, 0))
1296 return;
1297
1298 /* Now get the number itself. */
1299
1300 c = next_char (dtp);
1301 if (is_separator (c))
1302 { /* Repeated null value. */
1303 unget_char (dtp, c);
1304 eat_separator (dtp);
1305 return;
1306 }
1307
1308 if (c != '-' && c != '+')
1309 push_char (dtp, '+');
1310 else
1311 {
1312 got_sign:
1313 push_char (dtp, c);
1314 c = next_char (dtp);
1315 }
1316
1317 if (!isdigit (c) && c != '.')
1318 goto bad_real;
1319
1320 if (c == '.')
1321 {
1322 if (seen_dp)
1323 goto bad_real;
1324 else
1325 seen_dp = 1;
1326 }
1327
1328 push_char (dtp, c);
1329
1330 real_loop:
1331 for (;;)
1332 {
1333 c = next_char (dtp);
1334 switch (c)
1335 {
1336 CASE_DIGITS:
1337 push_char (dtp, c);
1338 break;
1339
1340 CASE_SEPARATORS:
1341 goto done;
1342
1343 case '.':
1344 if (seen_dp)
1345 goto bad_real;
1346
1347 seen_dp = 1;
1348 push_char (dtp, c);
1349 break;
1350
1351 case 'E':
1352 case 'e':
1353 case 'D':
1354 case 'd':
1355 goto exp1;
1356
1357 case '+':
1358 case '-':
1359 push_char (dtp, 'e');
1360 push_char (dtp, c);
1361 c = next_char (dtp);
1362 goto exp2;
1363
1364 default:
1365 goto bad_real;
1366 }
1367 }
1368
1369 exp1:
1370 push_char (dtp, 'e');
1371
1372 c = next_char (dtp);
1373 if (c != '+' && c != '-')
1374 push_char (dtp, '+');
1375 else
1376 {
1377 push_char (dtp, c);
1378 c = next_char (dtp);
1379 }
1380
1381 exp2:
1382 if (!isdigit (c))
1383 goto bad_real;
1384 push_char (dtp, c);
1385
1386 for (;;)
1387 {
1388 c = next_char (dtp);
1389
1390 switch (c)
1391 {
1392 CASE_DIGITS:
1393 push_char (dtp, c);
1394 break;
1395
1396 CASE_SEPARATORS:
1397 goto done;
1398
1399 default:
1400 goto bad_real;
1401 }
1402 }
1403
1404 done:
1405 unget_char (dtp, c);
1406 eat_separator (dtp);
1407 push_char (dtp, '\0');
1408 if (convert_real (dtp, dtp->u.p.value, dtp->u.p.saved_string, length))
1409 return;
1410
1411 free_saved (dtp);
1412 dtp->u.p.saved_type = BT_REAL;
1413 return;
1414
1415 bad_real:
1416
1417 if (nml_bad_return (dtp, c))
1418 return;
1419
1420 eat_line (dtp);
1421 free_saved (dtp);
1422 st_sprintf (message, "Bad real number in item %d of list input",
1423 dtp->u.p.item_count);
1424 generate_error (&dtp->common, ERROR_READ_VALUE, message);
1425 }
1426
1427
1428 /* Check the current type against the saved type to make sure they are
1429 compatible. Returns nonzero if incompatible. */
1430
1431 static int
1432 check_type (st_parameter_dt *dtp, bt type, int len)
1433 {
1434 char message[100];
1435
1436 if (dtp->u.p.saved_type != BT_NULL && dtp->u.p.saved_type != type)
1437 {
1438 st_sprintf (message, "Read type %s where %s was expected for item %d",
1439 type_name (dtp->u.p.saved_type), type_name (type),
1440 dtp->u.p.item_count);
1441
1442 generate_error (&dtp->common, ERROR_READ_VALUE, message);
1443 return 1;
1444 }
1445
1446 if (dtp->u.p.saved_type == BT_NULL || dtp->u.p.saved_type == BT_CHARACTER)
1447 return 0;
1448
1449 if (dtp->u.p.saved_length != len)
1450 {
1451 st_sprintf (message,
1452 "Read kind %d %s where kind %d is required for item %d",
1453 dtp->u.p.saved_length, type_name (dtp->u.p.saved_type), len,
1454 dtp->u.p.item_count);
1455 generate_error (&dtp->common, ERROR_READ_VALUE, message);
1456 return 1;
1457 }
1458
1459 return 0;
1460 }
1461
1462
1463 /* Top level data transfer subroutine for list reads. Because we have
1464 to deal with repeat counts, the data item is always saved after
1465 reading, usually in the dtp->u.p.value[] array. If a repeat count is
1466 greater than one, we copy the data item multiple times. */
1467
1468 static void
1469 list_formatted_read_scalar (st_parameter_dt *dtp, bt type, void *p, int kind,
1470 size_t size)
1471 {
1472 char c;
1473 int m;
1474 jmp_buf eof_jump;
1475
1476 dtp->u.p.namelist_mode = 0;
1477
1478 dtp->u.p.eof_jump = &eof_jump;
1479 if (setjmp (eof_jump))
1480 {
1481 generate_error (&dtp->common, ERROR_END, NULL);
1482 goto cleanup;
1483 }
1484
1485 if (dtp->u.p.first_item)
1486 {
1487 dtp->u.p.first_item = 0;
1488 dtp->u.p.input_complete = 0;
1489 dtp->u.p.repeat_count = 1;
1490 dtp->u.p.at_eol = 0;
1491
1492 c = eat_spaces (dtp);
1493 if (is_separator (c))
1494 { /* Found a null value. */
1495 eat_separator (dtp);
1496 dtp->u.p.repeat_count = 0;
1497
1498 /* eat_separator sets this flag if the separator was a comma */
1499 if (dtp->u.p.comma_flag)
1500 goto cleanup;
1501
1502 /* eat_separator sets this flag if the separator was a \n or \r */
1503 if (dtp->u.p.at_eol)
1504 finish_separator (dtp);
1505 else
1506 goto cleanup;
1507 }
1508
1509 }
1510 else
1511 {
1512 if (dtp->u.p.input_complete)
1513 goto cleanup;
1514
1515 if (dtp->u.p.repeat_count > 0)
1516 {
1517 if (check_type (dtp, type, kind))
1518 return;
1519 goto set_value;
1520 }
1521
1522 if (dtp->u.p.at_eol)
1523 finish_separator (dtp);
1524 else
1525 {
1526 eat_spaces (dtp);
1527 /* trailing spaces prior to end of line */
1528 if (dtp->u.p.at_eol)
1529 finish_separator (dtp);
1530 }
1531
1532 dtp->u.p.saved_type = BT_NULL;
1533 dtp->u.p.repeat_count = 1;
1534 }
1535
1536 switch (type)
1537 {
1538 case BT_INTEGER:
1539 read_integer (dtp, kind);
1540 break;
1541 case BT_LOGICAL:
1542 read_logical (dtp, kind);
1543 break;
1544 case BT_CHARACTER:
1545 read_character (dtp, kind);
1546 break;
1547 case BT_REAL:
1548 read_real (dtp, kind);
1549 break;
1550 case BT_COMPLEX:
1551 read_complex (dtp, kind, size);
1552 break;
1553 default:
1554 internal_error (&dtp->common, "Bad type for list read");
1555 }
1556
1557 if (dtp->u.p.saved_type != BT_CHARACTER && dtp->u.p.saved_type != BT_NULL)
1558 dtp->u.p.saved_length = size;
1559
1560 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1561 goto cleanup;
1562
1563 set_value:
1564 switch (dtp->u.p.saved_type)
1565 {
1566 case BT_COMPLEX:
1567 case BT_INTEGER:
1568 case BT_REAL:
1569 case BT_LOGICAL:
1570 memcpy (p, dtp->u.p.value, size);
1571 break;
1572
1573 case BT_CHARACTER:
1574 if (dtp->u.p.saved_string)
1575 {
1576 m = ((int) size < dtp->u.p.saved_used)
1577 ? (int) size : dtp->u.p.saved_used;
1578 memcpy (p, dtp->u.p.saved_string, m);
1579 }
1580 else
1581 /* Just delimiters encountered, nothing to copy but SPACE. */
1582 m = 0;
1583
1584 if (m < (int) size)
1585 memset (((char *) p) + m, ' ', size - m);
1586 break;
1587
1588 case BT_NULL:
1589 break;
1590 }
1591
1592 if (--dtp->u.p.repeat_count <= 0)
1593 free_saved (dtp);
1594
1595 cleanup:
1596 dtp->u.p.eof_jump = NULL;
1597 }
1598
1599
1600 void
1601 list_formatted_read (st_parameter_dt *dtp, bt type, void *p, int kind,
1602 size_t size, size_t nelems)
1603 {
1604 size_t elem;
1605 char *tmp;
1606
1607 tmp = (char *) p;
1608
1609 /* Big loop over all the elements. */
1610 for (elem = 0; elem < nelems; elem++)
1611 {
1612 dtp->u.p.item_count++;
1613 list_formatted_read_scalar (dtp, type, tmp + size*elem, kind, size);
1614 }
1615 }
1616
1617
1618 /* Finish a list read. */
1619
1620 void
1621 finish_list_read (st_parameter_dt *dtp)
1622 {
1623 char c;
1624
1625 free_saved (dtp);
1626
1627 if (dtp->u.p.at_eol)
1628 {
1629 dtp->u.p.at_eol = 0;
1630 return;
1631 }
1632
1633 do
1634 {
1635 c = next_char (dtp);
1636 }
1637 while (c != '\n');
1638 }
1639
1640 /* NAMELIST INPUT
1641
1642 void namelist_read (st_parameter_dt *dtp)
1643 calls:
1644 static void nml_match_name (char *name, int len)
1645 static int nml_query (st_parameter_dt *dtp)
1646 static int nml_get_obj_data (st_parameter_dt *dtp,
1647 namelist_info **prev_nl, char *)
1648 calls:
1649 static void nml_untouch_nodes (st_parameter_dt *dtp)
1650 static namelist_info * find_nml_node (st_parameter_dt *dtp,
1651 char * var_name)
1652 static int nml_parse_qualifier(descriptor_dimension * ad,
1653 array_loop_spec * ls, int rank, char *)
1654 static void nml_touch_nodes (namelist_info * nl)
1655 static int nml_read_obj (namelist_info *nl, index_type offset,
1656 namelist_info **prev_nl, char *,
1657 index_type clow, index_type chigh)
1658 calls:
1659 -itself- */
1660
1661 /* Inputs a rank-dimensional qualifier, which can contain
1662 singlets, doublets, triplets or ':' with the standard meanings. */
1663
1664 static try
1665 nml_parse_qualifier (st_parameter_dt *dtp, descriptor_dimension *ad,
1666 array_loop_spec *ls, int rank, char *parse_err_msg)
1667 {
1668 int dim;
1669 int indx;
1670 int neg;
1671 int null_flag;
1672 int is_array_section;
1673 char c;
1674
1675 is_array_section = 0;
1676 dtp->u.p.expanded_read = 0;
1677
1678 /* The next character in the stream should be the '('. */
1679
1680 c = next_char (dtp);
1681
1682 /* Process the qualifier, by dimension and triplet. */
1683
1684 for (dim=0; dim < rank; dim++ )
1685 {
1686 for (indx=0; indx<3; indx++)
1687 {
1688 free_saved (dtp);
1689 eat_spaces (dtp);
1690 neg = 0;
1691
1692 /* Process a potential sign. */
1693 c = next_char (dtp);
1694 switch (c)
1695 {
1696 case '-':
1697 neg = 1;
1698 break;
1699
1700 case '+':
1701 break;
1702
1703 default:
1704 unget_char (dtp, c);
1705 break;
1706 }
1707
1708 /* Process characters up to the next ':' , ',' or ')'. */
1709 for (;;)
1710 {
1711 c = next_char (dtp);
1712
1713 switch (c)
1714 {
1715 case ':':
1716 is_array_section = 1;
1717 break;
1718
1719 case ',': case ')':
1720 if ((c==',' && dim == rank -1)
1721 || (c==')' && dim < rank -1))
1722 {
1723 st_sprintf (parse_err_msg,
1724 "Bad number of index fields");
1725 goto err_ret;
1726 }
1727 break;
1728
1729 CASE_DIGITS:
1730 push_char (dtp, c);
1731 continue;
1732
1733 case ' ': case '\t':
1734 eat_spaces (dtp);
1735 c = next_char (dtp);
1736 break;
1737
1738 default:
1739 st_sprintf (parse_err_msg, "Bad character in index");
1740 goto err_ret;
1741 }
1742
1743 if ((c == ',' || c == ')') && indx == 0
1744 && dtp->u.p.saved_string == 0)
1745 {
1746 st_sprintf (parse_err_msg, "Null index field");
1747 goto err_ret;
1748 }
1749
1750 if ((c == ':' && indx == 1 && dtp->u.p.saved_string == 0)
1751 || (indx == 2 && dtp->u.p.saved_string == 0))
1752 {
1753 st_sprintf(parse_err_msg, "Bad index triplet");
1754 goto err_ret;
1755 }
1756
1757 /* If '( : ? )' or '( ? : )' break and flag read failure. */
1758 null_flag = 0;
1759 if ((c == ':' && indx == 0 && dtp->u.p.saved_string == 0)
1760 || (indx==1 && dtp->u.p.saved_string == 0))
1761 {
1762 null_flag = 1;
1763 break;
1764 }
1765
1766 /* Now read the index. */
1767 if (convert_integer (dtp, sizeof(ssize_t), neg))
1768 {
1769 st_sprintf (parse_err_msg, "Bad integer in index");
1770 goto err_ret;
1771 }
1772 break;
1773 }
1774
1775 /* Feed the index values to the triplet arrays. */
1776 if (!null_flag)
1777 {
1778 if (indx == 0)
1779 memcpy (&ls[dim].start, dtp->u.p.value, sizeof(ssize_t));
1780 if (indx == 1)
1781 memcpy (&ls[dim].end, dtp->u.p.value, sizeof(ssize_t));
1782 if (indx == 2)
1783 memcpy (&ls[dim].step, dtp->u.p.value, sizeof(ssize_t));
1784 }
1785
1786 /* Singlet or doublet indices. */
1787 if (c==',' || c==')')
1788 {
1789 if (indx == 0)
1790 {
1791 memcpy (&ls[dim].start, dtp->u.p.value, sizeof(ssize_t));
1792
1793 /* If -std=f95/2003 or an array section is specified,
1794 do not allow excess data to be processed. */
1795 if (is_array_section == 1
1796 || compile_options.allow_std < GFC_STD_GNU)
1797 ls[dim].end = ls[dim].start;
1798 else
1799 dtp->u.p.expanded_read = 1;
1800 }
1801 break;
1802 }
1803 }
1804
1805 /* Check the values of the triplet indices. */
1806 if ((ls[dim].start > (ssize_t)ad[dim].ubound)
1807 || (ls[dim].start < (ssize_t)ad[dim].lbound)
1808 || (ls[dim].end > (ssize_t)ad[dim].ubound)
1809 || (ls[dim].end < (ssize_t)ad[dim].lbound))
1810 {
1811 st_sprintf (parse_err_msg, "Index %d out of range", dim + 1);
1812 goto err_ret;
1813 }
1814 if (((ls[dim].end - ls[dim].start ) * ls[dim].step < 0)
1815 || (ls[dim].step == 0))
1816 {
1817 st_sprintf (parse_err_msg, "Bad range in index %d", dim + 1);
1818 goto err_ret;
1819 }
1820
1821 /* Initialise the loop index counter. */
1822 ls[dim].idx = ls[dim].start;
1823 }
1824 eat_spaces (dtp);
1825 return SUCCESS;
1826
1827 err_ret:
1828
1829 return FAILURE;
1830 }
1831
1832 static namelist_info *
1833 find_nml_node (st_parameter_dt *dtp, char * var_name)
1834 {
1835 namelist_info * t = dtp->u.p.ionml;
1836 while (t != NULL)
1837 {
1838 if (strcmp (var_name, t->var_name) == 0)
1839 {
1840 t->touched = 1;
1841 return t;
1842 }
1843 t = t->next;
1844 }
1845 return NULL;
1846 }
1847
1848 /* Visits all the components of a derived type that have
1849 not explicitly been identified in the namelist input.
1850 touched is set and the loop specification initialised
1851 to default values */
1852
1853 static void
1854 nml_touch_nodes (namelist_info * nl)
1855 {
1856 index_type len = strlen (nl->var_name) + 1;
1857 int dim;
1858 char * ext_name = (char*)get_mem (len + 1);
1859 strcpy (ext_name, nl->var_name);
1860 strcat (ext_name, "%");
1861 for (nl = nl->next; nl; nl = nl->next)
1862 {
1863 if (strncmp (nl->var_name, ext_name, len) == 0)
1864 {
1865 nl->touched = 1;
1866 for (dim=0; dim < nl->var_rank; dim++)
1867 {
1868 nl->ls[dim].step = 1;
1869 nl->ls[dim].end = nl->dim[dim].ubound;
1870 nl->ls[dim].start = nl->dim[dim].lbound;
1871 nl->ls[dim].idx = nl->ls[dim].start;
1872 }
1873 }
1874 else
1875 break;
1876 }
1877 free_mem (ext_name);
1878 return;
1879 }
1880
1881 /* Resets touched for the entire list of nml_nodes, ready for a
1882 new object. */
1883
1884 static void
1885 nml_untouch_nodes (st_parameter_dt *dtp)
1886 {
1887 namelist_info * t;
1888 for (t = dtp->u.p.ionml; t; t = t->next)
1889 t->touched = 0;
1890 return;
1891 }
1892
1893 /* Attempts to input name to namelist name. Returns
1894 dtp->u.p.nml_read_error = 1 on no match. */
1895
1896 static void
1897 nml_match_name (st_parameter_dt *dtp, const char *name, index_type len)
1898 {
1899 index_type i;
1900 char c;
1901 dtp->u.p.nml_read_error = 0;
1902 for (i = 0; i < len; i++)
1903 {
1904 c = next_char (dtp);
1905 if (tolower (c) != tolower (name[i]))
1906 {
1907 dtp->u.p.nml_read_error = 1;
1908 break;
1909 }
1910 }
1911 }
1912
1913 /* If the namelist read is from stdin, output the current state of the
1914 namelist to stdout. This is used to implement the non-standard query
1915 features, ? and =?. If c == '=' the full namelist is printed. Otherwise
1916 the names alone are printed. */
1917
1918 static void
1919 nml_query (st_parameter_dt *dtp, char c)
1920 {
1921 gfc_unit * temp_unit;
1922 namelist_info * nl;
1923 index_type len;
1924 char * p;
1925
1926 if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
1927 return;
1928
1929 /* Store the current unit and transfer to stdout. */
1930
1931 temp_unit = dtp->u.p.current_unit;
1932 dtp->u.p.current_unit = find_unit (options.stdout_unit);
1933
1934 if (dtp->u.p.current_unit)
1935 {
1936 dtp->u.p.mode = WRITING;
1937 next_record (dtp, 0);
1938
1939 /* Write the namelist in its entirety. */
1940
1941 if (c == '=')
1942 namelist_write (dtp);
1943
1944 /* Or write the list of names. */
1945
1946 else
1947 {
1948
1949 /* "&namelist_name\n" */
1950
1951 len = dtp->namelist_name_len;
1952 #ifdef HAVE_CRLF
1953 p = write_block (dtp, len + 3);
1954 #else
1955 p = write_block (dtp, len + 2);
1956 #endif
1957 if (!p)
1958 goto query_return;
1959 memcpy (p, "&", 1);
1960 memcpy ((char*)(p + 1), dtp->namelist_name, len);
1961 #ifdef HAVE_CRLF
1962 memcpy ((char*)(p + len + 1), "\r\n", 2);
1963 #else
1964 memcpy ((char*)(p + len + 1), "\n", 1);
1965 #endif
1966 for (nl = dtp->u.p.ionml; nl; nl = nl->next)
1967 {
1968
1969 /* " var_name\n" */
1970
1971 len = strlen (nl->var_name);
1972 #ifdef HAVE_CRLF
1973 p = write_block (dtp, len + 3);
1974 #else
1975 p = write_block (dtp, len + 2);
1976 #endif
1977 if (!p)
1978 goto query_return;
1979 memcpy (p, " ", 1);
1980 memcpy ((char*)(p + 1), nl->var_name, len);
1981 #ifdef HAVE_CRLF
1982 memcpy ((char*)(p + len + 1), "\r\n", 2);
1983 #else
1984 memcpy ((char*)(p + len + 1), "\n", 1);
1985 #endif
1986 }
1987
1988 /* "&end\n" */
1989
1990 #ifdef HAVE_CRLF
1991 p = write_block (dtp, 6);
1992 #else
1993 p = write_block (dtp, 5);
1994 #endif
1995 if (!p)
1996 goto query_return;
1997 #ifdef HAVE_CRLF
1998 memcpy (p, "&end\r\n", 6);
1999 #else
2000 memcpy (p, "&end\n", 5);
2001 #endif
2002 }
2003
2004 /* Flush the stream to force immediate output. */
2005
2006 flush (dtp->u.p.current_unit->s);
2007 unlock_unit (dtp->u.p.current_unit);
2008 }
2009
2010 query_return:
2011
2012 /* Restore the current unit. */
2013
2014 dtp->u.p.current_unit = temp_unit;
2015 dtp->u.p.mode = READING;
2016 return;
2017 }
2018
2019 /* Reads and stores the input for the namelist object nl. For an array,
2020 the function loops over the ranges defined by the loop specification.
2021 This default to all the data or to the specification from a qualifier.
2022 nml_read_obj recursively calls itself to read derived types. It visits
2023 all its own components but only reads data for those that were touched
2024 when the name was parsed. If a read error is encountered, an attempt is
2025 made to return to read a new object name because the standard allows too
2026 little data to be available. On the other hand, too much data is an
2027 error. */
2028
2029 static try
2030 nml_read_obj (st_parameter_dt *dtp, namelist_info * nl, index_type offset,
2031 namelist_info **pprev_nl, char *nml_err_msg,
2032 index_type clow, index_type chigh)
2033 {
2034
2035 namelist_info * cmp;
2036 char * obj_name;
2037 int nml_carry;
2038 int len;
2039 int dim;
2040 index_type dlen;
2041 index_type m;
2042 index_type obj_name_len;
2043 void * pdata ;
2044
2045 /* This object not touched in name parsing. */
2046
2047 if (!nl->touched)
2048 return SUCCESS;
2049
2050 dtp->u.p.repeat_count = 0;
2051 eat_spaces (dtp);
2052
2053 len = nl->len;
2054 switch (nl->type)
2055 {
2056
2057 case GFC_DTYPE_INTEGER:
2058 case GFC_DTYPE_LOGICAL:
2059 dlen = len;
2060 break;
2061
2062 case GFC_DTYPE_REAL:
2063 dlen = size_from_real_kind (len);
2064 break;
2065
2066 case GFC_DTYPE_COMPLEX:
2067 dlen = size_from_complex_kind (len);
2068 break;
2069
2070 case GFC_DTYPE_CHARACTER:
2071 dlen = chigh ? (chigh - clow + 1) : nl->string_length;
2072 break;
2073
2074 default:
2075 dlen = 0;
2076 }
2077
2078 do
2079 {
2080
2081 /* Update the pointer to the data, using the current index vector */
2082
2083 pdata = (void*)(nl->mem_pos + offset);
2084 for (dim = 0; dim < nl->var_rank; dim++)
2085 pdata = (void*)(pdata + (nl->ls[dim].idx - nl->dim[dim].lbound) *
2086 nl->dim[dim].stride * nl->size);
2087
2088 /* Reset the error flag and try to read next value, if
2089 dtp->u.p.repeat_count=0 */
2090
2091 dtp->u.p.nml_read_error = 0;
2092 nml_carry = 0;
2093 if (--dtp->u.p.repeat_count <= 0)
2094 {
2095 if (dtp->u.p.input_complete)
2096 return SUCCESS;
2097 if (dtp->u.p.at_eol)
2098 finish_separator (dtp);
2099 if (dtp->u.p.input_complete)
2100 return SUCCESS;
2101
2102 /* GFC_TYPE_UNKNOWN through for nulls and is detected
2103 after the switch block. */
2104
2105 dtp->u.p.saved_type = GFC_DTYPE_UNKNOWN;
2106 free_saved (dtp);
2107
2108 switch (nl->type)
2109 {
2110 case GFC_DTYPE_INTEGER:
2111 read_integer (dtp, len);
2112 break;
2113
2114 case GFC_DTYPE_LOGICAL:
2115 read_logical (dtp, len);
2116 break;
2117
2118 case GFC_DTYPE_CHARACTER:
2119 read_character (dtp, len);
2120 break;
2121
2122 case GFC_DTYPE_REAL:
2123 read_real (dtp, len);
2124 break;
2125
2126 case GFC_DTYPE_COMPLEX:
2127 read_complex (dtp, len, dlen);
2128 break;
2129
2130 case GFC_DTYPE_DERIVED:
2131 obj_name_len = strlen (nl->var_name) + 1;
2132 obj_name = get_mem (obj_name_len+1);
2133 strcpy (obj_name, nl->var_name);
2134 strcat (obj_name, "%");
2135
2136 /* If reading a derived type, disable the expanded read warning
2137 since a single object can have multiple reads. */
2138 dtp->u.p.expanded_read = 0;
2139
2140 /* Now loop over the components. Update the component pointer
2141 with the return value from nml_write_obj. This loop jumps
2142 past nested derived types by testing if the potential
2143 component name contains '%'. */
2144
2145 for (cmp = nl->next;
2146 cmp &&
2147 !strncmp (cmp->var_name, obj_name, obj_name_len) &&
2148 !strchr (cmp->var_name + obj_name_len, '%');
2149 cmp = cmp->next)
2150 {
2151
2152 if (nml_read_obj (dtp, cmp, (index_type)(pdata - nl->mem_pos),
2153 pprev_nl, nml_err_msg, clow, chigh)
2154 == FAILURE)
2155 {
2156 free_mem (obj_name);
2157 return FAILURE;
2158 }
2159
2160 if (dtp->u.p.input_complete)
2161 {
2162 free_mem (obj_name);
2163 return SUCCESS;
2164 }
2165 }
2166
2167 free_mem (obj_name);
2168 goto incr_idx;
2169
2170 default:
2171 st_sprintf (nml_err_msg, "Bad type for namelist object %s",
2172 nl->var_name);
2173 internal_error (&dtp->common, nml_err_msg);
2174 goto nml_err_ret;
2175 }
2176 }
2177
2178 /* The standard permits array data to stop short of the number of
2179 elements specified in the loop specification. In this case, we
2180 should be here with dtp->u.p.nml_read_error != 0. Control returns to
2181 nml_get_obj_data and an attempt is made to read object name. */
2182
2183 *pprev_nl = nl;
2184 if (dtp->u.p.nml_read_error)
2185 {
2186 dtp->u.p.expanded_read = 0;
2187 return SUCCESS;
2188 }
2189
2190 if (dtp->u.p.saved_type == GFC_DTYPE_UNKNOWN)
2191 {
2192 dtp->u.p.expanded_read = 0;
2193 goto incr_idx;
2194 }
2195
2196 /* Note the switch from GFC_DTYPE_type to BT_type at this point.
2197 This comes about because the read functions return BT_types. */
2198
2199 switch (dtp->u.p.saved_type)
2200 {
2201
2202 case BT_COMPLEX:
2203 case BT_REAL:
2204 case BT_INTEGER:
2205 case BT_LOGICAL:
2206 memcpy (pdata, dtp->u.p.value, dlen);
2207 break;
2208
2209 case BT_CHARACTER:
2210 m = (dlen < dtp->u.p.saved_used) ? dlen : dtp->u.p.saved_used;
2211 pdata = (void*)( pdata + clow - 1 );
2212 memcpy (pdata, dtp->u.p.saved_string, m);
2213 if (m < dlen)
2214 memset ((void*)( pdata + m ), ' ', dlen - m);
2215 break;
2216
2217 default:
2218 break;
2219 }
2220
2221 /* Warn if a non-standard expanded read occurs. A single read of a
2222 single object is acceptable. If a second read occurs, issue a warning
2223 and set the flag to zero to prevent further warnings. */
2224 if (dtp->u.p.expanded_read == 2)
2225 {
2226 notify_std (&dtp->common, GFC_STD_GNU, "Non-standard expanded namelist read.");
2227 dtp->u.p.expanded_read = 0;
2228 }
2229
2230 /* If the expanded read warning flag is set, increment it,
2231 indicating that a single read has occurred. */
2232 if (dtp->u.p.expanded_read >= 1)
2233 dtp->u.p.expanded_read++;
2234
2235 /* Break out of loop if scalar. */
2236 if (!nl->var_rank)
2237 break;
2238
2239 /* Now increment the index vector. */
2240
2241 incr_idx:
2242
2243 nml_carry = 1;
2244 for (dim = 0; dim < nl->var_rank; dim++)
2245 {
2246 nl->ls[dim].idx += nml_carry * nl->ls[dim].step;
2247 nml_carry = 0;
2248 if (((nl->ls[dim].step > 0) && (nl->ls[dim].idx > nl->ls[dim].end))
2249 ||
2250 ((nl->ls[dim].step < 0) && (nl->ls[dim].idx < nl->ls[dim].end)))
2251 {
2252 nl->ls[dim].idx = nl->ls[dim].start;
2253 nml_carry = 1;
2254 }
2255 }
2256 } while (!nml_carry);
2257
2258 if (dtp->u.p.repeat_count > 1)
2259 {
2260 st_sprintf (nml_err_msg, "Repeat count too large for namelist object %s" ,
2261 nl->var_name );
2262 goto nml_err_ret;
2263 }
2264 return SUCCESS;
2265
2266 nml_err_ret:
2267
2268 return FAILURE;
2269 }
2270
2271 /* Parses the object name, including array and substring qualifiers. It
2272 iterates over derived type components, touching those components and
2273 setting their loop specifications, if there is a qualifier. If the
2274 object is itself a derived type, its components and subcomponents are
2275 touched. nml_read_obj is called at the end and this reads the data in
2276 the manner specified by the object name. */
2277
2278 static try
2279 nml_get_obj_data (st_parameter_dt *dtp, namelist_info **pprev_nl,
2280 char *nml_err_msg)
2281 {
2282 char c;
2283 namelist_info * nl;
2284 namelist_info * first_nl = NULL;
2285 namelist_info * root_nl = NULL;
2286 int dim;
2287 int component_flag;
2288 char parse_err_msg[30];
2289 index_type clow, chigh;
2290
2291 /* Look for end of input or object name. If '?' or '=?' are encountered
2292 in stdin, print the node names or the namelist to stdout. */
2293
2294 eat_separator (dtp);
2295 if (dtp->u.p.input_complete)
2296 return SUCCESS;
2297
2298 if (dtp->u.p.at_eol)
2299 finish_separator (dtp);
2300 if (dtp->u.p.input_complete)
2301 return SUCCESS;
2302
2303 c = next_char (dtp);
2304 switch (c)
2305 {
2306 case '=':
2307 c = next_char (dtp);
2308 if (c != '?')
2309 {
2310 st_sprintf (nml_err_msg, "namelist read: misplaced = sign");
2311 goto nml_err_ret;
2312 }
2313 nml_query (dtp, '=');
2314 return SUCCESS;
2315
2316 case '?':
2317 nml_query (dtp, '?');
2318 return SUCCESS;
2319
2320 case '$':
2321 case '&':
2322 nml_match_name (dtp, "end", 3);
2323 if (dtp->u.p.nml_read_error)
2324 {
2325 st_sprintf (nml_err_msg, "namelist not terminated with / or &end");
2326 goto nml_err_ret;
2327 }
2328 case '/':
2329 dtp->u.p.input_complete = 1;
2330 return SUCCESS;
2331
2332 default :
2333 break;
2334 }
2335
2336 /* Untouch all nodes of the namelist and reset the flag that is set for
2337 derived type components. */
2338
2339 nml_untouch_nodes (dtp);
2340 component_flag = 0;
2341
2342 /* Get the object name - should '!' and '\n' be permitted separators? */
2343
2344 get_name:
2345
2346 free_saved (dtp);
2347
2348 do
2349 {
2350 push_char (dtp, tolower(c));
2351 c = next_char (dtp);
2352 } while (!( c=='=' || c==' ' || c=='\t' || c =='(' || c =='%' ));
2353
2354 unget_char (dtp, c);
2355
2356 /* Check that the name is in the namelist and get pointer to object.
2357 Three error conditions exist: (i) An attempt is being made to
2358 identify a non-existent object, following a failed data read or
2359 (ii) The object name does not exist or (iii) Too many data items
2360 are present for an object. (iii) gives the same error message
2361 as (i) */
2362
2363 push_char (dtp, '\0');
2364
2365 if (component_flag)
2366 {
2367 size_t var_len = strlen (root_nl->var_name);
2368 size_t saved_len
2369 = dtp->u.p.saved_string ? strlen (dtp->u.p.saved_string) : 0;
2370 char ext_name[var_len + saved_len + 1];
2371
2372 memcpy (ext_name, root_nl->var_name, var_len);
2373 if (dtp->u.p.saved_string)
2374 memcpy (ext_name + var_len, dtp->u.p.saved_string, saved_len);
2375 ext_name[var_len + saved_len] = '\0';
2376 nl = find_nml_node (dtp, ext_name);
2377 }
2378 else
2379 nl = find_nml_node (dtp, dtp->u.p.saved_string);
2380
2381 if (nl == NULL)
2382 {
2383 if (dtp->u.p.nml_read_error && *pprev_nl)
2384 st_sprintf (nml_err_msg, "Bad data for namelist object %s",
2385 (*pprev_nl)->var_name);
2386
2387 else
2388 st_sprintf (nml_err_msg, "Cannot match namelist object name %s",
2389 dtp->u.p.saved_string);
2390
2391 goto nml_err_ret;
2392 }
2393
2394 /* Get the length, data length, base pointer and rank of the variable.
2395 Set the default loop specification first. */
2396
2397 for (dim=0; dim < nl->var_rank; dim++)
2398 {
2399 nl->ls[dim].step = 1;
2400 nl->ls[dim].end = nl->dim[dim].ubound;
2401 nl->ls[dim].start = nl->dim[dim].lbound;
2402 nl->ls[dim].idx = nl->ls[dim].start;
2403 }
2404
2405 /* Check to see if there is a qualifier: if so, parse it.*/
2406
2407 if (c == '(' && nl->var_rank)
2408 {
2409 if (nml_parse_qualifier (dtp, nl->dim, nl->ls, nl->var_rank,
2410 parse_err_msg) == FAILURE)
2411 {
2412 st_sprintf (nml_err_msg, "%s for namelist variable %s",
2413 parse_err_msg, nl->var_name);
2414 goto nml_err_ret;
2415 }
2416 c = next_char (dtp);
2417 unget_char (dtp, c);
2418 }
2419
2420 /* Now parse a derived type component. The root namelist_info address
2421 is backed up, as is the previous component level. The component flag
2422 is set and the iteration is made by jumping back to get_name. */
2423
2424 if (c == '%')
2425 {
2426
2427 if (nl->type != GFC_DTYPE_DERIVED)
2428 {
2429 st_sprintf (nml_err_msg, "Attempt to get derived component for %s",
2430 nl->var_name);
2431 goto nml_err_ret;
2432 }
2433
2434 if (!component_flag)
2435 first_nl = nl;
2436
2437 root_nl = nl;
2438 component_flag = 1;
2439 c = next_char (dtp);
2440 goto get_name;
2441
2442 }
2443
2444 /* Parse a character qualifier, if present. chigh = 0 is a default
2445 that signals that the string length = string_length. */
2446
2447 clow = 1;
2448 chigh = 0;
2449
2450 if (c == '(' && nl->type == GFC_DTYPE_CHARACTER)
2451 {
2452 descriptor_dimension chd[1] = { {1, clow, nl->string_length} };
2453 array_loop_spec ind[1] = { {1, clow, nl->string_length, 1} };
2454
2455 if (nml_parse_qualifier (dtp, chd, ind, 1, parse_err_msg) == FAILURE)
2456 {
2457 st_sprintf (nml_err_msg, "%s for namelist variable %s",
2458 parse_err_msg, nl->var_name);
2459 goto nml_err_ret;
2460 }
2461
2462 clow = ind[0].start;
2463 chigh = ind[0].end;
2464
2465 if (ind[0].step != 1)
2466 {
2467 st_sprintf (nml_err_msg,
2468 "Bad step in substring for namelist object %s",
2469 nl->var_name);
2470 goto nml_err_ret;
2471 }
2472
2473 c = next_char (dtp);
2474 unget_char (dtp, c);
2475 }
2476
2477 /* If a derived type touch its components and restore the root
2478 namelist_info if we have parsed a qualified derived type
2479 component. */
2480
2481 if (nl->type == GFC_DTYPE_DERIVED)
2482 nml_touch_nodes (nl);
2483 if (component_flag)
2484 nl = first_nl;
2485
2486 /*make sure no extraneous qualifiers are there.*/
2487
2488 if (c == '(')
2489 {
2490 st_sprintf (nml_err_msg, "Qualifier for a scalar or non-character"
2491 " namelist object %s", nl->var_name);
2492 goto nml_err_ret;
2493 }
2494
2495 /* According to the standard, an equal sign MUST follow an object name. The
2496 following is possibly lax - it allows comments, blank lines and so on to
2497 intervene. eat_spaces (dtp); c = next_char (dtp); would be compliant*/
2498
2499 free_saved (dtp);
2500
2501 eat_separator (dtp);
2502 if (dtp->u.p.input_complete)
2503 return SUCCESS;
2504
2505 if (dtp->u.p.at_eol)
2506 finish_separator (dtp);
2507 if (dtp->u.p.input_complete)
2508 return SUCCESS;
2509
2510 c = next_char (dtp);
2511
2512 if (c != '=')
2513 {
2514 st_sprintf (nml_err_msg, "Equal sign must follow namelist object name %s",
2515 nl->var_name);
2516 goto nml_err_ret;
2517 }
2518
2519 if (nml_read_obj (dtp, nl, 0, pprev_nl, nml_err_msg, clow, chigh) == FAILURE)
2520 goto nml_err_ret;
2521
2522 return SUCCESS;
2523
2524 nml_err_ret:
2525
2526 return FAILURE;
2527 }
2528
2529 /* Entry point for namelist input. Goes through input until namelist name
2530 is matched. Then cycles through nml_get_obj_data until the input is
2531 completed or there is an error. */
2532
2533 void
2534 namelist_read (st_parameter_dt *dtp)
2535 {
2536 char c;
2537 jmp_buf eof_jump;
2538 char nml_err_msg[100];
2539 /* Pointer to the previously read object, in case attempt is made to read
2540 new object name. Should this fail, error message can give previous
2541 name. */
2542 namelist_info *prev_nl = NULL;
2543
2544 dtp->u.p.namelist_mode = 1;
2545 dtp->u.p.input_complete = 0;
2546 dtp->u.p.expanded_read = 0;
2547
2548 dtp->u.p.eof_jump = &eof_jump;
2549 if (setjmp (eof_jump))
2550 {
2551 dtp->u.p.eof_jump = NULL;
2552 generate_error (&dtp->common, ERROR_END, NULL);
2553 return;
2554 }
2555
2556 /* Look for &namelist_name . Skip all characters, testing for $nmlname.
2557 Exit on success or EOF. If '?' or '=?' encountered in stdin, print
2558 node names or namelist on stdout. */
2559
2560 find_nml_name:
2561 switch (c = next_char (dtp))
2562 {
2563 case '$':
2564 case '&':
2565 break;
2566
2567 case '=':
2568 c = next_char (dtp);
2569 if (c == '?')
2570 nml_query (dtp, '=');
2571 else
2572 unget_char (dtp, c);
2573 goto find_nml_name;
2574
2575 case '?':
2576 nml_query (dtp, '?');
2577
2578 default:
2579 goto find_nml_name;
2580 }
2581
2582 /* Match the name of the namelist. */
2583
2584 nml_match_name (dtp, dtp->namelist_name, dtp->namelist_name_len);
2585
2586 if (dtp->u.p.nml_read_error)
2587 goto find_nml_name;
2588
2589 /* Ready to read namelist objects. If there is an error in input
2590 from stdin, output the error message and continue. */
2591
2592 while (!dtp->u.p.input_complete)
2593 {
2594 if (nml_get_obj_data (dtp, &prev_nl, nml_err_msg) == FAILURE)
2595 {
2596 gfc_unit *u;
2597
2598 if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
2599 goto nml_err_ret;
2600
2601 u = find_unit (options.stderr_unit);
2602 st_printf ("%s\n", nml_err_msg);
2603 if (u != NULL)
2604 {
2605 flush (u->s);
2606 unlock_unit (u);
2607 }
2608 }
2609
2610 }
2611
2612 dtp->u.p.eof_jump = NULL;
2613 free_saved (dtp);
2614 free_line (dtp);
2615 return;
2616
2617 /* All namelist error calls return from here */
2618
2619 nml_err_ret:
2620
2621 dtp->u.p.eof_jump = NULL;
2622 free_saved (dtp);
2623 free_line (dtp);
2624 generate_error (&dtp->common, ERROR_READ_VALUE, nml_err_msg);
2625 return;
2626 }