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   } else if (FrameLength() > recordOffsetInFrame_) {
345     const char *record{Frame() + recordOffsetInFrame_};
346     std::size_t bytes{FrameLength() - recordOffsetInFrame_};
347     if (const char *nl{
348             reinterpret_cast<const char *>(std::memchr(record, '\n', bytes))}) {
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 (FrameLength() > recordOffsetInFrame_ + *recordLength &&
414             Frame()[recordOffsetInFrame_ + *recordLength] == '\r') {
415           ++recordOffsetInFrame_;
416         }
417         if (FrameLength() >= recordOffsetInFrame_ &&
418             Frame()[recordOffsetInFrame_ + *recordLength] == '\n') {
419           ++recordOffsetInFrame_;
420         }
421         if (!resumptionRecordNumber || mayPosition()) {
422           frameOffsetInFile_ += recordOffsetInFrame_ + *recordLength;
423           recordOffsetInFrame_ = 0;
424         }
425         recordLength.reset();
426       }
427     }
428   }
429   ++currentRecordNumber;
430   BeginRecord();
431 }
432 
433 bool ExternalFileUnit::AdvanceRecord(IoErrorHandler &handler) {
434   if (direction_ == Direction::Input) {
435     FinishReadingRecord(handler);
436     return BeginReadingRecord(handler);
437   } else { // Direction::Output
438     bool ok{true};
439     RUNTIME_CHECK(handler, isUnformatted.has_value());
440     if (isFixedRecordLength && recordLength) {
441       // Pad remainder of fixed length record
442       if (furthestPositionInRecord < *recordLength) {
443         WriteFrame(
444             frameOffsetInFile_, recordOffsetInFrame_ + *recordLength, handler);
445         std::memset(Frame() + recordOffsetInFrame_ + furthestPositionInRecord,
446             isUnformatted.value_or(false) ? 0 : ' ',
447             *recordLength - furthestPositionInRecord);
448       }
449     } else {
450       positionInRecord = furthestPositionInRecord;
451       if (isUnformatted.value_or(false)) {
452         // Append the length of a sequential unformatted variable-length record
453         // as its footer, then overwrite the reserved first four bytes of the
454         // record with its length as its header.  These four bytes were skipped
455         // over in BeginUnformattedIO<Output>().
456         // TODO: Break very large records up into subrecords with negative
457         // headers &/or footers
458         std::uint32_t length;
459         length = furthestPositionInRecord - sizeof length;
460         ok = ok &&
461             Emit(reinterpret_cast<const char *>(&length), sizeof length,
462                 sizeof length, handler);
463         positionInRecord = 0;
464         ok = ok &&
465             Emit(reinterpret_cast<const char *>(&length), sizeof length,
466                 sizeof length, handler);
467       } else {
468         // Terminate formatted variable length record
469         ok = ok && Emit("\n", 1, 1, handler); // TODO: Windows CR+LF
470       }
471     }
472     CommitWrites();
473     impliedEndfile_ = true;
474     ++currentRecordNumber;
475     if (endfileRecordNumber && currentRecordNumber >= *endfileRecordNumber) {
476       endfileRecordNumber.reset();
477     }
478     return ok;
479   }
480 }
481 
482 void ExternalFileUnit::BackspaceRecord(IoErrorHandler &handler) {
483   if (access != Access::Sequential) {
484     handler.SignalError(IostatBackspaceNonSequential,
485         "BACKSPACE(UNIT=%d) on non-sequential file", unitNumber());
486   } else {
487     if (endfileRecordNumber && currentRecordNumber > *endfileRecordNumber) {
488       // BACKSPACE after explicit ENDFILE
489       currentRecordNumber = *endfileRecordNumber;
490     } else {
491       DoImpliedEndfile(handler);
492       if (frameOffsetInFile_ + recordOffsetInFrame_ > 0) {
493         --currentRecordNumber;
494         if (isFixedRecordLength) {
495           BackspaceFixedRecord(handler);
496         } else {
497           RUNTIME_CHECK(handler, isUnformatted.has_value());
498           if (isUnformatted.value_or(false)) {
499             BackspaceVariableUnformattedRecord(handler);
500           } else {
501             BackspaceVariableFormattedRecord(handler);
502           }
503         }
504       }
505     }
506     BeginRecord();
507   }
508 }
509 
510 void ExternalFileUnit::FlushOutput(IoErrorHandler &handler) {
511   if (!mayPosition()) {
512     auto frameAt{FrameAt()};
513     if (frameOffsetInFile_ >= frameAt &&
514         frameOffsetInFile_ <
515             static_cast<std::int64_t>(frameAt + FrameLength())) {
516       // A Flush() that's about to happen to a non-positionable file
517       // needs to advance frameOffsetInFile_ to prevent attempts at
518       // impossible seeks
519       CommitWrites();
520     }
521   }
522   Flush(handler);
523 }
524 
525 void ExternalFileUnit::FlushIfTerminal(IoErrorHandler &handler) {
526   if (isTerminal()) {
527     FlushOutput(handler);
528   }
529 }
530 
531 void ExternalFileUnit::Endfile(IoErrorHandler &handler) {
532   if (access != Access::Sequential) {
533     handler.SignalError(IostatEndfileNonSequential,
534         "ENDFILE(UNIT=%d) on non-sequential file", unitNumber());
535   } else if (!mayWrite()) {
536     handler.SignalError(IostatEndfileUnwritable,
537         "ENDFILE(UNIT=%d) on read-only file", unitNumber());
538   } else if (endfileRecordNumber &&
539       currentRecordNumber > *endfileRecordNumber) {
540     // ENDFILE after ENDFILE
541   } else {
542     DoEndfile(handler);
543     // Explicit ENDFILE leaves position *after* the endfile record
544     RUNTIME_CHECK(handler, endfileRecordNumber.has_value());
545     currentRecordNumber = *endfileRecordNumber + 1;
546   }
547 }
548 
549 void ExternalFileUnit::Rewind(IoErrorHandler &handler) {
550   if (access == Access::Direct) {
551     handler.SignalError(IostatRewindNonSequential,
552         "REWIND(UNIT=%d) on non-sequential file", unitNumber());
553   } else {
554     DoImpliedEndfile(handler);
555     SetPosition(0);
556     currentRecordNumber = 1;
557   }
558 }
559 
560 void ExternalFileUnit::EndIoStatement() {
561   io_.reset();
562   u_.emplace<std::monostate>();
563   lock_.Drop();
564 }
565 
566 void ExternalFileUnit::BeginSequentialVariableUnformattedInputRecord(
567     IoErrorHandler &handler) {
568   std::int32_t header{0}, footer{0};
569   std::size_t need{recordOffsetInFrame_ + sizeof header};
570   std::size_t got{ReadFrame(frameOffsetInFile_, need, handler)};
571   // Try to emit informative errors to help debug corrupted files.
572   const char *error{nullptr};
573   if (got < need) {
574     if (got == recordOffsetInFrame_) {
575       handler.SignalEnd();
576     } else {
577       error = "Unformatted variable-length sequential file input failed at "
578               "record #%jd (file offset %jd): truncated record header";
579     }
580   } else {
581     std::memcpy(&header, Frame() + recordOffsetInFrame_, sizeof header);
582     recordLength = sizeof header + header; // does not include footer
583     need = recordOffsetInFrame_ + *recordLength + sizeof footer;
584     got = ReadFrame(frameOffsetInFile_, need, handler);
585     if (got < need) {
586       error = "Unformatted variable-length sequential file input failed at "
587               "record #%jd (file offset %jd): hit EOF reading record with "
588               "length %jd bytes";
589     } else {
590       std::memcpy(&footer, Frame() + recordOffsetInFrame_ + *recordLength,
591           sizeof footer);
592       if (footer != header) {
593         error = "Unformatted variable-length sequential file input failed at "
594                 "record #%jd (file offset %jd): record header has length %jd "
595                 "that does not match record footer (%jd)";
596       }
597     }
598   }
599   if (error) {
600     handler.SignalError(error, static_cast<std::intmax_t>(currentRecordNumber),
601         static_cast<std::intmax_t>(frameOffsetInFile_),
602         static_cast<std::intmax_t>(header), static_cast<std::intmax_t>(footer));
603     // TODO: error recovery
604   }
605   positionInRecord = sizeof header;
606 }
607 
608 void ExternalFileUnit::BeginSequentialVariableFormattedInputRecord(
609     IoErrorHandler &handler) {
610   if (this == defaultInput && defaultOutput) {
611     defaultOutput->FlushOutput(handler);
612   }
613   std::size_t length{0};
614   do {
615     std::size_t need{length + 1};
616     length =
617         ReadFrame(frameOffsetInFile_, recordOffsetInFrame_ + need, handler) -
618         recordOffsetInFrame_;
619     if (length < need) {
620       if (length > 0) {
621         // final record w/o \n
622         recordLength = length;
623       } else {
624         handler.SignalEnd();
625       }
626       break;
627     }
628   } while (!SetSequentialVariableFormattedRecordLength());
629 }
630 
631 void ExternalFileUnit::BackspaceFixedRecord(IoErrorHandler &handler) {
632   RUNTIME_CHECK(handler, recordLength.has_value());
633   if (frameOffsetInFile_ < *recordLength) {
634     handler.SignalError(IostatBackspaceAtFirstRecord);
635   } else {
636     frameOffsetInFile_ -= *recordLength;
637   }
638 }
639 
640 void ExternalFileUnit::BackspaceVariableUnformattedRecord(
641     IoErrorHandler &handler) {
642   std::int32_t header{0}, footer{0};
643   auto headerBytes{static_cast<std::int64_t>(sizeof header)};
644   frameOffsetInFile_ += recordOffsetInFrame_;
645   recordOffsetInFrame_ = 0;
646   if (frameOffsetInFile_ <= headerBytes) {
647     handler.SignalError(IostatBackspaceAtFirstRecord);
648     return;
649   }
650   // Error conditions here cause crashes, not file format errors, because the
651   // validity of the file structure before the current record will have been
652   // checked informatively in NextSequentialVariableUnformattedInputRecord().
653   std::size_t got{
654       ReadFrame(frameOffsetInFile_ - headerBytes, headerBytes, handler)};
655   RUNTIME_CHECK(handler, got >= sizeof footer);
656   std::memcpy(&footer, Frame(), sizeof footer);
657   recordLength = footer;
658   RUNTIME_CHECK(handler, frameOffsetInFile_ >= *recordLength + 2 * headerBytes);
659   frameOffsetInFile_ -= *recordLength + 2 * headerBytes;
660   if (frameOffsetInFile_ >= headerBytes) {
661     frameOffsetInFile_ -= headerBytes;
662     recordOffsetInFrame_ = headerBytes;
663   }
664   auto need{static_cast<std::size_t>(
665       recordOffsetInFrame_ + sizeof header + *recordLength)};
666   got = ReadFrame(frameOffsetInFile_, need, handler);
667   RUNTIME_CHECK(handler, got >= need);
668   std::memcpy(&header, Frame() + recordOffsetInFrame_, sizeof header);
669   RUNTIME_CHECK(handler, header == *recordLength);
670 }
671 
672 // There's no portable memrchr(), unfortunately, and strrchr() would
673 // fail on a record with a NUL, so we have to do it the hard way.
674 static const char *FindLastNewline(const char *str, std::size_t length) {
675   for (const char *p{str + length}; p-- > str;) {
676     if (*p == '\n') {
677       return p;
678     }
679   }
680   return nullptr;
681 }
682 
683 void ExternalFileUnit::BackspaceVariableFormattedRecord(
684     IoErrorHandler &handler) {
685   // File offset of previous record's newline
686   auto prevNL{
687       frameOffsetInFile_ + static_cast<std::int64_t>(recordOffsetInFrame_) - 1};
688   if (prevNL < 0) {
689     handler.SignalError(IostatBackspaceAtFirstRecord);
690     return;
691   }
692   while (true) {
693     if (frameOffsetInFile_ < prevNL) {
694       if (const char *p{
695               FindLastNewline(Frame(), prevNL - 1 - frameOffsetInFile_)}) {
696         recordOffsetInFrame_ = p - Frame() + 1;
697         recordLength = prevNL - (frameOffsetInFile_ + recordOffsetInFrame_);
698         break;
699       }
700     }
701     if (frameOffsetInFile_ == 0) {
702       recordOffsetInFrame_ = 0;
703       recordLength = prevNL;
704       break;
705     }
706     frameOffsetInFile_ -= std::min<std::int64_t>(frameOffsetInFile_, 1024);
707     auto need{static_cast<std::size_t>(prevNL + 1 - frameOffsetInFile_)};
708     auto got{ReadFrame(frameOffsetInFile_, need, handler)};
709     RUNTIME_CHECK(handler, got >= need);
710   }
711   RUNTIME_CHECK(handler, Frame()[recordOffsetInFrame_ + *recordLength] == '\n');
712   if (*recordLength > 0 &&
713       Frame()[recordOffsetInFrame_ + *recordLength - 1] == '\r') {
714     --*recordLength;
715   }
716 }
717 
718 void ExternalFileUnit::DoImpliedEndfile(IoErrorHandler &handler) {
719   if (impliedEndfile_) {
720     impliedEndfile_ = false;
721     if (access == Access::Sequential && mayPosition()) {
722       DoEndfile(handler);
723     }
724   }
725 }
726 
727 void ExternalFileUnit::DoEndfile(IoErrorHandler &handler) {
728   endfileRecordNumber = currentRecordNumber;
729   Truncate(frameOffsetInFile_ + recordOffsetInFrame_, handler);
730   BeginRecord();
731   impliedEndfile_ = false;
732 }
733 
734 void ExternalFileUnit::CommitWrites() {
735   frameOffsetInFile_ +=
736       recordOffsetInFrame_ + recordLength.value_or(furthestPositionInRecord);
737   recordOffsetInFrame_ = 0;
738   BeginRecord();
739 }
740 
741 ChildIo &ExternalFileUnit::PushChildIo(IoStatementState &parent) {
742   OwningPtr<ChildIo> current{std::move(child_)};
743   Terminator &terminator{parent.GetIoErrorHandler()};
744   OwningPtr<ChildIo> next{New<ChildIo>{terminator}(parent, std::move(current))};
745   child_.reset(next.release());
746   return *child_;
747 }
748 
749 void ExternalFileUnit::PopChildIo(ChildIo &child) {
750   if (child_.get() != &child) {
751     child.parent().GetIoErrorHandler().Crash(
752         "ChildIo being popped is not top of stack");
753   }
754   child_.reset(child.AcquirePrevious().release()); // deletes top child
755 }
756 
757 void ChildIo::EndIoStatement() {
758   io_.reset();
759   u_.emplace<std::monostate>();
760 }
761 
762 bool ChildIo::CheckFormattingAndDirection(Terminator &terminator,
763     const char *what, bool unformatted, Direction direction) {
764   bool parentIsUnformatted{!parent_.get_if<FormattedIoStatementState>()};
765   bool parentIsInput{!parent_.get_if<IoDirectionState<Direction::Output>>()};
766   if (unformatted != parentIsUnformatted) {
767     terminator.Crash("Child %s attempted on %s parent I/O unit", what,
768         parentIsUnformatted ? "unformatted" : "formatted");
769     return false;
770   } else if (parentIsInput != (direction == Direction::Input)) {
771     terminator.Crash("Child %s attempted on %s parent I/O unit", what,
772         parentIsInput ? "input" : "output");
773     return false;
774   } else {
775     return true;
776   }
777 }
778 
779 } // namespace Fortran::runtime::io
780