summaryrefslogtreecommitdiff
path: root/src/util/shmk.sh
diff options
context:
space:
mode:
authordavidovski <david@davidovski.xyz>2026-09-29 11:03:49 +0100
committerdavidovski <david@davidovski.xyz>2026-09-29 11:03:49 +0100
commitf20bd9d6369c2e1246dcdd4af26b085da99c18e0 (patch)
treec89ef80752867ef852397cdc8600ee1d7dce3963 /src/util/shmk.sh
parent982b671051d988b374808cd7bd67310d8e9a4d51 (diff)
Add readme for shmk
Diffstat (limited to 'src/util/shmk.sh')
-rwxr-xr-xsrc/util/shmk.sh64
1 files changed, 48 insertions, 16 deletions
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
-