diff --git a/bash-5.1/Cargo.toml b/bash-5.1/Cargo.toml index 87ccd3a0269ba34196c9eb01287cb92d0d552724..68e7298d46d9abd37ac7e11862095e6bb5c9ce0b 100644 --- a/bash-5.1/Cargo.toml +++ b/bash-5.1/Cargo.toml @@ -2,6 +2,10 @@ name = "rsbash" version = "0.1.0" edition = "2018" +build = "build.rs" + +[build-dependencies] +bindgen = "0.20.0" [lib] name = "rsbash" @@ -62,10 +66,12 @@ members=[ "builtins_rust/setattr", "builtins_rust/source", "builtins_rust/exec_cmd", + "u_src", ] [dependencies] libc = "*" + bindgen = "*" rread = {path = "./builtins_rust/read"} rhistory = {path = "./builtins_rust/history"} rkill = {path = "./builtins_rust/kill"} @@ -102,3 +108,5 @@ members=[ rwait = {path = "./builtins_rust/wait"} rdeclare = {path = "./builtins_rust/declare"} rexec_cmd = {path = "./builtins_rust/exec_cmd"} + + r_main = {path = "./u_src"} diff --git a/bash-5.1/Makefile.in b/bash-5.1/Makefile.in index dd1c8a4f2c61d16e4e293c1b55481b1bd0d2a2b2..d94c71812c2ae78cb152da43417d74758a688e9f 100644 --- a/bash-5.1/Makefile.in +++ b/bash-5.1/Makefile.in @@ -139,7 +139,7 @@ LOCALE_DEFS = -DLOCALEDIR='"$(localedir)"' -DPACKAGE='"$(PACKAGE)"' LOCAL_LIBS = @LOCAL_LIBS@ -LIBS = $(BUILTINS_LIB) $(LIBRARIES) @LIBS@ -lrt -lpthread -L./target/debug -lralias -lrbind -lrbreak -lrbuiltin -lrcaller -lrcd -lrcolon -lrcommon -lcommand -lrcomplete -lrdeclare -lrecho -lrenable -lreval -lrexec -lrexit -lrfc -lrfg_bg -lrgetopts -lrhash -lrexit -lrhelp -lrhistory -lrjobs -lrkill -lrmapfile -lrpushd -lrread -lrlet -lrreturn -lrset -lrsetattr -lrshift -lrshopt -lrsource -lrsuspend -lrtest -lrtimes -lrtrap -lrtype -lrulimit -lrumask -lrwait -lrprintf -lrexec_cmd +LIBS = $(BUILTINS_LIB) $(LIBRARIES) @LIBS@ -lrt -lpthread -L./target/debug -lralias -lrbind -lrbreak -lrbuiltin -lrcaller -lrcd -lrcolon -lrcommon -lcommand -lrcomplete -lrdeclare -lrecho -lrenable -lreval -lrexec -lrexit -lrfc -lrfg_bg -lrgetopts -lrhash -lrexit -lrhelp -lrhistory -lrjobs -lrkill -lrmapfile -lrpushd -lrread -lrlet -lrreturn -lrset -lrsetattr -lrshift -lrshopt -lrsource -lrsuspend -lrtest -lrtimes -lrtrap -lrtype -lrulimit -lrumask -lrwait -lrprintf -lrexec_cmd -lr_main LIBS_FOR_BUILD = STATIC_LD = @STATIC_LD@ @@ -571,7 +571,7 @@ LANGSRC = $(srcdir)/$(LANGSUBDIR) RUST_DIR = $(top_builddir)/builtins_rust #RUST_TARGET_LIB = $(top_builddir)/target/debug/librjobs.a $(top_builddir)/target/debug/librread.a -RUST_BUILTINS_DIRS = $(RUST_DIR)/alias $(RUST_DIR)/bind $(RUST_DIR)/break_1 $(RUST_DIR)/builtin $(RUST_DIR)/caller $(RUST_DIR)/cd $(RUST_DIR)/colon $(RUST_DIR)/command $(RUST_DIR)/common $(RUST_DIR)/complete $(RUST_DIR)/declare $(RUST_DIR)/echo $(RUST_DIR)/enable $(RUST_DIR)/eval $(RUST_DIR)/exit $(RUST_DIR)/fc $(RUST_DIR)/fg_bg $(RUST_DIR)/getopts $(RUST_DIR)/hash $(RUST_DIR)/help $(RUST_DIR)/history $(RUST_DIR)/jobs $(RUST_DIR)/kill $(RUST_DIR)/mapfile $(RUST_DIR)/printf $(RUST_DIR)/pushd $(RUST_DIR)/read $(RUST_DIR)/rlet $(RUST_DIR)/rreturn $(RUST_DIR)/set $(RUST_DIR)/setattr $(RUST_DIR)/shift $(RUST_DIR)/shopt $(RUST_DIR)/source $(RUST_DIR)/suspend $(RUST_DIR)/test $(RUST_DIR)/times $(RUST_DIR)/trap $(RUST_DIR)/type $(RUST_DIR)/ulimit $(RUST_DIR)/umask $(RUST_DIR)/wait $(RUST_DIR)/exec $(RUST_DIR)/exec_cmd +RUST_BUILTINS_DIRS = $(RUST_DIR)/alias $(RUST_DIR)/bind $(RUST_DIR)/break_1 $(RUST_DIR)/builtin $(RUST_DIR)/caller $(RUST_DIR)/cd $(RUST_DIR)/colon $(RUST_DIR)/command $(RUST_DIR)/common $(RUST_DIR)/complete $(RUST_DIR)/declare $(RUST_DIR)/echo $(RUST_DIR)/enable $(RUST_DIR)/eval $(RUST_DIR)/exit $(RUST_DIR)/fc $(RUST_DIR)/fg_bg $(RUST_DIR)/getopts $(RUST_DIR)/hash $(RUST_DIR)/help $(RUST_DIR)/history $(RUST_DIR)/jobs $(RUST_DIR)/kill $(RUST_DIR)/mapfile $(RUST_DIR)/printf $(RUST_DIR)/pushd $(RUST_DIR)/read $(RUST_DIR)/rlet $(RUST_DIR)/rreturn $(RUST_DIR)/set $(RUST_DIR)/setattr $(RUST_DIR)/shift $(RUST_DIR)/shopt $(RUST_DIR)/source $(RUST_DIR)/suspend $(RUST_DIR)/test $(RUST_DIR)/times $(RUST_DIR)/trap $(RUST_DIR)/type $(RUST_DIR)/ulimit $(RUST_DIR)/umask $(RUST_DIR)/wait $(RUST_DIR)/exec $(RUST_DIR)/exec_cmd $(RUST_DIR)/u_src # Keep GNU Make from exporting the entire environment for small machines. diff --git a/bash-5.1/builtins/exit.def b/bash-5.1/builtins/exit.def index 72d9dcb894f638186a59fd4c4348f1f7b92db8f4..d79bcd655b3eab11a0c5d70e9cd2b09bb88f1287 100644 --- a/bash-5.1/builtins/exit.def +++ b/bash-5.1/builtins/exit.def @@ -58,6 +58,7 @@ int exit_builtin (list) WORD_LIST *list; { +printf("exit_builtin\n"); r_exit_builtin(list); CHECK_HELPOPT (list); diff --git a/bash-5.1/builtins_rust/declare/src/lib.rs b/bash-5.1/builtins_rust/declare/src/lib.rs index 8f171df46219022dd71353f3c21ed42eac9e5889..dcc5beb7f57bd1e29e193a85de1bfb88aa33e0aa 100644 --- a/bash-5.1/builtins_rust/declare/src/lib.rs +++ b/bash-5.1/builtins_rust/declare/src/lib.rs @@ -5,7 +5,7 @@ use libc::{c_char, c_long, c_void}; use std::{ffi::CString}; use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE, r_savestring}; use rhelp::r_builtin_help; -use rsetattr::{show_name_attributes,set_or_show_attributes}; +use rsetattr::{show_name_attributes,set_or_show_attributes,show_all_var_attributes}; use std::ffi::CStr; #[repr(u8)] enum command_type { cm_for, cm_case, cm_while, cm_if, cm_simple, cm_select, @@ -463,7 +463,7 @@ extern "C" { fn builtin_usage(); static mut loptend:*mut WordList; fn show_local_var_attributes (v:i32, nodefs:i32)->i32; - fn show_all_var_attributes (v:i32, nodefs:i32)->i32; + // fn show_all_var_attributes (v:i32, nodefs:i32)->i32; fn set_builtin (list:*mut WordList)->i32; fn sh_chkwrite (ret:i32)->i32; fn show_func_attributes (name:* mut c_char, nodefs:i32)->i32; @@ -651,7 +651,6 @@ pub extern "C" fn r_declare_internal (mut list:* mut WordList, local_var:i32)->i let mut shell_fn:*mut function_def; refvar = std::ptr::null_mut(); - unsafe { reset_internal_getopt (); let tmp = DECLARE_OPTS(); @@ -777,7 +776,7 @@ pub extern "C" fn r_declare_internal (mut list:* mut WordList, local_var:i32)->i let wflags:i32; let mut created_var:i32; let mut namelen:i32; - let assoc_noexpand:i32; + let assoc_noexpand:bool; let mut making_array_special:i32; let mut compound_array_assign:i32; @@ -785,14 +784,14 @@ pub extern "C" fn r_declare_internal (mut list:* mut WordList, local_var:i32)->i let mut var_exists:i32; let mut array_exists:i32; let mut creating_array:i32; - let mut array_subscript_assignment:i32; + let mut array_subscript_assignment:bool; name = r_savestring ((*(*list).word).word); wflags = (*(*list).word).flags; - assoc_noexpand = (assoc_expand_once !=0 && (wflags & (1 << 2)) !=0) as i32; + assoc_noexpand = (assoc_expand_once !=0 && (wflags & (1 << 2)) !=0); // 分出= - if assoc_noexpand !=0 { + if assoc_noexpand { offset = assignment (name, 2); } else { offset = assignment (name, 0); @@ -816,15 +815,14 @@ pub extern "C" fn r_declare_internal (mut list:* mut WordList, local_var:i32)->i if offset !=0 { /* declare [-aAfFirx] name=value */ *name.offset(offset as isize) = '\0' as c_char; - value = (name as usize + (offset + 1) as usize) as * mut c_char; - if *((name as usize + (offset - 1) as usize) as * mut c_char) == '+' as c_char { + value = name.offset((offset + 1) as isize) ; + if *(name.offset((offset - 1) as isize)) == '+' as c_char { aflags |= ASS_APPEND!(); - *((name as usize + (offset - 1) as usize) as * mut c_char) = '\0' as c_char; + *(name.offset((offset - 1) as isize))= '\0' as c_char; } } else { value = tmpValue.as_ptr() as * mut c_char; } - /* Do some lexical error checking on the LHS and RHS of the assignment that is specific to nameref variables. */ if (flags_on & att_nameref!()) !=0 { @@ -861,7 +859,7 @@ pub extern "C" fn r_declare_internal (mut list:* mut WordList, local_var:i32)->i creating_array = 0; compound_array_assign = 0; simple_array_assign = 0; - array_subscript_assignment = 0; + array_subscript_assignment = false; subscript_start = std::ptr::null_mut(); t = libc::strchr (name, '[' as libc::c_int); if t !=std::ptr::null_mut() && (flags_on & att_function!()) == 0 {/* ] */ @@ -878,7 +876,7 @@ pub extern "C" fn r_declare_internal (mut list:* mut WordList, local_var:i32)->i subscript_start = t; *t = '\0' as c_char; making_array_special = 1; /* XXX - should this check offset? */ - array_subscript_assignment = (offset != 0) as i32; + array_subscript_assignment = offset != 0; } else { making_array_special = 0; } @@ -1103,7 +1101,7 @@ pub extern "C" fn r_declare_internal (mut list:* mut WordList, local_var:i32)->i var =find_variable (nameref_cell (refvar)); } } - } else if var == std::ptr::null_mut() && offset !=0 && array_subscript_assignment !=0 { + } else if var == std::ptr::null_mut() && offset !=0 && array_subscript_assignment { /* If we have an array assignment to a nameref, remove the nameref attribute and go on. */ if mkglobal !=0 { @@ -1160,24 +1158,25 @@ pub extern "C" fn r_declare_internal (mut list:* mut WordList, local_var:i32)->i libc::strcpy (name, nameref_cell (refvar)); if subscript_start != std::ptr::null_mut() { - libc::strcpy ((name as usize + (libc::strlen (nameref_cell (refvar))) as usize) as * mut c_char, subscript_start); + libc::strcpy (name.offset(libc::strlen (nameref_cell (refvar)) as isize), subscript_start); } /* We are committed to using the new name, so reset */ if offset !=0 { /* Rebuild assignment and restore offset and value */ if (aflags & ASS_APPEND!()) !=0 { - *((name as usize + namelen as usize) as * mut c_char) = '+' as c_char; + *(name.offset( namelen as isize) as * mut c_char) = '+' as c_char; + namelen+=1; } - - *((name as usize + namelen as usize) as * mut c_char) = '=' as c_char; + *(name.offset( namelen as isize) as * mut c_char) = '=' as c_char; + // *((name as usize + namelen as usize) as * mut c_char) = '=' as c_char; namelen+=1; if value != std::ptr::null_mut() && (*value) !=0 { - libc::strcpy ((name as usize + namelen as usize) as * mut c_char, value); + libc::strcpy (name.offset(namelen as isize), value); } else { - *((name as usize + namelen as usize) as * mut c_char) = '\0' as c_char; + *(name.offset( namelen as isize) as * mut c_char) = '\0' as c_char; } offset = assignment (name, 0); @@ -1192,8 +1191,9 @@ pub extern "C" fn r_declare_internal (mut list:* mut WordList, local_var:i32)->i list = (*list).next; continue 'outter; } - *((name as usize + offset as usize) as * mut c_char) = '\0' as c_char; - value = (name as usize + namelen as usize) as * mut c_char; + *(name.offset(offset as isize)) = '\0' as c_char; + + value = name.offset(namelen as isize) ; } libc::free (oldname as * mut c_void); @@ -1303,13 +1303,13 @@ pub extern "C" fn r_declare_internal (mut list:* mut WordList, local_var:i32)->i /* make declare a[2]=foo as similar to a[2]=foo as possible if a is already an array or assoc variable. */ - if array_subscript_assignment !=0 && array_exists !=0 && creating_array == 0 { + if array_subscript_assignment && array_exists !=0 && creating_array == 0 { simple_array_assign = 1; } else if (making_array_special !=0 || creating_array !=0 || array_exists !=0) && offset!=0 { let mut vlen:i32; vlen = libc::strlen (value) as i32; /*itrace("declare_builtin: name = %s value = %s flags = %d", name, value, wflags);*/ - if shell_compatibility_level > 43 && (wflags & W_COMPASSIGN!()) == 0 && *value == '(' as c_char && *((value as usize + (vlen-1) as usize) as * mut c_char) == ')' as c_char { + if shell_compatibility_level > 43 && (wflags & W_COMPASSIGN!()) == 0 && *value == '(' as c_char && *(value.offset((vlen-1) as isize) as * mut c_char) == ')' as c_char { /* The warning is only printed when using compound assignment to an array variable that doesn't already exist. We use creating_array to allow things like @@ -1319,7 +1319,7 @@ pub extern "C" fn r_declare_internal (mut list:* mut WordList, local_var:i32)->i } compound_array_assign = (array_exists !=0 || creating_array !=0) as i32; simple_array_assign = making_array_special; - } else if *value == '(' as c_char && *((value as usize + (vlen-1) as usize) as * mut c_char) == ')' as c_char && (shell_compatibility_level < 44 || (wflags & W_COMPASSIGN!()) !=0 ) { + } else if *value == '(' as c_char && *(value.offset((vlen-1) as isize) as * mut c_char) == ')' as c_char && (shell_compatibility_level < 44 || (wflags & W_COMPASSIGN!()) !=0 ) { compound_array_assign = 1; } else { simple_array_assign = 1; @@ -1390,7 +1390,7 @@ pub extern "C" fn r_declare_internal (mut list:* mut WordList, local_var:i32)->i *subscript_start = '[' as c_char; /* ] */ /* XXX - problem here with appending */ local_aflags = aflags&ASS_APPEND!(); - if assoc_noexpand !=0 { + if assoc_noexpand { local_aflags |= ASS_NOEXPAND!(); } else { local_aflags |= 0; diff --git a/bash-5.1/builtins_rust/help/src/lib.rs b/bash-5.1/builtins_rust/help/src/lib.rs index 99e69f84ff2a4b46903e2a85c70206126439d5ef..9647bbc07bae4084169f06a59ca11759efde72eb 100644 --- a/bash-5.1/builtins_rust/help/src/lib.rs +++ b/bash-5.1/builtins_rust/help/src/lib.rs @@ -353,7 +353,7 @@ fn show_helpsynopsis( i : i32) let mut pattern = value.value().expect("partern err"); let mut errors = vec![]; let mut msg1 = bundle.format_pattern(&pattern, Some(&args), &mut errors); - println!(" {}", msg1); + println!(" {}", msg1); } fn show_desc (i :i32){ diff --git a/bash-5.1/builtins_rust/history/src/lib.rs b/bash-5.1/builtins_rust/history/src/lib.rs index 31a320575bf4f04cbf1bb7812e3cc57a6132fe08..e1d9fd7d3f02b2773b9cc6c0897124354d6c5659 100644 --- a/bash-5.1/builtins_rust/history/src/lib.rs +++ b/bash-5.1/builtins_rust/history/src/lib.rs @@ -328,10 +328,8 @@ unsafe { builtin_error( c_err.as_ptr(), (*((*list).word)).word); result = EXECUTION_FAILURE; } else { - println!("{}",CStr::from_ptr(s).to_str().unwrap()); - //println!("{}",String::from(CStr::from_ptr(s).to_str().unwrap())); - //std::io::stdout().lock().write_all(CStr::from_ptr(s).to_bytes()).unwrap(); - //libc::putchar(b'\n' as c_int); + std::io::stdout().lock().write_all(CStr::from_ptr(s).to_bytes()).unwrap(); + libc::putchar(b'\n' as c_int); } if !s.is_null() { libc::free(s as *mut c_void); diff --git a/bash-5.1/builtins_rust/setattr/Cargo.toml b/bash-5.1/builtins_rust/setattr/Cargo.toml index 3eb07121f713c44ad9c7d7241aa6e6768e07c283..29dd143a1878c81095b7926c87c770d5e1b20bd6 100644 --- a/bash-5.1/builtins_rust/setattr/Cargo.toml +++ b/bash-5.1/builtins_rust/setattr/Cargo.toml @@ -15,4 +15,5 @@ crate-type = ["staticlib","rlib"] libc = "*" nix = "*" rcommon = {path = "../common"} -rhelp = {path ="../help"} \ No newline at end of file +rhelp = {path ="../help"} +rread = {path="../read"} \ No newline at end of file diff --git a/bash-5.1/builtins_rust/setattr/src/intercdep.rs b/bash-5.1/builtins_rust/setattr/src/intercdep.rs index 8c4655937ba5aea202fd59998c0c25838c719c1a..b5322b50eb3b5ff26cb27dff279bd049ad96c78c 100644 --- a/bash-5.1/builtins_rust/setattr/src/intercdep.rs +++ b/bash-5.1/builtins_rust/setattr/src/intercdep.rs @@ -1,4 +1,5 @@ use rcommon::{WordList}; +use rread::{ARRAY}; #[repr(C)] #[derive(Copy, Clone)] pub struct word_desc { @@ -299,6 +300,22 @@ pub struct jobstats { pub j_lastasync: *mut JOB, } +#[repr(C)] +pub struct BUCKET_CONTENTS { + next:* mut BUCKET_CONTENTS, /* Link to next hashed key in this bucket. */ + key:* mut c_char, /* What we look up. */ + data:* mut c_void, /* What we really want. */ + khash:u32, /* What key hashes to */ + times_found:i32 /* Number of times this item has been found. */ +} + +#[repr(C)] +pub struct HASH_TABLE { + bucket_array:*mut * mut BUCKET_CONTENTS, /* Where the data is kept. */ + nbuckets:i32, /* How many buckets does this table have. */ + nentries:i32 /* How many entries does this table have. */ +} + pub type sh_builtin_func_t = unsafe extern "C" fn(arg1: *mut WordList) -> c_int; @@ -378,5 +395,6 @@ extern "C" { pub fn bind_variable(name: *const c_char, value: *mut c_char, flags: c_int) -> *mut SHELL_VAR; pub fn stupidly_hack_special_variables(name: *mut c_char); - + pub fn array_to_assign(a:*mut ARRAY,quote : c_int) -> *mut c_char; + pub fn assoc_to_assign(a:*mut HASH_TABLE,quote : c_int) -> *mut c_char; } diff --git a/bash-5.1/builtins_rust/setattr/src/lib.rs b/bash-5.1/builtins_rust/setattr/src/lib.rs index fb491d1918ed9c6e2b3e347c0d269dd0fe91273c..a6de56a59791e4447d621991654a03271be8e178 100644 --- a/bash-5.1/builtins_rust/setattr/src/lib.rs +++ b/bash-5.1/builtins_rust/setattr/src/lib.rs @@ -4,6 +4,7 @@ use libc::{c_int, c_uint, c_char, c_long, PT_NULL, c_void}; use rhelp::r_builtin_help; include!(concat!("intercdep.rs")); use std::ffi::CStr; + #[no_mangle] pub extern "C" fn r_export_builtin(list: *mut WordList) -> c_int { return set_or_show_attributes(list, att_exported, 0); @@ -83,7 +84,6 @@ unsafe { list = (*list).next; continue; } - assign = assignment(name, 0); aflags = 0; if assign != 0 { @@ -157,7 +157,7 @@ unsafe { set_var_attribute(name, attribute, undo); if assign != 0 { *(name.offset(assign as isize)) = b'=' as c_char; - *((name as usize + assign as usize) as *mut c_char) = b'=' as c_char; + // *((name as usize + assign as usize) as *mut c_char) = b'=' as c_char; if (aflags & ASS_APPEND) != 0 { *(name.offset((assign-1) as isize)) = b'+' as c_char; } @@ -190,15 +190,15 @@ unsafe { if !variable_list.is_null() { let mut i = 0; loop { - var = *(variable_list.offset(i)) as *mut SHELL_VAR; + var = *(variable_list.offset(i)) as *mut SHELL_VAR; if var.is_null() { break; } - if arrays_only != 0 && ((*var).attributes & att_array) == 0 { + if arrays_only != 0 && (((*var).attributes & att_array) == 0) { i += 1; continue; - } else if assoc_only != 0 && ((*var).attributes & assoc_only) == 0 { + } else if assoc_only != 0 && (((*var).attributes & att_assoc) == 0) { i += 1; continue; } @@ -211,7 +211,12 @@ unsafe { if ((*var).attributes & attribute) != 0 { let pattr = (this_shell_builtin as usize == r_readonly_builtin as usize) || (this_shell_builtin as usize == r_export_builtin as usize); - show_var_attributes(var, pattr as c_int, nodefs); + if pattr { + show_var_attributes(var, 1, nodefs); + } + else { + show_var_attributes(var, 0, nodefs); + } any_failed = sh_chkwrite(any_failed); if any_failed != 0 { break; @@ -227,31 +232,34 @@ unsafe { } #[no_mangle] -pub extern "C" fn show_all_var_attributes(list: *mut WordList, v: c_int, nodefs: c_int) -> c_int { +pub extern "C" fn show_all_var_attributes(v: c_int, nodefs: c_int) -> c_int { let mut i = 0; let mut any_failed = 0; let mut var: *mut SHELL_VAR; let mut variable_list: *mut *mut SHELL_VAR; unsafe { - variable_list = if v != 0 {all_shell_variables()} else {all_shell_functions()}; + variable_list = if v != 0 {all_shell_variables() } else {all_shell_functions()}; if variable_list.is_null() { return EXECUTION_SUCCESS; } loop { - var = variable_list.offset(i) as *mut SHELL_VAR; + var = *(variable_list.offset(i)); if var.is_null() { break; } - let pattr = (this_shell_builtin as usize == r_readonly_builtin as usize) || (this_shell_builtin as usize == r_export_builtin as usize); - show_var_attributes(var, pattr as c_int, nodefs); + if pattr { + show_var_attributes(var, 1, nodefs); + } + else { + show_var_attributes(var, 0, nodefs); + } any_failed = sh_chkwrite(any_failed); if any_failed != 0 { break; } - i += 1; } @@ -280,7 +288,12 @@ unsafe { let pattr = (this_shell_builtin as usize == r_readonly_builtin as usize) || (this_shell_builtin as usize == r_export_builtin as usize); - show_var_attributes(var, pattr as c_int, nodefs); + if pattr { + show_var_attributes(var, 1, nodefs); + } + else { + show_var_attributes(var, 0, nodefs); + } any_failed = sh_chkwrite(any_failed); if any_failed != 0 { break; @@ -295,56 +308,57 @@ unsafe { } #[no_mangle] -pub extern "C" fn show_var_attributes(var: *mut SHELL_VAR, pattr: c_int, mut nodefs: c_int) -> c_int { - +pub unsafe extern "C" fn show_var_attributes(var: *mut SHELL_VAR, pattr: c_int, mut nodefs: c_int) -> c_int { let mut flags = [0;16]; -unsafe { let i = var_attribute_string(var, pattr, flags.as_mut_ptr()); - if ((*var).attributes & att_function) != 0 && nodefs == 0 && - (pattr == 0 || posixly_correct == 0) { - println!("{}",CStr::from_ptr(named_function_string((*var).name, (*var).value as *mut COMMAND, FUNC_MULTILINE | FUNC_EXTERNAL)).to_str().unwrap()); + if ((*var).attributes & att_function) != 0 && nodefs == 0 && (pattr == 0 || posixly_correct == 0) { + println!("{}",CStr::from_ptr(named_function_string((*var).name, (*var).value as *mut COMMAND, FUNC_MULTILINE | FUNC_EXTERNAL)).to_str().unwrap()); nodefs += 1; if pattr == 0 && i == 1 && flags[0] == b'f' as c_char { return 0; } } if pattr == 0 || posixly_correct == 0 { - if i !=0 { - print!("declare -{} ", CStr::from_ptr(flags.as_ptr()).to_str().unwrap()); + if i != 0 { + print!("declare -{} ",CStr::from_ptr(flags.as_ptr()).to_str().unwrap()); } else { print!("declare -- "); } } else if i != 0 { - print!("{} {}", CStr::from_ptr(this_command_name).to_str().unwrap(),CStr::from_ptr(flags.as_ptr()).to_str().unwrap()); + print!("{} -{} ",CStr::from_ptr(this_command_name).to_str().unwrap(),CStr::from_ptr(flags.as_ptr()).to_str().unwrap()); } - else { - print!("{}", CStr::from_ptr(this_command_name).to_str().unwrap()); + else { + print!("{} ",CStr::from_ptr(this_command_name).to_str().unwrap()); } if ((*var).attributes & att_invisible) != 0 && - ((*var).attributes & att_array) != 0 && - ((*var).attributes & att_assoc) != 0 { + (((*var).attributes & att_array) != 0 || + ((*var).attributes & att_assoc) != 0 ){ println!("{}",CStr::from_ptr((*var).name).to_str().unwrap()); } - else if ((*var).attributes & att_array) != 0 { - print_array_assignment(var, 0); + else if ((*var).attributes & att_array) != 0 { + r_print_array_assignment(var, 0); } else if ((*var).attributes & att_assoc) != 0 { - print_assoc_assignment(var, 0); + r_print_assoc_assignment(var, 0); } - else if nodefs != 0 || (((*var).attributes & att_function) != 0 && pattr != 0 && posixly_correct != 0) { + else if nodefs != 0 || ((((*var).attributes & att_function) != 0 && pattr != 0 && posixly_correct != 0)) { println!("{}",CStr::from_ptr((*var).name).to_str().unwrap()); } else if ((*var).attributes & att_function) != 0 { println!("{}",CStr::from_ptr(named_function_string((*var).name, (*var).value as *mut COMMAND, FUNC_MULTILINE | FUNC_EXTERNAL)).to_str().unwrap()); - } else if ((*var).attributes & att_invisible) != 0 || (*var).value.is_null() { + } + else if ((*var).attributes & att_invisible) != 0 || (*var).value == std::ptr::null_mut() { println!("{}",CStr::from_ptr((*var).name).to_str().unwrap()); - } else { - println!("{} = {}",CStr::from_ptr((*var).name).to_str().unwrap(),CStr::from_ptr(sh_double_quote(value_cell(var))).to_str().unwrap()); } -} + else { + let x = sh_double_quote (value_cell(var)); + println!("{}={}",CStr::from_ptr((*var).name).to_str().unwrap(),CStr::from_ptr(x).to_str().unwrap()); + libc::free(x as *mut c_void); + } + return 0; } @@ -353,6 +367,16 @@ unsafe fn value_cell(var:*mut SHELL_VAR)->* mut c_char return (*var).value; } +unsafe fn array_cell(var:*mut SHELL_VAR)->* mut ARRAY +{ + return (*var).value as *mut ARRAY; +} + +unsafe fn assoc_cell(var:*mut SHELL_VAR)->* mut HASH_TABLE +{ + return (*var).value as *mut HASH_TABLE; +} + #[no_mangle] pub extern "C" fn show_name_attributes(name: *mut c_char, nodefs: c_int) -> c_int { unsafe { @@ -360,7 +384,12 @@ unsafe { if !var.is_null() { let pattr = (this_shell_builtin as usize == r_readonly_builtin as usize) || (this_shell_builtin as usize == r_export_builtin as usize); - show_var_attributes(var, pattr as c_int, nodefs); + if pattr { + show_var_attributes(var, 1, nodefs); + } + else { + show_var_attributes(var, 0, nodefs); + } return 0; } else { return 1; @@ -376,7 +405,12 @@ unsafe { if cond { let pattr = (this_shell_builtin as usize == r_readonly_builtin as usize) || (this_shell_builtin as usize == r_export_builtin as usize); - show_var_attributes(var, pattr as c_int, nodefs); + if pattr { + show_var_attributes(var, 1, nodefs); + } + else { + show_var_attributes(var, 0, nodefs); + } return 0; } else { return 1; @@ -391,7 +425,12 @@ pub extern "C" fn show_func_attributes(name: *mut c_char, nodefs: c_int) -> c_in if !var.is_null() { let pattr = (this_shell_builtin as usize == r_readonly_builtin as usize) || (this_shell_builtin as usize == r_export_builtin as usize); - show_var_attributes(var, pattr as c_int, nodefs); + if pattr { + show_var_attributes(var, 1, nodefs); + } + else { + show_var_attributes(var, 0, nodefs); + } return 0; } else { return 1; @@ -540,9 +579,45 @@ unsafe { *(flags.offset(i as isize)) = b'\0' as c_char; } - return i as c_int; + return i ; } unsafe fn cmp_two(a: usize, b: usize) -> bool { return a == b; } + +#[no_mangle] +pub unsafe extern "C" fn r_print_array_assignment(var: *mut SHELL_VAR, quote: c_int) { + let mut vstr = array_to_assign(array_cell(var) as *mut ARRAY ,quote); + + if vstr == std::ptr::null_mut() { + if quote != 0 { + println!("{}=\"\'()\'\"",CStr::from_ptr((*var).name).to_str().unwrap()); + } + else { + println!("{}=\"()\"",CStr::from_ptr((*var).name).to_str().unwrap()); + } + } + else { + println!("{}={}",CStr::from_ptr((*var).name).to_str().unwrap(),CStr::from_ptr(vstr).to_str().unwrap()); + libc::free(vstr as *mut c_void); + } +} + +#[no_mangle] +pub unsafe extern "C" fn r_print_assoc_assignment(var: *mut SHELL_VAR, quote: c_int) { + let mut vstr = assoc_to_assign(assoc_cell(var) as *mut HASH_TABLE ,quote); + + if vstr == std::ptr::null_mut() { + if quote != 0 { + println!("{}=\"\'()\'\"",CStr::from_ptr((*var).name).to_str().unwrap()); + } + else { + println!("{}=\"()\"",CStr::from_ptr((*var).name).to_str().unwrap()); + } + } + else { + println!("{}={}",CStr::from_ptr((*var).name).to_str().unwrap(),CStr::from_ptr(vstr).to_str().unwrap()); + libc::free(vstr as *mut c_void); + } +} diff --git a/bash-5.1/parse.y b/bash-5.1/parse.y index ffda9f8918d9b6db6dee930d753e88159230ca79..2449fa80c860a3832f28c10d0298cbe88d56a47c 100644 --- a/bash-5.1/parse.y +++ b/bash-5.1/parse.y @@ -323,7 +323,6 @@ static REDIRECTEE redir; static FILE *yyoutstream; static FILE *yyerrstream; -extern int r_exit_builtin ( WORD_LIST *list); %} %union { @@ -6444,8 +6443,8 @@ handle_eof_input_unit () reset_parser (); last_shell_builtin = this_shell_builtin; - this_shell_builtin = r_exit_builtin; - r_exit_builtin ((WORD_LIST *)NULL); + this_shell_builtin = exit_builtin; + exit_builtin ((WORD_LIST *)NULL); } else { diff --git a/record.txt b/record.txt index 017c4b28637e6b3231ddf01e164f6c240db271b8..6bf497d1524cc23a10434e59be289314070982ad 100644 --- a/record.txt +++ b/record.txt @@ -123,3 +123,4 @@ 122 123 124 +125