Fields.pm 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package Dpkg::Fields;
  2. use strict;
  3. use warnings;
  4. use base qw(Exporter);
  5. use Dpkg::Gettext;
  6. use Dpkg::ErrorHandling;
  7. use Dpkg::Deps qw(@src_dep_fields @pkg_dep_fields);
  8. our @EXPORT_OK = qw(capit unknown %control_src_fields %control_pkg_fields
  9. $control_src_field_regex $control_pkg_field_regex);
  10. our %EXPORT_TAGS = ('list' => [qw(%control_src_fields %control_pkg_fields
  11. $control_src_field_regex $control_pkg_field_regex)]);
  12. # Some variables (list of fields)
  13. our %control_src_fields;
  14. our %control_pkg_fields;
  15. $control_src_fields{$_} = 1 foreach (qw(Bugs Dm-Upload-Allowed
  16. Homepage Origin Maintainer Priority Section Source Standards-Version
  17. Uploaders Vcs-Browser Vcs-Arch Vcs-Bzr Vcs-Cvs Vcs-Darcs Vcs-Git Vcs-Hg
  18. Vcs-Mtn Vcs-Svn));
  19. $control_src_fields{$_} = 1 foreach (@src_dep_fields);
  20. $control_pkg_fields{$_} = 1 foreach (qw(Architecture Bugs Description Essential
  21. Homepage Installer-Menu-Item Kernel-Version Package Package-Type
  22. Priority Section Subarchitecture Tag));
  23. $control_pkg_fields{$_} = 1 foreach (@pkg_dep_fields);
  24. our $control_src_field_regex = "(?:" . join("|", keys %control_src_fields) . ")";
  25. our $control_pkg_field_regex = "(?:" . join("|", keys %control_pkg_fields) . ")";
  26. # Some functions
  27. sub capit {
  28. my @pieces = map { ucfirst(lc) } split /-/, $_[0];
  29. return join '-', @pieces;
  30. }
  31. sub unknown($$)
  32. {
  33. my ($field, $desc) = @_;
  34. warning(_g("unknown information field '%s' in input data in %s"),
  35. $field, $desc);
  36. }
  37. package Dpkg::Fields::Object;
  38. =head1 OTHER OBJECTS
  39. =head2 Dpkg::Fields::Object
  40. This object is used to tie a hash. It implements hash-like functions by
  41. normalizing the name of fields received in keys (using
  42. Dpkg::Fields::capit). It also stores the order in which fields have been
  43. added in order to be able to dump them in the same order.
  44. =cut
  45. use Tie::Hash;
  46. our @ISA = qw(Tie::ExtraHash Tie::Hash);
  47. use Dpkg::ErrorHandling;
  48. use Dpkg::Gettext;
  49. # Import capit
  50. Dpkg::Fields->import('capit');
  51. # $self->[0] is the real hash
  52. # $self->[1] is an array containing the ordered list of keys
  53. # $self->[2] is an hash describing the relative importance of each field
  54. # (used to sort the output).
  55. =head2 Dpkg::Fields::Object->new()
  56. Return a reference to a tied hash implementing storage of simple
  57. "field: value" mapping as used in many Debian-specific files.
  58. =cut
  59. sub new {
  60. my $hash = {};
  61. tie %{$hash}, 'Dpkg::Fields::Object';
  62. return $hash;
  63. }
  64. sub TIEHASH {
  65. my $class = shift;
  66. return bless [{}, [], {}], $class;
  67. }
  68. sub FETCH {
  69. my ($self, $key) = @_;
  70. $key = capit($key);
  71. return $self->[0]->{$key} if exists $self->[0]->{$key};
  72. return undef;
  73. }
  74. sub STORE {
  75. my ($self, $key, $value) = @_;
  76. $key = capit($key);
  77. if (not exists $self->[0]->{$key}) {
  78. push @{$self->[1]}, $key;
  79. }
  80. $self->[0]->{$key} = $value;
  81. }
  82. sub EXISTS {
  83. my ($self, $key) = @_;
  84. $key = capit($key);
  85. return exists $self->[0]->{$key};
  86. }
  87. sub DELETE {
  88. my ($self, $key) = @_;
  89. $key = capit($key);
  90. if (exists $self->[0]->{$key}) {
  91. delete $self->[0]->{$key};
  92. @{$self->[1]} = grep { $_ ne $key } @{$self->[1]};
  93. return 1;
  94. } else {
  95. return 0;
  96. }
  97. }
  98. sub FIRSTKEY {
  99. my $self = shift;
  100. foreach (@{$self->[1]}) {
  101. return $_ if exists $self->[0]->{$_};
  102. }
  103. }
  104. sub NEXTKEY {
  105. my ($self, $last) = @_;
  106. my $found = 0;
  107. foreach (@{$self->[1]}) {
  108. if ($found) {
  109. return $_ if exists $self->[0]->{$_};
  110. } else {
  111. $found = 1 if $_ eq $last;
  112. }
  113. }
  114. return undef;
  115. }
  116. =head2 tied(%hash)->find_custom_field($name)
  117. Scan the fields and look for a user specific field whose name matches the
  118. following regex: /X[SBC]+-$name/i. Return the name of the field found or
  119. undef if nothing has been found.
  120. =cut
  121. sub find_custom_field {
  122. my ($self, $name) = @_;
  123. foreach my $key (keys %{$self->[0]}) {
  124. return $key if $key =~ /^X[SBC]*-\Q$name\E$/i;
  125. }
  126. return undef;
  127. }
  128. =head2 tied(%hash)->get_custom_field($name)
  129. Identify a user field and retrieve its value.
  130. =cut
  131. sub get_custom_field {
  132. my ($self, $name) = @_;
  133. my $key = $self->find_custom_field($name);
  134. return $self->[0]->{$key} if defined $key;
  135. return undef;
  136. }
  137. =head2 my $str = tied(%hash)->dump()
  138. =head2 tied(%hash)->dump($fh)
  139. Dump the raw content of the hash either as a string or to a filehandle.
  140. =cut
  141. sub dump {
  142. my ($self, $fh) = @_;
  143. my $str = "";
  144. foreach (@{$self->[1]}) {
  145. if (exists $self->[0]->{$_}) {
  146. print $fh "$_: " . $self->[0]->{$_} . "\n" if $fh;
  147. $str .= "$_: " . $self->[0]->{$_} . "\n" if defined wantarray;
  148. }
  149. }
  150. return $str;
  151. }
  152. =head2 tied(%hash)->set_field_importance(@fields)
  153. Define the order in which fields will be displayed in the output() method.
  154. =cut
  155. sub set_field_importance {
  156. my ($self, @fields) = @_;
  157. my $i = 1;
  158. $self->[2] = {};
  159. $self->[2]{$_} = $i++ foreach (@fields);
  160. }
  161. =head2 tied(%hash)->output($fh, $substvars)
  162. If $fh is defined, print the fields on the $fh filehandle after
  163. substitution of variables defined in the Dpkg::Substvars object.
  164. Also returns the string of what would printed on the filehandle.
  165. =cut
  166. sub output {
  167. my ($self, $fh, $substvars) = @_;
  168. my $str = "";
  169. my $imp = $self->[2]; # Hash of relative importance
  170. # Add substvars to refer to other fields
  171. if (defined($substvars)) {
  172. foreach my $f (keys %{$self->[0]}) {
  173. $substvars->set("F:$f", $self->[0]->{$f});
  174. $substvars->no_warn("F:$f");
  175. }
  176. }
  177. my @keys = sort {
  178. if (defined $imp->{$a} && defined $imp->{$b}) {
  179. $imp->{$a} <=> $imp->{$b};
  180. } elsif (defined($imp->{$a})) {
  181. -1;
  182. } elsif (defined($imp->{$b})) {
  183. 1;
  184. } else {
  185. $a cmp $b;
  186. }
  187. } keys %{$self->[0]};
  188. foreach my $f (@keys) {
  189. my $v = $self->[0]->{$f};
  190. if (defined($substvars)) {
  191. $v = $substvars->substvars($v);
  192. }
  193. $v =~ m/\S/ || next; # delete whitespace-only fields
  194. $v =~ m/\n\S/ &&
  195. internerr("field %s has newline then non whitespace >%s<",
  196. $f, $v);
  197. $v =~ m/\n[ \t]*\n/ &&
  198. internerr("field %s has blank lines >%s<", $f, $v);
  199. $v =~ m/\n$/ &&
  200. internerr("field %s has trailing newline >%s<", $f, $v);
  201. if (defined($substvars)) {
  202. $v =~ s/,[\s,]*,/,/g;
  203. $v =~ s/^\s*,\s*//;
  204. $v =~ s/\s*,\s*$//;
  205. }
  206. $v =~ s/\$\{\}/\$/g;
  207. if ($fh) {
  208. print $fh "$f: $v\n" || syserr(_g("write error on control data"));
  209. }
  210. if (defined wantarray) {
  211. $str .= "$f: $v\n";
  212. }
  213. }
  214. return $str;
  215. }
  216. 1;