Conf.pm 2.8 KB

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