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 {
573       DoImpliedEndfile(handler);
574       if (frameOffsetInFile_ + recordOffsetInFrame_ > 0) {
575         --currentRecordNumber;
576         if (openRecl && access == Access::Direct) {
577           BackspaceFixedRecord(handler);
578         } else {
579           RUNTIME_CHECK(handler, isUnformatted.has_value());
580           if (isUnformatted.value_or(false)) {
581             BackspaceVariableUnformattedRecord(handler);
582           } else {
583             BackspaceVariableFormattedRecord(handler);
584           }
585         }
586       }
587     }
588     BeginRecord();
589   }
590 }
591 
592 void ExternalFileUnit::FlushOutput(IoErrorHandler &handler) {
593   if (!mayPosition()) {
594     auto frameAt{FrameAt()};
595     if (frameOffsetInFile_ >= frameAt &&
596         frameOffsetInFile_ <
597             static_cast<std::int64_t>(frameAt + FrameLength())) {
598       // A Flush() that's about to happen to a non-positionable file
599       // needs to advance frameOffsetInFile_ to prevent attempts at
600       // impossible seeks
601       CommitWrites();
602     }
603   }
604   Flush(handler);
605 }
606 
607 void ExternalFileUnit::FlushIfTerminal(IoErrorHandler &handler) {
608   if (isTerminal()) {
609     FlushOutput(handler);
610   }
611 }
612 
613 void ExternalFileUnit::Endfile(IoErrorHandler &handler) {
614   if (access == Access::Direct) {
615     handler.SignalError(IostatEndfileDirect,
616         "ENDFILE(UNIT=%d) on direct-access file", unitNumber());
617   } else if (!mayWrite()) {
618     handler.SignalError(IostatEndfileUnwritable,
619         "ENDFILE(UNIT=%d) on read-only file", unitNumber());
620   } else if (IsAfterEndfile()) {
621     // ENDFILE after ENDFILE
622   } else {
623     DoEndfile(handler);
624     if (IsRecordFile() && access != Access::Direct) {
625       // Explicit ENDFILE leaves position *after* the endfile record
626       RUNTIME_CHECK(handler, endfileRecordNumber.has_value());
627       currentRecordNumber = *endfileRecordNumber + 1;
628     }
629   }
630 }
631 
632 void ExternalFileUnit::Rewind(IoErrorHandler &handler) {
633   if (access == Access::Direct) {
634     handler.SignalError(IostatRewindNonSequential,
635         "REWIND(UNIT=%d) on non-sequential file", unitNumber());
636   } else {
637     SetPosition(0, handler);
638     currentRecordNumber = 1;
639   }
640 }
641 
642 void ExternalFileUnit::SetPosition(std::int64_t pos, IoErrorHandler &handler) {
643   DoImpliedEndfile(handler);
644   frameOffsetInFile_ = pos;
645   recordOffsetInFrame_ = 0;
646   if (access == Access::Direct) {
647     directAccessRecWasSet_ = true;
648   }
649   BeginRecord();
650 }
651 
652 void ExternalFileUnit::EndIoStatement() {
653   io_.reset();
654   u_.emplace<std::monostate>();
655   lock_.Drop();
656 }
657 
658 void ExternalFileUnit::BeginSequentialVariableUnformattedInputRecord(
659     IoErrorHandler &handler) {
660   std::int32_t header{0}, footer{0};
661   std::size_t need{recordOffsetInFrame_ + sizeof header};
662   std::size_t got{ReadFrame(frameOffsetInFile_, need, handler)};
663   // Try to emit informative errors to help debug corrupted files.
664   const char *error{nullptr};
665   if (got < need) {
666     if (got == recordOffsetInFrame_) {
667       handler.SignalEnd();
668     } else {
669       error = "Unformatted variable-length sequential file input failed at "
670               "record #%jd (file offset %jd): truncated record header";
671     }
672   } else {
673     std::memcpy(&header, Frame() + recordOffsetInFrame_, sizeof header);
674     recordLength = sizeof header + header; // does not include footer
675     need = recordOffsetInFrame_ + *recordLength + sizeof footer;
676     got = ReadFrame(frameOffsetInFile_, need, handler);
677     if (got < need) {
678       error = "Unformatted variable-length sequential file input failed at "
679               "record #%jd (file offset %jd): hit EOF reading record with "
680               "length %jd bytes";
681     } else {
682       std::memcpy(&footer, Frame() + recordOffsetInFrame_ + *recordLength,
683           sizeof footer);
684       if (footer != header) {
685         error = "Unformatted variable-length sequential file input failed at "
686                 "record #%jd (file offset %jd): record header has length %jd "
687                 "that does not match record footer (%jd)";
688       }
689     }
690   }
691   if (error) {
692     handler.SignalError(error, static_cast<std::intmax_t>(currentRecordNumber),
693         static_cast<std::intmax_t>(frameOffsetInFile_),
694         static_cast<std::intmax_t>(header), static_cast<std::intmax_t>(footer));
695     // TODO: error recovery
696   }
697   positionInRecord = sizeof header;
698 }
699 
700 void ExternalFileUnit::BeginVariableFormattedInputRecord(
701     IoErrorHandler &handler) {
702   if (this == defaultInput) {
703     if (defaultOutput) {
704       defaultOutput->FlushOutput(handler);
705     }
706     if (errorOutput) {
707       errorOutput->FlushOutput(handler);
708     }
709   }
710   std::size_t length{0};
711   do {
712     std::size_t need{length + 1};
713     length =
714         ReadFrame(frameOffsetInFile_, recordOffsetInFrame_ + need, handler) -
715         recordOffsetInFrame_;
716     if (length < need) {
717       if (length > 0) {
718         // final record w/o \n
719         recordLength = length;
720         unterminatedRecord = true;
721       } else {
722         handler.SignalEnd();
723       }
724       break;
725     }
726   } while (!SetVariableFormattedRecordLength());
727 }
728 
729 void ExternalFileUnit::BackspaceFixedRecord(IoErrorHandler &handler) {
730   RUNTIME_CHECK(handler, openRecl.has_value());
731   if (frameOffsetInFile_ < *openRecl) {
732     handler.SignalError(IostatBackspaceAtFirstRecord);
733   } else {
734     frameOffsetInFile_ -= *openRecl;
735   }
736 }
737 
738 void ExternalFileUnit::BackspaceVariableUnformattedRecord(
739     IoErrorHandler &handler) {
740   std::int32_t header{0}, footer{0};
741   auto headerBytes{static_cast<std::int64_t>(sizeof header)};
742   frameOffsetInFile_ += recordOffsetInFrame_;
743   recordOffsetInFrame_ = 0;
744   if (frameOffsetInFile_ <= headerBytes) {
745     handler.SignalError(IostatBackspaceAtFirstRecord);
746     return;
747   }
748   // Error conditions here cause crashes, not file format errors, because the
749   // validity of the file structure before the current record will have been
750   // checked informatively in NextSequentialVariableUnformattedInputRecord().
751   std::size_t got{
752       ReadFrame(frameOffsetInFile_ - headerBytes, headerBytes, handler)};
753   if (static_cast<std::int64_t>(got) < headerBytes) {
754     handler.SignalError(IostatShortRead);
755     return;
756   }
757   std::memcpy(&footer, Frame(), sizeof footer);
758   recordLength = footer;
759   if (frameOffsetInFile_ < *recordLength + 2 * headerBytes) {
760     handler.SignalError(IostatBadUnformattedRecord);
761     return;
762   }
763   frameOffsetInFile_ -= *recordLength + 2 * headerBytes;
764   if (frameOffsetInFile_ >= headerBytes) {
765     frameOffsetInFile_ -= headerBytes;
766     recordOffsetInFrame_ = headerBytes;
767   }
768   auto need{static_cast<std::size_t>(
769       recordOffsetInFrame_ + sizeof header + *recordLength)};
770   got = ReadFrame(frameOffsetInFile_, need, handler);
771   if (got < need) {
772     handler.SignalError(IostatShortRead);
773     return;
774   }
775   std::memcpy(&header, Frame() + recordOffsetInFrame_, sizeof header);
776   if (header != *recordLength) {
777     handler.SignalError(IostatBadUnformattedRecord);
778     return;
779   }
780 }
781 
782 // There's no portable memrchr(), unfortunately, and strrchr() would
783 // fail on a record with a NUL, so we have to do it the hard way.
784 static const char *FindLastNewline(const char *str, std::size_t length) {
785   for (const char *p{str + length}; p-- > str;) {
786     if (*p == '\n') {
787       return p;
788     }
789   }
790   return nullptr;
791 }
792 
793 void ExternalFileUnit::BackspaceVariableFormattedRecord(
794     IoErrorHandler &handler) {
795   // File offset of previous record's newline
796   auto prevNL{
797       frameOffsetInFile_ + static_cast<std::int64_t>(recordOffsetInFrame_) - 1};
798   if (prevNL < 0) {
799     handler.SignalError(IostatBackspaceAtFirstRecord);
800     return;
801   }
802   while (true) {
803     if (frameOffsetInFile_ < prevNL) {
804       if (const char *p{
805               FindLastNewline(Frame(), prevNL - 1 - frameOffsetInFile_)}) {
806         recordOffsetInFrame_ = p - Frame() + 1;
807         recordLength = prevNL - (frameOffsetInFile_ + recordOffsetInFrame_);
808         break;
809       }
810     }
811     if (frameOffsetInFile_ == 0) {
812       recordOffsetInFrame_ = 0;
813       recordLength = prevNL;
814       break;
815     }
816     frameOffsetInFile_ -= std::min<std::int64_t>(frameOffsetInFile_, 1024);
817     auto need{static_cast<std::size_t>(prevNL + 1 - frameOffsetInFile_)};
818     auto got{ReadFrame(frameOffsetInFile_, need, handler)};
819     if (got < need) {
820       handler.SignalError(IostatShortRead);
821       return;
822     }
823   }
824   if (Frame()[recordOffsetInFrame_ + *recordLength] != '\n') {
825     handler.SignalError(IostatMissingTerminator);
826     return;
827   }
828   if (*recordLength > 0 &&
829       Frame()[recordOffsetInFrame_ + *recordLength - 1] == '\r') {
830     --*recordLength;
831   }
832 }
833 
834 void ExternalFileUnit::DoImpliedEndfile(IoErrorHandler &handler) {
835   if (impliedEndfile_) {
836     impliedEndfile_ = false;
837     if (access != Access::Direct && IsRecordFile() && mayPosition()) {
838       DoEndfile(handler);
839     }
840   }
841 }
842 
843 void ExternalFileUnit::DoEndfile(IoErrorHandler &handler) {
844   if (IsRecordFile() && access != Access::Direct) {
845     if (furthestPositionInRecord > 0) {
846       // Last write was non-advancing, so AdvanceRecord() was not called.
847       leftTabLimit.reset();
848       ++currentRecordNumber;
849     }
850     endfileRecordNumber = currentRecordNumber;
851   }
852   FlushOutput(handler);
853   Truncate(frameOffsetInFile_ + recordOffsetInFrame_ + furthestPositionInRecord,
854       handler);
855   BeginRecord();
856   impliedEndfile_ = false;
857 }
858 
859 void ExternalFileUnit::CommitWrites() {
860   frameOffsetInFile_ +=
861       recordOffsetInFrame_ + recordLength.value_or(furthestPositionInRecord);
862   recordOffsetInFrame_ = 0;
863   BeginRecord();
864 }
865 
866 bool ExternalFileUnit::CheckDirectAccess(IoErrorHandler &handler) {
867   if (access == Access::Direct) {
868     RUNTIME_CHECK(handler, openRecl);
869     if (!directAccessRecWasSet_) {
870       handler.SignalError(
871           "No REC= was specified for a data transfer with ACCESS='DIRECT'");
872       return false;
873     }
874   }
875   return true;
876 }
877 
878 ChildIo &ExternalFileUnit::PushChildIo(IoStatementState &parent) {
879   OwningPtr<ChildIo> current{std::move(child_)};
880   Terminator &terminator{parent.GetIoErrorHandler()};
881   OwningPtr<ChildIo> next{New<ChildIo>{terminator}(parent, std::move(current))};
882   child_.reset(next.release());
883   return *child_;
884 }
885 
886 void ExternalFileUnit::PopChildIo(ChildIo &child) {
887   if (child_.get() != &child) {
888     child.parent().GetIoErrorHandler().Crash(
889         "ChildIo being popped is not top of stack");
890   }
891   child_.reset(child.AcquirePrevious().release()); // deletes top child
892 }
893 
894 void ChildIo::EndIoStatement() {
895   io_.reset();
896   u_.emplace<std::monostate>();
897 }
898 
899 Iostat ChildIo::CheckFormattingAndDirection(
900     bool unformatted, Direction direction) {
901   bool parentIsInput{!parent_.get_if<IoDirectionState<Direction::Output>>()};
902   bool parentIsFormatted{parentIsInput
903           ? parent_.get_if<FormattedIoStatementState<Direction::Input>>() !=
904               nullptr
905           : parent_.get_if<FormattedIoStatementState<Direction::Output>>() !=
906               nullptr};
907   bool parentIsUnformatted{!parentIsFormatted};
908   if (unformatted != parentIsUnformatted) {
909     return unformatted ? IostatUnformattedChildOnFormattedParent
910                        : IostatFormattedChildOnUnformattedParent;
911   } else if (parentIsInput != (direction == Direction::Input)) {
912     return parentIsInput ? IostatChildOutputToInputParent
913                          : IostatChildInputFromOutputParent;
914   } else {
915     return IostatOk;
916   }
917 }
918 
919 } // namespace Fortran::runtime::io
920