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