Conf.pm 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. # Copyright © 2009-2010 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
  14. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. package Dpkg::Conf;
  16. use strict;
  17. use warnings;
  18. our $VERSION = '1.03';
  19. use Carp;
  20. use Dpkg::Gettext;
  21. use Dpkg::ErrorHandling;
  22. use parent qw(Dpkg::Interface::Storable);
  23. use overload
  24. '@{}' => sub { return [ $_[0]->get_options() ] },
  25. fallback => 1;
  26. =encoding utf8
  27. =head1 NAME
  28. Dpkg::Conf - parse dpkg configuration files
  29. =head1 DESCRIPTION
  30. The Dpkg::Conf object can be used to read options from a configuration
  31. file. It can export an array that can then be parsed exactly like @ARGV.
  32. =head1 METHODS
  33. =over 4
  34. =item $conf = Dpkg::Conf->new(%opts)
  35. Create a new Dpkg::Conf object. Some options can be set through %opts:
  36. if allow_short evaluates to true (it defaults to false), then short
  37. options are allowed in the configuration file, they should be prepended
  38. with a single hyphen.
  39. =cut
  40. sub new {
  41. my ($this, %opts) = @_;
  42. my $class = ref($this) || $this;
  43. my $self = {
  44. options => [],
  45. allow_short => 0,
  46. };
  47. foreach my $opt (keys %opts) {
  48. $self->{$opt} = $opts{$opt};
  49. }
  50. bless $self, $class;
  51. return $self;
  52. }
  53. =item @$conf
  54. =item @options = $conf->get_options()
  55. Returns the list of options that can be parsed like @ARGV.
  56. =cut
  57. sub get_options {
  58. my $self = shift;
  59. return @{$self->{options}};
  60. }
  61. # These functions existed for a brief time, but do not mesh well with
  62. # repeated options.
  63. sub get {
  64. croak 'obsolete function, use get_options instead';
  65. }
  66. sub set {
  67. croak 'obsolete function, use get_options instead';
  68. }
  69. =item $conf->load($file)
  70. Read options from a file. Return the number of options parsed.
  71. =item $conf->load_system_config($file)
  72. Read options from a system configuration file.
  73. Return the number of options parsed.
  74. =cut
  75. sub load_system_config {
  76. my ($self, $file) = @_;
  77. return 0 unless -e "$Dpkg::CONFDIR/$file";
  78. return $self->load("$Dpkg::CONFDIR/$file");
  79. }
  80. =item $conf->load_user_config($file)
  81. Read options from a user configuration file. It will try to use the XDG
  82. directory, either $XDG_CONFIG_HOME/dpkg/ or $HOME/.config/dpkg/.
  83. Return the number of options parsed.
  84. =cut
  85. sub load_user_config {
  86. my ($self, $file) = @_;
  87. my $confdir = $ENV{XDG_CONFIG_HOME};
  88. $confdir ||= $ENV{HOME} . '/.config' if length $ENV{HOME};
  89. return 0 unless length $confdir;
  90. return 0 unless -e "$confdir/dpkg/$file";
  91. return $self->load("$confdir/dpkg/$file") if length $confdir;
  92. return 0;
  93. }
  94. =item $conf->load_config($file)
  95. Read options from system and user configuration files.
  96. Return the number of options parsed.
  97. =cut
  98. sub load_config {
  99. my ($self, $file) = @_;
  100. my $nopts = 0;
  101. $nopts += $self->load_system_config($file);
  102. $nopts += $self->load_user_config($file);
  103. return $nopts;
  104. }
  105. =item $conf->parse($fh)
  106. Parse options from a file handle. Return the number of options parsed.
  107. =cut
  108. sub parse {
  109. my ($self, $fh, $desc) = @_;
  110. my $count = 0;
  111. local $_;
  112. while (<$fh>) {
  113. chomp;
  114. s/^\s+//; # Strip leading spaces
  115. s/\s+$//; # Strip trailing spaces
  116. s/\s+=\s+/=/; # Remove spaces around the first =
  117. s/\s+/=/ unless m/=/; # First spaces becomes = if no =
  118. # Skip empty lines and comments
  119. next if /^#/ or length == 0;
  120. if (/^-[^-]/ and not $self->{allow_short}) {
  121. warning(g_('short option not allowed in %s, line %d'), $desc, $.);
  122. next;
  123. }
  124. if (/^([^=]+)(?:=(.*))?$/) {
  125. my ($name, $value) = ($1, $2);
  126. $name = "--$name" unless $name =~ /^-/;
  127. if (defined $value) {
  128. $value =~ s/^"(.*)"$/$1/ or $value =~ s/^'(.*)'$/$1/;
  129. push @{$self->{options}}, "$name=$value";
  130. } else {
  131. push @{$self->{options}}, $name;
  132. }
  133. $count++;
  134. } else {
  135. warning(g_('invalid syntax for option in %s, line %d'), $desc, $.);
  136. }
  137. }
  138. return $count;
  139. }
  140. =item $conf->filter(%opts)
  141. Filter the list of options, either removing or keeping all those that
  142. return true when &$opts{remove}($option) or &opts{keep}($option) is called.
  143. =cut
  144. sub filter {
  145. my ($self, %opts) = @_;
  146. my $remove = $opts{remove} // sub { 0 };
  147. my $keep = $opts{keep} // sub { 1 };
  148. croak 'obsolete option format_argv' if exists $opts{format_argv};
  149. @{$self->{options}} = grep { not &$remove($_) and &$keep($_) }
  150. @{$self->{options}};
  151. }
  152. =item $string = $conf->output($fh)
  153. Write the options in the given filehandle (if defined) and return a string
  154. representation of the content (that would be) written.
  155. =item "$conf"
  156. Return a string representation of the content.
  157. =item $conf->save($file)
  158. Save the options in a file.
  159. =cut
  160. sub output {
  161. my ($self, $fh) = @_;
  162. my $ret = '';
  163. foreach my $opt ($self->get_options()) {
  164. $opt =~ s/^--//;
  165. $opt =~ s/^([^=]+)=(.*)$/$1 = "$2"/;
  166. $opt .= "\n";
  167. print { $fh } $opt if defined $fh;
  168. $ret .= $opt;
  169. }
  170. return $ret;
  171. }
  172. =back
  173. =head1 CHANGES
  174. =head2 Version 1.03 (dpkg 1.18.8)
  175. Obsolete option: 'format_argv' in $conf->filter().
  176. Obsolete methods: $conf->get(), $conf->set().
  177. New methods: $conf->load_system_config(), $conf->load_system_user(),
  178. $conf->load_config().
  179. =head2 Version 1.02 (dpkg 1.18.5)
  180. New option: Accept new option 'format_argv' in $conf->filter().
  181. New methods: $conf->get(), $conf->set().
  182. =head2 Version 1.01 (dpkg 1.15.8)
  183. New method: $conf->filter()
  184. =head2 Version 1.00 (dpkg 1.15.6)
  185. Mark the module as public.
  186. =head1 AUTHOR
  187. Raphaël Hertzog <hertzog@debian.org>.
  188. =cut
  189. 1;