Substvars.pm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. and ${dpkg:Upstream-Version}.
  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. };
  46. $self->{'dpkg:Upstream-Version'} =~ s/-[^-]+$//;
  47. bless $self, $class;
  48. if ($arg) {
  49. $self->parse($arg);
  50. }
  51. return $self;
  52. }
  53. =item $s->set($key, $value)
  54. Add/replace a substitution.
  55. =cut
  56. sub set {
  57. my ($self, $key, $value) = @_;
  58. $self->{$key} = $value;
  59. }
  60. =item $s->get($key)
  61. Get the value of a given substitution.
  62. =cut
  63. sub get {
  64. my ($self, $key) = @_;
  65. return $self->{$key};
  66. }
  67. =item $s->delete($key)
  68. Remove a given substitution.
  69. =cut
  70. sub delete {
  71. my ($self, $key) = @_;
  72. return delete $self->{$key};
  73. }
  74. =item $s->parse($file)
  75. Add new substitutions read from $file.
  76. =cut
  77. sub parse {
  78. my ($self, $varlistfile) = @_;
  79. $varlistfile="./$varlistfile" if $varlistfile =~ m/\s/;
  80. if (open(SV, "<", $varlistfile)) {
  81. binmode(SV);
  82. while (<SV>) {
  83. next if m/^\#/ || !m/\S/;
  84. s/\s*\n$//;
  85. m/^(\w[-:0-9A-Za-z]*)\=/ ||
  86. error(_g("bad line in substvars file %s at line %d"),
  87. $varlistfile, $.);
  88. $self->{$1} = $';
  89. }
  90. close(SV);
  91. } elsif ($! != ENOENT ) {
  92. error(_g("unable to open substvars file %s: %s"), $varlistfile, $!);
  93. }
  94. }
  95. =item $s->set_version_substvars($version)
  96. Defines ${binary:Version}, ${source:Version} and
  97. ${source:Upstream-Version} based on the given version string.
  98. =cut
  99. sub set_version_substvars {
  100. my ($self, $version) = @_;
  101. $self->{'binary:Version'} = $version;
  102. $self->{'source:Version'} = $version;
  103. $self->{'source:Version'} =~ s/\+b[0-9]+$//;
  104. $self->{'source:Upstream-Version'} = $version;
  105. $self->{'source:Upstream-Version'} =~ s/-[^-]*$//;
  106. # XXX: Source-Version is now deprecated, remove in the future.
  107. $self->{'Source-Version'} = $version;
  108. }
  109. =item $s->set_arch_substvars()
  110. Defines architecture variables: ${Arch}.
  111. =cut
  112. sub set_arch_substvars {
  113. my ($self) = @_;
  114. $self->{'Arch'} = get_host_arch();
  115. }
  116. =item $newstring = $s->substvars($string)
  117. Substitutes variables in $string and return the result in $newstring.
  118. =cut
  119. sub substvars {
  120. my ($self, $v) = @_;
  121. my $lhs;
  122. my $vn;
  123. my $rhs = '';
  124. my $count = 0;
  125. while ($v =~ m/\$\{([-:0-9a-z]+)\}/i) {
  126. # If we have consumed more from the leftover data, then
  127. # reset the recursive counter.
  128. $count = 0 if (length($POSTMATCH) < length($rhs));
  129. $count < $maxsubsts ||
  130. error(_g("too many substitutions - recursive ? - in \`%s'"), $v);
  131. $lhs = $PREMATCH; $vn = $1; $rhs = $POSTMATCH;
  132. if (defined($self->{$vn})) {
  133. $v = $lhs . $self->{$vn} . $rhs;
  134. $count++;
  135. } else {
  136. warning(_g("unknown substitution variable \${%s}"), $vn);
  137. $v = $lhs . $rhs;
  138. }
  139. }
  140. return $v;
  141. }
  142. =back
  143. =head1 AUTHOR
  144. Raphael Hertzog <hertzog@debian.org>.
  145. =cut
  146. 1;