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