diff --git a/bash-5.1/Cargo.toml b/bash-5.1/Cargo.toml index 424c3b2d0dfee3f3e11a5c7220b4e1700c60fb87..1d4c83a14d20d7fa0b27ee4b890d58dfbc967c0a 100644 --- a/bash-5.1/Cargo.toml +++ b/bash-5.1/Cargo.toml @@ -18,18 +18,23 @@ members=[ "builtins_rust/read", "builtins_rust/fc", "builtins_rust/getopts", + "builtins_rust/colon", + "builtins_rust/builtin", ] [dependencies] libc = "0.2" read = {path = "./builtins_rust/read"} -history = {path = "./builtins_rust/history"} -kill = {path = "./builtins_rust/kill"} -rlet = {path = "./builtins_rust/rlet"} -mapfile = {path = "./builtins_rust/mapfile"} -printf = {path = "./builtins_rust/printf"} -rreturn = {path = "./builtins_rust/rreturn"} -shift = {path = "./builtins_rust/shift"} -times = {path = "./builtins_rust/times"} -suspend = {path = "./builtins_rust/suspend"} -test = {path = "./builtins_rust/test"} +#history = {path = "./builtins_rust/history"} +#kill = {path = "./builtins_rust/kill"} +#rlet = {path = "./builtins_rust/rlet"} +#mapfile = {path = "./builtins_rust/mapfile"} +#printf = {path = "./builtins_rust/printf"} +#rreturn = {path = "./builtins_rust/rreturn"} +# shift = {path = "./builtins_rust/shift"} +#times = {path = "./builtins_rust/times"} +#suspend = {path = "./builtins_rust/suspend"} +# test = {path = "./builtins_rust/test"} +#trap = {path = "./builtins_rust/trap"} +rcolon = {path = "./builtins_rust/colon"} + builtin = {path = "./builtins_rust/builtin"} diff --git a/bash-5.1/Makefile.in b/bash-5.1/Makefile.in index 5736bc0d2af4009ed454e70efea1abd7c98bf4f5..565abd39cd47dce22b8ec2d5eb17eaea367cf4ce 100644 --- a/bash-5.1/Makefile.in +++ b/bash-5.1/Makefile.in @@ -138,7 +138,7 @@ LOCAL_DEFS = @LOCAL_DEFS@ LOCALE_DEFS = -DLOCALEDIR='"$(localedir)"' -DPACKAGE='"$(PACKAGE)"' LOCAL_LIBS = @LOCAL_LIBS@ -LIBS = $(BUILTINS_LIB) $(LIBRARIES) @LIBS@ -lrt -lpthread -L./target/debug -lrjobs -lrread -lrcd -lrfg_bg -lrfc -lrgetopts +LIBS = $(BUILTINS_LIB) $(LIBRARIES) @LIBS@ -lrt -lpthread -L./target/debug -lrjobs -lrread -lrcd -lrfg_bg -lrfc -lrgetopts -lrcolon -lrbuiltin LIBS_FOR_BUILD = @@ -574,7 +574,7 @@ OTHER_INSTALLED_DOCS = CHANGES COMPAT NEWS POSIX RBASH README LOADABLES_DIR = ${top_builddir}/examples/loadables RUST_DIR = $(top_builddir)/builtins_rust -RUST_BUILTINS_DIRS = $(RUST_DIR)/jobs $(RUST_DIR)/read $(RUST_DIR)/cd $(RUST_DIR)/fc $(RUST_DIR)/fg_bg $(RUST_DIR)/getopts +RUST_BUILTINS_DIRS = $(RUST_DIR)/jobs $(RUST_DIR)/read $(RUST_DIR)/cd $(RUST_DIR)/fc $(RUST_DIR)/fg_bg $(RUST_DIR)/getopts $(RUST_DIR)/colon $(RUST_DIR)/builtin #RUST_TARGET_LIB = $(top_builddir)/target/debug/librjobs.a $(top_builddir)/target/debug/librread.a diff --git a/bash-5.1/builtins/builtin.def b/bash-5.1/builtins/builtin.def index 74060ee09f00a4108f5fb358c7706d5a1e1083fa..c2c5d13ac5daa90da455b284cd984d39f9186f6d 100644 --- a/bash-5.1/builtins/builtin.def +++ b/bash-5.1/builtins/builtin.def @@ -47,12 +47,15 @@ $END #include "common.h" #include "bashgetopt.h" +extern int r_builtin_builtin ( WORD_LIST *list); + /* Run the command mentioned in list directly, without going through the normal alias/function/builtin/filename lookup process. */ int builtin_builtin (list) WORD_LIST *list; { +return r_builtin_builtin (list); sh_builtin_func_t *function; register char *command; diff --git a/bash-5.1/builtins/colon.def b/bash-5.1/builtins/colon.def index 6891b0799f14a74ebb195916f55c617a5632b03c..94d6e85bc339f38d249db0d8d2e982fbebe9865d 100644 --- a/bash-5.1/builtins/colon.def +++ b/bash-5.1/builtins/colon.def @@ -59,11 +59,15 @@ $END #include "../bashansi.h" #include "../shell.h" +extern r_colon_builtin(ignore); +extern r_false_builtin(ignore); /* Return a successful result. */ int colon_builtin (ignore) WORD_LIST *ignore; { + printf("colon builtin\n"); + r_colon_builtin(ignore); return (0); } @@ -72,5 +76,7 @@ int false_builtin (ignore) WORD_LIST *ignore; { + printf("in false builtin\n"); + r_false_builtin(ignore); return (1); } diff --git a/bash-5.1/builtins/read.def b/bash-5.1/builtins/read.def index f8741eb802d2885307dab6158bce28c7157dd4c2..b28ed06f60a416864445ca94643617a18b9cce69 100644 --- a/bash-5.1/builtins/read.def +++ b/bash-5.1/builtins/read.def @@ -1066,14 +1066,14 @@ ttyrestore (ttp) } void -read_tty_cleanup () +__read_tty_cleanup () { if (tty_modified) ttyrestore (&termsave); } int -read_tty_modified () +__read_tty_modified () { return (tty_modified); } diff --git a/bash-5.1/builtins_rust/trap/Cargo.toml b/bash-5.1/builtins_rust/trap/Cargo.toml index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..3845c566ecd886b34349431d343a1ca30e28b099 100644 --- a/bash-5.1/builtins_rust/trap/Cargo.toml +++ b/bash-5.1/builtins_rust/trap/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "trap" +version = "0.1.0" +edition = "2021" +authors = ["lvgenggeng"] +build = "../build.rs" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[lib] +name = "rtrap" +crate-type = ["staticlib"] + +[dependencies] +libc = "0.2" +nix = "0.23" diff --git a/bash-5.1/builtins_rust/trap/src/intercdep.rs b/bash-5.1/builtins_rust/trap/src/intercdep.rs new file mode 100644 index 0000000000000000000000000000000000000000..5b22830ad602c5f429edeafafa572f7c852c54bb --- /dev/null +++ b/bash-5.1/builtins_rust/trap/src/intercdep.rs @@ -0,0 +1,74 @@ + +#[repr(C)] +#[derive(Copy, Clone)] +pub struct word_desc { + pub word: *mut c_char, + pub flags: c_int, +} +pub type WORD_DESC = word_desc; + +#[repr(C)] +#[derive(Copy, Clone)] +pub struct word_list { + pub next: *mut word_list, + pub word: *mut WORD_DESC, +} +pub type WORD_LIST = word_list; + +pub type SigHandler = unsafe extern "C" fn(arg1: c_int); + +pub const EXECUTION_SUCCESS : c_int = 0; +pub const EXECUTION_FAILURE : c_int = 1; +pub const EX_USAGE: c_int = 258; + +pub const DSIG_SIGPREFIX: c_int = 0x01; +pub const DSIG_NOCASE: c_int = 0x02; + +pub const BASH_NSIG: c_int = (64 + 1) + 3; +pub const NO_SIG: c_int = -1; + +pub const SET: c_int = 0; +pub const REVERT: c_int = 1; +pub const IGNORE: c_int = 2; + +pub const SUBSHELL_RESETTRAP: c_int = 0x80; + +extern "C" { + pub fn reset_internal_getopt(); + pub fn internal_getopt(list: *mut WORD_LIST, opts: *mut c_char) -> c_int; + pub fn builtin_usage(); + pub fn builtin_error(format: *const c_char, ...); + + pub fn sh_chkwrite(s: c_int) -> c_int; + pub fn display_signal_list(list: *mut WORD_LIST, forcecols: c_int) -> c_int; + pub fn initialize_terminating_signals(); + pub fn get_all_original_signals(); + pub fn free_trap_strings(); + pub fn ignore_signal(sig: c_int); + pub fn set_signal(sig: c_int, s: *mut c_char); + pub fn restore_default_signal(sig: c_int); + pub fn sigint_sighandler(sig: c_int); + pub fn termsig_sighandler(sig: c_int); + pub fn set_signal_handler(arg1: c_int, arg2: *mut SigHandler) -> *mut SigHandler; + + pub fn all_digits(s: *const c_char) -> c_int; + pub fn decode_signal(s: *mut c_char, flags: c_int) -> c_int; + + pub fn signal_is_hard_ignored(sig: c_int) -> c_int; + pub fn sh_single_quote(s: *const c_char) -> *mut c_char; + pub fn signal_name(sig: c_int) -> *mut c_char; + + pub fn sh_invalidsig(s: *mut c_char); + + pub static mut list_optarg : *mut libc::c_char; + pub static mut loptend : *mut WORD_LIST; + + pub static trap_list: [*mut c_char; BASH_NSIG as usize]; + pub static posixly_correct: c_int; + pub static mut subshell_environment: c_int; + pub static interactive: c_int; + pub static interactive_shell: c_int; + pub static sourcelevel: c_int; + pub static running_trap: c_int; + pub static parse_and_execute_level: c_int; +} \ No newline at end of file diff --git a/bash-5.1/builtins_rust/trap/src/lib.rs b/bash-5.1/builtins_rust/trap/src/lib.rs index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..68ebe612e345dfe95a3b778f3a7f45c6cab1e5dc 100644 --- a/bash-5.1/builtins_rust/trap/src/lib.rs +++ b/bash-5.1/builtins_rust/trap/src/lib.rs @@ -0,0 +1,178 @@ +use std::{ffi::CString}; + +use libc::{c_int, c_char, c_void, PT_NULL}; + +include!(concat!("intercdep.rs")); + + +#[no_mangle] +pub extern "C" fn r_trap_builtin(mut list: *mut WORD_LIST) -> i32 { + println!("r_trap_builtin call"); + + let mut list_signal_names: c_int = 0; + let mut display: c_int = 0; + let mut result: c_int = EXECUTION_SUCCESS; + +unsafe { + reset_internal_getopt(); + let opt_str = CString::new("lp").unwrap(); + let mut opt = internal_getopt (list, opt_str.as_ptr() as * mut c_char); + while opt != -1 { + let opt_char:char=char::from(opt as u8); + match opt_char { + 'l' => list_signal_names += 1, + 'p' => display += 1, + _ => { + builtin_usage (); + return EX_USAGE; + } + } + opt = internal_getopt (list, opt_str.as_ptr() as * mut c_char); + } + list = loptend; + + opt = DSIG_NOCASE | DSIG_SIGPREFIX; + + if list_signal_names != 0 { + return sh_chkwrite(display_signal_list(PT_NULL as *mut WORD_LIST, 1)); + } else if display != 0 || list.is_null() { + initialize_terminating_signals(); + get_all_original_signals(); + return sh_chkwrite(display_traps(list, (display != 0 && posixly_correct != 0) as c_int)); + } else { + let mut operation = SET; + let first_arg = (*(*list).word).word; + let first_signal = !first_arg.is_null() && *first_arg != 0 && + all_digits(first_arg) != 0 && decode_signal (first_arg,opt) != NO_SIG; + if first_signal { + operation = REVERT; + } else if posixly_correct == 0 && !first_arg.is_null() && *first_arg != 0 && + (*first_arg != b'-' as c_char || *((first_arg as usize + 1) as *mut c_char) != 0 ) && + decode_signal (first_arg,opt) != NO_SIG && (*list).next.is_null() { + operation = REVERT; + } else { + list = (*list).next; + if list.is_null() { + builtin_usage(); + return EX_USAGE; + } else if *first_arg == b'\0' as c_char { + operation = IGNORE; + } else if *first_arg == b'-' as c_char && *((first_arg as usize + 1) as *mut c_char) == 0 { + operation = REVERT; + } + } + + if subshell_environment & SUBSHELL_RESETTRAP != 0 { + free_trap_strings(); + subshell_environment &= !SUBSHELL_RESETTRAP; + } + + let mut sig: c_int; + while !list.is_null() { + sig = decode_signal((*(*list).word).word, opt); + if sig == NO_SIG { + sh_invalidsig((*(*list).word).word); + result = EXECUTION_FAILURE; + } else { + match operation { + SET => set_signal(sig, first_arg), + IGNORE => ignore_signal(sig), + REVERT => { + restore_default_signal(sig); + match sig { + libc::SIGINT => { + if interactive != 0 { + set_signal_handler(libc::SIGINT, sigint_sighandler as *mut SigHandler); + } else if interactive_shell != 0 && + (sourcelevel != 0 || running_trap != 0 || parse_and_execute_level != 0) { + set_signal_handler(libc::SIGINT, sigint_sighandler as *mut SigHandler); + } else { + set_signal_handler(libc::SIGINT, termsig_sighandler as *mut SigHandler); + } + } + libc::SIGQUIT => { + set_signal_handler(libc::SIGQUIT, std::mem::transmute(1_usize)); + } + libc::SIGTERM | libc::SIGTTIN | libc::SIGTTOU | libc::SIGTSTP => { + if interactive != 0 { + set_signal_handler(sig, std::mem::transmute(1_usize)); + } + } + _ => (), + } + break; + } + _ => (), + } + } + + list = (*list).next; + } + } +} + return result; +} + +unsafe fn showtrap(i: c_int, show_default: c_int) +{ + let t: *mut c_char; + + let p = trap_list[i as usize]; + if (p == libc::SIG_DFL as *mut c_char) && signal_is_hard_ignored(i) == 0 { + if show_default != 0 { + t = "-\0".as_ptr() as *mut c_char; + } else { + return; + } + } else if signal_is_hard_ignored(i) != 0 { + t = PT_NULL as *mut c_char; + } else { + t = if p == libc::SIG_IGN as *mut c_char {PT_NULL as *mut c_char} else {sh_single_quote(p)} + } + + let sn = signal_name(i); + if libc::strncmp(sn, "SIGJUNK\0".as_ptr() as *const c_char, 7) == 0 || + libc::strncmp(sn, "unknown\0".as_ptr() as *const c_char, 7) == 0 { + libc::printf("trap -- %s %d\n\0".as_ptr() as *const c_char, if t.is_null() {"''\0".as_ptr() as *mut c_char} else {t}, i); + } else if posixly_correct != 0 { + if libc::strncmp(sn, "SIG\0".as_ptr() as *const c_char, 3) == 0 { + libc::printf("trap -- %s %s\n\0".as_ptr() as *const c_char, if t.is_null() {"''\0".as_ptr() as *mut c_char} else {t}, (sn as usize + 3) as *mut c_char); + } else { + libc::printf("trap -- %s %s\n\0".as_ptr() as *const c_char, if t.is_null() {"''\0".as_ptr() as *mut c_char} else {t}, sn); + } + } else { + libc::printf("trap -- %s %s\n\0".as_ptr() as *const c_char, if t.is_null() {"''\0".as_ptr() as *mut c_char} else {t}, sn); + } + + if show_default == 0 { + if !t.is_null() { + libc::free(t as *mut c_void); + } + } +} + +unsafe fn display_traps(mut list: *mut WORD_LIST, show_all: c_int) -> c_int +{ + if list.is_null() { + for i in 0..BASH_NSIG { + showtrap(i, show_all); + } + return EXECUTION_SUCCESS; + } + + let mut result = EXECUTION_SUCCESS; + let mut i: c_int; + while !list.is_null() { + i = decode_signal((*(*list).word).word, DSIG_NOCASE | DSIG_SIGPREFIX); + if i == NO_SIG { + sh_invalidsig((*(*list).word).word); + result = EXECUTION_FAILURE; + } else { + showtrap(i, show_all); + } + + list = (*list).next; + } + + return result; +} diff --git a/record.txt b/record.txt index 77ef57d086655654fd680002e2cbd42680b4f346..ba381795feb557ff10abc0395b8a63e6d7d63a3b 100644 --- a/record.txt +++ b/record.txt @@ -6,3 +6,4 @@ 28 29 30 +31