bashkit 0.20.1

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
//! sed - stream editor builtin.
//!
//! Important decisions (issue #2427):
//!
//! - Option parsing is a real getopt: short options cluster (`-ne`), take
//!   attached or separate arguments (`-e`, `-f`, `-l`), `-i` takes an *attached*
//!   suffix only, `--` ends option parsing, and the long spellings are
//!   accepted. Whether the first operand is the script or a file is decided
//!   after the scan, exactly as GNU does.
//! - All script fragments (`-e`, `-f`, the bare operand) are concatenated with
//!   newlines and compiled once, so multi-line `a\` text and `#` comments work.
//! - Without `-s`/`-i` every operand is one continuous stream: line numbers keep
//!   counting, `$` is the last line of the last file, and `q` stops everything.
//! - `-i` rewrites through [`crate::builtins::atomic_write::atomic_replace`],
//!   which preserves the file mode and leaves the original intact on failure
//!   (THREAT[TM-FS-016]), and refuses non-UTF-8 input rather than corrupting it.

mod exec;
mod pattern;

// Shared with `grep`: one POSIX BRE/ERE -> `regex` crate translator.
pub(crate) use pattern::translate as translate_posix_regex;
mod script;

#[cfg(test)]
mod tests;

use std::collections::HashMap;
use std::path::PathBuf;

use async_trait::async_trait;

use exec::{InputLine, Machine};

use super::atomic_write::{AtomicFailpoints, atomic_replace};
use super::{Builtin, Context};
use crate::error::Result;
use crate::fs::vfs_join;
use crate::interpreter::ExecResult;
use crate::limits::ExecutionBudget;

const HELP: &str = "Usage: sed [OPTION]... {script} [FILE]...\n\
Stream editor for filtering and transforming text.\n\n  \
-n, --quiet, --silent\tsuppress automatic printing of pattern space\n  \
-e script, --expression=script\n\t\tadd the script to the commands to be executed\n  \
-f file, --file=file\tadd the contents of script-file to the commands\n  \
-i[SUFFIX], --in-place[=SUFFIX]\n\t\tedit files in place (makes backup if SUFFIX supplied)\n  \
-E, -r, --regexp-extended\n\t\tuse extended regular expressions\n  \
-s, --separate\tconsider files as separate rather than as a single stream\n  \
-z, --null-data\tseparate lines by NUL characters\n  \
-l N, --line-length=N\tspecify the desired line-wrap length for the `l' command\n  \
-u, --unbuffered\taccepted for compatibility (no effect)\n  \
--help\t\tdisplay this help and exit\n  \
--version\toutput version information and exit\n";

/// The `l` command wraps here unless `-l` or an explicit argument says otherwise.
const DEFAULT_LINE_LENGTH: usize = 70;

/// sed command - stream editor
pub struct Sed;

enum ScriptSource {
    Text(String),
    File(String),
}

struct Options {
    sources: Vec<ScriptSource>,
    files: Vec<String>,
    /// `Some(suffix)`; an empty suffix means no backup copy.
    in_place: Option<String>,
    quiet: bool,
    extended: bool,
    separate: bool,
    null_data: bool,
    line_length: usize,
}

fn usage_error(message: impl std::fmt::Display) -> Box<ExecResult> {
    Box::new(ExecResult::err(format!("sed: {message}\n"), 1))
}

impl Options {
    fn parse(args: &[String]) -> std::result::Result<Options, Box<ExecResult>> {
        let mut opts = Options {
            sources: Vec::new(),
            files: Vec::new(),
            in_place: None,
            quiet: false,
            extended: false,
            separate: false,
            null_data: false,
            line_length: DEFAULT_LINE_LENGTH,
        };
        let mut operands: Vec<String> = Vec::new();
        let mut end_of_options = false;
        let mut i = 0;

        while i < args.len() {
            let arg = &args[i];
            if end_of_options || arg == "-" || !arg.starts_with('-') {
                operands.push(arg.clone());
                i += 1;
                continue;
            }
            if arg == "--" {
                end_of_options = true;
                i += 1;
                continue;
            }
            if let Some(long) = arg.strip_prefix("--") {
                let (name, value) = match long.split_once('=') {
                    Some((n, v)) => (n, Some(v.to_string())),
                    None => (long, None),
                };
                match name {
                    "quiet" | "silent" => opts.quiet = true,
                    "regexp-extended" => opts.extended = true,
                    "separate" => opts.separate = true,
                    "null-data" | "zero-terminated" => opts.null_data = true,
                    "unbuffered" | "debug" | "posix" | "sandbox" | "follow-symlinks" => {}
                    "in-place" => opts.in_place = Some(value.unwrap_or_default()),
                    "expression" => {
                        let value = take_value(args, &mut i, value, arg)?;
                        opts.sources.push(ScriptSource::Text(value));
                    }
                    "file" => {
                        let value = take_value(args, &mut i, value, arg)?;
                        opts.sources.push(ScriptSource::File(value));
                    }
                    "line-length" => {
                        let value = take_value(args, &mut i, value, arg)?;
                        opts.line_length = parse_length(&value)?;
                    }
                    _ => return Err(Box::new(super::invalid_option("sed", arg, 1))),
                }
                i += 1;
                continue;
            }

            let chars: Vec<char> = arg.chars().skip(1).collect();
            let mut j = 0;
            while j < chars.len() {
                match chars[j] {
                    'n' => opts.quiet = true,
                    'r' | 'E' => opts.extended = true,
                    's' => opts.separate = true,
                    'z' => opts.null_data = true,
                    'u' => {}
                    'e' | 'f' | 'l' => {
                        let flag = chars[j];
                        let attached: String = chars[j + 1..].iter().collect();
                        let value = if attached.is_empty() {
                            i += 1;
                            match args.get(i) {
                                Some(v) => v.clone(),
                                None => {
                                    return Err(usage_error(format!(
                                        "option requires an argument -- '{flag}'"
                                    )));
                                }
                            }
                        } else {
                            attached
                        };
                        match flag {
                            'e' => opts.sources.push(ScriptSource::Text(value)),
                            'f' => opts.sources.push(ScriptSource::File(value)),
                            _ => opts.line_length = parse_length(&value)?,
                        }
                        j = chars.len();
                        continue;
                    }
                    'i' => {
                        // GNU only accepts an *attached* suffix, so that
                        // `sed -i 's/a/b/' f` edits f rather than creating `s/a/b/`.
                        opts.in_place = Some(chars[j + 1..].iter().collect());
                        j = chars.len();
                        continue;
                    }
                    _ => {
                        let token = format!("-{}", chars[j]);
                        return Err(Box::new(super::invalid_option("sed", &token, 1)));
                    }
                }
                j += 1;
            }
            i += 1;
        }

        // GNU decides script-vs-file only after the whole scan: any -e/-f makes
        // every operand a file name.
        let mut operands = operands.into_iter();
        if opts.sources.is_empty() {
            match operands.next() {
                Some(script) => opts.sources.push(ScriptSource::Text(script)),
                None => {
                    return Err(Box::new(ExecResult::err(
                        format!("Usage: sed [OPTION]... {{script}} [FILE]...\n{HELP}"),
                        1,
                    )));
                }
            }
        }
        opts.files.extend(operands);

        if opts.in_place.is_some() {
            if opts.files.is_empty() {
                return Err(Box::new(ExecResult::err(
                    "sed: no input files\n".to_string(),
                    4,
                )));
            }
            // -i implies -s: each file is its own stream.
            opts.separate = true;
        }
        Ok(opts)
    }
}

fn take_value(
    args: &[String],
    i: &mut usize,
    inline: Option<String>,
    arg: &str,
) -> std::result::Result<String, Box<ExecResult>> {
    if let Some(v) = inline {
        return Ok(v);
    }
    *i += 1;
    args.get(*i)
        .cloned()
        .ok_or_else(|| usage_error(format!("option '{arg}' requires an argument")))
}

fn parse_length(value: &str) -> std::result::Result<usize, Box<ExecResult>> {
    value
        .parse::<usize>()
        .map_err(|_| usage_error(format!("invalid line length: {value}")))
}

/// Split input into lines, remembering whether the final line was terminated.
fn split_lines(content: &str, sep: char, file: usize, out: &mut Vec<InputLine>) {
    if content.is_empty() {
        return;
    }
    let mut rest = content;
    while let Some(idx) = rest.find(sep) {
        out.push(InputLine {
            text: rest[..idx].to_string(),
            had_newline: true,
            file,
        });
        rest = &rest[idx + sep.len_utf8()..];
    }
    if !rest.is_empty() {
        out.push(InputLine {
            text: rest.to_string(),
            had_newline: false,
            file,
        });
    }
}

fn resolve(cwd: &std::path::Path, name: &str) -> PathBuf {
    if name.starts_with('/') {
        PathBuf::from(name)
    } else {
        vfs_join(cwd, name)
    }
}

#[async_trait]
impl Builtin for Sed {
    async fn execute(&self, ctx: Context<'_>) -> Result<ExecResult> {
        if let Some(r) = super::check_help_version(ctx.args, HELP, Some("sed (bashkit) 0.1")) {
            return Ok(r);
        }

        let opts = match Options::parse(ctx.args) {
            Ok(o) => o,
            Err(e) => return Ok(*e),
        };

        // Assemble the whole script before compiling it: GNU joins every -e and
        // -f fragment with newlines and parses the result as one program.
        let mut script = String::new();
        for source in &opts.sources {
            if !script.is_empty() {
                script.push('\n');
            }
            match source {
                ScriptSource::Text(text) => script.push_str(text),
                ScriptSource::File(name) => {
                    if name == "-" {
                        script.push_str(
                            ctx.stdin
                                .map(ToString::to_string)
                                .unwrap_or_default()
                                .as_str(),
                        );
                        continue;
                    }
                    let path = resolve(ctx.cwd, name);
                    match ctx.fs.read_file(&path).await {
                        Ok(bytes) => {
                            let text = String::from_utf8_lossy(&bytes);
                            let trimmed = text.trim_end_matches('\n');
                            script.push_str(trimmed);
                            // An escaped final newline (`i\` + newline) is
                            // text, an empty line in GNU sed; keep it.
                            let escapes = trimmed.chars().rev().take_while(|&c| c == '\\').count();
                            if escapes % 2 == 1 && trimmed.len() < text.len() {
                                script.push('\n');
                            }
                        }
                        // GNU exits 4 when a script file cannot be opened.
                        Err(e) => {
                            return Ok(ExecResult::err(
                                format!("sed: couldn't open file {name}: {e}\n"),
                                4,
                            ));
                        }
                    }
                }
            }
        }

        let program = match script::parse_with(&script, opts.extended, opts.null_data) {
            Ok(p) => p,
            Err(e) => return Ok(ExecResult::err(format!("sed: {}\n", e.message), e.code)),
        };

        let sep = if opts.null_data { '\0' } else { '\n' };

        // `r`/`R` operands are read up front; a missing file is silently ignored
        // exactly as in GNU sed.
        let mut read_files: HashMap<String, String> = HashMap::new();
        for name in &program.read_files {
            if read_files.contains_key(name) {
                continue;
            }
            let text = if name == "/dev/stdin" || name == "-" {
                ctx.stdin.map(ToString::to_string).unwrap_or_default()
            } else {
                let path = resolve(ctx.cwd, name);
                match ctx.fs.read_file(&path).await {
                    Ok(bytes) => String::from_utf8_lossy(&bytes).into_owned(),
                    Err(_) => continue,
                }
            };
            read_files.insert(name.clone(), text);
        }

        // Gather input.
        let names: Vec<String> = if opts.files.is_empty() {
            vec!["-".to_string()]
        } else {
            opts.files.clone()
        };
        let mut lines: Vec<InputLine> = Vec::new();
        let mut raw: Vec<String> = Vec::new();
        // GNU reports an unreadable input file, goes on with the rest, and
        // exits 2 at the end.
        let mut read_errors = String::new();
        let mut unreadable: Vec<usize> = Vec::new();
        for (index, name) in names.iter().enumerate() {
            let content = if name == "-" || opts.files.is_empty() {
                ctx.stdin.map(ToString::to_string).unwrap_or_default()
            } else {
                let path = resolve(ctx.cwd, name);
                match ctx.fs.read_file(&path).await {
                    Ok(bytes) => match String::from_utf8(bytes) {
                        Ok(text) => text,
                        Err(e) => {
                            if opts.in_place.is_some() {
                                return Ok(ExecResult::err(
                                    format!(
                                        "sed: couldn't edit {name}: not a valid UTF-8 text file\n"
                                    ),
                                    1,
                                ));
                            }
                            String::from_utf8_lossy(e.as_bytes()).into_owned()
                        }
                    },
                    Err(e) => {
                        let reason = crate::error::io_error_reason(&e);
                        read_errors.push_str(&format!("sed: can't read {name}: {reason}\n"));
                        unreadable.push(index);
                        raw.push(String::new());
                        continue;
                    }
                }
            };
            split_lines(&content, sep, index, &mut lines);
            raw.push(content);
        }

        // Segment the stream: one segment overall, or one per operand under
        // `-s`/`-i`. Built before the machine so it can borrow the slices.
        let segments: Vec<Vec<InputLine>> = if opts.separate {
            (0..names.len())
                .map(|index| {
                    lines
                        .iter()
                        .filter(|l| l.file == index)
                        .map(|l| InputLine {
                            text: l.text.clone(),
                            had_newline: l.had_newline,
                            file: l.file,
                        })
                        .collect()
                })
                .collect()
        } else {
            vec![lines]
        };

        // THREAT[TM-DOS-112]: sed's sinks grow inside the builtin, so they are
        // charged to the shared live-intermediate budget before allocating. The
        // stdout *capture* cap is deliberately not used here: sed's output may
        // be piped to another command or redirected to a file, neither of which
        // is captured stdout, so bounding it by that cap would truncate
        // ordinary transformations (see #2455).
        let execution_budget = ctx
            .execution_budget()
            .and_then(|budget| budget.try_with(ExecutionBudget::clone).ok());
        // A dead budget fails the machine itself (fail closed) rather
        // than running pattern/hold uncharged.
        let mut machine = match Machine::new(
            &program,
            &names,
            &read_files,
            opts.quiet,
            opts.line_length,
            sep,
            execution_budget,
        ) {
            Ok(machine) => machine,
            Err(error) => {
                return Ok(ExecResult::err(format!("sed: {error}\n"), 1));
            }
        };

        let mut stdout = String::new();
        let mut edits: Vec<(usize, String, String)> = Vec::new();

        for (index, segment) in segments.iter().enumerate() {
            if machine.finished() {
                break;
            }
            let name = names.get(index).map(String::as_str).unwrap_or("-");
            // Under -s/-i segments are per operand; skip the unreadable ones.
            if opts.separate && unreadable.contains(&index) {
                continue;
            }
            let in_place = opts.separate && opts.in_place.is_some() && name != "-";
            let produced = match machine.run_segment(segment) {
                Ok(produced) => produced,
                Err(error) => return Ok(ExecResult::err(format!("sed: {error}\n"), 1)),
            };
            if in_place {
                edits.push((index, name.to_string(), produced));
            } else {
                stdout.push_str(&produced);
            }
        }

        let mut stderr = read_errors;
        stderr.push_str(&std::mem::take(&mut machine.stderr));
        let exit_code = machine
            .exit_code
            .unwrap_or(if unreadable.is_empty() { 0 } else { 2 });
        let write_files = std::mem::take(&mut machine.write_files);
        drop(machine);

        // Files named by `w`/`W`/`s///w`.
        for (name, sink) in write_files {
            if let Some(error) = sink.error {
                return Ok(ExecResult::err(format!("sed: {error}\n"), 1));
            }
            let path = resolve(ctx.cwd, &name);
            if let Err(e) = ctx.fs.write_file(&path, sink.buf.as_bytes()).await {
                return Ok(ExecResult::err(
                    format!("sed: couldn't open {name}: {e}\n"),
                    4,
                ));
            }
        }

        // In-place rewrites.
        for (index, name, content) in edits {
            let path = resolve(ctx.cwd, &name);
            if let Some(suffix) = opts.in_place.as_deref().filter(|s| !s.is_empty()) {
                let backup = if suffix.contains('*') {
                    suffix.replace('*', &name)
                } else {
                    format!("{name}{suffix}")
                };
                let backup_path = resolve(ctx.cwd, &backup);
                let original = raw.get(index).cloned().unwrap_or_default();
                if let Err(e) = ctx.fs.write_file(&backup_path, original.as_bytes()).await {
                    return Ok(ExecResult::err(
                        format!("sed: cannot rename {name}: {e}\n"),
                        4,
                    ));
                }
            }
            if let Err(e) = atomic_replace(
                &*ctx.fs,
                &path,
                content.as_bytes(),
                "sed",
                AtomicFailpoints {
                    allocate: "sed::temp_allocate",
                    chmod: "sed::temp_chmod",
                    rename: "sed::temp_rename",
                },
            )
            .await
            {
                return Ok(ExecResult::err(format!("sed: {name}: {e}\n"), 4));
            }
        }

        if stderr.len() > 1024 {
            stderr.truncate(1024);
        }
        Ok(ExecResult {
            stdout: stdout.into(),
            stderr: stderr.into(),
            exit_code,
            ..Default::default()
        })
    }
}