Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Jan 16, 2025
1 parent 6c4d25b commit 86d942b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 16 deletions.
34 changes: 29 additions & 5 deletions src/core/builtins/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//SPDX-License-Identifier: BSD-3-Clause

use crate::{error, ShellCore};
use crate::error::exec;
use crate::error::exec::ExecError;
use crate::utils::arg;
use super::parameter;

Expand All @@ -15,27 +17,36 @@ fn set_option(core: &mut ShellCore, opt: char, pm: char) {
}
}

pub fn set_options(core: &mut ShellCore, args: &[String]) -> i32 {
pub fn set_options(core: &mut ShellCore, args: &[String]) -> Result<(), ExecError> {
for a in args {
if a.len() != 2 {
return Err(ExecError::InvalidOption(a.to_string()));
/*
error::internal("invalid option");
return 1;
*/
}

let pm = a.chars().nth(0).unwrap();
let ch = a.chars().nth(1).unwrap();

if pm != '-' && pm != '+' {
return Err(ExecError::InvalidOption(a.to_string()));
/*
error::internal("not an option");
return 1;
*/
}else if "xveB".find(ch).is_none() {
return Err(ExecError::InvalidOption(a.to_string()));
/*
eprintln!("sush: set: {}: invalid option", &a);
return 2;
*/
}

set_option(core, ch, pm);
}
0
Ok(())
}

pub fn set(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
Expand All @@ -49,7 +60,13 @@ pub fn set(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {

if args[1].starts_with("--") {
args.remove(0);
return parameter::set_positions(core, &args)
return match parameter::set_positions(core, &args) {
Ok(()) => 0,
Err(e) => {
exec::print_error(e, core);
return 1;
},
}
}

if args[1] == "-o" || args[1] == "+o" {
Expand All @@ -71,9 +88,16 @@ pub fn set(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
}

match args[1].starts_with("-") || args[1].starts_with("+") {
true => set_options(core, &args[1..]),
false => parameter::set_positions(core, &args),
true => if let Err(e) = set_options(core, &args[1..]) {
exec::print_error(e, core);
return 2;
},
false => if let Err(e) = parameter::set_positions(core, &args) {
exec::print_error(e, core);
return 2;
},
}
0
}

pub fn shift(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
Expand Down
10 changes: 4 additions & 6 deletions src/core/builtins/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@
use crate::{ShellCore, utils, Feeder};
use crate::error::exec;
use crate::error::exec::ExecError;
use crate::utils::exit;
use crate::elements::substitution::Substitution;
use crate::utils::arg;

pub fn set_positions(core: &mut ShellCore, args: &[String]) -> i32 {
match core.db.position_parameters.pop() {
None => exit::internal("empty param stack"),
_ => {},
pub fn set_positions(core: &mut ShellCore, args: &[String]) -> Result<(), ExecError> {
if core.db.position_parameters.pop().is_none() {
return Err(ExecError::Other("empty param stack".to_string()));
}
core.db.position_parameters.push(args.to_vec());
0
Ok(())
}

fn print_data(name: &str, core: &mut ShellCore) {
Expand Down
2 changes: 2 additions & 0 deletions src/error/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub enum ExecError {
Exponent(i64),
InvalidBase(String),
InvalidName(String),
InvalidOption(String),
ValidOnlyInFunction(String),
VariableReadOnly(String),
VariableInvalid(String),
Expand All @@ -32,6 +33,7 @@ impl From<ExecError> for String {
ExecError::Exponent(s) => format!("exponent less than 0 (error token is \"{}\")", s),
ExecError::InvalidName(name) => format!("`{}': invalid name", name),
ExecError::InvalidBase(b) => format!("sush: {0}: invalid arithmetic base (error token is \"{0}\")", b),
ExecError::InvalidOption(opt) => format!("sush: {}: invalid option", opt),
ExecError::AssignmentToNonVariable(right) => format!("attempted assignment to non-variable (error token is \"{}\")", right),
ExecError::ValidOnlyInFunction(com) => format!("{}: can only be used in a function", &com),
ExecError::VariableReadOnly(name) => format!("{}: readonly variable", name),
Expand Down
21 changes: 17 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use builtins::{option, parameter};
use std::{env, process};
use std::sync::atomic::Ordering::Relaxed;
use crate::core::{builtins, ShellCore};
use crate::error::exec;
use crate::elements::script::Script;
use crate::feeder::Feeder;
use utils::{exit, file_check, arg};
Expand Down Expand Up @@ -63,8 +64,14 @@ fn configure(args: &Vec<String>) -> ShellCore {
}
}

option::set_options(&mut core, &options);
parameter::set_positions(&mut core, &parameters);
if let Err(e) = option::set_options(&mut core, &options) {
exec::print_error(e, &mut core);
panic!("");
}
if let Err(e) = parameter::set_positions(&mut core, &parameters) {
exec::print_error(e, &mut core);
panic!("");
}
core
}

Expand Down Expand Up @@ -159,8 +166,14 @@ fn run_and_exit_c_option(args: &Vec<String>, c_parts: &Vec<String>) {
vec![args[0].clone()]
};

option::set_options(&mut core, &mut args[1..].to_vec());
parameter::set_positions(&mut core, &parameters);
if let Err(e) = option::set_options(&mut core, &mut args[1..].to_vec()) {
exec::print_error(e, &mut core);
panic!("");
}
if let Err(e) = parameter::set_positions(&mut core, &parameters) {
exec::print_error(e, &mut core);
panic!("");
}
signal::run_signal_check(&mut core);
core.db.flags.retain(|f| f != 'i');

Expand Down
2 changes: 1 addition & 1 deletion test/ok
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
./test_script.bash
./test_options.bash
./test_glob.bash
./test_redirects.bash
./test_glob.bash
./test_brace.bash
./test_builtins.bash
./test_others.bash
Expand Down

0 comments on commit 86d942b

Please sign in to comment.