X-Git-Url: http://git.plutz.net/?p=cgilite;a=blobdiff_plain;f=storage.sh;h=7f70e6480d6c89c8a32d3a9ee3fb68a5bfcc35b6;hp=1d8764be745b1c0168a7b56302fc42340be272de;hb=f0383eeb634ce1ae71bfb409a9bc25d84b6d79e9;hpb=595fbc514d410ccdd46a81b8736d3711ac1ca7fe diff --git a/storage.sh b/storage.sh index 1d8764b..7f70e64 100755 --- a/storage.sh +++ b/storage.sh @@ -1,6 +1,6 @@ #!/bin/sh -# Copyright 2018 Paul Hänsch +# Copyright 2018, 2019 Paul Hänsch # # This is a file format helper, part of CGIlite. # @@ -20,7 +20,12 @@ [ -n "$include_storage" ] && return 0 include_storage="$0" +CR=" " +BR=' +' + LOCK(){ + local lock timeout block lock="${1}.lock" timeout="${2-20}" if [ \! -w "${lock%/*}" ] || [ -e "$lock" -a \! -d "$lock" ]; then @@ -28,7 +33,7 @@ LOCK(){ return 1 fi - while ! mkdir "$lock"; do + while ! mkdir "$lock" 2>&-; do block="$(cat "$lock/pid" || printf 1)" if ! { ps -eo pid |grep -qwF "$block"; }; then printf 'Overriding stale lock: %s\n' "$lock" >&2 @@ -42,10 +47,11 @@ LOCK(){ sleep 1 done printf '%i\n' $$ >"${lock}/pid" - return 1 + return 0 } RELEASE(){ + local lock lock="${1}.lock" if [ "$(cat "$lock/pid")" = "$$" ]; then rm "$lock/pid" @@ -54,29 +60,70 @@ RELEASE(){ printf '%i\n' $$ >"${lock}/pid" return 1 fi + return 0 else printf 'Refusing to release foreign lock: %s\n' "$lock" >&2 return 1 fi } +STRING=' + s;\\;\\\\;g; + s;\n;\\n;g; + s;\t;\\t;g; + s;\r;\\r;g; + s;\+;\\+;g; + s; ;+;g; +' + +STRING_OLD(){ + { [ $# -eq 0 ] && cat || printf %s "$*"; } \ + | sed -E ':X; $!{N;bX;}'"$STRING" +} + STRING(){ - printf %s "$*" |sed -r ' - :X; $!{N;bX;} - s;\\;\\\\;g; - s;\n;\\n;g; - s;\t;\\t;g; - s;\r;\\r;g; - ' + local in out='' + [ $# -gt 0 ] && in="$*" || in="$(cat)" + while [ "$in" ]; do case $in in + \\*) out="${out}\\\\"; in="${in#\\}" ;; + "$BR"*) out="${out}\\n"; in="${in#${BR}}" ;; + "$CR"*) out="${out}\\r"; in="${in#${CR}}" ;; + " "*) out="${out}\\t"; in="${in# }" ;; + +*) out="${out}\\+"; in="${in#+}" ;; + " "*) out="${out}+"; in="${in# }" ;; + *) out="${out}${in%%[\\${CR}${BR} + ]*}"; in="${in#"${in%%[\\${BR}${CR} + ]*}"}" ;; + esac; done + printf '%s' "$out" } + +UNSTRING=' + :UNSTRING_X + s;((^|[^\\])(\\\\)*)\\n;\1\n;g; + s;((^|[^\\])(\\\\)*)\\t;\1\t;g; + s;((^|[^\\])(\\\\)*)\\r;\1\r;g; + s;((^|[^\\])(\\\\)*)\+;\1 ;g; + tUNSTRING_X; + s;((^|[^\\])(\\\\)*)\\\+;\1+;g; + s;\\\\;\\;g; +' +UNSTRING_OLD(){ + { [ $# -eq 0 ] && cat || printf %s "$*"; } \ + | sed -E "$UNSTRING" +} UNSTRING(){ - printf %s "$*" |sed -r ' - :X - s;((^|[^\\])(\\\\)*)\\n;\1\n;g; - s;((^|[^\\])(\\\\)*)\\t;\1\t;g; - s;((^|[^\\])(\\\\)*)\\r;\1\r;g; - tX; - s;\\\\;\\;g; - ' + local in out='' + [ $# -gt 0 ] && in="$*" || in="$(cat)" + while [ "$in" ]; do case $in in + \\\\*) out="${out}\\"; in="${in#\\\\}" ;; + \\n*) out="${out}${BR}"; in="${in#\\n}" ;; + \\r*) out="${out}${CR}"; in="${in#\\r}" ;; + \\t*) out="${out} "; in="${in#\\t}" ;; + \\+) out="${out}+"; in="${in#\\+}" ;; + +*) out="${out} "; in="${in#+}" ;; + \\*) in="${in#\\}" ;; + *) out="${out}${in%%[\\+]*}"; in="${in#"${in%%[\\+]*}"}" ;; + esac; done + printf '%s' "$out" } +