]> git.p6c8.net - devedit.git/blob - modules/Config/DevEdit.pm
- Dev-Editor is now able to forbid accessing a file using the configuration file
[devedit.git] / modules / Config / DevEdit.pm
1 package Config::DevEdit;
2
3 #
4 # Dev-Editor - Module Config::DevEdit
5 #
6 # Read and parse the configuration files
7 #
8 # Author: Patrick Canterino <patrick@patshaping.de>
9 # Last modified: 2005-06-09
10 #
11
12 use strict;
13
14 use vars qw(@EXPORT);
15 use Carp qw(croak);
16
17 use Text::ParseWords;
18
19 ### Export ###
20
21 use base qw(Exporter);
22
23 @EXPORT = qw(read_config);
24
25 # read_config()
26 #
27 # Read the configuration files of Dev-Editor
28 #
29 # Params: Path to main configuration file
30 #
31 # Return: Configuration (Hash Reference)
32
33 sub read_config($)
34 {
35 my $file = shift;
36
37 my $config = parse_config($file);
38
39 $config->{'errors'} = parse_config($config->{'error_file'});
40 $config->{'templates'} = parse_config($config->{'template_file'});
41
42 # Parse list of forbidden files
43
44 if($config->{'forbidden'})
45 {
46 my @files;
47
48 foreach my $file(parse_line('\s+',0,$config->{'forbidden'}))
49 {
50 $file =~ tr!\\!/!;
51
52 $file = '/'.$file unless($file =~ m!^/!);
53 $file =~ s!/+$!!g;
54
55 push(@files,$file);
56 }
57
58 $config->{'forbidden'} = \@files;
59 }
60
61 return $config;
62 }
63
64 # parse_config()
65 #
66 # Parse a configuration file
67 #
68 # Params: Path to configuration file
69 #
70 # Return: Configuration (Hash Reference)
71
72 sub parse_config($)
73 {
74 my $file = shift;
75 local *CF;
76
77 open(CF,'<'.$file) or croak("Open $file: $!");
78 read(CF, my $data, -s $file);
79 close(CF);
80
81 my @lines = split(/\015\012|\012|\015/,$data);
82 my $config = {};
83 my $count = 0;
84
85 foreach my $line(@lines)
86 {
87 $count++;
88
89 next if($line =~ /^\s*#/);
90 next if($line !~ /^\s*\S+\s*=.*$/);
91
92 my ($key,$value) = split(/=/,$line,2);
93
94 # Remove whitespaces at the beginning and at the end
95
96 $key =~ s/^\s+//g;
97 $key =~ s/\s+$//g;
98 $value =~ s/^\s+//g;
99 $value =~ s/\s+$//g;
100
101 croak "Configuration option '$key' defined twice in line $count of configuration file '$file'" if($config->{$key});
102
103 $config->{$key} = $value;
104 }
105
106 return $config;
107 }
108
109 # it's true, baby ;-)
110
111 1;
112
113 #
114 ### End ###

patrick-canterino.de