use std::path::Path;
use std::process::Command;
pub const PINNED: &str = "1.24.0";
pub fn program() -> Result<String, String> {
let program = std::env::var("LUATEX_ORACLE").unwrap_or_else(|_| "luatex".to_string());
let out = Command::new(&program)
.arg("--version")
.output()
.map_err(|_| format!("no `{program}' on PATH -- the harness has no oracle"))?;
let banner = String::from_utf8_lossy(&out.stdout);
let version = banner
.lines()
.next()
.and_then(|l| l.split("Version ").nth(1))
.and_then(|v| v.split_whitespace().next())
.ok_or_else(|| format!("`{program} --version' did not report a version"))?;
match version == PINNED || std::env::var("LUATEX_VERSION_EXPECT").as_deref() == Ok(version) {
true => Ok(program),
false => Err(format!(
"oracle is luatex {version}, but the frozen expectations were measured \
against {PINNED}; set LUATEX_VERSION_EXPECT={version} to accept it"
)),
}
}
const ENABLE_ALL: &str = "\\catcode123=1 \\catcode125=2 \
\\directlua{tex.enableprimitives(\"\",tex.extraprimitives())}\
\\catcode123=12 \\catcode125=12 \\input case.tex";
pub fn reference(program: &str, src: &str) -> String {
let dir = scratch_dir();
if std::fs::write(dir.join("case.tex"), src).is_err() {
return String::new();
}
let out = Command::new(program)
.args(["--ini", "-interaction=nonstopmode", ENABLE_ALL])
.env("max_print_line", "100000")
.current_dir(&dir)
.output();
let text = out
.map(|o| String::from_utf8_lossy(&o.stdout).into_owned())
.unwrap_or_default();
let _ = std::fs::remove_dir_all(&dir);
crate::parity::messages_of(&text)
}
pub fn subject(src: &str) -> String {
crate::expand::set_luatex_registers(true);
match crate::run_messages_initex(src) {
Ok(m) => m,
Err(e) => format!("ERROR: {}", e.0),
}
}
pub struct Primitive {
pub origin: &'static str,
pub name: String,
pub meaning: String,
}
pub fn primitives(program: &str) -> Result<Vec<Primitive>, String> {
let dir = scratch_dir();
let lua = "for _,o in ipairs({'tex','etex','luatex','core'}) do \
for _,v in ipairs(tex.extraprimitives(o)) do \
texio.write_nl('log','ORIGIN '..o..' ['..v..']') end end \
for _,v in ipairs(tex.primitives()) do \
texio.write_nl('log','ALL ['..v..']') end";
std::fs::write(dir.join("list.lua"), lua).map_err(|e| e.to_string())?;
std::fs::write(
dir.join("list.tex"),
"\\catcode123=1 \\catcode125=2 \\directlua{dofile('list.lua')}\\end\n",
)
.map_err(|e| e.to_string())?;
Command::new(program)
.args(["--ini", "-interaction=nonstopmode", "list.tex"])
.current_dir(&dir)
.output()
.map_err(|e| e.to_string())?;
let log = std::fs::read_to_string(dir.join("list.log")).map_err(|e| e.to_string())?;
let _ = std::fs::remove_dir_all(&dir);
let mut origin_of = std::collections::HashMap::new();
let mut all = Vec::new();
let bracketed = |s: &str| {
s.strip_prefix('[')
.and_then(|s| s.rfind(']').map(|end| s[..end].to_string()))
};
for line in log.lines() {
if let Some(rest) = line.strip_prefix("ORIGIN ") {
let Some((o, name)) = rest.split_once(' ') else {
continue;
};
let o: &'static str = match o {
"tex" => "tex",
"etex" => "etex",
"luatex" => "luatex",
_ => "core",
};
if let Some(name) = bracketed(name) {
origin_of.entry(name).or_insert(o);
}
} else if let Some(name) = line.strip_prefix("ALL ").and_then(bracketed) {
all.push(name);
}
}
if all.is_empty() {
return Err("luatex listed no primitives".to_string());
}
all.sort();
all.dedup();
let meanings = meanings_in(program, &all, reference)?;
Ok(all
.into_iter()
.zip(meanings)
.map(|(name, meaning)| Primitive {
origin: origin_of.get(&name).copied().unwrap_or("unlisted"),
name,
meaning,
})
.collect())
}
pub fn command_table(program: &str) -> Result<Vec<(String, String, bool)>, String> {
let dir = scratch_dir();
let lua = "for _,v in ipairs(tex.primitives()) do \
local t = token.create(v) \
texio.write_nl('log','CMD '..t.cmdname..' '..tostring(t.expandable)..' ['..v..']') \
end";
std::fs::write(dir.join("cmds.lua"), lua).map_err(|e| e.to_string())?;
std::fs::write(
dir.join("case.tex"),
"\\catcode123=1 \\catcode125=2 \\directlua{dofile('cmds.lua')}\\end\n",
)
.map_err(|e| e.to_string())?;
Command::new(program)
.args(["--ini", "-interaction=nonstopmode", ENABLE_ALL])
.current_dir(&dir)
.output()
.map_err(|e| e.to_string())?;
let log = std::fs::read_to_string(dir.join("case.log")).map_err(|e| e.to_string())?;
let _ = std::fs::remove_dir_all(&dir);
let mut table: Vec<(String, String, bool)> = log
.lines()
.filter_map(|l| l.strip_prefix("CMD "))
.filter_map(|rest| {
let mut w = rest.splitn(3, ' ');
let (cmd, expandable, name) = (w.next()?, w.next()?, w.next()?);
let name = name.strip_prefix('[')?;
let name = &name[..name.rfind(']')?];
(cmd != "undefined_cs")
.then(|| (name.to_string(), cmd.to_string(), expandable == "true"))
})
.collect();
table.sort();
table.dedup();
match table.is_empty() {
true => Err("luatex reported no command codes".to_string()),
false => Ok(table),
}
}
pub fn render_command_table(table: &[(String, String, bool)]) -> String {
let mut out = format!(
"//! Every primitive a document can reach in luatex {PINNED}, with the command\n\
//! code luatex gives it (`token.create(name).cmdname`) and whether that\n\
//! command expands. WRITTEN by `bash scripts/primitive-census.sh` out of\n\
//! luatex's own table (`crate::luatex_oracle::command_table`); not edited\n\
//! by hand, and `tests/luatex_primitives.rs` checks it against luatex.\n\
\n\
/// `(name, luatex command, expandable)`, sorted by name.\n\
pub const TABLE: &[(&str, &str, bool)] = &[\n"
);
for (name, cmd, expandable) in table {
out.push_str(&format!(" ({name:?}, {cmd:?}, {expandable}),\n"));
}
out.push_str("];\n");
out
}
pub fn spelt(name: &str) -> String {
format!("\\{name}")
}
pub fn meaning_probe(names: &[String]) -> String {
let mut src = String::from("\\catcode`\\{=1 \\catcode`\\}=2\n");
for n in names {
src.push_str(&format!("\\message{{@@<\\meaning{}>@@}}\n", spelt(n)));
}
src.push_str("\\end\n");
src
}
pub fn meanings_of(stream: &str) -> Vec<String> {
stream
.split("@@<")
.skip(1)
.map(|s| s.split(">@@").next().unwrap_or("").to_string())
.collect()
}
fn meanings_in(
program: &str,
names: &[String],
run: fn(&str, &str) -> String,
) -> Result<Vec<String>, String> {
let got = meanings_of(&run(program, &meaning_probe(names)));
match got.len() == names.len() {
true => Ok(got),
false => Err(format!(
"the meaning probe printed {} of {} meanings",
got.len(),
names.len()
)),
}
}
fn scratch_dir() -> std::path::PathBuf {
use std::sync::atomic::{AtomicU64, Ordering};
static RUN: AtomicU64 = AtomicU64::new(0);
let dir = std::env::temp_dir().join(format!(
"texrs-luatex-{}-{}",
std::process::id(),
RUN.fetch_add(1, Ordering::Relaxed)
));
let _ = std::fs::create_dir_all(&dir);
dir
}
pub fn primitives_of_case(src: &str) -> Vec<String> {
src.lines()
.filter_map(|l| l.trim().strip_prefix("% primitives:"))
.flat_map(|rest| rest.split_whitespace())
.map(|w| w.trim_start_matches('\\').to_string())
.collect()
}
pub fn frozen_path(repo: &Path) -> std::path::PathBuf {
repo.join("tests/data/luatex_cases_expected.txt")
}