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