#!/bin/ksh ################################################################ #### #### Korn Shell function to parse CGI form variables and #### values. Processes a standard input stream and provides #### as output the processed stream, one variable/value pair #### per line. Also evaluates the variable/value pair so #### that subsequent commands can reference the CGI specified #### variables. #### #### This function assumes "bc" and "awk" commands can be #### located via the PATH environment variable. #### #### Load this file into your current environment as follows: #### #### . ./cgiparse.sh #### #### Thats "dot-space-dot-slash-cgiparse-dot-sh" #### #### You will then be able to issue the command "cgiparse" #### from your current environment to retrieve one character. #### #### SYNTAX: cgiparse [-v] < STDIN #### OR #### echo "UNPARSED CGI STRING" | cgiparse #### #### -v Verbose mode. Echoes each variable/value pair to STDOUT #### #### #### AUTHOR: Dana French #### EMAIL: dfrench@xoommail.com #### ################################################################ cgiparse() { VFLAG="0" [[ "_${1}" = "_-v" ]] && VFLAG="1" CGI_TMP1=`sed -e "s/\&/ /g;s/%\(..\)/\\\`print \\\'ibase=16\\\; \1\\\' \\\| bc \\\| awk \\\'\\\{printf\\\(\\\\\"%c\\\\\",\\\$1\\\)\\\}\\\'\\\`/g"` if [[ "_${CGI_TMP1}" != "_" ]] then for i in `eval echo "\"${CGI_TMP1}\""` do CGI_TMP2=`print "${i}" | sed -e "s/\"/\'/g;s/,/ /g;s/+/ /g"` CGI_VAR="${CGI_TMP2%%=*}" CGI_VAL="${CGI_TMP2#*=}" eval ${CGI_VAR}="\"\${CGI_VAL}\"" [[ ${VFLAG} -eq 1 ]] && print "${CGI_VAR}=\"${CGI_VAL}\"" done fi } ################################################################ # # uncomment the following command to test the cgiparse function # print 'EXCLAMATION=%21&POUND=%23&DOLLAR=%24&PERCENT=%25&ERSAND=%26&SINGLE=%27&OPEN_PAREN=%28&CLOSE_PAREN=%29&ATSIGN=%40&GRAVE=%60&EOD=' | cgiparse -v