Substvars.pm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. # Copyright © 2007 Raphaël Hertzog <hertzog@debian.org>
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 2 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License along
  11. # with this program; if not, write to the Free Software Foundation, Inc.,
  12. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  13. package Dpkg::Substvars;
  14. use strict;
  15. use warnings;
  16. use Dpkg qw($version);
  17. use Dpkg::Arch qw(get_host_arch);
  18. use Dpkg::ErrorHandling;
  19. use Dpkg::Gettext;
  20. use POSIX qw(:errno_h);
  21. use English;
  22. my $maxsubsts = 50;
  23. =head1 NAME
  24. Dpkg::Substvars - handle variable substitution in strings
  25. =head1 DESCRIPTION
  26. It provides some an object which is able to substitute variables in
  27. strings.
  28. =head1 METHODS
  29. =over 8
  30. =item my $s = Dpkg::Substvars->new($file)
  31. Create a new object that can do substitutions. By default it contains
  32. generic substitutions like ${Newline}, ${Space}, ${Tab}, ${dpkg:Version},
  33. ${dpkg:Upstream-Version} and ${Arch}.
  34. Additional substitutions will be read from the $file passed as parameter.
  35. =cut
  36. sub new {
  37. my ($this, $arg) = @_;
  38. my $class = ref($this) || $this;
  39. my $self = {
  40. "Newline" => "\n",
  41. "Space" => " ",
  42. "Tab" => "\t",
  43. "dpkg:Version" => $version,
  44. "dpkg:Upstream-Version" => $version,
  45. "Arch" => get_host_arch(),
  46. };
  47. $self->{'dpkg:Upstream-Version'} =~ s/-[^-]+$//;
  48. bless $self, $class;
  49. if ($arg) {
  50. $self->parse($arg);
  51. }
  52. return $self;
  53. }
  54. =item $s->set($key, $value)
  55. Add/replace a substitution.
  56. =cut
  57. sub set {
  58. my ($self, $key, $value) = @_;
  59. $self->{$key} = $value;
  60. }
  61. =item $s->get($key)
  62. Get the value of a given substitution.
  63. =cut
  64. sub get {
  65. my ($self, $key) = @_;
  66. return $self->{$key};
  67. }
  68. =item $s->delete($key)
  69. Remove a given substitution.
  70. =cut
  71. sub delete {
  72. my ($self, $key) = @_;
  73. return delete $self->{$key};
  74. }
  75. =item $s->parse($file)
  76. Add new substitutions read from $file.
  77. =cut
  78. sub parse {
  79. my ($self, $varlistfile) = @_;
  80. $varlistfile="./$varlistfile" if $varlistfile =~ m/\s/;
  81. if (open(SV, "<", $varlistfile)) {
  82. binmode(SV);
  83. while (<SV>) {
  84. next if m/^\#/ || !m/\S/;
  85. s/\s*\n$//;
  86. m/^(\w[-:0-9A-Za-z]*)\=/ ||
  87. error(_g("bad line in substvars file %s at line %d"),
  88. $varlistfile, $.);
  89. $self->{$1} = $';
  90. }
  91. close(SV);
  92. } elsif ($! != ENOENT ) {
  93. error(_g("unable to open substvars file %s: %s"), $varlistfile, $!);
  94. }
  95. }
  96. =item $s->set_version_substvars($version)
  97. Defines ${binary:Version}, ${source:Version} and
  98. ${source:Upstream-Version} based on the given version string.
  99. =cut
  100. sub set_version_substvars {
  101. my ($self, $version) = @_;
  102. $self->{'binary:Version'} = $version;
  103. $self->{'source:Version'} = $version;
  104. $self->{'source:Version'} =~ s/\+b[0-9]+$//;
  105. $self->{'source:Upstream-Version'} = $version;
  106. $self->{'source:Upstream-Version'} =~ s/-[^-]*$//;
  107. # XXX: Source-Version is now deprecated, remove in the future.
  108. $self->{'Source-Version'} = $version;
  109. }
  110. =item $newstring = $s->substvars($string)
  111. Substitutes variables in $string and return the result in $newstring.
  112. =cut
  113. sub substvars {
  114. my ($self, $v) = @_;
  115. my $lhs;
  116. my $vn;
  117. my $rhs = '';
  118. my $count = 0;
  119. while ($v =~ m/\$\{([-:0-9a-z]+)\}/i) {
  120. # If we have consumed more from the leftover data, then
  121. # reset the recursive counter.
  122. $count = 0 if (length($POSTMATCH) < length($rhs));
  123. $count < $maxsubsts ||
  124. error(_g("too many substitutions - recursive ? - in \`%s'"), $v);
  125. $lhs = $PREMATCH; $vn = $1; $rhs = $POSTMATCH;
  126. if (defined($self->{$vn})) {
  127. $v = $lhs . $self->{$vn} . $rhs;
  128. $count++;
  129. } else {
  130. warning(_g("unknown substitution variable \${%s}"), $vn);
  131. $v = $lhs . $rhs;
  132. }
  133. }
  134. return $v;
  135. }
  136. =back
  137. =head1 AUTHOR
  138. Raphael Hertzog <hertzog@debian.org>.
  139. =cut
  140. 1;