retex-engine 1.0.0-rc.2

The TeX typesetting engine for retex
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
//! Files: `\input`, the log file, `\openin`/`\read`, `\scantokens`
//! pseudo-files and the record of files read and written that the pass
//! driver uses (*TeX: The Program* §511–§538, §480–§486; e-TeX).

use std::collections::{HashMap, VecDeque};
use std::path::{Path, PathBuf};
use std::rc::Rc;

use crate::Engine;
use crate::cmd::Cmd;
use crate::input::{Level, LineSource, ScannerStatus};
use crate::primitives_gen as pg;
use crate::print::Selector;
use crate::token::Token;

/// The state of a `\read` stream.
#[derive(Clone, Debug)]
pub enum ReadFile {
    /// Closed.
    Closed,
    /// Open; no line read yet.
    JustOpen(LineReader),
    /// Open, at least one line read.
    Normal(LineReader),
}

/// The lines of a file opened by `\openin`, split when they are read:
/// files are often opened only to test that they exist.
#[derive(Clone, Debug, Default)]
pub struct LineReader {
    data: Vec<u8>,
    pos: usize,
}

impl LineReader {
    /// Reads the lines of `data`.
    pub fn new(data: Vec<u8>) -> Self {
        LineReader { data, pos: 0 }
    }

    /// The next line (without its terminator), if any.
    pub fn pop_front(&mut self) -> Option<Vec<u8>> {
        let (line, next) = crate::input::next_line(&self.data, self.pos)?;
        let line = line.to_vec();
        self.pos = next;
        Some(line)
    }
}

/// A file that was read or written during a pass, with a content hash.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FileRecord {
    /// The path as opened.
    pub path: PathBuf,
    /// FNV-1a hash of the content, or `None` if the file did not exist.
    pub hash: Option<u64>,
}

/// File-related state.
#[derive(Debug)]
pub struct FileState {
    /// `\read` streams 0–15.
    pub read_files: Vec<ReadFile>,
    /// Whether a file name is being scanned (`name_in_progress`).
    pub name_in_progress: bool,
    /// The directory for output files (`-output-directory`).
    pub output_dir: Option<PathBuf>,
    /// Files read during this pass, in order of first access.
    pub read_log: Vec<FileRecord>,
    /// Content of files written during this pass, by path.
    pub written: HashMap<PathBuf, Vec<u8>>,
    /// The first line given on the command line.
    pub command_line: Vec<u8>,
    /// The log file name, once opened.
    pub log_name: Option<PathBuf>,
    /// Whether the log went to TEXMFOUTPUT (the current directory was not
    /// writable).
    pub log_in_texmfoutput: bool,
    /// Further files the job depends on that the pass driver does not
    /// compare between passes (images), for `--watch`.
    pub watched: Vec<PathBuf>,
}

impl Default for FileState {
    fn default() -> Self {
        FileState {
            read_files: vec![ReadFile::Closed; 16],
            name_in_progress: false,
            output_dir: None,
            read_log: Vec::new(),
            written: HashMap::new(),
            command_line: Vec::new(),
            log_name: None,
            log_in_texmfoutput: false,
            watched: Vec::new(),
        }
    }
}

impl FileState {
    /// Whether `\read` stream `n` is open.
    pub fn read_open(&self, n: u32) -> bool {
        !matches!(
            self.read_files.get(n as usize),
            None | Some(ReadFile::Closed)
        )
    }
}

/// FNV-1a hash used to detect content changes between passes.
pub fn content_hash(data: &[u8]) -> u64 {
    let mut h: u64 = 0xcbf2_9ce4_8422_2325;
    for &b in data {
        h ^= u64::from(b);
        h = h.wrapping_mul(0x0100_0000_01b3);
    }
    h
}

impl Engine {
    /// Reads a whole file, recording it for the pass driver.
    pub fn read_file_recorded(&mut self, path: &Path) -> Option<Vec<u8>> {
        let data = std::fs::read(path).ok();
        if !self.files.read_log.iter().any(|r| r.path == path) {
            self.files.read_log.push(FileRecord {
                path: path.to_path_buf(),
                hash: data.as_deref().map(content_hash),
            });
        }
        data
    }

    /// Records a file lookup from Lua (`kpse.find_file`, which expl3 uses to
    /// test whether a file exists) for the pass driver: files of the job
    /// (relative names) are recorded with their content hash, or as absent.
    pub fn record_probe(&mut self, name: &str, found: Option<&Path>) {
        let path = match found {
            Some(p) if p.is_relative() => p.to_path_buf(),
            Some(_) => return,
            None if !Path::new(name).is_absolute() && !name.contains("//") => {
                self.absent_path(name)
            }
            None => return,
        };
        if self.files.read_log.iter().any(|r| r.path == path) {
            return;
        }
        let hash = found
            .and_then(|p| std::fs::read(p).ok())
            .map(|d| content_hash(&d));
        self.files.read_log.push(FileRecord { path, hash });
    }

    /// Where a job file `name` that was not found would appear: in the
    /// output directory, where the job writes its files, if there is one.
    fn absent_path(&self, name: &str) -> PathBuf {
        match &self.files.output_dir {
            Some(d) if Path::new(name).is_relative() => d.join(name),
            _ => PathBuf::from(name),
        }
    }

    /// Records that file `name` was looked for and not found, so that the
    /// pass driver reruns when this pass writes it (e.g. the `.aux` file
    /// LaTeX tests for with `\filesize`).
    pub fn record_absent_file(&mut self, name: &str) {
        let p = self.absent_path(name);
        if !self.files.read_log.iter().any(|r| r.path == p) {
            self.files.read_log.push(FileRecord {
                path: p,
                hash: None,
            });
        }
    }

    /// Whether file `name` may be opened for `access` (kpathsea's
    /// `openin_any` and `openout_any`); if not, kpathsea's warning goes to
    /// the standard error stream, as with LuaTeX. Names are checked as
    /// given, before any output directory or search path applies.
    pub fn name_ok(&self, name: &str, access: retex_kpath::Access) -> bool {
        let Some(k) = &self.kpse else {
            return true;
        };
        let refusal = match k.borrow().name_ok(name, access, false) {
            Ok(()) => return true,
            Err(r) => r,
        };
        let program = std::env::args().next().unwrap_or_else(|| "retex".into());
        eprint!("\n{program}: {}\n", refusal.message(name));
        false
    }

    /// Looks a TeX input file up: in the output directory first, then via
    /// kpathsea.
    pub fn find_input_file(&mut self, name: &str, fmt: retex_kpath::FileFormat) -> Option<PathBuf> {
        if let Some(dir) = &self.files.output_dir {
            for cand in [format!("{name}.tex"), name.to_owned()] {
                let p = dir.join(&cand);
                if p.is_file() {
                    return Some(p);
                }
            }
        }
        if let Some(k) = &self.kpse {
            return k.borrow_mut().find_file(name, fmt);
        }
        let p = PathBuf::from(name);
        if p.is_file() {
            return Some(p);
        }
        let p = PathBuf::from(format!("{name}.tex"));
        p.is_file().then_some(p)
    }

    /// Scans a file name (`scan_file_name`). LuaTeX also accepts a name in
    /// braces and names in double quotes.
    pub fn scan_file_name(&mut self) -> Vec<u8> {
        self.files.name_in_progress = true;
        let mut name: Vec<u8> = Vec::new();
        self.get_nonblank_noncall();
        if self.cur_cmd == Cmd::LeftBrace {
            self.back_input();
            let toks = self.scan_toks_expanded();
            let s = self.print_to_string(|e| e.show_token_list(&toks, 0, toks.len(), i64::MAX));
            name = s.into_iter().filter(|&b| b != b'"').collect();
            self.files.name_in_progress = false;
            return name;
        }
        let mut quoted = false;
        loop {
            if self.cur_cmd > Cmd::OtherChar || self.cur_cs != crate::hash::NO_CS {
                self.back_input();
                break;
            }
            let c = self.cur_chr;
            if c == ' ' as u32 && !quoted {
                break;
            }
            if c == '"' as u32 {
                quoted = !quoted;
            } else {
                crate::utf8x::push(&mut name, c);
            }
            self.get_x_token();
        }
        self.files.name_in_progress = false;
        name
    }

    /// The output of the command of a file name `|command` (a pipe), when
    /// shell escape allows running it.
    fn read_pipe(&mut self, name: &[u8]) -> Option<Vec<u8>> {
        let cmd = name.strip_prefix(b"|")?;
        if self.shell_escape == 0 {
            return None;
        }
        let host = self.lua.clone()?;
        host.pipe_read(self, cmd)
    }

    /// `\input` (`start_input`).
    pub fn start_input(&mut self) {
        let name = self.scan_file_name();
        let name_str = String::from_utf8_lossy(&name).into_owned();
        let (found, data) = if let Some(out) = self.read_pipe(&name) {
            // `\input|"command"` reads the command's output.
            (PathBuf::from(&name_str), out)
        } else {
            let allowed = self.name_ok(&name_str, retex_kpath::Access::Read);
            let found = match allowed
                .then(|| self.find_input_file(&name_str, retex_kpath::FileFormat::Tex))
                .flatten()
            {
                Some(p) => p,
                None => self.prompt_file_name(&name, "input file name", ".tex"),
            };
            let data = self.read_file_recorded(&found).unwrap_or_default();
            (found, data)
        };
        if let Some(f) = self.progress_hook {
            f(self);
        }
        if !self.printer.log_opened() && self.job_name.is_some() {
            self.open_log_file();
        }
        if self.job_name.is_none() {
            let base = Path::new(&name_str)
                .file_stem()
                .map(|s| s.to_string_lossy().into_owned())
                .unwrap_or_else(|| name_str.clone());
            self.job_name = Some(base.into_bytes());
            self.open_log_file();
        }
        let display = found.to_string_lossy().into_owned().into_bytes();
        // Observed: LuaTeX shows a name with spaces in double quotes.
        let shown = if display.contains(&b' ') {
            [&b"\""[..], &display, b"\""].concat()
        } else {
            display.clone()
        };
        self.check_in_open();
        if self.printer.term_offset + shown.len() > self.printer.max_print_line.saturating_sub(2) {
            self.printer.print_ln();
        } else if self.printer.term_offset > 0 || self.printer.file_offset > 0 {
            self.printer.print_char(' ' as u32);
        }
        self.printer.print_char('(' as u32);
        self.input.open_parens += 1;
        self.printer.print_bytes(&shown);
        self.printer.update_terminal();
        let source = LineSource::File {
            data: Rc::from(data),
            pos: 0,
        };
        self.begin_file_reading(source, display, true);
        self.synctex_input_opened(&found);
        // Read the first line of the new file.
        self.read_first_line();
    }

    fn read_first_line(&mut self) {
        let end_line_char = self.eqtb.int(pg::assign_int::ENDLINECHAR);
        if let Some(Level::Text(t)) = self.input.stack.last_mut() {
            t.line = 0;
            let line = match &mut t.source {
                LineSource::File { data, pos } => match crate::input::next_line(data, *pos) {
                    Some((bytes, next)) => {
                        let l = crate::input::decode_line(bytes);
                        *pos = next;
                        Some(l)
                    }
                    None => None,
                },
                _ => None,
            };
            t.line = 1;
            t.sync.1 = 1;
            t.buf = line.unwrap_or_default();
            if (0..=0x10_FFFF).contains(&end_line_char) {
                t.buf.push(end_line_char as u32);
            }
            t.loc = 0;
            t.state = crate::input::LexState::NewLine;
        }
        self.sync_input_changed();
    }

    /// Asks for a file name after a lookup failure (`prompt_file_name`). In
    /// non-interactive modes this is fatal.
    pub fn prompt_file_name(&mut self, name: &[u8], s: &str, e: &str) -> ! {
        if s == "input file name" {
            self.print_err("I can't find file `");
        } else {
            self.print_err("I can't write on file `");
        }
        self.printer.print_bytes(name);
        self.printer.print_str("'.");
        if e == ".tex" {
            self.show_context();
        }
        self.printer.print_ln();
        // Observed: LuaTeX does not mention the default extension, and
        // offers to retry only for input files.
        if s == "input file name" {
            self.printer
                .print_str("(Press Enter to retry, or Control-D to exit)");
        }
        self.printer.print_nl("Please type another ");
        self.printer.print_str(s);
        self.fatal_error("*** (job aborted, file error in nonstop mode)");
    }

    /// Opens the log file and prints the banner (`open_log_file`).
    pub fn open_log_file(&mut self) {
        let old = self.printer.selector;
        if self.job_name.is_none() {
            self.job_name = Some(b"texput".to_vec());
        }
        let job = String::from_utf8_lossy(self.job_name.as_ref().unwrap()).into_owned();
        let name = format!("{job}.log");
        if !self.name_ok(&name, retex_kpath::Access::Write) {
            self.prompt_file_name(name.as_bytes(), "transcript file name", ".log");
        }
        let mut path = PathBuf::from(&name);
        if let Some(dir) = &self.files.output_dir {
            path = dir.join(path);
        }
        let mut file = std::fs::File::create(&path);
        // Without an output directory, a log that cannot be written in the
        // current directory goes to TEXMFOUTPUT, and so do the files
        // written after it (web2c manual, "Output file location").
        if file.is_err()
            && self.files.output_dir.is_none()
            && let Some(dir) = self
                .kpse
                .as_ref()
                .and_then(|k| k.borrow().var_value("TEXMFOUTPUT"))
                .filter(|d| !d.is_empty())
        {
            let dir = PathBuf::from(dir);
            file = std::fs::File::create(dir.join(&name));
            if file.is_ok() {
                path = dir.join(&name);
                self.files.output_dir = Some(dir);
                self.files.log_in_texmfoutput = true;
            }
        }
        match file {
            Ok(f) => self
                .printer
                .set_log(Box::new(std::io::BufWriter::with_capacity(1 << 16, f))),
            Err(_) => self.prompt_file_name(name.as_bytes(), "transcript file name", ".log"),
        }
        self.files.log_name = Some(path);
        // Print the banner line, including the date and time.
        let banner = self.log_banner();
        self.printer.log_raw(banner.as_bytes());
        let shell = self.shell_escape_banner();
        if shell.is_empty() {
            self.printer.file_offset = banner.len();
        } else {
            self.printer.log_raw(b"\n");
            self.printer.log_raw(shell.as_bytes());
            self.printer.file_offset = shell.len();
        }
        self.printer.selector = Selector::LogOnly;
        self.printer.print_nl("**");
        let line = self.files.command_line.clone();
        self.printer.print_bytes(&line);
        self.printer.print_ln();
        self.printer.selector = match old {
            Selector::NoPrint => Selector::LogOnly,
            Selector::TermOnly => Selector::TermAndLog,
            s => s,
        };
    }

    /// The first line of the log file.
    /// The line after the banner that tells the shell escape mode (none
    /// when it is disabled).
    pub fn shell_escape_banner(&self) -> &'static str {
        match self.shell_escape {
            0 => "",
            1 => " system commands enabled.",
            _ => " restricted system commands enabled.",
        }
    }

    fn log_banner(&self) -> String {
        let c = self.start_time.civil();
        const MONTHS: [&str; 12] = [
            "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC",
        ];
        format!(
            "This is retex, Version {} (LuaTeX 1.22.0 compatible)  ({})  {} {} {} {:02}:{:02}",
            env!("CARGO_PKG_VERSION"),
            if self.ini_version {
                "INITEX".to_owned()
            } else {
                self.format_ident
                    .trim()
                    .trim_start_matches('(')
                    .trim_end_matches(')')
                    .to_owned()
            },
            c.day,
            MONTHS[(c.month as usize).saturating_sub(1).min(11)],
            c.year,
            c.hour,
            c.minute
        )
    }

    /// The terminal line is exhausted: in `\errorstopmode`/`\scrollmode`
    /// ask for more input (§360), otherwise stop.
    pub fn terminal_exhausted(&mut self) {
        if self.printer.selector == Selector::NoPrint || self.printer.selector == Selector::TermOnly
        {
            self.open_log_file();
        }
        if self.err.interaction > crate::error::Interaction::NonStop {
            let empty = matches!(self.input.stack.last(), Some(Level::Text(t)) if t.buf.is_empty());
            if empty {
                self.printer
                    .print_nl("(Please type a command or say `\\end')");
            }
            self.printer.print_ln();
            let mut line = self.prompt_input("*");
            let elc = self.eqtb.int(pg::assign_int::ENDLINECHAR);
            if (0..=0x10_FFFF).contains(&elc) {
                line.push(elc as u32);
            }
            if let Some(Level::Text(t)) = self.input.stack.last_mut() {
                t.buf = line;
                t.loc = 0;
                t.state = crate::input::LexState::NewLine;
            }
            return;
        }
        self.fatal_error("*** (job aborted, no legal \\end found)");
    }

    /// `\openin` and `\closein` (`open_or_close_in`).
    pub fn open_or_close_in(&mut self) {
        let c = self.cur_chr;
        self.scan_four_bit_int();
        let n = self.cur_val as usize;
        self.files.read_files[n] = ReadFile::Closed;
        if c != 0 {
            self.scan_optional_equals();
            let name = self.scan_file_name();
            if name.first() == Some(&b'|') {
                if let Some(out) = self.read_pipe(&name) {
                    self.files.read_files[n] = ReadFile::JustOpen(LineReader::new(out));
                }
                return;
            }
            let name = String::from_utf8_lossy(&name).into_owned();
            if !self.name_ok(&name, retex_kpath::Access::Read) {
                return;
            }
            if let Some(p) = self.find_input_file(&name, retex_kpath::FileFormat::Tex)
                && let Some(data) = self.read_file_recorded(&p)
            {
                self.files.read_files[n] = ReadFile::JustOpen(LineReader::new(data));
            } else {
                self.record_absent_file(&name);
            }
        }
    }

    /// Reads a line from a `\read` stream into a token list (`read_toks`,
    /// §482). `j` is 1 for `\readline` (e-TeX). The result starts with the
    /// `END_MATCH` token, as a macro body without parameters.
    pub fn read_toks(&mut self, n: i32, r: crate::hash::CsId, j: u32) -> Vec<Token> {
        self.input.scanner_status = ScannerStatus::Defining;
        self.input.warning_index = r;
        let mut out: Vec<Token> = vec![Token::END_MATCH];
        let m: usize = if (0..=15).contains(&n) {
            n as usize
        } else {
            16
        };
        let s = self.input.align_state;
        self.input.align_state = 1_000_000;
        loop {
            // Input and store tokens from the next line of the file.
            let line: Vec<u8> = match self.files.read_files.get_mut(m) {
                None | Some(ReadFile::Closed) => {
                    // The (empty) line being read shows in the error
                    // context as `<read *>` (§483).
                    self.begin_file_reading(LineSource::Read(m as u32), Vec::new(), false);
                    // Interactive terminal reading is not supported.
                    self.fatal_error("*** (cannot \\read from terminal in nonstop modes)");
                }
                Some(ReadFile::JustOpen(lines)) => {
                    let first = lines.pop_front();
                    match first {
                        Some(l) => {
                            let rest = std::mem::take(lines);
                            self.files.read_files[m] = ReadFile::Normal(rest);
                            l
                        }
                        None => {
                            self.files.read_files[m] = ReadFile::Closed;
                            Vec::new()
                        }
                    }
                }
                Some(ReadFile::Normal(lines)) => match lines.pop_front() {
                    Some(l) => l,
                    None => {
                        self.files.read_files[m] = ReadFile::Closed;
                        if self.input.align_state != 1_000_000 {
                            self.runaway();
                            self.print_err("File ended within ");
                            self.print_esc_str("read");
                            self.help(&["This \\read has unbalanced braces."]);
                            self.input.align_state = 1_000_000;
                            self.error();
                        }
                        Vec::new()
                    }
                },
            };
            let end_line_char = self.eqtb.int(pg::assign_int::ENDLINECHAR);
            let mut buf = crate::input::decode_line(&line);
            if (0..=0x10_FFFF).contains(&end_line_char) {
                buf.push(end_line_char as u32);
            }
            if j == 1 {
                // \readline: spaces become space tokens, the rest other.
                for &c in &buf {
                    out.push(if c == ' ' as u32 {
                        Token::SPACE
                    } else {
                        Token::other(c)
                    });
                }
                break;
            }
            self.begin_file_reading(LineSource::Read(m as u32), Vec::new(), false);
            if let Some(Level::Text(t)) = self.input.stack.last_mut() {
                t.buf = buf;
                t.loc = 0;
                t.state = crate::input::LexState::NewLine;
            }
            loop {
                self.get_token();
                if self.cur_tok.0 == 0 {
                    break;
                }
                if self.input.align_state < 1_000_000 {
                    // An unmatched `}` aborts the line.
                    loop {
                        self.get_token();
                        if self.cur_tok.0 == 0 {
                            break;
                        }
                    }
                    self.input.align_state = 1_000_000;
                    break;
                }
                out.push(self.cur_tok);
            }
            self.end_file_reading();
            if self.input.align_state == 1_000_000 {
                break;
            }
        }
        self.input.scanner_status = ScannerStatus::Normal;
        self.input.align_state = s;
        out
    }

    /// `\scantokens` and `\scantextokens` (e-TeX `pseudo_start`).
    pub fn pseudo_start(&mut self, text_tokens: bool) {
        let list = self.scan_general_text();
        let s = self.print_to_string(|e| e.show_token_list(&list, 0, list.len(), i64::MAX));
        // Split at the new-line character. As in etex.ch, each line ends
        // at a new-line character or at the end of the string, so a final
        // new-line character starts no further line and an empty string
        // has no line at all.
        let nl = self.eqtb.int(pg::assign_int::NEWLINECHAR);
        let mut lines: VecDeque<Vec<u8>> = VecDeque::new();
        let mut rest = s.as_slice();
        while !rest.is_empty() {
            let end = if (0..128).contains(&nl) {
                rest.iter().position(|&b| i32::from(b) == nl)
            } else {
                None
            };
            let (line, tail) = match end {
                Some(i) => (&rest[..i], &rest[i + 1..]),
                None => (rest, &rest[rest.len()..]),
            };
            lines.push_back(line.to_vec());
            rest = tail;
        }
        let tracing = self.eqtb.int(pg::assign_int::TRACINGSCANTOKENS) > 0 && !text_tokens;
        self.begin_file_reading(
            LineSource::Lines {
                lines,
                catcodes: None,
                pseudo: !text_tokens,
            },
            Vec::new(),
            tracing,
        );
        if let Some(Level::Text(t)) = self.input.stack.last_mut() {
            t.line = 0;
            t.buf.clear();
            t.loc = 0;
            t.eof_seen = text_tokens;
        }
        if tracing {
            if self.printer.term_offset > self.printer.max_print_line.saturating_sub(3) {
                self.printer.print_ln();
            } else if self.printer.term_offset > 0 || self.printer.file_offset > 0 {
                self.printer.print_char(' ' as u32);
            }
            self.printer.print_str("( ");
            self.input.open_parens += 1;
            self.printer.update_terminal();
        }
    }

    /// e-TeX's check when a file ends inside groups or conditionals.
    pub fn file_closed_checks(&mut self) {}

    /// Closes everything at the end of the run
    /// (`close_files_and_terminate`).
    pub fn close_files_and_terminate(&mut self) {
        for n in 0..128u32 {
            if self.printer.write_open(n) {
                self.printer.close_write(n);
            }
        }
        self.print_memory_stats();
        // Observed after a fatal error (an emergency stop): in PDF mode
        // there is no PDF file and the log ends with LuaTeX's notice; in
        // DVI mode the notice comes before the usual last lines.
        let pdf_mode = self.eqtb.int(crate::primitives_gen::assign_int::OUTPUTMODE) > 0;
        let fatal = self.err.history == crate::error::History::FatalErrorStop && !self.pdf.fatal;
        if fatal && pdf_mode {
            self.pdf.fatal = true;
            self.finish_output();
            self.printer
                .print_nl("!  ==> Fatal error occurred, no output PDF file produced!");
            // Observed: without a log (it could not be written) the line
            // is left open.
            if self.printer.log_opened() {
                self.printer.print_ln();
            }
            let _ = std::fs::remove_file(self.output_path("pdf"));
            self.finish_log();
            return;
        }
        if fatal {
            self.printer
                .print_nl("!  ==> Fatal error occurred, bad output DVI file produced!");
            self.printer.print_ln();
        }
        self.finish_output();
        if self.total_pages == 0 {
            self.printer.print_nl(
                if self.eqtb.int(crate::primitives_gen::assign_int::OUTPUTMODE) > 0 {
                    "warning  (pdf backend): no pages of output."
                } else {
                    "No pages of output."
                },
            );
            self.printer.print_ln();
            if self.pdf.stream_written
                && self.eqtb.int(crate::primitives_gen::assign_int::OUTPUTMODE) > 0
            {
                self.pdf_fatal("already written content discarded, no output file produced.");
            }
        }
        // LuaTeX reports the objects it allocated (log only).
        if self.eqtb.int(crate::primitives_gen::assign_int::OUTPUTMODE) > 0
            && self.pdf.max_obj() > 0
            && !self.pdf.fatal
            && self.printer.log_opened()
        {
            if self.printer.file_offset > 0 {
                self.printer.print_ln();
            }
            let n = self.pdf.max_obj();
            let names = self.pdf.named_dests();
            // The extra memory is not modelled exactly: LuaTeX uses 8
            // words per outline entry, ten per box resource and five per
            // object (observed), and more for threads.
            let extra = 1
                + 8 * self.pdf.outlines.len()
                + 10 * self.box_resources.len()
                + self.pdf.obj_words;
            let (objs, streams) = self.pdf.objstm_stats;
            let compressed = if streams > 0 {
                format!(
                    " {objs} compressed objects within {streams} object stream{}\n",
                    if streams == 1 { "" } else { "s" }
                )
            } else {
                String::new()
            };
            self.printer.log_raw(
                format!(
                    "\nPDF statistics: {n} PDF objects out of {} (max. 8388607)\n{compressed} \
                     {names} named destinations out of {} (max. 131072)\n \
                     {extra} words of extra memory for PDF output out of {} (max. 100000000)\n",
                    n.max(1000),
                    names.max(1000),
                    extra.max(10000),
                )
                .as_bytes(),
            );
        }
        if self.printer.log_opened() {
            self.printer.log_raw(b"\n");
        }
        self.finish_synctex();
        self.finish_log();
    }

    /// LuaTeX's memory usage report, which ends the log when
    /// `\tracingstats` is positive (compatibility mode only; just the
    /// counts retex keeps).
    fn print_memory_stats(&mut self) {
        if self.diagnostics.is_some()
            || !self.printer.log_opened()
            || self
                .eqtb
                .int(crate::primitives_gen::assign_int::TRACINGSTATS)
                <= 0
        {
            return;
        }
        let sel = self.printer.selector;
        if sel == Selector::TermAndLog {
            self.printer.selector = Selector::LogOnly;
        }
        // Observed: after an empty line.
        if self.printer.file_offset > 0 {
            self.printer.print_ln();
        }
        self.printer.print_ln();
        self.printer
            .print_str("Here is how much of LuaTeX's memory you used:");
        let cs = self.hash.len();
        self.printer.print_nl(&format!(
            " {cs} multiletter control sequences out of 65536+600000"
        ));
        let fonts = self.fonts.fonts.len();
        self.printer.print_nl(&format!(" {fonts} fonts"));
        self.printer.print_ln();
        self.printer.selector = sel;
    }

    /// Closes the log and names it on the terminal.
    pub(crate) fn finish_log(&mut self) {
        if self.printer.log_opened() {
            self.printer.close_log();
            self.printer.selector = Selector::TermOnly;
            if let Some(name) = self.files.log_name.clone() {
                // The name as opened: in an -output-directory, LuaTeX shows
                // it without the directory (observed).
                let shown = match &self.files.output_dir {
                    Some(dir) if !self.files.log_in_texmfoutput => {
                        name.strip_prefix(dir).map(PathBuf::from).unwrap_or(name)
                    }
                    _ => name,
                };
                self.printer.print_nl("Transcript written on ");
                self.printer.print_bytes(shown.to_string_lossy().as_bytes());
                self.printer.print_char('.' as u32);
            }
        }
        self.printer.print_ln();
        self.printer.flush();
    }
}

/// Shared record of the contents written to files during a pass.
pub type WrittenSink = std::rc::Rc<std::cell::RefCell<HashMap<PathBuf, Vec<u8>>>>;

/// A file writer that also records everything written, so that the pass
/// driver can compare it with what the next pass reads.
pub struct RecordingWriter {
    inner: std::io::BufWriter<std::fs::File>,
    path: PathBuf,
    sink: WrittenSink,
}

impl RecordingWriter {
    /// Wraps `f`, recording its content under `path`.
    pub fn new(f: std::fs::File, path: PathBuf, sink: WrittenSink) -> Self {
        sink.borrow_mut().insert(path.clone(), Vec::new());
        RecordingWriter {
            inner: std::io::BufWriter::new(f),
            path,
            sink,
        }
    }
}

impl RecordingWriter {
    /// Wraps `f`, appending to what is already recorded under `path` (a
    /// file reopened after resuming from a snapshot).
    pub fn resume(f: std::fs::File, path: PathBuf, sink: WrittenSink) -> Self {
        sink.borrow_mut().entry(path.clone()).or_default();
        RecordingWriter {
            inner: std::io::BufWriter::new(f),
            path,
            sink,
        }
    }
}

impl std::io::Write for RecordingWriter {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        if let Some(v) = self.sink.borrow_mut().get_mut(&self.path) {
            v.extend_from_slice(buf);
        }
        self.inner.write(buf)
    }

    fn flush(&mut self) -> std::io::Result<()> {
        self.inner.flush()
    }
}