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