1 | # |
---|
2 | # Copyright (c) 2004-2007 - Consultas, PKG.fr |
---|
3 | # |
---|
4 | # This file is part of A2P. |
---|
5 | # |
---|
6 | # A2P is free software; you can redistribute it and/or modify |
---|
7 | # it under the terms of the GNU General Public License as published by |
---|
8 | # the Free Software Foundation; either version 2 of the License, or |
---|
9 | # (at your option) any later version. |
---|
10 | # |
---|
11 | # A2P is distributed in the hope that it will be useful, |
---|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | # GNU General Public License for more details. |
---|
15 | # |
---|
16 | # You should have received a copy of the GNU General Public License |
---|
17 | # along with A2P; if not, write to the Free Software |
---|
18 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
19 | # |
---|
20 | # $Id: Init.pm 3 2007-10-18 16:20:19Z guillaume $ |
---|
21 | # |
---|
22 | |
---|
23 | package A2P::Init; |
---|
24 | |
---|
25 | use strict ; |
---|
26 | use integer ; |
---|
27 | |
---|
28 | BEGIN { |
---|
29 | use Exporter (); |
---|
30 | |
---|
31 | our ( $VERSION , @ISA , @EXPORT , @EXPORT_OK ); |
---|
32 | |
---|
33 | $VERSION = sprintf "%s", q$Rev: 415 $ =~ /(\d[0-9.]+)\s+/ ; |
---|
34 | |
---|
35 | @ISA = qw(Exporter); |
---|
36 | @EXPORT = qw( &Init ); |
---|
37 | @EXPORT_OK = qw( &ResetInit ); |
---|
38 | } |
---|
39 | our $VERSION ; |
---|
40 | |
---|
41 | my @Shareds = () ; |
---|
42 | my @ConfFiles = () ; |
---|
43 | my %CONF = () ; |
---|
44 | |
---|
45 | ############################################################################# |
---|
46 | ## Init code - Also called when receiving a HUP signal ## |
---|
47 | ############################################################################# |
---|
48 | sub Init { |
---|
49 | return unless @Shareds or @_ ; |
---|
50 | |
---|
51 | # First call should initialize our ConfFiles list |
---|
52 | return @ConfFiles = @_ if ( ! @ConfFiles and @_ ); |
---|
53 | |
---|
54 | # We have to (re-)read our system conf |
---|
55 | my %Updated = () ; |
---|
56 | |
---|
57 | # Second call should initialize our SHARED and defaults list |
---|
58 | if ( ! %CONF and @_ ) { |
---|
59 | %CONF = @_ ; |
---|
60 | @Shareds = keys(%CONF); |
---|
61 | |
---|
62 | map { |
---|
63 | my ( $strippedkey ) = $_ =~ /^[\$](.*)$/ ; |
---|
64 | $Updated{$_} = $strippedkey ; |
---|
65 | } @Shareds ; |
---|
66 | } |
---|
67 | |
---|
68 | # Next calls will update our conf |
---|
69 | |
---|
70 | foreach my $ConfFile ( @ConfFiles ) { |
---|
71 | next if ( ! -e $ConfFile ); |
---|
72 | |
---|
73 | # Open the file for reading and scan each line for standard shell definition |
---|
74 | if (! open( *CONF , '<', $ConfFile )) { |
---|
75 | warn "Can't open '$ConfFile' for reading" ; |
---|
76 | next ; |
---|
77 | } |
---|
78 | |
---|
79 | my @lines = (<CONF>); |
---|
80 | close(CONF); |
---|
81 | |
---|
82 | @lines = grep { $_ !~ /^\s*#/ } @lines ; # Strip comments |
---|
83 | @lines = grep { /=/ } @lines ; # Keep only variable definition |
---|
84 | |
---|
85 | foreach my $key ( @Shareds ) { # Not optimized but rarely called |
---|
86 | my ( $strippedkey ) = $key =~ /^[\$](.*)$/ ; |
---|
87 | |
---|
88 | # There should be only one line |
---|
89 | foreach my $def ( grep { /^$strippedkey=/ } @lines ) { |
---|
90 | chomp $def ; |
---|
91 | #&debugdev("Analysing $def from $ConfFile conf..."); |
---|
92 | my ( $value ) = $def =~/^$strippedkey=\s*["']?([^"']*)["']?\s*$/; |
---|
93 | if ( ! defined($value) ) { |
---|
94 | warn "Bad configuration line found for '$strippedkey' var: '$def' in '$ConfFile' file" ; |
---|
95 | next ; |
---|
96 | } |
---|
97 | $value = $1 if ( $value =~ m|^(.*)/$| ); # Strip / from folders |
---|
98 | if ( $CONF{$key} ne $value ) { |
---|
99 | $CONF{$key} = $value ; |
---|
100 | #&debugdev("Updating $key to $value"); |
---|
101 | $Updated{$key} = $strippedkey ; |
---|
102 | } |
---|
103 | } |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | # Get vars that are eventually defined with shell expansion |
---|
108 | my @needshellexpansion = grep { $CONF{$_} =~ /[\$]/ } @Shareds ; |
---|
109 | |
---|
110 | # Does expansion if needed |
---|
111 | foreach my $var ( @needshellexpansion ) { |
---|
112 | #&debugdev("$var has shell expansion: $CONF{$var}"); |
---|
113 | my $limitrecursion = 10 ; |
---|
114 | my $temp = "" ; |
---|
115 | while ( $limitrecursion and $CONF{$var} =~ /[\$]/ ) { |
---|
116 | $limitrecursion -- ; |
---|
117 | foreach my $key ( @Shareds ) { |
---|
118 | my ( $strippedkey ) = $key =~ /^[\$](.*)$/ ; |
---|
119 | next unless $CONF{$var} =~ m|$strippedkey| ; |
---|
120 | #&debugdev("$var has shell expansion with $key=$CONF{$key}"); |
---|
121 | $CONF{$var} =~ s|[\$]$strippedkey|$CONF{$key}| ; |
---|
122 | } |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | # Returns %ENV string list to evaluate |
---|
127 | return map { |
---|
128 | my $chr = $CONF{$_} =~ /^\d+$/ ? "" : "'" ; |
---|
129 | '$ENV{' . $Updated{$_} . '} = ' . $chr . $CONF{$_} . $chr ; |
---|
130 | } keys(%Updated) ; |
---|
131 | } |
---|
132 | |
---|
133 | sub ResetInit { |
---|
134 | @Shareds = () ; |
---|
135 | @ConfFiles = () ; |
---|
136 | %CONF = () ; |
---|
137 | } |
---|
138 | |
---|
139 | 1; |
---|