flams_stex/
rustex.rs

1use ::rustex_lib::engine::{CompilationResult, RusTeXEngineExt};
2use flams_utils::binary::BinaryWriter;
3use parking_lot::Mutex;
4use std::io::Write;
5use std::path::Path;
6use tex_engine::prelude::Mouth;
7
8#[allow(clippy::module_inception)]
9mod rustex {
10    pub use rustex_lib;
11    pub use rustex_lib::engine::commands::{
12        register_primitives_postinit, register_primitives_preinit,
13    };
14    pub use rustex_lib::engine::files::RusTeXFileSystem;
15    pub use rustex_lib::engine::output::{OutputCont, RusTeXOutput};
16    pub use rustex_lib::engine::stomach::RusTeXStomach;
17    pub use rustex_lib::engine::{fonts::Fontsystem, state::RusTeXState};
18    pub use rustex_lib::engine::{Extension, RusTeXEngine, RusTeXEngineT, Types};
19    pub use tex_engine::commands::{Macro, MacroSignature, TeXCommand};
20    pub use tex_engine::engine::filesystem::FileSystem;
21    pub use tex_engine::engine::gullet::DefaultGullet;
22    pub use tex_engine::engine::mouth::DefaultMouth;
23    pub use tex_engine::engine::{DefaultEngine, EngineAux};
24    pub use tex_engine::pdflatex::{nodes::PDFExtension, PDFTeXEngine};
25    pub use tex_engine::prelude::{CSName, InternedCSName, Token, TokenList};
26    pub use tex_engine::tex::tokens::control_sequences::CSInterner;
27    pub use tex_engine::tex::tokens::StandardToken;
28    pub use tex_engine::{engine::utils::memory::MemoryManager, tex::tokens::CompactToken};
29    pub use tracing::{debug, error, instrument, trace, warn};
30    pub type RTSettings = rustex_lib::engine::Settings;
31    pub use rustex_lib::ImageOptions;
32}
33pub use rustex::OutputCont;
34#[allow(clippy::wildcard_imports)]
35use rustex::*;
36
37struct FileOutput(std::cell::RefCell<std::io::BufWriter<std::fs::File>>);
38impl FileOutput {
39    fn new(path: &Path) -> Self {
40        let f = std::fs::File::create(path).expect("This should not happen!");
41        let buf = std::io::BufWriter::new(f);
42        Self(std::cell::RefCell::new(buf))
43    }
44}
45
46impl OutputCont for FileOutput {
47    fn message(&self, text: String) {
48        let _ = writeln!(self.0.borrow_mut(), "{text}");
49    }
50    fn errmessage(&self, text: String) {
51        let _ = writeln!(self.0.borrow_mut(), "{text}");
52    }
53    fn file_open(&self, text: String) {
54        let _ = writeln!(self.0.borrow_mut(), "({text}");
55    }
56    fn file_close(&self, _text: String) {
57        let _ = self.0.borrow_mut().write_string(")\n");
58    }
59    fn write_18(&self, text: String) {
60        let _ = writeln!(self.0.borrow_mut(), "{text}");
61    }
62    fn write_17(&self, text: String) {
63        let _ = writeln!(self.0.borrow_mut(), "{text}");
64    }
65    fn write_16(&self, text: String) {
66        let _ = writeln!(self.0.borrow_mut(), "{text}");
67    }
68    fn write_neg1(&self, text: String) {
69        let _ = writeln!(self.0.borrow_mut(), "{text}");
70    }
71    fn write_other(&self, text: String) {
72        let _ = writeln!(self.0.borrow_mut(), "{text}");
73    }
74    #[inline]
75    fn as_any(self: Box<Self>) -> Box<dyn std::any::Any> {
76        self
77    }
78}
79
80struct TracingOutput;
81impl OutputCont for TracingOutput {
82    fn message(&self, text: String) {
83        debug!(target:"rustex","{}", text);
84    }
85    fn errmessage(&self, text: String) {
86        debug!(target:"rustex","{}", text);
87    }
88    fn file_open(&self, text: String) {
89        trace!(target:"rustex","({}", text);
90    }
91    fn file_close(&self, _text: String) {
92        trace!(target:"rustex",")");
93    }
94    fn write_18(&self, text: String) {
95        trace!(target:"rustex","write18: {}", text);
96    }
97    fn write_17(&self, text: String) {
98        debug!(target:"rustex","{}", text);
99    }
100    fn write_16(&self, text: String) {
101        trace!(target:"rustex","write16: {}", text);
102    }
103    fn write_neg1(&self, text: String) {
104        trace!(target:"rustex","write-1: {}", text);
105    }
106    fn write_other(&self, text: String) {
107        trace!(target:"rustex","write: {}", text);
108    }
109
110    #[inline]
111    fn as_any(self: Box<Self>) -> Box<dyn std::any::Any> {
112        self
113    }
114}
115
116#[derive(Clone)]
117struct EngineBase {
118    state: RusTeXState,
119    memory: MemoryManager<CompactToken>,
120    font_system: Fontsystem,
121}
122static ENGINE_BASE: Mutex<Option<Result<EngineBase, ()>>> = Mutex::new(None);
123
124impl EngineBase {
125    fn into_engine<O: OutputCont + 'static, I: IntoIterator<Item = (String, String)>>(
126        mut self,
127        envs: I,
128        out: O,
129    ) -> RusTeXEngine {
130        //use tex_engine::engine::filesystem::FileSystem;
131        use tex_engine::engine::gullet::Gullet;
132        use tex_engine::engine::stomach::Stomach;
133        use tex_engine::engine::EngineExtension;
134        use tex_engine::prelude::ErrorHandler;
135        use tex_engine::prelude::Mouth;
136        let mut aux = EngineAux {
137            outputs: RusTeXOutput::Cont(Box::new(out)),
138            error_handler: ErrorThrower::new(),
139            start_time: chrono::Local::now(),
140            extension: Extension::new(&mut self.memory),
141            memory: self.memory,
142            jobname: String::new(),
143        };
144        let mut mouth = DefaultMouth::new(&mut aux, &mut self.state);
145        let gullet = DefaultGullet::new(&mut aux, &mut self.state, &mut mouth);
146        let mut stomach = RusTeXStomach::new(&mut aux, &mut self.state);
147        stomach.continuous = true;
148        DefaultEngine {
149            state: self.state,
150            aux,
151            fontsystem: self.font_system,
152            filesystem: RusTeXFileSystem::new_with_envs(tex_engine::utils::PWD.to_path_buf(), envs),
153            mouth,
154            gullet,
155            stomach,
156        }
157    }
158    fn get<R, F: FnOnce(&Self) -> R>(f: F) -> Result<R, ()> {
159        let mut lock = ENGINE_BASE.lock();
160        match &mut *lock {
161            Some(Ok(engine)) => Ok(f(engine)),
162            Some(_) => Err(()),
163            o => {
164                *o = Some(Self::initialize());
165                o.as_ref()
166                    .unwrap_or_else(|| unreachable!())
167                    .as_ref()
168                    .map(f)
169                    .map_err(|()| ())
170            }
171        }
172    }
173
174    #[instrument(level = "info", target = "sTeX", name = "Initializing RusTeX engine")]
175    fn initialize() -> Result<Self, ()> {
176        use tex_engine::engine::TeXEngine;
177        std::panic::catch_unwind(|| {
178            let mut engine = DefaultEngine::<Types>::default();
179            engine.aux.outputs = RusTeXOutput::Cont(Box::new(TracingOutput));
180            register_primitives_preinit(&mut engine);
181            match engine.initialize_pdflatex() {
182                Ok(()) => {}
183                Err(e) => {
184                    error!("Error initializing RusTeX engine: {}", e);
185                }
186            };
187            register_primitives_postinit(&mut engine);
188            match engine.init_file("rustex_defs.def") {
189                Ok(()) => {}
190                Err(e) => {
191                    error!("Error initializing RusTeX engine: {}", e);
192                }
193            };
194            Self {
195                state: engine.state.clone(),
196                memory: engine.aux.memory.clone(),
197                font_system: engine.fontsystem.clone(),
198            }
199        })
200        .map_err(|a| {
201            if let Some(s) = a.downcast_ref::<String>() {
202                tracing::error!("Error initializing RusTeX engine: {}", s);
203            } else if let Some(s) = a.downcast_ref::<&str>() {
204                tracing::error!("Error initializing RusTeX engine: {}", s);
205            } else {
206                tracing::error!("Error initializing RusTeX engine");
207            }
208        })
209    }
210}
211
212pub struct RusTeX(Mutex<EngineBase>);
213impl RusTeX {
214    pub fn get() -> Result<Self, ()> {
215        Ok(Self(
216            ENGINE_BASE
217                .lock()
218                .as_ref()
219                .unwrap_or_else(|| unreachable!())
220                .clone()?
221                .into(),
222        ))
223    }
224    pub fn initialize() {
225        let _ = EngineBase::get(|_| ());
226    }
227    /// ### Errors
228    pub fn run(&self, file: &Path, memorize: bool, out: Option<&Path>) -> Result<String, String> {
229        self.run_with_envs(
230            file,
231            memorize,
232            std::iter::once(("FLAMS_ADMIN_PWD".to_string(), "NOPE".to_string())),
233            out,
234        )
235    }
236
237    /// ### Errors
238    fn set_up<I: IntoIterator<Item = (String, String)>>(
239        &self,
240        envs: I,
241        out: Option<&Path>,
242    ) -> (DefaultEngine<Types>, RTSettings) {
243        let e = self.0.lock().clone();
244        let engine = match out {
245            None => e.into_engine(envs, TracingOutput),
246            Some(f) => e.into_engine(envs, FileOutput::new(f)),
247        };
248        let settings = RTSettings {
249            verbose: false,
250            sourcerefs: false,
251            log: true,
252            insert_font_info: false,
253            image_options: ImageOptions::AsIs,
254        };
255        (engine, settings)
256    }
257
258    /// ### Errors
259    pub fn run_with_envs<I: IntoIterator<Item = (String, String)>>(
260        &self,
261        file: &Path,
262        memorize: bool,
263        envs: I,
264        out: Option<&Path>,
265    ) -> Result<String, String> {
266        let (mut engine, settings) = self.set_up(envs, out);
267        let res = engine.run(file.to_str().unwrap_or_else(|| unreachable!()), settings);
268
269        res.error.as_ref().map_or_else(
270            || {
271                if memorize {
272                    let mut base = self.0.lock();
273                    give_back(engine, &mut base);
274                }
275                Ok(res.to_string())
276            },
277            |(e, _)| Err(e.to_string()),
278        )
279    }
280
281    pub fn builder(&self) -> RusTeXRunBuilder<false> {
282        RusTeXRunBuilder {
283            inner: self.0.lock().clone().into_engine(
284                std::iter::once(("FLAMS_ADMIN_PWD".to_string(), "NOPE".to_string())),
285                TracingOutput,
286            ),
287            settings: RTSettings {
288                verbose: false,
289                sourcerefs: false,
290                log: true,
291                insert_font_info: false,
292                image_options: ImageOptions::AsIs,
293            },
294        }
295    }
296}
297
298pub struct RusTeXRunBuilder<const HAS_PATH: bool> {
299    inner: DefaultEngine<Types>,
300    settings: RTSettings,
301}
302impl<const HAS_PATH: bool> RusTeXRunBuilder<HAS_PATH> {
303    pub fn set_output<O: OutputCont>(mut self, output: O) -> Self {
304        self.inner.aux.outputs = RusTeXOutput::Cont(Box::new(output));
305        self
306    }
307    pub const fn set_sourcerefs(mut self, b: bool) -> Self {
308        self.settings.sourcerefs = b;
309        self
310    }
311    pub fn set_envs<I: IntoIterator<Item = (String, String)>>(mut self, envs: I) -> Self {
312        self.inner.filesystem.add_envs(envs);
313        self
314    }
315    pub fn set_font_debug_info(mut self, b: bool) -> Self {
316        self.settings.insert_font_info = b;
317        self
318    }
319}
320
321pub struct EngineRemnants(DefaultEngine<Types>);
322
323impl EngineRemnants {
324    pub fn memorize(self, global: &RusTeX) {
325        let mut base = global.0.lock();
326        give_back(self.0, &mut base);
327    }
328    pub fn take_output<O: OutputCont>(&mut self) -> Option<O> {
329        match std::mem::replace(&mut self.0.aux.outputs, RusTeXOutput::None) {
330            RusTeXOutput::Cont(o) => o.as_any().downcast().ok().map(|b: Box<O>| *b),
331            _ => None,
332        }
333    }
334}
335
336impl RusTeXRunBuilder<true> {
337    pub fn run(mut self) -> (CompilationResult, EngineRemnants) {
338        *self.inner.aux.extension.elapsed() = std::time::Instant::now();
339        let res =
340            match tex_engine::engine::TeXEngine::run(&mut self.inner, rustex_lib::shipout::shipout)
341            {
342                Ok(()) => None,
343                Err(e) => {
344                    self.inner.aux.outputs.errmessage(format!(
345                        "{}\n\nat {}",
346                        e,
347                        self.inner
348                            .mouth
349                            .current_sourceref()
350                            .display(&self.inner.filesystem)
351                    ));
352                    Some(e)
353                }
354            };
355        let res = self.inner.do_result(res, self.settings);
356        (res, EngineRemnants(self.inner))
357    }
358}
359impl RusTeXRunBuilder<false> {
360    pub fn set_path(mut self, p: &Path) -> Option<RusTeXRunBuilder<true>> {
361        let parent = p.parent()?;
362        self.inner.filesystem.set_pwd(parent.to_path_buf());
363        self.inner.aux.jobname = p.with_extension("").file_name()?.to_str()?.to_string();
364        let f = self.inner.filesystem.get(p.as_os_str().to_str()?);
365        self.inner.mouth.push_file(f);
366        Some(RusTeXRunBuilder {
367            inner: self.inner,
368            settings: self.settings,
369        })
370    }
371    pub fn set_string(mut self, in_path: &Path, content: &str) -> Option<RusTeXRunBuilder<true>> {
372        let parent = in_path.parent()?;
373        self.inner.filesystem.set_pwd(parent.to_path_buf());
374        self.inner.aux.jobname = in_path
375            .with_extension("")
376            .file_name()?
377            .to_str()?
378            .to_string();
379        self.inner
380            .filesystem
381            .add_file(in_path.to_path_buf(), content);
382        let f = self.inner.filesystem.get(in_path.file_name()?.to_str()?);
383        self.inner.mouth.push_file(f);
384        Some(RusTeXRunBuilder {
385            inner: self.inner,
386            settings: self.settings,
387        })
388    }
389}
390
391fn save_macro(
392    name: InternedCSName<u8>,
393    m: &Macro<CompactToken>,
394    oldmem: &CSInterner<u8>,
395    newmem: &mut CSInterner<u8>,
396    state: &mut RusTeXState,
397) {
398    let oldname = oldmem.resolve(name);
399    let newname = convert_name(name, oldmem, newmem);
400
401    let exp = &m.expansion;
402    let newexp: TokenList<_> = exp
403        .0
404        .iter()
405        .map(|x| convert_token(*x, oldmem, newmem))
406        .collect();
407    let newsig = MacroSignature {
408        arity: m.signature.arity,
409        params: m
410            .signature
411            .params
412            .0
413            .iter()
414            .map(|x| convert_token(*x, oldmem, newmem))
415            .collect(),
416    };
417    let newmacro = Macro {
418        protected: m.protected,
419        long: m.long,
420        outer: m.outer,
421        signature: newsig,
422        expansion: newexp,
423    };
424    state.set_command_direct(newname, Some(TeXCommand::Macro(newmacro)));
425}
426
427fn convert_name(
428    oldname: InternedCSName<u8>,
429    oldmem: &CSInterner<u8>,
430    newmem: &mut CSInterner<u8>,
431) -> InternedCSName<u8> {
432    newmem.intern(oldmem.resolve(oldname))
433}
434
435fn convert_token(
436    old: CompactToken,
437    oldmem: &CSInterner<u8>,
438    newmem: &mut CSInterner<u8>,
439) -> CompactToken {
440    match old.to_enum() {
441        StandardToken::ControlSequence(cs) => {
442            CompactToken::from_cs(convert_name(cs, oldmem, newmem))
443        }
444        _ => old,
445    }
446}
447
448use tex_engine::tex::tokens::control_sequences::CSNameMap;
449use tex_engine::utils::errors::ErrorThrower;
450
451fn give_back(engine: RusTeXEngine, base: &mut EngineBase) {
452    let EngineBase {
453        state,
454        memory,
455        font_system,
456    } = base;
457    *font_system = engine.fontsystem;
458    let oldinterner = engine.aux.memory.cs_interner();
459    let iter = CommandIterator {
460        prefixes: &[b"c_stex_module_", b"c_stex_mathhub_"],
461        cmds: engine.state.destruct().into_iter(),
462        interner: oldinterner,
463    };
464    for (n, c) in iter.filter_map(|(a, b)| match b {
465        TeXCommand::Macro(m) => Some((a, m)),
466        _ => None,
467    }) {
468        save_macro(n, &c, oldinterner, memory.cs_interner_mut(), state);
469    }
470}
471
472pub struct CommandIterator<'a, I: Iterator<Item = (InternedCSName<u8>, TeXCommand<Types>)>> {
473    prefixes: &'static [&'static [u8]],
474    cmds: I,
475    interner: &'a <InternedCSName<u8> as CSName<u8>>::Handler,
476}
477impl<I: Iterator<Item = (InternedCSName<u8>, TeXCommand<Types>)>> Iterator
478    for CommandIterator<'_, I>
479{
480    type Item = (InternedCSName<u8>, TeXCommand<Types>);
481    fn next(&mut self) -> Option<Self::Item> {
482        loop {
483            if let Some((name, cmd)) = self.cmds.next() {
484                let bname = self.interner.resolve(name);
485                if self.prefixes.iter().any(|p| bname.starts_with(p)) {
486                    return Some((name, cmd));
487                }
488            } else {
489                return None;
490            }
491        }
492    }
493}