Conf.pm 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # Copyright © 2009 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 <http://www.gnu.org/licenses/>.
  15. package Dpkg::Conf;
  16. use strict;
  17. use warnings;
  18. use Dpkg::Gettext;
  19. use Dpkg::ErrorHandling;
  20. use overload
  21. '@{}' => sub { $_[0]->{'options'} },
  22. '""' => sub { join(" ", @{$_[0]->{'options'}}) },
  23. fallback => 1;
  24. =head1 NAME
  25. Dpkg::Conf - parse dpkg configuration files
  26. =head1 DESCRIPTION
  27. The Dpkg::Conf object can be used to read options from a configuration
  28. file. It can exports an array that can then be parsed exactly like @ARGV.
  29. =head1 FUNCTIONS
  30. =over 4
  31. =item my $conf = Dpkg::Conf->new()
  32. Create a new Dpkg::Conf object.
  33. =cut
  34. sub new {
  35. my ($this, %opts) = @_;
  36. my $class = ref($this) || $this;
  37. my $self = {
  38. options => [],
  39. };
  40. bless $self, $class;
  41. return $self;
  42. }
  43. =item @$conf
  44. Returns the list of options to be parsed.
  45. =item $conf->load($file)
  46. Read options from a file. Return the number of options parsed.
  47. =item $conf->parse($fh)
  48. Parse options from a file handle. Return the number of options parsed.
  49. =cut
  50. sub load {
  51. my ($self, $file) = @_;
  52. open(my $fh, "<", $file) or syserr(_g("cannot read %s"), $file);
  53. my $ret = $self->parse($fh, $file);
  54. close($fh);
  55. return $ret;
  56. }
  57. sub parse {
  58. my ($self, $fh, $desc) = @_;
  59. my $count = 0;
  60. while (<$fh>) {
  61. chomp;
  62. s/^\s+//; s/\s+$//; # Strip leading/trailing spaces
  63. s/\s+=\s+/=/; # Remove spaces around the first =
  64. s/\s+/=/ unless m/=/; # First spaces becomes = if no =
  65. next if /^#/ or /^$/; # Skip empty lines and comments
  66. if (/^(\S+)(?:=(.*))?$/) {
  67. my ($name, $value) = ($1, $2);
  68. $name = "--$name" unless $name =~ /^-/;
  69. if (defined $value) {
  70. $value =~ s/^"(.*)"$/$1/ or $value =~ s/^'(.*)'$/$1/;
  71. push @{$self->{'options'}}, "$name=$value";
  72. } else {
  73. push @{$self->{'options'}}, $name;
  74. }
  75. $count++;
  76. } else {
  77. warning(_g("invalid syntax for option in %s, line %d"),
  78. $desc, $.);
  79. }
  80. }
  81. return $count;
  82. }
  83. =back
  84. =head1 AUTHOR
  85. Raphaël Hertzog <hertzog@debian.org>.
  86. =cut
  87. 1;