string.c (compare0): Use gfc_charlen_type.
[gcc.git] / libgfortran / io / list_read.c
1 /* Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 2009
2 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
4 Namelist input contributed by Paul Thomas
5 F2003 I/O support contributed by Jerry DeLisle
6
7 This file is part of the GNU Fortran 95 runtime library (libgfortran).
8
9 Libgfortran is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13
14 In addition to the permissions in the GNU General Public License, the
15 Free Software Foundation gives you unlimited permission to link the
16 compiled version of this file into combinations with other programs,
17 and to distribute those combinations without any restriction coming
18 from the use of this file. (The General Public License restrictions
19 do apply in other respects; for example, they cover modification of
20 the file, and distribution when not linked into a combine
21 executable.)
22
23 Libgfortran is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
27
28 You should have received a copy of the GNU General Public License
29 along with Libgfortran; see the file COPYING. If not, write to
30 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
31 Boston, MA 02110-1301, USA. */
32
33
34 #include "io.h"
35 #include <string.h>
36 #include <stdlib.h>
37 #include <ctype.h>
38
39
40 /* List directed input. Several parsing subroutines are practically
41 reimplemented from formatted input, the reason being that there are
42 all kinds of small differences between formatted and list directed
43 parsing. */
44
45
46 /* Subroutines for reading characters from the input. Because a
47 repeat count is ambiguous with an integer, we have to read the
48 whole digit string before seeing if there is a '*' which signals
49 the repeat count. Since we can have a lot of potential leading
50 zeros, we have to be able to back up by arbitrary amount. Because
51 the input might not be seekable, we have to buffer the data
52 ourselves. */
53
54 #define CASE_DIGITS case '0': case '1': case '2': case '3': case '4': \
55 case '5': case '6': case '7': case '8': case '9'
56
57 #define CASE_SEPARATORS case ' ': case ',': case '/': case '\n': case '\t': \
58 case '\r': case ';'
59
60 /* This macro assumes that we're operating on a variable. */
61
62 #define is_separator(c) (c == '/' || c == ',' || c == '\n' || c == ' ' \
63 || c == '\t' || c == '\r' || c == ';')
64
65 /* Maximum repeat count. Less than ten times the maximum signed int32. */
66
67 #define MAX_REPEAT 200000000
68
69 #ifndef HAVE_SNPRINTF
70 # undef snprintf
71 # define snprintf(str, size, ...) sprintf (str, __VA_ARGS__)
72 #endif
73
74 /* Save a character to a string buffer, enlarging it as necessary. */
75
76 static void
77 push_char (st_parameter_dt *dtp, char c)
78 {
79 char *new;
80
81 if (dtp->u.p.saved_string == NULL)
82 {
83 dtp->u.p.saved_string = get_mem (SCRATCH_SIZE);
84 // memset below should be commented out.
85 memset (dtp->u.p.saved_string, 0, SCRATCH_SIZE);
86 dtp->u.p.saved_length = SCRATCH_SIZE;
87 dtp->u.p.saved_used = 0;
88 }
89
90 if (dtp->u.p.saved_used >= dtp->u.p.saved_length)
91 {
92 dtp->u.p.saved_length = 2 * dtp->u.p.saved_length;
93 new = realloc (dtp->u.p.saved_string, dtp->u.p.saved_length);
94 if (new == NULL)
95 generate_error (&dtp->common, LIBERROR_OS, NULL);
96 dtp->u.p.saved_string = new;
97
98 // Also this should not be necessary.
99 memset (new + dtp->u.p.saved_used, 0,
100 dtp->u.p.saved_length - dtp->u.p.saved_used);
101
102 }
103
104 dtp->u.p.saved_string[dtp->u.p.saved_used++] = c;
105 }
106
107
108 /* Free the input buffer if necessary. */
109
110 static void
111 free_saved (st_parameter_dt *dtp)
112 {
113 if (dtp->u.p.saved_string == NULL)
114 return;
115
116 free_mem (dtp->u.p.saved_string);
117
118 dtp->u.p.saved_string = NULL;
119 dtp->u.p.saved_used = 0;
120 }
121
122
123 /* Free the line buffer if necessary. */
124
125 static void
126 free_line (st_parameter_dt *dtp)
127 {
128 dtp->u.p.item_count = 0;
129 dtp->u.p.line_buffer_enabled = 0;
130
131 if (dtp->u.p.line_buffer == NULL)
132 return;
133
134 free_mem (dtp->u.p.line_buffer);
135 dtp->u.p.line_buffer = NULL;
136 }
137
138
139 static char
140 next_char (st_parameter_dt *dtp)
141 {
142 ssize_t length;
143 gfc_offset record;
144 char c;
145 int cc;
146
147 if (dtp->u.p.last_char != '\0')
148 {
149 dtp->u.p.at_eol = 0;
150 c = dtp->u.p.last_char;
151 dtp->u.p.last_char = '\0';
152 goto done;
153 }
154
155 /* Read from line_buffer if enabled. */
156
157 if (dtp->u.p.line_buffer_enabled)
158 {
159 dtp->u.p.at_eol = 0;
160
161 c = dtp->u.p.line_buffer[dtp->u.p.item_count];
162 if (c != '\0' && dtp->u.p.item_count < 64)
163 {
164 dtp->u.p.line_buffer[dtp->u.p.item_count] = '\0';
165 dtp->u.p.item_count++;
166 goto done;
167 }
168
169 dtp->u.p.item_count = 0;
170 dtp->u.p.line_buffer_enabled = 0;
171 }
172
173 /* Handle the end-of-record and end-of-file conditions for
174 internal array unit. */
175 if (is_array_io (dtp))
176 {
177 if (dtp->u.p.at_eof)
178 longjmp (*dtp->u.p.eof_jump, 1);
179
180 /* Check for "end-of-record" condition. */
181 if (dtp->u.p.current_unit->bytes_left == 0)
182 {
183 int finished;
184
185 c = '\n';
186 record = next_array_record (dtp, dtp->u.p.current_unit->ls,
187 &finished);
188
189 /* Check for "end-of-file" condition. */
190 if (finished)
191 {
192 dtp->u.p.at_eof = 1;
193 goto done;
194 }
195
196 record *= dtp->u.p.current_unit->recl;
197 if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0)
198 longjmp (*dtp->u.p.eof_jump, 1);
199
200 dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl;
201 goto done;
202 }
203 }
204
205 /* Get the next character and handle end-of-record conditions. */
206
207 if (is_internal_unit (dtp))
208 {
209 length = sread (dtp->u.p.current_unit->s, &c, 1);
210 if (length < 0)
211 {
212 generate_error (&dtp->common, LIBERROR_OS, NULL);
213 return '\0';
214 }
215
216 if (is_array_io (dtp))
217 {
218 /* Check whether we hit EOF. */
219 if (length == 0)
220 {
221 generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL);
222 return '\0';
223 }
224 dtp->u.p.current_unit->bytes_left--;
225 }
226 else
227 {
228 if (dtp->u.p.at_eof)
229 longjmp (*dtp->u.p.eof_jump, 1);
230 if (length == 0)
231 {
232 c = '\n';
233 dtp->u.p.at_eof = 1;
234 }
235 }
236 }
237 else
238 {
239 cc = fbuf_getc (dtp->u.p.current_unit);
240
241 if (cc == EOF)
242 {
243 if (dtp->u.p.current_unit->endfile == AT_ENDFILE)
244 longjmp (*dtp->u.p.eof_jump, 1);
245 dtp->u.p.current_unit->endfile = AT_ENDFILE;
246 c = '\n';
247 }
248 else
249 c = (char) cc;
250 if (is_stream_io (dtp) && cc != EOF)
251 dtp->u.p.current_unit->strm_pos++;
252
253 }
254 done:
255 dtp->u.p.at_eol = (c == '\n' || c == '\r');
256 return c;
257 }
258
259
260 /* Push a character back onto the input. */
261
262 static void
263 unget_char (st_parameter_dt *dtp, char c)
264 {
265 dtp->u.p.last_char = c;
266 }
267
268
269 /* Skip over spaces in the input. Returns the nonspace character that
270 terminated the eating and also places it back on the input. */
271
272 static char
273 eat_spaces (st_parameter_dt *dtp)
274 {
275 char c;
276
277 do
278 {
279 c = next_char (dtp);
280 }
281 while (c == ' ' || c == '\t');
282
283 unget_char (dtp, c);
284 return c;
285 }
286
287
288 /* This function reads characters through to the end of the current line and
289 just ignores them. */
290
291 static void
292 eat_line (st_parameter_dt *dtp)
293 {
294 char c;
295 if (!is_internal_unit (dtp))
296 do
297 c = next_char (dtp);
298 while (c != '\n');
299 }
300
301
302 /* Skip over a separator. Technically, we don't always eat the whole
303 separator. This is because if we've processed the last input item,
304 then a separator is unnecessary. Plus the fact that operating
305 systems usually deliver console input on a line basis.
306
307 The upshot is that if we see a newline as part of reading a
308 separator, we stop reading. If there are more input items, we
309 continue reading the separator with finish_separator() which takes
310 care of the fact that we may or may not have seen a comma as part
311 of the separator. */
312
313 static void
314 eat_separator (st_parameter_dt *dtp)
315 {
316 char c, n;
317
318 eat_spaces (dtp);
319 dtp->u.p.comma_flag = 0;
320
321 c = next_char (dtp);
322 switch (c)
323 {
324 case ',':
325 if (dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
326 {
327 unget_char (dtp, c);
328 break;
329 }
330 /* Fall through. */
331 case ';':
332 dtp->u.p.comma_flag = 1;
333 eat_spaces (dtp);
334 break;
335
336 case '/':
337 dtp->u.p.input_complete = 1;
338 break;
339
340 case '\r':
341 dtp->u.p.at_eol = 1;
342 n = next_char(dtp);
343 if (n != '\n')
344 {
345 unget_char (dtp, n);
346 break;
347 }
348 /* Fall through. */
349 case '\n':
350 dtp->u.p.at_eol = 1;
351 if (dtp->u.p.namelist_mode)
352 {
353 do
354 {
355 c = next_char (dtp);
356 if (c == '!')
357 {
358 eat_line (dtp);
359 c = next_char (dtp);
360 if (c == '!')
361 {
362 eat_line (dtp);
363 c = next_char (dtp);
364 }
365 }
366 }
367 while (c == '\n' || c == '\r' || c == ' ' || c == '\t');
368 unget_char (dtp, c);
369 }
370 break;
371
372 case '!':
373 if (dtp->u.p.namelist_mode)
374 { /* Eat a namelist comment. */
375 do
376 c = next_char (dtp);
377 while (c != '\n');
378
379 break;
380 }
381
382 /* Fall Through... */
383
384 default:
385 unget_char (dtp, c);
386 break;
387 }
388 }
389
390
391 /* Finish processing a separator that was interrupted by a newline.
392 If we're here, then another data item is present, so we finish what
393 we started on the previous line. */
394
395 static void
396 finish_separator (st_parameter_dt *dtp)
397 {
398 char c;
399
400 restart:
401 eat_spaces (dtp);
402
403 c = next_char (dtp);
404 switch (c)
405 {
406 case ',':
407 if (dtp->u.p.comma_flag)
408 unget_char (dtp, c);
409 else
410 {
411 c = eat_spaces (dtp);
412 if (c == '\n' || c == '\r')
413 goto restart;
414 }
415
416 break;
417
418 case '/':
419 dtp->u.p.input_complete = 1;
420 if (!dtp->u.p.namelist_mode)
421 return;
422 break;
423
424 case '\n':
425 case '\r':
426 goto restart;
427
428 case '!':
429 if (dtp->u.p.namelist_mode)
430 {
431 do
432 c = next_char (dtp);
433 while (c != '\n');
434
435 goto restart;
436 }
437
438 default:
439 unget_char (dtp, c);
440 break;
441 }
442 }
443
444
445 /* This function is needed to catch bad conversions so that namelist can
446 attempt to see if dtp->u.p.saved_string contains a new object name rather
447 than a bad value. */
448
449 static int
450 nml_bad_return (st_parameter_dt *dtp, char c)
451 {
452 if (dtp->u.p.namelist_mode)
453 {
454 dtp->u.p.nml_read_error = 1;
455 unget_char (dtp, c);
456 return 1;
457 }
458 return 0;
459 }
460
461 /* Convert an unsigned string to an integer. The length value is -1
462 if we are working on a repeat count. Returns nonzero if we have a
463 range problem. As a side effect, frees the dtp->u.p.saved_string. */
464
465 static int
466 convert_integer (st_parameter_dt *dtp, int length, int negative)
467 {
468 char c, *buffer, message[100];
469 int m;
470 GFC_INTEGER_LARGEST v, max, max10;
471
472 buffer = dtp->u.p.saved_string;
473 v = 0;
474
475 max = (length == -1) ? MAX_REPEAT : max_value (length, 1);
476 max10 = max / 10;
477
478 for (;;)
479 {
480 c = *buffer++;
481 if (c == '\0')
482 break;
483 c -= '0';
484
485 if (v > max10)
486 goto overflow;
487 v = 10 * v;
488
489 if (v > max - c)
490 goto overflow;
491 v += c;
492 }
493
494 m = 0;
495
496 if (length != -1)
497 {
498 if (negative)
499 v = -v;
500 set_integer (dtp->u.p.value, v, length);
501 }
502 else
503 {
504 dtp->u.p.repeat_count = v;
505
506 if (dtp->u.p.repeat_count == 0)
507 {
508 sprintf (message, "Zero repeat count in item %d of list input",
509 dtp->u.p.item_count);
510
511 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
512 m = 1;
513 }
514 }
515
516 free_saved (dtp);
517 return m;
518
519 overflow:
520 if (length == -1)
521 sprintf (message, "Repeat count overflow in item %d of list input",
522 dtp->u.p.item_count);
523 else
524 sprintf (message, "Integer overflow while reading item %d",
525 dtp->u.p.item_count);
526
527 free_saved (dtp);
528 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
529
530 return 1;
531 }
532
533
534 /* Parse a repeat count for logical and complex values which cannot
535 begin with a digit. Returns nonzero if we are done, zero if we
536 should continue on. */
537
538 static int
539 parse_repeat (st_parameter_dt *dtp)
540 {
541 char c, message[100];
542 int repeat;
543
544 c = next_char (dtp);
545 switch (c)
546 {
547 CASE_DIGITS:
548 repeat = c - '0';
549 break;
550
551 CASE_SEPARATORS:
552 unget_char (dtp, c);
553 eat_separator (dtp);
554 return 1;
555
556 default:
557 unget_char (dtp, c);
558 return 0;
559 }
560
561 for (;;)
562 {
563 c = next_char (dtp);
564 switch (c)
565 {
566 CASE_DIGITS:
567 repeat = 10 * repeat + c - '0';
568
569 if (repeat > MAX_REPEAT)
570 {
571 sprintf (message,
572 "Repeat count overflow in item %d of list input",
573 dtp->u.p.item_count);
574
575 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
576 return 1;
577 }
578
579 break;
580
581 case '*':
582 if (repeat == 0)
583 {
584 sprintf (message,
585 "Zero repeat count in item %d of list input",
586 dtp->u.p.item_count);
587
588 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
589 return 1;
590 }
591
592 goto done;
593
594 default:
595 goto bad_repeat;
596 }
597 }
598
599 done:
600 dtp->u.p.repeat_count = repeat;
601 return 0;
602
603 bad_repeat:
604
605 eat_line (dtp);
606 free_saved (dtp);
607 sprintf (message, "Bad repeat count in item %d of list input",
608 dtp->u.p.item_count);
609 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
610 return 1;
611 }
612
613
614 /* To read a logical we have to look ahead in the input stream to make sure
615 there is not an equal sign indicating a variable name. To do this we use
616 line_buffer to point to a temporary buffer, pushing characters there for
617 possible later reading. */
618
619 static void
620 l_push_char (st_parameter_dt *dtp, char c)
621 {
622 if (dtp->u.p.line_buffer == NULL)
623 {
624 dtp->u.p.line_buffer = get_mem (SCRATCH_SIZE);
625 memset (dtp->u.p.line_buffer, 0, SCRATCH_SIZE);
626 }
627
628 dtp->u.p.line_buffer[dtp->u.p.item_count++] = c;
629 }
630
631
632 /* Read a logical character on the input. */
633
634 static void
635 read_logical (st_parameter_dt *dtp, int length)
636 {
637 char c, message[100];
638 int i, v;
639
640 if (parse_repeat (dtp))
641 return;
642
643 c = tolower (next_char (dtp));
644 l_push_char (dtp, c);
645 switch (c)
646 {
647 case 't':
648 v = 1;
649 c = next_char (dtp);
650 l_push_char (dtp, c);
651
652 if (!is_separator(c))
653 goto possible_name;
654
655 unget_char (dtp, c);
656 break;
657 case 'f':
658 v = 0;
659 c = next_char (dtp);
660 l_push_char (dtp, c);
661
662 if (!is_separator(c))
663 goto possible_name;
664
665 unget_char (dtp, c);
666 break;
667
668 case '.':
669 c = tolower (next_char (dtp));
670 switch (c)
671 {
672 case 't':
673 v = 1;
674 break;
675 case 'f':
676 v = 0;
677 break;
678 default:
679 goto bad_logical;
680 }
681
682 break;
683
684 CASE_SEPARATORS:
685 unget_char (dtp, c);
686 eat_separator (dtp);
687 return; /* Null value. */
688
689 default:
690 /* Save the character in case it is the beginning
691 of the next object name. */
692 unget_char (dtp, c);
693 goto bad_logical;
694 }
695
696 dtp->u.p.saved_type = BT_LOGICAL;
697 dtp->u.p.saved_length = length;
698
699 /* Eat trailing garbage. */
700 do
701 {
702 c = next_char (dtp);
703 }
704 while (!is_separator (c));
705
706 unget_char (dtp, c);
707 eat_separator (dtp);
708 set_integer ((int *) dtp->u.p.value, v, length);
709 free_line (dtp);
710
711 return;
712
713 possible_name:
714
715 for(i = 0; i < 63; i++)
716 {
717 c = next_char (dtp);
718 if (is_separator(c))
719 {
720 /* All done if this is not a namelist read. */
721 if (!dtp->u.p.namelist_mode)
722 goto logical_done;
723
724 unget_char (dtp, c);
725 eat_separator (dtp);
726 c = next_char (dtp);
727 if (c != '=')
728 {
729 unget_char (dtp, c);
730 goto logical_done;
731 }
732 }
733
734 l_push_char (dtp, c);
735 if (c == '=')
736 {
737 dtp->u.p.nml_read_error = 1;
738 dtp->u.p.line_buffer_enabled = 1;
739 dtp->u.p.item_count = 0;
740 return;
741 }
742
743 }
744
745 bad_logical:
746
747 free_line (dtp);
748
749 if (nml_bad_return (dtp, c))
750 return;
751
752 eat_line (dtp);
753 free_saved (dtp);
754 sprintf (message, "Bad logical value while reading item %d",
755 dtp->u.p.item_count);
756 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
757 return;
758
759 logical_done:
760
761 dtp->u.p.saved_type = BT_LOGICAL;
762 dtp->u.p.saved_length = length;
763 set_integer ((int *) dtp->u.p.value, v, length);
764 free_saved (dtp);
765 free_line (dtp);
766 }
767
768
769 /* Reading integers is tricky because we can actually be reading a
770 repeat count. We have to store the characters in a buffer because
771 we could be reading an integer that is larger than the default int
772 used for repeat counts. */
773
774 static void
775 read_integer (st_parameter_dt *dtp, int length)
776 {
777 char c, message[100];
778 int negative;
779
780 negative = 0;
781
782 c = next_char (dtp);
783 switch (c)
784 {
785 case '-':
786 negative = 1;
787 /* Fall through... */
788
789 case '+':
790 c = next_char (dtp);
791 goto get_integer;
792
793 CASE_SEPARATORS: /* Single null. */
794 unget_char (dtp, c);
795 eat_separator (dtp);
796 return;
797
798 CASE_DIGITS:
799 push_char (dtp, c);
800 break;
801
802 default:
803 goto bad_integer;
804 }
805
806 /* Take care of what may be a repeat count. */
807
808 for (;;)
809 {
810 c = next_char (dtp);
811 switch (c)
812 {
813 CASE_DIGITS:
814 push_char (dtp, c);
815 break;
816
817 case '*':
818 push_char (dtp, '\0');
819 goto repeat;
820
821 CASE_SEPARATORS: /* Not a repeat count. */
822 goto done;
823
824 default:
825 goto bad_integer;
826 }
827 }
828
829 repeat:
830 if (convert_integer (dtp, -1, 0))
831 return;
832
833 /* Get the real integer. */
834
835 c = next_char (dtp);
836 switch (c)
837 {
838 CASE_DIGITS:
839 break;
840
841 CASE_SEPARATORS:
842 unget_char (dtp, c);
843 eat_separator (dtp);
844 return;
845
846 case '-':
847 negative = 1;
848 /* Fall through... */
849
850 case '+':
851 c = next_char (dtp);
852 break;
853 }
854
855 get_integer:
856 if (!isdigit (c))
857 goto bad_integer;
858 push_char (dtp, c);
859
860 for (;;)
861 {
862 c = next_char (dtp);
863 switch (c)
864 {
865 CASE_DIGITS:
866 push_char (dtp, c);
867 break;
868
869 CASE_SEPARATORS:
870 goto done;
871
872 default:
873 goto bad_integer;
874 }
875 }
876
877 bad_integer:
878
879 if (nml_bad_return (dtp, c))
880 return;
881
882 eat_line (dtp);
883 free_saved (dtp);
884 sprintf (message, "Bad integer for item %d in list input",
885 dtp->u.p.item_count);
886 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
887
888 return;
889
890 done:
891 unget_char (dtp, c);
892 eat_separator (dtp);
893
894 push_char (dtp, '\0');
895 if (convert_integer (dtp, length, negative))
896 {
897 free_saved (dtp);
898 return;
899 }
900
901 free_saved (dtp);
902 dtp->u.p.saved_type = BT_INTEGER;
903 }
904
905
906 /* Read a character variable. */
907
908 static void
909 read_character (st_parameter_dt *dtp, int length __attribute__ ((unused)))
910 {
911 char c, quote, message[100];
912
913 quote = ' '; /* Space means no quote character. */
914
915 c = next_char (dtp);
916 switch (c)
917 {
918 CASE_DIGITS:
919 push_char (dtp, c);
920 break;
921
922 CASE_SEPARATORS:
923 unget_char (dtp, c); /* NULL value. */
924 eat_separator (dtp);
925 return;
926
927 case '"':
928 case '\'':
929 quote = c;
930 goto get_string;
931
932 default:
933 if (dtp->u.p.namelist_mode)
934 {
935 unget_char (dtp, c);
936 return;
937 }
938
939 push_char (dtp, c);
940 goto get_string;
941 }
942
943 /* Deal with a possible repeat count. */
944
945 for (;;)
946 {
947 c = next_char (dtp);
948 switch (c)
949 {
950 CASE_DIGITS:
951 push_char (dtp, c);
952 break;
953
954 CASE_SEPARATORS:
955 unget_char (dtp, c);
956 goto done; /* String was only digits! */
957
958 case '*':
959 push_char (dtp, '\0');
960 goto got_repeat;
961
962 default:
963 push_char (dtp, c);
964 goto get_string; /* Not a repeat count after all. */
965 }
966 }
967
968 got_repeat:
969 if (convert_integer (dtp, -1, 0))
970 return;
971
972 /* Now get the real string. */
973
974 c = next_char (dtp);
975 switch (c)
976 {
977 CASE_SEPARATORS:
978 unget_char (dtp, c); /* Repeated NULL values. */
979 eat_separator (dtp);
980 return;
981
982 case '"':
983 case '\'':
984 quote = c;
985 break;
986
987 default:
988 push_char (dtp, c);
989 break;
990 }
991
992 get_string:
993 for (;;)
994 {
995 c = next_char (dtp);
996 switch (c)
997 {
998 case '"':
999 case '\'':
1000 if (c != quote)
1001 {
1002 push_char (dtp, c);
1003 break;
1004 }
1005
1006 /* See if we have a doubled quote character or the end of
1007 the string. */
1008
1009 c = next_char (dtp);
1010 if (c == quote)
1011 {
1012 push_char (dtp, quote);
1013 break;
1014 }
1015
1016 unget_char (dtp, c);
1017 goto done;
1018
1019 CASE_SEPARATORS:
1020 if (quote == ' ')
1021 {
1022 unget_char (dtp, c);
1023 goto done;
1024 }
1025
1026 if (c != '\n' && c != '\r')
1027 push_char (dtp, c);
1028 break;
1029
1030 default:
1031 push_char (dtp, c);
1032 break;
1033 }
1034 }
1035
1036 /* At this point, we have to have a separator, or else the string is
1037 invalid. */
1038 done:
1039 c = next_char (dtp);
1040 if (is_separator (c) || c == '!')
1041 {
1042 unget_char (dtp, c);
1043 eat_separator (dtp);
1044 dtp->u.p.saved_type = BT_CHARACTER;
1045 free_line (dtp);
1046 }
1047 else
1048 {
1049 free_saved (dtp);
1050 sprintf (message, "Invalid string input in item %d",
1051 dtp->u.p.item_count);
1052 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1053 }
1054 }
1055
1056
1057 /* Parse a component of a complex constant or a real number that we
1058 are sure is already there. This is a straight real number parser. */
1059
1060 static int
1061 parse_real (st_parameter_dt *dtp, void *buffer, int length)
1062 {
1063 char c, message[100];
1064 int m, seen_dp;
1065
1066 c = next_char (dtp);
1067 if (c == '-' || c == '+')
1068 {
1069 push_char (dtp, c);
1070 c = next_char (dtp);
1071 }
1072
1073 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1074 c = '.';
1075
1076 if (!isdigit (c) && c != '.')
1077 {
1078 if (c == 'i' || c == 'I' || c == 'n' || c == 'N')
1079 goto inf_nan;
1080 else
1081 goto bad;
1082 }
1083
1084 push_char (dtp, c);
1085
1086 seen_dp = (c == '.') ? 1 : 0;
1087
1088 for (;;)
1089 {
1090 c = next_char (dtp);
1091 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1092 c = '.';
1093 switch (c)
1094 {
1095 CASE_DIGITS:
1096 push_char (dtp, c);
1097 break;
1098
1099 case '.':
1100 if (seen_dp)
1101 goto bad;
1102
1103 seen_dp = 1;
1104 push_char (dtp, c);
1105 break;
1106
1107 case 'e':
1108 case 'E':
1109 case 'd':
1110 case 'D':
1111 push_char (dtp, 'e');
1112 goto exp1;
1113
1114 case '-':
1115 case '+':
1116 push_char (dtp, 'e');
1117 push_char (dtp, c);
1118 c = next_char (dtp);
1119 goto exp2;
1120
1121 CASE_SEPARATORS:
1122 unget_char (dtp, c);
1123 goto done;
1124
1125 default:
1126 goto done;
1127 }
1128 }
1129
1130 exp1:
1131 c = next_char (dtp);
1132 if (c != '-' && c != '+')
1133 push_char (dtp, '+');
1134 else
1135 {
1136 push_char (dtp, c);
1137 c = next_char (dtp);
1138 }
1139
1140 exp2:
1141 if (!isdigit (c))
1142 goto bad;
1143
1144 push_char (dtp, c);
1145
1146 for (;;)
1147 {
1148 c = next_char (dtp);
1149 switch (c)
1150 {
1151 CASE_DIGITS:
1152 push_char (dtp, c);
1153 break;
1154
1155 CASE_SEPARATORS:
1156 unget_char (dtp, c);
1157 goto done;
1158
1159 default:
1160 goto done;
1161 }
1162 }
1163
1164 done:
1165 unget_char (dtp, c);
1166 push_char (dtp, '\0');
1167
1168 m = convert_real (dtp, buffer, dtp->u.p.saved_string, length);
1169 free_saved (dtp);
1170
1171 return m;
1172
1173 inf_nan:
1174 /* Match INF and Infinity. */
1175 if ((c == 'i' || c == 'I')
1176 && ((c = next_char (dtp)) == 'n' || c == 'N')
1177 && ((c = next_char (dtp)) == 'f' || c == 'F'))
1178 {
1179 c = next_char (dtp);
1180 if ((c != 'i' && c != 'I')
1181 || ((c == 'i' || c == 'I')
1182 && ((c = next_char (dtp)) == 'n' || c == 'N')
1183 && ((c = next_char (dtp)) == 'i' || c == 'I')
1184 && ((c = next_char (dtp)) == 't' || c == 'T')
1185 && ((c = next_char (dtp)) == 'y' || c == 'Y')
1186 && (c = next_char (dtp))))
1187 {
1188 if (is_separator (c))
1189 unget_char (dtp, c);
1190 push_char (dtp, 'i');
1191 push_char (dtp, 'n');
1192 push_char (dtp, 'f');
1193 goto done;
1194 }
1195 } /* Match NaN. */
1196 else if (((c = next_char (dtp)) == 'a' || c == 'A')
1197 && ((c = next_char (dtp)) == 'n' || c == 'N')
1198 && (c = next_char (dtp)))
1199 {
1200 if (is_separator (c))
1201 unget_char (dtp, c);
1202 push_char (dtp, 'n');
1203 push_char (dtp, 'a');
1204 push_char (dtp, 'n');
1205 goto done;
1206 }
1207
1208 bad:
1209
1210 if (nml_bad_return (dtp, c))
1211 return 0;
1212
1213 eat_line (dtp);
1214 free_saved (dtp);
1215 sprintf (message, "Bad floating point number for item %d",
1216 dtp->u.p.item_count);
1217 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1218
1219 return 1;
1220 }
1221
1222
1223 /* Reading a complex number is straightforward because we can tell
1224 what it is right away. */
1225
1226 static void
1227 read_complex (st_parameter_dt *dtp, int kind, size_t size)
1228 {
1229 char message[100];
1230 char c;
1231
1232 if (parse_repeat (dtp))
1233 return;
1234
1235 c = next_char (dtp);
1236 switch (c)
1237 {
1238 case '(':
1239 break;
1240
1241 CASE_SEPARATORS:
1242 unget_char (dtp, c);
1243 eat_separator (dtp);
1244 return;
1245
1246 default:
1247 goto bad_complex;
1248 }
1249
1250 eat_spaces (dtp);
1251 if (parse_real (dtp, dtp->u.p.value, kind))
1252 return;
1253
1254 eol_1:
1255 eat_spaces (dtp);
1256 c = next_char (dtp);
1257 if (c == '\n' || c== '\r')
1258 goto eol_1;
1259 else
1260 unget_char (dtp, c);
1261
1262 if (next_char (dtp)
1263 != (dtp->u.p.current_unit->decimal_status == DECIMAL_POINT ? ',' : ';'))
1264 goto bad_complex;
1265
1266 eol_2:
1267 eat_spaces (dtp);
1268 c = next_char (dtp);
1269 if (c == '\n' || c== '\r')
1270 goto eol_2;
1271 else
1272 unget_char (dtp, c);
1273
1274 if (parse_real (dtp, dtp->u.p.value + size / 2, kind))
1275 return;
1276
1277 eat_spaces (dtp);
1278 if (next_char (dtp) != ')')
1279 goto bad_complex;
1280
1281 c = next_char (dtp);
1282 if (!is_separator (c))
1283 goto bad_complex;
1284
1285 unget_char (dtp, c);
1286 eat_separator (dtp);
1287
1288 free_saved (dtp);
1289 dtp->u.p.saved_type = BT_COMPLEX;
1290 return;
1291
1292 bad_complex:
1293
1294 if (nml_bad_return (dtp, c))
1295 return;
1296
1297 eat_line (dtp);
1298 free_saved (dtp);
1299 sprintf (message, "Bad complex value in item %d of list input",
1300 dtp->u.p.item_count);
1301 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1302 }
1303
1304
1305 /* Parse a real number with a possible repeat count. */
1306
1307 static void
1308 read_real (st_parameter_dt *dtp, int length)
1309 {
1310 char c, message[100];
1311 int seen_dp;
1312 int is_inf;
1313
1314 seen_dp = 0;
1315
1316 c = next_char (dtp);
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 push_char (dtp, c);
1327 seen_dp = 1;
1328 break;
1329
1330 case '+':
1331 case '-':
1332 goto got_sign;
1333
1334 CASE_SEPARATORS:
1335 unget_char (dtp, c); /* Single null. */
1336 eat_separator (dtp);
1337 return;
1338
1339 case 'i':
1340 case 'I':
1341 case 'n':
1342 case 'N':
1343 goto inf_nan;
1344
1345 default:
1346 goto bad_real;
1347 }
1348
1349 /* Get the digit string that might be a repeat count. */
1350
1351 for (;;)
1352 {
1353 c = next_char (dtp);
1354 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1355 c = '.';
1356 switch (c)
1357 {
1358 CASE_DIGITS:
1359 push_char (dtp, c);
1360 break;
1361
1362 case '.':
1363 if (seen_dp)
1364 goto bad_real;
1365
1366 seen_dp = 1;
1367 push_char (dtp, c);
1368 goto real_loop;
1369
1370 case 'E':
1371 case 'e':
1372 case 'D':
1373 case 'd':
1374 goto exp1;
1375
1376 case '+':
1377 case '-':
1378 push_char (dtp, 'e');
1379 push_char (dtp, c);
1380 c = next_char (dtp);
1381 goto exp2;
1382
1383 case '*':
1384 push_char (dtp, '\0');
1385 goto got_repeat;
1386
1387 CASE_SEPARATORS:
1388 if (c != '\n' && c != ',' && c != '\r' && c != ';')
1389 unget_char (dtp, c);
1390 goto done;
1391
1392 default:
1393 goto bad_real;
1394 }
1395 }
1396
1397 got_repeat:
1398 if (convert_integer (dtp, -1, 0))
1399 return;
1400
1401 /* Now get the number itself. */
1402
1403 c = next_char (dtp);
1404 if (is_separator (c))
1405 { /* Repeated null value. */
1406 unget_char (dtp, c);
1407 eat_separator (dtp);
1408 return;
1409 }
1410
1411 if (c != '-' && c != '+')
1412 push_char (dtp, '+');
1413 else
1414 {
1415 got_sign:
1416 push_char (dtp, c);
1417 c = next_char (dtp);
1418 }
1419
1420 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1421 c = '.';
1422
1423 if (!isdigit (c) && c != '.')
1424 {
1425 if (c == 'i' || c == 'I' || c == 'n' || c == 'N')
1426 goto inf_nan;
1427 else
1428 goto bad_real;
1429 }
1430
1431 if (c == '.')
1432 {
1433 if (seen_dp)
1434 goto bad_real;
1435 else
1436 seen_dp = 1;
1437 }
1438
1439 push_char (dtp, c);
1440
1441 real_loop:
1442 for (;;)
1443 {
1444 c = next_char (dtp);
1445 if (c == ',' && dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA)
1446 c = '.';
1447 switch (c)
1448 {
1449 CASE_DIGITS:
1450 push_char (dtp, c);
1451 break;
1452
1453 CASE_SEPARATORS:
1454 goto done;
1455
1456 case '.':
1457 if (seen_dp)
1458 goto bad_real;
1459
1460 seen_dp = 1;
1461 push_char (dtp, c);
1462 break;
1463
1464 case 'E':
1465 case 'e':
1466 case 'D':
1467 case 'd':
1468 goto exp1;
1469
1470 case '+':
1471 case '-':
1472 push_char (dtp, 'e');
1473 push_char (dtp, c);
1474 c = next_char (dtp);
1475 goto exp2;
1476
1477 default:
1478 goto bad_real;
1479 }
1480 }
1481
1482 exp1:
1483 push_char (dtp, 'e');
1484
1485 c = next_char (dtp);
1486 if (c != '+' && c != '-')
1487 push_char (dtp, '+');
1488 else
1489 {
1490 push_char (dtp, c);
1491 c = next_char (dtp);
1492 }
1493
1494 exp2:
1495 if (!isdigit (c))
1496 goto bad_real;
1497 push_char (dtp, c);
1498
1499 for (;;)
1500 {
1501 c = next_char (dtp);
1502
1503 switch (c)
1504 {
1505 CASE_DIGITS:
1506 push_char (dtp, c);
1507 break;
1508
1509 CASE_SEPARATORS:
1510 goto done;
1511
1512 default:
1513 goto bad_real;
1514 }
1515 }
1516
1517 done:
1518 unget_char (dtp, c);
1519 eat_separator (dtp);
1520 push_char (dtp, '\0');
1521 if (convert_real (dtp, dtp->u.p.value, dtp->u.p.saved_string, length))
1522 return;
1523
1524 free_saved (dtp);
1525 dtp->u.p.saved_type = BT_REAL;
1526 return;
1527
1528 inf_nan:
1529 l_push_char (dtp, c);
1530 is_inf = 0;
1531
1532 /* Match INF and Infinity. */
1533 if (c == 'i' || c == 'I')
1534 {
1535 c = next_char (dtp);
1536 l_push_char (dtp, c);
1537 if (c != 'n' && c != 'N')
1538 goto unwind;
1539 c = next_char (dtp);
1540 l_push_char (dtp, c);
1541 if (c != 'f' && c != 'F')
1542 goto unwind;
1543 c = next_char (dtp);
1544 l_push_char (dtp, c);
1545 if (!is_separator (c))
1546 {
1547 if (c != 'i' && c != 'I')
1548 goto unwind;
1549 c = next_char (dtp);
1550 l_push_char (dtp, c);
1551 if (c != 'n' && c != 'N')
1552 goto unwind;
1553 c = next_char (dtp);
1554 l_push_char (dtp, c);
1555 if (c != 'i' && c != 'I')
1556 goto unwind;
1557 c = next_char (dtp);
1558 l_push_char (dtp, c);
1559 if (c != 't' && c != 'T')
1560 goto unwind;
1561 c = next_char (dtp);
1562 l_push_char (dtp, c);
1563 if (c != 'y' && c != 'Y')
1564 goto unwind;
1565 c = next_char (dtp);
1566 l_push_char (dtp, c);
1567 }
1568 is_inf = 1;
1569 } /* Match NaN. */
1570 else
1571 {
1572 c = next_char (dtp);
1573 l_push_char (dtp, c);
1574 if (c != 'a' && c != 'A')
1575 goto unwind;
1576 c = next_char (dtp);
1577 l_push_char (dtp, c);
1578 if (c != 'n' && c != 'N')
1579 goto unwind;
1580 c = next_char (dtp);
1581 l_push_char (dtp, c);
1582 }
1583
1584 if (!is_separator (c))
1585 goto unwind;
1586
1587 if (dtp->u.p.namelist_mode)
1588 {
1589 if (c == ' ' || c =='\n' || c == '\r')
1590 {
1591 do
1592 c = next_char (dtp);
1593 while (c == ' ' || c =='\n' || c == '\r');
1594
1595 l_push_char (dtp, c);
1596
1597 if (c == '=')
1598 goto unwind;
1599 }
1600 }
1601
1602 if (is_inf)
1603 {
1604 push_char (dtp, 'i');
1605 push_char (dtp, 'n');
1606 push_char (dtp, 'f');
1607 }
1608 else
1609 {
1610 push_char (dtp, 'n');
1611 push_char (dtp, 'a');
1612 push_char (dtp, 'n');
1613 }
1614
1615 free_line (dtp);
1616 goto done;
1617
1618 unwind:
1619 if (dtp->u.p.namelist_mode)
1620 {
1621 dtp->u.p.nml_read_error = 1;
1622 dtp->u.p.line_buffer_enabled = 1;
1623 dtp->u.p.item_count = 0;
1624 return;
1625 }
1626
1627 bad_real:
1628
1629 if (nml_bad_return (dtp, c))
1630 return;
1631
1632 eat_line (dtp);
1633 free_saved (dtp);
1634 sprintf (message, "Bad real number in item %d of list input",
1635 dtp->u.p.item_count);
1636 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1637 }
1638
1639
1640 /* Check the current type against the saved type to make sure they are
1641 compatible. Returns nonzero if incompatible. */
1642
1643 static int
1644 check_type (st_parameter_dt *dtp, bt type, int len)
1645 {
1646 char message[100];
1647
1648 if (dtp->u.p.saved_type != BT_NULL && dtp->u.p.saved_type != type)
1649 {
1650 sprintf (message, "Read type %s where %s was expected for item %d",
1651 type_name (dtp->u.p.saved_type), type_name (type),
1652 dtp->u.p.item_count);
1653
1654 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1655 return 1;
1656 }
1657
1658 if (dtp->u.p.saved_type == BT_NULL || dtp->u.p.saved_type == BT_CHARACTER)
1659 return 0;
1660
1661 if (dtp->u.p.saved_length != len)
1662 {
1663 sprintf (message,
1664 "Read kind %d %s where kind %d is required for item %d",
1665 dtp->u.p.saved_length, type_name (dtp->u.p.saved_type), len,
1666 dtp->u.p.item_count);
1667 generate_error (&dtp->common, LIBERROR_READ_VALUE, message);
1668 return 1;
1669 }
1670
1671 return 0;
1672 }
1673
1674
1675 /* Top level data transfer subroutine for list reads. Because we have
1676 to deal with repeat counts, the data item is always saved after
1677 reading, usually in the dtp->u.p.value[] array. If a repeat count is
1678 greater than one, we copy the data item multiple times. */
1679
1680 static void
1681 list_formatted_read_scalar (st_parameter_dt *dtp, volatile bt type, void *p,
1682 int kind, size_t size)
1683 {
1684 char c;
1685 gfc_char4_t *q;
1686 int i, m;
1687 jmp_buf eof_jump;
1688
1689 dtp->u.p.namelist_mode = 0;
1690
1691 dtp->u.p.eof_jump = &eof_jump;
1692 if (setjmp (eof_jump))
1693 {
1694 generate_error (&dtp->common, LIBERROR_END, NULL);
1695 goto cleanup;
1696 }
1697
1698 if (dtp->u.p.first_item)
1699 {
1700 dtp->u.p.first_item = 0;
1701 dtp->u.p.input_complete = 0;
1702 dtp->u.p.repeat_count = 1;
1703 dtp->u.p.at_eol = 0;
1704
1705 c = eat_spaces (dtp);
1706 if (is_separator (c))
1707 {
1708 /* Found a null value. */
1709 eat_separator (dtp);
1710 dtp->u.p.repeat_count = 0;
1711
1712 /* eat_separator sets this flag if the separator was a comma. */
1713 if (dtp->u.p.comma_flag)
1714 goto cleanup;
1715
1716 /* eat_separator sets this flag if the separator was a \n or \r. */
1717 if (dtp->u.p.at_eol)
1718 finish_separator (dtp);
1719 else
1720 goto cleanup;
1721 }
1722
1723 }
1724 else
1725 {
1726 if (dtp->u.p.repeat_count > 0)
1727 {
1728 if (check_type (dtp, type, kind))
1729 return;
1730 goto set_value;
1731 }
1732
1733 if (dtp->u.p.input_complete)
1734 goto cleanup;
1735
1736 if (dtp->u.p.input_complete)
1737 goto cleanup;
1738
1739 if (dtp->u.p.at_eol)
1740 finish_separator (dtp);
1741 else
1742 {
1743 eat_spaces (dtp);
1744 /* Trailing spaces prior to end of line. */
1745 if (dtp->u.p.at_eol)
1746 finish_separator (dtp);
1747 }
1748
1749 dtp->u.p.saved_type = BT_NULL;
1750 dtp->u.p.repeat_count = 1;
1751 }
1752
1753 switch (type)
1754 {
1755 case BT_INTEGER:
1756 read_integer (dtp, kind);
1757 break;
1758 case BT_LOGICAL:
1759 read_logical (dtp, kind);
1760 break;
1761 case BT_CHARACTER:
1762 read_character (dtp, kind);
1763 break;
1764 case BT_REAL:
1765 read_real (dtp, kind);
1766 break;
1767 case BT_COMPLEX:
1768 read_complex (dtp, kind, size);
1769 break;
1770 default:
1771 internal_error (&dtp->common, "Bad type for list read");
1772 }
1773
1774 if (dtp->u.p.saved_type != BT_CHARACTER && dtp->u.p.saved_type != BT_NULL)
1775 dtp->u.p.saved_length = size;
1776
1777 if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK)
1778 goto cleanup;
1779
1780 set_value:
1781 switch (dtp->u.p.saved_type)
1782 {
1783 case BT_COMPLEX:
1784 case BT_INTEGER:
1785 case BT_REAL:
1786 case BT_LOGICAL:
1787 memcpy (p, dtp->u.p.value, size);
1788 break;
1789
1790 case BT_CHARACTER:
1791 if (dtp->u.p.saved_string)
1792 {
1793 m = ((int) size < dtp->u.p.saved_used)
1794 ? (int) size : dtp->u.p.saved_used;
1795 if (kind == 1)
1796 memcpy (p, dtp->u.p.saved_string, m);
1797 else
1798 {
1799 q = (gfc_char4_t *) p;
1800 for (i = 0; i < m; i++)
1801 q[i] = (unsigned char) dtp->u.p.saved_string[i];
1802 }
1803 }
1804 else
1805 /* Just delimiters encountered, nothing to copy but SPACE. */
1806 m = 0;
1807
1808 if (m < (int) size)
1809 {
1810 if (kind == 1)
1811 memset (((char *) p) + m, ' ', size - m);
1812 else
1813 {
1814 q = (gfc_char4_t *) p;
1815 for (i = m; i < (int) size; i++)
1816 q[i] = (unsigned char) ' ';
1817 }
1818 }
1819 break;
1820
1821 case BT_NULL:
1822 break;
1823 }
1824
1825 if (--dtp->u.p.repeat_count <= 0)
1826 free_saved (dtp);
1827
1828 cleanup:
1829 dtp->u.p.eof_jump = NULL;
1830 }
1831
1832
1833 void
1834 list_formatted_read (st_parameter_dt *dtp, bt type, void *p, int kind,
1835 size_t size, size_t nelems)
1836 {
1837 size_t elem;
1838 char *tmp;
1839 size_t stride = type == BT_CHARACTER ?
1840 size * GFC_SIZE_OF_CHAR_KIND(kind) : size;
1841
1842 tmp = (char *) p;
1843
1844 /* Big loop over all the elements. */
1845 for (elem = 0; elem < nelems; elem++)
1846 {
1847 dtp->u.p.item_count++;
1848 list_formatted_read_scalar (dtp, type, tmp + stride*elem, kind, size);
1849 }
1850 }
1851
1852
1853 /* Finish a list read. */
1854
1855 void
1856 finish_list_read (st_parameter_dt *dtp)
1857 {
1858 char c;
1859
1860 free_saved (dtp);
1861
1862 fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
1863
1864 if (dtp->u.p.at_eol)
1865 {
1866 dtp->u.p.at_eol = 0;
1867 return;
1868 }
1869
1870 do
1871 {
1872 c = next_char (dtp);
1873 }
1874 while (c != '\n');
1875
1876 if (dtp->u.p.current_unit->endfile != NO_ENDFILE)
1877 {
1878 generate_error (&dtp->common, LIBERROR_END, NULL);
1879 dtp->u.p.current_unit->endfile = AFTER_ENDFILE;
1880 dtp->u.p.current_unit->current_record = 0;
1881 }
1882 }
1883
1884 /* NAMELIST INPUT
1885
1886 void namelist_read (st_parameter_dt *dtp)
1887 calls:
1888 static void nml_match_name (char *name, int len)
1889 static int nml_query (st_parameter_dt *dtp)
1890 static int nml_get_obj_data (st_parameter_dt *dtp,
1891 namelist_info **prev_nl, char *, size_t)
1892 calls:
1893 static void nml_untouch_nodes (st_parameter_dt *dtp)
1894 static namelist_info * find_nml_node (st_parameter_dt *dtp,
1895 char * var_name)
1896 static int nml_parse_qualifier(descriptor_dimension * ad,
1897 array_loop_spec * ls, int rank, char *)
1898 static void nml_touch_nodes (namelist_info * nl)
1899 static int nml_read_obj (namelist_info *nl, index_type offset,
1900 namelist_info **prev_nl, char *, size_t,
1901 index_type clow, index_type chigh)
1902 calls:
1903 -itself- */
1904
1905 /* Inputs a rank-dimensional qualifier, which can contain
1906 singlets, doublets, triplets or ':' with the standard meanings. */
1907
1908 static try
1909 nml_parse_qualifier (st_parameter_dt *dtp, descriptor_dimension *ad,
1910 array_loop_spec *ls, int rank, char *parse_err_msg,
1911 int *parsed_rank)
1912 {
1913 int dim;
1914 int indx;
1915 int neg;
1916 int null_flag;
1917 int is_array_section, is_char;
1918 char c;
1919
1920 is_char = 0;
1921 is_array_section = 0;
1922 dtp->u.p.expanded_read = 0;
1923
1924 /* See if this is a character substring qualifier we are looking for. */
1925 if (rank == -1)
1926 {
1927 rank = 1;
1928 is_char = 1;
1929 }
1930
1931 /* The next character in the stream should be the '('. */
1932
1933 c = next_char (dtp);
1934
1935 /* Process the qualifier, by dimension and triplet. */
1936
1937 for (dim=0; dim < rank; dim++ )
1938 {
1939 for (indx=0; indx<3; indx++)
1940 {
1941 free_saved (dtp);
1942 eat_spaces (dtp);
1943 neg = 0;
1944
1945 /* Process a potential sign. */
1946 c = next_char (dtp);
1947 switch (c)
1948 {
1949 case '-':
1950 neg = 1;
1951 break;
1952
1953 case '+':
1954 break;
1955
1956 default:
1957 unget_char (dtp, c);
1958 break;
1959 }
1960
1961 /* Process characters up to the next ':' , ',' or ')'. */
1962 for (;;)
1963 {
1964 c = next_char (dtp);
1965
1966 switch (c)
1967 {
1968 case ':':
1969 is_array_section = 1;
1970 break;
1971
1972 case ',': case ')':
1973 if ((c==',' && dim == rank -1)
1974 || (c==')' && dim < rank -1))
1975 {
1976 if (is_char)
1977 sprintf (parse_err_msg, "Bad substring qualifier");
1978 else
1979 sprintf (parse_err_msg, "Bad number of index fields");
1980 goto err_ret;
1981 }
1982 break;
1983
1984 CASE_DIGITS:
1985 push_char (dtp, c);
1986 continue;
1987
1988 case ' ': case '\t':
1989 eat_spaces (dtp);
1990 c = next_char (dtp);
1991 break;
1992
1993 default:
1994 if (is_char)
1995 sprintf (parse_err_msg,
1996 "Bad character in substring qualifier");
1997 else
1998 sprintf (parse_err_msg, "Bad character in index");
1999 goto err_ret;
2000 }
2001
2002 if ((c == ',' || c == ')') && indx == 0
2003 && dtp->u.p.saved_string == 0)
2004 {
2005 if (is_char)
2006 sprintf (parse_err_msg, "Null substring qualifier");
2007 else
2008 sprintf (parse_err_msg, "Null index field");
2009 goto err_ret;
2010 }
2011
2012 if ((c == ':' && indx == 1 && dtp->u.p.saved_string == 0)
2013 || (indx == 2 && dtp->u.p.saved_string == 0))
2014 {
2015 if (is_char)
2016 sprintf (parse_err_msg, "Bad substring qualifier");
2017 else
2018 sprintf (parse_err_msg, "Bad index triplet");
2019 goto err_ret;
2020 }
2021
2022 if (is_char && !is_array_section)
2023 {
2024 sprintf (parse_err_msg,
2025 "Missing colon in substring qualifier");
2026 goto err_ret;
2027 }
2028
2029 /* If '( : ? )' or '( ? : )' break and flag read failure. */
2030 null_flag = 0;
2031 if ((c == ':' && indx == 0 && dtp->u.p.saved_string == 0)
2032 || (indx==1 && dtp->u.p.saved_string == 0))
2033 {
2034 null_flag = 1;
2035 break;
2036 }
2037
2038 /* Now read the index. */
2039 if (convert_integer (dtp, sizeof(ssize_t), neg))
2040 {
2041 if (is_char)
2042 sprintf (parse_err_msg, "Bad integer substring qualifier");
2043 else
2044 sprintf (parse_err_msg, "Bad integer in index");
2045 goto err_ret;
2046 }
2047 break;
2048 }
2049
2050 /* Feed the index values to the triplet arrays. */
2051 if (!null_flag)
2052 {
2053 if (indx == 0)
2054 memcpy (&ls[dim].start, dtp->u.p.value, sizeof(ssize_t));
2055 if (indx == 1)
2056 memcpy (&ls[dim].end, dtp->u.p.value, sizeof(ssize_t));
2057 if (indx == 2)
2058 memcpy (&ls[dim].step, dtp->u.p.value, sizeof(ssize_t));
2059 }
2060
2061 /* Singlet or doublet indices. */
2062 if (c==',' || c==')')
2063 {
2064 if (indx == 0)
2065 {
2066 memcpy (&ls[dim].start, dtp->u.p.value, sizeof(ssize_t));
2067
2068 /* If -std=f95/2003 or an array section is specified,
2069 do not allow excess data to be processed. */
2070 if (is_array_section == 1
2071 || compile_options.allow_std < GFC_STD_GNU)
2072 ls[dim].end = ls[dim].start;
2073 else
2074 dtp->u.p.expanded_read = 1;
2075 }
2076
2077 /* Check for non-zero rank. */
2078 if (is_array_section == 1 && ls[dim].start != ls[dim].end)
2079 *parsed_rank = 1;
2080
2081 break;
2082 }
2083 }
2084
2085 /* Check the values of the triplet indices. */
2086 if ((ls[dim].start > (ssize_t)ad[dim].ubound)
2087 || (ls[dim].start < (ssize_t)ad[dim].lbound)
2088 || (ls[dim].end > (ssize_t)ad[dim].ubound)
2089 || (ls[dim].end < (ssize_t)ad[dim].lbound))
2090 {
2091 if (is_char)
2092 sprintf (parse_err_msg, "Substring out of range");
2093 else
2094 sprintf (parse_err_msg, "Index %d out of range", dim + 1);
2095 goto err_ret;
2096 }
2097
2098 if (((ls[dim].end - ls[dim].start ) * ls[dim].step < 0)
2099 || (ls[dim].step == 0))
2100 {
2101 sprintf (parse_err_msg, "Bad range in index %d", dim + 1);
2102 goto err_ret;
2103 }
2104
2105 /* Initialise the loop index counter. */
2106 ls[dim].idx = ls[dim].start;
2107 }
2108 eat_spaces (dtp);
2109 return SUCCESS;
2110
2111 err_ret:
2112
2113 return FAILURE;
2114 }
2115
2116 static namelist_info *
2117 find_nml_node (st_parameter_dt *dtp, char * var_name)
2118 {
2119 namelist_info * t = dtp->u.p.ionml;
2120 while (t != NULL)
2121 {
2122 if (strcmp (var_name, t->var_name) == 0)
2123 {
2124 t->touched = 1;
2125 return t;
2126 }
2127 t = t->next;
2128 }
2129 return NULL;
2130 }
2131
2132 /* Visits all the components of a derived type that have
2133 not explicitly been identified in the namelist input.
2134 touched is set and the loop specification initialised
2135 to default values */
2136
2137 static void
2138 nml_touch_nodes (namelist_info * nl)
2139 {
2140 index_type len = strlen (nl->var_name) + 1;
2141 int dim;
2142 char * ext_name = (char*)get_mem (len + 1);
2143 memcpy (ext_name, nl->var_name, len-1);
2144 memcpy (ext_name + len - 1, "%", 2);
2145 for (nl = nl->next; nl; nl = nl->next)
2146 {
2147 if (strncmp (nl->var_name, ext_name, len) == 0)
2148 {
2149 nl->touched = 1;
2150 for (dim=0; dim < nl->var_rank; dim++)
2151 {
2152 nl->ls[dim].step = 1;
2153 nl->ls[dim].end = nl->dim[dim].ubound;
2154 nl->ls[dim].start = nl->dim[dim].lbound;
2155 nl->ls[dim].idx = nl->ls[dim].start;
2156 }
2157 }
2158 else
2159 break;
2160 }
2161 free_mem (ext_name);
2162 return;
2163 }
2164
2165 /* Resets touched for the entire list of nml_nodes, ready for a
2166 new object. */
2167
2168 static void
2169 nml_untouch_nodes (st_parameter_dt *dtp)
2170 {
2171 namelist_info * t;
2172 for (t = dtp->u.p.ionml; t; t = t->next)
2173 t->touched = 0;
2174 return;
2175 }
2176
2177 /* Attempts to input name to namelist name. Returns
2178 dtp->u.p.nml_read_error = 1 on no match. */
2179
2180 static void
2181 nml_match_name (st_parameter_dt *dtp, const char *name, index_type len)
2182 {
2183 index_type i;
2184 char c;
2185 dtp->u.p.nml_read_error = 0;
2186 for (i = 0; i < len; i++)
2187 {
2188 c = next_char (dtp);
2189 if (tolower (c) != tolower (name[i]))
2190 {
2191 dtp->u.p.nml_read_error = 1;
2192 break;
2193 }
2194 }
2195 }
2196
2197 /* If the namelist read is from stdin, output the current state of the
2198 namelist to stdout. This is used to implement the non-standard query
2199 features, ? and =?. If c == '=' the full namelist is printed. Otherwise
2200 the names alone are printed. */
2201
2202 static void
2203 nml_query (st_parameter_dt *dtp, char c)
2204 {
2205 gfc_unit * temp_unit;
2206 namelist_info * nl;
2207 index_type len;
2208 char * p;
2209 #ifdef HAVE_CRLF
2210 static const index_type endlen = 3;
2211 static const char endl[] = "\r\n";
2212 static const char nmlend[] = "&end\r\n";
2213 #else
2214 static const index_type endlen = 2;
2215 static const char endl[] = "\n";
2216 static const char nmlend[] = "&end\n";
2217 #endif
2218
2219 if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
2220 return;
2221
2222 /* Store the current unit and transfer to stdout. */
2223
2224 temp_unit = dtp->u.p.current_unit;
2225 dtp->u.p.current_unit = find_unit (options.stdout_unit);
2226
2227 if (dtp->u.p.current_unit)
2228 {
2229 dtp->u.p.mode = WRITING;
2230 next_record (dtp, 0);
2231
2232 /* Write the namelist in its entirety. */
2233
2234 if (c == '=')
2235 namelist_write (dtp);
2236
2237 /* Or write the list of names. */
2238
2239 else
2240 {
2241 /* "&namelist_name\n" */
2242
2243 len = dtp->namelist_name_len;
2244 p = write_block (dtp, len + endlen);
2245 if (!p)
2246 goto query_return;
2247 memcpy (p, "&", 1);
2248 memcpy ((char*)(p + 1), dtp->namelist_name, len);
2249 memcpy ((char*)(p + len + 1), &endl, endlen - 1);
2250 for (nl = dtp->u.p.ionml; nl; nl = nl->next)
2251 {
2252 /* " var_name\n" */
2253
2254 len = strlen (nl->var_name);
2255 p = write_block (dtp, len + endlen);
2256 if (!p)
2257 goto query_return;
2258 memcpy (p, " ", 1);
2259 memcpy ((char*)(p + 1), nl->var_name, len);
2260 memcpy ((char*)(p + len + 1), &endl, endlen - 1);
2261 }
2262
2263 /* "&end\n" */
2264
2265 p = write_block (dtp, endlen + 3);
2266 goto query_return;
2267 memcpy (p, &nmlend, endlen + 3);
2268 }
2269
2270 /* Flush the stream to force immediate output. */
2271
2272 fbuf_flush (dtp->u.p.current_unit, WRITING);
2273 sflush (dtp->u.p.current_unit->s);
2274 unlock_unit (dtp->u.p.current_unit);
2275 }
2276
2277 query_return:
2278
2279 /* Restore the current unit. */
2280
2281 dtp->u.p.current_unit = temp_unit;
2282 dtp->u.p.mode = READING;
2283 return;
2284 }
2285
2286 /* Reads and stores the input for the namelist object nl. For an array,
2287 the function loops over the ranges defined by the loop specification.
2288 This default to all the data or to the specification from a qualifier.
2289 nml_read_obj recursively calls itself to read derived types. It visits
2290 all its own components but only reads data for those that were touched
2291 when the name was parsed. If a read error is encountered, an attempt is
2292 made to return to read a new object name because the standard allows too
2293 little data to be available. On the other hand, too much data is an
2294 error. */
2295
2296 static try
2297 nml_read_obj (st_parameter_dt *dtp, namelist_info * nl, index_type offset,
2298 namelist_info **pprev_nl, char *nml_err_msg,
2299 size_t nml_err_msg_size, index_type clow, index_type chigh)
2300 {
2301 namelist_info * cmp;
2302 char * obj_name;
2303 int nml_carry;
2304 int len;
2305 int dim;
2306 index_type dlen;
2307 index_type m;
2308 size_t obj_name_len;
2309 void * pdata;
2310
2311 /* This object not touched in name parsing. */
2312
2313 if (!nl->touched)
2314 return SUCCESS;
2315
2316 dtp->u.p.repeat_count = 0;
2317 eat_spaces (dtp);
2318
2319 len = nl->len;
2320 switch (nl->type)
2321 {
2322 case GFC_DTYPE_INTEGER:
2323 case GFC_DTYPE_LOGICAL:
2324 dlen = len;
2325 break;
2326
2327 case GFC_DTYPE_REAL:
2328 dlen = size_from_real_kind (len);
2329 break;
2330
2331 case GFC_DTYPE_COMPLEX:
2332 dlen = size_from_complex_kind (len);
2333 break;
2334
2335 case GFC_DTYPE_CHARACTER:
2336 dlen = chigh ? (chigh - clow + 1) : nl->string_length;
2337 break;
2338
2339 default:
2340 dlen = 0;
2341 }
2342
2343 do
2344 {
2345 /* Update the pointer to the data, using the current index vector */
2346
2347 pdata = (void*)(nl->mem_pos + offset);
2348 for (dim = 0; dim < nl->var_rank; dim++)
2349 pdata = (void*)(pdata + (nl->ls[dim].idx - nl->dim[dim].lbound) *
2350 nl->dim[dim].stride * nl->size);
2351
2352 /* Reset the error flag and try to read next value, if
2353 dtp->u.p.repeat_count=0 */
2354
2355 dtp->u.p.nml_read_error = 0;
2356 nml_carry = 0;
2357 if (--dtp->u.p.repeat_count <= 0)
2358 {
2359 if (dtp->u.p.input_complete)
2360 return SUCCESS;
2361 if (dtp->u.p.at_eol)
2362 finish_separator (dtp);
2363 if (dtp->u.p.input_complete)
2364 return SUCCESS;
2365
2366 /* GFC_TYPE_UNKNOWN through for nulls and is detected
2367 after the switch block. */
2368
2369 dtp->u.p.saved_type = GFC_DTYPE_UNKNOWN;
2370 free_saved (dtp);
2371
2372 switch (nl->type)
2373 {
2374 case GFC_DTYPE_INTEGER:
2375 read_integer (dtp, len);
2376 break;
2377
2378 case GFC_DTYPE_LOGICAL:
2379 read_logical (dtp, len);
2380 break;
2381
2382 case GFC_DTYPE_CHARACTER:
2383 read_character (dtp, len);
2384 break;
2385
2386 case GFC_DTYPE_REAL:
2387 read_real (dtp, len);
2388 break;
2389
2390 case GFC_DTYPE_COMPLEX:
2391 read_complex (dtp, len, dlen);
2392 break;
2393
2394 case GFC_DTYPE_DERIVED:
2395 obj_name_len = strlen (nl->var_name) + 1;
2396 obj_name = get_mem (obj_name_len+1);
2397 memcpy (obj_name, nl->var_name, obj_name_len-1);
2398 memcpy (obj_name + obj_name_len - 1, "%", 2);
2399
2400 /* If reading a derived type, disable the expanded read warning
2401 since a single object can have multiple reads. */
2402 dtp->u.p.expanded_read = 0;
2403
2404 /* Now loop over the components. Update the component pointer
2405 with the return value from nml_write_obj. This loop jumps
2406 past nested derived types by testing if the potential
2407 component name contains '%'. */
2408
2409 for (cmp = nl->next;
2410 cmp &&
2411 !strncmp (cmp->var_name, obj_name, obj_name_len) &&
2412 !strchr (cmp->var_name + obj_name_len, '%');
2413 cmp = cmp->next)
2414 {
2415
2416 if (nml_read_obj (dtp, cmp, (index_type)(pdata - nl->mem_pos),
2417 pprev_nl, nml_err_msg, nml_err_msg_size,
2418 clow, chigh) == FAILURE)
2419 {
2420 free_mem (obj_name);
2421 return FAILURE;
2422 }
2423
2424 if (dtp->u.p.input_complete)
2425 {
2426 free_mem (obj_name);
2427 return SUCCESS;
2428 }
2429 }
2430
2431 free_mem (obj_name);
2432 goto incr_idx;
2433
2434 default:
2435 snprintf (nml_err_msg, nml_err_msg_size,
2436 "Bad type for namelist object %s", nl->var_name);
2437 internal_error (&dtp->common, nml_err_msg);
2438 goto nml_err_ret;
2439 }
2440 }
2441
2442 /* The standard permits array data to stop short of the number of
2443 elements specified in the loop specification. In this case, we
2444 should be here with dtp->u.p.nml_read_error != 0. Control returns to
2445 nml_get_obj_data and an attempt is made to read object name. */
2446
2447 *pprev_nl = nl;
2448 if (dtp->u.p.nml_read_error)
2449 {
2450 dtp->u.p.expanded_read = 0;
2451 return SUCCESS;
2452 }
2453
2454 if (dtp->u.p.saved_type == GFC_DTYPE_UNKNOWN)
2455 {
2456 dtp->u.p.expanded_read = 0;
2457 goto incr_idx;
2458 }
2459
2460 /* Note the switch from GFC_DTYPE_type to BT_type at this point.
2461 This comes about because the read functions return BT_types. */
2462
2463 switch (dtp->u.p.saved_type)
2464 {
2465
2466 case BT_COMPLEX:
2467 case BT_REAL:
2468 case BT_INTEGER:
2469 case BT_LOGICAL:
2470 memcpy (pdata, dtp->u.p.value, dlen);
2471 break;
2472
2473 case BT_CHARACTER:
2474 m = (dlen < dtp->u.p.saved_used) ? dlen : dtp->u.p.saved_used;
2475 pdata = (void*)( pdata + clow - 1 );
2476 memcpy (pdata, dtp->u.p.saved_string, m);
2477 if (m < dlen)
2478 memset ((void*)( pdata + m ), ' ', dlen - m);
2479 break;
2480
2481 default:
2482 break;
2483 }
2484
2485 /* Warn if a non-standard expanded read occurs. A single read of a
2486 single object is acceptable. If a second read occurs, issue a warning
2487 and set the flag to zero to prevent further warnings. */
2488 if (dtp->u.p.expanded_read == 2)
2489 {
2490 notify_std (&dtp->common, GFC_STD_GNU, "Non-standard expanded namelist read.");
2491 dtp->u.p.expanded_read = 0;
2492 }
2493
2494 /* If the expanded read warning flag is set, increment it,
2495 indicating that a single read has occurred. */
2496 if (dtp->u.p.expanded_read >= 1)
2497 dtp->u.p.expanded_read++;
2498
2499 /* Break out of loop if scalar. */
2500 if (!nl->var_rank)
2501 break;
2502
2503 /* Now increment the index vector. */
2504
2505 incr_idx:
2506
2507 nml_carry = 1;
2508 for (dim = 0; dim < nl->var_rank; dim++)
2509 {
2510 nl->ls[dim].idx += nml_carry * nl->ls[dim].step;
2511 nml_carry = 0;
2512 if (((nl->ls[dim].step > 0) && (nl->ls[dim].idx > nl->ls[dim].end))
2513 ||
2514 ((nl->ls[dim].step < 0) && (nl->ls[dim].idx < nl->ls[dim].end)))
2515 {
2516 nl->ls[dim].idx = nl->ls[dim].start;
2517 nml_carry = 1;
2518 }
2519 }
2520 } while (!nml_carry);
2521
2522 if (dtp->u.p.repeat_count > 1)
2523 {
2524 snprintf (nml_err_msg, nml_err_msg_size,
2525 "Repeat count too large for namelist object %s", nl->var_name);
2526 goto nml_err_ret;
2527 }
2528 return SUCCESS;
2529
2530 nml_err_ret:
2531
2532 return FAILURE;
2533 }
2534
2535 /* Parses the object name, including array and substring qualifiers. It
2536 iterates over derived type components, touching those components and
2537 setting their loop specifications, if there is a qualifier. If the
2538 object is itself a derived type, its components and subcomponents are
2539 touched. nml_read_obj is called at the end and this reads the data in
2540 the manner specified by the object name. */
2541
2542 static try
2543 nml_get_obj_data (st_parameter_dt *dtp, namelist_info **pprev_nl,
2544 char *nml_err_msg, size_t nml_err_msg_size)
2545 {
2546 char c;
2547 namelist_info * nl;
2548 namelist_info * first_nl = NULL;
2549 namelist_info * root_nl = NULL;
2550 int dim, parsed_rank;
2551 int component_flag;
2552 index_type clow, chigh;
2553 int non_zero_rank_count;
2554
2555 /* Look for end of input or object name. If '?' or '=?' are encountered
2556 in stdin, print the node names or the namelist to stdout. */
2557
2558 eat_separator (dtp);
2559 if (dtp->u.p.input_complete)
2560 return SUCCESS;
2561
2562 if (dtp->u.p.at_eol)
2563 finish_separator (dtp);
2564 if (dtp->u.p.input_complete)
2565 return SUCCESS;
2566
2567 c = next_char (dtp);
2568 switch (c)
2569 {
2570 case '=':
2571 c = next_char (dtp);
2572 if (c != '?')
2573 {
2574 sprintf (nml_err_msg, "namelist read: misplaced = sign");
2575 goto nml_err_ret;
2576 }
2577 nml_query (dtp, '=');
2578 return SUCCESS;
2579
2580 case '?':
2581 nml_query (dtp, '?');
2582 return SUCCESS;
2583
2584 case '$':
2585 case '&':
2586 nml_match_name (dtp, "end", 3);
2587 if (dtp->u.p.nml_read_error)
2588 {
2589 sprintf (nml_err_msg, "namelist not terminated with / or &end");
2590 goto nml_err_ret;
2591 }
2592 case '/':
2593 dtp->u.p.input_complete = 1;
2594 return SUCCESS;
2595
2596 default :
2597 break;
2598 }
2599
2600 /* Untouch all nodes of the namelist and reset the flag that is set for
2601 derived type components. */
2602
2603 nml_untouch_nodes (dtp);
2604 component_flag = 0;
2605 non_zero_rank_count = 0;
2606
2607 /* Get the object name - should '!' and '\n' be permitted separators? */
2608
2609 get_name:
2610
2611 free_saved (dtp);
2612
2613 do
2614 {
2615 if (!is_separator (c))
2616 push_char (dtp, tolower(c));
2617 c = next_char (dtp);
2618 } while (!( c=='=' || c==' ' || c=='\t' || c =='(' || c =='%' ));
2619
2620 unget_char (dtp, c);
2621
2622 /* Check that the name is in the namelist and get pointer to object.
2623 Three error conditions exist: (i) An attempt is being made to
2624 identify a non-existent object, following a failed data read or
2625 (ii) The object name does not exist or (iii) Too many data items
2626 are present for an object. (iii) gives the same error message
2627 as (i) */
2628
2629 push_char (dtp, '\0');
2630
2631 if (component_flag)
2632 {
2633 size_t var_len = strlen (root_nl->var_name);
2634 size_t saved_len
2635 = dtp->u.p.saved_string ? strlen (dtp->u.p.saved_string) : 0;
2636 char ext_name[var_len + saved_len + 1];
2637
2638 memcpy (ext_name, root_nl->var_name, var_len);
2639 if (dtp->u.p.saved_string)
2640 memcpy (ext_name + var_len, dtp->u.p.saved_string, saved_len);
2641 ext_name[var_len + saved_len] = '\0';
2642 nl = find_nml_node (dtp, ext_name);
2643 }
2644 else
2645 nl = find_nml_node (dtp, dtp->u.p.saved_string);
2646
2647 if (nl == NULL)
2648 {
2649 if (dtp->u.p.nml_read_error && *pprev_nl)
2650 snprintf (nml_err_msg, nml_err_msg_size,
2651 "Bad data for namelist object %s", (*pprev_nl)->var_name);
2652
2653 else
2654 snprintf (nml_err_msg, nml_err_msg_size,
2655 "Cannot match namelist object name %s",
2656 dtp->u.p.saved_string);
2657
2658 goto nml_err_ret;
2659 }
2660
2661 /* Get the length, data length, base pointer and rank of the variable.
2662 Set the default loop specification first. */
2663
2664 for (dim=0; dim < nl->var_rank; dim++)
2665 {
2666 nl->ls[dim].step = 1;
2667 nl->ls[dim].end = nl->dim[dim].ubound;
2668 nl->ls[dim].start = nl->dim[dim].lbound;
2669 nl->ls[dim].idx = nl->ls[dim].start;
2670 }
2671
2672 /* Check to see if there is a qualifier: if so, parse it.*/
2673
2674 if (c == '(' && nl->var_rank)
2675 {
2676 parsed_rank = 0;
2677 if (nml_parse_qualifier (dtp, nl->dim, nl->ls, nl->var_rank,
2678 nml_err_msg, &parsed_rank) == FAILURE)
2679 {
2680 char *nml_err_msg_end = strchr (nml_err_msg, '\0');
2681 snprintf (nml_err_msg_end,
2682 nml_err_msg_size - (nml_err_msg_end - nml_err_msg),
2683 " for namelist variable %s", nl->var_name);
2684 goto nml_err_ret;
2685 }
2686
2687 if (parsed_rank > 0)
2688 non_zero_rank_count++;
2689
2690 c = next_char (dtp);
2691 unget_char (dtp, c);
2692 }
2693 else if (nl->var_rank > 0)
2694 non_zero_rank_count++;
2695
2696 /* Now parse a derived type component. The root namelist_info address
2697 is backed up, as is the previous component level. The component flag
2698 is set and the iteration is made by jumping back to get_name. */
2699
2700 if (c == '%')
2701 {
2702 if (nl->type != GFC_DTYPE_DERIVED)
2703 {
2704 snprintf (nml_err_msg, nml_err_msg_size,
2705 "Attempt to get derived component for %s", nl->var_name);
2706 goto nml_err_ret;
2707 }
2708
2709 if (!component_flag)
2710 first_nl = nl;
2711
2712 root_nl = nl;
2713 component_flag = 1;
2714 c = next_char (dtp);
2715 goto get_name;
2716 }
2717
2718 /* Parse a character qualifier, if present. chigh = 0 is a default
2719 that signals that the string length = string_length. */
2720
2721 clow = 1;
2722 chigh = 0;
2723
2724 if (c == '(' && nl->type == GFC_DTYPE_CHARACTER)
2725 {
2726 descriptor_dimension chd[1] = { {1, clow, nl->string_length} };
2727 array_loop_spec ind[1] = { {1, clow, nl->string_length, 1} };
2728
2729 if (nml_parse_qualifier (dtp, chd, ind, -1, nml_err_msg, &parsed_rank)
2730 == FAILURE)
2731 {
2732 char *nml_err_msg_end = strchr (nml_err_msg, '\0');
2733 snprintf (nml_err_msg_end,
2734 nml_err_msg_size - (nml_err_msg_end - nml_err_msg),
2735 " for namelist variable %s", nl->var_name);
2736 goto nml_err_ret;
2737 }
2738
2739 clow = ind[0].start;
2740 chigh = ind[0].end;
2741
2742 if (ind[0].step != 1)
2743 {
2744 snprintf (nml_err_msg, nml_err_msg_size,
2745 "Step not allowed in substring qualifier"
2746 " for namelist object %s", nl->var_name);
2747 goto nml_err_ret;
2748 }
2749
2750 c = next_char (dtp);
2751 unget_char (dtp, c);
2752 }
2753
2754 /* If a derived type touch its components and restore the root
2755 namelist_info if we have parsed a qualified derived type
2756 component. */
2757
2758 if (nl->type == GFC_DTYPE_DERIVED)
2759 nml_touch_nodes (nl);
2760 if (component_flag && nl->var_rank > 0)
2761 nl = first_nl;
2762
2763 /* Make sure no extraneous qualifiers are there. */
2764
2765 if (c == '(')
2766 {
2767 snprintf (nml_err_msg, nml_err_msg_size,
2768 "Qualifier for a scalar or non-character namelist object %s",
2769 nl->var_name);
2770 goto nml_err_ret;
2771 }
2772
2773 /* Make sure there is no more than one non-zero rank object. */
2774 if (non_zero_rank_count > 1)
2775 {
2776 snprintf (nml_err_msg, nml_err_msg_size,
2777 "Multiple sub-objects with non-zero rank in namelist object %s",
2778 nl->var_name);
2779 non_zero_rank_count = 0;
2780 goto nml_err_ret;
2781 }
2782
2783 /* According to the standard, an equal sign MUST follow an object name. The
2784 following is possibly lax - it allows comments, blank lines and so on to
2785 intervene. eat_spaces (dtp); c = next_char (dtp); would be compliant*/
2786
2787 free_saved (dtp);
2788
2789 eat_separator (dtp);
2790 if (dtp->u.p.input_complete)
2791 return SUCCESS;
2792
2793 if (dtp->u.p.at_eol)
2794 finish_separator (dtp);
2795 if (dtp->u.p.input_complete)
2796 return SUCCESS;
2797
2798 c = next_char (dtp);
2799
2800 if (c != '=')
2801 {
2802 snprintf (nml_err_msg, nml_err_msg_size,
2803 "Equal sign must follow namelist object name %s",
2804 nl->var_name);
2805 goto nml_err_ret;
2806 }
2807
2808 if (first_nl != NULL && first_nl->var_rank > 0)
2809 nl = first_nl;
2810
2811 if (nml_read_obj (dtp, nl, 0, pprev_nl, nml_err_msg, nml_err_msg_size,
2812 clow, chigh) == FAILURE)
2813 goto nml_err_ret;
2814
2815 return SUCCESS;
2816
2817 nml_err_ret:
2818
2819 return FAILURE;
2820 }
2821
2822 /* Entry point for namelist input. Goes through input until namelist name
2823 is matched. Then cycles through nml_get_obj_data until the input is
2824 completed or there is an error. */
2825
2826 void
2827 namelist_read (st_parameter_dt *dtp)
2828 {
2829 char c;
2830 jmp_buf eof_jump;
2831 char nml_err_msg[200];
2832 /* Pointer to the previously read object, in case attempt is made to read
2833 new object name. Should this fail, error message can give previous
2834 name. */
2835 namelist_info *prev_nl = NULL;
2836
2837 dtp->u.p.namelist_mode = 1;
2838 dtp->u.p.input_complete = 0;
2839 dtp->u.p.expanded_read = 0;
2840
2841 dtp->u.p.eof_jump = &eof_jump;
2842 if (setjmp (eof_jump))
2843 {
2844 dtp->u.p.eof_jump = NULL;
2845 generate_error (&dtp->common, LIBERROR_END, NULL);
2846 return;
2847 }
2848
2849 /* Look for &namelist_name . Skip all characters, testing for $nmlname.
2850 Exit on success or EOF. If '?' or '=?' encountered in stdin, print
2851 node names or namelist on stdout. */
2852
2853 find_nml_name:
2854 switch (c = next_char (dtp))
2855 {
2856 case '$':
2857 case '&':
2858 break;
2859
2860 case '!':
2861 eat_line (dtp);
2862 goto find_nml_name;
2863
2864 case '=':
2865 c = next_char (dtp);
2866 if (c == '?')
2867 nml_query (dtp, '=');
2868 else
2869 unget_char (dtp, c);
2870 goto find_nml_name;
2871
2872 case '?':
2873 nml_query (dtp, '?');
2874
2875 default:
2876 goto find_nml_name;
2877 }
2878
2879 /* Match the name of the namelist. */
2880
2881 nml_match_name (dtp, dtp->namelist_name, dtp->namelist_name_len);
2882
2883 if (dtp->u.p.nml_read_error)
2884 goto find_nml_name;
2885
2886 /* A trailing space is required, we give a little lattitude here, 10.9.1. */
2887 c = next_char (dtp);
2888 if (!is_separator(c) && c != '!')
2889 {
2890 unget_char (dtp, c);
2891 goto find_nml_name;
2892 }
2893
2894 unget_char (dtp, c);
2895 eat_separator (dtp);
2896
2897 /* Ready to read namelist objects. If there is an error in input
2898 from stdin, output the error message and continue. */
2899
2900 while (!dtp->u.p.input_complete)
2901 {
2902 if (nml_get_obj_data (dtp, &prev_nl, nml_err_msg, sizeof nml_err_msg)
2903 == FAILURE)
2904 {
2905 gfc_unit *u;
2906
2907 if (dtp->u.p.current_unit->unit_number != options.stdin_unit)
2908 goto nml_err_ret;
2909
2910 u = find_unit (options.stderr_unit);
2911 st_printf ("%s\n", nml_err_msg);
2912 if (u != NULL)
2913 {
2914 sflush (u->s);
2915 unlock_unit (u);
2916 }
2917 }
2918
2919 }
2920
2921 dtp->u.p.eof_jump = NULL;
2922 free_saved (dtp);
2923 free_line (dtp);
2924 return;
2925
2926 /* All namelist error calls return from here */
2927
2928 nml_err_ret:
2929
2930 dtp->u.p.eof_jump = NULL;
2931 free_saved (dtp);
2932 free_line (dtp);
2933 generate_error (&dtp->common, LIBERROR_READ_VALUE, nml_err_msg);
2934 return;
2935 }