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
// Copyright (c) 2025 xiefujin <490021684@qq.com>
// Licensed under Apache-2.0, see LICENSE file for full license terms.
use crate::shell::{CommandOutput, Shell};
impl Shell {
pub fn cmd_unset(&mut self, args: &[&str]) -> CommandOutput {
if args.is_empty() {
return CommandOutput::error("unset: missing argument\n".to_string(), 1);
}
let mut i = 0;
while i < args.len() {
match args[i] {
"-f" => {
i += 1;
if i < args.len() {
let name = args[i];
if self.functions.remove(name).is_none() {
return CommandOutput::error(
format!("unset: {}: not a function\n", name),
1,
);
}
} else {
return CommandOutput::error(
"unset: -f requires a function name\n".to_string(),
1,
);
}
}
"-v" => {
i += 1;
if i < args.len() {
let name = args[i];
self.vars.remove(name);
self.exported.remove(name);
} else {
return CommandOutput::error(
"unset: -v requires a variable name\n".to_string(),
1,
);
}
}
name => {
if self.vars.remove(name).is_some() {
self.exported.remove(name);
} else if self.functions.remove(name).is_some() {
} else {
return CommandOutput::error(
format!("unset: {}: not found\n", name),
1,
);
}
}
}
i += 1;
}
CommandOutput::success(String::new())
}
pub fn cmd_declare(&mut self, args: &[&str]) -> CommandOutput {
if args.is_empty() {
return CommandOutput::success(String::new());
}
match args[0] {
"-f" => {
let mut out = String::new();
let mut keys: Vec<&String> = self.functions.keys().collect();
keys.sort();
for k in keys {
let v = &self.functions[k];
out.push_str(&format!("{}() {{\n {}\n}}\n", k, v));
}
CommandOutput::success(out)
}
"-p" => {
let mut out = String::new();
let mut keys: Vec<&String> = self.vars.keys().collect();
keys.sort();
for k in keys {
let v = &self.vars[k];
out.push_str(&format!("declare -- {}='{}'\n", k, v));
}
CommandOutput::success(out)
}
_ => CommandOutput::error(
format!("declare: unknown option {}\n", args[0]),
1,
),
}
}
}