1 # Copyright © 2009-2010 Raphaël Hertzog <hertzog@debian.org>
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.
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.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <https://www.gnu.org/licenses/>.
20 Dpkg::Conf - parse dpkg configuration files
24 The Dpkg::Conf object can be used to read options from a configuration
25 file. It can export an array that can then be parsed exactly like @ARGV.
29 package Dpkg
::Conf
1.04;
37 use Dpkg
::ErrorHandling
;
39 use parent
qw(Dpkg::Interface::Storable);
42 '@{}' => sub { return [ $_[0]->get_options() ] },
49 =item $conf = Dpkg::Conf->new(%opts)
51 Create a new Dpkg::Conf object. Some options can be set through %opts:
52 if allow_short evaluates to true (it defaults to false), then short
53 options are allowed in the configuration file, they should be prepended
59 my ($this, %opts) = @_;
60 my $class = ref($this) || $this;
66 foreach my $opt (keys %opts) {
67 $self->{$opt} = $opts{$opt};
76 =item @options = $conf->get_options()
78 Returns the list of options that can be parsed like @ARGV.
85 return @
{$self->{options
}};
88 =item $conf->load($file)
90 Read options from a file. Return the number of options parsed.
92 =item $conf->load_system_config($file)
94 Read options from a system configuration file.
96 Return the number of options parsed.
100 sub load_system_config
{
101 my ($self, $file) = @_;
103 return 0 unless -e
"$Dpkg::CONFDIR/$file";
104 return $self->load("$Dpkg::CONFDIR/$file");
107 =item $conf->load_user_config($file)
109 Read options from a user configuration file. It will try to use the XDG
110 directory, either $XDG_CONFIG_HOME/dpkg/ or $HOME/.config/dpkg/.
112 Return the number of options parsed.
116 sub load_user_config
{
117 my ($self, $file) = @_;
119 my $confdir = $ENV{XDG_CONFIG_HOME
};
120 $confdir ||= $ENV{HOME
} . '/.config' if length $ENV{HOME
};
122 return 0 unless length $confdir;
123 return 0 unless -e
"$confdir/dpkg/$file";
124 return $self->load("$confdir/dpkg/$file") if length $confdir;
128 =item $conf->load_config($file)
130 Read options from system and user configuration files.
132 Return the number of options parsed.
137 my ($self, $file) = @_;
141 $nopts += $self->load_system_config($file);
142 $nopts += $self->load_user_config($file);
147 =item $conf->parse($fh)
149 Parse options from a file handle. When called multiple times, the parsed
150 options are accumulated.
152 Return the number of options parsed.
157 my ($self, $fh, $desc) = @_;
163 s/^\s+//; # Strip leading spaces
164 s/\s+$//; # Strip trailing spaces
165 s/\s+=\s+/=/; # Remove spaces around the first =
166 s/\s+/=/ unless m/=/; # First spaces becomes = if no =
167 # Skip empty lines and comments
168 next if /^#/ or length == 0;
169 if (/^-[^-]/ and not $self->{allow_short
}) {
170 warning
(g_
('short option not allowed in %s, line %d'), $desc, $.);
173 if (/^([^=]+)(?:=(.*))?$/) {
174 my ($name, $value) = ($1, $2);
175 $name = "--$name" unless $name =~ /^-/;
176 if (defined $value) {
177 $value =~ s/^"(.*)"$/$1/ or $value =~ s/^'(.*)'$/$1/;
178 push @
{$self->{options
}}, "$name=$value";
180 push @
{$self->{options
}}, $name;
184 warning
(g_
('invalid syntax for option in %s, line %d'), $desc, $.);
190 =item $conf->filter(%opts)
192 Filter the list of options, either removing or keeping all those that
193 return true when $opts{remove}->($option) or $opts{keep}->($option) is called.
198 my ($self, %opts) = @_;
199 my $remove = $opts{remove
} // sub { 0 };
200 my $keep = $opts{keep
} // sub { 1 };
202 @
{$self->{options
}} = grep { not $remove->($_) and $keep->($_) }
206 =item $string = $conf->output([$fh])
208 Write the options in the given filehandle (if defined) and return a string
209 representation of the content (that would be) written.
213 Return a string representation of the content.
218 my ($self, $fh) = @_;
220 foreach my $opt ($self->get_options()) {
222 $opt =~ s/^([^=]+)=(.*)$/$1 = "$2"/;
224 print { $fh } $opt if defined $fh;
230 =item $conf->save($file)
232 Save the options in a file.
238 =head2 Version 1.04 (dpkg 1.20.0)
240 Remove croak: For 'format_argv' in $conf->filter().
242 Remove methods: $conf->get(), $conf->set().
244 =head2 Version 1.03 (dpkg 1.18.8)
246 Obsolete option: 'format_argv' in $conf->filter().
248 Obsolete methods: $conf->get(), $conf->set().
250 New methods: $conf->load_system_config(), $conf->load_system_user(),
251 $conf->load_config().
253 =head2 Version 1.02 (dpkg 1.18.5)
255 New option: Accept new option 'format_argv' in $conf->filter().
257 New methods: $conf->get(), $conf->set().
259 =head2 Version 1.01 (dpkg 1.15.8)
261 New method: $conf->filter()
263 =head2 Version 1.00 (dpkg 1.15.6)
265 Mark the module as public.