libdpkg: Add varbuf_add_strn()
[dpkg.git] / scripts / dpkg-parsechangelog.pl
blob939e610155db5c4b3471ee6ae8c847d7defb24ec
1 #!/usr/bin/perl
3 # dpkg-parsechangelog
5 # Copyright © 1996 Ian Jackson
6 # Copyright © 2001 Wichert Akkerman
7 # Copyright © 2006-2012 Guillem Jover <guillem@debian.org>
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <https://www.gnu.org/licenses/>.
22 use strict;
23 use warnings;
25 use Dpkg ();
26 use Dpkg::Gettext;
27 use Dpkg::Getopt;
28 use Dpkg::ErrorHandling;
29 use Dpkg::Changelog::Parse;
31 textdomain('dpkg-dev');
33 my %options;
34 my $fieldname;
36 sub version {
37 printf g_("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
39 printf g_('
40 This is free software; see the GNU General Public License version 2 or
41 later for copying conditions. There is NO warranty.
42 ');
45 sub usage {
46 printf g_(
47 'Usage: %s [<option>...]')
48 . "\n\n" . g_(
49 'Options:
50 -l, --file <changelog-file>
51 get per-version info from this file.
52 -F <changelog-format> force changelog format.
53 -S, --show-field <field> show the values for <field>.
54 -?, --help show this help message.
55 --version show the version.')
56 . "\n\n" . g_(
57 "Parser options:
58 --format <output-format>
59 set output format (defaults to 'dpkg').
60 --reverse include all changes in reverse order.
61 --all include all changes.
62 -s, --since <version> include all changes later than <version>.
63 -v <version> ditto.
64 -u, --until <version> include all changes earlier than <version>.
65 -f, --from <version> include all changes equal or later than <version>.
66 -t, --to <version> include all changes up to or equal than <version>.
67 -c, --count <number> include <number> entries from the top (or tail
68 if <number> is lower than 0).
69 -n <number> ditto.
70 -o, --offset <number> change starting point for --count, counted from
71 the top (or tail if <number> is lower than 0).
72 "), $Dpkg::PROGNAME;
75 @ARGV = normalize_options(args => \@ARGV, delim => '--');
77 while (@ARGV) {
78 last unless $ARGV[0] =~ m/^-/;
80 my $arg = shift;
82 if ($arg eq '--') {
83 last;
84 } elsif ($arg eq '-L') {
85 warning(g_('-L is obsolete; it is without effect'));
86 } elsif ($arg eq '-F') {
87 $options{changelogformat} = shift;
88 usageerr(g_('bad changelog format name'))
89 unless length $options{changelogformat} and
90 $options{changelogformat} =~ m/^([0-9a-z]+)$/;
91 } elsif ($arg eq '--format') {
92 $options{format} = shift;
93 } elsif ($arg eq '--reverse') {
94 $options{reverse} = 1;
95 } elsif ($arg eq '-l' or $arg eq '--file') {
96 $options{file} = shift;
97 usageerr(g_('missing changelog filename'))
98 unless length $options{file};
99 } elsif ($arg eq '-S' or $arg eq '--show-field') {
100 $fieldname = shift;
101 } elsif ($arg eq '-c' or $arg eq '--count' or $arg eq '-n') {
102 $options{count} = shift;
103 } elsif ($arg eq '-f' or $arg eq '--from') {
104 $options{from} = shift;
105 } elsif ($arg eq '-o' or $arg eq '--offset') {
106 $options{offset} = shift;
107 } elsif ($arg eq '-s' or $arg eq '--since' or $arg eq '-v') {
108 $options{since} = shift;
109 } elsif ($arg eq '-t' or $arg eq '--to') {
110 $options{to} = shift;
111 } elsif ($arg eq '-u' or $arg eq '--until') {
112 ## no critic (ControlStructures::ProhibitUntilBlocks)
113 $options{until} = shift;
114 ## use critic
115 } elsif ($arg eq '--all') {
116 $options{all} = undef;
117 } elsif ($arg eq '-?' or $arg eq '--help') {
118 usage(); exit(0);
119 } elsif ($arg eq '--version') {
120 version(); exit(0);
121 } else {
122 usageerr(g_("unknown option '%s'"), $arg);
125 usageerr(g_('takes no non-option arguments')) if @ARGV;
127 my $count = 0;
128 my @fields = changelog_parse(%options);
129 foreach my $f (@fields) {
130 print "\n" if $count++;
131 if ($fieldname) {
132 next if not exists $f->{$fieldname};
134 my ($first_line, @lines) = split /\n/, $f->{$fieldname};
136 my $v = '';
137 $v .= $first_line if length $first_line;
138 $v .= "\n";
139 foreach (@lines) {
140 s/\s+$//;
141 if (length == 0 or /^\.+$/) {
142 $v .= ".$_\n";
143 } else {
144 $v .= "$_\n";
147 print $v;
148 } else {
149 print $f->output();