re PR fortran/35617 (read namelist 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 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 if (c == '!')
360 {
361 eat_line (dtp);
362 c = next_char (dtp);
363 }
364 }
365 }
366 while (c == '\n' || c == '\r' || c == ' ');
367 unget_char (dtp, c);
368 }
369 break;
370
371 case '!':
372 if (dtp->u.p.namelist_mode)
373 { /* Eat a namelist comment. */
374 do
375 c = next_char (dtp);
376 while (c != '\n');
377
378 break;
379 }
380
381 /* Fall Through... */
382
383 default:
384 unget_char (dtp, c);
385 break;
386 }
387 }
388
389
390 /* Finish processing a separator that was interrupted by a newline.
391 If we're here, then another data item is present, so we finish what
392 we started on the previous line. */
393
394 static void
395 finish_separator (st_parameter_dt *dtp)
396 {
397 char c;
398
399 restart:
400 eat_spaces (dtp);
401
402 c = next_char (dtp);
403 switch (c)
404 {
405 case ',':
406 if (dtp->u.p.comma_flag)
407 unget_char (dtp, c);
408 else
409 {
410 c = eat_spaces (dtp);
411 if (c == '\n' || c == '\r')
412 goto restart;
413 }
414
415 break;
416
417 case '/':
418 dtp->u.p.input_complete = 1;
419 if (!dtp->u.p.namelist_mode)
420 return;
421 break;
422
423 case '\n':
424 case '\r':
425 goto restart;
426
427 case '!':
428 if (dtp->u.p.namelist_mode)
429 {
430 do
431 c = next_char (dtp);
432 while (c != '\n');
433
434 goto restart;
435 }
436
437 default:
438 unget_char (dtp, c);
439 break;
440 }
441 }
442
443
444 /* This function is needed to catch bad conversions so that namelist can
445 attempt to see if dtp->u.p.saved_string contains a new object name rather
446 than a bad value. */
447
448 static int
449 nml_bad_return (st_parameter_dt *dtp, char c)
450 {
451 if (dtp->u.p.namelist_mode)
452 {
453 dtp->u.p.nml_read_error = 1;
454 unget_char (dtp, c);
455 return 1;
456 }
457 return 0;
458 }
459
460 /* Convert an unsigned string to an integer. The length value is -1
461 if we are working on a repeat count. Returns nonzero if we have a
462 range problem. As a side effect, frees the dtp->u.p.saved_string. */
463
464 static int
465 convert_integer (st_parameter_dt *dtp, int length, int negative)
466 {
467 char c, *buffer, message[100];
468 int m;
469 GFC_INTEGER_LARGEST v, max, max10;
470
471 buffer = dtp->u.p.saved_string;
472 v = 0;
473
474 max = (length == -1) ? MAX_REPEAT : max_value (length, 1);
475 max10 = max / 10;
476
477 for (;;)
478 {
479 c = *buffer++;
480 if (c == '\0')
481 break;
482 c -= '0';
483
484 if (v > max10)
485 goto overflow;
486 v = 10 * v;
487
488 if (v > max - c)
489 goto overflow;
490 v += c;
491 }
492
493 m = 0;
494
495 if (length != -1)
496 {
497 if (negative)
498 v = -v;
499 set_integer (dtp->u.p.value, v, length);
500 }
501 else
502 {
503 dtp->u.p.repeat_count = v;
504
505 if (dtp->u.p.repeat_count == 0)
506 {
507 sprintf (message, "Zero repeat count in item %d of list input",
508 dtp->u.p.item_count);
509
510 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
511 m = 1;
512 }
513 }
514
515 free_saved (dtp);
516 return m;
517
518 overflow:
519 if (length == -1)
520 sprintf (message, "Repeat count overflow in item %d of list input",
521 dtp->u.p.item_count);
522 else
523 sprintf (message, "Integer overflow while reading item %d",
524 dtp->u.p.item_count);
525
526 free_saved (dtp);
527 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
528
529 return 1;
530 }
531
532
533 /* Parse a repeat count for logical and complex values which cannot
534 begin with a digit. Returns nonzero if we are done, zero if we
535 should continue on. */
536
537 static int
538 parse_repeat (st_parameter_dt *dtp)
539 {
540 char c, message[100];
541 int repeat;
542
543 c = next_char (dtp);
544 switch (c)
545 {
546 CASE_DIGITS:
547 repeat = c - '0';
548 break;
549
550 CASE_SEPARATORS:
551 unget_char (dtp, c);
552 eat_separator (dtp);
553 return 1;
554
555 default:
556 unget_char (dtp, c);
557 return 0;
558 }
559
560 for (;;)
561 {
562 c = next_char (dtp);
563 switch (c)
564 {
565 CASE_DIGITS:
566 repeat = 10 * repeat + c - '0';
567
568 if (repeat > MAX_REPEAT)
569 {
570 sprintf (message,
571 "Repeat count overflow in item %d of list input",
572 dtp->u.p.item_count);
573
574 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
575 return 1;
576 }
577
578 break;
579
580 case '*':
581 if (repeat == 0)
582 {
583 sprintf (message,
584 "Zero repeat count in item %d of list input",
585 dtp->u.p.item_count);
586
587 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
588 return 1;
589 }
590
591 goto done;
592
593 default:
594 goto bad_repeat;
595 }
596 }
597
598 done:
599 dtp->u.p.repeat_count = repeat;
600 return 0;
601
602 bad_repeat:
603
604 eat_line (dtp);
605 free_saved (dtp);
606 sprintf (message, "Bad repeat count in item %d of list input",
607 dtp->u.p.item_count);
608 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
609 return 1;
610 }
611
612
613 /* To read a logical we have to look ahead in the input stream to make sure
614 there is not an equal sign indicating a variable name. To do this we use
615 line_buffer to point to a temporary buffer, pushing characters there for
616 possible later reading. */
617
618 static void
619 l_push_char (st_parameter_dt *dtp, char c)
620 {
621 if (dtp->u.p.line_buffer == NULL)
622 {
623 dtp->u.p.line_buffer = get_mem (SCRATCH_SIZE);
624 memset (dtp->u.p.line_buffer, 0, SCRATCH_SIZE);
625 }
626
627 dtp->u.p.line_buffer[dtp->u.p.item_count++] = c;
628 }
629
630
631 /* Read a logical character on the input. */
632
633 static void
634 read_logical (st_parameter_dt *dtp, int length)
635 {
636 char c, message[100];
637 int i, v;
638
639 if (parse_repeat (dtp))
640 return;
641
642 c = tolower (next_char (dtp));
643 l_push_char (dtp, c);
644 switch (c)
645 {
646 case 't':
647 v = 1;
648 c = next_char (dtp);
649 l_push_char (dtp, c);
650
651 if (!is_separator(c))
652 goto possible_name;
653
654 unget_char (dtp, c);
655 break;
656 case 'f':
657 v = 0;
658 c = next_char (dtp);
659 l_push_char (dtp, c);
660
661 if (!is_separator(c))
662 goto possible_name;
663
664 unget_char (dtp, c);
665 break;
666 case '.':
667 c = tolower (next_char (dtp));
668 switch (c)
669 {
670 case 't':
671 v = 1;
672 break;
673 case 'f':
674 v = 0;
675 break;
676 default:
677 goto bad_logical;
678 }
679
680 break;
681
682 CASE_SEPARATORS:
683 unget_char (dtp, c);
684 eat_separator (dtp);
685 return; /* Null value. */
686
687 default:
688 goto bad_logical;
689 }
690
691 dtp->u.p.saved_type = BT_LOGICAL;
692 dtp->u.p.saved_length = length;
693
694 /* Eat trailing garbage. */
695 do
696 {
697 c = next_char (dtp);
698 }
699 while (!is_separator (c));
700
701 unget_char (dtp, c);
702 eat_separator (dtp);
703 dtp->u.p.item_count = 0;
704 dtp->u.p.line_buffer_enabled = 0;
705 set_integer ((int *) dtp->u.p.value, v, length);
706 free_line (dtp);
707
708 return;
709
710 possible_name:
711
712 for(i = 0; i < 63; i++)
713 {
714 c = next_char (dtp);
715 if (is_separator(c))
716 {
717 /* All done if this is not a namelist read. */
718 if (!dtp->u.p.namelist_mode)
719 goto logical_done;
720
721 unget_char (dtp, c);
722 eat_separator (dtp);
723 c = next_char (dtp);
724 if (c != '=')
725 {
726 unget_char (dtp, c);
727 goto logical_done;
728 }
729 }
730
731 l_push_char (dtp, c);
732 if (c == '=')
733 {
734 dtp->u.p.nml_read_error = 1;
735 dtp->u.p.line_buffer_enabled = 1;
736 dtp->u.p.item_count = 0;
737 return;
738 }
739
740 }
741
742 bad_logical:
743
744 free_line (dtp);
745
746 if (nml_bad_return (dtp, c))
747 return;
748
749 eat_line (dtp);
750 free_saved (dtp);
751 sprintf (message, "Bad logical value while reading item %d",
752 dtp->u.p.item_count);
753 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
754 return;
755
756 logical_done:
757
758 dtp->u.p.item_count = 0;
759 dtp->u.p.line_buffer_enabled = 0;
760 dtp->u.p.saved_type = BT_LOGICAL;
761 dtp->u.p.saved_length = length;
762 set_integer ((int *) dtp->u.p.value, v, length);
763 free_saved (dtp);
764 free_line (dtp);
765 }
766
767
768 /* Reading integers is tricky because we can actually be reading a
769 repeat count. We have to store the characters in a buffer because
770 we could be reading an integer that is larger than the default int
771 used for repeat counts. */
772
773 static void
774 read_integer (st_parameter_dt *dtp, int length)
775 {
776 char c, message[100];
777 int negative;
778
779 negative = 0;
780
781 c = next_char (dtp);
782 switch (c)
783 {
784 case '-':
785 negative = 1;
786 /* Fall through... */
787
788 case '+':
789 c = next_char (dtp);
790 goto get_integer;
791
792 CASE_SEPARATORS: /* Single null. */
793 unget_char (dtp, c);
794 eat_separator (dtp);
795 return;
796
797 CASE_DIGITS:
798 push_char (dtp, c);
799 break;
800
801 default:
802 goto bad_integer;
803 }
804
805 /* Take care of what may be a repeat count. */
806
807 for (;;)
808 {
809 c = next_char (dtp);
810 switch (c)
811 {
812 CASE_DIGITS:
813 push_char (dtp, c);
814 break;
815
816 case '*':
817 push_char (dtp, '\0');
818 goto repeat;
819
820 CASE_SEPARATORS: /* Not a repeat count. */
821 goto done;
822
823 default:
824 goto bad_integer;
825 }
826 }
827
828 repeat:
829 if (convert_integer (dtp, -1, 0))
830 return;
831
832 /* Get the real integer. */
833
834 c = next_char (dtp);
835 switch (c)
836 {
837 CASE_DIGITS:
838 break;
839
840 CASE_SEPARATORS:
841 unget_char (dtp, c);
842 eat_separator (dtp);
843 return;
844
845 case '-':
846 negative = 1;
847 /* Fall through... */
848
849 case '+':
850 c = next_char (dtp);
851 break;
852 }
853
854 get_integer:
855 if (!isdigit (c))
856 goto bad_integer;
857 push_char (dtp, c);
858
859 for (;;)
860 {
861 c = next_char (dtp);
862 switch (c)
863 {
864 CASE_DIGITS:
865 push_char (dtp, c);
866 break;
867
868 CASE_SEPARATORS:
869 goto done;
870
871 default:
872 goto bad_integer;
873 }
874 }
875
876 bad_integer:
877
878 if (nml_bad_return (dtp, c))
879 return;
880
881 eat_line (dtp);
882 free_saved (dtp);
883 sprintf (message, "Bad integer for item %d in list input",
884 dtp->u.p.item_count);
885 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
886
887 return;
888
889 done:
890 unget_char (dtp, c);
891 eat_separator (dtp);
892
893 push_char (dtp, '\0');
894 if (convert_integer (dtp, length, negative))
895 {
896 free_saved (dtp);
897 return;
898 }
899
900 free_saved (dtp);
901 dtp->u.p.saved_type = BT_INTEGER;
902 }
903
904
905 /* Read a character variable. */
906
907 static void
908 read_character (st_parameter_dt *dtp, int length __attribute__ ((unused)))
909 {
910 char c, quote, message[100];
911
912 quote = ' '; /* Space means no quote character. */
913
914 c = next_char (dtp);
915 switch (c)
916 {
917 CASE_DIGITS:
918 push_char (dtp, c);
919 break;
920
921 CASE_SEPARATORS:
922 unget_char (dtp, c); /* NULL value. */
923 eat_separator (dtp);
924 return;
925
926 case '"':
927 case '\'':
928 quote = c;
929 goto get_string;
930
931 default:
932 if (dtp->u.p.namelist_mode)
933 {
934 if (dtp->u.p.current_unit->flags.delim == DELIM_APOSTROPHE
935 || dtp->u.p.current_unit->flags.delim == DELIM_QUOTE
936 || c == '&' || c == '$' || c == '/')
937 {
938 unget_char (dtp, c);
939 return;
940 }
941
942 /* Check to see if we are seeing a namelist object name by using the
943 line buffer and looking ahead for an '=' or '('. */
944 l_push_char (dtp, c);
945
946 int i;
947 for(i = 0; i < 63; i++)
948 {
949 c = next_char (dtp);
950 if (is_separator(c))
951 {
952 unget_char (dtp, c);
953 eat_separator (dtp);
954 c = next_char (dtp);
955 if (c != '=')
956 {
957 l_push_char (dtp, c);
958 dtp->u.p.item_count = 0;
959 dtp->u.p.line_buffer_enabled = 1;
960 goto get_string;
961 }
962 }
963
964 l_push_char (dtp, c);
965
966 if (c == '=' || c == '(')
967 {
968 dtp->u.p.item_count = 0;
969 dtp->u.p.nml_read_error = 1;
970 dtp->u.p.line_buffer_enabled = 1;
971 return;
972 }
973 }
974
975 /* The string is too long to be a valid object name so assume that it
976 is a string to be read in as a value. */
977 dtp->u.p.item_count = 0;
978 dtp->u.p.line_buffer_enabled = 1;
979 goto get_string;
980 }
981
982 push_char (dtp, c);
983 goto get_string;
984 }
985
986 /* Deal with a possible repeat count. */
987
988 for (;;)
989 {
990 c = next_char (dtp);
991 switch (c)
992 {
993 CASE_DIGITS:
994 push_char (dtp, c);
995 break;
996
997 CASE_SEPARATORS:
998 unget_char (dtp, c);
999 goto done; /* String was only digits! */
1000
1001 case '*':
1002 push_char (dtp, '\0');
1003 goto got_repeat;
1004
1005 default:
1006 push_char (dtp, c);
1007 goto get_string; /* Not a repeat count after all. */
1008 }
1009 }
1010
1011 got_repeat:
1012 if (convert_integer (dtp, -1, 0))
1013 return;
1014
1015 /* Now get the real string. */
1016
1017 c = next_char (dtp);
1018 switch (c)
1019 {
1020 CASE_SEPARATORS:
1021 unget_char (dtp, c); /* Repeated NULL values. */
1022 eat_separator (dtp);
1023 return;
1024
1025 case '"':
1026 case '\'':
1027 quote = c;
1028 break;
1029
1030 default:
1031 push_char (dtp, c);
1032 break;
1033 }
1034
1035 get_string:
1036 for (;;)
1037 {
1038 c = next_char (dtp);
1039 switch (c)
1040 {
1041 case '"':
1042 case '\'':
1043 if (c != quote)
1044 {
1045 push_char (dtp, c);
1046 break;
1047 }
1048
1049 /* See if we have a doubled quote character or the end of
1050 the string. */
1051
1052 c = next_char (dtp);
1053 if (c == quote)
1054 {
1055 push_char (dtp, quote);
1056 break;
1057 }
1058
1059 unget_char (dtp, c);
1060 goto done;
1061
1062 CASE_SEPARATORS:
1063 if (quote == ' ')
1064 {
1065 unget_char (dtp, c);
1066 goto done;
1067 }
1068
1069 if (c != '\n' && c != '\r')
1070 push_char (dtp, c);
1071 break;
1072
1073 default:
1074 push_char (dtp, c);
1075 break;
1076 }
1077 }
1078
1079 /* At this point, we have to have a separator, or else the string is
1080 invalid. */
1081 done:
1082 c = next_char (dtp);
1083 if (is_separator (c))
1084 {
1085 unget_char (dtp, c);
1086 eat_separator (dtp);
1087 dtp->u.p.saved_type = BT_CHARACTER;
1088 free_line (dtp);
1089 }
1090 else
1091 {
1092 free_saved (dtp);
1093 sprintf (message, "Invalid string input in item %d",
1094 dtp->u.p.item_count);
1095 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1096 }
1097 }
1098
1099
1100 /* Parse a component of a complex constant or a real number that we
1101 are sure is already there. This is a straight real number parser. */
1102
1103 static int
1104 parse_real (st_parameter_dt *dtp, void *buffer, int length)
1105 {
1106 char c, message[100];
1107 int m, seen_dp;
1108
1109 c = next_char (dtp);
1110 if (c == '-' || c == '+')
1111 {
1112 push_char (dtp, c);
1113 c = next_char (dtp);
1114 }
1115
1116 if (!isdigit (c) && c != '.')
1117 {
1118 if (c == 'i' || c == 'I' || c == 'n' || c == 'N')
1119 goto inf_nan;
1120 else
1121 goto bad;
1122 }
1123
1124 push_char (dtp, c);
1125
1126 seen_dp = (c == '.') ? 1 : 0;
1127
1128 for (;;)
1129 {
1130 c = next_char (dtp);
1131 switch (c)
1132 {
1133 CASE_DIGITS:
1134 push_char (dtp, c);
1135 break;
1136
1137 case '.':
1138 if (seen_dp)
1139 goto bad;
1140
1141 seen_dp = 1;
1142 push_char (dtp, c);
1143 break;
1144
1145 case 'e':
1146 case 'E':
1147 case 'd':
1148 case 'D':
1149 push_char (dtp, 'e');
1150 goto exp1;
1151
1152 case '-':
1153 case '+':
1154 push_char (dtp, 'e');
1155 push_char (dtp, c);
1156 c = next_char (dtp);
1157 goto exp2;
1158
1159 CASE_SEPARATORS:
1160 unget_char (dtp, c);
1161 goto done;
1162
1163 default:
1164 goto done;
1165 }
1166 }
1167
1168 exp1:
1169 c = next_char (dtp);
1170 if (c != '-' && c != '+')
1171 push_char (dtp, '+');
1172 else
1173 {
1174 push_char (dtp, c);
1175 c = next_char (dtp);
1176 }
1177
1178 exp2:
1179 if (!isdigit (c))
1180 goto bad;
1181
1182 push_char (dtp, c);
1183
1184 for (;;)
1185 {
1186 c = next_char (dtp);
1187 switch (c)
1188 {
1189 CASE_DIGITS:
1190 push_char (dtp, c);
1191 break;
1192
1193 CASE_SEPARATORS:
1194 unget_char (dtp, c);
1195 goto done;
1196
1197 default:
1198 goto done;
1199 }
1200 }
1201
1202 done:
1203 unget_char (dtp, c);
1204 push_char (dtp, '\0');
1205
1206 m = convert_real (dtp, buffer, dtp->u.p.saved_string, length);
1207 free_saved (dtp);
1208
1209 return m;
1210
1211 inf_nan:
1212 /* Match INF and Infinity. */
1213 if ((c == 'i' || c == 'I')
1214 && ((c = next_char (dtp)) == 'n' || c == 'N')
1215 && ((c = next_char (dtp)) == 'f' || c == 'F'))
1216 {
1217 c = next_char (dtp);
1218 if ((c != 'i' && c != 'I')
1219 || ((c == 'i' || c == 'I')
1220 && ((c = next_char (dtp)) == 'n' || c == 'N')
1221 && ((c = next_char (dtp)) == 'i' || c == 'I')
1222 && ((c = next_char (dtp)) == 't' || c == 'T')
1223 && ((c = next_char (dtp)) == 'y' || c == 'Y')
1224 && (c = next_char (dtp))))
1225 {
1226 if (is_separator (c))
1227 unget_char (dtp, c);
1228 push_char (dtp, 'i');
1229 push_char (dtp, 'n');
1230 push_char (dtp, 'f');
1231 goto done;
1232 }
1233 } /* Match NaN. */
1234 else if (((c = next_char (dtp)) == 'a' || c == 'A')
1235 && ((c = next_char (dtp)) == 'n' || c == 'N')
1236 && (c = next_char (dtp)))
1237 {
1238 if (is_separator (c))
1239 unget_char (dtp, c);
1240 push_char (dtp, 'n');
1241 push_char (dtp, 'a');
1242 push_char (dtp, 'n');
1243 goto done;
1244 }
1245
1246 bad:
1247
1248 if (nml_bad_return (dtp, c))
1249 return 0;
1250
1251 eat_line (dtp);
1252 free_saved (dtp);
1253 sprintf (message, "Bad floating point number for item %d",
1254 dtp->u.p.item_count);
1255 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1256
1257 return 1;
1258 }
1259
1260
1261 /* Reading a complex number is straightforward because we can tell
1262 what it is right away. */
1263
1264 static void
1265 read_complex (st_parameter_dt *dtp, int kind, size_t size)
1266 {
1267 char message[100];
1268 char c;
1269
1270 if (parse_repeat (dtp))
1271 return;
1272
1273 c = next_char (dtp);
1274 switch (c)
1275 {
1276 case '(':
1277 break;
1278
1279 CASE_SEPARATORS:
1280 unget_char (dtp, c);
1281 eat_separator (dtp);
1282 return;
1283
1284 default:
1285 goto bad_complex;
1286 }
1287
1288 eat_spaces (dtp);
1289 if (parse_real (dtp, dtp->u.p.value, kind))
1290 return;
1291
1292 eol_1:
1293 eat_spaces (dtp);
1294 c = next_char (dtp);
1295 if (c == '\n' || c== '\r')
1296 goto eol_1;
1297 else
1298 unget_char (dtp, c);
1299
1300 if (next_char (dtp) != ',')
1301 goto bad_complex;
1302
1303 eol_2:
1304 eat_spaces (dtp);
1305 c = next_char (dtp);
1306 if (c == '\n' || c== '\r')
1307 goto eol_2;
1308 else
1309 unget_char (dtp, c);
1310
1311 if (parse_real (dtp, dtp->u.p.value + size / 2, kind))
1312 return;
1313
1314 eat_spaces (dtp);
1315 if (next_char (dtp) != ')')
1316 goto bad_complex;
1317
1318 c = next_char (dtp);
1319 if (!is_separator (c))
1320 goto bad_complex;
1321
1322 unget_char (dtp, c);
1323 eat_separator (dtp);
1324
1325 free_saved (dtp);
1326 dtp->u.p.saved_type = BT_COMPLEX;
1327 return;
1328
1329 bad_complex:
1330
1331 if (nml_bad_return (dtp, c))
1332 return;
1333
1334 eat_line (dtp);
1335 free_saved (dtp);
1336 sprintf (message, "Bad complex value in item %d of list input",
1337 dtp->u.p.item_count);
1338 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1339 }
1340
1341
1342 /* Parse a real number with a possible repeat count. */
1343
1344 static void
1345 read_real (st_parameter_dt *dtp, int length)
1346 {
1347 char c, message[100];
1348 int seen_dp;
1349 int is_inf;
1350
1351 seen_dp = 0;
1352
1353 c = next_char (dtp);
1354 switch (c)
1355 {
1356 CASE_DIGITS:
1357 push_char (dtp, c);
1358 break;
1359
1360 case '.':
1361 push_char (dtp, c);
1362 seen_dp = 1;
1363 break;
1364
1365 case '+':
1366 case '-':
1367 goto got_sign;
1368
1369 CASE_SEPARATORS:
1370 unget_char (dtp, c); /* Single null. */
1371 eat_separator (dtp);
1372 return;
1373
1374 case 'i':
1375 case 'I':
1376 case 'n':
1377 case 'N':
1378 goto inf_nan;
1379
1380 default:
1381 goto bad_real;
1382 }
1383
1384 /* Get the digit string that might be a repeat count. */
1385
1386 for (;;)
1387 {
1388 c = next_char (dtp);
1389 switch (c)
1390 {
1391 CASE_DIGITS:
1392 push_char (dtp, c);
1393 break;
1394
1395 case '.':
1396 if (seen_dp)
1397 goto bad_real;
1398
1399 seen_dp = 1;
1400 push_char (dtp, c);
1401 goto real_loop;
1402
1403 case 'E':
1404 case 'e':
1405 case 'D':
1406 case 'd':
1407 goto exp1;
1408
1409 case '+':
1410 case '-':
1411 push_char (dtp, 'e');
1412 push_char (dtp, c);
1413 c = next_char (dtp);
1414 goto exp2;
1415
1416 case '*':
1417 push_char (dtp, '\0');
1418 goto got_repeat;
1419
1420 CASE_SEPARATORS:
1421 if (c != '\n' && c != ',' && c != '\r')
1422 unget_char (dtp, c);
1423 goto done;
1424
1425 default:
1426 goto bad_real;
1427 }
1428 }
1429
1430 got_repeat:
1431 if (convert_integer (dtp, -1, 0))
1432 return;
1433
1434 /* Now get the number itself. */
1435
1436 c = next_char (dtp);
1437 if (is_separator (c))
1438 { /* Repeated null value. */
1439 unget_char (dtp, c);
1440 eat_separator (dtp);
1441 return;
1442 }
1443
1444 if (c != '-' && c != '+')
1445 push_char (dtp, '+');
1446 else
1447 {
1448 got_sign:
1449 push_char (dtp, c);
1450 c = next_char (dtp);
1451 }
1452
1453 if (!isdigit (c) && c != '.')
1454 {
1455 if (c == 'i' || c == 'I' || c == 'n' || c == 'N')
1456 goto inf_nan;
1457 else
1458 goto bad_real;
1459 }
1460
1461 if (c == '.')
1462 {
1463 if (seen_dp)
1464 goto bad_real;
1465 else
1466 seen_dp = 1;
1467 }
1468
1469 push_char (dtp, c);
1470
1471 real_loop:
1472 for (;;)
1473 {
1474 c = next_char (dtp);
1475 switch (c)
1476 {
1477 CASE_DIGITS:
1478 push_char (dtp, c);
1479 break;
1480
1481 CASE_SEPARATORS:
1482 goto done;
1483
1484 case '.':
1485 if (seen_dp)
1486 goto bad_real;
1487
1488 seen_dp = 1;
1489 push_char (dtp, c);
1490 break;
1491
1492 case 'E':
1493 case 'e':
1494 case 'D':
1495 case 'd':
1496 goto exp1;
1497
1498 case '+':
1499 case '-':
1500 push_char (dtp, 'e');
1501 push_char (dtp, c);
1502 c = next_char (dtp);
1503 goto exp2;
1504
1505 default:
1506 goto bad_real;
1507 }
1508 }
1509
1510 exp1:
1511 push_char (dtp, 'e');
1512
1513 c = next_char (dtp);
1514 if (c != '+' && c != '-')
1515 push_char (dtp, '+');
1516 else
1517 {
1518 push_char (dtp, c);
1519 c = next_char (dtp);
1520 }
1521
1522 exp2:
1523 if (!isdigit (c))
1524 goto bad_real;
1525 push_char (dtp, c);
1526
1527 for (;;)
1528 {
1529 c = next_char (dtp);
1530
1531 switch (c)
1532 {
1533 CASE_DIGITS:
1534 push_char (dtp, c);
1535 break;
1536
1537 CASE_SEPARATORS:
1538 goto done;
1539
1540 default:
1541 goto bad_real;
1542 }
1543 }
1544
1545 done:
1546 unget_char (dtp, c);
1547 eat_separator (dtp);
1548 push_char (dtp, '\0');
1549 if (convert_real (dtp, dtp->u.p.value, dtp->u.p.saved_string, length))
1550 return;
1551
1552 free_saved (dtp);
1553 dtp->u.p.saved_type = BT_REAL;
1554 return;
1555
1556 inf_nan:
1557 l_push_char (dtp, c);
1558 is_inf = 0;
1559
1560 /* Match INF and Infinity. */
1561 if (c == 'i' || c == 'I')
1562 {
1563 c = next_char (dtp);
1564 l_push_char (dtp, c);
1565 if (c != 'n' && c != 'N')
1566 goto unwind;
1567 c = next_char (dtp);
1568 l_push_char (dtp, c);
1569 if (c != 'f' && c != 'F')
1570 goto unwind;
1571 c = next_char (dtp);
1572 l_push_char (dtp, c);
1573 if (!is_separator (c))
1574 {
1575 if (c != 'i' && c != 'I')
1576 goto unwind;
1577 c = next_char (dtp);
1578 l_push_char (dtp, c);
1579 if (c != 'n' && c != 'N')
1580 goto unwind;
1581 c = next_char (dtp);
1582 l_push_char (dtp, c);
1583 if (c != 'i' && c != 'I')
1584 goto unwind;
1585 c = next_char (dtp);
1586 l_push_char (dtp, c);
1587 if (c != 't' && c != 'T')
1588 goto unwind;
1589 c = next_char (dtp);
1590 l_push_char (dtp, c);
1591 if (c != 'y' && c != 'Y')
1592 goto unwind;
1593 c = next_char (dtp);
1594 l_push_char (dtp, c);
1595 }
1596 is_inf = 1;
1597 } /* Match NaN. */
1598 else
1599 {
1600 c = next_char (dtp);
1601 l_push_char (dtp, c);
1602 if (c != 'a' && c != 'A')
1603 goto unwind;
1604 c = next_char (dtp);
1605 l_push_char (dtp, c);
1606 if (c != 'n' && c != 'N')
1607 goto unwind;
1608 c = next_char (dtp);
1609 l_push_char (dtp, c);
1610 }
1611
1612 if (!is_separator (c))
1613 goto unwind;
1614
1615 if (dtp->u.p.namelist_mode)
1616 {
1617 if (c == ' ' || c =='\n' || c == '\r')
1618 {
1619 do
1620 c = next_char (dtp);
1621 while (c == ' ' || c =='\n' || c == '\r');
1622
1623 l_push_char (dtp, c);
1624
1625 if (c == '=')
1626 goto unwind;
1627 }
1628 }
1629
1630 if (is_inf)
1631 {
1632 push_char (dtp, 'i');
1633 push_char (dtp, 'n');
1634 push_char (dtp, 'f');
1635 }
1636 else
1637 {
1638 push_char (dtp, 'n');
1639 push_char (dtp, 'a');
1640 push_char (dtp, 'n');
1641 }
1642
1643 dtp->u.p.item_count = 0;
1644 dtp->u.p.line_buffer_enabled = 0;
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 }