From 7bba6cd7612293796e905885f9ed3072877798ab Mon Sep 17 00:00:00 2001 From: davidovski Date: Mon, 27 Jun 2022 01:14:42 +0100 Subject: added shmk, building all with shmk --- Makefile | 77 ++------ build.shmk | 22 +++ shmk.shmk | 9 + src/colors.h | 45 ----- src/colors.list | 42 ----- src/colors.sh | 44 ----- src/default-jvm.sh | 49 ----- src/generate_colors.sh | 44 ----- src/glyphs.sh | 30 --- src/hbar.c | 171 ----------------- src/lib/colors.h | 45 +++++ src/lib/colors.list | 42 +++++ src/lib/colors.sh | 44 +++++ src/lib/generate_colors.sh | 44 +++++ src/lib/glyphs.sh | 30 +++ src/lib/xilib.sh | 34 ++++ src/lib/xitui.sh | 451 +++++++++++++++++++++++++++++++++++++++++++++ src/parseconf.sh | 115 ------------ src/shtests.sh | 67 ------- src/tools/default-jvm.sh | 52 ++++++ src/tools/xichroot.sh | 58 ++++++ src/util/hbar.c | 171 +++++++++++++++++ src/util/parseconf.sh | 115 ++++++++++++ src/util/shmk.sh | 200 ++++++++++++++++++++ src/util/shtests.sh | 66 +++++++ src/xichroot.sh | 58 ------ src/xilib.sh | 34 ---- src/xitui.sh | 451 --------------------------------------------- 28 files changed, 1396 insertions(+), 1214 deletions(-) create mode 100755 build.shmk create mode 100755 shmk.shmk delete mode 100644 src/colors.h delete mode 100644 src/colors.list delete mode 100644 src/colors.sh delete mode 100755 src/default-jvm.sh delete mode 100644 src/generate_colors.sh delete mode 100644 src/glyphs.sh delete mode 100644 src/hbar.c create mode 100644 src/lib/colors.h create mode 100644 src/lib/colors.list create mode 100644 src/lib/colors.sh create mode 100644 src/lib/generate_colors.sh create mode 100644 src/lib/glyphs.sh create mode 100644 src/lib/xilib.sh create mode 100644 src/lib/xitui.sh delete mode 100644 src/parseconf.sh delete mode 100644 src/shtests.sh create mode 100755 src/tools/default-jvm.sh create mode 100644 src/tools/xichroot.sh create mode 100644 src/util/hbar.c create mode 100644 src/util/parseconf.sh create mode 100755 src/util/shmk.sh create mode 100644 src/util/shtests.sh delete mode 100644 src/xichroot.sh delete mode 100644 src/xilib.sh delete mode 100644 src/xitui.sh diff --git a/Makefile b/Makefile index 07dd3ca..0829535 100755 --- a/Makefile +++ b/Makefile @@ -1,73 +1,22 @@ -FLAGS= - DESTDIR= PREFIX=/usr - -DIST=dist - .DEFAULT_GOAL := build -install: build install-hbar install-colors install-parseconf install-shtests install-glyphs install-xitui install-xilib -check: check-parseconf -build: make-dist hbar shtests parseconf colors - -make-dist: - mkdir -p ${DIST} - -install-headers: src/*.h - install -Dm644 src/*.h ${DESTDIR}${PREFIX}/include - -install-shtests: shtests - install -Dm755 ${DIST}/shtests ${DESTDIR}${PREFIX}/bin/shtests - -install-parseconf: parseconf - install -Dm755 ${DIST}/parseconf ${DESTDIR}${PREFIX}/bin/parseconf - -install-hbar: hbar - install -Dm755 ${DIST}/hbar ${DESTDIR}${PREFIX}/bin - -install-colors: src/colors.list - install -Dm755 ${DIST}/colors.sh ${DESTDIR}${PREFIX}/lib/colors.sh - install -Dm755 ${DIST}/colors.h ${DESTDIR}${PREFIX}/include/colors.h - -install-glyphs: src/glyphs.sh - install -Dm755 src/glyphs.sh ${DESTDIR}${PREFIX}/lib +export PATH := .:$(PATH) -install-xitui: src/xitui.sh - install -Dm755 src/xitui.sh ${DESTDIR}${PREFIX}/lib +shmk0: + @echo shmk stage0 + @install -m755 src/util/shmk.sh shmk -install-xilib: src/xilib.sh - install -Dm755 src/xilib.sh ${DESTDIR}${PREFIX}/lib +bootstrap: shmk0 + @echo shmk stage1 + @./shmk.shmk build + @install -m755 dist/shmk shmk -check-parseconf: shtests parseconf test/parseconf.sh - ${DIST}/shtests ./test/parseconf.sh - - -hbar: colors src/hbar.c - ${CC} -I${DIST} src/hbar.c -o ${DIST}/hbar ${FLAGS} - -shtests: src/shtests.sh - install -Dm755 src/shtests.sh ${DIST}/shtests - -parseconf: src/parseconf.sh - install -Dm755 src/parseconf.sh ${DIST}/parseconf - -colors: src/colors.list - cp src/colors.sh ${DIST} - cp src/colors.h ${DIST} +build: bootstrap + @echo all xiutils + @./build.shmk clean: - rm -r ${DIST} - - -# xichroot -# -install-chroot: src/xichroot.sh - install -Dm755 src/xichroot.sh ${DESTDIR}${PREFIX}/bin/xichroot - - -# default-jvm -# -install-default-jvm: src/default-jvm.sh - install -Dm755 src/default-jvm.sh ${DESTDIR}${PREFIX}/bin/default-jvm - + @rm -r dist + @rm -r shmk diff --git a/build.shmk b/build.shmk new file mode 100755 index 0000000..2ce463d --- /dev/null +++ b/build.shmk @@ -0,0 +1,22 @@ +#!/usr/bin/env shmk + +CC=gcc + +LIBS="src/lib/*.sh" + +PROGS=" +hbar +src/util/shmk.sh +src/util/shtests.sh +src/util/parseconf.sh +src/tools/xichroot.sh +src/tools/default-jvm.sh +" + +prog_hbar () { + ${CC} -I${DIST} -o ${DIST}/hbar src/util/hbar.c +} + +check_shtests () { + ${DIST}/shtests test/parseconf.sh +} diff --git a/shmk.shmk b/shmk.shmk new file mode 100755 index 0000000..c6a3e8c --- /dev/null +++ b/shmk.shmk @@ -0,0 +1,9 @@ +#!/bin/env shmk + +LIBS=" +src/lib/colors.sh +" + +PROGS=" +src/util/shmk.sh +" diff --git a/src/colors.h b/src/colors.h deleted file mode 100644 index e7bc26d..0000000 --- a/src/colors.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * colors.h - * - * list of ansi color codes - * provided by xiutils - * - */ - -#define RESET "" -#define BOLD "" -#define BLACK "" -#define RED "" -#define GREEN "" -#define YELLOW "" -#define BLUE "" -#define MAGENTA "" -#define CYAN "" -#define WHITE "" -#define DEFAULT "" -#define BG_BLACK "" -#define BG_RED "" -#define BG_GREEN "" -#define BG_YELLOW "" -#define BG_BLUE "" -#define BG_MAGENTA "" -#define BG_CYAN "" -#define BG_WHITE "" -#define BG_DEFAULT "" -#define LIGHT_BLACK "" -#define LIGHT_RED "" -#define LIGHT_GREEN "" -#define LIGHT_YELLOW "" -#define LIGHT_BLUE "" -#define LIGHT_MAGENTA "" -#define LIGHT_CYAN "" -#define LIGHT_WHITE "" -#define LIGHT_DEFAULT "" -#define BOLDBLACK "" -#define BOLDRED "" -#define BOLDGREEN "" -#define BOLDYELLOW "" -#define BOLDBLUE "" -#define BOLDMAGENTA "" -#define BOLDCYAN "" -#define BOLDWHITE "" diff --git a/src/colors.list b/src/colors.list deleted file mode 100644 index 0829a63..0000000 --- a/src/colors.list +++ /dev/null @@ -1,42 +0,0 @@ -RESET "\033[0m" - -BOLD "\033[0;1m" - -BLACK "\033[0;30m" -RED "\033[0;31m" -GREEN "\033[0;32m" -YELLOW "\033[0;33m" -BLUE "\033[0;34m" -MAGENTA "\033[0;35m" -CYAN "\033[0;36m" -WHITE "\033[0;37m" -DEFAULT "\033[0;39m" - -BG_BLACK "\033[40m" -BG_RED "\033[41m" -BG_GREEN "\033[42m" -BG_YELLOW "\033[43m" -BG_BLUE "\033[44m" -BG_MAGENTA "\033[45m" -BG_CYAN "\033[46m" -BG_WHITE "\033[47m" -BG_DEFAULT "\033[49m" - -LIGHT_BLACK "\033[0;90m" -LIGHT_RED "\033[0;91m" -LIGHT_GREEN "\033[0;92m" -LIGHT_YELLOW "\033[0;93m" -LIGHT_BLUE "\033[0;94m" -LIGHT_MAGENTA "\033[0;95m" -LIGHT_CYAN "\033[0;96m" -LIGHT_WHITE "\033[0;97m" -LIGHT_DEFAULT "\033[0;99m" - -BOLDBLACK "\033[1;30m" -BOLDRED "\033[1;31m" -BOLDGREEN "\033[1;32m" -BOLDYELLOW "\033[1;33m" -BOLDBLUE "\033[1;34m" -BOLDMAGENTA "\033[1;35m" -BOLDCYAN "\033[1;36m" -BOLDWHITE "\033[1;37m" diff --git a/src/colors.sh b/src/colors.sh deleted file mode 100644 index 8e5de11..0000000 --- a/src/colors.sh +++ /dev/null @@ -1,44 +0,0 @@ -# -# colors.h -# -# list of ansi color codes -# provided by xiutils -# - -export RESET=$(printf "") -export BOLD=$(printf "") -export BLACK=$(printf "") -export RED=$(printf "") -export GREEN=$(printf "") -export YELLOW=$(printf "") -export BLUE=$(printf "") -export MAGENTA=$(printf "") -export CYAN=$(printf "") -export WHITE=$(printf "") -export DEFAULT=$(printf "") -export BG_BLACK=$(printf "") -export BG_RED=$(printf "") -export BG_GREEN=$(printf "") -export BG_YELLOW=$(printf "") -export BG_BLUE=$(printf "") -export BG_MAGENTA=$(printf "") -export BG_CYAN=$(printf "") -export BG_WHITE=$(printf "") -export BG_DEFAULT=$(printf "") -export LIGHT_BLACK=$(printf "") -export LIGHT_RED=$(printf "") -export LIGHT_GREEN=$(printf "") -export LIGHT_YELLOW=$(printf "") -export LIGHT_BLUE=$(printf "") -export LIGHT_MAGENTA=$(printf "") -export LIGHT_CYAN=$(printf "") -export LIGHT_WHITE=$(printf "") -export LIGHT_DEFAULT=$(printf "") -export BOLDBLACK=$(printf "") -export BOLDRED=$(printf "") -export BOLDGREEN=$(printf "") -export BOLDYELLOW=$(printf "") -export BOLDBLUE=$(printf "") -export BOLDMAGENTA=$(printf "") -export BOLDCYAN=$(printf "") -export BOLDWHITE=$(printf "") diff --git a/src/default-jvm.sh b/src/default-jvm.sh deleted file mode 100755 index 96f4f68..0000000 --- a/src/default-jvm.sh +++ /dev/null @@ -1,49 +0,0 @@ -#!/bin/sh -JVM_DIR=/usr/lib/jvm -JAVA_BIN=/bin/java - -usage () { - cat << EOF -xilinux-java - Print the name of the currently linked jvm - non-zero exit code if none is linked - -xilinux-java [name] - create symlinks to /usr/lib/jvm/[name]/bin to /bin - -xilinux-java [-l] - list installed JVMs -EOF -} - -get () { - [ -h "$JAVA_BIN" ] && { - path=$(readlink "$JAVA_BIN") - path=${path%%/bin/java} - path=${path##*/} - echo $path - } -} - -link () { - [ -d "$1" ] && for bin in $1/bin/*; do - ln -sf $bin /bin/${bin##*/} - done -} - - -[ "$#" = "0" ] && { - get || return 1 -} || { - case "$1" in - "-l"|"--list") - ls -1 $JVM_DIR - ;; - *) - link $1 \ - || link $JVM_DIR/$1 \ - || get - ;; - esac -} - diff --git a/src/generate_colors.sh b/src/generate_colors.sh deleted file mode 100644 index 6e01d58..0000000 --- a/src/generate_colors.sh +++ /dev/null @@ -1,44 +0,0 @@ -#!/bin/bash -DIST=$1 - -headerfile=${DIST}/colors.h -shlib=${DIST}/colors.sh - -cat > $shlib << "EOF" -# -# colors.h -# -# list of ansi color codes -# provided by xiutils -# - -EOF - -cat > $headerfile << "EOF" -/* - * colors.h - * - * list of ansi color codes - * provided by xiutils - * - */ - -EOF - -append_header() { - echo "#define $1 $2" >> $headerfile -} - -append_sh() { - printf 'export %s=$(printf %s)\n' $1 $2 >> $shlib -} - -while IFS= read -r line; do - echo "$line" | grep -q "." || continue - name=$(echo $line | awk '{ print $1 }') - code=$(echo $line | awk '{ print $2 }') - - append_header $name $code - append_sh $name $code - -done < "$2" diff --git a/src/glyphs.sh b/src/glyphs.sh deleted file mode 100644 index b4ecccd..0000000 --- a/src/glyphs.sh +++ /dev/null @@ -1,30 +0,0 @@ -# -# Assorted Unicode glyphs that can be repurposed -# - -export CHECKMARK="✔" -export CROSSMARK="✘" -export CROSSMARK="✘" -export HOURGLASS="⧖" -export LARGE_CIRCLE="◯" -export REFRESH="🗘" -export TABCHAR="╚═" -export XI="Ξ" - -export LOGO_SMALL="████████ -▀ ▀ - █▄▄█ - █▀▀█ -▄ ▄ -████████" - -export LOGO="████████████████ -▀ ▀ - - - █▄▄▄▄▄▄█ - █▀▀▀▀▀▀█ - - -▄ ▄ -████████████████" diff --git a/src/hbar.c b/src/hbar.c deleted file mode 100644 index 59cf888..0000000 --- a/src/hbar.c +++ /dev/null @@ -1,171 +0,0 @@ -/* - * hbar - * - * create a horizontal progres bar across the screen - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "colors.h" - -#define DEFAULT_COLOR BLACK BG_WHITE -#define DEFAULT_RESET WHITE BG_DEFAULT - -#define SAVE_POS "\033[s" -#define LOAD_POS "\033[u" - -void human_format(int bytes, char *output) { - char *suffix[] = {"B", "KB", "MB", "GB", "TB"}; - char length = sizeof(suffix) / sizeof(suffix[0]); - - int i = 0; - double dblBytes = bytes; - - if (bytes > 1024) { - for (i = 0; (bytes / 1024) > 0 && i 0) { - float percent = (float) completed / (float) total; - reset_at = percent * width; - } - - if (i == reset_at) { - printf(reset); - } - - if (text && i < len(text)) { - printat(text, i); - } else if (i + 1 > width - len(count)) { - printat(count, i - width + len(count)); - } else { - printf(" "); - } - } - - if (line != -1) { - printf("\033[%dB", line); - } - if (terminate) { - printf(RESET "\n"); - } - - return 0; -} - diff --git a/src/lib/colors.h b/src/lib/colors.h new file mode 100644 index 0000000..e7bc26d --- /dev/null +++ b/src/lib/colors.h @@ -0,0 +1,45 @@ +/* + * colors.h + * + * list of ansi color codes + * provided by xiutils + * + */ + +#define RESET "" +#define BOLD "" +#define BLACK "" +#define RED "" +#define GREEN "" +#define YELLOW "" +#define BLUE "" +#define MAGENTA "" +#define CYAN "" +#define WHITE "" +#define DEFAULT "" +#define BG_BLACK "" +#define BG_RED "" +#define BG_GREEN "" +#define BG_YELLOW "" +#define BG_BLUE "" +#define BG_MAGENTA "" +#define BG_CYAN "" +#define BG_WHITE "" +#define BG_DEFAULT "" +#define LIGHT_BLACK "" +#define LIGHT_RED "" +#define LIGHT_GREEN "" +#define LIGHT_YELLOW "" +#define LIGHT_BLUE "" +#define LIGHT_MAGENTA "" +#define LIGHT_CYAN "" +#define LIGHT_WHITE "" +#define LIGHT_DEFAULT "" +#define BOLDBLACK "" +#define BOLDRED "" +#define BOLDGREEN "" +#define BOLDYELLOW "" +#define BOLDBLUE "" +#define BOLDMAGENTA "" +#define BOLDCYAN "" +#define BOLDWHITE "" diff --git a/src/lib/colors.list b/src/lib/colors.list new file mode 100644 index 0000000..0829a63 --- /dev/null +++ b/src/lib/colors.list @@ -0,0 +1,42 @@ +RESET "\033[0m" + +BOLD "\033[0;1m" + +BLACK "\033[0;30m" +RED "\033[0;31m" +GREEN "\033[0;32m" +YELLOW "\033[0;33m" +BLUE "\033[0;34m" +MAGENTA "\033[0;35m" +CYAN "\033[0;36m" +WHITE "\033[0;37m" +DEFAULT "\033[0;39m" + +BG_BLACK "\033[40m" +BG_RED "\033[41m" +BG_GREEN "\033[42m" +BG_YELLOW "\033[43m" +BG_BLUE "\033[44m" +BG_MAGENTA "\033[45m" +BG_CYAN "\033[46m" +BG_WHITE "\033[47m" +BG_DEFAULT "\033[49m" + +LIGHT_BLACK "\033[0;90m" +LIGHT_RED "\033[0;91m" +LIGHT_GREEN "\033[0;92m" +LIGHT_YELLOW "\033[0;93m" +LIGHT_BLUE "\033[0;94m" +LIGHT_MAGENTA "\033[0;95m" +LIGHT_CYAN "\033[0;96m" +LIGHT_WHITE "\033[0;97m" +LIGHT_DEFAULT "\033[0;99m" + +BOLDBLACK "\033[1;30m" +BOLDRED "\033[1;31m" +BOLDGREEN "\033[1;32m" +BOLDYELLOW "\033[1;33m" +BOLDBLUE "\033[1;34m" +BOLDMAGENTA "\033[1;35m" +BOLDCYAN "\033[1;36m" +BOLDWHITE "\033[1;37m" diff --git a/src/lib/colors.sh b/src/lib/colors.sh new file mode 100644 index 0000000..8e5de11 --- /dev/null +++ b/src/lib/colors.sh @@ -0,0 +1,44 @@ +# +# colors.h +# +# list of ansi color codes +# provided by xiutils +# + +export RESET=$(printf "") +export BOLD=$(printf "") +export BLACK=$(printf "") +export RED=$(printf "") +export GREEN=$(printf "") +export YELLOW=$(printf "") +export BLUE=$(printf "") +export MAGENTA=$(printf "") +export CYAN=$(printf "") +export WHITE=$(printf "") +export DEFAULT=$(printf "") +export BG_BLACK=$(printf "") +export BG_RED=$(printf "") +export BG_GREEN=$(printf "") +export BG_YELLOW=$(printf "") +export BG_BLUE=$(printf "") +export BG_MAGENTA=$(printf "") +export BG_CYAN=$(printf "") +export BG_WHITE=$(printf "") +export BG_DEFAULT=$(printf "") +export LIGHT_BLACK=$(printf "") +export LIGHT_RED=$(printf "") +export LIGHT_GREEN=$(printf "") +export LIGHT_YELLOW=$(printf "") +export LIGHT_BLUE=$(printf "") +export LIGHT_MAGENTA=$(printf "") +export LIGHT_CYAN=$(printf "") +export LIGHT_WHITE=$(printf "") +export LIGHT_DEFAULT=$(printf "") +export BOLDBLACK=$(printf "") +export BOLDRED=$(printf "") +export BOLDGREEN=$(printf "") +export BOLDYELLOW=$(printf "") +export BOLDBLUE=$(printf "") +export BOLDMAGENTA=$(printf "") +export BOLDCYAN=$(printf "") +export BOLDWHITE=$(printf "") diff --git a/src/lib/generate_colors.sh b/src/lib/generate_colors.sh new file mode 100644 index 0000000..6e01d58 --- /dev/null +++ b/src/lib/generate_colors.sh @@ -0,0 +1,44 @@ +#!/bin/bash +DIST=$1 + +headerfile=${DIST}/colors.h +shlib=${DIST}/colors.sh + +cat > $shlib << "EOF" +# +# colors.h +# +# list of ansi color codes +# provided by xiutils +# + +EOF + +cat > $headerfile << "EOF" +/* + * colors.h + * + * list of ansi color codes + * provided by xiutils + * + */ + +EOF + +append_header() { + echo "#define $1 $2" >> $headerfile +} + +append_sh() { + printf 'export %s=$(printf %s)\n' $1 $2 >> $shlib +} + +while IFS= read -r line; do + echo "$line" | grep -q "." || continue + name=$(echo $line | awk '{ print $1 }') + code=$(echo $line | awk '{ print $2 }') + + append_header $name $code + append_sh $name $code + +done < "$2" diff --git a/src/lib/glyphs.sh b/src/lib/glyphs.sh new file mode 100644 index 0000000..b4ecccd --- /dev/null +++ b/src/lib/glyphs.sh @@ -0,0 +1,30 @@ +# +# Assorted Unicode glyphs that can be repurposed +# + +export CHECKMARK="✔" +export CROSSMARK="✘" +export CROSSMARK="✘" +export HOURGLASS="⧖" +export LARGE_CIRCLE="◯" +export REFRESH="🗘" +export TABCHAR="╚═" +export XI="Ξ" + +export LOGO_SMALL="████████ +▀ ▀ + █▄▄█ + █▀▀█ +▄ ▄ +████████" + +export LOGO="████████████████ +▀ ▀ + + + █▄▄▄▄▄▄█ + █▀▀▀▀▀▀█ + + +▄ ▄ +████████████████" diff --git a/src/lib/xilib.sh b/src/lib/xilib.sh new file mode 100644 index 0000000..57dd10c --- /dev/null +++ b/src/lib/xilib.sh @@ -0,0 +1,34 @@ +#!/bin/sh + +# format a number into a bytes, kibibytes, mebibytes, or gibibytes +# +format_bytes () { + case "1" in + "$(($1>=1<<30))") printf "$(($1>>30))GiB";; + "$(($1>=1<<20))") printf "$(($1>>20))MiB";; + "$(($1>=1<<10))") printf "$(($1>>10))kiB";; + *) printf "$1B";; + esac +} + +# ensure that the user is a root user +# +checkroot () { + [ "$(id -u)" = "0" ] || { + printf "${RED}Please run as root!${RESET}\n" + exit 1 + } +} + +# reverse the order of lines +# +reverse_lines () { + local result= + while IFS= read -r line; do + result="$line + $result" + done + echo "$result" +} + + diff --git a/src/lib/xitui.sh b/src/lib/xitui.sh new file mode 100644 index 0000000..dcfaf02 --- /dev/null +++ b/src/lib/xitui.sh @@ -0,0 +1,451 @@ +#!/bin/sh + +################################################################# +#███████ ███████ ███████ ███████ ███████ ███████ ███████ ███████# +#█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █# +#█ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █# +#█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █# +#█ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███# +#█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ # +#█ ███████ ███████ ███████ ███████ ███████ ███████ ███████ █████# +# # +# # +# # +# # +# # +# # +# # +#█ ███████ ███████ ███████ ███████ ███████ ███████ ███████ █████# +#█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ # +#█ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███# +#█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █# +#█ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █# +#█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █# +#███████ ███████ ███████ ███████ ███████ ███████ ███████ ███████# +################################################################# + +. /usr/lib/colors.sh + +COLOR_FG=${WHITE} +COLOR_FG2=${LIGHT_BLUE} +COLOR_PTRN=${LIGHT_BLUE} +COLOR_BG=${BG_WHITE} + +t_init () { + export LINES=$(tput lines) + export COLUMNS=$(tput cols) + t_styl_cur l + tput clear + t_set_tty + t_gen_ptrn + +} + +t_set_tty () { + export SAVED_TTY_SETTINGS=$(stty -g) + stty raw -echo +} + +t_clean () { + stty $SAVED_TTY_SETTINGS + tput clear + t_yes_cur +} + +readc () { + stty -echo -icanon min 1 time 0 + s="$(dd bs=1 count=1 of=/dev/stdout 2>/dev/null)" + stty -icanon min 0 time 0 + [ "$s" = "" ] && { + s="$s$(dd bs=1 count=2 of=/dev/stdout 2>/dev/null)" + } + printf "$s" +} + + +t_cls () { + tput clear +} + +t_no_cur () { + tput civis +} + +t_yes_cur () { + tput cnorm +} + +t_set_cur () { + printf "[$1;$2H" +} + +max_line_length () { + local max=-1 + while IFS= read -r line; do + [ "${#line}" -gt "$max" ] && max=${#line} + done + echo "$max" +} + +t_styl_cur () { + case "$1" in + H) printf "\033[2 q";; + H0) printf "\033[1 q";; + l) printf "\033[6 q";; + l0) printf "\033[5 q";; + _) printf "\033[4 q";; + _0) printf "\033[3 q";; + *) printf"\033[0 q]";; + esac > $(tty) +} + + +t_gen_ptrn () { + export P="" + for l in $(seq 7); do + for c in $(seq $COLUMNS); do + case "$l" in + 1) [ "$((c%8))" != "0" ] ;; + 2) [ "$((c%8))" = "1" ] || [ "$((c%8))" = "7" ] ;; + 3) [ "$((c%2))" = "1" ] || [ "$((c%8))" = "4" ] ;; + 4) [ "$((c%2))" = "1" ] ;; + 5) [ "$((c%2))" != "0" ] || [ "$((c%8))" = "2" ] ;; + 6) [ "$((c%8))" = "5" ] || [ "$((c%8))" = "7" ] ;; + 7) [ "$((c%8))" != "6" ] ;; + *) false ;; + esac && P="$P█" || P="$P " + done + P="$P\n" + done +} + +t_prnt_ptrn () { + printf "$@$P" +} + +t_cls_ptrn () { + printf "" + printf " %.0s" $(seq $(( (LINES-14) * COLUMNS))) + t_drw_ptrn +} + +t_drw_ptrn () { + t_drw_txt_clr "${COLOR_PTRN}" 1 1 "$P" + t_drw_txt_clr "${COLOR_PTRN}" 1 $((LINES-6)) "$(printf "$P" | rev)" +} + +# draws inside the given area +# t_drw_box x y w h +# +t_drw_box () { + local x=$1 y=$2 w=$3 h=$4 + + + l="$(printf "${COLOR_FG2}║" + printf " %.0s" $(seq $((w-2))) + printf "${COLOR_FG2}║" + )" + b="$(printf "═%.0s" $(seq $((w-2))))" + + printf "[${y};${x}H${COLOR_FG2}╔$b╗" + for i in $(seq $((y+1)) $((y+h-1))); do + printf "[${i};${x}H$l" + done + printf "[$((y+h-1));${x}H${COLOR_FG2}╚$b╝" +} + +# draw text at location +# t_drw_txt x y [txt...] +# +t_drw_txt () { + local x=$1 y=$2 + shift 2 + + echo "$@" | while IFS= read -r line; do + printf "[${y};${x}H$line" + y=$((y+1)) + done +} + +# draw colored text +# +t_drw_txt_clr () { + local clr=$1 x=$2 y=$3 + shift 3 + + echo "$@" | while IFS= read -r line; do + printf "[${y};${x}H${clr}$line" + y=$((y+1)) + done +} + +t_msg () { + h_txt_w_h "$@\n" + h_cntr $w $h + t_drw_box $((x-1)) $((y-1)) $((w+2)) $((h+2)) + t_drw_txt_clr "${COLOR_FG}" $x $y "$@" +} + +h_drw_btns () { + local sel=$1 i=0 + while read -r btn; do + [ "$sel" = "$i" ] && styl="${COLOR_BG}" || styl="${COLOR_FG2}" + h_drw_btn "${styl}" ${btn} + i=$((i+1)) + done +} + +h_drw_btn () { + styl="$1" x=$2 y=$3 ; shift 3; txt="$@" + t_drw_txt $x $y "${COLOR_FG2}${styl}$txt${RESET}" +} + +h_cntr() { + local w=$1 h=$2 + x=$(( ( (COLUMNS+w)/2 ) - $1 )) + y=$(( ( (LINES+h)/2 ) - $2 )) +} + +h_txt_w_h() { + w=$(printf "$*" | max_line_length) + h=$(printf "$*" | wc -l) +} + +h_t_result() { + local s=$1 i=0; shift + for opt in "$@"; do + case " $s " in + *" $i "*) echo "$opt";; + esac + i=$((i+1)) + done +} + +t_dialog() { + # TODO these are messy af + local msg="$1"; shift + local btns_len="$#" w_btns=$(echo "$*" | wc -c) + + h_txt_w_h "$msg\n $*\n" + h_cntr $w $h + + local btn_y=$((y+h-1)) \ + btn_x=$(( ((COLUMNS+w_btns) / 2) - w_btns)) + + btns="" + for btn in "$@"; do + btns="$btns$btn_x $btn_y $btn\n" + btn_x=$((btn_x+${#btn}+1)) + done + + t_drw_box $((x-1)) $((y-1)) $((w+2)) $((h+2)) + t_drw_txt_clr ${COLOR_FG} $x $y "$msg" + + local sel="0" + while true; do + printf "$btns" | h_drw_btns $sel + + case "$(readc)" in + ' ') break;; + ''|''|' ') + sel=$(((sel+1)%btns_len));; + ''|'') + sel=$(((sel+btns_len-1)%btns_len));; + esac + + done + export T_RESULT=$(h_t_result "$sel" "$@") +} + +t_radio() { + local msg="$1"; shift + + local btns_len="$#" w_btns=$(echo "$*" | wc -c) + text=`{ + printf "$msg\n" + for b in "$@"; do + printf "╚═$b${RESET}\n" + done + }` + + t_msg "$text" + + x=$((x+2)) y=$((y+1)) + local py=$((y)) + btns="" + for btn in "$@"; do + btns="$btns$x $y $btn\n" + y=$((y+1)) + done + + local sel="0" + while true; do + printf "$btns" | h_drw_btns $sel + + case "$(readc)" in + ' ') break;; + ''|''|' ') + sel=$(((sel+1)%btns_len));; + ''|'') + sel=$(((sel+btns_len-1)%btns_len));; + esac + + done + export T_RESULT=$(h_t_result "$sel" "$@") +} + +t_check () { + local msg="$1"; shift + local btns_len="$#" + btns_len=$((btns_len+1)) + + text=`{ + printf "$msg\n" + for b in "$@"; do + printf "[ ] $b${RESET}\n" + done + printf " ${RESET}\n" + }` + + t_msg "$text" + + x=$((x+4)) y=$((y+1)) + local py=$((y)) + btns="" + for btn in "$@"; do + btns="$btns$x $y $btn\n" + y=$((y+1)) + done + btns="$btns$x $y \n" + + local sel="0" checked="" + while true; do + printf "$btns" | h_drw_btns $sel + case "$(readc)" in + ''|''|' ') + sel=$(((sel+1)%btns_len)) + ;; + ''|'') + sel=$(((sel+btns_len-1)%btns_len));; + ' '|' ') + [ "$((sel+1))" = "$btns_len" ] && break || { + case " $checked " in + *$sel*) + checked=$(echo "$checked" | sed "s/$sel//") + t_drw_txt $((x-3)) $((py+sel)) " " + ;; + *) + checked="$checked$sel " + t_drw_txt $((x-3)) $((py+sel)) "x" + ;; + esac + };; + esac + done + export T_RESULT=$(h_t_result "$checked" "$@") +} + +t_prompt () { + t_dialog "$@" "" +} + +t_yesno () { + t_dialog "$@" "" "" + [ "$T_RESULT" = "" ] +} + +t_input () { + stty $SAVED_TTY_SETTINGS + t_msg "$@ + +>" > $(tty) + t_yes_cu + read var + t_set_tty + t_no_cur + export T_RESULT="$var" +} + +t_input_hidden () { + stty $SAVED_TTY_SETTINGS + stty -echo + t_msg "$@ + +>" > $(tty) + t_yes_cur + read var + t_set_tty + t_no_cur + export T_RESULT="$var" +} + +t_tail() { + t_drw_txt 0 7 "$(tail -$((LINES-14)) $1)" +} + +t_paged_radio () { + perpage=9 + heading=$1 + shift + + T_RESULT="more..." + page=0 + max_pages=$((($#/perpage)+1)) + while true; do + start=$((page*perpage)) + current=$(echo $@ | tr ' ' '\n' | tail -$(($#-start)) | head -$perpage) + current="$current more..." + + t_cls_ptrn + t_radio "$heading" $current + [ "$T_RESULT" = "more..." ] && page=$(((page+1)%max_pages)) || return 0 + done +} + + +t_demo () { + t_init + t_no_cur + + + t_cls_ptrn + t_prompt "Hello world?" + t_cls_ptrn + + t_paged_radio "Pick a number" $(seq 34) + t_prompt "You selected the following: $T_RESULT" + t_cls_ptrn + + exit 1 + + t_radio "Pick one:" "toast" "bread" "bread but fishy" "empty sandwich" + t_prompt "You selected the following: $T_RESULT" + t_cls_ptrn + + t_check "Please select your order:" "yeast" "fish and chips" "twigs" "tuna" "pizza" "aspic" + t_prompt "You selected the following: $T_RESULT" + t_cls_ptrn + + t_dialog "Can a match box?" "" "" "" + t_prompt "You selected the following: $T_RESULT" + t_cls_ptrn + + #t_cls_ptrn + t_input "Enter your name" + t_prompt "Hello $T_RESULT" + t_cls_ptrn + + file=$2.sh + [ -f $file ] && { + t_tail $file + } || { + t_prompt "The file $file does not exist" + } + t_prompt "Clear" + t_cls_ptrn + t_prompt "Exit?" + + t_clean +} + +"$@" diff --git a/src/parseconf.sh b/src/parseconf.sh deleted file mode 100644 index d215a99..0000000 --- a/src/parseconf.sh +++ /dev/null @@ -1,115 +0,0 @@ -#!/bin/sh - -usage () { - printf "Usage $0 " - echo << "EOF" -OPTIONS... [FILTER] - -Print the parsed config file filtering by the keys -Arguments: - -f file read configuration from a file, uses /dev/sdtin otherwise - -v only print values - -c n print the last [n] -EOF -} - -# parse a single config file line -# -parse_line() { - [ "$#" = 0 ] && return - - local line="$@" - local key=$1 - - shift - local value="$@" - value=${value%#*} - - case $key in - "include") - cat $value | parse - return - ;; - "]") - IFS=. - set -- $list - list="${*%${!#}}" - IFS=" " - printf "\n" - return - ;; - "}") - [ "$level" = "${level%.*}" ] && - level="" || level=${level%.*} - return - ;; - esac - - case ${value##* } in - "{") - level="${level}${level:+.}${key}" - ;; - "[") - list="${list}${list:+.}${key}" - printf "$level${level:+.}$key:" - ;; - *) - - [ "${#list}" = "0" ] && - printf "$level${level:+.}$key:$value\n" || - printf "$line " - ;; - esac -} - -# print the parsed values from the config file in key:value format -# -parse () { - local file="$1" - - export level="" - export list="" - while IFS= read -r line; do - parse_line $line - done < "/dev/stdin" -} - -# Use the env variable if exists -[ -z ${CONF_FILE} ] && CONF_FILE="/dev/stdin" - -# initialise options -print_keys=true -count= - -while getopts ":f:c:v" opt; do - case "${opt}" in - f) - [ "${OPTARG}" = "-" ] && - CONF_FILE="/dev/stdin" || - CONF_FILE="${OPTARG}" - ;; - - v) - print_keys=false - ;; - c) - count="${OPTARG}q" - ;; - *) - esac -done - -shift $((OPTIND-1)) - -[ $# = 0 ] && - pattern=".*" || - pattern=$(echo $@ | sed "s/\*/[^:]*/g") - -$print_keys && - pattern="s/^($pattern:.+)/\1/p;${count}" || - pattern="s/^$pattern:(.+)/\1/p;${count}" - -# strip whitespace -sed "s/^\s*#.*$\|\s(\s\+)\|^\s\|\s^\|;*$//g" $CONF_FILE | - parse $@ | - sed -rn $pattern diff --git a/src/shtests.sh b/src/shtests.sh deleted file mode 100644 index a899f3c..0000000 --- a/src/shtests.sh +++ /dev/null @@ -1,67 +0,0 @@ -#!/bin/sh -# -# Simple shell test suite -# -# will run all functions with a name starting with test_ -# the return value of said function will determine if its a pass or fail -# -# to run a shell file full of unit tests, run: -# shtests [FILE] -# - -[ -f /usr/lib/colors.sh ] && . /usr/lib/colors.sh -[ -f colors.sh ] && . colors.sh - -PASS="${BLUE}[ ${GREEN}PASS${BLUE} ]${RESET}" -FAIL="${BLUE}[ ${RED}FAIL${BLUE} ]${RESET}" - -V=false - -runtest () { - test_name=$(echo $1 | sed "s/_/ /g") - test_func="$2" - printf "${BLUE}[ ] ${RESET}$test_name "; - if "$test_func" ; then - printf "\r$PASS\n" - return 0 - else - printf "\r$FAIL\n" - return 1 - fi -} - -# TODO use getopt for this -if [ "$1" = "-v" ]; then - shift - V=true -fi - -if [ $# = "0" ]; then - printf "${RED}No tests file has been provided\n" - exit 1; -else - . $@ -fi - -tests=$(sed -n "s/^test_\(.*\)\s*()\s*{/\1/p" $@) -total=$(echo $tests | wc -w) -passed=0 -failed=0 - -printf "${BLUE}Running $total tests: \n" -for name in $tests; do - if runtest "$name" "test_$name"; then - passed=$((passed+1)) - else - failed=$((failed+1)) - fi -done - -printf "\n${BLUE}Summary for $total tests:\n" -printf "\t${PASS} $passed\n" -printf "\t${FAIL} $failed\n" -printf "\n" - -[ "$passed" = "$total" ] || exit 1 - - diff --git a/src/tools/default-jvm.sh b/src/tools/default-jvm.sh new file mode 100755 index 0000000..56c427c --- /dev/null +++ b/src/tools/default-jvm.sh @@ -0,0 +1,52 @@ +#!/bin/sh +JVM_DIR=/usr/lib/jvm +JAVA_BIN=/bin/java + +usage () { + cat << EOF +xilinux-java + Print the name of the currently linked jvm + non-zero exit code if none is linked + +xilinux-java [name] + create symlinks to /usr/lib/jvm/[name]/bin to /bin + +xilinux-java [-l] + list installed JVMs +EOF +} + +get () { + [ -h "$JAVA_BIN" ] && { + path=$(readlink "$JAVA_BIN") + path=${path%%/bin/java} + path=${path##*/} + echo $path + } +} + +link () { + [ -d "$1" ] && for bin in $1/bin/*; do + ln -sf $bin /bin/${bin##*/} + done +} + + +[ "$#" = "0" ] && { + get || return 1 +} || { + case "$1" in + "-l"|"--list") + ls -1 $JVM_DIR + ;; + "-h"|"--help") + usage + ;; + *) + link $1 \ + || link $JVM_DIR/$1 \ + || get + ;; + esac +} + diff --git a/src/tools/xichroot.sh b/src/tools/xichroot.sh new file mode 100644 index 0000000..7136e25 --- /dev/null +++ b/src/tools/xichroot.sh @@ -0,0 +1,58 @@ +#!/bin/bash +# This script is intended for root users to automatically mount requried dirs and chroot into an installed system +# simplifed version of arch-chroot from + +args=$@ +target=$1 + +die() { printf "$@\n"; exit 1;} + +ignore_error() { + "$@" 2>/dev/null + return 0 +} + +chroot_add_mount() { + mount "$@" && CHROOT_ACTIVE_MOUNTS=("$2" "${CHROOT_ACTIVE_MOUNTS[@]}") +} + +chroot_maybe_add_mount() { + local cond=$1; shift + if eval "$cond"; then + chroot_add_mount "$@" + fi +} + +chroot_setup() { + CHROOT_ACTIVE_MOUNTS=() + [[ $(trap -p EXIT) ]] && die '(BUG): attempting to overwrite existing EXIT trap' + trap 'chroot_teardown' EXIT + + chroot_add_mount proc "$target/proc" -t proc -o nosuid,noexec,nodev + chroot_add_mount sys "$target/sys" -t sysfs -o nosuid,noexec,nodev,ro + ignore_error chroot_maybe_add_mount "[[ -d '$1/sys/firmware/efi/efivars' ]]" \ + efivarfs "$target/sys/firmware/efi/efivars" -t efivarfs -o nosuid,noexec,nodev + chroot_add_mount udev "$target/dev" -t devtmpfs -o mode=0755,nosuid + chroot_add_mount devpts "$target/dev/pts" -t devpts -o mode=0620,gid=5,nosuid,noexec + chroot_add_mount shm "$target/dev/shm" -t tmpfs -o mode=1777,nosuid,nodev + chroot_add_mount /run "$target/run" --bind + chroot_add_mount tmp "$target/tmp" -t tmpfs -o mode=1777,strictatime,nodev,nosuid +} + +chroot_teardown() { + if (( ${#CHROOT_ACTIVE_MOUNTS[@]} )); then + umount "${CHROOT_ACTIVE_MOUNTS[@]}" + fi + unset CHROOT_ACTIVE_MOUNTS +} + +[ "$EUID" -eq 0 ] || die "Please run this as root" +[[ -d $target ]] || die "$target is not a directory; cannot chroot!" + +chroot_setup $@ + +if [ $# -gt 1 ]; then + chroot $@ +else + chroot $@ /bin/sh -l +fi diff --git a/src/util/hbar.c b/src/util/hbar.c new file mode 100644 index 0000000..59cf888 --- /dev/null +++ b/src/util/hbar.c @@ -0,0 +1,171 @@ +/* + * hbar + * + * create a horizontal progres bar across the screen + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "colors.h" + +#define DEFAULT_COLOR BLACK BG_WHITE +#define DEFAULT_RESET WHITE BG_DEFAULT + +#define SAVE_POS "\033[s" +#define LOAD_POS "\033[u" + +void human_format(int bytes, char *output) { + char *suffix[] = {"B", "KB", "MB", "GB", "TB"}; + char length = sizeof(suffix) / sizeof(suffix[0]); + + int i = 0; + double dblBytes = bytes; + + if (bytes > 1024) { + for (i = 0; (bytes / 1024) > 0 && i 0) { + float percent = (float) completed / (float) total; + reset_at = percent * width; + } + + if (i == reset_at) { + printf(reset); + } + + if (text && i < len(text)) { + printat(text, i); + } else if (i + 1 > width - len(count)) { + printat(count, i - width + len(count)); + } else { + printf(" "); + } + } + + if (line != -1) { + printf("\033[%dB", line); + } + if (terminate) { + printf(RESET "\n"); + } + + return 0; +} + diff --git a/src/util/parseconf.sh b/src/util/parseconf.sh new file mode 100644 index 0000000..d215a99 --- /dev/null +++ b/src/util/parseconf.sh @@ -0,0 +1,115 @@ +#!/bin/sh + +usage () { + printf "Usage $0 " + echo << "EOF" +OPTIONS... [FILTER] + +Print the parsed config file filtering by the keys +Arguments: + -f file read configuration from a file, uses /dev/sdtin otherwise + -v only print values + -c n print the last [n] +EOF +} + +# parse a single config file line +# +parse_line() { + [ "$#" = 0 ] && return + + local line="$@" + local key=$1 + + shift + local value="$@" + value=${value%#*} + + case $key in + "include") + cat $value | parse + return + ;; + "]") + IFS=. + set -- $list + list="${*%${!#}}" + IFS=" " + printf "\n" + return + ;; + "}") + [ "$level" = "${level%.*}" ] && + level="" || level=${level%.*} + return + ;; + esac + + case ${value##* } in + "{") + level="${level}${level:+.}${key}" + ;; + "[") + list="${list}${list:+.}${key}" + printf "$level${level:+.}$key:" + ;; + *) + + [ "${#list}" = "0" ] && + printf "$level${level:+.}$key:$value\n" || + printf "$line " + ;; + esac +} + +# print the parsed values from the config file in key:value format +# +parse () { + local file="$1" + + export level="" + export list="" + while IFS= read -r line; do + parse_line $line + done < "/dev/stdin" +} + +# Use the env variable if exists +[ -z ${CONF_FILE} ] && CONF_FILE="/dev/stdin" + +# initialise options +print_keys=true +count= + +while getopts ":f:c:v" opt; do + case "${opt}" in + f) + [ "${OPTARG}" = "-" ] && + CONF_FILE="/dev/stdin" || + CONF_FILE="${OPTARG}" + ;; + + v) + print_keys=false + ;; + c) + count="${OPTARG}q" + ;; + *) + esac +done + +shift $((OPTIND-1)) + +[ $# = 0 ] && + pattern=".*" || + pattern=$(echo $@ | sed "s/\*/[^:]*/g") + +$print_keys && + pattern="s/^($pattern:.+)/\1/p;${count}" || + pattern="s/^$pattern:(.+)/\1/p;${count}" + +# strip whitespace +sed "s/^\s*#.*$\|\s(\s\+)\|^\s\|\s^\|;*$//g" $CONF_FILE | + parse $@ | + sed -rn $pattern diff --git a/src/util/shmk.sh b/src/util/shmk.sh new file mode 100755 index 0000000..0174857 --- /dev/null +++ b/src/util/shmk.sh @@ -0,0 +1,200 @@ +#!/bin/sh +# +# A tool to build a single executable from a collection of shell scripts + +#include colors + +usage () { + cat << EOF +${BLUE}Available Options: + +EOF +} + +find_file () { + for p in $search_path; do + for f in $p/$1.sh $p/$1; do + $verbose \ + && printf "${LIGHT_BLACK}checking: $f\n" 1>&2 + + [ -f "$f" ] && { + echo "$f" + return 0 + } + done + done + return 1 +} + +parse_lines () { + while IFS= read -r line; do + case "$line" in + "#include "*) + cat $(find_file ${line#\#include}) | parse_lines + ;; + "#>"*) + eval ${line#'#>'} + ;; + "#!"*) + printf "%s\n" "$line" + ;; + "#"*);; + *) + printf "%s\n" "$line" + ;; + esac + done +} + +build_shmk () { + [ -f "$1" ] || return 1 + [ ! -z "$2" ] && output="$2" + [ -z "$output" ] && output=$(basename $1) + output=${output%.*} + + $clean && { + rm -f $output + exit + } + + $verbose && echo "building $1 to $output" + + [ ! -d "${output%/*}" ] && mkdir -p "${output%/*}" + + cat $1 \ + | parse_lines > ${output} && + [ ! -z ${entry_function} ] && echo "$entry_function" >> ${output} + chmod +x ${output} +} + +interpret_shmk () { + . $1 + local cmdlist="" + + DIST="./dist" + PROGS="$PROGS $(sed -rn "s/^prog_(.*)\s*\(\)\s*\{/\1/p" $1)" + LIBS="$LIBS $(sed -rn "s/^lib_(.*)\s*\(\)\s*\{/\1/p" $1)" + CHECKS="$CHECKS $(sed -rn "s/^check_(.*)\s*\(\)\s*\{/\1/p" $1)" + + shift + [ -z "$1" ] && set -- clean build check install + while [ ! -z "$1" ]; do + case "$1" in + install) + for prog in $PROGS; do + prog=$(basename $prog) + prog="${prog%.*}" + cmdlist="$cmdlist + install -Dm755 $DIST/$prog ${DESTDIR}/${PREFIX}/bin/$prog #Install program $prog" + done + for lib in $LIBS; do + lib=$(basename $lib) + lib="${lib%.*}" + cmdlist="$cmdlist + install -Dm755 $DIST/$lib ${DESTDIR}/${PREFIX}/lib/$lib #Install library $lib" + done + ;; + clean) + [ -d "$DIST" ] && cmdlist="$cmdlist + rm -r $DIST #Clean" + ;; + check) + for check in $CHECKS; do + command -v check_$check > /dev/null 2>&1 && cmdlist="$cmdlist + check_$check #Check $check" + done + ;; + *) # build all programs + search_path="$DIST $search_path" + + for lib in $LIBS; do + name=$(basename $lib) + build_cmd=lib_$lib + command -v $build_cmd > /dev/null 2>&1 || { + build_cmd="build_shmk $lib $DIST/$name" + } + cmdlist="$cmdlist + $build_cmd #Build library $name" + done + + for prog in $PROGS; do + name=$(basename $prog) + build_cmd=prog_$prog + command -v $build_cmd > /dev/null 2>&1 || { + build_cmd="build_shmk $prog $DIST/$name" + } + cmdlist="$cmdlist + $build_cmd #Build program $name" + done + + # build all libs + ;; + esac + shift + done + + local out len i + $verbose && out=/dev/stdout || out=/dev/null + len=$(($(echo "$cmdlist" | wc -l)-1)) + i=-1 + echo "$cmdlist" | while read -r cmd; do + i=$((i+1)) + printf "${LIGHT_BLACK}[${LIGHT_BLUE}%s${LIGHT_BLACK}/${LIGHT_BLUE}%s${LIGHT_BLACK}] ${LIGHT_WHITE}%s${RESET}\n" "$i" "$len" "${cmd#*#}" + ${cmd%#*} 2>&1 > $out || { + printf "${RED}Error $?\n" + exit 1 + } + done +} + +search_path=". +/usr/lib +/usr/local/lib +/usr/share/shmk +" + +verbose=false +clean=false + +while getopts ":e:I:o:chv" opt; do + case "${opt}" in + e) + entry_function="$OPTARG" + ;; + o) + output="$OPTARG" + ;; + c) + clean=true + ;; + I) + search_path="$search_path + $OPTARG" + ;; + v) + verbose=true + ;; + h) + usage && exit 0 + ;; + esac +done + +shift $((OPTIND-1)) + +[ -z $1 ] && { + exit 1 +} + +echo "$0 $*" +[ -f "$1" ] && shebang="$(head -1 $1)" +case "$shebang" in + "#!"*"shmk"*) + interpret_shmk $@ + ;; + *) + build_shmk $@ + ;; +esac + + diff --git a/src/util/shtests.sh b/src/util/shtests.sh new file mode 100644 index 0000000..69da232 --- /dev/null +++ b/src/util/shtests.sh @@ -0,0 +1,66 @@ +#!/bin/sh +# +# Simple shell test suite +# +# will run all functions with a name starting with test_ +# the return value of said function will determine if its a pass or fail +# +# to run a shell file full of unit tests, run: +# shtests [FILE] +# + +#include colors + +PASS="${BLUE}[ ${GREEN}PASS${BLUE} ]${RESET}" +FAIL="${BLUE}[ ${RED}FAIL${BLUE} ]${RESET}" + +V=false + +runtest () { + test_name=$(echo $1 | sed "s/_/ /g") + test_func="$2" + printf "${BLUE}[ ] ${RESET}$test_name "; + if "$test_func" ; then + printf "\r$PASS\n" + return 0 + else + printf "\r$FAIL\n" + return 1 + fi +} + +# TODO use getopt for this +if [ "$1" = "-v" ]; then + shift + V=true +fi + +if [ $# = "0" ]; then + printf "${RED}No tests file has been provided\n" + exit 1; +else + . $@ +fi + +tests=$(sed -n "s/^test_\(.*\)\s*()\s*{/\1/p" $@) +total=$(echo $tests | wc -w) +passed=0 +failed=0 + +printf "${BLUE}Running $total tests: \n" +for name in $tests; do + if runtest "$name" "test_$name"; then + passed=$((passed+1)) + else + failed=$((failed+1)) + fi +done + +printf "\n${BLUE}Summary for $total tests:\n" +printf "\t${PASS} $passed\n" +printf "\t${FAIL} $failed\n" +printf "\n" + +[ "$passed" = "$total" ] || exit 1 + + diff --git a/src/xichroot.sh b/src/xichroot.sh deleted file mode 100644 index 7136e25..0000000 --- a/src/xichroot.sh +++ /dev/null @@ -1,58 +0,0 @@ -#!/bin/bash -# This script is intended for root users to automatically mount requried dirs and chroot into an installed system -# simplifed version of arch-chroot from - -args=$@ -target=$1 - -die() { printf "$@\n"; exit 1;} - -ignore_error() { - "$@" 2>/dev/null - return 0 -} - -chroot_add_mount() { - mount "$@" && CHROOT_ACTIVE_MOUNTS=("$2" "${CHROOT_ACTIVE_MOUNTS[@]}") -} - -chroot_maybe_add_mount() { - local cond=$1; shift - if eval "$cond"; then - chroot_add_mount "$@" - fi -} - -chroot_setup() { - CHROOT_ACTIVE_MOUNTS=() - [[ $(trap -p EXIT) ]] && die '(BUG): attempting to overwrite existing EXIT trap' - trap 'chroot_teardown' EXIT - - chroot_add_mount proc "$target/proc" -t proc -o nosuid,noexec,nodev - chroot_add_mount sys "$target/sys" -t sysfs -o nosuid,noexec,nodev,ro - ignore_error chroot_maybe_add_mount "[[ -d '$1/sys/firmware/efi/efivars' ]]" \ - efivarfs "$target/sys/firmware/efi/efivars" -t efivarfs -o nosuid,noexec,nodev - chroot_add_mount udev "$target/dev" -t devtmpfs -o mode=0755,nosuid - chroot_add_mount devpts "$target/dev/pts" -t devpts -o mode=0620,gid=5,nosuid,noexec - chroot_add_mount shm "$target/dev/shm" -t tmpfs -o mode=1777,nosuid,nodev - chroot_add_mount /run "$target/run" --bind - chroot_add_mount tmp "$target/tmp" -t tmpfs -o mode=1777,strictatime,nodev,nosuid -} - -chroot_teardown() { - if (( ${#CHROOT_ACTIVE_MOUNTS[@]} )); then - umount "${CHROOT_ACTIVE_MOUNTS[@]}" - fi - unset CHROOT_ACTIVE_MOUNTS -} - -[ "$EUID" -eq 0 ] || die "Please run this as root" -[[ -d $target ]] || die "$target is not a directory; cannot chroot!" - -chroot_setup $@ - -if [ $# -gt 1 ]; then - chroot $@ -else - chroot $@ /bin/sh -l -fi diff --git a/src/xilib.sh b/src/xilib.sh deleted file mode 100644 index 57dd10c..0000000 --- a/src/xilib.sh +++ /dev/null @@ -1,34 +0,0 @@ -#!/bin/sh - -# format a number into a bytes, kibibytes, mebibytes, or gibibytes -# -format_bytes () { - case "1" in - "$(($1>=1<<30))") printf "$(($1>>30))GiB";; - "$(($1>=1<<20))") printf "$(($1>>20))MiB";; - "$(($1>=1<<10))") printf "$(($1>>10))kiB";; - *) printf "$1B";; - esac -} - -# ensure that the user is a root user -# -checkroot () { - [ "$(id -u)" = "0" ] || { - printf "${RED}Please run as root!${RESET}\n" - exit 1 - } -} - -# reverse the order of lines -# -reverse_lines () { - local result= - while IFS= read -r line; do - result="$line - $result" - done - echo "$result" -} - - diff --git a/src/xitui.sh b/src/xitui.sh deleted file mode 100644 index dcfaf02..0000000 --- a/src/xitui.sh +++ /dev/null @@ -1,451 +0,0 @@ -#!/bin/sh - -################################################################# -#███████ ███████ ███████ ███████ ███████ ███████ ███████ ███████# -#█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █# -#█ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █# -#█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █# -#█ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███# -#█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ # -#█ ███████ ███████ ███████ ███████ ███████ ███████ ███████ █████# -# # -# # -# # -# # -# # -# # -# # -#█ ███████ ███████ ███████ ███████ ███████ ███████ ███████ █████# -#█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ # -#█ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███# -#█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █# -#█ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █# -#█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █# -#███████ ███████ ███████ ███████ ███████ ███████ ███████ ███████# -################################################################# - -. /usr/lib/colors.sh - -COLOR_FG=${WHITE} -COLOR_FG2=${LIGHT_BLUE} -COLOR_PTRN=${LIGHT_BLUE} -COLOR_BG=${BG_WHITE} - -t_init () { - export LINES=$(tput lines) - export COLUMNS=$(tput cols) - t_styl_cur l - tput clear - t_set_tty - t_gen_ptrn - -} - -t_set_tty () { - export SAVED_TTY_SETTINGS=$(stty -g) - stty raw -echo -} - -t_clean () { - stty $SAVED_TTY_SETTINGS - tput clear - t_yes_cur -} - -readc () { - stty -echo -icanon min 1 time 0 - s="$(dd bs=1 count=1 of=/dev/stdout 2>/dev/null)" - stty -icanon min 0 time 0 - [ "$s" = "" ] && { - s="$s$(dd bs=1 count=2 of=/dev/stdout 2>/dev/null)" - } - printf "$s" -} - - -t_cls () { - tput clear -} - -t_no_cur () { - tput civis -} - -t_yes_cur () { - tput cnorm -} - -t_set_cur () { - printf "[$1;$2H" -} - -max_line_length () { - local max=-1 - while IFS= read -r line; do - [ "${#line}" -gt "$max" ] && max=${#line} - done - echo "$max" -} - -t_styl_cur () { - case "$1" in - H) printf "\033[2 q";; - H0) printf "\033[1 q";; - l) printf "\033[6 q";; - l0) printf "\033[5 q";; - _) printf "\033[4 q";; - _0) printf "\033[3 q";; - *) printf"\033[0 q]";; - esac > $(tty) -} - - -t_gen_ptrn () { - export P="" - for l in $(seq 7); do - for c in $(seq $COLUMNS); do - case "$l" in - 1) [ "$((c%8))" != "0" ] ;; - 2) [ "$((c%8))" = "1" ] || [ "$((c%8))" = "7" ] ;; - 3) [ "$((c%2))" = "1" ] || [ "$((c%8))" = "4" ] ;; - 4) [ "$((c%2))" = "1" ] ;; - 5) [ "$((c%2))" != "0" ] || [ "$((c%8))" = "2" ] ;; - 6) [ "$((c%8))" = "5" ] || [ "$((c%8))" = "7" ] ;; - 7) [ "$((c%8))" != "6" ] ;; - *) false ;; - esac && P="$P█" || P="$P " - done - P="$P\n" - done -} - -t_prnt_ptrn () { - printf "$@$P" -} - -t_cls_ptrn () { - printf "" - printf " %.0s" $(seq $(( (LINES-14) * COLUMNS))) - t_drw_ptrn -} - -t_drw_ptrn () { - t_drw_txt_clr "${COLOR_PTRN}" 1 1 "$P" - t_drw_txt_clr "${COLOR_PTRN}" 1 $((LINES-6)) "$(printf "$P" | rev)" -} - -# draws inside the given area -# t_drw_box x y w h -# -t_drw_box () { - local x=$1 y=$2 w=$3 h=$4 - - - l="$(printf "${COLOR_FG2}║" - printf " %.0s" $(seq $((w-2))) - printf "${COLOR_FG2}║" - )" - b="$(printf "═%.0s" $(seq $((w-2))))" - - printf "[${y};${x}H${COLOR_FG2}╔$b╗" - for i in $(seq $((y+1)) $((y+h-1))); do - printf "[${i};${x}H$l" - done - printf "[$((y+h-1));${x}H${COLOR_FG2}╚$b╝" -} - -# draw text at location -# t_drw_txt x y [txt...] -# -t_drw_txt () { - local x=$1 y=$2 - shift 2 - - echo "$@" | while IFS= read -r line; do - printf "[${y};${x}H$line" - y=$((y+1)) - done -} - -# draw colored text -# -t_drw_txt_clr () { - local clr=$1 x=$2 y=$3 - shift 3 - - echo "$@" | while IFS= read -r line; do - printf "[${y};${x}H${clr}$line" - y=$((y+1)) - done -} - -t_msg () { - h_txt_w_h "$@\n" - h_cntr $w $h - t_drw_box $((x-1)) $((y-1)) $((w+2)) $((h+2)) - t_drw_txt_clr "${COLOR_FG}" $x $y "$@" -} - -h_drw_btns () { - local sel=$1 i=0 - while read -r btn; do - [ "$sel" = "$i" ] && styl="${COLOR_BG}" || styl="${COLOR_FG2}" - h_drw_btn "${styl}" ${btn} - i=$((i+1)) - done -} - -h_drw_btn () { - styl="$1" x=$2 y=$3 ; shift 3; txt="$@" - t_drw_txt $x $y "${COLOR_FG2}${styl}$txt${RESET}" -} - -h_cntr() { - local w=$1 h=$2 - x=$(( ( (COLUMNS+w)/2 ) - $1 )) - y=$(( ( (LINES+h)/2 ) - $2 )) -} - -h_txt_w_h() { - w=$(printf "$*" | max_line_length) - h=$(printf "$*" | wc -l) -} - -h_t_result() { - local s=$1 i=0; shift - for opt in "$@"; do - case " $s " in - *" $i "*) echo "$opt";; - esac - i=$((i+1)) - done -} - -t_dialog() { - # TODO these are messy af - local msg="$1"; shift - local btns_len="$#" w_btns=$(echo "$*" | wc -c) - - h_txt_w_h "$msg\n $*\n" - h_cntr $w $h - - local btn_y=$((y+h-1)) \ - btn_x=$(( ((COLUMNS+w_btns) / 2) - w_btns)) - - btns="" - for btn in "$@"; do - btns="$btns$btn_x $btn_y $btn\n" - btn_x=$((btn_x+${#btn}+1)) - done - - t_drw_box $((x-1)) $((y-1)) $((w+2)) $((h+2)) - t_drw_txt_clr ${COLOR_FG} $x $y "$msg" - - local sel="0" - while true; do - printf "$btns" | h_drw_btns $sel - - case "$(readc)" in - ' ') break;; - ''|''|' ') - sel=$(((sel+1)%btns_len));; - ''|'') - sel=$(((sel+btns_len-1)%btns_len));; - esac - - done - export T_RESULT=$(h_t_result "$sel" "$@") -} - -t_radio() { - local msg="$1"; shift - - local btns_len="$#" w_btns=$(echo "$*" | wc -c) - text=`{ - printf "$msg\n" - for b in "$@"; do - printf "╚═$b${RESET}\n" - done - }` - - t_msg "$text" - - x=$((x+2)) y=$((y+1)) - local py=$((y)) - btns="" - for btn in "$@"; do - btns="$btns$x $y $btn\n" - y=$((y+1)) - done - - local sel="0" - while true; do - printf "$btns" | h_drw_btns $sel - - case "$(readc)" in - ' ') break;; - ''|''|' ') - sel=$(((sel+1)%btns_len));; - ''|'') - sel=$(((sel+btns_len-1)%btns_len));; - esac - - done - export T_RESULT=$(h_t_result "$sel" "$@") -} - -t_check () { - local msg="$1"; shift - local btns_len="$#" - btns_len=$((btns_len+1)) - - text=`{ - printf "$msg\n" - for b in "$@"; do - printf "[ ] $b${RESET}\n" - done - printf " ${RESET}\n" - }` - - t_msg "$text" - - x=$((x+4)) y=$((y+1)) - local py=$((y)) - btns="" - for btn in "$@"; do - btns="$btns$x $y $btn\n" - y=$((y+1)) - done - btns="$btns$x $y \n" - - local sel="0" checked="" - while true; do - printf "$btns" | h_drw_btns $sel - case "$(readc)" in - ''|''|' ') - sel=$(((sel+1)%btns_len)) - ;; - ''|'') - sel=$(((sel+btns_len-1)%btns_len));; - ' '|' ') - [ "$((sel+1))" = "$btns_len" ] && break || { - case " $checked " in - *$sel*) - checked=$(echo "$checked" | sed "s/$sel//") - t_drw_txt $((x-3)) $((py+sel)) " " - ;; - *) - checked="$checked$sel " - t_drw_txt $((x-3)) $((py+sel)) "x" - ;; - esac - };; - esac - done - export T_RESULT=$(h_t_result "$checked" "$@") -} - -t_prompt () { - t_dialog "$@" "" -} - -t_yesno () { - t_dialog "$@" "" "" - [ "$T_RESULT" = "" ] -} - -t_input () { - stty $SAVED_TTY_SETTINGS - t_msg "$@ - ->" > $(tty) - t_yes_cu - read var - t_set_tty - t_no_cur - export T_RESULT="$var" -} - -t_input_hidden () { - stty $SAVED_TTY_SETTINGS - stty -echo - t_msg "$@ - ->" > $(tty) - t_yes_cur - read var - t_set_tty - t_no_cur - export T_RESULT="$var" -} - -t_tail() { - t_drw_txt 0 7 "$(tail -$((LINES-14)) $1)" -} - -t_paged_radio () { - perpage=9 - heading=$1 - shift - - T_RESULT="more..." - page=0 - max_pages=$((($#/perpage)+1)) - while true; do - start=$((page*perpage)) - current=$(echo $@ | tr ' ' '\n' | tail -$(($#-start)) | head -$perpage) - current="$current more..." - - t_cls_ptrn - t_radio "$heading" $current - [ "$T_RESULT" = "more..." ] && page=$(((page+1)%max_pages)) || return 0 - done -} - - -t_demo () { - t_init - t_no_cur - - - t_cls_ptrn - t_prompt "Hello world?" - t_cls_ptrn - - t_paged_radio "Pick a number" $(seq 34) - t_prompt "You selected the following: $T_RESULT" - t_cls_ptrn - - exit 1 - - t_radio "Pick one:" "toast" "bread" "bread but fishy" "empty sandwich" - t_prompt "You selected the following: $T_RESULT" - t_cls_ptrn - - t_check "Please select your order:" "yeast" "fish and chips" "twigs" "tuna" "pizza" "aspic" - t_prompt "You selected the following: $T_RESULT" - t_cls_ptrn - - t_dialog "Can a match box?" "" "" "" - t_prompt "You selected the following: $T_RESULT" - t_cls_ptrn - - #t_cls_ptrn - t_input "Enter your name" - t_prompt "Hello $T_RESULT" - t_cls_ptrn - - file=$2.sh - [ -f $file ] && { - t_tail $file - } || { - t_prompt "The file $file does not exist" - } - t_prompt "Clear" - t_cls_ptrn - t_prompt "Exit?" - - t_clean -} - -"$@" -- cgit v1.2.1