Control.pm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 along
  14. # with this program; if not, write to the Free Software Foundation, Inc.,
  15. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. package Dpkg::Control;
  17. use strict;
  18. use warnings;
  19. use Dpkg::Gettext;
  20. use Dpkg::ErrorHandling;
  21. use Dpkg::Fields qw(capit);
  22. use Dpkg::Control::Types;
  23. use Dpkg::Control::Hash;
  24. use base qw(Dpkg::Control::Hash Exporter);
  25. our @EXPORT = qw(parsecdata CTRL_UNKNOWN CTRL_INFO_SRC CTRL_INFO_PKG CTRL_APT_SRC
  26. CTRL_APT_PKG CTRL_PKG_SRC CTRL_PKG_DEB CTRL_FILE_CHANGES
  27. CTRL_FILE_VENDOR CTRL_FILE_STATUS CTRL_CHANGELOG);
  28. =head1 NAME
  29. Dpkg::Control - parse and manipulate official control-like information
  30. =head1 DESCRIPTION
  31. The Dpkg::Control object is a smart version of Dpkg::Control::Hash.
  32. It associates a type to the control information. That type can be
  33. used to know what fields are allowed and in what order they must be
  34. output.
  35. The types are constants that are exported by default. Here's the full
  36. list:
  37. =over 4
  38. =item CTRL_UNKNOWN
  39. This type is the default type, it indicates that the type of control
  40. information is not yet known.
  41. =item CTRL_INFO_SRC
  42. Corresponds to the first block of information in a debian/control file in
  43. a Debian source package.
  44. =item CTRL_INFO_PKG
  45. Corresponds to subsequent blocks of information in a debian/control file
  46. in a Debian source package.
  47. =item CTRL_APT_SRC
  48. Corresponds to an entry in a Sources file of an APT source package
  49. repository.
  50. =item CTRL_APT_PKG
  51. Corresponds to an entry in a Packages file of an APT binary package
  52. repository.
  53. =item CTRL_PKG_SRC
  54. Corresponds to a .dsc file of a Debian source package.
  55. =item CTRL_PKG_DEB
  56. Corresponds to the control file generated by dpkg-gencontrol
  57. (DEBIAN/control) and to the same file inside .deb packages.
  58. =item CTRL_FILE_CHANGES
  59. Corresponds to a .changes file.
  60. =item CTRL_FILE_VENDOR
  61. Corresponds to a vendor file in /etc/dpkg/origins/.
  62. =item CTRL_FILE_STATUS
  63. Corresponds to an entry in dpkg's status file (/var/lib/dpkg/status).
  64. =item CTRL_CHANGELOG
  65. Corresponds to the output of dpkg-parsechangelog.
  66. =back
  67. =head1 FUNCTIONS
  68. All the methods of Dpkg::Control::Hash are available. Those listed below
  69. are either new or overriden with a different behaviour.
  70. =over 4
  71. =item $obj = Dpkg::Control::parsecdata($input, $file, %options)
  72. $input is a filehandle, $file is the name of the file corresponding to
  73. $input. %options can contain two parameters: allow_pgp=>1 allows the parser
  74. to extrac the block of a data in a PGP-signed message (defaults to 0),
  75. and allow_duplicate=>1 ask the parser to not fail when it detects
  76. duplicate fields.
  77. The return value is a reference to a tied hash (Dpkg::Fields::Object) that
  78. can be used to access the various fields.
  79. =cut
  80. sub parsecdata {
  81. my ($input, $file, %options) = @_;
  82. $options{allow_pgp} = 0 unless exists $options{allow_pgp};
  83. $options{allow_duplicate} = 0 unless exists $options{allow_duplicate};
  84. my $paraborder = 1;
  85. my $fields = undef;
  86. my $cf = ''; # Current field
  87. my $expect_pgp_sig = 0;
  88. while (<$input>) {
  89. s/\s*\n$//;
  90. next if (m/^$/ and $paraborder);
  91. next if (m/^#/);
  92. $paraborder = 0;
  93. if (m/^(\S+?)\s*:\s*(.*)$/) {
  94. unless (defined $fields) {
  95. my %f;
  96. tie %f, "Dpkg::Fields::Object";
  97. $fields = \%f;
  98. }
  99. if (exists $fields->{$1}) {
  100. unless ($options{allow_duplicate}) {
  101. syntaxerr($file, sprintf(_g("duplicate field %s found"), capit($1)));
  102. }
  103. }
  104. $fields->{$1} = $2;
  105. $cf = $1;
  106. } elsif (m/^\s+\S/) {
  107. length($cf) || syntaxerr($file, _g("continued value line not in field"));
  108. $fields->{$cf} .= "\n$_";
  109. } elsif (m/^-----BEGIN PGP SIGNED MESSAGE/) {
  110. $expect_pgp_sig = 1;
  111. if ($options{allow_pgp}) {
  112. # Skip PGP headers
  113. while (<$input>) {
  114. last if m/^$/;
  115. }
  116. } else {
  117. syntaxerr($file, _g("PGP signature not allowed here"));
  118. }
  119. } elsif (m/^$/) {
  120. if ($expect_pgp_sig) {
  121. # Skip empty lines
  122. $_ = <$input> while defined($_) && $_ =~ /^\s*$/;
  123. length($_) ||
  124. syntaxerr($file, _g("expected PGP signature, found EOF after blank line"));
  125. s/\n$//;
  126. m/^-----BEGIN PGP SIGNATURE/ ||
  127. syntaxerr($file,
  128. sprintf(_g("expected PGP signature, found something else \`%s'"), $_));
  129. # Skip PGP signature
  130. while (<$input>) {
  131. last if m/^-----END PGP SIGNATURE/;
  132. }
  133. length($_) ||
  134. syntaxerr($file, _g("unfinished PGP signature"));
  135. }
  136. last; # Finished parsing one block
  137. } else {
  138. syntaxerr($file, _g("line with unknown format (not field-colon-value)"));
  139. }
  140. }
  141. return $fields;
  142. }
  143. =item my $c = Dpkg::Control->new(%opts)
  144. If the "type" option is given, it's used to setup default values
  145. for other options. See set_options() for more details.
  146. =cut
  147. sub new {
  148. my ($this, %opts) = @_;
  149. my $class = ref($this) || $this;
  150. my $self = Dpkg::Control::Hash->new();
  151. bless $self, $class;
  152. $self->set_options(%opts);
  153. return $self;
  154. }
  155. =item $c->set_options(%opts)
  156. Changes the value of one or more options. If the "type" option is changed,
  157. it is used first to define default values for others options. The option
  158. "allow_pgp" is set to 1 for CTRL_PKG_SRC and CTRL_FILE_CHANGES and to 0
  159. otherwise. The option "drop_empty" is set to 0 for CTRL_INFO_PKG and
  160. CTRL_INFO_SRC and to 1 otherwise. The option "name" is set to a textual
  161. description of the type of control information.
  162. The output order is also set to match the ordered list returned by
  163. Dpkg::Control::Fields::field_ordered_list($type).
  164. =cut
  165. sub set_options {
  166. my ($self, %opts) = @_;
  167. if (exists $opts{'type'}) {
  168. my $t = $opts{'type'};
  169. $$self->{'allow_pgp'} = ($t & (CTRL_PKG_SRC | CTRL_FILE_CHANGES)) ? 1 : 0;
  170. $$self->{'drop_empty'} = ($t & (CTRL_INFO_PKG | CTRL_INFO_SRC)) ? 0 : 1;
  171. if ($t == CTRL_INFO_SRC) {
  172. $$self->{'name'} = _g("general section of control info file");
  173. } elsif ($t == CTRL_INFO_PKG) {
  174. $$self->{'name'} = _g("package's section of control info file");
  175. } elsif ($t == CTRL_CHANGELOG) {
  176. $$self->{'name'} = _g("parsed version of changelog");
  177. } elsif ($t == CTRL_APT_SRC) {
  178. $$self->{'name'} = sprintf(_g("entry of APT's %s file"), "Sources");
  179. } elsif ($t == CTRL_APT_PKG) {
  180. $$self->{'name'} = sprintf(_g("entry of APT's %s file"), "Packages");
  181. } elsif ($t == CTRL_PKG_SRC) {
  182. $$self->{'name'} = sprintf(_g("%s file"), ".dsc");
  183. } elsif ($t == CTRL_PKG_DEB) {
  184. $$self->{'name'} = _g("control info of a .deb package");
  185. } elsif ($t == CTRL_FILE_CHANGES) {
  186. $$self->{'name'} = sprintf(_g("%s file"), ".changes");
  187. } elsif ($t == CTRL_FILE_VENDOR) {
  188. $$self->{'name'} = _g("vendor file");
  189. } elsif ($t == CTRL_FILE_STATUS) {
  190. $$self->{'name'} = _g("entry in dpkg's status file");
  191. }
  192. }
  193. # Options set by the user override default values
  194. $$self->{$_} = $opts{$_} foreach keys %opts;
  195. }
  196. =item $c->get_type()
  197. Returns the type of control information stored. See the type parameter
  198. set during new().
  199. =cut
  200. sub get_type {
  201. my ($self) = @_;
  202. return $$self->{'type'};
  203. }
  204. =back
  205. =head1 AUTHOR
  206. Raphaël Hertzog <hertzog@debian.org>.
  207. =cut
  208. 1;