From 30f089d3ca3e97607934177b19b49e022d729e8e Mon Sep 17 00:00:00 2001 From: zawz Date: Tue, 4 Aug 2020 12:26:10 +0200 Subject: [PATCH] update shcompile --- scripts/shcompile | 35 ++++++++++++++++++++++++----------- src/main.sh | 7 ++----- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/scripts/shcompile b/scripts/shcompile index 67426fa..6b4e8c3 100755 --- a/scripts/shcompile +++ b/scripts/shcompile @@ -9,7 +9,14 @@ Resolves '%include' lines with shell capacity" } # no arg -[ $# -lt 1 ] && usage && exit 1 +unset infile +if [ $# -lt 1 ] +then + if ! [ -t 0 ] + then infile=/dev/stdin + else usage && exit 1 + fi +fi [ -z "$TMPDIR" ] && TMPDIR=/tmp tmpdir="$TMPDIR/shcompile_$(tr -dc '[:alnum:]' < /dev/urandom | head -c10)" @@ -25,23 +32,28 @@ stop() exit $1 } -infile=$1 +[ -z "$infile" ] && infile=$1 +dirname=$() [ "$infile" = '-' ] && infile=/dev/stdin -cat "$infile" >/dev/null 2>&1 || { echo "Error: cannot read '$infile'" >&2 && stop 2; } +# create copy +cat "$infile" > "$tmpfile" 2>&1 || { echo "Error: cannot read '$infile'" >&2 && stop 2; } + +# env when file +[ "$infile" != "/dev/stdin" ] && { + echo "$(readlink -f "$infile")" > "$filelist" + cd "$(dirname "$infile")" +} + +firstline=$(head -n1 "$tmpfile" | grep '^#!/') -firstline=$(head -n1 "$infile" | grep '^#!/') [ -z "$firstline" ] && firstline='#!/bin/sh' -cp "$infile" "$tmpfile" - get_include_line() { grep -m1 -n '^%include ' "$1" | cut -d':' -f1 } -echo "$1" > "$filelist" - n=$(get_include_line "$tmpfile") while [ -n "$n" ] do @@ -53,12 +65,13 @@ do cp "$headfile" "$tmpfile" echo "$inc" | while read -r file do - cat "$file" >/dev/null 2>&1 || { echo "Error when trying to include '$incarg': '$ln' could not be read" >&2 && stop 10; } - if ! grep -q "^$file\$" "$filelist" + cat "$file" >/dev/null 2>&1 || { echo "Error when trying to include '$incarg': '$file' could not be read" >&2 && stop 10; } + if ! grep -q "^$(readlink -f "$file")\$" "$filelist" then # not already included cat "$file" >> "$tmpfile" - echo "$file" >> "$filelist" + echo "$(readlink -f "$file")" >> "$filelist" fi + cd "$pwd" done cat "$tailfile" >> "$tmpfile" # get next include line diff --git a/src/main.sh b/src/main.sh index 1856195..a837c1f 100644 --- a/src/main.sh +++ b/src/main.sh @@ -1,13 +1,10 @@ #!/bin/sh # ordered requirements -%include src/util.sh -%include src/env.sh -%include src/options.sh -%include src/config.sh +%include util.sh env.sh options.sh config.sh # everything else -%include src/*.sh +%include *.sh case "$1" in list) awk '{print $1}' "$PKG_PATH/installed" 2>/dev/null | sort ;;