OSDN Git Service

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