From 537cba7c12ac26d3a915840d2e6e1dfa6c2f7a55 Mon Sep 17 00:00:00 2001 From: Ryuichi Ueda Date: Sat, 18 Jan 2025 19:57:53 +0900 Subject: [PATCH 1/5] Fix --- src/elements/script.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/elements/script.rs b/src/elements/script.rs index 45a2590f..126bc9bd 100644 --- a/src/elements/script.rs +++ b/src/elements/script.rs @@ -89,14 +89,13 @@ impl Script { return Err(e); }, Status::NeedMoreLine => { - if ! feeder.feed_additional_line(core).is_ok() { - break; + let res = feeder.feed_additional_line(core); + if let Err(e) = res { + feeder.consume(feeder.len()); + return Err(e); } }, } } - - feeder.consume(feeder.len()); - Ok(None) } } From 58fe30702bdd65f8b3d8c6bebe4a3c41b5858eea Mon Sep 17 00:00:00 2001 From: Ryuichi Ueda Date: Sat, 18 Jan 2025 19:58:35 +0900 Subject: [PATCH 2/5] Fix --- src/utils/exit.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/exit.rs b/src/utils/exit.rs index 6786370c..9ef97f01 100644 --- a/src/utils/exit.rs +++ b/src/utils/exit.rs @@ -1,5 +1,5 @@ //SPDX-FileCopyrightText: 2024 Ryuichi Ueda ryuichiueda@gmail.com -//PDX-License-Identifier: BSD-3-Clause +//SPDX-License-Identifier: BSD-3-Clause use crate::ShellCore; use std::process; From 7a730314273ffd5d1ad5d37e0cb64dc8026621f3 Mon Sep 17 00:00:00 2001 From: Ryuichi Ueda Date: Mon, 20 Jan 2025 16:32:17 +0900 Subject: [PATCH 3/5] Simplify --- src/elements/command.rs | 3 +-- src/elements/job.rs | 3 +-- src/elements/script.rs | 3 +-- src/error.rs | 4 ---- src/error/exec.rs | 32 ++++++++++++++++++++------------ src/error/input.rs | 4 ++-- src/error/parse.rs | 30 ++++++++++++------------------ src/main.rs | 3 +-- 8 files changed, 38 insertions(+), 44 deletions(-) diff --git a/src/elements/command.rs b/src/elements/command.rs index 9d021b11..dad3598f 100644 --- a/src/elements/command.rs +++ b/src/elements/command.rs @@ -8,7 +8,6 @@ pub mod r#while; pub mod r#if; use crate::{ShellCore, Feeder, Script}; -use crate::error::exec; use crate::error::exec::ExecError; use crate::error::parse::ParseError; use crate::utils::exit; @@ -45,7 +44,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) }, diff --git a/src/elements/job.rs b/src/elements/job.rs index 52c2b7e4..32c1222d 100644 --- a/src/elements/job.rs +++ b/src/elements/job.rs @@ -4,7 +4,6 @@ use super::pipeline::Pipeline; use crate::{Feeder, ShellCore}; use crate::core::jobtable::JobEntry; -use crate::error::exec; use crate::error::exec::ExecError; use crate::error::parse::ParseError; use crate::utils::exit; @@ -73,7 +72,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) }, diff --git a/src/elements/script.rs b/src/elements/script.rs index 126bc9bd..c770898a 100644 --- a/src/elements/script.rs +++ b/src/elements/script.rs @@ -3,7 +3,6 @@ use super::job::Job; use crate::{Feeder, ShellCore}; -use crate::error::parse; use crate::error::exec::ExecError; use crate::error::parse::ParseError; @@ -83,7 +82,7 @@ impl Script { Status::UnexpectedSymbol(s) => { eprintln!("Unexpected token: {}", s); let e = ParseError::UnexpectedSymbol(s.clone()); - parse::print_error(e.clone(), core); + e.print(core); core.db.set_param("?", "2").unwrap(); feeder.consume(feeder.len()); return Err(e); diff --git a/src/error.rs b/src/error.rs index ad37adb6..5a206218 100644 --- a/src/error.rs +++ b/src/error.rs @@ -6,10 +6,6 @@ pub mod input; pub mod parse; /* -use crate::ShellCore; -use nix::sys::signal::Signal; -use nix::unistd::Pid; - pub fn print(s: &str, core: &mut ShellCore) { let name = core.db.get_param("0").unwrap(); if core.flags.contains('i') { diff --git a/src/error/exec.rs b/src/error/exec.rs index fdae5fa5..d67b29d0 100644 --- a/src/error/exec.rs +++ b/src/error/exec.rs @@ -4,7 +4,7 @@ use crate::ShellCore; use crate::error::parse::ParseError; -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum ExecError { Internal, ArrayIndexInvalid(String), @@ -28,6 +28,12 @@ pub enum ExecError { impl From 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), @@ -35,8 +41,8 @@ impl From for String { ExecError::DivZero => "divided by 0".to_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::InvalidBase(b) => format!("{0}: invalid arithmetic base (error token is \"{0}\")", b), + ExecError::InvalidOption(opt) => format!("{}: invalid option", opt), ExecError::Interrupted => "interrupted".to_string(), 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), @@ -46,18 +52,20 @@ impl From for String { 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::::from(e); - if core.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.flags.contains('i') { + eprintln!("{}: {}", &name, &s); + }else{ + let lineno = core.db.get_param("LINENO").unwrap_or("".to_string()); + eprintln!("{}: line {}: {}", &name, &lineno, s); + } } } diff --git a/src/error/input.rs b/src/error/input.rs index f60cb9be..7c01eed8 100644 --- a/src/error/input.rs +++ b/src/error/input.rs @@ -8,8 +8,8 @@ pub enum InputError { History, } -impl From for String { - fn from(e: InputError) -> String { +impl From<&InputError> for String { + fn from(e: &InputError) -> String { match e { InputError::Eof => "syntax error: unexpected end of file".to_string(), InputError::Interrupt => "interrupted".to_string(), diff --git a/src/error/parse.rs b/src/error/parse.rs index 77d7c05c..6e6f67e3 100644 --- a/src/error/parse.rs +++ b/src/error/parse.rs @@ -8,32 +8,26 @@ use super::input::InputError; pub enum ParseError { UnexpectedSymbol(String), Input(InputError), - /* - UnexpectedEof, - Interrupted, - */ } -impl From for String { - fn from(e: ParseError) -> String { +impl From<&ParseError> for String { + fn from(e: &ParseError) -> String { match e { ParseError::UnexpectedSymbol(s) => format!("Unexpected token: {}", s), ParseError::Input(e) => From::from(e), - /* - ParseError::UnexpectedEof => "syntax error: unexpected end of file".to_string(), - ParseError::Interrupted => "interrupted".to_string(), - */ } } } -pub fn print_error(e: ParseError, core: &mut ShellCore) { - let name = core.db.get_param("0").unwrap(); - let s: String = From::::from(e); - if core.flags.contains('i') { - eprintln!("{}: {}", &name, &s); - }else{ - let lineno = core.db.get_param("LINENO").unwrap_or("".to_string()); - eprintln!("{}: line {}: {}", &name, &lineno, s); +impl ParseError { + pub fn print(&self, core: &mut ShellCore) { + let name = core.db.get_param("0").unwrap(); + let s: String = From::<&ParseError>::from(self); + if core.flags.contains('i') { + eprintln!("{}: {}", &name, &s); + }else{ + let lineno = core.db.get_param("LINENO").unwrap_or("".to_string()); + eprintln!("{}: line {}: {}", &name, &lineno, s); + } } } diff --git a/src/main.rs b/src/main.rs index 278f3ce6..2f966f74 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,6 @@ use std::{env, process}; use crate::core::ShellCore; use crate::utils::exit; use crate::elements::script::Script; -use crate::error::exec; use crate::error::input::InputError; use crate::feeder::Feeder; use utils::file_check; @@ -56,7 +55,7 @@ fn main_loop(core: &mut ShellCore) { if let Ok(Some(mut s)) = Script::parse(&mut feeder, core){ if let Err(e) = s.exec(core) { - exec::print_error(e, core); + e.print(core); } } core.sigint.store(false, Relaxed); From 554f2953345deaa7202917d2495b386a6bb07548 Mon Sep 17 00:00:00 2001 From: Ryuichi Ueda Date: Mon, 20 Jan 2025 16:49:44 +0900 Subject: [PATCH 4/5] Simplify --- src/elements/script.rs | 12 ++---------- src/main.rs | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/elements/script.rs b/src/elements/script.rs index c770898a..f6a30654 100644 --- a/src/elements/script.rs +++ b/src/elements/script.rs @@ -80,19 +80,11 @@ impl Script { match ans.check_nest(feeder){ Status::NormalEnd => return Ok(Some(ans)), Status::UnexpectedSymbol(s) => { - eprintln!("Unexpected token: {}", s); - let e = ParseError::UnexpectedSymbol(s.clone()); - e.print(core); core.db.set_param("?", "2").unwrap(); - feeder.consume(feeder.len()); - return Err(e); + return Err(ParseError::UnexpectedSymbol(s.clone())); }, Status::NeedMoreLine => { - let res = feeder.feed_additional_line(core); - if let Err(e) = res { - feeder.consume(feeder.len()); - return Err(e); - } + feeder.feed_additional_line(core)?; }, } } diff --git a/src/main.rs b/src/main.rs index 2f966f74..2b6799ff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,9 +53,19 @@ fn main_loop(core: &mut ShellCore) { _ => break, } - if let Ok(Some(mut s)) = Script::parse(&mut feeder, core){ - if let Err(e) = s.exec(core) { + match Script::parse(&mut feeder, core){ + Ok(Some(mut s)) => { + if let Err(e) = s.exec(core) { + e.print(core); + break; + } + } + Ok(None) => {}, + Err(e) => { + dbg!("!!"); + feeder.consume(feeder.len()); e.print(core); + break; } } core.sigint.store(false, Relaxed); From 7daf2d4ed813f5f8c86d1eeb5a75897d3ef2e629 Mon Sep 17 00:00:00 2001 From: Ryuichi Ueda Date: Mon, 20 Jan 2025 16:52:26 +0900 Subject: [PATCH 5/5] Simplify --- src/elements/script.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/elements/script.rs b/src/elements/script.rs index f6a30654..d180fc48 100644 --- a/src/elements/script.rs +++ b/src/elements/script.rs @@ -79,13 +79,11 @@ impl Script { match ans.check_nest(feeder){ Status::NormalEnd => return Ok(Some(ans)), + Status::NeedMoreLine => feeder.feed_additional_line(core)?, Status::UnexpectedSymbol(s) => { core.db.set_param("?", "2").unwrap(); return Err(ParseError::UnexpectedSymbol(s.clone())); }, - Status::NeedMoreLine => { - feeder.feed_additional_line(core)?; - }, } } }