use byte_unit::{Byte, UnitType};
use quick_xml::{events::Event, NsReader};
use std::{
collections::{HashMap, HashSet},
fs::File,
io::{Cursor, Read, Write},
path::{Path, PathBuf},
process::Command,
rc::Rc,
result::Result as StdResult,
str::FromStr,
time::{Duration, SystemTime},
};
use tectonic_bridge_core::{CoreBridgeLauncher, DriverHooks, SecuritySettings, SystemRequestError};
use tectonic_bundles::Bundle;
use tectonic_engine_spx2html::AssetSpecification;
use tectonic_io_base::{
digest::DigestData,
filesystem::{FilesystemIo, FilesystemPrimaryInputIo},
stdstreams::{BufferedPrimaryIo, GenuineStdoutIo},
InputHandle, IoProvider, OpenResult, OutputHandle,
};
use which::which;
use crate::{
ctry, errmsg,
errors::{ChainErrCompatExt, ErrorKind, Result},
io::{
format_cache::FormatCache,
memory::{MemoryFileCollection, MemoryIo},
InputOrigin,
},
status::StatusBackend,
tt_error, tt_note, tt_warning,
unstable_opts::UnstableOptions,
BibtexEngine, Spx2HtmlEngine, TexEngine, TexOutcome, XdvipdfmxEngine,
};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum AccessPattern {
Read,
Written,
ReadThenWritten,
WrittenThenRead,
}
#[derive(Clone, Debug, Eq, PartialEq)]
struct FileSummary {
access_pattern: AccessPattern,
pub input_origin: InputOrigin,
pub read_digest: Option<DigestData>,
pub write_digest: Option<DigestData>,
got_written_to_disk: bool,
}
impl FileSummary {
fn new(access_pattern: AccessPattern, input_origin: InputOrigin) -> FileSummary {
FileSummary {
access_pattern,
input_origin,
read_digest: None,
write_digest: None,
got_written_to_disk: false,
}
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum OutputFormat {
Aux,
Html,
Xdv,
#[default]
Pdf,
Format,
}
impl FromStr for OutputFormat {
type Err = &'static str;
fn from_str(a_str: &str) -> StdResult<Self, Self::Err> {
match a_str {
"aux" => Ok(OutputFormat::Aux),
"html" => Ok(OutputFormat::Html),
"xdv" => Ok(OutputFormat::Xdv),
"pdf" => Ok(OutputFormat::Pdf),
"fmt" => Ok(OutputFormat::Format),
_ => Err("unsupported or unknown format"),
}
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum PassSetting {
#[default]
Default,
Tex,
BibtexFirst,
}
impl FromStr for PassSetting {
type Err = &'static str;
fn from_str(a_str: &str) -> StdResult<Self, Self::Err> {
match a_str {
"default" => Ok(PassSetting::Default),
"bibtex_first" => Ok(PassSetting::BibtexFirst),
"tex" => Ok(PassSetting::Tex),
_ => Err("unsupported or unknown pass setting"),
}
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
enum PrimaryInputMode {
#[default]
Stdin,
Path(PathBuf),
Buffer(Vec<u8>),
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
enum OutputDestination {
#[default]
Default,
Path(PathBuf),
Nowhere,
}
struct BridgeState {
primary_input: Box<dyn IoProvider>,
bundle: Box<dyn Bundle>,
mem: MemoryIo,
filesystem: FilesystemIo,
extra_search_paths: Vec<FilesystemIo>,
shell_escape_work: Option<FilesystemIo>,
format_cache: FormatCache,
genuine_stdout: Option<GenuineStdoutIo>,
format_primary: Option<BufferedPrimaryIo>,
events: HashMap<String, FileSummary>,
}
impl BridgeState {
fn enter_format_mode(&mut self, format_file_name: &str) {
self.format_primary = Some(BufferedPrimaryIo::from_text(format!(
"\\input {format_file_name}"
)));
}
fn leave_format_mode(&mut self) {
self.format_primary = None;
}
fn external_tool_pass(
&mut self,
tool: &ExternalToolPass,
status: &mut dyn StatusBackend,
) -> Result<()> {
status.note_highlighted("Running external tool ", &tool.argv[0], " ...");
let mut cmd = Command::new(&tool.argv[0]);
let mut read_files = tool.extra_requires.clone();
{
let mem_files = &*self.mem.files.borrow();
for arg in &tool.argv[1..] {
cmd.arg(arg);
if mem_files.contains_key(arg) {
read_files.insert(arg.to_owned());
}
}
}
let tempdir = ctry!(
tempfile::Builder::new().tempdir();
"can't create temporary directory for external tool"
);
{
for name in &read_files {
if name.contains("../") {
return Err(errmsg!(
"relative parent paths are not supported for the \
external tool. Got path `{}`.",
name
));
}
let mut ih = ctry!(
self.input_open_name(name, status).must_exist();
"can't open path `{}`", name
);
let path = Path::new(name);
if path.is_absolute() {
continue;
}
let tool_path = tempdir.path().join(name);
let tool_parent = tool_path.parent().unwrap();
if tool_parent != tempdir.path() {
ctry!(
std::fs::create_dir_all(tool_parent);
"failed to create sub directory `{}`", tool_parent.display()
);
}
let mut f = ctry!(
File::create(&tool_path);
"failed to create file `{}`", tool_path.display()
);
ctry!(
std::io::copy(&mut ih, &mut f);
"failed to write file `{}`", tool_path.display()
);
}
}
let output = cmd.current_dir(tempdir.path()).output()?;
if let Some(0) = output.status.code() {
} else {
tt_error!(
status,
"the external tool exited with an error code; its stdout was:\n"
);
status.dump_error_logs(&output.stdout[..]);
tt_error!(status, "its stderr was:\n");
status.dump_error_logs(&output.stderr[..]);
return if let Some(n) = output.status.code() {
Err(errmsg!("the external tool exited with error code {}", n))
} else {
Err(errmsg!("the external tool was terminated by a signal"))
};
}
for entry in std::fs::read_dir(tempdir.path())? {
let entry = entry?;
if !entry.file_type()?.is_file() {
continue;
}
if let Some(basename) = entry.file_name().to_str() {
if !self.mem.files.borrow().contains_key(basename) {
let path = entry.path();
let mut data = Vec::new();
let mut f = ctry!(
File::open(&path);
"failed to open tool-created file `{}`", path.display()
);
ctry!(
f.read_to_end(&mut data);
"failed to read tool-created file `{}`", path.display()
);
self.mem.create_entry(basename, data);
self.events.insert(
basename.to_owned(),
FileSummary::new(AccessPattern::Written, InputOrigin::NotInput),
);
}
}
}
for name in &read_files {
let summ = self.events.get_mut(name).unwrap();
summ.access_pattern = match summ.access_pattern {
AccessPattern::Written => AccessPattern::WrittenThenRead,
c => c, };
}
Ok(())
}
fn get_intermediate_file_names(&self) -> Vec<String> {
return self.mem.files.borrow().keys().cloned().collect();
}
}
macro_rules! bridgestate_ioprovider_try {
($provider:expr, $($inner:tt)+) => {
let r = $provider.$($inner)+;
match r {
OpenResult::NotAvailable => {},
_ => return r,
};
}
}
macro_rules! bridgestate_ioprovider_cascade {
($self:ident, $($inner:tt)+) => {
if let Some(ref mut p) = $self.genuine_stdout {
bridgestate_ioprovider_try!(p, $($inner)+);
}
let use_fs = if let Some(ref mut p) = $self.format_primary {
bridgestate_ioprovider_try!(p, $($inner)+);
false
} else {
bridgestate_ioprovider_try!($self.primary_input, $($inner)+);
true
};
bridgestate_ioprovider_try!($self.mem, $($inner)+);
if use_fs {
bridgestate_ioprovider_try!($self.filesystem, $($inner)+);
if let Some(ref mut p) = $self.shell_escape_work {
bridgestate_ioprovider_try!(p, $($inner)+);
}
for fsio in $self.extra_search_paths.iter_mut() {
bridgestate_ioprovider_try!(fsio, $($inner)+);
}
}
bridgestate_ioprovider_try!($self.bundle.as_ioprovider_mut(), $($inner)+);
bridgestate_ioprovider_try!($self.format_cache, $($inner)+);
return OpenResult::NotAvailable;
}
}
impl IoProvider for BridgeState {
fn output_open_name(&mut self, name: &str) -> OpenResult<OutputHandle> {
let r = (|| {
bridgestate_ioprovider_cascade!(self, output_open_name(name));
})();
if let OpenResult::Ok(_) = r {
if let Some(summ) = self.events.get_mut(name) {
summ.access_pattern = match summ.access_pattern {
AccessPattern::Read => AccessPattern::ReadThenWritten,
c => c, };
} else {
self.events.insert(
name.to_owned(),
FileSummary::new(AccessPattern::Written, InputOrigin::NotInput),
);
}
}
r
}
fn output_open_stdout(&mut self) -> OpenResult<OutputHandle> {
let r = (|| {
bridgestate_ioprovider_cascade!(self, output_open_stdout());
})();
if let OpenResult::Ok(_) = r {
if let Some(summ) = self.events.get_mut("") {
summ.access_pattern = match summ.access_pattern {
AccessPattern::Read => AccessPattern::ReadThenWritten,
c => c, };
} else {
self.events.insert(
String::from(""),
FileSummary::new(AccessPattern::Written, InputOrigin::NotInput),
);
}
}
r
}
fn input_open_name(
&mut self,
name: &str,
status: &mut dyn StatusBackend,
) -> OpenResult<InputHandle> {
match self.input_open_name_with_abspath(name, status) {
OpenResult::Ok((ih, _path)) => OpenResult::Ok(ih),
OpenResult::Err(e) => OpenResult::Err(e),
OpenResult::NotAvailable => OpenResult::NotAvailable,
}
}
fn input_open_name_with_abspath(
&mut self,
name: &str,
status: &mut dyn StatusBackend,
) -> OpenResult<(InputHandle, Option<PathBuf>)> {
let r = (|| {
bridgestate_ioprovider_cascade!(self, input_open_name_with_abspath(name, status));
})();
match r {
OpenResult::Ok((ref ih, ref _path)) => {
if let Some(summ) = self.events.get_mut(name) {
summ.access_pattern = match summ.access_pattern {
AccessPattern::Written => AccessPattern::WrittenThenRead,
c => c, };
} else {
self.events.insert(
name.to_owned(),
FileSummary::new(AccessPattern::Read, ih.origin()),
);
}
}
OpenResult::NotAvailable => {
if let Some(summ) = self.events.get_mut(name) {
summ.access_pattern = match summ.access_pattern {
AccessPattern::Written => AccessPattern::WrittenThenRead,
c => c, };
} else {
let mut fs = FileSummary::new(AccessPattern::Read, InputOrigin::NotInput);
fs.read_digest = Some(DigestData::of_nothing());
self.events.insert(name.to_owned(), fs);
}
}
OpenResult::Err(_) => {}
}
r
}
fn input_open_primary(&mut self, status: &mut dyn StatusBackend) -> OpenResult<InputHandle> {
match self.input_open_primary_with_abspath(status) {
OpenResult::Ok((ih, _path)) => OpenResult::Ok(ih),
OpenResult::Err(e) => OpenResult::Err(e),
OpenResult::NotAvailable => OpenResult::NotAvailable,
}
}
fn input_open_primary_with_abspath(
&mut self,
status: &mut dyn StatusBackend,
) -> OpenResult<(InputHandle, Option<PathBuf>)> {
bridgestate_ioprovider_cascade!(self, input_open_primary_with_abspath(status));
}
fn input_open_format(
&mut self,
name: &str,
status: &mut dyn StatusBackend,
) -> OpenResult<InputHandle> {
let r = (|| {
bridgestate_ioprovider_cascade!(self, input_open_format(name, status));
})();
if let OpenResult::Ok(ref ih) = r {
if let Some(summ) = self.events.get_mut(name) {
summ.access_pattern = match summ.access_pattern {
AccessPattern::Written => AccessPattern::WrittenThenRead,
c => c, };
} else {
self.events.insert(
name.to_owned(),
FileSummary::new(AccessPattern::Read, ih.origin()),
);
}
}
r
}
}
impl DriverHooks for BridgeState {
fn io(&mut self) -> &mut dyn IoProvider {
self
}
fn event_output_closed(&mut self, name: String, digest: DigestData) {
let summ = self
.events
.get_mut(&name)
.expect("closing file that wasn't opened?");
summ.write_digest = Some(digest);
}
fn event_input_closed(
&mut self,
name: String,
digest: Option<DigestData>,
_status: &mut dyn StatusBackend,
) {
let summ = self
.events
.get_mut(&name)
.expect("closing file that wasn't opened?");
if summ.read_digest.is_none() {
summ.read_digest = digest;
}
}
fn sysrq_shell_escape(
&mut self,
command: &str,
status: &mut dyn StatusBackend,
) -> StdResult<(), SystemRequestError> {
#[cfg(unix)]
const SHELL: &[&str] = &["sh", "-c"];
#[cfg(windows)]
const SHELL: &[&str] = &["cmd.exe", "/c"];
if let Some(work) = self.shell_escape_work.as_ref() {
for (name, file) in &*self.mem.files.borrow() {
if name == self.mem.stdout_key() {
continue;
}
let real_path = work.root().join(name);
if let Some(prefix) = real_path.parent() {
std::fs::create_dir_all(prefix).map_err(|e| {
tt_error!(status, "failed to create sub directory `{}`", prefix.display(); e.into());
SystemRequestError::Failed
})?;
}
let mut f = File::create(&real_path).map_err(|e| {
tt_error!(status, "failed to create file `{}`", real_path.display(); e.into());
SystemRequestError::Failed
})?;
f.write_all(&file.data).map_err(|e| {
tt_error!(status, "failed to write file `{}`", real_path.display(); e.into());
SystemRequestError::Failed
})?;
}
tt_note!(status, "running shell command: `{}`", command);
match Command::new(SHELL[0])
.args(&SHELL[1..])
.arg(command)
.current_dir(work.root())
.status()
{
Ok(s) => match s.code() {
Some(0) => Ok(()),
Some(n) => {
tt_warning!(status, "command exited with error code {}", n);
Err(SystemRequestError::Failed)
}
None => {
tt_warning!(status, "command was terminated by signal");
Err(SystemRequestError::Failed)
}
},
Err(err) => {
tt_warning!(status, "failed to run command"; err.into());
Err(SystemRequestError::Failed)
}
}
} else {
tt_error!(
status,
"the engine requested a shell-escape invocation but it's currently disabled"
);
Err(SystemRequestError::NotAllowed)
}
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
enum ShellEscapeMode {
#[default]
Defaulted,
Disabled,
TempDir,
ExternallyManagedDir(PathBuf),
}
#[derive(Debug)]
struct ExternalToolPass {
argv: Vec<String>,
extra_requires: HashSet<String>,
}
#[derive(Default)]
pub struct ProcessingSessionBuilder {
security: SecuritySettings,
primary_input: PrimaryInputMode,
tex_input_name: Option<String>,
output_dest: OutputDestination,
filesystem_root: Option<PathBuf>,
format_name: Option<String>,
format_cache_path: Option<PathBuf>,
output_format: OutputFormat,
makefile_output_path: Option<PathBuf>,
hidden_input_paths: HashSet<PathBuf>,
pass: PassSetting,
reruns: Option<usize>,
print_stdout: bool,
bundle: Option<Box<dyn Bundle>>,
keep_intermediates: bool,
keep_logs: bool,
synctex: bool,
build_date: Option<SystemTime>,
unstables: UnstableOptions,
shell_escape_mode: ShellEscapeMode,
html_assets_spec_path: Option<String>,
html_precomputed_assets: Option<AssetSpecification>,
html_do_not_emit_files: bool,
html_do_not_emit_assets: bool,
}
impl ProcessingSessionBuilder {
pub fn new_with_security(security: SecuritySettings) -> Self {
ProcessingSessionBuilder {
security,
..Default::default()
}
}
pub fn primary_input_path<P: AsRef<Path>>(&mut self, p: P) -> &mut Self {
self.primary_input = PrimaryInputMode::Path(p.as_ref().to_owned());
self
}
pub fn primary_input_buffer(&mut self, buf: &[u8]) -> &mut Self {
self.primary_input = PrimaryInputMode::Buffer(buf.to_owned());
self
}
pub fn tex_input_name(&mut self, s: &str) -> &mut Self {
self.tex_input_name = Some(s.to_owned());
self
}
pub fn filesystem_root<P: AsRef<Path>>(&mut self, p: P) -> &mut Self {
self.filesystem_root = Some(p.as_ref().to_owned());
self
}
pub fn output_dir<P: AsRef<Path>>(&mut self, p: P) -> &mut Self {
self.output_dest = OutputDestination::Path(p.as_ref().to_owned());
self
}
pub fn do_not_write_output_files(&mut self) -> &mut Self {
self.output_dest = OutputDestination::Nowhere;
self
}
pub fn format_name(&mut self, p: &str) -> &mut Self {
self.format_name = Some(p.to_owned());
self
}
pub fn format_cache_path<P: AsRef<Path>>(&mut self, p: P) -> &mut Self {
self.format_cache_path = Some(p.as_ref().to_owned());
self
}
pub fn output_format(&mut self, f: OutputFormat) -> &mut Self {
self.output_format = f;
self
}
pub fn makefile_output_path<P: AsRef<Path>>(&mut self, p: P) -> &mut Self {
self.makefile_output_path = Some(p.as_ref().to_owned());
self
}
pub fn pass(&mut self, p: PassSetting) -> &mut Self {
self.pass = p;
self
}
pub fn reruns(&mut self, r: usize) -> &mut Self {
self.reruns = Some(r);
self
}
pub fn print_stdout(&mut self, p: bool) -> &mut Self {
self.print_stdout = p;
self
}
pub fn hide<P: AsRef<Path>>(&mut self, p: P) -> &mut Self {
self.hidden_input_paths.insert(p.as_ref().to_owned());
self
}
pub fn bundle(&mut self, b: Box<dyn Bundle>) -> &mut Self {
self.bundle = Some(b);
self
}
pub fn keep_intermediates(&mut self, k: bool) -> &mut Self {
self.keep_intermediates = k;
self
}
pub fn keep_logs(&mut self, k: bool) -> &mut Self {
self.keep_logs = k;
self
}
pub fn synctex(&mut self, s: bool) -> &mut Self {
self.synctex = s;
self
}
pub fn build_date(&mut self, date: SystemTime) -> &mut Self {
self.build_date = Some(date);
self
}
pub fn build_date_from_env(&mut self, force_deterministic: bool) -> &mut Self {
let build_date_str = std::env::var("SOURCE_DATE_EPOCH").ok();
let build_date = match (force_deterministic, build_date_str) {
(_, Some(s)) => {
let epoch = s
.parse::<u64>()
.expect("invalid SOURCE_DATE_EPOCH (not a number)");
SystemTime::UNIX_EPOCH
.checked_add(Duration::from_secs(epoch))
.expect("time overflow")
}
(true, None) => SystemTime::UNIX_EPOCH,
(false, None) => SystemTime::now(),
};
self.build_date(build_date)
}
pub fn unstables(&mut self, opts: UnstableOptions) -> &mut Self {
self.unstables = opts;
self
}
pub fn shell_escape_with_work_dir<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
if self.security.allow_shell_escape() {
self.shell_escape_mode =
ShellEscapeMode::ExternallyManagedDir(path.as_ref().to_owned());
}
self
}
pub fn shell_escape_with_temp_dir(&mut self) -> &mut Self {
if self.security.allow_shell_escape() {
self.shell_escape_mode = ShellEscapeMode::TempDir;
}
self
}
pub fn shell_escape_disabled(&mut self) -> &mut Self {
self.shell_escape_mode = ShellEscapeMode::Disabled;
self
}
pub fn html_assets_spec_path<S: ToString>(&mut self, path: S) -> &mut Self {
self.html_assets_spec_path = Some(path.to_string());
self
}
pub fn html_precomputed_assets(&mut self, assets: AssetSpecification) -> &mut Self {
self.html_precomputed_assets = Some(assets);
self
}
pub fn html_emit_files(&mut self, do_emit: bool) -> &mut Self {
self.html_do_not_emit_files = !do_emit;
self
}
pub fn html_emit_assets(&mut self, do_emit: bool) -> &mut Self {
self.html_do_not_emit_assets = !do_emit;
self
}
pub fn create(self, status: &mut dyn StatusBackend) -> Result<ProcessingSession> {
let mut bundle = self.bundle.expect("a bundle must be specified");
let mut filesystem_root = self.filesystem_root.unwrap_or_default();
let (pio, primary_input_path, default_output_path) = match self.primary_input {
PrimaryInputMode::Path(p) => {
let parent = match p.parent() {
Some(parent) => parent.to_owned(),
None => {
return Err(errmsg!(
"can't figure out a parent directory for input path \"{}\"",
p.display()
));
}
};
filesystem_root.clone_from(&parent);
let pio: Box<dyn IoProvider> = Box::new(FilesystemPrimaryInputIo::new(&p));
(pio, Some(p), parent)
}
PrimaryInputMode::Stdin => {
let pio = ctry!(BufferedPrimaryIo::from_stdin(); "error reading standard input");
let pio: Box<dyn IoProvider> = Box::new(pio);
(pio, None, "".into())
}
PrimaryInputMode::Buffer(buf) => {
let pio: Box<dyn IoProvider> = Box::new(BufferedPrimaryIo::from_buffer(buf));
(pio, None, "".into())
}
};
let format_cache_path = self
.format_cache_path
.unwrap_or_else(|| filesystem_root.clone());
let format_cache = FormatCache::new(bundle.get_digest()?, format_cache_path);
let genuine_stdout = if self.print_stdout {
Some(GenuineStdoutIo::new())
} else {
None
};
let hidden_input_paths = self.hidden_input_paths;
let extra_search_paths = if self.security.allow_extra_search_paths() {
self.unstables
.extra_search_paths
.iter()
.map(|p| FilesystemIo::new(p, false, false, hidden_input_paths.clone()))
.collect()
} else {
if !self.unstables.extra_search_paths.is_empty() {
tt_warning!(status, "Extra search path(s) ignored due to security");
}
Vec::new()
};
let filesystem = FilesystemIo::new(&filesystem_root, false, true, hidden_input_paths);
let mem = MemoryIo::new(true);
let bs = BridgeState {
primary_input: pio,
mem,
filesystem,
extra_search_paths,
shell_escape_work: None,
format_cache,
bundle,
genuine_stdout,
format_primary: None,
events: HashMap::new(),
};
let output_path = match self.output_dest {
OutputDestination::Default => Some(default_output_path),
OutputDestination::Path(p) => Some(p),
OutputDestination::Nowhere => None,
};
let tex_input_name = self
.tex_input_name
.expect("tex_input_name must be specified");
let mut aux_path = PathBuf::from(tex_input_name.clone());
aux_path.set_extension("aux");
let mut xdv_path = aux_path.clone();
xdv_path.set_extension(if self.output_format == OutputFormat::Html {
"spx"
} else {
"xdv"
});
let mut pdf_path = aux_path.clone();
pdf_path.set_extension("pdf");
let shell_escape_mode = if !self.security.allow_shell_escape() {
ShellEscapeMode::Disabled
} else {
match self.shell_escape_mode {
ShellEscapeMode::Defaulted => {
if let Some(ref cwd) = self.unstables.shell_escape_cwd {
ShellEscapeMode::ExternallyManagedDir(cwd.into())
} else if self.unstables.shell_escape {
ShellEscapeMode::TempDir
} else {
ShellEscapeMode::Disabled
}
}
other => other,
}
};
Ok(ProcessingSession {
security: self.security,
bs,
pass: self.pass,
primary_input_path,
primary_input_tex_path: tex_input_name,
format_name: self.format_name.unwrap(),
tex_aux_path: aux_path.display().to_string(),
tex_xdv_path: xdv_path.display().to_string(),
tex_pdf_path: pdf_path.display().to_string(),
output_format: self.output_format,
makefile_output_path: self.makefile_output_path,
output_path,
tex_rerun_specification: self.reruns,
keep_intermediates: self.keep_intermediates,
keep_logs: self.keep_logs,
synctex_enabled: self.synctex,
build_date: self.build_date.unwrap_or(SystemTime::UNIX_EPOCH),
unstables: self.unstables,
shell_escape_mode,
html_assets_spec_path: self.html_assets_spec_path,
html_precomputed_assets: self.html_precomputed_assets,
html_emit_files: !self.html_do_not_emit_files,
html_emit_assets: !self.html_do_not_emit_assets,
})
}
}
#[derive(Debug, Clone)]
enum RerunReason {
Biber,
Bibtex,
FileChange(String),
}
pub struct ProcessingSession {
security: SecuritySettings,
bs: BridgeState,
primary_input_path: Option<PathBuf>,
primary_input_tex_path: String,
format_name: String,
tex_aux_path: String,
tex_xdv_path: String,
tex_pdf_path: String,
makefile_output_path: Option<PathBuf>,
output_path: Option<PathBuf>,
pass: PassSetting,
output_format: OutputFormat,
tex_rerun_specification: Option<usize>,
keep_intermediates: bool,
keep_logs: bool,
synctex_enabled: bool,
build_date: SystemTime,
unstables: UnstableOptions,
shell_escape_mode: ShellEscapeMode,
html_assets_spec_path: Option<String>,
html_precomputed_assets: Option<AssetSpecification>,
html_emit_files: bool,
html_emit_assets: bool,
}
const DEFAULT_MAX_TEX_PASSES: usize = 6;
const ALWAYS_INTERMEDIATE_EXTENSIONS: &[&str] = &[
".snm", ".toc", ];
impl ProcessingSession {
fn is_rerun_needed(&self, status: &mut dyn StatusBackend) -> Option<RerunReason> {
for (name, info) in &self.bs.events {
if info.access_pattern == AccessPattern::ReadThenWritten {
let file_changed = match (&info.read_digest, &info.write_digest) {
(Some(d1), Some(d2)) => d1 != d2,
(&None, &Some(_)) => true,
(_, _) => {
tt_warning!(
status,
"internal consistency problem when checking if {} changed",
name
);
true
}
};
if file_changed {
return Some(RerunReason::FileChange(name.clone()));
}
}
}
None
}
#[allow(dead_code)]
fn _dump_access_info(&self, status: &mut dyn StatusBackend) {
for (name, info) in &self.bs.events {
if info.access_pattern != AccessPattern::Read {
let r = match info.read_digest {
Some(ref d) => d.to_string(),
None => "-".into(),
};
let w = match info.write_digest {
Some(ref d) => d.to_string(),
None => "-".into(),
};
tt_note!(
status,
"ACCESS: {} {:?} {:?} {:?}",
name,
info.access_pattern,
r,
w
);
}
}
}
pub fn run(&mut self, status: &mut dyn StatusBackend) -> Result<()> {
let (shell_escape_work, clean_up_shell_escape) = match self.shell_escape_mode {
ShellEscapeMode::Disabled => (None, false),
ShellEscapeMode::ExternallyManagedDir(ref p) => (
Some(FilesystemIo::new(p, false, false, HashSet::new())),
false,
),
ShellEscapeMode::TempDir => {
let tempdir = ctry!(tempfile::Builder::new().tempdir(); "can't create temporary directory for shell-escape work");
(
Some(FilesystemIo::new(
&tempdir.keep(),
false,
false,
HashSet::new(),
)),
true,
)
}
ShellEscapeMode::Defaulted => unreachable!(),
};
self.bs.shell_escape_work = shell_escape_work;
let result = self.run_inner(status);
if clean_up_shell_escape {
let shell_escape_work = self.bs.shell_escape_work.take().unwrap();
let shell_escape_err = std::fs::remove_dir_all(shell_escape_work.root());
if let Err(e) = shell_escape_err {
tt_warning!(status, "an error occurred while cleaning up the \
shell-escape temporary directory `{}`", shell_escape_work.root().display(); e.into());
}
}
result
}
fn run_inner(&mut self, status: &mut dyn StatusBackend) -> Result<()> {
let generate_format = if self.output_format == OutputFormat::Format {
false
} else {
match self.bs.input_open_format(&self.format_name, status) {
OpenResult::Ok(_) => false,
OpenResult::NotAvailable => true,
OpenResult::Err(e) => {
return Err(e)
.chain_err(|| format!("could not open format file {}", self.format_name));
}
}
};
if generate_format {
tt_note!(status, "generating format \"{}\"", self.format_name);
self.make_format_pass(status)?;
}
let result = match self.pass {
PassSetting::Tex => match self.tex_pass(None, status) {
Ok(Some(warnings)) => {
tt_warning!(status, "{}", warnings);
Ok(0)
}
Ok(None) => Ok(0),
Err(e) => Err(e),
},
PassSetting::Default => self.default_pass(false, status),
PassSetting::BibtexFirst => self.default_pass(true, status),
};
if let Err(e) = result {
self.write_files(None, status, true)?;
return Err(e);
};
let mut mf_dest_maybe = match self.makefile_output_path {
Some(ref p) => {
if self.output_path.is_none() {
tt_warning!(
status,
"requested to generate Makefile rules, but no files written to disk!"
);
None
} else {
Some(File::create(p)?)
}
}
None => None,
};
let n_skipped_intermediates = self.write_files(mf_dest_maybe.as_mut(), status, false)?;
if n_skipped_intermediates > 0 {
status.note_highlighted(
"Skipped writing ",
&format!("{n_skipped_intermediates}"),
" intermediate files (use --keep-intermediates to keep them)",
);
}
if let Some(ref mut mf_dest) = mf_dest_maybe {
ctry!(write!(mf_dest, ": "); "couldn't write to Makefile-rules file");
if let Some(ref pip) = self.primary_input_path {
let opip = ctry!(pip.to_str(); "Makefile-rules file path must be Unicode-able");
ctry!(mf_dest.write_all(opip.as_bytes()); "couldn't write to Makefile-rules file");
}
let root = self.output_path.as_ref().unwrap();
for (name, info) in &self.bs.events {
if info.input_origin != InputOrigin::Filesystem {
continue;
}
if info.got_written_to_disk {
tt_warning!(status, "omitting circular Makefile dependency for {}", name);
continue;
}
ctry!(write!(mf_dest, " \\\n {}", root.join(name).display()); "couldn't write to Makefile-rules file");
}
ctry!(writeln!(mf_dest, ""); "couldn't write to Makefile-rules file");
}
Ok(())
}
fn write_files(
&mut self,
mut mf_dest_maybe: Option<&mut File>,
status: &mut dyn StatusBackend,
only_logs: bool,
) -> Result<u32> {
let root = match self.output_path {
Some(ref p) => p,
None => {
return Ok(0);
}
};
let mut n_skipped_intermediates = 0;
for (name, file) in &*self.bs.mem.files.borrow() {
if name == self.bs.mem.stdout_key() {
continue;
}
let sname = name;
let summ = self.bs.events.get_mut(name).unwrap();
if !only_logs && (self.output_format == OutputFormat::Aux) {
if !sname.ends_with(".aux") {
continue;
}
} else if !self.keep_intermediates
&& (summ.access_pattern != AccessPattern::Written
|| ALWAYS_INTERMEDIATE_EXTENSIONS
.iter()
.any(|ext| sname.ends_with(ext)))
{
n_skipped_intermediates += 1;
continue;
}
let is_logfile = sname.ends_with(".log") || sname.ends_with(".blg");
if is_logfile && !self.keep_logs {
continue;
}
if !is_logfile && only_logs {
continue;
}
if file.data.is_empty() {
status.note_highlighted(
"Not writing ",
&format!("`{sname}`"),
": it would be empty.",
);
continue;
}
let real_path = root.join(name);
let byte_len = Byte::from_u128(file.data.len() as u128).unwrap();
status.note_highlighted(
"Writing ",
&format!("`{}`", real_path.display()),
&format!(" ({})", byte_len.get_appropriate_unit(UnitType::Binary)),
);
if let Some(parent) = real_path.parent() {
std::fs::create_dir_all(parent)?;
}
let mut f = File::create(&real_path)?;
f.write_all(&file.data)?;
summ.got_written_to_disk = true;
if let Some(ref mut mf_dest) = mf_dest_maybe {
ctry!(write!(mf_dest, "{} ", real_path.display()); "couldn't write to Makefile-rules file");
}
}
Ok(n_skipped_intermediates)
}
fn default_pass(&mut self, bibtex_first: bool, status: &mut dyn StatusBackend) -> Result<i32> {
let mut warnings = None;
let mut rerun_result = if bibtex_first {
self.bibtex_pass(status)?;
Some(RerunReason::Bibtex)
} else {
warnings = self.tex_pass(None, status)?;
let maybe_biber = self.check_biber_requirement(status)?;
if let Some(biber) = maybe_biber {
self.bs.external_tool_pass(&biber, status)?;
Some(RerunReason::Biber)
} else if self.is_bibtex_needed() {
self.bibtex_pass(status)?;
Some(RerunReason::Bibtex)
} else {
self.is_rerun_needed(status)
}
};
let (pass_count, reruns_fixed) = match self.tex_rerun_specification {
Some(n) => (n, true),
None => (DEFAULT_MAX_TEX_PASSES, false),
};
for i in 0..pass_count {
let rerun_explanation = if reruns_fixed {
"I was told to".to_owned()
} else {
match rerun_result {
Some(RerunReason::Biber) => "biber was run".to_owned(),
Some(RerunReason::Bibtex) => "bibtex was run".to_owned(),
Some(RerunReason::FileChange(ref s)) => format!("\"{s}\" changed"),
None => break,
}
};
for summ in self.bs.events.values_mut() {
summ.read_digest = None;
}
warnings = self.tex_pass(Some(&rerun_explanation), status)?;
if !reruns_fixed {
rerun_result = self.is_rerun_needed(status);
if rerun_result.is_some() && i == DEFAULT_MAX_TEX_PASSES - 1 {
tt_warning!(
status,
"TeX rerun seems needed, but stopping at {} passes",
DEFAULT_MAX_TEX_PASSES
);
break;
}
}
}
if let Some(warnings) = warnings {
tt_warning!(status, "{}", warnings);
}
if let OutputFormat::Pdf = self.output_format {
self.xdvipdfmx_pass(status)?;
} else if let OutputFormat::Html = self.output_format {
self.spx2html_pass(status)?;
}
Ok(0)
}
fn is_bibtex_needed(&self) -> bool {
const BIBDATA: &[u8] = b"\\bibdata";
self.bs
.mem
.files
.borrow()
.get(&self.tex_aux_path)
.map(|file| {
file.data.windows(BIBDATA.len()).any(|s| s == BIBDATA)
})
.unwrap_or(false)
}
#[allow(clippy::manual_split_once)] fn make_format_pass(&mut self, status: &mut dyn StatusBackend) -> Result<i32> {
let r: Result<&str> = self.format_name.split('.').next().ok_or_else(|| {
ErrorKind::Msg(format!(
"incomprehensible format file name \"{}\"",
self.format_name
))
.into()
});
let stem = r?;
let result = {
self.bs
.enter_format_mode(&format!("tectonic-format-{stem}.tex"));
let mut launcher =
CoreBridgeLauncher::new_with_security(&mut self.bs, status, self.security.clone());
let r = TexEngine::default()
.halt_on_error_mode(true)
.initex_mode(true)
.shell_escape(self.shell_escape_mode != ShellEscapeMode::Disabled)
.process(&mut launcher, "UNUSED.fmt", "texput");
self.bs.leave_format_mode();
r
};
match result {
Ok(TexOutcome::Spotless) => {}
Ok(TexOutcome::Warnings) => {
tt_warning!(status, "warnings were issued by the TeX engine; use --print and/or --keep-logs for details.");
}
Ok(TexOutcome::Errors) => {
tt_error!(status, "errors were issued by the TeX engine; use --print and/or --keep-logs for details.");
return Err(ErrorKind::Msg("unhandled TeX engine error".to_owned()).into());
}
Err(e) => {
return Err(e.into());
}
}
for (name, file) in &*self.bs.mem.files.borrow() {
if name == self.bs.mem.stdout_key() {
continue;
}
let sname = name;
if !sname.ends_with(".fmt") {
continue;
}
ctry!(self.bs.format_cache.write_format(stem, &file.data, status); "cannot write format file {}", sname);
}
self.bs.mem.files.borrow_mut().clear();
Ok(0)
}
fn tex_pass(
&mut self,
rerun_explanation: Option<&str>,
status: &mut dyn StatusBackend,
) -> Result<Option<&'static str>> {
let result = {
if let Some(s) = rerun_explanation {
status.note_highlighted("Rerunning ", "TeX", &format!(" because {s} ..."));
} else {
status.note_highlighted("Running ", "TeX", " ...");
}
let mut launcher =
CoreBridgeLauncher::new_with_security(&mut self.bs, status, self.security.clone());
if self.unstables.deterministic_mode {
launcher.with_expose_absolute_paths(false);
launcher.with_mtime_override(Some(
self.build_date
.duration_since(SystemTime::UNIX_EPOCH)
.map(|x| x.as_secs() as i64)
.expect("invalid build date in deterministic mode"),
));
}
TexEngine::default()
.halt_on_error_mode(!self.unstables.continue_on_errors)
.initex_mode(self.output_format == OutputFormat::Format)
.synctex(self.synctex_enabled)
.semantic_pagination(self.output_format == OutputFormat::Html)
.shell_escape(self.shell_escape_mode != ShellEscapeMode::Disabled)
.build_date(self.build_date)
.process(
&mut launcher,
&self.format_name,
&self.primary_input_tex_path,
)
};
let warnings = match result {
Ok(TexOutcome::Spotless) => None,
Ok(TexOutcome::Warnings) =>
Some("warnings were issued by the TeX engine; use --print and/or --keep-logs for details."),
Ok(TexOutcome::Errors) =>
Some("errors were issued by the TeX engine, but were ignored; \
use --print and/or --keep-logs for details."),
Err(e) =>
return Err(e.into()),
};
if !self.bs.mem.files.borrow().contains_key(&self.tex_xdv_path) {
tt_warning!(
status,
"did not produce \"{}\"; this may mean that your document is empty",
self.tex_xdv_path
)
}
Ok(warnings)
}
fn bibtex_pass_for_one_aux_file(
&mut self,
status: &mut dyn StatusBackend,
aux_file: &String,
) -> Result<i32> {
let result = {
status.note_highlighted("Running ", "BibTeX", &format!(" on {aux_file} ..."));
let mut launcher =
CoreBridgeLauncher::new_with_security(&mut self.bs, status, self.security.clone());
let mut engine = BibtexEngine::new();
engine.process(&mut launcher, aux_file, &self.unstables)
};
match result {
Ok(TexOutcome::Spotless) => {}
Ok(TexOutcome::Warnings) => {
tt_note!(
status,
"warnings were issued by BibTeX; use --print and/or --keep-logs for details."
);
}
Ok(TexOutcome::Errors) => {
tt_warning!(
status,
"errors were issued by BibTeX, but were ignored; \
use --print and/or --keep-logs for details."
);
}
Err(e) => {
return Err(e.chain_err(|| ErrorKind::EngineError("BibTeX")));
}
}
Ok(0)
}
fn bibtex_pass(&mut self, status: &mut dyn StatusBackend) -> Result<i32> {
let mut aux_files = vec![self.tex_aux_path.clone()];
for f in self.bs.get_intermediate_file_names() {
if f.ends_with(".aux") && f != self.tex_aux_path {
aux_files.push(f);
}
}
for f in aux_files {
let _r = self.bibtex_pass_for_one_aux_file(status, &f)?;
}
Ok(0)
}
fn xdvipdfmx_pass(&mut self, status: &mut dyn StatusBackend) -> Result<i32> {
{
status.note_highlighted("Running ", "xdvipdfmx", " ...");
let mut launcher =
CoreBridgeLauncher::new_with_security(&mut self.bs, status, self.security.clone());
let mut engine = XdvipdfmxEngine::default();
engine.build_date(self.build_date);
if let Some(ref ps) = self.unstables.paper_size {
engine.paper_spec(ps.clone());
}
engine.process(&mut launcher, &self.tex_xdv_path, &self.tex_pdf_path)?;
}
self.bs.mem.files.borrow_mut().remove(&self.tex_xdv_path);
Ok(0)
}
fn spx2html_pass(&mut self, status: &mut dyn StatusBackend) -> Result<i32> {
{
let mut engine = Spx2HtmlEngine::default();
match (self.html_emit_files, self.output_path.as_ref()) {
(true, Some(p)) => engine.output_base(p),
(false, _) => engine.do_not_emit_files(),
(true, None) => return Err(errmsg!("HTML output must be saved directly to disk")),
};
if let Some(p) = self.html_assets_spec_path.as_ref() {
engine.assets_spec_path(p);
} else if !self.html_emit_assets {
engine.do_not_emit_assets();
}
if let Some(a) = self.html_precomputed_assets.as_ref() {
engine.precomputed_assets(a.clone());
}
status.note_highlighted("Running ", "spx2html", " ...");
engine.process_to_filesystem(&mut self.bs, status, &self.tex_xdv_path)?;
}
self.bs.mem.files.borrow_mut().remove(&self.tex_xdv_path);
Ok(0)
}
pub fn get_stdout_content(&self) -> Vec<u8> {
self.bs
.mem
.files
.borrow()
.get(self.bs.mem.stdout_key())
.map(|mfi| mfi.data.clone())
.unwrap_or_default()
}
pub fn into_file_data(self) -> MemoryFileCollection {
Rc::try_unwrap(self.bs.mem.files)
.expect("multiple strong refs to MemoryIo files")
.into_inner()
}
fn check_biber_requirement(
&self,
status: &mut dyn StatusBackend,
) -> Result<Option<ExternalToolPass>> {
let mut run_xml_path = PathBuf::from(&self.primary_input_tex_path);
run_xml_path.set_extension("run.xml");
let run_xml_path = run_xml_path.display().to_string();
let mem_files = &*self.bs.mem.files.borrow();
let run_xml_entry = match mem_files.get(&run_xml_path) {
Some(e) => e,
None => return Ok(None),
};
let s = (
crate::config::is_config_test_mode_activated(),
std::env::var("TECTONIC_TEST_FAKE_BIBER"),
);
let mut argv = match s {
(true, Ok(text)) if !text.trim().is_empty() => {
text.split_whitespace().map(|x| x.to_owned()).collect()
}
_ => vec!["biber".to_owned()],
};
let find_by = |binary_name: &str| -> Option<String> {
if let Ok(pathbuf) = which(binary_name) {
if let Some(biber_path) = pathbuf.to_str() {
return Some(biber_path.to_owned());
}
}
None
};
let mut use_tectonic_biber_override = false;
for binary_name in ["./tectonic-biber", "tectonic-biber"] {
if let Some(biber_path) = find_by(binary_name) {
argv = vec![biber_path];
use_tectonic_biber_override = true;
break;
}
}
let mut extra_requires = HashSet::new();
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum State {
Searching,
InBinaryName,
InBiberCmdline,
InBiberArgument,
InBiberRemainder,
InBiberRequirementSection,
InBiberFileRequirement,
}
let curs = Cursor::new(&run_xml_entry.data[..]);
let mut reader = NsReader::from_reader(curs);
let mut buf = Vec::new();
let mut state = State::Searching;
loop {
let event = ctry!(
reader.read_event_into(&mut buf);
"error parsing run.xml file"
);
if let Event::Eof = event {
break;
}
match (state, event) {
(State::Searching, Event::Start(ref e)) => {
let name = reader
.decoder()
.decode(e.local_name().into_inner())
.map_err(quick_xml::Error::from)?;
if name == "binary" {
state = State::InBinaryName;
}
}
(State::InBinaryName, Event::Text(ref e)) => {
let text = e.unescape()?;
state = if &text == "biber" {
State::InBiberCmdline
} else {
State::Searching
};
}
(State::InBinaryName, _) => {
state = State::Searching;
}
(State::InBiberCmdline, Event::Start(ref e)) => {
let name = reader
.decoder()
.decode(e.local_name().into_inner())
.map_err(quick_xml::Error::from)?;
state = match &*name {
"infile" | "outfile" | "option" => State::InBiberArgument,
_ => State::InBiberRemainder,
}
}
(State::InBiberCmdline, Event::End(ref e)) => {
let name = reader
.decoder()
.decode(e.local_name().into_inner())
.map_err(quick_xml::Error::from)?;
if name == "cmdline" {
state = State::InBiberRemainder;
}
}
(State::InBiberArgument, Event::Text(ref e)) => {
argv.push(e.unescape()?.to_string());
state = State::InBiberCmdline;
}
(State::InBiberRemainder, Event::Start(ref e)) => {
let name = reader
.decoder()
.decode(e.local_name().into_inner())
.map_err(quick_xml::Error::from)?;
state = match &*name {
"input" | "requires" => State::InBiberRequirementSection,
_ => State::InBiberRemainder,
}
}
(State::InBiberRemainder, Event::End(ref e)) => {
let name = reader
.decoder()
.decode(e.local_name().into_inner())
.map_err(quick_xml::Error::from)?;
if name == "external" {
break;
}
}
(State::InBiberRequirementSection, Event::Start(ref e)) => {
let name = reader
.decoder()
.decode(e.local_name().into_inner())
.map_err(quick_xml::Error::from)?;
state = match &*name {
"file" => State::InBiberFileRequirement,
_ => State::InBiberRemainder,
}
}
(State::InBiberRequirementSection, Event::End(ref e)) => {
let name = reader
.decoder()
.decode(e.local_name().into_inner())
.map_err(quick_xml::Error::from)?;
if name == "input" || name == "requires" {
state = State::InBiberRemainder;
}
}
(State::InBiberFileRequirement, Event::Text(ref e)) => {
extra_requires.insert(e.unescape()?.to_string());
state = State::InBiberRequirementSection;
}
(State::InBiberFileRequirement, _) => {
state = State::InBiberRequirementSection;
}
_ => {}
}
}
Ok(if state == State::Searching {
None
} else {
if use_tectonic_biber_override {
tt_note!(status, "using `tectonic-biber`, found at {}", argv[0]);
}
Some(ExternalToolPass {
argv,
extra_requires,
})
})
}
}