Symbol.pm 5.5 KB

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