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