<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># Copyright Â© 2006-2009, 2012-2020, 2022 Guillem Jover &lt;guillem@debian.org&gt;
# Copyright Â© 2007-2010 RaphaÃ«l Hertzog &lt;hertzog@debian.org&gt;
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see &lt;https://www.gnu.org/licenses/&gt;.

=encoding utf8

=head1 NAME

Dpkg::Substvars - handle variable substitution in strings

=head1 DESCRIPTION

It provides a class which is able to substitute variables in strings.

=cut

package Dpkg::Substvars 2.01;

use strict;
use warnings;

use Dpkg ();
use Dpkg::Arch qw(get_host_arch);
use Dpkg::Vendor qw(get_current_vendor);
use Dpkg::Version;
use Dpkg::ErrorHandling;
use Dpkg::Gettext;

use parent qw(Dpkg::Interface::Storable);

my $maxsubsts = 50;

use constant {
    SUBSTVAR_ATTR_USED =&gt; 1,
    SUBSTVAR_ATTR_AUTO =&gt; 2,
    SUBSTVAR_ATTR_AGED =&gt; 4,
    SUBSTVAR_ATTR_OPT  =&gt; 8,
    SUBSTVAR_ATTR_DEEP =&gt; 16,
};

=head1 METHODS

=over 8

=item $s = Dpkg::Substvars-&gt;new($file)

Create a new object that can do substitutions. By default it contains
generic substitutions like ${Newline}, ${Space}, ${Tab}, ${dpkg:Version}
and ${dpkg:Upstream-Version}.

Additional substitutions will be read from the $file passed as parameter.

It keeps track of which substitutions were actually used (only counting
substvars(), not get()), and warns about unused substvars when asked to. The
substitutions that are always present are not included in these warnings.

=cut

sub new {
    my ($this, $arg) = @_;
    my $class = ref($this) || $this;
    my $self = {
        vars =&gt; {
            'Newline' =&gt; "\n",
            'Space' =&gt; ' ',
            'Tab' =&gt; "\t",
            'dpkg:Version' =&gt; $Dpkg::PROGVERSION,
            'dpkg:Upstream-Version' =&gt; $Dpkg::PROGVERSION,
            },
        attr =&gt; {},
	msg_prefix =&gt; '',
    };
    $self-&gt;{vars}{'dpkg:Upstream-Version'} =~ s/-[^-]+$//;
    bless $self, $class;

    my $attr = SUBSTVAR_ATTR_USED | SUBSTVAR_ATTR_AUTO;
    $self-&gt;{attr}{$_} = $attr foreach keys %{$self-&gt;{vars}};
    if ($arg) {
        $self-&gt;load($arg) if -e $arg;
    }
    return $self;
}

=item $s-&gt;set($key, $value)

Add/replace a substitution.

=cut

sub set {
    my ($self, $key, $value, $attr) = @_;

    $attr //= 0;
    $attr |= SUBSTVAR_ATTR_DEEP if length $value &amp;&amp; $value =~ m{\$};

    $self-&gt;{vars}{$key} = $value;
    $self-&gt;{attr}{$key} = $attr;
}

=item $s-&gt;set_as_used($key, $value)

Add/replace a substitution and mark it as used (no warnings will be produced
even if unused).

=cut

sub set_as_used {
    my ($self, $key, $value) = @_;

    $self-&gt;set($key, $value, SUBSTVAR_ATTR_USED);
}

=item $s-&gt;set_as_auto($key, $value)

Add/replace a substitution and mark it as used and automatic (no warnings
will be produced even if unused).

=cut

sub set_as_auto {
    my ($self, $key, $value) = @_;

    $self-&gt;set($key, $value, SUBSTVAR_ATTR_USED | SUBSTVAR_ATTR_AUTO);
}

=item $s-&gt;get($key)

Get the value of a given substitution.

=cut

sub get {
    my ($self, $key) = @_;
    return $self-&gt;{vars}{$key};
}

=item $s-&gt;delete($key)

Remove a given substitution.

=cut

sub delete {
    my ($self, $key) = @_;
    delete $self-&gt;{attr}{$key};
    return delete $self-&gt;{vars}{$key};
}

=item $s-&gt;mark_as_used($key)

Prevents warnings about a unused substitution, for example if it is provided by
default.

=cut

sub mark_as_used {
    my ($self, $key) = @_;
    $self-&gt;{attr}{$key} |= SUBSTVAR_ATTR_USED;
}

=item $s-&gt;parse($fh, $desc)

Add new substitutions read from the filehandle. $desc is used to identify
the filehandle in error messages.

Returns the number of substitutions that have been parsed with success.

=cut

sub parse {
    my ($self, $fh, $varlistfile) = @_;
    my $count = 0;
    local $_;

    binmode($fh);
    while (&lt;$fh&gt;) {
        my $attr;

	next if m/^\s*\#/ || !m/\S/;
	s/\s*\n$//;
	if (! m/^(\w[-:0-9A-Za-z]*)(\?)?\=(.*)$/) {
	    error(g_('bad line in substvars file %s at line %d'),
		  $varlistfile, $.);
	}
        ## no critic (RegularExpressions::ProhibitCaptureWithoutTest)
        if (defined $2) {
            $attr = (SUBSTVAR_ATTR_USED | SUBSTVAR_ATTR_OPT) if $2 eq '?';
        }
        $self-&gt;set($1, $3, $attr);
        $count++;
    }

    return $count
}

=item $s-&gt;load($file)

Add new substitutions read from $file.

=item $s-&gt;set_version_substvars($sourceversion, $binaryversion)

Defines ${binary:Version}, ${source:Version} and
${source:Upstream-Version} based on the given version strings.

These will never be warned about when unused.

=cut

sub set_version_substvars {
    my ($self, $sourceversion, $binaryversion) = @_;

    # Handle old function signature taking only one argument.
    $binaryversion //= $sourceversion;

    # For backwards compatibility on binNMUs that do not use the Binary-Only
    # field on the changelog, always fix up the source version.
    $sourceversion =~ s/\+b[0-9]+$//;

    my $vs = Dpkg::Version-&gt;new($sourceversion, check =&gt; 1);
    if (not defined $vs) {
        error(g_('invalid source version %s'), $sourceversion);
    }
    my $upstreamversion = $vs-&gt;as_string(omit_revision =&gt; 1);

    my $attr = SUBSTVAR_ATTR_USED | SUBSTVAR_ATTR_AUTO;

    $self-&gt;set('binary:Version', $binaryversion, $attr);
    $self-&gt;set('source:Version', $sourceversion, $attr);
    $self-&gt;set('source:Upstream-Version', $upstreamversion, $attr);

    # XXX: Source-Version is now obsolete, remove in 1.19.x.
    $self-&gt;set('Source-Version', $binaryversion, $attr | SUBSTVAR_ATTR_AGED);
}

=item $s-&gt;set_arch_substvars()

Defines architecture variables: ${Arch}.

This will never be warned about when unused.

=cut

sub set_arch_substvars {
    my $self = shift;

    my $attr = SUBSTVAR_ATTR_USED | SUBSTVAR_ATTR_AUTO;

    $self-&gt;set('Arch', get_host_arch(), $attr);
}

=item $s-&gt;set_vendor_substvars()

Defines vendor variables: ${vendor:Name} and ${vendor:Id}.

These will never be warned about when unused.

=cut

sub set_vendor_substvars {
    my ($self, $desc) = @_;

    my $attr = SUBSTVAR_ATTR_USED | SUBSTVAR_ATTR_AUTO;

    my $vendor = get_current_vendor();
    $self-&gt;set('vendor:Name', $vendor, $attr);
    $self-&gt;set('vendor:Id', lc $vendor, $attr);
}

=item $s-&gt;set_desc_substvars()

Defines source description variables: ${source:Synopsis} and
${source:Extended-Description}.

These will never be warned about when unused.

=cut

sub set_desc_substvars {
    my ($self, $desc) = @_;

    my ($synopsis, $extended) = split /\n/, $desc, 2;

    my $attr = SUBSTVAR_ATTR_USED | SUBSTVAR_ATTR_AUTO;

    $self-&gt;set('source:Synopsis', $synopsis, $attr);
    $self-&gt;set('source:Extended-Description', $extended, $attr);
}

=item $s-&gt;set_field_substvars($ctrl, $prefix)

Defines field variables from a L&lt;Dpkg::Control&gt; object, with each variable
having the form "${$prefix:$field}".

They will never be warned about when unused.

=cut

sub set_field_substvars {
    my ($self, $ctrl, $prefix) = @_;

    foreach my $field (keys %{$ctrl}) {
        $self-&gt;set_as_auto("$prefix:$field", $ctrl-&gt;{$field});
    }
}

=item $newstring = $s-&gt;substvars($string)

Substitutes variables in $string and return the result in $newstring.

=cut

sub substvars {
    my ($self, $v, %opts) = @_;
    my %seen;
    my $lhs;
    my $vn;
    my $rhs = '';
    $opts{msg_prefix} //= $self-&gt;{msg_prefix};
    $opts{no_warn} //= 0;

    while ($v =~ m/^(.*?)\$\{([-:0-9a-z]+)\}(.*)$/si) {
        $lhs = $1;
        $vn = $2;
        $rhs = $3;

        if (defined($self-&gt;{vars}{$vn})) {
            $v = $lhs . $self-&gt;{vars}{$vn} . $rhs;
            $self-&gt;mark_as_used($vn);

            if ($self-&gt;{attr}{$vn} &amp; SUBSTVAR_ATTR_DEEP) {
                $seen{$vn}++;
            }
            if (exists $seen{$vn} &amp;&amp; $seen{$vn} &gt;= $maxsubsts) {
                error($opts{msg_prefix} .
                      g_("too many \${%s} substitutions (recursive?) in '%s'"),
                      $vn, $v);
            }

            if ($self-&gt;{attr}{$vn} &amp; SUBSTVAR_ATTR_AGED) {
                error($opts{msg_prefix} .
                      g_('obsolete substitution variable ${%s}'), $vn);
            }
        } else {
            warning($opts{msg_prefix} .
                    g_('substitution variable ${%s} used, but is not defined'),
	            $vn) unless $opts{no_warn};
            $v = $lhs . $rhs;
        }
    }
    return $v;
}

=item $s-&gt;warn_about_unused()

Issues warning about any variables that were set, but not used.

=cut

sub warn_about_unused {
    my ($self, %opts) = @_;
    $opts{msg_prefix} //= $self-&gt;{msg_prefix};

    foreach my $vn (sort keys %{$self-&gt;{vars}}) {
        next if $self-&gt;{attr}{$vn} &amp; SUBSTVAR_ATTR_USED;
        # Empty substitutions variables are ignored on the basis
        # that they are not required in the current situation
        # (example: debhelper's misc:Depends in many cases)
        next if $self-&gt;{vars}{$vn} eq '';
        warning($opts{msg_prefix} .
                g_('substitution variable ${%s} unused, but is defined'),
                $vn);
    }
}

=item $s-&gt;set_msg_prefix($prefix)

Define a prefix displayed before all warnings/error messages output
by the module.

=cut

sub set_msg_prefix {
    my ($self, $prefix) = @_;
    $self-&gt;{msg_prefix} = $prefix;
}

=item $s-&gt;filter(remove =&gt; $rmfunc)

=item $s-&gt;filter(keep =&gt; $keepfun)

Filter the substitution variables, either removing or keeping all those
that return true when $rmfunc-&gt;($key) or $keepfunc-&gt;($key) is called.

=cut

sub filter {
    my ($self, %opts) = @_;

    my $remove = $opts{remove} // sub { 0 };
    my $keep = $opts{keep} // sub { 1 };

    foreach my $vn (keys %{$self-&gt;{vars}}) {
        $self-&gt;delete($vn) if $remove-&gt;($vn) or not $keep-&gt;($vn);
    }
}

=item "$s"

Return a string representation of all substitutions variables except the
automatic ones.

=item $str = $s-&gt;output([$fh])

Return all substitutions variables except the automatic ones. If $fh
is passed print them into the filehandle.

=cut

sub output {
    my ($self, $fh) = @_;
    my $str = '';
    # Store all non-automatic substitutions only
    foreach my $vn (sort keys %{$self-&gt;{vars}}) {
	next if $self-&gt;{attr}{$vn} &amp; SUBSTVAR_ATTR_AUTO;
        my $op = $self-&gt;{attr}{$vn} &amp; SUBSTVAR_ATTR_OPT ? '?=' : '=';
        my $line = "$vn$op" . $self-&gt;{vars}{$vn} . "\n";
	print { $fh } $line if defined $fh;
	$str .= $line;
    }
    return $str;
}

=item $s-&gt;save($file)

Store all substitutions variables except the automatic ones in the
indicated file.

=back

=head1 CHANGES

=head2 Version 2.01 (dpkg 1.21.8)

New feature: Add support for optional substitution variables.

=head2 Version 2.00 (dpkg 1.20.0)

Remove method: $s-&gt;no_warn().

New method: $s-&gt;set_vendor_substvars().

=head2 Version 1.06 (dpkg 1.19.0)

New method: $s-&gt;set_desc_substvars().

=head2 Version 1.05 (dpkg 1.18.11)

Obsolete substvar: Emit an error on Source-Version substvar usage.

New return: $s-&gt;parse() now returns the number of parsed substvars.

New method: $s-&gt;set_field_substvars().

=head2 Version 1.04 (dpkg 1.18.0)

New method: $s-&gt;filter().

=head2 Version 1.03 (dpkg 1.17.11)

New method: $s-&gt;set_as_auto().

=head2 Version 1.02 (dpkg 1.16.5)

New argument: Accept a $binaryversion in $s-&gt;set_version_substvars(),
passing a single argument is still supported.

New method: $s-&gt;mark_as_used().

Deprecated method: $s-&gt;no_warn(), use $s-&gt;mark_as_used() instead.

=head2 Version 1.01 (dpkg 1.16.4)

New method: $s-&gt;set_as_used().

=head2 Version 1.00 (dpkg 1.15.6)

Mark the module as public.

=cut

1;
</pre></body></html>