Consolidate separate print_config functions

This commit is contained in:
Greg Shuflin 2021-03-01 20:46:53 -08:00
parent fed7cea057
commit 00570e0e91
2 changed files with 26 additions and 20 deletions

View File

@ -155,7 +155,7 @@ pub extern "C" fn rust_main(_argc: c_int, _argv: *const *const c_char) -> c_int
let filename = "device.conf";
println!("Print configuration to file '{}.", filename);
radio::print_config(&device, filename);
radio::print_config(&device, Some(filename));
} else if csv_flag {
if matches.free.len() != 1 {
@ -170,7 +170,7 @@ pub extern "C" fn rust_main(_argc: c_int, _argv: *const *const c_char) -> c_int
print_usage();
}
let device = radio::read_image(&matches.free[0]);
radio::print_config_to_stdout(&device);
radio::print_config(&device, None);
}
exit(0);

View File

@ -310,29 +310,35 @@ pub fn parse_config(radio: &Radio, filename: &str) {
}
/// Print full information about the device configuration.
pub fn print_config(radio: &Radio, filename: &str) {
let device = radio.ptr;
let file = std::fs::File::create(filename).unwrap();
let fd = file.as_raw_fd();
let mode = CString::new("w").unwrap();
unsafe {
let file = libc::fdopen(fd, mode.as_ptr());
radio_print_config(device, file, 1);
libc::fclose(file);
}
}
/// If `filename` is `None`, write to stdout.
pub fn print_config(radio: &Radio, filename: Option<&str>) {
pub fn print_config_to_stdout(radio: &Radio) {
let device = radio.ptr;
let mode = CString::new("w").unwrap();
let file;
let (fd, verbosity) = match filename {
None => {
unsafe {
let stdout = libc::fdopen(libc::STDOUT_FILENO, mode.as_ptr());
let verbosity = if libc::isatty(libc::STDOUT_FILENO) == 1 {
0
} else {
1
};
radio_print_config(device, stdout, verbosity);
(libc::STDOUT_FILENO, verbosity)
}
},
Some(filename) => {
file = std::fs::File::create(filename).unwrap();
(file.as_raw_fd(), 1)
}
};
let device = radio.ptr;
let mode = CString::new("w").unwrap();
unsafe {
let file = libc::fdopen(fd, mode.as_ptr());
radio_print_config(device, file, verbosity);
libc::fclose(file);
}
}