| 1 |
/* Copyright (C) 2002-2025 Free Software Foundation, Inc. |
| 2 |
Contributed by Andy Vaught |
| 3 |
Namelist transfer functions contributed by Paul Thomas |
| 4 |
F2003 I/O support contributed by Jerry DeLisle |
| 5 |
|
| 6 |
This file is part of the GNU Fortran runtime library (libgfortran). |
| 7 |
|
| 8 |
Libgfortran is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 3, or (at your option) |
| 11 |
any later version. |
| 12 |
|
| 13 |
Libgfortran is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
|
| 18 |
Under Section 7 of GPL version 3, you are granted additional |
| 19 |
permissions described in the GCC Runtime Library Exception, version |
| 20 |
3.1, as published by the Free Software Foundation. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License and |
| 23 |
a copy of the GCC Runtime Library Exception along with this program; |
| 24 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 25 |
<http://www.gnu.org/licenses/>. */ |
| 26 |
|
| 27 |
|
| 28 |
/* transfer.c -- Top level handling of data transfer statements. */ |
| 29 |
|
| 30 |
#include "io.h" |
| 31 |
#include "fbuf.h" |
| 32 |
#include "format.h" |
| 33 |
#include "unix.h" |
| 34 |
#include "async.h" |
| 35 |
#include <string.h> |
| 36 |
#include <errno.h> |
| 37 |
|
| 38 |
|
| 39 |
/* Calling conventions: Data transfer statements are unlike other |
| 40 |
library calls in that they extend over several calls. |
| 41 |
|
| 42 |
The first call is always a call to st_read() or st_write(). These |
| 43 |
subroutines return no status unless a namelist read or write is |
| 44 |
being done, in which case there is the usual status. No further |
| 45 |
calls are necessary in this case. |
| 46 |
|
| 47 |
For other sorts of data transfer, there are zero or more data |
| 48 |
transfer statement that depend on the format of the data transfer |
| 49 |
statement. For READ (and for backwards compatibily: for WRITE), one has |
| 50 |
|
| 51 |
transfer_integer |
| 52 |
transfer_logical |
| 53 |
transfer_character |
| 54 |
transfer_character_wide |
| 55 |
transfer_real |
| 56 |
transfer_complex |
| 57 |
transfer_real128 |
| 58 |
transfer_complex128 |
| 59 |
transfer_unsigned |
| 60 |
|
| 61 |
and for WRITE |
| 62 |
|
| 63 |
transfer_integer_write |
| 64 |
transfer_logical_write |
| 65 |
transfer_character_write |
| 66 |
transfer_character_wide_write |
| 67 |
transfer_real_write |
| 68 |
transfer_complex_write |
| 69 |
transfer_real128_write |
| 70 |
transfer_complex128_write |
| 71 |
transfer_unsigned_write |
| 72 |
|
| 73 |
These subroutines do not return status. The *128 functions |
| 74 |
are in the file transfer128.c. |
| 75 |
|
| 76 |
The last call is a call to st_[read|write]_done(). While |
| 77 |
something can easily go wrong with the initial st_read() or |
| 78 |
st_write(), an error inhibits any data from actually being |
| 79 |
transferred. */ |
| 80 |
|
| 81 |
extern void transfer_integer (st_parameter_dt *, void *, int); |
| 82 |
export_proto(transfer_integer); |
| 83 |
|
| 84 |
extern void transfer_integer_write (st_parameter_dt *, void *, int); |
| 85 |
export_proto(transfer_integer_write); |
| 86 |
|
| 87 |
extern void transfer_unsigned (st_parameter_dt *, void *, int); |
| 88 |
export_proto(transfer_unsigned); |
| 89 |
|
| 90 |
extern void transfer_unsigned_write (st_parameter_dt *, void *, int); |
| 91 |
export_proto(transfer_unsigned_write); |
| 92 |
|
| 93 |
extern void transfer_real (st_parameter_dt *, void *, int); |
| 94 |
export_proto(transfer_real); |
| 95 |
|
| 96 |
extern void transfer_real_write (st_parameter_dt *, void *, int); |
| 97 |
export_proto(transfer_real_write); |
| 98 |
|
| 99 |
extern void transfer_logical (st_parameter_dt *, void *, int); |
| 100 |
export_proto(transfer_logical); |
| 101 |
|
| 102 |
extern void transfer_logical_write (st_parameter_dt *, void *, int); |
| 103 |
export_proto(transfer_logical_write); |
| 104 |
|
| 105 |
extern void transfer_character (st_parameter_dt *, void *, gfc_charlen_type); |
| 106 |
export_proto(transfer_character); |
| 107 |
|
| 108 |
extern void transfer_character_write (st_parameter_dt *, void *, gfc_charlen_type); |
| 109 |
export_proto(transfer_character_write); |
| 110 |
|
| 111 |
extern void transfer_character_wide (st_parameter_dt *, void *, gfc_charlen_type, int); |
| 112 |
export_proto(transfer_character_wide); |
| 113 |
|
| 114 |
extern void transfer_character_wide_write (st_parameter_dt *, |
| 115 |
void *, gfc_charlen_type, int); |
| 116 |
export_proto(transfer_character_wide_write); |
| 117 |
|
| 118 |
extern void transfer_complex (st_parameter_dt *, void *, int); |
| 119 |
export_proto(transfer_complex); |
| 120 |
|
| 121 |
extern void transfer_complex_write (st_parameter_dt *, void *, int); |
| 122 |
export_proto(transfer_complex_write); |
| 123 |
|
| 124 |
extern void transfer_array (st_parameter_dt *, gfc_array_char *, int, |
| 125 |
gfc_charlen_type); |
| 126 |
export_proto(transfer_array); |
| 127 |
|
| 128 |
extern void transfer_array_write (st_parameter_dt *, gfc_array_char *, int, |
| 129 |
gfc_charlen_type); |
| 130 |
export_proto(transfer_array_write); |
| 131 |
|
| 132 |
/* User defined derived type input/output. */ |
| 133 |
extern void |
| 134 |
transfer_derived (st_parameter_dt *dtp, void *dtio_source, void *dtio_proc); |
| 135 |
export_proto(transfer_derived); |
| 136 |
|
| 137 |
extern void |
| 138 |
transfer_derived_write (st_parameter_dt *dtp, void *dtio_source, void *dtio_proc); |
| 139 |
export_proto(transfer_derived_write); |
| 140 |
|
| 141 |
static void us_read (st_parameter_dt *, int); |
| 142 |
static void us_write (st_parameter_dt *, int); |
| 143 |
static void next_record_r_unf (st_parameter_dt *, int); |
| 144 |
static void next_record_w_unf (st_parameter_dt *, int); |
| 145 |
|
| 146 |
static const st_option advance_opt[] = { |
| 147 |
{"yes", ADVANCE_YES}, |
| 148 |
{"no", ADVANCE_NO}, |
| 149 |
{NULL, 0} |
| 150 |
}; |
| 151 |
|
| 152 |
|
| 153 |
static const st_option decimal_opt[] = { |
| 154 |
{"point", DECIMAL_POINT}, |
| 155 |
{"comma", DECIMAL_COMMA}, |
| 156 |
{NULL, 0} |
| 157 |
}; |
| 158 |
|
| 159 |
static const st_option round_opt[] = { |
| 160 |
{"up", ROUND_UP}, |
| 161 |
{"down", ROUND_DOWN}, |
| 162 |
{"zero", ROUND_ZERO}, |
| 163 |
{"nearest", ROUND_NEAREST}, |
| 164 |
{"compatible", ROUND_COMPATIBLE}, |
| 165 |
{"processor_defined", ROUND_PROCDEFINED}, |
| 166 |
{NULL, 0} |
| 167 |
}; |
| 168 |
|
| 169 |
|
| 170 |
static const st_option sign_opt[] = { |
| 171 |
{"plus", SIGN_SP}, |
| 172 |
{"suppress", SIGN_SS}, |
| 173 |
{"processor_defined", SIGN_S}, |
| 174 |
{NULL, 0} |
| 175 |
}; |
| 176 |
|
| 177 |
static const st_option blank_opt[] = { |
| 178 |
{"null", BLANK_NULL}, |
| 179 |
{"zero", BLANK_ZERO}, |
| 180 |
{NULL, 0} |
| 181 |
}; |
| 182 |
|
| 183 |
static const st_option delim_opt[] = { |
| 184 |
{"apostrophe", DELIM_APOSTROPHE}, |
| 185 |
{"quote", DELIM_QUOTE}, |
| 186 |
{"none", DELIM_NONE}, |
| 187 |
{NULL, 0} |
| 188 |
}; |
| 189 |
|
| 190 |
static const st_option pad_opt[] = { |
| 191 |
{"yes", PAD_YES}, |
| 192 |
{"no", PAD_NO}, |
| 193 |
{NULL, 0} |
| 194 |
}; |
| 195 |
|
| 196 |
static const st_option async_opt[] = { |
| 197 |
{"yes", ASYNC_YES}, |
| 198 |
{"no", ASYNC_NO}, |
| 199 |
{NULL, 0} |
| 200 |
}; |
| 201 |
|
| 202 |
typedef enum |
| 203 |
{ FORMATTED_SEQUENTIAL, UNFORMATTED_SEQUENTIAL, |
| 204 |
FORMATTED_DIRECT, UNFORMATTED_DIRECT, FORMATTED_STREAM, |
| 205 |
UNFORMATTED_STREAM, FORMATTED_UNSPECIFIED |
| 206 |
} |
| 207 |
file_mode; |
| 208 |
|
| 209 |
|
| 210 |
static file_mode |
| 211 |
current_mode (st_parameter_dt *dtp) |
| 212 |
{ |
| 213 |
file_mode m; |
| 214 |
|
| 215 |
m = FORMATTED_UNSPECIFIED; |
| 216 |
|
| 217 |
if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT) |
| 218 |
{ |
| 219 |
m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ? |
| 220 |
FORMATTED_DIRECT : UNFORMATTED_DIRECT; |
| 221 |
} |
| 222 |
else if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL) |
| 223 |
{ |
| 224 |
m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ? |
| 225 |
FORMATTED_SEQUENTIAL : UNFORMATTED_SEQUENTIAL; |
| 226 |
} |
| 227 |
else if (dtp->u.p.current_unit->flags.access == ACCESS_STREAM) |
| 228 |
{ |
| 229 |
m = dtp->u.p.current_unit->flags.form == FORM_FORMATTED ? |
| 230 |
FORMATTED_STREAM : UNFORMATTED_STREAM; |
| 231 |
} |
| 232 |
|
| 233 |
return m; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
/* Mid level data transfer statements. */ |
| 238 |
|
| 239 |
/* Read sequential file - internal unit */ |
| 240 |
|
| 241 |
static char * |
| 242 |
read_sf_internal (st_parameter_dt *dtp, size_t *length) |
| 243 |
{ |
| 244 |
static char *empty_string[0]; |
| 245 |
char *base = NULL; |
| 246 |
size_t lorig; |
| 247 |
|
| 248 |
/* Zero size array gives internal unit len of 0. Nothing to read. */ |
| 249 |
if (dtp->internal_unit_len == 0 |
| 250 |
&& dtp->u.p.current_unit->pad_status == PAD_NO) |
| 251 |
hit_eof (dtp); |
| 252 |
|
| 253 |
/* There are some cases with mixed DTIO where we have read a character |
| 254 |
and saved it in the last character buffer, so we need to backup. */ |
| 255 |
if (unlikely (dtp->u.p.current_unit->child_dtio > 0 && |
| 256 |
dtp->u.p.current_unit->last_char != EOF - 1)) |
| 257 |
{ |
| 258 |
dtp->u.p.current_unit->last_char = EOF - 1; |
| 259 |
sseek (dtp->u.p.current_unit->s, -1, SEEK_CUR); |
| 260 |
} |
| 261 |
|
| 262 |
/* To support legacy code we have to scan the input string one byte |
| 263 |
at a time because we don't know where an early comma may be and the |
| 264 |
requested length could go past the end of a comma shortened |
| 265 |
string. We only do this if -std=legacy was given at compile |
| 266 |
time. We also do not support this on kind=4 strings. */ |
| 267 |
if (unlikely(compile_options.warn_std == 0)) // the slow legacy way. |
| 268 |
{ |
| 269 |
size_t n; |
| 270 |
size_t tmp = 1; |
| 271 |
char *q; |
| 272 |
|
| 273 |
/* If we have seen an eor previously, return a length of 0. The |
| 274 |
caller is responsible for correctly padding the input field. */ |
| 275 |
if (dtp->u.p.sf_seen_eor) |
| 276 |
{ |
| 277 |
*length = 0; |
| 278 |
/* Just return something that isn't a NULL pointer, otherwise the |
| 279 |
caller thinks an error occurred. */ |
| 280 |
return (char*) empty_string; |
| 281 |
} |
| 282 |
|
| 283 |
/* Get the first character of the string to establish the base |
| 284 |
address and check for comma or end-of-record condition. */ |
| 285 |
base = mem_alloc_r (dtp->u.p.current_unit->s, &tmp); |
| 286 |
if (tmp == 0) |
| 287 |
{ |
| 288 |
dtp->u.p.sf_seen_eor = 1; |
| 289 |
*length = 0; |
| 290 |
return (char*) empty_string; |
| 291 |
} |
| 292 |
if (*base == ',') |
| 293 |
{ |
| 294 |
dtp->u.p.current_unit->bytes_left--; |
| 295 |
*length = 0; |
| 296 |
return (char*) empty_string; |
| 297 |
} |
| 298 |
|
| 299 |
/* Now we scan the rest and deal with either an end-of-file |
| 300 |
condition or a comma, as needed. */ |
| 301 |
for (n = 1; n < *length; n++) |
| 302 |
{ |
| 303 |
q = mem_alloc_r (dtp->u.p.current_unit->s, &tmp); |
| 304 |
if (tmp == 0) |
| 305 |
{ |
| 306 |
hit_eof (dtp); |
| 307 |
return NULL; |
| 308 |
} |
| 309 |
if (*q == ',') |
| 310 |
{ |
| 311 |
dtp->u.p.current_unit->bytes_left -= n; |
| 312 |
*length = n; |
| 313 |
break; |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
else // the fast way |
| 318 |
{ |
| 319 |
lorig = *length; |
| 320 |
if (is_char4_unit(dtp)) |
| 321 |
{ |
| 322 |
gfc_char4_t *p = (gfc_char4_t *) mem_alloc_r4 (dtp->u.p.current_unit->s, |
| 323 |
length); |
| 324 |
base = fbuf_alloc (dtp->u.p.current_unit, lorig); |
| 325 |
for (size_t i = 0; i < *length; i++, p++) |
| 326 |
base[i] = *p > 255 ? '?' : (unsigned char) *p; |
| 327 |
} |
| 328 |
else |
| 329 |
base = mem_alloc_r (dtp->u.p.current_unit->s, length); |
| 330 |
|
| 331 |
if (unlikely (lorig > *length)) |
| 332 |
{ |
| 333 |
hit_eof (dtp); |
| 334 |
return NULL; |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
dtp->u.p.current_unit->bytes_left -= *length; |
| 339 |
|
| 340 |
if (((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0) || |
| 341 |
dtp->u.p.current_unit->has_size) |
| 342 |
dtp->u.p.current_unit->size_used += (GFC_IO_INT) *length; |
| 343 |
|
| 344 |
return base; |
| 345 |
|
| 346 |
} |
| 347 |
|
| 348 |
/* When reading sequential formatted records we have a problem. We |
| 349 |
don't know how long the line is until we read the trailing newline, |
| 350 |
and we don't want to read too much. If we read too much, we might |
| 351 |
have to do a physical seek backwards depending on how much data is |
| 352 |
present, and devices like terminals aren't seekable and would cause |
| 353 |
an I/O error. |
| 354 |
|
| 355 |
Given this, the solution is to read a byte at a time, stopping if |
| 356 |
we hit the newline. For small allocations, we use a static buffer. |
| 357 |
For larger allocations, we are forced to allocate memory on the |
| 358 |
heap. Hopefully this won't happen very often. */ |
| 359 |
|
| 360 |
/* Read sequential file - external unit */ |
| 361 |
|
| 362 |
static char * |
| 363 |
read_sf (st_parameter_dt *dtp, size_t *length) |
| 364 |
{ |
| 365 |
static char *empty_string[0]; |
| 366 |
size_t lorig, n; |
| 367 |
int q, q2; |
| 368 |
int seen_comma; |
| 369 |
|
| 370 |
/* If we have seen an eor previously, return a length of 0. The |
| 371 |
caller is responsible for correctly padding the input field. */ |
| 372 |
if (dtp->u.p.sf_seen_eor) |
| 373 |
{ |
| 374 |
*length = 0; |
| 375 |
/* Just return something that isn't a NULL pointer, otherwise the |
| 376 |
caller thinks an error occurred. */ |
| 377 |
return (char*) empty_string; |
| 378 |
} |
| 379 |
|
| 380 |
/* There are some cases with mixed DTIO where we have read a character |
| 381 |
and saved it in the last character buffer, so we need to backup. */ |
| 382 |
if (unlikely (dtp->u.p.current_unit->child_dtio > 0 && |
| 383 |
dtp->u.p.current_unit->last_char != EOF - 1)) |
| 384 |
{ |
| 385 |
dtp->u.p.current_unit->last_char = EOF - 1; |
| 386 |
fbuf_seek (dtp->u.p.current_unit, -1, SEEK_CUR); |
| 387 |
} |
| 388 |
|
| 389 |
n = seen_comma = 0; |
| 390 |
|
| 391 |
/* Read data into format buffer and scan through it. */ |
| 392 |
lorig = *length; |
| 393 |
|
| 394 |
while (n < *length) |
| 395 |
{ |
| 396 |
q = fbuf_getc (dtp->u.p.current_unit); |
| 397 |
if (q == EOF) |
| 398 |
break; |
| 399 |
else if (dtp->u.p.current_unit->flags.cc != CC_NONE |
| 400 |
&& (q == '\n' || q == '\r')) |
| 401 |
{ |
| 402 |
/* Unexpected end of line. Set the position. */ |
| 403 |
dtp->u.p.sf_seen_eor = 1; |
| 404 |
|
| 405 |
/* If we see an EOR during non-advancing I/O, we need to skip |
| 406 |
the rest of the I/O statement. Set the corresponding flag. */ |
| 407 |
if (dtp->u.p.advance_status == ADVANCE_NO || dtp->u.p.seen_dollar) |
| 408 |
dtp->u.p.eor_condition = 1; |
| 409 |
|
| 410 |
/* If we encounter a CR, it might be a CRLF. */ |
| 411 |
if (q == '\r') /* Probably a CRLF */ |
| 412 |
{ |
| 413 |
/* See if there is an LF. */ |
| 414 |
q2 = fbuf_getc (dtp->u.p.current_unit); |
| 415 |
if (q2 == '\n') |
| 416 |
dtp->u.p.sf_seen_eor = 2; |
| 417 |
else if (q2 != EOF) /* Oops, seek back. */ |
| 418 |
fbuf_seek (dtp->u.p.current_unit, -1, SEEK_CUR); |
| 419 |
} |
| 420 |
|
| 421 |
/* Without padding, terminate the I/O statement without assigning |
| 422 |
the value. With padding, the value still needs to be assigned, |
| 423 |
so we can just continue with a short read. */ |
| 424 |
if (dtp->u.p.current_unit->pad_status == PAD_NO) |
| 425 |
{ |
| 426 |
generate_error (&dtp->common, LIBERROR_EOR, NULL); |
| 427 |
return NULL; |
| 428 |
} |
| 429 |
|
| 430 |
*length = n; |
| 431 |
goto done; |
| 432 |
} |
| 433 |
/* Short circuit the read if a comma is found during numeric input. |
| 434 |
The flag is set to zero during character reads so that commas in |
| 435 |
strings are not ignored */ |
| 436 |
else if (q == ',') |
| 437 |
if (dtp->u.p.sf_read_comma == 1) |
| 438 |
{ |
| 439 |
seen_comma = 1; |
| 440 |
notify_std (&dtp->common, GFC_STD_GNU, |
| 441 |
"Comma in formatted numeric read."); |
| 442 |
break; |
| 443 |
} |
| 444 |
n++; |
| 445 |
} |
| 446 |
|
| 447 |
*length = n; |
| 448 |
|
| 449 |
/* A short read implies we hit EOF, unless we hit EOR, a comma, or |
| 450 |
some other stuff. Set the relevant flags. */ |
| 451 |
if (lorig > *length && !dtp->u.p.sf_seen_eor && !seen_comma) |
| 452 |
{ |
| 453 |
if (n > 0) |
| 454 |
{ |
| 455 |
if (dtp->u.p.advance_status == ADVANCE_NO) |
| 456 |
{ |
| 457 |
if (dtp->u.p.current_unit->pad_status == PAD_NO) |
| 458 |
{ |
| 459 |
hit_eof (dtp); |
| 460 |
return NULL; |
| 461 |
} |
| 462 |
else |
| 463 |
dtp->u.p.eor_condition = 1; |
| 464 |
} |
| 465 |
else |
| 466 |
dtp->u.p.at_eof = 1; |
| 467 |
} |
| 468 |
else if (dtp->u.p.advance_status == ADVANCE_NO |
| 469 |
|| dtp->u.p.current_unit->pad_status == PAD_NO |
| 470 |
|| dtp->u.p.current_unit->bytes_left |
| 471 |
== dtp->u.p.current_unit->recl) |
| 472 |
{ |
| 473 |
hit_eof (dtp); |
| 474 |
return NULL; |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
done: |
| 479 |
|
| 480 |
dtp->u.p.current_unit->bytes_left -= n; |
| 481 |
|
| 482 |
if (((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0) || |
| 483 |
dtp->u.p.current_unit->has_size) |
| 484 |
dtp->u.p.current_unit->size_used += (GFC_IO_INT) n; |
| 485 |
|
| 486 |
/* We can't call fbuf_getptr before the loop doing fbuf_getc, because |
| 487 |
fbuf_getc might reallocate the buffer. So return current pointer |
| 488 |
minus all the advances, which is n plus up to two characters |
| 489 |
of newline or comma. */ |
| 490 |
return fbuf_getptr (dtp->u.p.current_unit) |
| 491 |
- n - dtp->u.p.sf_seen_eor - seen_comma; |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
/* Function for reading the next couple of bytes from the current |
| 496 |
file, advancing the current position. We return NULL on end of record or |
| 497 |
end of file. This function is only for formatted I/O, unformatted uses |
| 498 |
read_block_direct. |
| 499 |
|
| 500 |
If the read is short, then it is because the current record does not |
| 501 |
have enough data to satisfy the read request and the file was |
| 502 |
opened with PAD=YES. The caller must assume trailing spaces for |
| 503 |
short reads. */ |
| 504 |
|
| 505 |
void * |
| 506 |
read_block_form (st_parameter_dt *dtp, size_t *nbytes) |
| 507 |
{ |
| 508 |
char *source; |
| 509 |
size_t norig; |
| 510 |
|
| 511 |
if (!is_stream_io (dtp)) |
| 512 |
{ |
| 513 |
if (dtp->u.p.current_unit->bytes_left < (gfc_offset) *nbytes) |
| 514 |
{ |
| 515 |
/* For preconnected units with default record length, set bytes left |
| 516 |
to unit record length and proceed, otherwise error. */ |
| 517 |
if (dtp->u.p.current_unit->unit_number == options.stdin_unit |
| 518 |
&& dtp->u.p.current_unit->recl == default_recl) |
| 519 |
dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl; |
| 520 |
else |
| 521 |
{ |
| 522 |
if (unlikely (dtp->u.p.current_unit->pad_status == PAD_NO) |
| 523 |
&& !is_internal_unit (dtp)) |
| 524 |
{ |
| 525 |
/* Not enough data left. */ |
| 526 |
generate_error (&dtp->common, LIBERROR_EOR, NULL); |
| 527 |
return NULL; |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
if (is_internal_unit(dtp)) |
| 532 |
{ |
| 533 |
if (*nbytes > 0 && dtp->u.p.current_unit->bytes_left == 0) |
| 534 |
{ |
| 535 |
if (dtp->u.p.advance_status == ADVANCE_NO) |
| 536 |
{ |
| 537 |
generate_error (&dtp->common, LIBERROR_EOR, NULL); |
| 538 |
return NULL; |
| 539 |
} |
| 540 |
} |
| 541 |
} |
| 542 |
else |
| 543 |
{ |
| 544 |
if (unlikely (dtp->u.p.current_unit->bytes_left == 0)) |
| 545 |
{ |
| 546 |
hit_eof (dtp); |
| 547 |
return NULL; |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
*nbytes = dtp->u.p.current_unit->bytes_left; |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED && |
| 556 |
(dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL || |
| 557 |
dtp->u.p.current_unit->flags.access == ACCESS_STREAM)) |
| 558 |
{ |
| 559 |
if (is_internal_unit (dtp)) |
| 560 |
source = read_sf_internal (dtp, nbytes); |
| 561 |
else |
| 562 |
source = read_sf (dtp, nbytes); |
| 563 |
|
| 564 |
dtp->u.p.current_unit->strm_pos += |
| 565 |
(gfc_offset) (*nbytes + dtp->u.p.sf_seen_eor); |
| 566 |
return source; |
| 567 |
} |
| 568 |
|
| 569 |
/* If we reach here, we can assume it's direct access. */ |
| 570 |
|
| 571 |
dtp->u.p.current_unit->bytes_left -= (gfc_offset) *nbytes; |
| 572 |
|
| 573 |
norig = *nbytes; |
| 574 |
source = fbuf_read (dtp->u.p.current_unit, nbytes); |
| 575 |
fbuf_seek (dtp->u.p.current_unit, *nbytes, SEEK_CUR); |
| 576 |
|
| 577 |
if (((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0) || |
| 578 |
dtp->u.p.current_unit->has_size) |
| 579 |
dtp->u.p.current_unit->size_used += (GFC_IO_INT) *nbytes; |
| 580 |
|
| 581 |
if (norig != *nbytes) |
| 582 |
{ |
| 583 |
/* Short read, this shouldn't happen. */ |
| 584 |
if (dtp->u.p.current_unit->pad_status == PAD_NO) |
| 585 |
{ |
| 586 |
generate_error (&dtp->common, LIBERROR_EOR, NULL); |
| 587 |
source = NULL; |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
dtp->u.p.current_unit->strm_pos += (gfc_offset) *nbytes; |
| 592 |
|
| 593 |
return source; |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
/* Read a block from a character(kind=4) internal unit, to be transferred into |
| 598 |
a character(kind=4) variable. Note: Portions of this code borrowed from |
| 599 |
read_sf_internal. */ |
| 600 |
void * |
| 601 |
read_block_form4 (st_parameter_dt *dtp, size_t *nbytes) |
| 602 |
{ |
| 603 |
static gfc_char4_t *empty_string[0]; |
| 604 |
gfc_char4_t *source; |
| 605 |
size_t lorig; |
| 606 |
|
| 607 |
if (dtp->u.p.current_unit->bytes_left < (gfc_offset) *nbytes) |
| 608 |
*nbytes = dtp->u.p.current_unit->bytes_left; |
| 609 |
|
| 610 |
/* Zero size array gives internal unit len of 0. Nothing to read. */ |
| 611 |
if (dtp->internal_unit_len == 0 |
| 612 |
&& dtp->u.p.current_unit->pad_status == PAD_NO) |
| 613 |
hit_eof (dtp); |
| 614 |
|
| 615 |
/* If we have seen an eor previously, return a length of 0. The |
| 616 |
caller is responsible for correctly padding the input field. */ |
| 617 |
if (dtp->u.p.sf_seen_eor) |
| 618 |
{ |
| 619 |
*nbytes = 0; |
| 620 |
/* Just return something that isn't a NULL pointer, otherwise the |
| 621 |
caller thinks an error occurred. */ |
| 622 |
return empty_string; |
| 623 |
} |
| 624 |
|
| 625 |
lorig = *nbytes; |
| 626 |
source = (gfc_char4_t *) mem_alloc_r4 (dtp->u.p.current_unit->s, nbytes); |
| 627 |
|
| 628 |
if (unlikely (lorig > *nbytes)) |
| 629 |
{ |
| 630 |
hit_eof (dtp); |
| 631 |
return NULL; |
| 632 |
} |
| 633 |
|
| 634 |
dtp->u.p.current_unit->bytes_left -= *nbytes; |
| 635 |
|
| 636 |
if (((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0) || |
| 637 |
dtp->u.p.current_unit->has_size) |
| 638 |
dtp->u.p.current_unit->size_used += (GFC_IO_INT) *nbytes; |
| 639 |
|
| 640 |
return source; |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
/* Reads a block directly into application data space. This is for |
| 645 |
unformatted files. */ |
| 646 |
|
| 647 |
static void |
| 648 |
read_block_direct (st_parameter_dt *dtp, void *buf, size_t nbytes) |
| 649 |
{ |
| 650 |
ssize_t to_read_record; |
| 651 |
ssize_t have_read_record; |
| 652 |
ssize_t to_read_subrecord; |
| 653 |
ssize_t have_read_subrecord; |
| 654 |
int short_record; |
| 655 |
|
| 656 |
if (is_stream_io (dtp)) |
| 657 |
{ |
| 658 |
have_read_record = sread (dtp->u.p.current_unit->s, buf, |
| 659 |
nbytes); |
| 660 |
if (unlikely (have_read_record < 0)) |
| 661 |
{ |
| 662 |
generate_error (&dtp->common, LIBERROR_OS, NULL); |
| 663 |
return; |
| 664 |
} |
| 665 |
|
| 666 |
dtp->u.p.current_unit->strm_pos += (gfc_offset) have_read_record; |
| 667 |
|
| 668 |
if (unlikely ((ssize_t) nbytes != have_read_record)) |
| 669 |
{ |
| 670 |
/* Short read, e.g. if we hit EOF. For stream files, |
| 671 |
we have to set the end-of-file condition. */ |
| 672 |
hit_eof (dtp); |
| 673 |
} |
| 674 |
return; |
| 675 |
} |
| 676 |
|
| 677 |
if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT) |
| 678 |
{ |
| 679 |
if (dtp->u.p.current_unit->bytes_left < (gfc_offset) nbytes) |
| 680 |
{ |
| 681 |
short_record = 1; |
| 682 |
to_read_record = dtp->u.p.current_unit->bytes_left; |
| 683 |
nbytes = to_read_record; |
| 684 |
} |
| 685 |
else |
| 686 |
{ |
| 687 |
short_record = 0; |
| 688 |
to_read_record = nbytes; |
| 689 |
} |
| 690 |
|
| 691 |
dtp->u.p.current_unit->bytes_left -= to_read_record; |
| 692 |
|
| 693 |
to_read_record = sread (dtp->u.p.current_unit->s, buf, to_read_record); |
| 694 |
if (unlikely (to_read_record < 0)) |
| 695 |
{ |
| 696 |
generate_error (&dtp->common, LIBERROR_OS, NULL); |
| 697 |
return; |
| 698 |
} |
| 699 |
|
| 700 |
if (to_read_record != (ssize_t) nbytes) |
| 701 |
{ |
| 702 |
/* Short read, e.g. if we hit EOF. Apparently, we read |
| 703 |
more than was written to the last record. */ |
| 704 |
return; |
| 705 |
} |
| 706 |
|
| 707 |
if (unlikely (short_record)) |
| 708 |
{ |
| 709 |
generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL); |
| 710 |
} |
| 711 |
return; |
| 712 |
} |
| 713 |
|
| 714 |
/* Unformatted sequential. We loop over the subrecords, reading |
| 715 |
until the request has been fulfilled or the record has run out |
| 716 |
of continuation subrecords. */ |
| 717 |
|
| 718 |
/* Check whether we exceed the total record length. */ |
| 719 |
|
| 720 |
if (dtp->u.p.current_unit->flags.has_recl |
| 721 |
&& ((gfc_offset) nbytes > dtp->u.p.current_unit->bytes_left)) |
| 722 |
{ |
| 723 |
to_read_record = dtp->u.p.current_unit->bytes_left; |
| 724 |
short_record = 1; |
| 725 |
} |
| 726 |
else |
| 727 |
{ |
| 728 |
to_read_record = nbytes; |
| 729 |
short_record = 0; |
| 730 |
} |
| 731 |
have_read_record = 0; |
| 732 |
|
| 733 |
while(1) |
| 734 |
{ |
| 735 |
if (dtp->u.p.current_unit->bytes_left_subrecord |
| 736 |
< (gfc_offset) to_read_record) |
| 737 |
{ |
| 738 |
to_read_subrecord = dtp->u.p.current_unit->bytes_left_subrecord; |
| 739 |
to_read_record -= to_read_subrecord; |
| 740 |
} |
| 741 |
else |
| 742 |
{ |
| 743 |
to_read_subrecord = to_read_record; |
| 744 |
to_read_record = 0; |
| 745 |
} |
| 746 |
|
| 747 |
dtp->u.p.current_unit->bytes_left_subrecord -= to_read_subrecord; |
| 748 |
|
| 749 |
have_read_subrecord = sread (dtp->u.p.current_unit->s, |
| 750 |
buf + have_read_record, to_read_subrecord); |
| 751 |
if (unlikely (have_read_subrecord < 0)) |
| 752 |
{ |
| 753 |
generate_error (&dtp->common, LIBERROR_OS, NULL); |
| 754 |
return; |
| 755 |
} |
| 756 |
|
| 757 |
have_read_record += have_read_subrecord; |
| 758 |
|
| 759 |
if (unlikely (to_read_subrecord != have_read_subrecord)) |
| 760 |
{ |
| 761 |
/* Short read, e.g. if we hit EOF. This means the record |
| 762 |
structure has been corrupted, or the trailing record |
| 763 |
marker would still be present. */ |
| 764 |
|
| 765 |
generate_error (&dtp->common, LIBERROR_CORRUPT_FILE, NULL); |
| 766 |
return; |
| 767 |
} |
| 768 |
|
| 769 |
if (to_read_record > 0) |
| 770 |
{ |
| 771 |
if (likely (dtp->u.p.current_unit->continued)) |
| 772 |
{ |
| 773 |
next_record_r_unf (dtp, 0); |
| 774 |
us_read (dtp, 1); |
| 775 |
} |
| 776 |
else |
| 777 |
{ |
| 778 |
/* Let's make sure the file position is correctly pre-positioned |
| 779 |
for the next read statement. */ |
| 780 |
|
| 781 |
dtp->u.p.current_unit->current_record = 0; |
| 782 |
next_record_r_unf (dtp, 0); |
| 783 |
generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL); |
| 784 |
return; |
| 785 |
} |
| 786 |
} |
| 787 |
else |
| 788 |
{ |
| 789 |
/* Normal exit, the read request has been fulfilled. */ |
| 790 |
break; |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
dtp->u.p.current_unit->bytes_left -= have_read_record; |
| 795 |
if (unlikely (short_record)) |
| 796 |
{ |
| 797 |
generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL); |
| 798 |
return; |
| 799 |
} |
| 800 |
return; |
| 801 |
} |
| 802 |
|
| 803 |
|
| 804 |
/* Function for writing a block of bytes to the current file at the |
| 805 |
current position, advancing the file pointer. We are given a length |
| 806 |
and return a pointer to a buffer that the caller must (completely) |
| 807 |
fill in. Returns NULL on error. */ |
| 808 |
|
| 809 |
void * |
| 810 |
write_block (st_parameter_dt *dtp, size_t length) |
| 811 |
{ |
| 812 |
char *dest; |
| 813 |
|
| 814 |
if (!is_stream_io (dtp)) |
| 815 |
{ |
| 816 |
if (dtp->u.p.current_unit->bytes_left < (gfc_offset) length) |
| 817 |
{ |
| 818 |
/* For preconnected units with default record length, set bytes left |
| 819 |
to unit record length and proceed, otherwise error. */ |
| 820 |
if (likely ((dtp->u.p.current_unit->unit_number |
| 821 |
== options.stdout_unit |
| 822 |
|| dtp->u.p.current_unit->unit_number |
| 823 |
== options.stderr_unit) |
| 824 |
&& dtp->u.p.current_unit->recl == default_recl)) |
| 825 |
dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl; |
| 826 |
else |
| 827 |
{ |
| 828 |
generate_error (&dtp->common, LIBERROR_EOR, NULL); |
| 829 |
return NULL; |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
dtp->u.p.current_unit->bytes_left -= (gfc_offset) length; |
| 834 |
} |
| 835 |
|
| 836 |
if (is_internal_unit (dtp)) |
| 837 |
{ |
| 838 |
if (is_char4_unit(dtp)) /* char4 internel unit. */ |
| 839 |
{ |
| 840 |
gfc_char4_t *dest4; |
| 841 |
dest4 = mem_alloc_w4 (dtp->u.p.current_unit->s, &length); |
| 842 |
if (dest4 == NULL) |
| 843 |
{ |
| 844 |
generate_error (&dtp->common, LIBERROR_END, NULL); |
| 845 |
return NULL; |
| 846 |
} |
| 847 |
return dest4; |
| 848 |
} |
| 849 |
else |
| 850 |
dest = mem_alloc_w (dtp->u.p.current_unit->s, &length); |
| 851 |
|
| 852 |
if (dest == NULL) |
| 853 |
{ |
| 854 |
generate_error (&dtp->common, LIBERROR_END, NULL); |
| 855 |
return NULL; |
| 856 |
} |
| 857 |
|
| 858 |
if (unlikely (dtp->u.p.current_unit->endfile == AT_ENDFILE)) |
| 859 |
generate_error (&dtp->common, LIBERROR_END, NULL); |
| 860 |
} |
| 861 |
else |
| 862 |
{ |
| 863 |
dest = fbuf_alloc (dtp->u.p.current_unit, length); |
| 864 |
if (dest == NULL) |
| 865 |
{ |
| 866 |
generate_error (&dtp->common, LIBERROR_OS, NULL); |
| 867 |
return NULL; |
| 868 |
} |
| 869 |
} |
| 870 |
|
| 871 |
if (((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0) || |
| 872 |
dtp->u.p.current_unit->has_size) |
| 873 |
dtp->u.p.current_unit->size_used += (GFC_IO_INT) length; |
| 874 |
|
| 875 |
dtp->u.p.current_unit->strm_pos += (gfc_offset) length; |
| 876 |
|
| 877 |
return dest; |
| 878 |
} |
| 879 |
|
| 880 |
|
| 881 |
/* High level interface to swrite(), taking care of errors. This is only |
| 882 |
called for unformatted files. There are three cases to consider: |
| 883 |
Stream I/O, unformatted direct, unformatted sequential. */ |
| 884 |
|
| 885 |
static bool |
| 886 |
write_buf (st_parameter_dt *dtp, void *buf, size_t nbytes) |
| 887 |
{ |
| 888 |
|
| 889 |
ssize_t have_written; |
| 890 |
ssize_t to_write_subrecord; |
| 891 |
int short_record; |
| 892 |
|
| 893 |
/* Stream I/O. */ |
| 894 |
|
| 895 |
if (is_stream_io (dtp)) |
| 896 |
{ |
| 897 |
have_written = swrite (dtp->u.p.current_unit->s, buf, nbytes); |
| 898 |
if (unlikely (have_written < 0)) |
| 899 |
{ |
| 900 |
generate_error (&dtp->common, LIBERROR_OS, NULL); |
| 901 |
return false; |
| 902 |
} |
| 903 |
|
| 904 |
dtp->u.p.current_unit->strm_pos += (gfc_offset) have_written; |
| 905 |
|
| 906 |
return true; |
| 907 |
} |
| 908 |
|
| 909 |
/* Unformatted direct access. */ |
| 910 |
|
| 911 |
if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT) |
| 912 |
{ |
| 913 |
if (unlikely (dtp->u.p.current_unit->bytes_left < (gfc_offset) nbytes)) |
| 914 |
{ |
| 915 |
generate_error (&dtp->common, LIBERROR_DIRECT_EOR, NULL); |
| 916 |
return false; |
| 917 |
} |
| 918 |
|
| 919 |
if (buf == NULL && nbytes == 0) |
| 920 |
return true; |
| 921 |
|
| 922 |
have_written = swrite (dtp->u.p.current_unit->s, buf, nbytes); |
| 923 |
if (unlikely (have_written < 0)) |
| 924 |
{ |
| 925 |
generate_error (&dtp->common, LIBERROR_OS, NULL); |
| 926 |
return false; |
| 927 |
} |
| 928 |
|
| 929 |
dtp->u.p.current_unit->strm_pos += (gfc_offset) have_written; |
| 930 |
dtp->u.p.current_unit->bytes_left -= (gfc_offset) have_written; |
| 931 |
|
| 932 |
return true; |
| 933 |
} |
| 934 |
|
| 935 |
/* Unformatted sequential. */ |
| 936 |
|
| 937 |
have_written = 0; |
| 938 |
|
| 939 |
if (dtp->u.p.current_unit->flags.has_recl |
| 940 |
&& (gfc_offset) nbytes > dtp->u.p.current_unit->bytes_left) |
| 941 |
{ |
| 942 |
nbytes = dtp->u.p.current_unit->bytes_left; |
| 943 |
short_record = 1; |
| 944 |
} |
| 945 |
else |
| 946 |
{ |
| 947 |
short_record = 0; |
| 948 |
} |
| 949 |
|
| 950 |
while (1) |
| 951 |
{ |
| 952 |
|
| 953 |
to_write_subrecord = |
| 954 |
(size_t) dtp->u.p.current_unit->bytes_left_subrecord < nbytes ? |
| 955 |
(size_t) dtp->u.p.current_unit->bytes_left_subrecord : nbytes; |
| 956 |
|
| 957 |
dtp->u.p.current_unit->bytes_left_subrecord -= |
| 958 |
(gfc_offset) to_write_subrecord; |
| 959 |
|
| 960 |
to_write_subrecord = swrite (dtp->u.p.current_unit->s, |
| 961 |
buf + have_written, to_write_subrecord); |
| 962 |
if (unlikely (to_write_subrecord < 0)) |
| 963 |
{ |
| 964 |
generate_error (&dtp->common, LIBERROR_OS, NULL); |
| 965 |
return false; |
| 966 |
} |
| 967 |
|
| 968 |
dtp->u.p.current_unit->strm_pos += (gfc_offset) to_write_subrecord; |
| 969 |
nbytes -= to_write_subrecord; |
| 970 |
have_written += to_write_subrecord; |
| 971 |
|
| 972 |
if (nbytes == 0) |
| 973 |
break; |
| 974 |
|
| 975 |
next_record_w_unf (dtp, 1); |
| 976 |
us_write (dtp, 1); |
| 977 |
} |
| 978 |
dtp->u.p.current_unit->bytes_left -= have_written; |
| 979 |
if (unlikely (short_record)) |
| 980 |
{ |
| 981 |
generate_error (&dtp->common, LIBERROR_SHORT_RECORD, NULL); |
| 982 |
return false; |
| 983 |
} |
| 984 |
return true; |
| 985 |
} |
| 986 |
|
| 987 |
|
| 988 |
/* Reverse memcpy - used for byte swapping. */ |
| 989 |
|
| 990 |
static void |
| 991 |
reverse_memcpy (void *dest, const void *src, size_t n) |
| 992 |
{ |
| 993 |
char *d, *s; |
| 994 |
size_t i; |
| 995 |
|
| 996 |
d = (char *) dest; |
| 997 |
s = (char *) src + n - 1; |
| 998 |
|
| 999 |
/* Write with ascending order - this is likely faster |
| 1000 |
on modern architectures because of write combining. */ |
| 1001 |
for (i=0; i<n; i++) |
| 1002 |
*(d++) = *(s--); |
| 1003 |
} |
| 1004 |
|
| 1005 |
|
| 1006 |
/* Utility function for byteswapping an array, using the bswap |
| 1007 |
builtins if possible. dest and src can overlap completely, or then |
| 1008 |
they must point to separate objects; partial overlaps are not |
| 1009 |
allowed. */ |
| 1010 |
|
| 1011 |
static void |
| 1012 |
bswap_array (void *dest, const void *src, size_t size, size_t nelems) |
| 1013 |
{ |
| 1014 |
const char *ps; |
| 1015 |
char *pd; |
| 1016 |
|
| 1017 |
switch (size) |
| 1018 |
{ |
| 1019 |
case 1: |
| 1020 |
break; |
| 1021 |
case 2: |
| 1022 |
for (size_t i = 0; i < nelems; i++) |
| 1023 |
((uint16_t*)dest)[i] = __builtin_bswap16 (((uint16_t*)src)[i]); |
| 1024 |
break; |
| 1025 |
case 4: |
| 1026 |
for (size_t i = 0; i < nelems; i++) |
| 1027 |
((uint32_t*)dest)[i] = __builtin_bswap32 (((uint32_t*)src)[i]); |
| 1028 |
break; |
| 1029 |
case 8: |
| 1030 |
for (size_t i = 0; i < nelems; i++) |
| 1031 |
((uint64_t*)dest)[i] = __builtin_bswap64 (((uint64_t*)src)[i]); |
| 1032 |
break; |
| 1033 |
case 12: |
| 1034 |
ps = src; |
| 1035 |
pd = dest; |
| 1036 |
for (size_t i = 0; i < nelems; i++) |
| 1037 |
{ |
| 1038 |
uint32_t tmp; |
| 1039 |
memcpy (&tmp, ps, 4); |
| 1040 |
*(uint32_t*)pd = __builtin_bswap32 (*(uint32_t*)(ps + 8)); |
| 1041 |
*(uint32_t*)(pd + 4) = __builtin_bswap32 (*(uint32_t*)(ps + 4)); |
| 1042 |
*(uint32_t*)(pd + 8) = __builtin_bswap32 (tmp); |
| 1043 |
ps += size; |
| 1044 |
pd += size; |
| 1045 |
} |
| 1046 |
break; |
| 1047 |
case 16: |
| 1048 |
ps = src; |
| 1049 |
pd = dest; |
| 1050 |
for (size_t i = 0; i < nelems; i++) |
| 1051 |
{ |
| 1052 |
uint64_t tmp; |
| 1053 |
memcpy (&tmp, ps, 8); |
| 1054 |
*(uint64_t*)pd = __builtin_bswap64 (*(uint64_t*)(ps + 8)); |
| 1055 |
*(uint64_t*)(pd + 8) = __builtin_bswap64 (tmp); |
| 1056 |
ps += size; |
| 1057 |
pd += size; |
| 1058 |
} |
| 1059 |
break; |
| 1060 |
default: |
| 1061 |
pd = dest; |
| 1062 |
if (dest != src) |
| 1063 |
{ |
| 1064 |
ps = src; |
| 1065 |
for (size_t i = 0; i < nelems; i++) |
| 1066 |
{ |
| 1067 |
reverse_memcpy (pd, ps, size); |
| 1068 |
ps += size; |
| 1069 |
pd += size; |
| 1070 |
} |
| 1071 |
} |
| 1072 |
else |
| 1073 |
{ |
| 1074 |
/* In-place byte swap. */ |
| 1075 |
for (size_t i = 0; i < nelems; i++) |
| 1076 |
{ |
| 1077 |
char tmp, *low = pd, *high = pd + size - 1; |
| 1078 |
for (size_t j = 0; j < size/2; j++) |
| 1079 |
{ |
| 1080 |
tmp = *low; |
| 1081 |
*low = *high; |
| 1082 |
*high = tmp; |
| 1083 |
low++; |
| 1084 |
high--; |
| 1085 |
} |
| 1086 |
pd += size; |
| 1087 |
} |
| 1088 |
} |
| 1089 |
} |
| 1090 |
} |
| 1091 |
|
| 1092 |
|
| 1093 |
/* Master function for unformatted reads. */ |
| 1094 |
|
| 1095 |
static void |
| 1096 |
unformatted_read (st_parameter_dt *dtp, bt type, |
| 1097 |
void *dest, int kind, size_t size, size_t nelems) |
| 1098 |
{ |
| 1099 |
unit_convert convert; |
| 1100 |
|
| 1101 |
if (type == BT_CLASS) |
| 1102 |
{ |
| 1103 |
GFC_INTEGER_4 unit = dtp->u.p.current_unit->unit_number; |
| 1104 |
char tmp_iomsg[IOMSG_LEN] = ""; |
| 1105 |
char *child_iomsg; |
| 1106 |
gfc_charlen_type child_iomsg_len; |
| 1107 |
GFC_INTEGER_4 noiostat; |
| 1108 |
GFC_INTEGER_4 *child_iostat = NULL; |
| 1109 |
|
| 1110 |
/* Set iostat, intent(out). */ |
| 1111 |
noiostat = 0; |
| 1112 |
child_iostat = ((dtp->common.flags & IOPARM_HAS_IOSTAT) |
| 1113 |
? dtp->common.iostat : &noiostat); |
| 1114 |
|
| 1115 |
/* Set iomsg, intent(inout). */ |
| 1116 |
if (dtp->common.flags & IOPARM_HAS_IOMSG) |
| 1117 |
{ |
| 1118 |
child_iomsg = dtp->common.iomsg; |
| 1119 |
child_iomsg_len = dtp->common.iomsg_len; |
| 1120 |
} |
| 1121 |
else |
| 1122 |
{ |
| 1123 |
child_iomsg = tmp_iomsg; |
| 1124 |
child_iomsg_len = IOMSG_LEN; |
| 1125 |
} |
| 1126 |
|
| 1127 |
/* Call the user defined unformatted READ procedure. */ |
| 1128 |
dtp->u.p.current_unit->child_dtio++; |
| 1129 |
dtp->u.p.ufdtio_ptr (dest, &unit, child_iostat, child_iomsg, |
| 1130 |
child_iomsg_len); |
| 1131 |
dtp->u.p.child_saved_iostat = *child_iostat; |
| 1132 |
dtp->u.p.current_unit->child_dtio--; |
| 1133 |
|
| 1134 |
if ((dtp->u.p.child_saved_iostat != 0) && |
| 1135 |
!(dtp->common.flags & IOPARM_HAS_IOMSG) && |
| 1136 |
!(dtp->common.flags & IOPARM_HAS_IOSTAT)) |
| 1137 |
{ |
| 1138 |
char message[IOMSG_LEN + 1]; |
| 1139 |
child_iomsg_len = string_len_trim (IOMSG_LEN, child_iomsg); |
| 1140 |
fstrcpy (message, child_iomsg_len, child_iomsg, child_iomsg_len); |
| 1141 |
message[child_iomsg_len] = '\0'; |
| 1142 |
generate_error (&dtp->common, dtp->u.p.child_saved_iostat, |
| 1143 |
message); |
| 1144 |
} |
| 1145 |
|
| 1146 |
return; |
| 1147 |
} |
| 1148 |
|
| 1149 |
if (type == BT_CHARACTER) |
| 1150 |
size *= GFC_SIZE_OF_CHAR_KIND(kind); |
| 1151 |
read_block_direct (dtp, dest, size * nelems); |
| 1152 |
|
| 1153 |
convert = dtp->u.p.current_unit->flags.convert; |
| 1154 |
if (unlikely (convert != GFC_CONVERT_NATIVE) && kind != 1) |
| 1155 |
{ |
| 1156 |
/* Handle wide chracters. */ |
| 1157 |
if (type == BT_CHARACTER) |
| 1158 |
{ |
| 1159 |
nelems *= size; |
| 1160 |
size = kind; |
| 1161 |
} |
| 1162 |
|
| 1163 |
/* Break up complex into its constituent reals. */ |
| 1164 |
else if (type == BT_COMPLEX) |
| 1165 |
{ |
| 1166 |
nelems *= 2; |
| 1167 |
size /= 2; |
| 1168 |
} |
| 1169 |
#ifndef HAVE_GFC_REAL_17 |
| 1170 |
#if defined(HAVE_GFC_REAL_16) && GFC_REAL_16_DIGITS == 106 |
| 1171 |
/* IBM extended format is stored as a pair of IEEE754 |
| 1172 |
double values, with the more significant value first |
| 1173 |
in both big and little endian. */ |
| 1174 |
if (kind == 16 && (type == BT_REAL || type == BT_COMPLEX)) |
| 1175 |
{ |
| 1176 |
nelems *= 2; |
| 1177 |
size /= 2; |
| 1178 |
} |
| 1179 |
#endif |
| 1180 |
bswap_array (dest, dest, size, nelems); |
| 1181 |
#else |
| 1182 |
unit_convert bswap = convert & ~(GFC_CONVERT_R16_IEEE | GFC_CONVERT_R16_IBM); |
| 1183 |
if (bswap == GFC_CONVERT_SWAP) |
| 1184 |
{ |
| 1185 |
if ((type == BT_REAL || type == BT_COMPLEX) |
| 1186 |
&& ((kind == 16 && (convert & GFC_CONVERT_R16_IEEE) == 0) |
| 1187 |
|| (kind == 17 && (convert & GFC_CONVERT_R16_IBM)))) |
| 1188 |
bswap_array (dest, dest, size / 2, nelems * 2); |
| 1189 |
else |
| 1190 |
bswap_array (dest, dest, size, nelems); |
| 1191 |
} |
| 1192 |
|
| 1193 |
if ((convert & GFC_CONVERT_R16_IEEE) |
| 1194 |
&& kind == 16 |
| 1195 |
&& (type == BT_REAL || type == BT_COMPLEX)) |
| 1196 |
{ |
| 1197 |
char *pd = dest; |
| 1198 |
for (size_t i = 0; i < nelems; i++) |
| 1199 |
{ |
| 1200 |
GFC_REAL_16 r16; |
| 1201 |
GFC_REAL_17 r17; |
| 1202 |
memcpy (&r17, pd, 16); |
| 1203 |
r16 = r17; |
| 1204 |
memcpy (pd, &r16, 16); |
| 1205 |
pd += size; |
| 1206 |
} |
| 1207 |
} |
| 1208 |
else if ((dtp->u.p.current_unit->flags.convert & GFC_CONVERT_R16_IBM) |
| 1209 |
&& kind == 17 |
| 1210 |
&& (type == BT_REAL || type == BT_COMPLEX)) |
| 1211 |
{ |
| 1212 |
if (type == BT_COMPLEX && size == 32) |
| 1213 |
{ |
| 1214 |
nelems *= 2; |
| 1215 |
size /= 2; |
| 1216 |
} |
| 1217 |
|
| 1218 |
char *pd = dest; |
| 1219 |
for (size_t i = 0; i < nelems; i++) |
| 1220 |
{ |
| 1221 |
GFC_REAL_16 r16; |
| 1222 |
GFC_REAL_17 r17; |
| 1223 |
memcpy (&r16, pd, 16); |
| 1224 |
r17 = r16; |
| 1225 |
memcpy (pd, &r17, 16); |
| 1226 |
pd += size; |
| 1227 |
} |
| 1228 |
} |
| 1229 |
#endif /* HAVE_GFC_REAL_17. */ |
| 1230 |
} |
| 1231 |
} |
| 1232 |
|
| 1233 |
|
| 1234 |
/* Master function for unformatted writes. NOTE: For kind=10 the size is 16 |
| 1235 |
bytes on 64 bit machines. The unused bytes are not initialized and never |
| 1236 |
used, which can show an error with memory checking analyzers like |
| 1237 |
valgrind. We us BT_CLASS to denote a User Defined I/O call. */ |
| 1238 |
|
| 1239 |
static void |
| 1240 |
unformatted_write (st_parameter_dt *dtp, bt type, |
| 1241 |
void *source, int kind, size_t size, size_t nelems) |
| 1242 |
{ |
| 1243 |
unit_convert convert; |
| 1244 |
|
| 1245 |
if (type == BT_CLASS) |
| 1246 |
{ |
| 1247 |
GFC_INTEGER_4 unit = dtp->u.p.current_unit->unit_number; |
| 1248 |
char tmp_iomsg[IOMSG_LEN] = ""; |
| 1249 |
char *child_iomsg; |
| 1250 |
gfc_charlen_type child_iomsg_len; |
| 1251 |
GFC_INTEGER_4 noiostat; |
| 1252 |
GFC_INTEGER_4 *child_iostat = NULL; |
| 1253 |
|
| 1254 |
/* Set iostat, intent(out). */ |
| 1255 |
noiostat = 0; |
| 1256 |
child_iostat = ((dtp->common.flags & IOPARM_HAS_IOSTAT) |
| 1257 |
? dtp->common.iostat : &noiostat); |
| 1258 |
|
| 1259 |
/* Set iomsg, intent(inout). */ |
| 1260 |
if (dtp->common.flags & IOPARM_HAS_IOMSG) |
| 1261 |
{ |
| 1262 |
child_iomsg = dtp->common.iomsg; |
| 1263 |
child_iomsg_len = dtp->common.iomsg_len; |
| 1264 |
} |
| 1265 |
else |
| 1266 |
{ |
| 1267 |
child_iomsg = tmp_iomsg; |
| 1268 |
child_iomsg_len = IOMSG_LEN; |
| 1269 |
} |
| 1270 |
|
| 1271 |
/* Call the user defined unformatted WRITE procedure. */ |
| 1272 |
dtp->u.p.current_unit->child_dtio++; |
| 1273 |
dtp->u.p.ufdtio_ptr (source, &unit, child_iostat, child_iomsg, |
| 1274 |
child_iomsg_len); |
| 1275 |
dtp->u.p.child_saved_iostat = *child_iostat; |
| 1276 |
dtp->u.p.current_unit->child_dtio--; |
| 1277 |
|
| 1278 |
if ((dtp->u.p.child_saved_iostat != 0) && |
| 1279 |
!(dtp->common.flags & IOPARM_HAS_IOMSG) && |
| 1280 |
!(dtp->common.flags & IOPARM_HAS_IOSTAT)) |
| 1281 |
{ |
| 1282 |
char message[IOMSG_LEN + 1]; |
| 1283 |
child_iomsg_len = string_len_trim (IOMSG_LEN, child_iomsg); |
| 1284 |
fstrcpy (message, child_iomsg_len, child_iomsg, child_iomsg_len); |
| 1285 |
message[child_iomsg_len] = '\0'; |
| 1286 |
generate_error (&dtp->common, dtp->u.p.child_saved_iostat, |
| 1287 |
message); |
| 1288 |
} |
| 1289 |
return; |
| 1290 |
} |
| 1291 |
|
| 1292 |
convert = dtp->u.p.current_unit->flags.convert; |
| 1293 |
if (likely (convert == GFC_CONVERT_NATIVE) || kind == 1 |
| 1294 |
#ifdef HAVE_GFC_REAL_17 |
| 1295 |
|| ((type == BT_REAL || type == BT_COMPLEX) |
| 1296 |
&& ((kind == 16 && convert == GFC_CONVERT_R16_IBM) |
| 1297 |
|| (kind == 17 && convert == GFC_CONVERT_R16_IEEE))) |
| 1298 |
#endif |
| 1299 |
) |
| 1300 |
{ |
| 1301 |
size_t stride = type == BT_CHARACTER ? |
| 1302 |
size * GFC_SIZE_OF_CHAR_KIND(kind) : size; |
| 1303 |
|
| 1304 |
write_buf (dtp, source, stride * nelems); |
| 1305 |
} |
| 1306 |
else |
| 1307 |
{ |
| 1308 |
#define BSWAP_BUFSZ 512 |
| 1309 |
char buffer[BSWAP_BUFSZ]; |
| 1310 |
char *p; |
| 1311 |
size_t nrem; |
| 1312 |
|
| 1313 |
p = source; |
| 1314 |
|
| 1315 |
/* Handle wide chracters. */ |
| 1316 |
if (type == BT_CHARACTER && kind != 1) |
| 1317 |
{ |
| 1318 |
nelems *= size; |
| 1319 |
size = kind; |
| 1320 |
} |
| 1321 |
|
| 1322 |
/* Break up complex into its constituent reals. */ |
| 1323 |
if (type == BT_COMPLEX) |
| 1324 |
{ |
| 1325 |
nelems *= 2; |
| 1326 |
size /= 2; |
| 1327 |
} |
| 1328 |
|
| 1329 |
#if !defined(HAVE_GFC_REAL_17) && defined(HAVE_GFC_REAL_16) \ |
| 1330 |
&& GFC_REAL_16_DIGITS == 106 |
| 1331 |
/* IBM extended format is stored as a pair of IEEE754 |
| 1332 |
double values, with the more significant value first |
| 1333 |
in both big and little endian. */ |
| 1334 |
if (kind == 16 && (type == BT_REAL || type == BT_COMPLEX)) |
| 1335 |
{ |
| 1336 |
nelems *= 2; |
| 1337 |
size /= 2; |
| 1338 |
} |
| 1339 |
#endif |
| 1340 |
|
| 1341 |
/* By now, all complex variables have been split into their |
| 1342 |
constituent reals. */ |
| 1343 |
|
| 1344 |
nrem = nelems; |
| 1345 |
do |
| 1346 |
{ |
| 1347 |
size_t nc; |
| 1348 |
if (size * nrem > BSWAP_BUFSZ) |
| 1349 |
nc = BSWAP_BUFSZ / size; |
| 1350 |
else |
| 1351 |
nc = nrem; |
| 1352 |
|
| 1353 |
#ifdef HAVE_GFC_REAL_17 |
| 1354 |
if ((dtp->u.p.current_unit->flags.convert & GFC_CONVERT_R16_IEEE) |
| 1355 |
&& kind == 16 |
| 1356 |
&& (type == BT_REAL || type == BT_COMPLEX)) |
| 1357 |
{ |
| 1358 |
for (size_t i = 0; i < nc; i++) |
| 1359 |
{ |
| 1360 |
GFC_REAL_16 r16; |
| 1361 |
GFC_REAL_17 r17; |
| 1362 |
memcpy (&r16, p, 16); |
| 1363 |
r17 = r16; |
| 1364 |
memcpy (&buffer[i * 16], &r17, 16); |
| 1365 |
p += 16; |
| 1366 |
} |
| 1367 |
if ((dtp->u.p.current_unit->flags.convert |
| 1368 |
& ~(GFC_CONVERT_R16_IEEE | GFC_CONVERT_R16_IBM)) |
| 1369 |
== GFC_CONVERT_SWAP) |
| 1370 |
bswap_array (buffer, buffer, size, nc); |
| 1371 |
} |
| 1372 |
else if ((dtp->u.p.current_unit->flags.convert & GFC_CONVERT_R16_IBM) |
| 1373 |
&& kind == 17 |
| 1374 |
&& (type == BT_REAL || type == BT_COMPLEX)) |
| 1375 |
{ |
| 1376 |
for (size_t i = 0; i < nc; i++) |
| 1377 |
{ |
| 1378 |
GFC_REAL_16 r16; |
| 1379 |
GFC_REAL_17 r17; |
| 1380 |
memcpy (&r17, p, 16); |
| 1381 |
r16 = r17; |
| 1382 |
memcpy (&buffer[i * 16], &r16, 16); |
| 1383 |
p += 16; |
| 1384 |
} |
| 1385 |
if ((dtp->u.p.current_unit->flags.convert |
| 1386 |
& ~(GFC_CONVERT_R16_IEEE | GFC_CONVERT_R16_IBM)) |
| 1387 |
== GFC_CONVERT_SWAP) |
| 1388 |
bswap_array (buffer, buffer, size / 2, nc * 2); |
| 1389 |
} |
| 1390 |
else if (kind == 16 && (type == BT_REAL || type == BT_COMPLEX)) |
| 1391 |
{ |
| 1392 |
bswap_array (buffer, p, size / 2, nc * 2); |
| 1393 |
p += size * nc; |
| 1394 |
} |
| 1395 |
else |
| 1396 |
#endif |
| 1397 |
{ |
| 1398 |
bswap_array (buffer, p, size, nc); |
| 1399 |
p += size * nc; |
| 1400 |
} |
| 1401 |
write_buf (dtp, buffer, size * nc); |
| 1402 |
nrem -= nc; |
| 1403 |
} |
| 1404 |
while (nrem > 0); |
| 1405 |
} |
| 1406 |
} |
| 1407 |
|
| 1408 |
|
| 1409 |
/* Return a pointer to the name of a type. */ |
| 1410 |
|
| 1411 |
const char * |
| 1412 |
type_name (bt type) |
| 1413 |
{ |
| 1414 |
const char *p; |
| 1415 |
|
| 1416 |
switch (type) |
| 1417 |
{ |
| 1418 |
case BT_INTEGER: |
| 1419 |
p = "INTEGER"; |
| 1420 |
break; |
| 1421 |
case BT_UNSIGNED: |
| 1422 |
p = "UNSIGNED"; |
| 1423 |
break; |
| 1424 |
case BT_LOGICAL: |
| 1425 |
p = "LOGICAL"; |
| 1426 |
break; |
| 1427 |
case BT_CHARACTER: |
| 1428 |
p = "CHARACTER"; |
| 1429 |
break; |
| 1430 |
case BT_REAL: |
| 1431 |
p = "REAL"; |
| 1432 |
break; |
| 1433 |
case BT_COMPLEX: |
| 1434 |
p = "COMPLEX"; |
| 1435 |
break; |
| 1436 |
case BT_CLASS: |
| 1437 |
p = "CLASS or DERIVED"; |
| 1438 |
break; |
| 1439 |
default: |
| 1440 |
internal_error (NULL, "type_name(): Bad type"); |
| 1441 |
} |
| 1442 |
|
| 1443 |
return p; |
| 1444 |
} |
| 1445 |
|
| 1446 |
|
| 1447 |
/* Write a constant string to the output. |
| 1448 |
This is complicated because the string can have doubled delimiters |
| 1449 |
in it. The length in the format node is the true length. */ |
| 1450 |
|
| 1451 |
static void |
| 1452 |
write_constant_string (st_parameter_dt *dtp, const fnode *f) |
| 1453 |
{ |
| 1454 |
char c, delimiter, *p, *q; |
| 1455 |
int length; |
| 1456 |
|
| 1457 |
length = f->u.string.length; |
| 1458 |
if (length == 0) |
| 1459 |
return; |
| 1460 |
|
| 1461 |
p = write_block (dtp, length); |
| 1462 |
if (p == NULL) |
| 1463 |
return; |
| 1464 |
|
| 1465 |
q = f->u.string.p; |
| 1466 |
delimiter = q[-1]; |
| 1467 |
|
| 1468 |
for (; length > 0; length--) |
| 1469 |
{ |
| 1470 |
c = *p++ = *q++; |
| 1471 |
if (c == delimiter && c != 'H' && c != 'h') |
| 1472 |
q++; /* Skip the doubled delimiter. */ |
| 1473 |
} |
| 1474 |
} |
| 1475 |
|
| 1476 |
|
| 1477 |
/* Given actual and expected types in a formatted data transfer, make |
| 1478 |
sure they agree. If not, an error message is generated. Returns |
| 1479 |
nonzero if something went wrong. */ |
| 1480 |
|
| 1481 |
static int |
| 1482 |
require_type (st_parameter_dt *dtp, bt expected, bt actual, const fnode *f) |
| 1483 |
{ |
| 1484 |
#define BUFLEN 100 |
| 1485 |
char buffer[BUFLEN]; |
| 1486 |
|
| 1487 |
if (actual == expected) |
| 1488 |
return 0; |
| 1489 |
|
| 1490 |
/* Adjust item_count before emitting error message. */ |
| 1491 |
snprintf (buffer, BUFLEN, |
| 1492 |
"Expected %s for item %d in formatted transfer, got %s", |
| 1493 |
type_name (expected), dtp->u.p.item_count - 1, type_name (actual)); |
| 1494 |
|
| 1495 |
format_error (dtp, f, buffer); |
| 1496 |
return 1; |
| 1497 |
} |
| 1498 |
|
| 1499 |
/* Check that the actual matches one of two expected types; issue an error |
| 1500 |
if that is not the case. */ |
| 1501 |
|
| 1502 |
|
| 1503 |
static int |
| 1504 |
require_one_of_two_types (st_parameter_dt *dtp, bt expected1, bt expected2, |
| 1505 |
bt actual, const fnode *f) |
| 1506 |
{ |
| 1507 |
char buffer[BUFLEN]; |
| 1508 |
|
| 1509 |
if (actual == expected1) |
| 1510 |
return 0; |
| 1511 |
|
| 1512 |
if (actual == expected2) |
| 1513 |
return 0; |
| 1514 |
|
| 1515 |
snprintf (buffer, BUFLEN, |
| 1516 |
"Expected %s or %s for item %d in formatted transfer, got %s", |
| 1517 |
type_name (expected1), type_name (expected2), |
| 1518 |
dtp->u.p.item_count - 1, type_name (actual)); |
| 1519 |
|
| 1520 |
format_error (dtp, f, buffer); |
| 1521 |
return 1; |
| 1522 |
|
| 1523 |
} |
| 1524 |
|
| 1525 |
/* Check that the dtio procedure required for formatted IO is present. */ |
| 1526 |
|
| 1527 |
static int |
| 1528 |
check_dtio_proc (st_parameter_dt *dtp, const fnode *f) |
| 1529 |
{ |
| 1530 |
char buffer[BUFLEN]; |
| 1531 |
|
| 1532 |
if (dtp->u.p.fdtio_ptr != NULL) |
| 1533 |
return 0; |
| 1534 |
|
| 1535 |
snprintf (buffer, BUFLEN, |
| 1536 |
"Missing DTIO procedure or intrinsic type passed for item %d " |
| 1537 |
"in formatted transfer", |
| 1538 |
dtp->u.p.item_count - 1); |
| 1539 |
|
| 1540 |
format_error (dtp, f, buffer); |
| 1541 |
return 1; |
| 1542 |
} |
| 1543 |
|
| 1544 |
|
| 1545 |
static int |
| 1546 |
require_numeric_type (st_parameter_dt *dtp, bt actual, const fnode *f) |
| 1547 |
{ |
| 1548 |
#define BUFLEN 100 |
| 1549 |
char buffer[BUFLEN]; |
| 1550 |
|
| 1551 |
if (actual == BT_INTEGER || actual == BT_REAL || actual == BT_COMPLEX) |
| 1552 |
return 0; |
| 1553 |
|
| 1554 |
/* Adjust item_count before emitting error message. */ |
| 1555 |
snprintf (buffer, BUFLEN, |
| 1556 |
"Expected numeric type for item %d in formatted transfer, got %s", |
| 1557 |
dtp->u.p.item_count - 1, type_name (actual)); |
| 1558 |
|
| 1559 |
format_error (dtp, f, buffer); |
| 1560 |
return 1; |
| 1561 |
} |
| 1562 |
|
| 1563 |
static char * |
| 1564 |
get_dt_format (char *p, gfc_charlen_type *length) |
| 1565 |
{ |
| 1566 |
char delim = p[-1]; /* The delimiter is always the first character back. */ |
| 1567 |
char c, *q, *res; |
| 1568 |
gfc_charlen_type len = *length; /* This length already correct, less 'DT'. */ |
| 1569 |
|
| 1570 |
res = q = xmalloc (len + 2); |
| 1571 |
|
| 1572 |
/* Set the beginning of the string to 'DT', length adjusted below. */ |
| 1573 |
*q++ = 'D'; |
| 1574 |
*q++ = 'T'; |
| 1575 |
|
| 1576 |
/* The string may contain doubled quotes so scan and skip as needed. */ |
| 1577 |
for (; len > 0; len--) |
| 1578 |
{ |
| 1579 |
c = *q++ = *p++; |
| 1580 |
if (c == delim) |
| 1581 |
p++; /* Skip the doubled delimiter. */ |
| 1582 |
} |
| 1583 |
|
| 1584 |
/* Adjust the string length by two now that we are done. */ |
| 1585 |
*length += 2; |
| 1586 |
|
| 1587 |
return res; |
| 1588 |
} |
| 1589 |
|
| 1590 |
|
| 1591 |
/* This function is in the main loop for a formatted data transfer |
| 1592 |
statement. It would be natural to implement this as a coroutine |
| 1593 |
with the user program, but C makes that awkward. We loop, |
| 1594 |
processing format elements. When we actually have to transfer |
| 1595 |
data instead of just setting flags, we return control to the user |
| 1596 |
program which calls a function that supplies the address and type |
| 1597 |
of the next element, then comes back here to process it. */ |
| 1598 |
|
| 1599 |
static void |
| 1600 |
formatted_transfer_scalar_read (st_parameter_dt *dtp, bt type, void *p, int kind, |
| 1601 |
size_t size) |
| 1602 |
{ |
| 1603 |
int pos, bytes_used; |
| 1604 |
const fnode *f; |
| 1605 |
format_token t; |
| 1606 |
int n; |
| 1607 |
int consume_data_flag; |
| 1608 |
|
| 1609 |
/* Change a complex data item into a pair of reals. */ |
| 1610 |
|
| 1611 |
n = (p == NULL) ? 0 : ((type != BT_COMPLEX) ? 1 : 2); |
| 1612 |
if (type == BT_COMPLEX) |
| 1613 |
{ |
| 1614 |
type = BT_REAL; |
| 1615 |
size /= 2; |
| 1616 |
} |
| 1617 |
|
| 1618 |
/* If there's an EOR condition, we simulate finalizing the transfer |
| 1619 |
by doing nothing. */ |
| 1620 |
if (dtp->u.p.eor_condition) |
| 1621 |
return; |
| 1622 |
|
| 1623 |
/* Set this flag so that commas in reads cause the read to complete before |
| 1624 |
the entire field has been read. The next read field will start right after |
| 1625 |
the comma in the stream. (Set to 0 for character reads). */ |
| 1626 |
dtp->u.p.sf_read_comma = |
| 1627 |
dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA ? 0 : 1; |
| 1628 |
|
| 1629 |
for (;;) |
| 1630 |
{ |
| 1631 |
/* If reversion has occurred and there is another real data item, |
| 1632 |
then we have to move to the next record. */ |
| 1633 |
if (dtp->u.p.reversion_flag && n > 0) |
| 1634 |
{ |
| 1635 |
dtp->u.p.reversion_flag = 0; |
| 1636 |
next_record (dtp, 0); |
| 1637 |
} |
| 1638 |
|
| 1639 |
consume_data_flag = 1; |
| 1640 |
if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK) |
| 1641 |
break; |
| 1642 |
|
| 1643 |
f = next_format (dtp); |
| 1644 |
if (f == NULL) |
| 1645 |
{ |
| 1646 |
/* No data descriptors left. */ |
| 1647 |
if (unlikely (n > 0)) |
| 1648 |
generate_error (&dtp->common, LIBERROR_FORMAT, |
| 1649 |
"Insufficient data descriptors in format after reversion"); |
| 1650 |
return; |
| 1651 |
} |
| 1652 |
|
| 1653 |
t = f->format; |
| 1654 |
|
| 1655 |
bytes_used = (int)(dtp->u.p.current_unit->recl |
| 1656 |
- dtp->u.p.current_unit->bytes_left); |
| 1657 |
|
| 1658 |
if (is_stream_io(dtp)) |
| 1659 |
bytes_used = 0; |
| 1660 |
|
| 1661 |
switch (t) |
| 1662 |
{ |
| 1663 |
case FMT_I: |
| 1664 |
if (n == 0) |
| 1665 |
goto need_read_data; |
| 1666 |
if (require_one_of_two_types (dtp, BT_INTEGER, BT_UNSIGNED, type, f)) |
| 1667 |
return; |
| 1668 |
if (type == BT_INTEGER) |
| 1669 |
read_decimal (dtp, f, p, kind); |
| 1670 |
else |
| 1671 |
read_decimal_unsigned (dtp, f, p, kind); |
| 1672 |
break; |
| 1673 |
|
| 1674 |
case FMT_B: |
| 1675 |
if (n == 0) |
| 1676 |
goto need_read_data; |
| 1677 |
if (!(compile_options.allow_std & GFC_STD_GNU) |
| 1678 |
&& require_numeric_type (dtp, type, f)) |
| 1679 |
return; |
| 1680 |
if (!(compile_options.allow_std & GFC_STD_F2008) |
| 1681 |
&& require_type (dtp, BT_INTEGER, type, f)) |
| 1682 |
return; |
| 1683 |
#ifdef HAVE_GFC_REAL_17 |
| 1684 |
if (type == BT_REAL && kind == 17) |
| 1685 |
kind = 16; |
| 1686 |
#endif |
| 1687 |
read_radix (dtp, f, p, kind, 2); |
| 1688 |
break; |
| 1689 |
|
| 1690 |
case FMT_O: |
| 1691 |
if (n == 0) |
| 1692 |
goto need_read_data; |
| 1693 |
if (!(compile_options.allow_std & GFC_STD_GNU) |
| 1694 |
&& require_numeric_type (dtp, type, f)) |
| 1695 |
return; |
| 1696 |
if (!(compile_options.allow_std & GFC_STD_F2008) |
| 1697 |
&& require_type (dtp, BT_INTEGER, type, f)) |
| 1698 |
return; |
| 1699 |
#ifdef HAVE_GFC_REAL_17 |
| 1700 |
if (type == BT_REAL && kind == 17) |
| 1701 |
kind = 16; |
| 1702 |
#endif |
| 1703 |
read_radix (dtp, f, p, kind, 8); |
| 1704 |
break; |
| 1705 |
|
| 1706 |
case FMT_Z: |
| 1707 |
if (n == 0) |
| 1708 |
goto need_read_data; |
| 1709 |
if (!(compile_options.allow_std & GFC_STD_GNU) |
| 1710 |
&& require_numeric_type (dtp, type, f)) |
| 1711 |
return; |
| 1712 |
if (!(compile_options.allow_std & GFC_STD_F2008) |
| 1713 |
&& require_type (dtp, BT_INTEGER, type, f)) |
| 1714 |
return; |
| 1715 |
#ifdef HAVE_GFC_REAL_17 |
| 1716 |
if (type == BT_REAL && kind == 17) |
| 1717 |
kind = 16; |
| 1718 |
#endif |
| 1719 |
read_radix (dtp, f, p, kind, 16); |
| 1720 |
break; |
| 1721 |
|
| 1722 |
case FMT_A: |
| 1723 |
if (n == 0) |
| 1724 |
goto need_read_data; |
| 1725 |
|
| 1726 |
/* It is possible to have FMT_A with something not BT_CHARACTER such |
| 1727 |
as when writing out hollerith strings, so check both type |
| 1728 |
and kind before calling wide character routines. */ |
| 1729 |
if (type == BT_CHARACTER && kind == 4) |
| 1730 |
read_a_char4 (dtp, f, p, size); |
| 1731 |
else |
| 1732 |
read_a (dtp, f, p, size); |
| 1733 |
break; |
| 1734 |
|
| 1735 |
case FMT_L: |
| 1736 |
if (n == 0) |
| 1737 |
goto need_read_data; |
| 1738 |
read_l (dtp, f, p, kind); |
| 1739 |
break; |
| 1740 |
|
| 1741 |
case FMT_D: |
| 1742 |
if (n == 0) |
| 1743 |
goto need_read_data; |
| 1744 |
if (require_type (dtp, BT_REAL, type, f)) |
| 1745 |
return; |
| 1746 |
read_f (dtp, f, p, kind); |
| 1747 |
break; |
| 1748 |
|
| 1749 |
case FMT_DT: |
| 1750 |
if (n == 0) |
| 1751 |
goto need_read_data; |
| 1752 |
|
| 1753 |
if (check_dtio_proc (dtp, f)) |
| 1754 |
return; |
| 1755 |
if (require_type (dtp, BT_CLASS, type, f)) |
| 1756 |
return; |
| 1757 |
GFC_INTEGER_4 unit = dtp->u.p.current_unit->unit_number; |
| 1758 |
char dt[] = "DT"; |
| 1759 |
char tmp_iomsg[IOMSG_LEN] = ""; |
| 1760 |
char *child_iomsg; |
| 1761 |
gfc_charlen_type child_iomsg_len; |
| 1762 |
GFC_INTEGER_4 noiostat; |
| 1763 |
GFC_INTEGER_4 *child_iostat = NULL; |
| 1764 |
char *iotype; |
| 1765 |
gfc_charlen_type iotype_len = f->u.udf.string_len; |
| 1766 |
|
| 1767 |
/* Build the iotype string. */ |
| 1768 |
if (iotype_len == 0) |
| 1769 |
{ |
| 1770 |
iotype_len = 2; |
| 1771 |
iotype = dt; |
| 1772 |
} |
| 1773 |
else |
| 1774 |
iotype = get_dt_format (f->u.udf.string, &iotype_len); |
| 1775 |
|
| 1776 |
/* Set iostat, intent(out). */ |
| 1777 |
noiostat = 0; |
| 1778 |
child_iostat = ((dtp->common.flags & IOPARM_HAS_IOSTAT) |
| 1779 |
? dtp->common.iostat : &noiostat); |
| 1780 |
|
| 1781 |
/* Set iomsg, intent(inout). */ |
| 1782 |
if (dtp->common.flags & IOPARM_HAS_IOMSG) |
| 1783 |
{ |
| 1784 |
child_iomsg = dtp->common.iomsg; |
| 1785 |
child_iomsg_len = dtp->common.iomsg_len; |
| 1786 |
} |
| 1787 |
else |
| 1788 |
{ |
| 1789 |
child_iomsg = tmp_iomsg; |
| 1790 |
child_iomsg_len = IOMSG_LEN; |
| 1791 |
} |
| 1792 |
|
| 1793 |
/* Call the user defined formatted READ procedure. */ |
| 1794 |
dtp->u.p.current_unit->child_dtio++; |
| 1795 |
dtp->u.p.current_unit->last_char = EOF - 1; |
| 1796 |
dtp->u.p.fdtio_ptr (p, &unit, iotype, f->u.udf.vlist, |
| 1797 |
child_iostat, child_iomsg, |
| 1798 |
iotype_len, child_iomsg_len); |
| 1799 |
dtp->u.p.child_saved_iostat = *child_iostat; |
| 1800 |
dtp->u.p.current_unit->child_dtio--; |
| 1801 |
|
| 1802 |
if ((dtp->u.p.child_saved_iostat != 0) && |
| 1803 |
!(dtp->common.flags & IOPARM_HAS_IOMSG) && |
| 1804 |
!(dtp->common.flags & IOPARM_HAS_IOSTAT)) |
| 1805 |
{ |
| 1806 |
char message[IOMSG_LEN + 1]; |
| 1807 |
child_iomsg_len = string_len_trim (IOMSG_LEN, child_iomsg); |
| 1808 |
fstrcpy (message, child_iomsg_len, child_iomsg, child_iomsg_len); |
| 1809 |
message[child_iomsg_len] = '\0'; |
| 1810 |
generate_error (&dtp->common, dtp->u.p.child_saved_iostat, |
| 1811 |
message); |
| 1812 |
} |
| 1813 |
|
| 1814 |
if (f->u.udf.string_len != 0) |
| 1815 |
free (iotype); |
| 1816 |
/* Note: vlist is freed in free_format_data. */ |
| 1817 |
break; |
| 1818 |
|
| 1819 |
case FMT_E: |
| 1820 |
if (n == 0) |
| 1821 |
goto need_read_data; |
| 1822 |
if (require_type (dtp, BT_REAL, type, f)) |
| 1823 |
return; |
| 1824 |
read_f (dtp, f, p, kind); |
| 1825 |
break; |
| 1826 |
|
| 1827 |
case FMT_EN: |
| 1828 |
if (n == 0) |
| 1829 |
goto need_read_data; |
| 1830 |
if (require_type (dtp, BT_REAL, type, f)) |
| 1831 |
return; |
| 1832 |
read_f (dtp, f, p, kind); |
| 1833 |
break; |
| 1834 |
|
| 1835 |
case FMT_ES: |
| 1836 |
if (n == 0) |
| 1837 |
goto need_read_data; |
| 1838 |
if (require_type (dtp, BT_REAL, type, f)) |
| 1839 |
return; |
| 1840 |
read_f (dtp, f, p, kind); |
| 1841 |
break; |
| 1842 |
|
| 1843 |
case FMT_F: |
| 1844 |
if (n == 0) |
| 1845 |
goto need_read_data; |
| 1846 |
if (require_type (dtp, BT_REAL, type, f)) |
| 1847 |
return; |
| 1848 |
read_f (dtp, f, p, kind); |
| 1849 |
break; |
| 1850 |
|
| 1851 |
case FMT_G: |
| 1852 |
if (n == 0) |
| 1853 |
goto need_read_data; |
| 1854 |
switch (type) |
| 1855 |
{ |
| 1856 |
case BT_INTEGER: |
| 1857 |
read_decimal (dtp, f, p, kind); |
| 1858 |
break; |
| 1859 |
case BT_LOGICAL: |
| 1860 |
read_l (dtp, f, p, kind); |
| 1861 |
break; |
| 1862 |
case BT_CHARACTER: |
| 1863 |
if (kind == 4) |
| 1864 |
read_a_char4 (dtp, f, p, size); |
| 1865 |
else |
| 1866 |
read_a (dtp, f, p, size); |
| 1867 |
break; |
| 1868 |
case BT_REAL: |
| 1869 |
read_f (dtp, f, p, kind); |
| 1870 |
break; |
| 1871 |
default: |
| 1872 |
internal_error (&dtp->common, |
| 1873 |
"formatted_transfer (): Bad type"); |
| 1874 |
} |
| 1875 |
break; |
| 1876 |
|
| 1877 |
case FMT_STRING: |
| 1878 |
consume_data_flag = 0; |
| 1879 |
format_error (dtp, f, "Constant string in input format"); |
| 1880 |
return; |
| 1881 |
|
| 1882 |
/* Format codes that don't transfer data. */ |
| 1883 |
case FMT_X: |
| 1884 |
case FMT_TR: |
| 1885 |
consume_data_flag = 0; |
| 1886 |
dtp->u.p.skips += f->u.n; |
| 1887 |
pos = bytes_used + dtp->u.p.skips - 1; |
| 1888 |
dtp->u.p.pending_spaces = pos - dtp->u.p.max_pos + 1; |
| 1889 |
read_x (dtp, f->u.n); |
| 1890 |
break; |
| 1891 |
|
| 1892 |
case FMT_TL: |
| 1893 |
case FMT_T: |
| 1894 |
consume_data_flag = 0; |
| 1895 |
|
| 1896 |
if (f->format == FMT_TL) |
| 1897 |
{ |
| 1898 |
/* Handle the special case when no bytes have been used yet. |
| 1899 |
Cannot go below zero. */ |
| 1900 |
if (bytes_used == 0) |
| 1901 |
{ |
| 1902 |
dtp->u.p.pending_spaces -= f->u.n; |
| 1903 |
dtp->u.p.skips -= f->u.n; |
| 1904 |
dtp->u.p.skips = dtp->u.p.skips < 0 ? 0 : dtp->u.p.skips; |
| 1905 |
} |
| 1906 |
|
| 1907 |
pos = bytes_used - f->u.n; |
| 1908 |
} |
| 1909 |
else /* FMT_T */ |
| 1910 |
pos = f->u.n - 1; |
| 1911 |
|
| 1912 |
/* Standard 10.6.1.1: excessive left tabbing is reset to the |
| 1913 |
left tab limit. We do not check if the position has gone |
| 1914 |
beyond the end of record because a subsequent tab could |
| 1915 |
bring us back again. */ |
| 1916 |
pos = pos < 0 ? 0 : pos; |
| 1917 |
|
| 1918 |
dtp->u.p.skips = dtp->u.p.skips + pos - bytes_used; |
| 1919 |
dtp->u.p.pending_spaces = dtp->u.p.pending_spaces |
| 1920 |
+ pos - dtp->u.p.max_pos; |
| 1921 |
dtp->u.p.pending_spaces = dtp->u.p.pending_spaces < 0 |
| 1922 |
? 0 : dtp->u.p.pending_spaces; |
| 1923 |
if (dtp->u.p.skips == 0) |
| 1924 |
break; |
| 1925 |
|
| 1926 |
/* Adjust everything for end-of-record condition */ |
| 1927 |
if (dtp->u.p.sf_seen_eor && !is_internal_unit (dtp)) |
| 1928 |
{ |
| 1929 |
dtp->u.p.current_unit->bytes_left -= dtp->u.p.sf_seen_eor; |
| 1930 |
dtp->u.p.skips -= dtp->u.p.sf_seen_eor; |
| 1931 |
bytes_used = pos; |
| 1932 |
if (dtp->u.p.pending_spaces == 0) |
| 1933 |
dtp->u.p.sf_seen_eor = 0; |
| 1934 |
} |
| 1935 |
if (dtp->u.p.skips < 0) |
| 1936 |
{ |
| 1937 |
if (is_internal_unit (dtp)) |
| 1938 |
sseek (dtp->u.p.current_unit->s, dtp->u.p.skips, SEEK_CUR); |
| 1939 |
else |
| 1940 |
fbuf_seek (dtp->u.p.current_unit, dtp->u.p.skips, SEEK_CUR); |
| 1941 |
dtp->u.p.current_unit->bytes_left -= (gfc_offset) dtp->u.p.skips; |
| 1942 |
dtp->u.p.skips = dtp->u.p.pending_spaces = 0; |
| 1943 |
} |
| 1944 |
else |
| 1945 |
read_x (dtp, dtp->u.p.skips); |
| 1946 |
break; |
| 1947 |
|
| 1948 |
case FMT_S: |
| 1949 |
consume_data_flag = 0; |
| 1950 |
dtp->u.p.sign_status = SIGN_PROCDEFINED; |
| 1951 |
break; |
| 1952 |
|
| 1953 |
case FMT_SS: |
| 1954 |
consume_data_flag = 0; |
| 1955 |
dtp->u.p.sign_status = SIGN_SUPPRESS; |
| 1956 |
break; |
| 1957 |
|
| 1958 |
case FMT_SP: |
| 1959 |
consume_data_flag = 0; |
| 1960 |
dtp->u.p.sign_status = SIGN_PLUS; |
| 1961 |
break; |
| 1962 |
|
| 1963 |
case FMT_BN: |
| 1964 |
consume_data_flag = 0 ; |
| 1965 |
dtp->u.p.blank_status = BLANK_NULL; |
| 1966 |
break; |
| 1967 |
|
| 1968 |
case FMT_BZ: |
| 1969 |
consume_data_flag = 0; |
| 1970 |
dtp->u.p.blank_status = BLANK_ZERO; |
| 1971 |
break; |
| 1972 |
|
| 1973 |
case FMT_DC: |
| 1974 |
consume_data_flag = 0; |
| 1975 |
dtp->u.p.current_unit->decimal_status = DECIMAL_COMMA; |
| 1976 |
break; |
| 1977 |
|
| 1978 |
case FMT_DP: |
| 1979 |
consume_data_flag = 0; |
| 1980 |
dtp->u.p.current_unit->decimal_status = DECIMAL_POINT; |
| 1981 |
break; |
| 1982 |
|
| 1983 |
case FMT_RC: |
| 1984 |
consume_data_flag = 0; |
| 1985 |
dtp->u.p.current_unit->round_status = ROUND_COMPATIBLE; |
| 1986 |
break; |
| 1987 |
|
| 1988 |
case FMT_RD: |
| 1989 |
consume_data_flag = 0; |
| 1990 |
dtp->u.p.current_unit->round_status = ROUND_DOWN; |
| 1991 |
break; |
| 1992 |
|
| 1993 |
case FMT_RN: |
| 1994 |
consume_data_flag = 0; |
| 1995 |
dtp->u.p.current_unit->round_status = ROUND_NEAREST; |
| 1996 |
break; |
| 1997 |
|
| 1998 |
case FMT_RP: |
| 1999 |
consume_data_flag = 0; |
| 2000 |
dtp->u.p.current_unit->round_status = ROUND_PROCDEFINED; |
| 2001 |
break; |
| 2002 |
|
| 2003 |
case FMT_RU: |
| 2004 |
consume_data_flag = 0; |
| 2005 |
dtp->u.p.current_unit->round_status = ROUND_UP; |
| 2006 |
break; |
| 2007 |
|
| 2008 |
case FMT_RZ: |
| 2009 |
consume_data_flag = 0; |
| 2010 |
dtp->u.p.current_unit->round_status = ROUND_ZERO; |
| 2011 |
break; |
| 2012 |
|
| 2013 |
case FMT_P: |
| 2014 |
consume_data_flag = 0; |
| 2015 |
dtp->u.p.scale_factor = f->u.k; |
| 2016 |
break; |
| 2017 |
|
| 2018 |
case FMT_DOLLAR: |
| 2019 |
consume_data_flag = 0; |
| 2020 |
dtp->u.p.seen_dollar = 1; |
| 2021 |
break; |
| 2022 |
|
| 2023 |
case FMT_SLASH: |
| 2024 |
consume_data_flag = 0; |
| 2025 |
dtp->u.p.skips = dtp->u.p.pending_spaces = 0; |
| 2026 |
next_record (dtp, 0); |
| 2027 |
break; |
| 2028 |
|
| 2029 |
case FMT_COLON: |
| 2030 |
/* A colon descriptor causes us to exit this loop (in |
| 2031 |
particular preventing another / descriptor from being |
| 2032 |
processed) unless there is another data item to be |
| 2033 |
transferred. */ |
| 2034 |
consume_data_flag = 0; |
| 2035 |
if (n == 0) |
| 2036 |
return; |
| 2037 |
break; |
| 2038 |
|
| 2039 |
default: |
| 2040 |
internal_error (&dtp->common, "Bad format node"); |
| 2041 |
} |
| 2042 |
|
| 2043 |
/* Adjust the item count and data pointer. */ |
| 2044 |
|
| 2045 |
if ((consume_data_flag > 0) && (n > 0)) |
| 2046 |
{ |
| 2047 |
n--; |
| 2048 |
p = ((char *) p) + size; |
| 2049 |
} |
| 2050 |
|
| 2051 |
dtp->u.p.skips = 0; |
| 2052 |
|
| 2053 |
pos = (int)(dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left); |
| 2054 |
dtp->u.p.max_pos = (dtp->u.p.max_pos > pos) ? dtp->u.p.max_pos : pos; |
| 2055 |
} |
| 2056 |
|
| 2057 |
return; |
| 2058 |
|
| 2059 |
/* Come here when we need a data descriptor but don't have one. We |
| 2060 |
push the current format node back onto the input, then return and |
| 2061 |
let the user program call us back with the data. */ |
| 2062 |
need_read_data: |
| 2063 |
unget_format (dtp, f); |
| 2064 |
} |
| 2065 |
|
| 2066 |
|
| 2067 |
static void |
| 2068 |
formatted_transfer_scalar_write (st_parameter_dt *dtp, bt type, void *p, int kind, |
| 2069 |
size_t size) |
| 2070 |
{ |
| 2071 |
gfc_offset tab_pos, bytes_used; |
| 2072 |
const fnode *f; |
| 2073 |
format_token t; |
| 2074 |
int n; |
| 2075 |
int consume_data_flag; |
| 2076 |
|
| 2077 |
tab_pos = 0; bytes_used = 0; |
| 2078 |
|
| 2079 |
/* Change a complex data item into a pair of reals. */ |
| 2080 |
|
| 2081 |
n = (p == NULL) ? 0 : ((type != BT_COMPLEX) ? 1 : 2); |
| 2082 |
if (type == BT_COMPLEX) |
| 2083 |
{ |
| 2084 |
type = BT_REAL; |
| 2085 |
size /= 2; |
| 2086 |
} |
| 2087 |
|
| 2088 |
/* If there's an EOR condition, we simulate finalizing the transfer |
| 2089 |
by doing nothing. */ |
| 2090 |
if (dtp->u.p.eor_condition) |
| 2091 |
return; |
| 2092 |
|
| 2093 |
/* Set this flag so that commas in reads cause the read to complete before |
| 2094 |
the entire field has been read. The next read field will start right after |
| 2095 |
the comma in the stream. (Set to 0 for character reads). */ |
| 2096 |
dtp->u.p.sf_read_comma = |
| 2097 |
dtp->u.p.current_unit->decimal_status == DECIMAL_COMMA ? 0 : 1; |
| 2098 |
|
| 2099 |
for (;;) |
| 2100 |
{ |
| 2101 |
/* If reversion has occurred and there is another real data item, |
| 2102 |
then we have to move to the next record. */ |
| 2103 |
if (dtp->u.p.reversion_flag && n > 0) |
| 2104 |
{ |
| 2105 |
dtp->u.p.reversion_flag = 0; |
| 2106 |
next_record (dtp, 0); |
| 2107 |
} |
| 2108 |
|
| 2109 |
consume_data_flag = 1; |
| 2110 |
if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK) |
| 2111 |
break; |
| 2112 |
|
| 2113 |
f = next_format (dtp); |
| 2114 |
if (f == NULL) |
| 2115 |
{ |
| 2116 |
/* No data descriptors left. */ |
| 2117 |
if (unlikely (n > 0)) |
| 2118 |
generate_error (&dtp->common, LIBERROR_FORMAT, |
| 2119 |
"Insufficient data descriptors in format after reversion"); |
| 2120 |
return; |
| 2121 |
} |
| 2122 |
|
| 2123 |
/* Now discharge T, TR and X movements to the right. This is delayed |
| 2124 |
until a data producing format to suppress trailing spaces. */ |
| 2125 |
|
| 2126 |
t = f->format; |
| 2127 |
if (dtp->u.p.mode == WRITING && dtp->u.p.skips != 0 |
| 2128 |
&& ((n>0 && ( t == FMT_I || t == FMT_B || t == FMT_O |
| 2129 |
|| t == FMT_Z || t == FMT_F || t == FMT_E |
| 2130 |
|| t == FMT_EN || t == FMT_ES || t == FMT_G |
| 2131 |
|| t == FMT_L || t == FMT_A || t == FMT_D |
| 2132 |
|| t == FMT_DT)) |
| 2133 |
|| t == FMT_STRING)) |
| 2134 |
{ |
| 2135 |
if (dtp->u.p.skips > 0) |
| 2136 |
{ |
| 2137 |
gfc_offset tmp; |
| 2138 |
write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces); |
| 2139 |
tmp = dtp->u.p.current_unit->recl |
| 2140 |
- dtp->u.p.current_unit->bytes_left; |
| 2141 |
dtp->u.p.max_pos = |
| 2142 |
dtp->u.p.max_pos > tmp ? dtp->u.p.max_pos : tmp; |
| 2143 |
dtp->u.p.skips = 0; |
| 2144 |
} |
| 2145 |
if (dtp->u.p.skips < 0) |
| 2146 |
{ |
| 2147 |
if (is_internal_unit (dtp)) |
| 2148 |
sseek (dtp->u.p.current_unit->s, dtp->u.p.skips, SEEK_CUR); |
| 2149 |
else |
| 2150 |
fbuf_seek (dtp->u.p.current_unit, dtp->u.p.skips, SEEK_CUR); |
| 2151 |
dtp->u.p.current_unit->bytes_left -= (gfc_offset) dtp->u.p.skips; |
| 2152 |
} |
| 2153 |
dtp->u.p.skips = dtp->u.p.pending_spaces = 0; |
| 2154 |
} |
| 2155 |
|
| 2156 |
if (is_stream_io(dtp)) |
| 2157 |
bytes_used = dtp->u.p.current_unit->fbuf->act; |
| 2158 |
else |
| 2159 |
bytes_used = dtp->u.p.current_unit->recl |
| 2160 |
- dtp->u.p.current_unit->bytes_left; |
| 2161 |
|
| 2162 |
switch (t) |
| 2163 |
{ |
| 2164 |
case FMT_I: |
| 2165 |
if (n == 0) |
| 2166 |
goto need_data; |
| 2167 |
if (require_one_of_two_types (dtp, BT_INTEGER, BT_UNSIGNED, type, f)) |
| 2168 |
return; |
| 2169 |
if (type == BT_INTEGER) |
| 2170 |
write_i (dtp, f, p, kind); |
| 2171 |
else |
| 2172 |
write_iu (dtp, f, p, kind); |
| 2173 |
break; |
| 2174 |
|
| 2175 |
case FMT_B: |
| 2176 |
if (n == 0) |
| 2177 |
goto need_data; |
| 2178 |
if (!(compile_options.allow_std & GFC_STD_GNU) |
| 2179 |
&& require_numeric_type (dtp, type, f)) |
| 2180 |
return; |
| 2181 |
if (!(compile_options.allow_std & GFC_STD_F2008) |
| 2182 |
&& require_type (dtp, BT_INTEGER, type, f)) |
| 2183 |
return; |
| 2184 |
#ifdef HAVE_GFC_REAL_17 |
| 2185 |
if (type == BT_REAL && kind == 17) |
| 2186 |
kind = 16; |
| 2187 |
#endif |
| 2188 |
write_b (dtp, f, p, kind); |
| 2189 |
break; |
| 2190 |
|
| 2191 |
case FMT_O: |
| 2192 |
if (n == 0) |
| 2193 |
goto need_data; |
| 2194 |
if (!(compile_options.allow_std & GFC_STD_GNU) |
| 2195 |
&& require_numeric_type (dtp, type, f)) |
| 2196 |
return; |
| 2197 |
if (!(compile_options.allow_std & GFC_STD_F2008) |
| 2198 |
&& require_type (dtp, BT_INTEGER, type, f)) |
| 2199 |
return; |
| 2200 |
#ifdef HAVE_GFC_REAL_17 |
| 2201 |
if (type == BT_REAL && kind == 17) |
| 2202 |
kind = 16; |
| 2203 |
#endif |
| 2204 |
write_o (dtp, f, p, kind); |
| 2205 |
break; |
| 2206 |
|
| 2207 |
case FMT_Z: |
| 2208 |
if (n == 0) |
| 2209 |
goto need_data; |
| 2210 |
if (!(compile_options.allow_std & GFC_STD_GNU) |
| 2211 |
&& require_numeric_type (dtp, type, f)) |
| 2212 |
return; |
| 2213 |
if (!(compile_options.allow_std & GFC_STD_F2008) |
| 2214 |
&& require_type (dtp, BT_INTEGER, type, f)) |
| 2215 |
return; |
| 2216 |
#ifdef HAVE_GFC_REAL_17 |
| 2217 |
if (type == BT_REAL && kind == 17) |
| 2218 |
kind = 16; |
| 2219 |
#endif |
| 2220 |
write_z (dtp, f, p, kind); |
| 2221 |
break; |
| 2222 |
|
| 2223 |
case FMT_A: |
| 2224 |
if (n == 0) |
| 2225 |
goto need_data; |
| 2226 |
|
| 2227 |
/* It is possible to have FMT_A with something not BT_CHARACTER such |
| 2228 |
as when writing out hollerith strings, so check both type |
| 2229 |
and kind before calling wide character routines. */ |
| 2230 |
if (type == BT_CHARACTER && kind == 4) |
| 2231 |
write_a_char4 (dtp, f, p, size); |
| 2232 |
else |
| 2233 |
write_a (dtp, f, p, size); |
| 2234 |
break; |
| 2235 |
|
| 2236 |
case FMT_L: |
| 2237 |
if (n == 0) |
| 2238 |
goto need_data; |
| 2239 |
write_l (dtp, f, p, kind); |
| 2240 |
break; |
| 2241 |
|
| 2242 |
case FMT_D: |
| 2243 |
if (n == 0) |
| 2244 |
goto need_data; |
| 2245 |
if (require_type (dtp, BT_REAL, type, f)) |
| 2246 |
return; |
| 2247 |
if (f->u.real.w == 0) |
| 2248 |
write_real_w0 (dtp, p, kind, f); |
| 2249 |
else |
| 2250 |
write_d (dtp, f, p, kind); |
| 2251 |
break; |
| 2252 |
|
| 2253 |
case FMT_DT: |
| 2254 |
if (n == 0) |
| 2255 |
goto need_data; |
| 2256 |
GFC_INTEGER_4 unit = dtp->u.p.current_unit->unit_number; |
| 2257 |
char dt[] = "DT"; |
| 2258 |
char tmp_iomsg[IOMSG_LEN] = ""; |
| 2259 |
char *child_iomsg; |
| 2260 |
gfc_charlen_type child_iomsg_len; |
| 2261 |
GFC_INTEGER_4 noiostat; |
| 2262 |
GFC_INTEGER_4 *child_iostat = NULL; |
| 2263 |
char *iotype; |
| 2264 |
gfc_charlen_type iotype_len = f->u.udf.string_len; |
| 2265 |
|
| 2266 |
/* Build the iotype string. */ |
| 2267 |
if (iotype_len == 0) |
| 2268 |
{ |
| 2269 |
iotype_len = 2; |
| 2270 |
iotype = dt; |
| 2271 |
} |
| 2272 |
else |
| 2273 |
iotype = get_dt_format (f->u.udf.string, &iotype_len); |
| 2274 |
|
| 2275 |
/* Set iostat, intent(out). */ |
| 2276 |
noiostat = 0; |
| 2277 |
child_iostat = ((dtp->common.flags & IOPARM_HAS_IOSTAT) |
| 2278 |
? dtp->common.iostat : &noiostat); |
| 2279 |
|
| 2280 |
/* Set iomsg, intent(inout). */ |
| 2281 |
if (dtp->common.flags & IOPARM_HAS_IOMSG) |
| 2282 |
{ |
| 2283 |
child_iomsg = dtp->common.iomsg; |
| 2284 |
child_iomsg_len = dtp->common.iomsg_len; |
| 2285 |
} |
| 2286 |
else |
| 2287 |
{ |
| 2288 |
child_iomsg = tmp_iomsg; |
| 2289 |
child_iomsg_len = IOMSG_LEN; |
| 2290 |
} |
| 2291 |
|
| 2292 |
if (check_dtio_proc (dtp, f)) |
| 2293 |
return; |
| 2294 |
|
| 2295 |
/* Call the user defined formatted WRITE procedure. */ |
| 2296 |
dtp->u.p.current_unit->child_dtio++; |
| 2297 |
|
| 2298 |
dtp->u.p.fdtio_ptr (p, &unit, iotype, f->u.udf.vlist, |
| 2299 |
child_iostat, child_iomsg, |
| 2300 |
iotype_len, child_iomsg_len); |
| 2301 |
dtp->u.p.child_saved_iostat = *child_iostat; |
| 2302 |
dtp->u.p.current_unit->child_dtio--; |
| 2303 |
|
| 2304 |
if ((dtp->u.p.child_saved_iostat != 0) && |
| 2305 |
!(dtp->common.flags & IOPARM_HAS_IOMSG) && |
| 2306 |
!(dtp->common.flags & IOPARM_HAS_IOSTAT)) |
| 2307 |
{ |
| 2308 |
char message[IOMSG_LEN + 1]; |
| 2309 |
child_iomsg_len = string_len_trim (IOMSG_LEN, child_iomsg); |
| 2310 |
fstrcpy (message, child_iomsg_len, child_iomsg, child_iomsg_len); |
| 2311 |
message[child_iomsg_len] = '\0'; |
| 2312 |
generate_error (&dtp->common, dtp->u.p.child_saved_iostat, |
| 2313 |
message); |
| 2314 |
} |
| 2315 |
|
| 2316 |
if (f->u.udf.string_len != 0) |
| 2317 |
free (iotype); |
| 2318 |
/* Note: vlist is freed in free_format_data. */ |
| 2319 |
break; |
| 2320 |
|
| 2321 |
case FMT_E: |
| 2322 |
if (n == 0) |
| 2323 |
goto need_data; |
| 2324 |
if (require_type (dtp, BT_REAL, type, f)) |
| 2325 |
return; |
| 2326 |
if (f->u.real.w == 0) |
| 2327 |
write_real_w0 (dtp, p, kind, f); |
| 2328 |
else |
| 2329 |
write_e (dtp, f, p, kind); |
| 2330 |
break; |
| 2331 |
|
| 2332 |
case FMT_EN: |
| 2333 |
if (n == 0) |
| 2334 |
goto need_data; |
| 2335 |
if (require_type (dtp, BT_REAL, type, f)) |
| 2336 |
return; |
| 2337 |
if (f->u.real.w == 0) |
| 2338 |
write_real_w0 (dtp, p, kind, f); |
| 2339 |
else |
| 2340 |
write_en (dtp, f, p, kind); |
| 2341 |
break; |
| 2342 |
|
| 2343 |
case FMT_ES: |
| 2344 |
if (n == 0) |
| 2345 |
goto need_data; |
| 2346 |
if (require_type (dtp, BT_REAL, type, f)) |
| 2347 |
return; |
| 2348 |
if (f->u.real.w == 0) |
| 2349 |
write_real_w0 (dtp, p, kind, f); |
| 2350 |
else |
| 2351 |
write_es (dtp, f, p, kind); |
| 2352 |
break; |
| 2353 |
|
| 2354 |
case FMT_F: |
| 2355 |
if (n == 0) |
| 2356 |
goto need_data; |
| 2357 |
if (require_type (dtp, BT_REAL, type, f)) |
| 2358 |
return; |
| 2359 |
write_f (dtp, f, p, kind); |
| 2360 |
break; |
| 2361 |
|
| 2362 |
case FMT_G: |
| 2363 |
if (n == 0) |
| 2364 |
goto need_data; |
| 2365 |
switch (type) |
| 2366 |
{ |
| 2367 |
case BT_INTEGER: |
| 2368 |
write_i (dtp, f, p, kind); |
| 2369 |
break; |
| 2370 |
case BT_UNSIGNED: |
| 2371 |
write_iu (dtp, f, p, kind); |
| 2372 |
break; |
| 2373 |
case BT_LOGICAL: |
| 2374 |
write_l (dtp, f, p, kind); |
| 2375 |
break; |
| 2376 |
case BT_CHARACTER: |
| 2377 |
if (kind == 4) |
| 2378 |
write_a_char4 (dtp, f, p, size); |
| 2379 |
else |
| 2380 |
write_a (dtp, f, p, size); |
| 2381 |
break; |
| 2382 |
case BT_REAL: |
| 2383 |
if (f->u.real.w == 0) |
| 2384 |
write_real_w0 (dtp, p, kind, f); |
| 2385 |
else |
| 2386 |
write_d (dtp, f, p, kind); |
| 2387 |
break; |
| 2388 |
default: |
| 2389 |
internal_error (&dtp->common, |
| 2390 |
"formatted_transfer (): Bad type"); |
| 2391 |
} |
| 2392 |
break; |
| 2393 |
|
| 2394 |
case FMT_STRING: |
| 2395 |
consume_data_flag = 0; |
| 2396 |
write_constant_string (dtp, f); |
| 2397 |
break; |
| 2398 |
|
| 2399 |
/* Format codes that don't transfer data. */ |
| 2400 |
case FMT_X: |
| 2401 |
case FMT_TR: |
| 2402 |
consume_data_flag = 0; |
| 2403 |
dtp->u.p.skips += f->u.n; |
| 2404 |
tab_pos = bytes_used + dtp->u.p.skips - 1; |
| 2405 |
dtp->u.p.pending_spaces = tab_pos - dtp->u.p.max_pos + 1; |
| 2406 |
dtp->u.p.pending_spaces = dtp->u.p.pending_spaces < 0 |
| 2407 |
? f->u.n : dtp->u.p.pending_spaces; |
| 2408 |
|
| 2409 |
/* Writes occur just before the switch on f->format, above, so |
| 2410 |
that trailing blanks are suppressed, unless we are doing a |
| 2411 |
non-advancing write in which case we want to output the blanks |
| 2412 |
now. */ |
| 2413 |
if (dtp->u.p.advance_status == ADVANCE_NO) |
| 2414 |
{ |
| 2415 |
write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces); |
| 2416 |
dtp->u.p.skips = dtp->u.p.pending_spaces = 0; |
| 2417 |
} |
| 2418 |
break; |
| 2419 |
|
| 2420 |
case FMT_TL: |
| 2421 |
consume_data_flag = 0; |
| 2422 |
/* Handle the special case when no bytes have been used yet. |
| 2423 |
Cannot go below zero. */ |
| 2424 |
if (bytes_used == 0) |
| 2425 |
{ |
| 2426 |
dtp->u.p.pending_spaces -= f->u.n; |
| 2427 |
dtp->u.p.skips -= f->u.n; |
| 2428 |
dtp->u.p.skips = dtp->u.p.skips < 0 ? 0 : dtp->u.p.skips; |
| 2429 |
} |
| 2430 |
|
| 2431 |
tab_pos = bytes_used - f->u.n; |
| 2432 |
|
| 2433 |
/* Standard 10.6.1.1: excessive left tabbing is reset to the |
| 2434 |
left tab limit. We do not check if the position has gone |
| 2435 |
beyond the end of record because a subsequent tab could |
| 2436 |
bring us back again. */ |
| 2437 |
tab_pos = tab_pos < 0 ? 0 : tab_pos; |
| 2438 |
|
| 2439 |
dtp->u.p.skips = dtp->u.p.skips + tab_pos - bytes_used; |
| 2440 |
dtp->u.p.pending_spaces = dtp->u.p.pending_spaces |
| 2441 |
+ tab_pos - dtp->u.p.max_pos; |
| 2442 |
dtp->u.p.pending_spaces = dtp->u.p.pending_spaces < 0 |
| 2443 |
? 0 : dtp->u.p.pending_spaces; |
| 2444 |
break; |
| 2445 |
|
| 2446 |
case FMT_T: |
| 2447 |
consume_data_flag = 0; |
| 2448 |
if (f->u.n < tab_pos + 1) |
| 2449 |
{ |
| 2450 |
tab_pos = f->u.n; |
| 2451 |
dtp->u.p.skips = tab_pos - bytes_used - 1; |
| 2452 |
dtp->u.p.pending_spaces = tab_pos - bytes_used - 1; |
| 2453 |
} |
| 2454 |
else |
| 2455 |
{ |
| 2456 |
tab_pos = f->u.n - dtp->u.p.pending_spaces - 1; |
| 2457 |
|
| 2458 |
/* Excessive left tabbing is reset to the left tab limit. */ |
| 2459 |
tab_pos = tab_pos < 0 ? 0 : tab_pos; |
| 2460 |
|
| 2461 |
dtp->u.p.skips = dtp->u.p.skips + tab_pos - bytes_used; |
| 2462 |
dtp->u.p.pending_spaces = dtp->u.p.pending_spaces |
| 2463 |
+ tab_pos - dtp->u.p.max_pos; |
| 2464 |
} |
| 2465 |
dtp->u.p.pending_spaces = dtp->u.p.pending_spaces < 0 |
| 2466 |
? 0 : dtp->u.p.pending_spaces; |
| 2467 |
break; |
| 2468 |
|
| 2469 |
case FMT_S: |
| 2470 |
consume_data_flag = 0; |
| 2471 |
dtp->u.p.sign_status = SIGN_PROCDEFINED; |
| 2472 |
break; |
| 2473 |
|
| 2474 |
case FMT_SS: |
| 2475 |
consume_data_flag = 0; |
| 2476 |
dtp->u.p.sign_status = SIGN_SUPPRESS; |
| 2477 |
break; |
| 2478 |
|
| 2479 |
case FMT_SP: |
| 2480 |
consume_data_flag = 0; |
| 2481 |
dtp->u.p.sign_status = SIGN_PLUS; |
| 2482 |
break; |
| 2483 |
|
| 2484 |
case FMT_BN: |
| 2485 |
consume_data_flag = 0 ; |
| 2486 |
dtp->u.p.blank_status = BLANK_NULL; |
| 2487 |
break; |
| 2488 |
|
| 2489 |
case FMT_BZ: |
| 2490 |
consume_data_flag = 0; |
| 2491 |
dtp->u.p.blank_status = BLANK_ZERO; |
| 2492 |
break; |
| 2493 |
|
| 2494 |
case FMT_DC: |
| 2495 |
consume_data_flag = 0; |
| 2496 |
dtp->u.p.current_unit->decimal_status = DECIMAL_COMMA; |
| 2497 |
break; |
| 2498 |
|
| 2499 |
case FMT_DP: |
| 2500 |
consume_data_flag = 0; |
| 2501 |
dtp->u.p.current_unit->decimal_status = DECIMAL_POINT; |
| 2502 |
break; |
| 2503 |
|
| 2504 |
case FMT_RC: |
| 2505 |
consume_data_flag = 0; |
| 2506 |
dtp->u.p.current_unit->round_status = ROUND_COMPATIBLE; |
| 2507 |
break; |
| 2508 |
|
| 2509 |
case FMT_RD: |
| 2510 |
consume_data_flag = 0; |
| 2511 |
dtp->u.p.current_unit->round_status = ROUND_DOWN; |
| 2512 |
break; |
| 2513 |
|
| 2514 |
case FMT_RN: |
| 2515 |
consume_data_flag = 0; |
| 2516 |
dtp->u.p.current_unit->round_status = ROUND_NEAREST; |
| 2517 |
break; |
| 2518 |
|
| 2519 |
case FMT_RP: |
| 2520 |
consume_data_flag = 0; |
| 2521 |
dtp->u.p.current_unit->round_status = ROUND_PROCDEFINED; |
| 2522 |
break; |
| 2523 |
|
| 2524 |
case FMT_RU: |
| 2525 |
consume_data_flag = 0; |
| 2526 |
dtp->u.p.current_unit->round_status = ROUND_UP; |
| 2527 |
break; |
| 2528 |
|
| 2529 |
case FMT_RZ: |
| 2530 |
consume_data_flag = 0; |
| 2531 |
dtp->u.p.current_unit->round_status = ROUND_ZERO; |
| 2532 |
break; |
| 2533 |
|
| 2534 |
case FMT_P: |
| 2535 |
consume_data_flag = 0; |
| 2536 |
dtp->u.p.scale_factor = f->u.k; |
| 2537 |
break; |
| 2538 |
|
| 2539 |
case FMT_DOLLAR: |
| 2540 |
consume_data_flag = 0; |
| 2541 |
dtp->u.p.seen_dollar = 1; |
| 2542 |
break; |
| 2543 |
|
| 2544 |
case FMT_SLASH: |
| 2545 |
consume_data_flag = 0; |
| 2546 |
dtp->u.p.skips = dtp->u.p.pending_spaces = 0; |
| 2547 |
next_record (dtp, 0); |
| 2548 |
break; |
| 2549 |
|
| 2550 |
case FMT_COLON: |
| 2551 |
/* A colon descriptor causes us to exit this loop (in |
| 2552 |
particular preventing another / descriptor from being |
| 2553 |
processed) unless there is another data item to be |
| 2554 |
transferred. */ |
| 2555 |
consume_data_flag = 0; |
| 2556 |
if (n == 0) |
| 2557 |
return; |
| 2558 |
break; |
| 2559 |
|
| 2560 |
default: |
| 2561 |
internal_error (&dtp->common, "Bad format node"); |
| 2562 |
} |
| 2563 |
|
| 2564 |
/* Adjust the item count and data pointer. */ |
| 2565 |
|
| 2566 |
if ((consume_data_flag > 0) && (n > 0)) |
| 2567 |
{ |
| 2568 |
n--; |
| 2569 |
p = ((char *) p) + size; |
| 2570 |
} |
| 2571 |
|
| 2572 |
/* Calculate the new max_pos if any. */ |
| 2573 |
gfc_offset new_pos; |
| 2574 |
if (is_stream_io(dtp)) |
| 2575 |
new_pos = dtp->u.p.current_unit->fbuf->act; |
| 2576 |
else |
| 2577 |
new_pos = dtp->u.p.current_unit->recl |
| 2578 |
- dtp->u.p.current_unit->bytes_left; |
| 2579 |
|
| 2580 |
dtp->u.p.max_pos = (dtp->u.p.max_pos > new_pos) ? |
| 2581 |
dtp->u.p.max_pos : new_pos; |
| 2582 |
} |
| 2583 |
|
| 2584 |
return; |
| 2585 |
|
| 2586 |
/* Come here when we need a data descriptor but don't have one. We |
| 2587 |
push the current format node back onto the input, then return and |
| 2588 |
let the user program call us back with the data. */ |
| 2589 |
need_data: |
| 2590 |
unget_format (dtp, f); |
| 2591 |
} |
| 2592 |
|
| 2593 |
/* This function is first called from data_init_transfer to initiate the loop |
| 2594 |
over each item in the format, transferring data as required. Subsequent |
| 2595 |
calls to this function occur for each data item foound in the READ/WRITE |
| 2596 |
statement. The item_count is incremented for each call. Since the first |
| 2597 |
call is from data_transfer_init, the item_count is always one greater than |
| 2598 |
the actual count number of the item being transferred. */ |
| 2599 |
|
| 2600 |
static void |
| 2601 |
formatted_transfer (st_parameter_dt *dtp, bt type, void *p, int kind, |
| 2602 |
size_t size, size_t nelems) |
| 2603 |
{ |
| 2604 |
size_t elem; |
| 2605 |
char *tmp; |
| 2606 |
|
| 2607 |
tmp = (char *) p; |
| 2608 |
size_t stride = type == BT_CHARACTER ? |
| 2609 |
size * GFC_SIZE_OF_CHAR_KIND(kind) : size; |
| 2610 |
if (dtp->u.p.mode == READING) |
| 2611 |
{ |
| 2612 |
/* Big loop over all the elements. */ |
| 2613 |
for (elem = 0; elem < nelems; elem++) |
| 2614 |
{ |
| 2615 |
dtp->u.p.item_count++; |
| 2616 |
formatted_transfer_scalar_read (dtp, type, tmp + stride*elem, kind, size); |
| 2617 |
} |
| 2618 |
} |
| 2619 |
else |
| 2620 |
{ |
| 2621 |
/* Big loop over all the elements. */ |
| 2622 |
for (elem = 0; elem < nelems; elem++) |
| 2623 |
{ |
| 2624 |
dtp->u.p.item_count++; |
| 2625 |
formatted_transfer_scalar_write (dtp, type, tmp + stride*elem, kind, size); |
| 2626 |
} |
| 2627 |
} |
| 2628 |
} |
| 2629 |
|
| 2630 |
/* Wrapper function for I/O of scalar types. If this should be an async I/O |
| 2631 |
request, queue it. For a synchronous write on an async unit, perform the |
| 2632 |
wait operation and return an error. For all synchronous writes, call the |
| 2633 |
right transfer function. */ |
| 2634 |
|
| 2635 |
static void |
| 2636 |
wrap_scalar_transfer (st_parameter_dt *dtp, bt type, void *p, int kind, |
| 2637 |
size_t size, size_t n_elem) |
| 2638 |
{ |
| 2639 |
if (dtp->u.p.current_unit && dtp->u.p.current_unit->au) |
| 2640 |
{ |
| 2641 |
if (dtp->u.p.async) |
| 2642 |
{ |
| 2643 |
transfer_args args; |
| 2644 |
args.scalar.transfer = dtp->u.p.transfer; |
| 2645 |
args.scalar.arg_bt = type; |
| 2646 |
args.scalar.data = p; |
| 2647 |
args.scalar.i = kind; |
| 2648 |
args.scalar.s1 = size; |
| 2649 |
args.scalar.s2 = n_elem; |
| 2650 |
enqueue_transfer (dtp->u.p.current_unit->au, &args, |
| 2651 |
AIO_TRANSFER_SCALAR); |
| 2652 |
return; |
| 2653 |
} |
| 2654 |
} |
| 2655 |
/* Come here if there was no asynchronous I/O to be scheduled. */ |
| 2656 |
if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK) |
| 2657 |
return; |
| 2658 |
|
| 2659 |
dtp->u.p.transfer (dtp, type, p, kind, size, 1); |
| 2660 |
} |
| 2661 |
|
| 2662 |
|
| 2663 |
/* Data transfer entry points. The type of the data entity is |
| 2664 |
implicit in the subroutine call. This prevents us from having to |
| 2665 |
share a common enum with the compiler. */ |
| 2666 |
|
| 2667 |
void |
| 2668 |
transfer_integer (st_parameter_dt *dtp, void *p, int kind) |
| 2669 |
{ |
| 2670 |
wrap_scalar_transfer (dtp, BT_INTEGER, p, kind, kind, 1); |
| 2671 |
} |
| 2672 |
|
| 2673 |
void |
| 2674 |
transfer_integer_write (st_parameter_dt *dtp, void *p, int kind) |
| 2675 |
{ |
| 2676 |
transfer_integer (dtp, p, kind); |
| 2677 |
} |
| 2678 |
|
| 2679 |
void |
| 2680 |
transfer_unsigned (st_parameter_dt *dtp, void *p, int kind) |
| 2681 |
{ |
| 2682 |
wrap_scalar_transfer (dtp, BT_UNSIGNED, p, kind, kind, 1); |
| 2683 |
} |
| 2684 |
|
| 2685 |
void |
| 2686 |
transfer_unsigned_write (st_parameter_dt *dtp, void *p, int kind) |
| 2687 |
{ |
| 2688 |
transfer_unsigned (dtp, p, kind); |
| 2689 |
} |
| 2690 |
|
| 2691 |
void |
| 2692 |
transfer_real (st_parameter_dt *dtp, void *p, int kind) |
| 2693 |
{ |
| 2694 |
size_t size; |
| 2695 |
if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK) |
| 2696 |
return; |
| 2697 |
size = size_from_real_kind (kind); |
| 2698 |
wrap_scalar_transfer (dtp, BT_REAL, p, kind, size, 1); |
| 2699 |
} |
| 2700 |
|
| 2701 |
void |
| 2702 |
transfer_real_write (st_parameter_dt *dtp, void *p, int kind) |
| 2703 |
{ |
| 2704 |
transfer_real (dtp, p, kind); |
| 2705 |
} |
| 2706 |
|
| 2707 |
void |
| 2708 |
transfer_logical (st_parameter_dt *dtp, void *p, int kind) |
| 2709 |
{ |
| 2710 |
wrap_scalar_transfer (dtp, BT_LOGICAL, p, kind, kind, 1); |
| 2711 |
} |
| 2712 |
|
| 2713 |
void |
| 2714 |
transfer_logical_write (st_parameter_dt *dtp, void *p, int kind) |
| 2715 |
{ |
| 2716 |
transfer_logical (dtp, p, kind); |
| 2717 |
} |
| 2718 |
|
| 2719 |
void |
| 2720 |
transfer_character (st_parameter_dt *dtp, void *p, gfc_charlen_type len) |
| 2721 |
{ |
| 2722 |
static char *empty_string[0]; |
| 2723 |
|
| 2724 |
if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK) |
| 2725 |
return; |
| 2726 |
|
| 2727 |
/* Strings of zero length can have p == NULL, which confuses the |
| 2728 |
transfer routines into thinking we need more data elements. To avoid |
| 2729 |
this, we give them a nice pointer. */ |
| 2730 |
if (len == 0 && p == NULL) |
| 2731 |
p = empty_string; |
| 2732 |
|
| 2733 |
/* Set kind here to 1. */ |
| 2734 |
wrap_scalar_transfer (dtp, BT_CHARACTER, p, 1, len, 1); |
| 2735 |
} |
| 2736 |
|
| 2737 |
void |
| 2738 |
transfer_character_write (st_parameter_dt *dtp, void *p, gfc_charlen_type len) |
| 2739 |
{ |
| 2740 |
transfer_character (dtp, p, len); |
| 2741 |
} |
| 2742 |
|
| 2743 |
void |
| 2744 |
transfer_character_wide (st_parameter_dt *dtp, void *p, gfc_charlen_type len, int kind) |
| 2745 |
{ |
| 2746 |
static char *empty_string[0]; |
| 2747 |
|
| 2748 |
if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK) |
| 2749 |
return; |
| 2750 |
|
| 2751 |
/* Strings of zero length can have p == NULL, which confuses the |
| 2752 |
transfer routines into thinking we need more data elements. To avoid |
| 2753 |
this, we give them a nice pointer. */ |
| 2754 |
if (len == 0 && p == NULL) |
| 2755 |
p = empty_string; |
| 2756 |
|
| 2757 |
/* Here we pass the actual kind value. */ |
| 2758 |
wrap_scalar_transfer (dtp, BT_CHARACTER, p, kind, len, 1); |
| 2759 |
} |
| 2760 |
|
| 2761 |
void |
| 2762 |
transfer_character_wide_write (st_parameter_dt *dtp, void *p, gfc_charlen_type len, int kind) |
| 2763 |
{ |
| 2764 |
transfer_character_wide (dtp, p, len, kind); |
| 2765 |
} |
| 2766 |
|
| 2767 |
void |
| 2768 |
transfer_complex (st_parameter_dt *dtp, void *p, int kind) |
| 2769 |
{ |
| 2770 |
size_t size; |
| 2771 |
if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK) |
| 2772 |
return; |
| 2773 |
size = size_from_complex_kind (kind); |
| 2774 |
wrap_scalar_transfer (dtp, BT_COMPLEX, p, kind, size, 1); |
| 2775 |
} |
| 2776 |
|
| 2777 |
void |
| 2778 |
transfer_complex_write (st_parameter_dt *dtp, void *p, int kind) |
| 2779 |
{ |
| 2780 |
transfer_complex (dtp, p, kind); |
| 2781 |
} |
| 2782 |
|
| 2783 |
void |
| 2784 |
transfer_array_inner (st_parameter_dt *dtp, gfc_array_char *desc, int kind, |
| 2785 |
gfc_charlen_type charlen) |
| 2786 |
{ |
| 2787 |
index_type count[GFC_MAX_DIMENSIONS]; |
| 2788 |
index_type extent[GFC_MAX_DIMENSIONS]; |
| 2789 |
index_type stride[GFC_MAX_DIMENSIONS]; |
| 2790 |
index_type stride0, rank, size, n; |
| 2791 |
size_t tsize; |
| 2792 |
char *data; |
| 2793 |
bt iotype; |
| 2794 |
|
| 2795 |
/* Adjust item_count before emitting error message. */ |
| 2796 |
|
| 2797 |
if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK) |
| 2798 |
return; |
| 2799 |
|
| 2800 |
iotype = (bt) GFC_DESCRIPTOR_TYPE (desc); |
| 2801 |
size = iotype == BT_CHARACTER ? charlen : GFC_DESCRIPTOR_SIZE (desc); |
| 2802 |
|
| 2803 |
rank = GFC_DESCRIPTOR_RANK (desc); |
| 2804 |
|
| 2805 |
for (n = 0; n < rank; n++) |
| 2806 |
{ |
| 2807 |
count[n] = 0; |
| 2808 |
stride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(desc,n); |
| 2809 |
extent[n] = GFC_DESCRIPTOR_EXTENT(desc,n); |
| 2810 |
|
| 2811 |
/* If the extent of even one dimension is zero, then the entire |
| 2812 |
array section contains zero elements, so we return after writing |
| 2813 |
a zero array record. */ |
| 2814 |
if (extent[n] <= 0) |
| 2815 |
{ |
| 2816 |
data = NULL; |
| 2817 |
tsize = 0; |
| 2818 |
dtp->u.p.transfer (dtp, iotype, data, kind, size, tsize); |
| 2819 |
return; |
| 2820 |
} |
| 2821 |
} |
| 2822 |
|
| 2823 |
stride0 = stride[0]; |
| 2824 |
|
| 2825 |
/* If the innermost dimension has a stride of 1, we can do the transfer |
| 2826 |
in contiguous chunks. */ |
| 2827 |
if (stride0 == size) |
| 2828 |
tsize = extent[0]; |
| 2829 |
else |
| 2830 |
tsize = 1; |
| 2831 |
|
| 2832 |
data = GFC_DESCRIPTOR_DATA (desc); |
| 2833 |
|
| 2834 |
/* When reading, we need to check endfile conditions so we do not miss |
| 2835 |
an END=label. Make this separate so we do not have an extra test |
| 2836 |
in a tight loop when it is not needed. */ |
| 2837 |
|
| 2838 |
if (dtp->u.p.current_unit && dtp->u.p.mode == READING) |
| 2839 |
{ |
| 2840 |
while (data) |
| 2841 |
{ |
| 2842 |
if (unlikely (dtp->u.p.current_unit->endfile == AFTER_ENDFILE)) |
| 2843 |
return; |
| 2844 |
|
| 2845 |
dtp->u.p.transfer (dtp, iotype, data, kind, size, tsize); |
| 2846 |
data += stride0 * tsize; |
| 2847 |
count[0] += tsize; |
| 2848 |
n = 0; |
| 2849 |
while (count[n] == extent[n]) |
| 2850 |
{ |
| 2851 |
count[n] = 0; |
| 2852 |
data -= stride[n] * extent[n]; |
| 2853 |
n++; |
| 2854 |
if (n == rank) |
| 2855 |
{ |
| 2856 |
data = NULL; |
| 2857 |
break; |
| 2858 |
} |
| 2859 |
else |
| 2860 |
{ |
| 2861 |
count[n]++; |
| 2862 |
data += stride[n]; |
| 2863 |
} |
| 2864 |
} |
| 2865 |
} |
| 2866 |
} |
| 2867 |
else |
| 2868 |
{ |
| 2869 |
while (data) |
| 2870 |
{ |
| 2871 |
dtp->u.p.transfer (dtp, iotype, data, kind, size, tsize); |
| 2872 |
data += stride0 * tsize; |
| 2873 |
count[0] += tsize; |
| 2874 |
n = 0; |
| 2875 |
while (count[n] == extent[n]) |
| 2876 |
{ |
| 2877 |
count[n] = 0; |
| 2878 |
data -= stride[n] * extent[n]; |
| 2879 |
n++; |
| 2880 |
if (n == rank) |
| 2881 |
{ |
| 2882 |
data = NULL; |
| 2883 |
break; |
| 2884 |
} |
| 2885 |
else |
| 2886 |
{ |
| 2887 |
count[n]++; |
| 2888 |
data += stride[n]; |
| 2889 |
} |
| 2890 |
} |
| 2891 |
} |
| 2892 |
} |
| 2893 |
} |
| 2894 |
|
| 2895 |
void |
| 2896 |
transfer_array (st_parameter_dt *dtp, gfc_array_char *desc, int kind, |
| 2897 |
gfc_charlen_type charlen) |
| 2898 |
{ |
| 2899 |
if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK) |
| 2900 |
return; |
| 2901 |
|
| 2902 |
if (dtp->u.p.current_unit && dtp->u.p.current_unit->au) |
| 2903 |
{ |
| 2904 |
if (dtp->u.p.async) |
| 2905 |
{ |
| 2906 |
transfer_args args; |
| 2907 |
size_t sz = sizeof (gfc_array_char) |
| 2908 |
+ sizeof (descriptor_dimension) |
| 2909 |
* GFC_DESCRIPTOR_RANK (desc); |
| 2910 |
args.array.desc = xmalloc (sz); |
| 2911 |
NOTE ("desc = %p", (void *) args.array.desc); |
| 2912 |
memcpy (args.array.desc, desc, sz); |
| 2913 |
args.array.kind = kind; |
| 2914 |
args.array.charlen = charlen; |
| 2915 |
enqueue_transfer (dtp->u.p.current_unit->au, &args, |
| 2916 |
AIO_TRANSFER_ARRAY); |
| 2917 |
return; |
| 2918 |
} |
| 2919 |
} |
| 2920 |
/* Come here if there was no asynchronous I/O to be scheduled. */ |
| 2921 |
transfer_array_inner (dtp, desc, kind, charlen); |
| 2922 |
} |
| 2923 |
|
| 2924 |
|
| 2925 |
void |
| 2926 |
transfer_array_write (st_parameter_dt *dtp, gfc_array_char *desc, int kind, |
| 2927 |
gfc_charlen_type charlen) |
| 2928 |
{ |
| 2929 |
transfer_array (dtp, desc, kind, charlen); |
| 2930 |
} |
| 2931 |
|
| 2932 |
|
| 2933 |
/* User defined input/output iomsg. */ |
| 2934 |
|
| 2935 |
#define IOMSG_LEN 256 |
| 2936 |
|
| 2937 |
void |
| 2938 |
transfer_derived (st_parameter_dt *parent, void *dtio_source, void *dtio_proc) |
| 2939 |
{ |
| 2940 |
if (parent->u.p.current_unit) |
| 2941 |
{ |
| 2942 |
if (parent->u.p.current_unit->flags.form == FORM_UNFORMATTED) |
| 2943 |
parent->u.p.ufdtio_ptr = (unformatted_dtio) dtio_proc; |
| 2944 |
else |
| 2945 |
parent->u.p.fdtio_ptr = (formatted_dtio) dtio_proc; |
| 2946 |
} |
| 2947 |
wrap_scalar_transfer (parent, BT_CLASS, dtio_source, 0, 0, 1); |
| 2948 |
} |
| 2949 |
|
| 2950 |
|
| 2951 |
/* Preposition a sequential unformatted file while reading. */ |
| 2952 |
|
| 2953 |
static void |
| 2954 |
us_read (st_parameter_dt *dtp, int continued) |
| 2955 |
{ |
| 2956 |
ssize_t n, nr; |
| 2957 |
GFC_INTEGER_4 i4; |
| 2958 |
GFC_INTEGER_8 i8; |
| 2959 |
gfc_offset i; |
| 2960 |
|
| 2961 |
if (compile_options.record_marker == 0) |
| 2962 |
n = sizeof (GFC_INTEGER_4); |
| 2963 |
else |
| 2964 |
n = compile_options.record_marker; |
| 2965 |
|
| 2966 |
nr = sread (dtp->u.p.current_unit->s, &i, n); |
| 2967 |
if (unlikely (nr < 0)) |
| 2968 |
{ |
| 2969 |
generate_error (&dtp->common, LIBERROR_BAD_US, NULL); |
| 2970 |
return; |
| 2971 |
} |
| 2972 |
else if (nr == 0) |
| 2973 |
{ |
| 2974 |
hit_eof (dtp); |
| 2975 |
return; /* end of file */ |
| 2976 |
} |
| 2977 |
else if (unlikely (n != nr)) |
| 2978 |
{ |
| 2979 |
generate_error (&dtp->common, LIBERROR_BAD_US, NULL); |
| 2980 |
return; |
| 2981 |
} |
| 2982 |
|
| 2983 |
int convert = dtp->u.p.current_unit->flags.convert; |
| 2984 |
#ifdef HAVE_GFC_REAL_17 |
| 2985 |
convert &= ~(GFC_CONVERT_R16_IEEE | GFC_CONVERT_R16_IBM); |
| 2986 |
#endif |
| 2987 |
/* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */ |
| 2988 |
if (likely (convert == GFC_CONVERT_NATIVE)) |
| 2989 |
{ |
| 2990 |
switch (nr) |
| 2991 |
{ |
| 2992 |
case sizeof(GFC_INTEGER_4): |
| 2993 |
memcpy (&i4, &i, sizeof (i4)); |
| 2994 |
i = i4; |
| 2995 |
break; |
| 2996 |
|
| 2997 |
case sizeof(GFC_INTEGER_8): |
| 2998 |
memcpy (&i8, &i, sizeof (i8)); |
| 2999 |
i = i8; |
| 3000 |
break; |
| 3001 |
|
| 3002 |
default: |
| 3003 |
runtime_error ("Illegal value for record marker"); |
| 3004 |
break; |
| 3005 |
} |
| 3006 |
} |
| 3007 |
else |
| 3008 |
{ |
| 3009 |
uint32_t u32; |
| 3010 |
uint64_t u64; |
| 3011 |
switch (nr) |
| 3012 |
{ |
| 3013 |
case sizeof(GFC_INTEGER_4): |
| 3014 |
memcpy (&u32, &i, sizeof (u32)); |
| 3015 |
u32 = __builtin_bswap32 (u32); |
| 3016 |
memcpy (&i4, &u32, sizeof (i4)); |
| 3017 |
i = i4; |
| 3018 |
break; |
| 3019 |
|
| 3020 |
case sizeof(GFC_INTEGER_8): |
| 3021 |
memcpy (&u64, &i, sizeof (u64)); |
| 3022 |
u64 = __builtin_bswap64 (u64); |
| 3023 |
memcpy (&i8, &u64, sizeof (i8)); |
| 3024 |
i = i8; |
| 3025 |
break; |
| 3026 |
|
| 3027 |
default: |
| 3028 |
runtime_error ("Illegal value for record marker"); |
| 3029 |
break; |
| 3030 |
} |
| 3031 |
} |
| 3032 |
|
| 3033 |
if (i >= 0) |
| 3034 |
{ |
| 3035 |
dtp->u.p.current_unit->bytes_left_subrecord = i; |
| 3036 |
dtp->u.p.current_unit->continued = 0; |
| 3037 |
} |
| 3038 |
else |
| 3039 |
{ |
| 3040 |
dtp->u.p.current_unit->bytes_left_subrecord = -i; |
| 3041 |
dtp->u.p.current_unit->continued = 1; |
| 3042 |
} |
| 3043 |
|
| 3044 |
if (! continued) |
| 3045 |
dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl; |
| 3046 |
} |
| 3047 |
|
| 3048 |
|
| 3049 |
/* Preposition a sequential unformatted file while writing. This |
| 3050 |
amount to writing a bogus length that will be filled in later. */ |
| 3051 |
|
| 3052 |
static void |
| 3053 |
us_write (st_parameter_dt *dtp, int continued) |
| 3054 |
{ |
| 3055 |
ssize_t nbytes; |
| 3056 |
gfc_offset dummy; |
| 3057 |
|
| 3058 |
dummy = 0; |
| 3059 |
|
| 3060 |
if (compile_options.record_marker == 0) |
| 3061 |
nbytes = sizeof (GFC_INTEGER_4); |
| 3062 |
else |
| 3063 |
nbytes = compile_options.record_marker ; |
| 3064 |
|
| 3065 |
if (swrite (dtp->u.p.current_unit->s, &dummy, nbytes) != nbytes) |
| 3066 |
generate_error (&dtp->common, LIBERROR_OS, NULL); |
| 3067 |
|
| 3068 |
/* For sequential unformatted, if RECL= was not specified in the OPEN |
| 3069 |
we write until we have more bytes than can fit in the subrecord |
| 3070 |
markers, then we write a new subrecord. */ |
| 3071 |
|
| 3072 |
dtp->u.p.current_unit->bytes_left_subrecord = |
| 3073 |
dtp->u.p.current_unit->recl_subrecord; |
| 3074 |
dtp->u.p.current_unit->continued = continued; |
| 3075 |
} |
| 3076 |
|
| 3077 |
|
| 3078 |
/* Position to the next record prior to transfer. We are assumed to |
| 3079 |
be before the next record. We also calculate the bytes in the next |
| 3080 |
record. */ |
| 3081 |
|
| 3082 |
static void |
| 3083 |
pre_position (st_parameter_dt *dtp) |
| 3084 |
{ |
| 3085 |
if (dtp->u.p.current_unit->current_record) |
| 3086 |
return; /* Already positioned. */ |
| 3087 |
|
| 3088 |
switch (current_mode (dtp)) |
| 3089 |
{ |
| 3090 |
case FORMATTED_STREAM: |
| 3091 |
case UNFORMATTED_STREAM: |
| 3092 |
/* There are no records with stream I/O. If the position was specified |
| 3093 |
data_transfer_init has already positioned the file. If no position |
| 3094 |
was specified, we continue from where we last left off. I.e. |
| 3095 |
there is nothing to do here. */ |
| 3096 |
break; |
| 3097 |
|
| 3098 |
case UNFORMATTED_SEQUENTIAL: |
| 3099 |
if (dtp->u.p.mode == READING) |
| 3100 |
us_read (dtp, 0); |
| 3101 |
else |
| 3102 |
us_write (dtp, 0); |
| 3103 |
|
| 3104 |
break; |
| 3105 |
|
| 3106 |
case FORMATTED_SEQUENTIAL: |
| 3107 |
case FORMATTED_DIRECT: |
| 3108 |
case UNFORMATTED_DIRECT: |
| 3109 |
dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl; |
| 3110 |
break; |
| 3111 |
case FORMATTED_UNSPECIFIED: |
| 3112 |
gcc_unreachable (); |
| 3113 |
} |
| 3114 |
|
| 3115 |
dtp->u.p.current_unit->current_record = 1; |
| 3116 |
} |
| 3117 |
|
| 3118 |
|
| 3119 |
/* Initialize things for a data transfer. This code is common for |
| 3120 |
both reading and writing. */ |
| 3121 |
|
| 3122 |
static void |
| 3123 |
data_transfer_init (st_parameter_dt *dtp, int read_flag) |
| 3124 |
{ |
| 3125 |
unit_flags u_flags; /* Used for creating a unit if needed. */ |
| 3126 |
GFC_INTEGER_4 cf = dtp->common.flags; |
| 3127 |
namelist_info *ionml; |
| 3128 |
async_unit *au; |
| 3129 |
|
| 3130 |
NOTE ("data_transfer_init"); |
| 3131 |
|
| 3132 |
ionml = ((cf & IOPARM_DT_IONML_SET) != 0) ? dtp->u.p.ionml : NULL; |
| 3133 |
|
| 3134 |
memset (&dtp->u.p, 0, sizeof (dtp->u.p)); |
| 3135 |
|
| 3136 |
dtp->u.p.ionml = ionml; |
| 3137 |
dtp->u.p.mode = read_flag ? READING : WRITING; |
| 3138 |
dtp->u.p.namelist_mode = 0; |
| 3139 |
dtp->u.p.cc.len = 0; |
| 3140 |
|
| 3141 |
if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK) |
| 3142 |
return; |
| 3143 |
|
| 3144 |
dtp->u.p.current_unit = get_unit (dtp, 1); |
| 3145 |
|
| 3146 |
if (dtp->u.p.current_unit == NULL) |
| 3147 |
{ |
| 3148 |
/* This means we tried to access an external unit < 0 without |
| 3149 |
having opened it first with NEWUNIT=. */ |
| 3150 |
generate_error (&dtp->common, LIBERROR_BAD_OPTION, |
| 3151 |
"Unit number is negative and unit was not already " |
| 3152 |
"opened with OPEN(NEWUNIT=...)"); |
| 3153 |
return; |
| 3154 |
} |
| 3155 |
else if (dtp->u.p.current_unit->s == NULL) |
| 3156 |
{ /* Open the unit with some default flags. */ |
| 3157 |
st_parameter_open opp; |
| 3158 |
unit_convert conv; |
| 3159 |
NOTE ("Open the unit with some default flags."); |
| 3160 |
memset (&u_flags, '\0', sizeof (u_flags)); |
| 3161 |
u_flags.access = ACCESS_SEQUENTIAL; |
| 3162 |
u_flags.action = ACTION_READWRITE; |
| 3163 |
|
| 3164 |
/* Is it unformatted? */ |
| 3165 |
if (!(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT |
| 3166 |
| IOPARM_DT_IONML_SET))) |
| 3167 |
u_flags.form = FORM_UNFORMATTED; |
| 3168 |
else |
| 3169 |
u_flags.form = FORM_UNSPECIFIED; |
| 3170 |
|
| 3171 |
u_flags.delim = DELIM_UNSPECIFIED; |
| 3172 |
u_flags.blank = BLANK_UNSPECIFIED; |
| 3173 |
u_flags.pad = PAD_UNSPECIFIED; |
| 3174 |
u_flags.decimal = DECIMAL_UNSPECIFIED; |
| 3175 |
u_flags.encoding = ENCODING_UNSPECIFIED; |
| 3176 |
u_flags.async = ASYNC_UNSPECIFIED; |
| 3177 |
u_flags.round = ROUND_UNSPECIFIED; |
| 3178 |
u_flags.sign = SIGN_UNSPECIFIED; |
| 3179 |
u_flags.share = SHARE_UNSPECIFIED; |
| 3180 |
u_flags.cc = CC_UNSPECIFIED; |
| 3181 |
u_flags.readonly = 0; |
| 3182 |
|
| 3183 |
u_flags.status = STATUS_UNKNOWN; |
| 3184 |
|
| 3185 |
conv = get_unformatted_convert (dtp->common.unit); |
| 3186 |
|
| 3187 |
if (conv == GFC_CONVERT_NONE) |
| 3188 |
conv = compile_options.convert; |
| 3189 |
|
| 3190 |
u_flags.convert = 0; |
| 3191 |
|
| 3192 |
#ifdef HAVE_GFC_REAL_17 |
| 3193 |
u_flags.convert = conv & (GFC_CONVERT_R16_IEEE | GFC_CONVERT_R16_IBM); |
| 3194 |
conv &= ~(GFC_CONVERT_R16_IEEE | GFC_CONVERT_R16_IBM); |
| 3195 |
#endif |
| 3196 |
|
| 3197 |
switch (conv) |
| 3198 |
{ |
| 3199 |
case GFC_CONVERT_NATIVE: |
| 3200 |
case GFC_CONVERT_SWAP: |
| 3201 |
break; |
| 3202 |
|
| 3203 |
case GFC_CONVERT_BIG: |
| 3204 |
conv = __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ ? GFC_CONVERT_NATIVE : GFC_CONVERT_SWAP; |
| 3205 |
break; |
| 3206 |
|
| 3207 |
case GFC_CONVERT_LITTLE: |
| 3208 |
conv = __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ ? GFC_CONVERT_SWAP : GFC_CONVERT_NATIVE; |
| 3209 |
break; |
| 3210 |
|
| 3211 |
default: |
| 3212 |
internal_error (&opp.common, "Illegal value for CONVERT"); |
| 3213 |
break; |
| 3214 |
} |
| 3215 |
|
| 3216 |
u_flags.convert |= conv; |
| 3217 |
|
| 3218 |
opp.common = dtp->common; |
| 3219 |
opp.common.flags &= IOPARM_COMMON_MASK; |
| 3220 |
dtp->u.p.current_unit = new_unit (&opp, dtp->u.p.current_unit, &u_flags); |
| 3221 |
dtp->common.flags &= ~IOPARM_COMMON_MASK; |
| 3222 |
dtp->common.flags |= (opp.common.flags & IOPARM_COMMON_MASK); |
| 3223 |
if (dtp->u.p.current_unit == NULL) |
| 3224 |
return; |
| 3225 |
} |
| 3226 |
|
| 3227 |
if (dtp->u.p.current_unit->child_dtio == 0) |
| 3228 |
{ |
| 3229 |
if ((cf & IOPARM_DT_HAS_SIZE) != 0) |
| 3230 |
{ |
| 3231 |
dtp->u.p.current_unit->has_size = true; |
| 3232 |
/* Initialize the count. */ |
| 3233 |
dtp->u.p.current_unit->size_used = 0; |
| 3234 |
} |
| 3235 |
else |
| 3236 |
dtp->u.p.current_unit->has_size = false; |
| 3237 |
} |
| 3238 |
else if (dtp->u.p.current_unit->internal_unit_kind > 0) |
| 3239 |
dtp->u.p.unit_is_internal = 1; |
| 3240 |
|
| 3241 |
if ((cf & IOPARM_DT_HAS_ASYNCHRONOUS) != 0) |
| 3242 |
{ |
| 3243 |
int f; |
| 3244 |
f = find_option (&dtp->common, dtp->asynchronous, dtp->asynchronous_len, |
| 3245 |
async_opt, "Bad ASYNCHRONOUS in data transfer " |
| 3246 |
"statement"); |
| 3247 |
if (f == ASYNC_YES && dtp->u.p.current_unit->flags.async != ASYNC_YES) |
| 3248 |
{ |
| 3249 |
generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT, |
| 3250 |
"ASYNCHRONOUS transfer without " |
| 3251 |
"ASYHCRONOUS='YES' in OPEN"); |
| 3252 |
return; |
| 3253 |
} |
| 3254 |
dtp->u.p.async = f == ASYNC_YES; |
| 3255 |
} |
| 3256 |
|
| 3257 |
au = dtp->u.p.current_unit->au; |
| 3258 |
if (au) |
| 3259 |
{ |
| 3260 |
if (dtp->u.p.async) |
| 3261 |
{ |
| 3262 |
/* If this is an asynchronous I/O statement, collect errors and |
| 3263 |
return if there are any. */ |
| 3264 |
if (collect_async_errors (&dtp->common, au)) |
| 3265 |
return; |
| 3266 |
} |
| 3267 |
else |
| 3268 |
{ |
| 3269 |
/* Synchronous statement: Perform a wait operation for any pending |
| 3270 |
asynchronous I/O. This needs to be done before all other error |
| 3271 |
checks. See F2008, 9.6.4.1. */ |
| 3272 |
if (async_wait (&(dtp->common), au)) |
| 3273 |
return; |
| 3274 |
} |
| 3275 |
} |
| 3276 |
|
| 3277 |
/* Check the action. */ |
| 3278 |
|
| 3279 |
if (read_flag && dtp->u.p.current_unit->flags.action == ACTION_WRITE) |
| 3280 |
{ |
| 3281 |
generate_error (&dtp->common, LIBERROR_BAD_ACTION, |
| 3282 |
"Cannot read from file opened for WRITE"); |
| 3283 |
return; |
| 3284 |
} |
| 3285 |
|
| 3286 |
if (!read_flag && dtp->u.p.current_unit->flags.action == ACTION_READ) |
| 3287 |
{ |
| 3288 |
generate_error (&dtp->common, LIBERROR_BAD_ACTION, |
| 3289 |
"Cannot write to file opened for READ"); |
| 3290 |
return; |
| 3291 |
} |
| 3292 |
|
| 3293 |
dtp->u.p.first_item = 1; |
| 3294 |
|
| 3295 |
/* Check the format. */ |
| 3296 |
|
| 3297 |
if ((cf & IOPARM_DT_HAS_FORMAT) != 0) |
| 3298 |
parse_format (dtp); |
| 3299 |
|
| 3300 |
if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED |
| 3301 |
&& (cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT)) |
| 3302 |
!= 0) |
| 3303 |
{ |
| 3304 |
generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT, |
| 3305 |
"Format present for UNFORMATTED data transfer"); |
| 3306 |
return; |
| 3307 |
} |
| 3308 |
|
| 3309 |
if ((cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0 && dtp->u.p.ionml != NULL) |
| 3310 |
{ |
| 3311 |
if ((cf & IOPARM_DT_HAS_FORMAT) != 0) |
| 3312 |
{ |
| 3313 |
generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT, |
| 3314 |
"A format cannot be specified with a namelist"); |
| 3315 |
return; |
| 3316 |
} |
| 3317 |
} |
| 3318 |
else if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED && |
| 3319 |
!(cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT))) |
| 3320 |
{ |
| 3321 |
generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT, |
| 3322 |
"Missing format for FORMATTED data transfer"); |
| 3323 |
return; |
| 3324 |
} |
| 3325 |
|
| 3326 |
if (is_internal_unit (dtp) |
| 3327 |
&& dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED) |
| 3328 |
{ |
| 3329 |
generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT, |
| 3330 |
"Internal file cannot be accessed by UNFORMATTED " |
| 3331 |
"data transfer"); |
| 3332 |
return; |
| 3333 |
} |
| 3334 |
|
| 3335 |
/* Check the record or position number. */ |
| 3336 |
|
| 3337 |
if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT |
| 3338 |
&& (cf & IOPARM_DT_HAS_REC) == 0) |
| 3339 |
{ |
| 3340 |
generate_error (&dtp->common, LIBERROR_MISSING_OPTION, |
| 3341 |
"Direct access data transfer requires record number"); |
| 3342 |
return; |
| 3343 |
} |
| 3344 |
|
| 3345 |
if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL) |
| 3346 |
{ |
| 3347 |
if ((cf & IOPARM_DT_HAS_REC) != 0) |
| 3348 |
{ |
| 3349 |
generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT, |
| 3350 |
"Record number not allowed for sequential access " |
| 3351 |
"data transfer"); |
| 3352 |
return; |
| 3353 |
} |
| 3354 |
|
| 3355 |
if (compile_options.warn_std && |
| 3356 |
dtp->u.p.current_unit->endfile == AFTER_ENDFILE) |
| 3357 |
{ |
| 3358 |
generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT, |
| 3359 |
"Sequential READ or WRITE not allowed after " |
| 3360 |
"EOF marker, possibly use REWIND or BACKSPACE"); |
| 3361 |
return; |
| 3362 |
} |
| 3363 |
} |
| 3364 |
|
| 3365 |
/* Process the ADVANCE option. */ |
| 3366 |
|
| 3367 |
dtp->u.p.advance_status |
| 3368 |
= !(cf & IOPARM_DT_HAS_ADVANCE) ? ADVANCE_UNSPECIFIED : |
| 3369 |
find_option (&dtp->common, dtp->advance, dtp->advance_len, advance_opt, |
| 3370 |
"Bad ADVANCE parameter in data transfer statement"); |
| 3371 |
|
| 3372 |
if (dtp->u.p.advance_status != ADVANCE_UNSPECIFIED) |
| 3373 |
{ |
| 3374 |
if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT) |
| 3375 |
{ |
| 3376 |
generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT, |
| 3377 |
"ADVANCE specification conflicts with sequential " |
| 3378 |
"access"); |
| 3379 |
return; |
| 3380 |
} |
| 3381 |
|
| 3382 |
if (is_internal_unit (dtp)) |
| 3383 |
{ |
| 3384 |
generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT, |
| 3385 |
"ADVANCE specification conflicts with internal file"); |
| 3386 |
return; |
| 3387 |
} |
| 3388 |
|
| 3389 |
if ((cf & (IOPARM_DT_HAS_FORMAT | IOPARM_DT_LIST_FORMAT)) |
| 3390 |
!= IOPARM_DT_HAS_FORMAT) |
| 3391 |
{ |
| 3392 |
generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT, |
| 3393 |
"ADVANCE specification requires an explicit format"); |
| 3394 |
return; |
| 3395 |
} |
| 3396 |
} |
| 3397 |
|
| 3398 |
/* Child IO is non-advancing and any ADVANCE= specifier is ignored. |
| 3399 |
F2008 9.6.2.4 */ |
| 3400 |
if (dtp->u.p.current_unit->child_dtio > 0) |
| 3401 |
dtp->u.p.advance_status = ADVANCE_NO; |
| 3402 |
|
| 3403 |
if (read_flag) |
| 3404 |
{ |
| 3405 |
dtp->u.p.current_unit->previous_nonadvancing_write = 0; |
| 3406 |
|
| 3407 |
if ((cf & IOPARM_EOR) != 0 && dtp->u.p.advance_status != ADVANCE_NO) |
| 3408 |
{ |
| 3409 |
generate_error (&dtp->common, LIBERROR_MISSING_OPTION, |
| 3410 |
"EOR specification requires an ADVANCE specification " |
| 3411 |
"of NO"); |
| 3412 |
return; |
| 3413 |
} |
| 3414 |
|
| 3415 |
if ((cf & IOPARM_DT_HAS_SIZE) != 0 |
| 3416 |
&& dtp->u.p.advance_status != ADVANCE_NO) |
| 3417 |
{ |
| 3418 |
generate_error (&dtp->common, LIBERROR_MISSING_OPTION, |
| 3419 |
"SIZE specification requires an ADVANCE " |
| 3420 |
"specification of NO"); |
| 3421 |
return; |
| 3422 |
} |
| 3423 |
} |
| 3424 |
else |
| 3425 |
{ /* Write constraints. */ |
| 3426 |
if ((cf & IOPARM_END) != 0) |
| 3427 |
{ |
| 3428 |
generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT, |
| 3429 |
"END specification cannot appear in a write " |
| 3430 |
"statement"); |
| 3431 |
return; |
| 3432 |
} |
| 3433 |
|
| 3434 |
if ((cf & IOPARM_EOR) != 0) |
| 3435 |
{ |
| 3436 |
generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT, |
| 3437 |
"EOR specification cannot appear in a write " |
| 3438 |
"statement"); |
| 3439 |
return; |
| 3440 |
} |
| 3441 |
|
| 3442 |
if ((cf & IOPARM_DT_HAS_SIZE) != 0) |
| 3443 |
{ |
| 3444 |
generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT, |
| 3445 |
"SIZE specification cannot appear in a write " |
| 3446 |
"statement"); |
| 3447 |
return; |
| 3448 |
} |
| 3449 |
} |
| 3450 |
|
| 3451 |
if (dtp->u.p.advance_status == ADVANCE_UNSPECIFIED) |
| 3452 |
dtp->u.p.advance_status = ADVANCE_YES; |
| 3453 |
|
| 3454 |
/* Check the decimal mode. */ |
| 3455 |
dtp->u.p.current_unit->decimal_status |
| 3456 |
= !(cf & IOPARM_DT_HAS_DECIMAL) ? DECIMAL_UNSPECIFIED : |
| 3457 |
find_option (&dtp->common, dtp->decimal, dtp->decimal_len, |
| 3458 |
decimal_opt, "Bad DECIMAL parameter in data transfer " |
| 3459 |
"statement"); |
| 3460 |
|
| 3461 |
if (dtp->u.p.current_unit->decimal_status == DECIMAL_UNSPECIFIED) |
| 3462 |
dtp->u.p.current_unit->decimal_status = dtp->u.p.current_unit->flags.decimal; |
| 3463 |
|
| 3464 |
/* Check the round mode. */ |
| 3465 |
dtp->u.p.current_unit->round_status |
| 3466 |
= !(cf & IOPARM_DT_HAS_ROUND) ? ROUND_UNSPECIFIED : |
| 3467 |
find_option (&dtp->common, dtp->round, dtp->round_len, |
| 3468 |
round_opt, "Bad ROUND parameter in data transfer " |
| 3469 |
"statement"); |
| 3470 |
|
| 3471 |
if (dtp->u.p.current_unit->round_status == ROUND_UNSPECIFIED) |
| 3472 |
dtp->u.p.current_unit->round_status = dtp->u.p.current_unit->flags.round; |
| 3473 |
|
| 3474 |
/* Check the sign mode. */ |
| 3475 |
dtp->u.p.sign_status |
| 3476 |
= !(cf & IOPARM_DT_HAS_SIGN) ? SIGN_UNSPECIFIED : |
| 3477 |
find_option (&dtp->common, dtp->sign, dtp->sign_len, sign_opt, |
| 3478 |
"Bad SIGN parameter in data transfer statement"); |
| 3479 |
|
| 3480 |
if (dtp->u.p.sign_status == SIGN_UNSPECIFIED) |
| 3481 |
dtp->u.p.sign_status = dtp->u.p.current_unit->flags.sign; |
| 3482 |
|
| 3483 |
/* Check the blank mode. */ |
| 3484 |
dtp->u.p.blank_status |
| 3485 |
= !(cf & IOPARM_DT_HAS_BLANK) ? BLANK_UNSPECIFIED : |
| 3486 |
find_option (&dtp->common, dtp->blank, dtp->blank_len, |
| 3487 |
blank_opt, |
| 3488 |
"Bad BLANK parameter in data transfer statement"); |
| 3489 |
|
| 3490 |
if (dtp->u.p.blank_status == BLANK_UNSPECIFIED) |
| 3491 |
dtp->u.p.blank_status = dtp->u.p.current_unit->flags.blank; |
| 3492 |
|
| 3493 |
/* Check the delim mode. */ |
| 3494 |
dtp->u.p.current_unit->delim_status |
| 3495 |
= !(cf & IOPARM_DT_HAS_DELIM) ? DELIM_UNSPECIFIED : |
| 3496 |
find_option (&dtp->common, dtp->delim, dtp->delim_len, |
| 3497 |
delim_opt, "Bad DELIM parameter in data transfer statement"); |
| 3498 |
|
| 3499 |
if (dtp->u.p.current_unit->delim_status == DELIM_UNSPECIFIED) |
| 3500 |
{ |
| 3501 |
if (ionml && dtp->u.p.current_unit->flags.delim == DELIM_UNSPECIFIED) |
| 3502 |
dtp->u.p.current_unit->delim_status = DELIM_QUOTE; |
| 3503 |
else |
| 3504 |
dtp->u.p.current_unit->delim_status = dtp->u.p.current_unit->flags.delim; |
| 3505 |
} |
| 3506 |
|
| 3507 |
/* Check the pad mode. */ |
| 3508 |
dtp->u.p.current_unit->pad_status |
| 3509 |
= !(cf & IOPARM_DT_HAS_PAD) ? PAD_UNSPECIFIED : |
| 3510 |
find_option (&dtp->common, dtp->pad, dtp->pad_len, pad_opt, |
| 3511 |
"Bad PAD parameter in data transfer statement"); |
| 3512 |
|
| 3513 |
if (dtp->u.p.current_unit->pad_status == PAD_UNSPECIFIED) |
| 3514 |
dtp->u.p.current_unit->pad_status = dtp->u.p.current_unit->flags.pad; |
| 3515 |
|
| 3516 |
/* Set up the subroutine that will handle the transfers. */ |
| 3517 |
|
| 3518 |
if (read_flag) |
| 3519 |
{ |
| 3520 |
if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED) |
| 3521 |
dtp->u.p.transfer = unformatted_read; |
| 3522 |
else |
| 3523 |
{ |
| 3524 |
if ((cf & IOPARM_DT_LIST_FORMAT) != 0) |
| 3525 |
dtp->u.p.transfer = list_formatted_read; |
| 3526 |
else |
| 3527 |
dtp->u.p.transfer = formatted_transfer; |
| 3528 |
} |
| 3529 |
} |
| 3530 |
else |
| 3531 |
{ |
| 3532 |
if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED) |
| 3533 |
dtp->u.p.transfer = unformatted_write; |
| 3534 |
else |
| 3535 |
{ |
| 3536 |
if ((cf & IOPARM_DT_LIST_FORMAT) != 0) |
| 3537 |
dtp->u.p.transfer = list_formatted_write; |
| 3538 |
else |
| 3539 |
dtp->u.p.transfer = formatted_transfer; |
| 3540 |
} |
| 3541 |
} |
| 3542 |
|
| 3543 |
if (au && dtp->u.p.async) |
| 3544 |
{ |
| 3545 |
NOTE ("enqueue_data_transfer"); |
| 3546 |
enqueue_data_transfer_init (au, dtp, read_flag); |
| 3547 |
} |
| 3548 |
else |
| 3549 |
{ |
| 3550 |
NOTE ("invoking data_transfer_init_worker"); |
| 3551 |
data_transfer_init_worker (dtp, read_flag); |
| 3552 |
} |
| 3553 |
} |
| 3554 |
|
| 3555 |
void |
| 3556 |
data_transfer_init_worker (st_parameter_dt *dtp, int read_flag) |
| 3557 |
{ |
| 3558 |
GFC_INTEGER_4 cf = dtp->common.flags; |
| 3559 |
|
| 3560 |
NOTE ("starting worker..."); |
| 3561 |
|
| 3562 |
if (read_flag && dtp->u.p.current_unit->flags.form != FORM_UNFORMATTED |
| 3563 |
&& ((cf & IOPARM_DT_LIST_FORMAT) != 0) |
| 3564 |
&& dtp->u.p.current_unit->child_dtio == 0) |
| 3565 |
dtp->u.p.current_unit->last_char = EOF - 1; |
| 3566 |
|
| 3567 |
/* Check to see if we might be reading what we wrote before */ |
| 3568 |
|
| 3569 |
if (dtp->u.p.mode != dtp->u.p.current_unit->mode |
| 3570 |
&& !is_internal_unit (dtp)) |
| 3571 |
{ |
| 3572 |
int pos = fbuf_reset (dtp->u.p.current_unit); |
| 3573 |
if (pos != 0) |
| 3574 |
sseek (dtp->u.p.current_unit->s, pos, SEEK_CUR); |
| 3575 |
sflush(dtp->u.p.current_unit->s); |
| 3576 |
} |
| 3577 |
|
| 3578 |
/* Check the POS= specifier: that it is in range and that it is used with a |
| 3579 |
unit that has been connected for STREAM access. F2003 9.5.1.10. */ |
| 3580 |
|
| 3581 |
if (((cf & IOPARM_DT_HAS_POS) != 0)) |
| 3582 |
{ |
| 3583 |
if (is_stream_io (dtp)) |
| 3584 |
{ |
| 3585 |
|
| 3586 |
if (dtp->pos <= 0) |
| 3587 |
{ |
| 3588 |
generate_error (&dtp->common, LIBERROR_BAD_OPTION, |
| 3589 |
"POS=specifier must be positive"); |
| 3590 |
return; |
| 3591 |
} |
| 3592 |
|
| 3593 |
if (dtp->pos >= dtp->u.p.current_unit->maxrec) |
| 3594 |
{ |
| 3595 |
generate_error (&dtp->common, LIBERROR_BAD_OPTION, |
| 3596 |
"POS=specifier too large"); |
| 3597 |
return; |
| 3598 |
} |
| 3599 |
|
| 3600 |
dtp->rec = dtp->pos; |
| 3601 |
|
| 3602 |
if (dtp->u.p.mode == READING) |
| 3603 |
{ |
| 3604 |
/* Reset the endfile flag; if we hit EOF during reading |
| 3605 |
we'll set the flag and generate an error at that point |
| 3606 |
rather than worrying about it here. */ |
| 3607 |
dtp->u.p.current_unit->endfile = NO_ENDFILE; |
| 3608 |
} |
| 3609 |
|
| 3610 |
if (dtp->pos != dtp->u.p.current_unit->strm_pos) |
| 3611 |
{ |
| 3612 |
fbuf_reset (dtp->u.p.current_unit); |
| 3613 |
if (sseek (dtp->u.p.current_unit->s, dtp->pos - 1, |
| 3614 |
SEEK_SET) < 0) |
| 3615 |
{ |
| 3616 |
generate_error (&dtp->common, LIBERROR_OS, NULL); |
| 3617 |
return; |
| 3618 |
} |
| 3619 |
dtp->u.p.current_unit->strm_pos = dtp->pos; |
| 3620 |
} |
| 3621 |
} |
| 3622 |
else |
| 3623 |
{ |
| 3624 |
generate_error (&dtp->common, LIBERROR_BAD_OPTION, |
| 3625 |
"POS=specifier not allowed, " |
| 3626 |
"Try OPEN with ACCESS='stream'"); |
| 3627 |
return; |
| 3628 |
} |
| 3629 |
} |
| 3630 |
|
| 3631 |
|
| 3632 |
/* Sanity checks on the record number. */ |
| 3633 |
if ((cf & IOPARM_DT_HAS_REC) != 0) |
| 3634 |
{ |
| 3635 |
if (dtp->rec <= 0) |
| 3636 |
{ |
| 3637 |
generate_error (&dtp->common, LIBERROR_BAD_OPTION, |
| 3638 |
"Record number must be positive"); |
| 3639 |
return; |
| 3640 |
} |
| 3641 |
|
| 3642 |
if (dtp->rec >= dtp->u.p.current_unit->maxrec) |
| 3643 |
{ |
| 3644 |
generate_error (&dtp->common, LIBERROR_BAD_OPTION, |
| 3645 |
"Record number too large"); |
| 3646 |
return; |
| 3647 |
} |
| 3648 |
|
| 3649 |
/* Make sure format buffer is reset. */ |
| 3650 |
if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED) |
| 3651 |
fbuf_reset (dtp->u.p.current_unit); |
| 3652 |
|
| 3653 |
|
| 3654 |
/* Check whether the record exists to be read. Only |
| 3655 |
a partial record needs to exist. */ |
| 3656 |
|
| 3657 |
if (dtp->u.p.mode == READING && (dtp->rec - 1) |
| 3658 |
* dtp->u.p.current_unit->recl >= ssize (dtp->u.p.current_unit->s)) |
| 3659 |
{ |
| 3660 |
generate_error (&dtp->common, LIBERROR_BAD_OPTION, |
| 3661 |
"Non-existing record number"); |
| 3662 |
return; |
| 3663 |
} |
| 3664 |
|
| 3665 |
/* Position the file. */ |
| 3666 |
if (sseek (dtp->u.p.current_unit->s, (gfc_offset) (dtp->rec - 1) |
| 3667 |
* dtp->u.p.current_unit->recl, SEEK_SET) < 0) |
| 3668 |
{ |
| 3669 |
generate_error (&dtp->common, LIBERROR_OS, NULL); |
| 3670 |
return; |
| 3671 |
} |
| 3672 |
|
| 3673 |
if (dtp->u.p.current_unit->flags.access == ACCESS_STREAM) |
| 3674 |
{ |
| 3675 |
generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT, |
| 3676 |
"Record number not allowed for stream access " |
| 3677 |
"data transfer"); |
| 3678 |
return; |
| 3679 |
} |
| 3680 |
} |
| 3681 |
|
| 3682 |
/* Bugware for badly written mixed C-Fortran I/O. */ |
| 3683 |
if (!is_internal_unit (dtp)) |
| 3684 |
flush_if_preconnected(dtp->u.p.current_unit->s); |
| 3685 |
|
| 3686 |
dtp->u.p.current_unit->mode = dtp->u.p.mode; |
| 3687 |
|
| 3688 |
/* Set the maximum position reached from the previous I/O operation. This |
| 3689 |
could be greater than zero from a previous non-advancing write. */ |
| 3690 |
dtp->u.p.max_pos = dtp->u.p.current_unit->saved_pos; |
| 3691 |
|
| 3692 |
pre_position (dtp); |
| 3693 |
|
| 3694 |
/* Make sure that we don't do a read after a nonadvancing write. */ |
| 3695 |
|
| 3696 |
if (read_flag) |
| 3697 |
{ |
| 3698 |
if (dtp->u.p.current_unit->read_bad && !is_stream_io (dtp)) |
| 3699 |
{ |
| 3700 |
generate_error (&dtp->common, LIBERROR_BAD_OPTION, |
| 3701 |
"Cannot READ after a nonadvancing WRITE"); |
| 3702 |
return; |
| 3703 |
} |
| 3704 |
} |
| 3705 |
else |
| 3706 |
{ |
| 3707 |
if (dtp->u.p.advance_status == ADVANCE_YES && !dtp->u.p.seen_dollar) |
| 3708 |
dtp->u.p.current_unit->read_bad = 1; |
| 3709 |
} |
| 3710 |
|
| 3711 |
if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED) |
| 3712 |
{ |
| 3713 |
#ifdef HAVE_POSIX_2008_LOCALE |
| 3714 |
dtp->u.p.old_locale = uselocale (c_locale); |
| 3715 |
#else |
| 3716 |
__gthread_mutex_lock (&old_locale_lock); |
| 3717 |
if (!old_locale_ctr++) |
| 3718 |
{ |
| 3719 |
old_locale = setlocale (LC_NUMERIC, NULL); |
| 3720 |
setlocale (LC_NUMERIC, "C"); |
| 3721 |
} |
| 3722 |
__gthread_mutex_unlock (&old_locale_lock); |
| 3723 |
#endif |
| 3724 |
/* Start the data transfer if we are doing a formatted transfer. */ |
| 3725 |
if ((cf & (IOPARM_DT_LIST_FORMAT | IOPARM_DT_HAS_NAMELIST_NAME)) == 0 |
| 3726 |
&& dtp->u.p.ionml == NULL) |
| 3727 |
formatted_transfer (dtp, 0, NULL, 0, 0, 1); |
| 3728 |
} |
| 3729 |
} |
| 3730 |
|
| 3731 |
|
| 3732 |
/* Initialize an array_loop_spec given the array descriptor. The function |
| 3733 |
returns the index of the last element of the array, and also returns |
| 3734 |
starting record, where the first I/O goes to (necessary in case of |
| 3735 |
negative strides). */ |
| 3736 |
|
| 3737 |
gfc_offset |
| 3738 |
init_loop_spec (gfc_array_char *desc, array_loop_spec *ls, |
| 3739 |
gfc_offset *start_record) |
| 3740 |
{ |
| 3741 |
int rank = GFC_DESCRIPTOR_RANK(desc); |
| 3742 |
int i; |
| 3743 |
gfc_offset index; |
| 3744 |
int empty; |
| 3745 |
|
| 3746 |
empty = 0; |
| 3747 |
index = 1; |
| 3748 |
*start_record = 0; |
| 3749 |
|
| 3750 |
for (i=0; i<rank; i++) |
| 3751 |
{ |
| 3752 |
ls[i].idx = GFC_DESCRIPTOR_LBOUND(desc,i); |
| 3753 |
ls[i].start = GFC_DESCRIPTOR_LBOUND(desc,i); |
| 3754 |
ls[i].end = GFC_DESCRIPTOR_UBOUND(desc,i); |
| 3755 |
ls[i].step = GFC_DESCRIPTOR_STRIDE(desc,i); |
| 3756 |
empty = empty || (GFC_DESCRIPTOR_UBOUND(desc,i) |
| 3757 |
< GFC_DESCRIPTOR_LBOUND(desc,i)); |
| 3758 |
|
| 3759 |
if (GFC_DESCRIPTOR_STRIDE(desc,i) > 0) |
| 3760 |
{ |
| 3761 |
index += (GFC_DESCRIPTOR_EXTENT(desc,i) - 1) |
| 3762 |
* GFC_DESCRIPTOR_STRIDE(desc,i); |
| 3763 |
} |
| 3764 |
else |
| 3765 |
{ |
| 3766 |
index -= (GFC_DESCRIPTOR_EXTENT(desc,i) - 1) |
| 3767 |
* GFC_DESCRIPTOR_STRIDE(desc,i); |
| 3768 |
*start_record -= (GFC_DESCRIPTOR_EXTENT(desc,i) - 1) |
| 3769 |
* GFC_DESCRIPTOR_STRIDE(desc,i); |
| 3770 |
} |
| 3771 |
} |
| 3772 |
|
| 3773 |
if (empty) |
| 3774 |
return 0; |
| 3775 |
else |
| 3776 |
return index; |
| 3777 |
} |
| 3778 |
|
| 3779 |
/* Determine the index to the next record in an internal unit array by |
| 3780 |
by incrementing through the array_loop_spec. */ |
| 3781 |
|
| 3782 |
gfc_offset |
| 3783 |
next_array_record (st_parameter_dt *dtp, array_loop_spec *ls, int *finished) |
| 3784 |
{ |
| 3785 |
int i, carry; |
| 3786 |
gfc_offset index; |
| 3787 |
|
| 3788 |
carry = 1; |
| 3789 |
index = 0; |
| 3790 |
|
| 3791 |
for (i = 0; i < dtp->u.p.current_unit->rank; i++) |
| 3792 |
{ |
| 3793 |
if (carry) |
| 3794 |
{ |
| 3795 |
ls[i].idx++; |
| 3796 |
if (ls[i].idx > ls[i].end) |
| 3797 |
{ |
| 3798 |
ls[i].idx = ls[i].start; |
| 3799 |
carry = 1; |
| 3800 |
} |
| 3801 |
else |
| 3802 |
carry = 0; |
| 3803 |
} |
| 3804 |
index = index + (ls[i].idx - ls[i].start) * ls[i].step; |
| 3805 |
} |
| 3806 |
|
| 3807 |
*finished = carry; |
| 3808 |
|
| 3809 |
return index; |
| 3810 |
} |
| 3811 |
|
| 3812 |
|
| 3813 |
|
| 3814 |
/* Skip to the end of the current record, taking care of an optional |
| 3815 |
record marker of size bytes. If the file is not seekable, we |
| 3816 |
read chunks of size MAX_READ until we get to the right |
| 3817 |
position. */ |
| 3818 |
|
| 3819 |
static void |
| 3820 |
skip_record (st_parameter_dt *dtp, gfc_offset bytes) |
| 3821 |
{ |
| 3822 |
ssize_t rlength, readb; |
| 3823 |
#define MAX_READ 4096 |
| 3824 |
char p[MAX_READ]; |
| 3825 |
|
| 3826 |
dtp->u.p.current_unit->bytes_left_subrecord += bytes; |
| 3827 |
if (dtp->u.p.current_unit->bytes_left_subrecord == 0) |
| 3828 |
return; |
| 3829 |
|
| 3830 |
/* Direct access files do not generate END conditions, |
| 3831 |
only I/O errors. */ |
| 3832 |
if (sseek (dtp->u.p.current_unit->s, |
| 3833 |
dtp->u.p.current_unit->bytes_left_subrecord, SEEK_CUR) < 0) |
| 3834 |
{ |
| 3835 |
/* Seeking failed, fall back to seeking by reading data. */ |
| 3836 |
while (dtp->u.p.current_unit->bytes_left_subrecord > 0) |
| 3837 |
{ |
| 3838 |
rlength = |
| 3839 |
(MAX_READ < dtp->u.p.current_unit->bytes_left_subrecord) ? |
| 3840 |
MAX_READ : dtp->u.p.current_unit->bytes_left_subrecord; |
| 3841 |
|
| 3842 |
readb = sread (dtp->u.p.current_unit->s, p, rlength); |
| 3843 |
if (readb < 0) |
| 3844 |
{ |
| 3845 |
generate_error (&dtp->common, LIBERROR_OS, NULL); |
| 3846 |
return; |
| 3847 |
} |
| 3848 |
|
| 3849 |
dtp->u.p.current_unit->bytes_left_subrecord -= readb; |
| 3850 |
} |
| 3851 |
return; |
| 3852 |
} |
| 3853 |
dtp->u.p.current_unit->bytes_left_subrecord = 0; |
| 3854 |
} |
| 3855 |
|
| 3856 |
|
| 3857 |
/* Advance to the next record reading unformatted files, taking |
| 3858 |
care of subrecords. If complete_record is nonzero, we loop |
| 3859 |
until all subrecords are cleared. */ |
| 3860 |
|
| 3861 |
static void |
| 3862 |
next_record_r_unf (st_parameter_dt *dtp, int complete_record) |
| 3863 |
{ |
| 3864 |
size_t bytes; |
| 3865 |
|
| 3866 |
bytes = compile_options.record_marker == 0 ? |
| 3867 |
sizeof (GFC_INTEGER_4) : compile_options.record_marker; |
| 3868 |
|
| 3869 |
while(1) |
| 3870 |
{ |
| 3871 |
|
| 3872 |
/* Skip over tail */ |
| 3873 |
|
| 3874 |
skip_record (dtp, bytes); |
| 3875 |
|
| 3876 |
if ( ! (complete_record && dtp->u.p.current_unit->continued)) |
| 3877 |
return; |
| 3878 |
|
| 3879 |
us_read (dtp, 1); |
| 3880 |
} |
| 3881 |
} |
| 3882 |
|
| 3883 |
|
| 3884 |
static gfc_offset |
| 3885 |
min_off (gfc_offset a, gfc_offset b) |
| 3886 |
{ |
| 3887 |
return (a < b ? a : b); |
| 3888 |
} |
| 3889 |
|
| 3890 |
|
| 3891 |
/* Space to the next record for read mode. */ |
| 3892 |
|
| 3893 |
static void |
| 3894 |
next_record_r (st_parameter_dt *dtp, int done) |
| 3895 |
{ |
| 3896 |
gfc_offset record; |
| 3897 |
char p; |
| 3898 |
int cc; |
| 3899 |
|
| 3900 |
switch (current_mode (dtp)) |
| 3901 |
{ |
| 3902 |
/* No records in unformatted STREAM I/O. */ |
| 3903 |
case UNFORMATTED_STREAM: |
| 3904 |
return; |
| 3905 |
|
| 3906 |
case UNFORMATTED_SEQUENTIAL: |
| 3907 |
next_record_r_unf (dtp, 1); |
| 3908 |
dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl; |
| 3909 |
break; |
| 3910 |
|
| 3911 |
case FORMATTED_DIRECT: |
| 3912 |
case UNFORMATTED_DIRECT: |
| 3913 |
skip_record (dtp, dtp->u.p.current_unit->bytes_left); |
| 3914 |
break; |
| 3915 |
|
| 3916 |
case FORMATTED_STREAM: |
| 3917 |
case FORMATTED_SEQUENTIAL: |
| 3918 |
/* read_sf has already terminated input because of an '\n', or |
| 3919 |
we have hit EOF. */ |
| 3920 |
if (dtp->u.p.sf_seen_eor) |
| 3921 |
{ |
| 3922 |
dtp->u.p.sf_seen_eor = 0; |
| 3923 |
break; |
| 3924 |
} |
| 3925 |
|
| 3926 |
if (is_internal_unit (dtp)) |
| 3927 |
{ |
| 3928 |
if (is_array_io (dtp)) |
| 3929 |
{ |
| 3930 |
int finished; |
| 3931 |
|
| 3932 |
record = next_array_record (dtp, dtp->u.p.current_unit->ls, |
| 3933 |
&finished); |
| 3934 |
if (!done && finished) |
| 3935 |
hit_eof (dtp); |
| 3936 |
|
| 3937 |
/* Now seek to this record. */ |
| 3938 |
record = record * dtp->u.p.current_unit->recl; |
| 3939 |
if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0) |
| 3940 |
{ |
| 3941 |
generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL); |
| 3942 |
break; |
| 3943 |
} |
| 3944 |
dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl; |
| 3945 |
} |
| 3946 |
else |
| 3947 |
{ |
| 3948 |
gfc_offset bytes_left = dtp->u.p.current_unit->bytes_left; |
| 3949 |
bytes_left = min_off (bytes_left, |
| 3950 |
ssize (dtp->u.p.current_unit->s) |
| 3951 |
- stell (dtp->u.p.current_unit->s)); |
| 3952 |
if (sseek (dtp->u.p.current_unit->s, |
| 3953 |
bytes_left, SEEK_CUR) < 0) |
| 3954 |
{ |
| 3955 |
generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL); |
| 3956 |
break; |
| 3957 |
} |
| 3958 |
dtp->u.p.current_unit->bytes_left |
| 3959 |
= dtp->u.p.current_unit->recl; |
| 3960 |
} |
| 3961 |
break; |
| 3962 |
} |
| 3963 |
else if (dtp->u.p.current_unit->flags.cc != CC_NONE) |
| 3964 |
{ |
| 3965 |
do |
| 3966 |
{ |
| 3967 |
errno = 0; |
| 3968 |
cc = fbuf_getc (dtp->u.p.current_unit); |
| 3969 |
if (cc == EOF) |
| 3970 |
{ |
| 3971 |
if (errno != 0) |
| 3972 |
generate_error (&dtp->common, LIBERROR_OS, NULL); |
| 3973 |
else |
| 3974 |
{ |
| 3975 |
if (is_stream_io (dtp) |
| 3976 |
|| dtp->u.p.current_unit->pad_status == PAD_NO |
| 3977 |
|| dtp->u.p.current_unit->bytes_left |
| 3978 |
== dtp->u.p.current_unit->recl) |
| 3979 |
hit_eof (dtp); |
| 3980 |
} |
| 3981 |
break; |
| 3982 |
} |
| 3983 |
|
| 3984 |
if (is_stream_io (dtp)) |
| 3985 |
dtp->u.p.current_unit->strm_pos++; |
| 3986 |
|
| 3987 |
p = (char) cc; |
| 3988 |
} |
| 3989 |
while (p != '\n'); |
| 3990 |
} |
| 3991 |
break; |
| 3992 |
case FORMATTED_UNSPECIFIED: |
| 3993 |
gcc_unreachable (); |
| 3994 |
} |
| 3995 |
} |
| 3996 |
|
| 3997 |
|
| 3998 |
/* Small utility function to write a record marker, taking care of |
| 3999 |
byte swapping and of choosing the correct size. */ |
| 4000 |
|
| 4001 |
static int |
| 4002 |
write_us_marker (st_parameter_dt *dtp, const gfc_offset buf) |
| 4003 |
{ |
| 4004 |
size_t len; |
| 4005 |
GFC_INTEGER_4 buf4; |
| 4006 |
GFC_INTEGER_8 buf8; |
| 4007 |
|
| 4008 |
if (compile_options.record_marker == 0) |
| 4009 |
len = sizeof (GFC_INTEGER_4); |
| 4010 |
else |
| 4011 |
len = compile_options.record_marker; |
| 4012 |
|
| 4013 |
int convert = dtp->u.p.current_unit->flags.convert; |
| 4014 |
#ifdef HAVE_GFC_REAL_17 |
| 4015 |
convert &= ~(GFC_CONVERT_R16_IEEE | GFC_CONVERT_R16_IBM); |
| 4016 |
#endif |
| 4017 |
/* Only GFC_CONVERT_NATIVE and GFC_CONVERT_SWAP are valid here. */ |
| 4018 |
if (likely (convert == GFC_CONVERT_NATIVE)) |
| 4019 |
{ |
| 4020 |
switch (len) |
| 4021 |
{ |
| 4022 |
case sizeof (GFC_INTEGER_4): |
| 4023 |
buf4 = buf; |
| 4024 |
return swrite (dtp->u.p.current_unit->s, &buf4, len); |
| 4025 |
break; |
| 4026 |
|
| 4027 |
case sizeof (GFC_INTEGER_8): |
| 4028 |
buf8 = buf; |
| 4029 |
return swrite (dtp->u.p.current_unit->s, &buf8, len); |
| 4030 |
break; |
| 4031 |
|
| 4032 |
default: |
| 4033 |
runtime_error ("Illegal value for record marker"); |
| 4034 |
break; |
| 4035 |
} |
| 4036 |
} |
| 4037 |
else |
| 4038 |
{ |
| 4039 |
uint32_t u32; |
| 4040 |
uint64_t u64; |
| 4041 |
switch (len) |
| 4042 |
{ |
| 4043 |
case sizeof (GFC_INTEGER_4): |
| 4044 |
buf4 = buf; |
| 4045 |
memcpy (&u32, &buf4, sizeof (u32)); |
| 4046 |
u32 = __builtin_bswap32 (u32); |
| 4047 |
return swrite (dtp->u.p.current_unit->s, &u32, len); |
| 4048 |
break; |
| 4049 |
|
| 4050 |
case sizeof (GFC_INTEGER_8): |
| 4051 |
buf8 = buf; |
| 4052 |
memcpy (&u64, &buf8, sizeof (u64)); |
| 4053 |
u64 = __builtin_bswap64 (u64); |
| 4054 |
return swrite (dtp->u.p.current_unit->s, &u64, len); |
| 4055 |
break; |
| 4056 |
|
| 4057 |
default: |
| 4058 |
runtime_error ("Illegal value for record marker"); |
| 4059 |
break; |
| 4060 |
} |
| 4061 |
} |
| 4062 |
|
| 4063 |
} |
| 4064 |
|
| 4065 |
/* Position to the next (sub)record in write mode for |
| 4066 |
unformatted sequential files. */ |
| 4067 |
|
| 4068 |
static void |
| 4069 |
next_record_w_unf (st_parameter_dt *dtp, int next_subrecord) |
| 4070 |
{ |
| 4071 |
gfc_offset m, m_write, record_marker; |
| 4072 |
|
| 4073 |
/* Bytes written. */ |
| 4074 |
m = dtp->u.p.current_unit->recl_subrecord |
| 4075 |
- dtp->u.p.current_unit->bytes_left_subrecord; |
| 4076 |
|
| 4077 |
if (compile_options.record_marker == 0) |
| 4078 |
record_marker = sizeof (GFC_INTEGER_4); |
| 4079 |
else |
| 4080 |
record_marker = compile_options.record_marker; |
| 4081 |
|
| 4082 |
/* Seek to the head and overwrite the bogus length with the real |
| 4083 |
length. */ |
| 4084 |
|
| 4085 |
if (unlikely (sseek (dtp->u.p.current_unit->s, - m - record_marker, |
| 4086 |
SEEK_CUR) < 0)) |
| 4087 |
goto io_error; |
| 4088 |
|
| 4089 |
if (next_subrecord) |
| 4090 |
m_write = -m; |
| 4091 |
else |
| 4092 |
m_write = m; |
| 4093 |
|
| 4094 |
if (unlikely (write_us_marker (dtp, m_write) < 0)) |
| 4095 |
goto io_error; |
| 4096 |
|
| 4097 |
/* Seek past the end of the current record. */ |
| 4098 |
|
| 4099 |
if (unlikely (sseek (dtp->u.p.current_unit->s, m, SEEK_CUR) < 0)) |
| 4100 |
goto io_error; |
| 4101 |
|
| 4102 |
/* Write the length tail. If we finish a record containing |
| 4103 |
subrecords, we write out the negative length. */ |
| 4104 |
|
| 4105 |
if (dtp->u.p.current_unit->continued) |
| 4106 |
m_write = -m; |
| 4107 |
else |
| 4108 |
m_write = m; |
| 4109 |
|
| 4110 |
if (unlikely (write_us_marker (dtp, m_write) < 0)) |
| 4111 |
goto io_error; |
| 4112 |
|
| 4113 |
return; |
| 4114 |
|
| 4115 |
io_error: |
| 4116 |
generate_error (&dtp->common, LIBERROR_OS, NULL); |
| 4117 |
return; |
| 4118 |
|
| 4119 |
} |
| 4120 |
|
| 4121 |
|
| 4122 |
/* Utility function like memset() but operating on streams. Return |
| 4123 |
value is same as for POSIX write(). */ |
| 4124 |
|
| 4125 |
static gfc_offset |
| 4126 |
sset (stream *s, int c, gfc_offset nbyte) |
| 4127 |
{ |
| 4128 |
#define WRITE_CHUNK 256 |
| 4129 |
char p[WRITE_CHUNK]; |
| 4130 |
gfc_offset bytes_left; |
| 4131 |
ssize_t trans; |
| 4132 |
|
| 4133 |
if (nbyte < WRITE_CHUNK) |
| 4134 |
memset (p, c, nbyte); |
| 4135 |
else |
| 4136 |
memset (p, c, WRITE_CHUNK); |
| 4137 |
|
| 4138 |
bytes_left = nbyte; |
| 4139 |
while (bytes_left > 0) |
| 4140 |
{ |
| 4141 |
trans = (bytes_left < WRITE_CHUNK) ? bytes_left : WRITE_CHUNK; |
| 4142 |
trans = swrite (s, p, trans); |
| 4143 |
if (trans <= 0) |
| 4144 |
return trans; |
| 4145 |
bytes_left -= trans; |
| 4146 |
} |
| 4147 |
|
| 4148 |
return nbyte - bytes_left; |
| 4149 |
} |
| 4150 |
|
| 4151 |
|
| 4152 |
/* Finish up a record according to the legacy carriagecontrol type, based |
| 4153 |
on the first character in the record. */ |
| 4154 |
|
| 4155 |
static void |
| 4156 |
next_record_cc (st_parameter_dt *dtp) |
| 4157 |
{ |
| 4158 |
/* Only valid with CARRIAGECONTROL=FORTRAN. */ |
| 4159 |
if (dtp->u.p.current_unit->flags.cc != CC_FORTRAN) |
| 4160 |
return; |
| 4161 |
|
| 4162 |
fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END); |
| 4163 |
if (dtp->u.p.cc.len > 0) |
| 4164 |
{ |
| 4165 |
char *p = fbuf_alloc (dtp->u.p.current_unit, dtp->u.p.cc.len); |
| 4166 |
if (!p) |
| 4167 |
generate_error (&dtp->common, LIBERROR_OS, NULL); |
| 4168 |
|
| 4169 |
/* Output CR for the first character with default CC setting. */ |
| 4170 |
*(p++) = dtp->u.p.cc.u.end; |
| 4171 |
if (dtp->u.p.cc.len > 1) |
| 4172 |
*p = dtp->u.p.cc.u.end; |
| 4173 |
} |
| 4174 |
} |
| 4175 |
|
| 4176 |
/* Position to the next record in write mode. */ |
| 4177 |
|
| 4178 |
static void |
| 4179 |
next_record_w (st_parameter_dt *dtp, int done) |
| 4180 |
{ |
| 4181 |
gfc_offset max_pos_off; |
| 4182 |
|
| 4183 |
/* Zero counters for X- and T-editing. */ |
| 4184 |
max_pos_off = dtp->u.p.max_pos; |
| 4185 |
dtp->u.p.max_pos = dtp->u.p.skips = dtp->u.p.pending_spaces = 0; |
| 4186 |
|
| 4187 |
switch (current_mode (dtp)) |
| 4188 |
{ |
| 4189 |
/* No records in unformatted STREAM I/O. */ |
| 4190 |
case UNFORMATTED_STREAM: |
| 4191 |
return; |
| 4192 |
|
| 4193 |
case FORMATTED_DIRECT: |
| 4194 |
if (dtp->u.p.current_unit->bytes_left == 0) |
| 4195 |
break; |
| 4196 |
|
| 4197 |
fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END); |
| 4198 |
fbuf_flush (dtp->u.p.current_unit, WRITING); |
| 4199 |
if (sset (dtp->u.p.current_unit->s, ' ', |
| 4200 |
dtp->u.p.current_unit->bytes_left) |
| 4201 |
!= dtp->u.p.current_unit->bytes_left) |
| 4202 |
goto io_error; |
| 4203 |
|
| 4204 |
break; |
| 4205 |
|
| 4206 |
case UNFORMATTED_DIRECT: |
| 4207 |
if (dtp->u.p.current_unit->bytes_left > 0) |
| 4208 |
{ |
| 4209 |
gfc_offset length = dtp->u.p.current_unit->bytes_left; |
| 4210 |
if (sset (dtp->u.p.current_unit->s, 0, length) != length) |
| 4211 |
goto io_error; |
| 4212 |
} |
| 4213 |
break; |
| 4214 |
|
| 4215 |
case UNFORMATTED_SEQUENTIAL: |
| 4216 |
next_record_w_unf (dtp, 0); |
| 4217 |
dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl; |
| 4218 |
break; |
| 4219 |
|
| 4220 |
case FORMATTED_STREAM: |
| 4221 |
case FORMATTED_SEQUENTIAL: |
| 4222 |
|
| 4223 |
if (is_internal_unit (dtp)) |
| 4224 |
{ |
| 4225 |
char *p; |
| 4226 |
/* Internal unit, so must fit in memory. */ |
| 4227 |
size_t length, m; |
| 4228 |
size_t max_pos = max_pos_off; |
| 4229 |
if (is_array_io (dtp)) |
| 4230 |
{ |
| 4231 |
int finished; |
| 4232 |
|
| 4233 |
length = dtp->u.p.current_unit->bytes_left; |
| 4234 |
|
| 4235 |
/* If the farthest position reached is greater than current |
| 4236 |
position, adjust the position and set length to pad out |
| 4237 |
whats left. Otherwise just pad whats left. |
| 4238 |
(for character array unit) */ |
| 4239 |
m = dtp->u.p.current_unit->recl |
| 4240 |
- dtp->u.p.current_unit->bytes_left; |
| 4241 |
if (max_pos > m) |
| 4242 |
{ |
| 4243 |
length = (max_pos - m); |
| 4244 |
if (sseek (dtp->u.p.current_unit->s, |
| 4245 |
length, SEEK_CUR) < 0) |
| 4246 |
{ |
| 4247 |
generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL); |
| 4248 |
return; |
| 4249 |
} |
| 4250 |
length = ((size_t) dtp->u.p.current_unit->recl - max_pos); |
| 4251 |
} |
| 4252 |
|
| 4253 |
p = write_block (dtp, length); |
| 4254 |
if (p == NULL) |
| 4255 |
return; |
| 4256 |
|
| 4257 |
if (unlikely (is_char4_unit (dtp))) |
| 4258 |
{ |
| 4259 |
gfc_char4_t *p4 = (gfc_char4_t *) p; |
| 4260 |
memset4 (p4, ' ', length); |
| 4261 |
} |
| 4262 |
else |
| 4263 |
memset (p, ' ', length); |
| 4264 |
|
| 4265 |
/* Now that the current record has been padded out, |
| 4266 |
determine where the next record in the array is. |
| 4267 |
Note that this can return a negative value, so it |
| 4268 |
needs to be assigned to a signed value. */ |
| 4269 |
gfc_offset record = next_array_record |
| 4270 |
(dtp, dtp->u.p.current_unit->ls, &finished); |
| 4271 |
if (finished) |
| 4272 |
dtp->u.p.current_unit->endfile = AT_ENDFILE; |
| 4273 |
|
| 4274 |
/* Now seek to this record */ |
| 4275 |
record = record * dtp->u.p.current_unit->recl; |
| 4276 |
|
| 4277 |
if (sseek (dtp->u.p.current_unit->s, record, SEEK_SET) < 0) |
| 4278 |
{ |
| 4279 |
generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL); |
| 4280 |
return; |
| 4281 |
} |
| 4282 |
|
| 4283 |
dtp->u.p.current_unit->bytes_left = dtp->u.p.current_unit->recl; |
| 4284 |
} |
| 4285 |
else |
| 4286 |
{ |
| 4287 |
length = 1; |
| 4288 |
|
| 4289 |
/* If this is the last call to next_record move to the farthest |
| 4290 |
position reached and set length to pad out the remainder |
| 4291 |
of the record. (for character scaler unit) */ |
| 4292 |
if (done) |
| 4293 |
{ |
| 4294 |
m = dtp->u.p.current_unit->recl |
| 4295 |
- dtp->u.p.current_unit->bytes_left; |
| 4296 |
if (max_pos > m) |
| 4297 |
{ |
| 4298 |
length = max_pos - m; |
| 4299 |
if (sseek (dtp->u.p.current_unit->s, |
| 4300 |
length, SEEK_CUR) < 0) |
| 4301 |
{ |
| 4302 |
generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL); |
| 4303 |
return; |
| 4304 |
} |
| 4305 |
length = (size_t) dtp->u.p.current_unit->recl |
| 4306 |
- max_pos; |
| 4307 |
} |
| 4308 |
else |
| 4309 |
length = dtp->u.p.current_unit->bytes_left; |
| 4310 |
} |
| 4311 |
if (length > 0) |
| 4312 |
{ |
| 4313 |
p = write_block (dtp, length); |
| 4314 |
if (p == NULL) |
| 4315 |
return; |
| 4316 |
|
| 4317 |
if (unlikely (is_char4_unit (dtp))) |
| 4318 |
{ |
| 4319 |
gfc_char4_t *p4 = (gfc_char4_t *) p; |
| 4320 |
memset4 (p4, (gfc_char4_t) ' ', length); |
| 4321 |
} |
| 4322 |
else |
| 4323 |
memset (p, ' ', length); |
| 4324 |
} |
| 4325 |
} |
| 4326 |
} |
| 4327 |
else if (dtp->u.p.seen_dollar == 1) |
| 4328 |
break; |
| 4329 |
/* Handle legacy CARRIAGECONTROL line endings. */ |
| 4330 |
else if (dtp->u.p.current_unit->flags.cc == CC_FORTRAN) |
| 4331 |
next_record_cc (dtp); |
| 4332 |
else |
| 4333 |
{ |
| 4334 |
/* Skip newlines for CC=CC_NONE. */ |
| 4335 |
const int len = (dtp->u.p.current_unit->flags.cc == CC_NONE) |
| 4336 |
? 0 |
| 4337 |
#ifdef HAVE_CRLF |
| 4338 |
: 2; |
| 4339 |
#else |
| 4340 |
: 1; |
| 4341 |
#endif |
| 4342 |
fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END); |
| 4343 |
if (dtp->u.p.current_unit->flags.cc != CC_NONE) |
| 4344 |
{ |
| 4345 |
char *p = fbuf_alloc (dtp->u.p.current_unit, len); |
| 4346 |
if (!p) |
| 4347 |
goto io_error; |
| 4348 |
#ifdef HAVE_CRLF |
| 4349 |
*(p++) = '\r'; |
| 4350 |
#endif |
| 4351 |
*p = '\n'; |
| 4352 |
} |
| 4353 |
if (is_stream_io (dtp)) |
| 4354 |
{ |
| 4355 |
dtp->u.p.current_unit->strm_pos += len; |
| 4356 |
if (dtp->u.p.current_unit->strm_pos |
| 4357 |
< ssize (dtp->u.p.current_unit->s)) |
| 4358 |
unit_truncate (dtp->u.p.current_unit, |
| 4359 |
dtp->u.p.current_unit->strm_pos - 1, |
| 4360 |
&dtp->common); |
| 4361 |
} |
| 4362 |
} |
| 4363 |
|
| 4364 |
break; |
| 4365 |
case FORMATTED_UNSPECIFIED: |
| 4366 |
gcc_unreachable (); |
| 4367 |
|
| 4368 |
io_error: |
| 4369 |
generate_error (&dtp->common, LIBERROR_OS, NULL); |
| 4370 |
break; |
| 4371 |
} |
| 4372 |
} |
| 4373 |
|
| 4374 |
/* Position to the next record, which means moving to the end of the |
| 4375 |
current record. This can happen under several different |
| 4376 |
conditions. If the done flag is not set, we get ready to process |
| 4377 |
the next record. */ |
| 4378 |
|
| 4379 |
void |
| 4380 |
next_record (st_parameter_dt *dtp, int done) |
| 4381 |
{ |
| 4382 |
gfc_offset fp; /* File position. */ |
| 4383 |
|
| 4384 |
dtp->u.p.current_unit->read_bad = 0; |
| 4385 |
|
| 4386 |
if (dtp->u.p.mode == READING) |
| 4387 |
next_record_r (dtp, done); |
| 4388 |
else |
| 4389 |
next_record_w (dtp, done); |
| 4390 |
|
| 4391 |
fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode); |
| 4392 |
|
| 4393 |
if (!is_stream_io (dtp)) |
| 4394 |
{ |
| 4395 |
/* Since we have changed the position, set it to unspecified so |
| 4396 |
that INQUIRE(POSITION=) knows it needs to look into it. */ |
| 4397 |
if (done) |
| 4398 |
dtp->u.p.current_unit->flags.position = POSITION_UNSPECIFIED; |
| 4399 |
|
| 4400 |
dtp->u.p.current_unit->current_record = 0; |
| 4401 |
if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT) |
| 4402 |
{ |
| 4403 |
fp = stell (dtp->u.p.current_unit->s); |
| 4404 |
/* Calculate next record, rounding up partial records. */ |
| 4405 |
dtp->u.p.current_unit->last_record = |
| 4406 |
(fp + dtp->u.p.current_unit->recl) / |
| 4407 |
dtp->u.p.current_unit->recl - 1; |
| 4408 |
} |
| 4409 |
else |
| 4410 |
dtp->u.p.current_unit->last_record++; |
| 4411 |
} |
| 4412 |
|
| 4413 |
if (!done) |
| 4414 |
pre_position (dtp); |
| 4415 |
|
| 4416 |
smarkeor (dtp->u.p.current_unit->s); |
| 4417 |
} |
| 4418 |
|
| 4419 |
|
| 4420 |
/* Finalize the current data transfer. For a nonadvancing transfer, |
| 4421 |
this means advancing to the next record. For internal units close the |
| 4422 |
stream associated with the unit. */ |
| 4423 |
|
| 4424 |
static void |
| 4425 |
finalize_transfer (st_parameter_dt *dtp) |
| 4426 |
{ |
| 4427 |
GFC_INTEGER_4 cf = dtp->common.flags; |
| 4428 |
|
| 4429 |
if ((dtp->u.p.ionml != NULL) |
| 4430 |
&& (cf & IOPARM_DT_HAS_NAMELIST_NAME) != 0) |
| 4431 |
{ |
| 4432 |
if (dtp->u.p.current_unit->flags.form == FORM_UNFORMATTED) |
| 4433 |
{ |
| 4434 |
generate_error (&dtp->common, LIBERROR_OPTION_CONFLICT, |
| 4435 |
"Namelist formatting for unit connected " |
| 4436 |
"with FORM='UNFORMATTED'"); |
| 4437 |
return; |
| 4438 |
} |
| 4439 |
|
| 4440 |
dtp->u.p.namelist_mode = 1; |
| 4441 |
if ((cf & IOPARM_DT_NAMELIST_READ_MODE) != 0) |
| 4442 |
namelist_read (dtp); |
| 4443 |
else |
| 4444 |
namelist_write (dtp); |
| 4445 |
} |
| 4446 |
|
| 4447 |
if ((dtp->common.flags & IOPARM_DT_HAS_SIZE) != 0) |
| 4448 |
*dtp->size = dtp->u.p.current_unit->size_used; |
| 4449 |
|
| 4450 |
if (dtp->u.p.eor_condition) |
| 4451 |
{ |
| 4452 |
generate_error (&dtp->common, LIBERROR_EOR, NULL); |
| 4453 |
goto done; |
| 4454 |
} |
| 4455 |
|
| 4456 |
if (dtp->u.p.current_unit && (dtp->u.p.current_unit->child_dtio > 0)) |
| 4457 |
{ |
| 4458 |
if (cf & IOPARM_DT_HAS_FORMAT) |
| 4459 |
{ |
| 4460 |
free (dtp->u.p.fmt); |
| 4461 |
free (dtp->format); |
| 4462 |
} |
| 4463 |
return; |
| 4464 |
} |
| 4465 |
|
| 4466 |
if ((dtp->common.flags & IOPARM_LIBRETURN_MASK) != IOPARM_LIBRETURN_OK) |
| 4467 |
{ |
| 4468 |
if (dtp->u.p.current_unit && current_mode (dtp) == UNFORMATTED_SEQUENTIAL) |
| 4469 |
dtp->u.p.current_unit->current_record = 0; |
| 4470 |
goto done; |
| 4471 |
} |
| 4472 |
|
| 4473 |
dtp->u.p.transfer = NULL; |
| 4474 |
if (dtp->u.p.current_unit == NULL) |
| 4475 |
goto done; |
| 4476 |
|
| 4477 |
if ((cf & IOPARM_DT_LIST_FORMAT) != 0 && dtp->u.p.mode == READING) |
| 4478 |
{ |
| 4479 |
finish_list_read (dtp); |
| 4480 |
goto done; |
| 4481 |
} |
| 4482 |
|
| 4483 |
if (dtp->u.p.mode == WRITING) |
| 4484 |
dtp->u.p.current_unit->previous_nonadvancing_write |
| 4485 |
= dtp->u.p.advance_status == ADVANCE_NO; |
| 4486 |
|
| 4487 |
if (is_stream_io (dtp)) |
| 4488 |
{ |
| 4489 |
if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED |
| 4490 |
&& dtp->u.p.advance_status != ADVANCE_NO) |
| 4491 |
next_record (dtp, 1); |
| 4492 |
|
| 4493 |
goto done; |
| 4494 |
} |
| 4495 |
|
| 4496 |
dtp->u.p.current_unit->current_record = 0; |
| 4497 |
|
| 4498 |
if (!is_internal_unit (dtp) && dtp->u.p.seen_dollar) |
| 4499 |
{ |
| 4500 |
fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode); |
| 4501 |
dtp->u.p.seen_dollar = 0; |
| 4502 |
goto done; |
| 4503 |
} |
| 4504 |
|
| 4505 |
/* For non-advancing I/O, save the current maximum position for use in the |
| 4506 |
next I/O operation if needed. */ |
| 4507 |
if (dtp->u.p.advance_status == ADVANCE_NO) |
| 4508 |
{ |
| 4509 |
if (dtp->u.p.skips > 0) |
| 4510 |
{ |
| 4511 |
int tmp; |
| 4512 |
write_x (dtp, dtp->u.p.skips, dtp->u.p.pending_spaces); |
| 4513 |
tmp = (int)(dtp->u.p.current_unit->recl |
| 4514 |
- dtp->u.p.current_unit->bytes_left); |
| 4515 |
dtp->u.p.max_pos = |
| 4516 |
dtp->u.p.max_pos > tmp ? dtp->u.p.max_pos : tmp; |
| 4517 |
dtp->u.p.skips = 0; |
| 4518 |
} |
| 4519 |
int bytes_written = (int) (dtp->u.p.current_unit->recl |
| 4520 |
- dtp->u.p.current_unit->bytes_left); |
| 4521 |
dtp->u.p.current_unit->saved_pos = |
| 4522 |
dtp->u.p.max_pos > 0 ? dtp->u.p.max_pos - bytes_written : 0; |
| 4523 |
fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode); |
| 4524 |
goto done; |
| 4525 |
} |
| 4526 |
else if (dtp->u.p.current_unit->flags.form == FORM_FORMATTED |
| 4527 |
&& dtp->u.p.mode == WRITING && !is_internal_unit (dtp)) |
| 4528 |
fbuf_seek (dtp->u.p.current_unit, 0, SEEK_END); |
| 4529 |
|
| 4530 |
dtp->u.p.current_unit->saved_pos = 0; |
| 4531 |
dtp->u.p.current_unit->last_char = EOF - 1; |
| 4532 |
next_record (dtp, 1); |
| 4533 |
|
| 4534 |
done: |
| 4535 |
|
| 4536 |
if (dtp->u.p.unit_is_internal) |
| 4537 |
{ |
| 4538 |
/* The unit structure may be reused later so clear the |
| 4539 |
internal unit kind. */ |
| 4540 |
dtp->u.p.current_unit->internal_unit_kind = 0; |
| 4541 |
|
| 4542 |
fbuf_destroy (dtp->u.p.current_unit); |
| 4543 |
if (dtp->u.p.current_unit |
| 4544 |
&& (dtp->u.p.current_unit->child_dtio == 0) |
| 4545 |
&& dtp->u.p.current_unit->s) |
| 4546 |
{ |
| 4547 |
sclose (dtp->u.p.current_unit->s); |
| 4548 |
dtp->u.p.current_unit->s = NULL; |
| 4549 |
} |
| 4550 |
} |
| 4551 |
|
| 4552 |
#ifdef HAVE_POSIX_2008_LOCALE |
| 4553 |
if (dtp->u.p.old_locale != (locale_t) 0) |
| 4554 |
{ |
| 4555 |
uselocale (dtp->u.p.old_locale); |
| 4556 |
dtp->u.p.old_locale = (locale_t) 0; |
| 4557 |
} |
| 4558 |
#else |
| 4559 |
__gthread_mutex_lock (&old_locale_lock); |
| 4560 |
if (!--old_locale_ctr) |
| 4561 |
{ |
| 4562 |
setlocale (LC_NUMERIC, old_locale); |
| 4563 |
old_locale = NULL; |
| 4564 |
} |
| 4565 |
__gthread_mutex_unlock (&old_locale_lock); |
| 4566 |
#endif |
| 4567 |
} |
| 4568 |
|
| 4569 |
/* Transfer function for IOLENGTH. It doesn't actually do any |
| 4570 |
data transfer, it just updates the length counter. */ |
| 4571 |
|
| 4572 |
static void |
| 4573 |
iolength_transfer (st_parameter_dt *dtp, bt type __attribute__((unused)), |
| 4574 |
void *dest __attribute__ ((unused)), |
| 4575 |
int kind __attribute__((unused)), |
| 4576 |
size_t size, size_t nelems) |
| 4577 |
{ |
| 4578 |
if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0) |
| 4579 |
*dtp->iolength += (GFC_IO_INT) (size * nelems); |
| 4580 |
} |
| 4581 |
|
| 4582 |
|
| 4583 |
/* Initialize the IOLENGTH data transfer. This function is in essence |
| 4584 |
a very much simplified version of data_transfer_init(), because it |
| 4585 |
doesn't have to deal with units at all. */ |
| 4586 |
|
| 4587 |
static void |
| 4588 |
iolength_transfer_init (st_parameter_dt *dtp) |
| 4589 |
{ |
| 4590 |
if ((dtp->common.flags & IOPARM_DT_HAS_IOLENGTH) != 0) |
| 4591 |
*dtp->iolength = 0; |
| 4592 |
|
| 4593 |
memset (&dtp->u.p, 0, sizeof (dtp->u.p)); |
| 4594 |
|
| 4595 |
/* Set up the subroutine that will handle the transfers. */ |
| 4596 |
|
| 4597 |
dtp->u.p.transfer = iolength_transfer; |
| 4598 |
} |
| 4599 |
|
| 4600 |
|
| 4601 |
/* Library entry point for the IOLENGTH form of the INQUIRE |
| 4602 |
statement. The IOLENGTH form requires no I/O to be performed, but |
| 4603 |
it must still be a runtime library call so that we can determine |
| 4604 |
the iolength for dynamic arrays and such. */ |
| 4605 |
|
| 4606 |
extern void st_iolength (st_parameter_dt *); |
| 4607 |
export_proto(st_iolength); |
| 4608 |
|
| 4609 |
void |
| 4610 |
st_iolength (st_parameter_dt *dtp) |
| 4611 |
{ |
| 4612 |
library_start (&dtp->common); |
| 4613 |
iolength_transfer_init (dtp); |
| 4614 |
} |
| 4615 |
|
| 4616 |
extern void st_iolength_done (st_parameter_dt *); |
| 4617 |
export_proto(st_iolength_done); |
| 4618 |
|
| 4619 |
void |
| 4620 |
st_iolength_done (st_parameter_dt *dtp __attribute__((unused))) |
| 4621 |
{ |
| 4622 |
free_ionml (dtp); |
| 4623 |
library_end (); |
| 4624 |
} |
| 4625 |
|
| 4626 |
|
| 4627 |
/* The READ statement. */ |
| 4628 |
|
| 4629 |
extern void st_read (st_parameter_dt *); |
| 4630 |
export_proto(st_read); |
| 4631 |
|
| 4632 |
void |
| 4633 |
st_read (st_parameter_dt *dtp) |
| 4634 |
{ |
| 4635 |
library_start (&dtp->common); |
| 4636 |
|
| 4637 |
data_transfer_init (dtp, 1); |
| 4638 |
} |
| 4639 |
|
| 4640 |
extern void st_read_done (st_parameter_dt *); |
| 4641 |
export_proto(st_read_done); |
| 4642 |
|
| 4643 |
void |
| 4644 |
st_read_done_worker (st_parameter_dt *dtp, bool unlock) |
| 4645 |
{ |
| 4646 |
bool free_newunit = false; |
| 4647 |
finalize_transfer (dtp); |
| 4648 |
|
| 4649 |
free_ionml (dtp); |
| 4650 |
|
| 4651 |
/* If this is a parent READ statement we do not need to retain the |
| 4652 |
internal unit structure for child use. */ |
| 4653 |
if (dtp->u.p.current_unit != NULL |
| 4654 |
&& dtp->u.p.current_unit->child_dtio == 0) |
| 4655 |
{ |
| 4656 |
if (dtp->u.p.unit_is_internal) |
| 4657 |
{ |
| 4658 |
if ((dtp->common.flags & IOPARM_DT_HAS_UDTIO) == 0) |
| 4659 |
{ |
| 4660 |
free (dtp->u.p.current_unit->filename); |
| 4661 |
dtp->u.p.current_unit->filename = NULL; |
| 4662 |
free (dtp->u.p.current_unit->ls); |
| 4663 |
dtp->u.p.current_unit->ls = NULL; |
| 4664 |
} |
| 4665 |
free_newunit = true; |
| 4666 |
} |
| 4667 |
if (dtp->u.p.unit_is_internal || dtp->u.p.format_not_saved) |
| 4668 |
{ |
| 4669 |
free_format_data (dtp->u.p.fmt); |
| 4670 |
free_format (dtp); |
| 4671 |
} |
| 4672 |
} |
| 4673 |
if (unlock) |
| 4674 |
unlock_unit (dtp->u.p.current_unit); |
| 4675 |
if (free_newunit) |
| 4676 |
{ |
| 4677 |
/* Avoid inverse lock issues by placing after unlock_unit. */ |
| 4678 |
WRLOCK (&unit_rwlock); |
| 4679 |
newunit_free (dtp->common.unit); |
| 4680 |
RWUNLOCK (&unit_rwlock); |
| 4681 |
} |
| 4682 |
} |
| 4683 |
|
| 4684 |
void |
| 4685 |
st_read_done (st_parameter_dt *dtp) |
| 4686 |
{ |
| 4687 |
if (dtp->u.p.current_unit) |
| 4688 |
{ |
| 4689 |
if (dtp->u.p.current_unit->au) |
| 4690 |
{ |
| 4691 |
if (dtp->common.flags & IOPARM_DT_HAS_ID) |
| 4692 |
*dtp->id = enqueue_done_id (dtp->u.p.current_unit->au, AIO_READ_DONE); |
| 4693 |
else |
| 4694 |
{ |
| 4695 |
if (dtp->u.p.async) |
| 4696 |
enqueue_done (dtp->u.p.current_unit->au, AIO_READ_DONE); |
| 4697 |
} |
| 4698 |
unlock_unit (dtp->u.p.current_unit); |
| 4699 |
} |
| 4700 |
else |
| 4701 |
st_read_done_worker (dtp, true); /* Calls unlock_unit. */ |
| 4702 |
} |
| 4703 |
|
| 4704 |
library_end (); |
| 4705 |
} |
| 4706 |
|
| 4707 |
extern void st_write (st_parameter_dt *); |
| 4708 |
export_proto (st_write); |
| 4709 |
|
| 4710 |
void |
| 4711 |
st_write (st_parameter_dt *dtp) |
| 4712 |
{ |
| 4713 |
library_start (&dtp->common); |
| 4714 |
data_transfer_init (dtp, 0); |
| 4715 |
} |
| 4716 |
|
| 4717 |
|
| 4718 |
void |
| 4719 |
st_write_done_worker (st_parameter_dt *dtp, bool unlock) |
| 4720 |
{ |
| 4721 |
bool free_newunit = false; |
| 4722 |
finalize_transfer (dtp); |
| 4723 |
|
| 4724 |
if (dtp->u.p.current_unit != NULL |
| 4725 |
&& dtp->u.p.current_unit->child_dtio == 0) |
| 4726 |
{ |
| 4727 |
/* Deal with endfile conditions associated with sequential files. */ |
| 4728 |
if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL) |
| 4729 |
switch (dtp->u.p.current_unit->endfile) |
| 4730 |
{ |
| 4731 |
case AT_ENDFILE: /* Remain at the endfile record. */ |
| 4732 |
break; |
| 4733 |
|
| 4734 |
case AFTER_ENDFILE: |
| 4735 |
dtp->u.p.current_unit->endfile = AT_ENDFILE; /* Just at it now. */ |
| 4736 |
break; |
| 4737 |
|
| 4738 |
case NO_ENDFILE: |
| 4739 |
/* Get rid of whatever is after this record. */ |
| 4740 |
if (!is_internal_unit (dtp)) |
| 4741 |
unit_truncate (dtp->u.p.current_unit, |
| 4742 |
stell (dtp->u.p.current_unit->s), |
| 4743 |
&dtp->common); |
| 4744 |
dtp->u.p.current_unit->endfile = AT_ENDFILE; |
| 4745 |
break; |
| 4746 |
} |
| 4747 |
|
| 4748 |
free_ionml (dtp); |
| 4749 |
|
| 4750 |
/* If this is a parent WRITE statement we do not need to retain the |
| 4751 |
internal unit structure for child use. */ |
| 4752 |
if (dtp->u.p.unit_is_internal) |
| 4753 |
{ |
| 4754 |
if ((dtp->common.flags & IOPARM_DT_HAS_UDTIO) == 0) |
| 4755 |
{ |
| 4756 |
free (dtp->u.p.current_unit->filename); |
| 4757 |
dtp->u.p.current_unit->filename = NULL; |
| 4758 |
free (dtp->u.p.current_unit->ls); |
| 4759 |
dtp->u.p.current_unit->ls = NULL; |
| 4760 |
} |
| 4761 |
free_newunit = true; |
| 4762 |
} |
| 4763 |
if (dtp->u.p.unit_is_internal || dtp->u.p.format_not_saved) |
| 4764 |
{ |
| 4765 |
free_format_data (dtp->u.p.fmt); |
| 4766 |
free_format (dtp); |
| 4767 |
} |
| 4768 |
} |
| 4769 |
if (unlock) |
| 4770 |
unlock_unit (dtp->u.p.current_unit); |
| 4771 |
if (free_newunit) |
| 4772 |
{ |
| 4773 |
/* Avoid inverse lock issues by placing after unlock_unit. */ |
| 4774 |
WRLOCK (&unit_rwlock); |
| 4775 |
newunit_free (dtp->common.unit); |
| 4776 |
RWUNLOCK (&unit_rwlock); |
| 4777 |
} |
| 4778 |
} |
| 4779 |
|
| 4780 |
extern void st_write_done (st_parameter_dt *); |
| 4781 |
export_proto(st_write_done); |
| 4782 |
|
| 4783 |
void |
| 4784 |
st_write_done (st_parameter_dt *dtp) |
| 4785 |
{ |
| 4786 |
if (dtp->u.p.current_unit) |
| 4787 |
{ |
| 4788 |
if (dtp->u.p.current_unit->au && dtp->u.p.async) |
| 4789 |
{ |
| 4790 |
if (dtp->common.flags & IOPARM_DT_HAS_ID) |
| 4791 |
*dtp->id = enqueue_done_id (dtp->u.p.current_unit->au, |
| 4792 |
AIO_WRITE_DONE); |
| 4793 |
else |
| 4794 |
{ |
| 4795 |
/* We perform synchronous I/O on an asynchronous unit, so no need |
| 4796 |
to enqueue AIO_READ_DONE. */ |
| 4797 |
if (dtp->u.p.async) |
| 4798 |
enqueue_done (dtp->u.p.current_unit->au, AIO_WRITE_DONE); |
| 4799 |
} |
| 4800 |
unlock_unit (dtp->u.p.current_unit); |
| 4801 |
} |
| 4802 |
else |
| 4803 |
st_write_done_worker (dtp, true); /* Calls unlock_unit. */ |
| 4804 |
} |
| 4805 |
|
| 4806 |
library_end (); |
| 4807 |
} |
| 4808 |
|
| 4809 |
/* Wait operation. We need to keep around the do-nothing version |
| 4810 |
of st_wait for compatibility with previous versions, which had marked |
| 4811 |
the argument as unused (and thus liable to be removed). |
| 4812 |
|
| 4813 |
TODO: remove at next bump in version number. */ |
| 4814 |
|
| 4815 |
void |
| 4816 |
st_wait (st_parameter_wait *wtp __attribute__((unused))) |
| 4817 |
{ |
| 4818 |
return; |
| 4819 |
} |
| 4820 |
|
| 4821 |
void |
| 4822 |
st_wait_async (st_parameter_wait *wtp) |
| 4823 |
{ |
| 4824 |
gfc_unit *u = find_unit (wtp->common.unit); |
| 4825 |
if (ASYNC_IO && u && u->au) |
| 4826 |
{ |
| 4827 |
if (wtp->common.flags & IOPARM_WAIT_HAS_ID) |
| 4828 |
async_wait_id (&(wtp->common), u->au, *wtp->id); |
| 4829 |
else |
| 4830 |
async_wait (&(wtp->common), u->au); |
| 4831 |
} |
| 4832 |
|
| 4833 |
unlock_unit (u); |
| 4834 |
} |
| 4835 |
|
| 4836 |
|
| 4837 |
/* Receives the scalar information for namelist objects and stores it |
| 4838 |
in a linked list of namelist_info types. */ |
| 4839 |
|
| 4840 |
static void |
| 4841 |
set_nml_var (st_parameter_dt *dtp, void *var_addr, char *var_name, |
| 4842 |
GFC_INTEGER_4 len, gfc_charlen_type string_length, |
| 4843 |
dtype_type dtype, void *dtio_sub, void *vtable) |
| 4844 |
{ |
| 4845 |
namelist_info *t1 = NULL; |
| 4846 |
namelist_info *nml; |
| 4847 |
size_t var_name_len = strlen (var_name); |
| 4848 |
|
| 4849 |
nml = (namelist_info*) xmalloc (sizeof (namelist_info)); |
| 4850 |
|
| 4851 |
nml->mem_pos = var_addr; |
| 4852 |
nml->dtio_sub = dtio_sub; |
| 4853 |
nml->vtable = vtable; |
| 4854 |
|
| 4855 |
nml->var_name = (char*) xmalloc (var_name_len + 1); |
| 4856 |
memcpy (nml->var_name, var_name, var_name_len); |
| 4857 |
nml->var_name[var_name_len] = '\0'; |
| 4858 |
|
| 4859 |
nml->len = (int) len; |
| 4860 |
nml->string_length = (index_type) string_length; |
| 4861 |
|
| 4862 |
nml->var_rank = (int) (dtype.rank); |
| 4863 |
nml->size = (index_type) (dtype.elem_len); |
| 4864 |
nml->type = (bt) (dtype.type); |
| 4865 |
|
| 4866 |
if (nml->var_rank > 0) |
| 4867 |
{ |
| 4868 |
nml->dim = (descriptor_dimension*) |
| 4869 |
xmallocarray (nml->var_rank, sizeof (descriptor_dimension)); |
| 4870 |
nml->ls = (array_loop_spec*) |
| 4871 |
xmallocarray (nml->var_rank, sizeof (array_loop_spec)); |
| 4872 |
} |
| 4873 |
else |
| 4874 |
{ |
| 4875 |
nml->dim = NULL; |
| 4876 |
nml->ls = NULL; |
| 4877 |
} |
| 4878 |
|
| 4879 |
nml->next = NULL; |
| 4880 |
|
| 4881 |
if ((dtp->common.flags & IOPARM_DT_IONML_SET) == 0) |
| 4882 |
{ |
| 4883 |
dtp->common.flags |= IOPARM_DT_IONML_SET; |
| 4884 |
dtp->u.p.ionml = nml; |
| 4885 |
} |
| 4886 |
else |
| 4887 |
{ |
| 4888 |
for (t1 = dtp->u.p.ionml; t1->next; t1 = t1->next); |
| 4889 |
t1->next = nml; |
| 4890 |
} |
| 4891 |
} |
| 4892 |
|
| 4893 |
extern void st_set_nml_var (st_parameter_dt *dtp, void *, char *, |
| 4894 |
GFC_INTEGER_4, gfc_charlen_type, dtype_type); |
| 4895 |
export_proto(st_set_nml_var); |
| 4896 |
|
| 4897 |
void |
| 4898 |
st_set_nml_var (st_parameter_dt *dtp, void *var_addr, char *var_name, |
| 4899 |
GFC_INTEGER_4 len, gfc_charlen_type string_length, |
| 4900 |
dtype_type dtype) |
| 4901 |
{ |
| 4902 |
set_nml_var (dtp, var_addr, var_name, len, string_length, |
| 4903 |
dtype, NULL, NULL); |
| 4904 |
} |
| 4905 |
|
| 4906 |
|
| 4907 |
/* Essentially the same as previous but carrying the dtio procedure |
| 4908 |
and the vtable as additional arguments. */ |
| 4909 |
extern void st_set_nml_dtio_var (st_parameter_dt *dtp, void *, char *, |
| 4910 |
GFC_INTEGER_4, gfc_charlen_type, dtype_type, |
| 4911 |
void *, void *); |
| 4912 |
export_proto(st_set_nml_dtio_var); |
| 4913 |
|
| 4914 |
|
| 4915 |
void |
| 4916 |
st_set_nml_dtio_var (st_parameter_dt *dtp, void *var_addr, char *var_name, |
| 4917 |
GFC_INTEGER_4 len, gfc_charlen_type string_length, |
| 4918 |
dtype_type dtype, void *dtio_sub, void *vtable) |
| 4919 |
{ |
| 4920 |
set_nml_var (dtp, var_addr, var_name, len, string_length, |
| 4921 |
dtype, dtio_sub, vtable); |
| 4922 |
} |
| 4923 |
|
| 4924 |
/* Store the dimensional information for the namelist object. */ |
| 4925 |
extern void st_set_nml_var_dim (st_parameter_dt *, GFC_INTEGER_4, |
| 4926 |
index_type, index_type, |
| 4927 |
index_type); |
| 4928 |
export_proto(st_set_nml_var_dim); |
| 4929 |
|
| 4930 |
void |
| 4931 |
st_set_nml_var_dim (st_parameter_dt *dtp, GFC_INTEGER_4 n_dim, |
| 4932 |
index_type stride, index_type lbound, |
| 4933 |
index_type ubound) |
| 4934 |
{ |
| 4935 |
namelist_info *nml; |
| 4936 |
int n; |
| 4937 |
|
| 4938 |
n = (int)n_dim; |
| 4939 |
|
| 4940 |
for (nml = dtp->u.p.ionml; nml->next; nml = nml->next); |
| 4941 |
|
| 4942 |
GFC_DIMENSION_SET(nml->dim[n],lbound,ubound,stride); |
| 4943 |
} |
| 4944 |
|
| 4945 |
|
| 4946 |
/* Once upon a time, a poor innocent Fortran program was reading a |
| 4947 |
file, when suddenly it hit the end-of-file (EOF). Unfortunately |
| 4948 |
the OS doesn't tell whether we're at the EOF or whether we already |
| 4949 |
went past it. Luckily our hero, libgfortran, keeps track of this. |
| 4950 |
Call this function when you detect an EOF condition. See Section |
| 4951 |
9.10.2 in F2003. */ |
| 4952 |
|
| 4953 |
void |
| 4954 |
hit_eof (st_parameter_dt *dtp) |
| 4955 |
{ |
| 4956 |
dtp->u.p.current_unit->flags.position = POSITION_APPEND; |
| 4957 |
|
| 4958 |
if (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL) |
| 4959 |
switch (dtp->u.p.current_unit->endfile) |
| 4960 |
{ |
| 4961 |
case NO_ENDFILE: |
| 4962 |
case AT_ENDFILE: |
| 4963 |
generate_error (&dtp->common, LIBERROR_END, NULL); |
| 4964 |
if (!is_internal_unit (dtp) && !dtp->u.p.namelist_mode) |
| 4965 |
{ |
| 4966 |
dtp->u.p.current_unit->endfile = AFTER_ENDFILE; |
| 4967 |
dtp->u.p.current_unit->current_record = 0; |
| 4968 |
} |
| 4969 |
else |
| 4970 |
dtp->u.p.current_unit->endfile = AT_ENDFILE; |
| 4971 |
break; |
| 4972 |
|
| 4973 |
case AFTER_ENDFILE: |
| 4974 |
generate_error (&dtp->common, LIBERROR_ENDFILE, NULL); |
| 4975 |
dtp->u.p.current_unit->current_record = 0; |
| 4976 |
break; |
| 4977 |
} |
| 4978 |
else |
| 4979 |
{ |
| 4980 |
/* Non-sequential files don't have an ENDFILE record, so we |
| 4981 |
can't be at AFTER_ENDFILE. */ |
| 4982 |
dtp->u.p.current_unit->endfile = AT_ENDFILE; |
| 4983 |
generate_error (&dtp->common, LIBERROR_END, NULL); |
| 4984 |
dtp->u.p.current_unit->current_record = 0; |
| 4985 |
} |
| 4986 |
} |