From 19e5f4e2f4a0681b38da29d4d975b1b1d9c82b96 Mon Sep 17 00:00:00 2001 From: emma Date: Sat, 25 Mar 2023 00:46:37 -0400 Subject: [PATCH] added c-main dependency --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + src/main.rs | 17 ++++++++--------- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4be7ee1..25a32a9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -30,6 +30,15 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "c-main" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "797bbff8bd2bcddb7f0ee638b55398686adac15174689a86da5ffc0f51219f75" +dependencies = [ + "libc", +] + [[package]] name = "cexpr" version = "0.6.0" @@ -205,6 +214,7 @@ dependencies = [ name = "tomcat" version = "0.0.1" dependencies = [ + "c-main", "toml", "yacexits", ] diff --git a/Cargo.toml b/Cargo.toml index fbd6f01..6e47e4d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,5 +6,6 @@ license = "AGPL-3.0-or-later" authors = [ "Emma Tebibyte " ] [dependencies] +c-main = "1.0.1" toml = "0.5.9" yacexits = "0.1.2" diff --git a/src/main.rs b/src/main.rs index 921577a..77758ea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,8 +16,9 @@ * along with this program. If not, see https://www.gnu.org/licenses/. */ +#![no_main] + use std::{ - env, fs::File, io::Read, iter::Peekable, @@ -26,6 +27,7 @@ use std::{ str::FromStr, }; +use c_main::Args; use toml::Value; use yacexits::*; @@ -97,8 +99,9 @@ fn parse_toml( Ok(out) } -fn main() { - let argv: Vec = env::args().collect(); +#[no_mangle] +fn rust_main(args: Args) { + let argv: Vec<&str> = args.into_iter().collect(); let usage_info = format!("Usage: {} [table.]key[[index]] [file...]", argv[0]); if argv.len() <= 1 { @@ -106,15 +109,11 @@ fn main() { exit(EX_USAGE); } - let input: &str; - - if let Some(arg) = argv.get(2) { - input = arg; - } else { input = &""; } + let input = argv.get(2).unwrap_or(&""); let mut content = Vec::new(); - match input { + match input.to_owned() { "-" | "" => unsafe { File::from_raw_fd(0) }, _ => { File::open(Path::new(&input)).unwrap_or_else(|_| {