rush-sh 0.8.0

A POSIX sh-compatible shell written in Rust
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
use crate::brace_expansion;
use crate::executor;
use crate::lexer;
use crate::parser;
use crate::state;
use std::collections::HashSet;
use std::sync::atomic::{AtomicBool, Ordering};

/// Check if a line contains a heredoc redirection using proper lexer-based detection
/// Returns the delimiter if found, None otherwise
pub fn line_contains_heredoc(line: &str, shell_state: &state::ShellState) -> Option<String> {
    // Use the lexer to properly parse the line
    match lexer::lex(line, shell_state) {
        Ok(tokens) => {
            // Look for a RedirHereDoc token
            for token in tokens {
                if let lexer::Token::RedirHereDoc(delimiter, _quoted) = token {
                    return Some(delimiter);
                }
            }
            None
        }
        Err(_) => None,
    }
}

/// Check if a line contains a specific keyword as a distinct token
/// This handles comments and ensures the keyword is not part of another word
pub fn contains_keyword(line: &str, keyword: &str) -> bool {
    let chars = line.chars().peekable();
    let mut in_single_quote = false;
    let mut in_double_quote = false;
    let mut escaped = false;
    let mut current_word = String::new();

    for ch in chars {
        if escaped {
            escaped = false;
            // Escaped characters are treated as part of the word
            current_word.push(ch);
            continue;
        }

        if in_single_quote {
            if ch == '\'' {
                in_single_quote = false;
            } else {
                current_word.push(ch);
            }
            continue;
        }

        if in_double_quote {
            if ch == '"' {
                in_double_quote = false;
            } else if ch == '\\' {
                escaped = true;
            } else {
                current_word.push(ch);
            }
            continue;
        }

        match ch {
            '#' => {
                if current_word.is_empty() {
                    return false; // Comment starts at word boundary
                }
                current_word.push(ch); // # inside word, treat as literal
            }
            '\'' => {
                in_single_quote = true;
                current_word.push(ch);
            }
            '"' => {
                in_double_quote = true;
                current_word.push(ch);
            }
            '\\' => escaped = true,
            ' ' | '\t' | '\n' | ';' | '|' | '&' | '(' | ')' | '{' | '}' => {
                if current_word == keyword {
                    return true;
                }
                current_word.clear();
            }
            _ => current_word.push(ch),
        }
    }

    // Check last word
    current_word == keyword
}

/// Determine whether the first token of a line equals the given keyword, ignoring leading spaces and tabs.
///
/// Returns `true` if the first token is equal to `keyword`, `false` otherwise.
///
/// # Examples
///
/// ```
/// use rush_sh::script_engine::starts_with_keyword;
/// assert!(starts_with_keyword("  if condition", "if"));
/// assert!(!starts_with_keyword("echo if", "if"));
/// ```
pub fn starts_with_keyword(line: &str, keyword: &str) -> bool {
    let mut chars = line.chars().peekable();
    let mut current_word = String::new();

    // Skip leading whitespace
    while let Some(&ch) = chars.peek() {
        if ch == ' ' || ch == '\t' {
            chars.next();
        } else {
            break;
        }
    }

    for ch in chars {
        match ch {
            ' ' | '\t' | '\n' | ';' | '|' | '&' | '(' | ')' | '{' | '}' => {
                return current_word == keyword;
            }
            _ => current_word.push(ch),
        }
    }

    current_word == keyword
}

/// Process and execute a single shell command line.
///
/// This performs lexical analysis, alias expansion, brace expansion, parsing, and execution
/// in sequence; prints errors (using the configured color scheme when enabled) and updates
/// the shell state (including the last exit code and, on certain lex errors, exit request).
///
/// # Parameters
///
/// - `line`: the input command line to process.
/// - `shell_state`: mutable shell state used for options (e.g., verbose, colors), color output,
///   and to store execution results such as the last exit code and exit-request flag.
///
/// # Examples
///
/// ```ignore
/// // Example usage (requires a configured ShellState):
/// let mut shell_state = state::ShellState::new();
/// execute_line("echo hello", &mut shell_state);
/// assert_eq!(shell_state.last_exit_code(), 0);
/// ```
pub fn execute_line(line: &str, shell_state: &mut state::ShellState) {
    // Print input line if verbose option (-v) is enabled
    if shell_state.options.verbose {
        if shell_state.colors_enabled {
            eprintln!("{}{}\x1b[0m", shell_state.color_scheme.builtin, line);
        } else {
            eprintln!("{}", line);
        }
    }

    match lexer::lex(line, shell_state) {
        Ok(tokens) => match lexer::expand_aliases(tokens, shell_state, &mut HashSet::new()) {
            Ok(expanded_tokens) => match brace_expansion::expand_braces(expanded_tokens) {
                Ok(brace_expanded_tokens) => match parser::parse(brace_expanded_tokens) {
                    Ok(ast) => {
                        let exit_code = executor::execute(ast, shell_state);
                        shell_state.set_last_exit_code(exit_code);
                    }
                    Err(e) => {
                        if shell_state.colors_enabled {
                            eprintln!(
                                "{}Parse error: {}\x1b[0m",
                                shell_state.color_scheme.error, e
                            );
                        } else {
                            eprintln!("Parse error: {}", e);
                        }
                        shell_state.set_last_exit_code(1);
                    }
                },
                Err(e) => {
                    if shell_state.colors_enabled {
                        eprintln!(
                            "{}Brace expansion error: {}\x1b[0m",
                            shell_state.color_scheme.error, e
                        );
                    } else {
                        eprintln!("Brace expansion error: {}", e);
                    }
                    shell_state.set_last_exit_code(1);
                }
            },
            Err(e) => {
                if shell_state.colors_enabled {
                    eprintln!(
                        "{}Alias expansion error: {}\x1b[0m",
                        shell_state.color_scheme.error, e
                    );
                } else {
                    eprintln!("Alias expansion error: {}", e);
                }
                shell_state.set_last_exit_code(1);
            }
        },
        Err(e) => {
            if shell_state.colors_enabled {
                eprintln!("{}Lex error: {}\x1b[0m", shell_state.color_scheme.error, e);
            } else {
                eprintln!("Lex error: {}", e);
            }
            shell_state.set_last_exit_code(1);

            // Check if this is a nounset error - if so, request shell exit
            if e.contains("unbound variable") {
                shell_state.exit_requested = true;
                shell_state.exit_code = 1;
            }
        }
    }
}

pub fn execute_script(
    content: &str,
    shell_state: &mut state::ShellState,
    shutdown_flag: Option<&AtomicBool>,
) {
    // Reset line number for script execution
    shell_state.current_line_number = 1;
    
    let mut current_block = String::new();
    let mut in_if_block = false;
    let mut if_depth = 0;
    let mut in_case_block = false;
    let mut in_function_block = false;
    let mut in_group_block = false;
    let mut brace_depth = 0;
    let mut in_for_block = false;
    let mut for_depth = 0;
    let mut in_while_block = false;
    let mut while_depth = 0;
    let mut in_until_block = false;
    let mut until_depth = 0;

    // Track quote state across lines to handle multiline strings correctly
    let mut in_double_quote = false;
    let mut in_single_quote = false;

    let lines: Vec<&str> = content.lines().collect();
    let mut i = 0;

    while i < lines.len() {
        let line = lines[i];
        
        // Update current line number for $LINENO - must be before any continue statements
        shell_state.current_line_number = i + 1;
        
        // Process pending signals at the start of each line
        state::process_pending_signals(shell_state);

        // Check for shutdown signal
        if let Some(flag) = shutdown_flag
            && flag.load(Ordering::Relaxed)
        {
            eprintln!("Script interrupted by SIGTERM");
            break;
        }

        // Check if exit was requested (e.g., from trap handler)
        if shell_state.exit_requested {
            break;
        }

        // Skip shebang lines
        if line.starts_with("#!") {
            i += 1;
            continue;
        }

        // Update quote state based on this line
        let chars = line.chars().peekable();
        let mut escaped = false;

        for ch in chars {
            if escaped {
                escaped = false;
                continue;
            }

            if in_single_quote {
                if ch == '\'' {
                    in_single_quote = false;
                }
                continue;
            }

            if in_double_quote {
                if ch == '"' {
                    in_double_quote = false;
                } else if ch == '\\' {
                    escaped = true;
                }
                continue;
            }

            match ch {
                '#' => break, // Comment starts
                '\'' => in_single_quote = true,
                '"' => in_double_quote = true,
                '\\' => escaped = true,
                _ => {}
            }
        }

        let trimmed = line.trim();
        if !in_double_quote && !in_single_quote && (trimmed.is_empty() || trimmed.starts_with("#"))
        {
            i += 1;
            continue;
        }

        let keywords_active = !in_double_quote && !in_single_quote;

        if keywords_active && !in_function_block {
            if starts_with_keyword(line, "if") {
                in_if_block = true;
                if_depth += 1;
            } else if starts_with_keyword(line, "case") {
                in_case_block = true;
            } else if starts_with_keyword(line, "for") {
                in_for_block = true;
                for_depth += 1;
            } else if starts_with_keyword(line, "while") {
                in_while_block = true;
                while_depth += 1;
            } else if starts_with_keyword(line, "until") {
                in_until_block = true;
                until_depth += 1;
            } else {
                let is_group_start = {
                    let trimmed = line.trim();
                    trimmed == "{" || trimmed.starts_with("{ ") || trimmed.starts_with("{\t")
                };
                if is_group_start {
                    in_group_block = true;
                    brace_depth += line.matches('{').count() as i32;
                    brace_depth -= line.matches('}').count() as i32;
                }
            }
        }

        if keywords_active
            && (line.contains("() {") || (trimmed.ends_with("()") && !in_function_block))
        {
            in_function_block = true;
            brace_depth += line.matches('{').count() as i32;
            brace_depth -= line.matches('}').count() as i32;
        } else if in_function_block || in_group_block {
            brace_depth += line.matches('{').count() as i32;
            brace_depth -= line.matches('}').count() as i32;
        }

        if !current_block.is_empty() {
            current_block.push('\n');
        }
        current_block.push_str(line);

        if keywords_active {
            if (in_function_block || in_group_block) && brace_depth == 0 {
                in_function_block = false;
                in_group_block = false;
                execute_line(&current_block, shell_state);
                current_block.clear();

                if shell_state.exit_requested {
                    break;
                }
            } else if in_if_block && contains_keyword(line, "fi") {
                if_depth -= 1;
                if if_depth == 0 {
                    in_if_block = false;
                    // Only execute if we're not inside a loop or other block
                    if !in_for_block
                        && !in_while_block
                        && !in_until_block
                        && !in_function_block
                        && !in_group_block
                        && !in_case_block
                    {
                        execute_line(&current_block, shell_state);
                        current_block.clear();

                        if shell_state.exit_requested {
                            break;
                        }
                    }
                }
            } else if in_for_block && contains_keyword(line, "done") {
                for_depth -= 1;
                if for_depth == 0 {
                    in_for_block = false;
                    execute_line(&current_block, shell_state);
                    current_block.clear();

                    if shell_state.exit_requested {
                        break;
                    }
                }
            } else if in_while_block && contains_keyword(line, "done") {
                while_depth -= 1;
                if while_depth == 0 {
                    in_while_block = false;
                    execute_line(&current_block, shell_state);
                    current_block.clear();

                    if shell_state.exit_requested {
                        break;
                    }
                }
            } else if in_until_block && contains_keyword(line, "done") {
                until_depth -= 1;
                if until_depth == 0 {
                    in_until_block = false;
                    execute_line(&current_block, shell_state);
                    current_block.clear();

                    if shell_state.exit_requested {
                        break;
                    }
                }
            } else if in_case_block && contains_keyword(line, "esac") {
                in_case_block = false;
                execute_line(&current_block, shell_state);
                current_block.clear();

                if shell_state.exit_requested {
                    break;
                }
            } else if !in_if_block
                && !in_case_block
                && !in_function_block
                && !in_group_block
                && !in_for_block
                && !in_while_block
                && !in_until_block
            {
                if let Some(delimiter) = line_contains_heredoc(&current_block, shell_state) {
                    i += 1;
                    let mut heredoc_content = String::new();
                    while i < lines.len() {
                        let content_line = lines[i];
                        if content_line.trim() == delimiter.trim() {
                            break;
                        }
                        if !heredoc_content.is_empty() {
                            heredoc_content.push('\n');
                        }
                        heredoc_content.push_str(content_line);
                        i += 1;
                    }
                    shell_state.pending_heredoc_content = Some(heredoc_content);
                    execute_line(&current_block, shell_state);
                    current_block.clear();
                } else if !in_single_quote
                    && !in_double_quote
                    && (line.ends_with(';') || !line.trim_end().ends_with('\\'))
                {
                    execute_line(&current_block, shell_state);
                    current_block.clear();
                }
            }
        }
        i += 1;
    }
}