From 02d3bd9f6eabb00885dc5b4ab35456d2c7a732d5 Mon Sep 17 00:00:00 2001 From: Ari Archer Date: Sat, 22 Oct 2022 17:12:04 +0300 Subject: [PATCH] 2.8.0: Improve performance, write baz-cat in C and corectness fixes Signed-off-by: Ari Archer --- baz | 23 ++++++++++++++--------- baz-cat.c | 32 ++++++++++++++++++++++++++++++++ baz-cat.cc | 33 --------------------------------- loader.sht | 9 +++++---- scripts/baz-cat-build.sh | 2 +- 5 files changed, 52 insertions(+), 47 deletions(-) create mode 100644 baz-cat.c delete mode 100644 baz-cat.cc diff --git a/baz b/baz index 1a10422..709586a 100755 --- a/baz +++ b/baz @@ -2,7 +2,7 @@ [ "$BAZ_DEBUG" ] && set -x -export BAZ_VERSION='2.7.0' +export BAZ_VERSION='2.8.0' export BAZ_DIR="$HOME/.local/share/baz" export BAZ_CONFDIR="$HOME/.config/baz" export BAZ_CONF="$BAZ_CONFDIR/config.env" @@ -27,6 +27,7 @@ export BAZ_LOADER_TEMPLATE="$BAZP_INITDIR/loader.sht" entry() { printf " + %-35s%s\n" "$1" "$2"; } log() { [ "$BAZP_NO_LOG" = true ] || ${2:-echo} " * $1"; } +elog() { log "$1" >&2; } eecho() { [ "$BAZP_NO_LOG" = true ] || echo "$1" >&2; } unexpand_path() { echo "${1/"$HOME"/"${2:-~}"}"; } baz_git_clone() { git clone --quiet --recursive -- "$1" "$2"; } @@ -89,7 +90,7 @@ use() { } error() { - log "Error: $1" >&2 + elog "Error: $1" exit 1 } @@ -435,7 +436,7 @@ bazp_install_gits() { baz_help() { [ "$BAZP_NO_LOG" = true ] && return - log "Help for baz v$BAZ_VERSION" + elog "Help for baz v$BAZ_VERSION" { log 'Subcommands:' @@ -671,7 +672,7 @@ baz_list() { { while read -r dir; do _plugent="$(get_base "$dir")" - [ -f "$dir/disabled" ] && _plugent+=' [DISABLED]' >&2 + [ -f "$dir/disabled" ] && _plugent+=' [DISABLED]' log "$_plugent" done <<<"$(find "${BAZ_PLUGDIR:?}" -maxdepth 1 -not -path "${BAZ_PLUGDIR:?}" -type d)" @@ -771,17 +772,17 @@ main() { if [ -f "$BAZ_CONF" ]; then . "$BAZ_CONF" else - log 'Warning: no config found' + elog 'Warning: no config found' fi if [ "$BAZ_LOADER_VERSION" ] && [ "$BAZ_LOADER_VERSION" != "$BAZ_VERSION" ]; then - log "Baz loader version ($BAZ_LOADER_VERSION) does not match current version ($BAZ_VERSION)" + elog "Baz loader version ($BAZ_LOADER_VERSION) does not match current version ($BAZ_VERSION)" fi BAZP_NO_LOG=true bazp_reset_env - case "$1" in - help) baz_help ;; + case "${1//[[:space:]]/}" in + help) baz_help 2>&1 ;; setup) baz_setup ;; unsetup) baz_unsetup ;; install) baz_install "$2" "${@:3}" ;; @@ -793,8 +794,12 @@ main() { version) echo "v$BAZ_VERSION" ;; disable) baz_disable "${@:2}" ;; enable) baz_enable "${@:2}" ;; + '') + baz_help + exit 2 + ;; *) - log "Error: subcommand '$1' is not valid" + elog "Error: subcommand '$1' is not valid" baz_help 2>&1 | indent >&2 exit 1 ;; diff --git a/baz-cat.c b/baz-cat.c new file mode 100644 index 0000000..3104917 --- /dev/null +++ b/baz-cat.c @@ -0,0 +1,32 @@ +#include + +/* + * Improved performance, C89 compatible cat(1) for `baz` usage, + * this will read input and then print it + */ + +int main(void) { + static char c; + + /* + * This is kind of an implementation of puts lol, + * it doesn't really slow down the program, but + * I have to read it char-by-char to support + * unicode + */ + while ((c = getchar()) != EOF) + putchar(c); + +#ifdef MANUAL_FLUSH + /* + * Enable this with -DMANUAL_FLUSH if you want + * to manually flush stdout on call, although + * C already has auto flushing to cover most + * cases and this just slows the program down + */ + + fflush(stdout); +#endif + + return 0; +} diff --git a/baz-cat.cc b/baz-cat.cc deleted file mode 100644 index e65802a..0000000 --- a/baz-cat.cc +++ /dev/null @@ -1,33 +0,0 @@ -#include -#include - -/* - * Improved performance, C++98 compatible cat(1) for `baz` usage, - * this will read input and then print it - */ - -int main(void) { - static std::string s, l; - - while (true) { - if (!std::getline(std::cin, l)) - break; - - s += l + '\n'; - } - - std::cout << s; - -#ifdef MANUAL_FLUSH - /* - * Enable this with -DMANUAL_FLUSH if you want - * to manually flush stdout on call, although - * C++ already has auto flushing to cover most - * cases and this just slows the program down - */ - - std::cout.flush(); -#endif - - return 0; -} diff --git a/loader.sht b/loader.sht index 15d276b..4415b91 100644 --- a/loader.sht +++ b/loader.sht @@ -35,8 +35,9 @@ __baz_sanitize() { # the diff is around 104ms set -- "${1%"${1##*[!/]}"}" - local base="${1##*/}" - local tmp="${base//[[:space:]\"\'~!#\\$%^&*\/()=]/}" + + local tmp="${1##*/}" + tmp="${tmp//[[:space:]\"\'~!#\\$%^&*\/()=]/}" echo "${tmp//../}" } @@ -99,8 +100,8 @@ __baz_load_functions() { __baz_vecho "Loading function '$_func_name'" eval "function $_func_name() { - $(<"$_baz_func") - }" + $(<"$_baz_func") + }" done } diff --git a/scripts/baz-cat-build.sh b/scripts/baz-cat-build.sh index ad2b813..0fb4113 100755 --- a/scripts/baz-cat-build.sh +++ b/scripts/baz-cat-build.sh @@ -3,7 +3,7 @@ set -xe main() { - ${CXX:-clang++} $CXXFLAGS -flto -Ofast -ffunction-sections -fdata-sections -s -std=c++98 -Wall -Wextra -Wpedantic -Wshadow -Werror -pedantic -march=native -pipe -o baz-cat baz-cat.cc + ${CC:-clang} $CFLAGS -flto -Ofast -ffunction-sections -fdata-sections -s -std=c89 -Wall -Wextra -Wpedantic -Wshadow -Werror -pedantic -march=native -pipe -o baz-cat baz-cat.c ${STRIP:-strip} --strip-all --remove-section=.note --remove-section=.gnu.version --remove-section=.comment --strip-debug --strip-unneeded baz-cat }