OSDN Git Service

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