More options processing in Rust

This commit is contained in:
Greg Shuflin 2021-02-27 23:37:41 -08:00
parent 88e8fb48d8
commit 1c81834b62
3 changed files with 42 additions and 10 deletions

11
main.c
View File

@ -38,7 +38,8 @@ extern int optind;
extern void print_message_from_rust(); extern void print_message_from_rust();
extern void print_usage(const char* version); 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; int trace_flag = 0;
@ -46,7 +47,11 @@ void usage() {
print_usage((const char*)version); 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 read_flag = 0, write_flag = 0, config_flag = 0, csv_flag = 0;
int list_flag = 0, verify_flag = 0; int list_flag = 0, verify_flag = 0;
@ -70,10 +75,12 @@ int main(int argc, char **argv)
} }
argc -= optind; argc -= optind;
argv += optind; argv += optind;
/*
if (list_flag) { if (list_flag) {
radio_list(); radio_list();
exit(0); exit(0);
} }
*/
if (read_flag + write_flag + config_flag + csv_flag + verify_flag > 1) { 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"); fprintf(stderr, "Only one of -r, -w, -c, -v or -u options is allowed.\n");
usage(); usage();

View File

@ -1,10 +1,16 @@
use std::ffi::CStr; use std::ffi::CStr;
use libc::c_char; use libc::{c_int, c_char};
use getopts::Options;
use std::process::exit;
mod radio; mod radio;
const COPYRIGHT: &'static str = "Copyright (C) 2018 Serge Vakulenko KK6ABQ"; 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] #[no_mangle]
pub extern "C" fn print_usage(version_ptr: *const c_char) { 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(); let version: String = unsafe { CStr::from_ptr(version_ptr) }.to_string_lossy().to_string();
@ -38,13 +44,10 @@ Options:
-l List all supported radios. -l List all supported radios.
-t Trace USB protocol."#; -t Trace USB protocol."#;
print!("{}", msg); print!("{}", msg);
std::process::exit(-1); exit(-1);
} }
#[no_mangle] fn get_options() -> Options {
pub extern "C" fn process_options() {
use getopts::Options;
let mut opts = Options::new(); let mut opts = Options::new();
opts.optflag("t", "", "Trace USB protocol."); 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'."); 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("u", "", "Update contacts database from CSV file.");
opts.optflag("l", "", "List all supported radios."); opts.optflag("l", "", "List all supported radios.");
opts.optflag("v", "", "Verify configuration script for the radio."); 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)] #[cfg(test)]

View File

@ -2,8 +2,7 @@ extern {
fn radio_list_c(); fn radio_list_c();
} }
#[no_mangle] pub fn list() {
pub extern "C" fn radio_list() {
unsafe { unsafe {
radio_list_c(); radio_list_c();
} }