bashkit 0.8.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
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
714
715
716
717
718
719
720
721
722
723
//! awk - Pattern scanning and processing builtin
//!
//! Implements basic AWK functionality.
//!
//! Usage:
//!   awk '{print $1}' file
//!   awk -F: '{print $1}' /etc/passwd
//!   echo "a b c" | awk '{print $2}'
//!   awk 'BEGIN{print "start"} {print} END{print "end"}' file
//!   awk '/pattern/{print}' file
//!   awk 'NR==2{print}' file

// Parser invariant: `pos` is always a byte offset on a UTF-8 char boundary.
// Move across user-controlled text with `advance()`/`consume_while()`, and use
// raw `pos += N` only for known ASCII tokens and delimiters.
#![allow(clippy::unwrap_used)]

use async_trait::async_trait;
use regex::Regex;
use std::collections::HashMap;

use super::{Builtin, Context, read_text_file};
use crate::error::{Error, Result};
use crate::interpreter::ExecResult;
use crate::limits::ExecutionLimits;

/// awk command - pattern scanning and processing
pub struct Awk;

#[derive(Debug)]
struct AwkProgram {
    begin_actions: Vec<AwkAction>,
    main_rules: Vec<AwkRule>,
    end_actions: Vec<AwkAction>,
    functions: HashMap<String, AwkFunctionDef>,
}

#[derive(Debug, Clone)]
struct AwkFunctionDef {
    params: Vec<String>,
    body: Vec<AwkAction>,
}

#[derive(Debug)]
struct AwkRule {
    pattern: Option<AwkPattern>,
    actions: Vec<AwkAction>,
}

#[derive(Debug)]
enum AwkPattern {
    Regex(Regex),
    Expression(AwkExpr),
    /// Range pattern: /start/,/end/ — matches from start to end inclusive.
    /// Each sub-pattern can be a Regex or Expression.
    Range(Box<AwkPattern>, Box<AwkPattern>),
}

#[derive(Debug, Clone)]
enum AwkExpr {
    Number(f64),
    String(String),
    Field(Box<AwkExpr>), // $n
    Variable(String),    // var
    BinOp(Box<AwkExpr>, String, Box<AwkExpr>),
    UnaryOp(String, Box<AwkExpr>),
    Assign(String, Box<AwkExpr>),
    ArrayAssign(String, Box<AwkExpr>, Box<AwkExpr>), // arr[key] = val
    CompoundArrayAssign(String, Box<AwkExpr>, String, Box<AwkExpr>), // arr[key] += val
    Concat(Vec<AwkExpr>),
    FuncCall(String, Vec<AwkExpr>),
    Regex(String),
    #[allow(dead_code)] // matched in eval but construction deferred to pattern expansion
    Match(Box<AwkExpr>, String), // expr ~ /pattern/
    PostIncrement(String),                   // var++
    PostDecrement(String),                   // var--
    PreIncrement(String),                    // ++var
    PreDecrement(String),                    // --var
    InArray(Box<AwkExpr>, String),           // key in arr
    FieldAssign(Box<AwkExpr>, Box<AwkExpr>), // $n = val
    /// getline [var] < file as expression — returns 1 on success, 0 on EOF, -1 on error
    GetlineFile {
        var: Option<String>,
        file: Box<AwkExpr>,
    },
}

/// Output target for print/printf redirection (e.g., `> file`, `>> file`).
/// Pipe (`| cmd`) is not supported and returns a clear error.
#[derive(Debug, Clone)]
enum AwkOutputTarget {
    /// Truncate/create file: `> file`
    Truncate(AwkExpr),
    /// Append to file: `>> file`
    Append(AwkExpr),
}

#[derive(Debug, Clone)]
enum AwkAction {
    Print(Vec<AwkExpr>, Option<AwkOutputTarget>),
    Printf(AwkExpr, Vec<AwkExpr>, Option<AwkOutputTarget>),
    Assign(String, AwkExpr),
    ArrayAssign(String, AwkExpr, AwkExpr), // arr[key] = val
    If(AwkExpr, Vec<AwkAction>, Vec<AwkAction>),
    While(AwkExpr, Vec<AwkAction>),
    DoWhile(AwkExpr, Vec<AwkAction>),
    For(Box<AwkAction>, AwkExpr, Box<AwkAction>, Vec<AwkAction>),
    ForIn(String, String, Vec<AwkAction>), // for (key in arr) { body }
    Next,
    Break,
    Continue,
    Delete(String, AwkExpr), // delete arr[key]
    Getline,                 // getline — read next input record into $0
    /// getline [var] < file — read next line from file
    GetlineFile {
        var: Option<String>,
        file: AwkExpr,
    },
    Exit(Option<AwkExpr>),
    Return(Option<AwkExpr>),
    Expression(AwkExpr),
}

struct AwkState {
    variables: HashMap<String, AwkValue>,
    fields: Vec<String>,
    fs: String,
    ofs: String,
    ors: String,
    nr: usize,
    nf: usize,
    fnr: usize,
    /// When true, fields are split per RFC 4180 CSV rules (--csv flag)
    csv_mode: bool,
}

#[derive(Debug, Clone, PartialEq)]
enum AwkValue {
    Number(f64),
    String(String),
    Uninitialized,
}

/// Format number using AWK's OFMT (%.6g): 6 significant digits, trim trailing zeros.
fn format_awk_number(n: f64) -> String {
    if n.is_nan() {
        return "nan".to_string();
    }
    if n.is_infinite() {
        return if n > 0.0 { "inf" } else { "-inf" }.to_string();
    }
    // Integers: no decimal point
    if n.fract() == 0.0 && n.abs() < 1e16 {
        return format!("{}", n as i64);
    }
    // %.6g: use 6 significant digits
    let abs = n.abs();
    let exp = abs.log10().floor() as i32;
    if !(-4..6).contains(&exp) {
        // Scientific notation: 5 decimal places = 6 sig digits
        let mut s = format!("{:.*e}", 5, n);
        // Trim trailing zeros in mantissa
        if let Some(e_pos) = s.find('e') {
            let (mantissa, exp_part) = s.split_at(e_pos);
            let trimmed = mantissa.trim_end_matches('0').trim_end_matches('.');
            s = format!("{}{}", trimmed, exp_part);
        }
        // Normalize exponent format: e1 -> e+01 etc. to match C printf
        // Actually AWK uses e+06 style. Rust uses e6. Fix:
        if let Some(e_pos) = s.find('e') {
            let exp_str = &s[e_pos + 1..];
            let exp_val: i32 = exp_str.parse().unwrap_or(0);
            let mantissa = &s[..e_pos];
            s = format!("{}e{:+03}", mantissa, exp_val);
        }
        s
    } else {
        // Fixed notation
        let decimal_places = (5 - exp).max(0) as usize;
        let mut s = format!("{:.*}", decimal_places, n);
        if s.contains('.') {
            s = s.trim_end_matches('0').trim_end_matches('.').to_string();
        }
        s
    }
}

impl AwkValue {
    fn as_number(&self) -> f64 {
        match self {
            AwkValue::Number(n) => *n,
            AwkValue::String(s) => s.parse().unwrap_or(0.0),
            AwkValue::Uninitialized => 0.0,
        }
    }

    fn as_string(&self) -> String {
        match self {
            AwkValue::Number(n) => format_awk_number(*n),
            AwkValue::String(s) => s.clone(),
            AwkValue::Uninitialized => String::new(),
        }
    }

    fn as_bool(&self) -> bool {
        match self {
            AwkValue::Number(n) => *n != 0.0,
            AwkValue::String(s) => {
                if s.is_empty() {
                    return false;
                }
                // In awk, numeric strings evaluate as numbers in boolean context
                if let Ok(n) = s.parse::<f64>() {
                    n != 0.0
                } else {
                    true
                }
            }
            AwkValue::Uninitialized => false,
        }
    }
}

impl Default for AwkState {
    fn default() -> Self {
        let mut variables = HashMap::new();
        // POSIX SUBSEP: subscript separator for multi-dimensional arrays
        variables.insert("SUBSEP".to_string(), AwkValue::String("\x1c".to_string()));
        Self {
            variables,
            fields: Vec::new(),
            fs: " ".to_string(),
            ofs: " ".to_string(),
            ors: "\n".to_string(),
            nr: 0,
            nf: 0,
            fnr: 0,
            csv_mode: false,
        }
    }
}

/// Parse a CSV line per RFC 4180: handle quoted fields, embedded commas,
/// and double-quote escaping.
fn csv_split_fields(line: &str) -> Vec<String> {
    let mut fields = Vec::new();
    let mut field = String::new();
    let mut in_quotes = false;
    let mut chars = line.chars().peekable();

    while let Some(c) = chars.next() {
        if in_quotes {
            if c == '"' {
                if chars.peek() == Some(&'"') {
                    // Escaped quote
                    field.push('"');
                    chars.next();
                } else {
                    in_quotes = false;
                }
            } else {
                field.push(c);
            }
        } else if c == '"' {
            in_quotes = true;
        } else if c == ',' {
            fields.push(std::mem::take(&mut field));
        } else {
            field.push(c);
        }
    }
    fields.push(field);
    fields
}

impl AwkState {
    /// Split a line into fields based on current mode (CSV or FS)
    fn split_fields(&self, line: &str) -> Vec<String> {
        if self.csv_mode {
            csv_split_fields(line)
        } else if self.fs == " " {
            line.split_whitespace().map(String::from).collect()
        } else {
            line.split(&self.fs).map(String::from).collect()
        }
    }

    fn set_line(&mut self, line: &str) {
        self.nr += 1;
        self.fnr += 1;

        // Split by field separator
        self.fields = self.split_fields(line);

        self.nf = self.fields.len();

        // Set built-in variables
        self.variables
            .insert("NR".to_string(), AwkValue::Number(self.nr as f64));
        self.variables
            .insert("NF".to_string(), AwkValue::Number(self.nf as f64));
        self.variables
            .insert("FNR".to_string(), AwkValue::Number(self.fnr as f64));
        self.variables
            .insert("$0".to_string(), AwkValue::String(line.to_string()));
    }

    fn get_field(&self, n: usize) -> AwkValue {
        if n == 0 {
            // $0 is the whole line
            self.variables
                .get("$0")
                .cloned()
                .unwrap_or(AwkValue::Uninitialized)
        } else if n <= self.fields.len() {
            AwkValue::String(self.fields[n - 1].clone())
        } else {
            AwkValue::Uninitialized
        }
    }

    fn get_variable(&self, name: &str) -> AwkValue {
        match name {
            "NR" => AwkValue::Number(self.nr as f64),
            "NF" => AwkValue::Number(self.nf as f64),
            "FNR" => AwkValue::Number(self.fnr as f64),
            "FS" => AwkValue::String(self.fs.clone()),
            "OFS" => AwkValue::String(self.ofs.clone()),
            "ORS" => AwkValue::String(self.ors.clone()),
            _ => self
                .variables
                .get(name)
                .cloned()
                .unwrap_or(AwkValue::Uninitialized),
        }
    }

    fn set_variable(&mut self, name: &str, value: AwkValue) {
        match name {
            "FS" => self.fs = value.as_string(),
            "OFS" => self.ofs = value.as_string(),
            "ORS" => self.ors = value.as_string(),
            "$0" => {
                let s = value.as_string();
                // Re-split fields when $0 is modified
                self.fields = self.split_fields(&s);
                self.nf = self.fields.len();
                self.variables
                    .insert("NF".to_string(), AwkValue::Number(self.nf as f64));
                self.variables.insert(name.to_string(), value);
            }
            _ => {
                self.variables.insert(name.to_string(), value);
            }
        }
    }
}

// THREAT[TM-DOS-027]: parser-depth limit lives in
// `super::limits::AWK_MAX_PARSER_DEPTH` (guards against deeply nested
// expressions).

/// Preprocess awk program: replace newlines with semicolons inside action blocks.
/// This makes newlines act as statement separators per POSIX awk spec.
/// Respects string literals, regex literals, and nested braces.
fn normalize_awk_newlines(input: &str) -> String {
    let mut result = String::with_capacity(input.len());
    let chars: Vec<char> = input.chars().collect();
    let mut i = 0;
    let mut brace_depth = 0;

    while i < chars.len() {
        match chars[i] {
            '{' => {
                brace_depth += 1;
                result.push('{');
                i += 1;
            }
            '}' => {
                if brace_depth > 0 {
                    brace_depth -= 1;
                }
                result.push('}');
                i += 1;
            }
            '"' => {
                // String literal — pass through unchanged
                result.push('"');
                i += 1;
                while i < chars.len() && chars[i] != '"' {
                    if chars[i] == '\\' && i + 1 < chars.len() {
                        result.push(chars[i]);
                        i += 1;
                    }
                    result.push(chars[i]);
                    i += 1;
                }
                if i < chars.len() {
                    result.push(chars[i]); // closing "
                    i += 1;
                }
            }
            '/' => {
                // Regex literal — pass through unchanged (both pattern and expression context)
                result.push('/');
                i += 1;
                while i < chars.len() && chars[i] != '/' {
                    if chars[i] == '\\' && i + 1 < chars.len() {
                        result.push(chars[i]);
                        i += 1;
                    }
                    result.push(chars[i]);
                    i += 1;
                }
                if i < chars.len() {
                    result.push(chars[i]); // closing /
                    i += 1;
                }
            }
            '#' => {
                // Comment — skip to end of line, replace with newline/semicolon
                while i < chars.len() && chars[i] != '\n' {
                    i += 1;
                }
                if i < chars.len() {
                    if brace_depth > 0 {
                        result.push(';');
                    } else {
                        result.push('\n');
                    }
                    i += 1;
                }
            }
            '\\' if i + 1 < chars.len() && chars[i + 1] == '\n' => {
                // Backslash-newline: line continuation — join lines
                i += 2;
            }
            '\n' if brace_depth > 0 => {
                // Inside action block: replace newline with semicolon
                result.push(';');
                i += 1;
            }
            _ => {
                result.push(chars[i]);
                i += 1;
            }
        }
    }
    result
}

mod interpreter;
mod parser;
use interpreter::{AwkFlow, AwkInterpreter};
use parser::AwkParser;

impl Awk {
    /// Process C-style escape sequences in a string (e.g., \t → tab, \n → newline)
    fn process_escape_sequences(s: &str) -> String {
        let mut result = String::new();
        let mut chars = s.chars();
        while let Some(c) = chars.next() {
            if c == '\\' {
                match chars.next() {
                    Some('t') => result.push('\t'),
                    Some('n') => result.push('\n'),
                    Some('r') => result.push('\r'),
                    Some('\\') => result.push('\\'),
                    Some('a') => result.push('\x07'),
                    Some('b') => result.push('\x08'),
                    Some('f') => result.push('\x0C'),
                    Some(other) => {
                        result.push('\\');
                        result.push(other);
                    }
                    None => result.push('\\'),
                }
            } else {
                result.push(c);
            }
        }
        result
    }
}

#[async_trait]
impl Builtin for Awk {
    async fn execute(&self, ctx: Context<'_>) -> Result<ExecResult> {
        if let Some(r) = super::check_help_version(
            ctx.args,
            "Usage: awk [OPTION]... 'program' [FILE]...\nPattern scanning and processing language.\n\n  -F SEP\t\tuse SEP as field separator\n  -v var=val\tassign variable before execution\n  -f progfile\tread program from file\n  --csv, -k\tCSV mode (set field separator to comma)\n  --help\tdisplay this help and exit\n  --version\toutput version information and exit\n",
            Some("awk (bashkit) 0.1"),
        ) {
            return Ok(r);
        }
        let mut program_str = String::new();
        let mut files: Vec<String> = Vec::new();
        let mut field_sep = " ".to_string();
        let mut pre_vars: Vec<(String, String)> = Vec::new();
        let mut csv_mode = false;
        let mut i = 0;

        while i < ctx.args.len() {
            let arg = &ctx.args[i];
            if arg == "--csv" || arg == "-k" {
                csv_mode = true;
                field_sep = ",".to_string();
            } else if arg == "-F" {
                i += 1;
                if i < ctx.args.len() {
                    field_sep = ctx.args[i].clone();
                }
            } else if let Some(sep) = arg.strip_prefix("-F") {
                field_sep = sep.to_string();
            } else if arg == "-v" {
                // Variable assignment: -v var=value
                i += 1;
                if i < ctx.args.len()
                    && let Some(eq_pos) = ctx.args[i].find('=')
                {
                    let name = ctx.args[i][..eq_pos].to_string();
                    let mut value = ctx.args[i][eq_pos + 1..].to_string();
                    // Strip surrounding quotes if present (shell may pass them)
                    if (value.starts_with('"') && value.ends_with('"'))
                        || (value.starts_with('\'') && value.ends_with('\''))
                    {
                        value = value[1..value.len() - 1].to_string();
                    }
                    pre_vars.push((name, value));
                }
            } else if arg == "-f" {
                // Read program from file
                i += 1;
                if i < ctx.args.len() {
                    let path = if ctx.args[i].starts_with('/') {
                        std::path::PathBuf::from(&ctx.args[i])
                    } else {
                        ctx.cwd.join(&ctx.args[i])
                    };
                    program_str = match read_text_file(&*ctx.fs, &path, "awk").await {
                        Ok(t) => t,
                        Err(e) => return Ok(e),
                    };
                }
            } else if arg.starts_with('-') {
                // Unknown option - ignore
            } else if program_str.is_empty() {
                program_str = arg.clone();
            } else {
                files.push(arg.clone());
            }
            i += 1;
        }

        if program_str.is_empty() {
            return Err(Error::Execution("awk: no program given".to_string()));
        }

        let program_str = normalize_awk_newlines(&program_str);
        let mut parser = AwkParser::new(&program_str);
        let program = parser.parse()?;

        let mut interp = AwkInterpreter::new();
        interp.max_loop_iterations = ctx
            .execution_extension::<ExecutionLimits>()
            .map(|limits| limits.max_loop_iterations)
            .unwrap_or_else(|| ExecutionLimits::default().max_loop_iterations);
        interp.functions = program.functions.clone();
        interp.state.fs = Self::process_escape_sequences(&field_sep);
        interp.fs = Some(ctx.fs.clone());
        interp.cwd = ctx.cwd.clone();
        if csv_mode {
            interp.state.csv_mode = true;
            interp.state.ofs = ",".to_string();
        }

        // Set pre-assigned variables (-v)
        for (name, value) in &pre_vars {
            let awk_val = if let Ok(n) = value.parse::<f64>() {
                AwkValue::Number(n)
            } else {
                AwkValue::String(value.clone())
            };
            interp.state.set_variable(name, awk_val);
        }

        // Run BEGIN actions
        let mut exit_code: Option<i32> = None;
        for action in &program.begin_actions {
            if let AwkFlow::Exit(code) = interp.exec_action(action) {
                exit_code = code;
                // Run END actions even after exit
                for end_action in &program.end_actions {
                    if let AwkFlow::Exit(_) = interp.exec_action(end_action) {
                        break;
                    }
                }
                Self::flush_file_outputs(&interp, &ctx).await?;
                let mut result = ExecResult::with_code(interp.output, exit_code.unwrap_or(0));
                result.stderr = interp.stderr_output;
                return Ok(result);
            }
        }

        // Process input
        let inputs: Vec<String> = if files.is_empty() {
            vec![ctx.stdin.unwrap_or("").to_string()]
        } else {
            let mut inputs = Vec::new();
            for file in &files {
                let path = if file.starts_with('/') {
                    std::path::PathBuf::from(file)
                } else {
                    ctx.cwd.join(file)
                };

                let text = match read_text_file(&*ctx.fs, &path, "awk").await {
                    Ok(t) => t,
                    Err(e) => return Ok(e),
                };
                inputs.push(text);
            }
            inputs
        };

        'files: for (file_idx, input) in inputs.iter().enumerate() {
            interp.state.fnr = 0;
            // Set FILENAME to current file path, or empty for stdin
            if !files.is_empty() {
                interp.state.variables.insert(
                    "FILENAME".to_string(),
                    AwkValue::String(files[file_idx].clone()),
                );
            } else {
                interp
                    .state
                    .variables
                    .insert("FILENAME".to_string(), AwkValue::String(String::new()));
            }
            // Index-based iteration so getline can advance the index
            interp.input_lines = input.lines().map(|l| l.to_string()).collect();
            interp.line_index = 0;

            while interp.line_index < interp.input_lines.len() {
                let line = interp.input_lines[interp.line_index].clone();
                interp.state.set_line(&line);

                for (rule_idx, rule) in program.main_rules.iter().enumerate() {
                    // Check pattern (with range state tracking)
                    let matches = match &rule.pattern {
                        Some(pattern) => interp.matches_pattern_with_index(pattern, rule_idx),
                        None => true,
                    };

                    if matches {
                        let mut next_record = false;
                        for action in &rule.actions {
                            match interp.exec_action(action) {
                                AwkFlow::Continue => {}
                                AwkFlow::Next => {
                                    next_record = true;
                                    break;
                                }
                                AwkFlow::Exit(code) => {
                                    exit_code = code;
                                    break 'files;
                                }
                                _ => {}
                            }
                        }
                        if next_record {
                            break;
                        }
                    }
                }
                interp.line_index += 1;
            }
        }

        // Run END actions (awk runs END even after exit in main body)
        for action in &program.end_actions {
            if let AwkFlow::Exit(code) = interp.exec_action(action) {
                if exit_code.is_none() {
                    exit_code = code;
                }
                break;
            }
        }

        Self::flush_file_outputs(&interp, &ctx).await?;
        let mut result = ExecResult::with_code(interp.output, exit_code.unwrap_or(0));
        result.stderr = interp.stderr_output;
        Ok(result)
    }
}

impl Awk {
    /// Flush buffered file outputs to VFS.
    async fn flush_file_outputs(interp: &AwkInterpreter, ctx: &Context<'_>) -> Result<()> {
        // Truncate mode: write entire buffer as file content
        for (path, content) in &interp.file_outputs {
            let path = if path.starts_with('/') {
                std::path::PathBuf::from(path)
            } else {
                ctx.cwd.join(path)
            };
            ctx.fs.write_file(&path, content.as_bytes()).await?;
        }
        // Append mode: append buffer to existing file (or create)
        for (path, content) in &interp.file_appends {
            let path = if path.starts_with('/') {
                std::path::PathBuf::from(path)
            } else {
                ctx.cwd.join(path)
            };
            ctx.fs.append_file(&path, content.as_bytes()).await?;
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests;