More options processing in Rust
This commit is contained in:
parent
88e8fb48d8
commit
1c81834b62
11
main.c
11
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();
|
||||
|
38
src/lib.rs
38
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)]
|
||||
|
@ -2,8 +2,7 @@ extern {
|
||||
fn radio_list_c();
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn radio_list() {
|
||||
pub fn list() {
|
||||
unsafe {
|
||||
radio_list_c();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user