Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Jan 20, 2025
1 parent 47e4120 commit ad3dfbf
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 39 deletions.
7 changes: 3 additions & 4 deletions src/core/builtins/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//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 Down Expand Up @@ -63,7 +62,7 @@ pub fn set(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
return match parameter::set_positions(core, &args) {
Ok(()) => 0,
Err(e) => {
exec::print_error(e, core);
e.print(core);
return 1;
},
}
Expand All @@ -89,11 +88,11 @@ pub fn set(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {

match args[1].starts_with("-") || args[1].starts_with("+") {
true => if let Err(e) = set_options(core, &args[1..]) {
exec::print_error(e, core);
e.print(core);
return 2;
},
false => if let Err(e) = parameter::set_positions(core, &args) {
exec::print_error(e, core);
e.print(core);
return 2;
},
}
Expand Down
13 changes: 6 additions & 7 deletions src/core/builtins/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//SPDX-License-Identifier: BSD-3-Clause

use crate::{ShellCore, utils, Feeder};
use crate::error::exec;
use crate::error::exec::ExecError;
use crate::elements::substitution::Substitution;
use crate::utils::arg;
Expand Down Expand Up @@ -79,12 +78,12 @@ pub fn local(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
let layer = if core.db.get_layer_num() > 2 {
core.db.get_layer_num() - 2//The last element of data.parameters is for local itself.
}else{
exec::print_error(ExecError::ValidOnlyInFunction("local".to_string()), core);
ExecError::ValidOnlyInFunction("local".to_string()).print(core);
return 1;
};

if let Err(e) = local_(core, args, layer) {
exec::print_error(e, core);
e.print(core);
return 1;
};
0
Expand All @@ -106,11 +105,11 @@ pub fn declare(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
if args.contains(&"-a".to_string()) {
if ! utils::is_name(&name, core) {
let e = ExecError::InvalidName(name.to_string());
exec::print_error(e, core);
e.print(core);
return 1;
}
if let Err(e) = core.db.set_array(&name, vec![], None) {
exec::print_error(e, core);
e.print(core);
return 1;
}

Expand All @@ -120,11 +119,11 @@ pub fn declare(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
if args.contains(&"-A".to_string()) {
if ! utils::is_name(&name, core) {
let e = ExecError::InvalidName(name.to_string());
exec::print_error(e, core);
e.print(core);
return 1;
}
if let Err(e) = core.db.set_assoc(&name, None) {
exec::print_error(e, core);
e.print(core);
return 1;
}

Expand Down
3 changes: 1 addition & 2 deletions src/elements/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub mod r#while;
pub mod r#if;

use crate::{proc_ctrl, ShellCore, Feeder, Script};
use crate::error::exec;
use crate::error::exec::ExecError;
use crate::error::parse::ParseError;
use crate::utils::exit;
Expand Down Expand Up @@ -61,7 +60,7 @@ pub trait Command {
core.initialize_as_subshell(Pid::from_raw(0), pipe.pgid);
io::connect(pipe, self.get_redirects(), core);
if let Err(e) = self.run(core, true) {
exec::print_error(e, core);
e.print(core);
}
exit::normal(core)
},
Expand Down
4 changes: 2 additions & 2 deletions src/elements/command/for.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//SPDX-License-Identifier: BSD-3-Clause

use crate::{ShellCore, Feeder, Script};
use crate::error::exec;

use crate::error::exec::ExecError;
use crate::error::parse::ParseError;
use super::{Command, Redirect};
Expand Down Expand Up @@ -59,7 +59,7 @@ impl ForCommand {
match w.eval(core) {
Ok(mut ws) => ans.append(&mut ws),
Err(e) => {
exec::print_error(e, core);
e.print(core);
return None;
},
}
Expand Down
4 changes: 2 additions & 2 deletions src/elements/command/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pub mod parser;

use crate::{proc_ctrl, ShellCore};
use crate::error::exec;

use crate::error::exec::ExecError;
use crate::utils::exit;
use super::{Command, Pipe, Redirect};
Expand Down Expand Up @@ -141,7 +141,7 @@ impl SimpleCommand {
Ok(())
},
Err(e) => {
exec::print_error(e.clone(), core);
e.print(core);
if ! core.sigint.load(Relaxed) {
core.db.exit_status = 1;
}
Expand Down
3 changes: 1 addition & 2 deletions src/elements/expr/conditional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ pub mod elem;
mod parser;

use crate::ShellCore;
use crate::error::exec;
use crate::error::exec::ExecError;
use crate::utils::{file_check, glob};
use crate::elements::word::Word;
Expand Down Expand Up @@ -139,7 +138,7 @@ impl ConditionalExpr {
let mut err = "syntax error".to_string();
if stack.len() > 1 {
err = format!("syntax error in conditional expression: unexpected token `{}'", &stack[0].to_string());
exec::print_error(ExecError::Other(err), core);
ExecError::Other(err).print(core);
err = format!("syntax error near `{}'", &stack[0].to_string());
}
return Err(ExecError::Other(err));
Expand Down
3 changes: 1 addition & 2 deletions src/elements/io/redirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use std::fs::{File, OpenOptions};
use std::os::fd::{IntoRawFd, RawFd};
use std::io::Error;
use crate::error::exec;
use crate::elements::io;
use crate::elements::word::Word;
use crate::{Feeder, ShellCore};
Expand Down Expand Up @@ -35,7 +34,7 @@ impl Redirect {
let args = match self.right.eval(core) {
Ok(v) => v,
Err(e) => {
exec::print_error(e, core);
e.print(core);
return false;
},
};
Expand Down
3 changes: 1 addition & 2 deletions src/elements/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use super::pipeline::Pipeline;
use crate::{proc_ctrl, Feeder, ShellCore};
use crate::core::jobtable::JobEntry;
use crate::utils::exit;
use crate::error::exec;
use crate::error::exec::ExecError;
use crate::error::parse::ParseError;
use nix::sys::wait::WaitStatus;
Expand Down Expand Up @@ -103,7 +102,7 @@ impl Job {
Ok(ForkResult::Child) => {
core.initialize_as_subshell(Pid::from_raw(0), pgid);
if let Err(e) = self.exec(core, false) {
exec::print_error(e, core);
e.print(core);
}
exit::normal(core)
},
Expand Down
28 changes: 18 additions & 10 deletions src/error/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ pub enum ExecError {

impl From<ExecError> for String {
fn from(e: ExecError) -> String {
Self::from(&e)
}
}

impl From<&ExecError> for String {
fn from(e: &ExecError) -> String {
match e {
ExecError::Internal => "INTERNAL ERROR".to_string(),
ExecError::ArrayIndexInvalid(name) => format!("`{}': not a valid index", name),
Expand All @@ -43,21 +49,23 @@ impl From<ExecError> for String {
ExecError::VariableReadOnly(name) => format!("{}: readonly variable", name),
ExecError::VariableInvalid(name) => format!("`{}': not a valid identifier", name),
ExecError::OperandExpected(token) => format!("{0}: syntax error: operand expected (error token is \"{0}\")", token),
ExecError::ParseError(p) => From::from(&p),
ExecError::ParseError(p) => From::from(p),
ExecError::Recursion(token) => format!("{0}: expression recursion level exceeded (error token is \"{0}\")", token),
ExecError::SubstringMinus(n) => format!("{}: substring expression < 0", n),
ExecError::Other(name) => name,
ExecError::Other(name) => name.to_string(),
}
}
}

pub fn print_error(e: ExecError, core: &mut ShellCore) {
let name = core.db.get_param("0").unwrap();
let s: String = From::<ExecError>::from(e);
if core.db.flags.contains('i') {
eprintln!("{}: {}", &name, &s);
}else{
let lineno = core.db.get_param("LINENO").unwrap_or("".to_string());
eprintln!("{}: line {}: {}", &name, &lineno, s);
impl ExecError {
pub fn print(&self, core: &mut ShellCore) {
let name = core.db.get_param("0").unwrap();
let s: String = From::<&ExecError>::from(self);
if core.db.flags.contains('i') {
eprintln!("{}: {}", &name, &s);
}else{
let lineno = core.db.get_param("LINENO").unwrap_or("".to_string());
eprintln!("{}: line {}: {}", &name, &lineno, s);
}
}
}
9 changes: 4 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ 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 @@ -65,11 +64,11 @@ fn configure(args: &Vec<String>) -> ShellCore {
}

if let Err(e) = option::set_options(&mut core, &options) {
exec::print_error(e, &mut core);
e.print(&mut core);
panic!("");
}
if let Err(e) = parameter::set_positions(&mut core, &parameters) {
exec::print_error(e, &mut core);
e.print(&mut core);
panic!("");
}
core
Expand Down Expand Up @@ -175,11 +174,11 @@ fn run_and_exit_c_option(args: &Vec<String>, c_parts: &Vec<String>) {
};

if let Err(e) = option::set_options(&mut core, &mut args[1..].to_vec()) {
exec::print_error(e, &mut core);
e.print(&mut core);
panic!("");
}
if let Err(e) = parameter::set_positions(&mut core, &parameters) {
exec::print_error(e, &mut core);
e.print(&mut core);
panic!("");
}
signal::run_signal_check(&mut core);
Expand Down
2 changes: 1 addition & 1 deletion test/ok
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
./test_brace.bash
./test_builtins.bash
./test_others.bash
./test_parameters.bash
./test_calculation.bash
./test_parameters.bash
./test_compound.bash
./test_job.bash

0 comments on commit ad3dfbf

Please sign in to comment.