home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / pub / public_html / samples / survey.txt
Text File  |  2020-01-01  |  4KB  |  151 lines

  1. #!/www/cgi/kermit/wermit +
  2. #
  3. # How to write a CGI script in the Kermit language.
  4. # This production example collects responses to the survey at:
  5. # http://www.columbia.edu/kermit/k95survey.html
  6. #
  7. # Requires C-Kermit 8.0.212 Dev.25 or later:
  8. # http://www.columbia.edu/kermit/ckdaily.html
  9. #
  10. # F. da Cruz, Columbia University, December 2006
  11. # Last update: Fri Dec 15 09:14:32 2006
  12.  
  13. # set flag on # debug
  14. set flag off # no debug
  15.  
  16. .datafile = /www/cgi-upload/kermit/survey.dat
  17.  
  18. # Macro to decode a "URL encoded" variable in which
  19. # '+' represents a space and %xx is a hexadecimal-encoded byte.
  20. #
  21. def decode {
  22.    .\%9 := \freplace(\m(\%1),+,\32)
  23.    .\%8 := \freplace(\fcont(\%9),%,\92x)
  24.    _asg \%1 \%8 
  25. }
  26.  
  27. # Check Kermit version for INPUT /COUNT:
  28. .havecount = 0
  29. if > \v(version) 800212 {
  30.     .havecount = 1
  31. } else if == \v(version) 800212 {
  32.     if def \v(test) {
  33.         if lgt \v(test) Dev.24 .havecount = 1
  34.     }
  35. }
  36.  
  37. # Emit the the HTML header for the response page.
  38. #
  39. echo Content-type: text/html
  40. echo
  41. echo <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  42. echo <html lang="\m(lang)"><head><title>Survey received - Thank you</title>
  43. echo <META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  44. echo <META http-equiv="Content-Style-Type" content="text/css">
  45. echo </head>
  46. echo <body margin:48 style="font-size:14pt">
  47.  
  48. # For debugging only
  49. #
  50. if flag {
  51.     echo <p>
  52.     pwd
  53.     echo <br>
  54.     echo \v(herald)<br>
  55.     echo Length: \$(CONTENT_LENGTH)<br>
  56.     echo Referred by: \$(HTTP_REFERER)<br>
  57.     echo Host: \$(REMOTE_HOST)<br>
  58.     echo Address: \$(REMOTE_ADDR)<br>
  59.     echo <p>
  60. }
  61. # Read the form contents.  If the environment variable CONTENT_LENGTH
  62. # is defined and has a numeric value, this is the length of the form data
  63. # string.  Otherwise (because of the definition of the form itself),
  64. # the data is terminated by "END_OF_MESSAGE".
  65.  
  66. clear input
  67. set input echo off
  68. if ( numeric \$(CONTENT_LENGTH) && defined havecount ) {
  69.     input /count:\$(CONTENT_LENGTH) 10
  70.     if flag echo INPUT STATUS=\v(status)
  71.     .line := \v(input)
  72.     if flag echo INPUT /COUNT:\$(CONTENT_LENGTH) 
  73.     if flag echo INPUT=[\v(input)]=\flen(\v(input))
  74. } else {
  75.     input 10 END_OF_MESSAGE
  76.     .line := \freplace(\m(input),END_OF_MESSAGE,)
  77.     if flag echo INPUT END_OF_MESSAGE
  78. }
  79. # Make sure we got some data
  80. #
  81. if not def line {
  82.     echo "Empty message received<br>"
  83.     forward end
  84. }
  85. # The form data string is a series of 'name=value' pairs separated by
  86. # ampersands or semicolons.  Kermit's split() function separates them
  87. # into an array, \&a[], one name=value pair in each array element:
  88. #
  89. .\%n := \fsplit(\m(line),&a,&;)
  90. if not \%n {
  91.     echo "Name-value string not received<br>"
  92.     forward end
  93. }
  94. if flag {                               # (Debugging)
  95.     echo <pre>
  96.     show array a
  97.     echo </pre>
  98. }
  99. # Open the response log and write the decoded name-value pairs into it:
  100. #
  101. fopen /append \%o \m(datafile)
  102. if fail {
  103.     echo "ERROR: \v(errstring)"
  104.     forward end
  105. }
  106. fwrite /line \%o "--- \v(timestamp) ---"
  107. fwrite /line \%o "# Host = \$(REMOTE_HOST)"
  108. fwrite /line \%o "# Addr = \$(REMOTE_ADDR)"
  109. fwrite /line \%o "# User = \$(REMOTE_USER)"
  110.  
  111. # The keywordval() function parses a name-value pair, assigning the
  112. # value to a macro of the given name.  Then we decode the value.
  113. # Then we write the decoded 'name=value' line into the mail program.
  114. # \v(keywordvalue) contains the name of the variable (macro).
  115. # \m(\v(keywordvalue)) gives its value (definition).
  116.  
  117. for \%i 1 \%n 1 {
  118.     void \fkeywordval(\&a[\%i])
  119.     if equ "\v(lastkeywordvalue)" "END_OF_MESSAGE" continue
  120.     decode \v(lastkeywordvalue)
  121.     if \flen(\m(\v(lastkeywordvalue))) {
  122.         fwrite /line \%o \v(lastkeywordvalue)=\m(\v(lastkeywordvalue))
  123.     }
  124. }
  125. # Close the response log file.
  126. #
  127. fclose \%o
  128. chmod 664 \m(datafile)
  129.  
  130. # Here we print a thank-you note for the user who filled out the form.
  131. # This is done simply by echoing HTML statements and text to our own
  132. # standard output:
  133.  
  134. echo <h2>Thank you</h2>
  135. echo Your response will help in our efforts to gain approval for creating
  136. echo a new Kermit 95 release.
  137. echo <p>
  138. echo Thanks again!
  139. echo <p>
  140. echo Frank da Cruz<br>
  141. echo <a href="mailto:fdc@columbia.edu">fdc@columbia.edu</a><br>
  142. echo \v(timestamp)
  143. echo <hr>
  144.  
  145. :end
  146. # Emit the HTML footer
  147. #
  148. echo "</body>"
  149. echo "</html>"
  150. exit 0
  151.