OSDN Git Service

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