From f20bd9d6369c2e1246dcdd4af26b085da99c18e0 Mon Sep 17 00:00:00 2001 From: davidovski Date: Tue, 29 Sep 2026 11:03:49 +0100 Subject: Add readme for shmk --- src/util/shmk.sh | 64 ++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 16 deletions(-) (limited to 'src/util/shmk.sh') diff --git a/src/util/shmk.sh b/src/util/shmk.sh index 75e1445..b4c040d 100755 --- a/src/util/shmk.sh +++ b/src/util/shmk.sh @@ -13,7 +13,11 @@ ${BLUE}Available Options: EOF } -find_file () { +# find a file in the search tree +# +# _find_file filename +# +_find_file () { for p in $search_path; do for f in $p/$1.sh $p/$1; do $verbose \ @@ -28,20 +32,31 @@ find_file () { return 1 } -parse_lines () { +# parse the lines of a file +# +# _parse_lines < file +# +_parse_lines () { + + # keep a running log of which files we've already included included="" - while IFS= read -r line; do - case "$line" in + + # go through the lines + while IFS= read -r line; do + case "$line" in "#include "*) - file="$(find_file ${line#\#include})" - case "$included" in - *"$file"*) - ;; + # find the file + file="$(_find_file ${line#\#include})" + + # if we have already included this file, do not parse it + case "$included" in + *"$file"*) ;; *) - cat $file | parse_lines + cat $file | _parse_lines included="$included $file" esac ;; + "#>"*) eval ${line#'#>'} ;; @@ -56,6 +71,11 @@ parse_lines () { done } +# 'build' a shell file too an output +# goes through the file without running it and interprets all shmk commands +# +# build_shmk file [output] +# build_shmk () { [ -f "$1" ] || return 1 [ ! -z "$2" ] && output="$2" @@ -72,11 +92,13 @@ build_shmk () { [ ! -d "${output%/*}" ] && mkdir -p "${output%/*}" cat $1 \ - | parse_lines > ${output} && + | _parse_lines > ${output} && [ ! -z ${entry_function} ] && echo "$entry_function" >> ${output} chmod +x ${output} } +# take a .shmk file and build it +# interpret_shmk () { . $1 local cmdlist="" @@ -173,14 +195,23 @@ interpret_shmk () { } search_path=". -/usr/lib -/usr/local/lib -/usr/share/shmk +$DIST/ +/usr/lib/ +/usr/local/lib/ +/usr/share/shmk/ " verbose=false clean=false +# test shmk was invoked on a file if so put the filename at the end because it will break getopts +# this is a sketchy workaround because the shebang will always put the +# filename of the project file as the first arg, so this way we can +# happily introduce more arguments +[ -f "$1" ] && { + set -- $* $1 +} + while getopts ":e:I:o:chv" opt; do case "${opt}" in e) @@ -193,8 +224,8 @@ while getopts ":e:I:o:chv" opt; do clean=true ;; I) - search_path="$search_path - $OPTARG" + search_path="$OPTARG + $search_path" ;; v) verbose=true @@ -212,7 +243,9 @@ shift $((OPTIND-1)) } echo "$0 $*" + [ -f "$1" ] && shebang="$(head -1 $1)" + case "$shebang" in "#!"*"shmk"*) interpret_shmk $@ @@ -222,4 +255,3 @@ case "$shebang" in ;; esac - -- cgit v1.2.3