* lib/scanasm.exp (hidden-scan-for): Add XCOFF support.
[official-gcc.git] / libgfortran / io / list_read.c
bloba35beb88a0045866238e9fb87cc59fae7e870353
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.current_unit->last_char != EOF - 1)
175 dtp->u.p.at_eol = 0;
176 c = dtp->u.p.current_unit->last_char;
177 dtp->u.p.current_unit->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 (is_char4_unit(dtp)) /* 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.current_unit->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.current_unit->last_char == EOF - 1))
390 gfc_offset offset = stell (dtp->u.p.current_unit->s);
391 gfc_offset i;
393 if (is_char4_unit(dtp)) /* 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))
1378 /* Extension: allow default exponent of 0 when omitted. */
1379 if (dtp->common.flags & IOPARM_DT_DEFAULT_EXP)
1381 push_char (dtp, '0');
1382 goto done;
1384 else
1385 goto bad_exponent;
1388 push_char (dtp, c);
1390 for (;;)
1392 if ((c = next_char (dtp)) == EOF)
1393 goto bad;
1394 switch (c)
1396 CASE_DIGITS:
1397 push_char (dtp, c);
1398 break;
1400 case '!':
1401 if (!dtp->u.p.namelist_mode)
1402 goto bad;
1404 CASE_SEPARATORS:
1405 case EOF:
1406 unget_char (dtp, c);
1407 goto done;
1409 default:
1410 goto done;
1414 done:
1415 unget_char (dtp, c);
1416 push_char (dtp, '\0');
1418 m = convert_real (dtp, buffer, dtp->u.p.saved_string, length);
1419 free_saved (dtp);
1421 return m;
1423 done_infnan:
1424 unget_char (dtp, c);
1425 push_char (dtp, '\0');
1427 m = convert_infnan (dtp, buffer, dtp->u.p.saved_string, length);
1428 free_saved (dtp);
1430 return m;
1432 inf_nan:
1433 /* Match INF and Infinity. */
1434 if ((c == 'i' || c == 'I')
1435 && ((c = next_char (dtp)) == 'n' || c == 'N')
1436 && ((c = next_char (dtp)) == 'f' || c == 'F'))
1438 c = next_char (dtp);
1439 if ((c != 'i' && c != 'I')
1440 || ((c == 'i' || c == 'I')
1441 && ((c = next_char (dtp)) == 'n' || c == 'N')
1442 && ((c = next_char (dtp)) == 'i' || c == 'I')
1443 && ((c = next_char (dtp)) == 't' || c == 'T')
1444 && ((c = next_char (dtp)) == 'y' || c == 'Y')
1445 && (c = next_char (dtp))))
1447 if (is_separator (c) || (c == EOF))
1448 unget_char (dtp, c);
1449 push_char (dtp, 'i');
1450 push_char (dtp, 'n');
1451 push_char (dtp, 'f');
1452 goto done_infnan;
1454 } /* Match NaN. */
1455 else if (((c = next_char (dtp)) == 'a' || c == 'A')
1456 && ((c = next_char (dtp)) == 'n' || c == 'N')
1457 && (c = next_char (dtp)))
1459 if (is_separator (c) || (c == EOF))
1460 unget_char (dtp, c);
1461 push_char (dtp, 'n');
1462 push_char (dtp, 'a');
1463 push_char (dtp, 'n');
1465 /* Match "NAN(alphanum)". */
1466 if (c == '(')
1468 for ( ; c != ')'; c = next_char (dtp))
1469 if (is_separator (c))
1470 goto bad;
1472 c = next_char (dtp);
1473 if (is_separator (c) || (c == EOF))
1474 unget_char (dtp, c);
1476 goto done_infnan;
1479 bad:
1481 if (nml_bad_return (dtp, c))
1482 return 0;
1484 bad_exponent:
1486 free_saved (dtp);
1487 if (c == EOF)
1489 free_line (dtp);
1490 hit_eof (dtp);
1491 return 1;
1493 else if (c != '\n')
1494 eat_line (dtp);
1496 snprintf (message, MSGLEN, "Bad complex floating point "
1497 "number for item %d", dtp->u.p.item_count);
1498 free_line (dtp);
1499 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1501 return 1;
1505 /* Reading a complex number is straightforward because we can tell
1506 what it is right away. */
1508 static void
1509 read_complex (st_parameter_dt *dtp, void * dest, int kind, size_t size)
1511 char message[MSGLEN];
1512 int c;
1514 if (parse_repeat (dtp))
1515 return;
1517 c = next_char (dtp);
1518 switch (c)
1520 case '(':
1521 break;
1523 case '!':
1524 if (!dtp->u.p.namelist_mode)
1525 goto bad_complex;
1527 CASE_SEPARATORS:
1528 case EOF:
1529 unget_char (dtp, c);
1530 eat_separator (dtp);
1531 return;
1533 default:
1534 goto bad_complex;
1537 eol_1:
1538 eat_spaces (dtp);
1539 c = next_char (dtp);
1540 if (c == '\n' || c== '\r')
1541 goto eol_1;
1542 else
1543 unget_char (dtp, c);
1545 if (parse_real (dtp, dest, kind))
1546 return;
1548 eol_2:
1549 eat_spaces (dtp);
1550 c = next_char (dtp);
1551 if (c == '\n' || c== '\r')
1552 goto eol_2;
1553 else
1554 unget_char (dtp, c);
1556 if (next_char (dtp)
1557 != (dtp->u.p.current_unit->decimal_status == DECIMAL_POINT ? ',' : ';'))
1558 goto bad_complex;
1560 eol_3:
1561 eat_spaces (dtp);
1562 c = next_char (dtp);
1563 if (c == '\n' || c== '\r')
1564 goto eol_3;
1565 else
1566 unget_char (dtp, c);
1568 if (parse_real (dtp, dest + size / 2, kind))
1569 return;
1571 eol_4:
1572 eat_spaces (dtp);
1573 c = next_char (dtp);
1574 if (c == '\n' || c== '\r')
1575 goto eol_4;
1576 else
1577 unget_char (dtp, c);
1579 if (next_char (dtp) != ')')
1580 goto bad_complex;
1582 c = next_char (dtp);
1583 if (!is_separator (c) && (c != EOF))
1584 goto bad_complex;
1586 unget_char (dtp, c);
1587 eat_separator (dtp);
1589 free_saved (dtp);
1590 dtp->u.p.saved_type = BT_COMPLEX;
1591 return;
1593 bad_complex:
1595 if (nml_bad_return (dtp, c))
1596 return;
1598 free_saved (dtp);
1599 if (c == EOF)
1601 free_line (dtp);
1602 hit_eof (dtp);
1603 return;
1605 else if (c != '\n')
1606 eat_line (dtp);
1608 snprintf (message, MSGLEN, "Bad complex value in item %d of list input",
1609 dtp->u.p.item_count);
1610 free_line (dtp);
1611 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1615 /* Parse a real number with a possible repeat count. */
1617 static void
1618 read_real (st_parameter_dt *dtp, void * dest, int length)
1620 char message[MSGLEN];
1621 int c;
1622 int seen_dp;
1623 int is_inf;
1625 seen_dp = 0;
1627 c = next_char (dtp);
1628 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1629 c = '.';
1630 switch (c)
1632 CASE_DIGITS:
1633 push_char (dtp, c);
1634 break;
1636 case '.':
1637 push_char (dtp, c);
1638 seen_dp = 1;
1639 break;
1641 case '+':
1642 case '-':
1643 goto got_sign;
1645 case '!':
1646 if (!dtp->u.p.namelist_mode)
1647 goto bad_real;
1649 CASE_SEPARATORS:
1650 unget_char (dtp, c); /* Single null. */
1651 eat_separator (dtp);
1652 return;
1654 case 'i':
1655 case 'I':
1656 case 'n':
1657 case 'N':
1658 goto inf_nan;
1660 default:
1661 goto bad_real;
1664 /* Get the digit string that might be a repeat count. */
1666 for (;;)
1668 c = next_char (dtp);
1669 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1670 c = '.';
1671 switch (c)
1673 CASE_DIGITS:
1674 push_char (dtp, c);
1675 break;
1677 case '.':
1678 if (seen_dp)
1679 goto bad_real;
1681 seen_dp = 1;
1682 push_char (dtp, c);
1683 goto real_loop;
1685 case 'E':
1686 case 'e':
1687 case 'D':
1688 case 'd':
1689 case 'Q':
1690 case 'q':
1691 goto exp1;
1693 case '+':
1694 case '-':
1695 push_char (dtp, 'e');
1696 push_char (dtp, c);
1697 c = next_char (dtp);
1698 goto exp2;
1700 case '*':
1701 push_char (dtp, '\0');
1702 goto got_repeat;
1704 case '!':
1705 if (!dtp->u.p.namelist_mode)
1706 goto bad_real;
1708 CASE_SEPARATORS:
1709 case EOF:
1710 if (c != '\n' && c != ',' && c != '\r' && c != ';')
1711 unget_char (dtp, c);
1712 goto done;
1714 default:
1715 goto bad_real;
1719 got_repeat:
1720 if (convert_integer (dtp, -1, 0))
1721 return;
1723 /* Now get the number itself. */
1725 if ((c = next_char (dtp)) == EOF)
1726 goto bad_real;
1727 if (is_separator (c))
1728 { /* Repeated null value. */
1729 unget_char (dtp, c);
1730 eat_separator (dtp);
1731 return;
1734 if (c != '-' && c != '+')
1735 push_char (dtp, '+');
1736 else
1738 got_sign:
1739 push_char (dtp, c);
1740 if ((c = next_char (dtp)) == EOF)
1741 goto bad_real;
1744 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1745 c = '.';
1747 if (!isdigit (c) && c != '.')
1749 if (c == 'i' || c == 'I' || c == 'n' || c == 'N')
1750 goto inf_nan;
1751 else
1752 goto bad_real;
1755 if (c == '.')
1757 if (seen_dp)
1758 goto bad_real;
1759 else
1760 seen_dp = 1;
1763 push_char (dtp, c);
1765 real_loop:
1766 for (;;)
1768 c = next_char (dtp);
1769 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1770 c = '.';
1771 switch (c)
1773 CASE_DIGITS:
1774 push_char (dtp, c);
1775 break;
1777 case '!':
1778 if (!dtp->u.p.namelist_mode)
1779 goto bad_real;
1781 CASE_SEPARATORS:
1782 case EOF:
1783 goto done;
1785 case '.':
1786 if (seen_dp)
1787 goto bad_real;
1789 seen_dp = 1;
1790 push_char (dtp, c);
1791 break;
1793 case 'E':
1794 case 'e':
1795 case 'D':
1796 case 'd':
1797 case 'Q':
1798 case 'q':
1799 goto exp1;
1801 case '+':
1802 case '-':
1803 push_char (dtp, 'e');
1804 push_char (dtp, c);
1805 c = next_char (dtp);
1806 goto exp2;
1808 default:
1809 goto bad_real;
1813 exp1:
1814 push_char (dtp, 'e');
1816 if ((c = next_char (dtp)) == EOF)
1817 goto bad_real;
1818 if (c != '+' && c != '-')
1819 push_char (dtp, '+');
1820 else
1822 push_char (dtp, c);
1823 c = next_char (dtp);
1826 exp2:
1827 if (!isdigit (c))
1829 /* Extension: allow default exponent of 0 when omitted. */
1830 if (dtp->common.flags & IOPARM_DT_DEFAULT_EXP)
1832 push_char (dtp, '0');
1833 goto done;
1835 else
1836 goto bad_exponent;
1839 push_char (dtp, c);
1841 for (;;)
1843 c = next_char (dtp);
1845 switch (c)
1847 CASE_DIGITS:
1848 push_char (dtp, c);
1849 break;
1851 case '!':
1852 if (!dtp->u.p.namelist_mode)
1853 goto bad_real;
1855 CASE_SEPARATORS:
1856 case EOF:
1857 goto done;
1859 default:
1860 goto bad_real;
1864 done:
1865 unget_char (dtp, c);
1866 eat_separator (dtp);
1867 push_char (dtp, '\0');
1868 if (convert_real (dtp, dest, dtp->u.p.saved_string, length))
1870 free_saved (dtp);
1871 return;
1874 free_saved (dtp);
1875 dtp->u.p.saved_type = BT_REAL;
1876 return;
1878 inf_nan:
1879 l_push_char (dtp, c);
1880 is_inf = 0;
1882 /* Match INF and Infinity. */
1883 if (c == 'i' || c == 'I')
1885 c = next_char (dtp);
1886 l_push_char (dtp, c);
1887 if (c != 'n' && c != 'N')
1888 goto unwind;
1889 c = next_char (dtp);
1890 l_push_char (dtp, c);
1891 if (c != 'f' && c != 'F')
1892 goto unwind;
1893 c = next_char (dtp);
1894 l_push_char (dtp, c);
1895 if (!is_separator (c) && (c != EOF))
1897 if (c != 'i' && c != 'I')
1898 goto unwind;
1899 c = next_char (dtp);
1900 l_push_char (dtp, c);
1901 if (c != 'n' && c != 'N')
1902 goto unwind;
1903 c = next_char (dtp);
1904 l_push_char (dtp, c);
1905 if (c != 'i' && c != 'I')
1906 goto unwind;
1907 c = next_char (dtp);
1908 l_push_char (dtp, c);
1909 if (c != 't' && c != 'T')
1910 goto unwind;
1911 c = next_char (dtp);
1912 l_push_char (dtp, c);
1913 if (c != 'y' && c != 'Y')
1914 goto unwind;
1915 c = next_char (dtp);
1916 l_push_char (dtp, c);
1918 is_inf = 1;
1919 } /* Match NaN. */
1920 else
1922 c = next_char (dtp);
1923 l_push_char (dtp, c);
1924 if (c != 'a' && c != 'A')
1925 goto unwind;
1926 c = next_char (dtp);
1927 l_push_char (dtp, c);
1928 if (c != 'n' && c != 'N')
1929 goto unwind;
1930 c = next_char (dtp);
1931 l_push_char (dtp, c);
1933 /* Match NAN(alphanum). */
1934 if (c == '(')
1936 for (c = next_char (dtp); c != ')'; c = next_char (dtp))
1937 if (is_separator (c))
1938 goto unwind;
1939 else
1940 l_push_char (dtp, c);
1942 l_push_char (dtp, ')');
1943 c = next_char (dtp);
1944 l_push_char (dtp, c);
1948 if (!is_separator (c) && (c != EOF))
1949 goto unwind;
1951 if (dtp->u.p.namelist_mode)
1953 if (c == ' ' || c =='\n' || c == '\r')
1957 if ((c = next_char (dtp)) == EOF)
1958 goto bad_real;
1960 while (c == ' ' || c =='\n' || c == '\r');
1962 l_push_char (dtp, c);
1964 if (c == '=')
1965 goto unwind;
1969 if (is_inf)
1971 push_char (dtp, 'i');
1972 push_char (dtp, 'n');
1973 push_char (dtp, 'f');
1975 else
1977 push_char (dtp, 'n');
1978 push_char (dtp, 'a');
1979 push_char (dtp, 'n');
1982 free_line (dtp);
1983 unget_char (dtp, c);
1984 eat_separator (dtp);
1985 push_char (dtp, '\0');
1986 if (convert_infnan (dtp, dest, dtp->u.p.saved_string, length))
1987 return;
1989 free_saved (dtp);
1990 dtp->u.p.saved_type = BT_REAL;
1991 return;
1993 unwind:
1994 if (dtp->u.p.namelist_mode)
1996 dtp->u.p.nml_read_error = 1;
1997 dtp->u.p.line_buffer_enabled = 1;
1998 dtp->u.p.line_buffer_pos = 0;
1999 return;
2002 bad_real:
2004 if (nml_bad_return (dtp, c))
2005 return;
2007 bad_exponent:
2009 free_saved (dtp);
2010 if (c == EOF)
2012 free_line (dtp);
2013 hit_eof (dtp);
2014 return;
2016 else if (c != '\n')
2017 eat_line (dtp);
2019 snprintf (message, MSGLEN, "Bad real number in item %d of list input",
2020 dtp->u.p.item_count);
2021 free_line (dtp);
2022 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
2026 /* Check the current type against the saved type to make sure they are
2027 compatible. Returns nonzero if incompatible. */
2029 static int
2030 check_type (st_parameter_dt *dtp, bt type, int kind)
2032 char message[MSGLEN];
2034 if (dtp->u.p.saved_type != BT_UNKNOWN && dtp->u.p.saved_type != type)
2036 snprintf (message, MSGLEN, "Read type %s where %s was expected for item %d",
2037 type_name (dtp->u.p.saved_type), type_name (type),
2038 dtp->u.p.item_count);
2039 free_line (dtp);
2040 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
2041 return 1;
2044 if (dtp->u.p.saved_type == BT_UNKNOWN || dtp->u.p.saved_type == BT_CHARACTER)
2045 return 0;
2047 if ((type != BT_COMPLEX && dtp->u.p.saved_length != kind)
2048 || (type == BT_COMPLEX && dtp->u.p.saved_length != kind*2))
2050 snprintf (message, MSGLEN,
2051 "Read kind %d %s where kind %d is required for item %d",
2052 type == BT_COMPLEX ? dtp->u.p.saved_length / 2
2053 : dtp->u.p.saved_length,
2054 type_name (dtp->u.p.saved_type), kind,
2055 dtp->u.p.item_count);
2056 free_line (dtp);
2057 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
2058 return 1;
2061 return 0;
2065 /* Initialize the function pointers to select the correct versions of
2066 next_char and push_char depending on what we are doing. */
2068 static void
2069 set_workers (st_parameter_dt *dtp)
2071 if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
2073 dtp->u.p.current_unit->next_char_fn_ptr = &next_char_utf8;
2074 dtp->u.p.current_unit->push_char_fn_ptr = &push_char4;
2076 else if (is_internal_unit (dtp))
2078 dtp->u.p.current_unit->next_char_fn_ptr = &next_char_internal;
2079 dtp->u.p.current_unit->push_char_fn_ptr = &push_char_default;
2081 else
2083 dtp->u.p.current_unit->next_char_fn_ptr = &next_char_default;
2084 dtp->u.p.current_unit->push_char_fn_ptr = &push_char_default;
2089 /* Top level data transfer subroutine for list reads. Because we have
2090 to deal with repeat counts, the data item is always saved after
2091 reading, usually in the dtp->u.p.value[] array. If a repeat count is
2092 greater than one, we copy the data item multiple times. */
2094 static int
2095 list_formatted_read_scalar (st_parameter_dt *dtp, bt type, void *p,
2096 int kind, size_t size)
2098 gfc_char4_t *q, *r;
2099 int c, i, m;
2100 int err = 0;
2102 dtp->u.p.namelist_mode = 0;
2104 /* Set the next_char and push_char worker functions. */
2105 set_workers (dtp);
2107 if (dtp->u.p.first_item)
2109 dtp->u.p.first_item = 0;
2110 dtp->u.p.input_complete = 0;
2111 dtp->u.p.repeat_count = 1;
2112 dtp->u.p.at_eol = 0;
2114 if ((c = eat_spaces (dtp)) == EOF)
2116 err = LIBERROR_END;
2117 goto cleanup;
2119 if (is_separator (c))
2121 /* Found a null value. */
2122 dtp->u.p.repeat_count = 0;
2123 eat_separator (dtp);
2125 /* Set end-of-line flag. */
2126 if (c == '\n' || c == '\r')
2128 dtp->u.p.at_eol = 1;
2129 if (finish_separator (dtp) == LIBERROR_END)
2131 err = LIBERROR_END;
2132 goto cleanup;
2135 else
2136 goto cleanup;
2139 else
2141 if (dtp->u.p.repeat_count > 0)
2143 if (check_type (dtp, type, kind))
2144 return err;
2145 goto set_value;
2148 if (dtp->u.p.input_complete)
2149 goto cleanup;
2151 if (dtp->u.p.at_eol)
2152 finish_separator (dtp);
2153 else
2155 eat_spaces (dtp);
2156 /* Trailing spaces prior to end of line. */
2157 if (dtp->u.p.at_eol)
2158 finish_separator (dtp);
2161 dtp->u.p.saved_type = BT_UNKNOWN;
2162 dtp->u.p.repeat_count = 1;
2165 switch (type)
2167 case BT_INTEGER:
2168 read_integer (dtp, kind);
2169 break;
2170 case BT_LOGICAL:
2171 read_logical (dtp, kind);
2172 break;
2173 case BT_CHARACTER:
2174 read_character (dtp, kind);
2175 break;
2176 case BT_REAL:
2177 read_real (dtp, p, kind);
2178 /* Copy value back to temporary if needed. */
2179 if (dtp->u.p.repeat_count > 0)
2180 memcpy (dtp->u.p.value, p, size);
2181 break;
2182 case BT_COMPLEX:
2183 read_complex (dtp, p, kind, size);
2184 /* Copy value back to temporary if needed. */
2185 if (dtp->u.p.repeat_count > 0)
2186 memcpy (dtp->u.p.value, p, size);
2187 break;
2188 case BT_CLASS:
2190 int unit = dtp->u.p.current_unit->unit_number;
2191 char iotype[] = "LISTDIRECTED";
2192 gfc_charlen_type iotype_len = 12;
2193 char tmp_iomsg[IOMSG_LEN] = "";
2194 char *child_iomsg;
2195 gfc_charlen_type child_iomsg_len;
2196 int noiostat;
2197 int *child_iostat = NULL;
2198 gfc_array_i4 vlist;
2200 GFC_DESCRIPTOR_DATA(&vlist) = NULL;
2201 GFC_DIMENSION_SET(vlist.dim[0],1, 0, 0);
2203 /* Set iostat, intent(out). */
2204 noiostat = 0;
2205 child_iostat = (dtp->common.flags & IOPARM_HAS_IOSTAT) ?
2206 dtp->common.iostat : &noiostat;
2208 /* Set iomsge, intent(inout). */
2209 if (dtp->common.flags & IOPARM_HAS_IOMSG)
2211 child_iomsg = dtp->common.iomsg;
2212 child_iomsg_len = dtp->common.iomsg_len;
2214 else
2216 child_iomsg = tmp_iomsg;
2217 child_iomsg_len = IOMSG_LEN;
2220 /* Call the user defined formatted READ procedure. */
2221 dtp->u.p.current_unit->child_dtio++;
2222 dtp->u.p.fdtio_ptr (p, &unit, iotype, &vlist,
2223 child_iostat, child_iomsg,
2224 iotype_len, child_iomsg_len);
2225 dtp->u.p.current_unit->child_dtio--;
2227 break;
2228 default:
2229 internal_error (&dtp->common, "Bad type for list read");
2232 if (dtp->u.p.saved_type != BT_CHARACTER && dtp->u.p.saved_type != BT_UNKNOWN)
2233 dtp->u.p.saved_length = size;
2235 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
2236 goto cleanup;
2238 set_value:
2239 switch (dtp->u.p.saved_type)
2241 case BT_COMPLEX:
2242 case BT_REAL:
2243 if (dtp->u.p.repeat_count > 0)
2244 memcpy (p, dtp->u.p.value, size);
2245 break;
2247 case BT_INTEGER:
2248 case BT_LOGICAL:
2249 memcpy (p, dtp->u.p.value, size);
2250 break;
2252 case BT_CHARACTER:
2253 if (dtp->u.p.saved_string)
2255 m = ((int) size < dtp->u.p.saved_used)
2256 ? (int) size : dtp->u.p.saved_used;
2258 q = (gfc_char4_t *) p;
2259 r = (gfc_char4_t *) dtp->u.p.saved_string;
2260 if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
2261 for (i = 0; i < m; i++)
2262 *q++ = *r++;
2263 else
2265 if (kind == 1)
2266 memcpy (p, dtp->u.p.saved_string, m);
2267 else
2268 for (i = 0; i < m; i++)
2269 *q++ = *r++;
2272 else
2273 /* Just delimiters encountered, nothing to copy but SPACE. */
2274 m = 0;
2276 if (m < (int) size)
2278 if (kind == 1)
2279 memset (((char *) p) + m, ' ', size - m);
2280 else
2282 q = (gfc_char4_t *) p;
2283 for (i = m; i < (int) size; i++)
2284 q[i] = (unsigned char) ' ';
2287 break;
2289 case BT_UNKNOWN:
2290 break;
2292 default:
2293 internal_error (&dtp->common, "Bad type for list read");
2296 if (--dtp->u.p.repeat_count <= 0)
2297 free_saved (dtp);
2299 cleanup:
2300 if (err == LIBERROR_END)
2302 free_line (dtp);
2303 hit_eof (dtp);
2305 fbuf_flush_list (dtp->u.p.current_unit, LIST_READING);
2306 return err;
2310 void
2311 list_formatted_read (st_parameter_dt *dtp, bt type, void *p, int kind,
2312 size_t size, size_t nelems)
2314 size_t elem;
2315 char *tmp;
2316 size_t stride = type == BT_CHARACTER ?
2317 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
2318 int err;
2320 tmp = (char *) p;
2322 /* Big loop over all the elements. */
2323 for (elem = 0; elem < nelems; elem++)
2325 dtp->u.p.item_count++;
2326 err = list_formatted_read_scalar (dtp, type, tmp + stride*elem,
2327 kind, size);
2328 if (err)
2329 break;
2334 /* Finish a list read. */
2336 void
2337 finish_list_read (st_parameter_dt *dtp)
2339 free_saved (dtp);
2341 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
2343 if (dtp->u.p.at_eol)
2345 dtp->u.p.at_eol = 0;
2346 return;
2349 if (!is_internal_unit (dtp))
2351 int c;
2353 /* Set the next_char and push_char worker functions. */
2354 set_workers (dtp);
2356 c = next_char (dtp);
2357 if (c == EOF)
2359 free_line (dtp);
2360 hit_eof (dtp);
2361 return;
2363 if (c != '\n')
2364 eat_line (dtp);
2367 free_line (dtp);
2371 /* NAMELIST INPUT
2373 void namelist_read (st_parameter_dt *dtp)
2374 calls:
2375 static void nml_match_name (char *name, int len)
2376 static int nml_query (st_parameter_dt *dtp)
2377 static int nml_get_obj_data (st_parameter_dt *dtp,
2378 namelist_info **prev_nl, char *, size_t)
2379 calls:
2380 static void nml_untouch_nodes (st_parameter_dt *dtp)
2381 static namelist_info * find_nml_node (st_parameter_dt *dtp,
2382 char * var_name)
2383 static int nml_parse_qualifier(descriptor_dimension * ad,
2384 array_loop_spec * ls, int rank, char *)
2385 static void nml_touch_nodes (namelist_info * nl)
2386 static int nml_read_obj (namelist_info *nl, index_type offset,
2387 namelist_info **prev_nl, char *, size_t,
2388 index_type clow, index_type chigh)
2389 calls:
2390 -itself- */
2392 /* Inputs a rank-dimensional qualifier, which can contain
2393 singlets, doublets, triplets or ':' with the standard meanings. */
2395 static bool
2396 nml_parse_qualifier (st_parameter_dt *dtp, descriptor_dimension *ad,
2397 array_loop_spec *ls, int rank, bt nml_elem_type,
2398 char *parse_err_msg, size_t parse_err_msg_size,
2399 int *parsed_rank)
2401 int dim;
2402 int indx;
2403 int neg;
2404 int null_flag;
2405 int is_array_section, is_char;
2406 int c;
2408 is_char = 0;
2409 is_array_section = 0;
2410 dtp->u.p.expanded_read = 0;
2412 /* See if this is a character substring qualifier we are looking for. */
2413 if (rank == -1)
2415 rank = 1;
2416 is_char = 1;
2419 /* The next character in the stream should be the '('. */
2421 if ((c = next_char (dtp)) == EOF)
2422 goto err_ret;
2424 /* Process the qualifier, by dimension and triplet. */
2426 for (dim=0; dim < rank; dim++ )
2428 for (indx=0; indx<3; indx++)
2430 free_saved (dtp);
2431 eat_spaces (dtp);
2432 neg = 0;
2434 /* Process a potential sign. */
2435 if ((c = next_char (dtp)) == EOF)
2436 goto err_ret;
2437 switch (c)
2439 case '-':
2440 neg = 1;
2441 break;
2443 case '+':
2444 break;
2446 default:
2447 unget_char (dtp, c);
2448 break;
2451 /* Process characters up to the next ':' , ',' or ')'. */
2452 for (;;)
2454 c = next_char (dtp);
2455 switch (c)
2457 case EOF:
2458 goto err_ret;
2460 case ':':
2461 is_array_section = 1;
2462 break;
2464 case ',': case ')':
2465 if ((c==',' && dim == rank -1)
2466 || (c==')' && dim < rank -1))
2468 if (is_char)
2469 snprintf (parse_err_msg, parse_err_msg_size,
2470 "Bad substring qualifier");
2471 else
2472 snprintf (parse_err_msg, parse_err_msg_size,
2473 "Bad number of index fields");
2474 goto err_ret;
2476 break;
2478 CASE_DIGITS:
2479 push_char (dtp, c);
2480 continue;
2482 case ' ': case '\t': case '\r': case '\n':
2483 eat_spaces (dtp);
2484 break;
2486 default:
2487 if (is_char)
2488 snprintf (parse_err_msg, parse_err_msg_size,
2489 "Bad character in substring qualifier");
2490 else
2491 snprintf (parse_err_msg, parse_err_msg_size,
2492 "Bad character in index");
2493 goto err_ret;
2496 if ((c == ',' || c == ')') && indx == 0
2497 && dtp->u.p.saved_string == 0)
2499 if (is_char)
2500 snprintf (parse_err_msg, parse_err_msg_size,
2501 "Null substring qualifier");
2502 else
2503 snprintf (parse_err_msg, parse_err_msg_size,
2504 "Null index field");
2505 goto err_ret;
2508 if ((c == ':' && indx == 1 && dtp->u.p.saved_string == 0)
2509 || (indx == 2 && dtp->u.p.saved_string == 0))
2511 if (is_char)
2512 snprintf (parse_err_msg, parse_err_msg_size,
2513 "Bad substring qualifier");
2514 else
2515 snprintf (parse_err_msg, parse_err_msg_size,
2516 "Bad index triplet");
2517 goto err_ret;
2520 if (is_char && !is_array_section)
2522 snprintf (parse_err_msg, parse_err_msg_size,
2523 "Missing colon in substring qualifier");
2524 goto err_ret;
2527 /* If '( : ? )' or '( ? : )' break and flag read failure. */
2528 null_flag = 0;
2529 if ((c == ':' && indx == 0 && dtp->u.p.saved_string == 0)
2530 || (indx==1 && dtp->u.p.saved_string == 0))
2532 null_flag = 1;
2533 break;
2536 /* Now read the index. */
2537 if (convert_integer (dtp, sizeof(index_type), neg))
2539 if (is_char)
2540 snprintf (parse_err_msg, parse_err_msg_size,
2541 "Bad integer substring qualifier");
2542 else
2543 snprintf (parse_err_msg, parse_err_msg_size,
2544 "Bad integer in index");
2545 goto err_ret;
2547 break;
2550 /* Feed the index values to the triplet arrays. */
2551 if (!null_flag)
2553 if (indx == 0)
2554 memcpy (&ls[dim].start, dtp->u.p.value, sizeof(index_type));
2555 if (indx == 1)
2556 memcpy (&ls[dim].end, dtp->u.p.value, sizeof(index_type));
2557 if (indx == 2)
2558 memcpy (&ls[dim].step, dtp->u.p.value, sizeof(index_type));
2561 /* Singlet or doublet indices. */
2562 if (c==',' || c==')')
2564 if (indx == 0)
2566 memcpy (&ls[dim].start, dtp->u.p.value, sizeof(index_type));
2568 /* If -std=f95/2003 or an array section is specified,
2569 do not allow excess data to be processed. */
2570 if (is_array_section == 1
2571 || !(compile_options.allow_std & GFC_STD_GNU)
2572 || nml_elem_type == BT_DERIVED)
2573 ls[dim].end = ls[dim].start;
2574 else
2575 dtp->u.p.expanded_read = 1;
2578 /* Check for non-zero rank. */
2579 if (is_array_section == 1 && ls[dim].start != ls[dim].end)
2580 *parsed_rank = 1;
2582 break;
2586 if (is_array_section == 1 && dtp->u.p.expanded_read == 1)
2588 int i;
2589 dtp->u.p.expanded_read = 0;
2590 for (i = 0; i < dim; i++)
2591 ls[i].end = ls[i].start;
2594 /* Check the values of the triplet indices. */
2595 if ((ls[dim].start > GFC_DIMENSION_UBOUND(ad[dim]))
2596 || (ls[dim].start < GFC_DIMENSION_LBOUND(ad[dim]))
2597 || (ls[dim].end > GFC_DIMENSION_UBOUND(ad[dim]))
2598 || (ls[dim].end < GFC_DIMENSION_LBOUND(ad[dim])))
2600 if (is_char)
2601 snprintf (parse_err_msg, parse_err_msg_size,
2602 "Substring out of range");
2603 else
2604 snprintf (parse_err_msg, parse_err_msg_size,
2605 "Index %d out of range", dim + 1);
2606 goto err_ret;
2609 if (((ls[dim].end - ls[dim].start ) * ls[dim].step < 0)
2610 || (ls[dim].step == 0))
2612 snprintf (parse_err_msg, parse_err_msg_size,
2613 "Bad range in index %d", dim + 1);
2614 goto err_ret;
2617 /* Initialise the loop index counter. */
2618 ls[dim].idx = ls[dim].start;
2620 eat_spaces (dtp);
2621 return true;
2623 err_ret:
2625 /* The EOF error message is issued by hit_eof. Return true so that the
2626 caller does not use parse_err_msg and parse_err_msg_size to generate
2627 an unrelated error message. */
2628 if (c == EOF)
2630 hit_eof (dtp);
2631 dtp->u.p.input_complete = 1;
2632 return true;
2634 return false;
2638 static bool
2639 extended_look_ahead (char *p, char *q)
2641 char *r, *s;
2643 /* Scan ahead to find a '%' in the p string. */
2644 for(r = p, s = q; *r && *s; s++)
2645 if ((*s == '%' || *s == '+') && strcmp (r + 1, s + 1) == 0)
2646 return true;
2647 return false;
2651 static bool
2652 strcmp_extended_type (char *p, char *q)
2654 char *r, *s;
2656 for (r = p, s = q; *r && *s; r++, s++)
2658 if (*r != *s)
2660 if (*r == '%' && *s == '+' && extended_look_ahead (r, s))
2661 return true;
2662 break;
2665 return false;
2669 static namelist_info *
2670 find_nml_node (st_parameter_dt *dtp, char * var_name)
2672 namelist_info * t = dtp->u.p.ionml;
2673 while (t != NULL)
2675 if (strcmp (var_name, t->var_name) == 0)
2677 t->touched = 1;
2678 return t;
2680 if (strcmp_extended_type (var_name, t->var_name))
2682 t->touched = 1;
2683 return t;
2685 t = t->next;
2687 return NULL;
2690 /* Visits all the components of a derived type that have
2691 not explicitly been identified in the namelist input.
2692 touched is set and the loop specification initialised
2693 to default values */
2695 static void
2696 nml_touch_nodes (namelist_info * nl)
2698 index_type len = strlen (nl->var_name) + 1;
2699 int dim;
2700 char * ext_name = xmalloc (len + 1);
2701 memcpy (ext_name, nl->var_name, len-1);
2702 memcpy (ext_name + len - 1, "%", 2);
2703 for (nl = nl->next; nl; nl = nl->next)
2705 if (strncmp (nl->var_name, ext_name, len) == 0)
2707 nl->touched = 1;
2708 for (dim=0; dim < nl->var_rank; dim++)
2710 nl->ls[dim].step = 1;
2711 nl->ls[dim].end = GFC_DESCRIPTOR_UBOUND(nl,dim);
2712 nl->ls[dim].start = GFC_DESCRIPTOR_LBOUND(nl,dim);
2713 nl->ls[dim].idx = nl->ls[dim].start;
2716 else
2717 break;
2719 free (ext_name);
2720 return;
2723 /* Resets touched for the entire list of nml_nodes, ready for a
2724 new object. */
2726 static void
2727 nml_untouch_nodes (st_parameter_dt *dtp)
2729 namelist_info * t;
2730 for (t = dtp->u.p.ionml; t; t = t->next)
2731 t->touched = 0;
2732 return;
2735 /* Attempts to input name to namelist name. Returns
2736 dtp->u.p.nml_read_error = 1 on no match. */
2738 static void
2739 nml_match_name (st_parameter_dt *dtp, const char *name, index_type len)
2741 index_type i;
2742 int c;
2744 dtp->u.p.nml_read_error = 0;
2745 for (i = 0; i < len; i++)
2747 c = next_char (dtp);
2748 if (c == EOF || (tolower (c) != tolower (name[i])))
2750 dtp->u.p.nml_read_error = 1;
2751 break;
2756 /* If the namelist read is from stdin, output the current state of the
2757 namelist to stdout. This is used to implement the non-standard query
2758 features, ? and =?. If c == '=' the full namelist is printed. Otherwise
2759 the names alone are printed. */
2761 static void
2762 nml_query (st_parameter_dt *dtp, char c)
2764 gfc_unit * temp_unit;
2765 namelist_info * nl;
2766 index_type len;
2767 char * p;
2768 #ifdef HAVE_CRLF
2769 static const index_type endlen = 2;
2770 static const char endl[] = "\r\n";
2771 static const char nmlend[] = "&end\r\n";
2772 #else
2773 static const index_type endlen = 1;
2774 static const char endl[] = "\n";
2775 static const char nmlend[] = "&end\n";
2776 #endif
2778 if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
2779 return;
2781 /* Store the current unit and transfer to stdout. */
2783 temp_unit = dtp->u.p.current_unit;
2784 dtp->u.p.current_unit = find_unit (options.stdout_unit);
2786 if (dtp->u.p.current_unit)
2788 dtp->u.p.mode = WRITING;
2789 next_record (dtp, 0);
2791 /* Write the namelist in its entirety. */
2793 if (c == '=')
2794 namelist_write (dtp);
2796 /* Or write the list of names. */
2798 else
2800 /* "&namelist_name\n" */
2802 len = dtp->namelist_name_len;
2803 p = write_block (dtp, len - 1 + endlen);
2804 if (!p)
2805 goto query_return;
2806 memcpy (p, "&", 1);
2807 memcpy ((char*)(p + 1), dtp->namelist_name, len);
2808 memcpy ((char*)(p + len + 1), &endl, endlen);
2809 for (nl = dtp->u.p.ionml; nl; nl = nl->next)
2811 /* " var_name\n" */
2813 len = strlen (nl->var_name);
2814 p = write_block (dtp, len + endlen);
2815 if (!p)
2816 goto query_return;
2817 memcpy (p, " ", 1);
2818 memcpy ((char*)(p + 1), nl->var_name, len);
2819 memcpy ((char*)(p + len + 1), &endl, endlen);
2822 /* "&end\n" */
2824 p = write_block (dtp, endlen + 4);
2825 if (!p)
2826 goto query_return;
2827 memcpy (p, &nmlend, endlen + 4);
2830 /* Flush the stream to force immediate output. */
2832 fbuf_flush (dtp->u.p.current_unit, WRITING);
2833 sflush (dtp->u.p.current_unit->s);
2834 unlock_unit (dtp->u.p.current_unit);
2837 query_return:
2839 /* Restore the current unit. */
2841 dtp->u.p.current_unit = temp_unit;
2842 dtp->u.p.mode = READING;
2843 return;
2846 /* Reads and stores the input for the namelist object nl. For an array,
2847 the function loops over the ranges defined by the loop specification.
2848 This default to all the data or to the specification from a qualifier.
2849 nml_read_obj recursively calls itself to read derived types. It visits
2850 all its own components but only reads data for those that were touched
2851 when the name was parsed. If a read error is encountered, an attempt is
2852 made to return to read a new object name because the standard allows too
2853 little data to be available. On the other hand, too much data is an
2854 error. */
2856 static bool
2857 nml_read_obj (st_parameter_dt *dtp, namelist_info * nl, index_type offset,
2858 namelist_info **pprev_nl, char *nml_err_msg,
2859 size_t nml_err_msg_size, index_type clow, index_type chigh)
2861 namelist_info * cmp;
2862 char * obj_name;
2863 int nml_carry;
2864 int len;
2865 int dim;
2866 index_type dlen;
2867 index_type m;
2868 size_t obj_name_len;
2869 void * pdata;
2871 /* If we have encountered a previous read error or this object has not been
2872 touched in name parsing, just return. */
2873 if (dtp->u.p.nml_read_error || !nl->touched)
2874 return true;
2876 dtp->u.p.item_count++; /* Used in error messages. */
2877 dtp->u.p.repeat_count = 0;
2878 eat_spaces (dtp);
2880 len = nl->len;
2881 switch (nl->type)
2883 case BT_INTEGER:
2884 case BT_LOGICAL:
2885 dlen = len;
2886 break;
2888 case BT_REAL:
2889 dlen = size_from_real_kind (len);
2890 break;
2892 case BT_COMPLEX:
2893 dlen = size_from_complex_kind (len);
2894 break;
2896 case BT_CHARACTER:
2897 dlen = chigh ? (chigh - clow + 1) : nl->string_length;
2898 break;
2900 default:
2901 dlen = 0;
2906 /* Update the pointer to the data, using the current index vector */
2908 pdata = (void*)(nl->mem_pos + offset);
2909 for (dim = 0; dim < nl->var_rank; dim++)
2910 pdata = (void*)(pdata + (nl->ls[dim].idx
2911 - GFC_DESCRIPTOR_LBOUND(nl,dim))
2912 * GFC_DESCRIPTOR_STRIDE(nl,dim) * nl->size);
2914 /* If we are finished with the repeat count, try to read next value. */
2916 nml_carry = 0;
2917 if (--dtp->u.p.repeat_count <= 0)
2919 if (dtp->u.p.input_complete)
2920 return true;
2921 if (dtp->u.p.at_eol)
2922 finish_separator (dtp);
2923 if (dtp->u.p.input_complete)
2924 return true;
2926 dtp->u.p.saved_type = BT_UNKNOWN;
2927 free_saved (dtp);
2929 switch (nl->type)
2931 case BT_INTEGER:
2932 read_integer (dtp, len);
2933 break;
2935 case BT_LOGICAL:
2936 read_logical (dtp, len);
2937 break;
2939 case BT_CHARACTER:
2940 read_character (dtp, len);
2941 break;
2943 case BT_REAL:
2944 /* Need to copy data back from the real location to the temp in
2945 order to handle nml reads into arrays. */
2946 read_real (dtp, pdata, len);
2947 memcpy (dtp->u.p.value, pdata, dlen);
2948 break;
2950 case BT_COMPLEX:
2951 /* Same as for REAL, copy back to temp. */
2952 read_complex (dtp, pdata, len, dlen);
2953 memcpy (dtp->u.p.value, pdata, dlen);
2954 break;
2956 case BT_DERIVED:
2957 obj_name_len = strlen (nl->var_name) + 1;
2958 obj_name = xmalloc (obj_name_len+1);
2959 memcpy (obj_name, nl->var_name, obj_name_len-1);
2960 memcpy (obj_name + obj_name_len - 1, "%", 2);
2962 /* If reading a derived type, disable the expanded read warning
2963 since a single object can have multiple reads. */
2964 dtp->u.p.expanded_read = 0;
2966 /* Now loop over the components. */
2968 for (cmp = nl->next;
2969 cmp &&
2970 !strncmp (cmp->var_name, obj_name, obj_name_len);
2971 cmp = cmp->next)
2973 /* Jump over nested derived type by testing if the potential
2974 component name contains '%'. */
2975 if (strchr (cmp->var_name + obj_name_len, '%'))
2976 continue;
2978 if (!nml_read_obj (dtp, cmp, (index_type)(pdata - nl->mem_pos),
2979 pprev_nl, nml_err_msg, nml_err_msg_size,
2980 clow, chigh))
2982 free (obj_name);
2983 return false;
2986 if (dtp->u.p.input_complete)
2988 free (obj_name);
2989 return true;
2993 free (obj_name);
2994 goto incr_idx;
2996 default:
2997 snprintf (nml_err_msg, nml_err_msg_size,
2998 "Bad type for namelist object %s", nl->var_name);
2999 internal_error (&dtp->common, nml_err_msg);
3000 goto nml_err_ret;
3004 /* The standard permits array data to stop short of the number of
3005 elements specified in the loop specification. In this case, we
3006 should be here with dtp->u.p.nml_read_error != 0. Control returns to
3007 nml_get_obj_data and an attempt is made to read object name. */
3009 *pprev_nl = nl;
3010 if (dtp->u.p.nml_read_error)
3012 dtp->u.p.expanded_read = 0;
3013 return true;
3016 if (dtp->u.p.saved_type == BT_UNKNOWN)
3018 dtp->u.p.expanded_read = 0;
3019 goto incr_idx;
3022 switch (dtp->u.p.saved_type)
3025 case BT_COMPLEX:
3026 case BT_REAL:
3027 case BT_INTEGER:
3028 case BT_LOGICAL:
3029 memcpy (pdata, dtp->u.p.value, dlen);
3030 break;
3032 case BT_CHARACTER:
3033 if (dlen < dtp->u.p.saved_used)
3035 if (compile_options.bounds_check)
3037 snprintf (nml_err_msg, nml_err_msg_size,
3038 "Namelist object '%s' truncated on read.",
3039 nl->var_name);
3040 generate_warning (&dtp->common, nml_err_msg);
3042 m = dlen;
3044 else
3045 m = dtp->u.p.saved_used;
3047 if (dtp->u.p.current_unit->flags.encoding == ENCODING_UTF8)
3049 gfc_char4_t *q4, *p4 = pdata;
3050 int i;
3052 q4 = (gfc_char4_t *) dtp->u.p.saved_string;
3053 p4 += clow -1;
3054 for (i = 0; i < m; i++)
3055 *p4++ = *q4++;
3056 if (m < dlen)
3057 for (i = 0; i < dlen - m; i++)
3058 *p4++ = (gfc_char4_t) ' ';
3060 else
3062 pdata = (void*)( pdata + clow - 1 );
3063 memcpy (pdata, dtp->u.p.saved_string, m);
3064 if (m < dlen)
3065 memset ((void*)( pdata + m ), ' ', dlen - m);
3067 break;
3069 default:
3070 break;
3073 /* Warn if a non-standard expanded read occurs. A single read of a
3074 single object is acceptable. If a second read occurs, issue a warning
3075 and set the flag to zero to prevent further warnings. */
3076 if (dtp->u.p.expanded_read == 2)
3078 notify_std (&dtp->common, GFC_STD_GNU, "Non-standard expanded namelist read.");
3079 dtp->u.p.expanded_read = 0;
3082 /* If the expanded read warning flag is set, increment it,
3083 indicating that a single read has occurred. */
3084 if (dtp->u.p.expanded_read >= 1)
3085 dtp->u.p.expanded_read++;
3087 /* Break out of loop if scalar. */
3088 if (!nl->var_rank)
3089 break;
3091 /* Now increment the index vector. */
3093 incr_idx:
3095 nml_carry = 1;
3096 for (dim = 0; dim < nl->var_rank; dim++)
3098 nl->ls[dim].idx += nml_carry * nl->ls[dim].step;
3099 nml_carry = 0;
3100 if (((nl->ls[dim].step > 0) && (nl->ls[dim].idx > nl->ls[dim].end))
3102 ((nl->ls[dim].step < 0) && (nl->ls[dim].idx < nl->ls[dim].end)))
3104 nl->ls[dim].idx = nl->ls[dim].start;
3105 nml_carry = 1;
3108 } while (!nml_carry);
3110 if (dtp->u.p.repeat_count > 1)
3112 snprintf (nml_err_msg, nml_err_msg_size,
3113 "Repeat count too large for namelist object %s", nl->var_name);
3114 goto nml_err_ret;
3116 return true;
3118 nml_err_ret:
3120 return false;
3123 /* Parses the object name, including array and substring qualifiers. It
3124 iterates over derived type components, touching those components and
3125 setting their loop specifications, if there is a qualifier. If the
3126 object is itself a derived type, its components and subcomponents are
3127 touched. nml_read_obj is called at the end and this reads the data in
3128 the manner specified by the object name. */
3130 static bool
3131 nml_get_obj_data (st_parameter_dt *dtp, namelist_info **pprev_nl,
3132 char *nml_err_msg, size_t nml_err_msg_size)
3134 int c;
3135 namelist_info * nl;
3136 namelist_info * first_nl = NULL;
3137 namelist_info * root_nl = NULL;
3138 int dim, parsed_rank;
3139 int component_flag, qualifier_flag;
3140 index_type clow, chigh;
3141 int non_zero_rank_count;
3143 /* Look for end of input or object name. If '?' or '=?' are encountered
3144 in stdin, print the node names or the namelist to stdout. */
3146 eat_separator (dtp);
3147 if (dtp->u.p.input_complete)
3148 return true;
3150 if (dtp->u.p.at_eol)
3151 finish_separator (dtp);
3152 if (dtp->u.p.input_complete)
3153 return true;
3155 if ((c = next_char (dtp)) == EOF)
3156 goto nml_err_ret;
3157 switch (c)
3159 case '=':
3160 if ((c = next_char (dtp)) == EOF)
3161 goto nml_err_ret;
3162 if (c != '?')
3164 snprintf (nml_err_msg, nml_err_msg_size,
3165 "namelist read: misplaced = sign");
3166 goto nml_err_ret;
3168 nml_query (dtp, '=');
3169 return true;
3171 case '?':
3172 nml_query (dtp, '?');
3173 return true;
3175 case '$':
3176 case '&':
3177 nml_match_name (dtp, "end", 3);
3178 if (dtp->u.p.nml_read_error)
3180 snprintf (nml_err_msg, nml_err_msg_size,
3181 "namelist not terminated with / or &end");
3182 goto nml_err_ret;
3184 /* Fall through. */
3185 case '/':
3186 dtp->u.p.input_complete = 1;
3187 return true;
3189 default :
3190 break;
3193 /* Untouch all nodes of the namelist and reset the flags that are set for
3194 derived type components. */
3196 nml_untouch_nodes (dtp);
3197 component_flag = 0;
3198 qualifier_flag = 0;
3199 non_zero_rank_count = 0;
3201 /* Get the object name - should '!' and '\n' be permitted separators? */
3203 get_name:
3205 free_saved (dtp);
3209 if (!is_separator (c))
3210 push_char_default (dtp, tolower(c));
3211 if ((c = next_char (dtp)) == EOF)
3212 goto nml_err_ret;
3214 while (!( c=='=' || c==' ' || c=='\t' || c =='(' || c =='%' ));
3216 unget_char (dtp, c);
3218 /* Check that the name is in the namelist and get pointer to object.
3219 Three error conditions exist: (i) An attempt is being made to
3220 identify a non-existent object, following a failed data read or
3221 (ii) The object name does not exist or (iii) Too many data items
3222 are present for an object. (iii) gives the same error message
3223 as (i) */
3225 push_char_default (dtp, '\0');
3227 if (component_flag)
3229 #define EXT_STACK_SZ 100
3230 char ext_stack[EXT_STACK_SZ];
3231 char *ext_name;
3232 size_t var_len = strlen (root_nl->var_name);
3233 size_t saved_len
3234 = dtp->u.p.saved_string ? strlen (dtp->u.p.saved_string) : 0;
3235 size_t ext_size = var_len + saved_len + 1;
3237 if (ext_size > EXT_STACK_SZ)
3238 ext_name = xmalloc (ext_size);
3239 else
3240 ext_name = ext_stack;
3242 memcpy (ext_name, root_nl->var_name, var_len);
3243 if (dtp->u.p.saved_string)
3244 memcpy (ext_name + var_len, dtp->u.p.saved_string, saved_len);
3245 ext_name[var_len + saved_len] = '\0';
3246 nl = find_nml_node (dtp, ext_name);
3248 if (ext_size > EXT_STACK_SZ)
3249 free (ext_name);
3251 else
3252 nl = find_nml_node (dtp, dtp->u.p.saved_string);
3254 if (nl == NULL)
3256 if (dtp->u.p.nml_read_error && *pprev_nl)
3257 snprintf (nml_err_msg, nml_err_msg_size,
3258 "Bad data for namelist object %s", (*pprev_nl)->var_name);
3260 else
3261 snprintf (nml_err_msg, nml_err_msg_size,
3262 "Cannot match namelist object name %s",
3263 dtp->u.p.saved_string);
3265 goto nml_err_ret;
3267 else if (nl->dtio_sub != NULL)
3269 int unit = dtp->u.p.current_unit->unit_number;
3270 char iotype[] = "NAMELIST";
3271 gfc_charlen_type iotype_len = 8;
3272 char tmp_iomsg[IOMSG_LEN] = "";
3273 char *child_iomsg;
3274 gfc_charlen_type child_iomsg_len;
3275 int noiostat;
3276 int *child_iostat = NULL;
3277 gfc_array_i4 vlist;
3278 gfc_class list_obj;
3279 formatted_dtio dtio_ptr = (formatted_dtio)nl->dtio_sub;
3281 GFC_DESCRIPTOR_DATA(&vlist) = NULL;
3282 GFC_DIMENSION_SET(vlist.dim[0],1, 0, 0);
3284 list_obj.data = (void *)nl->mem_pos;
3285 list_obj.vptr = nl->vtable;
3286 list_obj.len = 0;
3288 /* Set iostat, intent(out). */
3289 noiostat = 0;
3290 child_iostat = (dtp->common.flags & IOPARM_HAS_IOSTAT) ?
3291 dtp->common.iostat : &noiostat;
3293 /* Set iomsg, intent(inout). */
3294 if (dtp->common.flags & IOPARM_HAS_IOMSG)
3296 child_iomsg = dtp->common.iomsg;
3297 child_iomsg_len = dtp->common.iomsg_len;
3299 else
3301 child_iomsg = tmp_iomsg;
3302 child_iomsg_len = IOMSG_LEN;
3305 /* Call the user defined formatted READ procedure. */
3306 dtp->u.p.current_unit->child_dtio++;
3307 dtio_ptr ((void *)&list_obj, &unit, iotype, &vlist,
3308 child_iostat, child_iomsg,
3309 iotype_len, child_iomsg_len);
3310 dtp->u.p.current_unit->child_dtio--;
3312 return true;
3315 /* Get the length, data length, base pointer and rank of the variable.
3316 Set the default loop specification first. */
3318 for (dim=0; dim < nl->var_rank; dim++)
3320 nl->ls[dim].step = 1;
3321 nl->ls[dim].end = GFC_DESCRIPTOR_UBOUND(nl,dim);
3322 nl->ls[dim].start = GFC_DESCRIPTOR_LBOUND(nl,dim);
3323 nl->ls[dim].idx = nl->ls[dim].start;
3326 /* Check to see if there is a qualifier: if so, parse it.*/
3328 if (c == '(' && nl->var_rank)
3330 parsed_rank = 0;
3331 if (!nml_parse_qualifier (dtp, nl->dim, nl->ls, nl->var_rank,
3332 nl->type, nml_err_msg, nml_err_msg_size,
3333 &parsed_rank))
3335 char *nml_err_msg_end = strchr (nml_err_msg, '\0');
3336 snprintf (nml_err_msg_end,
3337 nml_err_msg_size - (nml_err_msg_end - nml_err_msg),
3338 " for namelist variable %s", nl->var_name);
3339 goto nml_err_ret;
3341 if (parsed_rank > 0)
3342 non_zero_rank_count++;
3344 qualifier_flag = 1;
3346 if ((c = next_char (dtp)) == EOF)
3347 goto nml_err_ret;
3348 unget_char (dtp, c);
3350 else if (nl->var_rank > 0)
3351 non_zero_rank_count++;
3353 /* Now parse a derived type component. The root namelist_info address
3354 is backed up, as is the previous component level. The component flag
3355 is set and the iteration is made by jumping back to get_name. */
3357 if (c == '%')
3359 if (nl->type != BT_DERIVED)
3361 snprintf (nml_err_msg, nml_err_msg_size,
3362 "Attempt to get derived component for %s", nl->var_name);
3363 goto nml_err_ret;
3366 /* Don't move first_nl further in the list if a qualifier was found. */
3367 if ((*pprev_nl == NULL && !qualifier_flag) || !component_flag)
3368 first_nl = nl;
3370 root_nl = nl;
3372 component_flag = 1;
3373 if ((c = next_char (dtp)) == EOF)
3374 goto nml_err_ret;
3375 goto get_name;
3378 /* Parse a character qualifier, if present. chigh = 0 is a default
3379 that signals that the string length = string_length. */
3381 clow = 1;
3382 chigh = 0;
3384 if (c == '(' && nl->type == BT_CHARACTER)
3386 descriptor_dimension chd[1] = { {1, clow, nl->string_length} };
3387 array_loop_spec ind[1] = { {1, clow, nl->string_length, 1} };
3389 if (!nml_parse_qualifier (dtp, chd, ind, -1, nl->type,
3390 nml_err_msg, nml_err_msg_size, &parsed_rank))
3392 char *nml_err_msg_end = strchr (nml_err_msg, '\0');
3393 snprintf (nml_err_msg_end,
3394 nml_err_msg_size - (nml_err_msg_end - nml_err_msg),
3395 " for namelist variable %s", nl->var_name);
3396 goto nml_err_ret;
3399 clow = ind[0].start;
3400 chigh = ind[0].end;
3402 if (ind[0].step != 1)
3404 snprintf (nml_err_msg, nml_err_msg_size,
3405 "Step not allowed in substring qualifier"
3406 " for namelist object %s", nl->var_name);
3407 goto nml_err_ret;
3410 if ((c = next_char (dtp)) == EOF)
3411 goto nml_err_ret;
3412 unget_char (dtp, c);
3415 /* Make sure no extraneous qualifiers are there. */
3417 if (c == '(')
3419 snprintf (nml_err_msg, nml_err_msg_size,
3420 "Qualifier for a scalar or non-character namelist object %s",
3421 nl->var_name);
3422 goto nml_err_ret;
3425 /* Make sure there is no more than one non-zero rank object. */
3426 if (non_zero_rank_count > 1)
3428 snprintf (nml_err_msg, nml_err_msg_size,
3429 "Multiple sub-objects with non-zero rank in namelist object %s",
3430 nl->var_name);
3431 non_zero_rank_count = 0;
3432 goto nml_err_ret;
3435 /* According to the standard, an equal sign MUST follow an object name. The
3436 following is possibly lax - it allows comments, blank lines and so on to
3437 intervene. eat_spaces (dtp); c = next_char (dtp); would be compliant*/
3439 free_saved (dtp);
3441 eat_separator (dtp);
3442 if (dtp->u.p.input_complete)
3443 return true;
3445 if (dtp->u.p.at_eol)
3446 finish_separator (dtp);
3447 if (dtp->u.p.input_complete)
3448 return true;
3450 if ((c = next_char (dtp)) == EOF)
3451 goto nml_err_ret;
3453 if (c != '=')
3455 snprintf (nml_err_msg, nml_err_msg_size,
3456 "Equal sign must follow namelist object name %s",
3457 nl->var_name);
3458 goto nml_err_ret;
3460 /* If a derived type, touch its components and restore the root
3461 namelist_info if we have parsed a qualified derived type
3462 component. */
3464 if (nl->type == BT_DERIVED)
3465 nml_touch_nodes (nl);
3467 if (first_nl)
3469 if (first_nl->var_rank == 0)
3471 if (component_flag && qualifier_flag)
3472 nl = first_nl;
3474 else
3475 nl = first_nl;
3478 dtp->u.p.nml_read_error = 0;
3479 if (!nml_read_obj (dtp, nl, 0, pprev_nl, nml_err_msg, nml_err_msg_size,
3480 clow, chigh))
3481 goto nml_err_ret;
3483 return true;
3485 nml_err_ret:
3487 /* The EOF error message is issued by hit_eof. Return true so that the
3488 caller does not use nml_err_msg and nml_err_msg_size to generate
3489 an unrelated error message. */
3490 if (c == EOF)
3492 dtp->u.p.input_complete = 1;
3493 unget_char (dtp, c);
3494 hit_eof (dtp);
3495 return true;
3497 return false;
3500 /* Entry point for namelist input. Goes through input until namelist name
3501 is matched. Then cycles through nml_get_obj_data until the input is
3502 completed or there is an error. */
3504 void
3505 namelist_read (st_parameter_dt *dtp)
3507 int c;
3508 char nml_err_msg[200];
3510 /* Initialize the error string buffer just in case we get an unexpected fail
3511 somewhere and end up at nml_err_ret. */
3512 strcpy (nml_err_msg, "Internal namelist read error");
3514 /* Pointer to the previously read object, in case attempt is made to read
3515 new object name. Should this fail, error message can give previous
3516 name. */
3517 namelist_info *prev_nl = NULL;
3519 dtp->u.p.namelist_mode = 1;
3520 dtp->u.p.input_complete = 0;
3521 dtp->u.p.expanded_read = 0;
3523 /* Set the next_char and push_char worker functions. */
3524 set_workers (dtp);
3526 /* Look for &namelist_name . Skip all characters, testing for $nmlname.
3527 Exit on success or EOF. If '?' or '=?' encountered in stdin, print
3528 node names or namelist on stdout. */
3530 find_nml_name:
3531 c = next_char (dtp);
3532 switch (c)
3534 case '$':
3535 case '&':
3536 break;
3538 case '!':
3539 eat_line (dtp);
3540 goto find_nml_name;
3542 case '=':
3543 c = next_char (dtp);
3544 if (c == '?')
3545 nml_query (dtp, '=');
3546 else
3547 unget_char (dtp, c);
3548 goto find_nml_name;
3550 case '?':
3551 nml_query (dtp, '?');
3552 goto find_nml_name;
3554 case EOF:
3555 return;
3557 default:
3558 goto find_nml_name;
3561 /* Match the name of the namelist. */
3563 nml_match_name (dtp, dtp->namelist_name, dtp->namelist_name_len);
3565 if (dtp->u.p.nml_read_error)
3566 goto find_nml_name;
3568 /* A trailing space is required, we give a little latitude here, 10.9.1. */
3569 c = next_char (dtp);
3570 if (!is_separator(c) && c != '!')
3572 unget_char (dtp, c);
3573 goto find_nml_name;
3576 unget_char (dtp, c);
3577 eat_separator (dtp);
3579 /* Ready to read namelist objects. If there is an error in input
3580 from stdin, output the error message and continue. */
3582 while (!dtp->u.p.input_complete)
3584 if (!nml_get_obj_data (dtp, &prev_nl, nml_err_msg, sizeof nml_err_msg))
3586 if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
3587 goto nml_err_ret;
3588 generate_error (&dtp->common, LIBERROR_READ_VALUE, nml_err_msg);
3591 /* Reset the previous namelist pointer if we know we are not going
3592 to be doing multiple reads within a single namelist object. */
3593 if (prev_nl && prev_nl->var_rank == 0)
3594 prev_nl = NULL;
3597 free_saved (dtp);
3598 free_line (dtp);
3599 return;
3602 nml_err_ret:
3604 /* All namelist error calls return from here */
3605 free_saved (dtp);
3606 free_line (dtp);
3607 generate_error (&dtp->common, LIBERROR_READ_VALUE, nml_err_msg);
3608 return;