Skip to content

Commit 9fa8887

Browse files
committed
Simplify cli.
1 parent 2f8e9ef commit 9fa8887

File tree

4 files changed

+37
-76
lines changed

4 files changed

+37
-76
lines changed

commands/src/cli.rs

+31-62
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::path::PathBuf;
1+
use std::path::{Path, PathBuf};
22

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

24-
const ABBRV: &[&str] = &["-H", "-L", "-S", "-D", "-C", "-T", "-B"];
20+
const ABBRV: &[&str] = &["-H", "-D", "-C", "-B"];
2521
const NAMES: &[&str] = &[
2622
"--help",
27-
"--libs",
28-
"--names",
29-
"--simplify",
3023
"--disassemble",
31-
"--tracing",
3224
"--config",
3325
"--debug",
3426
];
3527

3628
#[derive(Default, Debug, Clone)]
3729
pub struct Cli {
38-
/// Print shared libraries the object is linked against.
39-
pub libs: bool,
40-
41-
/// Print all symbols exposed by object.
42-
pub names: bool,
43-
44-
/// Strip symbols into a simpler format.
45-
pub simplify: bool,
46-
47-
/// Disassemble object into `readable` assembly,
48-
pub disassemble: bool,
49-
50-
/// Record syscalls.
51-
pub tracing: bool,
52-
53-
/// Show egui debug overlay.
54-
pub debug: bool,
55-
5630
/// Path to symbol being disassembled.
57-
pub path: Option<PathBuf>,
31+
pub path: PathBuf,
5832

5933
/// Optional path to config.
6034
pub config: Option<PathBuf>,
35+
36+
/// Show egui debug overlay.
37+
pub debug: bool,
6138
}
6239

6340
impl Cli {
@@ -68,36 +45,32 @@ impl Cli {
6845
while let Some(arg) = args.next() {
6946
match arg.as_str() {
7047
"-H" | "--help" => exit!(0 => "{HELP}"),
71-
"-S" | "--simplify" => cli.simplify = true,
72-
"-N" | "--names" => {
73-
cli.names = true;
74-
48+
"-D" | "--disassemble" => {
7549
if let Some(path) = args.next().as_deref() {
7650
if !NAMES.contains(&path) && !ABBRV.contains(&path) {
77-
cli.path = Some(PathBuf::from(path));
51+
if cli.path != Path::new("") {
52+
exit!(1 => "Path to object already given.");
53+
}
54+
cli.path = PathBuf::from(path);
7855
}
7956
}
80-
}
81-
"-L" | "--libs" => {
82-
cli.libs = true;
83-
57+
},
58+
"-C" | "--config" => {
8459
if let Some(path) = args.next().as_deref() {
8560
if !NAMES.contains(&path) && !ABBRV.contains(&path) {
86-
cli.path = Some(PathBuf::from(path));
61+
if cli.config.is_some() {
62+
exit!(1 => "Path to config already given.");
63+
}
64+
cli.config = Some(PathBuf::from(path));
8765
}
8866
}
89-
}
90-
"-D" | "--disassemble" => {
91-
cli.disassemble = true;
92-
93-
if let Some(path) = args.next().as_deref() {
94-
if !NAMES.contains(&path) && !ABBRV.contains(&path) {
95-
cli.path = Some(PathBuf::from(path));
96-
}
67+
},
68+
"-B" | "--debug" => {
69+
if cli.debug {
70+
exit!(1 => "Debug flag already set.");
9771
}
72+
cli.debug = true
9873
}
99-
"-T" | "--tracing" => cli.tracing = true,
100-
"-B" | "--debug" => cli.debug = true,
10174
unknown => {
10275
let mut distance = u32::MAX;
10376
let mut best_guess = "";
@@ -124,22 +97,18 @@ impl Cli {
12497
}
12598

12699
fn validate_args(&mut self) {
127-
if self.disassemble || self.libs || self.names {
128-
if self.path.is_none() {
129-
exit!(1 => "Missing path to an object.");
130-
}
131-
} else {
132-
// no action arguments were given
133-
self.disassemble = true;
134-
return;
100+
if self.path == Path::new("") {
101+
exit!(1 => "You must provide a path to disassemble.");
135102
}
136103

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

141-
if self.disassemble as usize + self.libs as usize + self.names as usize > 1 {
142-
exit!(1 => "Invalid combination of arguements.\n\n{HELP}");
108+
if let Some(ref cfg) = self.config {
109+
if !cfg.exists() {
110+
exit!(1 => "Config {cfg:?} does not exist.");
111+
}
143112
}
144113
}
145114
}

gui/src/lib.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,7 @@ impl UI {
135135
})
136136
}
137137

138-
pub fn process_args(&mut self) {
139-
if let Some(path) = commands::ARGS.path.as_ref().cloned() {
140-
self.offload_binary_processing(path);
141-
}
142-
}
143-
144-
fn offload_binary_processing(&mut self, path: std::path::PathBuf) {
138+
pub fn offload_binary_processing(&mut self, path: std::path::PathBuf) {
145139
// don't load multiple binaries at a time
146140
if self.panels.is_loading() {
147141
return;

processor_shared/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ pub fn encode_hex_bytes_truncated(bytes: &[u8], max_width: usize, is_padded: boo
194194
let len = bytes.len() * 3;
195195
let pad = is_padded as usize * max_width.saturating_sub(len);
196196
let mut buffer = Vec::with_capacity(len + pad);
197-
let slice = &mut buffer[..];
197+
let slice = buffer.spare_capacity_mut();
198+
let slice = std::mem::transmute::<_, &mut [u8]>(slice);
198199
let mut idx = 0;
199200

200201
// truncation has to occur

src/main.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ fn main() {
1515
wayland::set_env();
1616
}
1717

18-
if ARGS.disassemble {
19-
let mut ui = gui::UI::new().unwrap();
20-
ui.process_args();
21-
ui.run();
22-
return;
23-
}
18+
let mut ui = gui::UI::new().unwrap();
19+
ui.offload_binary_processing(ARGS.path.clone());
20+
ui.run();
2421
}

0 commit comments

Comments
 (0)