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