return to first page linux journal archive
keywordscontents

Listing 2. Tcl Script for processing data

#!/usr/bin/tclsh
# TCL script to display all user-defined values
# from a form
proc urlDecode {text}
{
   # Replace `+' chars, then hexidecimals
   regsub -all {\+} $text { } text
   regsub -all {%([0-9a-hA-H][0-9a-hA-H])} \
      $text {[format %c 0x\1]} url
   # Send back the result 
   return [subst $url]
}

proc cgiParse {} {
   global cgi env 
   # Find out what type of request it was, and get
   # the data 
   if {[info exists env(QUERY_STRING)]} {
      set text $env(QUERY_STRING)
   } elseif {[info exists env(CONTENT_LENGTH)]} {
      set text [read stdin $env(CONTENT_LENGTH)]
   } else {
      gets stdin text
   } 
   # Decode the data 
   foreach {name value} [split $text &=] {
      set cgi([urlDecode $name]) \
         [urlDecode $value]
   }
}
# Print a header and run 
puts "Content-type: text/html \n\n"
cgiParse
foreach foo [array names cgi] {
   puts "Variable: $foo Value: $cgi($foo) <br>
}