Skip to content

Commit 8d32a51

Browse files
authored
Merge pull request #167 from t-koba/alpha
Modify: SUSH and BASH version values / cargo fmt
2 parents 95b73b4 + dd0530c commit 8d32a51

File tree

22 files changed

+118
-64
lines changed

22 files changed

+118
-64
lines changed

.sushrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ case "$TERM" in
77
xterm-color|*-256color) color_prompt=yes;;
88
esac
99

10-
build_profile=$([[ "$BASH_VERSION" == *-release ]] || echo "(${BASH_VERSION##*-})")
10+
build_profile=$([[ "$SUSH_VERSION" == *-release ]] || echo "(${SUSH_VERSION##*-})")
1111
if [ "$color_prompt" = yes ]; then
1212
PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\b\[\033[00m\]\[\033[01;35m\]\w\[\033[00m\]'$build_profile'🍣 '
1313
else

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

8+
[package.metadata.compat]
9+
target_bash_version = "5.2.37" # Ubuntu 25.04
10+
811
[dependencies]
912
nix = { version = "0.29.0", features = ["fs", "process", "signal", "term", "user", "time", "hostname", "resource"]}
1013
termion = "4.0.5"
@@ -60,6 +63,8 @@ default = ["0", "1", "2", "3", "4", "5", "6"] # All
6063
5 = ["0", "lang_hi", "lang_ja", "lang_ko", "lang_zh"] # Asia
6164
6 = ["0", "lang_ar", "lang_sw"] # Africa / Middle East
6265

66+
[build-dependencies]
67+
cargo_toml = "0.22"
6368

6469
[profile.release]
6570
opt-level = 3

build.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,42 @@
11
//SPDX-FileCopyrightText: 2024 @caro@mi.shellgei.org
22
//SPDX-License-Identifier: BSD-3-Clause
33

4-
use std::env;
4+
use cargo_toml::Manifest;
5+
use std::{env, path::PathBuf};
56

67
fn main() {
7-
// BASH_VERSION, BASH_VERSINFO[4]
8+
// SUSH_VERSION, SUSH_VERSINFO[4]
89
let profile = env::var("PROFILE").unwrap_or("".to_string());
910
println!("cargo:rustc-env=CARGO_BUILD_PROFILE={profile}");
10-
// HOSTTYPE, MACHTYPE, BASH_VERSINFO[5]
11+
// HOSTTYPE, MACHTYPE, BASH_VERSINFO[5], SUSH_VERSINFO[5]
1112
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or("unknown".to_string());
1213
println!("cargo:rustc-env=CARGO_CFG_TARGET_ARCH={target_arch}");
13-
// MACHTYPE, BASH_VERSINFO[5]
14+
// MACHTYPE, BASH_VERSINFO[5], SUSH_VERSINFO[5]
1415
let target_vendor = env::var("CARGO_CFG_TARGET_VENDOR").unwrap_or("unknown".to_string());
1516
println!("cargo:rustc-env=CARGO_CFG_TARGET_VENDOR={target_vendor}");
16-
// OSTYPE, MACHTYPE, BASH_VERSINFO[5]
17+
// OSTYPE, MACHTYPE, BASH_VERSINFO[5], SUSH_VERSINFO[5]
1718
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or("unknown".to_string());
1819
println!("cargo:rustc-env=CARGO_CFG_TARGET_OS={target_os}");
20+
21+
// metadata
22+
let manifest_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("Cargo.toml");
23+
let manifest = Manifest::from_path(&manifest_path).expect("failed to parse Cargo.toml");
24+
// compat
25+
let compat = manifest
26+
.package
27+
.as_ref()
28+
.and_then(|p| p.metadata.as_ref()?.get("compat"))
29+
.and_then(|v| v.as_table())
30+
.expect("Missing [package.metadata.compat]");
31+
for (k, v) in compat {
32+
let env_key: String = k
33+
.chars()
34+
.map(|c| if c.is_ascii_alphanumeric() { c } else { '_' })
35+
.collect::<String>()
36+
.to_ascii_uppercase();
37+
let val = v
38+
.as_str()
39+
.unwrap_or_else(|| panic!("Non-string value for key {k} in [package.metadata.compat]"));
40+
println!("cargo:rustc-env=COMPAT_{env_key}={val}");
41+
}
1942
}

src/core.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,22 +164,41 @@ impl ShellCore {
164164
let t_os = env!("CARGO_CFG_TARGET_OS");
165165
let machtype = format!("{t_arch}-{t_vendor}-{t_os}");
166166
let symbol = "rusty_bash";
167+
let t_bash_symbol = "bash_compatible";
168+
let t_bash_ver = env!("COMPAT_TARGET_BASH_VERSION");
167169
let vparts = version.split('.').collect();
168-
let versinfo = [vparts, vec![symbol, profile, &machtype]]
170+
let tbvparts = t_bash_ver.split('.').collect();
171+
let sush_versinfo = [vparts, vec![symbol, profile, &machtype]]
172+
.concat()
173+
.iter()
174+
.map(|e| e.to_string())
175+
.collect();
176+
let bash_versinfo = [tbvparts, vec![t_bash_symbol, profile, &machtype]]
169177
.concat()
170178
.iter()
171179
.map(|e| e.to_string())
172180
.collect();
173181

174182
let _ = self.db.set_param(
175-
"BASH_VERSION",
183+
"SUSH_VERSION",
176184
&format!("{version}({symbol})-{profile}"),
177185
None,
178186
);
187+
let _ = self.db.set_param(
188+
"BASH_VERSION",
189+
&format!("{t_bash_ver}({t_bash_symbol})-{profile}"),
190+
None,
191+
);
179192
let _ = self.db.set_param("MACHTYPE", &machtype, None);
180193
let _ = self.db.set_param("HOSTTYPE", t_arch, None);
181194
let _ = self.db.set_param("OSTYPE", t_os, None);
182-
let _ = self.db.set_array("BASH_VERSINFO", Some(versinfo), None);
195+
let _ = self
196+
.db
197+
.set_array("SUSH_VERSINFO", Some(sush_versinfo), None);
198+
let _ = self
199+
.db
200+
.set_array("BASH_VERSINFO", Some(bash_versinfo), None);
201+
self.db.set_flag("SUSH_VERSINFO", 'r', None);
183202
self.db.set_flag("BASH_VERSINFO", 'r', None);
184203
}
185204

src/core/builtins/alias.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
use crate::ShellCore;
55

66
pub fn alias(core: &mut ShellCore, args: &[String]) -> i32 {
7-
if args.len() == 1
8-
|| args.len() == 2 && args[1] == "-p" {
9-
if ! core.shopts.query("expand_aliases") {
7+
if args.len() == 1 || args.len() == 2 && args[1] == "-p" {
8+
if !core.shopts.query("expand_aliases") {
109
return 0;
1110
}
1211
for k in &core.db.get_indexes_all("BASH_ALIASES") {
@@ -24,8 +23,7 @@ pub fn alias(core: &mut ShellCore, args: &[String]) -> i32 {
2423
return 0;
2524
}
2625

27-
if args.len() == 2
28-
&& core.db.has_array_value("BASH_ALIASES", &args[1]) {
26+
if args.len() == 2 && core.db.has_array_value("BASH_ALIASES", &args[1]) {
2927
let alias = core.db.get_elem("BASH_ALIASES", &args[1]).unwrap();
3028
println!("alias {}='{}'", &args[1], &alias);
3129
return 0;

src/core/builtins/command.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use crate::elements::command::simple::SimpleCommand;
55
use crate::elements::io::pipe::Pipe;
66
use crate::utils::{arg, file};
7-
use crate::{error, file_check, proc_ctrl, ShellCore, utils};
7+
use crate::{error, file_check, proc_ctrl, utils, ShellCore};
88

99
pub fn builtin(core: &mut ShellCore, args: &[String]) -> i32 {
1010
if args.len() <= 1 {
@@ -53,7 +53,7 @@ fn command_v(words: &[String], core: &mut ShellCore, large_v: bool) -> i32 {
5353
true => {
5454
println!("{} is a function", &com);
5555
core.db.functions.get_mut(com).unwrap().pretty_print(0);
56-
},
56+
}
5757
false => println!("{}", &com),
5858
}
5959
} else if let Some(path) = file::search_command(com) {

src/core/builtins/option.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ pub fn set(core: &mut ShellCore, args: &[String]) -> i32 {
151151
if let Err(e) = set_positions(core, &args) {
152152
e.print(core);
153153
return 2;
154-
}else{
154+
} else {
155155
return 0;
156156
}
157157
}

src/core/builtins/parameter.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,12 @@ fn set_substitution(
7171
}
7272

7373
/* TODO: chaos!!!! */
74-
let treat_as_array = core.db.is_array(&sub.left_hand.name)
75-
|| core.db.is_assoc(&sub.left_hand.name);
76-
let option_indicate_array = arg::has_option("-A", args)
77-
|| arg::has_option("-a", args);
78-
let treat_as_export = core.db.has_flag(&sub.left_hand.name, 'x')
79-
|| export_opt;
80-
let subs_elem_quoted_string = sub.left_hand.index.is_some()
81-
&& sub.right_hand.text.starts_with("'");
74+
let treat_as_array =
75+
core.db.is_array(&sub.left_hand.name) || core.db.is_assoc(&sub.left_hand.name);
76+
let option_indicate_array = arg::has_option("-A", args) || arg::has_option("-a", args);
77+
let treat_as_export = core.db.has_flag(&sub.left_hand.name, 'x') || export_opt;
78+
let subs_elem_quoted_string =
79+
sub.left_hand.index.is_some() && sub.right_hand.text.starts_with("'");
8280

8381
if option_indicate_array {
8482
sub.quoted = false;

src/core/builtins/type_.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ use crate::utils::{arg, file};
66
use crate::{file_check, utils, ShellCore};
77

88
fn type_no_opt_sub(core: &mut ShellCore, com: &String) -> i32 {
9-
if core.shopts.query("expand_aliases")
10-
&& core.db.has_array_value("BASH_ALIASES", com) {
9+
if core.shopts.query("expand_aliases") && core.db.has_array_value("BASH_ALIASES", com) {
1110
let alias = core.db.get_elem("BASH_ALIASES", com).unwrap();
1211
println!("{} is aliased to `{}'", &com, &alias);
1312
return 0;
@@ -37,7 +36,7 @@ fn type_no_opt_sub(core: &mut ShellCore, com: &String) -> i32 {
3736
}
3837

3938
let s = format!("{}: not found", &com);
40-
return super::error_exit(1, "type", &s, core);
39+
super::error_exit(1, "type", &s, core)
4140
}
4241

4342
fn type_no_opt(core: &mut ShellCore, args: &[String]) -> i32 {
@@ -63,8 +62,7 @@ fn type_t(core: &mut ShellCore, args: &[String]) -> i32 {
6362
}
6463

6564
fn type_t_sub(core: &mut ShellCore, com: &String) -> i32 {
66-
if core.shopts.query("expand_aliases")
67-
&& core.db.has_array_value("BASH_ALIASES", com) {
65+
if core.shopts.query("expand_aliases") && core.db.has_array_value("BASH_ALIASES", com) {
6866
println!("alias");
6967
return 0;
7068
}
@@ -119,8 +117,8 @@ fn type_p_sub(core: &mut ShellCore, com: &String) -> i32 {
119117
return 0;
120118
}
121119

122-
if let Ok(path) = core.db.get_elem("BASH_CMDS", &com) {
123-
if ! path.is_empty() {
120+
if let Ok(path) = core.db.get_elem("BASH_CMDS", com) {
121+
if !path.is_empty() {
124122
println!("{}", &path);
125123
return 0;
126124
}
@@ -155,7 +153,7 @@ fn type_large_p_sub(core: &mut ShellCore, com: &String) -> i32 {
155153
println!("{com}");
156154
return 0;
157155
}
158-
156+
159157
es
160158
}
161159

src/core/database/database_getter.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,3 @@ pub fn position_param(db: &DataBase, pos: usize) -> Result<String, ExecError> {
258258
false => Ok(String::new()),
259259
}
260260
}
261-

0 commit comments

Comments
 (0)