| 1 | # | ||
| 2 | # Universal::Config | ||
| 3 | # | ||
| 4 | # Maintained by Hartmut Vogler (hartmut.vogler@t-systems.com,it@guru.de) | ||
| 5 | # | ||
| 6 | # This program 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 | # This program 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 this program; if not, write to the Free Software | ||
| 18 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
| 19 | # | ||
| 20 | package Config::Universal; | ||
| 21 | use 5.005; | ||
| 22 | use strict; | ||
| 23 | |||
| 24 | |||
| 25 | =head1 NAME | ||
| 26 | |||
| 27 | Config::Universal - Universal object oriented config file reader | ||
| 28 | |||
| 29 | =begin __INTERNALS | ||
| 30 | |||
| 31 | =head1 PORTABILITY | ||
| 32 | |||
| 33 | At this time, it is only tested on Linux, but perhaps it is | ||
| 34 | writen in nativ perl, it should be no problem to use it on | ||
| 35 | other platforms. | ||
| 36 | |||
| 37 | =back | ||
| 38 | |||
| 39 | =end __INTERNALS | ||
| 40 | |||
| 41 | =head1 SYNOPSIS | ||
| 42 | |||
| 43 | use Config::Universal; | ||
| 44 | |||
| 45 | my $conf=new Config::Universal(); | ||
| 46 | $conf->ReadConfigFile("my.conf") && die ("ReadConfigFile failed"); | ||
| 47 | my $varval=$conf->GetVarValue("myvarname"); | ||
| 48 | |||
| 49 | =head1 DESCRIPTION | ||
| 50 | |||
| 51 | This module is designed to read object structured config files. | ||
| 52 | |||
| 53 | =head1 METHODS | ||
| 54 | |||
| 55 | =over 4 | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | use vars qw($VERSION); | ||
| 60 | |||
| 61 | $VERSION = '0.5'; | ||
| 62 | |||
| 63 | |||
| 64 | =item C<new>(PARAM) | ||
| 65 | |||
| 66 | Constructor allows the following parameters: | ||
| 67 | |||
| 68 | - none at this time - | ||
| 69 | |||
| 70 | =cut | ||
| 71 | |||
| 72 | sub new | ||
| 73 | { | ||
| 74 | my $type=shift; | ||
| 75 | my %param=@_; | ||
| 76 | my $self=\%param; | ||
| 77 | |||
| 78 | $self=bless($self,$type); | ||
| 79 | if (!defined($self->{'Var'})){ | ||
| 80 | $self->{'Var'}={}; | ||
| 81 | } | ||
| 82 | if (!defined($self->{'FQ'})){ | ||
| 83 | $self->{'FQ'}={}; | ||
| 84 | } | ||
| 85 | return($self); | ||
| 86 | } | ||
| 87 | |||
| 88 | =item C<ReadConfigFile>($filename) | ||
| 89 | |||
| 90 | This method reads the config file. If this fails, the method | ||
| 91 | returns a non zero value. | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | sub ReadConfigFile | ||
| 96 | { | ||
| 97 | my $self=shift; | ||
| 98 | |||
| 99 | $self->{'Var'}={}; | ||
| 100 | $self->{'FQ'}={}; | ||
| 101 | $self->{'Obj'}=0; | ||
| 102 | foreach my $filename (@_){ | ||
| 103 | my $result=$self->_ReadConfigFile($filename); | ||
| 104 | return($result) if ($result); | ||
| 105 | } | ||
| 106 | # | ||
| 107 | #Objekte im FQ Hash erfassen | ||
| 108 | # | ||
| 109 | foreach my $objtype (keys(%{$self->{Var}})){ | ||
| 110 | next if ($objtype eq '%GLOBAL%'); | ||
| 111 | foreach my $objname (keys(%{$self->{Var}->{$objtype}})){ | ||
| 112 | my $fqname=$self->{Var}->{$objtype}->{$objname}->{'$FQNAME$'}; | ||
| 113 | $self->{FQ}->{$fqname}={obj=>$self->{Var}->{$objtype}->{$objname}}; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | # | ||
| 117 | # Parent-Struktur erfassen | ||
| 118 | # | ||
| 119 | foreach my $obj (values(%{$self->{FQ}})){ | ||
| 120 | if (defined($obj->{obj}->{'$PARENT$'})){ | ||
| 121 | $obj->{parent}=$self->{FQ}->{$obj->{obj}->{'$PARENT$'}}->{obj}; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | # | ||
| 125 | # Transferieren und Entfernen der Struktur Variablen. | ||
| 126 | # | ||
| 127 | foreach my $obj (values(%{$self->{FQ}})){ | ||
| 128 | $obj->{FQNAME}=$obj->{obj}->{'$FQNAME$'}; | ||
| 129 | $obj->{TYPE}=$obj->{obj}->{'$TYPE$'}; | ||
| 130 | $obj->{NAME}=$obj->{obj}->{'$NAME$'}; | ||
| 131 | $obj->{LINE}=$obj->{obj}->{'$LINE$'}; | ||
| 132 | delete($obj->{obj}->{'$FQNAME$'}); | ||
| 133 | delete($obj->{obj}->{'$PARENT$'}); | ||
| 134 | delete($obj->{obj}->{'$NAME$'}); | ||
| 135 | delete($obj->{obj}->{'$LINE$'}); | ||
| 136 | delete($obj->{obj}->{'$TYPE$'}); | ||
| 137 | } | ||
| 138 | # | ||
| 139 | # objecte dereferenzieren | ||
| 140 | # | ||
| 141 | my %objtypes; | ||
| 142 | map({$objtypes{$_}=1} $self->GetObject()); | ||
| 143 | foreach my $obj (values(%{$self->{FQ}})){ | ||
| 144 | foreach my $v (keys(%{$obj->{obj}})){ | ||
| 145 | if (defined($objtypes{$v})){ | ||
| 146 | my @objlist=(); | ||
| 147 | my @namelist=($obj->{obj}->{$v}); | ||
| 148 | @namelist=@{$namelist[0]} if (ref($namelist[0]) eq "ARRAY"); | ||
| 149 | foreach my $name (@namelist){ | ||
| 150 | my $obj=$self->GetObject($v,$name); | ||
| 151 | if (!defined($obj)){ | ||
| 152 | printf STDERR ("ERROR: can't resolv referenz to ". | ||
| 153 | "object '$name' with type '$v'". | ||
| 154 | "at \n"); | ||
| 155 | exit(1); | ||
| 156 | } | ||
| 157 | push(@objlist,$obj); | ||
| 158 | } | ||
| 159 | $obj->{obj}->{$v}=\@objlist; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | return(0); | ||
| 165 | } | ||
| 166 | |||
| 167 | sub _ReadConfigFile | ||
| 168 | { | ||
| 169 | my $self=shift; | ||
| 170 | my $configfilename=shift; | ||
| 171 | |||
| 172 | if (open(F,"<$configfilename")){ | ||
| 173 | my $line=0; | ||
| 174 | my $buffer=""; | ||
| 175 | my $currentname=undef; | ||
| 176 | my $mode=undef; | ||
| 177 | my @param=(); | ||
| 178 | my $errormsg=undef; | ||
| 179 | my %config=(); | ||
| 180 | my %globalconfig=(); | ||
| 181 | my %cfgbuf=(); | ||
| 182 | my @curcfgbuf=\%cfgbuf; | ||
| 183 | my $objlevel=0; | ||
| 184 | while(my $l=<F>){ | ||
| 185 | next if ($l=~m/^\s*#.*$/ && $buffer eq ""); | ||
| 186 | $l=~s/\s*$//; | ||
| 187 | $line++; | ||
| 188 | $buffer.=$l; | ||
| 189 | if ($l=~m/\\$/){ | ||
| 190 | $buffer=~s/\\$//; | ||
| 191 | next; | ||
| 192 | } | ||
| 193 | $buffer=~s/\\"/\0/g; | ||
| 194 | while($buffer ne ""){ | ||
| 195 | my $vari; | ||
| 196 | if ($buffer=~m/^\s*{/){ | ||
| 197 | $buffer=~s/^\s*{//; | ||
| 198 | $self->{'Obj'}++; | ||
| 199 | my $objectname=undef; | ||
| 200 | if ($mode eq ""){ | ||
| 201 | $objectname="*:".$self->{'Obj'}; | ||
| 202 | } | ||
| 203 | elsif($mode eq "Name"){ | ||
| 204 | if ($param[0] eq "'%GLOBAL%'" || $param[0]=~m/[\s:\.\@]/){ | ||
| 205 | printf STDERR ("ERROR: invalid class '$param[0]' ". | ||
| 206 | "in $configfilename at $line\n"); | ||
| 207 | exit(1); | ||
| 208 | } | ||
| 209 | $objectname=$param[0].":".$param[0]. | ||
| 210 | sprintf("%02d",$self->{'Obj'}); | ||
| 211 | $mode=""; | ||
| 212 | } | ||
| 213 | elsif($mode eq "NameQuoteStartDataQuoteEnd"){ | ||
| 214 | if ($param[0] eq "'%GLOBAL%'" || $param[0]=~m/[\s:\.\@]/){ | ||
| 215 | printf STDERR ("ERROR: invalid class '$param[0]' ". | ||
| 216 | "in $configfilename at $line\n"); | ||
| 217 | exit(1); | ||
| 218 | } | ||
| 219 | if ($param[1]=~m/[\s\.\@:]/){ | ||
| 220 | printf STDERR ("ERROR: invalid object name '$param[1]' ". | ||
| 221 | "in $configfilename at $line\n"); | ||
| 222 | exit(1); | ||
| 223 | } | ||
| 224 | $objectname=$param[0].":".$param[1]; | ||
| 225 | $mode=""; | ||
| 226 | } | ||
| 227 | else{ | ||
| 228 | $errormsg="unexpected object open '$mode'"; | ||
| 229 | } | ||
| 230 | if (!defined($errormsg) && $objectname ne ""){ | ||
| 231 | my ($objtype,$shortname)=$objectname=~m/^(.*):([^:]+$)/; | ||
| 232 | my $obj={'$NAME$'=>$shortname}; | ||
| 233 | $obj->{'$LINE$'}="$configfilename:$line"; | ||
| 234 | $obj->{'$TYPE$'}=$objtype if ($objtype ne "*"); | ||
| 235 | my @parents=grep({defined($_->{'$TYPE$'})} @curcfgbuf); | ||
| 236 | if ($#parents!=-1){ | ||
| 237 | $obj->{'$PARENT$'}=join(".",map({$_->{'$NAME$'}} | ||
| 238 | reverse(@parents))); | ||
| 239 | } | ||
| 240 | $obj->{'$FQNAME$'}=$obj->{'$PARENT$'}; | ||
| 241 | $obj->{'$FQNAME$'}.="." if ($obj->{'$FQNAME$'} ne ""); | ||
| 242 | $obj->{'$FQNAME$'}.=$shortname; | ||
| 243 | if ($objtype ne "*" && | ||
| 244 | defined($self->{FQ}->{$obj->{'$FQNAME$'}})){ | ||
| 245 | $errormsg="fullqualivied name '$obj->{'$FQNAME$'}' ". | ||
| 246 | "already in use"; | ||
| 247 | } | ||
| 248 | else{ | ||
| 249 | if ($objtype ne "*"){ | ||
| 250 | $self->{FQ}->{$obj->{'$FQNAME$'}}=1; | ||
| 251 | } | ||
| 252 | $curcfgbuf[0]->{$objtype.":".$obj->{'$FQNAME$'}}=$obj; | ||
| 253 | unshift(@curcfgbuf,$obj); | ||
| 254 | $objlevel++; | ||
| 255 | @param=(); | ||
| 256 | } | ||
| 257 | } | ||
| 258 | } | ||
| 259 | elsif (!defined($errormsg) && | ||
| 260 | $buffer=~m/^\s*}/){ | ||
| 261 | $buffer=~s/^\s*}//; | ||
| 262 | $errormsg="unterminated command sequenz" if ($mode ne ""); | ||
| 263 | $objlevel--; | ||
| 264 | if ($objlevel<0){ | ||
| 265 | $errormsg="unexpected object close"; | ||
| 266 | } | ||
| 267 | shift(@curcfgbuf); | ||
| 268 | } | ||
| 269 | elsif (!defined($errormsg) && | ||
| 270 | $buffer=~m/^\s*"/ && ($mode=~m/Data$/)){ | ||
| 271 | $buffer=~s/^\s*"//; | ||
| 272 | $errormsg="unexpected quote" if ($mode=~m/QuoteEnd/); | ||
| 273 | if ($buffer=~/^\s*\,/){ | ||
| 274 | $buffer=~s/^\s*\,//; | ||
| 275 | $mode.="ArraySep"; | ||
| 276 | } | ||
| 277 | else{ | ||
| 278 | $mode.="QuoteEnd"; | ||
| 279 | } | ||
| 280 | } | ||
| 281 | elsif (!defined($errormsg) && | ||
| 282 | (($vari)=$buffer=~m/^([^"]*)/) && ($mode=~m/QuoteStart$/)){ | ||
| 283 | $buffer=~s/^([^"]*)//; | ||
| 284 | $vari=~s/\0/"/g; | ||
| 285 | push(@param,$vari); | ||
| 286 | $mode.="Data"; | ||
| 287 | } | ||
| 288 | elsif (!defined($errormsg) && | ||
| 289 | (($vari)=$buffer=~m/^\s*([a-zA-Z0-9_]+)/)){ | ||
| 290 | $buffer=~s/^\s*([a-zA-Z0-9_]+)//; | ||
| 291 | push(@param,$vari); | ||
| 292 | $mode.="Name"; | ||
| 293 | } | ||
| 294 | elsif (!defined($errormsg) && | ||
| 295 | $buffer=~m/^\s*=/){ | ||
| 296 | $buffer=~s/^\s*=//; | ||
| 297 | $mode.="Setvar"; | ||
| 298 | } | ||
| 299 | elsif (!defined($errormsg) && | ||
| 300 | ($buffer=~m/^\s*"/) && | ||
| 301 | (($mode=~m/Setvar$/) || ($mode=~m/ArraySep$/) || | ||
| 302 | ($mode=~m/Name$/))){ | ||
| 303 | $errormsg="unexpected \"" if (!($mode=~m/Setvar$/) && | ||
| 304 | !($mode=~m/ArraySep$/) | ||
| 305 | && $mode ne "Name"); | ||
| 306 | $buffer=~s/^\s*"//; | ||
| 307 | $mode.="QuoteStart"; | ||
| 308 | } | ||
| 309 | elsif (!defined($errormsg) && | ||
| 310 | (my ($incfile)=$buffer=~m/^\@INCLUDE\s+\"(.+)\"/)){ | ||
| 311 | $buffer=~s/^\@INCLUDE\s+\".+\"//; | ||
| 312 | if ($mode eq "" && $objlevel==0){ | ||
| 313 | my $result=$self->_ReadConfigFile($incfile); | ||
| 314 | return($result) if ($result); | ||
| 315 | } | ||
| 316 | else{ | ||
| 317 | $errormsg="\@INCLUDE not allowed in control structur"; | ||
| 318 | } | ||
| 319 | } | ||
| 320 | else{ | ||
| 321 | $errormsg="syntax error"; | ||
| 322 | } | ||
| 323 | if (defined($errormsg)){ | ||
| 324 | printf STDERR ("LINE: '$l'\n"); | ||
| 325 | printf STDERR ("ERROR: $errormsg in line $line\n"); | ||
| 326 | exit(1); | ||
| 327 | } | ||
| 328 | if ($mode eq "NameSetvarQuoteStartDataQuoteEnd" || | ||
| 329 | $mode=~m/^NameSetvarQuoteStartDataArraySep.*QuoteEnd$/){ | ||
| 330 | my $variname=shift(@param); | ||
| 331 | my $cfgwork=$curcfgbuf[0]; | ||
| 332 | $cfgwork=\%globalconfig if ($objlevel==0); | ||
| 333 | if ($#param==0 && $variname ne "alias"){ | ||
| 334 | $cfgwork->{$variname}=$param[0]; | ||
| 335 | } | ||
| 336 | else{ | ||
| 337 | $cfgwork->{$variname}=[@param]; | ||
| 338 | } | ||
| 339 | $mode=""; | ||
| 340 | @param=(); | ||
| 341 | } | ||
| 342 | } | ||
| 343 | } | ||
| 344 | MergeObjects(\%cfgbuf,\%globalconfig,$self->{Var}); | ||
| 345 | if ($objlevel!=0){ | ||
| 346 | printf STDERR ("ERROR: unexpected eof in '$configfilename' ". | ||
| 347 | "at $line\n"); | ||
| 348 | exit(1); | ||
| 349 | } | ||
| 350 | close(F); | ||
| 351 | return(0); | ||
| 352 | } | ||
| 353 | return(int($!)); | ||
| 354 | } | ||
| 355 | |||
| 356 | sub MergeObjects | ||
| 357 | { | ||
| 358 | my $src=shift; | ||
| 359 | my $globalconfig=shift; | ||
| 360 | my $dst=shift; | ||
| 361 | |||
| 362 | while(my $obj=FetchObject($src,{},undef,undef)){ | ||
| 363 | foreach my $key (keys(%{$obj})){ | ||
| 364 | my ($class,$name)=split(/:/,$key); | ||
| 365 | if ($class ne "*"){ | ||
| 366 | $dst->{$class}->{$name}=$obj->{$key}; | ||
| 367 | } | ||
| 368 | } | ||
| 369 | } | ||
| 370 | foreach my $key (keys(%{$globalconfig})){ | ||
| 371 | $dst->{'%GLOBAL%'}->{$key}=$globalconfig->{$key}; | ||
| 372 | } | ||
| 373 | } | ||
| 374 | |||
| 375 | sub FetchObject | ||
| 376 | { | ||
| 377 | my $src=shift; | ||
| 378 | my $buf=shift; | ||
| 379 | my $parent=shift; | ||
| 380 | my $name=shift; | ||
| 381 | my $hname=undef; | ||
| 382 | my %mybuf=%{$buf}; | ||
| 383 | |||
| 384 | foreach my $key (keys(%{$src})){ | ||
| 385 | if ((ref($src->{$key}) eq "HASH")){ | ||
| 386 | $hname=$key; | ||
| 387 | } | ||
| 388 | else{ | ||
| 389 | $mybuf{$key}=$src->{$key}; | ||
| 390 | } | ||
| 391 | } | ||
| 392 | if ($hname){ | ||
| 393 | my $tempobj=FetchObject($src->{$hname},\%mybuf,$src,$hname); | ||
| 394 | return($tempobj); | ||
| 395 | } | ||
| 396 | delete($parent->{$name}) if (defined($parent)); | ||
| 397 | return(undef) if (!defined($name)); | ||
| 398 | return({$name=>\%mybuf}); | ||
| 399 | } | ||
| 400 | |||
| 401 | |||
| 402 | |||
| 403 | sub FindFreeObjectName | ||
| 404 | { | ||
| 405 | my $config=shift; | ||
| 406 | my $class=shift; | ||
| 407 | my $basename=shift; | ||
| 408 | my $c=0; | ||
| 409 | my $name; | ||
| 410 | |||
| 411 | while(1){ | ||
| 412 | $name=sprintf("%s:%s%02d",$class,$basename,$c); | ||
| 413 | last if (!IsKeyInUse($config,$name)); | ||
| 414 | $c++; | ||
| 415 | } | ||
| 416 | return($name); | ||
| 417 | } | ||
| 418 | |||
| 419 | sub IsKeyInUse | ||
| 420 | { | ||
| 421 | my $cfgpoint=shift; | ||
| 422 | my $k=shift; | ||
| 423 | foreach my $chkkey (keys(%{$cfgpoint})){ | ||
| 424 | return(1) if ($chkkey eq $k); | ||
| 425 | if (ref($cfgpoint->{$chkkey}) eq "HASH"){ | ||
| 426 | return(1) if (IsKeyInUse($cfgpoint->{$chkkey},$k)); | ||
| 427 | } | ||
| 428 | } | ||
| 429 | return(0); | ||
| 430 | } | ||
| 431 | |||
| 432 | |||
| 433 | =item C<GetVarValue>() | ||
| 434 | |||
| 435 | =item C<GetVarValue>($varname) | ||
| 436 | |||
| 437 | With no $varname, the list of global variables (out of object structurs) | ||
| 438 | from configfile is returned. If the $varname is specified, the value | ||
| 439 | of the given name is returned. | ||
| 440 | |||
| 441 | =cut | ||
| 442 | |||
| 443 | sub GetVarValue | ||
| 444 | { | ||
| 445 | my $self=shift; | ||
| 446 | my $varname=shift; # if not spezified, the list of global vars ar returned | ||
| 447 | if (!defined($varname)){ | ||
| 448 | return(keys(%{$self->{Var}->{'%GLOBAL%'}})); | ||
| 449 | } | ||
| 450 | if (defined($self->{Var}->{'%GLOBAL%'}->{$varname})){ | ||
| 451 | return($self->{Var}->{'%GLOBAL%'}->{$varname}); | ||
| 452 | } | ||
| 453 | return("undef"); | ||
| 454 | } | ||
| 455 | |||
| 456 | =item C<FindParentObject>($object|$objectname) | ||
| 457 | |||
| 458 | Returns the parent object of a given object (by name or by | ||
| 459 | hash-referenz) | ||
| 460 | |||
| 461 | =cut | ||
| 462 | |||
| 463 | sub FindParentObject | ||
| 464 | { | ||
| 465 | my $self=shift; | ||
| 466 | my $obj=shift; | ||
| 467 | |||
| 468 | if (ref($obj) eq "HASH"){ | ||
| 469 | foreach my $p (values(%{$self->{FQ}})){ | ||
| 470 | return($p->{parent}) if ($p->{obj} eq $obj); | ||
| 471 | } | ||
| 472 | } | ||
| 473 | else{ | ||
| 474 | if (defined($self->{FQ}->{$obj})){ | ||
| 475 | $self->{FQ}->{$obj}->{parent}; | ||
| 476 | } | ||
| 477 | } | ||
| 478 | return(); | ||
| 479 | } | ||
| 480 | |||
| 481 | =item C<ObjectInfo>($objname|$object,'FQNAME'|'NAME'|'TYPE','LINE') | ||
| 482 | |||
| 483 | Returns the spezifed deltail information for the given object (by name or by | ||
| 484 | hash-referenz) | ||
| 485 | LINE = definition position of object in configfile | ||
| 486 | FQNAME = full qualified name of the object | ||
| 487 | NAME = the short name of object | ||
| 488 | TYPE = object type | ||
| 489 | |||
| 490 | =cut | ||
| 491 | |||
| 492 | sub ObjectInfo | ||
| 493 | { | ||
| 494 | my $self=shift; | ||
| 495 | my $obj=shift; | ||
| 496 | my $var=shift; | ||
| 497 | |||
| 498 | |||
| 499 | if (ref($obj) eq "HASH"){ | ||
| 500 | foreach my $p (values(%{$self->{FQ}})){ | ||
| 501 | return($p->{$var}) if ($p->{obj} eq $obj); | ||
| 502 | } | ||
| 503 | } | ||
| 504 | else{ | ||
| 505 | if (defined($self->{FQ}->{$obj})){ | ||
| 506 | return($self->{FQ}->{$obj}->{$var}); | ||
| 507 | } | ||
| 508 | } | ||
| 509 | return(undef); | ||
| 510 | } | ||
| 511 | |||
| 512 | |||
| 513 | =item C<FindSubordinate>($objname|$object,[TYPERESTRICTION]) | ||
| 514 | |||
| 515 | Returns all subordnate objects with the given objecttype restriction | ||
| 516 | TYPERESTRICTION. If no TYPERESTRICTION is spezified, all subordnate | ||
| 517 | are returned. If no subordnates are found, an empty array is returend. | ||
| 518 | |||
| 519 | =cut | ||
| 520 | |||
| 521 | sub FindSubordinate | ||
| 522 | { | ||
| 523 | my $self=shift; | ||
| 524 | my $obj=shift; | ||
| 525 | my @type=@_; | ||
| 526 | my $rootobj=undef; | ||
| 527 | |||
| 528 | if (ref($obj) eq "HASH"){ | ||
| 529 | foreach my $p (values(%{$self->{FQ}})){ | ||
| 530 | $rootobj=$p->{obj} if ($p->{obj} eq $obj); | ||
| 531 | } | ||
| 532 | } | ||
| 533 | else{ | ||
| 534 | if (defined($self->{FQ}->{$obj})){ | ||
| 535 | $rootobj=$self->{FQ}->{$obj}->{obj}; | ||
| 536 | } | ||
| 537 | } | ||
| 538 | return(undef) if (!defined($rootobj)); | ||
| 539 | sub getsub | ||
| 540 | { | ||
| 541 | my $sobj=shift; | ||
| 542 | my @fobj; | ||
| 543 | foreach my $p (values(%{$self->{FQ}})){ | ||
| 544 | if (defined($p->{parent}) && $p->{parent} eq $sobj){ | ||
| 545 | push(@fobj,getsub($p->{obj}),$p->{obj}); | ||
| 546 | } | ||
| 547 | } | ||
| 548 | return(@fobj); | ||
| 549 | } | ||
| 550 | return(getsub($rootobj)) if ($#type==-1); | ||
| 551 | return(grep({ my $t=$self->ObjectInfo($_,'TYPE'); | ||
| 552 | my $bk=0; | ||
| 553 | map({$bk=1 if ($t eq $_);}@type); | ||
| 554 | $bk; | ||
| 555 | } getsub($rootobj))); | ||
| 556 | } | ||
| 557 | |||
| 558 | |||
| 559 | =item C<GetObject>() | ||
| 560 | |||
| 561 | =item C<GetObject>($objecttype) | ||
| 562 | |||
| 563 | =item C<GetObject>($objecttype,$objectname) | ||
| 564 | |||
| 565 | With no paramaters, the method returns the list of available | ||
| 566 | object types in current config. | ||
| 567 | |||
| 568 | If the $objecttype is specified, the list of objectnames in the | ||
| 569 | given $objecttype is returned. | ||
| 570 | |||
| 571 | If $objecttype and $objectname is specified, the value ob the | ||
| 572 | described variable is returned. | ||
| 573 | |||
| 574 | =cut | ||
| 575 | |||
| 576 | sub GetObject | ||
| 577 | { | ||
| 578 | my $self=shift; | ||
| 579 | my $class=shift; | ||
| 580 | my $objname=shift; # if not spezified, the list of objects are returned | ||
| 581 | |||
| 582 | if (!defined($class)){ | ||
| 583 | return(grep(!/^\%GLOBAL\%$/,keys(%{$self->{Var}}))); | ||
| 584 | } | ||
| 585 | if (!defined($objname)){ | ||
| 586 | if (defined($self->{Var}->{$class})){ | ||
| 587 | return(sort(keys(%{$self->{Var}->{$class}}))); | ||
| 588 | } | ||
| 589 | else{ | ||
| 590 | return(undef); | ||
| 591 | } | ||
| 592 | } | ||
| 593 | else{ | ||
| 594 | if (defined($self->{Var}->{$class}) && | ||
| 595 | defined($self->{Var}->{$class}->{$objname})){ | ||
| 596 | return($self->{Var}->{$class}->{$objname}); | ||
| 597 | } | ||
| 598 | else{ | ||
| 599 | if (defined($self->{Var}->{$class})){ | ||
| 600 | # find alias | ||
| 601 | foreach my $objname (keys(%{$self->{Var}->{$class}})){ | ||
| 602 | if (grep(/^$objname$/,@{$self->{Var}->{$class}->{$objname}->{alias}})){ | ||
| 603 | return($self->{Var}->{$class}->{$objname}); | ||
| 604 | } | ||
| 605 | } | ||
| 606 | return(undef); | ||
| 607 | } | ||
| 608 | else{ | ||
| 609 | return(undef); | ||
| 610 | } | ||
| 611 | } | ||
| 612 | } | ||
| 613 | } | ||
| 614 | |||
| 615 | =head1 SAMPLES | ||
| 616 | |||
| 617 | General you should use only lowercase characters for variable, objecttypes | ||
| 618 | and objectnames. Of curse there are upper case characters are useable too, | ||
| 619 | but the only use of lower case creates a better readable config. | ||
| 620 | The simples kind of configuration are simple variables. | ||
| 621 | |||
| 622 | # | ||
| 623 | # remarks starting with # | ||
| 624 | # | ||
| 625 | server="myserver" | ||
| 626 | ipadress="192.168.2.2","192.168.1.2" | ||
| 627 | |||
| 628 | The next level of configuration is to create objects. A object | ||
| 629 | has always one type and sometimes a name. If no name is given, | ||
| 630 | Config::Universal create a uniq name at the given objecttype. | ||
| 631 | |||
| 632 | server "myserver" { | ||
| 633 | ipadress="192.168.2.2","192.168.1.2" | ||
| 634 | } | ||
| 635 | server{ | ||
| 636 | ipadress="164.168.2.2","164.168.1.2" | ||
| 637 | } | ||
| 638 | |||
| 639 | Config::Universal makes it posible, to use object inheritance. At the | ||
| 640 | following sample, every server object has the variable os="linux". | ||
| 641 | |||
| 642 | { | ||
| 643 | os="linux" | ||
| 644 | server "servera" { | ||
| 645 | ipadress="192.168.2.2","192.168.1.2" | ||
| 646 | } | ||
| 647 | server "serverb" { | ||
| 648 | ipadress="164.168.2.2","164.168.1.2" | ||
| 649 | } | ||
| 650 | } | ||
| 651 | |||
| 652 | It is posible to include an outher configfile, but a include directrive | ||
| 653 | ist only outsite object structures alowed. | ||
| 654 | |||
| 655 | server "myserver" { | ||
| 656 | ipadress="192.168.2.2","192.168.1.2" | ||
| 657 | } | ||
| 658 | @INCLUDE "/etc/one.other.conf" | ||
| 659 | |||
| 660 | If at one object no name is spezifed, an automatic name is generated. | ||
| 661 | If this mode is used, the name of the object can be diffrent between | ||
| 662 | each run ob ReadConfigFile()! | ||
| 663 | |||
| 664 | server "myserver" { | ||
| 665 | ipadress="192.168.2.2","192.168.1.2" | ||
| 666 | disk{ | ||
| 667 | name="/dev/hda" | ||
| 668 | } | ||
| 669 | disk{ | ||
| 670 | name="/dev/hdb" | ||
| 671 | } | ||
| 672 | } | ||
| 673 | |||
| 674 | References objects would be automaticly checked and dereferenced. The | ||
| 675 | corespondenting variable would have a array reference with the hash | ||
| 676 | references to the adressed objects. The object names must be always | ||
| 677 | spezified full qualified! | ||
| 678 | |||
| 679 | server "myserver" { | ||
| 680 | eventhandler="ev1","myserver.ev2" | ||
| 681 | eventhandler "ev2"{ | ||
| 682 | name="/etc/ev2" | ||
| 683 | } | ||
| 684 | } | ||
| 685 | eventhandler "ev1"{ | ||
| 686 | name="/etc/ev1" | ||
| 687 | } | ||
| 688 | |||
| 689 | =head1 AUTHORS | ||
| 690 | |||
| 691 | Config::Universal by Hartmut Vogler. | ||
| 692 | |||
| 693 | =head1 COPYRIGHT | ||
| 694 | |||
| 695 | The Config::Universal is Copyright (c) 2005 Hartmut Vogler. Germany. | ||
| 696 | All rights reserved. | ||
| 697 | |||
| 698 | =cut | ||
| 699 | |||
| 700 | |||
| 701 | 1; |