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