3 # Copyright 2018, 2019 Paul Hänsch
5 # This is a file format helper, part of CGIlite.
7 # CGIlite is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # CGIlite is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with CGIlite. If not, see <http://www.gnu.org/licenses/>.
20 [ -n "$include_storage" ] && return 0
28 local lock timeout block
31 if [ \! -w "${lock%/*}" ] || [ -e "$lock" -a \! -d "$lock" ]; then
32 printf 'Impossible to get lock: %s\n' "$lock" >&2
36 while ! mkdir "$lock" 2>&-; do
37 block="$(cat "$lock/pid" || printf 1)"
38 if ! { ps -eo pid |grep -qwF "$block"; }; then
39 printf 'Overriding stale lock: %s\n' "$lock" >&2
42 if [ $timeout -le 0 ]; then
43 printf 'Timeout while trying to get lock: %s\n' "$lock" >&2
46 timeout=$((timeout - 1))
49 printf '%i\n' $$ >"${lock}/pid"
56 if [ "$(cat "$lock/pid")" = "$$" ]; then
58 if ! rmdir "$lock"; then
59 printf 'Cannot remove tainted lock: %s\n' "$lock" >&2
60 printf '%i\n' $$ >"${lock}/pid"
65 printf 'Refusing to release foreign lock: %s\n' "$lock" >&2
80 { [ $# -eq 0 ] && cat || printf %s "$*"; } \
81 | sed -E ':X; $!{N;bX;}'"$STRING"
86 [ $# -gt 0 ] && in="$*" || in="$(cat)"
87 while [ "$in" ]; do case $in in
88 \\*) out="${out}\\\\"; in="${in#\\}" ;;
89 "$BR"*) out="${out}\\n"; in="${in#${BR}}" ;;
90 "$CR"*) out="${out}\\r"; in="${in#${CR}}" ;;
91 " "*) out="${out}\\t"; in="${in# }" ;;
92 +*) out="${out}\\+"; in="${in#+}" ;;
93 " "*) out="${out}+"; in="${in# }" ;;
94 *) out="${out}${in%%[\\${CR}${BR} + ]*}"; in="${in#"${in%%[\\${BR}${CR} + ]*}"}" ;;
102 s;((^|[^\\])(\\\\)*)\\n;\1\n;g;
103 s;((^|[^\\])(\\\\)*)\\t;\1\t;g;
104 s;((^|[^\\])(\\\\)*)\\r;\1\r;g;
105 s;((^|[^\\])(\\\\)*)\+;\1 ;g;
107 s;((^|[^\\])(\\\\)*)\\\+;\1+;g;
111 { [ $# -eq 0 ] && cat || printf %s "$*"; } \
116 [ $# -gt 0 ] && in="$*" || in="$(cat)"
117 while [ "$in" ]; do case $in in
118 \\\\*) out="${out}\\"; in="${in#\\\\}" ;;
119 \\n*) out="${out}${BR}"; in="${in#\\n}" ;;
120 \\r*) out="${out}${CR}"; in="${in#\\r}" ;;
121 \\t*) out="${out} "; in="${in#\\t}" ;;
122 \\+) out="${out}+"; in="${in#\\+}" ;;
123 +*) out="${out} "; in="${in#+}" ;;
124 \\*) in="${in#\\}" ;;
125 *) out="${out}${in%%[\\+]*}"; in="${in#"${in%%[\\+]*}"}" ;;