diff --git a/bash-5.1/Cargo.toml b/bash-5.1/Cargo.toml index 0c967670afade1c367ba5e87fb5f37d845513d73..595ff04a526f3c51f01f20ab870596e1678de811 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 3ea296c012dfd886dc9eb74e134086374264f86c..019ead7c23d64a18534266d8c550e98f9ad9e30d 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 -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 = $(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/break.def b/bash-5.1/builtins/break.def index f71bc0de6abbf009efd8a9e22bdef83bdcdc064b..346b632def2e473aa80c544fb31e73b8e025dd76 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 f538122dabbb1c70c075b8c6d0644c0c6aaddd1d..acd46cc1be0062d1ca15777821e1b309645bdb3e 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 973341975268e40afc0c211c6e9f33635861708d..28a9ec2f9a5ee9869056016a3a1543fed025f293 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 aee4afb1be2a76a68d24da5835bbdc5e48802ae8..21e4516d194abbc5eb4b0abdd01c4f3858b300e1 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 930ad5007ad0890c8ff3cf91743787b5b4e3227b..5db44c2c2906e6e76df17af781c545f362e1967f 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 f4dad26084075bf4eddfa34c7648f59667fef0db..d090a45bc51f2f1e032413a9e0a3a049407bb85d 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 831aeb39ca68a53e440b74f9417500934658af33..65c3cb4f35ea9076f5373f085a743355b9d7565f 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 5b51a97f56311dcf94f5d6823561ed98283e3e82..0a5f4897f34ca1819b64413fe937bbdb120ec802 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 de1bb2df1809c71e1e0636837fedfe554c52e358..829f827d5dc7c3d2e2cd7852e6e0b5224b9f6d49 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 bc2441082690d65b76ca9654ba796865603c7ad4..03c98eb11a5d4aabe237b0156d7569a458b53978 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 32584dd7ed04678fc436b04e3535834c016c5d18..bb9af01b51342fd002083123f843364087c23285 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 930d1a882a6216b1ef0f94af1e5fb68c162e5c64..5b2f994db414d08a87cc64e5e0f706f4ee9e3d48 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 c190383d6cde298f5ef8f77cbe07dc31b233d1ec..bd9a203b656800f28854f766d2755f6a78d7f1a3 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 f8391c5befe001fd27def7cd67da107a2f802858..f31f43331e96a8cb5a5961d75c4696ef0920e229 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 d2ba5918ebf91ad6849b23ced361de438b0487c7..daeec9eafe809fef9a4632484965d6321c993337 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 0bc7078cdc91da96d0adfc5d9a23fde39e03b86e..d9e5cfce5b76fea847abbd2ff0240e551f12fdcd 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); @@ -32,6 +29,20 @@ extern "C" { } 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 { @@ -61,6 +72,9 @@ pub type AliasT = alias; pub static AL_REUSABLE:i32 = 0x01; +//extern crate rcommon; +use rcommon::{EXECUTION_SUCCESS, EX_USAGE, EXECUTION_FAILURE}; +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 69fd41bec99397582a9b1bacb681cfaeb6eadffb..7dca3aa27df4c6f749b153cbad59ce14516c44d2 100644 --- a/bash-5.1/builtins_rust/bind/src/lib.rs +++ b/bash-5.1/builtins_rust/bind/src/lib.rs @@ -93,11 +93,6 @@ macro_rules! ANYOTHERKEY{ 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 +137,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 +159,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 = CString::new("line editing not enabled").unwrap(); + //let s_ptr = s_str.as_ptr() as *const c_char; + builtin_warning(s_str.as_ptr()); } kmap = std::ptr::null_mut(); @@ -193,16 +182,12 @@ pub extern "C" fn r_bind_builtin(mut list:*mut WordList)->i32{ 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(); @@ -247,10 +232,7 @@ 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/break_1/Cargo.toml b/bash-5.1/builtins_rust/break_1/Cargo.toml index 085d195ef9744a42dd817b5640401c107e756e1a..9871a47341c7e578899e72c3274c099227b0b8ef 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 a2f0a636195761e535f6bbd335f1db5866c418b7..c7b6760d32ea48b3c2b83c61b7b974de6c7406bb 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 9301c7dcd009457302aecbbd303f57108b28af44..c09d949752adfc65796e7882b67548f14854e8d7 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 6882fb9e1540d96c90ae8bec8653090721af66b4..661c38692303094cb2932a62170964c7efcb8c23 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 017a2a2e526d7e323765eee7a240cf0e8ca8ac70..71639d3922e23bbcfac06bdbeab83b244e1e32df 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 fe503dc2606224a8ce54bde544d86a1f3678b4a7..ef4d55d01c27ec625bf10c348ef40ec51003dca6 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 a888b5381dc63e71e6a9ca01e62fb775a0b13445..524ba95d6ea1ec838ed491ce4838e9b0dd8b730f 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 56ccd74535501fa2928fcedfe648cd6c4844f322..22ac06164d0ea09371345f2d4ef7b1b7c81e642d 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 1025580eb7aa141b5a54f4579699791ecc423550..72ebf5695244a09ffd3606193d3006921a82a6ec 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 dea395cb1e76818296846d5df4879168459a2ebf..3df1f22c7b2cbdb101a183d3d8758bed20aa0573 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 393088e96ede79e78474687505fc41faf77c5683..faea39698d5f58abfa3c107828d92ac83ab1d3d8 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,15 +10,30 @@ 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: *mut WordList )->i32 { +pub extern "C" fn r_colon_builtin(ignore:WordList)->i32 { println!("in r_colon_builtin"); 0 } #[no_mangle] -pub extern "C" fn r_false_builtin(ignore: *mut WordList) -> i32 { +pub extern "C" fn r_false_builtin(ignore: 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 d390905eefa11437e94991f8a574483cf2baefc3..13ff0ef0044e26d211a65d1848be68b9104457a6 100644 --- a/bash-5.1/builtins_rust/command/Cargo.toml +++ b/bash-5.1/builtins_rust/command/Cargo.toml @@ -7,8 +7,7 @@ edition = "2018" [dependencies] libc ="*" -rcommon = {path="../common"} [lib] crate-type = ["staticlib", "rlib"] -name = "command" +name = "rcommand" diff --git a/bash-5.1/builtins_rust/command/src/lib.rs b/bash-5.1/builtins_rust/command/src/lib.rs index b7f593deeadd6d95c9875232fbc8a00b5c1b3500..bf3989677261268364079106baffe8f5099a3c7c 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 fa1361f8f56d1d108639e1dc065001ed3519ae37..4a3a35e855f58dc27eb12a329e93456ac76ae9a4 100644 --- a/bash-5.1/builtins_rust/common/src/lib.rs +++ b/bash-5.1/builtins_rust/common/src/lib.rs @@ -8,9 +8,7 @@ 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 58cadc61ad0fe38c3f24970e3db1b71c1547f779..e18cffe9dc0553cfe8adea5db14a26be5d53a324 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 9dd58df4447e7ed94457e37ea33b672002f9442f..acfe6f50f92dbf0cb0494d0208d22b116657b147 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 eede3a82011608e5b881dbe1b6343bb3b3550b8d..b44473ab58b14abb030cbead0e24c9d92a8d5b40 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 31720c0d70bb621d272e1c95ec14127a5a2409a6..65f3bf2fb66895ad87436545d9c87980e06c1544 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 79a74fe95be096797255fb6a5ea6a25840d6d81e..5e69facdc2b9db9f7cedc4df1b1e9a631834abfe 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 99eea4b48a118a854ce5cf192719308c5ec03094..1c2bc1708a763056e7c3499c8c36eedcdd6ac56a 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 1edb81233dc11c52d8692fb10bef34ce7cee516e..2df3366e296a6f0706595d9e27f1768bcbbc78f1 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 33fcbb94d272a244e5019387459d2531d9195b3d..4d6609ef1152d08cbb605189171147d363bb7663 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 ca8399dbda1e308c7226d6e410baf7fc3fdf4957..691397090b2fecd1a4bfc392f144d7d8d3e57108 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/src/lib.rs b/bash-5.1/builtins_rust/exit/src/lib.rs index 0f74aab5b95990732227ea531c6780bcd46de023..3fe496c94c2c45ed6a9e2c37c0103e4adc329834 100644 --- a/bash-5.1/builtins_rust/exit/src/lib.rs +++ b/bash-5.1/builtins_rust/exit/src/lib.rs @@ -6,8 +6,7 @@ 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 rcommon::{ WordList}; #[repr(C)] pub struct JOB { @@ -59,6 +58,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 +153,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 +182,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 +270,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 70adc426c5f91082b9ff482832455a387cb88615..499532ab003f836f3c978e28b638b1928505206a 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 5d0eb3fcd91a5f7c48574b5291f41da800035aed..9d97c2e0fac6fb83a0192be4cf0ddb0f2805cce5 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 f054950a21edec34b603af084ca97ce030396c01..26f78f39bdf8adcc2b57bccc47b81a20dd67bfec 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 b86636ccbe387ed8f512f09c76f7a20d7fe6c553..7780ea47bf6aea6eb384042c86838bcbe43e5a49 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 07044a6db5506a590511a5bebacd6e1607c72e6d..ca691a19227afd0d321ffa8543acc0703f1f1056 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 1ecc59b32777ec410f68c75b0b6cec051e793653..a133a5d7d0baf42271d524c24079de44cf56e3df 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 698a3f95897fdc18c2804461078a77a2c518f6c3..142514f46c420989050e3c8afd51f8ac30bdf8c2 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 993d13e0d1533cd1e18757cb8589fb5f3952316e..50f09b29ea77657d58f295b0d95fdd3bb628e846 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 16bedff4edda98f7805d28e1811873f7391a20df..b215aacbca5ff9e2aa0b3f76745d6b23bc67ab71 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 a1c6d3d18cb77159890474cd36359a3d467de6ad..5ea73daf22ae470ee472a5fb7cd5bce400eef592 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 37caa34665bfa1594beba574e9bab53759899ece..077183ef8014c704478c2c4f73aca9296e7ff36e 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 6354a9e5769782a5533015e54413e3c0f73d4657..ce7f97b6e41dde849a0d17a4fdb22d1fb925e85b 100644 --- a/bash-5.1/builtins_rust/jobs/src/lib.rs +++ b/bash-5.1/builtins_rust/jobs/src/lib.rs @@ -597,6 +597,14 @@ pub extern "C" fn r_disown_builtin (list:* mut WordList)->libc::c_int { } } -fn cmd_name() ->*const u8 { +#[no_mangle] +pub extern "C" fn cmd_name() ->*const u8 { return b"jobs" as *const u8; } +/* +#[no_mangle] + +pub extern "C" fn run(list : *mut 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 5d8d8f77669adb2eb9783c1290955eed901f42b4..6ee88c638958d14cd9593b8e563fa74bee90b8ae 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 8450248d09589efc52448bfe539382c28fadf17c..d1d39561abfda380d46b6e62d4bc617ec5ab3e0e 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 4fa35f5b1b343e7ebedf6b75ee10128a879898ad..512d37f349516f9930c8360eae6203fadb681466 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 db66567dcfc63e9081874bfb07771a0f07a70f80..5fd1594045906f6675ece7468bb9a4e6f08c75a1 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 1a29c02d29c0eb4862f96d059430b2625f18370e..ce4971ffa96ee72834cb95c8efd6303ddb3ce400 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 51804e8276869208463dd1bd909ba1f634b97dda..cfd6cdc058f65f2e785a7208491835b4c4fc826e 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 08d04fa57d45a72c4b5baf5e3e48a06057567460..8a823e25f06b5a76d3966b807b261e68cefbd279 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 4ea450bce04cb2bc6cd064d7d8b1e1368724f725..2fed8da36bfd5e48de34d8e1da7784d4fb2c10be 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 946f8f9c41f6b3b83bc043fc308a1d005b9c07ea..6fcffeea12a1d1dd2007509dbb9ffd4c43da929e 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 b52101cab52d92ce7b9ddb1a90791237124ba2a3..c468265b2b38f149a4e8785644550422903d91c1 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 d37ae34bc367b7e15245baa881c25ee85051fd34..fda0e61a078cc0911365079533cfa55b133a2388 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 a437a2628cf64df7f2f97f6a8de50e845c9dc751..0a60e09d1e0bb572c6352a1fe64fdf28cf529805 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 32066f4e08f081832c20ce9a18c86a0d6feffd97..24ab4ecc8e53c32cbf770fdd59f5e54ae944c6c4 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 90004e4d728755daf4f9b44e5dc3c405edc7d9ad..1e1d381ea1ba1ef04b23a0b4682d27406fe492b4 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 8c4655937ba5aea202fd59998c0c25838c719c1a..98a323e01fa0ac0541ba0565a6a877e1f7ec74bb 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 7130cc619efba5d49b95ee4ed3c8249d1cf9329f..5e0851b52a14bf6b751662c63ad45ea28cc2edf9 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 c557ab770e664b3cc19575d38c2ecfc052ed6002..ff0a3197dcd121a16cc65e5ce99f34e1a648151e 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 8057a26f1cf24c6ac5234872cfda8f05b185dd84..91e316311ecb894664071ff1d1f7ebaebd17ce3b 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 a33457a46170f4c26f503b8e1cc0c348c6fab99e..53de38f7a16c3c5a7a9e38388b0f7ee2b36efa4a 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 c2f5d3213ba44c8fff14c155c067b46939b25667..eb18f3826b723b0d47faca93f06f27d7b6a9b55d 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 01ab1e935ed02420cc68410c9e3c1359a8db177c..08ffe400978a9e835162130207d93b7871807a07 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 c2318bfdca1f4188edd853ca1959f995b0624ce1..d4374c95bc4b899c80d3af44f667f43db9348fb6 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 0e03d966d164e20d4ae41f9ada30774a16fe745d..4fbd136f12bf563305b72d9692aa701483bf9937 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 2ed26a2227a7f2d15db6dd1db44009b07ad84bcf..adf0be5eb6c918cb44b50a24cb54ede864d07a1e 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 6e153736fdd426f78175e57aab61996ac18c7e3c..9f2b72a3f0c720047d6451891beb18fa595be526 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 79362038aef94d30cdd07e50d9aa2f554ca2bf97..98d236aeae660b0b5cd919b6cb5737dee84d167a 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/bash-5.1/execute_cmd.c b/bash-5.1/execute_cmd.c index 8fae15e6f29cffd6f85fe49edd2598be8982fe48..77e058cf7890670c26f37492facfa6944814edea 100644 --- a/bash-5.1/execute_cmd.c +++ b/bash-5.1/execute_cmd.c @@ -199,7 +199,6 @@ 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. */ @@ -4396,6 +4395,7 @@ 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,9 +4733,10 @@ 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 @@ -4839,9 +4840,8 @@ execute_builtin (builtin, words, flags, subshell) executing_builtin++; executing_command_builtin |= builtin == command_builtin; - //result = ((*builtin) (words->next)); - result = r_exec_cmd(this_command_name,words->next); - //r_execute_cmd2(words->next); + result = ((*builtin) (words->next)); + // r_execute_cmd2(words->next); /* This shouldn't happen, but in case `return' comes back instead of longjmp'ing, we need to unwind. */ @@ -5354,9 +5354,7 @@ 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); @@ -5378,15 +5376,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) { @@ -5500,7 +5498,6 @@ 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 */ @@ -5814,7 +5811,6 @@ 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 64ca5b7e450936d8f2bc4baef864bc262588098b..07c611e1752d082283ff3fc603320aa219b7fafd 100644 --- a/record.txt +++ b/record.txt @@ -49,3 +49,4 @@ 48 49 50 +51