OSDN Git Service

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