1 //===-- runtime/unit.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 #include "unit.h"
10 #include "io-error.h"
11 #include "lock.h"
12 #include "unit-map.h"
13 #include <cstdio>
14 #include <limits>
15 #include <utility>
16 
17 namespace Fortran::runtime::io {
18 
19 // The per-unit data structures are created on demand so that Fortran I/O
20 // should work without a Fortran main program.
21 static Lock unitMapLock;
22 static UnitMap *unitMap{nullptr};
23 static ExternalFileUnit *defaultInput{nullptr}; // unit 5
24 static ExternalFileUnit *defaultOutput{nullptr}; // unit 6
25 static ExternalFileUnit *errorOutput{nullptr}; // unit 0 extension
26 
27 void FlushOutputOnCrash(const Terminator &terminator) {
28   if (!defaultOutput && !errorOutput) {
29     return;
30   }
31   IoErrorHandler handler{terminator};
32   handler.HasIoStat(); // prevent nested crash if flush has error
33   CriticalSection critical{unitMapLock};
34   if (defaultOutput) {
35     defaultOutput->FlushOutput(handler);
36   }
37   if (errorOutput) {
38     errorOutput->FlushOutput(handler);
39   }
40 }
41 
42 ExternalFileUnit *ExternalFileUnit::LookUp(int unit) {
43   return GetUnitMap().LookUp(unit);
44 }
45 
46 ExternalFileUnit &ExternalFileUnit::LookUpOrCrash(
47     int unit, const Terminator &terminator) {
48   ExternalFileUnit *file{LookUp(unit)};
49   if (!file) {
50     terminator.Crash("%d is not an open I/O unit number", unit);
51   }
52   return *file;
53 }
54 
55 ExternalFileUnit &ExternalFileUnit::LookUpOrCreate(
56     int unit, const Terminator &terminator, bool &wasExtant) {
57   return GetUnitMap().LookUpOrCreate(unit, terminator, wasExtant);
58 }
59 
60 ExternalFileUnit &ExternalFileUnit::LookUpOrCreateAnonymous(int unit,
61     Direction dir, std::optional<bool> isUnformatted,
62     const Terminator &terminator) {
63   bool exists{false};
64   ExternalFileUnit &result{
65       GetUnitMap().LookUpOrCreate(unit, terminator, exists)};
66   if (!exists) {
67     IoErrorHandler handler{terminator};
68     result.OpenAnonymousUnit(
69         dir == Direction::Input ? OpenStatus::Unknown : OpenStatus::Replace,
70         Action::ReadWrite, Position::Rewind, Convert::Native, handler);
71     result.isUnformatted = isUnformatted;
72   }
73   return result;
74 }
75 
76 ExternalFileUnit *ExternalFileUnit::LookUp(const char *path) {
77   return GetUnitMap().LookUp(path);
78 }
79 
80 ExternalFileUnit &ExternalFileUnit::CreateNew(
81     int unit, const Terminator &terminator) {
82   bool wasExtant{false};
83   ExternalFileUnit &result{
84       GetUnitMap().LookUpOrCreate(unit, terminator, wasExtant)};
85   RUNTIME_CHECK(terminator, !wasExtant);
86   return result;
87 }
88 
89 ExternalFileUnit *ExternalFileUnit::LookUpForClose(int unit) {
90   return GetUnitMap().LookUpForClose(unit);
91 }
92 
93 ExternalFileUnit &ExternalFileUnit::NewUnit(
94     const Terminator &terminator, bool forChildIo) {
95   ExternalFileUnit &unit{GetUnitMap().NewUnit(terminator)};
96   unit.createdForInternalChildIo_ = forChildIo;
97   return unit;
98 }
99 
100 void ExternalFileUnit::OpenUnit(std::optional<OpenStatus> status,
101     std::optional<Action> action, Position position, OwningPtr<char> &&newPath,
102     std::size_t newPathLength, Convert convert, IoErrorHandler &handler) {
103   if (executionEnvironment.conversion != Convert::Unknown) {
104     convert = executionEnvironment.conversion;
105   }
106   swapEndianness_ = convert == Convert::Swap ||
107       (convert == Convert::LittleEndian && !isHostLittleEndian) ||
108       (convert == Convert::BigEndian && isHostLittleEndian);
109   if (IsConnected()) {
110     bool isSamePath{newPath.get() && path() && pathLength() == newPathLength &&
111         std::memcmp(path(), newPath.get(), newPathLength) == 0};
112     if (status && *status != OpenStatus::Old && isSamePath) {
113       handler.SignalError("OPEN statement for connected unit may not have "
114                           "explicit STATUS= other than 'OLD'");
115       return;
116     }
117     if (!newPath.get() || isSamePath) {
118       // OPEN of existing unit, STATUS='OLD' or unspecified, not new FILE=
119       newPath.reset();
120       return;
121     }
122     // Otherwise, OPEN on open unit with new FILE= implies CLOSE
123     DoImpliedEndfile(handler);
124     FlushOutput(handler);
125     Close(CloseStatus::Keep, handler);
126   }
127   set_path(std::move(newPath), newPathLength);
128   Open(status.value_or(OpenStatus::Unknown), action, position, handler);
129   auto totalBytes{knownSize()};
130   if (access == Access::Direct) {
131     if (!openRecl) {
132       handler.SignalError(IostatOpenBadRecl,
133           "OPEN(UNIT=%d,ACCESS='DIRECT'): record length is not known",
134           unitNumber());
135     } else if (*openRecl <= 0) {
136       handler.SignalError(IostatOpenBadRecl,
137           "OPEN(UNIT=%d,ACCESS='DIRECT',RECL=%jd): record length is invalid",
138           unitNumber(), static_cast<std::intmax_t>(*openRecl));
139     } else if (totalBytes && (*totalBytes % *openRecl != 0)) {
140       handler.SignalError(IostatOpenBadAppend,
141           "OPEN(UNIT=%d,ACCESS='DIRECT',RECL=%jd): record length is not an "
142           "even divisor of the file size %jd",
143           unitNumber(), static_cast<std::intmax_t>(*openRecl),
144           static_cast<std::intmax_t>(*totalBytes));
145     }
146     recordLength = openRecl;
147   }
148   endfileRecordNumber.reset();
149   currentRecordNumber = 1;
150   if (totalBytes && access == Access::Direct && openRecl.value_or(0) > 0) {
151     endfileRecordNumber = 1 + (*totalBytes / *openRecl);
152   }
153   if (position == Position::Append && access != Access::Stream) {
154     if (!endfileRecordNumber) {
155       // Fake it so that we can backspace relative from the end
156       endfileRecordNumber = std::numeric_limits<std::int64_t>::max() - 2;
157     }
158     currentRecordNumber = *endfileRecordNumber;
159   }
160 }
161 
162 void ExternalFileUnit::OpenAnonymousUnit(std::optional<OpenStatus> status,
163     std::optional<Action> action, Position position, Convert convert,
164     IoErrorHandler &handler) {
165   // I/O to an unconnected unit reads/creates a local file, e.g. fort.7
166   std::size_t pathMaxLen{32};
167   auto path{SizedNew<char>{handler}(pathMaxLen)};
168   std::snprintf(path.get(), pathMaxLen, "fort.%d", unitNumber_);
169   OpenUnit(status, action, position, std::move(path), std::strlen(path.get()),
170       convert, handler);
171 }
172 
173 void ExternalFileUnit::CloseUnit(CloseStatus status, IoErrorHandler &handler) {
174   DoImpliedEndfile(handler);
175   FlushOutput(handler);
176   Close(status, handler);
177 }
178 
179 void ExternalFileUnit::DestroyClosed() {
180   GetUnitMap().DestroyClosed(*this); // destroys *this
181 }
182 
183 Iostat ExternalFileUnit::SetDirection(Direction direction) {
184   if (direction == Direction::Input) {
185     if (mayRead()) {
186       direction_ = Direction::Input;
187       return IostatOk;
188     } else {
189       return IostatReadFromWriteOnly;
190     }
191   } else {
192     if (mayWrite()) {
193       direction_ = Direction::Output;
194       return IostatOk;
195     } else {
196       return IostatWriteToReadOnly;
197     }
198   }
199 }
200 
201 UnitMap &ExternalFileUnit::GetUnitMap() {
202   if (unitMap) {
203     return *unitMap;
204   }
205   CriticalSection critical{unitMapLock};
206   if (unitMap) {
207     return *unitMap;
208   }
209   Terminator terminator{__FILE__, __LINE__};
210   IoErrorHandler handler{terminator};
211   UnitMap *newUnitMap{New<UnitMap>{terminator}().release()};
212 
213   bool wasExtant{false};
214   ExternalFileUnit &out{newUnitMap->LookUpOrCreate(6, terminator, wasExtant)};
215   RUNTIME_CHECK(terminator, !wasExtant);
216   out.Predefine(1);
217   handler.SignalError(out.SetDirection(Direction::Output));
218   out.isUnformatted = false;
219   defaultOutput = &out;
220 
221   ExternalFileUnit &in{newUnitMap->LookUpOrCreate(5, terminator, wasExtant)};
222   RUNTIME_CHECK(terminator, !wasExtant);
223   in.Predefine(0);
224   handler.SignalError(in.SetDirection(Direction::Input));
225   in.isUnformatted = false;
226   defaultInput = &in;
227 
228   ExternalFileUnit &error{newUnitMap->LookUpOrCreate(0, terminator, wasExtant)};
229   RUNTIME_CHECK(terminator, !wasExtant);
230   error.Predefine(2);
231   handler.SignalError(error.SetDirection(Direction::Output));
232   error.isUnformatted = false;
233   errorOutput = &error;
234 
235   unitMap = newUnitMap;
236   return *unitMap;
237 }
238 
239 void ExternalFileUnit::CloseAll(IoErrorHandler &handler) {
240   CriticalSection critical{unitMapLock};
241   if (unitMap) {
242     unitMap->CloseAll(handler);
243     FreeMemoryAndNullify(unitMap);
244   }
245   defaultOutput = nullptr;
246   defaultInput = nullptr;
247   errorOutput = nullptr;
248 }
249 
250 void ExternalFileUnit::FlushAll(IoErrorHandler &handler) {
251   CriticalSection critical{unitMapLock};
252   if (unitMap) {
253     unitMap->FlushAll(handler);
254   }
255 }
256 
257 static void SwapEndianness(
258     char *data, std::size_t bytes, std::size_t elementBytes) {
259   if (elementBytes > 1) {
260     auto half{elementBytes >> 1};
261     for (std::size_t j{0}; j + elementBytes <= bytes; j += elementBytes) {
262       for (std::size_t k{0}; k < half; ++k) {
263         std::swap(data[j + k], data[j + elementBytes - 1 - k]);
264       }
265     }
266   }
267 }
268 
269 bool ExternalFileUnit::Emit(const char *data, std::size_t bytes,
270     std::size_t elementBytes, IoErrorHandler &handler) {
271   auto furthestAfter{std::max(furthestPositionInRecord,
272       positionInRecord + static_cast<std::int64_t>(bytes))};
273   if (openRecl) {
274     // Check for fixed-length record overrun, but allow for
275     // sequential record termination.
276     int extra{0};
277     int header{0};
278     if (access == Access::Sequential) {
279       if (isUnformatted.value_or(false)) {
280         // record header + footer
281         header = static_cast<int>(sizeof(std::uint32_t));
282         extra = 2 * header;
283       } else {
284 #ifdef _WIN32
285         if (!isWindowsTextFile()) {
286           ++extra; // carriage return (CR)
287         }
288 #endif
289         ++extra; // newline (LF)
290       }
291     }
292     if (furthestAfter > extra + *openRecl) {
293       handler.SignalError(IostatRecordWriteOverrun,
294           "Attempt to write %zd bytes to position %jd in a fixed-size record "
295           "of %jd bytes",
296           bytes, static_cast<std::intmax_t>(positionInRecord - header),
297           static_cast<std::intmax_t>(*openRecl));
298       return false;
299     }
300   }
301   if (recordLength) {
302     // It is possible for recordLength to have a value now for a
303     // variable-length output record if the previous operation
304     // was a BACKSPACE or non advancing input statement.
305     recordLength.reset();
306     beganReadingRecord_ = false;
307   }
308   if (IsAfterEndfile()) {
309     handler.SignalError(IostatWriteAfterEndfile);
310     return false;
311   }
312   CheckDirectAccess(handler);
313   WriteFrame(frameOffsetInFile_, recordOffsetInFrame_ + furthestAfter, handler);
314   if (positionInRecord > furthestPositionInRecord) {
315     std::memset(Frame() + recordOffsetInFrame_ + furthestPositionInRecord, ' ',
316         positionInRecord - furthestPositionInRecord);
317   }
318   char *to{Frame() + recordOffsetInFrame_ + positionInRecord};
319   std::memcpy(to, data, bytes);
320   if (swapEndianness_) {
321     SwapEndianness(to, bytes, elementBytes);
322   }
323   positionInRecord += bytes;
324   furthestPositionInRecord = furthestAfter;
325   return true;
326 }
327 
328 bool ExternalFileUnit::Receive(char *data, std::size_t bytes,
329     std::size_t elementBytes, IoErrorHandler &handler) {
330   RUNTIME_CHECK(handler, direction_ == Direction::Input);
331   auto furthestAfter{std::max(furthestPositionInRecord,
332       positionInRecord + static_cast<std::int64_t>(bytes))};
333   if (furthestAfter > recordLength.value_or(furthestAfter)) {
334     handler.SignalError(IostatRecordReadOverrun,
335         "Attempt to read %zd bytes at position %jd in a record of %jd bytes",
336         bytes, static_cast<std::intmax_t>(positionInRecord),
337         static_cast<std::intmax_t>(*recordLength));
338     return false;
339   }
340   auto need{recordOffsetInFrame_ + furthestAfter};
341   auto got{ReadFrame(frameOffsetInFile_, need, handler)};
342   if (got >= need) {
343     std::memcpy(data, Frame() + recordOffsetInFrame_ + positionInRecord, bytes);
344     if (swapEndianness_) {
345       SwapEndianness(data, bytes, elementBytes);
346     }
347     positionInRecord += bytes;
348     furthestPositionInRecord = furthestAfter;
349     return true;
350   } else {
351     handler.SignalEnd();
352     if (IsRecordFile() && access != Access::Direct) {
353       endfileRecordNumber = currentRecordNumber;
354     }
355     return false;
356   }
357 }
358 
359 std::size_t ExternalFileUnit::GetNextInputBytes(
360     const char *&p, IoErrorHandler &handler) {
361   RUNTIME_CHECK(handler, direction_ == Direction::Input);
362   std::size_t length{1};
363   if (auto recl{EffectiveRecordLength()}) {
364     if (positionInRecord < *recl) {
365       length = *recl - positionInRecord;
366     } else {
367       p = nullptr;
368       return 0;
369     }
370   }
371   p = FrameNextInput(handler, length);
372   return p ? length : 0;
373 }
374 
375 const char *ExternalFileUnit::FrameNextInput(
376     IoErrorHandler &handler, std::size_t bytes) {
377   RUNTIME_CHECK(handler, isUnformatted.has_value() && !*isUnformatted);
378   if (static_cast<std::int64_t>(positionInRecord + bytes) <=
379       recordLength.value_or(positionInRecord + bytes)) {
380     auto at{recordOffsetInFrame_ + positionInRecord};
381     auto need{static_cast<std::size_t>(at + bytes)};
382     auto got{ReadFrame(frameOffsetInFile_, need, handler)};
383     SetVariableFormattedRecordLength();
384     if (got >= need) {
385       return Frame() + at;
386     }
387     handler.SignalEnd();
388     if (IsRecordFile() && access != Access::Direct) {
389       endfileRecordNumber = currentRecordNumber;
390     }
391   }
392   return nullptr;
393 }
394 
395 bool ExternalFileUnit::SetVariableFormattedRecordLength() {
396   if (recordLength || access == Access::Direct) {
397     return true;
398   } else if (FrameLength() > recordOffsetInFrame_) {
399     const char *record{Frame() + recordOffsetInFrame_};
400     std::size_t bytes{FrameLength() - recordOffsetInFrame_};
401     if (const char *nl{
402             reinterpret_cast<const char *>(std::memchr(record, '\n', bytes))}) {
403       recordLength = nl - record;
404       if (*recordLength > 0 && record[*recordLength - 1] == '\r') {
405         --*recordLength;
406       }
407       return true;
408     }
409   }
410   return false;
411 }
412 
413 bool ExternalFileUnit::BeginReadingRecord(IoErrorHandler &handler) {
414   RUNTIME_CHECK(handler, direction_ == Direction::Input);
415   if (!beganReadingRecord_) {
416     beganReadingRecord_ = true;
417     if (access == Access::Direct) {
418       CheckDirectAccess(handler);
419       auto need{static_cast<std::size_t>(recordOffsetInFrame_ + *openRecl)};
420       auto got{ReadFrame(frameOffsetInFile_, need, handler)};
421       if (got >= need) {
422         recordLength = openRecl;
423       } else {
424         recordLength.reset();
425         handler.SignalEnd();
426       }
427     } else {
428       recordLength.reset();
429       if (IsAtEOF()) {
430         handler.SignalEnd();
431       } else {
432         RUNTIME_CHECK(handler, isUnformatted.has_value());
433         if (*isUnformatted) {
434           if (access == Access::Sequential) {
435             BeginSequentialVariableUnformattedInputRecord(handler);
436           }
437         } else { // formatted sequential or stream
438           BeginVariableFormattedInputRecord(handler);
439         }
440       }
441     }
442   }
443   RUNTIME_CHECK(handler,
444       recordLength.has_value() || !IsRecordFile() || handler.InError());
445   return !handler.InError();
446 }
447 
448 void ExternalFileUnit::FinishReadingRecord(IoErrorHandler &handler) {
449   RUNTIME_CHECK(handler, direction_ == Direction::Input && beganReadingRecord_);
450   beganReadingRecord_ = false;
451   if (handler.InError() && handler.GetIoStat() != IostatEor) {
452     // Avoid bogus crashes in END/ERR circumstances; but
453     // still increment the current record number so that
454     // an attempted read of an endfile record, followed by
455     // a BACKSPACE, will still be at EOF.
456     ++currentRecordNumber;
457   } else if (IsRecordFile()) {
458     RUNTIME_CHECK(handler, recordLength.has_value());
459     recordOffsetInFrame_ += *recordLength;
460     if (access != Access::Direct) {
461       RUNTIME_CHECK(handler, isUnformatted.has_value());
462       recordLength.reset();
463       if (isUnformatted.value_or(false)) {
464         // Retain footer in frame for more efficient BACKSPACE
465         frameOffsetInFile_ += recordOffsetInFrame_;
466         recordOffsetInFrame_ = sizeof(std::uint32_t);
467       } else { // formatted
468         if (FrameLength() > recordOffsetInFrame_ &&
469             Frame()[recordOffsetInFrame_] == '\r') {
470           ++recordOffsetInFrame_;
471         }
472         if (FrameLength() > recordOffsetInFrame_ &&
473             Frame()[recordOffsetInFrame_] == '\n') {
474           ++recordOffsetInFrame_;
475         }
476         if (!pinnedFrame || mayPosition()) {
477           frameOffsetInFile_ += recordOffsetInFrame_;
478           recordOffsetInFrame_ = 0;
479         }
480       }
481     }
482     ++currentRecordNumber;
483   } else { // unformatted stream
484     furthestPositionInRecord =
485         std::max(furthestPositionInRecord, positionInRecord);
486     frameOffsetInFile_ += recordOffsetInFrame_ + furthestPositionInRecord;
487   }
488   BeginRecord();
489 }
490 
491 bool ExternalFileUnit::AdvanceRecord(IoErrorHandler &handler) {
492   if (direction_ == Direction::Input) {
493     FinishReadingRecord(handler);
494     return BeginReadingRecord(handler);
495   } else { // Direction::Output
496     bool ok{true};
497     RUNTIME_CHECK(handler, isUnformatted.has_value());
498     positionInRecord = furthestPositionInRecord;
499     if (access == Access::Direct) {
500       if (furthestPositionInRecord <
501           openRecl.value_or(furthestPositionInRecord)) {
502         // Pad remainder of fixed length record
503         WriteFrame(
504             frameOffsetInFile_, recordOffsetInFrame_ + *openRecl, handler);
505         std::memset(Frame() + recordOffsetInFrame_ + furthestPositionInRecord,
506             isUnformatted.value_or(false) ? 0 : ' ',
507             *openRecl - furthestPositionInRecord);
508         furthestPositionInRecord = *openRecl;
509       }
510     } else if (*isUnformatted) {
511       if (access == Access::Sequential) {
512         // Append the length of a sequential unformatted variable-length record
513         // as its footer, then overwrite the reserved first four bytes of the
514         // record with its length as its header.  These four bytes were skipped
515         // over in BeginUnformattedIO<Output>().
516         // TODO: Break very large records up into subrecords with negative
517         // headers &/or footers
518         std::uint32_t length;
519         length = furthestPositionInRecord - sizeof length;
520         ok = ok &&
521             Emit(reinterpret_cast<const char *>(&length), sizeof length,
522                 sizeof length, handler);
523         positionInRecord = 0;
524         ok = ok &&
525             Emit(reinterpret_cast<const char *>(&length), sizeof length,
526                 sizeof length, handler);
527       } else {
528         // Unformatted stream: nothing to do
529       }
530     } else if (handler.GetIoStat() != IostatOk &&
531         furthestPositionInRecord == 0) {
532       // Error in formatted variable length record, and no output yet; do
533       // nothing, like most other Fortran compilers do.
534       return true;
535     } else {
536       // Terminate formatted variable length record
537       const char *lineEnding{"\n"};
538       std::size_t lineEndingBytes{1};
539 #ifdef _WIN32
540       if (!isWindowsTextFile()) {
541         lineEnding = "\r\n";
542         lineEndingBytes = 2;
543       }
544 #endif
545       ok = ok && Emit(lineEnding, lineEndingBytes, 1, handler);
546     }
547     leftTabLimit.reset();
548     if (IsAfterEndfile()) {
549       return false;
550     }
551     CommitWrites();
552     ++currentRecordNumber;
553     if (access != Access::Direct) {
554       impliedEndfile_ = IsRecordFile();
555       if (IsAtEOF()) {
556         endfileRecordNumber.reset();
557       }
558     }
559     return ok;
560   }
561 }
562 
563 void ExternalFileUnit::BackspaceRecord(IoErrorHandler &handler) {
564   if (access == Access::Direct || !IsRecordFile()) {
565     handler.SignalError(IostatBackspaceNonSequential,
566         "BACKSPACE(UNIT=%d) on direct-access file or unformatted stream",
567         unitNumber());
568   } else {
569     if (IsAfterEndfile()) {
570       // BACKSPACE after explicit ENDFILE
571       currentRecordNumber = *endfileRecordNumber;
572     } else if (leftTabLimit) {
573       // BACKSPACE after non-advancing I/O
574       leftTabLimit.reset();
575     } else {
576       DoImpliedEndfile(handler);
577       if (frameOffsetInFile_ + recordOffsetInFrame_ > 0) {
578         --currentRecordNumber;
579         if (openRecl && access == Access::Direct) {
580           BackspaceFixedRecord(handler);
581         } else {
582           RUNTIME_CHECK(handler, isUnformatted.has_value());
583           if (isUnformatted.value_or(false)) {
584             BackspaceVariableUnformattedRecord(handler);
585           } else {
586             BackspaceVariableFormattedRecord(handler);
587           }
588         }
589       }
590     }
591     BeginRecord();
592   }
593 }
594 
595 void ExternalFileUnit::FlushOutput(IoErrorHandler &handler) {
596   if (!mayPosition()) {
597     auto frameAt{FrameAt()};
598     if (frameOffsetInFile_ >= frameAt &&
599         frameOffsetInFile_ <
600             static_cast<std::int64_t>(frameAt + FrameLength())) {
601       // A Flush() that's about to happen to a non-positionable file
602       // needs to advance frameOffsetInFile_ to prevent attempts at
603       // impossible seeks
604       CommitWrites();
605     }
606   }
607   Flush(handler);
608 }
609 
610 void ExternalFileUnit::FlushIfTerminal(IoErrorHandler &handler) {
611   if (isTerminal()) {
612     FlushOutput(handler);
613   }
614 }
615 
616 void ExternalFileUnit::Endfile(IoErrorHandler &handler) {
617   if (access == Access::Direct) {
618     handler.SignalError(IostatEndfileDirect,
619         "ENDFILE(UNIT=%d) on direct-access file", unitNumber());
620   } else if (!mayWrite()) {
621     handler.SignalError(IostatEndfileUnwritable,
622         "ENDFILE(UNIT=%d) on read-only file", unitNumber());
623   } else if (IsAfterEndfile()) {
624     // ENDFILE after ENDFILE
625   } else {
626     DoEndfile(handler);
627     if (IsRecordFile() && access != Access::Direct) {
628       // Explicit ENDFILE leaves position *after* the endfile record
629       RUNTIME_CHECK(handler, endfileRecordNumber.has_value());
630       currentRecordNumber = *endfileRecordNumber + 1;
631     }
632   }
633 }
634 
635 void ExternalFileUnit::Rewind(IoErrorHandler &handler) {
636   if (access == Access::Direct) {
637     handler.SignalError(IostatRewindNonSequential,
638         "REWIND(UNIT=%d) on non-sequential file", unitNumber());
639   } else {
640     SetPosition(0, handler);
641     currentRecordNumber = 1;
642   }
643 }
644 
645 void ExternalFileUnit::SetPosition(std::int64_t pos, IoErrorHandler &handler) {
646   DoImpliedEndfile(handler);
647   frameOffsetInFile_ = pos;
648   recordOffsetInFrame_ = 0;
649   if (access == Access::Direct) {
650     directAccessRecWasSet_ = true;
651   }
652   BeginRecord();
653 }
654 
655 void ExternalFileUnit::EndIoStatement() {
656   io_.reset();
657   u_.emplace<std::monostate>();
658   lock_.Drop();
659 }
660 
661 void ExternalFileUnit::BeginSequentialVariableUnformattedInputRecord(
662     IoErrorHandler &handler) {
663   std::int32_t header{0}, footer{0};
664   std::size_t need{recordOffsetInFrame_ + sizeof header};
665   std::size_t got{ReadFrame(frameOffsetInFile_, need, handler)};
666   // Try to emit informative errors to help debug corrupted files.
667   const char *error{nullptr};
668   if (got < need) {
669     if (got == recordOffsetInFrame_) {
670       handler.SignalEnd();
671     } else {
672       error = "Unformatted variable-length sequential file input failed at "
673               "record #%jd (file offset %jd): truncated record header";
674     }
675   } else {
676     std::memcpy(&header, Frame() + recordOffsetInFrame_, sizeof header);
677     recordLength = sizeof header + header; // does not include footer
678     need = recordOffsetInFrame_ + *recordLength + sizeof footer;
679     got = ReadFrame(frameOffsetInFile_, need, handler);
680     if (got < need) {
681       error = "Unformatted variable-length sequential file input failed at "
682               "record #%jd (file offset %jd): hit EOF reading record with "
683               "length %jd bytes";
684     } else {
685       std::memcpy(&footer, Frame() + recordOffsetInFrame_ + *recordLength,
686           sizeof footer);
687       if (footer != header) {
688         error = "Unformatted variable-length sequential file input failed at "
689                 "record #%jd (file offset %jd): record header has length %jd "
690                 "that does not match record footer (%jd)";
691       }
692     }
693   }
694   if (error) {
695     handler.SignalError(error, static_cast<std::intmax_t>(currentRecordNumber),
696         static_cast<std::intmax_t>(frameOffsetInFile_),
697         static_cast<std::intmax_t>(header), static_cast<std::intmax_t>(footer));
698     // TODO: error recovery
699   }
700   positionInRecord = sizeof header;
701 }
702 
703 void ExternalFileUnit::BeginVariableFormattedInputRecord(
704     IoErrorHandler &handler) {
705   if (this == defaultInput) {
706     if (defaultOutput) {
707       defaultOutput->FlushOutput(handler);
708     }
709     if (errorOutput) {
710       errorOutput->FlushOutput(handler);
711     }
712   }
713   std::size_t length{0};
714   do {
715     std::size_t need{length + 1};
716     length =
717         ReadFrame(frameOffsetInFile_, recordOffsetInFrame_ + need, handler) -
718         recordOffsetInFrame_;
719     if (length < need) {
720       if (length > 0) {
721         // final record w/o \n
722         recordLength = length;
723         unterminatedRecord = true;
724       } else {
725         handler.SignalEnd();
726       }
727       break;
728     }
729   } while (!SetVariableFormattedRecordLength());
730 }
731 
732 void ExternalFileUnit::BackspaceFixedRecord(IoErrorHandler &handler) {
733   RUNTIME_CHECK(handler, openRecl.has_value());
734   if (frameOffsetInFile_ < *openRecl) {
735     handler.SignalError(IostatBackspaceAtFirstRecord);
736   } else {
737     frameOffsetInFile_ -= *openRecl;
738   }
739 }
740 
741 void ExternalFileUnit::BackspaceVariableUnformattedRecord(
742     IoErrorHandler &handler) {
743   std::int32_t header{0}, footer{0};
744   auto headerBytes{static_cast<std::int64_t>(sizeof header)};
745   frameOffsetInFile_ += recordOffsetInFrame_;
746   recordOffsetInFrame_ = 0;
747   if (frameOffsetInFile_ <= headerBytes) {
748     handler.SignalError(IostatBackspaceAtFirstRecord);
749     return;
750   }
751   // Error conditions here cause crashes, not file format errors, because the
752   // validity of the file structure before the current record will have been
753   // checked informatively in NextSequentialVariableUnformattedInputRecord().
754   std::size_t got{
755       ReadFrame(frameOffsetInFile_ - headerBytes, headerBytes, handler)};
756   if (static_cast<std::int64_t>(got) < headerBytes) {
757     handler.SignalError(IostatShortRead);
758     return;
759   }
760   std::memcpy(&footer, Frame(), sizeof footer);
761   recordLength = footer;
762   if (frameOffsetInFile_ < *recordLength + 2 * headerBytes) {
763     handler.SignalError(IostatBadUnformattedRecord);
764     return;
765   }
766   frameOffsetInFile_ -= *recordLength + 2 * headerBytes;
767   if (frameOffsetInFile_ >= headerBytes) {
768     frameOffsetInFile_ -= headerBytes;
769     recordOffsetInFrame_ = headerBytes;
770   }
771   auto need{static_cast<std::size_t>(
772       recordOffsetInFrame_ + sizeof header + *recordLength)};
773   got = ReadFrame(frameOffsetInFile_, need, handler);
774   if (got < need) {
775     handler.SignalError(IostatShortRead);
776     return;
777   }
778   std::memcpy(&header, Frame() + recordOffsetInFrame_, sizeof header);
779   if (header != *recordLength) {
780     handler.SignalError(IostatBadUnformattedRecord);
781     return;
782   }
783 }
784 
785 // There's no portable memrchr(), unfortunately, and strrchr() would
786 // fail on a record with a NUL, so we have to do it the hard way.
787 static const char *FindLastNewline(const char *str, std::size_t length) {
788   for (const char *p{str + length}; p-- > str;) {
789     if (*p == '\n') {
790       return p;
791     }
792   }
793   return nullptr;
794 }
795 
796 void ExternalFileUnit::BackspaceVariableFormattedRecord(
797     IoErrorHandler &handler) {
798   // File offset of previous record's newline
799   auto prevNL{
800       frameOffsetInFile_ + static_cast<std::int64_t>(recordOffsetInFrame_) - 1};
801   if (prevNL < 0) {
802     handler.SignalError(IostatBackspaceAtFirstRecord);
803     return;
804   }
805   while (true) {
806     if (frameOffsetInFile_ < prevNL) {
807       if (const char *p{
808               FindLastNewline(Frame(), prevNL - 1 - frameOffsetInFile_)}) {
809         recordOffsetInFrame_ = p - Frame() + 1;
810         recordLength = prevNL - (frameOffsetInFile_ + recordOffsetInFrame_);
811         break;
812       }
813     }
814     if (frameOffsetInFile_ == 0) {
815       recordOffsetInFrame_ = 0;
816       recordLength = prevNL;
817       break;
818     }
819     frameOffsetInFile_ -= std::min<std::int64_t>(frameOffsetInFile_, 1024);
820     auto need{static_cast<std::size_t>(prevNL + 1 - frameOffsetInFile_)};
821     auto got{ReadFrame(frameOffsetInFile_, need, handler)};
822     if (got < need) {
823       handler.SignalError(IostatShortRead);
824       return;
825     }
826   }
827   if (Frame()[recordOffsetInFrame_ + *recordLength] != '\n') {
828     handler.SignalError(IostatMissingTerminator);
829     return;
830   }
831   if (*recordLength > 0 &&
832       Frame()[recordOffsetInFrame_ + *recordLength - 1] == '\r') {
833     --*recordLength;
834   }
835 }
836 
837 void ExternalFileUnit::DoImpliedEndfile(IoErrorHandler &handler) {
838   if (impliedEndfile_) {
839     impliedEndfile_ = false;
840     if (access != Access::Direct && IsRecordFile() && mayPosition()) {
841       DoEndfile(handler);
842     }
843   }
844 }
845 
846 void ExternalFileUnit::DoEndfile(IoErrorHandler &handler) {
847   if (IsRecordFile() && access != Access::Direct) {
848     if (furthestPositionInRecord > 0) {
849       // Last write was non-advancing, so AdvanceRecord() was not called.
850       leftTabLimit.reset();
851       ++currentRecordNumber;
852     }
853     endfileRecordNumber = currentRecordNumber;
854   }
855   FlushOutput(handler);
856   Truncate(frameOffsetInFile_ + recordOffsetInFrame_ + furthestPositionInRecord,
857       handler);
858   BeginRecord();
859   impliedEndfile_ = false;
860 }
861 
862 void ExternalFileUnit::CommitWrites() {
863   frameOffsetInFile_ +=
864       recordOffsetInFrame_ + recordLength.value_or(furthestPositionInRecord);
865   recordOffsetInFrame_ = 0;
866   BeginRecord();
867 }
868 
869 bool ExternalFileUnit::CheckDirectAccess(IoErrorHandler &handler) {
870   if (access == Access::Direct) {
871     RUNTIME_CHECK(handler, openRecl);
872     if (!directAccessRecWasSet_) {
873       handler.SignalError(
874           "No REC= was specified for a data transfer with ACCESS='DIRECT'");
875       return false;
876     }
877   }
878   return true;
879 }
880 
881 ChildIo &ExternalFileUnit::PushChildIo(IoStatementState &parent) {
882   OwningPtr<ChildIo> current{std::move(child_)};
883   Terminator &terminator{parent.GetIoErrorHandler()};
884   OwningPtr<ChildIo> next{New<ChildIo>{terminator}(parent, std::move(current))};
885   child_.reset(next.release());
886   return *child_;
887 }
888 
889 void ExternalFileUnit::PopChildIo(ChildIo &child) {
890   if (child_.get() != &child) {
891     child.parent().GetIoErrorHandler().Crash(
892         "ChildIo being popped is not top of stack");
893   }
894   child_.reset(child.AcquirePrevious().release()); // deletes top child
895 }
896 
897 void ChildIo::EndIoStatement() {
898   io_.reset();
899   u_.emplace<std::monostate>();
900 }
901 
902 Iostat ChildIo::CheckFormattingAndDirection(
903     bool unformatted, Direction direction) {
904   bool parentIsInput{!parent_.get_if<IoDirectionState<Direction::Output>>()};
905   bool parentIsFormatted{parentIsInput
906           ? parent_.get_if<FormattedIoStatementState<Direction::Input>>() !=
907               nullptr
908           : parent_.get_if<FormattedIoStatementState<Direction::Output>>() !=
909               nullptr};
910   bool parentIsUnformatted{!parentIsFormatted};
911   if (unformatted != parentIsUnformatted) {
912     return unformatted ? IostatUnformattedChildOnFormattedParent
913                        : IostatFormattedChildOnUnformattedParent;
914   } else if (parentIsInput != (direction == Direction::Input)) {
915     return parentIsInput ? IostatChildOutputToInputParent
916                          : IostatChildInputFromOutputParent;
917   } else {
918     return IostatOk;
919   }
920 }
921 
922 } // namespace Fortran::runtime::io
923