]> git.plutz.net Git - cgilite/blobdiff - storage.sh
bugfix, STRING and UNSTRING input like "foo[bar]"
[cgilite] / storage.sh
index d4fbca36714a0cfe08db9ea9d465d43b3ef83b9c..7f70e6480d6c89c8a32d3a9ee3fb68a5bfcc35b6 100755 (executable)
@@ -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.
 # 
 # You should have received a copy of the GNU Affero General Public License
 # along with CGIlite.  If not, see <http://www.gnu.org/licenses/>. 
 
+[ -n "$include_storage" ] && return 0
+include_storage="$0"
+
+CR="\r"
+BR='
+'
+
 LOCK(){
+  local lock timeout block
   lock="${1}.lock"
   timeout="${2-20}"
   if [ \! -w "${lock%/*}" ] || [ -e "$lock" -a \! -d "$lock" ]; then
@@ -25,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
@@ -39,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"
@@ -51,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"
 }
+