Skip to content

Commit

Permalink
Filtering for arch command
Browse files Browse the repository at this point in the history
  • Loading branch information
Arsynth committed May 30, 2023
1 parent 4fb73d7 commit f13e4a5
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 31 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Arguments for `segs` subcommand
## [0.2.8]
### Added
Filtering by `arch` parameter to all commands
Formatting parameters (`short` and `noidx`) for commands: `syms`, `rpaths`, `dylibs`, `headers`, and `archs`
Formatting parameters (`short` and `noidx`) for commands: `syms`, `rpaths`, `dylibs`, and `headers`.
Help for all commands
Convenience functions to library
Documentation for several structs
12 changes: 11 additions & 1 deletion src/commands/common/object_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use getopts::*;

use super::options::*;
use crate::result::{Error, Result};
use crate::{MachObject, ObjectType};
use crate::{MachObject, ObjectType, FatArch};

const ARCH_ARG_SHORT: &str = "a";
const ARCH_ARG_LONG: &str = "arch";
Expand Down Expand Up @@ -35,6 +35,16 @@ impl ObjectFilter {
}
}

pub(crate) fn get_archs(&self, object_type: ObjectType) -> Vec<FatArch> {
match &self.arch {
Some(arch) => match object_type.arch_with_name(&arch) {
Some(o) => vec![o],
None => vec![],
},
None => object_type.archs(),
}
}

pub(crate) fn option_items() -> Vec<OptionItem> {
vec![OptionItem {
option_type: OptionType::Arg(IsRequired(false)),
Expand Down
68 changes: 39 additions & 29 deletions src/commands/fat.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use getopts::*;

use super::common::*;
use super::handler::*;
use super::Printer;
use super::Result;
use crate::auto_enum_fields::Field;
use crate::*;
use super::common::options::*;

static SUBCOMM_NAME: &str = "fat";

Expand All @@ -26,11 +29,21 @@ impl Handler for ArchsHandler {
SUBCOMM_NAME == name
}

fn handle_object(&self, object: ObjectType, _other_args: Vec<String>) -> Result<()> {
match object {
ObjectType::Fat(fat) => self.handle_fat(fat),
_ => (),
};
fn handle_object(&self, object: ObjectType, other_args: Vec<String>) -> Result<()> {
let mut opts = Options::new();
self.accepted_option_items().add_to_opts(&mut opts);

let filter = ObjectFilter::build(&mut opts, &other_args)?;

let archs = &filter.get_archs(object);
let out_index = archs.len() > 1;
for (idx, arch) in archs.iter().enumerate() {
if out_index {
self.printer.out_list_item_dash(0, idx);
}
self.handle_arch(arch)
}

Ok(())
}
}
Expand All @@ -40,31 +53,28 @@ const SIZE_STR: &str = "Size";
const ALIGN_STR: &str = "Align";

impl ArchsHandler {
fn handle_fat(&self, fat: FatObject) {
for (index, arch) in fat.arch_iterator().enumerate() {
self.printer.out_list_item_dash(0, index);
let mut fields = match arch.printable_cpu() {
Some(cpu) => {
vec![Field::new(ARCH_STR.to_string(), cpu.to_string())]
}
None => {
vec![
Field::new(CPU_TYPE_STR.to_string(), arch.cputype.to_string()),
Field::new(
CPU_SUBTYPE_STR.to_string(),
arch.cpusubtype.masked().to_string(),
),
]
}
};
fn handle_arch(&self, arch: &FatArch) {
let mut fields = match arch.printable_cpu() {
Some(cpu) => {
vec![Field::new(ARCH_STR.to_string(), cpu.to_string())]
}
None => {
vec![
Field::new(CPU_TYPE_STR.to_string(), arch.cputype.to_string()),
Field::new(
CPU_SUBTYPE_STR.to_string(),
arch.cpusubtype.masked().to_string(),
),
]
}
};

fields.append(&mut vec![
Field::new(OFFSET_STR.to_string(), arch.offset.to_string()),
Field::new(SIZE_STR.to_string(), arch.size.to_string()),
Field::new(ALIGN_STR.to_string(), arch.align.to_string()),
]);
fields.append(&mut vec![
Field::new(OFFSET_STR.to_string(), arch.offset.to_string()),
Field::new(SIZE_STR.to_string(), arch.size.to_string()),
Field::new(ALIGN_STR.to_string(), arch.align.to_string()),
]);

self.printer.out_default_colored_fields(fields, "\n")
}
self.printer.out_default_colored_fields(fields, "\n")
}
}
16 changes: 16 additions & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@ impl ObjectType {
ObjectType::MachO(o) => vec![o.clone()],
}
}

pub fn arch_with_name(&self, name: &str) -> Option<FatArch> {
self.archs().into_iter().find(|a| {
match a.printable_cpu() {
Some(cpu) => cpu.to_string() == name,
None => false,
}
})
}

pub fn archs(&self) -> Vec<FatArch> {
match &self {
ObjectType::Fat(fat) => fat.arch_iterator().collect(),
ObjectType::MachO(_) => vec![],
}
}
}

#[cfg(test)]
Expand Down

0 comments on commit f13e4a5

Please sign in to comment.