Conf.pm 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. =item get()
  62. =item set()
  63. Obsolete functions, use get_options() instead. They will croak.
  64. =cut
  65. sub get {
  66. croak 'obsolete function, use get_options instead';
  67. }
  68. sub set {
  69. croak 'obsolete function, use get_options instead';
  70. }
  71. =item $conf->load($file)
  72. Read options from a file. Return the number of options parsed.
  73. =item $conf->load_system_config($file)
  74. Read options from a system configuration file.
  75. Return the number of options parsed.
  76. =cut
  77. sub load_system_config {
  78. my ($self, $file) = @_;
  79. return 0 unless -e "$Dpkg::CONFDIR/$file";
  80. return $self->load("$Dpkg::CONFDIR/$file");
  81. }
  82. =item $conf->load_user_config($file)
  83. Read options from a user configuration file. It will try to use the XDG
  84. directory, either $XDG_CONFIG_HOME/dpkg/ or $HOME/.config/dpkg/.
  85. Return the number of options parsed.
  86. =cut
  87. sub load_user_config {
  88. my ($self, $file) = @_;
  89. my $confdir = $ENV{XDG_CONFIG_HOME};
  90. $confdir ||= $ENV{HOME} . '/.config' if length $ENV{HOME};
  91. return 0 unless length $confdir;
  92. return 0 unless -e "$confdir/dpkg/$file";
  93. return $self->load("$confdir/dpkg/$file") if length $confdir;
  94. return 0;
  95. }
  96. =item $conf->load_config($file)
  97. Read options from system and user configuration files.
  98. Return the number of options parsed.
  99. =cut
  100. sub load_config {
  101. my ($self, $file) = @_;
  102. my $nopts = 0;
  103. $nopts += $self->load_system_config($file);
  104. $nopts += $self->load_user_config($file);
  105. return $nopts;
  106. }
  107. =item $conf->parse($fh)
  108. Parse options from a file handle. When called multiple times, the parsed
  109. options are accumulated.
  110. Return the number of options parsed.
  111. =cut
  112. sub parse {
  113. my ($self, $fh, $desc) = @_;
  114. my $count = 0;
  115. local $_;
  116. while (<$fh>) {
  117. chomp;
  118. s/^\s+//; # Strip leading spaces
  119. s/\s+$//; # Strip trailing spaces
  120. s/\s+=\s+/=/; # Remove spaces around the first =
  121. s/\s+/=/ unless m/=/; # First spaces becomes = if no =
  122. # Skip empty lines and comments
  123. next if /^#/ or length == 0;
  124. if (/^-[^-]/ and not $self->{allow_short}) {
  125. warning(g_('short option not allowed in %s, line %d'), $desc, $.);
  126. next;
  127. }
  128. if (/^([^=]+)(?:=(.*))?$/) {
  129. my ($name, $value) = ($1, $2);
  130. $name = "--$name" unless $name =~ /^-/;
  131. if (defined $value) {
  132. $value =~ s/^"(.*)"$/$1/ or $value =~ s/^'(.*)'$/$1/;
  133. push @{$self->{options}}, "$name=$value";
  134. } else {
  135. push @{$self->{options}}, $name;
  136. }
  137. $count++;
  138. } else {
  139. warning(g_('invalid syntax for option in %s, line %d'), $desc, $.);
  140. }
  141. }
  142. return $count;
  143. }
  144. =item $conf->filter(%opts)
  145. Filter the list of options, either removing or keeping all those that
  146. return true when $opts{remove}->($option) or $opts{keep}->($option) is called.
  147. =cut
  148. sub filter {
  149. my ($self, %opts) = @_;
  150. my $remove = $opts{remove} // sub { 0 };
  151. my $keep = $opts{keep} // sub { 1 };
  152. croak 'obsolete option format_argv' if exists $opts{format_argv};
  153. @{$self->{options}} = grep { not $remove->($_) and $keep->($_) }
  154. @{$self->{options}};
  155. }
  156. =item $string = $conf->output($fh)
  157. Write the options in the given filehandle (if defined) and return a string
  158. representation of the content (that would be) written.
  159. =item "$conf"
  160. Return a string representation of the content.
  161. =item $conf->save($file)
  162. Save the options in a file.
  163. =cut
  164. sub output {
  165. my ($self, $fh) = @_;
  166. my $ret = '';
  167. foreach my $opt ($self->get_options()) {
  168. $opt =~ s/^--//;
  169. $opt =~ s/^([^=]+)=(.*)$/$1 = "$2"/;
  170. $opt .= "\n";
  171. print { $fh } $opt if defined $fh;
  172. $ret .= $opt;
  173. }
  174. return $ret;
  175. }
  176. =back
  177. =head1 CHANGES
  178. =head2 Version 1.03 (dpkg 1.18.8)
  179. Obsolete option: 'format_argv' in $conf->filter().
  180. Obsolete methods: $conf->get(), $conf->set().
  181. New methods: $conf->load_system_config(), $conf->load_system_user(),
  182. $conf->load_config().
  183. =head2 Version 1.02 (dpkg 1.18.5)
  184. New option: Accept new option 'format_argv' in $conf->filter().
  185. New methods: $conf->get(), $conf->set().
  186. =head2 Version 1.01 (dpkg 1.15.8)
  187. New method: $conf->filter()
  188. =head2 Version 1.00 (dpkg 1.15.6)
  189. Mark the module as public.
  190. =cut
  191. 1;