OSDN Git Service

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