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