Skip to content

Commit 2bf76b1

Browse files
committed
Fix conflict
2 parents d66d3f9 + 0bfad0d commit 2bf76b1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+692
-231
lines changed

.github/workflows/macos.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ jobs:
2020
- name: Bash tests
2121
run: |
2222
dir=$PWD
23-
git clone https://github.com/shellgei/rusty_bash_test -b v1.1.4 --depth 1
23+
git clone https://github.com/shellgei/rusty_bash_test -b v1.1.5 --depth 1
2424
cd rusty_bash_test
2525
./test.bash $dir

.github/workflows/ubuntu.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ jobs:
2020
- name: Bash tests
2121
run: |
2222
dir=$PWD
23-
git clone https://github.com/shellgei/rusty_bash_test -b v1.1.4 --depth 1
23+
git clone https://github.com/shellgei/rusty_bash_test -b v1.1.5 --depth 1
2424
cd rusty_bash_test
2525
./test.bash $dir

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sush"
3-
version = "1.1.4"
3+
version = "1.1.5"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -12,6 +12,7 @@ unicode-width = "0.1.11"
1212
signal-hook = "0.3.17"
1313
rev_lines = "0.3.0"
1414
faccess = "0.2.4"
15+
io-streams = "0.16.3"
1516
regex = "1.11.1"
1617
rand = "0.9"
1718
rand_chacha = { version = "0.9.0", features = [ "os_rng" ]}

src/core.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ pub mod jobtable;
1010
pub mod options;
1111

1212
use crate::{error, proc_ctrl, signal};
13-
use crate::error::exec::ExecError;
1413
use self::database::DataBase;
1514
use self::options::Options;
16-
use self::completion::CompletionInfo;
15+
use self::completion::{Completion, CompletionEntry};
1716
use std::collections::HashMap;
1817
use std::os::fd::{FromRawFd, OwnedFd};
1918
use std::{io, env, path};
@@ -51,7 +50,6 @@ pub struct ShellCore {
5150
pub builtins: HashMap<String, fn(&mut ShellCore, &mut Vec<String>) -> i32>,
5251
pub sigint: Arc<AtomicBool>,
5352
pub trapped: Vec<(Arc<AtomicBool>, String)>,
54-
pub read_stdin: bool,
5553
pub is_subshell: bool,
5654
pub source_function_level: i32,
5755
pub source_files: Vec<String>,
@@ -65,10 +63,7 @@ pub struct ShellCore {
6563
pub job_table: Vec<JobEntry>,
6664
pub job_table_priority: Vec<usize>,
6765
current_dir: Option<path::PathBuf>, // the_current_working_directory
68-
pub completion_info: HashMap<String, CompletionInfo>,
69-
pub current_completion_info: CompletionInfo,
70-
pub completion_functions: HashMap<String, String>,
71-
pub default_completion_functions: String,
66+
pub completion: Completion,
7267
pub measured_time: MeasuredTime,
7368
pub options: Options,
7469
pub shopts: Options,
@@ -83,7 +78,7 @@ impl ShellCore {
8378
let mut core = ShellCore{
8479
db: DataBase::new(),
8580
sigint: Arc::new(AtomicBool::new(false)),
86-
read_stdin: true,
81+
//read_stdin: true,
8782
options: Options::new_as_basic_opts(),
8883
shopts: Options::new_as_shopts(),
8984
script_name: "-".to_string(),
@@ -99,8 +94,8 @@ impl ShellCore {
9994
let _ = core.db.set_param("PS4", "+ ", None);
10095

10196
if unistd::isatty(0) == Ok(true) {
102-
core.db.flags += "im";
103-
core.read_stdin = false;
97+
core.db.flags += "imH";
98+
//core.read_stdin = false;
10499
let _ = core.db.set_param("PS1", "🍣 ", None);
105100
let _ = core.db.set_param("PS2", "> ", None);
106101
let fd = fcntl::fcntl(0, fcntl::F_DUPFD_CLOEXEC(255))
@@ -148,20 +143,20 @@ impl ShellCore {
148143
self.db.exit_status = if self.db.exit_status == 0 { 1 } else { 0 };
149144
}
150145

151-
pub fn run_builtin(&mut self, args: &mut Vec<String>, special_args: &mut Vec<String>)
152-
-> Result<bool, ExecError> {
146+
pub fn run_builtin(&mut self, args: &mut Vec<String>, special_args: &mut Vec<String>) -> bool {
153147
if args.is_empty() {
154-
return Err(ExecError::Bug("ShellCore::run_builtin".to_string()));
148+
eprintln!("ShellCore::run_builtin");
149+
return false;
155150
}
156151

157152
if self.builtins.contains_key(&args[0]) {
158153
let func = self.builtins[&args[0]];
159154
args.append(special_args);
160155
self.db.exit_status = func(self, args);
161-
return Ok(true);
156+
return true;
162157
}
163158

164-
Ok(false)
159+
false
165160
}
166161

167162
fn set_subshell_parameters(&mut self) -> Result<(), String> {
@@ -233,12 +228,15 @@ impl ShellCore {
233228
}
234229

235230
fn replace_alias_core(&self, word: &mut String) -> bool {
236-
if ! self.db.flags.contains('i') {
237-
return false;
231+
if ! self.shopts.query("expand_aliases") {
232+
if ! self.db.flags.contains('i') {
233+
return false;
234+
}
238235
}
239236

240237
let mut ans = false;
241238
let mut prev_head = "".to_string();
239+
let history = vec![word.clone()];
242240

243241
loop {
244242
let head = match word.replace("\n", " ").split(' ').nth(0) {
@@ -252,6 +250,9 @@ impl ShellCore {
252250

253251
if let Some(value) = self.aliases.get(&head) {
254252
*word = word.replacen(&head, value, 1);
253+
if history.contains(word) {
254+
return false;
255+
}
255256
ans = true;
256257
}
257258
prev_head = head;

src/core/builtins/compgen.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ fn replace_args_compgen(args: &mut Vec<String>) -> bool {
7676
"file" => "-f",
7777
"user" => "-u",
7878
"setopt" => "-o",
79+
"function" => "-A function",
7980
"hostname" => "-A hostname",
8081
"shopt" => "-A shopt",
8182
"stopped" => "-A stopped",
@@ -136,6 +137,7 @@ pub fn compgen(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
136137
"-j" => compgen_j(core, &mut args),
137138
"-u" => compgen_u(core, &mut args),
138139
"-v" => compgen_v(core, &mut args),
140+
"-A function" => compgen_function(core, &mut args),
139141
"-A hostname" => compgen_hostname(core, &mut args),
140142
"-A shopt" => compgen_shopt(core, &mut args),
141143
"-A stopped" => compgen_stopped(core, &mut args),
@@ -202,7 +204,7 @@ fn drop_unmatch(args: &mut Vec<String>, pos: usize, list: &mut Vec<String>) {
202204
pub fn compgen_a(core: &mut ShellCore, args: &mut Vec<String>) -> Vec<String> {
203205
let mut commands = vec![];
204206

205-
let mut aliases: Vec<String> = core.aliases.clone().into_keys().collect();
207+
let mut aliases: Vec<String> = core.aliases.keys().map(|k| k.clone()).collect();
206208
commands.append(&mut aliases);
207209

208210
let head = get_head(args, 2);
@@ -214,7 +216,7 @@ pub fn compgen_a(core: &mut ShellCore, args: &mut Vec<String>) -> Vec<String> {
214216

215217
pub fn compgen_b(core: &mut ShellCore, args: &mut Vec<String>) -> Vec<String> {
216218
let mut commands = vec![];
217-
let mut builtins: Vec<String> = core.builtins.clone().into_keys().collect();
219+
let mut builtins: Vec<String> = core.builtins.keys().map(|k| k.clone()).collect();
218220
commands.append(&mut builtins);
219221

220222
let head = get_head(args, 2);
@@ -231,11 +233,11 @@ pub fn compgen_c(core: &mut ShellCore, args: &mut Vec<String>) -> Vec<String> {
231233
}
232234
commands.retain(|p| Path::new(p).executable() || file_check::is_dir(p));
233235

234-
let mut aliases: Vec<String> = core.aliases.clone().into_keys().collect();
236+
let mut aliases: Vec<String> = core.aliases.keys().map(|k| k.clone()).collect();
235237
commands.append(&mut aliases);
236-
let mut builtins: Vec<String> = core.builtins.clone().into_keys().collect();
238+
let mut builtins: Vec<String> = core.builtins.keys().map(|k| k.clone()).collect();
237239
commands.append(&mut builtins);
238-
let mut functions: Vec<String> = core.db.functions.clone().into_keys().collect();
240+
let mut functions: Vec<String> = core.db.functions.keys().map(|k| k.clone()).collect();
239241
commands.append(&mut functions);
240242

241243
let head = get_head(args, 2);
@@ -281,9 +283,9 @@ pub fn compgen_h(core: &mut ShellCore, _: &mut Vec<String>) -> Vec<String> {
281283
pub fn compgen_v(core: &mut ShellCore, args: &mut Vec<String>) -> Vec<String> {
282284
let mut commands = vec![];
283285

284-
let mut aliases: Vec<String> = core.aliases.clone().into_keys().collect();
286+
let mut aliases: Vec<String> = core.aliases.keys().map(|k| k.clone()).collect();
285287
commands.append(&mut aliases);
286-
let mut functions: Vec<String> = core.db.functions.clone().into_keys().collect();
288+
let mut functions: Vec<String> = core.db.functions.keys().map(|k| k.clone()).collect();
287289
commands.append(&mut functions);
288290
let mut vars: Vec<String> = core.db.get_keys();
289291
commands.append(&mut vars);
@@ -367,6 +369,12 @@ pub fn compgen_shopt(core: &mut ShellCore, args: &mut Vec<String>) -> Vec<String
367369
ans
368370
}
369371

372+
pub fn compgen_function(core: &mut ShellCore, args: &mut Vec<String>) -> Vec<String> {
373+
let mut ans = core.db.functions.keys().map(|k| k.clone()).collect();
374+
drop_unmatch(args, 2, &mut ans);
375+
ans
376+
}
377+
370378
pub fn compgen_hostname(_: &mut ShellCore, _: &mut Vec<String>) -> Vec<String> {
371379
//TODO: Implement!
372380
vec![]

src/core/builtins/complete.rs

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

44
use crate::{builtins, ShellCore};
5-
use crate::core::{CompletionInfo, HashMap};
5+
use crate::core::{CompletionEntry, HashMap};
66
use crate::utils::arg;
77

88
fn action_to_reduce_symbol(arg: &str) -> String {
@@ -42,11 +42,11 @@ fn opt_to_action(arg: &str) -> String {
4242
}
4343

4444
fn print_complete(core: &mut ShellCore) -> i32 {
45-
if core.default_completion_functions != "" {
46-
println!("complete -F {} -D", &core.default_completion_functions);
45+
if core.completion.default_function != "" {
46+
println!("complete -F {} -D", &core.completion.default_function);
4747
}
4848

49-
for (name, info) in &core.completion_info {
49+
for (name, info) in &core.completion.entries {
5050
if info.function != "" {
5151
print!("complete -F {} ", &info.function);
5252
}else if info.action != "" {
@@ -80,16 +80,16 @@ fn complete_f(core: &mut ShellCore, args: &mut Vec<String>, o_options: &Vec<Stri
8080
}
8181

8282
if d_option {
83-
core.default_completion_functions = args[1].clone();
83+
core.completion.default_function = args[1].clone();
8484
return 0;
8585
}else {
8686
let func = args[1].clone();
8787
for command in &args[2..] {
88-
if ! core.completion_info.contains_key(command) {
89-
core.completion_info.insert(command.clone(), CompletionInfo::default());
88+
if ! core.completion.entries.contains_key(command) {
89+
core.completion.entries.insert(command.clone(), CompletionEntry::default());
9090
}
9191

92-
let info = &mut core.completion_info.get_mut(command).unwrap();
92+
let info = &mut core.completion.entries.get_mut(command).unwrap();
9393
info.function = func.clone();
9494
info.o_options = o_options.clone();
9595
}
@@ -100,7 +100,7 @@ fn complete_f(core: &mut ShellCore, args: &mut Vec<String>, o_options: &Vec<Stri
100100

101101
fn complete_r(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
102102
for command in &mut args[1..] {
103-
core.completion_info.remove(command);
103+
core.completion.entries.remove(command);
104104
}
105105

106106
0
@@ -135,11 +135,11 @@ pub fn complete(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
135135
let action = opt_to_action(&args[1]);
136136
if action != "" {
137137
for command in &args[2..] {
138-
if ! core.completion_info.contains_key(command) {
139-
core.completion_info.insert(command.clone(), CompletionInfo::default());
138+
if ! core.completion.entries.contains_key(command) {
139+
core.completion.entries.insert(command.clone(), CompletionEntry::default());
140140
}
141141

142-
let info = &mut core.completion_info.get_mut(command).unwrap();
142+
let info = &mut core.completion.entries.get_mut(command).unwrap();
143143
info.action = action.clone();
144144
info.options = options.clone();
145145
}
@@ -148,11 +148,11 @@ pub fn complete(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
148148

149149
if args.len() > 3 && args[1] == "-A" {
150150
for command in &args[3..] {
151-
if ! core.completion_info.contains_key(command) {
152-
core.completion_info.insert(command.clone(), CompletionInfo::default());
151+
if ! core.completion.entries.contains_key(command) {
152+
core.completion.entries.insert(command.clone(), CompletionEntry::default());
153153
}
154154

155-
let info = &mut core.completion_info.get_mut(command).unwrap();
155+
let info = &mut core.completion.entries.get_mut(command).unwrap();
156156
info.action = args[2].clone();
157157
info.options = options.clone();
158158
}

src/core/builtins/compopt.rs

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

44
use crate::ShellCore;
5-
use crate::core::CompletionInfo;
5+
use crate::core::CompletionEntry;
66
use crate::utils::arg;
77

8-
fn compopt_set(info: &mut CompletionInfo, plus: &Vec<String>, minus: &Vec<String>) -> i32 {
8+
fn compopt_set(info: &mut CompletionEntry, plus: &Vec<String>, minus: &Vec<String>) -> i32 {
99
for opt in minus { //add
1010
if ! info.o_options.contains(opt) {
1111
info.o_options.push(opt.to_string());
@@ -26,8 +26,8 @@ fn compopt_print(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
2626
let optlist: Vec<String> = optlist.iter().map(|s| s.to_string()).collect();
2727

2828
let com = args[1].clone();
29-
if core.completion_info.contains_key(&com) {
30-
let info = &core.completion_info.get_mut(&com).unwrap();
29+
if core.completion.entries.contains_key(&com) {
30+
let info = &core.completion.entries.get_mut(&com).unwrap();
3131

3232
print!("compopt ");
3333
for opt in &optlist {
@@ -47,7 +47,7 @@ fn compopt_print(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
4747

4848
pub fn compopt(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
4949
if args.len() < 2 {
50-
dbg!("{:?}", &core.completion_info);
50+
dbg!("{:?}", &core.completion.entries);
5151
return 1;
5252
}
5353

@@ -104,9 +104,9 @@ pub fn compopt(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
104104
}
105105

106106
let info = if args.len() == 1 {
107-
&mut core.current_completion_info
107+
&mut core.completion.current
108108
}else if args.len() == 2 {
109-
match core.completion_info.get_mut(&args[1]) {
109+
match core.completion.entries.get_mut(&args[1]) {
110110
Some(i) => i,
111111
None => return 1,
112112
}

src/core/builtins/option.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub fn set_options(core: &mut ShellCore, args: &[String]) -> Result<(), ExecErro
2525
let pm = a.chars().nth(0).unwrap();
2626
let ch = a.chars().nth(1).unwrap();
2727

28-
if (pm != '-' && pm != '+') || "xveB".find(ch).is_none() {
28+
if (pm != '-' && pm != '+') || "xveBH".find(ch).is_none() {
2929
return Err(ExecError::InvalidOption(a.to_string()));
3030
}
3131

@@ -188,7 +188,7 @@ pub fn shopt(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
188188

189189
let res = match args[1].as_str() { //TODO: args[3..] must to be set
190190
"-s" => {
191-
if ["extglob", "progcomp", "nullglob", "dotglob", "globstar", "globskipdots", "nocasematch"].iter().any(|&e| e == args[2]) {
191+
if core.shopts.implemented.contains(&args[2]) {
192192
core.shopts.set(&args[2], true)
193193
}else{
194194
let msg = format!("shopt: {}: not supported yet", &args[2]);

0 commit comments

Comments
 (0)