]> git.plutz.net Git - cgilite/blob - files.sh
documentation fo STORE function
[cgilite] / files.sh
1 #!/bin/sh
2
3 # Copyright 2018 Paul Hänsch
4 #
5 # This is a file format helper, part of CGIlite.
6
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.
11
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.
16
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/>. 
19
20 # ksh and zsh workaround
21 case "${0##*/}" in
22   zsh) setopt -o OCTAL_ZEROES 2>&- ;; # zsh, mostly ignored by other shells
23   ksh) set -o posix ;;                # ksh, will crash most other shells
24 esac
25
26 STORE(){
27   # Store fields from a HTTP GET or POST string in a file
28   # usage: STORE "${QUERY_STRING}"
29   #    or: STORE "$(head -n $HTTP_CONTENT_LENGTH)"
30   # backslashes and newline characters will be escaped in field values,
31   # the escape symbol is a backslash
32   # hexadecimal character descriptions (%00 - %FF) will be expanded
33   # the + character will be converted to [space]
34   # one line in the output corresponds to exactly one field, so you can
35   # use grep to filter which fields of a query should be stored
36   printf "$(
37   printf '%s' "$@" \
38   | sed -r ':X; $bY; N; bX; :Y;
39             s;\+; ;g; s;(\\|5[Cc]);\\\\\\\\;g; s;(\n|%0[Aa]);\\\\n;g; s;(^|&)([^=]+)=;\1\2:;g; s;&;\n;g;
40             s;^\n+;;; s;$;\\n;; s;\n+;\n;g;
41             # Hexadecimal { %00 - %FF } will be transformed to octal { \000 - \377 } for posix printf
42             s;%[0123].;&\\0;g; s;%[4567].;&\\1;g; s;%[89AB].;&\\2;g; s;%[CDEF].;&\\3;g;
43             s;%[048C][0-7]\\.;&0;g; s;%[048C][89A-F]\\.;&1;g; s;%[159D][0-7]\\.;&2;g; s;%[159D][89A-F]\\.;&3;g;
44             s;%[26AE][0-7]\\.;&4;g; s;%[26AE][89A-F]\\.;&5;g; s;%[37BF][0-7]\\.;&6;g; s;%[37BF][89A-F]\\.;&7;g;
45             s;%.[08](\\..);\10;g; s;%.[19](\\..);\11;g; s;%.[2A](\\..);\12;g; s;%.[3B](\\..);\13;g;
46             s;%.[4C](\\..);\14;g; s;%.[5D](\\..);\15;g; s;%.[6E](\\..);\16;g; s;%.[7F](\\..);\17;g;
47            ' 
48   )"
49 }
50
51 LOAD(){
52   # read a file written by STORE and assign all fields to variables
53   # usage: eval "$(LOAD <file)"
54   #
55   # fields will be assigned to shell variables of the same name, but with the following constraints:
56   # * small letters will be converted to capitals
57   # * all non-alphanumeric characters will be converted to underscore (_)
58   # * an underscore will be prepended
59   # as a consequence variable names are guaranteed to match the regex /_[A-Z0-9_]+/
60   #
61   # field values may contain any character, proper escaping for safe use in eval is taken care of
62   # Your shell may or may not purge certain binary characters from variable values during processing
63
64   sed -r 'h; s;^[^:]+:;;
65           s;'\'';'\''\\'\'\'';g;
66           s;^.*$;'\''&'\'';;
67           s;\\n;\n;g;
68           x; s;^([^:]+):.*$;\1;
69           y;abcdefghijklmnopqrstuvwxyz;ABCDEFGHIJKLMNOPQRSTUVWXYZ;
70           s;[^A-Z0-9_];_;g; s;^.*$;_&=;;
71           G; s;^([A-Z0-9_]+=)\n;\1;;
72          '
73 }