On Wed, Oct 18, 2023 at 10:25:59AM +0200, Marc Espie wrote:
> Instead of an archaic limit, just keep track of tc's.
> This is actually slightly faster, because the termcap links is a tree,
> not a list.

I like it, but I don't want to carry this patch against upstream.  I
will happily pull in this change from there as soon as they merge it
though.

Changing the 32 to 64 is an easy patch to keep up on and to solve merge
conflicts with when upgrading.  This larger change is much less so.

That said, there is so much in this file that could be improved, why
stop here?

> 
> Please test.
> 
> Index: Cap.pm
> ===================================================================
> RCS file: /cvs/src/gnu/usr.bin/perl/cpan/Term-Cap/Cap.pm,v
> retrieving revision 1.3
> diff -u -p -r1.3 Cap.pm
> --- Cap.pm    18 Oct 2023 01:49:26 -0000      1.3
> +++ Cap.pm    18 Oct 2023 08:24:54 -0000
> @@ -280,7 +280,7 @@ sub Tgetent
>  
>      $first = 0;    # first entry (keeps term name)
>  
> -    $max = 64;     # max :tc=...:'s
> +    my $seen = {};   # keep track of :tc=...:s
>  
>      if ($entry)
>      {
> @@ -291,6 +291,7 @@ sub Tgetent
>          if ( $entry =~ s/:tc=([^:]+):/:/ )
>          {
>              $tmp_term = $1;
> +         $seen->{$tmp_term} = 1;
>  
>              # protect any pattern metacharacters in $tmp_term
>              $termpat = $tmp_term;
> @@ -332,10 +333,7 @@ sub Tgetent
>          }
>          else
>          {
> -
>              # do the same file again
> -            # prevent endless recursion
> -            $max-- || croak "failed termcap loop at $tmp_term";
>              $state = 1;    # ok, maybe do a new file next time
>          }
>  
> @@ -345,11 +343,20 @@ sub Tgetent
>          close TERMCAP;
>  
>          # If :tc=...: found then search this file again
> -        $entry =~ s/:tc=([^:]+):/:/ && ( $tmp_term = $1, $state = 2 );
> +        while ($entry =~ s/:tc=([^:]+):/:/) {
> +         $tmp_term = $1;
> +         if ($seen->{$tmp_term}) {
> +             # XXX first version of this croaked, but we can actually
> +             # get several intermediate entries with the same tc !
> +             next;
> +         }
> +         $seen->{$tmp_term} = 1;
> +         $state = 2;
>  
> -        # protect any pattern metacharacters in $tmp_term
> -        $termpat = $tmp_term;
> -        $termpat =~ s/(\W)/\\$1/g;
> +         # protect any pattern metacharacters in $tmp_term
> +         $termpat = $tmp_term;
> +         $termpat =~ s/(\W)/\\$1/g;
> +     }
>      }
>  
>      croak "Can't find $term" if $entry eq '';
> 

-- 
andrew

"Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots. So far, the Universe is
winning."             -- Rich Cook

Reply via email to