stabilize store merging
[official-gcc.git] / libgfortran / io / list_read.c
blob7f57ff1a91606629e9658d11068bd30892edcab1
1 /* Copyright (C) 2002-2017 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
6 This file is part of the GNU Fortran runtime library (libgfortran).
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.
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.
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.
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/>. */
28 #include "io.h"
29 #include "fbuf.h"
30 #include "unix.h"
31 #include <string.h>
32 #include <ctype.h>
34 typedef unsigned char uchar;
37 /* List directed input. Several parsing subroutines are practically
38 reimplemented from formatted input, the reason being that there are
39 all kinds of small differences between formatted and list directed
40 parsing. */
43 /* Subroutines for reading characters from the input. Because a
44 repeat count is ambiguous with an integer, we have to read the
45 whole digit string before seeing if there is a '*' which signals
46 the repeat count. Since we can have a lot of potential leading
47 zeros, we have to be able to back up by arbitrary amount. Because
48 the input might not be seekable, we have to buffer the data
49 ourselves. */
51 #define CASE_DIGITS case '0': case '1': case '2': case '3': case '4': \
52 case '5': case '6': case '7': case '8': case '9'
54 #define CASE_SEPARATORS case ' ': case ',': case '/': case '\n': \
55 case '\t': case '\r': case ';'
57 /* This macro assumes that we're operating on a variable. */
59 #define is_separator(c) (c == '/' || c == ',' || c == '\n' || c == ' ' \
60 || c == '\t' || c == '\r' || c == ';' || \
61 (dtp->u.p.namelist_mode && c == '!'))
63 /* Maximum repeat count. Less than ten times the maximum signed int32. */
65 #define MAX_REPEAT 200000000
68 #define MSGLEN 100
71 /* Wrappers for calling the current worker functions. */
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))
76 /* Worker function to save a default KIND=1 character to a string
77 buffer, enlarging it as necessary. */
79 static void
80 push_char_default (st_parameter_dt *dtp, int c)
84 if (dtp->u.p.saved_string == NULL)
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;
92 if (dtp->u.p.saved_used >= dtp->u.p.saved_length)
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);
99 dtp->u.p.saved_string[dtp->u.p.saved_used++] = (char) c;
103 /* Worker function to save a KIND=4 character to a string buffer,
104 enlarging the buffer as necessary. */
105 static void
106 push_char4 (st_parameter_dt *dtp, int c)
108 gfc_char4_t *p = (gfc_char4_t *) dtp->u.p.saved_string;
110 if (p == NULL)
112 dtp->u.p.saved_string = xcalloc (SCRATCH_SIZE, sizeof (gfc_char4_t));
113 dtp->u.p.saved_length = SCRATCH_SIZE;
114 dtp->u.p.saved_used = 0;
115 p = (gfc_char4_t *) dtp->u.p.saved_string;
118 if (dtp->u.p.saved_used >= dtp->u.p.saved_length)
120 dtp->u.p.saved_length = 2 * dtp->u.p.saved_length;
121 dtp->u.p.saved_string =
122 xrealloc (dtp->u.p.saved_string,
123 dtp->u.p.saved_length * sizeof (gfc_char4_t));
124 p = (gfc_char4_t *) dtp->u.p.saved_string;
127 p[dtp->u.p.saved_used++] = c;
131 /* Free the input buffer if necessary. */
133 static void
134 free_saved (st_parameter_dt *dtp)
136 if (dtp->u.p.saved_string == NULL)
137 return;
139 free (dtp->u.p.saved_string);
141 dtp->u.p.saved_string = NULL;
142 dtp->u.p.saved_used = 0;
146 /* Free the line buffer if necessary. */
148 static void
149 free_line (st_parameter_dt *dtp)
151 dtp->u.p.line_buffer_pos = 0;
152 dtp->u.p.line_buffer_enabled = 0;
154 if (dtp->u.p.line_buffer == NULL)
155 return;
157 free (dtp->u.p.line_buffer);
158 dtp->u.p.line_buffer = NULL;
162 /* Unget saves the last character so when reading the next character,
163 we need to check to see if there is a character waiting. Similar,
164 if the line buffer is being used to read_logical, check it too. */
166 static int
167 check_buffers (st_parameter_dt *dtp)
169 int c;
171 c = '\0';
172 if (dtp->u.p.current_unit->last_char != EOF - 1)
174 dtp->u.p.at_eol = 0;
175 c = dtp->u.p.current_unit->last_char;
176 dtp->u.p.current_unit->last_char = EOF - 1;
177 goto done;
180 /* Read from line_buffer if enabled. */
182 if (dtp->u.p.line_buffer_enabled)
184 dtp->u.p.at_eol = 0;
186 c = dtp->u.p.line_buffer[dtp->u.p.line_buffer_pos];
187 if (c != '\0' && dtp->u.p.line_buffer_pos < 64)
189 dtp->u.p.line_buffer[dtp->u.p.line_buffer_pos] = '\0';
190 dtp->u.p.line_buffer_pos++;
191 goto done;
194 dtp->u.p.line_buffer_pos = 0;
195 dtp->u.p.line_buffer_enabled = 0;
198 done:
199 dtp->u.p.at_eol = (c == '\n' || c == '\r' || c == EOF);
200 return c;
204 /* Worker function for default character encoded file. */
205 static int
206 next_char_default (st_parameter_dt *dtp)
208 int c;
210 /* Always check the unget and line buffer first. */
211 if ((c = check_buffers (dtp)))
212 return c;
214 c = fbuf_getc (dtp->u.p.current_unit);
215 if (c != EOF && is_stream_io (dtp))
216 dtp->u.p.current_unit->strm_pos++;
218 dtp->u.p.at_eol = (c == '\n' || c == EOF);
219 return c;
223 /* Worker function for internal and array I/O units. */
224 static int
225 next_char_internal (st_parameter_dt *dtp)
227 ssize_t length;
228 gfc_offset record;
229 int c;
231 /* Always check the unget and line buffer first. */
232 if ((c = check_buffers (dtp)))
233 return c;
235 /* Handle the end-of-record and end-of-file conditions for
236 internal array unit. */
237 if (is_array_io (dtp))
239 if (dtp->u.p.at_eof)
240 return EOF;
242 /* Check for "end-of-record" condition. */
243 if (dtp->u.p.current_unit->bytes_left == 0)
245 int finished;
247 c = '\n';
248 record = next_array_record (dtp, dtp->u.p.current_unit->ls,
249 &finished);
251 /* Check for "end-of-file" condition. */
252 if (finished)
254 dtp->u.p.at_eof = 1;
255 goto done;
258 record *= dtp->u.p.current_unit->recl;
259 if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
260 return EOF;
262 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
263 goto done;
267 /* Get the next character and handle end-of-record conditions. */
269 if (is_char4_unit(dtp)) /* Check for kind=4 internal unit. */
270 length = sread (dtp->u.p.current_unit->s, &c, 1);
271 else
273 char cc;
274 length = sread (dtp->u.p.current_unit->s, &cc, 1);
275 c = cc;
278 if (unlikely (length < 0))
280 generate_error (&dtp->common, LIBERROR_OS, NULL);
281 return '\0';
284 if (is_array_io (dtp))
286 /* Check whether we hit EOF. */
287 if (unlikely (length == 0))
289 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
290 return '\0';
292 dtp->u.p.current_unit->bytes_left--;
294 else
296 if (dtp->u.p.at_eof)
297 return EOF;
298 if (length == 0)
300 c = '\n';
301 dtp->u.p.at_eof = 1;
305 done:
306 dtp->u.p.at_eol = (c == '\n' || c == EOF);
307 return c;
311 /* Worker function for UTF encoded files. */
312 static int
313 next_char_utf8 (st_parameter_dt *dtp)
315 static const uchar masks[6] = { 0x7F, 0x1F, 0x0F, 0x07, 0x02, 0x01 };
316 static const uchar patns[6] = { 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
317 int i, nb;
318 gfc_char4_t c;
320 /* Always check the unget and line buffer first. */
321 if (!(c = check_buffers (dtp)))
322 c = fbuf_getc (dtp->u.p.current_unit);
324 if (c < 0x80)
325 goto utf_done;
327 /* The number of leading 1-bits in the first byte indicates how many
328 bytes follow. */
329 for (nb = 2; nb < 7; nb++)
330 if ((c & ~masks[nb-1]) == patns[nb-1])
331 goto found;
332 goto invalid;
334 found:
335 c = (c & masks[nb-1]);
337 /* Decode the bytes read. */
338 for (i = 1; i < nb; i++)
340 gfc_char4_t n = fbuf_getc (dtp->u.p.current_unit);
341 if ((n & 0xC0) != 0x80)
342 goto invalid;
343 c = ((c << 6) + (n & 0x3F));
346 /* Make sure the shortest possible encoding was used. */
347 if (c <= 0x7F && nb > 1) goto invalid;
348 if (c <= 0x7FF && nb > 2) goto invalid;
349 if (c <= 0xFFFF && nb > 3) goto invalid;
350 if (c <= 0x1FFFFF && nb > 4) goto invalid;
351 if (c <= 0x3FFFFFF && nb > 5) goto invalid;
353 /* Make sure the character is valid. */
354 if (c > 0x7FFFFFFF || (c >= 0xD800 && c <= 0xDFFF))
355 goto invalid;
357 utf_done:
358 dtp->u.p.at_eol = (c == '\n' || c == (gfc_char4_t) EOF);
359 return (int) c;
361 invalid:
362 generate_error (&dtp->common, LIBERROR_READ_VALUE, "Invalid UTF-8 encoding");
363 return (gfc_char4_t) '?';
366 /* Push a character back onto the input. */
368 static void
369 unget_char (st_parameter_dt *dtp, int c)
371 dtp->u.p.current_unit->last_char = c;
375 /* Skip over spaces in the input. Returns the nonspace character that
376 terminated the eating and also places it back on the input. */
378 static int
379 eat_spaces (st_parameter_dt *dtp)
381 int c;
383 /* If internal character array IO, peak ahead and seek past spaces.
384 This is an optimization unique to character arrays with large
385 character lengths (PR38199). This code eliminates numerous calls
386 to next_character. */
387 if (is_array_io (dtp) && (dtp->u.p.current_unit->last_char == EOF - 1))
389 gfc_offset offset = stell (dtp->u.p.current_unit->s);
390 gfc_offset i;
392 if (is_char4_unit(dtp)) /* kind=4 */
394 for (i = 0; i < dtp->u.p.current_unit->bytes_left; i++)
396 if (dtp->internal_unit[(offset + i) * sizeof (gfc_char4_t)]
397 != (gfc_char4_t)' ')
398 break;
401 else
403 for (i = 0; i < dtp->u.p.current_unit->bytes_left; i++)
405 if (dtp->internal_unit[offset + i] != ' ')
406 break;
410 if (i != 0)
412 sseek (dtp->u.p.current_unit->s, offset + i, SEEK_SET);
413 dtp->u.p.current_unit->bytes_left -= i;
417 /* Now skip spaces, EOF and EOL are handled in next_char. */
419 c = next_char (dtp);
420 while (c != EOF && (c == ' ' || c == '\r' || c == '\t'));
422 unget_char (dtp, c);
423 return c;
427 /* This function reads characters through to the end of the current
428 line and just ignores them. Returns 0 for success and LIBERROR_END
429 if it hit EOF. */
431 static int
432 eat_line (st_parameter_dt *dtp)
434 int c;
437 c = next_char (dtp);
438 while (c != EOF && c != '\n');
439 if (c == EOF)
440 return LIBERROR_END;
441 return 0;
445 /* Skip over a separator. Technically, we don't always eat the whole
446 separator. This is because if we've processed the last input item,
447 then a separator is unnecessary. Plus the fact that operating
448 systems usually deliver console input on a line basis.
450 The upshot is that if we see a newline as part of reading a
451 separator, we stop reading. If there are more input items, we
452 continue reading the separator with finish_separator() which takes
453 care of the fact that we may or may not have seen a comma as part
454 of the separator.
456 Returns 0 for success, and non-zero error code otherwise. */
458 static int
459 eat_separator (st_parameter_dt *dtp)
461 int c, n;
462 int err = 0;
464 eat_spaces (dtp);
465 dtp->u.p.comma_flag = 0;
467 if ((c = next_char (dtp)) == EOF)
468 return LIBERROR_END;
469 switch (c)
471 case ',':
472 if (dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
474 unget_char (dtp, c);
475 break;
477 /* Fall through. */
478 case ';':
479 dtp->u.p.comma_flag = 1;
480 eat_spaces (dtp);
481 break;
483 case '/':
484 dtp->u.p.input_complete = 1;
485 break;
487 case '\r':
488 if ((n = next_char(dtp)) == EOF)
489 return LIBERROR_END;
490 if (n != '\n')
492 unget_char (dtp, n);
493 break;
495 /* Fall through. */
496 case '\n':
497 dtp->u.p.at_eol = 1;
498 if (dtp->u.p.namelist_mode)
502 if ((c = next_char (dtp)) == EOF)
503 return LIBERROR_END;
504 if (c == '!')
506 err = eat_line (dtp);
507 if (err)
508 return err;
509 c = '\n';
512 while (c == '\n' || c == '\r' || c == ' ' || c == '\t');
513 unget_char (dtp, c);
515 break;
517 case '!':
518 /* Eat a namelist comment. */
519 if (dtp->u.p.namelist_mode)
521 err = eat_line (dtp);
522 if (err)
523 return err;
525 break;
528 /* Fall Through... */
530 default:
531 unget_char (dtp, c);
532 break;
534 return err;
538 /* Finish processing a separator that was interrupted by a newline.
539 If we're here, then another data item is present, so we finish what
540 we started on the previous line. Return 0 on success, error code
541 on failure. */
543 static int
544 finish_separator (st_parameter_dt *dtp)
546 int c;
547 int err = LIBERROR_OK;
549 restart:
550 eat_spaces (dtp);
552 if ((c = next_char (dtp)) == EOF)
553 return LIBERROR_END;
554 switch (c)
556 case ',':
557 if (dtp->u.p.comma_flag)
558 unget_char (dtp, c);
559 else
561 if ((c = eat_spaces (dtp)) == EOF)
562 return LIBERROR_END;
563 if (c == '\n' || c == '\r')
564 goto restart;
567 break;
569 case '/':
570 dtp->u.p.input_complete = 1;
571 if (!dtp->u.p.namelist_mode)
572 return err;
573 break;
575 case '\n':
576 case '\r':
577 goto restart;
579 case '!':
580 if (dtp->u.p.namelist_mode)
582 err = eat_line (dtp);
583 if (err)
584 return err;
585 goto restart;
587 /* Fall through. */
588 default:
589 unget_char (dtp, c);
590 break;
592 return err;
596 /* This function is needed to catch bad conversions so that namelist can
597 attempt to see if dtp->u.p.saved_string contains a new object name rather
598 than a bad value. */
600 static int
601 nml_bad_return (st_parameter_dt *dtp, char c)
603 if (dtp->u.p.namelist_mode)
605 dtp->u.p.nml_read_error = 1;
606 unget_char (dtp, c);
607 return 1;
609 return 0;
612 /* Convert an unsigned string to an integer. The length value is -1
613 if we are working on a repeat count. Returns nonzero if we have a
614 range problem. As a side effect, frees the dtp->u.p.saved_string. */
616 static int
617 convert_integer (st_parameter_dt *dtp, int length, int negative)
619 char c, *buffer, message[MSGLEN];
620 int m;
621 GFC_UINTEGER_LARGEST v, max, max10;
622 GFC_INTEGER_LARGEST value;
624 buffer = dtp->u.p.saved_string;
625 v = 0;
627 if (length == -1)
628 max = MAX_REPEAT;
629 else
631 max = si_max (length);
632 if (negative)
633 max++;
635 max10 = max / 10;
637 for (;;)
639 c = *buffer++;
640 if (c == '\0')
641 break;
642 c -= '0';
644 if (v > max10)
645 goto overflow;
646 v = 10 * v;
648 if (v > max - c)
649 goto overflow;
650 v += c;
653 m = 0;
655 if (length != -1)
657 if (negative)
658 value = -v;
659 else
660 value = v;
661 set_integer (dtp->u.p.value, value, length);
663 else
665 dtp->u.p.repeat_count = v;
667 if (dtp->u.p.repeat_count == 0)
669 snprintf (message, MSGLEN, "Zero repeat count in item %d of list input",
670 dtp->u.p.item_count);
672 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
673 m = 1;
677 free_saved (dtp);
678 return m;
680 overflow:
681 if (length == -1)
682 snprintf (message, MSGLEN, "Repeat count overflow in item %d of list input",
683 dtp->u.p.item_count);
684 else
685 snprintf (message, MSGLEN, "Integer overflow while reading item %d",
686 dtp->u.p.item_count);
688 free_saved (dtp);
689 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
691 return 1;
695 /* Parse a repeat count for logical and complex values which cannot
696 begin with a digit. Returns nonzero if we are done, zero if we
697 should continue on. */
699 static int
700 parse_repeat (st_parameter_dt *dtp)
702 char message[MSGLEN];
703 int c, repeat;
705 if ((c = next_char (dtp)) == EOF)
706 goto bad_repeat;
707 switch (c)
709 CASE_DIGITS:
710 repeat = c - '0';
711 break;
713 CASE_SEPARATORS:
714 unget_char (dtp, c);
715 eat_separator (dtp);
716 return 1;
718 default:
719 unget_char (dtp, c);
720 return 0;
723 for (;;)
725 c = next_char (dtp);
726 switch (c)
728 CASE_DIGITS:
729 repeat = 10 * repeat + c - '0';
731 if (repeat > MAX_REPEAT)
733 snprintf (message, MSGLEN,
734 "Repeat count overflow in item %d of list input",
735 dtp->u.p.item_count);
737 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
738 return 1;
741 break;
743 case '*':
744 if (repeat == 0)
746 snprintf (message, MSGLEN,
747 "Zero repeat count in item %d of list input",
748 dtp->u.p.item_count);
750 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
751 return 1;
754 goto done;
756 default:
757 goto bad_repeat;
761 done:
762 dtp->u.p.repeat_count = repeat;
763 return 0;
765 bad_repeat:
767 free_saved (dtp);
768 if (c == EOF)
770 free_line (dtp);
771 hit_eof (dtp);
772 return 1;
774 else
775 eat_line (dtp);
776 snprintf (message, MSGLEN, "Bad repeat count in item %d of list input",
777 dtp->u.p.item_count);
778 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
779 return 1;
783 /* To read a logical we have to look ahead in the input stream to make sure
784 there is not an equal sign indicating a variable name. To do this we use
785 line_buffer to point to a temporary buffer, pushing characters there for
786 possible later reading. */
788 static void
789 l_push_char (st_parameter_dt *dtp, char c)
791 if (dtp->u.p.line_buffer == NULL)
792 dtp->u.p.line_buffer = xcalloc (SCRATCH_SIZE, 1);
794 dtp->u.p.line_buffer[dtp->u.p.line_buffer_pos++] = c;
798 /* Read a logical character on the input. */
800 static void
801 read_logical (st_parameter_dt *dtp, int length)
803 char message[MSGLEN];
804 int c, i, v;
806 if (parse_repeat (dtp))
807 return;
809 c = tolower (next_char (dtp));
810 l_push_char (dtp, c);
811 switch (c)
813 case 't':
814 v = 1;
815 c = next_char (dtp);
816 l_push_char (dtp, c);
818 if (!is_separator(c) && c != EOF)
819 goto possible_name;
821 unget_char (dtp, c);
822 break;
823 case 'f':
824 v = 0;
825 c = next_char (dtp);
826 l_push_char (dtp, c);
828 if (!is_separator(c) && c != EOF)
829 goto possible_name;
831 unget_char (dtp, c);
832 break;
834 case '.':
835 c = tolower (next_char (dtp));
836 switch (c)
838 case 't':
839 v = 1;
840 break;
841 case 'f':
842 v = 0;
843 break;
844 default:
845 goto bad_logical;
848 break;
850 case '!':
851 if (!dtp->u.p.namelist_mode)
852 goto bad_logical;
854 CASE_SEPARATORS:
855 case EOF:
856 unget_char (dtp, c);
857 eat_separator (dtp);
858 return; /* Null value. */
860 default:
861 /* Save the character in case it is the beginning
862 of the next object name. */
863 unget_char (dtp, c);
864 goto bad_logical;
867 dtp->u.p.saved_type = BT_LOGICAL;
868 dtp->u.p.saved_length = length;
870 /* Eat trailing garbage. */
872 c = next_char (dtp);
873 while (c != EOF && !is_separator (c));
875 unget_char (dtp, c);
876 eat_separator (dtp);
877 set_integer ((int *) dtp->u.p.value, v, length);
878 free_line (dtp);
880 return;
882 possible_name:
884 for(i = 0; i < 63; i++)
886 c = next_char (dtp);
887 if (is_separator(c))
889 /* All done if this is not a namelist read. */
890 if (!dtp->u.p.namelist_mode)
891 goto logical_done;
893 unget_char (dtp, c);
894 eat_separator (dtp);
895 c = next_char (dtp);
896 if (c != '=')
898 unget_char (dtp, c);
899 goto logical_done;
903 l_push_char (dtp, c);
904 if (c == '=')
906 dtp->u.p.nml_read_error = 1;
907 dtp->u.p.line_buffer_enabled = 1;
908 dtp->u.p.line_buffer_pos = 0;
909 return;
914 bad_logical:
916 if (nml_bad_return (dtp, c))
918 free_line (dtp);
919 return;
923 free_saved (dtp);
924 if (c == EOF)
926 free_line (dtp);
927 hit_eof (dtp);
928 return;
930 else if (c != '\n')
931 eat_line (dtp);
932 snprintf (message, MSGLEN, "Bad logical value while reading item %d",
933 dtp->u.p.item_count);
934 free_line (dtp);
935 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
936 return;
938 logical_done:
940 dtp->u.p.saved_type = BT_LOGICAL;
941 dtp->u.p.saved_length = length;
942 set_integer ((int *) dtp->u.p.value, v, length);
943 free_saved (dtp);
944 free_line (dtp);
948 /* Reading integers is tricky because we can actually be reading a
949 repeat count. We have to store the characters in a buffer because
950 we could be reading an integer that is larger than the default int
951 used for repeat counts. */
953 static void
954 read_integer (st_parameter_dt *dtp, int length)
956 char message[MSGLEN];
957 int c, negative;
959 negative = 0;
961 c = next_char (dtp);
962 switch (c)
964 case '-':
965 negative = 1;
966 /* Fall through... */
968 case '+':
969 if ((c = next_char (dtp)) == EOF)
970 goto bad_integer;
971 goto get_integer;
973 case '!':
974 if (!dtp->u.p.namelist_mode)
975 goto bad_integer;
977 CASE_SEPARATORS: /* Single null. */
978 unget_char (dtp, c);
979 eat_separator (dtp);
980 return;
982 CASE_DIGITS:
983 push_char (dtp, c);
984 break;
986 default:
987 goto bad_integer;
990 /* Take care of what may be a repeat count. */
992 for (;;)
994 c = next_char (dtp);
995 switch (c)
997 CASE_DIGITS:
998 push_char (dtp, c);
999 break;
1001 case '*':
1002 push_char (dtp, '\0');
1003 goto repeat;
1005 case '!':
1006 if (!dtp->u.p.namelist_mode)
1007 goto bad_integer;
1009 CASE_SEPARATORS: /* Not a repeat count. */
1010 case EOF:
1011 goto done;
1013 default:
1014 goto bad_integer;
1018 repeat:
1019 if (convert_integer (dtp, -1, 0))
1020 return;
1022 /* Get the real integer. */
1024 if ((c = next_char (dtp)) == EOF)
1025 goto bad_integer;
1026 switch (c)
1028 CASE_DIGITS:
1029 break;
1031 case '!':
1032 if (!dtp->u.p.namelist_mode)
1033 goto bad_integer;
1035 CASE_SEPARATORS:
1036 unget_char (dtp, c);
1037 eat_separator (dtp);
1038 return;
1040 case '-':
1041 negative = 1;
1042 /* Fall through... */
1044 case '+':
1045 c = next_char (dtp);
1046 break;
1049 get_integer:
1050 if (!isdigit (c))
1051 goto bad_integer;
1052 push_char (dtp, c);
1054 for (;;)
1056 c = next_char (dtp);
1057 switch (c)
1059 CASE_DIGITS:
1060 push_char (dtp, c);
1061 break;
1063 case '!':
1064 if (!dtp->u.p.namelist_mode)
1065 goto bad_integer;
1067 CASE_SEPARATORS:
1068 case EOF:
1069 goto done;
1071 default:
1072 goto bad_integer;
1076 bad_integer:
1078 if (nml_bad_return (dtp, c))
1079 return;
1081 free_saved (dtp);
1082 if (c == EOF)
1084 free_line (dtp);
1085 hit_eof (dtp);
1086 return;
1088 else if (c != '\n')
1089 eat_line (dtp);
1091 snprintf (message, MSGLEN, "Bad integer for item %d in list input",
1092 dtp->u.p.item_count);
1093 free_line (dtp);
1094 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1096 return;
1098 done:
1099 unget_char (dtp, c);
1100 eat_separator (dtp);
1102 push_char (dtp, '\0');
1103 if (convert_integer (dtp, length, negative))
1105 free_saved (dtp);
1106 return;
1109 free_saved (dtp);
1110 dtp->u.p.saved_type = BT_INTEGER;
1114 /* Read a character variable. */
1116 static void
1117 read_character (st_parameter_dt *dtp, int length __attribute__ ((unused)))
1119 char quote, message[MSGLEN];
1120 int c;
1122 quote = ' '; /* Space means no quote character. */
1124 if ((c = next_char (dtp)) == EOF)
1125 goto eof;
1126 switch (c)
1128 CASE_DIGITS:
1129 push_char (dtp, c);
1130 break;
1132 CASE_SEPARATORS:
1133 case EOF:
1134 unget_char (dtp, c); /* NULL value. */
1135 eat_separator (dtp);
1136 return;
1138 case '"':
1139 case '\'':
1140 quote = c;
1141 goto get_string;
1143 default:
1144 if (dtp->u.p.namelist_mode)
1146 unget_char (dtp, c);
1147 return;
1149 push_char (dtp, c);
1150 goto get_string;
1153 /* Deal with a possible repeat count. */
1155 for (;;)
1157 c = next_char (dtp);
1158 switch (c)
1160 CASE_DIGITS:
1161 push_char (dtp, c);
1162 break;
1164 CASE_SEPARATORS:
1165 case EOF:
1166 unget_char (dtp, c);
1167 goto done; /* String was only digits! */
1169 case '*':
1170 push_char (dtp, '\0');
1171 goto got_repeat;
1173 default:
1174 push_char (dtp, c);
1175 goto get_string; /* Not a repeat count after all. */
1179 got_repeat:
1180 if (convert_integer (dtp, -1, 0))
1181 return;
1183 /* Now get the real string. */
1185 if ((c = next_char (dtp)) == EOF)
1186 goto eof;
1187 switch (c)
1189 CASE_SEPARATORS:
1190 unget_char (dtp, c); /* Repeated NULL values. */
1191 eat_separator (dtp);
1192 return;
1194 case '"':
1195 case '\'':
1196 quote = c;
1197 break;
1199 default:
1200 push_char (dtp, c);
1201 break;
1204 get_string:
1206 for (;;)
1208 if ((c = next_char (dtp)) == EOF)
1209 goto done_eof;
1210 switch (c)
1212 case '"':
1213 case '\'':
1214 if (c != quote)
1216 push_char (dtp, c);
1217 break;
1220 /* See if we have a doubled quote character or the end of
1221 the string. */
1223 if ((c = next_char (dtp)) == EOF)
1224 goto done_eof;
1225 if (c == quote)
1227 push_char (dtp, quote);
1228 break;
1231 unget_char (dtp, c);
1232 goto done;
1234 CASE_SEPARATORS:
1235 if (quote == ' ')
1237 unget_char (dtp, c);
1238 goto done;
1241 if (c != '\n' && c != '\r')
1242 push_char (dtp, c);
1243 break;
1245 default:
1246 push_char (dtp, c);
1247 break;
1251 /* At this point, we have to have a separator, or else the string is
1252 invalid. */
1253 done:
1254 c = next_char (dtp);
1255 done_eof:
1256 if (is_separator (c) || c == EOF)
1258 unget_char (dtp, c);
1259 eat_separator (dtp);
1260 dtp->u.p.saved_type = BT_CHARACTER;
1262 else
1264 free_saved (dtp);
1265 snprintf (message, MSGLEN, "Invalid string input in item %d",
1266 dtp->u.p.item_count);
1267 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1269 free_line (dtp);
1270 return;
1272 eof:
1273 free_saved (dtp);
1274 free_line (dtp);
1275 hit_eof (dtp);
1279 /* Parse a component of a complex constant or a real number that we
1280 are sure is already there. This is a straight real number parser. */
1282 static int
1283 parse_real (st_parameter_dt *dtp, void *buffer, int length)
1285 char message[MSGLEN];
1286 int c, m, seen_dp;
1288 if ((c = next_char (dtp)) == EOF)
1289 goto bad;
1291 if (c == '-' || c == '+')
1293 push_char (dtp, c);
1294 if ((c = next_char (dtp)) == EOF)
1295 goto bad;
1298 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1299 c = '.';
1301 if (!isdigit (c) && c != '.')
1303 if (c == 'i' || c == 'I' || c == 'n' || c == 'N')
1304 goto inf_nan;
1305 else
1306 goto bad;
1309 push_char (dtp, c);
1311 seen_dp = (c == '.') ? 1 : 0;
1313 for (;;)
1315 if ((c = next_char (dtp)) == EOF)
1316 goto bad;
1317 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1318 c = '.';
1319 switch (c)
1321 CASE_DIGITS:
1322 push_char (dtp, c);
1323 break;
1325 case '.':
1326 if (seen_dp)
1327 goto bad;
1329 seen_dp = 1;
1330 push_char (dtp, c);
1331 break;
1333 case 'e':
1334 case 'E':
1335 case 'd':
1336 case 'D':
1337 case 'q':
1338 case 'Q':
1339 push_char (dtp, 'e');
1340 goto exp1;
1342 case '-':
1343 case '+':
1344 push_char (dtp, 'e');
1345 push_char (dtp, c);
1346 if ((c = next_char (dtp)) == EOF)
1347 goto bad;
1348 goto exp2;
1350 case '!':
1351 if (!dtp->u.p.namelist_mode)
1352 goto bad;
1354 CASE_SEPARATORS:
1355 case EOF:
1356 goto done;
1358 default:
1359 goto done;
1363 exp1:
1364 if ((c = next_char (dtp)) == EOF)
1365 goto bad;
1366 if (c != '-' && c != '+')
1367 push_char (dtp, '+');
1368 else
1370 push_char (dtp, c);
1371 c = next_char (dtp);
1374 exp2:
1375 if (!isdigit (c))
1377 /* Extension: allow default exponent of 0 when omitted. */
1378 if (dtp->common.flags & IOPARM_DT_DEFAULT_EXP)
1380 push_char (dtp, '0');
1381 goto done;
1383 else
1384 goto bad_exponent;
1387 push_char (dtp, c);
1389 for (;;)
1391 if ((c = next_char (dtp)) == EOF)
1392 goto bad;
1393 switch (c)
1395 CASE_DIGITS:
1396 push_char (dtp, c);
1397 break;
1399 case '!':
1400 if (!dtp->u.p.namelist_mode)
1401 goto bad;
1403 CASE_SEPARATORS:
1404 case EOF:
1405 unget_char (dtp, c);
1406 goto done;
1408 default:
1409 goto done;
1413 done:
1414 unget_char (dtp, c);
1415 push_char (dtp, '\0');
1417 m = convert_real (dtp, buffer, dtp->u.p.saved_string, length);
1418 free_saved (dtp);
1420 return m;
1422 done_infnan:
1423 unget_char (dtp, c);
1424 push_char (dtp, '\0');
1426 m = convert_infnan (dtp, buffer, dtp->u.p.saved_string, length);
1427 free_saved (dtp);
1429 return m;
1431 inf_nan:
1432 /* Match INF and Infinity. */
1433 if ((c == 'i' || c == 'I')
1434 && ((c = next_char (dtp)) == 'n' || c == 'N')
1435 && ((c = next_char (dtp)) == 'f' || c == 'F'))
1437 c = next_char (dtp);
1438 if ((c != 'i' && c != 'I')
1439 || ((c == 'i' || c == 'I')
1440 && ((c = next_char (dtp)) == 'n' || c == 'N')
1441 && ((c = next_char (dtp)) == 'i' || c == 'I')
1442 && ((c = next_char (dtp)) == 't' || c == 'T')
1443 && ((c = next_char (dtp)) == 'y' || c == 'Y')
1444 && (c = next_char (dtp))))
1446 if (is_separator (c) || (c == EOF))
1447 unget_char (dtp, c);
1448 push_char (dtp, 'i');
1449 push_char (dtp, 'n');
1450 push_char (dtp, 'f');
1451 goto done_infnan;
1453 } /* Match NaN. */
1454 else if (((c = next_char (dtp)) == 'a' || c == 'A')
1455 && ((c = next_char (dtp)) == 'n' || c == 'N')
1456 && (c = next_char (dtp)))
1458 if (is_separator (c) || (c == EOF))
1459 unget_char (dtp, c);
1460 push_char (dtp, 'n');
1461 push_char (dtp, 'a');
1462 push_char (dtp, 'n');
1464 /* Match "NAN(alphanum)". */
1465 if (c == '(')
1467 for ( ; c != ')'; c = next_char (dtp))
1468 if (is_separator (c))
1469 goto bad;
1471 c = next_char (dtp);
1472 if (is_separator (c) || (c == EOF))
1473 unget_char (dtp, c);
1475 goto done_infnan;
1478 bad:
1480 if (nml_bad_return (dtp, c))
1481 return 0;
1483 bad_exponent:
1485 free_saved (dtp);
1486 if (c == EOF)
1488 free_line (dtp);
1489 hit_eof (dtp);
1490 return 1;
1492 else if (c != '\n')
1493 eat_line (dtp);
1495 snprintf (message, MSGLEN, "Bad complex floating point "
1496 "number for item %d", dtp->u.p.item_count);
1497 free_line (dtp);
1498 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1500 return 1;
1504 /* Reading a complex number is straightforward because we can tell
1505 what it is right away. */
1507 static void
1508 read_complex (st_parameter_dt *dtp, void * dest, int kind, size_t size)
1510 char message[MSGLEN];
1511 int c;
1513 if (parse_repeat (dtp))
1514 return;
1516 c = next_char (dtp);
1517 switch (c)
1519 case '(':
1520 break;
1522 case '!':
1523 if (!dtp->u.p.namelist_mode)
1524 goto bad_complex;
1526 CASE_SEPARATORS:
1527 case EOF:
1528 unget_char (dtp, c);
1529 eat_separator (dtp);
1530 return;
1532 default:
1533 goto bad_complex;
1536 eol_1:
1537 eat_spaces (dtp);
1538 c = next_char (dtp);
1539 if (c == '\n' || c== '\r')
1540 goto eol_1;
1541 else
1542 unget_char (dtp, c);
1544 if (parse_real (dtp, dest, kind))
1545 return;
1547 eol_2:
1548 eat_spaces (dtp);
1549 c = next_char (dtp);
1550 if (c == '\n' || c== '\r')
1551 goto eol_2;
1552 else
1553 unget_char (dtp, c);
1555 if (next_char (dtp)
1556 != (dtp->u.p.current_unit->decimal_status == DECIMAL_POINT ? ',' : ';'))
1557 goto bad_complex;
1559 eol_3:
1560 eat_spaces (dtp);
1561 c = next_char (dtp);
1562 if (c == '\n' || c== '\r')
1563 goto eol_3;
1564 else
1565 unget_char (dtp, c);
1567 if (parse_real (dtp, dest + size / 2, kind))
1568 return;
1570 eol_4:
1571 eat_spaces (dtp);
1572 c = next_char (dtp);
1573 if (c == '\n' || c== '\r')
1574 goto eol_4;
1575 else
1576 unget_char (dtp, c);
1578 if (next_char (dtp) != ')')
1579 goto bad_complex;
1581 c = next_char (dtp);
1582 if (!is_separator (c) && (c != EOF))
1583 goto bad_complex;
1585 unget_char (dtp, c);
1586 eat_separator (dtp);
1588 free_saved (dtp);
1589 dtp->u.p.saved_type = BT_COMPLEX;
1590 return;
1592 bad_complex:
1594 if (nml_bad_return (dtp, c))
1595 return;
1597 free_saved (dtp);
1598 if (c == EOF)
1600 free_line (dtp);
1601 hit_eof (dtp);
1602 return;
1604 else if (c != '\n')
1605 eat_line (dtp);
1607 snprintf (message, MSGLEN, "Bad complex value in item %d of list input",
1608 dtp->u.p.item_count);
1609 free_line (dtp);
1610 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1614 /* Parse a real number with a possible repeat count. */
1616 static void
1617 read_real (st_parameter_dt *dtp, void * dest, int length)
1619 char message[MSGLEN];
1620 int c;
1621 int seen_dp;
1622 int is_inf;
1624 seen_dp = 0;
1626 c = next_char (dtp);
1627 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1628 c = '.';
1629 switch (c)
1631 CASE_DIGITS:
1632 push_char (dtp, c);
1633 break;
1635 case '.':
1636 push_char (dtp, c);
1637 seen_dp = 1;
1638 break;
1640 case '+':
1641 case '-':
1642 goto got_sign;
1644 case '!':
1645 if (!dtp->u.p.namelist_mode)
1646 goto bad_real;
1648 CASE_SEPARATORS:
1649 unget_char (dtp, c); /* Single null. */
1650 eat_separator (dtp);
1651 return;
1653 case 'i':
1654 case 'I':
1655 case 'n':
1656 case 'N':
1657 goto inf_nan;
1659 default:
1660 goto bad_real;
1663 /* Get the digit string that might be a repeat count. */
1665 for (;;)
1667 c = next_char (dtp);
1668 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1669 c = '.';
1670 switch (c)
1672 CASE_DIGITS:
1673 push_char (dtp, c);
1674 break;
1676 case '.':
1677 if (seen_dp)
1678 goto bad_real;
1680 seen_dp = 1;
1681 push_char (dtp, c);
1682 goto real_loop;
1684 case 'E':
1685 case 'e':
1686 case 'D':
1687 case 'd':
1688 case 'Q':
1689 case 'q':
1690 goto exp1;
1692 case '+':
1693 case '-':
1694 push_char (dtp, 'e');
1695 push_char (dtp, c);
1696 c = next_char (dtp);
1697 goto exp2;
1699 case '*':
1700 push_char (dtp, '\0');
1701 goto got_repeat;
1703 case '!':
1704 if (!dtp->u.p.namelist_mode)
1705 goto bad_real;
1707 CASE_SEPARATORS:
1708 case EOF:
1709 if (c != '\n' && c != ',' && c != '\r' && c != ';')
1710 unget_char (dtp, c);
1711 goto done;
1713 default:
1714 goto bad_real;
1718 got_repeat:
1719 if (convert_integer (dtp, -1, 0))
1720 return;
1722 /* Now get the number itself. */
1724 if ((c = next_char (dtp)) == EOF)
1725 goto bad_real;
1726 if (is_separator (c))
1727 { /* Repeated null value. */
1728 unget_char (dtp, c);
1729 eat_separator (dtp);
1730 return;
1733 if (c != '-' && c != '+')
1734 push_char (dtp, '+');
1735 else
1737 got_sign:
1738 push_char (dtp, c);
1739 if ((c = next_char (dtp)) == EOF)
1740 goto bad_real;
1743 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1744 c = '.';
1746 if (!isdigit (c) && c != '.')
1748 if (c == 'i' || c == 'I' || c == 'n' || c == 'N')
1749 goto inf_nan;
1750 else
1751 goto bad_real;
1754 if (c == '.')
1756 if (seen_dp)
1757 goto bad_real;
1758 else
1759 seen_dp = 1;
1762 push_char (dtp, c);
1764 real_loop:
1765 for (;;)
1767 c = next_char (dtp);
1768 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1769 c = '.';
1770 switch (c)
1772 CASE_DIGITS:
1773 push_char (dtp, c);
1774 break;
1776 case '!':
1777 if (!dtp->u.p.namelist_mode)
1778 goto bad_real;
1780 CASE_SEPARATORS:
1781 case EOF:
1782 goto done;
1784 case '.':
1785 if (seen_dp)
1786 goto bad_real;
1788 seen_dp = 1;
1789 push_char (dtp, c);
1790 break;
1792 case 'E':
1793 case 'e':
1794 case 'D':
1795 case 'd':
1796 case 'Q':
1797 case 'q':
1798 goto exp1;
1800 case '+':
1801 case '-':
1802 push_char (dtp, 'e');
1803 push_char (dtp, c);
1804 c = next_char (dtp);
1805 goto exp2;
1807 default:
1808 goto bad_real;
1812 exp1:
1813 push_char (dtp, 'e');
1815 if ((c = next_char (dtp)) == EOF)
1816 goto bad_real;
1817 if (c != '+' && c != '-')
1818 push_char (dtp, '+');
1819 else
1821 push_char (dtp, c);
1822 c = next_char (dtp);
1825 exp2:
1826 if (!isdigit (c))
1828 /* Extension: allow default exponent of 0 when omitted. */
1829 if (dtp->common.flags & IOPARM_DT_DEFAULT_EXP)
1831 push_char (dtp, '0');
1832 goto done;
1834 else
1835 goto bad_exponent;
1838 push_char (dtp, c);
1840 for (;;)
1842 c = next_char (dtp);
1844 switch (c)
1846 CASE_DIGITS:
1847 push_char (dtp, c);
1848 break;
1850 case '!':
1851 if (!dtp->u.p.namelist_mode)
1852 goto bad_real;
1854 CASE_SEPARATORS:
1855 case EOF:
1856 goto done;
1858 default:
1859 goto bad_real;
1863 done:
1864 unget_char (dtp, c);
1865 eat_separator (dtp);
1866 push_char (dtp, '\0');
1867 if (convert_real (dtp, dest, dtp->u.p.saved_string, length))
1869 free_saved (dtp);
1870 return;
1873 free_saved (dtp);
1874 dtp->u.p.saved_type = BT_REAL;
1875 return;
1877 inf_nan:
1878 l_push_char (dtp, c);
1879 is_inf = 0;
1881 /* Match INF and Infinity. */
1882 if (c == 'i' || c == 'I')
1884 c = next_char (dtp);
1885 l_push_char (dtp, c);
1886 if (c != 'n' && c != 'N')
1887 goto unwind;
1888 c = next_char (dtp);
1889 l_push_char (dtp, c);
1890 if (c != 'f' && c != 'F')
1891 goto unwind;
1892 c = next_char (dtp);
1893 l_push_char (dtp, c);
1894 if (!is_separator (c) && (c != EOF))
1896 if (c != 'i' && c != 'I')
1897 goto unwind;
1898 c = next_char (dtp);
1899 l_push_char (dtp, c);
1900 if (c != 'n' && c != 'N')
1901 goto unwind;
1902 c = next_char (dtp);
1903 l_push_char (dtp, c);
1904 if (c != 'i' && c != 'I')
1905 goto unwind;
1906 c = next_char (dtp);
1907 l_push_char (dtp, c);
1908 if (c != 't' && c != 'T')
1909 goto unwind;
1910 c = next_char (dtp);
1911 l_push_char (dtp, c);
1912 if (c != 'y' && c != 'Y')
1913 goto unwind;
1914 c = next_char (dtp);
1915 l_push_char (dtp, c);
1917 is_inf = 1;
1918 } /* Match NaN. */
1919 else
1921 c = next_char (dtp);
1922 l_push_char (dtp, c);
1923 if (c != 'a' && c != 'A')
1924 goto unwind;
1925 c = next_char (dtp);
1926 l_push_char (dtp, c);
1927 if (c != 'n' && c != 'N')
1928 goto unwind;
1929 c = next_char (dtp);
1930 l_push_char (dtp, c);
1932 /* Match NAN(alphanum). */
1933 if (c == '(')
1935 for (c = next_char (dtp); c != ')'; c = next_char (dtp))
1936 if (is_separator (c))
1937 goto unwind;
1938 else
1939 l_push_char (dtp, c);
1941 l_push_char (dtp, ')');
1942 c = next_char (dtp);
1943 l_push_char (dtp, c);
1947 if (!is_separator (c) && (c != EOF))
1948 goto unwind;
1950 if (dtp->u.p.namelist_mode)
1952 if (c == ' ' || c =='\n' || c == '\r')
1956 if ((c = next_char (dtp)) == EOF)
1957 goto bad_real;
1959 while (c == ' ' || c =='\n' || c == '\r');
1961 l_push_char (dtp, c);
1963 if (c == '=')
1964 goto unwind;
1968 if (is_inf)
1970 push_char (dtp, 'i');
1971 push_char (dtp, 'n');
1972 push_char (dtp, 'f');
1974 else
1976 push_char (dtp, 'n');
1977 push_char (dtp, 'a');
1978 push_char (dtp, 'n');
1981 free_line (dtp);
1982 unget_char (dtp, c);
1983 eat_separator (dtp);
1984 push_char (dtp, '\0');
1985 if (convert_infnan (dtp, dest, dtp->u.p.saved_string, length))
1986 return;
1988 free_saved (dtp);
1989 dtp->u.p.saved_type = BT_REAL;
1990 return;
1992 unwind:
1993 if (dtp->u.p.namelist_mode)
1995 dtp->u.p.nml_read_error = 1;
1996 dtp->u.p.line_buffer_enabled = 1;
1997 dtp->u.p.line_buffer_pos = 0;
1998 return;
2001 bad_real:
2003 if (nml_bad_return (dtp, c))
2004 return;
2006 bad_exponent:
2008 free_saved (dtp);
2009 if (c == EOF)
2011 free_line (dtp);
2012 hit_eof (dtp);
2013 return;
2015 else if (c != '\n')
2016 eat_line (dtp);
2018 snprintf (message, MSGLEN, "Bad real number in item %d of list input",
2019 dtp->u.p.item_count);
2020 free_line (dtp);
2021 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
2025 /* Check the current type against the saved type to make sure they are
2026 compatible. Returns nonzero if incompatible. */
2028 static int
2029 check_type (st_parameter_dt *dtp, bt type, int kind)
2031 char message[MSGLEN];
2033 if (dtp->u.p.saved_type != BT_UNKNOWN && dtp->u.p.saved_type != type)
2035 snprintf (message, MSGLEN, "Read type %s where %s was expected for item %d",
2036 type_name (dtp->u.p.saved_type), type_name (type),
2037 dtp->u.p.item_count);
2038 free_line (dtp);
2039 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
2040 return 1;
2043 if (dtp->u.p.saved_type == BT_UNKNOWN || dtp->u.p.saved_type == BT_CHARACTER)
2044 return 0;
2046 if ((type != BT_COMPLEX && dtp->u.p.saved_length != kind)
2047 || (type == BT_COMPLEX && dtp->u.p.saved_length != kind*2))
2049 snprintf (message, MSGLEN,
2050 "Read kind %d %s where kind %d is required for item %d",
2051 type == BT_COMPLEX ? dtp->u.p.saved_length / 2
2052 : dtp->u.p.saved_length,
2053 type_name (dtp->u.p.saved_type), kind,
2054 dtp->u.p.item_count);
2055 free_line (dtp);
2056 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
2057 return 1;
2060 return 0;
2064 /* Initialize the function pointers to select the correct versions of
2065 next_char and push_char depending on what we are doing. */
2067 static void
2068 set_workers (st_parameter_dt *dtp)
2070 if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
2072 dtp->u.p.current_unit->next_char_fn_ptr = &next_char_utf8;
2073 dtp->u.p.current_unit->push_char_fn_ptr = &push_char4;
2075 else if (is_internal_unit (dtp))
2077 dtp->u.p.current_unit->next_char_fn_ptr = &next_char_internal;
2078 dtp->u.p.current_unit->push_char_fn_ptr = &push_char_default;
2080 else
2082 dtp->u.p.current_unit->next_char_fn_ptr = &next_char_default;
2083 dtp->u.p.current_unit->push_char_fn_ptr = &push_char_default;
2088 /* Top level data transfer subroutine for list reads. Because we have
2089 to deal with repeat counts, the data item is always saved after
2090 reading, usually in the dtp->u.p.value[] array. If a repeat count is
2091 greater than one, we copy the data item multiple times. */
2093 static int
2094 list_formatted_read_scalar (st_parameter_dt *dtp, bt type, void *p,
2095 int kind, size_t size)
2097 gfc_char4_t *q, *r;
2098 int c, i, m;
2099 int err = 0;
2101 dtp->u.p.namelist_mode = 0;
2103 /* Set the next_char and push_char worker functions. */
2104 set_workers (dtp);
2106 if (dtp->u.p.first_item)
2108 dtp->u.p.first_item = 0;
2109 dtp->u.p.input_complete = 0;
2110 dtp->u.p.repeat_count = 1;
2111 dtp->u.p.at_eol = 0;
2113 if ((c = eat_spaces (dtp)) == EOF)
2115 err = LIBERROR_END;
2116 goto cleanup;
2118 if (is_separator (c))
2120 /* Found a null value. */
2121 dtp->u.p.repeat_count = 0;
2122 eat_separator (dtp);
2124 /* Set end-of-line flag. */
2125 if (c == '\n' || c == '\r')
2127 dtp->u.p.at_eol = 1;
2128 if (finish_separator (dtp) == LIBERROR_END)
2130 err = LIBERROR_END;
2131 goto cleanup;
2134 else
2135 goto cleanup;
2138 else
2140 if (dtp->u.p.repeat_count > 0)
2142 if (check_type (dtp, type, kind))
2143 return err;
2144 goto set_value;
2147 if (dtp->u.p.input_complete)
2148 goto cleanup;
2150 if (dtp->u.p.at_eol)
2151 finish_separator (dtp);
2152 else
2154 eat_spaces (dtp);
2155 /* Trailing spaces prior to end of line. */
2156 if (dtp->u.p.at_eol)
2157 finish_separator (dtp);
2160 dtp->u.p.saved_type = BT_UNKNOWN;
2161 dtp->u.p.repeat_count = 1;
2164 switch (type)
2166 case BT_INTEGER:
2167 read_integer (dtp, kind);
2168 break;
2169 case BT_LOGICAL:
2170 read_logical (dtp, kind);
2171 break;
2172 case BT_CHARACTER:
2173 read_character (dtp, kind);
2174 break;
2175 case BT_REAL:
2176 read_real (dtp, p, kind);
2177 /* Copy value back to temporary if needed. */
2178 if (dtp->u.p.repeat_count > 0)
2179 memcpy (dtp->u.p.value, p, size);
2180 break;
2181 case BT_COMPLEX:
2182 read_complex (dtp, p, kind, size);
2183 /* Copy value back to temporary if needed. */
2184 if (dtp->u.p.repeat_count > 0)
2185 memcpy (dtp->u.p.value, p, size);
2186 break;
2187 case BT_CLASS:
2189 int unit = dtp->u.p.current_unit->unit_number;
2190 char iotype[] = "LISTDIRECTED";
2191 gfc_charlen_type iotype_len = 12;
2192 char tmp_iomsg[IOMSG_LEN] = "";
2193 char *child_iomsg;
2194 gfc_charlen_type child_iomsg_len;
2195 int noiostat;
2196 int *child_iostat = NULL;
2197 gfc_array_i4 vlist;
2199 GFC_DESCRIPTOR_DATA(&vlist) = NULL;
2200 GFC_DIMENSION_SET(vlist.dim[0],1, 0, 0);
2202 /* Set iostat, intent(out). */
2203 noiostat = 0;
2204 child_iostat = (dtp->common.flags & IOPARM_HAS_IOSTAT) ?
2205 dtp->common.iostat : &noiostat;
2207 /* Set iomsge, intent(inout). */
2208 if (dtp->common.flags & IOPARM_HAS_IOMSG)
2210 child_iomsg = dtp->common.iomsg;
2211 child_iomsg_len = dtp->common.iomsg_len;
2213 else
2215 child_iomsg = tmp_iomsg;
2216 child_iomsg_len = IOMSG_LEN;
2219 /* Call the user defined formatted READ procedure. */
2220 dtp->u.p.current_unit->child_dtio++;
2221 dtp->u.p.fdtio_ptr (p, &unit, iotype, &vlist,
2222 child_iostat, child_iomsg,
2223 iotype_len, child_iomsg_len);
2224 dtp->u.p.current_unit->child_dtio--;
2226 break;
2227 default:
2228 internal_error (&dtp->common, "Bad type for list read");
2231 if (dtp->u.p.saved_type != BT_CHARACTER && dtp->u.p.saved_type != BT_UNKNOWN)
2232 dtp->u.p.saved_length = size;
2234 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2235 goto cleanup;
2237 set_value:
2238 switch (dtp->u.p.saved_type)
2240 case BT_COMPLEX:
2241 case BT_REAL:
2242 if (dtp->u.p.repeat_count > 0)
2243 memcpy (p, dtp->u.p.value, size);
2244 break;
2246 case BT_INTEGER:
2247 case BT_LOGICAL:
2248 memcpy (p, dtp->u.p.value, size);
2249 break;
2251 case BT_CHARACTER:
2252 if (dtp->u.p.saved_string)
2254 m = ((int) size < dtp->u.p.saved_used)
2255 ? (int) size : dtp->u.p.saved_used;
2257 q = (gfc_char4_t *) p;
2258 r = (gfc_char4_t *) dtp->u.p.saved_string;
2259 if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
2260 for (i = 0; i < m; i++)
2261 *q++ = *r++;
2262 else
2264 if (kind == 1)
2265 memcpy (p, dtp->u.p.saved_string, m);
2266 else
2267 for (i = 0; i < m; i++)
2268 *q++ = *r++;
2271 else
2272 /* Just delimiters encountered, nothing to copy but SPACE. */
2273 m = 0;
2275 if (m < (int) size)
2277 if (kind == 1)
2278 memset (((char *) p) + m, ' ', size - m);
2279 else
2281 q = (gfc_char4_t *) p;
2282 for (i = m; i < (int) size; i++)
2283 q[i] = (unsigned char) ' ';
2286 break;
2288 case BT_UNKNOWN:
2289 break;
2291 default:
2292 internal_error (&dtp->common, "Bad type for list read");
2295 if (--dtp->u.p.repeat_count <= 0)
2296 free_saved (dtp);
2298 cleanup:
2299 if (err == LIBERROR_END)
2301 free_line (dtp);
2302 hit_eof (dtp);
2304 fbuf_flush_list (dtp->u.p.current_unit, LIST_READING);
2305 return err;
2309 void
2310 list_formatted_read (st_parameter_dt *dtp, bt type, void *p, int kind,
2311 size_t size, size_t nelems)
2313 size_t elem;
2314 char *tmp;
2315 size_t stride = type == BT_CHARACTER ?
2316 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
2317 int err;
2319 tmp = (char *) p;
2321 /* Big loop over all the elements. */
2322 for (elem = 0; elem < nelems; elem++)
2324 dtp->u.p.item_count++;
2325 err = list_formatted_read_scalar (dtp, type, tmp + stride*elem,
2326 kind, size);
2327 if (err)
2328 break;
2333 /* Finish a list read. */
2335 void
2336 finish_list_read (st_parameter_dt *dtp)
2338 free_saved (dtp);
2340 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
2342 if (dtp->u.p.at_eol)
2344 dtp->u.p.at_eol = 0;
2345 return;
2348 if (!is_internal_unit (dtp))
2350 int c;
2352 /* Set the next_char and push_char worker functions. */
2353 set_workers (dtp);
2355 c = next_char (dtp);
2356 if (c == EOF)
2358 free_line (dtp);
2359 hit_eof (dtp);
2360 return;
2362 if (c != '\n')
2363 eat_line (dtp);
2366 free_line (dtp);
2370 /* NAMELIST INPUT
2372 void namelist_read (st_parameter_dt *dtp)
2373 calls:
2374 static void nml_match_name (char *name, int len)
2375 static int nml_query (st_parameter_dt *dtp)
2376 static int nml_get_obj_data (st_parameter_dt *dtp,
2377 namelist_info **prev_nl, char *, size_t)
2378 calls:
2379 static void nml_untouch_nodes (st_parameter_dt *dtp)
2380 static namelist_info * find_nml_node (st_parameter_dt *dtp,
2381 char * var_name)
2382 static int nml_parse_qualifier(descriptor_dimension * ad,
2383 array_loop_spec * ls, int rank, char *)
2384 static void nml_touch_nodes (namelist_info * nl)
2385 static int nml_read_obj (namelist_info *nl, index_type offset,
2386 namelist_info **prev_nl, char *, size_t,
2387 index_type clow, index_type chigh)
2388 calls:
2389 -itself- */
2391 /* Inputs a rank-dimensional qualifier, which can contain
2392 singlets, doublets, triplets or ':' with the standard meanings. */
2394 static bool
2395 nml_parse_qualifier (st_parameter_dt *dtp, descriptor_dimension *ad,
2396 array_loop_spec *ls, int rank, bt nml_elem_type,
2397 char *parse_err_msg, size_t parse_err_msg_size,
2398 int *parsed_rank)
2400 int dim;
2401 int indx;
2402 int neg;
2403 int null_flag;
2404 int is_array_section, is_char;
2405 int c;
2407 is_char = 0;
2408 is_array_section = 0;
2409 dtp->u.p.expanded_read = 0;
2411 /* See if this is a character substring qualifier we are looking for. */
2412 if (rank == -1)
2414 rank = 1;
2415 is_char = 1;
2418 /* The next character in the stream should be the '('. */
2420 if ((c = next_char (dtp)) == EOF)
2421 goto err_ret;
2423 /* Process the qualifier, by dimension and triplet. */
2425 for (dim=0; dim < rank; dim++ )
2427 for (indx=0; indx<3; indx++)
2429 free_saved (dtp);
2430 eat_spaces (dtp);
2431 neg = 0;
2433 /* Process a potential sign. */
2434 if ((c = next_char (dtp)) == EOF)
2435 goto err_ret;
2436 switch (c)
2438 case '-':
2439 neg = 1;
2440 break;
2442 case '+':
2443 break;
2445 default:
2446 unget_char (dtp, c);
2447 break;
2450 /* Process characters up to the next ':' , ',' or ')'. */
2451 for (;;)
2453 c = next_char (dtp);
2454 switch (c)
2456 case EOF:
2457 goto err_ret;
2459 case ':':
2460 is_array_section = 1;
2461 break;
2463 case ',': case ')':
2464 if ((c==',' && dim == rank -1)
2465 || (c==')' && dim < rank -1))
2467 if (is_char)
2468 snprintf (parse_err_msg, parse_err_msg_size,
2469 "Bad substring qualifier");
2470 else
2471 snprintf (parse_err_msg, parse_err_msg_size,
2472 "Bad number of index fields");
2473 goto err_ret;
2475 break;
2477 CASE_DIGITS:
2478 push_char (dtp, c);
2479 continue;
2481 case ' ': case '\t': case '\r': case '\n':
2482 eat_spaces (dtp);
2483 break;
2485 default:
2486 if (is_char)
2487 snprintf (parse_err_msg, parse_err_msg_size,
2488 "Bad character in substring qualifier");
2489 else
2490 snprintf (parse_err_msg, parse_err_msg_size,
2491 "Bad character in index");
2492 goto err_ret;
2495 if ((c == ',' || c == ')') && indx == 0
2496 && dtp->u.p.saved_string == 0)
2498 if (is_char)
2499 snprintf (parse_err_msg, parse_err_msg_size,
2500 "Null substring qualifier");
2501 else
2502 snprintf (parse_err_msg, parse_err_msg_size,
2503 "Null index field");
2504 goto err_ret;
2507 if ((c == ':' && indx == 1 && dtp->u.p.saved_string == 0)
2508 || (indx == 2 && dtp->u.p.saved_string == 0))
2510 if (is_char)
2511 snprintf (parse_err_msg, parse_err_msg_size,
2512 "Bad substring qualifier");
2513 else
2514 snprintf (parse_err_msg, parse_err_msg_size,
2515 "Bad index triplet");
2516 goto err_ret;
2519 if (is_char && !is_array_section)
2521 snprintf (parse_err_msg, parse_err_msg_size,
2522 "Missing colon in substring qualifier");
2523 goto err_ret;
2526 /* If '( : ? )' or '( ? : )' break and flag read failure. */
2527 null_flag = 0;
2528 if ((c == ':' && indx == 0 && dtp->u.p.saved_string == 0)
2529 || (indx==1 && dtp->u.p.saved_string == 0))
2531 null_flag = 1;
2532 break;
2535 /* Now read the index. */
2536 if (convert_integer (dtp, sizeof(index_type), neg))
2538 if (is_char)
2539 snprintf (parse_err_msg, parse_err_msg_size,
2540 "Bad integer substring qualifier");
2541 else
2542 snprintf (parse_err_msg, parse_err_msg_size,
2543 "Bad integer in index");
2544 goto err_ret;
2546 break;
2549 /* Feed the index values to the triplet arrays. */
2550 if (!null_flag)
2552 if (indx == 0)
2553 memcpy (&ls[dim].start, dtp->u.p.value, sizeof(index_type));
2554 if (indx == 1)
2555 memcpy (&ls[dim].end, dtp->u.p.value, sizeof(index_type));
2556 if (indx == 2)
2557 memcpy (&ls[dim].step, dtp->u.p.value, sizeof(index_type));
2560 /* Singlet or doublet indices. */
2561 if (c==',' || c==')')
2563 if (indx == 0)
2565 memcpy (&ls[dim].start, dtp->u.p.value, sizeof(index_type));
2567 /* If -std=f95/2003 or an array section is specified,
2568 do not allow excess data to be processed. */
2569 if (is_array_section == 1
2570 || !(compile_options.allow_std & GFC_STD_GNU)
2571 || nml_elem_type == BT_DERIVED)
2572 ls[dim].end = ls[dim].start;
2573 else
2574 dtp->u.p.expanded_read = 1;
2577 /* Check for non-zero rank. */
2578 if (is_array_section == 1 && ls[dim].start != ls[dim].end)
2579 *parsed_rank = 1;
2581 break;
2585 if (is_array_section == 1 && dtp->u.p.expanded_read == 1)
2587 int i;
2588 dtp->u.p.expanded_read = 0;
2589 for (i = 0; i < dim; i++)
2590 ls[i].end = ls[i].start;
2593 /* Check the values of the triplet indices. */
2594 if ((ls[dim].start > GFC_DIMENSION_UBOUND(ad[dim]))
2595 || (ls[dim].start < GFC_DIMENSION_LBOUND(ad[dim]))
2596 || (ls[dim].end > GFC_DIMENSION_UBOUND(ad[dim]))
2597 || (ls[dim].end < GFC_DIMENSION_LBOUND(ad[dim])))
2599 if (is_char)
2600 snprintf (parse_err_msg, parse_err_msg_size,
2601 "Substring out of range");
2602 else
2603 snprintf (parse_err_msg, parse_err_msg_size,
2604 "Index %d out of range", dim + 1);
2605 goto err_ret;
2608 if (((ls[dim].end - ls[dim].start ) * ls[dim].step < 0)
2609 || (ls[dim].step == 0))
2611 snprintf (parse_err_msg, parse_err_msg_size,
2612 "Bad range in index %d", dim + 1);
2613 goto err_ret;
2616 /* Initialise the loop index counter. */
2617 ls[dim].idx = ls[dim].start;
2619 eat_spaces (dtp);
2620 return true;
2622 err_ret:
2624 /* The EOF error message is issued by hit_eof. Return true so that the
2625 caller does not use parse_err_msg and parse_err_msg_size to generate
2626 an unrelated error message. */
2627 if (c == EOF)
2629 hit_eof (dtp);
2630 dtp->u.p.input_complete = 1;
2631 return true;
2633 return false;
2637 static bool
2638 extended_look_ahead (char *p, char *q)
2640 char *r, *s;
2642 /* Scan ahead to find a '%' in the p string. */
2643 for(r = p, s = q; *r && *s; s++)
2644 if ((*s == '%' || *s == '+') && strcmp (r + 1, s + 1) == 0)
2645 return true;
2646 return false;
2650 static bool
2651 strcmp_extended_type (char *p, char *q)
2653 char *r, *s;
2655 for (r = p, s = q; *r && *s; r++, s++)
2657 if (*r != *s)
2659 if (*r == '%' && *s == '+' && extended_look_ahead (r, s))
2660 return true;
2661 break;
2664 return false;
2668 static namelist_info *
2669 find_nml_node (st_parameter_dt *dtp, char * var_name)
2671 namelist_info * t = dtp->u.p.ionml;
2672 while (t != NULL)
2674 if (strcmp (var_name, t->var_name) == 0)
2676 t->touched = 1;
2677 return t;
2679 if (strcmp_extended_type (var_name, t->var_name))
2681 t->touched = 1;
2682 return t;
2684 t = t->next;
2686 return NULL;
2689 /* Visits all the components of a derived type that have
2690 not explicitly been identified in the namelist input.
2691 touched is set and the loop specification initialised
2692 to default values */
2694 static void
2695 nml_touch_nodes (namelist_info * nl)
2697 index_type len = strlen (nl->var_name) + 1;
2698 int dim;
2699 char * ext_name = xmalloc (len + 1);
2700 memcpy (ext_name, nl->var_name, len-1);
2701 memcpy (ext_name + len - 1, "%", 2);
2702 for (nl = nl->next; nl; nl = nl->next)
2704 if (strncmp (nl->var_name, ext_name, len) == 0)
2706 nl->touched = 1;
2707 for (dim=0; dim < nl->var_rank; dim++)
2709 nl->ls[dim].step = 1;
2710 nl->ls[dim].end = GFC_DESCRIPTOR_UBOUND(nl,dim);
2711 nl->ls[dim].start = GFC_DESCRIPTOR_LBOUND(nl,dim);
2712 nl->ls[dim].idx = nl->ls[dim].start;
2715 else
2716 break;
2718 free (ext_name);
2719 return;
2722 /* Resets touched for the entire list of nml_nodes, ready for a
2723 new object. */
2725 static void
2726 nml_untouch_nodes (st_parameter_dt *dtp)
2728 namelist_info * t;
2729 for (t = dtp->u.p.ionml; t; t = t->next)
2730 t->touched = 0;
2731 return;
2734 /* Attempts to input name to namelist name. Returns
2735 dtp->u.p.nml_read_error = 1 on no match. */
2737 static void
2738 nml_match_name (st_parameter_dt *dtp, const char *name, index_type len)
2740 index_type i;
2741 int c;
2743 dtp->u.p.nml_read_error = 0;
2744 for (i = 0; i < len; i++)
2746 c = next_char (dtp);
2747 if (c == EOF || (tolower (c) != tolower (name[i])))
2749 dtp->u.p.nml_read_error = 1;
2750 break;
2755 /* If the namelist read is from stdin, output the current state of the
2756 namelist to stdout. This is used to implement the non-standard query
2757 features, ? and =?. If c == '=' the full namelist is printed. Otherwise
2758 the names alone are printed. */
2760 static void
2761 nml_query (st_parameter_dt *dtp, char c)
2763 gfc_unit * temp_unit;
2764 namelist_info * nl;
2765 index_type len;
2766 char * p;
2767 #ifdef HAVE_CRLF
2768 static const index_type endlen = 2;
2769 static const char endl[] = "\r\n";
2770 static const char nmlend[] = "&end\r\n";
2771 #else
2772 static const index_type endlen = 1;
2773 static const char endl[] = "\n";
2774 static const char nmlend[] = "&end\n";
2775 #endif
2777 if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
2778 return;
2780 /* Store the current unit and transfer to stdout. */
2782 temp_unit = dtp->u.p.current_unit;
2783 dtp->u.p.current_unit = find_unit (options.stdout_unit);
2785 if (dtp->u.p.current_unit)
2787 dtp->u.p.mode = WRITING;
2788 next_record (dtp, 0);
2790 /* Write the namelist in its entirety. */
2792 if (c == '=')
2793 namelist_write (dtp);
2795 /* Or write the list of names. */
2797 else
2799 /* "&namelist_name\n" */
2801 len = dtp->namelist_name_len;
2802 p = write_block (dtp, len - 1 + endlen);
2803 if (!p)
2804 goto query_return;
2805 memcpy (p, "&", 1);
2806 memcpy ((char*)(p + 1), dtp->namelist_name, len);
2807 memcpy ((char*)(p + len + 1), &endl, endlen);
2808 for (nl = dtp->u.p.ionml; nl; nl = nl->next)
2810 /* " var_name\n" */
2812 len = strlen (nl->var_name);
2813 p = write_block (dtp, len + endlen);
2814 if (!p)
2815 goto query_return;
2816 memcpy (p, " ", 1);
2817 memcpy ((char*)(p + 1), nl->var_name, len);
2818 memcpy ((char*)(p + len + 1), &endl, endlen);
2821 /* "&end\n" */
2823 p = write_block (dtp, endlen + 4);
2824 if (!p)
2825 goto query_return;
2826 memcpy (p, &nmlend, endlen + 4);
2829 /* Flush the stream to force immediate output. */
2831 fbuf_flush (dtp->u.p.current_unit, WRITING);
2832 sflush (dtp->u.p.current_unit->s);
2833 unlock_unit (dtp->u.p.current_unit);
2836 query_return:
2838 /* Restore the current unit. */
2840 dtp->u.p.current_unit = temp_unit;
2841 dtp->u.p.mode = READING;
2842 return;
2845 /* Reads and stores the input for the namelist object nl. For an array,
2846 the function loops over the ranges defined by the loop specification.
2847 This default to all the data or to the specification from a qualifier.
2848 nml_read_obj recursively calls itself to read derived types. It visits
2849 all its own components but only reads data for those that were touched
2850 when the name was parsed. If a read error is encountered, an attempt is
2851 made to return to read a new object name because the standard allows too
2852 little data to be available. On the other hand, too much data is an
2853 error. */
2855 static bool
2856 nml_read_obj (st_parameter_dt *dtp, namelist_info * nl, index_type offset,
2857 namelist_info **pprev_nl, char *nml_err_msg,
2858 size_t nml_err_msg_size, index_type clow, index_type chigh)
2860 namelist_info * cmp;
2861 char * obj_name;
2862 int nml_carry;
2863 int len;
2864 int dim;
2865 index_type dlen;
2866 index_type m;
2867 size_t obj_name_len;
2868 void * pdata;
2870 /* If we have encountered a previous read error or this object has not been
2871 touched in name parsing, just return. */
2872 if (dtp->u.p.nml_read_error || !nl->touched)
2873 return true;
2875 dtp->u.p.item_count++; /* Used in error messages. */
2876 dtp->u.p.repeat_count = 0;
2877 eat_spaces (dtp);
2879 len = nl->len;
2880 switch (nl->type)
2882 case BT_INTEGER:
2883 case BT_LOGICAL:
2884 dlen = len;
2885 break;
2887 case BT_REAL:
2888 dlen = size_from_real_kind (len);
2889 break;
2891 case BT_COMPLEX:
2892 dlen = size_from_complex_kind (len);
2893 break;
2895 case BT_CHARACTER:
2896 dlen = chigh ? (chigh - clow + 1) : nl->string_length;
2897 break;
2899 default:
2900 dlen = 0;
2905 /* Update the pointer to the data, using the current index vector */
2907 pdata = (void*)(nl->mem_pos + offset);
2908 for (dim = 0; dim < nl->var_rank; dim++)
2909 pdata = (void*)(pdata + (nl->ls[dim].idx
2910 - GFC_DESCRIPTOR_LBOUND(nl,dim))
2911 * GFC_DESCRIPTOR_STRIDE(nl,dim) * nl->size);
2913 /* If we are finished with the repeat count, try to read next value. */
2915 nml_carry = 0;
2916 if (--dtp->u.p.repeat_count <= 0)
2918 if (dtp->u.p.input_complete)
2919 return true;
2920 if (dtp->u.p.at_eol)
2921 finish_separator (dtp);
2922 if (dtp->u.p.input_complete)
2923 return true;
2925 dtp->u.p.saved_type = BT_UNKNOWN;
2926 free_saved (dtp);
2928 switch (nl->type)
2930 case BT_INTEGER:
2931 read_integer (dtp, len);
2932 break;
2934 case BT_LOGICAL:
2935 read_logical (dtp, len);
2936 break;
2938 case BT_CHARACTER:
2939 read_character (dtp, len);
2940 break;
2942 case BT_REAL:
2943 /* Need to copy data back from the real location to the temp in
2944 order to handle nml reads into arrays. */
2945 read_real (dtp, pdata, len);
2946 memcpy (dtp->u.p.value, pdata, dlen);
2947 break;
2949 case BT_COMPLEX:
2950 /* Same as for REAL, copy back to temp. */
2951 read_complex (dtp, pdata, len, dlen);
2952 memcpy (dtp->u.p.value, pdata, dlen);
2953 break;
2955 case BT_DERIVED:
2956 obj_name_len = strlen (nl->var_name) + 1;
2957 obj_name = xmalloc (obj_name_len+1);
2958 memcpy (obj_name, nl->var_name, obj_name_len-1);
2959 memcpy (obj_name + obj_name_len - 1, "%", 2);
2961 /* If reading a derived type, disable the expanded read warning
2962 since a single object can have multiple reads. */
2963 dtp->u.p.expanded_read = 0;
2965 /* Now loop over the components. */
2967 for (cmp = nl->next;
2968 cmp &&
2969 !strncmp (cmp->var_name, obj_name, obj_name_len);
2970 cmp = cmp->next)
2972 /* Jump over nested derived type by testing if the potential
2973 component name contains '%'. */
2974 if (strchr (cmp->var_name + obj_name_len, '%'))
2975 continue;
2977 if (!nml_read_obj (dtp, cmp, (index_type)(pdata - nl->mem_pos),
2978 pprev_nl, nml_err_msg, nml_err_msg_size,
2979 clow, chigh))
2981 free (obj_name);
2982 return false;
2985 if (dtp->u.p.input_complete)
2987 free (obj_name);
2988 return true;
2992 free (obj_name);
2993 goto incr_idx;
2995 default:
2996 snprintf (nml_err_msg, nml_err_msg_size,
2997 "Bad type for namelist object %s", nl->var_name);
2998 internal_error (&dtp->common, nml_err_msg);
2999 goto nml_err_ret;
3003 /* The standard permits array data to stop short of the number of
3004 elements specified in the loop specification. In this case, we
3005 should be here with dtp->u.p.nml_read_error != 0. Control returns to
3006 nml_get_obj_data and an attempt is made to read object name. */
3008 *pprev_nl = nl;
3009 if (dtp->u.p.nml_read_error)
3011 dtp->u.p.expanded_read = 0;
3012 return true;
3015 if (dtp->u.p.saved_type == BT_UNKNOWN)
3017 dtp->u.p.expanded_read = 0;
3018 goto incr_idx;
3021 switch (dtp->u.p.saved_type)
3024 case BT_COMPLEX:
3025 case BT_REAL:
3026 case BT_INTEGER:
3027 case BT_LOGICAL:
3028 memcpy (pdata, dtp->u.p.value, dlen);
3029 break;
3031 case BT_CHARACTER:
3032 if (dlen < dtp->u.p.saved_used)
3034 if (compile_options.bounds_check)
3036 snprintf (nml_err_msg, nml_err_msg_size,
3037 "Namelist object '%s' truncated on read.",
3038 nl->var_name);
3039 generate_warning (&dtp->common, nml_err_msg);
3041 m = dlen;
3043 else
3044 m = dtp->u.p.saved_used;
3046 if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
3048 gfc_char4_t *q4, *p4 = pdata;
3049 int i;
3051 q4 = (gfc_char4_t *) dtp->u.p.saved_string;
3052 p4 += clow -1;
3053 for (i = 0; i < m; i++)
3054 *p4++ = *q4++;
3055 if (m < dlen)
3056 for (i = 0; i < dlen - m; i++)
3057 *p4++ = (gfc_char4_t) ' ';
3059 else
3061 pdata = (void*)( pdata + clow - 1 );
3062 memcpy (pdata, dtp->u.p.saved_string, m);
3063 if (m < dlen)
3064 memset ((void*)( pdata + m ), ' ', dlen - m);
3066 break;
3068 default:
3069 break;
3072 /* Warn if a non-standard expanded read occurs. A single read of a
3073 single object is acceptable. If a second read occurs, issue a warning
3074 and set the flag to zero to prevent further warnings. */
3075 if (dtp->u.p.expanded_read == 2)
3077 notify_std (&dtp->common, GFC_STD_GNU, "Non-standard expanded namelist read.");
3078 dtp->u.p.expanded_read = 0;
3081 /* If the expanded read warning flag is set, increment it,
3082 indicating that a single read has occurred. */
3083 if (dtp->u.p.expanded_read >= 1)
3084 dtp->u.p.expanded_read++;
3086 /* Break out of loop if scalar. */
3087 if (!nl->var_rank)
3088 break;
3090 /* Now increment the index vector. */
3092 incr_idx:
3094 nml_carry = 1;
3095 for (dim = 0; dim < nl->var_rank; dim++)
3097 nl->ls[dim].idx += nml_carry * nl->ls[dim].step;
3098 nml_carry = 0;
3099 if (((nl->ls[dim].step > 0) && (nl->ls[dim].idx > nl->ls[dim].end))
3101 ((nl->ls[dim].step < 0) && (nl->ls[dim].idx < nl->ls[dim].end)))
3103 nl->ls[dim].idx = nl->ls[dim].start;
3104 nml_carry = 1;
3107 } while (!nml_carry);
3109 if (dtp->u.p.repeat_count > 1)
3111 snprintf (nml_err_msg, nml_err_msg_size,
3112 "Repeat count too large for namelist object %s", nl->var_name);
3113 goto nml_err_ret;
3115 return true;
3117 nml_err_ret:
3119 return false;
3122 /* Parses the object name, including array and substring qualifiers. It
3123 iterates over derived type components, touching those components and
3124 setting their loop specifications, if there is a qualifier. If the
3125 object is itself a derived type, its components and subcomponents are
3126 touched. nml_read_obj is called at the end and this reads the data in
3127 the manner specified by the object name. */
3129 static bool
3130 nml_get_obj_data (st_parameter_dt *dtp, namelist_info **pprev_nl,
3131 char *nml_err_msg, size_t nml_err_msg_size)
3133 int c;
3134 namelist_info * nl;
3135 namelist_info * first_nl = NULL;
3136 namelist_info * root_nl = NULL;
3137 int dim, parsed_rank;
3138 int component_flag, qualifier_flag;
3139 index_type clow, chigh;
3140 int non_zero_rank_count;
3142 /* Look for end of input or object name. If '?' or '=?' are encountered
3143 in stdin, print the node names or the namelist to stdout. */
3145 eat_separator (dtp);
3146 if (dtp->u.p.input_complete)
3147 return true;
3149 if (dtp->u.p.at_eol)
3150 finish_separator (dtp);
3151 if (dtp->u.p.input_complete)
3152 return true;
3154 if ((c = next_char (dtp)) == EOF)
3155 goto nml_err_ret;
3156 switch (c)
3158 case '=':
3159 if ((c = next_char (dtp)) == EOF)
3160 goto nml_err_ret;
3161 if (c != '?')
3163 snprintf (nml_err_msg, nml_err_msg_size,
3164 "namelist read: misplaced = sign");
3165 goto nml_err_ret;
3167 nml_query (dtp, '=');
3168 return true;
3170 case '?':
3171 nml_query (dtp, '?');
3172 return true;
3174 case '$':
3175 case '&':
3176 nml_match_name (dtp, "end", 3);
3177 if (dtp->u.p.nml_read_error)
3179 snprintf (nml_err_msg, nml_err_msg_size,
3180 "namelist not terminated with / or &end");
3181 goto nml_err_ret;
3183 /* Fall through. */
3184 case '/':
3185 dtp->u.p.input_complete = 1;
3186 return true;
3188 default :
3189 break;
3192 /* Untouch all nodes of the namelist and reset the flags that are set for
3193 derived type components. */
3195 nml_untouch_nodes (dtp);
3196 component_flag = 0;
3197 qualifier_flag = 0;
3198 non_zero_rank_count = 0;
3200 /* Get the object name - should '!' and '\n' be permitted separators? */
3202 get_name:
3204 free_saved (dtp);
3208 if (!is_separator (c))
3209 push_char_default (dtp, tolower(c));
3210 if ((c = next_char (dtp)) == EOF)
3211 goto nml_err_ret;
3213 while (!( c=='=' || c==' ' || c=='\t' || c =='(' || c =='%' ));
3215 unget_char (dtp, c);
3217 /* Check that the name is in the namelist and get pointer to object.
3218 Three error conditions exist: (i) An attempt is being made to
3219 identify a non-existent object, following a failed data read or
3220 (ii) The object name does not exist or (iii) Too many data items
3221 are present for an object. (iii) gives the same error message
3222 as (i) */
3224 push_char_default (dtp, '\0');
3226 if (component_flag)
3228 #define EXT_STACK_SZ 100
3229 char ext_stack[EXT_STACK_SZ];
3230 char *ext_name;
3231 size_t var_len = strlen (root_nl->var_name);
3232 size_t saved_len
3233 = dtp->u.p.saved_string ? strlen (dtp->u.p.saved_string) : 0;
3234 size_t ext_size = var_len + saved_len + 1;
3236 if (ext_size > EXT_STACK_SZ)
3237 ext_name = xmalloc (ext_size);
3238 else
3239 ext_name = ext_stack;
3241 memcpy (ext_name, root_nl->var_name, var_len);
3242 if (dtp->u.p.saved_string)
3243 memcpy (ext_name + var_len, dtp->u.p.saved_string, saved_len);
3244 ext_name[var_len + saved_len] = '\0';
3245 nl = find_nml_node (dtp, ext_name);
3247 if (ext_size > EXT_STACK_SZ)
3248 free (ext_name);
3250 else
3251 nl = find_nml_node (dtp, dtp->u.p.saved_string);
3253 if (nl == NULL)
3255 if (dtp->u.p.nml_read_error && *pprev_nl)
3256 snprintf (nml_err_msg, nml_err_msg_size,
3257 "Bad data for namelist object %s", (*pprev_nl)->var_name);
3259 else
3260 snprintf (nml_err_msg, nml_err_msg_size,
3261 "Cannot match namelist object name %s",
3262 dtp->u.p.saved_string);
3264 goto nml_err_ret;
3266 else if (nl->dtio_sub != NULL)
3268 int unit = dtp->u.p.current_unit->unit_number;
3269 char iotype[] = "NAMELIST";
3270 gfc_charlen_type iotype_len = 8;
3271 char tmp_iomsg[IOMSG_LEN] = "";
3272 char *child_iomsg;
3273 gfc_charlen_type child_iomsg_len;
3274 int noiostat;
3275 int *child_iostat = NULL;
3276 gfc_array_i4 vlist;
3277 gfc_class list_obj;
3278 formatted_dtio dtio_ptr = (formatted_dtio)nl->dtio_sub;
3280 GFC_DESCRIPTOR_DATA(&vlist) = NULL;
3281 GFC_DIMENSION_SET(vlist.dim[0],1, 0, 0);
3283 list_obj.data = (void *)nl->mem_pos;
3284 list_obj.vptr = nl->vtable;
3285 list_obj.len = 0;
3287 /* Set iostat, intent(out). */
3288 noiostat = 0;
3289 child_iostat = (dtp->common.flags & IOPARM_HAS_IOSTAT) ?
3290 dtp->common.iostat : &noiostat;
3292 /* Set iomsg, intent(inout). */
3293 if (dtp->common.flags & IOPARM_HAS_IOMSG)
3295 child_iomsg = dtp->common.iomsg;
3296 child_iomsg_len = dtp->common.iomsg_len;
3298 else
3300 child_iomsg = tmp_iomsg;
3301 child_iomsg_len = IOMSG_LEN;
3304 /* If reading from an internal unit, stash it to allow
3305 the child procedure to access it. */
3306 if (is_internal_unit (dtp))
3307 stash_internal_unit (dtp);
3309 /* Call the user defined formatted READ procedure. */
3310 dtp->u.p.current_unit->child_dtio++;
3311 dtio_ptr ((void *)&list_obj, &unit, iotype, &vlist,
3312 child_iostat, child_iomsg,
3313 iotype_len, child_iomsg_len);
3314 dtp->u.p.current_unit->child_dtio--;
3316 return true;
3319 /* Get the length, data length, base pointer and rank of the variable.
3320 Set the default loop specification first. */
3322 for (dim=0; dim < nl->var_rank; dim++)
3324 nl->ls[dim].step = 1;
3325 nl->ls[dim].end = GFC_DESCRIPTOR_UBOUND(nl,dim);
3326 nl->ls[dim].start = GFC_DESCRIPTOR_LBOUND(nl,dim);
3327 nl->ls[dim].idx = nl->ls[dim].start;
3330 /* Check to see if there is a qualifier: if so, parse it.*/
3332 if (c == '(' && nl->var_rank)
3334 parsed_rank = 0;
3335 if (!nml_parse_qualifier (dtp, nl->dim, nl->ls, nl->var_rank,
3336 nl->type, nml_err_msg, nml_err_msg_size,
3337 &parsed_rank))
3339 char *nml_err_msg_end = strchr (nml_err_msg, '\0');
3340 snprintf (nml_err_msg_end,
3341 nml_err_msg_size - (nml_err_msg_end - nml_err_msg),
3342 " for namelist variable %s", nl->var_name);
3343 goto nml_err_ret;
3345 if (parsed_rank > 0)
3346 non_zero_rank_count++;
3348 qualifier_flag = 1;
3350 if ((c = next_char (dtp)) == EOF)
3351 goto nml_err_ret;
3352 unget_char (dtp, c);
3354 else if (nl->var_rank > 0)
3355 non_zero_rank_count++;
3357 /* Now parse a derived type component. The root namelist_info address
3358 is backed up, as is the previous component level. The component flag
3359 is set and the iteration is made by jumping back to get_name. */
3361 if (c == '%')
3363 if (nl->type != BT_DERIVED)
3365 snprintf (nml_err_msg, nml_err_msg_size,
3366 "Attempt to get derived component for %s", nl->var_name);
3367 goto nml_err_ret;
3370 /* Don't move first_nl further in the list if a qualifier was found. */
3371 if ((*pprev_nl == NULL && !qualifier_flag) || !component_flag)
3372 first_nl = nl;
3374 root_nl = nl;
3376 component_flag = 1;
3377 if ((c = next_char (dtp)) == EOF)
3378 goto nml_err_ret;
3379 goto get_name;
3382 /* Parse a character qualifier, if present. chigh = 0 is a default
3383 that signals that the string length = string_length. */
3385 clow = 1;
3386 chigh = 0;
3388 if (c == '(' && nl->type == BT_CHARACTER)
3390 descriptor_dimension chd[1] = { {1, clow, nl->string_length} };
3391 array_loop_spec ind[1] = { {1, clow, nl->string_length, 1} };
3393 if (!nml_parse_qualifier (dtp, chd, ind, -1, nl->type,
3394 nml_err_msg, nml_err_msg_size, &parsed_rank))
3396 char *nml_err_msg_end = strchr (nml_err_msg, '\0');
3397 snprintf (nml_err_msg_end,
3398 nml_err_msg_size - (nml_err_msg_end - nml_err_msg),
3399 " for namelist variable %s", nl->var_name);
3400 goto nml_err_ret;
3403 clow = ind[0].start;
3404 chigh = ind[0].end;
3406 if (ind[0].step != 1)
3408 snprintf (nml_err_msg, nml_err_msg_size,
3409 "Step not allowed in substring qualifier"
3410 " for namelist object %s", nl->var_name);
3411 goto nml_err_ret;
3414 if ((c = next_char (dtp)) == EOF)
3415 goto nml_err_ret;
3416 unget_char (dtp, c);
3419 /* Make sure no extraneous qualifiers are there. */
3421 if (c == '(')
3423 snprintf (nml_err_msg, nml_err_msg_size,
3424 "Qualifier for a scalar or non-character namelist object %s",
3425 nl->var_name);
3426 goto nml_err_ret;
3429 /* Make sure there is no more than one non-zero rank object. */
3430 if (non_zero_rank_count > 1)
3432 snprintf (nml_err_msg, nml_err_msg_size,
3433 "Multiple sub-objects with non-zero rank in namelist object %s",
3434 nl->var_name);
3435 non_zero_rank_count = 0;
3436 goto nml_err_ret;
3439 /* According to the standard, an equal sign MUST follow an object name. The
3440 following is possibly lax - it allows comments, blank lines and so on to
3441 intervene. eat_spaces (dtp); c = next_char (dtp); would be compliant*/
3443 free_saved (dtp);
3445 eat_separator (dtp);
3446 if (dtp->u.p.input_complete)
3447 return true;
3449 if (dtp->u.p.at_eol)
3450 finish_separator (dtp);
3451 if (dtp->u.p.input_complete)
3452 return true;
3454 if ((c = next_char (dtp)) == EOF)
3455 goto nml_err_ret;
3457 if (c != '=')
3459 snprintf (nml_err_msg, nml_err_msg_size,
3460 "Equal sign must follow namelist object name %s",
3461 nl->var_name);
3462 goto nml_err_ret;
3464 /* If a derived type, touch its components and restore the root
3465 namelist_info if we have parsed a qualified derived type
3466 component. */
3468 if (nl->type == BT_DERIVED)
3469 nml_touch_nodes (nl);
3471 if (first_nl)
3473 if (first_nl->var_rank == 0)
3475 if (component_flag && qualifier_flag)
3476 nl = first_nl;
3478 else
3479 nl = first_nl;
3482 dtp->u.p.nml_read_error = 0;
3483 if (!nml_read_obj (dtp, nl, 0, pprev_nl, nml_err_msg, nml_err_msg_size,
3484 clow, chigh))
3485 goto nml_err_ret;
3487 return true;
3489 nml_err_ret:
3491 /* The EOF error message is issued by hit_eof. Return true so that the
3492 caller does not use nml_err_msg and nml_err_msg_size to generate
3493 an unrelated error message. */
3494 if (c == EOF)
3496 dtp->u.p.input_complete = 1;
3497 unget_char (dtp, c);
3498 hit_eof (dtp);
3499 return true;
3501 return false;
3504 /* Entry point for namelist input. Goes through input until namelist name
3505 is matched. Then cycles through nml_get_obj_data until the input is
3506 completed or there is an error. */
3508 void
3509 namelist_read (st_parameter_dt *dtp)
3511 int c;
3512 char nml_err_msg[200];
3514 /* Initialize the error string buffer just in case we get an unexpected fail
3515 somewhere and end up at nml_err_ret. */
3516 strcpy (nml_err_msg, "Internal namelist read error");
3518 /* Pointer to the previously read object, in case attempt is made to read
3519 new object name. Should this fail, error message can give previous
3520 name. */
3521 namelist_info *prev_nl = NULL;
3523 dtp->u.p.namelist_mode = 1;
3524 dtp->u.p.input_complete = 0;
3525 dtp->u.p.expanded_read = 0;
3527 /* Set the next_char and push_char worker functions. */
3528 set_workers (dtp);
3530 /* Look for &namelist_name . Skip all characters, testing for $nmlname.
3531 Exit on success or EOF. If '?' or '=?' encountered in stdin, print
3532 node names or namelist on stdout. */
3534 find_nml_name:
3535 c = next_char (dtp);
3536 switch (c)
3538 case '$':
3539 case '&':
3540 break;
3542 case '!':
3543 eat_line (dtp);
3544 goto find_nml_name;
3546 case '=':
3547 c = next_char (dtp);
3548 if (c == '?')
3549 nml_query (dtp, '=');
3550 else
3551 unget_char (dtp, c);
3552 goto find_nml_name;
3554 case '?':
3555 nml_query (dtp, '?');
3556 goto find_nml_name;
3558 case EOF:
3559 return;
3561 default:
3562 goto find_nml_name;
3565 /* Match the name of the namelist. */
3567 nml_match_name (dtp, dtp->namelist_name, dtp->namelist_name_len);
3569 if (dtp->u.p.nml_read_error)
3570 goto find_nml_name;
3572 /* A trailing space is required, we give a little latitude here, 10.9.1. */
3573 c = next_char (dtp);
3574 if (!is_separator(c) && c != '!')
3576 unget_char (dtp, c);
3577 goto find_nml_name;
3580 unget_char (dtp, c);
3581 eat_separator (dtp);
3583 /* Ready to read namelist objects. If there is an error in input
3584 from stdin, output the error message and continue. */
3586 while (!dtp->u.p.input_complete)
3588 if (!nml_get_obj_data (dtp, &prev_nl, nml_err_msg, sizeof nml_err_msg))
3590 if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
3591 goto nml_err_ret;
3592 generate_error (&dtp->common, LIBERROR_READ_VALUE, nml_err_msg);
3595 /* Reset the previous namelist pointer if we know we are not going
3596 to be doing multiple reads within a single namelist object. */
3597 if (prev_nl && prev_nl->var_rank == 0)
3598 prev_nl = NULL;
3601 free_saved (dtp);
3602 free_line (dtp);
3603 return;
3606 nml_err_ret:
3608 /* All namelist error calls return from here */
3609 free_saved (dtp);
3610 free_line (dtp);
3611 generate_error (&dtp->common, LIBERROR_READ_VALUE, nml_err_msg);
3612 return;