From 42ae2690e5b6d5a17542608a428a424eebca4721 Mon Sep 17 00:00:00 2001 From: zhanghuanhuan Date: Thu, 10 Aug 2023 15:40:38 +0800 Subject: [PATCH] builtins_rust:add alias exit help --- bash-5.1/Cargo.toml | 7 +++ bash-5.1/Makefile.in | 4 +- bash-5.1/builtins/alias.def | 4 ++ bash-5.1/builtins/builtin.c | 2 +- bash-5.1/builtins/exit.def | 6 +++ bash-5.1/builtins/help.def | 10 ++++ bash-5.1/builtins_rust/alias/Cargo.toml | 8 ++- bash-5.1/builtins_rust/alias/src/lib.rs | 68 +++++++++++-------------- bash-5.1/builtins_rust/exit/Cargo.toml | 8 +-- bash-5.1/builtins_rust/help/Cargo.toml | 5 +- bash-5.1/builtins_rust/jobs/Cargo.toml | 4 +- bash-5.1/builtins_rust/jobs/src/lib.rs | 4 +- bash-5.1/builtins_rust/trap/Cargo.toml | 8 +-- bash-5.1/support/rash.pc | 2 +- record.txt | 1 + 15 files changed, 78 insertions(+), 63 deletions(-) diff --git a/bash-5.1/Cargo.toml b/bash-5.1/Cargo.toml index dc99ad7..0a6af35 100644 --- a/bash-5.1/Cargo.toml +++ b/bash-5.1/Cargo.toml @@ -20,6 +20,9 @@ members=[ "builtins_rust/getopts", "builtins_rust/colon", "builtins_rust/builtin", + "builtins_rust/exit", + "builtins_rust/help", + "builtins_rust/alias", ] [dependencies] @@ -35,5 +38,9 @@ read = {path = "./builtins_rust/read"} #times = {path = "./builtins_rust/times"} #suspend = {path = "./builtins_rust/suspend"} # test = {path = "./builtins_rust/test"} +#trap = {path = "./builtins_rust/trap"} rcolon = {path = "./builtins_rust/colon"} builtin = {path = "./builtins_rust/builtin"} + #alias= {path = "./builtins_rust/alias"} + exit = {path = "./builtins_rust/exit"} + rhelp = {path = "./builtins_rust/help"} diff --git a/bash-5.1/Makefile.in b/bash-5.1/Makefile.in index 565abd3..1856cf0 100644 --- a/bash-5.1/Makefile.in +++ b/bash-5.1/Makefile.in @@ -138,7 +138,7 @@ LOCAL_DEFS = @LOCAL_DEFS@ LOCALE_DEFS = -DLOCALEDIR='"$(localedir)"' -DPACKAGE='"$(PACKAGE)"' LOCAL_LIBS = @LOCAL_LIBS@ -LIBS = $(BUILTINS_LIB) $(LIBRARIES) @LIBS@ -lrt -lpthread -L./target/debug -lrjobs -lrread -lrcd -lrfg_bg -lrfc -lrgetopts -lrcolon -lrbuiltin +LIBS = $(BUILTINS_LIB) $(LIBRARIES) @LIBS@ -lrt -lpthread -L./target/debug -lrjobs -lrread -lrcd -lrfg_bg -lrfc -lrgetopts -lrcolon -lrbuiltin -lrexit -lrhelp -lralias LIBS_FOR_BUILD = @@ -574,7 +574,7 @@ OTHER_INSTALLED_DOCS = CHANGES COMPAT NEWS POSIX RBASH README LOADABLES_DIR = ${top_builddir}/examples/loadables RUST_DIR = $(top_builddir)/builtins_rust -RUST_BUILTINS_DIRS = $(RUST_DIR)/jobs $(RUST_DIR)/read $(RUST_DIR)/cd $(RUST_DIR)/fc $(RUST_DIR)/fg_bg $(RUST_DIR)/getopts $(RUST_DIR)/colon $(RUST_DIR)/builtin +RUST_BUILTINS_DIRS = $(RUST_DIR)/jobs $(RUST_DIR)/read $(RUST_DIR)/cd $(RUST_DIR)/fc $(RUST_DIR)/fg_bg $(RUST_DIR)/getopts $(RUST_DIR)/colon $(RUST_DIR)/builtin $(RUST_DIR)/exit $(RUST_DIR)/help $(RUST_DIR)/alias #RUST_TARGET_LIB = $(top_builddir)/target/debug/librjobs.a $(top_builddir)/target/debug/librread.a diff --git a/bash-5.1/builtins/alias.def b/bash-5.1/builtins/alias.def index 0ab9069..4154213 100644 --- a/bash-5.1/builtins/alias.def +++ b/bash-5.1/builtins/alias.def @@ -64,12 +64,14 @@ $END #define AL_REUSABLE 0x01 static void print_alias PARAMS((alias_t *, int)); +extern int r_alias_builtin ( register WORD_LIST *list); /* Hack the alias command in a Korn shell way. */ int alias_builtin (list) WORD_LIST *list; { + return r_alias_builtin(list); int any_failed, offset, pflag, dflags; alias_t **alias_list, *t; char *name, *value; @@ -166,10 +168,12 @@ $END #if defined (ALIAS) /* Remove aliases named in LIST from the aliases database. */ +extern int r_unalias_builtin ( register WORD_LIST *list); int unalias_builtin (list) register WORD_LIST *list; { +return r_unalias_builtin (list); register alias_t *alias; int opt, aflag; diff --git a/bash-5.1/builtins/builtin.c b/bash-5.1/builtins/builtin.c index dee9ef5..eff88dc 100644 --- a/bash-5.1/builtins/builtin.c +++ b/bash-5.1/builtins/builtin.c @@ -16,7 +16,7 @@ #include "common.h" #include "bashgetopt.h" -extern int r_builtin_builtin (WORD_LIST *list); +extern int r_builtin_builtin ( WORD_LIST *list); /* Run the command mentioned in list directly, without going through the normal alias/function/builtin/filename lookup process. */ diff --git a/bash-5.1/builtins/exit.def b/bash-5.1/builtins/exit.def index dc6f3d5..d79bcd6 100644 --- a/bash-5.1/builtins/exit.def +++ b/bash-5.1/builtins/exit.def @@ -53,10 +53,13 @@ extern int check_jobs_at_exit; static int exit_or_logout PARAMS((WORD_LIST *)); static int sourced_logout; +int r_exit_builtin ( WORD_LIST *list); int exit_builtin (list) WORD_LIST *list; { +printf("exit_builtin\n"); +r_exit_builtin(list); CHECK_HELPOPT (list); if (interactive) @@ -77,11 +80,14 @@ Exits a login shell with exit status N. Returns an error if not executed in a login shell. $END +int r_logout_builtin ( WORD_LIST *list); /* How to logout. */ int logout_builtin (list) WORD_LIST *list; { +printf("LOGOUT_BUILTIN\n"); + r_logout_builtin(list); CHECK_HELPOPT (list); if (login_shell == 0 /* && interactive */) diff --git a/bash-5.1/builtins/help.def b/bash-5.1/builtins/help.def index f33b2b9..6246919 100644 --- a/bash-5.1/builtins/help.def +++ b/bash-5.1/builtins/help.def @@ -88,10 +88,20 @@ static void show_longdoc PARAMS((int)); /* Print out a list of the known functions in the shell, and what they do. If LIST is supplied, print out the list which matches for each pattern specified. */ +extern int r_help_builtin ( WORD_LIST *list); +extern int r_help_null_builtin ( WORD_LIST *list); int help_builtin (list) WORD_LIST *list; { + if (list) + { + return r_help_builtin(list); + } + else + { + return r_help_null_builtin(list); + } register int i; char *pattern, *name; int plen, match_found, sflag, dflag, mflag, m, pass, this_found; diff --git a/bash-5.1/builtins_rust/alias/Cargo.toml b/bash-5.1/builtins_rust/alias/Cargo.toml index 9669a4d..339bc53 100644 --- a/bash-5.1/builtins_rust/alias/Cargo.toml +++ b/bash-5.1/builtins_rust/alias/Cargo.toml @@ -1,16 +1,14 @@ [package] -name = "ralias" +name = "alias" version = "0.1.0" -edition = "2018" +edition = "2021" authors = ["liutong"] [lib] name = "ralias" -crate-type = ["staticlib","rlib"] +crate-type = ["staticlib"] [dependencies] libc = "0.2" -rcmd = {path="../cmd"} -rcommon= {path="../common"} # nix = "0.23" diff --git a/bash-5.1/builtins_rust/alias/src/lib.rs b/bash-5.1/builtins_rust/alias/src/lib.rs index 7b53601..3e02eb7 100644 --- a/bash-5.1/builtins_rust/alias/src/lib.rs +++ b/bash-5.1/builtins_rust/alias/src/lib.rs @@ -1,3 +1,4 @@ + use std::ffi::CStr; extern "C" { fn free(__ptr: *mut libc::c_void); @@ -67,23 +68,15 @@ pub struct alias { pub flags: libc::c_char, } pub type alias_t = alias; - -pub static AL_REUSABLE:i32 = 0x01; -pub static EX_USAGE:i32 = 258; -//extern crate rcommon; -use rcommon::EXECUTION_SUCCESS; -use rcommon::r_builtin_usage; - - #[no_mangle] pub unsafe extern "C" fn r_alias_builtin(mut list: *mut WORD_LIST) -> libc::c_int { println!("alias_builtin run!"); - let mut any_failed = 0; - let mut offset = 0; - let mut pflag = 0; - let mut dflags = 0; - let mut alias_list: *mut *mut alias_t; - let mut t: *mut alias_t; + let mut any_failed: libc::c_int = 0; + let mut offset: libc::c_int = 0; + let mut pflag: libc::c_int = 0; + let mut dflags: libc::c_int = 0; + let mut alias_list: *mut *mut alias_t = 0 as *mut *mut alias_t; + let mut t: *mut alias_t = 0 as *mut alias_t; let mut name: *mut libc::c_char = 0 as *mut libc::c_char; let mut value: *mut libc::c_char = 0 as *mut libc::c_char; dflags = if posixly_correct != 0 { 0 as libc::c_int } else { 0x1 as libc::c_int }; @@ -99,42 +92,38 @@ pub unsafe extern "C" fn r_alias_builtin(mut list: *mut WORD_LIST) -> libc::c_in } match offset as u8 { b'p' => { - pflag = 1 ; - dflags |= AL_REUSABLE; + pflag = 1 as libc::c_int; + dflags |= 0x1 as libc::c_int; } _ => { - if offset == -99 { - builtin_help(); - return EX_USAGE; - } - r_builtin_usage(); - return EX_USAGE; + builtin_usage(); + return 258 as libc::c_int; } } } list = loptend; if list.is_null() || pflag != 0 { if aliases.is_null() { - return EXECUTION_SUCCESS; + return 0 as libc::c_int; } alias_list = all_aliases(); if alias_list.is_null() { - return EXECUTION_SUCCESS; + return 0 as libc::c_int; } - offset = 0; + offset = 0 as libc::c_int; while !(*alias_list.offset(offset as isize)).is_null() { print_alias(*alias_list.offset(offset as isize), dflags); offset += 1; } free(alias_list as *mut libc::c_void); if list.is_null() { - return sh_chkwrite(EXECUTION_SUCCESS); + return sh_chkwrite(0 as libc::c_int); } } - any_failed = 0; + any_failed = 0 as libc::c_int; while !list.is_null() { name = (*(*list).word).word; - offset = 0; + offset = 0 as libc::c_int; while *name.offset(offset as isize) as libc::c_int != 0 && *name.offset(offset as isize) as libc::c_int != '=' as i32 { @@ -143,7 +132,7 @@ pub unsafe extern "C" fn r_alias_builtin(mut list: *mut WORD_LIST) -> libc::c_in if offset != 0 && *name.offset(offset as isize) as libc::c_int == '=' as i32 { *name.offset(offset as isize) = '\u{0}' as i32 as libc::c_char; value = name.offset(offset as isize).offset(1 as libc::c_int as isize); - if legal_alias_name(name, 0) == 0 { + if legal_alias_name(name, 0 as libc::c_int) == 0 as libc::c_int { builtin_error( dcgettext( 0 as *const libc::c_char, @@ -168,7 +157,7 @@ pub unsafe extern "C" fn r_alias_builtin(mut list: *mut WORD_LIST) -> libc::c_in } list = (*list).next; } - return if any_failed != 0 { rcommon::EXECUTION_FAILURE!()} else { EXECUTION_SUCCESS}; + return if any_failed != 0 { 1 as libc::c_int } else { 0 as libc::c_int }; } #[no_mangle] pub unsafe extern "C" fn r_unalias_builtin(mut list: *mut WORD_LIST) -> libc::c_int { @@ -185,28 +174,28 @@ pub unsafe extern "C" fn r_unalias_builtin(mut list: *mut WORD_LIST) -> libc::c_ if !(opt != -(1 as libc::c_int)) { break; } - match opt as u8{ - b'a' => { + match opt { + 97 => { aflag = 1 as libc::c_int; } + -99 => { + builtin_help(); + return 258 as libc::c_int; + } _ => { - if opt == -99 { - builtin_help(); - return EX_USAGE; - } builtin_usage(); - return EX_USAGE; + return 258 as libc::c_int; } } } list = loptend; if aflag != 0 { delete_all_aliases(); - return 0; + return 0 as libc::c_int; } if list.is_null() { builtin_usage(); - return EX_USAGE; + return 258 as libc::c_int; } aflag = 0 as libc::c_int; while !list.is_null() { @@ -240,4 +229,5 @@ unsafe extern "C" fn print_alias(mut alias: *mut alias_t, mut flags: libc::c_int println!("{}={}", CStr::from_ptr((*alias).name).to_string_lossy().into_owned(), CStr::from_ptr(value).to_string_lossy().into_owned()); free(value as *mut libc::c_void); + } diff --git a/bash-5.1/builtins_rust/exit/Cargo.toml b/bash-5.1/builtins_rust/exit/Cargo.toml index 9971aed..5b87960 100644 --- a/bash-5.1/builtins_rust/exit/Cargo.toml +++ b/bash-5.1/builtins_rust/exit/Cargo.toml @@ -1,8 +1,8 @@ [package] -name = "rexit" +name = "exit" version = "0.1.0" -edition = "2018" - +edition = "2021" +#build = "../build.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -13,5 +13,5 @@ rjobs = {path="../jobs"} [lib] -crate-type = ["staticlib","rlib"] +crate-type = ["staticlib"] name = "rexit" diff --git a/bash-5.1/builtins_rust/help/Cargo.toml b/bash-5.1/builtins_rust/help/Cargo.toml index 50f09b2..77774e0 100644 --- a/bash-5.1/builtins_rust/help/Cargo.toml +++ b/bash-5.1/builtins_rust/help/Cargo.toml @@ -2,7 +2,8 @@ authors = ["zhanghuanhuan"] name = "rhelp" version = "0.0.1" -edition = "2018" +build = "../build.rs" +edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] @@ -10,5 +11,5 @@ libc = "0.2" nix = "0.24.1" [lib] -crate-type = ["staticlib","rlib"] +crate-type = ["staticlib"] name = "rhelp" diff --git a/bash-5.1/builtins_rust/jobs/Cargo.toml b/bash-5.1/builtins_rust/jobs/Cargo.toml index 820ebeb..c5c96c0 100644 --- a/bash-5.1/builtins_rust/jobs/Cargo.toml +++ b/bash-5.1/builtins_rust/jobs/Cargo.toml @@ -2,7 +2,6 @@ authors = ["huzhengming"] name = "rjobs" version = "0.0.1" -build = "../build.rs" edition = "2021" @@ -12,6 +11,5 @@ libc = "0.2" nix = "0.23.0" [lib] -crate-type = ["staticlib"] -#crate-type = ["cdylib"] +crate-type = ["staticlib","rlib"] name = "rjobs" diff --git a/bash-5.1/builtins_rust/jobs/src/lib.rs b/bash-5.1/builtins_rust/jobs/src/lib.rs index 2f97485..3f74083 100644 --- a/bash-5.1/builtins_rust/jobs/src/lib.rs +++ b/bash-5.1/builtins_rust/jobs/src/lib.rs @@ -13,8 +13,8 @@ pub struct WORD_DESC { #[repr(C)] #[derive(Copy,Clone)] pub struct WORD_LIST { - next: *mut WORD_LIST, - word: *mut WORD_DESC + pub next: *mut WORD_LIST, + pub word: *mut WORD_DESC } #[repr(i8)] diff --git a/bash-5.1/builtins_rust/trap/Cargo.toml b/bash-5.1/builtins_rust/trap/Cargo.toml index 9ac4419..3845c56 100644 --- a/bash-5.1/builtins_rust/trap/Cargo.toml +++ b/bash-5.1/builtins_rust/trap/Cargo.toml @@ -1,15 +1,15 @@ [package] -name = "rtrap" +name = "trap" version = "0.1.0" -edition = "2018" +edition = "2021" authors = ["lvgenggeng"] - +build = "../build.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [lib] name = "rtrap" -crate-type = ["staticlib","rlib"] +crate-type = ["staticlib"] [dependencies] libc = "0.2" diff --git a/bash-5.1/support/rash.pc b/bash-5.1/support/rash.pc index 43e72d1..4678e63 100644 --- a/bash-5.1/support/rash.pc +++ b/bash-5.1/support/rash.pc @@ -1,4 +1,4 @@ -# rush.pc.in +# rash.pc.in prefix=/usr/local exec_prefix=${prefix} diff --git a/record.txt b/record.txt index e2fdd0a..4957a05 100644 --- a/record.txt +++ b/record.txt @@ -30,3 +30,4 @@ 29 30 31 +32 -- Gitee