1#!/usr/local/bin/perl
2#***************************************************************************
3#                                  _   _ ____  _
4#  Project                     ___| | | |  _ \| |
5#                             / __| | | | |_) | |
6#                            | (__| |_| |  _ <| |___
7#                             \___|\___/|_| \_\_____|
8#
9# Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
10#
11# This software is licensed as described in the file COPYING, which
12# you should have received as part of this distribution. The terms
13# are also available at http://curl.haxx.se/docs/copyright.html.
14#
15# You may opt to use, copy, modify, merge, publish, distribute and/or sell
16# copies of the Software, and permit persons to whom the Software is
17# furnished to do so, under the terms of the COPYING file.
18#
19# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20# KIND, either express or implied.
21#
22###########################################################################
23
24# Yeah, I know, probably 1000 other persons already wrote a script like
25# this, but I'll tell ya:
26
27# THEY DON'T FIT ME :-)
28
29# Get readme file as parameter:
30
31if($ARGV[0] eq "-c") {
32    $c=1;
33    shift @ARGV;
34}
35
36my $README = $ARGV[0];
37
38if($README eq "") {
39    print "usage: mkreadme.pl [-c] <README> < manpage\n";
40    exit;
41}
42
43
44push @out, "                                  _   _ ____  _\n";
45push @out, "  Project                     ___| | | |  _ \\| |\n";
46push @out, "                             / __| | | | |_) | |\n";
47push @out, "                            | (__| |_| |  _ <| |___\n";
48push @out, "                             \\___|\\___/|_| \\_\\_____|\n";
49
50my $olen=0;
51while (<STDIN>) {
52    my $line = $_;
53
54    # this should be removed:
55    $line =~ s/(.|_)//g;
56
57    if($line =~ /^([ \t]*\n|curl)/i) {
58        # cut off headers and empty lines
59        $wline++; # count number of cut off lines
60        next;
61    }
62
63    my $text = $line;
64    $text =~ s/^\s+//g; # cut off preceding...
65    $text =~ s/\s+$//g; # and trailing whitespaces
66
67    $tlen = length($text);
68
69    if($wline && ($olen == $tlen)) {
70        # if the previous line with contents was exactly as long as
71        # this line, then we ignore the newlines!
72
73        # We do this magic because a header may abort a paragraph at
74        # any line, but we don't want that to be noticed in the output
75        # here
76        $wline=0;
77    }
78    $olen = $tlen;
79
80    if($wline) {
81        # we only make one empty line max
82        $wline = 0;
83        push @out, "\n";
84    }
85    push @out, $line;
86}
87push @out, "\n"; # just an extra newline
88
89open(READ, "<$README") ||
90    die "couldn't read the README infile $README";
91
92while(<READ>) {
93    push @out, $_;
94}
95close(READ);
96
97# if compressed
98if($c) {
99    my @test = `gzip --version 2>&1`;
100    if($test[0] =~ /gzip/) {
101        open(GZIP, ">dumpit") ||
102            die "can't create the dumpit file, try without -c";
103        binmode GZIP;
104        for(@out) {
105            print GZIP $_;
106            $gzip += length($_);
107        }
108        close(GZIP);
109
110        system("gzip --best --no-name dumpit");
111
112        open(GZIP, "<dumpit.gz") ||
113             die "can't read the dumpit.gz file, try without -c";
114        binmode GZIP;
115        while(<GZIP>) {
116            push @gzip, $_;
117            $gzipped += length($_);
118        }
119        close(GZIP);
120
121        unlink("dumpit.gz");
122    }
123    else {
124        # no gzip, no compression!
125        undef $c;
126        print STDERR "MEEEP: Couldn't find gzip, disable compression\n";
127    }
128}
129
130$now = localtime;
131print <<HEAD
132/*
133 * NEVER EVER edit this manually, fix the mkhelp.pl script instead!
134 * Generation time: $now
135 */
136#include "setup.h"
137#ifdef USE_MANUAL
138#include "hugehelp.h"
139#include <stdio.h>
140HEAD
141    ;
142if($c) {
143    print <<HEAD
144#include <stdlib.h>
145#include <zlib.h>
146static const unsigned char hugehelpgz[] = {
147  /* This mumbo-jumbo is the huge help text compressed with gzip.
148     Thanks to this operation, the size of this data shrunk from $gzip
149     to $gzipped bytes. You can disable the use of compressed help
150     texts by NOT passing -c to the mkhelp.pl tool. */
151HEAD
152;
153    my $c=0;
154    print " ";
155    for(@gzip) {
156        my @all=split(//, $_);
157        for(@all) {
158            my $num=ord($_);
159            printf(" 0x%02x,", 0+$num);
160            if(++$c>11) {
161                print "\n ";
162                $c=0;
163            }
164        }
165    }
166    print "\n};\n";
167
168    print <<EOF
169#define BUF_SIZE 0x10000
170/* Decompress and send to stdout a gzip-compressed buffer */
171void hugehelp(void)
172{
173  unsigned char* buf;
174  int status,headerlen;
175  z_stream z;
176
177  /* Make sure no gzip options are set */
178  if (hugehelpgz[3] & 0xfe)
179    return;
180
181  headerlen = 10;
182  z.avail_in = (unsigned int)(sizeof(hugehelpgz) - headerlen);
183  z.next_in = (unsigned char *)hugehelpgz + headerlen;
184  z.zalloc = (alloc_func)Z_NULL;
185  z.zfree = (free_func)Z_NULL;
186  z.opaque = 0;
187
188  if (inflateInit2(&z, -MAX_WBITS) != Z_OK)
189    return;
190
191  buf = malloc(BUF_SIZE);
192  if (buf) {
193    while(1) {
194      z.avail_out = BUF_SIZE;
195      z.next_out = buf;
196      status = inflate(&z, Z_SYNC_FLUSH);
197      if (status == Z_OK || status == Z_STREAM_END) {
198        fwrite(buf, BUF_SIZE - z.avail_out, 1, stdout);
199        if (status == Z_STREAM_END)
200          break;
201      }
202      else
203        break;    /* Error */
204    }
205    free(buf);
206  }
207  inflateEnd(&z);
208}
209EOF
210    ;
211foot();
212exit;
213}
214else {
215    print <<HEAD
216void hugehelp(void)
217{
218   fputs(
219HEAD
220         ;
221}
222
223$outsize=0;
224for(@out) {
225    chop;
226
227    $new = $_;
228
229    $outsize += length($new)+1; # one for the newline
230
231    $new =~ s/\\/\\\\/g;
232    $new =~ s/\"/\\\"/g;
233
234    # gcc 2.96 claims ISO C89 only is required to support 509 letter strings
235    if($outsize > 500) {
236        # terminate and make another fputs() call here
237        print ", stdout);\n fputs(\n";
238        $outsize=length($new)+1;
239    }
240    printf("\"%s\\n\"\n", $new);
241
242}
243
244print ", stdout) ;\n}\n";
245
246foot();
247
248sub foot {
249  print <<FOOT
250#endif /* USE_MANUAL */
251FOOT
252  ;
253}
254