3 # Copyright 2014, 2015 Paul Hänsch
5 # This file is part of shcgi.
7 # shcgi 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 # shcgi 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 shcgi. If not, see <http://www.gnu.org/licenses/>.
21 # set up directories for object storage
23 [ -d "$_DATA" -a -w "$_DATA" ] || die "storage directory must be writable"
25 [ ! -e "$_DATA/$each" ] && mkdir "$_DATA/$each"
26 [ -d "$_DATA/$each" -a -w "$_DATA/$each" ] || die "storage \"$_DATA/$each\" must be a writable directory"
31 # print value if value matches regex; otherwise print default
33 regex="$(printf %s\\n "$2" \
34 | sed -r 's;(^|[^\\]+)((\\\\)+)/;\1\2\\/;g; s;(^|[^\\])/;\1\\/;g; s;(^|[^\\]+)((\\\\)+)/;\1\2\\/;g; s;(^|[^\\])/;\1\\/;g;'
35 )" # ^^ escape only unescaped slash characters for later insertion
38 printf %s\\n "${value}" \
39 | sed -rn "2q; /^(${regex})\$/{p;q}; a${default}"
43 # print default if value matches regex; otherwise print value
45 regex="$(printf %s\\n "$2" \
46 | sed -r 's;(^|[^\\]+)((\\\\)+)/;\1\2\\/;g; s;(^|[^\\])/;\1\\/;g; s;(^|[^\\]+)((\\\\)+)/;\1\2\\/;g; s;(^|[^\\])/;\1\\/;g;'
47 )" # ^^ escape only unescaped slash characters for later insertion
50 printf %s\\n "${value}" \
51 | sed -rn "2q; /^(${regex})\$/{bd}; p;q; :d;a${default}"
56 [ -n "${item_name[$1]+x}" ] && printf %s "$item_name[$1]" || printf %s "$1"