Skip to content

Commit e31d818

Browse files
committed
Merge branch 'sd/202501_0' into sd/202501_1
2 parents b5451cd + 3937ba7 commit e31d818

File tree

14 files changed

+51
-39
lines changed

14 files changed

+51
-39
lines changed

src/core.rs

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

44
pub mod builtins;
5+
pub mod data;
56
pub mod jobtable;
6-
pub mod parameter;
77

8+
use self::data::Data;
89
use std::collections::HashMap;
910
use std::os::fd::{FromRawFd, OwnedFd};
1011
use std::{io, env, path, process};
@@ -19,9 +20,8 @@ use std::sync::atomic::AtomicBool;
1920
use std::sync::atomic::Ordering::Relaxed;
2021

2122
pub struct ShellCore {
23+
pub data: Data,
2224
pub history: Vec<String>,
23-
pub flags: String,
24-
parameters: HashMap<String, String>,
2525
pub builtins: HashMap<String, fn(&mut ShellCore, &mut Vec<String>) -> i32>,
2626
pub sigint: Arc<AtomicBool>,
2727
pub is_subshell: bool,
@@ -50,9 +50,8 @@ fn restore_signal(sig: Signal) {
5050
impl ShellCore {
5151
pub fn new() -> ShellCore {
5252
let mut core = ShellCore{
53+
data: Data::new(),
5354
history: Vec::new(),
54-
flags: String::new(),
55-
parameters: HashMap::new(),
5655
builtins: HashMap::new(),
5756
sigint: Arc::new(AtomicBool::new(false)),
5857
is_subshell: false,
@@ -66,7 +65,7 @@ impl ShellCore {
6665
core.set_builtins();
6766

6867
if is_interactive() {
69-
core.flags += "i";
68+
core.data.flags += "i";
7069
let fd = fcntl::fcntl(2, fcntl::F_DUPFD_CLOEXEC(255))
7170
.expect("sush(fatal): Can't allocate fd for tty FD");
7271
core.tty_fd = Some(unsafe{OwnedFd::from_raw_fd(fd)});
@@ -76,15 +75,15 @@ impl ShellCore {
7675
}
7776

7877
fn set_initial_parameters(&mut self) {
79-
self.set_param("$", &process::id().to_string());
80-
self.set_param("BASHPID", &process::id().to_string());
81-
self.set_param("BASH_SUBSHELL", "0");
82-
self.set_param("?", "0");
83-
self.set_param("HOME", &env::var("HOME").unwrap_or("/".to_string()));
78+
self.data.set_param("$", &process::id().to_string());
79+
self.data.set_param("BASHPID", &process::id().to_string());
80+
self.data.set_param("BASH_SUBSHELL", "0");
81+
self.data.set_param("?", "0");
82+
self.data.set_param("HOME", &env::var("HOME").unwrap_or("/".to_string()));
8483
}
8584

8685
pub fn has_flag(&self, flag: char) -> bool {
87-
self.flags.find(flag) != None
86+
self.data.flags.find(flag) != None
8887
}
8988

9089
pub fn wait_process(&mut self, child: Pid) {
@@ -108,7 +107,7 @@ impl ShellCore {
108107
if exit_status == 130 {
109108
self.sigint.store(true, Relaxed);
110109
}
111-
self.parameters.insert("?".to_string(), exit_status.to_string()); //追加
110+
self.data.parameters.insert("?".to_string(), exit_status.to_string()); //追加
112111
}
113112

114113
fn set_foreground(&self) {
@@ -151,15 +150,15 @@ impl ShellCore {
151150

152151
let func = self.builtins[&args[0]];
153152
let status = func(self, args);
154-
self.parameters.insert("?".to_string(), status.to_string());
153+
self.data.parameters.insert("?".to_string(), status.to_string());
155154
true
156155
}
157156

158157
pub fn exit(&self) -> ! {
159-
let exit_status = match self.parameters["?"].parse::<i32>() {
158+
let exit_status = match self.data.parameters["?"].parse::<i32>() {
160159
Ok(n) => n%256,
161160
Err(_) => {
162-
eprintln!("sush: exit: {}: numeric argument required", self.parameters["?"]);
161+
eprintln!("sush: exit: {}: numeric argument required", self.data.parameters["?"]);
163162
2
164163
},
165164
};
@@ -169,10 +168,10 @@ impl ShellCore {
169168

170169
fn set_subshell_parameters(&mut self) {
171170
let pid = nix::unistd::getpid();
172-
self.parameters.insert("BASHPID".to_string(), pid.to_string());
173-
match self.parameters["BASH_SUBSHELL"].parse::<usize>() {
174-
Ok(num) => self.parameters.insert("BASH_SUBSHELL".to_string(), (num+1).to_string()),
175-
Err(_) => self.parameters.insert("BASH_SUBSHELL".to_string(), "0".to_string()),
171+
self.data.parameters.insert("BASHPID".to_string(), pid.to_string());
172+
match self.data.parameters["BASH_SUBSHELL"].parse::<usize>() {
173+
Ok(num) => self.data.parameters.insert("BASH_SUBSHELL".to_string(), (num+1).to_string()),
174+
Err(_) => self.data.parameters.insert("BASH_SUBSHELL".to_string(), "0".to_string()),
176175
};
177176
}
178177

src/core/builtins.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl ShellCore {
2222
pub fn exit(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
2323
eprintln!("exit");
2424
if args.len() > 1 {
25-
core.parameters.insert("?".to_string(), args[1].clone());
25+
core.data.parameters.insert("?".to_string(), args[1].clone());
2626
}
2727
core.exit()
2828
}

src/core/builtins/cd.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fn cd_1arg(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
3131
}
3232

3333
fn cd_oldpwd(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
34-
if let Some(old) = core.parameters.get("OLDPWD") {
34+
if let Some(old) = core.data.parameters.get("OLDPWD") {
3535
println!("{}", &old);
3636
args[1] = old.to_string();
3737
}else {
@@ -45,14 +45,14 @@ fn cd_oldpwd(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
4545

4646
fn set_oldpwd(core: &mut ShellCore) {
4747
if let Some(old) = core.get_current_directory() {
48-
core.parameters.insert("OLDPWD".to_string(), old.display().to_string());
48+
core.data.parameters.insert("OLDPWD".to_string(), old.display().to_string());
4949
};
5050
}
5151

5252
fn change_directory(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
5353
let path = utils::make_canonical_path(core, &args[1]);
5454
if core.set_current_directory(&path).is_ok() {
55-
core.parameters.insert("PWD".to_string(), path.display().to_string());
55+
core.data.parameters.insert("PWD".to_string(), path.display().to_string());
5656
0
5757
}else{
5858
eprintln!("sush: cd: {:?}: No such file or directory", &path);

src/core/builtins/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn make_absolute_path(core: &mut ShellCore, path_str: &str) -> PathBuf {
1414
}
1515

1616
if path.starts_with("~") { // tilde -> $HOME
17-
if let Some(home_dir) = core.parameters.get("HOME") {
17+
if let Some(home_dir) = core.data.parameters.get("HOME") {
1818
absolute.push(PathBuf::from(home_dir));
1919
if path_str.len() > 1 && path_str.starts_with("~/") {
2020
absolute.push(PathBuf::from(&path_str[2..]));

src/core/parameter.rs renamed to src/core/data.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
//SPDXFileCopyrightText: 2024 Ryuichi Ueda ryuichiueda@gmail.com
22
//SPDXLicense-Identifier: BSD-3-Clause
33

4-
use crate::ShellCore;
4+
use std::collections::HashMap;
55

6-
impl ShellCore {
7-
pub fn get_param_ref(&self, key: &str) -> &str {
6+
#[derive(Debug)]
7+
pub struct Data {
8+
pub flags: String,
9+
pub parameters: HashMap<String, String>,
10+
}
11+
12+
impl Data {
13+
pub fn new() -> Data {
14+
Data {
15+
flags: String::new(),
16+
parameters: HashMap::new(),
17+
}
18+
}
19+
20+
pub fn get_param(&self, key: &str) -> String {
821
match self.parameters.get(key) {
922
Some(val) => val,
1023
None => "",
11-
}
24+
}.to_string()
1225
}
1326

1427
pub fn set_param(&mut self, key: &str, val: &str) {

src/elements/command.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub trait Command {
5757
if self.get_redirects().iter_mut().all(|r| r.connect(true, core)){
5858
self.run(core, false);
5959
}else{
60-
core.set_param("?", "1");
60+
core.data.set_param("?", "1");
6161
}
6262
self.get_redirects().iter_mut().rev().for_each(|r| r.restore());
6363
}

src/elements/command/if.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl Command for IfCommand {
1919
fn run(&mut self, core: &mut ShellCore, _: bool) {
2020
for i in 0..self.if_elif_scripts.len() {
2121
self.if_elif_scripts[i].exec(core);
22-
if core.get_param_ref("?") == "0" {
22+
if core.data.get_param("?") == "0" {
2323
self.then_scripts[i].exec(core);
2424
return;
2525
}

src/elements/command/while.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl Command for WhileCommand {
2121
.expect("SUSH INTERNAL ERROR (no script)")
2222
.exec(core);
2323

24-
if core.get_param_ref("?") != "0" {
24+
if core.data.get_param("?") != "0" {
2525
break;
2626
}
2727

src/elements/job.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl Job {
3636
let pids = pipeline.exec(core, pgid);
3737
core.wait_pipeline(pids);
3838
}
39-
do_next = (core.get_param_ref("?") == "0") == (end == "&&");
39+
do_next = (core.data.get_param("?") == "0") == (end == "&&");
4040
}
4141
}
4242

src/elements/pipeline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub struct Pipeline {
1818
impl Pipeline {
1919
pub fn exec(&mut self, core: &mut ShellCore, pgid: Pid) -> Vec<Option<Pid>> {
2020
if core.sigint.load(Relaxed) { //以下4行追加
21-
core.set_param("?", "130");
21+
core.data.set_param("?", "130");
2222
return vec![];
2323
}
2424

0 commit comments

Comments
 (0)