From a2efbd63dcf3f494f55ba7189b2bf286d2cb20d1 Mon Sep 17 00:00:00 2001 From: wangmengc Date: Tue, 27 Jun 2023 17:22:28 +0800 Subject: [PATCH] check mapfile printf read suspend test time trap and merge refact common --- bash-5.1/builtins/history.def | 217 +----------------- bash-5.1/builtins/let.def | 5 + bash-5.1/builtins/mapfile.def | 5 + bash-5.1/builtins/printf.def | 7 + bash-5.1/builtins/return.def | 5 + bash-5.1/builtins/shift.def | 4 + bash-5.1/builtins/test.def | 6 + bash-5.1/builtins/times.def | 6 + bash-5.1/builtins/trap.def | 5 + bash-5.1/builtins_rust/alias/src/lib.rs | 6 +- bash-5.1/builtins_rust/bind/src/lib.rs | 6 +- bash-5.1/builtins_rust/caller/src/lib.rs | 58 +---- bash-5.1/builtins_rust/cd/src/lib.rs | 4 +- bash-5.1/builtins_rust/common/src/command.rs | 5 + bash-5.1/builtins_rust/common/src/lib.rs | 41 +--- bash-5.1/builtins_rust/common/src/shell.rs | 19 ++ bash-5.1/builtins_rust/exec/src/lib.rs | 148 ++++-------- bash-5.1/builtins_rust/exit/src/lib.rs | 33 +-- bash-5.1/builtins_rust/fc/Cargo.toml | 1 + bash-5.1/builtins_rust/fc/src/lib.rs | 37 +-- bash-5.1/builtins_rust/fg_bg/Cargo.toml | 1 + bash-5.1/builtins_rust/fg_bg/src/lib.rs | 45 +--- bash-5.1/builtins_rust/getopts/Cargo.toml | 1 + bash-5.1/builtins_rust/getopts/src/lib.rs | 32 +-- bash-5.1/builtins_rust/hash/src/lib.rs | 28 +-- bash-5.1/builtins_rust/help/Cargo.toml | 1 + bash-5.1/builtins_rust/help/src/lib.rs | 28 +-- bash-5.1/builtins_rust/history/src/lib.rs | 18 +- bash-5.1/builtins_rust/mapfile/Cargo.toml | 1 + .../builtins_rust/mapfile/src/intercdep.rs | 1 + bash-5.1/builtins_rust/mapfile/src/lib.rs | 3 + .../builtins_rust/printf/src/intercdep.rs | 5 +- bash-5.1/builtins_rust/printf/src/lib.rs | 28 ++- bash-5.1/builtins_rust/pushd/Cargo.toml | 1 + bash-5.1/builtins_rust/pushd/src/lib.rs | 58 ++--- bash-5.1/builtins_rust/read/src/lib.rs | 4 +- bash-5.1/builtins_rust/set/Cargo.toml | 2 +- bash-5.1/builtins_rust/set/src/lib.rs | 37 +-- bash-5.1/builtins_rust/shopt/src/lib.rs | 36 ++- bash-5.1/builtins_rust/source/Cargo.toml | 1 + bash-5.1/builtins_rust/source/src/lib.rs | 35 +-- bash-5.1/builtins_rust/type/Cargo.toml | 1 + bash-5.1/builtins_rust/type/src/lib.rs | 33 +-- bash-5.1/builtins_rust/ulimit/Cargo.toml | 1 + bash-5.1/builtins_rust/ulimit/src/lib.rs | 36 +-- bash-5.1/builtins_rust/umask/src/lib.rs | 30 +-- bash-5.1/builtins_rust/wait/src/lib.rs | 22 +- record.txt | 1 + 48 files changed, 285 insertions(+), 823 deletions(-) create mode 100644 bash-5.1/builtins_rust/common/src/command.rs create mode 100644 bash-5.1/builtins_rust/common/src/shell.rs diff --git a/bash-5.1/builtins/history.def b/bash-5.1/builtins/history.def index 5db44c2c..930ad500 100644 --- a/bash-5.1/builtins/history.def +++ b/bash-5.1/builtins/history.def @@ -100,224 +100,15 @@ static int expand_and_print_history PARAMS((WORD_LIST *)); #define CFLAG 0x40 #define DFLAG 0x80 +extern int r_history_builtin(WORD_LIST *); + int history_builtin (list) WORD_LIST *list; { - int flags, opt, result, old_history_lines, obase, ind; - char *filename, *delete_arg, *range; - intmax_t delete_offset; - - flags = 0; - reset_internal_getopt (); - while ((opt = internal_getopt (list, "acd:npsrw")) != -1) - { - switch (opt) - { - case 'a': - flags |= AFLAG; - break; - case 'c': - flags |= CFLAG; - break; - case 'n': - flags |= NFLAG; - break; - case 'r': - flags |= RFLAG; - break; - case 'w': - flags |= WFLAG; - break; - case 's': - flags |= SFLAG; - break; - case 'd': - flags |= DFLAG; - delete_arg = list_optarg; - break; - case 'p': -#if defined (BANG_HISTORY) - flags |= PFLAG; -#endif - break; - CASE_HELPOPT; - default: - builtin_usage (); - return (EX_USAGE); - } - } - list = loptend; - - opt = flags & (AFLAG|RFLAG|WFLAG|NFLAG); - if (opt && opt != AFLAG && opt != RFLAG && opt != WFLAG && opt != NFLAG) - { - builtin_error (_("cannot use more than one of -anrw")); - return (EXECUTION_FAILURE); - } - - /* clear the history, but allow other arguments to add to it again. */ - if (flags & CFLAG) - { - bash_clear_history (); - if (list == 0) - return (EXECUTION_SUCCESS); - } - - if (flags & SFLAG) - { - if (list) - push_history (list); - return (EXECUTION_SUCCESS); - } -#if defined (BANG_HISTORY) - else if (flags & PFLAG) - { - if (list) - return (expand_and_print_history (list)); - return (sh_chkwrite (EXECUTION_SUCCESS)); - } -#endif - else if ((flags & DFLAG) && (range = strchr ((delete_arg[0] == '-') ? delete_arg + 1 : delete_arg, '-'))) - { - intmax_t delete_start, delete_end; - *range++ = '\0'; - if (legal_number (delete_arg, &delete_start) == 0 || legal_number (range, &delete_end) == 0) - { - range[-1] = '-'; - sh_erange (delete_arg, _("history position")); - return (EXECUTION_FAILURE); - } - if (delete_arg[0] == '-' && delete_start < 0) - { - /* the_history[history_length] == 0x0, so this is correct */ - delete_start += history_length; - if (delete_start < history_base) - { -start_error: - sh_erange (delete_arg, _("history position")); - return (EXECUTION_FAILURE); - } - } - /* numbers as displayed by display_history are offset by history_base */ - else if (delete_start > 0) - delete_start -= history_base; - if (delete_start < 0 || delete_start >= history_length) - goto start_error; - if (range[0] == '-' && delete_end < 0) - { - delete_end += history_length; - if (delete_end < history_base) - { -range_error: - sh_erange (range, _("history position")); - return (EXECUTION_FAILURE); - } - } - else if (delete_end > 0) - delete_end -= history_base; - if (delete_end < 0 || delete_end >= history_length) - goto range_error; - result = bash_delete_history_range (delete_start, delete_end); - if (where_history () > history_length) - history_set_pos (history_length); - return (result ? EXECUTION_SUCCESS : EXECUTION_FAILURE); - } - else if (flags & DFLAG) - { - if (legal_number (delete_arg, &delete_offset) == 0) - { - sh_erange (delete_arg, _("history position")); - return (EXECUTION_FAILURE); - } - /* check for negative offsets, count back from end of list */ - if (delete_arg[0] == '-' && delete_offset < 0) - { - /* since the_history[history_length] == 0x0, this calculation means - that history -d -1 will delete the last history entry, which at - this point is the history -d -1 we just added. */ - ind = history_length + delete_offset; - if (ind < history_base) - { - sh_erange (delete_arg, _("history position")); - return (EXECUTION_FAILURE); - } - opt = ind + history_base; /* compensate for opt - history_base below */ - } - else if ((delete_offset < history_base) || (delete_offset >= (history_base + history_length))) - { - sh_erange (delete_arg, _("history position")); - return (EXECUTION_FAILURE); - } - else - opt = delete_offset; - - /* Positive arguments from numbers as displayed by display_history need - to be offset by history_base */ - result = bash_delete_histent (opt - history_base); - /* Since remove_history changes history_length, this can happen if - we delete the last history entry. */ - if (where_history () > history_length) - history_set_pos (history_length); - return (result ? EXECUTION_SUCCESS : EXECUTION_FAILURE); - } - else if ((flags & (AFLAG|RFLAG|NFLAG|WFLAG|CFLAG)) == 0) - { - result = display_history (list); - return (sh_chkwrite (result)); - } + return r_history_builtin(list); - filename = list ? list->word->word : get_string_value ("HISTFILE"); - result = EXECUTION_SUCCESS; - -#if defined (RESTRICTED_SHELL) - if (restricted && strchr (filename, '/')) - { - sh_restricted (filename); - return (EXECUTION_FAILURE); - } -#endif - - if (flags & AFLAG) /* Append session's history to file. */ - result = maybe_append_history (filename); - else if (flags & WFLAG) /* Write entire history. */ - result = write_history (filename); - else if (flags & RFLAG) /* Read entire file. */ - { - result = read_history (filename); - history_lines_in_file = history_lines_read_from_file; - /* history_lines_in_file = where_history () + history_base - 1; */ - } - else if (flags & NFLAG) /* Read `new' history from file. */ - { - /* Read all of the lines in the file that we haven't already read. */ - old_history_lines = history_lines_in_file; - obase = history_base; - - using_history (); - result = read_history_range (filename, history_lines_in_file, -1); - using_history (); - - history_lines_in_file = history_lines_read_from_file; - /* history_lines_in_file = where_history () + history_base - 1; */ - - /* If we're rewriting the history file at shell exit rather than just - appending the lines from this session to it, the question is whether - we reset history_lines_this_session to 0, losing any history entries - we had before we read the new entries from the history file, or - whether we count the new entries we just read from the file as - history lines added during this session. - Right now, we do the latter. This will cause these history entries - to be written to the history file along with any intermediate entries - we add when we do a `history -a', but the alternative is losing - them altogether. */ - if (force_append_history == 0) - history_lines_this_session += history_lines_in_file - old_history_lines + - history_base - obase; - } - - return (result ? EXECUTION_FAILURE : EXECUTION_SUCCESS); -} + } /* Accessors for HIST_ENTRY lists that are called HLIST. */ #define histline(i) (hlist[(i)]->line) diff --git a/bash-5.1/builtins/let.def b/bash-5.1/builtins/let.def index d090a45b..f4dad260 100644 --- a/bash-5.1/builtins/let.def +++ b/bash-5.1/builtins/let.def @@ -78,11 +78,16 @@ $END #include "../shell.h" #include "common.h" +extern r_let_builtin(WORD_LIST *); + /* Arithmetic LET function. */ int let_builtin (list) WORD_LIST *list; { + + return r_let_builtin(list); + intmax_t ret; int expok; diff --git a/bash-5.1/builtins/mapfile.def b/bash-5.1/builtins/mapfile.def index 65c3cb4f..831aeb39 100644 --- a/bash-5.1/builtins/mapfile.def +++ b/bash-5.1/builtins/mapfile.def @@ -103,6 +103,8 @@ static int run_callback PARAMS((const char *, unsigned int, const char *)); #define MAPF_CLEARARRAY 0x01 #define MAPF_CHOP 0x02 +extern int r_mapfile_builtin(WORD_LIST *); + static int delim; static int @@ -244,6 +246,9 @@ int mapfile_builtin (list) WORD_LIST *list; { + + return r_mapfile_builtin(list); + int opt, code, fd, flags; intmax_t intval; long lines, origin, nskip, callback_quantum; diff --git a/bash-5.1/builtins/printf.def b/bash-5.1/builtins/printf.def index 0a5f4897..5b51a97f 100644 --- a/bash-5.1/builtins/printf.def +++ b/bash-5.1/builtins/printf.def @@ -235,10 +235,16 @@ static intmax_t tw; static char *conv_buf; static size_t conv_bufsize; + +extern r_printf_builtin(WORD_LIST *); + int printf_builtin (list) WORD_LIST *list; { + + return r_printf_builtin(list); + int ch, fieldwidth, precision; int have_fieldwidth, have_precision; char convch, thisch, nextch, *format, *modstart, *fmt, *start; @@ -309,6 +315,7 @@ printf_builtin (list) return (EXECUTION_SUCCESS); format = list->word->word; + tw = 0; garglist = orig_arglist = list->next; diff --git a/bash-5.1/builtins/return.def b/bash-5.1/builtins/return.def index 03c98eb1..bc244108 100644 --- a/bash-5.1/builtins/return.def +++ b/bash-5.1/builtins/return.def @@ -50,6 +50,8 @@ $END #include "common.h" #include "bashgetopt.h" +extern r_return_builtin(WORD_LIST *); + /* If we are executing a user-defined function then exit with the value specified as an argument. if no argument is given, then the last exit status is used. */ @@ -57,6 +59,9 @@ int return_builtin (list) WORD_LIST *list; { + + return r_return_builtin(list); + CHECK_HELPOPT (list); return_catch_value = get_exitstat (list); diff --git a/bash-5.1/builtins/shift.def b/bash-5.1/builtins/shift.def index bb9af01b..32584dd7 100644 --- a/bash-5.1/builtins/shift.def +++ b/bash-5.1/builtins/shift.def @@ -49,6 +49,8 @@ $END int print_shift_error; +extern r_shift_builtin(WORD_LIST *); + /* Shift the arguments ``left''. Shift DOLLAR_VARS down then take one off of REST_OF_ARGS and place it into DOLLAR_VARS[9]. If LIST has anything in it, it is a number which says where to start the @@ -57,6 +59,8 @@ int shift_builtin (list) WORD_LIST *list; { + r_shift_builtin(list); + intmax_t times; int itimes, nargs; diff --git a/bash-5.1/builtins/test.def b/bash-5.1/builtins/test.def index bd9a203b..c190383d 100644 --- a/bash-5.1/builtins/test.def +++ b/bash-5.1/builtins/test.def @@ -130,10 +130,16 @@ $END #include "common.h" /* TEST/[ builtin. */ + +extern r_test_builtin(WORD_LIST *); + int test_builtin (list) WORD_LIST *list; { + + return r_test_builtin(list); + char **argv; int argc, result; diff --git a/bash-5.1/builtins/times.def b/bash-5.1/builtins/times.def index f31f4333..f8391c5b 100644 --- a/bash-5.1/builtins/times.def +++ b/bash-5.1/builtins/times.def @@ -57,11 +57,17 @@ $END #include "common.h" + +extern r_times_builtin(WORD_LIST *); + /* Print the totals for system and user time used. */ int times_builtin (list) WORD_LIST *list; { + + return r_times_builtin(list); + #if defined (HAVE_GETRUSAGE) && defined (HAVE_TIMEVAL) && defined (RUSAGE_SELF) struct rusage self, kids; diff --git a/bash-5.1/builtins/trap.def b/bash-5.1/builtins/trap.def index daeec9ea..d2ba5918 100644 --- a/bash-5.1/builtins/trap.def +++ b/bash-5.1/builtins/trap.def @@ -97,10 +97,15 @@ static int display_traps PARAMS((WORD_LIST *, int)); #define REVERT 1 /* Revert to this signals original value. */ #define IGNORE 2 /* Ignore this signal. */ +extern r_trap_builtin(WORD_LIST *); + int trap_builtin (list) WORD_LIST *list; { + + return r_trap_builtin(list); + int list_signal_names, display, result, opt; list_signal_names = display = 0; diff --git a/bash-5.1/builtins_rust/alias/src/lib.rs b/bash-5.1/builtins_rust/alias/src/lib.rs index 61a4b016..0bc7078c 100644 --- a/bash-5.1/builtins_rust/alias/src/lib.rs +++ b/bash-5.1/builtins_rust/alias/src/lib.rs @@ -1,7 +1,9 @@ use std::ffi::CStr; + +//extern crate rcommon; use rcommon::r_sh_notfound; +use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE,r_builtin_usage}; -use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; extern "C" { fn free(__ptr: *mut libc::c_void); fn dcgettext( @@ -59,8 +61,6 @@ pub type AliasT = alias; pub static AL_REUSABLE:i32 = 0x01; -//extern crate rcommon; -use rcommon::r_builtin_usage; #[no_mangle] diff --git a/bash-5.1/builtins_rust/bind/src/lib.rs b/bash-5.1/builtins_rust/bind/src/lib.rs index 1498452c..69fd41be 100644 --- a/bash-5.1/builtins_rust/bind/src/lib.rs +++ b/bash-5.1/builtins_rust/bind/src/lib.rs @@ -142,6 +142,7 @@ extern "C"{ fn strvec_len(array:*mut *mut c_char)->i32; fn rl_parse_and_bind(string:*mut c_char)->i32; fn strvec_search(array:*mut *mut c_char,name:*mut c_char)->i32; + fn builtin_help(); } @@ -246,7 +247,10 @@ pub extern "C" fn r_bind_builtin(mut list:*mut WordList)->i32{ } 'X' => flags |= XXFLAG!(), _ => { - + if opt == -99 { + builtin_help(); + return EX_USAGE; + } r_builtin_usage(); return_code = EX_USAGE; diff --git a/bash-5.1/builtins_rust/caller/src/lib.rs b/bash-5.1/builtins_rust/caller/src/lib.rs index 3209c7f5..a888b538 100644 --- a/bash-5.1/builtins_rust/caller/src/lib.rs +++ b/bash-5.1/builtins_rust/caller/src/lib.rs @@ -61,23 +61,13 @@ macro_rules! CHECK_HELPOPT { ($l:expr) => { if $l != std::ptr::null_mut() && (*($l)).word != std::ptr::null_mut() && ISHELP((*(*($l)).word).word) == true{ builtin_help(); - return EX_USAGE!(); + return EX_USAGE; } }; } -// #[macro_export] -// macro_rules! ISHELP { -// ($s:expr) => { -// let s_str = CString::new("--help").unwrap().as_ptr(); -// STREQ( ($s),s_str); -// }; -// } - - - type arrayind_t = intmax_t; -//extern c + extern "C" { static loptend:*mut WordList; @@ -157,7 +147,7 @@ pub extern "C" fn r_caller_builtin(mut list:*mut WordList)->i32{ } if no_options(list) != 0{ - return EX_USAGE!(); + return EX_USAGE; } list = loptend; /* skip over possible `--' */ @@ -209,7 +199,7 @@ pub extern "C" fn r_caller_builtin(mut list:*mut WordList)->i32{ else{ sh_invalidnum((*(*list).word).word); builtin_usage(); - return EX_USAGE!(); + return EX_USAGE; } return EXECUTION_SUCCESS!(); @@ -217,43 +207,3 @@ pub extern "C" fn r_caller_builtin(mut list:*mut WordList)->i32{ } - -/* -#ifdef LOADABLE_BUILTIN -static char *caller_doc[] = { -N_("Returns the context of the current subroutine call.\n\ - \n\ - Without EXPR, returns \"$line $filename\". With EXPR, returns\n\ - \"$line $subroutine $filename\"; this extra information can be used to\n\ - provide a stack trace.\n\ - \n\ - The value of EXPR indicates how many call frames to go back before the\n\ - current one; the top frame is frame 0."), - (char *)NULL -}; - -struct builtin caller_struct = { - "caller", - caller_builtin, - BUILTIN_ENABLED, - caller_doc, - "caller [EXPR]", - 0 -}; - -*/ - - - - - - - -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - let result = 2 + 2; - assert_eq!(result, 4); - } -} diff --git a/bash-5.1/builtins_rust/cd/src/lib.rs b/bash-5.1/builtins_rust/cd/src/lib.rs index bd94b2ce..1025580e 100644 --- a/bash-5.1/builtins_rust/cd/src/lib.rs +++ b/bash-5.1/builtins_rust/cd/src/lib.rs @@ -500,7 +500,7 @@ pub extern "C" fn r_cd_builtin (mut list:*mut WordList)->i32 { 'e'=>{eflag = 1;} _=>{ builtin_usage (); - return EX_USAGE!(); + return EX_USAGE; } } opt =internal_getopt (list, c_str_elp.as_ptr() as * mut c_char); @@ -661,7 +661,7 @@ pub extern "C" fn r_pwd_builtin (list:* mut WordList)->i32 { pflag = 1;} 'L'=>{verbatim_pwd = 0;} _=>{builtin_usage (); - return EX_USAGE!(); + return EX_USAGE; } } opt = internal_getopt (list, c_str_lp.as_ptr() as * mut c_char); diff --git a/bash-5.1/builtins_rust/common/src/command.rs b/bash-5.1/builtins_rust/common/src/command.rs new file mode 100644 index 00000000..c8ce39e3 --- /dev/null +++ b/bash-5.1/builtins_rust/common/src/command.rs @@ -0,0 +1,5 @@ + +#[macro_export] +macro_rules! SUBSHELL_PAREN { + () => { 0x02 } +} diff --git a/bash-5.1/builtins_rust/common/src/lib.rs b/bash-5.1/builtins_rust/common/src/lib.rs index 4a3a35e8..fa1361f8 100644 --- a/bash-5.1/builtins_rust/common/src/lib.rs +++ b/bash-5.1/builtins_rust/common/src/lib.rs @@ -8,7 +8,9 @@ use std::ptr::read_volatile; use nix::errno::errno; include!(concat!("lib_readline_keymaps.rs")); -pub static EXECUTION_SUCCESS:i32 = 0; + +include!(concat!("command.rs")); + //struct #[repr(C)] #[derive(Copy, Clone)] @@ -600,25 +602,7 @@ pub type QSFUNC = unsafe extern "C" fn(*const c_void,*const c_void)->i32; pub static EX_SUCCESS:i32 = 0; pub static EX_USAGE:i32 = 258; -/////// -pub static EX_BADUSAGE:i32 = 2; - -pub static EX_MISCERROR:i32 = 2; - -pub static EX_RETRYFAIL:i32 = 124; -pub static EX_WEXPCOMSUB:i32 = 125; -pub static EX_BINARY_FILE :i32 = 126; -pub static EX_NOEXEC: i32 = 126; -pub static EX_NOINPUT:i32 = 126; -pub static EX_NOTFOUND:i32 = 127; - -pub static EX_SHERRBASE:i32 = 256; /* all special error values are > this. */ - -pub static EX_BADSYNTAX:i32 = 257; /* shell syntax error */ -pub static EX_REDIRFAIL:i32 = 259; /* redirection failed */ -pub static EX_BADASSIGN :i32 = 260; /* variable assignment error */ -pub static EX_EXPFAIL:i32= 261;/* word expansion failed */ -pub static EX_DISKFALLBACK:i32 = 262; /* fall back to disk command from builtin */ +include!("./shell.rs"); //extern C extern "C"{ @@ -1769,20 +1753,3 @@ pub extern "C" fn r_builtin_unbind_variable(vname:*const c_char)->i32{ } } - -#[cfg(test)] -mod tests { - use super::*; - #[test] - fn it_works() { - let result = 2 + 2; - assert_eq!(result, 4); - } - - // #[test] - // fn no_args(){ - // let c_str = CString::new("123"); - // let c_ptr = c_str.as_ - // r_sh_invalidnum("12"); - // } -} diff --git a/bash-5.1/builtins_rust/common/src/shell.rs b/bash-5.1/builtins_rust/common/src/shell.rs new file mode 100644 index 00000000..a0a380b9 --- /dev/null +++ b/bash-5.1/builtins_rust/common/src/shell.rs @@ -0,0 +1,19 @@ +pub static EX_BADUSAGE:i32 = 2; + +pub static EX_MISCERROR:i32 = 2; + +pub static EX_RETRYFAIL:i32 = 124; +pub static EX_WEXPCOMSUB:i32 = 125; +pub static EX_BINARY_FILE :i32 = 126; +pub static EX_NOEXEC: i32 = 126; +pub static EX_NOINPUT:i32 = 126; +pub static EX_NOTFOUND:i32 = 127; + +pub static EX_SHERRBASE:i32 = 256; /* all special error values are > this. */ + +pub static EX_BADSYNTAX:i32 = 257; /* shell syntax error */ +pub static EX_REDIRFAIL:i32 = 259; /* redirection failed */ +pub static EX_BADASSIGN :i32 = 260; /* variable assignment error */ +pub static EX_EXPFAIL:i32= 261;/* word expansion failed */ +pub static EX_DISKFALLBACK:i32 = 262; /* fall back to disk command from builtin */ + diff --git a/bash-5.1/builtins_rust/exec/src/lib.rs b/bash-5.1/builtins_rust/exec/src/lib.rs index 69139709..ca8399db 100644 --- a/bash-5.1/builtins_rust/exec/src/lib.rs +++ b/bash-5.1/builtins_rust/exec/src/lib.rs @@ -4,10 +4,11 @@ extern crate rcommon; use libc::{c_char,c_int, strlen, strcpy, size_t, c_void, free}; use std::ffi::{CString,CStr}; +use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE, EX_NOTFOUND, EX_NOEXEC, SUBSHELL_PAREN,r_builtin_usage}; use nix::errno::errno; use rcommon::r_sh_restricted; - +/* #[repr (C)] #[derive(Copy,Clone)] pub struct WordDesc{ @@ -21,8 +22,8 @@ pub struct WordList{ pub next:*mut WordList, pub word:*mut WordDesc, } - -#[repr (C)] +*/ +#[repr(C)] struct redirect{ next:*mut redirect, redirector:REDIRECTEE, @@ -40,69 +41,27 @@ union REDIRECTEE { filename:*mut WordDesc, } -#[repr(i8)] //i8 or C ??????? -enum r_instruction { - r_output_direction, - r_input_direction, - r_inputa_direction, - - r_appending_to, - r_reading_until, - r_reading_string, - - r_duplicating_input, - r_duplicating_output, - r_deblank_reading_until, - - r_close_this, - r_err_and_out, - r_input_output, - r_output_force, - - 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 -} - - - -//macro -#[macro_export] -macro_rules! EXECUTION_SUCCESS { - () => {0} -} - -#[macro_export] -macro_rules! EXECUTION_FAILURE { - () => { 1 }; -} - -#[macro_export] -macro_rules! EX_USAGE { - () => { 258 } -} - -#[macro_export] -macro_rules! EX_NOEXEC { - () => { 126 } -} - -#[macro_export] -macro_rules! EX_NOTFOUND { - () => { 127 } -} - -#[macro_export] -macro_rules! SUBSHELL_PAREN { - () => { 0x02 } -} - +pub type r_instruction = libc::c_uint; +pub const r_append_err_and_out: r_instruction = 19; +pub const r_move_output_word: r_instruction = 18; +pub const r_move_input_word: r_instruction = 17; +pub const r_move_output: r_instruction = 16; +pub const r_move_input: r_instruction = 15; +pub const r_duplicating_output_word: r_instruction = 14; +pub const r_duplicating_input_word: r_instruction = 13; +pub const r_output_force: r_instruction = 12; +pub const r_input_output: r_instruction = 11; +pub const r_err_and_out: r_instruction = 10; +pub const r_close_this: r_instruction = 9; +pub const r_deblank_reading_until: r_instruction = 8; +pub const r_duplicating_output: r_instruction = 7; +pub const r_duplicating_input: r_instruction = 6; +pub const r_reading_string: r_instruction = 5; +pub const r_reading_until: r_instruction = 4; +pub const r_appending_to: r_instruction = 3; +pub const r_inputa_direction: r_instruction = 2; +pub const r_input_direction: r_instruction = 1; +pub const r_output_direction: r_instruction = 0; #[macro_export] macro_rules! savestring { @@ -120,7 +79,6 @@ macro_rules! FREE { } } - extern "C" { // static errno:i32; static mut exec_argv0:*mut c_char; @@ -135,12 +93,12 @@ extern "C" { static job_control:i32; static interactive:i32; static default_buffered_input:i32; - // static no_exit_on_failed_exec:i32; - + static no_exit_on_failed_exec:i32; + fn builtin_help(); fn xmalloc(n:size_t)->*mut c_void; fn reset_internal_getopt(); fn internal_getopt(list:*mut WordList,opts:*mut c_char)->i32; - fn builtin_usage(); + // fn builtin_usage(); fn dispose_redirects(list:*mut REDIRECT); // fn sh_restricted(s:*mut c_char); fn strvec_from_word_list(list:*mut WordList,alloc:i32,starting_index:i32,ip:*mut i32)->*mut *mut c_char; @@ -169,10 +127,6 @@ extern "C" { fn restart_job_control(); } - -pub static no_exit_on_failed_exec:i32 = 0; - - /* If the user wants this to look like a login shell, then prepend a `-' onto NAME and return the new name. */ #[no_mangle] @@ -205,16 +159,15 @@ pub extern "C" fn r_exec_builtin(mut list:*mut WordList)->i32{ println!("r_exec_builtin"); - unsafe{ - loop{ - exec_argv0 = std::ptr::null_mut() as *mut c_char; + unsafe{ + exec_argv0 = std::ptr::null_mut() as *mut c_char; - reset_internal_getopt(); + reset_internal_getopt(); + loop{ let c_str = CString::new("cla:").unwrap(); - let c_ptr = c_str.as_ptr() as *mut c_char; - opt = internal_getopt(list,c_ptr); + opt = internal_getopt(list,c_str.as_ptr() as *mut c_char); while opt != -1{ let optu8 = opt as u8; let opt_char = char::from(optu8); @@ -223,12 +176,16 @@ pub extern "C" fn r_exec_builtin(mut list:*mut WordList)->i32{ 'l' => login = 1, 'a' => argv0 = list_optarg, _ => { - builtin_usage(); - return EX_USAGE!(); + if opt == -99 { + builtin_help(); + return EX_USAGE; + } + r_builtin_usage(); + return EX_USAGE; } } - opt = internal_getopt(list,c_ptr); + opt = internal_getopt(list,c_str.as_ptr() as *mut c_char); } list = loptend; @@ -264,11 +221,11 @@ pub extern "C" fn r_exec_builtin(mut list:*mut WordList)->i32{ let c_str = CString::new("%s: cannot execute: %s").unwrap(); let c_ptr = c_str.as_ptr(); builtin_error(c_ptr,*args.offset(0),strerror(errno())); - exit_value = EX_NOEXEC!(); + exit_value = EX_NOEXEC; } else{ sh_notfound(*args.offset(0)); - exit_value = EX_NOTFOUND!(); + exit_value = EX_NOTFOUND; } //goto failed_exec; break; @@ -347,7 +304,7 @@ pub extern "C" fn r_exec_builtin(mut list:*mut WordList)->i32{ adjust_shell_level(1); } - if exit_value == EX_NOTFOUND!(){ + if exit_value == EX_NOTFOUND{ //goto failed_exec; break; } @@ -355,7 +312,7 @@ pub extern "C" fn r_exec_builtin(mut list:*mut WordList)->i32{ let c_str = CString::new("%s: cannot execute: %s").unwrap(); let c_ptr = c_str.as_ptr(); builtin_error(c_ptr,command,strerror(errno())); - exit_value = EX_NOEXEC!(); + exit_value = EX_NOEXEC; } else{ file_error(command); @@ -393,20 +350,3 @@ pub extern "C" fn r_exec_builtin(mut list:*mut WordList)->i32{ } - - - - - - - - - -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - let result = 2 + 2; - assert_eq!(result, 4); - } -} diff --git a/bash-5.1/builtins_rust/exit/src/lib.rs b/bash-5.1/builtins_rust/exit/src/lib.rs index 3fe496c9..0f74aab5 100644 --- a/bash-5.1/builtins_rust/exit/src/lib.rs +++ b/bash-5.1/builtins_rust/exit/src/lib.rs @@ -6,7 +6,8 @@ use libc::c_char; use std::ffi::CString; use rjobs::{PROCESS, COMMAND, r_jobs_builtin, JLIST_STANDARD }; -use rcommon::{ WordList}; + +use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; #[repr(C)] pub struct JOB { @@ -58,20 +59,6 @@ pub enum JOB_STATE { } //宏 -#[macro_export] -macro_rules! EX_USAGE { - () => { 258 } -} - -#[macro_export] -macro_rules! EXECUTION_SUCCESS { - () => { 0 } -} - -#[macro_export] -macro_rules! EXECUTION_FAILURE{ - () => { 1 } -} #[macro_export] macro_rules! get_job_by_jid { @@ -153,7 +140,7 @@ pub extern "C" fn r_exit_builtin(list:*mut WordList) -> i32{ if list != std::ptr::null_mut() && (*list).word != std::ptr::null_mut() && STREQ((*(*list).word).word, c_ptr){ builtin_help(); - return EX_USAGE!(); + return EX_USAGE; } if interactive != 0 { @@ -182,7 +169,7 @@ pub extern "C" fn r_logout_builtin(list:*mut WordList)->i32{ if list != std::ptr::null_mut() && (*list).word != std::ptr::null_mut() && STREQ((*(*list).word).word, c_ptr){ builtin_help(); - return EX_USAGE!(); + return EX_USAGE; } if login_shell == 0{ @@ -270,15 +257,3 @@ pub extern "C" fn r_bash_logout(){ } } - - - - -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - let result = 2 + 2; - assert_eq!(result, 4); - } -} diff --git a/bash-5.1/builtins_rust/fc/Cargo.toml b/bash-5.1/builtins_rust/fc/Cargo.toml index 499532ab..70adc426 100644 --- a/bash-5.1/builtins_rust/fc/Cargo.toml +++ b/bash-5.1/builtins_rust/fc/Cargo.toml @@ -9,6 +9,7 @@ edition = "2018" lazy_static = "1.4.0" libc = "0.2" nix = "0.23.0" +rcommon = {path="../common"} [lib] crate-type = ["staticlib","rlib"] diff --git a/bash-5.1/builtins_rust/fc/src/lib.rs b/bash-5.1/builtins_rust/fc/src/lib.rs index 9d97c2e0..47d9e0be 100644 --- a/bash-5.1/builtins_rust/fc/src/lib.rs +++ b/bash-5.1/builtins_rust/fc/src/lib.rs @@ -1,21 +1,11 @@ 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}; -#[repr(C)] -pub struct WordDesc { - pub word: *mut libc::c_char, - pub flags:libc::c_int -} - -#[repr(C)] -#[derive(Copy,Clone)] -pub struct WordList { - next: *mut WordList, - word: *mut WordDesc -} +use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE,r_builtin_usage}; #[repr(i8)] pub enum JOB_STATE { @@ -261,20 +251,6 @@ pub struct HIST_ENTRY { data:*mut fn() } -#[macro_export] -macro_rules! EXECUTION_FAILURE { - () => {1} -} - -#[macro_export] -macro_rules! EX_USAGE { - () => {258} -} - -#[macro_export] -macro_rules! EXECUTION_SUCCESS { - () => {0} -} #[macro_export] macro_rules! ISHELP { @@ -413,6 +389,7 @@ extern "C" { fn run_unwind_frame (filename:* mut c_char); static mut echo_input_at_read:i32; static mut verbose_flag:i32; + fn builtin_help(); } #[no_mangle] @@ -548,8 +525,12 @@ pub extern "C" fn r_fc_builtin (list:* mut WordList)->i32 's'=>{execute = 1;} 'e'=>{ename = list_optarg;} _=>{ - builtin_usage (); - return EX_USAGE!(); + if opt == -99 { + builtin_help(); + return EX_USAGE; + } + r_builtin_usage(); + return EX_USAGE; } } loptend = lcurrent; diff --git a/bash-5.1/builtins_rust/fg_bg/Cargo.toml b/bash-5.1/builtins_rust/fg_bg/Cargo.toml index 26f78f39..f054950a 100644 --- a/bash-5.1/builtins_rust/fg_bg/Cargo.toml +++ b/bash-5.1/builtins_rust/fg_bg/Cargo.toml @@ -9,6 +9,7 @@ edition = "2018" lazy_static = "1.4.0" libc = "0.2" nix = "0.23.0" +rcommon = {path="../common"} [lib] crate-type = ["staticlib","rlib"] 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 7780ea47..b86636cc 100644 --- a/bash-5.1/builtins_rust/fg_bg/src/lib.rs +++ b/bash-5.1/builtins_rust/fg_bg/src/lib.rs @@ -3,19 +3,7 @@ extern crate nix; use libc::{c_char, c_long}; use std::{ffi::CString, ops::Add}; - -#[repr(C)] -pub struct WordDesc { - pub word: *mut libc::c_char, - pub flags:libc::c_int -} - -#[repr(C)] -#[derive(Copy,Clone)] -pub struct WordList { - next: *mut WordList, - word: *mut WordDesc -} +use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE,r_builtin_usage}; #[repr(i8)] pub enum JOB_STATE { @@ -53,21 +41,16 @@ pub struct PROCESS { command:*mut c_char } -#[repr(C)] -#[derive(Copy,Clone)] -pub union REDIRECTEE { - dest:libc::c_int, /* Place to redirect REDIRECTOR to, or ... */ - filename:* mut WordDesc /* filename to redirect to. */ -} + #[repr(C)] pub union REDIRECT { next:*mut REDIRECT, /* Next element, or NULL. */ - redirector:REDIRECTEE, /* Descriptor or varname to be redirected. */ + redirector:libc::c_int, /* Descriptor or varname to be redirected. */ rflags:libc::c_int, /* Private flags for this redirection */ flags:libc::c_int, /* Flag value for `open'. */ instruction:r_instruction, /* What to do with the information. */ - redirectee:REDIRECTEE, /* File descriptor or filename */ + redirectee:libc::c_int, /* File descriptor or filename */ 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 { @@ -345,7 +314,7 @@ 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!(); + return EX_USAGE; } } } @@ -379,7 +348,7 @@ pub extern "C" fn r_fg_builtin (list:*mut WordList)->i32 { } if no_options (list) !=0 { - return EX_USAGE!(); + return EX_USAGE; } /* If the last arg on the line is '&', then start this job in the @@ -419,7 +388,7 @@ pub extern "C" fn r_bg_builtin (list:*mut WordList)->i32 { } if no_options (list) !=0 { - return EX_USAGE!(); + return EX_USAGE; } /* This relies on the fact that fg_bg() takes a WordList *, but only acts diff --git a/bash-5.1/builtins_rust/getopts/Cargo.toml b/bash-5.1/builtins_rust/getopts/Cargo.toml index ca691a19..07044a6d 100644 --- a/bash-5.1/builtins_rust/getopts/Cargo.toml +++ b/bash-5.1/builtins_rust/getopts/Cargo.toml @@ -9,6 +9,7 @@ edition = "2018" lazy_static = "1.4.0" libc = "0.2" nix = "0.23.0" +rcommon = {path="../common"} [lib] crate-type = ["staticlib","rlib"] diff --git a/bash-5.1/builtins_rust/getopts/src/lib.rs b/bash-5.1/builtins_rust/getopts/src/lib.rs index a133a5d7..1ecc59b3 100644 --- a/bash-5.1/builtins_rust/getopts/src/lib.rs +++ b/bash-5.1/builtins_rust/getopts/src/lib.rs @@ -1,9 +1,9 @@ extern crate libc; extern crate nix; - +use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE,GETOPT_HELP, r_builtin_usage}; use libc::{c_char, c_long, c_void}; use std::{ffi::CString}; - +/* #[repr(C)] pub struct WordDesc { pub word: *mut libc::c_char, @@ -16,7 +16,7 @@ pub struct WordList { next: *mut WordList, word: *mut WordDesc } - + */ #[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, @@ -212,20 +212,6 @@ pub struct SHELL_VAR { context:i32 } -#[macro_export] -macro_rules! EXECUTION_FAILURE { - () => {1} -} - -#[macro_export] -macro_rules! EX_USAGE { - () => {258} -} - -#[macro_export] -macro_rules! EXECUTION_SUCCESS { - () => {0} -} #[macro_export] macro_rules! EX_MISCERROR { @@ -267,12 +253,6 @@ macro_rules! G_ARG_MISSING { } } -#[macro_export] -macro_rules! GETOPT_HELP { - () => { - -99 - } -} extern "C" { fn unbind_variable_noref (name: * const c_char)->i32; @@ -372,7 +352,7 @@ pub extern "C" fn r_dogetopts(argc:i32, argv:*mut*mut c_char)->i32 let mut argvv:*mut*mut c_char=argv; if argcc < 3 { builtin_usage(); - return EX_USAGE!(); + return EX_USAGE; } /* argv[0] is "getopts". */ @@ -524,7 +504,7 @@ pub extern "C" fn r_getopts_builtin(list: * mut WordList)->i32 if list == std::ptr::null_mut() { builtin_usage(); - return EX_USAGE!(); + return EX_USAGE; } reset_internal_getopt(); @@ -536,7 +516,7 @@ pub extern "C" fn r_getopts_builtin(list: * mut WordList)->i32 builtin_usage(); } - return EX_USAGE!(); + return EX_USAGE; } let llist: * mut WordList=loptend.clone(); av = make_builtin_argv(llist, &mut ac); diff --git a/bash-5.1/builtins_rust/hash/src/lib.rs b/bash-5.1/builtins_rust/hash/src/lib.rs index 142514f4..88cc8f5e 100644 --- a/bash-5.1/builtins_rust/hash/src/lib.rs +++ b/bash-5.1/builtins_rust/hash/src/lib.rs @@ -7,9 +7,10 @@ use std::ffi::{CStr,CString}; use std::io::{stdout, Write}; use rread::{SHELL_VAR}; use rcommon::{r_find_shell_builtin,r_builtin_usage}; - +use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; //struct //结构体 +/* #[repr (C)] pub struct WordDesc{ pub word:*mut c_char, @@ -22,7 +23,7 @@ pub struct WordList{ pub next:*mut WordList, pub word:*mut WordDesc, } - +*/ type PTR_T=c_void; #[repr (C)] pub struct bucket_contents{ @@ -50,21 +51,7 @@ pub struct _pathdata{ type PATH_DATA = _pathdata; //enum -//macro -#[macro_export] -macro_rules! EXECUTION_SUCCESS { - () => { 0 } -} - -#[macro_export] -macro_rules! EXECUTION_FAILURE{ - () => { 1 } -} -#[macro_export] -macro_rules! EX_USAGE { - () => { 258 } -} #[macro_export] macro_rules! PARAMS { @@ -140,7 +127,7 @@ extern "C"{ fn builtin_error(format:*const c_char,...); fn reset_internal_getopt(); fn internal_getopt(list:*mut WordList,opts:*mut c_char)->i32; - fn builtin_usage(); + fn sh_needarg(s:*mut c_char); fn phash_flush(); fn sh_restricted(s:*mut c_char); @@ -156,6 +143,7 @@ extern "C"{ fn hash_walk(table:*mut HASH_TABLE,func:*mut hash_wfunc); fn phash_search(filename:*const c_char)->*mut c_char; fn printable_filename(f:*mut c_char,flage:i32)->*mut c_char; + fn builtin_help(); } //rust @@ -200,8 +188,12 @@ pub extern "C" fn r_hash_builtin(mut list:*mut WordList)->i32{ 'r' => expunge_hash_table = 1, 't' => list_targets = 1, _ => { + if opt == -99 { + builtin_help(); + return EX_USAGE; + } r_builtin_usage(); - return EX_USAGE!(); + return EX_USAGE; } } diff --git a/bash-5.1/builtins_rust/help/Cargo.toml b/bash-5.1/builtins_rust/help/Cargo.toml index 50f09b29..993d13e0 100644 --- a/bash-5.1/builtins_rust/help/Cargo.toml +++ b/bash-5.1/builtins_rust/help/Cargo.toml @@ -8,6 +8,7 @@ edition = "2018" [dependencies] libc = "0.2" nix = "0.24.1" +rcommon = {path ="../common"} [lib] crate-type = ["staticlib","rlib"] diff --git a/bash-5.1/builtins_rust/help/src/lib.rs b/bash-5.1/builtins_rust/help/src/lib.rs index b215aacb..28321371 100644 --- a/bash-5.1/builtins_rust/help/src/lib.rs +++ b/bash-5.1/builtins_rust/help/src/lib.rs @@ -3,25 +3,13 @@ extern crate nix; extern crate std; use libc::{c_char, c_void ,putchar, free}; use std::{ffi::{CString,CStr}, i32, io::{Read, stdout, Write}, mem, string, u32}; +use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE, EX_NOTFOUND, EX_NOEXEC, SUBSHELL_PAREN,r_builtin_usage}; pub enum Option { None, Some(T), } -#[repr(C)] -pub struct WordDesc { - pub word: *mut libc::c_char, - pub flags:libc::c_int -} - -#[repr (C)] -#[derive(Copy,Clone)] -pub struct WordList { - next: *mut WordList, - word: *mut WordDesc -} - #[repr (C)] #[derive(Copy,Clone)] pub struct builtin { @@ -39,17 +27,6 @@ struct FieldStruct { name : *mut c_char, } - -#[macro_export] -macro_rules! EXECUTION_SUCCESS { - () => {0} -} - -#[macro_export] -macro_rules! EXECUTION_FAILURE{ - () => {-1} -} - #[macro_export] macro_rules! FNMATCH_EXTFLAG { () => {0} @@ -152,8 +129,9 @@ pub extern "C" fn r_help_builtin(mut list:*mut WordList)->i32 { 's'=> {sflag = 1; break;} _=>{ unsafe { + builtin_usage (); - return EX_USAGE!(); + return EX_USAGE; } } } diff --git a/bash-5.1/builtins_rust/history/src/lib.rs b/bash-5.1/builtins_rust/history/src/lib.rs index 077183ef..520f0cfc 100644 --- a/bash-5.1/builtins_rust/history/src/lib.rs +++ b/bash-5.1/builtins_rust/history/src/lib.rs @@ -55,8 +55,6 @@ unsafe { } list = loptend; - let c_tmp = if *delete_arg == b'-' as c_char {(delete_arg as usize + 1) as *mut c_char} else {delete_arg}; - range = libc::strchr(c_tmp, b'-' as c_int); opt = flags & (AFLAG | RFLAG | WFLAG | NFLAG); if opt != 0 && opt != AFLAG && opt != RFLAG && opt != WFLAG && opt != NFLAG { @@ -82,12 +80,10 @@ unsafe { return expand_and_print_history(list); } return r_sh_chkwrite(EXECUTION_SUCCESS); - } else if (flags & PFLAG) != 0 { - if !list.is_null() { - return expand_and_print_history(list); - } - return r_sh_chkwrite(EXECUTION_SUCCESS); - } else if (flags & DFLAG) != 0 && !range.is_null() { + } 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}; + range = libc::strchr(c_tmp, b'-' as c_int); + if !range.is_null() { let mut delete_start: c_long = 0; let mut delete_end: c_long = 0; @@ -130,7 +126,9 @@ unsafe { if where_history() > history_length { history_set_pos(history_length); } + 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 { r_sh_erange(delete_arg, "history position\0".as_ptr() as *mut c_char); @@ -140,13 +138,13 @@ unsafe { if *delete_arg == b'-' as c_char && delete_offset < 0 { let ind = history_length + delete_offset as c_int; if ind < history_base { - r_sh_erange(range, "history position\0".as_ptr() as *mut c_char); + r_sh_erange(delete_arg, "history position\0".as_ptr() as *mut c_char); return EXECUTION_FAILURE; } opt = ind + history_base; } else if delete_offset < history_base as c_long || (delete_offset >= (history_base + history_length) as c_long) { - r_sh_erange(range, "history position\0".as_ptr() as *mut c_char); + r_sh_erange(delete_arg, "history position\0".as_ptr() as *mut c_char); return EXECUTION_FAILURE; } else { opt = delete_offset as c_int; diff --git a/bash-5.1/builtins_rust/mapfile/Cargo.toml b/bash-5.1/builtins_rust/mapfile/Cargo.toml index 6ee88c63..5d8d8f77 100644 --- a/bash-5.1/builtins_rust/mapfile/Cargo.toml +++ b/bash-5.1/builtins_rust/mapfile/Cargo.toml @@ -3,6 +3,7 @@ name = "rmapfile" version = "0.1.0" edition = "2018" authors = ["lvgenggeng"] +build = "../build.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/bash-5.1/builtins_rust/mapfile/src/intercdep.rs b/bash-5.1/builtins_rust/mapfile/src/intercdep.rs index d1d39561..f439e7b6 100644 --- a/bash-5.1/builtins_rust/mapfile/src/intercdep.rs +++ b/bash-5.1/builtins_rust/mapfile/src/intercdep.rs @@ -30,6 +30,7 @@ pub type sh_var_value_func_t = >; #[repr(C)] +#[derive(Debug)] #[derive(Copy, Clone)] pub struct variable { pub name: *mut c_char, diff --git a/bash-5.1/builtins_rust/mapfile/src/lib.rs b/bash-5.1/builtins_rust/mapfile/src/lib.rs index 512d37f3..4fa35f5b 100644 --- a/bash-5.1/builtins_rust/mapfile/src/lib.rs +++ b/bash-5.1/builtins_rust/mapfile/src/lib.rs @@ -153,6 +153,9 @@ unsafe fn mapfile(fd: c_int, line_count_goal: c_long, origin: c_long, nskip: c_l let mut unbuffered_read: c_int; let entry = find_or_make_array_variable(array_name, 1); + // let entry_test = *entry; + // println!("entry:{:#?}",entry_test); + if entry.is_null() || ((*entry).attributes & att_readonly) != 0 || ((*entry).attributes & att_noassign) != 0 { if !entry.is_null() && ((*entry).attributes & att_readonly) != 0 { err_readonly(array_name); diff --git a/bash-5.1/builtins_rust/printf/src/intercdep.rs b/bash-5.1/builtins_rust/printf/src/intercdep.rs index 5fd15940..e5ab6fa7 100644 --- a/bash-5.1/builtins_rust/printf/src/intercdep.rs +++ b/bash-5.1/builtins_rust/printf/src/intercdep.rs @@ -89,7 +89,7 @@ extern "C" { pub fn xmalloc(bytes: size_t) -> *mut c_void; pub fn xrealloc(p: *mut c_void, bytes: size_t) -> *mut c_void; - pub fn savestring(s: *const c_char) -> *mut c_char; + //pub fn savestring(s: *const c_char) -> *mut c_char; pub fn ansic_shouldquote(s: *const c_char) -> c_int; pub fn ansic_quote(str: *mut c_char, flags: c_int, rlen: *mut c_int) -> *mut c_char; pub fn sh_backslash_quote(string: *mut c_char, table: *mut c_char, flags: c_int) -> *mut c_char; @@ -114,4 +114,5 @@ extern "C" { pub static stdout: *mut libc::FILE; pub static shell_start_time: libc::time_t; -} \ No newline at end of file +} + diff --git a/bash-5.1/builtins_rust/printf/src/lib.rs b/bash-5.1/builtins_rust/printf/src/lib.rs index ce4971ff..1a29c02d 100644 --- a/bash-5.1/builtins_rust/printf/src/lib.rs +++ b/bash-5.1/builtins_rust/printf/src/lib.rs @@ -1,5 +1,6 @@ +use std::ffi::CStr; use std::{ffi::CString}; -use libc::{size_t, c_int, c_uint, c_char, c_long, c_void, PT_NULL, c_ulong, strchr}; +use libc::{size_t, c_int, c_uint, c_char, c_long, c_void, PT_NULL, c_ulong, strchr, }; use rcommon::{r_builtin_usage,r_sh_invalidid,r_sh_wrerror,r_builtin_bind_variable,SHELL_VAR}; @@ -51,6 +52,13 @@ static mut tw: c_long = 0; static mut garglist: *mut WordList = PT_NULL as *mut WordList; static mut orig_arglist: *mut WordList = PT_NULL as *mut WordList; +unsafe fn savestring(x:* mut c_char)->* mut c_char +{ + let str1:* mut c_char=libc::malloc(1 + libc::strlen (x as * const c_char)) as * mut c_char; + return libc::strcpy(str1,x as * const c_char); +} + + #[no_mangle] pub extern "C" fn r_printf_builtin(mut list: *mut WordList) -> i32 { println!("r_printf_builtin call"); @@ -168,27 +176,35 @@ unsafe { 'outer: loop { tw = 0; fmt = format; + while *fmt != 0 { precision = 0; fieldwidth = 0; have_fieldwidth = 0; have_precision = 0; - if *fmt == b'\\' as c_char { + if *fmt == b'\\' as c_char { fmt = (fmt as usize + 1) as *mut c_char; + let mut mbch: [i8;25] = [0; 25]; let mut mblen: c_int = 0; fmt = (fmt as usize + tescape(fmt, mbch.as_ptr() as *mut c_char, std::mem::transmute(&mblen), PT_NULL as *mut c_int) as usize) as *mut c_char; let mut mbind = 0; + while mbind < mblen { PC(mbch[mbind as usize] as u8); + mbind += 1; } fmt = (fmt as usize - 1) as *mut c_char; + + fmt = (fmt as usize + 1) as *mut c_char; continue; } if *fmt != b'%' as c_char { PC(*fmt as u8); + + fmt = (fmt as usize + 1) as *mut c_char; continue; } @@ -196,6 +212,8 @@ unsafe { fmt = (fmt as usize + 1) as *mut c_char; if *fmt == b'%' as c_char { PC(b'%'); + + fmt = (fmt as usize + 1) as *mut c_char; continue; } @@ -426,6 +444,8 @@ unsafe { fmt = start; libc::free(timefmt as *mut c_void); PC(*fmt as u8); + + fmt = (fmt as usize + 1) as *mut c_char; continue; } if *timefmt == b'\0' as c_char { @@ -507,7 +527,8 @@ unsafe { let xp: *mut c_char; let p = getstr(); if !p.is_null() && *p == 0 { - xp = savestring(b"''\0".as_ptr() as *const c_char); + xp = savestring(b"''\0".as_ptr() as *mut c_char); + //xp = savestring(b"''\0".as_ptr() as *const c_char); } else if ansic_shouldquote(p) != 0 { xp = ansic_quote(p, 0, PT_NULL as *mut c_int); } else { @@ -1214,3 +1235,4 @@ unsafe fn asciicode() -> c_long return ch; } + diff --git a/bash-5.1/builtins_rust/pushd/Cargo.toml b/bash-5.1/builtins_rust/pushd/Cargo.toml index cfd6cdc0..51804e82 100644 --- a/bash-5.1/builtins_rust/pushd/Cargo.toml +++ b/bash-5.1/builtins_rust/pushd/Cargo.toml @@ -9,6 +9,7 @@ edition = "2018" lazy_static = "1.4.0" libc = "0.2" nix = "0.23.0" +rcommon = {path ="../common"} [lib] crate-type = ["staticlib","rlib"] diff --git a/bash-5.1/builtins_rust/pushd/src/lib.rs b/bash-5.1/builtins_rust/pushd/src/lib.rs index 8a823e25..b8bfcb04 100644 --- a/bash-5.1/builtins_rust/pushd/src/lib.rs +++ b/bash-5.1/builtins_rust/pushd/src/lib.rs @@ -4,18 +4,7 @@ extern crate nix; use libc::{c_char, c_long, c_void}; use std::{ffi::CString}; -#[repr(C)] -pub struct WordDesc { - pub word: *mut libc::c_char, - pub flags:libc::c_int -} - -#[repr(C)] -#[derive(Copy,Clone)] -pub struct WordList { - next: *mut WordList, - word: *mut WordDesc -} +use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE, EX_NOTFOUND, EX_NOEXEC, SUBSHELL_PAREN,r_builtin_usage}; #[repr(u8)] enum command_type { cm_for, cm_case, cm_while, cm_if, cm_simple, cm_select, @@ -34,22 +23,23 @@ enum r_instruction { r_move_input, r_move_output, r_move_input_word, r_move_output_word, r_append_err_and_out } - +/* #[repr(C)] #[derive(Copy,Clone)] pub union REDIRECTEE { dest:libc::c_int, filename:* mut WordDesc } +*/ #[repr(C)] pub union REDIRECT { next:*mut REDIRECT, - redirector:REDIRECTEE, + redirector:libc::c_int, rflags:libc::c_int, flags:libc::c_int, instruction:r_instruction, - redirectee:REDIRECTEE, + redirectee:libc::c_int, here_doc_eof:*mut c_char } @@ -201,22 +191,6 @@ pub struct COMMAND { value:VALUE_COMMAND } -#[macro_export] -macro_rules! EXECUTION_FAILURE { - () => {1} -} - -#[macro_export] -macro_rules! EX_USAGE { - () => {258} -} - -#[macro_export] -macro_rules! EXECUTION_SUCCESS { - () => { - 0 - } -} #[macro_export] macro_rules! NOCD { @@ -308,7 +282,7 @@ pub extern "C" fn r_pushd_builtin (listt:* mut WordList)->i32 if list != std::ptr::null_mut() && (*list).word != std::ptr::null_mut() && ISHELP((*((*list).word)).word) { builtin_help (); - return EX_USAGE!(); + return EX_USAGE; } if list != std::ptr::null_mut() && (*list).word != std::ptr::null_mut() && ISOPTION ((*((*list).word)).word, '-' as c_char) { @@ -356,7 +330,7 @@ pub extern "C" fn r_pushd_builtin (listt:* mut WordList)->i32 if legal_number (((*((*list).word)).word as usize +1) as * mut c_char, &mut num) == 0 { sh_invalidnum ((*((*list).word)).word); builtin_usage (); - return EX_USAGE!(); + return EX_USAGE; } if direction == '-' as c_char { @@ -371,7 +345,7 @@ pub extern "C" fn r_pushd_builtin (listt:* mut WordList)->i32 } else if *((*((*list).word)).word)== '-' as c_char { sh_invalidopt ((*((*list).word)).word); builtin_usage (); - return EX_USAGE!(); + return EX_USAGE; } else { break; } @@ -490,7 +464,7 @@ unsafe { let mut list:* mut WordList=listt.clone(); if list != std::ptr::null_mut() && (*list).word != std::ptr::null_mut() && ISHELP((*((*list).word)).word) { builtin_help (); - return EX_USAGE!(); + return EX_USAGE; } which_word = std::ptr::null_mut(); @@ -510,17 +484,17 @@ while list != std::ptr::null_mut() { if legal_number ((((*((*list).word)).word as usize + 1) as * mut c_char), & mut which) == 0 { sh_invalidnum ((*((*list).word)).word); builtin_usage (); - return EX_USAGE!(); + return EX_USAGE; } which_word = (*((*list).word)).word; } else if *((*((*list).word)).word) == '-' as c_char { sh_invalidopt ((*((*list).word)).word); builtin_usage (); - return EX_USAGE!(); + return EX_USAGE; } else if (*((*list).word)).word != std::ptr::null_mut() { builtin_error (CString::new("%s: invalid argument").unwrap().as_ptr() as * mut c_char, (*((*list).word)).word); builtin_usage (); - return EX_USAGE!(); + return EX_USAGE; } else { break; } @@ -603,7 +577,7 @@ pub extern "C" fn r_dirs_builtin (listt:* mut WordList)->i32 let mut list:* mut WordList=listt.clone(); if list != std::ptr::null_mut() && (*list).word != std::ptr::null_mut() && ISHELP((*((*list).word)).word) { builtin_help (); - return EX_USAGE!(); + return EX_USAGE; } while list != std::ptr::null_mut() { @@ -624,7 +598,7 @@ pub extern "C" fn r_dirs_builtin (listt:* mut WordList)->i32 if legal_number (w, &mut i) == 0 { sh_invalidnum ((*((*list).word)).word); builtin_usage (); - return EX_USAGE!(); + return EX_USAGE; } if *((*((*list).word)).word) == '+' as c_char { @@ -637,7 +611,7 @@ pub extern "C" fn r_dirs_builtin (listt:* mut WordList)->i32 } else { sh_invalidopt ((*((*list).word)).word); builtin_usage (); - return EX_USAGE!(); + return EX_USAGE; } list=(*list).next } @@ -969,4 +943,4 @@ pub extern "C" fn cmd_name() ->*const u8 { #[no_mangle] pub extern "C" fn run(list : *mut WordList)->i32 { return r_pushd_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 6fcffeea..720c6228 100644 --- a/bash-5.1/builtins_rust/read/src/lib.rs +++ b/bash-5.1/builtins_rust/read/src/lib.rs @@ -27,6 +27,7 @@ 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; @@ -168,7 +169,8 @@ unsafe { return EXECUTION_FAILURE; } } - 'd' => delim = *list_optarg, + 'd' => {delim = *list_optarg; + println!("-d");} _ => { r_builtin_usage (); return EX_USAGE; diff --git a/bash-5.1/builtins_rust/set/Cargo.toml b/bash-5.1/builtins_rust/set/Cargo.toml index 0a60e09d..a437a262 100644 --- a/bash-5.1/builtins_rust/set/Cargo.toml +++ b/bash-5.1/builtins_rust/set/Cargo.toml @@ -9,7 +9,7 @@ edition = "2018" libc="*" nix="*" lazy_static = "1.4.0" - +rcommon = {path ="../common"} [lib] crate-type = ["staticlib","rlib"] name = "rset" diff --git a/bash-5.1/builtins_rust/set/src/lib.rs b/bash-5.1/builtins_rust/set/src/lib.rs index 24ab4ecc..32066f4e 100644 --- a/bash-5.1/builtins_rust/set/src/lib.rs +++ b/bash-5.1/builtins_rust/set/src/lib.rs @@ -6,18 +6,7 @@ use std::ffi::CString; use std::ptr; use std::mem; use std::io; -#[repr(C)] -pub struct WordDesc { - pub word: *mut i8, - pub flags:i32 -} - -#[repr (C)] -#[derive(Copy,Clone)] -pub struct WordList { - next: *mut WordList, - word: *mut WordDesc -} +use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE, EX_NOTFOUND, EX_NOEXEC, SUBSHELL_PAREN,r_builtin_usage}; #[macro_export] macro_rules! FLAG_UNKNOWN { @@ -259,20 +248,6 @@ pub struct variable { // } // } -#[macro_export] -macro_rules! EXECUTION_SUCCESS { - () => {0} -} - -#[macro_export] -macro_rules! EXECUTION_FAILURE{ - () => {-1} -} - -#[macro_export] -macro_rules! EX_USAGE{ - () => {258} -} #[macro_export] macro_rules! FLAG_ERROR{ @@ -1239,7 +1214,7 @@ unsafe fn set_minus_o_option (on_or_off : i32, option_name : *mut i8) -> i32 { //println!("i is {}",i); if i < 0{ sh_invalidoptname (option_name); - return EX_USAGE!(); + return EX_USAGE; } if o_options[i as usize].letter == 0{ @@ -1445,7 +1420,7 @@ unsafe fn reset_shell_options () { sh_invalidopt (s.as_ptr() as *mut i8); builtin_usage(); } - return EX_USAGE!();} + return EX_USAGE;} '?' => { unsafe { builtin_usage (); @@ -1454,14 +1429,14 @@ unsafe fn reset_shell_options () { return EXECUTION_SUCCESS!(); } else { - return EX_USAGE!(); + return EX_USAGE; } } _ => { if opt == -99 { unsafe { builtin_usage (); - return EX_USAGE!(); + return EX_USAGE; } } } @@ -1672,7 +1647,7 @@ pub extern "C" fn r_unset_builtin(mut list: *mut WordList) -> i32 { 'n'=>{nameref = 1;} _=>{ builtin_usage (); - return EX_USAGE!(); + return EX_USAGE; } } opt =internal_getopt (list, c_str_fnv.as_ptr() as * mut i8); diff --git a/bash-5.1/builtins_rust/shopt/src/lib.rs b/bash-5.1/builtins_rust/shopt/src/lib.rs index ff0a3197..c557ab77 100644 --- a/bash-5.1/builtins_rust/shopt/src/lib.rs +++ b/bash-5.1/builtins_rust/shopt/src/lib.rs @@ -2,9 +2,8 @@ extern crate rcommon; use std::ffi::*; use rset::r_set_shellopts; use libc::*; -use rcommon::WordDesc; -use rcommon::WordList; +use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE, EX_NOTFOUND, EX_NOEXEC, SUBSHELL_PAREN,r_builtin_usage}; /* /* First, the user-visible attributes */ #define att_exported 0x0000001 /* export to environment */ @@ -913,11 +912,8 @@ static UFLAG:i32 = 0x02; static QFLAG:i32 = 0x04; static OFLAG:i32 = 0x08; static PFLAG:i32 = 0x10; -static EX_USAGE:i32 = 258; static SETOPT:i32 = 1; static UNSETOPT:i32 = 0; -static EXECUTION_SUCCESS : i32 = 0; -static EXECUTION_FAILURE :i32 = 1; static mut ON: *const libc::c_char = b"on\0" as *const u8 as *const libc::c_char; static mut OFF: *const libc::c_char = b"off\0" as *const u8 as *const libc::c_char; @@ -972,7 +968,7 @@ pub unsafe extern "C" fn r_shopt_builtin(mut list: *mut WordList) -> i32 { 5 as i32, ), ); - return EXECUTION_FAILURE; + return EXECUTION_FAILURE!(); } if (flags & OFLAG != 0) &&( (flags & (SFLAG | UFLAG)) == 0) // shopt -o @@ -1092,12 +1088,12 @@ unsafe extern "C" fn toggle_shopts( let mut rval: i32; let v: *mut ShellVar; l = list; - rval = EXECUTION_SUCCESS; + rval = EXECUTION_SUCCESS!(); while !l.is_null() { ind = find_shopt((*(*l).word).word); if ind < 0 { shopt_error((*(*l).word).word); - rval = EXECUTION_FAILURE; + rval = EXECUTION_FAILURE!(); } else { *SHOPT_VARS[ind as usize].value = mode; if (SHOPT_VARS[ind as usize].set_func).is_some() { @@ -1157,7 +1153,7 @@ unsafe extern "C" fn r_list_shopts( let mut i; let mut val = 0; let mut rval =0; - rval = EXECUTION_SUCCESS; + rval = EXECUTION_SUCCESS!(); if (flags & QFLAG) ==0 { if list.is_null() { for item in SHOPT_VARS { @@ -1166,18 +1162,18 @@ unsafe extern "C" fn r_list_shopts( print_shopt(item.name, val, flags); } } - return sh_chkwrite(EXECUTION_SUCCESS); + return sh_chkwrite(EXECUTION_SUCCESS!()); } l = list; while !l.is_null() { i = find_shopt((*(*l).word).word); if i < 0 { shopt_error((*(*l).word).word); - rval = EXECUTION_FAILURE; + rval = EXECUTION_FAILURE!(); } else { val = *SHOPT_VARS[i as usize].value; if val == 0 { - rval = EXECUTION_FAILURE; + rval = EXECUTION_FAILURE!(); } print_shopt((*(*l).word).word, val, flags); } @@ -1200,7 +1196,7 @@ unsafe extern "C" fn list_some_shopts( print_shopt(item.name, *item.value, flags); } } - return sh_chkwrite(EXECUTION_SUCCESS); + return sh_chkwrite(EXECUTION_SUCCESS!()); } unsafe extern "C" fn r_list_shopt_o_options( @@ -1209,12 +1205,12 @@ unsafe extern "C" fn r_list_shopt_o_options( ) -> i32 { let mut l: *mut WordList = 0 as *mut WordList; let mut val: i32 = 0; - let mut rval: i32 = EXECUTION_SUCCESS; + let mut rval: i32 = EXECUTION_SUCCESS!(); if list.is_null() { if flags & QFLAG == 0 { list_minus_o_opts(-1, flags & PFLAG); } - return sh_chkwrite(EXECUTION_SUCCESS); + return sh_chkwrite(EXECUTION_SUCCESS!()); } l = list; @@ -1222,10 +1218,10 @@ unsafe extern "C" fn r_list_shopt_o_options( val = minus_o_option_value((*(*l).word).word); if val == -1 { sh_invalidoptname((*(*l).word).word); - rval = EXECUTION_FAILURE; + rval = EXECUTION_FAILURE!(); } else { if val == 0 { - rval = EXECUTION_FAILURE; + rval = EXECUTION_FAILURE!(); } if flags & QFLAG == 0 { if flags & PFLAG != 0 { @@ -1256,7 +1252,7 @@ unsafe extern "C" fn list_some_o_options( if flags & QFLAG == 0 { list_minus_o_opts(mode, flags & PFLAG); } - return sh_chkwrite(EXECUTION_SUCCESS); + return sh_chkwrite(EXECUTION_SUCCESS!()); } unsafe extern "C" fn set_shopt_o_options( mode: i32, @@ -1267,7 +1263,7 @@ unsafe extern "C" fn set_shopt_o_options( let mut l: *mut WordList; let mut rval: i32 ; l = list; - rval = EXECUTION_SUCCESS; + rval = EXECUTION_SUCCESS!(); while !l.is_null() { if set_minus_o_option(mode, (*(*l).word).word) == 1 as i32 { rval = 1 as i32; @@ -1468,7 +1464,7 @@ pub unsafe extern "C" fn r_shopt_listopt( *SHOPT_VARS[i as usize].value, if reusable != 0 { PFLAG } else { 0 }, ); - return sh_chkwrite(EXECUTION_SUCCESS); + return sh_chkwrite(EXECUTION_SUCCESS!()); } #[no_mangle] pub unsafe extern "C" fn r_set_bashopts() { diff --git a/bash-5.1/builtins_rust/source/Cargo.toml b/bash-5.1/builtins_rust/source/Cargo.toml index 91e31631..8057a26f 100644 --- a/bash-5.1/builtins_rust/source/Cargo.toml +++ b/bash-5.1/builtins_rust/source/Cargo.toml @@ -9,6 +9,7 @@ edition = "2018" lazy_static = "1.4.0" libc = "0.2" nix = "0.23.0" +rcommon = {path ="../common"} [lib] crate-type = ["staticlib","rlib"] diff --git a/bash-5.1/builtins_rust/source/src/lib.rs b/bash-5.1/builtins_rust/source/src/lib.rs index 53de38f7..fcbbf1e0 100644 --- a/bash-5.1/builtins_rust/source/src/lib.rs +++ b/bash-5.1/builtins_rust/source/src/lib.rs @@ -3,19 +3,8 @@ extern crate nix; use libc::{c_char, c_long, c_void}; use std::{ffi::CString}; +use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE, EX_NOTFOUND, EX_NOEXEC, SUBSHELL_PAREN,r_builtin_usage}; -#[repr(C)] -pub struct WordDesc { - pub word: *mut libc::c_char, - pub flags:libc::c_int -} - -#[repr(C)] -#[derive(Copy,Clone)] -pub struct WordList { - next: *mut WordList, - word: *mut WordDesc -} #[repr(u8)] enum command_type { cm_for, cm_case, cm_while, cm_if, cm_simple, cm_select, @@ -34,22 +23,23 @@ enum r_instruction { r_move_input, r_move_output, r_move_input_word, r_move_output_word, r_append_err_and_out } - +/* #[repr(C)] #[derive(Copy,Clone)] pub union REDIRECTEE { dest:libc::c_int, filename:* mut WordDesc } +*/ #[repr(C)] pub union REDIRECT { next:*mut REDIRECT, - redirector:REDIRECTEE, + redirector:libc::c_int, rflags:libc::c_int, flags:libc::c_int, instruction:r_instruction, - redirectee:REDIRECTEE, + redirectee:libc::c_int, here_doc_eof:*mut c_char } @@ -201,15 +191,6 @@ pub struct COMMAND { value:VALUE_COMMAND } -#[macro_export] -macro_rules! EXECUTION_FAILURE { - () => {1} -} - -#[macro_export] -macro_rules! EX_USAGE { - () => {258} -} #[macro_export] macro_rules! ARGS_SETBLTIN { @@ -319,7 +300,7 @@ pub extern "C" fn r_source_builtin (list:* mut WordList)->i32 let x:* mut c_char; unsafe { if no_options (list) !=0{ - return EX_USAGE!(); + return EX_USAGE; } let mut llist:* mut WordList = loptend.clone(); @@ -327,7 +308,7 @@ pub extern "C" fn r_source_builtin (list:* mut WordList)->i32 if list == std::ptr::null_mut() { builtin_error (CString::new("filename argument required").unwrap().as_ptr()); builtin_usage (); - return EX_USAGE!(); + return EX_USAGE; } if restricted !=0 && libc::strchr ((*(*llist).word).word, '/' as libc::c_int) != std::ptr::null_mut() { @@ -413,4 +394,4 @@ pub extern "C" fn cmd_name() ->*const u8 { #[no_mangle] pub extern "C" fn run(list : *mut WordList)->i32 { return r_source_builtin(list); -} \ No newline at end of file +} diff --git a/bash-5.1/builtins_rust/type/Cargo.toml b/bash-5.1/builtins_rust/type/Cargo.toml index 08ffe400..01ab1e93 100644 --- a/bash-5.1/builtins_rust/type/Cargo.toml +++ b/bash-5.1/builtins_rust/type/Cargo.toml @@ -10,6 +10,7 @@ edition = "2018" [dependencies] libc = "0.2" nix = "0.23.0" +rcommon = {path ="../common"} [lib] crate-type = ["staticlib","rlib"] diff --git a/bash-5.1/builtins_rust/type/src/lib.rs b/bash-5.1/builtins_rust/type/src/lib.rs index d4374c95..c2318bfd 100644 --- a/bash-5.1/builtins_rust/type/src/lib.rs +++ b/bash-5.1/builtins_rust/type/src/lib.rs @@ -5,6 +5,7 @@ use libc::c_void; use std::ffi::CStr; use std::ffi::CString; use std::ptr; +use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE, EX_NOTFOUND, EX_NOEXEC, SUBSHELL_PAREN,r_builtin_usage}; #[macro_export] macro_rules! CDESC_ALL{ @@ -64,25 +65,12 @@ 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!(); + return EX_USAGE; } } } -#[macro_export] -macro_rules! EX_USAGE { - () => {258} -} - -#[macro_export] -macro_rules! EXECUTION_SUCCESS { - () => {0} -} -#[macro_export] -macro_rules! EXECUTION_FAILURE { - () => {1} -} #[macro_export] macro_rules! FS_EXECABLE{ @@ -123,19 +111,6 @@ macro_rules! SIZEOFWORD{ } } -#[repr(C)] -#[derive(Copy,Clone)] -pub struct WordDesc { - pub word: *mut i8, - pub flags:i32 -} - -#[repr (C)] -#[derive(Copy,Clone)] -pub struct WordList { - next: *mut WordList, - word: *mut WordDesc -} #[repr(C)] pub struct SHELL_VAR { @@ -468,11 +443,11 @@ pub unsafe extern "C" fn r_type_builtin (mut list :*mut WordList) -> i32 { _ =>{ if opt == -99 { builtin_usage(); - return EX_USAGE!(); + return EX_USAGE; } unsafe { builtin_usage (); - return EX_USAGE!(); + return EX_USAGE; } } } diff --git a/bash-5.1/builtins_rust/ulimit/Cargo.toml b/bash-5.1/builtins_rust/ulimit/Cargo.toml index 4fbd136f..0e03d966 100644 --- a/bash-5.1/builtins_rust/ulimit/Cargo.toml +++ b/bash-5.1/builtins_rust/ulimit/Cargo.toml @@ -11,6 +11,7 @@ edition = "2018" lazy_static = "1.4.0" libc = "0.2" nix = "0.23.0" +rcommon = {path = "../common"} [lib] crate-type = ["staticlib","rlib"] diff --git a/bash-5.1/builtins_rust/ulimit/src/lib.rs b/bash-5.1/builtins_rust/ulimit/src/lib.rs index adf0be5e..2ed26a22 100644 --- a/bash-5.1/builtins_rust/ulimit/src/lib.rs +++ b/bash-5.1/builtins_rust/ulimit/src/lib.rs @@ -5,6 +5,7 @@ use std::ffi::CStr; use std::ffi::CString; use std::ptr; use std::mem; +use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE, EX_NOTFOUND, EX_NOEXEC, SUBSHELL_PAREN,r_builtin_usage}; #[derive(Copy, Clone)] @@ -14,19 +15,6 @@ pub struct rlimit { pub rlim_max: rlim_t, } -#[repr(C)] -#[derive(Copy,Clone)] -pub struct WordDesc { - pub word: *mut i8, - pub flags:i32 -} - -#[repr (C)] -#[derive(Copy,Clone)] -pub struct WordList { - next: *mut WordList, - word: *mut WordDesc -} #[repr (C)] #[derive(Copy,Clone)] @@ -125,20 +113,6 @@ macro_rules! RLIM_SAVED_MAX{ } } -#[macro_export] -macro_rules! EXECUTION_SUCCESS { - () => {0} -} - -#[macro_export] -macro_rules! EXECUTION_FAILURE { - () => {1} -} - -#[macro_export] -macro_rules! EX_USAGE { - () => {258} -} #[deny(missing_fragment_specifier)] #[macro_export] @@ -510,17 +484,17 @@ pub unsafe extern "C" fn r_ulimit_builtin(mut list: *mut WordList) -> i32{ 'H' => { mode = mode | LIMIT_HARD!();} '?'=> { builtin_help(); - return EX_USAGE!(); + return EX_USAGE; } // => { // builtin_usage(); - // return EX_USAGE!(); + // return EX_USAGE; // } _ => { //println!("enter switch default,opt is {}",opt); if opt == -99 { builtin_usage(); - return EX_USAGE!(); + return EX_USAGE; } if ncmd >= cmdlistsz { cmdlistsz = cmdlistsz * 2 ; @@ -616,7 +590,7 @@ pub unsafe extern "C" fn r_ulimit_builtin(mut list: *mut WordList) -> i32{ (*cmdlist.offset(d as isize)).cmd, strerror(*__errno_location()) as *const i8); } - return EX_USAGE!(); + return EX_USAGE; } } diff --git a/bash-5.1/builtins_rust/umask/src/lib.rs b/bash-5.1/builtins_rust/umask/src/lib.rs index 9f2b72a3..6e153736 100644 --- a/bash-5.1/builtins_rust/umask/src/lib.rs +++ b/bash-5.1/builtins_rust/umask/src/lib.rs @@ -3,22 +3,10 @@ extern crate libc; use libc::{c_char,c_int}; use std::ffi::{CString}; use rcommon::{r_read_octal}; +use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE, EX_NOTFOUND, EX_NOEXEC, SUBSHELL_PAREN,r_builtin_usage}; //结构体 -#[repr (C)] -#[derive(Debug)] -pub struct WordDesc{ - pub word:*mut c_char, - pub flags:c_int, -} - -#[repr (C)] -#[derive(Debug)] -pub struct WordList{ - pub next:*mut WordList, - pub word:*mut WordDesc, -} //枚举 @@ -32,20 +20,6 @@ macro_rules! mode_t { }; } -#[macro_export] -macro_rules! EX_USAGE{ - () => { 258 } -} - -#[macro_export] -macro_rules! EXECUTION_FAILURE { - () => { 1 }; -} - -#[macro_export] -macro_rules! EXECUTION_SUCCESS{ - () => { 0 } -} #[macro_export] macro_rules! S_IREAD{ @@ -239,7 +213,7 @@ pub extern "C" fn r_umask_builtin(mut list:*mut WordList) ->i32{ 'S' => {print_symbolically = print_symbolically +1;} 'p' => {pflag = pflag + 1;} _ => { builtin_usage(); - return EX_USAGE!(); + return EX_USAGE; } } diff --git a/bash-5.1/builtins_rust/wait/src/lib.rs b/bash-5.1/builtins_rust/wait/src/lib.rs index 98d236ae..79362038 100644 --- a/bash-5.1/builtins_rust/wait/src/lib.rs +++ b/bash-5.1/builtins_rust/wait/src/lib.rs @@ -12,7 +12,7 @@ use rjobs::{PROCESS,COMMAND, BLOCK_CHILD, UNBLOCK_CHILD}; use rread::{SHELL_VAR,sh_var_value_func_t,sh_var_assign_func_t, sigjmp_buf,__jmp_buf_tag,__sigset_t,__sigsetjmp,}; use rcommon::{r_builtin_unbind_variable,r_builtin_usage,r_get_job_spec,WordList}; - +use rcommon::{ WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE, EX_NOTFOUND, EX_NOEXEC, SUBSHELL_PAREN}; //结构体 @@ -85,24 +85,6 @@ pub enum JOB_STATE { //宏 -#[macro_export] -macro_rules! EX_USAGE{ /* syntax error in usage */ - () => { 258 } -} - -#[macro_export] -macro_rules! EXECUTION_SUCCESS { - () => { - 0 - }; -} - -#[macro_export] -macro_rules! EXECUTION_FAILURE { - () => { - 1 - }; -} #[macro_export] macro_rules! J_WAITING { @@ -293,7 +275,7 @@ pub extern "C" fn r_wait_builtin(mut list:*mut WordList)->i32{ 'p' => vname = list_optarg, _ => { r_builtin_usage(); - return EX_USAGE!(); + return EX_USAGE; } } diff --git a/record.txt b/record.txt index 11f1dbf2..3f298cc8 100644 --- a/record.txt +++ b/record.txt @@ -22,3 +22,4 @@ 44 45 46 +47 -- Gitee