Skip to content

Commit c543b60

Browse files
committed
Implement unset
1 parent 8288058 commit c543b60

File tree

4 files changed

+57
-14
lines changed

4 files changed

+57
-14
lines changed

src/core/builtins/unset.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,34 @@
33

44
use crate::ShellCore;
55

6-
fn unset_var(core: &mut ShellCore, name: &str) -> i32 {
6+
fn unset_all(core: &mut ShellCore, name: &str) -> i32 {
77
core.data.unset(name);
88
0
99
}
1010

11+
fn unset_var(core: &mut ShellCore, name: &str) -> i32 {
12+
core.data.unset_var(name);
13+
0
14+
}
15+
16+
fn unset_function(core: &mut ShellCore, name: &str) -> i32 {
17+
core.data.unset_function(name);
18+
0
19+
}
20+
1121
pub fn unset(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
1222
match args[1].as_ref() {
13-
"-f" => 0,
14-
"-v" => 0,
15-
name => unset_var(core, name),
23+
"-f" => {
24+
if args.len() > 2 {
25+
return unset_function(core, &args[2]);
26+
}
27+
},
28+
"-v" => {
29+
if args.len() > 2 {
30+
return unset_var(core, &args[2]);
31+
}
32+
},
33+
name => return unset_all(core, name),
1634
}
35+
0
1736
}

src/core/data.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,6 @@ impl Data {
157157
self.set_layer_param(key, val, 0);
158158
}
159159

160-
pub fn unset(&mut self, key: &str) {
161-
for layer in &mut self.parameters {
162-
layer.remove(key);
163-
}
164-
}
165-
166160
pub fn set_local_param(&mut self, key: &str, val: &str) {
167161
let layer = self.parameters.len();
168162
self.set_layer_param(key, val, layer-1);
@@ -241,4 +235,19 @@ impl Data {
241235
prev_head = head;
242236
}
243237
}
238+
239+
pub fn unset_var(&mut self, key: &str) {
240+
for layer in &mut self.parameters {
241+
layer.remove(key);
242+
}
243+
}
244+
245+
pub fn unset_function(&mut self, key: &str) {
246+
self.functions.remove(key);
247+
}
248+
249+
pub fn unset(&mut self, key: &str) {
250+
self.unset_var(key);
251+
self.unset_function(key);
252+
}
244253
}

src/elements/command/simple.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,19 @@ impl SimpleCommand {
9191

9292
match unistd::execvp(&cargs[0], &cargs) {
9393
Err(Errno::E2BIG) => {
94-
println!("sush: {}: Arg list too long", &self.args[0]);
94+
eprintln!("sush: {}: Arg list too long", &self.args[0]);
9595
process::exit(126)
9696
},
9797
Err(Errno::EACCES) => {
98-
println!("sush: {}: Permission denied", &self.args[0]);
98+
eprintln!("sush: {}: Permission denied", &self.args[0]);
9999
process::exit(126)
100100
},
101101
Err(Errno::ENOENT) => {
102-
println!("{}: command not found", &self.args[0]);
102+
eprintln!("{}: command not found", &self.args[0]);
103103
process::exit(127)
104104
},
105105
Err(err) => {
106-
println!("Failed to execute. {:?}", err);
106+
eprintln!("Failed to execute. {:?}", err);
107107
process::exit(127)
108108
}
109109
_ => panic!("SUSH INTERNAL ERROR (never come here)")

test/test.bash

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,21 @@ res=$($com <<< 'eval "(" echo abc ")" "|" rev')
9090
res=$($com <<< 'A=aaa ; unset A ; echo $A')
9191
[ "$res" = "" ] || err $LINENO
9292

93+
res=$($com <<< 'A=aaa ; unset -f A ; echo $A')
94+
[ "$res" = "aaa" ] || err $LINENO
95+
96+
res=$($com <<< 'A=aaa ; unset -v A ; echo $A')
97+
[ "$res" = "" ] || err $LINENO
98+
99+
res=$($com <<< 'A () { echo aaa ; } ; unset -v A ; A')
100+
[ "$res" = "aaa" ] || err $LINENO
101+
102+
res=$($com <<< 'A () { echo aaa ; } ; unset -f A ; A')
103+
[ "$res" = "" ] || err $LINENO
104+
105+
res=$($com <<< 'A () { echo aaa ; } ; unset A ; A')
106+
[ "$res" = "" ] || err $LINENO
107+
93108
### COMPOUND COMMAND TEST ###
94109

95110
res=$($com <<< '(echo hoge; echo fuge)')

0 commit comments

Comments
 (0)