| 1 |
#! /usr/bin/perl
|
| 2 |
|
| 3 |
# Program for testing regular expressions with perl to check that PCRE handles
|
| 4 |
# them the same.
|
| 5 |
|
| 6 |
|
| 7 |
# Function for turning a string into a string of printing chars
|
| 8 |
|
| 9 |
sub pchars {
|
| 10 |
my($t) = "";
|
| 11 |
|
| 12 |
foreach $c (split(//, @_[0]))
|
| 13 |
{
|
| 14 |
if (ord $c >= 32 && ord $c < 127) { $t .= $c; }
|
| 15 |
else { $t .= sprintf("\\x%02x", ord $c); }
|
| 16 |
}
|
| 17 |
$t;
|
| 18 |
}
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
# Read lines from named file or stdin and write to named file or stdout; lines
|
| 23 |
# consist of a regular expression, in delimiters and optionally followed by
|
| 24 |
# options, followed by a set of test data, terminated by an empty line.
|
| 25 |
|
| 26 |
# Sort out the input and output files
|
| 27 |
|
| 28 |
if (@ARGV > 0)
|
| 29 |
{
|
| 30 |
open(INFILE, "<$ARGV[0]") || die "Failed to open $ARGV[0]\n";
|
| 31 |
$infile = "INFILE";
|
| 32 |
}
|
| 33 |
else { $infile = "STDIN"; }
|
| 34 |
|
| 35 |
if (@ARGV > 1)
|
| 36 |
{
|
| 37 |
open(OUTFILE, ">$ARGV[1]") || die "Failed to open $ARGV[1]\n";
|
| 38 |
$outfile = "OUTFILE";
|
| 39 |
}
|
| 40 |
else { $outfile = "STDOUT"; }
|
| 41 |
|
| 42 |
printf($outfile "Perl Regular Expressions\n\n");
|
| 43 |
|
| 44 |
# Main loop
|
| 45 |
|
| 46 |
NEXT_RE:
|
| 47 |
for (;;)
|
| 48 |
{
|
| 49 |
printf " re> " if $infile eq "STDIN";
|
| 50 |
last if ! ($_ = <$infile>);
|
| 51 |
printf $outfile "$_" if $infile ne "STDIN";
|
| 52 |
next if ($_ eq "");
|
| 53 |
|
| 54 |
$pattern = $_;
|
| 55 |
|
| 56 |
$delimiter = substr($_, 0, 1);
|
| 57 |
while ($pattern !~ /^\s*(.).*\1/s)
|
| 58 |
{
|
| 59 |
printf " > " if $infile eq "STDIN";
|
| 60 |
last if ! ($_ = <$infile>);
|
| 61 |
printf $outfile "$_" if $infile ne "STDIN";
|
| 62 |
$pattern .= $_;
|
| 63 |
}
|
| 64 |
|
| 65 |
chomp($pattern);
|
| 66 |
$pattern =~ s/\s+$//;
|
| 67 |
|
| 68 |
# Check that the pattern is valid
|
| 69 |
|
| 70 |
eval "\$_ =~ ${pattern}";
|
| 71 |
if ($@)
|
| 72 |
{
|
| 73 |
printf $outfile "Error: $@\n";
|
| 74 |
next NEXT_RE;
|
| 75 |
}
|
| 76 |
|
| 77 |
# Read data lines and test them
|
| 78 |
|
| 79 |
for (;;)
|
| 80 |
{
|
| 81 |
printf "data> " if $infile eq "STDIN";
|
| 82 |
last NEXT_RE if ! ($_ = <$infile>);
|
| 83 |
chomp;
|
| 84 |
printf $outfile "$_\n" if $infile ne "STDIN";
|
| 85 |
|
| 86 |
s/\s+$//;
|
| 87 |
s/^\s+//;
|
| 88 |
|
| 89 |
last if ($_ eq "");
|
| 90 |
|
| 91 |
$_ = eval "\"$_\""; # To get escapes processed
|
| 92 |
|
| 93 |
$ok = 0;
|
| 94 |
eval "if (\$_ =~ ${pattern}) {" .
|
| 95 |
"\$z = \$&;" .
|
| 96 |
"\$a = \$1;" .
|
| 97 |
"\$b = \$2;" .
|
| 98 |
"\$c = \$3;" .
|
| 99 |
"\$d = \$4;" .
|
| 100 |
"\$e = \$5;" .
|
| 101 |
"\$f = \$6;" .
|
| 102 |
"\$g = \$7;" .
|
| 103 |
"\$h = \$8;" .
|
| 104 |
"\$i = \$9;" .
|
| 105 |
"\$j = \$10;" .
|
| 106 |
"\$k = \$11;" .
|
| 107 |
"\$l = \$12;" .
|
| 108 |
"\$m = \$13;" .
|
| 109 |
"\$n = \$14;" .
|
| 110 |
"\$o = \$15;" .
|
| 111 |
"\$p = \$16;" .
|
| 112 |
"\$ok = 1; }";
|
| 113 |
|
| 114 |
if ($@)
|
| 115 |
{
|
| 116 |
printf $outfile "Error: $@\n";
|
| 117 |
next NEXT_RE;
|
| 118 |
}
|
| 119 |
elsif (!$ok)
|
| 120 |
{
|
| 121 |
printf $outfile "No match\n";
|
| 122 |
}
|
| 123 |
else
|
| 124 |
{
|
| 125 |
@subs = ($z,$a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p);
|
| 126 |
$last_printed = 0;
|
| 127 |
for ($i = 0; $i <= 17; $i++)
|
| 128 |
{
|
| 129 |
if ($i == 0 || defined $subs[$i])
|
| 130 |
{
|
| 131 |
while ($last_printed++ < $i-1)
|
| 132 |
{ printf $outfile ("%2d: <unset>\n", $last_printed); }
|
| 133 |
printf $outfile ("%2d: %s\n", $i, &pchars($subs[$i]));
|
| 134 |
$last_printed = $i;
|
| 135 |
}
|
| 136 |
}
|
| 137 |
}
|
| 138 |
}
|
| 139 |
}
|
| 140 |
|
| 141 |
printf $outfile "\n";
|
| 142 |
|
| 143 |
# End
|