Substvars.pm 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. # Copyright © 2007,2009 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
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. package Dpkg::Substvars;
  13. use strict;
  14. use warnings;
  15. use Dpkg qw($version);
  16. use Dpkg::Arch qw(get_host_arch);
  17. use Dpkg::ErrorHandling;
  18. use Dpkg::Gettext;
  19. use POSIX qw(:errno_h);
  20. my $maxsubsts = 50;
  21. =head1 NAME
  22. Dpkg::Substvars - handle variable substitution in strings
  23. =head1 DESCRIPTION
  24. It provides some an object which is able to substitute variables in
  25. strings.
  26. =head1 METHODS
  27. =over 8
  28. =item my $s = Dpkg::Substvars->new($file)
  29. Create a new object that can do substitutions. By default it contains
  30. generic substitutions like ${Newline}, ${Space}, ${Tab}, ${dpkg:Version}
  31. and ${dpkg:Upstream-Version}.
  32. Additional substitutions will be read from the $file passed as parameter.
  33. It keeps track of which substitutions were actually used (only counting
  34. substvars(), not get()), and warns about unused substvars when asked to. The
  35. substitutions that are always present are not included in these warnings.
  36. =cut
  37. sub new {
  38. my ($this, $arg) = @_;
  39. my $class = ref($this) || $this;
  40. my $self = {
  41. vars => {
  42. "Newline" => "\n",
  43. "Space" => " ",
  44. "Tab" => "\t",
  45. "dpkg:Version" => $version,
  46. "dpkg:Upstream-Version" => $version,
  47. },
  48. used => {},
  49. };
  50. $self->{'vars'}{'dpkg:Upstream-Version'} =~ s/-[^-]+$//;
  51. bless $self, $class;
  52. $self->no_warn($_) foreach keys %{$self->{'vars'}};
  53. if ($arg) {
  54. $self->parse($arg);
  55. }
  56. return $self;
  57. }
  58. =item $s->set($key, $value)
  59. Add/replace a substitution.
  60. =cut
  61. sub set {
  62. my ($self, $key, $value) = @_;
  63. $self->{'vars'}{$key} = $value;
  64. }
  65. =item $s->get($key)
  66. Get the value of a given substitution.
  67. =cut
  68. sub get {
  69. my ($self, $key) = @_;
  70. return $self->{'vars'}{$key};
  71. }
  72. =item $s->delete($key)
  73. Remove a given substitution.
  74. =cut
  75. sub delete {
  76. my ($self, $key) = @_;
  77. delete $self->{'used'}{$key};
  78. return delete $self->{'vars'}{$key};
  79. }
  80. =item $s->no_warn($key)
  81. Prevents warnings about a unused substitution, for example if it is provided by
  82. default.
  83. =cut
  84. sub no_warn {
  85. my ($self, $key) = @_;
  86. $self->{'used'}{$key}++;
  87. }
  88. =item $s->parse($file)
  89. Add new substitutions read from $file.
  90. =cut
  91. sub parse {
  92. my ($self, $varlistfile) = @_;
  93. $varlistfile = "./$varlistfile" if $varlistfile =~ m/\s/;
  94. if (open(SV, "<", $varlistfile)) {
  95. binmode(SV);
  96. while (<SV>) {
  97. next if m/^\s*\#/ || !m/\S/;
  98. s/\s*\n$//;
  99. m/^(\w[-:0-9A-Za-z]*)\=(.*)$/ ||
  100. error(_g("bad line in substvars file %s at line %d"),
  101. $varlistfile, $.);
  102. $self->{'vars'}{$1} = $2;
  103. }
  104. close(SV);
  105. } elsif ($! != ENOENT) {
  106. error(_g("unable to open substvars file %s: %s"), $varlistfile, $!);
  107. }
  108. }
  109. =item $s->set_version_substvars($version)
  110. Defines ${binary:Version}, ${source:Version} and
  111. ${source:Upstream-Version} based on the given version string.
  112. These will never be warned about when unused.
  113. =cut
  114. sub set_version_substvars {
  115. my ($self, $version) = @_;
  116. $self->{'vars'}{'binary:Version'} = $version;
  117. $self->{'vars'}{'source:Version'} = $version;
  118. $self->{'vars'}{'source:Version'} =~ s/\+b[0-9]+$//;
  119. $self->{'vars'}{'source:Upstream-Version'} = $version;
  120. $self->{'vars'}{'source:Upstream-Version'} =~ s/-[^-]*$//;
  121. # XXX: Source-Version is now deprecated, remove in the future.
  122. $self->{'vars'}{'Source-Version'} = $version;
  123. $self->no_warn($_) foreach qw/binary:Version source:Version source:Upstream-Version Source-Version/;
  124. }
  125. =item $s->set_arch_substvars()
  126. Defines architecture variables: ${Arch}.
  127. This will never be warned about when unused.
  128. =cut
  129. sub set_arch_substvars {
  130. my ($self) = @_;
  131. $self->{'vars'}{'Arch'} = get_host_arch();
  132. $self->no_warn('Arch');
  133. }
  134. =item $newstring = $s->substvars($string)
  135. Substitutes variables in $string and return the result in $newstring.
  136. =cut
  137. sub substvars {
  138. my ($self, $v) = @_;
  139. my $lhs;
  140. my $vn;
  141. my $rhs = '';
  142. my $count = 0;
  143. while ($v =~ m/^(.*?)\$\{([-:0-9a-z]+)\}(.*)$/si) {
  144. # If we have consumed more from the leftover data, then
  145. # reset the recursive counter.
  146. $count = 0 if (length($3) < length($rhs));
  147. $count < $maxsubsts ||
  148. error(_g("too many substitutions - recursive ? - in \`%s'"), $v);
  149. $lhs = $1; $vn = $2; $rhs = $3;
  150. if (defined($self->{'vars'}{$vn})) {
  151. $v = $lhs . $self->{'vars'}{$vn} . $rhs;
  152. $self->no_warn($vn);
  153. $count++;
  154. } else {
  155. warning(_g("unknown substitution variable \${%s}"), $vn);
  156. $v = $lhs . $rhs;
  157. }
  158. }
  159. return $v;
  160. }
  161. =item $s->warn_about_unused()
  162. Issues warning about any variables that were set, but not used
  163. =cut
  164. sub warn_about_unused {
  165. my ($self) = @_;
  166. foreach my $vn (keys %{$self->{'vars'}}) {
  167. next if $self->{'used'}{$vn};
  168. # Empty substitutions variables are ignored on the basis
  169. # that they are not required in the current situation
  170. # (example: debhelper's misc:Depends in many cases)
  171. next if $self->{'vars'}{$vn} eq "";
  172. warning(_g("unused substitution variable \${%s}"), $vn);
  173. }
  174. }
  175. =back
  176. =head1 AUTHOR
  177. Raphael Hertzog <hertzog@debian.org>.
  178. =cut
  179. 1;