Link to home
Start Free TrialLog in
Avatar of setae
setae

asked on

Parsing HTML input with perl as cgi, only partial result

Because Winrunner's (mercury interactive's gui-testing software) editor isn't too good, and doesn't even show opening and closing brackets, and while working in 5th if-loop, it's way too easy to mess up closing brackets, I tried to make cgi-script to grep out lines containing brackets and counting how many are open.

Even my script was succesful when entering input from commandline, results were not too good when ran from cgi-bin; it doesn't give any output, if amount of inputdata is minimal, and when processing 300 line input, it'll process only ~200 lines of it (I tried commenting unlink function, and $tmpfile contants whole 300 line input).

#!/usr/local/bin/perl

print "Content-type: text/html";
print "\n\n";

# I don't use CGI module to enable running script from commandline also;
# tried also with module with no result

read(STDIN,$temp,$ENV{'CONTENT_LENGTH'});
@pairs=split(/&/,$temp);
foreach $item(@pairs)
{
    ($key,$content)=split(/=/,$item,2);
    $content=~tr/+/ /;
    $content=~s/%(..)/pack("c",hex($1))/ge;
    $field{$key}=$content;
}
# If input comes from webpage, output as html
$runtype = $field{runtype};

# create temporary file using epoch time to make name unique
$systime = time;
$tmpfile = "/tmp/sulkeis_$systime";

if ($runtype eq "fromweb")
{
    open LOG, "> $tmpfile";
    select LOG;
    print $field{inputdata};
    select STDOUT;

    print("<HTML><BODY>\n");

    if ($field{format} == "1")
    {
        print("<table border=\"1\">");
        print("<tr>");
        print("<td>line number<td>");
        print("<td>line content</td>");
        print("<td>brackets open</td>");
        print("</tr><br><br>");
    } else {
        print("<PRE>\n");
    }
    if ($field{startrow})
    {
        $linenum = $field{startrow};
        if ($linenum > 0) { $linenum--; }
    }

} else {

    system("cp $ARGV[0] $tmpfile");
    if ($ARGV[1])
    {
        $linenum = $ARGV[1];
    } else {
        $linenum = 0;
    }
}


open (INFILE, $tmpfile);
$open = 0;

while (<INFILE>)
{
    chomp($_);
    $linenum++;

    if ($_ =~ /.*\}.*\{.*/)
    {
        # most propably row containing '} else {', so no modification to open brackets value
        if ($field{format} == 1)
        {
            print("<tr>");
            print("<td>$linenum</td>");
            print("<td><pre> $_ </pre></td>");
            print("<td>$open</td>");
            print("</tr>\n");
        } else {
            print("$linenum: $_ :$open\n");
        }

    } else {

        if ($_ =~ /.*\}.*/)
        {
            $open--;
            if ($field{format} == 1)
            {
                print ("<tr>");
                print("<td>$linenum</td>");
                print("<td><pre> $_ </pre></td>");
                print("<td>$open</td>");
                print("</tr>\n");
            } else {
                print("$linenum: $_ :$open\n");
            }
        }

        if ($_ =~ /.*\{.*/)
        {
            $open++;
            if ($field{format} == 1)
            {
                print ("<tr>");
                print("<td>$linenum</td>");
                print("<td><pre> $_ </pre></td>");
                print("<td>$open</td>");
                print("</tr>\n");
            } else {
                print("$linenum: $_ :$open\n");
            }
        }
    }
}
close (INFILE);

# delete temporarly file
unlink($tmpfile);

if ($runtype eq "fromweb")
{
    if ($field{format} == 0)
    {
        print("<PRE>\n");
    } else {
        print("</TABLE>");
    }

    print "</BODY></HTML>";
}


-- html input form --

<html>
<body>
<form action="/cgi-bin/sulkeiset.pl" method="post">
<table width="80%"><tr>
<td>Begin line<input name="startrow"></td>
<td>Show using format
<select name="format">
<option value="1" selected> HTML </option>
<option value="0"> ASCII </option>

</select></tr></table>
<textarea name="inputdata" rows="20" cols="90">
</textarea><br>
<input name="runtype" type="hidden" value="fromweb">
<input type="submit" value="show">
</form>

</body>
</html>
ASKER CERTIFIED SOLUTION
Avatar of jmcg
jmcg
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of setae
setae

ASKER

blind me..

thanks ;)