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
//! seq builtin - print a sequence of numbers
//!
//! Decision: output formatting follows GNU `seq.c`: the default format is
//! `%.Pf` with P the larger fraction-digit count of FIRST and INCREMENT
//! (LAST does not count), `-w` zero-pads to the wider of FIRST/LAST in that
//! format, `-f` goes through the printf formatter, and each value is
//! computed as FIRST + i*INCREMENT so steps do not accumulate rounding.

use async_trait::async_trait;

use super::limits::{SEQ_MAX_LINES, SEQ_MAX_OUTPUT_BYTES, cap_exceeded};
use super::{Builtin, Context};
use crate::error::Result;
use crate::interpreter::ExecResult;

/// The seq builtin - print a sequence of numbers.
///
/// Usage: seq [OPTION]... LAST
///        seq [OPTION]... FIRST LAST
///        seq [OPTION]... FIRST INCREMENT LAST
///
/// Options:
///   -f FORMAT  Use printf-style floating-point FORMAT
///   -s STRING  Use STRING as separator (default: newline)
///   -w         Equalize width by padding with leading zeroes
pub struct Seq;

/// A parsed operand with GNU's precision/width bookkeeping.
struct Operand {
    value: f64,
    /// Digits after the decimal point; `None` when not representable
    /// as fixed-point (hex, inf, nan).
    precision: Option<usize>,
    width: usize,
}

fn parse_operand(arg: &str) -> Option<Operand> {
    let t = arg.trim_start();
    let value: f64 = {
        let lower = t.to_ascii_lowercase();
        let body = lower.trim_start_matches(['+', '-']);
        if let Some(hex) = body.strip_prefix("0x") {
            let neg = lower.starts_with('-');
            let v = u64::from_str_radix(hex, 16).ok()? as f64;
            if neg { -v } else { v }
        } else {
            t.parse().ok()?
        }
    };
    if value.is_nan() {
        return None;
    }
    let plain = !t.to_ascii_lowercase().contains(['x', 'i', 'n']);
    let precision = plain.then(|| {
        let mantissa_end = t.find(['e', 'E']).unwrap_or(t.len());
        let frac = t[..mantissa_end]
            .split_once('.')
            .map_or(0, |(_, f)| f.len());
        let exp: i64 = t
            .get(mantissa_end + 1..)
            .and_then(|e| e.parse().ok())
            .unwrap_or(0);
        if exp < 0 {
            frac + exp.unsigned_abs() as usize
        } else {
            frac.saturating_sub(exp as usize)
        }
    });
    Some(Operand {
        value,
        precision,
        width: t.len(),
    })
}

/// GNU `get_default_format`, as (zero-pad width, precision).
fn default_format(
    first: &Operand,
    step: &Operand,
    last: &Operand,
    equal_width: bool,
) -> Option<(usize, usize)> {
    let prec = first.precision?.max(step.precision?);
    let last_prec = last.precision?;
    if !equal_width {
        return Some((0, prec));
    }
    let first_prec = first.precision?;
    let mut first_width = first.width + (prec - first_prec);
    let mut last_width = (last.width + prec).saturating_sub(last_prec);
    if last_prec > 0 && prec == 0 {
        last_width = last_width.saturating_sub(1);
    }
    if last_prec == 0 && prec > 0 {
        last_width += 1;
    }
    if first_prec == 0 && prec > 0 {
        first_width += 1;
    }
    Some((first_width.max(last_width), prec))
}

fn invalid_arg(arg: &str) -> ExecResult {
    ExecResult::err(
        format!(
            "seq: invalid floating point argument: '{arg}'\nTry 'seq --help' for more information.\n"
        ),
        1,
    )
}

/// Bytes per write when streaming into a pipeline (one pipe's capacity).
const STREAM_CHUNK_BYTES: usize = 4 * 1024;

#[async_trait]
impl Builtin for Seq {
    async fn execute(&self, ctx: Context<'_>) -> Result<ExecResult> {
        if let Some(r) = super::check_help_version(
            ctx.args,
            "Usage: seq [OPTION]... LAST\n  or:  seq [OPTION]... FIRST LAST\n  or:  seq [OPTION]... FIRST INCREMENT LAST\nPrint numbers from FIRST to LAST, in steps of INCREMENT.\n\n  -f, --format=FORMAT\tuse printf style floating-point FORMAT\n  -s, --separator=STRING\tuse STRING to separate numbers (default: \\n)\n  -w, --equal-width\tequalize width by padding with leading zeroes\n  --help\tdisplay this help and exit\n  --version\toutput version information and exit\n",
            Some("seq (bashkit) 0.1"),
        ) {
            return Ok(r);
        }
        // GNU checks each option position: an argument that looks like a
        // negative number ends option parsing, so `seq -3 -1` counts while
        // `seq -s -1 1 2` still takes `-1` as the separator.
        let mut separator = "\n".to_string();
        let mut equal_width = false;
        let mut format: Option<String> = None;
        let mut nums: Vec<String> = Vec::new();
        let args = ctx.args;
        let mut i = 0;
        while i < args.len() {
            let a = args[i].as_str();
            i += 1;
            let b = a.as_bytes();
            if b.len() > 1 && b[0] == b'-' && (b[1].is_ascii_digit() || b[1] == b'.') {
                nums.extend(args[i - 1..].iter().cloned());
                break;
            }
            if a == "--" {
                nums.extend(args[i..].iter().cloned());
                break;
            }
            if !a.starts_with('-') || a == "-" {
                nums.push(a.to_string());
                continue;
            }
            if let Some(long) = a.strip_prefix("--") {
                let (name, inline) = match long.split_once('=') {
                    Some((n, v)) => (n, Some(v.to_string())),
                    None => (long, None),
                };
                let key = ["equal-width", "format", "separator"]
                    .into_iter()
                    .find(|n| !name.is_empty() && n.starts_with(name));
                let Some(key) = key else {
                    return Ok(super::invalid_option("seq", a, 1));
                };
                if key == "equal-width" {
                    equal_width = true;
                    continue;
                }
                let value = match inline {
                    Some(v) => v,
                    None if i < args.len() => {
                        i += 1;
                        args[i - 1].clone()
                    }
                    None => {
                        return Ok(ExecResult::err(
                            format!("seq: option '--{key}' requires an argument\n"),
                            1,
                        ));
                    }
                };
                if key == "format" {
                    format = Some(value);
                } else {
                    separator = value;
                }
                continue;
            }
            for (pos, c) in a[1..].char_indices() {
                match c {
                    'w' => equal_width = true,
                    'f' | 's' => {
                        let attached = &a[1 + pos + 1..];
                        let value = if !attached.is_empty() {
                            attached.to_string()
                        } else if i < args.len() {
                            i += 1;
                            args[i - 1].clone()
                        } else {
                            return Ok(ExecResult::err(
                                format!("seq: option requires an argument -- '{c}'\n"),
                                1,
                            ));
                        };
                        if c == 'f' {
                            format = Some(value);
                        } else {
                            separator = value;
                        }
                        break;
                    }
                    _ => return Ok(super::invalid_option("seq", &format!("-{c}"), 1)),
                }
            }
        }
        if format.is_some() && equal_width {
            return Ok(ExecResult::err(
                "seq: format string may not be specified when printing equal width strings\n"
                    .to_string(),
                1,
            ));
        }

        if nums.is_empty() {
            return Ok(ExecResult::err("seq: missing operand\n".to_string(), 1));
        }
        if nums.len() > 3 {
            return Ok(ExecResult::err(
                format!("seq: extra operand '{}'\n", nums[3]),
                1,
            ));
        }
        let mut parsed = Vec::with_capacity(3);
        for n in &nums {
            match parse_operand(n) {
                Some(op) => parsed.push(op),
                None => return Ok(invalid_arg(n)),
            }
        }
        let one = || Operand {
            value: 1.0,
            precision: Some(0),
            width: 1,
        };
        let (first, step, last) = match parsed.len() {
            1 => (one(), one(), parsed.remove(0)),
            2 => {
                let last = parsed.remove(1);
                (parsed.remove(0), one(), last)
            }
            _ => {
                let last = parsed.remove(2);
                let step = parsed.remove(1);
                (parsed.remove(0), step, last)
            }
        };

        if step.value == 0.0 {
            return Ok(ExecResult::err("seq: zero increment\n".to_string(), 1));
        }

        let fixed = default_format(&first, &step, &last, equal_width);
        let render = |x: f64| -> std::result::Result<String, String> {
            if let Some(fmt) = &format {
                let bytes = super::printf::render_printf_bytes(fmt, &[format!("{x}")])?;
                return Ok(String::from_utf8_lossy(&bytes).into_owned());
            }
            Ok(match fixed {
                Some((width, prec)) => format!("{x:0width$.prec$}"),
                None => {
                    let bytes = super::printf::render_printf_bytes("%g", &[format!("{x}")])?;
                    String::from_utf8_lossy(&bytes).into_owned()
                }
            })
        };

        let mut output = String::new();
        // Pipeline producer: flush full chunks as they are made, so
        // `seq 1000000 | head -1` stops early with SIGPIPE.
        let stream = ctx.stdout_stream();
        let mut flushed = 0usize;
        // THREAT[TM-DOS-058]: Limit iterations and output size to prevent memory
        // exhaustion; THREAT[TM-DOS-109]: hitting either cap is reported.
        let mut capped = None;
        let mut i: u64 = 0;
        loop {
            let x = first.value + (i as f64) * step.value;
            if (step.value > 0.0 && x > last.value) || (step.value < 0.0 && x < last.value) {
                break;
            }
            if i as usize >= SEQ_MAX_LINES {
                capped = Some(("line", SEQ_MAX_LINES));
                break;
            }
            if let Some(stream) = &stream
                && output.len() >= STREAM_CHUNK_BYTES
            {
                ctx.consume_budget_work(1)?;
                if !stream.write(output.as_bytes()).await? {
                    return Ok(ExecResult::with_code("", 141));
                }
                flushed += output.len();
                output.clear();
            }
            if flushed + output.len() > SEQ_MAX_OUTPUT_BYTES {
                capped = Some(("output byte", SEQ_MAX_OUTPUT_BYTES));
                break;
            }
            if i > 0 {
                output.push_str(&separator);
            }
            match render(x) {
                Ok(s) => output.push_str(&s),
                Err(e) => return Ok(ExecResult::err(e.replacen("printf:", "seq:", 1), 1)),
            }
            i += 1;
            if !x.is_finite() {
                break;
            }
        }

        if i > 0 {
            output.push('\n');
        }
        if let Some(stream) = &stream {
            if !output.is_empty() && !stream.write(output.as_bytes()).await? {
                return Ok(ExecResult::with_code("", 141));
            }
            output.clear();
        }

        if let Some((what, limit)) = capped {
            return Ok(cap_exceeded("seq", output, what, limit));
        }
        Ok(ExecResult::ok(output))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;
    use std::path::PathBuf;
    use std::sync::Arc;

    use crate::fs::{FileSystem, InMemoryFs};

    async fn setup() -> (Arc<InMemoryFs>, PathBuf, HashMap<String, String>) {
        let fs = Arc::new(InMemoryFs::new());
        let cwd = PathBuf::from("/home/user");
        let variables = HashMap::new();
        fs.mkdir(&cwd, true).await.unwrap();
        (fs, cwd, variables)
    }

    // ==================== basic ranges ====================

    #[tokio::test]
    async fn seq_single_arg() {
        let (fs, mut cwd, mut variables) = setup().await;
        let env = HashMap::new();
        let args = vec!["5".to_string()];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        let result = Seq.execute(ctx).await.unwrap();
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.stdout, "1\n2\n3\n4\n5\n");
    }

    #[tokio::test]
    async fn seq_two_args() {
        let (fs, mut cwd, mut variables) = setup().await;
        let env = HashMap::new();
        let args = vec!["3".to_string(), "6".to_string()];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        let result = Seq.execute(ctx).await.unwrap();
        assert_eq!(result.stdout, "3\n4\n5\n6\n");
    }

    #[tokio::test]
    async fn seq_three_args_increment() {
        let (fs, mut cwd, mut variables) = setup().await;
        let env = HashMap::new();
        let args = vec!["1".to_string(), "2".to_string(), "9".to_string()];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        let result = Seq.execute(ctx).await.unwrap();
        assert_eq!(result.stdout, "1\n3\n5\n7\n9\n");
    }

    #[tokio::test]
    async fn seq_descending() {
        let (fs, mut cwd, mut variables) = setup().await;
        let env = HashMap::new();
        let args = vec!["5".to_string(), "-1".to_string(), "1".to_string()];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        let result = Seq.execute(ctx).await.unwrap();
        assert_eq!(result.stdout, "5\n4\n3\n2\n1\n");
    }

    #[tokio::test]
    async fn seq_single_element() {
        let (fs, mut cwd, mut variables) = setup().await;
        let env = HashMap::new();
        let args = vec!["1".to_string()];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        let result = Seq.execute(ctx).await.unwrap();
        assert_eq!(result.stdout, "1\n");
    }

    #[tokio::test]
    async fn seq_empty_range() {
        let (fs, mut cwd, mut variables) = setup().await;
        let env = HashMap::new();
        // first > last with positive increment => empty output
        let args = vec!["5".to_string(), "1".to_string(), "3".to_string()];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        let result = Seq.execute(ctx).await.unwrap();
        assert_eq!(result.exit_code, 0);
        assert!(result.stdout.is_empty());
    }

    // ==================== separator (-s) ====================

    #[tokio::test]
    async fn seq_custom_separator() {
        let (fs, mut cwd, mut variables) = setup().await;
        let env = HashMap::new();
        let args = vec!["-s".to_string(), ",".to_string(), "3".to_string()];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        let result = Seq.execute(ctx).await.unwrap();
        assert_eq!(result.stdout, "1,2,3\n");
    }

    #[tokio::test]
    async fn seq_separator_no_space() {
        let (fs, mut cwd, mut variables) = setup().await;
        let env = HashMap::new();
        let args = vec!["-s,".to_string(), "3".to_string()];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        let result = Seq.execute(ctx).await.unwrap();
        assert_eq!(result.stdout, "1,2,3\n");
    }

    // ==================== zero-padding (-w) ====================

    #[tokio::test]
    async fn seq_zero_padding() {
        let (fs, mut cwd, mut variables) = setup().await;
        let env = HashMap::new();
        let args = vec!["-w".to_string(), "8".to_string(), "10".to_string()];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        let result = Seq.execute(ctx).await.unwrap();
        assert_eq!(result.stdout, "08\n09\n10\n");
    }

    #[tokio::test]
    async fn seq_zero_padding_large() {
        let (fs, mut cwd, mut variables) = setup().await;
        let env = HashMap::new();
        let args = vec!["-w".to_string(), "1".to_string(), "100".to_string()];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        let result = Seq.execute(ctx).await.unwrap();
        let lines: Vec<&str> = result.stdout.lines().collect();
        assert_eq!(lines[0], "001");
        assert_eq!(lines[99], "100");
    }

    // ==================== error cases ====================

    #[tokio::test]
    async fn seq_missing_operand() {
        let (fs, mut cwd, mut variables) = setup().await;
        let env = HashMap::new();
        let args: Vec<String> = vec![];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        let result = Seq.execute(ctx).await.unwrap();
        assert_eq!(result.exit_code, 1);
        assert!(result.stderr.contains("missing operand"));
    }

    #[tokio::test]
    async fn seq_invalid_number() {
        let (fs, mut cwd, mut variables) = setup().await;
        let env = HashMap::new();
        let args = vec!["abc".to_string()];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        let result = Seq.execute(ctx).await.unwrap();
        assert_eq!(result.exit_code, 1);
        assert!(result.stderr.contains("invalid floating point"));
    }

    #[tokio::test]
    async fn seq_zero_increment() {
        let (fs, mut cwd, mut variables) = setup().await;
        let env = HashMap::new();
        let args = vec!["1".to_string(), "0".to_string(), "5".to_string()];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        let result = Seq.execute(ctx).await.unwrap();
        assert_eq!(result.exit_code, 1);
        assert!(result.stderr.contains("zero increment"));
    }

    // ==================== negative numbers ====================

    #[tokio::test]
    async fn seq_negative_range() {
        let (fs, mut cwd, mut variables) = setup().await;
        let env = HashMap::new();
        let args = vec!["-3".to_string(), "0".to_string()];
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        let result = Seq.execute(ctx).await.unwrap();
        assert_eq!(result.stdout, "-3\n-2\n-1\n0\n");
    }

    async fn run(args: &[&str]) -> ExecResult {
        let (fs, mut cwd, mut variables) = setup().await;
        let env = HashMap::new();
        let args: Vec<String> = args.iter().map(|s| s.to_string()).collect();
        let ctx = Context::new_for_test(&args, &env, &mut variables, &mut cwd, fs.clone(), None);
        Seq.execute(ctx).await.unwrap()
    }

    #[tokio::test]
    async fn seq_precision_follows_first_and_step() {
        assert_eq!(run(&["1", "0.5", "2"]).await.stdout, "1.0\n1.5\n2.0\n");
        assert_eq!(run(&["1", "2.50"]).await.stdout, "1\n2\n");
        assert_eq!(
            run(&["-w", "1", "-0.5", "-1"]).await.stdout,
            "01.0\n00.5\n00.0\n-0.5\n-1.0\n"
        );
    }

    #[tokio::test]
    async fn seq_format_and_separator_value() {
        assert_eq!(run(&["-f", "%03g", "9", "10"]).await.stdout, "009\n010\n");
        assert_eq!(run(&["-s", "-1", "1", "2"]).await.stdout, "1-12\n");
        assert_eq!(run(&["-f", "%e", "1"]).await.stdout, "1.000000e+00\n");
    }
}