# Useful routines for writing CGI scripts (and general puttering about) # '&find_form_args' takes as (first) argument the expected field names # separated by '|', as in: # &find_form_args("name|address|phone|wants_pepperoni"); # On return, the variables whose names are those of the form fields are # set to the appropriate values (unescaped, if necessary, so that for # type-in fields what you get is what the user typed). These would be # $name, $address, $phone, and $wants_pepperoni in the above example. # Second argument, if any, is the name of an associative array, indexed # by field-name, which gets the values supplied for unexpected fields (i.e., # those with names not listed in the first argument). If no second # argument is supplied, and an unexpected field is found, we &cgidie; # this is to help catch misspellings, and also may help with security # (so that a malicious client can't cause arbitrary variables to be set, # with potentially disruptive effects). # Return value is true if we clearly got arguments, false if the # caller looked as if he was looking for a coversheet. This is to # facilitate such stuff as: # do { print < 0) || (! $is_get); } # Error handling ... the well-tempered CGI script doesn't croak without # making sure that someone has a chance to see its dying gasp; that means # giving it a proper Content-type line (and hoping it doesn't get too # screwed up). # The first argument is an error message, which gets potential HTML syntax # ('<', '>', '&') escaped out so that it is seen by the user verbatim. The # second argument is optional; if present, it is treated as HTML (i.e., # left as is), for scripts which want to die a particularly elaborate # death. # Note that if the script has already produced output before calling this, # things will be a little garbled all around, but still somewhat better than # nothing. sub cgidie { local ($header, $body) = @_; $header = &HTMLize ($header); if ($body eq '') { $body = $header; $header = '' } if ($header eq '') { $header = "Error in processing your request"; } print < $header

$header

$body EOF exit(1); } # Self-referencing URL generation... the value of this function is, loosely # speaking, the URL of the directory which the script resides in ... more # precisely, it is the script's own URL (sans any PATH_INFO), with the name # of the script itself, and the preceding '/', stripped off. sub my_dir_url { local ($sname) = $ENV{"SERVER_NAME"}; local ($sport) = $ENV{"SERVER_PORT"}; local ($hroot) = &dirstring($ENV{"SCRIPT_NAME"}); "http://$sname:$sport$hroot" } # Useful string conversions... # '&unescape' translates the %.. escapes used in URLs back into what the user # originally typed. # '&HTMLize' translates a random ASCII string into HTML by HTML-escaping # stuff that might be mistaken for HTML syntax. # '&capitalize' does the obvious. sub unescape { local ($_) = @_; s/%(..)/pack ('H2', $1)/eg; $_ } sub HTMLize { local ($_) = @_; s/\&/&/g; s/\>/>/g; s/\$file") || &cgidie ("Couldn't create $file"); ( (print dummy $contents) || &cgidie ("Couldn't write to $file")) unless ($contents eq ''); close dummy; } sub copy_file { local ($dest, $source) = @_; local (*dst, *src); open (dst, ">$dest") || &cgidie("Couldn't write to $dest"); open (src, "$source") || &cgidie("Couldn't read from $source"); while () { (print dst) || &cgidie ("Couldn't write to $dest"); } close (dst); close (src); } sub print_file { local ($name) = @_; local (*file); local ($hname) = &HTMLize($name); if (! open (file, $name)) { print "Couldn't open $hname!!!"; return } while () { print } close (file) } sub read_file { local ($name) = @_; local (*file, $it); local ($hname) = &HTMLize($name); if (! open (file, $name)) { return "Couldn't open $hname!!!"; } $it = ''; while () { $it .= $_ } close (file); $it }