Pod/WWWBrowser.pm untested

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