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
//! echo builtin command
use async_trait::async_trait;
use super::{Builtin, Context};
use crate::error::Result;
use crate::interpreter::ExecResult;
/// The echo builtin command.
pub struct Echo;
#[async_trait]
impl Builtin for Echo {
async fn execute(&self, ctx: Context<'_>) -> Result<ExecResult> {
let mut output = String::new();
let mut add_newline = true;
let mut interpret_escapes = false;
let mut args_iter = ctx.args.iter().peekable();
// Parse options - support combined flags like -en, -ne, -neE
while let Some(arg) = args_iter.peek() {
let arg_str = arg.as_str();
if arg_str.starts_with('-') && arg_str.len() > 1 && !arg_str.starts_with("--") {
let mut is_valid_option = true;
// Check if all characters after '-' are valid options
for c in arg_str[1..].chars() {
if !matches!(c, 'n' | 'e' | 'E') {
is_valid_option = false;
break;
}
}
if is_valid_option {
// Process each flag character
for c in arg_str[1..].chars() {
match c {
'n' => add_newline = false,
'e' => interpret_escapes = true,
'E' => interpret_escapes = false,
_ => {}
}
}
args_iter.next();
} else {
break;
}
} else {
break;
}
}
// Collect remaining arguments
let remaining: Vec<&String> = args_iter.collect();
for (i, arg) in remaining.iter().enumerate() {
if i > 0 {
output.push(' ');
}
if interpret_escapes {
output.push_str(&interpret_escape_sequences(arg));
} else {
output.push_str(arg);
}
}
if add_newline {
output.push('\n');
}
Ok(ExecResult::ok(output))
}
}
fn interpret_escape_sequences(s: &str) -> String {
let mut result = String::new();
let mut chars = s.chars().peekable();
while let Some(ch) = chars.next() {
if ch == '\\' {
match chars.next() {
Some('n') => result.push('\n'),
Some('t') => result.push('\t'),
Some('r') => result.push('\r'),
Some('\\') => result.push('\\'),
Some('a') => result.push('\x07'), // bell
Some('b') => result.push('\x08'), // backspace
Some('f') => result.push('\x0c'), // form feed
Some('v') => result.push('\x0b'), // vertical tab
Some('0') => {
// Octal escape \0nnn
let mut value = 0u8;
for _ in 0..3 {
if let Some(&digit) = chars.peek() {
if ('0'..='7').contains(&digit) {
value = value * 8 + (digit as u8 - b'0');
chars.next();
} else {
break;
}
}
}
result.push(value as char);
}
Some('x') => {
// Hex escape \xHH
let mut value = 0u8;
for _ in 0..2 {
if let Some(&digit) = chars.peek() {
if digit.is_ascii_hexdigit() {
value = value * 16
+ digit.to_digit(16).expect(
"to_digit(16) valid: guarded by is_ascii_hexdigit()",
) as u8;
chars.next();
} else {
break;
}
}
}
result.push(value as char);
}
Some('c') => {
// Stop output
break;
}
Some(other) => {
result.push('\\');
result.push(other);
}
None => result.push('\\'),
}
} else {
result.push(ch);
}
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_escape_sequences() {
assert_eq!(interpret_escape_sequences("hello\\nworld"), "hello\nworld");
assert_eq!(interpret_escape_sequences("tab\\there"), "tab\there");
assert_eq!(interpret_escape_sequences("\\\\backslash"), "\\backslash");
}
}