blob: 406a14e0bd34d8b0f0b9584a07203e6c4a233d0d [file] [log] [blame]
Janis Danisevskis112c9cc2016-03-31 13:35:25 +01001#! /bin/sh
2
3# Script for testing regular expressions with perl to check that PCRE2 handles
Elliott Hughes0c26e192019-08-07 12:24:46 -07004# 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.
Elliott Hughes9bc971b2018-07-27 13:23:14 -07008#
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.)
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010013#
14# The desired effect is achieved by making this a shell script that passes the
Elliott Hughes0c26e192019-08-07 12:24:46 -070015# Perl script to Perl through a pipe. If the next argument is "-utf8", a
16# suitable prefix is set up.
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010017#
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
Elliott Hughes9bc971b2018-07-27 13:23:14 -070025perlarg=''
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010026prefix=''
Elliott Hughes9bc971b2018-07-27 13:23:14 -070027
Elliott Hughes0c26e192019-08-07 12:24:46 -070028if [ $# -gt 1 -a "$1" = "-perl" ] ; then
29 shift
30 perl=$1
31 shift
32fi
33
Elliott Hughes9bc971b2018-07-27 13:23:14 -070034if [ $# -gt 0 -a "$1" = "-w" ] ; then
35 perlarg="-w"
36 shift
37fi
38
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010039if [ $# -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)
Elliott Hughes9bc971b2018-07-27 13:23:14 -070053# jitstack ignored
Elliott Hughes653c2102019-01-09 15:41:36 -080054# mark show mark information
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010055# no_auto_possess ignored
Elliott Hughes653c2102019-01-09 15:41:36 -080056# no_start_optimize insert (??{""}) at pattern start (disables optimizing)
57# -no_start_optimize ignored
Elliott Hughes9bc971b2018-07-27 13:23:14 -070058# subject_literal does not process subjects for escapes
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010059# ucp sets Perl's /u modifier
60# utf invoke UTF-8 functionality
61#
Elliott Hughes653c2102019-01-09 15:41:36 -080062# 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#
Elliott Hughes9bc971b2018-07-27 13:23:14 -070071# The data lines must not have any pcre2test modifiers. Unless
Elliott Hughes653c2102019-01-09 15:41:36 -080072# "subject_literal" is on the pattern, data lines are processed as
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010073# Perl double-quoted strings, so if they contain " $ or @ characters, these
74# have to be escaped. For this reason, all such characters in the
75# Perl-compatible testinput1 and testinput4 files are escaped so that they can
76# be used for perltest as well as for pcre2test. The output from this script
77# should be same as from pcre2test, apart from the initial identifying banner.
78#
79# The other testinput files are not suitable for feeding to perltest.sh,
80# because they make use of the special modifiers that pcre2test uses for
81# testing features of PCRE2. Some of these files also contain malformed regular
82# expressions, in order to check that PCRE2 diagnoses them correctly.
83
84(echo "$prefix" ; cat <<'PERLEND'
85
Elliott Hughes0c26e192019-08-07 12:24:46 -070086# The alpha assertions currently give warnings even when -w is not specified.
87
88no warnings "experimental::alpha_assertions";
89no warnings "experimental::script_run";
90
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010091# Function for turning a string into a string of printing chars.
92
93sub pchars {
94my($t) = "";
95if ($utf8)
96 {
97 @p = unpack('U*', $_[0]);
98 foreach $c (@p)
99 {
100 if ($c >= 32 && $c < 127) { $t .= chr $c; }
101 else { $t .= sprintf("\\x{%02x}", $c);
102 }
103 }
104 }
105else
106 {
107 foreach $c (split(//, $_[0]))
108 {
109 if (ord $c >= 32 && ord $c < 127) { $t .= $c; }
110 else { $t .= sprintf("\\x%02x", ord $c); }
111 }
112 }
113$t;
114}
115
116
117# Read lines from a named file or stdin and write to a named file or stdout;
118# lines consist of a regular expression, in delimiters and optionally followed
119# by options, followed by a set of test data, terminated by an empty line.
120
121# Sort out the input and output files
122
123if (@ARGV > 0)
124 {
125 open(INFILE, "<$ARGV[0]") || die "Failed to open $ARGV[0]\n";
126 $infile = "INFILE";
127 $interact = 0;
128 }
129else
130 {
131 open(INFILE, "</dev/tty") || die "Failed to open /dev/tty\n";
132 $infile = "INFILE";
133 $interact = 1;
134 }
135
136if (@ARGV > 1)
137 {
138 open(OUTFILE, ">$ARGV[1]") || die "Failed to open $ARGV[1]\n";
139 $outfile = "OUTFILE";
140 }
141else { $outfile = "STDOUT"; }
142
143printf($outfile "Perl $] Regular Expressions\n\n");
144
Elliott Hughes0c26e192019-08-07 12:24:46 -0700145$extra_modifiers = "";
146$default_show_mark = 0;
147
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100148# Main loop
149
150NEXT_RE:
151for (;;)
152 {
153 printf " re> " if $interact;
154 last if ! ($_ = <$infile>);
155 printf $outfile "$_" if ! $interact;
Elliott Hughes653c2102019-01-09 15:41:36 -0800156 next if ($_ =~ /^\s*$/ || $_ =~ /^#[\s!]/);
157
158 # A few of pcre2test's #-commands are supported, or just ignored. Any others
159 # cause an error.
160
161 if ($_ =~ /^#pattern(.*)/)
162 {
163 $extra_modifiers = $1;
164 chomp($extra_modifiers);
165 $extra_modifiers =~ s/\s+$//;
166 next;
167 }
168 elsif ($_ =~ /^#subject(.*)/)
169 {
170 $mod = $1;
171 chomp($mod);
172 $mod =~ s/\s+$//;
173 if ($mod =~ s/(-?)mark,?//)
174 {
175 $minus = $1;
176 $default_show_mark = ($minus =~ /^$/);
177 }
178 if ($mod !~ /^\s*$/)
179 {
180 printf $outfile "** Warning: \"$mod\" in #subject ignored\n";
181 }
182 next;
183 }
184 elsif ($_ =~ /^#/)
185 {
186 if ($_ !~ /^#newline_default|^#perltest|^#forbid_utf/)
187 {
188 printf $outfile "** Warning: #-command ignored: %s", $_;
189 }
190 next;
191 }
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100192
193 $pattern = $_;
194
195 while ($pattern !~ /^\s*(.).*\1/s)
196 {
197 printf " > " if $interact;
198 last if ! ($_ = <$infile>);
199 printf $outfile "$_" if ! $interact;
200 $pattern .= $_;
201 }
202
203 chomp($pattern);
204 $pattern =~ s/\s+$//;
205
206 # Split the pattern from the modifiers and adjust them as necessary.
207
208 $pattern =~ /^\s*((.).*\2)(.*)$/s;
209 $pat = $1;
Elliott Hughes653c2102019-01-09 15:41:36 -0800210 $del = $2;
211 $mod = "$3,$extra_modifiers";
212 $mod =~ s/^,\s*//;
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100213
214 # The private "aftertext" modifier means "print $' afterwards".
215
216 $showrest = ($mod =~ s/aftertext,?//);
217
Elliott Hughes9bc971b2018-07-27 13:23:14 -0700218 # The "subject_literal" modifer disables escapes in subjects.
219
220 $subject_literal = ($mod =~ s/subject_literal,?//);
221
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100222 # "allaftertext" is used by pcre2test to print remainders after captures
223
224 $mod =~ s/allaftertext,?//;
225
226 # Detect utf
227
228 $utf8 = $mod =~ s/utf,?//;
229
230 # Remove "dupnames".
231
232 $mod =~ s/dupnames,?//;
233
Elliott Hughes9bc971b2018-07-27 13:23:14 -0700234 # Remove "jitstack".
235
236 $mod =~ s/jitstack=\d+,?//;
237
Elliott Hughes653c2102019-01-09 15:41:36 -0800238 # The "mark" modifier requests checking of MARK data */
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100239
Elliott Hughes653c2102019-01-09 15:41:36 -0800240 $show_mark = $default_show_mark | ($mod =~ s/mark,?//);
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100241
242 # "ucp" asks pcre2test to set PCRE2_UCP; change this to /u for Perl
243
244 $mod =~ s/ucp,?/u/;
245
Elliott Hughes653c2102019-01-09 15:41:36 -0800246 # Remove "no_auto_possess".
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100247
248 $mod =~ s/no_auto_possess,?//;
Elliott Hughes653c2102019-01-09 15:41:36 -0800249
250 # Use no_start_optimize (disable PCRE2 start-up optimization) to disable Perl
251 # optimization by inserting (??{""}) at the start of the pattern. We may
252 # also encounter -no_start_optimize from a #pattern setting.
253
254 $mod =~ s/-no_start_optimize,?//;
255 if ($mod =~ s/no_start_optimize,?//) { $pat =~ s/$del/$del(??{""})/; }
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100256
257 # Add back retained modifiers and check that the pattern is valid.
258
259 $mod =~ s/,//g;
260 $pattern = "$pat$mod";
261 eval "\$_ =~ ${pattern}";
262 if ($@)
263 {
264 printf $outfile "Error: $@";
265 if (! $interact)
266 {
267 for (;;)
268 {
269 last if ! ($_ = <$infile>);
270 last if $_ =~ /^\s*$/;
271 }
272 }
273 next NEXT_RE;
274 }
275
276 # If the /g modifier is present, we want to put a loop round the matching;
277 # otherwise just a single "if".
278
279 $cmd = ($pattern =~ /g[a-z]*$/)? "while" : "if";
280
281 # If the pattern is actually the null string, Perl uses the most recently
282 # executed (and successfully compiled) regex is used instead. This is a
283 # nasty trap for the unwary! The PCRE2 test suite does contain null strings
284 # in places - if they are allowed through here all sorts of weird and
285 # unexpected effects happen. To avoid this, we replace such patterns with
286 # a non-null pattern that has the same effect.
287
288 $pattern = "/(?#)/$2" if ($pattern =~ /^(.)\1(.*)$/);
289
290 # Read data lines and test them
291
292 for (;;)
293 {
294 printf "data> " if $interact;
295 last NEXT_RE if ! ($_ = <$infile>);
296 chomp;
297 printf $outfile "%s", "$_\n" if ! $interact;
298
299 s/\s+$//; # Remove trailing space
300 s/^\s+//; # Remove leading space
301
302 last if ($_ eq "");
303 next if $_ =~ /^\\=(?:\s|$)/; # Comment line
304
Elliott Hughes9bc971b2018-07-27 13:23:14 -0700305 if ($subject_literal)
306 {
307 $x = $_;
308 }
309 else
310 {
311 $x = eval "\"$_\""; # To get escapes processed
312 }
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100313
314 # Empty array for holding results, ensure $REGERROR and $REGMARK are
315 # unset, then do the matching.
316
317 @subs = ();
318
319 $pushes = "push \@subs,\$&;" .
320 "push \@subs,\$1;" .
321 "push \@subs,\$2;" .
322 "push \@subs,\$3;" .
323 "push \@subs,\$4;" .
324 "push \@subs,\$5;" .
325 "push \@subs,\$6;" .
326 "push \@subs,\$7;" .
327 "push \@subs,\$8;" .
328 "push \@subs,\$9;" .
329 "push \@subs,\$10;" .
330 "push \@subs,\$11;" .
331 "push \@subs,\$12;" .
332 "push \@subs,\$13;" .
333 "push \@subs,\$14;" .
334 "push \@subs,\$15;" .
335 "push \@subs,\$16;" .
336 "push \@subs,\$'; }";
337
338 undef $REGERROR;
339 undef $REGMARK;
340
341 eval "${cmd} (\$x =~ ${pattern}) {" . $pushes;
342
343 if ($@)
344 {
345 printf $outfile "Error: $@\n";
346 next NEXT_RE;
347 }
348 elsif (scalar(@subs) == 0)
349 {
350 printf $outfile "No match";
Elliott Hughes653c2102019-01-09 15:41:36 -0800351 if ($show_mark && defined $REGERROR && $REGERROR != 1)
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100352 { printf $outfile (", mark = %s", &pchars($REGERROR)); }
353 printf $outfile "\n";
354 }
355 else
356 {
357 while (scalar(@subs) != 0)
358 {
359 printf $outfile (" 0: %s\n", &pchars($subs[0]));
360 printf $outfile (" 0+ %s\n", &pchars($subs[17])) if $showrest;
361 $last_printed = 0;
362 for ($i = 1; $i <= 16; $i++)
363 {
364 if (defined $subs[$i])
365 {
366 while ($last_printed++ < $i-1)
367 { printf $outfile ("%2d: <unset>\n", $last_printed); }
368 printf $outfile ("%2d: %s\n", $i, &pchars($subs[$i]));
369 $last_printed = $i;
370 }
371 }
372 splice(@subs, 0, 18);
373 }
374
375 # It seems that $REGMARK is not marked as UTF-8 even when use utf8 is
376 # set and the input pattern was a UTF-8 string. We can, however, force
377 # it to be so marked.
378
Elliott Hughes653c2102019-01-09 15:41:36 -0800379 if ($show_mark && defined $REGMARK && $REGMARK != 1)
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100380 {
381 $xx = $REGMARK;
382 $xx = Encode::decode_utf8($xx) if $utf8;
383 printf $outfile ("MK: %s\n", &pchars($xx));
384 }
385 }
386 }
387 }
388
Elliott Hughes0c26e192019-08-07 12:24:46 -0700389# By closing OUTFILE explicitly, we avoid a Perl warning in -w mode
390# "main::OUTFILE" used only once".
391
392close(OUTFILE) if $outfile eq "OUTFILE";
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100393
394PERLEND
Elliott Hughes9bc971b2018-07-27 13:23:14 -0700395) | $perl $perlarg - $@
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100396
397# End