Skip to content

Commit 41fd00b

Browse files
committed
Refactor
1 parent 17113c3 commit 41fd00b

File tree

5 files changed

+65
-32
lines changed

5 files changed

+65
-32
lines changed

src/elements/command/simple.rs

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//SPDX-FileCopyrightText: 2022 Ryuichi Ueda ryuichiueda@gmail.com
22
//SPDX-License-Identifier: BSD-3-Clause
33

4-
use crate::{ShellCore, Feeder};
4+
use crate::{ShellCore, Feeder, proc_ctrl};
55
use crate::error::exec::ExecError;
66
use crate::error::parse::ParseError;
77
use super::{Command, Pipe, Redirect};
@@ -10,12 +10,7 @@ use crate::elements::substitution::Substitution;
1010
use crate::elements::word::Word;
1111
use crate::utils;
1212
use crate::utils::exit;
13-
use nix::unistd;
14-
use std::ffi::CString;
15-
use std::process;
16-
1713
use nix::unistd::Pid;
18-
use nix::errno::Errno;
1914

2015
#[derive(Debug, Default, Clone)]
2116
pub struct SimpleCommand {
@@ -65,7 +60,7 @@ impl Command for SimpleCommand {
6560
if ! core.run_function(&mut self.args)
6661
&& ! core.run_builtin(&mut self.args) {
6762
self.set_environment_variables(core)?;
68-
Self::exec_external_command(&mut self.args)
63+
proc_ctrl::exec_command(&mut self.args)
6964
}
7065

7166
core.db.pop_local();
@@ -84,25 +79,6 @@ impl Command for SimpleCommand {
8479
}
8580

8681
impl SimpleCommand {
87-
fn exec_external_command(args: &mut [String]) -> ! {
88-
let cargs = Self::to_cargs(args);
89-
match unistd::execvp(&cargs[0], &cargs) {
90-
Err(Errno::EACCES) => {
91-
println!("sush: {}: Permission denied", &args[0]);
92-
process::exit(126)
93-
},
94-
Err(Errno::ENOENT) => {
95-
println!("{}: command not found", &args[0]);
96-
process::exit(127)
97-
},
98-
Err(err) => {
99-
println!("Failed to execute. {:?}", err);
100-
process::exit(127)
101-
}
102-
_ => panic!("SUSH INTERNAL ERROR (never come here)")
103-
}
104-
}
105-
10682
fn set_local_params(&mut self,core: &mut ShellCore) -> Result<(), ExecError> {
10783
let layer = Some(core.db.get_layer_num() - 1);
10884
for s in self.substitutions.iter_mut() {
@@ -119,12 +95,6 @@ impl SimpleCommand {
11995
Ok(())
12096
}
12197

122-
fn to_cargs(args: &mut [String]) -> Vec<CString> {
123-
args.iter()
124-
.map(|a| CString::new(a.to_string()).unwrap())
125-
.collect()
126-
}
127-
12898
pub fn eat_substitution(&mut self, feeder: &mut Feeder, core: &mut ShellCore)
12999
-> Result<bool, ParseError> {
130100
if let Some(s) = Substitution::parse(feeder, core)? {

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod core;
55
mod error;
66
mod feeder;
77
mod elements;
8+
mod proc_ctrl;
89
mod signal;
910
mod utils;
1011

src/proc_ctrl.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//SPDX-FileCopyrightText: 2024 Ryuichi Ueda ryuichiueda@gmail.com
2+
//SPDX-License-Identifier: BSD-3-Clause
3+
4+
use crate::utils::c_string;
5+
use nix::errno::Errno;
6+
use nix::unistd;
7+
use std::process;
8+
9+
pub fn exec_command(args: &[String]) -> ! {
10+
let cargs = c_string::to_cargs(args);
11+
let result = unistd::execvp(&cargs[0], &cargs);
12+
13+
match result {
14+
Err(Errno::EACCES) => {
15+
println!("sush: {}: Permission denied", &args[0]);
16+
process::exit(126)
17+
},
18+
Err(Errno::ENOENT) => {
19+
println!("{}: command not found", &args[0]);
20+
process::exit(127)
21+
},
22+
Err(err) => {
23+
println!("Failed to execute. {:?}", err);
24+
process::exit(127)
25+
}
26+
_ => panic!("SUSH INTERNAL ERROR (never come here)")
27+
}
28+
}

src/utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//SPDX-FileCopyrightText: 2024 Ryuichi Ueda <ryuichiueda@gmail.com>
22
//SPDX-License-Identifier: BSD-3-Clause
33

4+
pub mod c_string;
45
pub mod directory;
56
pub mod exit;
67
pub mod file_check;

src/utils/c_string.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//SPDX-FileCopyrightText: 2025 Ryuichi Ueda <ryuichiueda@gmail.com>
2+
//SPDX-License-Identifier: BSD-3-Clause
3+
4+
use std::ffi::CString;
5+
6+
pub fn to_carg(arg: &str) -> CString {
7+
let mut tmp = String::new();
8+
let mut unicode8num = 0;
9+
10+
for c in arg.chars() {
11+
if c as u32 >= 0xE080 && c as u32 <= 0xE0FF {
12+
let num: u8 = (c as u32 - 0xE000) as u8;
13+
let ch = unsafe { String::from_utf8_unchecked(vec![num]) };
14+
tmp.push_str(&ch);
15+
} else if c as u32 >= 0xE200 && c as u32 <= 0xE4FF {
16+
unicode8num <<= 8;
17+
unicode8num += c as u32 & 0xFF;
18+
} else if c as u32 >= 0xE100 && c as u32 <= 0xE1FF {
19+
unicode8num <<= 8;
20+
unicode8num += c as u32 & 0xFF;
21+
let ch = unsafe { char::from_u32_unchecked(unicode8num) }.to_string();
22+
unicode8num = 0; // ^ An error occurs on debug mode.
23+
tmp.push_str(&ch);
24+
} else {
25+
tmp.push(c);
26+
}
27+
}
28+
CString::new(tmp.to_string()).unwrap()
29+
}
30+
31+
pub fn to_cargs(args: &[String]) -> Vec<CString> {
32+
args.iter().map(|s| to_carg(s)).collect()
33+
}

0 commit comments

Comments
 (0)