Pod/WWWBrowser.pm untested

filter: Statementstmt 0.0% 0 / 350 Branchbran 0.0% 0 / 252 Conditioncond 0.0% 0 / 89 MC/DCmcdc 0.0% 0 / 52 Subroutinesub 0.0% 0 / 32 Podpod 0.0% 0 / 23 totaltotal 0.0% 0 / 798 CC 125 SCAR 65.2
CC
125
Cov
0%
start_browser
2162.0
htmlview
182.0
start_browser_windows
110.0
CRAP 678.4
1 # DO NOT EDIT
2 # Created by the update-WWWBrowser makefile rule
3  
4 # DO NOT USE THIS MODULE IN YOUR PROJECTS
5 # (That is, the module's code is OK, but don't rely on the package
6 # name or the API of this module)
7 # -*- perl -*-
8  
9 #
10 # Author: Slaven Rezic
11 #
12 # Copyright (C) 1999,2000,2001,2003,2005,2006,2007,2008,2009,2012,2013,2014,2016,2017,2021 Slaven Rezic.
13 # All rights reserved.
14 # This package is free software; you can redistribute it and/or
15 # modify it under the same terms as Perl itself.
16 #
17 # Mail: slaven@rezic.de
18 # WWW: http://www.rezic.de/eserte/
19 #
20  
21 # TODO: check Win32::WebBrowser
22  
23 package # hide from PAUSE indexer
24 Tk::Pod::WWWBrowser;
25  
26 use strict;
27 use vars qw(@unix_browsers @available_browsers
28 @terminals @available_terminals
29 $VERSION $VERBOSE $initialized $os $fork
30 $ignore_config);
31  
32 my $no_process_checker_warned;
33  
34 $VERSION = 2.55;
35  
36 @available_browsers = qw(_debian_browser _internal_htmlview
37 _default_gnome _default_kde
38 htmlview
39 firefox seamonkey mozilla galeon konqueror netscape Netscape kfmclient
40 dillo w3m lynx
41 mosaic Mosaic
42 chimera arena tkweb
43 explorer);
44  
45 @unix_browsers = @available_browsers if !@unix_browsers;
46  
47 @available_terminals = qw(xterm konsole gnome-terminal rxvt Eterm kvt);
48  
49 @terminals = @available_terminals if !@terminals;
50  
51 init();
52  
53 sub init {
54 if (!$initialized) {
55 $os = ($^O eq 'MSWin32' || $^O eq 'os2' || $^O eq 'dos' ? 'win' :
56 $^O eq 'darwin' ? 'macosx' :
57 $^O eq 'MacOS' ? 'mac' :
58 'unix');
59 if (!defined &main::status_message) {
60 eval 'sub status_message { warn $_[0] }';
61 } else {
62 eval 'sub status_message { main::status_message(@_) }';
63 }
64 $fork = 1;
65 $initialized++;
66 get_from_config();
67 }
68 }
69  
70 sub start_browser {
71 my $url = shift;
72 my(%args) = @_;
73  
74 if ($os eq 'win') {
75 return start_browser_windows($url, %args);
76 }
77  
78 if ($os eq 'macosx') {
79 exec_bg("open", $url);
80 return 1;
81 }
82  
83 my @browsers = @unix_browsers;
84 if ($args{-browser}) {
85 unshift @browsers, delete $args{-browser};
86 }
87  
88 foreach my $browser (@browsers) {
89 if ($VERBOSE && $VERBOSE >= 2) {
90 warn "Try $browser ...\n";
91 }
92  
93 last if $browser eq '_last';
94  
95 next if ($browser !~ /^_/ && !is_in_path($browser));
96 if ($browser =~ /^(lynx|w3m)$/) { # text-orientierte Browser
97 return 1 if open_in_terminal($browser, $url, %args);
98 next;
99 }
100  
101 if ((!defined $ENV{DISPLAY} || $ENV{DISPLAY} eq '') &&
102 $^O ne 'cygwin') {
103 next;
104 }
105 # After this point only X11 browsers or cygwin as a special case
106  
107 my $url = $url;
108 if ($browser eq '_default_gnome') {
109 eval {
110 my $cmdline = _get_cmdline_for_url_from_Gnome($url);
111 exec_bg($cmdline);
112 };
113 if (!$@) {
114 return 1; # we're done;
115 } else {
116 if ($VERBOSE && $VERBOSE >= 2) {
117 warn "Can't exexute default gnome browser: $@";
118 }
119 }
120 } elsif ($browser eq '_default_kde') {
121 # NYI
122 } elsif ($browser =~ /^_exec\b(.*)/) {
123 my $cmdline = $1;
124 $cmdline =~ s/^\s+//;
125 my @exec_bg_args;
126 if ($cmdline =~ /\%s/) {
127 @exec_bg_args = sprintf $cmdline, $url;
128 } else {
129 @exec_bg_args = (split(/\s+/, $cmdline), $url);
130 }
131 warn "EXEC: @exec_bg_args"; # XXX make conditional!
132 exec_bg(@exec_bg_args);
133 return 1;
134 } elsif ($browser eq 'konqueror') {
135 return 1 if open_in_konqueror($url, %args);
136 } elsif ($browser eq 'galeon') {
137 return 1 if open_in_galeon($url, %args);
138 } elsif ($browser eq 'seamonkey') {
139 return 1 if open_in_seamonkey($url, %args);
140 } elsif ($browser eq 'mozilla') {
141 return 1 if open_in_mozilla($url, %args);
142 } elsif ($browser eq 'opera') {
143 return 1 if open_in_opera($url, %args);
144 } elsif ($browser =~ /^mosaic$/i &&
145 $url =~ /^file:/ && $url !~ m|file://|) {
146 $url =~ s|file:/|file://localhost/|;
147 } elsif ($browser eq 'kfmclient') {
148 # kfmclient loads kfm, which loads and displays all KDE icons
149 # on the desktop, even if KDE is not running at all.
150 exec_bg("kfmclient", "openURL", $url);
151 return 1 if (!$?)
152 } elsif ($browser eq 'netscape') {
153 if ($os eq 'unix') {
154 my $lockfile = "$ENV{HOME}/.netscape/lock";
155 if (-l $lockfile) {
156 my($host,$pid) = readlink($lockfile) =~ /^(.*):(\d+)$/;
157 # XXX check $host
158 # Check whether Netscape stills lives:
159 if (defined $pid && kill 0 => $pid) {
160 if ($args{-oldwindow}) {
161 exec_bg("netscape", "-remote", _openurl_cmd($url));
162 } else {
163 exec_bg("netscape", "-remote", _openurl_cmd($url,"new"));
164 }
165 # XXX further options: mailto(to-adresses)
166 # XXX check return code?
167 return 1;
168 }
169 }
170 exec_bg("netscape", $url);
171 return 1;
172 }
173 } elsif ($browser eq '_internal_htmlview') {
174 my $ret = eval {
175 htmlview($url);
176 };
177 if ($@) {
178 warn $@;
179 next;
180 } elsif ($ret) {
181 return $ret;
182 }
183 } elsif ($browser eq '_debian_browser') {
184 if (-x "/usr/bin/sensible-browser") {
185 exec_bg("/usr/bin/sensible-browser", $url);
186 return 1;
187 } else {
188 if ($ENV{DISPLAY}) {
189 if (-x "/etc/alternatives/gnome-www-browser") { # usually firefox or mozilla
190 exec_bg("/etc/alternatives/gnome-www-browser", $url); # use additional args if mozilla, learn args for firefox
191 return 1;
192 } elsif (-x "/etc/alternatives/x-www-browser") { # usually dillo
193 exec_bg("/etc/alternatives/x-www-browser", $url);
194 return 1;
195 }
196 } else {
197 if (-x "/etc/alternatives/www-browser") {
198 return 1 if open_in_terminal("/etc/alternatives/www-browser", $url, %args);
199 }
200 }
201 }
202 } else {
203 exec_bg($browser, $url);
204 return 1;
205 }
206 }
207  
208 if ($^O eq 'cygwin') {
209 return 1 if start_windows_browser_cygwin($url, %args);
210 }
211  
212 status_message("Can't find HTML viewer.", "err");
213  
214 return 0;
215 }
216  
217 sub start_windows_browser_cygwin {
218 my($url, %args) = @_;
219 _system("cmd", "/c", "start", $url);
220 if ($? == 0) {
221 return 1;
222 } else {
223 return 0;
224 }
225 }
226  
227 sub start_browser_windows {
228 my($url, %args) = @_;
229 my @methods;
230 if ($ENV{OS} && $ENV{OS} eq 'Windows_NT') { # NT, 2000, XP, Vista...
231 @methods = qw(rundll start win32util explorer);
232 } else {
233 @methods = qw(win32util start explorer);
234 }
235  
236 for my $method (@methods) {
237 if ($method eq 'rundll') {
238 _system("rundll32 url.dll,FileProtocolHandler \"$url\"");
239 if ($?/256 == 0) {
240 return 1;
241 }
242 } elsif ($method eq 'start') {
243 # XXX There are reports that "start" and Tk programms
244 # do not work well together (slow startup and such).
245 _system("start /b \"$url\"");
246 if ($?/256 == 0) {
247 return 1;
248 }
249 } elsif ($method eq 'explorer') {
250 _system("start explorer \"$url\"");
251 # maybe: system("start", "explorer", $url);
252 # or: system("start explorer \"$url\"");
253 if ($?/256 == 0) {
254 return 1;
255 }
256 } elsif ($method eq 'win32util' &&
257 eval { require Win32Util; 1 }) {
258 if (eval { Win32Util::start_html_viewer($url) }) {
259 return 1;
260 }
261 }
262 }
263  
264 status_message("Can't find HTML viewer.", "err");
265 return 0;
266 }
267  
268 sub open_in_konqueror {
269 my $url = shift;
270 my(%args) = @_;
271 if (is_in_path("dcop") && is_in_path("konqueror")) {
272  
273 # first try old window (if requested)
274 if ($args{-oldwindow}) {
275 my $konq_name;
276 foreach my $l (split /\n/, `dcop konqueror KonquerorIface getWindows`) {
277 if ($l =~ /(konqueror-mainwindow\#\d+)/) {
278 $konq_name = $1;
279 last;
280 }
281 }
282  
283 if (defined $konq_name) {
284 _system(qw/dcop konqueror/, $konq_name, qw/openURL/, $url);
285 return 1 if ($?/256 == 0);
286 }
287 }
288  
289 # then try to send to running konqueror process:
290 _system(qw/dcop konqueror KonquerorIface openBrowserWindow/, $url);
291 return 1 if ($?/256 == 0);
292  
293 # otherwise start a new konqueror
294 exec_bg("konqueror", $url);
295 return 1; # if ($?/256 == 0);
296 }
297 0;
298 }
299  
300 sub open_in_galeon {
301 my $url = shift;
302 my(%args) = @_;
303 if (is_in_path("galeon")) {
304  
305 $url = _guess_and_expand_url($url) if $args{-expandurl};
306  
307 # first try old window (if requested)
308 if ($args{-oldwindow}) {
309 _system("galeon", "-x", $url);
310 return 1 if ($?/256 == 0);
311 }
312  
313 exec_bg("galeon", "-n", $url);
314 return 1 if ($?/256 == 0);
315 return 0;
316 }
317 0;
318 }
319  
320 sub _open_in_mozilloid {
321 my($cmd, $url, %args) = @_;
322 if (is_in_path($cmd)) {
323 if ($args{-oldwindow}) {
324 _system($cmd, "-remote", _openurl_cmd($url));
325 } else {
326 # no new-tab support in older Mozillas (e.g. 1.0)!
327 _system($cmd, "-remote", _openurl_cmd($url,"new-tab"));
328 }
329 return 1 if ($?/256 == 0);
330  
331 # otherwise start a new mozilla process
332 exec_bg($cmd, $url);
333 return 1; # if ($?/256 == 0);
334 }
335 0;
336 }
337  
338 sub open_in_mozilla {
339 _open_in_mozilloid("mozilla", @_);
340 }
341  
342 sub open_in_seamonkey {
343 _open_in_mozilloid("seamonkey", @_);
344 }
345  
346 sub open_in_opera {
347 my $url = shift;
348 my(%args) = @_;
349 if (is_in_path("opera")) {
350 if ($args{-oldwindow}) {
351 exec_bg("opera", $url);
352 } else {
353 exec_bg("opera", "-newpage", $url);
354 }
355 return 1; # if ($?/256 == 0);
356 }
357 0;
358 }
359  
360 sub exec_bg {
361 my(@cmd) = @_;
362 warn "Execute: @cmd\n" if $VERBOSE && $VERBOSE >= 2;
363 if ($os eq 'unix' || $os eq 'macosx') {
364 eval {
365 my $pid = fork;
366 if (!defined $pid) {
367 die "Fork failed: $!";
368 }
369 if ($pid == 0) {
370 my $pid2 = fork;
371 if (!defined $pid2) {
372 _hard_die("Inner fork failed: $!");
373 }
374 if ($pid2 == 0) {
375 exec(@cmd)
376 or _hard_die("Can't exec @cmd: $!");
377 }
378 _hard_exit(0);
379 }
380 waitpid $pid, 0;
381 };
382 warn "Forking/executing @cmd failed: $@" if $@;
383 } else {
384 # XXX use Spawn
385 _system(join(" ", @cmd) . ($fork ? "&" : ""));
386 }
387 }
388  
389 sub _hard_die {
390 my $msg = shift;
391 warn $msg;
392 _hard_exit(1);
393 }
394  
395 sub _hard_exit {
396 my $code = shift;
397 if (eval { require POSIX; 1 }) {
398 POSIX::_exit($code);
399 } else {
400 CORE::exit($code);
401 }
402 }
403  
404 sub _system {
405 my(@cmd) = @_;
406 warn "Execute: @cmd\n" if $VERBOSE && $VERBOSE >= 2;
407 system @cmd;
408 }
409  
410 sub _get_cmdline_for_url_from_Gnome {
411 my($url) = @_;
412 (my $url_scheme = $url) =~ s/^([^:]+).*/$1/; # use URI.pm?
413 my $curr_section;
414 my $default_cmdline;
415 my $cmdline;
416 if (open(GNOME, "$ENV{HOME}/.gnome/Gnome")) {
417 while(<GNOME>) {
418 chomp;
419 if (/^\[(.*)\]/) {
420 $curr_section = $1;
421 } elsif (defined $curr_section && $curr_section eq 'URL Handlers' && /^(default|\Q$url_scheme\E)-show=(.*)/) {
422 if ($1 eq 'default') {
423 $default_cmdline = $2;
424 } else {
425 $cmdline = $2;
426 }
427 }
428 }
429 close GNOME;
430 }
431 if (!defined $cmdline) {
432 $cmdline = $default_cmdline;
433 }
434 if (!defined $cmdline) {
435 die "Can't find command for scheme $url_scheme";
436 }
437 $cmdline =~ s/%s/$url/g;
438 $cmdline;
439 }
440  
441 # XXX document get_from_config, $ignore_config, ~/.wwwbrowser
442 sub get_from_config {
443 if (!$ignore_config && $ENV{HOME} && open(CFG, "$ENV{HOME}/.wwwbrowser")) {
444 my @browser;
445 while(<CFG>) {
446 chomp;
447 next if /^\s*\#/;
448 push @browser, $_;
449 }
450 close CFG;
451 unshift @unix_browsers, @browser;
452 }
453 }
454  
455 sub _guess_and_expand_url {
456 my $url = shift;
457 if ($url =~ m|^[a-z]+://|) {
458 $url;
459 } elsif ($url =~ m|^www|) {
460 "http://$url";
461 } elsif ($url =~ m|^ftp|) {
462 "ftp://$url";
463 } else {
464 $url;
465 }
466 }
467  
468 # A port of htmlview to perl.
469 #
470 # This may work on linux, but not on *BSD because of different paths.
471 sub htmlview {
472 #!/bin/bash
473 #
474 # Invoke whatever HTML viewer is installed...
475 # Usage:
476 # htmlview [URL]
477 #
478 # Changes:
479 # v2.0.0
480 # ------
481 # - Allow users to override default settings in
482 # ~/.htmlviewrc and /etc/htmlview.conf.
483 # Users can define X11BROWSER, TEXTBROWSER and
484 # CONSOLE variables to indicate their preferences.
485 # - --remote and --local are deprecated, we don't
486 # have any non-browser HTML viewers these days.
487 #
488 # Christopher Blizzard <blizzard@redhat.com> Aug 09 2002
489 # - prefer mozilla over galeon
490 #
491 # written by Bernhard Rosenkraenzer <bero@redhat.com>
492 # (c) 2000-2002 Red Hat, Inc.
493 #
494 # This script is in the public domain.
495  
496 #XXXunset BROWSER CONSOLE TERMS_KDE TERMS_GNOME TERMS_GENERIC
497 #XXX[ -e /etc/htmlview.conf ] && source /etc/htmlview.conf
498 #XXX[ -e ~/.htmlviewrc ] && source ~/.htmlviewrc
499  
500 return 0 if $^O ne "linux";
501  
502 my(@args) = @_;
503  
504 my @TERMS_KDE = qw(/usr/bin/konsole /usr/bin/kvt);
505 my @TERMS_GNOME = qw(/usr/bin/gnome-terminal);
506 my @TERMS_GENERIC = qw(/usr/bin/rxvt /usr/X11R6/bin/xterm /usr/bin/Eterm);
507 my @TTYBROWSERS = qw(/usr/bin/links /usr/bin/lynx /usr/bin/w3m);
508 my @X11BROWSERS_KDE = qw(/usr/bin/konqueror /usr/bin/kfmbrowser);
509 my @X11BROWSERS_GNOME = qw(/usr/bin/mozilla /usr/bin/galeon);
510 my @X11BROWSERS_GENERIC = qw(/usr/bin/mozilla /usr/bin/netscape);
511  
512 my(@X11BROWSERS, @TERMS);
513  
514 my $gnome_is_running = _process_is_running('gnome-session');
515  
516 if ($gnome_is_running) {
517 @X11BROWSERS = (@X11BROWSERS_GENERIC, @X11BROWSERS_GNOME, @X11BROWSERS_KDE);
518 @TERMS = (@TERMS_GENERIC, @TERMS_GNOME, @TERMS_KDE);
519 } else {
520 @X11BROWSERS = (@X11BROWSERS_GENERIC, @X11BROWSERS_KDE, @X11BROWSERS_GNOME);
521 @TERMS = (@TERMS_GENERIC, @TERMS_KDE, @TERMS_GNOME);
522 }
523  
524 if ($ENV{X11BROWSER}) {
525 unshift @X11BROWSERS, $ENV{X11BROWSER};
526 }
527 if ($ENV{TEXTBROWSER}) {
528 unshift @TTYBROWSERS, $ENV{TEXTBROWSER};
529 }
530 if ($ENV{CONSOLE}) {
531 unshift @TERMS, $ENV{CONSOLE};
532 }
533  
534 if ($VERBOSE) {
535 require Data::Dumper;
536 print STDERR Data::Dumper->new([\@X11BROWSERS, \@TTYBROWSERS, \@TERMS],
537 [qw(X11BROWSERS TTYBROWSERS TERMS)])
538 ->Indent(1)->Useqq(1)->Dump;
539 }
540  
541 TRY: {
542 if (!defined $ENV{DISPLAY} || $ENV{DISPLAY} eq "") {
543 for my $ttybrowser (@TTYBROWSERS) {
544 if (is_in_path($ttybrowser)) {
545 _system($ttybrowser, @args); # blocks in tty mode
546 last TRY;
547 }
548 }
549  
550 die "No valid text mode browser found.\n";
551 } else {
552 for my $x11browser (@X11BROWSERS) {
553 if (is_in_path($x11browser)) {
554 exec_bg($x11browser, @args);
555 last TRY;
556 }
557 }
558  
559 my @console;
560 for my $term (@TERMS) {
561 if (is_in_path($term)) {
562 @console = ($term, "-e");
563 last;
564 }
565 }
566  
567 if (!@console) {
568 die "No CONSOLE found.\n";
569 }
570  
571 for my $ttybrowser (@TTYBROWSERS) {
572 if (is_in_path($ttybrowser)) {
573 exec_bg(@console, $ttybrowser, @args);
574 last TRY;
575 }
576 }
577 }
578  
579 die "No valid browser found.\n";
580 }
581  
582 return 1;
583 }
584  
585 sub open_in_terminal {
586 my($browser, $url, %args) = @_;
587  
588 if (defined $ENV{DISPLAY} && $ENV{DISPLAY} ne "") {
589 foreach my $term (@terminals) {
590 if (is_in_path($term)) {
591 exec_bg($term,
592 ($term eq 'gnome_terminal' ? '-x' : '-e'),
593 $browser, $url);
594 return 1;
595 }
596 }
597 } else {
598 # without X11: not in background!
599 _system($browser, $url);
600 return 1;
601 }
602 0;
603 }
604  
605 sub _openurl_cmd {
606 my($url, @args) = @_;
607 $url =~ s{,}{%2c}g; # collides with openURL argument separator
608 $url =~ s{\)}{%29}g; # collides with openURL function end token
609 "openURL($url" . (@args ? "," . join(",", @args) : "") . ")";
610 }
611  
612 sub _process_is_running {
613 my $processname = shift;
614  
615 if (eval { require Proc::ProcessTable }) {
616 require File::Basename;
617 for my $p (@{ Proc::ProcessTable->new->table }) {
618 if (File::Basename::basename($p->fname) eq $processname) {
619 return 1;
620 }
621 }
622 return 0;
623 }
624  
625 for my $check_prog (qw(/bin/pidof /sbin/pidof /usr/bin/pgrep)) {
626 if (-x $check_prog) {
627 my($out) = `$check_prog $processname`;
628 return $out ? 1 : 0;
629 }
630 }
631  
632 if (!$no_process_checker_warned) {
633 warn "Cannot determine whether $processname is running: neither Proc::ProcessTable nor pidof nor pgrep are available\n";
634 $no_process_checker_warned = 1;
635 }
636  
637 undef;
638 }
639  
640 # REPO BEGIN
641 # REPO NAME file_name_is_absolute /home/e/eserte/src/repository
642 # REPO MD5 89d0fdf16d11771f0f6e82c7d0ebf3a8
643 BEGIN {
644 if (eval { require File::Spec; defined &File::Spec::file_name_is_absolute }) {
645 *file_name_is_absolute = \&File::Spec::file_name_is_absolute;
646 } else {
647 *file_name_is_absolute = sub {
648 my $file = shift;
649 my $r;
650 if ($^O eq 'MSWin32') {
651 $r = ($file =~ m;^([a-z]:(/|\\)|\\\\|//);i);
652 } else {
653 $r = ($file =~ m|^/|);
654 }
655 $r;
656 };
657 }
658 }
659 # REPO END
660  
661 # REPO BEGIN
662 # REPO NAME is_in_path /home/e/eserte/src/repository
663 # REPO MD5 81c0124cc2f424c6acc9713c27b9a484
664 sub is_in_path {
665 my($prog) = @_;
666 return $prog if (file_name_is_absolute($prog) and -f $prog and -x $prog);
667 require Config;
668 my $sep = $Config::Config{'path_sep'} || ':';
669 foreach (split(/$sep/o, $ENV{PATH})) {
670 if ($^O eq 'MSWin32') {
671 # maybe use $ENV{PATHEXT} like maybe_command in ExtUtils/MM_Win32.pm?
672 return "$_\\$prog"
673 if (-x "$_\\$prog.bat" ||
674 -x "$_\\$prog.com" ||
675 -x "$_\\$prog.exe" ||
676 -x "$_\\$prog.cmd");
677 } else {
678 return "$_/$prog" if (-x "$_/$prog" && !-d "$_/$prog");
679 }
680 }
681 undef;
682 }
683 # REPO END
684  
685  
686 1;
687