From cf486f7d95a7092881885cb1be6b91e2aa5ccb09 Mon Sep 17 00:00:00 2001 From: zhanghuanhuan Date: Fri, 16 Jun 2023 13:57:21 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E6=9E=9A=E4=B8=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bash-5.1/builtins_rust/common/src/lib.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/bash-5.1/builtins_rust/common/src/lib.rs b/bash-5.1/builtins_rust/common/src/lib.rs index e3e6bfd8..bd8eefea 100644 --- a/bash-5.1/builtins_rust/common/src/lib.rs +++ b/bash-5.1/builtins_rust/common/src/lib.rs @@ -519,3 +519,14 @@ macro_rules! J_JOBSTATE { (*$j).state } } + +//enum +#[repr(i8)] +#[derive(PartialEq)] +pub enum JOB_STATE { + JNONE = -1, + JRUNNING = 1, + JSTOPPED = 2, + JDEAD = 4, + JMIXED = 8 +} -- Gitee From cab8e2bd761df49150cb99912cb667a1168b402b Mon Sep 17 00:00:00 2001 From: zhanghuanhuan Date: Fri, 16 Jun 2023 14:02:36 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bash-5.1/builtins_rust/common/src/lib.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/bash-5.1/builtins_rust/common/src/lib.rs b/bash-5.1/builtins_rust/common/src/lib.rs index bd8eefea..131b671d 100644 --- a/bash-5.1/builtins_rust/common/src/lib.rs +++ b/bash-5.1/builtins_rust/common/src/lib.rs @@ -530,3 +530,10 @@ pub enum JOB_STATE { JDEAD = 4, JMIXED = 8 } + +//type +pub type sh_builtin_func_t = fn (*mut WordList)->i32; +pub type QSFUNC = unsafe extern "C" fn(*const c_void,*const c_void)->i32; +pub static EX_SUCCESS:i32 = 0; +pub static EX_USAGE:i32 = 258; +include!("./shell.rs"); -- Gitee From a48a79b35c499010d34b9daacf14ee1252d51e79 Mon Sep 17 00:00:00 2001 From: zhanghuanhuan Date: Fri, 16 Jun 2023 14:03:56 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20c=20=E6=89=A9=E5=B1=95?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bash-5.1/builtins_rust/common/src/lib.rs | 59 +++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/bash-5.1/builtins_rust/common/src/lib.rs b/bash-5.1/builtins_rust/common/src/lib.rs index 131b671d..e0ff7509 100644 --- a/bash-5.1/builtins_rust/common/src/lib.rs +++ b/bash-5.1/builtins_rust/common/src/lib.rs @@ -536,4 +536,61 @@ pub type sh_builtin_func_t = fn (*mut WordList)->i32; pub type QSFUNC = unsafe extern "C" fn(*const c_void,*const c_void)->i32; pub static EX_SUCCESS:i32 = 0; pub static EX_USAGE:i32 = 258; -include!("./shell.rs"); +include!("./shell.rs");//extern C + +extern "C"{ + static interactive_shell:i32; + static this_command_name:*mut c_char; + static mut current_builtin:*mut builtin; + static terminating_signal:c_int; + static interrupt_state:c_int; + static stdout:*mut FILE; + static mut posparam_count:i32; + static mut dollar_vars:[*mut c_char;10]; + static mut rest_of_args:*mut WordList; + static variable_context:i32; + static running_trap:i32; + static trap_saved_exit_value:i32; + static last_command_exit_value:i32; + static no_symbolic_links:i32; + // static bsah_getcwd_errstr:*const c_char; + static js:jobstats; + static jobs:*mut*mut JOB; + static assoc_expand_once:i32; + static shell_builtins:*mut builtin; + static mut num_shell_builtins:i32; + static posixly_correct:i32; + fn get_name_for_error()->*mut c_char; + fn executing_line_number()->i32; + fn top_level_cleanup(); + fn jump_to_top_level(value:i32); + fn internal_getopt(list:*mut WordList,opts:*mut c_char)->i32; + fn reset_internal_getopt(); + fn termsig_handler(sig:i32); + fn throw_to_top_level(); + fn fpurge(stream:*mut FILE) ->i32; + fn strvec_from_word_list(list:*mut WordList,alloc:i32,starting_index:i32,ip:*mut i32)->*mut *mut c_char; + fn xmalloc(n:size_t)->*mut c_void; + fn dispose_words(list:*mut WordList); + fn copy_word_list(list:*mut WordList)->*mut WordList; + fn list_length(list:*mut GENERIC_LIST)->i32; + fn invalidate_cached_quoted_dollar_at(); + fn set_builtin(list:*mut WordList)->i32; + fn legal_number(string:*mut c_char,result:*mut c_long)->i32; + fn return_builtin(list:*mut WordList)->i32; + fn getcwd(buf:*mut c_char,size:size_t)->*mut c_char; + fn internal_error(format:*const c_char,...); + fn strcasestr(s1:*const c_char,s2:*const c_char)->*mut c_char; + fn all_digits(string:*const c_char)->i32; + fn valid_array_reference(name:*const c_char,flags:i32)->i32; + fn bind_variable (name:* const c_char,value:* mut c_char,flags:i32)->* mut SHELL_VAR; + fn assign_array_element(name:*mut c_char,value:*mut c_char,flags:i32)->*mut SHELL_VAR; + fn find_variable(_:*const c_char)->*mut SHELL_VAR; + fn unbind_variable(name:*const c_char)->i32; + fn signal_name(sig:i32)->*mut c_char; + fn kill_builtin(list:*mut WordList)->i32; + fn decode_signal(string:*mut c_char,flags:i32)->i32; + fn builtin_help(); + + fn builtin_error(format:*const c_char,...); +} -- Gitee From 1f7ebc437fcc8bbeb24f6a6a94e8fe4e2692f2a5 Mon Sep 17 00:00:00 2001 From: zhanghuanhuan Date: Fri, 16 Jun 2023 14:05:23 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=87=BD=E6=95=B0=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bash-5.1/builtins_rust/common/src/lib.rs | 131 +++++++++++++++++++++++ 1 file changed, 131 insertions(+) diff --git a/bash-5.1/builtins_rust/common/src/lib.rs b/bash-5.1/builtins_rust/common/src/lib.rs index e0ff7509..24bd1c0a 100644 --- a/bash-5.1/builtins_rust/common/src/lib.rs +++ b/bash-5.1/builtins_rust/common/src/lib.rs @@ -594,3 +594,134 @@ extern "C"{ fn builtin_error(format:*const c_char,...); } + +unsafe fn ISOPTION(s:* const c_char, c:c_char)->bool +{ + // return *s == '-' as c_char && *((s as usize + 1)as * mut c_char) == c && *((s as usize + 8)as * mut c_char) != 0; + return *s == '-' as c_char && *s.offset(1) == c && *s.offset(2) != 0; +} +/* Used by some builtins and the mainline code. */ +pub static mut last_shell_builtin:*mut sh_builtin_func_t = std::ptr::null_mut(); +pub static mut this_shell_builtin:*mut sh_builtin_func_t = std::ptr::null_mut(); + +/* **************************************************************** */ +/* */ +/* Error reporting, usage, and option processing */ +/* */ +/* **************************************************************** */ +/* This is a lot like report_error (), but it is for shell builtins + instead of shell control structures, and it won't ever exit the + shell. */ +#[no_mangle] +fn r_builitin_error_prolog(){ + let name:*mut c_char; + unsafe{ + name = get_name_for_error(); + eprint!("{}: ",CStr::from_ptr(name).to_str().unwrap()); + if interactive_shell == 0{ + eprint!("line {}: ",executing_line_number()) + } + if !this_command_name.is_null() && *this_command_name!=0{ + eprint!("{}:",CStr::from_ptr(name).to_str().unwrap()); + } + } +} +//builtin_error builtin_waring是可变参函数,先跳过 +/* Print a usage summary for the currently-executing builtin command. */ +#[no_mangle] +pub extern "C" fn r_builtin_usage(){ + unsafe{ + if !this_command_name.is_null() && *this_command_name != 0{ + eprint!("{}: usage: ",CStr::from_ptr(this_command_name).to_str().unwrap()); + eprintln!("{}",CStr::from_ptr((*current_builtin).short_doc).to_str().unwrap() ); + // stderr().flush(); + } + } + +} +/* Return if LIST is NULL else barf and jump to top_level. Used by some + builtins that do not accept arguments. */ +#[no_mangle] +pub extern "C" fn r_no_args(list:*mut WordList){ + unsafe{ + if !list.is_null(){ + let c_str = CString::new("too many arguments").unwrap(); + let c_ptr = c_str.as_ptr(); + builtin_error(c_ptr); + top_level_cleanup(); + jump_to_top_level(DISCARD!()); + } + } +} +/* Check that no options were given to the currently-executing builtin, + and return 0 if there were options. */ +#[no_mangle] +pub extern "C" fn r_no_options(list:*mut WordList)->i32{ + let opt:i32; + unsafe{ + reset_internal_getopt(); + let c_str = CString::new("").unwrap(); + let c_ptr = c_str.as_ptr(); + opt = internal_getopt(list,c_ptr as *mut libc::c_char); + if opt != -1{ + if opt == GETOPT_HELP!(){ + builtin_help(); + return 2; + } + r_builtin_usage(); + return 1; + } + return 0; + } +} +#[no_mangle] +pub extern "C" fn r_sh_needarg(s:*mut c_char){ + unsafe{ + let c_str = CString::new("%s: option requires an argument").unwrap(); + let c_ptr = c_str.as_ptr(); + builtin_error(c_ptr,s); + } +} +#[no_mangle] +pub extern "C" fn r_sh_neednumarg(s:*mut c_char){ + unsafe{ + let c_str = CString::new("%s: numeric argument requited").unwrap(); + let c_ptr = c_str.as_ptr(); + builtin_error(c_ptr,s); + } +} +#[no_mangle] +pub extern "C" fn r_sh_notfound(s:*mut c_char){ + unsafe{ + let c_str = CString::new("%s: not found").unwrap(); + let c_ptr = c_str.as_ptr(); + builtin_error(c_ptr,s); + } +} +/* Function called when one of the builtin commands detects an invalid + option. */ +#[no_mangle] +pub extern "C" fn r_sh_invalidopt(s:*mut c_char){ + unsafe{ + let c_str = CString::new("%s: invalid option").unwrap(); + let c_ptr = c_str.as_ptr(); + builtin_error(c_ptr,s); + } +} +#[no_mangle] +pub extern "C" fn r_sh_invalidoptname(s:*mut c_char){ + unsafe{ + let c_str = CString::new("%s: invalid option name").unwrap(); + let c_ptr = c_str.as_ptr(); + builtin_error(c_ptr,s); + } +} +#[no_mangle] +pub extern "C" fn r_sh_invalidid(s:*mut c_char){ + unsafe{ + let c_str = CString::new("`%s': not a valid identifier").unwrap(); + let c_ptr = c_str.as_ptr(); + builtin_error(c_ptr,s); + } +} + -- Gitee From 6b928581e75b5433df520b5c6a281ab79085e7e0 Mon Sep 17 00:00:00 2001 From: zhanghuanhuan Date: Fri, 16 Jun 2023 14:07:37 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=87=BD=E6=95=B0=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bash-5.1/builtins_rust/common/src/lib.rs | 117 ++++++++++++++++++++++- 1 file changed, 116 insertions(+), 1 deletion(-) diff --git a/bash-5.1/builtins_rust/common/src/lib.rs b/bash-5.1/builtins_rust/common/src/lib.rs index 24bd1c0a..52947dda 100644 --- a/bash-5.1/builtins_rust/common/src/lib.rs +++ b/bash-5.1/builtins_rust/common/src/lib.rs @@ -723,5 +723,120 @@ pub extern "C" fn r_sh_invalidid(s:*mut c_char){ let c_ptr = c_str.as_ptr(); builtin_error(c_ptr,s); } +}#[no_mangle] +pub extern "C" fn r_sh_invalidnum(s:*mut c_char){ + unsafe{ + // let msg:*mut c_char; + let mut msg = String::new(); + let mut mag_ptr:*const c_char = std::ptr::null_mut(); + if *s == b'0' as libc::c_char && isdigit(*s.offset(1) as c_int) != 0{ + msg.push_str("invalid octal number"); + mag_ptr = msg.as_ptr() as *mut c_char; + } + else if *s == b'0' as libc::c_char && *s.offset(1) == b'x' as libc::c_char{ + msg.push_str("invalid hex number"); + mag_ptr = msg.as_ptr() as *mut c_char; + } + else { + msg.push_str("invalid number"); + mag_ptr = msg.as_ptr() as *mut c_char; + } + let c_str = CString::new("%s: %s").unwrap(); + let c_ptr = c_str.as_ptr(); + builtin_error(c_ptr,s,mag_ptr); + } +} +#[no_mangle] +pub extern "C" fn r_sh_invalidsig(s:*mut c_char){ + unsafe{ + let c_str = CString::new("%s: invalid signal specification").unwrap(); + let c_ptr = c_str.as_ptr(); + builtin_error(c_ptr,s); + } +} +#[no_mangle] +pub extern "C" fn r_sh_badpid(s:*mut c_char){ + unsafe{ + let c_str = CString::new("`%s': not a pid or valid job spec").unwrap(); + let c_ptr = c_str.as_ptr(); + builtin_error(c_ptr,s); + } +} +#[no_mangle] +pub extern "C" fn r_sh_readonly(s:*mut c_char){ + unsafe{ + let c_str = CString::new("%s: readonly variable").unwrap(); + let c_ptr = c_str.as_ptr(); + builtin_error(c_ptr,s); + } +} +#[no_mangle] +pub extern "C" fn r_sh_erange(s:*mut c_char,desc:*mut c_char){ + unsafe{ + if !s.is_null(){ + let c_str = CString::new("%s: %s out of range").unwrap(); + let c_ptr = c_str.as_ptr(); + if !desc.is_null(){ + builtin_error(c_ptr, s,desc); + } + else{ + let desc_str = CString::new("argument").unwrap(); + let desc_ptr = desc_str.as_ptr(); + builtin_error(c_ptr, s,desc_ptr); + } + } + else{ + let c_str = CString::new("%s out of range").unwrap(); + let c_ptr = c_str.as_ptr(); + let desc_str = CString::new("argument").unwrap(); + let desc_ptr = desc_str.as_ptr(); + builtin_error(c_ptr,desc_ptr) + } + } +} +#[no_mangle] +pub extern "C" fn r_sh_badjob(s:*mut c_char){ + unsafe{ + let c_str = CString::new("%s: no job control").unwrap(); + let c_ptr = c_str.as_ptr(); + builtin_error(c_ptr,s); + } +} +#[no_mangle] +pub extern "C" fn r_sh_nojobs(s:*mut c_char){ + unsafe{ + if !s.is_null(){ + let c_str = CString::new("%s: no job control").unwrap(); + let c_ptr = c_str.as_ptr(); + builtin_error(c_ptr,s); + } + else{ + let c_str = CString::new("no job control").unwrap(); + let c_ptr = c_str.as_ptr(); + builtin_error(c_ptr); + } + } +} +#[no_mangle] +pub extern "C" fn r_sh_restricted(s:*mut c_char){ + unsafe{ + if !s.is_null(){ + let c_str = CString::new("%s: restricted").unwrap(); + let c_ptr = c_str.as_ptr(); + builtin_error(c_ptr,s); + } + else{ + let c_str = CString::new("restricted").unwrap(); + let c_ptr = c_str.as_ptr(); + builtin_error(c_ptr); + } + } +} +#[no_mangle] +pub extern "C" fn r_sh_notbuiltin(s:*mut c_char){ + unsafe{ + let c_str = CString::new("%s: not a shell builtin").unwrap(); + let c_ptr = c_str.as_ptr(); + builtin_error(c_ptr,s); + } } - -- Gitee