Skip to content

Commit

Permalink
Implement shopt -q
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Dec 23, 2024
1 parent 39851b3 commit d78686a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/core/builtins/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ pub fn shopt_print(core: &mut ShellCore, args: &mut Vec<String>, all: bool) -> i
match args[1].as_str() {
"-s" => core.shopts.print_if(true),
"-u" => core.shopts.print_if(false),
"-q" => return 0,
opt => res = core.shopts.print_opt(opt),
}

Expand All @@ -139,6 +140,19 @@ pub fn shopt(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {

let res = match args[1].as_str() {
"-s" => core.shopts.set(&args[2], true),
"-q" => {
for arg in &args[2..] {
if ! core.shopts.exist(arg) {
let msg = format!("shopt: {}: invalid shell option name", &arg);
error::print(&msg, core);
return 1;
}
if ! core.shopts.query(arg) {
return 1;
}
}
return 0;
},
"-u" => core.shopts.set(&args[2], false),
arg => {
eprintln!("sush: shopt: {}: invalid shell option name", arg);
Expand Down
6 changes: 5 additions & 1 deletion src/core/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,12 @@ impl Options {
list.iter().for_each(|e| println!("{}", e));
}

pub fn exist(&self, opt: &str) -> bool {
self.opts.contains_key(opt)
}

pub fn query(&self, opt: &str) -> bool {
self.opts.contains_key(opt) && self.opts[opt]
self.exist(opt) && self.opts[opt]
}

pub fn set(&mut self, opt: &str, onoff: bool) -> bool {
Expand Down

0 comments on commit d78686a

Please sign in to comment.