Skip to content

Commit

Permalink
Simplify cli.
Browse files Browse the repository at this point in the history
  • Loading branch information
WINSDK committed May 5, 2024
1 parent 2f8e9ef commit 9fa8887
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 76 deletions.
93 changes: 31 additions & 62 deletions commands/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::PathBuf;
use std::path::{Path, PathBuf};

macro_rules! exit {
($code:expr => $($arg:tt)*) => {{
Expand All @@ -13,51 +13,28 @@ USAGE: bite [options] <OBJECT>
OPTIONS:
-H, --help Print usage information
-L, --libs Print linked shared libraries
-N, --names Print all symbols exposed by object
-S, --simplify Replace common types with shortened paths
-D, --disassemble Path to object you're disassembling
-T, --tracing Trace all syscalls performed
-C, --config Path to config used for disassembling
-B, --debug Enable extra debug information";
-B, --debug Enable verbose internal info";

const ABBRV: &[&str] = &["-H", "-L", "-S", "-D", "-C", "-T", "-B"];
const ABBRV: &[&str] = &["-H", "-D", "-C", "-B"];
const NAMES: &[&str] = &[
"--help",
"--libs",
"--names",
"--simplify",
"--disassemble",
"--tracing",
"--config",
"--debug",
];

#[derive(Default, Debug, Clone)]
pub struct Cli {
/// Print shared libraries the object is linked against.
pub libs: bool,

/// Print all symbols exposed by object.
pub names: bool,

/// Strip symbols into a simpler format.
pub simplify: bool,

/// Disassemble object into `readable` assembly,
pub disassemble: bool,

/// Record syscalls.
pub tracing: bool,

/// Show egui debug overlay.
pub debug: bool,

/// Path to symbol being disassembled.
pub path: Option<PathBuf>,
pub path: PathBuf,

/// Optional path to config.
pub config: Option<PathBuf>,

/// Show egui debug overlay.
pub debug: bool,
}

impl Cli {
Expand All @@ -68,36 +45,32 @@ impl Cli {
while let Some(arg) = args.next() {
match arg.as_str() {
"-H" | "--help" => exit!(0 => "{HELP}"),
"-S" | "--simplify" => cli.simplify = true,
"-N" | "--names" => {
cli.names = true;

"-D" | "--disassemble" => {
if let Some(path) = args.next().as_deref() {
if !NAMES.contains(&path) && !ABBRV.contains(&path) {
cli.path = Some(PathBuf::from(path));
if cli.path != Path::new("") {
exit!(1 => "Path to object already given.");
}
cli.path = PathBuf::from(path);
}
}
}
"-L" | "--libs" => {
cli.libs = true;

},
"-C" | "--config" => {
if let Some(path) = args.next().as_deref() {
if !NAMES.contains(&path) && !ABBRV.contains(&path) {
cli.path = Some(PathBuf::from(path));
if cli.config.is_some() {
exit!(1 => "Path to config already given.");
}
cli.config = Some(PathBuf::from(path));
}
}
}
"-D" | "--disassemble" => {
cli.disassemble = true;

if let Some(path) = args.next().as_deref() {
if !NAMES.contains(&path) && !ABBRV.contains(&path) {
cli.path = Some(PathBuf::from(path));
}
},
"-B" | "--debug" => {
if cli.debug {
exit!(1 => "Debug flag already set.");
}
cli.debug = true
}
"-T" | "--tracing" => cli.tracing = true,
"-B" | "--debug" => cli.debug = true,
unknown => {
let mut distance = u32::MAX;
let mut best_guess = "";
Expand All @@ -124,22 +97,18 @@ impl Cli {
}

fn validate_args(&mut self) {
if self.disassemble || self.libs || self.names {
if self.path.is_none() {
exit!(1 => "Missing path to an object.");
}
} else {
// no action arguments were given
self.disassemble = true;
return;
if self.path == Path::new("") {
exit!(1 => "You must provide a path to disassemble.");
}

if self.tracing && !self.disassemble {
exit!(1 => "Invalid combination of arguements.\n\n{HELP}");
if !self.path.exists() {
exit!(1 => "Object {:?} does not exist.", self.path);
}

if self.disassemble as usize + self.libs as usize + self.names as usize > 1 {
exit!(1 => "Invalid combination of arguements.\n\n{HELP}");
if let Some(ref cfg) = self.config {
if !cfg.exists() {
exit!(1 => "Config {cfg:?} does not exist.");
}
}
}
}
8 changes: 1 addition & 7 deletions gui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,7 @@ impl UI {
})
}

pub fn process_args(&mut self) {
if let Some(path) = commands::ARGS.path.as_ref().cloned() {
self.offload_binary_processing(path);
}
}

fn offload_binary_processing(&mut self, path: std::path::PathBuf) {
pub fn offload_binary_processing(&mut self, path: std::path::PathBuf) {
// don't load multiple binaries at a time
if self.panels.is_loading() {
return;
Expand Down
3 changes: 2 additions & 1 deletion processor_shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ pub fn encode_hex_bytes_truncated(bytes: &[u8], max_width: usize, is_padded: boo
let len = bytes.len() * 3;
let pad = is_padded as usize * max_width.saturating_sub(len);
let mut buffer = Vec::with_capacity(len + pad);
let slice = &mut buffer[..];
let slice = buffer.spare_capacity_mut();
let slice = std::mem::transmute::<_, &mut [u8]>(slice);
let mut idx = 0;

// truncation has to occur
Expand Down
9 changes: 3 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ fn main() {
wayland::set_env();
}

if ARGS.disassemble {
let mut ui = gui::UI::new().unwrap();
ui.process_args();
ui.run();
return;
}
let mut ui = gui::UI::new().unwrap();
ui.offload_binary_processing(ARGS.path.clone());
ui.run();
}

0 comments on commit 9fa8887

Please sign in to comment.