diff --git a/bash-5.1/Cargo.toml b/bash-5.1/Cargo.toml index 0bb665eba25c14eba2317342294ea79e95a9fdb7..48dca7e2264e2c3503fafd3f43eed8fe91c141a2 100644 --- a/bash-5.1/Cargo.toml +++ b/bash-5.1/Cargo.toml @@ -8,11 +8,9 @@ crate-type = ["cdylib"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [workspace] -members = ["builtins_rust/read", "builtins/command1", "builtins/command2", "builtins_rust/history"] +members = ["builtins/command1", "builtins/command2"] [dependencies] -libc = "0.2" command1 = {path = "./builtins/command1"} command2 = {path = "./builtins/command2"} read = {path = "./builtins_rust/read"} -history = {path = "./builtins_rust/history"} diff --git a/bash-5.1/builtins_rust/fg_bg/src/lib.rs b/bash-5.1/builtins_rust/fg_bg/src/lib.rs index 689f42d8aaec91ca5c80b790473a884232dd233c..bfb359b537049fdf8f82ea2bb1c9d26456299ae5 100644 --- a/bash-5.1/builtins_rust/fg_bg/src/lib.rs +++ b/bash-5.1/builtins_rust/fg_bg/src/lib.rs @@ -29,7 +29,7 @@ pub enum JOB_STATE { #[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, - cm_arith, cm_cond, cm_arith_for, cm_subshell, cm_coproc + cm_arith, cm_cond, cm_arith_for, cm_subshell, cm_coproc } #[repr(u8)] @@ -71,6 +71,7 @@ pub union REDIRECT { here_doc_eof:*mut c_char /* The word that appeared in < {1} } - #[macro_export] macro_rules! EX_USAGE { () => {258} } - #[macro_export] macro_rules! EXECUTION_SUCCESS { () => {0} } - #[macro_export] macro_rules! BLOCK_SIGNAL { - ($sig:expr, $nvar:expr, $ovar:expr) => { + ($sig:expr, $nvar:expr, $ovar:expr) => { $nvar.unwrap().clear(); $nvar.unwrap().add($sig); $nvar.unwrap().clear(); - nix::sys::signal::sigprocmask(nix::sys::signal::SigmaskHow::SIG_BLOCK, $nvar, $ovar); + nix::sys::signal::sigprocmask(nix::sys::signal::SigmaskHow::SIG_BLOCK, $nvar, $ovar); } } #[macro_export] macro_rules! UNBLOCK_SIGNAL { ($ovar:expr) => { - nix::sys::signal::sigprocmask(nix::sys::signal::SigmaskHow::SIG_SETMASK, $ovar, None) + nix::sys::signal::sigprocmask(nix::sys::signal::SigmaskHow::SIG_SETMASK, $ovar, None) } } - #[macro_export] macro_rules! UNBLOCK_CHILD { ($ovar:expr) => { - UNBLOCK_SIGNAL!($ovar); + UNBLOCK_SIGNAL!($ovar); } } #[macro_export] macro_rules! BLOCK_CHILD { ($nvar:expr,$ovar:expr) => { - BLOCK_SIGNAL!(nix::sys::signal::SIGCHLD, $nvar, $ovar); + BLOCK_SIGNAL!(nix::sys::signal::SIGCHLD, $nvar, $ovar); } } @@ -310,7 +321,7 @@ macro_rules! DUP_JOB { #[macro_export] macro_rules! get_job_by_jid { ($ind:expr) => { - (*(((jobs as usize) + ($ind*8) as usize ) as *mut*mut JOB) as *mut JOB) + (*(((jobs as i32) + $ind*8 ) as *mut*mut JOB) as *mut JOB) } } @@ -329,24 +340,25 @@ macro_rules! IS_JOBCONTROL { #[macro_export] macro_rules! INVALID_JOB { ($j:expr) => { - $j <0 || $j >= js.j_jobslots || get_job_by_jid !($j) == std::ptr::null_mut() + $j <0 || $j >= js.j_jobslots || get_job_by_jid !($j) as u8 == 0 } } #[macro_export] macro_rules! ISHELP { - ($s:expr) => { - libc::strcmp($s as *const c_char,CString::new("--help").unwrap().as_ptr()) + ($s:expr) => { + libc::strcmp($s as *const c_char,CString::new("--help").unwrap().as_ptr()) } } #[macro_export] macro_rules! CHECK_HELPOPT { ($l:expr) => { - if $l !=std::ptr::null_mut() && (*$l).word !=std::ptr::null_mut() && ISHELP!((*(*$l).word).word) == 0 { - builtin_help (); - return EX_USAGE!(); - } + if $l as u8 !=0 && (*$l).word as u8 !=0 && ISHELP!((*(*$l).word).word) == 0 + { + builtin_help (); + return EX_USAGE!(); + } } } @@ -368,12 +380,12 @@ extern "C" { /* How to bring a job into the foreground. */ #[no_mangle] -pub extern "C" fn r_fg_builtin (list:*mut WORD_LIST)->i32 { +pub extern "C" fn r_fg_builtin (list:*mut WORD_LIST)->i32{ let fg_bit:i32; unsafe { CHECK_HELPOPT! (list); - if job_control == 0 { + if job_control == 0 { sh_nojobs (0 as *mut c_char); return EXECUTION_FAILURE!(); } @@ -385,22 +397,23 @@ pub extern "C" fn r_fg_builtin (list:*mut WORD_LIST)->i32 { /* If the last arg on the line is '&', then start this job in the background. Else, fg the job. */ - if loptend == std::ptr::null_mut() { + if loptend as u8 == 0{ return r_fg_bg (loptend, 1); - } else { + }else { let mut t:WORD_LIST=*loptend; - while t.next !=std::ptr::null_mut() { + while t.next as u8 !=0{ t=*(t.next); } let cstr:&std::ffi::CStr=std::ffi::CStr::from_ptr((*(t.word)).word ); let mut isfg:bool=char::from( cstr.to_bytes()[0] ) == '&'; isfg =isfg && char::from( cstr.to_bytes()[1]) == '\0'; isfg = isfg ==false; - if isfg { + if isfg{ fg_bit=1; - } else { + }else { fg_bit=0; - } + } + return r_fg_bg (loptend, fg_bit); } } @@ -408,7 +421,7 @@ pub extern "C" fn r_fg_builtin (list:*mut WORD_LIST)->i32 { /* How to put a job into the background. */ #[no_mangle] -pub extern "C" fn r_bg_builtin (list:*mut WORD_LIST)->i32 { +pub extern "C" fn r_bg_builtin (list:*mut WORD_LIST)->i32{ let mut r:i32; unsafe { CHECK_HELPOPT !(list); @@ -426,22 +439,23 @@ pub extern "C" fn r_bg_builtin (list:*mut WORD_LIST)->i32 { on the first member (if any) of that list. */ r = EXECUTION_SUCCESS!(); - if r_fg_bg(loptend,0) == EXECUTION_FAILURE!() { + if r_fg_bg(loptend,0) == EXECUTION_FAILURE!(){ r = EXECUTION_FAILURE!(); } - if loptend !=std::ptr::null_mut() { - let mut t:WORD_LIST=*loptend; - while t.next !=std::ptr::null_mut() { - if r_fg_bg (&mut t, 0) == EXECUTION_FAILURE!() { + if loptend as u8 !=0 { + let mut t:WORD_LIST=*loptend; + while t.next as u8 !=0 { + if r_fg_bg (&mut t, 0) == EXECUTION_FAILURE!(){ r = EXECUTION_FAILURE!(); - } + } t = *(t.next); } return r; } else { return r; - } + } + } } @@ -457,64 +471,58 @@ pub extern "C" fn r_fg_bg (list:*mut WORD_LIST, foreground:i32)->i32{ let j:*mut JOB; unsafe { - BLOCK_CHILD !(Some(&mut set), Some(&mut oset)); + + BLOCK_CHILD !(Some(&mut set), Some(&mut oset)); job = get_job_spec (list); - if INVALID_JOB !(job) { - if job != DUP_JOB!() { - if list != std::ptr::null_mut() { - sh_badjob ( (*(*list).word).word ); - } else { - let mut c_str_current = CString::new("current").unwrap(); // from a &str, creates a new allocation - sh_badjob (c_str_current.as_ptr() as * mut c_char); + if INVALID_JOB !(job){ + if job != DUP_JOB!(){ + if list as u8 != 0 { + sh_badjob ( (*(*list).word).word ); + }else { + let mut c_str_current = CString::new("current").unwrap(); // from a &str, creates a new allocation + sh_badjob (c_str_current.as_ptr() as * mut c_char); + } } - } - UNBLOCK_CHILD !(Some(&oset)); - return EXECUTION_FAILURE!(); - } + UNBLOCK_CHILD !(Some(&oset)); + return EXECUTION_FAILURE!(); + } j = get_job_by_jid !(job); /* Or if j->pgrp == shell_pgrp. */ - if ! IS_JOBCONTROL !(job) { + if ! IS_JOBCONTROL !(job) { let jobNum:i32=job + 1; builtin_error ( String::from("job ").add(&jobNum.to_string()).add(&String::from("started without job control").to_string()).as_ptr() as * const c_char); UNBLOCK_CHILD !(Some(&oset)); return EXECUTION_FAILURE!(); - } + } if foreground == 0 { old_async_pid = i32::from(last_asynchronous_pid); last_asynchronous_pid = i32::from((*j).pgrp); /* As per Posix.2 5.4.2 */ - } + } status = start_job (job, foreground); - if status >= 0 { + if status >= 0{ /* win: */ UNBLOCK_CHILD !(Some(&oset)); if foreground !=0 { return status; - } else { + }else { return EXECUTION_SUCCESS!(); } - } else { - if foreground == 0 { + } + else + { + if foreground == 0{ last_asynchronous_pid = i32::from(old_async_pid); } - + UNBLOCK_CHILD !(Some(&oset)); return EXECUTION_FAILURE!(); } } } -#[no_mangle] -pub extern "C" fn cmd_name() ->*const u8 { - return b"fg" as *const u8; -} - -#[no_mangle] -pub extern "C" fn run(list : *mut WORD_LIST)->i32 { - return r_fg_builtin(list); -} \ No newline at end of file diff --git a/bash-5.1/builtins_rust/jobs/src/lib.rs b/bash-5.1/builtins_rust/jobs/src/lib.rs index 9456b58f848d8503a96b63ff1294ee7ec04c19e4..5c0162713f275a4eccc43184e6ab56284ccba124 100644 --- a/bash-5.1/builtins_rust/jobs/src/lib.rs +++ b/bash-5.1/builtins_rust/jobs/src/lib.rs @@ -31,7 +31,6 @@ enum command_type { cm_for, cm_case, cm_while, cm_if, cm_simple, cm_select, cm_connection, cm_function_def, cm_until, cm_group, cm_arith, cm_cond, cm_arith_for, cm_subshell, cm_coproc } - #[repr(u8)] #[derive(Copy,Clone)] enum r_instruction { @@ -42,8 +41,7 @@ enum r_instruction { r_duplicating_input_word, r_duplicating_output_word, r_move_input, r_move_output, r_move_input_word, r_move_output_word, r_append_err_and_out -} - + } #[repr(C)] pub struct PROCESS { next: *mut PROCESS, @@ -71,6 +69,7 @@ pub union REDIRECT { here_doc_eof:*mut c_char /* The word that appeared in < {3} } - #[macro_export] macro_rules! EXECUTION_FAILURE { () => {1} } - #[macro_export] macro_rules! JSTATE_RUNNING { () => {0x1} } - #[macro_export] macro_rules! JSTATE_STOPPED { () => {0x2} } - #[macro_export] macro_rules! EX_USAGE { () => {258} } - #[macro_export] macro_rules! EXECUTION_SUCCESS { () => {0} } - #[macro_export] macro_rules! BLOCK_SIGNAL { - ($sig:expr, $nvar:expr, $ovar:expr) => { + ($sig:expr, $nvar:expr, $ovar:expr) => { $nvar.unwrap().clear(); $nvar.unwrap().add($sig); $nvar.unwrap().clear(); - nix::sys::signal::sigprocmask(nix::sys::signal::SigmaskHow::SIG_BLOCK, $nvar, $ovar); + nix::sys::signal::sigprocmask(nix::sys::signal::SigmaskHow::SIG_BLOCK, $nvar, $ovar); } } @@ -322,18 +317,17 @@ macro_rules! UNBLOCK_SIGNAL { nix::sys::signal::sigprocmask(nix::sys::signal::SigmaskHow::SIG_SETMASK, $ovar, None) } } - #[macro_export] macro_rules! UNBLOCK_CHILD { ($ovar:expr) => { - UNBLOCK_SIGNAL!($ovar); + UNBLOCK_SIGNAL!($ovar); } } #[macro_export] macro_rules! BLOCK_CHILD { ($nvar:expr,$ovar:expr) => { - BLOCK_SIGNAL!(nix::sys::signal::SIGCHLD, $nvar, $ovar); + BLOCK_SIGNAL!(nix::sys::signal::SIGCHLD, $nvar, $ovar); } } @@ -346,7 +340,6 @@ macro_rules! NO_JOB { macro_rules! DUP_JOB { () => {-2} } - #[macro_export] macro_rules! CMD_INHIBIT_EXPANSION {/* Do not expand the command words. */ () => {0x20} @@ -355,14 +348,14 @@ macro_rules! CMD_INHIBIT_EXPANSION {/* Do not expand the command words. */ #[macro_export] macro_rules! get_job_by_jid { ($ind:expr) => { - (*((jobs as usize + ($ind*8) as usize ) as *mut*mut JOB) as *mut JOB) + (*(((jobs as i32) + $ind*8 ) as *mut*mut JOB) as *mut JOB) } } #[macro_export] macro_rules! INVALID_JOB { ($j:expr) => { - $j <0 || $j >= js.j_jobslots || get_job_by_jid !($j) == std::ptr::null_mut() + $j <0 || $j >= js.j_jobslots || get_job_by_jid !($j) as u8 == 0 } } @@ -372,14 +365,15 @@ extern "C" { fn internal_getopt (list:*mut WORD_LIST , opts:*mut c_char)->i32; fn builtin_error(err:*const c_char,...); fn builtin_usage(); - static mut loptend:*mut WORD_LIST; + static mut loptend:*mut WORD_LIST; fn list_all_jobs(form:i32); fn list_stopped_jobs(form:i32); - fn list_one_job (jjob:*mut JOB, format:i32, ignore:i32, job_index:i32); + fn list_one_job (jjob:*mut JOB, format:i32, ignore:i32, job_index:i32); fn get_job_spec (list:*mut WORD_LIST)->i32; fn sh_badjob (str:*mut c_char); static jobs:*mut*mut JOB; - fn discard_unwind_frame (str: * mut c_char); + fn discard_unwind_frame (str: * mut c_char); + fn begin_unwind_frame (str: * mut c_char); fn execute_command (command:* mut COMMAND)->i32; fn dispose_command (command:* mut COMMAND); @@ -394,32 +388,34 @@ extern "C" { fn nohup_all_jobs (running_only:i32); fn delete_all_jobs(running_only:i32); } - #[no_mangle] pub extern "C" fn r_execute_list_with_replacements (list:*mut WORD_LIST)->i32{ - //println!("r_execute_list_with_replacements"); + println!("r_execute_list_with_replacements"); unsafe{ - let mut l:*mut WORD_LIST=list; + let mut l:WORD_LIST=*list; let mut job:i32; let result:i32; let command:*mut COMMAND; - /* First do the replacement of job specifications with pids. */ - while l !=std::ptr::null_mut() { - let lchar:char=char::from((*(*(*l).word).word) as u8); + /* First do the replacement of job specifications with pids. */ + + while l.next as u8 !=0 { + let lchar:char=char::from((*(*l.word).word) as u8); if lchar== '%' /* we have a winner */ { - job = get_job_spec ( l); + job = get_job_spec (&mut l); + /* A bad job spec is not really a job spec! Pass it through. */ if INVALID_JOB!(job){ continue; } - libc::free((*(*l).word).word as * mut libc::c_void); - (*(*(*l).word).word) = (*get_job_by_jid! (job)).pgrp as i8; - } - l=(*l).next; - } - + libc::free((*l.word).word as * mut libc::c_void); + (*(*l.word).word) = (*get_job_by_jid! (job)).pgrp as i8; + } + l=*(l.next); + } + + let mut c_str_jobs_builtin = CString::new("jobs_builtin").unwrap(); /* Next make a new simple command and execute it. */ begin_unwind_frame (c_str_jobs_builtin.as_ptr() as * mut c_char); @@ -433,14 +429,15 @@ extern "C" { add_unwind_protect(dispose_command, command); result = execute_command (command); dispose_command (command); + discard_unwind_frame (c_str_jobs_builtin.as_ptr() as * mut c_char); return result; } } #[no_mangle] -pub extern "C" fn r_jobs_builtin(list:*mut WORD_LIST)->i32 { - //println!("r_jobs_builtin"); +pub extern "C" fn r_jobs_builtin(list:*mut WORD_LIST)->i32{ + println!("r_jobs_builtin"); let mut form:i32; let mut execute:i32=0; let mut state:i32; @@ -455,41 +452,44 @@ pub extern "C" fn r_jobs_builtin(list:*mut WORD_LIST)->i32 { state = JSTATE_ANY!(); unsafe { - reset_internal_getopt(); - - let mut c_str_lpnxrs = CString::new("lpnxrs").unwrap(); // from a &str, creates a new allocation - - opt = internal_getopt (list, c_str_lpnxrs.as_ptr() as * mut c_char); - while opt != -1 { - let optu8:u8= opt as u8; - let optChar:char=char::from(optu8); - match optChar{ - 'l'=>{form = JLIST_LONG!();} - 'p'=>{form = JLIST_PID_ONLY!();} - 'n'=>{form = JLIST_CHANGED_ONLY!();} - 'x'=>{ - if form != JLIST_STANDARD!() { - let mut c_str_err = CString::new("no other options allowed with `-x'").unwrap(); // from a &str, creates a new allocation - builtin_error (c_str_err.as_ptr()); - return EXECUTION_FAILURE!(); + reset_internal_getopt(); + + let mut c_str_lpnxrs = CString::new("lpnxrs").unwrap(); // from a &str, creates a new allocation + + opt = internal_getopt (list, c_str_lpnxrs.as_ptr() as * mut c_char); + while opt != -1{ + let optu8:u8= opt as u8; + let optChar:char=char::from(optu8); + match optChar{ + 'l'=>{form = JLIST_LONG!();} + 'p'=>{form = JLIST_PID_ONLY!();} + 'n'=>{form = JLIST_CHANGED_ONLY!();} + 'x'=>{ + if form != JLIST_STANDARD!() { + let mut c_str_err = CString::new("no other options allowed with `-x'").unwrap(); // from a &str, creates a new allocation + builtin_error (c_str_err.as_ptr()); + return EXECUTION_FAILURE!(); + } + execute+=1; } - execute+=1; + 'r'=>{state = JSTATE_RUNNING!();} + 's'=>{state = JSTATE_STOPPED!();} + + _=>{ + builtin_usage (); + return EX_USAGE!(); } - 'r'=>{state = JSTATE_RUNNING!();} - 's'=>{state = JSTATE_STOPPED!();} - _=>{ - builtin_usage (); - return EX_USAGE!(); - } + + } + opt = internal_getopt (list, c_str_lpnxrs.as_ptr() as * mut c_char); } - opt = internal_getopt (list, c_str_lpnxrs.as_ptr() as * mut c_char); - } - if execute != 0 { - return r_execute_list_with_replacements (loptend); - } - if loptend ==std::ptr::null_mut() { + if execute != 0 { + return r_execute_list_with_replacements (loptend); + } + + if loptend as u8 ==0 { if state == JSTATE_ANY!() { list_all_jobs (form); } else if state == JSTATE_RUNNING!() { @@ -499,16 +499,16 @@ pub extern "C" fn r_jobs_builtin(list:*mut WORD_LIST)->i32 { } return EXECUTION_SUCCESS!(); } - + let mut loptendt=*loptend; - while loptendt.next !=std::ptr::null_mut() { + while loptendt.next as u8 !=0 { BLOCK_CHILD !(Some(&mut set), Some(&mut oset)); job = get_job_spec (&mut loptendt); - if (job == NO_JOB!()) || jobs == std::ptr::null_mut() || get_job_by_jid !(job) == std::ptr::null_mut() { - sh_badjob ((*loptendt.word).word); + if (job == NO_JOB!()) || jobs as u8 == 0 || get_job_by_jid !(job) as u8 == 0 { + sh_badjob ((*loptendt.word).word); any_failed+=1; - } else if (job != DUP_JOB!()) { + } else if (job != DUP_JOB!()){ list_one_job (0 as * mut JOB, form, 0, job); } @@ -517,14 +517,13 @@ pub extern "C" fn r_jobs_builtin(list:*mut WORD_LIST)->i32 { } if any_failed !=0 { return EXECUTION_FAILURE!(); - } else { + }else { return EXECUTION_SUCCESS!(); } } } - #[no_mangle] -pub extern "C" fn r_disown_builtin (list:* mut WORD_LIST)->libc::c_int { +pub extern "C" fn r_disown_builtin (list:* mut WORD_LIST)->libc::c_int{ let opt:i32; let mut job:i32=0; let mut retval:i32; @@ -535,89 +534,88 @@ pub extern "C" fn r_disown_builtin (list:* mut WORD_LIST)->libc::c_int { let mut set:nix::sys::signal::SigSet=nix::sys::signal::SigSet::empty(); let mut oset:nix::sys::signal::SigSet =nix::sys::signal::SigSet::empty(); let mut pid_value:c_long=0; - //println!("r_disown_builtin"); + println!("r_disown_builtin"); unsafe { reset_internal_getopt (); let mut c_str_ahr = CString::new("ahr").unwrap(); // from a &str, creates a new allocation + opt = internal_getopt (list, c_str_ahr.as_ptr() as * mut c_char); - while opt != -1 { + while opt != -1{ let optu8:u8= opt as u8; let optChar:char=char::from(optu8); match optChar{ - 'a'=>{all_jobs = 1;} - 'h'=>{nohup_only = 1;} - 'r'=>{running_jobs = 1;} - _=>{ - builtin_usage (); - return EX_USAGE!(); - } + 'a'=>{all_jobs = 1;} + 'h'=>{nohup_only = 1;} + 'r'=>{running_jobs = 1;} + _=>{ + builtin_usage (); + return EX_USAGE!(); + } + } - } - + } + retval = EXECUTION_SUCCESS!(); /* `disown -a' or `disown -r' */ - if loptend == std::ptr::null_mut() && (all_jobs !=0 || running_jobs != 0) { + if loptend as u8 == 0 && (all_jobs !=0 || running_jobs != 0) { if nohup_only!=0{ nohup_all_jobs (running_jobs); - } else { + } else{ delete_all_jobs (running_jobs); } - return EXECUTION_SUCCESS!(); - } + + return EXECUTION_SUCCESS!(); + } - BLOCK_CHILD !(Some(&mut set), Some(&mut oset)); - if (loptend !=std::ptr::null_mut() && legal_number ((*(*loptend).word).word, &mut pid_value) !=0 && pid_value == pid_value) { - job=get_job_by_pid ( pid_value as i32, 0, 0 as *mut*mut PROCESS); - }else { - get_job_spec (loptend); - } - if job == NO_JOB!() || jobs !=std::ptr::null_mut() || INVALID_JOB!(job) { - if loptend !=std::ptr::null_mut() { + BLOCK_CHILD !(Some(&mut set), Some(&mut oset)); + if (loptend as u8 !=0 && legal_number ((*(*loptend).word).word, &mut pid_value) !=0 && pid_value == pid_value) { + job=get_job_by_pid ( pid_value as i32, 0, 0 as *mut*mut PROCESS); + }else { + get_job_spec (loptend); + + } + if job == NO_JOB!() || jobs as u8 !=0 || INVALID_JOB!(job){ + if loptend as u8 !=0 { sh_badjob ((*(*loptend).word).word); - } else { + }else { sh_badjob (CString::new("current").unwrap().as_ptr() as * mut c_char); - } - retval = EXECUTION_FAILURE!(); - } else if nohup_only !=0{ + } + retval = EXECUTION_FAILURE!(); + } else if nohup_only !=0{ nohup_job (job); } else { delete_job (job, 1); } - - UNBLOCK_CHILD !(Some(&oset)); - - if loptend != std::ptr::null_mut() { + + UNBLOCK_CHILD !(Some(&oset)); + + + if loptend as u8 !=0 { let mut loptendt=*loptend; - while loptendt.next !=std::ptr::null_mut() { + while loptendt.next as u8 !=0 { loptendt = *loptendt.next; BLOCK_CHILD !(Some(&mut set), Some(&mut oset)); if legal_number ((*loptendt.word).word, &mut pid_value) !=0 && pid_value == pid_value { job=get_job_by_pid ( pid_value as i32, 0, 0 as *mut*mut PROCESS); - } else { + }else { get_job_spec (&mut loptendt); + } - if job == NO_JOB!() || jobs !=std::ptr::null_mut() || INVALID_JOB!(job) { - sh_badjob ((*loptendt.word).word); + if job == NO_JOB!() || jobs as u8 !=0 || INVALID_JOB!(job){ + + sh_badjob ((*loptendt.word).word); + retval = EXECUTION_FAILURE!(); } else if nohup_only !=0{ nohup_job (job); } else { delete_job (job, 1); } + UNBLOCK_CHILD !(Some(&oset)); } } return retval; } -} - -#[no_mangle] -pub extern "C" fn cmd_name() ->*const u8 { - return b"jobs" as *const u8; -} - -#[no_mangle] -pub extern "C" fn run(list : *mut WORD_LIST)->i32 { - return r_jobs_builtin(list); } \ No newline at end of file diff --git a/bash-5.1/builtins_rust/read/src/lib.rs b/bash-5.1/builtins_rust/read/src/lib.rs index 8015ec1bdb1e8a163b80b63470ef4306b9fbb214..3c47a31b6e18d7599265e43ca381506b18a42308 100644 --- a/bash-5.1/builtins_rust/read/src/lib.rs +++ b/bash-5.1/builtins_rust/read/src/lib.rs @@ -1,1008 +1,35 @@ -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}; -include!(concat!("intercdep.rs")); +use libc::{F_UNLCK, c_char, c_long}; +use std::ffi::CString; -static mut old_alrm : *mut SigHandler = PT_NULL as *mut SigHandler; - -static mut sigalrm_seen : c_int = 0; -static mut reading : c_int = 0; -static mut tty_modified : c_int = 0; - -static mut delim : c_char= b'\n' as c_char; -#[derive(Clone, Copy)] -pub struct tty_save { - fd: i32, - attrs: libc::termios, +#[repr(C)] +pub struct WORD_DESC { + pub word: *mut libc::c_char, + pub flags:libc::c_int } -static mut termsave : Option = None; -static mut ptermsave: *mut c_void = PT_NULL as *mut c_void; - -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 WORD_LIST) -> i32 { - println!("r_read_builtin call"); - - let mut varname :*mut c_char = libc::PT_NULL as *mut c_char; - let mut size : c_int = 0; - let mut nr : c_int = 0; - let mut pass_next : c_int = 0; - let mut saw_escape : c_int = 0; - let mut eof : c_int; - let mut opt : c_int; - let mut retval : c_int; - let mut code : c_int; - let mut print_ps2 : c_int; - let mut nflag : c_int = 0; - - let mut i : c_int = 0; - - let mut input_is_tty : c_int = 0; - let mut input_is_pipe : c_int = 0; - let mut unbuffered_read : c_int = 0; - let mut skip_ctlesc : c_int; - let mut skip_ctlnul : c_int; - - let mut raw : c_int = 0; - let mut edit : c_int = 0; - let mut nchars : c_int = 0; - let mut silent : c_int = 0; - let mut have_timeout : c_int = 0; - let mut ignore_delim : c_int = 0; - let mut fd : c_int = 0; - - let mut lastsig : c_int = 0; - let mut t_errno : c_int; - - let mut mb_cur_max : c_int; - - let mut tmsec : c_uint = 0; - let mut tmusec : c_uint = 0; - - let mut ival : c_long = 0; - let mut uval : c_long = 0; - let mut intval : c_long = 0; - - let mut c : c_char = 0; - - let mut input_string : *mut c_char; - let mut orig_input_string : *mut c_char; - let ifs_chars_null = CString::new("").unwrap(); - let mut ifs_chars : *mut c_char; - let mut prompt : *mut c_char = PT_NULL as *mut c_char; - let mut arrayname : *mut c_char = PT_NULL as *mut c_char; - - let mut e : *mut c_char; - let t : *mut c_char; - let t1 : *mut c_char; - let mut ps2 : *mut c_char; - let mut tofree : *mut c_char; - - let mut tsb : libc::stat; - - let mut var : *mut SHELL_VAR = PT_NULL as *mut SHELL_VAR; - - let mut ttattrs : libc::termios; - let mut ttset : libc::termios; - unsafe { - ttattrs = std::mem::zeroed(); - ttset = std::mem::zeroed(); - } - - let mut alist : *mut WORD_LIST; - - let mut vflags : c_int; - let mut rlbuf : *mut c_char = null_mut(); - let mut itext : *mut c_char = null_mut(); - - let mut rlind : c_int = 0; - - - let mut save_instream : *mut libc::FILE; - - let mut mb_cur_max : c_int = 1; - -unsafe { - - 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(); - 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 { - 'r' => raw = 1, - 'p' => prompt = list_optarg, - 's' => silent = 1, - 'e' => edit = 1, - 'i' => itext = list_optarg, - 'a' => arrayname = list_optarg, - 't' => { - code = uconvert(list_optarg, &mut ival, &mut uval, PT_NULL as *mut *mut c_char); - if code == 0 || ival < 0 || uval < 0 { - let c_err = CString::new("%s: invalid timeout specification").unwrap(); - builtin_error( c_err.as_ptr(), list_optarg); - return EXECUTION_FAILURE; - } else { - have_timeout = 1; - tmsec = ival as c_uint; - tmusec = uval as c_uint; - } - } - 'N' => { - ignore_delim = 1; - delim = -1; - } - 'n' => { - nflag = 1; - code = legal_number(list_optarg, &mut intval); - if code == 0 || intval < 0 || intval != (intval as c_int) as c_long { - sh_invalidnum(list_optarg); - return EXECUTION_FAILURE; - } else { - nchars = intval as c_int; - } - } - 'u' => { - code = legal_number(list_optarg, &mut intval); - if code == 0 || intval < 0 || intval != (intval as c_int) as c_long { - let c_err = CString::new("%s: invalid file descriptor specification").unwrap(); - builtin_error(c_err.as_ptr(), list_optarg); - return EXECUTION_FAILURE; - } else { - fd = intval as c_int; - } - if sh_validfd(fd) == 0 { - let c_err = CString::new("%d: invalid file descriptor: %s").unwrap(); - builtin_error(c_err.as_ptr(), fd, libc::strerror(nix::errno::errno())); - return EXECUTION_FAILURE; - } - } - 'd' => delim = *list_optarg, - _ => { - builtin_usage (); - return EX_USAGE; - } - } - opt = internal_getopt (list, opt_str.as_ptr() as * mut c_char); - } - - list = loptend; - - if have_timeout == 1 && 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); - return EXECUTION_FAILURE; - } - - ifs_chars = getifs(); - 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; - } - skip_ctlesc |= (*e == 1) as c_int; - skip_ctlnul |= (*e == 117) as c_int; - e = ((e as usize) + 1) as *mut c_char; - } - - input_string = xmalloc(112) as *mut c_char; - *input_string = b'\0' as c_char; - -'out_assig_vars: loop { - if nflag == 1 && nchars == 0 { - let mut gc : c_int = 0; - retval = libc::read(fd, &mut gc as *mut i32 as *mut c_void, 0) as c_int; - retval = if retval >= 0 {EXECUTION_SUCCESS} else {EXECUTION_FAILURE}; - - break 'out_assig_vars; - } - - let str_val = CString::new("TMOUT").unwrap(); - e = get_string_value(str_val.as_ptr()); - if have_timeout == 0 && !e.is_null() { - code = uconvert(e, &mut ival, &mut uval, 0 as *mut *mut c_char); - if code == 0 || ival < 0 || uval < 0 { - tmsec = 0; - tmusec = 0; - } else { - tmsec = ival as c_uint; - tmusec = uval as c_uint; - } - } - - let frame_name = CString::new("read_builtin").unwrap(); - 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); - } - - input_is_tty = libc::isatty(fd); - if input_is_tty == 0 { - input_is_pipe = (libc::lseek(fd, 0, libc::SEEK_CUR) < 0 && (errno() == libc::ESPIPE)) as c_int; - } - - if (!prompt.is_null() || edit != 0 || silent != 0) && input_is_tty == 0 { - itext = PT_NULL as *mut c_char; - edit = 0; - silent = 0; - } - - if edit != 0 { - add_unwind_protect(xfree as *mut c_void, rlbuf); - } - - tsb = std::mem::zeroed(); - if tmsec > 0 || tmusec > 0 { - if (libc::fstat(fd, &mut tsb as *mut libc::stat) < 0) || - ((tsb.st_mode & __S_IFMT) == __S_IFREG) { - tmsec = 0; - tmusec = 0; - } - } - - if tmsec > 0 || tmusec > 0 { - code = __sigsetjmp(&mut alrmbuf as *mut __jmp_buf_tag, 0); - if code != 0 { - sigalrm_seen = 0; - orig_input_string = PT_NULL as *mut c_char; - if i == 0 { - t = libc::malloc(1) as *mut c_char; - *t = b'\0' as c_char; - } else { - 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; - break 'out_assig_vars; - } - - if interactive_shell == 0 { - initialize_terminating_signals(); - } - - old_alrm = set_signal_handler(libc::SIGALRM, sigalrm as *mut SigHandler); - add_unwind_protect(reset_alarm as *mut c_void, PT_NULL as *mut c_char); - - if edit != 0 { - add_unwind_protect(reset_attempted_completion_function as *mut c_void, - PT_NULL as *mut c_char); - 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 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); - rl_num_chars_to_read = nchars; - } - - if delim != b'\n' as c_char { - 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; - ttgetattr(fd, &mut ttattrs as *mut libc::termios); - termsave.unwrap().attrs = ttattrs; - ttset = ttattrs; - if silent != 0 { - i = ttfd_cbreak(fd, std::mem::transmute(&ttset)); - } else { - i = ttfd_onechar(fd, std::mem::transmute(&ttset)); - } - - if i < 0 { - sh_ttyerror(1); - } - tty_modified = 1; - add_unwind_protect(ttyrestore as *mut c_void, ptermsave); - if interactive_shell == 0 { - initialize_terminating_signals(); - } - - } - } else if silent != 0 { - termsave.unwrap().fd = fd; - ttgetattr(fd, &mut ttattrs as *mut libc::termios); - termsave.unwrap().attrs = ttattrs; - - ttset = ttattrs; - i = ttfd_noecho(fd, std::mem::transmute(&ttset)); - if i < 0 { - sh_ttyerror(1); - } - - tty_modified = 1; - add_unwind_protect(ttyrestore as *mut c_void, ptermsave); - if interactive_shell == 0 { - initialize_terminating_signals(); - } - } - - save_instream = std::mem::zeroed(); - if edit != 0 && fd != 0 { - if bash_readline_initialized == 0 { - initialize_readline(); - } - - unwind_protect_mem(std::mem::transmute(rl_instream), std::mem::size_of_val(&rl_instream) as c_int); - save_instream = rl_instream; - rl_instream = libc::fdopen(fd, "r".as_ptr() as *const c_char); - - } - - add_unwind_protect(xfree as *mut c_void, input_string); - check_alrm(); - if nchars > 0 && input_is_tty == 0 && ignore_delim != 0 { - unbuffered_read = 2; - } else if nchars > 0 || delim != b'\n' as c_char || input_is_pipe != 0 { - unbuffered_read = 1; - } - - if !prompt.is_null() && edit == 0 { - eprintln!("{}", CStr::from_ptr(prompt).to_str().unwrap()); - - } - - ps2 = PT_NULL as *mut c_char; - print_ps2 = 0; - eof = 0; - retval = 0; - 'get_input_string: loop { - if sigalrm_seen != 0 { - siglongjmp (std::mem::transmute(&alrmbuf), 1); - } - - if edit != 0 { - if !rlbuf.is_null() && - *((rlbuf as usize + rlind as usize) as *mut c_char) == 0 && - delim != 0 { - libc::free(rlbuf as *mut c_void); - rlbuf = PT_NULL as *mut c_char; - } - 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)}; - reading = 0; - rlind = 0; - } - if rlbuf.is_null() { - eof = 1; - break 'get_input_string; - } - c = *((rlbuf as usize + rlind as usize) as *mut c_char); - rlind += 1; - } else { - if print_ps2 != 0 { - if ps2.is_null() { - ps2 = get_string_value("PS2".as_ptr() as *const c_char); - } - eprintln!("{}", CStr::from_ptr(prompt).to_str().unwrap()); - print_ps2 = 0; - } - - reading = 1; - 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}; - } 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}; - } - - reading = 0; - - if retval <= 0 { - let t = *libc::__errno_location(); - if retval < 0 && *libc::__errno_location() == libc::EINTR { - check_signals(); - //lastsig = LASTSIG(); - if terminating_signal != 0 { - lastsig = terminating_signal; - } else { - lastsig = if interrupt_state != 0 {libc::SIGINT} else { 0 }; - } - - if lastsig == 0 { - lastsig = trapped_signal_received; - } - } else { - lastsig = 0; - } - - if terminating_signal != 0 && tty_modified != 0 { - ttyrestore(); - } - check_alrm(); - eof = 1; - *libc::__errno_location() = t; - break 'get_input_string; - } - - quit(); - } - - if retval <= 0 { - check_alrm(); - } - - if mb_cur_max <= 4 { - mb_cur_max = 4; - } - if i + mb_cur_max >= size { - size += 128; - let t: *mut c_char= xrealloc(input_string as *mut c_void, size as usize) as *mut c_char; - if t != input_string { - input_string = t; - remove_unwind_protect(); - add_unwind_protect(xfree as *mut c_void, input_string); - } - } -'out_add_char: loop { - if pass_next != 0 { - pass_next = 0; - if c == b'\n' as c_char { - if skip_ctlesc == 0 && i > 0 {i -= 1;} - if interactive != 0 && input_is_tty != 0 && raw == 0 {print_ps2 = 1;} - } else { - break 'out_add_char; - } - continue 'get_input_string; - } - - if c == b'\\' as c_char && raw == 0 { - pass_next += 1; - if skip_ctlesc == 0 { - saw_escape += 1; - *((input_string as usize + i as usize) as *mut c_char) = CTLESC; - i += 1; - } - continue 'get_input_string; - } - - if ignore_delim == 0 && c == delim { - break 'get_input_string; - } - - if c == b'\0' as c_char && delim != b'\0' as c_char { - continue 'get_input_string; - } - - if (skip_ctlesc == 0 && c == CTLESC) || (skip_ctlnul == 0 && c == CTLNUL) { - saw_escape += 1; - *((input_string as usize + i as usize) as *mut c_char) = CTLESC; - i += 1; - } - break '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 { - let clen = mbrlen((rlbuf as usize + rlind as usize - 1) as *const c_char, - mb_cur_max as usize, - std::mem::transmute(&PT_NULL)); - if clen > 1 { - libc::memcpy( (input_string as usize + i as usize) as *mut c_void, - (rlbuf as usize + rlind as usize) as *mut c_void, (clen - 1) as size_t); - i += clen - 1; - rlind += clen - 1; - } - } 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; - } - } - } - - *((input_string as usize + i as usize) as *mut c_char) = b'\0' as c_char; - check_alrm(); - - if edit != 0 { - libc::free(rlbuf as *mut c_void); - } - - if retval < 0 { - t_errno = *libc::__errno_location(); - if *libc::__errno_location() != EINTR { - let c_err = CString::new("read error: %d: %s").unwrap(); - builtin_error( c_err.as_ptr(), fd, libc::strerror(*libc::__errno_location())); - } - - run_unwind_frame(frame_name.as_ptr() as *mut c_char); - return if t_errno != EINTR {EXECUTION_FAILURE} else { 128 + lastsig}; - } - - if tmsec > 0 || tmusec > 0 { - reset_alarm(); - } - - if nchars > 0 || delim != b'\n' as c_char { - if edit != 0 { - - } else if input_is_tty != 0 { - ttyrestore(); - } - } else if silent != 0 { - ttyrestore(); - } - - if unbuffered_read != 0 { - zsyncfd(fd); - } - - if !save_instream.is_null() { - rl_instream = save_instream; - } - - discard_unwind_frame(frame_name.as_ptr() as *mut c_char); - - retval = if eof != 0 {EXECUTION_FAILURE} else {EXECUTION_SUCCESS}; - - break 'out_assig_vars; -} - - if !arrayname.is_null() { - if legal_identifier(arrayname) == 0 { - sh_invalidid(arrayname); - libc::free(input_string as *mut c_void); - return EXECUTION_FAILURE; - } - - var = find_or_make_array_variable(arrayname, 1); - if var.is_null() { - libc::free(input_string as *mut c_void); - return EXECUTION_FAILURE; /* readonly or noassign */ - } - if ((*var).attributes & 0x0000040) != 0 { - let c_err = CString::new("%s: cannot convert associative to indexed array").unwrap(); - builtin_error(c_err.as_ptr(), arrayname); - libc::free(input_string as *mut c_void); - return EXECUTION_FAILURE; /* existing associative array */ - } else if ((*var).attributes & 0x0001000) != 0 { - (*var).attributes &= ((*var).attributes as u32 ^ 0xffffffff as u32) as i32; - } - - array_flush(std::mem::transmute((*var).value)); - - alist = list_string(input_string, ifs_chars, 0); - if !alist.is_null() { - if saw_escape != 0 { - dequote_list(alist); - } else { - word_list_remove_quoted_nulls(alist); - } - assign_array_var_from_word_list(var, alist, 0); - dispose_words(alist); - } - - libc::free(input_string as *mut c_void); - return retval; - } - - if list.is_null() { - if saw_escape != 0 { - let t = dequote_string(input_string); - var = bind_variable("REPLY".as_ptr() as *const c_char, t, 0); - libc::free(t as *mut c_void); - } else { - var = bind_variable("REPLY".as_ptr() as *const c_char, input_string, 0); - } - let cond = var.is_null() || ((*var).attributes & 0x0000002) != 0 || ((*var).attributes & 0x0004000) != 0; - if cond { - retval = EXECUTION_FAILURE; - } else { - (*var).attributes &= ((*var).attributes as u32 ^ 0xffffffff as u32) as i32; - } - - libc::free(input_string as *mut c_void); - return retval; - } - - orig_input_string = input_string; - - let mut t = input_string; - while !ifs_chars.is_null() && *ifs_chars != 0 && - (*t == b' ' as c_char|| *t == b'\t' as c_char || *t == b'\n' as c_char) && - (ifs_cmap[*t as usize] != 0) { - t = (t as usize + 1) as *mut c_char; - } - input_string = t; - - while !(*list).next.is_null() { - varname = (*((*list).word)).word; - - if legal_identifier(varname) == 0 && - valid_array_reference(varname, vflags) == 0 { - sh_invalidid(varname); - libc::free(orig_input_string as *mut c_void); - return EXECUTION_FAILURE; - } - - if *input_string != 0 { - t = get_word_from_string(std::mem::transmute(&input_string), ifs_chars, std::mem::transmute(&e)); - if !t.is_null() { *e = b'\0' as c_char;} - - if !t.is_null() && saw_escape != 0 { - let t1 = dequote_string(t); - var = bind_read_variable(varname, t1); - libc::free(t1 as *mut c_void); - } else { - var = bind_read_variable(varname, if !t.is_null() {t} else {"".as_ptr() as *mut c_char}); - } - } else { - t = PT_NULL as *mut c_char; - var = bind_read_variable(varname, "".as_ptr() as *mut c_char); - } - - if !t.is_null() { - libc::free(t as *mut c_void); - } - - if var.is_null() { - libc::free(orig_input_string as *mut c_void); - return EXECUTION_FAILURE; - } - - stupidly_hack_special_variables(varname); - (*var).attributes &= ((*var).attributes as u32 ^ 0xffffffff as u32) as i32; - - list = (*list).next; - } - - if legal_identifier((*((*list).word)).word) == 0 && - valid_array_reference((*((*list).word)).word, vflags) == 0 { - sh_invalidid((*((*list).word)).word); - libc::free(orig_input_string as *mut c_void); - return EXECUTION_FAILURE; - } - - tofree = PT_NULL as *mut c_char; - if *input_string != 0 { - t1 = input_string; - t = get_word_from_string(std::mem::transmute(&input_string), ifs_chars, std::mem::transmute(&e)); - if *input_string == 0 { - input_string = t; - tofree = input_string; - } else { - input_string = strip_trailing_ifs_whitespace(t1, ifs_chars, saw_escape); - tofree = t; - } - } - - if saw_escape != 0 && !input_string.is_null() && *input_string != 0 { - t = dequote_string(input_string); - var = bind_read_variable((*((*list).word)).word, t); - libc::free(t as *mut c_void); - } else { - var = bind_read_variable((*((*list).word)).word, if !input_string.is_null() {input_string} else {"".as_ptr() as *mut c_char}); - } - - if !var.is_null() { - stupidly_hack_special_variables((*((*list).word)).word); - (*var).attributes &= ((*var).attributes as u32 ^ 0xffffffff as u32) as i32; - } else { - retval = EXECUTION_FAILURE; - } - - if !tofree.is_null() { - libc::free(tofree as *mut c_void); - } - libc::free(orig_input_string as *mut c_void); - return retval; -} -} - -/* ---------------------------------------------------------------------------------- */ - -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; +#[repr(C)] +#[derive(Copy,Clone)] +pub struct WORD_LIST { + next: *mut WORD_LIST, + word: *mut WORD_DESC } -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); +/* +Read a line from the standard input and split it into fields. - if v.is_null() { - return v; - } else { - if ((*v).attributes & 0x0000002) != 0 || ((*v).attributes & 0x0004000) != 0 { - return PT_NULL as *mut SHELL_VAR; - } else { - return v; - } - } -} -} - -fn read_mbchar(fd: c_int, string: *mut c_char, ind: c_int, ch: c_int, unbuffered: c_int) -> c_int { - let mut i: size_t = 1; - let mut r: ssize_t; - let mut c: c_char = 0; - let mut ret: ssize_t; +Reads a single line from the standard input, or from file descriptor FD +if the -u option is supplied. The line is split into fields as with word +splitting, and the first word is assigned to the first NAME, the second +word to the second NAME, and so on, with any leftover words assigned to +the last NAME. Only the characters found in $IFS are recognized as word +delimiters. -unsafe { - let mut mbchar: [c_char; MB_LEN_MAX as usize + 1] = std::mem::zeroed(); - let mut ps: mbstate_t = std::mem::zeroed(); - let mut ps_back: mbstate_t = std::mem::zeroed(); - let mut wc: libc::wchar_t = std::mem::zeroed(); - -'out: loop { - mbchar[0] = ch as c_char; - for n in 0..= MB_LEN_MAX { - ps_back = ps; - ret = mbrtowc(std::mem::transmute(&wc), std::mem::transmute(&mbchar), i, std::mem::transmute(&ps)) as ssize_t; - if ret == -2 { - ps = ps_back; - - /* We don't want to be interrupted during a multibyte char read */ - if unbuffered == 2 { - r = zreadn(fd, std::mem::transmute(&c), 1); - } else if unbuffered != 0 { - r = zread(fd, std::mem::transmute(&c), 1); - } else { - r = zreadc(fd, std::mem::transmute(&c)); - } - if r <= 0 { - break 'out; - } - mbchar[i] = c; - i += 1; - continue; - } else if ret == -1 || ret == 0 || ret > 0 { - break; - } - } - break 'out; -} - if i > 1 { - r = 1; - while r < i as isize { - *((string as usize + ind as usize + r as usize -1) as *mut c_char) = mbchar[r as size_t]; - - r += 1; - } - } - return (i - 1) as c_int; -} -} - -fn quit() { -unsafe { - if terminating_signal != 0 { - termsig_handler(terminating_signal); - } - - if interrupt_state != 0 { - throw_to_top_level(); - } -} -} - -fn check_alrm() { - unsafe { - if sigalrm_seen != 0 { - siglongjmp (std::mem::transmute(&alrmbuf), 1); - } - } -} - -static mut old_attempted_completion_function: usize = 0; - -pub fn reset_attempted_completion_function(cp: *mut c_char) -{ -unsafe { - if rl_attempted_completion_function as usize == 0 && - old_attempted_completion_function as usize != 0 { - rl_attempted_completion_function = std::mem::transmute(old_attempted_completion_function); - } -} -} - -static mut old_startup_hook: usize = 0; -static mut deftext: *mut c_char = PT_NULL as *mut c_char; - -fn set_itext() -> c_int -{ - let mut r1 = 0; - let mut r2 = 0; - -unsafe { - if old_startup_hook != 0 { - let fp: rl_hook_func_t = std::mem::transmute(old_startup_hook); - r1 = fp(); - } - if !deftext.is_null() { - r2 = rl_insert_text(deftext as *const c_char); - deftext = PT_NULL as *mut c_char; - rl_startup_hook = std::mem::transmute(old_startup_hook); - old_startup_hook = std::mem::transmute(0 as usize); - } -} - 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(); - } - - 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; - } - - 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; -} -} - -fn reset_alarm() -{ -unsafe { - falarm(0, 0); - set_signal_handler(libc::SIGALRM, old_alrm); -} -} - -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; -} -} - -#[no_mangle] -pub extern "C" fn read_tty_cleanup() -{ -unsafe { - if tty_modified != 0 { - ttyrestore(); - } -} -} +If no NAMEs are supplied, the line read is stored in the REPLY variable. +*/ #[no_mangle] -pub extern "C" fn read_tty_modified() -> c_int -{ -unsafe { - return tty_modified; - } -} - -static mut old_delim_ctype: c_int = 0; -static mut old_delim_func: usize = 0; -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 reset_eol_delim(cp: *mut c_char) -{ -unsafe { - let cmap = rl_get_keymap(); - let n = std::mem::size_of_val(&*cmap); - let ret_pos = (b'M' & 0x1f) as usize * n; - let delim_pos = (delim_char & 0x1f) as usize * n; - - (*((cmap as usize + ret_pos) as Keymap)).tp = old_newline_ctype as c_char; - (*((cmap as usize + ret_pos) as Keymap)).function = std::mem::transmute(old_newline_func); - - (*((cmap as usize + delim_pos) as Keymap)).tp = old_delim_ctype as c_char; - (*((cmap as usize + delim_pos) as Keymap)).function = std::mem::transmute(old_delim_func); -} -} +pub extern "C" fn r_read_builtin(list: *mut WORD_LIST) -> i32 { + println!("r_read_builtin call"); + 0 +} \ No newline at end of file diff --git a/bash-5.1/execute_cmd.c b/bash-5.1/execute_cmd.c index 9d66af0b4ee4f01b484fbcebdc4ddfdc75e156e7..ecd509c32581a4e37a2b5e4ab5eb4bb138891e59 100644 --- a/bash-5.1/execute_cmd.c +++ b/bash-5.1/execute_cmd.c @@ -4737,7 +4737,7 @@ execute_builtin (builtin, words, flags, subshell) error_trap = 0; should_keep = 0; - //r_execute_cmd(); + r_execute_cmd(); /* The eval builtin calls parse_and_execute, which does not know about the setting of flags, and always calls the execution functions with flags that will exit the shell on an error if -e is set. If the @@ -4841,7 +4841,6 @@ execute_builtin (builtin, words, flags, subshell) executing_builtin++; executing_command_builtin |= builtin == command_builtin; result = ((*builtin) (words->next)); - // r_execute_cmd2(words->next); /* This shouldn't happen, but in case `return' comes back instead of longjmp'ing, we need to unwind. */ diff --git a/bash-5.1/rsbuiltins.h b/bash-5.1/rsbuiltins.h index f6f3be36af13058d9fcb82d8cc048c5a0921f165..f3b7f9be9d9ad1014d1aab6e2550fd762765ebc1 100644 --- a/bash-5.1/rsbuiltins.h +++ b/bash-5.1/rsbuiltins.h @@ -1,4 +1,2 @@ #include -#include "command.h" int r_execute_cmd(); -int r_execute_cmd2(WORD_LIST *l); diff --git a/bash-5.1/src/lib.rs b/bash-5.1/src/lib.rs index 39f431c556dcf9dc6f02557a42876c3844fdda25..874f7e601d0de1901eee964f3b7613a623bc330a 100644 --- a/bash-5.1/src/lib.rs +++ b/bash-5.1/src/lib.rs @@ -1,42 +1,5 @@ -use libc::{c_char, c_int, c_long}; -use std::ffi::CStr; -use std::str; - -#[repr(C)] -pub struct WORD_DESC { - pub word : *mut c_char, - pub flags : c_int -} - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct WORD_LIST { - next : *mut WORD_LIST, - word : *mut WORD_DESC -} - - -//#[link(name = "")] -//extern { - // pub fn printf( #[no_mangle] pub extern "C" fn r_execute_cmd() { - //println!("hello"); - //common::builtin_error("test error") + println!("hello"); } -#[no_mangle] -pub extern "C" fn r_execute_cmd2(l : *mut WORD_LIST) -> i32 { - unsafe { - let mut it : *mut WORD_LIST = l; - while std::ptr::null() != it { - //let mut a = (&((* ((*l).word)).word) ); - let mut a :*mut c_char =( *(*it).word).word; - let c_str: &CStr = CStr::from_ptr(a); - let str_slice: &str = c_str.to_str().unwrap(); - println! ("word is {:?}", str_slice); - it = (*it).next; - } - } - 0 -} diff --git a/record.txt b/record.txt index 0790a9461ab330126ce18994c948b92b1873ecd7..9b2408960210e3549963ce8b59ecd371c5b08b9a 100644 --- a/record.txt +++ b/record.txt @@ -9,3 +9,4 @@ 8 9 10 +11