use std::{
io::{stdin, BufReader},
process::ExitCode,
};
use syd::sandbox::Sandbox;
#[cfg(all(
not(coverage),
not(feature = "prof"),
not(target_os = "android"),
not(target_arch = "riscv64"),
target_page_size_4k,
target_pointer_width = "64"
))]
#[global_allocator]
static GLOBAL: hardened_malloc::HardenedMalloc = hardened_malloc::HardenedMalloc;
#[cfg(feature = "prof")]
#[global_allocator]
static GLOBAL: tcmalloc::TCMalloc = tcmalloc::TCMalloc;
syd::main! {
use lexopt::prelude::*;
syd::set_sigpipe_dfl()?;
let mut paths = Vec::new();
let mut parser = lexopt::Parser::from_env();
while let Some(arg) = parser.next()? {
match arg {
Short('h') => {
help();
return Ok(ExitCode::SUCCESS);
}
Value(val) => {
paths.push(val);
paths.extend(parser.raw_args()?);
}
_ => return Err(arg.unexpected().into()),
}
}
let mut sin = true; let mut syd = Sandbox::new();
for path in paths {
sin = false;
#[expect(clippy::disallowed_methods)]
#[expect(clippy::disallowed_types)]
let file = std::fs::File::open(path)?;
syd.parse_netset(BufReader::new(file))?;
}
if sin {
let file = stdin();
syd.parse_netset(BufReader::new(file))?;
}
syd.rule_agg_block("")?;
for addr in syd.block4() {
println!("{addr}");
}
for addr in syd.block6() {
println!("{addr}");
}
Ok(ExitCode::SUCCESS)
}
fn help() {
println!("Usage: syd-net [-h] <path>...");
println!("Tool to aggregate IP networks.");
println!("Reads IP networks from the given list of paths.");
println!("Given no arguments, reads from standard input.");
}