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