?
Solved

Parsing HTML input with perl as cgi, only partial result

Posted on 2003-03-15
2
Medium Priority
?
230 Views
Last Modified: 2009-12-16
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>
0
Comment
Question by:setae
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 20

Accepted Solution

by:
jmcg earned 200 total points
ID: 8145484
It's a buffering problem. Try adding the line

close LOG;

right after the line

print $field{inputdata};

(which could have been written

print LOG $field{inputdata};

without all the selects.
0
 

Author Comment

by:setae
ID: 8145821
blind me..

thanks ;)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question