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