## cgi.pl: CGI routines for getting data from a form or request
## 
## Adam S. Rosien (asr@geom.umn.edu)
## $Id: cgi.pl,v 1.3 1996/06/13 20:44:20 dpvc Exp $ 


## parse and return data from a form as an associative array, i.e.
## [$data{"name"} = "value"]
sub get_data {
    local($string);

    # get data
    if ($ENV{'REQUEST_METHOD'} eq 'GET') {
        $string = $ENV{'QUERY_STRING'};
    }				
    else { read(STDIN, $string, $ENV{'CONTENT_LENGTH'}); }

    # split data into name=value pairs
    @data = split(/&/, $string);
   
    # split into name=value pairs in associative array
    foreach (@data) {
	@_ = split(/=/, $_);
	$_[0] =~ s/\+/ /g; # plus to space
	$_[0] =~ s/%(..)/pack("c", hex($1))/ge; # hex to alphanumeric
	$data{"$_[0]"} = $_[1];
    }

    # translate special characters
    foreach (keys %data) {
	$data{"$_"} =~ s/\+/ /g; # plus to space
	$data{"$_"} =~ s/%(..)/pack("c", hex($1))/ge; # hex to alphanumeric
    }

    %data;			# return associative array of name=value
}

## remove anything past : or ; (for use with variables passed to maple)
sub maple_strip {
  local ($string) = "@_";
  $string =~ s/[:;].*//;
  return $string;
}


## return valid MIME value; defaults to "text/html"
sub mime_header {
    local($type);

    if (@_) { $type = $_[0]; }
    else { $type = "text/html"; }

    return "Content-type: $type\n\n";
}


## print submitted data in a nice format
## note: must have already run &get_data() to put the data in %data
## (data data data data.  data data.)
sub print_data {
    print "<HR>\nSubmitted data for the script <B>$0<\/B>:\n<PRE>\n";

    foreach (keys %data) { print $_, " = ", $data{"$_"}, "\n"; }

    print "<\/PRE>\n<HR><P>\n";
}

1;				# standard return code for perl "include" file
