jl165 merging heads
[LibreOffice.git] / testgraphical / source / CallExternals.pm
bloba0a3b1ae716a2c788775c7faa8166004d5216387
1 package CallExternals;
3 #*************************************************************************
5 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7 # Copyright 2000, 2010 Oracle and/or its affiliates.
9 # OpenOffice.org - a multi-platform office productivity suite
11 # This file is part of OpenOffice.org.
13 # OpenOffice.org is free software: you can redistribute it and/or modify
14 # it under the terms of the GNU Lesser General Public License version 3
15 # only, as published by the Free Software Foundation.
17 # OpenOffice.org is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU Lesser General Public License version 3 for more details
21 # (a copy is included in the LICENSE file that accompanied this code).
23 # You should have received a copy of the GNU Lesser General Public License
24 # version 3 along with OpenOffice.org. If not, see
25 # <http://www.openoffice.org/license.html>
26 # for a copy of the LGPLv3 License.
28 #*************************************************************************
30 use English;
31 use warnings;
32 use strict;
33 use loghelper;
35 BEGIN {
36 use Exporter ();
37 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
39 $VERSION = 1.00;
40 # if using RCS/CVS, this may be preferred
41 $VERSION = do { my @r = (q$Revision: 1.29 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; # must be all one line, for MakeMaker
42 @ISA = qw(Exporter);
43 @EXPORT = qw(&callphp &getPHPExecutable &ExecSQL &callperl &getPerlExecutable &calljava &setJavaExecutable &getJavaExecutable &setToolsPath &quote &quoteIfNeed &set_logfile &close_logfile );
44 %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
45 # your exported package globals go here,
46 # as well as any optionally exported functions
47 @EXPORT_OK = ( ); # qw($Var1 %Hashit &func3);
50 # ------------------------------------------------------------------------------
51 # small helper, which replaces the return code
52 sub errorAdaption($)
54 my $error = shift;
55 if ($error != 0)
57 $error = $error / 256;
59 if ($error > 127)
61 $error = $error - 256;
63 return $error;
65 # ------------------------------------------------------------------------------
66 # helper to call external php with popen
67 sub callphp($$$)
69 local *IN_FILE;
70 my $phpexe = shift;
71 my $phpprogram = shift;
72 my $sParams = shift;
73 my $line;
74 my $error;
75 my @result;
77 # print "Will send: $phpexe $sParams\n";
78 # log_print("CALLPHP: $phpexe $phpprogram $sParams\n");
79 # if (open(IN_FILE, "$phpexe $sParams 2>&1 |"))
80 if (open(IN_FILE, "$phpexe $phpprogram $sParams |"))
82 while ($line = <IN_FILE>)
84 chomp($line);
85 # $line .= " ";
86 push(@result, $line);
87 # print "callphp output: $line\n";
89 close(IN_FILE);
90 $error = errorAdaption($?);
92 else
94 print "callphp(): Can't popen '$phpexe' with parameter: '$sParams'\n";
95 $error = 1;
97 return $error, @result;
100 # ------------------------------------------------------------------------------
101 sub getPHPExecutable()
103 my $phpexe;
104 if ($OSNAME eq "solaris")
106 $phpexe = "php5";
108 elsif ($OSNAME eq "linux")
110 if ( -e "/usr/bin/php5") # Suse :-(
112 $phpexe = "php5";
114 elsif ( -e "/usr/bin/php") # Gentoo
116 $phpexe = "php";
118 else
120 print "getPHPExecutable(): no php exec found.\n";
123 elsif ( $OSNAME eq "MSWin32" )
125 $phpexe = "C:/programme/php/php.exe";
126 # add second try (xampp)
127 if (! -e $phpexe)
129 $phpexe = "C:/xampp/php/php.exe";
132 elsif ( $OSNAME eq "cygwin" )
134 $phpexe = "/cygdrive/c/programme/php/php";
136 else
138 print "getPHPExecutable(): unknown environment. ($OSNAME)\n";
140 if (! $phpexe)
142 print "getPHPExecutable(): ERROR: php executable not found.\n";
143 exit(1);
145 return $phpexe;
147 # ------------------------------------------------------------------------------
148 # helper to call external java with popen
149 sub calljava($$$)
151 local *IN_FILE;
152 my $javaexe = shift;
153 my $sParams = shift;
154 my $sDebug = shift;
155 my $line;
156 my $error = 1;
158 if (! $javaexe)
160 log_print("ERROR: javaexe not set.\n");
161 return;
163 if (! $sDebug)
165 $sDebug = "";
167 $javaexe = quoteIfNeed($javaexe);
168 log_print ("CALLJAVA: $javaexe $sDebug $sParams\n");
169 if (open(IN_FILE, "$javaexe $sDebug $sParams 2>&1 |"))
171 while ($line = <IN_FILE>)
173 chomp($line);
174 log_print ("- $line\n");
176 close(IN_FILE);
177 $error = errorAdaption($?);
179 else
181 log_print ("calljava(): Can't popen '$javaexe' with parameter '$sParams'\n");
182 $error = 1;
184 return $error;
187 # ------------------------------------------------------------------------------
188 sub getPerlExecutable()
190 my $perlexe;
191 if ( $ENV{PERL} )
193 $perlexe = $ENV{PERL};
195 elsif ( $ENV{PERLEXE} )
197 $perlexe = $ENV{PERLEXE};
199 else
201 if ($OSNAME eq "MSWin32")
203 $perlexe="C:/xampp/perl/bin/perl.exe";
204 if (! -e $perlexe)
206 $perlexe="r:/btw/perl/bin/perl";
208 if (! -e $perlexe)
210 $perlexe="C:/Programme/Perl/bin/perl.exe";
213 elsif ($OSNAME eq "cygwin")
215 $perlexe = "perl";
217 elsif ($OSNAME eq "solaris")
219 $perlexe="/so/env/bt_solaris_intel/bin/perl";
221 elsif ($OSNAME eq "linux")
223 $perlexe="/so/env/bt_linux_libc2.32/DEV300/bin/perl";
225 else
227 log_print "WARNING: Use only the fallback of perl executable.\n";
228 $perlexe = "perl"; # FALLBACK
231 if ( ! -e $perlexe)
233 log_print "getPerlExecutable(): There exist no perl executable.\n";
234 exit(1);
236 return $perlexe;
238 # ------------------------------------------------------------------------------
239 # helper to call external perl with popen
240 sub callperl($$$)
242 local *IN_FILE;
243 my $perlexe = shift;
244 my $perlprogram = shift;
245 my $sParams = shift;
246 my $line;
247 my $error;
249 log_print("CALLPERL: $perlexe $perlprogram $sParams\n");
250 # if (open(IN_FILE, "$perlexe $sParams 2>&1 |"))
251 if (open(IN_FILE, "$perlexe $perlprogram $sParams |"))
253 while ($line = <IN_FILE>)
255 chomp($line);
256 log_print ("- $line\n");
258 close(IN_FILE);
259 $error = errorAdaption($?);
261 else
263 log_print ("Can't popen '$perlexe' with parameter: '$sParams'\n");
264 $error = 1;
266 return $error;
268 # ------------------------------------------------------------------------------
269 our $sJavaExecutable;
270 sub setJavaExecutable($)
272 $sJavaExecutable = shift;
275 # sub getJava14()
277 # my $sJava14;
278 # if ($OSNAME eq "MSWin32")
280 # if ($sJavaExecutable)
282 # $sJava14 = $sJavaExecutable;
284 # else
286 # # HARDCODE!
287 # $sJava14 = "C:\\Programme\\Java\\j2re1.4.2_10\\bin\\java.exe";
290 # else
292 # if ($sJavaExecutable)
294 # $sJava14 = $sJavaExecutable;
296 # else
298 # # HARDCODE!
299 # $sJava14 = "/opt/java14/bin/java";
302 # if ( ! -e $sJava14 )
304 # log_print ("Java14 not found. Is searched in '$sJava14'\n");
305 # # exit(1);
306 # return "";
308 # return $sJava14;
310 # ------------------------------------------------------------------------------
311 sub getJava15()
313 my $sJava15;
314 if ($sJavaExecutable)
316 $sJava15 = $sJavaExecutable;
318 else
320 if ($OSNAME eq "MSWin32")
322 # HARDCODE!
323 $sJava15 = "C:\\Programme\\Java\\jre1.5.0_22\\bin\\java.exe";
324 if ( ! -e $sJava15)
326 $sJava15 = "C:\\Program Files\\Java\\jre6\\bin\\java.exe";
328 if ( ! -e $sJava15)
330 $sJava15 = "C:\\Java\\jdk1.6\\bin\\java.exe";
333 elsif ($OSNAME eq "cygwin")
335 $sJava15 = "java";
337 else
339 # HARDCODE!
340 if ($OSNAME eq "solaris")
342 $sJava15 = "/usr/bin/java";
344 else
346 $sJava15 = "/usr/bin/java";
347 if ( ! -e $sJava15 )
349 $sJava15 = "/opt/java15/bin/java";
353 if ( ! -e $sJava15 )
355 log_print ("Java15 not found. Is searched in '$sJava15'\n");
356 # exit(1);
357 return "";
360 return $sJava15;
362 # ------------------------------------------------------------------------------
363 sub getJava16()
365 my $sJava16;
366 if ($sJavaExecutable)
368 $sJava16 = $sJavaExecutable;
370 else
372 if ($OSNAME eq "MSWin32")
374 # HARDCODE!
375 $sJava16 = "C:\\Programme\\Java\\jre1.6.0_16\\bin\\java.exe";
376 if ( ! -e $sJava16)
378 $sJava16 = "C:\\Program Files\\Java\\jre6\\bin\\java.exe";
380 if ( ! -e $sJava16)
382 $sJava16 = "C:\\Java\\jdk1.6\\bin\\java.exe";
386 elsif ($OSNAME eq "cygwin")
388 # $sJava16 = "java";
389 $sJava16 = "C:/Program Files/Java/jdk1.6.0_16/bin/java.exe";
391 else
393 # HARDCODE!
394 if ($OSNAME eq "solaris")
396 $sJava16 = "/usr/bin/java";
398 else
400 $sJava16 = "/usr/bin/java";
401 if ( ! -e $sJava16 )
403 $sJava16 = "/opt/java16/bin/java";
407 if ( ! -e $sJava16 )
409 log_print ("Java16 not found. Is searched in '$sJava16'\n");
410 # exit(1);
411 return "";
414 return $sJava16;
417 # ------------------------------------------------------------------------------
418 sub getJavaExecutable()
420 return getJava16();
423 # ------------------------------------------------------------------------------
424 # this function is a helper for parameters
425 # if quotes the whole string with 'STR' or "STR" and replace quotes in it's content for the right.
426 sub singleQuote($)
428 my $sStr = shift;
429 if ( $OSNAME eq "MSWin32")
431 # we are MSWin32 (quote \" stronger)
432 # $sStr =~ s/\'/\"/g;
433 $sStr =~ s/\'/\\\"/g;
434 return "\"" . $sStr . "\"";
436 else
438 if (index($sStr, "'") >= 0)
440 # replace all single quotes ("'") by "\""
441 $sStr =~ s/\'/\"/g;
444 return "'" . $sStr . "'";
447 sub quote($)
449 my $sName = shift;
450 return "\"" . $sName . "\"";
453 sub quoteIfNeed($)
455 my $sName = shift;
456 if (-1 != index($sName, " "))
458 return quote($sName);
460 return $sName;
464 # ------------------------------------------------------------------------------
465 our $sToolsPath;
466 sub setToolsPath($)
468 my $sNewPath = shift;
469 $sToolsPath = $sNewPath;
472 sub ExecSQL($)
474 my $sSQL = shift;
476 my $error;
477 my @aResult;
478 my $sSQLDirect;
479 if ($sToolsPath)
481 $sSQLDirect = $sToolsPath;
482 $sSQLDirect .= "/";
484 $sSQLDirect .= "sql_direct.php";
486 # select(undef, undef, undef, 0.060);
487 # log_print("ExecSQL: $sSQL\n");
488 # sleep (1);
489 ($error, @aResult) = callphp(getPHPExecutable(), $sSQLDirect, singleQuote($sSQL));
490 if ($error)
492 log_print ("ExecSQL: An Error occured.\n");
493 log_print ("PHP: " . getPHPExecutable() . "\n");
494 log_print ("SQL Statement: " . singleQuote($sSQL) . "\n");
495 # exit(1);
497 # select(undef, undef, undef, 0.125);
498 # sleep (1);
499 return @aResult;
502 # ------------------------------------------------------------------------------
503 # helper to call external php with popen
504 # sub callexe($$$)
506 # local *IN_FILE;
507 # my $exe = shift;
508 # my $program = shift;
509 # my $sParams = shift;
510 # my $line;
511 # my $error;
512 # my @result;
514 # $exe = quoteIfNeed($exe);
515 # $program = quoteIfNeed($program);
517 # # print "Will send: $exe $sParams\n";
518 # # log_print("CALLEXE: $exe $program $sParams\n");
519 # if (open(IN_FILE, "$exe $program $sParams |"))
521 # while ($line = <IN_FILE>)
523 # chomp($line);
524 # # $line .= " ";
525 # push(@result, $line);
526 # # print "callphp output: $line\n";
528 # close(IN_FILE);
529 # $error = errorAdaption($?);
531 # else
533 # print "Can't popen '$exe' with parameter: '$sParams'\n";
534 # $error = 1;
536 # return $error, @result;