#!/www/cgi/kermit/wermit + # # How to write a CGI script in the Kermit language. # This production example collects responses to the survey at: # http://www.columbia.edu/kermit/k95survey.html # # Requires C-Kermit 8.0.212 Dev.25 or later: # http://www.columbia.edu/kermit/ckdaily.html # # F. da Cruz, Columbia University, December 2006 # Last update: Fri Dec 15 09:14:32 2006 # set flag on # debug set flag off # no debug .datafile = /www/cgi-upload/kermit/survey.dat # Macro to decode a "URL encoded" variable in which # '+' represents a space and %xx is a hexadecimal-encoded byte. # def decode { .\%9 := \freplace(\m(\%1),+,\32) .\%8 := \freplace(\fcont(\%9),%,\92x) _asg \%1 \%8 } # Check Kermit version for INPUT /COUNT: .havecount = 0 if > \v(version) 800212 { .havecount = 1 } else if == \v(version) 800212 { if def \v(test) { if lgt \v(test) Dev.24 .havecount = 1 } } # Emit the the HTML header for the response page. # echo Content-type: text/html echo echo echo
pwd
echo
echo \v(herald)
echo Length: \$(CONTENT_LENGTH)
echo Referred by: \$(HTTP_REFERER)
echo Host: \$(REMOTE_HOST)
echo Address: \$(REMOTE_ADDR)
echo
}
# Read the form contents. If the environment variable CONTENT_LENGTH
# is defined and has a numeric value, this is the length of the form data
# string. Otherwise (because of the definition of the form itself),
# the data is terminated by "END_OF_MESSAGE".
clear input
set input echo off
if ( numeric \$(CONTENT_LENGTH) && defined havecount ) {
input /count:\$(CONTENT_LENGTH) 10
if flag echo INPUT STATUS=\v(status)
.line := \v(input)
if flag echo INPUT /COUNT:\$(CONTENT_LENGTH)
if flag echo INPUT=[\v(input)]=\flen(\v(input))
} else {
input 10 END_OF_MESSAGE
.line := \freplace(\m(input),END_OF_MESSAGE,)
if flag echo INPUT END_OF_MESSAGE
}
# Make sure we got some data
#
if not def line {
echo "Empty message received
"
forward end
}
# The form data string is a series of 'name=value' pairs separated by
# ampersands or semicolons. Kermit's split() function separates them
# into an array, \&a[], one name=value pair in each array element:
#
.\%n := \fsplit(\m(line),&a,&;)
if not \%n {
echo "Name-value string not received
"
forward end
}
if flag { # (Debugging)
echo
show array a echo} # Open the response log and write the decoded name-value pairs into it: # fopen /append \%o \m(datafile) if fail { echo "ERROR: \v(errstring)" forward end } fwrite /line \%o "--- \v(timestamp) ---" fwrite /line \%o "# Host = \$(REMOTE_HOST)" fwrite /line \%o "# Addr = \$(REMOTE_ADDR)" fwrite /line \%o "# User = \$(REMOTE_USER)" # The keywordval() function parses a name-value pair, assigning the # value to a macro of the given name. Then we decode the value. # Then we write the decoded 'name=value' line into the mail program. # \v(keywordvalue) contains the name of the variable (macro). # \m(\v(keywordvalue)) gives its value (definition). for \%i 1 \%n 1 { void \fkeywordval(\&a[\%i]) if equ "\v(lastkeywordvalue)" "END_OF_MESSAGE" continue decode \v(lastkeywordvalue) if \flen(\m(\v(lastkeywordvalue))) { fwrite /line \%o \v(lastkeywordvalue)=\m(\v(lastkeywordvalue)) } } # Close the response log file. # fclose \%o chmod 664 \m(datafile) # Here we print a thank-you note for the user who filled out the form. # This is done simply by echoing HTML statements and text to our own # standard output: echo
echo Thanks again! echo
echo Frank da Cruz
echo fdc@columbia.edu
echo \v(timestamp)
echo