~noskcaj/ubuntu/saucy/dhelp/apache2.4

« back to all changes in this revision

Viewing changes to src/dhelp

  • Committer: Charlie Smotherman
  • Date: 2012-11-05 23:48:50 UTC
  • mfrom: (22.1.1 dhelp)
  • Revision ID: cjsmo@cableone.net-20121105234850-4a447ob81bs9a5je
Tags: 0.6.21+nmu1ubuntu1
* Merge from Debian unstable. Remaining changes:
  - lib/dhelp.rb:
    + Exit and return zero code if bdb isn't available; this usually
      indicates that dhelp is not configured yet.
* Non-Maintainer Upload
* Dropped the declaration of dependence on ruby-commandline, which was
  already done on the code
* New maintainer: Georgios M. Zarkadas <gz@member.fsf.org> (Closes: #650441). 
* Support other web servers in addition to apache2 (Closes: #669041).
* Support apache2 packaging transition for version 2.4 (Closes: #669758).
* Support new ruby packaging policy transition for Wheezy.
* Use OptionParser instead Commandline::Application (Closes: #678055).
  Thanks Gunnar Wolf.
* Support new layout of man2html cgi scripts for Wheezy.
* Keep supporting previous policies/layouts, either during build time or
  during runtime, to aid backporting.
* Man and info pages links are activated only if associated packages are
  installed on the system.
* Subsections now show in the sections list only if section is selected.
* New color, styles and icons themes.
* Package installation now does not fail if cache data cannot be generated
  during install.
* Fix some minor lintian warnings.
* Bump Standards-Version to 3.9.3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl -w
 
2
#
 
3
# Copyright 2002 by Stefan Hornburg (Racke) <racke@linuxia.de>
 
4
#
 
5
# Based on a sample implementation of Chris Tillman
 
6
# <tillman@azstarnet.com>.
 
7
#
 
8
# This program is free software; you can redistribute it and/or modify
 
9
# it under the terms of the GNU General Public License as published by
 
10
# the Free Software Foundation; either version 2 of the License, or
 
11
# (at your option) any later version.
 
12
#
 
13
# This program is distributed in the hope that it will be useful,
 
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
# GNU General Public License for more details.
 
17
#
 
18
# You should have received a copy of the GNU General Public
 
19
# License along with this program; if not, write to the Free
 
20
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 
21
# MA  02111-1307  USA.
 
22
 
 
23
use strict;
 
24
use warnings;
 
25
 
 
26
use File::Spec;
 
27
use File::Temp qw(tempfile);
 
28
use IO::Socket;
 
29
use Getopt::Long;
 
30
use Pod::Usage;
 
31
use URI::Escape;
 
32
 
 
33
# version (replaced on packaging time)
 
34
my $version = '__VERSION__';
 
35
 
 
36
sub fatal_error {
 
37
    chomp(my $msg = shift );
 
38
    print STDERR $msg, "\n";
 
39
    sleep 3;
 
40
    exit 1;
 
41
}
 
42
 
 
43
# check if there is a CGI capable WWW server running on the localhost, and
 
44
# return some identification string for it if any found. Return '' otherwise
 
45
sub detected_local_webserver {
 
46
    my $testdoc = "/doc/HTML/index.html";
 
47
    my $eol = "\015\012";
 
48
    my $blank = $eol x 2;
 
49
    my $sock = IO::Socket::INET->new('127.0.0.1:80');
 
50
    my $httpd_running = '';
 
51
 
 
52
    if ($sock) {
 
53
        $sock->autoflush(1);
 
54
        print $sock "HEAD $testdoc HTTP/1.0$eol";
 
55
        print $sock "Host: localhost" . $blank;
 
56
 
 
57
        while (my $line = <$sock>) {
 
58
            if ($line =~ s/^Server: //) {
 
59
                $httpd_running = $line;
 
60
                $httpd_running =~ s/\r?\n?//go;
 
61
            }
 
62
        }
 
63
        close $sock;
 
64
 
 
65
        if ($httpd_running =~ /dhttpd/) {
 
66
            # this server is not CGI capable
 
67
            $httpd_running = '';
 
68
        }
 
69
    }
 
70
 
 
71
    return $httpd_running;
 
72
}
 
73
 
 
74
# process commandline options
 
75
my %opts;
 
76
my $whandler = $SIG{__WARN__};
 
77
$SIG{__WARN__} = sub {print STDERR "$0: @_";};
 
78
unless (GetOptions(\%opts,
 
79
                   'file|f',
 
80
                   'help|h',
 
81
                   'version')) {
 
82
    fatal_error(pod2usage(1));
 
83
}
 
84
 
 
85
if ($opts{help}) {
 
86
    pod2usage(1);
 
87
    exit 0;
 
88
} elsif ($opts{version}) {
 
89
    print "dhelp version $version\n";
 
90
    exit 0;
 
91
}
 
92
 
 
93
my $searchterm = join(" ", @ARGV);
 
94
 
 
95
# home directory of the current user
 
96
my $homedir;
 
97
 
 
98
if (exists $ENV{'HOME'} && -d $ENV{'HOME'}) {
 
99
    $homedir = $ENV{'HOME'};
 
100
} else {
 
101
    $homedir = (getpwent()) [7];
 
102
}
 
103
 
 
104
# always use sensible-browser
 
105
my $browser = "/usr/bin/sensible-browser";
 
106
 
 
107
my $online_mode = ! $opts{file};
 
108
my $httpd_running = detected_local_webserver;
 
109
 
 
110
my $document;
 
111
 
 
112
if ($online_mode && $httpd_running) {
 
113
    # we can query the web server directly
 
114
    if ($searchterm) {
 
115
        my $searchterm_uri = uri_escape($searchterm);
 
116
        $searchterm_uri =~ s/'/'\\''/go;
 
117
        $document = "http://localhost/cgi-bin/dsearch?search=$searchterm_uri";
 
118
    } else {
 
119
        $document = "http://localhost/doc/HTML/index.html";
 
120
    }
 
121
    print "Starting browser (using HTTP $httpd_running) ...\n";
 
122
} else {
 
123
    if ($searchterm) {
 
124
        my ($basedir) = File::Spec->tmpdir();
 
125
        my ($fh, $tmpfile) = tempfile('dhelp' . 'X' x 6,
 
126
                                      DIR    => $basedir,
 
127
                                      SUFFIX => '.html',
 
128
                                      UNLINK => 1);
 
129
        print "Starting dsearch for $searchterm\n";
 
130
        $searchterm =~ s/'/'\\''/go;
 
131
        # call dsearch
 
132
        open (DSEARCH, "/usr/lib/cgi-bin/dsearch file=1 search='$searchterm'|");
 
133
        while (<DSEARCH>) {
 
134
            print $fh $_;
 
135
        }
 
136
        close (DSEARCH) || fatal_error "$0: dsearch failed\n";
 
137
        system ( "$browser $tmpfile" ) and fatal_error( "${browser}: Failed to open $tmpfile: $!\n" );
 
138
        exit 0;
 
139
    } else {
 
140
        $document = "/usr/share/doc/HTML/index.html";
 
141
        print "Starting browser (using local filesystem) ...\n";
 
142
    }
 
143
}
 
144
 
 
145
system("$browser '$document'") and fatal_error("${browser}: Failed to open $document: $!\n");
 
146
 
 
147
__END__
 
148
 
 
149
 
 
150
=head1 NAME
 
151
 
 
152
dhelp - Accessing Debian Online Help System
 
153
 
 
154
=head1 SYNOPSIS
 
155
 
 
156
   dhelp [ -h | -v | search-term ]
 
157
   dhelp -f
 
158
 
 
159
=head1 OPTIONS
 
160
 
 
161
=over 8
 
162
 
 
163
=item B<-f, --file>
 
164
 
 
165
Direct the browser to use the local file system instead of
 
166
contacting the local WWW server.
 
167
 
 
168
=item B<-h, --help>
 
169
 
 
170
Show a brief help message and exit.
 
171
 
 
172
=item B<-v, --version>
 
173
 
 
174
Show the program version number and exit.
 
175
 
 
176
=back
 
177
 
 
178
=head1 DESCRIPTION
 
179
 
 
180
B<dhelp> presents a list of installed html documentation. The
 
181
list can be browsed directly with Lynx, or if a web server
 
182
is installed then any web browser can be used.
 
183
 
 
184
In addition, you can search for terms indexed in the documentation
 
185
using B<dhelp search-term> .
 
186
 
 
187
=cut
 
188
 
 
189