@@ -12,9 +12,7 @@ use goblin::mach::Mach;
12
12
use goblin:: Object ;
13
13
use ignore:: Walk ;
14
14
use memmap:: Mmap ;
15
- #[ cfg( not( feature = "color" ) ) ]
16
- use serde_json:: to_string_pretty;
17
- use serde_json:: { json, Value } ;
15
+ use serde_json:: { json, to_string_pretty, Value } ;
18
16
use sysinfo:: {
19
17
PidExt , ProcessExt , ProcessRefreshKind , RefreshKind , System , SystemExt ,
20
18
} ;
@@ -36,15 +34,20 @@ use binary::{
36
34
use checksec:: elf;
37
35
#[ cfg( feature = "macho" ) ]
38
36
use checksec:: macho;
37
+ use checksec:: output;
39
38
#[ cfg( feature = "pe" ) ]
40
39
use checksec:: pe;
41
40
use checksec:: underline;
42
41
43
- fn json_print ( data : & Value , pretty : bool ) {
44
- if pretty {
42
+ fn json_print ( data : & Value , settings : & output :: Settings ) {
43
+ if settings . pretty {
45
44
#[ cfg( feature = "color" ) ]
46
- if let Ok ( colored_json) = to_colored_json_auto ( data) {
47
- println ! ( "{}" , colored_json) ;
45
+ if settings. color {
46
+ if let Ok ( colored_json) = to_colored_json_auto ( data) {
47
+ println ! ( "{}" , colored_json) ;
48
+ }
49
+ } else if let Ok ( json_str) = to_string_pretty ( data) {
50
+ println ! ( "{}" , json_str) ;
48
51
}
49
52
#[ cfg( not( feature = "color" ) ) ]
50
53
if let Ok ( json_str) = to_string_pretty ( data) {
@@ -135,13 +138,13 @@ fn parse(file: &Path) -> Result<Vec<Binary>, Error> {
135
138
Err ( Error :: IO ( io:: Error :: last_os_error ( ) ) )
136
139
}
137
140
138
- fn walk ( basepath : & Path , json : bool , pretty : bool ) {
141
+ fn walk ( basepath : & Path , settings : & output :: Settings ) {
139
142
let mut bins: Vec < Binary > = Vec :: new ( ) ;
140
143
for result in Walk :: new ( basepath) . flatten ( ) {
141
144
if let Some ( filetype) = result. file_type ( ) {
142
145
if filetype. is_file ( ) {
143
146
if let Ok ( mut result) = parse ( result. path ( ) ) {
144
- if json {
147
+ if settings . json {
145
148
bins. append ( & mut result) ;
146
149
} else {
147
150
for bin in & result {
@@ -152,8 +155,8 @@ fn walk(basepath: &Path, json: bool, pretty: bool) {
152
155
}
153
156
}
154
157
}
155
- if json {
156
- json_print ( & json ! ( Binaries :: new( bins) ) , pretty ) ;
158
+ if settings . json {
159
+ json_print ( & json ! ( Binaries :: new( bins) ) , settings ) ;
157
160
}
158
161
}
159
162
#[ allow( clippy:: too_many_lines, clippy:: cognitive_complexity) ]
@@ -193,6 +196,11 @@ fn main() {
193
196
. long ( "json" )
194
197
. help ( "Output in json format" ) ,
195
198
)
199
+ . arg (
200
+ Arg :: new ( "no-color" )
201
+ . long ( "no-color" )
202
+ . help ( "Disables color output" ) ,
203
+ )
196
204
. arg (
197
205
Arg :: new ( "pretty" )
198
206
. long ( "pretty" )
@@ -237,14 +245,19 @@ fn main() {
237
245
)
238
246
. get_matches ( ) ;
239
247
240
- let json = args. is_present ( "json" ) ;
241
248
let file = args. value_of ( "file" ) ;
242
249
let directory = args. value_of ( "directory" ) ;
243
- let pretty = args. is_present ( "pretty" ) ;
244
250
let procids = args. value_of ( "pid" ) ;
245
251
let procname = args. value_of ( "process" ) ;
246
252
let procall = args. is_present ( "process-all" ) ;
247
253
254
+ let settings = output:: Settings :: set (
255
+ #[ cfg( feature = "color" ) ]
256
+ !args. is_present ( "no-color" ) ,
257
+ args. is_present ( "json" ) ,
258
+ args. is_present ( "pretty" ) ,
259
+ ) ;
260
+
248
261
if procall {
249
262
let system = System :: new_with_specifics (
250
263
RefreshKind :: new ( )
@@ -253,7 +266,7 @@ fn main() {
253
266
let mut procs: Vec < Process > = Vec :: new ( ) ;
254
267
for ( pid, proc_entry) in system. processes ( ) {
255
268
if let Ok ( results) = parse ( proc_entry. exe ( ) ) {
256
- if json {
269
+ if settings . json {
257
270
#[ allow( clippy:: cast_sign_loss) ]
258
271
procs. append ( & mut vec ! [ Process :: new(
259
272
pid. as_u32( ) as usize ,
@@ -271,8 +284,8 @@ fn main() {
271
284
}
272
285
}
273
286
}
274
- if json {
275
- json_print ( & json ! ( Processes :: new( procs) ) , pretty ) ;
287
+ if settings . json {
288
+ json_print ( & json ! ( Processes :: new( procs) ) , & settings ) ;
276
289
}
277
290
} else if let Some ( procids) = procids {
278
291
let procids: Vec < sysinfo:: Pid > = procids
@@ -310,14 +323,14 @@ fn main() {
310
323
311
324
match parse ( process. exe ( ) ) {
312
325
Ok ( results) => {
313
- if json {
326
+ if settings . json {
314
327
#[ allow( clippy:: cast_sign_loss) ]
315
328
json_print (
316
329
& json ! ( Process :: new(
317
330
procid. as_u32( ) as usize ,
318
331
results
319
332
) ) ,
320
- pretty ,
333
+ & settings ,
321
334
) ;
322
335
} else {
323
336
for result in & results {
@@ -350,7 +363,7 @@ fn main() {
350
363
let mut procs: Vec < Process > = Vec :: new ( ) ;
351
364
for proc_entry in sysprocs {
352
365
if let Ok ( results) = parse ( proc_entry. exe ( ) ) {
353
- if json {
366
+ if settings . json {
354
367
#[ allow( clippy:: cast_sign_loss) ]
355
368
procs. append ( & mut vec ! [ Process :: new(
356
369
proc_entry. pid( ) . as_u32( ) as usize ,
@@ -372,8 +385,8 @@ fn main() {
372
385
eprintln ! ( "No process found matching name {}" , procname) ;
373
386
process:: exit ( 1 ) ;
374
387
}
375
- if json {
376
- json_print ( & json ! ( Processes :: new( procs) ) , pretty ) ;
388
+ if settings . json {
389
+ json_print ( & json ! ( Processes :: new( procs) ) , & settings ) ;
377
390
}
378
391
} else if let Some ( directory) = directory {
379
392
let directory_path = Path :: new ( directory) ;
@@ -383,7 +396,7 @@ fn main() {
383
396
process:: exit ( 1 ) ;
384
397
}
385
398
386
- walk ( directory_path, json , pretty ) ;
399
+ walk ( directory_path, & settings ) ;
387
400
} else if let Some ( file) = file {
388
401
let file_path = Path :: new ( file) ;
389
402
@@ -394,8 +407,8 @@ fn main() {
394
407
395
408
match parse ( file_path) {
396
409
Ok ( results) => {
397
- if json {
398
- json_print ( & json ! ( Binaries :: new( results) ) , pretty ) ;
410
+ if settings . json {
411
+ json_print ( & json ! ( Binaries :: new( results) ) , & settings ) ;
399
412
} else {
400
413
for result in & results {
401
414
println ! ( "{}" , result) ;
0 commit comments