Skip to content

Commit de34340

Browse files
committed
Refactor
1 parent e1693e8 commit de34340

File tree

7 files changed

+54
-15
lines changed

7 files changed

+54
-15
lines changed

src/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::{proc_ctrl, signal};
1818
use std::sync::Arc;
1919
use std::sync::atomic::AtomicBool;
2020

21-
type BuiltinFunc = fn(&mut ShellCore, &mut [String]) -> i32;
21+
type BuiltinFunc = fn(&mut ShellCore, &[String]) -> i32;
2222

2323
#[derive(Default)]
2424
pub struct ShellCore {

src/core/builtins.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ impl ShellCore {
2121
}
2222
}
2323

24-
pub fn exit(core: &mut ShellCore, args: &mut [String]) -> i32 {
24+
pub fn exit(core: &mut ShellCore, args: &[String]) -> i32 {
2525
eprintln!("exit");
2626
if args.len() > 1 {
2727
let _ = core.db.set_param("?", &args[1], None);
2828
}
2929
exit::normal(core)
3030
}
3131

32-
pub fn false_(_: &mut ShellCore, _: &mut [String]) -> i32 {
32+
pub fn false_(_: &mut ShellCore, _: &[String]) -> i32 {
3333
1
3434
}
3535

36-
pub fn return_(core: &mut ShellCore, args: &mut[String]) -> i32 {
36+
pub fn return_(core: &mut ShellCore, args: &[String]) -> i32 {
3737
if core.source_function_level <= 0 {
3838
eprintln!("sush: return: can only `return' from a function or sourced script");
3939
return 2;
@@ -50,6 +50,6 @@ pub fn return_(core: &mut ShellCore, args: &mut[String]) -> i32 {
5050
2
5151
}
5252

53-
pub fn true_(_: &mut ShellCore, _: &mut [String]) -> i32 {
53+
pub fn true_(_: &mut ShellCore, _: &[String]) -> i32 {
5454
0
5555
}

src/core/builtins/cd.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use crate::ShellCore;
66
use super::utils;
77

8-
pub fn cd(core: &mut ShellCore, args: &mut [String]) -> i32 {
8+
pub fn cd(core: &mut ShellCore, args: &[String]) -> i32 {
99
if args.len() > 2 {
1010
eprintln!("sush: cd: too many arguments");
1111
return 1;
@@ -23,15 +23,16 @@ pub fn cd(core: &mut ShellCore, args: &mut [String]) -> i32 {
2323
}
2424
}
2525

26-
fn cd_1arg(core: &mut ShellCore, args: &mut [String]) -> i32 {
26+
fn cd_1arg(core: &mut ShellCore, args: &[String]) -> i32 {
2727
let var = "~".to_string();
2828
let mut args = args.to_vec();
2929
args.push(var);
3030
set_oldpwd(core);
3131
change_directory(core, &mut args)
3232
}
3333

34-
fn cd_oldpwd(core: &mut ShellCore, args: &mut [String]) -> i32 {
34+
fn cd_oldpwd(core: &mut ShellCore, args: &[String]) -> i32 {
35+
let mut args = args.to_vec();
3536
if let Ok(old) = core.db.get_param("OLDPWD") {
3637
println!("{}", &old);
3738
args[1] = old.to_string();
@@ -41,7 +42,7 @@ fn cd_oldpwd(core: &mut ShellCore, args: &mut [String]) -> i32 {
4142
}
4243

4344
set_oldpwd(core);
44-
change_directory(core, args)
45+
change_directory(core, &mut args)
4546
}
4647

4748
fn set_oldpwd(core: &mut ShellCore) {
@@ -50,7 +51,7 @@ fn set_oldpwd(core: &mut ShellCore) {
5051
};
5152
}
5253

53-
fn change_directory(core: &mut ShellCore, args: &mut [String]) -> i32 {
54+
fn change_directory(core: &mut ShellCore, args: &[String]) -> i32 {
5455
let path = utils::make_canonical_path(core, &args[1]);
5556
if core.set_current_directory(&path).is_ok() {
5657
let _ = core.db.set_param("PWD", &path.display().to_string(), None);

src/core/builtins/pwd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use crate::ShellCore;
66

7-
pub fn pwd(core: &mut ShellCore, args: &mut [String]) -> i32 {
7+
pub fn pwd(core: &mut ShellCore, args: &[String]) -> i32 {
88
if args.len() == 1 || &args[1][..1] != "-" { // $ pwd, $ pwd aaa
99
return show_pwd(core, false);
1010
}

src/elements/command/function_def.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl Command for FunctionDefinition {
3434
}
3535

3636
impl FunctionDefinition {
37-
pub fn run_as_command(&mut self, args: &mut Vec<String>, core: &mut ShellCore) {
37+
pub fn run_as_command(&mut self, args: &mut [String], core: &mut ShellCore) {
3838
args[0] = core.db.position_parameters[0][0].clone();
3939
core.db.position_parameters.push(args.to_vec());
4040

src/elements/command/simple.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//SPDX-License-Identifier: BSD-3-Clause
33

44
mod parser;
5+
mod run_internal;
56

67
use crate::{ShellCore, proc_ctrl};
78
use crate::error::exec::ExecError;
@@ -56,10 +57,9 @@ impl Command for SimpleCommand {
5657
e.print(core);
5758
}
5859

59-
if ! core.run_function(&mut self.args)
60-
&& ! core.run_builtin(&mut self.args) {
60+
if !run_internal::run(self, core)? {
6161
self.set_environment_variables(core)?;
62-
proc_ctrl::exec_command(&mut self.args)
62+
proc_ctrl::exec_command(&self.args);
6363
}
6464

6565
core.db.pop_local();
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//SPDX-FileCopyrightText: 2025 Ryuichi Ueda ryuichiueda@gmail.com
2+
//SPDX-License-Identifier: BSD-3-Clause
3+
4+
use super::SimpleCommand;
5+
use crate::error::exec::ExecError;
6+
use crate::ShellCore;
7+
8+
pub fn run(com: &mut SimpleCommand, core: &mut ShellCore) -> Result<bool, ExecError> {
9+
let ans = run_function(&mut com.args, core)
10+
|| run_builtin(com, core)?;
11+
Ok(ans)
12+
}
13+
14+
fn run_function(args: &mut [String], core: &mut ShellCore) -> bool {
15+
match core.db.functions.get_mut(&args[0]) {
16+
Some(f) => {
17+
f.clone().run_as_command(args, core);
18+
true
19+
}
20+
None => false,
21+
}
22+
}
23+
24+
pub fn run_builtin(com: &mut SimpleCommand, core: &mut ShellCore) -> Result<bool, ExecError> {
25+
if com.args.is_empty() {
26+
eprintln!("ShellCore::run_builtin");
27+
return Ok(false);
28+
}
29+
30+
if !core.builtins.contains_key(&com.args[0]) {
31+
return Ok(false);
32+
}
33+
34+
let func = core.builtins[&com.args[0]];
35+
let exit_status = func(core, &com.args[..]);
36+
let _ = core.db.set_param("?", &exit_status.to_string(), Some(0));
37+
Ok(true)
38+
}

0 commit comments

Comments
 (0)