| 1 |
/* Copyright (C) 2002-2025 Free Software Foundation, Inc. |
| 2 |
Contributed by Andy Vaught |
| 3 |
F2003 I/O support contributed by Jerry DeLisle |
| 4 |
|
| 5 |
This file is part of the GNU Fortran runtime library (libgfortran). |
| 6 |
|
| 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) |
| 10 |
any later version. |
| 11 |
|
| 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. |
| 16 |
|
| 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. |
| 20 |
|
| 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/>. */ |
| 25 |
|
| 26 |
|
| 27 |
/* format.c-- parse a FORMAT string into a binary format suitable for |
| 28 |
interpretation during I/O statements. */ |
| 29 |
|
| 30 |
#include "io.h" |
| 31 |
#include "format.h" |
| 32 |
#include <string.h> |
| 33 |
|
| 34 |
|
| 35 |
static const fnode colon_node = { FMT_COLON, FMT_NONE, 0, NULL, NULL, {{ 0, 0, 0 }}, 0, |
| 36 |
NULL }; |
| 37 |
|
| 38 |
/* Error messages. */ |
| 39 |
|
| 40 |
static const char posint_required[] = "Positive integer required in format", |
| 41 |
period_required[] = "Period required in format", |
| 42 |
nonneg_required[] = "Nonnegative width required in format", |
| 43 |
unexpected_element[] = "Unexpected element '%c' in format\n", |
| 44 |
unexpected_end[] = "Unexpected end of format string", |
| 45 |
bad_string[] = "Unterminated character constant in format", |
| 46 |
bad_hollerith[] = "Hollerith constant extends past the end of the format", |
| 47 |
reversion_error[] = "Exhausted data descriptors in format", |
| 48 |
zero_width[] = "Zero width in format descriptor", |
| 49 |
comma_missing[] = "Missing comma between descriptors"; |
| 50 |
|
| 51 |
/* The following routines support caching format data from parsed format strings |
| 52 |
into a hash table. This avoids repeatedly parsing duplicate format strings |
| 53 |
or format strings in I/O statements that are repeated in loops. */ |
| 54 |
|
| 55 |
|
| 56 |
/* Traverse the table and free all data. */ |
| 57 |
|
| 58 |
void |
| 59 |
free_format_hash_table (gfc_unit *u) |
| 60 |
{ |
| 61 |
size_t i; |
| 62 |
|
| 63 |
/* free_format_data handles any NULL pointers. */ |
| 64 |
for (i = 0; i < FORMAT_HASH_SIZE; i++) |
| 65 |
{ |
| 66 |
if (u->format_hash_table[i].hashed_fmt != NULL) |
| 67 |
{ |
| 68 |
free_format_data (u->format_hash_table[i].hashed_fmt); |
| 69 |
free (u->format_hash_table[i].key); |
| 70 |
} |
| 71 |
u->format_hash_table[i].key = NULL; |
| 72 |
u->format_hash_table[i].key_len = 0; |
| 73 |
u->format_hash_table[i].hashed_fmt = NULL; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/* Traverse the format_data structure and reset the fnode counters. */ |
| 78 |
|
| 79 |
static void |
| 80 |
reset_node (fnode *fn) |
| 81 |
{ |
| 82 |
fnode *f; |
| 83 |
|
| 84 |
fn->count = 0; |
| 85 |
fn->current = NULL; |
| 86 |
|
| 87 |
if (fn->format != FMT_LPAREN) |
| 88 |
return; |
| 89 |
|
| 90 |
for (f = fn->u.child; f; f = f->next) |
| 91 |
{ |
| 92 |
if (f->format == FMT_RPAREN) |
| 93 |
break; |
| 94 |
reset_node (f); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
static void |
| 99 |
reset_fnode_counters (st_parameter_dt *dtp) |
| 100 |
{ |
| 101 |
fnode *f; |
| 102 |
format_data *fmt; |
| 103 |
|
| 104 |
fmt = dtp->u.p.fmt; |
| 105 |
|
| 106 |
/* Clear this pointer at the head so things start at the right place. */ |
| 107 |
fmt->array.array[0].current = NULL; |
| 108 |
|
| 109 |
for (f = fmt->array.array[0].u.child; f; f = f->next) |
| 110 |
reset_node (f); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
/* A simple hashing function to generate an index into the hash table. */ |
| 115 |
|
| 116 |
static uint32_t |
| 117 |
format_hash (st_parameter_dt *dtp) |
| 118 |
{ |
| 119 |
char *key; |
| 120 |
gfc_charlen_type key_len; |
| 121 |
uint32_t hash = 0; |
| 122 |
gfc_charlen_type i; |
| 123 |
|
| 124 |
/* Hash the format string. Super simple, but what the heck! */ |
| 125 |
key = dtp->format; |
| 126 |
key_len = dtp->format_len; |
| 127 |
for (i = 0; i < key_len; i++) |
| 128 |
hash ^= key[i]; |
| 129 |
hash &= (FORMAT_HASH_SIZE - 1); |
| 130 |
return hash; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
static void |
| 135 |
save_parsed_format (st_parameter_dt *dtp) |
| 136 |
{ |
| 137 |
uint32_t hash; |
| 138 |
gfc_unit *u; |
| 139 |
|
| 140 |
hash = format_hash (dtp); |
| 141 |
u = dtp->u.p.current_unit; |
| 142 |
|
| 143 |
/* Index into the hash table. We are simply replacing whatever is there |
| 144 |
relying on probability. */ |
| 145 |
if (u->format_hash_table[hash].hashed_fmt != NULL) |
| 146 |
free_format_data (u->format_hash_table[hash].hashed_fmt); |
| 147 |
u->format_hash_table[hash].hashed_fmt = NULL; |
| 148 |
|
| 149 |
free (u->format_hash_table[hash].key); |
| 150 |
u->format_hash_table[hash].key = dtp->format; |
| 151 |
|
| 152 |
u->format_hash_table[hash].key_len = dtp->format_len; |
| 153 |
u->format_hash_table[hash].hashed_fmt = dtp->u.p.fmt; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
static format_data * |
| 158 |
find_parsed_format (st_parameter_dt *dtp) |
| 159 |
{ |
| 160 |
uint32_t hash; |
| 161 |
gfc_unit *u; |
| 162 |
|
| 163 |
hash = format_hash (dtp); |
| 164 |
u = dtp->u.p.current_unit; |
| 165 |
|
| 166 |
if (u->format_hash_table[hash].key != NULL) |
| 167 |
{ |
| 168 |
/* See if it matches. */ |
| 169 |
if (u->format_hash_table[hash].key_len == dtp->format_len) |
| 170 |
{ |
| 171 |
/* So far so good. */ |
| 172 |
if (strncmp (u->format_hash_table[hash].key, |
| 173 |
dtp->format, dtp->format_len) == 0) |
| 174 |
return u->format_hash_table[hash].hashed_fmt; |
| 175 |
} |
| 176 |
} |
| 177 |
return NULL; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
/* next_char()-- Return the next character in the format string. |
| 182 |
Returns -1 when the string is done. If the literal flag is set, |
| 183 |
spaces are significant, otherwise they are not. */ |
| 184 |
|
| 185 |
static int |
| 186 |
next_char (format_data *fmt, int literal) |
| 187 |
{ |
| 188 |
int c; |
| 189 |
|
| 190 |
do |
| 191 |
{ |
| 192 |
if (fmt->format_string_len == 0) |
| 193 |
return -1; |
| 194 |
|
| 195 |
fmt->format_string_len--; |
| 196 |
c = safe_toupper (*fmt->format_string++); |
| 197 |
fmt->error_element = c; |
| 198 |
} |
| 199 |
while ((c == ' ' || c == '\t') && !literal); |
| 200 |
|
| 201 |
return c; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
/* unget_char()-- Back up one character position. */ |
| 206 |
|
| 207 |
#define unget_char(fmt) \ |
| 208 |
{ fmt->format_string--; fmt->format_string_len++; } |
| 209 |
|
| 210 |
|
| 211 |
/* get_fnode()-- Allocate a new format node, inserting it into the |
| 212 |
current singly linked list. These are initially allocated from the |
| 213 |
static buffer. */ |
| 214 |
|
| 215 |
static fnode * |
| 216 |
get_fnode (format_data *fmt, fnode **head, fnode **tail, format_token t) |
| 217 |
{ |
| 218 |
fnode *f; |
| 219 |
|
| 220 |
if (fmt->avail == &fmt->last->array[FARRAY_SIZE]) |
| 221 |
{ |
| 222 |
fmt->last->next = xmalloc (sizeof (fnode_array)); |
| 223 |
fmt->last = fmt->last->next; |
| 224 |
fmt->last->next = NULL; |
| 225 |
fmt->avail = &fmt->last->array[0]; |
| 226 |
} |
| 227 |
f = fmt->avail++; |
| 228 |
memset (f, '\0', sizeof (fnode)); |
| 229 |
f->pushed = FMT_NONE; |
| 230 |
|
| 231 |
if (*head == NULL) |
| 232 |
*head = *tail = f; |
| 233 |
else |
| 234 |
{ |
| 235 |
(*tail)->next = f; |
| 236 |
*tail = f; |
| 237 |
} |
| 238 |
|
| 239 |
f->format = t; |
| 240 |
f->repeat = -1; |
| 241 |
f->source = fmt->format_string; |
| 242 |
return f; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
/* free_format()-- Free allocated format string. */ |
| 247 |
void |
| 248 |
free_format (st_parameter_dt *dtp) |
| 249 |
{ |
| 250 |
if ((dtp->common.flags & IOPARM_DT_HAS_FORMAT) && dtp->format) |
| 251 |
{ |
| 252 |
free (dtp->format); |
| 253 |
dtp->format = NULL; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
/* free_format_data()-- Free all allocated format data. */ |
| 259 |
|
| 260 |
void |
| 261 |
free_format_data (format_data *fmt) |
| 262 |
{ |
| 263 |
fnode_array *fa, *fa_next; |
| 264 |
fnode *fnp; |
| 265 |
|
| 266 |
if (fmt == NULL) |
| 267 |
return; |
| 268 |
|
| 269 |
/* Free vlist descriptors in the fnode_array if one was allocated. */ |
| 270 |
for (fnp = fmt->array.array; fnp < &fmt->array.array[FARRAY_SIZE] && |
| 271 |
fnp->format != FMT_NONE; fnp++) |
| 272 |
if (fnp->format == FMT_DT) |
| 273 |
{ |
| 274 |
free (GFC_DESCRIPTOR_DATA(fnp->u.udf.vlist)); |
| 275 |
free (fnp->u.udf.vlist); |
| 276 |
} |
| 277 |
|
| 278 |
for (fa = fmt->array.next; fa; fa = fa_next) |
| 279 |
{ |
| 280 |
fa_next = fa->next; |
| 281 |
free (fa); |
| 282 |
} |
| 283 |
|
| 284 |
free (fmt); |
| 285 |
fmt = NULL; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 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. */ |
| 292 |
|
| 293 |
static format_token |
| 294 |
format_lex (format_data *fmt) |
| 295 |
{ |
| 296 |
format_token token; |
| 297 |
int negative_flag; |
| 298 |
int c; |
| 299 |
char delim; |
| 300 |
|
| 301 |
if (fmt->saved_token != FMT_NONE) |
| 302 |
{ |
| 303 |
token = fmt->saved_token; |
| 304 |
fmt->saved_token = FMT_NONE; |
| 305 |
return token; |
| 306 |
} |
| 307 |
|
| 308 |
negative_flag = 0; |
| 309 |
c = next_char (fmt, 0); |
| 310 |
|
| 311 |
switch (c) |
| 312 |
{ |
| 313 |
case '*': |
| 314 |
token = FMT_STAR; |
| 315 |
break; |
| 316 |
|
| 317 |
case '(': |
| 318 |
token = FMT_LPAREN; |
| 319 |
break; |
| 320 |
|
| 321 |
case ')': |
| 322 |
token = FMT_RPAREN; |
| 323 |
break; |
| 324 |
|
| 325 |
case '-': |
| 326 |
negative_flag = 1; |
| 327 |
/* Fall Through */ |
| 328 |
|
| 329 |
case '+': |
| 330 |
c = next_char (fmt, 0); |
| 331 |
if (!safe_isdigit (c)) |
| 332 |
{ |
| 333 |
token = FMT_UNKNOWN; |
| 334 |
break; |
| 335 |
} |
| 336 |
|
| 337 |
fmt->value = c - '0'; |
| 338 |
|
| 339 |
for (;;) |
| 340 |
{ |
| 341 |
c = next_char (fmt, 0); |
| 342 |
if (!safe_isdigit (c)) |
| 343 |
break; |
| 344 |
|
| 345 |
fmt->value = 10 * fmt->value + c - '0'; |
| 346 |
} |
| 347 |
|
| 348 |
unget_char (fmt); |
| 349 |
|
| 350 |
if (negative_flag) |
| 351 |
fmt->value = -fmt->value; |
| 352 |
token = FMT_SIGNED_INT; |
| 353 |
break; |
| 354 |
|
| 355 |
case '0': |
| 356 |
case '1': |
| 357 |
case '2': |
| 358 |
case '3': |
| 359 |
case '4': |
| 360 |
case '5': |
| 361 |
case '6': |
| 362 |
case '7': |
| 363 |
case '8': |
| 364 |
case '9': |
| 365 |
fmt->value = c - '0'; |
| 366 |
|
| 367 |
for (;;) |
| 368 |
{ |
| 369 |
c = next_char (fmt, 0); |
| 370 |
if (!safe_isdigit (c)) |
| 371 |
break; |
| 372 |
|
| 373 |
fmt->value = 10 * fmt->value + c - '0'; |
| 374 |
} |
| 375 |
|
| 376 |
unget_char (fmt); |
| 377 |
token = (fmt->value == 0) ? FMT_ZERO : FMT_POSINT; |
| 378 |
break; |
| 379 |
|
| 380 |
case '.': |
| 381 |
token = FMT_PERIOD; |
| 382 |
break; |
| 383 |
|
| 384 |
case ',': |
| 385 |
token = FMT_COMMA; |
| 386 |
break; |
| 387 |
|
| 388 |
case ':': |
| 389 |
token = FMT_COLON; |
| 390 |
break; |
| 391 |
|
| 392 |
case '/': |
| 393 |
token = FMT_SLASH; |
| 394 |
break; |
| 395 |
|
| 396 |
case '$': |
| 397 |
token = FMT_DOLLAR; |
| 398 |
break; |
| 399 |
|
| 400 |
case 'T': |
| 401 |
switch (next_char (fmt, 0)) |
| 402 |
{ |
| 403 |
case 'L': |
| 404 |
token = FMT_TL; |
| 405 |
break; |
| 406 |
case 'R': |
| 407 |
token = FMT_TR; |
| 408 |
break; |
| 409 |
default: |
| 410 |
token = FMT_T; |
| 411 |
unget_char (fmt); |
| 412 |
break; |
| 413 |
} |
| 414 |
|
| 415 |
break; |
| 416 |
|
| 417 |
case 'X': |
| 418 |
token = FMT_X; |
| 419 |
break; |
| 420 |
|
| 421 |
case 'S': |
| 422 |
switch (next_char (fmt, 0)) |
| 423 |
{ |
| 424 |
case 'S': |
| 425 |
token = FMT_SS; |
| 426 |
break; |
| 427 |
case 'P': |
| 428 |
token = FMT_SP; |
| 429 |
break; |
| 430 |
default: |
| 431 |
token = FMT_S; |
| 432 |
unget_char (fmt); |
| 433 |
break; |
| 434 |
} |
| 435 |
|
| 436 |
break; |
| 437 |
|
| 438 |
case 'B': |
| 439 |
switch (next_char (fmt, 0)) |
| 440 |
{ |
| 441 |
case 'N': |
| 442 |
token = FMT_BN; |
| 443 |
break; |
| 444 |
case 'Z': |
| 445 |
token = FMT_BZ; |
| 446 |
break; |
| 447 |
default: |
| 448 |
token = FMT_B; |
| 449 |
unget_char (fmt); |
| 450 |
break; |
| 451 |
} |
| 452 |
|
| 453 |
break; |
| 454 |
|
| 455 |
case '\'': |
| 456 |
case '"': |
| 457 |
delim = c; |
| 458 |
|
| 459 |
fmt->string = fmt->format_string; |
| 460 |
fmt->value = 0; /* This is the length of the string */ |
| 461 |
|
| 462 |
for (;;) |
| 463 |
{ |
| 464 |
c = next_char (fmt, 1); |
| 465 |
if (c == -1) |
| 466 |
{ |
| 467 |
token = FMT_BADSTRING; |
| 468 |
fmt->error = bad_string; |
| 469 |
break; |
| 470 |
} |
| 471 |
|
| 472 |
if (c == delim) |
| 473 |
{ |
| 474 |
c = next_char (fmt, 1); |
| 475 |
|
| 476 |
if (c == -1) |
| 477 |
{ |
| 478 |
token = FMT_BADSTRING; |
| 479 |
fmt->error = bad_string; |
| 480 |
break; |
| 481 |
} |
| 482 |
|
| 483 |
if (c != delim) |
| 484 |
{ |
| 485 |
unget_char (fmt); |
| 486 |
token = FMT_STRING; |
| 487 |
break; |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
fmt->value++; |
| 492 |
} |
| 493 |
|
| 494 |
break; |
| 495 |
|
| 496 |
case 'P': |
| 497 |
token = FMT_P; |
| 498 |
break; |
| 499 |
|
| 500 |
case 'I': |
| 501 |
token = FMT_I; |
| 502 |
break; |
| 503 |
|
| 504 |
case 'O': |
| 505 |
token = FMT_O; |
| 506 |
break; |
| 507 |
|
| 508 |
case 'Z': |
| 509 |
token = FMT_Z; |
| 510 |
break; |
| 511 |
|
| 512 |
case 'F': |
| 513 |
token = FMT_F; |
| 514 |
break; |
| 515 |
|
| 516 |
case 'E': |
| 517 |
switch (next_char (fmt, 0)) |
| 518 |
{ |
| 519 |
case 'N': |
| 520 |
token = FMT_EN; |
| 521 |
break; |
| 522 |
case 'S': |
| 523 |
token = FMT_ES; |
| 524 |
break; |
| 525 |
default: |
| 526 |
token = FMT_E; |
| 527 |
unget_char (fmt); |
| 528 |
break; |
| 529 |
} |
| 530 |
break; |
| 531 |
|
| 532 |
case 'G': |
| 533 |
token = FMT_G; |
| 534 |
break; |
| 535 |
|
| 536 |
case 'H': |
| 537 |
token = FMT_H; |
| 538 |
break; |
| 539 |
|
| 540 |
case 'L': |
| 541 |
token = FMT_L; |
| 542 |
break; |
| 543 |
|
| 544 |
case 'A': |
| 545 |
token = FMT_A; |
| 546 |
break; |
| 547 |
|
| 548 |
case 'D': |
| 549 |
switch (next_char (fmt, 0)) |
| 550 |
{ |
| 551 |
case 'P': |
| 552 |
token = FMT_DP; |
| 553 |
break; |
| 554 |
case 'C': |
| 555 |
token = FMT_DC; |
| 556 |
break; |
| 557 |
case 'T': |
| 558 |
token = FMT_DT; |
| 559 |
break; |
| 560 |
default: |
| 561 |
token = FMT_D; |
| 562 |
unget_char (fmt); |
| 563 |
break; |
| 564 |
} |
| 565 |
break; |
| 566 |
|
| 567 |
case 'R': |
| 568 |
switch (next_char (fmt, 0)) |
| 569 |
{ |
| 570 |
case 'C': |
| 571 |
token = FMT_RC; |
| 572 |
break; |
| 573 |
case 'D': |
| 574 |
token = FMT_RD; |
| 575 |
break; |
| 576 |
case 'N': |
| 577 |
token = FMT_RN; |
| 578 |
break; |
| 579 |
case 'P': |
| 580 |
token = FMT_RP; |
| 581 |
break; |
| 582 |
case 'U': |
| 583 |
token = FMT_RU; |
| 584 |
break; |
| 585 |
case 'Z': |
| 586 |
token = FMT_RZ; |
| 587 |
break; |
| 588 |
default: |
| 589 |
unget_char (fmt); |
| 590 |
token = FMT_UNKNOWN; |
| 591 |
break; |
| 592 |
} |
| 593 |
break; |
| 594 |
|
| 595 |
case -1: |
| 596 |
token = FMT_END; |
| 597 |
break; |
| 598 |
|
| 599 |
default: |
| 600 |
token = FMT_UNKNOWN; |
| 601 |
break; |
| 602 |
} |
| 603 |
|
| 604 |
return token; |
| 605 |
} |
| 606 |
|
| 607 |
|
| 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. */ |
| 611 |
|
| 612 |
static fnode * |
| 613 |
parse_format_list (st_parameter_dt *dtp, bool *seen_dd) |
| 614 |
{ |
| 615 |
fnode *head, *tail; |
| 616 |
format_token t, u, t2; |
| 617 |
int repeat; |
| 618 |
format_data *fmt = dtp->u.p.fmt; |
| 619 |
bool seen_data_desc = false; |
| 620 |
int standard; |
| 621 |
|
| 622 |
head = tail = NULL; |
| 623 |
|
| 624 |
/* Get the next format item */ |
| 625 |
format_item: |
| 626 |
t = format_lex (fmt); |
| 627 |
format_item_1: |
| 628 |
switch (t) |
| 629 |
{ |
| 630 |
case FMT_STAR: |
| 631 |
t = format_lex (fmt); |
| 632 |
if (t != FMT_LPAREN) |
| 633 |
{ |
| 634 |
fmt->error = "Left parenthesis required after '*'"; |
| 635 |
goto finished; |
| 636 |
} |
| 637 |
get_fnode (fmt, &head, &tail, FMT_LPAREN); |
| 638 |
tail->repeat = -2; /* Signifies unlimited format. */ |
| 639 |
tail->u.child = parse_format_list (dtp, &seen_data_desc); |
| 640 |
*seen_dd = seen_data_desc; |
| 641 |
if (fmt->error != NULL) |
| 642 |
goto finished; |
| 643 |
if (!seen_data_desc) |
| 644 |
{ |
| 645 |
fmt->error = "'*' requires at least one associated data descriptor"; |
| 646 |
goto finished; |
| 647 |
} |
| 648 |
goto between_desc; |
| 649 |
|
| 650 |
case FMT_POSINT: |
| 651 |
repeat = fmt->value; |
| 652 |
|
| 653 |
t = format_lex (fmt); |
| 654 |
switch (t) |
| 655 |
{ |
| 656 |
case FMT_LPAREN: |
| 657 |
get_fnode (fmt, &head, &tail, FMT_LPAREN); |
| 658 |
tail->repeat = repeat; |
| 659 |
tail->u.child = parse_format_list (dtp, &seen_data_desc); |
| 660 |
*seen_dd = seen_data_desc; |
| 661 |
if (fmt->error != NULL) |
| 662 |
goto finished; |
| 663 |
|
| 664 |
goto between_desc; |
| 665 |
|
| 666 |
case FMT_SLASH: |
| 667 |
get_fnode (fmt, &head, &tail, FMT_SLASH); |
| 668 |
tail->repeat = repeat; |
| 669 |
goto optional_comma; |
| 670 |
|
| 671 |
case FMT_X: |
| 672 |
get_fnode (fmt, &head, &tail, FMT_X); |
| 673 |
tail->repeat = 1; |
| 674 |
tail->u.k = fmt->value; |
| 675 |
goto between_desc; |
| 676 |
|
| 677 |
case FMT_P: |
| 678 |
goto p_descriptor; |
| 679 |
|
| 680 |
default: |
| 681 |
goto data_desc; |
| 682 |
} |
| 683 |
|
| 684 |
case FMT_LPAREN: |
| 685 |
get_fnode (fmt, &head, &tail, FMT_LPAREN); |
| 686 |
tail->repeat = 1; |
| 687 |
tail->u.child = parse_format_list (dtp, &seen_data_desc); |
| 688 |
*seen_dd = seen_data_desc; |
| 689 |
if (fmt->error != NULL) |
| 690 |
goto finished; |
| 691 |
|
| 692 |
goto between_desc; |
| 693 |
|
| 694 |
case FMT_SIGNED_INT: /* Signed integer can only precede a P format. */ |
| 695 |
case FMT_ZERO: /* Same for zero. */ |
| 696 |
t = format_lex (fmt); |
| 697 |
if (t != FMT_P) |
| 698 |
{ |
| 699 |
fmt->error = "Expected P edit descriptor in format"; |
| 700 |
goto finished; |
| 701 |
} |
| 702 |
|
| 703 |
p_descriptor: |
| 704 |
get_fnode (fmt, &head, &tail, FMT_P); |
| 705 |
tail->u.k = fmt->value; |
| 706 |
tail->repeat = 1; |
| 707 |
|
| 708 |
t = format_lex (fmt); |
| 709 |
if (t == FMT_F || t == FMT_EN || t == FMT_ES || t == FMT_D |
| 710 |
|| t == FMT_G || t == FMT_E) |
| 711 |
{ |
| 712 |
repeat = 1; |
| 713 |
goto data_desc; |
| 714 |
} |
| 715 |
|
| 716 |
if (t != FMT_COMMA && t != FMT_RPAREN && t != FMT_SLASH |
| 717 |
&& t != FMT_POSINT) |
| 718 |
{ |
| 719 |
fmt->error = "Comma required after P descriptor"; |
| 720 |
goto finished; |
| 721 |
} |
| 722 |
|
| 723 |
fmt->saved_token = t; |
| 724 |
goto optional_comma; |
| 725 |
|
| 726 |
case FMT_P: /* P and X require a prior number */ |
| 727 |
fmt->error = "P descriptor requires leading scale factor"; |
| 728 |
goto finished; |
| 729 |
|
| 730 |
case FMT_X: |
| 731 |
/* |
| 732 |
EXTENSION! |
| 733 |
|
| 734 |
If we would be pedantic in the library, we would have to reject |
| 735 |
an X descriptor without an integer prefix: |
| 736 |
|
| 737 |
fmt->error = "X descriptor requires leading space count"; |
| 738 |
goto finished; |
| 739 |
|
| 740 |
However, this is an extension supported by many Fortran compilers, |
| 741 |
including Cray, HP, AIX, and IRIX. Therefore, we allow it in the |
| 742 |
runtime library, and make the front end reject it if the compiler |
| 743 |
is in pedantic mode. The interpretation of 'X' is '1X'. |
| 744 |
*/ |
| 745 |
get_fnode (fmt, &head, &tail, FMT_X); |
| 746 |
tail->repeat = 1; |
| 747 |
tail->u.k = 1; |
| 748 |
goto between_desc; |
| 749 |
|
| 750 |
case FMT_STRING: |
| 751 |
get_fnode (fmt, &head, &tail, FMT_STRING); |
| 752 |
tail->u.string.p = fmt->string; |
| 753 |
tail->u.string.length = fmt->value; |
| 754 |
tail->repeat = 1; |
| 755 |
goto optional_comma; |
| 756 |
|
| 757 |
case FMT_RC: |
| 758 |
case FMT_RD: |
| 759 |
case FMT_RN: |
| 760 |
case FMT_RP: |
| 761 |
case FMT_RU: |
| 762 |
case FMT_RZ: |
| 763 |
notify_std (&dtp->common, GFC_STD_F2003, "Fortran 2003: Round " |
| 764 |
"descriptor not allowed"); |
| 765 |
get_fnode (fmt, &head, &tail, t); |
| 766 |
tail->repeat = 1; |
| 767 |
goto between_desc; |
| 768 |
|
| 769 |
case FMT_DC: |
| 770 |
case FMT_DP: |
| 771 |
notify_std (&dtp->common, GFC_STD_F2003, "Fortran 2003: DC or DP " |
| 772 |
"descriptor not allowed"); |
| 773 |
/* Fall through. */ |
| 774 |
case FMT_S: |
| 775 |
case FMT_SS: |
| 776 |
case FMT_SP: |
| 777 |
case FMT_BN: |
| 778 |
case FMT_BZ: |
| 779 |
get_fnode (fmt, &head, &tail, t); |
| 780 |
tail->repeat = 1; |
| 781 |
goto between_desc; |
| 782 |
|
| 783 |
case FMT_COLON: |
| 784 |
get_fnode (fmt, &head, &tail, FMT_COLON); |
| 785 |
tail->repeat = 1; |
| 786 |
goto optional_comma; |
| 787 |
|
| 788 |
case FMT_SLASH: |
| 789 |
get_fnode (fmt, &head, &tail, FMT_SLASH); |
| 790 |
tail->repeat = 1; |
| 791 |
tail->u.r = 1; |
| 792 |
goto optional_comma; |
| 793 |
|
| 794 |
case FMT_DOLLAR: |
| 795 |
get_fnode (fmt, &head, &tail, FMT_DOLLAR); |
| 796 |
tail->repeat = 1; |
| 797 |
notify_std (&dtp->common, GFC_STD_GNU, "Extension: $ descriptor"); |
| 798 |
goto between_desc; |
| 799 |
|
| 800 |
case FMT_T: |
| 801 |
case FMT_TL: |
| 802 |
case FMT_TR: |
| 803 |
t2 = format_lex (fmt); |
| 804 |
if (t2 != FMT_POSINT) |
| 805 |
{ |
| 806 |
fmt->error = posint_required; |
| 807 |
goto finished; |
| 808 |
} |
| 809 |
get_fnode (fmt, &head, &tail, t); |
| 810 |
tail->u.n = fmt->value; |
| 811 |
tail->repeat = 1; |
| 812 |
goto between_desc; |
| 813 |
|
| 814 |
case FMT_I: |
| 815 |
case FMT_B: |
| 816 |
case FMT_O: |
| 817 |
case FMT_Z: |
| 818 |
case FMT_E: |
| 819 |
case FMT_EN: |
| 820 |
case FMT_ES: |
| 821 |
case FMT_D: |
| 822 |
case FMT_DT: |
| 823 |
case FMT_L: |
| 824 |
case FMT_A: |
| 825 |
case FMT_F: |
| 826 |
case FMT_G: |
| 827 |
repeat = 1; |
| 828 |
*seen_dd = true; |
| 829 |
goto data_desc; |
| 830 |
|
| 831 |
case FMT_H: |
| 832 |
get_fnode (fmt, &head, &tail, FMT_STRING); |
| 833 |
if (fmt->format_string_len < 1) |
| 834 |
{ |
| 835 |
fmt->error = bad_hollerith; |
| 836 |
goto finished; |
| 837 |
} |
| 838 |
|
| 839 |
tail->u.string.p = fmt->format_string; |
| 840 |
tail->u.string.length = 1; |
| 841 |
tail->repeat = 1; |
| 842 |
|
| 843 |
fmt->format_string++; |
| 844 |
fmt->format_string_len--; |
| 845 |
|
| 846 |
goto between_desc; |
| 847 |
|
| 848 |
case FMT_END: |
| 849 |
fmt->error = unexpected_end; |
| 850 |
goto finished; |
| 851 |
|
| 852 |
case FMT_BADSTRING: |
| 853 |
goto finished; |
| 854 |
|
| 855 |
case FMT_RPAREN: |
| 856 |
goto finished; |
| 857 |
|
| 858 |
default: |
| 859 |
fmt->error = unexpected_element; |
| 860 |
goto finished; |
| 861 |
} |
| 862 |
|
| 863 |
/* In this state, t must currently be a data descriptor. Deal with |
| 864 |
things that can/must follow the descriptor */ |
| 865 |
data_desc: |
| 866 |
|
| 867 |
switch (t) |
| 868 |
{ |
| 869 |
case FMT_L: |
| 870 |
*seen_dd = true; |
| 871 |
t = format_lex (fmt); |
| 872 |
if (t != FMT_POSINT) |
| 873 |
{ |
| 874 |
if (t == FMT_ZERO) |
| 875 |
{ |
| 876 |
if (notification_std(GFC_STD_GNU) == NOTIFICATION_ERROR) |
| 877 |
{ |
| 878 |
fmt->error = "Extension: Zero width after L descriptor"; |
| 879 |
goto finished; |
| 880 |
} |
| 881 |
else |
| 882 |
notify_std (&dtp->common, GFC_STD_GNU, |
| 883 |
"Zero width after L descriptor"); |
| 884 |
} |
| 885 |
else |
| 886 |
{ |
| 887 |
fmt->saved_token = t; |
| 888 |
notify_std (&dtp->common, GFC_STD_GNU, |
| 889 |
"Positive width required with L descriptor"); |
| 890 |
} |
| 891 |
fmt->value = 1; /* Default width */ |
| 892 |
} |
| 893 |
get_fnode (fmt, &head, &tail, FMT_L); |
| 894 |
tail->u.n = fmt->value; |
| 895 |
tail->repeat = repeat; |
| 896 |
break; |
| 897 |
|
| 898 |
case FMT_A: |
| 899 |
*seen_dd = true; |
| 900 |
t = format_lex (fmt); |
| 901 |
if (t == FMT_ZERO) |
| 902 |
{ |
| 903 |
fmt->error = zero_width; |
| 904 |
goto finished; |
| 905 |
} |
| 906 |
|
| 907 |
if (t != FMT_POSINT) |
| 908 |
{ |
| 909 |
fmt->saved_token = t; |
| 910 |
fmt->value = -1; /* Width not present */ |
| 911 |
} |
| 912 |
|
| 913 |
get_fnode (fmt, &head, &tail, FMT_A); |
| 914 |
tail->repeat = repeat; |
| 915 |
tail->u.n = fmt->value; |
| 916 |
break; |
| 917 |
|
| 918 |
case FMT_D: |
| 919 |
case FMT_E: |
| 920 |
case FMT_F: |
| 921 |
case FMT_G: |
| 922 |
case FMT_EN: |
| 923 |
case FMT_ES: |
| 924 |
*seen_dd = true; |
| 925 |
get_fnode (fmt, &head, &tail, t); |
| 926 |
tail->repeat = repeat; |
| 927 |
tail->pushed = FMT_NONE; |
| 928 |
|
| 929 |
u = format_lex (fmt); |
| 930 |
|
| 931 |
/* Processing for zero width formats. */ |
| 932 |
if (u == FMT_ZERO) |
| 933 |
{ |
| 934 |
if (t == FMT_F) |
| 935 |
standard = GFC_STD_F95; |
| 936 |
else if (t == FMT_G) |
| 937 |
standard = GFC_STD_F2008; |
| 938 |
else |
| 939 |
standard = GFC_STD_F2018; |
| 940 |
|
| 941 |
if (notification_std (standard) == NOTIFICATION_ERROR |
| 942 |
|| dtp->u.p.mode == READING) |
| 943 |
{ |
| 944 |
fmt->error = zero_width; |
| 945 |
goto finished; |
| 946 |
} |
| 947 |
tail->u.real.w = 0; |
| 948 |
|
| 949 |
/* Look for the dot seperator. */ |
| 950 |
u = format_lex (fmt); |
| 951 |
if (u != FMT_PERIOD) |
| 952 |
{ |
| 953 |
fmt->saved_token = u; |
| 954 |
break; |
| 955 |
} |
| 956 |
|
| 957 |
/* Look for the precision. */ |
| 958 |
u = format_lex (fmt); |
| 959 |
if (u != FMT_ZERO && u != FMT_POSINT) |
| 960 |
{ |
| 961 |
fmt->error = nonneg_required; |
| 962 |
goto finished; |
| 963 |
} |
| 964 |
tail->u.real.d = fmt->value; |
| 965 |
|
| 966 |
/* Look for optional exponent, not allowed for FMT_D */ |
| 967 |
if (t == FMT_D) |
| 968 |
break; |
| 969 |
u = format_lex (fmt); |
| 970 |
if (u != FMT_E) |
| 971 |
fmt->saved_token = u; |
| 972 |
else |
| 973 |
{ |
| 974 |
u = format_lex (fmt); |
| 975 |
if (u != FMT_POSINT) |
| 976 |
{ |
| 977 |
if (u == FMT_ZERO) |
| 978 |
{ |
| 979 |
notify_std (&dtp->common, GFC_STD_F2018, |
| 980 |
"Positive exponent width required"); |
| 981 |
} |
| 982 |
else |
| 983 |
{ |
| 984 |
fmt->error = "Positive exponent width required in " |
| 985 |
"format string at %L"; |
| 986 |
goto finished; |
| 987 |
} |
| 988 |
} |
| 989 |
tail->u.real.e = fmt->value; |
| 990 |
} |
| 991 |
break; |
| 992 |
} |
| 993 |
|
| 994 |
/* Processing for positive width formats. */ |
| 995 |
if (u == FMT_POSINT) |
| 996 |
{ |
| 997 |
tail->u.real.w = fmt->value; |
| 998 |
|
| 999 |
/* Look for the dot separator. Because of legacy behaviors |
| 1000 |
we do some look ahead for missing things. */ |
| 1001 |
t2 = t; |
| 1002 |
t = format_lex (fmt); |
| 1003 |
if (t != FMT_PERIOD) |
| 1004 |
{ |
| 1005 |
/* We treat a missing decimal descriptor as 0. Note: This is only |
| 1006 |
allowed if -std=legacy, otherwise an error occurs. */ |
| 1007 |
if (compile_options.warn_std != 0) |
| 1008 |
{ |
| 1009 |
fmt->error = period_required; |
| 1010 |
goto finished; |
| 1011 |
} |
| 1012 |
fmt->saved_token = t; |
| 1013 |
tail->u.real.d = 0; |
| 1014 |
tail->u.real.e = -1; |
| 1015 |
break; |
| 1016 |
} |
| 1017 |
|
| 1018 |
/* If we made it here, we should have the dot so look for the |
| 1019 |
precision. */ |
| 1020 |
t = format_lex (fmt); |
| 1021 |
if (t != FMT_ZERO && t != FMT_POSINT) |
| 1022 |
{ |
| 1023 |
fmt->error = nonneg_required; |
| 1024 |
goto finished; |
| 1025 |
} |
| 1026 |
tail->u.real.d = fmt->value; |
| 1027 |
tail->u.real.e = -1; |
| 1028 |
|
| 1029 |
/* Done with D and F formats. */ |
| 1030 |
if (t2 == FMT_D || t2 == FMT_F) |
| 1031 |
{ |
| 1032 |
*seen_dd = true; |
| 1033 |
break; |
| 1034 |
} |
| 1035 |
|
| 1036 |
/* Look for optional exponent */ |
| 1037 |
u = format_lex (fmt); |
| 1038 |
if (u != FMT_E) |
| 1039 |
fmt->saved_token = u; |
| 1040 |
else |
| 1041 |
{ |
| 1042 |
u = format_lex (fmt); |
| 1043 |
if (u != FMT_POSINT) |
| 1044 |
{ |
| 1045 |
if (u == FMT_ZERO) |
| 1046 |
{ |
| 1047 |
notify_std (&dtp->common, GFC_STD_F2018, |
| 1048 |
"Positive exponent width required"); |
| 1049 |
} |
| 1050 |
else |
| 1051 |
{ |
| 1052 |
fmt->error = "Positive exponent width required in " |
| 1053 |
"format string at %L"; |
| 1054 |
goto finished; |
| 1055 |
} |
| 1056 |
} |
| 1057 |
tail->u.real.e = fmt->value; |
| 1058 |
} |
| 1059 |
break; |
| 1060 |
} |
| 1061 |
|
| 1062 |
/* Old DEC codes may not have width or precision specified. */ |
| 1063 |
if (dtp->u.p.mode == WRITING && (dtp->common.flags & IOPARM_DT_DEC_EXT)) |
| 1064 |
{ |
| 1065 |
tail->u.real.w = DEFAULT_WIDTH; |
| 1066 |
tail->u.real.d = 0; |
| 1067 |
tail->u.real.e = -1; |
| 1068 |
fmt->saved_token = u; |
| 1069 |
} |
| 1070 |
break; |
| 1071 |
|
| 1072 |
case FMT_DT: |
| 1073 |
*seen_dd = true; |
| 1074 |
get_fnode (fmt, &head, &tail, t); |
| 1075 |
tail->repeat = repeat; |
| 1076 |
|
| 1077 |
t = format_lex (fmt); |
| 1078 |
|
| 1079 |
/* Initialize the vlist to a zero size, rank-one array. */ |
| 1080 |
tail->u.udf.vlist= xmalloc (sizeof(gfc_array_i4) |
| 1081 |
+ sizeof (descriptor_dimension)); |
| 1082 |
GFC_DESCRIPTOR_DATA(tail->u.udf.vlist) = NULL; |
| 1083 |
GFC_DIMENSION_SET(tail->u.udf.vlist->dim[0],1, 0, 0); |
| 1084 |
|
| 1085 |
if (t == FMT_STRING) |
| 1086 |
{ |
| 1087 |
/* Get pointer to the optional format string. */ |
| 1088 |
tail->u.udf.string = fmt->string; |
| 1089 |
tail->u.udf.string_len = fmt->value; |
| 1090 |
t = format_lex (fmt); |
| 1091 |
} |
| 1092 |
if (t == FMT_LPAREN) |
| 1093 |
{ |
| 1094 |
/* Temporary buffer to hold the vlist values. */ |
| 1095 |
GFC_INTEGER_4 temp[FARRAY_SIZE]; |
| 1096 |
int i = 0; |
| 1097 |
loop: |
| 1098 |
t = format_lex (fmt); |
| 1099 |
if (t != FMT_POSINT) |
| 1100 |
{ |
| 1101 |
fmt->error = posint_required; |
| 1102 |
goto finished; |
| 1103 |
} |
| 1104 |
/* Save the positive integer value. */ |
| 1105 |
temp[i++] = fmt->value; |
| 1106 |
t = format_lex (fmt); |
| 1107 |
if (t == FMT_COMMA) |
| 1108 |
goto loop; |
| 1109 |
if (t == FMT_RPAREN) |
| 1110 |
{ |
| 1111 |
/* We have parsed the complete vlist so initialize the |
| 1112 |
array descriptor and save it in the format node. */ |
| 1113 |
gfc_full_array_i4 *vp = tail->u.udf.vlist; |
| 1114 |
GFC_DESCRIPTOR_DATA(vp) = xmalloc (i * sizeof(GFC_INTEGER_4)); |
| 1115 |
GFC_DIMENSION_SET(vp->dim[0],1, i, 1); |
| 1116 |
memcpy (GFC_DESCRIPTOR_DATA(vp), temp, i * sizeof(GFC_INTEGER_4)); |
| 1117 |
break; |
| 1118 |
} |
| 1119 |
fmt->error = unexpected_element; |
| 1120 |
goto finished; |
| 1121 |
} |
| 1122 |
fmt->saved_token = t; |
| 1123 |
break; |
| 1124 |
case FMT_H: |
| 1125 |
if (repeat > fmt->format_string_len) |
| 1126 |
{ |
| 1127 |
fmt->error = bad_hollerith; |
| 1128 |
goto finished; |
| 1129 |
} |
| 1130 |
|
| 1131 |
get_fnode (fmt, &head, &tail, FMT_STRING); |
| 1132 |
tail->u.string.p = fmt->format_string; |
| 1133 |
tail->u.string.length = repeat; |
| 1134 |
tail->repeat = 1; |
| 1135 |
|
| 1136 |
fmt->format_string += fmt->value; |
| 1137 |
fmt->format_string_len -= repeat; |
| 1138 |
|
| 1139 |
break; |
| 1140 |
|
| 1141 |
case FMT_I: |
| 1142 |
case FMT_B: |
| 1143 |
case FMT_O: |
| 1144 |
case FMT_Z: |
| 1145 |
*seen_dd = true; |
| 1146 |
get_fnode (fmt, &head, &tail, t); |
| 1147 |
tail->repeat = repeat; |
| 1148 |
|
| 1149 |
t = format_lex (fmt); |
| 1150 |
|
| 1151 |
if (dtp->u.p.mode == READING) |
| 1152 |
{ |
| 1153 |
if (t != FMT_POSINT) |
| 1154 |
{ |
| 1155 |
if (dtp->common.flags & IOPARM_DT_DEC_EXT) |
| 1156 |
{ |
| 1157 |
tail->u.integer.w = DEFAULT_WIDTH; |
| 1158 |
tail->u.integer.m = -1; |
| 1159 |
fmt->saved_token = t; |
| 1160 |
break; |
| 1161 |
} |
| 1162 |
fmt->error = posint_required; |
| 1163 |
goto finished; |
| 1164 |
} |
| 1165 |
} |
| 1166 |
else |
| 1167 |
{ |
| 1168 |
if (t != FMT_ZERO && t != FMT_POSINT) |
| 1169 |
{ |
| 1170 |
if (dtp->common.flags & IOPARM_DT_DEC_EXT) |
| 1171 |
{ |
| 1172 |
tail->u.integer.w = DEFAULT_WIDTH; |
| 1173 |
tail->u.integer.m = -1; |
| 1174 |
fmt->saved_token = t; |
| 1175 |
break; |
| 1176 |
} |
| 1177 |
fmt->error = nonneg_required; |
| 1178 |
goto finished; |
| 1179 |
} |
| 1180 |
} |
| 1181 |
|
| 1182 |
tail->u.integer.w = fmt->value; |
| 1183 |
tail->u.integer.m = -1; |
| 1184 |
|
| 1185 |
t = format_lex (fmt); |
| 1186 |
if (t != FMT_PERIOD) |
| 1187 |
{ |
| 1188 |
fmt->saved_token = t; |
| 1189 |
} |
| 1190 |
else |
| 1191 |
{ |
| 1192 |
t = format_lex (fmt); |
| 1193 |
if (t != FMT_ZERO && t != FMT_POSINT) |
| 1194 |
{ |
| 1195 |
fmt->error = nonneg_required; |
| 1196 |
goto finished; |
| 1197 |
} |
| 1198 |
|
| 1199 |
tail->u.integer.m = fmt->value; |
| 1200 |
} |
| 1201 |
|
| 1202 |
if (tail->u.integer.w != 0 && tail->u.integer.m > tail->u.integer.w) |
| 1203 |
{ |
| 1204 |
fmt->error = "Minimum digits exceeds field width"; |
| 1205 |
goto finished; |
| 1206 |
} |
| 1207 |
|
| 1208 |
break; |
| 1209 |
|
| 1210 |
default: |
| 1211 |
fmt->error = unexpected_element; |
| 1212 |
goto finished; |
| 1213 |
} |
| 1214 |
|
| 1215 |
/* Between a descriptor and what comes next */ |
| 1216 |
between_desc: |
| 1217 |
t = format_lex (fmt); |
| 1218 |
switch (t) |
| 1219 |
{ |
| 1220 |
case FMT_COMMA: |
| 1221 |
goto format_item; |
| 1222 |
|
| 1223 |
case FMT_RPAREN: |
| 1224 |
goto finished; |
| 1225 |
|
| 1226 |
case FMT_SLASH: |
| 1227 |
case FMT_COLON: |
| 1228 |
get_fnode (fmt, &head, &tail, t); |
| 1229 |
tail->repeat = 1; |
| 1230 |
goto optional_comma; |
| 1231 |
|
| 1232 |
case FMT_END: |
| 1233 |
fmt->error = unexpected_end; |
| 1234 |
goto finished; |
| 1235 |
|
| 1236 |
default: |
| 1237 |
/* Assume a missing comma with -std=legacy, GNU extension. */ |
| 1238 |
if (compile_options.warn_std != 0) |
| 1239 |
fmt->error = comma_missing; |
| 1240 |
goto format_item_1; |
| 1241 |
} |
| 1242 |
|
| 1243 |
/* Optional comma is a weird between state where we've just finished |
| 1244 |
reading a colon, slash or P descriptor. */ |
| 1245 |
optional_comma: |
| 1246 |
t = format_lex (fmt); |
| 1247 |
switch (t) |
| 1248 |
{ |
| 1249 |
case FMT_COMMA: |
| 1250 |
break; |
| 1251 |
|
| 1252 |
case FMT_RPAREN: |
| 1253 |
goto finished; |
| 1254 |
|
| 1255 |
default: /* Assume that we have another format item */ |
| 1256 |
fmt->saved_token = t; |
| 1257 |
break; |
| 1258 |
} |
| 1259 |
|
| 1260 |
goto format_item; |
| 1261 |
|
| 1262 |
finished: |
| 1263 |
|
| 1264 |
return head; |
| 1265 |
} |
| 1266 |
|
| 1267 |
|
| 1268 |
/* format_error()-- Generate an error message for a format statement. |
| 1269 |
If the node that gives the location of the error is NULL, the error |
| 1270 |
is assumed to happen at parse time, and the current location of the |
| 1271 |
parser is shown. |
| 1272 |
|
| 1273 |
We generate a message showing where the problem is. We take extra |
| 1274 |
care to print only the relevant part of the format if it is longer |
| 1275 |
than a standard 80 column display. */ |
| 1276 |
|
| 1277 |
void |
| 1278 |
format_error (st_parameter_dt *dtp, const fnode *f, const char *message) |
| 1279 |
{ |
| 1280 |
int width, i, offset; |
| 1281 |
#define BUFLEN 300 |
| 1282 |
char *p, buffer[BUFLEN]; |
| 1283 |
format_data *fmt = dtp->u.p.fmt; |
| 1284 |
|
| 1285 |
if (f != NULL) |
| 1286 |
p = f->source; |
| 1287 |
else /* This should not happen. */ |
| 1288 |
p = dtp->format; |
| 1289 |
|
| 1290 |
if (message == unexpected_element) |
| 1291 |
snprintf (buffer, BUFLEN, message, fmt->error_element); |
| 1292 |
else |
| 1293 |
snprintf (buffer, BUFLEN, "%s\n", message); |
| 1294 |
|
| 1295 |
/* Get the offset into the format string where the error occurred. */ |
| 1296 |
offset = dtp->format_len - (fmt->reversion_ok ? |
| 1297 |
(int) strlen(p) : fmt->format_string_len); |
| 1298 |
|
| 1299 |
width = dtp->format_len; |
| 1300 |
|
| 1301 |
if (width > 80) |
| 1302 |
width = 80; |
| 1303 |
|
| 1304 |
/* Show the format */ |
| 1305 |
|
| 1306 |
p = strchr (buffer, '\0'); |
| 1307 |
|
| 1308 |
if (dtp->format) |
| 1309 |
memcpy (p, dtp->format, width); |
| 1310 |
|
| 1311 |
p += width; |
| 1312 |
*p++ = '\n'; |
| 1313 |
|
| 1314 |
/* Show where the problem is */ |
| 1315 |
|
| 1316 |
for (i = 1; i < offset; i++) |
| 1317 |
*p++ = ' '; |
| 1318 |
|
| 1319 |
*p++ = '^'; |
| 1320 |
*p = '\0'; |
| 1321 |
|
| 1322 |
generate_error (&dtp->common, LIBERROR_FORMAT, buffer); |
| 1323 |
} |
| 1324 |
|
| 1325 |
|
| 1326 |
/* revert()-- Do reversion of the format. Control reverts to the left |
| 1327 |
parenthesis that matches the rightmost right parenthesis. From our |
| 1328 |
tree structure, we are looking for the rightmost parenthesis node |
| 1329 |
at the second level, the first level always being a single |
| 1330 |
parenthesis node. If this node doesn't exit, we use the top |
| 1331 |
level. */ |
| 1332 |
|
| 1333 |
static void |
| 1334 |
revert (st_parameter_dt *dtp) |
| 1335 |
{ |
| 1336 |
fnode *f, *r; |
| 1337 |
format_data *fmt = dtp->u.p.fmt; |
| 1338 |
|
| 1339 |
dtp->u.p.reversion_flag = 1; |
| 1340 |
|
| 1341 |
r = NULL; |
| 1342 |
|
| 1343 |
for (f = fmt->array.array[0].u.child; f; f = f->next) |
| 1344 |
if (f->format == FMT_LPAREN) |
| 1345 |
r = f; |
| 1346 |
|
| 1347 |
/* If r is NULL because no node was found, the whole tree will be used */ |
| 1348 |
|
| 1349 |
fmt->array.array[0].current = r; |
| 1350 |
fmt->array.array[0].count = 0; |
| 1351 |
} |
| 1352 |
|
| 1353 |
/* parse_format()-- Parse a format string. */ |
| 1354 |
|
| 1355 |
void |
| 1356 |
parse_format (st_parameter_dt *dtp) |
| 1357 |
{ |
| 1358 |
format_data *fmt; |
| 1359 |
bool format_cache_ok, seen_data_desc = false; |
| 1360 |
|
| 1361 |
/* Don't cache for internal units and set an arbitrary limit on the |
| 1362 |
size of format strings we will cache. (Avoids memory issues.) |
| 1363 |
Also, the format_hash_table resides in the current_unit, so |
| 1364 |
child_dtio procedures would overwrite the parent table */ |
| 1365 |
format_cache_ok = !is_internal_unit (dtp) |
| 1366 |
&& (dtp->u.p.current_unit->child_dtio == 0); |
| 1367 |
|
| 1368 |
/* Lookup format string to see if it has already been parsed. */ |
| 1369 |
if (format_cache_ok) |
| 1370 |
{ |
| 1371 |
dtp->u.p.fmt = find_parsed_format (dtp); |
| 1372 |
|
| 1373 |
if (dtp->u.p.fmt != NULL) |
| 1374 |
{ |
| 1375 |
dtp->u.p.fmt->reversion_ok = 0; |
| 1376 |
dtp->u.p.fmt->saved_token = FMT_NONE; |
| 1377 |
dtp->u.p.fmt->saved_format = NULL; |
| 1378 |
reset_fnode_counters (dtp); |
| 1379 |
return; |
| 1380 |
} |
| 1381 |
} |
| 1382 |
|
| 1383 |
/* Not found so proceed as follows. */ |
| 1384 |
|
| 1385 |
char *fmt_string = fc_strdup_notrim (dtp->format, dtp->format_len); |
| 1386 |
dtp->format = fmt_string; |
| 1387 |
|
| 1388 |
dtp->u.p.fmt = fmt = xmalloc (sizeof (format_data)); |
| 1389 |
fmt->format_string = dtp->format; |
| 1390 |
fmt->format_string_len = dtp->format_len; |
| 1391 |
|
| 1392 |
fmt->string = NULL; |
| 1393 |
fmt->saved_token = FMT_NONE; |
| 1394 |
fmt->error = NULL; |
| 1395 |
fmt->value = 0; |
| 1396 |
|
| 1397 |
/* Initialize variables used during traversal of the tree. */ |
| 1398 |
|
| 1399 |
fmt->reversion_ok = 0; |
| 1400 |
fmt->saved_format = NULL; |
| 1401 |
|
| 1402 |
/* Initialize the fnode_array. */ |
| 1403 |
|
| 1404 |
memset (&(fmt->array), 0, sizeof(fmt->array)); |
| 1405 |
|
| 1406 |
/* Allocate the first format node as the root of the tree. */ |
| 1407 |
|
| 1408 |
fmt->last = &fmt->array; |
| 1409 |
fmt->last->next = NULL; |
| 1410 |
fmt->avail = &fmt->array.array[0]; |
| 1411 |
|
| 1412 |
memset (fmt->avail, 0, sizeof (*fmt->avail)); |
| 1413 |
fmt->avail->format = FMT_LPAREN; |
| 1414 |
fmt->avail->repeat = 1; |
| 1415 |
fmt->avail++; |
| 1416 |
|
| 1417 |
if (format_lex (fmt) == FMT_LPAREN) |
| 1418 |
fmt->array.array[0].u.child = parse_format_list (dtp, &seen_data_desc); |
| 1419 |
else |
| 1420 |
fmt->error = "Missing initial left parenthesis in format"; |
| 1421 |
|
| 1422 |
if (format_cache_ok && !fmt->error) |
| 1423 |
save_parsed_format (dtp); |
| 1424 |
else |
| 1425 |
dtp->u.p.format_not_saved = 1; |
| 1426 |
|
| 1427 |
if (fmt->error) |
| 1428 |
format_error (dtp, NULL, fmt->error); |
| 1429 |
} |
| 1430 |
|
| 1431 |
|
| 1432 |
/* next_format0()-- Get the next format node without worrying about |
| 1433 |
reversion. Returns NULL when we hit the end of the list. |
| 1434 |
Parenthesis nodes are incremented after the list has been |
| 1435 |
exhausted, other nodes are incremented before they are returned. */ |
| 1436 |
|
| 1437 |
static const fnode * |
| 1438 |
next_format0 (fnode *f) |
| 1439 |
{ |
| 1440 |
const fnode *r; |
| 1441 |
|
| 1442 |
if (f == NULL) |
| 1443 |
return NULL; |
| 1444 |
|
| 1445 |
if (f->format != FMT_LPAREN) |
| 1446 |
{ |
| 1447 |
f->count++; |
| 1448 |
if (f->count <= f->repeat) |
| 1449 |
return f; |
| 1450 |
|
| 1451 |
f->count = 0; |
| 1452 |
return NULL; |
| 1453 |
} |
| 1454 |
|
| 1455 |
/* Deal with a parenthesis node with unlimited format. */ |
| 1456 |
|
| 1457 |
if (f->repeat == -2) /* -2 signifies unlimited. */ |
| 1458 |
for (;;) |
| 1459 |
{ |
| 1460 |
if (f->current == NULL) |
| 1461 |
f->current = f->u.child; |
| 1462 |
|
| 1463 |
for (; f->current != NULL; f->current = f->current->next) |
| 1464 |
{ |
| 1465 |
r = next_format0 (f->current); |
| 1466 |
if (r != NULL) |
| 1467 |
return r; |
| 1468 |
} |
| 1469 |
} |
| 1470 |
|
| 1471 |
/* Deal with a parenthesis node with specific repeat count. */ |
| 1472 |
for (; f->count < f->repeat; f->count++) |
| 1473 |
{ |
| 1474 |
if (f->current == NULL) |
| 1475 |
f->current = f->u.child; |
| 1476 |
|
| 1477 |
for (; f->current != NULL; f->current = f->current->next) |
| 1478 |
{ |
| 1479 |
r = next_format0 (f->current); |
| 1480 |
if (r != NULL) |
| 1481 |
return r; |
| 1482 |
} |
| 1483 |
} |
| 1484 |
|
| 1485 |
f->count = 0; |
| 1486 |
return NULL; |
| 1487 |
} |
| 1488 |
|
| 1489 |
|
| 1490 |
/* next_format()-- Return the next format node. If the format list |
| 1491 |
ends up being exhausted, we do reversion. Reversion is only |
| 1492 |
allowed if we've seen a data descriptor since the |
| 1493 |
initialization or the last reversion. We return NULL if there |
| 1494 |
are no more data descriptors to return (which is an error |
| 1495 |
condition). */ |
| 1496 |
|
| 1497 |
const fnode * |
| 1498 |
next_format (st_parameter_dt *dtp) |
| 1499 |
{ |
| 1500 |
format_token t; |
| 1501 |
const fnode *f; |
| 1502 |
format_data *fmt = dtp->u.p.fmt; |
| 1503 |
|
| 1504 |
if (fmt->saved_format != NULL) |
| 1505 |
{ /* Deal with a pushed-back format node */ |
| 1506 |
f = fmt->saved_format; |
| 1507 |
fmt->saved_format = NULL; |
| 1508 |
goto done; |
| 1509 |
} |
| 1510 |
|
| 1511 |
f = next_format0 (&fmt->array.array[0]); |
| 1512 |
if (f == NULL) |
| 1513 |
{ |
| 1514 |
if (!fmt->reversion_ok) |
| 1515 |
return NULL; |
| 1516 |
|
| 1517 |
fmt->reversion_ok = 0; |
| 1518 |
revert (dtp); |
| 1519 |
|
| 1520 |
f = next_format0 (&fmt->array.array[0]); |
| 1521 |
if (f == NULL) |
| 1522 |
{ |
| 1523 |
format_error (dtp, NULL, reversion_error); |
| 1524 |
return NULL; |
| 1525 |
} |
| 1526 |
|
| 1527 |
/* Push the first reverted token and return a colon node in case |
| 1528 |
there are no more data items. */ |
| 1529 |
|
| 1530 |
fmt->saved_format = f; |
| 1531 |
return &colon_node; |
| 1532 |
} |
| 1533 |
|
| 1534 |
/* If this is a data edit descriptor, then reversion has become OK. */ |
| 1535 |
done: |
| 1536 |
t = f->format; |
| 1537 |
|
| 1538 |
if (!fmt->reversion_ok && |
| 1539 |
(t == FMT_I || t == FMT_B || t == FMT_O || t == FMT_Z || t == FMT_F || |
| 1540 |
t == FMT_E || t == FMT_EN || t == FMT_ES || t == FMT_G || t == FMT_L || |
| 1541 |
t == FMT_A || t == FMT_D || t == FMT_DT)) |
| 1542 |
fmt->reversion_ok = 1; |
| 1543 |
return f; |
| 1544 |
} |
| 1545 |
|
| 1546 |
|
| 1547 |
/* unget_format()-- Push the given format back so that it will be |
| 1548 |
returned on the next call to next_format() without affecting |
| 1549 |
counts. This is necessary when we've encountered a data |
| 1550 |
descriptor, but don't know what the data item is yet. The format |
| 1551 |
node is pushed back, and we return control to the main program, |
| 1552 |
which calls the library back with the data item (or not). */ |
| 1553 |
|
| 1554 |
void |
| 1555 |
unget_format (st_parameter_dt *dtp, const fnode *f) |
| 1556 |
{ |
| 1557 |
dtp->u.p.fmt->saved_format = f; |
| 1558 |
} |
| 1559 |
|