Conf.pm 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. allow_short => 0,
  40. };
  41. bless $self, $class;
  42. return $self;
  43. }
  44. =item @$conf
  45. Returns the list of options to be parsed.
  46. =item $conf->load($file)
  47. Read options from a file. Return the number of options parsed.
  48. =item $conf->parse($fh)
  49. Parse options from a file handle. Return the number of options parsed.
  50. =cut
  51. sub load {
  52. my ($self, $file) = @_;
  53. open(my $fh, "<", $file) or syserr(_g("cannot read %s"), $file);
  54. my $ret = $self->parse($fh, $file);
  55. close($fh);
  56. return $ret;
  57. }
  58. sub parse {
  59. my ($self, $fh, $desc) = @_;
  60. my $count = 0;
  61. while (<$fh>) {
  62. chomp;
  63. s/^\s+//; s/\s+$//; # Strip leading/trailing spaces
  64. s/\s+=\s+/=/; # Remove spaces around the first =
  65. s/\s+/=/ unless m/=/; # First spaces becomes = if no =
  66. next if /^#/ or /^$/; # Skip empty lines and comments
  67. if (/^-[^-]/ and not $self->{'allow_short'}) {
  68. warning(_g("short option not allowed in %s, line %d"), $desc, $.);
  69. next;
  70. }
  71. if (/^([^=]+)(?:=(.*))?$/) {
  72. my ($name, $value) = ($1, $2);
  73. $name = "--$name" unless $name =~ /^-/;
  74. if (defined $value) {
  75. $value =~ s/^"(.*)"$/$1/ or $value =~ s/^'(.*)'$/$1/;
  76. push @{$self->{'options'}}, "$name=$value";
  77. } else {
  78. push @{$self->{'options'}}, $name;
  79. }
  80. $count++;
  81. } else {
  82. warning(_g("invalid syntax for option in %s, line %d"),
  83. $desc, $.);
  84. }
  85. }
  86. return $count;
  87. }
  88. =back
  89. =head1 AUTHOR
  90. Raphaël Hertzog <hertzog@debian.org>.
  91. =cut
  92. 1;