From: paul Date: Fri, 10 Jul 2015 13:48:18 +0000 (+0000) Subject: replace echo by the more unambiguous builtin printf X-Git-Url: https://git.plutz.net/?a=commitdiff_plain;h=3a43bceff672dae6d391a53a1a12943c207aeff1;p=cgilite replace echo by the more unambiguous builtin printf svn path=/trunk/; revision=7 --- diff --git a/action.sh b/action.sh index 1f44ca1..f8faf1f 100755 --- a/action.sh +++ b/action.sh @@ -17,7 +17,7 @@ # You should have received a copy of the GNU Affero General Public License # along with Serve0. If not, see . -ACTION="$(echo "$_GET[\"action\"]" |egrep '^[a-zA-Z0-9_-]+$')" +ACTION="$(printf %s "$_GET[\"action\"]" |egrep '^[a-zA-Z0-9_-]+$')" ACTION="${_EXEC}/actions/${ACTION}.sh" if [ -x "$ACTION" ]; then @@ -25,6 +25,6 @@ if [ -x "$ACTION" ]; then . $ACTION else debug "unable to execute $ACTION" - echo -n "Location: ?p=error\n\n" + printf "Location: ?p=error\n\n" fi diff --git a/cgi.sh b/cgi.sh index 37f4787..9994219 100755 --- a/cgi.sh +++ b/cgi.sh @@ -25,10 +25,10 @@ declare -A _REF cgi_get() { # parse HTTP GET string debug "== CGI DATA: GET ==" - echo "$QUERY_STRING" |tr '&' '\n' |while read query; do - key="$(echo -E "$query" |sed -r 's:^([a-zA-Z0-9_-]*)=(.*)$:\1:')" - val="$(echo -E "$query" |sed -r 's:^([a-zA-Z0-9_-]*)=(.*)$:\2:')" - _GET["$key"]="$(echo -e "$(echo -E "$val" |sed 's:+: :g;s:\\:\\\\:g;s:%:\\x:g')")" + printf '%s\n' "$QUERY_STRING" |tr '&' '\n' |while read query; do + key="$(printf %s "$query" |sed -r 's:^([a-zA-Z0-9_-]*)=(.*)$:\1:')" + val="$(printf %s "$query" |sed -r 's:^([a-zA-Z0-9_-]*)=(.*)$:\2:')" + _GET["$key"]="$(printf "$(printf %s "$val" |sed 's:+: :g;s:\\:\\\\:g;s:%:\\x:g')")" debug "_GET[$key] => $val" done } @@ -36,9 +36,9 @@ cgi_get() { # parse HTTP GET string cgi_post() { # parse HTTP POST string debug "== CGI DATA: POST ==" sed -u 1q |tr '&' '\n' |while read query; do - key="$(echo -E "$query" |sed -r 's:^([a-zA-Z0-9_-]*)=(.*)$:\1:')" - val="$(echo -E "$query" |sed -r 's:^([a-zA-Z0-9_-]*)=(.*)$:\2:')" - value="$(echo -e "$(echo -E "$val" |sed 's:+: :g;s:\\:\\\\:g;s:%:\\x:g;')")" + key="$(printf %s "$query" |sed -r 's:^([a-zA-Z0-9_-]*)=(.*)$:\1:')" + val="$(printf %s "$query" |sed -r 's:^([a-zA-Z0-9_-]*)=(.*)$:\2:')" + value="$(printf "$(printf %s "$val" |sed 's:+: :g;s:\\:\\\\:g;s:%:\\x:g;')")" if [ -n "$_POST[\"$key\"]" ]; then n=0 while [ -n "$_POST[\"$key$n\"]" ]; do n=$(($n + 1)); done @@ -52,14 +52,14 @@ cgi_post() { # parse HTTP POST string cgi_refdata() { # Parse GET data from referer debug "== CGI DATA: REFERER ==" - echo "$HTTP_REFERER" |cut -d'?' -f2- |tr '&' '\n' |while read query; do - key="$(echo -E "$query" |sed -r 's:^([a-zA-Z0-9_-]*)=(.*)$:\1:')" - val="$(echo -E "$query" |sed -r 's:^([a-zA-Z0-9_-]*)=(.*)$:\2:')" - _REF["$key"]="$(echo -e "$(echo -E "$val" |sed 's:+: :g;s:\\:\\\\:g;s:%:\\x:g')")" + printf '%s\n' "$HTTP_REFERER" |cut -d'?' -f2- |tr '&' '\n' |while read query; do + key="$(printf %s "$query" |sed -r 's:^([a-zA-Z0-9_-]*)=(.*)$:\1:')" + val="$(printf %s "$query" |sed -r 's:^([a-zA-Z0-9_-]*)=(.*)$:\2:')" + _REF["$key"]="$(printf "$(printf %s "$val" |sed 's:+: :g;s:\\:\\\\:g;s:%:\\x:g')")" debug "_REF[$key] => $val" done } urlsave(){ - echo -E "$*" |sed 's:%:\%25:g;s:\?:\%3F:g;s:&:\%26:g;s:'\'':\%27:g;s: :\%20:g;s:!:\%21:g;s:(:\%28:g;s:):\%29:g;s:":\%22:g;' + printf %s "$*" |sed 's:%:\%25:g;s:\?:\%3F:g;s:&:\%26:g;s:'\'':\%27:g;s: :\%20:g;s:!:\%21:g;s:(:\%28:g;s:):\%29:g;s:":\%22:g;' } diff --git a/index.cgi b/index.cgi index fada33d..af99fe1 100755 --- a/index.cgi +++ b/index.cgi @@ -36,7 +36,7 @@ _EXEC="${real%/shcgi/index.cgi}" #execution directory # basic functions debug() { #change to false to disable debugging if [ "$DBG_ENABLED" = true -a -n "$*" ]; then - echo -E "$@" >>"$DBG_FILE" + printf '%s\n' "$*" >>"$DBG_FILE" elif [ "$DBG_ENABLED" = true -a -z "$*" ]; then tee -a "$DBG_FILE" elif [ -z "$*" ]; then @@ -45,8 +45,8 @@ debug() { #change to false to disable debugging } die() { - debug "$@" - echo -E "$@" >>/dev/stderr + debug FATAL: "$*" + printf '%s\n' "$*" >&2 exit 1 } diff --git a/page.sh b/page.sh index 96f3fcc..d4e38ce 100755 --- a/page.sh +++ b/page.sh @@ -17,17 +17,17 @@ # You should have received a copy of the GNU Affero General Public License # along with shcgi. If not, see . -echo -n "Content-Type: text/html;charset=utf-8\n\n" +printf "Content-Type: text/html;charset=utf-8\n\n" -PAGE="$(echo -E "$_GET[\"p\"]" |egrep '^[a-zA-Z0-9_-]+$')" +PAGE="$(printf %s "$_GET[\"p\"]" |egrep '^[a-zA-Z0-9_-]+$')" PAGE="${_EXEC}/pages/${PAGE}.sh" [ -x "$PAGE" ] || PAGE="${_EXEC}/pages/error.sh" NAVIGATION() { for each in "${_EXEC}"/pages/*.sh; do - link="$(echo -E "$each" |sed -r "s:^.*/([^/]*)\.sh$:\1:")" + link="$(printf %s "$each" |sed -r "s:^.*/([^/]*)\.sh$:\1:")" title="$($each title)" - [ -n "$title" ] && echo -E "?p=$link $title" + [ -n "$title" ] && printf '%s\n' "?p=$link $title" done }