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