#!/usr/bin/ksh93
################################################################
####
#### Program: cgiparse
####
#### Description: Korn Shell 93 function to parse CGI variables
#### containing encoded HEX characters. Initializes CGI
#### variables with their assigned values into the current
#### shell environment. If no CGI variable is passed, then
#### the parsed CGI string is sent to standard output.
####
#### Description: Version 0.5
####
#### Author: Dana French (dfrench@mtxia.com)
#### 615.556.0456
####
#### Date: 20061024
#### Last Modified: 20120920
####
#### Usage:
####
#### IFS="&" PARSED=( ${QUERY_STRING} )
#### for VAR in "${PARSED[@]}"
#### do
#### print "${VAR}" | cgiparse_k93
#### done
####
################################################################
function cgiparse_k93
{
typeset CGILINE
typeset CGITMP
typeset CGIVAR
typeset CGIVAL
typeset CGIIFS="${IFS}"
IFS=''
while read -r -- CGILINE
do
CGITMP="${CGILINE//+/ }"
# print -- "\n1 CGITMP=${CGITMP}
\n"
CGITMP="${CGITMP//\045([4-9][A-Za-z0-9])/\$( printf "'\\\0%o'" 0x\1 )}"
# print -- "\n2 CGITMP=${CGITMP}
\n"
CGITMP="${CGITMP//\045([0-3][A-Za-z0-9])/\$( printf "'\\\00%o'" 0x\1 )}"
# print -- "\n3 CGITMP=${CGITMP}
\n"
CGITMP="${CGITMP//\045(0[0-7])/\$( printf "'\\\000%o'" 0x\1 )}"
# print -- "\n4 CGITMP=${CGITMP}
\n"
CGIVAR="${CGITMP%%=*}"
# print -- "\n5 CGIVAR=${CGIVAR}
\n"
CGIVAL="${CGITMP#*=}"
# print -- "\n6 CGIVAL=${CGIVAL}
\n"
CGITMP="${CGIVAR}=\"\\\"${CGIVAL}\\\"\""
# print -- "\n7 CGITMP=${CGITMP}
\n"
[[ "${CGITMP}" == *=* ]] &&
eval $( eval print -- "${CGITMP}" ) ||
eval print -- "${CGITMP}"
done
IFS="${CGIIFS}"
}
################################################################
QUERY_STRING="mtxia_server_home=http%3A%2F%2Ftestcase.mtxia.com&partname=4555301512&revision=all¤t_create_dir=jream-1708198-imagelist&edm_login=dummy_id&edm_passwd=dummy_pw&action2execute=view&adcn=&advlesamend=&advlesftamend=&advmlesamend=&advmlesftamend=&advvariation=&alertnotice=alert+notice%3A&apl=automated+parts+list%3A&attvariation=&dcn=dcn+and+adcn%3A&doc=description+of+change%3A&drawing=drawing%3A&ecr=&edr=&edrn=edrn%3A&eo=eo%3A&epn=&extr=&icr=&ladd=ladd%3A&les=&lesamend=&lesft=&lesftamend=&mles=&mlesamend=&mlesft=&mlesftamend=&mdl=&misc=&modsum=&nieo=nieo%3A&pages=pages%2C+book+form%3A&report=&specctrldwg=&srcctrldwg=&standards=&stoporder=stop+order%3A&unitdwg=&variation=variation%3A&vendor=&format=CGM&gzpd=GZ&fo=x11&scope=&oneclk=on&openthis_drawing=CGM%3A+A+4555301512+SHT1+CGM+GZ&selected_doc=CGM%3A+A+4555301512+SHT1"
# separate each variable/value pair into an indexed array
IFS="&"
PARSED=( ${QUERY_STRING} )
IFS=$' \t\n'
# get a list of variable names in the current shell environment
set | cut -d"=" -f1 | sort | uniq > /tmp/tmp1.out
# parse the CGI variables from the indexed array containing the
# variable/value pairs
for VAR in "${PARSED[@]}"
do
print -r -- ${VAR} | cgiparse_k93
done
# get a list of variable names in the current shell environment after
# parsing the CGI variables
set | cut -d"=" -f1 | egrep -v '^VAR$' | sort | uniq > /tmp/tmp2.out
# display the CGI variables and values
for i in $( comm -13 /tmp/tmp1.out /tmp/tmp2.out )
do
eval print -r -- "\"${i}=\\\"\${${i}}\\\"\""
done
# Remove the temporary files created to contain the shell variable names
rm -f /tmp/tmp1.out
rm -f /tmp/tmp2.out