OSDN Git Service

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