From bb8055ac5d60d8b68524f81df2d97199503e5e50 Mon Sep 17 00:00:00 2001 From: liutong Date: Tue, 27 Jun 2023 21:27:29 +0800 Subject: [PATCH 1/6] modify jobs --- bash-5.1/Cargo.toml | 2 - bash-5.1/Makefile.in | 4 +- bash-5.1/builtins/bind.def | 2 - bash-5.1/builtins/break.def | 25 +- bash-5.1/builtins/command.def | 2 - bash-5.1/builtins/complete.def | 2 - bash-5.1/builtins/declare.def | 5 +- 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/pushd.def | 11 +- bash-5.1/builtins/return.def | 5 - bash-5.1/builtins/shift.def | 4 - bash-5.1/builtins/source.def | 6 +- 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 | 73 ++-- bash-5.1/builtins_rust/bind/src/lib.rs | 120 ++++-- bash-5.1/builtins_rust/break_1/Cargo.toml | 1 - bash-5.1/builtins_rust/break_1/src/lib.rs | 68 +-- bash-5.1/builtins_rust/builtin/Cargo.toml | 1 - .../builtins_rust/builtin/src/intercdep.rs | 21 +- bash-5.1/builtins_rust/builtin/src/lib.rs | 4 +- bash-5.1/builtins_rust/caller/Cargo.toml | 1 - bash-5.1/builtins_rust/caller/src/lib.rs | 100 ++++- bash-5.1/builtins_rust/cd/Cargo.toml | 1 - bash-5.1/builtins_rust/cd/src/lib.rs | 41 +- bash-5.1/builtins_rust/colon/Cargo.toml | 1 - bash-5.1/builtins_rust/colon/src/lib.rs | 16 +- bash-5.1/builtins_rust/command/Cargo.toml | 1 - bash-5.1/builtins_rust/command/src/lib.rs | 26 +- bash-5.1/builtins_rust/common/src/lib.rs | 41 +- bash-5.1/builtins_rust/complete/Cargo.toml | 1 - bash-5.1/builtins_rust/complete/src/lib.rs | 66 ++- bash-5.1/builtins_rust/declare/src/lib.rs | 42 +- bash-5.1/builtins_rust/echo/Cargo.toml | 1 - bash-5.1/builtins_rust/echo/src/lib.rs | 19 +- bash-5.1/builtins_rust/enable/Cargo.toml | 1 - bash-5.1/builtins_rust/enable/src/lib.rs | 39 +- bash-5.1/builtins_rust/eval/src/lib.rs | 33 +- bash-5.1/builtins_rust/exec/src/lib.rs | 148 +++++-- bash-5.1/builtins_rust/exit/Cargo.toml | 1 - bash-5.1/builtins_rust/exit/src/lib.rs | 34 +- 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 | 32 +- bash-5.1/builtins_rust/help/Cargo.toml | 1 - bash-5.1/builtins_rust/help/src/lib.rs | 34 +- .../builtins_rust/history/src/intercdep.rs | 3 +- bash-5.1/builtins_rust/history/src/lib.rs | 90 ++-- bash-5.1/builtins_rust/jobs/src/lib.rs | 7 + 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 | 65 ++- bash-5.1/builtins_rust/read/src/intercdep.rs | 30 +- bash-5.1/builtins_rust/read/src/lib.rs | 392 ++++++------------ 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 | 42 +- 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 + 84 files changed, 1601 insertions(+), 769 deletions(-) diff --git a/bash-5.1/Cargo.toml b/bash-5.1/Cargo.toml index 0c967670..595ff04a 100644 --- a/bash-5.1/Cargo.toml +++ b/bash-5.1/Cargo.toml @@ -61,7 +61,6 @@ members=[ "builtins_rust/trap", "builtins_rust/setattr", "builtins_rust/source", - "builtins_rust/exec_cmd", ] [dependencies] @@ -101,4 +100,3 @@ 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 30ddc175..019ead7c 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 -lrcommand -lrcomplete -lrdeclare -lrecho -lrenable -lreval -lrexec -lrexit -lrfc -lrfg_bg -lrgetopts -lrhash -lrexit -lrhelp -lrhistory -lrjobs -lrkill -lrmapfile -lrpushd -lrread -lrlet -lrreturn -lrset -lrsetattr -lrshift -lrshopt -lrsource -lrsuspend -lrtest -lrtimes -lrtrap -lrtype -lrulimit -lrumask -lrwait -lrprintf -lrexec_cmd +LIBS = $(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_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_DIR)/exec_cmd +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 # Keep GNU Make from exporting the entire environment for small machines. diff --git a/bash-5.1/builtins/bind.def b/bash-5.1/builtins/bind.def index 72ace6d2..7fc8aba4 100644 --- a/bash-5.1/builtins/bind.def +++ b/bash-5.1/builtins/bind.def @@ -108,7 +108,6 @@ static int unbind_keyseq PARAMS((char *)); #define XXFLAG 0x2000 extern int r_bind_builtin(WORD_LIST *); -extern int r_unbind_builtin(WORD_LIST *); int bind_builtin (list) @@ -153,7 +152,6 @@ static int unbind_command (name) char *name; { -//return r_unbind_builtin(name); rl_command_func_t *function; function = rl_named_function (name); diff --git a/bash-5.1/builtins/break.def b/bash-5.1/builtins/break.def index f71bc0de..346b632d 100644 --- a/bash-5.1/builtins/break.def +++ b/bash-5.1/builtins/break.def @@ -144,4 +144,27 @@ check_loop_level () return (loop_level); } - +extern void set_loop_level(int i) +{ + loop_level = i; +} +extern int get_loop_level(void ) +{ + return loop_level; +} +extern void set_continuing(int i) +{ + continuing = i; +} +extern int get_continuing() +{ + return continuing; +} +extern void set_breaking(int i) +{ + breaking = i ; +} +extern int get_breaking() +{ + return breaking; +} diff --git a/bash-5.1/builtins/command.def b/bash-5.1/builtins/command.def index f538122d..acd46cc1 100644 --- a/bash-5.1/builtins/command.def +++ b/bash-5.1/builtins/command.def @@ -60,14 +60,12 @@ $END extern size_t confstr PARAMS((int, char *, size_t)); #endif -int r_command_builtin ( WORD_LIST *list); /* Run the commands mentioned in LIST without paying attention to shell functions. */ int command_builtin (list) WORD_LIST *list; { -return r_command_builtin ( list); int result, verbose, use_standard_path, opt; COMMAND *command; diff --git a/bash-5.1/builtins/complete.def b/bash-5.1/builtins/complete.def index 97334197..28a9ec2f 100644 --- a/bash-5.1/builtins/complete.def +++ b/bash-5.1/builtins/complete.def @@ -365,13 +365,11 @@ build_actions (list, flagp, actp, optp) return (opt_given ? EXECUTION_SUCCESS : EXECUTION_FAILURE); } -int r_complete_builtin ( WORD_LIST *list); /* Add, remove, and display completion specifiers. */ int complete_builtin (list) WORD_LIST *list; { -return r_complete_builtin (list); int opt_given, rval; unsigned long acts, copts; COMPSPEC *cs; diff --git a/bash-5.1/builtins/declare.def b/bash-5.1/builtins/declare.def index aee4afb1..21e4516d 100644 --- a/bash-5.1/builtins/declare.def +++ b/bash-5.1/builtins/declare.def @@ -92,14 +92,13 @@ $END static SHELL_VAR *declare_find_variable PARAMS((const char *, int, int)); static int declare_internal PARAMS((register WORD_LIST *, int)); -int r_declare_internal PARAMS((register WORD_LIST *, int)); /* Declare or change variable attributes. */ int declare_builtin (list) register WORD_LIST *list; { - return (r_declare_internal (list, 0)); + return (declare_internal (list, 0)); } $BUILTIN local @@ -129,7 +128,7 @@ local_builtin (list) } if (variable_context) - return (r_declare_internal (list, 1)); + return (declare_internal (list, 1)); else { builtin_error (_("can only be used in a function")); diff --git a/bash-5.1/builtins/history.def b/bash-5.1/builtins/history.def index 930ad500..5db44c2c 100644 --- a/bash-5.1/builtins/history.def +++ b/bash-5.1/builtins/history.def @@ -100,15 +100,224 @@ 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; { - return r_history_builtin(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)); + } - } + 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 f4dad260..d090a45b 100644 --- a/bash-5.1/builtins/let.def +++ b/bash-5.1/builtins/let.def @@ -78,16 +78,11 @@ $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 831aeb39..65c3cb4f 100644 --- a/bash-5.1/builtins/mapfile.def +++ b/bash-5.1/builtins/mapfile.def @@ -103,8 +103,6 @@ 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 @@ -246,9 +244,6 @@ 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 5b51a97f..0a5f4897 100644 --- a/bash-5.1/builtins/printf.def +++ b/bash-5.1/builtins/printf.def @@ -235,16 +235,10 @@ 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; @@ -315,7 +309,6 @@ printf_builtin (list) return (EXECUTION_SUCCESS); format = list->word->word; - tw = 0; garglist = orig_arglist = list->next; diff --git a/bash-5.1/builtins/pushd.def b/bash-5.1/builtins/pushd.def index de1bb2df..829f827d 100644 --- a/bash-5.1/builtins/pushd.def +++ b/bash-5.1/builtins/pushd.def @@ -169,13 +169,10 @@ static int get_dirstack_index PARAMS((intmax_t, int, int *)); #define LONGFORM 0x04 #define CLEARSTAK 0x08 -int r_pushd_builtin ( WORD_LIST *list); - int pushd_builtin (list) WORD_LIST *list; { -return r_pushd_builtin(list); WORD_LIST *orig_list; char *temp, *current_directory, *top; int j, flags, skipopt; @@ -315,15 +312,13 @@ return r_pushd_builtin(list); } } -int -r_popd_builtin ( - WORD_LIST *list); /* Pop the directory stack, and then change to the new top of the stack. If LIST is non-null it should consist of a word +N or -N, which says what element to delete from the stack. The default is the top one. */ -int popd_builtin ( WORD_LIST *list) +int +popd_builtin (list) + WORD_LIST *list; { -return r_popd_builtin(list); register int i; intmax_t which; int flags; diff --git a/bash-5.1/builtins/return.def b/bash-5.1/builtins/return.def index bc244108..03c98eb1 100644 --- a/bash-5.1/builtins/return.def +++ b/bash-5.1/builtins/return.def @@ -50,8 +50,6 @@ $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. */ @@ -59,9 +57,6 @@ 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 32584dd7..bb9af01b 100644 --- a/bash-5.1/builtins/shift.def +++ b/bash-5.1/builtins/shift.def @@ -49,8 +49,6 @@ $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 @@ -59,8 +57,6 @@ int shift_builtin (list) WORD_LIST *list; { - r_shift_builtin(list); - intmax_t times; int itimes, nargs; diff --git a/bash-5.1/builtins/source.def b/bash-5.1/builtins/source.def index 930d1a88..5b2f994d 100644 --- a/bash-5.1/builtins/source.def +++ b/bash-5.1/builtins/source.def @@ -108,14 +108,14 @@ maybe_pop_dollar_vars () invalidate_cached_quoted_dollar_at (); /* just invalidate to be safe */ } -int r_source_builtin ( WORD_LIST *list); /* Read and execute commands from the file passed as argument. Guess what. This cannot be done in a subshell, since things like variable assignments take place in there. So, I open the file, place it into a large string, close the file, and then execute the string. */ -int source_builtin ( WORD_LIST *list) +int +source_builtin (list) + WORD_LIST *list; { -return r_source_builtin (list); int result; char *filename, *debug_trap, *x; diff --git a/bash-5.1/builtins/test.def b/bash-5.1/builtins/test.def index c190383d..bd9a203b 100644 --- a/bash-5.1/builtins/test.def +++ b/bash-5.1/builtins/test.def @@ -130,16 +130,10 @@ $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 f8391c5b..f31f4333 100644 --- a/bash-5.1/builtins/times.def +++ b/bash-5.1/builtins/times.def @@ -57,17 +57,11 @@ $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 d2ba5918..daeec9ea 100644 --- a/bash-5.1/builtins/trap.def +++ b/bash-5.1/builtins/trap.def @@ -97,15 +97,10 @@ 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 0bc7078c..31bd0186 100644 --- a/bash-5.1/builtins_rust/alias/src/lib.rs +++ b/bash-5.1/builtins_rust/alias/src/lib.rs @@ -1,8 +1,5 @@ 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}; extern "C" { fn free(__ptr: *mut libc::c_void); @@ -15,12 +12,12 @@ extern "C" { fn legal_alias_name(_: *const libc::c_char, _: libc::c_int) -> libc::c_int; fn sh_single_quote(_: *const libc::c_char) -> *mut libc::c_char; static mut posixly_correct: libc::c_int; - static mut aliases: *mut HashTable; - fn find_alias(_: *mut libc::c_char) -> *mut AliasT; + static mut aliases: *mut HASH_TABLE; + fn find_alias(_: *mut libc::c_char) -> *mut alias_t; fn add_alias(_: *mut libc::c_char, _: *mut libc::c_char); fn remove_alias(_: *mut libc::c_char) -> libc::c_int; fn delete_all_aliases(); - fn all_aliases() -> *mut *mut AliasT; + fn all_aliases() -> *mut *mut alias_t; fn builtin_error(_: *const libc::c_char, _: ...); fn builtin_usage(); fn sh_notfound(_: *mut libc::c_char); @@ -30,8 +27,22 @@ extern "C" { fn internal_getopt(_: *mut WordList, _: *mut libc::c_char) -> libc::c_int; fn reset_internal_getopt(); } -pub type SizeT = libc::c_ulong; +pub type size_t = libc::c_ulong; +#[derive(Copy, Clone)] +#[repr(C)] +pub struct word_desc { + pub word: *mut libc::c_char, + pub flags: libc::c_int, +} +pub type WordDesc = word_desc; +#[derive(Copy, Clone)] +#[repr(C)] +pub struct word_list { + pub next: *mut word_list, + pub word: *mut WordDesc, +} +pub type WordList = word_list; #[derive(Copy, Clone)] #[repr(C)] pub struct bucket_contents { @@ -41,15 +52,15 @@ pub struct bucket_contents { pub khash: libc::c_uint, pub times_found: libc::c_int, } -pub type BucketContents = bucket_contents; +pub type BUCKET_CONTENTS = bucket_contents; #[derive(Copy, Clone)] #[repr(C)] pub struct hash_table { - pub bucket_array: *mut *mut BucketContents, + pub bucket_array: *mut *mut BUCKET_CONTENTS, pub nbuckets: libc::c_int, pub nentries: libc::c_int, } -pub type HashTable = hash_table; +pub type HASH_TABLE = hash_table; #[derive(Copy, Clone)] #[repr(C)] pub struct alias { @@ -57,23 +68,26 @@ pub struct alias { pub value: *mut libc::c_char, pub flags: libc::c_char, } -pub type AliasT = alias; +pub type alias_t = alias; pub static AL_REUSABLE:i32 = 0x01; - +pub static EX_USAGE:i32 = 258; +//extern crate rcommon; +use rcommon::EXECUTION_SUCCESS; +use rcommon::r_builtin_usage; #[no_mangle] pub unsafe extern "C" fn r_alias_builtin(mut list: *mut WordList) -> libc::c_int { println!("alias_builtin run!"); - let mut any_failed; - let mut offset; - let mut pflag ; - let mut dflags ; - let alias_list: *mut *mut AliasT; - let mut t: *mut AliasT; - let mut name: *mut libc::c_char; - let mut value: *mut libc::c_char; + let mut any_failed = 0; + let mut offset = 0; + let mut pflag = 0; + let mut dflags = 0; + let mut alias_list: *mut *mut alias_t; + let mut t: *mut alias_t; + let mut name: *mut libc::c_char = 0 as *mut libc::c_char; + let mut value: *mut libc::c_char = 0 as *mut libc::c_char; dflags = if posixly_correct != 0 { 0 as libc::c_int } else { 0x1 as libc::c_int }; pflag = 0 as libc::c_int; reset_internal_getopt(); @@ -103,11 +117,11 @@ pub unsafe extern "C" fn r_alias_builtin(mut list: *mut WordList) -> libc::c_int list = loptend; if list.is_null() || pflag != 0 { if aliases.is_null() { - return EXECUTION_SUCCESS!(); + return EXECUTION_SUCCESS; } alias_list = all_aliases(); if alias_list.is_null() { - return EXECUTION_SUCCESS!(); + return EXECUTION_SUCCESS; } offset = 0; while !(*alias_list.offset(offset as isize)).is_null() { @@ -116,7 +130,7 @@ pub unsafe extern "C" fn r_alias_builtin(mut list: *mut WordList) -> libc::c_int } free(alias_list as *mut libc::c_void); if list.is_null() { - return sh_chkwrite(EXECUTION_SUCCESS!()); + return sh_chkwrite(EXECUTION_SUCCESS); } } any_failed = 0; @@ -156,14 +170,13 @@ pub unsafe extern "C" fn r_alias_builtin(mut list: *mut WordList) -> libc::c_int } list = (*list).next; } - return if any_failed != 0 {EXECUTION_FAILURE!()} else { EXECUTION_SUCCESS!()}; + return if any_failed != 0 { rcommon::EXECUTION_FAILURE!()} else { EXECUTION_SUCCESS}; } #[no_mangle] pub unsafe extern "C" fn r_unalias_builtin(mut list: *mut WordList) -> libc::c_int { - println!("alias_builtin run!"); - let mut alias: *mut AliasT; - let mut opt: libc::c_int; - let mut aflag: libc::c_int; + let mut alias: *mut alias_t = 0 as *mut alias_t; + let mut opt: libc::c_int = 0; + let mut aflag: libc::c_int = 0; aflag = 0 as libc::c_int; reset_internal_getopt(); loop { @@ -210,8 +223,8 @@ pub unsafe extern "C" fn r_unalias_builtin(mut list: *mut WordList) -> libc::c_i } return if aflag != 0 { 1 as libc::c_int } else { 0 as libc::c_int }; } -unsafe extern "C" fn print_alias( alias: *mut AliasT, flags: libc::c_int) { - let value: *mut libc::c_char; +unsafe extern "C" fn print_alias(mut alias: *mut alias_t, mut flags: libc::c_int) { + let mut value: *mut libc::c_char = 0 as *mut libc::c_char; value = sh_single_quote((*alias).value); if flags & 0x1 as libc::c_int != 0 { printf( diff --git a/bash-5.1/builtins_rust/bind/src/lib.rs b/bash-5.1/builtins_rust/bind/src/lib.rs index 69fd41be..b065ae63 100644 --- a/bash-5.1/builtins_rust/bind/src/lib.rs +++ b/bash-5.1/builtins_rust/bind/src/lib.rs @@ -7,8 +7,20 @@ use std::{ffi::{CString,CStr}}; use std::fs::File; use nix::errno::errno; use rcommon::{r_builtin_usage,r_sh_chkwrite}; -use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; +//struct +#[repr (C)] +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)] pub struct _keymap_entry{ pub Type:c_char, @@ -19,6 +31,24 @@ type Keymap = *mut KEYMAP_ENTRY; type rl_command_func_t = extern fn(c_int,c_int)->c_int; //emun + + +//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! LFLAG { () => { 0x0001 } @@ -91,13 +121,17 @@ macro_rules! ANYOTHERKEY{ () => { KEYMAP_SIZE!() - 1} } +// #[macro_export] +// macro_rules! unwind_protect_var { +// ($X:expr) => { + +// unwind_protect_mem($X as *mut c_char,std::intrinsics::size_of(File)); +// // std::mem::size_of::<& REPL>() +// }; +// } +//extern "C" extern "C"{ - fn dcgettext( - __domainname: *const libc::c_char, - __msgid: *const libc::c_char, - __category: libc::c_int, - ) -> *mut libc::c_char; static no_line_editing:i32; static bash_readline_initialized:i32; static mut rl_outstream:*mut File; @@ -142,7 +176,6 @@ 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(); } @@ -165,14 +198,9 @@ pub extern "C" fn r_bind_builtin(mut list:*mut WordList)->i32{ unsafe{ if no_line_editing != 0{ - builtin_warning( - dcgettext( - 0 as *const libc::c_char, - CString::new("line editing not enabled").unwrap().as_ptr() as *const libc::c_char, - 5 as libc::c_int, - ), - ); - + let s_str = String::from("line editing not enabled"); + let s_ptr = s_str.as_ptr() as *const c_char; + builtin_warning(s_ptr); } kmap = std::ptr::null_mut(); @@ -190,19 +218,14 @@ pub extern "C" fn r_bind_builtin(mut list:*mut WordList)->i32{ if bash_readline_initialized == 0{ initialize_readline(); } - let bind_str = CString::new("bind_builtin").unwrap(); - // let bind_ptr = bind_str.as_ptr() as *mut c_char; - begin_unwind_frame(bind_str.as_ptr() as *mut c_char); -/* - unwind_protect_mem( - std::mem::transmute(rl_outstream), - std::mem::size_of_val(&rl_outstream) as c_int - ); -*/ - unwind_protect_mem( - &mut rl_outstream as *mut *mut File as *mut libc::c_char, - ::std::mem::size_of::<*mut File>() as libc::c_ulong as libc::c_int, - ); + let bind_str = String::from("bind_builtin"); + let bind_ptr = bind_str.as_ptr() as *mut c_char; + begin_unwind_frame(bind_ptr); + //这里是瞎写的!!!!! + // unwind_protect_var!(rl_outstream); + // unwind_protect_mem(rl_outstream as *mut c_char,8); + unwind_protect_mem(std::mem::transmute(rl_outstream), std::mem::size_of_val(&rl_outstream) as c_int); + rl_outstream = stdout; reset_internal_getopt(); @@ -247,17 +270,15 @@ pub extern "C" fn r_bind_builtin(mut list:*mut WordList)->i32{ } 'X' => flags |= XXFLAG!(), _ => { - if opt == -99 { - builtin_help(); - return EX_USAGE; - } + println!("111"); + // builtin_usage(); r_builtin_usage(); - - return_code = EX_USAGE; + // BIND_RETURN!(EX_USAGE!()); + return_code = EX_USAGE!(); if !saved_keymap.is_null(){ rl_set_keymap(saved_keymap); } - run_unwind_frame(bind_str.as_ptr() as *mut c_char); + run_unwind_frame(bind_ptr ); if return_code < 0 { return_code = EXECUTION_FAILURE!(); } @@ -282,7 +303,7 @@ pub extern "C" fn r_bind_builtin(mut list:*mut WordList)->i32{ if !saved_keymap.is_null(){ rl_set_keymap(saved_keymap); } - run_unwind_frame(bind_str.as_ptr() as *mut c_char ); + run_unwind_frame(bind_ptr ); if return_code < 0 { return_code = EXECUTION_FAILURE!(); } @@ -333,7 +354,7 @@ pub extern "C" fn r_bind_builtin(mut list:*mut WordList)->i32{ if !saved_keymap.is_null(){ rl_set_keymap(saved_keymap); } - run_unwind_frame(bind_str.as_ptr() as *mut c_char); + run_unwind_frame(bind_ptr ); if return_code < 0 { return_code = EXECUTION_FAILURE!(); } @@ -355,7 +376,7 @@ pub extern "C" fn r_bind_builtin(mut list:*mut WordList)->i32{ if !saved_keymap.is_null(){ rl_set_keymap(saved_keymap); } - run_unwind_frame(bind_str.as_ptr() as *mut c_char ); + run_unwind_frame(bind_ptr ); if return_code < 0 { return_code = EXECUTION_FAILURE!(); } @@ -420,7 +441,7 @@ pub extern "C" fn r_bind_builtin(mut list:*mut WordList)->i32{ if !saved_keymap.is_null(){ rl_set_keymap(saved_keymap); } - run_unwind_frame(bind_str.as_ptr() as *mut c_char); + run_unwind_frame(bind_ptr); if return_code < 0{ return_code = EXECUTION_FAILURE!(); @@ -444,7 +465,8 @@ extern "C" fn r_query_bindings(name:*mut c_char)->i32{ function = rl_named_function(name); if function.is_null(){ let c_str = CString::new("%s: unknow function name").unwrap(); - builtin_error(c_str.as_ptr(),name); + let c_ptr= c_str.as_ptr(); + builtin_error(c_ptr,name); return EXECUTION_FAILURE!(); } @@ -489,8 +511,8 @@ extern "C" fn r_unbind_command(name:*mut c_char)->i32{ function = rl_named_function(name); if function.is_null(){ let c_str = CString::new("`%s':unknown function name").unwrap(); - - builtin_error(c_str.as_ptr(),name); + let c_ptr = c_str.as_ptr(); + builtin_error(c_ptr,name); return EXECUTION_FAILURE!(); } @@ -510,8 +532,8 @@ extern "C" fn r_unbind_keyseq(seq:*mut c_char)->i32{ if rl_translate_keyseq(seq,kseq,&mut kslen) != 0{ free(kseq as *mut c_void); let c_str = CString::new("`%s': cannot unbind").unwrap(); - - builtin_error(c_str.as_ptr(), seq); + let c_ptr = c_str.as_ptr(); + builtin_error(c_ptr, seq); return EXECUTION_FAILURE!(); } //可能存在错误 @@ -545,3 +567,13 @@ extern "C" fn r_unbind_keyseq(seq:*mut c_char)->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/break_1/Cargo.toml b/bash-5.1/builtins_rust/break_1/Cargo.toml index 085d195e..9871a473 100644 --- a/bash-5.1/builtins_rust/break_1/Cargo.toml +++ b/bash-5.1/builtins_rust/break_1/Cargo.toml @@ -8,7 +8,6 @@ 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/break_1/src/lib.rs b/bash-5.1/builtins_rust/break_1/src/lib.rs index a2f0a636..c7b6760d 100644 --- a/bash-5.1/builtins_rust/break_1/src/lib.rs +++ b/bash-5.1/builtins_rust/break_1/src/lib.rs @@ -5,14 +5,38 @@ extern crate nix; use std::ffi::CString; use libc::c_long; +#[repr(C)] +pub struct WordDesc { + pub word: *mut libc::c_char, + pub flags: libc::c_int +} -use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; - +#[repr (C)] +#[derive(Copy,Clone)] +pub struct WordList { + next: *mut WordList, + word: *mut WordDesc +} // 屏蔽警告。 #[allow(non_camel_case_types)] type intmax_t = c_long; +#[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! ISHELP { ($s:expr) => { @@ -25,7 +49,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!(); } } } @@ -33,16 +57,13 @@ macro_rules! CHECK_HELPOPT { extern "C" { fn get_numeric_arg(list :*mut WordList, i: i32 , intmax :*mut intmax_t) -> i32; fn builtin_help (); - // fn get_loop_level() -> i32; - //fn set_continuing(cont : i32); - //fn set_breaking(breaking : i32); + fn get_loop_level() -> i32; + fn set_continuing(cont : i32); + fn set_breaking(breaking : i32); fn sh_erange (s:* mut libc::c_char, desc:* mut libc::c_char); //pub static fn check_loop_level () -> i64; /* Non-zero when a "break" instruction is encountered. */ pub static posixly_correct :i32; - static mut breaking : i32; - static mut continuing : i32; - static mut loop_level : i32; } #[no_mangle] @@ -52,23 +73,22 @@ pub extern "C" fn r_break_builtin(mut list :*mut WordList) -> i32 { unsafe { CHECK_HELPOPT! (list); if check_loop_level() == 0 { - return EXECUTION_SUCCESS!(); + return (EXECUTION_SUCCESS!()); } get_numeric_arg(list, 1, &mut newbreak as *mut intmax_t); if newbreak <= 0{ #[warn(temporary_cstring_as_ptr)] sh_erange ((*(*list).word).word, CString::new("loop count").unwrap().as_ptr() as * mut libc::c_char); - //set_breaking (get_loop_level()); - breaking = loop_level; - return EXECUTION_FAILURE!(); + set_breaking (get_loop_level()); + return (EXECUTION_FAILURE!()); } - if newbreak > loop_level as libc::c_long{ - newbreak = loop_level as i64; + if newbreak > get_loop_level() as libc::c_long{ + newbreak = get_loop_level() as i64; } - breaking = newbreak as i32; - // set_breaking(newbreak as i32); + + set_breaking(newbreak as i32); } return (EXECUTION_SUCCESS!()); } @@ -88,15 +108,13 @@ fn continue_builtin (list :*mut WordList) -> i32 { if newcont <= 0{ #[warn(temporary_cstring_as_ptr)] sh_erange ((*(*list).word).word, CString::new("loop count ").unwrap().as_ptr() as * mut libc::c_char); - //set_breaking(get_loop_level()); - breaking = loop_level; + set_breaking(get_loop_level()); return (EXECUTION_FAILURE!()); } - if newcont > loop_level.into(){ - newcont = loop_level as i64; + if newcont > get_loop_level().into(){ + newcont = get_loop_level() as i64; } - continuing = newcont as i32; - //set_continuing(newcont as i32); + set_continuing(newcont as i32); } return (EXECUTION_SUCCESS!()); @@ -105,11 +123,11 @@ fn continue_builtin (list :*mut WordList) -> i32 { #[no_mangle] pub extern "C" fn check_loop_level () -> i32 { unsafe { - if loop_level == 0 && posixly_correct == 0 { + if get_loop_level()== 0 && posixly_correct == 0 { println! ("only meaningful in a `for`, `while`, or until `loop` "); return 0; } - loop_level + return (get_loop_level()); } } diff --git a/bash-5.1/builtins_rust/builtin/Cargo.toml b/bash-5.1/builtins_rust/builtin/Cargo.toml index 9301c7dc..c09d9497 100644 --- a/bash-5.1/builtins_rust/builtin/Cargo.toml +++ b/bash-5.1/builtins_rust/builtin/Cargo.toml @@ -11,5 +11,4 @@ crate-type = ["staticlib","rlib"] [dependencies] libc = "0.2" rcmd = {path="../cmd"} -rcommon = {path="../common"} diff --git a/bash-5.1/builtins_rust/builtin/src/intercdep.rs b/bash-5.1/builtins_rust/builtin/src/intercdep.rs index 6882fb9e..661c3869 100644 --- a/bash-5.1/builtins_rust/builtin/src/intercdep.rs +++ b/bash-5.1/builtins_rust/builtin/src/intercdep.rs @@ -1,5 +1,24 @@ -use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; +#[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 const EX_USAGE: c_int = 258; +pub const EXECUTION_SUCCESS :c_int = 0; +pub const EXECUTION_FAILURE :c_int = 1; + extern "C" { static mut loption :*mut WordList; diff --git a/bash-5.1/builtins_rust/builtin/src/lib.rs b/bash-5.1/builtins_rust/builtin/src/lib.rs index 017a2a2e..71639d39 100644 --- a/bash-5.1/builtins_rust/builtin/src/lib.rs +++ b/bash-5.1/builtins_rust/builtin/src/lib.rs @@ -12,13 +12,13 @@ pub extern "C" fn r_builtin_builtin(mut list: *mut WordList) -> i32 { } list = loptend; if list.is_null() { - return EXECUTION_SUCCESS!(); + return EXECUTION_SUCCESS; } function = find_shell_builtin(command.as_ptr() as *mut c_char); if function.is_none() { sh_notbuiltin(command.as_ptr() as *mut c_char); - return EXECUTION_FAILURE!(); + return EXECUTION_FAILURE; } else { this_command_name = command.as_ptr() as *mut c_char; this_shell_builtin = function; diff --git a/bash-5.1/builtins_rust/caller/Cargo.toml b/bash-5.1/builtins_rust/caller/Cargo.toml index fe503dc2..ef4d55d0 100644 --- a/bash-5.1/builtins_rust/caller/Cargo.toml +++ b/bash-5.1/builtins_rust/caller/Cargo.toml @@ -8,7 +8,6 @@ edition = "2018" [dependencies] libc = "0.2" rread = {path="../read"} -rcommon = {path="../common"} [lib] crate-type = ["staticlib","lib"] diff --git a/bash-5.1/builtins_rust/caller/src/lib.rs b/bash-5.1/builtins_rust/caller/src/lib.rs index a888b538..524ba95d 100644 --- a/bash-5.1/builtins_rust/caller/src/lib.rs +++ b/bash-5.1/builtins_rust/caller/src/lib.rs @@ -1,12 +1,52 @@ extern crate libc; extern crate rread; -use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; use libc::{c_char,c_int,PT_NULL,c_long,}; use std::ffi::{CStr,CString}; use rread::{SHELL_VAR,ARRAY,intmax_t,}; +//struct +#[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; + + +//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! att_array { () => { @@ -61,13 +101,23 @@ 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!(); } }; } -type arrayind_t = intmax_t; +// #[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; @@ -147,7 +197,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 `--' */ @@ -199,7 +249,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!(); @@ -207,3 +257,43 @@ 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/Cargo.toml b/bash-5.1/builtins_rust/cd/Cargo.toml index 56ccd745..22ac0616 100644 --- a/bash-5.1/builtins_rust/cd/Cargo.toml +++ b/bash-5.1/builtins_rust/cd/Cargo.toml @@ -9,7 +9,6 @@ build = "../build.rs" [dependencies] lazy_static = "1.4.0" libc = "0.2" -rcommon = {path="../common"} nix = "0.23.0" [lib] diff --git a/bash-5.1/builtins_rust/cd/src/lib.rs b/bash-5.1/builtins_rust/cd/src/lib.rs index 1025580e..72ebf569 100644 --- a/bash-5.1/builtins_rust/cd/src/lib.rs +++ b/bash-5.1/builtins_rust/cd/src/lib.rs @@ -1,10 +1,21 @@ extern crate libc; extern crate nix; -use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; use libc::{c_char, c_long, c_void}; use std::{ffi::{CString, CStr}}; +#[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(i8)] pub enum JOB_STATE { @@ -235,6 +246,20 @@ pub struct SHELL_VAR { context:i32 /* Which context this variable belongs to. */ } +#[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! DUP_JOB { @@ -500,7 +525,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 +686,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); @@ -825,3 +850,13 @@ pub extern "C" fn r_change_to_directory (newdir:* mut c_char, nolinks:i32, xattr } } +/* +#[no_mangle] +pub extern "C" fn cmd_name() ->*const u8 { + return b"cd" as *const u8; +} +#[no_mangle] +pub extern "C" fn run(list : *mut WordList)->i32 { + return r_cd_builtin(list); +} +*/ diff --git a/bash-5.1/builtins_rust/colon/Cargo.toml b/bash-5.1/builtins_rust/colon/Cargo.toml index dea395cb..3df1f22c 100644 --- a/bash-5.1/builtins_rust/colon/Cargo.toml +++ b/bash-5.1/builtins_rust/colon/Cargo.toml @@ -11,4 +11,3 @@ name = "rcolon" [dependencies] libc = "0.2" -rcommon = {path ="../common"} diff --git a/bash-5.1/builtins_rust/colon/src/lib.rs b/bash-5.1/builtins_rust/colon/src/lib.rs index 6ead6461..faea3969 100644 --- a/bash-5.1/builtins_rust/colon/src/lib.rs +++ b/bash-5.1/builtins_rust/colon/src/lib.rs @@ -1,6 +1,5 @@ use std::{ffi::CString}; use libc::{size_t, c_int, c_uint, c_char, c_long, c_void, PT_NULL, c_ulong, strchr}; -use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; #[cfg(test)] mod tests { @@ -11,6 +10,21 @@ mod tests { } } +#[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; #[no_mangle] pub extern "C" fn r_colon_builtin(ignore:WordList)->i32 { diff --git a/bash-5.1/builtins_rust/command/Cargo.toml b/bash-5.1/builtins_rust/command/Cargo.toml index 0b76f4f5..13ff0ef0 100644 --- a/bash-5.1/builtins_rust/command/Cargo.toml +++ b/bash-5.1/builtins_rust/command/Cargo.toml @@ -7,7 +7,6 @@ edition = "2018" [dependencies] libc ="*" -rcommon = {path="../common"} [lib] crate-type = ["staticlib", "rlib"] diff --git a/bash-5.1/builtins_rust/command/src/lib.rs b/bash-5.1/builtins_rust/command/src/lib.rs index b7f593de..bf398967 100644 --- a/bash-5.1/builtins_rust/command/src/lib.rs +++ b/bash-5.1/builtins_rust/command/src/lib.rs @@ -1,6 +1,5 @@ use std::ffi::*; -use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; extern "C" { fn copy_word_list(_: *mut WordList) -> *mut WordList; fn begin_unwind_frame(_: *mut libc::c_char); @@ -59,6 +58,20 @@ pub const cm_case: command_type = 1; pub const cm_for: command_type = 0; #[derive(Copy, Clone)] #[repr(C)] +pub struct word_desc { + pub word: *mut libc::c_char, + pub flags: libc::c_int, +} +pub type WordDesc = word_desc; +#[derive(Copy, Clone)] +#[repr(C)] +pub struct word_list { + pub next: *mut word_list, + pub word: *mut WordDesc, +} +pub type WordList = word_list; +#[derive(Copy, Clone)] +#[repr(C)] pub union REDIRECTEE { pub dest: libc::c_int, pub filename: *mut WordDesc, @@ -246,6 +259,9 @@ pub const CDESC_STDPATH: i32 = 0x100; //#define CDESC_ABSPATH 0x080 /* convert to absolute path, no ./ */ //#define CDESC_STDPATH 0x100 /* command -p */ +pub const EX_USAGE :i32= 258; +pub const EXECUTION_SUCCESS:i32 = 0; +pub const EXECUTION_FAILURE:i32 = 1; pub const const_command_builtin:&CStr =unsafe{ CStr::from_bytes_with_nul_unchecked(b"command_builtin\0")};//.unwrap(); //#define COMMAND_BUILTIN_FLAGS (CMD_NO_FUNCTIONS | CMD_INHIBIT_EXPANSION | CMD_COMMAND_BUILTIN | (use_standard_path ? CMD_STDPATH : 0)) @@ -284,7 +300,7 @@ pub const CMD_STDPATH :i32 = 0x4000; pub const CMD_TRY_OPTIMIZING :i32 = 0x8000; #[no_mangle] -pub unsafe extern "C" fn r_command_builtin(mut list: *mut WordList) -> libc::c_int { +pub unsafe extern "C" fn command_builtin(mut list: *mut WordList) -> libc::c_int { let mut result: libc::c_int = 0; let mut verbose: libc::c_int = 0; let mut use_standard_path: libc::c_int = 0; @@ -324,11 +340,11 @@ pub unsafe extern "C" fn r_command_builtin(mut list: *mut WordList) -> libc::c_i list = loptend; if list.is_null() { - return EXECUTION_SUCCESS!(); + return EXECUTION_SUCCESS; } if use_standard_path != 0 && restricted != 0 { sh_restricted(b"-p\0" as *const u8 as *const libc::c_char as *mut libc::c_char); - return EXECUTION_FAILURE!(); + return EXECUTION_FAILURE; } if verbose != 0 { let mut found: libc::c_int = 0; @@ -342,7 +358,7 @@ pub unsafe extern "C" fn r_command_builtin(mut list: *mut WordList) -> libc::c_i any_found += found; list = (*list).next; } - return if any_found != 0 { EXECUTION_SUCCESS!() } else { EXECUTION_FAILURE!() }; + return if any_found != 0 { EXECUTION_SUCCESS } else { EXECUTION_FAILURE }; } begin_unwind_frame( const_command_builtin.as_ptr() as *mut libc::c_char diff --git a/bash-5.1/builtins_rust/common/src/lib.rs b/bash-5.1/builtins_rust/common/src/lib.rs index fa1361f8..5a159f93 100644 --- a/bash-5.1/builtins_rust/common/src/lib.rs +++ b/bash-5.1/builtins_rust/common/src/lib.rs @@ -7,10 +7,8 @@ use std::mem::size_of; use std::ptr::read_volatile; use nix::errno::errno; -include!(concat!("lib_readline_keymaps.rs")); - -include!(concat!("command.rs")); +pub static EXECUTION_SUCCESS:i32 = 0; //struct #[repr(C)] #[derive(Copy, Clone)] @@ -602,7 +600,25 @@ 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; -include!("./shell.rs"); +/////// +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 */ //extern C extern "C"{ @@ -1753,3 +1769,20 @@ 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/complete/Cargo.toml b/bash-5.1/builtins_rust/complete/Cargo.toml index 58cadc61..e18cffe9 100644 --- a/bash-5.1/builtins_rust/complete/Cargo.toml +++ b/bash-5.1/builtins_rust/complete/Cargo.toml @@ -10,7 +10,6 @@ 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/complete/src/lib.rs b/bash-5.1/builtins_rust/complete/src/lib.rs index 9dd58df4..acfe6f50 100644 --- a/bash-5.1/builtins_rust/complete/src/lib.rs +++ b/bash-5.1/builtins_rust/complete/src/lib.rs @@ -3,8 +3,19 @@ extern crate nix; use libc::{c_char, c_int, c_ulong, c_void}; use std::{ffi::CString}; -use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; +#[repr(C)] +pub struct WordDesc { + pub word: *mut c_char, + pub flags: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, @@ -308,6 +319,22 @@ pub struct STRINGLIST { list_len:c_int, } +#[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! CA_ALIAS { @@ -698,7 +725,7 @@ pub extern "C" fn r_build_actions (list : *mut WordList, flagp:* mut _optflags, } else { sh_invalidopt (CString::new("-r").unwrap().as_ptr() as * mut c_char); builtin_usage (); - return EX_USAGE; + return EX_USAGE!(); } } 'p'=>{ @@ -707,7 +734,7 @@ pub extern "C" fn r_build_actions (list : *mut WordList, flagp:* mut _optflags, } else { sh_invalidopt (CString::new("-p").unwrap().as_ptr() as * mut c_char); builtin_usage (); - return EX_USAGE; + return EX_USAGE!(); } } 'a'=>{ @@ -750,7 +777,7 @@ pub extern "C" fn r_build_actions (list : *mut WordList, flagp:* mut _optflags, ind = r_find_compopt (list_optarg); if ind < 0 { sh_invalidoptname (list_optarg); - return EX_USAGE; + return EX_USAGE!(); } let compopts:CompoptArray=CompoptArray::new(); copts |= compopts.compoptArr[ind as usize].optflag; @@ -759,7 +786,7 @@ pub extern "C" fn r_build_actions (list : *mut WordList, flagp:* mut _optflags, ind = r_find_compact (list_optarg); if ind < 0 { builtin_error (CString::new("%s: invalid action name").unwrap().as_ptr(), list_optarg); - return EX_USAGE; + return EX_USAGE!(); } let compacts:CompactsArray=CompactsArray::new(); acts |= compacts.compactsArr[ind as usize].actflag; @@ -773,7 +800,7 @@ pub extern "C" fn r_build_actions (list : *mut WordList, flagp:* mut _optflags, } else { sh_invalidopt (CString::new("-D").unwrap().as_ptr() as * mut c_char); builtin_usage (); - return EX_USAGE; + return EX_USAGE!(); } } 'E'=>{ @@ -782,7 +809,7 @@ pub extern "C" fn r_build_actions (list : *mut WordList, flagp:* mut _optflags, } else { sh_invalidopt (CString::new("-E").unwrap().as_ptr() as * mut c_char); builtin_usage (); - return EX_USAGE; + return EX_USAGE!(); } } 'I'=>{ @@ -791,7 +818,7 @@ pub extern "C" fn r_build_actions (list : *mut WordList, flagp:* mut _optflags, } else { sh_invalidopt (CString::new("-I").unwrap().as_ptr() as * mut c_char); builtin_usage (); - return EX_USAGE; + return EX_USAGE!(); } } 'F'=>{ @@ -800,7 +827,7 @@ pub extern "C" fn r_build_actions (list : *mut WordList, flagp:* mut _optflags, w.flags = 0; if check_identifier (&mut w, posixly_correct) == 0 || libc::strpbrk (Farg, shell_break_chars()) != std::ptr::null_mut() { sh_invalidid (Farg); - return EX_USAGE; + return EX_USAGE!(); } } 'G'=>{ @@ -823,7 +850,7 @@ pub extern "C" fn r_build_actions (list : *mut WordList, flagp:* mut _optflags, } _=>{ builtin_usage (); - return EX_USAGE; + return EX_USAGE!(); } } opt=internal_getopt(list, CString::new("abcdefgjko:prsuvA:G:W:P:S:X:F:C:DEI").unwrap().as_ptr() as * mut c_char); @@ -877,7 +904,7 @@ pub extern "C" fn r_complete_builtin (listt: *mut WordList)->i32 /* Build the actions from the arguments. Also sets the [A-Z]arg variables as a side effect if they are supplied as options. */ rval = r_build_actions (list, &mut oflags, &mut acts, &mut copts); - if rval == EX_USAGE { + if rval == EX_USAGE!() { return rval; } @@ -923,7 +950,7 @@ pub extern "C" fn r_complete_builtin (listt: *mut WordList)->i32 if wl == std::ptr::null_mut() && list == std::ptr::null_mut() && opt_given !=0 { builtin_usage (); - return EX_USAGE; + return EX_USAGE!(); } /* If we get here, we need to build a compspec and add it for each @@ -1170,7 +1197,7 @@ pub extern "C" fn r_compgen_builtin (listt:* mut WordList)->i32 /* Build the actions from the arguments. Also sets the [A-Z]arg variables as a side effect if they are supplied as options. */ rval = r_build_actions (list, std::ptr::null_mut(), &mut acts, &mut copts); - if rval == EX_USAGE { + if rval == EX_USAGE!() { return rval; } @@ -1287,7 +1314,7 @@ pub extern "C" fn r_compopt_builtin (listt:* mut WordList)->i32 oind = r_find_compopt (list_optarg); if oind < 0 { sh_invalidoptname (list_optarg); - return EX_USAGE; + return EX_USAGE!(); } let compopts:CompoptArray=CompoptArray::new(); *opts |= compopts.compoptArr[oind as usize].optflag as i32; @@ -1303,7 +1330,7 @@ pub extern "C" fn r_compopt_builtin (listt:* mut WordList)->i32 } _=>{ builtin_usage (); - return EX_USAGE; + return EX_USAGE!(); } } opt = internal_getopt (list, CString::new("+o:DEI").unwrap().as_ptr() as * mut c_char); @@ -1376,3 +1403,12 @@ pub extern "C" fn r_compopt_builtin (listt:* mut WordList)->i32 } } +#[no_mangle] +pub extern "C" fn cmd_name() ->*const u8 { + return b"complete" as *const u8; +} + +#[no_mangle] +pub extern "C" fn run(list : *mut WordList)->i32 { + return r_complete_builtin(list); +} \ No newline at end of file diff --git a/bash-5.1/builtins_rust/declare/src/lib.rs b/bash-5.1/builtins_rust/declare/src/lib.rs index eede3a82..b44473ab 100644 --- a/bash-5.1/builtins_rust/declare/src/lib.rs +++ b/bash-5.1/builtins_rust/declare/src/lib.rs @@ -3,8 +3,20 @@ 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}; +// use rcommon::{r_sh_notfound,r_sh_invalidopt,r_sh_invalidid,r_sh_readonly,r_sh_chkwrite,}; +#[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, @@ -231,6 +243,15 @@ pub struct VAR_CONTEXT { table:* mut HASH_TABLE /* variables at this scope */ } +#[macro_export] +macro_rules! EXECUTION_FAILURE { + () => {1} +} + +#[macro_export] +macro_rules! EX_USAGE { + () => {258} +} #[macro_export] macro_rules! ARGS_SETBLTIN { @@ -337,6 +358,12 @@ macro_rules! MKLOC_INHERIT { } } +#[macro_export] +macro_rules! EXECUTION_SUCCESS { + () => { + 0 + } +} #[macro_export] macro_rules! ASS_APPEND { @@ -531,7 +558,7 @@ pub extern "C" fn r_local_builtin (list:* mut WordList)->i32 /* Catch a straight `local --help' before checking function context */ if list !=std::ptr::null_mut() && (*list).word != std::ptr::null_mut() && STREQ ((*(*list).word).word, CString::new("--help").unwrap().as_ptr()) { builtin_help (); - return EX_USAGE; + return EX_USAGE!(); } if variable_context !=0 { @@ -717,7 +744,7 @@ pub extern "C" fn r_declare_internal (list:* mut WordList, local_var:i32)->i32 } 'I'=>{ inherit_flag = MKLOC_INHERIT!();} _=>{ builtin_usage (); - return EX_USAGE; + return EX_USAGE!(); } } internal_getopt (list, DECLARE_OPTS() as * mut c_char); @@ -1515,3 +1542,12 @@ pub extern "C" fn r_declare_internal (list:* mut WordList, local_var:i32)->i32 } } +#[no_mangle] +pub extern "C" fn cmd_name() ->*const u8 { + return b"declare" as *const u8; +} + +#[no_mangle] +pub extern "C" fn run(list : *mut WordList)->i32 { + return r_declare_builtin(list); +} diff --git a/bash-5.1/builtins_rust/echo/Cargo.toml b/bash-5.1/builtins_rust/echo/Cargo.toml index 31720c0d..65f3bf2f 100644 --- a/bash-5.1/builtins_rust/echo/Cargo.toml +++ b/bash-5.1/builtins_rust/echo/Cargo.toml @@ -7,7 +7,6 @@ edition = "2018" [dependencies] libc = "0.2" -rcommon = {path="../common"} [lib] crate-type = ["staticlib","rlib"] diff --git a/bash-5.1/builtins_rust/echo/src/lib.rs b/bash-5.1/builtins_rust/echo/src/lib.rs index 79a74fe9..5e69facd 100644 --- a/bash-5.1/builtins_rust/echo/src/lib.rs +++ b/bash-5.1/builtins_rust/echo/src/lib.rs @@ -2,11 +2,23 @@ extern crate libc; use libc::{c_char,c_int, strchr, putchar,clearerr,free,FILE, fprintf, c_void}; use std::ffi::{CString,CStr,}; -use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; // use std::io::{stdout, Write}; use std::ptr::read_volatile; //结构体 +#[repr (C)] +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, +} + //枚举 @@ -31,6 +43,11 @@ macro_rules! QUIT { }; } +#[macro_export] +macro_rules! EXECUTION_SUCCESS { + () => {0} +} + unsafe fn STRLEN (s:*const c_char) -> i32{ diff --git a/bash-5.1/builtins_rust/enable/Cargo.toml b/bash-5.1/builtins_rust/enable/Cargo.toml index 99eea4b4..1c2bc170 100644 --- a/bash-5.1/builtins_rust/enable/Cargo.toml +++ b/bash-5.1/builtins_rust/enable/Cargo.toml @@ -12,4 +12,3 @@ crate-type = ["staticlib","rlib"] libc = "0.2" libloading = "0.7" rcmd = {path="../cmd"} -rcommon = {path="../common"} diff --git a/bash-5.1/builtins_rust/enable/src/lib.rs b/bash-5.1/builtins_rust/enable/src/lib.rs index 1edb8123..2df3366e 100644 --- a/bash-5.1/builtins_rust/enable/src/lib.rs +++ b/bash-5.1/builtins_rust/enable/src/lib.rs @@ -4,7 +4,6 @@ use libc::c_char; use std::path::Path; use libloading::Library; use rcmd::*; -use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; /* #define ENABLED 1 #define DISABLED 2 @@ -28,6 +27,8 @@ pub const NFLAG: i32 = 0x08; pub const PFLAG: i32 = 0x10; pub const SFLAG: i32 = 0x20; +pub const EX_USAGE: i32 = 258; +pub const EXECUTION_FAILURE:i32 = 1; // Flags describing various things about a builtin. //#define BUILTIN_ENABLED 0x01 /* This builtin is enabled. */ @@ -49,6 +50,12 @@ pub const POSIX_BUILTIN :i32 = 0x20; pub const LOCALVAR_BUILTIN :i32 = 0x40; pub const REQUIRES_BUILTIN :i32 = 0x80; +/* +#define EXECUTION_FAILURE 1 +#define EXECUTION_SUCCESS 0 + */ +//pub const EXECUTION_FAILURE :i32 = 1; +pub const EXECUTION_SUCCESS :i32 = 0; /* The MODE argument to `dlopen' contains one of the following: */ // #define RTLD_LAZY 0x00001 /* Lazy function call binding. */ @@ -121,6 +128,20 @@ extern "C" { fn dlerror() -> *mut libc::c_char; } pub type size_t = libc::c_ulong; +#[derive(Copy, Clone)] +#[repr(C)] +pub struct word_desc { + pub word: *mut libc::c_char, + pub flags: libc::c_int, +} +pub type WordDesc = word_desc; +#[derive(Copy, Clone)] +#[repr(C)] +pub struct word_list { + pub next: *mut word_list, + pub word: *mut WordDesc, +} +pub type WordList = word_list; pub type sh_load_func_t = unsafe extern "C" fn(*mut libc::c_char) -> libc::c_int; pub type sh_unload_func_t = unsafe extern "C" fn(*mut libc::c_char) -> (); pub type sh_builtin_func_t = unsafe extern "C" fn(*mut WordList) -> libc::c_int; @@ -201,7 +222,7 @@ pub unsafe extern "C" fn r_enable_builtin(mut list: *mut WordList) -> i32 { // 如果是严格模式,就直接返回EXECUTION_FAILURE,命令结束。 if restricted != 0 && flags & (FFLAG|DFLAG) != 0 { sh_restricted (0 as *mut c_char); - return EXECUTION_FAILURE!(); + return EXECUTION_FAILURE; } // 配置filter,通过flag和PFLAG,后者表示打印, @@ -249,8 +270,8 @@ pub unsafe extern "C" fn r_enable_builtin(mut list: *mut WordList) -> i32 { // 否则判断-D,-D含义是删除以 -f 选项加载的内建 while !list.is_null() { opt = dyn_unload_builtin((*(*list).word).word); - if opt == EXECUTION_FAILURE!() { - result = EXECUTION_FAILURE!(); + if opt == EXECUTION_FAILURE { + result = EXECUTION_FAILURE; } list = (*list).next; } @@ -260,9 +281,9 @@ pub unsafe extern "C" fn r_enable_builtin(mut list: *mut WordList) -> i32 { // 不带-N -F -D,且选项不为空的其他 while !list.is_null() { opt = enable_shell_command((*(*list).word).word, flags & NFLAG); - if opt == EXECUTION_FAILURE!() { + if opt == EXECUTION_FAILURE { sh_notbuiltin((*(*list).word).word); - result = EXECUTION_FAILURE!(); + result = EXECUTION_FAILURE; } list = (*list).next; } @@ -308,7 +329,7 @@ unsafe extern "C" fn enable_shell_command( let mut b: *mut builtin = 0 as *mut builtin; b = builtin_address_internal(name, 1); if b.is_null() { - return EXECUTION_FAILURE!(); + return EXECUTION_FAILURE; } if disable_p != 0 { (*b).flags &= !(BUILTIN_ENABLED); @@ -319,7 +340,7 @@ unsafe extern "C" fn enable_shell_command( } } else if restricted != 0 && (*b).flags & BUILTIN_ENABLED == 0 { sh_restricted(0 as *mut libc::c_void as *mut libc::c_char); - return EXECUTION_FAILURE!(); + return EXECUTION_FAILURE; } else { (*b).flags |= BUILTIN_ENABLED; if !set_cmd_enable(CStr::from_ptr(name).to_string_lossy().into_owned(), true) { @@ -329,7 +350,7 @@ unsafe extern "C" fn enable_shell_command( } set_itemlist_dirty(&mut it_enabled); set_itemlist_dirty(&mut it_disabled); - return EXECUTION_SUCCESS!(); + return EXECUTION_SUCCESS; } unsafe extern "C" fn dyn_load_builtin( mut list: *mut WordList, diff --git a/bash-5.1/builtins_rust/eval/src/lib.rs b/bash-5.1/builtins_rust/eval/src/lib.rs index 33fcbb94..4d6609ef 100644 --- a/bash-5.1/builtins_rust/eval/src/lib.rs +++ b/bash-5.1/builtins_rust/eval/src/lib.rs @@ -3,14 +3,41 @@ extern crate rcommon; use libc::{c_char,c_int}; use std::ffi::{CString}; -use rcommon::{r_no_options}; -use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; +use rcommon::{r_no_options,WordList}; + +// #[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; + +#[macro_export] +macro_rules! EX_USAGE { + () => {258} +} #[macro_export] macro_rules! SEVAL_NOHIST { () => {0x004} } +#[macro_export] +macro_rules! EXECUTION_SUCCESS { + () => { + 0 + } +} + extern "C" { static loptend:*mut WordList; @@ -26,7 +53,7 @@ pub extern "C" fn r_eval_builtin(mut list:*mut WordList)->i32{ unsafe{ if r_no_options(list) != 0{ - return EX_USAGE; + return EX_USAGE!(); } list = loptend; diff --git a/bash-5.1/builtins_rust/exec/src/lib.rs b/bash-5.1/builtins_rust/exec/src/lib.rs index ca8399db..69139709 100644 --- a/bash-5.1/builtins_rust/exec/src/lib.rs +++ b/bash-5.1/builtins_rust/exec/src/lib.rs @@ -4,11 +4,10 @@ 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{ @@ -22,8 +21,8 @@ pub struct WordList{ pub next:*mut WordList, pub word:*mut WordDesc, } -*/ -#[repr(C)] + +#[repr (C)] struct redirect{ next:*mut redirect, redirector:REDIRECTEE, @@ -41,27 +40,69 @@ union REDIRECTEE { filename:*mut WordDesc, } -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; +#[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 } +} + #[macro_export] macro_rules! savestring { @@ -79,6 +120,7 @@ macro_rules! FREE { } } + extern "C" { // static errno:i32; static mut exec_argv0:*mut c_char; @@ -93,12 +135,12 @@ extern "C" { static job_control:i32; static interactive:i32; static default_buffered_input:i32; - static no_exit_on_failed_exec:i32; - fn builtin_help(); + // static no_exit_on_failed_exec:i32; + 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; @@ -127,6 +169,10 @@ 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] @@ -159,15 +205,16 @@ pub extern "C" fn r_exec_builtin(mut list:*mut WordList)->i32{ println!("r_exec_builtin"); - unsafe{ - exec_argv0 = std::ptr::null_mut() as *mut c_char; + loop{ + + exec_argv0 = std::ptr::null_mut() as *mut c_char; - reset_internal_getopt(); + reset_internal_getopt(); - loop{ let c_str = CString::new("cla:").unwrap(); - opt = internal_getopt(list,c_str.as_ptr() as *mut c_char); + let c_ptr = c_str.as_ptr() as *mut c_char; + opt = internal_getopt(list,c_ptr); while opt != -1{ let optu8 = opt as u8; let opt_char = char::from(optu8); @@ -176,16 +223,12 @@ pub extern "C" fn r_exec_builtin(mut list:*mut WordList)->i32{ 'l' => login = 1, 'a' => argv0 = list_optarg, _ => { - if opt == -99 { - builtin_help(); - return EX_USAGE; - } - r_builtin_usage(); - return EX_USAGE; + builtin_usage(); + return EX_USAGE!(); } } - opt = internal_getopt(list,c_str.as_ptr() as *mut c_char); + opt = internal_getopt(list,c_ptr); } list = loptend; @@ -221,11 +264,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; @@ -304,7 +347,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; } @@ -312,7 +355,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); @@ -350,3 +393,20 @@ 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/Cargo.toml b/bash-5.1/builtins_rust/exit/Cargo.toml index 466a0f78..9971aed9 100644 --- a/bash-5.1/builtins_rust/exit/Cargo.toml +++ b/bash-5.1/builtins_rust/exit/Cargo.toml @@ -10,7 +10,6 @@ edition = "2018" libc = "0.2" nix = "0.23.0" rjobs = {path="../jobs"} -rcommon = {path="../common"} [lib] diff --git a/bash-5.1/builtins_rust/exit/src/lib.rs b/bash-5.1/builtins_rust/exit/src/lib.rs index 0f74aab5..ac3f9255 100644 --- a/bash-5.1/builtins_rust/exit/src/lib.rs +++ b/bash-5.1/builtins_rust/exit/src/lib.rs @@ -5,9 +5,7 @@ extern crate nix; use libc::c_char; use std::ffi::CString; -use rjobs::{PROCESS, COMMAND, r_jobs_builtin, JLIST_STANDARD }; - -use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; +use rjobs::{PROCESS, COMMAND, r_jobs_builtin, JLIST_STANDARD, WordList}; #[repr(C)] pub struct JOB { @@ -59,6 +57,20 @@ 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 { @@ -140,7 +152,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 { @@ -169,7 +181,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{ @@ -257,3 +269,15 @@ 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 70adc426..499532ab 100644 --- a/bash-5.1/builtins_rust/fc/Cargo.toml +++ b/bash-5.1/builtins_rust/fc/Cargo.toml @@ -9,7 +9,6 @@ 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 5d0eb3fc..9d97c2e0 100644 --- a/bash-5.1/builtins_rust/fc/src/lib.rs +++ b/bash-5.1/builtins_rust/fc/src/lib.rs @@ -3,7 +3,19 @@ extern crate nix; use libc::{c_char, c_long, c_void}; use std::{ffi::CString, i32, io::{Write, stdout}, ops::Add, string, u32}; -use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE,r_builtin_usage}; + +#[repr(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(i8)] pub enum JOB_STATE { @@ -249,6 +261,20 @@ 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 { @@ -387,7 +413,6 @@ 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] @@ -523,12 +548,8 @@ pub extern "C" fn r_fc_builtin (list:* mut WordList)->i32 's'=>{execute = 1;} 'e'=>{ename = list_optarg;} _=>{ - if opt == -99 { - builtin_help(); - return EX_USAGE; - } - r_builtin_usage(); - return EX_USAGE; + 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 f054950a..26f78f39 100644 --- a/bash-5.1/builtins_rust/fg_bg/Cargo.toml +++ b/bash-5.1/builtins_rust/fg_bg/Cargo.toml @@ -9,7 +9,6 @@ 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 b86636cc..7780ea47 100644 --- a/bash-5.1/builtins_rust/fg_bg/src/lib.rs +++ b/bash-5.1/builtins_rust/fg_bg/src/lib.rs @@ -3,7 +3,19 @@ extern crate nix; use libc::{c_char, c_long}; use std::{ffi::CString, ops::Add}; -use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE,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(i8)] pub enum JOB_STATE { @@ -41,16 +53,21 @@ 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:libc::c_int, /* Descriptor or varname to be redirected. */ + redirector:REDIRECTEE, /* 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:libc::c_int, /* File descriptor or filename */ + redirectee:REDIRECTEE, /* 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 { @@ -314,7 +345,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!(); } } } @@ -348,7 +379,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 @@ -388,7 +419,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 07044a6d..ca691a19 100644 --- a/bash-5.1/builtins_rust/getopts/Cargo.toml +++ b/bash-5.1/builtins_rust/getopts/Cargo.toml @@ -9,7 +9,6 @@ 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 1ecc59b3..a133a5d7 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,6 +212,20 @@ 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 { @@ -253,6 +267,12 @@ macro_rules! G_ARG_MISSING { } } +#[macro_export] +macro_rules! GETOPT_HELP { + () => { + -99 + } +} extern "C" { fn unbind_variable_noref (name: * const c_char)->i32; @@ -352,7 +372,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". */ @@ -504,7 +524,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(); @@ -516,7 +536,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 698a3f95..142514f4 100644 --- a/bash-5.1/builtins_rust/hash/src/lib.rs +++ b/bash-5.1/builtins_rust/hash/src/lib.rs @@ -7,10 +7,9 @@ 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, @@ -23,7 +22,7 @@ pub struct WordList{ pub next:*mut WordList, pub word:*mut WordDesc, } -*/ + type PTR_T=c_void; #[repr (C)] pub struct bucket_contents{ @@ -51,7 +50,21 @@ 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 { @@ -127,7 +140,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); @@ -143,7 +156,6 @@ 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 @@ -188,15 +200,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!(); } } + opt = internal_getopt(list,opts.as_ptr() as *mut c_char); } @@ -209,13 +218,12 @@ pub extern "C" fn r_hash_builtin(mut list:*mut WordList)->i32{ if delete != 0{ temp = CString::new("-d").unwrap(); temp_ptr = temp.as_ptr() as *mut c_char; - sh_needarg(temp_ptr); } else{ temp = CString::new("-t").unwrap(); temp_ptr = temp.as_ptr() as *mut c_char; - sh_needarg(temp_ptr); } + sh_needarg(temp_ptr); return EXECUTION_FAILURE!(); } diff --git a/bash-5.1/builtins_rust/help/Cargo.toml b/bash-5.1/builtins_rust/help/Cargo.toml index 993d13e0..50f09b29 100644 --- a/bash-5.1/builtins_rust/help/Cargo.toml +++ b/bash-5.1/builtins_rust/help/Cargo.toml @@ -8,7 +8,6 @@ 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 16bedff4..b215aacb 100644 --- a/bash-5.1/builtins_rust/help/src/lib.rs +++ b/bash-5.1/builtins_rust/help/src/lib.rs @@ -3,13 +3,25 @@ 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 { @@ -27,6 +39,17 @@ 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} @@ -95,7 +118,6 @@ extern "C"{ static mut static_shell_builtin : [builtin ; 100]; static shell_builtins:*mut builtin; static mut current_builtin :*mut builtin; - fn builtin_help(); } #[no_mangle] @@ -130,12 +152,8 @@ pub extern "C" fn r_help_builtin(mut list:*mut WordList)->i32 { 's'=> {sflag = 1; break;} _=>{ unsafe { - if i == -99 { - builtin_help(); - return EX_USAGE; - } - builtin_usage (); - return EX_USAGE; + builtin_usage (); + return EX_USAGE!(); } } } diff --git a/bash-5.1/builtins_rust/history/src/intercdep.rs b/bash-5.1/builtins_rust/history/src/intercdep.rs index a1c6d3d1..5ea73daf 100644 --- a/bash-5.1/builtins_rust/history/src/intercdep.rs +++ b/bash-5.1/builtins_rust/history/src/intercdep.rs @@ -31,7 +31,6 @@ pub struct _hist_entry { pub type HIST_ENTRY = _hist_entry; extern "C" { - pub fn printf(_: *const libc::c_char, _: ...) -> libc::c_int; pub fn reset_internal_getopt(); pub fn internal_getopt(list: *mut WordList, opts: *mut c_char) -> c_int; pub fn builtin_usage(); @@ -68,7 +67,7 @@ extern "C" { pub fn sh_erange(s: *mut c_char, desc: *mut c_char); pub fn sh_restricted(s: *mut c_char) -> c_void; - fn builtin_help(); + pub static mut list_optarg : *mut libc::c_char; pub static mut loptend : *mut WordList; diff --git a/bash-5.1/builtins_rust/history/src/lib.rs b/bash-5.1/builtins_rust/history/src/lib.rs index 37caa346..077183ef 100644 --- a/bash-5.1/builtins_rust/history/src/lib.rs +++ b/bash-5.1/builtins_rust/history/src/lib.rs @@ -47,10 +47,6 @@ unsafe { } 'p' => flags |= PFLAG, _ => { - if opt == -99 { - builtin_help(); - return EX_USAGE; - } r_builtin_usage (); return EX_USAGE; } @@ -59,6 +55,9 @@ 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 { let c_err = CString::new("cannot use more than one of -anrw").unwrap(); @@ -83,16 +82,14 @@ unsafe { return expand_and_print_history(list); } return r_sh_chkwrite(EXECUTION_SUCCESS); - } else if (flags & DFLAG) != 0 { - let c_tmp = if *delete_arg == b'-' as c_char {delete_arg.offset(1 as isize ) as *mut c_char} else {delete_arg}; - range = libc::strchr(c_tmp, b'-' as c_int); - - printf(b"AAAAAAArange=%u, c_tmp=%s\n" as *const u8 as *const i8, range, c_tmp); - if !range.is_null() { - printf(b"AAAAAAArange=%s\n" as *const u8 as *const i8, range); - - let mut delete_start: c_long = 0; - let mut delete_end: c_long = 0; + } 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() { + let mut delete_start: c_long = 0; + let mut delete_end: c_long = 0; *range = b'\0' as c_char; range = (range as usize + 1) as *mut c_char; @@ -133,11 +130,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, &mut delete_offset) == 0 { + } 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); return EXECUTION_FAILURE; } @@ -145,24 +140,24 @@ 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(delete_arg, "history position\0".as_ptr() as *mut c_char); + r_sh_erange(range, "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(delete_arg, "history position\0".as_ptr() as *mut c_char); + r_sh_erange(range, "history position\0".as_ptr() as *mut c_char); return EXECUTION_FAILURE; } else { opt = delete_offset as c_int; } + result = bash_delete_histent(opt - history_base); if where_history() > history_length { history_set_pos(history_length); } return if result != 0 {EXECUTION_FAILURE} else {EXECUTION_SUCCESS}; - } -} else if (flags & (AFLAG | RFLAG | NFLAG | WFLAG | CFLAG)) == 0 { + } else if (flags & (AFLAG | RFLAG | NFLAG | WFLAG | CFLAG)) == 0 { result = display_history(list); return r_sh_chkwrite(result); } @@ -174,6 +169,7 @@ unsafe { r_sh_restricted(filename); return EXECUTION_FAILURE; } + if (flags & AFLAG) != 0 { result = maybe_append_history(filename); } else if (flags & WFLAG) != 0 { @@ -251,45 +247,37 @@ unsafe fn display_history(list: *mut WordList) -> c_int } else { limit = -1; } + let hlist = history_list(); if !hlist.is_null() { let mut i: c_long = 0; - while !(*hlist.offset(i as isize)).is_null() { + while !((hlist as usize + (i * 8) as usize) as *mut HIST_ENTRY).is_null() { i += 1; } i = if 0 <= limit && limit < i {i - limit} else {0}; - histtimefmt = get_string_value(b"HISTTIMEFORMAT\0" as *const u8 as *const c_char); + histtimefmt = get_string_value("HISTTIMEFORMAT\0".as_ptr() as *const c_char); - while !(*hlist.offset(i as isize)).is_null() { - if terminating_signal != 0 { - termsig_handler(terminating_signal); - } - if interrupt_state != 0 { - throw_to_top_level(); - } - timestr = if !histtimefmt.is_null() && *histtimefmt as libc::c_int != 0 { - histtime(*hlist.offset(i as isize), histtimefmt) + while !((hlist as usize + (i * 8) as usize) as *mut HIST_ENTRY).is_null(){ + quit(); + + if !histtimefmt.is_null() && *histtimefmt != 0 { + timestr = histtime((hlist as usize + (i * 8) as usize) as *mut HIST_ENTRY, histtimefmt); } else { - 0 as *mut libc::c_void as *mut libc::c_char - }; - printf( - b"%5d%c %s%s\n\0" as *const u8 as *const libc::c_char, - i + history_base as c_long, - if !((**hlist.offset(i as isize)).data).is_null() { - '*' as i32 - } else { - ' ' as i32 - }, - if !timestr.is_null() && *timestr as libc::c_int != 0 { - timestr - } else { - b"\0" as *const u8 as *const libc::c_char - }, - (**hlist.offset(i as isize)).line, - ); + timestr = PT_NULL as *mut c_char; + } + + let data = (*((hlist as usize + (i * 8) as usize) as *mut HIST_ENTRY)).data; + let line = (*((hlist as usize + (i * 8) as usize) as *mut HIST_ENTRY)).line; + let s_timestr = CString::from_raw(timestr).into_string().unwrap(); + println!("{:>5}{} {}{}", + i as c_int + history_base, + if !data.is_null() {"*"} else {" "}, + if !timestr.is_null() && *timestr != 0 {s_timestr} else {"".to_owned()}, + CString::from_raw(line).into_string().unwrap() + ); i += 1; } } @@ -298,12 +286,10 @@ unsafe fn display_history(list: *mut WordList) -> c_int } fn push_history(list: *mut WordList) { - println!("push_history!!!!"); unsafe { if remember_on_history != 0 && hist_last_line_pushed == 0 && (hist_last_line_added != 0 || (current_command_line_count > 0 && current_command_first_line_saved != 0 && command_oriented_history != 0)) && bash_delete_last_history() == 0 { - return; } diff --git a/bash-5.1/builtins_rust/jobs/src/lib.rs b/bash-5.1/builtins_rust/jobs/src/lib.rs index 8f70ce26..ce7f97b6 100644 --- a/bash-5.1/builtins_rust/jobs/src/lib.rs +++ b/bash-5.1/builtins_rust/jobs/src/lib.rs @@ -601,3 +601,10 @@ pub extern "C" fn r_disown_builtin (list:* mut WordList)->libc::c_int { pub extern "C" fn cmd_name() ->*const u8 { return b"jobs" as *const u8; } +/* +#[no_mangle] + +pub extern "C" fn run(list : *mut WordList)->i32 { + return r_jobs_builtin(list); +} +*/ diff --git a/bash-5.1/builtins_rust/mapfile/Cargo.toml b/bash-5.1/builtins_rust/mapfile/Cargo.toml index 5d8d8f77..6ee88c63 100644 --- a/bash-5.1/builtins_rust/mapfile/Cargo.toml +++ b/bash-5.1/builtins_rust/mapfile/Cargo.toml @@ -3,7 +3,6 @@ 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 8450248d..d1d39561 100644 --- a/bash-5.1/builtins_rust/mapfile/src/intercdep.rs +++ b/bash-5.1/builtins_rust/mapfile/src/intercdep.rs @@ -1,4 +1,19 @@ -use rcommon::{WordList}; + +#[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 arrayind_t = c_long; @@ -15,7 +30,6 @@ 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 4fa35f5b..512d37f3 100644 --- a/bash-5.1/builtins_rust/mapfile/src/lib.rs +++ b/bash-5.1/builtins_rust/mapfile/src/lib.rs @@ -153,9 +153,6 @@ 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 db66567d..5fd15940 100644 --- a/bash-5.1/builtins_rust/printf/src/intercdep.rs +++ b/bash-5.1/builtins_rust/printf/src/intercdep.rs @@ -1,4 +1,20 @@ -use rcommon::{WordList}; +#[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; pub type __intmax_t = c_long; pub type intmax_t = __intmax_t; @@ -73,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; @@ -98,5 +114,4 @@ 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 1a29c02d..ce4971ff 100644 --- a/bash-5.1/builtins_rust/printf/src/lib.rs +++ b/bash-5.1/builtins_rust/printf/src/lib.rs @@ -1,6 +1,5 @@ -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}; @@ -52,13 +51,6 @@ 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"); @@ -176,35 +168,27 @@ 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; } @@ -212,8 +196,6 @@ 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; } @@ -444,8 +426,6 @@ 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 { @@ -527,8 +507,7 @@ unsafe { let xp: *mut c_char; let p = getstr(); if !p.is_null() && *p == 0 { - xp = savestring(b"''\0".as_ptr() as *mut c_char); - //xp = savestring(b"''\0".as_ptr() as *const 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 { @@ -1235,4 +1214,3 @@ 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 51804e82..cfd6cdc0 100644 --- a/bash-5.1/builtins_rust/pushd/Cargo.toml +++ b/bash-5.1/builtins_rust/pushd/Cargo.toml @@ -9,7 +9,6 @@ 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 08d04fa5..8a823e25 100644 --- a/bash-5.1/builtins_rust/pushd/src/lib.rs +++ b/bash-5.1/builtins_rust/pushd/src/lib.rs @@ -4,7 +4,18 @@ 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, @@ -23,23 +34,22 @@ 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:libc::c_int, + redirector:REDIRECTEE, rflags:libc::c_int, flags:libc::c_int, instruction:r_instruction, - redirectee:libc::c_int, + redirectee:REDIRECTEE, here_doc_eof:*mut c_char } @@ -191,6 +201,22 @@ 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 { @@ -282,7 +308,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) { @@ -330,7 +356,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 { @@ -345,7 +371,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; } @@ -464,7 +490,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(); @@ -484,17 +510,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; } @@ -577,7 +603,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() { @@ -598,7 +624,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 { @@ -611,7 +637,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 } @@ -935,3 +961,12 @@ pub extern "C" fn r_get_directory_stack (flags:i32)->* mut WordList } } +#[no_mangle] +pub extern "C" fn cmd_name() ->*const u8 { + return b"pushd" as *const u8; +} + +#[no_mangle] +pub extern "C" fn run(list : *mut WordList)->i32 { + return r_pushd_builtin(list); +} \ 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 4ea450bc..2fed8da3 100644 --- a/bash-5.1/builtins_rust/read/src/intercdep.rs +++ b/bash-5.1/builtins_rust/read/src/intercdep.rs @@ -1,4 +1,22 @@ -use rcommon::{r_builtin_usage,r_sh_invalidid,r_builtin_bind_variable,WordList,WordDesc}; + +#[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::{r_builtin_usage,r_sh_invalidid,r_builtin_bind_variable}; +//use rcommon::{r_builtin_usage,r_sh_invalidid,r_builtin_bind_variable,SHELL_VAR}; pub type SHELL_VAR = rcommon::SHELL_VAR; pub type __intmax_t = c_long; @@ -115,7 +133,7 @@ extern "C" { pub static terminating_signal : c_int; pub static trapped_signal_received : c_int; - // pub static mut alrmbuf: sigjmp_buf; + pub static mut alrmbuf: sigjmp_buf; pub static mut rl_instream: *mut libc::FILE; @@ -303,11 +321,3 @@ pub struct array_element { pub prev: *mut array_element, } pub type ARRAY_ELEMENT = array_element; - - -extern "C" { - static is_basic_table:[libc::c_uint;0]; - static mut sigalrm_seen:libc::c_int; - // static mut tty_modified:libc::c_int ; - // fn is_basic(c:libc::c_char)->libc::c_int; -} diff --git a/bash-5.1/builtins_rust/read/src/lib.rs b/bash-5.1/builtins_rust/read/src/lib.rs index 946f8f9c..6fcffeea 100644 --- a/bash-5.1/builtins_rust/read/src/lib.rs +++ b/bash-5.1/builtins_rust/read/src/lib.rs @@ -1,20 +1,13 @@ use libc::{c_int, c_char, c_long, c_ulong, c_uint, size_t, c_void, PT_NULL, ssize_t}; use nix::errno::errno; -use std::{ffi::{CString, CStr}, ptr::null_mut,}; +use std::{ffi::{CString, CStr}, ptr::null_mut}; //use rcommon::{r_builtin_usage,r_sh_invalidid,r_builtin_bind_variable,SHELL_VAR}; include!(concat!("intercdep.rs")); -#[no_mangle] -pub static mut alrmbuf:sigjmp_buf = [__jmp_buf_tag{ - __jmpbuf:[0;8], - __mask_was_saved:0, - __saved_mask:__sigset_t{__val:[0;16]}, -};1]; - static mut old_alrm : *mut SigHandler = PT_NULL as *mut SigHandler; -// static mut sigalrm_seen : c_int = 0; +static mut sigalrm_seen : c_int = 0; static mut reading : c_int = 0; static mut tty_modified : c_int = 0; @@ -25,25 +18,12 @@ pub struct tty_save { attrs: libc::termios, } -// static mut termsave : Option = None; -static mut termsave:tty_save = tty_save{ - fd:0, - attrs:libc::termios { - c_iflag: (0), - c_oflag: (0), - c_cflag: (0), - c_lflag: (0), - c_line: (0), - c_cc: [0;32], - c_ispeed: (0), - c_ospeed: (0) - } -}; +static mut termsave : Option = None; +static mut ptermsave: *mut c_void = PT_NULL as *mut c_void; static mut interactive : c_int = 0; static mut default_buffered_input : c_int = -1; - #[no_mangle] pub extern "C" fn r_read_builtin(mut list: *mut WordList) -> i32 { println!("r_read_builtin call"); @@ -129,11 +109,11 @@ pub extern "C" fn r_read_builtin(mut list: *mut WordList) -> i32 { unsafe { - // if termsave.is_none() { - // let tmp: tty_save = std::mem::zeroed(); - // termsave = Some(tmp); - // } - // ptermsave = std::mem::transmute(&termsave.unwrap()); + if termsave.is_none() { + let tmp: tty_save = std::mem::zeroed(); + termsave = Some(tmp); + } + ptermsave = std::mem::transmute(&termsave.unwrap()); reset_internal_getopt(); let opt_str = CString::new("ersa:d:i:n:p:t:u:N:").unwrap(); @@ -188,12 +168,8 @@ unsafe { return EXECUTION_FAILURE; } } - 'd' => { - delim = *list_optarg; - } - + 'd' => delim = *list_optarg, _ => { - // builtin_usage(); r_builtin_usage (); return EX_USAGE; } @@ -203,38 +179,32 @@ unsafe { list = loptend; - //-t - if have_timeout != 0 && tmsec == 0 && tmusec == 0 { + if have_timeout == 1 && tmsec == 0 && tmusec == 0 { return if input_avail(fd) != 0 {EXECUTION_SUCCESS} else {EXECUTION_FAILURE}; } vflags = if assoc_expand_once != 0 {(VA_NOEXPAND | VA_ONEWORD) as c_int} else {0}; + if !list.is_null() && legal_identifier((*(*list).word).word) == 0 && valid_array_reference((*(*list).word).word, vflags) == 0 { - // sh_invalidid((*(*list).word).word); r_sh_invalidid((*(*list).word).word); return EXECUTION_FAILURE; } - //忽略界定符 - if ignore_delim != 0{ //-N ignore_delim = 1 - delim = -1; - } - - ifs_chars = getifs(); //ifs_chars is "\n" + ifs_chars = getifs(); if ifs_chars.is_null() { ifs_chars = ifs_chars_null.as_ptr() as *mut c_char; } if ignore_delim != 0 { + delim = -1; ifs_chars = ifs_chars_null.as_ptr() as *mut c_char; } skip_ctlesc = 0; skip_ctlnul = 0; e = ifs_chars; - loop { if *e == 0 { break; @@ -256,7 +226,6 @@ unsafe { break 'out_assig_vars; } - //设置TMOUT后,TMOUT是默认读取时间 let str_val = CString::new("TMOUT").unwrap(); e = get_string_value(str_val.as_ptr()); if have_timeout == 0 && !e.is_null() { @@ -270,10 +239,9 @@ unsafe { } } - let frame_name = CString::new("r_read_builtin").unwrap(); //有没有可能是r_read_builtin? + let frame_name = CString::new("read_builtin").unwrap(); begin_unwind_frame(frame_name.as_ptr() as *mut c_char); - if interactive == 0 && default_buffered_input >= 0 && fd_is_bash_input(fd) != 0 { sync_buffered_stream(default_buffered_input); } @@ -283,7 +251,6 @@ unsafe { input_is_pipe = (libc::lseek(fd, 0, libc::SEEK_CUR) < 0 && (errno() == libc::ESPIPE)) as c_int; } - //如果设置 -p,-e,-s但输入不是终端,忽略 if (!prompt.is_null() || edit != 0 || silent != 0) && input_is_tty == 0 { itext = PT_NULL as *mut c_char; edit = 0; @@ -308,7 +275,6 @@ unsafe { if code != 0 { sigalrm_seen = 0; orig_input_string = PT_NULL as *mut c_char; - *input_string.offset(i as isize) = b'\0' as c_char; if i == 0 { t = libc::malloc(1) as *mut c_char; *t = b'\0' as c_char; @@ -316,6 +282,7 @@ unsafe { t = libc::strcpy( xmalloc( (libc::strlen(input_string) + 1) as size_t) as *mut c_char, input_string); } + run_unwind_frame(frame_name.as_ptr() as *mut c_char); input_string = t; retval = 128 + libc::SIGALRM; @@ -335,10 +302,11 @@ unsafe { add_unwind_protect(bashline_reset_event_hook as *mut c_void, PT_NULL as *mut c_char); } + falarm(tmsec, tmusec); } - if nchars > 0 || delim != b'\n' as c_char { //-d -n + if nchars > 0 || delim != b'\n' as c_char { if edit != 0 { if nchars > 0 { unwind_protect_mem(&mut rl_num_chars_to_read as *mut c_int, std::mem::size_of_val(&rl_num_chars_to_read) as c_int); @@ -349,13 +317,10 @@ unsafe { set_eol_delim(delim as c_int); add_unwind_protect(reset_eol_delim as *mut c_void, PT_NULL as *mut c_char); } - } else if input_is_tty != 0 { //-d -n - // termsave.unwrap().fd = fd; - termsave.fd = fd; + } else if input_is_tty != 0 { + termsave.unwrap().fd = fd; ttgetattr(fd, &mut ttattrs as *mut libc::termios); - // termsave.unwrap().attrs = ttattrs; - termsave.attrs = ttattrs; - + termsave.unwrap().attrs = ttattrs; ttset = ttattrs; if silent != 0 { i = ttfd_cbreak(fd, std::mem::transmute(&ttset)); @@ -367,19 +332,16 @@ unsafe { sh_ttyerror(1); } tty_modified = 1; - // add_unwind_protect(ttyrestore as *mut c_void, ptermsave); - add_unwind_protect(ttyrestore as *mut c_void, &mut termsave); + add_unwind_protect(ttyrestore as *mut c_void, ptermsave); if interactive_shell == 0 { initialize_terminating_signals(); } } - } else if silent != 0 { //-s - // termsave.unwrap().fd = fd; - termsave.fd = fd; + } else if silent != 0 { + termsave.unwrap().fd = fd; ttgetattr(fd, &mut ttattrs as *mut libc::termios); - // termsave.unwrap().attrs = ttattrs; - termsave.attrs = ttattrs; + termsave.unwrap().attrs = ttattrs; ttset = ttattrs; i = ttfd_noecho(fd, std::mem::transmute(&ttset)); @@ -388,8 +350,7 @@ unsafe { } tty_modified = 1; - // add_unwind_protect(ttyrestore as *mut c_void, ptermsave); - add_unwind_protect(ttyrestore as *mut c_void, &mut termsave ); + add_unwind_protect(ttyrestore as *mut c_void, ptermsave); if interactive_shell == 0 { initialize_terminating_signals(); } @@ -397,7 +358,6 @@ unsafe { save_instream = std::mem::zeroed(); if edit != 0 && fd != 0 { - if bash_readline_initialized == 0 { initialize_readline(); } @@ -416,9 +376,8 @@ unsafe { unbuffered_read = 1; } - if !prompt.is_null() && edit == 0 { //-p no -e - // eprintln!("{}", CStr::from_ptr(prompt).to_str().unwrap()); - eprint!("{}", CStr::from_ptr(prompt).to_str().unwrap()); + if !prompt.is_null() && edit == 0 { + eprintln!("{}", CStr::from_ptr(prompt).to_str().unwrap()); } @@ -431,7 +390,7 @@ unsafe { siglongjmp (std::mem::transmute(&alrmbuf), 1); } - if edit != 0 { //没有设置-e edit等于0 + if edit != 0 { if !rlbuf.is_null() && *((rlbuf as usize + rlind as usize) as *mut c_char) == 0 && delim != 0 { @@ -440,14 +399,8 @@ unsafe { } if rlbuf.is_null() { reading = 1; - rlbuf = if prompt.is_null() { - // edit_line("".as_ptr() as *mut c_char, itext)} - // let c_str = b'\0'; // b'\0'代表的是空字符串,和String::from("")不是同一个东西。 - edit_line(b'\0' as *mut c_char,itext) // b'\0'代表的是空字符串,和String::from("")不是同一个东西。 - } - else { - edit_line(prompt, itext) - }; + rlbuf = if prompt.is_null() {edit_line("".as_ptr() as *mut c_char, itext)} + else {edit_line(prompt, itext)}; reading = 0; rlind = 0; } @@ -470,26 +423,16 @@ unsafe { check_alrm(); *(libc::__errno_location()) = 0; if unbuffered_read == 2 { - retval = if posixly_correct != 0 { - zreadintr(fd, &mut c as *mut c_char, 1) as c_int - } - else { - zreadn(fd, &mut c as *mut c_char, (nchars - nr) as usize) as c_int - }; + retval = if posixly_correct != 0 {zreadintr(fd, &mut c as *mut c_char, 1) as c_int} + else {zreadn(fd, &mut c as *mut c_char, (nchars - nr) as usize) as c_int}; } else if unbuffered_read != 0 { - retval = if posixly_correct != 0 { - zreadintr(fd, &mut c as *mut c_char, 1) as c_int} - else { - zread(fd, &mut c as *mut c_char, 1) as c_int}; - } - else { - retval = if posixly_correct != 0 { - zreadcintr(fd, &mut c as *mut c_char) as c_int - } - else { //-a -t - zreadc(fd, &mut c as *mut c_char) as c_int - }; + retval = if posixly_correct != 0 {zreadintr(fd, &mut c as *mut c_char, 1) as c_int} + else {zreadn(fd, &mut c as *mut c_char, 1) as c_int}; + } else { + retval = if posixly_correct != 0 {zreadcintr(fd, &mut c as *mut c_char) as c_int} + else {zreadc(fd, &mut c as *mut c_char) as c_int}; } + reading = 0; if retval <= 0 { @@ -509,9 +452,9 @@ unsafe { } else { lastsig = 0; } + if terminating_signal != 0 && tty_modified != 0 { - // ttyrestore(); - ttyrestore(&mut termsave); + ttyrestore(); } check_alrm(); eof = 1; @@ -539,7 +482,7 @@ unsafe { } } 'out_add_char: loop { - if pass_next != 0 { + if pass_next != 0 { pass_next = 0; if c == b'\n' as c_char { if skip_ctlesc == 0 && i > 0 {i -= 1;} @@ -557,11 +500,10 @@ unsafe { *((input_string as usize + i as usize) as *mut c_char) = CTLESC; i += 1; } - continue 'get_input_string; } - if ignore_delim == 0 && c == delim { //-a + if ignore_delim == 0 && c == delim { break 'get_input_string; } @@ -575,14 +517,12 @@ unsafe { i += 1; } break 'out_add_char; - }//out_add_char - + } *((input_string as usize + i as usize) as *mut c_char) = c; i += 1; check_alrm(); if mb_cur_max > 1 && is_basic(c) == 0 { - *((input_string as usize + i as usize) as *mut c_char) = b'\0' as c_char; if edit != 0 { @@ -598,13 +538,13 @@ unsafe { } else if locale_utf8locale == 0 || ((c as u8 & 0x80) != 0) { i += read_mbchar(fd, input_string, i, c as c_int, unbuffered_read); } + + nr += 1; + if nchars > 0 && nr >= nchars { + break 'get_input_string; + } } - nr += 1; - if nchars > 0 && nr >= nchars { - break 'get_input_string; - } - - } //get_input_string + } *((input_string as usize + i as usize) as *mut c_char) = b'\0' as c_char; check_alrm(); @@ -630,25 +570,15 @@ unsafe { if nchars > 0 || delim != b'\n' as c_char { if edit != 0 { - if nchars > 0{ - rl_num_chars_to_read = 0; - } - if delim != b'\n' as c_char{ - reset_eol_delim(0 as *mut c_char); - } - } - else if input_is_tty != 0 { - // ttyrestore(); - ttyrestore(&mut termsave); + + } else if input_is_tty != 0 { + ttyrestore(); } - } - else if silent != 0 { - // ttyrestore(); - ttyrestore(&mut termsave); + } else if silent != 0 { + ttyrestore(); } - // if unbuffered_read != 0 { - if unbuffered_read == 0 { + if unbuffered_read != 0 { zsyncfd(fd); } @@ -661,11 +591,10 @@ unsafe { retval = if eof != 0 {EXECUTION_FAILURE} else {EXECUTION_SUCCESS}; break 'out_assig_vars; -} //out_assig_vars +} - if !arrayname.is_null() { //和-a有关 - if legal_identifier(arrayname) == 0 { //标签不符合规范 - // sh_invalidid(arrayname); + if !arrayname.is_null() { + if legal_identifier(arrayname) == 0 { r_sh_invalidid(arrayname); libc::free(input_string as *mut c_void); return EXECUTION_FAILURE; @@ -699,11 +628,10 @@ unsafe { } libc::free(input_string as *mut c_void); - return retval; } - if list.is_null() { //和-d相关 -n 0可以退出,有显示 + if list.is_null() { if saw_escape != 0 { let t = dequote_string(input_string); var = bind_variable("REPLY".as_ptr() as *const c_char, t, 0); @@ -737,7 +665,6 @@ unsafe { if legal_identifier(varname) == 0 && valid_array_reference(varname, vflags) == 0 { - // sh_invalidid(varname); r_sh_invalidid(varname); libc::free(orig_input_string as *mut c_void); return EXECUTION_FAILURE; @@ -776,7 +703,6 @@ unsafe { if legal_identifier((*((*list).word)).word) == 0 && valid_array_reference((*((*list).word)).word, vflags) == 0 { - // sh_invalidid((*((*list).word)).word); r_sh_invalidid((*((*list).word)).word); libc::free(orig_input_string as *mut c_void); return EXECUTION_FAILURE; @@ -815,31 +741,21 @@ unsafe { } libc::free(orig_input_string as *mut c_void); return retval; -} //unsafe +} } /* ---------------------------------------------------------------------------------- */ -// pub fn is_basic(c: i8) -> u32 { -// let is_basic_table :[c_uint; 8] = [ 0x00001a00, 0xffffffef, 0xfffffffe, 0x7ffffffe, 0,0,0,0]; +pub fn is_basic(c: i8) -> u32 { + let is_basic_table :[c_uint; 8] = [ 0x00001a00, 0xffffffef, 0xfffffffe, 0x7ffffffe, 0,0,0,0]; -// let index = (c >> 5) as usize; -// return (is_basic_table[index] >> (c & 31) ) & 1; -// } - -#[inline] -unsafe extern "C" fn is_basic(mut c:libc::c_char)->libc::c_int{ - return (*is_basic_table - .as_ptr() - .offset((c as libc::c_uchar as libc::c_int>>5 as libc::c_int)as isize) - >>(c as libc::c_uchar as libc::c_int & 31 as libc::c_int) - & 1 as libc::c_int as libc::c_uint) as libc::c_int; + let index = (c >> 5) as usize; + return (is_basic_table[index] >> (c & 31) ) & 1; } pub fn bind_read_variable(name: *mut c_char, value: *mut c_char) -> * mut SHELL_VAR { let v: *mut SHELL_VAR; unsafe { - // v = builtin_bind_variable(name, value, 0); v = r_builtin_bind_variable(name, value, 0); if v.is_null() { @@ -922,7 +838,6 @@ fn check_alrm() { unsafe { if sigalrm_seen != 0 { siglongjmp (std::mem::transmute(&alrmbuf), 1); - // siglongjmp (&mut alrmbuf as *mut __jmp_buf_tag, 1); } } } @@ -962,78 +877,38 @@ unsafe { return (r1 != 0 || r2 != 0) as c_int; } -// fn edit_line(p : *mut c_char, itext : *mut c_char) -> *mut c_char { -// unsafe { -// if bash_readline_initialized == 0 { -// initialize_readline(); -// } - -// old_attempted_completion_function = std::mem::transmute(rl_attempted_completion_function); -// rl_attempted_completion_function = std::mem::transmute(0 as usize); -// bashline_set_event_hook(); -// if !itext.is_null() { -// old_startup_hook = std::mem::transmute(rl_startup_hook); -// rl_startup_hook = std::mem::transmute(set_itext as usize); -// deftext = itext; -// } - -// let mut ret = readline(p); - -// rl_attempted_completion_function = std::mem::transmute(old_attempted_completion_function); -// old_attempted_completion_function = std::mem::transmute(0 as usize); -// bashline_reset_event_hook(); - -// if ret.is_null() { -// return ret; -// } +fn edit_line(p : *mut c_char, itext : *mut c_char) -> *mut c_char { +unsafe { + if bash_readline_initialized == 0 { + initialize_readline(); + } -// let len:i32 = libc::strlen(ret) as i32; -// ret = xrealloc(ret as *mut c_void, (len + 2) as usize) as *mut c_char; -// // *ret = delim; -// *ret.offset(len as isize) = delim; -// *((ret as usize + 1) as *mut c_char) = b'\0' as c_char; + old_attempted_completion_function = std::mem::transmute(rl_attempted_completion_function); + rl_attempted_completion_function = std::mem::transmute(0 as usize); + bashline_set_event_hook(); + if !itext.is_null() { + old_startup_hook = std::mem::transmute(rl_startup_hook); + rl_startup_hook = std::mem::transmute(set_itext as usize); + deftext = itext; + } -// return ret; -// } -// } + let mut ret = readline(p); + rl_attempted_completion_function = std::mem::transmute(old_attempted_completion_function); + old_attempted_completion_function = std::mem::transmute(0 as usize); + bashline_reset_event_hook(); -fn edit_line(p : *mut c_char, itext : *mut c_char) -> *mut c_char { - let mut len:i32; - unsafe { - if bash_readline_initialized == 0 { - initialize_readline(); - } - - old_attempted_completion_function = std::mem::transmute(rl_attempted_completion_function); - rl_attempted_completion_function = std::mem::transmute(0 as usize); - bashline_set_event_hook(); - if !itext.is_null() { - old_startup_hook = std::mem::transmute(rl_startup_hook); - rl_startup_hook = std::mem::transmute(set_itext as usize); - deftext = itext; - } - - let mut ret = readline(p); - - rl_attempted_completion_function = std::mem::transmute(old_attempted_completion_function); - old_attempted_completion_function = std::mem::transmute(0 as usize); - bashline_reset_event_hook(); - - if ret.is_null() { - return ret; - } - - len = libc::strlen(ret) as i32; - ret = xrealloc(ret as *mut c_void, (len + 2) as usize) as *mut c_char; - *ret.offset(len as isize) = delim; - len += 1; - *ret.offset(len as isize) = b'\0' as c_char; + if ret.is_null() { return ret; } -} - + let len = libc::strlen(ret); + ret = xrealloc(ret as *mut c_void, len + 2) as *mut c_char; + *ret = delim; + *((ret as usize + 1) as *mut c_char) = b'\0' as c_char; + return ret; +} +} fn sigalrm(s : c_int) { unsafe { @@ -1049,23 +924,18 @@ unsafe { } } -// fn ttyrestore() -// { -// unsafe { -// if termsave.is_none() { -// let tmp: tty_save = std::mem::zeroed(); -// termsave = Some(tmp); -// } - -// let ter = termsave.unwrap(); -// ttsetattr(ter.fd, std::mem::transmute(&(ter.attrs))); -// tty_modified = 0; -// } -// } - -unsafe extern "C" fn ttyrestore(mut ttp:*mut tty_save){ - ttsetattr((*ttp).fd,&mut (*ttp).attrs); - tty_modified = 0 as libc::c_int; +fn ttyrestore() +{ +unsafe { + if termsave.is_none() { + let tmp: tty_save = std::mem::zeroed(); + termsave = Some(tmp); + } + + let ter = termsave.unwrap(); + ttsetattr(ter.fd, std::mem::transmute(&(ter.attrs))); + tty_modified = 0; +} } #[no_mangle] @@ -1073,7 +943,7 @@ pub extern "C" fn read_tty_cleanup() { unsafe { if tty_modified != 0 { - ttyrestore(&mut termsave); + ttyrestore(); } } } @@ -1092,59 +962,31 @@ static mut old_newline_ctype: c_int = 0; static mut old_newline_func: usize = 0; static mut delim_char: u8 = 0; -// fn set_eol_delim(c: c_int) -// { -// unsafe { -// if bash_readline_initialized == 0 { -// initialize_readline(); -// } - -// let cmap = rl_get_keymap(); -// let n = std::mem::size_of_val(&*cmap); -// let ret_pos = (b'M' & 0x1f) as usize * n; -// let c_pos = (c & 0x1f) as usize * n; - -// /* Save the old delimiter char binding */ -// old_newline_ctype = (*((cmap as usize + ret_pos) as Keymap)).tp as c_int; -// old_newline_func = (*((cmap as usize + ret_pos) as Keymap)).function as usize; -// old_delim_ctype = (*((cmap as usize + c_pos) as Keymap)).tp as c_int; -// old_delim_func = (*((cmap as usize + c_pos) as Keymap)).function as usize; - -// /* Change newline to self-insert */ -// (*((cmap as usize + ret_pos) as Keymap)).tp = ISFUNC as c_char; -// (*((cmap as usize + ret_pos) as Keymap)).function = rl_insert; - -// /* Bind the delimiter character to accept-line. */ -// (*((cmap as usize + c_pos) as Keymap)).tp = ISFUNC as c_char; -// (*((cmap as usize + c_pos) as Keymap)).function = rl_newline; - -// delim_char = c as u8; -// } -// } - fn set_eol_delim(c: c_int) { - let mut cmap:Keymap; unsafe { if bash_readline_initialized == 0 { initialize_readline(); } - // let cmap = rl_get_keymap(); - cmap = rl_get_keymap(); + let cmap = rl_get_keymap(); + let n = std::mem::size_of_val(&*cmap); + let ret_pos = (b'M' & 0x1f) as usize * n; + let c_pos = (c & 0x1f) as usize * n; - old_newline_ctype = (*cmap.offset((b'M' as i32 & 0x1f) as isize)).tp as c_int; - old_newline_func = (*cmap.offset((b'M' as i32 & 0x1f) as isize)).function as usize; - old_delim_ctype = (*cmap.offset(c as isize)).tp as c_int; - old_delim_func = (*cmap.offset(c as isize)).function as usize; + /* Save the old delimiter char binding */ + old_newline_ctype = (*((cmap as usize + ret_pos) as Keymap)).tp as c_int; + old_newline_func = (*((cmap as usize + ret_pos) as Keymap)).function as usize; + old_delim_ctype = (*((cmap as usize + c_pos) as Keymap)).tp as c_int; + old_delim_func = (*((cmap as usize + c_pos) as Keymap)).function as usize; - /* Change newline to self-insert */ - (*cmap.offset((b'M' as i32 & 0x1f) as isize)).tp = ISFUNC as c_char; - (*cmap.offset((b'M' as i32 & 0x1f) as isize)).function = rl_insert; + /* Change newline to self-insert */ + (*((cmap as usize + ret_pos) as Keymap)).tp = ISFUNC as c_char; + (*((cmap as usize + ret_pos) as Keymap)).function = rl_insert; /* Bind the delimiter character to accept-line. */ - (*cmap.offset(c as isize)).tp = ISFUNC as c_char; - (*cmap.offset(c as isize)).function = rl_newline; + (*((cmap as usize + c_pos) as Keymap)).tp = ISFUNC as c_char; + (*((cmap as usize + c_pos) as Keymap)).function = rl_newline; delim_char = c as u8; } diff --git a/bash-5.1/builtins_rust/rlet/Cargo.toml b/bash-5.1/builtins_rust/rlet/Cargo.toml index b52101ca..c468265b 100644 --- a/bash-5.1/builtins_rust/rlet/Cargo.toml +++ b/bash-5.1/builtins_rust/rlet/Cargo.toml @@ -14,4 +14,3 @@ 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 d37ae34b..fda0e61a 100644 --- a/bash-5.1/builtins_rust/rlet/src/intercdep.rs +++ b/bash-5.1/builtins_rust/rlet/src/intercdep.rs @@ -1,4 +1,19 @@ -use rcommon::{WordList}; + +#[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 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 a437a262..0a60e09d 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 32066f4e..24ab4ecc 100644 --- a/bash-5.1/builtins_rust/set/src/lib.rs +++ b/bash-5.1/builtins_rust/set/src/lib.rs @@ -6,7 +6,18 @@ use std::ffi::CString; use std::ptr; use std::mem; use std::io; -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 i8, + pub flags:i32 +} + +#[repr (C)] +#[derive(Copy,Clone)] +pub struct WordList { + next: *mut WordList, + word: *mut WordDesc +} #[macro_export] macro_rules! FLAG_UNKNOWN { @@ -248,6 +259,20 @@ 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{ @@ -1214,7 +1239,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{ @@ -1420,7 +1445,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 (); @@ -1429,14 +1454,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!(); } } } @@ -1647,7 +1672,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 90004e4d..1e1d381e 100644 --- a/bash-5.1/builtins_rust/setattr/Cargo.toml +++ b/bash-5.1/builtins_rust/setattr/Cargo.toml @@ -18,4 +18,3 @@ 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 8c465593..98a323e0 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,6 +7,14 @@ 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 7130cc61..5e0851b5 100644 --- a/bash-5.1/builtins_rust/shift/src/intercdep.rs +++ b/bash-5.1/builtins_rust/shift/src/intercdep.rs @@ -1,4 +1,19 @@ -use rcommon::{WordList}; + +#[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 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 c557ab77..ff0a3197 100644 --- a/bash-5.1/builtins_rust/shopt/src/lib.rs +++ b/bash-5.1/builtins_rust/shopt/src/lib.rs @@ -2,8 +2,9 @@ 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 */ @@ -912,8 +913,11 @@ 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; @@ -968,7 +972,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 @@ -1088,12 +1092,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() { @@ -1153,7 +1157,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 { @@ -1162,18 +1166,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); } @@ -1196,7 +1200,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( @@ -1205,12 +1209,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; @@ -1218,10 +1222,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 { @@ -1252,7 +1256,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, @@ -1263,7 +1267,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; @@ -1464,7 +1468,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 8057a26f..91e31631 100644 --- a/bash-5.1/builtins_rust/source/Cargo.toml +++ b/bash-5.1/builtins_rust/source/Cargo.toml @@ -9,7 +9,6 @@ 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 a33457a4..53de38f7 100644 --- a/bash-5.1/builtins_rust/source/src/lib.rs +++ b/bash-5.1/builtins_rust/source/src/lib.rs @@ -3,8 +3,19 @@ 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, @@ -23,23 +34,22 @@ 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:libc::c_int, + redirector:REDIRECTEE, rflags:libc::c_int, flags:libc::c_int, instruction:r_instruction, - redirectee:libc::c_int, + redirectee:REDIRECTEE, here_doc_eof:*mut c_char } @@ -191,6 +201,15 @@ 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 { @@ -300,7 +319,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(); @@ -308,7 +327,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() { @@ -386,3 +405,12 @@ pub extern "C" fn r_source_builtin (list:* mut WordList)->i32 } } +#[no_mangle] +pub extern "C" fn cmd_name() ->*const u8 { + return b"source" as *const u8; +} + +#[no_mangle] +pub extern "C" fn run(list : *mut WordList)->i32 { + return r_source_builtin(list); +} \ 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 c2f5d321..eb18f382 100644 --- a/bash-5.1/builtins_rust/times/src/intercdep.rs +++ b/bash-5.1/builtins_rust/times/src/intercdep.rs @@ -1,4 +1,19 @@ -use rcommon::WordList; + +#[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 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 01ab1e93..08ffe400 100644 --- a/bash-5.1/builtins_rust/type/Cargo.toml +++ b/bash-5.1/builtins_rust/type/Cargo.toml @@ -10,7 +10,6 @@ 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 c2318bfd..d4374c95 100644 --- a/bash-5.1/builtins_rust/type/src/lib.rs +++ b/bash-5.1/builtins_rust/type/src/lib.rs @@ -5,7 +5,6 @@ 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{ @@ -65,12 +64,25 @@ 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{ @@ -111,6 +123,19 @@ 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 { @@ -443,11 +468,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 0e03d966..4fbd136f 100644 --- a/bash-5.1/builtins_rust/ulimit/Cargo.toml +++ b/bash-5.1/builtins_rust/ulimit/Cargo.toml @@ -11,7 +11,6 @@ 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 2ed26a22..adf0be5e 100644 --- a/bash-5.1/builtins_rust/ulimit/src/lib.rs +++ b/bash-5.1/builtins_rust/ulimit/src/lib.rs @@ -5,7 +5,6 @@ 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)] @@ -15,6 +14,19 @@ 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)] @@ -113,6 +125,20 @@ 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] @@ -484,17 +510,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 ; @@ -590,7 +616,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 6e153736..9f2b72a3 100644 --- a/bash-5.1/builtins_rust/umask/src/lib.rs +++ b/bash-5.1/builtins_rust/umask/src/lib.rs @@ -3,10 +3,22 @@ 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, +} //枚举 @@ -20,6 +32,20 @@ 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{ @@ -213,7 +239,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 79362038..98d236ae 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,6 +85,24 @@ 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 { @@ -275,7 +293,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 5192ebbc..d8c40484 100644 --- a/record.txt +++ b/record.txt @@ -25,3 +25,4 @@ 47 48 49 +50 -- Gitee From b3856efd3890bafcf606d8e47b7df50e0b4ba77d Mon Sep 17 00:00:00 2001 From: liutong Date: Tue, 27 Jun 2023 21:37:52 +0800 Subject: [PATCH 2/6] merge common from alias to eval --- bash-5.1/builtins/bind.def | 2 + bash-5.1/builtins_rust/alias/src/lib.rs | 69 +++++------ bash-5.1/builtins_rust/bind/src/lib.rs | 116 ++++++------------ bash-5.1/builtins_rust/break_1/Cargo.toml | 1 + bash-5.1/builtins_rust/break_1/src/lib.rs | 30 +---- bash-5.1/builtins_rust/builtin/Cargo.toml | 1 + .../builtins_rust/builtin/src/intercdep.rs | 21 +--- bash-5.1/builtins_rust/builtin/src/lib.rs | 4 +- bash-5.1/builtins_rust/caller/Cargo.toml | 1 + bash-5.1/builtins_rust/caller/src/lib.rs | 42 +------ bash-5.1/builtins_rust/cd/Cargo.toml | 1 + bash-5.1/builtins_rust/cd/src/lib.rs | 37 +----- bash-5.1/builtins_rust/colon/Cargo.toml | 1 + bash-5.1/builtins_rust/colon/src/lib.rs | 16 +-- bash-5.1/builtins_rust/command/Cargo.toml | 1 + bash-5.1/builtins_rust/command/src/lib.rs | 24 +--- bash-5.1/builtins_rust/common/src/lib.rs | 2 +- bash-5.1/builtins_rust/complete/Cargo.toml | 1 + bash-5.1/builtins_rust/complete/src/lib.rs | 59 +++------ bash-5.1/builtins_rust/declare/src/lib.rs | 32 +---- bash-5.1/builtins_rust/echo/Cargo.toml | 1 + bash-5.1/builtins_rust/echo/src/lib.rs | 19 +-- bash-5.1/builtins_rust/enable/Cargo.toml | 1 + bash-5.1/builtins_rust/enable/src/lib.rs | 39 ++---- bash-5.1/builtins_rust/eval/src/lib.rs | 33 +---- bash-5.1/builtins_rust/exit/Cargo.toml | 1 + bash-5.1/builtins_rust/exit/src/lib.rs | 3 +- record.txt | 1 + 28 files changed, 129 insertions(+), 430 deletions(-) diff --git a/bash-5.1/builtins/bind.def b/bash-5.1/builtins/bind.def index 7fc8aba4..72ace6d2 100644 --- a/bash-5.1/builtins/bind.def +++ b/bash-5.1/builtins/bind.def @@ -108,6 +108,7 @@ static int unbind_keyseq PARAMS((char *)); #define XXFLAG 0x2000 extern int r_bind_builtin(WORD_LIST *); +extern int r_unbind_builtin(WORD_LIST *); int bind_builtin (list) @@ -152,6 +153,7 @@ static int unbind_command (name) char *name; { +//return r_unbind_builtin(name); rl_command_func_t *function; function = rl_named_function (name); diff --git a/bash-5.1/builtins_rust/alias/src/lib.rs b/bash-5.1/builtins_rust/alias/src/lib.rs index 31bd0186..61a4b016 100644 --- a/bash-5.1/builtins_rust/alias/src/lib.rs +++ b/bash-5.1/builtins_rust/alias/src/lib.rs @@ -1,6 +1,7 @@ use std::ffi::CStr; use rcommon::r_sh_notfound; +use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; extern "C" { fn free(__ptr: *mut libc::c_void); fn dcgettext( @@ -12,12 +13,12 @@ extern "C" { fn legal_alias_name(_: *const libc::c_char, _: libc::c_int) -> libc::c_int; fn sh_single_quote(_: *const libc::c_char) -> *mut libc::c_char; static mut posixly_correct: libc::c_int; - static mut aliases: *mut HASH_TABLE; - fn find_alias(_: *mut libc::c_char) -> *mut alias_t; + static mut aliases: *mut HashTable; + fn find_alias(_: *mut libc::c_char) -> *mut AliasT; fn add_alias(_: *mut libc::c_char, _: *mut libc::c_char); fn remove_alias(_: *mut libc::c_char) -> libc::c_int; fn delete_all_aliases(); - fn all_aliases() -> *mut *mut alias_t; + fn all_aliases() -> *mut *mut AliasT; fn builtin_error(_: *const libc::c_char, _: ...); fn builtin_usage(); fn sh_notfound(_: *mut libc::c_char); @@ -27,22 +28,8 @@ extern "C" { fn internal_getopt(_: *mut WordList, _: *mut libc::c_char) -> libc::c_int; fn reset_internal_getopt(); } -pub type size_t = libc::c_ulong; +pub type SizeT = libc::c_ulong; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct word_desc { - pub word: *mut libc::c_char, - pub flags: libc::c_int, -} -pub type WordDesc = word_desc; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct word_list { - pub next: *mut word_list, - pub word: *mut WordDesc, -} -pub type WordList = word_list; #[derive(Copy, Clone)] #[repr(C)] pub struct bucket_contents { @@ -52,15 +39,15 @@ pub struct bucket_contents { pub khash: libc::c_uint, pub times_found: libc::c_int, } -pub type BUCKET_CONTENTS = bucket_contents; +pub type BucketContents = bucket_contents; #[derive(Copy, Clone)] #[repr(C)] pub struct hash_table { - pub bucket_array: *mut *mut BUCKET_CONTENTS, + pub bucket_array: *mut *mut BucketContents, pub nbuckets: libc::c_int, pub nentries: libc::c_int, } -pub type HASH_TABLE = hash_table; +pub type HashTable = hash_table; #[derive(Copy, Clone)] #[repr(C)] pub struct alias { @@ -68,26 +55,25 @@ pub struct alias { pub value: *mut libc::c_char, pub flags: libc::c_char, } -pub type alias_t = alias; +pub type AliasT = alias; pub static AL_REUSABLE:i32 = 0x01; -pub static EX_USAGE:i32 = 258; + //extern crate rcommon; -use rcommon::EXECUTION_SUCCESS; use rcommon::r_builtin_usage; #[no_mangle] pub unsafe extern "C" fn r_alias_builtin(mut list: *mut WordList) -> libc::c_int { println!("alias_builtin run!"); - let mut any_failed = 0; - let mut offset = 0; - let mut pflag = 0; - let mut dflags = 0; - let mut alias_list: *mut *mut alias_t; - let mut t: *mut alias_t; - let mut name: *mut libc::c_char = 0 as *mut libc::c_char; - let mut value: *mut libc::c_char = 0 as *mut libc::c_char; + let mut any_failed; + let mut offset; + let mut pflag ; + let mut dflags ; + let alias_list: *mut *mut AliasT; + let mut t: *mut AliasT; + let mut name: *mut libc::c_char; + let mut value: *mut libc::c_char; dflags = if posixly_correct != 0 { 0 as libc::c_int } else { 0x1 as libc::c_int }; pflag = 0 as libc::c_int; reset_internal_getopt(); @@ -117,11 +103,11 @@ pub unsafe extern "C" fn r_alias_builtin(mut list: *mut WordList) -> libc::c_int list = loptend; if list.is_null() || pflag != 0 { if aliases.is_null() { - return EXECUTION_SUCCESS; + return EXECUTION_SUCCESS!(); } alias_list = all_aliases(); if alias_list.is_null() { - return EXECUTION_SUCCESS; + return EXECUTION_SUCCESS!(); } offset = 0; while !(*alias_list.offset(offset as isize)).is_null() { @@ -130,7 +116,7 @@ pub unsafe extern "C" fn r_alias_builtin(mut list: *mut WordList) -> libc::c_int } free(alias_list as *mut libc::c_void); if list.is_null() { - return sh_chkwrite(EXECUTION_SUCCESS); + return sh_chkwrite(EXECUTION_SUCCESS!()); } } any_failed = 0; @@ -170,13 +156,14 @@ pub unsafe extern "C" fn r_alias_builtin(mut list: *mut WordList) -> libc::c_int } list = (*list).next; } - return if any_failed != 0 { rcommon::EXECUTION_FAILURE!()} else { EXECUTION_SUCCESS}; + return if any_failed != 0 {EXECUTION_FAILURE!()} else { EXECUTION_SUCCESS!()}; } #[no_mangle] pub unsafe extern "C" fn r_unalias_builtin(mut list: *mut WordList) -> libc::c_int { - let mut alias: *mut alias_t = 0 as *mut alias_t; - let mut opt: libc::c_int = 0; - let mut aflag: libc::c_int = 0; + println!("alias_builtin run!"); + let mut alias: *mut AliasT; + let mut opt: libc::c_int; + let mut aflag: libc::c_int; aflag = 0 as libc::c_int; reset_internal_getopt(); loop { @@ -223,8 +210,8 @@ pub unsafe extern "C" fn r_unalias_builtin(mut list: *mut WordList) -> libc::c_i } return if aflag != 0 { 1 as libc::c_int } else { 0 as libc::c_int }; } -unsafe extern "C" fn print_alias(mut alias: *mut alias_t, mut flags: libc::c_int) { - let mut value: *mut libc::c_char = 0 as *mut libc::c_char; +unsafe extern "C" fn print_alias( alias: *mut AliasT, flags: libc::c_int) { + let value: *mut libc::c_char; value = sh_single_quote((*alias).value); if flags & 0x1 as libc::c_int != 0 { printf( diff --git a/bash-5.1/builtins_rust/bind/src/lib.rs b/bash-5.1/builtins_rust/bind/src/lib.rs index b065ae63..1498452c 100644 --- a/bash-5.1/builtins_rust/bind/src/lib.rs +++ b/bash-5.1/builtins_rust/bind/src/lib.rs @@ -7,20 +7,8 @@ use std::{ffi::{CString,CStr}}; use std::fs::File; use nix::errno::errno; use rcommon::{r_builtin_usage,r_sh_chkwrite}; +use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; -//struct -#[repr (C)] -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)] pub struct _keymap_entry{ pub Type:c_char, @@ -31,24 +19,6 @@ type Keymap = *mut KEYMAP_ENTRY; type rl_command_func_t = extern fn(c_int,c_int)->c_int; //emun - - -//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! LFLAG { () => { 0x0001 } @@ -121,17 +91,13 @@ macro_rules! ANYOTHERKEY{ () => { KEYMAP_SIZE!() - 1} } -// #[macro_export] -// macro_rules! unwind_protect_var { -// ($X:expr) => { - -// unwind_protect_mem($X as *mut c_char,std::intrinsics::size_of(File)); -// // std::mem::size_of::<& REPL>() -// }; -// } -//extern "C" extern "C"{ + fn dcgettext( + __domainname: *const libc::c_char, + __msgid: *const libc::c_char, + __category: libc::c_int, + ) -> *mut libc::c_char; static no_line_editing:i32; static bash_readline_initialized:i32; static mut rl_outstream:*mut File; @@ -198,9 +164,14 @@ pub extern "C" fn r_bind_builtin(mut list:*mut WordList)->i32{ unsafe{ if no_line_editing != 0{ - let s_str = String::from("line editing not enabled"); - let s_ptr = s_str.as_ptr() as *const c_char; - builtin_warning(s_ptr); + builtin_warning( + dcgettext( + 0 as *const libc::c_char, + CString::new("line editing not enabled").unwrap().as_ptr() as *const libc::c_char, + 5 as libc::c_int, + ), + ); + } kmap = std::ptr::null_mut(); @@ -218,14 +189,19 @@ pub extern "C" fn r_bind_builtin(mut list:*mut WordList)->i32{ if bash_readline_initialized == 0{ initialize_readline(); } - let bind_str = String::from("bind_builtin"); - let bind_ptr = bind_str.as_ptr() as *mut c_char; - begin_unwind_frame(bind_ptr); - //这里是瞎写的!!!!! - // unwind_protect_var!(rl_outstream); - // unwind_protect_mem(rl_outstream as *mut c_char,8); - unwind_protect_mem(std::mem::transmute(rl_outstream), std::mem::size_of_val(&rl_outstream) as c_int); - + let bind_str = CString::new("bind_builtin").unwrap(); + // let bind_ptr = bind_str.as_ptr() as *mut c_char; + begin_unwind_frame(bind_str.as_ptr() as *mut c_char); +/* + unwind_protect_mem( + std::mem::transmute(rl_outstream), + std::mem::size_of_val(&rl_outstream) as c_int + ); +*/ + unwind_protect_mem( + &mut rl_outstream as *mut *mut File as *mut libc::c_char, + ::std::mem::size_of::<*mut File>() as libc::c_ulong as libc::c_int, + ); rl_outstream = stdout; reset_internal_getopt(); @@ -270,15 +246,14 @@ pub extern "C" fn r_bind_builtin(mut list:*mut WordList)->i32{ } 'X' => flags |= XXFLAG!(), _ => { - println!("111"); - // builtin_usage(); + r_builtin_usage(); - // BIND_RETURN!(EX_USAGE!()); - return_code = EX_USAGE!(); + + return_code = EX_USAGE; if !saved_keymap.is_null(){ rl_set_keymap(saved_keymap); } - run_unwind_frame(bind_ptr ); + run_unwind_frame(bind_str.as_ptr() as *mut c_char); if return_code < 0 { return_code = EXECUTION_FAILURE!(); } @@ -303,7 +278,7 @@ pub extern "C" fn r_bind_builtin(mut list:*mut WordList)->i32{ if !saved_keymap.is_null(){ rl_set_keymap(saved_keymap); } - run_unwind_frame(bind_ptr ); + run_unwind_frame(bind_str.as_ptr() as *mut c_char ); if return_code < 0 { return_code = EXECUTION_FAILURE!(); } @@ -354,7 +329,7 @@ pub extern "C" fn r_bind_builtin(mut list:*mut WordList)->i32{ if !saved_keymap.is_null(){ rl_set_keymap(saved_keymap); } - run_unwind_frame(bind_ptr ); + run_unwind_frame(bind_str.as_ptr() as *mut c_char); if return_code < 0 { return_code = EXECUTION_FAILURE!(); } @@ -376,7 +351,7 @@ pub extern "C" fn r_bind_builtin(mut list:*mut WordList)->i32{ if !saved_keymap.is_null(){ rl_set_keymap(saved_keymap); } - run_unwind_frame(bind_ptr ); + run_unwind_frame(bind_str.as_ptr() as *mut c_char ); if return_code < 0 { return_code = EXECUTION_FAILURE!(); } @@ -441,7 +416,7 @@ pub extern "C" fn r_bind_builtin(mut list:*mut WordList)->i32{ if !saved_keymap.is_null(){ rl_set_keymap(saved_keymap); } - run_unwind_frame(bind_ptr); + run_unwind_frame(bind_str.as_ptr() as *mut c_char); if return_code < 0{ return_code = EXECUTION_FAILURE!(); @@ -465,8 +440,7 @@ extern "C" fn r_query_bindings(name:*mut c_char)->i32{ function = rl_named_function(name); if function.is_null(){ let c_str = CString::new("%s: unknow function name").unwrap(); - let c_ptr= c_str.as_ptr(); - builtin_error(c_ptr,name); + builtin_error(c_str.as_ptr(),name); return EXECUTION_FAILURE!(); } @@ -511,8 +485,8 @@ extern "C" fn r_unbind_command(name:*mut c_char)->i32{ function = rl_named_function(name); if function.is_null(){ let c_str = CString::new("`%s':unknown function name").unwrap(); - let c_ptr = c_str.as_ptr(); - builtin_error(c_ptr,name); + + builtin_error(c_str.as_ptr(),name); return EXECUTION_FAILURE!(); } @@ -532,8 +506,8 @@ extern "C" fn r_unbind_keyseq(seq:*mut c_char)->i32{ if rl_translate_keyseq(seq,kseq,&mut kslen) != 0{ free(kseq as *mut c_void); let c_str = CString::new("`%s': cannot unbind").unwrap(); - let c_ptr = c_str.as_ptr(); - builtin_error(c_ptr, seq); + + builtin_error(c_str.as_ptr(), seq); return EXECUTION_FAILURE!(); } //可能存在错误 @@ -567,13 +541,3 @@ extern "C" fn r_unbind_keyseq(seq:*mut c_char)->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/break_1/Cargo.toml b/bash-5.1/builtins_rust/break_1/Cargo.toml index 9871a473..085d195e 100644 --- a/bash-5.1/builtins_rust/break_1/Cargo.toml +++ b/bash-5.1/builtins_rust/break_1/Cargo.toml @@ -8,6 +8,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/break_1/src/lib.rs b/bash-5.1/builtins_rust/break_1/src/lib.rs index c7b6760d..f458c5a7 100644 --- a/bash-5.1/builtins_rust/break_1/src/lib.rs +++ b/bash-5.1/builtins_rust/break_1/src/lib.rs @@ -5,38 +5,14 @@ extern crate nix; use std::ffi::CString; use libc::c_long; -#[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}; + // 屏蔽警告。 #[allow(non_camel_case_types)] type intmax_t = c_long; -#[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! ISHELP { ($s:expr) => { @@ -49,7 +25,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; } } } diff --git a/bash-5.1/builtins_rust/builtin/Cargo.toml b/bash-5.1/builtins_rust/builtin/Cargo.toml index c09d9497..9301c7dc 100644 --- a/bash-5.1/builtins_rust/builtin/Cargo.toml +++ b/bash-5.1/builtins_rust/builtin/Cargo.toml @@ -11,4 +11,5 @@ crate-type = ["staticlib","rlib"] [dependencies] libc = "0.2" rcmd = {path="../cmd"} +rcommon = {path="../common"} diff --git a/bash-5.1/builtins_rust/builtin/src/intercdep.rs b/bash-5.1/builtins_rust/builtin/src/intercdep.rs index 661c3869..6882fb9e 100644 --- a/bash-5.1/builtins_rust/builtin/src/intercdep.rs +++ b/bash-5.1/builtins_rust/builtin/src/intercdep.rs @@ -1,24 +1,5 @@ -#[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 const EX_USAGE: c_int = 258; -pub const EXECUTION_SUCCESS :c_int = 0; -pub const EXECUTION_FAILURE :c_int = 1; - +use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; extern "C" { static mut loption :*mut WordList; diff --git a/bash-5.1/builtins_rust/builtin/src/lib.rs b/bash-5.1/builtins_rust/builtin/src/lib.rs index 71639d39..017a2a2e 100644 --- a/bash-5.1/builtins_rust/builtin/src/lib.rs +++ b/bash-5.1/builtins_rust/builtin/src/lib.rs @@ -12,13 +12,13 @@ pub extern "C" fn r_builtin_builtin(mut list: *mut WordList) -> i32 { } list = loptend; if list.is_null() { - return EXECUTION_SUCCESS; + return EXECUTION_SUCCESS!(); } function = find_shell_builtin(command.as_ptr() as *mut c_char); if function.is_none() { sh_notbuiltin(command.as_ptr() as *mut c_char); - return EXECUTION_FAILURE; + return EXECUTION_FAILURE!(); } else { this_command_name = command.as_ptr() as *mut c_char; this_shell_builtin = function; diff --git a/bash-5.1/builtins_rust/caller/Cargo.toml b/bash-5.1/builtins_rust/caller/Cargo.toml index ef4d55d0..fe503dc2 100644 --- a/bash-5.1/builtins_rust/caller/Cargo.toml +++ b/bash-5.1/builtins_rust/caller/Cargo.toml @@ -8,6 +8,7 @@ edition = "2018" [dependencies] libc = "0.2" rread = {path="../read"} +rcommon = {path="../common"} [lib] crate-type = ["staticlib","lib"] diff --git a/bash-5.1/builtins_rust/caller/src/lib.rs b/bash-5.1/builtins_rust/caller/src/lib.rs index 524ba95d..3209c7f5 100644 --- a/bash-5.1/builtins_rust/caller/src/lib.rs +++ b/bash-5.1/builtins_rust/caller/src/lib.rs @@ -1,52 +1,12 @@ extern crate libc; extern crate rread; +use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; use libc::{c_char,c_int,PT_NULL,c_long,}; use std::ffi::{CStr,CString}; use rread::{SHELL_VAR,ARRAY,intmax_t,}; -//struct -#[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; - - -//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! att_array { () => { diff --git a/bash-5.1/builtins_rust/cd/Cargo.toml b/bash-5.1/builtins_rust/cd/Cargo.toml index 22ac0616..56ccd745 100644 --- a/bash-5.1/builtins_rust/cd/Cargo.toml +++ b/bash-5.1/builtins_rust/cd/Cargo.toml @@ -9,6 +9,7 @@ build = "../build.rs" [dependencies] lazy_static = "1.4.0" libc = "0.2" +rcommon = {path="../common"} nix = "0.23.0" [lib] diff --git a/bash-5.1/builtins_rust/cd/src/lib.rs b/bash-5.1/builtins_rust/cd/src/lib.rs index 72ebf569..bd94b2ce 100644 --- a/bash-5.1/builtins_rust/cd/src/lib.rs +++ b/bash-5.1/builtins_rust/cd/src/lib.rs @@ -1,21 +1,10 @@ extern crate libc; extern crate nix; +use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; use libc::{c_char, c_long, c_void}; use std::{ffi::{CString, CStr}}; -#[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(i8)] pub enum JOB_STATE { @@ -246,20 +235,6 @@ pub struct SHELL_VAR { context:i32 /* Which context this variable belongs to. */ } -#[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! DUP_JOB { @@ -850,13 +825,3 @@ pub extern "C" fn r_change_to_directory (newdir:* mut c_char, nolinks:i32, xattr } } -/* -#[no_mangle] -pub extern "C" fn cmd_name() ->*const u8 { - return b"cd" as *const u8; -} -#[no_mangle] -pub extern "C" fn run(list : *mut WordList)->i32 { - return r_cd_builtin(list); -} -*/ diff --git a/bash-5.1/builtins_rust/colon/Cargo.toml b/bash-5.1/builtins_rust/colon/Cargo.toml index 3df1f22c..dea395cb 100644 --- a/bash-5.1/builtins_rust/colon/Cargo.toml +++ b/bash-5.1/builtins_rust/colon/Cargo.toml @@ -11,3 +11,4 @@ name = "rcolon" [dependencies] libc = "0.2" +rcommon = {path ="../common"} diff --git a/bash-5.1/builtins_rust/colon/src/lib.rs b/bash-5.1/builtins_rust/colon/src/lib.rs index faea3969..6ead6461 100644 --- a/bash-5.1/builtins_rust/colon/src/lib.rs +++ b/bash-5.1/builtins_rust/colon/src/lib.rs @@ -1,5 +1,6 @@ use std::{ffi::CString}; use libc::{size_t, c_int, c_uint, c_char, c_long, c_void, PT_NULL, c_ulong, strchr}; +use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; #[cfg(test)] mod tests { @@ -10,21 +11,6 @@ mod tests { } } -#[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; #[no_mangle] pub extern "C" fn r_colon_builtin(ignore:WordList)->i32 { diff --git a/bash-5.1/builtins_rust/command/Cargo.toml b/bash-5.1/builtins_rust/command/Cargo.toml index 13ff0ef0..0b76f4f5 100644 --- a/bash-5.1/builtins_rust/command/Cargo.toml +++ b/bash-5.1/builtins_rust/command/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" [dependencies] libc ="*" +rcommon = {path="../common"} [lib] crate-type = ["staticlib", "rlib"] diff --git a/bash-5.1/builtins_rust/command/src/lib.rs b/bash-5.1/builtins_rust/command/src/lib.rs index bf398967..c5d6d2b8 100644 --- a/bash-5.1/builtins_rust/command/src/lib.rs +++ b/bash-5.1/builtins_rust/command/src/lib.rs @@ -1,5 +1,6 @@ use std::ffi::*; +use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; extern "C" { fn copy_word_list(_: *mut WordList) -> *mut WordList; fn begin_unwind_frame(_: *mut libc::c_char); @@ -58,20 +59,6 @@ pub const cm_case: command_type = 1; pub const cm_for: command_type = 0; #[derive(Copy, Clone)] #[repr(C)] -pub struct word_desc { - pub word: *mut libc::c_char, - pub flags: libc::c_int, -} -pub type WordDesc = word_desc; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct word_list { - pub next: *mut word_list, - pub word: *mut WordDesc, -} -pub type WordList = word_list; -#[derive(Copy, Clone)] -#[repr(C)] pub union REDIRECTEE { pub dest: libc::c_int, pub filename: *mut WordDesc, @@ -259,9 +246,6 @@ pub const CDESC_STDPATH: i32 = 0x100; //#define CDESC_ABSPATH 0x080 /* convert to absolute path, no ./ */ //#define CDESC_STDPATH 0x100 /* command -p */ -pub const EX_USAGE :i32= 258; -pub const EXECUTION_SUCCESS:i32 = 0; -pub const EXECUTION_FAILURE:i32 = 1; pub const const_command_builtin:&CStr =unsafe{ CStr::from_bytes_with_nul_unchecked(b"command_builtin\0")};//.unwrap(); //#define COMMAND_BUILTIN_FLAGS (CMD_NO_FUNCTIONS | CMD_INHIBIT_EXPANSION | CMD_COMMAND_BUILTIN | (use_standard_path ? CMD_STDPATH : 0)) @@ -340,11 +324,11 @@ pub unsafe extern "C" fn command_builtin(mut list: *mut WordList) -> libc::c_int list = loptend; if list.is_null() { - return EXECUTION_SUCCESS; + return EXECUTION_SUCCESS!(); } if use_standard_path != 0 && restricted != 0 { sh_restricted(b"-p\0" as *const u8 as *const libc::c_char as *mut libc::c_char); - return EXECUTION_FAILURE; + return EXECUTION_FAILURE!(); } if verbose != 0 { let mut found: libc::c_int = 0; @@ -358,7 +342,7 @@ pub unsafe extern "C" fn command_builtin(mut list: *mut WordList) -> libc::c_int any_found += found; list = (*list).next; } - return if any_found != 0 { EXECUTION_SUCCESS } else { EXECUTION_FAILURE }; + return if any_found != 0 { EXECUTION_SUCCESS!() } else { EXECUTION_FAILURE!() }; } begin_unwind_frame( const_command_builtin.as_ptr() as *mut libc::c_char diff --git a/bash-5.1/builtins_rust/common/src/lib.rs b/bash-5.1/builtins_rust/common/src/lib.rs index 5a159f93..4a3a35e8 100644 --- a/bash-5.1/builtins_rust/common/src/lib.rs +++ b/bash-5.1/builtins_rust/common/src/lib.rs @@ -7,7 +7,7 @@ use std::mem::size_of; use std::ptr::read_volatile; use nix::errno::errno; - +include!(concat!("lib_readline_keymaps.rs")); pub static EXECUTION_SUCCESS:i32 = 0; //struct #[repr(C)] diff --git a/bash-5.1/builtins_rust/complete/Cargo.toml b/bash-5.1/builtins_rust/complete/Cargo.toml index e18cffe9..58cadc61 100644 --- a/bash-5.1/builtins_rust/complete/Cargo.toml +++ b/bash-5.1/builtins_rust/complete/Cargo.toml @@ -10,6 +10,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/complete/src/lib.rs b/bash-5.1/builtins_rust/complete/src/lib.rs index acfe6f50..cc1f2836 100644 --- a/bash-5.1/builtins_rust/complete/src/lib.rs +++ b/bash-5.1/builtins_rust/complete/src/lib.rs @@ -3,19 +3,8 @@ extern crate nix; use libc::{c_char, c_int, c_ulong, c_void}; use std::{ffi::CString}; +use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; -#[repr(C)] -pub struct WordDesc { - pub word: *mut c_char, - pub flags: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, @@ -319,22 +308,6 @@ pub struct STRINGLIST { list_len:c_int, } -#[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! CA_ALIAS { @@ -725,7 +698,7 @@ pub extern "C" fn r_build_actions (list : *mut WordList, flagp:* mut _optflags, } else { sh_invalidopt (CString::new("-r").unwrap().as_ptr() as * mut c_char); builtin_usage (); - return EX_USAGE!(); + return EX_USAGE; } } 'p'=>{ @@ -734,7 +707,7 @@ pub extern "C" fn r_build_actions (list : *mut WordList, flagp:* mut _optflags, } else { sh_invalidopt (CString::new("-p").unwrap().as_ptr() as * mut c_char); builtin_usage (); - return EX_USAGE!(); + return EX_USAGE; } } 'a'=>{ @@ -777,7 +750,7 @@ pub extern "C" fn r_build_actions (list : *mut WordList, flagp:* mut _optflags, ind = r_find_compopt (list_optarg); if ind < 0 { sh_invalidoptname (list_optarg); - return EX_USAGE!(); + return EX_USAGE; } let compopts:CompoptArray=CompoptArray::new(); copts |= compopts.compoptArr[ind as usize].optflag; @@ -786,7 +759,7 @@ pub extern "C" fn r_build_actions (list : *mut WordList, flagp:* mut _optflags, ind = r_find_compact (list_optarg); if ind < 0 { builtin_error (CString::new("%s: invalid action name").unwrap().as_ptr(), list_optarg); - return EX_USAGE!(); + return EX_USAGE; } let compacts:CompactsArray=CompactsArray::new(); acts |= compacts.compactsArr[ind as usize].actflag; @@ -800,7 +773,7 @@ pub extern "C" fn r_build_actions (list : *mut WordList, flagp:* mut _optflags, } else { sh_invalidopt (CString::new("-D").unwrap().as_ptr() as * mut c_char); builtin_usage (); - return EX_USAGE!(); + return EX_USAGE; } } 'E'=>{ @@ -809,7 +782,7 @@ pub extern "C" fn r_build_actions (list : *mut WordList, flagp:* mut _optflags, } else { sh_invalidopt (CString::new("-E").unwrap().as_ptr() as * mut c_char); builtin_usage (); - return EX_USAGE!(); + return EX_USAGE; } } 'I'=>{ @@ -818,7 +791,7 @@ pub extern "C" fn r_build_actions (list : *mut WordList, flagp:* mut _optflags, } else { sh_invalidopt (CString::new("-I").unwrap().as_ptr() as * mut c_char); builtin_usage (); - return EX_USAGE!(); + return EX_USAGE; } } 'F'=>{ @@ -827,7 +800,7 @@ pub extern "C" fn r_build_actions (list : *mut WordList, flagp:* mut _optflags, w.flags = 0; if check_identifier (&mut w, posixly_correct) == 0 || libc::strpbrk (Farg, shell_break_chars()) != std::ptr::null_mut() { sh_invalidid (Farg); - return EX_USAGE!(); + return EX_USAGE; } } 'G'=>{ @@ -850,7 +823,7 @@ pub extern "C" fn r_build_actions (list : *mut WordList, flagp:* mut _optflags, } _=>{ builtin_usage (); - return EX_USAGE!(); + return EX_USAGE; } } opt=internal_getopt(list, CString::new("abcdefgjko:prsuvA:G:W:P:S:X:F:C:DEI").unwrap().as_ptr() as * mut c_char); @@ -904,7 +877,7 @@ pub extern "C" fn r_complete_builtin (listt: *mut WordList)->i32 /* Build the actions from the arguments. Also sets the [A-Z]arg variables as a side effect if they are supplied as options. */ rval = r_build_actions (list, &mut oflags, &mut acts, &mut copts); - if rval == EX_USAGE!() { + if rval == EX_USAGE { return rval; } @@ -950,7 +923,7 @@ pub extern "C" fn r_complete_builtin (listt: *mut WordList)->i32 if wl == std::ptr::null_mut() && list == std::ptr::null_mut() && opt_given !=0 { builtin_usage (); - return EX_USAGE!(); + return EX_USAGE; } /* If we get here, we need to build a compspec and add it for each @@ -1197,7 +1170,7 @@ pub extern "C" fn r_compgen_builtin (listt:* mut WordList)->i32 /* Build the actions from the arguments. Also sets the [A-Z]arg variables as a side effect if they are supplied as options. */ rval = r_build_actions (list, std::ptr::null_mut(), &mut acts, &mut copts); - if rval == EX_USAGE!() { + if rval == EX_USAGE { return rval; } @@ -1314,7 +1287,7 @@ pub extern "C" fn r_compopt_builtin (listt:* mut WordList)->i32 oind = r_find_compopt (list_optarg); if oind < 0 { sh_invalidoptname (list_optarg); - return EX_USAGE!(); + return EX_USAGE; } let compopts:CompoptArray=CompoptArray::new(); *opts |= compopts.compoptArr[oind as usize].optflag as i32; @@ -1330,7 +1303,7 @@ pub extern "C" fn r_compopt_builtin (listt:* mut WordList)->i32 } _=>{ builtin_usage (); - return EX_USAGE!(); + return EX_USAGE; } } opt = internal_getopt (list, CString::new("+o:DEI").unwrap().as_ptr() as * mut c_char); @@ -1411,4 +1384,4 @@ pub extern "C" fn cmd_name() ->*const u8 { #[no_mangle] pub extern "C" fn run(list : *mut WordList)->i32 { return r_complete_builtin(list); -} \ No newline at end of file +} diff --git a/bash-5.1/builtins_rust/declare/src/lib.rs b/bash-5.1/builtins_rust/declare/src/lib.rs index b44473ab..225e7aa7 100644 --- a/bash-5.1/builtins_rust/declare/src/lib.rs +++ b/bash-5.1/builtins_rust/declare/src/lib.rs @@ -4,19 +4,8 @@ extern crate nix; use libc::{c_char, c_long, c_void}; use std::{ffi::CString}; // use rcommon::{r_sh_notfound,r_sh_invalidopt,r_sh_invalidid,r_sh_readonly,r_sh_chkwrite,}; +use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; -#[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, @@ -243,15 +232,6 @@ pub struct VAR_CONTEXT { table:* mut HASH_TABLE /* variables at this scope */ } -#[macro_export] -macro_rules! EXECUTION_FAILURE { - () => {1} -} - -#[macro_export] -macro_rules! EX_USAGE { - () => {258} -} #[macro_export] macro_rules! ARGS_SETBLTIN { @@ -358,12 +338,6 @@ macro_rules! MKLOC_INHERIT { } } -#[macro_export] -macro_rules! EXECUTION_SUCCESS { - () => { - 0 - } -} #[macro_export] macro_rules! ASS_APPEND { @@ -558,7 +532,7 @@ pub extern "C" fn r_local_builtin (list:* mut WordList)->i32 /* Catch a straight `local --help' before checking function context */ if list !=std::ptr::null_mut() && (*list).word != std::ptr::null_mut() && STREQ ((*(*list).word).word, CString::new("--help").unwrap().as_ptr()) { builtin_help (); - return EX_USAGE!(); + return EX_USAGE; } if variable_context !=0 { @@ -744,7 +718,7 @@ pub extern "C" fn r_declare_internal (list:* mut WordList, local_var:i32)->i32 } 'I'=>{ inherit_flag = MKLOC_INHERIT!();} _=>{ builtin_usage (); - return EX_USAGE!(); + return EX_USAGE; } } internal_getopt (list, DECLARE_OPTS() as * mut c_char); diff --git a/bash-5.1/builtins_rust/echo/Cargo.toml b/bash-5.1/builtins_rust/echo/Cargo.toml index 65f3bf2f..31720c0d 100644 --- a/bash-5.1/builtins_rust/echo/Cargo.toml +++ b/bash-5.1/builtins_rust/echo/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" [dependencies] libc = "0.2" +rcommon = {path="../common"} [lib] crate-type = ["staticlib","rlib"] diff --git a/bash-5.1/builtins_rust/echo/src/lib.rs b/bash-5.1/builtins_rust/echo/src/lib.rs index 5e69facd..79a74fe9 100644 --- a/bash-5.1/builtins_rust/echo/src/lib.rs +++ b/bash-5.1/builtins_rust/echo/src/lib.rs @@ -2,23 +2,11 @@ extern crate libc; use libc::{c_char,c_int, strchr, putchar,clearerr,free,FILE, fprintf, c_void}; use std::ffi::{CString,CStr,}; +use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; // use std::io::{stdout, Write}; use std::ptr::read_volatile; //结构体 -#[repr (C)] -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, -} - //枚举 @@ -43,11 +31,6 @@ macro_rules! QUIT { }; } -#[macro_export] -macro_rules! EXECUTION_SUCCESS { - () => {0} -} - unsafe fn STRLEN (s:*const c_char) -> i32{ diff --git a/bash-5.1/builtins_rust/enable/Cargo.toml b/bash-5.1/builtins_rust/enable/Cargo.toml index 1c2bc170..99eea4b4 100644 --- a/bash-5.1/builtins_rust/enable/Cargo.toml +++ b/bash-5.1/builtins_rust/enable/Cargo.toml @@ -12,3 +12,4 @@ crate-type = ["staticlib","rlib"] libc = "0.2" libloading = "0.7" rcmd = {path="../cmd"} +rcommon = {path="../common"} diff --git a/bash-5.1/builtins_rust/enable/src/lib.rs b/bash-5.1/builtins_rust/enable/src/lib.rs index 2df3366e..1edb8123 100644 --- a/bash-5.1/builtins_rust/enable/src/lib.rs +++ b/bash-5.1/builtins_rust/enable/src/lib.rs @@ -4,6 +4,7 @@ use libc::c_char; use std::path::Path; use libloading::Library; use rcmd::*; +use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; /* #define ENABLED 1 #define DISABLED 2 @@ -27,8 +28,6 @@ pub const NFLAG: i32 = 0x08; pub const PFLAG: i32 = 0x10; pub const SFLAG: i32 = 0x20; -pub const EX_USAGE: i32 = 258; -pub const EXECUTION_FAILURE:i32 = 1; // Flags describing various things about a builtin. //#define BUILTIN_ENABLED 0x01 /* This builtin is enabled. */ @@ -50,12 +49,6 @@ pub const POSIX_BUILTIN :i32 = 0x20; pub const LOCALVAR_BUILTIN :i32 = 0x40; pub const REQUIRES_BUILTIN :i32 = 0x80; -/* -#define EXECUTION_FAILURE 1 -#define EXECUTION_SUCCESS 0 - */ -//pub const EXECUTION_FAILURE :i32 = 1; -pub const EXECUTION_SUCCESS :i32 = 0; /* The MODE argument to `dlopen' contains one of the following: */ // #define RTLD_LAZY 0x00001 /* Lazy function call binding. */ @@ -128,20 +121,6 @@ extern "C" { fn dlerror() -> *mut libc::c_char; } pub type size_t = libc::c_ulong; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct word_desc { - pub word: *mut libc::c_char, - pub flags: libc::c_int, -} -pub type WordDesc = word_desc; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct word_list { - pub next: *mut word_list, - pub word: *mut WordDesc, -} -pub type WordList = word_list; pub type sh_load_func_t = unsafe extern "C" fn(*mut libc::c_char) -> libc::c_int; pub type sh_unload_func_t = unsafe extern "C" fn(*mut libc::c_char) -> (); pub type sh_builtin_func_t = unsafe extern "C" fn(*mut WordList) -> libc::c_int; @@ -222,7 +201,7 @@ pub unsafe extern "C" fn r_enable_builtin(mut list: *mut WordList) -> i32 { // 如果是严格模式,就直接返回EXECUTION_FAILURE,命令结束。 if restricted != 0 && flags & (FFLAG|DFLAG) != 0 { sh_restricted (0 as *mut c_char); - return EXECUTION_FAILURE; + return EXECUTION_FAILURE!(); } // 配置filter,通过flag和PFLAG,后者表示打印, @@ -270,8 +249,8 @@ pub unsafe extern "C" fn r_enable_builtin(mut list: *mut WordList) -> i32 { // 否则判断-D,-D含义是删除以 -f 选项加载的内建 while !list.is_null() { opt = dyn_unload_builtin((*(*list).word).word); - if opt == EXECUTION_FAILURE { - result = EXECUTION_FAILURE; + if opt == EXECUTION_FAILURE!() { + result = EXECUTION_FAILURE!(); } list = (*list).next; } @@ -281,9 +260,9 @@ pub unsafe extern "C" fn r_enable_builtin(mut list: *mut WordList) -> i32 { // 不带-N -F -D,且选项不为空的其他 while !list.is_null() { opt = enable_shell_command((*(*list).word).word, flags & NFLAG); - if opt == EXECUTION_FAILURE { + if opt == EXECUTION_FAILURE!() { sh_notbuiltin((*(*list).word).word); - result = EXECUTION_FAILURE; + result = EXECUTION_FAILURE!(); } list = (*list).next; } @@ -329,7 +308,7 @@ unsafe extern "C" fn enable_shell_command( let mut b: *mut builtin = 0 as *mut builtin; b = builtin_address_internal(name, 1); if b.is_null() { - return EXECUTION_FAILURE; + return EXECUTION_FAILURE!(); } if disable_p != 0 { (*b).flags &= !(BUILTIN_ENABLED); @@ -340,7 +319,7 @@ unsafe extern "C" fn enable_shell_command( } } else if restricted != 0 && (*b).flags & BUILTIN_ENABLED == 0 { sh_restricted(0 as *mut libc::c_void as *mut libc::c_char); - return EXECUTION_FAILURE; + return EXECUTION_FAILURE!(); } else { (*b).flags |= BUILTIN_ENABLED; if !set_cmd_enable(CStr::from_ptr(name).to_string_lossy().into_owned(), true) { @@ -350,7 +329,7 @@ unsafe extern "C" fn enable_shell_command( } set_itemlist_dirty(&mut it_enabled); set_itemlist_dirty(&mut it_disabled); - return EXECUTION_SUCCESS; + return EXECUTION_SUCCESS!(); } unsafe extern "C" fn dyn_load_builtin( mut list: *mut WordList, diff --git a/bash-5.1/builtins_rust/eval/src/lib.rs b/bash-5.1/builtins_rust/eval/src/lib.rs index 4d6609ef..33fcbb94 100644 --- a/bash-5.1/builtins_rust/eval/src/lib.rs +++ b/bash-5.1/builtins_rust/eval/src/lib.rs @@ -3,41 +3,14 @@ extern crate rcommon; use libc::{c_char,c_int}; use std::ffi::{CString}; -use rcommon::{r_no_options,WordList}; - -// #[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; - -#[macro_export] -macro_rules! EX_USAGE { - () => {258} -} +use rcommon::{r_no_options}; +use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; #[macro_export] macro_rules! SEVAL_NOHIST { () => {0x004} } -#[macro_export] -macro_rules! EXECUTION_SUCCESS { - () => { - 0 - } -} - extern "C" { static loptend:*mut WordList; @@ -53,7 +26,7 @@ pub extern "C" fn r_eval_builtin(mut list:*mut WordList)->i32{ unsafe{ if r_no_options(list) != 0{ - return EX_USAGE!(); + return EX_USAGE; } list = loptend; diff --git a/bash-5.1/builtins_rust/exit/Cargo.toml b/bash-5.1/builtins_rust/exit/Cargo.toml index 9971aed9..466a0f78 100644 --- a/bash-5.1/builtins_rust/exit/Cargo.toml +++ b/bash-5.1/builtins_rust/exit/Cargo.toml @@ -10,6 +10,7 @@ edition = "2018" libc = "0.2" nix = "0.23.0" rjobs = {path="../jobs"} +rcommon = {path="../common"} [lib] diff --git a/bash-5.1/builtins_rust/exit/src/lib.rs b/bash-5.1/builtins_rust/exit/src/lib.rs index ac3f9255..3fe496c9 100644 --- a/bash-5.1/builtins_rust/exit/src/lib.rs +++ b/bash-5.1/builtins_rust/exit/src/lib.rs @@ -5,7 +5,8 @@ extern crate nix; use libc::c_char; use std::ffi::CString; -use rjobs::{PROCESS, COMMAND, r_jobs_builtin, JLIST_STANDARD, WordList}; +use rjobs::{PROCESS, COMMAND, r_jobs_builtin, JLIST_STANDARD }; +use rcommon::{ WordList}; #[repr(C)] pub struct JOB { diff --git a/record.txt b/record.txt index d8c40484..45df0a3c 100644 --- a/record.txt +++ b/record.txt @@ -26,3 +26,4 @@ 48 49 50 +51 -- Gitee From 7a4f2e64c893b98ab27e7d64c5a356aa03971ea0 Mon Sep 17 00:00:00 2001 From: liutong Date: Tue, 27 Jun 2023 21:48:41 +0800 Subject: [PATCH 3/6] 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/lib.rs | 41 +--- 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 + 46 files changed, 261 insertions(+), 823 deletions(-) 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/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/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 45df0a3c..f60a51a0 100644 --- a/record.txt +++ b/record.txt @@ -27,3 +27,4 @@ 49 50 51 +52 -- Gitee From c794fc9c6ee7a51c708502a1d105615ec942a238 Mon Sep 17 00:00:00 2001 From: liutong Date: Tue, 27 Jun 2023 21:59:33 +0800 Subject: [PATCH 4/6] fix history can not -d error --- bash-5.1/Cargo.toml | 2 + bash-5.1/Makefile.in | 4 +- bash-5.1/builtins/command.def | 2 + bash-5.1/builtins/complete.def | 2 + bash-5.1/builtins/declare.def | 5 +- bash-5.1/builtins/pushd.def | 11 ++- bash-5.1/builtins/source.def | 6 +- bash-5.1/builtins_rust/command/src/lib.rs | 2 +- bash-5.1/builtins_rust/complete/src/lib.rs | 9 --- bash-5.1/builtins_rust/declare/src/lib.rs | 10 --- bash-5.1/builtins_rust/fc/src/lib.rs | 2 - bash-5.1/builtins_rust/hash/src/lib.rs | 4 +- bash-5.1/builtins_rust/help/src/lib.rs | 10 ++- .../builtins_rust/history/src/intercdep.rs | 3 +- bash-5.1/builtins_rust/history/src/lib.rs | 74 +++++++++++-------- bash-5.1/builtins_rust/jobs/src/lib.rs | 7 -- .../builtins_rust/mapfile/src/intercdep.rs | 17 +---- .../builtins_rust/printf/src/intercdep.rs | 18 +---- bash-5.1/builtins_rust/pushd/src/lib.rs | 9 --- bash-5.1/builtins_rust/read/src/intercdep.rs | 17 +---- bash-5.1/builtins_rust/rlet/Cargo.toml | 1 + bash-5.1/builtins_rust/rlet/src/intercdep.rs | 17 +---- 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/source/src/lib.rs | 9 --- bash-5.1/builtins_rust/times/src/intercdep.rs | 17 +---- record.txt | 1 + 28 files changed, 89 insertions(+), 198 deletions(-) diff --git a/bash-5.1/Cargo.toml b/bash-5.1/Cargo.toml index 595ff04a..0c967670 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 019ead7c..30ddc175 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 -lrcommand -lrcomplete -lrdeclare -lrecho -lrenable -lreval -lrexec -lrexit -lrfc -lrfg_bg -lrgetopts -lrhash -lrexit -lrhelp -lrhistory -lrjobs -lrkill -lrmapfile -lrpushd -lrread -lrlet -lrreturn -lrset -lrsetattr -lrshift -lrshopt -lrsource -lrsuspend -lrtest -lrtimes -lrtrap -lrtype -lrulimit -lrumask -lrwait -lrprintf -lrexec_cmd LIBS_FOR_BUILD = STATIC_LD = @STATIC_LD@ @@ -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/command.def b/bash-5.1/builtins/command.def index acd46cc1..f538122d 100644 --- a/bash-5.1/builtins/command.def +++ b/bash-5.1/builtins/command.def @@ -60,12 +60,14 @@ $END extern size_t confstr PARAMS((int, char *, size_t)); #endif +int r_command_builtin ( WORD_LIST *list); /* Run the commands mentioned in LIST without paying attention to shell functions. */ int command_builtin (list) WORD_LIST *list; { +return r_command_builtin ( list); int result, verbose, use_standard_path, opt; COMMAND *command; diff --git a/bash-5.1/builtins/complete.def b/bash-5.1/builtins/complete.def index 28a9ec2f..97334197 100644 --- a/bash-5.1/builtins/complete.def +++ b/bash-5.1/builtins/complete.def @@ -365,11 +365,13 @@ build_actions (list, flagp, actp, optp) return (opt_given ? EXECUTION_SUCCESS : EXECUTION_FAILURE); } +int r_complete_builtin ( WORD_LIST *list); /* Add, remove, and display completion specifiers. */ int complete_builtin (list) WORD_LIST *list; { +return r_complete_builtin (list); int opt_given, rval; unsigned long acts, copts; COMPSPEC *cs; diff --git a/bash-5.1/builtins/declare.def b/bash-5.1/builtins/declare.def index 21e4516d..aee4afb1 100644 --- a/bash-5.1/builtins/declare.def +++ b/bash-5.1/builtins/declare.def @@ -92,13 +92,14 @@ $END static SHELL_VAR *declare_find_variable PARAMS((const char *, int, int)); static int declare_internal PARAMS((register WORD_LIST *, int)); +int r_declare_internal PARAMS((register WORD_LIST *, int)); /* Declare or change variable attributes. */ int declare_builtin (list) register WORD_LIST *list; { - return (declare_internal (list, 0)); + return (r_declare_internal (list, 0)); } $BUILTIN local @@ -128,7 +129,7 @@ local_builtin (list) } if (variable_context) - return (declare_internal (list, 1)); + return (r_declare_internal (list, 1)); else { builtin_error (_("can only be used in a function")); diff --git a/bash-5.1/builtins/pushd.def b/bash-5.1/builtins/pushd.def index 829f827d..de1bb2df 100644 --- a/bash-5.1/builtins/pushd.def +++ b/bash-5.1/builtins/pushd.def @@ -169,10 +169,13 @@ static int get_dirstack_index PARAMS((intmax_t, int, int *)); #define LONGFORM 0x04 #define CLEARSTAK 0x08 +int r_pushd_builtin ( WORD_LIST *list); + int pushd_builtin (list) WORD_LIST *list; { +return r_pushd_builtin(list); WORD_LIST *orig_list; char *temp, *current_directory, *top; int j, flags, skipopt; @@ -312,13 +315,15 @@ pushd_builtin (list) } } +int +r_popd_builtin ( + WORD_LIST *list); /* Pop the directory stack, and then change to the new top of the stack. If LIST is non-null it should consist of a word +N or -N, which says what element to delete from the stack. The default is the top one. */ -int -popd_builtin (list) - WORD_LIST *list; +int popd_builtin ( WORD_LIST *list) { +return r_popd_builtin(list); register int i; intmax_t which; int flags; diff --git a/bash-5.1/builtins/source.def b/bash-5.1/builtins/source.def index 5b2f994d..930d1a88 100644 --- a/bash-5.1/builtins/source.def +++ b/bash-5.1/builtins/source.def @@ -108,14 +108,14 @@ maybe_pop_dollar_vars () invalidate_cached_quoted_dollar_at (); /* just invalidate to be safe */ } +int r_source_builtin ( WORD_LIST *list); /* Read and execute commands from the file passed as argument. Guess what. This cannot be done in a subshell, since things like variable assignments take place in there. So, I open the file, place it into a large string, close the file, and then execute the string. */ -int -source_builtin (list) - WORD_LIST *list; +int source_builtin ( WORD_LIST *list) { +return r_source_builtin (list); int result; char *filename, *debug_trap, *x; diff --git a/bash-5.1/builtins_rust/command/src/lib.rs b/bash-5.1/builtins_rust/command/src/lib.rs index c5d6d2b8..b7f593de 100644 --- a/bash-5.1/builtins_rust/command/src/lib.rs +++ b/bash-5.1/builtins_rust/command/src/lib.rs @@ -284,7 +284,7 @@ pub const CMD_STDPATH :i32 = 0x4000; pub const CMD_TRY_OPTIMIZING :i32 = 0x8000; #[no_mangle] -pub unsafe extern "C" fn command_builtin(mut list: *mut WordList) -> libc::c_int { +pub unsafe extern "C" fn r_command_builtin(mut list: *mut WordList) -> libc::c_int { let mut result: libc::c_int = 0; let mut verbose: libc::c_int = 0; let mut use_standard_path: libc::c_int = 0; diff --git a/bash-5.1/builtins_rust/complete/src/lib.rs b/bash-5.1/builtins_rust/complete/src/lib.rs index cc1f2836..9dd58df4 100644 --- a/bash-5.1/builtins_rust/complete/src/lib.rs +++ b/bash-5.1/builtins_rust/complete/src/lib.rs @@ -1376,12 +1376,3 @@ pub extern "C" fn r_compopt_builtin (listt:* mut WordList)->i32 } } -#[no_mangle] -pub extern "C" fn cmd_name() ->*const u8 { - return b"complete" as *const u8; -} - -#[no_mangle] -pub extern "C" fn run(list : *mut WordList)->i32 { - return r_complete_builtin(list); -} diff --git a/bash-5.1/builtins_rust/declare/src/lib.rs b/bash-5.1/builtins_rust/declare/src/lib.rs index 225e7aa7..eede3a82 100644 --- a/bash-5.1/builtins_rust/declare/src/lib.rs +++ b/bash-5.1/builtins_rust/declare/src/lib.rs @@ -3,7 +3,6 @@ extern crate nix; use libc::{c_char, c_long, c_void}; use std::{ffi::CString}; -// use rcommon::{r_sh_notfound,r_sh_invalidopt,r_sh_invalidid,r_sh_readonly,r_sh_chkwrite,}; use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE}; @@ -1516,12 +1515,3 @@ pub extern "C" fn r_declare_internal (list:* mut WordList, local_var:i32)->i32 } } -#[no_mangle] -pub extern "C" fn cmd_name() ->*const u8 { - return b"declare" as *const u8; -} - -#[no_mangle] -pub extern "C" fn run(list : *mut WordList)->i32 { - return r_declare_builtin(list); -} diff --git a/bash-5.1/builtins_rust/fc/src/lib.rs b/bash-5.1/builtins_rust/fc/src/lib.rs index 47d9e0be..5d0eb3fc 100644 --- a/bash-5.1/builtins_rust/fc/src/lib.rs +++ b/bash-5.1/builtins_rust/fc/src/lib.rs @@ -1,10 +1,8 @@ extern crate libc; extern crate nix; -//use rcommon::{ EX_USAGE,EXECUTION_FAILURE, EXECUTION_SUCCESS }; use libc::{c_char, c_long, c_void}; use std::{ffi::CString, i32, io::{Write, stdout}, ops::Add, string, u32}; - use rcommon::{WordList, WordDesc, EX_USAGE, EXECUTION_SUCCESS, EXECUTION_FAILURE,r_builtin_usage}; #[repr(i8)] diff --git a/bash-5.1/builtins_rust/hash/src/lib.rs b/bash-5.1/builtins_rust/hash/src/lib.rs index 88cc8f5e..698a3f95 100644 --- a/bash-5.1/builtins_rust/hash/src/lib.rs +++ b/bash-5.1/builtins_rust/hash/src/lib.rs @@ -197,7 +197,6 @@ pub extern "C" fn r_hash_builtin(mut list:*mut WordList)->i32{ } } - opt = internal_getopt(list,opts.as_ptr() as *mut c_char); } @@ -210,12 +209,13 @@ pub extern "C" fn r_hash_builtin(mut list:*mut WordList)->i32{ if delete != 0{ temp = CString::new("-d").unwrap(); temp_ptr = temp.as_ptr() as *mut c_char; + sh_needarg(temp_ptr); } else{ temp = CString::new("-t").unwrap(); temp_ptr = temp.as_ptr() as *mut c_char; + sh_needarg(temp_ptr); } - sh_needarg(temp_ptr); return EXECUTION_FAILURE!(); } diff --git a/bash-5.1/builtins_rust/help/src/lib.rs b/bash-5.1/builtins_rust/help/src/lib.rs index 28321371..16bedff4 100644 --- a/bash-5.1/builtins_rust/help/src/lib.rs +++ b/bash-5.1/builtins_rust/help/src/lib.rs @@ -95,6 +95,7 @@ extern "C"{ static mut static_shell_builtin : [builtin ; 100]; static shell_builtins:*mut builtin; static mut current_builtin :*mut builtin; + fn builtin_help(); } #[no_mangle] @@ -129,9 +130,12 @@ pub extern "C" fn r_help_builtin(mut list:*mut WordList)->i32 { 's'=> {sflag = 1; break;} _=>{ unsafe { - - builtin_usage (); - return EX_USAGE; + if i == -99 { + builtin_help(); + return EX_USAGE; + } + builtin_usage (); + return EX_USAGE; } } } diff --git a/bash-5.1/builtins_rust/history/src/intercdep.rs b/bash-5.1/builtins_rust/history/src/intercdep.rs index 5ea73daf..a1c6d3d1 100644 --- a/bash-5.1/builtins_rust/history/src/intercdep.rs +++ b/bash-5.1/builtins_rust/history/src/intercdep.rs @@ -31,6 +31,7 @@ pub struct _hist_entry { pub type HIST_ENTRY = _hist_entry; extern "C" { + pub fn printf(_: *const libc::c_char, _: ...) -> libc::c_int; pub fn reset_internal_getopt(); pub fn internal_getopt(list: *mut WordList, opts: *mut c_char) -> c_int; pub fn builtin_usage(); @@ -67,7 +68,7 @@ extern "C" { pub fn sh_erange(s: *mut c_char, desc: *mut c_char); pub fn sh_restricted(s: *mut c_char) -> c_void; - + fn builtin_help(); pub static mut list_optarg : *mut libc::c_char; pub static mut loptend : *mut WordList; diff --git a/bash-5.1/builtins_rust/history/src/lib.rs b/bash-5.1/builtins_rust/history/src/lib.rs index 520f0cfc..37caa346 100644 --- a/bash-5.1/builtins_rust/history/src/lib.rs +++ b/bash-5.1/builtins_rust/history/src/lib.rs @@ -47,6 +47,10 @@ unsafe { } 'p' => flags |= PFLAG, _ => { + if opt == -99 { + builtin_help(); + return EX_USAGE; + } r_builtin_usage (); return EX_USAGE; } @@ -55,7 +59,6 @@ unsafe { } list = loptend; - opt = flags & (AFLAG | RFLAG | WFLAG | NFLAG); if opt != 0 && opt != AFLAG && opt != RFLAG && opt != WFLAG && opt != NFLAG { let c_err = CString::new("cannot use more than one of -anrw").unwrap(); @@ -81,11 +84,15 @@ unsafe { } return r_sh_chkwrite(EXECUTION_SUCCESS); } else if (flags & DFLAG) != 0 { - let c_tmp = if *delete_arg == b'-' as c_char {(delete_arg as usize + 1) as *mut c_char} else {delete_arg}; + let c_tmp = if *delete_arg == b'-' as c_char {delete_arg.offset(1 as isize ) as *mut c_char} else {delete_arg}; range = libc::strchr(c_tmp, b'-' as c_int); + + printf(b"AAAAAAArange=%u, c_tmp=%s\n" as *const u8 as *const i8, range, c_tmp); if !range.is_null() { - let mut delete_start: c_long = 0; - let mut delete_end: c_long = 0; + printf(b"AAAAAAArange=%s\n" as *const u8 as *const i8, range); + + let mut delete_start: c_long = 0; + let mut delete_end: c_long = 0; *range = b'\0' as c_char; range = (range as usize + 1) as *mut c_char; @@ -129,8 +136,8 @@ unsafe { return if result != 0 {EXECUTION_SUCCESS} else {EXECUTION_FAILURE}; } - } else if (flags & DFLAG) != 0 { - if legal_number(delete_arg, std::mem::transmute(&delete_offset)) == 0 { + else if (flags & DFLAG) != 0 { + if legal_number(delete_arg, &mut delete_offset) == 0 { r_sh_erange(delete_arg, "history position\0".as_ptr() as *mut c_char); return EXECUTION_FAILURE; } @@ -149,13 +156,13 @@ unsafe { } else { opt = delete_offset as c_int; } - result = bash_delete_histent(opt - history_base); if where_history() > history_length { history_set_pos(history_length); } return if result != 0 {EXECUTION_FAILURE} else {EXECUTION_SUCCESS}; - } else if (flags & (AFLAG | RFLAG | NFLAG | WFLAG | CFLAG)) == 0 { + } +} else if (flags & (AFLAG | RFLAG | NFLAG | WFLAG | CFLAG)) == 0 { result = display_history(list); return r_sh_chkwrite(result); } @@ -167,7 +174,6 @@ unsafe { r_sh_restricted(filename); return EXECUTION_FAILURE; } - if (flags & AFLAG) != 0 { result = maybe_append_history(filename); } else if (flags & WFLAG) != 0 { @@ -245,37 +251,45 @@ unsafe fn display_history(list: *mut WordList) -> c_int } else { limit = -1; } - let hlist = history_list(); if !hlist.is_null() { let mut i: c_long = 0; - while !((hlist as usize + (i * 8) as usize) as *mut HIST_ENTRY).is_null() { + while !(*hlist.offset(i as isize)).is_null() { i += 1; } i = if 0 <= limit && limit < i {i - limit} else {0}; - histtimefmt = get_string_value("HISTTIMEFORMAT\0".as_ptr() as *const c_char); - - while !((hlist as usize + (i * 8) as usize) as *mut HIST_ENTRY).is_null(){ - quit(); + histtimefmt = get_string_value(b"HISTTIMEFORMAT\0" as *const u8 as *const c_char); - if !histtimefmt.is_null() && *histtimefmt != 0 { - timestr = histtime((hlist as usize + (i * 8) as usize) as *mut HIST_ENTRY, histtimefmt); - } else { - timestr = PT_NULL as *mut c_char; + while !(*hlist.offset(i as isize)).is_null() { + if terminating_signal != 0 { + termsig_handler(terminating_signal); } - - let data = (*((hlist as usize + (i * 8) as usize) as *mut HIST_ENTRY)).data; - let line = (*((hlist as usize + (i * 8) as usize) as *mut HIST_ENTRY)).line; - let s_timestr = CString::from_raw(timestr).into_string().unwrap(); - println!("{:>5}{} {}{}", - i as c_int + history_base, - if !data.is_null() {"*"} else {" "}, - if !timestr.is_null() && *timestr != 0 {s_timestr} else {"".to_owned()}, - CString::from_raw(line).into_string().unwrap() - ); + if interrupt_state != 0 { + throw_to_top_level(); + } + timestr = if !histtimefmt.is_null() && *histtimefmt as libc::c_int != 0 { + histtime(*hlist.offset(i as isize), histtimefmt) + } else { + 0 as *mut libc::c_void as *mut libc::c_char + }; + printf( + b"%5d%c %s%s\n\0" as *const u8 as *const libc::c_char, + i + history_base as c_long, + if !((**hlist.offset(i as isize)).data).is_null() { + '*' as i32 + } else { + ' ' as i32 + }, + if !timestr.is_null() && *timestr as libc::c_int != 0 { + timestr + } else { + b"\0" as *const u8 as *const libc::c_char + }, + (**hlist.offset(i as isize)).line, + ); i += 1; } } @@ -284,10 +298,12 @@ unsafe fn display_history(list: *mut WordList) -> c_int } fn push_history(list: *mut WordList) { + println!("push_history!!!!"); unsafe { if remember_on_history != 0 && hist_last_line_pushed == 0 && (hist_last_line_added != 0 || (current_command_line_count > 0 && current_command_first_line_saved != 0 && command_oriented_history != 0)) && bash_delete_last_history() == 0 { + return; } diff --git a/bash-5.1/builtins_rust/jobs/src/lib.rs b/bash-5.1/builtins_rust/jobs/src/lib.rs index ce7f97b6..8f70ce26 100644 --- a/bash-5.1/builtins_rust/jobs/src/lib.rs +++ b/bash-5.1/builtins_rust/jobs/src/lib.rs @@ -601,10 +601,3 @@ pub extern "C" fn r_disown_builtin (list:* mut WordList)->libc::c_int { pub extern "C" fn cmd_name() ->*const u8 { return b"jobs" as *const u8; } -/* -#[no_mangle] - -pub extern "C" fn run(list : *mut WordList)->i32 { - return r_jobs_builtin(list); -} -*/ diff --git a/bash-5.1/builtins_rust/mapfile/src/intercdep.rs b/bash-5.1/builtins_rust/mapfile/src/intercdep.rs index f439e7b6..8450248d 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; diff --git a/bash-5.1/builtins_rust/printf/src/intercdep.rs b/bash-5.1/builtins_rust/printf/src/intercdep.rs index e5ab6fa7..db66567d 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; diff --git a/bash-5.1/builtins_rust/pushd/src/lib.rs b/bash-5.1/builtins_rust/pushd/src/lib.rs index b8bfcb04..08d04fa5 100644 --- a/bash-5.1/builtins_rust/pushd/src/lib.rs +++ b/bash-5.1/builtins_rust/pushd/src/lib.rs @@ -935,12 +935,3 @@ pub extern "C" fn r_get_directory_stack (flags:i32)->* mut WordList } } -#[no_mangle] -pub extern "C" fn cmd_name() ->*const u8 { - return b"pushd" as *const u8; -} - -#[no_mangle] -pub extern "C" fn run(list : *mut WordList)->i32 { - return r_pushd_builtin(list); -} diff --git a/bash-5.1/builtins_rust/read/src/intercdep.rs b/bash-5.1/builtins_rust/read/src/intercdep.rs index 2fed8da3..60570c20 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/rlet/Cargo.toml b/bash-5.1/builtins_rust/rlet/Cargo.toml index c468265b..b52101ca 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 fda0e61a..d37ae34b 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/setattr/Cargo.toml b/bash-5.1/builtins_rust/setattr/Cargo.toml index 1e1d381e..90004e4d 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 98a323e0..8c465593 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 5e0851b5..7130cc61 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/source/src/lib.rs b/bash-5.1/builtins_rust/source/src/lib.rs index fcbbf1e0..a33457a4 100644 --- a/bash-5.1/builtins_rust/source/src/lib.rs +++ b/bash-5.1/builtins_rust/source/src/lib.rs @@ -386,12 +386,3 @@ pub extern "C" fn r_source_builtin (list:* mut WordList)->i32 } } -#[no_mangle] -pub extern "C" fn cmd_name() ->*const u8 { - return b"source" as *const u8; -} - -#[no_mangle] -pub extern "C" fn run(list : *mut WordList)->i32 { - return r_source_builtin(list); -} diff --git a/bash-5.1/builtins_rust/times/src/intercdep.rs b/bash-5.1/builtins_rust/times/src/intercdep.rs index eb18f382..c2f5d321 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/record.txt b/record.txt index f60a51a0..a681e558 100644 --- a/record.txt +++ b/record.txt @@ -28,3 +28,4 @@ 50 51 52 +53 -- Gitee From a79fd960c49d0382191566d4d572827eecd12ffc Mon Sep 17 00:00:00 2001 From: liutong Date: Tue, 27 Jun 2023 22:09:44 +0800 Subject: [PATCH 5/6] use static mut var --- bash-5.1/builtins/break.def | 25 +- bash-5.1/builtins_rust/break_1/src/lib.rs | 38 +- bash-5.1/builtins_rust/read/src/intercdep.rs | 15 +- bash-5.1/builtins_rust/read/src/lib.rs | 394 +++++++++++++------ record.txt | 1 + 5 files changed, 309 insertions(+), 164 deletions(-) diff --git a/bash-5.1/builtins/break.def b/bash-5.1/builtins/break.def index 346b632d..f71bc0de 100644 --- a/bash-5.1/builtins/break.def +++ b/bash-5.1/builtins/break.def @@ -144,27 +144,4 @@ check_loop_level () return (loop_level); } -extern void set_loop_level(int i) -{ - loop_level = i; -} -extern int get_loop_level(void ) -{ - return loop_level; -} -extern void set_continuing(int i) -{ - continuing = i; -} -extern int get_continuing() -{ - return continuing; -} -extern void set_breaking(int i) -{ - breaking = i ; -} -extern int get_breaking() -{ - return breaking; -} + diff --git a/bash-5.1/builtins_rust/break_1/src/lib.rs b/bash-5.1/builtins_rust/break_1/src/lib.rs index f458c5a7..a2f0a636 100644 --- a/bash-5.1/builtins_rust/break_1/src/lib.rs +++ b/bash-5.1/builtins_rust/break_1/src/lib.rs @@ -33,13 +33,16 @@ macro_rules! CHECK_HELPOPT { extern "C" { fn get_numeric_arg(list :*mut WordList, i: i32 , intmax :*mut intmax_t) -> i32; fn builtin_help (); - fn get_loop_level() -> i32; - fn set_continuing(cont : i32); - fn set_breaking(breaking : i32); + // fn get_loop_level() -> i32; + //fn set_continuing(cont : i32); + //fn set_breaking(breaking : i32); fn sh_erange (s:* mut libc::c_char, desc:* mut libc::c_char); //pub static fn check_loop_level () -> i64; /* Non-zero when a "break" instruction is encountered. */ pub static posixly_correct :i32; + static mut breaking : i32; + static mut continuing : i32; + static mut loop_level : i32; } #[no_mangle] @@ -49,22 +52,23 @@ pub extern "C" fn r_break_builtin(mut list :*mut WordList) -> i32 { unsafe { CHECK_HELPOPT! (list); if check_loop_level() == 0 { - return (EXECUTION_SUCCESS!()); + return EXECUTION_SUCCESS!(); } get_numeric_arg(list, 1, &mut newbreak as *mut intmax_t); if newbreak <= 0{ #[warn(temporary_cstring_as_ptr)] sh_erange ((*(*list).word).word, CString::new("loop count").unwrap().as_ptr() as * mut libc::c_char); - set_breaking (get_loop_level()); - return (EXECUTION_FAILURE!()); + //set_breaking (get_loop_level()); + breaking = loop_level; + return EXECUTION_FAILURE!(); } - if newbreak > get_loop_level() as libc::c_long{ - newbreak = get_loop_level() as i64; + if newbreak > loop_level as libc::c_long{ + newbreak = loop_level as i64; } - - set_breaking(newbreak as i32); + breaking = newbreak as i32; + // set_breaking(newbreak as i32); } return (EXECUTION_SUCCESS!()); } @@ -84,13 +88,15 @@ fn continue_builtin (list :*mut WordList) -> i32 { if newcont <= 0{ #[warn(temporary_cstring_as_ptr)] sh_erange ((*(*list).word).word, CString::new("loop count ").unwrap().as_ptr() as * mut libc::c_char); - set_breaking(get_loop_level()); + //set_breaking(get_loop_level()); + breaking = loop_level; return (EXECUTION_FAILURE!()); } - if newcont > get_loop_level().into(){ - newcont = get_loop_level() as i64; + if newcont > loop_level.into(){ + newcont = loop_level as i64; } - set_continuing(newcont as i32); + continuing = newcont as i32; + //set_continuing(newcont as i32); } return (EXECUTION_SUCCESS!()); @@ -99,11 +105,11 @@ fn continue_builtin (list :*mut WordList) -> i32 { #[no_mangle] pub extern "C" fn check_loop_level () -> i32 { unsafe { - if get_loop_level()== 0 && posixly_correct == 0 { + if loop_level == 0 && posixly_correct == 0 { println! ("only meaningful in a `for`, `while`, or until `loop` "); return 0; } - return (get_loop_level()); + loop_level } } diff --git a/bash-5.1/builtins_rust/read/src/intercdep.rs b/bash-5.1/builtins_rust/read/src/intercdep.rs index 60570c20..4ea450bc 100644 --- a/bash-5.1/builtins_rust/read/src/intercdep.rs +++ b/bash-5.1/builtins_rust/read/src/intercdep.rs @@ -1,7 +1,4 @@ -use rcommon::{WordList}; - -use rcommon::{r_builtin_usage,r_sh_invalidid,r_builtin_bind_variable}; -//use rcommon::{r_builtin_usage,r_sh_invalidid,r_builtin_bind_variable,SHELL_VAR}; +use rcommon::{r_builtin_usage,r_sh_invalidid,r_builtin_bind_variable,WordList,WordDesc}; pub type SHELL_VAR = rcommon::SHELL_VAR; pub type __intmax_t = c_long; @@ -118,7 +115,7 @@ extern "C" { pub static terminating_signal : c_int; pub static trapped_signal_received : c_int; - pub static mut alrmbuf: sigjmp_buf; + // pub static mut alrmbuf: sigjmp_buf; pub static mut rl_instream: *mut libc::FILE; @@ -306,3 +303,11 @@ pub struct array_element { pub prev: *mut array_element, } pub type ARRAY_ELEMENT = array_element; + + +extern "C" { + static is_basic_table:[libc::c_uint;0]; + static mut sigalrm_seen:libc::c_int; + // static mut tty_modified:libc::c_int ; + // fn is_basic(c:libc::c_char)->libc::c_int; +} diff --git a/bash-5.1/builtins_rust/read/src/lib.rs b/bash-5.1/builtins_rust/read/src/lib.rs index 720c6228..946f8f9c 100644 --- a/bash-5.1/builtins_rust/read/src/lib.rs +++ b/bash-5.1/builtins_rust/read/src/lib.rs @@ -1,13 +1,20 @@ use libc::{c_int, c_char, c_long, c_ulong, c_uint, size_t, c_void, PT_NULL, ssize_t}; use nix::errno::errno; -use std::{ffi::{CString, CStr}, ptr::null_mut}; +use std::{ffi::{CString, CStr}, ptr::null_mut,}; //use rcommon::{r_builtin_usage,r_sh_invalidid,r_builtin_bind_variable,SHELL_VAR}; include!(concat!("intercdep.rs")); +#[no_mangle] +pub static mut alrmbuf:sigjmp_buf = [__jmp_buf_tag{ + __jmpbuf:[0;8], + __mask_was_saved:0, + __saved_mask:__sigset_t{__val:[0;16]}, +};1]; + static mut old_alrm : *mut SigHandler = PT_NULL as *mut SigHandler; -static mut sigalrm_seen : c_int = 0; +// static mut sigalrm_seen : c_int = 0; static mut reading : c_int = 0; static mut tty_modified : c_int = 0; @@ -18,16 +25,28 @@ pub struct tty_save { attrs: libc::termios, } -static mut termsave : Option = None; -static mut ptermsave: *mut c_void = PT_NULL as *mut c_void; +// static mut termsave : Option = None; +static mut termsave:tty_save = tty_save{ + fd:0, + attrs:libc::termios { + c_iflag: (0), + c_oflag: (0), + c_cflag: (0), + c_lflag: (0), + c_line: (0), + c_cc: [0;32], + c_ispeed: (0), + c_ospeed: (0) + } +}; static mut interactive : c_int = 0; static mut default_buffered_input : c_int = -1; + #[no_mangle] pub extern "C" fn r_read_builtin(mut list: *mut WordList) -> i32 { println!("r_read_builtin call"); - println!("11111111111111"); let mut varname :*mut c_char = libc::PT_NULL as *mut c_char; let mut size : c_int = 0; @@ -110,11 +129,11 @@ pub extern "C" fn r_read_builtin(mut list: *mut WordList) -> i32 { unsafe { - if termsave.is_none() { - let tmp: tty_save = std::mem::zeroed(); - termsave = Some(tmp); - } - ptermsave = std::mem::transmute(&termsave.unwrap()); + // if termsave.is_none() { + // let tmp: tty_save = std::mem::zeroed(); + // termsave = Some(tmp); + // } + // ptermsave = std::mem::transmute(&termsave.unwrap()); reset_internal_getopt(); let opt_str = CString::new("ersa:d:i:n:p:t:u:N:").unwrap(); @@ -169,9 +188,12 @@ unsafe { return EXECUTION_FAILURE; } } - 'd' => {delim = *list_optarg; - println!("-d");} + 'd' => { + delim = *list_optarg; + } + _ => { + // builtin_usage(); r_builtin_usage (); return EX_USAGE; } @@ -181,32 +203,38 @@ unsafe { list = loptend; - if have_timeout == 1 && tmsec == 0 && tmusec == 0 { + //-t + if have_timeout != 0 && tmsec == 0 && tmusec == 0 { return if input_avail(fd) != 0 {EXECUTION_SUCCESS} else {EXECUTION_FAILURE}; } vflags = if assoc_expand_once != 0 {(VA_NOEXPAND | VA_ONEWORD) as c_int} else {0}; - if !list.is_null() && legal_identifier((*(*list).word).word) == 0 && valid_array_reference((*(*list).word).word, vflags) == 0 { + // sh_invalidid((*(*list).word).word); r_sh_invalidid((*(*list).word).word); return EXECUTION_FAILURE; } - ifs_chars = getifs(); + //忽略界定符 + if ignore_delim != 0{ //-N ignore_delim = 1 + delim = -1; + } + + ifs_chars = getifs(); //ifs_chars is "\n" if ifs_chars.is_null() { ifs_chars = ifs_chars_null.as_ptr() as *mut c_char; } if ignore_delim != 0 { - delim = -1; ifs_chars = ifs_chars_null.as_ptr() as *mut c_char; } skip_ctlesc = 0; skip_ctlnul = 0; e = ifs_chars; + loop { if *e == 0 { break; @@ -228,6 +256,7 @@ unsafe { break 'out_assig_vars; } + //设置TMOUT后,TMOUT是默认读取时间 let str_val = CString::new("TMOUT").unwrap(); e = get_string_value(str_val.as_ptr()); if have_timeout == 0 && !e.is_null() { @@ -241,9 +270,10 @@ unsafe { } } - let frame_name = CString::new("read_builtin").unwrap(); + let frame_name = CString::new("r_read_builtin").unwrap(); //有没有可能是r_read_builtin? begin_unwind_frame(frame_name.as_ptr() as *mut c_char); + if interactive == 0 && default_buffered_input >= 0 && fd_is_bash_input(fd) != 0 { sync_buffered_stream(default_buffered_input); } @@ -253,6 +283,7 @@ unsafe { input_is_pipe = (libc::lseek(fd, 0, libc::SEEK_CUR) < 0 && (errno() == libc::ESPIPE)) as c_int; } + //如果设置 -p,-e,-s但输入不是终端,忽略 if (!prompt.is_null() || edit != 0 || silent != 0) && input_is_tty == 0 { itext = PT_NULL as *mut c_char; edit = 0; @@ -277,6 +308,7 @@ unsafe { if code != 0 { sigalrm_seen = 0; orig_input_string = PT_NULL as *mut c_char; + *input_string.offset(i as isize) = b'\0' as c_char; if i == 0 { t = libc::malloc(1) as *mut c_char; *t = b'\0' as c_char; @@ -284,7 +316,6 @@ unsafe { t = libc::strcpy( xmalloc( (libc::strlen(input_string) + 1) as size_t) as *mut c_char, input_string); } - run_unwind_frame(frame_name.as_ptr() as *mut c_char); input_string = t; retval = 128 + libc::SIGALRM; @@ -304,11 +335,10 @@ unsafe { add_unwind_protect(bashline_reset_event_hook as *mut c_void, PT_NULL as *mut c_char); } - falarm(tmsec, tmusec); } - if nchars > 0 || delim != b'\n' as c_char { + if nchars > 0 || delim != b'\n' as c_char { //-d -n if edit != 0 { if nchars > 0 { unwind_protect_mem(&mut rl_num_chars_to_read as *mut c_int, std::mem::size_of_val(&rl_num_chars_to_read) as c_int); @@ -319,10 +349,13 @@ unsafe { set_eol_delim(delim as c_int); add_unwind_protect(reset_eol_delim as *mut c_void, PT_NULL as *mut c_char); } - } else if input_is_tty != 0 { - termsave.unwrap().fd = fd; + } else if input_is_tty != 0 { //-d -n + // termsave.unwrap().fd = fd; + termsave.fd = fd; ttgetattr(fd, &mut ttattrs as *mut libc::termios); - termsave.unwrap().attrs = ttattrs; + // termsave.unwrap().attrs = ttattrs; + termsave.attrs = ttattrs; + ttset = ttattrs; if silent != 0 { i = ttfd_cbreak(fd, std::mem::transmute(&ttset)); @@ -334,16 +367,19 @@ unsafe { sh_ttyerror(1); } tty_modified = 1; - add_unwind_protect(ttyrestore as *mut c_void, ptermsave); + // add_unwind_protect(ttyrestore as *mut c_void, ptermsave); + add_unwind_protect(ttyrestore as *mut c_void, &mut termsave); if interactive_shell == 0 { initialize_terminating_signals(); } } - } else if silent != 0 { - termsave.unwrap().fd = fd; + } else if silent != 0 { //-s + // termsave.unwrap().fd = fd; + termsave.fd = fd; ttgetattr(fd, &mut ttattrs as *mut libc::termios); - termsave.unwrap().attrs = ttattrs; + // termsave.unwrap().attrs = ttattrs; + termsave.attrs = ttattrs; ttset = ttattrs; i = ttfd_noecho(fd, std::mem::transmute(&ttset)); @@ -352,7 +388,8 @@ unsafe { } tty_modified = 1; - add_unwind_protect(ttyrestore as *mut c_void, ptermsave); + // add_unwind_protect(ttyrestore as *mut c_void, ptermsave); + add_unwind_protect(ttyrestore as *mut c_void, &mut termsave ); if interactive_shell == 0 { initialize_terminating_signals(); } @@ -360,6 +397,7 @@ unsafe { save_instream = std::mem::zeroed(); if edit != 0 && fd != 0 { + if bash_readline_initialized == 0 { initialize_readline(); } @@ -378,8 +416,9 @@ unsafe { unbuffered_read = 1; } - if !prompt.is_null() && edit == 0 { - eprintln!("{}", CStr::from_ptr(prompt).to_str().unwrap()); + if !prompt.is_null() && edit == 0 { //-p no -e + // eprintln!("{}", CStr::from_ptr(prompt).to_str().unwrap()); + eprint!("{}", CStr::from_ptr(prompt).to_str().unwrap()); } @@ -392,7 +431,7 @@ unsafe { siglongjmp (std::mem::transmute(&alrmbuf), 1); } - if edit != 0 { + if edit != 0 { //没有设置-e edit等于0 if !rlbuf.is_null() && *((rlbuf as usize + rlind as usize) as *mut c_char) == 0 && delim != 0 { @@ -401,8 +440,14 @@ unsafe { } if rlbuf.is_null() { reading = 1; - rlbuf = if prompt.is_null() {edit_line("".as_ptr() as *mut c_char, itext)} - else {edit_line(prompt, itext)}; + rlbuf = if prompt.is_null() { + // edit_line("".as_ptr() as *mut c_char, itext)} + // let c_str = b'\0'; // b'\0'代表的是空字符串,和String::from("")不是同一个东西。 + edit_line(b'\0' as *mut c_char,itext) // b'\0'代表的是空字符串,和String::from("")不是同一个东西。 + } + else { + edit_line(prompt, itext) + }; reading = 0; rlind = 0; } @@ -425,16 +470,26 @@ unsafe { check_alrm(); *(libc::__errno_location()) = 0; if unbuffered_read == 2 { - retval = if posixly_correct != 0 {zreadintr(fd, &mut c as *mut c_char, 1) as c_int} - else {zreadn(fd, &mut c as *mut c_char, (nchars - nr) as usize) as c_int}; + retval = if posixly_correct != 0 { + zreadintr(fd, &mut c as *mut c_char, 1) as c_int + } + else { + zreadn(fd, &mut c as *mut c_char, (nchars - nr) as usize) as c_int + }; } else if unbuffered_read != 0 { - retval = if posixly_correct != 0 {zreadintr(fd, &mut c as *mut c_char, 1) as c_int} - else {zreadn(fd, &mut c as *mut c_char, 1) as c_int}; - } else { - retval = if posixly_correct != 0 {zreadcintr(fd, &mut c as *mut c_char) as c_int} - else {zreadc(fd, &mut c as *mut c_char) as c_int}; + retval = if posixly_correct != 0 { + zreadintr(fd, &mut c as *mut c_char, 1) as c_int} + else { + zread(fd, &mut c as *mut c_char, 1) as c_int}; + } + else { + retval = if posixly_correct != 0 { + zreadcintr(fd, &mut c as *mut c_char) as c_int + } + else { //-a -t + zreadc(fd, &mut c as *mut c_char) as c_int + }; } - reading = 0; if retval <= 0 { @@ -454,9 +509,9 @@ unsafe { } else { lastsig = 0; } - if terminating_signal != 0 && tty_modified != 0 { - ttyrestore(); + // ttyrestore(); + ttyrestore(&mut termsave); } check_alrm(); eof = 1; @@ -484,7 +539,7 @@ unsafe { } } 'out_add_char: loop { - if pass_next != 0 { + if pass_next != 0 { pass_next = 0; if c == b'\n' as c_char { if skip_ctlesc == 0 && i > 0 {i -= 1;} @@ -502,10 +557,11 @@ unsafe { *((input_string as usize + i as usize) as *mut c_char) = CTLESC; i += 1; } + continue 'get_input_string; } - if ignore_delim == 0 && c == delim { + if ignore_delim == 0 && c == delim { //-a break 'get_input_string; } @@ -519,12 +575,14 @@ unsafe { i += 1; } break 'out_add_char; - } + }//out_add_char + *((input_string as usize + i as usize) as *mut c_char) = c; i += 1; check_alrm(); if mb_cur_max > 1 && is_basic(c) == 0 { + *((input_string as usize + i as usize) as *mut c_char) = b'\0' as c_char; if edit != 0 { @@ -540,13 +598,13 @@ unsafe { } else if locale_utf8locale == 0 || ((c as u8 & 0x80) != 0) { i += read_mbchar(fd, input_string, i, c as c_int, unbuffered_read); } - - nr += 1; - if nchars > 0 && nr >= nchars { - break 'get_input_string; - } } - } + nr += 1; + if nchars > 0 && nr >= nchars { + break 'get_input_string; + } + + } //get_input_string *((input_string as usize + i as usize) as *mut c_char) = b'\0' as c_char; check_alrm(); @@ -572,15 +630,25 @@ unsafe { if nchars > 0 || delim != b'\n' as c_char { if edit != 0 { - - } else if input_is_tty != 0 { - ttyrestore(); + if nchars > 0{ + rl_num_chars_to_read = 0; + } + if delim != b'\n' as c_char{ + reset_eol_delim(0 as *mut c_char); + } + } + else if input_is_tty != 0 { + // ttyrestore(); + ttyrestore(&mut termsave); } - } else if silent != 0 { - ttyrestore(); + } + else if silent != 0 { + // ttyrestore(); + ttyrestore(&mut termsave); } - if unbuffered_read != 0 { + // if unbuffered_read != 0 { + if unbuffered_read == 0 { zsyncfd(fd); } @@ -593,10 +661,11 @@ unsafe { retval = if eof != 0 {EXECUTION_FAILURE} else {EXECUTION_SUCCESS}; break 'out_assig_vars; -} +} //out_assig_vars - if !arrayname.is_null() { - if legal_identifier(arrayname) == 0 { + if !arrayname.is_null() { //和-a有关 + if legal_identifier(arrayname) == 0 { //标签不符合规范 + // sh_invalidid(arrayname); r_sh_invalidid(arrayname); libc::free(input_string as *mut c_void); return EXECUTION_FAILURE; @@ -630,10 +699,11 @@ unsafe { } libc::free(input_string as *mut c_void); + return retval; } - if list.is_null() { + if list.is_null() { //和-d相关 -n 0可以退出,有显示 if saw_escape != 0 { let t = dequote_string(input_string); var = bind_variable("REPLY".as_ptr() as *const c_char, t, 0); @@ -667,6 +737,7 @@ unsafe { if legal_identifier(varname) == 0 && valid_array_reference(varname, vflags) == 0 { + // sh_invalidid(varname); r_sh_invalidid(varname); libc::free(orig_input_string as *mut c_void); return EXECUTION_FAILURE; @@ -705,6 +776,7 @@ unsafe { if legal_identifier((*((*list).word)).word) == 0 && valid_array_reference((*((*list).word)).word, vflags) == 0 { + // sh_invalidid((*((*list).word)).word); r_sh_invalidid((*((*list).word)).word); libc::free(orig_input_string as *mut c_void); return EXECUTION_FAILURE; @@ -743,21 +815,31 @@ unsafe { } libc::free(orig_input_string as *mut c_void); return retval; -} +} //unsafe } /* ---------------------------------------------------------------------------------- */ -pub fn is_basic(c: i8) -> u32 { - let is_basic_table :[c_uint; 8] = [ 0x00001a00, 0xffffffef, 0xfffffffe, 0x7ffffffe, 0,0,0,0]; +// pub fn is_basic(c: i8) -> u32 { +// let is_basic_table :[c_uint; 8] = [ 0x00001a00, 0xffffffef, 0xfffffffe, 0x7ffffffe, 0,0,0,0]; - let index = (c >> 5) as usize; - return (is_basic_table[index] >> (c & 31) ) & 1; +// let index = (c >> 5) as usize; +// return (is_basic_table[index] >> (c & 31) ) & 1; +// } + +#[inline] +unsafe extern "C" fn is_basic(mut c:libc::c_char)->libc::c_int{ + return (*is_basic_table + .as_ptr() + .offset((c as libc::c_uchar as libc::c_int>>5 as libc::c_int)as isize) + >>(c as libc::c_uchar as libc::c_int & 31 as libc::c_int) + & 1 as libc::c_int as libc::c_uint) as libc::c_int; } pub fn bind_read_variable(name: *mut c_char, value: *mut c_char) -> * mut SHELL_VAR { let v: *mut SHELL_VAR; unsafe { + // v = builtin_bind_variable(name, value, 0); v = r_builtin_bind_variable(name, value, 0); if v.is_null() { @@ -840,6 +922,7 @@ fn check_alrm() { unsafe { if sigalrm_seen != 0 { siglongjmp (std::mem::transmute(&alrmbuf), 1); + // siglongjmp (&mut alrmbuf as *mut __jmp_buf_tag, 1); } } } @@ -879,39 +962,79 @@ unsafe { return (r1 != 0 || r2 != 0) as c_int; } -fn edit_line(p : *mut c_char, itext : *mut c_char) -> *mut c_char { -unsafe { - if bash_readline_initialized == 0 { - initialize_readline(); - } +// fn edit_line(p : *mut c_char, itext : *mut c_char) -> *mut c_char { +// unsafe { +// if bash_readline_initialized == 0 { +// initialize_readline(); +// } - old_attempted_completion_function = std::mem::transmute(rl_attempted_completion_function); - rl_attempted_completion_function = std::mem::transmute(0 as usize); - bashline_set_event_hook(); - if !itext.is_null() { - old_startup_hook = std::mem::transmute(rl_startup_hook); - rl_startup_hook = std::mem::transmute(set_itext as usize); - deftext = itext; - } +// old_attempted_completion_function = std::mem::transmute(rl_attempted_completion_function); +// rl_attempted_completion_function = std::mem::transmute(0 as usize); +// bashline_set_event_hook(); +// if !itext.is_null() { +// old_startup_hook = std::mem::transmute(rl_startup_hook); +// rl_startup_hook = std::mem::transmute(set_itext as usize); +// deftext = itext; +// } - let mut ret = readline(p); +// let mut ret = readline(p); - rl_attempted_completion_function = std::mem::transmute(old_attempted_completion_function); - old_attempted_completion_function = std::mem::transmute(0 as usize); - bashline_reset_event_hook(); +// rl_attempted_completion_function = std::mem::transmute(old_attempted_completion_function); +// old_attempted_completion_function = std::mem::transmute(0 as usize); +// bashline_reset_event_hook(); - if ret.is_null() { +// if ret.is_null() { +// return ret; +// } + +// let len:i32 = libc::strlen(ret) as i32; +// ret = xrealloc(ret as *mut c_void, (len + 2) as usize) as *mut c_char; +// // *ret = delim; +// *ret.offset(len as isize) = delim; +// *((ret as usize + 1) as *mut c_char) = b'\0' as c_char; + +// return ret; +// } +// } + + +fn edit_line(p : *mut c_char, itext : *mut c_char) -> *mut c_char { + let mut len:i32; + unsafe { + if bash_readline_initialized == 0 { + initialize_readline(); + } + + old_attempted_completion_function = std::mem::transmute(rl_attempted_completion_function); + rl_attempted_completion_function = std::mem::transmute(0 as usize); + bashline_set_event_hook(); + if !itext.is_null() { + old_startup_hook = std::mem::transmute(rl_startup_hook); + rl_startup_hook = std::mem::transmute(set_itext as usize); + deftext = itext; + } + + let mut ret = readline(p); + + rl_attempted_completion_function = std::mem::transmute(old_attempted_completion_function); + old_attempted_completion_function = std::mem::transmute(0 as usize); + bashline_reset_event_hook(); + + if ret.is_null() { + return ret; + } + + len = libc::strlen(ret) as i32; + ret = xrealloc(ret as *mut c_void, (len + 2) as usize) as *mut c_char; + *ret.offset(len as isize) = delim; + len += 1; + *ret.offset(len as isize) = b'\0' as c_char; return ret; } - - let len = libc::strlen(ret); - ret = xrealloc(ret as *mut c_void, len + 2) as *mut c_char; - *ret = delim; - *((ret as usize + 1) as *mut c_char) = b'\0' as c_char; - return ret; -} } + + fn sigalrm(s : c_int) { unsafe { sigalrm_seen = 1; @@ -926,18 +1049,23 @@ unsafe { } } -fn ttyrestore() -{ -unsafe { - if termsave.is_none() { - let tmp: tty_save = std::mem::zeroed(); - termsave = Some(tmp); - } - - let ter = termsave.unwrap(); - ttsetattr(ter.fd, std::mem::transmute(&(ter.attrs))); - tty_modified = 0; -} +// fn ttyrestore() +// { +// unsafe { +// if termsave.is_none() { +// let tmp: tty_save = std::mem::zeroed(); +// termsave = Some(tmp); +// } + +// let ter = termsave.unwrap(); +// ttsetattr(ter.fd, std::mem::transmute(&(ter.attrs))); +// tty_modified = 0; +// } +// } + +unsafe extern "C" fn ttyrestore(mut ttp:*mut tty_save){ + ttsetattr((*ttp).fd,&mut (*ttp).attrs); + tty_modified = 0 as libc::c_int; } #[no_mangle] @@ -945,7 +1073,7 @@ pub extern "C" fn read_tty_cleanup() { unsafe { if tty_modified != 0 { - ttyrestore(); + ttyrestore(&mut termsave); } } } @@ -964,31 +1092,59 @@ static mut old_newline_ctype: c_int = 0; static mut old_newline_func: usize = 0; static mut delim_char: u8 = 0; +// fn set_eol_delim(c: c_int) +// { +// unsafe { +// if bash_readline_initialized == 0 { +// initialize_readline(); +// } + +// let cmap = rl_get_keymap(); +// let n = std::mem::size_of_val(&*cmap); +// let ret_pos = (b'M' & 0x1f) as usize * n; +// let c_pos = (c & 0x1f) as usize * n; + +// /* Save the old delimiter char binding */ +// old_newline_ctype = (*((cmap as usize + ret_pos) as Keymap)).tp as c_int; +// old_newline_func = (*((cmap as usize + ret_pos) as Keymap)).function as usize; +// old_delim_ctype = (*((cmap as usize + c_pos) as Keymap)).tp as c_int; +// old_delim_func = (*((cmap as usize + c_pos) as Keymap)).function as usize; + +// /* Change newline to self-insert */ +// (*((cmap as usize + ret_pos) as Keymap)).tp = ISFUNC as c_char; +// (*((cmap as usize + ret_pos) as Keymap)).function = rl_insert; + +// /* Bind the delimiter character to accept-line. */ +// (*((cmap as usize + c_pos) as Keymap)).tp = ISFUNC as c_char; +// (*((cmap as usize + c_pos) as Keymap)).function = rl_newline; + +// delim_char = c as u8; +// } +// } + fn set_eol_delim(c: c_int) { + let mut cmap:Keymap; unsafe { if bash_readline_initialized == 0 { initialize_readline(); } - let cmap = rl_get_keymap(); - let n = std::mem::size_of_val(&*cmap); - let ret_pos = (b'M' & 0x1f) as usize * n; - let c_pos = (c & 0x1f) as usize * n; + // let cmap = rl_get_keymap(); + cmap = rl_get_keymap(); - /* Save the old delimiter char binding */ - old_newline_ctype = (*((cmap as usize + ret_pos) as Keymap)).tp as c_int; - old_newline_func = (*((cmap as usize + ret_pos) as Keymap)).function as usize; - old_delim_ctype = (*((cmap as usize + c_pos) as Keymap)).tp as c_int; - old_delim_func = (*((cmap as usize + c_pos) as Keymap)).function as usize; + old_newline_ctype = (*cmap.offset((b'M' as i32 & 0x1f) as isize)).tp as c_int; + old_newline_func = (*cmap.offset((b'M' as i32 & 0x1f) as isize)).function as usize; + old_delim_ctype = (*cmap.offset(c as isize)).tp as c_int; + old_delim_func = (*cmap.offset(c as isize)).function as usize; - /* Change newline to self-insert */ - (*((cmap as usize + ret_pos) as Keymap)).tp = ISFUNC as c_char; - (*((cmap as usize + ret_pos) as Keymap)).function = rl_insert; + /* Change newline to self-insert */ + (*cmap.offset((b'M' as i32 & 0x1f) as isize)).tp = ISFUNC as c_char; + (*cmap.offset((b'M' as i32 & 0x1f) as isize)).function = rl_insert; /* Bind the delimiter character to accept-line. */ - (*((cmap as usize + c_pos) as Keymap)).tp = ISFUNC as c_char; - (*((cmap as usize + c_pos) as Keymap)).function = rl_newline; + (*cmap.offset(c as isize)).tp = ISFUNC as c_char; + (*cmap.offset(c as isize)).function = rl_newline; delim_char = c as u8; } diff --git a/record.txt b/record.txt index a681e558..5a340212 100644 --- a/record.txt +++ b/record.txt @@ -29,3 +29,4 @@ 51 52 53 +54 -- Gitee From 8520e6b3da1ca79d8868e80eb7d7274239f05754 Mon Sep 17 00:00:00 2001 From: liutong Date: Tue, 27 Jun 2023 22:24:13 +0800 Subject: [PATCH 6/6] modify exec --- bash-5.1/Makefile.in | 2 +- bash-5.1/builtins_rust/break_1/src/lib.rs | 3 +- bash-5.1/builtins_rust/colon/src/lib.rs | 4 +- bash-5.1/builtins_rust/command/Cargo.toml | 2 +- bash-5.1/builtins_rust/exec_cmd/Cargo.toml | 2 +- bash-5.1/builtins_rust/exec_cmd/src/lib.rs | 577 +++++++++++++++------ bash-5.1/builtins_rust/jobs/src/lib.rs | 3 +- bash-5.1/execute_cmd.c | 26 +- record.txt | 1 + 9 files changed, 429 insertions(+), 191 deletions(-) diff --git a/bash-5.1/Makefile.in b/bash-5.1/Makefile.in index 30ddc175..3ea296c0 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 -lrcommand -lrcomplete -lrdeclare -lrecho -lrenable -lreval -lrexec -lrexit -lrfc -lrfg_bg -lrgetopts -lrhash -lrexit -lrhelp -lrhistory -lrjobs -lrkill -lrmapfile -lrpushd -lrread -lrlet -lrreturn -lrset -lrsetattr -lrshift -lrshopt -lrsource -lrsuspend -lrtest -lrtimes -lrtrap -lrtype -lrulimit -lrumask -lrwait -lrprintf -lrexec_cmd +LIBS = $(BUILTINS_LIB) $(LIBRARIES) @LIBS@ -lrt -lpthread -L./target/debug -lralias -lrbind -lrbreak -lrbuiltin -lrcaller -lrcd -lrcolon -lrcommon -lcommand -lrcomplete -lrdeclare -lrecho -lrenable -lreval -lrexec -lrexit -lrfc -lrfg_bg -lrgetopts -lrhash -lrexit -lrhelp -lrhistory -lrjobs -lrkill -lrmapfile -lrpushd -lrread -lrlet -lrreturn -lrset -lrsetattr -lrshift -lrshopt -lrsource -lrsuspend -lrtest -lrtimes -lrtrap -lrtype -lrulimit -lrumask -lrwait -lrprintf -lrexec_cmd LIBS_FOR_BUILD = STATIC_LD = @STATIC_LD@ diff --git a/bash-5.1/builtins_rust/break_1/src/lib.rs b/bash-5.1/builtins_rust/break_1/src/lib.rs index a2f0a636..54df7d5e 100644 --- a/bash-5.1/builtins_rust/break_1/src/lib.rs +++ b/bash-5.1/builtins_rust/break_1/src/lib.rs @@ -73,7 +73,8 @@ pub extern "C" fn r_break_builtin(mut list :*mut WordList) -> i32 { return (EXECUTION_SUCCESS!()); } -fn continue_builtin (list :*mut WordList) -> i32 { +#[no_mangle] +pub extern "C" fn r_continue_builtin (list :*mut WordList) -> i32 { let mut newcont : intmax_t = 0 as intmax_t; unsafe { CHECK_HELPOPT! (list); diff --git a/bash-5.1/builtins_rust/colon/src/lib.rs b/bash-5.1/builtins_rust/colon/src/lib.rs index 6ead6461..393088e9 100644 --- a/bash-5.1/builtins_rust/colon/src/lib.rs +++ b/bash-5.1/builtins_rust/colon/src/lib.rs @@ -13,13 +13,13 @@ mod tests { #[no_mangle] -pub extern "C" fn r_colon_builtin(ignore:WordList)->i32 { +pub extern "C" fn r_colon_builtin(ignore: *mut WordList )->i32 { println!("in r_colon_builtin"); 0 } #[no_mangle] -pub extern "C" fn r_false_builtin(ignore: WordList) -> i32 { +pub extern "C" fn r_false_builtin(ignore: *mut WordList) -> i32 { println!("in r_false_builtin"); 1 } diff --git a/bash-5.1/builtins_rust/command/Cargo.toml b/bash-5.1/builtins_rust/command/Cargo.toml index 0b76f4f5..d390905e 100644 --- a/bash-5.1/builtins_rust/command/Cargo.toml +++ b/bash-5.1/builtins_rust/command/Cargo.toml @@ -11,4 +11,4 @@ rcommon = {path="../common"} [lib] crate-type = ["staticlib", "rlib"] -name = "rcommand" +name = "command" diff --git a/bash-5.1/builtins_rust/exec_cmd/Cargo.toml b/bash-5.1/builtins_rust/exec_cmd/Cargo.toml index 9a416d5b..b120c60b 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 80f93ef6..0a31eced 100644 --- a/bash-5.1/builtins_rust/exec_cmd/src/lib.rs +++ b/bash-5.1/builtins_rust/exec_cmd/src/lib.rs @@ -1,23 +1,23 @@ -use rcommon::{WordList, WordDesc}; -use ralias::r_alias_builtin; +use rcommon::WordList; +use ralias::{r_alias_builtin,r_unalias_builtin}; use rbind::r_bind_builtin; -use rbreak::r_break_builtin; +use rbreak::{r_break_builtin,r_continue_builtin}; use rbuiltin::r_builtin_builtin; use rcaller::r_caller_builtin; -use rcd::r_cd_builtin; +use rcd::{r_cd_builtin,r_pwd_builtin}; //use rcmd::r_cmd_builtin; -use rcolon::r_colon_builtin; -//use command::command_builtin; +use rcolon::{r_colon_builtin,r_false_builtin}; +use command::r_command_builtin; //use rcommon ::r__builtin; -use rcomplete::r_complete_builtin; -use rdeclare::r_declare_builtin; +use rcomplete::{r_complete_builtin,r_compgen_builtin,r_compopt_builtin}; +use rdeclare::{r_declare_builtin,r_local_builtin}; use recho::r_echo_builtin; use renable::r_enable_builtin; use reval::r_eval_builtin; use rexec::r_exec_builtin; -use rexit::r_exit_builtin; +use rexit::{r_exit_builtin,r_logout_builtin}; use rfc::r_fc_builtin; -use rfg_bg::r_fg_builtin; +use rfg_bg::{r_fg_builtin,r_bg_builtin}; use rgetopts::r_getopts_builtin; use rhash::r_hash_builtin; use rhelp::r_help_builtin; @@ -26,12 +26,12 @@ use rjobs::r_jobs_builtin; use rkill::r_kill_builtin; use rmapfile::r_mapfile_builtin; use rprintf::r_printf_builtin; -use rpushd::r_pushd_builtin; +use rpushd::{r_pushd_builtin,r_dirs_builtin,r_popd_builtin}; use rread::r_read_builtin; use rlet::r_let_builtin; use rreturn::r_return_builtin; -use rset::r_set_builtin; -//use rsetattr::r_setattr_builtin; +use rset::{r_set_builtin,r_unset_builtin}; +use rsetattr::{r_export_builtin,r_readonly_builtin}; use rshift::r_shift_builtin; use rshopt::r_shopt_builtin; use rsource::r_source_builtin; @@ -43,26 +43,38 @@ 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, + UnAliasCmd, BindCmd, BreakCmd, + ContinueCmd, BuiltinCmd, CallerCmd, CdCmd, + PwdCmd, ColonCmd, + FalseCmd, CommandCmd, CommonCmd, CompleteCmd, + CompoptCmd, + CompgenCmd, DeclareCmd, + LocalCmd, EchoCmd, EnableCmd, EvalCmd, ExecCmd, ExitCmd, + LogoutCmd, FcCmd, - FgbgCmd, + FgCmd, + BgCmd, GetoptsCmd, HashCmd, HelpCmd, @@ -73,11 +85,16 @@ enum CMDType { MapfileCmd, PrintfCmd, PushdCmd, + DirsCmd, + PopdCmd, ReadCmd, ReservedCmd, ReturnCmd, SetattrCmd, + ExportCmd, + ReadonlyCmd, SetCmd, + UnSetCmd, ShiftCmd, ShoptCmd, SourceCmd, @@ -93,260 +110,365 @@ enum CMDType { struct AliasComand ; impl CommandExec for AliasComand{ - fn excute(&self,list : *mut WordList){ + fn excute(&self,list : *mut WordList)-> i32{ unsafe { - r_alias_builtin(list); + r_alias_builtin(list) } } } + struct UnAliasComand ; + impl CommandExec for UnAliasComand{ + fn excute(&self,list : *mut WordList)-> i32{ + unsafe { + r_unalias_builtin(list) + } + } + } + struct BindComand; impl CommandExec for BindComand{ - fn excute(&self,list : *mut WordList){ - r_bind_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_bind_builtin(list) } } struct BreakComand; impl CommandExec for BreakComand{ - fn excute(&self,list : *mut WordList){ - r_break_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_break_builtin(list) + } +} +struct ContinueComand; + impl CommandExec for ContinueComand{ + fn excute(&self,list : *mut WordList)-> i32{ + r_continue_builtin(list) } } struct BuiltinComand; impl CommandExec for BuiltinComand{ - fn excute(&self,list : *mut WordList){ - r_builtin_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_builtin_builtin(list) } } struct CallerComand; impl CommandExec for CallerComand{ - fn excute(&self,list : *mut WordList){ - r_caller_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_caller_builtin(list) } } struct CdComand; impl CommandExec for CdComand{ - fn excute(&self,list : *mut WordList){ - r_cd_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_cd_builtin(list) } } + +struct PwdComand; +impl CommandExec for PwdComand{ + fn excute(&self,list : *mut WordList)-> i32{ + r_pwd_builtin(list) + } +} struct ColonComand; impl CommandExec for ColonComand{ - fn excute(&self,list :*mut WordList){ - // r_colon_builtin(list); + fn excute(&self,list :*mut WordList)-> i32{ + r_colon_builtin(list) + // 0 } +} +struct FalseComand; +impl CommandExec for FalseComand{ + fn excute(&self,list :*mut WordList)-> i32{ + r_false_builtin(list) + // 0 + } } struct CommandComand; impl CommandExec for CommandComand{ - fn excute(&self,list : *mut WordList){ - //command_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + unsafe { + r_command_builtin(list) + } + } } struct CommonComand; impl CommandExec for CommonComand{ - fn excute(&self,list : *mut WordList){ - unsafe { - r_alias_builtin(list); - } + fn excute(&self,list : *mut WordList)-> i32{ + 0 } } struct CompleteComand; impl CommandExec for CompleteComand{ - fn excute(&self,list : *mut WordList){ - r_complete_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_complete_builtin(list) } } + + +struct CompgenCommand; +impl CommandExec for CompgenCommand{ + fn excute(&self,list : *mut WordList)-> i32{ + r_compgen_builtin(list) + } +} + +struct CompoptCommand; +impl CommandExec for CompoptCommand{ + fn excute(&self,list : *mut WordList)-> i32{ + r_compopt_builtin(list) + } +} struct DeclareComand; impl CommandExec for DeclareComand{ - fn excute(&self,list : *mut WordList){ - r_declare_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_declare_builtin(list) } } +struct LocalComand; + impl CommandExec for LocalComand{ + fn excute(&self,list : *mut WordList)-> i32{ + r_local_builtin(list) + } +} + struct EchoComand; impl CommandExec for EchoComand{ - fn excute(&self,list : *mut WordList){ - r_echo_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_echo_builtin(list) } } struct EnableComand; impl CommandExec for EnableComand{ - fn excute(&self,list : *mut WordList){ + fn excute(&self,list : *mut WordList)-> i32{ unsafe { - r_enable_builtin(list); + r_enable_builtin(list) } } } struct EvalComand; impl CommandExec for EvalComand{ - fn excute(&self,list : *mut WordList){ - r_eval_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_eval_builtin(list) } } struct ExecComand; impl CommandExec for ExecComand{ - fn excute(&self,list : *mut WordList){ - r_exec_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_exec_builtin(list) } } struct ExitComand; impl CommandExec for ExitComand{ - fn excute(&self,list : *mut WordList){ - r_exit_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_exit_builtin(list) } } + +struct LogoutCommand; +impl CommandExec for LogoutCommand{ + fn excute(&self,list : *mut WordList)-> i32{ + r_logout_builtin(list) + } +} struct FcComand; impl CommandExec for FcComand{ - fn excute(&self,list : *mut WordList){ - r_fc_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_fc_builtin(list) } } - struct FgbgComand; - impl CommandExec for FgbgComand{ - fn excute(&self,list : *mut WordList){ - r_fg_builtin(list); + struct FgComand; + impl CommandExec for FgComand{ + fn excute(&self,list : *mut WordList)-> i32{ + r_fg_builtin(list) } +} +struct BgComand; +impl CommandExec for BgComand{ + fn excute(&self,list : *mut WordList)-> i32{ + r_bg_builtin(list) + } } struct GetoptsComand; impl CommandExec for GetoptsComand{ - fn excute(&self,list : *mut WordList){ - r_getopts_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_getopts_builtin(list) } } struct HashComand; impl CommandExec for HashComand{ - fn excute(&self,list : *mut WordList){ - r_hash_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_hash_builtin(list) } } struct HelpComand; impl CommandExec for HelpComand{ - fn excute(&self,list : *mut WordList){ - r_help_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_help_builtin(list) } } struct HistoryComand; impl CommandExec for HistoryComand{ - fn excute(&self,list : *mut WordList){ - r_history_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_history_builtin(list) } } struct JobsComand; impl CommandExec for JobsComand{ - fn excute(&self,list : *mut WordList){ - r_jobs_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_jobs_builtin(list) } } struct KillComand; impl CommandExec for KillComand{ - fn excute(&self,list : *mut WordList){ - r_kill_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_kill_builtin(list) } } struct LetComand; impl CommandExec for LetComand{ - fn excute(&self,list : *mut WordList){ - r_let_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_let_builtin(list) } } struct MapfileComand; impl CommandExec for MapfileComand{ - fn excute(&self,list : *mut WordList){ - r_mapfile_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_mapfile_builtin(list) } } struct PrintfComand; impl CommandExec for PrintfComand{ - fn excute(&self,list : *mut WordList){ - r_printf_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_printf_builtin(list) } } - struct PushdComand; - impl CommandExec for PushdComand{ - fn excute(&self,list : *mut WordList){ - r_pushd_builtin(list); + struct PushdCommand; + impl CommandExec for PushdCommand{ + fn excute(&self,list : *mut WordList)-> i32{ + r_pushd_builtin(list) } } + +struct PopdComand; +impl CommandExec for PopdComand{ + fn excute(&self,list : *mut WordList)-> i32{ + r_popd_builtin(list) + } +} +struct DirsCommand; +impl CommandExec for DirsCommand{ + fn excute(&self,list : *mut WordList)-> i32{ + r_dirs_builtin(list) + } +} struct ReadComand; impl CommandExec for ReadComand{ - fn excute(&self,list : *mut WordList){ - r_read_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_read_builtin(list) } } struct ReservedComand; impl CommandExec for ReservedComand{ - fn excute(&self,list : *mut WordList){ - //r_reserve_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + // r_reserve_builtin(list) + 0 } } struct ReturnComand; impl CommandExec for ReturnComand{ - fn excute(&self,list : *mut WordList){ - r_return_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_return_builtin(list) } } struct SetattrComand; impl CommandExec for SetattrComand{ - fn excute(&self,list : *mut WordList){ + fn excute(&self,list : *mut WordList)-> i32{ //r_setattr_builtin(list); /*unkown enter which func */ + 0 } } +struct ExportComand; +impl CommandExec for ExportComand{ + fn excute(&self,list : *mut WordList)-> i32{ + r_export_builtin(list) + } + +} + +struct ReadonlyComand; +impl CommandExec for ReadonlyComand{ + fn excute(&self,list : *mut WordList)-> i32{ + r_readonly_builtin(list) + } + +} + struct SetComand; impl CommandExec for SetComand{ - fn excute(&self,list : *mut WordList){ - r_set_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_set_builtin(list) } +} + +struct UnSetComand; +impl CommandExec for UnSetComand{ + fn excute(&self,list : *mut WordList)-> i32{ + r_unset_builtin(list) + } + } struct ShiftComand; impl CommandExec for ShiftComand{ - fn excute(&self,list : *mut WordList){ - r_shift_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_shift_builtin(list) } } struct ShoptComand; impl CommandExec for ShoptComand{ - fn excute(&self,list : *mut WordList){ - unsafe { - r_shopt_builtin(list); - } - } + fn excute(&self,list : *mut WordList)-> i32{ + unsafe { + r_shopt_builtin(list) + } + } } struct SourceComand; impl CommandExec for SourceComand{ - fn excute(&self,list : *mut WordList){ - r_source_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_source_builtin(list) } } struct SuspendComand; impl CommandExec for SuspendComand{ - fn excute(&self,list : *mut WordList){ - r_suspend_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_suspend_builtin(list) } } struct TestComand; impl CommandExec for TestComand{ - fn excute(&self,list : *mut WordList){ - r_test_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_test_builtin(list) } } struct TimesComand; impl CommandExec for TimesComand{ - fn excute(&self,list : *mut WordList){ - r_times_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_times_builtin(list) } } struct TrapComand; impl CommandExec for TrapComand{ - fn excute(&self,list : *mut WordList){ - r_trap_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_trap_builtin(list) } } struct TypeComand; impl CommandExec for TypeComand{ - fn excute(&self,list : *mut WordList){ + fn excute(&self,list : *mut WordList)-> i32{ unsafe { - r_type_builtin(list); + r_type_builtin(list) } } @@ -354,29 +476,29 @@ enum CMDType { } struct UlimitComand; impl CommandExec for UlimitComand{ - fn excute(&self,list : *mut WordList){ + fn excute(&self,list : *mut WordList)-> i32{ unsafe { - r_ulimit_builtin(list); + r_ulimit_builtin(list) } } } struct UmaskComand; impl CommandExec for UmaskComand{ - fn excute(&self,list : *mut WordList){ - r_umask_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_umask_builtin(list) } } struct WaitComand; impl CommandExec for WaitComand{ - fn excute(&self,list : *mut WordList){ - r_wait_builtin(list); + fn excute(&self,list : *mut WordList)-> i32{ + r_wait_builtin(list) } } // 定义接口 pub trait CommandExec { - fn excute(&self,list : *mut WordList); + fn excute(&self,list : *mut WordList) -> i32; } @@ -398,12 +520,18 @@ impl Factory for SimpleFactory { CMDType::AliasCmd => Box::new( AliasComand{} ) , + CMDType::UnAliasCmd => Box::new( + UnAliasComand{} + ) , CMDType::BindCmd => Box::new( BindComand{} ), CMDType::BreakCmd => Box::new( BreakComand{} ) , + CMDType::ContinueCmd => Box::new( + ContinueComand{} + ) , CMDType::BuiltinCmd => Box::new( BuiltinComand{} ), @@ -413,21 +541,36 @@ impl Factory for SimpleFactory { CMDType::CdCmd => Box::new( CdComand{} ), + CMDType::PwdCmd => Box::new( + PwdComand{} + ), CMDType::ColonCmd => Box::new( ColonComand{} ), + CMDType::FalseCmd => Box::new( + FalseComand{} + ), CMDType::CommandCmd => Box::new( CommandComand{} ), CMDType::CommonCmd => Box::new( - CommandComand{} + CommonComand{} ), CMDType::CompleteCmd => Box::new( CompleteComand{} ), + CMDType::CompoptCmd => Box::new( + CompoptCommand{} + ), + CMDType::CompgenCmd => Box::new( + CompgenCommand{} + ), CMDType::DeclareCmd => Box::new( DeclareComand{} ), + CMDType::LocalCmd => Box::new( + LocalComand{} + ), CMDType::EchoCmd => Box::new( EchoComand{} ), @@ -443,11 +586,17 @@ impl Factory for SimpleFactory { CMDType::ExitCmd => Box::new( ExitComand{} ), + CMDType::LogoutCmd => Box::new( + LogoutCommand{} + ), CMDType::FcCmd => Box::new( FcComand{} ), - CMDType::FgbgCmd => Box::new( - FgbgComand{} + CMDType::FgCmd => Box::new( + FgComand{} + ), + CMDType::BgCmd => Box::new( + BgComand{} ), CMDType::GetoptsCmd => Box::new( GetoptsComand{} @@ -477,7 +626,13 @@ impl Factory for SimpleFactory { PrintfComand{} ), CMDType::PushdCmd => Box::new( - PushdComand{} + PushdCommand{} + ), + CMDType::DirsCmd => Box::new( + DirsCommand{} + ), + CMDType::PopdCmd => Box::new( + PopdComand{} ), CMDType::ReadCmd => Box::new( ReadComand{} @@ -491,9 +646,18 @@ impl Factory for SimpleFactory { CMDType::SetattrCmd => Box::new( SetattrComand{} ), + CMDType::ExportCmd => Box::new( + ExportComand{} + ), + CMDType::ReadonlyCmd => Box::new( + ReadonlyComand{} + ), CMDType::SetCmd => Box::new( SetComand{} ), + CMDType::UnSetCmd => Box::new( + UnSetComand{} + ), CMDType::ShiftCmd => Box::new( ShiftComand{} ), @@ -531,146 +695,215 @@ impl Factory for SimpleFactory { } } -fn get_cmd_type (command : *mut i8) -> CMDType{ +unsafe fn get_cmd_type (command : *mut i8) -> CMDType{ let mut types = CMDType::HelpCmd; - if command == b"alias\0" as *const u8 as *const i8 as *mut i8{ + if libc::strcmp(command, b"alias\0" as *const u8 as *const i8 as *mut i8) == 0{ types = CMDType::AliasCmd; } - else if command == b"bind\0" as *const u8 as *const i8 as *mut i8{ + if libc::strcmp(command, b"unalias\0" as *const u8 as *const i8 as *mut i8) == 0{ + types = CMDType::UnAliasCmd; + } + else if libc::strcmp(command, b"bind\0" as *const u8 as *const i8 as *mut i8) == 0{ types = CMDType::BindCmd; } - else if command == b"break\0" as *const u8 as *const i8 as *mut i8{ + else if libc::strcmp(command, b"break\0" as *const u8 as *const i8 as *mut i8) == 0 { types = CMDType::BreakCmd; } - else if command == b"builtin\0" as *const u8 as *const i8 as *mut i8{ + else if libc::strcmp(command, b"continue\0" as *const u8 as *const i8 as *mut i8) == 0 { + types = CMDType::ContinueCmd; + } + else if libc::strcmp(command, b"builtin\0" as *const u8 as *const i8 as *mut i8) == 0{ types = CMDType::BuiltinCmd; } - else if command == b"caller\0" as *const u8 as *const i8 as *mut i8{ + else if libc::strcmp(command, b"caller\0" as *const u8 as *const i8 as *mut i8) == 0{ types = CMDType::CallerCmd; } - else if command == b"cd\0" as *const u8 as *const i8 as *mut i8{ + else if libc::strcmp(command, b"cd\0" as *const u8 as *const i8 as *mut i8) == 0{ types = CMDType::CdCmd; } - else if command == b"colon\0" as *const u8 as *const i8 as *mut i8{ + else if libc::strcmp(command, b"pwd\0" as *const u8 as *const i8 as *mut i8) == 0{ + types = CMDType::PwdCmd; + } + else if libc::strcmp(command, b":\0" as *const u8 as *const i8 as *mut i8) == 0 + || libc::strcmp(command, b"true\0" as *const u8 as *const i8 as *mut i8) == 0{ types = CMDType::ColonCmd; } - else if command == b"command\0" as *const u8 as *const i8 as *mut i8{ + else if libc::strcmp(command, b"false\0" as *const u8 as *const i8 as *mut i8) == 0{ + types = CMDType::FalseCmd; + } + else if libc::strcmp(command, b"command\0" as *const u8 as *const i8 as *mut i8) == 0{ types = CMDType::CommandCmd; } - else if command == b"common\0" as *const u8 as *const i8 as *mut i8{ + else if libc::strcmp(command, b"common\0" as *const u8 as *const i8 as *mut i8) == 0{ types = CMDType::CommonCmd; } - else if command == b"complete\0" as *const u8 as *const i8 as *mut i8{ + else if libc::strcmp(command, b"complete\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{ + else if libc::strcmp(command, b"compopt\0" as *const u8 as *const i8 as *mut i8) == 0{ + types = CMDType::CompoptCmd; + } + else if libc::strcmp(command, b"compgen\0" as *const u8 as *const i8 as *mut i8) == 0{ + types = CMDType::CompgenCmd; + } + else if libc::strcmp(command,b"declare\0" as *const u8 as *const i8 as *mut i8) == 0{ types = CMDType::DeclareCmd; } - else if command == b"echo\0" as *const u8 as *const i8 as *mut i8{ + else if libc::strcmp(command,b"local\0" as *const u8 as *const i8 as *mut i8) == 0{ + types = CMDType::LocalCmd; + } + else if libc::strcmp(command,b"echo\0" as *const u8 as *const i8 as *mut i8) == 0{ types = CMDType::EchoCmd; } - else if command == b"enable\0" as *const u8 as *const i8 as *mut i8{ + + else if libc::strcmp(command,b"enable\0" as *const u8 as *const i8 as *mut i8) == 0{ types = CMDType::EnableCmd; } - else if command == b"eval\0" as *const u8 as *const i8 as *mut i8{ + else if libc::strcmp(command,b"eval\0" as *const u8 as *const i8 as *mut i8) == 0{ types = CMDType::EvalCmd; } - else if command == b"exec\0" as *const u8 as *const i8 as *mut i8{ + + else if libc::strcmp(command,b"exec\0" as *const u8 as *const i8 as *mut i8) == 0{ types = CMDType::ExecCmd; } - else if command == b"exit\0" as *const u8 as *const i8 as *mut i8{ + + else if libc::strcmp(command,b"exit\0" as *const u8 as *const i8 as *mut i8) == 0{ types = CMDType::ExitCmd; } - else if command == b"fc\0" as *const u8 as *const i8 as *mut i8{ + else if libc::strcmp(command,b"logout\0" as *const u8 as *const i8 as *mut i8) == 0{ + types = CMDType::LogoutCmd; + } + else if libc::strcmp(command,b"fc\0" as *const u8 as *const i8 as *mut i8) == 0 { types = CMDType::FcCmd; } - 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"fg\0" as *const u8 as *const i8 as *mut i8) == 0{ + types = CMDType::FgCmd; + } + else if libc::strcmp(command,b"bg\0" as *const u8 as *const i8 as *mut i8) == 0{ + types = CMDType::BgCmd; } - else if command == b"getopts\0" as *const u8 as *const i8 as *mut i8{ + + else if libc::strcmp(command,b"getopts\0" as *const u8 as *const i8 as *mut i8) == 0 { types = CMDType::GetoptsCmd; } - else if command == b"hash\0" as *const u8 as *const i8 as *mut i8{ + + else if libc::strcmp(command, b"hash\0" as *const u8 as *const i8 as *mut i8) == 0{ types = CMDType::HashCmd; } - else if command == b"help\0" as *const u8 as *const i8 as *mut i8{ + else if libc::strcmp(command,b"help\0" as *const u8 as *const i8 as * mut i8) == 0 { types = CMDType::HelpCmd; } - else if command == b"history\0" as *const u8 as *const i8 as *mut i8{ + else if libc::strcmp(command,b"history\0" as *const u8 as *const i8 as *mut i8) == 0 { types = CMDType::HistoryCmd; } - else if command == b"jobs\0" as *const u8 as *const i8 as *mut i8{ + else if libc::strcmp(command,b"jobs\0" as *const u8 as *const i8 as *mut i8) == 0{ types = CMDType::JobsCmd; } - else if command == b"kill\0" as *const u8 as *const i8 as *mut i8{ + else if libc::strcmp(command, b"kill\0" as *const u8 as *const i8 as *mut i8) == 0{ types = CMDType::KillCmd; - } - else if command == b"mapfile\0" as *const u8 as *const i8 as *mut i8{ - types = CMDType::MapfileCmd; } - else if command == b"printf\0" as *const u8 as *const i8 as *mut i8{ + else if libc::strcmp(command, b"mapfile\0" as *const u8 as *const i8 as *mut i8) == 0 || + libc::strcmp(command, b"readarray\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{ types = CMDType::PrintfCmd; } - else if command == b"pushd\0" as *const u8 as *const i8 as *mut i8{ + else if libc::strcmp(command,b"pushd\0" as *const u8 as *const i8 as *mut i8) == 0{ types = CMDType::PushdCmd; } - else if command == b"read\0" as *const u8 as *const i8 as *mut i8{ + else if libc::strcmp(command,b"dirs\0" as *const u8 as *const i8 as *mut i8) == 0{ + types = CMDType::DirsCmd; + } + else if libc::strcmp(command,b"popd\0" as *const u8 as *const i8 as *mut i8) == 0{ + types = CMDType::PopdCmd; + } + else if libc::strcmp(command, b"read\0" as *const u8 as *const i8 as *mut i8) == 0{ types = CMDType::ReadCmd; - } - else if command == b"let\0" as *const u8 as *const i8 as *mut i8{ + } + + else if libc::strcmp(command, b"let\0" as *const u8 as *const i8 as *mut i8) == 0{ types = CMDType::LetCmd; } - else if command == b"return\0" as *const u8 as *const i8 as *mut i8{ + + else if libc::strcmp(command,b"return\0" as *const u8 as *const i8 as *mut i8) == 0 { types = CMDType::ReturnCmd; - - } - else if command == b"set\0" as *const u8 as *const i8 as *mut i8{ + } + else if libc::strcmp(command,b"set\0" as *const u8 as *const i8 as *mut i8) == 0 + || libc::strcmp(command,b"typeset\0" as *const u8 as *const i8 as *mut i8) == 0 { types = CMDType::SetCmd; } - else if command == b"setattr\0" as *const u8 as *const i8 as *mut i8{ + else if libc::strcmp(command,b"unset\0" as *const u8 as *const i8 as *mut i8) == 0 { + types = CMDType::UnSetCmd; + } + else if libc::strcmp(command,b"setattr\0" as *const u8 as *const i8 as *mut i8) == 0{ types = CMDType::SetattrCmd; - } - else if command == b"shift\0" as *const u8 as *const i8 as *mut i8{ + } + + else if libc::strcmp(command,b"readonly\0" as *const u8 as *const i8 as *mut i8) == 0{ + types = CMDType::ReadonlyCmd; + } + else if libc::strcmp(command,b"export\0" as *const u8 as *const i8 as *mut i8) == 0{ + types = CMDType::ExportCmd; + } + else if libc::strcmp(command,b"shift\0" as *const u8 as *const i8 as *mut i8) == 0{ types = CMDType::ShiftCmd; } - else if command == b"shopt\0" as *const u8 as *const i8 as *mut i8{ + + else if libc::strcmp(command,b"shopt\0" as *const u8 as *const i8 as *mut i8) == 0{ types = CMDType::ShoptCmd; } - else if command == b"source\0" as *const u8 as *const i8 as *mut i8{ + + 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 { types = CMDType::SourceCmd; } - else if command == b"suspend\0" as *const u8 as *const i8 as *mut i8{ + + else if libc::strcmp(command, b"suspend\0" as *const u8 as *const i8 as *mut i8) == 0{ types = CMDType::SuspendCmd; } - else if command == b"test\0" as *const u8 as *const i8 as *mut i8{ + + else if libc::strcmp(command,b"test\0" as *const u8 as *const i8 as *mut i8) == 0{ types = CMDType::TestCmd; } - else if command == b"times\0" as *const u8 as *const i8 as *mut i8{ + + + else if libc::strcmp(command ,b"times\0" as *const u8 as *const i8 as *mut i8) == 0 { types = CMDType::TimesCmd; } - else if command == b"trap\0" as *const u8 as *const i8 as *mut i8{ + else if libc::strcmp(command ,b"trap\0" as *const u8 as *const i8 as *mut i8) == 0 { types = CMDType::TrapCmd; } - else if command == b"type\0" as *const u8 as *const i8 as *mut i8{ + + else if libc::strcmp(command ,b"type\0" as *const u8 as *const i8 as *mut i8) == 0 { types = CMDType::TypeCmd; } - else if command == b"ulimit\0" as *const u8 as *const i8 as *mut i8{ + + else if libc::strcmp(command ,b"ulimit\0" as *const u8 as *const i8 as *mut i8) == 0{ types = CMDType::UlimitCmd; } - else if command == b"umask\0" as *const u8 as *const i8 as *mut i8{ + + else if libc::strcmp(command ,b"umask\0" as *const u8 as *const i8 as *mut i8 ) == 0{ types = CMDType::UmaskCmd; - } - else if command == b"wait\0" as *const u8 as *const i8 as *mut i8{ + } + + else if libc::strcmp(command , b"wait\0" as *const u8 as *const i8 as *mut i8) == 0 { types = CMDType::WaitCmd; - } - return types + } + + types } #[no_mangle] -pub extern "C" fn r_exec_cmd(command : *mut i8, mut list :*mut WordList) { +pub extern "C" fn r_exec_cmd(command : *mut i8, mut list :*mut WordList) -> i32 { - let commandType = get_cmd_type(command); + println!("enter r_exec_cmd"); + unsafe { + println!("command is {:?}",CStr::from_ptr(command)); + } + let commandType = unsafe {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/jobs/src/lib.rs b/bash-5.1/builtins_rust/jobs/src/lib.rs index 8f70ce26..6354a9e5 100644 --- a/bash-5.1/builtins_rust/jobs/src/lib.rs +++ b/bash-5.1/builtins_rust/jobs/src/lib.rs @@ -597,7 +597,6 @@ pub extern "C" fn r_disown_builtin (list:* mut WordList)->libc::c_int { } } -#[no_mangle] -pub extern "C" fn cmd_name() ->*const u8 { +fn cmd_name() ->*const u8 { return b"jobs" as *const u8; } diff --git a/bash-5.1/execute_cmd.c b/bash-5.1/execute_cmd.c index 77e058cf..8fae15e6 100644 --- a/bash-5.1/execute_cmd.c +++ b/bash-5.1/execute_cmd.c @@ -199,6 +199,7 @@ static int execute_pipeline PARAMS((COMMAND *, int, int, int, struct fd_bitmap * static int execute_connection PARAMS((COMMAND *, int, int, int, struct fd_bitmap *)); static int execute_intern_function PARAMS((WORD_DESC *, FUNCTION_DEF *)); +extern int r_exec_cmd PARAMS((char *, WORD_LIST *)); /* Set to 1 if fd 0 was the subject of redirection to a subshell. Global so that reader_loop can set it to zero before executing a command. */ @@ -4395,7 +4396,6 @@ execute_simple_command (simple_command, pipe_in, pipe_out, async, fds_to_close) } else words = copy_word_list (simple_command->words); - /* It is possible for WORDS not to have anything left in it. Perhaps all the words consisted of `$foo', and there was no variable `$foo'. */ @@ -4733,10 +4733,9 @@ execute_builtin (builtin, words, flags, subshell) int result, eval_unwind, ignexit_flag; int isbltinenv, should_keep; char *error_trap; - error_trap = 0; should_keep = 0; - + //r_execute_cmd(); /* The eval builtin calls parse_and_execute, which does not know about the setting of flags, and always calls the execution functions with @@ -4840,8 +4839,9 @@ execute_builtin (builtin, words, flags, subshell) executing_builtin++; executing_command_builtin |= builtin == command_builtin; - result = ((*builtin) (words->next)); - // r_execute_cmd2(words->next); + //result = ((*builtin) (words->next)); + result = r_exec_cmd(this_command_name,words->next); + //r_execute_cmd2(words->next); /* This shouldn't happen, but in case `return' comes back instead of longjmp'ing, we need to unwind. */ @@ -5354,7 +5354,9 @@ execute_builtin_or_function (words, builtin, var, redirects, redirection_undo_list = (REDIRECT *)NULL; if (builtin) + { result = execute_builtin (builtin, words, flags, 0); + } else result = execute_function (var, words, flags, fds_to_close, 0, 0); @@ -5376,15 +5378,15 @@ execute_builtin_or_function (words, builtin, var, redirects, discard = 0; if (saved_undo_list) - { - dispose_redirects (saved_undo_list); - discard = 1; - } + { + dispose_redirects (saved_undo_list); + discard = 1; + } redirection_undo_list = exec_redirection_undo_list; saved_undo_list = exec_redirection_undo_list = (REDIRECT *)NULL; if (discard) - discard_unwind_frame ("saved-redirects"); - } + discard_unwind_frame ("saved-redirects"); + } if (saved_undo_list) { @@ -5498,6 +5500,7 @@ execute_disk_command (words, redirects, command_line, pipe_in, pipe_out, if (command) { + printf("command is %s ========== ======= \n",command); /* If we're optimizing out the fork (implicit `exec'), decrement the shell level like `exec' would do. */ #if 0 /* TAG: bash-5.2 psmith 10/11/2020 */ @@ -5811,6 +5814,7 @@ shell_execve (command, args, env) char *command; char **args, **env; { + printf("wwwwwwwwwwwwwwwwwwwwwwwwww===========\n"); int larray, i, fd; char sample[HASH_BANG_BUFSIZ]; int sample_len; diff --git a/record.txt b/record.txt index 5a340212..dcb84cb0 100644 --- a/record.txt +++ b/record.txt @@ -30,3 +30,4 @@ 52 53 54 +55 -- Gitee