tectonic 0.1.2

A modernized, complete, embeddable TeX/LaTeX engine. Tectonic is forked from the XeTeX extension to the classic “Web2C” implementation of TeX and uses the TeXLive distribution of support files.
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
// src/engines/mod.rs -- interface to Tectonic engines written in C
// Copyright 2016-2017 the Tectonic Project
// Licensed under the MIT License.

use flate2::{Compression, GzBuilder};
use flate2::read::{GzDecoder};
use std::collections::HashMap;
use std::ffi::{OsStr, OsString};
use std::io::{Read, SeekFrom, Write};
use std::path::PathBuf;
use std::ptr;

use digest::DigestData;
use errors::Result;
use io::{IoProvider, IoStack, InputFeatures, InputHandle, OpenResult, OutputHandle};
use status::StatusBackend;
use self::file_format::{format_to_extension, FileFormat};


// Internal sub-modules. Some of these must be public so that their symbols are
// exported for the C library to see them. That could be changed if we gave
// the C code a struct with pointers to the functions.

mod c_api;
mod file_format;
pub mod io_api;
pub mod kpse_api;
pub mod md5_api;


// Public sub-modules and reexports.

pub mod tex;
pub mod xdvipdfmx;
pub mod bibtex;

pub use self::tex::TexEngine;
pub use self::xdvipdfmx::XdvipdfmxEngine;
pub use self::bibtex::BibtexEngine;


/// Different patterns with which files may have been accessed by the
/// underlying engines. Once a file is marked as ReadThenWritten or
/// WrittenThenRead, its pattern does not evolve further.
#[derive(Clone,Copy,Debug,Eq,PartialEq)]
pub enum AccessPattern {
    /// This file is only ever read.
    Read,

    /// This file is only ever written. This suggests that it is
    /// a final output of the processing session.
    Written,

    /// This file is read, then written. We call this a "circular" access
    /// pattern. Multiple passes of an engine will result in outputs that
    /// change if this file's contents change, or if the file did not exist at
    /// the time of the first pass.
    ReadThenWritten,

    /// This file is written, then read. We call this a "temporary" access
    /// pattern. This file is likely a temporary buffer that is not of
    /// interest to the user.
    WrittenThenRead,
}

/// A summary of the I/O that happened on a file. We record its access
/// pattern, the cryptographic digest of the file when it was last read, and
/// the cryptographic digest of the file as it was last written.
#[derive(Clone,Debug,Eq,PartialEq)]
pub struct FileSummary {
    pub access_pattern: AccessPattern,
    pub read_digest: Option<DigestData>,
    pub write_digest: Option<DigestData>,
}

impl FileSummary {
    pub fn new(access_pattern: AccessPattern) -> FileSummary {
        FileSummary {
            access_pattern: access_pattern,
            read_digest: None,
            write_digest: None,
        }
    }
}


// The private interface for executing various engines implemented in C.
//
// The C code relies on an enormous number of global variables so, despite our
// fancy API, we can only ever safely run one backend at a time in any given
// process. (For now.) Here we set up the infrastructure to manage this. Of
// course, this is totally un-thread-safe, etc., because the underlying C code
// is.

// The double-boxing of the handles in ExecutionState isn't nice. I *think*
// that in principle we could turn the inner boxes into pointers and pass
// those around. But I can't get it to work in practice -- it may be Boxes of
// trait objects become fat pointers themselves. It's really not a big deal so
// let's just roll with it for now.

struct ExecutionState<'a, I: 'a + IoProvider>  {
    io: &'a mut I,
    summaries: Option<&'a mut HashMap<OsString, FileSummary>>,
    status: &'a mut StatusBackend,
    input_handles: Vec<Box<InputHandle>>,
    output_handles: Vec<Box<OutputHandle>>,
}


impl<'a, I: 'a + IoProvider> ExecutionState<'a, I> {
    pub fn new (io: &'a mut I, summaries: Option<&'a mut HashMap<OsString, FileSummary>>,
                status: &'a mut StatusBackend) -> ExecutionState<'a, I> {
        ExecutionState {
            io: io,
            summaries: summaries,
            status: status,
            output_handles: Vec::new(),
            input_handles: Vec::new(),
        }
    }

    // Helpers.

    fn input_open_name_format(&mut self, name: &OsStr, format: FileFormat) -> OpenResult<InputHandle> {
        // TODO: for some formats we should check multiple extensions, not
        // just one.

        let r = self.io.input_open_name(name, self.status);
        if let OpenResult::NotAvailable = r {
        } else {
            return r;
        }

        // Maybe there's a nicer way to alter the extension without turning
        // `name` into a Path?

        let mut ext = PathBuf::from(name);
        let mut ename = OsString::from(match ext.file_name() {
            Some(s) => s,
            None => return OpenResult::NotAvailable
        });
        ename.push(format_to_extension(format));
        ext.set_file_name(ename);

        return self.io.input_open_name(&ext.into_os_string(), self.status);
    }

    fn input_open_name_format_gz(&mut self, name: &OsStr, format: FileFormat,
                                 is_gz: bool) -> OpenResult<InputHandle> {
        let base = self.input_open_name_format(name, format);

        if !is_gz {
            return base;
        }

        match base {
            OpenResult::Ok(ih) => {
                match GzDecoder::new(ih) {
                    Ok(dr) => OpenResult::Ok(InputHandle::new(name, dr)),
                    Err(e) => OpenResult::Err(e.into()),
                }
            },
            _ => base
        }
    }

    // These functions are called from C via `io_api`:

    fn output_open(&mut self, name: &OsStr, is_gz: bool) -> *const OutputHandle {
        let mut oh = match self.io.output_open_name(name) {
            OpenResult::Ok(oh) => oh,
            OpenResult::NotAvailable => return ptr::null(),
            OpenResult::Err(e) => {
                tt_warning!(self.status, "open of output {} failed", name.to_string_lossy(); e);
                return ptr::null()
            }
        };

        let name = oh.name().to_os_string(); // mainly for symmetry with input_open()

        if is_gz {
            oh = OutputHandle::new(&name, GzBuilder::new().write(oh, Compression::Default));
        }

        if let Some(ref mut summaries) = self.summaries {
            // Borrow-checker fight.
            if {
                if let Some(summ) = summaries.get_mut(&name) {
                    summ.access_pattern = match summ.access_pattern {
                        AccessPattern::Read => AccessPattern::ReadThenWritten,
                        c => c, // identity mapping makes sense for remaining options
                    };
                    false // no, do not insert a new item
                } else {
                    true // yes, insert a new item
                }
            } {
                // The 'else' branch above returned 'true'.
                summaries.insert(name, FileSummary::new(AccessPattern::Written));
            }
        }

        self.output_handles.push(Box::new(oh));
        &*self.output_handles[self.output_handles.len()-1]
    }

    fn output_open_stdout(&mut self) -> *const OutputHandle {
        let oh = match self.io.output_open_stdout() {
            OpenResult::Ok(oh) => oh,
            OpenResult::NotAvailable => return ptr::null(),
            OpenResult::Err(e) => {
                tt_warning!(self.status, "open of stdout failed"; e);
                return ptr::null()
            }
        };

        // Life is easier if we track stdout in the same way that we do other
        // output files.

        if let Some(ref mut summaries) = self.summaries {
            // Borrow-checker fight.
            if {
                if let Some(summ) = summaries.get_mut(OsStr::new("")) {
                    summ.access_pattern = match summ.access_pattern {
                        AccessPattern::Read => AccessPattern::ReadThenWritten,
                        c => c, // identity mapping makes sense for remaining options
                    };
                    false // no, do not insert a new item
                } else {
                    true // yes, insert a new item
                }
            } {
                // The 'else' branch above returned 'true'.
                summaries.insert(OsString::from(""), FileSummary::new(AccessPattern::Written));
            }
        }

        self.output_handles.push(Box::new(oh));
        &*self.output_handles[self.output_handles.len()-1]
    }

    fn output_write(&mut self, handle: *mut OutputHandle, buf: &[u8]) -> bool {
        let rhandle: &mut OutputHandle = unsafe { &mut *handle };
        let result = rhandle.write_all(buf);

        match result {
            Ok(_) => false,
            Err(e) => {
                tt_warning!(self.status, "write failed"; e.into());
                true
            }
        }
    }

    fn output_flush(&mut self, handle: *mut OutputHandle) -> bool {
        let rhandle: &mut OutputHandle = unsafe { &mut *handle };
        let result = rhandle.flush();

        match result {
            Ok(_) => false,
            Err(e) => {
                tt_warning!(self.status, "flush failed"; e.into());
                true
            }
        }
    }

    fn output_close(&mut self, handle: *mut OutputHandle) -> bool {
        let len = self.output_handles.len();

        for i in 0..len {
            let p: *const OutputHandle = &*self.output_handles[i];

            if p == handle {
                let oh = self.output_handles.swap_remove(i);
                let (name, digest) = oh.into_name_digest();

                if let Some(ref mut summaries) = self.summaries {
                    let mut summ = summaries.get_mut(&name).expect("closing file that wasn't opened?");
                    summ.write_digest = Some(digest);
                }

                break;
            }
        }

        false
    }

    fn input_open(&mut self, name: &OsStr, format: FileFormat, is_gz: bool) -> *const InputHandle {
        let ih = match self.input_open_name_format_gz(name, format, is_gz) {
            OpenResult::Ok(ih) => ih,
            OpenResult::NotAvailable => {
                // For the purposes of file access pattern tracking, an
                // attempt to open a nonexistent file counts as a read of a
                // zero-size file. I don't see how such a file could have
                // previously been written, but let's use the full update
                // logic just in case.

                if let Some(ref mut summaries) = self.summaries {
                    // Borrow-checker fight.
                    if {
                        if let Some(summ) = summaries.get_mut(name) {
                            summ.access_pattern = match summ.access_pattern {
                                AccessPattern::Written => AccessPattern::WrittenThenRead,
                                c => c, // identity mapping makes sense for remaining options
                            };
                            false // no, do not insert a new item
                        } else {
                            true // yes, insert a new item
                        }
                    } {
                        // The 'else' branch above returned 'true'. Unlike
                        // other cases, here we need to fill in the
                        // read_digest. `None` is not an appropriate value
                        // since, if the file is written and then read again
                        // later, the `None` will be overwritten; but what
                        // matters is the contents of the file the very first
                        // time it was read.
                        let mut fs = FileSummary::new(AccessPattern::Read);
                        fs.read_digest = Some(DigestData::of_nothing());
                        summaries.insert(name.to_os_string(), fs);
                    }
                }
                return ptr::null();
            },
            OpenResult::Err(e) => {
                tt_warning!(self.status, "open of input {} failed", name.to_string_lossy(); e);
                return ptr::null();
            },
        };

        let name = ih.name().to_os_string(); // final name may have had an extension added, etc.

        if let Some(ref mut summaries) = self.summaries {
            // Borrow-checker fight.
            if {
                if let Some(summ) = summaries.get_mut(&name) {
                    summ.access_pattern = match summ.access_pattern {
                        AccessPattern::Written => AccessPattern::WrittenThenRead,
                        c => c, // identity mapping makes sense for remaining options
                    };
                    false // no, do not insert a new item
                } else {
                    true // yes, insert a new item
                }
            } {
                // The 'else' branch above returned 'true'.
                summaries.insert(name, FileSummary::new(AccessPattern::Read));
            }
        }

        self.input_handles.push(Box::new(ih));
        &*self.input_handles[self.input_handles.len()-1]
    }

    fn input_get_size(&mut self, handle: *mut InputHandle) -> usize {
        let rhandle: &mut InputHandle = unsafe { &mut *handle };
        match rhandle.get_size() {
            Ok(s) => s,
            Err(e) => {
                tt_warning!(self.status, "failed to get the size of an input"; e);
                0
            }
        }
    }

    fn input_seek(&mut self, handle: *mut InputHandle, pos: SeekFrom) -> u64 {
        let rhandle: &mut InputHandle = unsafe { &mut *handle };
        match rhandle.try_seek(pos) {
            Ok(pos) => pos,
            Err(e) => {
                tt_warning!(self.status, "input seek failed"; e);
                0
            }
        }
    }

    fn input_read(&mut self, handle: *mut InputHandle, buf: &mut [u8]) -> Result<()> {
        let rhandle: &mut InputHandle = unsafe { &mut *handle };
        Ok(rhandle.read_exact(buf)?)
    }

    fn input_close(&mut self, handle: *mut InputHandle) -> bool {
        let len = self.input_handles.len();

        for i in 0..len {
            let p: *const InputHandle = &*self.input_handles[i];

            if p == handle {
                let ih = self.input_handles.swap_remove(i);
                let (name, digest_opt) = ih.into_name_digest();

                if let Some(ref mut summaries) = self.summaries {
                    let mut summ = summaries.get_mut(&name).expect("closing file that wasn't opened?");

                    // It's what was in the file the *first* time that it was
                    // read that matters, so don't replace the read digest if
                    // it's already got one.

                    if summ.read_digest.is_none() {
                        summ.read_digest = digest_opt;
                    }
                }
                return false;
            }
        }

        panic!("unexpected handle {:?}", handle);
    }
}


// Here's the hacky framework for letting the C code get back an ExecutionState.

// note: ptr::null_mut() gives me a compile error related to const fns right now.
static mut GLOBAL_ENGINE_PTR: *mut () = 0 as *mut _;

// This wraps a Rust function called by the C code via some ttstub_*() function.
fn with_global_state<F, T> (f: F) -> T where F: FnOnce(&mut ExecutionState<IoStack>) -> T {
    unsafe { f(&mut *(GLOBAL_ENGINE_PTR as *mut ExecutionState<IoStack>)) }
}

// This wraps any activities that cause the C code to spin up.
unsafe fn assign_global_state<F, T> (state: &mut ExecutionState<IoStack>, f: F) -> T where F: FnOnce() -> T {
    GLOBAL_ENGINE_PTR = state as *mut ExecutionState<IoStack> as *mut ();
    let rv = f();
    GLOBAL_ENGINE_PTR = 0 as *mut _;
    rv
}