hy 0.18.1

HCLI - Hex-Rays CLI Utility
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
//! `hy ke` command group: Knowledge Explorer management.

use std::path::PathBuf;

use clap::{Args, Subcommand};
use dialoguer::Select;
use owo_colors::OwoColorize;

use crate::config::ConfigStore;
use crate::error::Result;
use crate::util::fmt;

// ── CLI definitions ─────────────────────────────────────────────────────

#[derive(Debug, Subcommand)]
pub enum KeCommands {
    /// Register the ida:// protocol handler and discover IDA instances
    Setup(KeSetupArgs),
    /// Open Knowledge Explorer in the browser
    Open(KeOpenArgs),
    /// Manage IDA instances for KE
    #[command(subcommand)]
    Ida(KeIdaCommands),
    /// Manage KE knowledge sources
    #[command(subcommand)]
    Source(KeSourceCommands),
}

#[derive(Debug, Args)]
pub struct KeSetupArgs {
    /// Force re-registration
    #[arg(short, long)]
    pub force: bool,
    /// Unregister the protocol handler
    #[arg(long)]
    pub unregister: bool,
}

#[derive(Debug, Args)]
pub struct KeOpenArgs {
    /// URL to open (ida://source/path or https://ke.hex-rays.com)
    pub url: Option<String>,
}

#[derive(Debug, Subcommand)]
pub enum KeIdaCommands {
    /// List IDA instances registered with KE
    List,
    /// Add an IDA instance
    Add(KeIdaAddArgs),
    /// Remove an IDA instance
    Remove(KeIdaRemoveArgs),
    /// Switch the default IDA instance
    Switch(KeIdaSwitchArgs),
}

#[derive(Debug, Args)]
pub struct KeIdaAddArgs {
    /// Instance name
    pub name: Option<String>,
    /// Path to IDA installation
    pub path: Option<PathBuf>,
    /// Auto-discover IDA installations
    #[arg(long)]
    pub auto: bool,
}

#[derive(Debug, Args)]
pub struct KeIdaRemoveArgs {
    /// Name of the IDA instance to remove
    pub name: Option<String>,
    /// Remove all instances
    #[arg(long)]
    pub all: bool,
}

#[derive(Debug, Args)]
pub struct KeIdaSwitchArgs {
    /// Name of the instance to set as default
    pub name: Option<String>,
}

#[derive(Debug, Subcommand)]
pub enum KeSourceCommands {
    /// List configured sources
    List,
    /// Add a knowledge source
    Add(KeSourceAddArgs),
    /// Remove a knowledge source
    Remove(KeSourceRemoveArgs),
}

#[derive(Debug, Args)]
pub struct KeSourceAddArgs {
    /// Source name
    pub name: String,
    /// Source directory path
    pub path: PathBuf,
}

#[derive(Debug, Args)]
pub struct KeSourceRemoveArgs {
    /// Source name
    pub name: String,
}

// ── dispatch ────────────────────────────────────────────────────────────

pub async fn run(cmd: KeCommands) -> Result<()> {
    match cmd {
        KeCommands::Setup(args) => run_setup(args).await,
        KeCommands::Open(args) => run_open(args).await,
        KeCommands::Ida(cmd) => match cmd {
            KeIdaCommands::List => run_ida_list().await,
            KeIdaCommands::Add(args) => run_ida_add(args).await,
            KeIdaCommands::Remove(args) => run_ida_remove(args).await,
            KeIdaCommands::Switch(args) => run_ida_switch(args).await,
        },
        KeCommands::Source(cmd) => match cmd {
            KeSourceCommands::List => run_source_list().await,
            KeSourceCommands::Add(args) => run_source_add(args).await,
            KeSourceCommands::Remove(args) => run_source_remove(args).await,
        },
    }
}

// ── ke setup ────────────────────────────────────────────────────────────

async fn run_setup(args: KeSetupArgs) -> Result<()> {
    if args.unregister {
        crate::ida::unregister_protocol_handler()?;
        fmt::success("Protocol handler unregistered.");
        return Ok(());
    }

    let exe = crate::util::io::executable_path();
    let exe_str = exe.to_string_lossy();
    crate::ida::register_protocol_handler(&exe_str)?;
    fmt::success("Registered ida:// protocol handler.");

    // Auto-discover and register IDA installations.
    let installations = crate::ida::find_standard_installations();
    if installations.is_empty() {
        fmt::info("No IDA installations found in standard locations.");
        fmt::info("Use `hy ke ida add` to manually register an installation.");
        return Ok(());
    }

    let valid: Vec<_> = installations
        .iter()
        .filter(|p| is_ida_dir(p))
        .cloned()
        .collect();

    if valid.is_empty() {
        fmt::info("Found installations but none appear to be valid IDA directories.");
        return Ok(());
    }

    eprintln!();
    eprintln!("  Found {} IDA installation(s):", valid.len());
    let mut last_name = String::new();
    for p in &valid {
        let name = generate_instance_name(p);
        let store = ConfigStore::global();
        let instances = store.get_string_map("ke.ida.instances");
        if !instances.contains_key(&name) || args.force {
            drop(store);
            let mut store = ConfigStore::global();
            let mut instances = store.get_string_map("ke.ida.instances");
            let path_str = p.to_string_lossy().to_string();
            instances.insert(name.clone(), path_str);
            store.set_nested(
                "ke.ida.instances",
                serde_json::to_value(&instances).unwrap(),
            );
            eprintln!("    + {:<24} {}", name, p.display().to_string().dimmed());
        } else {
            eprintln!(
                "    = {:<24} {} (already registered)",
                name,
                p.display().to_string().dimmed()
            );
        }
        last_name = name;
    }

    // Set default to last alphabetically sorted instance if none set.
    {
        let store = ConfigStore::global();
        let needs_default =
            store.get_nested_str("ke.ida.default").is_none() && !last_name.is_empty();
        drop(store);
        if needs_default {
            let mut store = ConfigStore::global();
            let instances = store.get_string_map("ke.ida.instances");
            let mut names: Vec<String> = instances.keys().cloned().collect();
            names.sort();
            if let Some(default_name) = names.last() {
                store.set_nested(
                    "ke.ida.default",
                    serde_json::Value::String(default_name.clone()),
                );
                eprintln!();
                fmt::success(&format!("Default IDA instance set to: {default_name}"));
            }
        }
    }

    Ok(())
}

// ── ke open ─────────────────────────────────────────────────────────────

async fn run_open(args: KeOpenArgs) -> Result<()> {
    let url_str = args
        .url
        .unwrap_or_else(|| "https://ke.hex-rays.com".to_string());

    // If it's an HTTP URL, just open in browser.
    if url_str.starts_with("http://") || url_str.starts_with("https://") {
        crate::util::io::open_url(&url_str);
        fmt::info("Opening Knowledge Explorer in the browser...");
        return Ok(());
    }

    // Parse ida:// URL.
    if !url_str.starts_with("ida://") {
        fmt::error(&format!("Invalid URL scheme: {url_str}"));
        fmt::info("Expected ida://source_name/file/path");
        return Ok(());
    }

    let without_scheme = &url_str["ida://".len()..];
    let (source_name, file_path) = match without_scheme.find('/') {
        Some(idx) => (&without_scheme[..idx], &without_scheme[idx + 1..]),
        None => (without_scheme, ""),
    };

    // Look up source.
    let store = ConfigStore::global();
    let sources = store.get_string_map("ke.sources");
    let source_path = match sources.get(source_name) {
        Some(p) => PathBuf::from(p),
        None => {
            fmt::error(&format!("Unknown source: {source_name}"));
            fmt::info("Use `hy ke source list` to see configured sources.");
            return Ok(());
        }
    };

    let full_path = source_path.join(file_path);
    if !full_path.exists() {
        fmt::error(&format!("File not found: {}", full_path.display()));
        return Ok(());
    }

    // Find IDA binary.
    let ida_bin = resolve_ida_binary(&store);
    drop(store);

    let ida_bin = match ida_bin {
        Some(p) => p,
        None => {
            fmt::error("No IDA installation found.");
            fmt::info("Use `hy ke ida add` to register an IDA installation.");
            return Ok(());
        }
    };

    // Log the URL resolution.
    if let Ok(mut log) = std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open("/tmp/hy_urls.log")
    {
        use std::io::Write;
        let _ = writeln!(
            log,
            "{} -> {} (ida: {})",
            url_str,
            full_path.display(),
            ida_bin.display()
        );
    }

    // Launch IDA.
    fmt::info(&format!(
        "Opening {} with {}",
        full_path.display(),
        ida_bin.display()
    ));

    if let Err(e) = crate::ida::launch_ida(&ida_bin, Some(&full_path)) {
        fmt::error(&format!("Failed to launch IDA: {e}"));
    }

    Ok(())
}

/// Resolve the IDA binary to use: first check KE default instance, then standard discovery.
fn resolve_ida_binary(store: &ConfigStore) -> Option<PathBuf> {
    // Try KE default instance.
    if let Some(default_name) = store.get_nested_str("ke.ida.default") {
        let instances = store.get_string_map("ke.ida.instances");
        if let Some(path_str) = instances.get(default_name) {
            let install_dir = PathBuf::from(path_str);
            if let Some(bin) = crate::ida::ida_binary_path(&install_dir) {
                return Some(bin);
            }
        }
    }

    // Fall back to current IDA install dir.
    crate::ida::current_install_dir()
        .and_then(|d| crate::ida::ida_binary_path(&d))
}

// ── ke ida list ─────────────────────────────────────────────────────────

async fn run_ida_list() -> Result<()> {
    let store = ConfigStore::global();
    let instances = store.get_string_map("ke.ida.instances");
    let default_name = store
        .get_nested_str("ke.ida.default")
        .unwrap_or("")
        .to_owned();

    if instances.is_empty() {
        fmt::info("No IDA instances registered.");
        fmt::info("Use `hy ke ida add --auto` to discover installations.");
        return Ok(());
    }

    let mut names: Vec<_> = instances.keys().collect();
    names.sort();

    let mut valid_count = 0;

    let mut table = crate::util::tui::Table::new(&["Name", "Path", "Status", "Default"]);
    for name in &names {
        let path = &instances[*name];
        let path_buf = PathBuf::from(path);
        let status_str = if !path_buf.exists() {
            "Missing".red().to_string()
        } else if is_ida_dir(&path_buf) {
            valid_count += 1;
            "Valid".green().to_string()
        } else {
            "Invalid".yellow().to_string()
        };

        let is_default = if *name == &default_name { "*" } else { "" };

        table.add_row(vec![
            (*name).clone(),
            path.dimmed().to_string(),
            status_str,
            is_default.to_string(),
        ]);
    }
    table.print();

    eprintln!();
    eprintln!(
        "  {valid_count}/{} valid instance(s).",
        instances.len()
    );

    Ok(())
}

// ── ke ida add ──────────────────────────────────────────────────────────

async fn run_ida_add(args: KeIdaAddArgs) -> Result<()> {
    if args.auto {
        return run_ida_add_auto().await;
    }

    let (name, path) = match (args.name, args.path) {
        (Some(n), Some(p)) => (n, p),
        _ => {
            fmt::error("Both NAME and PATH are required (or use --auto).");
            return Ok(());
        }
    };

    let path = path.canonicalize().unwrap_or(path);
    if !path.exists() || !path.is_dir() {
        fmt::error(&format!("Not a valid directory: {}", path.display()));
        return Ok(());
    }

    if !is_ida_dir(&path) {
        fmt::warning(&format!(
            "{} does not appear to be a valid IDA installation.",
            path.display()
        ));
    }

    let mut store = ConfigStore::global();
    let mut instances = store.get_string_map("ke.ida.instances");
    if instances.contains_key(&name) {
        fmt::warning(&format!("Instance '{name}' already exists, updating path."));
    }
    instances.insert(name.clone(), path.to_string_lossy().to_string());
    store.set_nested(
        "ke.ida.instances",
        serde_json::to_value(&instances).unwrap(),
    );

    fmt::success(&format!("Added instance '{name}' -> {}", path.display()));

    // Set as default if this is the only one.
    if instances.len() == 1 {
        store.set_nested(
            "ke.ida.default",
            serde_json::Value::String(name.clone()),
        );
        fmt::info(&format!("Set '{name}' as default."));
    }

    Ok(())
}

async fn run_ida_add_auto() -> Result<()> {
    let installations = crate::ida::find_standard_installations();
    let valid: Vec<_> = installations
        .iter()
        .filter(|p| is_ida_dir(p))
        .cloned()
        .collect();

    if valid.is_empty() {
        fmt::info("No valid IDA installations found in standard locations.");
        return Ok(());
    }

    // Display found installations.
    eprintln!("  Found {} IDA installation(s):", valid.len());
    eprintln!();

    let items: Vec<String> = valid
        .iter()
        .map(|p| {
            format!(
                "{:<24} {}",
                generate_instance_name(p),
                p.display()
            )
        })
        .collect();

    // Use multi-select if dialoguer supports it, otherwise add all.
    let selections: Vec<usize> = dialoguer::MultiSelect::new()
        .with_prompt("Select installations to register")
        .items(&items)
        .defaults(&vec![true; items.len()])
        .interact()
        .unwrap_or_else(|_| (0..items.len()).collect());

    if selections.is_empty() {
        fmt::info("No installations selected.");
        return Ok(());
    }

    let mut store = ConfigStore::global();
    let mut instances = store.get_string_map("ke.ida.instances");
    let mut last_name = String::new();

    for &idx in &selections {
        let p = &valid[idx];
        let name = generate_instance_name(p);
        instances.insert(name.clone(), p.to_string_lossy().to_string());
        fmt::success(&format!("  + {name}"));
        last_name = name;
    }

    store.set_nested(
        "ke.ida.instances",
        serde_json::to_value(&instances).unwrap(),
    );

    // Set default to last alphabetically if none set.
    if store.get_nested_str("ke.ida.default").is_none() && !last_name.is_empty() {
        let mut names: Vec<String> = instances.keys().cloned().collect();
        names.sort();
        if let Some(default_name) = names.last() {
            store.set_nested(
                "ke.ida.default",
                serde_json::Value::String(default_name.clone()),
            );
            fmt::info(&format!("Default set to: {default_name}"));
        }
    }

    Ok(())
}

// ── ke ida remove ───────────────────────────────────────────────────────

async fn run_ida_remove(args: KeIdaRemoveArgs) -> Result<()> {
    let mut store = ConfigStore::global();

    if args.all {
        store.set_nested(
            "ke.ida.instances",
            serde_json::Value::Object(Default::default()),
        );
        store.remove_nested("ke.ida.default");
        fmt::success("All IDA instances removed.");
        return Ok(());
    }

    let name = match args.name {
        Some(n) => n,
        None => {
            let instances = store.get_string_map("ke.ida.instances");
            if instances.is_empty() {
                fmt::info("No instances to remove.");
                return Ok(());
            }
            let mut names: Vec<String> = instances.keys().cloned().collect();
            names.sort();
            let selection = Select::new()
                .with_prompt("Select instance to remove")
                .items(&names)
                .interact()
                .map_err(|_| crate::error::Error::Other("Cancelled".into()))?;
            names[selection].clone()
        }
    };

    let mut instances = store.get_string_map("ke.ida.instances");
    if instances.remove(&name).is_none() {
        fmt::error(&format!("Instance '{name}' not found."));
        return Ok(());
    }

    store.set_nested(
        "ke.ida.instances",
        serde_json::to_value(&instances).unwrap(),
    );

    // Handle default reassignment.
    let was_default = store
        .get_nested_str("ke.ida.default")
        .map(|s| s == name)
        .unwrap_or(false);

    if was_default {
        if instances.is_empty() {
            store.remove_nested("ke.ida.default");
        } else {
            let mut names: Vec<String> = instances.keys().cloned().collect();
            names.sort();
            let new_default = names.last().unwrap().clone();
            store.set_nested(
                "ke.ida.default",
                serde_json::Value::String(new_default.clone()),
            );
            fmt::info(&format!("Default switched to: {new_default}"));
        }
    }

    fmt::success(&format!("Removed instance '{name}'."));
    Ok(())
}

// ── ke ida switch ───────────────────────────────────────────────────────

async fn run_ida_switch(args: KeIdaSwitchArgs) -> Result<()> {
    let mut store = ConfigStore::global();
    let instances = store.get_string_map("ke.ida.instances");

    if instances.is_empty() {
        fmt::info("No IDA instances registered.");
        return Ok(());
    }

    let name = match args.name {
        Some(n) => {
            if !instances.contains_key(&n) {
                fmt::error(&format!("Instance '{n}' not found."));
                return Ok(());
            }
            n
        }
        None => {
            let default_name = store
                .get_nested_str("ke.ida.default")
                .unwrap_or("")
                .to_owned();
            let mut names: Vec<String> = instances.keys().cloned().collect();
            names.sort();
            let items: Vec<String> = names
                .iter()
                .map(|n| {
                    if *n == default_name {
                        format!("{n} (current)")
                    } else {
                        n.clone()
                    }
                })
                .collect();

            let selection = Select::new()
                .with_prompt("Select default IDA instance")
                .items(&items)
                .interact()
                .map_err(|_| crate::error::Error::Other("Cancelled".into()))?;
            names[selection].clone()
        }
    };

    store.set_nested(
        "ke.ida.default",
        serde_json::Value::String(name.clone()),
    );
    fmt::success(&format!("Default IDA instance set to: {name}"));
    Ok(())
}

// ── ke source list ──────────────────────────────────────────────────────

async fn run_source_list() -> Result<()> {
    let store = ConfigStore::global();
    let sources = store.get_string_map("ke.sources");

    if sources.is_empty() {
        fmt::info("No knowledge sources configured.");
        fmt::info("Use `hy ke source add <name> <path>` to add one.");
        return Ok(());
    }

    let mut names: Vec<_> = sources.keys().collect();
    names.sort();

    eprintln!("  {:<24} Path", "Name");
    eprintln!("  {}", "-".repeat(74));
    for name in &names {
        eprintln!("  {:<24} {}", name, sources[*name].dimmed());
    }

    Ok(())
}

// ── ke source add ───────────────────────────────────────────────────────

async fn run_source_add(args: KeSourceAddArgs) -> Result<()> {
    let path = args.path.canonicalize().unwrap_or(args.path);
    if !path.exists() || !path.is_dir() {
        fmt::error(&format!("Not a valid directory: {}", path.display()));
        return Ok(());
    }

    let mut store = ConfigStore::global();
    let mut sources = store.get_string_map("ke.sources");

    if sources.contains_key(&args.name) {
        fmt::warning(&format!(
            "Source '{}' already exists, updating path.",
            args.name
        ));
    }

    sources.insert(args.name.clone(), path.to_string_lossy().to_string());
    store.set_nested("ke.sources", serde_json::to_value(&sources).unwrap());

    fmt::success(&format!(
        "Added source '{}' -> {}",
        args.name,
        path.display()
    ));
    Ok(())
}

// ── ke source remove ────────────────────────────────────────────────────

async fn run_source_remove(args: KeSourceRemoveArgs) -> Result<()> {
    let mut store = ConfigStore::global();
    let mut sources = store.get_string_map("ke.sources");

    if sources.remove(&args.name).is_none() {
        fmt::error(&format!("Source '{}' not found.", args.name));
        return Ok(());
    }

    store.set_nested("ke.sources", serde_json::to_value(&sources).unwrap());
    fmt::success(&format!("Removed source '{}'.", args.name));
    Ok(())
}

// ── helpers ─────────────────────────────────────────────────────────────

/// Check if a directory contains a valid IDA binary.
fn is_ida_dir(path: &std::path::Path) -> bool {
    crate::ida::ida_binary_path(path).is_some()
}

/// Generate a short instance name from an IDA installation path.
fn generate_instance_name(path: &std::path::Path) -> String {
    let name = path
        .file_name()
        .unwrap_or_default()
        .to_string_lossy()
        .to_string();
    // Strip .app suffix on macOS.
    let name = name.strip_suffix(".app").unwrap_or(&name);
    name.to_lowercase()
        .replace(' ', "-")
        .replace("ida-professional", "ida-pro")
}