Drop excess size used for run time allocated stack variables.
[official-gcc.git] / libgfortran / io / list_read.c
blob244430d9765bee1919eb26f585a02cba2a4c9ccc
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
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 <stdlib.h>
33 #include <ctype.h>
35 typedef unsigned char uchar;
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. */
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. */
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'
55 #define CASE_SEPARATORS case ' ': case ',': case '/': case '\n': \
56 case '\t': case '\r': case ';'
58 /* This macro assumes that we're operating on a variable. */
60 #define is_separator(c) (c == '/' || c == ',' || c == '\n' || c == ' ' \
61 || c == '\t' || c == '\r' || c == ';' || \
62 (dtp->u.p.namelist_mode && c == '!'))
64 /* Maximum repeat count. Less than ten times the maximum signed int32. */
66 #define MAX_REPEAT 200000000
69 #define MSGLEN 100
72 /* Wrappers for calling the current worker functions. */
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))
77 /* Worker function to save a default KIND=1 character to a string
78 buffer, enlarging it as necessary. */
80 static void
81 push_char_default (st_parameter_dt *dtp, int c)
85 if (dtp->u.p.saved_string == NULL)
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;
93 if (dtp->u.p.saved_used >= dtp->u.p.saved_length)
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);
100 dtp->u.p.saved_string[dtp->u.p.saved_used++] = (char) c;
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)
109 gfc_char4_t *p = (gfc_char4_t *) dtp->u.p.saved_string;
111 if (p == NULL)
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;
119 if (dtp->u.p.saved_used >= dtp->u.p.saved_length)
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;
128 p[dtp->u.p.saved_used++] = c;
132 /* Free the input buffer if necessary. */
134 static void
135 free_saved (st_parameter_dt *dtp)
137 if (dtp->u.p.saved_string == NULL)
138 return;
140 free (dtp->u.p.saved_string);
142 dtp->u.p.saved_string = NULL;
143 dtp->u.p.saved_used = 0;
147 /* Free the line buffer if necessary. */
149 static void
150 free_line (st_parameter_dt *dtp)
152 dtp->u.p.line_buffer_pos = 0;
153 dtp->u.p.line_buffer_enabled = 0;
155 if (dtp->u.p.line_buffer == NULL)
156 return;
158 free (dtp->u.p.line_buffer);
159 dtp->u.p.line_buffer = NULL;
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. */
167 static int
168 check_buffers (st_parameter_dt *dtp)
170 int c;
172 c = '\0';
173 if (dtp->u.p.last_char != EOF - 1)
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;
181 /* Read from line_buffer if enabled. */
183 if (dtp->u.p.line_buffer_enabled)
185 dtp->u.p.at_eol = 0;
187 c = dtp->u.p.line_buffer[dtp->u.p.line_buffer_pos];
188 if (c != '\0' && dtp->u.p.line_buffer_pos < 64)
190 dtp->u.p.line_buffer[dtp->u.p.line_buffer_pos] = '\0';
191 dtp->u.p.line_buffer_pos++;
192 goto done;
195 dtp->u.p.line_buffer_pos = 0;
196 dtp->u.p.line_buffer_enabled = 0;
199 done:
200 dtp->u.p.at_eol = (c == '\n' || c == '\r' || c == EOF);
201 return c;
205 /* Worker function for default character encoded file. */
206 static int
207 next_char_default (st_parameter_dt *dtp)
209 int c;
211 /* Always check the unget and line buffer first. */
212 if ((c = check_buffers (dtp)))
213 return c;
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++;
219 dtp->u.p.at_eol = (c == '\n' || c == EOF);
220 return c;
224 /* Worker function for internal and array I/O units. */
225 static int
226 next_char_internal (st_parameter_dt *dtp)
228 ssize_t length;
229 gfc_offset record;
230 int c;
232 /* Always check the unget and line buffer first. */
233 if ((c = check_buffers (dtp)))
234 return c;
236 /* Handle the end-of-record and end-of-file conditions for
237 internal array unit. */
238 if (is_array_io (dtp))
240 if (dtp->u.p.at_eof)
241 return EOF;
243 /* Check for "end-of-record" condition. */
244 if (dtp->u.p.current_unit->bytes_left == 0)
246 int finished;
248 c = '\n';
249 record = next_array_record (dtp, dtp->u.p.current_unit->ls,
250 &finished);
252 /* Check for "end-of-file" condition. */
253 if (finished)
255 dtp->u.p.at_eof = 1;
256 goto done;
259 record *= dtp->u.p.current_unit->recl;
260 if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
261 return EOF;
263 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
264 goto done;
268 /* Get the next character and handle end-of-record conditions. */
270 if (dtp->common.unit) /* Check for kind=4 internal unit. */
271 length = sread (dtp->u.p.current_unit->s, &c, 1);
272 else
274 char cc;
275 length = sread (dtp->u.p.current_unit->s, &cc, 1);
276 c = cc;
279 if (unlikely (length < 0))
281 generate_error (&dtp->common, LIBERROR_OS, NULL);
282 return '\0';
285 if (is_array_io (dtp))
287 /* Check whether we hit EOF. */
288 if (unlikely (length == 0))
290 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
291 return '\0';
293 dtp->u.p.current_unit->bytes_left--;
295 else
297 if (dtp->u.p.at_eof)
298 return EOF;
299 if (length == 0)
301 c = '\n';
302 dtp->u.p.at_eof = 1;
306 done:
307 dtp->u.p.at_eol = (c == '\n' || c == EOF);
308 return c;
312 /* Worker function for UTF encoded files. */
313 static int
314 next_char_utf8 (st_parameter_dt *dtp)
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;
321 /* Always check the unget and line buffer first. */
322 if (!(c = check_buffers (dtp)))
323 c = fbuf_getc (dtp->u.p.current_unit);
325 if (c < 0x80)
326 goto utf_done;
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;
335 found:
336 c = (c & masks[nb-1]);
338 /* Decode the bytes read. */
339 for (i = 1; i < nb; i++)
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));
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;
354 /* Make sure the character is valid. */
355 if (c > 0x7FFFFFFF || (c >= 0xD800 && c <= 0xDFFF))
356 goto invalid;
358 utf_done:
359 dtp->u.p.at_eol = (c == '\n' || c == (gfc_char4_t) EOF);
360 return (int) c;
362 invalid:
363 generate_error (&dtp->common, LIBERROR_READ_VALUE, "Invalid UTF-8 encoding");
364 return (gfc_char4_t) '?';
367 /* Push a character back onto the input. */
369 static void
370 unget_char (st_parameter_dt *dtp, int c)
372 dtp->u.p.last_char = c;
376 /* Skip over spaces in the input. Returns the nonspace character that
377 terminated the eating and also places it back on the input. */
379 static int
380 eat_spaces (st_parameter_dt *dtp)
382 int c;
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))
390 gfc_offset offset = stell (dtp->u.p.current_unit->s);
391 gfc_offset i;
393 if (dtp->common.unit) /* kind=4 */
395 for (i = 0; i < dtp->u.p.current_unit->bytes_left; i++)
397 if (dtp->internal_unit[(offset + i) * sizeof (gfc_char4_t)]
398 != (gfc_char4_t)' ')
399 break;
402 else
404 for (i = 0; i < dtp->u.p.current_unit->bytes_left; i++)
406 if (dtp->internal_unit[offset + i] != ' ')
407 break;
411 if (i != 0)
413 sseek (dtp->u.p.current_unit->s, offset + i, SEEK_SET);
414 dtp->u.p.current_unit->bytes_left -= i;
418 /* Now skip spaces, EOF and EOL are handled in next_char. */
420 c = next_char (dtp);
421 while (c != EOF && (c == ' ' || c == '\r' || c == '\t'));
423 unget_char (dtp, c);
424 return c;
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. */
432 static int
433 eat_line (st_parameter_dt *dtp)
435 int c;
438 c = next_char (dtp);
439 while (c != EOF && c != '\n');
440 if (c == EOF)
441 return LIBERROR_END;
442 return 0;
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.
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.
457 Returns 0 for success, and non-zero error code otherwise. */
459 static int
460 eat_separator (st_parameter_dt *dtp)
462 int c, n;
463 int err = 0;
465 eat_spaces (dtp);
466 dtp->u.p.comma_flag = 0;
468 if ((c = next_char (dtp)) == EOF)
469 return LIBERROR_END;
470 switch (c)
472 case ',':
473 if (dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
475 unget_char (dtp, c);
476 break;
478 /* Fall through. */
479 case ';':
480 dtp->u.p.comma_flag = 1;
481 eat_spaces (dtp);
482 break;
484 case '/':
485 dtp->u.p.input_complete = 1;
486 break;
488 case '\r':
489 if ((n = next_char(dtp)) == EOF)
490 return LIBERROR_END;
491 if (n != '\n')
493 unget_char (dtp, n);
494 break;
496 /* Fall through. */
497 case '\n':
498 dtp->u.p.at_eol = 1;
499 if (dtp->u.p.namelist_mode)
503 if ((c = next_char (dtp)) == EOF)
504 return LIBERROR_END;
505 if (c == '!')
507 err = eat_line (dtp);
508 if (err)
509 return err;
510 c = '\n';
513 while (c == '\n' || c == '\r' || c == ' ' || c == '\t');
514 unget_char (dtp, c);
516 break;
518 case '!':
519 /* Eat a namelist comment. */
520 if (dtp->u.p.namelist_mode)
522 err = eat_line (dtp);
523 if (err)
524 return err;
526 break;
529 /* Fall Through... */
531 default:
532 unget_char (dtp, c);
533 break;
535 return err;
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. */
544 static int
545 finish_separator (st_parameter_dt *dtp)
547 int c;
548 int err = LIBERROR_OK;
550 restart:
551 eat_spaces (dtp);
553 if ((c = next_char (dtp)) == EOF)
554 return LIBERROR_END;
555 switch (c)
557 case ',':
558 if (dtp->u.p.comma_flag)
559 unget_char (dtp, c);
560 else
562 if ((c = eat_spaces (dtp)) == EOF)
563 return LIBERROR_END;
564 if (c == '\n' || c == '\r')
565 goto restart;
568 break;
570 case '/':
571 dtp->u.p.input_complete = 1;
572 if (!dtp->u.p.namelist_mode)
573 return err;
574 break;
576 case '\n':
577 case '\r':
578 goto restart;
580 case '!':
581 if (dtp->u.p.namelist_mode)
583 err = eat_line (dtp);
584 if (err)
585 return err;
586 goto restart;
588 /* Fall through. */
589 default:
590 unget_char (dtp, c);
591 break;
593 return err;
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. */
601 static int
602 nml_bad_return (st_parameter_dt *dtp, char c)
604 if (dtp->u.p.namelist_mode)
606 dtp->u.p.nml_read_error = 1;
607 unget_char (dtp, c);
608 return 1;
610 return 0;
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. */
617 static int
618 convert_integer (st_parameter_dt *dtp, int length, int negative)
620 char c, *buffer, message[MSGLEN];
621 int m;
622 GFC_UINTEGER_LARGEST v, max, max10;
623 GFC_INTEGER_LARGEST value;
625 buffer = dtp->u.p.saved_string;
626 v = 0;
628 if (length == -1)
629 max = MAX_REPEAT;
630 else
632 max = si_max (length);
633 if (negative)
634 max++;
636 max10 = max / 10;
638 for (;;)
640 c = *buffer++;
641 if (c == '\0')
642 break;
643 c -= '0';
645 if (v > max10)
646 goto overflow;
647 v = 10 * v;
649 if (v > max - c)
650 goto overflow;
651 v += c;
654 m = 0;
656 if (length != -1)
658 if (negative)
659 value = -v;
660 else
661 value = v;
662 set_integer (dtp->u.p.value, value, length);
664 else
666 dtp->u.p.repeat_count = v;
668 if (dtp->u.p.repeat_count == 0)
670 snprintf (message, MSGLEN, "Zero repeat count in item %d of list input",
671 dtp->u.p.item_count);
673 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
674 m = 1;
678 free_saved (dtp);
679 return m;
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);
689 free_saved (dtp);
690 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
692 return 1;
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. */
700 static int
701 parse_repeat (st_parameter_dt *dtp)
703 char message[MSGLEN];
704 int c, repeat;
706 if ((c = next_char (dtp)) == EOF)
707 goto bad_repeat;
708 switch (c)
710 CASE_DIGITS:
711 repeat = c - '0';
712 break;
714 CASE_SEPARATORS:
715 unget_char (dtp, c);
716 eat_separator (dtp);
717 return 1;
719 default:
720 unget_char (dtp, c);
721 return 0;
724 for (;;)
726 c = next_char (dtp);
727 switch (c)
729 CASE_DIGITS:
730 repeat = 10 * repeat + c - '0';
732 if (repeat > MAX_REPEAT)
734 snprintf (message, MSGLEN,
735 "Repeat count overflow in item %d of list input",
736 dtp->u.p.item_count);
738 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
739 return 1;
742 break;
744 case '*':
745 if (repeat == 0)
747 snprintf (message, MSGLEN,
748 "Zero repeat count in item %d of list input",
749 dtp->u.p.item_count);
751 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
752 return 1;
755 goto done;
757 default:
758 goto bad_repeat;
762 done:
763 dtp->u.p.repeat_count = repeat;
764 return 0;
766 bad_repeat:
768 free_saved (dtp);
769 if (c == EOF)
771 free_line (dtp);
772 hit_eof (dtp);
773 return 1;
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;
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. */
789 static void
790 l_push_char (st_parameter_dt *dtp, char c)
792 if (dtp->u.p.line_buffer == NULL)
793 dtp->u.p.line_buffer = xcalloc (SCRATCH_SIZE, 1);
795 dtp->u.p.line_buffer[dtp->u.p.line_buffer_pos++] = c;
799 /* Read a logical character on the input. */
801 static void
802 read_logical (st_parameter_dt *dtp, int length)
804 char message[MSGLEN];
805 int c, i, v;
807 if (parse_repeat (dtp))
808 return;
810 c = tolower (next_char (dtp));
811 l_push_char (dtp, c);
812 switch (c)
814 case 't':
815 v = 1;
816 c = next_char (dtp);
817 l_push_char (dtp, c);
819 if (!is_separator(c) && c != EOF)
820 goto possible_name;
822 unget_char (dtp, c);
823 break;
824 case 'f':
825 v = 0;
826 c = next_char (dtp);
827 l_push_char (dtp, c);
829 if (!is_separator(c) && c != EOF)
830 goto possible_name;
832 unget_char (dtp, c);
833 break;
835 case '.':
836 c = tolower (next_char (dtp));
837 switch (c)
839 case 't':
840 v = 1;
841 break;
842 case 'f':
843 v = 0;
844 break;
845 default:
846 goto bad_logical;
849 break;
851 case '!':
852 if (!dtp->u.p.namelist_mode)
853 goto bad_logical;
855 CASE_SEPARATORS:
856 case EOF:
857 unget_char (dtp, c);
858 eat_separator (dtp);
859 return; /* Null value. */
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;
868 dtp->u.p.saved_type = BT_LOGICAL;
869 dtp->u.p.saved_length = length;
871 /* Eat trailing garbage. */
873 c = next_char (dtp);
874 while (c != EOF && !is_separator (c));
876 unget_char (dtp, c);
877 eat_separator (dtp);
878 set_integer ((int *) dtp->u.p.value, v, length);
879 free_line (dtp);
881 return;
883 possible_name:
885 for(i = 0; i < 63; i++)
887 c = next_char (dtp);
888 if (is_separator(c))
890 /* All done if this is not a namelist read. */
891 if (!dtp->u.p.namelist_mode)
892 goto logical_done;
894 unget_char (dtp, c);
895 eat_separator (dtp);
896 c = next_char (dtp);
897 if (c != '=')
899 unget_char (dtp, c);
900 goto logical_done;
904 l_push_char (dtp, c);
905 if (c == '=')
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;
915 bad_logical:
917 if (nml_bad_return (dtp, c))
919 free_line (dtp);
920 return;
924 free_saved (dtp);
925 if (c == EOF)
927 free_line (dtp);
928 hit_eof (dtp);
929 return;
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;
939 logical_done:
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);
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. */
954 static void
955 read_integer (st_parameter_dt *dtp, int length)
957 char message[MSGLEN];
958 int c, negative;
960 negative = 0;
962 c = next_char (dtp);
963 switch (c)
965 case '-':
966 negative = 1;
967 /* Fall through... */
969 case '+':
970 if ((c = next_char (dtp)) == EOF)
971 goto bad_integer;
972 goto get_integer;
974 case '!':
975 if (!dtp->u.p.namelist_mode)
976 goto bad_integer;
978 CASE_SEPARATORS: /* Single null. */
979 unget_char (dtp, c);
980 eat_separator (dtp);
981 return;
983 CASE_DIGITS:
984 push_char (dtp, c);
985 break;
987 default:
988 goto bad_integer;
991 /* Take care of what may be a repeat count. */
993 for (;;)
995 c = next_char (dtp);
996 switch (c)
998 CASE_DIGITS:
999 push_char (dtp, c);
1000 break;
1002 case '*':
1003 push_char (dtp, '\0');
1004 goto repeat;
1006 case '!':
1007 if (!dtp->u.p.namelist_mode)
1008 goto bad_integer;
1010 CASE_SEPARATORS: /* Not a repeat count. */
1011 case EOF:
1012 goto done;
1014 default:
1015 goto bad_integer;
1019 repeat:
1020 if (convert_integer (dtp, -1, 0))
1021 return;
1023 /* Get the real integer. */
1025 if ((c = next_char (dtp)) == EOF)
1026 goto bad_integer;
1027 switch (c)
1029 CASE_DIGITS:
1030 break;
1032 case '!':
1033 if (!dtp->u.p.namelist_mode)
1034 goto bad_integer;
1036 CASE_SEPARATORS:
1037 unget_char (dtp, c);
1038 eat_separator (dtp);
1039 return;
1041 case '-':
1042 negative = 1;
1043 /* Fall through... */
1045 case '+':
1046 c = next_char (dtp);
1047 break;
1050 get_integer:
1051 if (!isdigit (c))
1052 goto bad_integer;
1053 push_char (dtp, c);
1055 for (;;)
1057 c = next_char (dtp);
1058 switch (c)
1060 CASE_DIGITS:
1061 push_char (dtp, c);
1062 break;
1064 case '!':
1065 if (!dtp->u.p.namelist_mode)
1066 goto bad_integer;
1068 CASE_SEPARATORS:
1069 case EOF:
1070 goto done;
1072 default:
1073 goto bad_integer;
1077 bad_integer:
1079 if (nml_bad_return (dtp, c))
1080 return;
1082 free_saved (dtp);
1083 if (c == EOF)
1085 free_line (dtp);
1086 hit_eof (dtp);
1087 return;
1089 else if (c != '\n')
1090 eat_line (dtp);
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);
1097 return;
1099 done:
1100 unget_char (dtp, c);
1101 eat_separator (dtp);
1103 push_char (dtp, '\0');
1104 if (convert_integer (dtp, length, negative))
1106 free_saved (dtp);
1107 return;
1110 free_saved (dtp);
1111 dtp->u.p.saved_type = BT_INTEGER;
1115 /* Read a character variable. */
1117 static void
1118 read_character (st_parameter_dt *dtp, int length __attribute__ ((unused)))
1120 char quote, message[MSGLEN];
1121 int c;
1123 quote = ' '; /* Space means no quote character. */
1125 if ((c = next_char (dtp)) == EOF)
1126 goto eof;
1127 switch (c)
1129 CASE_DIGITS:
1130 push_char (dtp, c);
1131 break;
1133 CASE_SEPARATORS:
1134 case EOF:
1135 unget_char (dtp, c); /* NULL value. */
1136 eat_separator (dtp);
1137 return;
1139 case '"':
1140 case '\'':
1141 quote = c;
1142 goto get_string;
1144 default:
1145 if (dtp->u.p.namelist_mode)
1147 unget_char (dtp, c);
1148 return;
1150 push_char (dtp, c);
1151 goto get_string;
1154 /* Deal with a possible repeat count. */
1156 for (;;)
1158 c = next_char (dtp);
1159 switch (c)
1161 CASE_DIGITS:
1162 push_char (dtp, c);
1163 break;
1165 CASE_SEPARATORS:
1166 case EOF:
1167 unget_char (dtp, c);
1168 goto done; /* String was only digits! */
1170 case '*':
1171 push_char (dtp, '\0');
1172 goto got_repeat;
1174 default:
1175 push_char (dtp, c);
1176 goto get_string; /* Not a repeat count after all. */
1180 got_repeat:
1181 if (convert_integer (dtp, -1, 0))
1182 return;
1184 /* Now get the real string. */
1186 if ((c = next_char (dtp)) == EOF)
1187 goto eof;
1188 switch (c)
1190 CASE_SEPARATORS:
1191 unget_char (dtp, c); /* Repeated NULL values. */
1192 eat_separator (dtp);
1193 return;
1195 case '"':
1196 case '\'':
1197 quote = c;
1198 break;
1200 default:
1201 push_char (dtp, c);
1202 break;
1205 get_string:
1207 for (;;)
1209 if ((c = next_char (dtp)) == EOF)
1210 goto done_eof;
1211 switch (c)
1213 case '"':
1214 case '\'':
1215 if (c != quote)
1217 push_char (dtp, c);
1218 break;
1221 /* See if we have a doubled quote character or the end of
1222 the string. */
1224 if ((c = next_char (dtp)) == EOF)
1225 goto done_eof;
1226 if (c == quote)
1228 push_char (dtp, quote);
1229 break;
1232 unget_char (dtp, c);
1233 goto done;
1235 CASE_SEPARATORS:
1236 if (quote == ' ')
1238 unget_char (dtp, c);
1239 goto done;
1242 if (c != '\n' && c != '\r')
1243 push_char (dtp, c);
1244 break;
1246 default:
1247 push_char (dtp, c);
1248 break;
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)
1259 unget_char (dtp, c);
1260 eat_separator (dtp);
1261 dtp->u.p.saved_type = BT_CHARACTER;
1263 else
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);
1270 free_line (dtp);
1271 return;
1273 eof:
1274 free_saved (dtp);
1275 free_line (dtp);
1276 hit_eof (dtp);
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. */
1283 static int
1284 parse_real (st_parameter_dt *dtp, void *buffer, int length)
1286 char message[MSGLEN];
1287 int c, m, seen_dp;
1289 if ((c = next_char (dtp)) == EOF)
1290 goto bad;
1292 if (c == '-' || c == '+')
1294 push_char (dtp, c);
1295 if ((c = next_char (dtp)) == EOF)
1296 goto bad;
1299 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1300 c = '.';
1302 if (!isdigit (c) && c != '.')
1304 if (c == 'i' || c == 'I' || c == 'n' || c == 'N')
1305 goto inf_nan;
1306 else
1307 goto bad;
1310 push_char (dtp, c);
1312 seen_dp = (c == '.') ? 1 : 0;
1314 for (;;)
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)
1322 CASE_DIGITS:
1323 push_char (dtp, c);
1324 break;
1326 case '.':
1327 if (seen_dp)
1328 goto bad;
1330 seen_dp = 1;
1331 push_char (dtp, c);
1332 break;
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;
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;
1351 case '!':
1352 if (!dtp->u.p.namelist_mode)
1353 goto bad;
1355 CASE_SEPARATORS:
1356 case EOF:
1357 goto done;
1359 default:
1360 goto done;
1364 exp1:
1365 if ((c = next_char (dtp)) == EOF)
1366 goto bad;
1367 if (c != '-' && c != '+')
1368 push_char (dtp, '+');
1369 else
1371 push_char (dtp, c);
1372 c = next_char (dtp);
1375 exp2:
1376 if (!isdigit (c))
1377 goto bad_exponent;
1379 push_char (dtp, c);
1381 for (;;)
1383 if ((c = next_char (dtp)) == EOF)
1384 goto bad;
1385 switch (c)
1387 CASE_DIGITS:
1388 push_char (dtp, c);
1389 break;
1391 case '!':
1392 if (!dtp->u.p.namelist_mode)
1393 goto bad;
1395 CASE_SEPARATORS:
1396 case EOF:
1397 unget_char (dtp, c);
1398 goto done;
1400 default:
1401 goto done;
1405 done:
1406 unget_char (dtp, c);
1407 push_char (dtp, '\0');
1409 m = convert_real (dtp, buffer, dtp->u.p.saved_string, length);
1410 free_saved (dtp);
1412 return m;
1414 done_infnan:
1415 unget_char (dtp, c);
1416 push_char (dtp, '\0');
1418 m = convert_infnan (dtp, buffer, dtp->u.p.saved_string, length);
1419 free_saved (dtp);
1421 return m;
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'))
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))))
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;
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)))
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');
1456 /* Match "NAN(alphanum)". */
1457 if (c == '(')
1459 for ( ; c != ')'; c = next_char (dtp))
1460 if (is_separator (c))
1461 goto bad;
1463 c = next_char (dtp);
1464 if (is_separator (c) || (c == EOF))
1465 unget_char (dtp, c);
1467 goto done_infnan;
1470 bad:
1472 if (nml_bad_return (dtp, c))
1473 return 0;
1475 bad_exponent:
1477 free_saved (dtp);
1478 if (c == EOF)
1480 free_line (dtp);
1481 hit_eof (dtp);
1482 return 1;
1484 else if (c != '\n')
1485 eat_line (dtp);
1487 snprintf (message, MSGLEN, "Bad complex floating point "
1488 "number for item %d", dtp->u.p.item_count);
1489 free_line (dtp);
1490 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1492 return 1;
1496 /* Reading a complex number is straightforward because we can tell
1497 what it is right away. */
1499 static void
1500 read_complex (st_parameter_dt *dtp, void * dest, int kind, size_t size)
1502 char message[MSGLEN];
1503 int c;
1505 if (parse_repeat (dtp))
1506 return;
1508 c = next_char (dtp);
1509 switch (c)
1511 case '(':
1512 break;
1514 case '!':
1515 if (!dtp->u.p.namelist_mode)
1516 goto bad_complex;
1518 CASE_SEPARATORS:
1519 case EOF:
1520 unget_char (dtp, c);
1521 eat_separator (dtp);
1522 return;
1524 default:
1525 goto bad_complex;
1528 eol_1:
1529 eat_spaces (dtp);
1530 c = next_char (dtp);
1531 if (c == '\n' || c== '\r')
1532 goto eol_1;
1533 else
1534 unget_char (dtp, c);
1536 if (parse_real (dtp, dest, kind))
1537 return;
1539 eol_2:
1540 eat_spaces (dtp);
1541 c = next_char (dtp);
1542 if (c == '\n' || c== '\r')
1543 goto eol_2;
1544 else
1545 unget_char (dtp, c);
1547 if (next_char (dtp)
1548 != (dtp->u.p.current_unit->decimal_status == DECIMAL_POINT ? ',' : ';'))
1549 goto bad_complex;
1551 eol_3:
1552 eat_spaces (dtp);
1553 c = next_char (dtp);
1554 if (c == '\n' || c== '\r')
1555 goto eol_3;
1556 else
1557 unget_char (dtp, c);
1559 if (parse_real (dtp, dest + size / 2, kind))
1560 return;
1562 eol_4:
1563 eat_spaces (dtp);
1564 c = next_char (dtp);
1565 if (c == '\n' || c== '\r')
1566 goto eol_4;
1567 else
1568 unget_char (dtp, c);
1570 if (next_char (dtp) != ')')
1571 goto bad_complex;
1573 c = next_char (dtp);
1574 if (!is_separator (c) && (c != EOF))
1575 goto bad_complex;
1577 unget_char (dtp, c);
1578 eat_separator (dtp);
1580 free_saved (dtp);
1581 dtp->u.p.saved_type = BT_COMPLEX;
1582 return;
1584 bad_complex:
1586 if (nml_bad_return (dtp, c))
1587 return;
1589 free_saved (dtp);
1590 if (c == EOF)
1592 free_line (dtp);
1593 hit_eof (dtp);
1594 return;
1596 else if (c != '\n')
1597 eat_line (dtp);
1599 snprintf (message, MSGLEN, "Bad complex value in item %d of list input",
1600 dtp->u.p.item_count);
1601 free_line (dtp);
1602 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1606 /* Parse a real number with a possible repeat count. */
1608 static void
1609 read_real (st_parameter_dt *dtp, void * dest, int length)
1611 char message[MSGLEN];
1612 int c;
1613 int seen_dp;
1614 int is_inf;
1616 seen_dp = 0;
1618 c = next_char (dtp);
1619 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1620 c = '.';
1621 switch (c)
1623 CASE_DIGITS:
1624 push_char (dtp, c);
1625 break;
1627 case '.':
1628 push_char (dtp, c);
1629 seen_dp = 1;
1630 break;
1632 case '+':
1633 case '-':
1634 goto got_sign;
1636 case '!':
1637 if (!dtp->u.p.namelist_mode)
1638 goto bad_real;
1640 CASE_SEPARATORS:
1641 unget_char (dtp, c); /* Single null. */
1642 eat_separator (dtp);
1643 return;
1645 case 'i':
1646 case 'I':
1647 case 'n':
1648 case 'N':
1649 goto inf_nan;
1651 default:
1652 goto bad_real;
1655 /* Get the digit string that might be a repeat count. */
1657 for (;;)
1659 c = next_char (dtp);
1660 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1661 c = '.';
1662 switch (c)
1664 CASE_DIGITS:
1665 push_char (dtp, c);
1666 break;
1668 case '.':
1669 if (seen_dp)
1670 goto bad_real;
1672 seen_dp = 1;
1673 push_char (dtp, c);
1674 goto real_loop;
1676 case 'E':
1677 case 'e':
1678 case 'D':
1679 case 'd':
1680 case 'Q':
1681 case 'q':
1682 goto exp1;
1684 case '+':
1685 case '-':
1686 push_char (dtp, 'e');
1687 push_char (dtp, c);
1688 c = next_char (dtp);
1689 goto exp2;
1691 case '*':
1692 push_char (dtp, '\0');
1693 goto got_repeat;
1695 case '!':
1696 if (!dtp->u.p.namelist_mode)
1697 goto bad_real;
1699 CASE_SEPARATORS:
1700 case EOF:
1701 if (c != '\n' && c != ',' && c != '\r' && c != ';')
1702 unget_char (dtp, c);
1703 goto done;
1705 default:
1706 goto bad_real;
1710 got_repeat:
1711 if (convert_integer (dtp, -1, 0))
1712 return;
1714 /* Now get the number itself. */
1716 if ((c = next_char (dtp)) == EOF)
1717 goto bad_real;
1718 if (is_separator (c))
1719 { /* Repeated null value. */
1720 unget_char (dtp, c);
1721 eat_separator (dtp);
1722 return;
1725 if (c != '-' && c != '+')
1726 push_char (dtp, '+');
1727 else
1729 got_sign:
1730 push_char (dtp, c);
1731 if ((c = next_char (dtp)) == EOF)
1732 goto bad_real;
1735 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1736 c = '.';
1738 if (!isdigit (c) && c != '.')
1740 if (c == 'i' || c == 'I' || c == 'n' || c == 'N')
1741 goto inf_nan;
1742 else
1743 goto bad_real;
1746 if (c == '.')
1748 if (seen_dp)
1749 goto bad_real;
1750 else
1751 seen_dp = 1;
1754 push_char (dtp, c);
1756 real_loop:
1757 for (;;)
1759 c = next_char (dtp);
1760 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1761 c = '.';
1762 switch (c)
1764 CASE_DIGITS:
1765 push_char (dtp, c);
1766 break;
1768 case '!':
1769 if (!dtp->u.p.namelist_mode)
1770 goto bad_real;
1772 CASE_SEPARATORS:
1773 case EOF:
1774 goto done;
1776 case '.':
1777 if (seen_dp)
1778 goto bad_real;
1780 seen_dp = 1;
1781 push_char (dtp, c);
1782 break;
1784 case 'E':
1785 case 'e':
1786 case 'D':
1787 case 'd':
1788 case 'Q':
1789 case 'q':
1790 goto exp1;
1792 case '+':
1793 case '-':
1794 push_char (dtp, 'e');
1795 push_char (dtp, c);
1796 c = next_char (dtp);
1797 goto exp2;
1799 default:
1800 goto bad_real;
1804 exp1:
1805 push_char (dtp, 'e');
1807 if ((c = next_char (dtp)) == EOF)
1808 goto bad_real;
1809 if (c != '+' && c != '-')
1810 push_char (dtp, '+');
1811 else
1813 push_char (dtp, c);
1814 c = next_char (dtp);
1817 exp2:
1818 if (!isdigit (c))
1819 goto bad_exponent;
1821 push_char (dtp, c);
1823 for (;;)
1825 c = next_char (dtp);
1827 switch (c)
1829 CASE_DIGITS:
1830 push_char (dtp, c);
1831 break;
1833 case '!':
1834 if (!dtp->u.p.namelist_mode)
1835 goto bad_real;
1837 CASE_SEPARATORS:
1838 case EOF:
1839 goto done;
1841 default:
1842 goto bad_real;
1846 done:
1847 unget_char (dtp, c);
1848 eat_separator (dtp);
1849 push_char (dtp, '\0');
1850 if (convert_real (dtp, dest, dtp->u.p.saved_string, length))
1852 free_saved (dtp);
1853 return;
1856 free_saved (dtp);
1857 dtp->u.p.saved_type = BT_REAL;
1858 return;
1860 inf_nan:
1861 l_push_char (dtp, c);
1862 is_inf = 0;
1864 /* Match INF and Infinity. */
1865 if (c == 'i' || c == 'I')
1867 c = next_char (dtp);
1868 l_push_char (dtp, c);
1869 if (c != 'n' && c != 'N')
1870 goto unwind;
1871 c = next_char (dtp);
1872 l_push_char (dtp, c);
1873 if (c != 'f' && c != 'F')
1874 goto unwind;
1875 c = next_char (dtp);
1876 l_push_char (dtp, c);
1877 if (!is_separator (c) && (c != EOF))
1879 if (c != 'i' && c != 'I')
1880 goto unwind;
1881 c = next_char (dtp);
1882 l_push_char (dtp, c);
1883 if (c != 'n' && c != 'N')
1884 goto unwind;
1885 c = next_char (dtp);
1886 l_push_char (dtp, c);
1887 if (c != 'i' && c != 'I')
1888 goto unwind;
1889 c = next_char (dtp);
1890 l_push_char (dtp, c);
1891 if (c != 't' && c != 'T')
1892 goto unwind;
1893 c = next_char (dtp);
1894 l_push_char (dtp, c);
1895 if (c != 'y' && c != 'Y')
1896 goto unwind;
1897 c = next_char (dtp);
1898 l_push_char (dtp, c);
1900 is_inf = 1;
1901 } /* Match NaN. */
1902 else
1904 c = next_char (dtp);
1905 l_push_char (dtp, c);
1906 if (c != 'a' && c != 'A')
1907 goto unwind;
1908 c = next_char (dtp);
1909 l_push_char (dtp, c);
1910 if (c != 'n' && c != 'N')
1911 goto unwind;
1912 c = next_char (dtp);
1913 l_push_char (dtp, c);
1915 /* Match NAN(alphanum). */
1916 if (c == '(')
1918 for (c = next_char (dtp); c != ')'; c = next_char (dtp))
1919 if (is_separator (c))
1920 goto unwind;
1921 else
1922 l_push_char (dtp, c);
1924 l_push_char (dtp, ')');
1925 c = next_char (dtp);
1926 l_push_char (dtp, c);
1930 if (!is_separator (c) && (c != EOF))
1931 goto unwind;
1933 if (dtp->u.p.namelist_mode)
1935 if (c == ' ' || c =='\n' || c == '\r')
1939 if ((c = next_char (dtp)) == EOF)
1940 goto bad_real;
1942 while (c == ' ' || c =='\n' || c == '\r');
1944 l_push_char (dtp, c);
1946 if (c == '=')
1947 goto unwind;
1951 if (is_inf)
1953 push_char (dtp, 'i');
1954 push_char (dtp, 'n');
1955 push_char (dtp, 'f');
1957 else
1959 push_char (dtp, 'n');
1960 push_char (dtp, 'a');
1961 push_char (dtp, 'n');
1964 free_line (dtp);
1965 unget_char (dtp, c);
1966 eat_separator (dtp);
1967 push_char (dtp, '\0');
1968 if (convert_infnan (dtp, dest, dtp->u.p.saved_string, length))
1969 return;
1971 free_saved (dtp);
1972 dtp->u.p.saved_type = BT_REAL;
1973 return;
1975 unwind:
1976 if (dtp->u.p.namelist_mode)
1978 dtp->u.p.nml_read_error = 1;
1979 dtp->u.p.line_buffer_enabled = 1;
1980 dtp->u.p.line_buffer_pos = 0;
1981 return;
1984 bad_real:
1986 if (nml_bad_return (dtp, c))
1987 return;
1989 bad_exponent:
1991 free_saved (dtp);
1992 if (c == EOF)
1994 free_line (dtp);
1995 hit_eof (dtp);
1996 return;
1998 else if (c != '\n')
1999 eat_line (dtp);
2001 snprintf (message, MSGLEN, "Bad real number in item %d of list input",
2002 dtp->u.p.item_count);
2003 free_line (dtp);
2004 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
2008 /* Check the current type against the saved type to make sure they are
2009 compatible. Returns nonzero if incompatible. */
2011 static int
2012 check_type (st_parameter_dt *dtp, bt type, int kind)
2014 char message[MSGLEN];
2016 if (dtp->u.p.saved_type != BT_UNKNOWN && dtp->u.p.saved_type != type)
2018 snprintf (message, MSGLEN, "Read type %s where %s was expected for item %d",
2019 type_name (dtp->u.p.saved_type), type_name (type),
2020 dtp->u.p.item_count);
2021 free_line (dtp);
2022 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
2023 return 1;
2026 if (dtp->u.p.saved_type == BT_UNKNOWN || dtp->u.p.saved_type == BT_CHARACTER)
2027 return 0;
2029 if ((type != BT_COMPLEX && dtp->u.p.saved_length != kind)
2030 || (type == BT_COMPLEX && dtp->u.p.saved_length != kind*2))
2032 snprintf (message, MSGLEN,
2033 "Read kind %d %s where kind %d is required for item %d",
2034 type == BT_COMPLEX ? dtp->u.p.saved_length / 2
2035 : dtp->u.p.saved_length,
2036 type_name (dtp->u.p.saved_type), kind,
2037 dtp->u.p.item_count);
2038 free_line (dtp);
2039 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
2040 return 1;
2043 return 0;
2047 /* Initialize the function pointers to select the correct versions of
2048 next_char and push_char depending on what we are doing. */
2050 static void
2051 set_workers (st_parameter_dt *dtp)
2053 if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
2055 dtp->u.p.current_unit->next_char_fn_ptr = &next_char_utf8;
2056 dtp->u.p.current_unit->push_char_fn_ptr = &push_char4;
2058 else if (is_internal_unit (dtp))
2060 dtp->u.p.current_unit->next_char_fn_ptr = &next_char_internal;
2061 dtp->u.p.current_unit->push_char_fn_ptr = &push_char_default;
2063 else
2065 dtp->u.p.current_unit->next_char_fn_ptr = &next_char_default;
2066 dtp->u.p.current_unit->push_char_fn_ptr = &push_char_default;
2071 /* Top level data transfer subroutine for list reads. Because we have
2072 to deal with repeat counts, the data item is always saved after
2073 reading, usually in the dtp->u.p.value[] array. If a repeat count is
2074 greater than one, we copy the data item multiple times. */
2076 static int
2077 list_formatted_read_scalar (st_parameter_dt *dtp, bt type, void *p,
2078 int kind, size_t size)
2080 gfc_char4_t *q, *r;
2081 int c, i, m;
2082 int err = 0;
2084 dtp->u.p.namelist_mode = 0;
2086 /* Set the next_char and push_char worker functions. */
2087 set_workers (dtp);
2089 if (dtp->u.p.first_item)
2091 dtp->u.p.first_item = 0;
2092 dtp->u.p.input_complete = 0;
2093 dtp->u.p.repeat_count = 1;
2094 dtp->u.p.at_eol = 0;
2096 if ((c = eat_spaces (dtp)) == EOF)
2098 err = LIBERROR_END;
2099 goto cleanup;
2101 if (is_separator (c))
2103 /* Found a null value. */
2104 dtp->u.p.repeat_count = 0;
2105 eat_separator (dtp);
2107 /* Set end-of-line flag. */
2108 if (c == '\n' || c == '\r')
2110 dtp->u.p.at_eol = 1;
2111 if (finish_separator (dtp) == LIBERROR_END)
2113 err = LIBERROR_END;
2114 goto cleanup;
2117 else
2118 goto cleanup;
2121 else
2123 if (dtp->u.p.repeat_count > 0)
2125 if (check_type (dtp, type, kind))
2126 return err;
2127 goto set_value;
2130 if (dtp->u.p.input_complete)
2131 goto cleanup;
2133 if (dtp->u.p.at_eol)
2134 finish_separator (dtp);
2135 else
2137 eat_spaces (dtp);
2138 /* Trailing spaces prior to end of line. */
2139 if (dtp->u.p.at_eol)
2140 finish_separator (dtp);
2143 dtp->u.p.saved_type = BT_UNKNOWN;
2144 dtp->u.p.repeat_count = 1;
2147 switch (type)
2149 case BT_INTEGER:
2150 read_integer (dtp, kind);
2151 break;
2152 case BT_LOGICAL:
2153 read_logical (dtp, kind);
2154 break;
2155 case BT_CHARACTER:
2156 read_character (dtp, kind);
2157 break;
2158 case BT_REAL:
2159 read_real (dtp, p, kind);
2160 /* Copy value back to temporary if needed. */
2161 if (dtp->u.p.repeat_count > 0)
2162 memcpy (dtp->u.p.value, p, size);
2163 break;
2164 case BT_COMPLEX:
2165 read_complex (dtp, p, kind, size);
2166 /* Copy value back to temporary if needed. */
2167 if (dtp->u.p.repeat_count > 0)
2168 memcpy (dtp->u.p.value, p, size);
2169 break;
2170 default:
2171 internal_error (&dtp->common, "Bad type for list read");
2174 if (dtp->u.p.saved_type != BT_CHARACTER && dtp->u.p.saved_type != BT_UNKNOWN)
2175 dtp->u.p.saved_length = size;
2177 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2178 goto cleanup;
2180 set_value:
2181 switch (dtp->u.p.saved_type)
2183 case BT_COMPLEX:
2184 case BT_REAL:
2185 if (dtp->u.p.repeat_count > 0)
2186 memcpy (p, dtp->u.p.value, size);
2187 break;
2189 case BT_INTEGER:
2190 case BT_LOGICAL:
2191 memcpy (p, dtp->u.p.value, size);
2192 break;
2194 case BT_CHARACTER:
2195 if (dtp->u.p.saved_string)
2197 m = ((int) size < dtp->u.p.saved_used)
2198 ? (int) size : dtp->u.p.saved_used;
2200 q = (gfc_char4_t *) p;
2201 r = (gfc_char4_t *) dtp->u.p.saved_string;
2202 if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
2203 for (i = 0; i < m; i++)
2204 *q++ = *r++;
2205 else
2207 if (kind == 1)
2208 memcpy (p, dtp->u.p.saved_string, m);
2209 else
2210 for (i = 0; i < m; i++)
2211 *q++ = *r++;
2214 else
2215 /* Just delimiters encountered, nothing to copy but SPACE. */
2216 m = 0;
2218 if (m < (int) size)
2220 if (kind == 1)
2221 memset (((char *) p) + m, ' ', size - m);
2222 else
2224 q = (gfc_char4_t *) p;
2225 for (i = m; i < (int) size; i++)
2226 q[i] = (unsigned char) ' ';
2229 break;
2231 case BT_UNKNOWN:
2232 break;
2234 default:
2235 internal_error (&dtp->common, "Bad type for list read");
2238 if (--dtp->u.p.repeat_count <= 0)
2239 free_saved (dtp);
2241 cleanup:
2242 if (err == LIBERROR_END)
2244 free_line (dtp);
2245 hit_eof (dtp);
2247 fbuf_flush_list (dtp->u.p.current_unit, LIST_READING);
2248 return err;
2252 void
2253 list_formatted_read (st_parameter_dt *dtp, bt type, void *p, int kind,
2254 size_t size, size_t nelems)
2256 size_t elem;
2257 char *tmp;
2258 size_t stride = type == BT_CHARACTER ?
2259 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
2260 int err;
2262 tmp = (char *) p;
2264 /* Big loop over all the elements. */
2265 for (elem = 0; elem < nelems; elem++)
2267 dtp->u.p.item_count++;
2268 err = list_formatted_read_scalar (dtp, type, tmp + stride*elem,
2269 kind, size);
2270 if (err)
2271 break;
2276 /* Finish a list read. */
2278 void
2279 finish_list_read (st_parameter_dt *dtp)
2281 free_saved (dtp);
2283 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
2285 if (dtp->u.p.at_eol)
2287 dtp->u.p.at_eol = 0;
2288 return;
2291 if (!is_internal_unit (dtp))
2293 int c;
2295 /* Set the next_char and push_char worker functions. */
2296 set_workers (dtp);
2298 c = next_char (dtp);
2299 if (c == EOF)
2301 free_line (dtp);
2302 hit_eof (dtp);
2303 return;
2305 if (c != '\n')
2306 eat_line (dtp);
2309 free_line (dtp);
2313 /* NAMELIST INPUT
2315 void namelist_read (st_parameter_dt *dtp)
2316 calls:
2317 static void nml_match_name (char *name, int len)
2318 static int nml_query (st_parameter_dt *dtp)
2319 static int nml_get_obj_data (st_parameter_dt *dtp,
2320 namelist_info **prev_nl, char *, size_t)
2321 calls:
2322 static void nml_untouch_nodes (st_parameter_dt *dtp)
2323 static namelist_info * find_nml_node (st_parameter_dt *dtp,
2324 char * var_name)
2325 static int nml_parse_qualifier(descriptor_dimension * ad,
2326 array_loop_spec * ls, int rank, char *)
2327 static void nml_touch_nodes (namelist_info * nl)
2328 static int nml_read_obj (namelist_info *nl, index_type offset,
2329 namelist_info **prev_nl, char *, size_t,
2330 index_type clow, index_type chigh)
2331 calls:
2332 -itself- */
2334 /* Inputs a rank-dimensional qualifier, which can contain
2335 singlets, doublets, triplets or ':' with the standard meanings. */
2337 static bool
2338 nml_parse_qualifier (st_parameter_dt *dtp, descriptor_dimension *ad,
2339 array_loop_spec *ls, int rank, bt nml_elem_type,
2340 char *parse_err_msg, size_t parse_err_msg_size,
2341 int *parsed_rank)
2343 int dim;
2344 int indx;
2345 int neg;
2346 int null_flag;
2347 int is_array_section, is_char;
2348 int c;
2350 is_char = 0;
2351 is_array_section = 0;
2352 dtp->u.p.expanded_read = 0;
2354 /* See if this is a character substring qualifier we are looking for. */
2355 if (rank == -1)
2357 rank = 1;
2358 is_char = 1;
2361 /* The next character in the stream should be the '('. */
2363 if ((c = next_char (dtp)) == EOF)
2364 goto err_ret;
2366 /* Process the qualifier, by dimension and triplet. */
2368 for (dim=0; dim < rank; dim++ )
2370 for (indx=0; indx<3; indx++)
2372 free_saved (dtp);
2373 eat_spaces (dtp);
2374 neg = 0;
2376 /* Process a potential sign. */
2377 if ((c = next_char (dtp)) == EOF)
2378 goto err_ret;
2379 switch (c)
2381 case '-':
2382 neg = 1;
2383 break;
2385 case '+':
2386 break;
2388 default:
2389 unget_char (dtp, c);
2390 break;
2393 /* Process characters up to the next ':' , ',' or ')'. */
2394 for (;;)
2396 c = next_char (dtp);
2397 switch (c)
2399 case EOF:
2400 goto err_ret;
2402 case ':':
2403 is_array_section = 1;
2404 break;
2406 case ',': case ')':
2407 if ((c==',' && dim == rank -1)
2408 || (c==')' && dim < rank -1))
2410 if (is_char)
2411 snprintf (parse_err_msg, parse_err_msg_size,
2412 "Bad substring qualifier");
2413 else
2414 snprintf (parse_err_msg, parse_err_msg_size,
2415 "Bad number of index fields");
2416 goto err_ret;
2418 break;
2420 CASE_DIGITS:
2421 push_char (dtp, c);
2422 continue;
2424 case ' ': case '\t': case '\r': case '\n':
2425 eat_spaces (dtp);
2426 break;
2428 default:
2429 if (is_char)
2430 snprintf (parse_err_msg, parse_err_msg_size,
2431 "Bad character in substring qualifier");
2432 else
2433 snprintf (parse_err_msg, parse_err_msg_size,
2434 "Bad character in index");
2435 goto err_ret;
2438 if ((c == ',' || c == ')') && indx == 0
2439 && dtp->u.p.saved_string == 0)
2441 if (is_char)
2442 snprintf (parse_err_msg, parse_err_msg_size,
2443 "Null substring qualifier");
2444 else
2445 snprintf (parse_err_msg, parse_err_msg_size,
2446 "Null index field");
2447 goto err_ret;
2450 if ((c == ':' && indx == 1 && dtp->u.p.saved_string == 0)
2451 || (indx == 2 && dtp->u.p.saved_string == 0))
2453 if (is_char)
2454 snprintf (parse_err_msg, parse_err_msg_size,
2455 "Bad substring qualifier");
2456 else
2457 snprintf (parse_err_msg, parse_err_msg_size,
2458 "Bad index triplet");
2459 goto err_ret;
2462 if (is_char && !is_array_section)
2464 snprintf (parse_err_msg, parse_err_msg_size,
2465 "Missing colon in substring qualifier");
2466 goto err_ret;
2469 /* If '( : ? )' or '( ? : )' break and flag read failure. */
2470 null_flag = 0;
2471 if ((c == ':' && indx == 0 && dtp->u.p.saved_string == 0)
2472 || (indx==1 && dtp->u.p.saved_string == 0))
2474 null_flag = 1;
2475 break;
2478 /* Now read the index. */
2479 if (convert_integer (dtp, sizeof(index_type), neg))
2481 if (is_char)
2482 snprintf (parse_err_msg, parse_err_msg_size,
2483 "Bad integer substring qualifier");
2484 else
2485 snprintf (parse_err_msg, parse_err_msg_size,
2486 "Bad integer in index");
2487 goto err_ret;
2489 break;
2492 /* Feed the index values to the triplet arrays. */
2493 if (!null_flag)
2495 if (indx == 0)
2496 memcpy (&ls[dim].start, dtp->u.p.value, sizeof(index_type));
2497 if (indx == 1)
2498 memcpy (&ls[dim].end, dtp->u.p.value, sizeof(index_type));
2499 if (indx == 2)
2500 memcpy (&ls[dim].step, dtp->u.p.value, sizeof(index_type));
2503 /* Singlet or doublet indices. */
2504 if (c==',' || c==')')
2506 if (indx == 0)
2508 memcpy (&ls[dim].start, dtp->u.p.value, sizeof(index_type));
2510 /* If -std=f95/2003 or an array section is specified,
2511 do not allow excess data to be processed. */
2512 if (is_array_section == 1
2513 || !(compile_options.allow_std & GFC_STD_GNU)
2514 || nml_elem_type == BT_DERIVED)
2515 ls[dim].end = ls[dim].start;
2516 else
2517 dtp->u.p.expanded_read = 1;
2520 /* Check for non-zero rank. */
2521 if (is_array_section == 1 && ls[dim].start != ls[dim].end)
2522 *parsed_rank = 1;
2524 break;
2528 if (is_array_section == 1 && dtp->u.p.expanded_read == 1)
2530 int i;
2531 dtp->u.p.expanded_read = 0;
2532 for (i = 0; i < dim; i++)
2533 ls[i].end = ls[i].start;
2536 /* Check the values of the triplet indices. */
2537 if ((ls[dim].start > GFC_DIMENSION_UBOUND(ad[dim]))
2538 || (ls[dim].start < GFC_DIMENSION_LBOUND(ad[dim]))
2539 || (ls[dim].end > GFC_DIMENSION_UBOUND(ad[dim]))
2540 || (ls[dim].end < GFC_DIMENSION_LBOUND(ad[dim])))
2542 if (is_char)
2543 snprintf (parse_err_msg, parse_err_msg_size,
2544 "Substring out of range");
2545 else
2546 snprintf (parse_err_msg, parse_err_msg_size,
2547 "Index %d out of range", dim + 1);
2548 goto err_ret;
2551 if (((ls[dim].end - ls[dim].start ) * ls[dim].step < 0)
2552 || (ls[dim].step == 0))
2554 snprintf (parse_err_msg, parse_err_msg_size,
2555 "Bad range in index %d", dim + 1);
2556 goto err_ret;
2559 /* Initialise the loop index counter. */
2560 ls[dim].idx = ls[dim].start;
2562 eat_spaces (dtp);
2563 return true;
2565 err_ret:
2567 /* The EOF error message is issued by hit_eof. Return true so that the
2568 caller does not use parse_err_msg and parse_err_msg_size to generate
2569 an unrelated error message. */
2570 if (c == EOF)
2572 hit_eof (dtp);
2573 dtp->u.p.input_complete = 1;
2574 return true;
2576 return false;
2580 static bool
2581 extended_look_ahead (char *p, char *q)
2583 char *r, *s;
2585 /* Scan ahead to find a '%' in the p string. */
2586 for(r = p, s = q; *r && *s; s++)
2587 if ((*s == '%' || *s == '+') && strcmp (r + 1, s + 1) == 0)
2588 return true;
2589 return false;
2593 static bool
2594 strcmp_extended_type (char *p, char *q)
2596 char *r, *s;
2598 for (r = p, s = q; *r && *s; r++, s++)
2600 if (*r != *s)
2602 if (*r == '%' && *s == '+' && extended_look_ahead (r, s))
2603 return true;
2604 break;
2607 return false;
2611 static namelist_info *
2612 find_nml_node (st_parameter_dt *dtp, char * var_name)
2614 namelist_info * t = dtp->u.p.ionml;
2615 while (t != NULL)
2617 if (strcmp (var_name, t->var_name) == 0)
2619 t->touched = 1;
2620 return t;
2622 if (strcmp_extended_type (var_name, t->var_name))
2624 t->touched = 1;
2625 return t;
2627 t = t->next;
2629 return NULL;
2632 /* Visits all the components of a derived type that have
2633 not explicitly been identified in the namelist input.
2634 touched is set and the loop specification initialised
2635 to default values */
2637 static void
2638 nml_touch_nodes (namelist_info * nl)
2640 index_type len = strlen (nl->var_name) + 1;
2641 int dim;
2642 char * ext_name = xmalloc (len + 1);
2643 memcpy (ext_name, nl->var_name, len-1);
2644 memcpy (ext_name + len - 1, "%", 2);
2645 for (nl = nl->next; nl; nl = nl->next)
2647 if (strncmp (nl->var_name, ext_name, len) == 0)
2649 nl->touched = 1;
2650 for (dim=0; dim < nl->var_rank; dim++)
2652 nl->ls[dim].step = 1;
2653 nl->ls[dim].end = GFC_DESCRIPTOR_UBOUND(nl,dim);
2654 nl->ls[dim].start = GFC_DESCRIPTOR_LBOUND(nl,dim);
2655 nl->ls[dim].idx = nl->ls[dim].start;
2658 else
2659 break;
2661 free (ext_name);
2662 return;
2665 /* Resets touched for the entire list of nml_nodes, ready for a
2666 new object. */
2668 static void
2669 nml_untouch_nodes (st_parameter_dt *dtp)
2671 namelist_info * t;
2672 for (t = dtp->u.p.ionml; t; t = t->next)
2673 t->touched = 0;
2674 return;
2677 /* Attempts to input name to namelist name. Returns
2678 dtp->u.p.nml_read_error = 1 on no match. */
2680 static void
2681 nml_match_name (st_parameter_dt *dtp, const char *name, index_type len)
2683 index_type i;
2684 int c;
2686 dtp->u.p.nml_read_error = 0;
2687 for (i = 0; i < len; i++)
2689 c = next_char (dtp);
2690 if (c == EOF || (tolower (c) != tolower (name[i])))
2692 dtp->u.p.nml_read_error = 1;
2693 break;
2698 /* If the namelist read is from stdin, output the current state of the
2699 namelist to stdout. This is used to implement the non-standard query
2700 features, ? and =?. If c == '=' the full namelist is printed. Otherwise
2701 the names alone are printed. */
2703 static void
2704 nml_query (st_parameter_dt *dtp, char c)
2706 gfc_unit * temp_unit;
2707 namelist_info * nl;
2708 index_type len;
2709 char * p;
2710 #ifdef HAVE_CRLF
2711 static const index_type endlen = 2;
2712 static const char endl[] = "\r\n";
2713 static const char nmlend[] = "&end\r\n";
2714 #else
2715 static const index_type endlen = 1;
2716 static const char endl[] = "\n";
2717 static const char nmlend[] = "&end\n";
2718 #endif
2720 if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
2721 return;
2723 /* Store the current unit and transfer to stdout. */
2725 temp_unit = dtp->u.p.current_unit;
2726 dtp->u.p.current_unit = find_unit (options.stdout_unit);
2728 if (dtp->u.p.current_unit)
2730 dtp->u.p.mode = WRITING;
2731 next_record (dtp, 0);
2733 /* Write the namelist in its entirety. */
2735 if (c == '=')
2736 namelist_write (dtp);
2738 /* Or write the list of names. */
2740 else
2742 /* "&namelist_name\n" */
2744 len = dtp->namelist_name_len;
2745 p = write_block (dtp, len - 1 + endlen);
2746 if (!p)
2747 goto query_return;
2748 memcpy (p, "&", 1);
2749 memcpy ((char*)(p + 1), dtp->namelist_name, len);
2750 memcpy ((char*)(p + len + 1), &endl, endlen);
2751 for (nl = dtp->u.p.ionml; nl; nl = nl->next)
2753 /* " var_name\n" */
2755 len = strlen (nl->var_name);
2756 p = write_block (dtp, len + endlen);
2757 if (!p)
2758 goto query_return;
2759 memcpy (p, " ", 1);
2760 memcpy ((char*)(p + 1), nl->var_name, len);
2761 memcpy ((char*)(p + len + 1), &endl, endlen);
2764 /* "&end\n" */
2766 p = write_block (dtp, endlen + 4);
2767 if (!p)
2768 goto query_return;
2769 memcpy (p, &nmlend, endlen + 4);
2772 /* Flush the stream to force immediate output. */
2774 fbuf_flush (dtp->u.p.current_unit, WRITING);
2775 sflush (dtp->u.p.current_unit->s);
2776 unlock_unit (dtp->u.p.current_unit);
2779 query_return:
2781 /* Restore the current unit. */
2783 dtp->u.p.current_unit = temp_unit;
2784 dtp->u.p.mode = READING;
2785 return;
2788 /* Reads and stores the input for the namelist object nl. For an array,
2789 the function loops over the ranges defined by the loop specification.
2790 This default to all the data or to the specification from a qualifier.
2791 nml_read_obj recursively calls itself to read derived types. It visits
2792 all its own components but only reads data for those that were touched
2793 when the name was parsed. If a read error is encountered, an attempt is
2794 made to return to read a new object name because the standard allows too
2795 little data to be available. On the other hand, too much data is an
2796 error. */
2798 static bool
2799 nml_read_obj (st_parameter_dt *dtp, namelist_info * nl, index_type offset,
2800 namelist_info **pprev_nl, char *nml_err_msg,
2801 size_t nml_err_msg_size, index_type clow, index_type chigh)
2803 namelist_info * cmp;
2804 char * obj_name;
2805 int nml_carry;
2806 int len;
2807 int dim;
2808 index_type dlen;
2809 index_type m;
2810 size_t obj_name_len;
2811 void * pdata;
2813 /* If we have encountered a previous read error or this object has not been
2814 touched in name parsing, just return. */
2815 if (dtp->u.p.nml_read_error || !nl->touched)
2816 return true;
2818 dtp->u.p.item_count++; /* Used in error messages. */
2819 dtp->u.p.repeat_count = 0;
2820 eat_spaces (dtp);
2822 len = nl->len;
2823 switch (nl->type)
2825 case BT_INTEGER:
2826 case BT_LOGICAL:
2827 dlen = len;
2828 break;
2830 case BT_REAL:
2831 dlen = size_from_real_kind (len);
2832 break;
2834 case BT_COMPLEX:
2835 dlen = size_from_complex_kind (len);
2836 break;
2838 case BT_CHARACTER:
2839 dlen = chigh ? (chigh - clow + 1) : nl->string_length;
2840 break;
2842 default:
2843 dlen = 0;
2848 /* Update the pointer to the data, using the current index vector */
2850 pdata = (void*)(nl->mem_pos + offset);
2851 for (dim = 0; dim < nl->var_rank; dim++)
2852 pdata = (void*)(pdata + (nl->ls[dim].idx
2853 - GFC_DESCRIPTOR_LBOUND(nl,dim))
2854 * GFC_DESCRIPTOR_STRIDE(nl,dim) * nl->size);
2856 /* If we are finished with the repeat count, try to read next value. */
2858 nml_carry = 0;
2859 if (--dtp->u.p.repeat_count <= 0)
2861 if (dtp->u.p.input_complete)
2862 return true;
2863 if (dtp->u.p.at_eol)
2864 finish_separator (dtp);
2865 if (dtp->u.p.input_complete)
2866 return true;
2868 dtp->u.p.saved_type = BT_UNKNOWN;
2869 free_saved (dtp);
2871 switch (nl->type)
2873 case BT_INTEGER:
2874 read_integer (dtp, len);
2875 break;
2877 case BT_LOGICAL:
2878 read_logical (dtp, len);
2879 break;
2881 case BT_CHARACTER:
2882 read_character (dtp, len);
2883 break;
2885 case BT_REAL:
2886 /* Need to copy data back from the real location to the temp in
2887 order to handle nml reads into arrays. */
2888 read_real (dtp, pdata, len);
2889 memcpy (dtp->u.p.value, pdata, dlen);
2890 break;
2892 case BT_COMPLEX:
2893 /* Same as for REAL, copy back to temp. */
2894 read_complex (dtp, pdata, len, dlen);
2895 memcpy (dtp->u.p.value, pdata, dlen);
2896 break;
2898 case BT_DERIVED:
2899 obj_name_len = strlen (nl->var_name) + 1;
2900 obj_name = xmalloc (obj_name_len+1);
2901 memcpy (obj_name, nl->var_name, obj_name_len-1);
2902 memcpy (obj_name + obj_name_len - 1, "%", 2);
2904 /* If reading a derived type, disable the expanded read warning
2905 since a single object can have multiple reads. */
2906 dtp->u.p.expanded_read = 0;
2908 /* Now loop over the components. */
2910 for (cmp = nl->next;
2911 cmp &&
2912 !strncmp (cmp->var_name, obj_name, obj_name_len);
2913 cmp = cmp->next)
2915 /* Jump over nested derived type by testing if the potential
2916 component name contains '%'. */
2917 if (strchr (cmp->var_name + obj_name_len, '%'))
2918 continue;
2920 if (!nml_read_obj (dtp, cmp, (index_type)(pdata - nl->mem_pos),
2921 pprev_nl, nml_err_msg, nml_err_msg_size,
2922 clow, chigh))
2924 free (obj_name);
2925 return false;
2928 if (dtp->u.p.input_complete)
2930 free (obj_name);
2931 return true;
2935 free (obj_name);
2936 goto incr_idx;
2938 default:
2939 snprintf (nml_err_msg, nml_err_msg_size,
2940 "Bad type for namelist object %s", nl->var_name);
2941 internal_error (&dtp->common, nml_err_msg);
2942 goto nml_err_ret;
2946 /* The standard permits array data to stop short of the number of
2947 elements specified in the loop specification. In this case, we
2948 should be here with dtp->u.p.nml_read_error != 0. Control returns to
2949 nml_get_obj_data and an attempt is made to read object name. */
2951 *pprev_nl = nl;
2952 if (dtp->u.p.nml_read_error)
2954 dtp->u.p.expanded_read = 0;
2955 return true;
2958 if (dtp->u.p.saved_type == BT_UNKNOWN)
2960 dtp->u.p.expanded_read = 0;
2961 goto incr_idx;
2964 switch (dtp->u.p.saved_type)
2967 case BT_COMPLEX:
2968 case BT_REAL:
2969 case BT_INTEGER:
2970 case BT_LOGICAL:
2971 memcpy (pdata, dtp->u.p.value, dlen);
2972 break;
2974 case BT_CHARACTER:
2975 if (dlen < dtp->u.p.saved_used)
2977 if (compile_options.bounds_check)
2979 snprintf (nml_err_msg, nml_err_msg_size,
2980 "Namelist object '%s' truncated on read.",
2981 nl->var_name);
2982 generate_warning (&dtp->common, nml_err_msg);
2984 m = dlen;
2986 else
2987 m = dtp->u.p.saved_used;
2989 if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
2991 gfc_char4_t *q4, *p4 = pdata;
2992 int i;
2994 q4 = (gfc_char4_t *) dtp->u.p.saved_string;
2995 p4 += clow -1;
2996 for (i = 0; i < m; i++)
2997 *p4++ = *q4++;
2998 if (m < dlen)
2999 for (i = 0; i < dlen - m; i++)
3000 *p4++ = (gfc_char4_t) ' ';
3002 else
3004 pdata = (void*)( pdata + clow - 1 );
3005 memcpy (pdata, dtp->u.p.saved_string, m);
3006 if (m < dlen)
3007 memset ((void*)( pdata + m ), ' ', dlen - m);
3009 break;
3011 default:
3012 break;
3015 /* Warn if a non-standard expanded read occurs. A single read of a
3016 single object is acceptable. If a second read occurs, issue a warning
3017 and set the flag to zero to prevent further warnings. */
3018 if (dtp->u.p.expanded_read == 2)
3020 notify_std (&dtp->common, GFC_STD_GNU, "Non-standard expanded namelist read.");
3021 dtp->u.p.expanded_read = 0;
3024 /* If the expanded read warning flag is set, increment it,
3025 indicating that a single read has occurred. */
3026 if (dtp->u.p.expanded_read >= 1)
3027 dtp->u.p.expanded_read++;
3029 /* Break out of loop if scalar. */
3030 if (!nl->var_rank)
3031 break;
3033 /* Now increment the index vector. */
3035 incr_idx:
3037 nml_carry = 1;
3038 for (dim = 0; dim < nl->var_rank; dim++)
3040 nl->ls[dim].idx += nml_carry * nl->ls[dim].step;
3041 nml_carry = 0;
3042 if (((nl->ls[dim].step > 0) && (nl->ls[dim].idx > nl->ls[dim].end))
3044 ((nl->ls[dim].step < 0) && (nl->ls[dim].idx < nl->ls[dim].end)))
3046 nl->ls[dim].idx = nl->ls[dim].start;
3047 nml_carry = 1;
3050 } while (!nml_carry);
3052 if (dtp->u.p.repeat_count > 1)
3054 snprintf (nml_err_msg, nml_err_msg_size,
3055 "Repeat count too large for namelist object %s", nl->var_name);
3056 goto nml_err_ret;
3058 return true;
3060 nml_err_ret:
3062 return false;
3065 /* Parses the object name, including array and substring qualifiers. It
3066 iterates over derived type components, touching those components and
3067 setting their loop specifications, if there is a qualifier. If the
3068 object is itself a derived type, its components and subcomponents are
3069 touched. nml_read_obj is called at the end and this reads the data in
3070 the manner specified by the object name. */
3072 static bool
3073 nml_get_obj_data (st_parameter_dt *dtp, namelist_info **pprev_nl,
3074 char *nml_err_msg, size_t nml_err_msg_size)
3076 int c;
3077 namelist_info * nl;
3078 namelist_info * first_nl = NULL;
3079 namelist_info * root_nl = NULL;
3080 int dim, parsed_rank;
3081 int component_flag, qualifier_flag;
3082 index_type clow, chigh;
3083 int non_zero_rank_count;
3085 /* Look for end of input or object name. If '?' or '=?' are encountered
3086 in stdin, print the node names or the namelist to stdout. */
3088 eat_separator (dtp);
3089 if (dtp->u.p.input_complete)
3090 return true;
3092 if (dtp->u.p.at_eol)
3093 finish_separator (dtp);
3094 if (dtp->u.p.input_complete)
3095 return true;
3097 if ((c = next_char (dtp)) == EOF)
3098 goto nml_err_ret;
3099 switch (c)
3101 case '=':
3102 if ((c = next_char (dtp)) == EOF)
3103 goto nml_err_ret;
3104 if (c != '?')
3106 snprintf (nml_err_msg, nml_err_msg_size,
3107 "namelist read: misplaced = sign");
3108 goto nml_err_ret;
3110 nml_query (dtp, '=');
3111 return true;
3113 case '?':
3114 nml_query (dtp, '?');
3115 return true;
3117 case '$':
3118 case '&':
3119 nml_match_name (dtp, "end", 3);
3120 if (dtp->u.p.nml_read_error)
3122 snprintf (nml_err_msg, nml_err_msg_size,
3123 "namelist not terminated with / or &end");
3124 goto nml_err_ret;
3126 /* Fall through. */
3127 case '/':
3128 dtp->u.p.input_complete = 1;
3129 return true;
3131 default :
3132 break;
3135 /* Untouch all nodes of the namelist and reset the flags that are set for
3136 derived type components. */
3138 nml_untouch_nodes (dtp);
3139 component_flag = 0;
3140 qualifier_flag = 0;
3141 non_zero_rank_count = 0;
3143 /* Get the object name - should '!' and '\n' be permitted separators? */
3145 get_name:
3147 free_saved (dtp);
3151 if (!is_separator (c))
3152 push_char_default (dtp, tolower(c));
3153 if ((c = next_char (dtp)) == EOF)
3154 goto nml_err_ret;
3156 while (!( c=='=' || c==' ' || c=='\t' || c =='(' || c =='%' ));
3158 unget_char (dtp, c);
3160 /* Check that the name is in the namelist and get pointer to object.
3161 Three error conditions exist: (i) An attempt is being made to
3162 identify a non-existent object, following a failed data read or
3163 (ii) The object name does not exist or (iii) Too many data items
3164 are present for an object. (iii) gives the same error message
3165 as (i) */
3167 push_char_default (dtp, '\0');
3169 if (component_flag)
3171 #define EXT_STACK_SZ 100
3172 char ext_stack[EXT_STACK_SZ];
3173 char *ext_name;
3174 size_t var_len = strlen (root_nl->var_name);
3175 size_t saved_len
3176 = dtp->u.p.saved_string ? strlen (dtp->u.p.saved_string) : 0;
3177 size_t ext_size = var_len + saved_len + 1;
3179 if (ext_size > EXT_STACK_SZ)
3180 ext_name = xmalloc (ext_size);
3181 else
3182 ext_name = ext_stack;
3184 memcpy (ext_name, root_nl->var_name, var_len);
3185 if (dtp->u.p.saved_string)
3186 memcpy (ext_name + var_len, dtp->u.p.saved_string, saved_len);
3187 ext_name[var_len + saved_len] = '\0';
3188 nl = find_nml_node (dtp, ext_name);
3190 if (ext_size > EXT_STACK_SZ)
3191 free (ext_name);
3193 else
3194 nl = find_nml_node (dtp, dtp->u.p.saved_string);
3196 if (nl == NULL)
3198 if (dtp->u.p.nml_read_error && *pprev_nl)
3199 snprintf (nml_err_msg, nml_err_msg_size,
3200 "Bad data for namelist object %s", (*pprev_nl)->var_name);
3202 else
3203 snprintf (nml_err_msg, nml_err_msg_size,
3204 "Cannot match namelist object name %s",
3205 dtp->u.p.saved_string);
3207 goto nml_err_ret;
3210 /* Get the length, data length, base pointer and rank of the variable.
3211 Set the default loop specification first. */
3213 for (dim=0; dim < nl->var_rank; dim++)
3215 nl->ls[dim].step = 1;
3216 nl->ls[dim].end = GFC_DESCRIPTOR_UBOUND(nl,dim);
3217 nl->ls[dim].start = GFC_DESCRIPTOR_LBOUND(nl,dim);
3218 nl->ls[dim].idx = nl->ls[dim].start;
3221 /* Check to see if there is a qualifier: if so, parse it.*/
3223 if (c == '(' && nl->var_rank)
3225 parsed_rank = 0;
3226 if (!nml_parse_qualifier (dtp, nl->dim, nl->ls, nl->var_rank,
3227 nl->type, nml_err_msg, nml_err_msg_size,
3228 &parsed_rank))
3230 char *nml_err_msg_end = strchr (nml_err_msg, '\0');
3231 snprintf (nml_err_msg_end,
3232 nml_err_msg_size - (nml_err_msg_end - nml_err_msg),
3233 " for namelist variable %s", nl->var_name);
3234 goto nml_err_ret;
3236 if (parsed_rank > 0)
3237 non_zero_rank_count++;
3239 qualifier_flag = 1;
3241 if ((c = next_char (dtp)) == EOF)
3242 goto nml_err_ret;
3243 unget_char (dtp, c);
3245 else if (nl->var_rank > 0)
3246 non_zero_rank_count++;
3248 /* Now parse a derived type component. The root namelist_info address
3249 is backed up, as is the previous component level. The component flag
3250 is set and the iteration is made by jumping back to get_name. */
3252 if (c == '%')
3254 if (nl->type != BT_DERIVED)
3256 snprintf (nml_err_msg, nml_err_msg_size,
3257 "Attempt to get derived component for %s", nl->var_name);
3258 goto nml_err_ret;
3261 /* Don't move first_nl further in the list if a qualifier was found. */
3262 if ((*pprev_nl == NULL && !qualifier_flag) || !component_flag)
3263 first_nl = nl;
3265 root_nl = nl;
3267 component_flag = 1;
3268 if ((c = next_char (dtp)) == EOF)
3269 goto nml_err_ret;
3270 goto get_name;
3273 /* Parse a character qualifier, if present. chigh = 0 is a default
3274 that signals that the string length = string_length. */
3276 clow = 1;
3277 chigh = 0;
3279 if (c == '(' && nl->type == BT_CHARACTER)
3281 descriptor_dimension chd[1] = { {1, clow, nl->string_length} };
3282 array_loop_spec ind[1] = { {1, clow, nl->string_length, 1} };
3284 if (!nml_parse_qualifier (dtp, chd, ind, -1, nl->type,
3285 nml_err_msg, nml_err_msg_size, &parsed_rank))
3287 char *nml_err_msg_end = strchr (nml_err_msg, '\0');
3288 snprintf (nml_err_msg_end,
3289 nml_err_msg_size - (nml_err_msg_end - nml_err_msg),
3290 " for namelist variable %s", nl->var_name);
3291 goto nml_err_ret;
3294 clow = ind[0].start;
3295 chigh = ind[0].end;
3297 if (ind[0].step != 1)
3299 snprintf (nml_err_msg, nml_err_msg_size,
3300 "Step not allowed in substring qualifier"
3301 " for namelist object %s", nl->var_name);
3302 goto nml_err_ret;
3305 if ((c = next_char (dtp)) == EOF)
3306 goto nml_err_ret;
3307 unget_char (dtp, c);
3310 /* Make sure no extraneous qualifiers are there. */
3312 if (c == '(')
3314 snprintf (nml_err_msg, nml_err_msg_size,
3315 "Qualifier for a scalar or non-character namelist object %s",
3316 nl->var_name);
3317 goto nml_err_ret;
3320 /* Make sure there is no more than one non-zero rank object. */
3321 if (non_zero_rank_count > 1)
3323 snprintf (nml_err_msg, nml_err_msg_size,
3324 "Multiple sub-objects with non-zero rank in namelist object %s",
3325 nl->var_name);
3326 non_zero_rank_count = 0;
3327 goto nml_err_ret;
3330 /* According to the standard, an equal sign MUST follow an object name. The
3331 following is possibly lax - it allows comments, blank lines and so on to
3332 intervene. eat_spaces (dtp); c = next_char (dtp); would be compliant*/
3334 free_saved (dtp);
3336 eat_separator (dtp);
3337 if (dtp->u.p.input_complete)
3338 return true;
3340 if (dtp->u.p.at_eol)
3341 finish_separator (dtp);
3342 if (dtp->u.p.input_complete)
3343 return true;
3345 if ((c = next_char (dtp)) == EOF)
3346 goto nml_err_ret;
3348 if (c != '=')
3350 snprintf (nml_err_msg, nml_err_msg_size,
3351 "Equal sign must follow namelist object name %s",
3352 nl->var_name);
3353 goto nml_err_ret;
3355 /* If a derived type, touch its components and restore the root
3356 namelist_info if we have parsed a qualified derived type
3357 component. */
3359 if (nl->type == BT_DERIVED)
3360 nml_touch_nodes (nl);
3362 if (first_nl)
3364 if (first_nl->var_rank == 0)
3366 if (component_flag && qualifier_flag)
3367 nl = first_nl;
3369 else
3370 nl = first_nl;
3373 dtp->u.p.nml_read_error = 0;
3374 if (!nml_read_obj (dtp, nl, 0, pprev_nl, nml_err_msg, nml_err_msg_size,
3375 clow, chigh))
3376 goto nml_err_ret;
3378 return true;
3380 nml_err_ret:
3382 /* The EOF error message is issued by hit_eof. Return true so that the
3383 caller does not use nml_err_msg and nml_err_msg_size to generate
3384 an unrelated error message. */
3385 if (c == EOF)
3387 dtp->u.p.input_complete = 1;
3388 unget_char (dtp, c);
3389 hit_eof (dtp);
3390 return true;
3392 return false;
3395 /* Entry point for namelist input. Goes through input until namelist name
3396 is matched. Then cycles through nml_get_obj_data until the input is
3397 completed or there is an error. */
3399 void
3400 namelist_read (st_parameter_dt *dtp)
3402 int c;
3403 char nml_err_msg[200];
3405 /* Initialize the error string buffer just in case we get an unexpected fail
3406 somewhere and end up at nml_err_ret. */
3407 strcpy (nml_err_msg, "Internal namelist read error");
3409 /* Pointer to the previously read object, in case attempt is made to read
3410 new object name. Should this fail, error message can give previous
3411 name. */
3412 namelist_info *prev_nl = NULL;
3414 dtp->u.p.namelist_mode = 1;
3415 dtp->u.p.input_complete = 0;
3416 dtp->u.p.expanded_read = 0;
3418 /* Set the next_char and push_char worker functions. */
3419 set_workers (dtp);
3421 /* Look for &namelist_name . Skip all characters, testing for $nmlname.
3422 Exit on success or EOF. If '?' or '=?' encountered in stdin, print
3423 node names or namelist on stdout. */
3425 find_nml_name:
3426 c = next_char (dtp);
3427 switch (c)
3429 case '$':
3430 case '&':
3431 break;
3433 case '!':
3434 eat_line (dtp);
3435 goto find_nml_name;
3437 case '=':
3438 c = next_char (dtp);
3439 if (c == '?')
3440 nml_query (dtp, '=');
3441 else
3442 unget_char (dtp, c);
3443 goto find_nml_name;
3445 case '?':
3446 nml_query (dtp, '?');
3447 goto find_nml_name;
3449 case EOF:
3450 return;
3452 default:
3453 goto find_nml_name;
3456 /* Match the name of the namelist. */
3458 nml_match_name (dtp, dtp->namelist_name, dtp->namelist_name_len);
3460 if (dtp->u.p.nml_read_error)
3461 goto find_nml_name;
3463 /* A trailing space is required, we give a little latitude here, 10.9.1. */
3464 c = next_char (dtp);
3465 if (!is_separator(c) && c != '!')
3467 unget_char (dtp, c);
3468 goto find_nml_name;
3471 unget_char (dtp, c);
3472 eat_separator (dtp);
3474 /* Ready to read namelist objects. If there is an error in input
3475 from stdin, output the error message and continue. */
3477 while (!dtp->u.p.input_complete)
3479 if (!nml_get_obj_data (dtp, &prev_nl, nml_err_msg, sizeof nml_err_msg))
3481 if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
3482 goto nml_err_ret;
3483 generate_error (&dtp->common, LIBERROR_READ_VALUE, nml_err_msg);
3486 /* Reset the previous namelist pointer if we know we are not going
3487 to be doing multiple reads within a single namelist object. */
3488 if (prev_nl && prev_nl->var_rank == 0)
3489 prev_nl = NULL;
3492 free_saved (dtp);
3493 free_line (dtp);
3494 return;
3497 nml_err_ret:
3499 /* All namelist error calls return from here */
3500 free_saved (dtp);
3501 free_line (dtp);
3502 generate_error (&dtp->common, LIBERROR_READ_VALUE, nml_err_msg);
3503 return;