OSDN Git Service

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