<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Config::DevEdit;

#
# Dev-Editor - Module Config::DevEdit
#
# Read and parse the configuration files
#
# Author:        Patrick Canterino &lt;patrick@patshaping.de&gt;
# Last modified: 2005-09-30
#

use strict;

use vars qw(@EXPORT);
use Carp qw(croak);

use Text::ParseWords;

### Export ###

use base qw(Exporter);

@EXPORT = qw(read_config);

# read_config()
#
# Read the configuration files of Dev-Editor
#
# Params: Path to main configuration file
#
# Return: Configuration (Hash Reference)

sub read_config($)
{
 my $file = shift;

 my $config = parse_config($file);

 $config-&gt;{'errors'}    = parse_config($config-&gt;{'error_file'});
 $config-&gt;{'templates'} = parse_config($config-&gt;{'template_file'});

 # Check if we have to parse the user config file

 if($ENV{'REMOTE_USER'} &amp;&amp; $config-&gt;{'userconf_file'} &amp;&amp; -f $config-&gt;{'userconf_file'})
 {
  my $userconf = parse_config($config-&gt;{'userconf_file'});

  # Parse aliases (we use references, so we won't get a memory
  # problem so soon...)

  foreach my $user(keys(%$userconf))
  {
   if(my $aliases = $userconf-&gt;{$user}-&gt;{'aliases'})
   {
    foreach my $alias(parse_line('\s+',0,$aliases))
    {
     $userconf-&gt;{$alias} = $userconf-&gt;{$user} unless($userconf-&gt;{$alias});
    }
   }
  }

  if($userconf-&gt;{$ENV{'REMOTE_USER'}})
  {
   # The current HTTP Auth user has got an individual configuration
   # Overwrite the default values

   my $new_conf = $userconf-&gt;{$ENV{'REMOTE_USER'}};

   $config-&gt;{'fileroot'}  = $new_conf-&gt;{'fileroot'}  if($new_conf-&gt;{'fileroot'});
   $config-&gt;{'httproot'}  = $new_conf-&gt;{'httproot'}  if($new_conf-&gt;{'httproot'});

   $config-&gt;{'forbidden'} = $new_conf-&gt;{'forbidden'} if(defined $new_conf-&gt;{'forbidden'});

   $config-&gt;{'user_config'} = 1;
  }
 }

 # Parse list of forbidden files

 if($config-&gt;{'forbidden'})
 {
  my @files;

  foreach my $file(parse_line('\s+',0,$config-&gt;{'forbidden'}))
  {
   $file =~ tr!\\!/!;

   $file =  '/'.$file unless($file =~ m!^/!);
   $file =~ s!/+$!!g;

   push(@files,$file);
  }

  $config-&gt;{'forbidden'} = \@files;
 }
 else
 {
  $config-&gt;{'forbidden'} = [];
 }

 return $config;
}

# parse_config()
#
# Parse a configuration file
#
# Params: Path to configuration file
#
# Return: Configuration (Hash Reference)

sub parse_config($)
{
 my $file = shift;
 local *CF;

 open(CF,'&lt;'.$file) or croak("Open $file: $!");
 read(CF, my $data, -s $file);
 close(CF);

 my @lines  = split(/\015\012|\012|\015/,$data);
 my $config = {};
 my $count  = 0;
 my $sect;

 foreach my $line(@lines)
 {
  $count++;

  next if($line =~ /^\s*#/);

  if($line =~ /^\s*\[(\S+)\]\s*$/)
  {
   # Switch to new section

   $sect = $1;
  }
  elsif($line =~ /^\s*\S+\s*=.*$/)
  {
   # A normal "key = value" line

   my ($key,$value) = split(/=/,$line,2);

   # Remove whitespaces at the beginning and at the end

   $key   =~ s/^\s+//g;
   $key   =~ s/\s+$//g;
   $value =~ s/^\s+//g;
   $value =~ s/\s+$//g;

   if($sect)
   {
    $config-&gt;{$sect} = {} if(ref($config-&gt;{$sect}) ne 'HASH');

    croak "Configuration option '$key' of section '$sect' defined twice in line $count of configuration file '$file'" if($config-&gt;{$sect}-&gt;{$key});

    $config-&gt;{$sect}-&gt;{$key} = $value;
   }
   else
   {
    croak "Configuration option '$key' defined twice in line $count of configuration file '$file'" if($config-&gt;{$key});

    $config-&gt;{$key} = $value;
   }
  }
 }

 return $config;
}

# it's true, baby ;-)

1;

#
### End ###</pre></body></html>