From f642996bec9efe6dacb702978bdfeecaea2fba4f Mon Sep 17 00:00:00 2001 From: liutong Date: Thu, 10 Aug 2023 23:43:58 +0800 Subject: [PATCH] Unified Interface parameter type and Implement factory patterns --- bash-5.1/Cargo.toml | 2 + bash-5.1/Makefile.in | 4 +- 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/cd/src/lib.rs | 4 +- bash-5.1/builtins_rust/exec/src/lib.rs | 19 +- bash-5.1/builtins_rust/exec_cmd/Cargo.toml | 2 +- bash-5.1/builtins_rust/exec_cmd/src/lib.rs | 338 ++++++++---------- bash-5.1/builtins_rust/exit/src/lib.rs | 2 +- bash-5.1/builtins_rust/fc/src/lib.rs | 1 + bash-5.1/builtins_rust/help/Cargo.toml | 1 + bash-5.1/builtins_rust/help/src/lib.rs | 27 +- 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 | 18 +- bash-5.1/builtins_rust/mapfile/src/lib.rs | 3 + .../builtins_rust/printf/src/intercdep.rs | 23 +- 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/intercdep.rs | 17 +- bash-5.1/builtins_rust/read/src/lib.rs | 4 +- bash-5.1/builtins_rust/rlet/Cargo.toml | 1 + bash-5.1/builtins_rust/rlet/src/intercdep.rs | 17 +- bash-5.1/builtins_rust/set/Cargo.toml | 2 +- bash-5.1/builtins_rust/set/src/lib.rs | 37 +- bash-5.1/builtins_rust/setattr/Cargo.toml | 1 + .../builtins_rust/setattr/src/intercdep.rs | 10 +- bash-5.1/builtins_rust/shift/src/intercdep.rs | 17 +- 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/times/src/intercdep.rs | 17 +- 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 + 47 files changed, 343 insertions(+), 786 deletions(-) diff --git a/bash-5.1/Cargo.toml b/bash-5.1/Cargo.toml index 595ff04..0c96767 100644 --- a/bash-5.1/Cargo.toml +++ b/bash-5.1/Cargo.toml @@ -61,6 +61,7 @@ members=[ "builtins_rust/trap", "builtins_rust/setattr", "builtins_rust/source", + "builtins_rust/exec_cmd", ] [dependencies] @@ -100,3 +101,4 @@ members=[ rumask = {path = "./builtins_rust/umask"} rwait = {path = "./builtins_rust/wait"} rdeclare = {path = "./builtins_rust/declare"} + rexec_cmd = {path = "./builtins_rust/exec_cmd"} diff --git a/bash-5.1/Makefile.in b/bash-5.1/Makefile.in index 019ead7..7607ed8 100644 --- a/bash-5.1/Makefile.in +++ b/bash-5.1/Makefile.in @@ -139,7 +139,7 @@ LOCALE_DEFS = -DLOCALEDIR='"$(localedir)"' -DPACKAGE='"$(PACKAGE)"' LOCAL_LIBS = @LOCAL_LIBS@ -LIBS = $(BUILTINS_LIB) $(LIBRARIES) @LIBS@ -lrt -lpthread -L./target/debug -lralias -lrbind -lrbreak -lrbuiltin -lrcaller -lrcd -lrcolon -lrcommon -lrcomplete -lrdeclare -lrecho -lrenable -lreval -lrexec -lrexit -lrfc -lrfg_bg -lrgetopts -lrhash -lrexit -lrhelp -lrhistory -lrjobs -lrkill -lrmapfile -lrpushd -lrread -lrlet -lrreturn -lrset -lrsetattr -lrshift -lrshopt -lrsource -lrsuspend -lrtest -lrtimes -lrtrap -lrtype -lrulimit -lrumask -lrwait -lrprintf +LIBS = $(BUILTINS_LIB) $(LIBRARIES) @LIBS@ -lrt -lpthread -L./target/debug -lralias -lrbind -lrbreak -lrbuiltin -lrcaller -lrcd -lrcolon -lrcommon -lrcomplete -lrdeclare -lrecho -lrenable -lreval -lrexec -lrexit -lrfc -lrfg_bg -lrgetopts -lrhash -lrexit -lrhelp -lrhistory -lrjobs -lrkill -lrmapfile -lrpushd -lrread -lrlet -lrreturn -lrset -lrsetattr -lrshift -lrshopt -lrsource -lrsuspend -lrtest -lrtimes -lrtrap -lrtype -lrulimit -lrumask -lrwait -lrprintf -lrexec_cmd LIBS_FOR_BUILD = STATIC_LD = @STATIC_LD@ @@ -575,7 +575,7 @@ LOADABLES_DIR = ${top_builddir}/examples/loadables RUST_DIR = $(top_builddir)/builtins_rust #RUST_TARGET_LIB = $(top_builddir)/target/debug/librjobs.a $(top_builddir)/target/debug/librread.a -RUST_BUILTINS_DIRS = $(RUST_DIR)/alias $(RUST_DIR)/bind $(RUST_DIR)/break_1 $(RUST_DIR)/builtin $(RUST_DIR)/caller $(RUST_DIR)/cd $(RUST_DIR)/colon $(RUST_DIR)/command $(RUST_DIR)/common $(RUST_DIR)/complete $(RUST_DIR)/declare $(RUST_DIR)/echo $(RUST_DIR)/enable $(RUST_DIR)/eval $(RUST_DIR)/exit $(RUST_DIR)/fc $(RUST_DIR)/fg_bg $(RUST_DIR)/getopts $(RUST_DIR)/hash $(RUST_DIR)/help $(RUST_DIR)/history $(RUST_DIR)/jobs $(RUST_DIR)/kill $(RUST_DIR)/mapfile $(RUST_DIR)/printf $(RUST_DIR)/pushd $(RUST_DIR)/read $(RUST_DIR)/rlet $(RUST_DIR)/rreturn $(RUST_DIR)/set $(RUST_DIR)/setattr $(RUST_DIR)/shift $(RUST_DIR)/shopt $(RUST_DIR)/source $(RUST_DIR)/suspend $(RUST_DIR)/test $(RUST_DIR)/times $(RUST_DIR)/trap $(RUST_DIR)/type $(RUST_DIR)/ulimit $(RUST_DIR)/umask $(RUST_DIR)/wait $(RUST_DIR)/exec +RUST_BUILTINS_DIRS = $(RUST_DIR)/alias $(RUST_DIR)/bind $(RUST_DIR)/break_1 $(RUST_DIR)/builtin $(RUST_DIR)/caller $(RUST_DIR)/cd $(RUST_DIR)/colon $(RUST_DIR)/command $(RUST_DIR)/common $(RUST_DIR)/complete $(RUST_DIR)/declare $(RUST_DIR)/echo $(RUST_DIR)/enable $(RUST_DIR)/eval $(RUST_DIR)/exit $(RUST_DIR)/fc $(RUST_DIR)/fg_bg $(RUST_DIR)/getopts $(RUST_DIR)/hash $(RUST_DIR)/help $(RUST_DIR)/history $(RUST_DIR)/jobs $(RUST_DIR)/kill $(RUST_DIR)/mapfile $(RUST_DIR)/printf $(RUST_DIR)/pushd $(RUST_DIR)/read $(RUST_DIR)/rlet $(RUST_DIR)/rreturn $(RUST_DIR)/set $(RUST_DIR)/setattr $(RUST_DIR)/shift $(RUST_DIR)/shopt $(RUST_DIR)/source $(RUST_DIR)/suspend $(RUST_DIR)/test $(RUST_DIR)/times $(RUST_DIR)/trap $(RUST_DIR)/type $(RUST_DIR)/ulimit $(RUST_DIR)/umask $(RUST_DIR)/wait $(RUST_DIR)/exec $(RUST_DIR)/exec_cmd # Keep GNU Make from exporting the entire environment for small machines. diff --git a/bash-5.1/builtins/history.def b/bash-5.1/builtins/history.def index 5db44c2..930ad50 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 d090a45..f4dad26 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 65c3cb4..831aeb3 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 0a5f489..5b51a97 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 03c98eb..bc24410 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 bb9af01..32584dd 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 bd9a203..c190383 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 f31f433..f8391c5 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 daeec9e..d2ba591 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/cd/src/lib.rs b/bash-5.1/builtins_rust/cd/src/lib.rs index bd94b2c..1025580 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/exec/src/lib.rs b/bash-5.1/builtins_rust/exec/src/lib.rs index 530e99e..ca8399d 100644 --- a/bash-5.1/builtins_rust/exec/src/lib.rs +++ b/bash-5.1/builtins_rust/exec/src/lib.rs @@ -4,10 +4,25 @@ 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}; +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; -use rcommon::r_builtin_usage; + +/* +#[repr (C)] +#[derive(Copy,Clone)] +pub struct WordDesc{ + pub word:*mut c_char, + pub flags:c_int, +} + +#[repr (C)] +#[derive(Copy,Clone)] +pub struct WordList{ + pub next:*mut WordList, + pub word:*mut WordDesc, +} +*/ #[repr(C)] struct redirect{ next:*mut redirect, diff --git a/bash-5.1/builtins_rust/exec_cmd/Cargo.toml b/bash-5.1/builtins_rust/exec_cmd/Cargo.toml index b120c60..9a416d5 100644 --- a/bash-5.1/builtins_rust/exec_cmd/Cargo.toml +++ b/bash-5.1/builtins_rust/exec_cmd/Cargo.toml @@ -50,7 +50,7 @@ rtype = {path="../type"} rulimit = {path="../ulimit"} rumask = {path="../umask"} rwait = {path="../wait"} -libc = "0.2" + [lib] crate-type = ["staticlib","rlib"] name = "rexec_cmd" diff --git a/bash-5.1/builtins_rust/exec_cmd/src/lib.rs b/bash-5.1/builtins_rust/exec_cmd/src/lib.rs index 0779d99..80f93ef 100644 --- a/bash-5.1/builtins_rust/exec_cmd/src/lib.rs +++ b/bash-5.1/builtins_rust/exec_cmd/src/lib.rs @@ -1,4 +1,4 @@ -use rcommon::{WordList, WordDesc,}; +use rcommon::{WordList, WordDesc}; use ralias::r_alias_builtin; use rbind::r_bind_builtin; use rbreak::r_break_builtin; @@ -7,7 +7,7 @@ use rcaller::r_caller_builtin; use rcd::r_cd_builtin; //use rcmd::r_cmd_builtin; use rcolon::r_colon_builtin; -use command::r_command_builtin; +//use command::command_builtin; //use rcommon ::r__builtin; use rcomplete::r_complete_builtin; use rdeclare::r_declare_builtin; @@ -43,9 +43,6 @@ use rtype::r_type_builtin; use rulimit::r_ulimit_builtin; use rumask::r_umask_builtin; use rwait::r_wait_builtin; -use std::ffi::CStr; -use std::ffi::CString; -use libc::{strcmp}; enum CMDType { AliasCmd, @@ -96,270 +93,260 @@ enum CMDType { struct AliasComand ; impl CommandExec for AliasComand{ - fn excute(&self,list : *mut WordList)-> i32{ + fn excute(&self,list : *mut WordList){ unsafe { - r_alias_builtin(list) + r_alias_builtin(list); } } } struct BindComand; impl CommandExec for BindComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_bind_builtin(list) + fn excute(&self,list : *mut WordList){ + r_bind_builtin(list); } } struct BreakComand; impl CommandExec for BreakComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_break_builtin(list) + fn excute(&self,list : *mut WordList){ + r_break_builtin(list); } } struct BuiltinComand; impl CommandExec for BuiltinComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_builtin_builtin(list) + fn excute(&self,list : *mut WordList){ + r_builtin_builtin(list); } } struct CallerComand; impl CommandExec for CallerComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_caller_builtin(list) + fn excute(&self,list : *mut WordList){ + r_caller_builtin(list); } } struct CdComand; impl CommandExec for CdComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_cd_builtin(list) + fn excute(&self,list : *mut WordList){ + r_cd_builtin(list); } } struct ColonComand; impl CommandExec for ColonComand{ - fn excute(&self,list :*mut WordList)-> i32{ - r_colon_builtin(list) - // 0 + fn excute(&self,list :*mut WordList){ + // r_colon_builtin(list); } } struct CommandComand; impl CommandExec for CommandComand{ - fn excute(&self,list : *mut WordList)-> i32{ - unsafe { - r_command_builtin(list) - } - + fn excute(&self,list : *mut WordList){ + //command_builtin(list); } } struct CommonComand; impl CommandExec for CommonComand{ - fn excute(&self,list : *mut WordList)-> i32{ + fn excute(&self,list : *mut WordList){ unsafe { - r_alias_builtin(list) + r_alias_builtin(list); } } } struct CompleteComand; impl CommandExec for CompleteComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_complete_builtin(list) + fn excute(&self,list : *mut WordList){ + r_complete_builtin(list); } } struct DeclareComand; impl CommandExec for DeclareComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_declare_builtin(list) + fn excute(&self,list : *mut WordList){ + r_declare_builtin(list); } } struct EchoComand; impl CommandExec for EchoComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_echo_builtin(list) + fn excute(&self,list : *mut WordList){ + r_echo_builtin(list); } } struct EnableComand; impl CommandExec for EnableComand{ - fn excute(&self,list : *mut WordList)-> i32{ + fn excute(&self,list : *mut WordList){ unsafe { - println!("=======================d============="); - r_enable_builtin(list) + r_enable_builtin(list); } } } struct EvalComand; impl CommandExec for EvalComand{ - fn excute(&self,list : *mut WordList)-> i32{ - // r_eval_builtin(list) - 0 + fn excute(&self,list : *mut WordList){ + r_eval_builtin(list); } } struct ExecComand; impl CommandExec for ExecComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_exec_builtin(list) + fn excute(&self,list : *mut WordList){ + r_exec_builtin(list); } } struct ExitComand; impl CommandExec for ExitComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_exit_builtin(list) + fn excute(&self,list : *mut WordList){ + r_exit_builtin(list); } } struct FcComand; impl CommandExec for FcComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_fc_builtin(list) + fn excute(&self,list : *mut WordList){ + r_fc_builtin(list); } } struct FgbgComand; impl CommandExec for FgbgComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_fg_builtin(list) + fn excute(&self,list : *mut WordList){ + r_fg_builtin(list); } } struct GetoptsComand; impl CommandExec for GetoptsComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_getopts_builtin(list) + fn excute(&self,list : *mut WordList){ + r_getopts_builtin(list); } } struct HashComand; impl CommandExec for HashComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_hash_builtin(list) + fn excute(&self,list : *mut WordList){ + r_hash_builtin(list); } } struct HelpComand; impl CommandExec for HelpComand{ - fn excute(&self,list : *mut WordList)-> i32{ - println!("==============help========"); - r_help_builtin(list) + fn excute(&self,list : *mut WordList){ + r_help_builtin(list); } } struct HistoryComand; impl CommandExec for HistoryComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_history_builtin(list) + fn excute(&self,list : *mut WordList){ + r_history_builtin(list); } } struct JobsComand; impl CommandExec for JobsComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_jobs_builtin(list) + fn excute(&self,list : *mut WordList){ + r_jobs_builtin(list); } } struct KillComand; impl CommandExec for KillComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_kill_builtin(list) + fn excute(&self,list : *mut WordList){ + r_kill_builtin(list); } } struct LetComand; impl CommandExec for LetComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_let_builtin(list) + fn excute(&self,list : *mut WordList){ + r_let_builtin(list); } } struct MapfileComand; impl CommandExec for MapfileComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_mapfile_builtin(list) + fn excute(&self,list : *mut WordList){ + r_mapfile_builtin(list); } } struct PrintfComand; impl CommandExec for PrintfComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_printf_builtin(list) + fn excute(&self,list : *mut WordList){ + r_printf_builtin(list); } } struct PushdComand; impl CommandExec for PushdComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_pushd_builtin(list) + fn excute(&self,list : *mut WordList){ + r_pushd_builtin(list); } } struct ReadComand; impl CommandExec for ReadComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_read_builtin(list) + fn excute(&self,list : *mut WordList){ + r_read_builtin(list); } } struct ReservedComand; impl CommandExec for ReservedComand{ - fn excute(&self,list : *mut WordList)-> i32{ + fn excute(&self,list : *mut WordList){ //r_reserve_builtin(list); - 0 } } struct ReturnComand; impl CommandExec for ReturnComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_return_builtin(list) + fn excute(&self,list : *mut WordList){ + r_return_builtin(list); } } struct SetattrComand; impl CommandExec for SetattrComand{ - fn excute(&self,list : *mut WordList)-> i32{ + fn excute(&self,list : *mut WordList){ //r_setattr_builtin(list); /*unkown enter which func */ - 0 } } struct SetComand; impl CommandExec for SetComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_set_builtin(list) + fn excute(&self,list : *mut WordList){ + r_set_builtin(list); } } struct ShiftComand; impl CommandExec for ShiftComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_shift_builtin(list) + fn excute(&self,list : *mut WordList){ + r_shift_builtin(list); } } struct ShoptComand; impl CommandExec for ShoptComand{ - fn excute(&self,list : *mut WordList)-> i32{ - // unsafe { - // r_shopt_builtin(list) - // } - 0 - } + fn excute(&self,list : *mut WordList){ + unsafe { + r_shopt_builtin(list); + } + } } struct SourceComand; impl CommandExec for SourceComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_source_builtin(list) + fn excute(&self,list : *mut WordList){ + r_source_builtin(list); } } struct SuspendComand; impl CommandExec for SuspendComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_suspend_builtin(list) + fn excute(&self,list : *mut WordList){ + r_suspend_builtin(list); } } struct TestComand; impl CommandExec for TestComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_test_builtin(list) + fn excute(&self,list : *mut WordList){ + r_test_builtin(list); } } struct TimesComand; impl CommandExec for TimesComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_times_builtin(list) + fn excute(&self,list : *mut WordList){ + r_times_builtin(list); } } struct TrapComand; impl CommandExec for TrapComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_trap_builtin(list) + fn excute(&self,list : *mut WordList){ + r_trap_builtin(list); } } struct TypeComand; impl CommandExec for TypeComand{ - fn excute(&self,list : *mut WordList)-> i32{ + fn excute(&self,list : *mut WordList){ unsafe { - r_type_builtin(list) + r_type_builtin(list); } } @@ -367,29 +354,29 @@ enum CMDType { } struct UlimitComand; impl CommandExec for UlimitComand{ - fn excute(&self,list : *mut WordList)-> i32{ + fn excute(&self,list : *mut WordList){ unsafe { - r_ulimit_builtin(list) + r_ulimit_builtin(list); } } } struct UmaskComand; impl CommandExec for UmaskComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_umask_builtin(list) + fn excute(&self,list : *mut WordList){ + r_umask_builtin(list); } } struct WaitComand; impl CommandExec for WaitComand{ - fn excute(&self,list : *mut WordList)-> i32{ - r_wait_builtin(list) + fn excute(&self,list : *mut WordList){ + r_wait_builtin(list); } } // 定义接口 pub trait CommandExec { - fn excute(&self,list : *mut WordList) -> i32; + fn excute(&self,list : *mut WordList); } @@ -544,173 +531,146 @@ impl Factory for SimpleFactory { } } -unsafe fn get_cmd_type (command : *mut i8) -> CMDType{ +fn get_cmd_type (command : *mut i8) -> CMDType{ let mut types = CMDType::HelpCmd; - if libc::strcmp(command, b"alias\0" as *const u8 as *const i8 as *mut i8) == 0{ + if command == b"alias\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::AliasCmd; } - else if libc::strcmp(command, b"bind\0" as *const u8 as *const i8 as *mut i8) == 0{ + else if command == b"bind\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::BindCmd; } - else if libc::strcmp(command, b"break\0" as *const u8 as *const i8 as *mut i8) == 0 { + else if command == b"break\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::BreakCmd; } - else if libc::strcmp(command, b"builtin\0" as *const u8 as *const i8 as *mut i8) == 0{ + else if command == b"builtin\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::BuiltinCmd; } - else if libc::strcmp(command, b"caller\0" as *const u8 as *const i8 as *mut i8) == 0{ + else if command == b"caller\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::CallerCmd; } - else if libc::strcmp(command, b"cd\0" as *const u8 as *const i8 as *mut i8) == 0{ + else if command == b"cd\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::CdCmd; } - else if libc::strcmp(command, b"colon\0" as *const u8 as *const i8 as *mut i8) == 0{ + else if command == b"colon\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::ColonCmd; } - else if libc::strcmp(command, b"command\0" as *const u8 as *const i8 as *mut i8) == 0{ + else if command == b"command\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::CommandCmd; } - else if libc::strcmp(command, b"common\0" as *const u8 as *const i8 as *mut i8) == 0{ + else if command == b"common\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::CommonCmd; } - else if libc::strcmp(command, b"complete\0" as *const u8 as *const i8 as *mut i8) == 0{ + else if command == b"complete\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::CompleteCmd; } - else if libc::strcmp(command,b"declare\0" as *const u8 as *const i8 as *mut i8) == 0{ - types = CMDType::CompleteCmd; + else if command == b"declare\0" as *const u8 as *const i8 as *mut i8{ + types = CMDType::DeclareCmd; } - else if libc::strcmp(command,b"echo\0" as *const u8 as *const i8 as *mut i8) == 0{ + else if command == b"echo\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::EchoCmd; } - - else if libc::strcmp(command,b"enable\0" as *const u8 as *const i8 as *mut i8) == 0{ + else if command == b"enable\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::EnableCmd; } - else if libc::strcmp(command,b"eval\0" as *const u8 as *const i8 as *mut i8) == 0{ + else if command == b"eval\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::EvalCmd; } - - else if libc::strcmp(command,b"exec\0" as *const u8 as *const i8 as *mut i8) == 0{ + else if command == b"exec\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::ExecCmd; } - - else if libc::strcmp(command,b"exit\0" as *const u8 as *const i8 as *mut i8) == 0{ + else if command == b"exit\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::ExitCmd; } - - else if libc::strcmp(command,b"fc\0" as *const u8 as *const i8 as *mut i8) == 0 { + else if command == b"fc\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::FcCmd; } - - else if libc::strcmp(command,b"fg\0" as *const u8 as *const i8 as *mut i8) == 0{ + else if command == b"fg_bg\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::FgbgCmd; } - - else if libc::strcmp(command,b"getopts\0" as *const u8 as *const i8 as *mut i8) == 0 { + else if command == b"getopts\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::GetoptsCmd; } - - else if libc::strcmp(command, b"hash\0" as *const u8 as *const i8 as *mut i8) == 0{ + else if command == b"hash\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::HashCmd; } - else if libc::strcmp(command,CString::new("help").unwrap().as_ptr() as * mut i8) == 0 { - println!("222222222========222222"); + else if command == b"help\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::HelpCmd; } - else if libc::strcmp(command,b"history\0" as *const u8 as *const i8 as *mut i8) == 0 { + else if command == b"history\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::HistoryCmd; } - else if libc::strcmp(command,b"jobs\0" as *const u8 as *const i8 as *mut i8) == 0{ + else if command == b"jobs\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::JobsCmd; } - else if libc::strcmp(command, b"kill\0" as *const u8 as *const i8 as *mut i8) == 0{ + else if command == b"kill\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::KillCmd; + } + else if command == b"mapfile\0" as *const u8 as *const i8 as *mut i8{ + types = CMDType::MapfileCmd; } - else if libc::strcmp(command, b"mapfile\0" as *const u8 as *const i8 as *mut i8) == 0{ - types = CMDType::MapfileCmd;; - } - else if libc::strcmp(command,b"printf\0" as *const u8 as *const i8 as *mut i8) == 0{ + else if command == b"printf\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::PrintfCmd; } - else if libc::strcmp(command,b"pushd\0" as *const u8 as *const i8 as *mut i8) == 0{ + else if command == b"pushd\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::PushdCmd; } - else if libc::strcmp(command, b"read\0" as *const u8 as *const i8 as *mut i8) == 0{ + else if command == b"read\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::ReadCmd; - } - - else if libc::strcmp(command, b"let\0" as *const u8 as *const i8 as *mut i8) == 0{ + } + else if command == b"let\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::LetCmd; } - - else if libc::strcmp(command,b"return\0" as *const u8 as *const i8 as *mut i8) == 0 { + else if command == b"return\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::ReturnCmd; - } - else if libc::strcmp(command,b"set\0" as *const u8 as *const i8 as *mut i8) == 0 { + + } + else if command == b"set\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::SetCmd; } - - else if libc::strcmp(command,b"setattr\0" as *const u8 as *const i8 as *mut i8) == 0{ + else if command == b"setattr\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::SetattrCmd; - } - - else if libc::strcmp(command,b"shift\0" as *const u8 as *const i8 as *mut i8) == 0{ + } + else if command == b"shift\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::ShiftCmd; } - - else if libc::strcmp(command,b"shopt\0" as *const u8 as *const i8 as *mut i8) == 0{ + else if command == b"shopt\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::ShoptCmd; } - - else if libc::strcmp(command,b"source\0" as *const u8 as *const i8 as *mut i8) == 0 - || libc::strcmp(command,b".\0" as *const u8 as *const i8 as *mut i8)== 0 { + else if command == b"source\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::SourceCmd; } - - else if libc::strcmp(command, b"suspend\0" as *const u8 as *const i8 as *mut i8) == 0{ + else if command == b"suspend\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::SuspendCmd; } - - else if libc::strcmp(command,b"test\0" as *const u8 as *const i8 as *mut i8) == 0{ + else if command == b"test\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::TestCmd; } - - - else if libc::strcmp(command ,b"times\0" as *const u8 as *const i8 as *mut i8) == 0 { + else if command == b"times\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::TimesCmd; } - else if libc::strcmp(command ,b"trap\0" as *const u8 as *const i8 as *mut i8) == 0 { + else if command == b"trap\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::TrapCmd; } - - else if libc::strcmp(command ,b"type\0" as *const u8 as *const i8 as *mut i8) == 0 { + else if command == b"type\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::TypeCmd; } - - else if libc::strcmp(command ,b"ulimit\0" as *const u8 as *const i8 as *mut i8) == 0{ + else if command == b"ulimit\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::UlimitCmd; } - - else if libc::strcmp(command ,b"umask\0" as *const u8 as *const i8 as *mut i8 ) == 0{ + else if command == b"umask\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::UmaskCmd; - } - - else if libc::strcmp(command , b"wait\0" as *const u8 as *const i8 as *mut i8) == 0 { + } + else if command == b"wait\0" as *const u8 as *const i8 as *mut i8{ types = CMDType::WaitCmd; - } - - types + } + return types } #[no_mangle] -pub extern "C" fn r_exec_cmd(command : *mut i8, mut list :*mut WordList) -> i32 { +pub extern "C" fn r_exec_cmd(command : *mut i8, mut list :*mut WordList) { - println!("enter r_exec_cmd"); - unsafe { - println!("command is {:?}",CStr::from_ptr(command)); - } - let commandType = unsafe {get_cmd_type(command)}; + let commandType = get_cmd_type(command); let factory = SimpleFactory::new(); let cmdCall = factory.make_product(commandType); - cmdCall.excute(list) + cmdCall.excute(list); } \ No newline at end of file diff --git a/bash-5.1/builtins_rust/exit/src/lib.rs b/bash-5.1/builtins_rust/exit/src/lib.rs index 1cde64a..0f74aab 100644 --- a/bash-5.1/builtins_rust/exit/src/lib.rs +++ b/bash-5.1/builtins_rust/exit/src/lib.rs @@ -169,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{ diff --git a/bash-5.1/builtins_rust/fc/src/lib.rs b/bash-5.1/builtins_rust/fc/src/lib.rs index 1d03ac4..47d9e0b 100644 --- a/bash-5.1/builtins_rust/fc/src/lib.rs +++ b/bash-5.1/builtins_rust/fc/src/lib.rs @@ -1,6 +1,7 @@ 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}; diff --git a/bash-5.1/builtins_rust/help/Cargo.toml b/bash-5.1/builtins_rust/help/Cargo.toml index 50f09b2..993d13e 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 ca3fe77..2832137 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} @@ -154,7 +131,7 @@ pub extern "C" fn r_help_builtin(mut list:*mut WordList)->i32 { 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 077183e..520f0cf 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 6ee88c6..5d8d8f7 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 d1d3956..8450248 100644 --- a/bash-5.1/builtins_rust/mapfile/src/intercdep.rs +++ b/bash-5.1/builtins_rust/mapfile/src/intercdep.rs @@ -1,19 +1,4 @@ - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct word_desc { - pub word: *mut c_char, - pub flags: c_int, -} -pub type WordDesc = word_desc; - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct word_list { - pub next: *mut word_list, - pub word: *mut WordDesc, -} -pub type WordList = word_list; +use rcommon::{WordList}; pub type arrayind_t = c_long; @@ -30,6 +15,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 512d37f..4fa35f5 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 5fd1594..db66567 100644 --- a/bash-5.1/builtins_rust/printf/src/intercdep.rs +++ b/bash-5.1/builtins_rust/printf/src/intercdep.rs @@ -1,20 +1,4 @@ -#[repr(C)] -#[derive(Copy, Clone)] -pub struct word_desc { - pub word: *mut c_char, - pub flags: c_int, -} -pub type WordDesc = word_desc; - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct word_list { - pub next: *mut word_list, - pub word: *mut WordDesc, -} -pub type WordList = word_list; - -// pub type SHELL_VAR = variable; +use rcommon::{WordList}; pub type __intmax_t = c_long; pub type intmax_t = __intmax_t; @@ -89,7 +73,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 +98,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 ce4971f..1a29c02 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 cfd6cdc..51804e8 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 8a823e2..b8bfcb0 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/intercdep.rs b/bash-5.1/builtins_rust/read/src/intercdep.rs index 2fed8da..60570c2 100644 --- a/bash-5.1/builtins_rust/read/src/intercdep.rs +++ b/bash-5.1/builtins_rust/read/src/intercdep.rs @@ -1,19 +1,4 @@ - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct word_desc { - pub word: *mut c_char, - pub flags: c_int, -} -pub type WordDesc = word_desc; - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct word_list { - pub next: *mut word_list, - pub word: *mut WordDesc, -} -pub type WordList = word_list; +use rcommon::{WordList}; use rcommon::{r_builtin_usage,r_sh_invalidid,r_builtin_bind_variable}; //use rcommon::{r_builtin_usage,r_sh_invalidid,r_builtin_bind_variable,SHELL_VAR}; diff --git a/bash-5.1/builtins_rust/read/src/lib.rs b/bash-5.1/builtins_rust/read/src/lib.rs index 6fcffee..720c622 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/rlet/Cargo.toml b/bash-5.1/builtins_rust/rlet/Cargo.toml index c468265..b52101c 100644 --- a/bash-5.1/builtins_rust/rlet/Cargo.toml +++ b/bash-5.1/builtins_rust/rlet/Cargo.toml @@ -14,3 +14,4 @@ crate-type = ["staticlib","rlib"] [dependencies] libc = "0.2" nix = "0.23" +rcommon = {path = "../common"} \ No newline at end of file diff --git a/bash-5.1/builtins_rust/rlet/src/intercdep.rs b/bash-5.1/builtins_rust/rlet/src/intercdep.rs index fda0e61..d37ae34 100644 --- a/bash-5.1/builtins_rust/rlet/src/intercdep.rs +++ b/bash-5.1/builtins_rust/rlet/src/intercdep.rs @@ -1,19 +1,4 @@ - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct word_desc { - pub word: *mut c_char, - pub flags: c_int, -} -pub type WordDesc = word_desc; - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct word_list { - pub next: *mut word_list, - pub word: *mut WordDesc, -} -pub type WordList = word_list; +use rcommon::{WordList}; pub const EXECUTION_SUCCESS : c_int = 0; pub const EXECUTION_FAILURE : c_int = 1; diff --git a/bash-5.1/builtins_rust/set/Cargo.toml b/bash-5.1/builtins_rust/set/Cargo.toml index 0a60e09..a437a26 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 24ab4ec..32066f4 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/setattr/Cargo.toml b/bash-5.1/builtins_rust/setattr/Cargo.toml index 1e1d381..90004e4 100644 --- a/bash-5.1/builtins_rust/setattr/Cargo.toml +++ b/bash-5.1/builtins_rust/setattr/Cargo.toml @@ -18,3 +18,4 @@ rdeclare = {path = "../declare"} libc = "0.2" nix = "0.23" rdeclare = {path = "../declare"} +rcommon = {path = "../common"} \ No newline at end of file diff --git a/bash-5.1/builtins_rust/setattr/src/intercdep.rs b/bash-5.1/builtins_rust/setattr/src/intercdep.rs index 98a323e..8c46559 100644 --- a/bash-5.1/builtins_rust/setattr/src/intercdep.rs +++ b/bash-5.1/builtins_rust/setattr/src/intercdep.rs @@ -1,4 +1,4 @@ - +use rcommon::{WordList}; #[repr(C)] #[derive(Copy, Clone)] pub struct word_desc { @@ -7,14 +7,6 @@ pub struct word_desc { } pub type WordDesc = word_desc; -#[repr(C)] -#[derive(Copy, Clone)] -pub struct word_list { - pub next: *mut word_list, - pub word: *mut WordDesc, -} -pub type WordList = word_list; - pub type __intmax_t = c_long; pub type intmax_t = __intmax_t; pub type arrayind_t = intmax_t; diff --git a/bash-5.1/builtins_rust/shift/src/intercdep.rs b/bash-5.1/builtins_rust/shift/src/intercdep.rs index 5e0851b..7130cc6 100644 --- a/bash-5.1/builtins_rust/shift/src/intercdep.rs +++ b/bash-5.1/builtins_rust/shift/src/intercdep.rs @@ -1,19 +1,4 @@ - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct word_desc { - pub word: *mut c_char, - pub flags: c_int, -} -pub type WordDesc = word_desc; - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct word_list { - pub next: *mut word_list, - pub word: *mut WordDesc, -} -pub type WordList = word_list; +use rcommon::{WordList}; pub const EXECUTION_SUCCESS : c_int = 0; pub const EXECUTION_FAILURE : c_int = 1; diff --git a/bash-5.1/builtins_rust/shopt/src/lib.rs b/bash-5.1/builtins_rust/shopt/src/lib.rs index ff0a319..c557ab7 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 91e3163..8057a26 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 53de38f..fcbbf1e 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/times/src/intercdep.rs b/bash-5.1/builtins_rust/times/src/intercdep.rs index eb18f38..c2f5d32 100644 --- a/bash-5.1/builtins_rust/times/src/intercdep.rs +++ b/bash-5.1/builtins_rust/times/src/intercdep.rs @@ -1,19 +1,4 @@ - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct word_desc { - pub word: *mut c_char, - pub flags: c_int, -} -pub type WordDesc = word_desc; - -#[repr(C)] -#[derive(Copy, Clone)] -pub struct word_list { - pub next: *mut word_list, - pub word: *mut WordDesc, -} -pub type WordList = word_list; +use rcommon::WordList; pub const EXECUTION_SUCCESS : c_int = 0; pub const EXECUTION_FAILURE : c_int = 1; diff --git a/bash-5.1/builtins_rust/type/Cargo.toml b/bash-5.1/builtins_rust/type/Cargo.toml index 08ffe40..01ab1e9 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 d4374c9..c2318bf 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 4fbd136..0e03d96 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 adf0be5..2ed26a2 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 9f2b72a..6e15373 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 98d236a..7936203 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 fed710a..26f34bd 100644 --- a/record.txt +++ b/record.txt @@ -51,3 +51,4 @@ 50 51 52 +53 -- Gitee