Symbol.pm 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. # Copyright © 2007 Raphaël Hertzog <hertzog@debian.org>
  2. # Copyright © 2009 Modestas Vainius <modestas@vainius.eu>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. package Dpkg::Shlibs::Symbol;
  17. use strict;
  18. use warnings;
  19. use Dpkg::Gettext;
  20. use Dpkg::Deps;
  21. use Dpkg::ErrorHandling;
  22. use Storable qw();
  23. sub new {
  24. my $this = shift;
  25. my $class = ref($this) || $this;
  26. my %args = @_;
  27. my $self = bless {
  28. symbol => undef,
  29. symbol_templ => undef,
  30. minver => undef,
  31. dep_id => 0,
  32. deprecated => 0,
  33. tags => {},
  34. tagorder => [],
  35. }, $class;
  36. $self->{$_} = $args{$_} foreach keys %args;
  37. return $self;
  38. }
  39. # Shallow clone
  40. sub sclone {
  41. my $self = shift;
  42. my $clone = { %$self };
  43. if (@_) {
  44. my %args=@_;
  45. $clone->{$_} = $args{$_} foreach keys %args;
  46. }
  47. return bless $clone, ref $self;
  48. }
  49. # Deep clone
  50. sub dclone {
  51. my $self = shift;
  52. my $clone = Storable::dclone($self);
  53. if (@_) {
  54. my %args=@_;
  55. $clone->{$_} = $args{$_} foreach keys %args;
  56. }
  57. return $clone;
  58. }
  59. sub parse_tagspec {
  60. my ($self, $tagspec) = @_;
  61. if ($tagspec =~ /^\((.*?)\)(.*)$/ && $1) {
  62. # (tag1=t1 value|tag2|...|tagN=tNp)
  63. # Symbols ()|= cannot appear in the tag names and values
  64. my $tagspec = $1;
  65. my $rest = ($2) ? $2 : "";
  66. my @tags = split(/\|/, $tagspec);
  67. # Parse each tag
  68. for my $tag (@tags) {
  69. if ($tag =~ /^(.*)=(.*)$/) {
  70. # Tag with value
  71. $self->add_tag($1, $2);
  72. } else {
  73. # Tag without value
  74. $self->add_tag($tag, undef);
  75. }
  76. }
  77. return $rest;
  78. }
  79. return undef;
  80. }
  81. sub parse {
  82. my ($self, $symbolspec) = @_;
  83. my $symbol;
  84. my $symbol_templ;
  85. my $symbol_quoted;
  86. my $rest;
  87. if (defined($symbol = $self->parse_tagspec($symbolspec))) {
  88. # (tag1=t1 value|tag2|...|tagN=tNp)"Foo::Bar::foobar()"@Base 1.0 1
  89. # Symbols ()|= cannot appear in the tag names and values
  90. # If the tag specification exists symbol name template might be quoted too
  91. if ($symbol =~ /^(['"])/ && $symbol =~ /^($1)(.*?)$1(.*)$/) {
  92. $symbol_quoted = $1;
  93. $symbol_templ = $2;
  94. $symbol = $2;
  95. $rest = $3;
  96. } else {
  97. if ($symbol =~ m/^(\S+)(.*)$/) {
  98. $symbol_templ = $1;
  99. $symbol = $1;
  100. $rest = $2;
  101. }
  102. }
  103. error(_g("symbol name unspecified: %s"), $symbolspec) if (!$symbol);
  104. } else {
  105. # No tag specification. Symbol name is up to the first space
  106. # foobarsymbol@Base 1.0 1
  107. if ($symbolspec =~ m/^(\S+)(.*)$/) {
  108. $symbol = $1;
  109. $rest = $2;
  110. } else {
  111. return 0;
  112. }
  113. }
  114. $self->{symbol} = $symbol;
  115. $self->{symbol_templ} = $symbol_templ;
  116. $self->{symbol_quoted} = $symbol_quoted if ($symbol_quoted);
  117. # Now parse "the rest" (minver and dep_id)
  118. if ($rest =~ /^\s(\S+)(?:\s(\d+))?/) {
  119. $self->{minver} = $1;
  120. $self->{dep_id} = defined($2) ? $2 : 0;
  121. } else {
  122. return 0;
  123. }
  124. return 1;
  125. }
  126. # A hook for processing of tags which may change symbol name.
  127. # Called from Dpkg::Shlibs::SymbolFile::load(). Empty for now.
  128. sub process_tags {
  129. my $self = shift;
  130. }
  131. sub get_symbolname {
  132. return $_[0]->{symbol};
  133. }
  134. sub get_symboltempl {
  135. return $_[0]->{symbol_templ} || $_[0]->{symbol};
  136. }
  137. sub get_wildcard_version {
  138. my $self = shift;
  139. if ($self->get_symbolname() =~ /^\*@(.*)$/) {
  140. return $1;
  141. }
  142. return undef;
  143. }
  144. sub has_tags {
  145. my $self = shift;
  146. return scalar (@{$self->{tagorder}});
  147. }
  148. sub add_tag {
  149. my ($self, $tagname, $tagval) = @_;
  150. if (exists $self->{tags}{$tagname}) {
  151. $self->{tags}{$tagname} = $tagval;
  152. return 0;
  153. } else {
  154. $self->{tags}{$tagname} = $tagval;
  155. push @{$self->{tagorder}}, $tagname;
  156. }
  157. return 1;
  158. }
  159. sub delete_tag {
  160. my ($self, $tagname) = @_;
  161. if (exists $self->{tags}{$tagname}) {
  162. delete $self->{tags}{$tagname};
  163. $self->{tagorder} = [ grep { $_ ne $tagname } @{$self->{tagorder}} ];
  164. return 1;
  165. }
  166. return 0;
  167. }
  168. sub has_tag {
  169. my ($self, $tag) = @_;
  170. return exists $self->{tags}{$tag};
  171. }
  172. sub get_tag_value {
  173. my ($self, $tag) = @_;
  174. return $self->{tags}{$tag};
  175. }
  176. sub is_optional {
  177. my $self = shift;
  178. return $self->has_tag("optional");
  179. }
  180. sub is_arch_specific {
  181. my $self = shift;
  182. return $self->has_tag("arch");
  183. }
  184. sub arch_is_concerned {
  185. my ($self, $arch) = @_;
  186. my $arches = $self->{tags}{arch};
  187. if (defined $arch && defined $arches) {
  188. my $dep = Dpkg::Deps::Simple->new();
  189. my @arches = split(/[\s,]+/, $arches);
  190. $dep->{package} = "dummy";
  191. $dep->{arches} = \@arches;
  192. return $dep->arch_is_concerned($arch);
  193. }
  194. return 1;
  195. }
  196. sub get_tagspec {
  197. my ($self) = @_;
  198. if ($self->has_tags()) {
  199. my @tags;
  200. for my $tagname (@{$self->{tagorder}}) {
  201. my $tagval = $self->{tags}{$tagname};
  202. if (defined $tagval) {
  203. push @tags, $tagname . "=" . $tagval;
  204. } else {
  205. push @tags, $tagname;
  206. }
  207. }
  208. return "(". join("|", @tags) . ")";
  209. }
  210. return "";
  211. }
  212. sub get_symbolspec {
  213. my $self = shift;
  214. my $template_mode = shift;
  215. my $spec = "";
  216. $spec .= "#MISSING: $self->{deprecated}#" if $self->{deprecated};
  217. $spec .= " ";
  218. if ($template_mode && $self->has_tags()) {
  219. $spec .= sprintf('%s%3$s%s%3$s', $self->get_tagspec(),
  220. $self->get_symboltempl(), $self->{symbol_quoted} || "");
  221. } else {
  222. $spec .= $self->get_symbolname();
  223. }
  224. $spec .= " $self->{minver}";
  225. $spec .= " $self->{dep_id}" if $self->{dep_id};
  226. return $spec;
  227. }
  228. 1;