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"; let filename = "device.conf";
println!("Print configuration to file '{}.", filename); println!("Print configuration to file '{}.", filename);
radio::print_config(&device, filename); radio::print_config(&device, Some(filename));
} else if csv_flag { } else if csv_flag {
if matches.free.len() != 1 { 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(); print_usage();
} }
let device = radio::read_image(&matches.free[0]); let device = radio::read_image(&matches.free[0]);
radio::print_config_to_stdout(&device); radio::print_config(&device, None);
} }
exit(0); exit(0);

View File

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