#! /usr/bin/perl -Tw

#++
# Modified version of 
#  Figure 10.7 (Test program /form1/ accessed by the HTML form in Figure 9.9)
#  from p. 395 of _XHTML 1.0 Web Development Sourcebook_
#  by Ian S. Graham, (c) by the author
#  published by John Wiley & Sons, Inc.
#
# Original code downloaded from 
#  <URL:http://www.utoronto.ca/ian/books/xhtml2/chap10/form1.txt>
#--

use CGI::Carp 'fatalsToBrowser';
use diagnostics; # verbose error messages
use strict;      # stops us from making stupid mistakes
$| = 1;          # flush output buffer after each output statement ...
                 # ... slower but helpful for output over the network
$\ = "\n";       # append "\n" to all print statements

# 1. Header data
print "Status: 200 Okey-dokey\r";
print "content-TYPE: text/html\r";
print "\r";

# Initialize variables
my $num = $#ARGV+1;  # Number of command line arguments
my $i = 0;
my $key = 0;
my $val = 0;

# Start document
print <<EOF;
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 <head>
  <title>
    Form Processing Example for Dal CS 3172:
    Web-Centric Computing
  </title>
  <link rev="Made"    href="mailto:jamie\@cs.dal.ca" />
  <link rel="Copyright"  
        href="http://www.csd.uwo.ca/%7ejamie/Copyright.html" />
  <meta name="Copyright"
        content="This document is (c) copyright by J. Blustein.
         J. Blustein has asserted the right to be recognized
         as the author of this document." />
  <link rel="stylesheet"
        href="http://www.cs.dal.ca/~jamie/course/CS/3172/CS3172.css"
        type="text/css" />
  <style type="text/css">
    tr.spec td {background-color:rgb(255,255,153); color:black} 
    table      {background-color:white;            color:black;
                border:medium solid black; border-collapse:collapse} 
    tr         {border:thin black solid}
    td         {vertical-align:top}
    colgroup   {border:thin black solid}
  </style>
 </head>
 <body>
  <p class="topaut">
   <a title="professor's homepage"
      href="/~jamie/"
      ><abbr>J.</abbr>&nbsp;Blustein</a>
  </p>
  <h1>
   <a href="http://www.cs.dal.ca/~jamie/course/CS/3171/README.shtml"
      name="top"
      title="course homepage (Dalhousie CS 3172: Web-Centric Computing)"
      >Web-centric Computing</a>
  </h1>
   <p class="mini-menu">
     [<a href="/~jamie/course/CS/3172/Course/README.shtml" accesskey="C"
         title="administrative files for the course"
         >Course</a>&nbsp;| 
      <a href="/~jamie/course/CS/3172/Announcements/README.shtml" accesskey="A"
         title="announcements section of the course website"
         >Announcements</a>&nbsp;|
      <a href="/~jamie/course/CS/3172/Materials/README.shtml" accesskey="M"
         title="course materials (supplied by professor)"
         >Materials</abbr></a>&nbsp;|
      <a href="/~jamie/course/CS/3172/Resources/README.shtml" accesskey="R"
         title="resources section of the website"
         >Resources</a>]
  </p>

  <h2><a href="/~jamie/course/CS/3172/examples/list.html"
         >Example Code</a></h2>

  <h3>Information Available With Form Processing</h3>
   <ol>
    <li><a href="#params"
           >Command line arguments</a></li>
    <li><a href="#env"
           >Environment variables</a></li>
    <li><a href="#stdin"
           >Data at standard input</a></li>
   </ol>

   <ol>
EOF
;

print "  <li>";
print "  <h3>";
print "    <a name='params'>Number of Command Line Arguments = $num</a>";
print "  </h3>";

if ($num > 0) {
  print "   <p>Command Line Arguments are: <br />";
  my $i;
  for($i=0; $i<=$#ARGV; $i++) {
     print "    Arg $i = $ARGV[$i] <br /> ";
  }
  print "   </p>";
}
else {
  print "   <p><em>There are no command line arguments.</em>";
}
print "   </p>\n";

print "  </li>";
print "  <li>";
print '  <h3><a name="env">Environment Variables are:</a></h3>';
print "   <table border='1'>";
print "    <caption>Environment Variables</caption>";
print "    <colgroup><col /></colgroup><colgroup><col /></colgroup>";
print "    <tr>";
print "      <th>Name</th>";
print "      <th>Value</th>";
print "    </tr>";
  my $env_var;
  my $class;
  foreach $env_var (keys %ENV){
      $class = ("QUERY_STRING"   eq $env_var ||
                "CONTENT_TYPE"   eq $env_var ||
                "REQUEST_METHOD" eq $env_var)
	       ?" class='spec'" :"";
      print "    <tr$class>";
      print "      <td><code>$env_var</code></td>";
      print "      <td>$ENV{$env_var}</td>";
      print "    </tr>";
  }
print "   </table>\n";

print "  </li>";
print "  <li>";
print "  <h3><a name=\"stdin\">3. Data at Standard Input</a></h3>";
print "   <p>";
if ($ENV{"CONTENT_LENGTH"} ne "" ) {
   print "    <em>Content-length is ", $ENV{"CONTENT_LENGTH"}, ".";
   print "    <br />Content is:</em>";
   $\="";
   print "    <pre style='border:thin dotted rgb(153,51,51)'>";
   while( <STDIN> ) {
       $_ =~ s/&/&amp;/;
       $_ =~ s/</&lt;/;
       $_ =~ s/>/&gt;/;
       print;
   }
   $\="\n";
   print "</pre>";
}
else {
   print "   <em>There is no data at standard input.</em>";
}
print "   </p>";
print "  </li>";
print "  </ol>";

print << 'EOHTML'

  <p>
   [ Back to <a href="/~jamie/course/CS/3172/examples/list.html#upload2.cgi"
                >file upload</a> | <a
                href="/~jamie/course/CS/3172/examples/list.html#upload2.cgi#3form"
                >three-penny form</a> example]
  </p>

  <hr title="end of document proper" />

  <h6 title="document identification information">
   <kbd>http://www.cs.dal.ca/~jamie/cgi-bin/3172/form/upload2.cgi</kbd>
  </h6>
   <dl style="display:compact; float:left">
     <dt>Version:</dt> <dd>30 May 2003</dd>
     <dt><abbr title="professor" >Prof.</abbr>:</dt>
     <dd><a title="professor's homepage"
            href="/%7ejamie/"
            ><abbr>J.</abbr>&nbsp;Blustein</a>
         <kbd>&lt;jamie@cs.dal.ca&gt;</kbd></dd>
   </dl>
   <dl style="float:right">
    <dt><a href="http://validator.w3.org/check/referer"
           title="link requests HTML validator at W3C to check this file for adherence to standard"
           ><img
             style="border:0;width:88px;height:31px"
             src="/~jamie/image/vxhtml10.gif"
             alt="This webpage uses valid XHTML 1.0" /></a></dt>
   </dl>
 </body>
</html>
EOHTML
;
# EOF #
