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