#[cfg(windows)]
#[allow(dead_code)]
const SYS_CURL: &str = "C:\\Windows\\System32\\curl.exe";
#[cfg(not(windows))]
#[allow(dead_code)]
const SYS_CURL: &str = "curl";
#[allow(dead_code)]
fn _helix_allowed(var: &str, what: &str) -> bool {
let ok = std::env::var(var).map(|v| v == "1").unwrap_or(false);
if !ok {
eprintln!("helix: BLOCKED {} -- permission denied (set {}=1 to allow; `hxc run` allows by default, a `hxc build`/`export-rust` binary does not)", what, var);
}
ok
}
#[cfg(windows)]
#[allow(dead_code)]
fn _nw_exec(mut c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_RUN", "process execution") {
std::process::exit(13);
}
use std::os::windows::process::CommandExt;
c.creation_flags(0x08000000);
c
}
#[cfg(not(windows))]
#[allow(dead_code)]
fn _nw_exec(c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_RUN", "process execution") {
std::process::exit(13);
}
c
}
#[cfg(windows)]
#[allow(dead_code)]
fn _nw_net(mut c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_NET", "network access") {
std::process::exit(13);
}
use std::os::windows::process::CommandExt;
c.creation_flags(0x08000000);
c
}
#[cfg(not(windows))]
#[allow(dead_code)]
fn _nw_net(c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_NET", "network access") {
std::process::exit(13);
}
c
}
use std::{
env, fs, io,
path::Path,
process::Command,
time::{SystemTime, UNIX_EPOCH},
};
fn rust_source(input: &str) -> Result<String, String> {
let mut stage = 0;
let mut src = String::from("#![allow(dead_code)]\n");
for (number, line) in input.lines().enumerate() {
let line = line.trim();
if line.is_empty() || line.starts_with('#') {
continue;
}
let error = || {
format!(
"unsupported DB-workbench instruction at line {}: {line}",
number + 1
)
};
let (op, value) = line.split_once('#').ok_or_else(error)?;
match (stage, op.trim(), value.trim()) {
(0, "GGGGT", "db") => {
src.push_str(include_str!("../runtime/helix-db.rs"));
src.push('\n');
stage = 1;
}
(1, "AAA", "fn main()") => {
src.push_str("fn main() {\n");
stage = 2;
}
(2, "AAC", stmt) if !stmt.is_empty() => {
src.push_str(stmt);
src.push_str(";\n");
}
(2, "TTT", "end") => {
src.push_str("}\n");
stage = 3;
}
_ => return Err(error()),
}
}
if stage != 3 {
return Err("expected GGGGT db and a complete fn main() block".into());
}
Ok(src)
}
fn execute(dir: &Path, src: &str) -> io::Result<()> {
let file = dir.join("example.rs");
let bin = dir.join(format!("example{}", env::consts::EXE_SUFFIX));
fs::write(&file, src)?;
let built = Command::new(env::var_os("RUSTC").unwrap_or_else(|| "rustc".into()))
.args(["--edition=2021", "-o"])
.arg(&bin)
.arg(&file)
.status()?;
if !built.success() {
return Err(io::Error::other("Rust compilation failed"));
}
let ran = Command::new(&bin).current_dir(dir).status()?;
if !ran.success() {
return Err(io::Error::other("Helix DB example failed"));
}
Ok(())
}
fn main() -> io::Result<()> {
let path = env::args_os()
.nth(1)
.ok_or_else(|| io::Error::other("supply an example .hlx file"))?;
let src = rust_source(&fs::read_to_string(path)?).map_err(io::Error::other)?;
let stamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_err(io::Error::other)?
.as_nanos();
let dir = env::temp_dir().join(format!("helix-db-workbench-{}-{stamp}", std::process::id()));
fs::create_dir(&dir)?;
let result = execute(&dir, &src);
fs::remove_dir_all(dir)?;
result
}