From 593c18982ce7d0c2a65b1442ecd54c759956dc48 Mon Sep 17 00:00:00 2001 From: Jamie Cameron Date: Fri, 27 Feb 2009 07:37:38 +0000 Subject: [PATCH] Auto-detect and load API files --- web-lib-funcs.pl | 48 ++++++++++++++++++++++++++++++++++-------------- xmlrpc.cgi | 18 +++++------------- 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/web-lib-funcs.pl b/web-lib-funcs.pl index 3eda01129..ea97186fe 100755 --- a/web-lib-funcs.pl +++ b/web-lib-funcs.pl @@ -2890,14 +2890,14 @@ if ($main::licence_module) { return 1; } -=head2 foreign_require(module, file, [package]) +=head2 foreign_require(module, [file], [package]) Brings in functions from another module, and places them in the Perl namespace with the same name as the module. The parameters are : =item module - The source module's directory name, like sendmail. -=item file - The API file in that module, like sendmail-lib.pl. +=item file - The API file in that module, like sendmail-lib.pl. If missing, all API files are loaded. =item package - Perl package to place the module's functions and global variables in. @@ -2907,24 +2907,44 @@ the package name. =cut sub foreign_require { -my $pkg = $_[2] || $_[0] || "global"; +my ($mod, $file, $pkg) = @_; +$pkg ||= $mod || "global"; $pkg =~ s/[^A-Za-z0-9]/_/g; -return 1 if ($main::done_foreign_require{$pkg,$_[1]}++); +my @files; +if ($file) { + push(@files, $file); + } +else { + # Auto-detect files + my %minfo = &get_module_info($mod); + if ($minfo{'library'}) { + @files = split(/\s+/, $minfo{'library'}); + } + else { + @files = ( $mod."-lib.pl" ); + } + } +my @files = grep { !$main::done_foreign_require{$pkg,$_} } @files; +return 1 if (!@files); +foreach my $f (@files) { + $main::done_foreign_require{$pkg,$f}++; + } my @OLDINC = @INC; -my $mdir = &module_root_directory($_[0]); +my $mdir = &module_root_directory($mod); @INC = &unique($mdir, @INC); --d $mdir || &error("module $_[0] does not exist"); -if (!&get_module_name() && $_[0]) { +-d $mdir || &error("Module $mod does not exist"); +if (!&get_module_name() && $mod) { chdir($mdir); } my $old_fmn = $ENV{'FOREIGN_MODULE_NAME'}; my $old_frd = $ENV{'FOREIGN_ROOT_DIRECTORY'}; -eval <$@"); } +if ($@) { &error("Require $mod/$files[0] failed :
$@
"); } return 1; } diff --git a/xmlrpc.cgi b/xmlrpc.cgi index 952409c4f..f4652f6b6 100755 --- a/xmlrpc.cgi +++ b/xmlrpc.cgi @@ -99,19 +99,11 @@ foreach my $mc (&find_xmls("methodCall", $xml)) { &error_exit(5, "Webmin module $mod does not exist"); } - my %minfo = &get_module_info($mod); - my @libs = split(/\s+/, $minfo{'library'}); - if (!@libs) { - push(@libs, "$mod-lib.pl"); - } - foreach my $lib (@libs) { - eval { &foreign_require($mod, $lib); }; - if ($@) { - $xmlrv .= &make_error_xml(6, - "Failed to load library ". - "$mod/$lib : $@"); - last; - } + eval { &foreign_require($mod, $lib); }; + if ($@) { + $xmlrv .= &make_error_xml(6, + "Failed to load module $mod : $@"); + last; } } }