Conf.pm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.01';
  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 exports an array that can then be parsed exactly like @ARGV.
  31. =head1 FUNCTIONS
  32. =over 4
  33. =item my $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. return @{$self->{options}};
  59. }
  60. =item $conf->load($file)
  61. Read options from a file. Return the number of options parsed.
  62. =item $conf->parse($fh)
  63. Parse options from a file handle. Return the number of options parsed.
  64. =cut
  65. sub parse {
  66. my ($self, $fh, $desc) = @_;
  67. my $count = 0;
  68. local $_;
  69. while (<$fh>) {
  70. chomp;
  71. s/^\s+//; # Strip leading spaces
  72. s/\s+$//; # Strip trailing spaces
  73. s/\s+=\s+/=/; # Remove spaces around the first =
  74. s/\s+/=/ unless m/=/; # First spaces becomes = if no =
  75. # Skip empty lines and comments
  76. next if /^#/ or length == 0;
  77. if (/^-[^-]/ and not $self->{allow_short}) {
  78. warning(g_('short option not allowed in %s, line %d'), $desc, $.);
  79. next;
  80. }
  81. if (/^([^=]+)(?:=(.*))?$/) {
  82. my ($name, $value) = ($1, $2);
  83. $name = "--$name" unless $name =~ /^-/;
  84. if (defined $value) {
  85. $value =~ s/^"(.*)"$/$1/ or $value =~ s/^'(.*)'$/$1/;
  86. push @{$self->{options}}, "$name=$value";
  87. } else {
  88. push @{$self->{options}}, $name;
  89. }
  90. $count++;
  91. } else {
  92. warning(g_('invalid syntax for option in %s, line %d'), $desc, $.);
  93. }
  94. }
  95. return $count;
  96. }
  97. =item $conf->filter(remove => $rmfunc)
  98. =item $conf->filter(keep => $keepfunc)
  99. Filter the list of options, either removing or keeping all those that
  100. return true when &$rmfunc($option) or &keepfunc($option) is called.
  101. =cut
  102. sub filter {
  103. my ($self, %opts) = @_;
  104. if (defined($opts{remove})) {
  105. @{$self->{options}} = grep { not &{$opts{remove}}($_) }
  106. @{$self->{options}};
  107. }
  108. if (defined($opts{keep})) {
  109. @{$self->{options}} = grep { &{$opts{keep}}($_) }
  110. @{$self->{options}};
  111. }
  112. }
  113. =item $string = $conf->output($fh)
  114. Write the options in the given filehandle (if defined) and return a string
  115. representation of the content (that would be) written.
  116. =item "$conf"
  117. Return a string representation of the content.
  118. =item $conf->save($file)
  119. Save the options in a file.
  120. =cut
  121. sub output {
  122. my ($self, $fh) = @_;
  123. my $ret = '';
  124. foreach my $opt ($self->get_options()) {
  125. $opt =~ s/^--//;
  126. if ($opt =~ s/^([^=]+)=/$1 = "/) {
  127. $opt .= '"';
  128. }
  129. $opt .= "\n";
  130. print { $fh } $opt if defined $fh;
  131. $ret .= $opt;
  132. }
  133. return $ret;
  134. }
  135. =back
  136. =head1 CHANGES
  137. =head2 Version 1.01
  138. New method: $conf->filter()
  139. =head2 Version 1.00
  140. Mark the module as public.
  141. =head1 AUTHOR
  142. Raphaël Hertzog <hertzog@debian.org>.
  143. =cut
  144. 1;