summaryrefslogtreecommitdiff
path: root/drivers/dahdi/xpp/XppConfig.pm
blob: 1485a0d01887728b5d2549a5291e05e4e529a186 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package XppConfig;
#
# Written by Oron Peled <oron@actcom.co.il>
# Copyright (C) 2008, Xorcom
# This program is free software; you can redistribute and/or
# modify it under the same terms as Perl itself.
#
# $Id$
#
use strict;

my $conf_file = "/etc/dahdi/xpp.conf";

sub subst_var($$) {
	my $lookup = shift;
	my $string = shift;

	if(defined $lookup->{$string}) {
		return $lookup->{$string};
	} else {
		return $string;
	}
}

sub read_config($) {
	my $input = shift || die;
	my %xpp_config;
	my $lookup = \%xpp_config;

	open(F, $input) || die "Failed reading configuration $input: $!\n";
LINE:
	while(<F>) {
		chomp;
		s/#.*//;	# strip comments
		next unless /\S/;
		s/^\s*//;
		if(s/\\$//) {
			my $next = <F>;
			$next =~ s/^\s*//;
			$_ .= " $next";
			redo LINE;
		}
		my ($key, $value) = split(/=/, $_, 2);
		# Trim whitespace around key/value
		$key =~ s/^\s*(\S+)\s*$/$1/;
		$value =~ s/^\s*(\S+)\s*$/$1/;
		# Variable substitution
		my $new_value = $value;
		$new_value =~ s/\$(\w+)/subst_var($lookup,$1)/eg;
		$xpp_config{$key} = $new_value;
	}
	close F;
	return %xpp_config;
}

sub import {
	my $pack = shift || die "Import without package?";
	my $init_dir = shift || die "$pack::import -- missing init_dir parameter";
	my $local_conf = "$init_dir/xpp.conf";
	$conf_file = $local_conf if -r $local_conf;
	my %x = read_config($conf_file);
}

sub show_vars {
	my $assoc = shift;
	foreach (sort keys %{$assoc}) {
		print "$_\t$assoc->{$_}\n";
	}
}

sub source_vars {
	my @keys = @_;
	my %conf = read_config($conf_file);
	my %result;
	my $k;
	my $v;

	foreach (@keys) {
		if(defined $conf{$_}) {
			$result{$_} = $conf{$_};
		}
	}
	return ($conf_file, %result);
}


1;