From 372584cf240b3ef7042cbe90c21322af214b8853 Mon Sep 17 00:00:00 2001 From: liutong Date: Thu, 10 Aug 2023 23:31:44 +0800 Subject: [PATCH 1/2] modify command.rs file --- bash-5.1/Cargo.toml | 31 ++++++++++---------- bash-5.1/builtins_rust/alias/Cargo.toml | 1 - bash-5.1/builtins_rust/caller/Cargo.toml | 2 +- bash-5.1/builtins_rust/command/src/lib.rs | 3 +- bash-5.1/builtins_rust/hash/Cargo.toml | 2 +- bash-5.1/builtins_rust/read/Cargo.toml | 2 +- bash-5.1/builtins_rust/read/src/intercdep.rs | 7 +++-- bash-5.1/builtins_rust/read/src/lib.rs | 2 +- bash-5.1/builtins_rust/set/Cargo.toml | 4 ++- bash-5.1/builtins_rust/wait/Cargo.toml | 2 +- record.txt | 1 + 11 files changed, 32 insertions(+), 25 deletions(-) diff --git a/bash-5.1/Cargo.toml b/bash-5.1/Cargo.toml index d98e545..595ff04 100644 --- a/bash-5.1/Cargo.toml +++ b/bash-5.1/Cargo.toml @@ -37,13 +37,12 @@ members=[ "builtins_rust/cmd", "builtins_rust/enable", "builtins_rust/declare", - "builtins_rust/setattr", "builtins_rust/command", "builtins_rust/hash", "builtins_rust/break_1", "builtins_rust/type", "builtins_rust/ulimit", - "builtins_rust/set" + "builtins_rust/set", "builtins_rust/bind", "builtins_rust/echo", "builtins_rust/hash", @@ -61,34 +60,36 @@ members=[ "builtins_rust/times", "builtins_rust/trap", "builtins_rust/setattr", + "builtins_rust/source", ] [dependencies] libc = "0.2" - read = {path = "./builtins_rust/read"} - history = {path = "./builtins_rust/history"} + rread = {path = "./builtins_rust/read"} + rhistory = {path = "./builtins_rust/history"} rkill = {path = "./builtins_rust/kill"} rlet = {path = "./builtins_rust/rlet"} - mapfile = {path = "./builtins_rust/mapfile"} - printf = {path = "./builtins_rust/printf"} + rsource = {path = "./builtins_rust/source"} + rmapfile = {path = "./builtins_rust/mapfile"} + rprintf = {path = "./builtins_rust/printf"} rreturn = {path = "./builtins_rust/rreturn"} - shift = {path = "./builtins_rust/shift"} - times = {path = "./builtins_rust/times"} + rshift = {path = "./builtins_rust/shift"} + rtimes = {path = "./builtins_rust/times"} rsuspend = {path = "./builtins_rust/suspend"} - test = {path = "./builtins_rust/test"} - trap = {path = "./builtins_rust/trap"} - setattr = {path = "./builtins_rust/setattr"} + rtest = {path = "./builtins_rust/test"} + rtrap = {path = "./builtins_rust/trap"} + rsetattr = {path = "./builtins_rust/setattr"} rcolon = {path = "./builtins_rust/colon"} - builtin = {path = "./builtins_rust/builtin"} - alias= {path = "./builtins_rust/alias"} - exit = {path = "./builtins_rust/exit"} + rbuiltin = {path = "./builtins_rust/builtin"} + ralias= {path = "./builtins_rust/alias"} + rexit = {path = "./builtins_rust/exit"} rhelp = {path = "./builtins_rust/help"} rcaller = {path = "./builtins_rust/caller"} reval = {path = "./builtins_rust/eval"} rexec = {path = "./builtins_rust/exec"} rcommon = {path = "./builtins_rust/common"} rcmd = {path = "builtins_rust/cmd"} - enable = {path = "builtins_rust/enable"} + renable = {path = "builtins_rust/enable"} rbreak = {path = "builtins_rust/break_1"} rulimit = {path = "builtins_rust/ulimit"} rtype = {path = "builtins_rust/type"} diff --git a/bash-5.1/builtins_rust/alias/Cargo.toml b/bash-5.1/builtins_rust/alias/Cargo.toml index 9a3049b..9669a4d 100644 --- a/bash-5.1/builtins_rust/alias/Cargo.toml +++ b/bash-5.1/builtins_rust/alias/Cargo.toml @@ -14,4 +14,3 @@ libc = "0.2" rcmd = {path="../cmd"} rcommon= {path="../common"} # nix = "0.23" -rcommon = {path = "../common"} diff --git a/bash-5.1/builtins_rust/caller/Cargo.toml b/bash-5.1/builtins_rust/caller/Cargo.toml index de77b46..ef4d55d 100644 --- a/bash-5.1/builtins_rust/caller/Cargo.toml +++ b/bash-5.1/builtins_rust/caller/Cargo.toml @@ -7,7 +7,7 @@ edition = "2018" [dependencies] libc = "0.2" -read = {path="../read"} +rread = {path="../read"} [lib] crate-type = ["staticlib","lib"] diff --git a/bash-5.1/builtins_rust/command/src/lib.rs b/bash-5.1/builtins_rust/command/src/lib.rs index 25a3572..17b09ef 100644 --- a/bash-5.1/builtins_rust/command/src/lib.rs +++ b/bash-5.1/builtins_rust/command/src/lib.rs @@ -262,7 +262,8 @@ pub const CDESC_STDPATH: i32 = 0x100; pub const EX_USAGE :i32= 258; pub const EXECUTION_SUCCESS:i32 = 0; pub const EXECUTION_FAILURE:i32 = 1; -pub const const_command_builtin: &CStr = CStr::from_bytes_with_nul(b"command_builtin\0").unwrap(); + +pub const const_command_builtin:&CStr =unsafe{ CStr::from_bytes_with_nul_unchecked(b"command_builtin\0")};//.unwrap(); //#define COMMAND_BUILTIN_FLAGS (CMD_NO_FUNCTIONS | CMD_INHIBIT_EXPANSION | CMD_COMMAND_BUILTIN | (use_standard_path ? CMD_STDPATH : 0)) //#define CMD_WANT_SUBSHELL 0x01 /* User wants a subshell: ( command ) */ //#define CMD_FORCE_SUBSHELL 0x02 /* Shell needs to force a subshell. */ diff --git a/bash-5.1/builtins_rust/hash/Cargo.toml b/bash-5.1/builtins_rust/hash/Cargo.toml index 78c9205..4c1bd09 100644 --- a/bash-5.1/builtins_rust/hash/Cargo.toml +++ b/bash-5.1/builtins_rust/hash/Cargo.toml @@ -7,7 +7,7 @@ edition = "2018" [dependencies] libc = "0.2" -read = {path = "../read"} +rread = {path = "../read"} rcommon = {path = "../common"} [lib] diff --git a/bash-5.1/builtins_rust/read/Cargo.toml b/bash-5.1/builtins_rust/read/Cargo.toml index a180309..ca0e990 100644 --- a/bash-5.1/builtins_rust/read/Cargo.toml +++ b/bash-5.1/builtins_rust/read/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "read" +name = "rread" version = "0.1.0" edition = "2018" authors = ["lvgenggeng"] diff --git a/bash-5.1/builtins_rust/read/src/intercdep.rs b/bash-5.1/builtins_rust/read/src/intercdep.rs index 827ae19..231a6d9 100644 --- a/bash-5.1/builtins_rust/read/src/intercdep.rs +++ b/bash-5.1/builtins_rust/read/src/intercdep.rs @@ -15,7 +15,9 @@ pub struct word_list { } pub type WORD_LIST = word_list; -// pub type SHELL_VAR = variable; +use rcommon::{r_builtin_usage,r_sh_invalidid,r_builtin_bind_variable}; +//use rcommon::{r_builtin_usage,r_sh_invalidid,r_builtin_bind_variable,SHELL_VAR}; +pub type SHELL_VAR = rcommon::SHELL_VAR; pub type __intmax_t = c_long; pub type intmax_t = __intmax_t; @@ -98,7 +100,8 @@ pub const CTLNUL: c_char = b'\x4f' as c_char; pub const __S_IFMT: u32 = 0o0170000; pub const __S_IFREG: u32 = 0o0100000; -extern "C" { +extern "C" { + pub fn reset_internal_getopt(); pub fn internal_getopt( diff --git a/bash-5.1/builtins_rust/read/src/lib.rs b/bash-5.1/builtins_rust/read/src/lib.rs index 702836b..7a7c390 100644 --- a/bash-5.1/builtins_rust/read/src/lib.rs +++ b/bash-5.1/builtins_rust/read/src/lib.rs @@ -1,7 +1,7 @@ use libc::{c_int, c_char, c_long, c_ulong, c_uint, size_t, c_void, PT_NULL, ssize_t}; use nix::errno::errno; use std::{ffi::{CString, CStr}, ptr::null_mut}; -use rcommon::{r_builtin_usage,r_sh_invalidid,r_builtin_bind_variable,SHELL_VAR}; +//use rcommon::{r_builtin_usage,r_sh_invalidid,r_builtin_bind_variable,SHELL_VAR}; include!(concat!("intercdep.rs")); diff --git a/bash-5.1/builtins_rust/set/Cargo.toml b/bash-5.1/builtins_rust/set/Cargo.toml index ece27d8..0a60e09 100644 --- a/bash-5.1/builtins_rust/set/Cargo.toml +++ b/bash-5.1/builtins_rust/set/Cargo.toml @@ -1,4 +1,5 @@ -name = "set" +[package] +name = "rset" version = "0.1.0" edition = "2018" @@ -6,6 +7,7 @@ edition = "2018" [dependencies] libc="*" +nix="*" lazy_static = "1.4.0" [lib] diff --git a/bash-5.1/builtins_rust/wait/Cargo.toml b/bash-5.1/builtins_rust/wait/Cargo.toml index b4c5995..f0dad29 100644 --- a/bash-5.1/builtins_rust/wait/Cargo.toml +++ b/bash-5.1/builtins_rust/wait/Cargo.toml @@ -10,7 +10,7 @@ edition = "2018" libc = "0.2" nix = "0.23.0" rjobs = {path = "../jobs"} -read = {path = "../read"} +rread = {path = "../read"} rcommon = {path = "../common"} diff --git a/record.txt b/record.txt index 0a00b9c..5c5b86f 100644 --- a/record.txt +++ b/record.txt @@ -41,3 +41,4 @@ 40 41 42 +43 -- Gitee From 5acee66e8d5fdfd8650893c3df2d947d3ff2e998 Mon Sep 17 00:00:00 2001 From: liutong Date: Thu, 10 Aug 2023 23:31:55 +0800 Subject: [PATCH 2/2] fix shopt setflag error --- bash-5.1/builtins/set.def | 8 ++--- bash-5.1/builtins_rust/cd/Cargo.toml | 1 + bash-5.1/builtins_rust/set/src/lib.rs | 10 +++--- bash-5.1/builtins_rust/shopt/Cargo.toml | 1 + bash-5.1/builtins_rust/shopt/src/lib.rs | 45 ++++++++++++++----------- bash-5.1/builtins_rust/umask/Cargo.toml | 2 +- record.txt | 1 + 7 files changed, 39 insertions(+), 29 deletions(-) diff --git a/bash-5.1/builtins/set.def b/bash-5.1/builtins/set.def index b93149e..c81b8f1 100644 --- a/bash-5.1/builtins/set.def +++ b/bash-5.1/builtins/set.def @@ -309,7 +309,7 @@ list_minus_o_opts (mode, reusable) on_or_off = &value; if (mode == -1 || mode == *on_or_off) { - printf("now i is %d, value is %d, gflag is %d \n, ",i,*on_or_off , reusable ); + //printf("now i is %d, value is %d, gflag is %d \n, ",i,*on_or_off , reusable ); print_minus_o_option (o_optionsd[i].name, *on_or_off, reusable); } @@ -318,7 +318,7 @@ list_minus_o_opts (mode, reusable) value = GET_BINARY_O_OPTION_VALUE (i, o_optionsd[i].name); if (mode == -1 || mode == value) { - printf("now ==== i is %d,value is %d gflag is %d \n",i,value ,reusable); + //printf("now ==== i is %d,value is %d gflag is %d \n",i,value ,reusable); print_minus_o_option (o_optionsd[i].name, value, reusable); } } @@ -499,8 +499,8 @@ set_minus_o_option (on_or_off, option_name) register int i; i = find_minus_o_option (option_name); - printf ("option_name is %s",option_name); - printf("i is %d",i); + //printf ("option_name is %s",option_name); + //printf("i is %d",i); if (i < 0) { sh_invalidoptname (option_name); diff --git a/bash-5.1/builtins_rust/cd/Cargo.toml b/bash-5.1/builtins_rust/cd/Cargo.toml index 2212855..22ac061 100644 --- a/bash-5.1/builtins_rust/cd/Cargo.toml +++ b/bash-5.1/builtins_rust/cd/Cargo.toml @@ -3,6 +3,7 @@ authors = ["huzhengming"] name = "rcd" version = "0.0.1" edition = "2018" +build = "../build.rs" [dependencies] diff --git a/bash-5.1/builtins_rust/set/src/lib.rs b/bash-5.1/builtins_rust/set/src/lib.rs index deebcf1..74feac9 100644 --- a/bash-5.1/builtins_rust/set/src/lib.rs +++ b/bash-5.1/builtins_rust/set/src/lib.rs @@ -1279,7 +1279,7 @@ unsafe fn print_all_shell_variables (){ } } -unsafe fn set_shellopts () { +pub unsafe fn r_set_shellopts () { //println!("set shellopts by huanhuan"); let mut value : *mut i8; let mut tflag : [i8;N_O_OPTIONS!()] = [0 as i8 ;N_O_OPTIONS!()]; @@ -1391,7 +1391,7 @@ unsafe fn initialize_shell_options (no_shellopts : i32) { } /* Set up the $SHELLOPTS variable. */ - set_shellopts (); + r_set_shellopts (); } unsafe fn reset_shell_options () { @@ -1590,7 +1590,7 @@ unsafe fn reset_shell_options () { }; if r != EXECUTION_SUCCESS!() { unsafe { - set_shellopts () + r_set_shellopts () }; return (r); } @@ -1603,7 +1603,7 @@ unsafe fn reset_shell_options () { unsafe { sh_invalidopt (s.as_ptr() as *mut i8); builtin_usage (); - set_shellopts (); + r_set_shellopts (); } return EXECUTION_FAILURE!(); } @@ -1636,7 +1636,7 @@ unsafe fn reset_shell_options () { if opts_changed != 0 { unsafe { - set_shellopts(); + r_set_shellopts(); } } return rv; diff --git a/bash-5.1/builtins_rust/shopt/Cargo.toml b/bash-5.1/builtins_rust/shopt/Cargo.toml index 2591d5e..689ba72 100644 --- a/bash-5.1/builtins_rust/shopt/Cargo.toml +++ b/bash-5.1/builtins_rust/shopt/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" [dependencies] libc = "*" +rset = {path="../set"} [lib] crate-type = ["staticlib","rlib"] diff --git a/bash-5.1/builtins_rust/shopt/src/lib.rs b/bash-5.1/builtins_rust/shopt/src/lib.rs index ca2ba4b..b121fc0 100644 --- a/bash-5.1/builtins_rust/shopt/src/lib.rs +++ b/bash-5.1/builtins_rust/shopt/src/lib.rs @@ -1,4 +1,5 @@ use std::ffi::*; +use rset::r_set_shellopts; use libc::*; /* /* First, the user-visible attributes */ @@ -123,7 +124,7 @@ extern "C" { fn minus_o_option_value(_: *mut libc::c_char) -> i32; fn list_minus_o_opts(_: i32, _: i32); fn set_minus_o_option(_: i32, _: *mut libc::c_char) -> libc::c_int; - fn set_shellopts(); + //fn r_set_shellopts(); static mut print_shift_error: i32; static mut source_uses_path: i32; static mut loptend: *mut WordList; @@ -923,8 +924,8 @@ static QFLAG:i32 = 0x04; static OFLAG:i32 = 0x08; static PFLAG:i32 = 0x10; static EX_USAGE:i32 = 258; -static SETOPT:i32 = 0; -static UNSETOPT:i32 = 1; +static SETOPT:i32 = 1; +static UNSETOPT:i32 = 0; static EXECUTION_SUCCESS : i32 = 0; static EXECUTION_FAILURE :i32 = 1; @@ -937,7 +938,7 @@ pub unsafe extern "C" fn r_shopt_builtin(mut list: *mut WordList) -> i32 { let mut rval: i32 = 0; flags = 0 as i32; reset_internal_getopt(); - let psuoq = CString::new("adnpsf").expect("CString::new failed"); + let psuoq = CString::new("psuoq").expect("CString::new failed"); loop { opt = internal_getopt( list, psuoq.as_ptr() as *mut c_char); if !(opt != -(1 as i32)) { @@ -986,12 +987,12 @@ pub unsafe extern "C" fn r_shopt_builtin(mut list: *mut WordList) -> i32 { rval = 0; if flags & OFLAG != 0 && flags & (SFLAG | UFLAG) == 0 // shopt -o {//设置了o-flag,并没设s或u-flag - rval = list_shopt_o_options(list, flags); + rval = r_list_shopt_o_options(list, flags); } else if !list.is_null() && flags & OFLAG != 0 { //shopt -so args rval = set_shopt_o_options( if flags & SFLAG != 0 { '-' as i32 } else { '+' as i32 }, list, - flags & OFLAG, + flags & QFLAG, ); } else if flags & OFLAG != 0 { // shopt -so rval = list_some_o_options( @@ -1137,7 +1138,7 @@ unsafe extern "C" fn print_shopt( let msg: CString = CString::new("shopt %s %s\n").expect("CString new faild"); let s: CString = CString::new("-s").expect("CString new faild"); let u: CString = CString::new("-u").expect("CString new faild"); - let optfmt: CString = CString::new("%-15s\t\n").expect("CString new faild"); + let optfmt: CString = CString::new("%-15s\t%s\n").expect("CString new faild"); if flags & PFLAG != 0 { printf( msg.as_ptr(), @@ -1199,14 +1200,17 @@ unsafe extern "C" fn list_some_shopts( flags: i32, ) -> i32 { for item in SHOPT_VARS { - if ((flags & QFLAG) == 0 )&& mode==*item.value { + //if !item.name.is_null() { + //printf(b"===name=%s, value=%d\n\0" as *const u8 as *const libc::c_char, item.name as *const u8 as *const libc::c_char, *item.value); + //} + if ((flags & QFLAG) == 0 )&& item.value != std::ptr::null_mut() && mode==*item.value { print_shopt(item.name, *item.value, flags); } } return sh_chkwrite(EXECUTION_SUCCESS); } -unsafe extern "C" fn list_shopt_o_options( +unsafe extern "C" fn r_list_shopt_o_options( list: *mut WordList, flags: i32, ) -> i32 { @@ -1277,14 +1281,14 @@ unsafe extern "C" fn set_shopt_o_options( } l = (*l).next; } - set_shellopts(); + r_set_shellopts(); return rval; } unsafe extern "C" fn set_shellopts_after_change( option_name: *mut libc::c_char, mode: i32, ) -> i32 { - set_shellopts(); + r_set_shellopts(); return 0; } unsafe extern "C" fn shopt_set_debug_mode( @@ -1293,7 +1297,7 @@ unsafe extern "C" fn shopt_set_debug_mode( ) -> i32 { function_trace_mode = debugging_mode; error_trace_mode = function_trace_mode; - set_shellopts(); + r_set_shellopts(); if debugging_mode != 0 { init_bash_argv(); } @@ -1488,23 +1492,24 @@ pub unsafe extern "C" fn r_set_bashopts() { i = 0; vsize = 0; while !(SHOPT_VARS[i as usize].name).is_null() { - tflag[i as usize] = 0 as i32 as libc::c_char; + tflag[i as usize] = 0 as libc::c_char; if *SHOPT_VARS[i as usize].value != 0 { - vsize = vsize + strlen(SHOPT_VARS[i as usize].name) as i32; + vsize += strlen(SHOPT_VARS[i as usize].name) as i32; + vsize += 1; /* vsize = (vsize as libc::c_ulong) .wrapping_add( (strlen(SHOPT_VARS[i as usize].name)) .wrapping_add(1 as i32 as libc::c_ulong), ) as i32 as libc::c_int; - */ + */ tflag[i as usize] = 1 as i32 as libc::c_char; } i += 1; } - value = xmalloc((vsize + 1 ) as SizeT) as *mut libc::c_char; - vptr = 0 as i32; - i = vptr; + value = libc::malloc((vsize + 1 ) as usize) as *mut libc::c_char; + vptr = 0; + i = 0; while !(SHOPT_VARS[i as usize].name).is_null() { if tflag[i as usize] != 0 { strcpy(value.offset(vptr as isize), SHOPT_VARS[i as usize].name); @@ -1517,6 +1522,7 @@ pub unsafe extern "C" fn r_set_bashopts() { } i += 1; } + //printf(b"the values=%s" as *const u8 as *const libc::c_char, value); if vptr != 0 { vptr -= 1; } @@ -1539,7 +1545,8 @@ pub unsafe extern "C" fn r_set_bashopts() { { (*v).attributes &= !(0x1 as i32); } - free(value as *mut libc::c_void); + //libc::free(value ); + libc::free(value as *mut libc::c_void); } #[no_mangle] pub unsafe extern "C" fn r_parse_bashopts( value: *mut libc::c_char) { diff --git a/bash-5.1/builtins_rust/umask/Cargo.toml b/bash-5.1/builtins_rust/umask/Cargo.toml index 1270c48..3ec52c4 100644 --- a/bash-5.1/builtins_rust/umask/Cargo.toml +++ b/bash-5.1/builtins_rust/umask/Cargo.toml @@ -2,7 +2,7 @@ name = "rumask" version = "0.1.0" edition = "2018" -build = "../build.rs" +#build = "../build.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/record.txt b/record.txt index 5c5b86f..f8718de 100644 --- a/record.txt +++ b/record.txt @@ -42,3 +42,4 @@ 41 42 43 +44 -- Gitee