From 00f8f2ab0af5a5dc1f017829a6b4355529d56b6d Mon Sep 17 00:00:00 2001 From: zhanghuanhuan Date: Mon, 14 Aug 2023 16:54:24 +0800 Subject: [PATCH] Merge realize some command translation into rustdev --- bash-5.1/builtins_rust/alias/src/lib.rs | 15 +-- bash-5.1/builtins_rust/common/src/lib.rs | 5 +- bash-5.1/builtins_rust/fc/src/lib.rs | 158 ++++++++++++----------- bash-5.1/builtins_rust/help/src/lib.rs | 2 +- bash-5.1/builtins_rust/jobs/src/lib.rs | 1 - bash-5.1/builtins_rust/umask/src/lib.rs | 12 +- record.txt | 1 + 7 files changed, 93 insertions(+), 101 deletions(-) diff --git a/bash-5.1/builtins_rust/alias/src/lib.rs b/bash-5.1/builtins_rust/alias/src/lib.rs index 2ff7814..c9e53be 100644 --- a/bash-5.1/builtins_rust/alias/src/lib.rs +++ b/bash-5.1/builtins_rust/alias/src/lib.rs @@ -212,20 +212,17 @@ unsafe extern "C" fn print_alias( alias: *mut AliasT, flags: libc::c_int) { let value: *mut libc::c_char; value = sh_single_quote((*alias).value); if flags & 0x1 as libc::c_int != 0 { - print!("alias "); - //printf( - // b"alias %s\0" as *const u8 as *const libc::c_char, + printf( + b"alias %s\0" as *const u8 as *const libc::c_char, if !((*alias).name).is_null() && *((*alias).name).offset(0 as libc::c_int as isize) as libc::c_int == '-' as i32 { - // b"-- \0" as *const u8 as *const libc::c_char - print!("-- "); + b"-- \0" as *const u8 as *const libc::c_char } else { - // b"\0" as *const u8 as *const libc::c_char - print!(" "); - } - //); + b"\0" as *const u8 as *const libc::c_char + }, + ); } println!("{}={}", CStr::from_ptr((*alias).name).to_string_lossy().into_owned(), CStr::from_ptr(value).to_string_lossy().into_owned()); diff --git a/bash-5.1/builtins_rust/common/src/lib.rs b/bash-5.1/builtins_rust/common/src/lib.rs index 332e7e2..696f3bc 100644 --- a/bash-5.1/builtins_rust/common/src/lib.rs +++ b/bash-5.1/builtins_rust/common/src/lib.rs @@ -1757,7 +1757,7 @@ pub extern "C" fn r_builtin_unbind_variable(vname:*const c_char)->i32{ pub extern "C" fn get_local_str()-> Vec{ let lang : String; - match var("LANG") { + match var("LANGUAGE") { Ok(v) => lang = v , Err(e) => { @@ -1767,8 +1767,7 @@ pub extern "C" fn get_local_str()-> Vec{ } // println!("now language is {:?}",lang); //parse() 用于类型转换 - let v: Vec<_> = lang.split('.').collect(); - let langid : LanguageIdentifier = v[0].parse().expect("wrong language"); + let langid : LanguageIdentifier = lang.parse().expect("wrong language"); let locales = vec![langid.into()]; return locales; } diff --git a/bash-5.1/builtins_rust/fc/src/lib.rs b/bash-5.1/builtins_rust/fc/src/lib.rs index 357b603..f572838 100644 --- a/bash-5.1/builtins_rust/fc/src/lib.rs +++ b/bash-5.1/builtins_rust/fc/src/lib.rs @@ -1,9 +1,8 @@ extern crate libc; extern crate nix; -use libc::{c_char, c_long, c_void, c_int}; -use nix::sys::termios::SpecialCharacterIndices; -use std::{ffi::{CString,CStr}, i32, io::{Write, stdout}, ops::Add, string, u32}; +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)] @@ -389,7 +388,6 @@ extern "C" { static mut echo_input_at_read:i32; static mut verbose_flag:i32; fn builtin_help(); - } #[no_mangle] @@ -400,19 +398,22 @@ pub extern "C" fn r_set_verbose_flag (){ } #[no_mangle] -pub extern "C" fn r_fc_number(list:*mut WordList)->i32 +pub extern "C" fn r_fc_number (list:* mut WordList)->i32 { - let mut s:*mut c_char; - unsafe{ - if list.is_null(){ - return 0; - } - s = (*(*list).word).word; - if *s as c_int == '-' as i32{ - s = s.offset(1); + let mut s:*mut c_char = 0 as *mut libc::c_char; + + if list == std::ptr::null_mut(){ + return 0; + } + + unsafe { + if (*list).word != std::ptr::null_mut() { + s = (*(*list).word).word; + if char::from(*s as u8 ) == '-' { + s= s.offset(1) ; } - let res = legal_number(s,std::ptr::null_mut()); - return res; + } + return legal_number (s, std::ptr::null_mut()); } } @@ -476,7 +477,7 @@ unsafe fn STREQN ( a:* const c_char, b:* const c_char, n:i32)->bool { } #[no_mangle] -pub extern "C" fn r_fc_builtin (mut list:* mut WordList)->i32 +pub extern "C" fn r_fc_builtin (list:* mut WordList)->i32 { let mut i:i32; let mut sep:*mut c_char; @@ -511,34 +512,33 @@ pub extern "C" fn r_fc_builtin (mut list:* mut WordList)->i32 lcurrent = list; loptend = lcurrent; - while r_fc_number (loptend) ==0{ - opt = internal_getopt (list, CString::new(":e:lnrs").unwrap().as_ptr() as * mut c_char); - if opt != -1 { - let optu8:u8= opt as u8; - let optChar:char=char::from(optu8); - match optChar{ - 'n' => numbering=0, - 'l' => listing = HN_LISTING!(), - 'r' => reverse = 1, - 's' => execute = 1, - 'e' => ename = list_optarg, - _ => { - if opt == -99 { - builtin_help(); - return EX_USAGE; - } - r_builtin_usage(); + let mut ret:bool=r_fc_number (loptend) ==0; + opt = internal_getopt (list, CString::new(":e:lnrs").unwrap().as_ptr() as * mut c_char); + ret= ret && (opt !=-1); + while ret { + let optu8:u8= opt as u8; + let optChar:char=char::from(optu8); + match optChar{ + 'n'=>{numbering=0;} + 'l'=>{listing = HN_LISTING!();} + 'r'=>{reverse = 1;} + 's'=>{execute = 1;} + 'e'=>{ename = list_optarg;} + _=>{ + if opt == -99 { + builtin_help(); return EX_USAGE; - } } - } - else{ - break; - } + r_builtin_usage(); + return EX_USAGE; + } + } loptend = lcurrent; + let mut ret:bool=r_fc_number (loptend) ==0; + opt = internal_getopt (list, CString::new(":e:lnrs").unwrap().as_ptr() as * mut c_char); + ret= ret && (opt !=-1); } - - list = loptend; + let mut llist:* mut WordList = loptend.clone(); if ename != std::ptr::null_mut() && char::from(*ename as u8 ) == '-' && char::from(*((ename as usize +4) as * mut c_char) as u8 )== '\0'{ execute = 1; @@ -548,14 +548,14 @@ pub extern "C" fn r_fc_builtin (mut list:* mut WordList)->i32 rlist = std::ptr::null_mut(); let mut ret:bool=loptend !=std::ptr::null_mut(); - sep= libc::strchr((*(*list).word).word, char::from('=') as libc::c_int); + sep= libc::strchr((*(*llist).word).word, char::from('=') as libc::c_int); ret=ret && sep != std::ptr::null_mut(); while ret { sep= (sep as usize + 4) as * mut c_char; *sep = char::from('\0') as c_char ; rl = libc::malloc ( std::mem::size_of::<& REPL>() ) as * mut REPL; (*rl).next = std::ptr::null_mut(); - (*rl).pat = savestring ((*(*list).word).word); + (*rl).pat = savestring ((*(*llist).word).word); (*rl).rep = savestring (sep); if rlist == std::ptr::null_mut(){ @@ -564,13 +564,13 @@ pub extern "C" fn r_fc_builtin (mut list:* mut WordList)->i32 (*rl).next = rlist; rlist = rl; } - list = (*list).next; + llist = (*llist).next; } rlist = REVERSE_LIST (rlist as * mut GENERIC_LIST); hlist = history_list (); - if list != std::ptr::null_mut() { - command=r_fc_gethist((*(*list).word).word, hlist, 0); + if llist != std::ptr::null_mut() { + command=r_fc_gethist((*(*llist).word).word, hlist, 0); } else { command=r_fc_gethist(std::ptr::null_mut(), hlist, 0); } @@ -621,15 +621,13 @@ pub extern "C" fn r_fc_builtin (mut list:* mut WordList)->i32 r_fc_replhist (command); return parse_and_execute (command, CString::new("fc").unwrap().as_ptr(), SEVAL_NOHIST!()); } - + hlist = history_list (); - if hlist == std::ptr::null_mut(){ return EXECUTION_SUCCESS!(); } i=0; - - while !(*hlist.offset(i as isize)).is_null(){ + while *((hlist as usize +(8*i) as usize) as * mut * mut HIST_ENTRY) != std::ptr::null_mut() { i+=1; } @@ -637,13 +635,13 @@ pub extern "C" fn r_fc_builtin (mut list:* mut WordList)->i32 last_hist = i - rh - hist_last_line_added; real_last = i; - while (*hlist.offset(real_last as isize)).is_null() && real_last > 0 + while *((hlist as usize +(8*real_last) as usize ) as * mut * mut HIST_ENTRY) == std::ptr::null_mut() && real_last > 0 { real_last-=1; } - if i == last_hist && (*hlist.offset(last_hist as isize)).is_null() { - while last_hist >= 0 && (*hlist.offset(last_hist as isize)).is_null() + if i == last_hist && *((hlist as usize + (8*last_hist) as usize) as * mut * mut HIST_ENTRY) == std::ptr::null_mut() { + while last_hist >= 0 && *((hlist as usize + (8*last_hist) as usize) as * mut * mut HIST_ENTRY) == std::ptr::null_mut() { last_hist-=1; } @@ -653,19 +651,27 @@ pub extern "C" fn r_fc_builtin (mut list:* mut WordList)->i32 last_hist = 0; } - if !(list.is_null()) { - histbeg = r_fc_gethnum ((*(*list).word).word, hlist, listing| HN_FIRST!()); - list = (*list).next; + if llist != std::ptr::null_mut() { + histbeg = r_fc_gethnum ((*(*llist).word).word, hlist, listing| HN_FIRST!()); + llist = (*llist).next; - if list != std::ptr::null_mut(){ - histend = r_fc_gethnum ((*(*list).word).word, hlist, listing); - } else if histbeg == real_last { - histend = if listing != 0 { real_last } else { histbeg }; - } else { - histend = if listing != 0 { last_hist } else { histbeg } - } + if list != std::ptr::null_mut(){ + histend = r_fc_gethnum ((*(*list).word).word, hlist, listing); + } else if histbeg == real_last { + if listing >0 { + histend=real_last; + }else { + histend=histbeg; + } + } else { + if listing >0 { + histend=last_hist; + }else { + histend=histbeg; + } + } } else { - if listing != 0 { + if listing > 0 { histend = last_hist; histbeg = histend - 16 + 1; if histbeg < 0{ @@ -780,12 +786,12 @@ pub extern "C" fn r_fc_builtin (mut list:* mut WordList)->i32 if stream != std::ptr::null_mut(){ libc::fprintf (stream, CString::new ("%d").unwrap().as_ptr(), i + history_base); }else { - let diff =i + history_base; + let diff =i + history_base; printToStdout(CString::new (diff.to_string()).unwrap().as_ptr() as * mut c_char ); } } - if listing !=0 { + if listing !=0 { if posixly_correct !=0 { if stream != std::ptr::null_mut(){ libc::fputs (CString::new ("\t").unwrap().as_ptr(), stream); @@ -815,9 +821,7 @@ pub extern "C" fn r_fc_builtin (mut list:* mut WordList)->i32 libc::fprintf (stream, CString::new ("%s\n").unwrap().as_ptr(), (**((hlist as usize + (i*8) as usize) as *mut*mut HIST_ENTRY)).line); }else { printToStdout((**((hlist as usize + (i*8) as usize) as *mut*mut HIST_ENTRY)).line); - printToStdout(CString::new ("\n").unwrap().as_ptr() as * mut c_char ); } - //printToStdoutflush(); ret=reverse !=0; if ret { @@ -946,9 +950,10 @@ pub extern "C" fn r_fc_gethnum (command:* mut c_char, hlist:* mut * mut HIST_ENT listing = mode & HN_LISTING!(); sign = 1; /* Count history elements. */ - while !(*hlist.offset(i as isize)).is_null() { + while *((hlist as usize + (8*i) as usize ) as * mut * mut HIST_ENTRY) != std::ptr::null_mut() { i+=1; } + /* With the Bash implementation of history, the current command line ("fc blah..." and so on) is already part of the history list by the time we get to this point. This just skips over that command @@ -964,8 +969,8 @@ pub extern "C" fn r_fc_gethnum (command:* mut c_char, hlist:* mut * mut HIST_ENT rh = (remember_on_history !=0 || ((subshell_environment & SUBSHELL_COMSUB!()) !=0 && enable_history_list !=0)) as i32; last_hist = i - rh - hist_last_line_added; - if i == last_hist && (*hlist.offset(last_hist as isize)).is_null() { - while last_hist >= 0 && (*hlist.offset(last_hist as isize)).is_null(){ + if i == last_hist && *((hlist as usize + (8*last_hist) as usize ) as * mut * mut HIST_ENTRY) == std::ptr::null_mut() { + while last_hist >= 0 && *((hlist as usize + (8*last_hist) as usize ) as * mut * mut HIST_ENTRY) == std::ptr::null_mut(){ last_hist-=1; } } @@ -983,7 +988,7 @@ pub extern "C" fn r_fc_gethnum (command:* mut c_char, hlist:* mut * mut HIST_ENT } /* back up from the end to the last non-null history entry */ - while (*hlist.offset(real_last as isize)).is_null() && real_last > 0 { + while *((hlist as usize + (8*real_last) as usize ) as * mut * mut HIST_ENTRY) == std::ptr::null_mut() && real_last > 0 { real_last-=1; } @@ -992,13 +997,13 @@ pub extern "C" fn r_fc_gethnum (command:* mut c_char, hlist:* mut * mut HIST_ENT s = command; /* Handle possible leading minus sign. */ - if s != std::ptr::null_mut() && ( char::from(*s as u8) == '-') { + if s != std::ptr::null_mut() && ( char::from(*s as u8) != '-') { sign = -1; - s = s.offset(1) + s = (s as usize +4) as * mut c_char; } if s != std::ptr::null_mut() && DIGIT( *s ) { - n = libc::atoi(s); + n = std::ffi::CStr::from_ptr(s).to_str().unwrap().parse::().unwrap(); n *= sign; /* We want to return something that is an offset to HISTORY_BASE. */ @@ -1016,7 +1021,7 @@ pub extern "C" fn r_fc_gethnum (command:* mut c_char, hlist:* mut * mut HIST_ENT } } else if n == 0 { if sign == -1 { - if listing != 0 { + if listing > 0 { return real_last; } else { return HIST_INVALID!(); @@ -1051,8 +1056,7 @@ pub extern "C" fn r_fc_gethnum (command:* mut c_char, hlist:* mut * mut HIST_ENT clen = libc::strlen (command as * const c_char) as i32; j = i; while j >= 0 { - if STREQN (command, (*(*hlist.offset(j as isize))).line, clen) { - //if STREQN (command, (*(*((hlist as usize + (8*j) as usize ) as * mut * mut HIST_ENTRY))).line, clen) { + if STREQN (command, (*(*((hlist as usize + (8*j) as usize ) as * mut * mut HIST_ENTRY))).line, clen) { return j; } j-=1; diff --git a/bash-5.1/builtins_rust/help/src/lib.rs b/bash-5.1/builtins_rust/help/src/lib.rs index a5512ba..e4b9f7b 100644 --- a/bash-5.1/builtins_rust/help/src/lib.rs +++ b/bash-5.1/builtins_rust/help/src/lib.rs @@ -305,7 +305,7 @@ fn open_helpfile(name :*mut c_char) -> i32{ fn show_longdoc(i : i32){ let builtin1 = unsafe{&(*((shell_builtins as usize + (i*BUILTIN_SIZEOF!()) as usize) as *mut builtin))}; - let mgr = ResourceManager::new("/usr/share/utshell/resources/{locale}/{res_id}".into()); + let mgr = ResourceManager::new("./resources/{locale}/{res_id}".into()); let resources = vec![ "message.ftl".into()]; let mut args = FluentArgs::new(); let c_str: &CStr = unsafe { CStr::from_ptr(builtin1.name) }; diff --git a/bash-5.1/builtins_rust/jobs/src/lib.rs b/bash-5.1/builtins_rust/jobs/src/lib.rs index e072ee5..6b9fa88 100644 --- a/bash-5.1/builtins_rust/jobs/src/lib.rs +++ b/bash-5.1/builtins_rust/jobs/src/lib.rs @@ -534,7 +534,6 @@ pub extern "C" fn r_disown_builtin (list:* mut WordList)->libc::c_int { return EX_USAGE; } } - opt = internal_getopt (list, c_str_ahr.as_ptr() as * mut c_char); } retval = EXECUTION_SUCCESS!(); diff --git a/bash-5.1/builtins_rust/umask/src/lib.rs b/bash-5.1/builtins_rust/umask/src/lib.rs index 5ad7504..399070d 100644 --- a/bash-5.1/builtins_rust/umask/src/lib.rs +++ b/bash-5.1/builtins_rust/umask/src/lib.rs @@ -247,15 +247,7 @@ pub extern "C" fn r_umask_builtin(mut list:*mut WordList) ->i32{ else{ /* Display the UMASK for this user. */ umask_arg = umask(0o22); umask(umask_arg); - if pflag != 0{ - if print_symbolically != 0{ - println!("umask -S"); - } - else{ - print!("umask ") - } - } -/* + if pflag != 0{ if print_symbolically != 0{ println!("umask \" -S\" "); @@ -264,7 +256,7 @@ pub extern "C" fn r_umask_builtin(mut list:*mut WordList) ->i32{ println!("umask \"\" ") } } -*/ + if print_symbolically != 0{ r_print_symbolic_umask(umask_arg); } diff --git a/record.txt b/record.txt index 5be0227..cbbece1 100644 --- a/record.txt +++ b/record.txt @@ -79,3 +79,4 @@ 78 79 80 +81 -- Gitee