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