bashkit 0.19.0

Awesomely fast virtual sandbox with bash and file system
Documentation
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
//! The `bind` builtin, as a non-interactive bash shows it: bashkit has no
//! readline, so bindings are recorded and reported, never acted on.
//!
//! Decisions:
//! - Defaults are readline's emacs keymap (`readline_defaults.rs`); changes
//!   (`bind 'seq: fn'`, `-x`, `-r`, `-u`, `set var val`) are kept per
//!   interpreter in [`Bindings`] (inside `CompletionState`), built on first
//!   use. Other keymaps (`-m vi`) start empty.
//! - THREAT[TM-DOS-132]: at most [`MAX_BINDINGS`] changes of at most
//!   [`MAX_BINDING_BYTES`] each; `bind -f` reads at most `max_input_bytes`
//!   from the VFS.
//! - Outside `bash -i` every call warns `line editing not enabled` on stderr,
//!   as bash does.

use super::{ExecResult, Interpreter};
use crate::error::Result;
use crate::parser::Redirect;

pub(crate) use super::readline_defaults::FUNCTION_NAMES;
use super::readline_defaults::{DEFAULT_EMACS_BINDINGS, VARIABLES};

/// THREAT[TM-DOS-132]: cap on recorded binding changes per interpreter.
pub(crate) const MAX_BINDINGS: usize = 1024;
/// THREAT[TM-DOS-132]: cap on one binding's text.
pub(crate) const MAX_BINDING_BYTES: usize = 4096;

const USAGE: &str = "bind: usage: bind [-lpsvPSVX] [-m keymap] [-f filename] [-q name] [-u name] [-r keyseq] [-x keyseq:shell-command] [keyseq:readline-function or readline-command]\n";

const KEYMAPS: &[&str] = &[
    "emacs",
    "emacs-standard",
    "emacs-meta",
    "emacs-ctlx",
    "vi",
    "vi-move",
    "vi-command",
    "vi-insert",
];

#[derive(Debug, Clone, PartialEq, Eq)]
enum Target {
    Function(String),
    Macro(String),
    Command(String),
}

#[derive(Debug, Clone)]
struct Binding {
    keymap: &'static str,
    /// Key sequence as written (`\C-o\C-s`).
    seq: String,
    /// `None`: an unbound key (`bind -r`, `bind -u`).
    target: Option<Target>,
}

/// Binding changes and variable settings of one interpreter.
#[derive(Debug, Clone, Default)]
pub(crate) struct Bindings {
    changes: Vec<Binding>,
    variables: Vec<(String, String)>,
}

/// Canonical keymap for `-m NAME` (`emacs` is `emacs-standard`, `vi` is
/// `vi-command`).
fn keymap_name(name: &str) -> Option<&'static str> {
    match name {
        "emacs" | "emacs-standard" => Some("emacs"),
        "vi" | "vi-move" | "vi-command" => Some("vi"),
        other => KEYMAPS.iter().find(|k| **k == other).copied(),
    }
}

/// Readline key-sequence notation to bytes, for comparing and ordering.
fn keyseq_bytes(seq: &str) -> Vec<u8> {
    let mut out = Vec::new();
    let chars: Vec<char> = seq.chars().collect();
    let mut i = 0;
    let mut meta = false;
    let mut control = false;
    while i < chars.len() {
        let mut byte: Option<u8> = None;
        if chars[i] == '\\' && i + 1 < chars.len() {
            let n = chars[i + 1];
            if (n == 'C' || n == 'M') && chars.get(i + 2) == Some(&'-') {
                if n == 'C' {
                    control = true;
                } else {
                    meta = true;
                }
                i += 3;
                continue;
            }
            i += 2;
            byte = Some(match n {
                'e' => 0x1b,
                'a' => 0x07,
                'b' => 0x08,
                'd' => 0x7f,
                'f' => 0x0c,
                'n' => b'\n',
                'r' => b'\r',
                't' => b'\t',
                'v' => 0x0b,
                c => c as u8,
            });
        }
        let mut b = byte.unwrap_or_else(|| {
            let c = chars[i];
            i += 1;
            c as u8
        });
        if control {
            b = if b == b'?' { 0x7f } else { b & 0x1f };
            control = false;
        }
        if meta {
            out.push(0x1b);
            meta = false;
        }
        out.push(b);
    }
    out
}

/// Strip the quotes of `"seq"` (or take a bare sequence) and split
/// `"seq": rest`.
fn split_binding(spec: &str) -> Option<(String, String)> {
    let spec = spec.trim_start();
    if let Some(body) = spec.strip_prefix('"') {
        let mut seq = String::new();
        let mut chars = body.char_indices();
        while let Some((idx, c)) = chars.next() {
            match c {
                '\\' => {
                    seq.push(c);
                    if let Some((_, n)) = chars.next() {
                        seq.push(n);
                    }
                }
                '"' => {
                    let rest = body[idx + 1..].trim_start();
                    let rest = rest.strip_prefix(':')?;
                    return Some((seq, rest.trim().to_string()));
                }
                c => seq.push(c),
            }
        }
        None
    } else {
        let (seq, rest) = spec.split_once(':')?;
        Some((seq.trim().to_string(), rest.trim().to_string()))
    }
}

fn unquote(s: &str) -> String {
    let s = s.trim();
    for q in ['"', '\''] {
        if s.len() >= 2 && s.starts_with(q) && s.ends_with(q) {
            return s[1..s.len() - 1].to_string();
        }
    }
    s.to_string()
}

impl Bindings {
    pub(super) fn fork_storage(
        &self,
        storage: &mut super::pipeline_admission::ForkStorage<'_>,
    ) -> Result<()> {
        storage.slice::<Binding>(self.changes.len())?;
        for binding in &self.changes {
            storage.add(binding.seq.len())?;
            if let Some(Target::Function(text) | Target::Macro(text) | Target::Command(text)) =
                &binding.target
            {
                storage.add(text.len())?;
            }
        }
        storage.slice::<(String, String)>(self.variables.len())?;
        for (name, value) in &self.variables {
            storage.add(name.len())?;
            storage.add(value.len())?;
        }
        Ok(())
    }

    /// Every live binding of `keymap`: defaults minus unbound keys, plus
    /// changes (a later change of the same keys wins).
    fn effective(&self, keymap: &str) -> Vec<(Vec<u8>, String, Target)> {
        let mut map: Vec<(Vec<u8>, String, Target)> = Vec::new();
        if keymap == "emacs" {
            for line in DEFAULT_EMACS_BINDINGS.lines() {
                if let Some((seq, func)) = split_binding(line) {
                    map.push((keyseq_bytes(&seq), seq, Target::Function(func)));
                }
            }
        }
        for change in self.changes.iter().filter(|c| c.keymap == keymap) {
            let bytes = keyseq_bytes(&change.seq);
            map.retain(|(b, ..)| *b != bytes);
            if let Some(target) = &change.target {
                map.push((bytes, change.seq.clone(), target.clone()));
            }
        }
        map
    }

    fn keys_for(&self, keymap: &str, function: &str) -> Vec<String> {
        let mut keys: Vec<(Vec<u8>, String)> = self
            .effective(keymap)
            .into_iter()
            .filter(|(_, _, t)| *t == Target::Function(function.to_string()))
            .map(|(b, s, _)| (b, s))
            .collect();
        keys.sort();
        keys.into_iter().map(|(_, s)| s).collect()
    }

    fn push(&mut self, binding: Binding) -> std::result::Result<(), String> {
        if binding.seq.len()
            + match &binding.target {
                Some(Target::Function(s) | Target::Macro(s) | Target::Command(s)) => s.len(),
                None => 0,
            }
            > MAX_BINDING_BYTES
        {
            return Err(format!(
                "bind: binding too long (limit {MAX_BINDING_BYTES} bytes)\n"
            ));
        }
        let bytes = keyseq_bytes(&binding.seq);
        self.changes
            .retain(|c| !(c.keymap == binding.keymap && keyseq_bytes(&c.seq) == bytes));
        if self.changes.len() >= MAX_BINDINGS {
            return Err(format!("bind: too many bindings (limit {MAX_BINDINGS})\n"));
        }
        self.changes.push(binding);
        Ok(())
    }

    fn variable(&self, name: &str) -> Option<String> {
        self.variables
            .iter()
            .rev()
            .find(|(n, _)| n == name)
            .map(|(_, v)| v.clone())
            .or_else(|| {
                VARIABLES
                    .iter()
                    .find(|(n, _)| *n == name)
                    .map(|(_, v)| v.to_string())
            })
    }
}

impl Interpreter {
    fn bindings(&mut self) -> &mut Bindings {
        &mut self
            .completion
            .get_or_insert_with(Default::default)
            .bindings
    }

    /// The `bind` builtin.
    pub(super) async fn execute_bind_builtin(
        &mut self,
        args: &[String],
        redirects: &[Redirect],
    ) -> Result<ExecResult> {
        let mut result = self.bind_builtin(args).await;
        if !self.interactive {
            let warn = self.diag("bind: warning: line editing not enabled\n");
            let mut err = crate::StreamData::from(warn);
            err.append(&result.stderr);
            result.stderr = err;
        }
        self.redirect_result(result, redirects).await
    }

    async fn bind_builtin(&mut self, args: &[String]) -> ExecResult {
        let mut keymap: &'static str = "emacs";
        let mut list_functions = false;
        let mut print = false;
        let mut print_readable = false;
        let mut macros = false;
        let mut macros_readable = false;
        let mut vars = false;
        let mut vars_readable = false;
        let mut commands = false;
        let mut queries = Vec::new();
        let mut unbind_functions = Vec::new();
        let mut removes = Vec::new();
        let mut shell_bindings = Vec::new();
        let mut files = Vec::new();
        let mut idx = 0;
        while idx < args.len() {
            let arg = &args[idx];
            if arg == "--" {
                idx += 1;
                break;
            }
            if !arg.starts_with('-') || arg.len() < 2 {
                break;
            }
            let chars: Vec<char> = arg[1..].chars().collect();
            let mut ci = 0;
            while ci < chars.len() {
                let c = chars[ci];
                ci += 1;
                match c {
                    'l' => list_functions = true,
                    'p' => print_readable = true,
                    'P' => print = true,
                    's' => macros_readable = true,
                    'S' => macros = true,
                    'v' => vars_readable = true,
                    'V' => vars = true,
                    'X' => commands = true,
                    'm' | 'f' | 'q' | 'u' | 'r' | 'x' => {
                        let value = if ci < chars.len() {
                            let v: String = chars[ci..].iter().collect();
                            ci = chars.len();
                            v
                        } else {
                            idx += 1;
                            match args.get(idx) {
                                Some(v) => v.clone(),
                                None => {
                                    return ExecResult::err(
                                        format!(
                                            "{}{USAGE}",
                                            self.diag(format!(
                                                "bind: -{c}: option requires an argument\n"
                                            ))
                                        ),
                                        2,
                                    );
                                }
                            }
                        };
                        match c {
                            'm' => match keymap_name(&value) {
                                Some(k) => keymap = k,
                                None => {
                                    return ExecResult::err(
                                        self.diag(format!(
                                            "bind: `{value}': invalid keymap name\n"
                                        )),
                                        1,
                                    );
                                }
                            },
                            'f' => files.push(value),
                            'q' => queries.push(value),
                            'u' => unbind_functions.push(value),
                            'r' => removes.push(value),
                            _ => shell_bindings.push(value),
                        }
                    }
                    other => {
                        return ExecResult::err(
                            format!(
                                "{}{USAGE}",
                                self.diag(format!("bind: -{other}: invalid option\n"))
                            ),
                            2,
                        );
                    }
                }
            }
            idx += 1;
        }
        let rest: Vec<String> = args[idx..].to_vec();
        let mut out = String::new();
        let mut err = String::new();
        let mut status = 0;

        for file in &files {
            let path = self.resolve_path(file);
            match self.fs.read_file(&path).await {
                Ok(bytes) if bytes.len() <= self.limits.max_input_bytes => {
                    let text = String::from_utf8_lossy(&bytes).into_owned();
                    for line in text.lines() {
                        let line = line.trim();
                        if line.is_empty() || line.starts_with('#') || line.starts_with('$') {
                            continue;
                        }
                        if let Err(e) = self.bind_line(keymap, line) {
                            err.push_str(&self.diag(e));
                            status = 1;
                        }
                    }
                }
                _ => {
                    err.push_str(&self.diag(format!(
                        "bind: {file}: cannot read: No such file or directory\n"
                    )));
                    status = 1;
                }
            }
        }

        if list_functions {
            for name in FUNCTION_NAMES {
                out.push_str(name);
                out.push('\n');
            }
        }
        if print || print_readable {
            out.push('\n');
            let effective = self.bindings().effective(keymap);
            for name in FUNCTION_NAMES {
                let mut keys: Vec<(Vec<u8>, String)> = effective
                    .iter()
                    .filter(|(_, _, t)| *t == Target::Function(name.to_string()))
                    .map(|(b, s, _)| (b.clone(), s.clone()))
                    .collect();
                keys.sort();
                if print_readable {
                    if keys.is_empty() {
                        out.push_str(&format!("# {name} (not bound)\n"));
                    }
                    for (_, seq) in &keys {
                        out.push_str(&format!("\"{seq}\": {name}\n"));
                    }
                } else if keys.is_empty() {
                    out.push_str(&format!("{name} is not bound to any keys\n"));
                } else {
                    let list: Vec<String> = keys.iter().map(|(_, s)| format!("\"{s}\"")).collect();
                    out.push_str(&format!("{name} can be found on {}.\n", list.join(", ")));
                }
            }
        }
        if macros || macros_readable || commands {
            let effective = self.bindings().effective(keymap);
            let mut items: Vec<_> = effective.into_iter().collect();
            items.sort_by(|a, b| a.0.cmp(&b.0));
            for (_, seq, target) in items {
                match target {
                    Target::Macro(m) if macros_readable => {
                        out.push_str(&format!("\"{seq}\": \"{m}\"\n"))
                    }
                    Target::Macro(m) if macros => out.push_str(&format!("{seq} outputs {m}\n")),
                    Target::Command(c) if commands => {
                        out.push_str(&format!("\"{seq}\": \"{c}\"\n"))
                    }
                    _ => {}
                }
            }
        }
        if vars || vars_readable {
            let bindings = self.bindings().clone();
            for (name, _) in VARIABLES {
                let value = bindings.variable(name).unwrap_or_default();
                if vars_readable {
                    out.push_str(&format!("set {name} {value}\n"));
                } else {
                    out.push_str(&format!("{name} is set to `{value}'\n"));
                }
            }
        }
        for name in &queries {
            if !FUNCTION_NAMES.contains(&name.as_str()) {
                err.push_str(&self.diag(format!("bind: `{name}': unknown function name\n")));
                status = 1;
                continue;
            }
            let keys = self.bindings().keys_for(keymap, name);
            if keys.is_empty() {
                out.push_str(&format!("{name} is not bound to any keys.\n"));
                status = 1;
            } else {
                let list: Vec<String> = keys.iter().map(|s| format!("\"{s}\"")).collect();
                out.push_str(&format!("{name} can be invoked via {}.\n", list.join(", ")));
            }
        }
        for name in &unbind_functions {
            if !FUNCTION_NAMES.contains(&name.as_str()) {
                err.push_str(&self.diag(format!("bind: `{name}': unknown function name\n")));
                status = 1;
                continue;
            }
            for seq in self.bindings().keys_for(keymap, name) {
                if let Err(e) = self.bindings().push(Binding {
                    keymap,
                    seq,
                    target: None,
                }) {
                    err.push_str(&self.diag(e));
                    status = 1;
                }
            }
        }
        for seq in &removes {
            if let Err(e) = self.bindings().push(Binding {
                keymap,
                seq: seq.clone(),
                target: None,
            }) {
                err.push_str(&self.diag(e));
                status = 1;
            }
        }
        for spec in &shell_bindings {
            let Some((seq, command)) = split_binding(spec) else {
                err.push_str(&self.diag("bind: no colon in key sequence binding\n"));
                status = 1;
                continue;
            };
            if let Err(e) = self.bindings().push(Binding {
                keymap,
                seq,
                target: Some(Target::Command(unquote(&command))),
            }) {
                err.push_str(&self.diag(e));
                status = 1;
            }
        }
        for line in &rest {
            if let Err(e) = self.bind_line(keymap, line) {
                err.push_str(&self.diag(e));
                status = 1;
            }
        }
        let mut result = ExecResult::with_code(out, status);
        result.stderr = err.into();
        result
    }

    /// One inputrc line: `"seq": function`, `"seq": "macro"`, `set var val`.
    fn bind_line(&mut self, keymap: &'static str, line: &str) -> std::result::Result<(), String> {
        if let Some(rest) = line.trim_start().strip_prefix("set ") {
            let mut parts = rest.split_whitespace();
            let name = parts.next().unwrap_or("").to_string();
            let value = parts.collect::<Vec<_>>().join(" ");
            if VARIABLES.iter().any(|(n, _)| *n == name) {
                let bindings = self.bindings();
                if bindings.variables.len() >= MAX_BINDINGS {
                    return Err(format!("bind: too many bindings (limit {MAX_BINDINGS})\n"));
                }
                bindings.variables.retain(|(n, _)| *n != name);
                bindings.variables.push((name, value));
            }
            return Ok(());
        }
        let Some((seq, rhs)) = split_binding(line) else {
            return Ok(());
        };
        let target = if rhs.starts_with('"') || rhs.starts_with('\'') {
            Target::Macro(unquote(&rhs))
        } else {
            Target::Function(rhs)
        };
        self.bindings().push(Binding {
            keymap,
            seq,
            target: Some(target),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn keyseqs_order_like_readline() {
        assert!(keyseq_bytes("\\C-g") < keyseq_bytes("\\C-x\\C-g"));
        assert!(keyseq_bytes("\\C-x\\C-g") < keyseq_bytes("\\M-\\C-g"));
        assert_eq!(keyseq_bytes("\\C-o\\C-s"), vec![0x0f, 0x13]);
    }

    #[test]
    fn default_yank_is_c_y() {
        let b = Bindings::default();
        assert_eq!(b.keys_for("emacs", "yank"), vec!["\\C-y".to_string()]);
        assert!(b.keys_for("emacs", "vi-subst").is_empty());
    }

    #[test]
    fn binding_changes_are_capped() {
        let mut b = Bindings::default();
        for i in 0..MAX_BINDINGS {
            b.push(Binding {
                keymap: "emacs",
                seq: format!("\\C-x{i}"),
                target: Some(Target::Command("x".into())),
            })
            .unwrap();
        }
        assert!(
            b.push(Binding {
                keymap: "emacs",
                seq: "\\C-zz".into(),
                target: None,
            })
            .is_err()
        );
    }
}