From 1c81834b6240225d929dd3db78ec4c4688977c53 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Sat, 27 Feb 2021 23:37:41 -0800 Subject: [PATCH] More options processing in Rust --- main.c | 11 +++++++++-- src/lib.rs | 38 ++++++++++++++++++++++++++++++++------ src/radio.rs | 3 +-- 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/main.c b/main.c index 641528f..4ebc5cf 100644 --- a/main.c +++ b/main.c @@ -38,7 +38,8 @@ extern int optind; extern void print_message_from_rust(); extern void print_usage(const char* version); -extern void radio_list(); +extern void print_usage(const char* version); +extern int rust_main(int argc, char** argv); int trace_flag = 0; @@ -46,7 +47,11 @@ void usage() { print_usage((const char*)version); } -int main(int argc, char **argv) +int main(int argc, char **argv) { + return rust_main(argc, argv); +} + +int old_c_main(int argc, char **argv) { int read_flag = 0, write_flag = 0, config_flag = 0, csv_flag = 0; int list_flag = 0, verify_flag = 0; @@ -70,10 +75,12 @@ int main(int argc, char **argv) } argc -= optind; argv += optind; + /* if (list_flag) { radio_list(); exit(0); } + */ if (read_flag + write_flag + config_flag + csv_flag + verify_flag > 1) { fprintf(stderr, "Only one of -r, -w, -c, -v or -u options is allowed.\n"); usage(); diff --git a/src/lib.rs b/src/lib.rs index 7864c2b..164830c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,16 @@ use std::ffi::CStr; -use libc::c_char; +use libc::{c_int, c_char}; +use getopts::Options; +use std::process::exit; mod radio; const COPYRIGHT: &'static str = "Copyright (C) 2018 Serge Vakulenko KK6ABQ"; +extern { + fn old_c_main(argc: c_int, argv: *const *const c_char) -> c_int; +} + #[no_mangle] pub extern "C" fn print_usage(version_ptr: *const c_char) { let version: String = unsafe { CStr::from_ptr(version_ptr) }.to_string_lossy().to_string(); @@ -38,13 +44,10 @@ Options: -l List all supported radios. -t Trace USB protocol."#; print!("{}", msg); - std::process::exit(-1); + exit(-1); } -#[no_mangle] -pub extern "C" fn process_options() { - use getopts::Options; - +fn get_options() -> Options { let mut opts = Options::new(); opts.optflag("t", "", "Trace USB protocol."); opts.optflag("r", "", "Read codeplug from the radio to a file 'device.img'.\nSave configuration to a text file 'device.conf'."); @@ -53,7 +56,30 @@ pub extern "C" fn process_options() { opts.optflag("u", "", "Update contacts database from CSV file."); opts.optflag("l", "", "List all supported radios."); opts.optflag("v", "", "Verify configuration script for the radio."); + opts +} +#[no_mangle] +pub extern "C" fn rust_main(argc: c_int, argv: *const *const c_char) -> c_int { + + let args = std::env::args().skip(1); + let matches = match get_options().parse(args) { + Ok(m) => m, + Err(fail) => { + eprintln!("{}", fail); + exit(-1); + } + }; + + let list_flag = matches.opt_present("l"); + if list_flag { + radio::list(); + exit(0); + } + + unsafe { + old_c_main(argc, argv) + } } #[cfg(test)] diff --git a/src/radio.rs b/src/radio.rs index 58d293b..fc9bdde 100644 --- a/src/radio.rs +++ b/src/radio.rs @@ -2,8 +2,7 @@ extern { fn radio_list_c(); } -#[no_mangle] -pub extern "C" fn radio_list() { +pub fn list() { unsafe { radio_list_c(); }