]> git.plutz.net Git - cgilite/blob - misc.sh
function for reading http headers
[cgilite] / misc.sh
1 #!/bin/zsh
2
3 # Copyright 2014 - 2017 Paul Hänsch
4 #
5 # This file is part of shcgi.
6
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.
11
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.
16
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/>. 
19
20 BR='
21 '
22
23 data_dirs(){
24   # set up directories for object storage
25
26   [ -d "$_DATA" -a -w "$_DATA" ] || die "storage directory must be writable"
27   for each in "$@"; do
28     [ ! -e "$_DATA/$each" ] && mkdir "$_DATA/$each"
29     [ -d "$_DATA/$each" -a -w "$_DATA/$each" ] || die "storage \"$_DATA/$each\" must be a writable directory"
30   done
31 }
32
33 validate(){
34   # print value if value matches regex; otherwise print default
35   value="$1"
36   regex="$2"
37   default="$3"
38
39   printf %s "${value%%${BR}*}" |grep -xE "$regex" || printf %s "$default"
40 }
41
42 invalidate(){
43   # print default if value matches regex; otherwise print value
44   value="$1"
45   regex="$2"
46   default="$3"
47   
48   printf %s "${value%%${BR}*}" |grep -qxE "$regex" && printf %s "$default" || printf %s "${value%%${BR}*}"
49 }
50
51 declare -A item_name
52 l10n(){
53   [ -n "${item_name[$1]+x}" ] && printf %s "$item_name[$1]" || printf %s "$1"
54 }