diff --git a/bash-5.1/Makefile.in b/bash-5.1/Makefile.in index 7607ed84ddc3bb3c5687f4b0246d1bad07e0dd5b..30ddc175d57f4d239b2a20ebc0b3a59a6a075872 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 -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 -lrcommand -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_FOR_BUILD = STATIC_LD = @STATIC_LD@ diff --git a/bash-5.1/builtins/command.def b/bash-5.1/builtins/command.def index acd46cc1be0062d1ca15777821e1b309645bdb3e..f538122dabbb1c70c075b8c6d0644c0c6aaddd1d 100644 --- a/bash-5.1/builtins/command.def +++ b/bash-5.1/builtins/command.def @@ -60,12 +60,14 @@ $END extern size_t confstr PARAMS((int, char *, size_t)); #endif +int r_command_builtin ( WORD_LIST *list); /* Run the commands mentioned in LIST without paying attention to shell functions. */ int command_builtin (list) WORD_LIST *list; { +return r_command_builtin ( list); int result, verbose, use_standard_path, opt; COMMAND *command; diff --git a/bash-5.1/builtins/complete.def b/bash-5.1/builtins/complete.def index 28a9ec2f9a5ee9869056016a3a1543fed025f293..973341975268e40afc0c211c6e9f33635861708d 100644 --- a/bash-5.1/builtins/complete.def +++ b/bash-5.1/builtins/complete.def @@ -365,11 +365,13 @@ build_actions (list, flagp, actp, optp) return (opt_given ? EXECUTION_SUCCESS : EXECUTION_FAILURE); } +int r_complete_builtin ( WORD_LIST *list); /* Add, remove, and display completion specifiers. */ int complete_builtin (list) WORD_LIST *list; { +return r_complete_builtin (list); int opt_given, rval; unsigned long acts, copts; COMPSPEC *cs; diff --git a/bash-5.1/builtins/declare.def b/bash-5.1/builtins/declare.def index 21e4516d194abbc5eb4b0abdd01c4f3858b300e1..aee4afb1be2a76a68d24da5835bbdc5e48802ae8 100644 --- a/bash-5.1/builtins/declare.def +++ b/bash-5.1/builtins/declare.def @@ -92,13 +92,14 @@ $END static SHELL_VAR *declare_find_variable PARAMS((const char *, int, int)); static int declare_internal PARAMS((register WORD_LIST *, int)); +int r_declare_internal PARAMS((register WORD_LIST *, int)); /* Declare or change variable attributes. */ int declare_builtin (list) register WORD_LIST *list; { - return (declare_internal (list, 0)); + return (r_declare_internal (list, 0)); } $BUILTIN local @@ -128,7 +129,7 @@ local_builtin (list) } if (variable_context) - return (declare_internal (list, 1)); + return (r_declare_internal (list, 1)); else { builtin_error (_("can only be used in a function")); diff --git a/bash-5.1/builtins/pushd.def b/bash-5.1/builtins/pushd.def index 829f827d5dc7c3d2e2cd7852e6e0b5224b9f6d49..de1bb2df1809c71e1e0636837fedfe554c52e358 100644 --- a/bash-5.1/builtins/pushd.def +++ b/bash-5.1/builtins/pushd.def @@ -169,10 +169,13 @@ static int get_dirstack_index PARAMS((intmax_t, int, int *)); #define LONGFORM 0x04 #define CLEARSTAK 0x08 +int r_pushd_builtin ( WORD_LIST *list); + int pushd_builtin (list) WORD_LIST *list; { +return r_pushd_builtin(list); WORD_LIST *orig_list; char *temp, *current_directory, *top; int j, flags, skipopt; @@ -312,13 +315,15 @@ pushd_builtin (list) } } +int +r_popd_builtin ( + WORD_LIST *list); /* Pop the directory stack, and then change to the new top of the stack. If LIST is non-null it should consist of a word +N or -N, which says what element to delete from the stack. The default is the top one. */ -int -popd_builtin (list) - WORD_LIST *list; +int popd_builtin ( WORD_LIST *list) { +return r_popd_builtin(list); register int i; intmax_t which; int flags; diff --git a/bash-5.1/builtins/source.def b/bash-5.1/builtins/source.def index 5b2f994db414d08a87cc64e5e0f706f4ee9e3d48..930d1a882a6216b1ef0f94af1e5fb68c162e5c64 100644 --- a/bash-5.1/builtins/source.def +++ b/bash-5.1/builtins/source.def @@ -108,14 +108,14 @@ maybe_pop_dollar_vars () invalidate_cached_quoted_dollar_at (); /* just invalidate to be safe */ } +int r_source_builtin ( WORD_LIST *list); /* Read and execute commands from the file passed as argument. Guess what. This cannot be done in a subshell, since things like variable assignments take place in there. So, I open the file, place it into a large string, close the file, and then execute the string. */ -int -source_builtin (list) - WORD_LIST *list; +int source_builtin ( WORD_LIST *list) { +return r_source_builtin (list); int result; char *filename, *debug_trap, *x; diff --git a/bash-5.1/builtins_rust/command/src/lib.rs b/bash-5.1/builtins_rust/command/src/lib.rs index c5d6d2b833b0b816447e2e1550e76ad921281c44..b7f593deeadd6d95c9875232fbc8a00b5c1b3500 100644 --- a/bash-5.1/builtins_rust/command/src/lib.rs +++ b/bash-5.1/builtins_rust/command/src/lib.rs @@ -284,7 +284,7 @@ pub const CMD_STDPATH :i32 = 0x4000; pub const CMD_TRY_OPTIMIZING :i32 = 0x8000; #[no_mangle] -pub unsafe extern "C" fn command_builtin(mut list: *mut WordList) -> libc::c_int { +pub unsafe extern "C" fn r_command_builtin(mut list: *mut WordList) -> libc::c_int { let mut result: libc::c_int = 0; let mut verbose: libc::c_int = 0; let mut use_standard_path: libc::c_int = 0; diff --git a/bash-5.1/builtins_rust/complete/src/lib.rs b/bash-5.1/builtins_rust/complete/src/lib.rs index cc1f283697f8dfdc6025c25244d61a686cfe1b3c..9dd58df4447e7ed94457e37ea33b672002f9442f 100644 --- a/bash-5.1/builtins_rust/complete/src/lib.rs +++ b/bash-5.1/builtins_rust/complete/src/lib.rs @@ -1376,12 +1376,3 @@ pub extern "C" fn r_compopt_builtin (listt:* mut WordList)->i32 } } -#[no_mangle] -pub extern "C" fn cmd_name() ->*const u8 { - return b"complete" as *const u8; -} - -#[no_mangle] -pub extern "C" fn run(list : *mut WordList)->i32 { - return r_complete_builtin(list); -} diff --git a/bash-5.1/builtins_rust/declare/src/lib.rs b/bash-5.1/builtins_rust/declare/src/lib.rs index 225e7aa749f6665dc27b448c512264b97b62018c..eede3a82011608e5b881dbe1b6343bb3b3550b8d 100644 --- a/bash-5.1/builtins_rust/declare/src/lib.rs +++ b/bash-5.1/builtins_rust/declare/src/lib.rs @@ -3,7 +3,6 @@ extern crate nix; use libc::{c_char, c_long, c_void}; use std::{ffi::CString}; -// use rcommon::{r_sh_notfound,r_sh_invalidopt,r_sh_invalidid,r_sh_readonly,r_sh_chkwrite,}; use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; @@ -1516,12 +1515,3 @@ pub extern "C" fn r_declare_internal (list:* mut WordList, local_var:i32)->i32 } } -#[no_mangle] -pub extern "C" fn cmd_name() ->*const u8 { - return b"declare" as *const u8; -} - -#[no_mangle] -pub extern "C" fn run(list : *mut WordList)->i32 { - return r_declare_builtin(list); -} diff --git a/bash-5.1/builtins_rust/fc/src/lib.rs b/bash-5.1/builtins_rust/fc/src/lib.rs index 47d9e0be09f818de163bc4337f35352012206e81..5d0eb3fcd91a5f7c48574b5291f41da800035aed 100644 --- a/bash-5.1/builtins_rust/fc/src/lib.rs +++ b/bash-5.1/builtins_rust/fc/src/lib.rs @@ -1,10 +1,8 @@ extern crate libc; extern crate nix; -//use rcommon::{ EX_USAGE,EXECUTION_FAILURE, EXECUTION_SUCCESS }; use libc::{c_char, c_long, c_void}; use std::{ffi::CString, i32, io::{Write, stdout}, ops::Add, string, u32}; - use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE,r_builtin_usage}; #[repr(i8)] diff --git a/bash-5.1/builtins_rust/hash/src/lib.rs b/bash-5.1/builtins_rust/hash/src/lib.rs index 88cc8f5ee83a2fa45bfd3be46b7b95d0959c188c..698a3f95897fdc18c2804461078a77a2c518f6c3 100644 --- a/bash-5.1/builtins_rust/hash/src/lib.rs +++ b/bash-5.1/builtins_rust/hash/src/lib.rs @@ -197,7 +197,6 @@ pub extern "C" fn r_hash_builtin(mut list:*mut WordList)->i32{ } } - opt = internal_getopt(list,opts.as_ptr() as *mut c_char); } @@ -210,12 +209,13 @@ pub extern "C" fn r_hash_builtin(mut list:*mut WordList)->i32{ if delete != 0{ temp = CString::new("-d").unwrap(); temp_ptr = temp.as_ptr() as *mut c_char; + sh_needarg(temp_ptr); } else{ temp = CString::new("-t").unwrap(); temp_ptr = temp.as_ptr() as *mut c_char; + sh_needarg(temp_ptr); } - sh_needarg(temp_ptr); return EXECUTION_FAILURE!(); } diff --git a/bash-5.1/builtins_rust/help/src/lib.rs b/bash-5.1/builtins_rust/help/src/lib.rs index 28321371f37d974cc1e4e65aecd2fc870da94d31..16bedff4edda98f7805d28e1811873f7391a20df 100644 --- a/bash-5.1/builtins_rust/help/src/lib.rs +++ b/bash-5.1/builtins_rust/help/src/lib.rs @@ -95,6 +95,7 @@ extern "C"{ static mut static_shell_builtin : [builtin ; 100]; static shell_builtins:*mut builtin; static mut current_builtin :*mut builtin; + fn builtin_help(); } #[no_mangle] @@ -129,9 +130,12 @@ pub extern "C" fn r_help_builtin(mut list:*mut WordList)->i32 { 's'=> {sflag = 1; break;} _=>{ unsafe { - - builtin_usage (); - return EX_USAGE; + if i == -99 { + builtin_help(); + return EX_USAGE; + } + builtin_usage (); + return EX_USAGE; } } } diff --git a/bash-5.1/builtins_rust/history/src/intercdep.rs b/bash-5.1/builtins_rust/history/src/intercdep.rs index 5ea73daf22ae470ee472a5fb7cd5bce400eef592..a1c6d3d18cb77159890474cd36359a3d467de6ad 100644 --- a/bash-5.1/builtins_rust/history/src/intercdep.rs +++ b/bash-5.1/builtins_rust/history/src/intercdep.rs @@ -31,6 +31,7 @@ pub struct _hist_entry { pub type HIST_ENTRY = _hist_entry; extern "C" { + pub fn printf(_: *const libc::c_char, _: ...) -> libc::c_int; pub fn reset_internal_getopt(); pub fn internal_getopt(list: *mut WordList, opts: *mut c_char) -> c_int; pub fn builtin_usage(); @@ -67,7 +68,7 @@ extern "C" { pub fn sh_erange(s: *mut c_char, desc: *mut c_char); pub fn sh_restricted(s: *mut c_char) -> c_void; - + fn builtin_help(); pub static mut list_optarg : *mut libc::c_char; pub static mut loptend : *mut WordList; diff --git a/bash-5.1/builtins_rust/history/src/lib.rs b/bash-5.1/builtins_rust/history/src/lib.rs index 520f0cfc91dc61513107d225ca3785c7c22a20ee..37caa34665bfa1594beba574e9bab53759899ece 100644 --- a/bash-5.1/builtins_rust/history/src/lib.rs +++ b/bash-5.1/builtins_rust/history/src/lib.rs @@ -47,6 +47,10 @@ unsafe { } 'p' => flags |= PFLAG, _ => { + if opt == -99 { + builtin_help(); + return EX_USAGE; + } r_builtin_usage (); return EX_USAGE; } @@ -55,7 +59,6 @@ unsafe { } list = loptend; - opt = flags & (AFLAG | RFLAG | WFLAG | NFLAG); if opt != 0 && opt != AFLAG && opt != RFLAG && opt != WFLAG && opt != NFLAG { let c_err = CString::new("cannot use more than one of -anrw").unwrap(); @@ -81,11 +84,15 @@ unsafe { } return r_sh_chkwrite(EXECUTION_SUCCESS); } else if (flags & DFLAG) != 0 { - let c_tmp = if *delete_arg == b'-' as c_char {(delete_arg as usize + 1) as *mut c_char} else {delete_arg}; + let c_tmp = if *delete_arg == b'-' as c_char {delete_arg.offset(1 as isize ) as *mut c_char} else {delete_arg}; range = libc::strchr(c_tmp, b'-' as c_int); + + printf(b"AAAAAAArange=%u, c_tmp=%s\n" as *const u8 as *const i8, range, c_tmp); if !range.is_null() { - let mut delete_start: c_long = 0; - let mut delete_end: c_long = 0; + printf(b"AAAAAAArange=%s\n" as *const u8 as *const i8, range); + + let mut delete_start: c_long = 0; + let mut delete_end: c_long = 0; *range = b'\0' as c_char; range = (range as usize + 1) as *mut c_char; @@ -129,8 +136,8 @@ unsafe { return if result != 0 {EXECUTION_SUCCESS} else {EXECUTION_FAILURE}; } - } else if (flags & DFLAG) != 0 { - if legal_number(delete_arg, std::mem::transmute(&delete_offset)) == 0 { + else if (flags & DFLAG) != 0 { + if legal_number(delete_arg, &mut delete_offset) == 0 { r_sh_erange(delete_arg, "history position\0".as_ptr() as *mut c_char); return EXECUTION_FAILURE; } @@ -149,13 +156,13 @@ unsafe { } else { opt = delete_offset as c_int; } - result = bash_delete_histent(opt - history_base); if where_history() > history_length { history_set_pos(history_length); } return if result != 0 {EXECUTION_FAILURE} else {EXECUTION_SUCCESS}; - } else if (flags & (AFLAG | RFLAG | NFLAG | WFLAG | CFLAG)) == 0 { + } +} else if (flags & (AFLAG | RFLAG | NFLAG | WFLAG | CFLAG)) == 0 { result = display_history(list); return r_sh_chkwrite(result); } @@ -167,7 +174,6 @@ unsafe { r_sh_restricted(filename); return EXECUTION_FAILURE; } - if (flags & AFLAG) != 0 { result = maybe_append_history(filename); } else if (flags & WFLAG) != 0 { @@ -245,37 +251,45 @@ unsafe fn display_history(list: *mut WordList) -> c_int } else { limit = -1; } - let hlist = history_list(); if !hlist.is_null() { let mut i: c_long = 0; - while !((hlist as usize + (i * 8) as usize) as *mut HIST_ENTRY).is_null() { + while !(*hlist.offset(i as isize)).is_null() { i += 1; } i = if 0 <= limit && limit < i {i - limit} else {0}; - histtimefmt = get_string_value("HISTTIMEFORMAT\0".as_ptr() as *const c_char); - - while !((hlist as usize + (i * 8) as usize) as *mut HIST_ENTRY).is_null(){ - quit(); + histtimefmt = get_string_value(b"HISTTIMEFORMAT\0" as *const u8 as *const c_char); - if !histtimefmt.is_null() && *histtimefmt != 0 { - timestr = histtime((hlist as usize + (i * 8) as usize) as *mut HIST_ENTRY, histtimefmt); - } else { - timestr = PT_NULL as *mut c_char; + while !(*hlist.offset(i as isize)).is_null() { + if terminating_signal != 0 { + termsig_handler(terminating_signal); } - - let data = (*((hlist as usize + (i * 8) as usize) as *mut HIST_ENTRY)).data; - let line = (*((hlist as usize + (i * 8) as usize) as *mut HIST_ENTRY)).line; - let s_timestr = CString::from_raw(timestr).into_string().unwrap(); - println!("{:>5}{} {}{}", - i as c_int + history_base, - if !data.is_null() {"*"} else {" "}, - if !timestr.is_null() && *timestr != 0 {s_timestr} else {"".to_owned()}, - CString::from_raw(line).into_string().unwrap() - ); + if interrupt_state != 0 { + throw_to_top_level(); + } + timestr = if !histtimefmt.is_null() && *histtimefmt as libc::c_int != 0 { + histtime(*hlist.offset(i as isize), histtimefmt) + } else { + 0 as *mut libc::c_void as *mut libc::c_char + }; + printf( + b"%5d%c %s%s\n\0" as *const u8 as *const libc::c_char, + i + history_base as c_long, + if !((**hlist.offset(i as isize)).data).is_null() { + '*' as i32 + } else { + ' ' as i32 + }, + if !timestr.is_null() && *timestr as libc::c_int != 0 { + timestr + } else { + b"\0" as *const u8 as *const libc::c_char + }, + (**hlist.offset(i as isize)).line, + ); i += 1; } } @@ -284,10 +298,12 @@ unsafe fn display_history(list: *mut WordList) -> c_int } fn push_history(list: *mut WordList) { + println!("push_history!!!!"); unsafe { if remember_on_history != 0 && hist_last_line_pushed == 0 && (hist_last_line_added != 0 || (current_command_line_count > 0 && current_command_first_line_saved != 0 && command_oriented_history != 0)) && bash_delete_last_history() == 0 { + return; } diff --git a/bash-5.1/builtins_rust/jobs/src/lib.rs b/bash-5.1/builtins_rust/jobs/src/lib.rs index ce7f97b6e41dde849a0d17a4fdb22d1fb925e85b..8f70ce26c2303b630ec61f22d3379a430c5e5365 100644 --- a/bash-5.1/builtins_rust/jobs/src/lib.rs +++ b/bash-5.1/builtins_rust/jobs/src/lib.rs @@ -601,10 +601,3 @@ pub extern "C" fn r_disown_builtin (list:* mut WordList)->libc::c_int { pub extern "C" fn cmd_name() ->*const u8 { return b"jobs" as *const u8; } -/* -#[no_mangle] - -pub extern "C" fn run(list : *mut WordList)->i32 { - return r_jobs_builtin(list); -} -*/ diff --git a/bash-5.1/builtins_rust/pushd/src/lib.rs b/bash-5.1/builtins_rust/pushd/src/lib.rs index b8bfcb04aedec608023a1f5030694ea206fbce32..08d04fa57d45a72c4b5baf5e3e48a06057567460 100644 --- a/bash-5.1/builtins_rust/pushd/src/lib.rs +++ b/bash-5.1/builtins_rust/pushd/src/lib.rs @@ -935,12 +935,3 @@ pub extern "C" fn r_get_directory_stack (flags:i32)->* mut WordList } } -#[no_mangle] -pub extern "C" fn cmd_name() ->*const u8 { - return b"pushd" as *const u8; -} - -#[no_mangle] -pub extern "C" fn run(list : *mut WordList)->i32 { - return r_pushd_builtin(list); -} diff --git a/bash-5.1/builtins_rust/read/src/intercdep.rs b/bash-5.1/builtins_rust/read/src/intercdep.rs index 60570c20b4d02c2662ee319ee1301ab1a9fe10e5..4ea450bce04cb2bc6cd064d7d8b1e1368724f725 100644 --- a/bash-5.1/builtins_rust/read/src/intercdep.rs +++ b/bash-5.1/builtins_rust/read/src/intercdep.rs @@ -1,7 +1,4 @@ -use rcommon::{WordList}; - -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}; +use rcommon::{r_builtin_usage,r_sh_invalidid,r_builtin_bind_variable,WordList,WordDesc}; pub type SHELL_VAR = rcommon::SHELL_VAR; pub type __intmax_t = c_long; @@ -118,7 +115,7 @@ extern "C" { pub static terminating_signal : c_int; pub static trapped_signal_received : c_int; - pub static mut alrmbuf: sigjmp_buf; + // pub static mut alrmbuf: sigjmp_buf; pub static mut rl_instream: *mut libc::FILE; @@ -306,3 +303,11 @@ pub struct array_element { pub prev: *mut array_element, } pub type ARRAY_ELEMENT = array_element; + + +extern "C" { + static is_basic_table:[libc::c_uint;0]; + static mut sigalrm_seen:libc::c_int; + // static mut tty_modified:libc::c_int ; + // fn is_basic(c:libc::c_char)->libc::c_int; +} diff --git a/bash-5.1/builtins_rust/read/src/lib.rs b/bash-5.1/builtins_rust/read/src/lib.rs index 720c6228a546eb29044d7565530a624a35213d65..946f8f9c41f6b3b83bc043fc308a1d005b9c07ea 100644 --- a/bash-5.1/builtins_rust/read/src/lib.rs +++ b/bash-5.1/builtins_rust/read/src/lib.rs @@ -1,13 +1,20 @@ 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 std::{ffi::{CString, CStr}, ptr::null_mut,}; //use rcommon::{r_builtin_usage,r_sh_invalidid,r_builtin_bind_variable,SHELL_VAR}; include!(concat!("intercdep.rs")); +#[no_mangle] +pub static mut alrmbuf:sigjmp_buf = [__jmp_buf_tag{ + __jmpbuf:[0;8], + __mask_was_saved:0, + __saved_mask:__sigset_t{__val:[0;16]}, +};1]; + static mut old_alrm : *mut SigHandler = PT_NULL as *mut SigHandler; -static mut sigalrm_seen : c_int = 0; +// static mut sigalrm_seen : c_int = 0; static mut reading : c_int = 0; static mut tty_modified : c_int = 0; @@ -18,16 +25,28 @@ pub struct tty_save { attrs: libc::termios, } -static mut termsave : Option = None; -static mut ptermsave: *mut c_void = PT_NULL as *mut c_void; +// static mut termsave : Option = None; +static mut termsave:tty_save = tty_save{ + fd:0, + attrs:libc::termios { + c_iflag: (0), + c_oflag: (0), + c_cflag: (0), + c_lflag: (0), + c_line: (0), + c_cc: [0;32], + c_ispeed: (0), + c_ospeed: (0) + } +}; static mut interactive : c_int = 0; static mut default_buffered_input : c_int = -1; + #[no_mangle] pub extern "C" fn r_read_builtin(mut list: *mut WordList) -> i32 { println!("r_read_builtin call"); - println!("11111111111111"); let mut varname :*mut c_char = libc::PT_NULL as *mut c_char; let mut size : c_int = 0; @@ -110,11 +129,11 @@ pub extern "C" fn r_read_builtin(mut list: *mut WordList) -> i32 { unsafe { - if termsave.is_none() { - let tmp: tty_save = std::mem::zeroed(); - termsave = Some(tmp); - } - ptermsave = std::mem::transmute(&termsave.unwrap()); + // if termsave.is_none() { + // let tmp: tty_save = std::mem::zeroed(); + // termsave = Some(tmp); + // } + // ptermsave = std::mem::transmute(&termsave.unwrap()); reset_internal_getopt(); let opt_str = CString::new("ersa:d:i:n:p:t:u:N:").unwrap(); @@ -169,9 +188,12 @@ unsafe { return EXECUTION_FAILURE; } } - 'd' => {delim = *list_optarg; - println!("-d");} + 'd' => { + delim = *list_optarg; + } + _ => { + // builtin_usage(); r_builtin_usage (); return EX_USAGE; } @@ -181,32 +203,38 @@ unsafe { list = loptend; - if have_timeout == 1 && tmsec == 0 && tmusec == 0 { + //-t + if have_timeout != 0 && tmsec == 0 && tmusec == 0 { return if input_avail(fd) != 0 {EXECUTION_SUCCESS} else {EXECUTION_FAILURE}; } vflags = if assoc_expand_once != 0 {(VA_NOEXPAND | VA_ONEWORD) as c_int} else {0}; - if !list.is_null() && legal_identifier((*(*list).word).word) == 0 && valid_array_reference((*(*list).word).word, vflags) == 0 { + // sh_invalidid((*(*list).word).word); r_sh_invalidid((*(*list).word).word); return EXECUTION_FAILURE; } - ifs_chars = getifs(); + //忽略界定符 + if ignore_delim != 0{ //-N ignore_delim = 1 + delim = -1; + } + + ifs_chars = getifs(); //ifs_chars is "\n" if ifs_chars.is_null() { ifs_chars = ifs_chars_null.as_ptr() as *mut c_char; } if ignore_delim != 0 { - delim = -1; ifs_chars = ifs_chars_null.as_ptr() as *mut c_char; } skip_ctlesc = 0; skip_ctlnul = 0; e = ifs_chars; + loop { if *e == 0 { break; @@ -228,6 +256,7 @@ unsafe { break 'out_assig_vars; } + //设置TMOUT后,TMOUT是默认读取时间 let str_val = CString::new("TMOUT").unwrap(); e = get_string_value(str_val.as_ptr()); if have_timeout == 0 && !e.is_null() { @@ -241,9 +270,10 @@ unsafe { } } - let frame_name = CString::new("read_builtin").unwrap(); + let frame_name = CString::new("r_read_builtin").unwrap(); //有没有可能是r_read_builtin? begin_unwind_frame(frame_name.as_ptr() as *mut c_char); + if interactive == 0 && default_buffered_input >= 0 && fd_is_bash_input(fd) != 0 { sync_buffered_stream(default_buffered_input); } @@ -253,6 +283,7 @@ unsafe { input_is_pipe = (libc::lseek(fd, 0, libc::SEEK_CUR) < 0 && (errno() == libc::ESPIPE)) as c_int; } + //如果设置 -p,-e,-s但输入不是终端,忽略 if (!prompt.is_null() || edit != 0 || silent != 0) && input_is_tty == 0 { itext = PT_NULL as *mut c_char; edit = 0; @@ -277,6 +308,7 @@ unsafe { if code != 0 { sigalrm_seen = 0; orig_input_string = PT_NULL as *mut c_char; + *input_string.offset(i as isize) = b'\0' as c_char; if i == 0 { t = libc::malloc(1) as *mut c_char; *t = b'\0' as c_char; @@ -284,7 +316,6 @@ unsafe { t = libc::strcpy( xmalloc( (libc::strlen(input_string) + 1) as size_t) as *mut c_char, input_string); } - run_unwind_frame(frame_name.as_ptr() as *mut c_char); input_string = t; retval = 128 + libc::SIGALRM; @@ -304,11 +335,10 @@ unsafe { add_unwind_protect(bashline_reset_event_hook as *mut c_void, PT_NULL as *mut c_char); } - falarm(tmsec, tmusec); } - if nchars > 0 || delim != b'\n' as c_char { + if nchars > 0 || delim != b'\n' as c_char { //-d -n if edit != 0 { if nchars > 0 { unwind_protect_mem(&mut rl_num_chars_to_read as *mut c_int, std::mem::size_of_val(&rl_num_chars_to_read) as c_int); @@ -319,10 +349,13 @@ unsafe { set_eol_delim(delim as c_int); add_unwind_protect(reset_eol_delim as *mut c_void, PT_NULL as *mut c_char); } - } else if input_is_tty != 0 { - termsave.unwrap().fd = fd; + } else if input_is_tty != 0 { //-d -n + // termsave.unwrap().fd = fd; + termsave.fd = fd; ttgetattr(fd, &mut ttattrs as *mut libc::termios); - termsave.unwrap().attrs = ttattrs; + // termsave.unwrap().attrs = ttattrs; + termsave.attrs = ttattrs; + ttset = ttattrs; if silent != 0 { i = ttfd_cbreak(fd, std::mem::transmute(&ttset)); @@ -334,16 +367,19 @@ unsafe { sh_ttyerror(1); } tty_modified = 1; - add_unwind_protect(ttyrestore as *mut c_void, ptermsave); + // add_unwind_protect(ttyrestore as *mut c_void, ptermsave); + add_unwind_protect(ttyrestore as *mut c_void, &mut termsave); if interactive_shell == 0 { initialize_terminating_signals(); } } - } else if silent != 0 { - termsave.unwrap().fd = fd; + } else if silent != 0 { //-s + // termsave.unwrap().fd = fd; + termsave.fd = fd; ttgetattr(fd, &mut ttattrs as *mut libc::termios); - termsave.unwrap().attrs = ttattrs; + // termsave.unwrap().attrs = ttattrs; + termsave.attrs = ttattrs; ttset = ttattrs; i = ttfd_noecho(fd, std::mem::transmute(&ttset)); @@ -352,7 +388,8 @@ unsafe { } tty_modified = 1; - add_unwind_protect(ttyrestore as *mut c_void, ptermsave); + // add_unwind_protect(ttyrestore as *mut c_void, ptermsave); + add_unwind_protect(ttyrestore as *mut c_void, &mut termsave ); if interactive_shell == 0 { initialize_terminating_signals(); } @@ -360,6 +397,7 @@ unsafe { save_instream = std::mem::zeroed(); if edit != 0 && fd != 0 { + if bash_readline_initialized == 0 { initialize_readline(); } @@ -378,8 +416,9 @@ unsafe { unbuffered_read = 1; } - if !prompt.is_null() && edit == 0 { - eprintln!("{}", CStr::from_ptr(prompt).to_str().unwrap()); + if !prompt.is_null() && edit == 0 { //-p no -e + // eprintln!("{}", CStr::from_ptr(prompt).to_str().unwrap()); + eprint!("{}", CStr::from_ptr(prompt).to_str().unwrap()); } @@ -392,7 +431,7 @@ unsafe { siglongjmp (std::mem::transmute(&alrmbuf), 1); } - if edit != 0 { + if edit != 0 { //没有设置-e edit等于0 if !rlbuf.is_null() && *((rlbuf as usize + rlind as usize) as *mut c_char) == 0 && delim != 0 { @@ -401,8 +440,14 @@ unsafe { } if rlbuf.is_null() { reading = 1; - rlbuf = if prompt.is_null() {edit_line("".as_ptr() as *mut c_char, itext)} - else {edit_line(prompt, itext)}; + rlbuf = if prompt.is_null() { + // edit_line("".as_ptr() as *mut c_char, itext)} + // let c_str = b'\0'; // b'\0'代表的是空字符串,和String::from("")不是同一个东西。 + edit_line(b'\0' as *mut c_char,itext) // b'\0'代表的是空字符串,和String::from("")不是同一个东西。 + } + else { + edit_line(prompt, itext) + }; reading = 0; rlind = 0; } @@ -425,16 +470,26 @@ unsafe { check_alrm(); *(libc::__errno_location()) = 0; if unbuffered_read == 2 { - retval = if posixly_correct != 0 {zreadintr(fd, &mut c as *mut c_char, 1) as c_int} - else {zreadn(fd, &mut c as *mut c_char, (nchars - nr) as usize) as c_int}; + retval = if posixly_correct != 0 { + zreadintr(fd, &mut c as *mut c_char, 1) as c_int + } + else { + zreadn(fd, &mut c as *mut c_char, (nchars - nr) as usize) as c_int + }; } else if unbuffered_read != 0 { - retval = if posixly_correct != 0 {zreadintr(fd, &mut c as *mut c_char, 1) as c_int} - else {zreadn(fd, &mut c as *mut c_char, 1) as c_int}; - } else { - retval = if posixly_correct != 0 {zreadcintr(fd, &mut c as *mut c_char) as c_int} - else {zreadc(fd, &mut c as *mut c_char) as c_int}; + retval = if posixly_correct != 0 { + zreadintr(fd, &mut c as *mut c_char, 1) as c_int} + else { + zread(fd, &mut c as *mut c_char, 1) as c_int}; + } + else { + retval = if posixly_correct != 0 { + zreadcintr(fd, &mut c as *mut c_char) as c_int + } + else { //-a -t + zreadc(fd, &mut c as *mut c_char) as c_int + }; } - reading = 0; if retval <= 0 { @@ -454,9 +509,9 @@ unsafe { } else { lastsig = 0; } - if terminating_signal != 0 && tty_modified != 0 { - ttyrestore(); + // ttyrestore(); + ttyrestore(&mut termsave); } check_alrm(); eof = 1; @@ -484,7 +539,7 @@ unsafe { } } 'out_add_char: loop { - if pass_next != 0 { + if pass_next != 0 { pass_next = 0; if c == b'\n' as c_char { if skip_ctlesc == 0 && i > 0 {i -= 1;} @@ -502,10 +557,11 @@ unsafe { *((input_string as usize + i as usize) as *mut c_char) = CTLESC; i += 1; } + continue 'get_input_string; } - if ignore_delim == 0 && c == delim { + if ignore_delim == 0 && c == delim { //-a break 'get_input_string; } @@ -519,12 +575,14 @@ unsafe { i += 1; } break 'out_add_char; - } + }//out_add_char + *((input_string as usize + i as usize) as *mut c_char) = c; i += 1; check_alrm(); if mb_cur_max > 1 && is_basic(c) == 0 { + *((input_string as usize + i as usize) as *mut c_char) = b'\0' as c_char; if edit != 0 { @@ -540,13 +598,13 @@ unsafe { } else if locale_utf8locale == 0 || ((c as u8 & 0x80) != 0) { i += read_mbchar(fd, input_string, i, c as c_int, unbuffered_read); } - - nr += 1; - if nchars > 0 && nr >= nchars { - break 'get_input_string; - } } - } + nr += 1; + if nchars > 0 && nr >= nchars { + break 'get_input_string; + } + + } //get_input_string *((input_string as usize + i as usize) as *mut c_char) = b'\0' as c_char; check_alrm(); @@ -572,15 +630,25 @@ unsafe { if nchars > 0 || delim != b'\n' as c_char { if edit != 0 { - - } else if input_is_tty != 0 { - ttyrestore(); + if nchars > 0{ + rl_num_chars_to_read = 0; + } + if delim != b'\n' as c_char{ + reset_eol_delim(0 as *mut c_char); + } + } + else if input_is_tty != 0 { + // ttyrestore(); + ttyrestore(&mut termsave); } - } else if silent != 0 { - ttyrestore(); + } + else if silent != 0 { + // ttyrestore(); + ttyrestore(&mut termsave); } - if unbuffered_read != 0 { + // if unbuffered_read != 0 { + if unbuffered_read == 0 { zsyncfd(fd); } @@ -593,10 +661,11 @@ unsafe { retval = if eof != 0 {EXECUTION_FAILURE} else {EXECUTION_SUCCESS}; break 'out_assig_vars; -} +} //out_assig_vars - if !arrayname.is_null() { - if legal_identifier(arrayname) == 0 { + if !arrayname.is_null() { //和-a有关 + if legal_identifier(arrayname) == 0 { //标签不符合规范 + // sh_invalidid(arrayname); r_sh_invalidid(arrayname); libc::free(input_string as *mut c_void); return EXECUTION_FAILURE; @@ -630,10 +699,11 @@ unsafe { } libc::free(input_string as *mut c_void); + return retval; } - if list.is_null() { + if list.is_null() { //和-d相关 -n 0可以退出,有显示 if saw_escape != 0 { let t = dequote_string(input_string); var = bind_variable("REPLY".as_ptr() as *const c_char, t, 0); @@ -667,6 +737,7 @@ unsafe { if legal_identifier(varname) == 0 && valid_array_reference(varname, vflags) == 0 { + // sh_invalidid(varname); r_sh_invalidid(varname); libc::free(orig_input_string as *mut c_void); return EXECUTION_FAILURE; @@ -705,6 +776,7 @@ unsafe { if legal_identifier((*((*list).word)).word) == 0 && valid_array_reference((*((*list).word)).word, vflags) == 0 { + // sh_invalidid((*((*list).word)).word); r_sh_invalidid((*((*list).word)).word); libc::free(orig_input_string as *mut c_void); return EXECUTION_FAILURE; @@ -743,21 +815,31 @@ unsafe { } libc::free(orig_input_string as *mut c_void); return retval; -} +} //unsafe } /* ---------------------------------------------------------------------------------- */ -pub fn is_basic(c: i8) -> u32 { - let is_basic_table :[c_uint; 8] = [ 0x00001a00, 0xffffffef, 0xfffffffe, 0x7ffffffe, 0,0,0,0]; +// pub fn is_basic(c: i8) -> u32 { +// let is_basic_table :[c_uint; 8] = [ 0x00001a00, 0xffffffef, 0xfffffffe, 0x7ffffffe, 0,0,0,0]; - let index = (c >> 5) as usize; - return (is_basic_table[index] >> (c & 31) ) & 1; +// let index = (c >> 5) as usize; +// return (is_basic_table[index] >> (c & 31) ) & 1; +// } + +#[inline] +unsafe extern "C" fn is_basic(mut c:libc::c_char)->libc::c_int{ + return (*is_basic_table + .as_ptr() + .offset((c as libc::c_uchar as libc::c_int>>5 as libc::c_int)as isize) + >>(c as libc::c_uchar as libc::c_int & 31 as libc::c_int) + & 1 as libc::c_int as libc::c_uint) as libc::c_int; } pub fn bind_read_variable(name: *mut c_char, value: *mut c_char) -> * mut SHELL_VAR { let v: *mut SHELL_VAR; unsafe { + // v = builtin_bind_variable(name, value, 0); v = r_builtin_bind_variable(name, value, 0); if v.is_null() { @@ -840,6 +922,7 @@ fn check_alrm() { unsafe { if sigalrm_seen != 0 { siglongjmp (std::mem::transmute(&alrmbuf), 1); + // siglongjmp (&mut alrmbuf as *mut __jmp_buf_tag, 1); } } } @@ -879,39 +962,79 @@ unsafe { return (r1 != 0 || r2 != 0) as c_int; } -fn edit_line(p : *mut c_char, itext : *mut c_char) -> *mut c_char { -unsafe { - if bash_readline_initialized == 0 { - initialize_readline(); - } +// fn edit_line(p : *mut c_char, itext : *mut c_char) -> *mut c_char { +// unsafe { +// if bash_readline_initialized == 0 { +// initialize_readline(); +// } - old_attempted_completion_function = std::mem::transmute(rl_attempted_completion_function); - rl_attempted_completion_function = std::mem::transmute(0 as usize); - bashline_set_event_hook(); - if !itext.is_null() { - old_startup_hook = std::mem::transmute(rl_startup_hook); - rl_startup_hook = std::mem::transmute(set_itext as usize); - deftext = itext; - } +// old_attempted_completion_function = std::mem::transmute(rl_attempted_completion_function); +// rl_attempted_completion_function = std::mem::transmute(0 as usize); +// bashline_set_event_hook(); +// if !itext.is_null() { +// old_startup_hook = std::mem::transmute(rl_startup_hook); +// rl_startup_hook = std::mem::transmute(set_itext as usize); +// deftext = itext; +// } - let mut ret = readline(p); +// let mut ret = readline(p); - rl_attempted_completion_function = std::mem::transmute(old_attempted_completion_function); - old_attempted_completion_function = std::mem::transmute(0 as usize); - bashline_reset_event_hook(); +// rl_attempted_completion_function = std::mem::transmute(old_attempted_completion_function); +// old_attempted_completion_function = std::mem::transmute(0 as usize); +// bashline_reset_event_hook(); - if ret.is_null() { +// if ret.is_null() { +// return ret; +// } + +// let len:i32 = libc::strlen(ret) as i32; +// ret = xrealloc(ret as *mut c_void, (len + 2) as usize) as *mut c_char; +// // *ret = delim; +// *ret.offset(len as isize) = delim; +// *((ret as usize + 1) as *mut c_char) = b'\0' as c_char; + +// return ret; +// } +// } + + +fn edit_line(p : *mut c_char, itext : *mut c_char) -> *mut c_char { + let mut len:i32; + unsafe { + if bash_readline_initialized == 0 { + initialize_readline(); + } + + old_attempted_completion_function = std::mem::transmute(rl_attempted_completion_function); + rl_attempted_completion_function = std::mem::transmute(0 as usize); + bashline_set_event_hook(); + if !itext.is_null() { + old_startup_hook = std::mem::transmute(rl_startup_hook); + rl_startup_hook = std::mem::transmute(set_itext as usize); + deftext = itext; + } + + let mut ret = readline(p); + + rl_attempted_completion_function = std::mem::transmute(old_attempted_completion_function); + old_attempted_completion_function = std::mem::transmute(0 as usize); + bashline_reset_event_hook(); + + if ret.is_null() { + return ret; + } + + len = libc::strlen(ret) as i32; + ret = xrealloc(ret as *mut c_void, (len + 2) as usize) as *mut c_char; + *ret.offset(len as isize) = delim; + len += 1; + *ret.offset(len as isize) = b'\0' as c_char; return ret; } - - let len = libc::strlen(ret); - ret = xrealloc(ret as *mut c_void, len + 2) as *mut c_char; - *ret = delim; - *((ret as usize + 1) as *mut c_char) = b'\0' as c_char; - return ret; -} } + + fn sigalrm(s : c_int) { unsafe { sigalrm_seen = 1; @@ -926,18 +1049,23 @@ unsafe { } } -fn ttyrestore() -{ -unsafe { - if termsave.is_none() { - let tmp: tty_save = std::mem::zeroed(); - termsave = Some(tmp); - } - - let ter = termsave.unwrap(); - ttsetattr(ter.fd, std::mem::transmute(&(ter.attrs))); - tty_modified = 0; -} +// fn ttyrestore() +// { +// unsafe { +// if termsave.is_none() { +// let tmp: tty_save = std::mem::zeroed(); +// termsave = Some(tmp); +// } + +// let ter = termsave.unwrap(); +// ttsetattr(ter.fd, std::mem::transmute(&(ter.attrs))); +// tty_modified = 0; +// } +// } + +unsafe extern "C" fn ttyrestore(mut ttp:*mut tty_save){ + ttsetattr((*ttp).fd,&mut (*ttp).attrs); + tty_modified = 0 as libc::c_int; } #[no_mangle] @@ -945,7 +1073,7 @@ pub extern "C" fn read_tty_cleanup() { unsafe { if tty_modified != 0 { - ttyrestore(); + ttyrestore(&mut termsave); } } } @@ -964,31 +1092,59 @@ static mut old_newline_ctype: c_int = 0; static mut old_newline_func: usize = 0; static mut delim_char: u8 = 0; +// fn set_eol_delim(c: c_int) +// { +// unsafe { +// if bash_readline_initialized == 0 { +// initialize_readline(); +// } + +// let cmap = rl_get_keymap(); +// let n = std::mem::size_of_val(&*cmap); +// let ret_pos = (b'M' & 0x1f) as usize * n; +// let c_pos = (c & 0x1f) as usize * n; + +// /* Save the old delimiter char binding */ +// old_newline_ctype = (*((cmap as usize + ret_pos) as Keymap)).tp as c_int; +// old_newline_func = (*((cmap as usize + ret_pos) as Keymap)).function as usize; +// old_delim_ctype = (*((cmap as usize + c_pos) as Keymap)).tp as c_int; +// old_delim_func = (*((cmap as usize + c_pos) as Keymap)).function as usize; + +// /* Change newline to self-insert */ +// (*((cmap as usize + ret_pos) as Keymap)).tp = ISFUNC as c_char; +// (*((cmap as usize + ret_pos) as Keymap)).function = rl_insert; + +// /* Bind the delimiter character to accept-line. */ +// (*((cmap as usize + c_pos) as Keymap)).tp = ISFUNC as c_char; +// (*((cmap as usize + c_pos) as Keymap)).function = rl_newline; + +// delim_char = c as u8; +// } +// } + fn set_eol_delim(c: c_int) { + let mut cmap:Keymap; unsafe { if bash_readline_initialized == 0 { initialize_readline(); } - let cmap = rl_get_keymap(); - let n = std::mem::size_of_val(&*cmap); - let ret_pos = (b'M' & 0x1f) as usize * n; - let c_pos = (c & 0x1f) as usize * n; + // let cmap = rl_get_keymap(); + cmap = rl_get_keymap(); - /* Save the old delimiter char binding */ - old_newline_ctype = (*((cmap as usize + ret_pos) as Keymap)).tp as c_int; - old_newline_func = (*((cmap as usize + ret_pos) as Keymap)).function as usize; - old_delim_ctype = (*((cmap as usize + c_pos) as Keymap)).tp as c_int; - old_delim_func = (*((cmap as usize + c_pos) as Keymap)).function as usize; + old_newline_ctype = (*cmap.offset((b'M' as i32 & 0x1f) as isize)).tp as c_int; + old_newline_func = (*cmap.offset((b'M' as i32 & 0x1f) as isize)).function as usize; + old_delim_ctype = (*cmap.offset(c as isize)).tp as c_int; + old_delim_func = (*cmap.offset(c as isize)).function as usize; - /* Change newline to self-insert */ - (*((cmap as usize + ret_pos) as Keymap)).tp = ISFUNC as c_char; - (*((cmap as usize + ret_pos) as Keymap)).function = rl_insert; + /* Change newline to self-insert */ + (*cmap.offset((b'M' as i32 & 0x1f) as isize)).tp = ISFUNC as c_char; + (*cmap.offset((b'M' as i32 & 0x1f) as isize)).function = rl_insert; /* Bind the delimiter character to accept-line. */ - (*((cmap as usize + c_pos) as Keymap)).tp = ISFUNC as c_char; - (*((cmap as usize + c_pos) as Keymap)).function = rl_newline; + (*cmap.offset(c as isize)).tp = ISFUNC as c_char; + (*cmap.offset(c as isize)).function = rl_newline; delim_char = c as u8; } diff --git a/bash-5.1/builtins_rust/source/src/lib.rs b/bash-5.1/builtins_rust/source/src/lib.rs index fcbbf1e040211a924dd7352f74851788585eb01e..a33457a46170f4c26f503b8e1cc0c348c6fab99e 100644 --- a/bash-5.1/builtins_rust/source/src/lib.rs +++ b/bash-5.1/builtins_rust/source/src/lib.rs @@ -386,12 +386,3 @@ pub extern "C" fn r_source_builtin (list:* mut WordList)->i32 } } -#[no_mangle] -pub extern "C" fn cmd_name() ->*const u8 { - return b"source" as *const u8; -} - -#[no_mangle] -pub extern "C" fn run(list : *mut WordList)->i32 { - return r_source_builtin(list); -} diff --git a/record.txt b/record.txt index 26f34bdcde07b113b8f8acb588242ac4a12b8d86..abac46519a0a02d145149a501b619fa1d15e85ce 100644 --- a/record.txt +++ b/record.txt @@ -52,3 +52,4 @@ 51 52 53 +54