Conf.pm 5.3 KB

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