bashrs 6.66.0

Rust-to-Shell transpiler for deterministic bootstrap scripts
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
fn generate_function_stmt(pad: &str, name: &str, body: &[BashStmt], indent: usize) -> String {
    let mut func = format!("{}{}() {{\n", pad, name);
    for stmt in body {
        func.push_str(&generate_stmt(stmt, indent + 1));
        func.push('\n');
    }
    func.push_str(pad);
    func.push('}');
    func
}

/// Generate a loop body with header and "done" terminator
fn generate_loop_body(header: &str, pad: &str, body: &[BashStmt], indent: usize) -> String {
    let mut s = format!("{}\n", header);
    for stmt in body {
        s.push_str(&generate_stmt(stmt, indent + 1));
        s.push('\n');
    }
    s.push_str(pad);
    s.push_str("done");
    s
}

/// Generate a pipeline
fn generate_pipeline(pad: &str, commands: &[BashStmt]) -> String {
    let mut pipeline = pad.to_string();
    for (i, cmd) in commands.iter().enumerate() {
        if i > 0 {
            pipeline.push_str(" | ");
        }
        pipeline.push_str(&generate_statement(cmd));
    }
    pipeline
}

/// Generate an if/elif/else statement
fn generate_if_stmt(
    pad: &str,
    condition: &BashExpr,
    then_block: &[BashStmt],
    elif_blocks: &[(BashExpr, Vec<BashStmt>)],
    else_block: &Option<Vec<BashStmt>>,
    indent: usize,
) -> String {
    let mut s = format!("{}if {}; then\n", pad, generate_condition(condition));
    for stmt in then_block {
        s.push_str(&generate_stmt(stmt, indent + 1));
        s.push('\n');
    }
    for (elif_cond, elif_body) in elif_blocks {
        s.push_str(&format!(
            "{}elif {}; then\n",
            pad,
            generate_condition(elif_cond)
        ));
        for stmt in elif_body {
            s.push_str(&generate_stmt(stmt, indent + 1));
            s.push('\n');
        }
    }
    if let Some(else_stmts) = else_block {
        s.push_str(&format!("{}else\n", pad));
        for stmt in else_stmts {
            s.push_str(&generate_stmt(stmt, indent + 1));
            s.push('\n');
        }
    }
    s.push_str(pad);
    s.push_str("fi");
    s
}

/// Generate a C-style for loop as POSIX while loop
fn generate_for_c_style(
    pad: &str,
    inner_pad: &str,
    init: &str,
    condition: &str,
    increment: &str,
    body: &[BashStmt],
    indent: usize,
) -> String {
    let mut s = String::new();
    if !init.is_empty() {
        s.push_str(pad);
        s.push_str(&convert_c_init_to_posix(init));
        s.push('\n');
    }
    let posix_condition = convert_c_condition_to_posix(condition);
    s.push_str(&format!("{}while {}; do\n", pad, posix_condition));
    for stmt in body {
        s.push_str(&generate_stmt(stmt, indent + 1));
        s.push('\n');
    }
    if !increment.is_empty() {
        s.push_str(inner_pad);
        s.push_str(&convert_c_increment_to_posix(increment));
        s.push('\n');
    }
    s.push_str(pad);
    s.push_str("done");
    s
}

/// Generate a case statement
fn generate_case_stmt(pad: &str, word: &BashExpr, arms: &[CaseArm], indent: usize) -> String {
    let arm_pad = "    ".repeat(indent + 1);
    let body_pad = "    ".repeat(indent + 2);
    let mut s = format!("{}case {} in\n", pad, generate_expr(word));
    for arm in arms {
        let pattern_str = arm.patterns.join("|");
        s.push_str(&format!("{}{})\n", arm_pad, pattern_str));
        for stmt in &arm.body {
            s.push_str(&generate_stmt(stmt, indent + 2));
            s.push('\n');
        }
        s.push_str(&format!("{};;\n", body_pad));
    }
    s.push_str(pad);
    s.push_str("esac");
    s
}

/// Generate a brace group or subshell
fn generate_brace_group(pad: &str, body: &[BashStmt], subshell: bool, indent: usize) -> String {
    if subshell {
        let mut s = format!("{}(\n", pad);
        for stmt in body {
            s.push_str(&generate_stmt(stmt, indent + 1));
            s.push('\n');
        }
        s.push_str(pad);
        s.push(')');
        s
    } else {
        let mut brace = format!("{}{{ ", pad);
        for (i, stmt) in body.iter().enumerate() {
            if i > 0 {
                brace.push_str("; ");
            }
            brace.push_str(&generate_statement(stmt));
        }
        brace.push_str("; }");
        brace
    }
}

/// Generate a coproc statement
fn generate_coproc(pad: &str, name: &Option<String>, body: &[BashStmt]) -> String {
    let mut coproc = format!("{}coproc ", pad);
    if let Some(n) = name {
        coproc.push_str(n);
        coproc.push(' ');
    }
    coproc.push_str("{ ");
    for (i, stmt) in body.iter().enumerate() {
        if i > 0 {
            coproc.push_str("; ");
        }
        coproc.push_str(&generate_statement(stmt));
    }
    coproc.push_str("; }");
    coproc
}

/// Negate a condition for until → while transformation
fn negate_condition(condition: &BashExpr) -> String {
    match condition {
        BashExpr::Test(test) => {
            // Wrap the test in negation
            format!("[ ! {} ]", generate_test_condition(test))
        }
        _ => {
            // For other expressions, use ! prefix
            format!("! {}", generate_condition(condition))
        }
    }
}

/// Generate the inner part of a test condition (without brackets)
fn generate_test_condition(expr: &TestExpr) -> String {
    match expr {
        TestExpr::StringEq(left, right) => {
            format!("{} = {}", generate_expr(left), generate_expr(right))
        }
        TestExpr::StringNe(left, right) => {
            format!("{} != {}", generate_expr(left), generate_expr(right))
        }
        TestExpr::IntEq(left, right) => {
            format!("{} -eq {}", generate_expr(left), generate_expr(right))
        }
        TestExpr::IntNe(left, right) => {
            format!("{} -ne {}", generate_expr(left), generate_expr(right))
        }
        TestExpr::IntLt(left, right) => {
            format!("{} -lt {}", generate_expr(left), generate_expr(right))
        }
        TestExpr::IntLe(left, right) => {
            format!("{} -le {}", generate_expr(left), generate_expr(right))
        }
        TestExpr::IntGt(left, right) => {
            format!("{} -gt {}", generate_expr(left), generate_expr(right))
        }
        TestExpr::IntGe(left, right) => {
            format!("{} -ge {}", generate_expr(left), generate_expr(right))
        }
        TestExpr::FileExists(path) => {
            format!("-e {}", generate_expr(path))
        }
        TestExpr::FileReadable(path) => {
            format!("-r {}", generate_expr(path))
        }
        TestExpr::FileWritable(path) => {
            format!("-w {}", generate_expr(path))
        }
        TestExpr::FileExecutable(path) => {
            format!("-x {}", generate_expr(path))
        }
        TestExpr::FileDirectory(path) => {
            format!("-d {}", generate_expr(path))
        }
        TestExpr::StringEmpty(expr) => {
            format!("-z {}", generate_expr(expr))
        }
        TestExpr::StringNonEmpty(expr) => {
            format!("-n {}", generate_expr(expr))
        }
        TestExpr::And(left, right) => {
            format!(
                "{} && {}",
                generate_test_expr(left),
                generate_test_expr(right)
            )
        }
        TestExpr::Or(left, right) => {
            format!(
                "{} || {}",
                generate_test_expr(left),
                generate_test_expr(right)
            )
        }
        TestExpr::Not(expr) => {
            format!("! {}", generate_test_expr(expr))
        }
    }
}

/// Generate a condition expression (for if/while statements)
fn generate_condition(expr: &BashExpr) -> String {
    match expr {
        BashExpr::Test(test) => generate_test_expr(test),
        _ => generate_expr(expr),
    }
}

/// Generate an expression
fn generate_expr(expr: &BashExpr) -> String {
    match expr {
        BashExpr::Literal(s) => generate_literal_expr(s),
        BashExpr::Variable(name) => format!("\"${}\"", name),
        BashExpr::Array(items) => items
            .iter()
            .map(generate_expr)
            .collect::<Vec<_>>()
            .join(" "),
        BashExpr::Arithmetic(arith) => format!("$(({}))", generate_arith_expr(arith)),
        BashExpr::Test(test) => generate_test_expr(test),
        BashExpr::CommandSubst(cmd) => format!("$({})", generate_statement(cmd)),
        BashExpr::Concat(exprs) => exprs.iter().map(generate_expr).collect::<String>(),
        BashExpr::Glob(pattern) => pattern.clone(),
        BashExpr::DefaultValue { variable, default } => {
            format_param_expansion(variable, ":-", default)
        }
        BashExpr::AssignDefault { variable, default } => {
            format_param_expansion(variable, ":=", default)
        }
        BashExpr::ErrorIfUnset { variable, message } => generate_error_if_unset(variable, message),
        BashExpr::AlternativeValue {
            variable,
            alternative,
        } => format_param_expansion(variable, ":+", alternative),
        BashExpr::StringLength { variable } => format!("\"${{#{}}}\"", variable),
        BashExpr::RemoveSuffix { variable, pattern } => {
            format_param_expansion(variable, "%", pattern)
        }
        BashExpr::RemovePrefix { variable, pattern } => {
            format_param_expansion(variable, "#", pattern)
        }
        BashExpr::RemoveLongestPrefix { variable, pattern } => {
            format_param_expansion(variable, "##", pattern)
        }
        BashExpr::RemoveLongestSuffix { variable, pattern } => {
            format_param_expansion(variable, "%%", pattern)
        }
        BashExpr::CommandCondition(cmd) => generate_statement(cmd),
    }
}

/// Generate a quoted literal expression with proper quoting strategy
fn generate_literal_expr(s: &str) -> String {
    let is_simple_word = !s.is_empty()
        && s.chars().all(|c| {
            c.is_alphanumeric() || c == '_' || c == '-' || c == '.' || c == '/' || c == '='
        });

    if is_simple_word && !is_shell_keyword(s) {
        return s.to_string();
    }
    if is_shell_keyword(s) {
        return format!("\"{}\"", s);
    }

    let needs_double_quotes = s.contains("$(") || s.contains("${") || s.contains('$');
    if needs_double_quotes {
        let escaped = s.replace('"', "\\\"");
        format!("\"{}\"", escaped)
    } else {
        let escaped = s.replace('\'', "'\\''");
        format!("'{}'", escaped)
    }
}

/// Format a parameter expansion like ${VAR:-default}, ${VAR%pattern}, etc.
fn format_param_expansion(variable: &str, operator: &str, operand: &BashExpr) -> String {
    let val = generate_expr(operand);
    let unquoted = strip_quotes(&val);
    format!("\"${{{}{}{}}}\"", variable, operator, unquoted)
}

/// Generate ${VAR:?message} with special quote handling
fn generate_error_if_unset(variable: &str, message: &BashExpr) -> String {
    let msg_val = generate_expr(message);
    let msg_for_expansion = if msg_val.starts_with('"') && msg_val.ends_with('"') {
        msg_val.trim_start_matches('"').trim_end_matches('"')
    } else {
        &msg_val
    };
    format!("\"${{{}:?{}}}\"", variable, msg_for_expansion)
}

/// Strip surrounding quotes (both single and double) from a string
fn strip_quotes(s: &str) -> &str {
    s.trim_matches(|c| c == '"' || c == '\'')
}

/// Check if a string is a POSIX/bash shell keyword that needs quoting in argument context.
/// These keywords can confuse POSIX sh parsers when unquoted (shellcheck SC1010).
fn is_shell_keyword(s: &str) -> bool {
    matches!(
        s,
        "if" | "then"
            | "elif"
            | "else"
            | "fi"
            | "for"
            | "while"
            | "until"
            | "do"
            | "done"
            | "case"
            | "esac"
            | "in"
            | "function"
            | "select"
            | "coproc"
    )
}

/// Convert `declare`/`typeset` to POSIX equivalents.
/// - `declare -i var=val` → `var=val` (integer attribute is a hint, not POSIX)
/// - `declare -r var=val` → `readonly var=val`
/// - `declare -x var=val` → `export var=val`
/// - `declare -a var` → comment (arrays are not POSIX)
/// - `declare -A var` → comment (assoc arrays are not POSIX)
/// - `declare var=val` → `var=val` (plain declare → plain assignment)
fn generate_declare_posix(args: &[BashExpr], redirects: &[Redirect]) -> String {
    let mut flags = Vec::new();
    let mut assignments = Vec::new();

    for arg in args {
        match arg {
            BashExpr::Literal(s) if s.starts_with('-') => {
                flags.push(s.as_str());
            }
            _ => {
                assignments.push(generate_expr(arg));
            }
        }
    }

    let has_readonly = flags.iter().any(|f| f.contains('r'));
    let has_export = flags.iter().any(|f| f.contains('x'));
    let has_array = flags.iter().any(|f| f.contains('a'));
    let has_assoc = flags.iter().any(|f| f.contains('A'));

    // Arrays and associative arrays have no POSIX equivalent
    if has_array || has_assoc {
        let flag_str = flags.join(" ");
        let assign_str = assignments.join(" ");
        if assignments.is_empty() || !assign_str.contains('=') {
            return format!("# declare {} {} (not POSIX)", flag_str, assign_str)
                .trim_end()
                .to_string();
        }
        // Array with assignment: declare -a arr=(items) — emit comment
        return format!("# declare {} {} (not POSIX)", flag_str, assign_str)
            .trim_end()
            .to_string();
    }

    let mut output = String::new();

    // Build the POSIX command prefix
    if has_readonly && has_export {
        output.push_str("export ");
        // Note: readonly + export in a single declare; emit export first, readonly after
        let assign_str = assignments.join(" ");
        output.push_str(&assign_str);
        // Append redirects
        for redirect in redirects {
            output.push(' ');
            output.push_str(&generate_redirect(redirect));
        }
        // Add a second line for readonly
        output.push('\n');
        output.push_str("readonly ");
        output.push_str(&assign_str);
    } else if has_readonly {
        output.push_str("readonly ");
        output.push_str(&assignments.join(" "));
    } else if has_export {
        output.push_str("export ");
        output.push_str(&assignments.join(" "));
    } else {
        // Plain declare or declare -i/-l/-u → just emit the assignment
        output.push_str(&assignments.join(" "));
    }

    // Append redirects
    for redirect in redirects {
        output.push(' ');
        output.push_str(&generate_redirect(redirect));
    }

    output
}

include!("codegen_generate_arith.rs");