use tclrs::tk::{host, interp, load, trace};
fn main() {
let lib = match load::Libtk::open() {
Ok(l) => l,
Err(e) => {
eprintln!("tk-host: {e}");
std::process::exit(2);
}
};
println!("tkhost library {}", lib.path);
let impls = host::implemented_at(host::Level::Hosting);
println!(
"tkhost implemented {} of {} TclStubs slots",
impls.len(),
tclrs::tk::abi::TCL_STUBS_SLOTS
);
let tk_interp = host::build_hosting();
let root = lib.library_root();
if let Some(root) = &root {
let added = unsafe { load::seed_library_path(tk_interp as *mut std::ffi::c_void, root) };
println!("tkhost dylib root {root}");
for dir in added {
println!("tkhost library path {dir}");
}
}
println!("tkhost calling Tk_Init");
let rc = unsafe { load::call_tk_init(&lib, tk_interp) };
let code = match rc {
Ok(code) => code,
Err(e) => {
eprintln!("tk-host: {e}");
std::process::exit(2);
}
};
println!(
"tkhost Tk_Init returned {code} after {} served calls",
trace::served()
);
let result = unsafe { host::result_bytes(tk_interp as *mut std::ffi::c_void) };
println!("tkhost result {:?}", String::from_utf8_lossy(&result));
let host_ptr = unsafe { interp::host_of(tk_interp as *mut std::ffi::c_void) };
let commands = unsafe { &(*host_ptr).commands };
println!("tkhost commands {}", commands.len());
for c in commands {
println!("tkhost command {}", c.name);
}
let mut args = std::env::args().skip(1);
while let Some(arg) = args.next() {
if arg == "--events" {
let n: usize = args
.next()
.and_then(|s| s.parse().ok())
.expect("--events needs a count");
let mut serviced = 0usize;
for _ in 0..n {
serviced += (unsafe {
tclrs::tk::notifier::do_one_event_slot(
tclrs::tk::notifier::TCL_ALL_EVENTS | tclrs::tk::notifier::TCL_DONT_WAIT,
)
} != 0) as usize;
}
println!("tkhost events {n} passes, {serviced} serviced, process alive");
continue;
}
let code =
unsafe { tclrs::tk::eval::eval_script(tk_interp as *mut std::ffi::c_void, &arg) };
let result = unsafe { host::result_bytes(tk_interp as *mut std::ffi::c_void) };
let text = String::from_utf8_lossy(&result);
if code == tclrs::tk::abi::TCL_OK {
println!("tkhost eval {arg:?} -> {text:?}");
} else {
println!("tkhost eval {arg:?} !! {text}");
}
}
let (traces, links) = tclrs::tk::linkvar::counts();
println!("tkhost variable traces {traces}, linked variables {links}");
}