1 /* Copyright (C) 2002-2016 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
3 F2003 I/O support contributed by Jerry DeLisle
5 This file is part of the GNU Fortran runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
27 /* format.c-- parse a FORMAT string into a binary format suitable for
28 * interpretation during I/O statements */
37 static const fnode colon_node
= { FMT_COLON
, 0, NULL
, NULL
, {{ 0, 0, 0 }}, 0,
42 static const char posint_required
[] = "Positive width required in format",
43 period_required
[] = "Period required in format",
44 nonneg_required
[] = "Nonnegative width required in format",
45 unexpected_element
[] = "Unexpected element '%c' in format\n",
46 unexpected_end
[] = "Unexpected end of format string",
47 bad_string
[] = "Unterminated character constant in format",
48 bad_hollerith
[] = "Hollerith constant extends past the end of the format",
49 reversion_error
[] = "Exhausted data descriptors in format",
50 zero_width
[] = "Zero width in format descriptor";
52 /* The following routines support caching format data from parsed format strings
53 into a hash table. This avoids repeatedly parsing duplicate format strings
54 or format strings in I/O statements that are repeated in loops. */
57 /* Traverse the table and free all data. */
60 free_format_hash_table (gfc_unit
*u
)
64 /* free_format_data handles any NULL pointers. */
65 for (i
= 0; i
< FORMAT_HASH_SIZE
; i
++)
67 if (u
->format_hash_table
[i
].hashed_fmt
!= NULL
)
69 free_format_data (u
->format_hash_table
[i
].hashed_fmt
);
70 free (u
->format_hash_table
[i
].key
);
72 u
->format_hash_table
[i
].key
= NULL
;
73 u
->format_hash_table
[i
].key_len
= 0;
74 u
->format_hash_table
[i
].hashed_fmt
= NULL
;
78 /* Traverse the format_data structure and reset the fnode counters. */
81 reset_node (fnode
*fn
)
88 if (fn
->format
!= FMT_LPAREN
)
91 for (f
= fn
->u
.child
; f
; f
= f
->next
)
93 if (f
->format
== FMT_RPAREN
)
100 reset_fnode_counters (st_parameter_dt
*dtp
)
107 /* Clear this pointer at the head so things start at the right place. */
108 fmt
->array
.array
[0].current
= NULL
;
110 for (f
= fmt
->array
.array
[0].u
.child
; f
; f
= f
->next
)
115 /* A simple hashing function to generate an index into the hash table. */
118 format_hash (st_parameter_dt
*dtp
)
121 gfc_charlen_type key_len
;
125 /* Hash the format string. Super simple, but what the heck! */
127 key_len
= dtp
->format_len
;
128 for (i
= 0; i
< key_len
; i
++)
130 hash
&= (FORMAT_HASH_SIZE
- 1);
136 save_parsed_format (st_parameter_dt
*dtp
)
141 hash
= format_hash (dtp
);
142 u
= dtp
->u
.p
.current_unit
;
144 /* Index into the hash table. We are simply replacing whatever is there
145 relying on probability. */
146 if (u
->format_hash_table
[hash
].hashed_fmt
!= NULL
)
147 free_format_data (u
->format_hash_table
[hash
].hashed_fmt
);
148 u
->format_hash_table
[hash
].hashed_fmt
= NULL
;
150 free (u
->format_hash_table
[hash
].key
);
151 u
->format_hash_table
[hash
].key
= dtp
->format
;
153 u
->format_hash_table
[hash
].key_len
= dtp
->format_len
;
154 u
->format_hash_table
[hash
].hashed_fmt
= dtp
->u
.p
.fmt
;
159 find_parsed_format (st_parameter_dt
*dtp
)
164 hash
= format_hash (dtp
);
165 u
= dtp
->u
.p
.current_unit
;
167 if (u
->format_hash_table
[hash
].key
!= NULL
)
169 /* See if it matches. */
170 if (u
->format_hash_table
[hash
].key_len
== dtp
->format_len
)
172 /* So far so good. */
173 if (strncmp (u
->format_hash_table
[hash
].key
,
174 dtp
->format
, dtp
->format_len
) == 0)
175 return u
->format_hash_table
[hash
].hashed_fmt
;
182 /* next_char()-- Return the next character in the format string.
183 * Returns -1 when the string is done. If the literal flag is set,
184 * spaces are significant, otherwise they are not. */
187 next_char (format_data
*fmt
, int literal
)
193 if (fmt
->format_string_len
== 0)
196 fmt
->format_string_len
--;
197 c
= toupper (*fmt
->format_string
++);
198 fmt
->error_element
= c
;
200 while ((c
== ' ' || c
== '\t') && !literal
);
206 /* unget_char()-- Back up one character position. */
208 #define unget_char(fmt) \
209 { fmt->format_string--; fmt->format_string_len++; }
212 /* get_fnode()-- Allocate a new format node, inserting it into the
213 * current singly linked list. These are initially allocated from the
217 get_fnode (format_data
*fmt
, fnode
**head
, fnode
**tail
, format_token t
)
221 if (fmt
->avail
== &fmt
->last
->array
[FARRAY_SIZE
])
223 fmt
->last
->next
= xmalloc (sizeof (fnode_array
));
224 fmt
->last
= fmt
->last
->next
;
225 fmt
->last
->next
= NULL
;
226 fmt
->avail
= &fmt
->last
->array
[0];
229 memset (f
, '\0', sizeof (fnode
));
241 f
->source
= fmt
->format_string
;
246 /* free_format()-- Free allocated format string. */
248 free_format (st_parameter_dt
*dtp
)
250 if ((dtp
->common
.flags
& IOPARM_DT_HAS_FORMAT
) && dtp
->format
)
258 /* free_format_data()-- Free all allocated format data. */
261 free_format_data (format_data
*fmt
)
263 fnode_array
*fa
, *fa_next
;
269 /* Free vlist descriptors in the fnode_array if one was allocated. */
270 for (fnp
= fmt
->array
.array
; fnp
->format
!= FMT_NONE
; fnp
++)
271 if (fnp
->format
== FMT_DT
)
273 if (GFC_DESCRIPTOR_DATA(fnp
->u
.udf
.vlist
))
274 free (GFC_DESCRIPTOR_DATA(fnp
->u
.udf
.vlist
));
275 free (fnp
->u
.udf
.vlist
);
278 for (fa
= fmt
->array
.next
; fa
; fa
= fa_next
)
289 /* format_lex()-- Simple lexical analyzer for getting the next token
290 * in a FORMAT string. We support a one-level token pushback in the
291 * fmt->saved_token variable. */
294 format_lex (format_data
*fmt
)
301 if (fmt
->saved_token
!= FMT_NONE
)
303 token
= fmt
->saved_token
;
304 fmt
->saved_token
= FMT_NONE
;
309 c
= next_char (fmt
, 0);
330 c
= next_char (fmt
, 0);
337 fmt
->value
= c
- '0';
341 c
= next_char (fmt
, 0);
345 fmt
->value
= 10 * fmt
->value
+ c
- '0';
351 fmt
->value
= -fmt
->value
;
352 token
= FMT_SIGNED_INT
;
365 fmt
->value
= c
- '0';
369 c
= next_char (fmt
, 0);
373 fmt
->value
= 10 * fmt
->value
+ c
- '0';
377 token
= (fmt
->value
== 0) ? FMT_ZERO
: FMT_POSINT
;
401 switch (next_char (fmt
, 0))
422 switch (next_char (fmt
, 0))
439 switch (next_char (fmt
, 0))
459 fmt
->string
= fmt
->format_string
;
460 fmt
->value
= 0; /* This is the length of the string */
464 c
= next_char (fmt
, 1);
467 token
= FMT_BADSTRING
;
468 fmt
->error
= bad_string
;
474 c
= next_char (fmt
, 1);
478 token
= FMT_BADSTRING
;
479 fmt
->error
= bad_string
;
517 switch (next_char (fmt
, 0))
549 switch (next_char (fmt
, 0))
568 switch (next_char (fmt
, 0))
608 /* parse_format_list()-- Parse a format list. Assumes that a left
609 * paren has already been seen. Returns a list representing the
610 * parenthesis node which contains the rest of the list. */
613 parse_format_list (st_parameter_dt
*dtp
, bool *seen_dd
)
616 format_token t
, u
, t2
;
618 format_data
*fmt
= dtp
->u
.p
.fmt
;
619 bool seen_data_desc
= false;
623 /* Get the next format item */
625 t
= format_lex (fmt
);
630 t
= format_lex (fmt
);
633 fmt
->error
= "Left parenthesis required after '*'";
636 get_fnode (fmt
, &head
, &tail
, FMT_LPAREN
);
637 tail
->repeat
= -2; /* Signifies unlimited format. */
638 tail
->u
.child
= parse_format_list (dtp
, &seen_data_desc
);
639 *seen_dd
= seen_data_desc
;
640 if (fmt
->error
!= NULL
)
644 fmt
->error
= "'*' requires at least one associated data descriptor";
652 t
= format_lex (fmt
);
656 get_fnode (fmt
, &head
, &tail
, FMT_LPAREN
);
657 tail
->repeat
= repeat
;
658 tail
->u
.child
= parse_format_list (dtp
, &seen_data_desc
);
659 *seen_dd
= seen_data_desc
;
660 if (fmt
->error
!= NULL
)
666 get_fnode (fmt
, &head
, &tail
, FMT_SLASH
);
667 tail
->repeat
= repeat
;
671 get_fnode (fmt
, &head
, &tail
, FMT_X
);
673 tail
->u
.k
= fmt
->value
;
684 get_fnode (fmt
, &head
, &tail
, FMT_LPAREN
);
686 tail
->u
.child
= parse_format_list (dtp
, &seen_data_desc
);
687 *seen_dd
= seen_data_desc
;
688 if (fmt
->error
!= NULL
)
693 case FMT_SIGNED_INT
: /* Signed integer can only precede a P format. */
694 case FMT_ZERO
: /* Same for zero. */
695 t
= format_lex (fmt
);
698 fmt
->error
= "Expected P edit descriptor in format";
703 get_fnode (fmt
, &head
, &tail
, FMT_P
);
704 tail
->u
.k
= fmt
->value
;
707 t
= format_lex (fmt
);
708 if (t
== FMT_F
|| t
== FMT_EN
|| t
== FMT_ES
|| t
== FMT_D
709 || t
== FMT_G
|| t
== FMT_E
)
715 if (t
!= FMT_COMMA
&& t
!= FMT_RPAREN
&& t
!= FMT_SLASH
718 fmt
->error
= "Comma required after P descriptor";
722 fmt
->saved_token
= t
;
725 case FMT_P
: /* P and X require a prior number */
726 fmt
->error
= "P descriptor requires leading scale factor";
733 If we would be pedantic in the library, we would have to reject
734 an X descriptor without an integer prefix:
736 fmt->error = "X descriptor requires leading space count";
739 However, this is an extension supported by many Fortran compilers,
740 including Cray, HP, AIX, and IRIX. Therefore, we allow it in the
741 runtime library, and make the front end reject it if the compiler
742 is in pedantic mode. The interpretation of 'X' is '1X'.
744 get_fnode (fmt
, &head
, &tail
, FMT_X
);
750 get_fnode (fmt
, &head
, &tail
, FMT_STRING
);
751 tail
->u
.string
.p
= fmt
->string
;
752 tail
->u
.string
.length
= fmt
->value
;
762 notify_std (&dtp
->common
, GFC_STD_F2003
, "Fortran 2003: Round "
763 "descriptor not allowed");
764 get_fnode (fmt
, &head
, &tail
, t
);
770 notify_std (&dtp
->common
, GFC_STD_F2003
, "Fortran 2003: DC or DP "
771 "descriptor not allowed");
778 get_fnode (fmt
, &head
, &tail
, t
);
783 get_fnode (fmt
, &head
, &tail
, FMT_COLON
);
788 get_fnode (fmt
, &head
, &tail
, FMT_SLASH
);
794 get_fnode (fmt
, &head
, &tail
, FMT_DOLLAR
);
796 notify_std (&dtp
->common
, GFC_STD_GNU
, "Extension: $ descriptor");
802 t2
= format_lex (fmt
);
803 if (t2
!= FMT_POSINT
)
805 fmt
->error
= posint_required
;
808 get_fnode (fmt
, &head
, &tail
, t
);
809 tail
->u
.n
= fmt
->value
;
831 get_fnode (fmt
, &head
, &tail
, FMT_STRING
);
832 if (fmt
->format_string_len
< 1)
834 fmt
->error
= bad_hollerith
;
838 tail
->u
.string
.p
= fmt
->format_string
;
839 tail
->u
.string
.length
= 1;
842 fmt
->format_string
++;
843 fmt
->format_string_len
--;
848 fmt
->error
= unexpected_end
;
858 fmt
->error
= unexpected_element
;
862 /* In this state, t must currently be a data descriptor. Deal with
863 things that can/must follow the descriptor */
870 t
= format_lex (fmt
);
873 if (notification_std(GFC_STD_GNU
) == NOTIFICATION_ERROR
)
875 fmt
->error
= posint_required
;
880 fmt
->saved_token
= t
;
881 fmt
->value
= 1; /* Default width */
882 notify_std (&dtp
->common
, GFC_STD_GNU
, posint_required
);
886 get_fnode (fmt
, &head
, &tail
, FMT_L
);
887 tail
->u
.n
= fmt
->value
;
888 tail
->repeat
= repeat
;
893 t
= format_lex (fmt
);
896 fmt
->error
= zero_width
;
902 fmt
->saved_token
= t
;
903 fmt
->value
= -1; /* Width not present */
906 get_fnode (fmt
, &head
, &tail
, FMT_A
);
907 tail
->repeat
= repeat
;
908 tail
->u
.n
= fmt
->value
;
918 get_fnode (fmt
, &head
, &tail
, t
);
919 tail
->repeat
= repeat
;
921 u
= format_lex (fmt
);
922 if (t
== FMT_G
&& u
== FMT_ZERO
)
925 if (notification_std (GFC_STD_F2008
) == NOTIFICATION_ERROR
926 || dtp
->u
.p
.mode
== READING
)
928 fmt
->error
= zero_width
;
932 u
= format_lex (fmt
);
935 fmt
->saved_token
= u
;
939 u
= format_lex (fmt
);
942 fmt
->error
= posint_required
;
945 tail
->u
.real
.d
= fmt
->value
;
948 if (t
== FMT_F
&& dtp
->u
.p
.mode
== WRITING
)
951 if (u
!= FMT_POSINT
&& u
!= FMT_ZERO
)
953 fmt
->error
= nonneg_required
;
957 else if (u
!= FMT_POSINT
)
959 fmt
->error
= posint_required
;
963 tail
->u
.real
.w
= fmt
->value
;
965 t
= format_lex (fmt
);
968 /* We treat a missing decimal descriptor as 0. Note: This is only
969 allowed if -std=legacy, otherwise an error occurs. */
970 if (compile_options
.warn_std
!= 0)
972 fmt
->error
= period_required
;
975 fmt
->saved_token
= t
;
981 t
= format_lex (fmt
);
982 if (t
!= FMT_ZERO
&& t
!= FMT_POSINT
)
984 fmt
->error
= nonneg_required
;
988 tail
->u
.real
.d
= fmt
->value
;
991 if (t2
== FMT_D
|| t2
== FMT_F
)
997 /* Look for optional exponent */
998 t
= format_lex (fmt
);
1000 fmt
->saved_token
= t
;
1003 t
= format_lex (fmt
);
1004 if (t
!= FMT_POSINT
)
1006 fmt
->error
= "Positive exponent width required in format";
1010 tail
->u
.real
.e
= fmt
->value
;
1016 get_fnode (fmt
, &head
, &tail
, t
);
1017 tail
->repeat
= repeat
;
1019 t
= format_lex (fmt
);
1021 /* Initialize the vlist to a zero size array. */
1022 tail
->u
.udf
.vlist
= xmalloc (sizeof(gfc_array_i4
));
1023 GFC_DESCRIPTOR_DATA(tail
->u
.udf
.vlist
) = NULL
;
1024 GFC_DIMENSION_SET(tail
->u
.udf
.vlist
->dim
[0],1, 0, 0);
1026 if (t
== FMT_STRING
)
1028 /* Get pointer to the optional format string. */
1029 tail
->u
.udf
.string
= fmt
->string
;
1030 tail
->u
.udf
.string_len
= fmt
->value
;
1031 t
= format_lex (fmt
);
1033 if (t
== FMT_LPAREN
)
1035 /* Temporary buffer to hold the vlist values. */
1036 GFC_INTEGER_4 temp
[FARRAY_SIZE
];
1039 t
= format_lex (fmt
);
1040 if (t
!= FMT_POSINT
)
1042 fmt
->error
= posint_required
;
1045 /* Save the positive integer value. */
1046 temp
[i
++] = fmt
->value
;
1047 t
= format_lex (fmt
);
1050 if (t
== FMT_RPAREN
)
1052 /* We have parsed the complete vlist so initialize the
1053 array descriptor and save it in the format node. */
1054 gfc_array_i4
*vp
= tail
->u
.udf
.vlist
;
1055 GFC_DESCRIPTOR_DATA(vp
) = xmalloc (i
* sizeof(GFC_INTEGER_4
));
1056 GFC_DIMENSION_SET(vp
->dim
[0],1, i
, 1);
1057 memcpy (GFC_DESCRIPTOR_DATA(vp
), temp
, i
* sizeof(GFC_INTEGER_4
));
1060 fmt
->error
= unexpected_element
;
1063 fmt
->saved_token
= t
;
1066 if (repeat
> fmt
->format_string_len
)
1068 fmt
->error
= bad_hollerith
;
1072 get_fnode (fmt
, &head
, &tail
, FMT_STRING
);
1073 tail
->u
.string
.p
= fmt
->format_string
;
1074 tail
->u
.string
.length
= repeat
;
1077 fmt
->format_string
+= fmt
->value
;
1078 fmt
->format_string_len
-= repeat
;
1087 get_fnode (fmt
, &head
, &tail
, t
);
1088 tail
->repeat
= repeat
;
1090 t
= format_lex (fmt
);
1092 if (dtp
->u
.p
.mode
== READING
)
1094 if (t
!= FMT_POSINT
)
1096 fmt
->error
= posint_required
;
1102 if (t
!= FMT_ZERO
&& t
!= FMT_POSINT
)
1104 fmt
->error
= nonneg_required
;
1109 tail
->u
.integer
.w
= fmt
->value
;
1110 tail
->u
.integer
.m
= -1;
1112 t
= format_lex (fmt
);
1113 if (t
!= FMT_PERIOD
)
1115 fmt
->saved_token
= t
;
1119 t
= format_lex (fmt
);
1120 if (t
!= FMT_ZERO
&& t
!= FMT_POSINT
)
1122 fmt
->error
= nonneg_required
;
1126 tail
->u
.integer
.m
= fmt
->value
;
1129 if (tail
->u
.integer
.w
!= 0 && tail
->u
.integer
.m
> tail
->u
.integer
.w
)
1131 fmt
->error
= "Minimum digits exceeds field width";
1138 fmt
->error
= unexpected_element
;
1142 /* Between a descriptor and what comes next */
1144 t
= format_lex (fmt
);
1155 get_fnode (fmt
, &head
, &tail
, t
);
1157 goto optional_comma
;
1160 fmt
->error
= unexpected_end
;
1164 /* Assume a missing comma, this is a GNU extension */
1168 /* Optional comma is a weird between state where we've just finished
1169 reading a colon, slash or P descriptor. */
1171 t
= format_lex (fmt
);
1180 default: /* Assume that we have another format item */
1181 fmt
->saved_token
= t
;
1193 /* format_error()-- Generate an error message for a format statement.
1194 * If the node that gives the location of the error is NULL, the error
1195 * is assumed to happen at parse time, and the current location of the
1198 * We generate a message showing where the problem is. We take extra
1199 * care to print only the relevant part of the format if it is longer
1200 * than a standard 80 column display. */
1203 format_error (st_parameter_dt
*dtp
, const fnode
*f
, const char *message
)
1205 int width
, i
, offset
;
1207 char *p
, buffer
[BUFLEN
];
1208 format_data
*fmt
= dtp
->u
.p
.fmt
;
1212 else /* This should not happen. */
1215 if (message
== unexpected_element
)
1216 snprintf (buffer
, BUFLEN
, message
, fmt
->error_element
);
1218 snprintf (buffer
, BUFLEN
, "%s\n", message
);
1220 /* Get the offset into the format string where the error occurred. */
1221 offset
= dtp
->format_len
- (fmt
->reversion_ok
?
1222 (int) strlen(p
) : fmt
->format_string_len
);
1224 width
= dtp
->format_len
;
1229 /* Show the format */
1231 p
= strchr (buffer
, '\0');
1234 memcpy (p
, dtp
->format
, width
);
1239 /* Show where the problem is */
1241 for (i
= 1; i
< offset
; i
++)
1247 generate_error (&dtp
->common
, LIBERROR_FORMAT
, buffer
);
1251 /* revert()-- Do reversion of the format. Control reverts to the left
1252 * parenthesis that matches the rightmost right parenthesis. From our
1253 * tree structure, we are looking for the rightmost parenthesis node
1254 * at the second level, the first level always being a single
1255 * parenthesis node. If this node doesn't exit, we use the top
1259 revert (st_parameter_dt
*dtp
)
1262 format_data
*fmt
= dtp
->u
.p
.fmt
;
1264 dtp
->u
.p
.reversion_flag
= 1;
1268 for (f
= fmt
->array
.array
[0].u
.child
; f
; f
= f
->next
)
1269 if (f
->format
== FMT_LPAREN
)
1272 /* If r is NULL because no node was found, the whole tree will be used */
1274 fmt
->array
.array
[0].current
= r
;
1275 fmt
->array
.array
[0].count
= 0;
1278 /* parse_format()-- Parse a format string. */
1281 parse_format (st_parameter_dt
*dtp
)
1284 bool format_cache_ok
, seen_data_desc
= false;
1286 /* Don't cache for internal units and set an arbitrary limit on the
1287 size of format strings we will cache. (Avoids memory issues.)
1288 Also, the format_hash_table resides in the current_unit, so
1289 child_dtio procedures would overwrite the parent table */
1290 format_cache_ok
= !is_internal_unit (dtp
)
1291 && (dtp
->u
.p
.current_unit
->child_dtio
== 0);
1293 /* Lookup format string to see if it has already been parsed. */
1294 if (format_cache_ok
)
1296 dtp
->u
.p
.fmt
= find_parsed_format (dtp
);
1298 if (dtp
->u
.p
.fmt
!= NULL
)
1300 dtp
->u
.p
.fmt
->reversion_ok
= 0;
1301 dtp
->u
.p
.fmt
->saved_token
= FMT_NONE
;
1302 dtp
->u
.p
.fmt
->saved_format
= NULL
;
1303 reset_fnode_counters (dtp
);
1308 /* Not found so proceed as follows. */
1310 char *fmt_string
= fc_strdup_notrim (dtp
->format
, dtp
->format_len
);
1311 dtp
->format
= fmt_string
;
1313 dtp
->u
.p
.fmt
= fmt
= xmalloc (sizeof (format_data
));
1314 fmt
->format_string
= dtp
->format
;
1315 fmt
->format_string_len
= dtp
->format_len
;
1318 fmt
->saved_token
= FMT_NONE
;
1322 /* Initialize variables used during traversal of the tree. */
1324 fmt
->reversion_ok
= 0;
1325 fmt
->saved_format
= NULL
;
1327 /* Initialize the fnode_array. */
1329 memset (&(fmt
->array
), 0, sizeof(fmt
->array
));
1331 /* Allocate the first format node as the root of the tree. */
1333 fmt
->last
= &fmt
->array
;
1334 fmt
->last
->next
= NULL
;
1335 fmt
->avail
= &fmt
->array
.array
[0];
1337 memset (fmt
->avail
, 0, sizeof (*fmt
->avail
));
1338 fmt
->avail
->format
= FMT_LPAREN
;
1339 fmt
->avail
->repeat
= 1;
1342 if (format_lex (fmt
) == FMT_LPAREN
)
1343 fmt
->array
.array
[0].u
.child
= parse_format_list (dtp
, &seen_data_desc
);
1345 fmt
->error
= "Missing initial left parenthesis in format";
1347 if (format_cache_ok
)
1348 save_parsed_format (dtp
);
1350 dtp
->u
.p
.format_not_saved
= 1;
1353 format_error (dtp
, NULL
, fmt
->error
);
1357 /* next_format0()-- Get the next format node without worrying about
1358 * reversion. Returns NULL when we hit the end of the list.
1359 * Parenthesis nodes are incremented after the list has been
1360 * exhausted, other nodes are incremented before they are returned. */
1362 static const fnode
*
1363 next_format0 (fnode
* f
)
1370 if (f
->format
!= FMT_LPAREN
)
1373 if (f
->count
<= f
->repeat
)
1380 /* Deal with a parenthesis node with unlimited format. */
1382 if (f
->repeat
== -2) /* -2 signifies unlimited. */
1385 if (f
->current
== NULL
)
1386 f
->current
= f
->u
.child
;
1388 for (; f
->current
!= NULL
; f
->current
= f
->current
->next
)
1390 r
= next_format0 (f
->current
);
1396 /* Deal with a parenthesis node with specific repeat count. */
1397 for (; f
->count
< f
->repeat
; f
->count
++)
1399 if (f
->current
== NULL
)
1400 f
->current
= f
->u
.child
;
1402 for (; f
->current
!= NULL
; f
->current
= f
->current
->next
)
1404 r
= next_format0 (f
->current
);
1415 /* next_format()-- Return the next format node. If the format list
1416 * ends up being exhausted, we do reversion. Reversion is only
1417 * allowed if we've seen a data descriptor since the
1418 * initialization or the last reversion. We return NULL if there
1419 * are no more data descriptors to return (which is an error
1423 next_format (st_parameter_dt
*dtp
)
1427 format_data
*fmt
= dtp
->u
.p
.fmt
;
1429 if (fmt
->saved_format
!= NULL
)
1430 { /* Deal with a pushed-back format node */
1431 f
= fmt
->saved_format
;
1432 fmt
->saved_format
= NULL
;
1436 f
= next_format0 (&fmt
->array
.array
[0]);
1439 if (!fmt
->reversion_ok
)
1442 fmt
->reversion_ok
= 0;
1445 f
= next_format0 (&fmt
->array
.array
[0]);
1448 format_error (dtp
, NULL
, reversion_error
);
1452 /* Push the first reverted token and return a colon node in case
1453 * there are no more data items. */
1455 fmt
->saved_format
= f
;
1459 /* If this is a data edit descriptor, then reversion has become OK. */
1463 if (!fmt
->reversion_ok
&&
1464 (t
== FMT_I
|| t
== FMT_B
|| t
== FMT_O
|| t
== FMT_Z
|| t
== FMT_F
||
1465 t
== FMT_E
|| t
== FMT_EN
|| t
== FMT_ES
|| t
== FMT_G
|| t
== FMT_L
||
1466 t
== FMT_A
|| t
== FMT_D
|| t
== FMT_DT
))
1467 fmt
->reversion_ok
= 1;
1472 /* unget_format()-- Push the given format back so that it will be
1473 * returned on the next call to next_format() without affecting
1474 * counts. This is necessary when we've encountered a data
1475 * descriptor, but don't know what the data item is yet. The format
1476 * node is pushed back, and we return control to the main program,
1477 * which calls the library back with the data item (or not). */
1480 unget_format (st_parameter_dt
*dtp
, const fnode
*f
)
1482 dtp
->u
.p
.fmt
->saved_format
= f
;