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
//! The `echo` builtin — write arguments to stdout.
//!
//! Supports `-n` (no trailing newline), `-e` (interpret escapes),
//! and `-E` (do not interpret escapes, the default).
use crate::{Builtin, ShellEnvironment};
pub struct Echo;
impl Builtin for Echo {
fn name(&self) -> &str {
"echo"
}
fn execute(&self, args: &[&str], _env: &mut dyn ShellEnvironment) -> i32 {
let mut newline = true;
let mut interpret_escapes = false;
let mut text_start = 0;
// Parse option flags. `echo` stops parsing flags at the first
// argument that is not a recognized flag.
for (i, arg) in args.iter().enumerate() {
if !arg.starts_with('-') || arg.len() < 2 {
break;
}
let flag_bytes = &arg.as_bytes()[1..];
if flag_bytes.iter().all(|b| matches!(b, b'n' | b'e' | b'E')) {
for &b in flag_bytes {
match b {
b'n' => newline = false,
b'e' => interpret_escapes = true,
b'E' => interpret_escapes = false,
_ => unreachable!(),
}
}
text_start = i + 1;
} else {
break;
}
}
let text_args = &args[text_start..];
let joined = text_args.join(" ");
if interpret_escapes {
print!("{}", expand_escapes(&joined));
} else {
print!("{joined}");
}
if newline {
println!();
}
0
}
}
/// Expand C-style escape sequences.
fn expand_escapes(s: &str) -> String {
let mut out = String::with_capacity(s.len());
let mut chars = s.chars();
while let Some(c) = chars.next() {
if c == '\\' {
match chars.next() {
Some('n') => out.push('\n'),
Some('t') => out.push('\t'),
Some('r') => out.push('\r'),
Some('a') => out.push('\x07'),
Some('b') => out.push('\x08'),
Some('f') => out.push('\x0C'),
Some('v') => out.push('\x0B'),
Some('\\') => out.push('\\'),
Some('0') => {
// Octal: up to 3 digits after \0
let mut val: u8 = 0;
for _ in 0..3 {
if let Some(&d) = chars.as_str().as_bytes().first() {
if (b'0'..=b'7').contains(&d) {
val = val * 8 + (d - b'0');
chars.next();
} else {
break;
}
} else {
break;
}
}
out.push(val as char);
}
Some('x') => {
// Hex: up to 2 digits
let mut val: u8 = 0;
let mut count = 0;
while count < 2 {
if let Some(&d) = chars.as_str().as_bytes().first() {
if d.is_ascii_hexdigit() {
let digit = match d {
b'0'..=b'9' => d - b'0',
b'a'..=b'f' => d - b'a' + 10,
b'A'..=b'F' => d - b'A' + 10,
_ => unreachable!(),
};
val = val * 16 + digit;
chars.next();
count += 1;
} else {
break;
}
} else {
break;
}
}
out.push(val as char);
}
Some('c') => {
// \c suppresses further output (including trailing newline).
break;
}
Some(other) => {
out.push('\\');
out.push(other);
}
None => out.push('\\'),
}
} else {
out.push(c);
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn expand_newline() {
assert_eq!(expand_escapes("hello\\nworld"), "hello\nworld");
}
#[test]
fn expand_tab() {
assert_eq!(expand_escapes("a\\tb"), "a\tb");
}
#[test]
fn expand_octal() {
// \0101 = 'A' (octal 101 = 65)
assert_eq!(expand_escapes("\\0101"), "A");
}
#[test]
fn expand_hex() {
// \x41 = 'A'
assert_eq!(expand_escapes("\\x41"), "A");
}
#[test]
fn expand_c_stops_output() {
assert_eq!(expand_escapes("hello\\cworld"), "hello");
}
#[test]
fn unknown_escape_preserved() {
assert_eq!(expand_escapes("\\q"), "\\q");
}
}