Debian Codegraph @

libgfortran/io/list_read.c

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