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