Substvars.pm 5.8 KB

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