From f53f3103ded9696b19666f32620642172ffa9c09 Mon Sep 17 00:00:00 2001 From: paul Date: Thu, 22 Mar 2018 15:22:54 +0000 Subject: [PATCH 1/1] file handling functions svn path=/trunk/; revision=63 --- files.sh | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100755 files.sh diff --git a/files.sh b/files.sh new file mode 100755 index 0000000..6c459c5 --- /dev/null +++ b/files.sh @@ -0,0 +1,67 @@ +#!/bin/sh + +# Copyright 2018 Paul Hänsch +# +# This is a file format helper, part of CGIlite. +# +# CGIlite is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# CGIlite is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with CGIlite. If not, see . + +# ksh and zsh workaround +case "${0##*/}" in + zsh) setopt -o OCTAL_ZEROES 2>&- ;; # zsh, mostly ignored by other shells + ksh) set -o posix ;; # ksh, will crash most other shells +esac + +STORE(){ + # Store fields from a HTTP GET or POST string in a file + # usage: STORE "${QUERY_STRING}" + # or: STORE "$(head -n $HTTP_CONTENT_LENGTH)" + printf "$( + printf '&%s' "$@" \ + | sed -r ':X; $bY; N; bX; :Y; + s;\+; ;g; s;(\\|5[Cc]);\\\\\\\\;g; s;(\n|%0[Aa]);\\\\n;g; s;(^|&)([^=]+)=;\1\2:;g; s;&;\n;g; + s;^\n+;;; s;$;\\n;; s;\n+;\n;g; + # Hexadecimal { %00 - %FF } will be transformed to octal { \000 - \377 } for posix printf + s;%[0123].;&\\0;g; s;%[4567].;&\\1;g; s;%[89AB].;&\\2;g; s;%[CDEF].;&\\3;g; + s;%[048C][0-7]\\.;&0;g; s;%[048C][89A-F]\\.;&1;g; s;%[159D][0-7]\\.;&2;g; s;%[159D][89A-F]\\.;&3;g; + s;%[26AE][0-7]\\.;&4;g; s;%[26AE][89A-F]\\.;&5;g; s;%[37BF][0-7]\\.;&6;g; s;%[37BF][89A-F]\\.;&7;g; + s;%.[08](\\..);\10;g; s;%.[19](\\..);\11;g; s;%.[2A](\\..);\12;g; s;%.[3B](\\..);\13;g; + s;%.[4C](\\..);\14;g; s;%.[5D](\\..);\15;g; s;%.[6E](\\..);\16;g; s;%.[7F](\\..);\17;g; + ' + )" +} + +LOAD(){ + # read a file written by STORE and assign all fields to variables + # usage: eval "$(LOAD