OSDN Git Service

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