1#! /bin/sh 2 3# Script for testing regular expressions with perl to check that PCRE2 handles 4# them the same. For testing with different versions of Perl, if the first 5# argument is -perl then the second is taken as the Perl command to use, and 6# both are then removed. If the next argument is "-w", Perl is called with 7# "-w", which turns on its warning mode. 8# 9# The Perl code has to have "use utf8" and "require Encode" at the start when 10# running UTF-8 tests, but *not* for non-utf8 tests. (The "require" would 11# actually be OK for non-utf8-tests, but is not always installed, so this way 12# the script will always run for these tests.) 13# 14# The desired effect is achieved by making this a shell script that passes the 15# Perl script to Perl through a pipe. If the next argument is "-utf8", a 16# suitable prefix is set up. 17# 18# The remaining arguments, if any, are passed to Perl. They are an input file 19# and an output file. If there is one argument, the output is written to 20# STDOUT. If Perl receives no arguments, it opens /dev/tty as input, and writes 21# output to STDOUT. (I haven't found a way of getting it to use STDIN, because 22# of the contorted piping input.) 23 24perl=perl 25perlarg='' 26prefix='' 27 28if [ $# -gt 1 -a "$1" = "-perl" ] ; then 29 shift 30 perl=$1 31 shift 32fi 33 34if [ $# -gt 0 -a "$1" = "-w" ] ; then 35 perlarg="-w" 36 shift 37fi 38 39if [ $# -gt 0 -a "$1" = "-utf8" ] ; then 40 prefix="use utf8; require Encode;" 41 shift 42fi 43 44 45# The Perl script that follows has a similar specification to pcre2test, and so 46# can be given identical input, except that input patterns can be followed only 47# by Perl's lower case modifiers and certain other pcre2test modifiers that are 48# either handled or ignored: 49# 50# aftertext interpreted as "print $' afterwards" 51# afteralltext ignored 52# dupnames ignored (Perl always allows) 53# jitstack ignored 54# mark show mark information 55# no_auto_possess ignored 56# no_start_optimize insert (??{""}) at pattern start (disables optimizing) 57# -no_start_optimize ignored 58# subject_literal does not process subjects for escapes 59# ucp sets Perl's /u modifier 60# utf invoke UTF-8 functionality 61# 62# Comment lines are ignored. The #pattern command can be used to set modifiers 63# that will be added to each subsequent pattern, after any modifiers it may 64# already have. NOTE: this is different to pcre2test where #pattern sets 65# defaults which can be overridden on individual patterns. The #subject command 66# may be used to set or unset a default "mark" modifier for data lines. This is 67# the only use of #subject that is supported. The #perltest, #forbid_utf, and 68# #newline_default commands, which are needed in the relevant pcre2test files, 69# are ignored. Any other #-command is ignored, with a warning message. 70# 71# The pattern lines should use only / as the delimiter. The other characters 72# that pcre2test supports cause problems with this script. 73# 74# The data lines must not have any pcre2test modifiers. Unless 75# "subject_literal" is on the pattern, data lines are processed as 76# Perl double-quoted strings, so if they contain " $ or @ characters, these 77# have to be escaped. For this reason, all such characters in the 78# Perl-compatible testinput1 and testinput4 files are escaped so that they can 79# be used for perltest as well as for pcre2test. The output from this script 80# should be same as from pcre2test, apart from the initial identifying banner. 81# 82# The other testinput files are not suitable for feeding to perltest.sh, 83# because they make use of the special modifiers that pcre2test uses for 84# testing features of PCRE2. Some of these files also contain malformed regular 85# expressions, in order to check that PCRE2 diagnoses them correctly. 86 87(echo "$prefix" ; cat <<'PERLEND' 88 89# The alpha assertions currently give warnings even when -w is not specified. 90 91no warnings "experimental::alpha_assertions"; 92no warnings "experimental::script_run"; 93 94# Function for turning a string into a string of printing chars. 95 96sub pchars { 97my($t) = ""; 98if ($utf8) 99 { 100 @p = unpack('U*', $_[0]); 101 foreach $c (@p) 102 { 103 if ($c >= 32 && $c < 127) { $t .= chr $c; } 104 else { $t .= sprintf("\\x{%02x}", $c); 105 } 106 } 107 } 108else 109 { 110 foreach $c (split(//, $_[0])) 111 { 112 if (ord $c >= 32 && ord $c < 127) { $t .= $c; } 113 else { $t .= sprintf("\\x%02x", ord $c); } 114 } 115 } 116$t; 117} 118 119 120# Read lines from a named file or stdin and write to a named file or stdout; 121# lines consist of a regular expression, in delimiters and optionally followed 122# by options, followed by a set of test data, terminated by an empty line. 123 124# Sort out the input and output files 125 126if (@ARGV > 0) 127 { 128 open(INFILE, "<$ARGV[0]") || die "Failed to open $ARGV[0]\n"; 129 $infile = "INFILE"; 130 $interact = 0; 131 } 132else 133 { 134 open(INFILE, "</dev/tty") || die "Failed to open /dev/tty\n"; 135 $infile = "INFILE"; 136 $interact = 1; 137 } 138 139if (@ARGV > 1) 140 { 141 open(OUTFILE, ">$ARGV[1]") || die "Failed to open $ARGV[1]\n"; 142 $outfile = "OUTFILE"; 143 } 144else { $outfile = "STDOUT"; } 145 146printf($outfile "Perl $^V\n\n"); 147 148$extra_modifiers = ""; 149$default_show_mark = 0; 150 151# Main loop 152 153NEXT_RE: 154for (;;) 155 { 156 printf " re> " if $interact; 157 last if ! ($_ = <$infile>); 158 printf $outfile "$_" if ! $interact; 159 next if ($_ =~ /^\s*$/ || $_ =~ /^#[\s!]/); 160 161 # A few of pcre2test's #-commands are supported, or just ignored. Any others 162 # cause an error. 163 164 if ($_ =~ /^#pattern(.*)/) 165 { 166 $extra_modifiers = $1; 167 chomp($extra_modifiers); 168 $extra_modifiers =~ s/\s+$//; 169 next; 170 } 171 elsif ($_ =~ /^#subject(.*)/) 172 { 173 $mod = $1; 174 chomp($mod); 175 $mod =~ s/\s+$//; 176 if ($mod =~ s/(-?)mark,?//) 177 { 178 $minus = $1; 179 $default_show_mark = ($minus =~ /^$/); 180 } 181 if ($mod !~ /^\s*$/) 182 { 183 printf $outfile "** Warning: \"$mod\" in #subject ignored\n"; 184 } 185 next; 186 } 187 elsif ($_ =~ /^#/) 188 { 189 if ($_ !~ /^#newline_default|^#perltest|^#forbid_utf/) 190 { 191 printf $outfile "** Warning: #-command ignored: %s", $_; 192 } 193 next; 194 } 195 196 $pattern = $_; 197 198 while ($pattern !~ /^\s*(.).*\1/s) 199 { 200 printf " > " if $interact; 201 last if ! ($_ = <$infile>); 202 printf $outfile "$_" if ! $interact; 203 $pattern .= $_; 204 } 205 206 chomp($pattern); 207 $pattern =~ s/\s+$//; 208 209 # Split the pattern from the modifiers and adjust them as necessary. 210 211 $pattern =~ /^\s*((.).*\2)(.*)$/s; 212 $pat = $1; 213 $del = $2; 214 $mod = "$3,$extra_modifiers"; 215 $mod =~ s/^,\s*//; 216 217 # The private "aftertext" modifier means "print $' afterwards". 218 219 $showrest = ($mod =~ s/aftertext,?//); 220 221 # The "subject_literal" modifier disables escapes in subjects. 222 223 $subject_literal = ($mod =~ s/subject_literal,?//); 224 225 # "allaftertext" is used by pcre2test to print remainders after captures 226 227 $mod =~ s/allaftertext,?//; 228 229 # Detect utf 230 231 $utf8 = $mod =~ s/utf,?//; 232 233 # Remove "dupnames". 234 235 $mod =~ s/dupnames,?//; 236 237 # Remove "jitstack". 238 239 $mod =~ s/jitstack=\d+,?//; 240 241 # The "mark" modifier requests checking of MARK data */ 242 243 $show_mark = $default_show_mark | ($mod =~ s/mark,?//); 244 245 # "ucp" asks pcre2test to set PCRE2_UCP; change this to /u for Perl 246 247 $mod =~ s/ucp,?/u/; 248 249 # Remove "no_auto_possess". 250 251 $mod =~ s/no_auto_possess,?//; 252 253 # Use no_start_optimize (disable PCRE2 start-up optimization) to disable Perl 254 # optimization by inserting (??{""}) at the start of the pattern. We may 255 # also encounter -no_start_optimize from a #pattern setting. 256 257 $mod =~ s/-no_start_optimize,?//; 258 259 if ($mod =~ s/no_start_optimize,?//) { $pat =~ s/$del/$del(??{""})/; } 260 261 # Add back retained modifiers and check that the pattern is valid. 262 263 $mod =~ s/,//g; 264 $pattern = "$pat$mod"; 265 266 eval "\$_ =~ ${pattern}"; 267 if ($@) 268 { 269 printf $outfile "Error: $@"; 270 if (! $interact) 271 { 272 for (;;) 273 { 274 last if ! ($_ = <$infile>); 275 last if $_ =~ /^\s*$/; 276 } 277 } 278 next NEXT_RE; 279 } 280 281 # If the /g modifier is present, we want to put a loop round the matching; 282 # otherwise just a single "if". 283 284 $cmd = ($pattern =~ /g[a-z]*\s*$/)? "while" : "if"; 285 286 # If the pattern is actually the null string, Perl uses the most recently 287 # executed (and successfully compiled) regex is used instead. This is a 288 # nasty trap for the unwary! The PCRE2 test suite does contain null strings 289 # in places - if they are allowed through here all sorts of weird and 290 # unexpected effects happen. To avoid this, we replace such patterns with 291 # a non-null pattern that has the same effect. 292 293 $pattern = "/(?#)/$2" if ($pattern =~ /^(.)\1(.*)$/); 294 295 # Read data lines and test them 296 297 for (;;) 298 { 299 printf "data> " if $interact; 300 last NEXT_RE if ! ($_ = <$infile>); 301 chomp; 302 printf $outfile "%s", "$_\n" if ! $interact; 303 304 s/\s+$//; # Remove trailing space 305 s/^\s+//; # Remove leading space 306 307 last if ($_ eq ""); 308 next if $_ =~ /^\\=(?:\s|$)/; # Comment line 309 310 if ($subject_literal) 311 { 312 $x = $_; 313 } 314 else 315 { 316 $x = eval "\"$_\""; # To get escapes processed 317 } 318 319 # Empty array for holding results, ensure $REGERROR and $REGMARK are 320 # unset, then do the matching. 321 322 @subs = (); 323 324 $pushes = "push \@subs,\$&;" . 325 "push \@subs,\$1;" . 326 "push \@subs,\$2;" . 327 "push \@subs,\$3;" . 328 "push \@subs,\$4;" . 329 "push \@subs,\$5;" . 330 "push \@subs,\$6;" . 331 "push \@subs,\$7;" . 332 "push \@subs,\$8;" . 333 "push \@subs,\$9;" . 334 "push \@subs,\$10;" . 335 "push \@subs,\$11;" . 336 "push \@subs,\$12;" . 337 "push \@subs,\$13;" . 338 "push \@subs,\$14;" . 339 "push \@subs,\$15;" . 340 "push \@subs,\$16;" . 341 "push \@subs,\$'; }"; 342 343 undef $REGERROR; 344 undef $REGMARK; 345 346 eval "${cmd} (\$x =~ ${pattern}) {" . $pushes; 347 348 if ($@) 349 { 350 printf $outfile "Error: $@\n"; 351 next NEXT_RE; 352 } 353 elsif (scalar(@subs) == 0) 354 { 355 printf $outfile "No match"; 356 if ($show_mark && defined $REGERROR && $REGERROR != 1) 357 { printf $outfile (", mark = %s", &pchars($REGERROR)); } 358 printf $outfile "\n"; 359 } 360 else 361 { 362 while (scalar(@subs) != 0) 363 { 364 printf $outfile (" 0: %s\n", &pchars($subs[0])); 365 printf $outfile (" 0+ %s\n", &pchars($subs[17])) if $showrest; 366 $last_printed = 0; 367 for ($i = 1; $i <= 16; $i++) 368 { 369 if (defined $subs[$i]) 370 { 371 while ($last_printed++ < $i-1) 372 { printf $outfile ("%2d: <unset>\n", $last_printed); } 373 printf $outfile ("%2d: %s\n", $i, &pchars($subs[$i])); 374 $last_printed = $i; 375 } 376 } 377 splice(@subs, 0, 18); 378 } 379 380 # It seems that $REGMARK is not marked as UTF-8 even when use utf8 is 381 # set and the input pattern was a UTF-8 string. We can, however, force 382 # it to be so marked. 383 384 if ($show_mark && defined $REGMARK && $REGMARK != 1) 385 { 386 $xx = $REGMARK; 387 $xx = Encode::decode_utf8($xx) if $utf8; 388 printf $outfile ("MK: %s\n", &pchars($xx)); 389 } 390 } 391 } 392 } 393 394# By closing OUTFILE explicitly, we avoid a Perl warning in -w mode 395# "main::OUTFILE" used only once". 396 397close(OUTFILE) if $outfile eq "OUTFILE"; 398 399PERLEND 400) | $perl $perlarg - $@ 401 402# End 403