1 //===-- runtime/io-api.cpp ------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 // Implements the I/O statement API 10 11 #include "flang/Runtime/io-api.h" 12 #include "descriptor-io.h" 13 #include "edit-input.h" 14 #include "edit-output.h" 15 #include "environment.h" 16 #include "format.h" 17 #include "io-stmt.h" 18 #include "terminator.h" 19 #include "tools.h" 20 #include "unit.h" 21 #include "flang/Runtime/descriptor.h" 22 #include "flang/Runtime/memory.h" 23 #include <cstdlib> 24 #include <memory> 25 26 namespace Fortran::runtime::io { 27 28 const char *InquiryKeywordHashDecode( 29 char *buffer, std::size_t n, InquiryKeywordHash hash) { 30 if (n < 1) { 31 return nullptr; 32 } 33 char *p{buffer + n}; 34 *--p = '\0'; 35 while (hash > 1) { 36 if (p < buffer) { 37 return nullptr; 38 } 39 *--p = 'A' + (hash % 26); 40 hash /= 26; 41 } 42 return hash == 1 ? p : nullptr; 43 } 44 45 template <Direction DIR> 46 Cookie BeginInternalArrayListIO(const Descriptor &descriptor, 47 void ** /*scratchArea*/, std::size_t /*scratchBytes*/, 48 const char *sourceFile, int sourceLine) { 49 Terminator oom{sourceFile, sourceLine}; 50 return &New<InternalListIoStatementState<DIR>>{oom}( 51 descriptor, sourceFile, sourceLine) 52 .release() 53 ->ioStatementState(); 54 } 55 56 Cookie IONAME(BeginInternalArrayListOutput)(const Descriptor &descriptor, 57 void **scratchArea, std::size_t scratchBytes, const char *sourceFile, 58 int sourceLine) { 59 return BeginInternalArrayListIO<Direction::Output>( 60 descriptor, scratchArea, scratchBytes, sourceFile, sourceLine); 61 } 62 63 Cookie IONAME(BeginInternalArrayListInput)(const Descriptor &descriptor, 64 void **scratchArea, std::size_t scratchBytes, const char *sourceFile, 65 int sourceLine) { 66 return BeginInternalArrayListIO<Direction::Input>( 67 descriptor, scratchArea, scratchBytes, sourceFile, sourceLine); 68 } 69 70 template <Direction DIR> 71 Cookie BeginInternalArrayFormattedIO(const Descriptor &descriptor, 72 const char *format, std::size_t formatLength, void ** /*scratchArea*/, 73 std::size_t /*scratchBytes*/, const char *sourceFile, int sourceLine) { 74 Terminator oom{sourceFile, sourceLine}; 75 return &New<InternalFormattedIoStatementState<DIR>>{oom}( 76 descriptor, format, formatLength, sourceFile, sourceLine) 77 .release() 78 ->ioStatementState(); 79 } 80 81 Cookie IONAME(BeginInternalArrayFormattedOutput)(const Descriptor &descriptor, 82 const char *format, std::size_t formatLength, void **scratchArea, 83 std::size_t scratchBytes, const char *sourceFile, int sourceLine) { 84 return BeginInternalArrayFormattedIO<Direction::Output>(descriptor, format, 85 formatLength, scratchArea, scratchBytes, sourceFile, sourceLine); 86 } 87 88 Cookie IONAME(BeginInternalArrayFormattedInput)(const Descriptor &descriptor, 89 const char *format, std::size_t formatLength, void **scratchArea, 90 std::size_t scratchBytes, const char *sourceFile, int sourceLine) { 91 return BeginInternalArrayFormattedIO<Direction::Input>(descriptor, format, 92 formatLength, scratchArea, scratchBytes, sourceFile, sourceLine); 93 } 94 95 template <Direction DIR> 96 Cookie BeginInternalListIO( 97 std::conditional_t<DIR == Direction::Input, const char, char> *internal, 98 std::size_t internalLength, void ** /*scratchArea*/, 99 std::size_t /*scratchBytes*/, const char *sourceFile, int sourceLine) { 100 Terminator oom{sourceFile, sourceLine}; 101 return &New<InternalListIoStatementState<DIR>>{oom}( 102 internal, internalLength, sourceFile, sourceLine) 103 .release() 104 ->ioStatementState(); 105 } 106 107 Cookie IONAME(BeginInternalListOutput)(char *internal, 108 std::size_t internalLength, void **scratchArea, std::size_t scratchBytes, 109 const char *sourceFile, int sourceLine) { 110 return BeginInternalListIO<Direction::Output>(internal, internalLength, 111 scratchArea, scratchBytes, sourceFile, sourceLine); 112 } 113 114 Cookie IONAME(BeginInternalListInput)(const char *internal, 115 std::size_t internalLength, void **scratchArea, std::size_t scratchBytes, 116 const char *sourceFile, int sourceLine) { 117 return BeginInternalListIO<Direction::Input>(internal, internalLength, 118 scratchArea, scratchBytes, sourceFile, sourceLine); 119 } 120 121 template <Direction DIR> 122 Cookie BeginInternalFormattedIO( 123 std::conditional_t<DIR == Direction::Input, const char, char> *internal, 124 std::size_t internalLength, const char *format, std::size_t formatLength, 125 void ** /*scratchArea*/, std::size_t /*scratchBytes*/, 126 const char *sourceFile, int sourceLine) { 127 Terminator oom{sourceFile, sourceLine}; 128 return &New<InternalFormattedIoStatementState<DIR>>{oom}( 129 internal, internalLength, format, formatLength, sourceFile, sourceLine) 130 .release() 131 ->ioStatementState(); 132 } 133 134 Cookie IONAME(BeginInternalFormattedOutput)(char *internal, 135 std::size_t internalLength, const char *format, std::size_t formatLength, 136 void **scratchArea, std::size_t scratchBytes, const char *sourceFile, 137 int sourceLine) { 138 return BeginInternalFormattedIO<Direction::Output>(internal, internalLength, 139 format, formatLength, scratchArea, scratchBytes, sourceFile, sourceLine); 140 } 141 142 Cookie IONAME(BeginInternalFormattedInput)(const char *internal, 143 std::size_t internalLength, const char *format, std::size_t formatLength, 144 void **scratchArea, std::size_t scratchBytes, const char *sourceFile, 145 int sourceLine) { 146 return BeginInternalFormattedIO<Direction::Input>(internal, internalLength, 147 format, formatLength, scratchArea, scratchBytes, sourceFile, sourceLine); 148 } 149 150 template <Direction DIR, template <Direction> class STATE, typename... A> 151 Cookie BeginExternalListIO(const char *what, int unitNumber, 152 const char *sourceFile, int sourceLine, A &&...xs) { 153 Terminator terminator{sourceFile, sourceLine}; 154 if (unitNumber == DefaultUnit) { 155 unitNumber = DIR == Direction::Input ? 5 : 6; 156 } 157 ExternalFileUnit &unit{ExternalFileUnit::LookUpOrCreateAnonymous( 158 unitNumber, DIR, false /*!unformatted*/, terminator)}; 159 if (!unit.isUnformatted.has_value()) { 160 unit.isUnformatted = false; 161 } 162 if (*unit.isUnformatted) { 163 terminator.Crash("%s attempted on unformatted file", what); 164 return nullptr; 165 } 166 if (ChildIo * child{unit.GetChildIo()}) { 167 return child->CheckFormattingAndDirection(terminator, what, false, DIR) 168 ? &child->BeginIoStatement<ChildListIoStatementState<DIR>>( 169 *child, sourceFile, sourceLine) 170 : nullptr; 171 } else { 172 if (unit.access == Access::Direct) { 173 terminator.Crash("%s attempted on direct access file", what); 174 return nullptr; 175 } 176 IoErrorHandler handler{terminator}; 177 unit.SetDirection(DIR, handler); 178 IoStatementState &io{unit.BeginIoStatement<STATE<DIR>>( 179 std::forward<A>(xs)..., unit, sourceFile, sourceLine)}; 180 return &io; 181 } 182 } 183 184 Cookie IONAME(BeginExternalListOutput)( 185 ExternalUnit unitNumber, const char *sourceFile, int sourceLine) { 186 return BeginExternalListIO<Direction::Output, ExternalListIoStatementState>( 187 "List-directed output", unitNumber, sourceFile, sourceLine); 188 } 189 190 Cookie IONAME(BeginExternalListInput)( 191 ExternalUnit unitNumber, const char *sourceFile, int sourceLine) { 192 return BeginExternalListIO<Direction::Input, ExternalListIoStatementState>( 193 "List-directed input", unitNumber, sourceFile, sourceLine); 194 } 195 196 template <Direction DIR> 197 Cookie BeginExternalFormattedIO(const char *format, std::size_t formatLength, 198 ExternalUnit unitNumber, const char *sourceFile, int sourceLine) { 199 Terminator terminator{sourceFile, sourceLine}; 200 if (unitNumber == DefaultUnit) { 201 unitNumber = DIR == Direction::Input ? 5 : 6; 202 } 203 ExternalFileUnit &unit{ExternalFileUnit::LookUpOrCreateAnonymous( 204 unitNumber, DIR, false /*!unformatted*/, terminator)}; 205 if (!unit.isUnformatted.has_value()) { 206 unit.isUnformatted = false; 207 } 208 if (*unit.isUnformatted) { 209 terminator.Crash("Formatted I/O attempted on unformatted file"); 210 return nullptr; 211 } 212 if (ChildIo * child{unit.GetChildIo()}) { 213 return child->CheckFormattingAndDirection(terminator, 214 DIR == Direction::Output ? "formatted output" 215 : "formatted input", 216 false, DIR) 217 ? &child->BeginIoStatement<ChildFormattedIoStatementState<DIR>>( 218 *child, format, formatLength, sourceFile, sourceLine) 219 : nullptr; 220 } else { 221 IoErrorHandler handler{terminator}; 222 unit.SetDirection(DIR, handler); 223 IoStatementState &io{ 224 unit.BeginIoStatement<ExternalFormattedIoStatementState<DIR>>( 225 unit, format, formatLength, sourceFile, sourceLine)}; 226 return &io; 227 } 228 } 229 230 Cookie IONAME(BeginExternalFormattedOutput)(const char *format, 231 std::size_t formatLength, ExternalUnit unitNumber, const char *sourceFile, 232 int sourceLine) { 233 return BeginExternalFormattedIO<Direction::Output>( 234 format, formatLength, unitNumber, sourceFile, sourceLine); 235 } 236 237 Cookie IONAME(BeginExternalFormattedInput)(const char *format, 238 std::size_t formatLength, ExternalUnit unitNumber, const char *sourceFile, 239 int sourceLine) { 240 return BeginExternalFormattedIO<Direction::Input>( 241 format, formatLength, unitNumber, sourceFile, sourceLine); 242 } 243 244 template <Direction DIR> 245 Cookie BeginUnformattedIO( 246 ExternalUnit unitNumber, const char *sourceFile, int sourceLine) { 247 Terminator terminator{sourceFile, sourceLine}; 248 ExternalFileUnit &unit{ExternalFileUnit::LookUpOrCreateAnonymous( 249 unitNumber, DIR, true /*unformatted*/, terminator)}; 250 if (!unit.isUnformatted.has_value()) { 251 unit.isUnformatted = true; 252 } 253 if (!*unit.isUnformatted) { 254 terminator.Crash("Unformatted I/O attempted on formatted file"); 255 } 256 if (ChildIo * child{unit.GetChildIo()}) { 257 return child->CheckFormattingAndDirection(terminator, 258 DIR == Direction::Output ? "unformatted output" 259 : "unformatted input", 260 true, DIR) 261 ? &child->BeginIoStatement<ChildUnformattedIoStatementState<DIR>>( 262 *child, sourceFile, sourceLine) 263 : nullptr; 264 } else { 265 IoStatementState &io{ 266 unit.BeginIoStatement<ExternalUnformattedIoStatementState<DIR>>( 267 unit, sourceFile, sourceLine)}; 268 IoErrorHandler handler{terminator}; 269 unit.SetDirection(DIR, handler); 270 if constexpr (DIR == Direction::Output) { 271 if (unit.access == Access::Sequential) { 272 // Create space for (sub)record header to be completed by 273 // ExternalFileUnit::AdvanceRecord() 274 unit.recordLength.reset(); // in case of prior BACKSPACE 275 io.Emit("\0\0\0\0", 4); // placeholder for record length header 276 } 277 } 278 return &io; 279 } 280 } 281 282 Cookie IONAME(BeginUnformattedOutput)( 283 ExternalUnit unitNumber, const char *sourceFile, int sourceLine) { 284 return BeginUnformattedIO<Direction::Output>( 285 unitNumber, sourceFile, sourceLine); 286 } 287 288 Cookie IONAME(BeginUnformattedInput)( 289 ExternalUnit unitNumber, const char *sourceFile, int sourceLine) { 290 return BeginUnformattedIO<Direction::Input>( 291 unitNumber, sourceFile, sourceLine); 292 } 293 294 Cookie IONAME(BeginOpenUnit)( // OPEN(without NEWUNIT=) 295 ExternalUnit unitNumber, const char *sourceFile, int sourceLine) { 296 bool wasExtant{false}; 297 Terminator terminator{sourceFile, sourceLine}; 298 ExternalFileUnit &unit{ 299 ExternalFileUnit::LookUpOrCreate(unitNumber, terminator, wasExtant)}; 300 return &unit.BeginIoStatement<OpenStatementState>( 301 unit, wasExtant, sourceFile, sourceLine); 302 } 303 304 Cookie IONAME(BeginOpenNewUnit)( // OPEN(NEWUNIT=j) 305 const char *sourceFile, int sourceLine) { 306 Terminator terminator{sourceFile, sourceLine}; 307 ExternalFileUnit &unit{ 308 ExternalFileUnit::NewUnit(terminator, false /*not child I/O*/)}; 309 return &unit.BeginIoStatement<OpenStatementState>( 310 unit, false /*was an existing file*/, sourceFile, sourceLine); 311 } 312 313 Cookie IONAME(BeginClose)( 314 ExternalUnit unitNumber, const char *sourceFile, int sourceLine) { 315 if (ExternalFileUnit * unit{ExternalFileUnit::LookUpForClose(unitNumber)}) { 316 return &unit->BeginIoStatement<CloseStatementState>( 317 *unit, sourceFile, sourceLine); 318 } else { 319 // CLOSE(UNIT=bad unit) is just a no-op 320 Terminator oom{sourceFile, sourceLine}; 321 return &New<NoopStatementState>{oom}(sourceFile, sourceLine) 322 .release() 323 ->ioStatementState(); 324 } 325 } 326 327 Cookie IONAME(BeginFlush)( 328 ExternalUnit unitNumber, const char *sourceFile, int sourceLine) { 329 if (ExternalFileUnit * unit{ExternalFileUnit::LookUp(unitNumber)}) { 330 return &unit->BeginIoStatement<ExternalMiscIoStatementState>( 331 *unit, ExternalMiscIoStatementState::Flush, sourceFile, sourceLine); 332 } else { 333 // FLUSH(UNIT=unknown) is a no-op 334 Terminator oom{sourceFile, sourceLine}; 335 return &New<NoopStatementState>{oom}(sourceFile, sourceLine) 336 .release() 337 ->ioStatementState(); 338 } 339 } 340 341 Cookie IONAME(BeginBackspace)( 342 ExternalUnit unitNumber, const char *sourceFile, int sourceLine) { 343 Terminator terminator{sourceFile, sourceLine}; 344 ExternalFileUnit &unit{ 345 ExternalFileUnit::LookUpOrCrash(unitNumber, terminator)}; 346 return &unit.BeginIoStatement<ExternalMiscIoStatementState>( 347 unit, ExternalMiscIoStatementState::Backspace, sourceFile, sourceLine); 348 } 349 350 Cookie IONAME(BeginEndfile)( 351 ExternalUnit unitNumber, const char *sourceFile, int sourceLine) { 352 Terminator terminator{sourceFile, sourceLine}; 353 ExternalFileUnit &unit{ExternalFileUnit::LookUpOrCreateAnonymous( 354 unitNumber, Direction::Output, std::nullopt, terminator)}; 355 return &unit.BeginIoStatement<ExternalMiscIoStatementState>( 356 unit, ExternalMiscIoStatementState::Endfile, sourceFile, sourceLine); 357 } 358 359 Cookie IONAME(BeginRewind)( 360 ExternalUnit unitNumber, const char *sourceFile, int sourceLine) { 361 Terminator terminator{sourceFile, sourceLine}; 362 ExternalFileUnit &unit{ExternalFileUnit::LookUpOrCreateAnonymous( 363 unitNumber, Direction::Input, std::nullopt, terminator)}; 364 return &unit.BeginIoStatement<ExternalMiscIoStatementState>( 365 unit, ExternalMiscIoStatementState::Rewind, sourceFile, sourceLine); 366 } 367 368 Cookie IONAME(BeginInquireUnit)( 369 ExternalUnit unitNumber, const char *sourceFile, int sourceLine) { 370 if (ExternalFileUnit * unit{ExternalFileUnit::LookUp(unitNumber)}) { 371 if (ChildIo * child{unit->GetChildIo()}) { 372 return &child->BeginIoStatement<InquireUnitState>( 373 *unit, sourceFile, sourceLine); 374 } else { 375 return &unit->BeginIoStatement<InquireUnitState>( 376 *unit, sourceFile, sourceLine); 377 } 378 } else { 379 // INQUIRE(UNIT=unrecognized unit) 380 Terminator oom{sourceFile, sourceLine}; 381 return &New<InquireNoUnitState>{oom}(sourceFile, sourceLine) 382 .release() 383 ->ioStatementState(); 384 } 385 } 386 387 Cookie IONAME(BeginInquireFile)(const char *path, std::size_t pathLength, 388 const char *sourceFile, int sourceLine) { 389 Terminator oom{sourceFile, sourceLine}; 390 auto trimmed{ 391 SaveDefaultCharacter(path, TrimTrailingSpaces(path, pathLength), oom)}; 392 if (ExternalFileUnit * unit{ExternalFileUnit::LookUp(trimmed.get())}) { 393 // INQUIRE(FILE=) to a connected unit 394 return &unit->BeginIoStatement<InquireUnitState>( 395 *unit, sourceFile, sourceLine); 396 } else { 397 return &New<InquireUnconnectedFileState>{oom}( 398 std::move(trimmed), sourceFile, sourceLine) 399 .release() 400 ->ioStatementState(); 401 } 402 } 403 404 Cookie IONAME(BeginInquireIoLength)(const char *sourceFile, int sourceLine) { 405 Terminator oom{sourceFile, sourceLine}; 406 return &New<InquireIOLengthState>{oom}(sourceFile, sourceLine) 407 .release() 408 ->ioStatementState(); 409 } 410 411 // Control list items 412 413 void IONAME(EnableHandlers)(Cookie cookie, bool hasIoStat, bool hasErr, 414 bool hasEnd, bool hasEor, bool hasIoMsg) { 415 IoErrorHandler &handler{cookie->GetIoErrorHandler()}; 416 if (hasIoStat) { 417 handler.HasIoStat(); 418 } 419 if (hasErr) { 420 handler.HasErrLabel(); 421 } 422 if (hasEnd) { 423 handler.HasEndLabel(); 424 } 425 if (hasEor) { 426 handler.HasEorLabel(); 427 } 428 if (hasIoMsg) { 429 handler.HasIoMsg(); 430 } 431 } 432 433 static bool YesOrNo(const char *keyword, std::size_t length, const char *what, 434 IoErrorHandler &handler) { 435 static const char *keywords[]{"YES", "NO", nullptr}; 436 switch (IdentifyValue(keyword, length, keywords)) { 437 case 0: 438 return true; 439 case 1: 440 return false; 441 default: 442 handler.SignalError(IostatErrorInKeyword, "Invalid %s='%.*s'", what, 443 static_cast<int>(length), keyword); 444 return false; 445 } 446 } 447 448 bool IONAME(SetAdvance)( 449 Cookie cookie, const char *keyword, std::size_t length) { 450 IoStatementState &io{*cookie}; 451 bool nonAdvancing{ 452 !YesOrNo(keyword, length, "ADVANCE", io.GetIoErrorHandler())}; 453 if (nonAdvancing && io.GetConnectionState().access == Access::Direct) { 454 io.GetIoErrorHandler().SignalError( 455 "Non-advancing I/O attempted on direct access file"); 456 } else { 457 io.mutableModes().nonAdvancing = nonAdvancing; 458 } 459 return true; 460 } 461 462 bool IONAME(SetBlank)(Cookie cookie, const char *keyword, std::size_t length) { 463 IoStatementState &io{*cookie}; 464 ConnectionState &connection{io.GetConnectionState()}; 465 static const char *keywords[]{"NULL", "ZERO", nullptr}; 466 switch (IdentifyValue(keyword, length, keywords)) { 467 case 0: 468 connection.modes.editingFlags &= ~blankZero; 469 return true; 470 case 1: 471 connection.modes.editingFlags |= blankZero; 472 return true; 473 default: 474 io.GetIoErrorHandler().SignalError(IostatErrorInKeyword, 475 "Invalid BLANK='%.*s'", static_cast<int>(length), keyword); 476 return false; 477 } 478 } 479 480 bool IONAME(SetDecimal)( 481 Cookie cookie, const char *keyword, std::size_t length) { 482 IoStatementState &io{*cookie}; 483 ConnectionState &connection{io.GetConnectionState()}; 484 static const char *keywords[]{"COMMA", "POINT", nullptr}; 485 switch (IdentifyValue(keyword, length, keywords)) { 486 case 0: 487 connection.modes.editingFlags |= decimalComma; 488 return true; 489 case 1: 490 connection.modes.editingFlags &= ~decimalComma; 491 return true; 492 default: 493 io.GetIoErrorHandler().SignalError(IostatErrorInKeyword, 494 "Invalid DECIMAL='%.*s'", static_cast<int>(length), keyword); 495 return false; 496 } 497 } 498 499 bool IONAME(SetDelim)(Cookie cookie, const char *keyword, std::size_t length) { 500 IoStatementState &io{*cookie}; 501 ConnectionState &connection{io.GetConnectionState()}; 502 static const char *keywords[]{"APOSTROPHE", "QUOTE", "NONE", nullptr}; 503 switch (IdentifyValue(keyword, length, keywords)) { 504 case 0: 505 connection.modes.delim = '\''; 506 return true; 507 case 1: 508 connection.modes.delim = '"'; 509 return true; 510 case 2: 511 connection.modes.delim = '\0'; 512 return true; 513 default: 514 io.GetIoErrorHandler().SignalError(IostatErrorInKeyword, 515 "Invalid DELIM='%.*s'", static_cast<int>(length), keyword); 516 return false; 517 } 518 } 519 520 bool IONAME(SetPad)(Cookie cookie, const char *keyword, std::size_t length) { 521 IoStatementState &io{*cookie}; 522 ConnectionState &connection{io.GetConnectionState()}; 523 connection.modes.pad = 524 YesOrNo(keyword, length, "PAD", io.GetIoErrorHandler()); 525 return true; 526 } 527 528 bool IONAME(SetPos)(Cookie cookie, std::int64_t pos) { 529 IoStatementState &io{*cookie}; 530 ConnectionState &connection{io.GetConnectionState()}; 531 if (connection.access != Access::Stream) { 532 io.GetIoErrorHandler().SignalError( 533 "POS= may not appear unless ACCESS='STREAM'"); 534 return false; 535 } 536 if (pos < 1) { 537 io.GetIoErrorHandler().SignalError( 538 "POS=%zd is invalid", static_cast<std::intmax_t>(pos)); 539 return false; 540 } 541 if (auto *unit{io.GetExternalFileUnit()}) { 542 unit->SetPosition(pos); 543 return true; 544 } 545 io.GetIoErrorHandler().Crash("SetPos() on internal unit"); 546 return false; 547 } 548 549 bool IONAME(SetRec)(Cookie cookie, std::int64_t rec) { 550 IoStatementState &io{*cookie}; 551 ConnectionState &connection{io.GetConnectionState()}; 552 if (connection.access != Access::Direct) { 553 io.GetIoErrorHandler().SignalError( 554 "REC= may not appear unless ACCESS='DIRECT'"); 555 return false; 556 } 557 if (!connection.openRecl) { 558 io.GetIoErrorHandler().SignalError("RECL= was not specified"); 559 return false; 560 } 561 if (rec < 1) { 562 io.GetIoErrorHandler().SignalError( 563 "REC=%zd is invalid", static_cast<std::intmax_t>(rec)); 564 return false; 565 } 566 connection.currentRecordNumber = rec; 567 if (auto *unit{io.GetExternalFileUnit()}) { 568 unit->SetPosition((rec - 1) * *connection.openRecl); 569 } 570 return true; 571 } 572 573 bool IONAME(SetRound)(Cookie cookie, const char *keyword, std::size_t length) { 574 IoStatementState &io{*cookie}; 575 ConnectionState &connection{io.GetConnectionState()}; 576 static const char *keywords[]{"UP", "DOWN", "ZERO", "NEAREST", "COMPATIBLE", 577 "PROCESSOR_DEFINED", nullptr}; 578 switch (IdentifyValue(keyword, length, keywords)) { 579 case 0: 580 connection.modes.round = decimal::RoundUp; 581 return true; 582 case 1: 583 connection.modes.round = decimal::RoundDown; 584 return true; 585 case 2: 586 connection.modes.round = decimal::RoundToZero; 587 return true; 588 case 3: 589 connection.modes.round = decimal::RoundNearest; 590 return true; 591 case 4: 592 connection.modes.round = decimal::RoundCompatible; 593 return true; 594 case 5: 595 connection.modes.round = executionEnvironment.defaultOutputRoundingMode; 596 return true; 597 default: 598 io.GetIoErrorHandler().SignalError(IostatErrorInKeyword, 599 "Invalid ROUND='%.*s'", static_cast<int>(length), keyword); 600 return false; 601 } 602 } 603 604 bool IONAME(SetSign)(Cookie cookie, const char *keyword, std::size_t length) { 605 IoStatementState &io{*cookie}; 606 ConnectionState &connection{io.GetConnectionState()}; 607 static const char *keywords[]{ 608 "PLUS", "SUPPRESS", "PROCESSOR_DEFINED", nullptr}; 609 switch (IdentifyValue(keyword, length, keywords)) { 610 case 0: 611 connection.modes.editingFlags |= signPlus; 612 return true; 613 case 1: 614 case 2: // processor default is SS 615 connection.modes.editingFlags &= ~signPlus; 616 return true; 617 default: 618 io.GetIoErrorHandler().SignalError(IostatErrorInKeyword, 619 "Invalid SIGN='%.*s'", static_cast<int>(length), keyword); 620 return false; 621 } 622 } 623 624 bool IONAME(SetAccess)(Cookie cookie, const char *keyword, std::size_t length) { 625 IoStatementState &io{*cookie}; 626 auto *open{io.get_if<OpenStatementState>()}; 627 if (!open) { 628 io.GetIoErrorHandler().Crash( 629 "SetAccess() called when not in an OPEN statement"); 630 } 631 static const char *keywords[]{ 632 "SEQUENTIAL", "DIRECT", "STREAM", "APPEND", nullptr}; 633 switch (IdentifyValue(keyword, length, keywords)) { 634 case 0: 635 open->set_access(Access::Sequential); 636 break; 637 case 1: 638 open->set_access(Access::Direct); 639 break; 640 case 2: 641 open->set_access(Access::Stream); 642 break; 643 case 3: // Sun Fortran extension ACCESS=APPEND: treat as if POSITION=APPEND 644 open->set_position(Position::Append); 645 break; 646 default: 647 open->SignalError(IostatErrorInKeyword, "Invalid ACCESS='%.*s'", 648 static_cast<int>(length), keyword); 649 } 650 return true; 651 } 652 653 bool IONAME(SetAction)(Cookie cookie, const char *keyword, std::size_t length) { 654 IoStatementState &io{*cookie}; 655 auto *open{io.get_if<OpenStatementState>()}; 656 if (!open) { 657 io.GetIoErrorHandler().Crash( 658 "SetAction() called when not in an OPEN statement"); 659 } 660 std::optional<Action> action; 661 static const char *keywords[]{"READ", "WRITE", "READWRITE", nullptr}; 662 switch (IdentifyValue(keyword, length, keywords)) { 663 case 0: 664 action = Action::Read; 665 break; 666 case 1: 667 action = Action::Write; 668 break; 669 case 2: 670 action = Action::ReadWrite; 671 break; 672 default: 673 open->SignalError(IostatErrorInKeyword, "Invalid ACTION='%.*s'", 674 static_cast<int>(length), keyword); 675 return false; 676 } 677 RUNTIME_CHECK(io.GetIoErrorHandler(), action.has_value()); 678 if (open->wasExtant()) { 679 if ((*action != Action::Write) != open->unit().mayRead() || 680 (*action != Action::Read) != open->unit().mayWrite()) { 681 open->SignalError("ACTION= may not be changed on an open unit"); 682 } 683 } 684 open->set_action(*action); 685 return true; 686 } 687 688 bool IONAME(SetAsynchronous)( 689 Cookie cookie, const char *keyword, std::size_t length) { 690 IoStatementState &io{*cookie}; 691 auto *open{io.get_if<OpenStatementState>()}; 692 if (!open) { 693 io.GetIoErrorHandler().Crash( 694 "SetAsynchronous() called when not in an OPEN statement"); 695 } 696 static const char *keywords[]{"YES", "NO", nullptr}; 697 switch (IdentifyValue(keyword, length, keywords)) { 698 case 0: 699 open->unit().set_mayAsynchronous(true); 700 return true; 701 case 1: 702 open->unit().set_mayAsynchronous(false); 703 return true; 704 default: 705 open->SignalError(IostatErrorInKeyword, "Invalid ASYNCHRONOUS='%.*s'", 706 static_cast<int>(length), keyword); 707 return false; 708 } 709 } 710 711 bool IONAME(SetCarriagecontrol)( 712 Cookie cookie, const char *keyword, std::size_t length) { 713 IoStatementState &io{*cookie}; 714 auto *open{io.get_if<OpenStatementState>()}; 715 if (!open) { 716 io.GetIoErrorHandler().Crash( 717 "SetCarriageControl() called when not in an OPEN statement"); 718 } 719 static const char *keywords[]{"LIST", "FORTRAN", "NONE", nullptr}; 720 switch (IdentifyValue(keyword, length, keywords)) { 721 case 0: 722 return true; 723 case 1: 724 case 2: 725 open->SignalError(IostatErrorInKeyword, 726 "Unimplemented CARRIAGECONTROL='%.*s'", static_cast<int>(length), 727 keyword); 728 return false; 729 default: 730 open->SignalError(IostatErrorInKeyword, "Invalid CARRIAGECONTROL='%.*s'", 731 static_cast<int>(length), keyword); 732 return false; 733 } 734 } 735 736 bool IONAME(SetConvert)( 737 Cookie cookie, const char *keyword, std::size_t length) { 738 IoStatementState &io{*cookie}; 739 auto *open{io.get_if<OpenStatementState>()}; 740 if (!open) { 741 io.GetIoErrorHandler().Crash( 742 "SetConvert() called when not in an OPEN statement"); 743 } 744 if (auto convert{GetConvertFromString(keyword, length)}) { 745 open->set_convert(*convert); 746 return true; 747 } else { 748 open->SignalError(IostatErrorInKeyword, "Invalid CONVERT='%.*s'", 749 static_cast<int>(length), keyword); 750 return false; 751 } 752 } 753 754 bool IONAME(SetEncoding)( 755 Cookie cookie, const char *keyword, std::size_t length) { 756 IoStatementState &io{*cookie}; 757 auto *open{io.get_if<OpenStatementState>()}; 758 if (!open) { 759 io.GetIoErrorHandler().Crash( 760 "SetEncoding() called when not in an OPEN statement"); 761 } 762 bool isUTF8{false}; 763 static const char *keywords[]{"UTF-8", "DEFAULT", nullptr}; 764 switch (IdentifyValue(keyword, length, keywords)) { 765 case 0: 766 isUTF8 = true; 767 break; 768 case 1: 769 isUTF8 = false; 770 break; 771 default: 772 open->SignalError(IostatErrorInKeyword, "Invalid ENCODING='%.*s'", 773 static_cast<int>(length), keyword); 774 } 775 if (isUTF8 != open->unit().isUTF8) { 776 if (open->wasExtant()) { 777 open->SignalError("ENCODING= may not be changed on an open unit"); 778 } 779 open->unit().isUTF8 = isUTF8; 780 } 781 return true; 782 } 783 784 bool IONAME(SetForm)(Cookie cookie, const char *keyword, std::size_t length) { 785 IoStatementState &io{*cookie}; 786 auto *open{io.get_if<OpenStatementState>()}; 787 if (!open) { 788 io.GetIoErrorHandler().Crash( 789 "SetForm() called when not in an OPEN statement"); 790 } 791 static const char *keywords[]{"FORMATTED", "UNFORMATTED", nullptr}; 792 switch (IdentifyValue(keyword, length, keywords)) { 793 case 0: 794 open->set_isUnformatted(false); 795 break; 796 case 1: 797 open->set_isUnformatted(true); 798 break; 799 default: 800 open->SignalError(IostatErrorInKeyword, "Invalid FORM='%.*s'", 801 static_cast<int>(length), keyword); 802 } 803 return true; 804 } 805 806 bool IONAME(SetPosition)( 807 Cookie cookie, const char *keyword, std::size_t length) { 808 IoStatementState &io{*cookie}; 809 auto *open{io.get_if<OpenStatementState>()}; 810 if (!open) { 811 io.GetIoErrorHandler().Crash( 812 "SetPosition() called when not in an OPEN statement"); 813 } 814 static const char *positions[]{"ASIS", "REWIND", "APPEND", nullptr}; 815 switch (IdentifyValue(keyword, length, positions)) { 816 case 0: 817 open->set_position(Position::AsIs); 818 return true; 819 case 1: 820 open->set_position(Position::Rewind); 821 return true; 822 case 2: 823 open->set_position(Position::Append); 824 return true; 825 default: 826 io.GetIoErrorHandler().SignalError(IostatErrorInKeyword, 827 "Invalid POSITION='%.*s'", static_cast<int>(length), keyword); 828 } 829 return true; 830 } 831 832 bool IONAME(SetRecl)(Cookie cookie, std::size_t n) { 833 IoStatementState &io{*cookie}; 834 auto *open{io.get_if<OpenStatementState>()}; 835 if (!open) { 836 io.GetIoErrorHandler().Crash( 837 "SetRecl() called when not in an OPEN statement"); 838 } 839 if (n <= 0) { 840 io.GetIoErrorHandler().SignalError("RECL= must be greater than zero"); 841 return false; 842 } else if (open->wasExtant() && 843 open->unit().openRecl.value_or(0) != static_cast<std::int64_t>(n)) { 844 open->SignalError("RECL= may not be changed for an open unit"); 845 return false; 846 } else { 847 open->unit().openRecl = n; 848 return true; 849 } 850 } 851 852 bool IONAME(SetStatus)(Cookie cookie, const char *keyword, std::size_t length) { 853 IoStatementState &io{*cookie}; 854 if (auto *open{io.get_if<OpenStatementState>()}) { 855 static const char *statuses[]{ 856 "OLD", "NEW", "SCRATCH", "REPLACE", "UNKNOWN", nullptr}; 857 switch (IdentifyValue(keyword, length, statuses)) { 858 case 0: 859 open->set_status(OpenStatus::Old); 860 return true; 861 case 1: 862 open->set_status(OpenStatus::New); 863 return true; 864 case 2: 865 open->set_status(OpenStatus::Scratch); 866 return true; 867 case 3: 868 open->set_status(OpenStatus::Replace); 869 return true; 870 case 4: 871 open->set_status(OpenStatus::Unknown); 872 return true; 873 default: 874 io.GetIoErrorHandler().SignalError(IostatErrorInKeyword, 875 "Invalid STATUS='%.*s'", static_cast<int>(length), keyword); 876 } 877 return false; 878 } 879 if (auto *close{io.get_if<CloseStatementState>()}) { 880 static const char *statuses[]{"KEEP", "DELETE", nullptr}; 881 switch (IdentifyValue(keyword, length, statuses)) { 882 case 0: 883 close->set_status(CloseStatus::Keep); 884 return true; 885 case 1: 886 close->set_status(CloseStatus::Delete); 887 return true; 888 default: 889 io.GetIoErrorHandler().SignalError(IostatErrorInKeyword, 890 "Invalid STATUS='%.*s'", static_cast<int>(length), keyword); 891 } 892 return false; 893 } 894 if (io.get_if<NoopStatementState>()) { 895 return true; // don't bother validating STATUS= in a no-op CLOSE 896 } 897 io.GetIoErrorHandler().Crash( 898 "SetStatus() called when not in an OPEN or CLOSE statement"); 899 } 900 901 bool IONAME(SetFile)(Cookie cookie, const char *path, std::size_t chars) { 902 IoStatementState &io{*cookie}; 903 if (auto *open{io.get_if<OpenStatementState>()}) { 904 open->set_path(path, chars); 905 return true; 906 } 907 io.GetIoErrorHandler().Crash( 908 "SetFile() called when not in an OPEN statement"); 909 return false; 910 } 911 912 bool IONAME(GetNewUnit)(Cookie cookie, int &unit, int kind) { 913 IoStatementState &io{*cookie}; 914 auto *open{io.get_if<OpenStatementState>()}; 915 if (!open) { 916 io.GetIoErrorHandler().Crash( 917 "GetNewUnit() called when not in an OPEN statement"); 918 } 919 if (!SetInteger(unit, kind, open->unit().unitNumber())) { 920 open->SignalError("GetNewUnit(): Bad INTEGER kind(%d) for result"); 921 } 922 return true; 923 } 924 925 // Data transfers 926 927 bool IONAME(OutputDescriptor)(Cookie cookie, const Descriptor &descriptor) { 928 return descr::DescriptorIO<Direction::Output>(*cookie, descriptor); 929 } 930 931 bool IONAME(InputDescriptor)(Cookie cookie, const Descriptor &descriptor) { 932 return descr::DescriptorIO<Direction::Input>(*cookie, descriptor); 933 } 934 935 bool IONAME(OutputUnformattedBlock)(Cookie cookie, const char *x, 936 std::size_t length, std::size_t elementBytes) { 937 IoStatementState &io{*cookie}; 938 if (auto *unf{io.get_if< 939 ExternalUnformattedIoStatementState<Direction::Output>>()}) { 940 return unf->Emit(x, length, elementBytes); 941 } else if (auto *inq{io.get_if<InquireIOLengthState>()}) { 942 return inq->Emit(x, length, elementBytes); 943 } 944 io.GetIoErrorHandler().Crash("OutputUnformattedBlock() called for an I/O " 945 "statement that is not unformatted output"); 946 return false; 947 } 948 949 bool IONAME(InputUnformattedBlock)( 950 Cookie cookie, char *x, std::size_t length, std::size_t elementBytes) { 951 IoStatementState &io{*cookie}; 952 io.BeginReadingRecord(); 953 if (io.GetIoErrorHandler().InError()) { 954 return false; 955 } 956 if (auto *unf{ 957 io.get_if<ExternalUnformattedIoStatementState<Direction::Input>>()}) { 958 return unf->Receive(x, length, elementBytes); 959 } 960 io.GetIoErrorHandler().Crash("InputUnformattedBlock() called for an I/O " 961 "statement that is not unformatted output"); 962 return false; 963 } 964 965 bool IONAME(OutputInteger8)(Cookie cookie, std::int8_t n) { 966 cookie->CheckFormattedStmtType<Direction::Output>("OutputInteger8"); 967 StaticDescriptor staticDescriptor; 968 Descriptor &descriptor{staticDescriptor.descriptor()}; 969 descriptor.Establish( 970 TypeCategory::Integer, 1, reinterpret_cast<void *>(&n), 0); 971 return descr::DescriptorIO<Direction::Output>(*cookie, descriptor); 972 } 973 974 bool IONAME(OutputInteger16)(Cookie cookie, std::int16_t n) { 975 cookie->CheckFormattedStmtType<Direction::Output>("OutputInteger16"); 976 StaticDescriptor staticDescriptor; 977 Descriptor &descriptor{staticDescriptor.descriptor()}; 978 descriptor.Establish( 979 TypeCategory::Integer, 2, reinterpret_cast<void *>(&n), 0); 980 return descr::DescriptorIO<Direction::Output>(*cookie, descriptor); 981 } 982 983 bool IONAME(OutputInteger32)(Cookie cookie, std::int32_t n) { 984 cookie->CheckFormattedStmtType<Direction::Output>("OutputInteger32"); 985 StaticDescriptor staticDescriptor; 986 Descriptor &descriptor{staticDescriptor.descriptor()}; 987 descriptor.Establish( 988 TypeCategory::Integer, 4, reinterpret_cast<void *>(&n), 0); 989 return descr::DescriptorIO<Direction::Output>(*cookie, descriptor); 990 } 991 992 bool IONAME(OutputInteger64)(Cookie cookie, std::int64_t n) { 993 cookie->CheckFormattedStmtType<Direction::Output>("OutputInteger64"); 994 StaticDescriptor staticDescriptor; 995 Descriptor &descriptor{staticDescriptor.descriptor()}; 996 descriptor.Establish( 997 TypeCategory::Integer, 8, reinterpret_cast<void *>(&n), 0); 998 return descr::DescriptorIO<Direction::Output>(*cookie, descriptor); 999 } 1000 1001 #ifdef __SIZEOF_INT128__ 1002 bool IONAME(OutputInteger128)(Cookie cookie, common::int128_t n) { 1003 cookie->CheckFormattedStmtType<Direction::Output>("OutputInteger128"); 1004 StaticDescriptor staticDescriptor; 1005 Descriptor &descriptor{staticDescriptor.descriptor()}; 1006 descriptor.Establish( 1007 TypeCategory::Integer, 16, reinterpret_cast<void *>(&n), 0); 1008 return descr::DescriptorIO<Direction::Output>(*cookie, descriptor); 1009 } 1010 #endif 1011 1012 bool IONAME(InputInteger)(Cookie cookie, std::int64_t &n, int kind) { 1013 cookie->CheckFormattedStmtType<Direction::Input>("InputInteger"); 1014 StaticDescriptor staticDescriptor; 1015 Descriptor &descriptor{staticDescriptor.descriptor()}; 1016 descriptor.Establish( 1017 TypeCategory::Integer, kind, reinterpret_cast<void *>(&n), 0); 1018 return descr::DescriptorIO<Direction::Input>(*cookie, descriptor); 1019 } 1020 1021 bool IONAME(OutputReal32)(Cookie cookie, float x) { 1022 cookie->CheckFormattedStmtType<Direction::Output>("OutputReal32"); 1023 StaticDescriptor staticDescriptor; 1024 Descriptor &descriptor{staticDescriptor.descriptor()}; 1025 descriptor.Establish(TypeCategory::Real, 4, reinterpret_cast<void *>(&x), 0); 1026 return descr::DescriptorIO<Direction::Output>(*cookie, descriptor); 1027 } 1028 1029 bool IONAME(OutputReal64)(Cookie cookie, double x) { 1030 cookie->CheckFormattedStmtType<Direction::Output>("OutputReal64"); 1031 StaticDescriptor staticDescriptor; 1032 Descriptor &descriptor{staticDescriptor.descriptor()}; 1033 descriptor.Establish(TypeCategory::Real, 8, reinterpret_cast<void *>(&x), 0); 1034 return descr::DescriptorIO<Direction::Output>(*cookie, descriptor); 1035 } 1036 1037 bool IONAME(InputReal32)(Cookie cookie, float &x) { 1038 cookie->CheckFormattedStmtType<Direction::Input>("InputReal32"); 1039 StaticDescriptor staticDescriptor; 1040 Descriptor &descriptor{staticDescriptor.descriptor()}; 1041 descriptor.Establish(TypeCategory::Real, 4, reinterpret_cast<void *>(&x), 0); 1042 return descr::DescriptorIO<Direction::Input>(*cookie, descriptor); 1043 } 1044 1045 bool IONAME(InputReal64)(Cookie cookie, double &x) { 1046 cookie->CheckFormattedStmtType<Direction::Input>("InputReal64"); 1047 StaticDescriptor staticDescriptor; 1048 Descriptor &descriptor{staticDescriptor.descriptor()}; 1049 descriptor.Establish(TypeCategory::Real, 8, reinterpret_cast<void *>(&x), 0); 1050 return descr::DescriptorIO<Direction::Input>(*cookie, descriptor); 1051 } 1052 1053 bool IONAME(OutputComplex32)(Cookie cookie, float r, float i) { 1054 cookie->CheckFormattedStmtType<Direction::Output>("OutputComplex32"); 1055 float z[2]{r, i}; 1056 StaticDescriptor staticDescriptor; 1057 Descriptor &descriptor{staticDescriptor.descriptor()}; 1058 descriptor.Establish( 1059 TypeCategory::Complex, 4, reinterpret_cast<void *>(&z), 0); 1060 return descr::DescriptorIO<Direction::Output>(*cookie, descriptor); 1061 } 1062 1063 bool IONAME(OutputComplex64)(Cookie cookie, double r, double i) { 1064 cookie->CheckFormattedStmtType<Direction::Output>("OutputComplex64"); 1065 double z[2]{r, i}; 1066 StaticDescriptor staticDescriptor; 1067 Descriptor &descriptor{staticDescriptor.descriptor()}; 1068 descriptor.Establish( 1069 TypeCategory::Complex, 8, reinterpret_cast<void *>(&z), 0); 1070 return descr::DescriptorIO<Direction::Output>(*cookie, descriptor); 1071 } 1072 1073 bool IONAME(InputComplex32)(Cookie cookie, float z[2]) { 1074 cookie->CheckFormattedStmtType<Direction::Input>("InputComplex32"); 1075 StaticDescriptor staticDescriptor; 1076 Descriptor &descriptor{staticDescriptor.descriptor()}; 1077 descriptor.Establish( 1078 TypeCategory::Complex, 4, reinterpret_cast<void *>(z), 0); 1079 return descr::DescriptorIO<Direction::Input>(*cookie, descriptor); 1080 } 1081 1082 bool IONAME(InputComplex64)(Cookie cookie, double z[2]) { 1083 cookie->CheckFormattedStmtType<Direction::Input>("InputComplex64"); 1084 StaticDescriptor staticDescriptor; 1085 Descriptor &descriptor{staticDescriptor.descriptor()}; 1086 descriptor.Establish( 1087 TypeCategory::Complex, 8, reinterpret_cast<void *>(z), 0); 1088 return descr::DescriptorIO<Direction::Input>(*cookie, descriptor); 1089 } 1090 1091 bool IONAME(OutputCharacter)( 1092 Cookie cookie, const char *x, std::size_t length, int kind) { 1093 cookie->CheckFormattedStmtType<Direction::Output>("OutputCharacter"); 1094 StaticDescriptor staticDescriptor; 1095 Descriptor &descriptor{staticDescriptor.descriptor()}; 1096 descriptor.Establish( 1097 kind, length, reinterpret_cast<void *>(const_cast<char *>(x)), 0); 1098 return descr::DescriptorIO<Direction::Output>(*cookie, descriptor); 1099 } 1100 1101 bool IONAME(OutputAscii)(Cookie cookie, const char *x, std::size_t length) { 1102 return IONAME(OutputCharacter(cookie, x, length, 1)); 1103 } 1104 1105 bool IONAME(InputCharacter)( 1106 Cookie cookie, char *x, std::size_t length, int kind) { 1107 cookie->CheckFormattedStmtType<Direction::Input>("InputCharacter"); 1108 StaticDescriptor staticDescriptor; 1109 Descriptor &descriptor{staticDescriptor.descriptor()}; 1110 descriptor.Establish(kind, length, reinterpret_cast<void *>(x), 0); 1111 return descr::DescriptorIO<Direction::Input>(*cookie, descriptor); 1112 } 1113 1114 bool IONAME(InputAscii)(Cookie cookie, char *x, std::size_t length) { 1115 return IONAME(InputCharacter(cookie, x, length, 1)); 1116 } 1117 1118 bool IONAME(OutputLogical)(Cookie cookie, bool truth) { 1119 cookie->CheckFormattedStmtType<Direction::Output>("OutputLogical"); 1120 StaticDescriptor staticDescriptor; 1121 Descriptor &descriptor{staticDescriptor.descriptor()}; 1122 descriptor.Establish( 1123 TypeCategory::Logical, sizeof truth, reinterpret_cast<void *>(&truth), 0); 1124 return descr::DescriptorIO<Direction::Output>(*cookie, descriptor); 1125 } 1126 1127 bool IONAME(InputLogical)(Cookie cookie, bool &truth) { 1128 cookie->CheckFormattedStmtType<Direction::Input>("InputLogical"); 1129 StaticDescriptor staticDescriptor; 1130 Descriptor &descriptor{staticDescriptor.descriptor()}; 1131 descriptor.Establish( 1132 TypeCategory::Logical, sizeof truth, reinterpret_cast<void *>(&truth), 0); 1133 return descr::DescriptorIO<Direction::Input>(*cookie, descriptor); 1134 } 1135 1136 std::size_t IONAME(GetSize)(Cookie cookie) { 1137 IoStatementState &io{*cookie}; 1138 if (const auto *formatted{ 1139 io.get_if<FormattedIoStatementState<Direction::Input>>()}) { 1140 return formatted->GetEditDescriptorChars(); 1141 } 1142 io.GetIoErrorHandler().Crash( 1143 "GetIoSize() called for an I/O statement that is not a formatted READ()"); 1144 return 0; 1145 } 1146 1147 std::size_t IONAME(GetIoLength)(Cookie cookie) { 1148 IoStatementState &io{*cookie}; 1149 if (const auto *inq{io.get_if<InquireIOLengthState>()}) { 1150 return inq->bytes(); 1151 } 1152 io.GetIoErrorHandler().Crash("GetIoLength() called for an I/O statement that " 1153 "is not INQUIRE(IOLENGTH=)"); 1154 return 0; 1155 } 1156 1157 void IONAME(GetIoMsg)(Cookie cookie, char *msg, std::size_t length) { 1158 IoErrorHandler &handler{cookie->GetIoErrorHandler()}; 1159 if (handler.InError()) { // leave "msg" alone when no error 1160 handler.GetIoMsg(msg, length); 1161 } 1162 } 1163 1164 bool IONAME(InquireCharacter)(Cookie cookie, InquiryKeywordHash inquiry, 1165 char *result, std::size_t length) { 1166 IoStatementState &io{*cookie}; 1167 return io.Inquire(inquiry, result, length); 1168 } 1169 1170 bool IONAME(InquireLogical)( 1171 Cookie cookie, InquiryKeywordHash inquiry, bool &result) { 1172 IoStatementState &io{*cookie}; 1173 return io.Inquire(inquiry, result); 1174 } 1175 1176 bool IONAME(InquirePendingId)(Cookie cookie, std::int64_t id, bool &result) { 1177 IoStatementState &io{*cookie}; 1178 return io.Inquire(HashInquiryKeyword("PENDING"), id, result); 1179 } 1180 1181 bool IONAME(InquireInteger64)( 1182 Cookie cookie, InquiryKeywordHash inquiry, std::int64_t &result, int kind) { 1183 IoStatementState &io{*cookie}; 1184 std::int64_t n; 1185 if (io.Inquire(inquiry, n)) { 1186 SetInteger(result, kind, n); 1187 return true; 1188 } 1189 return false; 1190 } 1191 1192 enum Iostat IONAME(EndIoStatement)(Cookie cookie) { 1193 IoStatementState &io{*cookie}; 1194 return static_cast<enum Iostat>(io.EndIoStatement()); 1195 } 1196 } // namespace Fortran::runtime::io 1197