re PR libfortran/33672 (Additional runtime checks needed for namelist reads)
[platform/upstream/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             {
901               unget_char (dtp, c);
902               return;
903             }
904
905           /* Check to see if we are seeing a namelist object name by using the
906              line buffer and looking ahead for an '=' or '('.  */
907           l_push_char (dtp, c);
908
909           int i;
910           for(i = 0; i < 63; i++)
911             {
912               c = next_char (dtp);
913               if (is_separator(c))
914                 {
915                   unget_char (dtp, c);
916                   eat_separator (dtp);
917                   c = next_char (dtp);
918                   if (c != '=')
919                     {
920                       l_push_char (dtp, c);
921                       dtp->u.p.item_count = 0;
922                       dtp->u.p.line_buffer_enabled = 1;
923                       goto get_string;
924                     }
925                 }
926  
927               l_push_char (dtp, c);
928               if (c == '=' || c == '(')
929                 {
930                   dtp->u.p.item_count = 0;
931                   dtp->u.p.nml_read_error = 1;
932                   dtp->u.p.line_buffer_enabled = 1;
933                   return;
934                 }
935             }
936
937           /* The string is too long to be a valid object name so assume that it
938              is a string to be read in as a value.  */
939           dtp->u.p.line_buffer_enabled = 1;
940           goto get_string;
941         }
942
943       push_char (dtp, c);
944       goto get_string;
945     }
946
947   /* Deal with a possible repeat count.  */
948
949   for (;;)
950     {
951       c = next_char (dtp);
952       switch (c)
953         {
954         CASE_DIGITS:
955           push_char (dtp, c);
956           break;
957
958         CASE_SEPARATORS:
959           unget_char (dtp, c);
960           goto done;            /* String was only digits!  */
961
962         case '*':
963           push_char (dtp, '\0');
964           goto got_repeat;
965
966         default:
967           push_char (dtp, c);
968           goto get_string;      /* Not a repeat count after all.  */
969         }
970     }
971
972  got_repeat:
973   if (convert_integer (dtp, -1, 0))
974     return;
975
976   /* Now get the real string.  */
977
978   c = next_char (dtp);
979   switch (c)
980     {
981     CASE_SEPARATORS:
982       unget_char (dtp, c);              /* Repeated NULL values.  */
983       eat_separator (dtp);
984       return;
985
986     case '"':
987     case '\'':
988       quote = c;
989       break;
990
991     default:
992       push_char (dtp, c);
993       break;
994     }
995
996  get_string:
997   for (;;)
998     {
999       c = next_char (dtp);
1000       switch (c)
1001         {
1002         case '"':
1003         case '\'':
1004           if (c != quote)
1005             {
1006               push_char (dtp, c);
1007               break;
1008             }
1009
1010           /* See if we have a doubled quote character or the end of
1011              the string.  */
1012
1013           c = next_char (dtp);
1014           if (c == quote)
1015             {
1016               push_char (dtp, quote);
1017               break;
1018             }
1019
1020           unget_char (dtp, c);
1021           goto done;
1022
1023         CASE_SEPARATORS:
1024           if (quote == ' ')
1025             {
1026               unget_char (dtp, c);
1027               goto done;
1028             }
1029
1030           if (c != '\n' && c != '\r')
1031             push_char (dtp, c);
1032           break;
1033
1034         default:
1035           push_char (dtp, c);
1036           break;
1037         }
1038     }
1039
1040   /* At this point, we have to have a separator, or else the string is
1041      invalid.  */
1042  done:
1043   c = next_char (dtp);
1044   if (is_separator (c))
1045     {
1046       unget_char (dtp, c);
1047       eat_separator (dtp);
1048       dtp->u.p.saved_type = BT_CHARACTER;
1049       free_line (dtp);
1050     }
1051   else
1052     {
1053       free_saved (dtp);
1054       sprintf (message, "Invalid string input in item %d",
1055                   dtp->u.p.item_count);
1056       generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1057     }
1058 }
1059
1060
1061 /* Parse a component of a complex constant or a real number that we
1062    are sure is already there.  This is a straight real number parser.  */
1063
1064 static int
1065 parse_real (st_parameter_dt *dtp, void *buffer, int length)
1066 {
1067   char c, message[100];
1068   int m, seen_dp;
1069
1070   c = next_char (dtp);
1071   if (c == '-' || c == '+')
1072     {
1073       push_char (dtp, c);
1074       c = next_char (dtp);
1075     }
1076
1077   if (!isdigit (c) && c != '.')
1078     goto bad;
1079
1080   push_char (dtp, c);
1081
1082   seen_dp = (c == '.') ? 1 : 0;
1083
1084   for (;;)
1085     {
1086       c = next_char (dtp);
1087       switch (c)
1088         {
1089         CASE_DIGITS:
1090           push_char (dtp, c);
1091           break;
1092
1093         case '.':
1094           if (seen_dp)
1095             goto bad;
1096
1097           seen_dp = 1;
1098           push_char (dtp, c);
1099           break;
1100
1101         case 'e':
1102         case 'E':
1103         case 'd':
1104         case 'D':
1105           push_char (dtp, 'e');
1106           goto exp1;
1107
1108         case '-':
1109         case '+':
1110           push_char (dtp, 'e');
1111           push_char (dtp, c);
1112           c = next_char (dtp);
1113           goto exp2;
1114
1115         CASE_SEPARATORS:
1116           unget_char (dtp, c);
1117           goto done;
1118
1119         default:
1120           goto done;
1121         }
1122     }
1123
1124  exp1:
1125   c = next_char (dtp);
1126   if (c != '-' && c != '+')
1127     push_char (dtp, '+');
1128   else
1129     {
1130       push_char (dtp, c);
1131       c = next_char (dtp);
1132     }
1133
1134  exp2:
1135   if (!isdigit (c))
1136     goto bad;
1137   push_char (dtp, c);
1138
1139   for (;;)
1140     {
1141       c = next_char (dtp);
1142       switch (c)
1143         {
1144         CASE_DIGITS:
1145           push_char (dtp, c);
1146           break;
1147
1148         CASE_SEPARATORS:
1149           unget_char (dtp, c);
1150           goto done;
1151
1152         default:
1153           goto done;
1154         }
1155     }
1156
1157  done:
1158   unget_char (dtp, c);
1159   push_char (dtp, '\0');
1160
1161   m = convert_real (dtp, buffer, dtp->u.p.saved_string, length);
1162   free_saved (dtp);
1163
1164   return m;
1165
1166  bad:
1167
1168   if (nml_bad_return (dtp, c))
1169     return 0;
1170
1171   eat_line (dtp);
1172   free_saved (dtp);
1173   sprintf (message, "Bad floating point number for item %d",
1174               dtp->u.p.item_count);
1175   generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1176
1177   return 1;
1178 }
1179
1180
1181 /* Reading a complex number is straightforward because we can tell
1182    what it is right away.  */
1183
1184 static void
1185 read_complex (st_parameter_dt *dtp, int kind, size_t size)
1186 {
1187   char message[100];
1188   char c;
1189
1190   if (parse_repeat (dtp))
1191     return;
1192
1193   c = next_char (dtp);
1194   switch (c)
1195     {
1196     case '(':
1197       break;
1198
1199     CASE_SEPARATORS:
1200       unget_char (dtp, c);
1201       eat_separator (dtp);
1202       return;
1203
1204     default:
1205       goto bad_complex;
1206     }
1207
1208   eat_spaces (dtp);
1209   if (parse_real (dtp, dtp->u.p.value, kind))
1210     return;
1211
1212 eol_1:
1213   eat_spaces (dtp);
1214   c = next_char (dtp);
1215   if (c == '\n' || c== '\r')
1216     goto eol_1;
1217   else
1218     unget_char (dtp, c);
1219
1220   if (next_char (dtp) != ',')
1221     goto bad_complex;
1222
1223 eol_2:
1224   eat_spaces (dtp);
1225   c = next_char (dtp);
1226   if (c == '\n' || c== '\r')
1227     goto eol_2;
1228   else
1229     unget_char (dtp, c);
1230
1231   if (parse_real (dtp, dtp->u.p.value + size / 2, kind))
1232     return;
1233
1234   eat_spaces (dtp);
1235   if (next_char (dtp) != ')')
1236     goto bad_complex;
1237
1238   c = next_char (dtp);
1239   if (!is_separator (c))
1240     goto bad_complex;
1241
1242   unget_char (dtp, c);
1243   eat_separator (dtp);
1244
1245   free_saved (dtp);
1246   dtp->u.p.saved_type = BT_COMPLEX;
1247   return;
1248
1249  bad_complex:
1250
1251   if (nml_bad_return (dtp, c))
1252     return;
1253
1254   eat_line (dtp);
1255   free_saved (dtp);
1256   sprintf (message, "Bad complex value in item %d of list input",
1257               dtp->u.p.item_count);
1258   generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1259 }
1260
1261
1262 /* Parse a real number with a possible repeat count.  */
1263
1264 static void
1265 read_real (st_parameter_dt *dtp, int length)
1266 {
1267   char c, message[100];
1268   int seen_dp;
1269
1270   seen_dp = 0;
1271
1272   c = next_char (dtp);
1273   switch (c)
1274     {
1275     CASE_DIGITS:
1276       push_char (dtp, c);
1277       break;
1278
1279     case '.':
1280       push_char (dtp, c);
1281       seen_dp = 1;
1282       break;
1283
1284     case '+':
1285     case '-':
1286       goto got_sign;
1287
1288     CASE_SEPARATORS:
1289       unget_char (dtp, c);              /* Single null.  */
1290       eat_separator (dtp);
1291       return;
1292
1293     default:
1294       goto bad_real;
1295     }
1296
1297   /* Get the digit string that might be a repeat count.  */
1298
1299   for (;;)
1300     {
1301       c = next_char (dtp);
1302       switch (c)
1303         {
1304         CASE_DIGITS:
1305           push_char (dtp, c);
1306           break;
1307
1308         case '.':
1309           if (seen_dp)
1310             goto bad_real;
1311
1312           seen_dp = 1;
1313           push_char (dtp, c);
1314           goto real_loop;
1315
1316         case 'E':
1317         case 'e':
1318         case 'D':
1319         case 'd':
1320           goto exp1;
1321
1322         case '+':
1323         case '-':
1324           push_char (dtp, 'e');
1325           push_char (dtp, c);
1326           c = next_char (dtp);
1327           goto exp2;
1328
1329         case '*':
1330           push_char (dtp, '\0');
1331           goto got_repeat;
1332
1333         CASE_SEPARATORS:
1334           if (c != '\n' &&  c != ',' && c != '\r')
1335             unget_char (dtp, c);
1336           goto done;
1337
1338         default:
1339           goto bad_real;
1340         }
1341     }
1342
1343  got_repeat:
1344   if (convert_integer (dtp, -1, 0))
1345     return;
1346
1347   /* Now get the number itself.  */
1348
1349   c = next_char (dtp);
1350   if (is_separator (c))
1351     {                           /* Repeated null value.  */
1352       unget_char (dtp, c);
1353       eat_separator (dtp);
1354       return;
1355     }
1356
1357   if (c != '-' && c != '+')
1358     push_char (dtp, '+');
1359   else
1360     {
1361     got_sign:
1362       push_char (dtp, c);
1363       c = next_char (dtp);
1364     }
1365
1366   if (!isdigit (c) && c != '.')
1367     goto bad_real;
1368
1369   if (c == '.')
1370     {
1371       if (seen_dp)
1372         goto bad_real;
1373       else
1374         seen_dp = 1;
1375     }
1376
1377   push_char (dtp, c);
1378
1379  real_loop:
1380   for (;;)
1381     {
1382       c = next_char (dtp);
1383       switch (c)
1384         {
1385         CASE_DIGITS:
1386           push_char (dtp, c);
1387           break;
1388
1389         CASE_SEPARATORS:
1390           goto done;
1391
1392         case '.':
1393           if (seen_dp)
1394             goto bad_real;
1395
1396           seen_dp = 1;
1397           push_char (dtp, c);
1398           break;
1399
1400         case 'E':
1401         case 'e':
1402         case 'D':
1403         case 'd':
1404           goto exp1;
1405
1406         case '+':
1407         case '-':
1408           push_char (dtp, 'e');
1409           push_char (dtp, c);
1410           c = next_char (dtp);
1411           goto exp2;
1412
1413         default:
1414           goto bad_real;
1415         }
1416     }
1417
1418  exp1:
1419   push_char (dtp, 'e');
1420
1421   c = next_char (dtp);
1422   if (c != '+' && c != '-')
1423     push_char (dtp, '+');
1424   else
1425     {
1426       push_char (dtp, c);
1427       c = next_char (dtp);
1428     }
1429
1430  exp2:
1431   if (!isdigit (c))
1432     goto bad_real;
1433   push_char (dtp, c);
1434
1435   for (;;)
1436     {
1437       c = next_char (dtp);
1438
1439       switch (c)
1440         {
1441         CASE_DIGITS:
1442           push_char (dtp, c);
1443           break;
1444
1445         CASE_SEPARATORS:
1446           goto done;
1447
1448         default:
1449           goto bad_real;
1450         }
1451     }
1452
1453  done:
1454   unget_char (dtp, c);
1455   eat_separator (dtp);
1456   push_char (dtp, '\0');
1457   if (convert_real (dtp, dtp->u.p.value, dtp->u.p.saved_string, length))
1458     return;
1459
1460   free_saved (dtp);
1461   dtp->u.p.saved_type = BT_REAL;
1462   return;
1463
1464  bad_real:
1465
1466   if (nml_bad_return (dtp, c))
1467     return;
1468
1469   eat_line (dtp);
1470   free_saved (dtp);
1471   sprintf (message, "Bad real number in item %d of list input",
1472               dtp->u.p.item_count);
1473   generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1474 }
1475
1476
1477 /* Check the current type against the saved type to make sure they are
1478    compatible.  Returns nonzero if incompatible.  */
1479
1480 static int
1481 check_type (st_parameter_dt *dtp, bt type, int len)
1482 {
1483   char message[100];
1484
1485   if (dtp->u.p.saved_type != BT_NULL && dtp->u.p.saved_type != type)
1486     {
1487       sprintf (message, "Read type %s where %s was expected for item %d",
1488                   type_name (dtp->u.p.saved_type), type_name (type),
1489                   dtp->u.p.item_count);
1490
1491       generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1492       return 1;
1493     }
1494
1495   if (dtp->u.p.saved_type == BT_NULL || dtp->u.p.saved_type == BT_CHARACTER)
1496     return 0;
1497
1498   if (dtp->u.p.saved_length != len)
1499     {
1500       sprintf (message,
1501                   "Read kind %d %s where kind %d is required for item %d",
1502                   dtp->u.p.saved_length, type_name (dtp->u.p.saved_type), len,
1503                   dtp->u.p.item_count);
1504       generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1505       return 1;
1506     }
1507
1508   return 0;
1509 }
1510
1511
1512 /* Top level data transfer subroutine for list reads.  Because we have
1513    to deal with repeat counts, the data item is always saved after
1514    reading, usually in the dtp->u.p.value[] array.  If a repeat count is
1515    greater than one, we copy the data item multiple times.  */
1516
1517 static void
1518 list_formatted_read_scalar (st_parameter_dt *dtp, bt type, void *p, int kind,
1519                             size_t size)
1520 {
1521   char c;
1522   int m;
1523   jmp_buf eof_jump;
1524
1525   dtp->u.p.namelist_mode = 0;
1526
1527   dtp->u.p.eof_jump = &eof_jump;
1528   if (setjmp (eof_jump))
1529     {
1530       generate_error (&dtp->common, LIBERROR_END, NULL);
1531       goto cleanup;
1532     }
1533
1534   if (dtp->u.p.first_item)
1535     {
1536       dtp->u.p.first_item = 0;
1537       dtp->u.p.input_complete = 0;
1538       dtp->u.p.repeat_count = 1;
1539       dtp->u.p.at_eol = 0;
1540
1541       c = eat_spaces (dtp);
1542       if (is_separator (c))
1543         {
1544           /* Found a null value.  */
1545           eat_separator (dtp);
1546           dtp->u.p.repeat_count = 0;
1547
1548           /* eat_separator sets this flag if the separator was a comma.  */
1549           if (dtp->u.p.comma_flag)
1550             goto cleanup;
1551
1552           /* eat_separator sets this flag if the separator was a \n or \r.  */
1553           if (dtp->u.p.at_eol)
1554             finish_separator (dtp);
1555           else
1556             goto cleanup;
1557         }
1558
1559     }
1560   else
1561     {
1562       if (dtp->u.p.input_complete)
1563         goto cleanup;
1564
1565       if (dtp->u.p.repeat_count > 0)
1566         {
1567           if (check_type (dtp, type, kind))
1568             return;
1569           goto set_value;
1570         }
1571
1572       if (dtp->u.p.at_eol)
1573         finish_separator (dtp);
1574       else
1575         {
1576           eat_spaces (dtp);
1577           /* Trailing spaces prior to end of line.  */
1578           if (dtp->u.p.at_eol)
1579             finish_separator (dtp);
1580         }
1581
1582       dtp->u.p.saved_type = BT_NULL;
1583       dtp->u.p.repeat_count = 1;
1584     }
1585
1586   switch (type)
1587     {
1588     case BT_INTEGER:
1589       read_integer (dtp, kind);
1590       break;
1591     case BT_LOGICAL:
1592       read_logical (dtp, kind);
1593       break;
1594     case BT_CHARACTER:
1595       read_character (dtp, kind);
1596       break;
1597     case BT_REAL:
1598       read_real (dtp, kind);
1599       break;
1600     case BT_COMPLEX:
1601       read_complex (dtp, kind, size);
1602       break;
1603     default:
1604       internal_error (&dtp->common, "Bad type for list read");
1605     }
1606
1607   if (dtp->u.p.saved_type != BT_CHARACTER && dtp->u.p.saved_type != BT_NULL)
1608     dtp->u.p.saved_length = size;
1609
1610   if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1611     goto cleanup;
1612
1613  set_value:
1614   switch (dtp->u.p.saved_type)
1615     {
1616     case BT_COMPLEX:
1617     case BT_INTEGER:
1618     case BT_REAL:
1619     case BT_LOGICAL:
1620       memcpy (p, dtp->u.p.value, size);
1621       break;
1622
1623     case BT_CHARACTER:
1624       if (dtp->u.p.saved_string)
1625        {
1626           m = ((int) size < dtp->u.p.saved_used)
1627               ? (int) size : dtp->u.p.saved_used;
1628           memcpy (p, dtp->u.p.saved_string, m);
1629        }
1630       else
1631         /* Just delimiters encountered, nothing to copy but SPACE.  */
1632         m = 0;
1633
1634       if (m < (int) size)
1635         memset (((char *) p) + m, ' ', size - m);
1636       break;
1637
1638     case BT_NULL:
1639       break;
1640     }
1641
1642   if (--dtp->u.p.repeat_count <= 0)
1643     free_saved (dtp);
1644
1645 cleanup:
1646   dtp->u.p.eof_jump = NULL;
1647 }
1648
1649
1650 void
1651 list_formatted_read (st_parameter_dt *dtp, bt type, void *p, int kind,
1652                      size_t size, size_t nelems)
1653 {
1654   size_t elem;
1655   char *tmp;
1656
1657   tmp = (char *) p;
1658
1659   /* Big loop over all the elements.  */
1660   for (elem = 0; elem < nelems; elem++)
1661     {
1662       dtp->u.p.item_count++;
1663       list_formatted_read_scalar (dtp, type, tmp + size*elem, kind, size);
1664     }
1665 }
1666
1667
1668 /* Finish a list read.  */
1669
1670 void
1671 finish_list_read (st_parameter_dt *dtp)
1672 {
1673   char c;
1674
1675   free_saved (dtp);
1676
1677   if (dtp->u.p.at_eol)
1678     {
1679       dtp->u.p.at_eol = 0;
1680       return;
1681     }
1682
1683   do
1684     {
1685       c = next_char (dtp);
1686     }
1687   while (c != '\n');
1688 }
1689
1690 /*                      NAMELIST INPUT
1691
1692 void namelist_read (st_parameter_dt *dtp)
1693 calls:
1694    static void nml_match_name (char *name, int len)
1695    static int nml_query (st_parameter_dt *dtp)
1696    static int nml_get_obj_data (st_parameter_dt *dtp,
1697                                 namelist_info **prev_nl, char *)
1698 calls:
1699       static void nml_untouch_nodes (st_parameter_dt *dtp)
1700       static namelist_info * find_nml_node (st_parameter_dt *dtp,
1701                                             char * var_name)
1702       static int nml_parse_qualifier(descriptor_dimension * ad,
1703                                      array_loop_spec * ls, int rank, char *)
1704       static void nml_touch_nodes (namelist_info * nl)
1705       static int nml_read_obj (namelist_info *nl, index_type offset,
1706                                namelist_info **prev_nl, char *,
1707                                index_type clow, index_type chigh)
1708 calls:
1709       -itself-  */
1710
1711 /* Inputs a rank-dimensional qualifier, which can contain
1712    singlets, doublets, triplets or ':' with the standard meanings.  */
1713
1714 static try
1715 nml_parse_qualifier (st_parameter_dt *dtp, descriptor_dimension *ad,
1716                      array_loop_spec *ls, int rank, char *parse_err_msg,
1717                      int *parsed_rank)
1718 {
1719   int dim;
1720   int indx;
1721   int neg;
1722   int null_flag;
1723   int is_array_section, is_char;
1724   char c;
1725
1726   is_char = 0;
1727   is_array_section = 0;
1728   dtp->u.p.expanded_read = 0;
1729
1730   /* See if this is a character substring qualifier we are looking for.  */
1731   if (rank == -1)
1732     {
1733       rank = 1;
1734       is_char = 1;
1735     }
1736
1737   /* The next character in the stream should be the '('.  */
1738
1739   c = next_char (dtp);
1740
1741   /* Process the qualifier, by dimension and triplet.  */
1742
1743   for (dim=0; dim < rank; dim++ )
1744     {
1745       for (indx=0; indx<3; indx++)
1746         {
1747           free_saved (dtp);
1748           eat_spaces (dtp);
1749           neg = 0;
1750
1751           /* Process a potential sign.  */
1752           c = next_char (dtp);
1753           switch (c)
1754             {
1755             case '-':
1756               neg = 1;
1757               break;
1758
1759             case '+':
1760               break;
1761
1762             default:
1763               unget_char (dtp, c);
1764               break;
1765             }
1766
1767           /* Process characters up to the next ':' , ',' or ')'.  */
1768           for (;;)
1769             {
1770               c = next_char (dtp);
1771
1772               switch (c)
1773                 {
1774                 case ':':
1775                   is_array_section = 1;
1776                   break;
1777
1778                 case ',': case ')':
1779                   if ((c==',' && dim == rank -1)
1780                       || (c==')' && dim < rank -1))
1781                     {
1782                       if (is_char)
1783                         sprintf (parse_err_msg, "Bad substring qualifier");
1784                       else
1785                         sprintf (parse_err_msg, "Bad number of index fields");
1786                       goto err_ret;
1787                     }
1788                   break;
1789
1790                 CASE_DIGITS:
1791                   push_char (dtp, c);
1792                   continue;
1793
1794                 case ' ': case '\t':
1795                   eat_spaces (dtp);
1796                   c = next_char (dtp);
1797                   break;
1798
1799                 default:
1800                   if (is_char)
1801                     sprintf (parse_err_msg,
1802                              "Bad character in substring qualifier");
1803                   else
1804                     sprintf (parse_err_msg, "Bad character in index");
1805                   goto err_ret;
1806                 }
1807
1808               if ((c == ',' || c == ')') && indx == 0
1809                   && dtp->u.p.saved_string == 0)
1810                 {
1811                   if (is_char)
1812                     sprintf (parse_err_msg, "Null substring qualifier");
1813                   else
1814                     sprintf (parse_err_msg, "Null index field");
1815                   goto err_ret;
1816                 }
1817
1818               if ((c == ':' && indx == 1 && dtp->u.p.saved_string == 0)
1819                   || (indx == 2 && dtp->u.p.saved_string == 0))
1820                 {
1821                   if (is_char)
1822                     sprintf (parse_err_msg, "Bad substring qualifier");
1823                   else
1824                     sprintf (parse_err_msg, "Bad index triplet");
1825                   goto err_ret;
1826                 }
1827
1828               if (is_char && !is_array_section)
1829                 {
1830                   sprintf (parse_err_msg,
1831                            "Missing colon in substring qualifier");
1832                   goto err_ret;
1833                 }
1834
1835               /* If '( : ? )' or '( ? : )' break and flag read failure.  */
1836               null_flag = 0;
1837               if ((c == ':' && indx == 0 && dtp->u.p.saved_string == 0)
1838                   || (indx==1 && dtp->u.p.saved_string == 0))
1839                 {
1840                   null_flag = 1;
1841                   break;
1842                 }
1843
1844               /* Now read the index.  */
1845               if (convert_integer (dtp, sizeof(ssize_t), neg))
1846                 {
1847                   if (is_char)
1848                     sprintf (parse_err_msg, "Bad integer substring qualifier");
1849                   else
1850                     sprintf (parse_err_msg, "Bad integer in index");
1851                   goto err_ret;
1852                 }
1853               break;
1854             }
1855
1856           /* Feed the index values to the triplet arrays.  */
1857           if (!null_flag)
1858             {
1859               if (indx == 0)
1860                 memcpy (&ls[dim].start, dtp->u.p.value, sizeof(ssize_t));
1861               if (indx == 1)
1862                 memcpy (&ls[dim].end, dtp->u.p.value, sizeof(ssize_t));
1863               if (indx == 2)
1864                 memcpy (&ls[dim].step, dtp->u.p.value, sizeof(ssize_t));
1865             }
1866
1867           /* Singlet or doublet indices.  */
1868           if (c==',' || c==')')
1869             {
1870               if (indx == 0)
1871                 {
1872                   memcpy (&ls[dim].start, dtp->u.p.value, sizeof(ssize_t));
1873
1874                   /*  If -std=f95/2003 or an array section is specified,
1875                       do not allow excess data to be processed.  */
1876                   if (is_array_section == 1
1877                       || compile_options.allow_std < GFC_STD_GNU)
1878                     ls[dim].end = ls[dim].start;
1879                   else
1880                     dtp->u.p.expanded_read = 1;
1881                 }
1882
1883               /* Check for non-zero rank.  */
1884               if (is_array_section == 1 && ls[dim].start != ls[dim].end)
1885                 *parsed_rank = 1;
1886
1887               break;
1888             }
1889         }
1890
1891       /* Check the values of the triplet indices.  */
1892       if ((ls[dim].start > (ssize_t)ad[dim].ubound)
1893           || (ls[dim].start < (ssize_t)ad[dim].lbound)
1894           || (ls[dim].end > (ssize_t)ad[dim].ubound)
1895           || (ls[dim].end < (ssize_t)ad[dim].lbound))
1896         {
1897           if (is_char)
1898             sprintf (parse_err_msg, "Substring out of range");
1899           else
1900             sprintf (parse_err_msg, "Index %d out of range", dim + 1);
1901           goto err_ret;
1902         }
1903
1904       if (((ls[dim].end - ls[dim].start ) * ls[dim].step < 0)
1905           || (ls[dim].step == 0))
1906         {
1907           sprintf (parse_err_msg, "Bad range in index %d", dim + 1);
1908           goto err_ret;
1909         }
1910
1911       /* Initialise the loop index counter.  */
1912       ls[dim].idx = ls[dim].start;
1913     }
1914   eat_spaces (dtp);
1915   return SUCCESS;
1916
1917 err_ret:
1918
1919   return FAILURE;
1920 }
1921
1922 static namelist_info *
1923 find_nml_node (st_parameter_dt *dtp, char * var_name)
1924 {
1925   namelist_info * t = dtp->u.p.ionml;
1926   while (t != NULL)
1927     {
1928       if (strcmp (var_name, t->var_name) == 0)
1929         {
1930           t->touched = 1;
1931           return t;
1932         }
1933       t = t->next;
1934     }
1935   return NULL;
1936 }
1937
1938 /* Visits all the components of a derived type that have
1939    not explicitly been identified in the namelist input.
1940    touched is set and the loop specification initialised
1941    to default values  */
1942
1943 static void
1944 nml_touch_nodes (namelist_info * nl)
1945 {
1946   index_type len = strlen (nl->var_name) + 1;
1947   int dim;
1948   char * ext_name = (char*)get_mem (len + 1);
1949   memcpy (ext_name, nl->var_name, len-1);
1950   memcpy (ext_name + len - 1, "%", 2);
1951   for (nl = nl->next; nl; nl = nl->next)
1952     {
1953       if (strncmp (nl->var_name, ext_name, len) == 0)
1954         {
1955           nl->touched = 1;
1956           for (dim=0; dim < nl->var_rank; dim++)
1957             {
1958               nl->ls[dim].step = 1;
1959               nl->ls[dim].end = nl->dim[dim].ubound;
1960               nl->ls[dim].start = nl->dim[dim].lbound;
1961               nl->ls[dim].idx = nl->ls[dim].start;
1962             }
1963         }
1964       else
1965         break;
1966     }
1967   free_mem (ext_name);
1968   return;
1969 }
1970
1971 /* Resets touched for the entire list of nml_nodes, ready for a
1972    new object.  */
1973
1974 static void
1975 nml_untouch_nodes (st_parameter_dt *dtp)
1976 {
1977   namelist_info * t;
1978   for (t = dtp->u.p.ionml; t; t = t->next)
1979     t->touched = 0;
1980   return;
1981 }
1982
1983 /* Attempts to input name to namelist name.  Returns
1984    dtp->u.p.nml_read_error = 1 on no match.  */
1985
1986 static void
1987 nml_match_name (st_parameter_dt *dtp, const char *name, index_type len)
1988 {
1989   index_type i;
1990   char c;
1991   dtp->u.p.nml_read_error = 0;
1992   for (i = 0; i < len; i++)
1993     {
1994       c = next_char (dtp);
1995       if (tolower (c) != tolower (name[i]))
1996         {
1997           dtp->u.p.nml_read_error = 1;
1998           break;
1999         }
2000     }
2001 }
2002
2003 /* If the namelist read is from stdin, output the current state of the
2004    namelist to stdout.  This is used to implement the non-standard query
2005    features, ? and =?. If c == '=' the full namelist is printed. Otherwise
2006    the names alone are printed.  */
2007
2008 static void
2009 nml_query (st_parameter_dt *dtp, char c)
2010 {
2011   gfc_unit * temp_unit;
2012   namelist_info * nl;
2013   index_type len;
2014   char * p;
2015
2016   if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
2017     return;
2018
2019   /* Store the current unit and transfer to stdout.  */
2020
2021   temp_unit = dtp->u.p.current_unit;
2022   dtp->u.p.current_unit = find_unit (options.stdout_unit);
2023
2024   if (dtp->u.p.current_unit)
2025     {
2026       dtp->u.p.mode = WRITING;
2027       next_record (dtp, 0);
2028
2029       /* Write the namelist in its entirety.  */
2030
2031       if (c == '=')
2032         namelist_write (dtp);
2033
2034       /* Or write the list of names.  */
2035
2036       else
2037         {
2038           /* "&namelist_name\n"  */
2039
2040           len = dtp->namelist_name_len;
2041 #ifdef HAVE_CRLF
2042           p = write_block (dtp, len + 3);
2043 #else
2044           p = write_block (dtp, len + 2);
2045 #endif
2046           if (!p)
2047             goto query_return;
2048           memcpy (p, "&", 1);
2049           memcpy ((char*)(p + 1), dtp->namelist_name, len);
2050 #ifdef HAVE_CRLF
2051           memcpy ((char*)(p + len + 1), "\r\n", 2);
2052 #else
2053           memcpy ((char*)(p + len + 1), "\n", 1);
2054 #endif
2055           for (nl = dtp->u.p.ionml; nl; nl = nl->next)
2056             {
2057               /* " var_name\n"  */
2058
2059               len = strlen (nl->var_name);
2060 #ifdef HAVE_CRLF
2061               p = write_block (dtp, len + 3);
2062 #else
2063               p = write_block (dtp, len + 2);
2064 #endif
2065               if (!p)
2066                 goto query_return;
2067               memcpy (p, " ", 1);
2068               memcpy ((char*)(p + 1), nl->var_name, len);
2069 #ifdef HAVE_CRLF
2070               memcpy ((char*)(p + len + 1), "\r\n", 2);
2071 #else
2072               memcpy ((char*)(p + len + 1), "\n", 1);
2073 #endif
2074             }
2075
2076           /* "&end\n"  */
2077
2078 #ifdef HAVE_CRLF
2079           p = write_block (dtp, 6);
2080 #else
2081           p = write_block (dtp, 5);
2082 #endif
2083           if (!p)
2084             goto query_return;
2085 #ifdef HAVE_CRLF
2086           memcpy (p, "&end\r\n", 6);
2087 #else
2088           memcpy (p, "&end\n", 5);
2089 #endif
2090         }
2091
2092       /* Flush the stream to force immediate output.  */
2093
2094       flush (dtp->u.p.current_unit->s);
2095       unlock_unit (dtp->u.p.current_unit);
2096     }
2097
2098 query_return:
2099
2100   /* Restore the current unit.  */
2101
2102   dtp->u.p.current_unit = temp_unit;
2103   dtp->u.p.mode = READING;
2104   return;
2105 }
2106
2107 /* Reads and stores the input for the namelist object nl.  For an array,
2108    the function loops over the ranges defined by the loop specification.
2109    This default to all the data or to the specification from a qualifier.
2110    nml_read_obj recursively calls itself to read derived types. It visits
2111    all its own components but only reads data for those that were touched
2112    when the name was parsed.  If a read error is encountered, an attempt is
2113    made to return to read a new object name because the standard allows too
2114    little data to be available.  On the other hand, too much data is an
2115    error.  */
2116
2117 static try
2118 nml_read_obj (st_parameter_dt *dtp, namelist_info * nl, index_type offset,
2119               namelist_info **pprev_nl, char *nml_err_msg,
2120               index_type clow, index_type chigh)
2121 {
2122   namelist_info * cmp;
2123   char * obj_name;
2124   int nml_carry;
2125   int len;
2126   int dim;
2127   index_type dlen;
2128   index_type m;
2129   index_type obj_name_len;
2130   void * pdata;
2131
2132   /* This object not touched in name parsing.  */
2133
2134   if (!nl->touched)
2135     return SUCCESS;
2136
2137   dtp->u.p.repeat_count = 0;
2138   eat_spaces (dtp);
2139
2140   len = nl->len;
2141   switch (nl->type)
2142   {
2143     case GFC_DTYPE_INTEGER:
2144     case GFC_DTYPE_LOGICAL:
2145       dlen = len;
2146       break;
2147
2148     case GFC_DTYPE_REAL:
2149       dlen = size_from_real_kind (len);
2150       break;
2151
2152     case GFC_DTYPE_COMPLEX:
2153       dlen = size_from_complex_kind (len);
2154       break;
2155
2156     case GFC_DTYPE_CHARACTER:
2157       dlen = chigh ? (chigh - clow + 1) : nl->string_length;
2158       break;
2159
2160     default:
2161       dlen = 0;
2162     }
2163
2164   do
2165     {
2166       /* Update the pointer to the data, using the current index vector  */
2167
2168       pdata = (void*)(nl->mem_pos + offset);
2169       for (dim = 0; dim < nl->var_rank; dim++)
2170         pdata = (void*)(pdata + (nl->ls[dim].idx - nl->dim[dim].lbound) *
2171                  nl->dim[dim].stride * nl->size);
2172
2173       /* Reset the error flag and try to read next value, if
2174          dtp->u.p.repeat_count=0  */
2175
2176       dtp->u.p.nml_read_error = 0;
2177       nml_carry = 0;
2178       if (--dtp->u.p.repeat_count <= 0)
2179         {
2180           if (dtp->u.p.input_complete)
2181             return SUCCESS;
2182           if (dtp->u.p.at_eol)
2183             finish_separator (dtp);
2184           if (dtp->u.p.input_complete)
2185             return SUCCESS;
2186
2187           /* GFC_TYPE_UNKNOWN through for nulls and is detected
2188              after the switch block.  */
2189
2190           dtp->u.p.saved_type = GFC_DTYPE_UNKNOWN;
2191           free_saved (dtp);
2192
2193           switch (nl->type)
2194           {
2195           case GFC_DTYPE_INTEGER:
2196               read_integer (dtp, len);
2197               break;
2198
2199           case GFC_DTYPE_LOGICAL:
2200               read_logical (dtp, len);
2201               break;
2202
2203           case GFC_DTYPE_CHARACTER:
2204               read_character (dtp, len);
2205               break;
2206
2207           case GFC_DTYPE_REAL:
2208               read_real (dtp, len);
2209               break;
2210
2211           case GFC_DTYPE_COMPLEX:
2212               read_complex (dtp, len, dlen);
2213               break;
2214
2215           case GFC_DTYPE_DERIVED:
2216             obj_name_len = strlen (nl->var_name) + 1;
2217             obj_name = get_mem (obj_name_len+1);
2218             memcpy (obj_name, nl->var_name, obj_name_len-1);
2219             memcpy (obj_name + obj_name_len - 1, "%", 2);
2220
2221             /* If reading a derived type, disable the expanded read warning
2222                since a single object can have multiple reads.  */
2223             dtp->u.p.expanded_read = 0;
2224
2225             /* Now loop over the components. Update the component pointer
2226                with the return value from nml_write_obj.  This loop jumps
2227                past nested derived types by testing if the potential
2228                component name contains '%'.  */
2229
2230             for (cmp = nl->next;
2231                  cmp &&
2232                    !strncmp (cmp->var_name, obj_name, obj_name_len) &&
2233                    !strchr (cmp->var_name + obj_name_len, '%');
2234                  cmp = cmp->next)
2235               {
2236
2237                 if (nml_read_obj (dtp, cmp, (index_type)(pdata - nl->mem_pos),
2238                                   pprev_nl, nml_err_msg, clow, chigh)
2239                     == FAILURE)
2240                   {
2241                     free_mem (obj_name);
2242                     return FAILURE;
2243                   }
2244
2245                 if (dtp->u.p.input_complete)
2246                   {
2247                     free_mem (obj_name);
2248                     return SUCCESS;
2249                   }
2250               }
2251
2252             free_mem (obj_name);
2253             goto incr_idx;
2254
2255           default:
2256             sprintf (nml_err_msg, "Bad type for namelist object %s",
2257                         nl->var_name);
2258             internal_error (&dtp->common, nml_err_msg);
2259             goto nml_err_ret;
2260           }
2261         }
2262
2263       /* The standard permits array data to stop short of the number of
2264          elements specified in the loop specification.  In this case, we
2265          should be here with dtp->u.p.nml_read_error != 0.  Control returns to
2266          nml_get_obj_data and an attempt is made to read object name.  */
2267
2268       *pprev_nl = nl;
2269       if (dtp->u.p.nml_read_error)
2270         {
2271           dtp->u.p.expanded_read = 0;
2272           return SUCCESS;
2273         }
2274
2275       if (dtp->u.p.saved_type == GFC_DTYPE_UNKNOWN)
2276         {
2277           dtp->u.p.expanded_read = 0;
2278           goto incr_idx;
2279         }
2280
2281       /* Note the switch from GFC_DTYPE_type to BT_type at this point.
2282          This comes about because the read functions return BT_types.  */
2283
2284       switch (dtp->u.p.saved_type)
2285       {
2286
2287         case BT_COMPLEX:
2288         case BT_REAL:
2289         case BT_INTEGER:
2290         case BT_LOGICAL:
2291           memcpy (pdata, dtp->u.p.value, dlen);
2292           break;
2293
2294         case BT_CHARACTER:
2295           m = (dlen < dtp->u.p.saved_used) ? dlen : dtp->u.p.saved_used;
2296           pdata = (void*)( pdata + clow - 1 );
2297           memcpy (pdata, dtp->u.p.saved_string, m);
2298           if (m < dlen)
2299             memset ((void*)( pdata + m ), ' ', dlen - m);
2300           break;
2301
2302         default:
2303           break;
2304       }
2305
2306       /* Warn if a non-standard expanded read occurs. A single read of a
2307          single object is acceptable.  If a second read occurs, issue a warning
2308          and set the flag to zero to prevent further warnings.  */
2309       if (dtp->u.p.expanded_read == 2)
2310         {
2311           notify_std (&dtp->common, GFC_STD_GNU, "Non-standard expanded namelist read.");
2312           dtp->u.p.expanded_read = 0;
2313         }
2314
2315       /* If the expanded read warning flag is set, increment it,
2316          indicating that a single read has occurred.  */
2317       if (dtp->u.p.expanded_read >= 1)
2318         dtp->u.p.expanded_read++;
2319
2320       /* Break out of loop if scalar.  */
2321       if (!nl->var_rank)
2322         break;
2323
2324       /* Now increment the index vector.  */
2325
2326 incr_idx:
2327
2328       nml_carry = 1;
2329       for (dim = 0; dim < nl->var_rank; dim++)
2330         {
2331           nl->ls[dim].idx += nml_carry * nl->ls[dim].step;
2332           nml_carry = 0;
2333           if (((nl->ls[dim].step > 0) && (nl->ls[dim].idx > nl->ls[dim].end))
2334               ||
2335               ((nl->ls[dim].step < 0) && (nl->ls[dim].idx < nl->ls[dim].end)))
2336             {
2337               nl->ls[dim].idx = nl->ls[dim].start;
2338               nml_carry = 1;
2339             }
2340         }
2341     } while (!nml_carry);
2342
2343   if (dtp->u.p.repeat_count > 1)
2344     {
2345        sprintf (nml_err_msg, "Repeat count too large for namelist object %s" ,
2346                    nl->var_name );
2347        goto nml_err_ret;
2348     }
2349   return SUCCESS;
2350
2351 nml_err_ret:
2352
2353   return FAILURE;
2354 }
2355
2356 /* Parses the object name, including array and substring qualifiers.  It
2357    iterates over derived type components, touching those components and
2358    setting their loop specifications, if there is a qualifier.  If the
2359    object is itself a derived type, its components and subcomponents are
2360    touched.  nml_read_obj is called at the end and this reads the data in
2361    the manner specified by the object name.  */
2362
2363 static try
2364 nml_get_obj_data (st_parameter_dt *dtp, namelist_info **pprev_nl,
2365                   char *nml_err_msg)
2366 {
2367   char c;
2368   namelist_info * nl;
2369   namelist_info * first_nl = NULL;
2370   namelist_info * root_nl = NULL;
2371   int dim, parsed_rank;
2372   int component_flag;
2373   char parse_err_msg[30];
2374   index_type clow, chigh;
2375   int non_zero_rank_count;
2376
2377   /* Look for end of input or object name.  If '?' or '=?' are encountered
2378      in stdin, print the node names or the namelist to stdout.  */
2379
2380   eat_separator (dtp);
2381   if (dtp->u.p.input_complete)
2382     return SUCCESS;
2383
2384   if (dtp->u.p.at_eol)
2385     finish_separator (dtp);
2386   if (dtp->u.p.input_complete)
2387     return SUCCESS;
2388
2389   c = next_char (dtp);
2390   switch (c)
2391     {
2392     case '=':
2393       c = next_char (dtp);
2394       if (c != '?')
2395         {
2396           sprintf (nml_err_msg, "namelist read: misplaced = sign");
2397           goto nml_err_ret;
2398         }
2399       nml_query (dtp, '=');
2400       return SUCCESS;
2401
2402     case '?':
2403       nml_query (dtp, '?');
2404       return SUCCESS;
2405
2406     case '$':
2407     case '&':
2408       nml_match_name (dtp, "end", 3);
2409       if (dtp->u.p.nml_read_error)
2410         {
2411           sprintf (nml_err_msg, "namelist not terminated with / or &end");
2412           goto nml_err_ret;
2413         }
2414     case '/':
2415       dtp->u.p.input_complete = 1;
2416       return SUCCESS;
2417
2418     default :
2419       break;
2420     }
2421
2422   /* Untouch all nodes of the namelist and reset the flag that is set for
2423      derived type components.  */
2424
2425   nml_untouch_nodes (dtp);
2426   component_flag = 0;
2427   non_zero_rank_count = 0;
2428
2429   /* Get the object name - should '!' and '\n' be permitted separators?  */
2430
2431 get_name:
2432
2433   free_saved (dtp);
2434
2435   do
2436     {
2437       push_char (dtp, tolower(c));
2438       c = next_char (dtp);
2439     } while (!( c=='=' || c==' ' || c=='\t' || c =='(' || c =='%' ));
2440
2441   unget_char (dtp, c);
2442
2443   /* Check that the name is in the namelist and get pointer to object.
2444      Three error conditions exist: (i) An attempt is being made to
2445      identify a non-existent object, following a failed data read or
2446      (ii) The object name does not exist or (iii) Too many data items
2447      are present for an object.  (iii) gives the same error message
2448      as (i)  */
2449
2450   push_char (dtp, '\0');
2451
2452   if (component_flag)
2453     {
2454       size_t var_len = strlen (root_nl->var_name);
2455       size_t saved_len
2456         = dtp->u.p.saved_string ? strlen (dtp->u.p.saved_string) : 0;
2457       char ext_name[var_len + saved_len + 1];
2458
2459       memcpy (ext_name, root_nl->var_name, var_len);
2460       if (dtp->u.p.saved_string)
2461         memcpy (ext_name + var_len, dtp->u.p.saved_string, saved_len);
2462       ext_name[var_len + saved_len] = '\0';
2463       nl = find_nml_node (dtp, ext_name);
2464     }
2465   else
2466     nl = find_nml_node (dtp, dtp->u.p.saved_string);
2467
2468   if (nl == NULL)
2469     {
2470       if (dtp->u.p.nml_read_error && *pprev_nl)
2471         sprintf (nml_err_msg, "Bad data for namelist object %s",
2472                     (*pprev_nl)->var_name);
2473
2474       else
2475         sprintf (nml_err_msg, "Cannot match namelist object name %s",
2476                     dtp->u.p.saved_string);
2477
2478       goto nml_err_ret;
2479     }
2480
2481   /* Get the length, data length, base pointer and rank of the variable.
2482      Set the default loop specification first.  */
2483
2484   for (dim=0; dim < nl->var_rank; dim++)
2485     {
2486       nl->ls[dim].step = 1;
2487       nl->ls[dim].end = nl->dim[dim].ubound;
2488       nl->ls[dim].start = nl->dim[dim].lbound;
2489       nl->ls[dim].idx = nl->ls[dim].start;
2490     }
2491
2492 /* Check to see if there is a qualifier: if so, parse it.*/
2493
2494   if (c == '(' && nl->var_rank)
2495     {
2496       parsed_rank = 0;
2497       if (nml_parse_qualifier (dtp, nl->dim, nl->ls, nl->var_rank,
2498                                parse_err_msg, &parsed_rank) == FAILURE)
2499         {
2500           sprintf (nml_err_msg, "%s for namelist variable %s",
2501                       parse_err_msg, nl->var_name);
2502           goto nml_err_ret;
2503         }
2504
2505       if (parsed_rank > 0)
2506         non_zero_rank_count++;
2507
2508       c = next_char (dtp);
2509       unget_char (dtp, c);
2510     }
2511   else if (nl->var_rank > 0)
2512     non_zero_rank_count++;
2513
2514   /* Now parse a derived type component. The root namelist_info address
2515      is backed up, as is the previous component level.  The  component flag
2516      is set and the iteration is made by jumping back to get_name.  */
2517
2518   if (c == '%')
2519     {
2520       if (nl->type != GFC_DTYPE_DERIVED)
2521         {
2522           sprintf (nml_err_msg, "Attempt to get derived component for %s",
2523                       nl->var_name);
2524           goto nml_err_ret;
2525         }
2526
2527       if (!component_flag)
2528         first_nl = nl;
2529
2530       root_nl = nl;
2531       component_flag = 1;
2532       c = next_char (dtp);
2533       goto get_name;
2534     }
2535
2536   /* Parse a character qualifier, if present.  chigh = 0 is a default
2537      that signals that the string length = string_length.  */
2538
2539   clow = 1;
2540   chigh = 0;
2541
2542   if (c == '(' && nl->type == GFC_DTYPE_CHARACTER)
2543     {
2544       descriptor_dimension chd[1] = { {1, clow, nl->string_length} };
2545       array_loop_spec ind[1] = { {1, clow, nl->string_length, 1} };
2546
2547       if (nml_parse_qualifier (dtp, chd, ind, -1, parse_err_msg, &parsed_rank)
2548           == FAILURE)
2549         {
2550           sprintf (nml_err_msg, "%s for namelist variable %s",
2551                       parse_err_msg, nl->var_name);
2552           goto nml_err_ret;
2553         }
2554
2555       clow = ind[0].start;
2556       chigh = ind[0].end;
2557
2558       if (ind[0].step != 1)
2559         {
2560           sprintf (nml_err_msg,
2561                    "Step not allowed in substring qualifier"
2562                    " for namelist object %s", nl->var_name);
2563           goto nml_err_ret;
2564         }
2565
2566       c = next_char (dtp);
2567       unget_char (dtp, c);
2568     }
2569
2570   /* If a derived type touch its components and restore the root
2571      namelist_info if we have parsed a qualified derived type
2572      component.  */
2573
2574   if (nl->type == GFC_DTYPE_DERIVED)
2575     nml_touch_nodes (nl);
2576   if (component_flag)
2577     nl = first_nl;
2578
2579   /* Make sure no extraneous qualifiers are there.  */
2580
2581   if (c == '(')
2582     {
2583       sprintf (nml_err_msg, "Qualifier for a scalar or non-character"
2584                   " namelist object %s", nl->var_name);
2585       goto nml_err_ret;
2586     }
2587
2588   /* Make sure there is no more than one non-zero rank object.  */
2589   if (non_zero_rank_count > 1)
2590     {
2591       sprintf (nml_err_msg, "Multiple sub-objects with non-zero rank in"
2592                " namelist object %s", nl->var_name);
2593       non_zero_rank_count = 0;
2594       goto nml_err_ret;
2595     }
2596
2597 /* According to the standard, an equal sign MUST follow an object name. The
2598    following is possibly lax - it allows comments, blank lines and so on to
2599    intervene.  eat_spaces (dtp); c = next_char (dtp); would be compliant*/
2600
2601   free_saved (dtp);
2602
2603   eat_separator (dtp);
2604   if (dtp->u.p.input_complete)
2605     return SUCCESS;
2606
2607   if (dtp->u.p.at_eol)
2608     finish_separator (dtp);
2609   if (dtp->u.p.input_complete)
2610     return SUCCESS;
2611
2612   c = next_char (dtp);
2613
2614   if (c != '=')
2615     {
2616       sprintf (nml_err_msg, "Equal sign must follow namelist object name %s",
2617                   nl->var_name);
2618       goto nml_err_ret;
2619     }
2620
2621   if (nml_read_obj (dtp, nl, 0, pprev_nl, nml_err_msg, clow, chigh) == FAILURE)
2622     goto nml_err_ret;
2623
2624   return SUCCESS;
2625
2626 nml_err_ret:
2627
2628   return FAILURE;
2629 }
2630
2631 /* Entry point for namelist input.  Goes through input until namelist name
2632   is matched.  Then cycles through nml_get_obj_data until the input is
2633   completed or there is an error.  */
2634
2635 void
2636 namelist_read (st_parameter_dt *dtp)
2637 {
2638   char c;
2639   jmp_buf eof_jump;
2640   char nml_err_msg[100];
2641   /* Pointer to the previously read object, in case attempt is made to read
2642      new object name.  Should this fail, error message can give previous
2643      name.  */
2644   namelist_info *prev_nl = NULL;
2645
2646   dtp->u.p.namelist_mode = 1;
2647   dtp->u.p.input_complete = 0;
2648   dtp->u.p.expanded_read = 0;
2649
2650   dtp->u.p.eof_jump = &eof_jump;
2651   if (setjmp (eof_jump))
2652     {
2653       dtp->u.p.eof_jump = NULL;
2654       generate_error (&dtp->common, LIBERROR_END, NULL);
2655       return;
2656     }
2657
2658   /* Look for &namelist_name .  Skip all characters, testing for $nmlname.
2659      Exit on success or EOF. If '?' or '=?' encountered in stdin, print
2660      node names or namelist on stdout.  */
2661
2662 find_nml_name:
2663   switch (c = next_char (dtp))
2664     {
2665     case '$':
2666     case '&':
2667           break;
2668
2669     case '!':
2670       eat_line (dtp);
2671       goto find_nml_name;
2672
2673     case '=':
2674       c = next_char (dtp);
2675       if (c == '?')
2676         nml_query (dtp, '=');
2677       else
2678         unget_char (dtp, c);
2679       goto find_nml_name;
2680
2681     case '?':
2682       nml_query (dtp, '?');
2683
2684     default:
2685       goto find_nml_name;
2686     }
2687
2688   /* Match the name of the namelist.  */
2689
2690   nml_match_name (dtp, dtp->namelist_name, dtp->namelist_name_len);
2691
2692   if (dtp->u.p.nml_read_error)
2693     goto find_nml_name;
2694
2695   /* A trailing space is required, we give a little lattitude here, 10.9.1.  */ 
2696   c = next_char (dtp);
2697   if (!is_separator(c))
2698     {
2699       unget_char (dtp, c);
2700       goto find_nml_name;
2701     }
2702
2703   /* Ready to read namelist objects.  If there is an error in input
2704      from stdin, output the error message and continue.  */
2705
2706   while (!dtp->u.p.input_complete)
2707     {
2708       if (nml_get_obj_data (dtp, &prev_nl, nml_err_msg) == FAILURE)
2709         {
2710           gfc_unit *u;
2711
2712           if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
2713             goto nml_err_ret;
2714
2715           u = find_unit (options.stderr_unit);
2716           st_printf ("%s\n", nml_err_msg);
2717           if (u != NULL)
2718             {
2719               flush (u->s);
2720               unlock_unit (u);
2721             }
2722         }
2723
2724    }
2725
2726   dtp->u.p.eof_jump = NULL;
2727   free_saved (dtp);
2728   free_line (dtp);
2729   return;
2730
2731   /* All namelist error calls return from here */
2732
2733 nml_err_ret:
2734
2735   dtp->u.p.eof_jump = NULL;
2736   free_saved (dtp);
2737   free_line (dtp);
2738   generate_error (&dtp->common, LIBERROR_READ_VALUE, nml_err_msg);
2739   return;
2740 }