From e27f1c6d71d547ad539c1a9fcb42b880f6b7cf0b Mon Sep 17 00:00:00 2001 From: zhanghuanhuan Date: Mon, 14 Aug 2023 16:55:24 +0800 Subject: [PATCH] chage ptr move as offset --- bash-5.1/builtins_rust/declare/Cargo.toml | 1 + bash-5.1/builtins_rust/declare/src/lib.rs | 37 +++++---- bash-5.1/builtins_rust/setattr/Cargo.toml | 4 - bash-5.1/builtins_rust/setattr/src/lib.rs | 93 ++++++++++++----------- record.txt | 1 + utshell | 1 + 6 files changed, 71 insertions(+), 66 deletions(-) create mode 100644 utshell diff --git a/bash-5.1/builtins_rust/declare/Cargo.toml b/bash-5.1/builtins_rust/declare/Cargo.toml index 01673f7..dbe006b 100644 --- a/bash-5.1/builtins_rust/declare/Cargo.toml +++ b/bash-5.1/builtins_rust/declare/Cargo.toml @@ -11,6 +11,7 @@ libc = "*" nix = "*" rcommon = {path = "../common"} rhelp = {path ="../help"} +rsetattr = {path = "../setattr"} [lib] crate-type = ["staticlib","rlib"] diff --git a/bash-5.1/builtins_rust/declare/src/lib.rs b/bash-5.1/builtins_rust/declare/src/lib.rs index 19548a7..0fc92c7 100644 --- a/bash-5.1/builtins_rust/declare/src/lib.rs +++ b/bash-5.1/builtins_rust/declare/src/lib.rs @@ -5,7 +5,8 @@ use libc::{c_char, c_long, c_void}; use std::{ffi::CString}; use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; use rhelp::r_builtin_help; - +use rsetattr::show_name_attributes; +use std::ffi::CStr; #[repr(u8)] enum command_type { cm_for, cm_case, cm_while, cm_if, cm_simple, cm_select, cm_connection, cm_function_def, cm_until, cm_group, @@ -468,7 +469,6 @@ extern "C" { fn sh_chkwrite (ret:i32)->i32; fn show_func_attributes (name:* mut c_char, nodefs:i32)->i32; fn show_localname_attributes (name:* mut c_char, nodefs:i32)->i32; - fn show_name_attributes (name:* mut c_char, nodefs:i32)->i32; fn sh_notfound (name:* mut c_char); static assoc_expand_once:i32; fn assignment (str1:* const c_char, flags:i32)->i32; @@ -589,7 +589,7 @@ unsafe fn var_setvalue(var:*mut SHELL_VAR,str1:* mut c_char) } unsafe fn VSETATTR(var:*mut SHELL_VAR, attr:i32) { - (*var).attributes=attr; + (*var).attributes |= attr; } unsafe fn readonly_p(var:*mut SHELL_VAR) ->i32 { @@ -608,8 +608,8 @@ unsafe fn function_cell(var:*mut SHELL_VAR) ->* mut COMMAND { return (*var).value as * mut COMMAND; } -unsafe fn VUNSETATTR(var:*mut SHELL_VAR,attr:i32) ->i32 { - return (*var).attributes & !attr; +unsafe fn VUNSETATTR(var:*mut SHELL_VAR,attr:i32) { + (*var).attributes &= !attr; } unsafe fn array_p(var:*mut SHELL_VAR) ->i32 { @@ -727,7 +727,6 @@ pub extern "C" fn r_declare_internal (mut list:* mut WordList, local_var:i32)->i } opt = internal_getopt (list, tmp.as_ptr() as * mut c_char); } - list = loptend; /* If there are no more arguments left, then we just want to show some variables. */ @@ -760,12 +759,11 @@ pub extern "C" fn r_declare_internal (mut list:* mut WordList, local_var:i32)->i } else { pflag = show_name_attributes ((*(*list).word).word, nodefs); } - if pflag !=0 { sh_notfound ((*(*list).word).word); any_failed += 1; } - list = (*list).next; + list = (*list).next; } if any_failed !=0 { @@ -775,7 +773,6 @@ pub extern "C" fn r_declare_internal (mut list:* mut WordList, local_var:i32)->i } } - /* There are arguments left, so we are making variables. */ 'outter: while list !=std::ptr::null_mut() { /* declare [-aAfFirx] name [name ...] */ let mut value:* mut c_char; @@ -892,7 +889,7 @@ pub extern "C" fn r_declare_internal (mut list:* mut WordList, local_var:i32)->i shell function names don't have to be valid identifiers when the shell's not in posix mode), check whether or not the argument is a valid, well-formed shell identifier. */ - if (posixly_correct !=0 || (flags_on & att_function!()) == 0) && legal_identifier (name) == 0 { + if (posixly_correct !=0 || (flags_on & att_function!()) == 0) && legal_identifier(name) == 0 { sh_invalidid (name); assign_error+=1; libc::free (name as * mut c_void); @@ -914,16 +911,18 @@ pub extern "C" fn r_declare_internal (mut list:* mut WordList, local_var:i32)->i var = find_variable (name); if var == std::ptr::null_mut() { newname = nameref_transform_name (name, ASS_MKLOCAL!()); - } else if (flags_on & att_nameref!()) == 0 && (flags_off & att_nameref!()) == 0 { + } + else if (flags_on & att_nameref!()) == 0 && (flags_off & att_nameref!()) == 0 { /* Ok, we're following namerefs here, so let's make sure that if we followed one, it was at the same context (see below for more details). */ refvar = find_variable_last_nameref (name, 1); if refvar != std::ptr::null_mut() && (*refvar).context != variable_context { newname = name ; - } else { - newname = (*var).name; } + else { + newname = (*var).name; + } refvar = std::ptr::null_mut(); } else { newname = name; /* dealing with nameref attribute */ @@ -1015,9 +1014,9 @@ pub extern "C" fn r_declare_internal (mut list:* mut WordList, local_var:i32)->i if nodefs !=0 && debugging_mode !=0 { shell_fn = find_function_def ((*var).name); if shell_fn !=std::ptr::null_mut() { - libc::printf (CString::new("%s %d %s\n").unwrap().as_ptr(), (*var).name, (*shell_fn).line, (*shell_fn).source_file); + println!("{} {} {}",CStr::from_ptr((*var).name).to_str().unwrap(),(*shell_fn).line,CStr::from_ptr((*shell_fn).source_file).to_str().unwrap()); } else { - libc::printf (CString::new("%s\n").unwrap().as_ptr(), (*var).name); + println!("{}",CStr::from_ptr((*var).name).to_str().unwrap()); } } else { if nodefs !=0 { @@ -1025,7 +1024,7 @@ pub extern "C" fn r_declare_internal (mut list:* mut WordList, local_var:i32)->i } else { named_function_string (name, function_cell (var), FUNC_MULTILINE!()|FUNC_EXTERNAL!()); } - libc::printf (CString::new("%s\n").unwrap().as_ptr(), t); + println!("{}",CStr::from_ptr(t).to_str().unwrap()); any_failed = sh_chkwrite (any_failed); } } else { /* declare -[fF] -[rx] name [name...] */ @@ -1040,7 +1039,8 @@ pub extern "C" fn r_declare_internal (mut list:* mut WordList, local_var:i32)->i list = (*list).next; continue 'outter; } - } else { /* declare -[aAinrx] name [name...] */ + } else { + /* declare -[aAinrx] name [name...] */ /* Non-null if we just created or fetched a local variable. */ /* Here's what ksh93 seems to do as of the 2012 version: if we are @@ -1381,7 +1381,7 @@ pub extern "C" fn r_declare_internal (mut list:* mut WordList, local_var:i32)->i /* ksh93 seems to do this */ offref = flags_off & att_nameref!(); flags_off &= !att_nameref!(); - + VSETATTR (var, flags_on); VUNSETATTR (var, flags_off); @@ -1515,4 +1515,3 @@ pub extern "C" fn r_declare_internal (mut list:* mut WordList, local_var:i32)->i } } } - diff --git a/bash-5.1/builtins_rust/setattr/Cargo.toml b/bash-5.1/builtins_rust/setattr/Cargo.toml index 92f9f82..3eb0712 100644 --- a/bash-5.1/builtins_rust/setattr/Cargo.toml +++ b/bash-5.1/builtins_rust/setattr/Cargo.toml @@ -11,12 +11,8 @@ authors = ["lvgenggeng"] name = "rsetattr" crate-type = ["staticlib","rlib"] -[build-dependencies] -rdeclare = {path = "../declare"} - [dependencies] libc = "*" nix = "*" -rdeclare = {path = "../declare"} rcommon = {path = "../common"} rhelp = {path ="../help"} \ No newline at end of file diff --git a/bash-5.1/builtins_rust/setattr/src/lib.rs b/bash-5.1/builtins_rust/setattr/src/lib.rs index e6e7668..555197e 100644 --- a/bash-5.1/builtins_rust/setattr/src/lib.rs +++ b/bash-5.1/builtins_rust/setattr/src/lib.rs @@ -3,7 +3,7 @@ use std::mem::size_of_val; 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); @@ -239,7 +239,7 @@ unsafe { } loop { - var = (variable_list as usize + 8 * i) as *mut SHELL_VAR; + var = variable_list.offset(i) as *mut SHELL_VAR; if var.is_null() { break; } @@ -273,7 +273,7 @@ unsafe { } loop { - var = (variable_list as usize + 8 * i) as *mut SHELL_VAR; + var = variable_list.offset(i) as *mut SHELL_VAR; if var.is_null() { break; } @@ -298,54 +298,61 @@ unsafe { pub 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) { - libc::printf("%s\n\0".as_ptr() as *const c_char, - named_function_string((*var).name, (*var).value as *mut COMMAND, FUNC_MULTILINE | FUNC_EXTERNAL)); - + 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 { - libc::printf("declare -%s \0".as_ptr() as *const c_char, if i != 0 {flags.as_ptr()} else {"-\0".as_ptr() as *const c_char}); - } else if i != 0 { - libc::printf("%s -%s \0".as_ptr() as *const c_char, this_command_name, flags.as_ptr()); - } else { - libc::printf("%s \0".as_ptr() as *const c_char, this_command_name); + 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()); + } + 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 { - libc::printf("%s\n\0".as_ptr() as *const c_char, (*var).name); - } else if ((*var).attributes & att_array) != 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_assoc) != 0 { + } + else if ((*var).attributes & att_assoc) != 0 { print_assoc_assignment(var, 0); - } else if nodefs != 0 || - (((*var).attributes & att_function) != 0 && pattr != 0 && posixly_correct != 0) { - libc::printf("%s\n\0".as_ptr() as *const c_char, (*var).name); - } else if ((*var).attributes & att_function) != 0 { - libc::printf("%s\n\0".as_ptr() as *const c_char, - named_function_string((*var).name, (*var).value as *mut COMMAND, FUNC_MULTILINE | FUNC_EXTERNAL)); - } else if ((*var).attributes & att_invisible) != 0 && (*var).value.is_null() { - libc::printf("%s\n\0".as_ptr() as *const c_char, (*var).name); + } + 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() { + println!("{}",CStr::from_ptr((*var).name).to_str().unwrap()); } else { - let x = sh_double_quote((*var).value); - libc::printf("%s=%s\n\0".as_ptr() as *const c_char, (*var).name, x); - libc::free(x as *mut c_void); + println!("{} = {}",CStr::from_ptr((*var).name).to_str().unwrap(),CStr::from_ptr(sh_double_quote(value_cell(var))).to_str().unwrap()); } } return 0; } +unsafe fn value_cell(var:*mut SHELL_VAR)->* mut c_char +{ + return (*var).value; +} + #[no_mangle] pub extern "C" fn show_name_attributes(name: *mut c_char, nodefs: c_int) -> c_int { unsafe { @@ -473,65 +480,65 @@ pub extern "C" fn var_attribute_string(var: *mut SHELL_VAR, pattr: c_int, flags: unsafe { if pattr == 0 || posixly_correct == 0 { if ((*var).attributes & att_array) != 0 { - *((flags as usize + i) as *mut c_char) = b'a' as c_char; + *(flags.offset(i as isize)) = b'a' as c_char; i += 1; } if ((*var).attributes & att_assoc) != 0 { - *((flags as usize + i) as *mut c_char) = b'A' as c_char; + *(flags.offset(i as isize)) = b'A' as c_char; i += 1; } if ((*var).attributes & att_function) != 0 { - *((flags as usize + i) as *mut c_char) = b'f' as c_char; + *(flags.offset(i as isize)) = b'f' as c_char; i += 1; } if ((*var).attributes & att_integer) != 0 { - *((flags as usize + i) as *mut c_char) = b'i' as c_char; + *(flags.offset(i as isize)) = b'i' as c_char; i += 1; } if ((*var).attributes & att_nameref) != 0 { - *((flags as usize + i) as *mut c_char) = b'n' as c_char; + *(flags.offset(i as isize)) = b'n' as c_char; i += 1; } if ((*var).attributes & att_readonly) != 0 { - *((flags as usize + i) as *mut c_char) = b'r' as c_char; + *(flags.offset(i as isize)) = b'r' as c_char; i += 1; } if ((*var).attributes & att_trace) != 0 { - *((flags as usize + i) as *mut c_char) = b't' as c_char; + *(flags.offset(i as isize)) = b't' as c_char; i += 1; } if ((*var).attributes & att_exported) != 0 { - *((flags as usize + i) as *mut c_char) = b'x' as c_char; + *(flags.offset(i as isize)) = b'x' as c_char; i += 1; } if ((*var).attributes & att_capcase) != 0 { - *((flags as usize + i) as *mut c_char) = b'c' as c_char; + *(flags.offset(i as isize)) = b'c' as c_char; i += 1; } if ((*var).attributes & att_lowercase) != 0 { - *((flags as usize + i) as *mut c_char) = b'l' as c_char; + *(flags.offset(i as isize)) = b'l' as c_char; i += 1; } if ((*var).attributes & att_uppercase) != 0 { - *((flags as usize + i) as *mut c_char) = b'u' as c_char; + *(flags.offset(i as isize)) = b'u' as c_char; i += 1; } } else { if ((*var).attributes & att_array) != 0 { - *((flags as usize + i) as *mut c_char) = b'a' as c_char; + *(flags.offset(i as isize)) = b'a' as c_char; i += 1; } if ((*var).attributes & att_assoc) != 0 { - *((flags as usize + i) as *mut c_char) = b'A' as c_char; + *(flags.offset(i as isize)) = b'A' as c_char; i += 1; } if ((*var).attributes & att_function) != 0 { - *((flags as usize + i) as *mut c_char) = b'f' as c_char; + *(flags.offset(i as isize)) = b'f' as c_char; i += 1; } } - *((flags as usize + i) as *mut c_char) = b'\0' as c_char; + *(flags.offset(i as isize)) = b'\0' as c_char; } return i as c_int; } diff --git a/record.txt b/record.txt index f332cfe..6bdd323 100644 --- a/record.txt +++ b/record.txt @@ -88,3 +88,4 @@ 87 88 89 +90 diff --git a/utshell b/utshell new file mode 100644 index 0000000..d881b1c --- /dev/null +++ b/utshell @@ -0,0 +1 @@ +git commit -m modify project name rash- -- Gitee