pkgsrc-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[pkgsrc/trunk]: pkgsrc/pkgtools/lintpkgsrc/files lintpksrc: fix parsing of th...



details:   https://anonhg.NetBSD.org/pkgsrc/rev/ec8e8911d2d4
branches:  trunk
changeset: 382959:ec8e8911d2d4
user:      rillig <rillig%pkgsrc.org@localhost>
date:      Tue Aug 09 18:35:43 2022 +0000

description:
lintpksrc: fix parsing of the ':S' modifier in makefiles

diffstat:

 pkgtools/lintpkgsrc/files/lintpkgsrc.pl |  46 ++++++++++++++++++--------------
 pkgtools/lintpkgsrc/files/t/glob.t      |  47 +++++++++++++++++---------------
 2 files changed, 51 insertions(+), 42 deletions(-)

diffs (165 lines):

diff -r 9af0a7371faf -r ec8e8911d2d4 pkgtools/lintpkgsrc/files/lintpkgsrc.pl
--- a/pkgtools/lintpkgsrc/files/lintpkgsrc.pl   Tue Aug 09 18:14:32 2022 +0000
+++ b/pkgtools/lintpkgsrc/files/lintpkgsrc.pl   Tue Aug 09 18:35:43 2022 +0000
@@ -1,6 +1,6 @@
 #!@PERL5@
 
-# $NetBSD: lintpkgsrc.pl,v 1.57 2022/08/09 18:14:22 rillig Exp $
+# $NetBSD: lintpkgsrc.pl,v 1.58 2022/08/09 18:35:43 rillig Exp $
 
 # Written by David Brownlee <abs%netbsd.org@localhost>.
 #
@@ -879,10 +879,10 @@
 # Return undef on error.
 sub glob2regex($) {
        my ($glob) = @_;
-       my (@chars, $in_alt);
-       my ($regex);
 
-       @chars = split(//, $glob);
+       my @chars = split(//, $glob);
+       my $alternative_depth = 0;
+       my $regex = '';
        while (defined($_ = shift @chars)) {
                if ($_ eq '*') {
                        $regex .= '.*';
@@ -890,37 +890,43 @@
                        $regex .= '.';
                } elsif ($_ eq '+') {
                        $regex .= '\\+';
-               } elsif ($_ eq '\\') {
-                       $regex .= $_ . shift @chars;
+               } elsif ($_ eq '\\' && @chars > 0) {
+                       my $next = shift @chars;
+                       $regex .= $next =~ /\w/ ? "$next" : "\\$next";
                } elsif ($_ eq '.' || $_ eq '|') {
                        $regex .= quotemeta;
                } elsif ($_ eq '{') {
                        $regex .= '(';
-                       ++$in_alt;
+                       ++$alternative_depth;
                } elsif ($_ eq '}') {
-                       if (!$in_alt) {
+                       if ($alternative_depth == 0) {
                                # Error
                                return undef;
                        }
                        $regex .= ')';
-                       --$in_alt;
-               } elsif ($_ eq ',' && $in_alt) {
+                       --$alternative_depth;
+               } elsif ($_ eq ',' && $alternative_depth) {
                        $regex .= '|';
+               } elsif ($_ eq '[') {
+                       $regex .= '[';
+                       while (defined($_ = shift @chars)) {
+                               $regex .= $_;
+                               if ($_ eq ']') {
+                                       last;
+                               } elsif ($_ eq '\\' && @chars > 0) {
+                                       $regex .= shift @chars;
+                               }
+                       }
+                       return undef if $_ ne ']';
                } else {
                        $regex .= $_;
                }
        }
 
-       if ($in_alt) {
-               # Error
-               return undef;
-       }
-       if ($regex eq $glob) {
-               return ('');
-       }
-       if ($opt{D}) {
-               print "glob2regex: $glob -> $regex\n";
-       }
+       return undef if $alternative_depth > 0;
+       return '' if $regex eq $glob; # XXX: why?
+
+       $opt{D} and print "glob2regex: $glob -> $regex\n";
        '^' . $regex . '$';
 }
 
diff -r 9af0a7371faf -r ec8e8911d2d4 pkgtools/lintpkgsrc/files/t/glob.t
--- a/pkgtools/lintpkgsrc/files/t/glob.t        Tue Aug 09 18:14:32 2022 +0000
+++ b/pkgtools/lintpkgsrc/files/t/glob.t        Tue Aug 09 18:35:43 2022 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: glob.t,v 1.5 2022/08/09 18:14:22 rillig Exp $
+# $NetBSD: glob.t,v 1.6 2022/08/09 18:35:43 rillig Exp $
 
 use strict;
 use warnings;
@@ -11,46 +11,49 @@
 sub test_glob2regex() {
 
        ok(glob2regex('*'), '^.*$');
+       ok(glob2regex('\*'), '');
 
        ok(glob2regex('?'), '^.$');
+       ok(glob2regex('\?'), '');
+
+       # Ordinary characters in glob patterns.
+       ok(glob2regex('+'), '^\+$');
+       ok(glob2regex('\+'), '');
+       ok(glob2regex('|'), '^\|$');
+       ok(glob2regex('\|'), '');
+
+       ok(glob2regex('\.'), '');
+       ok(glob2regex('\n'), '^n$');
+       ok(glob2regex('\\\\'), '');
+       ok(glob2regex('\['), '');
+       ok(glob2regex('\{'), '');
+       ok(glob2regex('\-'), '');
 
        ok(glob2regex('[a-z]'), '');
-
        ok(glob2regex('[a-z0-9]'), '');
-
        ok(glob2regex('[a-z0-9_]'), '');
-
-       # Outside of braces, the ',' is a regular character.
-       ok(glob2regex('a,b'), '');
-
-       # FIXME: Inside brackets, the '*' is a literal '*'.
-       ok(glob2regex('[*]'), '^[.*]$');
-
-       ok(glob2regex('\*'), '');
+       ok(glob2regex('[*]'), '');
 
        ok(glob2regex('*.[ch]'), '^.*\.[ch]$');
 
+       # Outside of braces, the ',' is a regular character.
+       ok(glob2regex('a,b'), '');
        ok(glob2regex('{one,two}'), '^(one|two)$');
-
        ok(glob2regex('{{thi,fou}r,fif}teen'), '^((thi|fou)r|fif)teen$');
 
        # There is an unbalanced '}' at the very end.
-       ok(glob2regex('{{thi,fou}r,fif}teen}'), undef);
+       ok(glob2regex('{four,fif}teen}'), undef);
 
-       ok(glob2regex('a+b|c'), '^a\+b\|c$');
-
+       # An escaped '[' does not start a character class.
        ok(glob2regex('a\[b*'), '^a\[b.*$');
 
-       ok(glob2regex('a\+b'), '');
-
-       ok(glob2regex('a\?b'), '');
-
-       # XXX: Depending on the exact implementation, the '\n' may be
-       # interpreted as a newline, a literal 'n' or a literal '\' 'n'.
-       ok(glob2regex('a\n*'), '^a\n.*$');
+       ok(glob2regex('a\n*'), '^an.*$');
 
        # https://gnats.netbsd.org/12996
        ok(glob2regex('libsigc++'), '^libsigc\+\+$');
+
+       my $re = 'a\nb';
+       ok("a\nb" =~ $re, 1);
 }
 
 test_glob2regex();



Home | Main Index | Thread Index | Old Index